Skip to content

Fix abandoned session cleanup terminal-state gap - #336

Merged
brettheap merged 6 commits into
mainfrom
change/fix-abandoned-session-cleanup-terminal-states
Aug 25, 2026
Merged

brettheap merged 6 commits into
mainfrom
change/fix-abandoned-session-cleanup-terminal-states

Conversation

@openxfactory

@openxfactory openxfactory Bot commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • close the abandoned-session terminal-state gap with exact active, archived, demoted, legacy, or explicit human retention-release evidence
  • bind abandonment and cleanup to the exact branch head, durable evidence chronology, and repository identity
  • execute cleanup as a repository-locked, ref-keyed prepared/completed transaction with atomic expected-SHA deletion and safe restoration
  • make demotion receipts complete, collision-safe, contained, and independently contract-validated
  • align CLI and HTTP refusal behavior, update operator guidance, and complete all 30 OpenSpec tasks
  • realize the additive contract-v1.43 candidate bundle; tagging remains a post-merge action

Validation

  • 274 focused cleanup/session regressions passed
  • 6,181 repository tests passed, 15 skipped; the two in-process source-inspection artifacts pass in isolation, and the one dirty sibling-checkout corpus failure passes against this feature worktree
  • 38 release-inventory tests passed
  • Hermes contract harness passed: 51 contracts, 32 schemas, 110 fixtures, 837 tests collected
  • ideation contracts passed: 43 positive, 73 negative, 8 transition fixtures; 0 errors and 4 expected deprecation warnings
  • 151 manifest digests and strict wallet validation passed
  • contract-v1.43 candidate inventory reproduced byte-for-byte and verify-commit passed at the pushed tree
  • strict OpenSpec validation, Python compilation, and diff checks passed

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @openxfactory[bot]! 👋

Your private repo does not have access to Sourcery.

Please upgrade to continue using Sourcery ✨

@brettheap

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9ce9a06875

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1641 to +1642
references=(_repo_reference(root, folder / ".openspec.yaml"),),
recorded_at=_path_recorded_at(root, folder))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Correlate the origin evidence, not any folder edit

When an active proposal's staged origin predates abandonment but any unrelated file in its change folder is committed afterward, _path_recorded_at(root, folder) reports that later folder-wide commit as the origin evidence's recording time. machine_release_correlation_error then accepts the historical proposal as post-abandonment evidence and cleanup can delete a branch whose newer exploration was never preserved. Record the commit time of the actual origin evidence rather than the newest commit touching the entire change directory.

AGENTS.md reference: AGENTS.md:L9-L10

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid, and it was fail-OPEN — reproduced, then fixed in #362 (commit a21d40f).

The reproduction: archive exact-origin custody before the session, abandon it, then commit an unrelated tasks.md inside that archived change folder. _path_recorded_at(root, folder) reported that unrelated commit as the origin evidence's recording time, machine_release_correlation_error accepted the historical proposal, and cleanup answered 200 and DELETED the branch — the newer exploration it was the only record of was preserved by nothing.

Fix: recorded_at is now the recording time of the origin evidence the candidate actually cites (<folder>/.openspec.yaml), derived from the same path as the reference so the two cannot drift. An origin declaration with no commit of its own therefore has no durable recording time, which the correlation already treats as a refusal — the tightening runs in the fail-closed direction. Regression: test_a_later_edit_elsewhere_in_the_change_folder_does_not_freshen_the_origin, which also pins that a later commit to the declaration ITSELF still releases cleanup.

Comment on lines +3210 to +3214
if machine_retention is not None and abandonment is not None and head:
correlation_error = branch_session.machine_release_correlation_error(
machine_retention, abandonment, head)
if correlation_error:
machine_retention = None

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Evaluate every matching retention candidate

When a reused tile has multiple matching lifecycle records, retention_release_for returns one deterministic candidate before abandonment correlation is checked here. If that first candidate predates abandonment, it is discarded and cleanup refuses without considering a later active, archived, or demotion record that independently satisfies the retention requirement; the operator must unnecessarily use an explicit release despite valid machine evidence. Correlate all matching candidates and select a qualifying one before concluding that machine retention is unavailable.

AGENTS.md reference: AGENTS.md:L9-L10

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid — reproduced and fixed in #362 (commit a21d40f). Measured impact is operator friction rather than a safety hole: the wrong answer here was a refusal, not a deletion.

