Skip to content

fix(runtime): persist attachment bytes so reconstructed context matches what the model saw (F05) - #69

Merged
lg2m merged 1 commit into
mainfrom
fix/eng-788-f05-attachment-artifacts
Sep 17, 2026
Merged

lg2m merged 1 commit into
mainfrom
fix/eng-788-f05-attachment-artifacts

Conversation

@lg2m

@lg2m lg2m commented Sep 17, 2026

Copy link
Copy Markdown
Collaborator

Linear: ENG-788. Audit finding F05 (docs/design/harness-scale-audit-2026-09-16.md), the last open item in work-order 1.

Behavior

Before: attach a.txt (ALPHA_ORIGINAL), complete a run, send continue. Request 1 contains the bytes; request 2 contains inspect\[email protected]. Same after reopen, in the compaction summarizer's input, and — worse — in the retry after an automatic compaction, which re-read the file and could send different bytes than the first attempt.

After: every later request reproduces the first request's <attached-file> blocks byte-for-byte from the store, whatever happened to the file since. MessageSnapshot.output (the transcript row) still shows @a.txt for display.

Design

  • Schema 28 → 29. attachment_blobs(session_id, blob_key, content, content_bytes, created_at_ms, evicted_at_ms) keyed by whole-file SHA-256 (+ start-end for ranges) so a file attached to several prompts is stored once per session. message_attachments(message_id, ordinal, path, digest, blob_key, window_*) in prompt order. Both written in the start_reserved_run transaction with RunStarted, so a run is never "started" with attachments that weren't kept. Deleted with the session.
  • Read path. load_model_context adds one indexed query per assembly (empty for sessions without attachments) and re-renders through the same render_file_attachment the live run uses. Prompts that never started, or predate schema 29, keep their placeholder text.
  • Bounded. MAX_SESSION_ATTACHMENT_BYTES = 64 MiB (mirrors the spill cap). Eviction nulls content on the oldest blobs not referenced by a running/queued prompt; the row stays, and the block renders as <attached-file path=".." evicted="true"> with a note to re-read — never the bare placeholder. INSERT OR IGNORE keeps the first copy so an evicted blob is not resurrected by a later re-attach (which would make an earlier prompt's rendering depend on a later one).
  • Retry after auto-compaction. ClaimedRun.resolved_input: Option<Arc<ResolvedInput>> holds the first resolution; prepare_execution reuses it instead of re-reading. reload_reserved_messages unchanged.
  • Alternative rejected: a messages.resolved_output TEXT column. Smaller diff, but duplicates up to 1 MiB per prompt with no dedup and no eviction story; the audit's acceptance asked for both.
  • No PROTOCOL_VERSION change; no new events.

Tests

  • attached_files_are_reconstructed_as_the_model_first_saw_them: whole file + range, modify one file and delete the other, continue → request 2 equals request 1's prompt text; re-attach modified file (new blob) then again (dedup) → 3 blobs / 4 references; reopen on a fresh runtime → same; assert_assembly_matches_reference oracle (updated to look up attachments per message); DeleteSession empties both tables.
  • evicted_attachments_render_an_explicit_stub_not_the_placeholder.
  • an_over_budget_prompt_keeps_its_first_attachment_bytes_across_auto_compaction: file rewritten while the summary runs; retried prompt and its follow-up still carry ALPHA_ORIGINAL.
  • version_twenty_nine_migration_adds_the_attachment_tables_empty.
  • input::tests cover the shared renderer round trip and the evicted rendering.

cargo fmt --check, cargo clippy --workspace --all-targets --all-features -D warnings, cargo test --workspace green.

Not in scope

Steering messages with @path parts still send placeholders on the live run (pre-existing gap noted in progress/tool-layer.md). search_history still searches the placeholder text, not attachment bytes.

…es what the model saw (F05)

A prompt's `@path` attachments were read into the live request at run start
while the durable message row kept the placeholder, so every later request
in the session — follow-up, reopen, compaction, and the retry after an
automatic compaction — replayed `inspect\[email protected]` where the model's first
turn had seen the file bytes. Modifying or deleting the file made the gap
permanent.

Run start now persists each attachment (path, whole-file SHA-256, line
window, content) in the same transaction that marks the run started: store
schema 28 -> 29 adds `attachment_blobs`, keyed per session by hash+window so
a file attached to several prompts is stored once, and `message_attachments`
in prompt order. `load_model_context` joins them and re-renders the
`<attached-file>` blocks through the same renderer the live run uses, so the
assembled context is byte-identical to the first request. Past a 64 MiB
per-session cap the oldest blobs of finished runs lose their bytes and render
an explicit `evicted="true"` block, never the bare placeholder. Session
delete cascades. `ClaimedRun.resolved_input` carries the first read across
the auto-compaction retry instead of re-reading a file that may have changed.

`MessageSnapshot.output` still carries the placeholder for display; no
protocol change. Assembly cost is one additional indexed query per session.

Tests: attach/modify/delete/continue round trip with reopen, dedup counts,
reference-assembly oracle and delete cascade; eviction stub rendering;
auto-compaction retry keeps the original bytes; schema-29 migration.

Closes ENG-788.
@lg2m
lg2m merged commit e0f5655 into main Sep 17, 2026
4 checks passed
@lg2m
lg2m deleted the fix/eng-788-f05-attachment-artifacts branch September 17, 2026 23:07
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