Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/drivers/pi_agent_core_driver_sample_node.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Self-contained Pi coding-agent SDK driver example.
*
* Declare @earendil-works/pi-coding-agent@1.0.0 as a project dependency and
* Declare @earendil-works/pi-coding-agent@1.1.0 as a project dependency and
* install it in setup-steps before the agent runs. Select this file with
* engine.driver: .github/drivers/pi_agent_core_driver_sample_node.cjs.
*
Expand Down
32 changes: 31 additions & 1 deletion actions/setup/js/fixtures/pi_ci_stream.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,34 @@ const failure = [
{ type: "agent_settled" },
];

module.exports = { success, failure };
// Shape from Smoke Pi Sub Agents run 37735099783, agent artifact
// 11531640894/pi-streaming.jsonl. Names, IDs, timestamps and content replaced.
// This historical wrapper has no native invocation or parent tool identity.
const childObservations = [
{ type: "gh_aw_subagent_dispatch", agent: "sanitized-reader", requested_model: "gpt-5-mini", resolved_model: "gpt-5-mini" },
{
type: "gh_aw_subagent_event",
agent: "sanitized-reader",
event: {
type: "message_end",
message: {
role: "assistant",
content: [
{ type: "thinking", thinking: "Sanitized reasoning.", thinkingSignature: "sanitized-signature" },
{ type: "text", text: "Sanitized child answer.", textSignature: "sanitized-signature" },
],
api: "openai-responses",
provider: "aw-gateway",
model: "gpt-5-mini",
responseId: "sanitized-child-response",
timestamp: 6,
rawStopReason: "completed",
stopReason: "stop",
thinkingLevel: "minimal",
usage: { input: 3085, output: 136, cacheRead: 0, cacheWrite: 0, reasoning: 64, totalTokens: 3221, cost: { input: 0.00077125, output: 0.000272, cacheRead: 0, cacheWrite: 0, total: 0.00104325 } },
},
},
},
];

module.exports = { success, failure, childObservations };
26 changes: 6 additions & 20 deletions actions/setup/js/parse_pi_log.cjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
// @ts-check
/// <reference types="@actions/github-script" />

const {
createEngineLogParser,
generateConversationMarkdown,
generateInformationSection,
buildStepSummaryDetailsSection,
formatInitializationSummary,
formatToolUse,
convertLegacyLogEntriesToCopilotEvents,
parseLogEntries,
} = require("./log_parser_shared.cjs");
const { createEngineLogParser, generateConversationMarkdown, generateInformationSection, buildStepSummaryDetailsSection, formatInitializationSummary, formatToolUse, parseLogEntries } = require("./log_parser_shared.cjs");
const { transformFlatSessionEntries, projectSessionResult, selectSessionResult, reconcileSessionUsage } = require("./agent_session.cjs");
const { transformPiV3Entries, computePiV3Stats, normalizePiObservations } = require("./pi_session.cjs");

