Skip to content

fix(science): never let a synthetic run take a journal ordinal - #784

Merged
Ishaan Gangwani (ishaan1124) merged 4 commits into
synthetic-sciences:mainfrom
aniruddhaadak80:fix/execution-history-sequence
Sep 29, 2026
Merged

Ishaan Gangwani (ishaan1124) merged 4 commits into
synthetic-sciences:mainfrom
aniruddhaadak80:fix/execution-history-sequence

Conversation

@aniruddhaadak80

Copy link
Copy Markdown
Contributor

What does this PR do, and why?

ExecutionHistory.list numbers a session's runs with one shared counter that
mixes two different sequence spaces:

const storedSequence = Sequence.safeParse(run.meta?.executionSequence)
const sequence = storedSequence.success ? storedSequence.data : (count.get(session) ?? 0) + 1
count.set(session, sequence)

count is advanced by every run, but only kernel cells carry a real
executionSequence (assigned by ExecutionHistory.submit). A local_compute run
has no meta.executionSequence — tool/bash.ts records
{ messageID, callID, exit, cwd, stdout, stderr } — so it takes the synthetic
branch and is handed count + 1, which is exactly the ordinal the durable
journal already gave the next kernel cell.

The result, reproduced in the new test with python → bash → python:

run_kernel_1  sequence 1
run_shell     sequence 2     ← the same number the journal gave run_kernel_2
run_kernel_2  sequence 2

The agent reads sequence as "execution N of this session". The ledger now
disagrees with the journal about which result is the second result, and
anything keying on (session_id, sequence) — including the ordered rendering,
which tie-breaks on a content hash — sees a collision and an arbitrary order.

The fix reserves the journal-assigned ordinals first, so a synthetic number can
never land on one:

const sequence = storedSequence.success ? storedSequence.data : (count.get(session) ?? 0) + 1
if (!storedSequence.success) {
  const taken = reserved.get(session)
  while (taken?.has(sequence)) sequence += 1
}
// Monotone, so a journal ordinal seen later cannot rewind the counter.
count.set(session, Math.max(count.get(session) ?? 0, sequence))

The Math.max matters: without it, a journal ordinal of 2 seen after a
synthetic 3 would rewind the counter to 2 and the next synthetic run would
collide again.

What this does not change

A sequence-less run is still numbered after the ones before it, so it remains
slightly out of chronological position. Numbering it correctly needs tool/bash.ts
to take its ordinal from ExecutionHistory.submit so both producers share one
counter — a larger change than this fix, and one that would touch the hot write
path. The invariant repaired here is that no two runs claim the same number,
which is what the journal and any consumer keying on it need.

Linked issue

Self-identified. Happy to file a report if you would rather track it; my issue
budget this turn went to the two apply_patch defects and the localStorage
copies.

How did you verify it?

One case added to backend/cli/test/science/execution-history.test.ts:
a run recorded without a sequence does not take a kernel execution's number.
It records a kernel run at ordinal 1, a local_compute run exactly as
tool/bash.ts records it (envelope with no executionSequence, meta.exit), and
a kernel run at ordinal 2, then asserts the sequence numbers are unique and that
the two kernel runs keep 1 and 2.

On unmodified 3e94875c:

error: expect(received).toBe(expected)
Expected: 3
Received: 2

Commands run:

  • bun test --timeout 120000 ./test/science/execution-history.test.ts
    → 4 pass, 0 fail
  • bun run --cwd backend/cli typecheck → exit 0

The whole file matters more than the new case: the three pre-existing cases cover
two runs with no ordinal (both synthetic, so no collision) and two runs that
both have one (no synthetic allocation), which is precisely why the mixed case
survived. All three still pass, so neither numbering path regressed.

