@koalaauto/agentchannel 3.33.0 → 3.34.1

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.
@@ -27,6 +27,15 @@ export interface Agent {
27
27
  net_allow?: string[];
28
28
  /** The repo names its runner can work in, as the runner reported them (empty: never reported). */
29
29
  repos?: string[];
30
+ /** GitHub repositories ("owner/name", "owner/*") that limit what the agent's runs may read; empty: every repository the hub's GitHub App sees. */
31
+ github_repos?: string[];
32
+ }
33
+ /** A read-only GitHub token the hub issued for one run, valid an hour. `repos`: the repository names it covers, or ["*"]. */
34
+ export interface GithubToken {
35
+ token: string;
36
+ expires_at: string;
37
+ owner: string;
38
+ repos: string[];
30
39
  }
31
40
  export interface Task {
32
41
  id: string;
@@ -382,6 +391,8 @@ export declare class Client {
382
391
  /** Download one attachment into `dir`; returns the local path. */
383
392
  download(a: Attachment, dir: string): Promise<string>;
384
393
  me(): Promise<Agent>;
394
+ /** A read-only GitHub token for one run (every repository the hub's App sees, or the agent's list); `runId` names a live run (the runner's key needs it). */
395
+ githubToken(runId: string): Promise<GithubToken>;
385
396
  agents(): Promise<Agent[]>;
386
397
  sendTask(input: SendTaskInput, files?: string[]): Promise<Task>;
387
398
  /** The sender's side accepts a task that waits for acceptance: it is completed. */
@@ -264,6 +264,10 @@ export class Client {
264
264
  a.owner_name = r.owner_name;
265
265
  return a;
266
266
  }
267
+ /** A read-only GitHub token for one run (every repository the hub's App sees, or the agent's list); `runId` names a live run (the runner's key needs it). */
268
+ async githubToken(runId) {
269
+ return this.ach('POST', '/github/token', { run_id: runId });
270
+ }
267
271
  async agents() {
268
272
  const r = await this.ach('GET', '/agents');
269
273
  return Array.isArray(r) ? r : arr(r?.agents ?? r?.items);
@@ -107,6 +107,7 @@ export async function runDoctor(o) {
107
107
  let hubMinProto = 0;
108
108
  let runtime;
109
109
  let agentName;
110
+ let githubRepos = [];
110
111
  if (cfg.hub) {
111
112
  const fetchFn = o.fetch ?? ((u, i) => fetch(u, i));
112
113
  let host = cfg.hub;
@@ -168,6 +169,7 @@ export async function runDoctor(o) {
168
169
  const me = await c.me();
169
170
  runtime = typeof me.runtime === 'string' ? me.runtime : undefined;
170
171
  agentName = me.name;
172
+ githubRepos = Array.isArray(me.github_repos) ? me.github_repos : [];
171
173
  add('hub login', 'ok', `agent ${me.name}${me.owner_name ? ` (owner ${me.owner_name})` : ''}, runtime ${me.runtime ?? '?'}`, 'key');
172
174
  }
173
175
  catch (e) {
@@ -187,6 +189,13 @@ export async function runDoctor(o) {
187
189
  hubLatest = c.latest_client;
188
190
  if (c && Number.isInteger(c.min_client_proto))
189
191
  hubMinProto = c.min_client_proto;
192
+ // a run's GitHub token comes from the hub's GitHub App; repositories an admin listed on this agent narrow it
193
+ if (c && typeof c.github_app === 'boolean') {
194
+ if (c.github_app)
195
+ add('github', 'ok', githubRepos.length ? `a read-only token for ${githubRepos.join(', ')}, issued for each run` : 'a read-only token for every repository the hub\'s GitHub App sees, issued for each run', 'key');
196
+ else if (githubRepos.length)
197
+ add('github', 'warn', `${githubRepos.join(', ')} are listed for this agent, but the hub has no GitHub App`, 'key', 'ask your admin to set ACH_GITHUB_APP_ID and ACH_GITHUB_APP_KEY on the hub');
198
+ }
190
199
  }
191
200
  catch {
192
201
  /* informational only */
@@ -7,7 +7,8 @@
7
7
  * ach admin invite --feishu [--people a,b] [--yes] | ach admin invite --people a,b --yes (print links)
8
8
  *
9
9
  * GitHub is read on THIS machine with the admin's own `gh` login (gh also
10
- * honors GITHUB_TOKEN / GH_TOKEN). The hub never sees a GitHub token: it gets a
10
+ * honors GITHUB_TOKEN / GH_TOKEN). The hub never sees the admin's token (it holds
11
+ * a read-only App key of its own for the runs of agents, docs/design.md §33): it gets a
11
12
  * plan (channels with repo mappings, people) and compact items (issues/PRs:
12
13
  * title, state, who, a short excerpt, the link; never comment threads).
13
14
  * Nothing here writes to GitHub. Feishu invites go through the local
@@ -5,7 +5,7 @@ import { RunnerState } from './state.js';
5
5
  import type { Workspace } from './workspace.js';
6
6
  import { TraceRecorder } from '../trace.js';
7
7
  import { type SandboxStatus } from '../sandboxcheck.js';
8
- export type RunnerClient = Pick<Client, 'me' | 'inbox' | 'claim' | 'getTask' | 'postMessage' | 'complete' | 'fail' | 'usage' | 'heartbeat' | 'answerRun' | 'endRun'> & Partial<Pick<Client, 'download' | 'channelPosts' | 'mentionRun' | 'mentionEnd' | 'mentionUsage' | 'promptSize' | 'reportSetup' | 'reportRepos' | 'reportRunner' | 'release' | 'putTrace'>>;
8
+ export type RunnerClient = Pick<Client, 'me' | 'inbox' | 'claim' | 'getTask' | 'postMessage' | 'complete' | 'fail' | 'usage' | 'heartbeat' | 'answerRun' | 'endRun'> & Partial<Pick<Client, 'download' | 'githubToken' | 'channelPosts' | 'mentionRun' | 'mentionEnd' | 'mentionUsage' | 'promptSize' | 'reportSetup' | 'reportRepos' | 'reportRunner' | 'release' | 'putTrace'>>;
9
9
  export interface RunnerOptions {
10
10
  client: RunnerClient;
11
11
  adapter: RuntimeAdapter;
@@ -167,6 +167,8 @@ export declare class Runner {
167
167
  me?: Agent;
168
168
  /** When `me` was last read from the hub (ms). */
169
169
  private meAt;
170
+ /** A hub that said it has no GitHub App is not asked for a token before this time. */
171
+ private githubOffUntil;
170
172
  /** When old work dirs were last cleaned up (ms); the first prune of a process does it. */
171
173
  private lastGc;
172
174
  /** No new task is taken before this time (ms): the account behind the runtime is out of budget. */
@@ -279,6 +281,13 @@ export declare class Runner {
279
281
  filesDir(taskId: string): string;
280
282
  /** The task's attachments, downloaded into filesDir (best effort: a failed download is listed without a path). */
281
283
  private fetchFiles;
284
+ /**
285
+ * The GitHub token of a run: read-only, issued by the hub from its GitHub App for this one run and valid an hour (every
286
+ * repository the App sees, or the ones an admin listed on this agent). A hub that cannot issue one does not fail the run: the
287
+ * agent finds `gh` without a login, and the runner's log for the task says why. After a failure the hub is not asked again
288
+ * for a minute (ten when it has no GitHub App).
289
+ */
290
+ private githubToken;
282
291
  /** Run the adapter with heartbeat, idle watchdog, session persistence and usage posting. */
283
292
  private execute;
284
293
  private traceDir;
@@ -212,6 +212,10 @@ export function prepareAgentHome(root, agentName, key, realHome, realCodexHome)
212
212
  }
213
213
  /** How long the runner trusts its copy of the agent's record before reading it again. */
214
214
  const ME_TTL_MS = 60_000;
215
+ /** How long a hub that has no GitHub App is left alone before a run asks it for a token again. */
216
+ const GITHUB_OFF_MS = 10 * 60_000;
217
+ /** The same after a failure that may pass (GitHub down, a setup being fixed). */
218
+ const GITHUB_RETRY_MS = 60_000;
215
219
  /** How often a parked run (waiting for an answer) is looked at: an answer reaches it this much later at most. */
216
220
  const PARKED_CHECK_MS = 12_000;
217
221
  /** A task handed back this many times for an account limit then fails normally. */
@@ -227,6 +231,8 @@ export class Runner {
227
231
  me;
228
232
  /** When `me` was last read from the hub (ms). */
229
233
  meAt = 0;
234
+ /** A hub that said it has no GitHub App is not asked for a token before this time. */
235
+ githubOffUntil = 0;
230
236
  /** When old work dirs were last cleaned up (ms); the first prune of a process does it. */
231
237
  lastGc = 0;
232
238
  /** No new task is taken before this time (ms): the account behind the runtime is out of budget. */
@@ -1273,6 +1279,30 @@ export class Runner {
1273
1279
  }
1274
1280
  return out;
1275
1281
  }
1282
+ /**
1283
+ * The GitHub token of a run: read-only, issued by the hub from its GitHub App for this one run and valid an hour (every
1284
+ * repository the App sees, or the ones an admin listed on this agent). A hub that cannot issue one does not fail the run: the
1285
+ * agent finds `gh` without a login, and the runner's log for the task says why. After a failure the hub is not asked again
1286
+ * for a minute (ten when it has no GitHub App).
1287
+ */
1288
+ async githubToken(rec) {
1289
+ if (!this.o.client.githubToken || Date.now() < this.githubOffUntil)
1290
+ return '';
1291
+ try {
1292
+ return (await this.o.client.githubToken(rec.runId)).token;
1293
+ }
1294
+ catch (e) {
1295
+ // a hub with no App is left alone for long; any other failure (GitHub down, the App installed nowhere) for a minute, so a
1296
+ // broken setup does not delay every run start by a round trip to the hub and GitHub
1297
+ if (e?.code === 'github_unconfigured')
1298
+ this.githubOffUntil = Date.now() + GITHUB_OFF_MS;
1299
+ else {
1300
+ this.githubOffUntil = Date.now() + GITHUB_RETRY_MS;
1301
+ this.taskLog(rec.taskId, `github token: ${e?.message ?? e}`);
1302
+ }
1303
+ return '';
1304
+ }
1305
+ }
1276
1306
  /** Run the adapter with heartbeat, idle watchdog, session persistence and usage posting. */
1277
1307
  async execute(rec, entry, prompt, resume, mode, outputSchema, extra = {}) {
1278
1308
  const { client } = this.o;
@@ -1295,6 +1325,15 @@ export class Runner {
1295
1325
  const baseEnv = this.o.baseEnv ?? process.env;
1296
1326
  const realHome = baseEnv.HOME || undefined;
1297
1327
  Object.assign(env, prepareAgentHome(this.o.agentHomesDir ?? join(tmpdir(), 'ach-homes'), me.name, taskId, realHome, baseEnv.CODEX_HOME || undefined).env);
1328
+ // a read-only GitHub token issued by the hub for this run (see githubToken)
1329
+ const gh = await this.githubToken(rec);
1330
+ if (gh) {
1331
+ env.GH_TOKEN = gh;
1332
+ env.GH_NO_UPDATE_NOTIFIER = '1';
1333
+ // gh writes its state beside its config; the run's HOME is not writable, its TMPDIR is
1334
+ if (env.TMPDIR)
1335
+ env.GH_CONFIG_DIR = join(env.TMPDIR, 'gh');
1336
+ }
1298
1337
  // C6: heartbeats are not activity. The watchdog fires after idleMs without
1299
1338
  // adapter events, unless a tool call is still in flight.
1300
1339
  let lastEvent = Date.now();
@@ -1303,7 +1342,7 @@ export class Runner {
1303
1342
  const startedAt = new Date();
1304
1343
  // the values of the variables handed through (--pass-env) are secrets too: masked wherever a tool prints them
1305
1344
  const passed = (this.o.passEnv ?? []).map((n) => process.env[n] ?? '').filter((v) => v.length >= 8);
1306
- const recorder = new TraceRecorder([rec.runToken, ...(this.o.secrets ?? []), this.o.cfSecret ?? '', ...passed].filter(Boolean));
1345
+ const recorder = new TraceRecorder([rec.runToken, ...(this.o.secrets ?? []), this.o.cfSecret ?? '', ...passed, gh].filter(Boolean));
1307
1346
  extra.recorder?.(recorder);
1308
1347
  let hb;
1309
1348
  hb =
@@ -1,4 +1,4 @@
1
- export declare const VERSION = "3.33.0";
1
+ export declare const VERSION = "3.34.1";
2
2
  /** The npm package (scripts/release.mjs keeps it and VERSION in step with package.json). */
3
3
  export declare const PKG_NAME = "@koalaauto/agentchannel";
4
4
  /**
@@ -1,4 +1,4 @@
1
- export const VERSION = '3.33.0';
1
+ export const VERSION = '3.34.1';
2
2
  /** The npm package (scripts/release.mjs keeps it and VERSION in step with package.json). */
3
3
  export const PKG_NAME = '@koalaauto/agentchannel';
4
4
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koalaauto/agentchannel",
3
- "version": "3.33.0",
3
+ "version": "3.34.1",
4
4
  "description": "Hand work between your team's agents (Claude Code, Codex) and follow it in one panel: the ach CLI, MCP server and runner for AgentChannel v2",
5
5
  "keywords": [
6
6
  "agentchannel",