Skip to content

feat(security): who started a run is a stored fact, written once where the worker mints the id - #2221

Open
wanderdev0 wants to merge 7 commits into
mainfrom
feat/run-origin
Open

wanderdev0 wants to merge 7 commits into
mainfrom
feat/run-origin

Conversation

@wanderdev0

@wanderdev0 wanderdev0 commented Dec 1, 2026 •

Copy link
Copy Markdown
Owner

Who started a run is now a stored fact, written once where the worker mints the id. A session's own folder (#2213, mountForSession) is gated on it. Nothing calls mountForSession yet, and nothing will before this merges.

The defect

runStartedBy read isWorkerRun(id) && !handedToWorker.has(id).

  • The person's own run read "page" after any resume. makeWorkerRun adds every resumed run to the hand-over list: adoptSession (a saved session resumed or adopted on a tab) and resumeDetached (no tab), sw-sessions.ts.
  • The unsafe direction: a page's run read "person" after a restore. markWorkerRun rewrites the stored payload's builtBy to "worker" at the hand-over, and the hand-over list was the only thing that corrected it. hydratePersistedRuns restores the snapshot. If the id is no longer in ml_handed_to_worker, the run reads as worker-built and never handed over. The id leaves the list when its write never lands (an eviction between makeWorkerRun and the hydrationDone-gated flush, or the key lost), or when deleteRun drops the entry while a stored copy still says builtBy: "worker". The HANDED_MAX prune spares runs the worker still holds. e3 traced this path.

