@citeark/agent 0.3.20

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 (347) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +128 -0
  3. package/data/dataset-source-registry.v1.json +300 -0
  4. package/dist/arkgraph/boot.js +6 -0
  5. package/dist/arkgraph/index.html +1 -0
  6. package/dist/arkgraph/viewer.css +1 -0
  7. package/dist/arkgraph/viewer.en.css +1 -0
  8. package/dist/arkgraph/viewer.en.js +49 -0
  9. package/dist/arkgraph/viewer.en.js.LEGAL.txt +56 -0
  10. package/dist/arkgraph/viewer.js +49 -0
  11. package/dist/arkgraph/viewer.js.LEGAL.txt +56 -0
  12. package/docker/claude-code/Dockerfile +97 -0
  13. package/docker/claude-code/codex-pro-relay.mjs +466 -0
  14. package/docker/claude-code/runtime-contract-check.mjs +79 -0
  15. package/docs/arkgraph-reading.md +79 -0
  16. package/docs/configuration.md +100 -0
  17. package/docs/integration.md +92 -0
  18. package/docs/maturity-plan.md +27 -0
  19. package/docs/npm-release.md +44 -0
  20. package/docs/paper-reading.md +40 -0
  21. package/docs/research-plan-granularity.md +27 -0
  22. package/docs/terminal.md +49 -0
  23. package/examples/toy-evaluation/compile-task.json +27 -0
  24. package/examples/toy-evaluation/paper.md +5 -0
  25. package/examples/toy-evaluation/repository/README.md +9 -0
  26. package/examples/toy-evaluation/repository/checkpoint.json +4 -0
  27. package/examples/toy-evaluation/repository/evaluate.py +17 -0
  28. package/examples/toy-evaluation/task.json +81 -0
  29. package/package.json +59 -0
  30. package/prompts/compile-research.md +58 -0
  31. package/prompts/execute-contract.md +72 -0
  32. package/prompts/execute-workspace-simple.md +51 -0
  33. package/prompts/execute-workspace.md +34 -0
  34. package/prompts/prepare-reproduction.md +82 -0
  35. package/prompts/repair-research.md +45 -0
  36. package/protocol/CAP.md +129 -0
  37. package/protocol/LICENSE +12 -0
  38. package/protocol/MAPPINGS.md +72 -0
  39. package/protocol/README.md +38 -0
  40. package/protocol/conformance-v2.0-alpha.1.json +36 -0
  41. package/protocol/examples/arkgraph/checkpoint-evaluation.json +309 -0
  42. package/protocol/examples/arkgraph/fixtures.mjs +49 -0
  43. package/protocol/examples/arkgraph/paper-free.json +291 -0
  44. package/protocol/examples/arkgraph/partial-failure.json +344 -0
  45. package/protocol/examples/arkgraph/training-evaluation.json +443 -0
  46. package/protocol/profiles/agent-trace.md +16 -0
  47. package/protocol/profiles/computational-run.md +16 -0
  48. package/protocol/profiles/core.md +15 -0
  49. package/protocol/profiles/public-bundle.md +18 -0
  50. package/protocol/profiles/reproduction.md +29 -0
  51. package/protocol/profiles/research-compilation.md +44 -0
  52. package/protocol/profiles/research-plan.md +39 -0
  53. package/protocol/profiles/restricted-evidence.md +15 -0
  54. package/runtime/bootstrap-autodl-runtime.sh +314 -0
  55. package/runtime/create-runtime-venv.sh +41 -0
  56. package/runtime/install-local-cpu-runtime.sh +23 -0
  57. package/runtime/install-scientific-runtime.sh +153 -0
  58. package/runtime/mineru/parse.py +62 -0
  59. package/runtime/mineru/requirements.txt +4 -0
  60. package/runtime/requirements-baseline.txt +38 -0
  61. package/schemas/cap/v2/activity.schema.json +47 -0
  62. package/schemas/cap/v2/agent.schema.json +32 -0
  63. package/schemas/cap/v2/assertion.schema.json +110 -0
  64. package/schemas/cap/v2/descriptor.schema.json +243 -0
  65. package/schemas/cap/v2/entity.schema.json +64 -0
  66. package/schemas/cap/v2/manifest.schema.json +67 -0
  67. package/schemas/cap/v2/relation.schema.json +82 -0
  68. package/schemas/compute-catalog.schema.json +63 -0
  69. package/schemas/compute-decision.schema.json +27 -0
  70. package/schemas/execution-contract.schema.json +1024 -0
  71. package/schemas/research-card.schema.json +30 -0
  72. package/schemas/research-inventory-draft.schema.json +366 -0
  73. package/schemas/research.schema.json +1044 -0
  74. package/schemas/result.schema.json +173 -0
  75. package/schemas/verification-policy.schema.json +47 -0
  76. package/schemas/verified-conclusion.schema.json +58 -0
  77. package/schemas/workspace-summary.schema.json +24 -0
  78. package/scripts/build-arkgraph-view.mjs +12 -0
  79. package/scripts/check-execution-feasibility.mjs +24 -0
  80. package/scripts/check-syntax.mjs +15 -0
  81. package/scripts/deterministic-asset-preparation.py +438 -0
  82. package/scripts/package-cap.mjs +23 -0
  83. package/scripts/package-local-agent.mjs +23 -0
  84. package/scripts/preview-arkgraph.mjs +25 -0
  85. package/scripts/replay-research-compiler-candidate.mjs +134 -0
  86. package/scripts/review-compiler-sources.mjs +44 -0
  87. package/scripts/run-asset-preparation.sh +17 -0
  88. package/scripts/run-research-plan.mjs +98 -0
  89. package/scripts/validate-asset-preparation.py +290 -0
  90. package/scripts/verify-local-runtime.mjs +57 -0
  91. package/scripts/verify-npm-package.mjs +57 -0
  92. package/src/adapters/paper2agent.mjs +107 -0
  93. package/src/assets/cache.mjs +159 -0
  94. package/src/assets/compute.mjs +98 -0
  95. package/src/assets/executor.mjs +145 -0
  96. package/src/assets/lifecycle.mjs +213 -0
  97. package/src/assets/manifest.mjs +242 -0
  98. package/src/assets/opportunistic-preparation.mjs +81 -0
  99. package/src/assets/plan.mjs +411 -0
  100. package/src/assets/prompts.mjs +29 -0
  101. package/src/assets/public-asset-probe.mjs +525 -0
  102. package/src/assets/qualification.mjs +119 -0
  103. package/src/assets/readiness.mjs +130 -0
  104. package/src/assets/reproduction-admission.mjs +355 -0
  105. package/src/assets/requirements.mjs +152 -0
  106. package/src/assets/source-grounding.mjs +341 -0
  107. package/src/assets/source-policy.mjs +118 -0
  108. package/src/autodl/client.mjs +260 -0
  109. package/src/autodl/ssh.mjs +380 -0
  110. package/src/autodl/tools.mjs +129 -0
  111. package/src/cap/redaction.mjs +38 -0
  112. package/src/cap/v2/archive.mjs +152 -0
  113. package/src/cap/v2/attestation.mjs +204 -0
  114. package/src/cap/v2/canonical-json.mjs +114 -0
  115. package/src/cap/v2/compilation-artifact.mjs +240 -0
  116. package/src/cap/v2/core.mjs +282 -0
  117. package/src/cap/v2/measurement-assessment-records.mjs +23 -0
  118. package/src/cap/v2/pipeline-artifact.mjs +922 -0
  119. package/src/cap/v2/read.mjs +41 -0
  120. package/src/cap/v2/reassessment-artifact.mjs +383 -0
  121. package/src/cap/v2/research-artifact.mjs +231 -0
  122. package/src/cap/v2/research-map-records.mjs +46 -0
  123. package/src/cap/v2/research-object-records.mjs +163 -0
  124. package/src/cap/v2/research-records.mjs +187 -0
  125. package/src/cap/v2/verify.mjs +642 -0
  126. package/src/cli.mjs +1146 -0
  127. package/src/compute/autodl-pro-compiler.mjs +347 -0
  128. package/src/compute/autodl-pro-executor.mjs +459 -0
  129. package/src/compute/autodl-pro-job.mjs +843 -0
  130. package/src/compute/autodl-pro-network.mjs +295 -0
  131. package/src/compute/autodl-pro-remote.mjs +810 -0
  132. package/src/compute/autodl-pro-staging.mjs +117 -0
  133. package/src/compute/campaign.mjs +110 -0
  134. package/src/compute/catalog.mjs +123 -0
  135. package/src/compute/checkpoint-protocol.mjs +154 -0
  136. package/src/compute/codex-account-lock.mjs +111 -0
  137. package/src/compute/codex-account-session.mjs +107 -0
  138. package/src/compute/compiler-profile.mjs +38 -0
  139. package/src/compute/compiler-router.mjs +23 -0
  140. package/src/compute/coordinator-recovery.mjs +210 -0
  141. package/src/compute/executor-router.mjs +29 -0
  142. package/src/compute/gcp-batch-compiler.mjs +685 -0
  143. package/src/compute/gcp-batch-executor.mjs +1215 -0
  144. package/src/compute/gcp-batch-failure.mjs +92 -0
  145. package/src/compute/gcp-batch-job.mjs +527 -0
  146. package/src/compute/gcp-batch-lifecycle.mjs +81 -0
  147. package/src/compute/gcp-checkpoint-worker.mjs +1633 -0
  148. package/src/compute/local-codex-compiler.mjs +52 -0
  149. package/src/compute/measurement-hardware.mjs +128 -0
  150. package/src/compute/remote-attempt.mjs +226 -0
  151. package/src/compute/requirements.mjs +124 -0
  152. package/src/compute/research-phases.mjs +48 -0
  153. package/src/compute/scheduler.mjs +452 -0
  154. package/src/compute/shared-workloads.mjs +26 -0
  155. package/src/compute/stage-archive.mjs +79 -0
  156. package/src/contracts/campaign-contract.mjs +52 -0
  157. package/src/contracts/execution-contract.mjs +819 -0
  158. package/src/contracts/execution-mode.mjs +19 -0
  159. package/src/contracts/execution-timeouts.mjs +45 -0
  160. package/src/contracts/execution-workload.mjs +68 -0
  161. package/src/contracts/preflight-schema.mjs +25 -0
  162. package/src/contracts/public-contract.mjs +63 -0
  163. package/src/contracts/subject-tags.mjs +31 -0
  164. package/src/dashboard/data.mjs +898 -0
  165. package/src/dashboard/server.mjs +79 -0
  166. package/src/dashboard/static/dashboard.css +366 -0
  167. package/src/dashboard/static/dashboard.js +560 -0
  168. package/src/dashboard/static/index.html +85 -0
  169. package/src/deployment/community-policy.mjs +9 -0
  170. package/src/deployment/environment.mjs +112 -0
  171. package/src/deployment/guided.mjs +98 -0
  172. package/src/deployment/handoff.mjs +102 -0
  173. package/src/deployment/local-contract.mjs +31 -0
  174. package/src/deployment/local.mjs +100 -0
  175. package/src/deployment/prepare.mjs +46 -0
  176. package/src/deployment/recipe.mjs +108 -0
  177. package/src/deployment/supplement.mjs +51 -0
  178. package/src/deployment/terminal.mjs +43 -0
  179. package/src/diagnosis/renderer.mjs +75 -0
  180. package/src/diagnosis/target-failure.mjs +46 -0
  181. package/src/evidence/parser-registry.mjs +54 -0
  182. package/src/evidence/parsers/fasttext-classification.mjs +82 -0
  183. package/src/evidence/parsers/json-scalar.mjs +96 -0
  184. package/src/evidence/parsers/simcse-senteval.mjs +104 -0
  185. package/src/evidence/parsers/starspace-classification.mjs +78 -0
  186. package/src/evidence/registry.mjs +147 -0
  187. package/src/execution/runner-audit.mjs +473 -0
  188. package/src/gcp/auth.mjs +106 -0
  189. package/src/gcp/batch-client.mjs +120 -0
  190. package/src/gcp/resource-discovery.mjs +177 -0
  191. package/src/gcp/rest.mjs +82 -0
  192. package/src/gcp/secret-manager.mjs +34 -0
  193. package/src/gcp/signed-url.mjs +133 -0
  194. package/src/gcp/storage.mjs +220 -0
  195. package/src/graph/command.mjs +41 -0
  196. package/src/graph/execution.mjs +97 -0
  197. package/src/graph/model.mjs +37 -0
  198. package/src/graph/presentation.mjs +110 -0
  199. package/src/graph/query.mjs +159 -0
  200. package/src/graph/research-relations.mjs +69 -0
  201. package/src/graph/source-page.mjs +12 -0
  202. package/src/graph/source-preview.mjs +34 -0
  203. package/src/graph/validate.mjs +76 -0
  204. package/src/job.mjs +496 -0
  205. package/src/network/autodl-routing-proxy.mjs +462 -0
  206. package/src/network/egress-proxy.mjs +158 -0
  207. package/src/observability/event-contract.mjs +230 -0
  208. package/src/observability/pipeline-monitor.mjs +166 -0
  209. package/src/pipeline/orchestrator.mjs +1281 -0
  210. package/src/pipeline/recovery-error.mjs +11 -0
  211. package/src/pipeline/replay.mjs +304 -0
  212. package/src/pipeline/shared-execution.mjs +115 -0
  213. package/src/pipeline/stage-checkpoint.mjs +86 -0
  214. package/src/pipeline/stage-recovery.mjs +101 -0
  215. package/src/pipeline/targets.mjs +110 -0
  216. package/src/process.mjs +143 -0
  217. package/src/protocol.mjs +312 -0
  218. package/src/provider/codex-account.mjs +44 -0
  219. package/src/provider/codex-completion.mjs +49 -0
  220. package/src/provider/completion.mjs +292 -0
  221. package/src/provider/model-client.mjs +44 -0
  222. package/src/provider/model-route.mjs +29 -0
  223. package/src/provider/openrouter-readiness.mjs +189 -0
  224. package/src/provider/reader-bridge.mjs +35 -0
  225. package/src/provider/relay.mjs +263 -0
  226. package/src/provider/runtime-auth.mjs +40 -0
  227. package/src/public/cap.d.mts +90 -0
  228. package/src/public/cap.mjs +12 -0
  229. package/src/public/contracts.d.mts +2 -0
  230. package/src/public/host.mjs +171 -0
  231. package/src/public/operations.d.mts +11 -0
  232. package/src/public/presentation.d.mts +4 -0
  233. package/src/records/views.mjs +26 -0
  234. package/src/remote/command.mjs +178 -0
  235. package/src/remote/ssh.mjs +59 -0
  236. package/src/repository-origin.mjs +81 -0
  237. package/src/reproduction/evidence-feedback.mjs +96 -0
  238. package/src/reproduction/incomplete-initialization.mjs +25 -0
  239. package/src/reproduction/lifecycle.mjs +253 -0
  240. package/src/reproduction/plan.mjs +132 -0
  241. package/src/reproduction/prompts.mjs +70 -0
  242. package/src/reproduction/runner.mjs +188 -0
  243. package/src/reproduction/summary.mjs +130 -0
  244. package/src/reproduction/workspace-mode.mjs +7 -0
  245. package/src/research/automatic-admission.mjs +156 -0
  246. package/src/research/compiler-coverage.mjs +85 -0
  247. package/src/research/compiler-failure.mjs +24 -0
  248. package/src/research/compiler-normalization-guards.mjs +112 -0
  249. package/src/research/compiler-repair.mjs +3 -0
  250. package/src/research/compiler.mjs +853 -0
  251. package/src/research/continuation-selection.mjs +26 -0
  252. package/src/research/execution-graph-context.mjs +43 -0
  253. package/src/research/experiment-importance.mjs +15 -0
  254. package/src/research/inventory-handoff.mjs +104 -0
  255. package/src/research/inventory-revisions.mjs +32 -0
  256. package/src/research/mineru-local.mjs +73 -0
  257. package/src/research/paper-command.mjs +19 -0
  258. package/src/research/paper-markdown.mjs +180 -0
  259. package/src/research/paper-source-map.mjs +69 -0
  260. package/src/research/planning-policy.mjs +88 -0
  261. package/src/research/reference-materials.mjs +11 -0
  262. package/src/research/reproduction-scope.mjs +30 -0
  263. package/src/research/research-map.mjs +94 -0
  264. package/src/research/research-objects.mjs +88 -0
  265. package/src/research/source-discovery.mjs +646 -0
  266. package/src/research/source-observations.mjs +75 -0
  267. package/src/research/source-review-cli-mcp.mjs +26 -0
  268. package/src/research/source-review-input.mjs +209 -0
  269. package/src/research/source-review-local-codex.mjs +36 -0
  270. package/src/research/source-review-model.mjs +70 -0
  271. package/src/research/source-review.mjs +173 -0
  272. package/src/research/structure.mjs +3163 -0
  273. package/src/research-card/renderer.mjs +277 -0
  274. package/src/research-card/verified-conclusion.mjs +143 -0
  275. package/src/results/output-registry.mjs +183 -0
  276. package/src/runtime/claude-code.mjs +52 -0
  277. package/src/runtime/codex-capacity-retry.mjs +87 -0
  278. package/src/runtime/codex.mjs +64 -0
  279. package/src/runtime/config.mjs +157 -0
  280. package/src/runtime/final-output.mjs +40 -0
  281. package/src/runtime/index.mjs +21 -0
  282. package/src/runtime/local-codex.mjs +74 -0
  283. package/src/runtime/opencode.mjs +95 -0
  284. package/src/runtime/prompt.mjs +13 -0
  285. package/src/sandbox/docker.mjs +363 -0
  286. package/src/settings/command.mjs +297 -0
  287. package/src/settings/store.mjs +119 -0
  288. package/src/telemetry/pricing.mjs +68 -0
  289. package/src/telemetry/usage.mjs +265 -0
  290. package/src/terminal/events.mjs +97 -0
  291. package/src/terminal/input.mjs +40 -0
  292. package/src/terminal/plain.mjs +40 -0
  293. package/src/terminal/remote-stream.mjs +22 -0
  294. package/src/terminal/screen.mjs +214 -0
  295. package/src/terminal/transcript.mjs +69 -0
  296. package/src/util.mjs +107 -0
  297. package/src/verification/ai-assessor.mjs +534 -0
  298. package/src/verification/claim-evaluator.mjs +242 -0
  299. package/src/verification/evidence-context.mjs +165 -0
  300. package/src/verification/evidence-reader.mjs +95 -0
  301. package/src/verification/integrity.mjs +570 -0
  302. package/src/verification/tolerance.mjs +32 -0
  303. package/src/workloads/cpu-research-preparation.mjs +56 -0
  304. package/src/workloads/definition.mjs +74 -0
  305. package/src/workloads/phase-aware-reproduction.mjs +46 -0
  306. package/src/workloads/reproduction.mjs +85 -0
  307. package/src/workspace/command.mjs +242 -0
  308. package/src/workspace/control.mjs +49 -0
  309. package/src/workspace/entry.mjs +28 -0
  310. package/src/workspace/input.mjs +93 -0
  311. package/src/workspace/interactive.mjs +94 -0
  312. package/src/workspace/jobs.mjs +418 -0
  313. package/src/workspace/session.mjs +97 -0
  314. package/src/workspace/worker.mjs +137 -0
  315. package/ui/arkgraph/ambient-motion.mjs +10 -0
  316. package/ui/arkgraph/app.jsx +153 -0
  317. package/ui/arkgraph/boot.js +6 -0
  318. package/ui/arkgraph/camera-motion.mjs +20 -0
  319. package/ui/arkgraph/context-reveal.mjs +39 -0
  320. package/ui/arkgraph/details.css +3 -0
  321. package/ui/arkgraph/entry.jsx +28 -0
  322. package/ui/arkgraph/experiment-curves.mjs +17 -0
  323. package/ui/arkgraph/experiment-selection.mjs +15 -0
  324. package/ui/arkgraph/experiment-style.css +26 -0
  325. package/ui/arkgraph/experiment-ui.jsx +32 -0
  326. package/ui/arkgraph/frame.html +1 -0
  327. package/ui/arkgraph/graph-gestures.mjs +62 -0
  328. package/ui/arkgraph/label-layout.mjs +57 -0
  329. package/ui/arkgraph/locales/en.json +229 -0
  330. package/ui/arkgraph/locales/source-types.json +15 -0
  331. package/ui/arkgraph/localization-build.mjs +27 -0
  332. package/ui/arkgraph/material-build.mjs +23 -0
  333. package/ui/arkgraph/material-colors.mjs +39 -0
  334. package/ui/arkgraph/material-style.css +15 -0
  335. package/ui/arkgraph/open-graph.jsx +326 -0
  336. package/ui/arkgraph/outline.jsx +49 -0
  337. package/ui/arkgraph/package-lock.json +888 -0
  338. package/ui/arkgraph/package.json +17 -0
  339. package/ui/arkgraph/reading-layout.mjs +130 -0
  340. package/ui/arkgraph/reading-presentation.mjs +73 -0
  341. package/ui/arkgraph/record-detail.css +51 -0
  342. package/ui/arkgraph/record-details.jsx +29 -0
  343. package/ui/arkgraph/research-types.mjs +31 -0
  344. package/ui/arkgraph/selection-mark.jsx +6 -0
  345. package/ui/arkgraph/soft-spine.mjs +26 -0
  346. package/ui/arkgraph/steering-style.css +187 -0
  347. package/ui/arkgraph/style.css +272 -0
