Skip to content

sync: port module changes from constructive-db - #129

Merged
pyramation merged 2 commits into
mainfrom
feat/sync-from-constructive-db
Sep 9, 2026
Merged

sync: port module changes from constructive-db#129
pyramation merged 2 commits into
mainfrom
feat/sync-from-constructive-db

Conversation

@pyramation

Copy link
Copy Markdown
Contributor

Summary

Brings every module that exists here back in line with its vendored copy in constructive-db/pgpm-modules/, which had drifted ahead. Only modules already present upstream are synced (db-utils / infra-utils exist only in constructive-db and are left out). No version bumps; sql/ bundles regenerated with pgpm package.

SQL source changes:

  • database-jobs run_scheduled_job: the coverage lookup now adds AND js.attempts < js.max_attempts, so an exhausted keyed job (e.g. max_attempts = 1 after a missing-function failure) no longer counts as "still scheduled" and the next tick resets it instead of raising ALREADY_SCHEDULED forever (constructive-db#3723 / constructive-planning#2013).
  • function-resolution: install_route_bindings honours an anonymous flag on binding entries; resolve_capabilities echoes required_capabilities (NULL = undeclared, [] = declared none).
  • metaschema-modules: new refusal_log_module table + one_platform_scope constraint (2 plan entries); agent_module.settle_run_cost_function_name; inference_log_module.inference_price_table_*; principal_auth_module.private_schema_id / sweep_expired_principals_function; user_auth_module updates.
  • metaschema-schema: index table, is_valid_step_up.
  • utils: new default_self_reference (1 plan entry).

Tests were ported with constructive-testpgsql-test imports. Two places where the vendored tests were stale relative to upstream were kept at upstream's version: the catalog_module seed inserts (must include app_store_identities_table_id) and the enqueue() actor claims in resolution.test.ts. The metaschema-modules FK snapshot was regenerated (+1 constraint from the price-table FK).

Verified locally: pgpm test-packages --full-cycle — all modules pass; package tests for database-jobs, jobs, function-resolution, metaschema-modules, metaschema-schema, utils, object-store pass; pnpm run policy:check clean.

Link to Devin session: https://app.devin.ai/sessions/09f9e1a7ad7c4e6480b0e489ab2bee95
Open in Devin Desktop: https://app.devin.ai/desktop/session/09f9e1a7ad7c4e6480b0e489ab2bee95?variant=devin
Requested by: @pyramation

database-jobs: run_scheduled_job no longer treats an exhausted keyed job as
active coverage, so a schedule's next tick re-runs it instead of raising
ALREADY_SCHEDULED.
function-resolution: route bindings carry an anonymous flag;
resolve_capabilities echoes required_capabilities.
metaschema-modules: refusal_log_module table + one-platform-scope
constraint; agent_module settle_run_cost_function_name;
inference_log_module price table; principal_auth_module private schema +
sweep function; user_auth_module changes.
metaschema-schema: index table and is_valid_step_up updates.
utils: default_self_reference.
Tests ported from constructive-db (pgsql-test imports) and bundles
regenerated with pgpm package.
@devin-ai-integration

Copy link
Copy Markdown
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@tenki-reviewer

tenki-reviewer Bot commented Sep 9, 2026

Copy link
Copy Markdown

Review complete. 🟠 1 high

💬 Inline comments (1)

🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 Redundant one_platform_scope partial unique index (one_platform_scope.sql:8) — The partial unique index refusal_log_module_one_platform_scope on (database_id) WHERE scope = 'platform' (packages/metaschema-modules/deploy/schemas/metaschema_modules_public/tables/refusal_log_module/constraints/one_platform_scope.sql:8-10) duplicates the refusal_log_module_database_id_scope_unique UNIQUE (database_id, scope) constraint already created in table.sql at line 60.

The PR spans four packages. database-jobs reworks run_scheduled_job to track keyed and non-keyed schedules with in-flight detection and attempt budgeting, plus a new race test. metaschema-modules adds agent_module, inference_log_module, principal_auth_module, refusal_log_module, and user_auth_module tables with a one_platform_scope partial unique index and matching revert/verify files. metaschema-schema adds an is_valid_step_up guard validator. function-resolution updates install_route_bindings and resolve_capabilities with anonymous-route and capability-bundle semantics. utils adds a default_self_reference procedure. Generated sql/*.sql bundles, pgpm.plan files, and TypeScript tests/snapshots are updated in lockstep.

Files Change
database-jobs deploy + jobs race test Rework scheduled-job run to handle keyed/non-keyed schedules, in-flight locking, and attempt budgets
metaschema-modules tables + constraints + revert/verify Add five module tables and a partial unique one_platform_scope constraint with mirrored revert/verify
metaschema-schema is_valid_step_up Add declarative step-up guard validator for module tables
function-resolution procedures + tests Update route binding and capability resolution semantics
utils default_self_reference Add default self-reference procedure with revert/verify
sql bundles + plans + tests/snapshots Regenerate artifacts and update tests to match DDL changes

Reviewed commit: 0fb9407

@tenki-reviewer tenki-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This PR adds metaschema module tables and a step-up guard validator, reworks scheduled-job concurrency, and updates function-resolution route binding and capability resolution, along with regenerated SQL bundles and tests.

Key findings

-- covers nothing: fail_job cleared its locked_at, which otherwise reads
-- as "never been run" and wedges the schedule on a permanently failed
-- job. The keyed upsert below replaces it with a fresh attempt instead.
AND js.attempts < js.max_attempts

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 bug · high

Non-keyed schedule duplicates in-flight last-attempt job

The new probe condition AND js.attempts < js.max_attempts (packages/database-jobs/deploy/schemas/app_jobs/procedures/run_scheduled_job.sql:42) also excludes a job that is currently locked and running on its final attempt (attempts == max_attempts, locked_at set), even though such a job still covers the tick. For a schedule with sched.key IS NULL the keyed in-flight guard at lines 60-71 is skipped, so the function inserts a brand-new job while the previous tick is still executing, producing two concurrent executions of the same schedule. A non-keyed schedule with max_attempts = 1 whose worker is still processing when the next tick fires now enqueues a duplicate instead of raising ALREADY_SCHEDULED.

📋 Prompt for AI Agents

In packages/database-jobs/deploy/schemas/app_jobs/procedures/run_scheduled_job.sql around line 42, change the already-scheduled probe so that a locked (in-flight) job always raises ALREADY_SCHEDULED regardless of attempt budget. Replace AND js.attempts < js.max_attempts with AND (js.attempts < js.max_attempts OR js.locked_at IS NOT NULL). Rationale: the fix intended to let a permanently-dead keyed job (locked_at NULL, attempts >= max_attempts) be refreshed, but it also excludes a job currently running on its final attempt (locked_at NOT NULL, attempts == max_attempts), which still covers the tick. For non-keyed schedules the keyed in-flight guard at lines 60-71 is skipped, so this regression enqueues a duplicate concurrent job instead of raising ALREADY_SCHEDULED.

@pyramation
pyramation merged commit 9d08636 into main Sep 9, 2026
29 checks passed
@pyramation
pyramation deleted the feat/sync-from-constructive-db branch September 9, 2026 21:08
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