@commonlyai/cli 0.1.252 → 0.1.254
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/package.json +1 -1
- package/src/commands/agent.js +65 -22
- package/src/lib/grant-broker-guard.js +29 -0
- package/src/lib/windows-command.js +63 -25
package/package.json
CHANGED
package/src/commands/agent.js
CHANGED
|
@@ -81,7 +81,7 @@ import {
|
|
|
81
81
|
withDefaultMcpServer,
|
|
82
82
|
withDefaultSandbox,
|
|
83
83
|
} from '../lib/default-environment.js';
|
|
84
|
-
import { withholdGrantBroker } from '../lib/grant-broker-guard.js';
|
|
84
|
+
import { mergeGrantBrokers, withholdGrantBroker } from '../lib/grant-broker-guard.js';
|
|
85
85
|
import {
|
|
86
86
|
FOCUS_FRAME_MAX_CODE_POINTS,
|
|
87
87
|
formatPodFocusFrame,
|
|
@@ -1667,6 +1667,9 @@ export const performRun = ({
|
|
|
1667
1667
|
// saved on the server before each turn. Off for daemon seats, whose daemon
|
|
1668
1668
|
// projects the same values into `environment`.
|
|
1669
1669
|
readSavedControls = false,
|
|
1670
|
+
// F150: a seat with no daemon reads its live grant brokers before each turn.
|
|
1671
|
+
// Off for daemon seats, whose daemon merges /assigned's into the record.
|
|
1672
|
+
readGrantBrokers = false,
|
|
1670
1673
|
resolveOwnProviderKey = null,
|
|
1671
1674
|
runtimeReportClient = null,
|
|
1672
1675
|
refusalHandoff = null,
|
|
@@ -1744,26 +1747,60 @@ export const performRun = ({
|
|
|
1744
1747
|
return spawnPolicyPromise;
|
|
1745
1748
|
};
|
|
1746
1749
|
|
|
1747
|
-
// THE
|
|
1748
|
-
//
|
|
1749
|
-
//
|
|
1750
|
-
//
|
|
1751
|
-
//
|
|
1752
|
-
//
|
|
1753
|
-
//
|
|
1754
|
-
// (
|
|
1755
|
-
//
|
|
1750
|
+
// THE BROKER REACHES A SEAT AS AN INJECTED MCP ENTRY, and the confinement it
|
|
1751
|
+
// promises is declared in a different field (`sandbox`), which can disagree.
|
|
1752
|
+
// Two sources put the entry there. Under the daemon, its work list
|
|
1753
|
+
// (`/api/agent-binding/assigned`) merges the seat's live grants into the
|
|
1754
|
+
// record before this process starts. Without a daemon (`readGrantBrokers`),
|
|
1755
|
+
// this loop reads `/api/agents/runtime/grant-brokers` itself, before every
|
|
1756
|
+
// turn, and merges the same projection the same way (`mergeGrantBrokers`)
|
|
1757
|
+
// (F150). Either way the entry is then judged HERE by `withholdGrantBroker`,
|
|
1758
|
+
// the same predicate the daemon runs: one refusal with two emitters, so the
|
|
1759
|
+
// server's answer can add a broker and the seat's own guard can still
|
|
1760
|
+
// withhold it (pi, an absent sandbox the grant does not name).
|
|
1756
1761
|
//
|
|
1757
|
-
// IN MEMORY, RECORD UNTOUCHED: the withheld environment is what
|
|
1758
|
-
//
|
|
1759
|
-
//
|
|
1760
|
-
//
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1762
|
+
// IN MEMORY, RECORD UNTOUCHED: the withheld environment is what the adapter
|
|
1763
|
+
// spawns with, and the stored record keeps its own entries, so the daemon
|
|
1764
|
+
// stays the layer that judges a host it knows.
|
|
1765
|
+
//
|
|
1766
|
+
// Read per turn, not once at boot: a grant is revoked or expires while a seat
|
|
1767
|
+
// runs, and one GET beside the per-turn reads already made here is cheap; the
|
|
1768
|
+
// daemon re-reads /assigned every 30s and restarts the seat, which is coarser.
|
|
1769
|
+
// A failed read keeps the last answer: a stale entry reaches nothing, because
|
|
1770
|
+
// the broker re-reads the grant and refuses a revoked or expired one on every
|
|
1771
|
+
// call (`toolBrokerService` → `assertGrantUsable`).
|
|
1772
|
+
let lastRefusalKey = null;
|
|
1773
|
+
const guardedEnvironment = (grantBrokers) => withholdGrantBroker(
|
|
1774
|
+
mergeGrantBrokers(environment, grantBrokers), adapter?.name, {
|
|
1775
|
+
instanceUrl,
|
|
1776
|
+
onRefuse: (refusal, names) => {
|
|
1777
|
+
// Said once per change, not once per turn.
|
|
1778
|
+
const key = `${refusal.reason}:${names.join(',')}`;
|
|
1779
|
+
if (key === lastRefusalKey) return;
|
|
1780
|
+
lastRefusalKey = key;
|
|
1781
|
+
log(`${refusal.code} (${refusal.reason}) — withholding ${names.join(', ')}: ${refusal.detail}`);
|
|
1782
|
+
},
|
|
1783
|
+
},
|
|
1784
|
+
);
|
|
1785
|
+
let lastGrantBrokers = [];
|
|
1786
|
+
let lastServerGrantRefusal = null;
|
|
1787
|
+
let requestedSeatEnvironment = guardedEnvironment(lastGrantBrokers);
|
|
1788
|
+
const refreshGrantBrokers = async () => {
|
|
1789
|
+
if (!readGrantBrokers) return;
|
|
1790
|
+
try {
|
|
1791
|
+
const projection = await client.get('/api/agents/runtime/grant-brokers', { adapter: adapter?.name }, {
|
|
1792
|
+
signal: AbortSignal.timeout(5000),
|
|
1793
|
+
});
|
|
1794
|
+
lastGrantBrokers = Array.isArray(projection?.servers) ? projection.servers : [];
|
|
1795
|
+
const refusal = projection?.refusal || null;
|
|
1796
|
+
const refusalKey = refusal ? `${refusal.reason}` : null;
|
|
1797
|
+
if (refusal && refusalKey !== lastServerGrantRefusal) {
|
|
1798
|
+
log(`${refusal.code} (${refusal.reason}) — a live grant was withheld by the server: ${refusal.detail}`);
|
|
1799
|
+
}
|
|
1800
|
+
lastServerGrantRefusal = refusalKey;
|
|
1801
|
+
} catch { /* keep the last answer; the broker refuses a revoked grant on every call */ }
|
|
1802
|
+
requestedSeatEnvironment = guardedEnvironment(lastGrantBrokers);
|
|
1803
|
+
};
|
|
1767
1804
|
// Without native effort inventory a machine runner cannot promise that a
|
|
1768
1805
|
// stored effort is supported. Preserve the record and use harness default.
|
|
1769
1806
|
// Standalone CLI behavior remains under the operator's control.
|
|
@@ -1860,6 +1897,7 @@ export const performRun = ({
|
|
|
1860
1897
|
let savedControlsNoticeShown = false;
|
|
1861
1898
|
const refreshSeatEnvironment = async () => {
|
|
1862
1899
|
const inventory = await runtimeInventoryPromise;
|
|
1900
|
+
await refreshGrantBrokers();
|
|
1863
1901
|
let desiredEnvironment = requestedSeatEnvironment;
|
|
1864
1902
|
if (readSavedControls) {
|
|
1865
1903
|
try {
|
|
@@ -1871,11 +1909,15 @@ export const performRun = ({
|
|
|
1871
1909
|
...(identity && 'runtimeSourcePodId' in identity ? { runtimeSourcePodId: identity.runtimeSourcePodId } : {}),
|
|
1872
1910
|
});
|
|
1873
1911
|
} catch { /* keep the last controls read; a failed read changes nothing */ }
|
|
1874
|
-
|
|
1912
|
+
// Keyed on the RECORD's environment, not the merged one: grant brokers
|
|
1913
|
+
// (F150) can give a record with none an `mcp` list, and that must not
|
|
1914
|
+
// turn on saved controls the notice below says don't apply.
|
|
1915
|
+
desiredEnvironment = environment
|
|
1916
|
+
? withSavedRuntimeControls(requestedSeatEnvironment, lastSavedControls) : requestedSeatEnvironment;
|
|
1875
1917
|
// A record with no environment (from before attach wrote one) isn't
|
|
1876
1918
|
// given one silently. `agent config` writes one to the record, and from
|
|
1877
1919
|
// then on saved controls apply. Say so once, with the command.
|
|
1878
|
-
if (!
|
|
1920
|
+
if (!environment && lastSavedControls && !savedControlsNoticeShown) {
|
|
1879
1921
|
savedControlsNoticeShown = true;
|
|
1880
1922
|
const flag = (name, value) => (value && /^[A-Za-z0-9._:/-]+$/.test(value) ? ` --${name} ${value}` : '');
|
|
1881
1923
|
const flags = `${flag('model', lastSavedControls.model)}${flag('effort', lastSavedControls.effort)}` || ' --model <model>';
|
|
@@ -4490,6 +4532,7 @@ Docs:
|
|
|
4490
4532
|
machineId: record.machineId, agentName: record.agentName, instanceId: record.instanceId }),
|
|
4491
4533
|
reportRuntime: Boolean(record.machineId || process.env.COMMONLY_MANAGED_RUNNER === '1'),
|
|
4492
4534
|
readSavedControls: !record.machineId && process.env.COMMONLY_MANAGED_RUNNER !== '1',
|
|
4535
|
+
readGrantBrokers: !record.machineId && process.env.COMMONLY_MANAGED_RUNNER !== '1',
|
|
4493
4536
|
spawnCredentialTtlSeconds: resolveSpawnTtlSeconds(process.env),
|
|
4494
4537
|
refusalHandoff: opts.refusalHandoff || null,
|
|
4495
4538
|
log: (line) => console.log(`${stamp()} [${name}] ${line}`),
|
|
@@ -268,6 +268,34 @@ export const withholdGrantBroker = (environment, adapter, {
|
|
|
268
268
|
return { ...environment, mcp: kept };
|
|
269
269
|
};
|
|
270
270
|
|
|
271
|
+
/**
|
|
272
|
+
* Merge the server's grant-broker entries into a seat environment, the way the
|
|
273
|
+
* daemon's work list does it (`GET /api/agent-binding/assigned` in
|
|
274
|
+
* `backend/routes/agentBinding.ts`): an existing entry with a broker's name is
|
|
275
|
+
* replaced, the brokers are appended, and a seat with no environment gets one
|
|
276
|
+
* holding only them. Used by `agent run` without a daemon (F150), whose
|
|
277
|
+
* `GET /api/agents/runtime/grant-brokers` returns the same per-seat projection.
|
|
278
|
+
*
|
|
279
|
+
* Merge only. The caller runs `withholdGrantBroker` on the result, so the
|
|
280
|
+
* client-side refusal still applies to whatever the server sent.
|
|
281
|
+
* Identity is preserved when there is nothing to merge.
|
|
282
|
+
*/
|
|
283
|
+
export const mergeGrantBrokers = (environment, servers) => {
|
|
284
|
+
const brokers = Array.isArray(servers)
|
|
285
|
+
? servers.filter((server) => server && typeof server === 'object' && !Array.isArray(server))
|
|
286
|
+
: [];
|
|
287
|
+
if (!brokers.length) return environment;
|
|
288
|
+
if (!environment) return { mcp: brokers };
|
|
289
|
+
const existing = Array.isArray(environment.mcp) ? environment.mcp : [];
|
|
290
|
+
return {
|
|
291
|
+
...environment,
|
|
292
|
+
mcp: [
|
|
293
|
+
...existing.filter((server) => !brokers.some((broker) => broker.name === server?.name)),
|
|
294
|
+
...brokers,
|
|
295
|
+
],
|
|
296
|
+
};
|
|
297
|
+
};
|
|
298
|
+
|
|
271
299
|
export default {
|
|
272
300
|
GRANT_BROKER_REFUSAL_CODE,
|
|
273
301
|
ENFORCING_MODES,
|
|
@@ -275,4 +303,5 @@ export default {
|
|
|
275
303
|
declaresGrantBroker,
|
|
276
304
|
confinementReason,
|
|
277
305
|
withholdGrantBroker,
|
|
306
|
+
mergeGrantBrokers,
|
|
278
307
|
};
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
// runs as itself; anything else is refused with a reason, never handed to a
|
|
17
17
|
// shell. On every other platform this module is a passthrough.
|
|
18
18
|
|
|
19
|
+
import { spawnSync as realSpawnSync } from 'child_process';
|
|
19
20
|
import { existsSync as realExistsSync, readFileSync as realReadFileSync } from 'fs';
|
|
20
21
|
import { win32 } from 'path';
|
|
21
22
|
|
|
@@ -113,44 +114,79 @@ const programIsNode = (token, shimDir, text) => {
|
|
|
113
114
|
return false;
|
|
114
115
|
};
|
|
115
116
|
|
|
116
|
-
const envValue = (env, name) => {
|
|
117
|
-
const key = Object.keys(env || {}).find((k) => k.toLowerCase() === name.toLowerCase());
|
|
118
|
-
return key ? env[key] : undefined;
|
|
119
|
-
};
|
|
120
|
-
|
|
121
117
|
// npm's own `npm.cmd` / `npx.cmd` (npm/bin/*.cmd, which the Node installer
|
|
122
118
|
// copies next to node.exe) is hand-written, not a cmd-shim:
|
|
123
119
|
//
|
|
124
120
|
// SET "NODE_EXE=%~dp0\node.exe" (else `node`)
|
|
121
|
+
// SET "NPM_PREFIX_JS=%~dp0\node_modules\npm\bin\npm-prefix.js"
|
|
125
122
|
// SET "NPX_CLI_JS=%~dp0\node_modules\npm\bin\npx-cli.js"
|
|
126
123
|
// FOR /F … ('CALL "%NODE_EXE%" "%NPM_PREFIX_JS%"') … SET "NPM_PREFIX_NPX_CLI_JS=%%F\node_modules\npm\bin\npx-cli.js"
|
|
127
124
|
// IF EXIST "%NPM_PREFIX_NPX_CLI_JS%" ( SET "NPX_CLI_JS=%NPM_PREFIX_NPX_CLI_JS%" )
|
|
128
125
|
// "%NODE_EXE%" "%NPX_CLI_JS%" %*
|
|
129
126
|
//
|
|
130
|
-
// The FOR line
|
|
131
|
-
// person upgraded with `npm i -g npm` wins over the
|
|
132
|
-
// is %APPDATA%\npm unless an npmrc
|
|
133
|
-
//
|
|
127
|
+
// The FOR line asks npm for its global prefix (older npm: `npm-cli.js prefix
|
|
128
|
+
// -g`), so an npm the person upgraded with `npm i -g npm` wins over the
|
|
129
|
+
// bundled one. That prefix is %APPDATA%\npm unless an npmrc or
|
|
130
|
+
// `npm_config_prefix` says otherwise, and npm's config loader is the only
|
|
131
|
+
// thing that knows which. So the same script is asked the same way, as
|
|
132
|
+
// `node.exe <script>` with no shell, in the child's env, and in its folder
|
|
133
|
+
// when the caller passes `cwd` (a project .npmrc counts there). An answer that names no npm, or no answer at all,
|
|
134
|
+
// leaves the bundled npm, as npm.cmd does.
|
|
134
135
|
const NPM_LAUNCH = /^"%NODE_EXE%"\s+"%(NP[MX]_CLI_JS)%"\s+%\*$/i;
|
|
136
|
+
const NPM_PREFIX_FOR = /^FOR\s+\/F\s+"delims="\s+%%F\s+IN\s+\('CALL\s+"%NODE_EXE%"\s+"%([A-Z_]+)%"((?:\s+[A-Za-z0-9-]+)*)'\)\s+DO\s+\($/i;
|
|
137
|
+
const PREFIX_TIMEOUT_MS = 10000;
|
|
138
|
+
|
|
139
|
+
const declaredBesideShim = (lines, variable, shimDir) => {
|
|
140
|
+
const declared = lines
|
|
141
|
+
.map((line) => line.match(new RegExp(`^SET\\s+"${variable}=([^"]*)"$`, 'i')))
|
|
142
|
+
.find((match) => match && DP0_PREFIX.test(match[1]));
|
|
143
|
+
return declared ? fromShimDir(declared[1], shimDir) : null;
|
|
144
|
+
};
|
|
135
145
|
|
|
136
|
-
|
|
146
|
+
// The npm script npm.cmd would switch to after asking for the prefix, or
|
|
147
|
+
// null to keep the bundled one.
|
|
148
|
+
const npmFromPrefix = (lines, variable, shimDir, node, deps) => {
|
|
149
|
+
const forLine = lines.map((line) => line.match(NPM_PREFIX_FOR)).find(Boolean);
|
|
150
|
+
if (!forLine) return null;
|
|
151
|
+
const script = declaredBesideShim(lines, forLine[1], shimDir);
|
|
152
|
+
if (!script || !deps.existsSync(script)) return null;
|
|
153
|
+
const target = lines
|
|
154
|
+
.map((line) => line.match(new RegExp(`^SET\\s+"NPM_PREFIX_${variable}=%%F\\\\([^"%]+)"$`, 'i')))
|
|
155
|
+
.find(Boolean);
|
|
156
|
+
if (!target) return null;
|
|
157
|
+
let result;
|
|
158
|
+
try {
|
|
159
|
+
result = deps.spawnSync(node, [script, ...forLine[2].trim().split(/\s+/).filter(Boolean)], {
|
|
160
|
+
env: deps.env,
|
|
161
|
+
cwd: deps.cwd,
|
|
162
|
+
encoding: 'utf8',
|
|
163
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
164
|
+
timeout: PREFIX_TIMEOUT_MS,
|
|
165
|
+
windowsHide: true,
|
|
166
|
+
});
|
|
167
|
+
} catch {
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
if (!result || result.status !== 0) return null;
|
|
171
|
+
// FOR /F runs its body once per line, so the last line is the one that sticks.
|
|
172
|
+
const prefix = String(result.stdout || '').split(/\r?\n/).map((line) => line.trim()).filter(Boolean).pop();
|
|
173
|
+
if (!prefix || !win32.isAbsolute(prefix)) return null;
|
|
174
|
+
const upgraded = win32.join(prefix, target[1]);
|
|
175
|
+
return deps.existsSync(upgraded) ? upgraded : null;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
const parseNpmLauncher = (lines, shimPath, deps) => {
|
|
137
179
|
const launch = lines.map((line) => line.match(NPM_LAUNCH)).find(Boolean);
|
|
138
180
|
if (!launch) return null;
|
|
139
181
|
const variable = launch[1];
|
|
140
182
|
const shimDir = win32.dirname(shimPath);
|
|
141
|
-
const
|
|
142
|
-
.map((line) => line.match(new RegExp(`^SET\\s+"${variable}=([^"]*)"$`, 'i')))
|
|
143
|
-
.find((match) => match && DP0_PREFIX.test(match[1]));
|
|
144
|
-
const bundled = declared ? fromShimDir(declared[1], shimDir) : null;
|
|
183
|
+
const bundled = declaredBesideShim(lines, variable, shimDir);
|
|
145
184
|
if (!bundled) return { command: null, reason: `${shimPath} does not name npm's script beside itself` };
|
|
146
|
-
const appData = envValue(env, 'APPDATA');
|
|
147
|
-
const upgraded = appData
|
|
148
|
-
? win32.join(appData, 'npm', 'node_modules', 'npm', 'bin', win32.basename(bundled))
|
|
149
|
-
: null;
|
|
150
|
-
const script = upgraded && existsSync(upgraded) ? upgraded : bundled;
|
|
151
|
-
if (!existsSync(script)) return { command: null, reason: `${shimPath} points at ${script}, which does not exist` };
|
|
152
185
|
const nodeBeside = win32.join(shimDir, 'node.exe');
|
|
153
|
-
|
|
186
|
+
const node = deps.existsSync(nodeBeside) ? nodeBeside : deps.execPath;
|
|
187
|
+
const script = npmFromPrefix(lines, variable, shimDir, node, deps) || bundled;
|
|
188
|
+
if (!deps.existsSync(script)) return { command: null, reason: `${shimPath} points at ${script}, which does not exist` };
|
|
189
|
+
return { command: node, args: [script] };
|
|
154
190
|
};
|
|
155
191
|
|
|
156
192
|
/**
|
|
@@ -166,14 +202,14 @@ const parseNpmLauncher = (lines, shimPath, { existsSync, execPath, env }) => {
|
|
|
166
202
|
* @returns {{ command: string, args: string[] } | { command: null, reason: string }}
|
|
167
203
|
*/
|
|
168
204
|
export const parseCmdShim = (text, shimPath, {
|
|
169
|
-
existsSync = realExistsSync, execPath = process.execPath, env = {},
|
|
205
|
+
existsSync = realExistsSync, execPath = process.execPath, env = {}, spawnSync = realSpawnSync, cwd,
|
|
170
206
|
} = {}) => {
|
|
171
207
|
const shimDir = win32.dirname(shimPath);
|
|
172
208
|
const lines = String(text)
|
|
173
209
|
.split(/\r?\n/)
|
|
174
210
|
.map((line) => line.trim().replace(/^@/, ''))
|
|
175
211
|
.filter(Boolean);
|
|
176
|
-
const npmLauncher = parseNpmLauncher(lines, shimPath, { existsSync, execPath, env });
|
|
212
|
+
const npmLauncher = parseNpmLauncher(lines, shimPath, { existsSync, execPath, env, spawnSync, cwd });
|
|
177
213
|
if (npmLauncher) return npmLauncher;
|
|
178
214
|
// The launch line is the one that forwards the caller's arguments. The
|
|
179
215
|
// legacy shape has two (node beside the shim, else node on PATH) that name
|
|
@@ -256,7 +292,8 @@ const classify = (file, deps) => {
|
|
|
256
292
|
* it cannot be. On anything but win32, `{ command: name, args: [] }`.
|
|
257
293
|
* On win32, `source` is the file found on PATH (the shim, or the .exe).
|
|
258
294
|
* `cwd` is the child's working directory; relative PATH entries are read
|
|
259
|
-
* against it (unset: this process's, as before)
|
|
295
|
+
* against it (unset: this process's, as before), and npm's own launcher asks
|
|
296
|
+
* npm for its prefix there.
|
|
260
297
|
*
|
|
261
298
|
* @returns {{ command: string, args: string[], source?: string }
|
|
262
299
|
* | { command: null, reason: string, source?: string }}
|
|
@@ -267,10 +304,11 @@ export const resolveWindowsCommandWithReason = (name, {
|
|
|
267
304
|
existsSync = realExistsSync,
|
|
268
305
|
readFileSync = realReadFileSync,
|
|
269
306
|
execPath = process.execPath,
|
|
307
|
+
spawnSync = realSpawnSync,
|
|
270
308
|
cwd,
|
|
271
309
|
} = {}) => {
|
|
272
310
|
if (platform !== 'win32') return { command: name, args: [] };
|
|
273
|
-
const deps = { existsSync, readFileSync, execPath, env };
|
|
311
|
+
const deps = { existsSync, readFileSync, execPath, env, spawnSync, cwd };
|
|
274
312
|
const exts = pathExtensions(env);
|
|
275
313
|
const hasKnownExt = [...BINARY_EXTS, ...SHIM_EXTS].includes(extOf(name));
|
|
276
314
|
// PATHEXT is upper case by convention and NTFS ignores case, so the
|