@clagentic/console 1.10.0 → 1.11.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 (97) hide show
  1. package/README.md +14 -1
  2. package/bin/cli.js +38 -16
  3. package/lib/agents.js +1 -55
  4. package/lib/bash-auto-approve.js +133 -0
  5. package/lib/build-identity.js +23 -5
  6. package/lib/build-sha.json +2 -2
  7. package/lib/build-update-check.js +0 -62
  8. package/lib/build-update-offer.js +23 -13
  9. package/lib/build-update-restart.js +59 -43
  10. package/lib/cli/arg-validation.js +50 -0
  11. package/lib/cli/daemon-launch.js +5 -4
  12. package/lib/cli/menus.js +2 -2
  13. package/lib/cli/schedule-input-subcommands.js +327 -0
  14. package/lib/cli/session-subcommands.js +101 -17
  15. package/lib/client-build-id.js +56 -0
  16. package/lib/daemon.js +38 -6
  17. package/lib/identity/index.js +9 -4
  18. package/lib/injected-sender.js +225 -0
  19. package/lib/message-sender-record.js +133 -0
  20. package/lib/pending-prompts.js +55 -0
  21. package/lib/permission-mode-policy.js +63 -0
  22. package/lib/permission-settings.js +96 -0
  23. package/lib/permission-summary.js +67 -0
  24. package/lib/project-connection.js +10 -11
  25. package/lib/project-external-trigger.js +9 -1
  26. package/lib/project-loop.js +50 -23
  27. package/lib/project-permission-settings.js +59 -0
  28. package/lib/project-prompt-responses.js +25 -15
  29. package/lib/project-sessions.js +164 -60
  30. package/lib/project-user-message.js +18 -47
  31. package/lib/project.js +46 -74
  32. package/lib/public/app.js +4 -7
  33. package/lib/public/css/channel-layout.css +20 -0
  34. package/lib/public/css/diagnostics.css +7 -0
  35. package/lib/public/css/input.css +78 -0
  36. package/lib/public/css/menus.css +17 -0
  37. package/lib/public/css/overlays.css +0 -3
  38. package/lib/public/css/scheduler-modal.css +5 -0
  39. package/lib/public/index.html +44 -8
  40. package/lib/public/modules/app-connection.js +18 -1
  41. package/lib/public/modules/app-loop-ui.js +11 -1
  42. package/lib/public/modules/app-messages.js +23 -30
  43. package/lib/public/modules/app-panels.js +10 -3
  44. package/lib/public/modules/app-rate-limit.js +64 -88
  45. package/lib/public/modules/app-rendering.js +37 -6
  46. package/lib/public/modules/approve-tools-toggle.js +33 -0
  47. package/lib/public/modules/at-agents.js +8 -0
  48. package/lib/public/modules/build-guard.js +61 -0
  49. package/lib/public/modules/build-update-detail.js +1 -4
  50. package/lib/public/modules/diagnostics.js +98 -9
  51. package/lib/public/modules/injected-identity.js +78 -0
  52. package/lib/public/modules/injected-label.js +62 -0
  53. package/lib/public/modules/input.js +64 -114
  54. package/lib/public/modules/loop-hooks-warning.js +20 -0
  55. package/lib/public/modules/mode-options.js +35 -16
  56. package/lib/public/modules/permission-settings.js +56 -0
  57. package/lib/public/modules/project-settings.js +3 -0
  58. package/lib/public/modules/rate-limit-state.js +47 -22
  59. package/lib/public/modules/schedule-panel.js +234 -0
  60. package/lib/public/modules/schedule-time.js +193 -0
  61. package/lib/public/modules/scheduled-input-actions.js +19 -0
  62. package/lib/public/modules/scheduler-config.js +12 -0
  63. package/lib/public/modules/scheduler.js +8 -0
  64. package/lib/public/modules/server-settings.js +5 -9
  65. package/lib/public/modules/settings-defaults.js +18 -4
  66. package/lib/runtime-env.js +4 -0
  67. package/lib/scheduled-input/ipc.js +92 -0
  68. package/lib/scheduled-input/rate-limit-resume.js +53 -0
  69. package/lib/scheduled-input/records.js +350 -0
  70. package/lib/scheduled-input/service.js +743 -0
  71. package/lib/scheduled-input/store.js +132 -0
  72. package/lib/scheduled-input/ws.js +194 -0
  73. package/lib/scheduler.js +4 -0
  74. package/lib/sdk-bridge.js +194 -175
  75. package/lib/sdk-message-processor.js +13 -5
  76. package/lib/sdk-skill-discovery.js +17 -0
  77. package/lib/sender-ref.js +109 -0
  78. package/lib/server.js +37 -11
  79. package/lib/session-directory.js +224 -0
  80. package/lib/session-file-scan.js +55 -0
  81. package/lib/session-input-ipc.js +21 -0
  82. package/lib/session-input.js +396 -23
  83. package/lib/session-name-holder.js +82 -0
  84. package/lib/session-resume.js +3 -0
  85. package/lib/sessions.js +111 -2
  86. package/lib/turn-initiator.js +6 -2
  87. package/lib/updater.js +8 -2
  88. package/lib/ws-schema.js +20 -9
  89. package/lib/yoke/adapters/claude-worker.js +3 -1
  90. package/lib/yoke/adapters/claude.js +13 -17
  91. package/lib/yoke/adapters/codex.js +18 -5
  92. package/lib/yoke/adapters/permission-relay.js +9 -0
  93. package/lib/yoke/interface.js +7 -1
  94. package/package.json +1 -1
  95. package/scripts/check-test-count.js +1 -1
  96. package/scripts/install-local-test.js +45 -1
  97. package/scripts/postinstall.js +1 -0
