@clagentic/console 1.10.0 → 1.11.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -0
- package/bin/cli.js +15 -3
- 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/schedule-input-subcommands.js +327 -0
- package/lib/cli/session-subcommands.js +100 -17
- package/lib/client-build-id.js +56 -0
- package/lib/daemon.js +20 -5
- 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-summary.js +67 -0
- package/lib/project-connection.js +2 -1
- package/lib/project-external-trigger.js +9 -1
- package/lib/project-loop.js +26 -20
- package/lib/project-prompt-responses.js +20 -14
- package/lib/project-sessions.js +73 -21
- package/lib/project-user-message.js +18 -47
- package/lib/project.js +36 -62
- package/lib/public/app.js +3 -5
- 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/modules/app-connection.js +18 -1
- package/lib/public/modules/app-messages.js +19 -17
- package/lib/public/modules/app-panels.js +9 -2
- package/lib/public/modules/app-rate-limit.js +63 -87
- package/lib/public/modules/app-rendering.js +37 -6
- 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/mode-options.js +19 -16
- 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/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/sdk-bridge.js +137 -152
- 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 +28 -8
- package/lib/session-directory.js +224 -0
- package/lib/session-file-scan.js +55 -0
- package/lib/session-input-ipc.js +20 -0
- package/lib/session-input.js +355 -19
- package/lib/session-name-holder.js +82 -0
- package/lib/session-resume.js +1 -0
- package/lib/sessions.js +44 -0
- package/lib/turn-initiator.js +6 -2
- package/lib/ws-schema.js +15 -7
- package/lib/yoke/adapters/claude-worker.js +3 -1
- package/lib/yoke/adapters/claude.js +13 -7
- 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,28 @@ 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 { slugifyAgentName } = agentsModule;
|
|
21
26
|
|
|
22
27
|
// The result vocabulary reported to the caller. "error" is not a verdict on
|
|
23
28
|
// the request: delivery was attempted and failed, so a caller may retry.
|
|
29
|
+
// "ambiguous" is a clean answer, not a failure: an address named several
|
|
30
|
+
// sessions and the caller picks one by id (see resolveSendTarget).
|
|
31
|
+
// "busy" is only answered to a scheduled firing (fireScheduled): the session was
|
|
32
|
+
// mid-turn and the input was not delivered; the IPC verbs never answer it.
|
|
24
33
|
var RESULT = Object.freeze({
|
|
25
34
|
DELIVERED: "delivered",
|
|
26
35
|
RESUMED: "resumed-and-delivered",
|
|
27
36
|
OPENED: "opened",
|
|
37
|
+
LISTED: "listed",
|
|
28
38
|
NOT_FOUND: "not-found",
|
|
39
|
+
AMBIGUOUS: "ambiguous",
|
|
29
40
|
REFUSED: "refused",
|
|
30
41
|
ERROR: "error",
|
|
42
|
+
BUSY: "busy",
|
|
31
43
|
});
|
|
32
44
|
|
|
33
45
|
// Initiators injected input may name. HUMAN is deliberately absent.
|
|
@@ -136,6 +148,9 @@ function neverThrows(fn) {
|
|
|
136
148
|
function logResult(verb, req, out) {
|
|
137
149
|
req = req || {};
|
|
138
150
|
out = out || {};
|
|
151
|
+
// A scheduled firing skipped for a busy session retries every few seconds;
|
|
152
|
+
// one line per retry would bury the log.
|
|
153
|
+
if (out.result === RESULT.BUSY) return;
|
|
139
154
|
var line = "[session-input] " + verb + ": project=" + logField(req.projectSlug)
|
|
140
155
|
+ " session=" + logField(out.consoleSessionId || req.sessionId)
|
|
141
156
|
+ " result=" + logField(out.result);
|
|
@@ -156,11 +171,17 @@ function logged(verb, fn) {
|
|
|
156
171
|
};
|
|
157
172
|
}
|
|
158
173
|
|
|
174
|
+
function present(value) {
|
|
175
|
+
return value !== undefined && value !== null && value !== false;
|
|
176
|
+
}
|
|
177
|
+
|
|
159
178
|
// deps:
|
|
160
179
|
// getProject(slug) -> project context | null
|
|
180
|
+
// listProjects() -> [{ slug, project }] every project held (default: none)
|
|
161
181
|
// agentCatalog(project) -> [{ name }] named agents on offer (default: the picker's catalog)
|
|
162
182
|
function createSessionInput(deps) {
|
|
163
183
|
var getProject = deps.getProject;
|
|
184
|
+
var listProjects = deps.listProjects || function () { return []; };
|
|
164
185
|
|
|
165
186
|
function defaultAgentCatalog(project) {
|
|
166
187
|
// Neither lookup throws: both answer [] when discovery has not run or the
|
|
@@ -182,26 +203,191 @@ function createSessionInput(deps) {
|
|
|
182
203
|
return typeof prompt === "string" && prompt ? null : refused("prompt must be a non-empty string");
|
|
183
204
|
}
|
|
184
205
|
|
|
206
|
+
function checkFrom(from) {
|
|
207
|
+
var v = validateFrom(from);
|
|
208
|
+
return v.error ? refused(v.error) : null;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function checkFromSession(fromSession) {
|
|
212
|
+
var v = validateFromSession(fromSession);
|
|
213
|
+
return v.error ? refused(v.error) : null;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// The directory entries of one project.
|
|
217
|
+
function entriesOf(slug, project) {
|
|
218
|
+
return directory.collect([{ slug: slug, project: project }]);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Resolves --to to a console session. Returns { projectSlug, sessionId, via }
|
|
222
|
+
// or { refusal } (not-found, ambiguous or refused).
|
|
223
|
+
function resolveAddressTarget(req) {
|
|
224
|
+
var parsed = directory.parseAddress(req.to);
|
|
225
|
+
if (parsed.error) return { refusal: refused(parsed.error) };
|
|
226
|
+
if (req.projectSlug && req.projectSlug !== parsed.project) {
|
|
227
|
+
return { refusal: refused("project " + shown(req.projectSlug) + " does not match the address " + shown(req.to)) };
|
|
228
|
+
}
|
|
229
|
+
var resolved = resolveProject(parsed.project);
|
|
230
|
+
if (resolved.refusal) return { refusal: resolved.refusal };
|
|
231
|
+
var found = directory.resolve(entriesOf(parsed.project, resolved.project), parsed);
|
|
232
|
+
if (found.none) {
|
|
233
|
+
return { refusal: { ok: false, result: RESULT.NOT_FOUND, reason: "no session matches the address " + shown(req.to), to: req.to } };
|
|
234
|
+
}
|
|
235
|
+
if (found.candidates) {
|
|
236
|
+
return {
|
|
237
|
+
refusal: {
|
|
238
|
+
ok: false,
|
|
239
|
+
result: RESULT.AMBIGUOUS,
|
|
240
|
+
reason: "the address " + shown(req.to) + " matches " + found.candidates.length + " sessions; choose one and send again with --project and --session",
|
|
241
|
+
to: req.to,
|
|
242
|
+
candidates: found.candidates,
|
|
243
|
+
},
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
return { projectSlug: parsed.project, sessionId: found.match.consoleSessionId, via: "to" };
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// Resolves --reply: the sender of the most recent injected message in the
|
|
250
|
+
// caller's own session. The caller is the session its environment references
|
|
251
|
+
// (lib/sender-ref.js), checked against the daemon's own store.
|
|
252
|
+
function resolveReplyTarget(req) {
|
|
253
|
+
if (!req.senderProject || !req.senderSessionId) {
|
|
254
|
+
return { refusal: refused("reply works only from inside a console session: no session reference was given (CLAGENTIC_CONSOLE_SESSION_ID is not set)") };
|
|
255
|
+
}
|
|
256
|
+
var ref = { project: req.senderProject, sessionId: req.senderSessionId };
|
|
257
|
+
var caller = senderRef.resolve(getProject, ref);
|
|
258
|
+
if (!caller) {
|
|
259
|
+
return { refusal: refused("reply refused: the calling session " + shown(req.senderProject) + "/" + shown(req.senderSessionId) + " is not a running session the console holds") };
|
|
260
|
+
}
|
|
261
|
+
var callerProject = getProject(req.senderProject);
|
|
262
|
+
var callerSession = null;
|
|
263
|
+
callerProject.sm.sessions.forEach(function (s) {
|
|
264
|
+
if (!callerSession && String(s.localId) === caller.sessionId) callerSession = s;
|
|
265
|
+
});
|
|
266
|
+
if (typeof callerProject.sm.loadSessionHistory === "function") callerProject.sm.loadSessionHistory(callerSession);
|
|
267
|
+
var last = directory.lastInjectedSender(callerSession.history);
|
|
268
|
+
// The in-memory window holds only the tail of a long history. A miss there
|
|
269
|
+
// falls through to a bounded reverse read of the stored file, which stops
|
|
270
|
+
// at the newest injected entry and touches no session state.
|
|
271
|
+
if (last.none) {
|
|
272
|
+
if (typeof callerProject.sm.findNewestHistoryEntry !== "function") {
|
|
273
|
+
console.error("[session-input] reply: the session manager has no stored-history reader; cannot look past the in-memory window");
|
|
274
|
+
return { refusal: refused("reply refused: the stored history of the calling session cannot be searched") };
|
|
275
|
+
}
|
|
276
|
+
var stored = null;
|
|
277
|
+
try {
|
|
278
|
+
stored = callerProject.sm.findNewestHistoryEntry(callerSession, isInjected);
|
|
279
|
+
} catch (e) {
|
|
280
|
+
console.error("[session-input] reply: stored history unreadable:", messageOf(e));
|
|
281
|
+
}
|
|
282
|
+
if (stored) last = directory.lastInjectedSender([stored]);
|
|
283
|
+
}
|
|
284
|
+
if (last.reason) return { refusal: refused(last.reason) };
|
|
285
|
+
|
|
286
|
+
// The sender is found the way --to finds a session: through the directory, by
|
|
287
|
+
// the recorded project, vendor and vendor session id together. The console id
|
|
288
|
+
// it matched is read now and never stored, because a console id can name
|
|
289
|
+
// another session after a restart.
|
|
290
|
+
var senderProject = getProject(last.project);
|
|
291
|
+
if (!senderProject || !senderProject.sm) {
|
|
292
|
+
return { refusal: { ok: false, result: RESULT.NOT_FOUND, reason: "reply refused: the sender's project " + shown(last.project) + " is not held by the console" } };
|
|
293
|
+
}
|
|
294
|
+
var found = directory.resolve(entriesOf(last.project, senderProject), { project: last.project, vendor: last.vendor, sessionId: last.cliSessionId });
|
|
295
|
+
if (found.none) {
|
|
296
|
+
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) + ")" } };
|
|
297
|
+
}
|
|
298
|
+
if (found.candidates) {
|
|
299
|
+
return {
|
|
300
|
+
refusal: {
|
|
301
|
+
ok: false,
|
|
302
|
+
result: RESULT.AMBIGUOUS,
|
|
303
|
+
reason: "reply refused: the recorded sender matches " + found.candidates.length + " sessions; choose one and send again with --project and --session",
|
|
304
|
+
candidates: found.candidates,
|
|
305
|
+
},
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
return { projectSlug: last.project, sessionId: found.match.consoleSessionId, via: "reply" };
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// A reply goes only to a session this daemon's OS user may address (the rule
|
|
312
|
+
// sender-ref applies to a caller). Returns a refusal or null.
|
|
313
|
+
function verifyReplyTarget(project, session, req) {
|
|
314
|
+
if (req.resolvedBy !== "reply") return null;
|
|
315
|
+
if (typeof project.getLinuxUserForSession === "function" && project.getLinuxUserForSession(session)) {
|
|
316
|
+
return refused("reply refused: the sender session " + shown(req.sessionId) + " runs as another OS user");
|
|
317
|
+
}
|
|
318
|
+
return null;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// Exactly one way of naming the target: sessionId (with projectSlug), to, or
|
|
322
|
+
// reply. Returns { projectSlug, sessionId, via } for to and reply, {} to leave
|
|
323
|
+
// the request as it is, or { refusal }.
|
|
324
|
+
function resolveSendTarget(req) {
|
|
325
|
+
var named = ["sessionId", "to", "reply"].filter(function (k) { return present(req[k]); });
|
|
326
|
+
if (named.length > 1) return { refusal: refused("name the target one way only: sessionId, to or reply (given: " + named.join(", ") + ")") };
|
|
327
|
+
if (present(req.to)) return resolveAddressTarget(req);
|
|
328
|
+
if (present(req.reply)) return resolveReplyTarget(req);
|
|
329
|
+
return {};
|
|
330
|
+
}
|
|
331
|
+
|
|
185
332
|
// History entry that marks the turn as injected, not typed. The input is
|
|
186
333
|
// already delivered when this runs, so a failure to record is logged and does
|
|
187
334
|
// 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
|
-
|
|
335
|
+
function recordInjected(project, session, req, initiator, messageId) {
|
|
336
|
+
var entry;
|
|
337
|
+
if (req.automation) {
|
|
338
|
+
// A turn the console composed itself (fireScheduled): the console is the
|
|
339
|
+
// sender, shown as the automation, never as the operator or a caller.
|
|
340
|
+
entry = consoleTurnEntry(initiator, req.prompt, req.automation.note);
|
|
341
|
+
entry.messageId = messageId;
|
|
342
|
+
} else {
|
|
343
|
+
entry = stampRecord({
|
|
344
|
+
type: "user_message",
|
|
345
|
+
text: req.prompt,
|
|
346
|
+
source: "external-trigger",
|
|
347
|
+
initiator: initiator,
|
|
348
|
+
messageId: messageId,
|
|
349
|
+
});
|
|
350
|
+
if (req.triggerId) entry.triggerId = req.triggerId;
|
|
351
|
+
if (req.contextNote) entry.contextNote = req.contextNote;
|
|
352
|
+
// The reference a console-started caller holds, resolved in the daemon's own
|
|
353
|
+
// store; one it does not hold falls through to declared or undeclared.
|
|
354
|
+
var extracted = senderRef.resolve(getProject, { project: req.senderProject, sessionId: req.senderSessionId });
|
|
355
|
+
var sender = senderFields(initiator, validateFrom(req.from).from, extracted, validateFromSession(req.fromSession).fromSession);
|
|
356
|
+
for (var k in sender) entry[k] = sender[k];
|
|
357
|
+
}
|
|
197
358
|
try {
|
|
198
359
|
project.sm.recordHistoryEntry(session, entry, true);
|
|
199
360
|
project.sm.appendToSessionFile(session, entry);
|
|
361
|
+
// Clients already viewing the session see it now, not after a reload.
|
|
362
|
+
if (typeof project.sm.sendToSession === "function") {
|
|
363
|
+
project.sm.sendToSession(session, forClient(entry));
|
|
364
|
+
}
|
|
200
365
|
} catch (e) {
|
|
201
366
|
console.error("[session-input] failed to record injected message:", e.message || e);
|
|
202
367
|
}
|
|
203
368
|
}
|
|
204
369
|
|
|
370
|
+
// The OS user a turn starts as. A console automation also grants that user
|
|
371
|
+
// access to the project, as the scheduled turn always did; the callers that
|
|
372
|
+
// act for someone else (send-input, a trigger) keep the lookup alone.
|
|
373
|
+
function startUserFor(project, session, req) {
|
|
374
|
+
if (req.automation && typeof project.ensureProjectAccessForSession === "function") {
|
|
375
|
+
return project.ensureProjectAccessForSession(session);
|
|
376
|
+
}
|
|
377
|
+
return project.getLinuxUserForSession(session);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// A turn the console started on its own is not one a client is waiting on, so
|
|
381
|
+
// the clients viewing the session are told it is running.
|
|
382
|
+
function announceStartedTurn(project, session) {
|
|
383
|
+
try {
|
|
384
|
+
project.sm.sendToSession(session, { type: "status", status: "processing" });
|
|
385
|
+
project.sm.broadcastSessionList();
|
|
386
|
+
} catch (e) {
|
|
387
|
+
console.error("[session-input] could not announce the started turn:", messageOf(e));
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
205
391
|
function describe(project, session) {
|
|
206
392
|
return {
|
|
207
393
|
consoleSessionId: String(session.localId),
|
|
@@ -209,6 +395,7 @@ function createSessionInput(deps) {
|
|
|
209
395
|
localId: session.localId,
|
|
210
396
|
vendor: session.vendor || project.sm.defaultVendor || null,
|
|
211
397
|
agent: session.agentName || null,
|
|
398
|
+
name: session.sessionName || null,
|
|
212
399
|
};
|
|
213
400
|
}
|
|
214
401
|
|
|
@@ -225,11 +412,22 @@ function createSessionInput(deps) {
|
|
|
225
412
|
if (refusal) return refusedForLoad(refusal);
|
|
226
413
|
}
|
|
227
414
|
|
|
415
|
+
// The same id goes to the vendor and into the history entry and sender record.
|
|
416
|
+
var messageId = newMessageId();
|
|
417
|
+
|
|
228
418
|
function delivered() {
|
|
229
|
-
recordInjected(project, session, req, initiator);
|
|
419
|
+
recordInjected(project, session, req, initiator, messageId);
|
|
420
|
+
if (req.automation && !processing) announceStartedTurn(project, session);
|
|
230
421
|
var out = describe(project, session);
|
|
231
422
|
out.ok = true;
|
|
232
423
|
out.result = (resumed || needsStart) ? RESULT.RESUMED : RESULT.DELIVERED;
|
|
424
|
+
// Said back so a caller that gave an address (or none, for a reply)
|
|
425
|
+
// learns which session took the input.
|
|
426
|
+
if (req.resolvedBy) {
|
|
427
|
+
out.resolvedBy = req.resolvedBy;
|
|
428
|
+
out.project = req.projectSlug;
|
|
429
|
+
out.label = senderRef.labelFor(out.agent, out.vendor, req.projectSlug);
|
|
430
|
+
}
|
|
233
431
|
return out;
|
|
234
432
|
}
|
|
235
433
|
|
|
@@ -239,7 +437,7 @@ function createSessionInput(deps) {
|
|
|
239
437
|
}
|
|
240
438
|
|
|
241
439
|
if (processing) {
|
|
242
|
-
try { sdk.pushMessage(session, req.prompt, null, initiator); }
|
|
440
|
+
try { sdk.pushMessage(session, req.prompt, null, initiator, messageId); }
|
|
243
441
|
catch (e) { return pushFailure(e); }
|
|
244
442
|
return delivered();
|
|
245
443
|
}
|
|
@@ -249,10 +447,10 @@ function createSessionInput(deps) {
|
|
|
249
447
|
session.sentToolResults = {};
|
|
250
448
|
if (needsStart) {
|
|
251
449
|
return startThen(project, session, "session " + String(session.cliSessionId), function () {
|
|
252
|
-
return sdk.startQuery(session, req.prompt, undefined, project
|
|
450
|
+
return sdk.startQuery(session, req.prompt, undefined, startUserFor(project, session, req), initiator, messageId);
|
|
253
451
|
}, delivered, sendStartFailure);
|
|
254
452
|
}
|
|
255
|
-
try { sdk.pushMessage(session, req.prompt, null, initiator); }
|
|
453
|
+
try { sdk.pushMessage(session, req.prompt, null, initiator, messageId); }
|
|
256
454
|
catch (e) {
|
|
257
455
|
session.isProcessing = false;
|
|
258
456
|
project.onProcessingChanged();
|
|
@@ -265,11 +463,25 @@ function createSessionInput(deps) {
|
|
|
265
463
|
// Returns the result, or a Promise of it when the session must be read from
|
|
266
464
|
// the store first (see settle()).
|
|
267
465
|
function sendInput(req) {
|
|
268
|
-
|
|
466
|
+
// A copy: resolving an address fills in the project and session id.
|
|
467
|
+
req = Object.assign({}, req);
|
|
468
|
+
// Set by fireScheduled and nowhere else, so a caller cannot make its input
|
|
469
|
+
// look like the console's own automation.
|
|
470
|
+
delete req.automation;
|
|
269
471
|
var init = resolveInitiator(req.initiator);
|
|
270
472
|
if (init.refusal) return init.refusal;
|
|
271
473
|
var promptRefusal = checkPrompt(req.prompt);
|
|
272
474
|
if (promptRefusal) return promptRefusal;
|
|
475
|
+
var fromRefusal = checkFrom(req.from) || checkFromSession(req.fromSession);
|
|
476
|
+
if (fromRefusal) return fromRefusal;
|
|
477
|
+
var target = resolveSendTarget(req);
|
|
478
|
+
if (target.refusal) return target.refusal;
|
|
479
|
+
// Set here and nowhere else, so a caller cannot supply it.
|
|
480
|
+
req.resolvedBy = target.via;
|
|
481
|
+
if (target.via) {
|
|
482
|
+
req.projectSlug = target.projectSlug;
|
|
483
|
+
req.sessionId = target.sessionId;
|
|
484
|
+
}
|
|
273
485
|
if (typeof req.sessionId !== "string" || !req.sessionId) {
|
|
274
486
|
return refused("sessionId must be a non-empty string");
|
|
275
487
|
}
|
|
@@ -282,8 +494,18 @@ function createSessionInput(deps) {
|
|
|
282
494
|
if (resolved.refusal) return resolved.refusal;
|
|
283
495
|
var project = resolved.project;
|
|
284
496
|
|
|
497
|
+
return withSession(project, req, function (session, resumed) {
|
|
498
|
+
return verifyReplyTarget(project, session, req) || deliver(project, session, req, init.initiator, resumed);
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// Runs act(session, resumed) on the session req.sessionId names: the one in
|
|
503
|
+
// memory, else the one the console's store holds, brought back on its own
|
|
504
|
+
// vendor and agent. Answers act's result, or a Promise of it when the store
|
|
505
|
+
// must be read; a session the store does not hold is not-found.
|
|
506
|
+
function withSession(project, req, act) {
|
|
285
507
|
var live = findSession(project.sm, req.sessionId);
|
|
286
|
-
if (live) return
|
|
508
|
+
if (live) return act(live, false);
|
|
287
509
|
|
|
288
510
|
// Only the console's own store is asked: a session started outside the
|
|
289
511
|
// console is not addressable, and its absence here is positive.
|
|
@@ -302,7 +524,7 @@ function createSessionInput(deps) {
|
|
|
302
524
|
if (!session) {
|
|
303
525
|
return { ok: false, result: RESULT.ERROR, retryable: false, reason: "session " + shown(req.sessionId) + " exists but could not be resumed" };
|
|
304
526
|
}
|
|
305
|
-
return
|
|
527
|
+
return act(session, true);
|
|
306
528
|
});
|
|
307
529
|
}).catch(function (err) {
|
|
308
530
|
// Lookup and resume failures carry whether trying again can help
|
|
@@ -312,6 +534,39 @@ function createSessionInput(deps) {
|
|
|
312
534
|
});
|
|
313
535
|
}
|
|
314
536
|
|
|
537
|
+
// The scheduled-input firing (lib/scheduled-input/service.js): one scheduled
|
|
538
|
+
// input delivered to the session its record names, through deliver(), the path
|
|
539
|
+
// send-input uses. Not an IPC verb and not reachable from a request body:
|
|
540
|
+
// only the service calls it.
|
|
541
|
+
//
|
|
542
|
+
// req: { projectSlug, sessionId, prompt, note?, initiator?, force? }
|
|
543
|
+
// initiator is scheduled (the default) or scheduled-now. A session that is
|
|
544
|
+
// mid-turn is skipped (result "busy", retryable) unless force is set, which
|
|
545
|
+
// is a person pressing "send now": the input then joins the running turn,
|
|
546
|
+
// as send-input does.
|
|
547
|
+
function fireScheduled(req) {
|
|
548
|
+
req = Object.assign({}, req);
|
|
549
|
+
if (req.initiator === undefined) req.initiator = INITIATORS.SCHEDULED;
|
|
550
|
+
if (req.initiator !== INITIATORS.SCHEDULED && req.initiator !== INITIATORS.SCHEDULED_NOW) {
|
|
551
|
+
return refused("a scheduled firing is " + INITIATORS.SCHEDULED + " or " + INITIATORS.SCHEDULED_NOW);
|
|
552
|
+
}
|
|
553
|
+
var promptRefusal = checkPrompt(req.prompt);
|
|
554
|
+
if (promptRefusal) return promptRefusal;
|
|
555
|
+
if (typeof req.sessionId !== "string" || !sessionResume.isSafeSessionId(req.sessionId)) {
|
|
556
|
+
return refused("unsafe session id " + shown(req.sessionId) + " refused");
|
|
557
|
+
}
|
|
558
|
+
var resolved = resolveProject(req.projectSlug);
|
|
559
|
+
if (resolved.refusal) return resolved.refusal;
|
|
560
|
+
var project = resolved.project;
|
|
561
|
+
req.automation = { note: req.note };
|
|
562
|
+
return withSession(project, req, function (session, resumed) {
|
|
563
|
+
if (session.isProcessing && !req.force) {
|
|
564
|
+
return { ok: false, result: RESULT.BUSY, retryable: true, reason: "the session is mid-turn", consoleSessionId: String(session.localId) };
|
|
565
|
+
}
|
|
566
|
+
return deliver(project, session, req, req.initiator, resumed);
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
|
|
315
570
|
// Checks an open request's session kind; returns { options } for
|
|
316
571
|
// sm.createSession (the options the browser's new_session passes) or { refusal }.
|
|
317
572
|
function resolveSessionKind(project, req) {
|
|
@@ -335,22 +590,74 @@ function createSessionInput(deps) {
|
|
|
335
590
|
}
|
|
336
591
|
options.sessionVisibility = req.sessionVisibility;
|
|
337
592
|
}
|
|
593
|
+
if (present(req.sessionName)) {
|
|
594
|
+
var named = directory.validateName(req.sessionName);
|
|
595
|
+
if (named.error) return { refusal: refused(named.error) };
|
|
596
|
+
var taken = nameTakenBy(project, named.name);
|
|
597
|
+
if (taken) {
|
|
598
|
+
return { refusal: refused("session name " + shown(named.name) + " is already in use by " + taken + " in this project; a name has one holder") };
|
|
599
|
+
}
|
|
600
|
+
options.sessionName = named.name;
|
|
601
|
+
}
|
|
338
602
|
return { options: options };
|
|
339
603
|
}
|
|
340
604
|
|
|
605
|
+
// The console id of a non-reaped session of the project holding the name, as
|
|
606
|
+
// text for a refusal, or null. Reaped sessions do not hold a name.
|
|
607
|
+
function nameTakenBy(project, name) {
|
|
608
|
+
var holder = findNameHolder(project.sm.sessions, name);
|
|
609
|
+
return holder ? "session " + String(holder.localId) : null;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
// The token of an address, for a session to open: a named agent of the project's
|
|
613
|
+
// catalog, else a vendor (opening addresses no existing session, so there is
|
|
614
|
+
// nothing to be ambiguous). Returns { kind: { agentName } | { vendor } } or { refusal }.
|
|
615
|
+
function openKindFromToken(project, token) {
|
|
616
|
+
var agent = agentCatalog(project).filter(function (a) {
|
|
617
|
+
return a && slugifyAgentName(a.name) === token;
|
|
618
|
+
})[0];
|
|
619
|
+
if (agent) return { kind: { agentName: agent.name } };
|
|
620
|
+
if (isKnownVendor(token)) return { kind: { vendor: token } };
|
|
621
|
+
return { refusal: refused("unknown agent or vendor " + shown(token)) };
|
|
622
|
+
}
|
|
623
|
+
|
|
341
624
|
// req: { projectSlug, prompt, vendor?, agentName?, sessionVisibility?,
|
|
342
625
|
// initiator?, contextNote?, cwd?, triggerId? }
|
|
343
626
|
// Returns the result, or a Promise of it when the first turn starts
|
|
344
627
|
// asynchronously (see settle()).
|
|
345
628
|
function openSession(req) {
|
|
346
|
-
req =
|
|
629
|
+
req = Object.assign({}, req);
|
|
630
|
+
delete req.automation;
|
|
347
631
|
var init = resolveInitiator(req.initiator);
|
|
348
632
|
if (init.refusal) return init.refusal;
|
|
349
633
|
var promptRefusal = checkPrompt(req.prompt);
|
|
350
634
|
if (promptRefusal) return promptRefusal;
|
|
635
|
+
var openFromRefusal = checkFrom(req.from) || checkFromSession(req.fromSession);
|
|
636
|
+
if (openFromRefusal) return openFromRefusal;
|
|
637
|
+
var parsed = null;
|
|
638
|
+
if (present(req.to)) {
|
|
639
|
+
parsed = directory.parseAddress(req.to);
|
|
640
|
+
if (parsed.error) return refused(parsed.error);
|
|
641
|
+
if (req.projectSlug && req.projectSlug !== parsed.project) {
|
|
642
|
+
return refused("project " + shown(req.projectSlug) + " does not match the address " + shown(req.to));
|
|
643
|
+
}
|
|
644
|
+
if (present(req.vendor) || present(req.agentName)) {
|
|
645
|
+
return refused("the address names the agent or vendor; do not also give vendor or agentName");
|
|
646
|
+
}
|
|
647
|
+
if (parsed.name !== undefined) {
|
|
648
|
+
if (present(req.sessionName)) return refused("the address names the session (/" + parsed.name + "); do not also give sessionName");
|
|
649
|
+
req.sessionName = parsed.name;
|
|
650
|
+
}
|
|
651
|
+
req.projectSlug = parsed.project;
|
|
652
|
+
}
|
|
351
653
|
var resolved = resolveProject(req.projectSlug);
|
|
352
654
|
if (resolved.refusal) return resolved.refusal;
|
|
353
655
|
var project = resolved.project;
|
|
656
|
+
if (parsed && parsed.token) {
|
|
657
|
+
var named = openKindFromToken(project, parsed.token);
|
|
658
|
+
if (named.refusal) return named.refusal;
|
|
659
|
+
req = Object.assign(req, named.kind);
|
|
660
|
+
}
|
|
354
661
|
var kind = resolveSessionKind(project, req);
|
|
355
662
|
if (kind.refusal) return kind.refusal;
|
|
356
663
|
var gate = getNewWorkRefusal();
|
|
@@ -376,19 +683,48 @@ function createSessionInput(deps) {
|
|
|
376
683
|
out.result = RESULT.OPENED;
|
|
377
684
|
return out;
|
|
378
685
|
}
|
|
686
|
+
var openedMessageId = newMessageId();
|
|
379
687
|
return startThen(project, sess, "local session " + sess.localId, function () {
|
|
380
|
-
return project.sdk.startQuery(sess, req.prompt, undefined, project.getLinuxUserForSession(sess), init.initiator);
|
|
688
|
+
return project.sdk.startQuery(sess, req.prompt, undefined, project.getLinuxUserForSession(sess), init.initiator, openedMessageId);
|
|
381
689
|
}, function () {
|
|
382
690
|
// Recorded once, only now that the query has started: a rejected start
|
|
383
691
|
// leaves no history entry for a prompt that never ran.
|
|
384
|
-
recordInjected(project, sess, req, init.initiator);
|
|
692
|
+
recordInjected(project, sess, req, init.initiator, openedMessageId);
|
|
693
|
+
// A name has one holder: the claim took effect in memory at open (the new
|
|
694
|
+
// session is non-reaped and wins over its reaped twins meanwhile), and
|
|
695
|
+
// only now, with the start done, is it taken from the reaped sessions
|
|
696
|
+
// that still carry it. A failed open changes no other session.
|
|
697
|
+
if (kind.options.sessionName) {
|
|
698
|
+
releaseReapedHolders(sm.sessions, kind.options.sessionName, function (s) {
|
|
699
|
+
if (typeof sm.saveSessionFile === "function") sm.saveSessionFile(s);
|
|
700
|
+
}, sess.localId);
|
|
701
|
+
}
|
|
385
702
|
return opened();
|
|
386
703
|
}, openStartFailure);
|
|
387
704
|
}
|
|
388
705
|
|
|
706
|
+
// req: { projectSlug?, agentName?, vendor?, state? }. Read-only: the sessions
|
|
707
|
+
// of the console's own store (lib/session-directory.js), newest first.
|
|
708
|
+
function listSessions(req) {
|
|
709
|
+
req = req || {};
|
|
710
|
+
if (req.projectSlug && !getProject(req.projectSlug)) {
|
|
711
|
+
return refused("unknown project " + shown(req.projectSlug));
|
|
712
|
+
}
|
|
713
|
+
var found = directory.list(listProjects(), {
|
|
714
|
+
project: req.projectSlug,
|
|
715
|
+
agent: req.agentName,
|
|
716
|
+
vendor: req.vendor,
|
|
717
|
+
state: req.state,
|
|
718
|
+
});
|
|
719
|
+
if (found.error) return refused(found.error);
|
|
720
|
+
return { ok: true, result: RESULT.LISTED, count: found.entries.length, sessions: found.entries };
|
|
721
|
+
}
|
|
722
|
+
|
|
389
723
|
return {
|
|
390
724
|
openSession: logged("open_session", neverThrows(openSession)),
|
|
391
725
|
sendInput: logged("send_session_input", neverThrows(sendInput)),
|
|
726
|
+
listSessions: logged("list_sessions", neverThrows(listSessions)),
|
|
727
|
+
fireScheduled: logged("fire_scheduled_input", neverThrows(fireScheduled)),
|
|
392
728
|
};
|
|
393
729
|
}
|
|
394
730
|
|
|
@@ -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
|
+
};
|
package/lib/session-resume.js
CHANGED
|
@@ -124,6 +124,7 @@ function resumeStoredSession(deps, cliSessionId, opts) {
|
|
|
124
124
|
if (meta) {
|
|
125
125
|
if (meta.vendor) resumeOpts.vendor = meta.vendor;
|
|
126
126
|
if (meta.agentName) resumeOpts.agentName = meta.agentName;
|
|
127
|
+
if (meta.sessionName) resumeOpts.sessionName = meta.sessionName;
|
|
127
128
|
if (meta.ownerId) resumeOpts.ownerId = meta.ownerId;
|
|
128
129
|
if (meta.sessionVisibility) resumeOpts.sessionVisibility = meta.sessionVisibility;
|
|
129
130
|
// Sanitized so a crafted meta record cannot auto-approve a tool.
|