Skip to content

Task 6: gated Kind smoke for the leaf-session contract + live-path fixes - #10

Merged
pdettori merged 6 commits into
mainfrom
feat/mvp-leaf-task6
Jun 27, 2026
Merged

pdettori merged 6 commits into
mainfrom
feat/mvp-leaf-task6

Conversation

@pdettori

@pdettori pdettori commented Jun 27, 2026 •

Copy link
Copy Markdown
Member

Summary

Completes Task 6 of the MVP leaf-session invocation contract — the gated Kind smoke proving the harness can serve as a leaf-session backend for an external orchestrator — and closes the deferred live verifications from the plan (realProduceVerdict wiring, per-call model boundary, and sandbox tool execution).

Plan: docs/superpowers/plans/2026-06-26-mvp-leaf-session-contract.md · Spec: docs/specs/2026-06-26-mvp-leaf-session-contract-design.md

What's here

Task 6 artifacts

  • deploy/knative/leaf-pvc.yaml — shared leaf-work PVC (RWO; single-node Kind) for inputs/results.
  • deploy/knative/leaf-orchestrator.yaml — orchestrator stand-in pod mounting /work (also pins the WaitForFirstConsumer PVC).
  • deploy/knative/service.yaml — /work PVC mount on the harness service.
  • deploy/knative/leaf-smoke.sh — gated (LEAF_LIVE_SMOKE=1) driver covering the §7 gates plus a workspace-isolation gate.
  • deploy/knative/setup-kind.sh — enables the two Knative persistent-volume feature flags so the prereq is reproducible.

Live-path / correctness fixes (the deferred verifications)

  1. fix(harness): apply LLM gateway auth in runLeaf — realProduceVerdict ignored the gateway base URL + Bearer auth runTurn applies; extracted a shared, unit-tested applyModelGateway() helper used by both.
  2. fix(harness): disable gateway-incompatible tool compat flags — tool-bearing requests 400'd on the litellm gateway (tools.0.custom.eager_input_streaming: Extra inputs are not permitted). applyModelGateway now disables supportsEagerToolInputStreaming / supportsCacheControlOnTools / supportsLongCacheRetention when routing to a gateway. Also hardens runTurn.
  3. fix(harness): route leaf agent tools through the sandbox pod — the merged Task 3 runLeaf registered only submitVerdictExtension, so the agent's read/grep/bash/edit/write were Pi's native tools and ran inside the credentialed harness pod — violating spec §3 and the brain/hands split. Added k8sSandboxExtension() so those tools route into KAGENTI_SANDBOX_POD; submit_verdict is the only harness-side tool. Session cwd is decoupled from workspaceRef (a path inside the sandbox), and buildLeafPrompt hands the agent the absolute sandbox path.

Volume model (spec §5, brain/hands split)

  • inputs_ref / result_ref → harness-mounted /work PVC (trusted Node fs I/O the harness performs).
  • workspace_ref (the repo) → only in sandbox-0 at /workspace/<run>/repo; the agent reads it via the sandbox tools. The repo is never placed on /work, so a correct verdict is only possible if the agent read the file in the sandbox.

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

LEAF_LIVE_SMOKE=1 bash deploy/knative/leaf-smoke.sh → 8/8 checks pass, 0 fail:

Claim Result
0 — workspace isolation repo present in sandbox-0, absent on the harness /work volume
1 — fan-out, N parallel 3 leaves done; scaled out to 3 concurrent pods
2 — structured output + sandbox proof verdicts schema-valid and semantically correct (i1=FLAGGED, i2/i3=CLEAR) — only possible by reading the sandbox file
3 — parameterized model routes bogus model → failed, valid → done
4 — structured-output failure terminal failed, no invalid result_ref written
5 — retry / idempotency idempotent re-invoke rewrote a valid verdict
6 — scale-to-zero service scaled to zero when idle

Gate 7 (resume via M5 checkpoint) is the documented stretch — not separately built here.

Unit tests: full harness suite green (51 passed, 2 live-gated skipped), including new applyModelGateway coverage.

Assisted-By: Claude Code

Paolo Dettori added 5 commits June 26, 2026 22:23
…Gateway

