Commit Graph
14 Commits
Author SHA1 Message Date
Theo BrowneandClaude 6588b843da Handle root seed-home containment
Co-Authored-By: Claude <[email protected]>
2026-07-27 00:48:45 -07:00
Theo BrowneandClaude 6800ee32ea Address dev utility review findings
Co-Authored-By: Claude <[email protected]>
2026-07-27 00:44:44 -07:00
Theo Browne 2187054482 Keep seeded development state internally consistent 2026-07-26 20:40:30 -07:00
Theo BrowneandClaude Opus 5 b769bb6455 Resolve the database file itself; missing-target outranks same-source
Two Cursor findings on the previous guard fix:

- `canonicalDbPath` resolved only the directory and re-joined the
  filename, so a symlink at the *file* — `state.sqlite` itself linking
  into the shared home — passed the guard. Reproduced: the seed replaced
  the shared database through the link. Resolve the full path first (the
  file can be the link), falling back to directory-resolution only when
  the file does not exist yet, which is the one case a full resolution
  cannot handle and the one where the missing-target advice would create
  the database in the shared home.
- With --from and --to aliasing the same base and no database behind
  them, same-source fired before the existence check, hiding the
  actionable "start the dev server" guidance. The existence check now
  runs between the shared-home refusal (which must stay first — its
  advice is exactly wrong for the shared home) and same-source.

Verified seven ways: symlinked file refused with the shared rows
untouched, symlinked directory still refused, shared home with and
without a database refused as shared-home, aliased-empty reported as
missing-target, aliased-with-database as same-source, and a legitimate
seed unaffected.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-07-26 20:39:13 -07:00
Theo BrowneandClaude Opus 5 aad1bdd49a Refuse the shared home before reporting a missing database
Cursor caught a consequence of moving the shared-home guard after path
resolution: the missing-target check ran first, so a shared home with no
database yet got "start the dev server (`bun run dev`), then seed" —
advice that creates the database in the shared home and walks the user
into the exact write the guard refuses.

The refusals run first again. That surfaced why the previous ordering
existed: realPath fails on a missing final component, and falling back
to the literal path would lose symlink resolution precisely when the bad
advice would be given. Canonicalize the directory and re-join the
filename instead, so a symlinked state dir is resolved whether or not
the database behind it exists yet.

Verified all three ways: a shared home without a database is refused as
shared-home, a symlinked base without a database behind it is refused as
shared-home, and a legitimate empty target still gets the missing-target
hint.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-07-26 20:39:13 -07:00
Theo BrowneandClaude Opus 5 14a9306ea6 Guard the canonical database path, not the base directory
Macroscope found a bypass in the shared-home refusal: the guard
canonicalized the *base* directory, but a base can canonicalize to
itself while its `userdata` or `dev` subdirectory is a symlink into
`~/.t3`. The base comparison then passes and the seed destructively
replaces the real shared database — the exact accident the guard exists
to refuse. Reproduced with a symlinked fixture before fixing.

The database path is what gets opened, so it is what gets checked now:
canonicalize the resolved db path and refuse when it lies under the
canonical shared home. The same-source check moves to db paths for the
same reason. Guard ordering shifts after state-dir resolution, which the
guard now depends on; the missing-target check runs first either way.

Verified all three ways: the symlinked base is refused with the shared
database untouched, a direct --to at the shared home is still refused,
and a legitimate worktree seed still succeeds.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-07-26 20:39:13 -07:00
Theo BrowneandClaude Opus 5 6045ce23d0 Rank activities by sequence, scope the seed guard, unify the probes
Two Cursor Bugbot findings on the merge, plus the simplification they
enabled:

- The activity cap ordered `created_at` before `sequence`; the app ranks
  `sequence` first. The keys disagree outright under clock skew — a
  higher-sequence activity can carry an older timestamp — and then the
  cap keeps rows the timeline treats as oldest. Reordered to match, with
  the regression test now covering a skewed row, not just a tied one.
