counterparts 0.3.13 → 0.3.15

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.
Files changed (106) hide show
  1. package/CHANGELOG.md +264 -0
  2. package/docs/QUICKSTART.md +22 -0
  3. package/docs/README.md +1 -0
  4. package/package.json +1 -1
  5. package/src/adapters/claude-code/CONTRACT.md +48 -0
  6. package/src/adapters/claude-code/NOTES.md +148 -0
  7. package/src/adapters/claude-code/bin/hook.mjs +4 -2
  8. package/src/adapters/claude-code/bin/hook.ts +61 -7
  9. package/src/adapters/claude-code/bin/nightly.ts +5 -1
  10. package/src/adapters/claude-code/bin/runner.ts +11 -3
  11. package/src/adapters/claude-code/claim.ts +319 -0
  12. package/src/adapters/claude-code/doctor.ts +170 -16
  13. package/src/adapters/claude-code/embed-client.ts +3 -0
  14. package/src/adapters/cli/CONTRACT.md +5 -0
  15. package/src/adapters/cli/NOTES.md +25 -0
  16. package/src/adapters/cli/bin/counterparts.mjs +4 -2
  17. package/src/adapters/cli/bin/counterparts.ts +49 -3
  18. package/src/adapters/cli/commands.ts +169 -32
  19. package/src/adapters/cli/desktop.ts +112 -7
  20. package/src/adapters/cli/help.ts +10 -0
  21. package/src/adapters/cli/index.ts +1 -0
  22. package/src/adapters/cli/install.ts +94 -14
  23. package/src/adapters/cli/self-page.ts +5 -8
  24. package/src/adapters/cli/settle.ts +39 -2
  25. package/src/adapters/cli/wire.ts +18 -1
  26. package/src/adapters/config.ts +29 -1
  27. package/src/adapters/dashboard/NOTES.md +33 -0
  28. package/src/adapters/dashboard/bin/dashboard.mjs +4 -2
  29. package/src/adapters/dashboard/bin/dashboard.ts +11 -3
  30. package/src/adapters/dashboard/index.ts +14 -0
  31. package/src/adapters/dashboard/registries.ts +3 -0
  32. package/src/adapters/dashboard/source.ts +0 -8
  33. package/src/adapters/dashboard/web/fired.ts +8 -0
  34. package/src/adapters/dashboard/web/flow.ts +1 -0
  35. package/src/adapters/dashboard/web/lanes.ts +1 -0
  36. package/src/adapters/dashboard/web/narrate.ts +11 -0
  37. package/src/adapters/dashboard/web/pages/flow/sections/fired.js +1 -1
  38. package/src/adapters/dashboard/web/pages/health/sections/cycle.js +7 -10
  39. package/src/adapters/dashboard/web/pages/memories/memories.css +3 -0
  40. package/src/adapters/dashboard/web/pages/memories/row.js +5 -2
  41. package/src/adapters/dashboard/web/pages/memories/sections/hold.js +2 -1
  42. package/src/adapters/dashboard/web/pages/memories/sections/search.js +19 -3
  43. package/src/adapters/dashboard/web/server.ts +16 -4
  44. package/src/adapters/dashboard/web/shared/dates.js +6 -16
  45. package/src/adapters/dashboard/web/shared/memory-modal.js +21 -8
  46. package/src/adapters/dashboard/web/views/health.ts +19 -5
  47. package/src/adapters/dashboard/web/views/mechanism-panel.ts +8 -5
  48. package/src/adapters/dashboard/web/views/memories.ts +26 -7
  49. package/src/adapters/dashboard/web/views/memory-words.ts +16 -0
  50. package/src/adapters/dashboard/web/views/memory.ts +51 -2
  51. package/src/adapters/dashboard/web/views/mind.ts +38 -6
  52. package/src/adapters/empty-bunfig.toml +5 -0
  53. package/src/adapters/fired.ts +16 -2
  54. package/src/adapters/host-wiring.ts +112 -17
  55. package/src/adapters/log/index.ts +2 -2
  56. package/src/adapters/mcp/CONTRACT.md +17 -2
  57. package/src/adapters/mcp/INTERFACE-GAPS.md +15 -1
  58. package/src/adapters/mcp/NOTES.md +269 -0
  59. package/src/adapters/mcp/bin/serve.mjs +4 -2
  60. package/src/adapters/mcp/bin/serve.ts +17 -6
  61. package/src/adapters/mcp/facts.ts +72 -4
  62. package/src/adapters/mcp/meaning.ts +218 -15
  63. package/src/adapters/mcp/server.ts +21 -6
  64. package/src/adapters/mcp/tools.ts +86 -61
  65. package/src/adapters/plugin-run.sh +413 -34
  66. package/src/adapters/plugin.ts +141 -17
  67. package/src/adapters/runtime.ts +192 -17
  68. package/src/adapters/sessions.ts +2 -2
  69. package/src/core/associate/NOTES.md +10 -0
  70. package/src/core/bridge.ts +1 -1
  71. package/src/core/briefing.ts +20 -3
  72. package/src/core/contradictions.ts +56 -20
  73. package/src/core/counterpart.ts +72 -23
  74. package/src/core/dream/CONTRACT.md +6 -14
  75. package/src/core/dream/INTERFACE-GAPS.md +3 -2
  76. package/src/core/dream/NOTES.md +10 -0
  77. package/src/core/dream/index.ts +11 -53
  78. package/src/core/dream/reflect.ts +11 -2
  79. package/src/core/embed/static.ts +5 -1
  80. package/src/core/encode/INTERFACE-GAPS.md +2 -1
  81. package/src/core/encode/NOTES.md +12 -0
  82. package/src/core/encode/chunk.ts +6 -30
  83. package/src/core/encode/index.ts +0 -1
  84. package/src/core/encode/types.ts +1 -13
  85. package/src/core/handoff/INTERFACE-GAPS.md +1 -1
  86. package/src/core/handoff/index.ts +2 -1
  87. package/src/core/handoff/last-here.ts +18 -2
  88. package/src/core/physics/CONTRACT.md +6 -0
  89. package/src/core/physics/NOTES.md +33 -2
  90. package/src/core/physics/index.ts +22 -7
  91. package/src/core/prospective/CONTRACT.md +3 -1
  92. package/src/core/prospective/NOTES.md +15 -0
  93. package/src/core/recall/activate.ts +1 -1
  94. package/src/core/recall/time-ask.ts +173 -19
  95. package/src/core/self/CONTRACT.md +56 -19
  96. package/src/core/self/NOTES.md +172 -0
  97. package/src/core/self/behind.ts +6 -3
  98. package/src/core/self/briefing.ts +384 -28
  99. package/src/core/self/index.ts +105 -44
  100. package/src/core/self/page.ts +12 -85
  101. package/src/core/self/tunables.ts +16 -19
  102. package/src/core/self/writer.ts +31 -4
  103. package/src/core/sleep/NOTES.md +32 -0
  104. package/src/core/store/NOTES.md +21 -2
  105. package/src/core/store/index.ts +2 -1
  106. package/src/core/time.ts +15 -0
