pi-odd-worktree 3.1.1 → 3.3.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/README.md +2 -1
- package/extensions/odd-worktree-tools.ts +83 -3
- package/lib/bash-activity.js +405 -32
- package/lib/bash-inline-script.js +314 -0
- package/lib/codegraph-guard.js +36 -19
- package/lib/codegraph-prompt/generic.md +1 -1
- package/lib/codegraph-prompt/orchestrator.md +7 -7
- package/lib/codegraph-session.js +10 -2
- package/lib/odd-prompt/odd-worktrees-child.md +3 -0
- package/lib/odd-prompt/odd-worktrees.md +11 -7
- package/lib/odd-worktrees-prompt.js +68 -14
- package/lib/skill-read-guard.js +397 -0
- package/lib/worktree-guard.js +205 -5
- package/package.json +1 -1
- package/tools/bash-activity.test.js +400 -35
- package/tools/codegraph-guard.test.js +109 -11
- package/tools/codegraph-rules.test.js +8 -8
- package/tools/codegraph-session.test.js +45 -3
- package/tools/odd-worktrees-prompt.test.js +98 -11
- package/tools/package-surface.test.js +15 -1
- package/tools/skill-read-guard.test.js +316 -0
- package/tools/worktree-write-guard.test.js +516 -0
package/lib/worktree-guard.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
// T1, T5 (S1–S6, S8–S11): the worktree guard, decided from one `bash` command string
|
|
2
|
+
// T1, T5 (S1–S6, S8–S11): the worktree guard, decided from one `bash` command string,
|
|
3
|
+
// plus (T3, S4) the `edit`/`write` guard, decided from the paths a call touches.
|
|
3
4
|
//
|
|
4
5
|
// A worktree is the checkout one ODD owns, and it is created, listed and removed
|
|
5
6
|
// through this package's surfaces — never through Git typed into `bash`. The guard
|
|
@@ -17,6 +18,19 @@
|
|
|
17
18
|
// S6 everything else passes: inspection, file restores, every `git branch` form,
|
|
18
19
|
// `git worktree remove|prune|move`, and every non-`bash` tool call.
|
|
19
20
|
//
|
|
21
|
+
// T3 (S4, L2) adds the WRITE guard, and it is a different question over different
|
|
22
|
+
// input: an `edit` or `write` whose `path` lies inside the PRINCIPAL checkout of the
|
|
23
|
+
// session's own repository is refused while that checkout is on the repository's
|
|
24
|
+
// default branch. A write is where the rule is broken, so the refusal arrives before
|
|
25
|
+
// the file is touched. It judges NOTHING else (L2 is the human's own word): a path
|
|
26
|
+
// outside every repository, a path under the OS temp directories even when a scratch
|
|
27
|
+
// repository lives there, a path in another repository, a path in a linked worktree, a
|
|
28
|
+
// session outside version control, an unreadable identity, a detached HEAD, another
|
|
29
|
+
// branch and a default branch that does not resolve all pass. The path is judged by
|
|
30
|
+
// its existing parent directory, realpath-resolved, so neither a symlink spelling nor
|
|
31
|
+
// a file that does not exist yet can dodge or false-trip it, and anything it cannot
|
|
32
|
+
// place passes.
|
|
33
|
+
//
|
|
20
34
|
// S3 (L10) is a permission, not a refusal: creating, moving, copying, deleting or
|
|
21
35
|
// listing a branch changes no checkout, so every `git branch` form yields no intent
|
|
22
36
|
// here and is never refused. A branch created and not checked out is exactly what
|
|
@@ -79,12 +93,15 @@
|
|
|
79
93
|
// or executed to find out what a command would do. What surrounds the sequence never
|
|
80
94
|
// matters: one occurrence refuses the whole call.
|
|
81
95
|
//
|
|
82
|
-
// Nothing here spawns a child,
|
|
96
|
+
// Nothing here spawns a child, executes anything or caches an answer: `repoIdentity`,
|
|
83
97
|
// `defaultBranch` and `currentBranch` are injected seams, so this module is a pure
|
|
84
|
-
// decision over the command text — it runs on the hot path of
|
|
85
|
-
// and it never throws, because a thrown `tool_call` handler blocks
|
|
86
|
-
// (S8).
|
|
98
|
+
// decision over the command text (or the touched paths) — it runs on the hot path of
|
|
99
|
+
// every tool call — and it never throws, because a thrown `tool_call` handler blocks
|
|
100
|
+
// the tool fail-safe (S8). The one thing it reads from disk is a path's parent
|
|
101
|
+
// directory, and only to resolve a symlink: no file content is ever read.
|
|
87
102
|
|
|
103
|
+
const fs = require('node:fs');
|
|
104
|
+
const os = require('node:os');
|
|
88
105
|
const nodePath = require('node:path');
|
|
89
106
|
|
|
90
107
|
/** S1 (S10): creating a worktree with Git, refused in every cwd. */
|
|
@@ -96,6 +113,15 @@ const WORKTREE_LIST_REFUSAL = '`git worktree list` did not run: use `worktree_li
|
|
|
96
113
|
/** S4 (S10): leaving the default branch of the principal checkout. */
|
|
97
114
|
const BRANCH_SWITCH_REFUSAL = 'The branch switch did not run: the principal checkout stays on its default branch. Create the ODD\'s checkout with `worktree_create`.';
|
|
98
115
|
|
|
116
|
+
/** S4 (T3): a path inside the principal checkout on its default branch. */
|
|
117
|
+
const WRITE_GUARD_REFUSAL = (file, branch) => `${file} is in the primary checkout on \`${branch}\`. Work in a worktree (\`worktree_list\` / \`worktree_create\`).`;
|
|
118
|
+
|
|
119
|
+
/** The two tools that write a file the agent names (S4, T3). */
|
|
120
|
+
const WRITE_GUARD_TOOLS = new Set(['edit', 'write']);
|
|
121
|
+
|
|
122
|
+
/** The OS temp directories whose paths are never judged, whatever lives there (L2). */
|
|
123
|
+
const TEMP_DIR_SPELLINGS = ['/tmp', '/var/tmp'];
|
|
124
|
+
|
|
99
125
|
/** git's global options that take their value as a SEPARATE argv element. */
|
|
100
126
|
const GIT_GLOBAL_OPTIONS_WITH_VALUE = new Set([
|
|
101
127
|
'-C',
|
|
@@ -459,6 +485,179 @@ function switchLeavesDefault(intent, defaultBranch) {
|
|
|
459
485
|
return intent.ref !== defaultBranch;
|
|
460
486
|
}
|
|
461
487
|
|
|
488
|
+
// ---------------------------------------------------------------------
|
|
489
|
+
// The write guard's own reading of a path (T3, S4, L2).
|
|
490
|
+
// ---------------------------------------------------------------------
|
|
491
|
+
|
|
492
|
+
/** The real path of `value`, or undefined when the filesystem cannot resolve it. */
|
|
493
|
+
function realpathOf(value) {
|
|
494
|
+
try {
|
|
495
|
+
return fs.realpathSync(value);
|
|
496
|
+
} catch {
|
|
497
|
+
return undefined;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/** Whether `child` IS `parent` or lies below it. Lexical: both are already resolved. */
|
|
502
|
+
function isInside(child, parent) {
|
|
503
|
+
if (child === parent) return true;
|
|
504
|
+
const base = parent.endsWith(nodePath.sep) ? parent : `${parent}${nodePath.sep}`;
|
|
505
|
+
return child.startsWith(base);
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* S4: the PRINCIPAL checkout of the repository the session stands in — the checkout
|
|
510
|
+
* that owns the shared git dir (`<root>/.git`, so the principal is that dir's parent,
|
|
511
|
+
* for the principal itself and for a linked worktree alike). Undefined when the session
|
|
512
|
+
* is outside version control, when the identity cannot be read, and when the common dir
|
|
513
|
+
* is not a `.git` of a normal repository (a bare repository, a separate git dir): an
|
|
514
|
+
* unplaceable repository refuses nothing (L2). Never throws.
|
|
515
|
+
*/
|
|
516
|
+
function sessionPrincipal(repoIdentity, cwd) {
|
|
517
|
+
if (typeof repoIdentity !== 'function') return undefined;
|
|
518
|
+
if (typeof cwd !== 'string' || cwd.length === 0) return undefined;
|
|
519
|
+
try {
|
|
520
|
+
const identity = repoIdentity(cwd);
|
|
521
|
+
if (!identity || typeof identity.commonDir !== 'string' || identity.commonDir.length === 0) return undefined;
|
|
522
|
+
const commonDir = nodePath.resolve(identity.commonDir);
|
|
523
|
+
if (nodePath.basename(commonDir) !== '.git') return undefined;
|
|
524
|
+
const principal = nodePath.dirname(commonDir);
|
|
525
|
+
return realpathOf(principal) || principal;
|
|
526
|
+
} catch {
|
|
527
|
+
return undefined;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/** The real root of the checkout that owns `dir`, or undefined when it cannot be read. Never throws. */
|
|
532
|
+
function ownerRootOf(repoIdentity, dir) {
|
|
533
|
+
try {
|
|
534
|
+
const identity = repoIdentity(dir);
|
|
535
|
+
if (!identity || typeof identity.root !== 'string' || identity.root.length === 0) return undefined;
|
|
536
|
+
const root = nodePath.resolve(identity.root);
|
|
537
|
+
return realpathOf(root) || root;
|
|
538
|
+
} catch {
|
|
539
|
+
return undefined;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* L2: the spellings a path is compared against to learn it is in a temp directory. The
|
|
545
|
+
* OS answer, the two fixed ones, the environment's own, and the realpath of each: on a
|
|
546
|
+
* platform where `/tmp` is a symlink, both spellings mean the same directory.
|
|
547
|
+
*/
|
|
548
|
+
function tempRoots(env) {
|
|
549
|
+
const roots = [];
|
|
550
|
+
const push = (value) => {
|
|
551
|
+
if (typeof value !== 'string' || value.length === 0) return;
|
|
552
|
+
let resolved;
|
|
553
|
+
try {
|
|
554
|
+
resolved = nodePath.resolve(value);
|
|
555
|
+
} catch {
|
|
556
|
+
return;
|
|
557
|
+
}
|
|
558
|
+
for (const spelling of [resolved, realpathOf(resolved)]) {
|
|
559
|
+
if (typeof spelling === 'string' && spelling.length > 0 && !roots.includes(spelling)) roots.push(spelling);
|
|
560
|
+
}
|
|
561
|
+
};
|
|
562
|
+
for (const value of TEMP_DIR_SPELLINGS) push(value);
|
|
563
|
+
try {
|
|
564
|
+
push(os.tmpdir());
|
|
565
|
+
} catch {
|
|
566
|
+
// An OS without an answer simply contributes no root.
|
|
567
|
+
}
|
|
568
|
+
if (env && typeof env === 'object') push(env.TMPDIR);
|
|
569
|
+
return roots;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* S4: the absolute path a written file really is, or undefined when it cannot be
|
|
574
|
+
* placed. The FILE may not exist yet — a new file in an existing directory is judged by
|
|
575
|
+
* that directory — so the existing parent is what is resolved, and a symlinked spelling
|
|
576
|
+
* of a checkout resolves to the checkout itself instead of dodging the check.
|
|
577
|
+
*/
|
|
578
|
+
function judgedPath(raw, cwd) {
|
|
579
|
+
if (typeof raw !== 'string' || raw.length === 0) return undefined;
|
|
580
|
+
// Pi strips one leading `@` from a file tool's path before it opens the file, so the
|
|
581
|
+
// guard judges the path the tool will really touch, not the spelling it was given.
|
|
582
|
+
const spelled = raw.startsWith('@') && raw.length > 1 ? raw.slice(1) : raw;
|
|
583
|
+
let absolute;
|
|
584
|
+
try {
|
|
585
|
+
absolute = nodePath.resolve(cwd, spelled);
|
|
586
|
+
} catch {
|
|
587
|
+
return undefined;
|
|
588
|
+
}
|
|
589
|
+
const parent = realpathOf(nodePath.dirname(absolute));
|
|
590
|
+
if (parent === undefined) return undefined;
|
|
591
|
+
return nodePath.join(parent, nodePath.basename(absolute));
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* Decide one `edit`/`write` tool call (T3, S4, L2): `{block: false}`, or
|
|
596
|
+
* `{block: true, reason}` with the one sentence the session reads. Inputs:
|
|
597
|
+
* `toolName`, `paths` (every path the call touches), `cwd`, and the same seams the
|
|
598
|
+
* `bash` guard takes: `repoIdentity(cwd)`, `defaultBranch(root)` and
|
|
599
|
+
* `currentBranch(root)` — the last one read fresh, because only the checkout knows
|
|
600
|
+
* where it points. Never throws.
|
|
601
|
+
*/
|
|
602
|
+
function decideWriteGuard(input) {
|
|
603
|
+
try {
|
|
604
|
+
if (!input || typeof input !== 'object') return { block: false };
|
|
605
|
+
if (!WRITE_GUARD_TOOLS.has(input.toolName)) return { block: false };
|
|
606
|
+
const raw = typeof input.paths === 'string' ? [input.paths] : input.paths;
|
|
607
|
+
if (!Array.isArray(raw) || raw.length === 0) return { block: false };
|
|
608
|
+
const cwd = typeof input.cwd === 'string' && input.cwd.length > 0 ? input.cwd : undefined;
|
|
609
|
+
if (cwd === undefined) return { block: false };
|
|
610
|
+
|
|
611
|
+
// L2: a path under a temp directory is never judged, and that answer must cost no
|
|
612
|
+
// git at all — scratch work (and a scratch repository) is the common case, so it is
|
|
613
|
+
// settled first, before the session's own checkout is even resolved. A path the
|
|
614
|
+
// filesystem cannot place is settled here too.
|
|
615
|
+
let temps;
|
|
616
|
+
const candidates = [];
|
|
617
|
+
for (const value of raw) {
|
|
618
|
+
const judged = judgedPath(value, cwd);
|
|
619
|
+
if (judged === undefined) continue;
|
|
620
|
+
if (temps === undefined) temps = tempRoots(process.env);
|
|
621
|
+
if (temps.some((root) => isInside(judged, root))) continue;
|
|
622
|
+
candidates.push(judged);
|
|
623
|
+
}
|
|
624
|
+
if (candidates.length === 0) return { block: false };
|
|
625
|
+
|
|
626
|
+
// S4: only the session's OWN repository is ever judged. Outside version control,
|
|
627
|
+
// or with an identity the seam cannot answer, nothing blocks.
|
|
628
|
+
const principal = sessionPrincipal(input.repoIdentity, cwd);
|
|
629
|
+
if (principal === undefined) return { block: false };
|
|
630
|
+
|
|
631
|
+
// The first path that really lies in the principal checkout is the one the refusal
|
|
632
|
+
// names; a path in another repository, or in a linked worktree, is placed and passes.
|
|
633
|
+
const target = candidates.find((judged) => isInside(judged, principal));
|
|
634
|
+
if (target === undefined) return { block: false };
|
|
635
|
+
|
|
636
|
+
// A linked worktree or a nested repository can live INSIDE the principal directory,
|
|
637
|
+
// so lying under it is not enough: the checkout that really owns the path's own
|
|
638
|
+
// directory must be the principal. Another owner, or the principal itself, is settled
|
|
639
|
+
// by that directory's identity; one the seam cannot answer is not judged (L2).
|
|
640
|
+
// The owner is asked through its own seam, never a remembered answer: a nested
|
|
641
|
+
// worktree can appear during the session. It falls back to the session's seam.
|
|
642
|
+
const ownerSeam = typeof input.ownerIdentity === 'function' ? input.ownerIdentity : input.repoIdentity;
|
|
643
|
+
const owner = ownerRootOf(ownerSeam, nodePath.dirname(target));
|
|
644
|
+
if (owner === undefined || owner !== principal) return { block: false };
|
|
645
|
+
|
|
646
|
+
// S4: the rule protects the default branch, so the checkout must be ON it. The
|
|
647
|
+
// current branch is read first and never cached; a detached HEAD, an unreadable
|
|
648
|
+
// answer or any other branch passes without resolving the default at all.
|
|
649
|
+
const current = currentBranchOf(input.currentBranch, principal);
|
|
650
|
+
if (current === undefined) return { block: false };
|
|
651
|
+
const defaultBranch = defaultBranchOf(input.defaultBranch, principal);
|
|
652
|
+
if (defaultBranch === undefined || defaultBranch !== current) return { block: false };
|
|
653
|
+
return { block: true, reason: WRITE_GUARD_REFUSAL(target, defaultBranch) };
|
|
654
|
+
} catch {
|
|
655
|
+
// S8: a write guard that threw would block the tool fail-safe, and a path it cannot
|
|
656
|
+
// judge must pass (L2), so nothing escapes here.
|
|
657
|
+
return { block: false };
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
|
|
462
661
|
// ---------------------------------------------------------------------
|
|
463
662
|
// The decision (S1–S6, S8–S10).
|
|
464
663
|
// ---------------------------------------------------------------------
|
|
@@ -545,4 +744,5 @@ function decideBashGuard(input) {
|
|
|
545
744
|
|
|
546
745
|
module.exports = {
|
|
547
746
|
decideBashGuard,
|
|
747
|
+
decideWriteGuard,
|
|
548
748
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-odd-worktree",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.0",
|
|
4
4
|
"description": "Pi package for odd-worktree: the Git worktree tools a Pi session uses (create and list the checkouts the agent works in), the /warp worktree panel, the /odd ODD viewer, and the /issues GitHub issue reader. Needs only Pi and git: no companion app, no extra service.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|