@@ -0,0 +1,46 @@
1
+ import { CAP_RECORD_TYPE } from './core.mjs';
2
+ import { capRecordId } from './research-records.mjs';
3
+
4
+ export function researchReferenceResolver(research, objectGraph, sourceWorkId) {
5
+ const map = new Map([
6
+ ...[...objectGraph.objectIds].map(([id, ref]) => [`object:${id}`, ref]),
7
+ ...[...objectGraph.sourceIds].map(([id, ref]) => [`source:${id}`, ref]),
8
+ ...(research.claims ?? []).map(n => [`claim:${n.id}`, capRecordId('claim', `${research.work.id}:${n.id}`)]),
9
+ ...(research.hypotheses ?? []).map(n => [`hypothesis:${n.id}`, capRecordId('hypothesis', `${research.work.id}:${n.id}`)]),
10
+ ]);
11
+ return id => {
12
+ const resolved = map.get(id);
13
+ if (!resolved) throw Error(`Unknown research map reference: ${id}`);
14
+ return { ref: resolved };
15
+ };
16
+ }
17
+
18
+ export function researchMapRecords({ research, objectGraph, sourceWorkId, actorId }) {
19
+ const resolve = researchReferenceResolver(research, objectGraph, sourceWorkId);
20
+ return (research.researchRelations ?? []).map(relation => ({
21
+ $schema: 'https://citeark.com/schemas/cap/v2/relation.schema.json',
22
+ id: capRecordId('source-relation', `${research.work.id}:${relation.id}`),
23
+ type: CAP_RECORD_TYPE.relation, role: 'provenance', predicate: relation.predicate,
24
+ subject: resolve(relation.from), object: resolve(relation.to),
25
+ attributedTo: { ref: actorId }, basis: relation.basis,
26
+ sources: relation.sourceLocators.map(s => resolve(`source:${s.sourceId}`)),
27
+ qualifiers: { sourceLocators: structuredClone(relation.sourceLocators), ...(relation.note ? { note: relation.note } : {}) },
28
+ }));
29
+ }
30
+
31
+ // Keep editorial organization separate from scientific edges. The assembler binds node
32
+ // identities; consumers resolve their exact versions inside this immutable snapshot.
33
+ export function bindResearchReading(research, objectGraph, sourceWorkId) {
34
+ if (!research.reading) return undefined;
35
+ const resolve = researchReferenceResolver(research, objectGraph, sourceWorkId), view = research.reading;
36
+ return { schemaVersion: '1.0', overview: view.overview,
37
+ mainline: view.mainline.map(resolve),
38
+ branches: view.branches.map(b => ({ parent: resolve(b.parent), children: b.children.map(resolve) })),
39
+ labels: (view.labels ?? []).map(({target,...label}) => ({ ...label, ...resolve(target) })),
40
+ };
41
+ }
42
+ export function bindResearchIntent(experiment, research, objectGraph, sourceWorkId) {
43
+ if (!experiment.researchIntent) return undefined;
44
+ const resolve = researchReferenceResolver(research, objectGraph, sourceWorkId), value = experiment.researchIntent;
45
+ return { ...value, from: resolve(value.from), to: resolve(value.to), context: (value.context ?? []).map(resolve) };
46
+ }
@@ -0,0 +1,163 @@
1
+ import { CONTEXT_ROLES } from '../../research/research-map.mjs';
2
+ import { CAP_RECORD_TYPE } from './core.mjs';
3
+ import { canonicalJsonBytes, sha256Bytes } from './canonical-json.mjs';
4
+ import { CAP_SCHEMA, capRecordId, capSourceRights, capDecimal } from './research-records.mjs';
5
+ import { reportedObservationCollections } from '../../research/source-observations.mjs';
6
+
7
+ const ref = id => ({ ref: id });
8
+ const objectId = (research, localId) => capRecordId('research-object', `${research.work.id}:${localId}`);
9
+ const sourceId = (research, source, workId) => source.kind === 'paper' ? workId
10
+ : source.kind === 'repository' && source.version ? capRecordId('code', `${source.uri}@${source.version}`)
11
+ : capRecordId('source', `${research.work.id}:${source.id}`);
12
+ const role = kind => ({ repository: 'code', supplement: 'material' })[kind] ?? kind;
13
+
14
+ /** Materialize only named objects and explicitly recorded inputs. Never extract identities from prose. */
15
+ export function researchObjectRecords({ research, sourceWorkId, actorId }) {
16
+ const records = [], inputIdsByExperiment = new Map(), stepIdsByExperiment = new Map();
17
+ const collections = reportedObservationCollections(research);
18
+ const sourceIds = new Map((research.sources ?? []).map(source => [source.id, sourceId(research, source, sourceWorkId)]));
19
+ const objectIds = new Map((research.researchObjects ?? []).map(object => [object.id, object.sourceId ? sourceIds.get(object.sourceId) : objectId(research, object.id)]));
20
+ for (const source of research.sources ?? []) {
21
+ if (source.kind === 'paper') continue;
22
+ records.push({ $schema: CAP_SCHEMA.sourceWork, id: sourceIds.get(source.id), type: CAP_RECORD_TYPE.entity,
23
+ role: role(source.kind), title: source.title ?? source.uri,
24
+ basis: 'declared', identity: { uri: source.uri, ...(source.version ? { version: source.version } : {}), ...(source.digest ? { digest: source.digest } : {}) },
25
+ rights: capSourceRights(source.kind, research.license),
26
+ origin: { basis: 'declared', actor: ref(actorId) }, citeark: { sourceId: source.id },
27
+ });
28
+ }
29
+ for (const object of research.researchObjects ?? []) {
30
+ const sourceIndex = records.findIndex(record => record.id === objectIds.get(object.id));
31
+ const sourceRecord = sourceIndex >= 0 ? records.splice(sourceIndex, 1)[0] : {};
32
+ const primitive = CONTEXT_ROLES[object.role]?.[0] ?? 'entity';
33
+ records.push({ ...sourceRecord, $schema: `https://citeark.com/schemas/cap/v2/${primitive}.schema.json`, id: objectIds.get(object.id), type: CAP_RECORD_TYPE[primitive],
34
+ ...(primitive === 'activity' ? { status: 'unknown', reportingBasis: 'declared' } : {}),
35
+ ...(primitive === 'assertion' ? { statement: { value: object.description ?? object.title, language: 'und' } } : {}),
36
+ ...(primitive === 'agent' ? { name: object.title, kind: object.role === 'person' ? 'human' : object.role } : {}),
37
+ role: object.role, title: object.title, basis: object.basis ?? 'declared',
38
+ ...(object.role === 'observation' ? { kind: object.prospective ? 'observation-specification'
39
+ : object.observationKind && object.observationKind !== 'scalar' ? 'source-observation-collection' : 'source-observation',
40
+ ...(object.observationKind ? { observationKind: object.observationKind } : {}) } : {}),
41
+ ...(collections.has(object.id) ? { kind: 'source-observation-collection',
42
+ measurements: collections.get(object.id).map(({ claim, measurement }) => ({
43
+ measurementId: measurement.id, metric: measurement.metric,
44
+ value: { decimal: capDecimal(measurement.value) }, unit: measurement.unit,
45
+ ...(measurement.dimensions ? { scope: structuredClone(measurement.dimensions) } : {}),
46
+ conditions: structuredClone(measurement.conditions ?? claim.conditions ?? {}),
47
+ sourceLocator: structuredClone(measurement.sourceLocator ?? claim.sourceLocator),
48
+ contextObjects: (measurement.objectIds ?? claim.objectIds ?? []).map(id => ref(objectIds.get(id))),
49
+ ...(measurement.aggregation ? { aggregation: measurement.aggregation } : {}),
50
+ ...(measurement.evidenceBasis ? { evidenceBasis: measurement.evidenceBasis } : {}),
51
+ ...(measurement.extractionUncertainty !== undefined ? { uncertainty: { absolute: capDecimal(measurement.extractionUncertainty) } } : {}),
52
+ })),
53
+ contextObjects: [...new Set(collections.get(object.id).flatMap(({ claim, measurement }) => measurement.objectIds ?? claim.objectIds ?? []))]
54
+ .map(id => ref(objectIds.get(id))),
55
+ } : {}),
56
+ ...(object.description ? { description: object.description } : {}),
57
+ ...((sourceRecord.identity || object.identity) ? { identity: { ...sourceRecord.identity, ...structuredClone(object.identity ?? {}) } } : {}),
58
+ ...(object.conditions !== undefined ? { conditions: structuredClone(object.conditions) } : {}),
59
+ prospective: object.prospective === true,
60
+ ...(object.prospective ? { availability: 'not-yet-generated' } : {}),
61
+ sourceContext: (object.sourceLocators ?? []).map(locator => ({ source: ref(sourceIds.get(locator.sourceId)), selector: { label: locator.locator } })),
62
+ derivedFrom: (object.derivedFromIds ?? []).map(id => ref(objectIds.get(id))),
63
+ origin: { basis: object.basis ?? 'declared', actor: ref(actorId) },
64
+ citeark: { ...sourceRecord.citeark, objectId: object.id, sourceLocators: object.sourceLocators ?? [], limitations: object.limitations ?? [] },
65
+ });
66
+ }
67
+ // Older compiler inputs already carry source-grounded asset declarations. Keep each exact recipe
68
+ // once; availability labels and matching names never establish observed bytes or shared identity.
69
+ const materialIds = new Set(records.map(record => record.id));
70
+ for (const experiment of research.experiments ?? []) {
71
+ const inputs = new Set((experiment.inputIds ?? []).map(id => objectIds.get(id)));
72
+ const repository = experiment.repository;
73
+ if (repository?.url && repository?.commit) {
74
+ const codeId = capRecordId('code', `${repository.url}@${repository.commit}`);
75
+ if (!materialIds.has(codeId)) {
76
+ records.push({ $schema: CAP_SCHEMA.sourceWork, id: codeId, type: CAP_RECORD_TYPE.entity, role: 'code',
77
+ title: repository.url, identity: { uri: repository.url, revision: repository.commit }, basis: 'declared',
78
+ origin: { basis: 'declared', actor: ref(actorId) } });
79
+ materialIds.add(codeId);
80
+ }
81
+ inputs.add(codeId);
82
+ }
83
+ for (const asset of experiment.protocol?.preflight?.assets ?? []) {
84
+ const identity = { kind: asset.kind, identity: asset.identity, acquisition: asset.acquisition ?? null,
85
+ sourceUrls: [...(asset.sourceUrls ?? [])].sort(), registryDatasetId: asset.registryDatasetId ?? null,
86
+ scientificRole: asset.scientificRole ?? null };
87
+ const assetId = capRecordId('material', sha256Bytes(canonicalJsonBytes(identity)));
88
+ if (!materialIds.has(assetId)) {
89
+ records.push({ $schema: CAP_SCHEMA.sourceWork, id: assetId, type: CAP_RECORD_TYPE.entity, role: role(asset.kind ?? 'material'),
90
+ title: asset.identity ?? asset.id, identity, basis: 'declared', availability: asset.availability ?? 'unknown',
91
+ origin: { basis: 'declared', actor: ref(actorId) }, citeark: { assetId: asset.id, verification: asset.verification ?? null } });
92
+ materialIds.add(assetId);
93
+ }
94
+ inputs.add(assetId);
95
+ }
96
+ inputIdsByExperiment.set(experiment.id, [...inputs]);
97
+ }
98
+ const steps = new Map();
99
+ for (const experiment of research.experiments ?? []) {
100
+ const ids = [];
101
+ const internalOutputs = new Set((experiment.steps ?? []).flatMap(step => step.outputIds ?? []));
102
+ const packageInputs = new Set(inputIdsByExperiment.get(experiment.id));
103
+ for (const step of experiment.steps ?? []) {
104
+ const id = capRecordId('research-step', `${research.work.id}:${step.id}`);
105
+ ids.push(id);
106
+ for (const input of step.inputIds ?? []) if (!internalOutputs.has(input)) packageInputs.add(objectIds.get(input));
107
+ if (!steps.has(step.id)) steps.set(step.id, { id, step, claimIds: new Set(), experimentIds: new Set() });
108
+ const entry = steps.get(step.id);
109
+ entry.experimentIds.add(experiment.id);
110
+ for (const claimId of step.claimIds ?? []) if (research.claims.some(claim => claim.id === claimId)) entry.claimIds.add(claimId);
111
+ }
112
+ stepIdsByExperiment.set(experiment.id, ids);
113
+ inputIdsByExperiment.set(experiment.id, [...packageInputs]);
114
+ }
115
+ for (const entry of steps.values()) {
116
+ const step = entry.step;
117
+ records.push({ $schema: CAP_SCHEMA.experiment, id: entry.id, type: CAP_RECORD_TYPE.entity, role: 'procedure-step',
118
+ title: step.title, kind: step.kind, prospective: true,
119
+ inputs: (step.inputIds ?? []).map(id => ({ record: ref(objectIds.get(id)), role: 'scientific-input', basis: 'declared' })),
120
+ expectedOutputs: (step.outputIds ?? []).map(id => ref(objectIds.get(id))),
121
+ dependencies: (step.dependsOn ?? []).map(id => ref(capRecordId('research-step', `${research.work.id}:${id}`))),
122
+ tests: [...entry.claimIds].map(id => ref(capRecordId('claim', `${research.work.id}:${id}`))),
123
+ origin: { basis: 'inferred', actor: ref(actorId) },
124
+ evaluationPlan: { title: step.title, state: 'planned', ...(step.description ? { instructions: step.description } : {}) },
125
+ citeark: { stepId: step.id, experimentIds: [...entry.experimentIds], workloadIds: step.workloadIds ?? [] },
126
+ });
127
+ }
128
+ return { records, objectIds, sourceIds, inputIdsByExperiment, stepIdsByExperiment };
129
+ }
130
+
131
+ /** Source measurements remain declared observations, including during compilation. */
132
+ export function reportedObservationRecords({ research, sourceWorkId, objectIds = new Map() }) {
133
+ const records = [], idsByClaim = new Map();
134
+ for (const claim of research.claims ?? []) {
135
+ const ids = [];
136
+ for (const measurement of claim.reportedMeasurements ?? []) {
137
+ if (measurement.observationId !== undefined) {
138
+ const object = (research.researchObjects ?? []).find(object => object.id === measurement.observationId);
139
+ const id = objectIds.get(measurement.observationId);
140
+ if (!id || object?.role !== 'observation' || object.prospective || object.basis === 'inferred') throw Error('Invalid source observation collection');
141
+ if (!ids.includes(id)) ids.push(id);
142
+ continue;
143
+ }
144
+ const id = capRecordId('evidence', `${research.work.id}:${claim.id}:reported:${measurement.id}`);
145
+ ids.push(id);
146
+ records.push({ $schema: CAP_SCHEMA.evidence, id, type: CAP_RECORD_TYPE.entity, role: 'observation',
147
+ title: `${measurement.metric}: ${measurement.value} ${measurement.unit}`, kind: 'measurement', basis: 'declared',
148
+ derivedFrom: [{ ref: sourceWorkId }],
149
+ measurement: { measurementId: measurement.id, metric: measurement.metric,
150
+ value: { decimal: capDecimal(measurement.value) }, unit: measurement.unit,
151
+ ...(measurement.dimensions ? { scope: measurement.dimensions } : {}) },
152
+ contextObjects: (measurement.objectIds ?? claim.objectIds ?? []).map(id => ref(objectIds.get(id))),
153
+ limitations: ['Declared by the fixed source work; not independently observed.',
154
+ ...(measurement.evidenceBasis === 'digitized_figure' ? [`Digitized with absolute extraction uncertainty ${measurement.extractionUncertainty} ${measurement.unit}.`] : [])],
155
+ citeark: { claimId: claim.id, sourceLocator: measurement.sourceLocator, conditions: measurement.conditions ?? claim.conditions ?? null,
156
+ aggregation: measurement.aggregation ?? null, evidenceBasis: measurement.evidenceBasis ?? 'reported_numeric',
157
+ extractionUncertainty: measurement.extractionUncertainty ?? null },
158
+ });
159
+ }
160
+ idsByClaim.set(claim.id, ids);
161
+ }
162
+ return { records, idsByClaim };
163
+ }
@@ -0,0 +1,187 @@
1
+ import { CAP_RECORD_TYPE } from "./core.mjs";
2
+ import { sha256Bytes } from "./canonical-json.mjs";
3
+
4
+ export const CAP_SCHEMA = Object.freeze({
5
+ actor: "https://citeark.com/schemas/cap/v2/agent.schema.json",
6
+ sourceWork: "https://citeark.com/schemas/cap/v2/entity.schema.json",
7
+ claim: "https://citeark.com/schemas/cap/v2/assertion.schema.json",
8
+ experiment: "https://citeark.com/schemas/cap/v2/entity.schema.json",
9
+ execution: "https://citeark.com/schemas/cap/v2/activity.schema.json",
10
+ evidence: "https://citeark.com/schemas/cap/v2/entity.schema.json",
11
+ assessment: "https://citeark.com/schemas/cap/v2/assertion.schema.json",
12
+ });
13
+
14
+ export function capRecordId(kind, value) {
15
+ return `urn:citeark:${kind}:${sha256Bytes(Buffer.from(String(value), "utf8")).slice(7)}`;
16
+ }
17
+
18
+ export function capActorRecord(id, name, kind, version) {
19
+ return {
20
+ $schema: CAP_SCHEMA.actor,
21
+ id,
22
+ type: CAP_RECORD_TYPE.agent, role: "agent",
23
+ kind,
24
+ name,
25
+ ...(version ? { version: String(version) } : {}),
26
+ };
27
+ }
28
+
29
+ export function capSourceWorkRecord({
30
+ research,
31
+ id,
32
+ processingBinding,
33
+ }) {
34
+ return {
35
+ $schema: CAP_SCHEMA.sourceWork,
36
+ id,
37
+ type: CAP_RECORD_TYPE.entity, role: "source-work",
38
+ title: research.work.title,
39
+ abstract: research.work.abstract,
40
+ sources: research.sources.map((source) => capSourceDescriptor({
41
+ source,
42
+ research,
43
+ processingBinding,
44
+ })),
45
+ citeark: {
46
+ workId: research.work.id,
47
+ ...(research.compilationStage ? { compilationStage: research.compilationStage } : {}),
48
+ ...(research.sourceInventory ? { sourceInventory: structuredClone(research.sourceInventory) } : {}),
49
+ ...(research.reproductionScope ? { reproductionScope: research.reproductionScope } : {}),
50
+ ...(research.work.significance ? { significance: research.work.significance } : {}),
51
+ authorNames: research.work.authors ?? [],
52
+ subjects: research.work.subjects ?? [],
53
+ license: research.license ?? {},
54
+ provenance: research.provenance ?? {},
55
+ coverage: research.coverage ?? { items: [] },
56
+ lineage: research.lineage ?? [],
57
+ },
58
+ };
59
+ }
60
+
61
+ export function capClaimRecord({
62
+ claim,
63
+ id,
64
+ sourceWorkId,
65
+ actorId,
66
+ reportedEvidenceIds = [],
67
+ objectReferences = [],
68
+ }) {
69
+ const ref = (value) => ({ ref: value });
70
+ return {
71
+ $schema: CAP_SCHEMA.claim,
72
+ id,
73
+ type: CAP_RECORD_TYPE.assertion, role: "claim",
74
+ statement: { value: claim.statement, language: "en" },
75
+ kind: capClaimKind(claim.type),
76
+ ...(objectReferences.length ? { objects: objectReferences } : {}),
77
+ scope: {
78
+ reportedMeasurements: (claim.reportedMeasurements ?? []).map((item) => ({
79
+ id: item.id,
80
+ metric: item.metric,
81
+ value: capDecimal(item.value),
82
+ unit: item.unit,
83
+ evidenceBasis: item.evidenceBasis ?? "reported_numeric",
84
+ ...(item.extractionUncertainty !== undefined
85
+ ? { extractionUncertainty: capDecimal(item.extractionUncertainty) }
86
+ : {}),
87
+ })),
88
+ reproduction: claim.reproduction ?? {},
89
+ },
90
+ source: {
91
+ work: ref(sourceWorkId),
92
+ selector: {
93
+ type: "TextQuoteSelector",
94
+ exact: claim.statement,
95
+ label: `${claim.sourceLocator.sourceId}:${claim.sourceLocator.locator}`,
96
+ },
97
+ selectedTextDigest: sha256Bytes(Buffer.from(claim.statement, "utf8")),
98
+ },
99
+ origin: {
100
+ basis: "inferred",
101
+ actor: ref(actorId),
102
+ },
103
+ ...(reportedEvidenceIds.length
104
+ ? { reportedEvidence: reportedEvidenceIds.map(ref) }
105
+ : {}),
106
+ citeark: {
107
+ localId: claim.id,
108
+ versionId: claim.versionId,
109
+ type: claim.type,
110
+ sourceLocator: claim.sourceLocator,
111
+ ...Object.fromEntries(["description", "sourceLocators", "conditions", "relations", "ambiguities", "verificationHint"]
112
+ .filter((key) => claim[key] !== undefined).map((key) => [key, structuredClone(claim[key])])),
113
+ reportedMeasurements: claim.reportedMeasurements ?? [],
114
+ reproduction: claim.reproduction ?? {},
115
+ },
116
+ };
117
+ }
118
+
119
+ export function capSourceDescriptor({ source, research, processingBinding }) {
120
+ const paperDigest = source.kind === "paper" && capDigest(processingBinding?.sourceDigest)
121
+ ? processingBinding.sourceDigest
122
+ : capDigest(source.digest) ? source.digest : null;
123
+ return {
124
+ kind: capSourceKind(source.kind),
125
+ uri: source.uri,
126
+ ...(source.version ? { version: String(source.version) } : {}),
127
+ integrity: paperDigest ? "verified" : "unverified",
128
+ ...(paperDigest ? { digest: paperDigest } : {}),
129
+ rights: capSourceRights(source.kind, research.license),
130
+ };
131
+ }
132
+
133
+ export function capSourceRights(kind, license = {}) {
134
+ const key = kind === "repository"
135
+ ? "code"
136
+ : kind === "paper"
137
+ ? "paper"
138
+ : kind === "dataset"
139
+ ? "dataset"
140
+ : kind === "checkpoint" ? "checkpoint" : null;
141
+ return {
142
+ statement: key && license?.[key]
143
+ ? String(license[key])
144
+ : "Source-specific rights; CAP records identity and provenance but grants no redistribution permission.",
145
+ };
146
+ }
147
+
148
+ export function capClaimKind(value) {
149
+ return ({
150
+ finding: "descriptive",
151
+ method: "methodological",
152
+ measurement: "descriptive",
153
+ limitation: "limitation",
154
+ })[value] ?? "descriptive";
155
+ }
156
+
157
+ /** A source's proposed mechanism is an Assertion, without a reproduction disposition or verdict. */
158
+ export function capHypothesisRecords({ research, sourceWorkId, actorId, objectIds }) {
159
+ return (research.hypotheses ?? []).map(hypothesis => ({
160
+ $schema: CAP_SCHEMA.claim, id: capRecordId('hypothesis', `${research.work.id}:${hypothesis.id}`),
161
+ type: CAP_RECORD_TYPE.assertion, role: 'hypothesis',
162
+ statement: { value: hypothesis.statement, language: 'en' },
163
+ source: { work: { ref: sourceWorkId }, selector: { label: `${hypothesis.sourceLocator.sourceId}:${hypothesis.sourceLocator.locator}` } },
164
+ origin: { basis: 'inferred', actor: { ref: actorId } },
165
+ objects: (hypothesis.objectIds ?? []).map(id => ({ ref: objectIds.get(id) })),
166
+ ...(hypothesis.conditions !== undefined ? { scope: structuredClone(hypothesis.conditions) } : {}),
167
+ citeark: { localId: hypothesis.id, sourceLocator: hypothesis.sourceLocator,
168
+ limitations: hypothesis.limitations ?? [], status: 'proposed' },
169
+ }));
170
+ }
171
+
172
+ export function capSourceKind(value) {
173
+ if (value === "checkpoint") return "model";
174
+ return new Set(["paper", "repository", "dataset", "model", "supplement", "other"]).has(value)
175
+ ? value
176
+ : "other";
177
+ }
178
+
179
+ export function capDecimal(value) {
180
+ if (typeof value === "number" && Number.isFinite(value)) return String(value);
181
+ if (typeof value === "string" && /^-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?$/.test(value)) return value;
182
+ throw new Error(`CAP scientific number cannot be represented as a decimal string: ${String(value)}`);
183
+ }
184
+
185
+ export function capDigest(value) {
186
+ return typeof value === "string" && /^sha256:[0-9a-f]{64}$/.test(value);
187
+ }