← Back to Pull requests

feat(agentctl): add exec --json #286

Closed
unknown closed this pull request without merging on Nov 20, 2026 21:41 UTC
View on GitHub
unknown unknown commented on Nov 18, 2026 17:42 UTC
Author

First step toward #278: give scripts a stable way to read exec, so the human output of exec, replay and fork can be reworked next without breaking them.

What changes

  • agentctl exec --json prints the turn as JSON Lines, one protobuf ExecUpdate per line as it arrives, using the same encoding as the other --json output:

  • - The first line is a session, so a script learns the UID of a session exec created.
    - Each committed record follows as record, holding the same LogRecord a replay --json line holds.
    - A turn that pauses for approval exits 0 and ends with a session whose pending_approval names the call.
    - Streamed model chunks are left out. They aren't committed, the completed model call's EVENT_OUTPUT carries the full response, and nothing would read them. An opt-in flag can add them later; the docs tell scripts to ignore update kinds they don't expect.
    - If the stream fails, the lines already written stay and exec exits with the error. No retry.
  • Fix: a failed stdout write no longer goes unnoticed (it was discarded and exec exited 0). exec stops writing, reads the stream to its end so the turn isn't cut short, then exits with an error noting the records are committed. The error names the session to replay and says to use the same --server or --journal. If the stream also fails, both errors are reported, still naming the session, and nothing claims the turn finished. This applies to both text and JSON. With --json, exec also ignores SIGPIPE, so a closed pipe takes the same path: exec --json | head -1 gets its line and the turn still completes, local or remote, instead of being cancelled. Without --json, exec keeps exiting via SIGPIPE; TestResponseBrokenPipe asserts both.

  • The human output is unchanged.
  • Example

    From the built-in server with the default echo harness:

    $ agentctl exec --server= --journal /tmp/demo.db --input hello --json | jq -c '{session: .session.metadata.uid, seq: .record.seq, kind: .record.event.kind}'
    {"session":"sess-2be5b546e37dbb75f6e49d15","seq":null,"kind":null}
    {"session":null,"seq":"1","kind":"EVENT_EXECUTION_START"}
    {"session":null,"seq":"2","kind":"EVENT_INPUT"}
    {"session":null,"seq":"3","kind":"EVENT_MODEL_CALL"}
    {"session":null,"seq":"4","kind":"EVENT_OUTPUT"}
    {"session":null,"seq":"5","kind":"EVENT_END"}
    

    Each line is one ExecUpdate. The first and the EVENT_OUTPUT line, pretty-printed:

    {
      "session": {
        "metadata": {
          "project": "default",
          "uid": "sess-2be5b546e37dbb75f6e49d15",
          "create_time": "2026-11-18T17:50:35.157697716Z",
          "update_time": "2026-11-18T17:50:35.157697716Z"
        },
        "harness": "echo",
        "exec_state": "EXEC_PENDING",
        "compute_state": "COMPUTE_NONE",
        "deletion_state": "DELETION_ACTIVE"
      }
    }
    {
      "record": {
        "seq": "4",
        "prev_hash": "aa9a446a5c8271f940e83c4f0318d6e3c0bc2650f9216f10271eb641894f83d4",
        "content_hash": "debf5e797cd152d76b6259b05a1da99dea8ea1a7194dbeb3a887eead8b5eb349",
        "fence": "1",
        "event": {
          "execution_id": "7d75d32b130446a5",
          "kind": "EVENT_OUTPUT",
          "message": {
            "role": "assistant",
            "parts": [
              {
                "text": {
                  "text": "echo:hello"
                }
              }
            ]
          }
        }
      }
    }
    

    Getting just the reply:

    $ agentctl exec --server= --journal /tmp/demo.db --session "$SID" --input hi --json |
        jq -a 'select(.record.event.kind == "EVENT_OUTPUT") | [.record.event.message.parts[]?.text.text // empty] | join("")'
    "echo:hi"
    

    A failed stdout write, which used to exit 0:

    $ agentctl exec --server= --journal /tmp/demo.db --session "$SID" --input x --json > /dev/full; echo "exit=$?"
    agentctl: write exec output (the turn was not interrupted and its records are committed; read them with agentctl replay --session sess-bbbbc03934ce7881f1117a10 and the same --server or --journal): write /dev/stdout: no space left on device
    exit=1
    

    The quickstart's JSON section now includes a similar example.

    Tests and scripts moved off text parsing

    The tests and scripts that checked or parsed the human output of exec, replay or fork now read --json, so the next PR can change that output. What's left is display only, or relies on something the next PR keeps:

  • driver.sh's split-view highlight assumes one line per record; the next PR updates it with the layout.

  • examples/substrate-kind/smoke.sh reads the session <uid> header, which stays.

  • Checks on get, list, suspend and delete output stay as they are; those formats aren't changing.
  • What changed:

  • cmd/agentsessionsd/lifecycle_e2e_test.go (the daemon smoke test): turn outputs, END, the fork child and the replay prefix across delete and restart are now checked on decoded records with proto.Equal, instead of text regexes and byte prefixes.

  • cmd/agentctl/delete_test.go: the fork child comes from fork --json, and the audit replay check uses replay --json.

  • examples/agentsuite-substrate/demo.sh reads exec, replay and fork through --json, parsed with jq, which up.sh and the README already require. say still shows each turn, via replay --from. Every read consumes all its input, because a jq first(...) that stops early could end replay with SIGPIPE under pipefail.

  • examples/agentsuite-substrate/demo/driver.sh captures the journal with replay --json and parses it with python3, as it already parses its other JSON.

  • - Its step log gets one seq kind line per record instead of the JSON, which holds provider data (such as opaque reasoning bytes) that its redaction cannot inspect.
    - The full JSON, and the session list it reads the fork child from, go to a private mktemp file that is deleted on exit, not to the shared work directory. Only the child's UID is logged, and the parent and child comparison keeps only content hashes.
    - Its checks now read the journal rather than the screen: the usage and hash chain (chapter 6), that the replay appended nothing (chapter 8), and the fork child's own answer and input, plus the shared prefix compared by content_hash (chapter 9).
    - The fork child's UID comes from list --json, matched by parent and name.
  • examples/agentsuite-substrate/demo/trace-turn.sh: the model-call hop reads a JSON journal kept outside the saved trace directory. The human journal.log stays for reading.

  • examples/agentsuite-substrate/scripts_test.go: the fake agentctl behind the demo.sh tests now prints JSON Lines for --json.

  • hack/demos/kubernetes-tour.sh: the tour shows SID=$(agentctl create ...) before its first exec, and gets the fork child from list --json. The landing-page cast is unchanged until it is re-recorded.
  • Docs and changelog

  • docs/quickstart.md: an exec row in the JSON table, a jq example, and how pauses, chunks and failures appear. It also says the human output of exec, replay and fork can change.

  • docs/approval-gated-tool.md: how a script detects the approval pause.

  • Both docs' jq examples set pipefail, so a failed agentctl fails the pipeline, not just jq.

  • The quickstart notes that exec --json needs a build from main until the next release; v0.4.0 has no exec --json.

  • spec.md §1.2 defines the exec --json framing: the first session frame, committed records, the approval-pause frame, deltas left out, and stream and write failures. It also says the human output of exec, replay and fork isn't a stable contract.

  • Changelog entries: Added (exec --json), Compatibility (human output will change; use --json), Fixed (stdout write errors).
  • Not in this PR

  • The compact human layout, which comes next.

  • Color, which follows after that.
  • Testing

  • gofmt, go build ./..., go vet ./..., golangci-lint (changed packages), go test -race -shuffle=on ./cmd/agentctl ./cmd/agentsessionsd ./client, the examples/agentsuite-substrate module's vet, tests and golangci-lint, hack/check-doc-links.py, go run ./hack/doccheck, go run ./hack/changelog check.

  • New tests:

  • - TestExecJSON: lines round-trip to the sent updates, with and without --session; deltas are left out; escaped control and format characters; the approval-pause frame; a stream error.
    - TestExecWriteError: text and JSON modes.
  • Ran exec --json against the built-in server.

  • The new demo.sh, driver.sh, trace-turn.sh and tour helpers and checks were exercised against real journals from the built-in server, including fork, multi-line text and checks that must fail. I could not run demo.sh, driver.sh or the tour end to end, because they need substrate or a cluster.
  • Diff: 650 lines (+487 / -163) across 8 files Reviewer: aramase Review duration: 3119m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #286"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%