@abelxiaoxing/cadence 2.0.27 → 2.0.28

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abelxiaoxing/cadence",
3
- "version": "2.0.27",
3
+ "version": "2.0.28",
4
4
  "description": "Conversation-driven Pi workspace: persistent native and WebUI workflows, explicit plan approval, checks and local delivery.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -1,9 +1,19 @@
1
1
  /** Private ordinary-chat context. No Pi session import, hidden thinking or provider envelopes. */
2
- import { closeSync, constants, fstatSync, openSync, readSync } from "node:fs";
2
+ import { createHash } from "node:crypto";
3
+ import {
4
+ closeSync,
5
+ constants,
6
+ fstatSync,
7
+ lstatSync,
8
+ openSync,
9
+ readSync,
10
+ } from "node:fs";
11
+ import { dirname, join } from "node:path";
3
12
  import {
4
13
  canonicalJson,
5
14
  closed,
6
15
  integer,
16
+ LIMITS,
7
17
  list,
8
18
  oneOf,
9
19
  parseWireJson,
@@ -12,14 +22,17 @@ import {
12
22
  import { DetailSanitizer, sanitizeText } from "./worker-redaction.mjs";
13
23
 
14
24
  export const CONTEXT_BYTES = 2 * 1024 * 1024;
15
- const clean = (s, secrets, max = CONTEXT_BYTES) =>
25
+ // One message/pipe record, not the lifetime context. Same logical body bound as capture.
26
+ export const CONTEXT_RECORD_BYTES = LIMITS.detailBytes;
27
+ const clean = (s, secrets, max = CONTEXT_RECORD_BYTES) =>
16
28
  sanitizeText(text(s, max), secrets);
17
29
  function argumentsData(value, secrets) {
18
30
  const sanitizer = new DetailSanitizer("canonical-json", secrets);
19
31
  const source =
20
- sanitizer.write(canonicalJson(value, 64 * 1024)) + sanitizer.end();
32
+ sanitizer.write(canonicalJson(value, CONTEXT_RECORD_BYTES)) +
33
+ sanitizer.end();
21
34
  if (sanitizer.limited) throw new Error("Unsupported context arguments");
22
- return parseWireJson(source, 64 * 1024);
35
+ return parseWireJson(source, CONTEXT_RECORD_BYTES);
23
36
  }
24
37
  function own(value, key) {
25
38
  const d =
@@ -106,15 +119,14 @@ export function conversationMessages(messages, secrets = []) {
106
119
  };
107
120
  });
108
121
  }
