mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 12:07:28 +01:00
Asked to synthesize user feedback, the agent in a session searched its memory folder, the repo and GitHub, found nothing, and asked for an export of reports Loki already holds. And the conversation view said "Auto" with no model and no way to learn what Auto meant. - Loki's feedback reports reach the agent as a visible file on the box (/api/feedback/export, fenced as untrusted, no contact data): from "/", from a chip when the agent asks for feedback, and with any library prompt that declares `attach: ["feedback"]` — in a session and in Run with Loki. - SessionInfo names the model (read from the session log) and the mode in words; its sheet changes the model, the effort (/effort) and the mode. - Next-step chips use gpt-oss-120b and never approve an agent that said it lacks data or access. - Library prompts that pointed at things the agent cannot reach (the knowledge graph, Loki's database) now say what to use and to say "can't tell" instead of guessing. - A pasted_content closing tag with attributes no longer leaks into the conversation. - "/" opens SessionPicker, never taller than the room above the box (it ran off a 320×568 screen). Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx Co-authored-by: Claude <noreply@anthropic.com>
304 lines
9.0 KiB
TypeScript
304 lines
9.0 KiB
TypeScript
// Claude Code's session log read as a conversation (src/lib/claude-transcript.ts).
|
|
// Run: npx tsx scripts/test/claude-transcript.ts
|
|
import {
|
|
CYCLE_MODE_KEY,
|
|
sessionMode,
|
|
sessionModel,
|
|
modelLabel,
|
|
describeToolStep,
|
|
turnElapsedLabel,
|
|
describeToolRun,
|
|
groupTranscript,
|
|
mergeTranscriptItems,
|
|
newTranscriptState,
|
|
reduceTranscriptLine,
|
|
summariseToolInput,
|
|
type TranscriptItem,
|
|
} from "@/lib/claude-transcript";
|
|
|
|
let pass = 0;
|
|
let fail = 0;
|
|
function ok(cond: boolean, label: string) {
|
|
if (cond) pass++;
|
|
else {
|
|
fail++;
|
|
console.error(`✗ ${label}`);
|
|
}
|
|
}
|
|
|
|
const L = (o: unknown) => JSON.stringify(o);
|
|
const s = newTranscriptState();
|
|
let items: TranscriptItem[] = [];
|
|
const feed = (raw: string) => {
|
|
const out = reduceTranscriptLine(s, raw);
|
|
items = mergeTranscriptItems(items, out);
|
|
return out;
|
|
};
|
|
|
|
// A typed prompt (string content) becomes a user message.
|
|
feed(
|
|
L({
|
|
type: "user",
|
|
uuid: "u1",
|
|
timestamp: "t1",
|
|
message: { role: "user", content: "Fix the header" },
|
|
}),
|
|
);
|
|
ok(
|
|
items.length === 1 && items[0].kind === "user" && items[0].text === "Fix the header",
|
|
"user prompt",
|
|
);
|
|
|
|
// Assistant text + a tool call in one line → two items, tool running.
|
|
feed(
|
|
L({
|
|
type: "assistant",
|
|
uuid: "a1",
|
|
timestamp: "t2",
|
|
message: {
|
|
role: "assistant",
|
|
content: [
|
|
{ type: "thinking", thinking: "hmm" },
|
|
{ type: "text", text: "Looking at it." },
|
|
{ type: "tool_use", id: "tu1", name: "Bash", input: { command: "git status\n--short" } },
|
|
],
|
|
},
|
|
}),
|
|
);
|
|
ok(items.length === 3, "thinking dropped, text + tool kept");
|
|
ok(items[1].kind === "assistant" && items[1].text === "Looking at it.", "assistant text");
|
|
const tool = items[2];
|
|
ok(
|
|
tool.kind === "tool" && tool.status === "running" && tool.summary === "git status --short",
|
|
"tool call summarised, whitespace folded",
|
|
);
|
|
|
|
// Its result arrives on a later user line → the SAME item updates in place.
|
|
feed(
|
|
L({
|
|
type: "user",
|
|
uuid: "u2",
|
|
message: {
|
|
role: "user",
|
|
content: [
|
|
{
|
|
type: "tool_result",
|
|
tool_use_id: "tu1",
|
|
content: [{ type: "text", text: "M src/a.ts" }],
|
|
},
|
|
],
|
|
},
|
|
}),
|
|
);
|
|
ok(items.length === 3, "result does not add a row");
|
|
const done = items[2];
|
|
ok(
|
|
done.kind === "tool" && done.status === "done" && done.result === "M src/a.ts",
|
|
"tool row updated with result",
|
|
);
|
|
|
|
// Errors are marked.
|
|
feed(
|
|
L({
|
|
type: "assistant",
|
|
uuid: "a2",
|
|
message: {
|
|
content: [{ type: "tool_use", id: "tu2", name: "Edit", input: { file_path: "/x/y.ts" } }],
|
|
},
|
|
}),
|
|
);
|
|
feed(
|
|
L({
|
|
type: "user",
|
|
uuid: "u3",
|
|
message: {
|
|
content: [{ type: "tool_result", tool_use_id: "tu2", is_error: true, content: "boom" }],
|
|
},
|
|
}),
|
|
);
|
|
const err = items.find((i) => i.id === "tu2");
|
|
ok(
|
|
err?.kind === "tool" && err.status === "error" && err.summary === "/x/y.ts",
|
|
"error result + path summary",
|
|
);
|
|
|
|
// Noise yields nothing: harness text, sidechains, meta, summaries, junk.
|
|
const before = items.length;
|
|
feed(L({ type: "user", uuid: "n1", message: { content: "<command-name>/clear</command-name>" } }));
|
|
feed(
|
|
L({
|
|
type: "assistant",
|
|
uuid: "n2",
|
|
isSidechain: true,
|
|
message: { content: [{ type: "text", text: "sub" }] },
|
|
}),
|
|
);
|
|
feed(L({ type: "user", uuid: "n3", isMeta: true, message: { content: "meta" } }));
|
|
feed(L({ type: "summary", summary: "x", leafUuid: "a1" }));
|
|
feed("{not json");
|
|
feed(
|
|
L({
|
|
type: "user",
|
|
uuid: "n4",
|
|
message: { content: [{ type: "tool_result", tool_use_id: "unknown", content: "x" }] },
|
|
}),
|
|
);
|
|
ok(items.length === before, "noise and orphan results ignored");
|
|
|
|
// Results are previews, never whole files.
|
|
feed(
|
|
L({
|
|
type: "assistant",
|
|
uuid: "a3",
|
|
message: {
|
|
content: [{ type: "tool_use", id: "tu3", name: "Read", input: { file_path: "/big" } }],
|
|
},
|
|
}),
|
|
);
|
|
feed(
|
|
L({
|
|
type: "user",
|
|
uuid: "u4",
|
|
message: {
|
|
content: [{ type: "tool_result", tool_use_id: "tu3", content: "x".repeat(50_000) }],
|
|
},
|
|
}),
|
|
);
|
|
const big = items.find((i) => i.id === "tu3");
|
|
ok(big?.kind === "tool" && (big.result?.length ?? 0) <= 1200, "result clipped to a preview");
|
|
|
|
ok(summariseToolInput("Grep", { pattern: "foo" }) === "foo", "grep summary");
|
|
ok(
|
|
summariseToolInput("Task", { description: "Map it", prompt: "long" }) === "Map it",
|
|
"task summary prefers description",
|
|
);
|
|
|
|
// Grouping: consecutive tools fold into one block between messages.
|
|
const blocks = groupTranscript(items);
|
|
ok(blocks[0].type === "message" && blocks[1].type === "message", "user + assistant are messages");
|
|
ok(blocks[2].type === "tools" && blocks[2].items.length === 3, "tu1..tu3 fold into one run");
|
|
ok(
|
|
describeToolRun([
|
|
{ id: "1", at: null, kind: "tool", name: "Bash", summary: "", status: "done", result: null },
|
|
{ id: "2", at: null, kind: "tool", name: "Bash", summary: "", status: "done", result: null },
|
|
{ id: "3", at: null, kind: "tool", name: "Edit", summary: "", status: "done", result: null },
|
|
]) === "Ran 2 commands, edited a file",
|
|
"run described in words",
|
|
);
|
|
|
|
ok(
|
|
describeToolRun([
|
|
{ id: "1", at: null, kind: "tool", name: "Bash", summary: "ls", status: "done", result: null },
|
|
{
|
|
id: "2",
|
|
at: null,
|
|
kind: "tool",
|
|
name: "Bash",
|
|
summary: "git push",
|
|
status: "running",
|
|
result: null,
|
|
},
|
|
]) === "Running git push",
|
|
"a live run says what is happening now",
|
|
);
|
|
|
|
// A single call names itself, the way the Claude app writes a step.
|
|
const lone = {
|
|
id: "3",
|
|
at: null,
|
|
kind: "tool" as const,
|
|
name: "Bash",
|
|
summary: "cd /home/user/loki && git status",
|
|
status: "done" as const,
|
|
result: null,
|
|
};
|
|
ok(
|
|
describeToolStep(lone).verb === "Ran" && describeToolStep(lone).target === lone.summary,
|
|
"a finished command reads Ran + the command",
|
|
);
|
|
ok(
|
|
describeToolStep({ ...lone, status: "running" }).verb === "Running",
|
|
"a running command reads Running",
|
|
);
|
|
ok(
|
|
describeToolStep({ ...lone, name: "Mystery" }).verb === "Mystery",
|
|
"an unknown tool keeps its name",
|
|
);
|
|
|
|
// The turn clock.
|
|
const t0 = "2026-10-05T13:00:00.000Z";
|
|
ok(turnElapsedLabel(t0, Date.parse(t0) + 13_000) === "13 s", "13 s");
|
|
ok(turnElapsedLabel(t0, Date.parse(t0) + 125_000) === "2 min 5 s", "2 min 5 s");
|
|
ok(turnElapsedLabel(t0, Date.parse(t0) + 120_000) === "2 min", "2 min");
|
|
ok(
|
|
turnElapsedLabel(null, 0) === null && turnElapsedLabel("nope", 0) === null,
|
|
"no start, no clock",
|
|
);
|
|
|
|
// The permission mode rides on what you send (operator, 2026-10-05: the
|
|
// Claude app's Auto / Accept edits / Plan, in Loki's conversation view).
|
|
{
|
|
const st = newTranscriptState();
|
|
const sent = (uuid: string, mode: string | undefined, text: string) =>
|
|
reduceTranscriptLine(
|
|
st,
|
|
JSON.stringify({
|
|
type: "user",
|
|
uuid,
|
|
timestamp: "2026-10-05T13:00:00Z",
|
|
...(mode ? { permissionMode: mode } : {}),
|
|
message: { role: "user", content: text },
|
|
}),
|
|
);
|
|
const first = sent("u1", "acceptEdits", "fix it");
|
|
ok(first[0]?.kind === "user" && first[0].mode === "acceptEdits", "a message carries its mode");
|
|
const listItems = [...first, ...sent("u2", "plan", [{ type: "text", text: "plan it" }] as never)];
|
|
ok(sessionMode(listItems)?.label === "Plan only", "the newest message's mode wins");
|
|
ok(sessionMode(sent("u3", "auto", "go"))?.label === "Auto", "auto reads Auto");
|
|
ok(sessionMode(sent("u4", "default", "go"))?.label === "Ask first", "default is named");
|
|
ok(
|
|
sessionMode(sent("u5", "someNewMode", "go"))?.label === "someNewMode",
|
|
"an unknown mode keeps its name",
|
|
);
|
|
ok(sessionMode(sent("u6", undefined, "go")) === null, "an older log says nothing");
|
|
const asst = reduceTranscriptLine(
|
|
st,
|
|
JSON.stringify({
|
|
type: "assistant",
|
|
uuid: "a1",
|
|
permissionMode: "plan",
|
|
message: { role: "assistant", content: "hi" },
|
|
}),
|
|
);
|
|
ok(asst[0] && !("mode" in asst[0]), "only your messages carry a mode");
|
|
ok(CYCLE_MODE_KEY === "\x1b[Z", "the switch key is Shift+Tab");
|
|
}
|
|
|
|
{
|
|
// Which model answered is read off the log, and named in words
|
|
// (2026-10-15: "I have no idea what model is being used").
|
|
const st = newTranscriptState();
|
|
const answer = (uuid: string, model: string) =>
|
|
reduceTranscriptLine(
|
|
st,
|
|
JSON.stringify({
|
|
type: "assistant",
|
|
uuid,
|
|
message: { role: "assistant", model, content: "done" },
|
|
}),
|
|
);
|
|
const items = [...answer("m1", "claude-sonnet-4-5-20250929"), ...answer("m2", "claude-opus-5-5")];
|
|
ok(sessionModel(items) === "claude-opus-5-5", "the newest answer's model wins");
|
|
ok(
|
|
!("model" in (answer("m3", "<synthetic>")[0] ?? {})),
|
|
"Claude Code's own synthetic lines name no model",
|
|
);
|
|
ok(modelLabel("claude-opus-5-5") === "Opus 5.5", "an id reads as a name");
|
|
ok(modelLabel("claude-sonnet-4-5-20250929") === "Sonnet 4.5", "a dated id drops the date");
|
|
ok(modelLabel("claude-3-5-haiku-20241022") === "Haiku 3.5", "the old id order still reads");
|
|
ok(modelLabel("gpt-5") === "gpt-5", "an unknown id is shown as given, not guessed");
|
|
}
|
|
|
|
console.log(`${pass}/${pass + fail} claude-transcript cases passed`);
|
|
if (fail > 0) process.exit(1);
|