@clagentic/console 1.9.0 → 1.10.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/LICENSE +117 -12
- package/LICENSES/MIT-legacy.txt +22 -0
- package/LICENSES/README.md +15 -0
- package/README.md +8 -2
- package/bin/cli.js +30 -5
- package/deploy/clagentic-console.service +10 -6
- package/lib/audit.js +3 -2
- package/lib/build-sha.json +2 -2
- package/lib/build-update-check.js +138 -0
- package/lib/cli/daemon-launch.js +17 -17
- package/lib/config.js +32 -20
- package/lib/daemon-env-settings.js +84 -0
- package/lib/daemon-projects.js +38 -0
- package/lib/daemon.js +202 -158
- package/lib/drain.js +74 -17
- package/lib/env-compat.js +61 -0
- package/lib/extension-commands.js +45 -0
- package/lib/history-visibility.js +4 -0
- package/lib/idle-adapter-reaper.js +71 -0
- package/lib/link-trap.js +78 -0
- package/lib/lore-attestation.js +73 -0
- package/lib/mcp-local.js +8 -6
- package/lib/mcp-tool-input.js +116 -0
- package/lib/mcp-tool-schema.js +179 -0
- package/lib/memory-limits.js +61 -5
- package/lib/memory-setting-validate.js +65 -0
- package/lib/new-work-gate.js +47 -0
- package/lib/os-users.js +5 -3
- package/lib/project-access.js +139 -0
- package/lib/project-connection.js +23 -22
- package/lib/project-external-trigger.js +103 -10
- package/lib/project-file-scope.js +172 -0
- package/lib/project-file-watch.js +86 -61
- package/lib/project-filesystem.js +144 -128
- package/lib/project-git.js +119 -0
- package/lib/project-http.js +44 -28
- package/lib/project-loop.js +12 -5
- package/lib/project-mcp.js +53 -72
- package/lib/project-message-gate.js +70 -0
- package/lib/project-notifications.js +31 -4
- package/lib/project-prompt-responses.js +216 -0
- package/lib/project-sessions.js +164 -302
- package/lib/project-user-message.js +36 -11
- package/lib/project.js +189 -142
- package/lib/projects-dir.js +90 -0
- package/lib/prompt-access.js +22 -0
- package/lib/prompt-kinds/answer-limits.js +15 -0
- package/lib/prompt-kinds/ask-user.js +58 -0
- package/lib/prompt-kinds/codecs.js +97 -0
- package/lib/prompt-kinds/elicitation.js +77 -0
- package/lib/prompt-kinds/extension.js +39 -0
- package/lib/prompt-kinds/index.js +68 -0
- package/lib/prompt-kinds/permission.js +114 -0
- package/lib/prompt-kinds/plan.js +79 -0
- package/lib/prompt-kinds/tool-call.js +38 -0
- package/lib/prompt-registry.js +673 -0
- package/lib/public/app.js +1 -1
- package/lib/public/css/diagnostics.css +50 -0
- package/lib/public/css/input.css +17 -3
- package/lib/public/css/menus.css +10 -0
- package/lib/public/css/messages.css +5 -0
- package/lib/public/css/rewind.css +22 -0
- package/lib/public/index.html +4 -12
- package/lib/public/modules/app-connection.js +7 -0
- package/lib/public/modules/app-cursors.js +7 -5
- package/lib/public/modules/app-messages.js +45 -45
- package/lib/public/modules/app-notifications.js +47 -71
- package/lib/public/modules/app-panels.js +61 -58
- package/lib/public/modules/app-projects.js +2 -1
- package/lib/public/modules/app-rendering.js +4 -10
- package/lib/public/modules/diagnostics.js +60 -16
- package/lib/public/modules/filebrowser.js +18 -16
- package/lib/public/modules/history-visibility.js +4 -0
- package/lib/public/modules/mention.js +3 -2
- package/lib/public/modules/mode-options.js +38 -0
- package/lib/public/modules/project-settings.js +19 -8
- package/lib/public/modules/prompt-banner.js +78 -0
- package/lib/public/modules/prompt-card.js +201 -0
- package/lib/public/modules/prompt-kinds/ask-user-codec.js +131 -0
- package/lib/public/modules/prompt-kinds/ask-user.js +213 -0
- package/lib/public/modules/prompt-kinds/elicitation-codec.js +343 -0
- package/lib/public/modules/prompt-kinds/elicitation.js +176 -0
- package/lib/public/modules/prompt-kinds/index.js +18 -0
- package/lib/public/modules/prompt-kinds/own-key.js +29 -0
- package/lib/public/modules/prompt-kinds/package.json +3 -0
- package/lib/public/modules/prompt-kinds/permission-choices.js +31 -0
- package/lib/public/modules/prompt-kinds/permission.js +209 -0
- package/lib/public/modules/prompt-kinds/plan.js +102 -0
- package/lib/public/modules/prompt-state.js +142 -0
- package/lib/public/modules/prompts.js +134 -0
- package/lib/public/modules/rewind.js +4 -1
- package/lib/public/modules/server-settings.js +2 -4
- package/lib/public/modules/sidebar-mobile.js +19 -9
- package/lib/public/modules/sidebar-sessions.js +21 -14
- package/lib/public/modules/sticky-notes.js +3 -2
- package/lib/public/modules/team-panel.js +5 -4
- package/lib/public/modules/tools.js +67 -904
- package/lib/public/modules/vendor-picker.js +53 -0
- package/lib/public/modules/vendor-registry.js +133 -0
- package/lib/public/sw.js +3 -1
- package/lib/rc-migrate.js +44 -0
- package/lib/runtime-env-keys.js +109 -0
- package/lib/runtime-env.js +236 -0
- package/lib/sdk-bridge.js +290 -273
- package/lib/sdk-message-processor.js +125 -145
- package/lib/server-hub-sessions.js +13 -10
- package/lib/server-palette.js +28 -19
- package/lib/server.js +141 -113
- package/lib/service-install.js +96 -0
- package/lib/session-capabilities.js +76 -0
- package/lib/sessions.js +45 -28
- package/lib/users-permissions.js +9 -6
- package/lib/users.js +9 -0
- package/lib/utils.js +4 -2
- package/lib/vendor-auth.js +22 -0
- package/lib/vendor-registry-message.js +65 -0
- package/lib/ws-origin.js +99 -0
- package/lib/ws-schema.js +33 -22
- package/lib/yoke/adapters/claude-capabilities.js +38 -0
- package/lib/yoke/adapters/claude-vendor.js +35 -0
- package/lib/yoke/adapters/claude-worker.js +55 -87
- package/lib/yoke/adapters/claude.js +156 -62
- package/lib/yoke/adapters/codex-capabilities.js +37 -0
- package/lib/yoke/adapters/codex-vendor.js +46 -0
- package/lib/yoke/adapters/codex.js +347 -156
- package/lib/yoke/adapters/effort-control.js +34 -0
- package/lib/yoke/adapters/permission-relay.js +58 -0
- package/lib/yoke/adapters/worker-mcp-servers.js +81 -0
- package/lib/yoke/auth-checks.js +104 -0
- package/lib/yoke/capabilities.js +49 -0
- package/lib/yoke/codex-app-server.js +23 -5
- package/lib/yoke/env-ownership.js +40 -0
- package/lib/yoke/index.js +67 -173
- package/lib/yoke/install-probe.js +121 -0
- package/lib/yoke/instructions.js +7 -11
- package/lib/yoke/interface.js +199 -3
- package/lib/yoke/package.json +1 -1
- package/lib/yoke/provider-credentials.js +96 -0
- package/lib/yoke/registry.js +340 -0
- package/package.json +12 -8
- package/scripts/check-sdk-lock-drift.js +160 -0
- package/scripts/check-test-count.js +76 -1
- package/scripts/postinstall.js +218 -136
- package/scripts/prompt-harness-history.js +202 -0
- package/scripts/verify-installed.js +67 -0
- package/lib/sdk-permission-ownership.js +0 -136
package/lib/sdk-bridge.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
const crypto = require("crypto");
|
|
2
1
|
var fs = require("fs");
|
|
3
2
|
var path = require("path");
|
|
4
3
|
var execSync = require("child_process").execSync;
|
|
@@ -12,18 +11,25 @@ var { readAgentToolsFromFile, parseFrontmatter, slugifyAgentName, AGENTS_SOURCE_
|
|
|
12
11
|
var { discoverWorkflows } = require("./sdk-workflow-discovery");
|
|
13
12
|
var { buildEnrichedSlashCommands } = require("./sdk-slash-enrichment");
|
|
14
13
|
var { runPreflight } = require("./settings-preflight");
|
|
15
|
-
var {
|
|
16
|
-
var {
|
|
14
|
+
var { promptsFor } = require("./prompt-registry");
|
|
15
|
+
var { toolApprovalKind } = require("./prompt-kinds");
|
|
17
16
|
var sessionActivity = require("./session-activity");
|
|
17
|
+
var { getNewWorkRefusal } = require("./new-work-gate");
|
|
18
|
+
var runtimeEnv = require("./runtime-env");
|
|
19
|
+
var mcpToolSchema = require("./mcp-tool-schema");
|
|
20
|
+
var { registry: yokeRegistry } = require("./yoke");
|
|
21
|
+
var sessionCapabilities = require("./session-capabilities");
|
|
22
|
+
var { buildVendorRegistryMessage, recordInstallState } = require("./vendor-registry-message");
|
|
23
|
+
var { authRequiredFacts } = require("./vendor-auth");
|
|
18
24
|
|
|
19
25
|
// --- lr-29f9: Global concurrent-session ceiling ---
|
|
20
26
|
// Counts queries that are actively running (startQuery entered, processQueryStream
|
|
21
27
|
// finally not yet reached) across ALL project bridge instances in this daemon process.
|
|
22
28
|
// Each project's sdk-bridge instance shares this module-level counter.
|
|
23
|
-
// Env override:
|
|
29
|
+
// Env override: CLAGENTIC_CONSOLE_MAX_CONCURRENT_SESSIONS (must be a positive integer).
|
|
24
30
|
var _activeLiveCount = 0;
|
|
25
31
|
var MAX_CONCURRENT_SESSIONS = (function () {
|
|
26
|
-
var env = parseInt(
|
|
32
|
+
var env = parseInt(require("./env-compat").readConsoleEnv("CLAGENTIC_CONSOLE_MAX_CONCURRENT_SESSIONS"), 10);
|
|
27
33
|
return (env > 0) ? env : 50;
|
|
28
34
|
})();
|
|
29
35
|
|
|
@@ -294,31 +300,16 @@ function resolveEffectiveHome(linuxUser) {
|
|
|
294
300
|
|
|
295
301
|
// Extract serializable tool descriptors from MCP server instances.
|
|
296
302
|
// Used for IPC to worker processes (McpSdkServerConfigWithInstance is not serializable).
|
|
297
|
-
|
|
303
|
+
// A tool whose input schema cannot be converted is left out and reported
|
|
304
|
+
// through onSkipped; it is never registered with an empty schema.
|
|
305
|
+
function extractMcpDescriptors(mcpServers, onSkipped, toJSONSchema) {
|
|
298
306
|
if (!mcpServers) return null;
|
|
299
|
-
|
|
300
|
-
try { toJSONSchema = require("zod").toJSONSchema; } catch (e) { return null; }
|
|
307
|
+
if (toJSONSchema === undefined) toJSONSchema = mcpToolSchema.loadToJSONSchema();
|
|
301
308
|
var descriptors = [];
|
|
302
309
|
var names = Object.keys(mcpServers);
|
|
303
310
|
for (var i = 0; i < names.length; i++) {
|
|
304
311
|
var serverName = names[i];
|
|
305
|
-
var
|
|
306
|
-
if (!server || !server.instance || !server.instance._registeredTools) continue;
|
|
307
|
-
var tools = [];
|
|
308
|
-
var toolNames = Object.keys(server.instance._registeredTools);
|
|
309
|
-
for (var j = 0; j < toolNames.length; j++) {
|
|
310
|
-
var toolName = toolNames[j];
|
|
311
|
-
var toolDef = server.instance._registeredTools[toolName];
|
|
312
|
-
var inputSchema = { type: "object", properties: {} };
|
|
313
|
-
try {
|
|
314
|
-
if (toolDef.inputSchema) inputSchema = toJSONSchema(toolDef.inputSchema);
|
|
315
|
-
} catch (e) { /* fallback to empty schema */ }
|
|
316
|
-
tools.push({
|
|
317
|
-
name: toolName,
|
|
318
|
-
description: toolDef.description || toolName,
|
|
319
|
-
inputSchema: inputSchema,
|
|
320
|
-
});
|
|
321
|
-
}
|
|
312
|
+
var tools = mcpToolSchema.listServerTools(serverName, mcpServers[serverName], toJSONSchema, onSkipped);
|
|
322
313
|
if (tools.length > 0) descriptors.push({ serverName: serverName, tools: tools });
|
|
323
314
|
}
|
|
324
315
|
return descriptors.length > 0 ? descriptors : null;
|
|
@@ -384,6 +375,9 @@ function createSDKBridge(opts) {
|
|
|
384
375
|
var slug = opts.slug || "";
|
|
385
376
|
var sm = opts.sessionManager; // session manager instance
|
|
386
377
|
var send = opts.send; // broadcast to all clients
|
|
378
|
+
// Every registered agent has a recorded capability set from the first moment,
|
|
379
|
+
// so a query that starts before warmup finishes is still gated on its agent.
|
|
380
|
+
sessionCapabilities.seedCapabilities(sm);
|
|
387
381
|
var pushModule = opts.pushModule;
|
|
388
382
|
var getNotificationsModule = opts.getNotificationsModule || function () { return null; };
|
|
389
383
|
var adapter = opts.adapter;
|
|
@@ -402,6 +396,46 @@ function createSDKBridge(opts) {
|
|
|
402
396
|
var clayTls = opts.clayTls || false;
|
|
403
397
|
var clayAuthToken = opts.clayAuthToken || null;
|
|
404
398
|
var onProcessingChanged = opts.onProcessingChanged || function () {};
|
|
399
|
+
|
|
400
|
+
// --- Environment saved in Settings ---
|
|
401
|
+
// The variables Settings add to the environment of an agent runtime, read
|
|
402
|
+
// fresh at every runtime start, so a change reaches the runtimes started
|
|
403
|
+
// after it; a runtime already running keeps the environment it started with.
|
|
404
|
+
// Null when there are none (no settings source, or nothing saved): the
|
|
405
|
+
// runtime then has the daemon's own environment. `session` is the session the
|
|
406
|
+
// runtime is for: which settings reach it depends on who owns it (see
|
|
407
|
+
// getRuntimeEnvSettings in lib/project.js). No session is a runtime that
|
|
408
|
+
// serves everyone, such as a model-list warmup.
|
|
409
|
+
function getRuntimeEnvOverlay(session) {
|
|
410
|
+
if (typeof opts.getRuntimeEnvSettings !== "function") return null;
|
|
411
|
+
var settings = opts.getRuntimeEnvSettings(session) || {};
|
|
412
|
+
var overlay = runtimeEnv.buildRuntimeEnvOverlay([
|
|
413
|
+
{ label: "shared", text: settings.shared },
|
|
414
|
+
{ label: "project " + slug, text: settings.project },
|
|
415
|
+
]);
|
|
416
|
+
return runtimeEnv.hasOverlay(overlay) ? overlay : null;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// queryOptions with runtimeEnv set: the settings' variables, or null for
|
|
420
|
+
// none. Always set, because an adapter that keeps a server per environment
|
|
421
|
+
// (Codex) refuses a caller that does not say which one it is.
|
|
422
|
+
function withRuntimeEnv(queryOptions, session) {
|
|
423
|
+
queryOptions.runtimeEnv = getRuntimeEnvOverlay(session);
|
|
424
|
+
return queryOptions;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// Tells each connected client the agent registry again, after something
|
|
428
|
+
// changed what it holds (a vendor's live capabilities were recorded). Install
|
|
429
|
+
// state is per OS identity, so the project supplies a sender that gives every
|
|
430
|
+
// client its own; the default is a single broadcast for a daemon account.
|
|
431
|
+
function sendVendorRegistry() {
|
|
432
|
+
if (typeof opts.sendVendorRegistry === "function") {
|
|
433
|
+
opts.sendVendorRegistry();
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
send(buildVendorRegistryMessage(sm, null));
|
|
437
|
+
}
|
|
438
|
+
|
|
405
439
|
var _cachedFreshAuthState = null;
|
|
406
440
|
var _cachedFreshAuthAt = 0;
|
|
407
441
|
|
|
@@ -428,10 +462,17 @@ function createSDKBridge(opts) {
|
|
|
428
462
|
|| errLower.indexOf("please login") !== -1;
|
|
429
463
|
}
|
|
430
464
|
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
465
|
+
// The vendor a session runs on: its own, else the project's default adapter's.
|
|
466
|
+
function sessionVendorId(session) {
|
|
467
|
+
return session.vendor || (adapter && adapter.vendor) || yokeRegistry.ids()[0];
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
// Message shown when the user stops a turn; each agent words it itself.
|
|
471
|
+
function interruptMessageFor(session) {
|
|
472
|
+
var vendor = sessionVendorId(session);
|
|
473
|
+
return yokeRegistry.isKnown(vendor)
|
|
474
|
+
? yokeRegistry.get(vendor).interruptMessage
|
|
475
|
+
: "Interrupted.";
|
|
435
476
|
}
|
|
436
477
|
|
|
437
478
|
function notifyAuthRequired(session, title, body, authLinuxUser, canAutoLogin, loginCommand) {
|
|
@@ -504,7 +545,6 @@ function createSDKBridge(opts) {
|
|
|
504
545
|
models: getModelsForVendor(vendor),
|
|
505
546
|
vendor: vendor || (adapter && adapter.vendor) || "claude",
|
|
506
547
|
availableVendors: sm.availableVendors || [],
|
|
507
|
-
installedVendors: sm.installedVendors || [],
|
|
508
548
|
});
|
|
509
549
|
}
|
|
510
550
|
|
|
@@ -526,7 +566,6 @@ function createSDKBridge(opts) {
|
|
|
526
566
|
models: getModelsForVendor(vendor),
|
|
527
567
|
vendor: vendor || (adapter && adapter.vendor) || "claude",
|
|
528
568
|
availableVendors: sm.availableVendors || [],
|
|
529
|
-
installedVendors: sm.installedVendors || [],
|
|
530
569
|
});
|
|
531
570
|
}
|
|
532
571
|
var onTurnDone = opts.onTurnDone || null;
|
|
@@ -699,7 +738,7 @@ function createSDKBridge(opts) {
|
|
|
699
738
|
// real onAutoTitle callers (sdk-message-processor.js) call this
|
|
700
739
|
// fire-and-forget already and are unaffected by a function now
|
|
701
740
|
// returning a value they don't use.
|
|
702
|
-
return sessionAdapter.generateTitle(userMessages, { cwd: cwd }).then(function(title) {
|
|
741
|
+
return sessionAdapter.generateTitle(userMessages, withRuntimeEnv({ cwd: cwd }, session)).then(function(title) {
|
|
703
742
|
if (!title || title.length < 2) return;
|
|
704
743
|
title = title.substring(0, 100);
|
|
705
744
|
// lr-62157d: overwrite whenever the current title is provisional (the
|
|
@@ -767,47 +806,26 @@ function createSDKBridge(opts) {
|
|
|
767
806
|
return Promise.resolve({ action: "reject" });
|
|
768
807
|
}
|
|
769
808
|
|
|
770
|
-
|
|
771
|
-
var requestId = crypto.randomUUID();
|
|
772
|
-
if (!session.pendingElicitations) session.pendingElicitations = {};
|
|
773
|
-
session.pendingElicitations[requestId] = {
|
|
774
|
-
resolve: resolve,
|
|
775
|
-
request: request,
|
|
776
|
-
};
|
|
777
|
-
sendAndRecord(session, {
|
|
778
|
-
type: "elicitation_request",
|
|
779
|
-
requestId: requestId,
|
|
780
|
-
serverName: request.serverName,
|
|
781
|
-
message: request.message,
|
|
782
|
-
mode: request.mode || "form",
|
|
783
|
-
url: request.url || null,
|
|
784
|
-
elicitationId: request.elicitationId || null,
|
|
785
|
-
requestedSchema: request.requestedSchema || null,
|
|
786
|
-
});
|
|
809
|
+
var opened = promptsFor(sm).open(session, "elicitation", request, { signal: opts && opts.signal });
|
|
787
810
|
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
811
|
+
// No push for a prompt that ended at once (its signal was already aborted).
|
|
812
|
+
if (pushModule && opened.pending) {
|
|
813
|
+
var _elicitPayload = {
|
|
814
|
+
type: "elicitation",
|
|
815
|
+
slug: slug,
|
|
816
|
+
title: (request.serverName || "MCP Server") + " needs input",
|
|
817
|
+
body: request.message || "Waiting for your response",
|
|
818
|
+
tag: "claude-elicitation",
|
|
819
|
+
};
|
|
820
|
+
// Route to session owner only — body carries MCP message text.
|
|
821
|
+
if (session.ownerId && pushModule.sendPushToUser) {
|
|
822
|
+
pushModule.sendPushToUser(session.ownerId, _elicitPayload);
|
|
823
|
+
} else {
|
|
824
|
+
pushModule.sendPush(_elicitPayload);
|
|
802
825
|
}
|
|
826
|
+
}
|
|
803
827
|
|
|
804
|
-
|
|
805
|
-
opts.signal.addEventListener("abort", function() {
|
|
806
|
-
delete session.pendingElicitations[requestId];
|
|
807
|
-
resolve({ action: "reject" });
|
|
808
|
-
});
|
|
809
|
-
}
|
|
810
|
-
});
|
|
828
|
+
return opened.answer;
|
|
811
829
|
}
|
|
812
830
|
|
|
813
831
|
|
|
@@ -1034,15 +1052,22 @@ function createSDKBridge(opts) {
|
|
|
1034
1052
|
}
|
|
1035
1053
|
|
|
1036
1054
|
function handleCanUseTool(session, toolName, input, opts) {
|
|
1055
|
+
opts = opts || {};
|
|
1056
|
+
// An ask the user's own ask rule forced is a human prompt by the user's
|
|
1057
|
+
// stated intent (the SDK's matchedAskRule): none of the host-side
|
|
1058
|
+
// auto-approvals below - full auto, an autonomous loop, the read-only
|
|
1059
|
+
// whitelist, an earlier Allow for session - may answer it.
|
|
1060
|
+
var ruleForced = !!opts.matchedAskRule;
|
|
1061
|
+
|
|
1037
1062
|
// Full-auto mode: auto-approve everything except AskUserQuestion
|
|
1038
1063
|
// (which still needs to go through the user interaction flow).
|
|
1039
|
-
if (sm.currentPermissionMode === "bypassPermissions" && toolName !== "AskUserQuestion") {
|
|
1064
|
+
if (!ruleForced && sm.currentPermissionMode === "bypassPermissions" && toolName !== "AskUserQuestion") {
|
|
1040
1065
|
return Promise.resolve({ behavior: "allow", updatedInput: input });
|
|
1041
1066
|
}
|
|
1042
1067
|
|
|
1043
1068
|
// Ralph Loop execution: auto-approve all tools, deny interactive ones.
|
|
1044
1069
|
// Crafting sessions are interactive — user and Claude collaborate to build PROMPT.md / JUDGE.md.
|
|
1045
|
-
if (session.loop && session.loop.active && session.loop.role !== "crafting") {
|
|
1070
|
+
if (!ruleForced && session.loop && session.loop.active && session.loop.role !== "crafting") {
|
|
1046
1071
|
if (toolName === "AskUserQuestion") {
|
|
1047
1072
|
return Promise.resolve({ behavior: "deny", message: "Autonomous mode. Make your own decision." });
|
|
1048
1073
|
}
|
|
@@ -1053,32 +1078,36 @@ function createSDKBridge(opts) {
|
|
|
1053
1078
|
}
|
|
1054
1079
|
|
|
1055
1080
|
// Check shared whitelist (read-only tools, safe browser tools, safe bash commands)
|
|
1056
|
-
var whitelisted = checkToolWhitelist(toolName, input);
|
|
1081
|
+
var whitelisted = ruleForced ? null : checkToolWhitelist(toolName, input);
|
|
1057
1082
|
if (whitelisted) {
|
|
1058
1083
|
return Promise.resolve(whitelisted);
|
|
1059
1084
|
}
|
|
1060
1085
|
|
|
1061
|
-
|
|
1062
|
-
if (toolName === "AskUserQuestion") {
|
|
1063
|
-
return new Promise(function(resolve) {
|
|
1064
|
-
session.pendingAskUser[opts.toolUseID] = {
|
|
1065
|
-
resolve: resolve,
|
|
1066
|
-
input: input,
|
|
1067
|
-
};
|
|
1068
|
-
if (opts.signal) {
|
|
1069
|
-
opts.signal.addEventListener("abort", function() {
|
|
1070
|
-
delete session.pendingAskUser[opts.toolUseID];
|
|
1071
|
-
sendAndRecord(session, { type: "ask_user_answered", toolId: opts.toolUseID });
|
|
1072
|
-
resolve({ behavior: "deny", message: "Cancelled" });
|
|
1073
|
-
});
|
|
1074
|
-
}
|
|
1075
|
-
});
|
|
1076
|
-
}
|
|
1086
|
+
var prompts = promptsFor(sm);
|
|
1077
1087
|
|
|
1078
|
-
//
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1088
|
+
// AskUserQuestion: the operator's answers become the tool's input.
|
|
1089
|
+
if (toolName === "AskUserQuestion") {
|
|
1090
|
+
return prompts.open(session, "ask_user", { input: input }, {
|
|
1091
|
+
toolUseId: opts.toolUseID,
|
|
1092
|
+
signal: opts.signal,
|
|
1093
|
+
}).answer;
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
var kind = toolApprovalKind(toolName);
|
|
1097
|
+
|
|
1098
|
+
// Auto-approve if tool was previously allowed for session (keyed on
|
|
1099
|
+
// toolName + input discriminator, not bare toolName — see
|
|
1100
|
+
// permissionGrantKey's doc comment for why the Skill tool needs this).
|
|
1101
|
+
// An Allow-for-session grant is a standing rule for every later call with
|
|
1102
|
+
// the same key. The SDK forbids that for an ask marked
|
|
1103
|
+
// suppressAlwaysAllowRule (a rule would grant more than this ask's own
|
|
1104
|
+
// action), so such an ask is never answered by an earlier grant. An ask
|
|
1105
|
+
// marked defaultToNo must open on No, which a silent approval would
|
|
1106
|
+
// bypass; that is the stricter, safe reading of the hint. A rule-forced ask
|
|
1107
|
+
// (matched_ask_rule) was put to the operator on purpose and is never
|
|
1108
|
+
// auto-approved either.
|
|
1109
|
+
var needsOwnAnswer = ruleForced || opts.suppressAlwaysAllowRule === true || opts.defaultToNo === true;
|
|
1110
|
+
if (kind === "permission" && !needsOwnAnswer && prompts.isGranted(session, toolName, input)) {
|
|
1082
1111
|
return Promise.resolve({ behavior: "allow", updatedInput: input });
|
|
1083
1112
|
}
|
|
1084
1113
|
|
|
@@ -1093,70 +1122,48 @@ function createSDKBridge(opts) {
|
|
|
1093
1122
|
// this hang-fix should bundle silently. Needs a design pass (e.g. scope
|
|
1094
1123
|
// the timeout to sub-agent-owned entries only, or make it operator-
|
|
1095
1124
|
// configurable) rather than copying the worker's blanket 30s.
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1125
|
+
// The registry creates the notification and dismisses it when the
|
|
1126
|
+
// prompt settles.
|
|
1127
|
+
var opened = prompts.open(session, kind, {
|
|
1128
|
+
toolName: toolName,
|
|
1129
|
+
toolInput: input,
|
|
1130
|
+
decisionReason: opts.decisionReason || "",
|
|
1131
|
+
vendor: session.vendor || (adapter && adapter.vendor) || "claude",
|
|
1132
|
+
suppressAlwaysAllowRule: opts.suppressAlwaysAllowRule,
|
|
1133
|
+
defaultToNo: opts.defaultToNo,
|
|
1134
|
+
matchedAskRule: opts.matchedAskRule,
|
|
1135
|
+
}, {
|
|
1136
|
+
toolUseId: opts.toolUseID,
|
|
1137
|
+
signal: opts.signal,
|
|
1138
|
+
notification: {
|
|
1139
|
+
title: permissionPushTitle(toolName, input),
|
|
1140
|
+
body: permissionPushBody(toolName, input),
|
|
1141
|
+
slug: slug,
|
|
1142
|
+
sessionId: session.localId,
|
|
1143
|
+
ownerId: session.ownerId || null,
|
|
1102
1144
|
toolName: toolName,
|
|
1103
1145
|
toolInput: input,
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
};
|
|
1146
|
+
},
|
|
1147
|
+
});
|
|
1107
1148
|
|
|
1108
|
-
|
|
1149
|
+
// No push for a prompt that ended at once (its signal was already aborted).
|
|
1150
|
+
if (pushModule && opened.pending) {
|
|
1151
|
+
var _permPayload = {
|
|
1109
1152
|
type: "permission_request",
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
decisionReason: opts.decisionReason || "",
|
|
1115
|
-
vendor: session.vendor || (adapter && adapter.vendor) || "claude",
|
|
1153
|
+
slug: slug,
|
|
1154
|
+
requestId: opened.requestId,
|
|
1155
|
+
title: permissionPushTitle(toolName, input),
|
|
1156
|
+
body: permissionPushBody(toolName, input),
|
|
1116
1157
|
};
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
type: "permission_request",
|
|
1123
|
-
slug: slug,
|
|
1124
|
-
requestId: requestId,
|
|
1125
|
-
title: permissionPushTitle(toolName, input),
|
|
1126
|
-
body: permissionPushBody(toolName, input),
|
|
1127
|
-
};
|
|
1128
|
-
// Route to session owner only — body carries tool name and input (may contain paths/code).
|
|
1129
|
-
if (session.ownerId && pushModule.sendPushToUser) {
|
|
1130
|
-
pushModule.sendPushToUser(session.ownerId, _permPayload);
|
|
1131
|
-
} else {
|
|
1132
|
-
pushModule.sendPush(_permPayload);
|
|
1133
|
-
}
|
|
1134
|
-
}
|
|
1135
|
-
|
|
1136
|
-
var _nm = getNotificationsModule();
|
|
1137
|
-
if (_nm) {
|
|
1138
|
-
_nm.notify("permission_request", {
|
|
1139
|
-
title: permissionPushTitle(toolName, input),
|
|
1140
|
-
body: permissionPushBody(toolName, input),
|
|
1141
|
-
slug: slug,
|
|
1142
|
-
sessionId: session.localId,
|
|
1143
|
-
ownerId: session.ownerId || null,
|
|
1144
|
-
requestId: requestId,
|
|
1145
|
-
toolName: toolName,
|
|
1146
|
-
toolInput: input,
|
|
1147
|
-
});
|
|
1158
|
+
// Route to session owner only — body carries tool name and input (may contain paths/code).
|
|
1159
|
+
if (session.ownerId && pushModule.sendPushToUser) {
|
|
1160
|
+
pushModule.sendPushToUser(session.ownerId, _permPayload);
|
|
1161
|
+
} else {
|
|
1162
|
+
pushModule.sendPush(_permPayload);
|
|
1148
1163
|
}
|
|
1164
|
+
}
|
|
1149
1165
|
|
|
1150
|
-
|
|
1151
|
-
opts.signal.addEventListener("abort", function() {
|
|
1152
|
-
delete session.pendingPermissions[requestId];
|
|
1153
|
-
delete sm.permissionRequestIndex[requestId];
|
|
1154
|
-
sendAndRecord(session, { type: "permission_cancel", requestId: requestId });
|
|
1155
|
-
onProcessingChanged(); // update cross-project permission badge
|
|
1156
|
-
resolve({ behavior: "deny", message: "Request cancelled" });
|
|
1157
|
-
});
|
|
1158
|
-
}
|
|
1159
|
-
});
|
|
1166
|
+
return opened.answer;
|
|
1160
1167
|
}
|
|
1161
1168
|
|
|
1162
1169
|
/**
|
|
@@ -1289,10 +1296,7 @@ function createSDKBridge(opts) {
|
|
|
1289
1296
|
sessionActivity.bumpGeneration(session);
|
|
1290
1297
|
send({ type: "status", processing: false });
|
|
1291
1298
|
sendAndRecord(session, { type: "thinking_stop" });
|
|
1292
|
-
|
|
1293
|
-
? "\u25a0 Conversation interrupted - tell the model what to do differently."
|
|
1294
|
-
: "Interrupted \u00b7 What should Claude do instead?";
|
|
1295
|
-
sendAndRecord(session, { type: "info", text: interruptMsg });
|
|
1299
|
+
sendAndRecord(session, { type: "info", text: interruptMessageFor(session) });
|
|
1296
1300
|
sendAndRecord(session, { type: "done", code: 0 });
|
|
1297
1301
|
sm.broadcastSessionList();
|
|
1298
1302
|
}
|
|
@@ -1308,10 +1312,7 @@ function createSDKBridge(opts) {
|
|
|
1308
1312
|
if (err.name === "AbortError" || (myAbortController && myAbortController.signal.aborted) || session.taskStopRequested) {
|
|
1309
1313
|
if (!session.destroying) {
|
|
1310
1314
|
sendAndRecord(session, { type: "thinking_stop" });
|
|
1311
|
-
|
|
1312
|
-
? "\u25a0 Conversation interrupted - tell the model what to do differently."
|
|
1313
|
-
: "Interrupted \u00b7 What should Claude do instead?";
|
|
1314
|
-
sendAndRecord(session, { type: "info", text: interruptMsg2 });
|
|
1315
|
+
sendAndRecord(session, { type: "info", text: interruptMessageFor(session) });
|
|
1315
1316
|
sendAndRecord(session, { type: "done", code: 0 });
|
|
1316
1317
|
}
|
|
1317
1318
|
} else if (session.destroying) {
|
|
@@ -1361,23 +1362,23 @@ function createSDKBridge(opts) {
|
|
|
1361
1362
|
var authLinuxUser = authUser && authUser.linuxUser ? authUser.linuxUser : null;
|
|
1362
1363
|
var canAutoLogin = !!authLinuxUser
|
|
1363
1364
|
|| (authUser && authUser.role === "admin");
|
|
1364
|
-
var
|
|
1365
|
+
var authFacts = authRequiredFacts(sessionVendorId(session));
|
|
1365
1366
|
var authMsg = {
|
|
1366
1367
|
type: "auth_required",
|
|
1367
|
-
text:
|
|
1368
|
-
vendor:
|
|
1369
|
-
loginCommand:
|
|
1368
|
+
text: authFacts.title,
|
|
1369
|
+
vendor: authFacts.vendor,
|
|
1370
|
+
loginCommand: authFacts.loginCommand,
|
|
1370
1371
|
linuxUser: authLinuxUser,
|
|
1371
1372
|
canAutoLogin: canAutoLogin,
|
|
1372
1373
|
};
|
|
1373
1374
|
sendAndRecord(session, authMsg);
|
|
1374
1375
|
if (!notifyAuthRequired(
|
|
1375
1376
|
session,
|
|
1376
|
-
|
|
1377
|
+
authFacts.title,
|
|
1377
1378
|
"Open a terminal, then click the URL and follow the instructions.",
|
|
1378
1379
|
authLinuxUser,
|
|
1379
1380
|
canAutoLogin,
|
|
1380
|
-
|
|
1381
|
+
authFacts.loginCommand
|
|
1381
1382
|
)) {
|
|
1382
1383
|
// chat message already sent above
|
|
1383
1384
|
}
|
|
@@ -1451,50 +1452,20 @@ function createSDKBridge(opts) {
|
|
|
1451
1452
|
// unwinding (e.g. rewind_execute nulls session.queryInstance, then the
|
|
1452
1453
|
// user immediately sends a message, starting a fresh query). If that
|
|
1453
1454
|
// happened, session.queryInstance now points at the newer query's
|
|
1454
|
-
// instance, not ours.
|
|
1455
|
-
//
|
|
1456
|
-
//
|
|
1457
|
-
//
|
|
1458
|
-
//
|
|
1459
|
-
//
|
|
1460
|
-
//
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
// once the for-await loop drains. Unconditionally clearing here would
|
|
1469
|
-
// re-orphan the resolver the result handler just preserved. Use the
|
|
1470
|
-
// same shared keep-decision as that handler (see
|
|
1471
|
-
// lib/sdk-permission-ownership.js) rather than duplicating it.
|
|
1472
|
-
var _perm9d4b = partitionSubagentOwnedPermissions(session);
|
|
1473
|
-
// lr-f940 (N3): sweep sm.permissionRequestIndex for every entry this
|
|
1474
|
-
// pass is about to drop (everything not in keptPermissions) — this is
|
|
1475
|
-
// the ONLY cleanup that runs for the worker path, whose fake abort
|
|
1476
|
-
// signal (yoke/adapters/claude.js) never fires the normal
|
|
1477
|
-
// abort-listener deletion. Must run against the PRE-reassignment map.
|
|
1478
|
-
sweepClearedPermissionIndex(sm, session.pendingPermissions, _perm9d4b.keptPermissions);
|
|
1479
|
-
session.pendingPermissions = _perm9d4b.keptPermissions;
|
|
1480
|
-
// Preserve MCP-mode AskUserQuestion entries across turn boundaries.
|
|
1481
|
-
// The MCP path is intentionally stateless: ask_user returns immediately
|
|
1482
|
-
// ("card posted, end your turn") and the answer arrives as a new
|
|
1483
|
-
// user_message on the next turn. Those entries MUST survive this finally
|
|
1484
|
-
// block so ask_user_response can find the toolId and inject the answer.
|
|
1485
|
-
// canUseTool-mode entries hold an open SDK callback that dies with the
|
|
1486
|
-
// query anyway, so those are correctly cleared here.
|
|
1487
|
-
var _keepAskUser = {};
|
|
1488
|
-
for (var _tid in session.pendingAskUser) {
|
|
1489
|
-
var _pau = session.pendingAskUser[_tid];
|
|
1490
|
-
if (_pau && _pau.mode === "mcp") _keepAskUser[_tid] = _pau;
|
|
1491
|
-
}
|
|
1492
|
-
session.pendingAskUser = _keepAskUser;
|
|
1493
|
-
session.pendingElicitations = {};
|
|
1494
|
-
// lr-9d4b: keep activeTaskToolIds/taskIdMap bookkeeping in sync with
|
|
1495
|
-
// whichever Task ids just had a permission preserved above — mirrors
|
|
1496
|
-
// the same reconciliation the 'result' handler performs.
|
|
1497
|
-
retainPreservedTaskBookkeeping(session, _perm9d4b.preservedTaskIds);
|
|
1455
|
+
// instance, not ours. Ending the session's prompts or resetting
|
|
1456
|
+
// isProcessing here would wipe the NEW query's in-flight state out
|
|
1457
|
+
// from under it (approval clicks silently ignored, turn hangs forever)
|
|
1458
|
+
// and flip isProcessing=false while the new query is still live. Only
|
|
1459
|
+
// run the reset when we still own the session (queryInstance is
|
|
1460
|
+
// unchanged or has already been nulled by our own completion path
|
|
1461
|
+
// above) — never when a newer query has taken over.
|
|
1462
|
+
var ownsSession = session.queryInstance === myQueryInstance || session.queryInstance === null;
|
|
1463
|
+
// The registry ends the prompts OUR query opened, except those of a
|
|
1464
|
+
// tracked backgrounded sub-agent, which the operator can still answer;
|
|
1465
|
+
// a replacement query's prompts are never touched.
|
|
1466
|
+
promptsFor(sm).endQuery(session, myQueryInstance, ownsSession);
|
|
1467
|
+
if (ownsSession) {
|
|
1468
|
+
msgProcessor.retainTaskIdMap(session);
|
|
1498
1469
|
|
|
1499
1470
|
// Auto-continue on rate limit (scheduler sessions, or user setting)
|
|
1500
1471
|
// Mark session as done processing so the late rate_limit_event handler
|
|
@@ -1545,7 +1516,7 @@ function createSDKBridge(opts) {
|
|
|
1545
1516
|
|
|
1546
1517
|
var handle;
|
|
1547
1518
|
try {
|
|
1548
|
-
handle = await adapter.createQuery({
|
|
1519
|
+
handle = await adapter.createQuery(withRuntimeEnv({
|
|
1549
1520
|
cwd: cwd,
|
|
1550
1521
|
resumeSessionId: session.cliSessionId,
|
|
1551
1522
|
adapterOptions: {
|
|
@@ -1554,7 +1525,7 @@ function createSDKBridge(opts) {
|
|
|
1554
1525
|
enableFileCheckpointing: true,
|
|
1555
1526
|
},
|
|
1556
1527
|
},
|
|
1557
|
-
});
|
|
1528
|
+
}, session));
|
|
1558
1529
|
} catch (e) {
|
|
1559
1530
|
sendAndRecord(session, { type: "error", text: "Failed to load Claude SDK: " + (e.message || e) });
|
|
1560
1531
|
throw e;
|
|
@@ -1616,7 +1587,7 @@ function createSDKBridge(opts) {
|
|
|
1616
1587
|
async function rollbackConversation(session, numTurns) {
|
|
1617
1588
|
var sessionAdapter = getAdapterForSession(session);
|
|
1618
1589
|
if (sessionAdapter && typeof sessionAdapter.rollbackThread === "function") {
|
|
1619
|
-
await sessionAdapter.rollbackThread(session.cliSessionId, numTurns);
|
|
1590
|
+
await sessionAdapter.rollbackThread(session.cliSessionId, numTurns, withRuntimeEnv({}, session));
|
|
1620
1591
|
}
|
|
1621
1592
|
// Claude: conversation rollback is handled by rewindFiles + local history trim
|
|
1622
1593
|
}
|
|
@@ -1628,7 +1599,12 @@ function createSDKBridge(opts) {
|
|
|
1628
1599
|
|
|
1629
1600
|
async function forkSessionUnified(session, uuid) {
|
|
1630
1601
|
var sessionAdapter = getAdapterForSession(session);
|
|
1631
|
-
var
|
|
1602
|
+
var forkOpts = { upToMessageId: uuid, dir: cwd };
|
|
1603
|
+
// Only an adapter that keeps its own servers (rollbackThread marks it) is
|
|
1604
|
+
// told whose environment the thread ran under; the Claude SDK would take
|
|
1605
|
+
// the extra key as one of its own options.
|
|
1606
|
+
if (typeof sessionAdapter.rollbackThread === "function") withRuntimeEnv(forkOpts, session);
|
|
1607
|
+
var result = await sessionAdapter.forkSession(session.cliSessionId, forkOpts);
|
|
1632
1608
|
if (!result || !result.sessionId) throw new Error("Fork returned no session id");
|
|
1633
1609
|
|
|
1634
1610
|
// Adapters with rollbackThread (e.g. Codex) use local history copy
|
|
@@ -1642,10 +1618,20 @@ function createSDKBridge(opts) {
|
|
|
1642
1618
|
async function startQuery(session, text, images, linuxUser) {
|
|
1643
1619
|
async function ensureVendorReady(vendor) {
|
|
1644
1620
|
if (!vendor) return null;
|
|
1621
|
+
// What the adapter's live session advertises narrows its declaration; the
|
|
1622
|
+
// result is recorded for this vendor and the browser is told.
|
|
1623
|
+
function recordInit(initResult) {
|
|
1624
|
+
sessionCapabilities.recordCapabilities(sm, vendor, initResult && initResult.capabilities);
|
|
1625
|
+
sendVendorRegistry();
|
|
1626
|
+
}
|
|
1645
1627
|
var vendorAdapter = adapters[vendor] || null;
|
|
1646
1628
|
if (!vendorAdapter) {
|
|
1647
1629
|
var yoke = require("./yoke");
|
|
1648
|
-
|
|
1630
|
+
// The shared model list is read in the environment no session adds to
|
|
1631
|
+
// (as the warmup and the vendor switch do), never a session's own: one
|
|
1632
|
+
// user's endpoint must not decide the models other users are shown. A
|
|
1633
|
+
// session's own server is started for it when its query begins.
|
|
1634
|
+
vendorAdapter = await yoke.lazyCreateAdapter(adapters, vendor, withRuntimeEnv({
|
|
1649
1635
|
cwd: cwd,
|
|
1650
1636
|
dangerouslySkipPermissions: dangerouslySkipPermissions,
|
|
1651
1637
|
linuxUser: linuxUser || undefined,
|
|
@@ -1653,9 +1639,9 @@ function createSDKBridge(opts) {
|
|
|
1653
1639
|
clayTls: clayTls,
|
|
1654
1640
|
clayAuthToken: clayAuthToken,
|
|
1655
1641
|
slug: slug,
|
|
1656
|
-
});
|
|
1642
|
+
}), recordInit);
|
|
1657
1643
|
} else if ((!sm.modelsByVendor || !sm.modelsByVendor[vendor]) && typeof vendorAdapter.init === "function") {
|
|
1658
|
-
await vendorAdapter.init({
|
|
1644
|
+
recordInit(await vendorAdapter.init(withRuntimeEnv({
|
|
1659
1645
|
cwd: cwd,
|
|
1660
1646
|
dangerouslySkipPermissions: dangerouslySkipPermissions,
|
|
1661
1647
|
linuxUser: linuxUser || undefined,
|
|
@@ -1663,18 +1649,25 @@ function createSDKBridge(opts) {
|
|
|
1663
1649
|
clayTls: clayTls,
|
|
1664
1650
|
clayAuthToken: clayAuthToken,
|
|
1665
1651
|
slug: slug,
|
|
1666
|
-
});
|
|
1652
|
+
})));
|
|
1667
1653
|
}
|
|
1668
1654
|
if (vendorAdapter) {
|
|
1669
1655
|
sm.availableVendors = Object.keys(adapters);
|
|
1670
1656
|
sm.modelsByVendor = sm.modelsByVendor || {};
|
|
1671
1657
|
if (!sm.modelsByVendor[vendor] && typeof vendorAdapter.supportedModels === "function") {
|
|
1672
|
-
sm.modelsByVendor[vendor] = await vendorAdapter.supportedModels();
|
|
1658
|
+
sm.modelsByVendor[vendor] = await vendorAdapter.supportedModels(withRuntimeEnv({}));
|
|
1673
1659
|
}
|
|
1674
1660
|
}
|
|
1675
1661
|
return vendorAdapter;
|
|
1676
1662
|
}
|
|
1677
1663
|
|
|
1664
|
+
// A vendor that is not registered cannot be started, logged into or
|
|
1665
|
+
// looked up: say so by name instead of falling through to a default.
|
|
1666
|
+
if (session.vendor && !yokeRegistry.isKnown(session.vendor)) {
|
|
1667
|
+
sendAndRecord(session, { type: "error", text: "Unknown agent: " + String(session.vendor) });
|
|
1668
|
+
sendAndRecord(session, { type: "done", code: 1 });
|
|
1669
|
+
return;
|
|
1670
|
+
}
|
|
1678
1671
|
// If vendor is set but adapter not ready, try lazy creation (user may have logged in)
|
|
1679
1672
|
if (session.vendor && !adapters[session.vendor]) {
|
|
1680
1673
|
var lazyAdapter = await ensureVendorReady(session.vendor);
|
|
@@ -1696,7 +1689,8 @@ function createSDKBridge(opts) {
|
|
|
1696
1689
|
}
|
|
1697
1690
|
// If still not available after lazy check, send auth_required
|
|
1698
1691
|
if (session.vendor && !adapters[session.vendor]) {
|
|
1699
|
-
var
|
|
1692
|
+
var authFacts2 = authRequiredFacts(session.vendor);
|
|
1693
|
+
var vendorName = authFacts2.displayName;
|
|
1700
1694
|
var authUser = session.ownerId ? usersModule.findUserById(session.ownerId) : null;
|
|
1701
1695
|
var authLinuxUser = authUser && authUser.linuxUser ? authUser.linuxUser : null;
|
|
1702
1696
|
var canAutoLogin = !!authLinuxUser
|
|
@@ -1713,20 +1707,20 @@ function createSDKBridge(opts) {
|
|
|
1713
1707
|
}
|
|
1714
1708
|
var authMsg2 = {
|
|
1715
1709
|
type: "auth_required",
|
|
1716
|
-
text:
|
|
1717
|
-
vendor:
|
|
1718
|
-
loginCommand:
|
|
1710
|
+
text: authFacts2.title,
|
|
1711
|
+
vendor: authFacts2.vendor,
|
|
1712
|
+
loginCommand: authFacts2.loginCommand,
|
|
1719
1713
|
linuxUser: authLinuxUser,
|
|
1720
1714
|
canAutoLogin: canAutoLogin,
|
|
1721
1715
|
};
|
|
1722
1716
|
sendAndRecord(session, authMsg2);
|
|
1723
1717
|
if (!notifyAuthRequired(
|
|
1724
1718
|
session,
|
|
1725
|
-
|
|
1719
|
+
authFacts2.title,
|
|
1726
1720
|
"Open a terminal, then click the URL and follow the instructions.",
|
|
1727
1721
|
authLinuxUser,
|
|
1728
1722
|
canAutoLogin,
|
|
1729
|
-
|
|
1723
|
+
authFacts2.loginCommand
|
|
1730
1724
|
)) {
|
|
1731
1725
|
// chat message already sent above
|
|
1732
1726
|
}
|
|
@@ -1758,6 +1752,20 @@ function createSDKBridge(opts) {
|
|
|
1758
1752
|
return;
|
|
1759
1753
|
}
|
|
1760
1754
|
|
|
1755
|
+
// Soft-limit pressure / drain: refuse new query processes but leave
|
|
1756
|
+
// sessions that already hold a live query untouched. Checked before the
|
|
1757
|
+
// slot is claimed so a refusal never increments the live counter.
|
|
1758
|
+
var newWorkRefusal = isNewQuery ? getNewWorkRefusal() : null;
|
|
1759
|
+
if (newWorkRefusal) {
|
|
1760
|
+
console.warn("[sdk-bridge] New-work gate closed — refusing new query for session " + session.localId);
|
|
1761
|
+
session.isProcessing = false;
|
|
1762
|
+
onProcessingChanged();
|
|
1763
|
+
sendAndRecord(session, { type: "error", text: newWorkRefusal });
|
|
1764
|
+
sendAndRecord(session, { type: "done", code: 1 });
|
|
1765
|
+
sm.broadcastSessionList();
|
|
1766
|
+
return;
|
|
1767
|
+
}
|
|
1768
|
+
|
|
1761
1769
|
// lr-2d91: MemAvailable gate — check before claiming a slot so a rejected
|
|
1762
1770
|
// query never increments the live counter.
|
|
1763
1771
|
if (isNewQuery) {
|
|
@@ -1907,12 +1915,13 @@ function createSDKBridge(opts) {
|
|
|
1907
1915
|
|
|
1908
1916
|
session.blocks = {};
|
|
1909
1917
|
session.sentToolResults = {};
|
|
1910
|
-
|
|
1911
|
-
|
|
1918
|
+
// Tasks of earlier queries are gone, except one whose prompt the
|
|
1919
|
+
// operator can still answer.
|
|
1920
|
+
promptsFor(sm).beginQuery(session);
|
|
1912
1921
|
session.streamedText = false;
|
|
1913
1922
|
// lr-9bcd7b: PRIMARY leak-resistance layer — bump the activity
|
|
1914
1923
|
// generation at every startQuery call (turn boundary), same spot
|
|
1915
|
-
//
|
|
1924
|
+
// Task tracking is reset above. Any token from a prior generation
|
|
1916
1925
|
// (e.g. a subagent that died silently mid-turn without ever releasing)
|
|
1917
1926
|
// becomes invisible to isSessionActive immediately, with no explicit
|
|
1918
1927
|
// release or timer needed — see lib/session-activity.js header.
|
|
@@ -1941,9 +1950,11 @@ function createSDKBridge(opts) {
|
|
|
1941
1950
|
// settingSources catalog it uses internally — no in-band AgentDefinition
|
|
1942
1951
|
// map is needed (that was a workaround for the old CLI-parse path).
|
|
1943
1952
|
//
|
|
1944
|
-
// Agent injection
|
|
1945
|
-
//
|
|
1946
|
-
//
|
|
1953
|
+
// Agent injection needs the agentInjection capability, as RESOLVED for the
|
|
1954
|
+
// session's vendor (declaration narrowed by what its live session
|
|
1955
|
+
// advertised), never the declaration alone. An adapter that
|
|
1956
|
+
// lacks it (Codex has no equivalent API surface) skips this block. The UI
|
|
1957
|
+
// disables the Agent Chat entry point for such an agent; this guard is
|
|
1947
1958
|
// belt-and-suspenders for sessions that carry agentName from a prior
|
|
1948
1959
|
// vendor switch or a direct ws message.
|
|
1949
1960
|
//
|
|
@@ -1956,7 +1967,8 @@ function createSDKBridge(opts) {
|
|
|
1956
1967
|
// applied when queryModel is computed below (after loop-settings resolution).
|
|
1957
1968
|
var _agentDeclaredModel = null;
|
|
1958
1969
|
|
|
1959
|
-
|
|
1970
|
+
var _injectionVendor = sessionCapabilities.effectiveVendorId(sm, session, sessionAdapter.vendor);
|
|
1971
|
+
if (session.agentName && sessionCapabilities.capabilitiesFor(sm, _injectionVendor).agentInjection) {
|
|
1960
1972
|
claudeOpts.agent = session.agentName;
|
|
1961
1973
|
|
|
1962
1974
|
// lr-3ccc78 — on a shared multi-user daemon (linuxUser set), read the
|
|
@@ -2100,7 +2112,9 @@ function createSDKBridge(opts) {
|
|
|
2100
2112
|
model: queryModel,
|
|
2101
2113
|
effort: ls.effort || sm.currentEffort || undefined,
|
|
2102
2114
|
toolServers: mergedMcpServers,
|
|
2103
|
-
toolServerDescriptors: extractMcpDescriptors(mergedMcpServers)
|
|
2115
|
+
toolServerDescriptors: extractMcpDescriptors(mergedMcpServers, function (diagnostic) {
|
|
2116
|
+
sendAndRecord(session, diagnostic);
|
|
2117
|
+
}) || undefined,
|
|
2104
2118
|
resumeSessionId: session.cliSessionId || undefined,
|
|
2105
2119
|
abortController: linuxUser ? undefined : session.abortController,
|
|
2106
2120
|
canUseTool: function(toolName, input, toolOpts) {
|
|
@@ -2137,6 +2151,9 @@ function createSDKBridge(opts) {
|
|
|
2137
2151
|
},
|
|
2138
2152
|
};
|
|
2139
2153
|
|
|
2154
|
+
// The variables Settings add to this runtime's environment, as of now.
|
|
2155
|
+
withRuntimeEnv(queryOpts, session);
|
|
2156
|
+
|
|
2140
2157
|
var handle;
|
|
2141
2158
|
console.log("[sdk-bridge] calling adapter.createQuery... vendor=" + sessionAdapter.vendor);
|
|
2142
2159
|
try {
|
|
@@ -2303,40 +2320,25 @@ function createSDKBridge(opts) {
|
|
|
2303
2320
|
return text;
|
|
2304
2321
|
}
|
|
2305
2322
|
|
|
2306
|
-
// Detect which vendor binaries are installed for
|
|
2307
|
-
// In multi-user mode
|
|
2323
|
+
// Detect which vendor binaries are installed for an OS identity and record
|
|
2324
|
+
// the result for it. In multi-user mode the lookup runs as that Linux user;
|
|
2325
|
+
// a null user is the daemon's own account.
|
|
2308
2326
|
function detectInstalledVendors(linuxUser) {
|
|
2309
|
-
|
|
2310
|
-
var
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
// name MUST be a value from the allowlist below — shell injection risk if dynamic
|
|
2315
|
-
if (!["claude", "codex"].includes(name)) throw new Error("Disallowed binary name: " + name);
|
|
2316
|
-
try {
|
|
2317
|
-
if (linuxUser) {
|
|
2318
|
-
execFileSync("su", ["-", linuxUser, "-c", "which " + name], { timeout: 3000, stdio: ["pipe", "pipe", "pipe"] });
|
|
2319
|
-
} else {
|
|
2320
|
-
if (process.platform === "win32") execFileSync("where", [name], { timeout: 3000, stdio: ["pipe", "pipe", "pipe"] });
|
|
2321
|
-
else execFileSync("which", [name], { timeout: 3000, stdio: ["pipe", "pipe", "pipe"] });
|
|
2322
|
-
}
|
|
2323
|
-
return true;
|
|
2324
|
-
} catch (e) {
|
|
2325
|
-
return false;
|
|
2326
|
-
}
|
|
2327
|
-
}
|
|
2328
|
-
|
|
2329
|
-
// Claude: check if binary is in PATH
|
|
2330
|
-
if (tryLookup("claude")) result.push("claude");
|
|
2331
|
-
|
|
2332
|
-
// Codex: check bundled binary or PATH
|
|
2333
|
-
var codexBin = null;
|
|
2334
|
-
try {
|
|
2335
|
-
codexBin = require("./yoke/codex-app-server").findCodexPath();
|
|
2336
|
-
} catch (e) {}
|
|
2337
|
-
if ((codexBin && fs.existsSync(codexBin)) || tryLookup("codex")) result.push("codex");
|
|
2327
|
+
// The registry owns the probes and the allowlist of executable names.
|
|
2328
|
+
var state = require("./yoke").detectVendorInstallState(linuxUser || null);
|
|
2329
|
+
recordInstallState(sm, linuxUser || null, state);
|
|
2330
|
+
return state.installed;
|
|
2331
|
+
}
|
|
2338
2332
|
|
|
2339
|
-
|
|
2333
|
+
// The user installed or signed in to an agent after the daemon started: look
|
|
2334
|
+
// again, as the requesting OS identity (null: the daemon's account). The
|
|
2335
|
+
// caller sends the refreshed registry to the clients of that same identity;
|
|
2336
|
+
// install state is per identity, so nobody else receives it.
|
|
2337
|
+
function recheckVendors(linuxUser) {
|
|
2338
|
+
require("./yoke").invalidateAuthCache();
|
|
2339
|
+
_cachedFreshAuthState = null;
|
|
2340
|
+
detectInstalledVendors(linuxUser || null);
|
|
2341
|
+
return buildVendorRegistryMessage(sm, linuxUser || null);
|
|
2340
2342
|
}
|
|
2341
2343
|
|
|
2342
2344
|
// SDK warmup: initialize all available adapters and collect models.
|
|
@@ -2346,10 +2348,16 @@ function createSDKBridge(opts) {
|
|
|
2346
2348
|
var defaultVendor = adapter ? adapter.vendor : "claude";
|
|
2347
2349
|
sm.defaultVendor = defaultVendor;
|
|
2348
2350
|
|
|
2351
|
+
// Every registered vendor's declared capabilities are known up front, so a
|
|
2352
|
+
// session on a vendor other than the project default is gated on what its
|
|
2353
|
+
// own agent can do. The default vendor's entry is narrowed below by what
|
|
2354
|
+
// its live session advertises.
|
|
2355
|
+
sessionCapabilities.seedCapabilities(sm);
|
|
2356
|
+
|
|
2349
2357
|
// Initialize default adapter first (provides skills, slash commands, etc.)
|
|
2350
2358
|
if (adapter) {
|
|
2351
2359
|
try {
|
|
2352
|
-
var result = await adapter.init({
|
|
2360
|
+
var result = await adapter.init(withRuntimeEnv({
|
|
2353
2361
|
cwd: cwd,
|
|
2354
2362
|
dangerouslySkipPermissions: dangerouslySkipPermissions,
|
|
2355
2363
|
linuxUser: linuxUser || undefined,
|
|
@@ -2357,7 +2365,7 @@ function createSDKBridge(opts) {
|
|
|
2357
2365
|
clayTls: clayTls,
|
|
2358
2366
|
clayAuthToken: clayAuthToken,
|
|
2359
2367
|
slug: slug,
|
|
2360
|
-
});
|
|
2368
|
+
}));
|
|
2361
2369
|
|
|
2362
2370
|
// lr-3ccc78 — resolve the session owner's own home on a shared
|
|
2363
2371
|
// multi-user daemon so global skill discovery isn't the daemon's
|
|
@@ -2385,8 +2393,7 @@ function createSDKBridge(opts) {
|
|
|
2385
2393
|
// Store per-vendor models and capabilities
|
|
2386
2394
|
sm.modelsByVendor = sm.modelsByVendor || {};
|
|
2387
2395
|
sm.modelsByVendor[defaultVendor] = result.models || [];
|
|
2388
|
-
sm
|
|
2389
|
-
sm.capabilitiesByVendor[defaultVendor] = result.capabilities || {};
|
|
2396
|
+
sessionCapabilities.recordCapabilities(sm, defaultVendor, result.capabilities);
|
|
2390
2397
|
|
|
2391
2398
|
// lr-1a26: Console-side settings preflight (epic lr-1a52 stage 5/5).
|
|
2392
2399
|
// Non-blocking: runs after warmup succeeds, errors are swallowed so
|
|
@@ -2418,8 +2425,9 @@ function createSDKBridge(opts) {
|
|
|
2418
2425
|
sm.modelsByVendor = sm.modelsByVendor || {};
|
|
2419
2426
|
|
|
2420
2427
|
// Detect installed vendors per-user (binary existence check)
|
|
2421
|
-
|
|
2428
|
+
detectInstalledVendors(linuxUser);
|
|
2422
2429
|
sm.availableVendors = Object.keys(adapters);
|
|
2430
|
+
send(buildVendorRegistryMessage(sm, linuxUser || null));
|
|
2423
2431
|
|
|
2424
2432
|
// Send initial state to client
|
|
2425
2433
|
send({
|
|
@@ -2428,7 +2436,6 @@ function createSDKBridge(opts) {
|
|
|
2428
2436
|
models: getModelsForVendor(defaultVendor),
|
|
2429
2437
|
vendor: defaultVendor,
|
|
2430
2438
|
availableVendors: sm.availableVendors,
|
|
2431
|
-
installedVendors: sm.installedVendors,
|
|
2432
2439
|
});
|
|
2433
2440
|
}
|
|
2434
2441
|
|
|
@@ -2483,18 +2490,26 @@ function createSDKBridge(opts) {
|
|
|
2483
2490
|
}
|
|
2484
2491
|
}
|
|
2485
2492
|
|
|
2493
|
+
// Returns { ok: true } or { ok: false, error } like setModel: the effort is
|
|
2494
|
+
// recorded and announced only once the running query has taken it, and a
|
|
2495
|
+
// failure is shown to the operator instead of reading as a change.
|
|
2486
2496
|
async function setEffort(session, effort) {
|
|
2487
2497
|
if (!session.queryInstance) {
|
|
2488
2498
|
sm.currentEffort = effort;
|
|
2489
2499
|
send({ type: "config_state", model: sm.currentModel || "", mode: sm.currentPermissionMode || "default", effort: sm.currentEffort, betas: sm.currentBetas || [] });
|
|
2490
|
-
return;
|
|
2500
|
+
return { ok: true };
|
|
2491
2501
|
}
|
|
2492
|
-
|
|
2493
|
-
|
|
2502
|
+
try {
|
|
2503
|
+
// Route through QueryHandle (works for both in-process and worker paths)
|
|
2494
2504
|
await session.queryInstance.setEffort(effort);
|
|
2505
|
+
} catch (e) {
|
|
2506
|
+
var errMsg = (e && e.message) || String(e);
|
|
2507
|
+
send({ type: "error", text: "Failed to change effort: " + errMsg });
|
|
2508
|
+
return { ok: false, error: errMsg };
|
|
2495
2509
|
}
|
|
2496
2510
|
sm.currentEffort = effort;
|
|
2497
2511
|
send({ type: "config_state", model: sm.currentModel || "", mode: sm.currentPermissionMode || "default", effort: sm.currentEffort, betas: sm.currentBetas || [] });
|
|
2512
|
+
return { ok: true };
|
|
2498
2513
|
}
|
|
2499
2514
|
|
|
2500
2515
|
async function setPermissionMode(session, mode) {
|
|
@@ -2572,9 +2587,11 @@ function createSDKBridge(opts) {
|
|
|
2572
2587
|
permissionPushTitle: permissionPushTitle,
|
|
2573
2588
|
permissionPushBody: permissionPushBody,
|
|
2574
2589
|
warmup: warmup,
|
|
2590
|
+
recheckVendors: recheckVendors,
|
|
2575
2591
|
stopTask: stopTask,
|
|
2576
2592
|
startIdleReaper: startIdleReaper,
|
|
2577
2593
|
stopIdleReaper: stopIdleReaper,
|
|
2594
|
+
getRuntimeEnvOverlay: getRuntimeEnvOverlay,
|
|
2578
2595
|
getMemoryStats: getMemoryStats,
|
|
2579
2596
|
// lr-62157d: exposed for direct unit testing of the provisional-title
|
|
2580
2597
|
// overwrite guard, following the _test_-prefixed exposure convention
|
|
@@ -2591,5 +2608,5 @@ function getActiveLiveCount() {
|
|
|
2591
2608
|
return _activeLiveCount;
|
|
2592
2609
|
}
|
|
2593
2610
|
|
|
2594
|
-
module.exports = { createSDKBridge, createMessageQueue, readMemAvailableMB, readCgroupHeadroomMB, resolveMemAvailableThresholdMB, computeCgroupBlockCauseMessage, DEFAULT_MEM_AVAILABLE_MIN_MB, DEFAULT_TOKENS_PER_MB_HEADROOM, getActiveLiveCount, getActivityDivergenceStats, buildActivityDiagnosticsResponse };
|
|
2611
|
+
module.exports = { createSDKBridge, createMessageQueue, extractMcpDescriptors, readMemAvailableMB, readCgroupHeadroomMB, resolveMemAvailableThresholdMB, computeCgroupBlockCauseMessage, DEFAULT_MEM_AVAILABLE_MIN_MB, DEFAULT_TOKENS_PER_MB_HEADROOM, getActiveLiveCount, getActivityDivergenceStats, buildActivityDiagnosticsResponse };
|
|
2595
2612
|
|