pi-braid 0.43.9 → 0.43.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,234 @@
1
+ /**
2
+ * Owner speech: the braid half of the ruling write path.
3
+ *
4
+ * Two jobs, both of them specific to this deployment:
5
+ *
6
+ * - **Decide whether a turn is the owner speaking at all.** The corpus holds
7
+ * every user-role turn, and in this transcript tree that includes the
8
+ * system's own voice: dispatch prompts the seat writes to a running agent,
9
+ * and whisper envelopes from braid's subsystems. A directive-language
10
+ * classifier over that input files the seat's own brief text as an owner
11
+ * ruling, with a pointer into a worker session - a store that looks like
12
+ * provenance and is self-quotation. A telegram envelope is the exception:
13
+ * its body is the owner's message in a transport wrapper, so the wrapper is
14
+ * stripped and the body is what gets classified.
15
+ * - **File a ruling in braid's store.** The label stamped into `source`, the
16
+ * pointer shape, the duplicate key and the fixed tags are braid's filing
17
+ * convention, not English grammar, so they live here and the script that
18
+ * writes them carries no domain string of its own.
19
+ *
20
+ * The classification itself is `classifyRulingBody()` in the generic layer.
21
+ * This module supplies the vocabulary that layer cannot know - what a ruling
22
+ * can be about, and which words describe braid's own machinery - and removes
23
+ * the envelope before it ever reaches the grammar.
24
+ *
25
+ * Pure by construction: no I/O, no clock, no randomness.
26
+ */
27
+ import { classifyRulingBody, } from "../memory/decision-grammar.js";
28
+ /**
29
+ * The label every filed ruling carries at the front of `source`, and the only
30
+ * handle a later run greps for. It is a constant here because the store's
31
+ * retention policy keys on the same prefix: a second spelling would be a class
32
+ * of entry the prune could not see.
33
+ */
34
+ export const OWNER_RULING_LABEL = "owner ruling";
35
+ /**
36
+ * A whisper envelope header. The transport line is not speech: for a telegram
37
+ * envelope the body beneath it is the owner's message, and for every other
38
+ * subsystem it is braid talking to itself.
39
+ */
40
+ const WHISPER_HEADER = /^\s*\[braid-whisper - ([a-z0-9-]+)(?: @ [^\]]*)?\](.*)$/i;
41
+ /** The envelope's closing marker. */
42
+ const WHISPER_END = /^\s*\[braid-whisper-end\]\s*$/i;
43
+ /**
44
+ * Transport lines inside a telegram envelope: the sender line, and the
45
+ * sub-header a coalesced batch puts above each message. Neither is the owner's
46
+ * words.
47
+ */
48
+ const TELEGRAM_TRANSPORT = /^\s*(?:from @\S+ .*in chat \d+\S*|\[\d+\/\d+\s@ [^\]]*\](?:\s+from @\S+.*)?|-{3,})$/i;
49
+ /**
50
+ * How a dispatch prompt opens. These are turns the seat wrote to a running
51
+ * agent; they carry imperatives because that is what a dispatch is.
52
+ */
53
+ const SEAT_OPENER = /^\s*(?:you are\b|your (?:branch|task|brief|report|cwd|job|pane|mission)\b|read your brief in full|read the brief\b|read (?:the )?(?:\/|~|[\w-]+\.md)|begin backlog item|your task:|mission:|task:|authori[sz]ed (?:execution )?brief|re-?read\b|acknowledge\b|this is a fresh session|first: )/i;
54
+ /**
55
+ * Words a dispatch prompt tends to carry: the seat's own shorthands, which the
56
+ * owner does not use when he states a ruling. One in a turn is enough, because
57
+ * none of them is owner vocabulary.
58
+ */
59
+ const SEAT_ANATOMY = [
60
+ /\bAUTHORITATIVE BRIEF\b/i,
61
+ /\bAUTHORISED BRIEF\b/i,
62
+ /\bresult block\b/i,
63
+ /check-result\.mjs/,
64
+ /\bConfirm receipt\b/i,
65
+ /\bPI_SESSION_FILE\b/i,
66
+ /\bEnd it with the result block\b/i,
67
+ /\bDispatch facts\b/i,
68
+ /\bRead your brief\b/i,
69
+ ];
70
+ /** Pasted-brief structure: headings that only a dispatch prompt carries. */
71
+ const BRIEF_HEADINGS = /^\s*#{2,}\s/m;
72
+ /** Sends a turn from the seat to a running agent. */
73
+ const STEER_OPENER = /^\s*(?:steer(?:,| from| to|:)|seat to |\[agent:|dispatch(?:ed)?:|correction to|status check|reminder:)/i;
74
+ /**
75
+ * Session machinery. A message about a timer, a poller, a pane or a
76
+ * predecessor's transcript is braid talking to a process; the owner states
77
+ * rules about how braid behaves, not about which lock file the previous
78
+ * occupant held.
79
+ */
80
+ const SESSION_MACHINERY = /\b(?:timer|poller|polling|pane|worktree|predecessor|successor|lock (?:file|PID)|transcript|session file|placement section|check-result|result block|ci:check)\b/i;
81
+ /**
82
+ * What a ruling is about when it does not open with the owner's own voice: the
83
+ * system, its agents, its tools. A bare directive with none of this and no
84
+ * first person ("Do not read or write files.") is an instruction aimed at the
85
+ * reader, not a decision about braid.
86
+ */
87
+ const SYSTEM_SCOPE = /\b(?:braid|agent|agents|worker|workers|writer|writers|reader|readers|seat|seats|system|prompt|memory|store|skill|skills|tool|tools|model|models|subscription|subscri\w*|session|sessions|orchestrator|identity|layer|layers|gate|gates|report|reports|brief|briefs|docs?|telegram|context|queue|pipeline|repo|repository|codebase|website|app|product)\b/i;
88
+ /**
89
+ * Explicit generalization. A rule aimed at any instance is a rule; one aimed
90
+ * at the agent reading it is a steer, and the escape hatch has to be stated.
91
+ */
92
+ const GENERALIZED = /\b(?:any (?:instance|agent|worker|seat|one)|every (?:agent|instance|worker|seat)|generically?|whichever|regardless)\b/i;
93
+ /**
94
+ * The vocabulary this deployment hands the generic grammar. It is the only
95
+ * braid-specific input `classifyRulingBody()` takes, and it is a value rather
96
+ * than a parameter chain so a fixture can classify the same body with and
97
+ * without an envelope and compare.
98
+ */
99
+ export const BRAID_VOCABULARY = {
100
+ namesSubject: (text) => SYSTEM_SCOPE.test(text),
101
+ talksProcess: (text) => SESSION_MACHINERY.test(text),
102
+ generalizes: (text) => GENERALIZED.test(text),
103
+ };
104
+ /**
105
+ * The owner's words inside a turn, or `null` when the turn is not the owner's
106
+ * speech.
107
+ *
108
+ * Stripping happens line by line, and the kept lines are the original lines, so
109
+ * a quote later cut from the body is still a contiguous slice of the stored
110
+ * turn - the pointer resolves either way. A turn that carries any non-telegram
111
+ * envelope is refused whole: a subsystem whisper and an owner tail can share
112
+ * one turn, and no mechanical rule here can say which sentence is whose.
113
+ */
114
+ function ownerSpeech(body) {
115
+ const lines = body.split("\n");
116
+ const headers = lines.map((line) => WHISPER_HEADER.exec(line)).filter(Boolean);
117
+ if (headers.length > 0) {
118
+ const foreign = headers.find((header) => header[1].toLowerCase() !== "telegram");
119
+ if (foreign)
120
+ return null;
121
+ }
122
+ if (headers.length === 0)
123
+ return body;
124
+ const kept = [];
125
+ let inside = false;
126
+ for (const line of lines) {
127
+ if (WHISPER_HEADER.test(line)) {
128
+ inside = true;
129
+ continue;
130
+ }
131
+ if (WHISPER_END.test(line)) {
132
+ inside = false;
133
+ continue;
134
+ }
135
+ if (inside && TELEGRAM_TRANSPORT.test(line))
136
+ continue;
137
+ if (inside)
138
+ kept.push(line);
139
+ }
140
+ const stripped = kept.join("\n").trim();
141
+ return stripped || null;
142
+ }
143
+ /** Is this turn a dispatch prompt rather than the owner speaking? */
144
+ function looksLikeSeatPrompt(body) {
145
+ const firstLine = body.split("\n").find((line) => line.trim()) ?? "";
146
+ if (SEAT_OPENER.test(firstLine))
147
+ return true;
148
+ if (STEER_OPENER.test(firstLine))
149
+ return true;
150
+ for (const pattern of SEAT_ANATOMY)
151
+ if (pattern.test(body))
152
+ return true;
153
+ return BRIEF_HEADINGS.test(body) && /\bbrief\b/i.test(body);
154
+ }
155
+ /**
156
+ * Classify one user turn.
157
+ *
158
+ * Refuses everything that is not owner speech before the grammar runs, then
159
+ * hands the body to the generic classifier with this deployment's vocabulary,
160
+ * and stamps the provenance the pointer and the duplicate key are built from.
161
+ */
162
+ export function classifyOwnerRuling(turn) {
163
+ if (!turn || typeof turn.text !== "string")
164
+ return null;
165
+ const raw = turn.text.trim();
166
+ if (!raw)
167
+ return null;
168
+ const speech = ownerSpeech(raw);
169
+ if (!speech)
170
+ return null;
171
+ if (looksLikeSeatPrompt(speech))
172
+ return null;
173
+ const shape = classifyRulingBody(speech, BRAID_VOCABULARY);
174
+ if (!shape)
175
+ return null;
176
+ return {
177
+ class: shape.class,
178
+ quote: shape.quote,
179
+ session: turn.session,
180
+ session_id: turn.session_id,
181
+ project: turn.project,
182
+ ts: turn.ts,
183
+ line: turn.line,
184
+ };
185
+ }
186
+ /**
187
+ * The pointer the store carries in `source`, in one fixed shape:
188
+ * `owner ruling: <session path>:<line> (<iso ts>, <project>)`.
189
+ *
190
+ * Greppable by prefix, and it resolves: the path and line address the turn the
191
+ * quote came from, so `sed -n '<line>p' '<session path>'` prints the words.
192
+ */
193
+ export function rulingPointer(candidate) {
194
+ return `${OWNER_RULING_LABEL}: ${candidate.session}:${candidate.line} (${candidate.ts}, ${candidate.project})`;
195
+ }
196
+ /**
197
+ * The pointer as a pattern, so a caller that scans a store for filed rulings
198
+ * reads the label from the same place it writes it instead of spelling it a
199
+ * second time. Groups: 1 the session path, 2 the line number.
200
+ */
201
+ export function rulingPointerPattern() {
202
+ const escaped = OWNER_RULING_LABEL.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
203
+ return new RegExp(`^${escaped}: (.+):(\\d+) \\(`);
204
+ }
205
+ /**
206
+ * The duplicate key: one turn is one ruling, so `<session_id>:<line>` names it
207
+ * whether the entry was written by this script, by hand, or by a later run.
208
+ */
209
+ export function rulingDuplicateKey(candidate) {
210
+ return `${candidate.session_id}:${candidate.line}`;
211
+ }
212
+ /**
213
+ * A project label as a tag token: lowercase alphanumerics, single hyphens, no
214
+ * leading/trailing hyphen. `projects/agents/pi-braid` becomes
215
+ * `projects-agents-pi-braid`, which is greppable and cannot collide with a
216
+ * different project's label through punctuation alone.
217
+ */
218
+ export function projectTag(label) {
219
+ const slug = label
220
+ .toLowerCase()
221
+ .replace(/[^a-z0-9]+/g, "-")
222
+ .replace(/^-+|-+$/g, "");
223
+ return slug || "unknown-project";
224
+ }
225
+ /**
226
+ * The fixed handles every filed ruling carries: the class a reader filters on,
227
+ * and the project it came from. The quote's own words are added beside these by
228
+ * `deriveTopicTags()`, because a handle names the entry while a topic word is
229
+ * what a later question arrives through.
230
+ */
231
+ export function rulingBaseTags(candidate) {
232
+ return ["owner-ruling", candidate.class, projectTag(candidate.project)];
233
+ }
234
+ //# sourceMappingURL=owner-speech.js.map
package/docs/LOCAL_DEV.md CHANGED
@@ -138,7 +138,7 @@ before and after a full run. A non-zero delta is the leak this rule prevents.
138
138
  ### New fixtures: use `node:test`
139
139
 
140
140
  A new fixture is written on `node:test`. The runner does not care which style a
141
- file uses - 94 of the core suite's 329 fixtures already use `node:test` - so the
141
+ file uses - 97 of the core suite's 332 fixtures already use `node:test` - so the
142
142
  reason is the failure the other style produces. A hand-rolled helper that calls
143
143
  `fn()` without awaiting it discards the case's promise, so an `async` case runs
144
144
  detached and the file can print its green tally before that case settles, with
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-braid",
3
- "version": "0.43.9",
3
+ "version": "0.43.11",
4
4
  "description": "🥨 Braid - Metacognitive extension suite for pi coding agent: Adds memory, plan tracking, structured cognitive signals and completion checks on top of pi core loop, so long sessions keep their context and stop claiming unfinished work.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -64,8 +64,8 @@ export function skillsCatalogRule(readRoot: string, grouped = false): string {
64
64
  return `This catalog is every installed skill: one row per skill, name and description.${readPath} When the task enters a listed skill's domain, read the WHOLE SKILL.md before acting in that domain; never act from memory of a skill's contents.`;
65
65
  }
66
66
 
67
- /** Paragraph 2: the pointer rule (pinned verbatim by test-prompt-budget.mjs). */
68
- export const SKILLS_POINTER_RULE = `Pointer rule: the catalog one-liner is metadata, not content — the SKILL.md path is the knowledge. The prompt already covering the domain is the only reason to skip the read.`;
67
+ /** Paragraph 2: the pointer rule, exported so a fixture can pin its text. */
68
+ export const SKILLS_POINTER_RULE = `Pointer rule: the catalog one-liner is metadata, not content — the SKILL.md path is the knowledge. If the body itself is already in this prompt you have read it; if you have only a description or an excerpt, you have not.`;
69
69
 
70
70
  const MECHANICAL_TRIGGER_LEAD = `Mechanical trigger, no judgment call: about to use a tool family whose word appears in a skill's name`;
71
71
  const MECHANICAL_TRIGGER_TAIL = `→ read that SKILL.md before the first call in the family.`;
@@ -0,0 +1,405 @@
1
+ /**
2
+ * Decision grammar: the generic half of the ruling write path.
3
+ *
4
+ * One body of text in, either the standing instruction it carries or `null`.
5
+ * "Generic" is a claim about what the module is allowed to know: sentence
6
+ * spans, the marker table that turns a sentence into a standing instruction,
7
+ * the shapes that are not instructions (a question, a status report, a paste, a
8
+ * hedge, a conversational aside), and the first- and second-person grammar of
9
+ * English. It knows nothing about who wrote the text, which channel carried it,
10
+ * or what the caller's world is made of.
11
+ *
12
+ * The last of those is the one thing a grammar cannot supply, so the caller
13
+ * hands it in as `GrammarVocabulary`: which words name a subject a ruling can be
14
+ * about, which words describe the caller's own machinery, and how an explicit
15
+ * generalization reads. `classifyRulingBody()` applies those three tests at the
16
+ * same two places it applies its own - the turn and the sentence that gets
17
+ * quoted - so a directive aimed at the reader is told apart from a rule about
18
+ * the system by vocabulary this layer never spells.
19
+ *
20
+ * The second half of the module is the store side of the same write path:
21
+ * `deriveTopicTags()` reads a quote and the population it is filed into and
22
+ * returns the words worth carrying as tags. The rule reasons about tokens,
23
+ * document frequency and a generic English stop list, so it belongs next to the
24
+ * grammar rather than in the caller's vocabulary.
25
+ *
26
+ * Pure by construction: no I/O, no clock, no randomness, so its behaviour is a
27
+ * fixture instead of a story.
28
+ */
29
+
30
+ import { QUERY_STOP_WORDS } from "./constants.js";
31
+
32
+ /** The closed set of ruling shapes the classifier can name. */
33
+ export type RulingClass = "prohibition" | "preference" | "requirement" | "policy";
34
+
35
+ /** A classified body: what kind of instruction it is, and the words to keep. */
36
+ export interface RulingShape {
37
+ class: RulingClass;
38
+ /** A contiguous, verbatim slice of the body, capped with a trailing ellipsis. */
39
+ quote: string;
40
+ }
41
+
42
+ /**
43
+ * The three questions a grammar has to ask the caller's world.
44
+ *
45
+ * `namesSubject` is the escape hatch for a bare directive: "Do not read or
46
+ * write files." names no system, so without first person it is an instruction
47
+ * aimed at the reader, not a rule about anything. `talksProcess` refuses a body
48
+ * whose subject is the caller's own machinery rather than the thing a ruling is
49
+ * about, and `generalizes` admits a second-person body that says it speaks to
50
+ * every recipient ("any instance", "generically"), which is a rule rather than
51
+ * a steer.
52
+ */
53
+ export interface GrammarVocabulary {
54
+ namesSubject(text: string): boolean;
55
+ talksProcess(text: string): boolean;
56
+ generalizes(text: string): boolean;
57
+ }
58
+
59
+ /** Longest quote the caller receives; past this the quote is cut, not summarized. */
60
+ export const QUOTE_LIMIT = 400;
61
+
62
+ /**
63
+ * A body longer than this is a pasted brief, a log or a tool dump, not the
64
+ * sentence an owner states a ruling in. The quote is capped at QUOTE_LIMIT
65
+ * anyway, so a long body can only contribute a fragment whose subject was three
66
+ * paragraphs earlier.
67
+ */
68
+ const MAX_TURN_CHARS = 1200;
69
+
70
+ /** Below this a body is an acknowledgement ("ok", "proceed"), never a ruling. */
71
+ const MIN_TURN_WORDS = 3;
72
+
73
+ /** A continuation sentence is kept only while the quote stays short enough to read. */
74
+ const CONTINUATION_LIMIT = 200;
75
+
76
+ /** The owner's own voice: first person, singular or collective. */
77
+ const FIRST_PERSON = /\b(?:i|i'm|i've|i'll|i'd|we|we're|we've|we'll|we'd|my|our|ours|let's)\b/i;
78
+
79
+ /**
80
+ * Second-person address. A directive written to *you* is an instruction for
81
+ * the reader; a rule for the system talks about it, or generalizes explicitly.
82
+ */
83
+ const SECOND_PERSON = /\b(?:you|your|yours|yourself|yourselves)\b/i;
84
+
85
+ /**
86
+ * First-person report voice: the speaker narrating what was seen, changed or
87
+ * disabled. Such a sentence can carry "always" or "no longer" by accident ("I
88
+ * still see only ...", "it no longer indexed"), so it is refused unless it opens
89
+ * with a directive.
90
+ */
91
+ const REPORT_VOICE =
92
+ /\b(?:i|we)\b[^.!?]{0,120}\b(?:noticed|noticing|see|saw|seen|disabled|enabled|changed|updated|merged|pushed|committed|created|deleted|found|think|thought|guess|believe|realised|realized|checked|ran|built|fixed|added|removed|renamed|replaced|read|told|said|mentioned)\b/i;
93
+
94
+ /**
95
+ * Conversational openers that carry no decision: thinking aloud. A marker
96
+ * inside one of these ("doesn't really matter") is not a ruling.
97
+ */
98
+ const CONVERSATIONAL =
99
+ /^\s*(?:or i don'?t know|i don'?t know|not sure|doesn'?t really matter|whatever|never mind|nevermind)\b/i;
100
+
101
+ /** A body that reads as a report of work already done, on its first line. */
102
+ const STATUS_OPENER =
103
+ /^(?:done|finished|merged|shipped|pushed|committed|built|fixed|updated|completed|released|deployed|landed|green|all (?:tests|gates|checks)|tests? (?:pass|green)|gates? (?:pass|green))/i;
104
+
105
+ /**
106
+ * Hedged advice: the shape of a recommendation that was floated and not
107
+ * adopted. `should` and `could` are deliberately absent from the durable
108
+ * marker table below - they are how advice is phrased, not how a ruling is
109
+ * stated - so a hedged sentence has to be refused explicitly.
110
+ */
111
+ const HEDGE =
112
+ /\b(?:maybe|perhaps|might|could|would|possibly|probably|consider|i think|i guess|not sure|unsure|what if|why not|we should probably)\b/i;
113
+
114
+ /**
115
+ * Absolute frames that make a hedged sentence a ruling anyway: "maybe we should
116
+ * never post through an API" has still ruled, while "we could consider APIs"
117
+ * has not.
118
+ */
119
+ const ABSOLUTE =
120
+ /\b(?:never|must|always|only one|required|mandatory|forbidden|banned|not allowed|by default|from now on|going forward)\b/i;
121
+
122
+ /**
123
+ * Opening words that make a sentence a question, used only together with a
124
+ * question mark: the mark alone is enough at the end of a body, but a body that
125
+ * opens with a question and closes with a ruling keeps its ruling.
126
+ */
127
+ const INTERROGATIVE_OPENER = /^\s*(?:how|what|why|when|where|which|who|whose|whether)\b/i;
128
+
129
+ const QUESTION_OPENER =
130
+ /^(?:should|shall|can|could|may|might|do|does|did|is|are|was|were|will|would|have|has|how|what|why|when|where|which|who|whose|am|if)\b/i;
131
+
132
+ /**
133
+ * Decision frames, in class-priority order. A frame is the grammar of a
134
+ * standing instruction rather than of this turn's work: an absolute (`never`,
135
+ * `always`, `must`, `only one`), a comparison between ways of doing something
136
+ * (`prefer`, `instead of`, `rather than`, `better than`), or a prescription
137
+ * stated in the collective voice (`we need to`, `should`). Bare imperatives are
138
+ * deliberately absent: "investigate X" is a task for this turn, and a table
139
+ * built from every imperative is a task list filed as policy.
140
+ */
141
+ const MARKERS: ReadonlyArray<{ class: RulingClass; pattern: RegExp }> = [
142
+ {
143
+ class: "prohibition",
144
+ pattern:
145
+ /\b(?:never|don't|do not|must not|may not|not allowed|forbidden|banned|no longer|stop (?:using|doing|running|adding|writing|posting|editing|treating))\b/i,
146
+ },
147
+ {
148
+ class: "preference",
149
+ pattern: /\b(?:prefer|preferably|instead of|rather than|better than|avoid)\b/i,
150
+ },
151
+ {
152
+ class: "requirement",
153
+ pattern: /\b(?:must|should|always|required|mandatory|only one|need(?:s)? to|have to|has to)\b/i,
154
+ },
155
+ {
156
+ class: "policy",
157
+ pattern:
158
+ /\b(?:by default|default is|from now on|going forward|rule is|policy|standard|convention|principle)\b/i,
159
+ },
160
+ ];
161
+
162
+ /** A sentence span over the original text; every quote is a slice of one. */
163
+ interface Span {
164
+ start: number;
165
+ end: number;
166
+ }
167
+
168
+ /** Abbreviations whose period does not end a sentence. */
169
+ const ABBREVIATION = /(?:^|[\s(])(?:e\.g|i\.e|etc|vs|cf|approx|resp|no|dr|mr|mrs|ms|st)\.$/i;
170
+
171
+ /**
172
+ * Split a body into contiguous sentence spans.
173
+ *
174
+ * The spans are offsets into the original string, so a quote assembled from
175
+ * them is a byte-for-byte slice of the words - the property the pointer check
176
+ * leans on. Newlines end a sentence; a period does not end one inside a known
177
+ * abbreviation, a number (`3.5`), or a path (`decision-grammar.ts`).
178
+ */
179
+ function sentenceSpans(text: string): Span[] {
180
+ const spans: Span[] = [];
181
+ let start = 0;
182
+ const push = (end: number): void => {
183
+ if (end > start) {
184
+ const slice = text.slice(start, end);
185
+ if (slice.trim()) spans.push({ start, end });
186
+ }
187
+ start = end;
188
+ };
189
+ for (let i = 0; i < text.length; i++) {
190
+ const ch = text[i];
191
+ if (ch === "\n") {
192
+ push(i);
193
+ continue;
194
+ }
195
+ if (ch !== "." && ch !== "!" && ch !== "?") continue;
196
+ const next = text[i + 1];
197
+ if (next !== undefined && !/\s/.test(next)) continue;
198
+ if (ch === "." && ABBREVIATION.test(text.slice(Math.max(0, i - 12), i + 1))) continue;
199
+ if (ch === "." && /\d$/.test(text[i - 1] ?? "") && /^\d/.test(next ?? "")) continue;
200
+ push(i + 1);
201
+ }
202
+ push(text.length);
203
+ return spans;
204
+ }
205
+
206
+ /** Words in a string, counted the way a reader would. */
207
+ function wordCount(text: string): number {
208
+ return text.split(/\s+/).filter(Boolean).length;
209
+ }
210
+
211
+ /**
212
+ * Is the body a paste - a shell transcript, a code block, a log or a payload?
213
+ *
214
+ * A pasted command is the single most common false ruling: it carries
215
+ * imperatives ("rm -rf"), digits, and often the word "only". The screens are
216
+ * cheap because a ruling sentence is prose, and prose does not start every line
217
+ * with a prompt, a box, or a stack frame.
218
+ */
219
+ function looksPasted(text: string): boolean {
220
+ if (text.includes("```")) return true;
221
+ if (/^\s*[{[]/.test(text)) return true;
222
+ if (/^\s*(?:\$|%|> |#!)\s/m.test(text)) return true;
223
+ if (/(?:^|\n)\s*(?:[│├└─]|\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2})/m.test(text)) return true;
224
+ if (/Traceback \(most recent call last\)|\bat [\w$.]+ \([\w/.-]+:\d+:\d+\)/.test(text))
225
+ return true;
226
+ const lines = text.split("\n").filter((line) => line.trim());
227
+ const commandLines = lines.filter((line) =>
228
+ /^\s*(?:sudo |git |npm |npx |node |python3? |cd |ls |cat |rg |grep |curl |chmod |brew |make |docker |kubectl |export |rm )/.test(
229
+ line,
230
+ ),
231
+ );
232
+ if (commandLines.length >= 2) return true;
233
+ // A payload of paths and key=value rows, with almost no sentence structure.
234
+ if (lines.length >= 6 && lines.every((line) => line.length < 200)) {
235
+ const payloadish = lines.filter((line) => /^[\w./-]{2,}\s*[:=]\s*\S/.test(line));
236
+ if (payloadish.length >= lines.length / 2) return true;
237
+ }
238
+ return false;
239
+ }
240
+
241
+ /** Does the body end in a question, or open with one and carry nothing after it? */
242
+ function isQuestionShaped(text: string, spans: Span[]): boolean {
243
+ const trimmed = text.trimEnd();
244
+ if (trimmed.endsWith("?")) return true;
245
+ const first = spans[0] ? text.slice(spans[0].start, spans[0].end).trim() : "";
246
+ return QUESTION_OPENER.test(first) && first.includes("?") && spans.length === 1;
247
+ }
248
+
249
+ /**
250
+ * The class a sentence marks, or `null` when it marks no standing instruction.
251
+ * First match in table order wins, so "must never" reads as a prohibition and
252
+ * "always prefer" as a preference.
253
+ */
254
+ function markerClass(sentence: string): RulingClass | null {
255
+ for (const { class: kind, pattern } of MARKERS) {
256
+ if (pattern.test(sentence)) return kind;
257
+ }
258
+ return null;
259
+ }
260
+
261
+ /**
262
+ * Assemble the quote: the sentence that carries the marker, plus its immediate
263
+ * continuation while that stays short. Contiguous, never reordered, never
264
+ * stitched from two places in the body.
265
+ */
266
+ function quoteFrom(text: string, spans: Span[], markerIndex: number): string {
267
+ let end = spans[markerIndex].end;
268
+ const next = spans[markerIndex + 1];
269
+ if (next) {
270
+ const candidate = text.slice(spans[markerIndex].start, next.end);
271
+ if (candidate.length <= CONTINUATION_LIMIT) end = next.end;
272
+ }
273
+ const quote = text.slice(spans[markerIndex].start, end).trim();
274
+ if (quote.length <= QUOTE_LIMIT) return quote;
275
+ return `${quote.slice(0, QUOTE_LIMIT).trimEnd()}...`;
276
+ }
277
+
278
+ /**
279
+ * Classify one body that is already known to be owner speech.
280
+ *
281
+ * Returns `null` for everything that is not a standing instruction: a question,
282
+ * a paste, a status report, a hedged recommendation, an acknowledgement, a
283
+ * second-person directive, or a body too long to cut a self-contained sentence
284
+ * from. Returns the class and the verbatim quote otherwise. The caller owns the
285
+ * provenance fields, because the caller is the one that knows where the text
286
+ * came from.
287
+ */
288
+ export function classifyRulingBody(
289
+ text: string,
290
+ vocabulary: GrammarVocabulary,
291
+ ): RulingShape | null {
292
+ if (typeof text !== "string") return null;
293
+ const body = text.trim();
294
+ if (!body) return null;
295
+ if (wordCount(body) < MIN_TURN_WORDS) return null;
296
+ if (body.length > MAX_TURN_CHARS) return null;
297
+ if (looksPasted(body)) return null;
298
+
299
+ const spans = sentenceSpans(body);
300
+ if (spans.length === 0) return null;
301
+ if (isQuestionShaped(body, spans)) return null;
302
+
303
+ const firstSentence = body.slice(spans[0].start, spans[0].end).trim();
304
+ if (STATUS_OPENER.test(firstSentence)) return null;
305
+ if (INTERROGATIVE_OPENER.test(firstSentence)) return null;
306
+
307
+ const firstPerson = FIRST_PERSON.test(body);
308
+ if (SECOND_PERSON.test(body) && !vocabulary.generalizes(body)) return null;
309
+ if (!firstPerson && !vocabulary.namesSubject(body)) return null;
310
+ if (!firstPerson && vocabulary.talksProcess(body)) return null;
311
+
312
+ for (let index = 0; index < spans.length; index++) {
313
+ const sentence = body.slice(spans[index].start, spans[index].end).trim();
314
+ const kind = markerClass(sentence);
315
+ if (!kind) continue;
316
+ if (STATUS_OPENER.test(sentence)) continue;
317
+ if (CONVERSATIONAL.test(sentence)) continue;
318
+ if (sentence.includes("?")) continue;
319
+ if (SECOND_PERSON.test(sentence) && !vocabulary.generalizes(sentence)) continue;
320
+ // The same voice test the body passed, applied to the sentence that will
321
+ // be quoted: a bare directive lifted out of a longer message is an
322
+ // instruction for the reader, not a decision about the system.
323
+ const sentenceFirstPerson = FIRST_PERSON.test(sentence);
324
+ if (!sentenceFirstPerson && !vocabulary.namesSubject(sentence)) continue;
325
+ if (!sentenceFirstPerson && vocabulary.talksProcess(sentence)) continue;
326
+ if (
327
+ REPORT_VOICE.test(sentence) &&
328
+ !/^\s*(?:never|always|prefer|must not|by default|from now on)/i.test(sentence)
329
+ ) {
330
+ continue;
331
+ }
332
+ if (HEDGE.test(sentence) && !ABSOLUTE.test(sentence)) continue;
333
+ const quote = quoteFrom(body, spans, index);
334
+ if (!quote) continue;
335
+ return { class: kind, quote };
336
+ }
337
+ return null;
338
+ }
339
+
340
+ // ── Tag derivation ──────────────────────────────────────────────────────────
341
+
342
+ /** How many tags a ruling carries. */
343
+ export const TAG_LIMIT = 3;
344
+
345
+ /** Shortest token a tag may be cut from. */
346
+ const TAG_TOKEN_MIN_LENGTH = 4;
347
+
348
+ /**
349
+ * The share of the population past which a token names nothing. A word a
350
+ * quarter of the store already carries is the store's background, not a topic:
351
+ * it would fire on every query and add nothing a reader could act on.
352
+ */
353
+ const TAG_POPULATION_SHARE = 0.25;
354
+
355
+ /** Tokens a tag may be cut from: letters, then letters/digits/hyphens. */
356
+ const TAG_TOKEN = /[a-z][a-z0-9-]{3,}/g;
357
+
358
+ /**
359
+ * The population a quote is filed into, as the frequency test sees it:
360
+ * how many documents it holds, and how many of them carry a token.
361
+ */
362
+ export interface TagPopulation {
363
+ /** Documents in the population. */
364
+ total: number;
365
+ /** Documents carrying `token`. */
366
+ count(token: string): number;
367
+ }
368
+
369
+ /**
370
+ * The tags a quote earns from the population it is filed into.
371
+ *
372
+ * Take the quote's tokens of four characters or more, drop the read path's stop
373
+ * words, drop any token more than a quarter of the population carries, and keep
374
+ * the three the population carries most, ties by first occurrence.
375
+ *
376
+ * The ranking is by frequency and not by rarity, and the reason is the read
377
+ * path's own arithmetic: a tag's bonus is a flat constant however rare the
378
+ * token is, so rarity buys nothing a later query will say. Two tokens of equal
379
+ * rarity are worth the same as tags, and the one a question is likelier to name
380
+ * is the one the population's own prose uses more often. That makes the
381
+ * frequency ranking the findability ranking, and it leaves the 25% cap as the
382
+ * only guard against a tag that names nothing.
383
+ */
384
+ export function deriveTopicTags(
385
+ text: string,
386
+ population: TagPopulation,
387
+ limit: number = TAG_LIMIT,
388
+ ): string[] {
389
+ const ceiling = population.total * TAG_POPULATION_SHARE;
390
+ const seen = new Set<string>();
391
+ const candidates: Array<{ token: string; count: number; first: number }> = [];
392
+ for (const token of String(text ?? "")
393
+ .toLowerCase()
394
+ .match(TAG_TOKEN) ?? []) {
395
+ if (token.length < TAG_TOKEN_MIN_LENGTH) continue;
396
+ if (QUERY_STOP_WORDS.has(token)) continue;
397
+ if (seen.has(token)) continue;
398
+ seen.add(token);
399
+ const count = population.count(token);
400
+ if (count > ceiling) continue;
401
+ candidates.push({ token, count, first: candidates.length });
402
+ }
403
+ candidates.sort((a, b) => b.count - a.count || a.first - b.first);
404
+ return candidates.slice(0, limit).map((candidate) => candidate.token);
405
+ }