Reproduction: two archived changes carrying the same staged origin, the first-sorted one older than the abandonment and a successor archived after it. Cleanup refused with "the retention evidence was recorded ... before the session was abandoned" and the operator's only way through was an explicit human release they should not have needed.

Fix: retention_release_candidates returns EVERY matching record in acceptance order (active custody, archived custody, pick fallback, receipt-proved demotion, corroborated legacy demotion) and execute_cleanup_abandoned_branch takes the first that CORRELATES — a candidate that fails disqualifies only itself. The reported error stays the FIRST candidate's, so a refusal still names the nearest-miss evidence. No candidate clears a lower evidence bar than before, and when every candidate predates the abandonment the refusal is unchanged: test_no_matching_record_after_abandonment_still_refuses_cleanup pins that half alongside test_a_second_matching_record_recorded_after_abandonment_releases_cleanup.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR closes the abandoned-session cleanup “terminal-state gap” by making cleanup decisions evidence-based and transactional: cleanup now requires explicit retention-release evidence (active, archived, executed-demotion, or explicit human release), binds that evidence to the exact abandoned head and chronology, and deletes refs atomically using Git’s expected-old-value mechanism with safe restoration on record-finalization failure. It also hardens related governance surfaces (demotion receipts, contract/validator coverage, NotebookLM projection title injectivity, and doc-health family enumeration drift prevention).

Changes:

  • Add atomic expected-SHA local ref deletion (and guarded restore) plus additional “checked out anywhere” safety checks for session branch deletion.
  • Introduce durable demotion execution receipts (schema + examples + validator rules) and write receipts after successful demotion execution.
  • Fix NotebookLM projection title collisions by deriving injective stems, and add a new doc-health family (“family-enumeration”) with fixtures/tests to prevent deterministic-family prose drift.

Reviewed changes

