Skip to content

[codex] Guard dev session schemas against silent drift#431

Merged
Cheggin merged 2 commits into
mainfrom
codex/session-schema-identity-guard
May 13, 2026
Merged

[codex] Guard dev session schemas against silent drift#431
Cheggin merged 2 commits into
mainfrom
codex/session-schema-identity-guard

Conversation

@Cheggin
Copy link
Copy Markdown
Collaborator

@Cheggin Cheggin commented May 13, 2026

Summary

  • add a deterministic SessionDb schema identity derived from SQLite sqlite_schema
  • track the expected fresh-schema ID in src/main/sessions/schema-manifest.json
  • add local db:schema:check / db:schema:update commands and a CI check before coverage
  • add per-worktree DB/profile commands:
    • task worktree:profile:path
    • task worktree:up
    • task db:worktree:copy FROM=default
    • task db:worktree:doctor
    • task worktree:profile:clean FORCE=1
  • add tracked agent instructions in root AGENTS.md plus the app-spawned harness AGENTS.md

Why

Worktree-local sessions.db copies can be moved between branches. DB_SCHEMA_VERSION/user_version catches newer-vs-older DBs, but it does not prove two branches with the same version have the same actual schema shape.

This PR gives developers a direct workflow to copy a stopped sessions.db into the active branch profile and immediately diagnose schema/version drift. It also keeps task agent:run aligned with the same AGB_USER_DATA_DIR, which is required because the task runner reads <userData>/local-task-server.json.

Notes

  • The copy command copies only sessions.db plus WAL/SHM companions, not volatile runtime files like local-task-server.json, logs, harness output, or crash data.
  • The schema identity check is a CI/dev guard; it does not add a production metadata table or change app startup behavior for existing installs.

Validation

  • yarn db:schema:check
  • task db:schema:check
  • task db:schema:update
  • task worktree:profile:path
  • task db:worktree:copy against a throwaway DB
  • task db:worktree:doctor against a throwaway DB
  • task worktree:profile:clean against a throwaway profile
  • task typecheck
  • task lint (0 errors; existing warnings remain)
  • git diff --check

Known baseline

  • Full npm run test is not clean in this checkout because tests/unit/sessions/BrowserPool.test.ts has two existing zoom-fit assertion failures unrelated to this DB/schema change.

@Cheggin Cheggin marked this pull request as ready for review May 13, 2026 17:42
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 9 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="app/package.json">

<violation number="1" location="app/package.json:52">
P2: The new `db:schema:*` scripts use single-quoted `--compiler-options` JSON, which is not cross-shell safe and can fail on Windows npm runs. Use escaped double quotes so ts-node receives valid JSON on all platforms.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review, or fix all with cubic.

Comment thread app/package.json
"visual:qa": "npm run visual:capture && npm run visual:diff",
"qa:review": "open tests/visual/review.html",
"qa": "npm run lint && npm run typecheck && npm run test",
"db:schema:check": "ts-node --project tsconfig.json --compiler-options '{\"module\":\"CommonJS\"}' scripts/session-schema-manifest.ts --check && vitest run tests/unit/sessions/SessionDb.schemaIdentity.test.ts",
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot May 13, 2026

Choose a reason for hiding this comment

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

P2: The new db:schema:* scripts use single-quoted --compiler-options JSON, which is not cross-shell safe and can fail on Windows npm runs. Use escaped double quotes so ts-node receives valid JSON on all platforms.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At app/package.json, line 52:

<comment>The new `db:schema:*` scripts use single-quoted `--compiler-options` JSON, which is not cross-shell safe and can fail on Windows npm runs. Use escaped double quotes so ts-node receives valid JSON on all platforms.</comment>

<file context>
@@ -49,6 +49,8 @@
     "visual:qa": "npm run visual:capture && npm run visual:diff",
     "qa:review": "open tests/visual/review.html",
     "qa": "npm run lint && npm run typecheck && npm run test",
+    "db:schema:check": "ts-node --project tsconfig.json --compiler-options '{\"module\":\"CommonJS\"}' scripts/session-schema-manifest.ts --check && vitest run tests/unit/sessions/SessionDb.schemaIdentity.test.ts",
+    "db:schema:update": "ts-node --project tsconfig.json --compiler-options '{\"module\":\"CommonJS\"}' scripts/session-schema-manifest.ts --write",
     "start:reset-onboarding": "node scripts/reset-onboarding.mjs",
</file context>
Fix with Cubic

@Cheggin Cheggin force-pushed the codex/session-schema-identity-guard branch from 394af67 to 567d721 Compare May 13, 2026 17:50
@Cheggin Cheggin marked this pull request as draft May 13, 2026 17:50
@Cheggin
Copy link
Copy Markdown
Collaborator Author

Cheggin commented May 13, 2026

@cubic review

@Cheggin Cheggin marked this pull request as ready for review May 13, 2026 17:52
@cubic-dev-ai
Copy link
Copy Markdown

cubic-dev-ai Bot commented May 13, 2026

@cubic review

@Cheggin I have started the AI code review. It will take a few minutes to complete.

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 12 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="app/scripts/dev-profile.mjs">

