Skip to content

PGBot for macOS (desktop/macos) + pgbot sql run - #171

Open
alexshapalov wants to merge 8 commits into
mainfrom
feat/macos-app
Open

alexshapalov wants to merge 8 commits into
mainfrom
feat/macos-app

Conversation

@alexshapalov

Copy link
Copy Markdown
Contributor

Summary

A native SwiftUI app for macOS under desktop/macos: one chat-first bot per PostgreSQL database, built entirely on the existing engine's machine contracts. It also adds one engine command, pgbot sql run, which powers the app's new PSQL mode.

Commits

  1. feat(sql): pgbot sql run runs your own SQL read-only. Output is psql-style aligned text, or --json (sql_run_schema_version 1.0.0).
    • Everything runs in one BEGIN READ ONLY transaction that is always rolled back.
    • Before anything is sent, it refuses transaction control, COPY, dblink, large-object file I/O, pg_terminate_backend, pg_reload_conf, stats resets, replication slots, VACUUM and LOCK.
    • Statements go over the extended protocol, one statement per call, with statement and lock timeouts and a server-side --max-rows.
  2. feat(desktop): PGBotKit is the headless core, with no SwiftUI:
    • the engine adapter (DSN in the environment, SQL on stdin, allow-listed environment, redacted stderr, timeouts and cancellation)
    • the Context mirror, health score, finding diff and notification dedup
    • Keychain, SQLite, the scheduler, OAuth with PKCE, and psql meta-command translation
  3. feat(desktop): PGBot for macOS is the SwiftUI app:
    • chat, health reports and findings
    • PSQL mode (+ → PSQL Mode, ⌘⇧P)
    • metrics, logs, the notification center and menu bar, and macOS notifications with per-category, severity, quiet-hours and per-database settings
    • appearance settings, demo mode and onboarding
    • scripts/build-app.sh builds a signed PGBot.app with the universal engine.
  4. ci: macOS job adds .github/workflows/desktop-macos.yml.

Each commit builds on its own.

Read-only guarantees

  • No credentials in argv or logs, secrets only in the Keychain, and no password field in SQLite.
  • Explicit consent before anything is sent to an external AI provider.
  • PSQL mode can't change the database: writes and DDL fail with 25006 even for a superuser, and that is tested.

Tests

  • Go:
    • Unit: TestSplitSQLStatements, TestRefuseStatement, TestRenderSQLRunText*, TestSQLRunJSONContract (pins the keys the app decodes) and TestExecuteSQLRunRefusesBeforeConnecting.
    • Integration: TestIntegration_sqlRun (PGBOT_TEST_SUPERUSER_DSN) checks that writes and DDL never land, plus types, NULL, max-rows, statement timeout and error position. The existing integration job picks it up on PG 14–19.
  • Swift: swift run PGBotKitChecks has 49 unit checks, plus 8 live checks with PGBOT_TEST_DSN and PGBOT_ENGINE_PATH. The live checks cover inspect scoring against the CLI, error classification, and PSQL mode typed rows, the 25006 block, the COMMIT refusal, and \dt / \d against the real catalog.
  • New macOS CI job: unit checks, live checks against a Homebrew PG17, the sql run integration test, and a PGBot.app build. It verifies the bundle, the arm64+x86_64 engine and the code signature.
  • Bug fix: an unknown role (SQLSTATE 28000) was classified as "server unreachable". It is now an authentication problem.

Verified locally on a clean checkout of the branch:

  • gofmt clean, go vet clean, golangci-lint 0 issues
  • go test ./... passes
  • the Swift build succeeds, and all 57 checks pass against PostgreSQL 17

Not in this PR

  • No CHANGELOG.md or top-level README.md entry for sql run yet.
  • The OAuth auth broker, notarization and a universal Swift binary (needs Xcode), and XCUITests. They're listed in desktop/macos/README.md.

🤖 Generated with Claude Code

