@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.
- package/README.md +21 -13
- package/assets/dashboard.html +95 -3
- package/dist/backfill-privacy.d.ts +109 -0
- package/dist/backfill-privacy.js +304 -0
- package/dist/capture.d.ts +60 -0
- package/dist/capture.js +90 -1
- package/dist/classification.d.ts +315 -0
- package/dist/classification.js +525 -0
- package/dist/classify-domains.js +24 -7
- package/dist/cli.js +80 -0
- package/dist/cluster.d.ts +15 -8
- package/dist/cluster.js +10 -3
- package/dist/confidential-marker.d.ts +103 -0
- package/dist/confidential-marker.js +285 -0
- package/dist/consolidate.d.ts +65 -5
- package/dist/consolidate.js +275 -48
- package/dist/dashboard.d.ts +32 -0
- package/dist/dashboard.js +33 -0
- package/dist/db.js +49 -1
- package/dist/dedup.d.ts +5 -3
- package/dist/dedup.js +26 -12
- package/dist/distill-queue.d.ts +34 -1
- package/dist/distill-queue.js +57 -10
- package/dist/graph.d.ts +28 -3
- package/dist/graph.js +46 -4
- package/dist/hermes-transcript-reader.js +5 -0
- package/dist/init.d.ts +18 -9
- package/dist/init.js +111 -81
- package/dist/marker-key-api.d.ts +79 -0
- package/dist/marker-key-api.js +96 -0
- package/dist/mcp-server.js +317 -32
- package/dist/nightly.js +36 -7
- package/dist/opencode-transcript-reader.js +3 -0
- package/dist/pi-transcript-reader.js +3 -0
- package/dist/prompts.d.ts +33 -1
- package/dist/prompts.js +37 -2
- package/dist/recall-index.d.ts +19 -9
- package/dist/recall-index.js +14 -4
- package/dist/recall-precision.js +3 -1
- package/dist/reconsolidation.d.ts +10 -0
- package/dist/reconsolidation.js +80 -17
- package/dist/redact.d.ts +9 -1
- package/dist/redact.js +32 -1
- package/dist/rescore-importance.js +21 -4
- package/dist/retrieval.d.ts +23 -6
- package/dist/retrieval.js +41 -15
- package/dist/seed-lesson.js +3 -1
- package/dist/storage.d.ts +23 -4
- package/dist/storage.js +43 -11
- package/dist/transcript-reader.d.ts +10 -0
- package/dist/transcript-reader.js +13 -0
- package/dist/type-classify.js +22 -7
- package/dist/types.d.ts +117 -15
- package/dist/uninstall.js +6 -5
- package/hermes-plugin/hicortex/README.md +4 -0
- package/hermes-plugin/hicortex/client.py +36 -9
- package/hermes-plugin/hicortex/plugin.yaml +1 -1
- package/hermes-plugin/hicortex/provider.py +28 -6
- package/package.json +1 -1
- package/server.json +2 -2
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* `hicortex backfill-privacy` (#332) — one-shot classification stamping for
|
|
4
|
+
* EXISTING rows, by source agent, and (#609) by workspace path.
|
|
5
|
+
*
|
|
6
|
+
* The intake derivation stamps every NEW delivery; this command heals history
|
|
7
|
+
* when the roster or the path patterns change (an agent joins the circle, a
|
|
8
|
+
* folder is listed, or the feature lands on a store that already has captures
|
|
9
|
+
* from a now-covered birthplace). The plan is a pure function of the config ×
|
|
10
|
+
* the rows' stored values:
|
|
11
|
+
* - every memories/distill_queue row whose source_agent matches the roster
|
|
12
|
+
* (isConfidentialSourceAgent — the same `<harness>/`-stripping matcher the
|
|
13
|
+
* intake uses) AND whose privacy != 'confidential' → stamped confidential;
|
|
14
|
+
* - #609: every row whose stored `workspace_path` matches a
|
|
15
|
+
* `confidentialPaths` pattern (the same pure matcher the intake uses),
|
|
16
|
+
* grouped per pattern. NULL-path rows are NEVER path-matched (pre-v28
|
|
17
|
+
* rows have no path by construction; they are reported as unmatched, not
|
|
18
|
+
* silently stamped). A row matching both a rostered agent and a pattern
|
|
19
|
+
* flips once (agent groups claim it first; each remaining row joins its
|
|
20
|
+
* first matching pattern so overlapping patterns never double-count);
|
|
21
|
+
* - NOTHING is ever stamped BACK to standard (a roster/pattern removal is
|
|
22
|
+
* not a demotion — memories stay confidential until the operator
|
|
23
|
+
* explicitly changes them; re-running with a smaller config is a no-op by
|
|
24
|
+
* design).
|
|
25
|
+
*
|
|
26
|
+
* Safety rails (the `hicortex dedup` / `reconcile-projects` pattern): dry run
|
|
27
|
+
* by default (report only, zero writes); `--apply` takes a pre-apply DB
|
|
28
|
+
* backup (abort all writes on failure), the single-flight capture lock
|
|
29
|
+
* (waitMs 0 — busy aborts), and stamps in ONE transaction. The queue rows are
|
|
30
|
+
* stamped too: a pre-upgrade queued segment from a covered birthplace would
|
|
31
|
+
* otherwise distill as standard (migration v27 normalized it) — the backfill
|
|
32
|
+
* closes that seam.
|
|
33
|
+
*
|
|
34
|
+
* Idempotent by construction: a second run finds every covered row already
|
|
35
|
+
* confidential and reports zero.
|
|
36
|
+
*/
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
exports.planBackfill = planBackfill;
|
|
39
|
+
exports.runBackfillPrivacy = runBackfillPrivacy;
|
|
40
|
+
const paths_js_1 = require("./paths.js");
|
|
41
|
+
const node_fs_1 = require("node:fs");
|
|
42
|
+
const node_path_1 = require("node:path");
|
|
43
|
+
const db_js_1 = require("./db.js");
|
|
44
|
+
const capture_js_1 = require("./capture.js");
|
|
45
|
+
const dedup_js_1 = require("./dedup.js");
|
|
46
|
+
const classification_js_1 = require("./classification.js");
|
|
47
|
+
const HICORTEX_HOME = (0, paths_js_1.hicortexHome)();
|
|
48
|
+
function readConfig(stateDir) {
|
|
49
|
+
try {
|
|
50
|
+
return JSON.parse((0, node_fs_1.readFileSync)((0, node_path_1.join)(stateDir, "config.json"), "utf-8"));
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/** Distinct source_agent values (with row counts) from one table. */
|
|
57
|
+
function readSourceAgents(db, table) {
|
|
58
|
+
const column = table === "memories" ? "source_agent" : "source_agent";
|
|
59
|
+
const rows = db
|
|
60
|
+
.prepare(`SELECT ${column} AS agent, COUNT(*) AS c FROM ${table}
|
|
61
|
+
WHERE ${column} IS NOT NULL AND ${column} != '' GROUP BY ${column}`)
|
|
62
|
+
.all();
|
|
63
|
+
return new Map(rows.map((r) => [r.agent, r.c]));
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The pure plan: which stored source_agent values match the roster, and how
|
|
67
|
+
* many of their rows are not yet confidential. Roster matching uses the
|
|
68
|
+
* INTAKE matcher (isConfidentialSourceAgent — `<harness>/`-stripping) so the
|
|
69
|
+
* backfill and the intake can never disagree about who is rostered. #609 adds
|
|
70
|
+
* the per-pattern groups (see planPathGroups) alongside the agent groups —
|
|
71
|
+
* same rails, same never-demotes rule.
|
|
72
|
+
*/
|
|
73
|
+
function planBackfill(db, roster, patterns = []) {
|
|
74
|
+
const memoryAgents = readSourceAgents(db, "memories");
|
|
75
|
+
const queuedAgents = readSourceAgents(db, "distill_queue");
|
|
76
|
+
// Row ids the agent groups below claim — the path groups skip them so a row
|
|
77
|
+
// covered by BOTH a rostered agent and a pattern flips (and is counted)
|
|
78
|
+
// exactly once.
|
|
79
|
+
const agentClaimedMem = new Set();
|
|
80
|
+
const agentClaimedQueue = new Set();
|
|
81
|
+
const groups = [];
|
|
82
|
+
for (const [agent, memoryRows] of memoryAgents) {
|
|
83
|
+
if (!(0, classification_js_1.isConfidentialSourceAgent)(agent, roster))
|
|
84
|
+
continue;
|
|
85
|
+
const pending = db
|
|
86
|
+
.prepare("SELECT COUNT(*) AS c FROM memories WHERE source_agent = ? AND privacy != 'confidential'")
|
|
87
|
+
.get(agent).c;
|
|
88
|
+
const queued = queuedAgents.has(agent)
|
|
89
|
+
? db
|
|
90
|
+
.prepare("SELECT COUNT(*) AS c FROM distill_queue WHERE source_agent = ? AND privacy != 'confidential'")
|
|
91
|
+
.get(agent).c
|
|
92
|
+
: 0;
|
|
93
|
+
if (pending === 0 && queued === 0)
|
|
94
|
+
continue;
|
|
95
|
+
if (pending > 0) {
|
|
96
|
+
for (const r of db
|
|
97
|
+
.prepare("SELECT id FROM memories WHERE source_agent = ? AND privacy != 'confidential'")
|
|
98
|
+
.all(agent)) {
|
|
99
|
+
agentClaimedMem.add(r.id);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (queued > 0) {
|
|
103
|
+
for (const r of db
|
|
104
|
+
.prepare("SELECT id FROM distill_queue WHERE source_agent = ? AND privacy != 'confidential'")
|
|
105
|
+
.all(agent)) {
|
|
106
|
+
agentClaimedQueue.add(r.id);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
groups.push({ sourceAgent: agent, memoryRows: pending, queuedRows: queued });
|
|
110
|
+
}
|
|
111
|
+
// Queue-only groups (memories already stamped, inbox holds old rows).
|
|
112
|
+
for (const agent of queuedAgents.keys()) {
|
|
113
|
+
if (memoryAgents.has(agent))
|
|
114
|
+
continue;
|
|
115
|
+
if (!(0, classification_js_1.isConfidentialSourceAgent)(agent, roster))
|
|
116
|
+
continue;
|
|
117
|
+
const queued = db
|
|
118
|
+
.prepare("SELECT COUNT(*) AS c FROM distill_queue WHERE source_agent = ? AND privacy != 'confidential'")
|
|
119
|
+
.get(agent).c;
|
|
120
|
+
if (queued > 0) {
|
|
121
|
+
groups.push({ sourceAgent: agent, memoryRows: 0, queuedRows: queued });
|
|
122
|
+
for (const r of db
|
|
123
|
+
.prepare("SELECT id FROM distill_queue WHERE source_agent = ? AND privacy != 'confidential'")
|
|
124
|
+
.all(agent)) {
|
|
125
|
+
agentClaimedQueue.add(r.id);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
groups.push(...planPathGroups(db, patterns, agentClaimedMem, agentClaimedQueue));
|
|
130
|
+
groups.sort((a, b) => b.memoryRows - a.memoryRows || a.sourceAgent.localeCompare(b.sourceAgent));
|
|
131
|
+
return groups;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* #609: the per-PATTERN stamp groups. Rows whose STORED `workspace_path`
|
|
135
|
+
* matches a configured pattern flip to confidential — matched in JS with the
|
|
136
|
+
* same pure matcher the intake uses (SQL cannot express the three forms),
|
|
137
|
+
* over the not-yet-confidential rows that carry a path. NULL-path rows are
|
|
138
|
+
* never matched (they have no path to match — reported as unmatched, never
|
|
139
|
+
* silently stamped). Rows already claimed by an agent group are skipped
|
|
140
|
+
* (single flip); each remaining row joins its FIRST matching pattern (config
|
|
141
|
+
* order) so overlapping patterns never double-count.
|
|
142
|
+
*/
|
|
143
|
+
function planPathGroups(db, patterns, agentClaimedMem, agentClaimedQueue) {
|
|
144
|
+
if (patterns.length === 0)
|
|
145
|
+
return [];
|
|
146
|
+
const claims = patterns.map(() => ({ memIds: [], queueIds: [] }));
|
|
147
|
+
const memRows = db
|
|
148
|
+
.prepare("SELECT id, workspace_path FROM memories WHERE privacy != 'confidential' AND workspace_path IS NOT NULL")
|
|
149
|
+
.all();
|
|
150
|
+
for (const row of memRows) {
|
|
151
|
+
if (agentClaimedMem.has(row.id))
|
|
152
|
+
continue;
|
|
153
|
+
for (let p = 0; p < patterns.length; p++) {
|
|
154
|
+
if ((0, classification_js_1.matchesConfidentialPath)(row.workspace_path, [patterns[p]])) {
|
|
155
|
+
claims[p].memIds.push(row.id);
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const queueRows = db
|
|
161
|
+
.prepare("SELECT id, workspace_path FROM distill_queue WHERE privacy != 'confidential' AND workspace_path IS NOT NULL")
|
|
162
|
+
.all();
|
|
163
|
+
for (const row of queueRows) {
|
|
164
|
+
if (agentClaimedQueue.has(row.id))
|
|
165
|
+
continue;
|
|
166
|
+
for (let p = 0; p < patterns.length; p++) {
|
|
167
|
+
if ((0, classification_js_1.matchesConfidentialPath)(row.workspace_path, [patterns[p]])) {
|
|
168
|
+
claims[p].queueIds.push(row.id);
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
const groups = [];
|
|
174
|
+
for (let p = 0; p < patterns.length; p++) {
|
|
175
|
+
const { memIds, queueIds } = claims[p];
|
|
176
|
+
if (memIds.length === 0 && queueIds.length === 0)
|
|
177
|
+
continue;
|
|
178
|
+
groups.push({
|
|
179
|
+
// Display label: the pattern's raw config entry.
|
|
180
|
+
sourceAgent: patterns[p].raw,
|
|
181
|
+
pattern: patterns[p],
|
|
182
|
+
memoryRows: memIds.length,
|
|
183
|
+
queuedRows: queueIds.length,
|
|
184
|
+
memoryIds: memIds,
|
|
185
|
+
queuedIds: queueIds,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
return groups;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Run `hicortex backfill-privacy`. Dry run by default (options.apply falsy) —
|
|
192
|
+
* plan + report only, zero writes. `options.apply` executes: backup, capture
|
|
193
|
+
* lock, ONE transaction stamping every planned row in BOTH tables. A busy
|
|
194
|
+
* lock is an abort (reported as `lockBusy: true`, never a wait); a failed
|
|
195
|
+
* backup aborts all writes (throw, like dedup/reconcile-projects).
|
|
196
|
+
*/
|
|
197
|
+
async function runBackfillPrivacy(options = {}) {
|
|
198
|
+
const stateDir = options.stateDir ?? HICORTEX_HOME;
|
|
199
|
+
const config = options.config !== undefined ? options.config : readConfig(stateDir);
|
|
200
|
+
// The roster AND the path patterns come from the SAME resolutions the
|
|
201
|
+
// server uses at boot (resolveConfidentialAgents / resolveConfidentialPaths)
|
|
202
|
+
// — one config shape, one matcher, so the backfill can never disagree with
|
|
203
|
+
// the intake about what is covered.
|
|
204
|
+
const roster = (0, classification_js_1.resolveConfidentialAgents)(config).roster;
|
|
205
|
+
const patterns = (0, classification_js_1.resolveConfidentialPaths)(config).patterns;
|
|
206
|
+
// Server-mode only — client installs have no local DB.
|
|
207
|
+
if (config?.mode === "client") {
|
|
208
|
+
throw new Error("[hicortex] backfill-privacy is server-mode only (it needs the local DB). " +
|
|
209
|
+
`This machine is a client of ${config.serverUrl ?? "a remote server"} — run backfill-privacy on the server.`);
|
|
210
|
+
}
|
|
211
|
+
const apply = options.apply ?? false;
|
|
212
|
+
const dbPath = (0, db_js_1.resolveDbPath)(options.dbPath);
|
|
213
|
+
const db = (0, db_js_1.initDb)(dbPath);
|
|
214
|
+
try {
|
|
215
|
+
console.log(`[hicortex] backfill-privacy starting (${apply ? "APPLY" : "dry-run"}): db ${dbPath}, ` +
|
|
216
|
+
`${roster.length} rostered agent(s)${roster.length > 0 ? ` (${roster.join(", ")})` : ""}` +
|
|
217
|
+
`, ${patterns.length} path pattern(s)${patterns.length > 0 ? ` (${patterns.map((p) => p.raw).join(", ")})` : ""}`);
|
|
218
|
+
if (roster.length === 0 && patterns.length === 0) {
|
|
219
|
+
console.log("[hicortex] backfill-privacy: no confidentialAgents or confidentialPaths configured — nothing to stamp. " +
|
|
220
|
+
"Add agent ids to config.json confidentialAgents and/or path patterns to confidentialPaths first.");
|
|
221
|
+
return { dryRun: !apply, roster, patterns: [], groups: [], totalMemories: 0, totalQueued: 0 };
|
|
222
|
+
}
|
|
223
|
+
const groups = planBackfill(db, roster, patterns);
|
|
224
|
+
const totalMemories = groups.reduce((s, g) => s + g.memoryRows, 0);
|
|
225
|
+
const totalQueued = groups.reduce((s, g) => s + g.queuedRows, 0);
|
|
226
|
+
const report = {
|
|
227
|
+
dryRun: !apply,
|
|
228
|
+
roster,
|
|
229
|
+
patterns: patterns.map((p) => p.raw),
|
|
230
|
+
groups,
|
|
231
|
+
totalMemories,
|
|
232
|
+
totalQueued,
|
|
233
|
+
};
|
|
234
|
+
console.log(`[hicortex] backfill-privacy: ${groups.length} group(s) match the config, ` +
|
|
235
|
+
`${totalMemories} memory row(s)${totalQueued > 0 ? `, ${totalQueued} queued row(s)` : ""} ` +
|
|
236
|
+
`→ confidential (${apply ? "applying" : "dry run — zero writes"})`);
|
|
237
|
+
for (const g of groups) {
|
|
238
|
+
const label = g.pattern ? `pattern ${g.pattern.raw}` : g.sourceAgent;
|
|
239
|
+
console.log(`[hicortex] ${label}: ${g.memoryRows} memory row(s)${g.queuedRows > 0 ? `, ${g.queuedRows} queued` : ""}`);
|
|
240
|
+
}
|
|
241
|
+
if (!apply)
|
|
242
|
+
return report;
|
|
243
|
+
// --apply: single-flight capture lock (waitMs 0) so a concurrent
|
|
244
|
+
// nightly/capture run cannot race the stamps. Busy → abort (reported,
|
|
245
|
+
// never a silent wait — a deliberate manual command is retried by the
|
|
246
|
+
// operator, like dedup/reconcile-projects).
|
|
247
|
+
const acquireLock = options.acquireLock ?? capture_js_1.acquireCaptureLock;
|
|
248
|
+
const releaseLock = await acquireLock(stateDir, 0);
|
|
249
|
+
if (!releaseLock) {
|
|
250
|
+
console.error("[hicortex] backfill-privacy --apply aborted: another capture/nightly run holds the lock. Retry when it finishes.");
|
|
251
|
+
report.lockBusy = true;
|
|
252
|
+
return report;
|
|
253
|
+
}
|
|
254
|
+
try {
|
|
255
|
+
// Backup FIRST — abort entirely (no stamps attempted) if it fails.
|
|
256
|
+
let backupPath;
|
|
257
|
+
try {
|
|
258
|
+
backupPath = await (0, dedup_js_1.takePreDedupBackup)(db, stateDir, config);
|
|
259
|
+
}
|
|
260
|
+
catch (err) {
|
|
261
|
+
throw new Error(`[hicortex] backfill-privacy --apply aborted: backup failed (${err instanceof Error ? err.message : String(err)}). No stamps attempted.`);
|
|
262
|
+
}
|
|
263
|
+
console.log(`[hicortex] Backup written: ${backupPath}`);
|
|
264
|
+
report.backupPath = backupPath;
|
|
265
|
+
// ONE transaction: every group's memory + queue stamps commit together
|
|
266
|
+
// (the plan is order-independent — pure predicates). Agent groups stamp
|
|
267
|
+
// by source_agent; #609 path groups stamp by exact row id (the JS-side
|
|
268
|
+
// matcher planned them — SQL cannot express the pattern forms).
|
|
269
|
+
const stampMem = db.prepare("UPDATE memories SET privacy = 'confidential' WHERE source_agent = ? AND privacy != 'confidential'");
|
|
270
|
+
const stampQueue = db.prepare("UPDATE distill_queue SET privacy = 'confidential' WHERE source_agent = ? AND privacy != 'confidential'");
|
|
271
|
+
const stampMemById = db.prepare("UPDATE memories SET privacy = 'confidential' WHERE id = ? AND privacy != 'confidential'");
|
|
272
|
+
const stampQueueById = db.prepare("UPDATE distill_queue SET privacy = 'confidential' WHERE id = ? AND privacy != 'confidential'");
|
|
273
|
+
const applied = db.transaction(() => {
|
|
274
|
+
let memories = 0;
|
|
275
|
+
let queued = 0;
|
|
276
|
+
for (const g of groups) {
|
|
277
|
+
if (g.pattern) {
|
|
278
|
+
for (const id of g.memoryIds ?? [])
|
|
279
|
+
memories += stampMemById.run(id).changes;
|
|
280
|
+
for (const id of g.queuedIds ?? [])
|
|
281
|
+
queued += stampQueueById.run(id).changes;
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
memories += stampMem.run(g.sourceAgent).changes;
|
|
285
|
+
queued += stampQueue.run(g.sourceAgent).changes;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return { memories, queued };
|
|
289
|
+
})();
|
|
290
|
+
report.appliedMemories = applied.memories;
|
|
291
|
+
report.appliedQueued = applied.queued;
|
|
292
|
+
console.log(`[hicortex] backfill-privacy complete: ${applied.memories} memory row(s) stamped confidential` +
|
|
293
|
+
(applied.queued > 0 ? `, ${applied.queued} queued row(s)` : "") +
|
|
294
|
+
" (nothing is ever stamped back to standard — a roster/pattern removal is not a demotion)");
|
|
295
|
+
return report;
|
|
296
|
+
}
|
|
297
|
+
finally {
|
|
298
|
+
releaseLock();
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
finally {
|
|
302
|
+
db.close();
|
|
303
|
+
}
|
|
304
|
+
}
|
package/dist/capture.d.ts
CHANGED
|
@@ -64,10 +64,70 @@ export interface DistillBody {
|
|
|
64
64
|
session_id: string;
|
|
65
65
|
segment_id: string;
|
|
66
66
|
session_date: string;
|
|
67
|
+
/**
|
|
68
|
+
* #609: the session's workspace path (batch.workspacePath) — the directory
|
|
69
|
+
* the capturing agent worked in. Always sent when known (the server cannot
|
|
70
|
+
* match confidentialPaths or backfill by path without it); null/omitted
|
|
71
|
+
* when the harness has no workspace (Hermes) or the reader predates #609.
|
|
72
|
+
* Old servers ignore unknown body fields by construction.
|
|
73
|
+
*/
|
|
74
|
+
workspace_path?: string | null;
|
|
75
|
+
/**
|
|
76
|
+
* #614: the marker file's relayed CONTENT — readConfidentialMarker's walk
|
|
77
|
+
* result (the trimmed first line of `.hicortex-confidential` at the
|
|
78
|
+
* workspace or an ancestor). "" when the marker exists but is empty (the
|
|
79
|
+
* pre-#614 shape — the server warns on it, never validates it); null when
|
|
80
|
+
* no marker was found. Content = the server's confidentialMarkerKey today,
|
|
81
|
+
* a space-id + key pair later (#613) — the wire field never changes shape.
|
|
82
|
+
* Sent on every segment of the batch; the SERVER validates it against its
|
|
83
|
+
* own key at intake (a client-side match alone is never trusted).
|
|
84
|
+
*/
|
|
85
|
+
client_marker?: string | null;
|
|
86
|
+
/**
|
|
87
|
+
* LEGACY (pre-#614): the marker-walk boolean. Capture STOPPED sending it
|
|
88
|
+
* in #614 — the boolean was the client-writable classification channel the
|
|
89
|
+
* server-side key validation closes; a #614 server accepts-and-ignores it
|
|
90
|
+
* (not even destructured, the #332 client `privacy` precedent). Kept on
|
|
91
|
+
* the interface for the version-skew window exactly like `privacy` below:
|
|
92
|
+
* a #609-era client's `true` against a #614 server reads standard, and a
|
|
93
|
+
* #614 client against a #609-era server reads standard too — fail-closed
|
|
94
|
+
* in both directions (intended; both ends must upgrade for marker-derived
|
|
95
|
+
* confidentiality).
|
|
96
|
+
*/
|
|
97
|
+
client_confidential?: boolean;
|
|
67
98
|
/** 0.16.x: optional/vestigial. The distiller no longer sets it; a legacy
|
|
68
99
|
* client may. Honored if present, else the memory stores NULL. */
|
|
69
100
|
privacy?: string;
|
|
70
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* #614 read cap for the relayed marker content, in BYTES. A marker is ONE
|
|
104
|
+
* short line (the key); anything larger is junk someone planted in the file,
|
|
105
|
+
* not a marker — the walk relays at most this many bytes so a multi-KB blob
|
|
106
|
+
* can never ride the capture body. (Judgment constant — a key comfortably
|
|
107
|
+
* fits; matches the common single-read page intuition.)
|
|
108
|
+
*/
|
|
109
|
+
export declare const MARKER_READ_CAP_BYTES = 4096;
|
|
110
|
+
/**
|
|
111
|
+
* #614 marker walk: does `.hicortex-confidential` (the constant lives in
|
|
112
|
+
* confidential-marker.ts — the command writes it, this walk reads it) exist
|
|
113
|
+
* at `workspacePath` or any ancestor directory up to `/`, and WHAT does it
|
|
114
|
+
* carry? Returns the marker file's trimmed FIRST line — the content the
|
|
115
|
+
* server validates against its `confidentialMarkerKey` (was a boolean walk
|
|
116
|
+
* in #609; #614 made the marker carry a key, so the client relays content
|
|
117
|
+
* and the SERVER decides). "" = a marker exists but is empty (the pre-#614
|
|
118
|
+
* shape — present, never valid); null = no marker found.
|
|
119
|
+
*
|
|
120
|
+
* Same walk semantics as the #609 boolean: a marked parent covers child
|
|
121
|
+
* workspaces, the marker rides folder renames/moves, one bounded read per
|
|
122
|
+
* ancestor, FIRST hit wins, a PLAIN STRING walk with NO symlink resolution
|
|
123
|
+
* (a symlinked workspace must not silently change which directory chain is
|
|
124
|
+
* inspected), null/empty path → null (Hermes, unknown workspace — a signal
|
|
125
|
+
* is never invented), any stat error reads as absent and the walk continues.
|
|
126
|
+
* A stat-successful but UNREADABLE marker relays "" — the marker is present,
|
|
127
|
+
* its content cannot be validated, so the server refuses it (fail-closed:
|
|
128
|
+
* never confidential off a file we could not read).
|
|
129
|
+
*/
|
|
130
|
+
export declare function readConfidentialMarker(workspacePath: string | null | undefined): string | null;
|
|
71
131
|
/** Normalized POST result the caller's transport returns. */
|
|
72
132
|
export interface PostResult {
|
|
73
133
|
status: number;
|
package/dist/capture.js
CHANGED
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
* multi-night simulation can run as a pure unit test with no HTTP listener.
|
|
15
15
|
*/
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.RATE_LIMIT_RETRIES = exports.MIN_SEGMENT_CHARS = exports.SEGMENT_MAX_CHARS = void 0;
|
|
17
|
+
exports.RATE_LIMIT_RETRIES = exports.MARKER_READ_CAP_BYTES = exports.MIN_SEGMENT_CHARS = exports.SEGMENT_MAX_CHARS = void 0;
|
|
18
|
+
exports.readConfidentialMarker = readConfidentialMarker;
|
|
18
19
|
exports.hardSplitText = hardSplitText;
|
|
19
20
|
exports.packSegments = packSegments;
|
|
20
21
|
exports.isTokenBudget429 = isTokenBudget429;
|
|
@@ -22,6 +23,7 @@ exports.captureBatches = captureBatches;
|
|
|
22
23
|
exports.acquireCaptureLock = acquireCaptureLock;
|
|
23
24
|
const node_fs_1 = require("node:fs");
|
|
24
25
|
const node_path_1 = require("node:path");
|
|
26
|
+
const confidential_marker_js_1 = require("./confidential-marker.js");
|
|
25
27
|
const distiller_js_1 = require("./distiller.js");
|
|
26
28
|
/**
|
|
27
29
|
* Max denoised chars per segment. Kept below the server's 80K distill cap
|
|
@@ -43,6 +45,74 @@ exports.SEGMENT_MAX_CHARS = 60_000;
|
|
|
43
45
|
exports.MIN_SEGMENT_CHARS = 200;
|
|
44
46
|
/** Chars added by the "\n\n" joiner extractConversationText places between entries. */
|
|
45
47
|
const JOINER_CHARS = 2;
|
|
48
|
+
/**
|
|
49
|
+
* #614 read cap for the relayed marker content, in BYTES. A marker is ONE
|
|
50
|
+
* short line (the key); anything larger is junk someone planted in the file,
|
|
51
|
+
* not a marker — the walk relays at most this many bytes so a multi-KB blob
|
|
52
|
+
* can never ride the capture body. (Judgment constant — a key comfortably
|
|
53
|
+
* fits; matches the common single-read page intuition.)
|
|
54
|
+
*/
|
|
55
|
+
exports.MARKER_READ_CAP_BYTES = 4096;
|
|
56
|
+
/**
|
|
57
|
+
* #614 marker walk: does `.hicortex-confidential` (the constant lives in
|
|
58
|
+
* confidential-marker.ts — the command writes it, this walk reads it) exist
|
|
59
|
+
* at `workspacePath` or any ancestor directory up to `/`, and WHAT does it
|
|
60
|
+
* carry? Returns the marker file's trimmed FIRST line — the content the
|
|
61
|
+
* server validates against its `confidentialMarkerKey` (was a boolean walk
|
|
62
|
+
* in #609; #614 made the marker carry a key, so the client relays content
|
|
63
|
+
* and the SERVER decides). "" = a marker exists but is empty (the pre-#614
|
|
64
|
+
* shape — present, never valid); null = no marker found.
|
|
65
|
+
*
|
|
66
|
+
* Same walk semantics as the #609 boolean: a marked parent covers child
|
|
67
|
+
* workspaces, the marker rides folder renames/moves, one bounded read per
|
|
68
|
+
* ancestor, FIRST hit wins, a PLAIN STRING walk with NO symlink resolution
|
|
69
|
+
* (a symlinked workspace must not silently change which directory chain is
|
|
70
|
+
* inspected), null/empty path → null (Hermes, unknown workspace — a signal
|
|
71
|
+
* is never invented), any stat error reads as absent and the walk continues.
|
|
72
|
+
* A stat-successful but UNREADABLE marker relays "" — the marker is present,
|
|
73
|
+
* its content cannot be validated, so the server refuses it (fail-closed:
|
|
74
|
+
* never confidential off a file we could not read).
|
|
75
|
+
*/
|
|
76
|
+
function readConfidentialMarker(workspacePath) {
|
|
77
|
+
if (!workspacePath)
|
|
78
|
+
return null;
|
|
79
|
+
let dir = workspacePath;
|
|
80
|
+
for (;;) {
|
|
81
|
+
const markerPath = (0, node_path_1.join)(dir, confidential_marker_js_1.CONFIDENTIAL_MARKER_FILENAME);
|
|
82
|
+
try {
|
|
83
|
+
if ((0, node_fs_1.statSync)(markerPath).isFile()) {
|
|
84
|
+
try {
|
|
85
|
+
// Bounded read: open + readSync at the cap, never readFileSync (a
|
|
86
|
+
// planted "marker" of arbitrary size must not be buffered whole).
|
|
87
|
+
const fd = (0, node_fs_1.openSync)(markerPath, "r");
|
|
88
|
+
try {
|
|
89
|
+
const buf = Buffer.alloc(exports.MARKER_READ_CAP_BYTES);
|
|
90
|
+
const n = (0, node_fs_1.readSync)(fd, buf, 0, exports.MARKER_READ_CAP_BYTES, 0);
|
|
91
|
+
// First line only, trimmed: "key\n…" → "key" (CRLF folds through
|
|
92
|
+
// the trim); an empty file → "".
|
|
93
|
+
return buf.subarray(0, n).toString("utf-8").split("\n")[0].trim();
|
|
94
|
+
}
|
|
95
|
+
finally {
|
|
96
|
+
(0, node_fs_1.closeSync)(fd);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
// Present but unreadable — see the doc comment: "" (refused), not
|
|
101
|
+
// absent (the marker IS there; pretending otherwise would let a
|
|
102
|
+
// chmod decide confidentiality).
|
|
103
|
+
return "";
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
/* absent — walk on */
|
|
109
|
+
}
|
|
110
|
+
const parent = (0, node_path_1.dirname)(dir);
|
|
111
|
+
if (parent === dir)
|
|
112
|
+
return null; // reached "/"
|
|
113
|
+
dir = parent;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
46
116
|
/**
|
|
47
117
|
* Split an already-denoised string into ≤maxChars pieces (A2 hard-split).
|
|
48
118
|
* Prefers paragraph, then line, then hard boundaries — mirrors the distiller's
|
|
@@ -243,6 +313,16 @@ async function captureBatches(batches, opts) {
|
|
|
243
313
|
// has no prefix — keeps ids byte-identical to first-cut and to any already
|
|
244
314
|
// stored on an older server.
|
|
245
315
|
const genPrefix = batch.generation > 0 ? `g${batch.generation}.` : "";
|
|
316
|
+
// #609/#614: the workspace birthplace signals, computed ONCE per batch
|
|
317
|
+
// before the segment loop — the marker walk reads the workspace and each
|
|
318
|
+
// ancestor (first hit wins), and both values ride EVERY segment POST so
|
|
319
|
+
// the server can compose its confidentialPaths rule and validate the
|
|
320
|
+
// marker key. null workspace (Hermes / unknown) sends a null path and a
|
|
321
|
+
// null marker — never invented. #614: the client RELAYS marker content
|
|
322
|
+
// (client_marker); it no longer asserts a confidentiality boolean —
|
|
323
|
+
// validation is server-side.
|
|
324
|
+
const workspacePath = batch.workspacePath ?? null;
|
|
325
|
+
const markerContent = readConfidentialMarker(workspacePath);
|
|
246
326
|
// Cursor value the last confirmed boundary reached — advanced once, at
|
|
247
327
|
// session end (A4), so per-session file writes stay bounded.
|
|
248
328
|
let lastConfirmedEnd = batch.startCursor;
|
|
@@ -281,6 +361,15 @@ async function captureBatches(batches, opts) {
|
|
|
281
361
|
session_id: batch.sessionId,
|
|
282
362
|
segment_id: `${genPrefix}${seg.segStart}-${seg.segEnd}${seg.idSuffix}`,
|
|
283
363
|
session_date: batch.date,
|
|
364
|
+
// #609/#614: both workspace signals on every segment (see the
|
|
365
|
+
// per-batch computation above) — the path verbatim, the marker's
|
|
366
|
+
// relayed CONTENT. Old servers never read unknown fields; the
|
|
367
|
+
// pre-#614 client_confidential boolean is deliberately NOT sent
|
|
368
|
+
// (DistillBody keeps the field documented as legacy for the skew
|
|
369
|
+
// window — a #609 server reading only the boolean sees standard,
|
|
370
|
+
// fail-closed, intended).
|
|
371
|
+
workspace_path: workspacePath,
|
|
372
|
+
client_marker: markerContent,
|
|
284
373
|
};
|
|
285
374
|
let result;
|
|
286
375
|
try {
|