pi-odd-worktree 1.2.0 → 2.1.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.
@@ -471,7 +471,7 @@ describe('A2: ignored files are disclosed, never silently deleted', () => {
471
471
  });
472
472
 
473
473
  // S7/S8: the warp panel's Ctrl+D runs the SAME active-session guard as the
474
- // `worktree_close` tool, and it runs it BEFORE the pending-change question.
474
+ // `worktree_delete` tool, and it runs it BEFORE the pending-change question.
475
475
  // These cases drive the real `/warp` command with a host double: `ui.custom`
476
476
  // answers the marker the selector's `ctrl+d` emits, `ui.confirm` records the
477
477
  // one dialog, and every temp worktree is created through the registered tool.
@@ -1,5 +1,5 @@
1
1
  'use strict';
2
- // S6: `worktree_close` removes a checkout with `git worktree remove` and NOTHING
2
+ // S6: `worktree_delete` removes a checkout with `git worktree remove` and NOTHING
3
3
  // else. The Herdr workspace reconcile it used to run — closing every workspace
4
4
  // of the checkout's group and opening a fresh one at the principal — is gone:
5
5
  // this package never closes or opens a Herdr panel, and it never asks Herdr to
@@ -192,7 +192,7 @@ describe('S6: a real close spawns no Herdr process', () => {
192
192
  // From here on any Herdr spawn would leave a line in the log.
193
193
  process.env.HERDR_BIN_PATH = HERDR_SHIM;
194
194
  try {
195
- const closed = await tools.get('worktree_close').execute('c2', { name: 'feat-quiet' }, undefined, undefined, ctx);
195
+ const closed = await tools.get('worktree_delete').execute('c2', { name: 'feat-quiet' }, undefined, undefined, ctx);
196
196
  assert.notEqual(closed.isError, true, resultText(closed));
197
197
  } finally {
198
198
  process.env.HERDR_BIN_PATH = NO_HERDR;
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
  // S7/S8/S9: lib/worktree-session-guard.js — the ONE active-session guard the
3
- // `worktree_close` tool and the warp panel's Ctrl+D both run.
3
+ // `worktree_delete` tool and the warp panel's Ctrl+D both run.
4
4
  //
5
5
  // Gentle Shell publishes its session presence store under
6
6
  // `<agent home>/gentle-agents/presence`: a bounded `<sessionHash>.<incarnation>
@@ -11,7 +11,8 @@ const fs = require('node:fs');
11
11
  const os = require('node:os');
12
12
  const path = require('node:path');
13
13
  const { spawnSync } = require('node:child_process');
14
- const { loadExtension } = require('./fixtures/load-extension.js');
14
+ const { pathToFileURL } = require('node:url');
15
+ const { LIB_DIR, stubbedSource } = require('./fixtures/load-extension.js');
15
16
 
16
17
  const scratch = [];
17
18
  function track(dir) {
@@ -168,15 +169,45 @@ function makeUiCtx(repoRoot, thread = 'thread-1') {
168
169
  }
169
170
 
170
171
  let tools;
172
+ let extensionMod;
173
+ let extensionHooks;
171
174
  before(async () => {
172
175
  process.env.HERDR_BIN_PATH = NO_HERDR;
173
176
  process.env.GENTLE_WORKTREE_THREAD = 'thread-1';
174
177
  process.env.PATH = `${HOOK_BIN}${path.delimiter}${savedEnv.PATH || ''}`;
175
178
  delete process.env.GENTLE_PI_AGENT_HOME;
176
179
  process.env.PI_CODING_AGENT_DIR = AGENT_HOME;
177
- ({ tools } = await loadExtension({}));
180
+ ({ tools, mod: extensionMod, hooks: extensionHooks } = await loadExtensionWithModule({}));
178
181
  });
179
182
 
183
+ // The create is instant on BOTH fronts, and its deferred setup is tracked by the
184
+ // module instance that registered the tool. `awaitPendingCreationSetups` is that
185
+ // instance's own exported seam (the same one a shutdown path uses), so this file
186
+ // loads the extension itself — same stubs, same lib override, same fake host as
187
+ // the shared fixture — and keeps the module namespace beside the registrations.
188
+ async function loadExtensionWithModule(pi) {
189
+ process.env.GENTLE_WORKTREE_LIB_DIR = LIB_DIR;
190
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'odd-wt-ext-'));
191
+ try {
192
+ const copy = path.join(dir, 'odd-worktree-tools.mjs');
193
+ fs.writeFileSync(copy, stubbedSource());
194
+ const mod = await import(pathToFileURL(copy).href);
195
+ const commands = new Map();
196
+ const tools = new Map();
197
+ const hooks = [];
198
+ mod.default({
199
+ registerCommand: (name, handler) => { commands.set(name, handler); },
200
+ registerTool: (tool) => { tools.set(tool.name, tool); },
201
+ registerMessageRenderer: () => {},
202
+ on: (event, handler) => { hooks.push([event, handler]); },
203
+ ...pi,
204
+ });
205
+ return { mod, commands, tools, hooks };
206
+ } finally {
207
+ fs.rmSync(dir, { recursive: true, force: true });
208
+ }
209
+ }
210
+
180
211
  describe('worktree_list', () => {
181
212
  test('a fresh repository lists its principal checkout only', async () => {
182
213
  const repo = makeRepo();
@@ -279,7 +310,7 @@ describe('worktree_create', () => {
279
310
  });
280
311
  });
281
312
 
282
- describe('worktree_close', () => {
313
+ describe('worktree_delete', () => {
283
314
  test('removes the checkout and keeps the branch', async () => {
284
315
  const repo = makeRepo();
285
316
  const { ctx } = makeCtx(repo);
@@ -293,7 +324,7 @@ describe('worktree_close', () => {
293
324
  assert.notEqual(created.isError, true, resultText(created));
294
325
  const target = path.join(path.dirname(repo), `${path.basename(repo)}-worktrees`, 'feat-close-me');
295
326
 
296
- const closed = await tools.get('worktree_close').execute('call-8', { name: 'feat-close-me' }, undefined, undefined, ctx);
327
+ const closed = await tools.get('worktree_delete').execute('call-8', { name: 'feat-close-me' }, undefined, undefined, ctx);
297
328
  assert.notEqual(closed.isError, true, resultText(closed));
298
329
  assert.equal(
299
330
  resultText(closed),
@@ -313,7 +344,7 @@ describe('worktree_close', () => {
313
344
  test('an unknown name is refused and leaves the repository untouched', async () => {
314
345
  const repo = makeRepo();
315
346
  const { ctx } = makeCtx(repo);
316
- const result = await tools.get('worktree_close').execute('call-10', { name: 'nope' }, undefined, undefined, ctx);
347
+ const result = await tools.get('worktree_delete').execute('call-10', { name: 'nope' }, undefined, undefined, ctx);
317
348
  assert.equal(result.isError, true);
318
349
  assert.equal(resultText(result), 'no registered worktree for "nope": no worktrees are registered');
319
350
  assert.equal(listWorktreePaths(repo).length, 1);
@@ -330,7 +361,7 @@ describe('worktree_close', () => {
330
361
  const target = path.join(path.dirname(repo), `${path.basename(repo)}-worktrees`, 'feat-busy');
331
362
  const sessionHash = publishSession({ sessionId: 'worker-9', label: 'Worker', workspace: target });
332
363
 
333
- const closed = await tools.get('worktree_close').execute('call-s2', { name: 'feat-busy' }, undefined, undefined, ctx);
364
+ const closed = await tools.get('worktree_delete').execute('call-s2', { name: 'feat-busy' }, undefined, undefined, ctx);
334
365
  assert.equal(closed.isError, true, 'a live session blocks the close');
335
366
  const text = resultText(closed);
336
367
  assert.match(text, /working in that checkout/);
@@ -355,7 +386,7 @@ describe('worktree_close', () => {
355
386
  // The session now STANDS in the checkout: no presence record is needed for
356
387
  // the deterministic cwd check to catch it.
357
388
  const { ctx } = makeCtx(target);
358
- const closed = await tools.get('worktree_close').execute('call-s5', { name: 'feat-mine' }, undefined, undefined, ctx);
389
+ const closed = await tools.get('worktree_delete').execute('call-s5', { name: 'feat-mine' }, undefined, undefined, ctx);
359
390
  assert.equal(closed.isError, true);
360
391
  const text = resultText(closed);
361
392
  assert.match(text, /this session is working in that checkout/);
@@ -373,7 +404,7 @@ describe('worktree_close', () => {
373
404
  const target = path.join(path.dirname(repo), `${path.basename(repo)}-worktrees`, 'feat-blocked-ui');
374
405
  publishSession({ sessionId: 'ui-worker', label: 'UI Worker', workspace: target, incarnationIndex: 2 });
375
406
  const { ctx, confirmations } = makeUiCtx(repo);
376
- const closed = await tools.get('worktree_close').execute('call-s7', { name: 'feat/blocked-ui' }, undefined, undefined, ctx);
407
+ const closed = await tools.get('worktree_delete').execute('call-s7', { name: 'feat/blocked-ui' }, undefined, undefined, ctx);
377
408
  assert.equal(closed.isError, true);
378
409
  assert.equal(confirmations.length, 0, 'the session check runs before the approval question');
379
410
  assert.ok(fs.existsSync(target));
@@ -385,7 +416,7 @@ describe('worktree_close', () => {
385
416
  await tools.get('worktree_create').execute('call-s8', { branch: 'feat/free', changes: 'none' }, undefined, undefined, ctx);
386
417
  const target = path.join(path.dirname(repo), `${path.basename(repo)}-worktrees`, 'feat-free');
387
418
  publishSession({ sessionId: 'other-worker', label: 'Other', workspace: repo, incarnationIndex: 3 });
388
- const closed = await tools.get('worktree_close').execute('call-s9', { name: 'feat-free' }, undefined, undefined, ctx);
419
+ const closed = await tools.get('worktree_delete').execute('call-s9', { name: 'feat-free' }, undefined, undefined, ctx);
389
420
  assert.notEqual(closed.isError, true, resultText(closed));
390
421
  assert.equal(fs.existsSync(target), false);
391
422
  });
@@ -398,7 +429,7 @@ describe('worktree_close', () => {
398
429
  const sessionHash = publishSession({ sessionId: 'raw-worker-9', label: 'Raw', workspace: target, incarnationIndex: 7 });
399
430
  publishTransport({ sessionId: 'raw-worker-9' });
400
431
 
401
- const closed = await tools.get('worktree_close').execute('call-s13', { name: 'feat-joined' }, undefined, undefined, ctx);
432
+ const closed = await tools.get('worktree_delete').execute('call-s13', { name: 'feat-joined' }, undefined, undefined, ctx);
402
433
  assert.equal(closed.isError, true);
403
434
  const text = resultText(closed);
404
435
  assert.ok(text.includes('id raw-worker-9'), text);
@@ -415,7 +446,7 @@ describe('worktree_close', () => {
415
446
  const target = path.join(path.dirname(repo), `${path.basename(repo)}-worktrees`, 'feat-unjoined');
416
447
  const sessionHash = publishSession({ sessionId: 'unjoined-worker', label: 'Unjoined', workspace: target, incarnationIndex: 8 });
417
448
 
418
- const closed = await tools.get('worktree_close').execute('call-s15', { name: 'feat-unjoined' }, undefined, undefined, ctx);
449
+ const closed = await tools.get('worktree_delete').execute('call-s15', { name: 'feat-unjoined' }, undefined, undefined, ctx);
419
450
  assert.equal(closed.isError, true);
420
451
  const text = resultText(closed);
421
452
  assert.ok(text.includes(sessionHash.slice(0, 12)), text);
@@ -431,7 +462,7 @@ describe('worktree_close', () => {
431
462
  const target = path.join(path.dirname(repo), `${path.basename(repo)}-worktrees`, 'feat-advisory');
432
463
  publishSession({ sessionId: 'legacy-peer', label: 'Legacy', workspace: null, heartbeat: Date.now(), incarnationIndex: 9, discovery: false });
433
464
 
434
- const closed = await tools.get('worktree_close').execute('call-s17', { name: 'feat-advisory' }, undefined, undefined, ctx);
465
+ const closed = await tools.get('worktree_delete').execute('call-s17', { name: 'feat-advisory' }, undefined, undefined, ctx);
435
466
  assert.notEqual(closed.isError, true, resultText(closed));
436
467
  assert.match(resultText(closed), /published no workspace/);
437
468
  assert.match(resultText(closed), /best effort/);
@@ -447,7 +478,7 @@ describe('worktree_close', () => {
447
478
  sessionId: 'crashed-worker', label: 'Crashed', workspace: target,
448
479
  heartbeat: Date.now() - 10 * 60 * 1000, incarnationIndex: 4,
449
480
  });
450
- const closed = await tools.get('worktree_close').execute('call-s11', { name: 'feat-stale' }, undefined, undefined, ctx);
481
+ const closed = await tools.get('worktree_delete').execute('call-s11', { name: 'feat-stale' }, undefined, undefined, ctx);
451
482
  assert.notEqual(closed.isError, true, resultText(closed));
452
483
  const text = resultText(closed);
453
484
  assert.match(text, /stale Gentle Shell presence record ignored/);
@@ -456,3 +487,136 @@ describe('worktree_close', () => {
456
487
  assert.ok(branches(repo).includes('feat/stale'), 'the branch is kept');
457
488
  });
458
489
  });
490
+
491
+ // ---------------------------------------------------------------------------
492
+ // The create is INSTANT on both fronts: the checkout and its registry row are
493
+ // the answer, and the slow half of the setup (dependency install, `codegraph
494
+ // init`) finishes behind it in a tracked task. Nothing is skipped, and the tool
495
+ // front never notifies a UI its call did not ask for.
496
+ // ---------------------------------------------------------------------------
497
+ describe('an instant create finishes its setup in the background', { concurrency: false }, () => {
498
+ // A `codegraph` that is visibly SLOW: it records that it started, waits, then
499
+ // records that it finished. A create that awaited the setup could never return
500
+ // between the two lines.
501
+ function slowCodegraph(markerPath) {
502
+ const dir = track(fs.mkdtempSync(path.join(os.tmpdir(), 'odd-wt-slowbin-')));
503
+ fs.writeFileSync(
504
+ path.join(dir, 'codegraph'),
505
+ `#!/bin/sh\nprintf 'started\\n' >> '${markerPath}'\nsleep 0.5\nprintf 'finished\\n' >> '${markerPath}'\n`,
506
+ { mode: 0o755 },
507
+ );
508
+ return dir;
509
+ }
510
+
511
+ function markerFile() {
512
+ return path.join(track(fs.mkdtempSync(path.join(os.tmpdir(), 'odd-wt-marker-'))), 'codegraph.log');
513
+ }
514
+
515
+ function readMarker(marker) {
516
+ return fs.existsSync(marker) ? fs.readFileSync(marker, 'utf8') : '';
517
+ }
518
+
519
+ function binWith(name, script) {
520
+ const dir = track(fs.mkdtempSync(path.join(os.tmpdir(), 'odd-wt-bin-')));
521
+ fs.writeFileSync(path.join(dir, name), script, { mode: 0o755 });
522
+ return dir;
523
+ }
524
+
525
+ test('the result returns first, the codegraph step still runs to the end, and a later create reports it', async () => {
526
+ const repo = makeRepo();
527
+ const marker = markerFile();
528
+ const savedPath = process.env.PATH;
529
+ process.env.PATH = `${slowCodegraph(marker)}${path.delimiter}${savedPath}`;
530
+ try {
531
+ const { ctx, notifications } = makeCtx(repo);
532
+ const created = await tools.get('worktree_create').execute('instant-1', { branch: 'feat/instant', changes: 'none' }, undefined, undefined, ctx);
533
+ assert.notEqual(created.isError, true, resultText(created));
534
+ const text = resultText(created);
535
+ assert.match(text, /^created: feat-instant · feat\/instant$/m);
536
+ assert.match(text, /^setup: running in the background \(install, codegraph\)$/m, text);
537
+ assert.match(text, /^codegraph: pending /m, 'the create states the step is not done yet');
538
+ assert.equal(created.details.setupPending, true);
539
+ assert.equal(/finished/.test(readMarker(marker)), false, 'the create returned only after its setup had finished');
540
+ assert.ok(fs.existsSync(path.join(path.dirname(repo), `${path.basename(repo)}-worktrees`, 'feat-instant')), 'the checkout is usable at return time');
541
+
542
+ // Nothing is skipped: the deferred task still reaches the end of the
543
+ // codegraph step, observed through the module's own
544
+ // `awaitPendingCreationSetups` seam.
545
+ await extensionMod.awaitPendingCreationSetups();
546
+ assert.equal(readMarker(marker).includes('finished'), true, 'the background codegraph step never ran to the end');
547
+
548
+ // The settled lifecycle is readable through the tool surface: asking again
549
+ // for the same checkout reports the state and the steps that really ran.
550
+ const replay = await tools.get('worktree_create').execute('instant-2', { branch: 'feat/instant', changes: 'none' }, undefined, undefined, ctx);
551
+ assert.notEqual(replay.isError, true, resultText(replay));
552
+ assert.match(resultText(replay), /^registered: unchanged$/m);
553
+ assert.match(resultText(replay), /^setup: finished$/m, resultText(replay));
554
+ assert.ok(
555
+ replay.details.setup.steps.some((step) => step.name === 'codegraph-init' && step.status === 'done'),
556
+ `the codegraph step is reported as done: ${JSON.stringify(replay.details.setup.steps)}`,
557
+ );
558
+ assert.deepEqual(notifications, [], 'the tool front never notifies a UI its call did not ask for');
559
+ } finally {
560
+ process.env.PATH = savedPath;
561
+ }
562
+ });
563
+
564
+ test('only a caller that asks for the notice gets it, and a session that ends gets none', async () => {
565
+ const repo = makeRepo();
566
+ const { ctx } = makeCtx(repo);
567
+ const notes = [];
568
+ const notify = (message, level) => { notes.push([level, message]); };
569
+ const created = await extensionMod.executeWorktreeCreate(
570
+ { branch: 'feat/notified', changes: 'none' },
571
+ ctx,
572
+ { backgroundHooks: true, notify },
573
+ );
574
+ assert.notEqual(created.isError, true, resultText(created));
575
+ await extensionMod.awaitPendingCreationSetups();
576
+ assert.deepEqual(notes, [['info', 'warp: setup finished for feat/notified: codegraph initialized']]);
577
+
578
+ // A setup that settles after its session ended records and reports nothing:
579
+ // the session it belonged to is gone, so there is no UI to write into.
580
+ const marker = markerFile();
581
+ const savedPath = process.env.PATH;
582
+ process.env.PATH = `${slowCodegraph(marker)}${path.delimiter}${savedPath}`;
583
+ try {
584
+ const late = await extensionMod.executeWorktreeCreate(
585
+ { branch: 'feat/late', changes: 'none' },
586
+ ctx,
587
+ { backgroundHooks: true, notify },
588
+ );
589
+ assert.notEqual(late.isError, true, resultText(late));
590
+ notes.length = 0;
591
+ // The shutdown hooks of the SAME instance that owns the deferred task: it
592
+ // is the session that created the checkout that ends here.
593
+ for (const [, handler] of extensionHooks.filter(([name]) => name === 'session_shutdown')) await handler({}, ctx);
594
+ await extensionMod.awaitPendingCreationSetups();
595
+ assert.deepEqual(notes, [], 'a setup that settles after shutdown reports nothing');
596
+ } finally {
597
+ process.env.PATH = savedPath;
598
+ }
599
+ });
600
+
601
+ test('a problem in the deferred setup is a warning, never an error', async () => {
602
+ const repo = makeRepo();
603
+ const { ctx } = makeCtx(repo);
604
+ const notes = [];
605
+ const savedPath = process.env.PATH;
606
+ process.env.PATH = `${binWith('codegraph', '#!/bin/sh\nexit 1\n')}${path.delimiter}${savedPath}`;
607
+ try {
608
+ const created = await extensionMod.executeWorktreeCreate(
609
+ { branch: 'feat/broken', changes: 'none' },
610
+ ctx,
611
+ { backgroundHooks: true, notify: (message, level) => { notes.push([level, message]); } },
612
+ );
613
+ assert.notEqual(created.isError, true, resultText(created));
614
+ await extensionMod.awaitPendingCreationSetups();
615
+ assert.deepEqual(notes.map(([level]) => level), ['warning'], JSON.stringify(notes));
616
+ assert.match(notes[0][1], /^warp: setup for feat\/broken finished with problems: codegraph-init: codegraph init failed/);
617
+ assert.match(notes[0][1], / — the checkout at .+ is usable$/);
618
+ } finally {
619
+ process.env.PATH = savedPath;
620
+ }
621
+ });
622
+ });