@tangle-network/agent-knowledge 15.0.3 → 17.0.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.
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
- import { $ as formatKnowledgeCitationReference, $t as inspectPendingKnowledgeMutation, A as addSourceText, An as writeJsonDurableWithinRoot, At as ResearchSourceVersionSchema, B as KB_STORE_DIR, Bt as claimId, C as gradeClaims, Cn as renameDurable, Ct as KnowledgePageSchema, D as buildKnowledgeIndex, Dn as writeFileDurable, Dt as ResearchClaimEvidenceSchema, E as verifyGradeableEvidence, En as withSafeDirectory, Et as KnowledgeRelationSchema, F as ClaimLedgerMigrationRequiredError, Ft as assertDeepQuestionIntegrity, G as KnowledgeCitationAuditError, Gt as materializeRegisteredClaimEvidence, H as assertClaimLedgerId, Ht as deepQuestionId, I as FileSystemKbStore, It as assertResearchClaimEvidenceIntegrity, J as assertKnowledgeCitationAudit, Jt as normalizeClaimText, K as KnowledgeCitationResolutionError, Kt as mergeClaimLedgers, L as KB_CLAIM_LEDGER_DIR, Lt as assertResearchClaimLedgerIntegrity, M as snapshotSourceTextInput, Mn as textSourceAdapter, Mt as SourceRecordSchema, N as sourceRegistryPath, Nt as KNOWLEDGE_EVENT_TYPES, O as writeKnowledgeIndex, On as writeFileDurableWithinRoot, Ot as ResearchClaimLedgerSchema, P as writeSourceRegistry, Pt as ClaimLedgerGoalConflictError, Q as auditKnowledgeCitations, Qt as acquireDurableFileLock, R as KB_EVENTS_PATH, Rt as assertTrackedClaimIntegrity, S as claimCheckKey, Sn as removeDurable, St as KnowledgePageInvalidationSchema, T as verdictFor, Tn as withSafeDescendant, Tt as KnowledgeRelationNodeSchema, U as buildKnowledgeGraph, Ut as emptyClaimLedger, V as MemoryKbStore, Vt as claimSourceHost, W as knowledgePageRelations, Wt as linkClaimContradictions, X as assertRunScopedCitationsResolved, Xt as formatFrontmatter, Y as assertKnowledgeCitationsResolved, Yt as researchSourceVersionKey, Z as auditCurrentRunCitations, Zt as parseFrontmatter, _ as lintKnowledgeIndex, _n as isMissingFile, _t as KnowledgeBaseCandidateSchema, a as applyKnowledgeWriteBlocksFile, an as applyKnowledgeFileTransaction, at as SCAFFOLD_PAGE_BASENAMES, b as UncheckableClaimError, bn as readRegularFileWithinRoot, bt as KnowledgeGraphNodeSchema, c as KnowledgeDuplicateIntakeError, cn as finishKnowledgeFileTransaction, ct as isScaffoldPath, d as createFileRunLineageAuthority, dn as rollbackKnowledgeFileTransaction, dt as loadKnowledgePages, en as isKnowledgeMutationHeld, et as parseKnowledgeCitationReference, f as createRunScopedStores, fn as DEFAULT_PAGES_DIRECTORY, ft as writeJson, g as validateKnowledgeIndex, gn as isKernelAnchoredPath, gt as DeepQuestionSchema, h as normalizePageText, hn as canonicalRelativeWithinRoot, ht as normalizeLinkTarget, i as applyKnowledgeWriteBlocks, in as withKnowledgeRead, it as resolveRunScopedCitations, j as loadSourceRegistry, jn as mediaTypeFor, jt as SourceAnchorSchema, k as addSourcePath, kn as writeJsonDurable, kt as ResearchClaimRecordSchema, l as assertKnowledgeWriteIntake, ln as knowledgeFileTransactionPlanHash, lt as knowledgePageFromMarkdown, m as detectNearDuplicatePages, mn as canonicalPathsEqual, mt as extractWikilinks, n as inspectKnowledgeIndex, nn as runInKnowledgeMutationScope, nt as resolveKnowledgeCitations, o as isSafeKnowledgePath, on as assertKnowledgeMutationPath, ot as initKnowledgeBase, p as originatedPages, pn as normalizePagesDirectory, pt as WIKILINK_REGEX, q as assertCurrentRunCitationsResolved, qt as mergeTrackedClaims, r as stringMetadata, rn as withKnowledgeMutation, rt as resolveRunScopedCitation, s as parseKnowledgeWriteBlocks, sn as commitKnowledgeFileMutations, st as isKnowledgePagePath, t as explainKnowledgeTarget, tn as recoverPendingKnowledgeMutation, tt as resolveKnowledgeCitation, u as RUN_LINEAGE_BASENAME, un as prepareKnowledgeFileTransaction, ut as layoutFor, v as CHECKABLE_RUNG_THRESHOLD, vn as listRegularFilesWithinRoot, vt as KnowledgeEventSchema, w as gradeFor, wn as syncDirectory, wt as KnowledgeRelationGraphSchema, x as assertGradeableEvidence, xn as relativeWithinRoot, xt as KnowledgeIndexSchema, y as DEADLINE_EXIT_CODE, yn as readRegularFileNoFollow, yt as KnowledgeGraphEdgeSchema, z as KB_INDEX_PATH, zt as claimEvidenceId } from "./inspect-DMZXOQYl.js";
1
+ import { $ as formatKnowledgeCitationReference, $t as inspectPendingKnowledgeMutation, A as addSourceText, An as writeJsonDurableWithinRoot, At as ResearchSourceVersionSchema, B as KB_STORE_DIR, Bt as claimId, C as gradeClaims, Cn as renameDurable, Ct as KnowledgePageSchema, D as buildKnowledgeIndex, Dn as writeFileDurable, Dt as ResearchClaimEvidenceSchema, E as verifyGradeableEvidence, En as withSafeDirectory, Et as KnowledgeRelationSchema, F as ClaimLedgerMigrationRequiredError, Ft as assertDeepQuestionIntegrity, G as KnowledgeCitationAuditError, Gt as materializeRegisteredClaimEvidence, H as assertClaimLedgerId, Ht as deepQuestionId, I as FileSystemKbStore, It as assertResearchClaimEvidenceIntegrity, J as assertKnowledgeCitationAudit, Jt as normalizeClaimText, K as KnowledgeCitationResolutionError, Kt as mergeClaimLedgers, L as KB_CLAIM_LEDGER_DIR, Lt as assertResearchClaimLedgerIntegrity, M as snapshotSourceTextInput, Mn as textSourceAdapter, Mt as SourceRecordSchema, N as sourceRegistryPath, Nt as KNOWLEDGE_EVENT_TYPES, O as writeKnowledgeIndex, On as writeFileDurableWithinRoot, Ot as ResearchClaimLedgerSchema, P as writeSourceRegistry, Pt as ClaimLedgerGoalConflictError, Q as auditKnowledgeCitations, Qt as acquireDurableFileLock, R as KB_EVENTS_PATH, Rt as assertTrackedClaimIntegrity, S as claimCheckKey, Sn as removeDurable, St as KnowledgePageInvalidationSchema, T as verdictFor, Tn as withSafeDescendant, Tt as KnowledgeRelationNodeSchema, U as buildKnowledgeGraph, Ut as emptyClaimLedger, V as MemoryKbStore, Vt as claimSourceHost, W as knowledgePageRelations, Wt as linkClaimContradictions, X as assertRunScopedCitationsResolved, Xt as formatFrontmatter, Y as assertKnowledgeCitationsResolved, Yt as researchSourceVersionKey, Z as auditCurrentRunCitations, Zt as parseFrontmatter, _ as lintKnowledgeIndex, _n as isMissingFile, _t as KnowledgeBaseCandidateSchema, a as applyKnowledgeWriteBlocksFile, an as applyKnowledgeFileTransaction, at as SCAFFOLD_PAGE_BASENAMES, b as UncheckableClaimError, bn as readRegularFileWithinRoot, bt as KnowledgeGraphNodeSchema, c as KnowledgeDuplicateIntakeError, cn as finishKnowledgeFileTransaction, ct as isScaffoldPath, d as createFileRunLineageAuthority, dn as rollbackKnowledgeFileTransaction, dt as loadKnowledgePages, en as isKnowledgeMutationHeld, et as parseKnowledgeCitationReference, f as createRunScopedStores, fn as DEFAULT_PAGES_DIRECTORY, ft as writeJson, g as validateKnowledgeIndex, gn as isKernelAnchoredPath, gt as DeepQuestionSchema, h as normalizePageText, hn as canonicalRelativeWithinRoot, ht as normalizeLinkTarget, i as applyKnowledgeWriteBlocks, in as withKnowledgeRead, it as resolveRunScopedCitations, j as loadSourceRegistry, jn as mediaTypeFor, jt as SourceAnchorSchema, k as addSourcePath, kn as writeJsonDurable, kt as ResearchClaimRecordSchema, l as assertKnowledgeWriteIntake, ln as knowledgeFileTransactionPlanHash, lt as knowledgePageFromMarkdown, m as detectNearDuplicatePages, mn as canonicalPathsEqual, mt as extractWikilinks, n as inspectKnowledgeIndex, nn as runInKnowledgeMutationScope, nt as resolveKnowledgeCitations, o as isSafeKnowledgePath, on as assertKnowledgeMutationPath, ot as initKnowledgeBase, p as originatedPages, pn as normalizePagesDirectory, pt as WIKILINK_REGEX, q as assertCurrentRunCitationsResolved, qt as mergeTrackedClaims, r as stringMetadata, rn as withKnowledgeMutation, rt as resolveRunScopedCitation, s as parseKnowledgeWriteBlocks, sn as commitKnowledgeFileMutations, st as isKnowledgePagePath, t as explainKnowledgeTarget, tn as recoverPendingKnowledgeMutation, tt as resolveKnowledgeCitation, u as RUN_LINEAGE_BASENAME, un as prepareKnowledgeFileTransaction, ut as layoutFor, v as CHECKABLE_RUNG_THRESHOLD, vn as listRegularFilesWithinRoot, vt as KnowledgeEventSchema, w as gradeFor, wn as syncDirectory, wt as KnowledgeRelationGraphSchema, x as assertGradeableEvidence, xn as relativeWithinRoot, xt as KnowledgeIndexSchema, y as DEADLINE_EXIT_CODE, yn as readRegularFileNoFollow, yt as KnowledgeGraphEdgeSchema, z as KB_INDEX_PATH, zt as claimEvidenceId } from "./inspect-Dn7xCZLn.js";
2
2
  import { i as textSourceId, n as slugify, r as stableId, t as sha256 } from "./ids-Bevz_pXV.js";
