pi-odd-worktree 3.15.1 → 3.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +48 -25
- package/extensions/odd-worktree-tools.ts +64 -211
- package/lib/shell-api/api.ts +523 -0
- package/lib/shell-api/arm.ts +83 -0
- package/lib/shell-api/bus.ts +1389 -0
- package/lib/shell-api/capture.ts +1340 -0
- package/lib/shell-api/card-style-line.ts +112 -0
- package/lib/shell-api/card-style-rows.ts +390 -0
- package/lib/shell-api/card-style-transform.ts +862 -0
- package/lib/shell-api/card-style-wiring.ts +477 -0
- package/lib/shell-api/card-styles.ts +153 -0
- package/lib/shell-api/config.ts +71 -0
- package/lib/shell-api/extension.ts +247 -0
- package/lib/shell-api/hook.ts +322 -0
- package/lib/shell-api/index.ts +88 -0
- package/lib/shell-api/message-design-adoption.ts +549 -0
- package/lib/shell-api/message-design-rows.ts +313 -0
- package/lib/shell-api/message-design-wiring.ts +369 -0
- package/lib/shell-api/message-designs.ts +746 -0
- package/lib/shell-api/notices.ts +137 -0
- package/lib/shell-api/plugin-lines.ts +877 -0
- package/lib/shell-api/presence.ts +258 -0
- package/lib/shell-api/repaint.ts +351 -0
- package/lib/shell-api/row-labels.ts +146 -0
- package/lib/shell-api/row-names.ts +273 -0
- package/lib/shell-api/row-origin.ts +144 -0
- package/lib/shell-api/rows.ts +591 -0
- package/lib/shell-api/section-panel.ts +378 -0
- package/lib/shell-api/section-pointer.ts +164 -0
- package/lib/shell-api/sections-preview.ts +275 -0
- package/lib/shell-api/sections-rows.ts +133 -0
- package/lib/shell-api/sidebar-render.ts +705 -0
- package/lib/shell-api/sidebar.ts +1126 -0
- package/lib/shell-api/store.ts +279 -0
- package/lib/shell-api/tool-design-adoption.ts +263 -0
- package/lib/shell-api/tool-design-rows.ts +307 -0
- package/lib/shell-api/tool-design-wiring.ts +481 -0
- package/lib/shell-api/tool-designs.ts +1411 -0
- package/lib/shell-api/transform-pipeline.ts +351 -0
- package/lib/shell-api/types.ts +803 -0
- package/lib/shell-api/visibility.ts +154 -0
- package/package.json +3 -2
- package/tools/files-bridge.test.js +81 -66
- package/tools/fixtures/load-extension.js +40 -18
- package/tools/issues-command.test.js +4 -7
- package/tools/issues-section.test.js +71 -81
- package/tools/odd-rail-wiring.test.js +16 -10
- package/tools/package-surface.test.js +42 -13
- package/tools/prs-command.test.js +11 -6
- package/tools/shell-api/api.test.ts +484 -0
- package/tools/shell-api/apply-on-move.test.ts +529 -0
- package/tools/shell-api/arm.test.ts +220 -0
- package/tools/shell-api/bridge-ready.test.ts +309 -0
- package/tools/shell-api/bus.test.ts +194 -0
- package/tools/shell-api/button-rows.test.ts +435 -0
- package/tools/shell-api/capture.test.ts +583 -0
- package/tools/shell-api/card-style-rows.test.ts +493 -0
- package/tools/shell-api/card-style-transform.test.ts +379 -0
- package/tools/shell-api/card-style-wiring.test.ts +258 -0
- package/tools/shell-api/card-styles-e2e.test.ts +502 -0
- package/tools/shell-api/card-styles.test.ts +324 -0
- package/tools/shell-api/card-transcript-path.test.ts +178 -0
- package/tools/shell-api/compatibility-regression.test.mjs +64 -0
- package/tools/shell-api/config.test.ts +93 -0
- package/tools/shell-api/docs-coverage.test.ts +82 -0
- package/tools/shell-api/e2e.test.ts +562 -0
- package/tools/shell-api/example-bash-identity.test.ts +372 -0
- package/tools/shell-api/example-bash-matrix.test.ts +856 -0
- package/tools/shell-api/example-designs.test.ts +737 -0
- package/tools/shell-api/example-neon-transform.test.ts +283 -0
- package/tools/shell-api/example-plain-style.test.ts +1225 -0
- package/tools/shell-api/extension.test.ts +157 -0
- package/tools/shell-api/fake-events.ts +58 -0
- package/tools/shell-api/fake-host.ts +211 -0
- package/tools/shell-api/fake-sidebar.ts +516 -0
- package/tools/shell-api/fake-transcript.ts +486 -0
- package/tools/shell-api/frame-telemetry.test.ts +317 -0
- package/tools/shell-api/hook.test.ts +196 -0
- package/tools/shell-api/manifest.test.ts +47 -0
- package/tools/shell-api/message-design-adoption.test.ts +611 -0
- package/tools/shell-api/message-design-follows.test.ts +361 -0
- package/tools/shell-api/message-design-pi-release.test.mjs +157 -0
- package/tools/shell-api/message-design-regressions.test.mjs +162 -0
- package/tools/shell-api/message-design-rows.test.ts +266 -0
- package/tools/shell-api/message-design-wiring.test.ts +362 -0
- package/tools/shell-api/message-designs.test.ts +339 -0
- package/tools/shell-api/new-category.test.ts +1101 -0
- package/tools/shell-api/notices.test.ts +292 -0
- package/tools/shell-api/plugin-integration-lines.test.ts +827 -0
- package/tools/shell-api/presence.test.ts +396 -0
- package/tools/shell-api/rail-part-hits.test.ts +281 -0
- package/tools/shell-api/rail-surfaces.test.ts +402 -0
- package/tools/shell-api/rail-width.test.ts +406 -0
- package/tools/shell-api/regression.test.ts +434 -0
- package/tools/shell-api/repaint-api.test.ts +200 -0
- package/tools/shell-api/reserved-categories.test.ts +250 -0
- package/tools/shell-api/row-labels.test.ts +261 -0
- package/tools/shell-api/row-names.test.ts +407 -0
- package/tools/shell-api/row-origin.test.ts +395 -0
- package/tools/shell-api/row-placement-regression.test.ts +797 -0
- package/tools/shell-api/row-placement.test.ts +410 -0
- package/tools/shell-api/section-interaction.test.ts +364 -0
- package/tools/shell-api/section-panel.test.ts +430 -0
- package/tools/shell-api/section-placement-rail.test.ts +390 -0
- package/tools/shell-api/section-placement.test.ts +386 -0
- package/tools/shell-api/sections-preview.test.ts +312 -0
- package/tools/shell-api/sections-rows.test.ts +293 -0
- package/tools/shell-api/sidebar-backend.test.ts +477 -0
- package/tools/shell-api/sidebar-e2e.test.ts +317 -0
- package/tools/shell-api/sidebar-hover.test.ts +276 -0
- package/tools/shell-api/sidebar-independent.test.ts +291 -0
- package/tools/shell-api/sidebar-render.test.ts +281 -0
- package/tools/shell-api/sidebar-sections.test.ts +247 -0
- package/tools/shell-api/store.test.ts +192 -0
- package/tools/shell-api/surface-repaint-cost.test.ts +520 -0
- package/tools/shell-api/tool-design-card-style.test.ts +308 -0
- package/tools/shell-api/tool-design-chain.test.ts +471 -0
- package/tools/shell-api/tool-design-cost.test.ts +591 -0
- package/tools/shell-api/tool-design-rows.test.ts +627 -0
- package/tools/shell-api/tool-design-scopes-audit.test.ts +212 -0
- package/tools/shell-api/tool-designs.test.ts +2412 -0
- package/tools/shell-api/transform-pipeline.test.ts +376 -0
- package/tools/shell-api/transforms-bus.test.ts +233 -0
- package/tools/shell-api/verifier-width-regressions.test.ts +147 -0
- package/tools/shell-api-rpc.test.js +189 -0
- package/tools/user-prompt-card.test.js +8 -9
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The transform pipeline (S2, S3, S4): the general ordered pass the conversation wrapper runs
|
|
3
|
+
* BEFORE the rows reach the host's frame loop, and after the card-style repaint.
|
|
4
|
+
*
|
|
5
|
+
* ## Why the pipeline lives here, and not in each plugin
|
|
6
|
+
*
|
|
7
|
+
* `pi.registerToolRenderer` runs when a tool row is BUILT (`docs/extensions.md`, "Custom
|
|
8
|
+
* Rendering"), so a rendering choice made through that slot applies to the NEXT row and leaves the
|
|
9
|
+
* cards already on screen untouched. `./card-style-wiring.ts` already wraps the conversation
|
|
10
|
+
* component's `render(width)`, which is the one place that sees every frame, so this module turns
|
|
11
|
+
* that wrapper into an extension point: registered transforms receive the rows the host produced
|
|
12
|
+
* and return the rows to draw.
|
|
13
|
+
*
|
|
14
|
+
* ## What the pipeline guarantees
|
|
15
|
+
*
|
|
16
|
+
* - **Order.** `snapshotTransforms()` decides the sequence (`./bus.ts`); each transform receives the
|
|
17
|
+
* PREVIOUS transform's output, so two plugins compose without knowing about each other (S2).
|
|
18
|
+
* - **Identity.** With no transform registered the input array itself is returned, which is what
|
|
19
|
+
* keeps a session that never used the feature byte-identical — the same promise the card-style
|
|
20
|
+
* transformer makes for `native` (S2).
|
|
21
|
+
* - **Isolation.** A transform that throws, or that returns something which is not an array of
|
|
22
|
+
* strings, loses only its own contribution: the lines that were in force are kept, the failure is
|
|
23
|
+
* recorded with the transform's id and message, and the remaining transforms still run (S3).
|
|
24
|
+
* Nothing here can throw into the host's render, and a frame is never left without an array.
|
|
25
|
+
* - **Width.** Whatever a transform returns is clipped to the width the host rendered at, with the
|
|
26
|
+
* same `clipToWidth` the card styles use, so a transform cannot make the frame wider than the
|
|
27
|
+
* terminal (S4).
|
|
28
|
+
*
|
|
29
|
+
* ## Diagnostics
|
|
30
|
+
*
|
|
31
|
+
* The outcome of the most recent frame is kept per transform, replacing the previous frame's record
|
|
32
|
+
* — a report is about "now", not a history. the diagnostics record keeps it. A session
|
|
33
|
+
* that registered no transform records nothing at all, which is what keeps its report unchanged.
|
|
34
|
+
*
|
|
35
|
+
* ## Frame telemetry (F1, F4)
|
|
36
|
+
*
|
|
37
|
+
* Each frame's entry is more than an outcome: it also carries the COUNTS of what that transform did
|
|
38
|
+
* with it — the lines it received, plus whatever the transform reported through `ctx.report(...)`:
|
|
39
|
+
* candidates examined, cards recognised, cards rewritten, shapes matched by name. It is what turns
|
|
40
|
+
* "the toggle changes nothing in a real TUI" into either "the grammar recognised no card" or "the
|
|
41
|
+
* grammar recognised cards and rewrote them, so the host draws them elsewhere".
|
|
42
|
+
*
|
|
43
|
+
* The record is COUNTS ONLY. A frame carries the user's private output, so no line and no text is
|
|
44
|
+
* ever stored: every number is clamped to a non-negative integer, and a shape name is kept only
|
|
45
|
+
* when it is a name (lowercase, no spaces, no escapes, at most `MAX_SHAPE_NAME` characters) — a line
|
|
46
|
+
* smuggled into that field is dropped, not displayed. Recording reads primitives only and is
|
|
47
|
+
* guarded, so telemetry cannot throw into the frame and cannot cost one; the counts are collected
|
|
48
|
+
* from the same call that produces the output, with no second pass.
|
|
49
|
+
*
|
|
50
|
+
* This module is internal: `src/index.ts` does not re-export it.
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
import { snapshotTransforms } from "./bus.ts";
|
|
54
|
+
import { clipToWidth } from "./card-style-transform.ts";
|
|
55
|
+
import { isRecord } from "./rows.ts";
|
|
56
|
+
import type { RowOrigin, TransformContext, TranscriptTransform } from "./types.ts";
|
|
57
|
+
|
|
58
|
+
/** What the pipeline did with one transform in the most recent frame. */
|
|
59
|
+
export type TransformFrameStatus = "applied" | "skipped" | "failed";
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* One transform's telemetry for the frame that just ran (F1): counts, and nothing of the frame.
|
|
63
|
+
*
|
|
64
|
+
* `lines` is what the pipeline handed the transform (the number of rows in force at that point, so
|
|
65
|
+
* it shrinks and grows with the card-style repaint and the transforms before it); the rest is what
|
|
66
|
+
* the transform reported about its own grammar. `shapes` is ordered by first report, so a report is
|
|
67
|
+
* deterministic; it is empty for a grammar with a single shape or a transform that reports none.
|
|
68
|
+
*/
|
|
69
|
+
export interface TransformFrameCounts {
|
|
70
|
+
lines: number;
|
|
71
|
+
candidates: number;
|
|
72
|
+
recognised: number;
|
|
73
|
+
rewritten: number;
|
|
74
|
+
shapes: ReadonlyArray<{ name: string; count: number }>;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* One transform's contribution to the last frame. `skipped` means it was called and left the rows
|
|
79
|
+
* as they were; `failed` means it threw or returned something that is not lines, and its message
|
|
80
|
+
* says which. `counts` is this frame's telemetry, present for every status because the line count is
|
|
81
|
+
* known even when the transform never reported.
|
|
82
|
+
*/
|
|
83
|
+
export interface TransformFrameOutcome {
|
|
84
|
+
id: string;
|
|
85
|
+
status: TransformFrameStatus;
|
|
86
|
+
/** Only for `failed`: what went wrong, verbatim, for the diagnostics record. */
|
|
87
|
+
message?: string;
|
|
88
|
+
/** F1: this frame's counts. Never any line, text or content of the frame. */
|
|
89
|
+
counts: TransformFrameCounts;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** The counts a transform reported, sanitised. `lines` is added by the pipeline. */
|
|
93
|
+
type ReportedCounts = Omit<TransformFrameCounts, "lines">;
|
|
94
|
+
|
|
95
|
+
/** The longest a shape name may be. A shape is a grammar branch, not a sentence. */
|
|
96
|
+
const MAX_SHAPE_NAME = 24;
|
|
97
|
+
|
|
98
|
+
/** How many distinct shape names are kept per frame; anything past this is dropped. */
|
|
99
|
+
const MAX_SHAPES = 12;
|
|
100
|
+
|
|
101
|
+
/** A count above this is not a measurement; it is clamped so one frame cannot bloat the record. */
|
|
102
|
+
const MAX_COUNT = 1_000_000_000;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* What a shape name may be: a lowercase name — letters, digits, `_`, `.`, `-` — and nothing else.
|
|
106
|
+
* No space means no line of output can be carried in this field, and no uppercase or escape means a
|
|
107
|
+
* plugin cannot paint or smuggle through it. A name that does not match is dropped, never stored.
|
|
108
|
+
*/
|
|
109
|
+
const SHAPE_NAME = /^[a-z][a-z0-9_.-]*$/u;
|
|
110
|
+
|
|
111
|
+
interface PipelineState {
|
|
112
|
+
/** The most recent frame's outcomes, in the order the transforms ran. */
|
|
113
|
+
lastFrame: TransformFrameOutcome[];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Process-global, like every other piece of bridge state (`./bus.ts` explains why). */
|
|
117
|
+
const PIPELINE_STATE = Symbol.for("gentle-shell-bridge.transform-pipeline.state.v1");
|
|
118
|
+
|
|
119
|
+
function pipelineHost(): Record<symbol, PipelineState | undefined> {
|
|
120
|
+
// SAFETY: only `pipelineState()` writes this key, and it re-checks the shape it finds.
|
|
121
|
+
return globalThis as unknown as Record<symbol, PipelineState | undefined>;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function pipelineState(): PipelineState {
|
|
125
|
+
const host = pipelineHost();
|
|
126
|
+
const existing = host[PIPELINE_STATE];
|
|
127
|
+
if (isRecord(existing) && Array.isArray(existing.lastFrame)) return existing as PipelineState;
|
|
128
|
+
const created: PipelineState = { lastFrame: [] };
|
|
129
|
+
host[PIPELINE_STATE] = created;
|
|
130
|
+
return created;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** What the most recent frame did with each transform, in pipeline order. A copy, never the state. */
|
|
134
|
+
export function getTransformFrameOutcomes(): TransformFrameOutcome[] {
|
|
135
|
+
return pipelineState().lastFrame.map((outcome) => {
|
|
136
|
+
// A copy all the way down: a caller cannot reach into the record and rewrite the frame it holds.
|
|
137
|
+
// Counts from a copy of this module older than the telemetry read as zeros, never as `undefined`.
|
|
138
|
+
const counts = normaliseCounts(outcome.counts);
|
|
139
|
+
return { ...outcome, counts: { ...counts, shapes: counts.shapes.map((shape) => ({ ...shape })) } };
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Test-only seam; deliberately not re-exported from `src/index.ts`. */
|
|
144
|
+
export function resetTransformPipelineForTests(): void {
|
|
145
|
+
delete pipelineHost()[PIPELINE_STATE];
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function errorMessage(error: unknown): string {
|
|
149
|
+
return error instanceof Error ? error.message : String(error);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Whether a transform's return value is a row array at all. */
|
|
153
|
+
function isLineArray(value: unknown): value is readonly string[] {
|
|
154
|
+
return Array.isArray(value) && value.every((line) => typeof line === "string");
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function describeValue(value: unknown): string {
|
|
158
|
+
if (value === null) return "null";
|
|
159
|
+
if (Array.isArray(value)) return "an array with non-string entries";
|
|
160
|
+
return typeof value;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// ---------------------------------------------------------------------------------------------
|
|
164
|
+
// Telemetry: counts in, counts out, and never a line of the frame
|
|
165
|
+
// ---------------------------------------------------------------------------------------------
|
|
166
|
+
|
|
167
|
+
/** One count: a finite, non-negative integer, clamped so a report cannot bloat the record. */
|
|
168
|
+
function countOf(value: unknown): number {
|
|
169
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) return 0;
|
|
170
|
+
return Math.min(Math.floor(value), MAX_COUNT);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The shape names a transform reported, keeping only the ones that ARE names (F1).
|
|
175
|
+
*
|
|
176
|
+
* This is the rule that makes "counts only" true rather than promised: a name with a space could be
|
|
177
|
+
* a line of the user's output, one with an escape could paint, and a long one could be a paragraph.
|
|
178
|
+
* Such an entry is DROPPED — never rewritten, never shown — and the counts are kept.
|
|
179
|
+
*/
|
|
180
|
+
function shapeNames(shapes: unknown): Array<{ name: string; count: number }> {
|
|
181
|
+
if (!isRecord(shapes)) return [];
|
|
182
|
+
const names: Array<{ name: string; count: number }> = [];
|
|
183
|
+
for (const [name, value] of Object.entries(shapes)) {
|
|
184
|
+
if (names.length >= MAX_SHAPES) break;
|
|
185
|
+
if (name.length > MAX_SHAPE_NAME || !SHAPE_NAME.test(name)) continue;
|
|
186
|
+
const count = countOf(value);
|
|
187
|
+
if (count === 0) continue;
|
|
188
|
+
names.push({ name, count });
|
|
189
|
+
}
|
|
190
|
+
return names;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** A report, sanitised: numbers clamped, shape names filtered, anything else zero. */
|
|
194
|
+
function reportedCounts(reported: unknown): ReportedCounts {
|
|
195
|
+
if (!isRecord(reported)) return { candidates: 0, recognised: 0, rewritten: 0, shapes: [] };
|
|
196
|
+
return {
|
|
197
|
+
candidates: countOf(reported.candidates),
|
|
198
|
+
recognised: countOf(reported.recognised),
|
|
199
|
+
rewritten: countOf(reported.rewritten),
|
|
200
|
+
shapes: shapeNames(reported.shapes),
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** A record read back (possibly written by another copy of this module): counts, or zeros. */
|
|
205
|
+
function normaliseCounts(value: unknown): TransformFrameCounts {
|
|
206
|
+
const counts = isRecord(value) ? value : {};
|
|
207
|
+
return {
|
|
208
|
+
lines: countOf(counts.lines),
|
|
209
|
+
candidates: countOf(counts.candidates),
|
|
210
|
+
recognised: countOf(counts.recognised),
|
|
211
|
+
rewritten: countOf(counts.rewritten),
|
|
212
|
+
shapes: readShapes(counts.shapes),
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** The shape list of a stored record, keeping only entries that are still names and counts. */
|
|
217
|
+
function readShapes(value: unknown): Array<{ name: string; count: number }> {
|
|
218
|
+
if (!Array.isArray(value)) return [];
|
|
219
|
+
const names: Array<{ name: string; count: number }> = [];
|
|
220
|
+
for (const entry of value) {
|
|
221
|
+
if (names.length >= MAX_SHAPES) break;
|
|
222
|
+
if (!isRecord(entry)) continue;
|
|
223
|
+
const { name } = entry;
|
|
224
|
+
const count = countOf(entry.count);
|
|
225
|
+
if (
|
|
226
|
+
typeof name !== "string" ||
|
|
227
|
+
name.length > MAX_SHAPE_NAME ||
|
|
228
|
+
!SHAPE_NAME.test(name) ||
|
|
229
|
+
count === 0
|
|
230
|
+
) {
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
names.push({ name, count });
|
|
234
|
+
}
|
|
235
|
+
return names;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** This frame's entry counts: the lines the transform received plus what it reported (F1). */
|
|
239
|
+
function frameCounts(lines: number, reported: ReportedCounts | undefined): TransformFrameCounts {
|
|
240
|
+
const base = reported ?? { candidates: 0, recognised: 0, rewritten: 0, shapes: [] };
|
|
241
|
+
return {
|
|
242
|
+
lines,
|
|
243
|
+
candidates: base.candidates,
|
|
244
|
+
recognised: base.recognised,
|
|
245
|
+
rewritten: base.rewritten,
|
|
246
|
+
shapes: base.shapes.map((shape) => ({ ...shape })),
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* The transform's output, clipped to the host's width (S4). Returns `undefined` when nothing
|
|
252
|
+
* changed, so the caller keeps the array it already had and identity survives a no-op.
|
|
253
|
+
*
|
|
254
|
+
* The same ARRAY means the transform touched nothing: it is handed back without measuring or
|
|
255
|
+
* clipping a single row. That is the rule a real TUI needed (W2-1): the example returns `lines` in
|
|
256
|
+
* `default` mode, and clipping the host's own rows afterwards cut them on the right, because the
|
|
257
|
+
* clip's model disagreed with the host's about hyperlinks and grapheme clusters.
|
|
258
|
+
*/
|
|
259
|
+
function clipResult(next: readonly string[], current: readonly string[], width: number): string[] | undefined {
|
|
260
|
+
if (next === current) return undefined;
|
|
261
|
+
const clipped = next.map((line) => clipToWidth(line, width));
|
|
262
|
+
const unchanged =
|
|
263
|
+
clipped.length === current.length &&
|
|
264
|
+
clipped.every((line, index) => line === current[index]);
|
|
265
|
+
return unchanged ? undefined : clipped;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Run the registered transforms over the frame, in order, and return the rows to draw (S2, S3, S4).
|
|
270
|
+
*
|
|
271
|
+
* `rows` is the frame's row origin (S1, `./row-origin.ts`): which host child drew each row, one entry
|
|
272
|
+
* per row, or an empty array when the origin is unknown. It is handed to every transform in `ctx`
|
|
273
|
+
* and never READ here — the pipeline is a pass-through for it, so a transform that ignores the field
|
|
274
|
+
* produces the same bytes it did before the field existed, and a frame whose origin is unknown is
|
|
275
|
+
* simply a frame with no identity (never a skipped frame, never a throw).
|
|
276
|
+
*
|
|
277
|
+
* The returned array is the input instance whenever the frame was not changed: no transform
|
|
278
|
+
* registered, every transform a no-op, or every transform failing. The frame's outcome record is
|
|
279
|
+
* replaced here, once per frame.
|
|
280
|
+
*/
|
|
281
|
+
export function applyTransforms(
|
|
282
|
+
lines: string[],
|
|
283
|
+
width: number,
|
|
284
|
+
rows: readonly RowOrigin[] = [],
|
|
285
|
+
): string[] {
|
|
286
|
+
let transforms: TranscriptTransform[];
|
|
287
|
+
try {
|
|
288
|
+
transforms = snapshotTransforms();
|
|
289
|
+
} catch {
|
|
290
|
+
// The registry is process-global state written by other copies; a broken read is a pass-through
|
|
291
|
+
// rather than a frame this module takes down.
|
|
292
|
+
return lines;
|
|
293
|
+
}
|
|
294
|
+
if (transforms.length === 0) return lines;
|
|
295
|
+
|
|
296
|
+
const outcomes: TransformFrameOutcome[] = [];
|
|
297
|
+
let current = lines;
|
|
298
|
+
|
|
299
|
+
for (const transform of transforms) {
|
|
300
|
+
// The count the pipeline owns (F1): the rows in force when this transform was handed the frame.
|
|
301
|
+
const received = current.length;
|
|
302
|
+
let reported: ReportedCounts | undefined;
|
|
303
|
+
const ctx: TransformContext = {
|
|
304
|
+
width,
|
|
305
|
+
rows,
|
|
306
|
+
report: (counts): void => {
|
|
307
|
+
// F4: telemetry is diagnostics. Sanitising reads primitives only, and this guard means no
|
|
308
|
+
// report — however malformed — can reach the frame the host is waiting for.
|
|
309
|
+
try {
|
|
310
|
+
reported = reportedCounts(counts);
|
|
311
|
+
} catch {
|
|
312
|
+
reported = undefined;
|
|
313
|
+
}
|
|
314
|
+
},
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
let next: readonly string[];
|
|
318
|
+
try {
|
|
319
|
+
// `transform` is validated at registration; a foreign mutation after that is still caught
|
|
320
|
+
// here, because the call and the shape check are both inside this guard.
|
|
321
|
+
const produced: unknown = transform.transform.call(transform, current, ctx);
|
|
322
|
+
if (!isLineArray(produced)) {
|
|
323
|
+
throw new Error(
|
|
324
|
+
`the transform returned ${describeValue(produced)}; a transform must return an array of lines (string[]).`,
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
next = produced;
|
|
328
|
+
} catch (error) {
|
|
329
|
+
// S3: this transform is skipped for the frame, and the remaining ones still run. F1: the
|
|
330
|
+
// counts it reached before the throw are what the report shows, never lost.
|
|
331
|
+
outcomes.push({
|
|
332
|
+
id: transform.id,
|
|
333
|
+
status: "failed",
|
|
334
|
+
message: errorMessage(error),
|
|
335
|
+
counts: frameCounts(received, reported),
|
|
336
|
+
});
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const clipped = clipResult(next, current, width);
|
|
341
|
+
outcomes.push({
|
|
342
|
+
id: transform.id,
|
|
343
|
+
status: clipped === undefined ? "skipped" : "applied",
|
|
344
|
+
counts: frameCounts(received, reported),
|
|
345
|
+
});
|
|
346
|
+
if (clipped !== undefined) current = clipped;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
pipelineState().lastFrame = outcomes;
|
|
350
|
+
return current;
|
|
351
|
+
}
|