@commonlyai/cli 0.1.378 → 0.1.379
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 +49 -5
- package/src/lib/adapters/claude.js +1 -1
- package/src/lib/run-preflight.js +149 -0
package/package.json
CHANGED
package/src/commands/agent.js
CHANGED
|
@@ -39,6 +39,9 @@ import { startPoller, terminalDeliveryAckError } from '../lib/poller.js';
|
|
|
39
39
|
import { formatDigestFrame, formatUnreadMessagesFrame } from '../lib/digest-frame.js';
|
|
40
40
|
import { startWebhookServer, forwardToLocalWebhook, logOnlyHandler } from '../lib/webhook-server.js';
|
|
41
41
|
import { getAdapter, listAdapterNames, listPublicAdapterNames } from '../lib/adapters/index.js';
|
|
42
|
+
import {
|
|
43
|
+
harnessFindings, nodeFinding, preflightOutcome, reportListenBlocked,
|
|
44
|
+
} from '../lib/run-preflight.js';
|
|
42
45
|
import {
|
|
43
46
|
getSession,
|
|
44
47
|
getSessionEntry,
|
|
@@ -5891,6 +5894,23 @@ Docs:
|
|
|
5891
5894
|
name = agentArg;
|
|
5892
5895
|
}
|
|
5893
5896
|
}
|
|
5897
|
+
// F515: Node first, before anything uses the token: on a Node without
|
|
5898
|
+
// fetch, the first call fails and reads as a bad token. A first run stops
|
|
5899
|
+
// here and says so, telling the instance when it can; a seat with a saved
|
|
5900
|
+
// record only warns.
|
|
5901
|
+
const oldNode = nodeFinding();
|
|
5902
|
+
const nodeOutcome = preflightOutcome(oldNode ? [oldNode] : [], { firstRun: !record });
|
|
5903
|
+
for (const line of nodeOutcome.lines) console.error(`${stamp()} [${name}] ${line}`);
|
|
5904
|
+
if (nodeOutcome.stop) {
|
|
5905
|
+
const envToken = (process.env.COMMONLY_AGENT_TOKEN || '').trim();
|
|
5906
|
+
if (envToken.startsWith('cm_agent_') && typeof fetch === 'function') {
|
|
5907
|
+
await reportListenBlocked(createClient({
|
|
5908
|
+
instance: (process.env.COMMONLY_API_URL || '').trim() || resolveInstanceUrl(undefined),
|
|
5909
|
+
token: envToken,
|
|
5910
|
+
}), nodeOutcome.report);
|
|
5911
|
+
}
|
|
5912
|
+
process.exit(1);
|
|
5913
|
+
}
|
|
5894
5914
|
let bootstrapped = false;
|
|
5895
5915
|
const saved = record;
|
|
5896
5916
|
const savedRefusal = record && named ? runInstanceRefusal({ record, instanceId }) : null;
|
|
@@ -5924,11 +5944,14 @@ Docs:
|
|
|
5924
5944
|
process.exit(1);
|
|
5925
5945
|
}
|
|
5926
5946
|
if (!record) {
|
|
5927
|
-
|
|
5928
|
-
|
|
5929
|
-
|
|
5930
|
-
|
|
5931
|
-
|
|
5947
|
+
// F515: on Windows the page's lines are PowerShell's ($env:…), and in
|
|
5948
|
+
// Command Prompt they set nothing, so this is where that run lands.
|
|
5949
|
+
console.error(process.platform === 'win32'
|
|
5950
|
+
? `No token for '${name}'. Run all the lines from the Connect your own agent page together, `
|
|
5951
|
+
+ 'in PowerShell (not Command Prompt): the $env: lines set the token for this window.'
|
|
5952
|
+
: `No token for '${name}'. Either export COMMONLY_API_URL + COMMONLY_AGENT_TOKEN`
|
|
5953
|
+
+ ` (shown on the Connect your own agent page) and re-run, or:`
|
|
5954
|
+
+ ` ${CLI} agent attach <adapter> --pod <podId> --name ${agentArg}`);
|
|
5932
5955
|
process.exit(1);
|
|
5933
5956
|
}
|
|
5934
5957
|
const instanceRefusal = named ? runInstanceRefusal({ record, instanceId }) : null;
|
|
@@ -5939,6 +5962,27 @@ Docs:
|
|
|
5939
5962
|
bootstrapped = true;
|
|
5940
5963
|
}
|
|
5941
5964
|
|
|
5965
|
+
// F515: the harness this record names, installed and signed in, before
|
|
5966
|
+
// the run starts waiting. A first run has already looked for the harness
|
|
5967
|
+
// (and reported a missing one); it stops on a signed-out one too, and the
|
|
5968
|
+
// instance hears why, so the page and the pod can say it. A seat with a
|
|
5969
|
+
// saved record only warns: a probe that reads it wrong must not stop it.
|
|
5970
|
+
const harnessProblems = await harnessFindings({
|
|
5971
|
+
adapterName: record.adapter,
|
|
5972
|
+
adapter: getAdapter(record.adapter),
|
|
5973
|
+
environment: record.environment,
|
|
5974
|
+
checkInstalled: !bootstrapped,
|
|
5975
|
+
});
|
|
5976
|
+
const harnessOutcome = preflightOutcome(harnessProblems, { firstRun: bootstrapped });
|
|
5977
|
+
for (const line of harnessOutcome.lines) console.error(`${stamp()} [${name}] ${line}`);
|
|
5978
|
+
if (harnessOutcome.stop) {
|
|
5979
|
+
await reportListenBlocked(
|
|
5980
|
+
createClient({ instance: record.instanceUrl, token: record.runtimeToken }),
|
|
5981
|
+
harnessOutcome.report,
|
|
5982
|
+
);
|
|
5983
|
+
process.exit(1);
|
|
5984
|
+
}
|
|
5985
|
+
|
|
5942
5986
|
// On native Windows the user may choose to run without an OS
|
|
5943
5987
|
// sandbox, by typing the flag or by installing the daemon with it (the
|
|
5944
5988
|
// marker is read only by the seats that daemon spawns). Nothing from the
|
|
@@ -1370,7 +1370,7 @@ const createMcpConfig = async (mcpServers, ctx = {}) => {
|
|
|
1370
1370
|
// child environment and to every detection lookup so a daemon does not report
|
|
1371
1371
|
// a false "adapter unavailable" (or hit spawn ENOENT) merely because it was
|
|
1372
1372
|
// started outside an interactive shell.
|
|
1373
|
-
const withClaudePath = (input) => {
|
|
1373
|
+
export const withClaudePath = (input) => {
|
|
1374
1374
|
const output = { ...(input || process.env) };
|
|
1375
1375
|
const localBin = join(homedir(), '.local', 'bin');
|
|
1376
1376
|
// Windows keys are case-insensitive, and a child given both `Path` and a
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// F515: what `agent run` checks before it starts waiting. Both newcomers in the
|
|
2
|
+
// week of 11-09 created an agent and never got it listening: the page waited,
|
|
3
|
+
// and the terminal said nothing they could act on. So the run checks the three
|
|
4
|
+
// things the connect command needs (Node 22+, the harness installed, the
|
|
5
|
+
// harness signed in), and prints one plain line for each that fails.
|
|
6
|
+
//
|
|
7
|
+
// A first run (the pasted command, no saved record) stops on a failure, and
|
|
8
|
+
// tells the instance why, by reason code only, so the page and the pod can say
|
|
9
|
+
// it. A run with a saved record only warns: that is a seat that has run here
|
|
10
|
+
// before, and a login probe that reads it wrong must not take it down.
|
|
11
|
+
|
|
12
|
+
import { spawn } from 'child_process';
|
|
13
|
+
import { readFileSync } from 'fs';
|
|
14
|
+
|
|
15
|
+
import { checkNode, FAIL } from './doctor.js';
|
|
16
|
+
import { loginState } from './adapter-login.js';
|
|
17
|
+
import { missingHarnessHint } from './missing-harness.js';
|
|
18
|
+
import { withClaudePath } from './adapters/claude.js';
|
|
19
|
+
import { resolveWindowsCommandWithReason } from './windows-command.js';
|
|
20
|
+
|
|
21
|
+
// The reason codes the instance accepts (POST /api/agents/runtime/listen-blocked).
|
|
22
|
+
// A code, never text: nothing from this computer but the code is sent.
|
|
23
|
+
export const LISTEN_BLOCKED_REASONS = ['node-too-old', 'harness-signed-out'];
|
|
24
|
+
|
|
25
|
+
// The floor the CLI declares, read as `commonly doctor` reads it.
|
|
26
|
+
const CLI_ENGINES_NODE = (() => {
|
|
27
|
+
try {
|
|
28
|
+
return JSON.parse(readFileSync(new URL('../../package.json', import.meta.url), 'utf8')).engines?.node;
|
|
29
|
+
} catch {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
})();
|
|
33
|
+
|
|
34
|
+
const LABEL = { claude: 'Claude Code', codex: 'Codex' };
|
|
35
|
+
const SIGN_IN = { claude: 'claude', codex: 'codex login' };
|
|
36
|
+
|
|
37
|
+
/** The Node finding, or null when this Node meets the CLI's floor. */
|
|
38
|
+
export const nodeFinding = ({ nodeVersion = process.versions.node, enginesRange = CLI_ENGINES_NODE } = {}) => {
|
|
39
|
+
const check = checkNode({ nodeVersion, enginesRange });
|
|
40
|
+
if (check.status !== FAIL) return null;
|
|
41
|
+
const floor = /(\d+)/.exec(String(enginesRange || ''))?.[1] || '22';
|
|
42
|
+
return {
|
|
43
|
+
reason: 'node-too-old',
|
|
44
|
+
harness: null,
|
|
45
|
+
line: `This computer has Node ${nodeVersion}, and Commonly needs Node ${floor} or later. `
|
|
46
|
+
+ `Install Node ${floor} from nodejs.org, then run this line again.`,
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The exit code of a harness's login status, run as its adapter runs it: the
|
|
52
|
+
* same command `detect()` found (Claude Code with its own ~/.local/bin on
|
|
53
|
+
* PATH, where the native installer puts it), without a shell. Through a shell
|
|
54
|
+
* on Windows a name cmd can't find exits 1, which is the signed-out code, and
|
|
55
|
+
* would stop a run that is signed in. null when the command can't be resolved
|
|
56
|
+
* or the probe can't run or times out (no verdict).
|
|
57
|
+
*/
|
|
58
|
+
export const runHarnessLoginStatus = (name, args, {
|
|
59
|
+
env = process.env,
|
|
60
|
+
platform = process.platform,
|
|
61
|
+
resolve = resolveWindowsCommandWithReason,
|
|
62
|
+
spawnFn = spawn,
|
|
63
|
+
timeoutMs = 10_000,
|
|
64
|
+
} = {}) => new Promise((done) => {
|
|
65
|
+
const childEnv = name === 'claude' ? withClaudePath(env) : env;
|
|
66
|
+
const located = resolve(name, { env: childEnv, platform });
|
|
67
|
+
if (!located?.command) {
|
|
68
|
+
done(null);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
let child;
|
|
72
|
+
try {
|
|
73
|
+
child = spawnFn(located.command, [...(located.args || []), ...args], {
|
|
74
|
+
stdio: 'ignore', windowsHide: true, env: childEnv, shell: false,
|
|
75
|
+
});
|
|
76
|
+
} catch {
|
|
77
|
+
done(null);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const timer = setTimeout(() => { child.kill(); done(null); }, timeoutMs);
|
|
81
|
+
child.on('error', () => { clearTimeout(timer); done(null); });
|
|
82
|
+
child.on('exit', (code) => { clearTimeout(timer); done(typeof code === 'number' ? code : null); });
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The harness findings for one adapter: not installed, or installed and not
|
|
87
|
+
* signed in. `checkInstalled` is false where the caller has already looked (a
|
|
88
|
+
* first run's bootstrap does, and reports it on its own route). A seat that
|
|
89
|
+
* runs on its own provider block needs no harness login, and a key in the
|
|
90
|
+
* environment is skipped by loginState itself.
|
|
91
|
+
*/
|
|
92
|
+
export const harnessFindings = async ({
|
|
93
|
+
adapterName,
|
|
94
|
+
adapter,
|
|
95
|
+
environment = null,
|
|
96
|
+
checkInstalled = true,
|
|
97
|
+
env = process.env,
|
|
98
|
+
runStatus = (name, args) => runHarnessLoginStatus(name, args, { env }),
|
|
99
|
+
}) => {
|
|
100
|
+
if (!adapter || !adapterName) return [];
|
|
101
|
+
if (checkInstalled) {
|
|
102
|
+
let installed = false;
|
|
103
|
+
try { installed = Boolean(await adapter.detect()); } catch { /* not installed */ }
|
|
104
|
+
if (!installed) {
|
|
105
|
+
return [{
|
|
106
|
+
reason: 'harness-missing',
|
|
107
|
+
harness: adapterName,
|
|
108
|
+
line: `${LABEL[adapterName] || adapterName} isn't installed on this computer. ${missingHarnessHint(adapterName)}`,
|
|
109
|
+
}];
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (environment?.provider !== undefined || !LABEL[adapterName]) return [];
|
|
113
|
+
const signedIn = await loginState(adapterName, { runStatus, env });
|
|
114
|
+
if (signedIn !== false) return [];
|
|
115
|
+
return [{
|
|
116
|
+
reason: 'harness-signed-out',
|
|
117
|
+
harness: adapterName,
|
|
118
|
+
line: `${LABEL[adapterName]} is installed but not signed in on this computer. `
|
|
119
|
+
+ `Run ${SIGN_IN[adapterName]} once to sign in, then run this line again.`,
|
|
120
|
+
}];
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* What the run does with its findings. A first run (the pasted command) stops,
|
|
125
|
+
* and the first finding is the one reported. A run from a saved record only
|
|
126
|
+
* warns and goes on: that seat has run here before, and a probe that misreads
|
|
127
|
+
* it must not take it down.
|
|
128
|
+
*/
|
|
129
|
+
export const preflightOutcome = (findings, { firstRun }) => {
|
|
130
|
+
if (!findings.length) return { stop: false, lines: [], report: null };
|
|
131
|
+
if (firstRun) return { stop: true, lines: findings.map((f) => f.line), report: findings[0] };
|
|
132
|
+
return { stop: false, lines: findings.map((f) => `warning: ${f.line}`), report: null };
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Tell the instance why this agent isn't listening yet. Best effort, as the
|
|
137
|
+
* harness-missing report: an older instance without the route, or a failed
|
|
138
|
+
* call, must not hide the line the person needs in the terminal (which is
|
|
139
|
+
* printed first). A missing harness has its own route, posted by bootstrap.
|
|
140
|
+
*/
|
|
141
|
+
export const reportListenBlocked = async (client, finding) => {
|
|
142
|
+
if (!finding) return;
|
|
143
|
+
try {
|
|
144
|
+
if (!LISTEN_BLOCKED_REASONS.includes(finding.reason)) return;
|
|
145
|
+
await client?.post?.('/api/agents/runtime/listen-blocked', { reason: finding.reason, harness: finding.harness });
|
|
146
|
+
} catch {
|
|
147
|
+
// The terminal line is the answer either way.
|
|
148
|
+
}
|
|
149
|
+
};
|