principles-disciple 1.71.0 → 1.73.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (309) hide show
  1. package/openclaw.plugin.json +10 -5
  2. package/package.json +17 -19
  3. package/scripts/acceptance-test.mjs +16 -73
  4. package/scripts/sync-plugin.mjs +382 -77
  5. package/src/commands/archive-impl.ts +2 -1
  6. package/src/commands/capabilities.ts +2 -2
  7. package/src/commands/context.ts +2 -2
  8. package/src/commands/disable-impl.ts +2 -1
  9. package/src/commands/evolution-status.ts +16 -16
  10. package/src/commands/export.ts +12 -67
  11. package/src/commands/pain.ts +91 -1
  12. package/src/commands/principle-rollback.ts +2 -1
  13. package/src/commands/promote-impl.ts +7 -43
  14. package/src/commands/rollback-impl.ts +2 -1
  15. package/src/commands/rollback.ts +2 -1
  16. package/src/commands/samples.ts +2 -1
  17. package/src/commands/thinking-os.ts +2 -1
  18. package/src/config/errors.ts +18 -2
  19. package/src/constants/diagnostician.ts +2 -2
  20. package/src/constants/tools.ts +2 -1
  21. package/src/core/__tests__/focus-history.test.ts +210 -0
  22. package/src/core/config.ts +1 -1
  23. package/src/core/confirm-first-gate.ts +255 -0
  24. package/src/core/correction-cue-learner.ts +2 -136
  25. package/src/core/correction-types.ts +16 -88
  26. package/src/core/dictionary.ts +19 -20
  27. package/src/core/empathy-keyword-matcher.ts +17 -289
  28. package/src/core/empathy-types.ts +18 -229
  29. package/src/core/event-log.ts +38 -132
  30. package/src/core/evolution-reducer.ts +21 -2
  31. package/src/core/evolution-types.ts +76 -464
  32. package/src/core/file-store.ts +80 -0
  33. package/src/core/focus-history.ts +228 -955
  34. package/src/core/local-worker-routing.ts +34 -314
  35. package/src/core/merge-gate-audit.ts +0 -195
  36. package/src/core/pain-diagnostic-gate.ts +154 -0
  37. package/src/core/pain-signal.ts +21 -138
  38. package/src/core/pain.ts +15 -88
  39. package/src/core/pd-task-reconciler.ts +26 -115
  40. package/src/core/pd-task-service.ts +9 -9
  41. package/src/core/pd-task-types.ts +23 -127
  42. package/src/core/principle-compiler/__tests__/compiler-replay-gate.test.ts +174 -0
  43. package/src/core/principle-compiler/code-validator.ts +15 -42
  44. package/src/core/principle-compiler/compiler.ts +100 -15
  45. package/src/core/principle-compiler/index.ts +5 -2
  46. package/src/core/principle-compiler/template-generator.ts +4 -104
  47. package/src/core/principle-injection.ts +10 -202
  48. package/src/core/principle-internalization/filesystem-lifecycle-datasource.ts +42 -0
  49. package/src/core/principle-internalization/lifecycle-read-model.ts +39 -242
  50. package/src/core/principle-internalization/principle-lifecycle-service.ts +12 -10
  51. package/src/core/principle-tree-ledger-adapter.ts +145 -0
  52. package/src/core/principle-tree-ledger.ts +8 -6
  53. package/src/core/reflection/reflection-context.ts +14 -109
  54. package/src/core/replay-engine.ts +8 -500
  55. package/src/core/rule-host-helpers.ts +5 -35
  56. package/src/core/rule-host-types.ts +10 -82
  57. package/src/core/rule-host.ts +6 -63
  58. package/src/core/runtime-v2-prompt-activation-reader.ts +231 -0
  59. package/src/core/session-tracker.ts +87 -101
  60. package/src/core/shadow-observation-registry.ts +19 -48
  61. package/src/core/trajectory.ts +3 -1
  62. package/src/core/workflow-funnel-loader.ts +62 -68
  63. package/src/core/workspace-context.ts +46 -0
  64. package/src/core/workspace-dir-service.ts +1 -1
  65. package/src/core/workspace-dir-validation.ts +18 -9
  66. package/src/hooks/AGENTS.md +1 -1
  67. package/src/hooks/gate-block-helper.ts +46 -44
  68. package/src/hooks/gate.ts +207 -7
  69. package/src/hooks/lifecycle.ts +30 -32
  70. package/src/hooks/llm.ts +60 -32
  71. package/src/hooks/pain.ts +297 -103
  72. package/src/hooks/prompt.ts +469 -339
  73. package/src/hooks/subagent.ts +2 -29
  74. package/src/i18n/commands.ts +2 -10
  75. package/src/index.ts +95 -85
  76. package/src/openclaw-sdk.ts +311 -0
  77. package/src/service/central-database.ts +8 -4
  78. package/src/service/evolution-queue-migration.ts +2 -1
  79. package/src/service/evolution-worker.ts +163 -1786
  80. package/src/service/internalization-trigger-adapter.ts +302 -0
  81. package/src/service/keyword-optimization-service.ts +4 -4
  82. package/src/service/monitoring-query-service.ts +1 -215
  83. package/src/service/queue-io.ts +60 -331
  84. package/src/service/runtime-summary-service.ts +115 -18
  85. package/src/service/subagent-workflow/index.ts +0 -41
  86. package/src/service/subagent-workflow/types.ts +9 -120
  87. package/src/service/subagent-workflow/workflow-store.ts +2 -119
  88. package/src/service/workflow-watchdog.ts +0 -43
  89. package/src/types/event-payload.ts +16 -74
  90. package/src/types/event-types.ts +39 -547
  91. package/src/types/hygiene-types.ts +7 -30
  92. package/src/types/principle-tree-schema.ts +20 -222
  93. package/src/types/queue.ts +15 -70
  94. package/src/types/runtime-summary.ts +5 -49
  95. package/src/utils/io.ts +10 -0
  96. package/src/utils/retry.ts +1 -1
  97. package/src/utils/shadow-fingerprint.ts +2 -2
  98. package/src/utils/workspace-resolver.ts +50 -0
  99. package/templates/langs/en/core/AGENTS.md +2 -2
  100. package/templates/langs/en/core/BOOT.md +1 -1
  101. package/templates/langs/en/core/HEARTBEAT.md +2 -2
  102. package/templates/langs/en/skills/ai-sprint-orchestration/references/agent-registry.json +1 -72
  103. package/templates/langs/en/skills/ai-sprint-orchestration/references/specs/bugfix-complex-template.json +6 -6
  104. package/templates/langs/en/skills/ai-sprint-orchestration/references/specs/feature-complex-template.json +6 -6
  105. package/templates/langs/en/skills/ai-sprint-orchestration/references/specs/workflow-validation-minimal-verify.json +2 -12
  106. package/templates/langs/en/skills/ai-sprint-orchestration/references/specs/workflow-validation-minimal.json +2 -12
  107. package/templates/langs/en/skills/ai-sprint-orchestration/runtime/.gitignore +2 -2
  108. package/templates/langs/en/skills/ai-sprint-orchestration/scripts/run.mjs +51 -15
  109. package/templates/langs/en/skills/evolve-task/SKILL.md +1 -1
  110. package/templates/langs/en/skills/pd-cli-operator/SKILL.md +67 -0
  111. package/templates/langs/en/skills/pd-diagnostician/SKILL.md +1 -1
  112. package/templates/langs/en/skills/pd-mentor/SKILL.md +1 -1
  113. package/templates/langs/en/skills/pd-pain-signal/SKILL.md +17 -39
  114. package/templates/langs/en/skills/pd-runtime-v2/SKILL.md +61 -0
  115. package/templates/langs/zh/core/AGENTS.md +2 -2
  116. package/templates/langs/zh/core/BOOT.md +1 -1
  117. package/templates/langs/zh/core/HEARTBEAT.md +2 -2
  118. package/templates/langs/zh/skills/ai-sprint-orchestration/references/agent-registry.json +1 -72
  119. package/templates/langs/zh/skills/ai-sprint-orchestration/references/specs/bugfix-complex-template.json +6 -6
  120. package/templates/langs/zh/skills/ai-sprint-orchestration/references/specs/feature-complex-template.json +6 -6
  121. package/templates/langs/zh/skills/ai-sprint-orchestration/references/specs/nocturnal-trinity-quality-enhancement.json +8 -8
  122. package/templates/langs/zh/skills/ai-sprint-orchestration/references/specs/workflow-validation-minimal-verify.json +2 -12
  123. package/templates/langs/zh/skills/ai-sprint-orchestration/references/specs/workflow-validation-minimal.json +2 -12
  124. package/templates/langs/zh/skills/ai-sprint-orchestration/runtime/.gitignore +2 -2
  125. package/templates/langs/zh/skills/ai-sprint-orchestration/scripts/run.mjs +51 -15
  126. package/templates/langs/zh/skills/ai-sprint-orchestration/test/run.test.mjs +21 -5
  127. package/templates/langs/zh/skills/evolve-task/SKILL.md +2 -2
  128. package/templates/langs/zh/skills/pd-cli-operator/SKILL.md +67 -0
  129. package/templates/langs/zh/skills/pd-diagnostician/SKILL.md +1 -1
  130. package/templates/langs/zh/skills/pd-mentor/SKILL.md +1 -1
  131. package/templates/langs/zh/skills/pd-pain-signal/SKILL.md +17 -38
  132. package/templates/langs/zh/skills/pd-runtime-v2/SKILL.md +61 -0
  133. package/tests/build-artifacts.test.ts +1 -3
  134. package/tests/commands/evolution-status.test.ts +0 -118
  135. package/tests/core/bootstrap-rules.test.ts +1 -1
  136. package/tests/core/config.test.ts +1 -1
  137. package/tests/core/event-log.test.ts +35 -0
  138. package/tests/core/evolution-engine.test.ts +610 -0
  139. package/tests/core/file-store.test.ts +102 -0
  140. package/tests/core/focus-history.test.ts +203 -11
  141. package/tests/core/merge-gate-audit.test.ts +2 -169
  142. package/tests/core/model-deployment-registry.test.ts +7 -1
  143. package/tests/core/model-training-registry.test.ts +19 -0
  144. package/tests/core/observability.test.ts +0 -1
  145. package/tests/core/pain-diagnostic-gate.test.ts +498 -0
  146. package/tests/core/pain.test.ts +0 -1
  147. package/tests/core/principle-internalization/deprecated-readiness.test.ts +2 -2
  148. package/tests/core/principle-internalization/lifecycle-metrics.test.ts +2 -2
  149. package/tests/core/principle-internalization/{internalization-routing-policy.test.ts → lifecycle-routing-policy.test.ts} +6 -6
  150. package/tests/core/principle-internalization/lineage-source-retired.test.ts +56 -0
  151. package/tests/core/principle-internalization/principle-lifecycle-service.test.ts +1 -23
  152. package/tests/core/principle-tree-ledger-adapter.test.ts +253 -0
  153. package/tests/core/reflection-context.test.ts +0 -14
  154. package/tests/core/replay-engine.test.ts +127 -215
  155. package/tests/core/rule-host-helpers.test.ts +2 -2
  156. package/tests/core/rule-implementation-runtime.test.ts +0 -27
  157. package/tests/core/workflow-funnel-loader.test.ts +162 -0
  158. package/tests/core/workspace-dir-validation.test.ts +8 -1
  159. package/tests/core-anti-growth.test.ts +192 -0
  160. package/tests/hook-workspace-nextaction-contract.test.ts +42 -0
  161. package/tests/hooks/confirm-first-gate.test.ts +333 -0
  162. package/tests/hooks/gate-auto-correct-shadow.test.ts +310 -0
  163. package/tests/hooks/gate-auto-correct.test.ts +665 -0
  164. package/tests/hooks/gate-rule-host-pipeline.test.ts +2 -1
  165. package/tests/hooks/pain.test.ts +269 -12
  166. package/tests/hooks/prompt-characterization.test.ts +500 -0
  167. package/tests/hooks/prompt-size-guard.test.ts +329 -0
  168. package/tests/hooks/runtime-v2-prompt-activation.test.ts +869 -0
  169. package/tests/index.test.ts +94 -1
  170. package/tests/integration/auto-entry-gate.test.ts +248 -0
  171. package/tests/integration/internalization-trigger-guard.test.ts +69 -0
  172. package/tests/integration/m8-legacy-paths.test.ts +63 -0
  173. package/tests/integration/runtime-v2-pain-guard.test.ts +125 -0
  174. package/tests/plugin-config-resolution-cutover.test.ts +359 -0
  175. package/tests/runtime-v2-discovery-guard.test.ts +154 -0
  176. package/tests/service/central-database.test.ts +457 -0
  177. package/tests/service/evolution-worker.correction-observer.test.ts +173 -0
  178. package/tests/service/evolution-worker.timeout.test.ts +11 -129
  179. package/tests/service/internalization-trigger-adapter.test.ts +251 -0
  180. package/tests/service/monitoring-query-service.test.ts +1 -47
  181. package/tests/service/queue-io.test.ts +1 -62
  182. package/tests/service/runtime-summary-service.test.ts +184 -3
  183. package/tests/service/workflow-watchdog.test.ts +0 -91
  184. package/tests/utils/file-lock.test.ts +5 -3
  185. package/tests/utils/session-key.test.ts +52 -0
  186. package/tests/utils/subagent-probe.test.ts +48 -1
  187. package/vitest.config.ts +4 -11
  188. package/.planning/codebase/ARCHITECTURE.md +0 -157
  189. package/.planning/codebase/CONCERNS.md +0 -145
  190. package/.planning/codebase/CONVENTIONS.md +0 -148
  191. package/.planning/codebase/INTEGRATIONS.md +0 -81
  192. package/.planning/codebase/STACK.md +0 -87
  193. package/.planning/codebase/STRUCTURE.md +0 -193
  194. package/.planning/codebase/TESTING.md +0 -243
  195. package/.planning/phases/01-basic-visualization/01-GAP-CLOSURE-VERIFICATION.md +0 -113
  196. package/docs/COMMAND_REFERENCE.md +0 -76
  197. package/docs/COMMAND_REFERENCE_EN.md +0 -79
  198. package/scripts/build-web.mjs +0 -46
  199. package/scripts/diagnose-nocturnal.mjs +0 -537
  200. package/scripts/seed-nocturnal-scenarios.mjs +0 -384
  201. package/src/commands/nocturnal-review.ts +0 -322
  202. package/src/commands/nocturnal-rollout.ts +0 -790
  203. package/src/commands/nocturnal-train.ts +0 -986
  204. package/src/commands/pd-reflect.ts +0 -88
  205. package/src/core/adaptive-thresholds.ts +0 -478
  206. package/src/core/diagnostician-task-store.ts +0 -192
  207. package/src/core/nocturnal-arbiter.ts +0 -715
  208. package/src/core/nocturnal-artifact-lineage.ts +0 -116
  209. package/src/core/nocturnal-artificer.ts +0 -257
  210. package/src/core/nocturnal-candidate-scoring.ts +0 -530
  211. package/src/core/nocturnal-compliance.ts +0 -1146
  212. package/src/core/nocturnal-dataset.ts +0 -763
  213. package/src/core/nocturnal-executability.ts +0 -428
  214. package/src/core/nocturnal-export.ts +0 -499
  215. package/src/core/nocturnal-paths.ts +0 -240
  216. package/src/core/nocturnal-reasoning-deriver.ts +0 -343
  217. package/src/core/nocturnal-rule-implementation-validator.ts +0 -246
  218. package/src/core/nocturnal-snapshot-contract.ts +0 -99
  219. package/src/core/nocturnal-trajectory-extractor.ts +0 -512
  220. package/src/core/nocturnal-trinity-types.ts +0 -218
  221. package/src/core/nocturnal-trinity.ts +0 -2680
  222. package/src/core/principle-internalization/deprecated-readiness.ts +0 -93
  223. package/src/core/principle-internalization/internalization-routing-policy.ts +0 -208
  224. package/src/core/principle-internalization/lifecycle-metrics.ts +0 -152
  225. package/src/http/principles-console-route.ts +0 -709
  226. package/src/service/central-health-service.ts +0 -49
  227. package/src/service/central-overview-service.ts +0 -138
  228. package/src/service/control-ui-query-service.ts +0 -900
  229. package/src/service/cooldown-strategy.ts +0 -97
  230. package/src/service/evolution-pain-context.ts +0 -79
  231. package/src/service/evolution-query-service.ts +0 -407
  232. package/src/service/health-query-service.ts +0 -1038
  233. package/src/service/nocturnal-config.ts +0 -214
  234. package/src/service/nocturnal-runtime.ts +0 -734
  235. package/src/service/nocturnal-service.ts +0 -1605
  236. package/src/service/nocturnal-target-selector.ts +0 -545
  237. package/src/service/sleep-cycle.ts +0 -157
  238. package/src/service/startup-reconciler.ts +0 -112
  239. package/src/service/subagent-workflow/correction-observer-types.ts +0 -82
  240. package/src/service/subagent-workflow/correction-observer-workflow-manager.ts +0 -250
  241. package/src/service/subagent-workflow/deep-reflect-workflow-manager.ts +0 -1
  242. package/src/service/subagent-workflow/dynamic-timeout.ts +0 -30
  243. package/src/service/subagent-workflow/empathy-observer-workflow-manager.ts +0 -268
  244. package/src/service/subagent-workflow/nocturnal-workflow-manager.ts +0 -795
  245. package/src/service/subagent-workflow/runtime-direct-driver.ts +0 -268
  246. package/src/service/subagent-workflow/workflow-manager-base.ts +0 -580
  247. package/src/tools/write-pain-flag.ts +0 -215
  248. package/tests/commands/nocturnal-review.test.ts +0 -448
  249. package/tests/commands/nocturnal-train.test.ts +0 -97
  250. package/tests/commands/pd-reflect.test.ts +0 -49
  251. package/tests/core/adaptive-thresholds.test.ts +0 -261
  252. package/tests/core/nocturnal-arbiter.test.ts +0 -559
  253. package/tests/core/nocturnal-artifact-lineage.test.ts +0 -53
  254. package/tests/core/nocturnal-artificer.test.ts +0 -241
  255. package/tests/core/nocturnal-candidate-scoring.test.ts +0 -532
  256. package/tests/core/nocturnal-compliance-p-principles.test.ts +0 -133
  257. package/tests/core/nocturnal-compliance.test.ts +0 -646
  258. package/tests/core/nocturnal-dataset.test.ts +0 -892
  259. package/tests/core/nocturnal-e2e.test.ts +0 -234
  260. package/tests/core/nocturnal-executability.test.ts +0 -357
  261. package/tests/core/nocturnal-export.test.ts +0 -517
  262. package/tests/core/nocturnal-reasoning-deriver.test.ts +0 -372
  263. package/tests/core/nocturnal-reviewed-subset-comparison.test.ts +0 -428
  264. package/tests/core/nocturnal-rule-implementation-validator.test.ts +0 -127
  265. package/tests/core/nocturnal-snapshot-contract.test.ts +0 -121
  266. package/tests/core/nocturnal-trajectory-extractor.test.ts +0 -634
  267. package/tests/core/nocturnal-trinity.test.ts +0 -2053
  268. package/tests/core/pain-auto-repair.test.ts +0 -96
  269. package/tests/core/pain-integration.test.ts +0 -510
  270. package/tests/fixtures/nocturnal-reviewed-subset.json +0 -183
  271. package/tests/http/principles-console-route.test.ts +0 -162
  272. package/tests/integration/chaos-resilience.test.ts +0 -348
  273. package/tests/integration/empathy-workflow-integration.test.ts +0 -626
  274. package/tests/integration/pain-diagnostician-loop.e2e.test.ts +0 -380
  275. package/tests/service/control-ui-query-service.test.ts +0 -121
  276. package/tests/service/cooldown-strategy.test.ts +0 -164
  277. package/tests/service/data-endpoints-regression.test.ts +0 -834
  278. package/tests/service/empathy-observer-workflow-manager.test.ts +0 -175
  279. package/tests/service/evolution-worker.nocturnal.test.ts +0 -601
  280. package/tests/service/nocturnal-runtime-hardening.test.ts +0 -118
  281. package/tests/service/nocturnal-runtime.test.ts +0 -473
  282. package/tests/service/nocturnal-service-code-candidate.test.ts +0 -330
  283. package/tests/service/nocturnal-target-selector.test.ts +0 -615
  284. package/tests/service/startup-reconciler.test.ts +0 -148
  285. package/tests/tools/write-pain-flag.test.ts +0 -358
  286. package/ui/src/App.tsx +0 -45
  287. package/ui/src/api.ts +0 -220
  288. package/ui/src/charts.tsx +0 -955
  289. package/ui/src/components/ErrorState.tsx +0 -6
  290. package/ui/src/components/Loading.tsx +0 -13
  291. package/ui/src/components/ProtectedRoute.tsx +0 -12
  292. package/ui/src/components/Shell.tsx +0 -91
  293. package/ui/src/components/WorkspaceConfig.tsx +0 -178
  294. package/ui/src/components/index.ts +0 -5
  295. package/ui/src/context/auth.tsx +0 -80
  296. package/ui/src/context/theme.tsx +0 -66
  297. package/ui/src/hooks/useAutoRefresh.ts +0 -39
  298. package/ui/src/i18n/ui.ts +0 -473
  299. package/ui/src/main.tsx +0 -16
  300. package/ui/src/pages/EvolutionPage.tsx +0 -333
  301. package/ui/src/pages/FeedbackPage.tsx +0 -138
  302. package/ui/src/pages/GateMonitorPage.tsx +0 -136
  303. package/ui/src/pages/LoginPage.tsx +0 -89
  304. package/ui/src/pages/OverviewPage.tsx +0 -599
  305. package/ui/src/pages/SamplesPage.tsx +0 -174
  306. package/ui/src/pages/ThinkingModelsPage.tsx +0 -702
  307. package/ui/src/styles.css +0 -2020
  308. package/ui/src/types.ts +0 -384
  309. package/ui/src/utils/format.ts +0 -15
