pi-odd-worktree 3.13.0 → 3.15.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,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
+ });