@commonlyai/cli 0.1.288 → 0.1.290
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
package/src/commands/agent.js
CHANGED
|
@@ -226,6 +226,66 @@ export const savedSeatLacksDefaultSandbox = (record) => Boolean(record)
|
|
|
226
226
|
|| (typeof record.environment === 'object' && !Array.isArray(record.environment)
|
|
227
227
|
&& withDefaultSandbox(record.environment) !== record.environment));
|
|
228
228
|
|
|
229
|
+
/**
|
|
230
|
+
* A key reissued for an agent that already ran here reaches `run` in the
|
|
231
|
+
* environment, beside the saved token it replaced. The fresh one wins only when
|
|
232
|
+
* the saved one is rejected and the server says the fresh one is this agent's.
|
|
233
|
+
* The server keeps only a hash, so overwriting a saved token that works can't be
|
|
234
|
+
* undone: an export for a same-named agent on another instance would lose it.
|
|
235
|
+
*/
|
|
236
|
+
export const adoptEnvToken = async ({
|
|
237
|
+
record,
|
|
238
|
+
name,
|
|
239
|
+
env = process.env,
|
|
240
|
+
clientFactory = createClient,
|
|
241
|
+
log = () => {},
|
|
242
|
+
}) => {
|
|
243
|
+
const runtimeToken = (env.COMMONLY_AGENT_TOKEN || '').trim();
|
|
244
|
+
if (!runtimeToken || runtimeToken === record.runtimeToken) return record;
|
|
245
|
+
const keepSaved = (why) => {
|
|
246
|
+
log(`COMMONLY_AGENT_TOKEN ${why}, so this run uses the saved token`);
|
|
247
|
+
return record;
|
|
248
|
+
};
|
|
249
|
+
if (!runtimeToken.startsWith('cm_agent_')) return keepSaved('is not a runtime token');
|
|
250
|
+
|
|
251
|
+
const whoIs = (instance, token) => clientFactory({ instance, token })
|
|
252
|
+
.get('/api/agents/runtime/installations');
|
|
253
|
+
try {
|
|
254
|
+
await whoIs(record.instanceUrl, record.runtimeToken);
|
|
255
|
+
return keepSaved('differs from the saved token, which still works');
|
|
256
|
+
} catch (err) {
|
|
257
|
+
// 401 alone, unlike the poller: every token refusal here is a 401, a 403
|
|
258
|
+
// comes from something in front of the app, and this one call overwrites.
|
|
259
|
+
if (err?.status !== 401) {
|
|
260
|
+
return keepSaved(`was not used, because the saved token could not be checked (${err.message})`);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const instanceUrl = (env.COMMONLY_API_URL || '').trim() || record.instanceUrl;
|
|
265
|
+
let identity;
|
|
266
|
+
try {
|
|
267
|
+
identity = await whoIs(instanceUrl, runtimeToken);
|
|
268
|
+
} catch (err) {
|
|
269
|
+
return keepSaved(`could not be checked against ${instanceUrl} (${err.message})`);
|
|
270
|
+
}
|
|
271
|
+
const lower = (value) => String(value).toLowerCase();
|
|
272
|
+
if (lower(identity?.agentName) !== lower(record.agentName || name)
|
|
273
|
+
|| lower(identity?.instanceId || 'default') !== lower(record.instanceId || 'default')) {
|
|
274
|
+
return keepSaved(`belongs to another agent (${identity?.agentName}, instance ${identity?.instanceId || 'default'})`);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// A reinstall can move the agent, and the saved pod is then gone.
|
|
278
|
+
const active = (identity?.installations || []).filter((row) => row?.status === 'active');
|
|
279
|
+
const stillThere = active.some((row) => String(row.podId) === String(record.podId));
|
|
280
|
+
const podId = stillThere
|
|
281
|
+
? record.podId
|
|
282
|
+
: (active.find((row) => row.type === 'installation') || active[0])?.podId || record.podId;
|
|
283
|
+
log('the saved token was rejected, so this run uses COMMONLY_AGENT_TOKEN and saves it');
|
|
284
|
+
return {
|
|
285
|
+
...record, runtimeToken, instanceUrl, podId,
|
|
286
|
+
};
|
|
287
|
+
};
|
|
288
|
+
|
|
229
289
|
/** Return the adapter binding resolved for this exact installation by the server. */
|
|
230
290
|
export const runtimeAdapterForInstallation = ({ installations, podId, instanceId } = {}) => {
|
|
231
291
|
if (!Array.isArray(installations) || !podId) return null;
|
|
@@ -2134,7 +2194,7 @@ export const performRun = ({
|
|
|
2134
2194
|
let running = true;
|
|
2135
2195
|
// Stop-after-N-auth-failures: without this, a revoked token leaves the
|
|
2136
2196
|
// poller hammering 401s forever at 60s backoff — invisible to the user.
|
|
2137
|
-
// Exiting
|
|
2197
|
+
// Exiting names the two ways out: a reissued key, or detach.
|
|
2138
2198
|
// 3 is deliberate — 1 would churn on a token-rotation race during
|
|
2139
2199
|
// reprovision-all; 5+ wastes rate-limit budget after the real-revoke case.
|
|
2140
2200
|
let consecutiveAuthErrors = 0;
|
|
@@ -4572,7 +4632,8 @@ export const performRun = ({
|
|
|
4572
4632
|
if (consecutiveAuthErrors >= MAX_AUTH_ERRORS) {
|
|
4573
4633
|
onError?.(new Error(
|
|
4574
4634
|
`Runtime token rejected ${consecutiveAuthErrors} times in a row — stopping. `
|
|
4575
|
-
+ `
|
|
4635
|
+
+ `If this agent's key was reissued, run it again with the new key in COMMONLY_AGENT_TOKEN. `
|
|
4636
|
+
+ `To remove the agent from its pod: commonly agent detach ${agentName}`,
|
|
4576
4637
|
));
|
|
4577
4638
|
running = false;
|
|
4578
4639
|
return;
|
|
@@ -5242,7 +5303,14 @@ Docs:
|
|
|
5242
5303
|
}
|
|
5243
5304
|
let record = loadAgentToken(name);
|
|
5244
5305
|
let bootstrapped = false;
|
|
5245
|
-
|
|
5306
|
+
const saved = record;
|
|
5307
|
+
if (record) {
|
|
5308
|
+
record = await adoptEnvToken({
|
|
5309
|
+
record,
|
|
5310
|
+
name,
|
|
5311
|
+
log: (line) => console.log(`${stamp()} [${name}] ${line}`),
|
|
5312
|
+
});
|
|
5313
|
+
} else {
|
|
5246
5314
|
// First run on this machine: the BYO connect page hands out env vars,
|
|
5247
5315
|
// not a token file — bootstrap the record from them (#913).
|
|
5248
5316
|
try {
|
|
@@ -5334,6 +5402,7 @@ Docs:
|
|
|
5334
5402
|
console.error(`${stamp()} [${name}] ${hostRefusal}`);
|
|
5335
5403
|
process.exit(1);
|
|
5336
5404
|
}
|
|
5405
|
+
if (saved && record !== saved) saveAgentToken(name, record);
|
|
5337
5406
|
if (bootstrapped) {
|
|
5338
5407
|
saveAgentToken(record.agentName, record);
|
|
5339
5408
|
console.log(`${stamp()} [${name}] bootstrapped ${tokenFile(record.agentName)} from COMMONLY_AGENT_TOKEN (adapter: ${record.adapter})`);
|
|
@@ -65,8 +65,9 @@ const jsonBody404 = (text) => {
|
|
|
65
65
|
}
|
|
66
66
|
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return false;
|
|
67
67
|
const error = parsed.error && typeof parsed.error === 'object' ? parsed.error : parsed;
|
|
68
|
-
const
|
|
69
|
-
|
|
68
|
+
const statuses = [parsed.status, parsed.code, error.status, error.code];
|
|
69
|
+
if (statuses.some((value) => Number(value) >= 500)) return false;
|
|
70
|
+
const is404 = statuses.some((value) => String(value) === '404') || error.type === 'not_found_error';
|
|
70
71
|
const message = error.message ?? parsed.message;
|
|
71
72
|
return is404 && typeof message === 'string' && modelShaped(message);
|
|
72
73
|
};
|
|
@@ -114,11 +115,13 @@ export const modelUnavailableFailure = (error, { fallbackModel = null } = {}) =>
|
|
|
114
115
|
const lines = text.split('\n');
|
|
115
116
|
const keyScope = NOT_AVAILABLE_FOR_KEY_RE.test(text) && (attached !== null ? attached === 403
|
|
116
117
|
: lines.some((line) => STATUS_403_RE.test(line) && NOT_AVAILABLE_FOR_KEY_RE.test(line)));
|
|
118
|
+
// The attached status reads the message, so its model shape must be on the
|
|
119
|
+
// message's own first line, not in what later stderr happens to mention.
|
|
117
120
|
const notFound = attached !== null
|
|
118
|
-
? attached === 404 && (
|
|
121
|
+
? attached === 404 && (modelShaped(String(error.message).split('\n')[0]) || jsonBody404(text))
|
|
119
122
|
: sameLine404(text) || jsonBody404(text);
|
|
120
123
|
const matched = keyScope
|
|
121
|
-
|| error.providerCode === 'model_not_found'
|
|
124
|
+
|| (error.providerCode === 'model_not_found' && !(attached >= 500))
|
|
122
125
|
|| notFound;
|
|
123
126
|
if (!matched) return null;
|
|
124
127
|
for (const pattern of MODEL_NAME_RES) {
|
package/src/lib/spawn-retry.js
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
import { PUBLIC_MCP_NOT_READY_CODE, PUBLIC_TOOL_FAILED_CODE } from './public-tool-error.js';
|
|
11
11
|
import { PUBLIC_CODEX_VERSION_CODE } from './adapters/tested-version.js';
|
|
12
|
+
import { modelUnavailableFailure } from './model-unavailable.js';
|
|
12
13
|
|
|
13
14
|
export const SPAWN_FAILURE_CLASS = Object.freeze({
|
|
14
15
|
// The provider refused the request's CONTENT. Re-sending the same content
|
|
@@ -125,6 +126,8 @@ const THROTTLE_WORDING_RE = /(?:rate[ -]?limit|too many requests|overloaded|capa
|
|
|
125
126
|
const RATE_LIMIT_RE = /(?:rate[ -]?limit|too many requests|\b429\b|overloaded|capacity)/i;
|
|
126
127
|
const CONFIGURATION_RE = /(?:ENOENT|command not found|not on PATH|login required|not logged in|invalid api key|authentication failed|unauthori[sz]ed|forbidden|\b40[13]\b)/i;
|
|
127
128
|
|
|
129
|
+
const CONNECTION_FAULT_RE = /(?:ECONNRESET|ECONNREFUSED|ETIMEDOUT|EPIPE|socket hang up|fetch failed)/;
|
|
130
|
+
|
|
128
131
|
const errorText = (error) => [
|
|
129
132
|
error?.message,
|
|
130
133
|
error?.stderr,
|
|
@@ -170,11 +173,17 @@ export const classifySpawnFailure = (error) => {
|
|
|
170
173
|
if (error?.status === 429 || RATE_LIMIT_RE.test(text)) {
|
|
171
174
|
return SPAWN_FAILURE_CLASS.RATE_LIMIT;
|
|
172
175
|
}
|
|
176
|
+
// A model the provider won't run fails the same way until someone changes it.
|
|
177
|
+
// A 403 for that reads as configuration below; a 404 has no status of its own
|
|
178
|
+
// there, so without this it retried in seconds against the same answer. A
|
|
179
|
+
// turn's text can hold a tool's 404 from before a dropped connection killed
|
|
180
|
+
// the turn, so a connection fault keeps the quick retry.
|
|
173
181
|
if (
|
|
174
182
|
error?.code === 'ENOENT'
|
|
175
183
|
|| error?.status === 401
|
|
176
184
|
|| error?.status === 403
|
|
177
185
|
|| CONFIGURATION_RE.test(text)
|
|
186
|
+
|| (modelUnavailableFailure(error) && !CONNECTION_FAULT_RE.test(text))
|
|
178
187
|
) {
|
|
179
188
|
return SPAWN_FAILURE_CLASS.CONFIGURATION;
|
|
180
189
|
}
|