pi-odd-worktree 3.12.4 → 3.14.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.
@@ -0,0 +1,159 @@
1
+ 'use strict';
2
+ // Where the session is named FROM, through the extension's own wiring.
3
+ //
4
+ // The rule (the user's): an ODD write or edit renames the session to that ODD,
5
+ // wherever it lands. A worktree was already a naming source; the PRINCIPAL checkout
6
+ // was not — the naming observer resolved a registry row and returned early for
7
+ // anything else, so writing `odd/tasks/<name>.md` from the repository itself renamed
8
+ // nothing. `odd/` is the one path the write guard leaves alone in the primary
9
+ // checkout, which is exactly the case this closes.
10
+ //
11
+ // What is asserted here, through the fake host only (the events the host really sends):
12
+ // - A `tool_call` writing `odd/tasks/prompt-card-fix.md` from the PRINCIPAL checkout
13
+ // renames the session to `Prompt card fix`.
14
+ // - The name comes from the document's own name, so the prose inside it is irrelevant
15
+ // and the file need not exist yet.
16
+ // - A write anywhere else (`docs/`, a worktree-shaped path outside the repository)
17
+ // renames nothing.
18
+ // - A human's name is never overwritten.
19
+ //
20
+ // A real repository in the OS temp directory, because the naming observer asks git
21
+ // which checkout the call happened in; nothing else is faked.
22
+
23
+ const { test, describe, after } = require('node:test');
24
+ const assert = require('node:assert/strict');
25
+ const fs = require('node:fs');
26
+ const os = require('node:os');
27
+ const path = require('node:path');
28
+ const { execFileSync } = require('node:child_process');
29
+
30
+ const { BRIDGE_SLOT, loadExtension } = require('./fixtures/load-extension.js');
31
+
32
+ const scratch = [];
33
+ after(() => {
34
+ for (const dir of scratch) fs.rmSync(dir, { recursive: true, force: true });
35
+ delete globalThis[BRIDGE_SLOT];
36
+ });
37
+
38
+ function git(cwd, args) {
39
+ return execFileSync('git', args, { cwd, encoding: 'utf8' }).trim();
40
+ }
41
+
42
+ // A repository whose principal checkout IS the ODD's home: one commit, `odd/tasks/`
43
+ // present, nothing registered.
44
+ function makeRepo() {
45
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'odd-name-repo-'));
46
+ scratch.push(dir);
47
+ git(dir, ['init', '-q', '-b', 'main']);
48
+ git(dir, ['config', 'user.email', 'test@example.com']);
49
+ git(dir, ['config', 'user.name', 'Test']);
50
+ fs.mkdirSync(path.join(dir, 'odd', 'tasks'), { recursive: true });
51
+ fs.writeFileSync(path.join(dir, 'odd', 'tasks', 'PROMPT.txt'), 'place\n');
52
+ fs.writeFileSync(path.join(dir, '.gitignore'), 'odd/tasks/PROMPT.txt\n');
53
+ git(dir, ['add', '.gitignore']);
54
+ git(dir, ['commit', '-q', '-m', 'init']);
55
+ return dir;
56
+ }
57
+
58
+ // The host double: the session-name accessors the naming path requires. The name
59
+ // lives in a holder the test keeps its own reference to, because the loader spreads
60
+ // this object into the host it hands the extension (a method mutating `this` would
61
+ // land on that copy and the assertion would read a stale value).
62
+ function makeHost() {
63
+ const state = { name: '', entries: [] };
64
+ return {
65
+ state,
66
+ getSessionName() {
67
+ return state.name;
68
+ },
69
+ setSessionName(next) {
70
+ state.name = next;
71
+ },
72
+ };
73
+ }
74
+
75
+ function contextFor(host, cwd) {
76
+ return {
77
+ cwd,
78
+ sessionManager: {
79
+ getSessionId: () => 'session-wiring',
80
+ getEntries: () => host.state.entries.slice(),
81
+ },
82
+ };
83
+ }
84
+
85
+ // Every `tool_call` handler, in registration order, exactly as the host runs them.
86
+ function fireToolCall(extension, event, ctx) {
87
+ const answers = [];
88
+ for (const [name, handler] of extension.hooks) {
89
+ if (name !== 'tool_call') continue;
90
+ answers.push(handler(event, ctx));
91
+ }
92
+ return answers;
93
+ }
94
+
95
+ describe('naming from the principal checkout', () => {
96
+ test('a write on odd/tasks/<name>.md renames the session after the ODD', async () => {
97
+ const repo = makeRepo();
98
+ const host = makeHost();
99
+ const extension = await loadExtension(host);
100
+ fireToolCall(
101
+ extension,
102
+ { toolName: 'write', arguments: { path: 'odd/tasks/prompt-card-fix.md' } },
103
+ contextFor(host, repo),
104
+ );
105
+ assert.equal(host.state.name, 'Prompt card fix');
106
+ });
107
+
108
+ test('an edit renames exactly like a write', async () => {
109
+ const repo = makeRepo();
110
+ const host = makeHost();
111
+ const extension = await loadExtension(host);
112
+ fireToolCall(
113
+ extension,
114
+ { toolName: 'edit', arguments: { path: 'odd/tasks/warp-odd-picker.md' } },
115
+ contextFor(host, repo),
116
+ );
117
+ assert.equal(host.state.name, 'Warp odd picker');
118
+ });
119
+
120
+ test('a second ODD written from the same checkout renames again', async () => {
121
+ const repo = makeRepo();
122
+ const host = makeHost();
123
+ const extension = await loadExtension(host);
124
+ const first = contextFor(host, repo);
125
+ fireToolCall(extension, { toolName: 'write', arguments: { path: 'odd/tasks/prompt-card-fix.md' } }, first);
126
+ assert.equal(host.state.name, 'Prompt card fix');
127
+ fireToolCall(extension, { toolName: 'write', arguments: { path: 'odd/tasks/prs-panel.md' } }, first);
128
+ assert.equal(host.state.name, 'Prs panel');
129
+ });
130
+
131
+ test('a write outside odd/tasks renames nothing', async () => {
132
+ const repo = makeRepo();
133
+ const host = makeHost();
134
+ const extension = await loadExtension(host);
135
+ fireToolCall(extension, { toolName: 'write', arguments: { path: 'docs/reference.md' } }, contextFor(host, repo));
136
+ assert.equal(host.state.name, '');
137
+ });
138
+
139
+ test('a read of an ODD renames nothing', async () => {
140
+ const repo = makeRepo();
141
+ const host = makeHost();
142
+ const extension = await loadExtension(host);
143
+ fireToolCall(extension, { toolName: 'read', arguments: { path: 'odd/tasks/prompt-card-fix.md' } }, contextFor(host, repo));
144
+ assert.equal(host.state.name, '');
145
+ });
146
+
147
+ test('a human name survives an ODD write', async () => {
148
+ const repo = makeRepo();
149
+ const host = makeHost();
150
+ host.state.name = 'Mi sesión de investigación';
151
+ const extension = await loadExtension(host);
152
+ fireToolCall(
153
+ extension,
154
+ { toolName: 'write', arguments: { path: 'odd/tasks/prompt-card-fix.md' } },
155
+ contextFor(host, repo),
156
+ );
157
+ assert.equal(host.state.name, 'Mi sesión de investigación');
158
+ });
159
+ });
@@ -0,0 +1,342 @@
1
+ 'use strict';
2
+ // The ODD session name: lib/session-auto-name.js, the one module that decides what
3
+ // a session is called after it writes an ODD task document.
4
+ //
5
+ // What is asserted here, through the module's public functions:
6
+ // - D2 (label): the session is named after the ODD DOCUMENT ITSELF, never after
7
+ // prose inside it: `odd/tasks/prompt-card-fix.md` names the session `Prompt card
8
+ // fix` — `-` and `_` become spaces, `/` of a nested feature becomes a space, the
9
+ // first letter is capitalized and the rest of the name is left alone.
10
+ // - D3 (every write): each `write`/`edit` on an `odd/tasks/*.md` document re-derives
11
+ // the name, so a second ODD written later in the same session renames it again.
12
+ // - D4 (never clobber a human): a name the plugin cannot account for is untouched;
13
+ // the plugin writes only an empty name or one it can prove it produced.
14
+ // - D5 (resume): the proof survives a process restart because it comes from the
15
+ // ODDs this session already wrote (the persisted `odd-worktree.odd-touch` record
16
+ // hands their names in), not from memory alone; the dashed slugs earlier versions
17
+ // wrote stay adoptable for the same reason.
18
+ // - D6 (trigger): only a WRITE renames. Reading a document is context gathering and
19
+ // leaves the name alone, and so does any path that is not `odd/tasks/<name>.md`.
20
+ // - D7 (idempotent): the derived name already applied writes nothing.
21
+ // - D8 (phase): a `gentle_odd_phase` transition re-applies the session's own label,
22
+ // including after a resume, and invents nothing when the session never wrote one.
23
+ //
24
+ // Pure: no fs, no git, no clock. The branch is deliberately NOT part of the name any
25
+ // more (the user's decision: the session is called `Prompt card fix`, not
26
+ // `Prompt card fix @ feat/prompt-card-fix`).
27
+
28
+ const { test, describe } = require('node:test');
29
+ const assert = require('node:assert/strict');
30
+
31
+ const {
32
+ LABEL_MAX_CHARS,
33
+ ODD_PHASE_TOOL_NAME,
34
+ canAdoptSessionName,
35
+ isPluginSessionName,
36
+ labelFromFeature,
37
+ planPhaseSessionRename,
38
+ planSessionRename,
39
+ } = require('../lib/session-auto-name.js');
40
+
41
+ // A fresh session id per case: the module remembers the last applied label per
42
+ // session, and two cases must never share that memory.
43
+ let sessionSeq = 0;
44
+ function nextSessionId() {
45
+ sessionSeq += 1;
46
+ return `session-${sessionSeq}`;
47
+ }
48
+
49
+ const BRANCH = 'feat/prompt-card-fix';
50
+
51
+ describe('labelFromFeature: the ODD document names the session', () => {
52
+ test('dashes become spaces and only the first letter is capitalized', () => {
53
+ assert.equal(labelFromFeature('prompt-card-fix'), 'Prompt card fix');
54
+ });
55
+
56
+ test('the rest of the name keeps its own case', () => {
57
+ assert.equal(labelFromFeature('alt-i-and-PR-links'), 'Alt i and PR links');
58
+ });
59
+
60
+ test('underscores are a word separator too', () => {
61
+ assert.equal(labelFromFeature('rail_cards_fixes'), 'Rail cards fixes');
62
+ });
63
+
64
+ test('a nested feature keeps every segment, joined by spaces', () => {
65
+ assert.equal(labelFromFeature('perf/plan'), 'Perf plan');
66
+ });
67
+
68
+ test('a name that is not a feature yields no label', () => {
69
+ assert.equal(labelFromFeature(''), null);
70
+ assert.equal(labelFromFeature('---'), null);
71
+ assert.equal(labelFromFeature(undefined), null);
72
+ });
73
+
74
+ test('a long name is cut at the last whole word inside the limit', () => {
75
+ const label = labelFromFeature('a-very-long-odd-name-that-goes-past-the-limit-of-the-label');
76
+ assert.ok(label.length <= LABEL_MAX_CHARS, `label ${label} exceeds ${LABEL_MAX_CHARS}`);
77
+ assert.equal(label, 'A very long odd name that goes past the limit');
78
+ });
79
+
80
+ test('one word longer than the limit is cut hard, never left empty', () => {
81
+ const label = labelFromFeature('a'.repeat(LABEL_MAX_CHARS + 10));
82
+ assert.equal(label.length, LABEL_MAX_CHARS);
83
+ });
84
+ });
85
+
86
+ describe('planSessionRename: every ODD write renames the session', () => {
87
+ test('the first ODD write names an unnamed session after that document', () => {
88
+ const label = planSessionRename({
89
+ rels: ['odd/tasks/prompt-card-fix.md'],
90
+ isWrite: true,
91
+ currentName: '',
92
+ sessionId: nextSessionId(),
93
+ });
94
+ assert.equal(label, 'Prompt card fix');
95
+ });
96
+
97
+ test('a session with no name at all is adopted', () => {
98
+ const label = planSessionRename({
99
+ rels: ['odd/tasks/prompt-card-fix.md'],
100
+ isWrite: true,
101
+ currentName: undefined,
102
+ sessionId: nextSessionId(),
103
+ });
104
+ assert.equal(label, 'Prompt card fix');
105
+ });
106
+
107
+ test('writing ANOTHER ODD renames again, to that ODD', () => {
108
+ const sessionId = nextSessionId();
109
+ const first = planSessionRename({
110
+ rels: ['odd/tasks/prompt-card-fix.md'],
111
+ isWrite: true,
112
+ currentName: '',
113
+ sessionId,
114
+ });
115
+ assert.equal(first, 'Prompt card fix');
116
+ const second = planSessionRename({
117
+ rels: ['odd/tasks/warp-odd-picker.md'],
118
+ isWrite: true,
119
+ currentName: first,
120
+ sessionId,
121
+ });
122
+ assert.equal(second, 'Warp odd picker');
123
+ });
124
+
125
+ test('an EDIT renames exactly like a write', () => {
126
+ const sessionId = nextSessionId();
127
+ planSessionRename({
128
+ rels: ['odd/tasks/prompt-card-fix.md'],
129
+ isWrite: true,
130
+ currentName: 'Something else entirely',
131
+ sessionId,
132
+ });
133
+ const label = planSessionRename({
134
+ rels: ['odd/tasks/prs-panel.md'],
135
+ isWrite: true,
136
+ currentName: '',
137
+ sessionId,
138
+ });
139
+ assert.equal(label, 'Prs panel');
140
+ });
141
+
142
+ test('the same label already applied writes nothing', () => {
143
+ const sessionId = nextSessionId();
144
+ const first = planSessionRename({
145
+ rels: ['odd/tasks/prompt-card-fix.md'],
146
+ isWrite: true,
147
+ currentName: '',
148
+ sessionId,
149
+ });
150
+ const again = planSessionRename({
151
+ rels: ['odd/tasks/prompt-card-fix.md'],
152
+ isWrite: true,
153
+ currentName: first,
154
+ sessionId,
155
+ });
156
+ assert.equal(again, null);
157
+ });
158
+
159
+ test('a human name is never overwritten', () => {
160
+ const sessionId = nextSessionId();
161
+ const label = planSessionRename({
162
+ rels: ['odd/tasks/prompt-card-fix.md'],
163
+ isWrite: true,
164
+ currentName: 'Mi sesión de investigación',
165
+ sessionId,
166
+ });
167
+ assert.equal(label, null);
168
+ });
169
+
170
+ test('a READ renames nothing', () => {
171
+ const label = planSessionRename({
172
+ rels: ['odd/tasks/prompt-card-fix.md'],
173
+ isWrite: false,
174
+ currentName: '',
175
+ sessionId: nextSessionId(),
176
+ });
177
+ assert.equal(label, null);
178
+ });
179
+
180
+ test('a document outside odd/tasks renames nothing', () => {
181
+ const sessionId = nextSessionId();
182
+ assert.equal(
183
+ planSessionRename({
184
+ rels: ['docs/reference.md', 'odd/tasks/nested/deep/x.txt', 'odd/tasks/'],
185
+ isWrite: true,
186
+ currentName: '',
187
+ sessionId,
188
+ }),
189
+ null,
190
+ );
191
+ });
192
+
193
+ test('the title inside the document is not the source of the name', () => {
194
+ const label = planSessionRename({
195
+ rels: ['odd/tasks/prompt-card-fix.md'],
196
+ isWrite: true,
197
+ currentName: '',
198
+ sessionId: nextSessionId(),
199
+ // A caller that still hands a reader in changes nothing: the document's own
200
+ // prose never names the session.
201
+ readDocument: () => '# Una guía completamente distinta\n',
202
+ });
203
+ assert.equal(label, 'Prompt card fix');
204
+ });
205
+
206
+ test('the branch is not part of the name', () => {
207
+ const label = planSessionRename({
208
+ rels: ['odd/tasks/prompt-card-fix.md'],
209
+ isWrite: true,
210
+ branch: BRANCH,
211
+ currentName: '',
212
+ sessionId: nextSessionId(),
213
+ });
214
+ assert.equal(label, 'Prompt card fix');
215
+ assert.ok(!label.includes('@'), 'a branch suffix must not appear');
216
+ });
217
+ });
218
+
219
+ describe('resume: the proof survives a restart', () => {
220
+ // A resumed session has no in-memory memory: the only evidence is the ODDs this
221
+ // session already wrote, handed in from the persisted touch record.
222
+ test('a label that belongs to one of this session ODDs is its own', () => {
223
+ const label = planSessionRename({
224
+ rels: ['odd/tasks/warp-odd-picker.md'],
225
+ isWrite: true,
226
+ currentName: 'Prompt card fix',
227
+ sessionId: nextSessionId(),
228
+ touchedNames: ['prompt-card-fix'],
229
+ });
230
+ assert.equal(label, 'Warp odd picker');
231
+ });
232
+
233
+ test('a label belonging to no ODD of this session stays a human name', () => {
234
+ const label = planSessionRename({
235
+ rels: ['odd/tasks/warp-odd-picker.md'],
236
+ isWrite: true,
237
+ currentName: 'Prompt card fix',
238
+ sessionId: nextSessionId(),
239
+ touchedNames: ['unrelated-odd'],
240
+ });
241
+ assert.equal(label, null);
242
+ });
243
+
244
+ test('the dashed slugs earlier versions wrote stay adoptable', () => {
245
+ const label = planSessionRename({
246
+ rels: ['odd/tasks/warp-odd-picker.md'],
247
+ isWrite: true,
248
+ currentName: 'prompt-card-fix',
249
+ sessionId: nextSessionId(),
250
+ });
251
+ assert.equal(label, 'Warp odd picker');
252
+ });
253
+
254
+ test('a name with a branch suffix from an earlier version stays adoptable', () => {
255
+ const label = planSessionRename({
256
+ rels: ['odd/tasks/warp-odd-picker.md'],
257
+ isWrite: true,
258
+ currentName: `prompt-card-fix @ ${BRANCH}`,
259
+ sessionId: nextSessionId(),
260
+ });
261
+ assert.equal(label, 'Warp odd picker');
262
+ });
263
+ });
264
+
265
+ describe('isPluginSessionName / canAdoptSessionName: the ownership test', () => {
266
+ test('an empty or absent name is adoptable', () => {
267
+ assert.equal(canAdoptSessionName(''), true);
268
+ assert.equal(canAdoptSessionName(undefined), true);
269
+ });
270
+
271
+ test('a human name is not', () => {
272
+ assert.equal(isPluginSessionName('Mi sesión de investigación'), false);
273
+ assert.equal(canAdoptSessionName('Mi sesión de investigación', {}), false);
274
+ });
275
+
276
+ test('a name it produced for the touched document is its own', () => {
277
+ assert.equal(isPluginSessionName('Prompt card fix', { feature: 'prompt-card-fix' }), true);
278
+ });
279
+
280
+ test('a name from its own durable record is its own', () => {
281
+ assert.equal(
282
+ isPluginSessionName('Prompt card fix', { sessionLabels: ['Prompt card fix'] }),
283
+ true,
284
+ );
285
+ });
286
+
287
+ test('a non-string name is never adopted', () => {
288
+ assert.equal(canAdoptSessionName(42), false);
289
+ });
290
+ });
291
+
292
+ describe('planPhaseSessionRename: a phase transition re-applies what the session derived', () => {
293
+ test('the phase tool name is the one Pi publishes', () => {
294
+ assert.equal(ODD_PHASE_TOOL_NAME, 'gentle_odd_phase');
295
+ });
296
+
297
+ test('it re-applies the label this session last derived from a write', () => {
298
+ const sessionId = nextSessionId();
299
+ const first = planSessionRename({
300
+ rels: ['odd/tasks/prompt-card-fix.md'],
301
+ isWrite: true,
302
+ currentName: '',
303
+ sessionId,
304
+ });
305
+ // Something else set the name in between; the plugin's own label still applies.
306
+ const label = planPhaseSessionRename({ sessionId, currentName: '' });
307
+ assert.equal(label, first);
308
+ });
309
+
310
+ test('a session that never wrote an ODD invents nothing', () => {
311
+ assert.equal(planPhaseSessionRename({ sessionId: nextSessionId(), currentName: '' }), null);
312
+ });
313
+
314
+ test('after a resume it re-applies the newest ODD of its durable record', () => {
315
+ const label = planPhaseSessionRename({
316
+ sessionId: nextSessionId(),
317
+ currentName: '',
318
+ touchedNames: ['prompt-card-fix', 'warp-odd-picker'],
319
+ });
320
+ assert.equal(label, 'Prompt card fix');
321
+ });
322
+
323
+ test('it never overwrites a human name', () => {
324
+ const label = planPhaseSessionRename({
325
+ sessionId: nextSessionId(),
326
+ currentName: 'Mi sesión de investigación',
327
+ touchedNames: ['prompt-card-fix'],
328
+ });
329
+ assert.equal(label, null);
330
+ });
331
+
332
+ test('the label already applied writes nothing', () => {
333
+ const sessionId = nextSessionId();
334
+ planSessionRename({
335
+ rels: ['odd/tasks/prompt-card-fix.md'],
336
+ isWrite: true,
337
+ currentName: '',
338
+ sessionId,
339
+ });
340
+ assert.equal(planPhaseSessionRename({ sessionId, currentName: 'Prompt card fix' }), null);
341
+ });
342
+ });