Skip to content

Gate 7: resumable leaf sessions (crash mid-run → resume → verdict) - #11

Merged
pdettori merged 3 commits into
mainfrom
feat/mvp-leaf-resume
Jun 27, 2026
Merged

pdettori merged 3 commits into
mainfrom
feat/mvp-leaf-resume

Conversation

@pdettori

Copy link
Copy Markdown
Member

Summary

Implements MVP spec §7 gate 7 (resume) — the documented stretch — completing the leaf-session verification gate end-to-end. A leaf session killed mid-run now resumes via the M5 durable log and still produces its verdict.

Builds on #10 (Task 6). Plan: docs/superpowers/plans/2026-06-26-mvp-leaf-session-contract.md §7.

What's here

  • runLeaf is now resumable. realProduceVerdict persists the session to Redis under the envelope sessionId (BufferedRedisBackend, which drains continuously so a mid-run crash preserves progress), wiring flushExtension + checkpointExtension alongside submitVerdictExtension + k8sSandboxExtension. Re-invoking the same sessionId resumes from the durable log (openFromCheckpoint, falling back to full reconstruction) instead of starting fresh — unifying §2.4 retry with §7 gate-7 resume.
  • Durable verdict recovery. submit_verdict also appends a verdict custom session entry (same pattern as the checkpoint marker). On resume, if that entry already exists the verdict is recovered from the log and the agent is not re-run (covers a crash after submitting); otherwise the session re-prompts and completes (covers a crash before submitting). verdictFromCustomEntry is unit-tested.
  • Session-id sanitization. The spec's <run_id>/<item_id> id contains a slash, which fails Pi's session-id validation when used as the actual session id. toSessionId() maps it deterministically (slash → dash, trim to alphanumeric ends); unit-tested.

Verification — MVP spec §7, live on Kind sh-knative

LEAF_LIVE_SMOKE=1 bash deploy/knative/leaf-smoke.sh → 9/9 checks pass, 0 fail. New Claim 6:

dispatch a leaf, confirm the session persisted to Redis, force-kill the harness pod mid-run, re-invoke the same sessionId → resumes and produces its verdict.

All prior claims (workspace isolation, fan-out, structured output, model routing, failure path, idempotency, scale-to-zero) still pass.

Unit tests: full harness suite green (63 passed, 2 live-gated skipped).

Behavior note

Re-invoking a sessionId now resumes the existing session rather than starting fresh — consistent with §2.4 ("retry = re-invoke") and required for gate 7. A genuinely fresh re-run uses a new sessionId.

Assisted-By: Claude Code

Paolo Dettori added 3 commits June 27, 2026 10:11
realProduceVerdict now persists the leaf session to Redis under env.sessionId
(BufferedRedisBackend drains continuously) and wires flush + checkpoint
extensions. Re-invoking the same sessionId resumes from the durable log
(openFromCheckpoint) instead of starting fresh — unifying §2.4 retry with §7
gate-7 resume. submit_verdict also writes a durable 'verdict' custom entry, so a
session that crashed after submitting recovers the verdict without re-running the
agent (verdictFromCustomEntry). Unit-tested helper + durable-write; live path in
the smoke.

Signed-off-by: Paolo Dettori <[email protected]>
The resumable path uses env.sessionId as the Pi session id, but the spec's
'<run_id>/<item_id>' convention contains a slash, which fails Pi's id validation
(alphanumeric + .-_ , alphanumeric ends). Add toSessionId() to map the envelope
id deterministically (slash -> dash, trim to alphanumeric ends) and use it for
the session id, Redis keying, and verdict recovery. Unit-tested.

Signed-off-by: Paolo Dettori <[email protected]>
Dispatches a leaf, confirms the session persisted to Redis, force-kills the
harness pod mid-run, then re-invokes the same sessionId — asserting the session
resumes from the durable log and still produces its verdict (MVP spec §7 gate 7).
Live: 9/9 checks pass on kind sh-knative.

Signed-off-by: Paolo Dettori <[email protected]>

@pdettori pdettori left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Self-review (cannot APPROVE own PR). Clean, well-structured implementation of gate 7 (resumable leaf sessions). The resume logic is solid: fast-path verdict recovery from the durable log, checkpoint-based session restore, and belt-and-suspenders post-prompt recovery. toSessionId sanitization and verdictFromCustomEntry validation are both correct with good edge-case test coverage. The live smoke (claim 6) covers the full crash → resume → verdict path convincingly.

One nit below (non-blocking). Ready for external review.

Comment thread harness/src/run-leaf.ts
// Durable, resumable session keyed by the (sanitized) session id. BufferedRedisBackend drains
// writes continuously, so a mid-run crash preserves progress up to the last drained entry.
const sid = toSessionId(env.sessionId);
const store = new RedisSessionBackend<FileEntry>(config?.redisUrl ?? "redis://localhost:6379");

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

nit: store (RedisSessionBackend) is never explicitly disconnected after use. Under Knative (process dies after request) this is fine, but if realProduceVerdict were ever called in a long-lived process it would leak connections. Worth a store.disconnect?.() in the finally block if the backend exposes one — or a comment noting the Knative lifecycle assumption.

@pdettori
pdettori merged commit 4910e4d into main Jun 27, 2026
1 check passed
@pdettori
pdettori deleted the feat/mvp-leaf-resume branch June 27, 2026 14:41
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