@clagentic/console 1.12.0-beta.1 → 1.12.0-beta.3

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 (161) hide show
  1. package/LICENSES/README.md +5 -0
  2. package/README.md +1 -0
  3. package/bin/cli.js +5 -2
  4. package/lib/agent-roster-ws.js +84 -0
  5. package/lib/agent-roster.js +312 -0
  6. package/lib/auth-required-notify.js +42 -0
  7. package/lib/build-changes.js +8 -2
  8. package/lib/build-sha.json +2 -2
  9. package/lib/build-update-notify.js +45 -0
  10. package/lib/build-update-offer.js +34 -11
  11. package/lib/build-update-standing.js +34 -14
  12. package/lib/claude-settings-env.js +142 -0
  13. package/lib/cli/duration.js +27 -0
  14. package/lib/cli/schedule-input-subcommands.js +1 -22
  15. package/lib/cli/session-subcommands.js +60 -11
  16. package/lib/client-address.js +44 -0
  17. package/lib/daemon.js +31 -10
  18. package/lib/idle-compaction-setting.js +203 -0
  19. package/lib/mcp-bridge-credential.js +41 -0
  20. package/lib/notification-prefs.js +18 -22
  21. package/lib/notify/core.js +210 -0
  22. package/lib/notify/index.js +31 -0
  23. package/lib/notify/live-prompts.js +36 -0
  24. package/lib/notify/payload.js +62 -0
  25. package/lib/notify/target.js +5 -0
  26. package/lib/notify/type-registry.js +81 -0
  27. package/lib/notify/types.js +57 -0
  28. package/lib/open-sockets.js +27 -0
  29. package/lib/outbound-access.js +85 -0
  30. package/lib/outbound-entry.js +116 -0
  31. package/lib/outbound-ledger.js +306 -0
  32. package/lib/outbound-query.js +153 -0
  33. package/lib/pending-prompts.js +31 -16
  34. package/lib/project-connection.js +48 -8
  35. package/lib/project-filesystem.js +43 -5
  36. package/lib/project-http.js +9 -1
  37. package/lib/project-loop.js +38 -26
  38. package/lib/project-notifications.js +122 -39
  39. package/lib/project-prompt-responses.js +1 -1
  40. package/lib/project-sessions.js +11 -1
  41. package/lib/project-user-mention.js +22 -34
  42. package/lib/project-user-message.js +208 -4
  43. package/lib/project-user-shell.js +298 -0
  44. package/lib/project.js +41 -5
  45. package/lib/prompt-answer.js +145 -0
  46. package/lib/prompt-expiry.js +50 -0
  47. package/lib/prompt-registry.js +98 -25
  48. package/lib/prompt-reminders.js +38 -33
  49. package/lib/prompt-source.js +59 -8
  50. package/lib/public/app.js +32 -2
  51. package/lib/public/css/agent-messages.css +96 -0
  52. package/lib/public/css/agent-roster.css +93 -0
  53. package/lib/public/css/diagnostics.css +29 -1
  54. package/lib/public/css/home-hub.css +12 -0
  55. package/lib/public/css/icon-strip.css +9 -0
  56. package/lib/public/css/input.css +20 -141
  57. package/lib/public/css/login-panel.css +73 -0
  58. package/lib/public/css/menus.css +2 -2
  59. package/lib/public/css/mobile-nav.css +11 -0
  60. package/lib/public/css/sidebar.css +18 -0
  61. package/lib/public/css/team-panel.css +1 -6
  62. package/lib/public/css/team-view.css +101 -0
  63. package/lib/public/css/title-bar.css +10 -0
  64. package/lib/public/css/user-shell.css +144 -0
  65. package/lib/public/index.html +44 -7
  66. package/lib/public/modules/activity-state.js +20 -4
  67. package/lib/public/modules/agent-avatar-picker.js +162 -0
  68. package/lib/public/modules/agent-avatar.js +153 -0
  69. package/lib/public/modules/agent-messages.js +285 -0
  70. package/lib/public/modules/agent-roster.js +157 -0
  71. package/lib/public/modules/app-connection.js +34 -5
  72. package/lib/public/modules/app-messages.js +55 -16
  73. package/lib/public/modules/app-notifications.js +104 -75
  74. package/lib/public/modules/app-panels.js +26 -3
  75. package/lib/public/modules/app-projects.js +3 -1
  76. package/lib/public/modules/app-rate-limit.js +8 -165
  77. package/lib/public/modules/app-rendering.js +32 -7
  78. package/lib/public/modules/build-update-sessions.js +81 -0
  79. package/lib/public/modules/collapsible-text.js +33 -18
  80. package/lib/public/modules/compaction-state.js +34 -0
  81. package/lib/public/modules/custom-icons.js +23 -5
  82. package/lib/public/modules/diagnostics.js +61 -57
  83. package/lib/public/modules/env-shadow-notice.js +34 -0
  84. package/lib/public/modules/injected-identity.js +24 -21
  85. package/lib/public/modules/input.js +176 -15
  86. package/lib/public/modules/login-panel.js +136 -0
  87. package/lib/public/modules/login-parser.js +73 -0
  88. package/lib/public/modules/login-tab.js +66 -0
  89. package/lib/public/modules/notifications.js +9 -1
  90. package/lib/public/modules/notify-client.js +246 -0
  91. package/lib/public/modules/pending-term-request.js +30 -0
  92. package/lib/public/modules/project-icon.js +22 -2
  93. package/lib/public/modules/project-settings.js +5 -0
  94. package/lib/public/modules/prompt-state.js +3 -3
  95. package/lib/public/modules/prompts.js +1 -1
  96. package/lib/public/modules/queued-button.js +11 -0
  97. package/lib/public/modules/scheduled-input-actions.js +12 -4
  98. package/lib/public/modules/scheduled-outbox.js +230 -0
  99. package/lib/public/modules/send-now.js +200 -0
  100. package/lib/public/modules/server-settings.js +50 -1
  101. package/lib/public/modules/shell-card.js +76 -0
  102. package/lib/public/modules/shell-line.js +49 -0
  103. package/lib/public/modules/shell-queue.js +253 -0
  104. package/lib/public/modules/shell-settings-controls.js +76 -0
  105. package/lib/public/modules/sidebar-mobile.js +3 -3
  106. package/lib/public/modules/sidebar-sessions.js +8 -0
  107. package/lib/public/modules/sidebar-users.js +5 -7
  108. package/lib/public/modules/team-panel.js +30 -88
  109. package/lib/public/modules/team-view.js +211 -0
  110. package/lib/public/modules/terminal-links.js +193 -0
  111. package/lib/public/modules/terminal.js +84 -107
  112. package/lib/public/modules/tools.js +19 -0
  113. package/lib/public/modules/user-shell.js +824 -0
  114. package/lib/public/notify-sw-logic.js +252 -0
  115. package/lib/public/notify-target.js +157 -0
  116. package/lib/public/style.css +5 -0
  117. package/lib/public/sw.js +20 -69
  118. package/lib/push.js +63 -19
  119. package/lib/queued-chat.js +392 -0
  120. package/lib/runtime-env.js +1 -0
  121. package/lib/scheduled-input/ws.js +67 -9
  122. package/lib/sdk-bridge.js +184 -73
  123. package/lib/sdk-message-processor.js +124 -76
  124. package/lib/server-auth.js +6 -4
  125. package/lib/server-dm.js +11 -20
  126. package/lib/server-settings.js +39 -1
  127. package/lib/server.js +112 -16
  128. package/lib/session-directory.js +39 -12
  129. package/lib/session-end.js +97 -0
  130. package/lib/session-input-ipc.js +37 -1
  131. package/lib/session-input.js +263 -21
  132. package/lib/session-liveness.js +53 -0
  133. package/lib/session-reclaim.js +37 -2
  134. package/lib/session-resume.js +1 -0
  135. package/lib/session-settings-file.js +53 -0
  136. package/lib/sessions.js +80 -9
  137. package/lib/team-traffic-ws.js +68 -0
  138. package/lib/team-traffic.js +318 -0
  139. package/lib/usage-store.js +36 -7
  140. package/lib/user-shell-settings.js +105 -0
  141. package/lib/vendor-auth.js +33 -2
  142. package/lib/ws-schema.js +33 -5
  143. package/lib/yoke/adapters/claude-capabilities.js +2 -0
  144. package/lib/yoke/adapters/claude-vendor.js +17 -1
  145. package/lib/yoke/adapters/claude-worker.js +39 -11
  146. package/lib/yoke/adapters/claude.js +225 -6
  147. package/lib/yoke/adapters/codex-capabilities.js +6 -1
  148. package/lib/yoke/adapters/codex-user-shell.js +265 -0
  149. package/lib/yoke/adapters/codex-vendor.js +7 -0
  150. package/lib/yoke/adapters/codex.js +186 -52
  151. package/lib/yoke/auth-checks.js +17 -2
  152. package/lib/yoke/codex-app-server.js +8 -2
  153. package/lib/yoke/index.js +21 -3
  154. package/lib/yoke/interface.js +54 -1
  155. package/lib/yoke/mcp-bridge-server.js +12 -8
  156. package/lib/yoke/provider-credentials.js +24 -0
  157. package/lib/yoke/registry.js +40 -1
  158. package/lib/yoke/user-shell.js +224 -0
  159. package/package.json +2 -2
  160. package/scripts/check-test-count.js +14 -0
  161. package/lib/public/modules/build-notice-gate.js +0 -73