109
- export function readConversationContext(path) {
110
- if (path === null) return [];
122
+ function readPrivateContext(path, maximum) {
111
123
  const fd = openSync(path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
112
124
  try {
113
125
  const st = fstatSync(fd);
114
126
  if (
115
127
  !st.isFile() ||
116
128
  st.nlink !== 1 ||
117
- st.size > CONTEXT_BYTES ||
129
+ st.size > maximum ||
118
130
  (process.platform !== "win32" &&
119
131
  (st.mode & 0o077 || st.uid !== process.getuid()))
120
132
  )
@@ -130,25 +142,78 @@ export function readConversationContext(path) {
130
142
  if (
131
143
  size !== st.size ||
132
144
  after.size !== st.size ||
133
- after.mtimeMs !== st.mtimeMs
145
+ after.mtimeMs !== st.mtimeMs ||
146
+ after.ctimeMs !== st.ctimeMs ||
147
+ lstatSync(path).ino !== st.ino
134
148
  )
135
149
  throw new Error("Changed context file");
136
- const o = closed(
137
- parseWireJson(
138
- new TextDecoder("utf-8", { fatal: true }).decode(
139
- buffer.subarray(0, size),
140
- ),
141
- CONTEXT_BYTES,
142
- ),
143
- ["schema", "messages"],
150
+ return new TextDecoder("utf-8", { fatal: true }).decode(
151
+ buffer.subarray(0, size),
144
152
  );
145
- if (o.schema !== "cadence-conversation-context/1")
146
- throw new Error("Invalid context format");
147
- return conversationMessages(o.messages);
148
153
  } finally {
149
154
  closeSync(fd);
150
155
  }
151
156
  }
157
+ /** Old checkpoints remain readable without migration. New checkpoints hash-chain
158
+ * private per-message records, so no aggregate JSON/pipe size is required. */
159
+ export function* iterateConversationContext(path) {
160
+ if (path === null) return;
161
+ const source = readPrivateContext(path, CONTEXT_BYTES);
162
+ const value = parseWireJson(source, CONTEXT_BYTES);
163
+ if (value.schema === "cadence-conversation-context/1") {
164
+ const o = closed(value, ["schema", "messages"]);
165
+ yield* conversationMessages(o.messages);
166
+ return;
167
+ }
168
+ const o = closed(value, ["schema", "messages", "head"]);
169
+ if (o.schema !== "cadence-conversation-context/2")
170
+ throw new Error("Invalid context format");
171
+ const count = integer(o.messages, 0);
172
+ let head = null;
173
+ for (let index = 0; index < count; index++) {
174
+ const body = readPrivateContext(
175
+ join(dirname(path), `context-${index}.json`),
176
+ CONTEXT_RECORD_BYTES,
177
+ );
178
+ const record = closed(parseWireJson(body, CONTEXT_RECORD_BYTES), [
179
+ "schema",
180
+ "index",
181
+ "previous",
182
+ "message",
183
+ ]);
184
+ if (
185
+ record.schema !== "cadence-conversation-message/1" ||
186
+ record.index !== index ||
187
+ record.previous !== head
188
+ )
189
+ throw new Error("Invalid context chain");
190
+ head = createHash("sha256").update(body).digest("hex");
191
+ yield conversationMessages([record.message])[0];
192
+ }
193
+ if (o.head !== head) throw new Error("Changed context chain");
194
+ }
195
+ export function readConversationContext(path) {
196
+ return [...iterateConversationContext(path)];
197
+ }
198
+ /** Anonymous pipe framing: each message is bounded, the stream is not. The
199
+ * owner sanitizes each record again before persisting it and commits only on
200
+ * the end marker plus original successful process closure. */
201
+ export function* conversationContextRecords(messages, secrets = []) {
202
+ if (!Array.isArray(messages)) throw new Error("Invalid context");
203
+ let count = 0;
204
+ for (const message of messages) {
205
+ yield `${canonicalJson(
206
+ {
207
+ schema: "cadence-conversation-stream/1",
208
+ message: conversationMessages([message], secrets)[0],
209
+ },
210
+ CONTEXT_RECORD_BYTES - 4096,
211
+ )}\n`;
212
+ count++;
213
+ }
214
+ yield canonicalJson({ schema: "cadence-conversation-stream-end/1", count }) +
215
+ "\n";
216
+ }
152
217
  export function encodeConversationContext(messages, secrets) {
153
218
  return canonicalJson(
154
219
  {
@@ -2,5 +2,5 @@
2
2
  export const nativeBuild = {
3
3
  kind: "native",
4
4
  contractHash:
5
- "b6935a8daa09f70de094136c18d8e3588fdd34fc24b9a8fe95e465de11ddae8d",
5
+ "45f3666b0fc0d853e95e618b50156819f6282ce648e9703b876d9658ae15b78f",
6
6
  } as const;
@@ -17,8 +17,8 @@ import {
17
17
  VERSION,
18
18
  } from "@earendil-works/pi-coding-agent";
19
19
  import {
20
- encodeConversationContext,
21
- readConversationContext,
20
+ conversationContextRecords,
21
+ iterateConversationContext,
22
22
  } from "./conversation-context.mjs";
23
23
  import { conversationResources } from "./conversation-resources.mjs";
24
24
  import { coordinatorTools } from "./coordinator-tools.mjs";
@@ -390,7 +390,7 @@ try {
390
390
  const requestedThinkingLevel = thinkingLevel ?? "off";
391
391
  const manager = SessionManager.inMemory(cwd);
392
392
  if (conversation)
393
- for (const message of readConversationContext(conversation.previous))
393
+ for (const message of iterateConversationContext(conversation.previous))
394
394
  manager.appendMessage(message);
395
395
  const coordinationTools = coordinator ? coordinatorTools() : undefined;
396
396
  ownsCoordinatorChannel = coordinator;
@@ -583,7 +583,8 @@ try {
583
583
  // any checkpoint reaches disk; worker output cannot contain owner credentials.
584
584
  if (conversation) {
585
585
  stage = "conversation-context";
586
- writeFileSync(3, encodeConversationContext(session.messages, secrets));
586
+ for (const record of conversationContextRecords(session.messages, secrets))
587
+ writeFileSync(3, record);
587
588
  }
588
589
  await capture.final();
589
590
  await emitter.drain();
@@ -72,7 +72,14 @@ export type WorkbenchRead =
72
72
  | { kind: "workflow-detail"; id: string; stepId: string }
73
73
  | { kind: "workflow-transcript"; id: string; stepId: string }
74
74
  | { kind: "workflow"; id: string }
75
- | { kind: "conversations" }
75
+ | {
76
+ kind: "conversation-transcript";
77
+ id: string;
78
+ turnId: string;
79
+ page: number;
80
+ }
81
+ | { kind: "conversation-active" }
82
+ | { kind: "conversations"; after?: ChatCursor | null; scan?: string }
76
83
  | { kind: "task-origin"; taskId: string }
77
84
  | { kind: "conversation"; id: string; before: string | null }
78
85
  | { kind: "files"; path: string }
@@ -160,10 +167,20 @@ export interface ChatTurn {
160
167
  status: ChatStatus;
161
168
  items: ChatItem[];
162
169
  notice: string;
170
+ /** Immutable full-transcript pages; items above are only the latest preview. */
171
+ transcript?: { pages: number };
163
172
  /** Content kinds observed and not retained; never written into `notice`.
164
173
  * `other` is a block of a type Cadence does not recognize. */
165
174
  omitted: { thinking: boolean; media: boolean; other: boolean };
166
175
  }
176
+ export interface ChatCursor {
177
+ id: string;
178
+ updatedAt: number;
179
+ }
180
+ function decodeChatCursor(value: unknown): ChatCursor {
181
+ const o = closed(value, ["id", "updatedAt"]);
182
+ return { id: decodeId(o.id), updatedAt: integer(o.updatedAt, 0) };
183
+ }
167
184
  export interface ChatSummary {
168
185
  id: string;
169
186
  title: string;
@@ -178,6 +195,12 @@ export interface FileEntry {
178
195
  directory: boolean;
179
196
  }
180
197
  export type WorkbenchResult =
198
+ | {
199
+ kind: "conversation-transcript";
200
+ conversationId: string;
201
+ page: number;
202
+ turn: ChatTurn;
203
+ }
181
204
  | { kind: "workflow-history-progress"; after: string | null; scan: string }
182
205
  | {
183
206
  kind: "workflow-history";
@@ -207,7 +230,9 @@ export type WorkbenchResult =
207
230
  }
208
231
  | { kind: "workflow"; commandId: string | null; response: WorkflowResponse }
209
232
  | { kind: "task-origin"; taskId: string; conversationId: string | null }
210
- | { kind: "conversations"; entries: ChatSummary[] }
233
+ | { kind: "conversation-active"; entries: ChatSummary[] }
234
+ | { kind: "conversations-progress"; after: ChatCursor | null; scan: string }
235
+ | { kind: "conversations"; entries: ChatSummary[]; next?: ChatCursor | null }
211
236
  | {
212
237
  kind: "conversation";
213
238
  summary: ChatSummary | null;
@@ -248,6 +273,15 @@ const statuses = [
248
273
  export function decodeWorkbenchRead(v: unknown): WorkbenchRead {
249
274
  const kind = Object.getOwnPropertyDescriptor(v ?? {}, "kind")?.value;
250
275
  switch (kind) {
276
+ case "conversation-transcript": {
277
+ const o = closed(v, ["kind", "id", "turnId", "page"]);
278
+ return {
279
+ kind,
280
+ id: decodeId(o.id),
281
+ turnId: decodeId(o.turnId),
282
+ page: integer(o.page, 0),
283
+ };
284
+ }
251
285
  case "cadence-conversation": {
252
286
  const o = closed(v, ["kind", "id"]);
253
287
  return { kind, id: decodeId(o.id) };
@@ -273,7 +307,21 @@ export function decodeWorkbenchRead(v: unknown): WorkbenchRead {
273
307
  const o = closed(v, ["kind", "id"]);
274
308
  return { kind, id: decodeId(o.id) };
275
309
  }
276
- case "conversations":
310
+ case "conversations": {
311
+ const after = Object.hasOwn(v as object, "after"),
312
+ scan = Object.hasOwn(v as object, "scan");
313
+ const o = closed(v, [
314
+ "kind",
315
+ ...(after ? ["after"] : []),
316
+ ...(scan ? ["scan"] : []),
317
+ ]);
318
+ return {
319
+ kind,
320
+ ...(after ? { after: nullable(o.after, decodeChatCursor) } : {}),
321
+ ...(scan ? { scan: decodeId(o.scan) } : {}),
322
+ };
323
+ }
324
+ case "conversation-active":
277
325
  case "projects":
278
326
  closed(v, ["kind"]);
279
327
  return { kind };
@@ -424,7 +472,14 @@ export function decodeChatTurn(v: unknown): ChatTurn {
424
472
  const keys = ["id", "epoch", "status", "items", "notice"];
425
473
  const current =
426
474
  v !== null && typeof v === "object" && Object.hasOwn(v, "omitted");
427
- const o = closed(v, current ? [...keys, "omitted"] : keys);
475
+ const paged =
476
+ v !== null && typeof v === "object" && Object.hasOwn(v, "transcript");
477
+ const o = closed(v, [
478
+ ...keys,
479
+ ...(current ? ["omitted"] : []),
480
+ ...(paged ? ["transcript"] : []),
481
+ ]);
482
+ const transcript = paged ? closed(o.transcript, ["pages"]) : null;
428
483
  const omitted = current
429
484
  ? closed(o.omitted, ["thinking", "media", "other"])
430
485
  : null;
@@ -433,6 +488,9 @@ export function decodeChatTurn(v: unknown): ChatTurn {
433
488
  epoch: decodeId(o.epoch),
434
489
  status: oneOf(o.status, statuses),
435
490
  notice: text(o.notice, 1024),
491
+ ...(transcript
492
+ ? { transcript: { pages: integer(transcript.pages, 0) } }
493
+ : {}),
436
494
  omitted: {
437
495
  thinking: omitted ? bool(omitted.thinking) : false,
438
496
  media: omitted ? bool(omitted.media) : false,
@@ -474,6 +532,14 @@ export function decodeChatSummary(v: unknown): ChatSummary {
474
532
  export function decodeWorkbenchResult(v: unknown): WorkbenchResult {
475
533
  canonicalJson(v, 512 * 1024);
476
534
  const k = Object.getOwnPropertyDescriptor(v ?? {}, "kind")?.value;
535
+ if (k === "conversation-transcript") {
536
+ const o = closed(v, ["kind", "conversationId", "page", "turn"]);
537
+ const turn = decodeChatTurn(o.turn),
538
+ page = integer(o.page, 0);
539
+ if (!turn.transcript || page >= turn.transcript.pages)
540
+ throw new ContractError("invalid");
541
+ return { kind: k, conversationId: decodeId(o.conversationId), page, turn };
542
+ }
477
543
  if (k === "workflow-transcript") {
478
544
  const o = closed(v, ["kind", "runId", "stepId", "turn"]);
479
545
  return {
@@ -569,9 +635,26 @@ export function decodeWorkbenchResult(v: unknown): WorkbenchResult {
569
635
  conversationId: nullable(o.conversationId, decodeId),
570
636
  };
571
637
  }
572
- if (kind === "conversations") {
638
+ if (kind === "conversations-progress") {
639
+ const o = closed(v, ["kind", "after", "scan"]);
640
+ return {
641
+ kind,
642
+ after: nullable(o.after, decodeChatCursor),
643
+ scan: decodeId(o.scan),
644
+ };
645
+ }
646
+ if (kind === "conversation-active") {
573
647
  const o = closed(v, ["kind", "entries"]);
574
- return { kind, entries: list(o.entries, 200, decodeChatSummary) };
648
+ return { kind, entries: list(o.entries, 8, decodeChatSummary) };
649
+ }
650
+ if (kind === "conversations") {
651
+ const paged = Object.hasOwn(v as object, "next");
652
+ const o = closed(v, ["kind", "entries", ...(paged ? ["next"] : [])]);
653
+ return {
654
+ kind,
655
+ entries: list(o.entries, 200, decodeChatSummary),
656
+ ...(paged ? { next: nullable(o.next, decodeChatCursor) } : {}),
657
+ };
575
658
  }
576
659
  if (kind === "conversation") {
577
660
  const o = closed(v, ["kind", "summary", "turns", "before"]);