Skip to content

fix(training-agent): add the SDK's obligation claim-priority indexes - #9219

Merged
bokelley merged 2 commits into
mainfrom
fix/training-reporting-obligation-priority-indexes
Nov 24, 2026
Merged

bokelley merged 2 commits into
mainfrom
fix/training-reporting-obligation-priority-indexes

Conversation

@bokelley

@bokelley bokelley commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The repository checks in the SDK's reporting SQL as its own migrations in schema training_reporting_gcs (620, 621, 624, 630 and 637). Migration 620 copied REPORTING_LEDGER_MIGRATION without two of its indexes:

  • adcp_reporting_obligations_priority_due;
  • adcp_reporting_obligations_account_priority_due.

The SDK's obligation claim orders live work before history backfill: ORDER BY (COALESCE(data->>'history', 'false') = 'true'), next_attempt_at, obligation_id over pending obligations, both globally and per account. These partial indexes serve that order, so without them the claim sorts the pending backlog. Both @adcp/reporting-storage 0.3.0-beta.1 and 0.3.0-beta.9, the release main now pins (#9188), define them. A catalog comparison during the #9188 review found that they are the only objects from the SDK SQL the training agent runs that are missing from the repository schema.

Change

New migration 638_training_gcs_reporting_obligation_priority_indexes.sql:

  • Both indexes are verbatim from REPORTING_LEDGER_MIGRATION. Each is a CREATE INDEX IF NOT EXISTS … WHERE state = 'pending', keyed on (history flag, next_attempt_at, obligation_id), with account_id leading on the account index.
  • It has the same namespace precondition and timeouts as 624 and 637 (lock_timeout 5s, statement_timeout 60s), plus a check that 620's table exists.

Not concurrent. server/src/db/migrate.ts applies each migration inside BEGIN … COMMIT, so CREATE INDEX CONCURRENTLY is not possible. The first build takes a SHARE lock on adcp_reporting_obligations, and the migration's transaction holds it across both builds until COMMIT. Writes to the table wait until the migration commits; reads do not. lock_timeout (5 s) bounds the wait to acquire the lock, so a busy table fails the migration rather than stalling startup. statement_timeout (60 s) applies to each CREATE INDEX separately, so writes can be blocked for up to about two minutes in total. Each build scans the whole table, so before deploying, SELECT pg_size_pretty(pg_relation_size('training_reporting_gcs.adcp_reporting_obligations')); shows how much it reads.

The migration runner applies every migration not yet recorded in schema_migrations, whatever its number, so 638 does not depend on the order in which numbered migrations reach a database.

The runbook's migration paragraph names 638.

No changeset: this is training-agent server code, not the protocol package (.agents/playbook.md § Changesets).

Tests

server/tests/integration/training-gcs-reporting-postgres.test.ts passes 10/10 against PostgreSQL 16:

  • It applies 620, 621, 624, 630, 637 and 638, then replays 630, 637 and 638.
  • The new test checks each index's pg_indexes.indexdef (non-unique, USING btree (<keys>) WHERE (state = 'pending'::text)) and pg_index.indisvalid.
  • With sequential scans disabled, EXPLAIN of the SDK's claim ordering uses adcp_reporting_obligations_priority_due. That shows the planner can use the index; it says nothing about plans on production data.

Local hooks: the commit used --no-verify, so the pre-commit checks were run by hand afterwards on this head. Both passed: scripts/check-image-quality.sh and npm run precommit (the dynamic-import, format-identity and callApi guards, then npm run typecheck). The pre-push hook ran and passed (version sync and changeset policy). Its opt-in storyboard matrix (ADCP_RUN_STORYBOARD_MATRIX=1) was not requested; CI is the gate.

🤖 Generated with Claude Code

aao-secretariat[bot]
aao-secretariat Bot previously approved these changes Nov 24, 2026

@aao-secretariat aao-secretariat Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ladon verdict: Approve

Approve — no blocking findings.

The reviewer surfaced no Critical/High/Medium findings. Working through the decision table top-down:

  • Row 1 (critical/high): no such findings. Skip.
  • Row 2 (gated_paths unsatisfied): gated_paths is false. Skip.
  • Row 3 (high_risk + deleted): high_risk is true, but all high-risk reasons are (added) or (modified) — no deletions. Skip.
  • Row 4 (medium + data-loss/schema/infra): no medium findings. Skip.
  • Row 5 (high_risk + modified + medium): no medium findings. Skip.
  • Row 6 (sticky escalate): no prior decision. Skip.
  • Row 7 (no-auto-approve team): no team match. Skip.
  • Row 8 (≥3 medium): zero medium findings. Skip.
  • Row 9: approve.