- The running-server guard blocked on a live server in either state
  directory while the seed touches exactly one. A server using only the
  other directory's database now no longer refuses the seed; both the
  pre-copy check and the pre-commit re-check take the resolved state dir.
- Scoping the probe meant threading one rule through both variants, so
  they now share a single implementation: the sync function reads and
  checks, and the Effect wrapper just lifts it. That deletes the schema
  decoder the Effect path used, removes the duplicated parsing, and
  makes the parity the tests pin structural rather than incidental.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-07-26 20:39:13 -07:00
Theo BrowneandClaude Opus 5 bc12b990a4 Re-check for a live server before commit; make the tie WAL-aware
Two findings from Macroscope review:

- The running-server guard was check-then-act. A copy takes seconds, and
  nothing stopped a server starting after the check returned None — the
  seed would then replace the projections and event log underneath it,
  which is exactly what the guard exists to prevent. `seedDevDatabase`
  now takes an optional probe and re-runs it immediately before COMMIT,
  where a rollback still costs nothing. This narrows the window to the
  commit itself rather than closing it: a filesystem read and a SQLite
  transaction share no lock, so a server starting in that last instant is
  still possible. Documented as such rather than claimed as airtight.
- The state-dir tiebreak read `state.sqlite` mtime, but the server runs
  SQLite in WAL mode, where writes land in `state.sqlite-wal` and the
  main file can stay untouched until a checkpoint. Verified: 50 writes
  left the main mtime unchanged while the sidecar advanced. So a busy
  directory lost the tie to a dormant one, and the seed overwrote the
  wrong database. Recency is now the newer of the two files.

Both verified end-to-end: a backdated main file with a fresh -wal now
wins the tie, and a probe returning a pid mid-copy leaves the target's
rows and event log intact.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-07-26 20:38:49 -07:00
Theo BrowneandClaude Opus 5 da233f0a96 Break the state-dir tie on mtime, not a fixed preference
Preferring `userdata` whenever both directories exist was wrong for the
case that motivated resolving at all: an implicit `bun run dev` stores
state in `dev`, so a base directory that had also been used with an
explicit `--home-dir` would seed the database the running server does
not open — looking like the seed did nothing.

Neither directory is categorically right, so the tie now goes to
whichever was written most recently: the one the last server actually
used. `userdata` still wins an equal or unavailable mtime. The chosen
path is already printed, so a wrong guess is visible rather than silent.

Verified both directions against a target holding both databases.

Caught by Cursor Bugbot.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-07-26 20:38:49 -07:00
Theo BrowneandClaude Opus 5 c92ad95fe3 Delete pending turn starts, and follow the real state directory
Three findings from Macroscope review:

- Settling `pending` rows to `interrupted` was the wrong verb. The server
  deletes them (`clearPendingProjectionTurnsByThread`) once the turn
  starts or the thread stops, and never settles one in place. They also
  cannot be settled coherently: `turn_id` is NULL, so an "interrupted"
  pending row is a terminal turn with no id, a shape no real turn has.
  Now deleted, matching the server's predicate exactly so a checkpoint
  row is never caught by it.
- `COALESCE(completed_at, requested_at)` preserved a stale completion on
  a running turn. One can carry a completion from an earlier settle that
  a later checkpoint reverted to "running" — the `...existingTurn` spread
  in ProjectionPipeline keeps the old value — and dating the interruption
  to that checkpoint gives a wrong duration. Overwritten unconditionally.
- The CLI hard-coded `<base>/userdata/state.sqlite` while a dev server
  under an implicit dev home stores state in `<base>/dev`. A main-checkout
  `bun run dev` was therefore unseedable, and said so by claiming no
  database existed and suggesting the user start the server that was
  already running. Source and target now follow whichever directory holds
  a database, `userdata` breaking the tie; attachments follow the same
  directory. Verified both a dev-only target and the precedence order.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-07-26 20:38:49 -07:00
