[PR #5852] [MERGED] fix(evidence): clear staged write cleanup state after a confirmed retry #7085

Closed
opened 2026-11-01 21:45:17 +00:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/morluto/rea/pull/5852
Author: @kaoru0822-kitauji
Created: 10/21/2026
Status: ✅ Merged
Merged: 10/21/2026
Merged by: @kaoru0822-kitauji

Base: main ← Head: fix/evidence-export-cleanup-retry


📝 Commits (2)

  • 6d4e019 fix(evidence): clear staged write cleanup state after a confirmed retry
  • 7582bed Merge remote-tracking branch 'origin/main' into fix/evidence-export-cleanup-retry

📊 Changes

4 files changed (+272 additions, -19 deletions)

View changed files

📝 src/application/EvidenceBundleCommands.ts (+4 -8)
📝 src/application/JsonFiles.ts (+37 -11)
➕ tests/boundary/filesystem/evidenceExportCleanupRetry.test.ts (+124 -0)
📝 tests/boundary/filesystem/jsonOutputOwnership.test.ts (+107 -0)

📄 Description

What broke: after a failed export, a staging cleanup that failed once and then succeeded on retry still reported cleanup_incomplete, the old cleanup reason, and a retained .rea-write-* stage.
Fix: the atomic JSON writer now binds each staged-output cleanup error, and each admission-retry error, to the exact AnalysisCleanupLease it retains, so a confirmed release brings back the primary outcome. Export recovery reads that bound state.
Risk: low. The change touches only the error/lease binding in JsonFiles.ts and the export recovery check. A retry that still fails keeps the error incomplete with the same stage.

Summary

Fixes #5842.

rea evidence-export SOURCE OUTPUT can fail to publish (for example EACCES on the publication link) and then fail its first staging-directory removal. The workflow then closes its resource scope, and the scope's retry removes the stage. The returned error still projected cleanup_incomplete with one retained resource. The lifecycle contract says that once cleanup is confirmed, the original typed outcome comes back and only unresolved resources are reported. In this case that outcome is execution_failure.

Problem and expected behavior

  • Trigger: an unpublished write failure, then a staging release that fails, then a scope retry that releases the stage.
  • Violated invariant: own the lifecycle. A typed error whose owner has confirmed release must stop claiming current cleanup state.
  • Root cause: writeTextPartsOwned built EvidenceFileError / AnalysisCancelledError with a static cleanup observation. It never registered the error with the lease that ArtifactResourceScope.release() retained, so a later successful retry had nothing to resolve. The writeTextParts admission path had the same gap: ArtifactResourceRetryFailure carries exact cleanupLeases, but the translated error was never bound to them.
  • New behavior: after confirmed release, the export returns the primary execution_failure (or cancelled for cancellation) without cleanup/resources details. If the retry also fails, the export still reports cleanup_incomplete with the retained stage. The published-output recovery behaves as before.

Change and scope

  • src/application/JsonFiles.ts (owning boundary): StagedOutputCleanupFailure carries the failed attempt's lease. Every staged-cleanup error (published, cancelled, unpublished) is bound with AnalysisCleanupLeaseGroup, so the error's resource attribution follows the live lease labels. Admission-retry errors use the existing bindArtifactFileCleanup helper.
  • src/application/EvidenceBundleCommands.ts: the local cleanupSucceeded flag is gone. Published recovery now checks the error's own cleanupIncomplete, which belongs to the exact staging owner.
  • Every writer sharing this scope benefits because the fix sits at the shared boundary. One example is --evidence-output delivery through finishAnalysisCommand, which already relies on resource owners resolving their errors during close. That consumer's benefit is source-derived and was not separately fault-injected.
  • Not included: BinarySessionRecords.closeWriteResources also translates a retry failure into an unbound EvidenceFileError. That error comes from session shutdown and is not a writer result, so it is left for a separate change.
  • Contribution scope: ordinary fix within existing boundaries; maintainer scope decision not applicable.

Contract and boundary impact

  • Semantic owner and earliest changed stage: the atomic JSON writer's staged-output cleanup translation (src/application/JsonFiles.ts).
  • Contribution scope fit and linked maintainer decision: existing boundary; not applicable.
  • CLI and MCP/tool-catalog contract: no schema change. Error codes now follow the documented cleanup contract after a confirmed retry.
  • Provider, bridge, target-format, or platform compatibility: none.
  • Evidence, artifact, provenance, or reconstruction contract: none. Output publication and identity checks are unchanged.
  • Target selection, authority, upstream behavior, and cleanup details: cleanup state now clears only when the exact retained staging lease confirms release. Failed retries keep it.
  • Generated metadata, package, or installation impact: none.

Expected effects:

  • Read existing files or captured data.
  • Launch or attach to a process.
  • Inject instrumentation or run scripts/code.
  • Change process memory, registers, files, or configuration.
  • Contact network services.
  • Recover protected code or work around protections.
  • Documentation or usability only; no target interaction.
  • Not sure yet; investigation or maintainer guidance needed.

Evidence and regression coverage

  • Tests added or updated:
    • tests/boundary/filesystem/evidenceExportCleanupRetry.test.ts (new) runs the export workflow with injected link EACCES and rmdir EBUSY. It covers three cases: a single cleanup failure after failed publication, which now returns execution_failure with no cleanup details; a retry that also fails, which keeps cleanup_incomplete with the retained stage; and a single cleanup failure after successful publication, which still returns the receipt.
    • tests/boundary/filesystem/jsonOutputOwnership.test.ts exercises the writer boundary without mocks, using an unexpected staging entry as the real fault. The unpublished write failure and the cancellation case each resolve to their primary outcome after release. The published case keeps its published receipt once cleanup state clears. An admission retry failure stays incomplete through a failed retry and clears after release. An identity-unknown stage stays incomplete when its retry fails.
  • Base reproduction: the issue's repro test ran on origin/main (f801b2fbf). The middle case printed "code":"cleanup_incomplete","cleanup":"incomplete","retainedResourceCount":1 with stagesRemaining: 0. With this change the same repro printed "code":"execution_failure","cleanup":null,"retainedResourceCount":0, and the two control cases were unchanged.
  • Regressions on base: with src/ reverted to main, 5 of the new or extended assertions fail (export retry restoration; published, cancelled, unpublished and admission-retry resolution). The failed-retry and published-receipt controls pass both before and after the change.
  • Remaining proof gaps: fault injection only. No spontaneous host filesystem faults and no spawned-CLI fault run. macOS only. MCP export_evidence_bundle and --evidence-output benefits are source-derived.

For evidence-bearing changes:

  • Observed, derived, and inferred claims remain distinguishable.
  • Artifact identity, source provenance, and failed attempts remain preserved.
  • Unsupported, incomplete, unavailable, or uncertain outcomes remain visible.

Validation performed

  • Issue repro (npm run test:local -- src/application/repro.test.ts, temporary file, isolated HOME/XDG/TMPDIR) on main: 3 passed. This confirmed the defect, because that test asserts the stale state. With the fix: the middle case returns execution_failure (its bug assertion fails as expected) and the controls pass.
  • npm run test:focused -- tests/boundary/filesystem/evidenceExportCleanupRetry.test.ts tests/boundary/filesystem/jsonOutputOwnership.test.ts tests/boundary/filesystem/evidenceBundleFiles.test.ts: 3 files, 45 tests passed.
  • Same new/extended files with src/ reverted to main: 5 failed, 12 passed (expected).
  • npm run test:focused -- tests/boundary/filesystem/analysisSnapshot.test.ts tests/boundary/filesystem/directAnalysisPermissions.test.ts tests/boundary/filesystem/directAnalysisSnapshotReplay.test.ts tests/boundary/mcp/evidenceExportCancellation.test.ts: 37 passed, 2 skipped.
  • npm run test:focused -- tests/boundary/cli/evidenceBundleCommands.test.ts tests/boundary/cli/commandEvidenceDelivery.test.ts: 12 passed.
  • npm run check:fast: passed.
  • npm run format:check: passed.

Performance: one extra lease-group registration, made only on the cleanup-failure path. No measurable effect on successful writes; not separately measured.

Compatibility, safety, and release

  • Breaking changes or migration steps: none.
  • Real Hopper/Ghidra, browser, or OS coverage: not applicable. Ran on macOS arm64 only.
  • Package or release metadata impact: none.
  • Security, privacy, process, or containment review: no new filesystem operations. Staging removal still goes through the existing identity-checked OwnedStagedOutput.close().

Author readiness

Human owner: @kaoru0822-kitauji

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/morluto/rea/pull/5852 **Author:** [@kaoru0822-kitauji](https://github.com/kaoru0822-kitauji) **Created:** 10/21/2026 **Status:** ✅ Merged **Merged:** 10/21/2026 **Merged by:** [@kaoru0822-kitauji](https://github.com/kaoru0822-kitauji) **Base:** `main` ← **Head:** `fix/evidence-export-cleanup-retry` --- ### 📝 Commits (2) - [`6d4e019`](https://github.com/morluto/rea/commit/6d4e019f408903dc63ed565a2bc66b38ae1857ee) fix(evidence): clear staged write cleanup state after a confirmed retry - [`7582bed`](https://github.com/morluto/rea/commit/7582bed86217290e6df70c4bce5e79bb38461f2d) Merge remote-tracking branch 'origin/main' into fix/evidence-export-cleanup-retry ### 📊 Changes **4 files changed** (+272 additions, -19 deletions) <details> <summary>View changed files</summary> 📝 `src/application/EvidenceBundleCommands.ts` (+4 -8) 📝 `src/application/JsonFiles.ts` (+37 -11) ➕ `tests/boundary/filesystem/evidenceExportCleanupRetry.test.ts` (+124 -0) 📝 `tests/boundary/filesystem/jsonOutputOwnership.test.ts` (+107 -0) </details> ### 📄 Description **What broke:** after a failed export, a staging cleanup that failed once and then succeeded on retry still reported `cleanup_incomplete`, the old cleanup reason, and a retained `.rea-write-*` stage. **Fix:** the atomic JSON writer now binds each staged-output cleanup error, and each admission-retry error, to the exact `AnalysisCleanupLease` it retains, so a confirmed release brings back the primary outcome. Export recovery reads that bound state. **Risk:** low. The change touches only the error/lease binding in `JsonFiles.ts` and the export recovery check. A retry that still fails keeps the error incomplete with the same stage. ## Summary Fixes #5842. `rea evidence-export SOURCE OUTPUT` can fail to publish (for example `EACCES` on the publication link) and then fail its first staging-directory removal. The workflow then closes its resource scope, and the scope's retry removes the stage. The returned error still projected `cleanup_incomplete` with one retained resource. The lifecycle contract says that once cleanup is confirmed, the original typed outcome comes back and only unresolved resources are reported. In this case that outcome is `execution_failure`. ## Problem and expected behavior - Trigger: an unpublished write failure, then a staging release that fails, then a scope retry that releases the stage. - Violated invariant: [own the lifecycle](https://github.com/morluto/rea/blob/main/docs/tool-design.md#own-the-lifecycle). A typed error whose owner has confirmed release must stop claiming current cleanup state. - Root cause: `writeTextPartsOwned` built `EvidenceFileError` / `AnalysisCancelledError` with a static `cleanup` observation. It never registered the error with the lease that `ArtifactResourceScope.release()` retained, so a later successful retry had nothing to resolve. The `writeTextParts` admission path had the same gap: `ArtifactResourceRetryFailure` carries exact `cleanupLeases`, but the translated error was never bound to them. - New behavior: after confirmed release, the export returns the primary `execution_failure` (or `cancelled` for cancellation) without `cleanup`/`resources` details. If the retry also fails, the export still reports `cleanup_incomplete` with the retained stage. The published-output recovery behaves as before. ## Change and scope - `src/application/JsonFiles.ts` (owning boundary): `StagedOutputCleanupFailure` carries the failed attempt's lease. Every staged-cleanup error (published, cancelled, unpublished) is bound with `AnalysisCleanupLeaseGroup`, so the error's resource attribution follows the live lease labels. Admission-retry errors use the existing `bindArtifactFileCleanup` helper. - `src/application/EvidenceBundleCommands.ts`: the local `cleanupSucceeded` flag is gone. Published recovery now checks the error's own `cleanupIncomplete`, which belongs to the exact staging owner. - Every writer sharing this scope benefits because the fix sits at the shared boundary. One example is `--evidence-output` delivery through `finishAnalysisCommand`, which already relies on resource owners resolving their errors during close. That consumer's benefit is source-derived and was not separately fault-injected. - Not included: `BinarySessionRecords.closeWriteResources` also translates a retry failure into an unbound `EvidenceFileError`. That error comes from session shutdown and is not a writer result, so it is left for a separate change. - Contribution scope: ordinary fix within existing boundaries; maintainer scope decision not applicable. ## Contract and boundary impact - Semantic owner and earliest changed stage: the atomic JSON writer's staged-output cleanup translation (`src/application/JsonFiles.ts`). - Contribution scope fit and linked maintainer decision: existing boundary; not applicable. - CLI and MCP/tool-catalog contract: no schema change. Error codes now follow the documented cleanup contract after a confirmed retry. - Provider, bridge, target-format, or platform compatibility: none. - Evidence, artifact, provenance, or reconstruction contract: none. Output publication and identity checks are unchanged. - Target selection, authority, upstream behavior, and cleanup details: cleanup state now clears only when the exact retained staging lease confirms release. Failed retries keep it. - Generated metadata, package, or installation impact: none. Expected effects: - [x] Read existing files or captured data. - [ ] Launch or attach to a process. - [ ] Inject instrumentation or run scripts/code. - [ ] Change process memory, registers, files, or configuration. - [ ] Contact network services. - [ ] Recover protected code or work around protections. - [ ] Documentation or usability only; no target interaction. - [ ] Not sure yet; investigation or maintainer guidance needed. ## Evidence and regression coverage - Tests added or updated: - `tests/boundary/filesystem/evidenceExportCleanupRetry.test.ts` (new) runs the export workflow with injected `link` `EACCES` and `rmdir` `EBUSY`. It covers three cases: a single cleanup failure after failed publication, which now returns `execution_failure` with no cleanup details; a retry that also fails, which keeps `cleanup_incomplete` with the retained stage; and a single cleanup failure after successful publication, which still returns the receipt. - `tests/boundary/filesystem/jsonOutputOwnership.test.ts` exercises the writer boundary without mocks, using an unexpected staging entry as the real fault. The unpublished write failure and the cancellation case each resolve to their primary outcome after release. The published case keeps its `published` receipt once cleanup state clears. An admission retry failure stays incomplete through a failed retry and clears after release. An identity-unknown stage stays incomplete when its retry fails. - Base reproduction: the issue's repro test ran on `origin/main` (f801b2fbf). The middle case printed `"code":"cleanup_incomplete","cleanup":"incomplete","retainedResourceCount":1` with `stagesRemaining: 0`. With this change the same repro printed `"code":"execution_failure","cleanup":null,"retainedResourceCount":0`, and the two control cases were unchanged. - Regressions on base: with `src/` reverted to main, 5 of the new or extended assertions fail (export retry restoration; published, cancelled, unpublished and admission-retry resolution). The failed-retry and published-receipt controls pass both before and after the change. - Remaining proof gaps: fault injection only. No spontaneous host filesystem faults and no spawned-CLI fault run. macOS only. MCP `export_evidence_bundle` and `--evidence-output` benefits are source-derived. For evidence-bearing changes: - [x] Observed, derived, and inferred claims remain distinguishable. - [x] Artifact identity, source provenance, and failed attempts remain preserved. - [x] Unsupported, incomplete, unavailable, or uncertain outcomes remain visible. ## Validation performed - Issue repro (`npm run test:local -- src/application/repro.test.ts`, temporary file, isolated HOME/XDG/TMPDIR) on main: 3 passed. This confirmed the defect, because that test asserts the stale state. With the fix: the middle case returns `execution_failure` (its bug assertion fails as expected) and the controls pass. - `npm run test:focused -- tests/boundary/filesystem/evidenceExportCleanupRetry.test.ts tests/boundary/filesystem/jsonOutputOwnership.test.ts tests/boundary/filesystem/evidenceBundleFiles.test.ts`: 3 files, 45 tests passed. - Same new/extended files with `src/` reverted to main: 5 failed, 12 passed (expected). - `npm run test:focused -- tests/boundary/filesystem/analysisSnapshot.test.ts tests/boundary/filesystem/directAnalysisPermissions.test.ts tests/boundary/filesystem/directAnalysisSnapshotReplay.test.ts tests/boundary/mcp/evidenceExportCancellation.test.ts`: 37 passed, 2 skipped. - `npm run test:focused -- tests/boundary/cli/evidenceBundleCommands.test.ts tests/boundary/cli/commandEvidenceDelivery.test.ts`: 12 passed. - `npm run check:fast`: passed. - `npm run format:check`: passed. Performance: one extra lease-group registration, made only on the cleanup-failure path. No measurable effect on successful writes; not separately measured. ## Compatibility, safety, and release - Breaking changes or migration steps: none. - Real Hopper/Ghidra, browser, or OS coverage: not applicable. Ran on macOS arm64 only. - Package or release metadata impact: none. - Security, privacy, process, or containment review: no new filesystem operations. Staging removal still goes through the existing identity-checked `OwnedStagedOutput.close()`. ## Author readiness Human owner: @kaoru0822-kitauji - [x] I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work. - [x] I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps. - [x] I declare this PR ready for initial maintainer review. ## Before merge - [x] This PR addresses a concrete problem or an agreed enhancement. - [x] Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff. - [x] The PR has one focused outcome and the title follows `type(scope): outcome`. - [x] Related issue is linked, or the reason for not linking one is stated above. - [x] Tests cover changed observable behavior and meaningful failure paths. - [x] Owning docs, contracts, and generated metadata are updated where needed. - [x] User-visible CLI/MCP changes include representative output. - [x] I checked the final diff for secrets, unrelated cleanup, and unsupported claims. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-01 21:45:17 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
morluto/rea#7085
No description provided.