@@ -18,7 +18,9 @@
18
18
  * There is no default in this module to fall back to.
19
19
  * - **The trim order is declared and tested** (`TRIM_ORDER`), and trimming eats
20
20
  * from the END of a lane, whose ordering is itself policy: *truncation must
21
- * never be iteration luck* (§1 G3–G4).
21
+ * never be iteration luck* (§1 G3–G4). One declared exception, after the
22
+ * loop: "Still open" keeps its first item before Arriving keeps its second
23
+ * line (`keepFirstOpen`, 2026-10-09).
22
24
  * - **The identity SHARE decides how the total is split when lanes compete.**
23
25
  * The budget still governs the composed total (§5 G2); the share is not a
24
26
  * lane budget but a rule about who wins the contested bytes, and it exists
@@ -29,7 +31,9 @@
29
31
  * remainder, identity takes the leftover back — a ceiling, not a cap.
30
32
  * - **Truncation is never mid-statement.** A statement is admitted whole or not
31
33
  * at all — there is no `clip()` here on purpose. A half-sentence about who
32
- * someone is, is not a smaller identity; it is a corrupted one.
34
+ * someone is, is not a smaller identity; it is a corrupted one. The same is
35
+ * true of the self page (2026-10-09): it prints whole, or one line says it
36
+ * did not fit and where to read it (`PAGE_LIMIT_BYTES`).
33
37
  * - **The header AND the tail sentinel each state the bundle's own true counts
34
38
  * and bytes**, so a truncated injection is detectable from a truncation
35
39
  * preview alone — from either end (§1 G2). Because both lines state a number
@@ -41,6 +45,8 @@
41
45
  import type { LaneName, Ranked } from "./identity.js";
42
46
  import { byteLength } from "./identity.js";
43
47
  import type { SelfTunables } from "./tunables.js";
48
+ import { addDays, isDay } from "../time.js";
49
+ import { HANDOFF_RESERVE_MAX_BYTES, HANDOFF_RESERVE_MIN_BUDGET_MULTIPLE } from "../handoff/index.js";
44
50
 
45
51
  /**
46
52
  * Composed order — behavioral-spec §1's, minus the two riders the Rulings drop
@@ -142,9 +148,41 @@ const MORE_ORDER: readonly LaneName[] = [
142
148
 
143
149
  /** `(3 more still open; recall ids: mem_…, mem_…, mem_…)` */