Copilot reviewed 35 out of 35 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/ideation-dashboard/test_session_git.py Adds regression for atomic expected-value delete preserving concurrently moved refs.
tests/ideation-dashboard/test_gate_console.py Adds demotion receipt validation tests and cleanup record collision/semantics tests.
tests/doc-health/test_lifecycle_scan_set.py Updates reader/non-reader classification expectations for the new doc-health family.
tests/doc-health/fixtures/family-enumeration-unknown-name/alphaFactory/openspec/specs/doc-health/spec.md New fixture: unknown family name in deterministic-family prose.
tests/doc-health/fixtures/family-enumeration-thin-delta/alphaFactory/openspec/specs/doc-health/spec.md New fixture: canon expectation for thin-delta prevention scenario.
tests/doc-health/fixtures/family-enumeration-thin-delta/alphaFactory/openspec/changes/add-something/specs/doc-health/spec.md New fixture: thin delta that improperly truncates requirement text.
tests/doc-health/fixtures/family-enumeration-stale-numeral/alphaFactory/openspec/specs/doc-health/spec.md New fixture: stale numerals in deterministic-family prose.
tests/doc-health/fixtures/family-enumeration-pending/alphaFactory/openspec/specs/doc-health/spec.md New fixture: pending-promotion behavior for canon vs active delta.
tests/doc-health/fixtures/family-enumeration-pending/alphaFactory/openspec/changes/add-something/specs/doc-health/spec.md New fixture: delta that updates enumeration/numerals.
tests/doc-health/fixtures/family-enumeration-no-spec/betaFactory/openspec/specs/solo/spec.md New fixture: repository without doc-health capability.
tests/doc-health/fixtures/family-enumeration-missing-name/alphaFactory/openspec/specs/doc-health/spec.md New fixture: missing “family-enumeration” name in prose list.
tests/doc-health/fixtures/family-enumeration-clean/alphaFactory/openspec/specs/doc-health/spec.md New fixture: clean/consistent deterministic-family prose.
specs/011-council-feature-clearance/tasks.md Updates task gating notes to reflect successor dependency vs “ruling exists”.
specs/011-council-feature-clearance/spec.md Records correction about schema capabilities vs security/ruling basis.
specs/011-council-feature-clearance/implementation-notes.md Records the R4 ruling outcome and successor dependency.
scripts/validate-ideation-dashboard-contracts.py Adds demotion receipt + cleanup record semantic checks (scope identity, safe paths).
scripts/sync-notebooklm-books.py Makes notebook title derivation injective (shortest distinguishing suffix) and improves parity to detect collapses.
scripts/ideation_dashboard/session_git.py Adds path commit-time reader; implements atomic expected-SHA ref deletion and restore; adds “checked out anywhere” delete guard.
scripts/ideation_dashboard/generator.py Promotes change/origin helpers to public names with compatibility aliases.
scripts/ideation_dashboard/cli.py Writes demotion execution receipts on --execute; hardens cleanup CLI validation and reporting.
scripts/ideation_dashboard/boundary.py Adds exclusive-create and atomic-replace output helpers to support prepared/completed transactions.
scripts/hermes_runtime_validation/release.py Includes ideation-dashboard contract validator in Hermes runtime auxiliary members.
scripts/doc_health/release_inventory.py Clarifies release-inventory rationale around untagged bundles as a general rule.
scripts/doc_health/families.py Registers new family-enumeration family.
scripts/doc_health/duplicate_packet.py Updates module docstring with ruled measurement basis decision.
scripts/doc_health/init.py Fixes/report-order projection drift by ensuring FAMILY_IDS includes all registered families and adds family-enumeration.
README.md Adds index entries for new changes (projection title uniqueness; family enumeration check) and updates archived-change notes.
openspec/changes/fix-abandoned-session-cleanup-terminal-states/tasks.md New change packet tasks for cleanup evidence, transactions, tests, and guidance.
openspec/changes/fix-abandoned-session-cleanup-terminal-states/specs/ideation-dashboard/spec.md New ideation-dashboard spec delta defining cleanup evidence/transaction semantics.
openspec/changes/fix-abandoned-session-cleanup-terminal-states/proposal.md New proposal describing evidence-based abandoned-branch cleanup and atomic deletion.
openspec/changes/fix-abandoned-session-cleanup-terminal-states/design.md New design describing evidence model, demotion proof, and transactional cleanup.
openspec/changes/fix-abandoned-session-cleanup-terminal-states/.openspec.yaml New change metadata declaring ad-hoc origin for the cleanup-terminal-states change.
openspec/changes/create-medxpractice-overlay-boundary/.openspec.yaml Adds ad_hoc origin metadata and approval provenance.
openspec/changes/create-medxchart-overlay-boundary/.openspec.yaml Adds ad_hoc origin metadata and approval provenance.
openspec/changes/archive/2026-08-25-add-duplicate-packet-check/tasks.md Records closure of previously open questions by ruling.
openspec/changes/archive/2026-08-14-add-worker-credential-by-reference/.openspec.yaml Adds derived origin metadata for archived packet.
openspec/changes/archive/2026-08-07-add-register-edit-lane/.openspec.yaml Adds derived origin metadata for archived packet.
openspec/changes/archive/2026-08-06-add-lens-gate-verbs/.openspec.yaml Adds derived origin metadata for archived packet.
openspec/changes/archive/2026-08-06-add-consent-instrument/.openspec.yaml Adds derived origin metadata for archived packet.
openspec/changes/archive/2026-08-05-adopt-neutral-tooling-home/.openspec.yaml Adds derived origin metadata for archived packet.
openspec/changes/archive/2026-08-05-add-neutrality-drift-lane/.openspec.yaml Adds derived origin metadata for archived packet.
openspec/changes/archive/2026-08-04-adopt-neutral-utility-pack/.openspec.yaml Adds derived origin metadata for archived packet.
openspec/changes/archive/2026-08-02-add-workbench-integrated-editor-chat/.openspec.yaml Adds derived origin metadata and approval provenance.
openspec/changes/archive/2026-08-01-add-propose-verb/.openspec.yaml Adds derived origin metadata for archived packet.
openspec/changes/archive/2026-07-30-publish-semantic-kernel/.openspec.yaml Adds derived origin metadata for archived packet.
openspec/changes/archive/2026-07-30-add-ontology-term-lifecycle-enforcement/.openspec.yaml Adds derived origin metadata for archived packet.
openspec/changes/archive/2026-07-30-add-ontology-stewardship-hardening/.openspec.yaml Adds derived origin metadata for archived packet.
openspec/changes/archive/2026-07-30-add-omnigent-semantic-wiring/.openspec.yaml Adds derived origin metadata for archived packet.
openspec/changes/archive/2026-07-30-add-domain-ontology-layer/.openspec.yaml Adds derived origin metadata for archived packet.
openspec/changes/archive/2026-07-30-add-deployment-handoff-boundary/.openspec.yaml Adds derived origin metadata for archived packet.
openspec/changes/archive/2026-07-24-add-hermes-domain-content-manifest/.openspec.yaml Adds derived origin metadata for archived packet.
openspec/changes/archive/2026-07-24-add-client-layer-tuning-contracts/.openspec.yaml Adds derived origin metadata for archived packet.
openspec/changes/archive/2026-07-23-add-hermes-domain-overlay-contract/.openspec.yaml Adds derived origin metadata for archived packet.
openspec/changes/archive/2026-07-23-add-governed-derived-model/.openspec.yaml Adds derived origin metadata for archived packet.
openspec/changes/archive/2026-07-17-add-client-infrastructure-liaison/.openspec.yaml Adds derived origin metadata for archived packet.
openspec/changes/archive/2026-07-16-adopt-avatar-client-lab-candidates/.openspec.yaml Adds derived origin metadata for archived packet.
openspec/changes/archive/2026-07-14-add-github-app-identity-tiers/.openspec.yaml Adds derived origin metadata for archived packet.
openspec/changes/add-worker-enrollment-broker/.openspec.yaml Adds staged origin metadata and approval provenance.
openspec/changes/add-substantive-review-lane/tasks.md Records corrections about matcher/schema capabilities and upstream discharge notes.
openspec/changes/add-shared-identity-seeds/.openspec.yaml Adds ad_hoc origin metadata and approval provenance.
openspec/changes/add-projection-title-uniqueness/specs/lifecycle-notebook-projection/spec.md New spec delta formalizing injective title derivation and document-level parity.
openspec/changes/add-projection-title-uniqueness/.openspec.yaml New change metadata for projection title injectivity.
openspec/changes/add-omnigent-domain-terminology/.openspec.yaml Adds ad_hoc origin metadata and admission approval provenance.
openspec/changes/add-nightly-dashboard-refresh/.openspec.yaml Adds ad_hoc origin metadata and admission approval provenance.
openspec/changes/add-model-provider-broker/.openspec.yaml Adds ad_hoc origin metadata and admission approval provenance.
openspec/changes/add-hermes-customer-subject-runtime-contract/.openspec.yaml Adds ad_hoc origin metadata and approval provenance.
openspec/changes/add-family-enumeration-check/proposal.md New proposal adding doc-health family-enumeration check.
openspec/changes/add-family-enumeration-check/design.md New design for the family-enumeration check.
openspec/changes/add-family-enumeration-check/.openspec.yaml New change metadata for family-enumeration check.
openspec/changes/add-doxchat-model-intake/.openspec.yaml Adds ad_hoc origin metadata and approval provenance.
openspec/changes/add-dispatch-credential-contract/.openspec.yaml Adds ad_hoc origin metadata and approval provenance.
openspec/changes/add-composed-view-authoring/.openspec.yaml Adds ad_hoc origin metadata and approval provenance.
examples/ideation-dashboard/README.md Registers new examples/negatives for cleanup records and demotion receipts.
examples/ideation-dashboard/negative/gate-action-cleanup-scope-mismatch.yaml New negative example: cleanup scope mismatch.
examples/ideation-dashboard/negative/gate-action-cleanup-machine-empty-evidence.yaml New negative example: machine evidence missing required references.
examples/ideation-dashboard/negative/gate-action-cleanup-explicit-release-without-reason.yaml New negative example: explicit release missing required reason.
examples/ideation-dashboard/negative/demotion-execution-receipt-unsafe-path.yaml New negative example: demotion receipt path traversal/unsafe path.
examples/ideation-dashboard/negative/demotion-execution-receipt-destination-mismatch.yaml New negative example: demotion receipt destination mismatch.
examples/ideation-dashboard/gate-action-record-cleanup-abandoned-branch.example.yaml New positive example: cleanup-abandoned-branch record.
examples/ideation-dashboard/gate-action-record-abandon-session.example.yaml Updates abandon-session example commentary to reflect new cleanup semantics.
examples/ideation-dashboard/demotion-execution-receipt.example.yaml New positive example: demotion execution receipt.
docs/lifecycle-notebook-projection.md Updates lifecycle notebook projection rule documentation to require injective titles and document-level parity.
docs/ideation-dashboard-session-runbook.md Updates operator guidance for cleanup evidence lanes and transactional semantics.
docs/contract-versioning-policy.md Updates policy section to record discharge of previously untagged bundles and tagging rule.
docs/archive-record-discrepancies.md Adds record entry about projection title collapse and its remediation.
contracts/schemas/gate-action-record.schema.yaml Adds cleanup-abandoned-branch action and cleanup evidence structure (prepared/completed).
contracts/schemas/demotion-execution-receipt.schema.yaml Adds new schema for executed demotion receipts.
contracts/README.md Documents new schemas and extended validator responsibilities for contract-v1.43.
contracts/manifest.yaml Advances bundle version and registers new/updated schema digests.
contracts/hermes-runtime/contract-index.yaml Registers demotion-execution-receipt as a release-schema.
contracts/CHANGELOG.md Adds contract-v1.43 (and v1.42 editorial cut) entries describing the new schema surfaces and release identity changes.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +712 to +718
current = self.branch_sha(branch)
if current != expect_sha:
raise SessionGitRefused(
f"refusing to delete {branch!r}: it was at {expect_sha} when "
f"cleanup was authorized and is at {current} now. Git's "
"atomic expected-value ref transaction preserved the changed "
"ref; observe it and decide again")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid — fixed in #362 (commit a21d40f), at both sites.

