Public: anyone can read this; the crew can change it.
Talk logs one line per model call: account, model, seconds, how it ended #17980
Merged 18h 13m ago, live in production
scout/forge:scout/17976-talk-logs-one-line-per-model-call-accoun → main 0 commits fork
3 files
changed +26
−22
src/crew/call_line.ts +5 −5
| @@ -21,13 +21,13 @@ export interface CallEnd { | ||
| 21 | 21 | readonly stopped: boolean; |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | /** How a call ended: answered, timed_out, refused, limit, stopped, or `error:<class>`. */ | |
| 24 | /** How a call ended: ok, timeout, refused, limit, stopped, or `error:<class>`. */ | |
| 25 | 25 | export function callOutcome(end: Pick<CallEnd, "outcome" | "refused" | "stopped">): string { |
| 26 | 26 | const { outcome } = end; |
| 27 | if (outcome?.ok === true) return "answered"; | |
| 27 | if (outcome?.ok === true) return "ok"; | |
| 28 | 28 | if (end.stopped) return "stopped"; |
| 29 | 29 | if (outcome === undefined) return "error:threw"; |
| 30 | if (outcome.reason === "timeout") return "timed_out"; | |
| 30 | if (outcome.reason === "timeout") return "timeout"; | |
| 31 | 31 | if (end.refused || outcome.limit?.refused === true) return "refused"; |
| 32 | 32 | if (outcome.limit !== undefined || outcome.reason === "rate_limited") return "limit"; |
| 33 | 33 | return `error:${outcome.reason.toLowerCase().replace(/[^a-z0-9]+/g, "_").replace(/^_+|_+$/g, "").slice(0, 40) || "unknown"}`; |
| @@ -39,13 +39,13 @@ export function callFields(end: CallEnd): LogFields { | ||
| 39 | 39 | account: end.account, |
| 40 | 40 | model: end.model, |
| 41 | 41 | seconds: Math.round(end.ms / 100) / 10, |
| 42 | outcome: callOutcome(end), | |
| 42 | end: callOutcome(end), | |
| 43 | 43 | }; |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | /** What each class of ending says as a cause; an error's class is added to its words. */ |
| 47 | 47 | const CAUSES: Readonly<Record<string, string>> = { |
| 48 | timed_out: "it ran out of time", | |
| 48 | timeout: "it ran out of time", | |
| 49 | 49 | refused: "the provider refused it (its credit is spent)", |
| 50 | 50 | limit: "the account hit a rate limit", |
| 51 | 51 | }; |
src/crew/conversation.ts +5 −2
| @@ -50,7 +50,7 @@ import { GateRefusedError } from "../kernel/outbound.ts"; | ||
| 50 | 50 | import { getUserById } from "../core/users.ts"; |
| 51 | 51 | import { mintViaToken, VIA_TOKEN_MAX_MS } from "../core/via_token.ts"; |
| 52 | 52 | import { id } from "../kernel/id.ts"; |
| 53 | import { redactHeld, releaseValues } from "../kernel/log.ts"; | |
| 53 | import { createLog, redactHeld, releaseValues } from "../kernel/log.ts"; | |
| 54 | 54 | import { running } from "../kernel/version.ts"; |
| 55 | 55 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 56 | 56 | import { storedText } from "../kernel/blocked.ts"; |
| @@ -76,6 +76,9 @@ import { listenForSteer, steerable, steerRun } from "./steer.ts"; | ||
| 76 | 76 | import { sandboxPolicy } from "./sandbox.ts"; |
| 77 | 77 | import { sandboxRefusal } from "./sandbox_policy.ts"; |
| 78 | 78 | |
| 79 | /** The scope of the one line each model call writes (#17976). */ | |
| 80 | const talkLog = createLog("talk"); | |
| 81 | ||
| 79 | 82 | export interface ConversationOptions { |
| 80 | 83 | readonly binary: string; |
| 81 | 84 | /** Where this forge answers on this machine: `/mcp` is reached here. */ |
| @@ -951,7 +954,7 @@ function begin( | ||
| 951 | 954 | let lastEnd: CallEnd | undefined; |
| 952 | 955 | const logCall = (startedAt: number, result: AgentOutcome | undefined): void => { |
| 953 | 956 | lastEnd = endOf(startedAt, result); |
| 954 | app.log.info("talk model call", callFields(lastEnd)); | |
| 957 | talkLog.info("talk model call", callFields(lastEnd)); | |
| 955 | 958 | }; |
| 956 | 959 | try { |
| 957 | 960 | const cwd = turnDir(app.config.dataDir); |
test/talk-call-line.test.ts +16 −15
| @@ -58,8 +58,8 @@ beforeAll(async () => { | ||
| 58 | 58 | if (!made.ok) throw new Error(made.error); |
| 59 | 59 | promote(app.db, made.value); |
| 60 | 60 | user = made.value; |
| 61 | teeLines((_scope, message, line) => { | |
| 62 | if (message === "talk model call") lines.push(line); | |
| 61 | teeLines((scope, message, line) => { | |
| 62 | if (message === "talk model call" && scope === "talk") lines.push(line); | |
| 63 | 63 | }); |
| 64 | 64 | }); |
| 65 | 65 | afterAll(async () => { |
| @@ -85,13 +85,14 @@ const call = async (end: Partial<AgentOutcome> | "throw"): Promise<string> => { | ||
| 85 | 85 | |
| 86 | 86 | test("each way a call ends is one line with account, model, seconds and the outcome", async () => { |
| 87 | 87 | const cases: [Partial<AgentOutcome> | "throw", string][] = [ |
| 88 | [{}, "outcome=answered"], | |
| 89 | [{ ok: false, reason: "timeout", result: "" }, "outcome=timed_out"], | |
| 90 | [{ ok: false, reason: "error", result: "API Error: 402 credit" }, "outcome=refused"], | |
| 91 | [{ ok: false, reason: "error", result: "x", limit: { refused: true } }, "outcome=refused"], | |
| 92 | [{ ok: false, reason: "error", result: "x", limit: { said: "5 hour limit" } }, "outcome=limit"], | |
| 93 | [{ ok: false, reason: "exit 1", result: "SECRET-STDERR" }, "outcome=error:exit_1"], | |
| 94 | ["throw", "outcome=error:threw"], | |
| 88 | [{}, "end=ok"], | |
| 89 | [{ ok: false, reason: "timeout", result: "" }, "end=timeout"], | |
| 90 | [{ ok: false, reason: "error", result: "API Error: 402 credit" }, "end=refused"], | |
| 91 | [{ ok: false, reason: "error", result: "x", limit: { refused: true } }, "end=refused"], | |
| 92 | [{ ok: false, reason: "error", result: "x", limit: { said: "5 hour limit" } }, "end=limit"], | |
| 93 | [{ ok: false, reason: "exit 1", result: "SECRET-STDERR" }, "end=error:exit_1"], | |
| 94 | [{ ok: false, reason: "rate_limited", result: "429 SECRET-STDERR" }, "end=limit"], | |
| 95 | ["throw", "end=error:threw"], | |
| 95 | 96 | ]; |
| 96 | 97 | for (const [end, outcome] of cases) { |
| 97 | 98 | const line = await call(end); |
| @@ -125,8 +126,8 @@ test("a lost session then a fresh start is two calls, two lines", async () => { | ||
| 125 | 126 | queue = [{ ok: false, reason: "error", result: "No conversation found with session ID: s-old" }, {}]; |
| 126 | 127 | await sayIn(conversation.value.id); |
| 127 | 128 | expect(lines).toHaveLength(2); |
| 128 | expect(lines[0]).toContain("outcome=error:error"); | |
| 129 | expect(lines[1]).toContain("outcome=answered"); | |
| 129 | expect(lines[0]).toContain("end=error:error"); | |
| 130 | expect(lines[1]).toContain("end=ok"); | |
| 130 | 131 | }); |
| 131 | 132 | |
| 132 | 133 | test("no runner call, no line: a removed chosen connection logs nothing", async () => { |
| @@ -140,7 +141,7 @@ test("no runner call, no line: a removed chosen connection logs nothing", async | ||
| 140 | 141 | }); |
| 141 | 142 | |
| 142 | 143 | test("a call that answered stays answered even when a stop came after", () => { |
| 143 | expect(callOutcome({ outcome: base, refused: false, stopped: true })).toBe("answered"); | |
| 144 | expect(callOutcome({ outcome: base, refused: false, stopped: true })).toBe("ok"); | |
| 144 | 145 | expect(callOutcome({ outcome: { ...base, ok: false, reason: "cancelled" }, refused: false, stopped: true })).toBe("stopped"); |
| 145 | 146 | }); |
| 146 | 147 | |
| @@ -186,13 +187,13 @@ test("a call that answered adds no cause line, and a stopped one is not a failed | ||
| 186 | 187 | }); |
| 187 | 188 | |
| 188 | 189 | test("the pane's words are the log line's outcome, said in words", () => { |
| 189 | expect(causeWords("answered", "a", "m")).toBe(""); | |
| 190 | expect(causeWords("ok", "a", "m")).toBe(""); | |
| 190 | 191 | expect(causeWords("stopped", "a", "m")).toBe(""); |
| 191 | 192 | expect(causeWords("limit", "work", "opus")).toBe("Which call failed: opus on work; the account hit a rate limit."); |
| 192 | expect(causeWords("timed_out", "work", "opus")).toBe("Which call failed: opus on work; it ran out of time."); | |
| 193 | expect(causeWords("timeout", "work", "opus")).toBe("Which call failed: opus on work; it ran out of time."); | |
| 193 | 194 | expect(causeWords("refused", "work", "opus")).toBe("Which call failed: opus on work; the provider refused it (its credit is spent)."); |
| 194 | 195 | expect(causeWords("error:exit_1", "work", "opus")).toBe("Which call failed: opus on work; the account or model returned an error (exit_1)."); |
| 195 | 196 | expect(causeWords("error:exit_1", "work", "opus", "call")).toBe("Which call failed: opus on work."); |
| 196 | 197 | expect(causeWords("error:exit_1", "work", "opus", "cause")).toBe("Why: the account or model returned an error (exit_1)."); |
| 197 | expect(causeWords("timed_out", "platform", "default")).toBe("Which call failed: the default model on platform; it ran out of time."); | |
| 198 | expect(causeWords("timeout", "platform", "default")).toBe("Which call failed: the default model on platform; it ran out of time."); | |
| 198 | 199 | }); |
new above
offline