@imdeadpool/guardex 8.4.2 → 8.4.3

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/mcp/collect.js +45 -24
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@imdeadpool/guardex",
3
- "version": "8.4.2",
3
+ "version": "8.4.3",
4
4
  "description": "Guardian T-Rex for your multi-agent repo. Isolated worktrees, file locks, and PR-only merges stop parallel Codex & Claude agents from overwriting each other's work. Auto-wires Oh My Codex, Oh My Claude, OpenSpec, and Caveman.",
5
5
  "license": "MIT",
6
6
  "preferGlobal": true,
@@ -97,5 +97,5 @@
97
97
  "jsonc-parser": "3.3.1",
98
98
  "semver": "7.7.4"
99
99
  },
100
- "gitHead": "3bf9656768d002719dc9498f17d317a3b2bf39cf"
100
+ "gitHead": "e8b55a9f1a26a2aec887019932c687e570ff1fc4"
101
101
  }
@@ -183,8 +183,7 @@ function locksByBranch(repoRoot) {
183
183
  // Resolve the MAIN repository root from any path inside it (a linked agent
184
184
  // worktree resolves up to the primary checkout). Worktrees share one ref store
185
185
  // via --git-common-dir, so all git ref ops below run against the main root.
186
- function mainRepoRoot(somePath) {
187
- const top = git(somePath, ['rev-parse', '--show-toplevel']);
186
+ function mainRepoRoot(somePath, top = git(somePath, ['rev-parse', '--show-toplevel'])) {
188
187
  if (!top) return null;
189
188
  const common = git(somePath, ['rev-parse', '--git-common-dir']);
190
189
  if (!common) return top;
@@ -192,6 +191,19 @@ function mainRepoRoot(somePath) {
192
191
  return path.basename(commonAbs) === '.git' ? path.dirname(commonAbs) : top;
193
192
  }
194
193
 
194
+ // Share expensive Git observations within one context call, never across calls.
195
+ function repoSnapshot(mainRoot) {
196
+ let worktrees;
197
+ let shared;
198
+ let remoteLocks;
199
+ return {
200
+ mainRoot,
201
+ worktrees: () => (worktrees ??= listWorktrees(mainRoot)),
202
+ shared: () => (shared ??= sharedGitState.settings(mainRoot)),
203
+ remoteLocks: () => (remoteLocks ??= sharedGitState.listLocks(mainRoot)),
204
+ };
205
+ }
206
+
195
207
  function branchHasUpstream(repoRoot, branch) {
196
208
  return Boolean(git(repoRoot, ['rev-parse', '--abbrev-ref', `${branch}@{upstream}`]));
197
209
  }
@@ -300,9 +312,14 @@ function isAgentLane(wt) {
300
312
 
301
313
  function collectRepoAgents(repoPath, { includePrs = true } = {}) {
302
314
  const mainRoot = mainRepoRoot(repoPath) || repoPath;
303
- const lanes = listWorktrees(mainRoot).filter(isAgentLane);
304
- const shared = sharedGitState.settings(mainRoot);
305
- const sharedLocks = shared.enabled ? sharedGitState.listLocks(mainRoot) : [];
315
+ return collectSnapshotAgents(repoSnapshot(mainRoot), includePrs);
316
+ }
317
+
318
+ function collectSnapshotAgents(snapshot, includePrs, excludeBranch) {
319
+ const { mainRoot } = snapshot;
320
+ const lanes = snapshot.worktrees().filter(isAgentLane);
321
+ const shared = snapshot.shared();
322
+ const sharedLocks = shared.enabled ? snapshot.remoteLocks() : [];
306
323
  const remoteBranches = shared.enabled ? sharedGitState.listRemoteAgentBranches(mainRoot) : [];
307
324
  const sharedLocksByBranch = {};
308
325
  for (const entry of sharedLocks) {
@@ -318,7 +335,7 @@ function collectRepoAgents(repoPath, { includePrs = true } = {}) {
318
335
  const prInfo = includePrs ? prMapForRepo(mainRoot) : null;
319
336
  const nowMs = Date.now();
320
337
  const localBranches = new Set(lanes.map((wt) => wt.branch));
321
- const records = lanes.map((wt) => {
338
+ const records = lanes.filter((wt) => wt.branch !== excludeBranch).map((wt) => {
322
339
  // Each worktree owns its OWN lock file; a lane's locks are the entries in
323
340
  // its own worktree keyed to its branch.
324
341
  const localLocks = locksByBranch(wt.path)[wt.branch] || [];
@@ -327,7 +344,7 @@ function collectRepoAgents(repoPath, { includePrs = true } = {}) {
327
344
  return buildAgentRecord(mainRoot, wt, locks, prInfo, nowMs);
328
345
  });
329
346
  for (const branch of remoteLaneBranches) {
330
- if (localBranches.has(branch)) continue;
347
+ if (branch === excludeBranch || localBranches.has(branch)) continue;
331
348
  const remoteBranch = remoteByBranch.get(branch);
332
349
  const locks = sharedLocksByBranch[branch] || [];
333
350
  const commit = remoteBranch ? lastCommit(mainRoot, remoteBranch.oid) : null;
@@ -395,17 +412,18 @@ function repoState(repoOrCwd, { includePrs = true } = {}) {
395
412
  // Aggregate locks across ALL worktrees of the repo. Lock files are per-worktree
396
413
  // on disk, so a single worktree's file only shows its own claims — the
397
414
  // collision view requires the union.
398
- function whoOwnsMany(files, { cwd = process.cwd(), repoPath } = {}) {
415
+ function whoOwnsMany(files, { cwd = process.cwd(), repoPath } = {}, existingSnapshot) {
399
416
  const requested = [...new Set(
400
417
  (Array.isArray(files) ? files : [])
401
418
  .filter((file) => typeof file === 'string' && file.trim())
402
419
  .map((file) => file.trim()),
403
420
  )].slice(0, 200);
404
421
  if (requested.length === 0) return [];
405
- const mainRoot = mainRepoRoot(repoPath || cwd);
422
+ const mainRoot = existingSnapshot?.mainRoot ?? mainRepoRoot(repoPath || cwd);
406
423
  if (!mainRoot) {
407
424
  return requested.map((file) => ({ file, owner: null, error: 'not a git repo' }));
408
425
  }
426
+ const snapshot = existingSnapshot ?? repoSnapshot(mainRoot);
409
427
  const targets = requested.map((file) => ({
410
428
  file,
411
429
  rel: path.isAbsolute(file) ? path.relative(mainRoot, file) : file,
@@ -422,7 +440,7 @@ function whoOwnsMany(files, { cwd = process.cwd(), repoPath } = {}) {
422
440
  // Read each worktree lock map once for the whole requested batch. A lock is
423
441
  // ignored only when its branch/ref disappeared and its recorded pane is
424
442
  // confirmed to be on another checkout; ambiguous ownership remains blocking.
425
- const worktrees = listWorktrees(mainRoot);
443
+ const worktrees = snapshot.worktrees();
426
444
  const knownBranches = knownBranchNames(mainRoot, worktrees);
427
445
  for (const wt of worktrees) {
428
446
  const map = readLockMap(wt.path);
@@ -437,9 +455,9 @@ function whoOwnsMany(files, { cwd = process.cwd(), repoPath } = {}) {
437
455
  });
438
456
  }
439
457
  }
440
- if (sharedGitState.settings(mainRoot).enabled) {
458
+ if (snapshot.shared().enabled) {
441
459
  const sharedByFile = new Map(
442
- sharedGitState.listLocks(mainRoot).map((entry) => [entry.file, entry]),
460
+ snapshot.remoteLocks().map((entry) => [entry.file, entry]),
443
461
  );
444
462
  for (const target of targets) {
445
463
  const entry = sharedByFile.get(target.rel) || sharedByFile.get(target.file);
@@ -495,22 +513,26 @@ function radarRecord(a) {
495
513
  }
496
514
 
497
515
  function myContext({ cwd = process.cwd(), includePr = true } = {}) {
516
+ return readCurrentContext(cwd, includePr).context;
517
+ }
518
+
519
+ function readCurrentContext(cwd, includePr) {
498
520
  const here = git(cwd, ['rev-parse', '--show-toplevel']);
499
- if (!here) return { error: 'not a git repo', cwd };
500
- const mainRoot = mainRepoRoot(cwd) || here;
521
+ if (!here) return { context: { error: 'not a git repo', cwd } };
522
+ const mainRoot = mainRepoRoot(cwd, here) || here;
523
+ const snapshot = repoSnapshot(mainRoot);
501
524
  const branch = git(here, ['rev-parse', '--abbrev-ref', 'HEAD']);
502
- const self = listWorktrees(mainRoot).find((w) => path.resolve(w.path) === path.resolve(here));
525
+ const self = snapshot.worktrees().find((w) => path.resolve(w.path) === path.resolve(here));
503
526
  const lc = branch ? lastCommit(mainRoot, branch) : null;
504
527
  const localLocks = branch ? locksByBranch(here)[branch] || [] : [];
505
- const shared = sharedGitState.settings(mainRoot);
528
+ const shared = snapshot.shared();
506
529
  const remoteLocks =
507
530
  shared.enabled && branch
508
- ? sharedGitState
509
- .listLocks(mainRoot)
531
+ ? snapshot.remoteLocks()
510
532
  .filter((entry) => entry.branch === branch)
511
533
  .map((entry) => entry.file)
512
534
  : [];
513
- return {
535
+ const context = {
514
536
  repo: repoName(mainRoot),
515
537
  repoPath: mainRoot,
516
538
  worktree: here,
@@ -527,6 +549,7 @@ function myContext({ cwd = process.cwd(), includePr = true } = {}) {
527
549
  // The repo's own AGENTS.md rules: are they enforced here, and what fired lately.
528
550
  abide: compactAbide(abide.abideSummary(here))
529
551
  };
552
+ return { context, snapshot };
530
553
  }
531
554
 
532
555
  function compactAbide(summary) {
@@ -565,12 +588,10 @@ function editContext({ cwd = process.cwd(), files = [], includePrs = false, maxB
565
588
  if (maxBytes !== undefined && (!Array.isArray(files) || files.length > 200 || files.some((file) => typeof file !== 'string' || !file.trim()))) {
566
589
  throw new Error('Bounded context requires at most 200 file paths; split the file batch');
567
590
  }
568
- const context = myContext({ cwd, includePr: includePrs });
591
+ const { context, snapshot } = readCurrentContext(cwd, includePrs);
569
592
  if (context.error) return maxBytes === undefined ? context : boundContext(context, maxBytes);
570
- const otherAgents = collectRepoAgents(context.repoPath, { includePrs })
571
- .filter((agent) => agent.branch !== context.branch)
572
- .map(radarRecord);
573
- const ownership = whoOwnsMany(files, { repoPath: context.repoPath })
593
+ const otherAgents = collectSnapshotAgents(snapshot, includePrs, context.branch).map(radarRecord);
594
+ const ownership = whoOwnsMany(files, {}, snapshot)
574
595
  .map((result) => ({ ...compactOwnership(result), ...(maxBytes !== undefined && result.error ? { error: result.error } : {}) }));
575
596
  const result = { ...context, otherAgents, ownership };
576
597
  return maxBytes === undefined ? result : boundContext(result, maxBytes);