Measured first: git update-ref -d <ref> <old> cannot lock a ref that is already gone (error: cannot lock ref ..., rc=1), so a concurrently removed branch reaches this failure path rather than the success path. branch_sha() then returned None and the refusal described an absence as an object id. The pre-flight compare in delete_branch had the same defect and is the likelier place to observe it, so it is fixed too.

Both refusals now say the ref NO LONGER EXISTS and that this process deleted nothing, and restore_branch_if_absent distinguishes "still absent" from a value. Behaviour is deliberately unchanged: the transaction still refuses and the prepared cleanup record still unwinds, so no durable artifact claims a deletion this process did not perform. Regressions: test_a_concurrently_removed_ref_is_reported_as_absent_not_as_none, test_a_ref_removed_before_the_delete_is_reported_as_absent_too, test_a_restore_onto_a_ref_that_is_still_absent_says_so.

@brettheap
brettheap merged commit 2434b84 into main Aug 25, 2026
2 checks passed
@brettheap

Copy link
Copy Markdown
Contributor

All three bot findings on this PR are VALID and are fixed in #362 (change/correlate-cleanup-retention-evidence, commit a21d40f69f676af1089556468e9666bb6e0056dd) rather than here: the reviews landed at 16:32/16:33Z and this PR merged at 16:41Z, so main carried every defect. Each one was reproduced against the merged code before being touched.

