Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
import assert from "node:assert/strict";
import { register } from "node:module";
import { dirname, join } from "node:path";
import test from "node:test";
import { fileURLToPath, pathToFileURL } from "node:url";
const here = dirname(fileURLToPath(import.meta.url));
register(pathToFileURL(join(here, "helpers/ts-import-hooks.mjs")));
const {
assistantTurnContent,
assistantTurnResponseOutputTokens,
assistantTurnResponseOutputIsEstimated,
assistantTurnUsage,
buildTranscriptEntries,
messageThinking,
reuseTranscriptEntries,
subagentRunsEqual,
} = await import("../src/lib/assistant-turns.ts");
function message(id, role, content, extra = {}) {
return {
id,
role,
content,
createdAt: `2026-07-28T00:00:0${id.length}.000Z`,
...extra,
};
}
test("groups assistant fragments and tools into one conversational turn", () => {
const { entries } = buildTranscriptEntries([
message("user", "user", "Fix the issue"),
message("intro", "assistant", "I will inspect the code."),
message("read", "tool", "result", {
toolName: "Read",
toolCallId: "read",
}),
message("followup", "assistant", "The problem is in the renderer."),
message("edit", "tool", "done", {
toolName: "Edit",
toolCallId: "edit",
}),
message("final", "assistant", "Fixed and verified."),
]);
assert.equal(entries.length, 2);
assert.equal(entries[0].kind, "message");
assert.equal(entries[1].kind, "assistant-turn");
assert.deepEqual(
entries[1].parts.map((part) => part.kind),
["message", "activity", "message", "activity", "message"],
);
assert.equal(entries[1].anchorId, "intro");
assert.equal(
assistantTurnContent(entries[1]),
"I will inspect the code.\n\nThe problem is in the renderer.\n\nFixed and verified.",
);
});
test("assistant turn output prefers exact usage and falls back to stopped estimates", () => {
const { entries } = buildTranscriptEntries([
message("user", "user", "Stop"),
message("first", "assistant", "Partial", { responseOutputTokens: 7 }),
message("second", "assistant", "Done", {
usage: { inputTokens: 10, outputTokens: 3, totalTokens: 13 },
responseOutputTokens: 99,
}),
]);
const turn = entries[1];
assert.equal(turn.kind, "assistant-turn");
assert.equal(assistantTurnResponseOutputTokens(turn), 10);
assert.equal(assistantTurnResponseOutputIsEstimated(turn), true);
});
test("keeps a recovered tool error inside one successful assistant turn", () => {
const { entries } = buildTranscriptEntries([
message("user", "user", "Inspect the handlers"),
message("read", "tool", "directory", {
toolName: "Read",
toolCallId: "read",
toolStatus: "error",
isError: true,
}),
message("recovery", "assistant", "I will list the directory instead."),
message("glob", "tool", "router.go", {
toolName: "Glob",
toolCallId: "glob",
toolStatus: "success",
}),
message("final", "assistant", "The handler is registered in router.go."),
]);
assert.equal(entries.length, 2);
const turn = entries[1];
assert.equal(turn.kind, "assistant-turn");
assert.deepEqual(
turn.parts.map((part) => part.kind),
["activity", "message", "activity", "message"],
);
assert.equal(turn.parts[0].items[0].message.toolStatus, "error");
assert.equal(turn.parts[0].items[0].message.isError, true);
assert.equal(turn.parts[2].items[0].message.toolStatus, "success");
assert.match(assistantTurnContent(turn), /registered in router\.go/);
});
test("starts a new assistant turn only after the next user message", () => {
const { entries } = buildTranscriptEntries([
message("user-1", "user", "First"),
message("assistant-1", "assistant", "First response"),
message("user-2", "user", "Second"),
message("assistant-2", "assistant", "Second response"),
]);
assert.deepEqual(
entries.map((entry) => entry.kind),
["message", "assistant-turn", "message", "assistant-turn"],
);
});
test("keeps thinking and tool-only activity in the assistant turn", () => {
const { entries } = buildTranscriptEntries([
message("user", "user", "Inspect"),
message("thinking", "assistant", "", { thinking: "Planning" }),
message("tool", "tool", "result", {
toolName: "Read",
toolCallId: "tool",
}),
message("answer", "assistant", "Done"),
]);
const turn = entries[1];
assert.equal(turn.kind, "assistant-turn");
assert.equal(turn.parts[0].kind, "activity");
assert.deepEqual(
turn.parts[0].items.map((item) => item.kind),
["thinking", "tool"],
);
assert.equal(turn.parts[0].endedAt, "2026-07-28T00:00:06.000Z");
});
test("aggregates provider usage across response fragments", () => {
const usage = {
inputTokens: 10,
outputTokens: 4,
cacheReadTokens: 2,
totalTokens: 14,
};
const { entries } = buildTranscriptEntries([
message("user", "user", "Count"),
message("first", "assistant", "One", { usage }),
message("tool", "tool", "result", { toolCallId: "tool" }),
message("second", "assistant", "Two", { usage }),
]);
const turn = entries[1];
assert.equal(turn.kind, "assistant-turn");
assert.deepEqual(assistantTurnUsage(turn), {
inputTokens: 20,
outputTokens: 8,
cacheReadTokens: 4,
totalTokens: 28,
});
});
test("nests delegate rows under the Task call that spawned them", () => {
const { entries, visible } = buildTranscriptEntries([
message("user", "user", "Audit the store"),
message("task", "tool", "report", {
toolName: "Task",
toolCallId: "task-1",
}),
message("delegate-think", "assistant", "", {
thinking: "Looking for the reducer",
parentToolCallId: "task-1",
agentName: "code-reviewer",
}),
message("delegate-read", "tool", "file", {
toolName: "Read",
toolCallId: "read-1",
parentToolCallId: "task-1",
agentName: "code-reviewer",
}),
message("delegate-report", "assistant", "Two dead branches.", {
parentToolCallId: "task-1",
agentName: "code-reviewer",
}),
message("final", "assistant", "Removed both."),
]);
// The delegate's rows are not transcript rows: the minimap and the turn
// stream only ever see the parent's `Task` call.
assert.deepEqual(
visible.map((entry) => entry.id),
["user", "task", "final"],
);
const turn = entries[1];
assert.equal(turn.kind, "assistant-turn");
const activity = turn.parts[0];
assert.equal(activity.kind, "activity");
assert.equal(activity.items.length, 1);
assert.equal(activity.items[0].message.id, "task");
assert.equal(activity.items[0].delegate.agentName, "code-reviewer");
assert.deepEqual(
activity.items[0].delegate.items.map((item) => item.kind),
["thinking", "tool", "answer"],
);
});
test("parallel delegate nodes keep parent Task order when child rows interleave", () => {
const { entries } = buildTranscriptEntries([
message("user", "user", "Fan out"),
message("task-a", "tool", "first report", {
toolName: "Task",
toolCallId: "task-a",
}),
message("task-b", "tool", "second report", {
toolName: "Task",
toolCallId: "task-b",
}),
message("b-read", "tool", "b", {
toolName: "Read",
parentToolCallId: "task-b",
agentName: "scout-b",
}),
message("a-read", "tool", "a", {
toolName: "Read",
parentToolCallId: "task-a",
agentName: "scout-a",
}),
message("final", "assistant", "Both finished."),
]);
const activity = entries[1].parts[0];
assert.equal(activity.kind, "activity");
assert.deepEqual(
activity.items.map((item) => item.message.id),
["task-a", "task-b"],
);
assert.deepEqual(
activity.items.map((item) => item.delegate.agentName),
["scout-a", "scout-b"],
);
});
test("a delegate turn with both reasoning and text keeps both rows", () => {
const { entries } = buildTranscriptEntries([
message("user", "user", "Delegate"),
message("task", "tool", "report", { toolName: "Task", toolCallId: "t" }),
message("both", "assistant", "Here is the report.", {
thinking: "Summarizing",
parentToolCallId: "t",
}),
]);
const delegate = entries[1].parts[0].items[0].delegate;
assert.equal(delegate.agentName, undefined);
assert.deepEqual(
delegate.items.map((item) => item.kind),
["thinking", "answer"],
);
});
test("a resumed delegation continues in the latest Task card (ADR 0279)", () => {
const { entries, visible } = buildTranscriptEntries([
message("user", "user", "Audit the store"),
message("task-1", "tool", "report", {
toolName: "Task",
toolCallId: "task-1",
toolArgs: { agent: "explorer", task: "Explore the parser." },
toolResult: { details: { delegationId: "del-1" } },
}),
message("delegate-1", "assistant", "Found the parser.", {
parentToolCallId: "task-1",
agentName: "explorer",
}),
message("task-2", "tool", "report", {
toolName: "Task",
toolCallId: "task-2",
toolArgs: { agent: "explorer", task: "Now cover the lexer.", resume: "del-1" },
toolResult: { details: { delegationId: "del-2" } },
}),
message("delegate-2", "assistant", "Covered the lexer.", {
parentToolCallId: "task-2",
agentName: "explorer",
}),
message("final", "assistant", "Both halves are covered."),
]);
// Each `Task` call still owns its own row in the parent's turn stream…
assert.deepEqual(
visible.map((entry) => entry.id),
["user", "task-1", "task-2", "final"],
);
const turn = entries[1];
const activity = turn.parts[0];
assert.equal(activity.kind, "activity");
assert.equal(activity.items.length, 2);
// …but the chain's rows all live on the latest card, in production order, so
// a resumed run reads as one continuing conversation instead of a card that
// starts from nothing.
assert.equal(activity.items[0].delegate, undefined);
assert.equal(activity.items[1].delegate.agentName, "explorer");
assert.deepEqual(
activity.items[1].delegate.items.map((item) => item.message.id),
["delegate-1", "delegate-2"],
);
});
test("a resume link whose parent Task row is gone leaves the card intact", () => {
const { entries } = buildTranscriptEntries([
message("user", "user", "Audit the store"),
message("task-2", "tool", "report", {
toolName: "Task",
toolCallId: "task-2",
toolArgs: { agent: "explorer", task: "More.", resume: "del-1" },
toolResult: { details: { delegationId: "del-2" } },
}),
message("delegate-2", "assistant", "More.", {
parentToolCallId: "task-2",
agentName: "explorer",
}),
]);
const delegate = entries[1].parts[0].items[0].delegate;
assert.deepEqual(
delegate.items.map((item) => item.message.id),
["delegate-2"],
);
});
function mark(id, throughMessageId, overrides = {}) {
return {
id,
throughMessageId,
generation: 1,
summaryTokens: 900,
summarized: true,
...overrides,
};
}
test("a compaction row divides the transcript right after the message it covers", () => {
const { entries } = buildTranscriptEntries(
[
message("user-1", "user", "First"),
message("assistant-1", "assistant", "First response"),
message("user-2", "user", "Second"),
message("assistant-2", "assistant", "Second response"),
],
[mark("cp-1", "assistant-1")],
);
assert.deepEqual(
entries.map((entry) => entry.kind),
["message", "assistant-turn", "compaction", "message", "assistant-turn"],
);
assert.equal(entries[2].mark.id, "cp-1");
});
test("a compaction row ends the assistant turn it lands inside", () => {
// The runtime splices its checkpoint after the anchor entry, so a later
// fragment of the same provider turn belongs to a new visual turn here too.
const { entries } = buildTranscriptEntries(
[
message("user", "user", "Work"),
message("before", "assistant", "Reading"),
message("tool", "tool", "result", { toolName: "Read", toolCallId: "t" }),
message("after", "assistant", "Done"),
],
[mark("cp-1", "tool")],
);
assert.deepEqual(
entries.map((entry) => entry.kind),
["message", "assistant-turn", "compaction", "assistant-turn"],
);
assert.equal(entries[3].id, "after");
});
test("every checkpoint gets its own row, and an orphaned one gets none", () => {
const { entries } = buildTranscriptEntries(
[
message("user-1", "user", "First"),
message("user-2", "user", "Second"),
],
[
mark("cp-1", "user-1"),
mark("cp-2", "user-2", { generation: 2, summarized: false }),
// Its anchor was rewritten away, so this checkpoint has nowhere to sit.
mark("cp-3", "gone"),
],
);
assert.deepEqual(
entries.map((entry) => entry.kind),
["message", "compaction", "message", "compaction"],
);
assert.deepEqual(
entries
.filter((entry) => entry.kind === "compaction")
.map((entry) => entry.mark.id),
["cp-1", "cp-2"],
);
});
test("delegate runs compare by rows so memoized groups still update", () => {
const rowA = message("a", "tool", "one", { toolCallId: "a" });
const rowB = message("b", "tool", "two", { toolCallId: "b" });
const run = (items, agentName) => ({
...(agentName ? { agentName } : {}),
items,
});
assert.equal(subagentRunsEqual(undefined, undefined), true);
assert.equal(subagentRunsEqual(run([]), undefined), false);
// Rebuilt on every render, so identity never matches: the rows must.
assert.equal(
subagentRunsEqual(
run([{ kind: "tool", message: rowA }], "reviewer"),
run([{ kind: "tool", message: rowA }], "reviewer"),
),
true,
);
assert.equal(
subagentRunsEqual(
run([{ kind: "tool", message: rowA }]),
run([{ kind: "tool", message: rowB }]),
),
false,
);
// A streamed row grew: same length, same message, different kind.
assert.equal(
subagentRunsEqual(
run([{ kind: "thinking", message: rowA }]),
run([{ kind: "answer", message: rowA }]),
),
false,
);
assert.equal(
subagentRunsEqual(
run([{ kind: "tool", message: rowA }], "reviewer"),
run([{ kind: "tool", message: rowA }], "planner"),
),
false,
);
});
test("parent tools after a Task fan-out stay out of the delegation card (D319)", () => {
const { entries } = buildTranscriptEntries([
message("user", "user", "Investigate"),
message("think-before", "assistant", "", { thinking: "I will delegate." }),
message("task-a", "tool", "running", {
toolName: "Task",
toolCallId: "task-a",
}),
message("task-b", "tool", "running", {
toolName: "Task",
toolCallId: "task-b",
}),
message("think-after", "assistant", "", {
thinking: "I will keep working in parallel.",
}),
message("read", "tool", "file", {
toolName: "Read",
toolCallId: "read",
}),
message("wait", "tool", "done", {
toolName: "TaskWait",
toolCallId: "wait",
}),
]);
const turn = entries[1];
assert.equal(turn.kind, "assistant-turn");
assert.deepEqual(
turn.parts.map((part) => [
part.kind,
part.items.map((item) => item.message.id),
]),
[
["activity", ["think-before"]],
["activity", ["task-a", "task-b"]],
["activity", ["think-after", "read", "wait"]],
],
);
});
test("reuses unchanged activity parts when only the tail thinking token changes", () => {
const history = [
message("user", "user", "Work through the files"),
];
for (let index = 0; index < 40; index += 1) {
history.push(
message(`read-${index}`, "tool", "ok", {
toolName: "Read",
toolCallId: `read-${index}`,
}),
);
}
history.push(message("mid", "assistant", "Continuing."));
const thinking = message("think-live", "assistant", "", {
thinking: "Looking at step 1",
status: "streaming",
});
const first = buildTranscriptEntries([...history, thinking]);
const nextThinking = {
...thinking,
thinking: "Looking at step 1 and 2",
};
const rebuilt = buildTranscriptEntries([...history, nextThinking]);
const shared = reuseTranscriptEntries(first.entries, rebuilt.entries);
const firstTurn = first.entries[1];
const sharedTurn = shared[1];
assert.equal(firstTurn.kind, "assistant-turn");
assert.equal(sharedTurn.kind, "assistant-turn");
const firstTools = firstTurn.parts[0];
const sharedTools = sharedTurn.parts[0];
assert.equal(firstTools.kind, "activity");
assert.equal(sharedTools.kind, "activity");
assert.equal(sharedTools, firstTools);
assert.equal(sharedTools.items[0], firstTools.items[0]);
assert.equal(sharedTools.items[39], firstTools.items[39]);
const firstMid = firstTurn.parts[1];
const sharedMid = sharedTurn.parts[1];
assert.equal(firstMid.kind, "message");
assert.equal(sharedMid, firstMid);
const firstThink = firstTurn.parts[2];
const sharedThink = sharedTurn.parts[2];
assert.equal(firstThink.kind, "activity");
assert.equal(sharedThink.kind, "activity");
assert.notEqual(sharedThink, firstThink);
assert.equal(sharedThink.items[0].message, nextThinking);
});
test("an echoed reasoning replay placeholder is not shown as thinking (#1945)", () => {
const placeholder = "[reasoning not retained for this turn]";
assert.equal(messageThinking(message("a", "assistant", "answer", { thinking: placeholder })), "");
assert.equal(
messageThinking(message("b", "assistant", "answer", { thinking: `${placeholder}real plan` })),
"real plan",
);
assert.equal(messageThinking(message("c", "assistant", "answer", { thinking: "real plan" })), "real plan");
// A placeholder-only row adds no thinking activity to its turn.
const { entries } = buildTranscriptEntries([
message("u", "user", "question"),
message("a", "assistant", "answer", { thinking: placeholder, status: "complete" }),
]);
const turn = entries.find((entry) => entry.kind === "assistant-turn");
assert.ok(turn);
assert.equal(JSON.stringify(turn).includes('"kind":"thinking"'), false);
});
test("autonomous conversations remain separate without synthetic user rows", () => {
const { entries, visible } = buildTranscriptEntries([
message("first", "assistant", "First round", { turnId: "root-1" }),
message("tool", "tool", "read result", { turnId: "root-1", toolName: "Read" }),
message("answer", "assistant", "First result", { turnId: "root-1" }),
message("second", "assistant", "Second round", { turnId: "root-2" }),
]);
assert.equal(entries.filter((entry) => entry.kind === "assistant-turn").length, 2);
assert.equal(visible.filter((row) => row.role === "user").length, 0);
});
test("legacy messages and delegates do not create extra root conversation boundaries", () => {
const { entries } = buildTranscriptEntries([
message("first", "assistant", "First round", { turnId: "root-1" }),
message("legacy", "assistant", "Same round", { turnId: " " }),
message("delegate", "assistant", "Delegate result", { turnId: "delegate-1", parentToolCallId: "task-1" }),
message("second", "assistant", "Second round", { turnId: "root-2" }),
]);
assert.equal(entries.filter((entry) => entry.kind === "assistant-turn").length, 2);
});