Merge branch 'main' into web-chat-composer

# Conflicts:
#	web/src/app.js
#	web/src/client.js
#	web/src/protocol.js
#	web/test/chat.test.mjs
#	web/test/repository.test.mjs
#	web/test/ui-smoke.mjs
This commit is contained in:
2026-10-19 07:33:49 +01:00
14 changed files with 1840 additions and 39 deletions
+286
View File
@@ -16,6 +16,11 @@ import {
MAX_RENDERED_DIFF_LINES,
TOOL_DETAIL_MAX_LENGTH,
TOOL_LONG_RUNNING_MILLIS,
TURN_PHASE_THINKING,
TURN_PHASE_TOOL,
TURN_PHASE_WAITING,
TURN_THINKING_WINDOW_MILLIS,
applyChatEvent,
attachmentDisplayName,
attachmentError,
attachmentFiles,
@@ -30,17 +35,22 @@ import {
chatNoMatchMessage,
chatOpensAtNewest,
chatShowJumpToLatest,
chatStreamState,
closeChatConfirmation,
composerAction,
composerCanSend,
composerSendEnabled,
composerSubmit,
deltaPartId,
directoryLabel,
filterSessions,
formatToolDuration,
groupSessionsByDirectory,
historyResync,
lineDiff,
mergeHistoryPage,
mergeToolPart,
newChatThread,
parseChatInput,
partRendering,
prettyToolInput,
@@ -48,6 +58,7 @@ import {
reasoningLabel,
runningAfterFrame,
runningToolElapsedMillis,
runningToolStamps,
sessionActivity,
sessionActivityBadge,
sessionChipLabel,
@@ -56,6 +67,11 @@ import {
sessionStatusLabel,
sessionTitle,
sessionWorkLabel,
settledThread,
threadElapsedMillis,
threadPhase,
threadRunningTools,
threadToolElapsedMillis,
toolCallCount,
toolCallRow,
toolCallsAnnouncement,
@@ -71,6 +87,8 @@ import {
toolsRunning,
transcriptRows,
turnElapsedMillis,
turnPhase,
turnPhaseLabel,
} from "../src/chat.js";
import { ChangeLineKind } from "../src/changes.js";
@@ -882,3 +900,271 @@ test("chatCommandEffect reports the exact Android error strings", () => {
text: "Can't start a new session here: the server reported no directory for this chat.",
});
});
// --- C5: live stream folding ------------------------------------------------
// Every frame below is the server's `chat_event` shape (internal/protocol/chat.go
// ChatEventFrame). The synthetic frames prove the pure fold; the ui-smoke replays
// the same shapes against the real page.
const evt = (kind, extra = {}) => ({ session_id: "s1", kind, ...extra });
test("a text delta appends to the part the first frame started", () => {
let thread = newChatThread("s1");
thread = applyChatEvent(thread, evt("text", { message_id: "a1", part: { id: "a1#text#0" }, delta: "Hel" }), 1000);
thread = applyChatEvent(thread, evt("text", { message_id: "a1", part: { id: "a1#text#0" }, delta: "lo" }), 1001);
assert.equal(thread.messages.length, 1);
assert.equal(thread.messages[0].role, "assistant");
assert.equal(thread.messages[0].parts.length, 1);
assert.equal(thread.messages[0].parts[0].text, "Hello");
assert.equal(thread.running, true);
assert.equal(thread.runningSince, 1000);
assert.equal(thread.lastDeltaAt, 1001);
});
test("a text delta and a reasoning delta sharing a bare id land in separate parts", () => {
let thread = newChatThread("s1");
thread = applyChatEvent(thread, evt("text", { message_id: "a1", part: { id: "x" }, delta: "answer" }), 1000);
thread = applyChatEvent(thread, evt("reasoning", { message_id: "a1", part: { id: "x" }, delta: "think" }), 1001);
const parts = thread.messages[0].parts;
assert.equal(parts.length, 2);
const text = parts.find((part) => part.type === "text");
const reasoning = parts.find((part) => part.type === "reasoning");
assert.equal(text.id, "x");
assert.equal(text.text, "answer");
assert.equal(reasoning.id, "x#reasoning");
assert.equal(reasoning.text, "think");
// The id derivation is the one the broker uses once the kinds are distinct.
assert.equal(deltaPartId(parts, "text", "x"), "x");
assert.equal(deltaPartId(parts, "reasoning", "x"), "x#reasoning");
});
test("tool frames merge field by field without blanking what an earlier frame taught", () => {
let thread = newChatThread("s1");
thread = applyChatEvent(
thread,
evt("tool", {
message_id: "a1",
part: { type: "tool", id: "t1", name: "read", state: { status: "running", input: { filePath: "a.js" } } },
}),
1000,
);
// A later frame carries the result but no name and no input: both are retained.
thread = applyChatEvent(
thread,
evt("tool", {
message_id: "a1",
part: { type: "tool", id: "t1", state: { status: "completed", content: [{ type: "text", text: "done" }] } },
}),
1001,
);
// A final frame carries the error but no content: the content is retained too.
thread = applyChatEvent(
thread,
evt("tool", {
message_id: "a1",
part: { type: "tool", id: "t1", state: { status: "error", error: { message: "boom" } } },
}),
1002,
);
const tool = thread.messages[0].parts.find((part) => part.type === "tool");
assert.equal(tool.name, "read");
assert.deepEqual(tool.state.input, { filePath: "a.js" });
assert.equal(tool.state.content[0].text, "done");
assert.equal(tool.state.error.message, "boom");
assert.equal(tool.state.status, "error");
// And the pure merge keeps a non-empty incoming field while retaining the prior.
const merged = mergeToolPart(
{ type: "tool", id: "t1", name: "read", executed: true, state: { status: "running", input: { a: 1 } } },
{ type: "tool", id: "t1", name: "", state: { status: "", input: { b: 2 } } },
);
assert.equal(merged.name, "read");
assert.equal(merged.executed, true);
assert.equal(merged.state.status, "running");
assert.deepEqual(merged.state.input, { b: 2 });
});
test("a repeated user message id adds no second turn and keeps a non-blank client", () => {
let thread = newChatThread("s1");
const frame = evt("user", { message_id: "u9", part: { text: "hi there" }, client: "Pixel" });
thread = applyChatEvent(thread, frame, 1000);
thread = applyChatEvent(thread, frame, 1001);
assert.equal(thread.messages.filter((message) => message.role === "user").length, 1);
assert.equal(thread.messages[0].client, "Pixel");
assert.equal(thread.messages[0].parts[0].text, "hi there");
// Another id with no text or no id at all is ignored.
thread = applyChatEvent(thread, evt("user", { message_id: "u10", part: { text: "" } }), 1002);
thread = applyChatEvent(thread, evt("user", { part: { text: "no id" } }), 1003);
assert.equal(thread.messages.length, 1);
});
test("step started runs the turn and step ended settles it", () => {
let thread = newChatThread("s1");
thread = applyChatEvent(thread, evt("step", { step: { status: "started", index: 1 } }), 1000);
assert.equal(thread.running, true);
assert.equal(thread.runningSince, 1000);
// A tool call in flight leaves a stamp; ending the step clears running and the stamp.
thread = applyChatEvent(
thread,
evt("tool", { message_id: "a1", part: { type: "tool", id: "t1", state: { status: "running" } } }),
1001,
);
assert.equal(thread.toolStartedAt.t1, 1001);
thread = applyChatEvent(thread, evt("step", { step: { status: "ended" } }), 2000);
assert.equal(thread.running, false);
assert.equal(thread.runningSince, 0);
assert.deepEqual(thread.toolStartedAt, {});
assert.equal(thread.lastDeltaAt, 0);
});
test("any frame clears the queued flag", () => {
for (const frame of [
evt("text", { message_id: "a1", part: { id: "p" }, delta: "x" }),
evt("step", { step: { status: "started" } }),
evt("usage", { usage: { input_tokens: 1 } }),
evt("shell", { shell: { id: "sh1", status: "running" } }),
]) {
const queued = { ...newChatThread("s1"), queued: true };
assert.equal(applyChatEvent(queued, frame, 1000).queued, false);
}
});
test("a shell frame never marks the thread running", () => {
const thread = applyChatEvent(
newChatThread("s1"),
evt("shell", { shell: { id: "sh1", status: "running", command: "go test ./..." } }),
1000,
);
assert.equal(thread.running, false);
assert.equal(thread.runningSince, 0);
});
test("a usage frame changes nothing", () => {
const before = newChatThread("s1");
const after = applyChatEvent(before, evt("usage", { usage: { input_tokens: 5, cost: 0.01 } }), 1000);
assert.deepEqual(after, before);
});
test("settling a thread (a socket drop) clears running, its stamps and queued", () => {
let thread = newChatThread("s1");
thread = applyChatEvent(thread, evt("text", { message_id: "a1", part: { id: "p" }, delta: "x" }), 1000);
thread = applyChatEvent(
thread,
evt("tool", { message_id: "a1", part: { type: "tool", id: "t1", state: { status: "running" } } }),
1001,
);
assert.equal(thread.running, true);
assert.equal(thread.toolStartedAt.t1, 1001);
const settled = settledThread(thread);
assert.equal(settled.running, false);
assert.equal(settled.runningSince, 0);
assert.deepEqual(settled.toolStartedAt, {});
assert.equal(settled.lastDeltaAt, 0);
assert.equal(settled.queued, false);
// The conversation itself is untouched by settling.
assert.equal(settled.messages.length, 1);
});
test("historyResync replaces the turn wholesale and settles it", () => {
const held = {
...newChatThread("s1"),
messages: [assistant("a1", [{ type: "text", text: "stale" }])],
nextCursor: "old",
hasMore: true,
loaded: true,
running: true,
runningSince: 555,
toolStartedAt: { t1: 555 },
lastDeltaAt: 600,
};
const page = {
messages: [user("u1", "hi"), assistant("a2", [{ type: "text", text: "fresh" }])],
nextCursor: "",
hasMore: false,
};
const resynced = historyResync(held, page);
assert.deepEqual(resynced.messages, page.messages);
assert.ok(!resynced.messages.some((message) => message.id === "a1"));
assert.equal(resynced.nextCursor, "");
assert.equal(resynced.hasMore, false);
assert.equal(resynced.loaded, true);
assert.equal(resynced.running, false);
assert.equal(resynced.runningSince, 0);
assert.deepEqual(resynced.toolStartedAt, {});
assert.equal(resynced.lastDeltaAt, 0);
});
test("the chat stream badge uses the live / reconnecting / not live words", () => {
assert.deepEqual(chatStreamState("connected", true), { tone: "live", label: "live" });
assert.equal(chatStreamState("connected", false).label, "not live");
assert.equal(chatStreamState("connecting", true).label, "reconnecting");
assert.equal(chatStreamState("disconnected", true).label, "reconnecting");
assert.equal(chatStreamState("idle", false).label, "not live");
});
test("the turn phase names what is happening", () => {
assert.equal(turnPhase([], 1000), TURN_PHASE_THINKING);
assert.equal(turnPhaseLabel(TURN_PHASE_THINKING), "Thinking…");
assert.equal(turnPhase([{ type: "tool", id: "t", state: { status: "running" } }], 100000), TURN_PHASE_TOOL);
assert.equal(turnPhaseLabel(TURN_PHASE_TOOL), "Running a tool…");
assert.equal(turnPhase([], null), TURN_PHASE_WAITING);
assert.equal(turnPhase([], TURN_THINKING_WINDOW_MILLIS), TURN_PHASE_WAITING);
assert.equal(turnPhaseLabel(TURN_PHASE_WAITING), "Waiting…");
});
test("only a call at or past 60s reads as long", () => {
assert.equal(toolLongRunning(TOOL_LONG_RUNNING_MILLIS - 1), false);
assert.equal(toolLongRunning(TOOL_LONG_RUNNING_MILLIS), true);
assert.equal(TOOL_LONG_RUNNING_MILLIS, 60000);
});
test("a live turn interleaving reasoning, text and tool frames folds to one agent row", () => {
let thread = newChatThread("live");
const frames = [
evt("reasoning", { message_id: "a1", part: { id: "r0" }, delta: "think" }),
evt("text", { message_id: "a1", part: { id: "t0" }, delta: "Hel" }),
evt("tool", { message_id: "a1", part: { type: "tool", id: "tool1", name: "read", state: { status: "running" } } }),
evt("tool", {
message_id: "a1",
part: { type: "tool", id: "tool1", state: { status: "completed", content: [{ type: "text", text: "ok" }] } },
}),
evt("reasoning", { message_id: "a2", part: { id: "r0" }, delta: "more" }),
evt("text", { message_id: "a2", part: { id: "t0" }, delta: "lo" }),
];
for (const frame of frames) thread = applyChatEvent(thread, frame, 1000);
const rows = transcriptRows(thread.messages);
const agentRows = rows.filter((row) => row.kind === "agent");
assert.equal(agentRows.length, 1);
assert.equal(agentRows[0].key, "a1");
assert.deepEqual(agentRows[0].turn.reasoningSteps, ["think", "more"]);
assert.equal(agentRows[0].turn.tools.length, 1);
assert.equal(agentRows[0].turn.answer, "Hel\n\nlo");
assert.equal(threadRunningTools(thread.messages).length, 0);
});
test("the running indicator's elapsed and per-tool timing use the thread's own stamps", () => {
let thread = newChatThread("s1");
thread = applyChatEvent(thread, evt("step", { step: { status: "started" } }), 1000);
thread = applyChatEvent(
thread,
evt("tool", {
message_id: "a1",
part: { type: "tool", id: "t1", name: "shell", state: { status: "running", input: { command: "go build" } } },
}),
1500,
);
assert.equal(threadElapsedMillis(thread, 3000), 2000);
assert.equal(formatToolDuration(threadToolElapsedMillis(thread, thread.messages[0].parts[0], 3000)), "1.5s");
assert.equal(threadPhase(thread, 3000), TURN_PHASE_TOOL);
// runningToolStamps keeps a stamp it already had and drops one that settled.
assert.deepEqual(runningToolStamps({ t1: 42 }, thread.messages, 9999), { t1: 42 });
assert.deepEqual(runningToolStamps({ t1: 42 }, [], 9999), {});
});