3
- import { A as runSerializedKnowledgeOptimization, C as deterministicRng, D as toOffPolicyTrajectory, E as retrievalHoldoutConfigHash, M as assertImmutableRef, O as jsonCandidateCodec, S as applySessionStickyRetrievalHoldout, T as resetRetrievalHoldoutRegistry, _ as createAgentMemoryBranch, a as createNeo4jAgentMemoryAdapter, b as renderMemoryContext, c as runAgentMemoryImprovement, d as compareAgentMemoryLearning, f as runAgentMemoryLearningExperiment, g as buildAgentMemorySequencesFromBenchmarkCases, h as buildAgentMemorySequenceScenarios, i as AgentMemoryWriteInputSchema, j as scenarioContentFingerprint, k as jsonObjectCandidateCodec, l as createGraphitiMemoryAdapter, m as agentMemorySequenceJudge, n as AgentMemoryKindSchema, o as createMem0MemoryAdapter, p as runAgentMemoryExperiment, r as AgentMemoryScopeSchema, s as mem0MemoryAdapterIdentity, t as AgentMemoryHitSchema, u as graphitiMemoryAdapterIdentity, v as forkAgentMemoryBranchSnapshot, w as emitRetrievalHoldoutBypass, x as applyRetrievalHoldout, y as defaultGetMemoryContext } from "./memory-BxwOtvd7.js";
3
+ import { A as runSerializedKnowledgeOptimization, C as deterministicRng, D as toOffPolicyTrajectory, E as retrievalHoldoutConfigHash, M as assertImmutableRef, O as jsonCandidateCodec, S as applySessionStickyRetrievalHoldout, T as resetRetrievalHoldoutRegistry, _ as createAgentMemoryBranch, a as createNeo4jAgentMemoryAdapter, b as renderMemoryContext, c as runAgentMemoryImprovement, d as compareAgentMemoryLearning, f as runAgentMemoryLearningExperiment, g as buildAgentMemorySequencesFromBenchmarkCases, h as buildAgentMemorySequenceScenarios, i as AgentMemoryWriteInputSchema, j as scenarioContentFingerprint, k as jsonObjectCandidateCodec, l as createGraphitiMemoryAdapter, m as agentMemorySequenceJudge, n as AgentMemoryKindSchema, o as createMem0MemoryAdapter, p as runAgentMemoryExperiment, r as AgentMemoryScopeSchema, s as mem0MemoryAdapterIdentity, t as AgentMemoryHitSchema, u as graphitiMemoryAdapterIdentity, v as forkAgentMemoryBranchSnapshot, w as emitRetrievalHoldoutBypass, x as applyRetrievalHoldout, y as defaultGetMemoryContext } from "./memory-ce9n5O24.js";
4
4
  import { C as sleepForMemoryRecovery, F as buildIndustryRagBenchmarkSmokeCases, G as buildRetrievalEvalDispatch, H as createNoopMemoryBenchmarkAdapter, I as respondToIndustryMemoryBenchmarkSmokeCase, J as retrievalConfigSurface, K as partitionRetrievalScenarios, L as respondToIndustryRagBenchmarkSmokeCase, M as INDUSTRY_RAG_BENCHMARKS, N as buildFirstPartyMemoryLifecycleBenchmarkCases, P as buildIndustryMemoryBenchmarkSmokeCases, R as isKnowledgeMemoryBenchmarkCase, S as runBoundedMemoryLifecycle, U as memoryHitToSourceRecord, V as createInMemoryBenchmarkAdapter, W as memoryWriteResultToSourceRecord, X as scoreRetrievalArtifact, Y as retrievalRecallJudge, Z as mean, a as buildKnowledgeBenchmarkScenarios, b as memoryRecoveryDelayMs, c as runKnowledgeBenchmarkSuite, d as summarizeKnowledgeBenchmarkCampaign, f as acquireAgentMemoryRunLease, h as DEFAULT_MEMORY_CLEANUP_TIMEOUT_MS, i as runMemoryAdapterBenchmark, j as INDUSTRY_MEMORY_BENCHMARKS, l as scoreKnowledgeBenchmarkArtifact, m as AgentMemoryLifecycleUnsafeError, n as parseKnowledgeBenchmarkJsonl, o as knowledgeBenchmarkJudge, p as AgentMemoryLifecycleTimeoutError, q as retrievalConfigFromSurface, r as parseKnowledgeBenchmarkQrels, s as renderKnowledgeBenchmarkReportMarkdown, t as buildRetrievalBenchmarkCasesFromQrels, u as scoreMemoryBenchmarkArtifact, x as resolveMemoryCleanupTimeoutMs, y as createMemoryExecutionPool } from "./benchmarks-BbPJdmHe.js";
