pi-odd-worktree 3.14.0 → 3.15.1

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
+ }
@@ -169,8 +169,13 @@ class StubMarkdown {
169
169
  if (this.text === 'THROW') throw new Error('the renderer blew up');
170
170
  if (this.text === 'NOT_ROWS') return 'not rows';
171
171
  if (this.text === 'EMPTY_BODY') return [];
172
+ // Pi's Markdown hands the host's composed `transform` the WHOLE text and the content width
173
+ // before it parses anything (`markdown.js`: `this.options.transform?.(this.text, contentWidth)`),
174
+ // so a stub that ignored it could never show what an extension transformer does to a prompt.
175
+ const contentWidth = Math.max(1, width - this.paddingX * 2);
176
+ const transformed = this.options?.transform?.(String(this.text), contentWidth) ?? this.text;
172
177
  // Rows the renderer already painted: the card must PLACE them, never repaint them.
173
- const lines = String(this.text).split('\n').map((line) => `\x1b[${this.code}m${line}\x1b[39m`);
178
+ const lines = String(transformed).split('\n').map((line) => `\x1b[${this.code}m${line}\x1b[39m`);
174
179
  // Pi's Markdown pads its content with `paddingY` blank rows above and below
175
180
  // (`emptyLines.concat(contentLines, emptyLines)`), and the stub models that: a clone that
176
181
  // keeps the host's padding costs the terminal two rows per prompt.
@@ -525,6 +530,72 @@ test('the body rows are the child\'s own Markdown rebuilt with no background and
525
530
  assert.equal(typeof clone.defaultTextStyle.color, 'function', 'the text colour is the body\'s own paint');
526
531
  });
527
532
 
533
+ // ---- prompt-card-mark: the ❯ an extension transformer prepends - ` ❯ ` in pi-pretty ----
534
+
535
+ /**
536
+ * pi-pretty's own markdown transformer, as the host composes it: the user's markdown comes back
537
+ * prefixed with ` ❯ ` and an empty prompt is left alone (`dist/index.js`: `" ❯ " + markdown`).
538
+ */
539
+ const prettyTransform = (markdown) => (markdown.trim() === '' ? markdown : ` ❯ ${markdown}`);
540
+
541
+ /** A prompt whose Markdown carries the host's composed `transform`, exactly as Pi wires it. */
542
+ function decoratedPrompt(text = PROMPT) {
543
+ return new UserMessageComponent(text, {
544
+ markdownOptions: { preserveOrderedListMarkers: true, preserveBackslashEscapes: true, transform: prettyTransform },
545
+ });
546
+ }
547
+
548
+ test('the ❯ an extension transformer prepends is dropped, and the body starts at the frame padding', () => {
549
+ publishBridge(ACTIVITY_ANSWER);
550
+ install();
551
+ const rows = decoratedPrompt().render(60);
552
+ assert.equal(rows.some((row) => unpaint(row).includes('❯')), false, 'no row carries the decoration');
553
+ const body = rows.filter((row) => row.includes('explain the card'));
554
+ assert.equal(body.length, 1, 'the first line is one body row');
555
+ assert.ok(unpaint(body[0]).startsWith('│ explain the card'), `the message starts at the frame padding: ${unpaint(body[0])}`);
556
+ assert.ok(rows.some((row) => unpaint(row).includes('and keep `this` visible')), 'every line of the prompt is still drawn');
557
+ for (const row of rows) assert.equal(chars(row).length, 60, `"${unpaint(row)}" is 60 wide`);
558
+ });
559
+
560
+ test('a prompt with no transformer is drawn byte for byte as before', () => {
561
+ publishBridge(ACTIVITY_ANSWER);
562
+ install();
563
+ const component = promptComponent();
564
+ component.timestamp = '2026-01-02T09:07:00Z';
565
+ const clock = localClock(new Date(component.timestamp));
566
+ // The rows this card drew BEFORE the decoration was removed, pinned: no transformer, no
567
+ // decoration, so the body, the frame and the prompt-zone markers are untouched to the byte.
568
+ const pinned = [
569
+ OSC133_ZONE_START + `╭─ ✿ You · ${clock} ${'─'.repeat(42)}╮`,
570
+ `│ \x1b[${MARKDOWN_CODE}mexplain the card\x1b[39m${' '.repeat(40)} │`,
571
+ `│ \x1b[${MARKDOWN_CODE}mand keep \`this\` visible\x1b[39m${' '.repeat(33)} │`,
572
+ OSC133_ZONE_CLOSE + `╰${'─'.repeat(58)}╯`,
573
+ ];
574
+ assert.deepEqual(component.render(60), pinned);
575
+ });
576
+
577
+ test('a prompt that starts with ❯ keeps exactly one ❯: only the decoration is consumed', () => {
578
+ publishBridge(ACTIVITY_ANSWER);
579
+ install();
580
+ const rows = decoratedPrompt('❯ my own mark\nsecond line').render(60).map(unpaint);
581
+ const body = rows.filter((row) => row.includes('my own mark'));
582
+ assert.equal(body.length, 1, 'the user\'s line is one body row');
583
+ assert.ok(body[0].startsWith('│ ❯ my own mark'), `the user\'s own mark stays at the frame padding: ${body[0]}`);
584
+ assert.equal(body[0].split('❯').length - 1, 1, 'exactly one ❯: the decoration, never the user\'s own');
585
+ });
586
+
587
+ test('the body re-wraps at the full inner width: the decoration costs no columns', () => {
588
+ publishBridge(ACTIVITY_ANSWER);
589
+ install();
590
+ // Exactly the inner width of a 60-column card (`cardInnerWidth(60)`): one body row undecorated,
591
+ // and one row plus a mark-only row when the three decoration columns still eat the line.
592
+ const line = 'x'.repeat(56);
593
+ const rows = decoratedPrompt(line).render(60);
594
+ assert.equal(rows.length, 3, 'the frame and ONE body row: the 56 columns fit');
595
+ assert.ok(rows[1].includes(line), 'the whole line is on its one row, with no three-column slack');
596
+ assert.equal(chars(rows[1]).length, 60, 'and the row still fills the frame');
597
+ });
598
+
528
599
  test('a steady frame rebuilds nothing, and a width change or a style flip redraws', () => {
529
600
  publishBridge(ACTIVITY_ANSWER);
530
601
  const counting = countingMetrics();