@commonlyai/cli 0.1.253 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commonlyai/cli",
3
- "version": "0.1.253",
3
+ "version": "0.1.254",
4
4
  "license": "Apache-2.0",
5
5
  "description": "The Commonly CLI — connect agents, manage pods, iterate fast",
6
6
  "type": "module",
@@ -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 SEAT'S OWN RECORD IS THE ONLY DECLARATION THERE IS. The broker reaches a
1748
- // seat as an injected MCP entry written by hand into its environment — no
1749
- // server projection puts it there (`GRANT_BROKER_URL` is imported by one
1750
- // route, the daemon assignment, and that one already refuses). So a seat run
1751
- // by `commonly agent run` under launchd spawns the entry verbatim, and the
1752
- // confinement it promises is declared in a different field (`sandbox`), which
1753
- // can disagree. The daemon half of this refusal already runs
1754
- // (`daemon-supervisor.js`); this is the same predicate at the only other site
1755
- // that can see the record — same code, so one refusal with two emitters.
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
- // the adapter spawns with, and the stored record keeps its broker, so the
1759
- // daemon stays the layer that judges a host it knows. Applied once for the
1760
- // run rather than per turn: `agent run` reads its record once at boot.
1761
- const requestedSeatEnvironment = withholdGrantBroker(environment, adapter?.name, {
1762
- instanceUrl,
1763
- onRefuse: (refusal, names) => log(
1764
- `${refusal.code} (${refusal.reason}) — withholding ${names.join(', ')}: ${refusal.detail}`,
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
- desiredEnvironment = withSavedRuntimeControls(requestedSeatEnvironment, lastSavedControls);
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 (!requestedSeatEnvironment && lastSavedControls && !savedControlsNoticeShown) {
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
  };