paseo-plugin-zcode-provider 0.4.4 → 0.5.1

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 (59) hide show
  1. package/NOTICE.md +2 -2
  2. package/README.md +18 -13
  3. package/client/account.tsx +13 -22
  4. package/client/goal-pills.ts +252 -0
  5. package/client/goal.tsx +203 -0
  6. package/index.client.tsx +4 -0
  7. package/index.server.ts +12 -4
  8. package/package.json +4 -4
  9. package/paseo-plugin.json +1 -1
  10. package/server/build-info.ts +1 -1
  11. package/server/diagnostics.ts +4 -1
  12. package/server/discovery/manifest.ts +2 -2
  13. package/server/errors.ts +1 -0
  14. package/server/goal-command.ts +32 -0
  15. package/server/goal.ts +58 -0
  16. package/server/host/bridge.ts +3 -0
  17. package/server/mapping.ts +23 -6
  18. package/server/presentation.ts +4 -1
  19. package/server/provider-status.ts +3 -3
  20. package/server/provider.ts +91 -21
  21. package/server/runtime/pins.ts +3 -3
  22. package/server/session.ts +278 -92
  23. package/server/turn-failure.ts +6 -1
  24. package/server/usage.ts +1 -3
  25. package/server/vendor/zcode/NOTICE.md +1 -1
  26. package/server/vendor/zcode/packages/rpc/src/channelClient.ts +18 -3
  27. package/server/vendor/zcode/packages/rpc/src/channelServer.ts +32 -12
  28. package/server/vendor/zcode/packages/rpc/src/channels.shared.ts +8 -1
  29. package/server/vendor/zcode/packages/rpc/src/serialization.ts +9 -3
  30. package/server/vendor/zcode/packages/shared/src/browser-use/command-metadata.ts +2 -0
  31. package/server/vendor/zcode/packages/shared/src/browser-use/commands.ts +2 -0
  32. package/server/vendor/zcode/packages/shared/src/browser-use/result.ts +4 -0
  33. package/server/vendor/zcode/packages/shared/src/gen-ui/contract.ts +246 -0
  34. package/server/vendor/zcode/packages/shared/src/gen-ui/tweakProtocol.ts +143 -0
  35. package/server/vendor/zcode/packages/shared/src/{localTtft.ts → local-ttft/contract.ts} +56 -1
  36. package/server/vendor/zcode/packages/shared/src/mcp-apps/contract.ts +8 -0
  37. package/server/vendor/zcode/packages/shared/src/official-mcp-auth.ts +8 -0
  38. package/server/vendor/zcode/packages/shared/src/protocol.ts +2 -0
  39. package/server/vendor/zcode/packages/shared/src/request-security-edition/verification-policy.ts +9 -1
  40. package/server/vendor/zcode/packages/shared/src/runtimeEnv.ts +10 -0
  41. package/server/vendor/zcode/packages/shared/src/server-remote.ts +2 -0
  42. package/server/vendor/zcode/packages/shared/src/source-server/compatibility.ts +11 -0
  43. package/server/vendor/zcode/packages/shared/src/source-server/contracts.ts +21 -1
  44. package/server/vendor/zcode/packages/shared/src/source-server/errorCodes.ts +27 -9
  45. package/server/vendor/zcode/packages/shared/src/subagent-profile.ts +5 -0
  46. package/server/vendor/zcode/packages/shared/src/validationAppSettings.ts +1 -0
  47. package/server/vendor/zcode/packages/shared/src/zcode-protocol/index.ts +164 -11
  48. package/server/vendor/zcode/packages/shared/src/zcode-protocol-legacy-types.ts +1 -0
  49. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/apply.ts +4 -2
  50. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/command.ts +11 -7
  51. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/delta.ts +2 -0
  52. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/input-intent.ts +2 -0
  53. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/rows.ts +19 -1
  54. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/snapshot.ts +11 -0
  55. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/transport.ts +20 -1
  56. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/workflow-row-meta.ts +8 -0
  57. package/server/vendor/zcode/packages/ui/src/v4/conversationGoalSummaryModel.ts +88 -0
  58. package/server/vendor/zcode/provenance.json +78 -64
  59. package/shared/goal.ts +89 -0
@@ -1,5 +1,6 @@
1
+ import { genUiModelContextSchema } from "../gen-ui/contract.js";
1
2
  import { createSessionRequestedConfigSchema } from "./create-session-config.js";
2
- import { localTtftContextSchema, localTtftClockSchema } from "../localTtft.js";
3
+ import { localTtftContextSchema, localTtftClockSchema } from "../local-ttft/contract.js";
3
4
  import { conversationQuotesSchema } from "../conversationSelection.js";
4
5
  // Command 层:信封 / ACK / 命令全集 payload。
5
6
  // conversation rewind 无独立命令(裁决:= editUserQuery 的 UI 入口);
@@ -99,6 +100,7 @@ export const commandPayloadSchemas = {
99
100
  // Desktop Cmd/Ctrl+Enter 只覆盖本次 busy input,不改 session followupMode。
100
101
  // startNow 由 CLI 原子抢占当前 turn,不经过 queue admission。
101
102
  requestedDelivery: z.enum(["startNow", "queue", "guide"]).optional(),
103
+ genUiContext: genUiModelContextSchema.optional(),
102
104
  browserAmbientContext: zcodeBrowserAmbientContextSchema.optional(),
103
105
  // Share handover 只允许当前 session 的一个已导入上下文;完整正文由 runtime 从
104
106
  // 持久化 provenance 解析,不能随 command 从 renderer 传入。
@@ -164,23 +166,23 @@ export const commandPayloadSchemas = {
164
166
  }),
165
167
  // compact 是输入型维护命令:idle 时立即执行,busy/held 时进入 FIFO。
166
168
  // 因为 admission 与当前 revision 无关,不走 CAS;sourceCommandId 提供幂等边界。
167
- compact: z.object({}),
169
+ compact: z.object({ modelSelection: modelSelectionSchema.optional() }),
168
170
  // running 时对稳定 assistant row 可用。
169
171
  forkAssistant: z.object({ target: conversationRowTargetSchema }),
170
172
  applyFileRewind: z.object({ target: conversationRowTargetSchema }),
171
173
  editUserQuery: z
172
174
  .object({
173
- target: conversationRowTargetSchema,
174
- newText: z.string(),
175
+ target: conversationRowTargetSchema,
176
+ newText: z.string(),
175
177
  conversationQuotes: conversationQuotesSchema.optional(),
176
- attachments: z.array(attachmentRefSchema).optional(),
178
+ attachments: z.array(attachmentRefSchema).optional(),
177
179
  // 编辑重发按新的主动发送处理:只接受本次重新 prepare 得到的卡与执行材料,
178
180
  // 不从被 rewind 的历史消息复用旧凭据。
179
181
  modelSelection: modelSelectionSchema.optional(),
180
182
  modelExecution: modelExecutionSchema.optional(),
181
183
  highspeedMeta: highspeedMessageMetaSchema.optional(),
182
- // 缺省 preserve:仅切 conversation branch;rewind 会先安全恢复该轮文件。
183
- workspaceMode: z.enum(["preserve", "rewind"]).optional(),
184
+ // 缺省 preserve:仅切 conversation branch;rewind 会先安全恢复该轮文件。
185
+ workspaceMode: z.enum(["preserve", "rewind"]).optional(),
184
186
  })
185
187
  .superRefine(requireModelSelectionForExecution),
186
188
  retryTurn: z.object({ target: conversationRowTargetSchema }),
@@ -341,6 +343,8 @@ export const ROW_TARGETING_COMMANDS: ReadonlySet<CommandType> = new Set([
341
343
 
342
344
  // ── 信封 ──
343
345
  export const commandEnvelopeSchema = z.object({
346
+ /** Host 可信 attachment 注入;CLI 保留到原 input intent。 */
347
+ inputOrigin: z.enum(["desktop", "mobile"]).optional(),
344
348
  ttft: localTtftContextSchema.optional(),
345
349
  // uuid v7,客户端生成,重试不变。
346
350
  commandId: z.string(),
@@ -28,6 +28,7 @@ import {
28
28
  sessionUsageStateSchema,
29
29
  subagentProjectionStateSchema,
30
30
  workspaceHookAdmissionStateSchema,
31
+ workspaceMcpAdmissionStateSchema,
31
32
  } from "./snapshot.js";
32
33
  import {
33
34
  WORKFLOW_RUNS_LIMITS,
@@ -61,6 +62,7 @@ export const statePatchSchema = z.object({
61
62
  plan: planStateSchema.nullable().optional(),
62
63
  // 软门禁:null = pending 清零(提示条消失);对象 = 待审核状态更新。
63
64
  workspaceHookAdmission: workspaceHookAdmissionStateSchema.nullable().optional(),
65
+ workspaceMcpAdmission: workspaceMcpAdmissionStateSchema.nullable().optional(),
64
66
  });
65
67
  export type StatePatch = z.infer<typeof statePatchSchema>;
66
68
 
@@ -1,3 +1,4 @@
1
+ import { genUiModelContextSchema } from "../gen-ui/contract.js";
1
2
  import { conversationQuotesSchema } from "../conversationSelection.js";
2
3
  import { botGroupInputSourceSchema } from "../bots.js";
3
4
  // CLI admission 后的自包含输入事实。
@@ -78,6 +79,7 @@ export const conversationInputIntentSchema = z
78
79
  modelSelection: modelSelectionSchema.optional(),
79
80
  mode: submissionModeSchema.optional(),
80
81
  planEnabled: z.boolean().optional(),
82
+ genUiContext: genUiModelContextSchema.optional(),
81
83
  sharedContextRefs: z.array(sharedContextRefSchema).max(1).optional(),
82
84
  delivery: conversationInputDeliverySchema,
83
85
  order: conversationInputOrderSchema,
@@ -43,6 +43,7 @@ export type RowActions = NonNullable<z.infer<typeof rowActionsSchema>>;
43
43
  // workflow 轮的行级元数据拆到了 workflow-row-meta.ts(max-lines);名字在此原样再导出。
44
44
  export {
45
45
  backgroundResultOriginMetaSchema,
46
+ subagentResultMetaSchema,
46
47
  workflowBlockedMetaSchema,
47
48
  workflowLaunchMetaSchema,
48
49
  workflowNotificationMetaSchema,
@@ -95,7 +96,7 @@ export const turnHeaderRowSchema = z.object({
95
96
  modelDurationMs: z.number().nonnegative().optional(),
96
97
  toolDurationMs: z.number().nonnegative().optional(),
97
98
  otherDurationMs: z.number().nonnegative().optional(),
98
- // guide 不切 product turn,但每条 accepted guide 都开启独立视觉工作段。
99
+ // guide 不切 product turn;一个 drain 边界按 FIFO 取出全部已等待的 guide,它们共用首条开启的视觉工作段。
99
100
  // 普通 turn 缺省以保持旧 snapshot 兼容;一旦出现 guide,CLI 负责完整投影首段与后续段。
100
101
  workSegments: z.array(turnWorkSegmentSchema).optional(),
101
102
  originMeta: backgroundResultOriginMetaSchema.optional(),
@@ -445,3 +446,20 @@ export const conversationRowSchema = z.discriminatedUnion("kind", [
445
446
  ]);
446
447
  export type ConversationRow = z.infer<typeof conversationRowSchema>;
447
448
  export type ConversationRowKind = ConversationRow["kind"];
449
+
450
+ /**
451
+ * rows.window 的「冻结历史 + 尾轮」分区(spec:docs/ui/chat-virtual-scroll.md「每帧派生成本」)。
452
+ *
453
+ * 只是行数组的视图类型,不上 wire,没有 schema;构造与缓存在 renderer 的 conversationProjectionStore。
454
+ * 放在这里是为了让插件 UI 等 domain 层只依赖协议类型,不依赖 v4 store。
455
+ * history 与 tail 拼接后逐项等于 rows。
456
+ */
457
+ export interface ConversationRowsPartition {
458
+ /** tail 之前的行;只在历史段变化时更换引用。 */
459
+ readonly history: readonly ConversationRow[];
460
+ /**
461
+ * window 的后缀:包含最后一行所在轮的全部行,可能包含多轮(旧轮追加了行、或尾轮内部截断后),
462
+ * 与 history 的轮次不相交。消费方不能假设 tail 只有一轮。
463
+ */
464
+ readonly tail: readonly ConversationRow[];
465
+ }
@@ -215,6 +215,8 @@ export const queueItemSchema = conversationInputIntentSchema.extend({
215
215
  dispatch: conversationInputDispatchSchema.extend({
216
216
  state: z.enum(["queued", "reserved", "promoting"]),
217
217
  }),
218
+ /** Host 准入注入的可信 automation 身份;只允许队列提升原样回放。 */
219
+ automationId: z.string().min(1).optional(),
218
220
  toolDisallowlist: z.array(z.string().min(1)).optional(),
219
221
  });
220
222
  export type QueueItem = z.infer<typeof queueItemSchema>;
@@ -472,6 +474,13 @@ export const workspaceHookAdmissionStateSchema = z.object({
472
474
  });
473
475
  export type WorkspaceHookAdmissionSnapshotState = z.infer<typeof workspaceHookAdmissionStateSchema>;
474
476
 
477
+ export const workspaceMcpAdmissionStateSchema = z.object({
478
+ pendingCount: z.number().int().nonnegative(),
479
+ bundleDigest: z.string().regex(/^[a-f0-9]{64}$/u),
480
+ workspaceIdentity: z.string().min(1).optional(),
481
+ });
482
+ export type WorkspaceMcpAdmissionSnapshotState = z.infer<typeof workspaceMcpAdmissionStateSchema>;
483
+
475
484
  // 冷历史可读不代表 runtime 已可执行;缺席兼容旧发送端。
476
485
  export const runtimeInitializationSchema = z.discriminatedUnion("status", [
477
486
  z.object({ status: z.literal("initializing"), attemptId: z.string().min(1) }),
@@ -522,6 +531,8 @@ export const conversationSnapshotSchema = z.object({
522
531
  // 旧快照/旧发送端不携带此字段 → 解析得 null,不破坏兼容性(遵守冻结规则)。
523
532
  // pendingCount === 0 时投影层置 null(提示条消失)。
524
533
  workspaceHookAdmission: workspaceHookAdmissionStateSchema.nullable().default(null),
534
+ // 旧发送端可缺席;null 或缺席均无 MCP 待审核提示。
535
+ workspaceMcpAdmission: workspaceMcpAdmissionStateSchema.nullable().optional(),
525
536
  // (2026-09-12):原 additive 字段 pluginUiSurfaces 已删除,插件面板 widgetState 只留在
526
537
  // 宿主 renderer 内存。
527
538
  // B 区
@@ -1,4 +1,4 @@
1
- import { localTtftFactsSchema } from "../localTtft.js";
1
+ import { localTtftFactsSchema } from "../local-ttft/contract.js";
2
2
  /* eslint-disable max-lines -- 三个 topic 的 logical/physical/candidate schema 必须共享同一通用传输声明,避免跨文件分叉。 */
3
3
  // 传输外壳:连接握手 / 订阅 / 帧信封。
4
4
  // 阶段为类型占位(后半接通道层时启用),数据形状已按规范定稿。
@@ -29,6 +29,7 @@ export const hostCapabilitiesSchema = z.object({
29
29
  // Wire-compatible:旧 Host 缺失等价于 false;调用方必须用 === true 判断。
30
30
  workspaceHookReview: z.boolean().optional(),
31
31
  independentPlanState: z.boolean().optional(),
32
+ genUiContext: z.boolean().optional(),
32
33
  /**
33
34
  * 本 Host 会发 `workflowRun.*` 键级增量(delta.ts 的两条 op),因而 `workflowRuns` 的
34
35
  * actors / nodes 可以到 1024 而不是旧界的 256。没有这个位的消费者收到的仍是整键
@@ -36,6 +37,13 @@ export const hostCapabilitiesSchema = z.object({
36
37
  */
37
38
  workflowRunDeltas: z.boolean().optional(),
38
39
  historyFirst: z.boolean().optional(),
40
+ /**
41
+ * 本 Host 开启并转发 `v4/conversation/inputPrediction`。旧 Host 缺失等价于 false,
42
+ * 手机必须用 === true 判断,否则不发预测请求。
43
+ */
44
+ inputPrediction: z.boolean().optional(),
45
+ /** 本 Host 支持 rowsRange 的 `residency: "resident-only"`;缺失等价于 false,手机不得预加载。 */
46
+ residentRowsRange: z.boolean().optional(),
39
47
  });
40
48
  export type HostCapabilities = z.infer<typeof hostCapabilitiesSchema>;
41
49
 
@@ -345,6 +353,8 @@ export const V4_METHODS = {
345
353
  conversationUnsubscribe: "v4/conversation/unsubscribe",
346
354
  // 行分页 query(rows/range):只读、无状态、超时重发安全。
347
355
  conversationRowsRange: "v4/conversation/rowsRange",
356
+ // 手机输入预测(input-prediction.ts):只读查询;旧 CLI 不认识时手机静默无 hint。
357
+ conversationInputPrediction: "v4/conversation/inputPrediction",
348
358
  // 当前有效分支的终态 ExitPlanMode 目录;只读、无状态、超时重发安全。
349
359
  conversationPlans: "v4/conversation/plans",
350
360
  conversationFileChanges: "v4/conversation/fileChanges",
@@ -372,6 +382,10 @@ export const V4_METHODS = {
372
382
  // 故收敛为 v4 query 而非 host 直连;旧词 usage/stats、session/usage 就此消费清零。
373
383
  usageStats: "v4/usage/stats",
374
384
  conversationUsage: "v4/conversation/usage",
385
+ // 项目数据彻底删除(schema 在 session-data.ts):measure 只读;purge 幂等,已不存在的会话按 0 字节成功。
386
+ // 新方法偏斜安全:旧 CLI 返回 method-not-found,Host 据此不显示「删除数据」选项。
387
+ sessionDataMeasure: "v4/storage/sessionData/measure",
388
+ sessionDataPurge: "v4/storage/sessionData/purge",
375
389
  // 附件事务:禁止 full-data RPC。每个 chunk 的 decoded bytes <=512KiB,
376
390
  // renderer->host Channel 与 host->CLI NDJSON 都必须逐 request 证明 <=1MiB。
377
391
  attachmentBegin: "v4/attachment/begin",
@@ -526,6 +540,11 @@ export const v4ConversationRowsRangeParamsSchema = z.object({
526
540
  // 取 rowId < beforeRowId 的行;缺省 = 从当前尾部向前。
527
541
  beforeRowId: z.number().optional(),
528
542
  limit: z.number().min(1).max(PROTOCOL_V4_LIMITS.rowsRangeMaxLimit),
543
+ /**
544
+ * 手机预加载只读热会话:会话不在内存就报 proto.sessionNotResident,不冷恢复、不重建投影;
545
+ * Host 同时只复用已运行的工作区 CLI,不为它拉起进程。缺省 = 现有行为(按需冷恢复)。
546
+ */
547
+ residency: z.literal("resident-only").optional(),
529
548
  });
530
549
  export type V4ConversationRowsRangeParams = z.infer<typeof v4ConversationRowsRangeParamsSchema>;
531
550
 
@@ -106,6 +106,12 @@ export const workflowBlockedMetaSchema = z.object({
106
106
  });
107
107
  export type WorkflowBlockedMeta = z.infer<typeof workflowBlockedMetaSchema>;
108
108
 
109
+ // 后台 Agent 的终态与失败原文(contracts 的 SubagentResultMeta);产品状态读它,不解析通知文本。
110
+ export const subagentResultMetaSchema = z.object({
111
+ status: z.enum(["completed", "failed", "killed"]),
112
+ error: z.string().optional(),
113
+ });
114
+
109
115
  export const backgroundResultOriginMetaSchema = z.object({
110
116
  // 三个取值与 contracts 的 BackgroundResultOriginMeta 保持同步。
111
117
  // "workflow" 是 dynamic-workflow run(workId ≡ runId),复用整条后台通知管线。
@@ -118,6 +124,8 @@ export const backgroundResultOriginMetaSchema = z.object({
118
124
  // 提醒载荷,与 workflowNotification 至多在场一个。`.catch(undefined)`:将来它的形状变了,认识这个
119
125
  // 键的客户端把它当作缺席(裸标题行),而不是丢掉整帧。
120
126
  workflowBlocked: workflowBlockedMetaSchema.optional().catch(undefined),
127
+ // 只在 subagent 的单条通知轮上在场;形状不认识时当作缺席,界面退回通知文本。
128
+ subagentResult: subagentResultMetaSchema.optional().catch(undefined),
121
129
  });
122
130
  export type BackgroundResultOriginMeta = z.infer<typeof backgroundResultOriginMetaSchema>;
123
131
 
@@ -0,0 +1,88 @@
1
+ import type { GoalState } from "../../../shared/src/zcode-protocol-v4/snapshot.js";
2
+
3
+ const INTERNAL_GOAL_VERIFICATION_FALLBACK_NEXT_ACTION =
4
+ "Continue verifying and completing the goal.";
5
+
6
+ export interface ConversationGoalIterationSummary {
7
+ iteration: number;
8
+ title: string | null;
9
+ items: GoalState["iterations"][number]["items"];
10
+ completedCount: number;
11
+ totalCount: number;
12
+ completed: boolean;
13
+ verificationOutcome: GoalState["verifications"][number]["outcome"] | null;
14
+ }
15
+
16
+ function visibleNextAction(value: string | undefined): string | null {
17
+ const nextAction = value?.trim();
18
+ if (!nextAction || nextAction === INTERNAL_GOAL_VERIFICATION_FALLBACK_NEXT_ACTION) {
19
+ return null;
20
+ }
21
+ return nextAction;
22
+ }
23
+
24
+ function requiredIterationCount(goal: GoalState): number {
25
+ const maxTodoIteration = (goal.iterations ?? []).reduce(
26
+ (maximum, entry) => Math.max(maximum, entry.iteration),
27
+ 0,
28
+ );
29
+ const maxVerificationIteration = goal.verifications.reduce(
30
+ (maximum, entry) => Math.max(maximum, entry.iteration),
31
+ 0,
32
+ );
33
+ const latestVerification = goal.verifications.findLast(
34
+ (entry) => entry.iteration === goal.iteration,
35
+ );
36
+ const continuationIsOpen =
37
+ goal.status === "active" ||
38
+ goal.status === "notSatisfied" ||
39
+ (goal.status === "paused" && latestVerification?.outcome === "notSatisfied");
40
+ const statusIteration = continuationIsOpen
41
+ ? goal.iteration + 1
42
+ : Math.max(1, goal.iteration);
43
+ return Math.max(1, statusIteration, maxTodoIteration, maxVerificationIteration);
44
+ }
45
+
46
+ /** 从 V4 权威 goal 投影构造右上角逐轮摘要;不从 renderer timeline 反推轮次。 */
47
+ export function buildConversationGoalIterationSummaries(
48
+ goal: GoalState,
49
+ ): ConversationGoalIterationSummary[] {
50
+ const todoByIteration = new Map(
51
+ (goal.iterations ?? []).map((entry) => [entry.iteration, entry.items] as const),
52
+ );
53
+ const verificationByIteration = new Map(
54
+ goal.verifications.map((entry) => [entry.iteration, entry] as const),
55
+ );
56
+ const rows: ConversationGoalIterationSummary[] = [];
57
+
58
+ for (let iteration = 1; iteration <= requiredIterationCount(goal); iteration += 1) {
59
+ const items = todoByIteration.get(iteration) ?? [];
60
+ const completedCount = items.filter((item) => item.status === "completed").length;
61
+ const previousVerification = verificationByIteration.get(iteration - 1);
62
+ rows.push({
63
+ iteration,
64
+ title:
65
+ iteration === 1
66
+ ? (goal.summaryTitle?.trim() || goal.objective.trim() || null)
67
+ : visibleNextAction(previousVerification?.nextAction),
68
+ items,
69
+ completedCount,
70
+ totalCount: items.length,
71
+ completed: items.length > 0 && completedCount >= items.length,
72
+ verificationOutcome: verificationByIteration.get(iteration)?.outcome ?? null,
73
+ });
74
+ }
75
+
76
+ return rows.sort((left, right) => {
77
+ if (left.completed !== right.completed) return left.completed ? -1 : 1;
78
+ return left.iteration - right.iteration;
79
+ });
80
+ }
81
+
82
+ export function getConversationGoalElapsedSeconds(goal: GoalState, now: number): number {
83
+ const baseSeconds = Math.max(0, Math.floor(goal.timeUsedSeconds ?? 0));
84
+ const isRunning =
85
+ goal.status === "active" || goal.status === "verifying" || goal.status === "notSatisfied";
86
+ if (!isRunning || goal.activeRunStartedAtMs == null) return baseSeconds;
87
+ return baseSeconds + Math.max(0, Math.floor((now - goal.activeRunStartedAtMs) / 1000));
88
+ }