5
5
  import { a as buildKnowledgeLexicalIndex, c as tokenizeText, i as searchKnowledgePages, n as reciprocalRankFusion, o as scoreBm25, r as searchKnowledge, s as tokenizeQuery, t as KNOWLEDGE_SEARCH_RETRIEVER_ID } from "./search-Cw6eYfSd.js";
6
6
  import { IRS_DIMENSION_HINTS, MAX_RESPONSE_BYTES, MIN_REQUEST_GAP_MS, POLITE_USER_AGENT, __resetHttpThrottle, createCornellLiiSource, createIrsPublicationsSource, createStateSosSource, extractLinks, firstMatch, htmlToText, innerHtmlById, looksLikeBlockPage, politeFetch } from "./sources/index.js";
@@ -2583,7 +2583,7 @@ function rejectUnsafePromotionEvidence(evidence) {
2583
2583
  if (!comparison.totalCost.accountingComplete) reasons.push(`${label} final comparison has incomplete cost accounting`);
2584
2584
  const optimizerSource = comparison.best.provenance?.source;
2585
2585
  if (optimizerSource && optimizerSource.evidence !== "observed") reasons.push(`${label} optimizer package identity was not observed`);
2586
- if (comparison.best.liftCi.low < 0) reasons.push(`${label} final comparison does not rule out a regression`);
2586
+ if (!comparison.best.decision.promote) reasons.push(comparison.best.decision.low < 0 ? `${label} final comparison does not rule out a regression` : `${label} final comparison does not establish the required improvement`);
2587
2587
  if (costCeiling !== void 0 && exceedsCostCeiling(comparison.totalCost.totalCostUsd, costCeiling)) reasons.push(`${label} final comparison cost ${comparison.totalCost.totalCostUsd} exceeds ${costCeiling}`);
2588
2588
  }
