@gamaze/hicortex 0.26.3 → 0.27.1

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 (60) hide show
  1. package/README.md +21 -13
  2. package/assets/dashboard.html +95 -3
  3. package/dist/backfill-privacy.d.ts +109 -0
  4. package/dist/backfill-privacy.js +304 -0
  5. package/dist/capture.d.ts +60 -0
  6. package/dist/capture.js +90 -1
  7. package/dist/classification.d.ts +315 -0
  8. package/dist/classification.js +525 -0
  9. package/dist/classify-domains.js +24 -7
  10. package/dist/cli.js +80 -0
  11. package/dist/cluster.d.ts +15 -8
  12. package/dist/cluster.js +10 -3
  13. package/dist/confidential-marker.d.ts +103 -0
  14. package/dist/confidential-marker.js +285 -0
  15. package/dist/consolidate.d.ts +65 -5
  16. package/dist/consolidate.js +275 -48
  17. package/dist/dashboard.d.ts +32 -0
  18. package/dist/dashboard.js +33 -0
  19. package/dist/db.js +49 -1
  20. package/dist/dedup.d.ts +5 -3
  21. package/dist/dedup.js +26 -12
  22. package/dist/distill-queue.d.ts +34 -1
  23. package/dist/distill-queue.js +57 -10
  24. package/dist/graph.d.ts +28 -3
  25. package/dist/graph.js +46 -4
  26. package/dist/hermes-transcript-reader.js +5 -0
  27. package/dist/init.d.ts +18 -9
  28. package/dist/init.js +111 -81
  29. package/dist/marker-key-api.d.ts +79 -0
  30. package/dist/marker-key-api.js +96 -0
  31. package/dist/mcp-server.js +317 -32
  32. package/dist/nightly.js +36 -7
  33. package/dist/opencode-transcript-reader.js +3 -0
  34. package/dist/pi-transcript-reader.js +3 -0
  35. package/dist/prompts.d.ts +33 -1
  36. package/dist/prompts.js +37 -2
  37. package/dist/recall-index.d.ts +19 -9
  38. package/dist/recall-index.js +14 -4
  39. package/dist/recall-precision.js +3 -1
  40. package/dist/reconsolidation.d.ts +10 -0
  41. package/dist/reconsolidation.js +80 -17
  42. package/dist/redact.d.ts +9 -1
  43. package/dist/redact.js +32 -1
  44. package/dist/rescore-importance.js +21 -4
  45. package/dist/retrieval.d.ts +23 -6
  46. package/dist/retrieval.js +41 -15
  47. package/dist/seed-lesson.js +3 -1
  48. package/dist/storage.d.ts +23 -4
  49. package/dist/storage.js +43 -11
  50. package/dist/transcript-reader.d.ts +10 -0
  51. package/dist/transcript-reader.js +13 -0
  52. package/dist/type-classify.js +22 -7
  53. package/dist/types.d.ts +117 -15
  54. package/dist/uninstall.js +6 -5
  55. package/hermes-plugin/hicortex/README.md +4 -0
  56. package/hermes-plugin/hicortex/client.py +36 -9
  57. package/hermes-plugin/hicortex/plugin.yaml +1 -1
  58. package/hermes-plugin/hicortex/provider.py +28 -6
  59. package/package.json +1 -1
  60. package/server.json +2 -2