realProduceVerdict used the base pi-ai model directly, ignoring the gateway
base URL + Bearer auth that runTurn applies. Against a gateway-backed
llm-credentials secret (base-url + auth-token), leaf model calls would hit the
wrong endpoint. Extract runTurn's gateway transform into a shared, unit-tested
applyModelGateway() helper and use it in both runTurn and runLeaf.

Signed-off-by: Paolo Dettori <[email protected]>
Anthropic-compatible gateways (litellm) reject the per-tool extras the direct
Anthropic API accepts. Tool-bearing leaf requests failed with
'tools.0.custom.eager_input_streaming: Extra inputs are not permitted' because
getAnthropicCompat defaults supportsEagerToolInputStreaming to true for the
anthropic provider. When applyModelGateway routes to a gateway baseUrl, disable
supportsEagerToolInputStreaming / supportsCacheControlOnTools /
supportsLongCacheRetention so convertTools() omits those fields. Verified live:
the leaf agent now reads the workspace and submits a valid verdict.

Signed-off-by: Paolo Dettori <[email protected]>
…age, scale-to-zero

Task 6 of the MVP leaf-session contract. Adds the shared leaf-work PVC, the
orchestrator stand-in pod, the /work mount on the harness service, and the gated
leaf-smoke.sh driver. setup-kind.sh now enables the Knative persistent-volume
feature flags. Verified live on kind sh-knative: MVP spec §7 gates 1-6 all PASS
(7/7 checks) — 3 parallel leaves each writing a schema-valid verdict, per-call
model routing, structured-output enforcement, idempotent retry, coverage audit,
and scale-to-zero.

Signed-off-by: Paolo Dettori <[email protected]>
runLeaf registered only submitVerdictExtension, so the agent's read/grep/bash/edit/
write were Pi's native tools and executed inside the credentialed harness pod —
violating spec §3 (tools run in the M2/M3 sandbox) and the brain/hands split.
Add k8sSandboxExtension() so those tools route into KAGENTI_SANDBOX_POD; the only
harness-side tool is submit_verdict. Session cwd is decoupled from workspaceRef
(a path inside the sandbox), and buildLeafPrompt now hands the agent the absolute
sandbox path to read. Verified live: repo seeded only in sandbox-0, agent produces
correct verdicts, nothing executes in the harness pod.

Signed-off-by: Paolo Dettori <[email protected]>
Rework leaf-smoke.sh for the brain/hands volume split: inputs/results on the
harness /work PVC, the repo seeded ONLY into sandbox-0:/workspace/<run>/repo.
Adds Claim 0 (repo present in sandbox, absent on the harness volume) and tightens
Claim 2 to require semantically-correct verdicts (i1=FLAGGED, i2/i3=CLEAR) — only
achievable by reading the file in the sandbox. Live: 8/8 checks pass on kind.

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: clean, well-structured PR. The applyModelGateway extraction eliminates real duplication and the brain/hands split (sandbox tool routing) is a meaningful security improvement — agent tools no longer execute in the credentialed harness pod. Shell scripts are solid (set -euo pipefail, jq --arg for safe interpolation, proper gate checks). YAML volume wiring is consistent across all manifests.

Commits: 5, all DCO signed ✓
CI: passing

No must-fix issues. A few suggestions below for future hardening.

Comment thread harness/src/run-turn.ts
config?: Pick<TurnConfig, "anthropicBaseUrl" | "anthropicAuthToken">,
): M {
const authToken = config?.anthropicAuthToken ?? process.env.ANTHROPIC_AUTH_TOKEN;
if (authToken && !process.env.ANTHROPIC_API_KEY) {

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.

suggestion: ?? process.env.ANTHROPIC_AUTH_TOKEN — the ?? means an explicit empty-string config value suppresses the env-var fallback (unlike ||). If config could ever have "" as a "not set" sentinel, || would be more defensive. Not blocking — just flagging the semantic difference.

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.

Done in 88e860d — switched authToken/baseUrl resolution from ?? to || so an empty-string config value falls back to the env var. Added a comment marking "" as the not-set sentinel, plus an empty-string-fallback test.

Comment thread harness/src/run-turn.ts
const authToken = config?.anthropicAuthToken ?? process.env.ANTHROPIC_AUTH_TOKEN;
if (authToken && !process.env.ANTHROPIC_API_KEY) {
process.env.ANTHROPIC_API_KEY = authToken;
}

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.

suggestion: This process.env mutation was already in runTurn, but now runs from two call sites (here via runLeaf + the runTurn call below). Consider a brief comment noting this is intentional (Pi SDK reads it at invocation time) so a future reader doesn't "clean it up".

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.

Done in 88e860d — added a comment marking the ANTHROPIC_API_KEY env mutation as intentional (pi-ai reads it at invocation time) and noting it now runs from both runTurn and runLeaf, so it should not be refactored into a local.

const m = applyModelGateway(baseModel, { anthropicAuthToken: "tok-only" }) as any;
expect(m.compat).toBeUndefined();
});
});

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.

