#636·Trellis

Pi subagent errors can inject the entire JSON event stream into the parent context

Author: xwellCreated Sep 24, 2026Updated Sep 24, 2026
Labelsbugpkg:cli

Bug Description

Summary

The generated Pi extension can treat a failed subagent run as successful and inject the child process's entire JSON event stream into the parent conversation.

This happens when the child pi --mode json process:

  1. Emits a final assistant message_end with errorMessage and no text content.
  2. Emits agent_end.
  3. Exits with status code 0.

The extension records errorMessage, but it does not mark the run as failed. The later agent_end event marks the run as succeeded. Because there is no final assistant text, formatPiOutput() falls back to returning the complete captured stdout.

For a long-running subagent, stdout contains every JSON event, including thinking deltas, tool calls, and tool results. This can add several megabytes to the parent model context in a single trellis_subagent result.

Observed impact

In one real session:

Metric Value
Parent context before subagent result 440,524 tokens
Child's last successful context size 116,163 tokens
Child error upstream stream closed before [DONE]
Child JSON events returned to parent 4,863
Returned tool-result text 3,503,321 characters
Parent compaction input after injection 1,316,355 tokens

The parent request then failed with:

Your input exceeds the context window of this model. Please adjust your input and try again.

The trellis_subagent result was recorded with isError: false, and its run details reported status: "succeeded" even though errorMessage was present.

Relevant code

Generated file:

.pi/extensions/trellis/index.ts

The same implementation is present in:

dist/templates/pi/extensions/trellis/index.ts.txt

The problematic flow appears to be:

  1. applyEvent() stores msg.errorMessage without setting r.status = "failed".
  2. The agent_end handler sets a pending/running run to "succeeded" without checking r.errorMessage.
  3. runPi() treats exit code 0 as success without checking state.errorMessage.
  4. formatPiOutput() returns the entire stdout when it cannot find assistant text:
typescript
return ft || stdout || stderr;

MAX_STDOUT is 8 MiB, so a large portion of the child event stream can enter the parent context.

Expected behavior

  • An assistant message containing errorMessage should mark the subagent run as failed.
  • agent_end should preserve the failed state.
  • A zero process exit code should not override a model-level error.
  • The tool result should contain a concise error message.
  • Raw JSON event stdout should never be returned as model-visible tool content.
  • Final subagent output should have a hard size limit.
  • The parent tool_result event should have isError: true.

Suggested fix

Conceptually:

typescript
if (typeof msg.errorMessage === "string") {
  r.errorMessage = msg.errorMessage;
  r.status = "failed";
}

The agent_end handler should only mark success when no error was recorded:

typescript
if (
  !r.errorMessage &&
  (r.status === "running" || r.status === "pending")
) {
  r.status = "succeeded";
}

The process close handler should check the parsed model state before accepting exit code 0:

typescript
if (state.errorMessage) {
  state.status = "failed";
  done({
    output: state.errorMessage,
    failed: true,
  });
  return;
}

formatPiOutput() should return a bounded diagnostic when no final assistant text exists:

typescript
return (
  ft ||
  stderr.trim() ||
  "Subagent completed without a final assistant response."
);

A separate final-output limit, such as 64 KiB, would prevent other unexpectedly large subagent results from exhausting the parent context.

Version check

The generated project file matches the unmodified Trellis 0.6.17 template hash.

I also inspected the published 0.7.0-beta.4 template. It still contains the same error-state and raw-stdout fallback logic.

Steps to Reproduce

The issue can be reproduced deterministically by replacing the child Pi executable with a small script through the existing TRELLIS_PI_CLI_JS override.

  1. Initialize a project with Pi support and confirm that .pi/extensions/trellis/index.ts is enabled.

  2. Create /tmp/fake-pi.js:

javascript
#!/usr/bin/env node

const usage = {
  input: 0,
  output: 0,
  cacheRead: 0,
  cacheWrite: 0,
  totalTokens: 0,
  cost: {
    input: 0,
    output: 0,
    cacheRead: 0,
    cacheWrite: 0,
    total: 0,
  },
};

console.log(JSON.stringify({ type: "agent_start" }));
console.log(JSON.stringify({ type: "turn_start" }));

// Simulate a large Pi JSON event stream without producing final assistant text.
for (let i = 0; i < 3000; i++) {
  console.log(
    JSON.stringify({
      type: "message_update",
      assistantMessageEvent: {
        type: "thinking_delta",
        delta: "x".repeat(1024),
      },
    }),
  );
}

console.log(
  JSON.stringify({
    type: "message_end",
    message: {
      role: "assistant",
      content: [],
      provider: "test",
      model: "test-model",
      stopReason: "error",
      errorMessage: "synthetic upstream failure",
      usage,
    },
  }),
);

console.log(JSON.stringify({ type: "agent_end" }));

// Pi can exit 0 even though the assistant message reports an inference error.
process.exit(0);
  1. Start the parent Pi session:
bash
cd /path/to/trellis-project
TRELLIS_PI_CLI_JS=/tmp/fake-pi.js pi
  1. Invoke trellis_subagent with any installed Trellis agent, for example:
Use trellis_subagent with agent trellis-check and prompt "reproduce child failure".
  1. Inspect the resulting tool message.

Actual result

  • The subagent run is reported as succeeded.
  • The tool result has isError: false.
  • The model-visible result contains the complete fake Pi JSON event stream.
  • The result is several megabytes despite the actual error being only synthetic upstream failure.

Expected result

  • The subagent run is reported as failed.
  • The tool result has isError: true.
  • The model-visible result contains only a concise error message.
  • Raw JSON event output stays internal to the extension.

Trellis Version

0.6.17

Node.js Version

v24.19.0

Operating System

Linux