A desktop coding agent with free access to frontier models.
TypeScript
fix(capture): number archive turns by prompts, record how a turn ended
ogsamratf68dc1e411f4
diff --git a/apps/server/src/session-archive/lifecycle.test.ts b/apps/server/src/session-archive/lifecycle.test.tsindex b239e52f..7c5b5afa 100644--- a/apps/server/src/session-archive/lifecycle.test.ts+++ b/apps/server/src/session-archive/lifecycle.test.ts@@ -163,6 +163,43 @@ describe("completedTurnCount", () => {expect(completedTurnCount(null)).toBeNull();expect(completedTurnCount({ status: 500, unavailable: true })).toBeNull();});++ test("a compaction and synthetic notices are no turn: the count follows the prompts", () => {+ const prompt = (id: string) => ({ info: { id, role: "user" }, parts: [{ type: "text" }] });+ const answer = (id: string) => ({ info: { id, role: "assistant", time: { completed: 1 } } });+ expect(completedTurnCount([+ prompt("u1"), answer("a1"),+ prompt("u2"), answer("a2"),+ // An automatic compaction inside turn 2 (1.x: a "compaction" part; the 2.x adapter: its compaction agent)...+ { info: { id: "c1", role: "user", agent: "build" }, parts: [{ type: "compaction" }] },+ { info: { id: "s1", role: "assistant", summary: true, mode: "compaction", finish: "stop" } },+ { info: { id: "c2", role: "user", agent: "compaction" }, parts: [] },+ { info: { id: "s2", role: "assistant", summary: true, agent: "compaction", finish: "stop" } },+ // ...the engine's continue nudge and its reply...+ { info: { id: "n1", role: "user" }, parts: [{ type: "text", synthetic: true }] },+ answer("a3"),+ // ...a user-run shell command, an activated skill (synthetic text alone).+ { info: { id: "sh", role: "user" }, parts: [{ type: "text", synthetic: true }] },+ { info: { id: "sh-a", role: "assistant", time: { completed: 2 } }, parts: [{ type: "tool" }] },+ prompt("u3"), answer("a4"),+ ])).toBe(3);+ // A prompt with a synthetic note beside what was typed (an attachment path) is one.+ expect(completedTurnCount([+ { info: { role: "user" }, parts: [{ type: "text", synthetic: true }, { type: "text" }] },+ answer("a1"),+ { info: { role: "user" }, parts: [{ type: "text", synthetic: true }, { type: "file" }] },+ answer("a2"),+ ])).toBe(2);+ });++ test("a prompt stopped before any answer is a turn once the next prompt follows it", () => {+ const prompt = { info: { role: "user" }, parts: [{ type: "text" }] };+ const answer = { info: { role: "assistant", time: { completed: 1 } } };+ // Stopped with Esc before the engine answered: still the running turn...+ expect(completedTurnCount([prompt, answer, prompt])).toBe(1);+ // ...and a turn once the next prompt was sent, so turn numbers keep matching the prompts.+ expect(completedTurnCount([prompt, answer, prompt, prompt, answer])).toBe(3);+ });});describe("projectArchiveEnabled", () => {diff --git a/apps/server/src/session-archive/lifecycle.ts b/apps/server/src/session-archive/lifecycle.tsindex 68596b19..a65f0ea8 100644--- a/apps/server/src/session-archive/lifecycle.ts+++ b/apps/server/src/session-archive/lifecycle.ts@@ -87,8 +87,29 @@ export function isFinishedAssistantMessage(message: Record<string, unknown>): bo}/**- * Completed turns in an engine message list: prompts answered by an assistant- * message that ended (several assistant steps of one turn count once). Each+ * A user message that is a prompt someone sent: not a compaction's request+ * (a "compaction" part, or the 2.x adapter's compaction agent) and not+ * synthetic text alone (a user-run shell command's notice, an activated+ * skill, a background job's report, the engine's own continue nudge). A+ * message without parts (an outline from before parts were kept) is one.+ */+export function isPromptMessage(message: Record<string, unknown>): boolean {+ const info = messageInfo(message);+ if ((info.role ?? info.type) !== "user") return false;+ if (info.agent === "compaction") return false;+ const parts = (Array.isArray(message.parts) ? message.parts : []).filter(isRecord);+ if (parts.some((part) => part.type === "compaction")) return false;+ const content = parts.filter((part) => part.type === "text" || part.type === "file" || part.type === "agent" || part.type === "subtask");+ return !(content.length > 0 && content.every((part) => part.type === "text" && part.synthetic === true));+}++/**+ * Completed turns in an engine message list: the prompts that ended,+ * answered by an assistant message that ended (several assistant steps of+ * one turn count once) or followed by the next prompt (stopped before any+ * answer). A compaction's request and summary, and synthetic notices with+ * the replies they woke, are no turn: the count is the number of prompts the+ * session finished, as the trace's prompt milestones number them. Each* completed turn adds one, so the number strictly increases from one turn to* the next and survives app restarts. Null when the list could not be read.*/@@ -100,7 +121,9 @@ export function completedTurnCount(messages: unknown): number | null {if (!isRecord(message)) continue;const info = messageInfo(message);const role = info.role ?? info.type;- if (role === "user") {+ if (isPromptMessage(message)) {+ // The prompt before ended without an answer (stopped right away): it was a turn too.+ if (awaitingReply) turns += 1;awaitingReply = true;} else if (role === "assistant" && awaitingReply && isFinishedAssistantMessage(message)) {turns += 1;diff --git a/apps/server/src/session-upload-observer.test.ts b/apps/server/src/session-upload-observer.test.tsindex 232ccc8b..912c8264 100644--- a/apps/server/src/session-upload-observer.test.ts+++ b/apps/server/src/session-upload-observer.test.ts@@ -7,6 +7,7 @@ import { zstdDecompressSync } from "node:zlib";import {createSessionObservers,engineReplaced,+ messageOutline,observeUploadedSession,projectArchiveEngineReads,promptDispatched,@@ -14,6 +15,7 @@ import {type ObservedUploader,type ObserverTiming,type SessionObservers,+ turnOutcome,} from "./session-upload-observer.js";import type { CaptureResult, DrainResult, FinalReason } from "./session-archive/index.js";import { ProjectArchiveLifecycle, type ProjectArchiver } from "./session-archive/lifecycle.js";@@ -785,3 +787,43 @@ describe("session upload observer with the session uploader", () => {for (const envelope of omitted) expect(document(envelope)).toMatchObject({ trace_truncated: false, dropped_event_count: 0 });}, 60_000);});++describe("message outlines and turn outcomes", () => {+ test("a user message's outline keeps what turn counting reads of its parts, never their text", () => {+ const outline = messageOutline({+ info: { id: "u1", role: "user", summary: { diffs: [{ file: "a.ts", before: "x", after: "y" }] } },+ parts: [+ { id: "p1", type: "text", text: "secret prompt", synthetic: true },+ { id: "p2", type: "text", text: "typed" },+ { id: "p3", type: "file", url: "file:///a.png" },+ { id: "p4", type: "compaction", auto: true },+ ],+ });+ expect(outline).toEqual({+ info: { id: "u1", role: "user" },+ parts: [{ type: "text", synthetic: true }, { type: "text" }, { type: "file" }, { type: "compaction" }],+ });+ // An assistant message keeps only its finish parts.+ expect(messageOutline({ info: { id: "a1", role: "assistant" }, parts: [{ type: "text", text: "answer" }, { type: "step-finish", reason: "stop" }] }))+ .toEqual({ info: { id: "a1", role: "assistant" }, parts: [{ type: "step-finish", reason: "stop" }] });+ });++ test("how the latest turn ended", () => {+ const prompt = { info: { role: "user" }, parts: [{ type: "text" }] };+ expect(turnOutcome([])).toBeNull();+ expect(turnOutcome([prompt, { info: { role: "assistant", time: { completed: 2 } } }])).toBe("completed");+ // Stopped with Esc: the 1.x engine (and the 2.x adapter) record MessageAbortedError, the 2.x context an "aborted" error.+ expect(turnOutcome([prompt, { info: { role: "assistant", finish: "tool-calls" } }, { info: { role: "assistant", error: { name: "MessageAbortedError" } } }])).toBe("stopped");+ expect(turnOutcome([{ type: "user" }, { type: "assistant", error: { type: "aborted", message: "Aborted" } }])).toBe("stopped");+ expect(turnOutcome([prompt, { info: { role: "assistant", error: { name: "APIError" } } }])).toBe("error");+ expect(turnOutcome([prompt, { info: { role: "assistant" } }])).toBe("incomplete");+ expect(turnOutcome([prompt, { info: { role: "assistant", time: { completed: 1 } } }, prompt])).toBe("no_reply");+ // A compaction's summary after the reply does not hide how the turn ended.+ expect(turnOutcome([+ prompt,+ { info: { role: "assistant", error: { name: "MessageAbortedError" } } },+ { info: { role: "user", agent: "compaction" }, parts: [{ type: "compaction" }] },+ { info: { role: "assistant", summary: true, finish: "stop" } },+ ])).toBe("stopped");+ });+});diff --git a/apps/server/src/session-upload-observer.ts b/apps/server/src/session-upload-observer.tsindex f6852d4d..f0862afd 100644--- a/apps/server/src/session-upload-observer.ts+++ b/apps/server/src/session-upload-observer.ts@@ -8,7 +8,7 @@* named by plain data (EngineTarget) so the target can cross threads.*/import { loopbackFetch } from "./server-fetch.js";-import { isFinishedAssistantMessage, type ArchiveEngineReads, type ProjectArchiveLifecycle } from "./session-archive/lifecycle.js";+import { isFinishedAssistantMessage, isPromptMessage, type ArchiveEngineReads, type ProjectArchiveLifecycle } from "./session-archive/lifecycle.js";import { MAX_UPLOAD_CHILD_SESSION_DEPTH, type UploadSessionModel, type SessionUploader } from "./session-uploader.js";/** The engine a captured request went to: base URL, request headers (the engine's auth), query and API generation. */@@ -463,21 +463,71 @@ async function* engineMessages(fetchEngine: EngineFetch, path: string, paged: bo}}+/** What of a user message's part turn counting reads: its type, and whether it is synthetic (never its text). */+const OUTLINED_USER_PARTS = new Set(["text", "file", "agent", "subtask", "compaction"]);+/*** What turn counting, the model and the checkpoint read of a message: its- * info, less a user message's diff summary (whole-file patches), and its- * finish parts. Other shapes (v2 context entries) are kept as they are.+ * info, less a user message's diff summary (whole-file patches), its finish+ * parts and, for a user message, the type of each content part and whether+ * it is synthetic (a compaction's request or a synthetic notice is no+ * prompt, see isPromptMessage). Other shapes (v2 context entries) are kept+ * as they are.*/-function messageOutline(message: unknown): unknown {+export function messageOutline(message: unknown): unknown {if (!isRecord(message) || !isRecord(message.info)) return message;const { summary, ...info } = message.info;const parts = Array.isArray(message.parts) ? message.parts : [];+ const user = message.info.role === "user";return {info: isRecord(summary) ? info : message.info,- parts: parts.filter((part) => isRecord(part) && ["step-finish", "finish", "error"].includes(String(part.type))),+ parts: parts.flatMap((part) => {+ if (!isRecord(part)) return [];+ const type = String(part.type);+ if (["step-finish", "finish", "error"].includes(type)) return [part];+ if (user && OUTLINED_USER_PARTS.has(type)) return [part.synthetic === true ? { type, synthetic: true } : { type }];+ return [];+ }),};}+export type TurnOutcome = "completed" | "stopped" | "error" | "no_reply" | "incomplete";++/**+ * How the session's latest turn ended, from its messages in outline: its+ * last assistant message ended normally ("completed"), was stopped (Esc,+ * the engine's MessageAbortedError: "stopped") or failed ("error"), had not+ * ended ("incomplete"), or the prompt got no answer at all ("no_reply").+ * Null without a prompt.+ */+export function turnOutcome(outline: readonly unknown[]): TurnOutcome | null {+ let prompt = -1;+ for (let index = outline.length - 1; index >= 0; index -= 1) {+ const message = outline[index];+ if (isRecord(message) && isPromptMessage(message)) {+ prompt = index;+ break;+ }+ }+ if (prompt < 0) return null;+ let last: Record<string, unknown> | null = null;+ for (const message of outline.slice(prompt + 1)) {+ if (!isRecord(message)) continue;+ const info = isRecord(message.info) ? message.info : message;+ // A compaction's summary is the engine's, not the turn's answer.+ if (info.summary === true || info.agent === "compaction" || info.mode === "compaction") continue;+ if ((info.role ?? info.type) === "assistant") last = message;+ }+ if (!last) return "no_reply";+ const info = isRecord(last.info) ? last.info : last;+ const error = info.error;+ if (error != null) {+ const name = isRecord(error) ? error.name ?? error.type : error;+ return name === "MessageAbortedError" || name === "aborted" ? "stopped" : "error";+ }+ return isFinishedAssistantMessage(last) ? "completed" : "incomplete";+}+async function messageOutlines(messages: AsyncIterable<EngineMessage>): Promise<unknown[]> {const outline: unknown[] = [];for await (const { message } of messages) outline.push(messageOutline(message));@@ -850,7 +900,9 @@ export function observeUploadedSession(input: {}}room(newest.bytes);- sessionUploader.recordTrace(sessionId, "session.idle", { status });+ // How the turn ended, so a turn stopped with Esc reads as stopped, not as answered.+ const outcome = history ? turnOutcome(history.outline) : null;+ sessionUploader.recordTrace(sessionId, "session.idle", outcome ? { status, outcome } : { status });// The turn snapshot runs first so the artifacts it discovers are part of// the trace flushed right behind it.captureTurnSnapshot();