suggestion: Consider an additional edge case — applyModelGateway(baseModel, { anthropicBaseUrl: "https://gw" }) with no token. This validates the "public gateway" path (baseUrl applied, no Authorization header set, compat flags still disabled).

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.

Done in 88e860d — added the token-less public-gateway case (baseUrl applied, compat disabled, no Authorization header, original headers untouched). Suite now 8 tests.

# It is long-lived so it pins the WaitForFirstConsumer PVC to the node.
apiVersion: v1
kind: Pod
metadata:

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: alpine:3.20 not pinned to a digest — fine for a local smoke test, but worth a comment if this ever gets promoted beyond Kind.

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.

Done in 88e860d — added a comment noting alpine:3.20 is a tag (fine for the local Kind smoke) and to pin a digest if this is ever promoted beyond Kind.

… docs

- applyModelGateway: use || (not ??) for authToken/baseUrl so an empty-string
  config value falls back to env instead of suppressing it.
- Comment the intentional ANTHROPIC_API_KEY env mutation (read by pi-ai at
  invocation time; now called from runTurn and runLeaf) so it is not refactored away.
- Add token-less public-gateway and empty-string-fallback test cases (8 total).
- Note the unpinned alpine:3.20 orchestrator image (pin to a digest if promoted
  beyond Kind).

Signed-off-by: Paolo Dettori <[email protected]>
@pdettori
pdettori merged commit 34908c1 into main Jun 27, 2026
1 check passed
@pdettori
pdettori deleted the feat/mvp-leaf-task6 branch June 30, 2026 19:40
cwiklik added a commit to cwiklik/moca that referenced this pull request Nov 10, 2026
…state it

Review suggestion on f206e8b (clawgenti, turn-handlers.test.ts:109), and it
is right: the coupling test passed `newId: () => randomUUID()` itself, so it
restated the production wiring rather than exercising it. Switch main.ts:523
to nanoid, ulid or a prefixed id and that test still passes while every real
claim 400s -- the dead-letter failure its own comment describes, and a direct
contradiction of "the only test that fails when it breaks".

The production id factory is now one exported binding, `defaultNewId` in
handler-util.ts, following `defaultAuditLog` and `defaultRunKubectl` -- the
shape this repo already uses for a production default of an injectable CpDeps
field. `depsFromEnv` wires it (the only production CpDeps producer; grepped
`packages` and `harness` for another `newId:`), and the three tests that
depend on the id's SHAPE inject the same binding instead of rebuilding it:
turn-handlers (the coupling test), turn-e2e (claims every turn it submits),
and turn-record (`accepts exactly what submitTurn mints`).

handler-util.ts rather than beside MINTED_TURN_ID_RE, because `newId` has
three shape-dependent consumers and only one is turns: session ids are their
own leafSessionId sanitisation (gap rossoctl#10), and `sandboxId` needs 26 random hex
and refuses a shorter source. Naming it `newTurnUuid` in turn-record.ts would
have put the session and sandbox contract inside the turn module.

Verified by mutation: `defaultNewId` -> `nid-${randomUUID()}` fails 8 tests --
the coupling test, all six turn-e2e tests, and the turn-record unit test.
Before this commit the same mutation left every one of them green, which is
exactly the gap the suggestion identified.

Control-plane suite: 1158 passed, 3 skipped against real Redis, no Errors.
tsc and prettier clean.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: cwiklik <[email protected]>
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