Skip to content

fix(placement): refuse unsafe memory checkpoints - #296

Open
Tatsat (Tats) Mishra 🐉 (Tatsinnit) wants to merge 5 commits into
agent-primitives:mainfrom
Tatsinnit:fix/98-incomplete-memory-checkpoint
Open

Tatsat (Tats) Mishra 🐉 (Tatsinnit) wants to merge 5 commits into
agent-primitives:mainfrom
Tatsinnit:fix/98-incomplete-memory-checkpoint

Conversation

@Tatsinnit

Copy link
Copy Markdown
Contributor

Summary

  • refuse memory-tier Suspend and Fork when the latest execution can be re-driven
  • stop stateful Fork from cancelling a live parent turn, and bind memory Suspend to an exact validated journal head
  • publish CHECKPOINT_INCOMPLETE_TURN, strengthen the Runtime checkpoint contract, and update conformance/docs

Fixes #98.

Problem

A REQUIRES_MEMORY_SNAPSHOT harness can mutate RAM before its next durable sink record. Previously, a stateful Fork could fence and close that turn, then snapshot the mutated RAM while its journal still lacked END. Suspend could also snapshot an abandoned incomplete execution. A later Resume restored that RAM and re-drove the incomplete turn, applying the mutation twice.

The same mismatch could occur when another host advanced the journal while public Suspend was capturing RAM: Suspend used to append at the post-snapshot current head rather than the head it validated before capture.

Design

Controller-owned checkpoint classifier

controller.ValidateCheckpointState verifies the supplied hash chain and reuses the controller's execution and approval projections. A memory checkpoint is accepted only when recovery cannot re-run captured mutations:

  • empty, lifecycle-only, or completed history
  • a strict partial-input prefix that never reached Harness.Run
  • an owned undecided approval park, where recovery parks without running the harness
  • an inherited unresolved approval, where recovery refuses before running the harness

It refuses ordinary modern or legacy incomplete turns, modern ERROR tails, markerless pending turns, decided approval gates without a receipt, receipted executions without END, and malformed evidence.

A decided gate is deliberately refused: an earlier recovery may have changed RAM before failing without a receipt.

Stateful Fork admission

A memory-tier Fork temporarily takes the existing session operation guard through journal validation and checkpoint-mark installation. Therefore:

  • a locally active turn or pre-journal Connect preparation returns retryable SESSION_BUSY
  • the turn is not cancelled, fenced, or disconnected
  • an abandoned incomplete journal tail returns CHECKPOINT_INCOMPLETE_TURN
  • the refusal happens before a fence, snapshot, child allocation plan, runtime Fork, or child journal write

After the checkpoint mark is installed, the transient operation hold is released so calls during snapshot/fan-out retain CHECKPOINT_IN_PROGRESS behavior.

Stateless forks are unchanged.

Memory Suspend publication

On a memory-capable backend, Suspend admits the harness to determine whether it is memory-tier. A memory-tier Suspend:

  1. verifies and classifies the current records
  2. mints a fence before capture
  3. rereads and revalidates the same exact head
  4. calls Snapshot(EXTERNAL)
  5. appends SUSPEND with CAS at that validated head and fence

A peer writer can therefore invalidate publication, but cannot widen the accepted checkpoint boundary over its incomplete turn. If the physical capture landed before the CAS failed, the Runtime contract now requires a memory backend to quarantine it by refusing a mismatched RecordedCheckpoint; Substrate already enforces that rule.

Stateless Suspend keeps its existing replay-safe behavior.

Public errors

Abandoned incomplete memory checkpoints map to:

  • gRPC FAILED_PRECONDITION
  • google.rpc.ErrorInfo{domain: "agentsessions", reason: "CHECKPOINT_INCOMPLETE_TURN"}

Active local contention remains ABORTED / SESSION_BUSY. Tampered Suspend journals are DATA_LOSS. Memory-capable Suspend admission maps unavailable harnesses to UNAVAILABLE / HARNESS_UNAVAILABLE, and descriptor/class refusals to FAILED_PRECONDITION.

Tests

Added or updated coverage for:

  • modern, legacy, malformed, partial-input, approval, inherited, decided and receipted checkpoint states
  • active turns across the same Placer, another Registry Placer, and dynamically added Placers
  • no stream cancellation, fence, snapshot, journal movement, child publication, runtime Fork, or cleanup allocation row on refusal
  • peer movement before capture and during capture
  • quarantine of an unrecorded physical capture
  • tampered Suspend journals
  • memory-capable Suspend admission status mapping
  • generated protobuf/API reference and public ErrorInfo reason

