@koalaauto/agentchannel 3.41.1 → 3.43.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.
@@ -1,5 +1,5 @@
1
1
  import { execFile } from 'node:child_process';
2
- import { existsSync, mkdirSync, readdirSync, rmSync, statSync } from 'node:fs';
2
+ import { existsSync, lstatSync, mkdirSync, readdirSync, rmSync, statSync, symlinkSync } from 'node:fs';
3
3
  import { basename, dirname, isAbsolute, join, resolve } from 'node:path';
4
4
  import { promisify } from 'node:util';
5
5
  const pexec = promisify(execFile);
@@ -166,8 +166,12 @@ export class GitWorkspace {
166
166
  async prepare(task, agentName) {
167
167
  const p = this.path(task.id);
168
168
  const dir = this.repoDir(task);
169
- if (existsSync(p))
169
+ if (existsSync(p)) {
170
+ // a worktree made before its dependencies were linked in (an older runner) gets them on its next round
171
+ if ((await isGitRepo(dir)) && (await isGitRepo(p)))
172
+ await linkDeps(dir, p, this.log);
170
173
  return p;
174
+ }
171
175
  mkdirSync(this.root, { recursive: true, mode: 0o700 });
172
176
  if (!(await isGitRepo(dir))) {
173
177
  // a task that names a repo expects its code: an empty dir would only let the agent find out for itself
@@ -203,6 +207,7 @@ export class GitWorkspace {
203
207
  await git(dir, ['worktree', 'add', p, branch]);
204
208
  else
205
209
  await git(dir, ['worktree', 'add', '-b', branch, p, start]);
210
+ await linkDeps(dir, p, this.log);
206
211
  return p;
207
212
  }
208
213
  async resolveRef(dir, base) {
@@ -288,7 +293,9 @@ export class GitWorkspace {
288
293
  }
289
294
  const safe = [...ident];
290
295
  // .ach/ holds the agent's notes, result file and screenshots, ach-files/ the task's downloads: neither belongs in the work
291
- await git(p, [...safe, 'add', '-A', '--', '.', ':(exclude).ach', ':(exclude)ach-files']);
296
+ // node_modules: the checkout's installed dependencies, linked in by linkDeps (a link is a file to git, which ignore rules
297
+ // written as `node_modules/` do not match)
298
+ await git(p, [...safe, 'add', '-A', '--', '.', ':(exclude).ach', ':(exclude)ach-files', ':(exclude,glob)**/node_modules']);
292
299
  if (!(await git(p, ['diff', '--cached', '--name-only'])))
293
300
  return undefined;
294
301
  await git(p, [...safe, 'commit', '--no-verify', '-q', '-m', message]);
@@ -312,23 +319,32 @@ export class GitWorkspace {
312
319
  return undefined;
313
320
  const commit = await git(p, ['rev-parse', 'HEAD']);
314
321
  // ahead of its base: a run that changed nothing has nothing to deliver
315
- const base = typeof task.links?.base === 'string' && task.links.base ? task.links.base : '';
316
- let start = '';
317
- for (const cand of base ? [base, `origin/${base}`] : ['origin/HEAD', 'origin/main', 'origin/master']) {
318
- try {
319
- await git(p, ['rev-parse', '--verify', '--quiet', `${cand}^{commit}`]);
320
- start = cand;
321
- break;
322
- }
323
- catch {
324
- /* try next */
325
- }
326
- }
322
+ const start = await baseOf(p, task);
327
323
  if (!start)
328
324
  return undefined;
329
325
  const ahead = parseInt(await git(p, ['rev-list', '--count', `${start}..HEAD`]), 10);
330
326
  return ahead > 0 ? { github, branch, commit } : undefined;
331
327
  }
328
+ async githubOf(taskId) {
329
+ const p = this.path(taskId);
330
+ if (!existsSync(p) || !(await isGitRepo(p)))
331
+ return '';
332
+ try {
333
+ return githubSlug(await git(p, ['config', '--get', 'remote.origin.url']));
334
+ }
335
+ catch {
336
+ return '';
337
+ }
338
+ }
339
+ async changedFiles(task) {
340
+ const p = this.path(task.id);
341
+ if (!existsSync(p) || !(await isGitRepo(p)))
342
+ return [];
343
+ const start = await baseOf(p, task);
344
+ if (!start)
345
+ return [];
346
+ return (await git(p, ['diff', '--name-only', `${start}...HEAD`])).split('\n').filter(Boolean);
347
+ }
332
348
  async push(taskId, d, token) {
333
349
  const p = this.path(taskId);
334
350
  // the token goes in a header for this one call: not in the URL, the config or any credential store. Never forced: the task
@@ -359,3 +375,73 @@ export class GitWorkspace {
359
375
  }
360
376
  }
361
377
  }
378
+ /** Where a task's branch started: its links.base, else the origin's default branch; '' when none resolves. */
379
+ async function baseOf(p, task) {
380
+ const base = typeof task.links?.base === 'string' && task.links.base ? task.links.base : '';
381
+ for (const cand of base ? [base, `origin/${base}`] : ['origin/HEAD', 'origin/main', 'origin/master']) {
382
+ try {
383
+ await git(p, ['rev-parse', '--verify', '--quiet', `${cand}^{commit}`]);
384
+ return cand;
385
+ }
386
+ catch {
387
+ /* try next */
388
+ }
389
+ }
390
+ return '';
391
+ }
392
+ /**
393
+ * A fresh worktree has the code but none of the checkout's installed dependencies, so the agent could neither build nor test what
394
+ * it wrote. Each node_modules the checkout has (at its root and up to two folders down) that git ignores is linked into the same
395
+ * place in the worktree. The agent reads them; its sandbox lets it write only inside the worktree, so it cannot change them.
396
+ */
397
+ export async function linkDeps(dir, p, log = () => undefined) {
398
+ const found = [];
399
+ const walk = (rel, depth) => {
400
+ let names = [];
401
+ try {
402
+ names = readdirSync(join(dir, rel));
403
+ }
404
+ catch {
405
+ return;
406
+ }
407
+ for (const n of names) {
408
+ if (n === '.git')
409
+ continue;
410
+ const r = rel ? `${rel}/${n}` : n;
411
+ let st;
412
+ try {
413
+ st = lstatSync(join(dir, r));
414
+ }
415
+ catch {
416
+ continue;
417
+ }
418
+ if (!st.isDirectory())
419
+ continue;
420
+ if (n === 'node_modules')
421
+ found.push(r);
422
+ else if (depth < 2 && !n.startsWith('.'))
423
+ walk(r, depth + 1);
424
+ }
425
+ };
426
+ walk('', 0);
427
+ let linked = 0;
428
+ for (const r of found) {
429
+ const to = join(p, r);
430
+ if (!existsSync(dirname(to)) || existsSync(to))
431
+ continue;
432
+ try {
433
+ await git(dir, ['check-ignore', '-q', r]); // exits non-zero when git would track it: never link that
434
+ }
435
+ catch {
436
+ continue;
437
+ }
438
+ try {
439
+ symlinkSync(join(dir, r), to, 'dir');
440
+ linked++;
441
+ }
442
+ catch (e) {
443
+ log(`link ${r}: ${e?.message ?? e}`);
444
+ }
445
+ }
446
+ return linked;
447
+ }
@@ -1,4 +1,4 @@
1
- export declare const VERSION = "3.41.1";
1
+ export declare const VERSION = "3.43.0";
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.41.1';
1
+ export const VERSION = '3.43.0';
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.41.1",
3
+ "version": "3.43.0",
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",