144
150
  export function moreLine(lane: LaneName, ids: readonly string[]): string {
151
+ return `(${String(ids.length)} more ${MORE_NOUN[lane]}; ${recallIds(ids)})`;
152
+ }
153
+
154
+ function recallIds(ids: readonly string[]): string {
145
155
  const shown = ids.slice(0, MORE_LINE_IDS);
146
156
  const which = ids.length > shown.length ? `recall ids (the first ${String(shown.length)})` : "recall ids";
147
- return `(${String(ids.length)} more ${MORE_NOUN[lane]}; ${which}: ${shown.join(", ")})`;
157
+ return `${which}: ${shown.join(", ")}`;
158
+ }
159
+
160
+ /**
161
+ * A LANE WITH NOTHING LISTED SAYS SO IN ONE LINE, NOT A HEADING OVER A COUNT
162
+ * (2026-10-09). The owner's wake that morning read "Still open:" and, under
163
+ * it, "(20 more still open; recall ids …)" with no item between — a heading
164
+ * over nothing reads like a broken page, and "more" than none is not a count.
165
+ * When a lane keeps no element and has something to say about what it left
166
+ * out, the heading and the count are one line: `Still open: 20 — no room to
167
+ * list them in this wake; recall ids (the first 5): mem_…`. Furniture, like
168
+ * the "more" line it replaces; the identity lane keeps its heading, which has
169
+ * the page or the day-0 line under it. The dashboard reads the lane off the
170
+ * line's own heading (`collapsedLane`).
171
+ */
172
+ export const COLLAPSED_WORDS = "no room to list them in this wake";
173
+
174
+ export function collapsedLine(lane: LaneName, ids: readonly string[]): string {
175
+ return `${laneHeading(lane)} ${String(ids.length)} — ${COLLAPSED_WORDS}; ${recallIds(ids)}`;
176
+ }
177
+
178
+ /** The lane a collapsed line (`collapsedLine`) speaks for, or null for any other line. */
179
+ export function collapsedLane(line: string): LaneName | null {
180
+ for (const lane of LANE_ORDER) {
181
+ const heading = laneHeading(lane);
182
+ if (!line.startsWith(`${heading} `)) continue;
183
+ if (new RegExp(`^ \\d+ — ${COLLAPSED_WORDS}; recall ids`).test(line.slice(heading.length))) return lane;
184
+ }
185
+ return null;
148
186
  }
149
187
 
150
188
  /**
@@ -208,11 +246,14 @@ export function identityCoreLine(name: string): string {
208
246
  * — and the page carries its own date on its own line instead.
209
247
  */