Expand All @@ -22,12 +13,8 @@ const main = createEngineLogParser({

/**
* Parse Pi CLI JSONL streaming log output and format as markdown.
* Pi CLI emits one JSON object per line (JSONL) with typed events:
* - type "init": session initialization with model and session_id
* - type "assistant": agent message content (delta:true for streaming chunks)
* - type "tool_use": tool invocations with tool_name, tool_id, and parameters
* - type "tool_result": tool responses with tool_id, status, and output
* - type "result": final stats with token usage and duration
* Normalize native streaming events and historical flat records directly into
* canonical session events, then render the shared conversation and usage view.
* @param {string} logContent - The raw log content to parse
* @returns {{markdown: string, logEntries: Array, mcpFailures: Array<string>, maxTurnsHit: boolean}} Parsed log data
*/
Expand Down Expand Up @@ -58,10 +45,8 @@ function parsePiLog(logContent) {
// Detect which schema this log uses and transform accordingly so the step summary
// renders the conversation and token stats for both.
const useV3Schema = isPiV3Schema(rawEntries);
const logEntries = useV3Schema ? transformPiV3Entries(rawEntries) : transformPiEntries(rawEntries);

const stats = useV3Schema ? computePiV3Stats(rawEntries) : null;
const canonicalLogEntries = normalizePiObservations(convertLegacyLogEntriesToCopilotEvents(logEntries, { sourceEngine: "pi" }));
const canonicalLogEntries = transformPiV3Entries(rawEntries);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Routing every Pi log through transformPiV3Entries regresses legacy flat logs because we stop normalizing them as a batch, so consecutive assistant/reasoning deltas no longer coalesce into the single message the renderer expects.

💡 Why this blocks merge

transformFlatSessionEntries() intentionally carries state across the whole record list (entries.at(-1), anonymous tool-start pairing, etc.). The new fallback in transformPiV3Entries() invokes transformFlatSessionEntries([raw]) one record at a time, so old flat traces lose that cross-record behavior. A two-line legacy stream now produces two assistant events instead of one merged message, which fragments historical transcripts and breaks the existing merged-delta coverage in parse_pi_log.test.cjs. Please keep the batched flat-log path for !useV3Schema, or batch legacy records before feeding them through the v3 normalizer.

if (stats) {
const terminal = [...canonicalLogEntries].reverse().find(event => event.type === "session.result");
const selected = selectSessionResult(canonicalLogEntries);
Expand All @@ -74,7 +59,7 @@ function parsePiLog(logContent) {
};
if (terminal) {
Object.assign(terminal.data, data);
if (stats.errors.length || stats.exposedErrors) terminal.data.errors = [...(terminal.data.errors ?? []), ...stats.errors];
if (stats.errors.length || stats.exposedErrors) terminal.data.errors = [...(Array.isArray(terminal.data.errors) ? terminal.data.errors : []), ...stats.errors];
} else {
canonicalLogEntries.push({ type: "session.result", data: { ...data, errors: stats.errors.length || stats.exposedErrors ? stats.errors : undefined } });
}
Expand Down Expand Up @@ -143,6 +128,7 @@ function isPiV3Schema(rawEntries) {
e.type === "message_update" ||
e.type === "message_start" ||
e.type === "message_end" ||
(e.type === "message" && e.message && typeof e.message.role === "string") ||
(e.type === "session" && typeof e.version === "number")
) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion actions/setup/js/pi_legacy_session.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe("historical Pi observations", () => {
{ type: "pi.subagent_event", data: { agent: "reader", invocation_id: "child", event: { message: { role: "assistant", model: "gpt-5.4-mini", usage: { input: 5, output: 2 } } } } },
{ type: "pi.subagent_result", data: { agent: "reader", invocation_id: "child", outcome: "failed", error_code: "SUBAGENT_MODEL_UNAVAILABLE" } },
]);
expect(events.map(event => event.type)).toEqual(["subagent.started", "subagent.configured", "subagent.request", "subagent.failed"]);
expect(events.map(event => event.type)).toEqual(["subagent.started", "subagent.configured", "subagent.request", "session.info", "subagent.failed"]);
expect(events.every(event => event.agentId === "child")).toBe(true);
expect(byType(events, "subagent.request")[0].data).toMatchObject({ inputTokens: 5, outputTokens: 2 });
expect(byType(events, "subagent.failed")[0].data).toMatchObject({ errorCode: "SUBAGENT_MODEL_UNAVAILABLE" });
Expand Down
60 changes: 53 additions & 7 deletions actions/setup/js/pi_session.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ function transformPiV3Entries(records, mapLegacyEvent = event => event) {
const events = [];
const subagentInvocations = new Map();
const subagentInvocationCounts = new Map();
const subagentRecords = new Map();
const subagentEvents = new Map();
const subagentRequests = new Map();
const legacyCallIds = new Map();
const executionEnds = new Set(records.filter(r => r.type === "tool_execution_end" && r.toolCallId !== undefined).map(r => r.toolCallId));
const calls = new Map();
Expand Down Expand Up @@ -202,7 +205,10 @@ function transformPiV3Entries(records, mapLegacyEvent = event => event) {
const value = usage[source] ?? usage[target];
if (Number.isSafeInteger(value) && value >= 0) data[target] = value;
}
emit(raw, "subagent.request", data, { agentId: invocationId });
const key = JSON.stringify([invocationId, messageIdentity(message) ?? message]);
const previous = subagentRequests.get(key);
if (previous) Object.assign(previous.data, data);
else subagentRequests.set(key, emit(raw, "subagent.request", data, { agentId: invocationId }));
Comment on lines +208 to +211
};

const emitText = (raw, state, kind, index, text, delta, role = "assistant", metadata = {}) => {
Expand Down Expand Up @@ -301,7 +307,14 @@ function transformPiV3Entries(records, mapLegacyEvent = event => event) {
};

const emitMessage = (raw, message, fallback) => {
if (!message || !["assistant", "user", "toolResult"].includes(message.role)) return;
if (!message || typeof message.role !== "string") return;
if (!["assistant", "user", "toolResult"].includes(message.role)) {
if (!snapshots.some(snapshot => isDeepStrictEqual(snapshot.message, message))) {
snapshots.push({ message: structuredClone(message), state: fallback });
emit(raw, "session.info", { sourceType: "message_snapshot", role: message.role, content: message });
}
return;
}
if (message.role === "toolResult") {
emitCompletion(raw, message);
return;
Expand Down Expand Up @@ -389,7 +402,33 @@ function transformPiV3Entries(records, mapLegacyEvent = event => event) {
emit(raw, "subagent.configured", { invocationId, model: raw.resolved_model ?? raw.resolvedModel }, { agentId: invocationId });
}
} else if (raw.type === "gh_aw_subagent_event") {
emitSubagentRequest(raw, invocationIdentity(raw, false));
const invocationId = invocationIdentity(raw, false);
emitSubagentRequest(raw, invocationId);
if (raw.event && typeof raw.event === "object" && !Array.isArray(raw.event) && typeof raw.event.type === "string") {
const childRecords = subagentRecords.get(invocationId) ?? [];
childRecords.push({
...structuredClone(raw.event),
...(raw.provenance !== undefined ? { provenance: structuredClone(raw.provenance) } : {}),
});
subagentRecords.set(invocationId, childRecords);
const previous = subagentEvents.get(invocationId) ?? [];
// Replay only this child's observations so repeated snapshots reconcile
// without sharing tool/message identities with the parent or other children.
const childEvents = transformPiV3Entries(childRecords).map(event => ({
Comment on lines +414 to +417
...event,
agentId: event.agentId ?? invocationId,
data: { ...event.data, agentId: event.data.agentId ?? invocationId },
}));
Comment on lines +417 to +421
for (let index = 0; index < previous.length; index++) {
const position = events.indexOf(previous[index]);
if (position >= 0) {
if (index < childEvents.length) events[position] = childEvents[index];
else events.splice(position, 1);
}
}
events.push(...childEvents.slice(previous.length));
subagentEvents.set(invocationId, childEvents);
} else emit(raw, "session.info", { sourceType: raw.type, content: raw.event }, { agentId: invocationId });
} else if (raw.type === "gh_aw_subagent_result") {
const invocationId = invocationIdentity(raw, false);
const failed = raw.outcome !== "completed";
Expand Down Expand Up @@ -431,8 +470,9 @@ function transformPiV3Entries(records, mapLegacyEvent = event => event) {
if (call) call.data.argumentText = (call.data.argumentText ?? "") + update.delta;
else emit(raw, "tool.execution_update", { input: update.delta, delta: true, partial: true, contentIndex: update.contentIndex });
} else if (update?.type === "done" || update?.type === "error") emitMessage(raw, update.message ?? update.error, assistantState);
else if (update && !["start", "text_start", "thinking_start"].includes(update.type)) emit(raw, "assistant.message_update", { ...update, usage: raw.usage });
} else if (raw.type === "message_end") emitMessage(raw, raw.message, raw.message?.role === "assistant" ? assistantState : activeState);
else if (update && !["start", "text_start", "thinking_start"].includes(update.type)) emit(raw, "assistant.message_update", { ...update, content: update, usage: raw.usage });
} else if (raw.type === "message" && raw.message) emitMessage(raw, raw.message, newMessageState());
else if (raw.type === "message_end") emitMessage(raw, raw.message, raw.message?.role === "assistant" ? assistantState : activeState);
else if (raw.type === "turn_end") {
emitMessage(raw, raw.message, assistantState);
for (const result of raw.toolResults ?? []) emitCompletion(raw, result);
Expand Down Expand Up @@ -470,7 +510,11 @@ function transformPiV3Entries(records, mapLegacyEvent = event => event) {
emitMessage(raw, message, index >= 0 ? snapshots[index].state : partialAssistant && position === lastAssistant ? assistantState : newMessageState());
}
} else if (raw.type === "error") emit(raw, "session.error", { error: raw.error ?? raw.message });
else events.push(...normalizeAgentSession(transformFlatSessionEntries([raw]), { sourceEngine: "pi" }));
else {
const flat = normalizeAgentSession(transformFlatSessionEntries([raw]), { sourceEngine: "pi" });
if (flat.length) events.push(...flat);
else if (typeof raw.type === "string" && raw.type !== "") emit(raw, "session.info", { sourceType: raw.type, content: raw });
}
}
return events;
}
Expand Down Expand Up @@ -566,10 +610,12 @@ function computePiV3Stats(records) {
lastWasMessageUpdate = false;
} else if (raw.type === "message_update" && ["done", "error"].includes(raw.assistantMessageEvent?.type)) {
addMessage(raw, raw.assistantMessageEvent.message ?? raw.assistantMessageEvent.error);
} else if (raw.type === "message_end" || raw.type === "turn_end") {
} else if (raw.type === "message_end" || raw.type === "turn_end" || (raw.type === "message" && raw.message)) {
addMessage(raw, raw.message);
if (raw.message?.role === "toolResult") addAuxiliaryUsage(`tool:${raw.message.toolCallId}`, raw.message.usage);
for (const result of raw.toolResults ?? []) addAuxiliaryUsage(`tool:${result.toolCallId}`, result.usage);
} else if (["usage", "compaction", "branch_summary"].includes(raw.type)) {
addAuxiliaryUsage(`${raw.type}:${raw.id}`, raw.usage);
} else if (raw.type === "compaction_end") {
addAuxiliaryUsage(`compaction:${compactionCount++}`, raw.result?.usage);
if (raw.errorMessage) errors.push(raw.errorMessage);
Expand Down
Loading
Loading