@@ -20,11 +20,17 @@ var sessionResume = require("./session-resume");
20
20
  var { logField, logText } = require("./log-field");
21
21
  var { validateFrom, validateFromSession, senderFields, forClient, stampRecord, newMessageId, isInjected, consoleTurnEntry } = require("./injected-sender");
22
22
  var senderRef = require("./sender-ref");
23
+ var { senderKey } = require("./agent-roster");
23
24
  var { findNameHolder, releaseReapedHolders } = require("./session-name-holder");
24
25
  var directory = require("./session-directory");
25
26
  var permissionSettings = require("./permission-settings");
27
+ var { checkSettingsFile } = require("./session-settings-file");
26
28
  var { checkPermissionMode } = require("./permission-mode-policy");
27
29
  var { slugifyAgentName } = agentsModule;
30
+ var outboundEntry = require("./outbound-entry");
31
+ var sessionEnd = require("./session-end");
32
+ var promptSource = require("./prompt-source");
33
+ var promptAnswer = require("./prompt-answer");
28
34
 
29
35
  // The result vocabulary reported to the caller. "error" is not a verdict on
30
36
  // the request: delivery was attempted and failed, so a caller may retry.
@@ -42,6 +48,8 @@ var RESULT = Object.freeze({
42
48
  REFUSED: "refused",
43
49
  ERROR: "error",
44
50
  BUSY: "busy",
51
+ ENDED: "ended",
52
+ ALREADY_ENDED: "already-ended",
45
53
  });
