Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(transcript): reconcile only the matching optimistic prompt

vastsa3d9440e2d254
diff --git a/apps/desktop/src/lib/session-transcript.ts b/apps/desktop/src/lib/session-transcript.ts
index 03423caae..426a86674 100644
--- a/apps/desktop/src/lib/session-transcript.ts
+++ b/apps/desktop/src/lib/session-transcript.ts
@@ -15,6 +15,9 @@ type OptimisticFileReference = {
token?: string;
};
+const OPTIMISTIC_USER_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
+const MAX_OPTIMISTIC_ECHO_DELAY_MS = 30_000;
+
/**
* The user row shown the moment a prompt is sent, before the host has
* persisted or echoed it (D288). The host reuses `id`, so its echo replaces
@@ -203,36 +206,47 @@ export function mergeLiveSessionMessages(
const used = new Set<string>();
// An optimistic prompt whose reconcile event was missed survives with its
- // temporary id, so id-based merging replays it next to the durable echo of
- // the same prompt (#1308: prompt reappears after switching away and back).
- // Each durable completed user row grants one collapse credit: an orphan
- // live user row with equal text consumes one and is dropped. A genuinely
- // repeated prompt keeps every durable row because two sends persist two
- // rows while each live orphan still only consumes one credit.
- const durableUserTextCredits = new Map<string, number>();
- for (const message of durable) {
- if (message.role !== "user") continue;
- durableUserTextCredits.set(
- message.content,
- (durableUserTextCredits.get(message.content) ?? 0) + 1,
- );
+ // temporary UUID id, so id-based merging can replay it next to the durable
+ // echo (#1308). Content alone is not identity: only collapse an attachment-
+ // free optimistic row when one same-text durable user row was persisted
+ // shortly after that optimistic row. This keeps an older identical prompt
+ // from hiding a newly submitted one.
+ const matchedDurableUserIds = new Set<string>();
+ const collapsedOptimisticIds = new Set<string>();
+ for (const optimistic of liveNormalized) {
+ if (
+ optimistic.role !== "user" ||
+ optimistic.status !== "complete" ||
+ !OPTIMISTIC_USER_ID.test(optimistic.id) ||
+ durableIds.has(optimistic.id) ||
+ optimistic.attachments?.length
+ ) continue;
+ const optimisticTime = Date.parse(optimistic.createdAt ?? "");
+ if (!Number.isFinite(optimisticTime)) continue;
+ const candidates = durable
+ .filter((message) =>
+ message.role === "user" &&
+ !message.attachments?.length &&
+ !matchedDurableUserIds.has(message.id) &&
+ message.content === optimistic.content,
+ )
+ .map((message) => ({
+ message,
+ delay: Date.parse(message.createdAt ?? "") - optimisticTime,
+ }))
+ .filter(({ delay }) => delay >= 0 && delay <= MAX_OPTIMISTIC_ECHO_DELAY_MS)
+ .sort((left, right) => left.delay - right.delay);
+ if (!candidates.length) continue;
+ if (candidates.length > 1 && candidates[0].delay === candidates[1].delay) continue;
+ matchedDurableUserIds.add(candidates[0].message.id);
+ collapsedOptimisticIds.add(optimistic.id);
}
const merged: UiMessage[] = [];
const push = (message: UiMessage) => {
if (used.has(message.id)) return;
- if (
- message.role === "user" &&
- !isInFlightMessage(message) &&
- !durableIds.has(message.id)
- ) {
- const credits = durableUserTextCredits.get(message.content) ?? 0;
- if (credits > 0) {
- durableUserTextCredits.set(message.content, credits - 1);
- // Record the drop so a later merge pass cannot replay the orphan
- // after its collapse credit has been consumed.
- used.add(message.id);
- return;
- }
+ if (collapsedOptimisticIds.has(message.id)) {
+ used.add(message.id);
+ return;
}
used.add(message.id);
merged.push(message);
@@ -276,17 +290,6 @@ export function mergeLiveSessionMessages(
for (const message of liveNormalized) {
if (used.has(message.id)) continue;
- if (
- message.role === "user" &&
- !isInFlightMessage(message) &&
- !durableIds.has(message.id)
- ) {
- const credits = durableUserTextCredits.get(message.content) ?? 0;
- if (credits > 0) {
- durableUserTextCredits.set(message.content, credits - 1);
- continue;
- }
- }
push(message);
}
diff --git a/apps/desktop/test/session-transcript.test.mjs b/apps/desktop/test/session-transcript.test.mjs
index d911c33d4..bf67b5ec5 100644
--- a/apps/desktop/test/session-transcript.test.mjs
+++ b/apps/desktop/test/session-transcript.test.mjs
@@ -115,7 +115,12 @@ test("an orphan optimistic prompt collapses into its durable echo (D334)", () =>
// durable page carries the same prompt under the host id.
const orphan = message(
"11111111-2222-4333-8444-555555555555",
- { role: "user", content: "1", createdAt: "2026-08-31T00:00:00.000Z" },
+ {
+ role: "user",
+ content: "1",
+ createdAt: "2026-08-31T00:00:00.000Z",
+ status: "complete",
+ },
);
const echoed = message("sdk-user-1", {
role: "user",
@@ -140,7 +145,12 @@ test("a genuinely repeated prompt stays visible after a switch", () => {
// may consume one, but the second repeat must survive the merge.
const orphan = message(
"11111111-2222-4333-8444-555555555555",
- { role: "user", content: "1", createdAt: "2026-08-31T00:02:00.000Z" },
+ {
+ role: "user",
+ content: "1",
+ createdAt: "2026-08-31T00:02:00.000Z",
+ status: "complete",
+ },
);
const first = message("sdk-user-1", {
role: "user",
@@ -161,6 +171,45 @@ test("a genuinely repeated prompt stays visible after a switch", () => {
);
});
+test("an earlier identical durable prompt cannot hide a new optimistic prompt", () => {
+ const previous = message("sdk-user-old", {
+ role: "user",
+ content: "same prompt",
+ createdAt: "2026-08-31T00:00:00.000Z",
+ });
+ const optimistic = message("11111111-2222-4333-8444-555555555555", {
+ role: "user",
+ content: "same prompt",
+ createdAt: "2026-08-31T00:01:00.000Z",
+ status: "complete",
+ });
+
+ assert.deepEqual(
+ mergeLiveSessionMessages([previous], [previous, optimistic]).map(({ id }) => id),
+ ["sdk-user-old", "11111111-2222-4333-8444-555555555555"],
+ );
+});
+
+test("matching text does not collapse an optimistic prompt with attachments", () => {
+ const optimistic = message("11111111-2222-4333-8444-555555555555", {
+ role: "user",
+ content: "same prompt",
+ createdAt: "2026-08-31T00:00:00.000Z",
+ status: "complete",
+ attachments: [{ kind: "file", name: "notes.txt", ref: "/notes.txt" }],
+ });
+ const durable = message("sdk-user-1", {
+ role: "user",
+ content: "same prompt",
+ createdAt: "2026-08-31T00:00:01.000Z",
+ });
+
+ assert.deepEqual(
+ mergeLiveSessionMessages([durable], [optimistic, durable]).map(({ id }) => id),
+ ["11111111-2222-4333-8444-555555555555", "sdk-user-1"],
+ );
+});
+
test("live event upserts preserve array identity for unchanged rows", () => {
const original = [message("answer", { status: "streaming" })];
const updated = upsertLiveSessionMessage(original, {
diff --git a/docs/spec/04-ux/08-component-spec.md b/docs/spec/04-ux/08-component-spec.md
index e4c04e776..ae6c21e2d 100644
--- a/docs/spec/04-ux/08-component-spec.md
+++ b/docs/spec/04-ux/08-component-spec.md
@@ -2910,9 +2910,10 @@ reasoning-level control.
their source paths until the echo brings the session-scoped refs; a slash
prompt shows its typed form until the echo brings the expanded body and
command chip. A send that never reaches the host withdraws the row again.
- Revalidation and older-page prepends are idempotent by message id, so leaving
- and re-entering a session cannot display a second copy of an existing user
- row.
+ Revalidation and older-page prepends are idempotent by message id. If the
+ renderer missed the persistence acknowledgement, re-entry may collapse only
+ an exact, attachment-free optimistic prompt with a nearby durable echo; an
+ older identical prompt cannot hide a newly sent row.
- Shift+Enter: newline in textarea. Cmd/Ctrl+Enter sends when Enter-to-send is off. IME composition and an open autocomplete menu still take precedence over send.
- Placeholder guidance: the initially rendered context starts on its welcome copy and remains
unchanged while the page/session context, draft, focus, and IME state change.
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index e29247e23..a2da35f07 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -12183,17 +12183,22 @@ are withdrawn with ADR 0165.
back. 3) Confirm the newest user row (and any streaming tail) is at the
bottom of the transcript and the session still shows as running. 4) Optional:
Stop, switch away and back; the same newest rows remain in chronological
- order.
+ order. 5) With an older identical prompt already durable, send that same
+ text again and immediately switch away and back before its persistence
+ acknowledgement is applied.
- **Expected**: Revalidation does not append older live history after the
bounded durable page. The mounted trailing window still shows the just-sent
prompt and the live tail. The turn continues in the background across the
- switch. Stop is not required to make the prompt visible again.
+ switch. The older same-text row remains distinct and cannot suppress the new
+ optimistic prompt; the durable echo replaces that prompt in place. Stop is
+ not required to make the prompt visible again.
- **Specs linked**: `04-ux/08-component-spec.md` §1.6 / §3.5,
`04-ux/09-interaction-patterns.md` (session isolation), ADR 0120, ADR 0137,
`08-meta/decisions-log.md` (D261, D317)
- **Acceptance**: C (conversation & stream), F (persistence), Quality
- **Milestone**: M5
-- **Status**: Unit-covered (`session-transcript.test.mjs` D317 cases); full
+- **Status**: Unit-covered (`session-transcript.test.mjs` D317 and missed-ack
+ same-text cases); full
desktop journey Draft (run only in a capable environment when this surface changes)
#### E2E-183: Switching an idle session keeps a completed reply that is not on disk yet