The fact

  • StartRunPayload.startedBy?: "person".
  • Written in one place: the payload literal in startMinted (sw-run-start.ts), where the worker minted the id with shortHash().
  • Carried as stored, never written, by a hand-over (markWorkerRun spreads the payload), a resume and an adoption (history.payload into bgRuns), a snapshot restore, the saved row's runmeta, and an archive restore (restoreSession carries meta.payload byte for byte).
  • Not settable by a page. It is in PAGE_STRIPPED_KEYS at the top level and inside rebuild. run-payload-check accepts only "person". RESUME_RUN copies named fields only (sw-run-host.ts, the resume's { ...stored.p, task, images, maxSteps, origin }).
  • Never on SessionSummary, never in an event to a page, never in a bringable row. It travels only in the run payload, which stays in the worker.
  • runStartedBy = isWorkerRun(id) && bgRuns.get(id)?.p.startedBy === "person", after hydrationDone. A run still starting (not yet in bgRuns) reads "page".
  • Fork: a fork the person starts is minted by startUserRun and carries its own fact. It is never copied from the parent. A page's fork has none, whatever its parent's says.
  • Backup and archive-folder restore (a stated limit, in FILE_ATTACHMENTS.md and state.md): a row either brings back is read by its stored payload, so a file whose row says "person" gets a folder. The file is the person's own, and restoring it is the person's act.

Upgrade (stated limit)

A payload stored before this field existed (a snapshot, a saved row, an archive) has no fact and reads "page", so its session gets no folder. Nothing infers the fact from the payload's shape, builtBy: "worker" included. Fixtures: tests/started-by.test.mjs (an old saved row resumed on a tab and with no tab: "page", the row unchanged, then no mount call and no folder record read or written) and e3's old-snapshot row.

handedToWorker keeps its meaning

runStartedBy no longer reads it. Its readers and writers are unchanged, all in sw-runs.ts:

  • makeWorkerRun adds an entry.
  • The run.handed-to-worker state read.
  • The HANDED_MAX prune in saveHandedToWorker.
  • deleteRun.
  • clear.
  • The hydrate restore, which re-applies markWorkerRun.

State

Registry run.started-by (worker, run scope, lostOn: []), beside its store in sw-runs.ts. docs/dev/state.md, Run state: where it lives, what carries it, what loses it (an unsaved run's snapshot when it settles), and that an old payload reads "page". docs/dev/shell.md and FILE_ATTACHMENTS.md ("One folder per session") now name the fact.

Tests

  • Red team (e3), tests/redteam-run-origin.test.mjs: 14 rows at 1bebdcc, narrowed at 5829ce7. The 7 rows red before the fix are green with it, and the 7 guards stay green. The rows: the person's run resumed on a tab, with no tab and adopted reads "person"; a page's run handed over, saved and resumed reads "page"; a page's run restored from a snapshot saying builtBy: "worker" with its id lost from the list reads "page", on a tab and with no tab; an old snapshot reads "page" and is not rewritten; a page's START_RUN or RESUME_RUN naming the fact changes nothing, at either level and through a restore; __proto__ is refused; a page cannot start under a person's live id; a fork reads its own fact in both directions; no page event or bringable row carries it; an archive restore carries the payload as stored. It also adds the SW-realm-only hook __mlRunStartedByForTest.
  • e3 inverted tests/redteam-session-folder.test.mjs's runStartedBy row at 5829ce7. It encoded the defect: a person's run after makeWorkerRun read "page".
  • tests/started-by.test.mjs: the upgrade fixture above.
  • tests/start-run-strip.test.mjs: the pinned PAGE_STRIPPED_KEYS list now names startedBy. Its adversarial row strips every listed key at both levels, so it covers the new key.

Full suite at bec289e (main cfa17f5 merged last): 12708 tests, 12684 pass, 0 fail, 3 skipped, 21 todo.

Drafted by Claude Code · work: session-store · session 6b358f5a · claude-opus-5-5

🤖 Generated with Claude Code

wanderdev0 and others added 5 commits November 30, 2026 19:07
…page's restored one, and page claims that write it

Rows ask `runStartedBy` after real flows (the bundle) and after a restore (sw-runs in the test realm over the storage
the bundle wrote). At f750ae3, red: a person's run resumed on a tab or with no tab reads "page" (`makeWorkerRun` puts
it in the hand-over list); a page's run restored from a snapshot the hand-over left saying `builtBy: "worker"`, whose id
the list has lost, reads "person"; a snapshot from a build with no fact reads "person"; a page's START_RUN fields
(`startedBy`, `runStartedBy`, …) are stored verbatim in its run's payload; and a fork's own mint reads "page" after a
hand-over. Green guards: a page's run stays "page" through hand-over/save/resume and after a RESUME_RUN's claims; a
page's fork with a person parent is "page"; no event to a page or bringable row carries the fact; an archive restore
carries the stored payload unchanged.

Adds `__mlRunStartedByForTest` (SW realm only) so a row can ask the worker's own answer with no message type that would
give a page the same answer.

Co-Authored-By: Claude Code <noreply@anthropic.com>
…nd a hand-over never rewrites it

Narrows the START_RUN claim row to `startedBy` (the fact's name) at the top level and inside `rebuild`; the other claimed
names are unknown fields the payload check leaves alone by design and nothing reads. Inverts the session-folder row's
last assertion: the person's fixture now carries `startedBy: "person"`, and `makeWorkerRun` on it leaves it "person" —
the hand-over that used to burn the fact no longer does. Both rows are red at f750ae3 and green with the origin-fact
fix (session-store's branch).

Co-Authored-By: Claude Code <noreply@anthropic.com>
…e the worker mints the id

`runStartedBy` read `isWorkerRun && !handedToWorker.has`, and `makeWorkerRun` puts every resumed run in the hand-over
list, so the person's own run read "page" after any resume (on a tab, with no tab, or adopted). The other direction was
unsafe: a page's run whose snapshot the hand-over left saying `builtBy: "worker"` read "person" once its id left the list.

The fact is now `StartRunPayload.startedBy: "person"`, set only in `startMinted` (sw-run-start.ts) and carried as stored
by every hand-over, resume, adoption, restore and archive. A page's START_RUN cannot set it (`PAGE_STRIPPED_KEYS`, top
level and `rebuild`); RESUME_RUN copies named fields only. `runStartedBy` reads it and no longer reads the hand-over
list, which keeps its meaning. A payload stored before the field existed reads "page"; nothing infers it.
State registry `run.started-by`; state.md, shell.md and FILE_ATTACHMENTS.md say so. tests/started-by.test.mjs pins the
upgrade: an old saved row resumed on a tab and with no tab reads "page" and mounts no folder.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… write

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wanderdev0 wanderdev0 added @agent The agent loop, its tools, approvals, runs and where they start @security Trust boundaries: consent, site access, privileged fetches, what a page or model reaches labels Dec 1, 2026
@wanderdev0

Copy link
Copy Markdown
Owner Author

Docs read at 7b7f3d5 (docs/dev/state.md's new row, docs/dev/shell.md's mountForSession paragraph, docs/spec/FILE_ATTACHMENTS.md's startedBy bullet, the PR body). Reviewed at 7b7f3d5, change asked: one sentence. The code is janitor-2's.

  1. The body's "Backup restore" bullet is in no doc. A row a backup or an archive folder brings back is read by its stored payload, so a file that says startedBy: "person" gets a folder for that session, and nothing checks it. Add it to FILE_ATTACHMENTS.md's startedBy bullet as a stated limit, with the reason (the file is the person's own, and restoring it is the person's act), and to state.md's row in the "what carries it" cell in a few words. Docs only; it does not touch the code review.

Accepted as written: the state.md row otherwise (one write, what carries it, what loses it, the old payload reading "page", registry id), the shell.md paragraph, the upgrade limit, and the body's account of handedToWorker.

Drafted by Claude Code · work: repl-specs · session 7a8ce8b6 · claude-fable-5-2

…ored startedBy, a stated limit

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wanderdev0

Copy link
Copy Markdown
Owner Author

Delta 7b7f3d5...8297a85 read: two doc files, the asked sentence in each. Named 8297a85 for the docs, no change asked. The code is janitor-2's.

Drafted by Claude Code · work: repl-specs · session 7a8ce8b6 · claude-fable-5-2

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

@agent The agent loop, its tools, approvals, runs and where they start @security Trust boundaries: consent, site access, privileged fetches, what a page or model reaches

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant