diff --git a/.github/drivers/pi_agent_core_driver_sample_node.cjs b/.github/drivers/pi_agent_core_driver_sample_node.cjs index a20dc40244e..e1e63c66dd6 100644 --- a/.github/drivers/pi_agent_core_driver_sample_node.cjs +++ b/.github/drivers/pi_agent_core_driver_sample_node.cjs @@ -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. * diff --git a/actions/setup/js/fixtures/pi_ci_stream.cjs b/actions/setup/js/fixtures/pi_ci_stream.cjs index a58a7ff9019..eb74103c6e6 100644 --- a/actions/setup/js/fixtures/pi_ci_stream.cjs +++ b/actions/setup/js/fixtures/pi_ci_stream.cjs @@ -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 }; diff --git a/actions/setup/js/parse_pi_log.cjs b/actions/setup/js/parse_pi_log.cjs index af869c3db7b..c47eeaf7b59 100644 --- a/actions/setup/js/parse_pi_log.cjs +++ b/actions/setup/js/parse_pi_log.cjs @@ -1,16 +1,7 @@ // @ts-check /// -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"); @@ -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, maxTurnsHit: boolean}} Parsed log data */ @@ -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); if (stats) { const terminal = [...canonicalLogEntries].reverse().find(event => event.type === "session.result"); const selected = selectSessionResult(canonicalLogEntries); @@ -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 } }); } @@ -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; diff --git a/actions/setup/js/pi_legacy_session.test.cjs b/actions/setup/js/pi_legacy_session.test.cjs index 93a24bf11ef..895b65774e1 100644 --- a/actions/setup/js/pi_legacy_session.test.cjs +++ b/actions/setup/js/pi_legacy_session.test.cjs @@ -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" }); diff --git a/actions/setup/js/pi_session.cjs b/actions/setup/js/pi_session.cjs index 880435ee912..70ed3274b25 100644 --- a/actions/setup/js/pi_session.cjs +++ b/actions/setup/js/pi_session.cjs @@ -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(); @@ -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 })); }; const emitText = (raw, state, kind, index, text, delta, role = "assistant", metadata = {}) => { @@ -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; @@ -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 => ({ + ...event, + agentId: event.agentId ?? invocationId, + data: { ...event.data, agentId: event.data.agentId ?? invocationId }, + })); + 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"; @@ -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); @@ -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; } @@ -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); diff --git a/actions/setup/js/pi_session.test.cjs b/actions/setup/js/pi_session.test.cjs index f23a8f83d5e..684e66a2237 100644 --- a/actions/setup/js/pi_session.test.cjs +++ b/actions/setup/js/pi_session.test.cjs @@ -1,7 +1,7 @@ import { describe, it, expect } from "vitest"; import { parsePiLog } from "./parse_pi_log.cjs"; import { transformPiV3Entries, computePiV3Stats } from "./pi_session.cjs"; -import { success, failure } from "./fixtures/pi_ci_stream.cjs"; +import { success, failure, childObservations } from "./fixtures/pi_ci_stream.cjs"; import { normalizeAgentSession, selectSessionResult, sessionTokenTotal } from "./agent_session.cjs"; import { mergeSessionSources } from "./unified_session.cjs"; import { normalizeUnifiedSessionEvent } from "./unified_session_payload.cjs"; @@ -11,6 +11,110 @@ const parse = records => parsePiLog(records.map(record => JSON.stringify(record) const byType = (events, type) => events.filter(event => event.type === type); describe("Pi CI stream regressions", () => { + it("retains sampled child text and reasoning in their evidenced dispatch scope through unified projection", () => { + const original = structuredClone(childObservations); + const records = [...success, ...childObservations]; + const events = parse(records).logEntries; + const scope = "legacy:pi:sanitized-reader:1"; + const unified = mergeSessionSources([{ component: "agent", phase: "agent", path: "pi-streaming.jsonl", events }]); + expect(byType(unified, "assistant.reasoning")).toMatchObject([{ agentId: scope, data: { content: "Sanitized reasoning.", agentId: scope } }]); + expect(byType(unified, "assistant.message").find(event => event.agentId === scope)).toMatchObject({ + data: { content: "Sanitized child answer.", model: "gpt-5-mini", messageId: "sanitized-child-response", agentId: scope }, + }); + expect(byType(unified, "subagent.request")).toMatchObject([{ agentId: scope, data: { inputTokens: 3085, outputTokens: 136, cacheReadTokens: 0, cacheWriteTokens: 0 } }]); + expect(selectSessionResult(events)).toMatchObject({ numTurns: 2, usage: { input_tokens: 6427, output_tokens: 145 } }); + expect(byType(unified, "subagent.completed")).toEqual([]); + expect(byType(unified, "subagent.failed")).toEqual([]); + const validate = createSessionValidator("unified").event; + for (const event of unified) expect(validate(event), JSON.stringify(validate.errors)).toBe(true); + expect(childObservations).toEqual(original); + }); + + it("reconciles synthetic child streams without conflating parent tool IDs or repeated snapshots", () => { + const message = { role: "assistant", id: "child-answer", model: "fixture-model", content: [{ type: "text", text: "Child answer" }] }; + const wrap = event => ({ type: "gh_aw_subagent_event", agent: "reader", invocation_id: "child", event, provenance: { path: "pi-streaming.jsonl", index: 7 } }); + const records = [ + { type: "gh_aw_subagent_dispatch", agent: "reader", invocation_id: "child" }, + wrap({ type: "message_update", assistantMessageEvent: { type: "text_delta", contentIndex: 0, delta: "Child " } }), + { type: "tool_execution_start", toolCallId: "same-id", toolName: "parent-tool", args: {} }, + wrap({ type: "tool_execution_start", toolCallId: "same-id", toolName: "child-tool", args: { child: true } }), + wrap({ type: "tool_execution_end", toolCallId: "same-id", result: { error: "Child failure" } }), + wrap({ type: "message_end", message }), + wrap({ type: "turn_end", message }), + wrap({ type: "agent_end", messages: [message] }), + wrap({ type: "error", message: "Child error" }), + ]; + const events = transformPiV3Entries(records); + expect(byType(events, "tool.execution_start").map(event => [event.agentId, event.data.toolName])).toEqual([ + [undefined, "parent-tool"], + ["child", "child-tool"], + ]); + expect(byType(events, "tool.execution_complete")).toMatchObject([{ agentId: "child", data: { success: false, error: "Child failure" } }]); + expect(byType(events, "assistant.message").map(event => event.data.content)).toEqual(["Child ", "answer"]); + expect(byType(events, "assistant.message").every(event => event.agentId === "child" && event.provenance.path === "pi-streaming.jsonl" && event.provenance.index === 7)).toBe(true); + expect(byType(events, "session.error")).toMatchObject([{ agentId: "child", data: { error: "Child error" } }]); + expect(byType(events, "subagent.request")).toHaveLength(1); + expect(transformPiV3Entries(JSON.parse(JSON.stringify(events)))).toEqual(JSON.parse(JSON.stringify(events))); + }); + + it("preserves native unknown observations and deduplicated system snapshots without new custom types", () => { + const message = { role: "system", content: "SANITIZED SYSTEM PROMPT", sections: ["sanitized"], timestamp: 1, toolsAdded: [] }; + const unknown = { type: "future_native_event", payload: { preserved: false }, timestamp: 2 }; + const update = { type: "future_update", payload: { preserved: 0 }, contentIndex: 1 }; + const records = [{ type: "message_start", message }, { type: "message_end", message }, unknown, { type: "message_update", assistantMessageEvent: update }, { type: "agent_end", messages: [message] }]; + const events = parse(records).logEntries; + const compact = events.map(event => normalizeUnifiedSessionEvent(event)); + expect(byType(compact, "session.info").map(event => event.data)).toEqual([ + { sourceType: "message_snapshot", role: "system", content: message }, + { sourceType: "future_native_event", content: unknown }, + ]); + expect(byType(compact, "assistant.message_update")).toMatchObject([{ data: { type: "future_update", content: update } }]); + expect(parse(records).markdown).not.toContain("SANITIZED SYSTEM PROMPT"); + expect(byType(events, "session.result")).toEqual([]); + }); + + // Official v1.1.0 agent/src/types.ts and coding-agent/src/core/agent-session.ts; + // synthetic observations, not a workflow run using the upgraded CLI. + it.each([true, false])("retains Pi 1.1.0 cancellation=%s and measured tool duration without fabricating a terminal result", aborted => { + const records = [ + { type: "session", version: 3, id: "release-contract" }, + { type: "tool_execution_start", toolCallId: "measured", toolName: "bash", args: { command: "sanitized" } }, + { type: "tool_execution_end", toolCallId: "measured", toolName: "bash", result: { content: [] }, isError: false, durationMs: 0 }, + { type: "agent_settled", aborted }, + ]; + const compact = parse(records).logEntries.map(event => normalizeUnifiedSessionEvent(event)); + expect(byType(compact, "tool.execution_complete")).toMatchObject([{ data: { durationMs: 0, success: true } }]); + expect(byType(compact, "session.idle")).toEqual([{ type: "session.idle", data: { aborted } }]); + expect(byType(compact, "session.result")).toEqual([]); + }); + + // Synthetic persisted entries following v1.1.0 core/session-manager.ts, + // not a downloaded persisted session (the sampled artifacts contain streams). + it("maps persisted native message envelopes through the same canonical content and tool mapping", () => { + const records = [ + { type: "session", version: 3, id: "persisted", cwd: "/sanitized" }, + { type: "message", id: "user-entry", parentId: null, timestamp: 0, message: { role: "user", content: "Sanitized prompt" } }, + { + type: "message", + id: "call-entry", + parentId: "user-entry", + timestamp: 1, + message: { role: "assistant", id: "call-message", content: [{ type: "toolCall", id: "tool", name: "bash", arguments: false }], usage: { input: 3, output: 1 } }, + }, + { type: "message", id: "result-entry", parentId: "call-entry", timestamp: 2, message: { role: "toolResult", toolCallId: "tool", content: 0, isError: false } }, + { type: "message", id: "answer-entry", parentId: "result-entry", timestamp: 3, message: { role: "assistant", id: "answer-message", content: [{ type: "text", text: "Sanitized answer" }], usage: { input: 2, output: 1 } } }, + { type: "usage", id: "auxiliary", parentId: "answer-entry", timestamp: 4, kind: "cache_warm", model: "fixture", provider: "fixture", usage: { input: 1, output: 0 } }, + ]; + const events = parse(records).logEntries; + expect(byType(events, "user.message")).toMatchObject([{ id: "user-entry", parentId: null, timestamp: 0, data: { content: "Sanitized prompt" } }]); + expect(byType(events, "tool.execution_start")).toMatchObject([{ id: "call-entry", data: { toolCallId: "tool", toolName: "bash", input: false } }]); + expect(byType(events, "tool.execution_complete")).toMatchObject([{ id: "result-entry", data: { output: 0, success: true } }]); + expect(byType(events, "assistant.message")).toMatchObject([{ id: "answer-entry", data: { content: "Sanitized answer" } }]); + expect(byType(events, "session.info")).toMatchObject([{ data: { sourceType: "usage", content: records.at(-1) } }]); + expect(selectSessionResult(events)).toMatchObject({ numTurns: 2, usage: { input_tokens: 6, output_tokens: 2 } }); + expect(parse(records.slice(1, -1)).logEntries).toHaveLength(events.length - 2); + }); + it.each([ ["success", success], ["provider failure", failure], @@ -85,7 +189,12 @@ describe("Pi CI stream regressions", () => { expect(byType(events, "pi.subagent_dispatch")).toEqual([]); expect(byType(events, "pi.subagent_event")).toEqual([]); expect(byType(events, "pi.subagent_result")).toEqual([]); - expect(byType(events, "assistant.message").map(e => e.data.content)).toEqual(["Parent answer"]); + expect( + byType(events, "assistant.message") + .filter(e => !e.agentId) + .map(e => e.data.content) + ).toEqual(["Parent answer"]); + expect(byType(events, "assistant.message").find(e => e.agentId === "invocation-1")).toMatchObject({ data: { content: "Child answer", agentId: "invocation-1" } }); expect(computePiV3Stats(records).usage.input_tokens).toBe(10); }); @@ -499,7 +608,7 @@ describe("Pi CI stream regressions", () => { expect(logEntries.map(e => e.type)).toEqual(["assistant.message", "vendor.progress", "assistant.message", "session.result"]); expect(byType(logEntries, "assistant.message").map(e => e.data.content)).toEqual([" flat ", " v3 "]); expect(logEntries[1]).toMatchObject({ id: "native", data: { count: 0 } }); - expect(parsePiLog('{"type":"unknown","value":1}').logEntries).toEqual([]); + expect(parsePiLog('{"type":"unknown","value":1}').logEntries).toMatchObject([{ type: "session.info", data: { sourceType: "unknown", content: { type: "unknown", value: 1 } } }]); }); it("accounts for truncated message_end and terminal-only transcript snapshots", () => { diff --git a/actions/setup/js/pi_session_pipeline.test.cjs b/actions/setup/js/pi_session_pipeline.test.cjs new file mode 100644 index 00000000000..b85ba3d38d1 --- /dev/null +++ b/actions/setup/js/pi_session_pipeline.test.cjs @@ -0,0 +1,78 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { success, failure, childObservations } from "./fixtures/pi_ci_stream.cjs"; +import { parsePiLog } from "./parse_pi_log.cjs"; +import { runLogParser } from "./log_parser_bootstrap.cjs"; +import { writeUnifiedSession } from "./unified_session.cjs"; +import { normalizeUnifiedSessionEvent } from "./unified_session_payload.cjs"; +import { createSessionValidator } from "./scripts/validate_session.cjs"; + +describe("Pi native-to-unified artifact pipeline", () => { + let root; + let originalEnv; + let originalCore; + const readEvents = file => fs.readFileSync(file, "utf8").trimEnd().split("\n").map(JSON.parse); + + beforeEach(() => { + root = fs.mkdtempSync(path.join(os.tmpdir(), "gh-aw-pi-session-")); + originalEnv = { ...process.env }; + originalCore = global.core; + process.env.GH_AW_AGENT_OUTPUT = path.join(root, "pi-streaming.jsonl"); + delete process.env.GH_AW_SAFE_OUTPUTS; + global.core = { + info: vi.fn(), + debug: vi.fn(), + warning: vi.fn(), + error: vi.fn(), + setOutput: vi.fn(), + setFailed: vi.fn(), + exportVariable: vi.fn(), + summary: { addRaw: vi.fn().mockReturnThis(), write: vi.fn().mockResolvedValue(undefined) }, + }; + }); + + afterEach(() => { + process.env = originalEnv; + global.core = originalCore; + fs.rmSync(root, { recursive: true, force: true }); + }); + + // Sanitized excerpts from separate runs, combined locally to exercise the + // collector. This is not a new live execution or an exact single-run fixture. + it.each([ + ["success with child observations", [...success, ...childObservations]], + ["provider failure", failure], + ])("publishes %s once with authoritative content, scope and provenance", async (_, records) => { + const raw = records.map(record => JSON.stringify(record)).join("\n") + "\n"; + fs.writeFileSync(process.env.GH_AW_AGENT_OUTPUT, raw); + await runLogParser({ parserName: "Pi", parseLog: parsePiLog, rootDir: root }); + expect(global.core.setFailed).not.toHaveBeenCalled(); + const canonical = readEvents(path.join(root, "agent-session.jsonl")); + const parsed = JSON.parse(JSON.stringify(parsePiLog(raw).logEntries)); + expect(canonical.filter(event => event.type !== "agent.execution")).toEqual(parsed); + + const unified = writeUnifiedSession({ rootDir: root, engine: "pi", warn: vi.fn() }); + const file = path.join(root, "usage/aw_session.jsonl"); + expect(readEvents(file)).toEqual(unified); + const agent = unified.filter(event => event.provenance?.path === "agent-session.jsonl" && event.type !== "agent.execution"); + expect(agent.map(event => event.type)).toEqual(parsed.map(event => event.type)); + expect(agent.map(event => event.data)).toEqual(parsed.map(event => normalizeUnifiedSessionEvent(event).data)); + for (const [index, event] of agent.entries()) { + expect(event.provenance).toMatchObject({ component: "agent", phase: "agent", path: "agent-session.jsonl", index }); + } + const validate = createSessionValidator("unified").event; + for (const event of unified) expect(validate(event), JSON.stringify(validate.errors)).toBe(true); + expect(unified.some(event => event.type.startsWith("pi."))).toBe(false); + expect(unified.some(event => event.type === "session.collection_warning")).toBe(false); + const bytes = fs.readFileSync(file, "utf8"); + writeUnifiedSession({ rootDir: root, engine: "pi", warn: vi.fn() }); + expect(fs.readFileSync(file, "utf8")).toBe(bytes); + expect(fs.readFileSync(process.env.GH_AW_AGENT_OUTPUT, "utf8")).toBe(raw); + + fs.unlinkSync(path.join(root, "agent-session.jsonl")); + const reconstructed = writeUnifiedSession({ rootDir: root, engine: "pi", warn: vi.fn() }); + expect(reconstructed.filter(event => event.provenance?.component === "agent" && event.type !== "agent.execution").map(event => event.data)).toEqual(agent.map(event => event.data)); + }); +});