piroboros 0.0.0-stage → 0.2.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.
Files changed (118) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/LICENSE +21 -0
  3. package/NOTICE +39 -0
  4. package/README.md +140 -2
  5. package/agents/astrelia.md +31 -0
  6. package/agents/avaritia.md +31 -0
  7. package/agents/elaria.md +31 -0
  8. package/agents/nytheria.md +32 -0
  9. package/agents/raskreia.md +32 -0
  10. package/agents/samaela.md +40 -0
  11. package/agents/vaelia.md +35 -0
  12. package/agents/vesperia.md +45 -0
  13. package/bin/piroboros.mjs +17 -0
  14. package/dist/auth.js +105 -0
  15. package/dist/base.js +19 -0
  16. package/dist/chat/budget.js +87 -0
  17. package/dist/chat/compactloop.js +63 -0
  18. package/dist/chat/compactor.js +530 -0
  19. package/dist/chat/extension.js +36 -0
  20. package/dist/chat/hook.js +40 -0
  21. package/dist/chat/log.js +317 -0
  22. package/dist/chat/outbound.js +114 -0
  23. package/dist/chat/plan.js +64 -0
  24. package/dist/chat/prompt.js +115 -0
  25. package/dist/chat/status.js +37 -0
  26. package/dist/chat/stuck.js +83 -0
  27. package/dist/chat/tools.js +163 -0
  28. package/dist/chat/tree.js +73 -0
  29. package/dist/chat/turn.js +305 -0
  30. package/dist/chat/view.js +163 -0
  31. package/dist/conclave/agents.js +113 -0
  32. package/dist/conclave/pipeline.js +715 -0
  33. package/dist/conclave/report.js +43 -0
  34. package/dist/config.js +614 -0
  35. package/dist/durable.js +177 -0
  36. package/dist/guard.js +346 -0
  37. package/dist/lock.js +56 -0
  38. package/dist/main.js +348 -0
  39. package/dist/memory/akashic/api.js +121 -0
  40. package/dist/memory/akashic/confusables.js +41 -0
  41. package/dist/memory/akashic/scanner.js +67 -0
  42. package/dist/memory/akashic/snapshot.js +31 -0
  43. package/dist/memory/akashic/store.js +1254 -0
  44. package/dist/memory/akashic/tools.js +168 -0
  45. package/dist/memory/akashic/vault.js +984 -0
  46. package/dist/memory/host.js +124 -0
  47. package/dist/memory/mnemosia/adapters/akashic-memories.js +180 -0
  48. package/dist/memory/mnemosia/adapters/contract.js +219 -0
  49. package/dist/memory/mnemosia/agents.js +387 -0
  50. package/dist/memory/mnemosia/brief.js +890 -0
  51. package/dist/memory/mnemosia/claims.js +609 -0
  52. package/dist/memory/mnemosia/conflicts.js +769 -0
  53. package/dist/memory/mnemosia/escalation.js +257 -0
  54. package/dist/memory/mnemosia/feedback.js +108 -0
  55. package/dist/memory/mnemosia/filters.js +248 -0
  56. package/dist/memory/mnemosia/intent.js +221 -0
  57. package/dist/memory/mnemosia/open.js +34 -0
  58. package/dist/memory/mnemosia/orchestrator.js +926 -0
  59. package/dist/memory/mnemosia/plan.js +266 -0
  60. package/dist/memory/mnemosia/policy.js +426 -0
  61. package/dist/memory/mnemosia/progressive.js +550 -0
  62. package/dist/memory/mnemosia/provenance.js +458 -0
  63. package/dist/memory/mnemosia/qualifiers.js +428 -0
  64. package/dist/memory/mnemosia/rank.js +525 -0
  65. package/dist/memory/mnemosia/relations.js +156 -0
  66. package/dist/memory/mnemosia/requirements.js +138 -0
  67. package/dist/memory/mnemosia/rerank.js +253 -0
  68. package/dist/memory/mnemosia/retention.js +174 -0
  69. package/dist/memory/mnemosia/rollback.js +337 -0
  70. package/dist/memory/mnemosia/select.js +607 -0
  71. package/dist/memory/mnemosia/similarity.js +164 -0
  72. package/dist/memory/mnemosia/situation.js +251 -0
  73. package/dist/memory/mnemosia/task.js +418 -0
  74. package/dist/memory/mnemosia/temporal.js +136 -0
  75. package/dist/memory/mnemosia/tokens.js +66 -0
  76. package/dist/memory/mnemosia/tools.js +160 -0
  77. package/dist/memory/mnemosia/trace.js +306 -0
  78. package/dist/memory/mnemosia/types.js +298 -0
  79. package/dist/models.js +360 -0
  80. package/dist/paths.js +103 -0
  81. package/dist/redact.js +79 -0
  82. package/dist/runtime.js +378 -0
  83. package/dist/search-format.js +102 -0
  84. package/dist/search.js +351 -0
  85. package/dist/setup.js +366 -0
  86. package/dist/shell.js +110 -0
  87. package/dist/skills.js +311 -0
  88. package/dist/telemetry.js +84 -0
  89. package/dist/tui/app.js +1011 -0
  90. package/dist/tui/commands.js +121 -0
  91. package/dist/tui/keys.js +83 -0
  92. package/dist/tui/pane.js +387 -0
  93. package/dist/tui/panel.js +60 -0
  94. package/dist/tui/picker.js +25 -0
  95. package/dist/tui/sanitize.js +59 -0
  96. package/dist/tui/settings.js +198 -0
  97. package/dist/tui/setup.js +256 -0
  98. package/dist/tui/theme.js +26 -0
  99. package/dist/tui/thinking/parse.js +1094 -0
  100. package/dist/tui/thinking/select.js +86 -0
  101. package/dist/tui/thinking/state.js +137 -0
  102. package/dist/tui/thinking/types.js +3 -0
  103. package/dist/tui/thinking/view.js +74 -0
  104. package/dist/tui/viewport.js +208 -0
  105. package/dist/undo.js +258 -0
  106. package/dist/web.js +933 -0
  107. package/docs/conclave.md +21 -0
  108. package/docs/configuration.md +48 -0
  109. package/docs/development.md +25 -0
  110. package/docs/interface.md +33 -0
  111. package/docs/memory.md +11 -0
  112. package/docs/privacy.md +13 -0
  113. package/docs/tools.md +20 -0
  114. package/docs/web.md +38 -0
  115. package/models.json.example +34 -0
  116. package/package.json +76 -4
  117. package/settings.json.example +29 -0
  118. package/skills/conclave/SKILL.md +76 -0