@@ -1,892 +0,0 @@
1
- import { describe, it, expect, beforeEach, afterEach } from 'vitest';
2
- import * as fs from 'fs';
3
- import * as path from 'path';
4
- import * as os from 'os';
5
- import {
6
- generateSampleFingerprint,
7
- generateFingerprintFromArtifact,
8
- registerSample,
9
- getDatasetRecord,
10
- getDatasetRecordByArtifactId,
11
- listDatasetRecords,
12
- updateReviewStatus,
13
- updateTargetModelFamily,
14
- isExportReady,
15
- listExportReadyRecords,
16
- getArtifactPath,
17
- readDatasetArtifact,
18
- getDatasetStats,
19
- migrateSampleArtifacts,
20
- type NocturnalDatasetRecord,
21
- type NocturnalReviewStatus,
22
- } from '../../src/core/nocturnal-dataset.js';
23
- import type { NocturnalArtifact } from '../../src/core/nocturnal-arbiter.js';
24
-
25
- // ---------------------------------------------------------------------------
26
- // Test Fixtures
27
- // ---------------------------------------------------------------------------
28
-
29
- function makeArtifact(overrides: Partial<NocturnalArtifact> = {}): NocturnalArtifact {
30
- return {
31
- artifactId: 'a1b2c3d4-e5f6-7890-abcd-ef1234567890',
32
- sessionId: 'session-abc123',
33
- principleId: 'T-08',
34
- sourceSnapshotRef: 'snapshot-2026-03-27-001',
35
- badDecision: 'After bash command failed, immediately retried without diagnosing',
36
- betterDecision: 'Check the error message before retrying',
37
- rationale: 'Diagnosing failures prevents repeated failures',
38
- createdAt: '2026-03-27T12:00:00.000Z',
39
- ...overrides,
40
- };
41
- }
42
-
43
- function makeTmpDir(): string {
44
- return fs.mkdtempSync(path.join(os.tmpdir(), 'pd-nocturnal-dataset-test-'));
45
- }
46
-
47
- function rmdir(dir: string): void {
48
- try {
49
- if (fs.existsSync(dir)) {
50
- fs.rmSync(dir, { recursive: true, force: true });
51
- }
52
- } catch {
53
- // Ignore cleanup errors
54
- }
55
- }
56
-
57
- // ---------------------------------------------------------------------------
58
- // Tests: generateSampleFingerprint
59
- // ---------------------------------------------------------------------------
60
-
61
- describe('NocturnalDataset generateSampleFingerprint', () => {
62
- it('produces deterministic fingerprint', () => {
63
- const fp1 = generateSampleFingerprint('art-1', 'T-01', 'sess-1');
64
- const fp2 = generateSampleFingerprint('art-1', 'T-01', 'sess-1');
65
- expect(fp1).toBe(fp2);
66
- });
67
-
68
- it('produces different fingerprints for different inputs', () => {
69
- const fp1 = generateSampleFingerprint('art-1', 'T-01', 'sess-1');
70
- const fp2 = generateSampleFingerprint('art-2', 'T-01', 'sess-1');
71
- const fp3 = generateSampleFingerprint('art-1', 'T-02', 'sess-1');
72
- const fp4 = generateSampleFingerprint('art-1', 'T-01', 'sess-2');
73
- expect(fp1).not.toBe(fp2);
74
- expect(fp1).not.toBe(fp3);
75
- expect(fp1).not.toBe(fp4);
76
- });
77
-
78
- it('produces 64-char hex string (SHA-256)', () => {
79
- const fp = generateSampleFingerprint('art-1', 'T-01', 'sess-1');
80
- expect(fp).toMatch(/^[0-9a-f]{64}$/);
81
- });
82
- });
83
-
84
- // ---------------------------------------------------------------------------
85
- // Tests: generateFingerprintFromArtifact
86
- // ---------------------------------------------------------------------------
87
-
88
- describe('NocturnalDataset generateFingerprintFromArtifact', () => {
89
- it('produces same fingerprint as separate args', () => {
90
- const artifact = makeArtifact();
91
- const fp1 = generateFingerprintFromArtifact(artifact);
92
- const fp2 = generateSampleFingerprint(
93
- artifact.artifactId,
94
- artifact.principleId,
95
- artifact.sessionId
96
- );
97
- expect(fp1).toBe(fp2);
98
- });
99
- });
100
-
101
- // ---------------------------------------------------------------------------
102
- // Tests: registerSample
103
- // ---------------------------------------------------------------------------
104
-
105
- describe('NocturnalDataset registerSample', () => {
106
- let tmpDir: string;
107
-
108
- beforeEach(() => {
109
- tmpDir = makeTmpDir();
110
- });
111
-
112
- afterEach(() => {
113
- rmdir(tmpDir);
114
- });
115
-
116
- it('registers a new sample and returns isNew=true', () => {
117
- const artifact = makeArtifact();
118
- const artifactPath = path.join(tmpDir, 'samples', `${artifact.artifactId}.json`);
119
-
120
- // Create artifact file
121
- fs.mkdirSync(path.dirname(artifactPath), { recursive: true });
122
- fs.writeFileSync(artifactPath, JSON.stringify({ ...artifact, status: 'approved' }), 'utf-8');
123
-
124
- const result = registerSample(tmpDir, artifact, artifactPath, 'gpt-4');
125
-
126
- expect(result.isNew).toBe(true);
127
- expect(result.record.sampleFingerprint).toBe(
128
- generateFingerprintFromArtifact(artifact)
129
- );
130
- expect(result.record.artifactId).toBe(artifact.artifactId);
131
- expect(result.record.sessionId).toBe(artifact.sessionId);
132
- expect(result.record.principleId).toBe(artifact.principleId);
133
- expect(result.record.reviewStatus).toBe('pending_review');
134
- expect(result.record.targetModelFamily).toBe('gpt-4');
135
- expect(result.record.artifactPath).toBe(artifactPath);
136
- });
137
-
138
- it('registers sample with null targetModelFamily', () => {
139
- const artifact = makeArtifact();
140
- const artifactPath = path.join(tmpDir, 'samples', `${artifact.artifactId}.json`);
141
- fs.mkdirSync(path.dirname(artifactPath), { recursive: true });
142
- fs.writeFileSync(artifactPath, JSON.stringify({ ...artifact, status: 'approved' }), 'utf-8');
143
-
144
- const result = registerSample(tmpDir, artifact, artifactPath, null);
145
-
146
- expect(result.isNew).toBe(true);
147
- expect(result.record.targetModelFamily).toBeNull();
148
- });
149
-
150
- it('returns isNew=false for duplicate registration', () => {
151
- const artifact = makeArtifact();
152
- const artifactPath = path.join(tmpDir, 'samples', `${artifact.artifactId}.json`);
153
- fs.mkdirSync(path.dirname(artifactPath), { recursive: true });
154
- fs.writeFileSync(artifactPath, JSON.stringify({ ...artifact, status: 'approved' }), 'utf-8');
155
-
156
- const result1 = registerSample(tmpDir, artifact, artifactPath, 'gpt-4');
157
- const result2 = registerSample(tmpDir, artifact, artifactPath, 'gpt-4');
158
-
159
- expect(result1.isNew).toBe(true);
160
- expect(result2.isNew).toBe(false);
161
- expect(result2.existingRecord?.sampleFingerprint).toBe(result1.record.sampleFingerprint);
162
- });
163
-
164
- it('persists record to registry file', () => {
165
- const artifact = makeArtifact();
166
- const artifactPath = path.join(tmpDir, 'samples', `${artifact.artifactId}.json`);
167
- fs.mkdirSync(path.dirname(artifactPath), { recursive: true });
168
- fs.writeFileSync(artifactPath, JSON.stringify({ ...artifact, status: 'approved' }), 'utf-8');
169
-
170
- registerSample(tmpDir, artifact, artifactPath, 'gpt-4');
171
-
172
- // Read registry directly
173
- const records = JSON.parse(
174
- fs.readFileSync(path.join(tmpDir, '.state', 'nocturnal', 'dataset-registry.json'), 'utf-8')
175
- );
176
- expect(records).toHaveLength(1);
177
- expect(records[0].artifactId).toBe(artifact.artifactId);
178
- });
179
-
180
- it('creates createdAt and updatedAt timestamps', () => {
181
- const artifact = makeArtifact();
182
- const artifactPath = path.join(tmpDir, 'samples', `${artifact.artifactId}.json`);
183
- fs.mkdirSync(path.dirname(artifactPath), { recursive: true });
184
- fs.writeFileSync(artifactPath, JSON.stringify({ ...artifact, status: 'approved' }), 'utf-8');
185
-
186
- const before = new Date().toISOString();
187
- const result = registerSample(tmpDir, artifact, artifactPath);
188
- const after = new Date().toISOString();
189
-
190
- expect(result.record.createdAt).toBeDefined();
191
- expect(result.record.updatedAt).toBeDefined();
192
- expect(result.record.createdAt >= before).toBe(true);
193
- expect(result.record.createdAt <= after).toBe(true);
194
- expect(result.record.createdAt).toBe(result.record.updatedAt);
195
- });
196
- });
197
-
198
- // ---------------------------------------------------------------------------
199
- // Tests: getDatasetRecord / getDatasetRecordByArtifactId
200
- // ---------------------------------------------------------------------------
201
-
202
- describe('NocturnalDataset getDatasetRecord', () => {
203
- let tmpDir: string;
204
- let artifact: NocturnalArtifact;
205
- let artifactPath: string;
206
-
207
- beforeEach(() => {
208
- tmpDir = makeTmpDir();
209
- artifact = makeArtifact();
210
- artifactPath = path.join(tmpDir, 'samples', `${artifact.artifactId}.json`);
211
- fs.mkdirSync(path.dirname(artifactPath), { recursive: true });
212
- fs.writeFileSync(artifactPath, JSON.stringify({ ...artifact, status: 'approved' }), 'utf-8');
213
- });
214
-
215
- afterEach(() => {
216
- rmdir(tmpDir);
217
- });
218
-
219
- it('returns null for non-existent fingerprint', () => {
220
- const result = getDatasetRecord(tmpDir, 'nonexistent-fingerprint');
221
- expect(result).toBeNull();
222
- });
223
-
224
- it('returns record for existing fingerprint', () => {
225
- const registered = registerSample(tmpDir, artifact, artifactPath, 'gpt-4');
226
- const result = getDatasetRecord(tmpDir, registered.record.sampleFingerprint);
227
- expect(result).not.toBeNull();
228
- expect(result!.artifactId).toBe(artifact.artifactId);
229
- });
230
-
231
- it('returns null for corrupted registry', () => {
232
- const registryPath = path.join(tmpDir, '.state', 'nocturnal', 'dataset-registry.json');
233
- fs.mkdirSync(path.dirname(registryPath), { recursive: true });
234
- fs.writeFileSync(registryPath, 'not valid json', 'utf-8');
235
-
236
- const result = getDatasetRecord(tmpDir, 'any');
237
- expect(result).toBeNull();
238
- });
239
- });
240
-
241
- describe('NocturnalDataset getDatasetRecordByArtifactId', () => {
242
- let tmpDir: string;
243
- let artifact: NocturnalArtifact;
244
- let artifactPath: string;
245
-
246
- beforeEach(() => {
247
- tmpDir = makeTmpDir();
248
- artifact = makeArtifact();
249
- artifactPath = path.join(tmpDir, 'samples', `${artifact.artifactId}.json`);
250
- fs.mkdirSync(path.dirname(artifactPath), { recursive: true });
251
- fs.writeFileSync(artifactPath, JSON.stringify({ ...artifact, status: 'approved' }), 'utf-8');
252
- });
253
-
254
- afterEach(() => {
255
- rmdir(tmpDir);
256
- });
257
-
258
- it('returns null for non-existent artifactId', () => {
259
- const result = getDatasetRecordByArtifactId(tmpDir, 'nonexistent');
260
- expect(result).toBeNull();
261
- });
262
-
263
- it('returns record for existing artifactId', () => {
264
- registerSample(tmpDir, artifact, artifactPath, 'gpt-4');
265
- const result = getDatasetRecordByArtifactId(tmpDir, artifact.artifactId);
266
- expect(result).not.toBeNull();
267
- expect(result!.artifactId).toBe(artifact.artifactId);
268
- });
269
- });
270
-
271
- // ---------------------------------------------------------------------------
272
- // Tests: listDatasetRecords + filtering
273
- // ---------------------------------------------------------------------------
274
-
275
- describe('NocturnalDataset listDatasetRecords', () => {
276
- let tmpDir: string;
277
-
278
- beforeEach(() => {
279
- tmpDir = makeTmpDir();
280
- });
281
-
282
- afterEach(() => {
283
- rmdir(tmpDir);
284
- });
285
-
286
- function registerSampleWithStatus(
287
- artifactId: string,
288
- status: NocturnalReviewStatus,
289
- family: string | null = 'gpt-4'
290
- ): NocturnalDatasetRecord {
291
- const artifact = makeArtifact({ artifactId });
292
- const artifactPath = path.join(tmpDir, 'samples', `${artifact.artifactId}.json`);
293
- fs.mkdirSync(path.dirname(artifactPath), { recursive: true });
294
- fs.writeFileSync(artifactPath, JSON.stringify({ ...artifact, status: 'approved' }), 'utf-8');
295
- const result = registerSample(tmpDir, artifact, artifactPath, family);
296
- if (result.record.reviewStatus !== status) {
297
- updateReviewStatus(tmpDir, result.record.sampleFingerprint, status, 'test reason');
298
- }
299
- return getDatasetRecord(tmpDir, result.record.sampleFingerprint)!;
300
- }
301
-
302
- it('returns all records sorted by createdAt descending', () => {
303
- const r1 = registerSampleWithStatus('art-1', 'pending_review');
304
- const r2 = registerSampleWithStatus('art-2', 'approved_for_training');
305
- const r3 = registerSampleWithStatus('art-3', 'rejected');
306
-
307
- const records = listDatasetRecords(tmpDir);
308
- expect(records).toHaveLength(3);
309
- // Newest first
310
- expect(records[0].artifactId).toBe('art-3');
311
- expect(records[1].artifactId).toBe('art-2');
312
- expect(records[2].artifactId).toBe('art-1');
313
- });
314
-
315
- it('filters by reviewStatus (single)', () => {
316
- registerSampleWithStatus('art-1', 'pending_review');
317
- registerSampleWithStatus('art-2', 'approved_for_training');
318
- registerSampleWithStatus('art-3', 'approved_for_training');
319
-
320
- const approved = listDatasetRecords(tmpDir, { reviewStatus: 'approved_for_training' });
321
- expect(approved).toHaveLength(2);
322
- expect(approved.every((r) => r.reviewStatus === 'approved_for_training')).toBe(true);
323
- });
324
-
325
- it('filters by reviewStatus (array)', () => {
326
- registerSampleWithStatus('art-1', 'pending_review');
327
- registerSampleWithStatus('art-2', 'approved_for_training');
328
- registerSampleWithStatus('art-3', 'rejected');
329
-
330
- const filtered = listDatasetRecords(tmpDir, {
331
- reviewStatus: ['pending_review', 'rejected'],
332
- });
333
- expect(filtered).toHaveLength(2);
334
- expect(filtered.every((r) =>
335
- r.reviewStatus === 'pending_review' || r.reviewStatus === 'rejected'
336
- )).toBe(true);
337
- });
338
-
339
- it('filters by targetModelFamily (specific family)', () => {
340
- registerSampleWithStatus('art-1', 'approved_for_training', 'gpt-4');
341
- registerSampleWithStatus('art-2', 'approved_for_training', 'gpt-4');
342
- registerSampleWithStatus('art-3', 'approved_for_training', 'claude-3');
343
-
344
- const gpt4 = listDatasetRecords(tmpDir, { targetModelFamily: 'gpt-4' });
345
- expect(gpt4).toHaveLength(2);
346
- expect(gpt4.every((r) => r.targetModelFamily === 'gpt-4')).toBe(true);
347
- });
348
-
349
- it('filters by targetModelFamily (null = unassigned)', () => {
350
- registerSampleWithStatus('art-1', 'pending_review', null);
351
- registerSampleWithStatus('art-2', 'pending_review', 'gpt-4');
352
-
353
- const unassigned = listDatasetRecords(tmpDir, { targetModelFamily: null });
354
- expect(unassigned).toHaveLength(1);
355
- expect(unassigned[0].targetModelFamily).toBeNull();
356
- });
357
-
358
- it('returns empty array when no records exist', () => {
359
- const records = listDatasetRecords(tmpDir);
360
- expect(records).toHaveLength(0);
361
- });
362
- });
363
-
364
- // ---------------------------------------------------------------------------
365
- // Tests: updateReviewStatus
366
- // ---------------------------------------------------------------------------
367
-
368
- describe('NocturnalDataset updateReviewStatus', () => {
369
- let tmpDir: string;
370
-
371
- beforeEach(() => {
372
- tmpDir = makeTmpDir();
373
- });
374
-
375
- afterEach(() => {
376
- rmdir(tmpDir);
377
- });
378
-
379
- function registerAndGet(
380
- artifactId: string,
381
- family: string | null = 'gpt-4'
382
- ): NocturnalDatasetRecord {
383
- const artifact = makeArtifact({ artifactId });
384
- const artifactPath = path.join(tmpDir, 'samples', `${artifact.artifactId}.json`);
385
- fs.mkdirSync(path.dirname(artifactPath), { recursive: true });
386
- fs.writeFileSync(artifactPath, JSON.stringify({ ...artifact, status: 'approved' }), 'utf-8');
387
- return registerSample(tmpDir, artifact, artifactPath, family).record;
388
- }
389
-
390
- it('updates reviewStatus and reviewReason', () => {
391
- const record = registerAndGet('art-1');
392
- const updated = updateReviewStatus(
393
- tmpDir,
394
- record.sampleFingerprint,
395
- 'approved_for_training',
396
- 'Looks good for training'
397
- );
398
-
399
- expect(updated.reviewStatus).toBe('approved_for_training');
400
- expect(updated.reviewReason).toBe('Looks good for training');
401
- expect(updated.updatedAt).not.toBe(record.createdAt);
402
- });
403
-
404
- it('updates updatedAt timestamp', () => {
405
- const record = registerAndGet('art-1');
406
- const before = new Date().toISOString();
407
- const updated = updateReviewStatus(
408
- tmpDir,
409
- record.sampleFingerprint,
410
- 'rejected',
411
- 'Not suitable'
412
- );
413
-
414
- expect(updated.updatedAt >= before).toBe(true);
415
- });
416
-
417
- it('throws for invalid transition (pending_review → approved_for_training without reason)', () => {
418
- const record = registerAndGet('art-1');
419
- expect(() =>
420
- updateReviewStatus(tmpDir, record.sampleFingerprint, 'approved_for_training')
421
- ).toThrow('reviewReason is required');
422
- });
423
-
424
- it('throws for invalid transition (approved_for_training → rejected)', () => {
425
- const record = registerAndGet('art-1');
426
- // First go to approved
427
- updateReviewStatus(tmpDir, record.sampleFingerprint, 'approved_for_training', 'approved');
428
- // Then try to reject (invalid)
429
- expect(() =>
430
- updateReviewStatus(tmpDir, record.sampleFingerprint, 'rejected', 'changed mind')
431
- ).toThrow(/Invalid review status transition/);
432
- });
433
-
434
- it('throws for non-existent fingerprint', () => {
435
- expect(() =>
436
- updateReviewStatus(tmpDir, 'nonexistent', 'approved_for_training', 'reason')
437
- ).toThrow('Dataset record not found');
438
- });
439
-
440
- it('allows superseded from approved_for_training', () => {
441
- const record = registerAndGet('art-1');
442
- updateReviewStatus(tmpDir, record.sampleFingerprint, 'approved_for_training', 'looks good');
443
- const updated = updateReviewStatus(
444
- tmpDir,
445
- record.sampleFingerprint,
446
- 'superseded',
447
- 'Superseded by better sample'
448
- );
449
- expect(updated.reviewStatus).toBe('superseded');
450
- });
451
-
452
- it('allows rejected → pending_review (re-review)', () => {
453
- const record = registerAndGet('art-1');
454
- updateReviewStatus(tmpDir, record.sampleFingerprint, 'rejected', 'Not good');
455
- const updated = updateReviewStatus(
456
- tmpDir,
457
- record.sampleFingerprint,
458
- 'pending_review'
459
- );
460
- expect(updated.reviewStatus).toBe('pending_review');
461
- });
462
- });
463
-
464
- // ---------------------------------------------------------------------------
465
- // Tests: updateTargetModelFamily
466
- // ---------------------------------------------------------------------------
467
-
468
- describe('NocturnalDataset updateTargetModelFamily', () => {
469
- let tmpDir: string;
470
-
471
- beforeEach(() => {
472
- tmpDir = makeTmpDir();
473
- });
474
-
475
- afterEach(() => {
476
- rmdir(tmpDir);
477
- });
478
-
479
- it('updates targetModelFamily', () => {
480
- const artifact = makeArtifact();
481
- const artifactPath = path.join(tmpDir, 'samples', `${artifact.artifactId}.json`);
482
- fs.mkdirSync(path.dirname(artifactPath), { recursive: true });
483
- fs.writeFileSync(artifactPath, JSON.stringify({ ...artifact, status: 'approved' }), 'utf-8');
484
- const record = registerSample(tmpDir, artifact, artifactPath, null).record;
485
-
486
- const updated = updateTargetModelFamily(tmpDir, record.sampleFingerprint, 'claude-3');
487
- expect(updated.targetModelFamily).toBe('claude-3');
488
- });
489
-
490
- it('can set targetModelFamily back to null', () => {
491
- const artifact = makeArtifact();
492
- const artifactPath = path.join(tmpDir, 'samples', `${artifact.artifactId}.json`);
493
- fs.mkdirSync(path.dirname(artifactPath), { recursive: true });
494
- fs.writeFileSync(artifactPath, JSON.stringify({ ...artifact, status: 'approved' }), 'utf-8');
495
- const record = registerSample(tmpDir, artifact, artifactPath, 'gpt-4').record;
496
-
497
- const updated = updateTargetModelFamily(tmpDir, record.sampleFingerprint, null);
498
- expect(updated.targetModelFamily).toBeNull();
499
- });
500
- });
501
-
502
- // ---------------------------------------------------------------------------
503
- // Tests: isExportReady + listExportReadyRecords
504
- // ---------------------------------------------------------------------------
505
-
506
- describe('NocturnalDataset isExportReady', () => {
507
- let tmpDir: string;
508
-
509
- beforeEach(() => {
510
- tmpDir = makeTmpDir();
511
- });
512
-
513
- afterEach(() => {
514
- rmdir(tmpDir);
515
- });
516
-
517
- function setupExportReady(
518
- artifactId: string,
519
- family: string | null = 'gpt-4'
520
- ): NocturnalDatasetRecord {
521
- const artifact = makeArtifact({ artifactId });
522
- const artifactPath = path.join(tmpDir, 'samples', `${artifact.artifactId}.json`);
523
- fs.mkdirSync(path.dirname(artifactPath), { recursive: true });
524
- fs.writeFileSync(artifactPath, JSON.stringify({ ...artifact, status: 'approved' }), 'utf-8');
525
- const record = registerSample(tmpDir, artifact, artifactPath, family).record;
526
- updateReviewStatus(tmpDir, record.sampleFingerprint, 'approved_for_training', 'ready for training');
527
- return getDatasetRecord(tmpDir, record.sampleFingerprint)!;
528
- }
529
-
530
- it('returns true for fully configured record', () => {
531
- const record = setupExportReady('art-1', 'gpt-4');
532
- expect(isExportReady(tmpDir, record.sampleFingerprint)).toBe(true);
533
- });
534
-
535
- it('returns false for rejected record', () => {
536
- const artifact = makeArtifact({ artifactId: 'art-rejected' });
537
- const artifactPath = path.join(tmpDir, 'samples', `${artifact.artifactId}.json`);
538
- fs.mkdirSync(path.dirname(artifactPath), { recursive: true });
539
- fs.writeFileSync(artifactPath, JSON.stringify({ ...artifact, status: 'approved' }), 'utf-8');
540
- const record = registerSample(tmpDir, artifact, artifactPath, 'gpt-4').record;
541
- updateReviewStatus(tmpDir, record.sampleFingerprint, 'rejected', 'not suitable');
542
-
543
- expect(isExportReady(tmpDir, record.sampleFingerprint)).toBe(false);
544
- });
545
-
546
- it('returns false for pending_review record', () => {
547
- const artifact = makeArtifact({ artifactId: 'art-pending' });
548
- const artifactPath = path.join(tmpDir, 'samples', `${artifact.artifactId}.json`);
549
- fs.mkdirSync(path.dirname(artifactPath), { recursive: true });
550
- fs.writeFileSync(artifactPath, JSON.stringify({ ...artifact, status: 'approved' }), 'utf-8');
551
- const record = registerSample(tmpDir, artifact, artifactPath, 'gpt-4').record;
552
-
553
- expect(isExportReady(tmpDir, record.sampleFingerprint)).toBe(false);
554
- });
555
-
556
- it('returns false for record with null targetModelFamily', () => {
557
- const record = setupExportReady('art-1', null);
558
- expect(isExportReady(tmpDir, record.sampleFingerprint)).toBe(false);
559
- });
560
-
561
- it('returns false when artifact file is missing', () => {
562
- const artifact = makeArtifact({ artifactId: 'art-missing' });
563
- const artifactPath = path.join(tmpDir, 'samples', `${artifact.artifactId}.json`);
564
- fs.mkdirSync(path.dirname(artifactPath), { recursive: true });
565
- fs.writeFileSync(artifactPath, JSON.stringify({ ...artifact, status: 'approved' }), 'utf-8');
566
- const record = registerSample(tmpDir, artifact, artifactPath, 'gpt-4').record;
567
- updateReviewStatus(tmpDir, record.sampleFingerprint, 'approved_for_training', 'ready');
568
- // Delete artifact file
569
- fs.unlinkSync(artifactPath);
570
-
571
- expect(isExportReady(tmpDir, record.sampleFingerprint)).toBe(false);
572
- });
573
-
574
- it('returns false for non-existent fingerprint', () => {
575
- expect(isExportReady(tmpDir, 'nonexistent')).toBe(false);
576
- });
577
- });
578
-
579
- describe('NocturnalDataset listExportReadyRecords', () => {
580
- let tmpDir: string;
581
-
582
- beforeEach(() => {
583
- tmpDir = makeTmpDir();
584
- });
585
-
586
- afterEach(() => {
587
- rmdir(tmpDir);
588
- });
589
-
590
- function setup(
591
- artifactId: string,
592
- family: string | null,
593
- status: NocturnalReviewStatus
594
- ): void {
595
- const artifact = makeArtifact({ artifactId });
596
- const artifactPath = path.join(tmpDir, 'samples', `${artifact.artifactId}.json`);
597
- fs.mkdirSync(path.dirname(artifactPath), { recursive: true });
598
- fs.writeFileSync(artifactPath, JSON.stringify({ ...artifact, status: 'approved' }), 'utf-8');
599
- const record = registerSample(tmpDir, artifact, artifactPath, family).record;
600
- if (status !== 'pending_review') {
601
- updateReviewStatus(tmpDir, record.sampleFingerprint, status, 'test');
602
- }
603
- }
604
-
605
- it('returns only approved_for_training records with target family and artifact', () => {
606
- setup('art-1', 'gpt-4', 'approved_for_training');
607
- setup('art-2', 'gpt-4', 'pending_review');
608
- setup('art-3', 'claude-3', 'approved_for_training');
609
- setup('art-4', 'gpt-4', 'rejected');
610
-
611
- const gpt4 = listExportReadyRecords(tmpDir, 'gpt-4');
612
- expect(gpt4).toHaveLength(1);
613
- expect(gpt4[0].artifactId).toBe('art-1');
614
- expect(gpt4[0].targetModelFamily).toBe('gpt-4');
615
- });
616
-
617
- it('returns all export-ready records when no family specified', () => {
618
- setup('art-1', 'gpt-4', 'approved_for_training');
619
- setup('art-2', 'claude-3', 'approved_for_training');
620
-
621
- const all = listExportReadyRecords(tmpDir);
622
- expect(all).toHaveLength(2);
623
- });
624
-
625
- it('returns empty array when no export-ready records', () => {
626
- setup('art-1', 'gpt-4', 'pending_review');
627
-
628
- const records = listExportReadyRecords(tmpDir, 'gpt-4');
629
- expect(records).toHaveLength(0);
630
- });
631
- });
632
-
633
- // ---------------------------------------------------------------------------
634
- // Tests: getArtifactPath + readDatasetArtifact
635
- // ---------------------------------------------------------------------------
636
-
637
- describe('NocturnalDataset getArtifactPath', () => {
638
- let tmpDir: string;
639
-
640
- beforeEach(() => {
641
- tmpDir = makeTmpDir();
642
- });
643
-
644
- afterEach(() => {
645
- rmdir(tmpDir);
646
- });
647
-
648
- it('returns path when record exists and artifact file exists', () => {
649
- const artifact = makeArtifact();
650
- const artifactPath = path.join(tmpDir, 'samples', `${artifact.artifactId}.json`);
651
- fs.mkdirSync(path.dirname(artifactPath), { recursive: true });
652
- fs.writeFileSync(artifactPath, JSON.stringify({ ...artifact, status: 'approved' }), 'utf-8');
653
- const record = registerSample(tmpDir, artifact, artifactPath, 'gpt-4').record;
654
-
655
- const pathResult = getArtifactPath(tmpDir, record.sampleFingerprint);
656
- expect(pathResult).toBe(artifactPath);
657
- });
658
-
659
- it('returns null when record does not exist', () => {
660
- const pathResult = getArtifactPath(tmpDir, 'nonexistent');
661
- expect(pathResult).toBeNull();
662
- });
663
-
664
- it('returns null when artifact file is missing', () => {
665
- const artifact = makeArtifact();
666
- const artifactPath = path.join(tmpDir, 'samples', `${artifact.artifactId}.json`);
667
- fs.mkdirSync(path.dirname(artifactPath), { recursive: true });
668
- fs.writeFileSync(artifactPath, JSON.stringify({ ...artifact, status: 'approved' }), 'utf-8');
669
- const record = registerSample(tmpDir, artifact, artifactPath, 'gpt-4').record;
670
- fs.unlinkSync(artifactPath);
671
-
672
- const pathResult = getArtifactPath(tmpDir, record.sampleFingerprint);
673
- expect(pathResult).toBeNull();
674
- });
675
- });
676
-
677
- describe('NocturnalDataset readDatasetArtifact', () => {
678
- let tmpDir: string;
679
-
680
- beforeEach(() => {
681
- tmpDir = makeTmpDir();
682
- });
683
-
684
- afterEach(() => {
685
- rmdir(tmpDir);
686
- });
687
-
688
- it('reads and returns artifact for existing record', () => {
689
- const artifact = makeArtifact();
690
- const artifactPath = path.join(tmpDir, 'samples', `${artifact.artifactId}.json`);
691
- fs.mkdirSync(path.dirname(artifactPath), { recursive: true });
692
- fs.writeFileSync(artifactPath, JSON.stringify({ ...artifact, status: 'approved' }), 'utf-8');
693
- const record = registerSample(tmpDir, artifact, artifactPath, 'gpt-4').record;
694
-
695
- const readArtifact = readDatasetArtifact(tmpDir, record.sampleFingerprint);
696
- expect(readArtifact).not.toBeNull();
697
- expect(readArtifact!.artifactId).toBe(artifact.artifactId);
698
- expect(readArtifact!.sessionId).toBe(artifact.sessionId);
699
- expect(readArtifact!.principleId).toBe(artifact.principleId);
700
- });
701
-
702
- it('throws for non-existent record', () => {
703
- expect(() => readDatasetArtifact(tmpDir, 'nonexistent')).toThrow('Artifact file not found');
704
- });
705
- });
706
-
707
- // ---------------------------------------------------------------------------
708
- // Tests: getDatasetStats
709
- // ---------------------------------------------------------------------------
710
-
711
- describe('NocturnalDataset getDatasetStats', () => {
712
- let tmpDir: string;
713
-
714
- beforeEach(() => {
715
- tmpDir = makeTmpDir();
716
- });
717
-
718
- afterEach(() => {
719
- rmdir(tmpDir);
720
- });
721
-
722
- function setup(
723
- artifactId: string,
724
- status: NocturnalReviewStatus,
725
- family: string | null = 'gpt-4'
726
- ): void {
727
- const artifact = makeArtifact({ artifactId });
728
- const artifactPath = path.join(tmpDir, 'samples', `${artifact.artifactId}.json`);
729
- fs.mkdirSync(path.dirname(artifactPath), { recursive: true });
730
- fs.writeFileSync(artifactPath, JSON.stringify({ ...artifact, status: 'approved' }), 'utf-8');
731
- const record = registerSample(tmpDir, artifact, artifactPath, family).record;
732
- if (status !== 'pending_review') {
733
- updateReviewStatus(tmpDir, record.sampleFingerprint, status, 'test');
734
- }
735
- }
736
-
737
- it('returns correct counts', () => {
738
- setup('art-1', 'pending_review');
739
- setup('art-2', 'approved_for_training');
740
- setup('art-3', 'approved_for_training');
741
- setup('art-4', 'rejected');
742
- setup('art-5', 'superseded');
743
-
744
- const stats = getDatasetStats(tmpDir);
745
- expect(stats.total).toBe(5);
746
- expect(stats.pendingReview).toBe(1);
747
- expect(stats.approvedForTraining).toBe(2);
748
- expect(stats.rejected).toBe(1);
749
- expect(stats.superseded).toBe(1);
750
- });
751
-
752
- it('counts export-ready by family', () => {
753
- setup('art-1', 'approved_for_training', 'gpt-4');
754
- setup('art-2', 'approved_for_training', 'gpt-4');
755
- setup('art-3', 'approved_for_training', 'claude-3');
756
- setup('art-4', 'pending_review', 'gpt-4');
757
-
758
- const stats = getDatasetStats(tmpDir);
759
- expect(stats.exportReadyByFamily['gpt-4']).toBe(2);
760
- expect(stats.exportReadyByFamily['claude-3']).toBe(1);
761
- });
762
-
763
- it('returns zero counts when empty', () => {
764
- const stats = getDatasetStats(tmpDir);
765
- expect(stats.total).toBe(0);
766
- expect(stats.pendingReview).toBe(0);
767
- expect(stats.approvedForTraining).toBe(0);
768
- });
769
- });
770
-
771
- // ---------------------------------------------------------------------------
772
- // Tests: migrateSampleArtifacts
773
- // ---------------------------------------------------------------------------
774
-
775
- describe('NocturnalDataset migrateSampleArtifacts', () => {
776
- let tmpDir: string;
777
-
778
- beforeEach(() => {
779
- tmpDir = makeTmpDir();
780
- });
781
-
782
- afterEach(() => {
783
- rmdir(tmpDir);
784
- });
785
-
786
- it('registers approved samples not yet in registry', () => {
787
- // Create artifact files directly in samples directory
788
- const samplesDir = path.join(tmpDir, '.state', 'nocturnal', 'samples');
789
- fs.mkdirSync(samplesDir, { recursive: true });
790
-
791
- const artifact1 = { ...makeArtifact({ artifactId: 'art-migrate-1' }), status: 'approved' };
792
- const artifact2 = { ...makeArtifact({ artifactId: 'art-migrate-2' }), status: 'approved' };
793
- const artifact3 = { ...makeArtifact({ artifactId: 'art-migrate-3' }), status: 'rejected' }; // wrong status
794
-
795
- fs.writeFileSync(
796
- path.join(samplesDir, 'art-migrate-1.json'),
797
- JSON.stringify(artifact1)
798
- );
799
- fs.writeFileSync(
800
- path.join(samplesDir, 'art-migrate-2.json'),
801
- JSON.stringify(artifact2)
802
- );
803
- fs.writeFileSync(
804
- path.join(samplesDir, 'art-rejected.json'),
805
- JSON.stringify(artifact3)
806
- );
807
-
808
- const count = migrateSampleArtifacts(tmpDir, 'gpt-4');
809
-
810
- expect(count).toBe(2);
811
- const records = listDatasetRecords(tmpDir);
812
- expect(records).toHaveLength(2);
813
- expect(records.map((r) => r.artifactId).sort()).toEqual(['art-migrate-1', 'art-migrate-2']);
814
- });
815
-
816
- it('does not duplicate already registered samples', () => {
817
- const samplesDir = path.join(tmpDir, '.state', 'nocturnal', 'samples');
818
- fs.mkdirSync(samplesDir, { recursive: true });
819
-
820
- const artifact = { ...makeArtifact({ artifactId: 'art-dup' }), status: 'approved' };
821
- fs.writeFileSync(
822
- path.join(samplesDir, 'art-dup.json'),
823
- JSON.stringify(artifact)
824
- );
825
-
826
- // First migration
827
- const count1 = migrateSampleArtifacts(tmpDir, 'gpt-4');
828
- expect(count1).toBe(1);
829
-
830
- // Second migration (should be no-op)
831
- const count2 = migrateSampleArtifacts(tmpDir, 'gpt-4');
832
- expect(count2).toBe(0);
833
-
834
- // Still only one record
835
- const records = listDatasetRecords(tmpDir);
836
- expect(records).toHaveLength(1);
837
- });
838
-
839
- it('skips malformed sample files', () => {
840
- const samplesDir = path.join(tmpDir, '.state', 'nocturnal', 'samples');
841
- fs.mkdirSync(samplesDir, { recursive: true });
842
-
843
- fs.writeFileSync(path.join(samplesDir, 'bad.json'), 'not valid json');
844
- fs.writeFileSync(path.join(samplesDir, 'missing-fields.json'), JSON.stringify({ artifactId: 'art-x' }));
845
-
846
- const count = migrateSampleArtifacts(tmpDir, 'gpt-4');
847
- expect(count).toBe(0);
848
- });
849
- });
850
-
851
- // ---------------------------------------------------------------------------
852
- // Integration: full lineage tracing
853
- // ---------------------------------------------------------------------------
854
-
855
- describe('NocturnalDataset lineage tracing integration', () => {
856
- let tmpDir: string;
857
-
858
- beforeEach(() => {
859
- tmpDir = makeTmpDir();
860
- });
861
-
862
- afterEach(() => {
863
- rmdir(tmpDir);
864
- });
865
-
866
- it('traces sample back to artifact + session + principle', () => {
867
- const artifact = makeArtifact({
868
- artifactId: 'art-lineage-001',
869
- sessionId: 'session-lineage-001',
870
- principleId: 'T-08',
871
- });
872
- const artifactPath = path.join(tmpDir, 'samples', `${artifact.artifactId}.json`);
873
- fs.mkdirSync(path.dirname(artifactPath), { recursive: true });
874
- fs.writeFileSync(artifactPath, JSON.stringify({ ...artifact, status: 'approved' }), 'utf-8');
875
-
876
- // Register
877
- const registered = registerSample(tmpDir, artifact, artifactPath, 'gpt-4');
878
-
879
- // Trace back
880
- const byFingerprint = getDatasetRecord(tmpDir, registered.record.sampleFingerprint);
881
- const byArtifactId = getDatasetRecordByArtifactId(tmpDir, artifact.artifactId);
882
- const pathResult = getArtifactPath(tmpDir, registered.record.sampleFingerprint);
883
- const readArtifact = readDatasetArtifact(tmpDir, registered.record.sampleFingerprint);
884
-
885
- expect(byFingerprint).not.toBeNull();
886
- expect(byArtifactId).not.toBeNull();
887
- expect(pathResult).toBe(artifactPath);
888
- expect(readArtifact).not.toBeNull();
889
- expect(readArtifact!.sessionId).toBe('session-lineage-001');
890
- expect(readArtifact!.principleId).toBe('T-08');
891
- });
892
- });