2589
2589
  if (evidence.answerQuality) reasons.push(...ragAnswerEvidenceRejectionReasons(evidence.answerQuality, evidence.answerQualityCostCeiling));
@@ -5979,32 +5979,9 @@ function originOf(originByPage, hit) {
5979
5979
  return origin;
5980
5980
  }
5981
5981
  //#endregion
5982
- //#region src/knowledge-use-receipts.ts
5982
+ //#region src/knowledge-visibility.ts
5983
5983
  const KNOWLEDGE_USE_RECEIPT_SCHEMA_VERSION = "2.0.0";
5984
5984
  const KNOWLEDGE_RECEIPT_DIGEST_ALGORITHM = "rfc8785-sha256";
5985
- /**
5986
- * The visibility snapshot a receipt references could not be obtained, so the
5987
- * result-to-visibility join cannot be proven. This is distinct from a snapshot
5988
- * that loads and fails to match.
5989
- */
5990
- var KnowledgeVisibilityUnavailableError = class extends Error {
5991
- reason;
5992
- snapshotDigest;
5993
- uri;
5994
- constructor(input) {
5995
- const where = input.uri === void 0 ? "" : ` at '${input.uri}'`;
5996
- const detail = {
5997
- "no-artifact-reference": "the receipt carries no artifact reference",
5998
- missing: `no artifact is stored${where}`,
5999
- "load-failed": `the artifact${where} could not be loaded`
6000
- }[input.reason];
6001
- super(`knowledge visibility snapshot ${input.snapshotDigest} is unavailable: ${detail}`, input.cause === void 0 ? void 0 : { cause: input.cause });
6002
- this.name = "KnowledgeVisibilityUnavailableError";
6003
- this.reason = input.reason;
6004
- this.snapshotDigest = input.snapshotDigest;
6005
- if (input.uri !== void 0) this.uri = input.uri;
6006
- }
6007
- };
6008
5985
  /** Stable content identity for one exact knowledge page. */
6009
5986
  function knowledgePageDigest(page) {
6010
5987
  validateKnowledgePage(page);
@@ -6110,6 +6087,128 @@ function knowledgeVisibilityArtifactRef(input) {
6110
6087
  byteLength: input.bytes.byteLength
6111
6088
  });
6112
6089
  }