46
54
 
47
55
  // Initiators injected input may name. HUMAN is deliberately absent.
@@ -162,14 +170,23 @@ function logResult(verb, req, out) {
162
170
  console.log(line);
163
171
  }
164
172
 
165
- function logged(verb, fn) {
173
+ // after(verb, req, result), when given, runs once the line is logged. It must
174
+ // not throw into the caller: the result is already decided.
175
+ function logged(verb, fn, after) {
176
+ function finish(req, result) {
177
+ logResult(verb, req, result);
178
+ if (after) {
179
+ try { after(verb, req, result); }
180
+ catch (e) { console.error("[session-input] after-result hook failed:", messageOf(e)); }
181
+ }
182
+ return result;
183
+ }
166
184
  return function (req) {
167
185
  var out = fn(req);
168
186
  if (out && typeof out.then === "function") {
169
- return out.then(function (result) { logResult(verb, req, result); return result; });
187
+ return out.then(function (result) { return finish(req, result); });
170
188
  }
171
- logResult(verb, req, out);
172
- return out;
189
+ return finish(req, out);
173
190
  };
174
191
  }
175
192
 
@@ -183,9 +200,21 @@ function present(value) {
183
200
  // agentCatalog(project) -> [{ name }] named agents on offer (default: the picker's catalog)
184
201
  // getPermissionSettings() -> the server's permission settings (lib/permission-settings.js)
185
202
  // or null (default: the settings' defaults)
203
+ // outboundLedger -> { record(entry) } the sender-side ledger
204
+ // (lib/outbound-ledger.js); default: none kept
205
+ // now() -> ms clock for ledger entries (default Date.now)
206
+ // onAutomationInjected -> called after a console-composed turn (a scheduled
207
+ // firing) is recorded on its target; such a turn has
208
+ // no ledger entry, so a live view hears of it here
209
+ // onAgentSeen(sighting) -> told of each injected sender that names an agent
210
+ // (the agent roster); read-only, its result is unused
186
211
  function createSessionInput(deps) {
187
212
  var getProject = deps.getProject;
188
213
  var listProjects = deps.listProjects || function () { return []; };
214
+ var outboundLedger = deps.outboundLedger || null;
215
+ var onAutomationInjected = deps.onAutomationInjected || null;
216
+ var onAgentSeen = deps.onAgentSeen || null;
217
+ var clock = deps.now || Date.now;
189
218
 
190
219
  function defaultAgentCatalog(project) {
191
220
  // Neither lookup throws: both answer [] when discovery has not run or the
@@ -223,8 +252,9 @@ function createSessionInput(deps) {
223
252
  }
224
253
 
225
254
  // Resolves --to to a console session. Returns { projectSlug, sessionId, via }
226
- // or { refusal } (not-found, ambiguous or refused).
227
- function resolveAddressTarget(req) {
255
+ // or { refusal } (not-found, ambiguous or refused). holdsRequest, when given,
256
+ // names the one session by the prompt request it holds (answer-prompt).
257
+ function resolveAddressTarget(req, holdsRequest) {
228
258
  var parsed = directory.parseAddress(req.to);
229
259
  if (parsed.error) return { refusal: refused(parsed.error) };
230
260
  if (req.projectSlug && req.projectSlug !== parsed.project) {
@@ -232,9 +262,12 @@ function createSessionInput(deps) {
232
262
  }
233
263
  var resolved = resolveProject(parsed.project);
234
264
  if (resolved.refusal) return { refusal: resolved.refusal };
235
- var found = directory.resolve(entriesOf(parsed.project, resolved.project), parsed);
265
+ var found = directory.resolve(entriesOf(parsed.project, resolved.project), parsed, holdsRequest);
236
266
  if (found.none) {
237
- return { refusal: { ok: false, result: RESULT.NOT_FOUND, reason: "no session matches the address " + shown(req.to), to: req.to } };
267
+ var reason = holdsRequest === undefined
268
+ ? "no session matches the address " + shown(req.to)
269
+ : "no session matching the address " + shown(req.to) + " holds the request " + shown(holdsRequest);
270
+ return { refusal: { ok: false, result: RESULT.NOT_FOUND, reason: reason, to: req.to } };
238
271
  }
239
272
  if (found.candidates) {
240
273
  return {
@@ -325,18 +358,86 @@ function createSessionInput(deps) {
325
358
  // Exactly one way of naming the target: sessionId (with projectSlug), to, or
326
359
  // reply. Returns { projectSlug, sessionId, via } for to and reply, {} to leave
327
360
  // the request as it is, or { refusal }.
328
- function resolveSendTarget(req) {
361
+ function resolveSendTarget(req, holdsRequest) {
329
362
  var named = ["sessionId", "to", "reply"].filter(function (k) { return present(req[k]); });
330
363
  if (named.length > 1) return { refusal: refused("name the target one way only: sessionId, to or reply (given: " + named.join(", ") + ")") };
331
- if (present(req.to)) return resolveAddressTarget(req);
364
+ if (present(req.to)) return resolveAddressTarget(req, holdsRequest);
332
365
  if (present(req.reply)) return resolveReplyTarget(req);
333
366
  return {};
334
367
  }
335
368
 
369
+ // The sender fields of a request. The reference a console-started caller holds
370
+ // is resolved in the daemon's own store; one it does not hold falls through to
371
+ // declared or undeclared.
372
+ function senderOf(req, initiator) {
373
+ var extracted = senderRef.resolve(getProject, { project: req.senderProject, sessionId: req.senderSessionId });
374
+ return senderFields(initiator, validateFrom(req.from).from, extracted, validateFromSession(req.fromSession).fromSession);
375
+ }
376
+
377
+ // Appends to the sender-side ledger. A ledger that cannot be written is
378
+ // logged by the ledger and never changes the answer to the request.
379
+ function writeLedger(entry) {
380
+ if (!outboundLedger) return;
381
+ try { outboundLedger.record(entry); }
382
+ catch (e) { console.error("[session-input] outbound ledger failed:", messageOf(e)); }
383
+ }
384
+
385
+ // The verb a result is logged under, as the ledger names it. Other verbs
386
+ // (listing, a scheduled firing) are not outbound calls of a sender.
387
+ var LEDGER_VERB = {
388
+ open_session: outboundEntry.VERB.OPEN_SESSION,
389
+ send_session_input: outboundEntry.VERB.SEND_INPUT,
390
+ end_session: outboundEntry.VERB.END_SESSION,
391
+ answer_prompt: outboundEntry.VERB.ANSWER_PROMPT,
392
+ };
393
+
394
+ // A delivery, recorded where the history entry is, so both carry the same
395
+ // message id and sender.
396
+ function ledgerDelivery(verb, project, session, req, initiator, messageId, sender) {
397
+ if (!outboundLedger) return;
398
+ // Building the entry reads the session and the project; none of it may
399
+ // throw into the delivery.
400
+ try {
401
+ var linuxUser = typeof project.getLinuxUserForSession === "function" ? project.getLinuxUserForSession(session) : null;
402
+ writeLedger(outboundEntry.buildEntry({
403
+ verb: verb,
404
+ ts: new Date(clock()).toISOString(),
405
+ req: req,
406
+ out: null,
407
+ initiator: initiator,
408
+ messageId: messageId,
409
+ sender: sender,
410
+ target: Object.assign({ project: req.projectSlug, runsAs: linuxUser || outboundEntry.RUNS_AS_DAEMON }, describe(project, session)),
411
+ }));
412
+ } catch (e) {
413
+ console.error("[session-input] outbound ledger entry failed:", messageOf(e));
414
+ }
415
+ }
416
+
417
+ // A request that was not delivered (refused, not found, ambiguous, failed).
418
+ // A delivery already left its entry in recordInjected.
419
+ function ledgerOutcome(verb, req, out) {
420
+ if (!outboundLedger || !LEDGER_VERB[verb] || !out) return;
421
+ // A delivery already left its entry; an end and an answer leave theirs here.
422
+ if (out.ok === true && verb !== "end_session" && verb !== "answer_prompt") return;
423
+ req = req || {};
424
+ var init = resolveInitiator(req.initiator);
425
+ var initiator = init.initiator || null;
426
+ writeLedger(outboundEntry.buildEntry({
427
+ verb: LEDGER_VERB[verb],
428
+ ts: new Date(clock()).toISOString(),
429
+ req: req,
430
+ out: out,
431
+ initiator: initiator,
432
+ sender: senderOf(req, initiator),
433
+ target: { project: req.projectSlug, consoleSessionId: out.consoleSessionId, sessionId: out.sessionId, agent: out.agent, vendor: out.vendor, name: out.name },
434
+ }));
435
+ }
436
+
336
437
  // History entry that marks the turn as injected, not typed. The input is
337
438
  // already delivered when this runs, so a failure to record is logged and does
338
439
  // not turn a delivered turn into an error a caller would retry.
339
- function recordInjected(project, session, req, initiator, messageId) {
440
+ function recordInjected(verb, project, session, req, initiator, messageId) {
340
441
  var entry;
341
442
  if (req.automation) {
342
443
  // A turn the console composed itself (fireScheduled): the console is the
@@ -353,11 +454,13 @@ function createSessionInput(deps) {
353
454
  });
354
455
  if (req.triggerId) entry.triggerId = req.triggerId;
355
456
  if (req.contextNote) entry.contextNote = req.contextNote;
356
- // The reference a console-started caller holds, resolved in the daemon's own
357
- // store; one it does not hold falls through to declared or undeclared.
358
- var extracted = senderRef.resolve(getProject, { project: req.senderProject, sessionId: req.senderSessionId });
359
- var sender = senderFields(initiator, validateFrom(req.from).from, extracted, validateFromSession(req.fromSession).fromSession);
457
+ var sender = senderOf(req, initiator);
360
458
  for (var k in sender) entry[k] = sender[k];
459
+ ledgerDelivery(verb, project, session, req, initiator, messageId, sender);
460
+ var senderName = senderKey(sender);
461
+ if (onAgentSeen && senderName) {
462
+ onAgentSeen({ key: senderName, source: "sender", project: req.projectSlug, sessionId: String(session.localId) });
463
+ }
361
464
  }
362
465
  try {
363
466
  project.sm.recordHistoryEntry(session, entry, true);
@@ -369,6 +472,10 @@ function createSessionInput(deps) {
369
472
  } catch (e) {
370
473
  console.error("[session-input] failed to record injected message:", e.message || e);
371
474
  }
475
+ if (req.automation && onAutomationInjected) {
476
+ try { onAutomationInjected(); }
477
+ catch (e) { console.error("[session-input] automation hook failed:", messageOf(e)); }
478
+ }
372
479
  }
373
480
 
374
481
  // The OS user a turn starts as. A console automation also grants that user
@@ -420,7 +527,7 @@ function createSessionInput(deps) {
420
527
  var messageId = newMessageId();
421
528
 
422
529
  function delivered() {
423
- recordInjected(project, session, req, initiator, messageId);
530
+ recordInjected(outboundEntry.VERB.SEND_INPUT, project, session, req, initiator, messageId);
424
531
  if (req.automation && !processing) announceStartedTurn(project, session);
425
532
  var out = describe(project, session);
426
533
  out.ok = true;
@@ -538,6 +645,129 @@ function createSessionInput(deps) {
538
645
  });
539
646
  }
540
647
 
648
+ // The target of a verb that acts on one existing session (end-session,
649
+ // answer-prompt), named by id with its project or by --to, resolved as
650
+ // send-input resolves it. Fills req.projectSlug and req.sessionId in (req is
651
+ // the caller's own copy). holdsRequest (answer-prompt) makes --to pick the
652
+ // address match holding that request id instead of by tier. Returns
653
+ // { project } or { refusal }.
654
+ function resolveSessionTarget(req, holdsRequest) {
655
+ var named = ["sessionId", "to"].filter(function (k) { return present(req[k]); });
656
+ if (named.length > 1) return { refusal: refused("name the target one way only: sessionId or to (given: " + named.join(", ") + ")") };
657
+ var target = resolveSendTarget(Object.assign({}, req, { reply: undefined }), holdsRequest);
658
+ if (target.refusal) return { refusal: target.refusal };
659
+ if (target.via) {
660
+ req.projectSlug = target.projectSlug;
661
+ req.sessionId = target.sessionId;
662
+ }
663
+ if (typeof req.sessionId !== "string" || !req.sessionId) {
664
+ return { refusal: refused("sessionId must be a non-empty string") };
665
+ }
666
+ if (!sessionResume.isSafeSessionId(req.sessionId)) {
667
+ return { refusal: refused("unsafe session id " + shown(req.sessionId) + " refused") };
668
+ }
669
+ var resolved = resolveProject(req.projectSlug);
670
+ if (resolved.refusal) return { refusal: resolved.refusal };
671
+ return { project: resolved.project };
672
+ }
673
+
674
+ // req: { projectSlug, sessionId | to, from?, fromSession?, reason? }
675
+ // Stands one live session down: its prompts settle as cancelled (reason
676
+ // ended), its turn is aborted and its query closed (lib/session-end.js).
677
+ // History is kept and the session resumes by id. Addressed as send-input
678
+ // addresses a session; a session that is not live, or only in the store, is
679
+ // already-ended, and nothing is resumed to be ended. Returns the result, or a
680
+ // Promise of it when the store must be asked.
681
+ function endSession(req) {
682
+ req = Object.assign({}, req);
683
+ var fromRefusal = checkFrom(req.from) || checkFromSession(req.fromSession);
684
+ if (fromRefusal) return fromRefusal;
685
+ var by = sessionEnd.sourceFor(validateFrom(req.from).from);
686
+ if (by.error) return refused(by.error);
687
+ var located = resolveSessionTarget(req);
688
+ if (located.refusal) return located.refusal;
689
+ var project = located.project;
690
+
691
+ function answer(result, session) {
692
+ var out = session ? describe(project, session) : {};
693
+ out.ok = result === RESULT.ENDED || result === RESULT.ALREADY_ENDED;
694
+ out.result = result;
695
+ out.endedBy = by.source;
696
+ return out;
697
+ }
698
+
699
+ var live = findSession(project.sm, req.sessionId);
700
+ if (live) {
701
+ if (!senderRef.isLive(live)) return answer(RESULT.ALREADY_ENDED, live);
702
+ var ended = sessionEnd.endLiveSession({
703
+ session: live,
704
+ sm: project.sm,
705
+ send: project.send,
706
+ slug: req.projectSlug,
707
+ source: by.source,
708
+ reason: req.reason,
709
+ });
710
+ var out = answer(RESULT.ENDED, live);
711
+ out.agents = ended.agents;
712
+ out.failures = ended.failures;
713
+ if (ended.failures.length) out.ok = false;
714
+ return out;
715
+ }
716
+ // Only the console's own store is asked, and a stored session is never
717
+ // brought back just to be ended.
718
+ return sessionResume.sessionExistsInStore(project.sm, req.sessionId).then(function (exists) {
719
+ if (!exists) return { ok: false, result: RESULT.NOT_FOUND, reason: "no session " + shown(req.sessionId) + " in project " + shown(req.projectSlug) };
720
+ var stored = answer(RESULT.ALREADY_ENDED, null);
721
+ stored.sessionId = req.sessionId;
722
+ return stored;
723
+ }).catch(function (err) {
724
+ console.error("[session-input] end failed:", messageOf(err));
725
+ return { ok: false, result: RESULT.ERROR, retryable: !!(err && err.retryable), reason: "end failed: " + messageOf(err) };
726
+ });
727
+ }
728
+
729
+ // req: { projectSlug, sessionId | to, requestId, decision, from?, fromSession? }
730
+ // Answers one pending prompt of a live session through the prompt registry
731
+ // (lib/prompt-answer.js), as a browser answer does. Addressed as end-session
732
+ // addresses a session, under the same trust (the daemon's OS user); the
733
+ // answer is recorded as caller:<from>, or user without --from. A session with
734
+ // no runtime holds no pending prompt, so a stored one is not-found and is
735
+ // never resumed. Returns the result, or a Promise of it when the store must
736
+ // be asked.
737
+ function answerPrompt(req) {
738
+ req = Object.assign({}, req);
739
+ var fromRefusal = checkFrom(req.from) || checkFromSession(req.fromSession);
740
+ if (fromRefusal) return fromRefusal;
741
+ var by = promptSource.callerSource(validateFrom(req.from).from, "an answer");
742
+ if (by.error) return refused(by.error);
743
+ if (typeof req.requestId !== "string" || !req.requestId) return refused("requestId must be a non-empty string");
744
+ if (typeof req.decision !== "string" || !req.decision) return refused("decision must be a non-empty string");
745
+ var located = resolveSessionTarget(req, req.requestId);
746
+ if (located.refusal) return located.refusal;
747
+ var project = located.project;
748
+
749
+ function answered(session) {
750
+ var out = describe(project, session);
751
+ return Object.assign(out, promptAnswer.answerPrompt({
752
+ sm: project.sm,
753
+ session: session,
754
+ requestId: req.requestId,
755
+ decision: req.decision,
756
+ source: by.source,
757
+ }));
758
+ }
759
+
760
+ var live = findSession(project.sm, req.sessionId);
761
+ if (live) return answered(live);
762
+ return sessionResume.sessionExistsInStore(project.sm, req.sessionId).then(function (exists) {
763
+ if (!exists) return { ok: false, result: RESULT.NOT_FOUND, reason: "no session " + shown(req.sessionId) + " in project " + shown(req.projectSlug), requestId: req.requestId, kind: null, source: by.source };
764
+ return { ok: false, result: RESULT.NOT_FOUND, reason: "session " + shown(req.sessionId) + " has no runtime, so no pending request", requestId: req.requestId, kind: null, source: by.source, sessionId: req.sessionId };
765
+ }).catch(function (err) {
766
+ console.error("[session-input] answer failed:", messageOf(err));
767
+ return { ok: false, result: RESULT.ERROR, retryable: !!(err && err.retryable), reason: "answer failed: " + messageOf(err) };
768
+ });
769
+ }
770
+
541
771
  // The scheduled-input firing (lib/scheduled-input/service.js): one scheduled
542
772
  // input delivered to the session its record names, through deliver(), the path
543
773
  // send-input uses. Not an IPC verb and not reachable from a request body:
@@ -603,6 +833,15 @@ function createSessionInput(deps) {
603
833
  }
604
834
  options.sessionName = named.name;
605
835
  }
836
+ if (present(req.settingsFile)) {
837
+ var checked = checkSettingsFile(req.settingsFile);
838
+ if (checked.error) return { refusal: refused(checked.error) };
839
+ // A bare test double without the vendor check is let through, as for the mode.
840
+ if (typeof project.supportsSettingsFileFor === "function" && !project.supportsSettingsFileFor({ vendor: options.vendor || null })) {
841
+ return { refusal: refused("this vendor does not take a settings file (--settings)") };
842
+ }
843
+ options.settingsFile = checked.path;
844
+ }
606
845
  return { options: options };
607
846
  }
608
847
 
@@ -655,7 +894,7 @@ function createSessionInput(deps) {
655
894
  }
656
895
 
657
896
  // req: { projectSlug, prompt, vendor?, agentName?, sessionVisibility?,
658
- // permissionMode?, initiator?, contextNote?, cwd?, triggerId? }
897
+ // permissionMode?, settingsFile?, initiator?, contextNote?, cwd?, triggerId? }
659
898
  // Returns the result, or a Promise of it when the first turn starts
660
899
  // asynchronously (see settle()).
661
900
  function openSession(req) {
@@ -707,7 +946,8 @@ function createSessionInput(deps) {
707
946
  // that session's owner, so its prompts push to a person who can read it. A
708
947
  // declared or undeclared sender is a claim, never an inheritance.
709
948
  var openedBy = senderRef.resolve(getProject, { project: req.senderProject, sessionId: req.senderSessionId });
710
- var created = Object.assign({}, kind.options, { permissionMode: openMode.mode });
949
+ // activate:false: an opened session is listed but never moves a connected tab.
950
+ var created = Object.assign({}, kind.options, { permissionMode: openMode.mode, activate: false });
711
951
  if (openedBy && openedBy.ownerId) created.ownerId = openedBy.ownerId;
712
952
  var sess = sm.createSession(created);
713
953
  var label = req.triggerId ? " — " + String(req.triggerId).substring(0, 12) : "";
@@ -732,7 +972,7 @@ function createSessionInput(deps) {
732
972
  }, function () {
733
973
  // Recorded once, only now that the query has started: a rejected start
734
974
  // leaves no history entry for a prompt that never ran.
735
- recordInjected(project, sess, req, init.initiator, openedMessageId);
975
+ recordInjected(outboundEntry.VERB.OPEN_SESSION, project, sess, req, init.initiator, openedMessageId);
736
976
  // A name has one holder: the claim took effect in memory at open (the new
737
977
  // session is non-reaped and wins over its reaped twins meanwhile), and
738
978
  // only now, with the start done, is it taken from the reaped sessions
@@ -764,8 +1004,10 @@ function createSessionInput(deps) {
764
1004
  }
765
1005
 
766
1006
  return {
767
- openSession: logged("open_session", neverThrows(openSession)),
768
- sendInput: logged("send_session_input", neverThrows(sendInput)),
1007
+ openSession: logged("open_session", neverThrows(openSession), ledgerOutcome),
1008
+ sendInput: logged("send_session_input", neverThrows(sendInput), ledgerOutcome),
1009
+ endSession: logged("end_session", neverThrows(endSession), ledgerOutcome),
1010
+ answerPrompt: logged("answer_prompt", neverThrows(answerPrompt), ledgerOutcome),
769
1011
  listSessions: logged("list_sessions", neverThrows(listSessions)),
770
1012
  fireScheduled: logged("fire_scheduled_input", neverThrows(fireScheduled)),
771
1013
  };
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+
3
+ // session-liveness.js - the one daemon-side derivation of what a session is
4
+ // doing, read by every surface that shows it: the build-update confirm card,
5
+ // the session list served to the client (picker dots, sidebar), and
6
+ // list-sessions.
7
+ //
8
+ // Inputs are the three facts a session holds: whether a runtime is attached,
9
+ // whether a turn is running, and whether an operator prompt is pending.
10
+ //
11
+ // reaped no runtime (idle reaper, daemon restart); input resumes it
12
+ // waiting a runtime is attached and at least one operator prompt is
13
+ // pending: a turn or runtime is blocked on the operator
14
+ // running a runtime is attached, a turn is running, nothing is pending
15
+ // idle a runtime is attached, nothing running, nothing pending
16
+ //
17
+ // A pending prompt outranks a running turn: a turn stopped on a prompt is
18
+ // not progressing, and "running" would hide that the operator is the one
19
+ // being waited on. A prompt on a session with no runtime is not waited on by
20
+ // anything (nothing can use the answer), so that session is reaped; its
21
+ // prompt stays visible in list-sessions pendingPrompts.
22
+ //
23
+ // list-sessions keeps its three-valued `state` (live | idle | reaped); a
24
+ // waiting session is live there. liveStateOf() is that projection.
25
+
26
+ var senderRef = require("./sender-ref");
27
+ var { countPending } = require("./pending-prompts");
28
+
29
+ var ACTIVITY = Object.freeze({ REAPED: "reaped", IDLE: "idle", RUNNING: "running", WAITING: "waiting" });
30
+
31
+ // facts: { attached, turnRunning, pendingCount }
32
+ function derive(facts) {
33
+ if (!facts.attached) return ACTIVITY.REAPED;
34
+ if (facts.pendingCount > 0) return ACTIVITY.WAITING;
35
+ return facts.turnRunning ? ACTIVITY.RUNNING : ACTIVITY.IDLE;
36
+ }
37
+
38
+ // The activity of a session object held by a project's session manager.
39
+ function activityOf(session) {
40
+ return derive({
41
+ attached: senderRef.isLive(session),
42
+ turnRunning: !!session.isProcessing,
43
+ pendingCount: countPending(session),
44
+ });
45
+ }
46
+
47
+ // list-sessions' `state`: waiting and running are both "live".
48
+ function liveStateOf(activity) {
49
+ if (activity === ACTIVITY.REAPED) return "reaped";
50
+ return activity === ACTIVITY.IDLE ? "idle" : "live";
51
+ }
52
+
53
+ module.exports = { ACTIVITY: ACTIVITY, derive: derive, activityOf: activityOf, liveStateOf: liveStateOf };
@@ -7,6 +7,8 @@
7
7
  // announceLiveAgentReclaim: the agents are named, the session is marked as
8
8
  // expected to resume, and both UI surfaces are told.
9
9
 
10
+ var sessionActivity = require("./session-activity");
11
+
10
12
  var LABEL_MAX = 80;
11
13
  var FALLBACK_LABEL = "background agent";
12
14
 
@@ -42,9 +44,16 @@ function noteTaskLabel(session, taskId, input) {
42
44
  * answer instead of guessing at the end event.
43
45
  */
44
46
  function pruneTaskLabels(session) {
45
- var table = session && session.taskLabels;
46
- if (!table) return;
47
+ if (!session) return;
47
48
  var tracked = session.activeTaskToolIds || {};
49
+ var marks = session.backgroundTaskIds;
50
+ if (marks) {
51
+ Object.keys(marks).forEach(function (id) {
52
+ if (!Object.prototype.hasOwnProperty.call(tracked, id) || !tracked[id]) delete marks[id];
53
+ });
54
+ }
55
+ var table = session.taskLabels;
56
+ if (!table) return;
48
57
  Object.keys(table).forEach(function (id) {
49
58
  if (!Object.prototype.hasOwnProperty.call(tracked, id) || !tracked[id]) delete table[id];
50
59
  });
@@ -68,6 +77,30 @@ function liveAgentTasks(session) {
68
77
  });
69
78
  }
70
79
 
80
+ /** Record that a tracked Task was acknowledged as launched in the background. */
81
+ function noteBackgroundLaunch(session, taskId) {
82
+ if (typeof taskId !== "string" || !taskId) return;
83
+ if (!session.backgroundTaskIds) session.backgroundTaskIds = Object.create(null);
84
+ session.backgroundTaskIds[taskId] = true;
85
+ }
86
+
87
+ /**
88
+ * True when the session is busy only because tracked background agents hold
89
+ * activity tokens: no tool call, thinking block or other live work of its own.
90
+ * The idle reaper reads this so a session waiting on silent agents stays
91
+ * reapable (and announced) instead of looking like a running turn.
92
+ */
93
+ function onlyBackgroundAgentsActive(session) {
94
+ var tracked = session && session.activeTaskToolIds;
95
+ var marks = session && session.backgroundTaskIds;
96
+ if (!tracked || !marks) return false;
97
+ var has = Object.prototype.hasOwnProperty;
98
+ var sources = sessionActivity.listActiveSources(session);
99
+ return sources.length > 0 && sources.every(function (s) {
100
+ return has.call(tracked, s.token) && !!tracked[s.token] && has.call(marks, s.token);
101
+ });
102
+ }
103
+
71
104
  /** Operator-facing names of every Task the session still tracks. */
72
105
  function liveAgentLabels(session) {
73
106
  return liveAgentTasks(session).map(function (t) { return t.label; });
@@ -276,6 +309,8 @@ module.exports = {
276
309
  surfaceRestartReclaims: surfaceRestartReclaims,
277
310
  restartDiagnostics: restartDiagnostics,
278
311
  noteTaskLabel: noteTaskLabel,
312
+ noteBackgroundLaunch: noteBackgroundLaunch,
313
+ onlyBackgroundAgentsActive: onlyBackgroundAgentsActive,
279
314
  pruneTaskLabels: pruneTaskLabels,
280
315
  liveAgentLabels: liveAgentLabels,
281
316
  liveAgentTasks: liveAgentTasks,
@@ -129,6 +129,7 @@ function resumeStoredSession(deps, cliSessionId, opts) {
129
129
  if (meta.sessionVisibility) resumeOpts.sessionVisibility = meta.sessionVisibility;
130
130
  // The mode the session had; the session manager checks it before use.
131
131
  if (typeof meta.permissionMode === "string" && meta.permissionMode) resumeOpts.permissionMode = meta.permissionMode;
132
+ if (typeof meta.settingsFile === "string" && meta.settingsFile) resumeOpts.settingsFile = meta.settingsFile;
132
133
  // Sanitized so a crafted meta record cannot auto-approve a tool.
133
134
  if (meta.allowedTools) resumeOpts.allowedTools = utils.sanitizeAllowedTools(meta.allowedTools);
134
135
  }
@@ -0,0 +1,53 @@
1
+ // A session's additive settings file: a user-supplied path handed to the
2
+ // vendor runtime as an extra settings source. The console checks the path and,
3
+ // when it must also pass a settings object of its own, combines the two; it
4
+ // does not interpret the file's content otherwise.
5
+
6
+ var fs = require("fs");
7
+ var path = require("path");
8
+
9
+ // Checks a path at open time and at every launch. Returns { path } or { error }.
10
+ // Readable is checked for the daemon's identity; a session run as another OS
11
+ // user is checked again by that user's runtime, which fails the turn if it
12
+ // cannot read the file.
13
+ function checkSettingsFile(file) {
14
+ if (typeof file !== "string" || !file) return { error: "settings file must be a path" };
15
+ var shown = JSON.stringify(file.slice(0, 200));
16
+ if (!path.isAbsolute(file)) return { error: "settings file " + shown + " is not an absolute path" };
17
+ var stat;
18
+ try {
19
+ stat = fs.statSync(file);
20
+ } catch (e) {
21
+ return { error: "settings file " + shown + " does not exist or cannot be read" };
22
+ }
23
+ if (!stat.isFile()) return { error: "settings file " + shown + " is not a regular file" };
24
+ try {
25
+ fs.accessSync(file, fs.constants.R_OK);
26
+ } catch (e) {
27
+ return { error: "settings file " + shown + " is not readable" };
28
+ }
29
+ return { path: file };
30
+ }
31
+
32
+ // The value for the runtime's single `settings` option. The option takes a
33
+ // path or one object, not both, so with no console-supplied object the path is
34
+ // passed as is; with one, the file is read and the two are shallow-merged,
35
+ // the console's keys winning (they are the console's own switches). Returns
36
+ // { settings } or { error } (an unreadable or non-object file fails closed).
37
+ function composeSettings(file, consoleSettings) {
38
+ var checked = checkSettingsFile(file);
39
+ if (checked.error) return checked;
40
+ if (!consoleSettings || Object.keys(consoleSettings).length === 0) return { settings: file };
41
+ var parsed;
42
+ try {
43
+ parsed = JSON.parse(fs.readFileSync(file, "utf8"));
44
+ } catch (e) {
45
+ return { error: "settings file " + JSON.stringify(file.slice(0, 200)) + " is not valid JSON" };
46
+ }
47
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
48
+ return { error: "settings file " + JSON.stringify(file.slice(0, 200)) + " is not a JSON object" };
49
+ }
50
+ return { settings: Object.assign({}, parsed, consoleSettings) };
51
+ }
52
+
53
+ module.exports = { checkSettingsFile: checkSettingsFile, composeSettings: composeSettings };