@@ -16,6 +16,10 @@ var { ownValue } = require("./prompt-kinds/codecs");
16
16
  var { logField, logText } = require("./log-field");
17
17
  var { isKnownVendor } = require("./yoke");
18
18
  var sessionCapabilities = require("./session-capabilities");
19
+ var { checkPermissionMode } = require("./permission-mode-policy");
20
+ var { attachPermissionSettings, isServerAdmin } = require("./project-permission-settings");
21
+ var { effectiveSessionMode } = require("./sessions");
22
+ var { composeRestartingNotice } = require("./build-update-offer");
19
23
  // Kick off SDK agent discovery in the background at module load.
20
24
  // Errors are swallowed inside refresh(); the cache starts empty and fills
21
25
  // when the SDK subprocess completes initialization (~9s on this box).
@@ -44,7 +48,7 @@ if (!process.env.NODE_TEST_CONTEXT) {
44
48
  * message handlers to a project context.
45
49
  *
46
50
  * ctx fields:
47
- * cwd, slug, isMate, osUsers, debug, dangerouslySkipPermissions, currentVersion,
51
+ * cwd, slug, isMate, osUsers, debug, currentVersion,
48
52
  * sm, sdk, tm, clients,
49
53
  * send, sendTo, sendToAdmins, sendToSession, sendToSessionOthers,
50
54
  * opts, usersModule, userPresence, matesModule, pushModule,
@@ -53,7 +57,6 @@ if (!process.env.NODE_TEST_CONTEXT) {
53
57
  * adapter, getProjectList, getProjectCount, getScheduleCount,
54
58
  * moveScheduleToProject, moveAllSchedulesToProject, getHubSchedules,
55
59
  * fetchVersion, isNewer, onCreateWorktree, IGNORED_DIRS,
56
- * scheduleMessage, cancelScheduledMessage,
57
60
  * getProjectOwnerId, setProjectOwnerId,
58
61
  * getUpdateChannel, setUpdateChannel,
59
62
  * getLatestVersion, setLatestVersion
@@ -80,13 +83,6 @@ var PROCESS_STATS_BASE_FIELD_KEYS = [
80
83
  "memAvailableMB",
81
84
  ];
82
85
 
83
- // auto and dontAsk are permission modes only a vendor that lists them in
84
- // capabilities.permissionModes can run. A session whose vendor does not
85
- // declare them (or whose capabilities are not known yet) cannot, so a request
86
- // for one is refused rather than handed to a vendor that would reject or
87
- // ignore it.
88
- var CAPABILITY_GATED_MODES = { auto: true, dontAsk: true };
89
-
90
86
  function attachSessions(ctx) {
91
87
  var cwd = ctx.cwd;
92
88
  var slug = ctx.slug;
@@ -131,8 +127,6 @@ function attachSessions(ctx) {
131
127
  var isNewer = ctx.isNewer;
132
128
  var onCreateWorktree = ctx.onCreateWorktree;
133
129
  var IGNORED_DIRS = ctx.IGNORED_DIRS;
134
- var scheduleMessage = ctx.scheduleMessage;
135
- var cancelScheduledMessage = ctx.cancelScheduledMessage;
136
130
  var getProjectOwnerId = ctx.getProjectOwnerId;
137
131
  var setProjectOwnerId = ctx.setProjectOwnerId;
138
132
  var getUpdateChannel = ctx.getUpdateChannel;
@@ -157,17 +151,120 @@ function attachSessions(ctx) {
157
151
  sendToSessionOthers: sendToSessionOthers,
158
152
  getSessionForWs: getSessionForWs,
159
153
  });
154
+ var permissionSettings = attachPermissionSettings({
155
+ slug: slug,
156
+ sendTo: sendTo,
157
+ send: send,
158
+ opts: opts,
159
+ projectAccess: projectAccess,
160
+ checkRequestedMode: checkRequestedMode,
161
+ });
160
162
 
161
- function sessionAcceptsMode(session, mode) {
162
- if (!ownValue(CAPABILITY_GATED_MODES, mode)) return true;
163
+ // The shared mode check (lib/permission-mode-policy.js) for a mode that a
164
+ // client asks for. `session` is the session it would run in, or null for a
165
+ // default that applies to whatever vendor a session later uses (the project's
166
+ // default vendor stands in for it). A refusal is logged and returned as
167
+ // { ok: false, error }.
168
+ function checkRequestedMode(session, mode, what) {
163
169
  var vendor = sessionCapabilities.effectiveVendorId(sm, session, adapter && adapter.vendor);
164
- return sessionCapabilities.capabilitiesFor(sm, vendor).permissionModes.indexOf(mode) !== -1;
170
+ var verdict = checkPermissionMode(mode, {
171
+ supportedModes: sessionCapabilities.capabilitiesFor(sm, vendor).permissionModes,
172
+ });
173
+ if (!verdict.ok) {
174
+ console.warn("[project-sessions] refused " + what + " " + logField(mode) + " in " + slug + ": " + verdict.error);
175
+ }
176
+ return verdict;
177
+ }
178
+
179
+ // Changing a default that every session of the project (or of the server)
180
+ // starts from takes the same standing as changing project settings: an
181
+ // administrator, or the project owner in multi-user mode; in single-user
182
+ // mode the connection is the implicit owner (projectAccess.isAdmin).
183
+ // The server-wide default reaches every project, so it is the administrator's
184
+ // alone (ownerAllowed false); the project default also accepts the owner.
185
+ function maySetDefaultMode(ws, ownerAllowed) {
186
+ if (!projectAccess || typeof projectAccess.isAdmin !== "function") return false;
187
+ var user = ws._clagenticUser;
188
+ if (isServerAdmin(projectAccess, ws)) return true;
189
+ if (!ownerAllowed) return false;
190
+ var ownerId = getProjectOwnerId ? getProjectOwnerId() : null;
191
+ return !!(user && ownerId && user.id === ownerId);
192
+ }
193
+
194
+ // Both default setters share this gate. Returns true when the request was
195
+ // refused (and answered), false when the caller may go on.
196
+ function refuseDefaultMode(ws, msg) {
197
+ var what = msg.type;
198
+ var serverWide = what === "set_server_default_mode";
199
+ if (!maySetDefaultMode(ws, !serverWide)) {
200
+ console.warn("[project-sessions] refused " + what + " in " + slug + ": caller is not " + (serverWide ? "an admin" : "an admin or the project owner"));
201
+ sendTo(ws, { type: "error", text: serverWide
202
+ ? "Only admins can change the server default permission mode."
203
+ : "Only project owners or admins can change the default permission mode." });
204
+ return true;
205
+ }
206
+ var verdict = checkRequestedMode(null, msg.mode, what);
207
+ if (!verdict.ok) {
208
+ sendTo(ws, { type: "error", text: verdict.error });
209
+ return true;
210
+ }
211
+ return false;
212
+ }
213
+
214
+ // The mode a client is shown for a session (sessions.js effectiveSessionMode).
215
+ function shownMode(session) {
216
+ return effectiveSessionMode(session, sm);
217
+ }
218
+
219
+ // Re-modes every session this project holds to `mode`, each through the
220
+ // shared check for its own vendor. A refusal skips that session and is
221
+ // reported; it never stops the others. Resolves { applied, skipped }.
222
+ function applyModeToSessions(mode) {
223
+ var applied = 0;
224
+ var skipped = [];
225
+ var pending = [];
226
+ sm.sessions.forEach(function (s) {
227
+ var verdict = sdk.checkModeFor(s, mode, true);
228
+ if (!verdict.ok) {
229
+ console.warn("[project-sessions] apply of " + logField(mode) + " skipped session " + s.localId + " in " + slug + ": " + verdict.error);
230
+ skipped.push({ sessionId: s.localId, reason: verdict.error });
231
+ return;
232
+ }
233
+ pending.push(sdk.setPermissionMode(s, mode).then(function (ok) {
234
+ if (ok) applied++;
235
+ else skipped.push({ sessionId: s.localId, reason: "The session did not accept the mode" });
236
+ }));
237
+ });
238
+ return Promise.all(pending).then(function () { return { applied: applied, skipped: skipped }; });
239
+ }
240
+
241
+ // Each client is told the mode of the session it is viewing, never the
242
+ // project default (see effectiveSessionMode in sessions.js).
243
+ function pushSessionModes() {
244
+ clients.forEach(function (client) {
245
+ var viewed = getSessionForWs(client);
246
+ sendTo(client, { type: "config_state", model: sm.effectiveSessionModel(viewed), mode: shownMode(viewed), effort: sm.currentEffort || "medium", betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
247
+ });
248
+ }
249
+
250
+ // The default a new session starts in (and the fallback for one that has no
251
+ // mode of its own). A running session keeps its mode unless the person chose
252
+ // to apply the new one to the running sessions.
253
+ function changeDefaultMode(ws, msg) {
254
+ sm.currentPermissionMode = msg.mode;
255
+ sm._savedDefaultMode = msg.mode;
256
+ if (msg.applyToRunning !== true) return;
257
+ applyModeToSessions(msg.mode).then(function (outcome) {
258
+ pushSessionModes();
259
+ sendTo(ws, { type: "permission_mode_applied", mode: msg.mode, applied: outcome.applied, skipped: outcome.skipped });
260
+ });
165
261
  }
166
262
 
167
263
  function handleSessionsMessage(ws, msg) {
168
264
 
169
265
  if (promptResponses.handlePromptMessage(ws, msg)) return true;
170
266
  if (sessionGrants.handleGrantMessage(ws, msg)) return true;
267
+ if (permissionSettings.handlePermissionSettingsMessage(ws, msg)) return true;
171
268
 
172
269
  if (msg.type === "push_subscribe") {
173
270
  var _pushUserId = ws._clagenticUser ? ws._clagenticUser.id : null;
@@ -245,17 +342,17 @@ function attachSessions(ctx) {
245
342
  catch (e) { /* recents are best-effort */ }
246
343
  }
247
344
  var newSess = sm.createSession(sessionOpts, ws);
248
- // Seed per-session model/mode from the project/global DEFAULT
249
- // (sm._savedDefaultModel / sm._savedDefaultMode, populated at project
250
- // startup from onGetProjectDefaultModel/onGetServerDefaultModel — see
251
- // lib/project.js) so the first query uses the right values even before
252
- // the user changes anything. Seeding from sm.currentModel/
253
- // sm.currentPermissionMode directly is wrong (lr-db0437): those two
254
- // fields also mirror whatever model/mode the CURRENTLY ACTIVE session
255
- // happens to be using, so a brand-new session would inherit another
256
- // session's in-progress choice instead of the actual saved default.
345
+ // Seed the per-session model from the project/global DEFAULT
346
+ // (sm._savedDefaultModel, populated at project startup from
347
+ // onGetProjectDefaultModel/onGetServerDefaultModel — see lib/project.js)
348
+ // so the first query uses the right value even before the user changes
349
+ // anything. Seeding from sm.currentModel directly is wrong: that field
350
+ // also mirrors whatever model the CURRENTLY ACTIVE session happens to be
351
+ // using, so a brand-new session would inherit another session's
352
+ // in-progress choice instead of the actual saved default.
353
+ // The mode is given by sm.createSession from sm._savedDefaultMode and is
354
+ // saved with the session, so it is not copied here.
257
355
  newSess.model = sm._savedDefaultModel || null;
258
- newSess.permissionMode = sm._savedDefaultMode || null;
259
356
  ws._clagenticActiveSession = newSess.localId;
260
357
  // Apply project-level email defaults to new session
261
358
  if (typeof ctx._email === "object" && ctx._email.getEmailDefaults) {
@@ -600,7 +697,7 @@ function attachSessions(ctx) {
600
697
  // reintroducing the sm.currentModel-only bug this task fixes.
601
698
  var _swSess = sm.sessions.get(msg.id);
602
699
  var _swModel = sm.effectiveSessionModel(_swSess);
603
- var _swMode = (_swSess && _swSess.permissionMode) || sm.currentPermissionMode || "default";
700
+ var _swMode = shownMode(_swSess);
604
701
  sendTo(ws, { type: "config_state", model: _swModel, mode: _swMode, effort: sm.currentEffort || "medium", betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
605
702
  var swPresKey = ws._clagenticUser ? ws._clagenticUser.id : "_default";
606
703
  userPresence.setPresence(slug, swPresKey, msg.id, null);
@@ -832,7 +929,7 @@ function attachSessions(ctx) {
832
929
  // Targeted reply — only this client's chip updates, not all sessions.
833
930
  // lr-041af8: shared helper (see switch_session above and
834
931
  // effectiveSessionModel() in sessions.js).
835
- sendTo(ws, { type: "config_state", model: sm.effectiveSessionModel(session), mode: session.permissionMode || sm.currentPermissionMode || "default", effort: sm.currentEffort || "medium", betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
932
+ sendTo(ws, { type: "config_state", model: sm.effectiveSessionModel(session), mode: shownMode(session), effort: sm.currentEffort || "medium", betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
836
933
  }).catch(function (e) {
837
934
  // Defensive: setModel is not expected to reject (its own try/catch
838
935
  // returns { ok: false } instead), but a rejection must still reach
@@ -920,7 +1017,7 @@ function attachSessions(ctx) {
920
1017
  // fixes elsewhere. vendorSession may be null (no active session for
921
1018
  // this ws yet) — effectiveSessionModel handles that by falling back
922
1019
  // to the shared default, same as every other targeted sender.
923
- sendTo(ws, { type: "config_state", model: sm.effectiveSessionModel(vendorSession), mode: sm.currentPermissionMode || "default", effort: sm.currentEffort || "medium", betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
1020
+ sendTo(ws, { type: "config_state", model: sm.effectiveSessionModel(vendorSession), mode: shownMode(vendorSession), effort: sm.currentEffort || "medium", betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
924
1021
  }
925
1022
  return true;
926
1023
  }
@@ -938,7 +1035,7 @@ function attachSessions(ctx) {
938
1035
  // (sdk-bridge.js) as the project/global default.
939
1036
  sm.currentModel = msg.model;
940
1037
  sm._savedDefaultModel = msg.model;
941
- send({ type: "config_state", model: sm.currentModel || "", mode: sm.currentPermissionMode || "default", effort: sm.currentEffort || "medium", betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
1038
+ send({ type: "config_state", model: sm.currentModel || "", effort: sm.currentEffort || "medium", betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
942
1039
  return true;
943
1040
  }
944
1041
 
@@ -949,51 +1046,48 @@ function attachSessions(ctx) {
949
1046
  // lr-db0437: same as set_server_default_model above — default-only write.
950
1047
  sm.currentModel = msg.model;
951
1048
  sm._savedDefaultModel = msg.model;
952
- send({ type: "config_state", model: sm.currentModel || "", mode: sm.currentPermissionMode || "default", effort: sm.currentEffort || "medium", betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
1049
+ send({ type: "config_state", model: sm.currentModel || "", effort: sm.currentEffort || "medium", betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
953
1050
  return true;
954
1051
  }
955
1052
 
956
1053
  if (msg.type === "set_permission_mode" && msg.mode) {
957
1054
  var session = getSessionForWs(ws);
958
- if (session && !sessionAcceptsMode(session, msg.mode)) {
959
- sendTo(ws, { type: "error", text: "This session's agent does not support the " + String(msg.mode).slice(0, 40) + " permission mode" });
960
- sendTo(ws, { type: "config_state", model: sm.effectiveSessionModel(session), mode: session.permissionMode || sm.currentPermissionMode || "default", effort: sm.currentEffort || "medium", betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
1055
+ var modeVerdict = checkRequestedMode(session, msg.mode, "set_permission_mode");
1056
+ if (!modeVerdict.ok) {
1057
+ sendTo(ws, { type: "error", text: modeVerdict.error });
1058
+ if (!session) return true;
1059
+ sendTo(ws, { type: "config_state", model: sm.effectiveSessionModel(session), mode: shownMode(session), effort: sm.currentEffort || "medium", betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
961
1060
  return true;
962
1061
  }
963
1062
  if (session) {
964
1063
  sdk.setPermissionMode(session, msg.mode).then(function () {
965
1064
  // Targeted reply — only update the requesting client's chip.
966
1065
  // lr-041af8: shared helper (see effectiveSessionModel() in sessions.js).
967
- sendTo(ws, { type: "config_state", model: sm.effectiveSessionModel(session), mode: session.permissionMode || "default", effort: sm.currentEffort || "medium", betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
1066
+ sendTo(ws, { type: "config_state", model: sm.effectiveSessionModel(session), mode: shownMode(session), effort: sm.currentEffort || "medium", betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
968
1067
  });
969
1068
  }
970
1069
  return true;
971
1070
  }
972
1071
 
1072
+ // Changing a default changes what NEW sessions start in and nothing else.
1073
+ // The request may also carry applyToRunning: true, the person's explicit
1074
+ // "Apply to running sessions" choice for this one change (it is not a
1075
+ // stored setting), which re-modes this project's sessions as well.
973
1076
  if (msg.type === "set_server_default_mode" && msg.mode) {
1077
+ if (refuseDefaultMode(ws, msg)) return true;
974
1078
  if (typeof opts.onSetServerDefaultMode === "function") {
975
1079
  opts.onSetServerDefaultMode(msg.mode);
976
1080
  }
977
- sm.currentPermissionMode = msg.mode;
978
- var session = getSessionForWs(ws);
979
- if (session) {
980
- sdk.setPermissionMode(session, msg.mode);
981
- }
982
- send({ type: "config_state", model: sm.currentModel || "", mode: sm.currentPermissionMode, effort: sm.currentEffort || "medium", betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
1081
+ changeDefaultMode(ws, msg);
983
1082
  return true;
984
1083
  }
985
1084
 
986
1085
  if (msg.type === "set_project_default_mode" && msg.mode) {
1086
+ if (refuseDefaultMode(ws, msg)) return true;
987
1087
  if (typeof opts.onSetProjectDefaultMode === "function") {
988
1088
  opts.onSetProjectDefaultMode(slug, msg.mode);
989
1089
  }
990
- sm.currentPermissionMode = msg.mode;
991
- // Apply to ALL active sessions in this project, not just the focused one.
992
- // Without this, sessions that already started a query keep the old mode.
993
- sm.sessions.forEach(function (s) {
994
- sdk.setPermissionMode(s, msg.mode);
995
- });
996
- send({ type: "config_state", model: sm.currentModel || "", mode: sm.currentPermissionMode, effort: sm.currentEffort || "medium", betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
1090
+ changeDefaultMode(ws, msg);
997
1091
  return true;
998
1092
  }
999
1093
 
@@ -1001,7 +1095,7 @@ function attachSessions(ctx) {
1001
1095
  var session = getSessionForWs(ws);
1002
1096
  if (!session) {
1003
1097
  sm.currentEffort = msg.effort;
1004
- send({ type: "config_state", model: sm.currentModel || "", mode: sm.currentPermissionMode || "default", effort: sm.currentEffort, betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
1098
+ send({ type: "config_state", model: sm.currentModel || "", effort: sm.currentEffort, betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
1005
1099
  return true;
1006
1100
  }
1007
1101
  // The effort is recorded only once the running query has taken it
@@ -1009,7 +1103,7 @@ function attachSessions(ctx) {
1009
1103
  // bridge as an error; the requester's picker is put back on the effort
1010
1104
  // the session still runs at.
1011
1105
  sdk.setEffort(session, msg.effort).then(function (result) {
1012
- var state = { type: "config_state", model: sm.currentModel || "", mode: sm.currentPermissionMode || "default", effort: sm.currentEffort || "medium", betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 };
1106
+ var state = { type: "config_state", model: sm.currentModel || "", effort: sm.currentEffort || "medium", betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 };
1013
1107
  if (result && result.ok === false) sendTo(ws, state);
1014
1108
  else send(state);
1015
1109
  }).catch(function (err) {
@@ -1023,7 +1117,7 @@ function attachSessions(ctx) {
1023
1117
  opts.onSetServerDefaultEffort(msg.effort);
1024
1118
  }
1025
1119
  sm.currentEffort = msg.effort;
1026
- send({ type: "config_state", model: sm.currentModel || "", mode: sm.currentPermissionMode || "default", effort: sm.currentEffort, betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
1120
+ send({ type: "config_state", model: sm.currentModel || "", effort: sm.currentEffort, betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
1027
1121
  return true;
1028
1122
  }
1029
1123
 
@@ -1032,13 +1126,13 @@ function attachSessions(ctx) {
1032
1126
  opts.onSetProjectDefaultEffort(slug, msg.effort);
1033
1127
  }
1034
1128
  sm.currentEffort = msg.effort;
1035
- send({ type: "config_state", model: sm.currentModel || "", mode: sm.currentPermissionMode || "default", effort: sm.currentEffort, betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
1129
+ send({ type: "config_state", model: sm.currentModel || "", effort: sm.currentEffort, betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
1036
1130
  return true;
1037
1131
  }
1038
1132
 
1039
1133
  if (msg.type === "set_betas") {
1040
1134
  sm.currentBetas = msg.betas || [];
1041
- send({ type: "config_state", model: sm.currentModel || "", mode: sm.currentPermissionMode || "default", effort: sm.currentEffort || "medium", betas: sm.currentBetas, thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
1135
+ send({ type: "config_state", model: sm.currentModel || "", effort: sm.currentEffort || "medium", betas: sm.currentBetas, thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
1042
1136
  return true;
1043
1137
  }
1044
1138
 
@@ -1047,7 +1141,7 @@ function attachSessions(ctx) {
1047
1141
  opts.onSetServerDefaultBetas(msg.betas || []);
1048
1142
  }
1049
1143
  sm.currentBetas = msg.betas || [];
1050
- send({ type: "config_state", model: sm.currentModel || "", mode: sm.currentPermissionMode || "default", effort: sm.currentEffort || "medium", betas: sm.currentBetas, thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
1144
+ send({ type: "config_state", model: sm.currentModel || "", effort: sm.currentEffort || "medium", betas: sm.currentBetas, thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
1051
1145
  return true;
1052
1146
  }
1053
1147
 
@@ -1056,14 +1150,14 @@ function attachSessions(ctx) {
1056
1150
  opts.onSetProjectDefaultBetas(slug, msg.betas || []);
1057
1151
  }
1058
1152
  sm.currentBetas = msg.betas || [];
1059
- send({ type: "config_state", model: sm.currentModel || "", mode: sm.currentPermissionMode || "default", effort: sm.currentEffort || "medium", betas: sm.currentBetas, thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
1153
+ send({ type: "config_state", model: sm.currentModel || "", effort: sm.currentEffort || "medium", betas: sm.currentBetas, thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
1060
1154
  return true;
1061
1155
  }
1062
1156
 
1063
1157
  if (msg.type === "set_thinking") {
1064
1158
  sm.currentThinking = msg.thinking || "adaptive";
1065
1159
  if (msg.budgetTokens) sm.currentThinkingBudget = msg.budgetTokens;
1066
- send({ type: "config_state", model: sm.currentModel || "", mode: sm.currentPermissionMode || "default", effort: sm.currentEffort || "medium", betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
1160
+ send({ type: "config_state", model: sm.currentModel || "", effort: sm.currentEffort || "medium", betas: sm.currentBetas || [], thinking: sm.currentThinking || "adaptive", thinkingBudget: sm.currentThinkingBudget || 10000 });
1067
1161
  return true;
1068
1162
  }
1069
1163
 
@@ -1815,18 +1909,23 @@ function attachSessions(ctx) {
1815
1909
  // issued, and only if that offer is still true (lib/build-update-restart.js).
1816
1910
  if (msg.type === "restart_for_build_update") {
1817
1911
  if (typeof opts.onRestartForBuildUpdate !== "function") {
1818
- sendTo(ws, { type: "restart_for_build_update_result", ok: false, error: "Restart not supported" });
1912
+ sendTo(ws, { type: "restart_for_build_update_result", requestId: typeof msg.requestId === "string" ? msg.requestId : undefined, ok: false, error: "Restart not supported" });
1819
1913
  return true;
1820
1914
  }
1821
1915
  var buildToken = typeof msg.confirmToken === "string" ? msg.confirmToken : null;
1916
+ // Echoed in every result so the client releases only the button that asked.
1917
+ var buildRequestId = typeof msg.requestId === "string" && msg.requestId.length <= 64 ? msg.requestId : undefined;
1918
+ var buildResult = function (fields) {
1919
+ return Object.assign({ type: "restart_for_build_update_result", requestId: buildRequestId }, fields);
1920
+ };
1822
1921
  Promise.resolve(opts.onRestartForBuildUpdate(buildToken)).then(function (outcome) {
1823
1922
  if (outcome.status === "not_stale") {
1824
1923
  var nothingToApply = "No newer build is installed, so there is nothing to apply.";
1825
- sendTo(ws, { type: "restart_for_build_update_result", ok: false, error: nothingToApply });
1924
+ sendTo(ws, buildResult({ ok: false, error: nothingToApply }));
1826
1925
  sendTo(ws, { type: "error", text: nothingToApply });
1827
1926
  } else if (outcome.status === "confirm") {
1828
1927
  var offer = outcome.offer;
1829
- sendTo(ws, { type: "restart_for_build_update_result", ok: false, needsConfirmation: true });
1928
+ sendTo(ws, buildResult({ ok: false, needsConfirmation: true }));
1830
1929
  sendTo(ws, {
1831
1930
  type: "diagnostic",
1832
1931
  severity: "warning",
@@ -1836,12 +1935,17 @@ function attachSessions(ctx) {
1836
1935
  actionable: { label: "Restart now", action: "confirm_build_update_restart" },
1837
1936
  });
1838
1937
  } else {
1839
- sendTo(ws, { type: "restart_for_build_update_result", ok: true });
1840
- send({ type: "toast", level: "info", message: "Restarting to apply the installed update. Running turns get up to " + (outcome.waitSeconds || 60) + " seconds to finish first." });
1938
+ // The requester always gets the pending state, including on a repeat
1939
+ // request: the offer is withdrawn and one card that does not close on a
1940
+ // timer takes its place. A timed toast here vanished before it could be
1941
+ // read and left the offer card clickable.
1942
+ sendTo(ws, buildResult({ ok: true }));
1943
+ sendTo(ws, { type: "diagnostic_retract", source: "build-update" });
1944
+ sendTo(ws, composeRestartingNotice());
1841
1945
  }
1842
1946
  }).catch(function (e) {
1843
1947
  console.error("[project] restart_for_build_update failed:", e && e.message ? e.message : e);
1844
- sendTo(ws, { type: "restart_for_build_update_result", ok: false, error: "Could not prepare the restart offer" });
1948
+ sendTo(ws, buildResult({ ok: false, error: "Could not prepare the restart offer" }));
1845
1949
  sendTo(ws, { type: "error", text: "Could not prepare the restart offer. Nothing was restarted." });
1846
1950
  });
1847
1951
  return true;
@@ -4,6 +4,8 @@ var { promptsFor } = require("./prompt-registry");
4
4
  var { putOwn } = require("./prompt-kinds/codecs");
5
5
  var { isKnownVendor } = require("./yoke");
6
6
  var { INITIATORS } = require("./turn-initiator");
7
+ var { createWsHandlers: createScheduledInputHandlers } = require("./scheduled-input/ws");
8
+ var { stampRecord } = require("./injected-sender");
7
9
  var promptSource = require("./prompt-source");
8
10
 
9
11
  /**
@@ -25,7 +27,7 @@ var promptSource = require("./prompt-source");
25
27
  * browserState - { _browserTabList, _extensionWs } (mutable refs)
26
28
  * sendExtensionCommandAny, requestTabContext,
27
29
  * startFileWatch, stopFileWatch,
28
- * scheduleMessage, cancelScheduledMessage,
30
+ * scheduledInput - the scheduled-input service (null in a bare harness)
29
31
  * loadContextSources, saveContextSources,
30
32
  * digestDmTurn,
31
33
  * adapter - YOKE adapter instance
@@ -120,14 +122,18 @@ function attachUserMessage(ctx) {
120
122
  var sendExtensionCommandAny = ctx.sendExtensionCommandAny;
121
123
  var requestTabContext = ctx.requestTabContext;
122
124
 
123
- var scheduleMessage = ctx.scheduleMessage;
124
- var cancelScheduledMessage = ctx.cancelScheduledMessage;
125
-
126
125
  var loadContextSources = ctx.loadContextSources;
127
126
  var saveContextSources = ctx.saveContextSources;
128
127
 
129
128
  var adapter = ctx.adapter;
130
129
 
130
+ var scheduledInput = createScheduledInputHandlers({
131
+ slug: slug,
132
+ service: ctx.scheduledInput || null,
133
+ getSessionForWs: getSessionForWs,
134
+ sendTo: sendTo,
135
+ });
136
+
131
137
  // --------------- Terminal list broadcast ---------------
132
138
 
133
139
  // Each client only sees their own terminals — send filtered term_list messages individually.
@@ -322,45 +328,8 @@ function attachUserMessage(ctx) {
322
328
  // --- Loop message delegation (project-loop.js) ---
323
329
  if (_loop.handleLoopMessage(ws, msg)) return true;
324
330
 
325
- // --- Schedule message for after rate limit resets ---
326
- if (msg.type === "schedule_message") {
327
- var schedSession = getSessionForWs(ws);
328
- if (!schedSession || !msg.text || !msg.resetsAt) return true;
329
- scheduleMessage(schedSession, msg.text, msg.resetsAt);
330
- return true;
331
- }
332
-
333
- if (msg.type === "cancel_scheduled_message") {
334
- var cancelSession = getSessionForWs(ws);
335
- if (!cancelSession) return true;
336
- cancelScheduledMessage(cancelSession);
337
- return true;
338
- }
339
-
340
- if (msg.type === "send_scheduled_now") {
341
- var nowSession = getSessionForWs(ws);
342
- if (!nowSession || !nowSession.scheduledMessage) return true;
343
- var schedText = nowSession.scheduledMessage.text;
344
- clearTimeout(nowSession.scheduledMessage.timer);
345
- nowSession.scheduledMessage = null;
346
- console.log("[project] Scheduled message sent immediately for session " + nowSession.localId);
347
- sm.sendAndRecord(nowSession, { type: "scheduled_message_sent" });
348
- var userMsg = { type: "user_message", text: schedText, _ts: Date.now() };
349
- // lr-2ea2a7: routes through the shared history-cap helper (see grep-guard
350
- // test) instead of pushing directly, so a long-running session's heap
351
- // history stays bounded even for scheduled-message injection.
352
- sm.recordHistoryEntry(nowSession, userMsg, true);
353
- sm.appendToSessionFile(nowSession, userMsg);
354
- sendToSession(nowSession.localId, userMsg);
355
- nowSession.isProcessing = true;
356
- onProcessingChanged();
357
- sendToSession(nowSession.localId, { type: "status", status: "processing" });
358
- // A person forced the send, but the text is the one scheduled earlier:
359
- // it is a scheduled turn that fired manually, not typed text.
360
- sdk.startQuery(nowSession, schedText, null, ensureProjectAccessForSession(nowSession), INITIATORS.SCHEDULED_NOW);
361
- sm.broadcastSessionList();
362
- return true;
363
- }
331
+ // --- Scheduled input: the clock button and the scheduled-message bubble ---
332
+ if (scheduledInput.handle(ws, msg)) return true;
364
333
 
365
334
  if (msg.type !== "message") return false;
366
335
  if (!msg.text && (!msg.images || msg.images.length === 0) && (!msg.pastes || msg.pastes.length === 0)) return true;
@@ -395,7 +364,9 @@ function attachUserMessage(ctx) {
395
364
 
396
365
  // Keep any pending scheduled message alive when user sends a regular message
397
366
 
398
- var userMsg2 = { type: "user_message", text: msg.text || "" };
367
+ // The human initiator is what marks an entry as the operator's own: this is
368
+ // the one place the authenticated browser composer's text is recorded.
369
+ var userMsg2 = stampRecord({ type: "user_message", text: msg.text || "", initiator: INITIATORS.HUMAN });
399
370
  // Attach sender info for multi-user attribution (backward-compatible: old clients ignore these)
400
371
  if (ws._clagenticUser) {
401
372
  userMsg2.from = ws._clagenticUser.id;
@@ -568,12 +539,12 @@ function attachUserMessage(ctx) {
568
539
  // No active query (or worker idle between queries): start a new query
569
540
  session._queryStartTs = Date.now();
570
541
  console.log("[PERF] project.js: startQuery called, localId=" + session.localId + " t=0ms");
571
- sdk.startQuery(session, finalText, msg.images, ensureProjectAccessForSession(session), INITIATORS.HUMAN);
542
+ sdk.startQuery(session, finalText, msg.images, ensureProjectAccessForSession(session), INITIATORS.HUMAN, userMsg2.messageId);
572
543
  } else {
573
- sdk.pushMessage(session, finalText, msg.images, INITIATORS.HUMAN);
544
+ sdk.pushMessage(session, finalText, msg.images, INITIATORS.HUMAN, userMsg2.messageId);
574
545
  }
575
546
  } else {
576
- sdk.pushMessage(session, finalText, msg.images, INITIATORS.HUMAN);
547
+ sdk.pushMessage(session, finalText, msg.images, INITIATORS.HUMAN, userMsg2.messageId);
577
548
  }
578
549
  sm.broadcastSessionList();
579
550
  }