-
Notifications
You must be signed in to change notification settings - Fork 30
fix(pi): separate typed final answers from activity text #547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lidge-jun
wants to merge
14
commits into
codex/native-activity-02
Choose a base branch
from
codex/native-activity-pi-finality
base: codex/native-activity-02
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7426296
docs: record Pi finality repair roadmap
lidge-jun 360e2a8
fix(pi): select typed final response at session settlement
lidge-jun 9d14a20
fix(pi): hand off finality and salvage before lifecycle settlement
lidge-jun 4997685
docs: record Pi finality verification evidence
lidge-jun c87e5be
test(pi): cover user stop and settled continuation boundaries
lidge-jun d9f464e
fix(pi): preserve failure status and isolate exit observers
lidge-jun 897e3d3
docs: attach final Pi review evidence
lidge-jun f3d1dc5
docs: align API inventory counts with inherited routes
lidge-jun 67100bd
docs: attach CI baseline repair evidence
lidge-jun 0d74bbd
docs: close Pi finality implementation record
lidge-jun 6f27ce3
merge: integrate published native parent into Pi finality
lidge-jun 5279a13
docs: align private cascade ancestry
lidge-jun 6577fcb
Merge commit 'ef1fb23bf125513fd9a595d46cded24c18107689' into codex/na…
lidge-jun fe7d683
docs: record Pi cascade verification and parent ancestry
lidge-jun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Submodule devlog
updated
from 3f2b77 to cd6d62
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| import type { RuntimeTurnOutcome } from '../../shared/runtime-contract.js'; | ||
| import { appendBoundedFullText } from '../events/fulltext-bound.js'; | ||
|
|
||
| type RecordValue = Record<string, unknown>; | ||
| const record = (value: unknown): RecordValue => value !== null && typeof value === 'object' && !Array.isArray(value) | ||
| ? value as RecordValue : {}; | ||
|
|
||
| function assistant(value: unknown): Omit<RuntimeTurnOutcome, 'partialText'> & { text: string | null } | null { | ||
| const message = record(value); | ||
| if (message['role'] !== 'assistant') return null; | ||
| const content = message['content']; | ||
| let text: string | null = null, tool = false, oversized = false, invalid = !Array.isArray(content); | ||
| for (const part of Array.isArray(content) ? content : []) { | ||
| const block = record(part); | ||
| if (block['type'] === 'toolCall') { tool = true; continue; } | ||
| if (block['type'] === 'thinking') continue; | ||
| if (block['type'] !== 'text' || typeof block['text'] !== 'string') { invalid = true; continue; } | ||
| if (block['text'] === '') { text ??= ''; continue; } | ||
| const bounded = appendBoundedFullText(text ?? '', block['text']); | ||
| text = bounded.text; oversized ||= bounded.truncated; | ||
| } | ||
| const reason = message['stopReason']; | ||
| const status = reason === 'aborted' ? 'stopped' | ||
| : !invalid && !oversized && (reason === 'stop' || reason === 'toolUse') ? 'done' : 'error'; | ||
| return { text, status, finalText: status === 'done' && reason === 'stop' && !tool ? text : null }; | ||
| } | ||
|
|
||
| /** agent_settled entered upstream RPC in 0.80.4; willRetry existed before it. */ | ||
| export function piSupportsSettled(version: string): boolean { | ||
| const match = /^(?:pi\s+)?v?(\d+)\.(\d+)\.(\d+)\s*$/.exec(version.trim()); | ||
| if (!match) return false; | ||
| const [, major, minor, patch] = match; | ||
| return Number(major) > 0 || Number(minor) > 80 || (Number(minor) === 80 && Number(patch) >= 4); | ||
| } | ||
|
|
||
| /** Per admitted RPC prompt; no journal, session IDs, raw snapshots or callbacks retained. */ | ||
| export class PiTurnAccumulator { | ||
| private partial = ''; | ||
| private current = ''; | ||
| private runText = ''; | ||
| private completed = 0; | ||
| private candidate: Omit<RuntimeTurnOutcome, 'partialText'> = { status: 'done', finalText: null }; | ||
| private ended = false; | ||
|
|
||
| constructor(private readonly settledProtocol: boolean) {} | ||
|
|
||
| private append(text: string): string { | ||
| const previous = this.partial.length; | ||
| this.partial = appendBoundedFullText(this.partial, text).text; | ||
| this.runText = appendBoundedFullText(this.runText, text).text; | ||
| this.current = appendBoundedFullText(this.current, text).text; | ||
| return text.slice(0, this.partial.length - previous); | ||
| } | ||
|
|
||
| private reconcile(text: string | null): string { | ||
| // Snapshots echo prior deltas. A changed snapshot still owns finalText, | ||
| // but must not rewrite or duplicate already accepted salvage bytes. | ||
| if (text === null || !text.startsWith(this.current)) return ''; | ||
| return this.append(text.slice(this.current.length)); | ||
| } | ||
|
|
||
| observe(value: unknown): { text: string; done: boolean } { | ||
| if (this.ended) return { text: '', done: false }; | ||
| const row = record(value); | ||
| let text = ''; | ||
| if (row['type'] === 'agent_start') { | ||
| this.current = ''; this.runText = ''; this.completed = 0; | ||
| this.candidate = { status: 'done', finalText: null }; | ||
| } else if (row['type'] === 'message_start' && record(row['message'])['role'] === 'assistant') { | ||
| this.current = ''; | ||
| this.candidate = { status: 'error', finalText: null }; | ||
| } else if (row['type'] === 'message_update') { | ||
| const event = record(row['assistantMessageEvent']); | ||
| const role = record(row['message'])['role']; | ||
| if ((role === undefined || role === 'assistant') && event['type'] === 'text_delta' && typeof event['delta'] === 'string') { | ||
| text = this.append(event['delta']); | ||
| } | ||
| } else if (row['type'] === 'message_end') { | ||
| const message = assistant(row['message']); | ||
| if (message) { | ||
| text = this.reconcile(message.text); | ||
| this.candidate = { status: message.status, finalText: message.finalText }; | ||
| this.completed++; | ||
| this.current = ''; | ||
| } | ||
| } else if (row['type'] === 'agent_end') { | ||
| text = this.terminal(row['messages']); | ||
| this.ended = !this.settledProtocol && row['willRetry'] !== true; | ||
| } else if (row['type'] === 'agent_settled') { | ||
| this.ended = true; | ||
| } | ||
| return { text, done: this.ended }; | ||
| } | ||
|
|
||
| private terminal(value: unknown): string { | ||
| if (!Array.isArray(value)) { | ||
| this.candidate = { status: this.candidate.status === 'stopped' ? 'stopped' : 'error', finalText: null }; | ||
| return ''; | ||
| } | ||
| let index = 0, added = ''; | ||
| // Without message_end boundaries, the aggregate snapshot echoes the | ||
| // low-level run's stream. Consume that prefix by offset, not text identity. | ||
| let streamed = this.completed === 0 ? this.runText : ''; | ||
| // An empty/tool-only terminal cannot erase an observed failure. Only | ||
| // an actual assistant snapshot can supply a newer completion status. | ||
| this.candidate = { status: this.candidate.status, finalText: null }; | ||
| for (const entry of value) { | ||
| const message = assistant(entry); | ||
| if (!message) continue; | ||
| this.candidate = { status: message.status, finalText: message.finalText }; | ||
| if (index++ < this.completed) continue; | ||
| if (this.completed === 0) { | ||
| const snapshot = message.text ?? ''; | ||
| const consumed = Math.min(streamed.length, snapshot.length); | ||
| this.current = streamed.slice(0, consumed); | ||
| streamed = streamed.slice(consumed); | ||
| } | ||
| added = appendBoundedFullText(added, this.reconcile(message.text)).text; | ||
| this.current = ''; | ||
| } | ||
| this.completed = index; | ||
| return added; | ||
| } | ||
|
|
||
| snapshot(status?: RuntimeTurnOutcome['status']): RuntimeTurnOutcome { | ||
| return { status: status ?? this.candidate.status, | ||
| finalText: status ? null : this.candidate.finalText, partialText: this.partial }; | ||
| } | ||
| } | ||
|
|
||
| /** Only this local carrier can supply failure outcome; arbitrary Error fields cannot. */ | ||
| export class PiRuntimeError extends Error { | ||
| readonly runtimeOutcome: RuntimeTurnOutcome; | ||
| constructor(cause: Error, outcome: RuntimeTurnOutcome) { | ||
| super(cause.message, { cause }); | ||
| this.name = 'PiRuntimeError'; | ||
| this.runtimeOutcome = { ...outcome }; | ||
| } | ||
| } | ||
|
|
||
| export function piFailureOutcome(error: unknown): RuntimeTurnOutcome | undefined { | ||
| if (!(error instanceof PiRuntimeError) || !Object.hasOwn(error, 'runtimeOutcome')) return undefined; | ||
| return { ...error.runtimeOutcome }; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whenever a Pi runtime is created,
usesPiSettled()synchronously launches<pi command> --versionwith a 15-second timeout;resolvePiCommand()may already have run its own probe immediately beforehand. With the npm-exec fallback or a slow/broken wrapper, each pooled-session creation—and every direct worker turn—can block the server event loop for seconds, delaying unrelated HTTP, SSE, and agent work. Cache the detected capability or perform this probe asynchronously rather than runningspawnSyncduring each spawn.Useful? React with 👍 / 👎.