pi-odd-worktree 3.8.1 → 3.9.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 CHANGED
@@ -79,6 +79,7 @@ The full rules of each step, including every refusal, are in [Create, list, move
79
79
  - **Background-job tools are cards too.** While `Card activity` answers with `activity` for the tool being painted, `bash_background`, `monitor`, `job_stop` and `job_list` are drawn in the shell's own card style instead of Pi's plain box. Any other answer leaves Pi's box untouched. See [the jobs card](docs/reference.md#the-jobs-card).
80
80
  - **The two advisory messages are cards too.** While `Card activity` answers with `activity`, `gentle-pi.exploration-budget` and `gentle-pi.verifier-settle-reminder` are drawn as `⌕ Exploration budget` and `⚑ Verifier reminder` in the shell's own card style, the whole body at every content level. They follow that line — no second switch, no row added to `Cards`, and a flip repaints the messages on screen and turns them off again. Any other answer hands the message back as Pi drew it, for the price of a registry walk and at most one tool-document read, and a bridge older than `0.12.1` — or one whose release string is not a clean numeric release — registers nothing at all. See [the message cards](docs/reference.md#the-message-cards).
81
81
  - **`edit` and `write` cannot land in the principal checkout's default branch.** A write whose path is inside the principal checkout of the session's repository, while that checkout is on the default branch, is refused with one line pointing at `worktree_list` and `worktree_create`. Paths outside the repository, anything under the OS temp directories, ODD artifacts (any path below an `odd/` folder, on any branch), other repositories and linked worktrees are never blocked. See [the guard](docs/reference.md#the-edit-and-write-guard).
82
+ - **A skill or an ODD is read whole.** A `read` of a `.md` below a `skills/` or an `odd/` folder drops its `offset` and `limit` before the tool runs, so the call delivers the whole document and never half a procedure. It follows the `Worktrees+` module, like the guards.
82
83
  - **`bash` cannot leave the principal checkout's default branch.** A `git worktree add` is refused in any cwd and pointed at `worktree_create`; in the principal checkout, a switch that would leave the branch it is on is refused while that branch is the repository's default, and creating a branch is not blocked. See [the guards](docs/reference.md#the-bash-guards).
83
84
  - **Two blocks in the system prompt.** `## ODD worktrees` tells the agent how worktrees and ODDs work. `## CodeGraph` tells a session that already holds a CodeGraph tool how to use it, with one text per role. Both are appended after the host's own text, and `ODD_WORKTREE_PROMPT=0` suppresses them. See [the prompt blocks](docs/reference.md#the--odd-worktrees-block-in-the-system-prompt).
84
85
  - **CodeGraph stays fresh, only when its CLI is installed.** At session start the package creates or syncs the checkout's index and warms its daemon, and makes sure your global `mcp.json` has the `codegraph` entry (it adds it or fixes only the differing fields and keeps the rest). It also syncs after a compaction, after a native `codegraph init` and after an `edit` or `write`, in the background and without ever blocking you. See [CodeGraph](docs/reference.md#codegraph-index-daemon-and-mcp-entry). Its tool rows are drawn as a card.
@@ -3070,6 +3070,24 @@ function readCurrentBranch(git, root) {
3070
3070
  // command string, and only for the `bash` tool. Nothing is registered here either — no
3071
3071
  // command, no tool, no option and no setting — and `ODD_WORKTREE_GUARD=0`, the worktree
3072
3072
  // guard's own switch, turns this guard off the same way, read per call.
3073
+ // S1 (skill-read-full): a skill's or an ODD's Markdown is read WHOLE. The host's `read` takes `offset`
3074
+ // and `limit`, so a caller paging through a skill cuts it in half — and a half-read skill
3075
+ // states an incomplete procedure. This observation DROPS those two fields from the call the
3076
+ // host executes (`event.input` is the host's mutable seam; no re-validation follows), so the
3077
+ // read delivers the file from its first line to its last. It blocks nothing, answers nothing
3078
+ // and adds no tool: a call that is not a skill's Markdown is left byte-identical, and the
3079
+ // decision itself is `lib/skill-read-full.js`'s, which reads the file card's own identity.
3080
+ function keepSkillReadWholeCall(event) {
3081
+ try {
3082
+ if (!event || event.toolName !== "read") return undefined;
3083
+ libRequire("artifact-read-full.js").keepArtifactReadWhole("read", event.input);
3084
+ return undefined;
3085
+ } catch {
3086
+ // A read is never failed by a rule that only meant to complete it.
3087
+ return undefined;
3088
+ }
3089
+ }
3090
+
3073
3091
  function guardSkillReadBashCall(event) {
3074
3092
  try {
3075
3093
  if (process.env.ODD_WORKTREE_GUARD === "0") return undefined;
@@ -9934,7 +9952,14 @@ function registerOddWorktreeSurfaceEntries(pi) {
9934
9952
  // of the two, so a call that would break both shows the worktree refusal. It reads the
9935
9953
  // command text only, shares the same `ODD_WORKTREE_GUARD=0` switch, and adds no
9936
9954
  // command, tool, option or setting of its own.
9937
- pi.on("tool_call", (event) => guardSkillReadBashCall(event));
9955
+ // invariant 22 (S5 of skill-read-full): the skill-read guard is the second refusal — a
9956
+ // `bash` reader pointed at a skill file — and it follows the `Worktrees+` module, like every
9957
+ // other guard here: with that module off, neither refusal nor the read rule below exists.
9958
+ if (oddModules.worktrees) pi.on("tool_call", (event) => guardSkillReadBashCall(event));
9959
+ // S1/S2/S5 (skill-read-full): the other half of the same rule — a skill's or an ODD's
9960
+ // Markdown is read whole, so a `read` that pages through one has its range dropped before
9961
+ // the host runs it. Same switch: with `Worktrees+` off, a read is left as the host proposed it.
9962
+ if (oddModules.worktrees) pi.on("tool_call", (event) => keepSkillReadWholeCall(event));
9938
9963
  if (oddModules.worktrees) pi.on("tool_result", (event, ctx) => handleWorktreeOddEvent(event, ctx, pi, true));
9939
9964
  // S10 (T9): ONE `tool_result` observation for the rail's forced refresh. It
9940
9965
  // answers the `bash` tool only — every other tool returns before the classifier
@@ -0,0 +1,68 @@
1
+ 'use strict';
2
+ // S1/S5 (skill-read-full): a skill's or an ODD's Markdown is read WHOLE.
3
+ //
4
+ // The host's `read` tool takes `offset` and `limit`, and a caller that pages through a
5
+ // long file cuts a skill in half — the half that is read then states an incomplete
6
+ // procedure, which is worse than not reading it. So this module decides one thing: when
7
+ // a `read` call names a Markdown file below a `skills` or an `odd` folder, the call's
8
+ // `offset` and `limit` are DROPPED from the input the host executes.
9
+ //
10
+ // It blocks nothing and answers nothing. The decision is a mutation of the host's own
11
+ // `event.input`, the documented seam ("`event.input` is mutable... Mutations to
12
+ // `event.input` affect the actual tool execution"), and a call that is not a skill's
13
+ // Markdown is left byte-identical.
14
+ //
15
+ // "A skill's or an ODD's Markdown" is the file card's identity, reused and never copied
16
+ // (`lib/skill-odd-card.js`): the FOLDER decides, by path components, so
17
+ // `lib/skillset/notes.md` and `docs/oddity.md` are ordinary files. The host still applies its own read
18
+ // budget (2000 lines or 50 KB); a skill is far below both, and a file that size is not
19
+ // a skill anyone can follow.
20
+
21
+ const { ODD_FILE_CARD, SKILL_FILE_CARD, fileCardIdentity, isMarkdownFile } = require('./skill-odd-card.js');
22
+
23
+ const READ_TOOL = 'read';
24
+ // The two fields that cut a file. `path` is the only required one, so these are the
25
+ // whole difference between a whole read and a partial one.
26
+ const PARTIAL_FIELDS = Object.freeze(['offset', 'limit']);
27
+
28
+ /** The two identities whose Markdown is read whole: a skill's and an ODD's. */
29
+ const WHOLE_READ_IDENTITIES = Object.freeze([SKILL_FILE_CARD, ODD_FILE_CARD]);
30
+
31
+ /** Whether a path names a skill's or an ODD's Markdown: the folder decides, then the `.md` name. */
32
+ function isArtifactMarkdown(path) {
33
+ const identity = fileCardIdentity(path);
34
+ return isMarkdownFile(path) && identity !== undefined && WHOLE_READ_IDENTITIES.includes(identity);
35
+ }
36
+
37
+ /**
38
+ * Patch one `read` call before the host runs it: `{ patched, dropped }`, where `dropped`
39
+ * lists the fields this call no longer carries. A call that is not an artifact's Markdown, or
40
+ * that carries neither field, is untouched and reports `patched: false`. Mutates the
41
+ * object it is given, in place, and never throws — a `tool_call` handler that threw would
42
+ * fail the call it was only meant to improve.
43
+ */
44
+ function keepArtifactReadWhole(toolName, input) {
45
+ const answer = { patched: false, dropped: [] };
46
+ try {
47
+ if (toolName !== READ_TOOL) return answer;
48
+ if (input === null || typeof input !== 'object') return answer;
49
+ if (!isArtifactMarkdown(input.path)) return answer;
50
+ for (const field of PARTIAL_FIELDS) {
51
+ if (!Object.hasOwn(input, field)) continue;
52
+ delete input[field];
53
+ answer.dropped.push(field);
54
+ }
55
+ answer.patched = answer.dropped.length > 0;
56
+ return answer;
57
+ } catch {
58
+ return answer;
59
+ }
60
+ }
61
+
62
+ module.exports = {
63
+ PARTIAL_FIELDS,
64
+ READ_TOOL,
65
+ WHOLE_READ_IDENTITIES,
66
+ isArtifactMarkdown,
67
+ keepArtifactReadWhole,
68
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-odd-worktree",
3
- "version": "3.8.1",
3
+ "version": "3.9.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",
@@ -75,9 +75,12 @@ describe('Worktrees+', () => {
75
75
  assert.equal(off.commands.has('warp'), false);
76
76
  assert.equal(off.messageRenderers.has('worktree-warp'), false);
77
77
  assert.doesNotMatch(await promptOf(off, []), /## ODD worktrees/);
78
- // the naming handler, the bash guard and the write guard go; the skill-read guard stays
79
- assert.equal(toolCallHandlers(on) - toolCallHandlers(off), 3);
80
- assert.ok(toolCallHandlers(off) >= 1, 'the skill-read guard is transversal');
78
+ // the naming handler, the bash guard, the write guard, the skill-read bash guard and the
79
+ // whole-read rule go: every ``tool_call`` observation of the worktree half
80
+ assert.equal(toolCallHandlers(on) - toolCallHandlers(off), 5);
81
+ // The CodeGraph wiring adds ONE `tool_call` observation of its own, and only where its CLI is
82
+ // installed — so a host without it leaves zero, and neither answer is this half's.
83
+ assert.ok(toolCallHandlers(off) <= 1, `at most the CodeGraph observation is left, got ${toolCallHandlers(off)}`);
81
84
  for (const name of ['odd', 'files']) assert.ok(off.commands.has(name), `${name} stays`);
82
85
  assert.equal(off.hooks.some(([hook]) => hook === 'agent_settled'), false, 'the warp settle tick goes');
83
86
  });
@@ -137,13 +137,14 @@ describe('registration', () => {
137
137
  }
138
138
  });
139
139
 
140
- test('the skill-read guard is one more bash hook and adds no name (invariant 22)', async () => {
141
- // invariant 22: the skill-read guard is one more `bash` observation, so the `tool_call`
142
- // hooks stay the naming handler, the worktree bash and write guards and this one — a
143
- // fifth would be a surface nobody asked for.
140
+ test('the skill rules are two more hooks and add no name (invariant 22)', async () => {
141
+ // invariant 22 (S5 of skill-read-full): the `tool_call` hooks are the naming handler, the
142
+ // worktree bash and write guards, the skill-read bash guard and the whole-read rule, all of
143
+ // them under `Worktrees+` — a sixth would be a surface nobody asked for.
144
144
  const source = stubbedSource();
145
- assert.equal(source.split('pi.on("tool_call"').length - 1, 4, 'the naming handler, the worktree bash and write guards and the skill-read guard');
145
+ assert.equal(source.split('pi.on("tool_call"').length - 1, 5, 'the naming handler, the two worktree guards, the skill-read guard and the whole-read rule');
146
146
  assert.ok(source.includes('libRequire("skill-read-guard.js")'), 'the decision comes from lib/skill-read-guard.js');
147
+ assert.ok(source.includes('libRequire("artifact-read-full.js")'), 'the whole-read decision comes from lib/artifact-read-full.js');
147
148
  const { commands, tools } = await loadExtension({});
148
149
  for (const gone of ['skill-read', 'skill_read', 'skill-read-guard']) {
149
150
  assert.equal(commands.has(gone), false, `the guard adds no /${gone} command`);
@@ -0,0 +1,156 @@
1
+ 'use strict';
2
+ // S1-S5 (skill-read-full): a skill's or an ODD's Markdown is read whole, the decision the
3
+ // extension's `tool_call` handler makes is a mutation of the host's own input, and the rule
4
+ // follows the `Worktrees+` module switch.
5
+ //
6
+ // The pure half is exercised directly; the wiring half loads the extension and drives
7
+ // the registered handlers with the event the host sends, which is the only way to prove
8
+ // the mutation reaches the tool.
9
+ const { test, describe } = require('node:test');
10
+ const assert = require('node:assert/strict');
11
+ const os = require('node:os');
12
+
13
+ const { isArtifactMarkdown, keepArtifactReadWhole } = require('../lib/artifact-read-full.js');
14
+ const { BRIDGE_SLOT, defaultBridge, loadExtension } = require('./fixtures/load-extension.js');
15
+
16
+ // S5: the module switch is read from the bridge's own store when the extension registers, so a
17
+ // suite that turns `Worktrees+` off publishes the document first and restores the slot after.
18
+ const savedBridge = globalThis[BRIDGE_SLOT];
19
+ async function loadWithModules(modules) {
20
+ const bridge = defaultBridge();
21
+ bridge.api.createStore = () => ({
22
+ get: (key, fallback) => (key === 'modules' && modules !== undefined ? modules : fallback),
23
+ set: () => {},
24
+ delete: () => {},
25
+ });
26
+ globalThis[BRIDGE_SLOT] = bridge.api;
27
+ try {
28
+ return await loadExtension({});
29
+ } finally {
30
+ if (savedBridge === undefined) delete globalThis[BRIDGE_SLOT];
31
+ else globalThis[BRIDGE_SLOT] = savedBridge;
32
+ }
33
+ }
34
+
35
+ const SKILL = '/home/me/project/skills/release/SKILL.md';
36
+
37
+ describe('what counts as an artifact\'s Markdown', () => {
38
+ test('a .md below a skills or an odd folder, at any depth', () => {
39
+ for (const path of [
40
+ SKILL,
41
+ 'skills/release/SKILL.md',
42
+ '/a/b/skills/x/references/notes.md',
43
+ 'C:\\repo\\skills\\x\\SKILL.MD',
44
+ 'odd/tasks/a.md',
45
+ '/home/me/project/odd/tasks/section-placement.md',
46
+ ]) {
47
+ assert.equal(isArtifactMarkdown(path), true, path);
48
+ }
49
+ });
50
+
51
+ test('everything else is not: other folders, a non-Markdown file, a sibling name, no path', () => {
52
+ for (const path of [
53
+ 'lib/skillset/notes.md',
54
+ 'docs/oddity.md',
55
+ 'skills/release/scripts/run.js',
56
+ 'skills/release/SKILL',
57
+ undefined,
58
+ null,
59
+ 42,
60
+ '',
61
+ ]) {
62
+ assert.equal(isArtifactMarkdown(path), false, String(path));
63
+ }
64
+ });
65
+ });
66
+
67
+ describe('the patch', () => {
68
+ test('offset and limit are dropped, and both are reported', () => {
69
+ const input = { path: SKILL, offset: 1, limit: 90 };
70
+ assert.deepEqual(keepArtifactReadWhole('read', input), { patched: true, dropped: ['offset', 'limit'] });
71
+ assert.deepEqual(input, { path: SKILL }, 'the host executes a whole read');
72
+ });
73
+
74
+ test('one field alone is dropped alone', () => {
75
+ const onlyOffset = { path: SKILL, offset: 12 };
76
+ assert.deepEqual(keepArtifactReadWhole('read', onlyOffset), { patched: true, dropped: ['offset'] });
77
+ assert.deepEqual(onlyOffset, { path: SKILL });
78
+ const onlyLimit = { path: SKILL, limit: 5 };
79
+ assert.deepEqual(keepArtifactReadWhole('read', onlyLimit), { patched: true, dropped: ['limit'] });
80
+ assert.deepEqual(onlyLimit, { path: SKILL });
81
+ });
82
+
83
+ test('a whole read, another tool and a non-skill path are untouched', () => {
84
+ const whole = { path: SKILL };
85
+ assert.deepEqual(keepArtifactReadWhole('read', whole), { patched: false, dropped: [] });
86
+ assert.deepEqual(whole, { path: SKILL });
87
+
88
+ const other = { path: 'lib/odd-worktree-worktrees/x.md', offset: 1, limit: 2, extra: 'kept' };
89
+ assert.deepEqual(keepArtifactReadWhole('read', other), { patched: false, dropped: [] });
90
+ assert.deepEqual(other, { path: 'lib/odd-worktree-worktrees/x.md', offset: 1, limit: 2, extra: 'kept' });
91
+
92
+ const edit = { path: SKILL, offset: 1, limit: 2 };
93
+ assert.deepEqual(keepArtifactReadWhole('edit', edit), { patched: false, dropped: [] });
94
+ assert.deepEqual(edit, { path: SKILL, offset: 1, limit: 2 });
95
+ });
96
+
97
+ test('a hostile input never throws', () => {
98
+ for (const value of [null, undefined, 42, 'path', []]) {
99
+ assert.doesNotThrow(() => keepArtifactReadWhole('read', value));
100
+ assert.deepEqual(keepArtifactReadWhole('read', value), { patched: false, dropped: [] });
101
+ }
102
+ const hostile = { get path() { throw new Error('boom'); }, offset: 1 };
103
+ assert.deepEqual(keepArtifactReadWhole('read', hostile), { patched: false, dropped: [] });
104
+ });
105
+ });
106
+
107
+ describe('the extension wiring', () => {
108
+ const readEvent = (input) => ({ type: 'tool_call', toolName: 'read', toolCallId: 'call-1', input });
109
+
110
+ const runToolCall = async (loaded, event, ctx = { cwd: os.tmpdir() }) => {
111
+ let answer;
112
+ for (const [name, handler] of loaded.hooks) {
113
+ if (name !== 'tool_call') continue;
114
+ const result = await handler(event, ctx);
115
+ if (result !== undefined) answer = result;
116
+ }
117
+ return answer;
118
+ };
119
+
120
+ test('a skill read with a range is executed whole, and the call is not blocked', async () => {
121
+ const loaded = await loadExtension({});
122
+ const event = readEvent({ path: SKILL, offset: 1, limit: 90 });
123
+ const answer = await runToolCall(loaded, event);
124
+ assert.equal(answer, undefined, 'nothing is blocked: the call still runs');
125
+ assert.deepEqual(event.input, { path: SKILL }, 'the host executes a whole read');
126
+ });
127
+
128
+ test('an ordinary read keeps its range, byte for byte', async () => {
129
+ const loaded = await loadExtension({});
130
+ const event = readEvent({ path: 'lib/skill-read-full.js', offset: 3, limit: 40 });
131
+ await runToolCall(loaded, event);
132
+ assert.deepEqual(event.input, { path: 'lib/skill-read-full.js', offset: 3, limit: 40 });
133
+ });
134
+
135
+ test('S5: worktrees+ off leaves the read exactly as the host proposed it', async () => {
136
+ const loaded = await loadWithModules({ worktrees: false });
137
+ const event = readEvent({ path: SKILL, offset: 1, limit: 90 });
138
+ await runToolCall(loaded, event);
139
+ assert.deepEqual(event.input, { path: SKILL, offset: 1, limit: 90 });
140
+ });
141
+
142
+ test('an odd artifact read with a range is executed whole too', async () => {
143
+ const loaded = await loadExtension({});
144
+ const event = readEvent({ path: 'odd/tasks/x.md', offset: 40, limit: 10 });
145
+ await runToolCall(loaded, event);
146
+ assert.deepEqual(event.input, { path: 'odd/tasks/x.md' });
147
+ });
148
+
149
+ test('the old bash guard still refuses a skill printed with a reader', async () => {
150
+ const loaded = await loadExtension({});
151
+ const event = { type: 'tool_call', toolName: 'bash', toolCallId: 'call-2', input: { command: `cat ${SKILL}` } };
152
+ const answer = await runToolCall(loaded, event);
153
+ assert.equal(answer?.block, true, 'the bash reader is still refused');
154
+ assert.match(answer.reason, /read/);
155
+ });
156
+ });