Repository navigation
Conversation
`readLatestRunLog` matched the wrong run-log under two scenarios: 1. Concurrent `sequant_run` calls — both filenames passed the 5-minute recency filter, and `.sort().reverse()` returned whichever was lexicographically latest. 2. Stale same-issue log — a prior run's log within the 5-minute window was returned when fsync lag meant the new log wasn't on disk yet. Shared root cause: the MCP server had no way to identify which log file belonged to the subprocess it just spawned. Fix threads the runId end-to-end: - `emitRunIdLine(runId)` in `batch-executor.ts` writes `SEQUANT_RUN_ID:<uuid>\n` to stderr (gated on `SEQUANT_ORCHESTRATOR`, mirroring `emitProgressLine`). - `run-orchestrator.ts` calls it immediately after `logWriter.initialize()` so the line precedes any `SEQUANT_PROGRESS` event. - MCP `run.ts` captures the line via the existing stderr line-buffer (now always-on so capture works without a `progressToken`) and uses a new `readRunLogById(runId)` helper that matches by exact filename suffix. - Fallback to `readLatestRunLog(runStartTime)` preserves behavior on older CLIs (no runId emitted) and on lookup miss — no new failure mode.
6 tasks
…k logging
Refactor inline closure state out of `registerRunTool` so the runId-capture
and lookup-fallback paths can be exercised directly by tests. Adds
observability for the silent fallback path identified in QA Gap 3.
Changes:
- Extract `createRunIdCapture()` factory from the `registerRunTool` closure.
Returns `{ routeLine, getCapturedRunId }`; `routeLine` captures the first
valid runId line and otherwise delegates to `parseProgressLine`,
returning the parsed event for the caller to emit.
- Extract `resolveRunLog(capturedRunId, runStartTime)` from the inline
`readRunLogById ?? readLatestRunLog` chain at the spawn-result site.
When a captured runId is set but `readRunLogById` returns null
(corrupted file, slow fsync), emit
`[mcp:run] runId <id> lookup miss — falling back to readLatestRunLog`
on the MCP server's own stderr (`console.error`) so the silent
fallback is observable.
- Update `hasProgressToken` comment to document its surviving purpose
(gates `onProgress` timeout-reset only; no longer gates the buffer).
Test coverage added (14 new, total 77/77 in run.test.ts):
- `createRunIdCapture`: capture semantics, only-first-wins, fall-through
to progress parsing, before- and after-capture paths.
- `createRunIdCapture + createLineBuffer` integration: chunk-boundary
reconstruction, end-to-end AC-5 (two concurrent captures resolve to
their own logs even when the wrong filename would win `.sort().reverse()`),
end-to-end AC-6 (stale same-issue log is not returned; `readFile`
throws on the stale path so regressions fail loudly).
- `resolveRunLog`: hit path (no debug line), miss-with-fallback path
(debug line includes runId), null-capturedRunId path (no debug line),
both-null path (returns null).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes
readLatestRunLoginsrc/mcp/tools/run.tsreturning the wrong log file under two scenarios:sequant_runcalls. Both filenames passed the 5-minute recency filter, and.sort().reverse()returned whichever filename was lexicographically latest — the wrong run could return another run's results.Shared root cause: MCP had no way to identify which log file belonged to this subprocess.
The fix threads the
runIdend-to-end:emitRunIdLine(runId)insrc/lib/workflow/batch-executor.tswritesSEQUANT_RUN_ID:<uuid>\nto stderr (gated onSEQUANT_ORCHESTRATOR, mirroringemitProgressLine).run-orchestrator.tscalls it immediately afterlogWriter.initialize()so the line precedes anySEQUANT_PROGRESSevent.run.tscaptures the line via the existing stderr line-buffer (now always-on so capture works regardless of whether the client passes aprogressToken) and uses a newreadRunLogById(runId)helper that matches by exactendsWith(-<runId>.json)suffix.readLatestRunLog(runStartTime)preserves today's behavior on older CLIs and on lookup miss — no new failure mode (AC-4).Carved out from #508 per the 2026-04-09 architecture review; the parent in-process MCP rewrite remains parked.
AC Verification
SEQUANT_RUN_ID:<uuid>once on stderr, before anySEQUANT_PROGRESSeventemitRunIdLineinbatch-executor.ts; called inrun-orchestrator.tsright afterlogWriter.initialize(). Test:emitRunIdLine (#631 AC-1) > should be emittable before emitProgressLine (ordering invariant)readRunLogById(runId)added; exact filename suffix match; returnsnullif absentsrc/mcp/tools/run.ts:readRunLogById. Tests inreadRunLogById (#631 AC-2, AC-5, AC-6)describe blockSEQUANT_RUN_IDand usesreadRunLogByIdwhen availablehandleLineroutes runId lines tocapturedRunId; readlog path atrun.tsusescapturedRunIdfirstreadLatestRunLog(runStartTime)when runId not capturedcapturedRunId !== null ? await readRunLogById(...) : null) ?? await readLatestRunLog(...)— fallback also fires on lookup misssequant_runcalls return their own logsshould return the caller's own log when concurrent run logs coexist— both filenames pass the time filter but lookup returns the caller's own logshould not return a stale same-issue log when looked up by fresh runId— stale file is lexicographically later, would have won underreadLatestRunLog; runId lookup ignores itTest plan
npm run build— cleannpm run lint— cleannpx vitest run src/mcp src/lib/workflow src/commands/run.test.ts— 1100 passednpx vitest run __tests__/integration/mcp-e2e.integration.test.ts __tests__/integration/run-orchestrator.integration.test.ts— 19 passedparseRunIdLine(4),readRunLogById(6 including AC-5 + AC-6 race fixtures),emitRunIdLine(3 including ordering invariant)Closes #631