@commonlyai/cli 0.1.289 → 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 +1 -1
- package/src/commands/agent.js +72 -3
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})`);
|