210
248
  export interface PageBlock {
211
- /** The page as it will be injected: already cut to the cap, marker included. */
249
+ /** The page as it will be injected: the whole page, or — when the room
250
+ * could not hold it whole — the one line that says so (`pageTooLargeLine`).
251
+ * Never part of the page (2026-10-09). */
212
252
  readonly text: string;
213
253
  /** The page's own "last revised" line, or null when the page carries no date. */
214
254
  readonly dateline: string | null;
215
- /** True when the cap cut it — `text` already carries the marker that says so. */
255
+ /** True when the page did not print whole — `text` is then the line that
256
+ * says so, and nothing of the page itself. */
216
257
  readonly truncated: boolean;
217
258
  /** The page's own bytes, whole, whatever was rendered. */
218
259
  readonly wholeBytes: number;
@@ -278,6 +319,12 @@ export interface Resolved {
278
319
  * Only a horizon item that repeats carries one.
279
320
  */
280
321
  readonly every?: string;
322
+ /**
323
+ * True when `due` was already BEHIND the day the wake was composed for
324
+ * (`HorizonItem.past`, 2026-10-09) — a one-off in its grace days — so the
325
+ * line says `(was due …)` rather than reading as still to come.
326
+ */
327
+ readonly duePast?: boolean;
281
328
  /**
282
329
  * True when the encode date is only an UPPER BOUND — the element was known BY
283
330
  * then, not learned then. Set for migrated elements, whose `learned_on` is
@@ -310,13 +357,14 @@ export interface BriefingRequest {
310
357
  */
311
358
  readonly coreName?: string;
312
359
  /**
313
- * THE WRITTEN SELF PAGE, already cut to its cap and dated by the caller
314
- * (`Self.build` → `page.ts#renderPage`). Present means "Who I am" prints this
360
+ * THE WRITTEN SELF PAGE, whole or the line that says it did not fit, dated
361
+ * by the caller (`Self#pageBlock`). Present means "Who I am" prints this
315
362
  * and NOT the rotating list; absent means the list renders as it always has,
316
363
  * or — when `PAGE_EMPTY_SHOWS_LIST` is off — the still-forming line does.
317
364
  *
318
- * It arrives ready because the cap is a byte decision that needs the caller's
319
- * budget and the page's own prose, and this module composes rather than reads.
365
+ * It arrives ready because whether it fits is a byte decision that needs the
366
+ * caller's budget and the page's own prose, and this module composes rather
367
+ * than reads.
320
368
  */
321
369
  readonly page?: PageBlock;
322
370
  /**
@@ -337,6 +385,24 @@ export interface BriefingRequest {
337
385
  * the floor would not fit with it. Absent: no line.
338
386
  */
339
387
  readonly yesterday?: string;
388
+ /**
389
+ * SHORTER FORMS OF THE SAME LINE, widest first (review of #350,
390
+ * 2026-10-09): fewer titles, the rest by count — composed by the caller,
391
+ * which holds the chapters the count is of. Stepped down only to make room
392
+ * for "Still open"'s first item (`keepFirstOpen`). Ignored without
393
+ * `yesterday`.
394
+ */
395
+ readonly yesterdayShorter?: readonly string[];
396
+ /**
397
+ * THE ROOM THE DELIVERY HOLDS FOR "WORK HERE" (review of #350, 2026-10-09),
398
+ * in bytes: the caller's reserve for this directory's work lines, which it
399
+ * took out of `budgetBytes` before composing. Lent to "Still open"'s first
400
+ * item and its count only (`keepFirstOpen`), and before Arriving or the
401
+ * Yesterday line give anything up: the work lines are hints, and the
402
+ * delivery shows fewer of them in what is left. Nothing else composes into
403
+ * it. Absent: nothing to lend.
404
+ */
405
+ readonly lendBytes?: number;
340
406
  }
341
407
 
342
408
  export interface TrimEvent {
@@ -375,8 +441,8 @@ export interface BriefingResult extends Composed {
375
441
  * a host misconfiguration, and the wake never fails the session (§1 G7). */
376
442
  readonly overBudget: boolean;
377
443
  /** The page as it RENDERED — null when no page was handed to this render. The
378
- * bytes are the injected ones (the marker included), so a cut page's cost and
379
- * its true size are both readable. */
444
+ * bytes are the injected ones (the line, when it did not fit), so what the
445
+ * page cost this wake and its true size are both readable. */
380
446
  readonly page: { readonly bytes: number; readonly truncated: boolean; readonly wholeBytes: number } | null;
381
447
  }
382
448
 
@@ -469,6 +535,9 @@ export function flatten(text: string): string {
469
535
  */
470
536
  export const DATE_SEP = " · ";
471
537
  export const DATE_BOUND = "by ";
538
+ /** How an arriving line names its date, ahead of it and once it has passed. */
539
+ export const DUE_WORD = "due";
540
+ export const DUE_PAST_WORD = "was due";
472
541
 
473
542
  export function datePrefix(r: Resolved): string {
474
543
  const learned = (r.learnedOn ?? "").trim();
@@ -481,8 +550,15 @@ export function datePrefix(r: Resolved): string {
481
550
  // due on the day it was learned, since "every May 14" is news there too.
482
551
  const every = (r.every ?? "").trim();
483
552
  const often = every === "" ? "" : `, ${every}`;
484
- if (learned === "") return `due ${due}${often}${DATE_SEP}`;
485
- if (due !== learned || often !== "") return `${r.boundedDate === true ? DATE_BOUND : ""}${learned} (due ${due}${often})${DATE_SEP}`;
553
+ // PAST ITS DATE, IT SAYS SO (2026-10-09): a one-off stays under
554
+ // "Arriving:" for its grace days (`prospective/` GRACE_DAYS), and
555
+ // "(due 2026-10-08)" read on the 9th reads as still to come.
556
+ const word = r.duePast === true ? DUE_PAST_WORD : DUE_WORD;
557
+ // DUE ON THE DAY IT WAS LEARNED: the one date, stated once — as the DUE
558
+ // date (2026-10-09), so the line still says when, and the delivery can
559
+ // put it in the past tense the morning after (`arrivingTense`).
560
+ if (learned === "" || (due === learned && often === "")) return `${word} ${due}${often}${DATE_SEP}`;
561
+ return `${r.boundedDate === true ? DATE_BOUND : ""}${learned} (${word} ${due}${often})${DATE_SEP}`;
486
562
  }
487
563
  if (learned === "") return "";
488
564
  const happened = (r.happenedOn ?? "").trim();
@@ -594,6 +670,12 @@ export function compose(
594
670
  }
595
671
  const tail = more[lane];
596
672
  if (items.length === 0 && tail === undefined) continue;
673
+ // Nothing listed: the tail is the lane's one collapsed line, heading
674
+ // included (`collapsedLine`), never a heading over a count.
675
+ if (items.length === 0 && tail !== undefined) {
676
+ lines.push("", tail);
677
+ continue;
678
+ }
597
679
  lines.push("", laneHeading(lane));
598
680
  for (const item of items) lines.push(elementLine(item, resolve));
599
681
  if (tail !== undefined) lines.push(tail);
@@ -718,6 +800,123 @@ function lostByLane(
718
800
  return out;
719
801
  }
720
802
 
803
+ /**
804
+ * "STILL OPEN" KEEPS ITS FIRST ITEM, AND ITS COUNT, BEFORE ARRIVING KEEPS ITS
805
+ * SECOND LINE OR THE YESTERDAY LINE ITS TITLES (review of #350, 2026-10-09).
806
+ *
807
+ * The owner's wake on 10-09 read "Still open:" over "(20 more still open; …)"
808
+ * and no item. The self page is furniture the trim cannot pop, the Yesterday
809
+ * line is too, and `TRIM_ORDER` takes "Still open" before "Arriving" — so
810
+ * beside a long page the lane gave up its last item while Arriving kept every
811
+ * line. The page is not what gives way: it is the self the wake is for, and it
812
+ * prints whole under its cap (`Self#pageBlock`). What gives way is, in order
813
+ * — Nearby and craft are already gone by the time this runs, since they trim
814
+ * first —
815
+ *
816
+ * 1. "Work here": the room the delivery holds for this directory's work
817
+ * lines (`lendBytes`), which it then fills with fewer of them;
818
+ * 2. Arriving beyond its first line (each a `TrimEvent`, so its own "N more"
819
+ * line can still say so if there is room);
820
+ * 3. the Yesterday line, down its shorter forms (`yesterdayShorter`: fewer
821
+ * titles, the rest by count — the caller's, which holds the chapters).
822
+ *
823
+ * The target is the lane's first item AND its "N more" line: one item under a
824
+ * heading with no count reads as the only thing open. Only after the trim loop
825
+ * has FIT, and all or nothing: when even the last step leaves no room, the
826
+ * render is exactly what the trim loop made, and a lane that lists nothing
827
+ * says so in one line (`collapsedLine`). Returns null then, or when there is
828
+ * nothing to keep.
829
+ *
830
+ * AND THE COUNT BESIDE WHAT THE TRIM KEPT (review of #358, 2026-10-09): when
831
+ * the trim loop left the lane its items but no room for its "N more" line —
832
+ * #350's morning with a 5,904-byte page read one item and no count — the
833
+ * count is paid for out of "Work here" (`lendBytes`), the first rung, and out
834
+ * of nothing else: the items stay as the trim kept them (a smaller budget
835
+ * still keeps a subset), and Arriving and Yesterday, which the trim order
836
+ * already ranks above Still open's later items, give nothing for a count. Not
837
+ * when the count already fits: `withMoreLines` adds it then, as it adds every
838
+ * lane's.
839
+ */
840
+ function keepFirstOpen(
841
+ arrived: readonly Ranked[],
842
+ arrivedHorizon: number,
843
+ overflow: readonly string[],
844
+ kept: Kept,
845
+ trimmed: readonly TrimEvent[],
846
+ yesterday: string | undefined,
847
+ req: BriefingRequest,
848
+ resolve: Resolve,
849
+ coreName: string | undefined,
850
+ identity: IdentityBlock,
851
+ ): {
852
+ kept: Kept;
853
+ trimmed: TrimEvent[];
854
+ yesterday: string | undefined;
855
+ pinned: Partial<Record<LaneName, string[]>>;
856
+ composed: Composed;
857
+ /** Bytes taken from `lendBytes`: what the composition runs past `budgetBytes`. */
858
+ lent: number;
859
+ } | null {
860
+ const first = arrived[0];
861
+ if (first === undefined) return null;
862
+ // Arriving's FIRST line still outranks it, as the trim order says: when the
863
+ // trim already took that, this budget has no room to rearrange.
864
+ if (kept.threads.length === 0 && arrivedHorizon > 0 && kept.horizon.length === 0) return null;
865
+ const k: Kept = {
866
+ identity: [...kept.identity],
867
+ craft: [...kept.craft],
868
+ // What the lane lists: the items the trim kept, or — when it kept none —
869
+ // its first item.
870
+ threads: kept.threads.length > 0 ? [...kept.threads] : [first],
871
+ hints: [...kept.hints],
872
+ horizon: [...kept.horizon],
873
+ };
874
+ const t = trimmed.filter((e) => !(e.lane === "threads" && k.threads.some((l) => l.id === e.id)));
875
+ // The rest of the lane, as `lostByLane` will list it: the trim's (popped
876
+ // from the end, so reversed back), then what the lane's cap left out.
877
+ const restOf = (): string[] => [...t.filter((e) => e.lane === "threads").map((e) => e.id).reverse(), ...overflow];
878
+ const countLine = (rest: readonly string[]): Partial<Record<LaneName, string>> =>
879
+ rest.length === 0 ? {} : { threads: moreLine("threads", rest) };
880
+ const rest = restOf();
881
+ const lines = countLine(rest);
882
+ const pinned: Partial<Record<LaneName, string[]>> = rest.length === 0 ? {} : { threads: rest };
883
+ const lend = Math.max(0, Math.floor(req.lendBytes ?? 0));
884
+ if (kept.threads.length > 0) {
885
+ // Items the trim kept: only their count is owed here, only when it does
886
+ // not fit as it is, and only out of "Work here" — the items stay as the
887
+ // trim kept them, and Arriving and Yesterday give nothing for a count
888
+ // (the trim order already ranks Arriving above Still open's later items).
889
+ if (rest.length === 0) return null;
890
+ const c = compose(k, req.day, resolve, coreName, identity, lines, yesterday);
891
+ if (c.bytes <= req.budgetBytes || c.bytes > req.budgetBytes + lend) return null;
892
+ return { kept: k, trimmed: t, yesterday, pinned, composed: c, lent: c.bytes - req.budgetBytes };
893
+ }
894
+ const shorter =
895
+ yesterday === undefined ? [] : (req.yesterdayShorter ?? []).filter((s) => s.length > 0 && byteLength(s) < byteLength(yesterday));
896
+ let limit = req.budgetBytes;
897
+ let y = yesterday;
898
+ let rung = 0;
899
+ for (;;) {
900
+ const c = compose(k, req.day, resolve, coreName, identity, lines, y);
901
+ if (c.bytes <= limit) {
902
+ return { kept: k, trimmed: t, yesterday: y, pinned, composed: c, lent: Math.max(0, c.bytes - req.budgetBytes) };
903
+ }
904
+ if (limit < req.budgetBytes + lend) {
905
+ limit = req.budgetBytes + lend;
906
+ continue;
907
+ }
908
+ if (k.horizon.length > 1) {
909
+ const dropped = k.horizon.pop();
910
+ if (dropped !== undefined) t.push({ lane: "horizon", id: dropped.id, strength: dropped.strength });
911
+ continue;
912
+ }
913
+ const next = shorter[rung];
914
+ if (next === undefined) return null;
915
+ y = next;
916
+ rung += 1;
917
+ }
918
+ }
919
+
721
920
  /**
722
921
  * Add each lane's "N more" line, in `MORE_ORDER`, only while the whole
723
922
  * composition still fits the budget. A line that does not fit is left out and
@@ -733,15 +932,26 @@ function withMoreLines(
733
932
  coreName: string | undefined;
734
933
  identity: IdentityBlock;
735
934
  yesterday?: string;
935
+ /** Lines already in `composed`, paid for before the others are offered
936
+ * room (`keepFirstOpen`'s count): kept, never re-tried. */
937
+ pinned?: Partial<Record<LaneName, string[]>>;
736
938
  },
737
939
  ): { composed: Composed; more: Partial<Record<LaneName, number>> } {
738
940
  let current = composed;
739
941
  const lines: Partial<Record<LaneName, string>> = {};
740
942
  const more: Partial<Record<LaneName, number>> = {};
741
- for (const lane of MORE_ORDER) {
742
- const ids = ctx.lost[lane];
943
+ for (const lane of LANE_ORDER) {
944
+ const ids = ctx.pinned?.[lane];
743
945
  if (ids === undefined || ids.length === 0) continue;
744
946
  lines[lane] = moreLine(lane, ids);
947
+ more[lane] = ids.length;
948
+ }
949
+ for (const lane of MORE_ORDER) {
950
+ const ids = ctx.lost[lane];
951
+ if (ids === undefined || ids.length === 0 || lines[lane] !== undefined) continue;
952
+ // A lane that kept nothing gets the one collapsed line instead: "N more"
953
+ // over no item is neither a list nor a count (2026-10-09).
954
+ lines[lane] = lane !== "identity" && ctx.kept[lane].length === 0 ? collapsedLine(lane, ids) : moreLine(lane, ids);
745
955
  const candidate = compose(ctx.kept, ctx.req.day, ctx.resolve, ctx.coreName, ctx.identity, lines, ctx.yesterday);
746
956
  if (candidate.bytes > ctx.req.budgetBytes) {
747
957
  delete lines[lane];
@@ -858,13 +1068,31 @@ export function render(
858
1068
  }
859
1069
  }
860
1070
  if (fits || cut === null) {
1071
+ // "STILL OPEN" KEEPS ITS FIRST ITEM AND ITS COUNT (review of #350,
1072
+ // 2026-10-09), out of the room held for "Work here", Arriving's later
1073
+ // lines and the Yesterday line's titles, never out of the page — or the
1074
+ // render stays as the trim loop left it (`keepFirstOpen`).
1075
+ const first = fits
1076
+ ? keepFirstOpen(lanes.threads, lanes.horizon.length, lanes.overflow?.threads ?? [], kept, trimmed, yesterday, req, resolve, coreName, identity)
1077
+ : null;
1078
+ if (first !== null) {
1079
+ for (const lane of LANE_ORDER) kept[lane] = first.kept[lane];
1080
+ trimmed.splice(0, trimmed.length, ...first.trimmed);
1081
+ }
1082
+ const said = first === null ? yesterday : first.yesterday;
861
1083
  // The leftover clause. Only when NOTHING had to be trimmed: a lane that
862
1084
  // lost an element wanted the room, and handing it to identity instead
863
1085
  // would make the share decide the opposite of what it was set for. When
864
1086
  // the other lanes are all present and the budget is still not spent, the
865
- // held-back identity elements take it back, in rank order, whole.
1087
+ // held-back identity elements take it back, in rank order, whole. Not
1088
+ // beside a kept first item: its count is already in, and the leftover
1089
+ // composes without it.
866
1090
  const offered =
867
- trimmed.length === 0 ? offerLeftover(kept, held, c, req, resolve, coreName, identity, yesterday) : c;
1091
+ first !== null
1092
+ ? first.composed
1093
+ : trimmed.length === 0
1094
+ ? offerLeftover(kept, held, c, req, resolve, coreName, identity, yesterday)
1095
+ : c;
868
1096
  // THE "N MORE" LINES, last: they take only room nothing else wanted.
869
1097
  const told = withMoreLines(offered, {
870
1098
  kept,
@@ -873,12 +1101,18 @@ export function render(
873
1101
  resolve,
874
1102
  coreName,
875
1103
  identity,
876
- ...(yesterday === undefined ? {} : { yesterday }),
1104
+ ...(said === undefined ? {} : { yesterday: said }),
1105
+ ...(first === null ? {} : { pinned: first.pinned }),
877
1106
  });
878
1107
  const composed = told.composed;
1108
+ // What it was composed to: the caller's budget, and what "Work here"
1109
+ // lent "Still open" (`keepFirstOpen`) — so a reader comparing the bundle
1110
+ // to its ceiling sees one that fits, which it does: the delivery shows
1111
+ // fewer work lines in what is left.
1112
+ const budgetBytes = req.budgetBytes + (first?.lent ?? 0);
879
1113
  return {
880
1114
  ...composed,
881
- budgetBytes: req.budgetBytes,
1115
+ budgetBytes,
882
1116
  day: req.day,
883
1117
  kept: {
884
1118
  identity: kept.identity.map((r) => r.id),
@@ -889,7 +1123,7 @@ export function render(
889
1123
  },
890
1124
  trimmed,
891
1125
  more: told.more,
892
- pressure: composed.bytes >= req.budgetBytes * t.BUDGET_PRESSURE,
1126
+ pressure: composed.bytes >= budgetBytes * t.BUDGET_PRESSURE,
893
1127
  overBudget: !fits,
894
1128
  page:
895
1129
  identity.page === null
@@ -992,8 +1226,8 @@ export const PREFACE_RESERVE_BYTES = 160;
992
1226
 
993
1227
  /**
994
1228
  * THE ROOM THE WAKE'S OWN FURNITURE TAKES AROUND THE PAGE, in bytes — the
995
- * number `Self.build` subtracts from the caller's ceiling before it caps the
996
- * page. Structural, not tunable, and measured rather than guessed: a test
1229
+ * number `Self.build` subtracts from the caller's ceiling before it asks
1230
+ * whether the page fits. Structural, not tunable, and measured rather than guessed: a test
997
1231
  * composes the widest plausible furniture (a six-digit day and six-digit lane
998
1232
  * counts in both comment lines, the stale dateline at a six-digit threshold)
999
1233
  * and asserts it fits under this.
@@ -1014,15 +1248,78 @@ export const PREFACE_RESERVE_BYTES = 160;
1014
1248
  export const PAGE_FLOOR_RESERVE_BYTES = 512;
1015
1249
 
1016
1250
  /**
1017
- * The smallest room worth rendering a PAGE into. Below it the wake says the
1018
- * page exists and does not fit, in one short line, rather than handing the
1019
- * reader a sentence and a marker — a fragment of a self is not a smaller self.
1251
+ * HOW MANY OF THE DELIVERY'S RESERVES ARE HELD UNDER THE SHARE RULE
1252
+ * (`handoff/#reserveBytes`), beside the preface: the handoff pointer with its
1253
+ * "Last here" line, and "Work here" (`counterpart.ts#wakeReserveBytes` and
1254
+ * `#workReserveBytes`). Each is at most an eighth of the ceiling and never past
1255
+ * `HANDOFF_RESERVE_MAX_BYTES`. A third reserve there is a third here, or the
1256
+ * page's limit below promises room the wake does not have — a test holds the
1257
+ * root to `deliveryReserveBound`.
1258
+ */
1259
+ export const DELIVERY_SHARED_RESERVES = 2;
1260
+
1261
+ /**
1262
+ * THE MOST THE DELIVERY CAN HOLD BACK FROM A HOST CEILING, in bytes
1263
+ * (2026-10-09): the preface's reserve and `DELIVERY_SHARED_RESERVES` terms at
1264
+ * the share rule's widest. What the root subtracts on a given day is usually
1265
+ * less; this is the bound the page's limit is sized under. Pure.
1266
+ */
1267
+ export function deliveryReserveBound(budgetBytes: number): number {
1268
+ const share = Math.max(
1269
+ 0,
1270
+ Math.min(HANDOFF_RESERVE_MAX_BYTES, Math.floor(budgetBytes / HANDOFF_RESERVE_MIN_BUDGET_MULTIPLE)),
1271
+ );
1272
+ return PREFACE_RESERVE_BYTES + DELIVERY_SHARED_RESERVES * share;
1273
+ }
1274
+
1275
+ /**
1276
+ * THE PAGE THE WAKE PRINTS WHOLE at a host ceiling, whatever the delivery
1277
+ * reserves that day, in bytes (2026-10-09): the ceiling, less the most the
1278
+ * delivery can hold back (`deliveryReserveBound`), less the furniture the wake
1279
+ * wraps the page in (`PAGE_FLOOR_RESERVE_BYTES`). `Self#pageBlock` prints a
1280
+ * page whole whenever it fits the room the composition actually has, which is
1281
+ * never less than this. Grows with the ceiling. Pure.
1282
+ */
1283
+ export function pageRoomBytes(hostBudgetBytes: number): number {
1284
+ return hostBudgetBytes - deliveryReserveBound(hostBudgetBytes) - PAGE_FLOOR_RESERVE_BYTES;
1285
+ }
1286
+
1287
+ /**
1288
+ * THE HOST CEILING THE PAGE'S LIMIT IS SIZED AGAINST, in bytes — the one
1289
+ * `install` writes when nobody names one (`adapters/cli/commands.ts`'s
1290
+ * `DEFAULT_BUDGET_BYTES` is this constant), and the one every host reads
1291
+ * today: Claude Code's hooks and Claude Desktop's `wake` tool deliver the same
1292
+ * bundle, composed at the configuration's `injectionBudgetBytes`.
1293
+ *
1294
+ * NOT a ceiling anything composes against: scar §2.18 holds, and a host that
1295
+ * reported none still composes no wake. It sizes one write limit, below.
1296
+ */
1297
+ export const PAGE_HOST_BUDGET_BYTES = 9_000;
1298
+
1299
+ /**
1300
+ * THE SELF PAGE'S LIMIT, in bytes (2026-10-09): the most a page may be written
1301
+ * as, and so the most the wake ever has to print — `pageRoomBytes` at
1302
+ * `PAGE_HOST_BUDGET_BYTES`, 6,078. One number for both ends, derived rather
1303
+ * than chosen, so the writer's limit and the wake's guarantee cannot drift
1304
+ * apart: a page any door accepts prints whole, byte for byte, at that ceiling
1305
+ * or a larger one, under the widest reserves the delivery can take.
1306
+ *
1307
+ * Why: #350's second reviewer measured the old pair apart — the wake's cap was
1308
+ * `min(6,144, budget − 512)`, which under the widest reserves at 9,000 is
1309
+ * 6,078, while the writer accepted up to 16,384 — so a page between them was
1310
+ * cut. A configured ceiling below 9,000 can still be too small for a page
1311
+ * under this limit; that wake says so in one line (`pageTooLargeLine`) and
1312
+ * never prints part of the page.
1020
1313
  */
1021
- export const PAGE_MIN_RENDER_BYTES = 240;
1314
+ export const PAGE_LIMIT_BYTES = pageRoomBytes(PAGE_HOST_BUDGET_BYTES);
1022
1315
 
1023
- /** The one short line a wake with no room for the page prints instead of it. */
1316
+ /**
1317
+ * The one short line a wake with no room for the WHOLE page prints instead of
1318
+ * it (2026-10-09: instead of any cut — a fragment of a self is not a smaller
1319
+ * self). It names both doors to the whole page.
1320
+ */
1024
1321
  export function pageTooLargeLine(bytes: number): string {
1025
- return `(My page is ${bytes} bytes — no room for it in this wake. Read it with 'counterparts self-page'.)`;
1322
+ return `(My page is ${bytes} bytes — no room for it whole in this wake. Read it with the self_page tool, or 'counterparts self-page'.)`;
1026
1323
  }
1027
1324
 
1028
1325
  export interface PrefaceFacts {
@@ -1075,6 +1372,65 @@ export function prefaceLine(f: PrefaceFacts): string {
1075
1372
  );
1076
1373
  }
1077
1374
 
1375
+ /**
1376
+ * THE TENSE ONLY THE DELIVERY KNOWS (2026-10-09).
1377
+ *
1378
+ * The owner's wake on 10-09 carried "Arriving: - … (due 2026-10-08) · …". By
1379
+ * design: a one-off stays in the horizon lane for its grace days, and the wake
1380
+ * is composed at a boundary — the evening one, the day before — and read the
1381
+ * next morning. The DATE was true; the lane's heading made it read as still to
1382
+ * come. A render can put a date it is already past into the past tense
1383
+ * (`Resolved.duePast`), but on the evening of the 8th the 8th is today, and only
1384
+ * the delivery knows the morning is the 9th.
1385
+ *
1386
+ * So, where the preface is composed and with the same date (`WakeDelivery
1387
+ * .date`, the person's day): an Arriving line whose due date is before today
1388
+ * reads `(was due 2026-10-05)`, and `(was due yesterday, 2026-10-08)` the day
1389
+ * after. Only the date prefix of a `- ` line in the Arriving lane is touched —
1390
+ * never a statement, never another lane — and only as far as `room` allows:
1391
+ * "was " on every such line first, then "yesterday, " while room remains, so a
1392
+ * line that would not fit keeps the date it was composed with, which is still
1393
+ * true. The caller passes what the preface reserve has left
1394
+ * (`PREFACE_RESERVE_BYTES`), so the delivered bundle stays inside it.
1395
+ */
1396
+ export function arrivingTense(text: string, today: string, room: number): string {
1397
+ if (!isDay(today) || room <= 0) return text;
1398
+ const lines = text.split("\n");
1399
+ // The LAST such heading: the lane is last, and a page above it is printed as
1400
+ // written, so a line of the page that happens to read "Arriving:" is not it.
1401
+ const start = lines.lastIndexOf(FRAMING.horizon);
1402
+ if (start < 0) return text;
1403
+ const prior = addDays(today, -1);
1404
+ const shape = /^(- (?:(?:by )?\d{4}-\d{2}-\d{2} \()?)(was )?due (\d{4}-\d{2}-\d{2})(?=[,)]| · )/;
1405
+ const was = `${DUE_PAST_WORD} `;
1406
+ const words = { was: byteLength(was) - byteLength(`${DUE_WORD} `), yesterday: byteLength("yesterday, ") };
1407
+ const past: { i: number; head: string; already: boolean; date: string; rest: string; was: boolean; yesterday: boolean }[] = [];
1408
+ for (let i = start + 1; i < lines.length && lines[i] !== ""; i++) {
1409
+ const line = lines[i] ?? "";
1410
+ const m = shape.exec(line);
1411
+ const date = m?.[3];
1412
+ if (m === null || date === undefined || date >= today) continue;
1413
+ const already = m[2] !== undefined;
1414
+ past.push({ i, head: m[1] ?? "- ", already, date, rest: line.slice(m[0].length), was: already, yesterday: false });
1415
+ }
1416
+ let left = room;
1417
+ for (const p of past) {
1418
+ if (p.was || left < words.was) continue;
1419
+ p.was = true;
1420
+ left -= words.was;
1421
+ }
1422
+ for (const p of past) {
1423
+ if (!p.was || p.date !== prior || left < words.yesterday) continue;
1424
+ p.yesterday = true;
1425
+ left -= words.yesterday;
1426
+ }
1427
+ for (const p of past) {
1428
+ if (!p.was || (p.already && !p.yesterday)) continue;
1429
+ lines[p.i] = `${p.head}${was}${p.yesterday ? "yesterday, " : ""}${p.date}${p.rest}`;
1430
+ }
1431
+ return lines.join("\n");
1432
+ }
1433
+
1078
1434
  export interface PrefacedBundle {
1079
1435
  readonly text: string;
1080
1436
  readonly bytes: number;