@@ -0,0 +1,428 @@
1
+ // Ported from mnemosia/qualifiers.ts @ f0a8ad4 (MIT; see NOTICE)
2
+ /**
3
+ * Branch and environment qualifier extraction (design §2, §11, §17 ambiguity 4; Phase 2).
4
+ *
5
+ * Design §2 resolved a spec/reality contradiction by making `branch` and `environment` **claim
6
+ * qualifiers** rather than storage boundaries: neither foundation has a column for either, so they
7
+ * can only ever be read out of the text a claim is made of. Design §17 ambiguity 4 then deferred the
8
+ * whole thing to Phase 2 "behind a measurement gate before it influences ranking", because the
9
+ * precision of a regex over prose was unknown on this corpus and an unmeasured extractor feeding a
10
+ * ranking signal is a signal made of noise.
11
+ *
12
+ * This module is the extractor. `eval/qualifier-eval.ts` is the measurement, and
13
+ * `QUALIFIER_RANKING` below is the gate it decides.
14
+ *
15
+ * **The rules are shape-constrained, not word-list-constrained.** The obvious rule —
16
+ * `/branch\s+(\w+)/` — reads "the branch is ready" as a branch named `is`, and the obvious repair is
17
+ * a stoplist of verbs that grows every time it is wrong. A stoplist tuned against the corpus it is
18
+ * measured on is a fitted pass, which the Phase 2 brief forbids outright. So every rule below keys
19
+ * on a *shape* the corpus cannot produce by accident: a `remote/name` pair, a `kind/name` pair, a
20
+ * quoted span after the word `branch`, a well-known branch name in a directional position, or the
21
+ * literal word `environment`. Each rule carries an id so a false positive can be attributed to the
22
+ * rule that produced it rather than to "the regex".
23
+ *
24
+ * **Scope qualifiers are never a hard filter.** Design §2 is explicit: they are "used for conflict
25
+ * classification, never hard filtering". `filters.ts` does not import this module, and `scopeKey`
26
+ * already folds the qualifier fields in (types.ts:119-121) so that adding them later cannot silently
27
+ * change a `claimId` that was recorded without them.
28
+ */
29
+ export const QUALIFIER_KINDS = ["branch", "environment"];
30
+ export const QUALIFIER_RULES = [
31
+ "branch_remote_ref",
32
+ "branch_kind_prefix",
33
+ "branch_quoted",
34
+ "branch_directional",
35
+ "branch_named_noun",
36
+ "env_preposition",
37
+ "env_noun",
38
+ "env_node_env",
39
+ "env_platform",
40
+ "env_ci",
41
+ ];
42
+ /** `origin/main` and `main` are the same branch; the remote is not part of the branch's identity. */
43
+ function stripRemote(value) {
44
+ return value.replace(/^(?:origin|upstream|fork)\//u, "");
45
+ }
46
+ /**
47
+ * A filename extension: a dot, then an *alphabetic* first character, then a short alphanumeric tail,
48
+ * at the very end of the string.
49
+ *
50
+ * **Not a blacklist of extensions.** A list of `.ts|.md|.js|…` is exactly the fitted repair this
51
+ * module's header forbids — it would grow by one entry every time the corpus produced a new file
52
+ * type, and each entry would have been added after seeing the failure it fixes. This is a statement
53
+ * about *shape*, and it is the positive half of a claim about branch names: a branch is named after
54
+ * a piece of work, and a piece of work does not have a file type.
55
+ *
56
+ * The leading `[A-Za-z]` is load-bearing in the other direction: `release/2.1` and `feature/v1.2`
57
+ * are branch names whose last component ends in a dotted *number*, and a rule that read `.1` as an
58
+ * extension would reject the very shape git-flow's release branches take.
59
+ */
60
+ const FILENAME_EXTENSION = /\.[A-Za-z][A-Za-z0-9]{0,6}$/u;
61
+ /**
62
+ * Whether a capture is a legal Git branch-ref name rather than a repository path.
63
+ *
64
+ * The predicate is `git check-ref-format`'s grammar, restricted to the characters a branch name
65
+ * actually uses, plus the extension test above. Stated positively, because the Phase 2 measurement
66
+ * showed that the negative form — "a `kind/name` token, unless it looks like a file" — is a rule
67
+ * with no grammar at all:
68
+ *
69
+ * - every `/`-separated component is non-empty, so `docs/` (a directory, 1 of the 23 Phase 2 errors)
70
+ * is not a ref name; git refuses a trailing slash for the same reason;
71
+ * - no component begins with `.` and none ends in `.lock` — both are `check-ref-format` rules;
72
+ * - no `..`, no `@{`, and the whole name ends in neither `/` nor `.`;
73
+ * - the characters are `[A-Za-z0-9._-]`, which excludes every byte `check-ref-format` bans
74
+ * (` ~^:?*[\`, ASCII control) by whitelist rather than by enumeration;
75
+ * - and the final component carries no filename extension.
76
+ *
77
+ * `feature/parser-v2` passes every clause. `test/engine.test.ts` fails the last one, and would fail
78
+ * it even if `test` were still a recognized branch kind — which is what makes this a fix to the
79
+ * grammar rather than a narrowing of a word list.
80
+ */
81
+ export function isBranchRefName(value) {
82
+ if (value.length === 0 || value.length > 64)
83
+ return false;
84
+ if (value.startsWith("/") || value.endsWith("/") || value.endsWith("."))
85
+ return false;
86
+ if (value.includes("//") || value.includes("..") || value.includes("@{"))
87
+ return false;
88
+ const components = value.split("/");
89
+ for (const component of components) {
90
+ if (component.length === 0)
91
+ return false;
92
+ if (component.startsWith("."))
93
+ return false;
94
+ if (component.endsWith(".lock"))
95
+ return false;
96
+ if (!/^[A-Za-z0-9._-]+$/u.test(component))
97
+ return false;
98
+ }
99
+ return !FILENAME_EXTENSION.test(components[components.length - 1] ?? "");
100
+ }
101
+ /**
102
+ * The characters that continue an identifier for the purpose of a *left* boundary.
103
+ *
104
+ * `\b` is the wrong boundary for a token that may be part of a longer hyphenated name. It asks
105
+ * whether the previous character is a word character, and `-` is not one — so `\bfork\/` matches
106
+ * inside the GitHub organisation name `repoe-fork`, and `repoe-fork/poe2` reads as remote `fork` plus
107
+ * branch `poe2`. That single mechanism produced **33 of `branch_remote_ref`'s 36 errors** on the
108
+ * Phase 6 fresh sample, and 33 of that sample's 49 errors overall.
109
+ *
110
+ * The repair is to state the boundary the rule actually needs: a remote name must be a *whole token*,
111
+ * so it may not be preceded by an alphanumeric, an underscore or a hyphen. `.` and `/` are
112
+ * deliberately *not* in the class, and both exclusions are load-bearing in the other direction:
113
+ * `## main...origin/main` is `git status -sb`'s ahead/behind line and `refs/remotes/origin/main` is
114
+ * the canonical remote-tracking path, and both are genuine remote refs that a `\W`-style boundary
115
+ * would have thrown away along with the organisation names.
116
+ */
117
+ const IDENTIFIER_LEFT_BOUNDARY = /[A-Za-z0-9_-]$/u;
118
+ function notInsideIdentifier(text, start) {
119
+ return start === 0 || !IDENTIFIER_LEFT_BOUNDARY.test(text.slice(start - 1, start));
120
+ }
121
+ /**
122
+ * A word that only makes sense if a branch or a ref is under discussion, immediately before the
123
+ * match (whitespace only in between) — or the literal word `branch` immediately after it.
124
+ *
125
+ * **Adjacency is the point, not the vocabulary.** A marker anywhere "nearby" would be satisfied by
126
+ * the ambient vocabulary of a repository conversation, and the errors this is aimed at all sit inside
127
+ * paths and prose that such a window would happily admit. Requiring the marker to be the *previous
128
+ * token* is what refuses `../target/release/attack_poc`, `CI/release/documentation fixes` and
129
+ * `threadweaver-skilltree-release/v2` on the same clause — in each the previous token is `target/`,
130
+ * `CI/` or `skilltree-`, not a word about branches.
131
+ *
132
+ * The vocabulary is git's own porcelain — the commands that take a branch argument — plus the noun
133
+ * itself and the directional prepositions `branch_directional` already uses. It is not a stoplist and
134
+ * it does not grow when a new false positive appears; it is the set of words that put a ref in the
135
+ * sentence.
136
+ */
137
+ const BRANCH_CONTEXT_BEFORE = /(?:^|[^A-Za-z0-9_-])(?:branch(?:es)?|checkout|checked\s+out|switch(?:ed)?|merge[sd]?|merging|rebase[sd]?|rebasing|cherry-pick(?:ed)?|push(?:ed|es)?|pull(?:ed|s)?|fetch(?:ed)?|cut|track(?:s|ed|ing)?|PR|HEAD|on|onto|from|into|to|against)\s+$/iu;
138
+ const BRANCH_CONTEXT_AFTER = /^\s+branch(?:es)?\b/iu;
139
+ function hasBranchContext(text, start, end) {
140
+ return BRANCH_CONTEXT_BEFORE.test(text.slice(0, start)) || BRANCH_CONTEXT_AFTER.test(text.slice(end));
141
+ }
142
+ /**
143
+ * The rules, in evaluation order. Earlier rules claim their character span, so a later rule cannot
144
+ * re-attribute the same text — the same span-claiming discipline `task.ts:extractEntities` uses, and
145
+ * for the same reason: without it `origin/release/1.2` would count as one branch and two, and a
146
+ * per-claim qualifier count would be measuring regex overlap rather than subject matter.
147
+ */
148
+ const RULES = [
149
+ // `origin/main`, `upstream/release/2.1`. A remote name, a slash, and a ref.
150
+ //
151
+ // **CORRECTED AFTER PHASE 6 — measured 158 correct out of 194, and 33 of the 36 errors are one
152
+ // mechanism.** Two corrections, and as with `branch_kind_prefix` each removes a class of error the
153
+ // other does not:
154
+ //
155
+ // 1. **The remote name must be a whole token** (`notInsideIdentifier`). `\b` let `fork` match
156
+ // inside the GitHub organisation name `repoe-fork`, so `repoe-fork/poe2` and every
157
+ // `api.github.com` URL beneath it read as remote `fork` + branch `poe2/…`. That is 33 of the 36.
158
+ // 2. **The capture must parse as a Git ref name**, the same `isBranchRefName` grammar
159
+ // `branch_kind_prefix` already uses, applied here for the first time rather than invented for
160
+ // the occasion. It is what refuses `origin/main..HEAD` and `origin/main...HEAD` — revision
161
+ // *ranges* whose extracted value `main..head` is not the name of anything — and
162
+ // `upstream/.test(line)`, a JavaScript regex call whose capture begins with a dot.
163
+ //
164
+ // What neither correction reaches, stated rather than patched: `origin/allowlist` and
165
+ // `upstream/human` are prose slash-pairs meaning "origin or allowlist", and they are legal ref
166
+ // names preceded by nothing. A rule keyed on shape cannot see the conjunction.
167
+ { kind: "branch", ruleId: "branch_remote_ref", pattern: /(?:origin|upstream|fork)\/([A-Za-z0-9._-][A-Za-z0-9._/-]{0,48})/gu, group: 1, accept: isBranchRefName, context: (text, start) => notInsideIdentifier(text, start) },
168
+ // `feature/parser-v2`, `release/2.1`, `hotfix/oncall`.
169
+ //
170
+ // **CORRECTED AFTER PHASE 2 — measured 0 correct out of 23, and the defect was two conflated
171
+ // vocabularies plus a missing grammar.** The rule read `feature|feat|release|hotfix|bugfix|fix|
172
+ // chore|docs|test|refactor` followed by a slash, and every one of its 23 live hits was a
173
+ // repository path (`test/engine.test.ts`, `docs/operations.md`, `docs/`). Two independent
174
+ // corrections, and each removes a class of error the other does not:
175
+ //
176
+ // 1. **The first component must name a kind of *branch*.** `docs`, `test`, `fix`, `chore` and
177
+ // `refactor` are not branch kinds. `docs` and `test` are the ordinary names of directories —
178
+ // both exist in this repository and in akashic-forge — and `fix`, `chore` and `refactor` are
179
+ // Conventional Commits *types*, a vocabulary that names commits, not branches. The set kept is
180
+ // git-flow's documented branch prefixes (`feature`, `bugfix`, `release`, `hotfix`, `support`)
181
+ // plus the abbreviation `feat`, which is not a directory name in any common source layout.
182
+ // Two different conventions were being read as one, and a word both vocabularies use cannot
183
+ // tell a directory from a branch.
184
+ // 2. **The capture must parse as a Git ref name** (`isBranchRefName`). This is the half that does
185
+ // not depend on the word list: it is what rejects `feature/notes.md`, `docs/` and
186
+ // `release/..\hack`, and it would have rejected 22 of the 23 Phase 2 errors on its own.
187
+ // 3. **A branch must be under discussion** (`hasBranchContext`), added after Phase 6 measured the
188
+ // corrected rule at **0 correct out of 7** on a fresh sample. The Phase 2 failure mode was gone —
189
+ // not one repository path under `test/` or `docs/` survived — and a *different* one was
190
+ // underneath it: `release/security audit` and `CI/release/documentation fixes` are slashes
191
+ // written in prose as conjunctions, `../target/release/attack_poc` is a Rust build path,
192
+ // `threadweaver-skilltree-release/v2` is a schemaVersion string. **Every one of the seven is a
193
+ // legal Git ref name**, so the grammar cannot reach them. What they lack is a neighbouring word
194
+ // that puts a branch in the sentence, and that is the discipline the two rules that measured
195
+ // well already have: `branch_quoted` (2/2) requires the literal word `branch`,
196
+ // `branch_directional` requires a preposition. This is the same requirement, stated for a rule
197
+ // whose shape spans a slash.
198
+ { kind: "branch", ruleId: "branch_kind_prefix", pattern: /\b((?:feature|feat|release|hotfix|bugfix|support)\/[A-Za-z0-9._-][A-Za-z0-9._/-]{0,48})/gu, group: 1, normalize: stripRemote, accept: isBranchRefName, context: hasBranchContext },
199
+ // `branch \`foo\``, `branch "foo"`. A quoted span immediately after the word is the author
200
+ // naming a branch on purpose; an unquoted word after it is usually a verb.
201
+ { kind: "branch", ruleId: "branch_quoted", pattern: /\bbranch(?:es)?\s+[`'"]([A-Za-z0-9._/-]{1,48})[`'"]/giu, group: 1, normalize: stripRemote },
202
+ // `deploy from main`, `merged into develop`. A directional preposition plus one of the four
203
+ // branch names this ecosystem actually uses. Restricted to that closed set because
204
+ // `from the release` is a noun phrase and `from main` is not.
205
+ { kind: "branch", ruleId: "branch_directional", pattern: /\b(?:on|from|onto|into|off|against|to)\s+(main|master|develop)\b/giu, group: 1 },
206
+ // `the release branch`, `the main branch`. The word `branch` as a head noun with a modifier.
207
+ { kind: "branch", ruleId: "branch_named_noun", pattern: /\b(main|master|develop|release|staging)\s+branch\b/giu, group: 1 },
208
+ // `in production`, `on staging`. The preposition is required: `production code` is an adjective.
209
+ { kind: "environment", ruleId: "env_preposition", pattern: /\b(?:in|on|to|against|for)\s+(production|prod|staging|stage|sandbox)\b/giu, group: 1 },
210
+ // `the staging environment`, `a test environment`.
211
+ { kind: "environment", ruleId: "env_noun", pattern: /\b(production|prod|staging|stage|development|dev|test|testing|sandbox|local|ci)\s+environments?\b/giu, group: 1 },
212
+ { kind: "environment", ruleId: "env_node_env", pattern: /\bNODE_ENV\s*[=:]\s*["']?([A-Za-z_]{2,20})["']?/gu, group: 1 },
213
+ // The platform a statement was observed on. This corpus is a Windows machine talking about
214
+ // cross-platform behaviour, so `on Windows` genuinely qualifies a claim's validity.
215
+ { kind: "environment", ruleId: "env_platform", pattern: /\b(?:on|under|for)\s+(Windows|Linux|macOS|darwin|win32|WSL)\b/giu, group: 1 },
216
+ { kind: "environment", ruleId: "env_ci", pattern: /\b(?:in|on|under)\s+(CI)\b/gu, group: 1 },
217
+ ];
218
+ /** Values that are never a real qualifier however the shape matched. Kept minimal and structural. */
219
+ const REJECTED_VALUES = new Set(["", "-", "_", "."]);
220
+ /**
221
+ * Every branch and environment qualifier the rules find in one text, in span order.
222
+ *
223
+ * Overlapping matches are resolved by span claiming, earliest rule first. Duplicates of the same
224
+ * `(kind, value)` collapse to their first occurrence: a claim that says `main` three times is one
225
+ * claim about `main`, and counting it three times would make repetition look like corroboration.
226
+ */
227
+ export function extractQualifiers(text) {
228
+ const claimed = [];
229
+ const hits = [];
230
+ const seen = new Set();
231
+ for (const rule of RULES) {
232
+ rule.pattern.lastIndex = 0;
233
+ for (const match of text.matchAll(rule.pattern)) {
234
+ const raw = match[rule.group];
235
+ if (raw === undefined)
236
+ continue;
237
+ const start = match.index ?? 0;
238
+ const end = start + match[0].length;
239
+ if (claimed.some(([from, to]) => start < to && end > from))
240
+ continue;
241
+ // Checked before the capture is even normalized, and like `accept` it does not claim the
242
+ // span: a rule that says "the text around this is not about a branch" has declined, and a
243
+ // declined span must stay visible to every later rule.
244
+ if (rule.context !== undefined && !rule.context(text, start, end))
245
+ continue;
246
+ const normalized = (rule.normalize === undefined ? raw : rule.normalize(raw)).toLowerCase().replace(/[.,;:)\]]+$/u, "");
247
+ if (REJECTED_VALUES.has(normalized))
248
+ continue;
249
+ // A rule that declines a capture does not claim its span: declining is "this is not mine",
250
+ // not "this is mine and empty", and consuming the text would hide it from every later rule.
251
+ if (rule.accept !== undefined && !rule.accept(normalized))
252
+ continue;
253
+ claimed.push([start, end]);
254
+ const key = `${rule.kind}:${normalized}`;
255
+ if (seen.has(key))
256
+ continue;
257
+ seen.add(key);
258
+ hits.push({ kind: rule.kind, value: normalized, ruleId: rule.ruleId, matchedText: match[0], start, end });
259
+ }
260
+ }
261
+ hits.sort((a, b) => a.start - b.start);
262
+ return hits;
263
+ }
264
+ /** The distinct qualifier values of one kind, sorted, for comparison between two claims. */
265
+ export function qualifierValues(hits, kind) {
266
+ return [...new Set(hits.filter((hit) => hit.kind === kind).map((hit) => hit.value))].sort();
267
+ }
268
+ /**
269
+ * Whether two claims' qualifiers of one kind are in genuine disagreement.
270
+ *
271
+ * Both sides must carry at least one qualifier and the sets must be disjoint. A claim with no
272
+ * qualifier is *unqualified*, not *qualified differently* — the overwhelmingly common case on this
273
+ * corpus — and treating absence as a difference would report a `BRANCH_DIFFERENCE` between a
274
+ * statement about `main` and a statement about nothing in particular, which is the false positive
275
+ * this whole extractor is gated on avoiding.
276
+ */
277
+ export function qualifiersDiverge(a, b, kind) {
278
+ const left = qualifierValues(a, kind);
279
+ const right = qualifierValues(b, kind);
280
+ if (left.length === 0 || right.length === 0)
281
+ return { diverges: false, left, right };
282
+ const overlap = left.some((value) => right.includes(value));
283
+ return { diverges: !overlap, left, right };
284
+ }
285
+ /* ------------------------------------------------------------------------------------------------
286
+ * The measurement gate (design §17 ambiguity 4)
287
+ * ---------------------------------------------------------------------------------------------- */
288
+ /**
289
+ * Whether extracted qualifiers may influence **ranking**.
290
+ *
291
+ * Design §17 ambiguity 4: "deferred to Phase 2 behind a measurement gate before it influences
292
+ * ranking". Design §16's Phase 2 addition pre-registers the bar at **precision ≥ 0.80 over a
293
+ * labelled sample of at least 20 hits drawn from the live corpus**, matching the conflict-precision
294
+ * bar already written there.
295
+ *
296
+ * ## MEASURED IN PHASE 2 — 0.281 over 32 labelled hits. The gate FAILED and the answer was no.
297
+ *
298
+ * `eval/qualifier-eval.ts` ran the extractor over 802 claims — every claim the nineteen labelled
299
+ * gold retrieval tasks accept, every claim from all fourteen live compaction summaries, and all ten
300
+ * curated memories — and produced 32 distinct hits, labelled one by one in
301
+ * `eval/qualifier-labels.json`:
302
+ *
303
+ * ```
304
+ * branch_remote_ref 9 hits 9 correct 1.000
305
+ * branch_kind_prefix 23 hits 0 correct 0.000
306
+ * every other rule 0 hits — —
307
+ * ```
308
+ *
309
+ * Phase 2 **left the failing rule in place**, because deleting it and re-measuring the same 32 hits
310
+ * gives 1.000 over the surviving nine, and a precision obtained by removing the rule that generated
311
+ * the errors *after seeing which errors it generated* is a fitted pass. It pre-registered the
312
+ * correction instead: fix the rule, then measure on a sample drawn **after** the change.
313
+ *
314
+ * ## RE-MEASURED AFTER THE CORRECTION — 0.784 over 227 fresh hits. The gate STILL FAILS.
315
+ *
316
+ * `branch_kind_prefix` is corrected above (two independent changes: a branch-kind vocabulary, and
317
+ * `isBranchRefName`'s positive Git ref grammar). The fresh sample is **every one of the 11,182 live
318
+ * conversation events**, extracted through the genuine pipeline into 11,903 claims, yielding 227 hits
319
+ * whose keys are disjoint from the 32 — labelled by hand in `eval/qualifier-labels-fresh.json`
320
+ * before the number was computed.
321
+ *
322
+ * ```
323
+ * fresh sample diagnostic set (the Phase 2 population, for comparison)
324
+ * branch_remote_ref 194 hits 158 correct 0.814 9 hits 9 correct 1.000
325
+ * branch_kind_prefix 7 hits 0 correct 0.000 0 hits — — (23 errors removed)
326
+ * branch_quoted 2 hits 2 correct 1.000
327
+ * branch_directional 4 hits 2 correct 0.500
328
+ * env_platform 12 hits 12 correct 1.000
329
+ * env_ci 2 hits 2 correct 1.000
330
+ * env_preposition 6 hits 2 correct 0.333
331
+ * overall 227 hits 178 correct 0.784 9 hits 9 correct 1.000
332
+ * ```
333
+ *
334
+ * **The correction worked and the gate still fails, and those are two separate findings.** All 23
335
+ * Phase 2 errors are gone and not one repository path under `test/` or `docs/` survives. What the
336
+ * fresh draw exposes is that the *dominant* rule, not the corrected one, is where the volume of
337
+ * error now is: `branch_remote_ref`'s `\b(?:origin|upstream|fork)\/` matches inside the GitHub
338
+ * organisation name `repoe-fork`, so `repoe-fork/poe2` and every `api.github.com` URL beneath it
339
+ * reads as remote `fork` + branch `poe2/…` — 33 of its 36 errors, and 33 of the sample's 49. That
340
+ * failure mode does not exist in the Phase 2 population at all, which is precisely why the sample
341
+ * had to be drawn again rather than re-scored.
342
+ *
343
+ * **`branch_kind_prefix` is 0 for 7 on the fresh sample, and is again left in place.** Its new
344
+ * errors are a different class from its old ones — `release/security audit` and
345
+ * `CI/release/documentation fixes` are slashes written in prose as conjunctions, and
346
+ * `../target/release/attack_poc` is a build output path — and every one of the seven is a *legal*
347
+ * Git ref name, so no refinement of the ref grammar reaches them. The residual defect is that the
348
+ * `kind/name` shape carries no evidence that a branch is being *discussed*; the repair would be to
349
+ * require a contextual marker, as `branch_quoted` (2/2) and `branch_directional` do. That is
350
+ * recorded as the next thing to measure, on a sample drawn after *that* change.
351
+ *
352
+ * ## RE-MEASURED AFTER THE SECOND ROUND OF CORRECTIONS — 0.837 over 620 third-sample hits. **PASS.**
353
+ *
354
+ * Both defects above are corrected (`branch_remote_ref`'s left boundary and ref grammar;
355
+ * `branch_kind_prefix`'s contextual marker), and the gate was measured a third time on a sample drawn
356
+ * **after** those changes.
357
+ *
358
+ * **The third population had to be a different part of the corpus, and the reason is arithmetic.**
359
+ * Both corrections are *narrowings*, so re-running the head-excerpt sweep can only produce a subset of
360
+ * the 227 already labelled — measured, **196 hits of which 0 are unlabelled**. The corpus has not
361
+ * grown (11,182 events) and the ten curated memories are in the diagnostic set. What was left is the corpus itself: `claims.ts:excerptFor` hands a
362
+ * briefing a 481-character window, the head-excerpt sweep read offset 0 of every event and nothing
363
+ * else — **5.4 MB of 44.4 MB** — and a lexical search anchors that window anywhere. The third sample
364
+ * is every remaining window, tiled from offset 481, all 86,594 of them, hand-labelled in
365
+ * `eval/qualifier-labels-third.json` before the number was computed.
366
+ *
367
+ * ```
368
+ * third sample (THE GATE) diagnostic 1 (Phase 2) diagnostic 2 (Phase 6)
369
+ * branch_remote_ref 320 hits 299 correct 0.934 9 hits 9 1.000 163 hits 158 0.969
370
+ * branch_kind_prefix 0 hits — — 0 hits — — 0 hits — —
371
+ * branch_quoted 39 hits 36 correct 0.923 2 hits 2 1.000
372
+ * branch_directional 13 hits 3 correct 0.231 4 hits 2 0.500
373
+ * branch_named_noun 19 hits 19 correct 1.000
374
+ * env_preposition 94 hits 27 correct 0.287 6 hits 2 0.333
375
+ * env_noun 2 hits 2 correct 1.000
376
+ * env_node_env 1 hit 1 correct 1.000
377
+ * env_platform 84 hits 84 correct 1.000 12 hits 12 1.000
378
+ * env_ci 48 hits 48 correct 1.000 2 hits 2 1.000
379
+ * OVERALL 620 hits 519 correct 0.837 9 hits 9 1.000 189 hits 178 0.942
380
+ * ```
381
+ *
382
+ * **Both prior samples are diagnostics and neither is the gate.** Each correction was written after
383
+ * reading the errors in them, so 1.000 and 0.942 report how much known error the fixes removed and
384
+ * nothing about text the rules have not seen.
385
+ *
386
+ * **The aggregate hides a spread, and the spread is why the wiring below is what it is.** 0.837 is a
387
+ * mixture of five rules at 0.923–1.000 and two at **0.231** (`branch_directional`) and **0.287**
388
+ * (`env_preposition`). `branch_directional` is convicted by the third sample: `off main thread`,
389
+ * `into main context`, `from main-tree allocation`, `to main site`, `on main-hand` — a directional
390
+ * preposition proves direction, not that a branch is being named, and `main` is an ordinary English
391
+ * adjective. `env_preposition`'s 67 errors are 57 hits of `sandbox` naming a tool's execution or
392
+ * storage location. Both are recorded as the next thing to correct, and neither was corrected in this
393
+ * pass, because they were found *in the sample that decides the gate* and repairing them now would be
394
+ * repairing the system toward the evidence.
395
+ *
396
+ * **`branch_kind_prefix` now fires zero times on the whole corpus**, and that is disclosed rather
397
+ * than presented as a win. The contextual-marker requirement is the repair design §23 pre-registered,
398
+ * its contract is exercised by unit tests, and this corpus simply contains no git-flow-prefixed branch
399
+ * reference. Its contribution to the gate is nil, so the gate does not test it.
400
+ *
401
+ * ## Consequence: `enabled = true`, and what that does and does not turn on
402
+ *
403
+ * **What it turns on** is the one place a qualifier can reach a *score*: a `QUALIFIES` edge suppresses
404
+ * the `CONTRADICTS` test for that pair (`provenance.ts`), and `contradictions` is a term in
405
+ * `evidence_strength` (design §9). Two statements about two different branches disagree about
406
+ * nothing, so the `−0.15` should not be charged — but charging or not charging it on the strength of a
407
+ * 0.281-precise extractor was exactly the silent damage §17 ambiguity 4 gated. That suppression is now
408
+ * *conditional on this flag*, which is the mechanical meaning the flag previously lacked.
409
+ *
410
+ * **What it does not turn on is a weight, and that is not this code's decision to make.** The obvious
411
+ * next step — a relevance bonus when a claim's qualifiers match the qualifiers the *task* names —
412
+ * would add a field to `RankingPolicy.relevanceBonus`, and `policy.ts` is what design §19 item 3
413
+ * reserves for a human, with a §22 change packet. Every prior change to that table was declared as an
414
+ * approval item and a version bump. It is pre-registered here, not applied, and the per-rule spread
415
+ * above is the evidence a reviewer needs: an aggregate 0.837 multiplied into a score would carry
416
+ * `branch_directional`'s 0.231 in with it.
417
+ *
418
+ * Conflict **classification** reads the qualifiers either way, as it always has: design §2 assigned
419
+ * them that job, and a `BRANCH_DIFFERENCE` record is a disclosure the reader can check rather than a
420
+ * number multiplied into a score they cannot see.
421
+ */
422
+ export const QUALIFIER_RANKING = Object.freeze({
423
+ enabled: true,
424
+ measuredPrecision: 0.837,
425
+ sampleSize: 620,
426
+ gate: 0.8,
427
+ decidedBy: "eval/qualifier-eval.ts, third sample — every 481-character claim window of the live corpus the head-excerpt sweep never read, labelled in eval/qualifier-labels-third.json before the number was computed and disjoint by assertion from both prior label sets (0 of 620 keys collide). The two prior measurements are retained as diagnostics: 0.281 over 32 in eval/qualifier-labels.json and 0.784 over 227 in eval/qualifier-labels-fresh.json, each over a population whose errors motivated a later correction — see PHASE0-DESIGN.md §17 ambiguity 4",
428
+ });