alexshapalov and others added 8 commits November 26, 2026 12:02
Executes one or more statements inside a single BEGIN READ ONLY transaction
that is always rolled back, so the server itself refuses writes and DDL.
Before anything is sent, pgbot refuses statements that could end or escape
the transaction (COMMIT/BEGIN/SAVEPOINT/…), reach outside the database
(COPY, dblink, large-object file I/O) or act on other sessions or the server
(pg_terminate_backend, pg_reload_conf, stats resets, replication slots,
VACUUM, LOCK). Each statement goes over the extended protocol, so one call
carries exactly one statement. Statement and lock timeouts bound every
statement and --max-rows stops fetching server-side.

Output is psql-style aligned text, or JSON with --json
(sql_run_schema_version 1.0.0), which the desktop app's PSQL mode decodes.

Tests: statement splitting (quotes, E'', dollar quoting, nested comments),
the refusal list, the aligned renderer, the JSON contract, refusal before
connecting, and an integration test (PGBOT_TEST_SUPERUSER_DSN) proving that
writes and DDL never land even for a superuser, plus types, NULL, max-rows,
statement timeout and error positions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The headless half of PGBot for macOS (desktop/macos), with no SwiftUI so all
of it is checkable from the command line:

- Engine adapter over the bundled pgbot binary: typed calls for inspect,
  ask, sql explain, sql run, logs, activity, why, diff and init. The DSN goes
  in PGBOT_DATABASE_URL and SQL over stdin (never argv), the child
  environment is allow-listed, stderr is redacted, and runs are bounded by a
  timeout, SIGTERM→SIGKILL cancellation and a concurrency limit.
- Codable mirror of the Context JSON contract, the engine's health-score
  formula with a completeness gate, and the id+object finding diff with an
  announcement ledger so a finding is never re-announced.
- Keychain secrets (AfterFirstUnlockThisDeviceOnly), SQLite persistence
  (0600, no password field), a per-bot scheduler with backoff and
  sleep/wake, OAuth 2.0 + PKCE via an auth broker, and demo data.
- PSQL mode: psql meta-commands (\d, \dt, \l, \dn, \df, \du, \dx, \x, …)
  translated to catalog queries with patterns quoted safely; shell, file
  and session commands refused.

Checks: `swift run PGBotKitChecks` (49 unit checks; Command Line Tools ship
no XCTest), plus 8 live checks with PGBOT_TEST_DSN and PGBOT_ENGINE_PATH.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
One bot per PostgreSQL database, built on PGBotKit and the bundled engine:

- Chat per database with health reports, findings, plans and tables;
  answers from `pgbot ask` behind an explicit per-provider consent sheet.
- PSQL mode (+ → PSQL Mode, ⌘⇧P): a read-only psql console in the chat
  with history, psql Return semantics, typed result tables, \x records,
  copy as TSV/CSV/Markdown and psql-style errors with a caret.
- Metrics charts, logs with filters, notification center, menu bar extra,
  and macOS notifications with per-category, severity, quiet-hours and
  per-database settings.
- Settings: appearance (theme, accent, text size, density, message style,
  avatars), new-database defaults, monitoring, AI providers, privacy.
- Demo mode (clearly labelled), onboarding and add-database flows with the
  read-only role setup SQL.

scripts/build-app.sh builds the universal engine and assembles a signed
PGBot.app; launch scenarios support screenshots and UI automation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Runs on macos-14 (Swift 5.10) when desktop/, cmd/, internal/ or the Go
module change: PGBotKit unit checks, live engine checks against a Homebrew
PostgreSQL 17 (including PSQL mode's read-only guarantees), the sql run
superuser integration test, and a PGBot.app build whose bundle, universal
engine and signature are verified.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…database

Adding a database posts its first health report. The once-a-minute daily
report timer then saw a bot that had never had a daily report, past its
report hour (09:00 by default), and ran a second inspection, so the chat
showed the same report twice.

The first report now counts as today's full report, and the due rule moves
to AnnouncementLedger.isDailyReportDue, covered by a new check. Verified in
the built app: one report after 2.5 minutes, where there used to be two.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The composer mode was only reachable from the + menu or ⌘⇧P, and nothing
near the input said which mode was on. A two-segment Chat | PSQL switch now
sits next to +: the active mode is a raised pill (green for PSQL) that
slides between segments, with tooltips and accessibility labels.

Also: the \? help rendered its Markdown table as raw pipes (chat messages
don't render tables); it is a list now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant