Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
a063dd3
feat(schema): browse procedures, functions, views and triggers
ProjectAJ14 Oct 30, 2026
34b9fff
feat(schema): object dependencies and cross-environment drift report
ProjectAJ14 Oct 30, 2026
5de9a28
feat(schema): read SQL Server Agent and pg_cron jobs
ProjectAJ14 Oct 30, 2026
1a73d36
fix(schema): hide job steps that run against another database
ProjectAJ14 Oct 30, 2026
6903728
feat(write): governed object changes with diff, drift guard and rollback
ProjectAJ14 Oct 30, 2026
7618cd7
feat(schema): browse MongoDB and Elasticsearch objects
ProjectAJ14 Oct 30, 2026
4ae577c
feat(write): governed job changes with a change-jobs capability
ProjectAJ14 Oct 30, 2026
19c33a6
fix(write): keep job changes inside the connection's own database
ProjectAJ14 Oct 30, 2026
9d29e91
chore(dev): local DEV, QA and PROD Postgres databases
ProjectAJ14 Oct 30, 2026
8d1cf1f
fix(write): close review gaps in object and job changes
ProjectAJ14 Oct 30, 2026
5aa1644
fix(write): close job-step parser gaps and AI review exposure
ProjectAJ14 Oct 30, 2026
b584c72
docs(server): describe the job-step catalog check
ProjectAJ14 Oct 30, 2026
d0a572a
fix(write): read job steps with each dialect's own string rules
ProjectAJ14 Oct 30, 2026
2190f0a
fix(write): check job steps with each engine's own parser
ProjectAJ14 Oct 30, 2026
f60c67a
fix(write): pin the SHOWPLAN session and keep SQL Server steps in the…
ProjectAJ14 Oct 30, 2026
27f6442
fix(write): run SQL Server steps in the database they were checked in
ProjectAJ14 Oct 30, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .claude/skills/verify/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ description: How to launch and drive D-Pilot locally to verify changes end-to-en
- Get a JWT: `POST /api/auth/login` with `{"username","password"}` → `{token}`; pass as
`Authorization: Bearer <token>` to everything else.

## Local DEV/QA/PROD databases (quickest)

`npm run dev:db` (Docker only) starts three Postgres servers from
`docker-compose.dev.yml` — DEV :5433, QA :5434, PROD :5435, user/password `dpilot` —
reseeds each from `scripts/seed-local.sql` (different row counts per env, schema drift
between DEV and PROD, pg_cron jobs) and registers them as connections in the store
under `.env`'s `DATA_DIR`. Re-run it to reset the data; restart a running server to
pick up the connections. `npm run dev:db:down` stops them.

## Isolated stack against a local Postgres

The default way to verify end-to-end without touching the main `.env` or the shared
Expand Down
7 changes: 5 additions & 2 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ npm run format # Prettier
npm run format:check # Prettier in check mode (what CI runs)
npm run typecheck # tsc --noEmit over src + server
npm run verify # format:check + lint + typecheck + test — the CI gate, minus the build
npm run dev:db # Docker: local DEV/QA/PROD Postgres (5433-5435), reseeded and registered as connections
npm run dev:db:down # Stop them
```

`predev` frees the dev ports first (`scripts/free-ports.mjs`). Ports are set in `.env`
Expand Down Expand Up @@ -62,9 +64,10 @@ run, since that would put the Basic credential in a model's context and transcri
code needs a hardcoded fallback, use the neutral `D-Pilot` / `d-pilot`. Test fixtures
must use generic names (`app_core`, `orders`, `customers`), never real internal ones.
- **Capability-based access, scoped per environment** (`DEV`/`QA`/`UAT`/`STG`/`PROD` in a
standard deployment). A user has `isAdmin` plus four env lists: read
standard deployment). A user has `isAdmin` plus five env lists: read
(`allowedEnvironments`), unmask PHI (`unmaskEnvironments`), write (`writeEnvironments`),
approve (`approveEnvironments`). Admin implies all capabilities on all envs. There is
approve (`approveEnvironments`), change jobs (`jobEnvironments` — job_change requests;
empty by default, never implied by write, and needs write and read too). Admin implies all capabilities on all envs. There is
**no** legacy `role` column — it was migrated to capabilities (see `initAuthTables` in
`server/middleware/auth.ts`).
- **The environment list is runtime config, never a literal.** `getEnvironments()`
Expand Down
98 changes: 75 additions & 23 deletions README.md

Large diffs are not rendered by default.

72 changes: 50 additions & 22 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,56 @@
# Local Postgres for experimenting with the write / migration workflow.
# Isolated from any production target DB: point the dev stack's connection at it
# in Settings → Connections (localhost:5433, dpilot_dev, user/password dpilot).
# Start with: npm run dev:db (stop: npm run dev:db:down)
# Local databases for development: one Postgres server per environment, so
# DEV, QA and PROD differ the way real ones do (data volumes, schema drift,
# scheduled jobs). Isolated from any real target DB.
#
# Host port 5433 (not 5432) so it never clashes with a Postgres you already run.
# Start, reseed and register them in D-Pilot with: npm run dev:db
# Stop with: npm run dev:db:down
#
# Host ports 5433-5435 (not 5432) so they never clash with a Postgres you
# already run. Each server has pg_cron, scoped to its own database, so the
# Jobs view has something to show.
x-postgres: &postgres
build:
dockerfile_inline: |
FROM postgres:16
RUN apt-get update && apt-get install -y --no-install-recommends postgresql-16-cron \
&& rm -rf /var/lib/apt/lists/*
image: dpilot-dev-postgres:16-cron
healthcheck:
test: ["CMD-SHELL", "pg_isready -U dpilot -d $${POSTGRES_DB}"]
interval: 3s
timeout: 3s
retries: 20

x-pg-env: &pg-env
POSTGRES_USER: dpilot
POSTGRES_PASSWORD: dpilot

services:
postgres:
image: postgres:16-alpine
pg-dev:
<<: *postgres
container_name: dpilot-dev-pg
environment:
POSTGRES_USER: dpilot
POSTGRES_PASSWORD: dpilot
POSTGRES_DB: dpilot_dev
ports:
- "5433:5432"
volumes:
- dpilot_dev_pg:/var/lib/postgresql/data
# Seeds a baseline schema on first boot (only when the volume is empty).
- ./scripts/dev-seed.sql:/docker-entrypoint-initdb.d/01-seed.sql:ro
healthcheck:
test: ["CMD-SHELL", "pg_isready -U dpilot -d dpilot_dev"]
interval: 3s
timeout: 3s
retries: 10
environment: { <<: *pg-env, POSTGRES_DB: dpilot_dev }
command: postgres -c shared_preload_libraries=pg_cron -c cron.database_name=dpilot_dev
ports: ["5433:5432"]
volumes: [dpilot_dev_pg:/var/lib/postgresql/data]

pg-qa:
<<: *postgres
container_name: dpilot-qa-pg
environment: { <<: *pg-env, POSTGRES_DB: dpilot_qa }
command: postgres -c shared_preload_libraries=pg_cron -c cron.database_name=dpilot_qa
ports: ["5434:5432"]
volumes: [dpilot_qa_pg:/var/lib/postgresql/data]

pg-prod:
<<: *postgres
container_name: dpilot-prod-pg
environment: { <<: *pg-env, POSTGRES_DB: dpilot_prod }
command: postgres -c shared_preload_libraries=pg_cron -c cron.database_name=dpilot_prod
ports: ["5435:5432"]
volumes: [dpilot_prod_pg:/var/lib/postgresql/data]

volumes:
dpilot_dev_pg:
dpilot_qa_pg:
dpilot_prod_pg:
2 changes: 1 addition & 1 deletion docs/bruno/CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Naming conventions for the Action word:
credentials directly, not a JWT.
Run **Auth / Login** once — its post-response script writes the JWT into `{{token}}`.
- **Variables** (per environment): `{{baseUrl}}`, `{{token}}`, `{{connectionId}}`,
`{{tableName}}`, `{{savedQueryId}}`, `{{phiRuleId}}`, `{{userId}}`, `{{writeRequestId}}`,
`{{tableName}}`, `{{objectKind}}`, `{{objectName}}`, `{{jobId}}`, `{{savedQueryId}}`, `{{phiRuleId}}`, `{{userId}}`, `{{writeRequestId}}`,
`{{mcpUsername}}` / `{{mcpPassword}}` (MCP service account).
Path params always use one of these placeholders — never hard-code an id.
- **PHI** — endpoints that return maskable data (Execute Query, Export CSV/JSON, Preview
Expand Down
6 changes: 4 additions & 2 deletions docs/bruno/MCP/POST List Tools.bru
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ docs {
Lists the tools exposed to agents.

Database access — `whoami`, `list_connections`, `list_schemas`, `list_tables`,
`describe_table`, `run_query` — is read-only.
`describe_table`, `list_objects`, `get_object_definition`, `get_object_dependencies`,
`compare_objects`, `list_jobs`, `get_job`, `run_query` — is read-only.

`create_artifact` / `update_artifact` / `get_artifact` / `list_artifacts` /
`archive_artifact` write D-Pilot's own document store, never a target database.

`create_write_request` saves a **DRAFT** write request for a human to review; it
`create_write_request` saves a **DRAFT** write request for a human to review (pass `object`
to draft a procedure/function/view/trigger change); it
executes nothing, on any environment. `get_write_request` reads one back —
including the `aiReview` a human triggered in the UI — and `update_write_request`
edits it, drafts only and still a draft afterwards. There is deliberately no tool
Expand Down
24 changes: 24 additions & 0 deletions docs/bruno/Schema/GET Drift Report.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
meta {
name: Drift Report
type: http
seq: 8
}

get {
url: {{baseUrl}}/api/schema/{{connectionId}}/drift?against={{connectionId}}
body: none
auth: inherit
}

params:query {
against: {{connectionId}}
~schema: public
}

docs {
Procedures, functions, views and triggers in one schema that differ between this connection and `against` (same engine — MongoDB and Elasticsearch objects compare too): { schema, identical, items: [{ schema, name, kind, args?, parent?, status }] }, status different | only_left | only_right. Definitions are compared by SHA-256 of their normalized text (line endings and trailing whitespace removed); none is returned. A definition hidden on either side counts as different.

Requires a valid Bearer JWT ({{token}}) plus **read** capability on **both** connections' environments — 403 otherwise.

Errors: 400 when `against` is missing or the two connections are different engines; 503 { error, code: "CONNECTION_FAILED" }; 500 { error }.
}
19 changes: 19 additions & 0 deletions docs/bruno/Schema/GET Job Detail.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
meta {
name: Job Detail
type: http
seq: 10
}

get {
url: {{baseUrl}}/api/schema/{{connectionId}}/jobs/{{jobId}}
body: none
auth: inherit
}

docs {
One job with its steps (full commands, subsystem, database, last outcome), schedules ({ description, sharedWith, editable, msdb? | cron? }) and its last 20 job-level runs. `jobId` is a GUID for SQL Server Agent and an integer for pg_cron — anything else is a 400 before any SQL runs. Audited as JOB_VIEW.

Requires a valid Bearer JWT ({{token}}) plus **read** capability for the connection's environment — 403 otherwise.

Errors: 400 malformed id; 404 no such job on this database; 503 { error, code: "CONNECTION_FAILED" }; 500 { error }.
}
21 changes: 21 additions & 0 deletions docs/bruno/Schema/GET List Jobs.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
meta {
name: List Jobs
type: http
seq: 9
}

get {
url: {{baseUrl}}/api/schema/{{connectionId}}/jobs
body: none
auth: inherit
}

docs {
Scheduled jobs that run against this connection's database — SQL Server Agent jobs (from msdb) or pg_cron jobs: { available, reason?, jobs: [{ id, name, engine, enabled, description?, category?, schedule?, lastRun?, nextRunAt?, stepCount }] }. Agent jobs are listed only when a step targets this database. Dates are the server's local time.

`available: false` with a `reason` when the connection's login lacks the grant (SQLAgentReaderRole in msdb; USAGE + SELECT on the cron schema); `available: false` with no reason when pg_cron is not installed.

Requires a valid Bearer JWT ({{token}}) plus **read** capability for the connection's environment — 403 otherwise.

Errors: 503 { error, code: "CONNECTION_FAILED" }; 500 { error }.
}
27 changes: 27 additions & 0 deletions docs/bruno/Schema/GET List Objects.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
meta {
name: List Objects
type: http
seq: 5
}

get {
url: {{baseUrl}}/api/schema/{{connectionId}}/objects
body: none
auth: inherit
}

params:query {
~schema: public
~kind: procedure
}

docs {
The objects a database keeps beside its data, read live: procedures, functions, views and triggers in a schema (Postgres/MSSQL); views, indexes and validation rules (MongoDB); index templates, ingest pipelines, aliases and ILM policies (Elasticsearch — Elastic-managed ones are skipped).

Each entry is { schema, name, kind, args?, parent? }: `args` (Postgres routines) are the identity argument types that tell overloads apart; `parent` is a trigger's table. `kind` filters to one kind; `parent` is also a MongoDB index's collection.

Requires a valid Bearer JWT ({{token}}) plus **read** capability for the connection's environment — 403 otherwise (admins hold every environment).

Errors: 503 { error, code: "CONNECTION_FAILED" } when the database host is
unreachable (network); 500 { error } for other introspection failures.
}
27 changes: 27 additions & 0 deletions docs/bruno/Schema/GET Object Definition.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
meta {
name: Object Definition
type: http
seq: 6
}

get {
url: {{baseUrl}}/api/schema/{{connectionId}}/objects/{{objectKind}}/{{objectName}}/definition
body: none
auth: inherit
}

params:query {
~schema: public
~args: integer
~parent: orders
}

docs {
One object's full definition, read live: { schema, name, kind, args?, parent?, definition, migration }. `definition` is null when the engine hides it (encrypted, or no VIEW DEFINITION permission). `migration` is the same definition as a re-runnable script (`CREATE OR ALTER` / `CREATE OR REPLACE`) — the starting point for an object-change write request — or null (hidden, or a PostgreSQL materialized view).

`objectKind` is procedure | function | view | trigger (Postgres/MSSQL), view | index | validator (MongoDB — pass `parent` for an index's collection), or index_template | ingest_pipeline | alias | ilm_policy (Elasticsearch); MongoDB/Elasticsearch definitions are JSON. `args` picks a Postgres overload by its identity argument types (empty string = no arguments); `parent` picks a Postgres trigger by its table. Without them an ambiguous name is a 400 that lists the choices.

Requires a valid Bearer JWT ({{token}}) plus **read** capability for the connection's environment — 403 otherwise (admins hold every environment). Each successful read is audited as OBJECT_VIEW.

Errors: 400 for an unknown kind, an engine without objects, or an ambiguous name; 404 { error, code: "OBJECT_NOT_FOUND" }; 503 { error, code: "CONNECTION_FAILED" } when the database host is unreachable; 500 { error } for other failures.
}
27 changes: 27 additions & 0 deletions docs/bruno/Schema/GET Object Dependencies.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
meta {
name: Object Dependencies
type: http
seq: 7
}

get {
url: {{baseUrl}}/api/schema/{{connectionId}}/objects/{{objectKind}}/{{objectName}}/dependencies
body: none
auth: inherit
}

params:query {
~schema: public
~args: integer
~parent: orders
}

docs {
What an object reads and what reads it: { uses, usedBy, bestEffort, jobs }. Each reference is { schema?, name, kind, database? }; `kind` is procedure | function | view | trigger | table, or null for a reference SQL Server could not resolve (dropped, late-bound, cross-database — `database` is set for the last). `bestEffort` is true when part of the answer came from matching names in Postgres function bodies. `jobs` lists { id, name } of the Agent or pg_cron jobs on this database whose step names the object — also matched by name, and empty when the connection can't read jobs.

Same `objectKind` / `args` / `parent` rules as Object Definition.

Requires a valid Bearer JWT ({{token}}) plus **read** capability for the connection's environment — 403 otherwise.

Errors: 400 for an unknown kind, an engine without objects, or an ambiguous name; 503 { error, code: "CONNECTION_FAILED" }; 500 { error }.
}
5 changes: 3 additions & 2 deletions docs/bruno/Users/POST Create User.bru
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ body:json {
"writeEnvironments": [
"DEV"
],
"approveEnvironments": []
"approveEnvironments": [],
"jobEnvironments": []
}
}

docs {
Create a user. Admin implies all capabilities; otherwise supply per-env lists.
Create a user. Admin implies all capabilities; otherwise supply per-env lists. `jobEnvironments` grants raising job_change requests (changing SQL Server Agent / pg_cron jobs) — separate from `writeEnvironments`, and empty by default.

Environment names outside this deployment's set (see `GET /api/config` →
`environments`) are silently dropped from every capability list.
Expand Down
3 changes: 2 additions & 1 deletion docs/bruno/Users/PUT Update User.bru
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ body:json {
],
"approveEnvironments": [
"QA"
]
],
"jobEnvironments": []
}
}

Expand Down
2 changes: 2 additions & 0 deletions docs/bruno/Write Requests/POST Approve Write Request.bru
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ docs {

Requires a valid Bearer JWT ({{token}}).
Requires **write mode** to be enabled.

Object changes: 409 `{ code: "OBJECT_CHANGED" }` when the object's deployed definition no longer matches the snapshot taken at submit — nothing runs and the request is unchanged; revise it to re-base.
}
4 changes: 4 additions & 0 deletions docs/bruno/Write Requests/POST Create Write Request.bru
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ docs {

On DIRECT-policy envs an authorized author executes immediately; otherwise it goes PENDING for approval.

**Object change** — send `object: { schema, name, kind, args?, parent? }` (kind procedure | function | view | trigger) and the object's complete new definition as `writeSql`. It must be one idempotent `CREATE OR ALTER` (SQL Server) / `CREATE OR REPLACE` (PostgreSQL) of that same object, run as one batch (no `GO`). The server snapshots the definition deployed now into `snapshot` (null = the object doesn't exist yet); approval, override and submit re-read it and answer 409 `{ code: "OBJECT_CHANGED" }` if it changed since — revise to re-base. 400 when the deployed definition is hidden (encrypted / no VIEW DEFINITION). `warnings` lists anything that looks like a credential in the definition. Always transactional (`noTransaction` is ignored).

**Job change** — send `jobChange: { jobId, action }` instead of SQL (`writeSql` is ignored; the server generates `sp_update_job` / `sp_update_jobstep` / `sp_add_jobstep` / `sp_delete_jobstep` / `sp_update_schedule` for SQL Server Agent, `cron.alter_job` for pg_cron). `action` is one of `{ type: "set_enabled", enabled }`, `{ type: "rename", name }`, `{ type: "set_description", description }`, `{ type: "edit_step", stepId, command }`, `{ type: "add_step", name, command }`, `{ type: "delete_step", stepId }`, `{ type: "set_schedule", scheduleId, msdb | cron }` (rename/description/add/delete are Agent-only). Requires the **jobEnvironments** capability for the connection's env (403 otherwise). A new or edited step command must pass as a direct write would — an EXEC/CALL, or scoped INSERT/UPDATE/DELETE statements; DDL, dynamic SQL and unscoped UPDATE/DELETE are refused (400) — and CmdExec/PowerShell/SSIS steps can't be changed. Every refusal is audited as `JOB_CHANGE_REFUSED`. The job as it is now is stored as `snapshot`; submit/approve/override answer 409 `{ code: "OBJECT_CHANGED" }` if it changed since. A job change can't be revised — cancel it and raise a new one. A schedule shared with other jobs can't be changed, and on a job that also runs steps against another database only this database's own step commands can (400).

Set `draft: true` to save it instead: the request is stored as DRAFT and neither queued for approval nor executed, whatever the environment's policy says, until someone calls **Submit Write Request**. This is what the MCP `create_write_request` tool sends, so an agent can never reach a target database.

503 `CONNECTION_FAILED` when a direct run can't reach the database before sending anything: nothing ran, the request is back in DRAFT, and the body carries it as `request`. Audited as `WRITE_EXECUTE_ERROR`.
Expand Down
2 changes: 2 additions & 0 deletions docs/bruno/Write Requests/POST Override Write Request.bru
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ docs {

Requires a valid Bearer JWT ({{token}}).
Requires **write mode** to be enabled.

Object changes: 409 `{ code: "OBJECT_CHANGED" }` when the object's deployed definition no longer matches the snapshot taken at submit — nothing runs and the request is unchanged; revise it to re-base.
}
2 changes: 2 additions & 0 deletions docs/bruno/Write Requests/POST Revise Write Request.bru
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ docs {

Requires a valid Bearer JWT ({{token}}).
Requires **write mode** to be enabled.

Object changes: the new `writeSql` is checked against the request's object, and the snapshot is re-taken from what is deployed now.
}
2 changes: 2 additions & 0 deletions docs/bruno/Write Requests/POST Submit Write Request.bru
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ docs {

Requires **write** capability on the request's environment and a valid Bearer JWT ({{token}}).
Requires **write mode** to be enabled.

Object changes: 409 `{ code: "OBJECT_CHANGED" }` when the object's deployed definition no longer matches the snapshot taken at submit — nothing runs and the request is unchanged; revise it to re-base.
}
3 changes: 3 additions & 0 deletions docs/bruno/environments/LOCAL.bru
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ vars {
token:
connectionId: dev-pg
tableName: patients
objectKind: procedure
objectName: refresh_order_totals
jobId:
savedQueryId:
phiRuleId:
userId:
Expand Down
3 changes: 3 additions & 0 deletions docs/bruno/environments/PROD.bru
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ vars {
token:
connectionId: dev-pg
tableName: patients
objectKind: procedure
objectName: refresh_order_totals
jobId:
savedQueryId:
phiRuleId:
userId:
Expand Down
3 changes: 3 additions & 0 deletions docs/bruno/environments/QA.bru
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ vars {
token:
connectionId: dev-pg
tableName: patients
objectKind: procedure
objectName: refresh_order_totals
jobId:
savedQueryId:
phiRuleId:
userId:
Expand Down
3 changes: 3 additions & 0 deletions docs/bruno/environments/STG.bru
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ vars {
token:
connectionId: dev-pg
tableName: patients
objectKind: procedure
objectName: refresh_order_totals
jobId:
savedQueryId:
phiRuleId:
userId:
Expand Down
Loading
Loading