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,423 @@
1
+ 'use strict';
2
+ // S5/S7 (opencode-usage-alert): the zone-aware peak engine. DeepSeek's windows are published in
3
+ // UTC, Claude's in Pacific time, so the engine reads `zone`, `windows` and `weekdaysOnly` from
4
+ // the registry entry it is handed and is otherwise provider-agnostic: a third provider needs no
5
+ // change here. Windows are half-open, weekdays are the provider's OWN weekdays, and nothing
6
+ // reads a clock — every function takes the instant it must reason about.
7
+ //
8
+ // The dates below are all 2026 and are the whole point of the zone parameter:
9
+ // 09-23 Wed, 09-25 Fri, 09-26 Sat, 09-27 Sun, 09-28 Mon — DeepSeek's UTC week
10
+ // 07-15 Wed, 07-17 Fri, 07-18 Sat, 07-19 Sun, 07-20 Mon — Claude under PDT (UTC-7)
11
+ // 01-14 Wed, 01-17 Sat — Claude under PST (UTC-8)
12
+ // 03-06 Fri, 03-09 Mon — across the PST->PDT switch on 03-08
13
+ const { test, describe } = require('node:test');
14
+ const assert = require('node:assert/strict');
15
+
16
+ const {
17
+ earliestUntil,
18
+ formatBoundaryClock,
19
+ formatCountdown,
20
+ formatPeakState,
21
+ nextTransitionFor,
22
+ peakStateFor,
23
+ } = require('../lib/peak-schedule.js');
24
+ const { providerById } = require('../lib/peak-providers.js');
25
+
26
+ const DEEPSEEK = providerById('deepseek');
27
+ const CLAUDE = providerById('claude');
28
+
29
+ const utc = (iso) => Date.parse(iso);
30
+
31
+ // A provider the registry does not declare: the engine is generic, so a future window is a
32
+ // plain object here and no code below has to know about it.
33
+ const TOKYO = {
34
+ id: 'third',
35
+ setting: 'Third peak',
36
+ label: 'third',
37
+ zone: 'Asia/Tokyo',
38
+ windows: [[9, 17]],
39
+ weekdaysOnly: true,
40
+ };
41
+ const ALWAYS = {
42
+ id: 'always',
43
+ setting: 'Always peak',
44
+ label: 'always',
45
+ zone: 'UTC',
46
+ windows: [[0, 12]],
47
+ weekdaysOnly: false,
48
+ };
49
+
50
+ describe('the DeepSeek schedule, published in UTC', () => {
51
+ test('both published windows are peak on a weekday', () => {
52
+ for (const iso of ['2026-09-23T01:00:00Z', '2026-09-23T02:30:00Z', '2026-09-23T03:59:59Z']) {
53
+ assert.equal(peakStateFor(DEEPSEEK, utc(iso)).kind, 'peak', iso);
54
+ }
55
+ for (const iso of ['2026-09-23T06:00:00Z', '2026-09-23T08:15:00Z', '2026-09-23T09:59:59Z']) {
56
+ assert.equal(peakStateFor(DEEPSEEK, utc(iso)).kind, 'peak', iso);
57
+ }
58
+ });
59
+
60
+ test('window bounds are half-open: a window starts on its first second and ends at its last', () => {
61
+ assert.equal(peakStateFor(DEEPSEEK, utc('2026-09-23T00:59:59Z')).kind, 'off-peak');
62
+ assert.equal(peakStateFor(DEEPSEEK, utc('2026-09-23T04:00:00Z')).kind, 'off-peak');
63
+ assert.equal(peakStateFor(DEEPSEEK, utc('2026-09-23T05:59:59Z')).kind, 'off-peak');
64
+ assert.equal(peakStateFor(DEEPSEEK, utc('2026-09-23T10:00:00Z')).kind, 'off-peak');
65
+ });
66
+
67
+ test('weekends are off-peak all day', () => {
68
+ for (const iso of [
69
+ '2026-09-26T02:00:00Z',
70
+ '2026-09-26T08:00:00Z',
71
+ '2026-09-27T02:00:00Z',
72
+ '2026-09-27T08:00:00Z',
73
+ ]) {
74
+ assert.equal(peakStateFor(DEEPSEEK, utc(iso)).kind, 'off-peak', iso);
75
+ }
76
+ });
77
+
78
+ test('the reported boundary is the end of the current state, in both directions', () => {
79
+ const duringPeak = peakStateFor(DEEPSEEK, utc('2026-09-23T03:40:00Z'));
80
+ assert.deepEqual(
81
+ duringPeak,
82
+ { kind: 'peak', until: utc('2026-09-23T04:00:00Z') },
83
+ 'peak ends at its window close',
84
+ );
85
+
86
+ assert.equal(peakStateFor(DEEPSEEK, utc('2026-09-23T09:45:00Z')).until, utc('2026-09-23T10:00:00Z'));
87
+
88
+ assert.deepEqual(
89
+ peakStateFor(DEEPSEEK, utc('2026-09-23T04:30:00Z')),
90
+ { kind: 'off-peak', until: utc('2026-09-23T06:00:00Z') },
91
+ 'the next window is the boundary',
92
+ );
93
+
94
+ assert.equal(peakStateFor(DEEPSEEK, utc('2026-09-23T00:30:00Z')).until, utc('2026-09-23T01:00:00Z'));
95
+ });
96
+
97
+ test('the boundary crosses into the next weekday, skipping the weekend', () => {
98
+ // Friday 10:30 UTC is off-peak until Monday 01:00 UTC; nothing peaks over the weekend.
99
+ assert.deepEqual(peakStateFor(DEEPSEEK, utc('2026-09-25T10:30:00Z')), {
100
+ kind: 'off-peak',
101
+ until: utc('2026-09-28T01:00:00Z'),
102
+ });
103
+ // Sunday evening is off-peak until Monday 01:00 UTC.
104
+ assert.deepEqual(peakStateFor(DEEPSEEK, utc('2026-09-27T23:00:00Z')), {
105
+ kind: 'off-peak',
106
+ until: utc('2026-09-28T01:00:00Z'),
107
+ });
108
+ // Friday's second window ends at 10:00 and the next peak is Monday 01:00.
109
+ assert.deepEqual(peakStateFor(DEEPSEEK, utc('2026-09-25T09:00:00Z')), {
110
+ kind: 'peak',
111
+ until: utc('2026-09-25T10:00:00Z'),
112
+ });
113
+ });
114
+ });
115
+
116
+ describe('the Claude schedule, published in Pacific time', () => {
117
+ test('in July the window is 05:00-11:00 PDT, which is 12:00-18:00 UTC', () => {
118
+ assert.equal(localClock(utc('2026-07-15T12:00:00Z'), 'America/Los_Angeles'), '05:00', 'PDT start');
119
+ assert.equal(peakStateFor(CLAUDE, utc('2026-07-15T12:00:00Z')).kind, 'peak', '12:00 UTC starts it');
120
+ assert.equal(peakStateFor(CLAUDE, utc('2026-07-15T12:30:00Z')).kind, 'peak');
121
+ assert.equal(peakStateFor(CLAUDE, utc('2026-07-15T17:59:59.999Z')).kind, 'peak');
122
+ assert.equal(peakStateFor(CLAUDE, utc('2026-07-15T11:59:59.999Z')).kind, 'off-peak', 'a hair early');
123
+ assert.equal(peakStateFor(CLAUDE, utc('2026-07-15T18:00:00Z')).kind, 'off-peak', '18:00 UTC ends it');
124
+ });
125
+
126
+ test('in January the same window is 05:00-11:00 PST, which is 13:00-19:00 UTC', () => {
127
+ assert.equal(localClock(utc('2026-01-14T13:00:00Z'), 'America/Los_Angeles'), '05:00', 'PST start');
128
+ assert.equal(peakStateFor(CLAUDE, utc('2026-01-14T13:00:00Z')).kind, 'peak', '13:00 UTC starts it');
129
+ assert.equal(peakStateFor(CLAUDE, utc('2026-01-14T18:59:59.999Z')).kind, 'peak');
130
+ assert.equal(peakStateFor(CLAUDE, utc('2026-01-14T12:59:59.999Z')).kind, 'off-peak', 'a hair early');
131
+ assert.equal(peakStateFor(CLAUDE, utc('2026-01-14T19:00:00Z')).kind, 'off-peak', '19:00 UTC ends it');
132
+ });
133
+
134
+ test('the wall clock is what is fixed: the UTC edges move an hour with the offset', () => {
135
+ // The July and January windows start at the same local clock and an hour apart in UTC. That
136
+ // hour is the entire reason the engine carries a zone instead of adding another UTC pair.
137
+ const summerStart = peakStateFor(CLAUDE, utc('2026-07-15T12:00:00Z')).until;
138
+ const winterStart = peakStateFor(CLAUDE, utc('2026-01-14T13:00:00Z')).until;
139
+ assert.equal(summerStart, utc('2026-07-15T18:00:00Z'));
140
+ assert.equal(winterStart, utc('2026-01-14T19:00:00Z'));
141
+ assert.equal(utc('2026-01-14T19:00:00Z') - utc('2026-01-14T13:00:00Z'), 6 * 3_600_000, 'same length');
142
+ assert.equal(utc('2026-07-15T18:00:00Z') - utc('2026-07-15T12:00:00Z'), 6 * 3_600_000, 'same length');
143
+ });
144
+
145
+ test('weekends are off-peak for Claude too, under both offsets', () => {
146
+ for (const iso of [
147
+ '2026-07-18T12:00:00Z', // Saturday 05:00 PDT: the window's own start, on a Saturday
148
+ '2026-07-18T16:00:00Z',
149
+ '2026-07-19T17:00:00Z', // Sunday 10:00 PDT
150
+ '2026-01-17T13:00:00Z', // Saturday 05:00 PST
151
+ '2026-01-17T18:30:00Z',
152
+ '2026-01-18T15:00:00Z',
153
+ ]) {
154
+ assert.equal(peakStateFor(CLAUDE, utc(iso)).kind, 'off-peak', iso);
155
+ }
156
+ });
157
+
158
+ test('the boundary crosses the Pacific weekend, not the UTC one', () => {
159
+ // Friday 11:00 PDT: the window is over and the next one is Monday 05:00 PDT = 12:00 UTC.
160
+ assert.deepEqual(peakStateFor(CLAUDE, utc('2026-07-17T18:00:00Z')), {
161
+ kind: 'off-peak',
162
+ until: utc('2026-07-20T12:00:00Z'),
163
+ });
164
+ // Sunday evening in Pacific time is off-peak until that same Monday start.
165
+ assert.deepEqual(peakStateFor(CLAUDE, utc('2026-07-19T23:00:00Z')), {
166
+ kind: 'off-peak',
167
+ until: utc('2026-07-20T12:00:00Z'),
168
+ });
169
+ // Friday's own window still ends on Friday.
170
+ assert.deepEqual(peakStateFor(CLAUDE, utc('2026-07-17T16:00:00Z')), {
171
+ kind: 'peak',
172
+ until: utc('2026-07-17T18:00:00Z'),
173
+ });
174
+ });
175
+
176
+ test('the boundary follows the DST switch, so the same wall clock answers a different instant', () => {
177
+ // 2026-03-08 is the PST->PDT switch. Friday 03-06 11:00 PST is 19:00 UTC; the next window is
178
+ // Monday 03-09 05:00 PDT, which is 12:00 UTC — the same 05:00 wall clock, one hour earlier.
179
+ assert.deepEqual(peakStateFor(CLAUDE, utc('2026-03-06T19:00:00Z')), {
180
+ kind: 'off-peak',
181
+ until: utc('2026-03-09T12:00:00Z'),
182
+ });
183
+ assert.equal(localClock(utc('2026-03-06T19:00:00Z'), 'America/Los_Angeles'), '11:00', 'still PST');
184
+ assert.equal(localClock(utc('2026-03-09T12:00:00Z'), 'America/Los_Angeles'), '05:00', 'now PDT');
185
+ // The window before the switch ends an hour later in UTC than the one after it.
186
+ assert.equal(peakStateFor(CLAUDE, utc('2026-03-05T14:00:00Z')).until, utc('2026-03-05T19:00:00Z'));
187
+ assert.equal(peakStateFor(CLAUDE, utc('2026-03-10T13:00:00Z')).until, utc('2026-03-10T18:00:00Z'));
188
+ });
189
+
190
+ test('the two providers keep their own windows, so both can be told apart at one instant', () => {
191
+ const claudeOnly = utc('2026-07-15T12:00:00Z');
192
+ assert.equal(peakStateFor(CLAUDE, claudeOnly).kind, 'peak');
193
+ assert.equal(peakStateFor(DEEPSEEK, claudeOnly).kind, 'off-peak');
194
+
195
+ const deepseekOnly = utc('2026-09-23T02:00:00Z');
196
+ assert.equal(peakStateFor(DEEPSEEK, deepseekOnly).kind, 'peak');
197
+ assert.equal(peakStateFor(CLAUDE, deepseekOnly).kind, 'off-peak');
198
+ });
199
+ });
200
+
201
+ describe('the transition search', () => {
202
+ test('nextTransitionFor is the same boundary the state reports, for both providers', () => {
203
+ for (const [provider, iso] of [
204
+ [DEEPSEEK, '2026-09-23T00:30:00Z'],
205
+ [DEEPSEEK, '2026-09-23T03:40:00Z'],
206
+ [DEEPSEEK, '2026-09-23T04:30:00Z'],
207
+ [DEEPSEEK, '2026-09-25T10:30:00Z'],
208
+ [DEEPSEEK, '2026-09-27T23:00:00Z'],
209
+ [CLAUDE, '2026-07-15T11:30:00Z'],
210
+ [CLAUDE, '2026-07-15T12:00:00Z'],
211
+ [CLAUDE, '2026-07-17T18:00:00Z'],
212
+ [CLAUDE, '2026-07-19T23:00:00Z'],
213
+ [CLAUDE, '2026-01-14T13:00:00Z'],
214
+ [CLAUDE, '2026-03-06T19:00:00Z'],
215
+ ]) {
216
+ assert.equal(nextTransitionFor(provider, utc(iso)), peakStateFor(provider, utc(iso)).until, iso);
217
+ }
218
+ });
219
+
220
+ test('a weekend candidate is not a transition: the state across it is unchanged', () => {
221
+ // Saturday's own 05:00 Pacific edge is a candidate, but the predicate is off-peak on both
222
+ // sides of it, so the Friday boundary must survive it.
223
+ const fridayEvening = utc('2026-07-17T18:00:00Z');
224
+ const saturdayEdge = utc('2026-07-18T12:00:00Z');
225
+ assert.equal(peakStateFor(CLAUDE, saturdayEdge - 1).kind, 'off-peak');
226
+ assert.equal(peakStateFor(CLAUDE, saturdayEdge).kind, 'off-peak');
227
+ assert.equal(nextTransitionFor(CLAUDE, fridayEvening), utc('2026-07-20T12:00:00Z'));
228
+
229
+ const deepseekSaturday = utc('2026-09-26T01:00:00Z');
230
+ assert.deepEqual(peakStateFor(DEEPSEEK, deepseekSaturday), {
231
+ kind: 'off-peak',
232
+ until: utc('2026-09-28T01:00:00Z'),
233
+ });
234
+ });
235
+
236
+ test('a boundary instant belongs to the NEXT state, half-open', () => {
237
+ for (const [provider, boundary, before, after] of [
238
+ [DEEPSEEK, '2026-09-23T04:00:00Z', 'peak', 'off-peak'],
239
+ [DEEPSEEK, '2026-09-23T06:00:00Z', 'off-peak', 'peak'],
240
+ [CLAUDE, '2026-07-15T12:00:00Z', 'off-peak', 'peak'],
241
+ [CLAUDE, '2026-07-15T18:00:00Z', 'peak', 'off-peak'],
242
+ [CLAUDE, '2026-01-14T13:00:00Z', 'off-peak', 'peak'],
243
+ [CLAUDE, '2026-01-14T19:00:00Z', 'peak', 'off-peak'],
244
+ ]) {
245
+ assert.equal(peakStateFor(provider, utc(boundary) - 1).kind, before, `${boundary} - 1ms`);
246
+ assert.equal(peakStateFor(provider, utc(boundary)).kind, after, boundary);
247
+ }
248
+ });
249
+ });
250
+
251
+ describe('the earliest boundary of several providers', () => {
252
+ test('earliestUntil answers the nearest boundary among the states that are peak', () => {
253
+ const early = utc('2026-09-23T04:00:00Z');
254
+ const late = utc('2026-09-23T06:00:00Z');
255
+ assert.equal(
256
+ earliestUntil([
257
+ { kind: 'peak', until: late },
258
+ { kind: 'off-peak', until: early - 3_600_000 },
259
+ { kind: 'peak', until: early },
260
+ ]),
261
+ early,
262
+ 'the off-peak state is not a peak boundary, even when it is nearer',
263
+ );
264
+ assert.equal(earliestUntil([{ kind: 'peak', until: late }]), late);
265
+ });
266
+
267
+ test('earliestUntil is Infinity when nothing is peaking', () => {
268
+ assert.equal(earliestUntil([]), Number.POSITIVE_INFINITY);
269
+ assert.equal(
270
+ earliestUntil([
271
+ { kind: 'off-peak', until: utc('2026-09-23T06:00:00Z') },
272
+ { kind: 'off-peak', until: utc('2026-09-23T01:00:00Z') },
273
+ ]),
274
+ Number.POSITIVE_INFINITY,
275
+ );
276
+ assert.equal(earliestUntil(undefined), Number.POSITIVE_INFINITY);
277
+ });
278
+
279
+ test('two providers peaking at once give one timer at the nearer boundary', () => {
280
+ const deepseek = peakStateFor(DEEPSEEK, utc('2026-09-23T03:40:00Z'));
281
+ const claude = peakStateFor(CLAUDE, utc('2026-07-15T12:30:00Z'));
282
+ assert.deepEqual(
283
+ [deepseek.kind, claude.kind],
284
+ ['peak', 'peak'],
285
+ 'the fixtures really are two peak states',
286
+ );
287
+ // Both rows are up, so one timer waits for whichever window closes first — whatever order the
288
+ // states arrive in.
289
+ assert.equal(earliestUntil([deepseek, claude]), utc('2026-07-15T18:00:00Z'));
290
+ assert.equal(earliestUntil([claude, deepseek]), utc('2026-07-15T18:00:00Z'));
291
+ });
292
+ });
293
+
294
+ describe('a provider the registry does not declare', () => {
295
+ test('a third window needs no change to the engine: it is read from the entry', () => {
296
+ // Tokyo 09:00-17:00 JST on weekdays. 2026-09-23 is a Wednesday.
297
+ assert.equal(peakStateFor(TOKYO, utc('2026-09-23T00:00:00Z')).kind, 'peak', '09:00 JST');
298
+ assert.equal(peakStateFor(TOKYO, utc('2026-09-22T23:59:59.999Z')).kind, 'off-peak');
299
+ assert.equal(peakStateFor(TOKYO, utc('2026-09-23T07:59:59.999Z')).kind, 'peak');
300
+ assert.deepEqual(peakStateFor(TOKYO, utc('2026-09-23T08:00:00Z')), {
301
+ kind: 'off-peak',
302
+ until: utc('2026-09-24T00:00:00Z'),
303
+ });
304
+ // Saturday 10:00 JST is a weekend in the provider's own zone.
305
+ assert.equal(peakStateFor(TOKYO, utc('2026-09-26T01:00:00Z')).kind, 'off-peak');
306
+ // Sunday 23:00 JST is off-peak until Monday 09:00 JST.
307
+ assert.equal(peakStateFor(TOKYO, utc('2026-09-27T14:00:00Z')).until, utc('2026-09-28T00:00:00Z'));
308
+ });
309
+
310
+ test('weekdaysOnly is honoured: a provider without it peaks on a weekend too', () => {
311
+ // UTC 00:00-12:00 every day.
312
+ assert.equal(peakStateFor(ALWAYS, utc('2026-09-26T06:00:00Z')).kind, 'peak', 'Saturday');
313
+ assert.equal(peakStateFor(ALWAYS, utc('2026-09-27T06:00:00Z')).kind, 'peak', 'Sunday');
314
+ assert.equal(peakStateFor(ALWAYS, utc('2026-09-26T12:00:00Z')).kind, 'off-peak');
315
+ assert.deepEqual(peakStateFor(ALWAYS, utc('2026-09-26T12:30:00Z')), {
316
+ kind: 'off-peak',
317
+ until: utc('2026-09-27T00:00:00Z'),
318
+ });
319
+ });
320
+ });
321
+
322
+ describe('an instant or a provider that cannot be read', () => {
323
+ test('an instant that cannot be read is off-peak with no boundary', () => {
324
+ assert.deepEqual(peakStateFor(DEEPSEEK, Number.NaN), { kind: 'off-peak', until: 0 });
325
+ assert.deepEqual(peakStateFor(DEEPSEEK, Number.POSITIVE_INFINITY), { kind: 'off-peak', until: 0 });
326
+ assert.deepEqual(peakStateFor(CLAUDE, Number.NaN), { kind: 'off-peak', until: 0 });
327
+ });
328
+
329
+ test('a provider that is not a registry entry is off-peak with no boundary, never a throw', () => {
330
+ for (const bad of [
331
+ undefined,
332
+ null,
333
+ 'deepseek',
334
+ {},
335
+ { zone: 'UTC' },
336
+ { windows: [[1, 4]], zone: 'not a zone' },
337
+ { windows: [], zone: '' },
338
+ ]) {
339
+ assert.deepEqual(peakStateFor(bad, utc('2026-09-23T02:00:00Z')), { kind: 'off-peak', until: 0 }, String(bad));
340
+ assert.equal(nextTransitionFor(bad, utc('2026-09-23T02:00:00Z')), 0, String(bad));
341
+ }
342
+ });
343
+ });
344
+
345
+ describe('the countdown', () => {
346
+ test('countdowns are compact, hour-based and never round down to zero', () => {
347
+ assert.equal(formatCountdown(0), 'now');
348
+ assert.equal(formatCountdown(-5_000), 'now');
349
+ assert.equal(formatCountdown(30_000), '1m');
350
+ assert.equal(formatCountdown(20 * 60_000), '20m');
351
+ assert.equal(formatCountdown(59 * 60_000 + 30_000), '1hr');
352
+ assert.equal(formatCountdown(80 * 60_000), '1hr 20m');
353
+ assert.equal(formatCountdown(3 * 3_600_000 + 20 * 60_000), '3hr 20m');
354
+ assert.equal(formatCountdown(5 * 3_600_000), '5hr');
355
+ assert.equal(formatCountdown(22 * 3_600_000), '22hr');
356
+ assert.equal(formatCountdown(2 * 86_400_000 + 3 * 3_600_000), '2d 3hr');
357
+ assert.equal(formatCountdown(86_400_000 + 19 * 3_600_000), '1d 19hr');
358
+ });
359
+
360
+ test('an unreadable remaining time is "now"', () => {
361
+ assert.equal(formatCountdown(Number.NaN), 'now');
362
+ assert.equal(formatCountdown(Number.POSITIVE_INFINITY), 'now');
363
+ });
364
+ });
365
+
366
+ describe('the state label', () => {
367
+ test('the state label names the schedule and the time left in it, with no word between', () => {
368
+ const duringPeak = utc('2026-09-23T03:40:00Z');
369
+ assert.equal(formatPeakState(peakStateFor(DEEPSEEK, duringPeak), duringPeak), 'peak 20m');
370
+ const offPeak = utc('2026-09-23T04:30:00Z');
371
+ assert.equal(formatPeakState(peakStateFor(DEEPSEEK, offPeak), offPeak), 'off-peak 1hr 30m');
372
+ // Claude's window reads the same way: the label is the schedule, never the provider.
373
+ const claudePeak = utc('2026-07-15T13:00:00Z');
374
+ assert.equal(formatPeakState(peakStateFor(CLAUDE, claudePeak), claudePeak), 'peak 5hr');
375
+ // The asked-for literals, from states the schedule itself can reach.
376
+ assert.equal(
377
+ formatPeakState({ kind: 'off-peak', until: offPeak + 3 * 3_600_000 + 20 * 60_000 }, offPeak),
378
+ 'off-peak 3hr 20m',
379
+ );
380
+ assert.equal(
381
+ formatPeakState({ kind: 'off-peak', until: offPeak + 86_400_000 + 19 * 3_600_000 }, offPeak),
382
+ 'off-peak 1d 19hr',
383
+ );
384
+ assert.equal(formatPeakState({ kind: 'peak', until: duringPeak + 5 * 3_600_000 }, duringPeak), 'peak 5hr');
385
+ for (const label of ['peak 20m', 'off-peak 1hr 30m', 'off-peak 3hr 20m', 'off-peak 1d 19hr', 'peak 5hr']) {
386
+ assert.equal(label.includes('change'), false, label);
387
+ assert.equal(label.includes(' \u00b7 '), false, label);
388
+ }
389
+ });
390
+ });
391
+
392
+ describe('the boundary clock', () => {
393
+ test('the boundary clock is a 24h local time, weekday-prefixed across a date, and "now" when unknown', () => {
394
+ const duringPeak = utc('2026-09-23T03:40:00Z');
395
+ // Same local date: just the clock.
396
+ assert.equal(formatBoundaryClock(utc('2026-09-23T04:00:00Z'), duringPeak, 'UTC'), '04:00');
397
+ // A different local date carries the short en-US weekday.
398
+ const friday = utc('2026-09-25T10:30:00Z');
399
+ assert.equal(formatBoundaryClock(utc('2026-09-28T01:00:00Z'), friday, 'UTC'), 'Mon 01:00');
400
+ // Without a boundary there is nothing to show.
401
+ assert.equal(formatBoundaryClock(Number.NaN, duringPeak, 'UTC'), 'now');
402
+ assert.equal(formatBoundaryClock(Number.POSITIVE_INFINITY, duringPeak, 'UTC'), 'now');
403
+ assert.equal(formatBoundaryClock(utc('2026-09-23T04:00:00Z'), Number.NaN, 'UTC'), 'now');
404
+ });
405
+
406
+ test('the boundary clock is rendered in whatever zone the caller names', () => {
407
+ const claudePeak = utc('2026-07-15T12:30:00Z');
408
+ const until = peakStateFor(CLAUDE, claudePeak).until;
409
+ assert.equal(formatBoundaryClock(until, claudePeak, 'UTC'), '18:00');
410
+ assert.equal(formatBoundaryClock(until, claudePeak, 'America/Los_Angeles'), '11:00');
411
+ assert.equal(formatBoundaryClock(until, claudePeak, 'Asia/Tokyo'), 'Thu 03:00');
412
+ });
413
+ });
414
+
415
+ /** The wall clock one instant reads in one zone, as `HH:MM`: the assertion the DST cases lean on. */
416
+ function localClock(ts, timeZone) {
417
+ return new Intl.DateTimeFormat('en-US', {
418
+ hour: '2-digit',
419
+ minute: '2-digit',
420
+ hour12: false,
421
+ timeZone,
422
+ }).format(new Date(ts));
423
+ }
@@ -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
+ });