@remnic/core 9.3.608 → 9.3.609

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.
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  Orchestrator
3
- } from "./chunk-YJ6QCQNE.js";
3
+ } from "./chunk-HJNQQICM.js";
4
4
  import "./chunk-5RIRL3XL.js";
5
5
  import "./chunk-KVEVLBKC.js";
6
6
  import "./chunk-BFBF3XEF.js";
@@ -47,7 +47,7 @@ import "./chunk-VU3SVYMA.js";
47
47
  import "./chunk-H63EDPFJ.js";
48
48
  import "./chunk-PD6O7AXF.js";
49
49
  import "./chunk-YAZNBMNF.js";
50
- import "./chunk-LCR46JY5.js";
50
+ import "./chunk-7YX23JBA.js";
51
51
  import "./chunk-C4SQJZAF.js";
52
52
  import "./chunk-KM2A35EO.js";
53
53
  import "./chunk-4RA3C3EV.js";
@@ -5,7 +5,7 @@ import {
5
5
  // src/extraction-judge-training.ts
6
6
  import path from "path";
7
7
  import { homedir } from "os";
8
- import { appendFile, chmod, mkdir, readFile, readdir } from "fs/promises";
8
+ import { appendFile, chmod, lstat, mkdir, readFile, readdir, realpath } from "fs/promises";
9
9
  function expandTilde(p) {
10
10
  const home = homedir();
11
11
  if (p === "~" || p.startsWith("~/") || p.startsWith("~\\")) {
@@ -36,6 +36,10 @@ function dateStamp(iso) {
36
36
  function trainingFilePathFor(directory, iso) {
37
37
  return path.join(directory, `${dateStamp(iso)}.jsonl`);
38
38
  }
39
+ function isPathInsideDirectory(filePath, directory) {
40
+ const relative = path.relative(directory, filePath);
41
+ return relative === "" || !relative.startsWith("..") && !path.isAbsolute(relative);
42
+ }
39
43
  async function recordJudgeTrainingPair(row, options) {
40
44
  if (!options.enabled) return;
41
45
  const dir = resolveTrainingDir(options);
@@ -56,6 +60,19 @@ async function recordJudgeTrainingPair(row, options) {
56
60
  }
57
61
  async function readJudgeTrainingPairs(options) {
58
62
  const dir = resolveTrainingDir({ enabled: true, ...options });
63
+ try {
64
+ const dirStat = await lstat(dir);
65
+ if (dirStat.isSymbolicLink()) {
66
+ throw new Error("Judge training directory must not be a symlink");
67
+ }
68
+ if (!dirStat.isDirectory()) {
69
+ throw new Error("Judge training path must be a directory");
70
+ }
71
+ } catch (err) {
72
+ const code = err.code;
73
+ if (code === "ENOENT") return { rows: [], malformed: 0 };
74
+ throw err;
75
+ }
59
76
  let entries;
60
77
  try {
61
78
  entries = await readdir(dir);
@@ -66,10 +83,30 @@ async function readJudgeTrainingPairs(options) {
66
83
  }
67
84
  const rows = [];
68
85
  let malformed = 0;
86
+ const resolvedDir = await realpath(dir);
69
87
  entries.sort();
70
88
  for (const name of entries) {
71
89
  if (!name.endsWith(".jsonl")) continue;
72
- const raw = await readFile(path.join(dir, name), "utf-8");
90
+ const filePath = path.join(dir, name);
91
+ let fileStat;
92
+ try {
93
+ fileStat = await lstat(filePath);
94
+ } catch (err) {
95
+ const code = err.code;
96
+ if (code === "ENOENT") continue;
97
+ throw err;
98
+ }
99
+ if (fileStat.isSymbolicLink() || !fileStat.isFile()) continue;
100
+ let resolvedFilePath;
101
+ try {
102
+ resolvedFilePath = await realpath(filePath);
103
+ } catch (err) {
104
+ const code = err.code;
105
+ if (code === "ENOENT") continue;
106
+ throw err;
107
+ }
108
+ if (!isPathInsideDirectory(resolvedFilePath, resolvedDir)) continue;
109
+ const raw = await readFile(resolvedFilePath, "utf-8");
73
110
  for (const line of raw.split("\n")) {
74
111
  if (!line.trim()) continue;
75
112
  let parsed;
@@ -120,4 +157,4 @@ export {
120
157
  readJudgeTrainingPairs,
121
158
  isValidTrainingPair
122
159
  };
123
- //# sourceMappingURL=chunk-LCR46JY5.js.map
160
+ //# sourceMappingURL=chunk-7YX23JBA.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/extraction-judge-training.ts"],"sourcesContent":["/**\n * Extraction Judge Training Data Shim (issue #562, PR 4).\n *\n * Opt-in collector for `(candidate_text, verdict_kind, reason,\n * ground_truth_label?)` tuples. Rows are appended to JSONL files under\n * `~/.remnic/judge-training/<YYYY-MM-DD>.jsonl` so operators can ship the\n * data into a future GRPO training pipeline without exfiltrating live\n * memory content through the regular observation ledger.\n *\n * Gating:\n * - Off by default. Must be explicitly enabled via\n * `collectJudgeTrainingPairs: true` in plugin config.\n * - The ground-truth label is always optional — labels are added out-of-\n * band once reviewers disambiguate the candidate's fate.\n *\n * Privacy: the row carries only what the judge already sees — the\n * candidate text and its metadata. It does NOT carry session keys,\n * principal IDs, or any user identifiers. The file lives in the user's\n * home directory rather than the shared memory directory so it is never\n * committed, sync'd, or bundled into exports.\n */\n\nimport path from \"node:path\";\nimport { homedir } from \"node:os\";\nimport { appendFile, chmod, lstat, mkdir, readFile, readdir, realpath } from \"node:fs/promises\";\nimport { log } from \"./logger.js\";\nimport type { JudgeVerdictKind } from \"./extraction-judge.js\";\n\n/**\n * Persisted training row. Intentionally minimal: just the signal needed\n * to train a judge replacement policy. Schema version is tagged so future\n * readers can migrate older rows.\n */\nexport interface JudgeTrainingPair {\n version: 1;\n ts: string; // ISO-8601\n candidateText: string;\n candidateCategory: string;\n candidateConfidence?: number;\n verdictKind: JudgeVerdictKind;\n reason: string;\n /**\n * Number of prior deferrals when the verdict was resolved. `0` for the\n * first resolution; only set when known (defer pathway).\n */\n priorDeferrals?: number;\n /**\n * Optional human-applied ground-truth label. Added after the fact by a\n * reviewer / labelling script; not present on fresh rows.\n */\n groundTruthLabel?: JudgeVerdictKind;\n}\n\nexport interface JudgeTrainingOptions {\n enabled: boolean;\n /**\n * Override for the output directory. Defaults to\n * `~/.remnic/judge-training`. Tests pass a temp path here.\n */\n directory?: string;\n}\n\n/**\n * Expand a leading `~` / `~/` / `$HOME/` / `${HOME}/` to the process home\n * directory. Node's `fs` APIs do not expand `~` themselves (CLAUDE.md\n * gotcha 17), so every user-facing path input must be funnelled through\n * this helper before it reaches the filesystem.\n */\nfunction expandTilde(p: string): string {\n const home = homedir();\n if (p === \"~\" || p.startsWith(\"~/\") || p.startsWith(\"~\\\\\")) {\n return home + p.slice(1);\n }\n if (p === \"$HOME\" || p.startsWith(\"$HOME/\") || p.startsWith(\"$HOME\\\\\")) {\n return home + p.slice(5);\n }\n if (p === \"${HOME}\" || p.startsWith(\"${HOME}/\") || p.startsWith(\"${HOME}\\\\\")) {\n return home + p.slice(7);\n }\n return p;\n}\n\nexport function resolveTrainingDir(options: JudgeTrainingOptions): string {\n if (options.directory && options.directory.length > 0) {\n // Expand `~` / `$HOME` in the override so operators can write the\n // config as the user sees it (CLAUDE.md gotcha 17).\n return expandTilde(options.directory);\n }\n return path.join(homedir(), \".remnic\", \"judge-training\");\n}\n\nfunction dateStamp(iso: string): string {\n // `YYYY-MM-DD` from an ISO-8601 string. Falls back to today on a parse\n // failure rather than throwing — the caller already wrote a row and the\n // timestamp is best-effort.\n const ms = Date.parse(iso);\n const d = Number.isFinite(ms) ? new Date(ms) : new Date();\n const yyyy = d.getUTCFullYear().toString().padStart(4, \"0\");\n const mm = (d.getUTCMonth() + 1).toString().padStart(2, \"0\");\n const dd = d.getUTCDate().toString().padStart(2, \"0\");\n return `${yyyy}-${mm}-${dd}`;\n}\n\nexport function trainingFilePathFor(directory: string, iso: string): string {\n return path.join(directory, `${dateStamp(iso)}.jsonl`);\n}\n\nfunction isPathInsideDirectory(filePath: string, directory: string): boolean {\n const relative = path.relative(directory, filePath);\n return relative === \"\" || (!relative.startsWith(\"..\") && !path.isAbsolute(relative));\n}\n\n/**\n * Append a single training row. Fails open — write errors are logged at\n * debug level and swallowed, same policy as the telemetry emitter.\n * No-op when `options.enabled` is false.\n */\nexport async function recordJudgeTrainingPair(row: JudgeTrainingPair, options: JudgeTrainingOptions): Promise<void> {\n if (!options.enabled) return;\n const dir = resolveTrainingDir(options);\n const filePath = trainingFilePathFor(dir, row.ts);\n try {\n await mkdir(dir, { recursive: true, mode: 0o700 });\n await appendFile(filePath, `${JSON.stringify(row)}\\n`, {\n encoding: \"utf-8\",\n mode: 0o600,\n });\n await chmod(filePath, 0o600);\n } catch (err) {\n log.debug(\n `extraction-judge-training: append failed (non-fatal): ${err instanceof Error ? err.message : String(err)}`\n );\n }\n}\n\n/**\n * Read all training rows from the configured directory. Returns an empty\n * array when the directory is missing. Malformed lines are skipped and\n * counted in the returned `malformed` tally.\n */\nexport async function readJudgeTrainingPairs(\n options: Pick<JudgeTrainingOptions, \"directory\">\n): Promise<{ rows: JudgeTrainingPair[]; malformed: number }> {\n const dir = resolveTrainingDir({ enabled: true, ...options });\n try {\n const dirStat = await lstat(dir);\n if (dirStat.isSymbolicLink()) {\n throw new Error(\"Judge training directory must not be a symlink\");\n }\n if (!dirStat.isDirectory()) {\n throw new Error(\"Judge training path must be a directory\");\n }\n } catch (err) {\n const code = (err as NodeJS.ErrnoException).code;\n if (code === \"ENOENT\") return { rows: [], malformed: 0 };\n throw err;\n }\n\n let entries: string[];\n try {\n entries = await readdir(dir);\n } catch (err) {\n const code = (err as NodeJS.ErrnoException).code;\n if (code === \"ENOENT\") return { rows: [], malformed: 0 };\n throw err;\n }\n\n const rows: JudgeTrainingPair[] = [];\n let malformed = 0;\n const resolvedDir = await realpath(dir);\n // Sort so reads are deterministic across platforms.\n entries.sort();\n for (const name of entries) {\n if (!name.endsWith(\".jsonl\")) continue;\n const filePath = path.join(dir, name);\n let fileStat: Awaited<ReturnType<typeof lstat>>;\n try {\n fileStat = await lstat(filePath);\n } catch (err) {\n const code = (err as NodeJS.ErrnoException).code;\n if (code === \"ENOENT\") continue;\n throw err;\n }\n if (fileStat.isSymbolicLink() || !fileStat.isFile()) continue;\n\n let resolvedFilePath: string;\n try {\n resolvedFilePath = await realpath(filePath);\n } catch (err) {\n const code = (err as NodeJS.ErrnoException).code;\n if (code === \"ENOENT\") continue;\n throw err;\n }\n if (!isPathInsideDirectory(resolvedFilePath, resolvedDir)) continue;\n\n const raw = await readFile(resolvedFilePath, \"utf-8\");\n for (const line of raw.split(\"\\n\")) {\n if (!line.trim()) continue;\n let parsed: unknown;\n try {\n parsed = JSON.parse(line);\n } catch {\n malformed += 1;\n continue;\n }\n if (!isValidTrainingPair(parsed)) {\n malformed += 1;\n continue;\n }\n rows.push(parsed);\n }\n }\n return { rows, malformed };\n}\n\n/**\n * Structural validator matching the persisted schema. Forward-compat: an\n * unknown `verdictKind` string is treated as malformed (strict training\n * signal — we do not want to admit unlabelled gibberish into a trainer).\n */\nexport function isValidTrainingPair(value: unknown): value is JudgeTrainingPair {\n if (typeof value !== \"object\" || value === null || Array.isArray(value)) {\n return false;\n }\n const p = value as Record<string, unknown>;\n if (p.version !== 1) return false;\n if (typeof p.ts !== \"string\") return false;\n if (typeof p.candidateText !== \"string\") return false;\n if (typeof p.candidateCategory !== \"string\") return false;\n if (p.verdictKind !== \"accept\" && p.verdictKind !== \"reject\" && p.verdictKind !== \"defer\") {\n return false;\n }\n if (typeof p.reason !== \"string\") return false;\n if (p.candidateConfidence !== undefined && typeof p.candidateConfidence !== \"number\") {\n return false;\n }\n if (p.priorDeferrals !== undefined && typeof p.priorDeferrals !== \"number\") {\n return false;\n }\n if (\n p.groundTruthLabel !== undefined &&\n p.groundTruthLabel !== \"accept\" &&\n p.groundTruthLabel !== \"reject\" &&\n p.groundTruthLabel !== \"defer\"\n ) {\n return false;\n }\n return true;\n}\n"],"mappings":";;;;;AAsBA,OAAO,UAAU;AACjB,SAAS,eAAe;AACxB,SAAS,YAAY,OAAO,OAAO,OAAO,UAAU,SAAS,gBAAgB;AA4C7E,SAAS,YAAY,GAAmB;AACtC,QAAM,OAAO,QAAQ;AACrB,MAAI,MAAM,OAAO,EAAE,WAAW,IAAI,KAAK,EAAE,WAAW,KAAK,GAAG;AAC1D,WAAO,OAAO,EAAE,MAAM,CAAC;AAAA,EACzB;AACA,MAAI,MAAM,WAAW,EAAE,WAAW,QAAQ,KAAK,EAAE,WAAW,SAAS,GAAG;AACtE,WAAO,OAAO,EAAE,MAAM,CAAC;AAAA,EACzB;AACA,MAAI,MAAM,aAAa,EAAE,WAAW,UAAU,KAAK,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vB,EAAE,qBAAqB,YACvB,EAAE,qBAAqB,YACvB,EAAE,qBAAqB,SACvB;AACA,WAAO;AAAA,EACT;AACA,SAAO;AACT;","names":[]}
@@ -140,7 +140,7 @@ import {
140
140
  } from "./chunk-YAZNBMNF.js";
141
141
  import {
142
142
  recordJudgeTrainingPair
143
- } from "./chunk-LCR46JY5.js";
143
+ } from "./chunk-7YX23JBA.js";
144
144
  import {
145
145
  createDeferCountMap,
146
146
  createVerdictCache,
@@ -12441,4 +12441,4 @@ export {
12441
12441
  resolvePersistedMemoryRelativePath,
12442
12442
  Orchestrator
12443
12443
  };
12444
- //# sourceMappingURL=chunk-YJ6QCQNE.js.map
12444
+ //# sourceMappingURL=chunk-HJNQQICM.js.map
@@ -4,7 +4,7 @@ import {
4
4
  recordJudgeTrainingPair,
5
5
  resolveTrainingDir,
6
6
  trainingFilePathFor
7
- } from "./chunk-LCR46JY5.js";
7
+ } from "./chunk-7YX23JBA.js";
8
8
  import "./chunk-2ODBA7MQ.js";
9
9
  import "./chunk-PZ5AY32C.js";
10
10
  export {
package/dist/index.js CHANGED
@@ -182,7 +182,7 @@ import {
182
182
  saveTaxonomy,
183
183
  validateSlug,
184
184
  validateTaxonomy
185
- } from "./chunk-YJ6QCQNE.js";
185
+ } from "./chunk-HJNQQICM.js";
186
186
  import "./chunk-5RIRL3XL.js";
187
187
  import {
188
188
  migrateFromEngram,
@@ -265,7 +265,7 @@ import {
265
265
  planRecallMode
266
266
  } from "./chunk-PD6O7AXF.js";
267
267
  import "./chunk-YAZNBMNF.js";
268
- import "./chunk-LCR46JY5.js";
268
+ import "./chunk-7YX23JBA.js";
269
269
  import {
270
270
  clearVerdictCache,
271
271
  createVerdictCache,
@@ -26,7 +26,7 @@ import {
26
26
  sanitizeSessionKeyForFilename,
27
27
  shouldFilterLifecycleRecallCandidate,
28
28
  summarizeGraphShadowComparison
29
- } from "./chunk-YJ6QCQNE.js";
29
+ } from "./chunk-HJNQQICM.js";
30
30
  import "./chunk-5RIRL3XL.js";
31
31
  import "./chunk-KVEVLBKC.js";
32
32
  import "./chunk-BFBF3XEF.js";
@@ -73,7 +73,7 @@ import "./chunk-VU3SVYMA.js";
73
73
  import "./chunk-H63EDPFJ.js";
74
74
  import "./chunk-PD6O7AXF.js";
75
75
  import "./chunk-YAZNBMNF.js";
76
- import "./chunk-LCR46JY5.js";
76
+ import "./chunk-7YX23JBA.js";
77
77
  import "./chunk-C4SQJZAF.js";
78
78
  import "./chunk-KM2A35EO.js";
79
79
  import "./chunk-4RA3C3EV.js";
package/dist/schemas.d.ts CHANGED
@@ -275,12 +275,12 @@ declare const EntityMentionSchema: z.ZodObject<{
275
275
  title: z.ZodString;
276
276
  facts: z.ZodArray<z.ZodString, "many">;
277
277
  }, "strip", z.ZodTypeAny, {
278
- title: string;
279
278
  key: string;
279
+ title: string;
280
280
  facts: string[];
281
281
  }, {
282
- title: string;
283
282
  key: string;
283
+ title: string;
284
284
  facts: string[];
285
285
  }>, "many">>>;
286
286
  }, "strip", z.ZodTypeAny, {
@@ -288,8 +288,8 @@ declare const EntityMentionSchema: z.ZodObject<{
288
288
  name: string;
289
289
  facts: string[];
290
290
  structuredSections?: {
291
- title: string;
292
291
  key: string;
292
+ title: string;
293
293
  facts: string[];
294
294
  }[] | null | undefined;
295
295
  promptedByQuestion?: string | null | undefined;
@@ -298,8 +298,8 @@ declare const EntityMentionSchema: z.ZodObject<{
298
298
  name: string;
299
299
  facts: string[];
300
300
  structuredSections?: {
301
- title: string;
302
301
  key: string;
302
+ title: string;
303
303
  facts: string[];
304
304
  }[] | null | undefined;
305
305
  promptedByQuestion?: string | null | undefined;
@@ -584,12 +584,12 @@ declare const ProactiveExtractionResultSchema: z.ZodObject<{
584
584
  title: z.ZodString;
585
585
  facts: z.ZodArray<z.ZodString, "many">;
586
586
  }, "strip", z.ZodTypeAny, {
587
- title: string;
588
587
  key: string;
588
+ title: string;
589
589
  facts: string[];
590
590
  }, {
591
- title: string;
592
591
  key: string;
592
+ title: string;
593
593
  facts: string[];
594
594
  }>, "many">>>;
595
595
  }, "strip", z.ZodTypeAny, {
@@ -597,8 +597,8 @@ declare const ProactiveExtractionResultSchema: z.ZodObject<{
597
597
  name: string;
598
598
  facts: string[];
599
599
  structuredSections?: {
600
- title: string;
601
600
  key: string;
601
+ title: string;
602
602
  facts: string[];
603
603
  }[] | null | undefined;
604
604
  promptedByQuestion?: string | null | undefined;
@@ -607,8 +607,8 @@ declare const ProactiveExtractionResultSchema: z.ZodObject<{
607
607
  name: string;
608
608
  facts: string[];
609
609
  structuredSections?: {
610
- title: string;
611
610
  key: string;
611
+ title: string;
612
612
  facts: string[];
613
613
  }[] | null | undefined;
614
614
  promptedByQuestion?: string | null | undefined;
@@ -665,8 +665,8 @@ declare const ProactiveExtractionResultSchema: z.ZodObject<{
665
665
  name: string;
666
666
  facts: string[];
667
667
  structuredSections?: {
668
- title: string;
669
668
  key: string;
669
+ title: string;
670
670
  facts: string[];
671
671
  }[] | null | undefined;
672
672
  promptedByQuestion?: string | null | undefined;
@@ -714,8 +714,8 @@ declare const ProactiveExtractionResultSchema: z.ZodObject<{
714
714
  name: string;
715
715
  facts: string[];
716
716
  structuredSections?: {
717
- title: string;
718
717
  key: string;
718
+ title: string;
719
719
  facts: string[];
720
720
  }[] | null | undefined;
721
721
  promptedByQuestion?: string | null | undefined;
@@ -952,12 +952,12 @@ declare const ExtractionResultSchema: z.ZodObject<{
952
952
  title: z.ZodString;
953
953
  facts: z.ZodArray<z.ZodString, "many">;
954
954
  }, "strip", z.ZodTypeAny, {
955
- title: string;
956
955
  key: string;
956
+ title: string;
957
957
  facts: string[];
958
958
  }, {
959
- title: string;
960
959
  key: string;
960
+ title: string;
961
961
  facts: string[];
962
962
  }>, "many">>>;
963
963
  }, "strip", z.ZodTypeAny, {
@@ -965,8 +965,8 @@ declare const ExtractionResultSchema: z.ZodObject<{
965
965
  name: string;
966
966
  facts: string[];
967
967
  structuredSections?: {
968
- title: string;
969
968
  key: string;
969
+ title: string;
970
970
  facts: string[];
971
971
  }[] | null | undefined;
972
972
  promptedByQuestion?: string | null | undefined;
@@ -975,8 +975,8 @@ declare const ExtractionResultSchema: z.ZodObject<{
975
975
  name: string;
976
976
  facts: string[];
977
977
  structuredSections?: {
978
- title: string;
979
978
  key: string;
979
+ title: string;
980
980
  facts: string[];
981
981
  }[] | null | undefined;
982
982
  promptedByQuestion?: string | null | undefined;
@@ -1047,8 +1047,8 @@ declare const ExtractionResultSchema: z.ZodObject<{
1047
1047
  name: string;
1048
1048
  facts: string[];
1049
1049
  structuredSections?: {
1050
- title: string;
1051
1050
  key: string;
1051
+ title: string;
1052
1052
  facts: string[];
1053
1053
  }[] | null | undefined;
1054
1054
  promptedByQuestion?: string | null | undefined;
@@ -1102,8 +1102,8 @@ declare const ExtractionResultSchema: z.ZodObject<{
1102
1102
  name: string;
1103
1103
  facts: string[];
1104
1104
  structuredSections?: {
1105
- title: string;
1106
1105
  key: string;
1106
+ title: string;
1107
1107
  facts: string[];
1108
1108
  }[] | null | undefined;
1109
1109
  promptedByQuestion?: string | null | undefined;
@@ -1172,12 +1172,12 @@ declare const ConsolidationResultSchema: z.ZodObject<{
1172
1172
  title: z.ZodString;
1173
1173
  facts: z.ZodArray<z.ZodString, "many">;
1174
1174
  }, "strip", z.ZodTypeAny, {
1175
- title: string;
1176
1175
  key: string;
1176
+ title: string;
1177
1177
  facts: string[];
1178
1178
  }, {
1179
- title: string;
1180
1179
  key: string;
1180
+ title: string;
1181
1181
  facts: string[];
1182
1182
  }>, "many">>>;
1183
1183
  }, "strip", z.ZodTypeAny, {
@@ -1185,8 +1185,8 @@ declare const ConsolidationResultSchema: z.ZodObject<{
1185
1185
  name: string;
1186
1186
  facts: string[];
1187
1187
  structuredSections?: {
1188
- title: string;
1189
1188
  key: string;
1189
+ title: string;
1190
1190
  facts: string[];
1191
1191
  }[] | null | undefined;
1192
1192
  promptedByQuestion?: string | null | undefined;
@@ -1195,8 +1195,8 @@ declare const ConsolidationResultSchema: z.ZodObject<{
1195
1195
  name: string;
1196
1196
  facts: string[];
1197
1197
  structuredSections?: {
1198
- title: string;
1199
1198
  key: string;
1199
+ title: string;
1200
1200
  facts: string[];
1201
1201
  }[] | null | undefined;
1202
1202
  promptedByQuestion?: string | null | undefined;
@@ -1215,8 +1215,8 @@ declare const ConsolidationResultSchema: z.ZodObject<{
1215
1215
  name: string;
1216
1216
  facts: string[];
1217
1217
  structuredSections?: {
1218
- title: string;
1219
1218
  key: string;
1219
+ title: string;
1220
1220
  facts: string[];
1221
1221
  }[] | null | undefined;
1222
1222
  promptedByQuestion?: string | null | undefined;
@@ -1235,8 +1235,8 @@ declare const ConsolidationResultSchema: z.ZodObject<{
1235
1235
  name: string;
1236
1236
  facts: string[];
1237
1237
  structuredSections?: {
1238
- title: string;
1239
1238
  key: string;
1239
+ title: string;
1240
1240
  facts: string[];
1241
1241
  }[] | null | undefined;
1242
1242
  promptedByQuestion?: string | null | undefined;
@@ -313,13 +313,13 @@ declare const CapsuleBlockSchema: z.ZodObject<{
313
313
  peerProfiles: boolean;
314
314
  }>;
315
315
  }, "strip", z.ZodTypeAny, {
316
- schemaVersion: string;
317
316
  includes: {
318
317
  procedural: boolean;
319
318
  taxonomy: boolean;
320
319
  identityAnchors: boolean;
321
320
  peerProfiles: boolean;
322
321
  };
322
+ schemaVersion: string;
323
323
  id: string;
324
324
  description: string;
325
325
  version: string;
@@ -334,13 +334,13 @@ declare const CapsuleBlockSchema: z.ZodObject<{
334
334
  directAnswerEnabled: boolean;
335
335
  };
336
336
  }, {
337
- schemaVersion: string;
338
337
  includes: {
339
338
  procedural: boolean;
340
339
  taxonomy: boolean;
341
340
  identityAnchors: boolean;
342
341
  peerProfiles: boolean;
343
342
  };
343
+ schemaVersion: string;
344
344
  id: string;
345
345
  description: string;
346
346
  version: string;
@@ -464,13 +464,13 @@ declare const ExportManifestV2Schema: z.ZodObject<{
464
464
  peerProfiles: boolean;
465
465
  }>;
466
466
  }, "strip", z.ZodTypeAny, {
467
- schemaVersion: string;
468
467
  includes: {
469
468
  procedural: boolean;
470
469
  taxonomy: boolean;
471
470
  identityAnchors: boolean;
472
471
  peerProfiles: boolean;
473
472
  };
473
+ schemaVersion: string;
474
474
  id: string;
475
475
  description: string;
476
476
  version: string;
@@ -485,13 +485,13 @@ declare const ExportManifestV2Schema: z.ZodObject<{
485
485
  directAnswerEnabled: boolean;
486
486
  };
487
487
  }, {
488
- schemaVersion: string;
489
488
  includes: {
490
489
  procedural: boolean;
491
490
  taxonomy: boolean;
492
491
  identityAnchors: boolean;
493
492
  peerProfiles: boolean;
494
493
  };
494
+ schemaVersion: string;
495
495
  id: string;
496
496
  description: string;
497
497
  version: string;
@@ -518,13 +518,13 @@ declare const ExportManifestV2Schema: z.ZodObject<{
518
518
  pluginVersion: string;
519
519
  includesTranscripts: boolean;
520
520
  capsule: {
521
- schemaVersion: string;
522
521
  includes: {
523
522
  procedural: boolean;
524
523
  taxonomy: boolean;
525
524
  identityAnchors: boolean;
526
525
  peerProfiles: boolean;
527
526
  };
527
+ schemaVersion: string;
528
528
  id: string;
529
529
  description: string;
530
530
  version: string;
@@ -551,13 +551,13 @@ declare const ExportManifestV2Schema: z.ZodObject<{
551
551
  pluginVersion: string;
552
552
  includesTranscripts: boolean;
553
553
  capsule: {
554
- schemaVersion: string;
555
554
  includes: {
556
555
  procedural: boolean;
557
556
  taxonomy: boolean;
558
557
  identityAnchors: boolean;
559
558
  peerProfiles: boolean;
560
559
  };
560
+ schemaVersion: string;
561
561
  id: string;
562
562
  description: string;
563
563
  version: string;
@@ -683,13 +683,13 @@ declare const ExportBundleV2Schema: z.ZodObject<{
683
683
  peerProfiles: boolean;
684
684
  }>;
685
685
  }, "strip", z.ZodTypeAny, {
686
- schemaVersion: string;
687
686
  includes: {
688
687
  procedural: boolean;
689
688
  taxonomy: boolean;
690
689
  identityAnchors: boolean;
691
690
  peerProfiles: boolean;
692
691
  };
692
+ schemaVersion: string;
693
693
  id: string;
694
694
  description: string;
695
695
  version: string;
@@ -704,13 +704,13 @@ declare const ExportBundleV2Schema: z.ZodObject<{
704
704
  directAnswerEnabled: boolean;
705
705
  };
706
706
  }, {
707
- schemaVersion: string;
708
707
  includes: {
709
708
  procedural: boolean;
710
709
  taxonomy: boolean;
711
710
  identityAnchors: boolean;
712
711
  peerProfiles: boolean;
713
712
  };
713
+ schemaVersion: string;
714
714
  id: string;
715
715
  description: string;
716
716
  version: string;
@@ -737,13 +737,13 @@ declare const ExportBundleV2Schema: z.ZodObject<{
737
737
  pluginVersion: string;
738
738
  includesTranscripts: boolean;
739
739
  capsule: {
740
- schemaVersion: string;
741
740
  includes: {
742
741
  procedural: boolean;
743
742
  taxonomy: boolean;
744
743
  identityAnchors: boolean;
745
744
  peerProfiles: boolean;
746
745
  };
746
+ schemaVersion: string;
747
747
  id: string;
748
748
  description: string;
749
749
  version: string;
@@ -770,13 +770,13 @@ declare const ExportBundleV2Schema: z.ZodObject<{
770
770
  pluginVersion: string;
771
771
  includesTranscripts: boolean;
772
772
  capsule: {
773
- schemaVersion: string;
774
773
  includes: {
775
774
  procedural: boolean;
776
775
  taxonomy: boolean;
777
776
  identityAnchors: boolean;
778
777
  peerProfiles: boolean;
779
778
  };
779
+ schemaVersion: string;
780
780
  id: string;
781
781
  description: string;
782
782
  version: string;
@@ -815,13 +815,13 @@ declare const ExportBundleV2Schema: z.ZodObject<{
815
815
  pluginVersion: string;
816
816
  includesTranscripts: boolean;
817
817
  capsule: {
818
- schemaVersion: string;
819
818
  includes: {
820
819
  procedural: boolean;
821
820
  taxonomy: boolean;
822
821
  identityAnchors: boolean;
823
822
  peerProfiles: boolean;
824
823
  };
824
+ schemaVersion: string;
825
825
  id: string;
826
826
  description: string;
827
827
  version: string;
@@ -854,13 +854,13 @@ declare const ExportBundleV2Schema: z.ZodObject<{
854
854
  pluginVersion: string;
855
855
  includesTranscripts: boolean;
856
856
  capsule: {
857
- schemaVersion: string;
858
857
  includes: {
859
858
  procedural: boolean;
860
859
  taxonomy: boolean;
861
860
  identityAnchors: boolean;
862
861
  peerProfiles: boolean;
863
862
  };
863
+ schemaVersion: string;
864
864
  id: string;
865
865
  description: string;
866
866
  version: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remnic/core",
3
- "version": "9.3.608",
3
+ "version": "9.3.609",
4
4
  "description": "Framework-agnostic Remnic memory engine — orchestrator, storage, extraction, search, trust zones",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -22,7 +22,7 @@
22
22
 
23
23
  import path from "node:path";
24
24
  import { homedir } from "node:os";
25
- import { appendFile, chmod, mkdir, readFile, readdir } from "node:fs/promises";
25
+ import { appendFile, chmod, lstat, mkdir, readFile, readdir, realpath } from "node:fs/promises";
26
26
  import { log } from "./logger.js";
27
27
  import type { JudgeVerdictKind } from "./extraction-judge.js";
28
28
 
@@ -101,22 +101,21 @@ function dateStamp(iso: string): string {
101
101
  return `${yyyy}-${mm}-${dd}`;
102
102
  }
103
103
 
104
- export function trainingFilePathFor(
105
- directory: string,
106
- iso: string,
107
- ): string {
104
+ export function trainingFilePathFor(directory: string, iso: string): string {
108
105
  return path.join(directory, `${dateStamp(iso)}.jsonl`);
109
106
  }
110
107
 
108
+ function isPathInsideDirectory(filePath: string, directory: string): boolean {
109
+ const relative = path.relative(directory, filePath);
110
+ return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
111
+ }
112
+
111
113
  /**
112
114
  * Append a single training row. Fails open — write errors are logged at
113
115
  * debug level and swallowed, same policy as the telemetry emitter.
114
116
  * No-op when `options.enabled` is false.
115
117
  */
116
- export async function recordJudgeTrainingPair(
117
- row: JudgeTrainingPair,
118
- options: JudgeTrainingOptions,
119
- ): Promise<void> {
118
+ export async function recordJudgeTrainingPair(row: JudgeTrainingPair, options: JudgeTrainingOptions): Promise<void> {
120
119
  if (!options.enabled) return;
121
120
  const dir = resolveTrainingDir(options);
122
121
  const filePath = trainingFilePathFor(dir, row.ts);
@@ -129,7 +128,7 @@ export async function recordJudgeTrainingPair(
129
128
  await chmod(filePath, 0o600);
130
129
  } catch (err) {
131
130
  log.debug(
132
- `extraction-judge-training: append failed (non-fatal): ${err instanceof Error ? err.message : String(err)}`,
131
+ `extraction-judge-training: append failed (non-fatal): ${err instanceof Error ? err.message : String(err)}`
133
132
  );
134
133
  }
135
134
  }
@@ -140,9 +139,23 @@ export async function recordJudgeTrainingPair(
140
139
  * counted in the returned `malformed` tally.
141
140
  */
142
141
  export async function readJudgeTrainingPairs(
143
- options: Pick<JudgeTrainingOptions, "directory">,
142
+ options: Pick<JudgeTrainingOptions, "directory">
144
143
  ): Promise<{ rows: JudgeTrainingPair[]; malformed: number }> {
145
144
  const dir = resolveTrainingDir({ enabled: true, ...options });
145
+ try {
146
+ const dirStat = await lstat(dir);
147
+ if (dirStat.isSymbolicLink()) {
148
+ throw new Error("Judge training directory must not be a symlink");
149
+ }
150
+ if (!dirStat.isDirectory()) {
151
+ throw new Error("Judge training path must be a directory");
152
+ }
153
+ } catch (err) {
154
+ const code = (err as NodeJS.ErrnoException).code;
155
+ if (code === "ENOENT") return { rows: [], malformed: 0 };
156
+ throw err;
157
+ }
158
+
146
159
  let entries: string[];
147
160
  try {
148
161
  entries = await readdir(dir);
@@ -154,11 +167,33 @@ export async function readJudgeTrainingPairs(
154
167
 
155
168
  const rows: JudgeTrainingPair[] = [];
156
169
  let malformed = 0;
170
+ const resolvedDir = await realpath(dir);
157
171
  // Sort so reads are deterministic across platforms.
158
172
  entries.sort();
159
173
  for (const name of entries) {
160
174
  if (!name.endsWith(".jsonl")) continue;
161
- const raw = await readFile(path.join(dir, name), "utf-8");
175
+ const filePath = path.join(dir, name);
176
+ let fileStat: Awaited<ReturnType<typeof lstat>>;
177
+ try {
178
+ fileStat = await lstat(filePath);
179
+ } catch (err) {
180
+ const code = (err as NodeJS.ErrnoException).code;
181
+ if (code === "ENOENT") continue;
182
+ throw err;
183
+ }
184
+ if (fileStat.isSymbolicLink() || !fileStat.isFile()) continue;
185
+
186
+ let resolvedFilePath: string;
187
+ try {
188
+ resolvedFilePath = await realpath(filePath);
189
+ } catch (err) {
190
+ const code = (err as NodeJS.ErrnoException).code;
191
+ if (code === "ENOENT") continue;
192
+ throw err;
193
+ }
194
+ if (!isPathInsideDirectory(resolvedFilePath, resolvedDir)) continue;
195
+
196
+ const raw = await readFile(resolvedFilePath, "utf-8");
162
197
  for (const line of raw.split("\n")) {
163
198
  if (!line.trim()) continue;
164
199
  let parsed: unknown;
@@ -192,18 +227,11 @@ export function isValidTrainingPair(value: unknown): value is JudgeTrainingPair
192
227
  if (typeof p.ts !== "string") return false;
193
228
  if (typeof p.candidateText !== "string") return false;
194
229
  if (typeof p.candidateCategory !== "string") return false;
195
- if (
196
- p.verdictKind !== "accept" &&
197
- p.verdictKind !== "reject" &&
198
- p.verdictKind !== "defer"
199
- ) {
230
+ if (p.verdictKind !== "accept" && p.verdictKind !== "reject" && p.verdictKind !== "defer") {
200
231
  return false;
201
232
  }
202
233
  if (typeof p.reason !== "string") return false;
203
- if (
204
- p.candidateConfidence !== undefined &&
205
- typeof p.candidateConfidence !== "number"
206
- ) {
234
+ if (p.candidateConfidence !== undefined && typeof p.candidateConfidence !== "number") {
207
235
  return false;
208
236
  }
209
237
  if (p.priorDeferrals !== undefined && typeof p.priorDeferrals !== "number") {
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/extraction-judge-training.ts"],"sourcesContent":["/**\n * Extraction Judge Training Data Shim (issue #562, PR 4).\n *\n * Opt-in collector for `(candidate_text, verdict_kind, reason,\n * ground_truth_label?)` tuples. Rows are appended to JSONL files under\n * `~/.remnic/judge-training/<YYYY-MM-DD>.jsonl` so operators can ship the\n * data into a future GRPO training pipeline without exfiltrating live\n * memory content through the regular observation ledger.\n *\n * Gating:\n * - Off by default. Must be explicitly enabled via\n * `collectJudgeTrainingPairs: true` in plugin config.\n * - The ground-truth label is always optional — labels are added out-of-\n * band once reviewers disambiguate the candidate's fate.\n *\n * Privacy: the row carries only what the judge already sees — the\n * candidate text and its metadata. It does NOT carry session keys,\n * principal IDs, or any user identifiers. The file lives in the user's\n * home directory rather than the shared memory directory so it is never\n * committed, sync'd, or bundled into exports.\n */\n\nimport path from \"node:path\";\nimport { homedir } from \"node:os\";\nimport { appendFile, chmod, mkdir, readFile, readdir } from \"node:fs/promises\";\nimport { log } from \"./logger.js\";\nimport type { JudgeVerdictKind } from \"./extraction-judge.js\";\n\n/**\n * Persisted training row. Intentionally minimal: just the signal needed\n * to train a judge replacement policy. Schema version is tagged so future\n * readers can migrate older rows.\n */\nexport interface JudgeTrainingPair {\n version: 1;\n ts: string; // ISO-8601\n candidateText: string;\n candidateCategory: string;\n candidateConfidence?: number;\n verdictKind: JudgeVerdictKind;\n reason: string;\n /**\n * Number of prior deferrals when the verdict was resolved. `0` for the\n * first resolution; only set when known (defer pathway).\n */\n priorDeferrals?: number;\n /**\n * Optional human-applied ground-truth label. Added after the fact by a\n * reviewer / labelling script; not present on fresh rows.\n */\n groundTruthLabel?: JudgeVerdictKind;\n}\n\nexport interface JudgeTrainingOptions {\n enabled: boolean;\n /**\n * Override for the output directory. Defaults to\n * `~/.remnic/judge-training`. Tests pass a temp path here.\n */\n directory?: string;\n}\n\n/**\n * Expand a leading `~` / `~/` / `$HOME/` / `${HOME}/` to the process home\n * directory. Node's `fs` APIs do not expand `~` themselves (CLAUDE.md\n * gotcha 17), so every user-facing path input must be funnelled through\n * this helper before it reaches the filesystem.\n */\nfunction expandTilde(p: string): string {\n const home = homedir();\n if (p === \"~\" || p.startsWith(\"~/\") || p.startsWith(\"~\\\\\")) {\n return home + p.slice(1);\n }\n if (p === \"$HOME\" || p.startsWith(\"$HOME/\") || p.startsWith(\"$HOME\\\\\")) {\n return home + p.slice(5);\n }\n if (p === \"${HOME}\" || p.startsWith(\"${HOME}/\") || p.startsWith(\"${HOME}\\\\\")) {\n return home + p.slice(7);\n }\n return p;\n}\n\nexport function resolveTrainingDir(options: JudgeTrainingOptions): string {\n if (options.directory && options.directory.length > 0) {\n // Expand `~` / `$HOME` in the override so operators can write the\n // config as the user sees it (CLAUDE.md gotcha 17).\n return expandTilde(options.directory);\n }\n return path.join(homedir(), \".remnic\", \"judge-training\");\n}\n\nfunction dateStamp(iso: string): string {\n // `YYYY-MM-DD` from an ISO-8601 string. Falls back to today on a parse\n // failure rather than throwing — the caller already wrote a row and the\n // timestamp is best-effort.\n const ms = Date.parse(iso);\n const d = Number.isFinite(ms) ? new Date(ms) : new Date();\n const yyyy = d.getUTCFullYear().toString().padStart(4, \"0\");\n const mm = (d.getUTCMonth() + 1).toString().padStart(2, \"0\");\n const dd = d.getUTCDate().toString().padStart(2, \"0\");\n return `${yyyy}-${mm}-${dd}`;\n}\n\nexport function trainingFilePathFor(\n directory: string,\n iso: string,\n): string {\n return path.join(directory, `${dateStamp(iso)}.jsonl`);\n}\n\n/**\n * Append a single training row. Fails open — write errors are logged at\n * debug level and swallowed, same policy as the telemetry emitter.\n * No-op when `options.enabled` is false.\n */\nexport async function recordJudgeTrainingPair(\n row: JudgeTrainingPair,\n options: JudgeTrainingOptions,\n): Promise<void> {\n if (!options.enabled) return;\n const dir = resolveTrainingDir(options);\n const filePath = trainingFilePathFor(dir, row.ts);\n try {\n await mkdir(dir, { recursive: true, mode: 0o700 });\n await appendFile(filePath, `${JSON.stringify(row)}\\n`, {\n encoding: \"utf-8\",\n mode: 0o600,\n });\n await chmod(filePath, 0o600);\n } catch (err) {\n log.debug(\n `extraction-judge-training: append failed (non-fatal): ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n}\n\n/**\n * Read all training rows from the configured directory. Returns an empty\n * array when the directory is missing. Malformed lines are skipped and\n * counted in the returned `malformed` tally.\n */\nexport async function readJudgeTrainingPairs(\n options: Pick<JudgeTrainingOptions, \"directory\">,\n): Promise<{ rows: JudgeTrainingPair[]; malformed: number }> {\n const dir = resolveTrainingDir({ enabled: true, ...options });\n let entries: string[];\n try {\n entries = await readdir(dir);\n } catch (err) {\n const code = (err as NodeJS.ErrnoException).code;\n if (code === \"ENOENT\") return { rows: [], malformed: 0 };\n throw err;\n }\n\n const rows: JudgeTrainingPair[] = [];\n let malformed = 0;\n // Sort so reads are deterministic across platforms.\n entries.sort();\n for (const name of entries) {\n if (!name.endsWith(\".jsonl\")) continue;\n const raw = await readFile(path.join(dir, name), \"utf-8\");\n for (const line of raw.split(\"\\n\")) {\n if (!line.trim()) continue;\n let parsed: unknown;\n try {\n parsed = JSON.parse(line);\n } catch {\n malformed += 1;\n continue;\n }\n if (!isValidTrainingPair(parsed)) {\n malformed += 1;\n continue;\n }\n rows.push(parsed);\n }\n }\n return { rows, malformed };\n}\n\n/**\n * Structural validator matching the persisted schema. Forward-compat: an\n * unknown `verdictKind` string is treated as malformed (strict training\n * signal — we do not want to admit unlabelled gibberish into a trainer).\n */\nexport function isValidTrainingPair(value: unknown): value is JudgeTrainingPair {\n if (typeof value !== \"object\" || value === null || Array.isArray(value)) {\n return false;\n }\n const p = value as Record<string, unknown>;\n if (p.version !== 1) return false;\n if (typeof p.ts !== \"string\") return false;\n if (typeof p.candidateText !== \"string\") return false;\n if (typeof p.candidateCategory !== \"string\") return false;\n if (\n p.verdictKind !== \"accept\" &&\n p.verdictKind !== \"reject\" &&\n p.verdictKind !== \"defer\"\n ) {\n return false;\n }\n if (typeof p.reason !== \"string\") return false;\n if (\n p.candidateConfidence !== undefined &&\n typeof p.candidateConfidence !== \"number\"\n ) {\n return false;\n }\n if (p.priorDeferrals !== undefined && typeof p.priorDeferrals !== \"number\") {\n return false;\n }\n if (\n p.groundTruthLabel !== undefined &&\n p.groundTruthLabel !== \"accept\" &&\n p.groundTruthLabel !== \"reject\" &&\n p.groundTruthLabel !== \"defer\"\n ) {\n return false;\n }\n return true;\n}\n"],"mappings":";;;;;AAsBA,OAAO,UAAU;AACjB,SAAS,eAAe;AACxB,SAAS,YAAY,OAAO,OAAO,UAAU,eAAe;AA4C5D,SAAS,YAAY,GAAmB;AACtC,QAAM,OAAO,QAAQ;AACrB,MAAI,MAAM,OA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