Checklist

  • bun run check is green (format, typecheck, backend + frontend/ui + SDK tests) — blocked on this Windows checkout by the CRLF and symlink artifacts in my other pull requests; backend typecheck is clean and the execution-history suite is green
  • bun run --cwd frontend/workspace build succeeds if I touched frontend/workspace or frontend/ui — not touched
  • ./tooling/repo/generate.ts was run and the tooling/sdk output committed if I changed backend/cli/src/server — not touched
  • CHANGELOG.md has an Unreleased entry if the change is user-visible
  • The matching docs page under frontend/docs/src/content/openscience/ is updated if behavior changed — no doc change needed: the ordinal contract is unchanged, only the collision is gone
  • Screenshots or a short video are attached for UI changes — not a UI change
  • No version bumps (package.json versions and tags are written by the release workflow)
  • install and frontend/landing/public/install are still byte-identical if I touched either — not touched

@vercel

vercel Bot commented Sep 28, 2026

Copy link
Copy Markdown

ANIRUDDHA ADAK (@aniruddhaadak80) is attempting to deploy a commit to the InkVell Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, the collision is real and the test shows it. The fix is incomplete, though.

In backend/cli/src/science/execution/history.ts, the new loop reserves ordinals only from finished runs (runs). A kernel run that is queued or still running exists only in the journal (durable), so a shell run can still take its number. With this PR applied, a finished shell run and a queued kernel run both came back as sequence 1.

Please also reserve the journal's ordinals:

for (const record of durable) add(record.session_id, record.sequence)

and add a test with a queued kernel run. (For context, the only consumer today is the /provenance/executions route, so this is low urgency.)

The list numbered every run from one counter, but only kernel cells carry a real
executionSequence, so a local shell run took the next number — the one the
durable journal had already given the following kernel execution. Reserve the
journal-assigned ordinals first and skip them when synthesising, keeping the
counter monotone.
A kernel run that is queued or still running has no provenance node yet, so
reserving only the ordinals already stamped on graph runs left its number
free: a finished shell run, which carries no ordinal of its own, was handed
sequence 1 while the journal already held it, and the two records then
contradict each other for the same session. Read the ordinals out of the
journal as well, which is the authority that assigned them.
@aniruddhaadak80

Copy link
Copy Markdown
Contributor Author

Done in 3839585.

  • The reservation loop now also reads the journal: for (const record of durable) reserve(record.session_id, record.sequence) runs before the graph runs, so a queued or still-running kernel cell holds its number even though it has no provenance node yet. Both loops share one reserve helper over the same Map<string, Set<number>>, and the journal is read first because it is the authority that assigned those numbers.
  • Added a queued kernel execution keeps its number from a finished shell run: it submits a queued kernel execution (which takes sequence 1), records a finished bash run with no executionSequence beside it, and asserts the kernel record is 1 and the shell record is 2. Against the previous loop the shell run came back as 1, which is the collision you described — I ran it that way to confirm.
  • Noted that asserting uniqueness alone would not have caught it: the merge at the end keys on session_id + sequence, so the colliding legacy record overwrote the journal one and the list still looked unique. The test pins the actual numbers instead.
  • Rebased onto f1bdf252.

@aniruddhaadak80

Copy link
Copy Markdown
Contributor Author

All checks are green on 3839585d — the rebase resolved the conflict and the new tests pass. Ishaan Gangwani (@ishaan1124) ready for another look whenever you have a moment.

The file had been re-saved with a UTF-8 BOM and a cp437 round trip that
turned the existing fixture label 'Result · version 1' into mojibake.
Restoring main's bytes leaves the new tests as the file's only change.

Co-authored-by: Cursor <[email protected]>
@ishaan1124

Copy link
Copy Markdown
Member

I pushed one commit (b8335cb) that restores execution-history.test.ts to UTF-8: the file had picked up a BOM and a mojibake fixture label (Result ┬╖ version 1). The ordinal fix itself is unchanged. CI is green.

@ishaan1124
Ishaan Gangwani (ishaan1124) dismissed their stale review September 29, 2026 15:33

The requested changes are in and verified; CI is green.

@ishaan1124
Ishaan Gangwani (ishaan1124) merged commit 7f39c0a into synthetic-sciences:main Sep 29, 2026
8 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.

2 participants