Codex P1, branch_session.py — correlate the origin evidence, not any folder edit. Fail-OPEN. Archive exact-origin custody before a session, abandon it, then commit an unrelated tasks.md in that change folder: _path_recorded_at(root, folder) reported the unrelated commit as the origin evidence's recording time, correlation accepted the historical proposal, and cleanup answered 200 and DELETED the branch. recorded_at now comes from the cited .openspec.yaml itself, so evidence and timestamp cannot drift; an undated declaration has no durable recording time and the correlation refuses.

Codex P2, gate_routes.py — evaluate every matching retention candidate. Fail-closed friction, not a safety hole. Two archived changes with the same staged origin, the first-sorted older than the abandonment: cleanup refused despite a successor archived afterwards. retention_release_candidates now returns every matching record in acceptance order and the route takes the first that correlates; a failing candidate disqualifies only itself, the refusal still reports the first candidate's error, and no candidate clears a lower evidence bar than before.

Copilot, session_git.py — the missing-ref case. git update-ref -d <ref> <old> cannot lock an already-deleted ref (measured), so a concurrent removal lands on the failure path and the refusal said the branch "is at None now". Both compare sites (the transaction and the pre-flight in delete_branch) now report that the ref NO LONGER EXISTS and that nothing was deleted; the prepared record still unwinds, so no artifact claims a deletion that did not happen.

No declines — nothing was left unfixed.

Tests (real numbers, set -o pipefail, exit codes unmasked): tests/ideation-dashboard 4181 passed, 13 skipped; focused test_session_gates.py + test_session_git.py + test_gate_console.py 278 passed; five new regressions. openspec validate --all --strict 77 passed, 0 failed; validate-ideation-dashboard-contracts.py 0 errors (4 pre-existing deprecation warnings).

#362 deliberately touches neither the OpenSpec packet nor the canonical spec, so it does not conflict with #361's archive-and-promote of this change.

@brettheap
brettheap deleted the change/fix-abandoned-session-cleanup-terminal-states branch August 26, 2026 07:17
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.

2 participants