@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.
- package/README.md +14 -1
- package/bin/cli.js +38 -16
- package/lib/agents.js +1 -55
- package/lib/bash-auto-approve.js +133 -0
- package/lib/build-identity.js +23 -5
- package/lib/build-sha.json +2 -2
- package/lib/build-update-check.js +0 -62
- package/lib/build-update-offer.js +23 -13
- package/lib/build-update-restart.js +59 -43
- package/lib/cli/arg-validation.js +50 -0
- package/lib/cli/daemon-launch.js +5 -4
- package/lib/cli/menus.js +2 -2
- package/lib/cli/schedule-input-subcommands.js +327 -0
- package/lib/cli/session-subcommands.js +101 -17
- package/lib/client-build-id.js +56 -0
- package/lib/daemon.js +38 -6
- package/lib/identity/index.js +9 -4
- package/lib/injected-sender.js +225 -0
- package/lib/message-sender-record.js +133 -0
- package/lib/pending-prompts.js +55 -0
- package/lib/permission-mode-policy.js +63 -0
- package/lib/permission-settings.js +96 -0
- package/lib/permission-summary.js +67 -0
- package/lib/project-connection.js +10 -11
- package/lib/project-external-trigger.js +9 -1
- package/lib/project-loop.js +50 -23
- package/lib/project-permission-settings.js +59 -0
- package/lib/project-prompt-responses.js +25 -15
- package/lib/project-sessions.js +164 -60
- package/lib/project-user-message.js +18 -47
- package/lib/project.js +46 -74
- package/lib/public/app.js +4 -7
- package/lib/public/css/channel-layout.css +20 -0
- package/lib/public/css/diagnostics.css +7 -0
- package/lib/public/css/input.css +78 -0
- package/lib/public/css/menus.css +17 -0
- package/lib/public/css/overlays.css +0 -3
- package/lib/public/css/scheduler-modal.css +5 -0
- package/lib/public/index.html +44 -8
- package/lib/public/modules/app-connection.js +18 -1
- package/lib/public/modules/app-loop-ui.js +11 -1
- package/lib/public/modules/app-messages.js +23 -30
- package/lib/public/modules/app-panels.js +10 -3
- package/lib/public/modules/app-rate-limit.js +64 -88
- package/lib/public/modules/app-rendering.js +37 -6
- package/lib/public/modules/approve-tools-toggle.js +33 -0
- package/lib/public/modules/at-agents.js +8 -0
- package/lib/public/modules/build-guard.js +61 -0
- package/lib/public/modules/build-update-detail.js +1 -4
- package/lib/public/modules/diagnostics.js +98 -9
- package/lib/public/modules/injected-identity.js +78 -0
- package/lib/public/modules/injected-label.js +62 -0
- package/lib/public/modules/input.js +64 -114
- package/lib/public/modules/loop-hooks-warning.js +20 -0
- package/lib/public/modules/mode-options.js +35 -16
- package/lib/public/modules/permission-settings.js +56 -0
- package/lib/public/modules/project-settings.js +3 -0
- package/lib/public/modules/rate-limit-state.js +47 -22
- package/lib/public/modules/schedule-panel.js +234 -0
- package/lib/public/modules/schedule-time.js +193 -0
- package/lib/public/modules/scheduled-input-actions.js +19 -0
- package/lib/public/modules/scheduler-config.js +12 -0
- package/lib/public/modules/scheduler.js +8 -0
- package/lib/public/modules/server-settings.js +5 -9
- package/lib/public/modules/settings-defaults.js +18 -4
- package/lib/runtime-env.js +4 -0
- package/lib/scheduled-input/ipc.js +92 -0
- package/lib/scheduled-input/rate-limit-resume.js +53 -0
- package/lib/scheduled-input/records.js +350 -0
- package/lib/scheduled-input/service.js +743 -0
- package/lib/scheduled-input/store.js +132 -0
- package/lib/scheduled-input/ws.js +194 -0
- package/lib/scheduler.js +4 -0
- package/lib/sdk-bridge.js +194 -175
- package/lib/sdk-message-processor.js +13 -5
- package/lib/sdk-skill-discovery.js +17 -0
- package/lib/sender-ref.js +109 -0
- package/lib/server.js +37 -11
- package/lib/session-directory.js +224 -0
- package/lib/session-file-scan.js +55 -0
- package/lib/session-input-ipc.js +21 -0
- package/lib/session-input.js +396 -23
- package/lib/session-name-holder.js +82 -0
- package/lib/session-resume.js +3 -0
- package/lib/sessions.js +111 -2
- package/lib/turn-initiator.js +6 -2
- package/lib/updater.js +8 -2
- package/lib/ws-schema.js +20 -9
- package/lib/yoke/adapters/claude-worker.js +3 -1
- package/lib/yoke/adapters/claude.js +13 -17
- package/lib/yoke/adapters/codex.js +18 -5
- package/lib/yoke/adapters/permission-relay.js +9 -0
- package/lib/yoke/interface.js +7 -1
- package/package.json +1 -1
- package/scripts/check-test-count.js +1 -1
- package/scripts/install-local-test.js +45 -1
- package/scripts/postinstall.js +1 -0
package/lib/session-input.js
CHANGED
|
@@ -18,16 +18,30 @@ var { getNewWorkRefusal } = require("./new-work-gate");
|
|
|
18
18
|
var agentsModule = require("./agents");
|
|
19
19
|
var sessionResume = require("./session-resume");
|
|
20
20
|
var { logField, logText } = require("./log-field");
|
|
21
|
+
var { validateFrom, validateFromSession, senderFields, forClient, stampRecord, newMessageId, isInjected, consoleTurnEntry } = require("./injected-sender");
|
|
22
|
+
var senderRef = require("./sender-ref");
|
|
23
|
+
var { findNameHolder, releaseReapedHolders } = require("./session-name-holder");
|
|
24
|
+
var directory = require("./session-directory");
|
|
25
|
+
var permissionSettings = require("./permission-settings");
|
|
26
|
+
var { checkPermissionMode } = require("./permission-mode-policy");
|
|
27
|
+
var { slugifyAgentName } = agentsModule;
|
|
21
28
|
|
|
22
29
|
// The result vocabulary reported to the caller. "error" is not a verdict on
|
|
23
30
|
// the request: delivery was attempted and failed, so a caller may retry.
|
|
31
|
+
// "ambiguous" is a clean answer, not a failure: an address named several
|
|
32
|
+
// sessions and the caller picks one by id (see resolveSendTarget).
|
|
33
|
+
// "busy" is only answered to a scheduled firing (fireScheduled): the session was
|
|
34
|
+
// mid-turn and the input was not delivered; the IPC verbs never answer it.
|
|
24
35
|
var RESULT = Object.freeze({
|
|
25
36
|
DELIVERED: "delivered",
|
|
26
37
|
RESUMED: "resumed-and-delivered",
|
|
27
38
|
OPENED: "opened",
|
|
39
|
+
LISTED: "listed",
|
|
28
40
|
NOT_FOUND: "not-found",
|
|
41
|
+
AMBIGUOUS: "ambiguous",
|
|
29
42
|
REFUSED: "refused",
|
|
30
43
|
ERROR: "error",
|
|
44
|
+
BUSY: "busy",
|
|
31
45
|
});
|
|
32
46
|
|
|
33
47
|
// Initiators injected input may name. HUMAN is deliberately absent.
|
|
@@ -136,6 +150,9 @@ function neverThrows(fn) {
|
|
|
136
150
|
function logResult(verb, req, out) {
|
|
137
151
|
req = req || {};
|
|
138
152
|
out = out || {};
|
|
153
|
+
// A scheduled firing skipped for a busy session retries every few seconds;
|
|
154
|
+
// one line per retry would bury the log.
|
|
155
|
+
if (out.result === RESULT.BUSY) return;
|
|
139
156
|
var line = "[session-input] " + verb + ": project=" + logField(req.projectSlug)
|
|
140
157
|
+ " session=" + logField(out.consoleSessionId || req.sessionId)
|
|
141
158
|
+ " result=" + logField(out.result);
|
|
@@ -156,11 +173,19 @@ function logged(verb, fn) {
|
|
|
156
173
|
};
|
|
157
174
|
}
|
|
158
175
|
|
|
176
|
+
function present(value) {
|
|
177
|
+
return value !== undefined && value !== null && value !== false;
|
|
178
|
+
}
|
|
179
|
+
|
|
159
180
|
// deps:
|
|
160
181
|
// getProject(slug) -> project context | null
|
|
182
|
+
// listProjects() -> [{ slug, project }] every project held (default: none)
|
|
161
183
|
// agentCatalog(project) -> [{ name }] named agents on offer (default: the picker's catalog)
|
|
184
|
+
// getPermissionSettings() -> the server's permission settings (lib/permission-settings.js)
|
|
185
|
+
// or null (default: the settings' defaults)
|
|
162
186
|
function createSessionInput(deps) {
|
|
163
187
|
var getProject = deps.getProject;
|
|
188
|
+
var listProjects = deps.listProjects || function () { return []; };
|
|
164
189
|
|
|
165
190
|
function defaultAgentCatalog(project) {
|
|
166
191
|
// Neither lookup throws: both answer [] when discovery has not run or the
|
|
@@ -182,26 +207,191 @@ function createSessionInput(deps) {
|
|
|
182
207
|
return typeof prompt === "string" && prompt ? null : refused("prompt must be a non-empty string");
|
|
183
208
|
}
|
|
184
209
|
|
|
210
|
+
function checkFrom(from) {
|
|
211
|
+
var v = validateFrom(from);
|
|
212
|
+
return v.error ? refused(v.error) : null;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function checkFromSession(fromSession) {
|
|
216
|
+
var v = validateFromSession(fromSession);
|
|
217
|
+
return v.error ? refused(v.error) : null;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// The directory entries of one project.
|
|
221
|
+
function entriesOf(slug, project) {
|
|
222
|
+
return directory.collect([{ slug: slug, project: project }]);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Resolves --to to a console session. Returns { projectSlug, sessionId, via }
|
|
226
|
+
// or { refusal } (not-found, ambiguous or refused).
|
|
227
|
+
function resolveAddressTarget(req) {
|
|
228
|
+
var parsed = directory.parseAddress(req.to);
|
|
229
|
+
if (parsed.error) return { refusal: refused(parsed.error) };
|
|
230
|
+
if (req.projectSlug && req.projectSlug !== parsed.project) {
|
|
231
|
+
return { refusal: refused("project " + shown(req.projectSlug) + " does not match the address " + shown(req.to)) };
|
|
232
|
+
}
|
|
233
|
+
var resolved = resolveProject(parsed.project);
|
|
234
|
+
if (resolved.refusal) return { refusal: resolved.refusal };
|
|
235
|
+
var found = directory.resolve(entriesOf(parsed.project, resolved.project), parsed);
|
|
236
|
+
if (found.none) {
|
|
237
|
+
return { refusal: { ok: false, result: RESULT.NOT_FOUND, reason: "no session matches the address " + shown(req.to), to: req.to } };
|
|
238
|
+
}
|
|
239
|
+
if (found.candidates) {
|
|
240
|
+
return {
|
|
241
|
+
refusal: {
|
|
242
|
+
ok: false,
|
|
243
|
+
result: RESULT.AMBIGUOUS,
|
|
244
|
+
reason: "the address " + shown(req.to) + " matches " + found.candidates.length + " sessions; choose one and send again with --project and --session",
|
|
245
|
+
to: req.to,
|
|
246
|
+
candidates: found.candidates,
|
|
247
|
+
},
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
return { projectSlug: parsed.project, sessionId: found.match.consoleSessionId, via: "to" };
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Resolves --reply: the sender of the most recent injected message in the
|
|
254
|
+
// caller's own session. The caller is the session its environment references
|
|
255
|
+
// (lib/sender-ref.js), checked against the daemon's own store.
|
|
256
|
+
function resolveReplyTarget(req) {
|
|
257
|
+
if (!req.senderProject || !req.senderSessionId) {
|
|
258
|
+
return { refusal: refused("reply works only from inside a console session: no session reference was given (CLAGENTIC_CONSOLE_SESSION_ID is not set)") };
|
|
259
|
+
}
|
|
260
|
+
var ref = { project: req.senderProject, sessionId: req.senderSessionId };
|
|
261
|
+
var caller = senderRef.resolve(getProject, ref);
|
|
262
|
+
if (!caller) {
|
|
263
|
+
return { refusal: refused("reply refused: the calling session " + shown(req.senderProject) + "/" + shown(req.senderSessionId) + " is not a running session the console holds") };
|
|
264
|
+
}
|
|
265
|
+
var callerProject = getProject(req.senderProject);
|
|
266
|
+
var callerSession = null;
|
|
267
|
+
callerProject.sm.sessions.forEach(function (s) {
|
|
268
|
+
if (!callerSession && String(s.localId) === caller.sessionId) callerSession = s;
|
|
269
|
+
});
|
|
270
|
+
if (typeof callerProject.sm.loadSessionHistory === "function") callerProject.sm.loadSessionHistory(callerSession);
|
|
271
|
+
var last = directory.lastInjectedSender(callerSession.history);
|
|
272
|
+
// The in-memory window holds only the tail of a long history. A miss there
|
|
273
|
+
// falls through to a bounded reverse read of the stored file, which stops
|
|
274
|
+
// at the newest injected entry and touches no session state.
|
|
275
|
+
if (last.none) {
|
|
276
|
+
if (typeof callerProject.sm.findNewestHistoryEntry !== "function") {
|
|
277
|
+
console.error("[session-input] reply: the session manager has no stored-history reader; cannot look past the in-memory window");
|
|
278
|
+
return { refusal: refused("reply refused: the stored history of the calling session cannot be searched") };
|
|
279
|
+
}
|
|
280
|
+
var stored = null;
|
|
281
|
+
try {
|
|
282
|
+
stored = callerProject.sm.findNewestHistoryEntry(callerSession, isInjected);
|
|
283
|
+
} catch (e) {
|
|
284
|
+
console.error("[session-input] reply: stored history unreadable:", messageOf(e));
|
|
285
|
+
}
|
|
286
|
+
if (stored) last = directory.lastInjectedSender([stored]);
|
|
287
|
+
}
|
|
288
|
+
if (last.reason) return { refusal: refused(last.reason) };
|
|
289
|
+
|
|
290
|
+
// The sender is found the way --to finds a session: through the directory, by
|
|
291
|
+
// the recorded project, vendor and vendor session id together. The console id
|
|
292
|
+
// it matched is read now and never stored, because a console id can name
|
|
293
|
+
// another session after a restart.
|
|
294
|
+
var senderProject = getProject(last.project);
|
|
295
|
+
if (!senderProject || !senderProject.sm) {
|
|
296
|
+
return { refusal: { ok: false, result: RESULT.NOT_FOUND, reason: "reply refused: the sender's project " + shown(last.project) + " is not held by the console" } };
|
|
297
|
+
}
|
|
298
|
+
var found = directory.resolve(entriesOf(last.project, senderProject), { project: last.project, vendor: last.vendor, sessionId: last.cliSessionId });
|
|
299
|
+
if (found.none) {
|
|
300
|
+
return { refusal: { ok: false, result: RESULT.NOT_FOUND, reason: "reply refused: no session of the console holds the recorded sender (project " + shown(last.project) + ", vendor " + shown(last.vendor) + ", vendor session id " + shown(last.cliSessionId) + ")" } };
|
|
301
|
+
}
|
|
302
|
+
if (found.candidates) {
|
|
303
|
+
return {
|
|
304
|
+
refusal: {
|
|
305
|
+
ok: false,
|
|
306
|
+
result: RESULT.AMBIGUOUS,
|
|
307
|
+
reason: "reply refused: the recorded sender matches " + found.candidates.length + " sessions; choose one and send again with --project and --session",
|
|
308
|
+
candidates: found.candidates,
|
|
309
|
+
},
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
return { projectSlug: last.project, sessionId: found.match.consoleSessionId, via: "reply" };
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// A reply goes only to a session this daemon's OS user may address (the rule
|
|
316
|
+
// sender-ref applies to a caller). Returns a refusal or null.
|
|
317
|
+
function verifyReplyTarget(project, session, req) {
|
|
318
|
+
if (req.resolvedBy !== "reply") return null;
|
|
319
|
+
if (typeof project.getLinuxUserForSession === "function" && project.getLinuxUserForSession(session)) {
|
|
320
|
+
return refused("reply refused: the sender session " + shown(req.sessionId) + " runs as another OS user");
|
|
321
|
+
}
|
|
322
|
+
return null;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// Exactly one way of naming the target: sessionId (with projectSlug), to, or
|
|
326
|
+
// reply. Returns { projectSlug, sessionId, via } for to and reply, {} to leave
|
|
327
|
+
// the request as it is, or { refusal }.
|
|
328
|
+
function resolveSendTarget(req) {
|
|
329
|
+
var named = ["sessionId", "to", "reply"].filter(function (k) { return present(req[k]); });
|
|
330
|
+
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);
|
|
332
|
+
if (present(req.reply)) return resolveReplyTarget(req);
|
|
333
|
+
return {};
|
|
334
|
+
}
|
|
335
|
+
|
|
185
336
|
// History entry that marks the turn as injected, not typed. The input is
|
|
186
337
|
// already delivered when this runs, so a failure to record is logged and does
|
|
187
338
|
// not turn a delivered turn into an error a caller would retry.
|
|
188
|
-
function recordInjected(project, session, req, initiator) {
|
|
189
|
-
var entry
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
339
|
+
function recordInjected(project, session, req, initiator, messageId) {
|
|
340
|
+
var entry;
|
|
341
|
+
if (req.automation) {
|
|
342
|
+
// A turn the console composed itself (fireScheduled): the console is the
|
|
343
|
+
// sender, shown as the automation, never as the operator or a caller.
|
|
344
|
+
entry = consoleTurnEntry(initiator, req.prompt, req.automation.note);
|
|
345
|
+
entry.messageId = messageId;
|
|
346
|
+
} else {
|
|
347
|
+
entry = stampRecord({
|
|
348
|
+
type: "user_message",
|
|
349
|
+
text: req.prompt,
|
|
350
|
+
source: "external-trigger",
|
|
351
|
+
initiator: initiator,
|
|
352
|
+
messageId: messageId,
|
|
353
|
+
});
|
|
354
|
+
if (req.triggerId) entry.triggerId = req.triggerId;
|
|
355
|
+
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);
|
|
360
|
+
for (var k in sender) entry[k] = sender[k];
|
|
361
|
+
}
|
|
197
362
|
try {
|
|
198
363
|
project.sm.recordHistoryEntry(session, entry, true);
|
|
199
364
|
project.sm.appendToSessionFile(session, entry);
|
|
365
|
+
// Clients already viewing the session see it now, not after a reload.
|
|
366
|
+
if (typeof project.sm.sendToSession === "function") {
|
|
367
|
+
project.sm.sendToSession(session, forClient(entry));
|
|
368
|
+
}
|
|
200
369
|
} catch (e) {
|
|
201
370
|
console.error("[session-input] failed to record injected message:", e.message || e);
|
|
202
371
|
}
|
|
203
372
|
}
|
|
204
373
|
|
|
374
|
+
// The OS user a turn starts as. A console automation also grants that user
|
|
375
|
+
// access to the project, as the scheduled turn always did; the callers that
|
|
376
|
+
// act for someone else (send-input, a trigger) keep the lookup alone.
|
|
377
|
+
function startUserFor(project, session, req) {
|
|
378
|
+
if (req.automation && typeof project.ensureProjectAccessForSession === "function") {
|
|
379
|
+
return project.ensureProjectAccessForSession(session);
|
|
380
|
+
}
|
|
381
|
+
return project.getLinuxUserForSession(session);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// A turn the console started on its own is not one a client is waiting on, so
|
|
385
|
+
// the clients viewing the session are told it is running.
|
|
386
|
+
function announceStartedTurn(project, session) {
|
|
387
|
+
try {
|
|
388
|
+
project.sm.sendToSession(session, { type: "status", status: "processing" });
|
|
389
|
+
project.sm.broadcastSessionList();
|
|
390
|
+
} catch (e) {
|
|
391
|
+
console.error("[session-input] could not announce the started turn:", messageOf(e));
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
205
395
|
function describe(project, session) {
|
|
206
396
|
return {
|
|
207
397
|
consoleSessionId: String(session.localId),
|
|
@@ -209,6 +399,7 @@ function createSessionInput(deps) {
|
|
|
209
399
|
localId: session.localId,
|
|
210
400
|
vendor: session.vendor || project.sm.defaultVendor || null,
|
|
211
401
|
agent: session.agentName || null,
|
|
402
|
+
name: session.sessionName || null,
|
|
212
403
|
};
|
|
213
404
|
}
|
|
214
405
|
|
|
@@ -225,11 +416,22 @@ function createSessionInput(deps) {
|
|
|
225
416
|
if (refusal) return refusedForLoad(refusal);
|
|
226
417
|
}
|
|
227
418
|
|
|
419
|
+
// The same id goes to the vendor and into the history entry and sender record.
|
|
420
|
+
var messageId = newMessageId();
|
|
421
|
+
|
|
228
422
|
function delivered() {
|
|
229
|
-
recordInjected(project, session, req, initiator);
|
|
423
|
+
recordInjected(project, session, req, initiator, messageId);
|
|
424
|
+
if (req.automation && !processing) announceStartedTurn(project, session);
|
|
230
425
|
var out = describe(project, session);
|
|
231
426
|
out.ok = true;
|
|
232
427
|
out.result = (resumed || needsStart) ? RESULT.RESUMED : RESULT.DELIVERED;
|
|
428
|
+
// Said back so a caller that gave an address (or none, for a reply)
|
|
429
|
+
// learns which session took the input.
|
|
430
|
+
if (req.resolvedBy) {
|
|
431
|
+
out.resolvedBy = req.resolvedBy;
|
|
432
|
+
out.project = req.projectSlug;
|
|
433
|
+
out.label = senderRef.labelFor(out.agent, out.vendor, req.projectSlug);
|
|
434
|
+
}
|
|
233
435
|
return out;
|
|
234
436
|
}
|
|
235
437
|
|
|
@@ -239,7 +441,7 @@ function createSessionInput(deps) {
|
|
|
239
441
|
}
|
|
240
442
|
|
|
241
443
|
if (processing) {
|
|
242
|
-
try { sdk.pushMessage(session, req.prompt, null, initiator); }
|
|
444
|
+
try { sdk.pushMessage(session, req.prompt, null, initiator, messageId); }
|
|
243
445
|
catch (e) { return pushFailure(e); }
|
|
244
446
|
return delivered();
|
|
245
447
|
}
|
|
@@ -249,10 +451,10 @@ function createSessionInput(deps) {
|
|
|
249
451
|
session.sentToolResults = {};
|
|
250
452
|
if (needsStart) {
|
|
251
453
|
return startThen(project, session, "session " + String(session.cliSessionId), function () {
|
|
252
|
-
return sdk.startQuery(session, req.prompt, undefined, project
|
|
454
|
+
return sdk.startQuery(session, req.prompt, undefined, startUserFor(project, session, req), initiator, messageId);
|
|
253
455
|
}, delivered, sendStartFailure);
|
|
254
456
|
}
|
|
255
|
-
try { sdk.pushMessage(session, req.prompt, null, initiator); }
|
|
457
|
+
try { sdk.pushMessage(session, req.prompt, null, initiator, messageId); }
|
|
256
458
|
catch (e) {
|
|
257
459
|
session.isProcessing = false;
|
|
258
460
|
project.onProcessingChanged();
|
|
@@ -265,11 +467,25 @@ function createSessionInput(deps) {
|
|
|
265
467
|
// Returns the result, or a Promise of it when the session must be read from
|
|
266
468
|
// the store first (see settle()).
|
|
267
469
|
function sendInput(req) {
|
|
268
|
-
|
|
470
|
+
// A copy: resolving an address fills in the project and session id.
|
|
471
|
+
req = Object.assign({}, req);
|
|
472
|
+
// Set by fireScheduled and nowhere else, so a caller cannot make its input
|
|
473
|
+
// look like the console's own automation.
|
|
474
|
+
delete req.automation;
|
|
269
475
|
var init = resolveInitiator(req.initiator);
|
|
270
476
|
if (init.refusal) return init.refusal;
|
|
271
477
|
var promptRefusal = checkPrompt(req.prompt);
|
|
272
478
|
if (promptRefusal) return promptRefusal;
|
|
479
|
+
var fromRefusal = checkFrom(req.from) || checkFromSession(req.fromSession);
|
|
480
|
+
if (fromRefusal) return fromRefusal;
|
|
481
|
+
var target = resolveSendTarget(req);
|
|
482
|
+
if (target.refusal) return target.refusal;
|
|
483
|
+
// Set here and nowhere else, so a caller cannot supply it.
|
|
484
|
+
req.resolvedBy = target.via;
|
|
485
|
+
if (target.via) {
|
|
486
|
+
req.projectSlug = target.projectSlug;
|
|
487
|
+
req.sessionId = target.sessionId;
|
|
488
|
+
}
|
|
273
489
|
if (typeof req.sessionId !== "string" || !req.sessionId) {
|
|
274
490
|
return refused("sessionId must be a non-empty string");
|
|
275
491
|
}
|
|
@@ -282,8 +498,18 @@ function createSessionInput(deps) {
|
|
|
282
498
|
if (resolved.refusal) return resolved.refusal;
|
|
283
499
|
var project = resolved.project;
|
|
284
500
|
|
|
501
|
+
return withSession(project, req, function (session, resumed) {
|
|
502
|
+
return verifyReplyTarget(project, session, req) || deliver(project, session, req, init.initiator, resumed);
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// Runs act(session, resumed) on the session req.sessionId names: the one in
|
|
507
|
+
// memory, else the one the console's store holds, brought back on its own
|
|
508
|
+
// vendor and agent. Answers act's result, or a Promise of it when the store
|
|
509
|
+
// must be read; a session the store does not hold is not-found.
|
|
510
|
+
function withSession(project, req, act) {
|
|
285
511
|
var live = findSession(project.sm, req.sessionId);
|
|
286
|
-
if (live) return
|
|
512
|
+
if (live) return act(live, false);
|
|
287
513
|
|
|
288
514
|
// Only the console's own store is asked: a session started outside the
|
|
289
515
|
// console is not addressable, and its absence here is positive.
|
|
@@ -302,7 +528,7 @@ function createSessionInput(deps) {
|
|
|
302
528
|
if (!session) {
|
|
303
529
|
return { ok: false, result: RESULT.ERROR, retryable: false, reason: "session " + shown(req.sessionId) + " exists but could not be resumed" };
|
|
304
530
|
}
|
|
305
|
-
return
|
|
531
|
+
return act(session, true);
|
|
306
532
|
});
|
|
307
533
|
}).catch(function (err) {
|
|
308
534
|
// Lookup and resume failures carry whether trying again can help
|
|
@@ -312,6 +538,39 @@ function createSessionInput(deps) {
|
|
|
312
538
|
});
|
|
313
539
|
}
|
|
314
540
|
|
|
541
|
+
// The scheduled-input firing (lib/scheduled-input/service.js): one scheduled
|
|
542
|
+
// input delivered to the session its record names, through deliver(), the path
|
|
543
|
+
// send-input uses. Not an IPC verb and not reachable from a request body:
|
|
544
|
+
// only the service calls it.
|
|
545
|
+
//
|
|
546
|
+
// req: { projectSlug, sessionId, prompt, note?, initiator?, force? }
|
|
547
|
+
// initiator is scheduled (the default) or scheduled-now. A session that is
|
|
548
|
+
// mid-turn is skipped (result "busy", retryable) unless force is set, which
|
|
549
|
+
// is a person pressing "send now": the input then joins the running turn,
|
|
550
|
+
// as send-input does.
|
|
551
|
+
function fireScheduled(req) {
|
|
552
|
+
req = Object.assign({}, req);
|
|
553
|
+
if (req.initiator === undefined) req.initiator = INITIATORS.SCHEDULED;
|
|
554
|
+
if (req.initiator !== INITIATORS.SCHEDULED && req.initiator !== INITIATORS.SCHEDULED_NOW) {
|
|
555
|
+
return refused("a scheduled firing is " + INITIATORS.SCHEDULED + " or " + INITIATORS.SCHEDULED_NOW);
|
|
556
|
+
}
|
|
557
|
+
var promptRefusal = checkPrompt(req.prompt);
|
|
558
|
+
if (promptRefusal) return promptRefusal;
|
|
559
|
+
if (typeof req.sessionId !== "string" || !sessionResume.isSafeSessionId(req.sessionId)) {
|
|
560
|
+
return refused("unsafe session id " + shown(req.sessionId) + " refused");
|
|
561
|
+
}
|
|
562
|
+
var resolved = resolveProject(req.projectSlug);
|
|
563
|
+
if (resolved.refusal) return resolved.refusal;
|
|
564
|
+
var project = resolved.project;
|
|
565
|
+
req.automation = { note: req.note };
|
|
566
|
+
return withSession(project, req, function (session, resumed) {
|
|
567
|
+
if (session.isProcessing && !req.force) {
|
|
568
|
+
return { ok: false, result: RESULT.BUSY, retryable: true, reason: "the session is mid-turn", consoleSessionId: String(session.localId) };
|
|
569
|
+
}
|
|
570
|
+
return deliver(project, session, req, req.initiator, resumed);
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
|
|
315
574
|
// Checks an open request's session kind; returns { options } for
|
|
316
575
|
// sm.createSession (the options the browser's new_session passes) or { refusal }.
|
|
317
576
|
function resolveSessionKind(project, req) {
|
|
@@ -335,29 +594,116 @@ function createSessionInput(deps) {
|
|
|
335
594
|
}
|
|
336
595
|
options.sessionVisibility = req.sessionVisibility;
|
|
337
596
|
}
|
|
597
|
+
if (present(req.sessionName)) {
|
|
598
|
+
var named = directory.validateName(req.sessionName);
|
|
599
|
+
if (named.error) return { refusal: refused(named.error) };
|
|
600
|
+
var taken = nameTakenBy(project, named.name);
|
|
601
|
+
if (taken) {
|
|
602
|
+
return { refusal: refused("session name " + shown(named.name) + " is already in use by " + taken + " in this project; a name has one holder") };
|
|
603
|
+
}
|
|
604
|
+
options.sessionName = named.name;
|
|
605
|
+
}
|
|
338
606
|
return { options: options };
|
|
339
607
|
}
|
|
340
608
|
|
|
609
|
+
// The console id of a non-reaped session of the project holding the name, as
|
|
610
|
+
// text for a refusal, or null. Reaped sessions do not hold a name.
|
|
611
|
+
function nameTakenBy(project, name) {
|
|
612
|
+
var holder = findNameHolder(project.sm.sessions, name);
|
|
613
|
+
return holder ? "session " + String(holder.localId) : null;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
// The mode a session opened here starts in: the request's own permissionMode,
|
|
617
|
+
// else the server setting "Mode for sessions opened by the CLI or a trigger",
|
|
618
|
+
// else accept edits. Every value passes the shared mode check for the vendor
|
|
619
|
+
// the session will run on. A mode the request names that fails it refuses the
|
|
620
|
+
// open; a stored setting that fails it falls back to accept edits (logged).
|
|
621
|
+
// Returns { mode } or { refusal }.
|
|
622
|
+
function resolveOpenMode(project, req, options) {
|
|
623
|
+
if (req.permissionMode !== undefined && req.permissionMode !== null && typeof req.permissionMode !== "string") {
|
|
624
|
+
return { refusal: refused("permissionMode must be a string") };
|
|
625
|
+
}
|
|
626
|
+
var settings = deps.getPermissionSettings ? deps.getPermissionSettings() : null;
|
|
627
|
+
var vendorProbe = { vendor: options.vendor || null };
|
|
628
|
+
var chosen = permissionSettings.resolveOpenedSessionMode({
|
|
629
|
+
requested: req.permissionMode,
|
|
630
|
+
setting: settings && settings.openedSessionMode,
|
|
631
|
+
check: function (mode) {
|
|
632
|
+
// A project without the vendor-aware check (a bare double) still gets
|
|
633
|
+
// the mode and server-setting checks.
|
|
634
|
+
if (typeof project.checkPermissionModeFor === "function") return project.checkPermissionModeFor(vendorProbe, mode);
|
|
635
|
+
return checkPermissionMode(mode, {});
|
|
636
|
+
},
|
|
637
|
+
onFallback: function (why) {
|
|
638
|
+
console.warn("[session-input] stored opened-session mode " + logField(settings.openedSessionMode) + " refused (" + why + "); using " + permissionSettings.DEFAULT_OPENED_SESSION_MODE);
|
|
639
|
+
},
|
|
640
|
+
});
|
|
641
|
+
if (chosen.error) return { refusal: refused("permissionMode " + shown(req.permissionMode) + " refused: " + chosen.error) };
|
|
642
|
+
return { mode: chosen.mode };
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
// The token of an address, for a session to open: a named agent of the project's
|
|
646
|
+
// catalog, else a vendor (opening addresses no existing session, so there is
|
|
647
|
+
// nothing to be ambiguous). Returns { kind: { agentName } | { vendor } } or { refusal }.
|
|
648
|
+
function openKindFromToken(project, token) {
|
|
649
|
+
var agent = agentCatalog(project).filter(function (a) {
|
|
650
|
+
return a && slugifyAgentName(a.name) === token;
|
|
651
|
+
})[0];
|
|
652
|
+
if (agent) return { kind: { agentName: agent.name } };
|
|
653
|
+
if (isKnownVendor(token)) return { kind: { vendor: token } };
|
|
654
|
+
return { refusal: refused("unknown agent or vendor " + shown(token)) };
|
|
655
|
+
}
|
|
656
|
+
|
|
341
657
|
// req: { projectSlug, prompt, vendor?, agentName?, sessionVisibility?,
|
|
342
|
-
// initiator?, contextNote?, cwd?, triggerId? }
|
|
658
|
+
// permissionMode?, initiator?, contextNote?, cwd?, triggerId? }
|
|
343
659
|
// Returns the result, or a Promise of it when the first turn starts
|
|
344
660
|
// asynchronously (see settle()).
|
|
345
661
|
function openSession(req) {
|
|
346
|
-
req =
|
|
662
|
+
req = Object.assign({}, req);
|
|
663
|
+
delete req.automation;
|
|
347
664
|
var init = resolveInitiator(req.initiator);
|
|
348
665
|
if (init.refusal) return init.refusal;
|
|
349
666
|
var promptRefusal = checkPrompt(req.prompt);
|
|
350
667
|
if (promptRefusal) return promptRefusal;
|
|
668
|
+
var openFromRefusal = checkFrom(req.from) || checkFromSession(req.fromSession);
|
|
669
|
+
if (openFromRefusal) return openFromRefusal;
|
|
670
|
+
var parsed = null;
|
|
671
|
+
if (present(req.to)) {
|
|
672
|
+
parsed = directory.parseAddress(req.to);
|
|
673
|
+
if (parsed.error) return refused(parsed.error);
|
|
674
|
+
if (req.projectSlug && req.projectSlug !== parsed.project) {
|
|
675
|
+
return refused("project " + shown(req.projectSlug) + " does not match the address " + shown(req.to));
|
|
676
|
+
}
|
|
677
|
+
if (present(req.vendor) || present(req.agentName)) {
|
|
678
|
+
return refused("the address names the agent or vendor; do not also give vendor or agentName");
|
|
679
|
+
}
|
|
680
|
+
if (parsed.name !== undefined) {
|
|
681
|
+
if (present(req.sessionName)) return refused("the address names the session (/" + parsed.name + "); do not also give sessionName");
|
|
682
|
+
req.sessionName = parsed.name;
|
|
683
|
+
}
|
|
684
|
+
req.projectSlug = parsed.project;
|
|
685
|
+
}
|
|
351
686
|
var resolved = resolveProject(req.projectSlug);
|
|
352
687
|
if (resolved.refusal) return resolved.refusal;
|
|
353
688
|
var project = resolved.project;
|
|
689
|
+
if (parsed && parsed.token) {
|
|
690
|
+
var named = openKindFromToken(project, parsed.token);
|
|
691
|
+
if (named.refusal) return named.refusal;
|
|
692
|
+
req = Object.assign(req, named.kind);
|
|
693
|
+
}
|
|
354
694
|
var kind = resolveSessionKind(project, req);
|
|
355
695
|
if (kind.refusal) return kind.refusal;
|
|
696
|
+
var openMode = resolveOpenMode(project, req, kind.options);
|
|
697
|
+
if (openMode.refusal) return openMode.refusal;
|
|
356
698
|
var gate = getNewWorkRefusal();
|
|
357
699
|
if (gate) return refusedForLoad(gate);
|
|
358
700
|
|
|
359
701
|
var sm = project.sm;
|
|
360
|
-
|
|
702
|
+
// The opened mode is given at creation, so the session_switched that
|
|
703
|
+
// createSession emits already carries it (not the project default). It is
|
|
704
|
+
// saved with the session just below: a follow-up from a person, a resume
|
|
705
|
+
// and a restart all keep it.
|
|
706
|
+
var sess = sm.createSession(Object.assign({}, kind.options, { permissionMode: openMode.mode }));
|
|
361
707
|
var label = req.triggerId ? " — " + String(req.triggerId).substring(0, 12) : "";
|
|
362
708
|
sess.title = (req.contextNote || "External trigger") + label;
|
|
363
709
|
if (req.cwd) sess.cwd = req.cwd;
|
|
@@ -367,8 +713,6 @@ function createSessionInput(deps) {
|
|
|
367
713
|
sess.isProcessing = true;
|
|
368
714
|
project.onProcessingChanged();
|
|
369
715
|
sess.sentToolResults = {};
|
|
370
|
-
// The session stays open for follow-up from a person.
|
|
371
|
-
sess.acceptEditsAfterStart = true;
|
|
372
716
|
|
|
373
717
|
function opened() {
|
|
374
718
|
var out = describe(project, sess);
|
|
@@ -376,19 +720,48 @@ function createSessionInput(deps) {
|
|
|
376
720
|
out.result = RESULT.OPENED;
|
|
377
721
|
return out;
|
|
378
722
|
}
|
|
723
|
+
var openedMessageId = newMessageId();
|
|
379
724
|
return startThen(project, sess, "local session " + sess.localId, function () {
|
|
380
|
-
return project.sdk.startQuery(sess, req.prompt, undefined, project.getLinuxUserForSession(sess), init.initiator);
|
|
725
|
+
return project.sdk.startQuery(sess, req.prompt, undefined, project.getLinuxUserForSession(sess), init.initiator, openedMessageId);
|
|
381
726
|
}, function () {
|
|
382
727
|
// Recorded once, only now that the query has started: a rejected start
|
|
383
728
|
// leaves no history entry for a prompt that never ran.
|
|
384
|
-
recordInjected(project, sess, req, init.initiator);
|
|
729
|
+
recordInjected(project, sess, req, init.initiator, openedMessageId);
|
|
730
|
+
// A name has one holder: the claim took effect in memory at open (the new
|
|
731
|
+
// session is non-reaped and wins over its reaped twins meanwhile), and
|
|
732
|
+
// only now, with the start done, is it taken from the reaped sessions
|
|
733
|
+
// that still carry it. A failed open changes no other session.
|
|
734
|
+
if (kind.options.sessionName) {
|
|
735
|
+
releaseReapedHolders(sm.sessions, kind.options.sessionName, function (s) {
|
|
736
|
+
if (typeof sm.saveSessionFile === "function") sm.saveSessionFile(s);
|
|
737
|
+
}, sess.localId);
|
|
738
|
+
}
|
|
385
739
|
return opened();
|
|
386
740
|
}, openStartFailure);
|
|
387
741
|
}
|
|
388
742
|
|
|
743
|
+
// req: { projectSlug?, agentName?, vendor?, state? }. Read-only: the sessions
|
|
744
|
+
// of the console's own store (lib/session-directory.js), newest first.
|
|
745
|
+
function listSessions(req) {
|
|
746
|
+
req = req || {};
|
|
747
|
+
if (req.projectSlug && !getProject(req.projectSlug)) {
|
|
748
|
+
return refused("unknown project " + shown(req.projectSlug));
|
|
749
|
+
}
|
|
750
|
+
var found = directory.list(listProjects(), {
|
|
751
|
+
project: req.projectSlug,
|
|
752
|
+
agent: req.agentName,
|
|
753
|
+
vendor: req.vendor,
|
|
754
|
+
state: req.state,
|
|
755
|
+
});
|
|
756
|
+
if (found.error) return refused(found.error);
|
|
757
|
+
return { ok: true, result: RESULT.LISTED, count: found.entries.length, sessions: found.entries };
|
|
758
|
+
}
|
|
759
|
+
|
|
389
760
|
return {
|
|
390
761
|
openSession: logged("open_session", neverThrows(openSession)),
|
|
391
762
|
sendInput: logged("send_session_input", neverThrows(sendInput)),
|
|
763
|
+
listSessions: logged("list_sessions", neverThrows(listSessions)),
|
|
764
|
+
fireScheduled: logged("fire_scheduled_input", neverThrows(fireScheduled)),
|
|
392
765
|
};
|
|
393
766
|
}
|
|
394
767
|
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// session-name-holder.js - the one collision rule for a session name. A name
|
|
2
|
+
// has at most one holder in a project. A claim is refused while a non-reaped
|
|
3
|
+
// session (live, see sender-ref.isLive) holds it, and otherwise takes it from
|
|
4
|
+
// every reaped holder; a reaped session coming back yields to a current holder.
|
|
5
|
+
// Every path that gives a session a name asks here.
|
|
6
|
+
|
|
7
|
+
var senderRef = require("./sender-ref");
|
|
8
|
+
|
|
9
|
+
// The session of `sessions` (a Map or anything with forEach) that holds `name`,
|
|
10
|
+
// or null. A hidden session and the session `exceptLocalId` never hold it.
|
|
11
|
+
function findNameHolder(sessions, name, exceptLocalId) {
|
|
12
|
+
var holder = null;
|
|
13
|
+
sessions.forEach(function (s) {
|
|
14
|
+
if (holder || s.hidden || s.localId === exceptLocalId) return;
|
|
15
|
+
if (s.sessionName === name && (senderRef.isLive(s) || s._nameReserved)) holder = s;
|
|
16
|
+
});
|
|
17
|
+
return holder;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// A name has at most one holder in a project. A claim of `name` (the caller has
|
|
21
|
+
// already refused it when findNameHolder finds a non-reaped holder) takes it
|
|
22
|
+
// from every reaped session that still carries it: the name is cleared and the
|
|
23
|
+
// clear logged, and `persist(session)` is called so the stored meta follows.
|
|
24
|
+
// The claiming session `exceptLocalId` is never cleared. Call this only once
|
|
25
|
+
// the claim has succeeded: it changes other sessions durably. Returns the
|
|
26
|
+
// sessions it cleared.
|
|
27
|
+
function releaseReapedHolders(sessions, name, persist, exceptLocalId) {
|
|
28
|
+
var cleared = [];
|
|
29
|
+
sessions.forEach(function (s) {
|
|
30
|
+
if (s.localId === exceptLocalId) return;
|
|
31
|
+
if (s.sessionName !== name || senderRef.isLive(s) || s._nameReserved) return;
|
|
32
|
+
s.sessionName = null;
|
|
33
|
+
console.warn("[sessions] session " + s.localId + " is reaped: name " + JSON.stringify(name) +
|
|
34
|
+
" moved to a newly opened session");
|
|
35
|
+
cleared.push(s);
|
|
36
|
+
});
|
|
37
|
+
if (typeof persist === "function") cleared.forEach(persist);
|
|
38
|
+
return cleared;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// A reaped session gets a runtime again (a turn is about to start on it). If it
|
|
42
|
+
// holds a name that another non-reaped session now holds, it comes back
|
|
43
|
+
// unnamed: the name is cleared and the clear logged; the holder keeps the name.
|
|
44
|
+
// Returns the holder it yielded to, or null when nothing changed. The caller
|
|
45
|
+
// persists the session.
|
|
46
|
+
function yieldNameOnRevival(sessions, session) {
|
|
47
|
+
if (!session.sessionName) return null;
|
|
48
|
+
var holder = findNameHolder(sessions, session.sessionName, session.localId);
|
|
49
|
+
if (!holder) return null;
|
|
50
|
+
console.warn("[sessions] session " + session.localId + " restarted: name " + JSON.stringify(session.sessionName) +
|
|
51
|
+
" is held by session " + holder.localId + "; continuing unnamed");
|
|
52
|
+
session.sessionName = null;
|
|
53
|
+
return holder;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Revival in one synchronous step: the collision check and the mark that the
|
|
57
|
+
// session holds its name happen together, before any await, so a second
|
|
58
|
+
// same-named revival in the same tick finds the first as a holder. Returns
|
|
59
|
+
// { holder, release }. `holder` is who the session yielded to (or null);
|
|
60
|
+
// `release` drops the mark and is safe to call more than once. The caller calls
|
|
61
|
+
// it when the start ends: a session that started is held by being live, one
|
|
62
|
+
// that failed stays reaped and must not keep holding the name.
|
|
63
|
+
function reserveNameOnRevival(sessions, session) {
|
|
64
|
+
var holder = yieldNameOnRevival(sessions, session);
|
|
65
|
+
var owned = !!session.sessionName && !session._nameReserved;
|
|
66
|
+
if (owned) session._nameReserved = true;
|
|
67
|
+
return {
|
|
68
|
+
holder: holder,
|
|
69
|
+
release: function () {
|
|
70
|
+
if (!owned) return;
|
|
71
|
+
owned = false;
|
|
72
|
+
session._nameReserved = false;
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
module.exports = {
|
|
78
|
+
findNameHolder: findNameHolder,
|
|
79
|
+
releaseReapedHolders: releaseReapedHolders,
|
|
80
|
+
yieldNameOnRevival: yieldNameOnRevival,
|
|
81
|
+
reserveNameOnRevival: reserveNameOnRevival,
|
|
82
|
+
};
|