Skip to content

Fix Pi RPC completion handling and multi-turn usage accounting #638

Closed

Description

@zzwong

Context

CR's Pi RPC adapter stops scanning at agent_end in internal/llmadapters/pi_rpc.go. Pi 1.0 documents agent_settled as the end of automatic work: retries and recovery can follow agent_end. Usage currently flows through overwrite-style mergeUsage, losing earlier completed turns.

Observed baseline and hypothesis

  • Environment: local Pi 1.0.0, isolated local mock provider; no paid model or Git-provider writes.
  • A mocked HTTP 503 produced agent_end with willRetry: true, then a successful retry and agent_settled. Replaying the captured stream through CR's scanner returned empty structured output rather than the successful JSON: 0/1 recovered answers.
  • A synthetic two-turn stream reported 100 + 200 input tokens; CR retained 200 rather than 300.
  • Existing Pi adapter tests, including installed-runtime extension preflight, pass. They do not cover these contracts.
  • Hypothesis: honoring session settlement and summing authoritative completed-message usage preserves successful recovery and accurate spend without changing review safety.

Acceptance

  • Add failing regression tests before production changes.
  • Consume through agent_settled, including retries/recovery; do not close stdin at a recoverable agent_end.
  • Preserve terminal provider error/abort diagnostics; reject truncated or unsettled streams rather than accepting stale/partial output.
  • Sum completed assistant-turn usage once, including cache/cost fields; do not add cumulative streaming updates or duplicate agent_end.messages snapshots.
  • Preserve tool allowlists, diff evidence, log caps, deadlines and process-group cancellation.
  • Update helper fixtures and document the supported completion contract/version implications.
  • Run targeted adapter tests, full Go suite and lint.

Success and rollback

Target: 1/1 recovered successful answer, exactly 300 input tokens for the two-turn fixture, no double counting or safety regression. Validation is local/synthetic, not a deployed outcome. Roll back by reverting the adapter change if runtime smoke tests regress completion or cancellation.

References

Pi RPC remains the integration. Native session persistence and Pi Durable are out of scope.

Activity

  1. zzwong commented on Oct 2, 2026

    @zzwong
    ContributorAuthor

    Draft implementation: #641. Local/mock-provider verification recovered the successful answer after a 503 retry (baseline 0/1, now 1/1), preserved permanent-failure diagnostics, and reported 300 input tokens for the 100 + 200 fixture. Supported one-hour cache usage is also preserved and summed. Targeted race tests, repeated settlement tests, full-suite rerun, and lint with Go 1.26.3 passed. This is local/synthetic evidence, not a deployed outcome; GitHub CI and the dedicated pinned-runtime gate (#639) remain pending. RPC is retained; session persistence and Durable remain deferred.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions