@clagentic/console 1.11.0 → 1.12.0-beta.2

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 (99) hide show
  1. package/README.md +1 -0
  2. package/bin/cli.js +14 -2
  3. package/lib/build-changes.js +8 -2
  4. package/lib/build-sha.json +2 -2
  5. package/lib/build-update-offer.js +31 -7
  6. package/lib/claude-settings-env.js +142 -0
  7. package/lib/cli/arg-validation.js +1 -1
  8. package/lib/cli/duration.js +27 -0
  9. package/lib/cli/ipc-subcommands.js +26 -0
  10. package/lib/cli/schedule-input-subcommands.js +1 -22
  11. package/lib/cli/session-subcommands.js +38 -7
  12. package/lib/daemon.js +26 -0
  13. package/lib/notification-prefs.js +257 -0
  14. package/lib/notify/core.js +175 -0
  15. package/lib/notify/index.js +31 -0
  16. package/lib/notify/live-prompts.js +36 -0
  17. package/lib/notify/payload.js +63 -0
  18. package/lib/notify/target.js +5 -0
  19. package/lib/notify/type-registry.js +64 -0
  20. package/lib/notify/types.js +20 -0
  21. package/lib/open-sockets.js +27 -0
  22. package/lib/outbound-access.js +74 -0
  23. package/lib/outbound-entry.js +109 -0
  24. package/lib/outbound-ledger.js +306 -0
  25. package/lib/outbound-query.js +153 -0
  26. package/lib/pending-prompts.js +31 -16
  27. package/lib/project-connection.js +40 -1
  28. package/lib/project-filesystem.js +20 -5
  29. package/lib/project-http.js +9 -1
  30. package/lib/project-loop.js +25 -13
  31. package/lib/project-notifications.js +206 -42
  32. package/lib/project-prompt-responses.js +1 -1
  33. package/lib/project-sessions.js +30 -1
  34. package/lib/project.js +15 -1
  35. package/lib/prompt-registry.js +92 -24
  36. package/lib/prompt-reminders.js +282 -0
  37. package/lib/prompt-source.js +43 -8
  38. package/lib/public/app.js +1 -0
  39. package/lib/public/css/diagnostics.css +28 -0
  40. package/lib/public/css/home-hub.css +12 -0
  41. package/lib/public/css/icon-strip.css +9 -0
  42. package/lib/public/css/input.css +93 -0
  43. package/lib/public/css/menus.css +22 -0
  44. package/lib/public/css/mobile-nav.css +11 -0
  45. package/lib/public/css/server-settings.css +24 -0
  46. package/lib/public/css/sidebar.css +24 -0
  47. package/lib/public/index.html +6 -0
  48. package/lib/public/modules/activity-state.js +20 -4
  49. package/lib/public/modules/app-connection.js +22 -5
  50. package/lib/public/modules/app-header.js +2 -0
  51. package/lib/public/modules/app-messages.js +6 -2
  52. package/lib/public/modules/app-notifications.js +65 -40
  53. package/lib/public/modules/app-projects.js +3 -1
  54. package/lib/public/modules/app-rate-limit.js +12 -6
  55. package/lib/public/modules/app-rendering.js +26 -3
  56. package/lib/public/modules/build-update-sessions.js +81 -0
  57. package/lib/public/modules/collapsible-text.js +77 -0
  58. package/lib/public/modules/diagnostics.js +9 -2
  59. package/lib/public/modules/env-shadow-notice.js +34 -0
  60. package/lib/public/modules/keep-alive.js +66 -0
  61. package/lib/public/modules/notification-prefs.js +151 -0
  62. package/lib/public/modules/notify-client.js +153 -0
  63. package/lib/public/modules/project-settings.js +5 -0
  64. package/lib/public/modules/prompt-reminders-setting.js +13 -0
  65. package/lib/public/modules/prompt-state.js +3 -3
  66. package/lib/public/modules/prompts.js +1 -1
  67. package/lib/public/modules/server-settings.js +3 -0
  68. package/lib/public/modules/sidebar-mobile.js +8 -0
  69. package/lib/public/modules/sidebar-sessions.js +27 -0
  70. package/lib/public/modules/terminal-links.js +175 -0
  71. package/lib/public/modules/terminal.js +19 -103
  72. package/lib/public/notify-sw-logic.js +300 -0
  73. package/lib/public/notify-target.js +97 -0
  74. package/lib/public/sw.js +15 -62
  75. package/lib/push-audience.js +84 -0
  76. package/lib/push.js +87 -20
  77. package/lib/runtime-env.js +1 -0
  78. package/lib/sdk-bridge.js +83 -36
  79. package/lib/sdk-message-processor.js +148 -49
  80. package/lib/sender-ref.js +3 -0
  81. package/lib/server.js +80 -4
  82. package/lib/session-directory.js +6 -4
  83. package/lib/session-input-ipc.js +11 -1
  84. package/lib/session-input.js +95 -14
  85. package/lib/session-liveness.js +53 -0
  86. package/lib/session-reclaim.js +37 -2
  87. package/lib/sessions.js +58 -4
  88. package/lib/usage-store.js +403 -0
  89. package/lib/vendor-auth.js +33 -2
  90. package/lib/ws-schema.js +13 -2
  91. package/lib/yoke/adapters/claude-vendor.js +11 -1
  92. package/lib/yoke/adapters/claude.js +17 -0
  93. package/lib/yoke/adapters/codex.js +5 -1
  94. package/lib/yoke/auth-checks.js +17 -2
  95. package/lib/yoke/index.js +21 -3
  96. package/lib/yoke/provider-credentials.js +24 -0
  97. package/lib/yoke/registry.js +8 -1
  98. package/package.json +2 -2
  99. package/scripts/check-test-count.js +14 -0
@@ -50,6 +50,8 @@ var sessionReclaim = require("./session-reclaim");
50
50
  var { promptLabel } = require("./prompt-label");
51
51
  var promptSource = require("./prompt-source");
52
52
  var { logField } = require("./log-field");
53
+ var { notifyFor } = require("./notify");
54
+ var { withPromptLabel } = require("./prompt-label");
53
55
  var { ownValue, putOwn, hasOwnKey } = codecs;
54
56
 
55
57
  var KINDS = promptKinds.KINDS;
@@ -62,6 +64,13 @@ function cancelReasonForAbort(signal) {
62
64
  return signal && signal.reason === "expired" ? "expired" : "aborted";
63
65
  }
64
66
 
67
+ // Who ended a prompt through its signal: the worker or app-server that gave up
68
+ // waiting ("expired"), otherwise the session's own abort, close or query end,
69
+ // which is what cancels every callback a query still has out.
70
+ function cancelSourceForAbort(signal) {
71
+ return signal && signal.reason === "expired" ? promptSource.CANCEL.WORKER : promptSource.CANCEL.SESSION_END;
72
+ }
73
+
65
74
  var RESOLVED_FIELDS = ["decision", "answers", "skipped", "action"];
66
75
 
67
76
  function deepFreeze(value) {
@@ -89,13 +98,13 @@ function terminalOf(e) {
89
98
  return e.requestId ? { id: e.requestId, state: Object.assign({ state: "resolved" }, pick(e, RESOLVED_FIELDS)) } : null;
90
99
  case "prompt_cancel":
91
100
  case "permission_cancel":
92
- return e.requestId ? { id: e.requestId, state: { state: "cancelled", reason: e.reason || null } } : null;
101
+ return e.requestId ? { id: e.requestId, state: { state: "cancelled", reason: e.reason || null, source: e.source || promptSource.CANCEL.UNSPECIFIED } } : null;
93
102
  case "ask_user_answered":
94
103
  if (!e.toolId) return null;
95
104
  if (e.answers && Object.keys(e.answers).length) return { id: e.toolId, state: { state: "resolved", answers: e.answers } };
96
105
  // A recorded skip is an answer, not an ending nobody chose.
97
106
  if (e.skipped === true) return { id: e.toolId, state: { state: "resolved", skipped: true } };
98
- return { id: e.toolId, state: { state: "cancelled", reason: null } };
107
+ return { id: e.toolId, state: { state: "cancelled", reason: null, source: promptSource.CANCEL.UNSPECIFIED } };
99
108
  default:
100
109
  return null;
101
110
  }
@@ -137,6 +146,7 @@ function createPromptRegistry(deps) {
137
146
  var onProcessingChanged = deps.onProcessingChanged || function () {};
138
147
  var getProjectName = deps.getProjectName || function () { return ""; };
139
148
  var getProjectSlug = deps.getProjectSlug || function () { return ""; };
149
+ var getPushModule = deps.getPushModule || function () { return null; };
140
150
 
141
151
  // Project-scope prompts (no session): requestId -> record.
142
152
  var projectPrompts = Object.create(null);
@@ -185,9 +195,30 @@ function createPromptRegistry(deps) {
185
195
  return null;
186
196
  }
187
197
 
188
- function dismissNotification(requestId) {
198
+ // The one close for a settled prompt: the core closes its banner and its
199
+ // push on every surface. The keys come from the target alone, so this holds
200
+ // after a restart and during the shutdown settle. A question's notification
201
+ // is keyed by its tool-use id, which is its request id unless another prompt
202
+ // already held that id. With no session to name (a project-level prompt, or a
203
+ // stale answer no session claims) only the request's banners are retired.
204
+ function closeNotifications(session, requestId, record) {
205
+ // A request id that is no string (a malformed client message) names no
206
+ // prompt, and must not widen the close to the session's own notification.
207
+ if (typeof requestId !== "string") return;
208
+ var core = notifyFor(getPushModule(), getNotificationsModule());
209
+ var slug = getProjectSlug();
210
+ if (!session) {
211
+ core.resolve({ slug: slug, requestId: requestId });
212
+ return;
213
+ }
214
+ var audience = { ownerId: session.ownerId };
215
+ var scope = { slug: slug, sessionId: session.localId };
216
+ var ids = [requestId];
217
+ if (record && record.toolUseId && record.toolUseId !== requestId) ids.push(record.toolUseId);
218
+ core.resolve(ids.map(function (id) { return Object.assign({ requestId: id }, scope); }), audience);
219
+ // A reminder that names this prompt is now wrong, or gone.
189
220
  var nm = getNotificationsModule();
190
- if (nm && typeof nm.dismissByRequestId === "function") nm.dismissByRequestId(requestId);
221
+ if (nm && typeof nm.reconcileReminders === "function") nm.reconcileReminders();
191
222
  }
192
223
 
193
224
  function ownerTaskOf(session, record) {
@@ -271,7 +302,7 @@ function createPromptRegistry(deps) {
271
302
  var detach = detachers.get(entry.record);
272
303
  if (detach) detach();
273
304
  if (entry.kind.journal) {
274
- dismissNotification(requestId);
305
+ closeNotifications(session, requestId, entry.record);
275
306
  if (event) sendAndRecord(session, event);
276
307
  onProcessingChanged();
277
308
  if (info) logSettled(session, requestId, entry, info);
@@ -286,14 +317,17 @@ function createPromptRegistry(deps) {
286
317
  return true;
287
318
  }
288
319
 
320
+ // opts.source is one of lib/prompt-source.js's cancel sources; a missing or
321
+ // unknown one is recorded as "unspecified" with a warning.
289
322
  function cancelEntry(session, requestId, entry, reason, opts) {
323
+ var source = promptSource.cancelSource(opts && opts.source, reason);
290
324
  var event = (entry.kind.journal && !(opts && opts.record === false))
291
- ? { type: "prompt_cancel", requestId: requestId, kind: entry.kind.name, reason: reason }
325
+ ? { type: "prompt_cancel", requestId: requestId, kind: entry.kind.name, reason: reason, source: source }
292
326
  : null;
293
327
  return settle(session, requestId, entry, entry.kind.cancelled(reason), event, {
294
328
  status: "cancelled",
295
329
  reason: reason,
296
- source: (opts && opts.source) || promptSource.fromCancel(reason),
330
+ source: source,
297
331
  });
298
332
  }
299
333
 
@@ -325,8 +359,9 @@ function createPromptRegistry(deps) {
325
359
  * @param {AbortSignal} [opts.signal] - ends the prompt (aborted, or
326
360
  * expired when the signal's reason is "expired").
327
361
  * @param {number} [opts.timeoutMs] - ends the prompt as expired.
328
- * @param {object} [opts.notification] - permission_request notification
329
- * payload (title, body, slug, ...); dismissed when the prompt settles.
362
+ * @param {object} [opts.notification] - permission_request banner
363
+ * content (title, body, slug, ...), delivered through the notification
364
+ * core and closed with it when the prompt settles.
330
365
  * @returns {{requestId: string, answer: Promise<*>, pending: boolean}}
331
366
  * pending is false when its signal was already aborted: no prompt was
332
367
  * opened or announced, and answer carries the kind's cancelled value.
@@ -377,7 +412,7 @@ function createPromptRegistry(deps) {
377
412
 
378
413
  var cleanups = [];
379
414
  if (signal && typeof signal.addEventListener === "function") {
380
- var onAbort = function () { cancelEntry(owner, requestId, entry, cancelReasonForAbort(signal)); };
415
+ var onAbort = function () { cancelEntry(owner, requestId, entry, cancelReasonForAbort(signal), { source: cancelSourceForAbort(signal) }); };
381
416
  signal.addEventListener("abort", onAbort, { once: true });
382
417
  if (typeof signal.removeEventListener === "function") {
383
418
  cleanups.push(function () { signal.removeEventListener("abort", onAbort); });
@@ -386,7 +421,7 @@ function createPromptRegistry(deps) {
386
421
  var timeoutMs = opts.timeoutMs != null ? opts.timeoutMs : kind.defaultTimeoutMs;
387
422
  if (timeoutMs) {
388
423
  var timer = setTimeout(function () {
389
- cancelEntry(owner, requestId, entry, "expired", { source: promptSource.fromTimeout() });
424
+ cancelEntry(owner, requestId, entry, "expired", { source: promptSource.CANCEL.TIMEOUT });
390
425
  }, timeoutMs);
391
426
  cleanups.push(function () { clearTimeout(timer); });
392
427
  }
@@ -394,12 +429,22 @@ function createPromptRegistry(deps) {
394
429
  detachers.set(record, function () { cleanups.forEach(function (fn) { fn(); }); });
395
430
  }
396
431
 
397
- if (kind.journal && opts.notification) {
398
- var nm = getNotificationsModule();
399
- if (nm && typeof nm.notify === "function") {
400
- var kindFields = typeof kind.notificationFields === "function" ? kind.notificationFields(record) : {};
401
- nm.notify("permission_request", Object.assign({}, opts.notification, kindFields, labelFields(record), { requestId: requestId, kind: kind.name }));
402
- }
432
+ if (kind.journal && opts.notification && session) {
433
+ var kindFields = typeof kind.notificationFields === "function" ? kind.notificationFields(record) : {};
434
+ var pushText = opts.notification.push || null;
435
+ var banner = Object.assign({}, opts.notification, kindFields, labelFields(record), { kind: kind.name });
436
+ delete banner.push;
437
+ // One notify() per prompt: the emitter supplies the text of each surface
438
+ // and the core picks the surfaces from the type's registration. The
439
+ // audience is the session's own, for the push and the banner alike.
440
+ notifyFor(getPushModule(), getNotificationsModule()).notify("permission_request", {
441
+ slug: getProjectSlug(),
442
+ sessionId: session.localId,
443
+ requestId: requestId,
444
+ }, {
445
+ push: pushText ? { title: pushText.title, body: withPromptLabel(record.label, pushText.body) } : null,
446
+ banner: banner,
447
+ }, { ownerId: session.ownerId || null });
403
448
  }
404
449
  return { requestId: requestId, answer: answer, pending: true, label: record.label || null };
405
450
  }
@@ -471,7 +516,7 @@ function createPromptRegistry(deps) {
471
516
  opts = opts || {};
472
517
  var found = find(requestId, opts.session);
473
518
  if (!found) {
474
- dismissNotification(requestId);
519
+ closeNotifications(opts.session || null, requestId, null);
475
520
  return { status: "stale" };
476
521
  }
477
522
  if (opts.kinds && opts.kinds.indexOf(found.kind.name) === -1) return { status: "stale" };
@@ -517,7 +562,7 @@ function createPromptRegistry(deps) {
517
562
  function endTurn(session) {
518
563
  cancelWhere(session, "turn_ended", function (record) {
519
564
  return !ownedByTrackedTask(session, record);
520
- });
565
+ }, { source: promptSource.CANCEL.SESSION_END });
521
566
  }
522
567
 
523
568
  /**
@@ -532,7 +577,7 @@ function createPromptRegistry(deps) {
532
577
  cancelWhere(session, "query_ended", function (record) {
533
578
  var ours = record.query ? record.query === query : !!ownsSession;
534
579
  return ours && !ownedByTrackedTask(session, record);
535
- });
580
+ }, { source: promptSource.CANCEL.SESSION_END });
536
581
  if (ownsSession) pruneTasks(session);
537
582
  }
538
583
 
@@ -541,7 +586,11 @@ function createPromptRegistry(deps) {
541
586
  pruneTasks(session);
542
587
  }
543
588
 
544
- /** End every prompt of a session (rewind, context clear, deletion). */
589
+ /**
590
+ * End every prompt of a session (rewind, context clear, deletion).
591
+ * opts.source names who ended them (lib/prompt-source.js); opts.record false
592
+ * keeps the ending out of history.
593
+ */
545
594
  function cancelSession(session, reason, opts) {
546
595
  cancelWhere(session, reason, function () { return true; }, opts);
547
596
  session.activeTaskToolIds = Object.create(null);
@@ -557,11 +606,11 @@ function createPromptRegistry(deps) {
557
606
  Object.keys(index).forEach(function (id) { localIds.add(index[id]); });
558
607
  localIds.forEach(function (localId) {
559
608
  var session = getSession ? getSession(localId) : null;
560
- if (session) cancelWhere(session, "shutdown", function () { return true; });
609
+ if (session) cancelWhere(session, "shutdown", function () { return true; }, { source: promptSource.CANCEL.DAEMON_RESTART });
561
610
  });
562
611
  Object.keys(projectPrompts).forEach(function (id) {
563
612
  var record = projectPrompts[id];
564
- if (record) cancelEntry(null, id, entryOf(null, record), "shutdown");
613
+ if (record) cancelEntry(null, id, entryOf(null, record), "shutdown", { source: promptSource.CANCEL.DAEMON_RESTART });
565
614
  });
566
615
  }
567
616
 
@@ -670,6 +719,24 @@ function createPromptRegistry(deps) {
670
719
  return entries(session).filter(function (e) { return e.kind.journal; }).length;
671
720
  }
672
721
 
722
+ /**
723
+ * What the reminder sweep needs to know of each operator-facing pending
724
+ * prompt of a session: when it opened, what it gates and for which
725
+ * background agent. Read-only facts; never the record or its resolver.
726
+ */
727
+ function pendingInfo(session) {
728
+ return entries(session).filter(function (e) { return e.kind.journal; }).map(function (e) {
729
+ return {
730
+ requestId: e.id,
731
+ toolUseId: typeof e.record.toolUseId === "string" ? e.record.toolUseId : null,
732
+ kind: e.kind.name,
733
+ toolName: typeof e.record.toolName === "string" ? e.record.toolName : null,
734
+ agent: (e.record.label && e.record.label.agent) || "",
735
+ createdAt: typeof e.record.createdAt === "number" ? e.record.createdAt : 0,
736
+ };
737
+ });
738
+ }
739
+
673
740
  /**
674
741
  * Returns a mapper for replayed history items that stamps every item
675
742
  * opening a prompt with its authoritative promptState, so a client renders
@@ -706,7 +773,7 @@ function createPromptRegistry(deps) {
706
773
  if (!state) {
707
774
  var mayStillOpen = item.type === "tool_executing" && !askBeforeTurnEnd.has(item);
708
775
  if (mayStillOpen) return item;
709
- state = { state: "cancelled", reason: "stale" };
776
+ state = { state: "cancelled", reason: "stale", source: promptSource.CANCEL.UNSPECIFIED };
710
777
  }
711
778
  return Object.assign({}, item, { promptState: state });
712
779
  };
@@ -736,6 +803,7 @@ function createPromptRegistry(deps) {
736
803
  revokeGrant: revokeGrant,
737
804
  pendingMessages: pendingMessages,
738
805
  pendingCount: pendingCount,
806
+ pendingInfo: pendingInfo,
739
807
  replayAnnotator: replayAnnotator,
740
808
  };
741
809
  }
@@ -0,0 +1,282 @@
1
+ "use strict";
2
+
3
+ // Reminders for operator prompts nobody has answered.
4
+ //
5
+ // A permission prompt has no timeout, so a prompt the operator never saw (their
6
+ // own, or a background agent's) waits for ever and pins its session. A reminder
7
+ // is the notification that closes that: it repeats at growing intervals, through
8
+ // the notification paths that already exist (the in-app banner and push).
9
+ //
10
+ // Derived, not timed: nothing is scheduled per prompt. A sweep reads the pending
11
+ // prompts from each session's prompt registry (lib/prompt-registry.js) and
12
+ // decides from their opening times what is due:
13
+ // - the schedule is offsets from the oldest pending prompt's opening: the
14
+ // configured steps, then one more every `repeatMs`;
15
+ // - a session has at most ONE live reminder; the next one replaces it, with
16
+ // the current count, the oldest age, and the waiting agents and tools;
17
+ // - a prompt that settles (an answer, a cancel, an end of turn) clears or
18
+ // recounts the reminder at once, through the notifications module's
19
+ // reconcile hook, not at the next tick;
20
+ // - the sweep only reads. A reminder never opens, settles or touches a prompt,
21
+ // and nothing here is consulted by the idle reaper.
22
+ // Nothing survives a restart because no prompt does (the registry ends every
23
+ // prompt at shutdown): a prompt that is pending again after one (the agent
24
+ // asked once more) starts its own schedule from its own opening time.
25
+ //
26
+ // Per-session switch: session.promptRemindersOff (persisted with the session).
27
+ // Configuration: daemon.json `promptReminders` { enabled, scheduleMinutes,
28
+ // repeatMinutes }. The schedule is read on every sweep, so a change needs no
29
+ // restart. The sweep tick is a constant, not configuration: a timer period taken
30
+ // from a config file is a value that can be set to nothing. A `tickSeconds` key
31
+ // left in daemon.json is ignored.
32
+
33
+ var { notifyFor } = require("./notify");
34
+
35
+ var DEFAULT_SCHEDULE_MINUTES = [5, 15, 30];
36
+ var DEFAULT_REPEAT_MINUTES = 60;
37
+ var SWEEP_TICK_MS = 30000;
38
+ var MAX_NAMED = 3;
39
+
40
+ // A configured interval is at least one whole minute once rounded to
41
+ // milliseconds. Anything smaller would round the repeat step to 0 and make the
42
+ // due count divide by zero.
43
+ var MIN_INTERVAL_MS = 60000;
44
+
45
+ // The one validator for every configurable interval: the minutes, or null.
46
+ function intervalMinutes(v) {
47
+ var ms = typeof v === "number" && isFinite(v) && v > 0 ? Math.round(v * 60000) : 0;
48
+ return ms >= MIN_INTERVAL_MS ? v : null;
49
+ }
50
+
51
+ var warned = Object.create(null);
52
+
53
+ // One warning per key and value, not one per sweep: the schedule is re-read on
54
+ // every tick.
55
+ function warnOnce(key, value) {
56
+ var id = key + "=" + JSON.stringify(value);
57
+ if (warned[id]) return;
58
+ warned[id] = true;
59
+ console.warn("[prompt-reminders] promptReminders." + key + " must be at least 1 minute; using the default");
60
+ }
61
+
62
+ function repeatMinutes(v) {
63
+ var ok = intervalMinutes(v);
64
+ if (ok !== null) return ok;
65
+ if (v !== undefined) warnOnce("repeatMinutes", v);
66
+ return DEFAULT_REPEAT_MINUTES;
67
+ }
68
+
69
+ /**
70
+ * The schedule daemon.json asks for, with the defaults wherever it is silent
71
+ * or malformed.
72
+ * @param {?object} cfg the daemon.json `promptReminders` value
73
+ * @returns {{enabled: boolean, offsetsMs: number[], repeatMs: number}}
74
+ */
75
+ function resolveSchedule(cfg) {
76
+ cfg = cfg && typeof cfg === "object" ? cfg : {};
77
+ var minutes = Array.isArray(cfg.scheduleMinutes) ? cfg.scheduleMinutes.map(intervalMinutes) : null;
78
+ if (!minutes || minutes.length === 0 || minutes.indexOf(null) !== -1) {
79
+ if (cfg.scheduleMinutes !== undefined) warnOnce("scheduleMinutes", cfg.scheduleMinutes);
80
+ minutes = DEFAULT_SCHEDULE_MINUTES;
81
+ }
82
+ // Increasing, so "due" is a count.
83
+ minutes = minutes.slice().sort(function (a, b) { return a - b; });
84
+ return {
85
+ enabled: cfg.enabled !== false,
86
+ offsetsMs: minutes.map(function (m) { return Math.round(m * 60000); }),
87
+ repeatMs: Math.round(repeatMinutes(cfg.repeatMinutes) * 60000),
88
+ };
89
+ }
90
+
91
+ /** How many reminders a prompt this old has earned. */
92
+ function dueCount(ageMs, schedule) {
93
+ var offsets = schedule.offsetsMs;
94
+ var n = 0;
95
+ while (n < offsets.length && ageMs >= offsets[n]) n++;
96
+ if (n < offsets.length) return n;
97
+ return n + Math.floor((ageMs - offsets[offsets.length - 1]) / schedule.repeatMs);
98
+ }
99
+
100
+ function waitingFor(ageMs) {
101
+ // Under a minute only a short configured schedule gets here.
102
+ if (ageMs < 60000) return Math.max(1, Math.floor(ageMs / 1000)) + " sec";
103
+ var min = Math.floor(ageMs / 60000);
104
+ return min >= 120 ? Math.floor(min / 60) + " h" : min + " min";
105
+ }
106
+
107
+ function uniqueNamed(values) {
108
+ var seen = Object.create(null);
109
+ var out = [];
110
+ values.forEach(function (v) {
111
+ if (v && !seen[v]) { seen[v] = true; out.push(v); }
112
+ });
113
+ return out;
114
+ }
115
+
116
+ function nameList(values) {
117
+ var shown = values.slice(0, MAX_NAMED).join(", ");
118
+ return values.length > MAX_NAMED ? shown + " +" + (values.length - MAX_NAMED) + " more" : shown;
119
+ }
120
+
121
+ /**
122
+ * The reminder's text for one session's pending prompts.
123
+ * @param {{project: string, session: string, infos: object[], ageMs: number}} s
124
+ */
125
+ function describe(s) {
126
+ var count = s.infos.length;
127
+ var agents = uniqueNamed(s.infos.map(function (i) { return i.agent; }));
128
+ var tools = uniqueNamed(s.infos.map(function (i) { return i.toolName; }));
129
+ var parts = [];
130
+ if (s.project) parts.push("Project: " + s.project);
131
+ if (s.session) parts.push("Session: " + s.session);
132
+ if (agents.length) parts.push("Agent: " + nameList(agents));
133
+ if (tools.length) parts.push("Tool: " + nameList(tools));
134
+ return {
135
+ title: count + (count === 1 ? " prompt" : " prompts") + " waiting, oldest " + waitingFor(s.ageMs),
136
+ body: parts.join(" | "),
137
+ agents: agents,
138
+ tools: tools,
139
+ };
140
+ }
141
+
142
+ /**
143
+ * @param {object} deps
144
+ * @param {function(): Array<{slug: string, project: string, session: object, prompts: object}>} deps.listSessions
145
+ * every session of every project with its prompt registry.
146
+ * @param {object} deps.notifications the in-app banner store (show, dismissKeys,
147
+ * setReminderReconciler).
148
+ * @param {?object} [deps.push] the push module.
149
+ * @param {function(): ?object} [deps.getConfig] daemon.json `promptReminders`.
150
+ * @param {function(): boolean} [deps.isMultiUser] true when the daemon has
151
+ * accounts; absent means single-user. A throw reads as multi-user, the
152
+ * stricter mode.
153
+ * @param {function(): number} [deps.now]
154
+ */
155
+ function createPromptReminders(deps) {
156
+ var notifications = deps.notifications;
157
+ var push = deps.push || null;
158
+ var getConfig = deps.getConfig || function () { return null; };
159
+ var now = deps.now || Date.now;
160
+ var isMultiUser = deps.isMultiUser || function () { return false; };
161
+ var listAdmins = deps.listAdmins;
162
+ // sessionKey -> { sent: reminders already sent, live: a reminder is showing,
163
+ // sig: the prompts it names }
164
+ var states = Object.create(null);
165
+ var timer = null;
166
+
167
+ // What a live reminder says about the set of waiting prompts; a change in it
168
+ // (one answered, one more opened) recounts the reminder, no change leaves it.
169
+ function signature(infos) {
170
+ return infos.map(function (i) { return i.requestId; }).join(",");
171
+ }
172
+
173
+ function keyOf(slug, session) {
174
+ return slug + ":" + session.localId;
175
+ }
176
+
177
+ function audienceOf(session) {
178
+ return { ownerId: session.ownerId, isMultiUser: isMultiUser, listAdmins: listAdmins };
179
+ }
180
+
181
+ function core() {
182
+ return notifyFor(push, notifications);
183
+ }
184
+
185
+ // One notify() delivers the banner and, when due, the push, and the core
186
+ // collapses a repeat under the session's key: no reminder is replaced here.
187
+ function raise(entry, infos, ageMs, key, withPush) {
188
+ states[key].sig = signature(infos);
189
+ var target = { slug: entry.slug, sessionId: entry.session.localId };
190
+ var audience = audienceOf(entry.session);
191
+ states[key].scope = { target: target, audience: audience };
192
+ var text = describe({ project: entry.project, session: entry.session.title, infos: infos, ageMs: ageMs });
193
+ core().notify("permission_reminder", Object.assign({ requestId: infos[0].requestId }, target),
194
+ {
195
+ push: withPush ? { title: text.title, body: text.body } : null,
196
+ banner: {
197
+ title: text.title,
198
+ body: text.body,
199
+ requestIds: infos.map(function (i) { return i.requestId; }),
200
+ count: infos.length,
201
+ ageMs: ageMs,
202
+ agents: text.agents,
203
+ tools: text.tools,
204
+ },
205
+ }, audience);
206
+ }
207
+
208
+ // Closes the session's reminder, banner and push alike, if one was raised.
209
+ function close(key) {
210
+ var scope = states[key] && states[key].scope;
211
+ if (scope) core().resolve(scope.target, scope.audience);
212
+ }
213
+
214
+ function clear(key) {
215
+ if (states[key]) {
216
+ close(key);
217
+ delete states[key];
218
+ }
219
+ }
220
+
221
+ /**
222
+ * One pass over every pending prompt.
223
+ * @param {boolean} sendDue true on the clock: send what the schedule says is
224
+ * due. false when a prompt settled: only clear or recount a live reminder.
225
+ */
226
+ function sweep(sendDue) {
227
+ var schedule = resolveSchedule(getConfig());
228
+ var t = now();
229
+ var seen = Object.create(null);
230
+ deps.listSessions().forEach(function (entry) {
231
+ var session = entry.session;
232
+ var key = keyOf(entry.slug, session);
233
+ var infos = entry.prompts.pendingInfo(session);
234
+ if (!infos.length || session.promptRemindersOff === true || !schedule.enabled) return;
235
+ seen[key] = true;
236
+ infos.sort(function (a, b) { return a.createdAt - b.createdAt; });
237
+ var ageMs = Math.max(0, t - infos[0].createdAt);
238
+ var state = states[key] || (states[key] = { sent: 0, live: false, sig: "" });
239
+ var due = dueCount(ageMs, schedule);
240
+ // The oldest prompt was answered and a newer one remains: its schedule
241
+ // starts from its own opening.
242
+ if (due < state.sent) state.sent = due;
243
+ if (sendDue && due > state.sent) {
244
+ state.sent = due;
245
+ state.live = true;
246
+ raise(entry, infos, ageMs, key, true);
247
+ } else if (state.live && state.sig !== signature(infos)) {
248
+ raise(entry, infos, ageMs, key, false);
249
+ }
250
+ });
251
+ Object.keys(states).forEach(function (key) {
252
+ if (!seen[key]) clear(key);
253
+ });
254
+ }
255
+
256
+ function start() {
257
+ if (timer) return;
258
+ notifications.setReminderReconciler(function () { sweep(false); });
259
+ timer = setInterval(function () { sweep(true); }, SWEEP_TICK_MS);
260
+ // The sweep must not keep a daemon that is going away alive.
261
+ if (timer.unref) timer.unref();
262
+ }
263
+
264
+ // The daemon is going away: a reminder still shown would outlive the prompt
265
+ // it nags about, and the reconciler below is detached before the prompts
266
+ // settle, so the reminder pushes are closed here.
267
+ function stop() {
268
+ Object.keys(states).forEach(close);
269
+ if (timer) { clearInterval(timer); timer = null; }
270
+ notifications.setReminderReconciler(null);
271
+ }
272
+
273
+ return { start: start, stop: stop, sweep: sweep };
274
+ }
275
+
276
+ module.exports = {
277
+ createPromptReminders: createPromptReminders,
278
+ resolveSchedule: resolveSchedule,
279
+ dueCount: dueCount,
280
+ SWEEP_TICK_MS: SWEEP_TICK_MS,
281
+ describe: describe,
282
+ };
@@ -7,8 +7,22 @@
7
7
  //
8
8
  // { kind: "ws", clientId, user } an answer from a browser connection
9
9
  // { kind: "push", action, user } an answer from a push notification
10
- // { kind: "timeout" } the prompt's own timer ran out
11
- // { kind: "cancel", reason } the scope that owned the prompt ended
10
+ //
11
+ // A prompt that ends without an answer carries a cancel source instead: one
12
+ // string from a stable vocabulary, recorded on the prompt_cancel event, in the
13
+ // journal line and in the replayed promptState.
14
+ //
15
+ // user the operator ended it (a rewind, a context clear)
16
+ // timeout the prompt's own timer ran out
17
+ // session_end the scope that owned it ended (turn, query, session abort
18
+ // or close, session deletion)
19
+ // daemon_restart the daemon is going away
20
+ // worker the worker or app-server process stopped waiting for it
21
+ // caller:<name> an outside caller ended it; <name> is the caller's own
22
+ // string (1-64 of A-Z a-z 0-9 . _ @ / + -), never interpreted
23
+ // unspecified the fallback for a cancel that named no valid source (it is
24
+ // logged as a warning); also what a replay reports for an
25
+ // ending recorded before cancels carried a source
12
26
  //
13
27
  // clientId is a per-connection label made here, not a credential; user is the
14
28
  // authenticated user's id, or null in single-user mode. Prompt content never
@@ -36,18 +50,38 @@ function fromPush(req, action) {
36
50
  return { kind: "push", action: action, user: userIdOf(req && req._clagenticUser) };
37
51
  }
38
52
 
39
- function fromTimeout() {
40
- return { kind: "timeout" };
53
+ var CANCEL = Object.freeze({
54
+ USER: "user",
55
+ TIMEOUT: "timeout",
56
+ SESSION_END: "session_end",
57
+ DAEMON_RESTART: "daemon_restart",
58
+ WORKER: "worker",
59
+ UNSPECIFIED: "unspecified",
60
+ });
61
+
62
+ var FIXED_CANCEL_SOURCES = Object.keys(CANCEL).map(function (k) { return CANCEL[k]; });
63
+ var CALLER_SOURCE = /^caller:[A-Za-z0-9._@\/+-]{1,64}$/;
64
+
65
+ function isCancelSource(value) {
66
+ return typeof value === "string" && (FIXED_CANCEL_SOURCES.indexOf(value) !== -1 || CALLER_SOURCE.test(value));
41
67
  }
42
68
 
43
- function fromCancel(reason) {
44
- return { kind: "cancel", reason: reason };
69
+ // The one rule for a cancel's source: a value from the vocabulary is kept; a
70
+ // missing or unknown one becomes "unspecified" and is warned about, so a
71
+ // settle always carries a source and never an undefined one.
72
+ function cancelSource(value, reason) {
73
+ if (isCancelSource(value)) return value;
74
+ console.warn("[prompt-source] cancel (reason=" + logField(reason) + ") named "
75
+ + (value === undefined || value === null ? "no source" : "an unknown source " + logField(value))
76
+ + "; recorded as " + CANCEL.UNSPECIFIED);
77
+ return CANCEL.UNSPECIFIED;
45
78
  }
46
79
 
47
80
  // The source as log fields, "source=ws client=c-1a2b user=u1". A cancel's
48
81
  // reason is its own field on the settle line, not repeated here.
49
82
  function describe(source) {
50
83
  if (!source) return "source=unspecified";
84
+ if (typeof source === "string") return "source=" + logField(source);
51
85
  var out = "source=" + logField(source.kind);
52
86
  if (source.clientId) out += " client=" + logField(source.clientId);
53
87
  if (source.action) out += " action=" + logField(source.action);
@@ -58,8 +92,9 @@ function describe(source) {
58
92
  module.exports = {
59
93
  fromWs: fromWs,
60
94
  fromPush: fromPush,
61
- fromTimeout: fromTimeout,
62
- fromCancel: fromCancel,
95
+ CANCEL: CANCEL,
96
+ isCancelSource: isCancelSource,
97
+ cancelSource: cancelSource,
63
98
  describe: describe,
64
99
  clientIdOf: clientIdOf,
65
100
  };
package/lib/public/app.js CHANGED
@@ -648,6 +648,7 @@ import { initDiagnostics } from './modules/diagnostics.js';
648
648
  // session_switched where the list must be preserved.
649
649
  resetSessionList();
650
650
  resetClientState();
651
+ _connCancelReconnect();
651
652
  connect();
652
653
  }
653
654
  });
@@ -259,6 +259,31 @@
259
259
  margin-left: 6px;
260
260
  }
261
261
 
262
+ /* The sessions a restart would end: a count line and an expand control above the
263
+ card's actions. Collapsed by default; expanded, the list scrolls in a bounded
264
+ region, so the card's height does not depend on the session count. */
265
+ .build-update-sessions {
266
+ display: flex;
267
+ flex-direction: column;
268
+ gap: 2px;
269
+ margin-top: 4px;
270
+ font-size: 11px;
271
+ color: var(--text-secondary);
272
+ }
273
+ .build-update-sessions-head {
274
+ display: flex;
275
+ align-items: center;
276
+ gap: 8px;
277
+ }
278
+ .build-update-sessions.is-collapsed .build-update-session-list { display: none; }
279
+ .build-update-session-list {
280
+ margin: 0;
281
+ padding-left: 16px;
282
+ max-height: 160px;
283
+ overflow-y: auto;
284
+ overflow-wrap: anywhere;
285
+ }
286
+
262
287
  /* Shown beside a restart button whose click could not be sent. */
263
288
  .build-update-send-failed {
264
289
  margin-top: 4px;
@@ -480,6 +505,9 @@
480
505
  .toast-diagnostic.visible {
481
506
  transform: translateY(0);
482
507
  }
508
+ .build-update-session-list {
509
+ max-height: 25vh;
510
+ }
483
511
  .toast-diagnostic-msg {
484
512
  /* Long unbreakable identifiers (e.g. "PostToolUse", absolute paths)
485
513
  must wrap instead of forcing the flex child's intrinsic width