@koalaauto/agentchannel 3.78.1 → 3.79.0

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/dist/src/args.js CHANGED
@@ -185,7 +185,7 @@ Tasks: ach send --to <agent> --title <t> --priority P0..P3 --purpose <p> --
185
185
  [--review] (the task waits for acceptance when its worker is done: ach accept or ach request-changes)
186
186
  ach accept <task> [<note>] | ach request-changes <task> "<what to change>" (the sender's side judges work sent with --review)
187
187
  ach handoff <taskId> --note <change note> [--file <f>] [--priority P0..P3] [--purpose p] [--money m] [--key-question q] [--attach <file> ...]
188
- [--channel <name>] (default: like --repo, else the team agent's channel, else #general)
188
+ [--channel <name>] (default: like --repo, else the team bot's channel, else #general)
189
189
  ach task <id|TASK-128> (lists attachments with download URLs) | ach inbox [--owner]
190
190
  Every task argument also takes the TASK-<seq> number shown in the panel (TASK-128; T-128 still works).
191
191
  ach download <taskId> [--out <dir>] (fetches every attachment; default ./ach-files-<taskId>)
@@ -8,7 +8,19 @@ export declare function runInTerminal(line: string, env: NodeJS.ProcessEnv): Pro
8
8
  * with npx or `node dist/src/cli.js`). Returns the dir to prepend to PATH.
9
9
  */
10
10
  export declare function writeAchShim(home: string): string | undefined;
11
+ /**
12
+ * `ach run`. A key that is missing or that the hub refuses does not end it: the runner waits (backing off, reading the config again
13
+ * each time, so a new `ach join` here is picked up) and starts again once the hub takes the key. 2026-11-24: during the ids cutover
14
+ * the hub refused every key for 3 minutes, and runners that exited for good on that 401 stayed down until someone restarted them.
15
+ */
11
16
  export declare function runCommand(p: Parsed, io: Io): Promise<number>;
17
+ /** Waits between tries for a key that works: 30 s, then longer, at most 15 minutes. ACH_AUTH_RETRY_MS: one fixed wait (tests). */
18
+ export declare const KEY_RETRY_MS: number[];
19
+ /**
20
+ * Until the hub takes this agent's key: read the config (a new `ach join` may have written a new key), ask the hub who this key is,
21
+ * wait and try again. Resolves true when the key works, false on SIGINT/SIGTERM.
22
+ */
23
+ export declare function waitForKey(io: Io, why: string): Promise<boolean>;
12
24
  /**
13
25
  * `ach run` where the agent's key is not stored: the key lives only on its own machine and the hub keeps a hash of it, so it
14
26
  * cannot be read back. The way back is a new key (the panel: the agent's page, Settings or the stopped-runner card) and its join line.
@@ -17,9 +29,6 @@ export declare const NO_KEY_MESSAGE: {
17
29
  readonly en: "ach run: there is no key for this agent on this machine (deleted, or a new machine or directory).\nA key cannot be recovered: in the panel open the agent and rotate its key (an admin or its owner can), then run the new `join` line here, in the agent's project directory.";
18
30
  readonly zh: "ach run:这台机器上没有它的密钥(被删除了,或是新机器、新目录)。\n密钥找不回,只能换新:在面板里打开它的页面,点「换新密钥」(管理员或负责人可以),再到它的项目目录里运行新生成的 `join` 命令。";
19
31
  };
20
- /** B7: `ach run` exit code when the hub answers 401 (systemd: RestartPreventExitStatus=78). */
21
- export declare const AUTH_EXIT_CODE = 78;
22
- export declare const AUTH_EXIT_MESSAGE = "ach run: key rotated or agent deactivated \u2014 run ach join";
23
32
  /**
24
33
  * The agent whose key this home already holds for `hub`, when it is another agent than `meId` and its key still works (a key the
25
34
  * hub refuses belongs to nobody: replacing it loses nothing). Unknown (hub unreachable) counts as another agent: keeping a key
@@ -44,7 +44,90 @@ export function writeAchShim(home) {
44
44
  chmodSync(f, 0o700);
45
45
  return dir;
46
46
  }
47
+ /**
48
+ * `ach run`. A key that is missing or that the hub refuses does not end it: the runner waits (backing off, reading the config again
49
+ * each time, so a new `ach join` here is picked up) and starts again once the hub takes the key. 2026-11-24: during the ids cutover
50
+ * the hub refused every key for 3 minutes, and runners that exited for good on that 401 stayed down until someone restarted them.
51
+ */
47
52
  export async function runCommand(p, io) {
53
+ for (;;) {
54
+ const r = await runOnce(p, io);
55
+ if (!(r instanceof KeyRefused))
56
+ return r;
57
+ // one-shot commands (setting up the service, cleaning worktrees) say why and stop
58
+ if (p.values['install-service'] || p.values.gc) {
59
+ io.err(r.message);
60
+ return 1;
61
+ }
62
+ if (!(await waitForKey(io, r.message)))
63
+ return 0;
64
+ io.err('ach run: the hub accepts the key; starting again');
65
+ }
66
+ }
67
+ /** Why runOnce stopped: no key here, or the hub refused it (401). */
68
+ class KeyRefused {
69
+ message;
70
+ constructor(message) {
71
+ this.message = message;
72
+ }
73
+ }
74
+ /** Waits between tries for a key that works: 30 s, then longer, at most 15 minutes. ACH_AUTH_RETRY_MS: one fixed wait (tests). */
75
+ export const KEY_RETRY_MS = [30_000, 60_000, 120_000, 300_000, 600_000, 900_000];
76
+ /**
77
+ * Until the hub takes this agent's key: read the config (a new `ach join` may have written a new key), ask the hub who this key is,
78
+ * wait and try again. Resolves true when the key works, false on SIGINT/SIGTERM.
79
+ */
80
+ export async function waitForKey(io, why) {
81
+ const fixed = Number(io.env.ACH_AUTH_RETRY_MS);
82
+ const delay = (i) => (Number.isFinite(fixed) && fixed > 0 ? fixed : KEY_RETRY_MS[Math.min(i, KEY_RETRY_MS.length - 1)]);
83
+ io.err(`${why}
84
+ ${isZh(io.env) ? 'ach run:先不退出,隔一段时间重试;在这里重新运行 `ach join` 换了新密钥也会自动接上。' : 'ach run: not exiting; trying again from time to time. A new `ach join` here is picked up.'}`);
85
+ let stop = false;
86
+ let wake = () => { };
87
+ const onSignal = () => {
88
+ stop = true;
89
+ wake();
90
+ };
91
+ process.once('SIGINT', onSignal);
92
+ process.once('SIGTERM', onSignal);
93
+ try {
94
+ let last = why;
95
+ for (let i = 0; !stop; i++) {
96
+ await new Promise((r) => {
97
+ const t = setTimeout(r, delay(i));
98
+ wake = () => {
99
+ clearTimeout(t);
100
+ r();
101
+ };
102
+ });
103
+ if (stop)
104
+ break;
105
+ let now;
106
+ try {
107
+ const cfg = loadConfig(io.env);
108
+ if (!cfg.key)
109
+ now = NO_KEY_MESSAGE[isZh(io.env) ? 'zh' : 'en'];
110
+ else {
111
+ await clientFromConfig(cfg, { fetch: io.fetch, role: 'runner' }).me();
112
+ return true;
113
+ }
114
+ }
115
+ catch (e) {
116
+ now = e instanceof HubError && e.status === 401 ? `ach run: the hub refuses the key: ${e.message}` : `ach run: cannot check the key: ${e?.message ?? e}`;
117
+ }
118
+ // the same reason is said once; a change (the key works but the hub is down, a new reason) is said when it happens
119
+ if (now !== last)
120
+ io.err(now);
121
+ last = now;
122
+ }
123
+ return false;
124
+ }
125
+ finally {
126
+ process.off('SIGINT', onSignal);
127
+ process.off('SIGTERM', onSignal);
128
+ }
129
+ }
130
+ async function runOnce(p, io) {
48
131
  const v = p.values;
49
132
  const env = io.env;
50
133
  if (env.ACH_RUN_TOKEN)
@@ -55,10 +138,9 @@ export async function runCommand(p, io) {
55
138
  return env.ACH_SUPERVISED === '1' ? UPDATE_EXIT_CODE : superviseSelf({ log: io.err });
56
139
  }
57
140
  const cfg = loadConfig(env);
58
- // No key on this machine (deleted, a new machine or directory). It exits like a refused key: a service that exits 2 here
59
- // would be restarted for ever, and `ach login` is no way back for someone who no longer has the key.
141
+ // No key on this machine (deleted, a new machine or directory): wait for one, like a refused key (runCommand).
60
142
  if (!cfg.key)
61
- return authExit(io, NO_KEY_MESSAGE[isZh(env) ? 'zh' : 'en']);
143
+ return new KeyRefused(NO_KEY_MESSAGE[isZh(env) ? 'zh' : 'en']);
62
144
  // the hub names the newest release on every answer: the updater hears it at once instead of at its next 6-hourly check
63
145
  let updater;
64
146
  const c = clientFromConfig(cfg, { fetch: io.fetch, role: 'runner', onLatestClient: (latest) => void updater?.heardLatest(latest)?.catch(() => { }) });
@@ -103,9 +185,9 @@ export async function runCommand(p, io) {
103
185
  me = await c.me();
104
186
  }
105
187
  catch (e) {
106
- // B7: a key that is already refused at startup exits like a 401 mid-run, so systemd/launchd stop restarting it
188
+ // a key the hub refuses at startup waits like a 401 mid-run (runCommand)
107
189
  if (e instanceof HubError && e.status === 401)
108
- return authExit(io);
190
+ return new KeyRefused(`ach run: the hub refuses the key: ${e.message}`);
109
191
  throw e;
110
192
  }
111
193
  if (v['install-service'])
@@ -225,6 +307,10 @@ export async function runCommand(p, io) {
225
307
  };
226
308
  process.once('SIGINT', shutdown);
227
309
  process.once('SIGTERM', shutdown);
310
+ const unhook = () => {
311
+ process.off('SIGINT', shutdown);
312
+ process.off('SIGTERM', shutdown);
313
+ };
228
314
  // one runner per home: a second one would overwrite the first's state
229
315
  let release = () => { };
230
316
  try {
@@ -253,9 +339,11 @@ export async function runCommand(p, io) {
253
339
  }
254
340
  updater.stop();
255
341
  desk?.stop();
256
- // B7: the hub refused the agent key: rotated, agent disabled or owner deactivated.
257
- if (end === 'unauthorized')
258
- return authExit(io);
342
+ // the hub refused the agent key mid-run (rotated, agent disabled, owner deactivated, or the hub briefly wrong): wait for it
343
+ if (end === 'unauthorized') {
344
+ unhook();
345
+ return new KeyRefused(`ach run: the hub refuses the key: ${runner.fatalReason ?? 'unauthorized'}`);
346
+ }
259
347
  if (updater.restarting && updater.installed === 'handoff') {
260
348
  io.err(`ach run: exiting for the update; the installer starts the runner again when it is done`);
261
349
  return UPDATE_HANDOFF_EXIT_CODE;
@@ -281,15 +369,6 @@ export const NO_KEY_MESSAGE = {
281
369
  en: 'ach run: there is no key for this agent on this machine (deleted, or a new machine or directory).\nA key cannot be recovered: in the panel open the agent and rotate its key (an admin or its owner can), then run the new `join` line here, in the agent\'s project directory.',
282
370
  zh: 'ach run:这台机器上没有它的密钥(被删除了,或是新机器、新目录)。\n密钥找不回,只能换新:在面板里打开它的页面,点「换新密钥」(管理员或负责人可以),再到它的项目目录里运行新生成的 `join` 命令。',
283
371
  };
284
- /** B7: print the rejoin hint and return the auth exit code (ACH_AUTH_EXIT_CODE overrides it). */
285
- function authExit(io, message = AUTH_EXIT_MESSAGE) {
286
- io.err(message);
287
- const code = Number(io.env.ACH_AUTH_EXIT_CODE ?? AUTH_EXIT_CODE);
288
- return Number.isInteger(code) && code >= 0 ? code : AUTH_EXIT_CODE;
289
- }
290
- /** B7: `ach run` exit code when the hub answers 401 (systemd: RestartPreventExitStatus=78). */
291
- export const AUTH_EXIT_CODE = 78;
292
- export const AUTH_EXIT_MESSAGE = 'ach run: key rotated or agent deactivated — run ach join';
293
372
  /**
294
373
  * P4: write a launchd plist (macOS) or systemd user unit (Linux) that runs this
295
374
  * runner with the same flags, and load it: it runs now and at every login.
@@ -326,10 +405,6 @@ async function installService(p, io, o) {
326
405
  svcEnv.AGENTCHANNEL_HOME = env.AGENTCHANNEL_HOME;
327
406
  if (env.ACH_KEYSTORE)
328
407
  svcEnv.ACH_KEYSTORE = env.ACH_KEYSTORE;
329
- // B7: launchd restarts only after an unsuccessful exit (KeepAlive.SuccessfulExit
330
- // = false), so under launchd a 401 exits 0 and the runner stays down.
331
- if (platform === 'darwin')
332
- svcEnv.ACH_AUTH_EXIT_CODE = '0';
333
408
  mkdirSync(join(o.home, 'logs'), { recursive: true, mode: 0o700 });
334
409
  const self = CLI_JS;
335
410
  // the install in the AgentChannel home (appinstall.ts), so the service follows its updates; run through npx, this release is copied
package/dist/src/cli.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  export { VERSION } from './version.js';
3
3
  import { type Io } from './cli-shared.js';
4
- export { AUTH_EXIT_CODE, AUTH_EXIT_MESSAGE, NO_KEY_MESSAGE, writeAchShim } from './cli-run.js';
4
+ export { KEY_RETRY_MS, NO_KEY_MESSAGE, waitForKey, writeAchShim } from './cli-run.js';
5
5
  export type { Io } from './cli-shared.js';
6
6
  export { channelFile, loginTokenOf } from './cli-shared.js';
7
7
  /** Runs the opener and waits for it: a non-zero exit (no browser, no display) is a failure, not "opened". */
package/dist/src/cli.js CHANGED
@@ -20,7 +20,7 @@ import { adminCommand } from './cli-admin.js';
20
20
  import { memoryCommand } from './cli-memory.js';
21
21
  import { otherAgentHere, runCommand, runInTerminal } from './cli-run.js';
22
22
  import { normalizeId } from './ids.js';
23
- export { AUTH_EXIT_CODE, AUTH_EXIT_MESSAGE, NO_KEY_MESSAGE, writeAchShim } from './cli-run.js';
23
+ export { KEY_RETRY_MS, NO_KEY_MESSAGE, waitForKey, writeAchShim } from './cli-run.js';
24
24
  // moved to cli-shared.ts; still reachable from here for the code and tests that import them from cli.js
25
25
  export { channelFile, loginTokenOf } from './cli-shared.js';
26
26
  /** Why a task failed (docs/specs/task-failures.md); the hub's list. */
@@ -318,7 +318,7 @@ async function dispatch(p, io) {
318
318
  throw new Error('the hub returned no key');
319
319
  key = String(ex.key);
320
320
  if (ex.created && ex.agent?.name)
321
- io.err(zhJ ? `已创建环境 ${ex.agent.name}。` : `created the environment ${ex.agent.name}.`);
321
+ io.err(zhJ ? `已创建机器人 ${ex.agent.name}。` : `created the bot ${ex.agent.name}.`);
322
322
  }
323
323
  catch (e) {
324
324
  if (e instanceof HubError && (e.code === 'conflict' || e.code === 'invalid_name')) {
@@ -1171,7 +1171,7 @@ function hubHint(e, env) {
1171
1171
  if (e.code === 'session_ended' || e.code === 'deactivated')
1172
1172
  return zh ? '\n登录已失效:运行 `ach admin login --link <链接>`。' : '\nYour session ended: run `ach admin login --link <login link>`.';
1173
1173
  if (e.code === 'needs_admin_login')
1174
- return zh ? '\n这个会话来自环境或机器人的密钥,不能做管理员操作:用设备链接或登录链接登录。' : '\nThis session came from an agent key and cannot use admin actions: log in with a device link or login link.';
1174
+ return zh ? '\n这个会话来自机器人的密钥,不能做管理员操作:用设备链接或登录链接登录。' : '\nThis session came from an agent key and cannot use admin actions: log in with a device link or login link.';
1175
1175
  return '';
1176
1176
  }
1177
1177
  function linkOf(env, t) {