6090
+ const visibilityIndexes = /* @__PURE__ */ new WeakMap();
6091
+ function visibilityIndexOf(snapshot) {
6092
+ if (!snapshot || typeof snapshot !== "object") throw new TypeError("knowledge visibility snapshot is required");
6093
+ const cached = visibilityIndexes.get(snapshot);
6094
+ if (cached !== void 0) return cached;
6095
+ assertSchemaVersion(snapshot.schemaVersion, "knowledge visibility");
6096
+ if (snapshot.digestAlgorithm !== "rfc8785-sha256") throw new Error(`unsupported knowledge visibility digestAlgorithm '${snapshot.digestAlgorithm}'`);
6097
+ if (!Array.isArray(snapshot.entries)) throw new TypeError("knowledge visibility snapshot entries must be an array");
6098
+ if (canonicalCandidateDigest(visibilityMaterial(snapshot.entries)) !== digest(snapshot.snapshotDigest, "knowledge visibility snapshotDigest")) throw new Error("knowledge visibility snapshot digest mismatch");
6099
+ const index = indexVisibility(snapshot);
6100
+ visibilityIndexes.set(snapshot, index);
6101
+ return index;
6102
+ }
6103
+ function indexVisibility(snapshot) {
6104
+ const byIdentity = /* @__PURE__ */ new Map();
6105
+ for (const entry of snapshot.entries) {
6106
+ const identity = visibilityIdentity(entry.origin, entry.path);
6107
+ if (byIdentity.has(identity)) throw new Error(`knowledge visibility repeats path '${entry.path}' at origin '${entry.origin}'`);
6108
+ byIdentity.set(identity, entry);
6109
+ }
6110
+ return {
6111
+ snapshot,
6112
+ byIdentity
6113
+ };
6114
+ }
6115
+ function visibilityIdentity(origin, path) {
6116
+ return `${origin}\u0000${path}`;
6117
+ }
6118
+ function assertSchemaVersion(value, label) {
6119
+ if (value === "2.0.0") return;
6120
+ throw new Error(`unsupported ${label} schemaVersion '${String(value)}'${value === "1.0.0" ? "; 1.0.0 records embed the visibility snapshot and have no reader, create a 2.0.0 record from the snapshot" : ""}`);
6121
+ }
6122
+ function visibilityMaterial(entries) {
6123
+ return {
6124
+ schemaVersion: KNOWLEDGE_USE_RECEIPT_SCHEMA_VERSION,
6125
+ digestAlgorithm: KNOWLEDGE_RECEIPT_DIGEST_ALGORITHM,
6126
+ entries: entries.map((entry, position) => {
6127
+ if (!entry || typeof entry !== "object") throw new TypeError(`knowledge visibility[${position}] must be an object`);
6128
+ if (entry.position !== position) throw new Error(`knowledge visibility position mismatch: expected ${position}, observed ${entry.position}`);
6129
+ if (!Array.isArray(entry.sourceIds)) throw new TypeError(`knowledge visibility[${position}].sourceIds must be an array`);
6130
+ return {
6131
+ position,
6132
+ pageId: nonEmpty$1(entry.pageId, `knowledge visibility[${position}].pageId`),
6133
+ origin: validateOrigin(entry.origin, `knowledge visibility[${position}].origin`),
6134
+ path: nonEmpty$1(entry.path, `knowledge visibility[${position}].path`),
6135
+ pageDigest: digest(entry.pageDigest, `knowledge visibility[${position}].pageDigest`),
6136
+ sourceIds: entry.sourceIds.map((sourceId, sourceIndex) => nonEmpty$1(sourceId, `knowledge visibility[${position}].sourceIds[${sourceIndex}]`)),
6137
+ invalidated: Boolean(entry.invalidated)
6138
+ };
6139
+ })
6140
+ };
6141
+ }
6142
+ function normalizeVisibilityRef(input) {
6143
+ if (!input || typeof input !== "object") throw new TypeError("knowledge retrieval visibility reference is required");
6144
+ if (!Number.isSafeInteger(input.pageCount) || input.pageCount < 0) throw new TypeError("knowledge retrieval visibility pageCount must be a non-negative integer");
6145
+ return Object.freeze({
6146
+ snapshotDigest: digest(input.snapshotDigest, "knowledge retrieval visibility snapshotDigest"),
6147
+ pageCount: input.pageCount,
6148
+ ...input.artifact === void 0 ? {} : { artifact: normalizeVisibilityArtifact(input.artifact, "knowledge retrieval visibility artifact") }
6149
+ });
6150
+ }
6151
+ function normalizeVisibilityArtifact(input, label) {
6152
+ if (!input || typeof input !== "object") throw new TypeError(`${label} must be an object`);
6153
+ if (!Number.isSafeInteger(input.byteLength) || input.byteLength < 1) throw new TypeError(`${label} byteLength must be a positive integer`);
6154
+ return Object.freeze({
6155
+ uri: nonEmpty$1(input.uri, `${label} uri`),
6156
+ digest: digest(input.digest, `${label} digest`),
6157
+ byteLength: input.byteLength
6158
+ });
6159
+ }
6160
+ function validateKnowledgePage(page) {
6161
+ if (!page || typeof page !== "object") throw new TypeError("knowledge page must be an object");
6162
+ nonEmpty$1(page.id, "knowledge page id");
6163
+ nonEmpty$1(page.path, "knowledge page path");
6164
+ nonEmpty$1(page.title, "knowledge page title");
6165
+ if (typeof page.text !== "string") throw new TypeError("knowledge page text must be a string");
6166
+ if (!page.frontmatter || typeof page.frontmatter !== "object" || Array.isArray(page.frontmatter)) throw new TypeError("knowledge page frontmatter must be an object");
6167
+ for (const [name, values] of [
6168
+ ["sourceIds", page.sourceIds],
6169
+ ["tags", page.tags],
6170
+ ["outLinks", page.outLinks]
6171
+ ]) if (!Array.isArray(values) || values.some((value) => typeof value !== "string")) throw new TypeError(`knowledge page ${name} must be a string array`);
6172
+ }
6173
+ function validateOrigin(value, label) {
6174
+ if (value === "here" || value === "shared") return value;
6175
+ if (typeof value === "string" && value.startsWith("inherited:") && value.slice(10).trim().length > 0) return value;
6176
+ throw new TypeError(`${label} is invalid: ${String(value)}`);
6177
+ }
6178
+ function digest(value, label) {
6179
+ const parsed = sha256DigestSchema.safeParse(value);
6180
+ if (!parsed.success) throw new TypeError(`${label} must be a lowercase sha256 digest`);
6181
+ return parsed.data;
6182
+ }
6183
+ function nonEmpty$1(value, label) {
6184
+ if (typeof value !== "string" || value.trim().length === 0) throw new TypeError(`${label} must be a non-empty string`);
6185
+ return value.trim();
6186
+ }
6187
+ //#endregion
6188
+ //#region src/knowledge-use-receipts.ts
6189
+ /**
6190
+ * The visibility snapshot a receipt references could not be obtained, so the
6191
+ * result-to-visibility join cannot be proven. This is distinct from a snapshot
6192
+ * that loads and fails to match.
6193
+ */
6194
+ var KnowledgeVisibilityUnavailableError = class extends Error {
6195
+ reason;
6196
+ snapshotDigest;
6197
+ uri;
6198
+ constructor(input) {
6199
+ const where = input.uri === void 0 ? "" : ` at '${input.uri}'`;
6200
+ const detail = {
6201
+ "no-artifact-reference": "the receipt carries no artifact reference",
6202
+ missing: `no artifact is stored${where}`,
6203
+ "load-failed": `the artifact${where} could not be loaded`
6204
+ }[input.reason];
6205
+ super(`knowledge visibility snapshot ${input.snapshotDigest} is unavailable: ${detail}`, input.cause === void 0 ? void 0 : { cause: input.cause });
6206
+ this.name = "KnowledgeVisibilityUnavailableError";
6207
+ this.reason = input.reason;
6208
+ this.snapshotDigest = input.snapshotDigest;
6209
+ if (input.uri !== void 0) this.uri = input.uri;
6210
+ }
6211
+ };
6113
6212
  /**
6114
6213
  * Create a retrieval receipt over a precomputed snapshot. Every result must
6115
6214
  * occur in the snapshot with the same page id and bytes; the receipt stores the
@@ -6314,34 +6413,6 @@ function verifyKnowledgeRetrievalDisposition(disposition, retrieval) {
6314
6413
  })) !== disposition.receiptDigest) throw new Error("knowledge retrieval disposition digest mismatch");
6315
6414
  return disposition;
6316
6415
  }
6317
- const visibilityIndexes = /* @__PURE__ */ new WeakMap();
6318
- function visibilityIndexOf(snapshot) {
6319
- if (!snapshot || typeof snapshot !== "object") throw new TypeError("knowledge visibility snapshot is required");
6320
- const cached = visibilityIndexes.get(snapshot);
6321
- if (cached !== void 0) return cached;
6322
- assertSchemaVersion(snapshot.schemaVersion, "knowledge visibility");
6323
- if (snapshot.digestAlgorithm !== "rfc8785-sha256") throw new Error(`unsupported knowledge visibility digestAlgorithm '${snapshot.digestAlgorithm}'`);
6324
- if (!Array.isArray(snapshot.entries)) throw new TypeError("knowledge visibility snapshot entries must be an array");
6325
- if (canonicalCandidateDigest(visibilityMaterial(snapshot.entries)) !== digest(snapshot.snapshotDigest, "knowledge visibility snapshotDigest")) throw new Error("knowledge visibility snapshot digest mismatch");
6326
- const index = indexVisibility(snapshot);
6327
- visibilityIndexes.set(snapshot, index);
6328
- return index;
6329
- }
6330
- function indexVisibility(snapshot) {
6331
- const byIdentity = /* @__PURE__ */ new Map();
6332
- for (const entry of snapshot.entries) {
6333
- const identity = visibilityIdentity(entry.origin, entry.path);
6334
- if (byIdentity.has(identity)) throw new Error(`knowledge visibility repeats path '${entry.path}' at origin '${entry.origin}'`);
6335
- byIdentity.set(identity, entry);
6336
- }
6337
- return {
6338
- snapshot,
6339
- byIdentity
6340
- };
6341
- }
6342
- function visibilityIdentity(origin, path) {
6343
- return `${origin}\u0000${path}`;
6344
- }
6345
6416
  function assertReceiptJoinsVisibility(receipt, index) {
6346
6417
  if (index.snapshot.snapshotDigest !== receipt.visibility.snapshotDigest || index.snapshot.entries.length !== receipt.visibility.pageCount) throw new Error("knowledge retrieval receipt does not match the supplied visibility snapshot");
6347
6418
  for (const result of receipt.results) {
@@ -6349,30 +6420,6 @@ function assertReceiptJoinsVisibility(receipt, index) {
6349
6420
  if (!entry || entry.pageId !== result.pageId || entry.pageDigest !== result.pageDigest) throw new Error(`knowledge retrieval result rank ${result.rank} is not in the visibility snapshot`);
6350
6421
  }
6351
6422
  }
6352
- function assertSchemaVersion(value, label) {
6353
- if (value === "2.0.0") return;
6354
- throw new Error(`unsupported ${label} schemaVersion '${String(value)}'${value === "1.0.0" ? "; 1.0.0 records embed the visibility snapshot and have no reader, create a 2.0.0 record from the snapshot" : ""}`);
6355
- }
6356
- function visibilityMaterial(entries) {
6357
- return {
6358
- schemaVersion: KNOWLEDGE_USE_RECEIPT_SCHEMA_VERSION,
6359
- digestAlgorithm: KNOWLEDGE_RECEIPT_DIGEST_ALGORITHM,
6360
- entries: entries.map((entry, position) => {
6361
- if (!entry || typeof entry !== "object") throw new TypeError(`knowledge visibility[${position}] must be an object`);
6362
- if (entry.position !== position) throw new Error(`knowledge visibility position mismatch: expected ${position}, observed ${entry.position}`);
6363
- if (!Array.isArray(entry.sourceIds)) throw new TypeError(`knowledge visibility[${position}].sourceIds must be an array`);
6364
- return {
6365
- position,
6366
- pageId: nonEmpty$1(entry.pageId, `knowledge visibility[${position}].pageId`),
6367
- origin: validateOrigin(entry.origin, `knowledge visibility[${position}].origin`),
6368
- path: nonEmpty$1(entry.path, `knowledge visibility[${position}].path`),
6369
- pageDigest: digest(entry.pageDigest, `knowledge visibility[${position}].pageDigest`),
6370
- sourceIds: entry.sourceIds.map((sourceId, sourceIndex) => nonEmpty$1(sourceId, `knowledge visibility[${position}].sourceIds[${sourceIndex}]`)),
6371
- invalidated: Boolean(entry.invalidated)
6372
- };
6373
- })
6374
- };
6375
- }
6376
6423
  function retrievalMaterial(input) {
6377
6424
  return {
6378
6425
  schemaVersion: KNOWLEDGE_USE_RECEIPT_SCHEMA_VERSION,
@@ -6503,24 +6550,6 @@ function assertResultScores(result, position) {
6503
6550
  finite(result.normalizedScore, `knowledge retrieval results[${position}].normalizedScore`);
6504
6551
  if (result.normalizedScore < 0 || result.normalizedScore > 1) throw new TypeError(`knowledge retrieval results[${position}].normalizedScore must be in [0,1]`);
6505
6552
  }
6506
- function normalizeVisibilityRef(input) {
6507
- if (!input || typeof input !== "object") throw new TypeError("knowledge retrieval visibility reference is required");
6508
- if (!Number.isSafeInteger(input.pageCount) || input.pageCount < 0) throw new TypeError("knowledge retrieval visibility pageCount must be a non-negative integer");
6509
- return Object.freeze({
6510
- snapshotDigest: digest(input.snapshotDigest, "knowledge retrieval visibility snapshotDigest"),
6511
- pageCount: input.pageCount,
6512
- ...input.artifact === void 0 ? {} : { artifact: normalizeVisibilityArtifact(input.artifact, "knowledge retrieval visibility artifact") }
6513
- });
6514
- }
6515
- function normalizeVisibilityArtifact(input, label) {
6516
- if (!input || typeof input !== "object") throw new TypeError(`${label} must be an object`);
6517
- if (!Number.isSafeInteger(input.byteLength) || input.byteLength < 1) throw new TypeError(`${label} byteLength must be a positive integer`);
6518
- return Object.freeze({
6519
- uri: nonEmpty$1(input.uri, `${label} uri`),
6520
- digest: digest(input.digest, `${label} digest`),
6521
- byteLength: input.byteLength
6522
- });
6523
- }
6524
6553
  function normalizeRetriever(input) {
6525
6554
  if (!input || typeof input !== "object") throw new TypeError("knowledge retriever identity is required");
6526
6555
  return Object.freeze({
@@ -6584,36 +6613,9 @@ function validateUseRelation(value) {
6584
6613
  ].includes(value)) throw new TypeError(`knowledge use relation is invalid: ${String(value)}`);
6585
6614
  return value;
6586
6615
  }
6587
- function validateKnowledgePage(page) {
6588
- if (!page || typeof page !== "object") throw new TypeError("knowledge page must be an object");
6589
- nonEmpty$1(page.id, "knowledge page id");
6590
- nonEmpty$1(page.path, "knowledge page path");
6591
- nonEmpty$1(page.title, "knowledge page title");
6592
- if (typeof page.text !== "string") throw new TypeError("knowledge page text must be a string");
6593
- if (!page.frontmatter || typeof page.frontmatter !== "object" || Array.isArray(page.frontmatter)) throw new TypeError("knowledge page frontmatter must be an object");
6594
- for (const [name, values] of [
6595
- ["sourceIds", page.sourceIds],
6596
- ["tags", page.tags],
6597
- ["outLinks", page.outLinks]
6598
- ]) if (!Array.isArray(values) || values.some((value) => typeof value !== "string")) throw new TypeError(`knowledge page ${name} must be a string array`);
6599
- }
6600
- function validateOrigin(value, label) {
6601
- if (value === "here" || value === "shared") return value;
6602
- if (typeof value === "string" && value.startsWith("inherited:") && value.slice(10).trim().length > 0) return value;
6603
- throw new TypeError(`${label} is invalid: ${String(value)}`);
6604
- }
6605
- function digest(value, label) {
6606
- const parsed = sha256DigestSchema.safeParse(value);
6607
- if (!parsed.success) throw new TypeError(`${label} must be a lowercase sha256 digest`);
6608
- return parsed.data;
6609
- }
6610
6616
  function optionalDigest(value, label) {
6611
6617
  return value === void 0 ? void 0 : digest(value, label);
6612
6618
  }
6613
- function nonEmpty$1(value, label) {
6614
- if (typeof value !== "string" || value.trim().length === 0) throw new TypeError(`${label} must be a non-empty string`);
6615
- return value.trim();
6616
- }
6617
6619
  function optionalText(value, label) {
6618
6620
  return value === void 0 ? void 0 : nonEmpty$1(value, label);
6619
6621
  }
@@ -6708,6 +6710,7 @@ function createKnowledgeTools(options) {
6708
6710
  const intake = options.intake;
6709
6711
  return applyKnowledgeWriteBlocks(stores.storePath(runId), input.proposal, {
6710
6712
  ...pages,
6713
+ retainHistory: options.retainHistory,
6711
6714
  ...intake === void 0 ? {} : { intake: {
6712
6715
  ...intake,
6713
6716
  inheritedPages: await inheritedOf(stores, runId)
@@ -6803,16 +6806,16 @@ async function promoteRunScopedPages(stores, runId, options) {
6803
6806
  const reason = nonEmpty(options.reason, "promotion reason");
6804
6807
  if (!Array.isArray(options.pageIds) || options.pageIds.length === 0) throw new TypeError("promotion pageIds must name at least one page");
6805
6808
  const travellers = collectTravellers(await stores.loadChain(runId), options.pageIds);
6809
+ const mutations = await Promise.all(travellers.map(async (traveller) => ({
6810
+ path: traveller.entry.page.path,
6811
+ content: await readPageBytes(stores, runId, traveller.entry, pagesDirectory)
6812
+ })));
6806
6813
  await initKnowledgeBase(sharedRoot);
6807
6814
  return withKnowledgeMutation(sharedRoot, async (lock) => {
6808
6815
  const existing = await loadKnowledgePages(sharedRoot, { pagesDirectory });
6809
6816
  assertNoIdentityConflict(travellers, existing);
6810
6817
  const promotedPaths = new Set(travellers.map((traveller) => traveller.entry.page.path));
6811
6818
  assertKnowledgeCitationsResolved(originatedPages([...existing.filter((page) => !promotedPaths.has(page.path)), ...travellers.map((traveller) => traveller.entry.page)], "shared"), travellers.flatMap((traveller) => (traveller.entry.page.cites ?? []).map((persisted) => parseKnowledgeCitationReference(persisted))));
6812
- const mutations = await Promise.all(travellers.map(async (traveller) => ({
6813
- path: traveller.entry.page.path,
6814
- content: await readPageBytes(stores, runId, traveller.entry)
6815
- })));
6816
6819
  await commitKnowledgeFileMutations({
6817
6820
  root: sharedRoot,
6818
6821
  transactionRoot: lock.transactionRoot,
@@ -6900,9 +6903,11 @@ function assertNoIdentityConflict(travellers, existing) {
6900
6903
  * The page as its own store holds it. A promoted page must be the same bytes
6901
6904
  * in both scopes, so the record's digest describes what a reader will load.
6902
6905
  */
6903
- async function readPageBytes(stores, runId, entry) {
6906
+ async function readPageBytes(stores, runId, entry, pagesDirectory) {
6904
6907
  const sourceRunId = sourceRunOf(entry.origin, runId);
6905
- return (await readRegularFileWithinRoot(stores.storePath(sourceRunId), entry.page.path)).bytes.toString("utf8");
6908
+ const content = (await readRegularFileWithinRoot(stores.storePath(sourceRunId), entry.page.path)).bytes.toString("utf8");
6909
+ if (knowledgePageDigest(knowledgePageFromMarkdown(entry.page.path, content, pagesDirectory)) !== knowledgePageDigest(entry.page)) throw new KnowledgePromotionError("path-conflict", `source page "${entry.page.path}" in run "${sourceRunId}" changed after the promotion snapshot`);
6910
+ return content;
6906
6911
  }
6907
6912
  function sourceRunOf(origin, runId) {
6908
6913
  if (origin === "here") return runId;