Note on high_risk: the flag is true because migrations 637/638 are newly added SQL under server/src/db/migrations/** and docs/reference/versions.mdx is modified. New migration files are normal scaffolding; the versions.mdx modification carried no medium-or-higher concern. The reviewer verified 637's constraint drops are FK-safe (FKs reference configuration_id PKs, not the dropped 3-col uniques; new 4-col unique built before old drops; subscription_key '' default preserves uniqueness — no replay violation), 638's partial indexes match the SDK claim ORDER BY, migrate.ts wraps each file in BEGIN/COMMIT making CONCURRENTLY correctly unavailable with bounded lock_timeout 5s / statement_timeout 60s (fail-closed, not data loss), the gcs-reporting notification downgrade preserves databaseCode precedence and abandonment escalation (startup.test.ts:691 verified), and hosted-compliance-version.ts is a tsc-gated type-only swap. Migration-only training-agent code correctly carries no changeset. No schema/docs coherence, oneOf, artifact-immutability, or governance concerns apply.

aao-secretariat[bot]
aao-secretariat Bot previously approved these changes Nov 24, 2026

@aao-secretariat aao-secretariat Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ladon verdict: Approve

Approve.

Delta since the prior clean approve is a single comment-only change in server/src/db/migrations/638_training_gcs_reporting_obligation_priority_indexes.sql, correcting the lock-semantics description. The revised comment is accurate: a non-concurrent CREATE INDEX takes a SHARE lock (blocks writes, not reads), the transaction holds it across both builds until COMMIT, lock_timeout (5s) bounds the lock wait and statement_timeout (60s) bounds each build. No behavior change.

Checks applied:

  • No critical/high findings (row 1 n/a).
  • gated_paths is false, so row 2 does not fire despite review_decision=REVIEW_REQUIRED.
  • high_risk is true, but the high-risk entries are two (added) migration files and one (modified) docs file — no (deleted) entries (row 3 n/a) and no medium findings on the modified file (rows 4/5 n/a).
  • Prior decision was approve, not escalate (row 6 n/a).
  • No no-auto-approve team match (row 7 n/a).
  • Zero medium findings (row 8 n/a).

Falls through to row 9 → approve. No schema↔docs drift, changeset present and operational-scoped appropriately, no immutable-artifact edits, no undiscriminated oneOf, no breaking-class change.

bokelley and others added 2 commits November 24, 2026 18:49
…to training_reporting_gcs

Migration 620 copied the SDK reporting ledger SQL without
adcp_reporting_obligations_priority_due and
adcp_reporting_obligations_account_priority_due, the partial indexes the
SDK's live-first obligation claim orders by. Migration 638 adds both,
verbatim from REPORTING_LEDGER_MIGRATION, with the same namespace
precondition and lock and statement timeouts as 624 and 637. The runner
wraps each migration in a transaction, so the builds are not CONCURRENTLY
and hold a SHARE lock on adcp_reporting_obligations while they run.

The integration suite applies and replays 638 and checks both index
definitions, their validity, and that the planner can serve the claim
ordering from the global index.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…dex builds

The header said each build blocks writes for the build. The migration's
transaction holds the lock from the first CREATE INDEX until COMMIT, and
statement_timeout bounds each statement separately, not the file.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@bokelley
bokelley dismissed aao-secretariat[bot]’s stale review November 24, 2026 18:49

The merge-base changed after approval.

@bokelley
bokelley force-pushed the fix/training-reporting-obligation-priority-indexes branch from cd8ea7c to 2e1d1f5 Compare November 24, 2026 18:49
@bokelley bokelley changed the title fix(training-agent): add the SDK's obligation claim-priority indexes (stacked on #9188) fix(training-agent): add the SDK's obligation claim-priority indexes Nov 24, 2026

@aao-secretariat aao-secretariat Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ladon verdict: Approve

Approve — migration-only server PR, no blocking or medium findings.

Checked:

  • Migration 638 adds two partial obligation-claim priority indexes, verbatim from the established 637 pattern: schema/table preconditions, SET LOCAL search_path, lock_timeout=5s, statement_timeout=60s, idempotent CREATE INDEX IF NOT EXISTS. Fail-closed: a timeout rolls back and blocks the deploy with no partial state; the comment accurately describes the lock behavior.
  • Index key order matches the SDK claim ORDER BY; the new integration test verifies validity, non-uniqueness, replay idempotency, and live-first planner ordering.
  • No protocol surface touched (no static/schemas/source, docs/reference, or dist changes), so no changeset is the correct call for a migration-only PR.

high_risk is true only because migration 638 is an (added) file matching server/src/db/migrations/** — normal scaffolding, not an escalation trigger. gated_paths is false. No no-auto-approve team match. No critical/high/medium findings. Rows 1–8 do not fire; falls through to row 9.

@bokelley
bokelley merged commit 3c87f97 into main Nov 24, 2026
40 checks passed
@bokelley
bokelley deleted the fix/training-reporting-obligation-priority-indexes branch November 24, 2026 19:18
KonstantinMirin added a commit to KonstantinMirin/adcp that referenced this pull request Nov 25, 2026
…, certification retry (adcontextprotocol#9196), release-audit changesets (adcontextprotocol#8879), reporting subscriber suspend and resume (adcontextprotocol#9199), inventory goal forecast refusal (adcontextprotocol#9206), enum hoisting annotations (adcontextprotocol#9167), format_option_ref (adcontextprotocol#8876), claim-priority indexes (adcontextprotocol#9219)

Two conflicts.

Resolution ledger:
- semantic: package.json `test` -> union: this branch's chain (the
  suites it added and the build-cache and git-env steps) plus main's
  test:build-schemas-hoist-enum-annotations and
  test:build-schemas-hoist-inside-definitions, after
  test:build-schemas-exit-status. Every script main's chain runs is in
  the merged chain. Dependencies auto-merged to main's versions.
- semantic: server/src/training-agent/gcs-reporting.ts imports -> union:
  this branch's narrow `@adcp/sdk/utils` import of canonicalize; main's
  randomUUID import and its added `@adcp/sdk/server` names. The rest of
  the file auto-merged.
- mechanical: package-lock.json -> main's (auto-merged; this branch
  does not change it).

Checks: npm ci on main's lockfile (SDK 17.0.0-beta.40), npm run
typecheck, and the pre-commit hook's full run.
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