17 KiB
Workstream 9 Implementation Plan — Cleanup As The Final Pipeline Stage
Status
Planned implementation for Workstream 9, Cleanup as the final pipeline
stage, in refactoring.md. Workstreams 1 through 8 are
complete prerequisites. This plan does not independently undertake Workstream
10; its test and documentation requirements apply only where this work changes
the owning behavior.
Authoritative Scope And Fixed Decisions
Workstream 9 in refactoring.md is authoritative. Its
supporting records are H20 and H21 in audit.md, Findings 2 and 4
in contract-audit.md, and C02 and C04 in
audit-decisions.md. Read those cited sections rather
than rereading or reinterpreting the complete audits.
The following decisions are final:
- Add
cleanupafternotifyas the twelfth and final canonical stage.publishcommits remote state,notifycompletes delivery behavior, and only then maycleanupremove local run data. - Retain the existing configuration names
pipeline.spool.delete_audio_after_publishandpipeline.workspace.cleanup_after_publish. They select cleanup-stage targets; they no longer cause a runner-finalization side effect. - When neither policy is enabled,
cleanupreturns an explicit self-skip with no outputs. Self-skips remain reconsidered on later invocations under the ordinary runner contract. - When either policy is enabled, cleanup requires a succeeded
publishstage whose metadata proves a completed pointer-selected upload:uploaded=true,skippedis not true, and canonical nonemptypublished_run_id,remote_commit_key, andcurrent_commit_pointer_keyvalues agree with the manifest's campaign/session remote prefix. Missing, malformed, skipped, or disabled publication evidence is an error and no deletion begins. - The succeeded publish record is sufficient durable evidence. Cleanup does not reread object storage: publish success is recorded only after the pointer write, immutable run objects remain durable if a later pointer supersedes them, and adding a network dependency would make local retry less reliable.
- Compute targets from the validated published run ID, manifest campaign and
session identities, and the currently resolved application-owned workspace
and spool roots using canonical artifact path helpers. Never use the new
invocation's
RunID,LocalWorkDir, orLocalSpoolDir, and never accept a path from publish metadata as a deletion target. - Process requested targets deterministically in spool-then-workspace order.
Use
fileops.RemoveAllUnderRoot; a missing root or target is successful. Preserve refusal of a root itself, traversal, and out-of-root deletion. - Do not record per-target progress. If one deletion succeeds and a later one fails, the ordinary stage record becomes failed. A later run recomputes the same canonical targets, treats the first as already absent, and retries the remainder.
- A successful cleanup produces no canonical artifacts. Its bounded metadata may identify the published run and completed policy names, but must not become a second cleanup ledger.
- Cleanup has a semantic fingerprint over the two policy booleans and their normalized configured roots. Its resume validator revalidates publish evidence and confirms every enabled canonical target is absent. Recreated or newly enabled targets make a prior success non-resumable.
- Reject statically impossible configuration: either cleanup policy requires publish to be enabled with run upload enabled. Dynamic absence or corruption of publish evidence remains a cleanup-stage error.
notify -> cleanupis the final direct invalidation edge andcleanuphas no dependents. This makes every earlier prerequisite transitively stale a prior cleanup result while retaining the fixed relation separately from execution order.- A default full run includes cleanup.
--through notify, any earlier bounded range,narratio publish, and artifact-regeneration aliases do not perform cleanup.narratio run-stage cleanup SESSIONis the explicit single-stage retry path and applies cleanup's own publication-evidence checks. - Remove
post_publish_cleanupcompletely. Do not migrate, reinterpret, or retain its types, validators, runner hooks, restore clearing, fixtures, or tests. Keep session manifest schema versionnarratio.session-manifest.v1: the retired field was optional, and strict decoding will reject manifests that actually contain it without invalidating otherwise canonical v1 state.
Instructions For Every Stage
Before each stage:
- Read
docs/development.md, all documents underdocs/policy/, this plan, Workstream 9, and only its cited audit sections. Read the affected portions of../internal/overview.md,../internal/workspace.md,../internal/manifest.md,../internal/stage-publish.md,../config.md,../cli.md, and../operations.md. - Inspect affected production code and focused tests. Prefer the codebase knowledge graph for Go discovery and call tracing; use text search for configuration, schemas, fixtures, documentation, and removal checks.
- Confirm worktree state and preserve unrelated changes. Do not implement Workstream 10 work that is not made necessary by this cleanup change.
During each stage, use ordinary stage and manifest transitions, keep paths
root-confined, preserve group-owned workspace behavior, avoid raw secrets,
honor context before destructive work, and keep tests deterministic and
offline. Prefer real temporary filesystems over private deletion hooks. Add
tests for meaningful destructive, recovery, range, and persistence boundaries;
delete tests whose only subject is the retired obligation choreography. Run
gofmt on changed Go files and git diff --check. Commit only a completed
stage with a short plain-English message, and update each canonical document in
the same stage as the behavior it owns.
Stage 1 — Define Cleanup Evidence, Targets, And Configuration Rules
Status: Completed
Goal
Create one deterministic, side-effect-free cleanup plan and reject configurations that can never authorize it.
Required Work
- Add package-private cleanup planning types in
internal/stage. A plan contains the validated published run ID and zero, one, or two root/target/ policy entries in deterministic spool-then-workspace order. Do not register the stage or delete files yet. - If both cleanup flags are false, return a disabled plan without requiring
publish evidence. Otherwise require and validate the succeeded
publishrecord and its metadata exactly as fixed above. Use artifact identity/key helpers rather than string-prefix approximations; add a narrow helper only if the existing remote-key API cannot validate the commit identity. - Construct the spool target with
artifacts.SessionSpoolAudioDirand the workspace target withartifacts.SessionRunRootForCampaign, always using the publish metadata's run ID. Validate manifest/config identities before constructing paths. Do not consumeManifest.LocalWorkDirorManifest.LocalSpoolDir. - Add cross-field configuration validation so either cleanup flag requires
effective
publish.enabled=trueandpublish.upload_run=true. Preserve existing defaults and field names. Add focused tests for each impossible combination and both valid enabled policies without duplicating exact error prose outside the config owner. - Table-test planning for disabled cleanup, each target selection, stable ordering, prior-run publication, missing/failed/skipped publish state, false or mistyped metadata, invalid run identity, wrong commit key, wrong pointer key, and canonical paths. Prove every invalid evidence case produces no plan that can be executed.
- Update the cleanup fields in
docs/config.mdin this stage to describe the new validation and their role as final-stage target selectors. Do not yet describe the unregistered stage as implemented elsewhere.
Verification And Exit Criteria
Run go test ./internal/config ./internal/stage -run 'Cleanup|Publish' and
git diff --check. Cleanup authorization and targets are deterministic and
cannot be derived from the current invocation or uncommitted publication.
Stage 2 — Implement Idempotent Cleanup Stage Behavior
Status: Completed
Goal
Implement cleanup as an ordinary, independently testable stage without yet changing the canonical pipeline.
Required Work
- Add
cleanupStagewith namecleanup. ItsRunobtains the Stage 1 plan, self-skips when disabled, checks context before each target, and removes each enabled target with the root-scoped filesystem helper. - Make
fileops.RemoveAllUnderRootexplicitly idempotent when either the configured root or target is already absent. Do not weaken rejection of root deletion, traversal, or out-of-root targets and do not introduce a general filesystem facade. - Return a successful
StageResultwith no outputs, logs, or generated configuration. Keep metadata bounded to the published run and completed policy identifiers; do not persist target paths, completion flags, or an obligation-like structure. - Implement cleanup semantic fingerprinting with a new private versioned
payload containing normalized roots and policy booleans. Implement
ResumeValidator: disabled cleanup is reconsidered through self-skip rather than reusable success; enabled cleanup first validates the current publish evidence and then reports resumable only when all planned targets remain absent. - Test through the stage contract with
t.TempDir: disabled self-skip; spool-only, workspace-only, and both; preservation of another run and the durable cache; already-absent root/target; cancellation before deletion; refusal of unsafe scope; first-target success followed by second-target failure and successful idempotent retry; no canonical outputs; fingerprint changes; and resume after success versus target recreation. Use real filesystem state rather than adding mutable package globals or a deletion interface solely for tests.
Verification And Exit Criteria
Run go test ./internal/fileops ./internal/stage -run Cleanup,
go test -race ./internal/stage -run Cleanup, and git diff --check. The
unregistered stage provides safe, idempotent behavior through existing
filesystem and manifest contracts.
Stage 3 — Integrate Cleanup Into Canonical Orchestration
Status: Pending
Goal
Make cleanup the normal final stage and remove cleanup from runner finalization.
Required Work
- Register
cleanupStage{}afternotifyinstage.All; make it selectable by the existing planner andrun-stagecommand without a special command or alias. - Extend the validated invalidation relation with
notify -> cleanupand an empty cleanup classification. Update transitive expectations so every earlier stage reaches cleanup while preserving the render/extract sibling relationship. - Reduce
finalizeInvocationto summary construction. Delete its cleanup call and cleanup-specific terminal-failure path. RemoveremoveScopedDirfrominvocationDependenciesand all normalization/test plumbing made dead by that removal. - Delete
internal/app/post_publish_cleanup.go. Retain application cleanup helpers only where the explicitcleancommand still uses them; remove or rename types and wrappers whose only owner was post-publish cleanup. - Replace
post_publish_cleanup_test.gowith proportionate runner-level integration coverage. Prove full order ends in cleanup, notify failure prevents cleanup, publish failure prevents cleanup, cleanup failure leaves publish succeeded and remote state untouched, a later ordinary full run skips reusable upstream work and retries cleanup, and a forced prerequisite stales cleanup. - Cover selection semantics: the default full plan includes cleanup;
--through notifyand earlier ranges do not delete;publishexecutes only publish; directrun-stage cleanupworks with valid evidence and fails safely without it; artifact selection remains accepted only by analyze and publish. Update plan, command, assembled-workflow, bounded-prerequisite, runner, and stage-count expectations without broad snapshots. - Update current-behavior owners in the same stage: architecture's publish and
cleanup boundary, the internal overview/stage list and lifecycle,
docs/cli.md,docs/operations.md,docs/internal/workspace.md, anddocs/internal/stage-publish.md. Add a focuseddocs/internal/stage-cleanup.mdand link it from the overview. Clearly state that the standalone publish command does not invoke cleanup and normal resume retries a failed cleanup stage without republishing valid state.
Verification And Exit Criteria
Run go test ./internal/stage ./internal/app,
go test -race ./internal/app -run 'Cleanup|Runner|RunStage|Bounded|Plan',
go test ./internal/doccheck, and git diff --check. Execution order, range
behavior, failure attribution, and retry are ordinary stage behavior, with no
post-loop deletion path.
Stage 4 — Remove The Cleanup Obligation From Manifest State
Status: Pending
Goal
Delete the second cleanup state machine and leave stage status as the only durable cleanup progress model.
Required Work
- Remove
CleanupTarget,PostPublishCleanup, andManifest.PostPublishCleanup; remove their validation, cloning, test helpers, JSON fixtures, and persistence assertions. - Remove restore's special clearing of
post_publish_cleanup. Restore should treat cleanup exactly like other stage state and must not synthesize or reset a separate obligation. - Keep
SessionManifestSchemaVersionat v1. Confirm strict decoding rejects a literalpost_publish_cleanupfield as unknown, while canonical v1 manifests without it still round-trip unchanged. Add no compatibility reader, migration code, or silent field discard. - Update
docs/internal/manifest.md, restore/workspace internals, operations, and troubleshooting where they own the removed state or recovery advice. Migration guidance should tell operators with an outstanding old obligation to finish cleanup with the pre-upgrade binary or verify remote publication, remove/regenerate the incompatible local manifest, and then use the cleanup stage. Do not present manual JSON editing as the normal migration path. - Search production, tests, fixtures, examples, and non-audit documentation
for
PostPublishCleanup,CleanupTarget,post_publish_cleanup,runPostPublishCleanup, andpost-publish cleanup obligation. Remove every live reference; historical roadmap/audit records may retain the terms.
Verification And Exit Criteria
Run go test ./internal/manifest ./internal/app ./internal/stage,
go test ./internal/doccheck, and git diff --check. The session manifest
contains only the ordinary cleanup stage record, and old obligation-bearing
documents fail strict decoding rather than entering a compatibility path.
Stage 5 — Consolidate, Validate, And Close Workstream 9
Status: Pending
Goal
Ensure the reduced implementation, tests, and canonical documentation agree, then record the workstream complete.
Required Work
- Review the complete diff for duplicated evidence/target logic, obsolete runner branches, redundant wrappers, stale fixtures, accidental remote cleanup reads, and any new per-target persistence. Consolidate only into the narrow owners established above.
- Confirm the effective behavioral matrix: disabled self-skip; valid publish evidence before deletion; final-stage ordering; bounded exclusion; direct retry; idempotent absence; partial-deletion recovery; publish success surviving cleanup failure; semantic invalidation; and destructive confinement.
- Check all current-behavior documentation, CLI help, examples, configuration diffs, stage lists, and migration guidance. Volatile details must have one canonical owner and roadmap documents must not be cited as current runtime documentation.
- Run every repository-wide validation command from
docs/development.md, plusgit diff --check. Run the scheduled shuffle/repetition command on the affectedinternal/app,internal/stage,internal/manifest, andinternal/fileopspackages. Record platform-only limitations without weakening or silently skipping applicable checks. - Only after all checks pass, mark every stage in this plan and Workstream 9
in
refactoring.mdComplete. Leave Workstream 10 as the next pending scope and do not prepare or publish a release.
Verification And Exit Criteria
At minimum run:
go test ./...
go test -race ./...
go test -race -shuffle=on -count=3 ./internal/app ./internal/stage ./internal/manifest ./internal/fileops
go vet ./...
go build ./...
go test ./internal/doccheck
go test ./internal/config -run '^TestExamplesLoadAndValidate$'
git diff --check
The worktree contains one final canonical cleanup stage, no special cleanup obligation or runner side effect, aligned documentation, and no Workstream 10 scope beyond changes required by this behavior.
Open Questions
None. The approved audits, refactoring roadmap, and fixed decisions above make this implementation plan decision-complete.