Skip to content

Owner-only discard of construction evidence - #138

Closed
leonardoaraujosantos wants to merge 8 commits into
feat/managed-recovery-ownerfrom
feat/construction-evidence-discard
Closed

leonardoaraujosantos wants to merge 8 commits into
feat/managed-recovery-ownerfrom
feat/construction-evidence-discard

Conversation

@leonardoaraujosantos

Copy link
Copy Markdown
Contributor

Advances #93. Stacked on #123 (feat/managed-recovery-owner). Retarget to main once #123 merges.

Summary

This implements the approved OpenSpec change add-construction-evidence-discard (tasks 2.1–2.4). The local owner can now discard one unpublished construction stage at a time, in two steps (review, then confirm), and each discard leaves an audit receipt. A stage is one of:

  • a checkout claim stage (preparing_*)
  • a checkout acknowledgement stage (ack_preparing_*)
  • a file-copy cancellation-record stage (tmp_* beside preparation.json)

Before this, nothing could remove these stages, so a managed worktree with one could never be removed.

  • Native primitives (cyber_core::construction_discard):
    • Observe a stage through a no-follow, non-blocking handle: regular file, one link, at most 1 MiB, recorded as dev/inode/size/mtime/SHA-256.
    • Move it with a no-replace rename into a private 0700 discarded/<key>/ slot in the same namespace. Every step re-checks the identity; a file that differs is reported and never deleted.
    • Checkout fences are in Repository::{review,fence}_snapshot_construction. Cancellation-record fences are in cyber-snapshot copy::discard.
  • Runtime/API:
    • GET /sessions/{id}/rewind/constructions lists stages and discard pending requests.
    • POST .../discard/review, POST .../discard/confirm, GET .../discard/requests/{request_id}.
    • Receipts are session.snapshot.construction_discarded.1 events in the Session's history (intent, then completed / pending / changed).
    • A repeated request ID resumes the frozen plan. Lookup never acts.
    • Maintenance retries pending unlinks a minute after start, then hourly, with no time limit.
    • OpenAPI and the TypeScript SDK are regenerated (constructionInventory, constructionDiscardReview/Confirm/Lookup).
  • CLI/TUI/doctor:
    • New commands: cyber sessions recovery-discard and recovery-discard-request.
    • sessions recovery, both TUI recovery panels and cyber doctor show the evidence and pending discards with the exact command. The TUI has no discard action (owner decision).
    • docs/cli.md and docs/rewind.md are updated.

Design decisions

These are recorded in design.md under "Implementation notes".

  • Owner-only.
    • The main listener already requires the owner credential.
    • Review and confirm also return 403 owner_only for a bearer token, X-Cyber-Attach, a browser (Origin / Sec-Fetch-Mode) or an mcp: client label.
    • Peers have no route to it, and there is no model tool.
  • Fencing order, under the Session lock:
    1. The Session is idle, with no background work, no admitted worker and no unsettled snapshot activity.
    2. The review fingerprint is unchanged (stage and Session revision).
    3. The activity's lifetime lock and the stage's own constructor lock are acquired. For cancellation records, the namespace lock is held too.
    4. The published-record rule passes.
    5. The intent is committed. Only then is the stage moved and unlinked.
  • Published-record rule. With rename publication, a record published from a stage cannot exist next to that stage. So:
    • A claim stage refuses while <op>.lock exists.
    • An acknowledgement stage refuses until a .snapshot-ack from another stage exists.
    • A cancellation-record stage refuses until its journal's preparation.json exists.
    • The last two mean receipt reconciliation has run once. This is stricter than the design text, which only required it for acknowledgement stages.
  • What counts as a cancellation-record stage. A tmp_* file counts only when its complete bytes are the cancellation receipt of the exact origin that owns the journal. Any other journal file is never touched.
  • Removed Location. A Location whose directory is gone is supported for cancellation-record stages, found from the origin's recorded identities. A managed Session whose Location was removed refuses.