@@ -0,0 +1,315 @@
1
+ /**
2
+ * The confidential circle (#332) — the pure decision core for the two-level
3
+ * memory classification (`standard` | `confidential`, carried by
4
+ * `memories.privacy`; see MemoryPrivacy in types.ts).
5
+ *
6
+ * The model, in one paragraph: an operator lists inner-circle agents in
7
+ * config (`confidentialAgents`, the same agent-id namespace as
8
+ * `identityAgents` / `?agent=`). Captures whose `source_agent` is rostered
9
+ * are stamped `confidential` at intake — server-derived, deterministic, by
10
+ * birthplace; never a content judgment (content classification was rejected,
11
+ * #197) and never client-declared. The workspace marker (#609/#614) is
12
+ * client-RELAYED but server-VALIDATED: the marker file carries the owner-set
13
+ * `confidentialMarkerKey` as its content, compared timing-safe at intake —
14
+ * marker content without the key never stamps (the client-writable
15
+ * classification channel #614 closes; an unkeyed install ignores markers
16
+ * entirely, fail-closed both directions). Every agent-facing read path accepts an
17
+ * optional `agent` requester id: absent or unknown = non-member = HARD OMIT
18
+ * of confidential rows (fail-closed both ends — unrostered sources are
19
+ * standard, unknown requesters are non-members). The circle is ADDITIVE:
20
+ * members see both pools. Every LLM touch of confidential text routes to a
21
+ * separately configured second model (`llmConfidential*` config keys /
22
+ * `HICORTEX_LLM_CONFIDENTIAL_*` env) — when that model is absent, the work
23
+ * HOLDS (queue stays, deliveries refuse), never falls back to the main
24
+ * model. An install that configures nothing sees zero change: no roster, no
25
+ * confidential rows, no warning, no second client.
26
+ *
27
+ * All functions here are pure (no DB, no HTTP): the handlers in mcp-server.ts
28
+ * and the pipeline stages consume them, and the tests exercise the decisions
29
+ * directly.
30
+ */
31
+ import { LlmClient, type LlmConfig } from "./llm.js";
32
+ import type { MemoryPrivacy } from "./types.js";
33
+ /**
34
+ * Resolve the `confidentialAgents` config value into a clean roster of agent
35
+ * ids. Each entry is sanitized EXACTLY like `?agent=` (sanitizeAgentId —
36
+ * lowercase, collapse invalid runs to `-`, allowlist-validated) so the roster,
37
+ * the wire `agent` param, and capture's `<harness>/<id>` stamps live in one
38
+ * id namespace. Entries that sanitize to null are dropped and their raw
39
+ * values returned in `dropped` for a one-time boot warning (the
40
+ * resolveIdentityAgents pattern — a typo must surface, not silently drop an
41
+ * agent out of the circle).
42
+ */
43
+ export declare function resolveConfidentialAgents(config: Record<string, unknown> | null | undefined): {
44
+ roster: string[];
45
+ dropped: string[];
46
+ };
47
+ /**
48
+ * Normalize a capture-side `source_agent` for roster matching. Capture stamps
49
+ * harness-qualified ids (`hermes/nano`, `claude-code/myproject`), while the
50
+ * roster carries bare agent ids (the same ids the read side receives as
51
+ * `agent`) — ONE config list, no read-ids/capture-ids sync hazard. The match
52
+ * is exact, or exact after stripping the FIRST path segment (so `hermes/nano`
53
+ * matches roster `nano`, and a future `anything/nano` matches too — generic
54
+ * prefix, not a per-harness table). Case-insensitive on the id half via the
55
+ * roster's sanitized (lowercased) form.
56
+ */
57
+ export declare function isConfidentialSourceAgent(sourceAgent: string | null | undefined, roster: string[]): boolean;
58
+ /**
59
+ * One validated `confidentialPaths` entry, compiled once at boot (the
60
+ * resolveConfidentialAgents pattern). The three match forms are deliberately
61
+ * tiny — no glob library, no character classes, no brace expansion — so the
62
+ * auditable surface is exactly what the spec rules:
63
+ * - absolute exact path (`/home/alice/finance`);
64
+ * - `*` as a FULL path segment — matches exactly ONE segment (a lone-star
65
+ * segment between /home/ and /finance matches any single middle segment);
66
+ * - trailing `/**` — the whole subtree INCLUDING the prefix itself
67
+ * (`/home/alice/**` matches `/home/alice`);
68
+ * - a leading `~/` matches any single home prefix STRUCTURALLY
69
+ * (`/home/<user>/…` or `/Users/<user>/…`) so one entry covers a
70
+ * multi-OS fleet.
71
+ * Comparison normalizes separators to `/` + Unicode NFC and is
72
+ * case-insensitive — over-match is the fail-closed direction (more
73
+ * confidential, never less).
74
+ */
75
+ export interface ConfidentialPathPattern {
76
+ /** The raw config entry as written (boot warnings, backfill reports). */
77
+ raw: string;
78
+ /** Leading `~/` — match any user's home directory as the prefix. */
79
+ home: boolean;
80
+ /** Trailing `/**` — prefix match including the prefix path itself. */
81
+ subtree: boolean;
82
+ /** Normalized segments of the literal part; `"*"` = exactly one segment. */
83
+ segments: string[];
84
+ }
85
+ /**
86
+ * Resolve the `confidentialPaths` config value into validated patterns.
87
+ * Entries that do not compile are dropped and their raw values returned in
88
+ * `dropped` for a one-time boot warning (a dropped pattern matches nothing —
89
+ * the operator learns from the log, not from a folder silently staying
90
+ * standard). Duplicates (same raw entry) collapse. Absent/non-array/empty →
91
+ * no patterns, nothing dropped — the zero-change default.
92
+ */
93
+ export declare function resolveConfidentialPaths(config: Record<string, unknown> | null | undefined): {
94
+ patterns: ConfidentialPathPattern[];
95
+ dropped: string[];
96
+ };
97
+ /**
98
+ * The pure matcher: does `workspacePath` fall under any resolved pattern?
99
+ * Empty pattern list matches nothing (and the zero-change default is exactly
100
+ * that); a null/empty path matches nothing (Hermes has no workspace — path
101
+ * signals never fire there). No fs access — callers pass the delivery's
102
+ * declared workspace path; the server decides (the client never knows the
103
+ * patterns).
104
+ */
105
+ export declare function matchesConfidentialPath(workspacePath: string | null | undefined, patterns: ConfidentialPathPattern[]): boolean;
106
+ /**
107
+ * Constant-time marker-content × key comparison (#614) — the safeBearerMatch
108
+ * pattern (viz.ts auth). `crypto.timingSafeEqual` THROWS RangeError on
109
+ * mismatched buffer lengths, so the length check MUST gate the call; the
110
+ * short-circuit leaks only the CONTENT length, which is already visible on
111
+ * the wire (the delivery body carries the content verbatim) — the key bytes
112
+ * are never compared byte-by-byte through a timing side channel. Empty
113
+ * content never matches: an empty (pre-#614) marker file is inert by DESIGN,
114
+ * not by luck of the length gate.
115
+ */
116
+ export declare function safeMarkerKeyEqual(markerContent: string, key: string): boolean;
117
+ /**
118
+ * Resolve the `confidentialMarkerKey` (#614) — the owner-set secret a
119
+ * workspace marker must carry for its folder to stamp confidential at
120
+ * intake. Precedence: env `HICORTEX_CONFIDENTIAL_MARKER_KEY` OVERRIDES the
121
+ * config value (the env-last-word ops override, so a restart-free rotation
122
+ * or override path exists beside the file), then the config value through
123
+ * the readStringConfig boundary (wrong type warns + null — never coerced;
124
+ * a JSON slip like a number cannot silently arm the arm). Blank/absent →
125
+ * null = the marker arm is INERT: no key configured means client markers
126
+ * are ignored entirely (fail-closed both directions — the empty-marker
127
+ * attack surface ships disabled by default, AC-1). Boot-time like the
128
+ * roster; restart to apply. Distributed like authToken: mirror the server's
129
+ * value into each client machine's config.json (or its env).
130
+ */
131
+ export declare function resolveConfidentialMarkerKey(config: Record<string, unknown> | null | undefined): string | null;
132
+ /**
133
+ * The intake-side derivation: which class a delivery from `sourceAgent`
134
+ * stores as. Fail-closed by construction — anything unattributed or
135
+ * unrostered is `standard` (the global pool).
136
+ *
137
+ * #609/#614 composition invariant: confidential iff roster agent OR server
138
+ * path pattern OR VALIDATED marker content. The marker arm is key-gated
139
+ * (#614): the delivery's relayed marker CONTENT must equal the boot-resolved
140
+ * `confidentialMarkerKey` (timing-safe, server-side — a client-side match is
141
+ * never trusted, the client only relays). An absent key makes every marker
142
+ * inert (the empty-marker client-writable channel is closed), and a
143
+ * present-but-refused marker never reduces a server rule (markers only add,
144
+ * never subtract — safe because a token holder can already POST arbitrary
145
+ * content, the #599 F5 ruling, token = owner). The optional third argument
146
+ * keeps every existing caller byte-identical (no signals = the #332
147
+ * agent-only rule).
148
+ */
149
+ export declare function deriveIntakePrivacy(sourceAgent: string | null | undefined, roster: string[], signals?: IntakeWorkspaceSignals): MemoryPrivacy;
150
+ /**
151
+ * The #609/#614 workspace birthplace signals `deriveIntakePrivacy` composes
152
+ * with the roster. Every field optional/nullable — a delivery from a harness
153
+ * with no workspace (Hermes) or a pre-#609 client carries none of them.
154
+ */
155
+ export interface IntakeWorkspaceSignals {
156
+ /** The delivery's workspace path (wire `workspace_path`). Null/absent when
157
+ * unknown — never invented client-side. */
158
+ workspacePath?: string | null;
159
+ /**
160
+ * #614: the relayed marker content (wire `client_marker`) — the trimmed
161
+ * first line of `.hicortex-confidential` at the workspace or an ancestor
162
+ * (readConfidentialMarker in capture.ts). Null/absent = no marker found;
163
+ * "" = a marker file exists but is empty (the pre-#614 shape — present,
164
+ * never valid). The field's MEANING is "what the marker carried" (a key
165
+ * today, a space-id + key pair later, #613) — validity is decided HERE,
166
+ * against `markerKey`, never client-side.
167
+ */
168
+ markerContent?: string | null;
169
+ /**
170
+ * #614: the boot-resolved `confidentialMarkerKey`
171
+ * (resolveConfidentialMarkerKey). Null = markers INERT — no key, no
172
+ * marker-derived stamp (fail-closed both directions).
173
+ */
174
+ markerKey?: string | null;
175
+ /** Boot-resolved confidentialPaths patterns (resolveConfidentialPaths). */
176
+ paths?: ConfidentialPathPattern[];
177
+ }
178
+ /**
179
+ * The read-side membership check: is the requester a circle member? Absent,
180
+ * blank, or unknown `agent` → false (fail-closed: an unknown id can never
181
+ * widen visibility, exactly like the identity layer's mode resolution). The
182
+ * `agent` value is sanitized first so a wire id arrives in the roster's
183
+ * namespace (`Nano` → `nano`); note sanitizeAgentId also collapses inner
184
+ * separators, so `hermes/nano` sent as a requester id reads as `hermes-nano`
185
+ * — the read side intentionally matches BARE ids only (plugins send their
186
+ * resolved profile name, not their capture-side qualified stamp).
187
+ */
188
+ export declare function isCircleMember(agent: string | null | undefined, roster: string[]): boolean;
189
+ /**
190
+ * Resolve the confidential model's CONFIG from explicit config keys or env —
191
+ * the same shape and precedence as the main model
192
+ * (resolveExplicitLlmConfig):
193
+ *
194
+ * 1. config `llmConfidentialBaseUrl` + `llmConfidentialApiKey` (both
195
+ * required; `llmConfidentialModel` optional; `llmConfidentialProvider`
196
+ * selects the request dialect, else URL detection — the llmProvider
197
+ * precedent);
198
+ * 2. env `HICORTEX_LLM_CONFIDENTIAL_BASE_URL` +
199
+ * `HICORTEX_LLM_CONFIDENTIAL_API_KEY` (both required;
200
+ * `HICORTEX_LLM_CONFIDENTIAL_MODEL` optional) — mirroring the main
201
+ * model's HICORTEX_LLM_* env pattern.
202
+ *
203
+ * null when nothing explicit is set — the caller holds/skips confidential
204
+ * work and warns (never falls back to the main model).
205
+ *
206
+ * Deliberately NOT routed through the named backends (claude-cli / ollama):
207
+ * the confidential model is a full explicit-config surface by spec — a
208
+ * second claude-cli subprocess or a local ollama alias would silently be the
209
+ * SAME trust domain as the main model.
210
+ */
211
+ export declare function resolveConfidentialLlmConfig(savedConfig: Record<string, unknown> | null | undefined): LlmConfig | null;
212
+ /**
213
+ * Build the confidential LlmClient for a resolved config — the daemon
214
+ * funnel's construction half. Applies the SAME tuning overlay as the main
215
+ * client (applyTierTuningOverlay, the both-sites discipline: maxTokens,
216
+ * timeouts, single-flight apply per-process at construction) so the second
217
+ * model is not a second-class client.
218
+ */
219
+ export declare function buildConfidentialLlm(savedConfig: Record<string, unknown> | null | undefined): {
220
+ llm: LlmClient;
221
+ config: LlmConfig;
222
+ } | null;
223
+ /**
224
+ * The two clients a pipeline run needs. `standard` is the main model (always
225
+ * present when the run does LLM work at all); `confidential` is null when the
226
+ * circle is active but the second model is unconfigured — callers HOLD
227
+ * confidential work in that case (clientForPool returns null), never fall
228
+ * back.
229
+ */
230
+ export interface LlmPair {
231
+ standard: LlmClient;
232
+ confidential: LlmClient | null;
233
+ }
234
+ /**
235
+ * Resolve the full pair for a pipeline process (the nightly/CLI funnel —
236
+ * resolveSavedLlmConfig for the standard client, buildConfidentialLlm for
237
+ * the confidential one). Returns the pair plus both configs (the drain and
238
+ * the /distill sync path need the config for chunk-size detection/probing),
239
+ * and passes through resolveSavedLlmConfig's `reason` so callers keep their
240
+ * claude-binary-missing logging.
241
+ */
242
+ export declare function resolveLlmPair(savedConfig: Record<string, unknown> | null | undefined): {
243
+ pair: LlmPair | null;
244
+ standardConfig: LlmConfig | null;
245
+ confidentialConfig: LlmConfig | null;
246
+ reason?: "claude_binary_missing";
247
+ };
248
+ /**
249
+ * The per-pool selection every stage call site goes through: the client that
250
+ * MUST serve a memory class, or null when the confidential pool has no
251
+ * client configured (the caller holds/skips that pool's work — the
252
+ * structurally-hard guarantee that confidential text can never reach the
253
+ * standard model: there is no code path that answers "give me SOME client
254
+ * for confidential").
255
+ */
256
+ export declare function clientForPool(pair: LlmPair, privacy: MemoryPrivacy | null | undefined): LlmClient | null;
257
+ /**
258
+ * The ONE loud boot warning when the circle is ACTIVE (a roster OR
259
+ * confidentialPaths patterns are configured, OR confidential rows exist, OR
260
+ * confidential items sit queued) but the confidential model is not. Called at
261
+ * daemon boot and nightly start; prints once per call site (the callers are
262
+ * once-per-process). The wording names the exposure and the HOLD behavior —
263
+ * the operator learns from the logs that deliveries are piling up, not
264
+ * silently distilling on the main model. Returns whether the warning fired
265
+ * (tests + callers that compose their own log lines).
266
+ */
267
+ export declare function warnIfCircleUnconfigured(opts: {
268
+ roster: string[];
269
+ /** #609: boot-resolved confidentialPaths patterns — configured patterns
270
+ * activate the circle exactly like a roster (deliveries can derive
271
+ * confidential with no rostered agent involved). Absent = none. */
272
+ patterns?: ConfidentialPathPattern[];
273
+ confidentialLlm: LlmClient | LlmConfig | null;
274
+ confidentialRows: number;
275
+ queueConfidential: number;
276
+ }): boolean;
277
+ /**
278
+ * The boot note when the circle is ACTIVE but the marker key is not (#614):
279
+ * one line naming `confidentialMarkerKey` and that client workspace markers
280
+ * are IGNORED (inert) until it is set — an operator running a marked folder
281
+ * learns from the log why it stamps standard, not from silently
282
+ * unclassified captures. Fires ONLY when the key is absent AND the circle is
283
+ * otherwise active (the SAME active predicate as warnIfCircleUnconfigured:
284
+ * roster, confidentialPaths patterns, stored confidential rows, or queued
285
+ * confidential items); a zero-config install stays silent (the header's
286
+ * zero-change-default discipline). Pure — returns the note string, the
287
+ * caller prints it (testable without console spies). Null = say nothing.
288
+ */
289
+ export declare function confidentialMarkerKeyNote(opts: {
290
+ markerKey: string | null;
291
+ roster: string[];
292
+ /** Boot-resolved confidentialPaths patterns — configured patterns
293
+ * activate the circle exactly like a roster (#609). Absent = none. */
294
+ patterns?: unknown[];
295
+ confidentialRows: number;
296
+ queueConfidential: number;
297
+ }): string | null;
298
+ /** Test seam: re-arm the warnRefusedMarker once-latch. */
299
+ export declare function resetRefusedMarkerWarn(): void;
300
+ /**
301
+ * The refused-marker warn (#614): a delivery carried marker content that did
302
+ * NOT validate at intake — present with NO key configured (the marker arm is
303
+ * inert) or present but NOT equal to the key (a stale pre-key marker, a
304
+ * rotated key, or a forgery attempt). The CALLER guards the call
305
+ * (`clientMarker !== null && clientMarker !== key` — one guard covers both
306
+ * reasons); the reason wording here distinguishes them for the operator.
307
+ * NEVER echoes the marker content or the key (log discipline — the secret
308
+ * and the guess both stay out of the logs); naming the workspace path is
309
+ * fine (it is the operator's own path, and it is what they need to fix).
310
+ * Returns whether the line printed (the once-latch ate it → false).
311
+ */
312
+ export declare function warnRefusedMarker(opts: {
313
+ markerKey: string | null;
314
+ workspacePath: string | null;
315
+ }): boolean;