@vellumai/assistant 0.10.11-dev.202607230140.b414d01 → 0.10.11-dev.202607230310.c962b95

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. package/package.json +1 -1
  2. package/src/__tests__/channel-setup-panel-ack.test.ts +2 -9
  3. package/src/__tests__/conversation-surface-state.test.ts +125 -0
  4. package/src/__tests__/conversation-surfaces-action-delivery.test.ts +4 -21
  5. package/src/__tests__/conversation-surfaces-activation-emit.test.ts +11 -12
  6. package/src/__tests__/conversation-surfaces-app-open.test.ts +49 -9
  7. package/src/__tests__/conversation-surfaces-data-persist.test.ts +1 -4
  8. package/src/__tests__/conversation-surfaces-deselection.test.ts +3 -9
  9. package/src/__tests__/conversation-surfaces-state-update.test.ts +76 -38
  10. package/src/__tests__/conversation-surfaces-table-action.test.ts +2 -6
  11. package/src/__tests__/conversation-surfaces-task-progress.test.ts +34 -16
  12. package/src/__tests__/conversation-tool-setup-app-refresh.test.ts +1 -5
  13. package/src/__tests__/conversation-tool-setup-attribution.test.ts +1 -5
  14. package/src/__tests__/safe-parse-surface-data.test.ts +25 -0
  15. package/src/__tests__/subagent-tool-gate-mode.test.ts +1 -5
  16. package/src/__tests__/ui-choice-copy-surfaces.test.ts +1 -15
  17. package/src/__tests__/ui-work-result-surface.test.ts +4 -16
  18. package/src/api/index.ts +3 -1
  19. package/src/api/surfaces.ts +84 -54
  20. package/src/daemon/__tests__/conversation-surfaces-launch.test.ts +2 -6
  21. package/src/daemon/conversation-agent-loop.ts +7 -34
  22. package/src/daemon/conversation-runtime-assembly.ts +4 -10
  23. package/src/daemon/conversation-surface-state.ts +212 -0
  24. package/src/daemon/conversation-surfaces.ts +159 -150
  25. package/src/daemon/conversation-tool-setup.ts +20 -10
  26. package/src/daemon/conversation.ts +4 -47
  27. package/src/daemon/doordash-steps.ts +50 -20
  28. package/src/daemon/message-types/surfaces.ts +36 -73
  29. package/src/runtime/routes/surface-conversation-resolver.ts +28 -32
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vellumai/assistant",
3
- "version": "0.10.11-dev.202607230140.b414d01",
3
+ "version": "0.10.11-dev.202607230310.c962b95",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {
@@ -18,11 +18,7 @@ import {
18
18
  type SurfaceConversationContext,
19
19
  surfaceProxyResolver,
20
20
  } from "../daemon/conversation-surfaces.js";
21
- import type {
22
- ServerMessage,
23
- SurfaceData,
24
- SurfaceType,
25
- } from "../daemon/message-protocol.js";
21
+ import type { ServerMessage, SurfaceType } from "../daemon/message-protocol.js";
26
22
 
27
23
  function makeContext(
28
24
  sent: ServerMessage[] = [],
@@ -36,10 +32,7 @@ function makeContext(
36
32
  string,
37
33
  { actionId: string; data?: Record<string, unknown> }
38
34
  >(),
39
- surfaceState: new Map<
40
- string,
41
- { surfaceType: SurfaceType; data: SurfaceData; title?: string }
42
- >(),
35
+ surfaceState: new Map(),
43
36
  surfaceUndoStacks: new Map<string, string[]>(),
44
37
  accumulatedSurfaceState: new Map<string, Record<string, unknown>>(),
45
38
  surfaceActionRequestIds: new Set<string>(),
@@ -0,0 +1,125 @@
1
+ import { describe, expect, test } from "bun:test";
2
+
3
+ import {
4
+ parseStoredActions,
5
+ restoreSurfaceStateEntry,
6
+ } from "../daemon/conversation-surface-state.js";
7
+
8
+ describe("restoreSurfaceStateEntry", () => {
9
+ test("preserves persisted data verbatim, including keys the canonical schema does not model", () => {
10
+ // A card whose persisted data carries a key outside CardSurfaceDataSchema
11
+ // (a client-owned/legacy field). Restore must keep it — the entry feeds
12
+ // the `GET /v1/surfaces/:id` in-memory fast path, and re-parsing here
13
+ // would strip it and regress display on reload.
14
+ const entry = restoreSurfaceStateEntry({
15
+ type: "ui_surface",
16
+ surfaceId: "s1",
17
+ surfaceType: "card",
18
+ data: { title: "Hi", clientOwnedField: { nested: true } },
19
+ title: "Hi",
20
+ });
21
+
22
+ expect(entry.surfaceType).toBe("card");
23
+ expect(entry.data).toEqual({
24
+ title: "Hi",
25
+ clientOwnedField: { nested: true },
26
+ });
27
+ });
28
+
29
+ test("survives a reload round-trip for a surface whose client reads keys outside the daemon schema", () => {
30
+ // document_preview's client renderer reads `content` and `mimeType`, which
31
+ // DocumentPreviewSurfaceDataSchema does not model. A persisted block
32
+ // carrying them must round-trip through restore (the GET fast path) with
33
+ // those keys intact — re-parsing through the schema would strip them and
34
+ // blank the preview on reload.
35
+ const persistedBlock = {
36
+ type: "ui_surface",
37
+ surfaceId: "doc-1",
38
+ surfaceType: "document_preview",
39
+ title: "Notes",
40
+ data: {
41
+ title: "Notes",
42
+ surfaceId: "doc-real",
43
+ content: "# Heading\n\nbody",
44
+ mimeType: "text/markdown",
45
+ },
46
+ };
47
+
48
+ const entry = restoreSurfaceStateEntry(persistedBlock);
49
+
50
+ expect(entry.surfaceType).toBe("document_preview");
51
+ expect(entry.data).toEqual(persistedBlock.data);
52
+ expect((entry.data as Record<string, unknown>).mimeType).toBe(
53
+ "text/markdown",
54
+ );
55
+ });
56
+
57
+ test("preserves daemon-internal surface types and their opaque data", () => {
58
+ const entry = restoreSurfaceStateEntry({
59
+ surfaceType: "skill_card",
60
+ data: { skills: [{ name: "foo" }], cta: "Add" },
61
+ });
62
+
63
+ expect(entry.surfaceType).toBe("skill_card");
64
+ expect(entry.data).toEqual({ skills: [{ name: "foo" }], cta: "Add" });
65
+ });
66
+
67
+ test("preserves an unknown but non-empty surfaceType verbatim (future/custom surfaces)", () => {
68
+ const entry = restoreSurfaceStateEntry({
69
+ surfaceType: "some_future_surface",
70
+ data: { keep: "me" },
71
+ });
72
+
73
+ // The recorded type must survive restart so the client renders the surface
74
+ // it recorded rather than a coerced dynamic_page. (The verbatim string is
75
+ // carried under the SurfaceType-typed field via the restore boundary cast.)
76
+ expect(entry.surfaceType as string).toBe("some_future_surface");
77
+ expect(entry.data).toEqual({ keep: "me" });
78
+ });
79
+
80
+ test("falls back to dynamic_page only when surfaceType is missing or blank", () => {
81
+ expect(restoreSurfaceStateEntry({ data: {} }).surfaceType).toBe(
82
+ "dynamic_page",
83
+ );
84
+ expect(
85
+ restoreSurfaceStateEntry({ surfaceType: " ", data: {} }).surfaceType,
86
+ ).toBe("dynamic_page");
87
+ expect(
88
+ restoreSurfaceStateEntry({ surfaceType: 42, data: {} }).surfaceType,
89
+ ).toBe("dynamic_page");
90
+ });
91
+
92
+ test("rehydrates the daemon-only activationMoment tag and drops a malformed one", () => {
93
+ const valid = restoreSurfaceStateEntry({
94
+ surfaceType: "card",
95
+ data: {},
96
+ activationMoment: "first_wow_executed",
97
+ });
98
+ expect(valid.activationMoment).toBe("first_wow_executed");
99
+
100
+ const invalid = restoreSurfaceStateEntry({
101
+ surfaceType: "card",
102
+ data: {},
103
+ activationMoment: "not_a_real_moment",
104
+ });
105
+ expect(invalid.activationMoment).toBeUndefined();
106
+ });
107
+ });
108
+
109
+ describe("parseStoredActions", () => {
110
+ test("keeps well-formed actions and drops entries missing id or label", () => {
111
+ expect(
112
+ parseStoredActions([
113
+ { id: "a", label: "A", style: "primary", data: { x: 1 } },
114
+ { id: "b" },
115
+ { label: "no id" },
116
+ "not an object",
117
+ ]),
118
+ ).toEqual([{ id: "a", label: "A", style: "primary", data: { x: 1 } }]);
119
+ });
120
+
121
+ test("returns undefined for a non-array", () => {
122
+ expect(parseStoredActions(undefined)).toBeUndefined();
123
+ expect(parseStoredActions("nope")).toBeUndefined();
124
+ });
125
+ });
@@ -13,11 +13,7 @@ const { createSurfaceMutex, handleSurfaceAction, surfaceProxyResolver } =
13
13
  await import("../daemon/conversation-surfaces.js");
14
14
 
15
15
  import type { SurfaceConversationContext } from "../daemon/conversation-surfaces.js";
16
- import type {
17
- SurfaceData,
18
- SurfaceType,
19
- UiSurfaceShow,
20
- } from "../daemon/message-protocol.js";
16
+ import type { SurfaceType, UiSurfaceShow } from "../daemon/message-protocol.js";
21
17
  import type { UserMessageAttachment } from "../daemon/message-types/shared.js";
22
18
 
23
19
  interface ProcessMessageCall {
@@ -41,20 +37,7 @@ function makeContext(sent: ServerMessage[] = []): SurfaceConversationContext & {
41
37
  string,
42
38
  { actionId: string; data?: Record<string, unknown> }
43
39
  >(),
44
- surfaceState: new Map<
45
- string,
46
- {
47
- surfaceType: SurfaceType;
48
- data: SurfaceData;
49
- title?: string;
50
- actions?: Array<{
51
- id: string;
52
- label: string;
53
- style?: string;
54
- data?: Record<string, unknown>;
55
- }>;
56
- }
57
- >(),
40
+ surfaceState: new Map(),
58
41
  surfaceUndoStacks: new Map<string, string[]>(),
59
42
  accumulatedSurfaceState: new Map<string, Record<string, unknown>>(),
60
43
  surfaceActionRequestIds: new Set<string>(),
@@ -188,7 +171,7 @@ describe("surface action delivery to assistant", () => {
188
171
  data: {
189
172
  columns: [{ id: "col", label: "Col" }],
190
173
  rows: [],
191
- } as unknown as SurfaceData,
174
+ },
192
175
  title: "History Table",
193
176
  actions: [{ id: "delete", label: "Delete" }],
194
177
  });
@@ -294,7 +277,7 @@ describe("surface action delivery to assistant", () => {
294
277
  data: {
295
278
  columns: [{ id: "col", label: "Col" }],
296
279
  rows: [],
297
- } as unknown as SurfaceData,
280
+ },
298
281
  title: "History Table",
299
282
  actions: [{ id: "delete", label: "Delete" }],
300
283
  });
@@ -22,8 +22,12 @@ mock.module("../daemon/conversation-launch.js", () => ({
22
22
  launchConversation: async () => ({ conversationId: "spawned-conv" }),
23
23
  }));
24
24
 
25
- const { createSurfaceMutex, handleSurfaceAction, surfaceProxyResolver } =
26
- await import("../daemon/conversation-surfaces.js");
25
+ const {
26
+ createSurfaceMutex,
27
+ handleSurfaceAction,
28
+ restoreSurfaceStateEntry,
29
+ surfaceProxyResolver,
30
+ } = await import("../daemon/conversation-surfaces.js");
27
31
 
28
32
  import type { SurfaceConversationContext } from "../daemon/conversation-surfaces.js";
29
33
  import type { SurfaceType, UiSurfaceShow } from "../daemon/message-protocol.js";
@@ -334,18 +338,13 @@ describe("activation moment emission from ui_show surface commits", () => {
334
338
  }));
335
339
  expect(persisted[0]!.activationMoment).toBe("moment_2");
336
340
 
337
- // Simulate a reload: drop the in-memory surfaceState, then rebuild it from
338
- // the persisted history block the same way restoreSurfaceStateFromHistory
339
- // does (including rehydrating the daemon-only tag).
341
+ // Simulate a reload: drop the in-memory surfaceState, then rebuild it
342
+ // from the persisted history block through the same helper
343
+ // `restoreSurfaceStateFromHistory` uses (including rehydrating the
344
+ // daemon-only tag).
340
345
  ctx.surfaceState.clear();
341
346
  for (const b of persisted) {
342
- ctx.surfaceState.set(b.surfaceId, {
343
- surfaceType: b.surfaceType,
344
- data: b.data,
345
- title: b.title,
346
- actions: b.actions,
347
- ...(b.activationMoment ? { activationMoment: b.activationMoment } : {}),
348
- });
347
+ ctx.surfaceState.set(b.surfaceId, restoreSurfaceStateEntry(b));
349
348
  }
350
349
 
351
350
  // A commit after restore still records exactly one row.
@@ -1,15 +1,13 @@
1
1
  import { describe, expect, test } from "bun:test";
2
2
 
3
+ import { DynamicPagePreviewSchema } from "../api/surfaces.js";
3
4
  import {
5
+ buildAppOpenPreview,
4
6
  createSurfaceMutex,
5
7
  type SurfaceConversationContext,
6
8
  surfaceProxyResolver,
7
9
  } from "../daemon/conversation-surfaces.js";
8
- import type {
9
- ServerMessage,
10
- SurfaceData,
11
- SurfaceType,
12
- } from "../daemon/message-protocol.js";
10
+ import type { ServerMessage, SurfaceType } from "../daemon/message-protocol.js";
13
11
 
14
12
  interface ContextOptions {
15
13
  hasNoClient?: boolean;
@@ -30,10 +28,7 @@ function makeContext(
30
28
  string,
31
29
  { actionId: string; data?: Record<string, unknown> }
32
30
  >(),
33
- surfaceState: new Map<
34
- string,
35
- { surfaceType: SurfaceType; data: SurfaceData; title?: string }
36
- >(),
31
+ surfaceState: new Map(),
37
32
  surfaceUndoStacks: new Map<string, string[]>(),
38
33
  accumulatedSurfaceState: new Map<string, Record<string, unknown>>(),
39
34
  surfaceActionRequestIds: new Set<string>(),
@@ -74,3 +69,48 @@ describe("app_open render-capability gate", () => {
74
69
  expect(sent).toHaveLength(0);
75
70
  });
76
71
  });
72
+
73
+ describe("buildAppOpenPreview — app-name default vs supplied preview", () => {
74
+ const dflt = { title: "My App", subtitle: "An app that does things" };
75
+ // Inputs are passed through the same schema the app_open path uses, so these
76
+ // exercise the real parse → merge flow (the parse fills `title: ""` for an
77
+ // omitted title, which the merge must not let clobber the app name).
78
+ const parse = (raw: Record<string, unknown>) =>
79
+ DynamicPagePreviewSchema.parse(raw);
80
+
81
+ test("no preview keeps the app name and description", () => {
82
+ expect(buildAppOpenPreview(dflt, undefined, null)).toEqual({
83
+ title: "My App",
84
+ subtitle: "An app that does things",
85
+ });
86
+ });
87
+
88
+ test("a supplied non-empty title wins over the default", () => {
89
+ const preview = parse({ title: "Custom Title", subtitle: "Custom sub" });
90
+ expect(buildAppOpenPreview(dflt, preview, null)).toMatchObject({
91
+ title: "Custom Title",
92
+ subtitle: "Custom sub",
93
+ });
94
+ });
95
+
96
+ test("a preview that omits the title falls back to the app name (not blank)", () => {
97
+ // Parsing `{ subtitle }` yields `title: ""` via `z.string().catch("")`.
98
+ const preview = parse({ subtitle: "Only a subtitle" });
99
+ expect(preview.title).toBe(""); // precondition: the parse fills it blank
100
+ const merged = buildAppOpenPreview(dflt, preview, null);
101
+ expect(merged.title).toBe("My App");
102
+ expect(merged.subtitle).toBe("Only a subtitle");
103
+ });
104
+
105
+ test("an explicit empty-string title falls back to the app name", () => {
106
+ const preview = parse({ title: "" });
107
+ expect(buildAppOpenPreview(dflt, preview, null).title).toBe("My App");
108
+ });
109
+
110
+ test("a stored preview image always wins", () => {
111
+ const preview = parse({ title: "T", previewImage: "caller-supplied" });
112
+ expect(
113
+ buildAppOpenPreview(dflt, preview, "generated-image").previewImage,
114
+ ).toBe("generated-image");
115
+ });
116
+ });
@@ -62,10 +62,7 @@ function makeContext(sent: ServerMessage[] = []): SurfaceConversationContext {
62
62
  string,
63
63
  { actionId: string; data?: Record<string, unknown> }
64
64
  >(),
65
- surfaceState: new Map<
66
- string,
67
- { surfaceType: SurfaceType; data: SurfaceData; title?: string }
68
- >(),
65
+ surfaceState: new Map(),
69
66
  surfaceUndoStacks: new Map<string, string[]>(),
70
67
  accumulatedSurfaceState: new Map<string, Record<string, unknown>>(),
71
68
  surfaceActionRequestIds: new Set<string>(),
@@ -4,8 +4,8 @@ import {
4
4
  buildDeselectionDescription,
5
5
  describeTableRow,
6
6
  formatDeselectionList,
7
+ type SurfaceShowPair,
7
8
  } from "../daemon/conversation-surfaces.js";
8
- import type { SurfaceData, SurfaceType } from "../daemon/message-protocol.js";
9
9
  import type {
10
10
  ListSurfaceData,
11
11
  TableColumn,
@@ -18,10 +18,7 @@ const cols: TableColumn[] = [
18
18
  { id: "count", label: "Emails Found" },
19
19
  ];
20
20
 
21
- function makeTableState(rows: TableRow[]): {
22
- surfaceType: SurfaceType;
23
- data: SurfaceData;
24
- } {
21
+ function makeTableState(rows: TableRow[]): SurfaceShowPair {
25
22
  const data: TableSurfaceData = {
26
23
  columns: cols,
27
24
  rows,
@@ -30,10 +27,7 @@ function makeTableState(rows: TableRow[]): {
30
27
  return { surfaceType: "table", data };
31
28
  }
32
29
 
33
- function makeListState(items: ListSurfaceData["items"]): {
34
- surfaceType: SurfaceType;
35
- data: SurfaceData;
36
- } {
30
+ function makeListState(items: ListSurfaceData["items"]): SurfaceShowPair {
37
31
  const data: ListSurfaceData = { items, selectionMode: "multiple" };
38
32
  return { surfaceType: "list", data };
39
33
  }
@@ -3,14 +3,11 @@ import { describe, expect, test } from "bun:test";
3
3
  import {
4
4
  createSurfaceMutex,
5
5
  handleSurfaceAction,
6
+ restoreSurfaceStateEntry,
6
7
  type SurfaceConversationContext,
7
8
  surfaceProxyResolver,
8
9
  } from "../daemon/conversation-surfaces.js";
9
- import type {
10
- ServerMessage,
11
- SurfaceData,
12
- SurfaceType,
13
- } from "../daemon/message-protocol.js";
10
+ import type { ServerMessage, SurfaceType } from "../daemon/message-protocol.js";
14
11
 
15
12
  /**
16
13
  * Build a minimal SurfaceConversationContext for testing.
@@ -35,10 +32,7 @@ function makeContext(opts?: {
35
32
  string,
36
33
  { actionId: string; data?: Record<string, unknown> }
37
34
  >(),
38
- surfaceState: new Map<
39
- string,
40
- { surfaceType: SurfaceType; data: SurfaceData; title?: string }
41
- >(),
35
+ surfaceState: new Map(),
42
36
  surfaceUndoStacks: new Map<string, string[]>(),
43
37
  accumulatedSurfaceState: new Map<string, Record<string, unknown>>(),
44
38
  surfaceActionRequestIds: new Set<string>(),
@@ -71,7 +65,7 @@ function registerDynamicPage(
71
65
  ctx.pendingSurfaceActions.set(surfaceId, { surfaceType: "dynamic_page" });
72
66
  ctx.surfaceState.set(surfaceId, {
73
67
  surfaceType: "dynamic_page",
74
- data: { html: "<div>test</div>" } as SurfaceData,
68
+ data: { html: "<div>test</div>" },
75
69
  });
76
70
  }
77
71
 
@@ -110,7 +104,7 @@ describe("state_update silent accumulation", () => {
110
104
  data: {
111
105
  columns: [],
112
106
  rows: [],
113
- } as unknown as SurfaceData,
107
+ },
114
108
  });
115
109
 
116
110
  handleSurfaceAction(ctx, "surface-table", "state_update", { page: 1 });
@@ -252,17 +246,21 @@ describe("ui_update preserves client-owned keys the daemon schema omits", () =>
252
246
  test("a valid patch keeps unmodeled keys on the merged, sent, and stored data", async () => {
253
247
  const sent: ServerMessage[] = [];
254
248
  const ctx = makeContext({ sent });
255
- // A document_preview whose stored data carries `surfaceId` — an unmodeled
256
- // key read by the client renderer, not by the daemon schema.
257
- ctx.surfaceState.set("doc-1", {
258
- surfaceType: "document_preview",
259
- data: {
260
- title: "Notes",
261
- surfaceId: "doc-real",
262
- content: "# Heading",
263
- mimeType: "text/markdown",
264
- } as SurfaceData,
265
- });
249
+ // A document_preview whose stored data carries `content`/`mimeType`
250
+ // (read by the client renderer, not modeled by the daemon schema).
251
+ // Built the way history restore produces it (verbatim client-owned keys).
252
+ ctx.surfaceState.set(
253
+ "doc-1",
254
+ restoreSurfaceStateEntry({
255
+ surfaceType: "document_preview",
256
+ data: {
257
+ title: "Notes",
258
+ surfaceId: "doc-real",
259
+ content: "# Heading",
260
+ mimeType: "text/markdown",
261
+ },
262
+ }),
263
+ );
266
264
 
267
265
  const result = await surfaceProxyResolver(ctx, "ui_update", {
268
266
  surface_id: "doc-1",
@@ -270,33 +268,40 @@ describe("ui_update preserves client-owned keys the daemon schema omits", () =>
270
268
  });
271
269
  expect(result.isError).toBe(false);
272
270
 
273
- // Stored state keeps the unmodeled key and applies the patch.
274
- expect(ctx.surfaceState.get("doc-1")?.data).toMatchObject({
271
+ // Stored state keeps the unmodeled keys and applies the patch.
272
+ expect(ctx.surfaceState.get("doc-1")?.data).toEqual({
275
273
  title: "Notes (edited)",
276
274
  surfaceId: "doc-real",
275
+ content: "# Heading",
276
+ mimeType: "text/markdown",
277
277
  });
278
278
 
279
- // The update sent to the client also carries it.
279
+ // The update sent to the client also carries them verbatim.
280
280
  const update = sent.find((m) => m.type === "ui_surface_update");
281
281
  expect(update).toBeDefined();
282
- expect((update as { data: Record<string, unknown> }).data).toMatchObject({
282
+ expect((update as { data: Record<string, unknown> }).data).toEqual({
283
283
  title: "Notes (edited)",
284
284
  surfaceId: "doc-real",
285
+ content: "# Heading",
286
+ mimeType: "text/markdown",
285
287
  });
286
288
  });
287
289
  });
288
290
 
289
291
  describe("ui_update on a restored unknown surface type", () => {
290
- test("forwards the merge opaquely instead of storing garbage", async () => {
292
+ test("forwards the merge opaquely instead of throwing on the schema registry", async () => {
291
293
  const sent: ServerMessage[] = [];
292
294
  const ctx = makeContext({ sent });
293
295
  // Restore preserves an unknown-but-non-empty surfaceType verbatim (a
294
- // newer/custom client-rendered surface). safeParseSurfaceData has no schema
295
- // for it — the update must forward the merge, not the type string.
296
- ctx.surfaceState.set("future-1", {
297
- surfaceType: "future_widget" as SurfaceType,
298
- data: { title: "Widget", customField: 42 } as SurfaceData,
299
- });
296
+ // newer/custom client-rendered surface). Indexing SURFACE_DATA_SCHEMAS with
297
+ // it would read `undefined` and throw — the update must forward opaquely.
298
+ ctx.surfaceState.set(
299
+ "future-1",
300
+ restoreSurfaceStateEntry({
301
+ surfaceType: "future_widget",
302
+ data: { title: "Widget", customField: 42 },
303
+ }),
304
+ );
300
305
 
301
306
  const result = await surfaceProxyResolver(ctx, "ui_update", {
302
307
  surface_id: "future-1",
@@ -304,12 +309,10 @@ describe("ui_update on a restored unknown surface type", () => {
304
309
  });
305
310
  expect(result.isError).toBe(false);
306
311
 
312
+ // The unknown type is preserved and the merge applied verbatim.
307
313
  const stored = ctx.surfaceState.get("future-1");
308
314
  expect(stored?.surfaceType as string).toBe("future_widget");
309
- expect(stored?.data as Record<string, unknown>).toEqual({
310
- title: "Widget (edited)",
311
- customField: 42,
312
- });
315
+ expect(stored?.data).toEqual({ title: "Widget (edited)", customField: 42 });
313
316
 
314
317
  const update = sent.find((m) => m.type === "ui_surface_update");
315
318
  expect(update).toBeDefined();
@@ -318,6 +321,41 @@ describe("ui_update on a restored unknown surface type", () => {
318
321
  customField: 42,
319
322
  });
320
323
  });
324
+
325
+ test("also syncs the current-turn snapshot so the two persist writers agree", async () => {
326
+ const ctx = makeContext();
327
+ ctx.surfaceState.set(
328
+ "future-1",
329
+ restoreSurfaceStateEntry({
330
+ surfaceType: "future_widget",
331
+ data: { title: "Widget", customField: 42 },
332
+ }),
333
+ );
334
+ // The surface is also tracked in the current turn: the turn-end persist
335
+ // loop writes `currentTurnSurfaces[i].data` to the same ui_surface block
336
+ // that the debounced persist writes. If this snapshot were left stale for
337
+ // an opaquely-forwarded (unknown-type) update, the two writers would race
338
+ // on divergent data.
339
+ ctx.currentTurnSurfaces.push({
340
+ surfaceId: "future-1",
341
+ surfaceType: "future_widget",
342
+ data: { title: "Widget", customField: 42 },
343
+ } as unknown as (typeof ctx.currentTurnSurfaces)[number]);
344
+
345
+ const result = await surfaceProxyResolver(ctx, "ui_update", {
346
+ surface_id: "future-1",
347
+ data: { title: "Widget (edited)" },
348
+ });
349
+ expect(result.isError).toBe(false);
350
+
351
+ // The current-turn snapshot reflects the update, matching surfaceState.
352
+ expect(ctx.currentTurnSurfaces[0]?.data as Record<string, unknown>).toEqual(
353
+ {
354
+ title: "Widget (edited)",
355
+ customField: 42,
356
+ },
357
+ );
358
+ });
321
359
  });
322
360
 
323
361
  describe("ui_update normalizes modeled fields for known surface types", () => {
@@ -326,7 +364,7 @@ describe("ui_update normalizes modeled fields for known surface types", () => {
326
364
  const ctx = makeContext({ sent });
327
365
  ctx.surfaceState.set("page-1", {
328
366
  surfaceType: "dynamic_page",
329
- data: { html: "<p>original</p>" } as SurfaceData,
367
+ data: { html: "<p>original</p>" },
330
368
  });
331
369
 
332
370
  // A malformed patch: `html` as a non-string. The tolerant schema
@@ -5,11 +5,7 @@ import {
5
5
  handleSurfaceAction,
6
6
  type SurfaceConversationContext,
7
7
  } from "../daemon/conversation-surfaces.js";
8
- import type {
9
- ServerMessage,
10
- SurfaceData,
11
- SurfaceType,
12
- } from "../daemon/message-protocol.js";
8
+ import type { ServerMessage, SurfaceType } from "../daemon/message-protocol.js";
13
9
 
14
10
  /**
15
11
  * Build a minimal SurfaceConversationContext for testing table surface actions.
@@ -146,7 +142,7 @@ function registerTableSurface(
146
142
 
147
143
  ctx.surfaceState.set(surfaceId, {
148
144
  surfaceType: "table",
149
- data: data as unknown as SurfaceData,
145
+ data,
150
146
  title: "Test Table",
151
147
  actions,
152
148
  });