@vibefield/rosetta-room 0.1.11 → 0.1.12

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 (87) hide show
  1. package/README.md +64 -2
  2. package/dist/components/Transcript.js +6 -2
  3. package/dist/components/file-viewer/kinds.d.ts +22 -0
  4. package/dist/components/file-viewer/kinds.js +57 -0
  5. package/dist/components/file-viewer/markdown/render.d.ts +7 -1
  6. package/dist/components/file-viewer/markdown/render.js +16 -12
  7. package/dist/components/file-viewer/registry.d.ts +2 -2
  8. package/dist/components/file-viewer/registry.js +20 -59
  9. package/dist/components/transcript/Activity.d.ts +10 -4
  10. package/dist/components/transcript/Activity.js +36 -11
  11. package/dist/components/transcript/Compaction.d.ts +9 -0
  12. package/dist/components/transcript/Compaction.js +8 -4
  13. package/dist/components/transcript/Document.js +150 -208
  14. package/dist/components/transcript/PromptJump.js +28 -8
  15. package/dist/components/transcript/Prose.d.ts +5 -1
  16. package/dist/components/transcript/Prose.js +3 -3
  17. package/dist/components/transcript/ReadingBlocks.d.ts +71 -0
  18. package/dist/components/transcript/ReadingBlocks.js +279 -0
  19. package/dist/components/transcript/activity-plan.d.ts +38 -7
  20. package/dist/components/transcript/activity-plan.js +198 -28
  21. package/dist/components/transcript/attachment-file-source.d.ts +1 -2
  22. package/dist/components/transcript/attachment-file-source.js +7 -5
  23. package/dist/components/transcript/disclosure-state.d.ts +2 -0
  24. package/dist/components/transcript/disclosure-state.js +6 -2
  25. package/dist/components/transcript/document-model.js +53 -9
  26. package/dist/components/transcript/document-plan.js +263 -98
  27. package/dist/components/transcript/reading-identity.d.ts +14 -1
  28. package/dist/components/transcript/reading-identity.js +126 -10
  29. package/dist/conversation/ConversationGallery.d.ts +11 -0
  30. package/dist/conversation/ConversationGallery.js +14 -0
  31. package/dist/conversation/ConversationMedia.d.ts +44 -0
  32. package/dist/conversation/ConversationMedia.js +210 -0
  33. package/dist/conversation/ConversationProse.d.ts +13 -0
  34. package/dist/conversation/ConversationProse.js +6 -0
  35. package/dist/conversation/ConversationRunRows.d.ts +57 -0
  36. package/dist/conversation/ConversationRunRows.js +84 -0
  37. package/dist/conversation/ConversationTurnView.d.ts +64 -0
  38. package/dist/conversation/ConversationTurnView.js +169 -0
  39. package/dist/conversation/ConversationView.d.ts +3 -0
  40. package/dist/conversation/ConversationView.js +257 -0
  41. package/dist/conversation/feed-reads.d.ts +36 -0
  42. package/dist/conversation/feed-reads.js +155 -0
  43. package/dist/conversation/feed.d.ts +106 -0
  44. package/dist/conversation/feed.js +884 -0
  45. package/dist/conversation/index.d.ts +11 -0
  46. package/dist/conversation/index.js +25 -0
  47. package/dist/conversation/model.d.ts +108 -0
  48. package/dist/conversation/model.js +257 -0
  49. package/dist/conversation/ports.d.ts +194 -0
  50. package/dist/conversation/ports.js +9 -0
  51. package/dist/conversation/rosetta-client.d.ts +8 -0
  52. package/dist/conversation/rosetta-client.js +1 -0
  53. package/dist/conversation/use-conversation-feed.d.ts +16 -0
  54. package/dist/conversation/use-conversation-feed.js +41 -0
  55. package/dist/conversation/use-conversation-window.d.ts +50 -0
  56. package/dist/conversation/use-conversation-window.js +336 -0
  57. package/dist/conversation/words.d.ts +51 -0
  58. package/dist/conversation/words.js +79 -0
  59. package/dist/fixtures/conversation.d.ts +34 -0
  60. package/dist/fixtures/conversation.js +723 -0
  61. package/dist/fixtures/index.d.ts +2 -0
  62. package/dist/fixtures/index.js +1 -0
  63. package/dist/lib/session-leases.d.ts +11 -8
  64. package/dist/lib/transcript/collaboration.js +53 -19
  65. package/dist/lib/transcript/head-of-document.js +9 -3
  66. package/dist/lib/transcript/index.d.ts +1 -1
  67. package/dist/lib/transcript/index.js +1 -1
  68. package/dist/lib/transcript/prompt-jump.d.ts +2 -0
  69. package/dist/lib/transcript/prompt-jump.js +5 -1
  70. package/dist/lib/transcript/sittings.d.ts +2 -1
  71. package/dist/lib/transcript/sittings.js +72 -10
  72. package/dist/lib/transcript/tail-state.d.ts +9 -0
  73. package/dist/lib/transcript/tail-state.js +25 -1
  74. package/dist/lib/transcript/turns.d.ts +5 -0
  75. package/dist/lib/transcript/turns.js +221 -57
  76. package/dist/lib/transcript/use-transcript-tail.js +17 -3
  77. package/dist/lib/transcript/vocabulary.d.ts +2 -1
  78. package/dist/lib/transcript/vocabulary.js +11 -1
  79. package/dist/parts-context.d.ts +25 -0
  80. package/dist/parts-context.js +32 -0
  81. package/dist/ports.d.ts +3 -0
  82. package/dist/room-context.d.ts +1 -7
  83. package/dist/room-context.js +12 -18
  84. package/dist/styles/conversation-turn.css +147 -0
  85. package/dist/styles/conversation.css +45 -0
  86. package/dist/styles/transcript.css +11 -2
  87. package/package.json +9 -4
