Skip to content

Add human-gate (gate-while-idle, Archetype B) to the leaf-session backend - #14

Merged
pdettori merged 20 commits into
mainfrom
feat/human-gate
Jun 28, 2026
Merged

pdettori merged 20 commits into
mainfrom
feat/human-gate

Conversation

@pdettori

Copy link
Copy Markdown
Member

Summary

Archetype B — human-gate ("gate-while-idle") for the leaf-session backend, the last unbuilt archetype. A single leaf session can pause mid-pipeline awaiting an external human decision, park in durable state with the pod scaled to zero, and resume with the decision injected — across one or more gates — before a verdict. Additive: every existing path (sync /run-leaf, async enqueue, cron-dispatch, leaf-queue, KEDA ScaledJob, submit_verdict, the leaf-job runner) is unchanged; a leaf that never gates behaves exactly as today.

Spec: docs/specs/2026-06-28-human-gate-design.md · Plan: docs/superpowers/plans/2026-06-28-human-gate.md

How it works

  • Agent calls a request_approval({summary, proposed_action}) tool → durable gate-request entry (monotonic gateId) → runLeaf writes an awaiting_approval gate marker to <resultRef>.gate → session parks.
  • External approver writes a decisionRef file ({gateId, action: approve|reject|abort, feedback?}) and re-invokes the same sessionId (sync or async). runLeaf resumes from the durable log, records a gate-decision entry, and re-seeds a continuation prompt (approve/reject) or returns aborted.
  • gateId-keyed entries make multi-gate sequences, stale decisions, double-resume, and crash-mid-resume idempotent (effectively-once).
  • Hard gate: for require_approval items the submit_verdict tool is withheld until a gate decision exists, so the gate cannot be bypassed even if the model ignores the prompt.

Design decisions (spec §1): agent-declared gate with external decision/re-invocation (charter G1/G2); continuation-prompt resume (no tool-result injection); decisionRef file transport; no harness-side timeout (orchestrator owns deadlines); decision actions approve/reject/abort. Auto-approve mode is explicitly not built.

Verification

  • Unit (vitest): @sh/harness 115 passed (2 skipped), @sh/knative-server 29 passed, gate.ts 19 tests.
  • Live gate on Kind (deploy/knative/leaf-gate-smoke.sh, all 6 claims PASS): pause (marker, no verdict) → scale-to-zero while parked → resume-approve → idempotent re-invoke → reject (agent re-gated gateId 1 → approve → done; multi-gate) → abort.

Deferred follow-ups (non-blocking; final review confirmed none block merge)

  • processOne cosmetically labels paused/aborted as "failed" (only idle/retry drive the loop; ack is correct).
  • Restore dropped cwd/gateway invariant comments in realProduceVerdict.
  • Add an empty-proposed_action rejection test; shape-validate readGateMarker if external marker producers are ever added.

Assisted-By: Claude Code

Paolo Dettori added 20 commits June 28, 2026 12:36
Design for the last unbuilt leaf-session archetype: a single leaf
session pauses mid-pipeline awaiting an external human decision, parks
in durable state with the pod scaled to zero, and resumes with the
decision injected, preserving context across one or more gates.

Decisions: agent-declared gate via a request_approval tool with external
decision authority + re-invocation (charter G1/G2); continuation-prompt
resume (well-formed transcript, no Pi-internals dependency); decisionRef
file transport + re-invoke-by-sessionId trigger; no harness-side timeout
(park indefinitely). gateId-keyed durable gate-request/gate-decision
entries make multi-gate replay and double-resume idempotent. paused acks
the queue (parked work); runLeaf writes the gate marker. Reuses M4/M5 +
the async queue/marker/KEDA substrate unchanged.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
Match the codebase resume idiom: realProduceVerdict re-seeds via
session.prompt() on every invocation (openFromCheckpoint loads context
but does not auto-run a turn). The gate continuation is re-derived from
the durable gate-decision entry and re-seeded each resume, idempotent
exactly like the existing initial-prompt verdict resume. Replaces the
earlier 'replayed durable turn' wording.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
Bite-sized TDD tasks for Archetype B: gate.ts pure logic (types,
decision validation, computeGateState, decideSeed), request_approval
tool, gate-marker I/O, classifyOutcome paused/aborted, runLeaf gate
front-end + marker, status endpoint awaiting_approval, and a gated live
smoke. Each task ends with an independently testable deliverable; the
realProduceVerdict wiring and live smoke are verified on the Kind
sh-knative cluster per the existing convention.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
…rompt gate instruction

Signed-off-by: Paolo Dettori <[email protected]>
…pod-poll best-effort)

The idempotent re-invoke hits the verdict fast-path (M5 recovery, no
agent/model call), so the leaf-job pod Running window is sub-second and
a 5s poll may miss it. Document that pod observation is best-effort (a
hard-fail would be flaky); the real proof is the terminal-state-unchanged
assertions (done stable, verdict identical, exactly 1 result file).

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
The smoke seeded inputs (heredoc) and the decision file (pipe) through
oexec, which runs kubectl exec WITHOUT -i, so local stdin was never
forwarded to the pod and cat wrote empty files -> runLeaf returned
bad_inputs and the gate never fired. Add an oexec_i (-i) helper for the
four stdin-fed writes; non-stdin calls keep using oexec. Verified the
-i seeding writes the expected 82-byte inputs JSON into the pod.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
…ict instruction

In the require_approval turn buildLeafPrompt was instructing BOTH
request_approval AND submit_verdict, so the model (haiku) took the
simpler path and submitted a verdict directly, never gating. The gated
branch now instructs request_approval as the first-and-only action and
withholds the verdict instruction (delivered later by the approve
continuation). Unit test asserts the gated prompt omits the submit_verdict
token. Full harness suite 115(+2 skip) green.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
…gate)

Live testing showed the model (haiku) ignores the prompt and calls
submit_verdict directly in the pre-gate turn, bypassing the gate. Make
require_approval a hard guarantee: in realProduceVerdict the
submit_verdict extension is registered only when the agent has passed
at least one gate (a gate-decision exists, or is being applied this
turn). In the pre-gate turn the only structured-output tool is
request_approval, so the gate cannot be bypassed. Non-gated items are
unaffected (allowVerdict is true when require_approval is false).
Harness suite 115(+2 skip) green; typecheck clean.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
bash parses ${1:-{}} as ${1:-{} followed by a literal }, so when an
extra-json arg was passed (Claim 3+) the value got a trailing } and jq
--argjson rejected it as invalid JSON. Claim 1 only worked by
coincidence (unset -> {}). Use an explicit local default instead.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
@pdettori
pdettori merged commit 527ed1d into main Jun 28, 2026
1 check passed
@pdettori
pdettori deleted the feat/human-gate branch June 28, 2026 20:17
pdettori added a commit that referenced this pull request Jun 28, 2026
Address human-gate (#14) review follow-ups
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