Verification

  • go test -race -shuffle=on ./controller
  • go test -race -shuffle=on ./placement
  • go test -race -shuffle=on ./session
  • go test -race -shuffle=on ./... in integrations/substrate
  • go test -race -shuffle=on ./... in integrations/claudecode
  • go build ./...
  • go vet ./...
  • buf lint
  • buf breaking --against ".git#tag=v0.4.0,subdir=api"
  • go run ./hack/doccheck
  • python3 hack/verify_chain.py --golden
  • ./hack/verify_chain.sh
  • gofmt -l .
  • git diff --check

The repository-wide root race run still reaches the pre-existing Python 3.9 portability failure in internal/agentsessionsd.TestDocumentedPythonExecutorLogsStorageFailureWithoutPayload: Python reports the documented fallback sqlite_errorname=unknown while the test expects SQLITE_ERROR. The same targeted failure reproduces on clean main; all changed packages pass under the race detector.

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.

🟡 Changes recommended

The refusal test never cancels and joins Exec, leaving its goroutine, live connection, and session guard running indefinitely after successful assertions.

1 open issue
1 file not reviewed
  • api/genpb/session_grpc.pb.go: Generated file
What changed in this PR

This PR addresses #98 by preventing memory checkpoints that could cause recovery to apply RAM mutations twice. It coordinates checkpoint admission with execution state and journal publication.

Changes:

  • Adds a controller-owned checkpoint validator for incomplete executions and approval states.
  • Makes stateful Fork refuse active turns without cancelling them, and binds memory Suspend publication to the validated journal head.
  • Publishes CHECKPOINT_INCOMPLETE_TURN, strengthens the Runtime contract, and updates tests and documentation.
File Description
spec.md Defines checkpoint safety, admission, and error contracts.
session/​service.go Maps checkpoint and Suspend admission errors.
session/​fork_wake_test.go Removes the obsolete fork-versus-wake race test.
session/​errors.go Adds the incomplete-checkpoint error reason.
session/​error_reasons_test.go Tests checkpoint error mappings.
session/​checkpoint_test.go Tests admission failures and non-cancelling Fork refusal.
session/​approval_override_test.go Checks refusal of interrupted memory overrides.
placement/​transport_test.go Updates stream tests for non-cancelling checkpoint refusal.
placement/​suspend_test.go Covers incomplete turns, journal races, and capture quarantine.
placement/​session_guard.go Updates guard documentation.
placement/​resume_cas_test.go Tests Fork refusal during Resume restoration.
placement/​registry.go Documents shared non-cancelling Fork admission.
placement/​registry_add_test.go Tests refusal across dynamically added Placers.
placement/​placement.go Implements checkpoint validation, guarding, and exact-head publication.
placement/​placement_test.go Uses completed histories in memory-fork fixtures.
placement/​memory_approval_test.go Tests refusal of decided approval checkpoints.
placement/​legacy_inventory_test.go Updates legacy fork fixtures to completed turns.
placement/​fork_verify_test.go Updates the tampered-parent test fixture.
placement/​compute_guard.go Updates checkpoint cleanup commentary.
placement/​compute_guard_test.go Removes obsolete checkpoint-supersession cases.
placement/​cleanup_resources_test.go Checks refusal before child allocation tracking.
placement/​approval_test.go Updates decided-approval fork expectations.
docs/​substrate-conformance.md Documents checkpoint admission and race guarantees.
docs/​faq.md Explains contention and incomplete-turn refusal.
docs/​architecture.md Describes safe memory checkpoint coordination.
docs/​api-reference.md Updates generated error and Suspend documentation.
controller/​checkpoint.go Adds journal-based checkpoint safety classification.
controller/​checkpoint_test.go Covers execution and approval checkpoint states.
changelog/​unreleased/​incomplete-memory-checkpoint.md Records safety changes and Runtime obligations.
api/​session.proto Documents the new reason and Suspend admission behavior.
api/​runtime.go Requires rejection of unrecorded retained captures.
api/​genpb/​session_grpc.pb.go Synchronizes generated service comments.

Review effort: Balanced

Comment thread placement/transport_test.go Resolved
Copilot AI balanced review requested due to automatic review settings November 27, 2026 08:52

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.

🔵 Needs a closer look

