Skip to content

fix(pi): wait for terminal assistant response in RPC mode#118

Merged
roninjin10 merged 7 commits intocodeplaneapp:mainfrom
SamuelLHuber:fix/pi-rpc-agent-trace
Mar 27, 2026
Merged

fix(pi): wait for terminal assistant response in RPC mode#118
roninjin10 merged 7 commits intocodeplaneapp:mainfrom
SamuelLHuber:fix/pi-rpc-agent-trace

Conversation

@SamuelLHuber
Copy link
Copy Markdown
Contributor

@SamuelLHuber SamuelLHuber commented Mar 26, 2026

Problem

When PiAgent runs in RPC mode, Smithers could finalize the session on the first assistant turn_end even when PI was still continuing internally through tool use and subsequent turns.

Observed behavior

  • PI sessions that used tools could appear to finish early
  • long-running implementation tasks could look idle, truncated, or schema-only
  • Smithers could miss the true terminal assistant response

This made PI-backed tasks unreliable in the exact cases where RPC mode is most useful: longer, tool-using sessions.

Fix

  • update PI RPC completion handling so Smithers does not finalize on assistant turn_end events whose stopReason is toolUse
  • continue listening until the real terminal assistant answer arrives
  • finalize from the later terminal turn or agent_end
  • preserve existing behavior for simpler single-turn PI sessions

Docs / samples

  • add a minimal runnable sample at examples/pi-hello-world.tsx
  • add a second runnable sample at examples/pi-tools-workflow.tsx that uses PI tool calls (read and bash)
  • add a brief PI docs pointer showing how to run the sample and how to discover available PI models (models.json / pi --list-models)

Why this approach

PI tool-use sessions are multi-turn conversations. Treating the first assistant turn_end as final is too eager. The minimal safe fix is to keep the RPC session open across tool-use turns and only finalize once PI reaches a true terminal assistant state.

Validation

  • bun test tests/pi-support.test.ts
  • bun run cli run examples/pi-hello-world.tsx
  • bun run cli run examples/pi-tools-workflow.tsx

Added focused coverage for:

  • standard PI RPC prompt/response flow
  • RPC extension UI requests
  • tool-use RPC sessions that continue past an intermediate turn_end and only finish on the final assistant answer
  • JSON mode extraction behavior already covered by existing PI tests

Observed sample results:

  • examples/pi-hello-world.tsx returned {"message":"hello world"}
  • examples/pi-tools-workflow.tsx successfully used tools and returned the expected structured output, including:
    • phrase: "saffron-orbit-lantern"
    • lineCount: 3
    • cwdBasename: "examples"

Scope

This PR intentionally contains only:

  • the minimal PI RPC completion fix
  • two minimal PI runnable samples
  • a brief PI docs note

No trace capture or observability changes are included in this branch.

🤖 Generated with pi / smithers

…apture

Pi was working at the CLI level, but Smithers was finalizing RPC sessions on the
first assistant turn_end. That is no longer correct when Pi emits a tool-use
turn followed by additional turns and a final assistant answer. The result was
that tool-using Pi sessions were truncated before the real final response, which
made long-running implementation tasks look idle or schema-only even when Pi was
continuing internally.

This change fixes the RPC completion logic so Smithers waits past tool-use turns
and can finalize from the true terminal assistant state. It also adds the
canonical Pi-first trace event model, persistence path, and tests needed to make
that trace capture observable and durable.
@SamuelLHuber SamuelLHuber marked this pull request as draft March 26, 2026 20:22
@SamuelLHuber SamuelLHuber changed the title fix(pi): finalize RPC sessions correctly and add canonical trace capture fix(pi): wait for terminal assistant response in RPC mode Mar 26, 2026
@SamuelLHuber SamuelLHuber marked this pull request as ready for review March 26, 2026 20:36
@roninjin10 roninjin10 merged commit 4f1a891 into codeplaneapp:main Mar 27, 2026
1 of 2 checks passed
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.

2 participants