diff --git a/docs/cli.md b/docs/cli.md index 2942d418..189e4584 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -336,6 +336,15 @@ history refresh without inventing text deltas or ending a queued prompt. Native step markers may include `step.turn_id`; a terminal marker for an older turn must not clear a newer turn's Stop control or release its queue slot. +Native context compaction is reported as `chat_event.kind="compaction"`, with +`compaction: {status: "running" | "completed" | "failed", id?: string}`. +This is session activity and may have no `message_id`: clients must not invent +an assistant message or tool call for it. Show **Compacting context…** while +running; completion clears that phase but does not itself end the model turn. +A new subscriber receives any retained active compaction. Clear stale activity +before reconnecting, then preserve fresh replayed activity while history loads. +This reports native observations only; it does not request or trigger compaction. + The brokered OpenCode chat exposes the operator's own sessions, separate from the sessions pipeline runs create. The protocol types are: diff --git a/web/PARITY.md b/web/PARITY.md index c2136c8d..39703495 100644 --- a/web/PARITY.md +++ b/web/PARITY.md @@ -8,6 +8,24 @@ test, or a code change without a capture, is `partial` at best. Status legend: `todo` · `partial` (some of it works) · `done` (working + captured). +### Context compaction activity (2026-10-20) + +**Partial against the live-provider gate.** A native compaction frame shows +**Compacting context…** with the active turn's spinner and Stop control, even +before the first answer or alongside existing reasoning/tools. Previously stored +reasoning is labeled Reasoning, not actively Thinking, while context compacts. +The label clears on compaction completion/failure; that does not finish the +surrounding model turn or count as a tool/message. Disconnect/turn settlement +clear stale activity. Reconnect clears stale state before subscribing, then keeps +fresh replayed activity while authoritative history loads. + +`test/chat-compaction-browser.mjs` captures before-answer and with-history states +at 390/1100px plus cleared-on-disconnect. It checks polite status announcements, +no horizontal overflow, preserved message counts and Stop lifecycle. Captures: +approved OpenCode temporary `chat-compaction-captures/` directory. Fixtures do not +trigger compaction, inference or changes to real chats. A server update is needed +for the new native activity frames; shell cache v11 refreshes the browser modules. + ### Tool summary error scope (2026-10-20) The aggregate **N tools** label stays neutral and borderless even when a call diff --git a/web/chat.css b/web/chat.css index 1f4298c3..ce1b3ed6 100644 --- a/web/chat.css +++ b/web/chat.css @@ -512,6 +512,9 @@ .chat-activity-elapsed { flex: none; } .chat-activity-sep { color: var(--muted); flex: none; } .chat-activity-head > .chat-tool-chip { min-width: 0; overflow: hidden; } +.chat-activity-compacting { flex-wrap: wrap; row-gap: 0; } +.chat-compaction-break { flex-basis: 100%; height: 0; } +.chat-compaction-status { color: var(--accent); } .chat-jump { position: absolute; right: 14px; bottom: 14px; border-color: var(--accent); color: var(--accent); } diff --git a/web/src/chat.js b/web/src/chat.js index 9f4a284c..adace062 100644 --- a/web/src/chat.js +++ b/web/src/chat.js @@ -1209,6 +1209,7 @@ export function mergeHistoryPage(previous, page, { older = false } = {}) { /** ChatEvent.Kind values pushed on the `chat_event` stream (internal/protocol/chat.go). */ export const EVENT_STEP = "step"; +export const EVENT_COMPACTION = "compaction"; export const EVENT_USAGE = "usage"; export const EVENT_USER = "user"; export const EVENT_SHELL = "shell"; @@ -1222,6 +1223,7 @@ export const STEP_ENDED = "ended"; export const TURN_PHASE_THINKING = "thinking"; export const TURN_PHASE_TOOL = "tool"; export const TURN_PHASE_WAITING = "waiting"; +export const TURN_PHASE_COMPACTING = "compacting"; /** * The live-stream badge words, shared verbatim with `liveBadgeState` / @@ -1396,6 +1398,7 @@ export function newChatThread(sessionId = "") { error: "", running: false, runningSince: 0, + compaction: null, toolStartedAt: {}, lastDeltaAt: 0, queued: false, @@ -1430,6 +1433,7 @@ export function settledThread(thread) { ...(thread ?? {}), running: false, runningSince: 0, + compaction: null, toolStartedAt: {}, lastDeltaAt: 0, queued: false, @@ -1555,6 +1559,7 @@ export function turnPhase(tools) { /** The running indicator's headline for [phase], naming what is happening. */ export function turnPhaseLabel(phase) { + if (phase === TURN_PHASE_COMPACTING) return "Compacting context…"; if (phase === TURN_PHASE_THINKING) return "Thinking…"; if (phase === TURN_PHASE_TOOL) return "Running a tool…"; return "Waiting…"; @@ -1567,6 +1572,7 @@ export function turnPhaseLabel(phase) { */ export function threadPhase(thread, nowMillis) { if (!thread?.running) return null; + if (thread.compaction?.status === "running") return TURN_PHASE_COMPACTING; if (thread.queued) return TURN_PHASE_WAITING; return turnPhase(threadRunningTools(thread.messages)); } @@ -1609,6 +1615,7 @@ export function applyChatForm(thread, form) { * targets is created on first sight, so a live turn never gains a second bubble. */ export function applyChatEvent(thread, frame, now = Date.now()) { + if (frame?.kind === EVENT_COMPACTION) return applyChatCompaction(thread, frame, now); if (frame?.kind === EVENT_RESYNC) return thread ?? newChatThread(str(frame?.session_id)); if (frame?.kind === EVENT_STEP && frame.step?.status === STEP_ENDED && frame.step?.turn_id && frame.step.turn_id !== thread?.nativeTurnId) return thread ?? newChatThread(str(frame?.session_id)); const current = thread?.queued @@ -1641,6 +1648,21 @@ export function applyChatEvent(thread, frame, now = Date.now()) { } } +/** Native session activity, not a synthetic tool call or assistant message. */ +function applyChatCompaction(thread, frame, now) { + const current = thread ?? newChatThread(str(frame?.session_id)); + if (current.sessionId && frame.session_id !== current.sessionId) return current; + const incoming = frame.compaction; + if (!incoming || !["running", "completed", "failed"].includes(incoming.status)) return current; + if (incoming.status === "running") { + return setThreadRunning({ ...current, compaction: { id: str(incoming.id), status: "running" } }, true, now); + } + if (!current.compaction || (incoming.id && current.compaction.id && incoming.id !== current.compaction.id)) return current; + // A compacted context can immediately continue the same model turn. Only a + // real final step/interrupt/disconnect settles the turn and its Stop control. + return { ...current, compaction: null }; +} + /** * Folds a user turn another client sent into the transcript, in place, once. The * server never echoes this client's own prompts, so the sender's own message is @@ -1740,8 +1762,10 @@ function upsertParts(thread, frame, now, deltaArrivedAt, upsert) { * dropped too (the reconnect re-fetches `chat_shells`), exactly like the Android * `streamResumed` path. */ -export function historyResync(thread, page) { - const settled = resumedThread(thread ?? newChatThread()); +export function historyResync(thread, page, { preserveActivity = false } = {}) { + // Reconnect callers clear stale activity BEFORE reattaching. Anything learned + // after that reset is fresh native state and must survive the history read. + const settled = preserveActivity ? (thread ?? newChatThread()) : resumedThread(thread ?? newChatThread()); return { ...settled, messages: (page?.messages ?? []).slice(), @@ -1951,7 +1975,7 @@ export function composerSubmit(thread, selected = 0) { * (mirrors ChatStore.event). A null frame leaves the flag untouched. */ export function queuedAfterFrame(queued, frame) { - if (frame == null) return queued === true; + if (frame == null || frame.kind === EVENT_COMPACTION) return queued === true; return false; } @@ -1962,6 +1986,7 @@ export function queuedAfterFrame(queued, frame) { * running bit, which the composer's Stop control reads. */ export function runningAfterFrame(running, frame) { + if (frame?.kind === EVENT_COMPACTION && frame.compaction?.status === "running") return true; if (frame?.kind === "step" && frame?.step?.status === "ended" && !frame?.step?.intermediate) return false; return running === true; } diff --git a/web/src/views/chat.js b/web/src/views/chat.js index 8628751e..6b8a2622 100644 --- a/web/src/views/chat.js +++ b/web/src/views/chat.js @@ -158,6 +158,7 @@ import { threadPhase, threadToolElapsedMillis, TURN_PHASE_WAITING, + TURN_PHASE_COMPACTING, toolCallCount, toolCallRow, toolCallsAnnouncement, @@ -1017,12 +1018,12 @@ function shellOutputFailed(sessionId, shellId, message) { async function resyncOpenChat() { const id = state.chatActiveId; if (!id || state.connection.kind !== "connected") return; - await subscribeChatSession(id); const previous = state.chatThreads.get(id) ?? newChatThread(id); state.chatThreads.set(id, resumedThread(previous)); + await subscribeChatSession(id); try { const page = await repo.chatHistory(id); - state.chatThreads.set(id, historyResync(previous, page)); + state.chatThreads.set(id, historyResync(state.chatThreads.get(id), page, { preserveActivity: true })); } catch { // The history refresh failed; the settled thread stands and the badge is honest. } @@ -3400,6 +3401,8 @@ function renderChatActivity(turn, streaming = false, elapsedMillis = () => 0, th class: `chat-reasoning chat-activity${streaming ? " chat-reasoning-live" : ""}`, }); const head = h("div", { class: "chat-activity-head" }); + const compacting = streaming && threadPhase(thread, Date.now()) === TURN_PHASE_COMPACTING; + if (compacting) head.classList.add("chat-activity-compacting"); if (streaming) { const spinner = h("span", { class: "spinner chat-activity-spinner", @@ -3409,9 +3412,18 @@ function renderChatActivity(turn, streaming = false, elapsedMillis = () => 0, th spinner.style.animationDelay = `-${(performance.now() % 800) / 1000}s`; head.append(spinner); } + if (compacting) { + head.append(h("span", { + class: "chat-activity-phase chat-compaction-status", + role: "status", + "aria-live": "polite", + text: turnPhaseLabel(TURN_PHASE_COMPACTING), + })); + if (steps.length || parts.length) head.append(h("span", { class: "chat-compaction-break", "aria-hidden": "true" })); + } if (steps.length > 0) { - head.append(renderChatReasoningToggle(turn, streaming, steps)); - } else if (streaming) { + head.append(renderChatReasoningToggle(turn, streaming && !compacting, steps)); + } else if (streaming && !compacting) { head.append( h("span", { class: "chat-activity-phase", @@ -3420,7 +3432,7 @@ function renderChatActivity(turn, streaming = false, elapsedMillis = () => 0, th ); } if (parts.length > 0) { - if (head.childElementCount > 0) head.append(h("span", { class: "chat-activity-sep", text: "\u00b7" })); + if (steps.length > 0 || (streaming && !compacting)) head.append(h("span", { class: "chat-activity-sep", text: "\u00b7" })); head.append(renderChatToolChip(turn, { streaming, elapsedMillis })); } const elapsed = streaming diff --git a/web/sw.js b/web/sw.js index d3554c45..1347ac02 100644 --- a/web/sw.js +++ b/web/sw.js @@ -1,7 +1,7 @@ // A shell cache for an installed Pipelines app. The API (the WebSocket proxy and // artifact files) is never cached; static files prefer the network and fall back // to the last good copy so an installed app still opens when the server is away. -const CACHE = "pipelines-shell-v10"; +const CACHE = "pipelines-shell-v11"; const SHELL = ["/", "/index.html", "/manifest.webmanifest", "/favicon.svg"]; self.addEventListener("install", (event) => { diff --git a/web/test/chat-compaction-browser.mjs b/web/test/chat-compaction-browser.mjs new file mode 100644 index 00000000..5edaf8ea --- /dev/null +++ b/web/test/chat-compaction-browser.mjs @@ -0,0 +1,68 @@ +// Native compaction fixture through the real chat fold/view; no harness calls. +import assert from "node:assert/strict"; +import { createServer } from "node:http"; +import { readFile, mkdir } from "node:fs/promises"; +import { resolve, extname, dirname } from "node:path"; +import { fileURLToPath } from "node:url"; +import { launchChrome } from "./browser.mjs"; + +const root = resolve(dirname(fileURLToPath(import.meta.url)), ".."); +const out = resolve(process.argv[2] ?? "captures-chat-compaction"); +await mkdir(out, {recursive:true}); +const html = ` +${["design/tokens/tokens","base","shell","lists","run-detail","chat","dialogs"].map(n=>``).join("")} +