A maintainer should confirm that deployed custom memory backends quarantine captures after a failed journal CAS, as checkpoint safety now depends on this newly mandatory Runtime SPI behavior.

No open issues 🎉

1 resolved since last review
1 file not reviewed
  • api/genpb/session_grpc.pb.go: Generated file

Review effort: Balanced

@aramase Anish Ramasekar (aramase) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The fix for #98 looks right; one changelog gap blocks it, plus a few small things.

Nits:

  • spec.md:1426 still says the reference host accepts Suspend on a session that is already cold. A parked memory session with a decided gate now refuses a second Suspend with CHECKPOINT_INCOMPLETE_TURN; qualify the sentence with the OPS-3 exception.
  • api/session.proto:472-478 and :490-497: the Suspend and Fork comments don't mention CHECKPOINT_INCOMPLETE_TURN (FAILED_PRECONDITION) or Fork's new SESSION_BUSY refusal of a running turn. One sentence each, then regenerate genpb and api-reference.
  • placement/placement.go:1736, :988-1030: endHarnesses is a no-op now that beginCheckpoint returns no connections, and superseded/checkSuperseded/errSuperseded (placement.go:807-860, :568-573, cleanup.go:135) are unreachable. Delete them here or in a follow-up.
  • placement/placement.go:1727-1729: the comment breaks mid-sentence; rewrap to 100 columns.

Copilot AI balanced review requested due to automatic review settings November 29, 2026 09:36
@Tatsinnit

Copy link
Copy Markdown
Contributor Author

Thank you for the careful review. I addressed the remaining nits in 4b746b8: qualified the already-cold Suspend text with the OPS-3 exception; documented the Suspend and Fork refusals in the proto and regenerated genpb/API reference; removed the now-unreachable endHarnesses and supersession machinery; and fixed the broken comment wrap. I also tightened the Fork wording to preserve the existing CHECKPOINT_IN_PROGRESS result when a checkpoint mark is already held. Focused race tests and the controller, placement, and session race suites pass.

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.

🟡 Changes recommended

A concurrent approval result during automatic handoff can be misclassified as an incomplete checkpoint, causing an INTERNAL error instead of the expected ABORTED / STALE_CURSOR conflict.

1 open issue
1 file not reviewed
  • api/genpb/session_grpc.pb.go: Generated file

Review effort: Balanced

Comment thread placement/placement.go Resolved
Copilot AI balanced review requested due to automatic review settings November 29, 2026 10:33

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.

🔵 Needs a closer look

Human assessment is needed because a concurrent peer append can cause checkpoint validation to report an incomplete turn instead of a retryable conflict, or snapshot a moved head.

No open issues 🎉

1 resolved since last review
1 file not reviewed
  • api/genpb/session_grpc.pb.go: Generated file

Review effort: Balanced

@aramase Anish Ramasekar (aramase) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, the earlier threads are all addressed, and the handoff head check at placement.go:993 fixes the Copilot 09:41Z race (reverting it fails both new test rows). Suspend's reread is fine too: it checks the head before classifying.

One blocker, inline: Fork's second read still classifies without re-checking the head.

Optional: in spec.md OPS-3 and the compat fragment, say that a turn still running on another writer sharing the journal is also reported as CHECKPOINT_INCOMPLETE_TURN, and that a retry after it ends succeeds. Today the text only says "abandoned".

Comment thread placement/placement.go Resolved
Copilot AI balanced review requested due to automatic review settings November 29, 2026 21:50
@Tatsinnit

Copy link
Copy Markdown
Contributor Author

Thank you for the follow-up. I also took the optional documentation suggestion in e878df3: OPS-3 and the compatibility fragment now explicitly cover a turn still running through another journal-sharing writer and state that a retry passes the completeness check after the turn records END.

The focused regression passed 100 times under -race; the placement, session, and controller race suites, build, vet, formatting, doccheck, changelog, and chain checks also pass. Anish Ramasekar (@aramase), when convenient, would you mind taking another look?

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.

🔵 Needs a closer look

Failed-publication safety depends on custom Runtime backends enforcing RecordedCheckpoint quarantine and coordinated upgrade/rollback ordering, both requiring maintainer validation beyond the in-memory conformance evidence.

No open issues 🎉

1 file not reviewed
  • api/genpb/session_grpc.pb.go: Generated file

Review effort: Balanced

This branch has not been deployed

No deployments
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.

placement: do not checkpoint a memory-snapshot session with an incomplete turn (re-drive on Resume double-applies RAM mutations)

3 participants