Files
PaperClipAI/server
DottaandClaude Opus 5.5 506211b917 fix(run-logs): archive a finalized log that a later attempt extended (#22709)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Hosted stacks move from a volume to volume-free storage, and the
durable-data migration must move every run log first
> - Some finalized run logs grew after their size and SHA-256 were
recorded, because a later attempt of the same run appended to the same
local file
> - Both the online and offline migration refused these logs as
`legacy_length_or_digest_mismatch`, so the stack could never leave its
volume
> - This pull request archives such a log when the recorded proof
matches a prefix of the file, and names the real corruption case
distinctly
> - The benefit is that these stacks can move without deleting or hiding
any bytes, and real corruption still blocks the move

## Linked Issues or Issue Description

**What happened?**
Durable-data migration blocks a run log with
`legacy_length_or_digest_mismatch` when the local file is longer than
the size and SHA-256 recorded at finalize. The run log store never
truncates, so a later attempt that shares the run id appends to the
finalized file. The stack then cannot move to volume-free storage.

**Steps to reproduce**
1. Finalize a run log so that the run records its size and SHA-256.
2. Append more output to the same local file.
3. Run the online or offline durable-data migration.
4. See the log blocked as `legacy_length_or_digest_mismatch`.

**Expected behavior**
When the recorded size and SHA-256 match a prefix of the file, the
migration keeps every current byte, records both proofs, and lets the
stack move. When the recorded proof matches no prefix, the log stays
blocked under a distinct name.

## What Changed

- `server/src/services/run-evidence-migration.ts`: before it claims the
object journal, the migration checks if the recorded size and SHA-256
match a prefix of a longer file. If yes, it archives every current byte
as a read-only `s3_archive` log in the `<run>.archive` journal.
- The publish sets the run's size and SHA-256 to the archived bytes. In
the same transaction it writes a `run.historical_log_archived` activity
entry with the original prefix proof (`recorded`) and the new size and
SHA-256.
- A log whose recorded size and SHA-256 match no prefix (a different
longer file, a shorter file, or a same-size file with different bytes)
stays blocked as `legacy_recorded_log_not_a_prefix`. Nothing is uploaded
or published for a longer file that fails the prefix check.
- The extended log must come from the local file. A longer mirror copy
stays blocked as `mirror_completeness_unproven`, because a matching
prefix does not prove that the mirror holds the whole file.
- While the run still has a pending or leased attempt (a native
finalization with `next_attempt_at`, a lease owner, or a phase other
than `committed` or `terminal_failure`), the log stays blocked as
`legacy_writer_retry_pending`. The publish `WHERE` rechecks this
condition, so a later attempt of the same run never finds a read-only
archive.
- Both archive publications (settled native history and extended logs)
share one helper that publishes and writes the activity entry in one
transaction.
- `legacy_length_or_digest_mismatch` stays for the other cases: a source
that changed while it was read, and a run that recorded only one of size
or SHA-256.
- The online path (`migrateRunEvidencePage`) and the offline path
(`preserveHistoricalFiles`) use the same code, so the rule is the same
for both.
- `doc/run-evidence-storage.md` describes the new rule.

## Verification

- New test `server/src/services/run-evidence-migration-appended.test.ts`
uses embedded PostgreSQL, real local files and an in-memory object
store:
- online and offline: a log with appended output is archived, the run
row has the new size and SHA-256, the archive reads back every byte
after the local file is deleted, and one activity entry holds both
proofs.
- online and offline: a longer file, a shorter file and a same-size file
whose bytes differ stay blocked as `legacy_recorded_log_not_a_prefix`,
keep their local reference, and write no activity.
- online and offline: an extended log whose run has a pending attempt
stays blocked as `legacy_writer_retry_pending`, unchanged and with no
activity.
- a longer mirror copy (local file missing) stays blocked as
`mirror_completeness_unproven`.
- Red: the first 3 tests fail on master
(`legacy_length_or_digest_mismatch`). The pending-attempt and mirror
tests fail on the first commit. Green: all 5 pass with this change.
- `npx vitest run src/services/run-evidence-migration-appended.test.ts
src/services/run-evidence-migration-page.test.ts
src/services/online-durable-data-migration.test.ts`: 40 passed.
- `npx tsc --noEmit -p server`: clean.
- I did not run the opt-in S3 integration suite
(`run-evidence-migration.integration.test.ts`), because it needs an
external S3 endpoint. Its `wrong-digest` case records only a SHA-256, so
it keeps `legacy_length_or_digest_mismatch`.

## Risks

- An archived log is read-only. This is the same as the existing
historical archives. The publish requires a settled run with no writer
lease, so no attempt can still be writing.
- The prefix check reads the recorded prefix (not the whole file) one
more time before the copy. This happens only when the file is longer
than the recorded size. It lets a non-prefix file fail before anything
is uploaded.
- Low risk otherwise: logs whose bytes match their recorded proof follow
the same path as before.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, in Claude Code with
tool use and code execution.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

Related: #22697 (archived workspaces whose files are gone) and #22698
(skills whose remote source is gone) settle the other two cases that
block the same move.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 18:32:00 -06:00
..