<violation number="1" location="app/scripts/dev-profile.mjs:379">
P1: Guard against copying a profile onto itself. With `--force`, the current logic can delete the source path when source and target resolve to the same location.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review, or fix all with cubic.

ensureAppStopped(productName, opts);
const source = resolveProfileRef(opts.from ?? 'default', productName);
const target = resolveProfileRef(opts.to ?? opts.name ?? currentBranchName(), productName);
copyProfile(source, target, opts);
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot May 13, 2026

Choose a reason for hiding this comment

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

P1: Guard against copying a profile onto itself. With --force, the current logic can delete the source path when source and target resolve to the same location.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At app/scripts/dev-profile.mjs, line 379:

<comment>Guard against copying a profile onto itself. With `--force`, the current logic can delete the source path when source and target resolve to the same location.</comment>

<file context>
@@ -0,0 +1,419 @@
+    ensureAppStopped(productName, opts);
+    const source = resolveProfileRef(opts.from ?? 'default', productName);
+    const target = resolveProfileRef(opts.to ?? opts.name ?? currentBranchName(), productName);
+    copyProfile(source, target, opts);
+    const result = {
+      message: `copied ${opts.dbOnly ? 'session DB' : 'profile'} from ${source} to ${target}`,
</file context>
Fix with Cubic

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

2 issues found across 12 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="app/src/main/sessions/schemaIdentity.ts">

<violation number="1" location="app/src/main/sessions/schemaIdentity.ts:35">
P2: Global whitespace collapsing can hide real schema differences by altering quoted SQL literals before hashing.</violation>
</file>

<file name="app/scripts/dev-profile.mjs">

<violation number="1" location="app/scripts/dev-profile.mjs:121">
P1: Refs containing `/` are misclassified as filesystem paths, so common branch-style names are resolved to unintended directories.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review, or fix all with cubic.

Comment thread app/scripts/dev-profile.mjs Outdated
}

function looksLikePath(ref) {
return ref.startsWith('.') || ref.startsWith('~') || isAbsolute(ref) || ref.includes('/') || ref.includes('\\');
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot May 13, 2026

Choose a reason for hiding this comment

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

P1: Refs containing / are misclassified as filesystem paths, so common branch-style names are resolved to unintended directories.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At app/scripts/dev-profile.mjs, line 121:

<comment>Refs containing `/` are misclassified as filesystem paths, so common branch-style names are resolved to unintended directories.</comment>

<file context>
@@ -0,0 +1,419 @@
+}
+
+function looksLikePath(ref) {
+  return ref.startsWith('.') || ref.startsWith('~') || isAbsolute(ref) || ref.includes('/') || ref.includes('\\');
+}
+
</file context>
Suggested change
return ref.startsWith('.') || ref.startsWith('~') || isAbsolute(ref) || ref.includes('/') || ref.includes('\\');
return ref.startsWith('.') || ref.startsWith('~') || isAbsolute(ref);
Fix with Cubic

Comment thread app/src/main/sessions/schemaIdentity.ts Outdated
Worktree-local copies of sessions.db can be copied between branches, so DB_SCHEMA_VERSION alone does not prove the actual SQLite schema shape still matches the checkout. This adds a deterministic schema identity derived from sqlite_schema, stores the expected identity in a repo manifest, and gives developers task commands to copy a stopped sessions.db into the active branch profile and diagnose schema mismatches.

Constraint: Existing production databases should keep opening through the current SessionDb migration path without a new metadata table requirement.

Constraint: task agent:run must use the same userData root as the running app because it reads local-task-server.json.

Rejected: Adopt Drizzle migration tooling | new dependency and ORM/tooling migration is broader than the current hand-written SessionDb migration surface needs.

Rejected: Store schema ID in production databases | requires a metadata migration and can change runtime behavior for existing installs.

Rejected: Copy whole runtime profiles by default | local-task-server.json, logs, harness, and crash data are volatile per-run state.

Confidence: high

Scope-risk: moderate

Directive: Keep worktree profile commands aligned with AGB_USER_DATA_DIR and do not copy SQLite files while the app is running.

Tested: yarn db:schema:check; task db:schema:check; task db:schema:update; task worktree:profile:path; slash-containing FROM branch profile copy against a throwaway DB; task db:worktree:doctor against a throwaway DB; task worktree:profile:clean against a throwaway profile; task typecheck; task lint; git diff --check

Not-tested: Full npm run test remains blocked by existing BrowserPool zoom-fit assertion failures.

Co-authored-by: OmX <omx@oh-my-codex.dev>
@Cheggin Cheggin force-pushed the codex/session-schema-identity-guard branch from 567d721 to 002e37c Compare May 13, 2026 18:00
@Cheggin
Copy link
Copy Markdown
Collaborator Author

Cheggin commented May 13, 2026

@cubic review

@cubic-dev-ai
Copy link
Copy Markdown

cubic-dev-ai Bot commented May 13, 2026

@cubic review

@Cheggin I have started the AI code review. It will take a few minutes to complete.

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

No issues found across 12 files

@Cheggin Cheggin merged commit 1d0c22f into main May 13, 2026
8 of 9 checks passed
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