Tests

  • cyber-cli/tests/support/construction_discard.rs, real binary, native-git-acceptance (new CI step in non-git-snapshot-proof.yml):
    • A SIGKILLed claim stage is listed and reviewed read-only.
    • Confirm refuses while the preparation is unresolved, and refuses a stale fingerprint.
    • Forced worktree removal stays fenced, then is admitted after the discard.
    • Retry and lookup return the same receipt.
    • Kill table at construction-discard-intent-committed, -quarantined and -unlinked. For each cut: the intent is durable; the listing and doctor show discard pending; maintenance finishes a moved stage on its own; the same request ID resumes to completed; removal is admitted.
  • cyber-server/tests/http.rs: 403 for every non-owner caller on review and confirm; listing is read-only; lookup returns 404.
  • cyber-core tests:
    • A live constructor lock or a published claim refuses.
    • An unreconciled acknowledgement refuses.
    • Malformed names have no owner.
    • Links, FIFOs, aliases and oversized stages are refused.
    • Changed or foreign files are never moved or deleted.
  • cyber-snapshot tests:
    • A busy namespace or a held lifetime lock refuses.
    • An unreconciled cancellation refuses.
    • Foreign journal files are untouched.
    • Discard works after the Location is removed.
    • Quarantined bytes count toward the snapshot cap until unlinked.
  • TUI: the panel shows the command and no key acts.
  • Existing branches_cli preparation_ / managed_git_ / checkout_claim tests pass.
  • Gates: fmt --check, clippy --all-targets -D warnings (cognitive complexity ≤ 15 enforced by clippy.toml), openspec validate --all --strict, spec_lint (0 errors), spec_inventory --check, help-markdown equals docs/cli.md.

Not covered / open for the owner

  • Snapshot cap. Quarantined checkout stages live in the repository's Git metadata, outside the snapshot store. They are reported as discard pending but do not count toward the snapshot cap.
  • Removed managed checkouts. Discard refuses for a managed Session whose Location was removed: its registration cannot be verified and its checkout stages cannot be discovered.
  • Partial stages. A partially written (unparsable) cancellation stage is not listed and stays fenced.
  • Request interrupted before the move. It waits for the owner to repeat the confirmation. Maintenance only finishes stages that already moved.
  • Platforms. Windows and other non-unix platforms refuse.
  • Same-user races. A process running as the same user could still swap a file between the identity check and the unlink; the 0700 private directories and the identity re-checks narrow that window.

Observe a stage through a no-follow, non-blocking handle (regular single-link
file, 1 MiB bound, SHA-256), move it with a no-replace rename into a private
discarded/<key>/ slot of its namespace and unlink it there only while its
identity still matches. Checkout claim and acknowledgement stages are fenced by
their constructor lock and published records; file-copy cancellation-record
stages are bound to their exact origin, the namespace lock and the owner's
lifetime lock, and quarantined bytes keep counting toward the snapshot cap.
Review is read-only and fingerprints the stage and Session revision; confirm
re-fences the Session and owner activity, records a durable
session.snapshot.construction_discarded.1 intent, quarantines and unlinks the
stage and completes the receipt. A repeated request ID resumes the frozen plan,
lookup never acts and maintenance retries pending unlinks hourly. Review and
confirm refuse bearer, peer, browser and MCP callers with 403.
cyber sessions recovery-discard lists, reviews and confirms one stage;
recovery-discard-request looks a receipt up. sessions recovery, the TUI
recovery panels and cyber doctor show construction evidence and discard
pending requests with the exact command; the TUI has no discard action.
A SIGKILLed claim stage refuses discard while its preparation is unresolved and
for a stale review, then is discarded and worktree removal is admitted. A cut
table kills the service after the intent, after the quarantine move and after
the unlink; each request resumes, maintenance finishes a moved stage and doctor
reports it as pending meanwhile.
…ade fixture

The two path helpers are only used by the Unix implementation and broke the
Windows -D warnings build. The doctor JSON test now expects the new
construction discards check. The upgrade test archive is compressed at the
fastest level, since a stored debug binary on Linux CI now exceeds the
upgrader's 512 MiB download cap.
On Linux CI the debug binary with line tables is larger than the upgrader's
per-member archive limit, so compression alone did not keep the fixture
valid. A stripped copy still runs, which the test needs to check that the
next start removes cyber.old.
@leonardoaraujosantos

Copy link
Copy Markdown
Contributor Author

Merged into main via #140 (contains every commit of this PR).

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