@clagentic/console 1.10.0-beta.1 → 1.10.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 +1 -1
- package/bin/cli.js +10 -0
- package/lib/agent-dispatch-tool.js +20 -0
- package/lib/build-changes.js +79 -0
- package/lib/build-identity.js +192 -0
- package/lib/build-monitor.js +125 -0
- package/lib/build-sha.json +2 -2
- package/lib/build-update-offer.js +141 -0
- package/lib/build-update-restart.js +142 -0
- package/lib/build-update-standing.js +184 -0
- package/lib/cli/daemon-launch.js +8 -5
- package/lib/cli/ipc-subcommands.js +6 -0
- package/lib/cli/session-subcommands.js +122 -0
- package/lib/cli-sessions.js +8 -1
- package/lib/codex-defaults.js +8 -2
- package/lib/daemon-projects.js +19 -0
- package/lib/daemon.js +166 -127
- package/lib/memory-shed.js +13 -32
- package/lib/project-connection.js +14 -0
- package/lib/project-external-trigger.js +91 -123
- package/lib/project-loop.js +5 -4
- package/lib/project-prompt-responses.js +5 -4
- package/lib/project-sessions.js +43 -68
- package/lib/project-user-message.js +7 -20
- package/lib/project.js +23 -5
- package/lib/prompt-registry.js +7 -0
- package/lib/public/css/diagnostics.css +30 -0
- package/lib/public/modules/agent-dispatch-tool.js +9 -0
- package/lib/public/modules/app-messages.js +7 -2
- package/lib/public/modules/app-panels.js +0 -1
- package/lib/public/modules/build-update-detail.js +54 -0
- package/lib/public/modules/diagnostics.js +126 -20
- package/lib/public/modules/tools.js +14 -2
- package/lib/public/modules/vendor-picker.js +2 -2
- package/lib/public/modules/vendor-registry.js +11 -2
- package/lib/sdk-bridge.js +62 -15
- package/lib/sdk-message-processor.js +11 -20
- package/lib/sdk-skill-discovery.js +65 -60
- package/lib/server.js +15 -2
- package/lib/session-id.js +33 -0
- package/lib/session-input-ipc.js +32 -0
- package/lib/session-input.js +379 -0
- package/lib/session-reclaim.js +284 -0
- package/lib/session-resume.js +172 -0
- package/lib/sessions.js +16 -2
- package/lib/supervisor.js +39 -0
- package/lib/turn-initiator.js +108 -0
- package/lib/vendor-registry-message.js +1 -0
- package/lib/ws-schema.js +3 -1
- package/lib/yoke/adapters/claude-vendor.js +0 -1
- package/lib/yoke/adapters/claude-worker.js +6 -4
- package/lib/yoke/adapters/claude.js +34 -34
- package/lib/yoke/adapters/codex-capabilities.js +9 -6
- package/lib/yoke/adapters/codex-vendor.js +15 -15
- package/lib/yoke/adapters/codex.js +121 -61
- package/lib/yoke/auth-checks.js +8 -21
- package/lib/yoke/codex-app-server.js +27 -71
- package/lib/yoke/codex-binary.js +254 -0
- package/lib/yoke/codex-command.js +122 -0
- package/lib/yoke/codex-protocol.js +96 -0
- package/lib/yoke/codex-version.js +135 -0
- package/lib/yoke/index.js +20 -4
- package/lib/yoke/install-probe.js +21 -6
- package/lib/yoke/interface.js +11 -5
- package/lib/yoke/registry.js +41 -4
- package/package.json +1 -2
- package/scripts/verify-installed-build.js +59 -17
- package/lib/lore-attestation.js +0 -73
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
// session-input.js - open a session, or send input to one, on behalf of a
|
|
2
|
+
// caller that is not the authenticated browser: an external-trigger file
|
|
3
|
+
// (project-external-trigger.js) or the daemon IPC verbs (lib/daemon.js).
|
|
4
|
+
// One implementation behind both, so the same request gets the same result.
|
|
5
|
+
//
|
|
6
|
+
// Any caller, any session kind: the vendor and agent come from the request on
|
|
7
|
+
// open, and from the stored session on send. Input never counts as a human
|
|
8
|
+
// turn: an initiator of "human" (or anything unknown) is refused, and an absent
|
|
9
|
+
// one is "trigger". Human turns come only from the browser's WebSocket path.
|
|
10
|
+
//
|
|
11
|
+
// Security is unchanged from the external trigger: the callers that reach this
|
|
12
|
+
// are the OS user that owns the config directory (file drop; daemon.sock is
|
|
13
|
+
// chmod 0700). Routing per user in multi-user mode remains future work.
|
|
14
|
+
|
|
15
|
+
var { INITIATORS } = require("./turn-initiator");
|
|
16
|
+
var { isKnownVendor } = require("./yoke");
|
|
17
|
+
var { getNewWorkRefusal } = require("./new-work-gate");
|
|
18
|
+
var agentsModule = require("./agents");
|
|
19
|
+
var sessionResume = require("./session-resume");
|
|
20
|
+
|
|
21
|
+
// The result vocabulary reported to the caller. "error" is not a verdict on
|
|
22
|
+
// the request: delivery was attempted and failed, so a caller may retry.
|
|
23
|
+
var RESULT = Object.freeze({
|
|
24
|
+
DELIVERED: "delivered",
|
|
25
|
+
RESUMED: "resumed-and-delivered",
|
|
26
|
+
OPENED: "opened",
|
|
27
|
+
NOT_FOUND: "not-found",
|
|
28
|
+
REFUSED: "refused",
|
|
29
|
+
ERROR: "error",
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// Initiators injected input may name. HUMAN is deliberately absent.
|
|
33
|
+
var ACCEPTED_INITIATORS = Object.freeze([
|
|
34
|
+
INITIATORS.TRIGGER,
|
|
35
|
+
INITIATORS.SCHEDULED,
|
|
36
|
+
INITIATORS.SCHEDULED_NOW,
|
|
37
|
+
INITIATORS.LOOP,
|
|
38
|
+
]);
|
|
39
|
+
|
|
40
|
+
var VISIBILITIES = Object.freeze(["shared", "private"]);
|
|
41
|
+
|
|
42
|
+
function shown(value) {
|
|
43
|
+
return JSON.stringify(String(value).slice(0, 64));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function refused(reason) {
|
|
47
|
+
return { ok: false, result: RESULT.REFUSED, reason: reason };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// The daemon is shedding new work (memory pressure). Not a verdict on the
|
|
51
|
+
// request, so a file-drop caller keeps its trigger and tries again later.
|
|
52
|
+
function refusedForLoad(reason) {
|
|
53
|
+
return { ok: false, result: RESULT.REFUSED, reason: reason, retryable: true };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Returns { initiator } or { refusal }.
|
|
57
|
+
function resolveInitiator(raw) {
|
|
58
|
+
if (raw === undefined || raw === null) return { initiator: INITIATORS.TRIGGER };
|
|
59
|
+
if (typeof raw === "string" && ACCEPTED_INITIATORS.indexOf(raw) !== -1) return { initiator: raw };
|
|
60
|
+
var why = raw === INITIATORS.HUMAN
|
|
61
|
+
? "human turns come only from the authenticated browser"
|
|
62
|
+
: "unknown initiator";
|
|
63
|
+
return {
|
|
64
|
+
refusal: refused("initiator " + shown(raw) + " refused: " + why + " (accepted: " + ACCEPTED_INITIATORS.join(", ") + ")"),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// A session in memory is addressed by the id its vendor assigned or by the
|
|
69
|
+
// console's own session id (what open-session returns, always present).
|
|
70
|
+
function findSession(sm, id) {
|
|
71
|
+
var byVendorId = null;
|
|
72
|
+
var byConsoleId = null;
|
|
73
|
+
sm.sessions.forEach(function (s) {
|
|
74
|
+
if (!byVendorId && s.cliSessionId === id) byVendorId = s;
|
|
75
|
+
if (!byConsoleId && String(s.localId) === id) byConsoleId = s;
|
|
76
|
+
});
|
|
77
|
+
return byVendorId || byConsoleId;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function messageOf(err) {
|
|
81
|
+
return err && err.message ? err.message : String(err);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// The query did not start, so the input was not delivered and sending it again
|
|
85
|
+
// cannot deliver it twice.
|
|
86
|
+
function sendStartFailure(project, session, label, err) {
|
|
87
|
+
session.isProcessing = false;
|
|
88
|
+
project.onProcessingChanged();
|
|
89
|
+
console.error("[session-input] turn start failed for " + label + ":", messageOf(err));
|
|
90
|
+
return { ok: false, result: RESULT.ERROR, retryable: true, reason: "turn start failed: " + messageOf(err) };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// A session this request created whose first turn did not start is removed, so
|
|
94
|
+
// a failed open leaves nothing behind, and is not retried: the caller opens
|
|
95
|
+
// again if it wants to.
|
|
96
|
+
function openStartFailure(project, session, label, err) {
|
|
97
|
+
session.isProcessing = false;
|
|
98
|
+
project.onProcessingChanged();
|
|
99
|
+
console.error("[session-input] open failed for " + label + ":", messageOf(err));
|
|
100
|
+
try { project.sm.deleteSession(session.localId); }
|
|
101
|
+
catch (e) { console.error("[session-input] could not remove " + label + ":", messageOf(e)); }
|
|
102
|
+
return { ok: false, result: RESULT.ERROR, retryable: false, reason: "open failed: " + messageOf(err) };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Starts the session's query and answers when it has actually started: a
|
|
106
|
+
// rejection (or a synchronous throw) is a failed delivery, never a delivered
|
|
107
|
+
// one. Returns the value of onStarted(), or a Promise of it when the start is
|
|
108
|
+
// asynchronous. onFailed(project, session, label, err) makes the failure result.
|
|
109
|
+
function startThen(project, session, label, start, onStarted, onFailed) {
|
|
110
|
+
var started;
|
|
111
|
+
try { started = start(); }
|
|
112
|
+
catch (e) { return onFailed(project, session, label, e); }
|
|
113
|
+
if (started && typeof started.then === "function") {
|
|
114
|
+
return started.then(onStarted, function (err) { return onFailed(project, session, label, err); });
|
|
115
|
+
}
|
|
116
|
+
return onStarted();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// A caller on the daemon's IPC or the trigger watcher must always get a
|
|
120
|
+
// result, so an unexpected throw is reported as an error, not propagated.
|
|
121
|
+
function neverThrows(fn) {
|
|
122
|
+
return function (req) {
|
|
123
|
+
try { return fn(req); }
|
|
124
|
+
catch (e) {
|
|
125
|
+
console.error("[session-input] unexpected failure:", messageOf(e));
|
|
126
|
+
return { ok: false, result: RESULT.ERROR, reason: "unexpected failure: " + messageOf(e) };
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// deps:
|
|
132
|
+
// getProject(slug) -> project context | null
|
|
133
|
+
// agentCatalog(project) -> [{ name }] named agents on offer (default: the picker's catalog)
|
|
134
|
+
function createSessionInput(deps) {
|
|
135
|
+
var getProject = deps.getProject;
|
|
136
|
+
|
|
137
|
+
function defaultAgentCatalog(project) {
|
|
138
|
+
// Neither lookup throws: both answer [] when discovery has not run or the
|
|
139
|
+
// directory is absent.
|
|
140
|
+
return agentsModule.getAll().concat(agentsModule.readProjectAgents(project.cwd));
|
|
141
|
+
}
|
|
142
|
+
var agentCatalog = deps.agentCatalog || defaultAgentCatalog;
|
|
143
|
+
|
|
144
|
+
function resolveProject(slug) {
|
|
145
|
+
var project = typeof slug === "string" && slug ? getProject(slug) : null;
|
|
146
|
+
if (!project) return { refusal: refused("unknown project " + shown(slug)) };
|
|
147
|
+
if (!project.sm || !project.sdk) {
|
|
148
|
+
return { refusal: { ok: false, result: RESULT.ERROR, reason: "project " + shown(slug) + " is not ready" } };
|
|
149
|
+
}
|
|
150
|
+
return { project: project };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function checkPrompt(prompt) {
|
|
154
|
+
return typeof prompt === "string" && prompt ? null : refused("prompt must be a non-empty string");
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// History entry that marks the turn as injected, not typed. The input is
|
|
158
|
+
// already delivered when this runs, so a failure to record is logged and does
|
|
159
|
+
// not turn a delivered turn into an error a caller would retry.
|
|
160
|
+
function recordInjected(project, session, req, initiator) {
|
|
161
|
+
var entry = {
|
|
162
|
+
type: "user_message",
|
|
163
|
+
text: req.prompt,
|
|
164
|
+
source: "external-trigger",
|
|
165
|
+
initiator: initiator,
|
|
166
|
+
};
|
|
167
|
+
if (req.triggerId) entry.triggerId = req.triggerId;
|
|
168
|
+
if (req.contextNote) entry.contextNote = req.contextNote;
|
|
169
|
+
try {
|
|
170
|
+
project.sm.recordHistoryEntry(session, entry, true);
|
|
171
|
+
project.sm.appendToSessionFile(session, entry);
|
|
172
|
+
} catch (e) {
|
|
173
|
+
console.error("[session-input] failed to record injected message:", e.message || e);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function describe(project, session) {
|
|
178
|
+
return {
|
|
179
|
+
consoleSessionId: String(session.localId),
|
|
180
|
+
sessionId: session.cliSessionId || null,
|
|
181
|
+
localId: session.localId,
|
|
182
|
+
vendor: session.vendor || project.sm.defaultVendor || null,
|
|
183
|
+
agent: session.agentName || null,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Delivers req.prompt as the session's next turn: into the running query,
|
|
188
|
+
// or by starting one (the session's runtime was ended) on the session's own
|
|
189
|
+
// vendor and agent.
|
|
190
|
+
function deliver(project, session, req, initiator, resumed) {
|
|
191
|
+
var sdk = project.sdk;
|
|
192
|
+
var processing = !!session.isProcessing;
|
|
193
|
+
var needsStart = !processing && !session.queryInstance && (!session.worker || session.messageQueue !== "worker");
|
|
194
|
+
|
|
195
|
+
if (needsStart) {
|
|
196
|
+
var refusal = getNewWorkRefusal();
|
|
197
|
+
if (refusal) return refusedForLoad(refusal);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function delivered() {
|
|
201
|
+
recordInjected(project, session, req, initiator);
|
|
202
|
+
var out = describe(project, session);
|
|
203
|
+
out.ok = true;
|
|
204
|
+
out.result = (resumed || needsStart) ? RESULT.RESUMED : RESULT.DELIVERED;
|
|
205
|
+
return out;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function pushFailure(e) {
|
|
209
|
+
console.error("[session-input] delivery failed:", messageOf(e));
|
|
210
|
+
return { ok: false, result: RESULT.ERROR, reason: "delivery failed: " + messageOf(e) };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (processing) {
|
|
214
|
+
try { sdk.pushMessage(session, req.prompt, null, initiator); }
|
|
215
|
+
catch (e) { return pushFailure(e); }
|
|
216
|
+
return delivered();
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
session.isProcessing = true;
|
|
220
|
+
project.onProcessingChanged();
|
|
221
|
+
session.sentToolResults = {};
|
|
222
|
+
if (needsStart) {
|
|
223
|
+
return startThen(project, session, "session " + String(session.cliSessionId), function () {
|
|
224
|
+
return sdk.startQuery(session, req.prompt, undefined, project.getLinuxUserForSession(session), initiator);
|
|
225
|
+
}, delivered, sendStartFailure);
|
|
226
|
+
}
|
|
227
|
+
try { sdk.pushMessage(session, req.prompt, null, initiator); }
|
|
228
|
+
catch (e) {
|
|
229
|
+
session.isProcessing = false;
|
|
230
|
+
project.onProcessingChanged();
|
|
231
|
+
return pushFailure(e);
|
|
232
|
+
}
|
|
233
|
+
return delivered();
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// req: { projectSlug, sessionId, prompt, initiator?, contextNote?, triggerId? }
|
|
237
|
+
// Returns the result, or a Promise of it when the session must be read from
|
|
238
|
+
// the store first (see settle()).
|
|
239
|
+
function sendInput(req) {
|
|
240
|
+
req = req || {};
|
|
241
|
+
var init = resolveInitiator(req.initiator);
|
|
242
|
+
if (init.refusal) return init.refusal;
|
|
243
|
+
var promptRefusal = checkPrompt(req.prompt);
|
|
244
|
+
if (promptRefusal) return promptRefusal;
|
|
245
|
+
if (typeof req.sessionId !== "string" || !req.sessionId) {
|
|
246
|
+
return refused("sessionId must be a non-empty string");
|
|
247
|
+
}
|
|
248
|
+
// An id names files in the store; one that is not a plain id is refused, not
|
|
249
|
+
// looked up and not reported as absent.
|
|
250
|
+
if (!sessionResume.isSafeSessionId(req.sessionId)) {
|
|
251
|
+
return refused("unsafe session id " + shown(req.sessionId) + " refused");
|
|
252
|
+
}
|
|
253
|
+
var resolved = resolveProject(req.projectSlug);
|
|
254
|
+
if (resolved.refusal) return resolved.refusal;
|
|
255
|
+
var project = resolved.project;
|
|
256
|
+
|
|
257
|
+
var live = findSession(project.sm, req.sessionId);
|
|
258
|
+
if (live) return deliver(project, live, req, init.initiator, false);
|
|
259
|
+
|
|
260
|
+
// Only the console's own store is asked: a session started outside the
|
|
261
|
+
// console is not addressable, and its absence here is positive.
|
|
262
|
+
var storeDeps = { sm: project.sm, cwd: project.cwd };
|
|
263
|
+
function notFound() {
|
|
264
|
+
return { ok: false, result: RESULT.NOT_FOUND, reason: "no session " + shown(req.sessionId) + " in project " + shown(req.projectSlug) };
|
|
265
|
+
}
|
|
266
|
+
return sessionResume.sessionExistsInStore(project.sm, req.sessionId).then(function (exists) {
|
|
267
|
+
if (!exists) return notFound();
|
|
268
|
+
var gate = getNewWorkRefusal();
|
|
269
|
+
if (gate) return refusedForLoad(gate);
|
|
270
|
+
// activate:false: nobody asked to look at this session.
|
|
271
|
+
return sessionResume.resumeStoredSession(storeDeps, req.sessionId, { activate: false }).then(function (session) {
|
|
272
|
+
// The session was found in the store, so a manager that declines to
|
|
273
|
+
// bring it back is a failure to resume, not an absence.
|
|
274
|
+
if (!session) {
|
|
275
|
+
return { ok: false, result: RESULT.ERROR, retryable: false, reason: "session " + shown(req.sessionId) + " exists but could not be resumed" };
|
|
276
|
+
}
|
|
277
|
+
return deliver(project, session, req, init.initiator, true);
|
|
278
|
+
});
|
|
279
|
+
}).catch(function (err) {
|
|
280
|
+
// Lookup and resume failures carry whether trying again can help
|
|
281
|
+
// (session-resume.js); the input was not delivered either way.
|
|
282
|
+
console.error("[session-input] send failed:", messageOf(err));
|
|
283
|
+
return { ok: false, result: RESULT.ERROR, retryable: !!(err && err.retryable), reason: "send failed: " + messageOf(err) };
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// Checks an open request's session kind; returns { options } for
|
|
288
|
+
// sm.createSession (the options the browser's new_session passes) or { refusal }.
|
|
289
|
+
function resolveSessionKind(project, req) {
|
|
290
|
+
var options = {};
|
|
291
|
+
if (req.vendor !== undefined && req.vendor !== null) {
|
|
292
|
+
if (typeof req.vendor !== "string" || !isKnownVendor(req.vendor)) {
|
|
293
|
+
return { refusal: refused("unknown vendor " + shown(req.vendor)) };
|
|
294
|
+
}
|
|
295
|
+
options.vendor = req.vendor;
|
|
296
|
+
}
|
|
297
|
+
if (req.agentName !== undefined && req.agentName !== null) {
|
|
298
|
+
var known = typeof req.agentName === "string" && agentCatalog(project).some(function (a) {
|
|
299
|
+
return a && a.name === req.agentName;
|
|
300
|
+
});
|
|
301
|
+
if (!known) return { refusal: refused("unknown agent " + shown(req.agentName)) };
|
|
302
|
+
options.agentName = req.agentName;
|
|
303
|
+
}
|
|
304
|
+
if (req.sessionVisibility !== undefined && req.sessionVisibility !== null) {
|
|
305
|
+
if (VISIBILITIES.indexOf(req.sessionVisibility) === -1) {
|
|
306
|
+
return { refusal: refused("unknown sessionVisibility " + shown(req.sessionVisibility) + " (accepted: " + VISIBILITIES.join(", ") + ")") };
|
|
307
|
+
}
|
|
308
|
+
options.sessionVisibility = req.sessionVisibility;
|
|
309
|
+
}
|
|
310
|
+
return { options: options };
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// req: { projectSlug, prompt, vendor?, agentName?, sessionVisibility?,
|
|
314
|
+
// initiator?, contextNote?, cwd?, triggerId? }
|
|
315
|
+
// Returns the result, or a Promise of it when the first turn starts
|
|
316
|
+
// asynchronously (see settle()).
|
|
317
|
+
function openSession(req) {
|
|
318
|
+
req = req || {};
|
|
319
|
+
var init = resolveInitiator(req.initiator);
|
|
320
|
+
if (init.refusal) return init.refusal;
|
|
321
|
+
var promptRefusal = checkPrompt(req.prompt);
|
|
322
|
+
if (promptRefusal) return promptRefusal;
|
|
323
|
+
var resolved = resolveProject(req.projectSlug);
|
|
324
|
+
if (resolved.refusal) return resolved.refusal;
|
|
325
|
+
var project = resolved.project;
|
|
326
|
+
var kind = resolveSessionKind(project, req);
|
|
327
|
+
if (kind.refusal) return kind.refusal;
|
|
328
|
+
var gate = getNewWorkRefusal();
|
|
329
|
+
if (gate) return refusedForLoad(gate);
|
|
330
|
+
|
|
331
|
+
var sm = project.sm;
|
|
332
|
+
var sess = sm.createSession(kind.options);
|
|
333
|
+
var label = req.triggerId ? " — " + String(req.triggerId).substring(0, 12) : "";
|
|
334
|
+
sess.title = (req.contextNote || "External trigger") + label;
|
|
335
|
+
if (req.cwd) sess.cwd = req.cwd;
|
|
336
|
+
sm.saveSessionFile(sess);
|
|
337
|
+
sm.broadcastSessionList();
|
|
338
|
+
|
|
339
|
+
sess.isProcessing = true;
|
|
340
|
+
project.onProcessingChanged();
|
|
341
|
+
sess.sentToolResults = {};
|
|
342
|
+
// The session stays open for follow-up from a person.
|
|
343
|
+
sess.acceptEditsAfterStart = true;
|
|
344
|
+
|
|
345
|
+
function opened() {
|
|
346
|
+
var out = describe(project, sess);
|
|
347
|
+
out.ok = true;
|
|
348
|
+
out.result = RESULT.OPENED;
|
|
349
|
+
return out;
|
|
350
|
+
}
|
|
351
|
+
return startThen(project, sess, "local session " + sess.localId, function () {
|
|
352
|
+
return project.sdk.startQuery(sess, req.prompt, undefined, project.getLinuxUserForSession(sess), init.initiator);
|
|
353
|
+
}, function () {
|
|
354
|
+
// Recorded once, only now that the query has started: a rejected start
|
|
355
|
+
// leaves no history entry for a prompt that never ran.
|
|
356
|
+
recordInjected(project, sess, req, init.initiator);
|
|
357
|
+
console.log("[session-input] session opened: project=" + req.projectSlug + " session=" + sess.localId);
|
|
358
|
+
return opened();
|
|
359
|
+
}, openStartFailure);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
return { openSession: neverThrows(openSession), sendInput: neverThrows(sendInput) };
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// sendInput and openSession answer synchronously when nothing has to wait and
|
|
366
|
+
// with a Promise otherwise, so a caller that can act at once (the trigger
|
|
367
|
+
// watcher on a live session) does. settle() runs fn on either.
|
|
368
|
+
function settle(valueOrPromise, fn) {
|
|
369
|
+
if (valueOrPromise && typeof valueOrPromise.then === "function") return valueOrPromise.then(fn);
|
|
370
|
+
return fn(valueOrPromise);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
module.exports = {
|
|
374
|
+
createSessionInput: createSessionInput,
|
|
375
|
+
settle: settle,
|
|
376
|
+
resolveInitiator: resolveInitiator,
|
|
377
|
+
RESULT: RESULT,
|
|
378
|
+
ACCEPTED_INITIATORS: ACCEPTED_INITIATORS,
|
|
379
|
+
};
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// Shared bookkeeping for ending a session that still has live background
|
|
4
|
+
// agents (Task dispatches). Two paths end such a session without the operator
|
|
5
|
+
// asking: the idle reaper (lib/sdk-bridge.js) and memory shedding
|
|
6
|
+
// (lib/memory-shed.js). Neither may do it silently, so both announce through
|
|
7
|
+
// announceLiveAgentReclaim: the agents are named, the session is marked as
|
|
8
|
+
// expected to resume, and both UI surfaces are told.
|
|
9
|
+
|
|
10
|
+
var LABEL_MAX = 80;
|
|
11
|
+
var FALLBACK_LABEL = "background agent";
|
|
12
|
+
|
|
13
|
+
function labelTable(session) {
|
|
14
|
+
if (!session.taskLabels) session.taskLabels = Object.create(null);
|
|
15
|
+
return session.taskLabels;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// A Task's description or subagent type comes from the model, so it is bound
|
|
19
|
+
// and reduced to one line before it is shown to the operator.
|
|
20
|
+
function cleanLabel(raw) {
|
|
21
|
+
if (typeof raw !== "string") return FALLBACK_LABEL;
|
|
22
|
+
var one = raw.replace(/\s+/g, " ").trim();
|
|
23
|
+
if (!one) return FALLBACK_LABEL;
|
|
24
|
+
return one.length > LABEL_MAX ? one.slice(0, LABEL_MAX - 3) + "..." : one;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Remember how to name a Task for the operator. */
|
|
28
|
+
function noteTaskLabel(session, taskId, input) {
|
|
29
|
+
if (typeof taskId !== "string" || !taskId) return;
|
|
30
|
+
var src = input && typeof input === "object" ? input : {};
|
|
31
|
+
// Bounds the table: a Task that left tracking via a settled prompt has no
|
|
32
|
+
// other forget event.
|
|
33
|
+
pruneTaskLabels(session);
|
|
34
|
+
labelTable(session)[taskId] = cleanLabel(src.description || src.subagent_type);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Drop the label of every Task the session no longer tracks. A label lives
|
|
39
|
+
* exactly as long as its Task: a Task that completed while its sub-agent's
|
|
40
|
+
* prompt is still open stays tracked, and a reclaim must still name it. The
|
|
41
|
+
* prompt registry decides when a Task leaves tracking, so this follows its
|
|
42
|
+
* answer instead of guessing at the end event.
|
|
43
|
+
*/
|
|
44
|
+
function pruneTaskLabels(session) {
|
|
45
|
+
var table = session && session.taskLabels;
|
|
46
|
+
if (!table) return;
|
|
47
|
+
var tracked = session.activeTaskToolIds || {};
|
|
48
|
+
Object.keys(table).forEach(function (id) {
|
|
49
|
+
if (!Object.prototype.hasOwnProperty.call(tracked, id) || !tracked[id]) delete table[id];
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function labelOf(session, taskId) {
|
|
54
|
+
var table = session.taskLabels;
|
|
55
|
+
return (table && Object.prototype.hasOwnProperty.call(table, taskId) && table[taskId]) || FALLBACK_LABEL;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Every Task the session still tracks, as [{id, label}]. The one reader of
|
|
60
|
+
* session.activeTaskToolIds for operator-facing lists: the reclaim announcer
|
|
61
|
+
* and the build-update offer both go through it.
|
|
62
|
+
*/
|
|
63
|
+
function liveAgentTasks(session) {
|
|
64
|
+
var tasks = session && session.activeTaskToolIds;
|
|
65
|
+
if (!tasks) return [];
|
|
66
|
+
return Object.keys(tasks).filter(function (id) { return tasks[id]; }).map(function (id) {
|
|
67
|
+
return { id: id, label: cleanLabel(labelOf(session, id)) };
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Operator-facing names of every Task the session still tracks. */
|
|
72
|
+
function liveAgentLabels(session) {
|
|
73
|
+
return liveAgentTasks(session).map(function (t) { return t.label; });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Name of the Task that owns a tool call raised by a sub-agent, or null. */
|
|
77
|
+
function ownerLabelOfTool(session, prompts, toolUseId) {
|
|
78
|
+
var owner = prompts.ownerTaskOfTool(session, toolUseId);
|
|
79
|
+
return owner ? labelOf(session, owner) : null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function describeAgents(labels) {
|
|
83
|
+
return labels.length === 1
|
|
84
|
+
? 'background agent "' + labels[0] + '"'
|
|
85
|
+
: labels.length + " background agents (" + labels.map(function (l) { return '"' + l + '"'; }).join(", ") + ")";
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function reclaimMessage(cause, labels) {
|
|
89
|
+
return cause + " Interrupted: " + describeAgents(labels) + ". "
|
|
90
|
+
+ "The session will resume on your next message; if it does not, resume it manually.";
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// A daemon shutdown ends every session at once and nobody is connected to hear
|
|
94
|
+
// it, so the agents are written to the session file and told to the operator
|
|
95
|
+
// at the next startup (surfaceRestartReclaims / restartNoticeMessages).
|
|
96
|
+
var RESTART_REASON = "daemon_restart";
|
|
97
|
+
var RESTART_CAUSE = "Clagentic: Console shut down or restarted while this session had background agents running.";
|
|
98
|
+
// Bounds the meta line of the session file, which is read back from a 4 KiB chunk.
|
|
99
|
+
var PERSISTED_AGENTS_MAX = 8;
|
|
100
|
+
|
|
101
|
+
/** Validate a persisted agent-label list read back from a session file. */
|
|
102
|
+
function sanitizeAgentLabels(raw) {
|
|
103
|
+
if (!Array.isArray(raw)) return null;
|
|
104
|
+
var out = raw.filter(function (l) { return typeof l === "string"; })
|
|
105
|
+
.slice(0, PERSISTED_AGENTS_MAX).map(cleanLabel);
|
|
106
|
+
return out.length ? out : null;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function errText(e) {
|
|
110
|
+
return e && e.message ? e.message : String(e);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// A failure here is never silent: the journal names the session, the agents
|
|
114
|
+
// and the error, so a lost resume marker or notice can be traced afterwards.
|
|
115
|
+
function logReclaimFailure(what, session, labels, e) {
|
|
116
|
+
console.error("[session-reclaim] " + what + " for session " + session.localId
|
|
117
|
+
+ " (agents: " + labels.map(function (l) { return JSON.stringify(l); }).join(", ") + "): " + errText(e));
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Tell the operator a session with live background agents was ended, and
|
|
122
|
+
* record that it is expected to resume. Never throws: the caller is ending a
|
|
123
|
+
* session (or the process) and must proceed whatever happens here.
|
|
124
|
+
*
|
|
125
|
+
* Returns { labels, persisted }. labels are the agents announced (empty when
|
|
126
|
+
* the session had none, in which case nothing is sent or written and
|
|
127
|
+
* persisted is true). persisted is false when the resume marker could not be
|
|
128
|
+
* written to the session file, so a caller can count the failures.
|
|
129
|
+
*
|
|
130
|
+
* @param {object} o
|
|
131
|
+
* @param {object} o.session
|
|
132
|
+
* @param {object} o.sm - session manager (saveSessionFile, sendAndRecord)
|
|
133
|
+
* @param {?function(object)} o.send - project-wide broadcast
|
|
134
|
+
* @param {?string} o.slug
|
|
135
|
+
* @param {string} o.reason - durable marker stored on the session
|
|
136
|
+
* @param {string} o.source - diagnostic source shown in the Diagnostics panel
|
|
137
|
+
* @param {string} o.cause - sentence naming why the session was ended
|
|
138
|
+
* @param {boolean} [o.deferNotice] - persist the marker and the agent names but
|
|
139
|
+
* send nothing now (the process is exiting; the notice goes out at startup)
|
|
140
|
+
*/
|
|
141
|
+
function announceLiveAgentReclaim(o) {
|
|
142
|
+
var session = o.session;
|
|
143
|
+
if (!session) return { labels: [], persisted: true };
|
|
144
|
+
var labels = liveAgentLabels(session);
|
|
145
|
+
if (labels.length === 0) return { labels: labels, persisted: true };
|
|
146
|
+
|
|
147
|
+
var persisted = true;
|
|
148
|
+
try {
|
|
149
|
+
session.pendingAutoResume = true;
|
|
150
|
+
session.pendingAutoResumeReason = o.reason;
|
|
151
|
+
// Only a deferred notice needs the names to outlive the process.
|
|
152
|
+
if (o.deferNotice) session.pendingAutoResumeAgents = labels.slice(0, PERSISTED_AGENTS_MAX);
|
|
153
|
+
if (o.sm && typeof o.sm.saveSessionFile === "function") o.sm.saveSessionFile(session);
|
|
154
|
+
} catch (e) {
|
|
155
|
+
// The notification below still has to go out if the marker cannot be written.
|
|
156
|
+
persisted = false;
|
|
157
|
+
logReclaimFailure("Could not persist the resume marker (" + o.reason + ")", session, labels, e);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (o.deferNotice) return { labels: labels, persisted: persisted };
|
|
161
|
+
|
|
162
|
+
var message = reclaimMessage(o.cause, labels);
|
|
163
|
+
if (typeof o.send === "function") {
|
|
164
|
+
try {
|
|
165
|
+
o.send({
|
|
166
|
+
type: "diagnostic",
|
|
167
|
+
severity: "warning",
|
|
168
|
+
source: o.source,
|
|
169
|
+
message: (o.slug ? "[" + o.slug + "] " : "") + message,
|
|
170
|
+
});
|
|
171
|
+
} catch (e) {
|
|
172
|
+
logReclaimFailure("Could not broadcast the reclaim notice", session, labels, e);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (o.sm && typeof o.sm.sendAndRecord === "function") {
|
|
176
|
+
try {
|
|
177
|
+
o.sm.sendAndRecord(session, { type: "error", text: message });
|
|
178
|
+
} catch (e) {
|
|
179
|
+
logReclaimFailure("Could not record the inline reclaim notice", session, labels, e);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return { labels: labels, persisted: persisted };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Daemon shutdown, called per session before its query is aborted. A session
|
|
187
|
+
* whose query is running and still tracks background agents is announced
|
|
188
|
+
* through the same helper as the reap and the shed; a session without one is
|
|
189
|
+
* left untouched. Synchronous, because the process is exiting.
|
|
190
|
+
*/
|
|
191
|
+
function announceShutdownReclaim(session, sm) {
|
|
192
|
+
if (!session || !(session.queryInstance || session.worker)) return { labels: [], persisted: true };
|
|
193
|
+
return announceLiveAgentReclaim({
|
|
194
|
+
session: session,
|
|
195
|
+
sm: sm,
|
|
196
|
+
reason: RESTART_REASON,
|
|
197
|
+
source: "daemon-shutdown",
|
|
198
|
+
cause: RESTART_CAUSE,
|
|
199
|
+
deferNotice: true,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Counts one daemon shutdown's announcements, across every project. */
|
|
204
|
+
function createShutdownTally() {
|
|
205
|
+
return { sessions: 0, failed: 0 };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Fold one announceShutdownReclaim result into the tally. */
|
|
209
|
+
function countShutdownReclaim(tally, result) {
|
|
210
|
+
if (!tally || !result || result.labels.length === 0) return;
|
|
211
|
+
tally.sessions += 1;
|
|
212
|
+
if (!result.persisted) tally.failed += 1;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** The one journal line per shutdown; an error line when any marker was lost. */
|
|
216
|
+
function logShutdownSummary(tally) {
|
|
217
|
+
var line = "[session-reclaim] Shutdown: " + tally.sessions + " session(s) with background agents recorded as resumable, "
|
|
218
|
+
+ tally.failed + " could not be persisted";
|
|
219
|
+
if (tally.failed > 0) console.error(line);
|
|
220
|
+
else console.log(line);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function restartAgents(session) {
|
|
224
|
+
return session.pendingAutoResume && session.pendingAutoResumeReason === RESTART_REASON
|
|
225
|
+
? sanitizeAgentLabels(session.pendingAutoResumeAgents) : null;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* First startup after a shutdown that interrupted background agents: record
|
|
230
|
+
* the inline notice in each such session (it replays to every client that
|
|
231
|
+
* opens it) and keep the names in memory for the diagnostic that
|
|
232
|
+
* restartDiagnostics sends to each connecting client. The persisted names are
|
|
233
|
+
* dropped so a second restart does not repeat the notice.
|
|
234
|
+
*/
|
|
235
|
+
function surfaceRestartReclaims(o) {
|
|
236
|
+
var sm = o.sm;
|
|
237
|
+
sm.sessions.forEach(function (session) {
|
|
238
|
+
var labels = restartAgents(session);
|
|
239
|
+
if (!labels) return;
|
|
240
|
+
session.pendingAutoResumeAgents = null;
|
|
241
|
+
session.restartNoticeAgents = labels;
|
|
242
|
+
try {
|
|
243
|
+
sm.sendAndRecord(session, { type: "error", text: reclaimMessage(RESTART_CAUSE, labels) });
|
|
244
|
+
if (typeof sm.saveSessionFile === "function") sm.saveSessionFile(session);
|
|
245
|
+
} catch (e) {
|
|
246
|
+
console.error("[session-reclaim] Could not record the restart notice:", e && e.message ? e.message : e);
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** Diagnostic messages for a client that connects while a restart notice is unread. */
|
|
252
|
+
function restartDiagnostics(sm, slug, canSee) {
|
|
253
|
+
var out = [];
|
|
254
|
+
// Agent names are session content: without a visibility check nobody gets them.
|
|
255
|
+
if (typeof canSee !== "function") return out;
|
|
256
|
+
sm.sessions.forEach(function (session) {
|
|
257
|
+
if (!session.restartNoticeAgents) return;
|
|
258
|
+
if (!canSee(session)) return;
|
|
259
|
+
out.push({
|
|
260
|
+
type: "diagnostic",
|
|
261
|
+
severity: "warning",
|
|
262
|
+
source: "daemon-restart",
|
|
263
|
+
message: (slug ? "[" + slug + "] " : "") + reclaimMessage(RESTART_CAUSE, session.restartNoticeAgents),
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
return out;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
module.exports = {
|
|
270
|
+
RESTART_REASON: RESTART_REASON,
|
|
271
|
+
sanitizeAgentLabels: sanitizeAgentLabels,
|
|
272
|
+
announceShutdownReclaim: announceShutdownReclaim,
|
|
273
|
+
createShutdownTally: createShutdownTally,
|
|
274
|
+
countShutdownReclaim: countShutdownReclaim,
|
|
275
|
+
logShutdownSummary: logShutdownSummary,
|
|
276
|
+
surfaceRestartReclaims: surfaceRestartReclaims,
|
|
277
|
+
restartDiagnostics: restartDiagnostics,
|
|
278
|
+
noteTaskLabel: noteTaskLabel,
|
|
279
|
+
pruneTaskLabels: pruneTaskLabels,
|
|
280
|
+
liveAgentLabels: liveAgentLabels,
|
|
281
|
+
liveAgentTasks: liveAgentTasks,
|
|
282
|
+
ownerLabelOfTool: ownerLabelOfTool,
|
|
283
|
+
announceLiveAgentReclaim: announceLiveAgentReclaim,
|
|
284
|
+
};
|