@@ -5,10 +5,12 @@
5
5
  // + `FileSource` pair the worktree supplies, so thumbnails and inline image
6
6
  // markers open the established image view instead of navigating the page.
7
7
  // Addresses come from the host's asset source; bytes ride the reading
8
- // session's client.
8
+ // session's client. Both are read from the parts' own context, which the room
9
+ // provides and a view with no room around it provides alone.
9
10
  import { useMemo } from "react";
10
- import { useReadingSession, useRoom } from "../../room-context.js";
11
- import { resolveKind } from "../file-viewer/index.js";
11
+ import { useParts, useReadingSession } from "../../parts-context.js";
12
+ // The kind table alone: the viewer's index would load every view with it.
13
+ import { kindOfName } from "../file-viewer/kinds.js";
12
14
  import { isRecordedImageAsset, isRecordedVideoAsset } from "../../lib/image-media.js";
13
15
  const EXTENSION_BY_MEDIA_TYPE = new Map([
14
16
  ["image/png", "png"],
@@ -27,7 +29,7 @@ export function attachmentViewerName(fact) {
27
29
  const base = fact.name?.trim() ||
28
30
  fact.ref?.label?.trim() ||
29
31
  "Image";
30
- const kind = resolveKind(base).viewer.id;
32
+ const kind = kindOfName(base).viewer;
31
33
  if (kind === "image" || kind === "svg")
32
34
  return base;
33
35
  const mediaType = fact.mediaType ?? (source?.type === "asset" ? source.asset.media_type : null);
@@ -101,7 +103,7 @@ export function createAttachmentReader(assets, readAsset) {
101
103
  /** The attachment reader for the document being read here: the host's
102
104
  * addresses, and bytes through the reading session's client. */
103
105
  export function useAttachmentReader() {
104
- const { assets, sessions } = useRoom();
106
+ const { assets, sessions } = useParts();
105
107
  const sessionId = useReadingSession();
106
108
  return useMemo(() => createAttachmentReader(assets, (asset, signal) => {
107
109
  if (sessionId === null)
@@ -3,4 +3,6 @@ export declare function DisclosureState({ children }: {
3
3
  readonly children: ReactNode;
4
4
  }): import("react").JSX.Element;
5
5
  export declare function useRowDisclosure(key: string, initial?: boolean): [boolean, (value: boolean | ((open: boolean) => boolean)) => void];
6
+ /** A run's own override. It subscribes to that run's answer, not to the
7
+ * store's version, so one run's press re-renders that run alone. */
6
8
  export declare function useGroupDisclosure(keys: readonly string[], revision: number): [boolean | undefined, (open: boolean) => void];
@@ -25,8 +25,12 @@ export function useRowDisclosure(key, initial = false) {
25
25
  }, [store, key, fallback]);
26
26
  return [open, setOpen];
27
27
  }
28
+ /** A run's own override. It subscribes to that run's answer, not to the
29
+ * store's version, so one run's press re-renders that run alone. */
28
30
  export function useGroupDisclosure(keys, revision) {
29
31
  const store = useContext(DisclosureContext);
30
- useSyncExternalStore(store?.subscribe ?? subscribeNothing, store?.version ?? (() => 0), () => 0);
31
- return [store?.group(keys, revision), (open) => store?.setGroup(keys, open, revision)];
32
+ const read = useCallback(() => store?.group(keys, revision), [store, keys, revision]);
33
+ const open = useSyncExternalStore(store?.subscribe ?? subscribeNothing, read, read);
34
+ const setOpen = useCallback((value) => store?.setGroup(keys, value, revision), [store, keys, revision]);
35
+ return [open, setOpen];
32
36
  }
@@ -15,6 +15,7 @@
15
15
  // silently closed over.
16
16
  import { useMemo } from "react";
17
17
  import { calendarDayKey, dayList, documentTurns, sittings } from "../../lib/transcript/index.js";
18
+ import { drawsNoMarker } from "../../lib/transcript/turns.js";
18
19
  import { documentPlan } from "./document-plan.js";
19
20
  function buildRepresentatives(turns) {
20
21
  const map = new Map();
@@ -52,23 +53,42 @@ function buildRepresentatives(turns) {
52
53
  }
53
54
  return map;
54
55
  }
55
- function buildGaps(rows) {
56
- const gaps = new Map();
57
- const previousBySession = new Map();
58
- for (const row of rows) {
56
+ /** The last read of each conversation's rows, by its first row (2026-11-29, the reader's arrival cost). */
57
+ const rowsRead = new WeakMap();
58
+ /** The rows drawn and the gaps. Both are read in order and carry nothing back,
59
+ * so rows that extend the rows read last read only the rows that arrived. */
60
+ function readRows(rows) {
61
+ const head = rows[0];
62
+ const held = head === undefined ? undefined : rowsRead.get(head);
63
+ const resumes = held !== undefined && held.rows.length <= rows.length &&
64
+ held.rows.every((row, index) => row === rows[index]);
65
+ const drawn = resumes ? held.drawn.slice() : [];
66
+ const gaps = new Map(resumes ? held.gaps : []);
67
+ const previousBySession = new Map(resumes ? held.previous : []);
68
+ for (let index = resumes ? held.rows.length : 0; index < rows.length; index += 1) {
69
+ const row = rows[index];
70
+ if (!drawsNoMarker(row))
71
+ drawn.push(row);
59
72
  const previous = previousBySession.get(row.session_id);
60
73
  const missing = previous === undefined ? 0 : row.ordinal - previous - 1;
61
74
  if (missing > 0)
62
75
  gaps.set(row.timeline_id, missing);
63
76
  previousBySession.set(row.session_id, row.ordinal);
64
77
  }
65
- return gaps;
78
+ const read = { rows: rows.slice(), drawn, gaps, previous: previousBySession };
79
+ if (head !== undefined)
80
+ rowsRead.set(head, read);
81
+ return read;
66
82
  }
67
83
  export function transcriptModel(rows, session, homeDirectory, now, options = {}) {
68
84
  // Sittings first: a run never straddles a pill, so the turns are cut at
69
- // every sitting's first row (`cutBefore`) before they are folded.
70
- const list = sittings(rows, now);
71
- const gaps = buildGaps(rows);
85
+ // every sitting's first row (`cutBefore`) before they are folded. They are
86
+ // cut over the rows the reader draws: a turn that finished draws nothing,
87
+ // so it opens no sitting, is counted in none and is no day's target, and a
88
+ // pause is measured from the last row drawn before it.
89
+ const read = readRows(rows);
90
+ const list = sittings(read.drawn, now);
91
+ const gaps = new Map(read.gaps);
72
92
  const cutBefore = new Set([...(options.cutBefore ?? gaps.keys()), ...list
73
93
  .slice(1)
74
94
  .map((sitting) => sitting.rows[0]?.timeline_id)
@@ -83,10 +103,34 @@ export function transcriptModel(rows, session, homeDirectory, now, options = {})
83
103
  sittings: list,
84
104
  days: dayList(list),
85
105
  plan: documentPlan(turns, list),
86
- representatives: buildRepresentatives(turns),
106
+ representatives: new DrawnBy(() => buildRepresentatives(turns)),
87
107
  gaps,
88
108
  };
89
109
  }
110
+ /** The representative map, built the first time it is read (2026-11-29, the
111
+ * reader's arrival cost). Only a landing reads it (a search hit, a meta link, a
112
+ * team message), so an arriving row derives no map over every row of the
113
+ * document; the first landing pays for it once per document. */
114
+ class DrawnBy {
115
+ #built = null;
116
+ #build;
117
+ constructor(build) {
118
+ this.#build = build;
119
+ }
120
+ get #map() {
121
+ return (this.#built ??= this.#build());
122
+ }
123
+ get size() { return this.#map.size; }
124
+ get(key) { return this.#map.get(key); }
125
+ has(key) { return this.#map.has(key); }
126
+ forEach(each, self) {
127
+ this.#map.forEach((value, key) => each.call(self, value, key, this));
128
+ }
129
+ entries() { return this.#map.entries(); }
130
+ keys() { return this.#map.keys(); }
131
+ values() { return this.#map.values(); }
132
+ [Symbol.iterator]() { return this.#map[Symbol.iterator](); }
133
+ }
90
134
  export function useTranscriptModel(rows, session, homeDirectory, now, options = {}) {
91
135
  const cwd = session?.cwd ?? null;
92
136
  const sharedKey = [...(options.sharedCollaborationSessionIds ?? [])].sort().join("\u0000");
@@ -28,29 +28,17 @@ function turnSitting(turn, indexOf) {
28
28
  const first = turn.segments[0];
29
29
  return first === undefined ? 0 : indexOf(segmentRows(first)[0]);
30
30
  }
31
- /** Adjacent setting turns that share one compact configuration group. */
32
- function clusterGroups(turns, sittingOf) {
33
- const groups = [];
34
- for (const turn of turns) {
35
- const current = groups.at(-1);
36
- const previous = current?.at(-1);
37
- const joins = current !== undefined &&
38
- previous !== undefined &&
39
- turn.clusteredWithPrevious &&
40
- turn.prompt !== null &&
41
- previous.prompt !== null &&
42
- turn.prompt.setting !== null &&
43
- previous.prompt.setting !== null &&
44
- !turn.prompt.fromParent &&
45
- !previous.prompt.fromParent &&
46
- previous.segments.length === 0 &&
47
- sittingOf(previous) === sittingOf(turn);
48
- if (joins)
49
- current.push(turn);
50
- else
51
- groups.push([turn]);
52
- }
53
- return groups;
31
+ /** Whether a setting turn joins the compact configuration group the turn before it ends. */
32
+ function joinsGroup(previous, turn, sittingOf) {
33
+ return turn.clusteredWithPrevious &&
34
+ turn.prompt !== null &&
35
+ previous.prompt !== null &&
36
+ turn.prompt.setting !== null &&
37
+ previous.prompt.setting !== null &&
38
+ !turn.prompt.fromParent &&
39
+ !previous.prompt.fromParent &&
40
+ previous.segments.length === 0 &&
41
+ sittingOf(previous) === sittingOf(turn);
54
42
  }
55
43
  /**
56
44
  * Fold turns and sittings into the sections the column draws. Sittings that
@@ -58,82 +46,12 @@ function clusterGroups(turns, sittingOf) {
58
46
  * be a cut with nothing on the far side of it.
59
47
  */
60
48
  export function documentPlan(turns, sittings) {
61
- const indexByRow = new Map();
62
- sittings.forEach((sitting, index) => {
63
- for (const row of sitting.rows)
64
- indexByRow.set(row.timeline_id, index);
65
- });
66
- const indexOf = (row) => indexByRow.get(row.timeline_id) ?? 0;
49
+ const { byRow, unique } = sittingIndexByRow(sittings);
50
+ const indexOf = (row) => byRow.get(row.timeline_id) ?? 0;
67
51
  const sittingOf = (turn) => turnSitting(turn, indexOf);
68
52
  const parts = [];
69
- for (const group of clusterGroups(turns, sittingOf)) {
70
- // Only the last turn of a cluster can carry an answer, by construction.
71
- const turn = group[group.length - 1];
72
- const prompts = group
73
- .map((member) => member.prompt)
74
- .filter((prompt) => prompt !== null);
75
- const openIndex = prompts[0] === undefined ? null : indexOf(prompts[0].row);
76
- // Cut the segments wherever the sitting changes under them.
77
- const slices = [];
78
- turn.segments.forEach((segment, at) => {
79
- const index = indexOf(segmentRows(segment)[0]);
80
- const last = slices.at(-1);
81
- if (last !== undefined && last.index === index)
82
- last.segments.push(segment);
83
- else
84
- slices.push({ index, segments: [segment], from: at });
85
- });
86
- const headSlice = turn.head === null ? -1 : slices.findIndex((slice) => turn.headIndex >= slice.from && turn.headIndex < slice.from + slice.segments.length);
87
- // A head with no segments to sit above still belongs to the reply that
88
- // reported it; it rides with the prompt rather than disappearing.
89
- const headOnOpener = turn.head !== null && headSlice === -1;
90
- if (slices.length === 0) {
91
- parts.push({
92
- key: turn.key,
93
- sittingIndex: openIndex ?? 0,
94
- turn,
95
- prompts,
96
- before: [],
97
- after: [],
98
- showHead: turn.head !== null,
99
- firstRowId: firstRowIdOf(prompts, []),
100
- at: firstStampOf(prompts, []),
101
- });
102
- continue;
103
- }
104
- slices.forEach((slice, at) => {
105
- const opensWithPrompt = at === 0 && openIndex !== null && openIndex === slice.index;
106
- const slicePrompts = at === 0 && (opensWithPrompt || openIndex === null) ? prompts : [];
107
- const cut = Math.max(0, Math.min(turn.headIndex - slice.from, slice.segments.length));
108
- const showHead = at === headSlice || (headOnOpener && at === 0);
109
- parts.push({
110
- key: `${turn.key}:${at}`,
111
- sittingIndex: slice.index,
112
- turn,
113
- prompts: slicePrompts,
114
- before: slice.segments.slice(0, cut),
115
- after: slice.segments.slice(cut),
116
- showHead,
117
- firstRowId: firstRowIdOf(slicePrompts, slice.segments),
118
- at: firstStampOf(slicePrompts, slice.segments),
119
- });
120
- });
121
- // The prompt fell in an earlier sitting than every segment: it needs its
122
- // own part so its pill and its panel are not filed under the reply's day.
123
- if (openIndex !== null && slices[0].index !== openIndex) {
124
- parts.splice(parts.length - slices.length, 0, {
125
- key: `${turn.key}:opener`,
126
- sittingIndex: openIndex,
127
- turn,
128
- prompts,
129
- before: [],
130
- after: [],
131
- showHead: false,
132
- firstRowId: firstRowIdOf(prompts, []),
133
- at: firstStampOf(prompts, []),
134
- });
135
- }
136
- }
53
+ for (const made of groupedParts(turns, sittings, unique, sittingOf, indexOf))
54
+ parts.push(...made);
137
55
  const planned = sittings.map((sitting) => ({ sitting, parts: [] }));
138
56
  for (const part of parts) {
139
57
  const target = planned[part.sittingIndex] ?? planned[0];
@@ -141,5 +59,252 @@ export function documentPlan(turns, sittings) {
141
59
  continue;
142
60
  target.parts.push(part);
143
61
  }
144
- return planned.filter((entry) => entry.parts.length > 0);
62
+ return planned.filter((entry) => entry.parts.length > 0).map(keptPlan);
63
+ }
64
+ /** The last maps of each conversation, by its first sitting's first row (2026-11-29, the reader's arrival cost). */
65
+ const sittingIndexes = new WeakMap();
66
+ /**
67
+ * Each row's sitting, by `timeline_id`; a row in two sittings is the later
68
+ * one's. Sittings that begin as the last map's sittings did, as the same
69
+ * objects at the same places, keep their rows' entries, so one arriving row
70
+ * maps the sittings it changed and not every row of the document (2026-11-29,
71
+ * the reader's arrival cost). Only a map whose ids were each in one sitting is
72
+ * carried on; any other is made again.
73
+ */
74
+ function sittingIndexByRow(sittings) {
75
+ const head = sittings[0]?.rows[0];
76
+ const known = head === undefined ? [] : (sittingIndexes.get(head) ?? []);
77
+ let held;
78
+ let kept = 0;
79
+ for (const index of known) {
80
+ if (!index.unique)
81
+ continue;
82
+ let same = 0;
83
+ while (same < sittings.length && same < index.sittings.length && sittings[same] === index.sittings[same])
84
+ same += 1;
85
+ if (same > kept) {
86
+ held = index;
87
+ kept = same;
88
+ }
89
+ }
90
+ let byRow;
91
+ let unique = true;
92
+ if (held !== undefined) {
93
+ byRow = held.byRow;
94
+ for (let index = kept; index < held.sittings.length; index += 1) {
95
+ for (const row of held.sittings[index].rows)
96
+ byRow.delete(row.timeline_id);
97
+ }
98
+ for (let index = kept; index < sittings.length && unique; index += 1) {
99
+ for (const row of sittings[index].rows) {
100
+ if (byRow.has(row.timeline_id)) {
101
+ unique = false;
102
+ break;
103
+ }
104
+ byRow.set(row.timeline_id, index);
105
+ }
106
+ }
107
+ }
108
+ if (held === undefined || !unique) {
109
+ byRow = new Map();
110
+ let rows = 0;
111
+ sittings.forEach((sitting, index) => {
112
+ rows += sitting.rows.length;
113
+ for (const row of sitting.rows)
114
+ byRow.set(row.timeline_id, index);
115
+ });
116
+ unique = byRow.size === rows;
117
+ }
118
+ if (head !== undefined) {
119
+ const made = { sittings: sittings.slice(), byRow: byRow, unique };
120
+ // Two per conversation: its column and its document may plan two chronologies that begin with the same row.
121
+ sittingIndexes.set(head, [made, ...known.filter((index) => index !== held).slice(0, 1)]);
122
+ }
123
+ return { byRow: byRow, unique };
124
+ }
125
+ /** The last groupings of each conversation, by its first turn (2026-11-29, the reader's arrival cost). */
126
+ const groupFolds = new WeakMap();
127
+ function leading(a, b) {
128
+ let at = 0;
129
+ while (at < a.length && at < b.length && a[at] === b[at])
130
+ at += 1;
131
+ return at;
132
+ }
133
+ /**
134
+ * The turns grouped, and each group's parts. A group of the last grouping is
135
+ * kept as it was when its turns are the same objects at the same places and
136
+ * every sitting it is placed by is the same object at the same place, in a
137
+ * document where each row is in one sitting: its parts are then what they
138
+ * were. The turns after the kept groups are grouped again, the first of them
139
+ * against the last kept group, so one arriving row groups the turn it joins
140
+ * and not every turn of the document (2026-11-29, the reader's arrival cost).
141
+ */
142
+ function groupedParts(turns, sittings, unique, sittingOf, indexOf) {
143
+ const head = turns[0];
144
+ const known = head === undefined ? [] : (groupFolds.get(head) ?? []);
145
+ let held;
146
+ let keep = 0;
147
+ if (unique) {
148
+ for (const fold of known) {
149
+ const sameTurns = leading(turns, fold.turns);
150
+ const sameSittings = leading(sittings, fold.sittings);
151
+ let count = 0;
152
+ while (count < fold.groups.length && fold.starts[count] + fold.groups[count].length <= sameTurns &&
153
+ fold.reach[count] < sameSittings)
154
+ count += 1;
155
+ if (count > keep) {
156
+ held = fold;
157
+ keep = count;
158
+ }
159
+ }
160
+ }
161
+ const groups = held === undefined ? [] : held.groups.slice(0, keep);
162
+ const starts = held === undefined ? [] : held.starts.slice(0, keep);
163
+ const parts = held === undefined ? [] : held.parts.slice(0, keep);
164
+ const reach = held === undefined ? [] : held.reach.slice(0, keep);
165
+ // Groups from `made` on are this call's own arrays, and their parts are made below.
166
+ let made = keep;
167
+ const from = keep === 0 ? 0 : starts[keep - 1] + groups[keep - 1].length;
168
+ for (let index = from; index < turns.length; index += 1) {
169
+ const turn = turns[index];
170
+ const last = groups.length - 1;
171
+ const current = groups[last];
172
+ const previous = current?.at(-1);
173
+ if (current !== undefined && previous !== undefined && joinsGroup(previous, turn, sittingOf)) {
174
+ if (last < made) {
175
+ groups[last] = [...current, turn];
176
+ made = last;
177
+ }
178
+ else
179
+ current.push(turn);
180
+ }
181
+ else {
182
+ groups.push([turn]);
183
+ starts.push(index);
184
+ }
185
+ }
186
+ parts.length = made;
187
+ reach.length = made;
188
+ for (let at = made; at < groups.length; at += 1) {
189
+ const kept = keptGroupParts(groups[at], indexOf);
190
+ parts.push(kept.parts);
191
+ reach.push(kept.reach);
192
+ }
193
+ if (head !== undefined) {
194
+ const fold = { turns: turns.slice(), sittings: sittings.slice(), groups, starts, parts, reach };
195
+ // Two per conversation: its column and its document may plan two chronologies that begin with the same turn.
196
+ groupFolds.set(head, [fold, ...known.filter((other) => other !== held).slice(0, 1)]);
197
+ }
198
+ return parts;
199
+ }
200
+ /** The parts one cluster of turns draws, kept by its last turn (2026-11-29, the
201
+ * reader's arrival cost): the same turns, opening and cut in the same sittings,
202
+ * give back the parts made before, as the same objects. */
203
+ const madeGroupParts = new WeakMap();
204
+ function keptGroupParts(group, indexOf) {
205
+ const turn = group[group.length - 1];
206
+ // Every sitting the parts are placed by: each prompt's, and each segment's first row's.
207
+ const indices = [];
208
+ for (const member of group)
209
+ if (member.prompt !== null)
210
+ indices.push(indexOf(member.prompt.row));
211
+ for (const segment of turn.segments)
212
+ indices.push(indexOf(segmentRows(segment)[0]));
213
+ const reach = indices.reduce((highest, index) => Math.max(highest, index), 0);
214
+ const held = madeGroupParts.get(turn);
215
+ if (held !== undefined && sameItems(held.group, group) && sameItems(held.indices, indices))
216
+ return { parts: held.parts, reach };
217
+ const parts = groupParts(group, indexOf);
218
+ madeGroupParts.set(turn, { group: [...group], indices, parts });
219
+ return { parts, reach };
220
+ }
221
+ function groupParts(group, indexOf) {
222
+ const out = [];
223
+ // Only the last turn of a cluster can carry an answer, by construction.
224
+ const turn = group[group.length - 1];
225
+ const prompts = group
226
+ .map((member) => member.prompt)
227
+ .filter((prompt) => prompt !== null);
228
+ const openIndex = prompts[0] === undefined ? null : indexOf(prompts[0].row);
229
+ // Cut the segments wherever the sitting changes under them.
230
+ const slices = [];
231
+ turn.segments.forEach((segment, at) => {
232
+ const index = indexOf(segmentRows(segment)[0]);
233
+ const last = slices.at(-1);
234
+ if (last !== undefined && last.index === index)
235
+ last.segments.push(segment);
236
+ else
237
+ slices.push({ index, segments: [segment], from: at });
238
+ });
239
+ const headSlice = turn.head === null ? -1 : slices.findIndex((slice) => turn.headIndex >= slice.from && turn.headIndex < slice.from + slice.segments.length);
240
+ // A head with no segments to sit above still belongs to the reply that
241
+ // reported it; it rides with the prompt rather than disappearing.
242
+ const headOnOpener = turn.head !== null && headSlice === -1;
243
+ if (slices.length === 0) {
244
+ out.push({
245
+ key: turn.key,
246
+ sittingIndex: openIndex ?? 0,
247
+ turn,
248
+ prompts,
249
+ before: [],
250
+ after: [],
251
+ showHead: turn.head !== null,
252
+ firstRowId: firstRowIdOf(prompts, []),
253
+ at: firstStampOf(prompts, []),
254
+ });
255
+ return out;
256
+ }
257
+ slices.forEach((slice, at) => {
258
+ const opensWithPrompt = at === 0 && openIndex !== null && openIndex === slice.index;
259
+ const slicePrompts = at === 0 && (opensWithPrompt || openIndex === null) ? prompts : [];
260
+ const cut = Math.max(0, Math.min(turn.headIndex - slice.from, slice.segments.length));
261
+ const showHead = at === headSlice || (headOnOpener && at === 0);
262
+ out.push({
263
+ key: `${turn.key}:${at}`,
264
+ sittingIndex: slice.index,
265
+ turn,
266
+ prompts: slicePrompts,
267
+ before: slice.segments.slice(0, cut),
268
+ after: slice.segments.slice(cut),
269
+ showHead,
270
+ firstRowId: firstRowIdOf(slicePrompts, slice.segments),
271
+ at: firstStampOf(slicePrompts, slice.segments),
272
+ });
273
+ });
274
+ // The prompt fell in an earlier sitting than every segment: it needs its
275
+ // own part so its pill and its panel are not filed under the reply's day.
276
+ if (openIndex !== null && slices[0].index !== openIndex) {
277
+ out.splice(out.length - slices.length, 0, {
278
+ key: `${turn.key}:opener`,
279
+ sittingIndex: openIndex,
280
+ turn,
281
+ prompts,
282
+ before: [],
283
+ after: [],
284
+ showHead: false,
285
+ firstRowId: firstRowIdOf(prompts, []),
286
+ at: firstStampOf(prompts, []),
287
+ });
288
+ }
289
+ return out;
290
+ }
291
+ /** Planned sittings kept by their sitting (2026-11-29, the reader's arrival cost): the same sitting holding parts
292
+ * equal field by field (the same turns, prompts and segments) is handed back as the same object, so the reading
293
+ * blocks drawn from it are not made again. */
294
+ const madePlans = new WeakMap();
295
+ function keptPlan(entry) {
296
+ const held = madePlans.get(entry.sitting);
297
+ if (held !== undefined && held.parts.length === entry.parts.length &&
298
+ held.parts.every((part, index) => samePart(part, entry.parts[index])))
299
+ return held;
300
+ madePlans.set(entry.sitting, entry);
301
+ return entry;
302
+ }
303
+ function samePart(a, b) {
304
+ return a === b || a.key === b.key && a.sittingIndex === b.sittingIndex && a.turn === b.turn && a.showHead === b.showHead &&
305
+ a.firstRowId === b.firstRowId && a.at === b.at && sameItems(a.prompts, b.prompts) &&
306
+ sameItems(a.before, b.before) && sameItems(a.after, b.after);
307
+ }
308
+ function sameItems(a, b) {
309
+ return a.length === b.length && a.every((item, index) => item === b[index]);
145
310
  }
@@ -1,2 +1,15 @@
1
- import type { ReadingSitting } from "./activity-plan.js";
1
+ import type { TimelineEntry } from "@vibefield/rosetta-protocol";
2
+ import type { ReadingBlock, ReadingSitting } from "./activity-plan.js";
3
+ /** Every row a reading block draws, in order. */
4
+ export declare function readingBlockRows(block: ReadingBlock): readonly TimelineEntry[];
5
+ /** Structural equality over the document's plain data: arrays, maps and sets
6
+ * in order, own keys of plain objects (whose prototype is `Object.prototype`
7
+ * or null), and identity for everything else: two dates, two patterns or two
8
+ * instances of a class are equal only when they are one object. A row that is
9
+ * the same object is equal without being read. */
10
+ export declare function sameValue(a: unknown, b: unknown, depth?: number): boolean;
11
+ /** Keys survive as before; beyond that, a block whose rows did not change is
12
+ * handed back as the PREVIOUS object, and a sitting whose blocks all came back
13
+ * is the previous sitting. A memoised view of either then skips its render, so
14
+ * one arriving row costs the blocks it changed and not the document. */
2
15
  export declare function retainReadingIdentity(previous: readonly ReadingSitting[], next: readonly ReadingSitting[]): ReadingSitting[];