Theo BrowneandClaude Opus 5 d3ee18cd2c Own the thread ceiling where the bindings are built
The cap was declared in the CLI while the constraint belongs to the
seeder's statements, and the number appeared in three places. That split
is the mechanism behind the last regression: a change to a statement in
the library silently invalidated a bound stated in the CLI, and the
mismatch only surfaced at the maximum — after the target was emptied.

Move it to `MAX_SEED_THREAD_LIMIT` beside the statements that have to
satisfy it, have the flag derive its bound from there, and check it in
`seedDevDatabase` before anything is opened so a caller bypassing the
flag fails while the target is still intact.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-07-26 20:38:49 -07:00
Theo BrowneandClaude Opus 5 8a76bb83cc Fix liveness probe, --json output, and remaining unsettled state
Follow-up review of the pairing path and a second pass over seeding:

- `isPersistedServerRuntimeStateLive` reported non-positive pids as live.
  `process.kill` reads 0 and -1 as signal-group targets rather than
  process lookups, so neither throws and both were caught by the "it
  didn't throw, so it's running" branch. A truncated or hand-edited file
  recording either would be believed forever — the dev candidate wins the
  devUrl preference, so it also outranks a genuinely live server
  elsewhere and mints tokens into a database nothing reads. Rejected in
  the schema and re-checked at the call site; the errno cast is now a
  guard, since an out-of-range pid throws a TypeError with no `code`.
- `pairing url --json` emitted warnings on stdout ahead of the payload.
  The probe reads both state directories, so a corrupt file in the one
  *not* in use broke JSON for any consumer piping it; the quiet-logs
  layer was only applied around the issuing step that runs after. Verified
  with a crafted fixture: stdout is now valid JSON.
- Turns copied in the `pending` state were left unsettled. Same defect as
  the `running` case fixed previously — a user message accepted before its
  provider turn started, which only that thread's next message would ever
  clear.
- The source was read across a dozen implicit transactions while the
  installed app may be writing to it, so a project could vanish between
  the query that selected it and the copy that read its row. One deferred
  read transaction pins every query to a single snapshot.
- `--from` and `--to` resolving to the same directory is now refused. Not
  for the data loss it appears to be (the source reads a pre-transaction
  snapshot, so the rows come back — verified in both journal modes), but
  because the event log, the cursors, and everything outside the caps do
  not.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-07-26 20:38:48 -07:00
Theo BrowneandClaude Opus 5 373e7919d1 Harden dev:seed against state it cannot settle
Review of the seeding path found five ways a seeded database could
describe something the copy cannot back up:

- Nothing stopped `dev:seed` from running against a live server. SQLite
  locking lets the seed take the write lock between the server's own
  writes and commit cleanly; what it cannot do is tell that server its
  projections and event log were replaced underneath it. Refuse instead,
  via a pid probe over both state directories.
- A thread copied mid-turn kept `state = 'running'`. The session override
  did not cover it — the turn is read independently, and an unsettled one
  leaves the thread spinning and its timeline unfoldable forever. Settle
  those to "interrupted", matching what the server does when a session
  leaves "running". Streaming messages are finished for the same reason.
- Attachment metadata was copied without the files behind it, so a seeded
  thread rendered an image whose request 404s. Report the referenced ids
  and copy the matching files alongside the rows.
- Orphaned `provider_session_runtime` rows survived the seed naming
  threads it had just deleted, which the session reaper then tried to
  stop against a provider instance the worktree may not define.
- `--threads` had no upper bound, so a large enough value died with "too
  many SQL variables" after the target was already emptied.

Also corrects the projector-cursor comment: `orchestration_events.sequence`
is AUTOINCREMENT, so emptying the log does not reset its high-water mark
and sequence does not restart at 1. Zero remains the right cursor, for the
reason now stated.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-07-26 20:38:48 -07:00
Theo Browne 180070865e Add dev pairing and seed utilities 2026-07-26 20:38:23 -07:00