@indigoai-us/hq-cli 5.345.246 → 5.345.248
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/CHANGELOG.md +63 -0
- package/assets/lanes-hooks/hq-lanes-drain.sh +1 -1
- package/contracts/host-canary-release.json +14 -0
- package/dist/command-catalog.generated.d.ts +1116 -3
- package/dist/command-catalog.generated.js +1373 -3
- package/dist/command-registration-plan.d.ts +6 -0
- package/dist/command-registration-plan.js +1 -0
- package/dist/commands/install-global.d.ts +15 -0
- package/dist/commands/install-global.js +98 -4
- package/dist/commands/lanes-hold.js +7 -7
- package/dist/commands/lanes-resume.js +2 -2
- package/dist/commands/lanes-review.js +1 -1
- package/dist/commands/lanes-session-start.d.ts +1 -1
- package/dist/commands/lanes-session-start.js +3 -3
- package/dist/commands/lanes-shared.d.ts +1 -1
- package/dist/commands/lanes.d.ts +2 -2
- package/dist/commands/lanes.js +50 -50
- package/dist/commands/mcp-registration.d.ts +17 -0
- package/dist/commands/mcp-registration.js +119 -0
- package/dist/commands/mesh.d.ts +25 -0
- package/dist/commands/mesh.js +48 -8
- package/dist/commands/ontology-graph.d.ts +62 -0
- package/dist/commands/ontology-graph.js +439 -0
- package/dist/commands/pkg-install.js +1 -1
- package/dist/commands/reindex.js +1 -1
- package/dist/index.js +1 -1
- package/dist/lanes-monitor-check-argv.d.ts +1 -1
- package/dist/lanes-monitor-check-argv.js +1 -1
- package/dist/lanes-monitor-check-fast.d.ts +1 -1
- package/dist/lanes-monitor-check-fast.js +1 -1
- package/dist/lanes-session-start-argv.d.ts +1 -1
- package/dist/lanes-session-start-argv.js +1 -1
- package/dist/lib/capacity/sign-in.d.ts +1 -1
- package/dist/lib/capacity/sign-in.js +1 -1
- package/dist/lib/daemon/hook-requests-conduct-link.js +2 -2
- package/dist/lib/daemon/lanes-monitor-state.d.ts +2 -2
- package/dist/lib/daemon/lanes-monitor-state.js +2 -2
- package/dist/lib/daemon/merge-sections.d.ts +3 -3
- package/dist/lib/daemon/merge-sections.js +7 -7
- package/dist/lib/daemon/remote-control-pools.js +1 -1
- package/dist/lib/daemon/run.js +1 -1
- package/dist/lib/daemon/state-file.d.ts +1 -1
- package/dist/lib/daemon/state-file.js +1 -1
- package/dist/lib/doctor/checks/antigravity-wiring.d.ts +21 -0
- package/dist/lib/doctor/checks/antigravity-wiring.js +75 -0
- package/dist/lib/doctor/checks/anywhere.js +30 -0
- package/dist/lib/doctor/checks/runtime-probe.js +13 -0
- package/dist/lib/doctor/platform.d.ts +1 -1
- package/dist/lib/doctor/platform.js +5 -0
- package/dist/lib/doctor/registry.js +3 -0
- package/dist/lib/flag-snapshot-file.d.ts +2 -0
- package/dist/lib/flag-snapshot-file.js +118 -48
- package/dist/lib/hook-engine/canary-gate.d.ts +57 -3
- package/dist/lib/hook-engine/canary-gate.js +138 -6
- package/dist/lib/hook-engine/coverage.d.ts +91 -0
- package/dist/lib/hook-engine/coverage.js +184 -0
- package/dist/lib/hot-channels/graph-context.d.ts +105 -0
- package/dist/lib/hot-channels/graph-context.js +377 -0
- package/dist/lib/hot-channels/signals-pass.js +27 -1
- package/dist/lib/install/antigravity-targets.d.ts +39 -0
- package/dist/lib/install/antigravity-targets.js +122 -0
- package/dist/lib/lanes/claude-credential.d.ts +1 -1
- package/dist/lib/lanes/claude-credential.js +1 -1
- package/dist/lib/lanes/delivery-core.d.ts +1 -1
- package/dist/lib/lanes/delivery-core.js +1 -1
- package/dist/lib/lanes/delivery.d.ts +10 -10
- package/dist/lib/lanes/delivery.js +8 -8
- package/dist/lib/lanes/dropbox.d.ts +1 -1
- package/dist/lib/lanes/dropbox.js +1 -1
- package/dist/lib/lanes/hooks-install.js +2 -2
- package/dist/lib/lanes/index.d.ts +1 -1
- package/dist/lib/lanes/index.js +1 -1
- package/dist/lib/lanes/monitor.js +1 -1
- package/dist/lib/lanes/quota-notice.js +1 -1
- package/dist/lib/lanes/quota.d.ts +1 -1
- package/dist/lib/lanes/quota.js +1 -1
- package/dist/lib/lanes/repo-merge-hold.d.ts +2 -2
- package/dist/lib/lanes/repo-merge-hold.js +2 -2
- package/dist/lib/lanes/spawn.d.ts +4 -4
- package/dist/lib/lanes/spawn.js +5 -5
- package/dist/lib/mesh/live/company-flag-refresh.d.ts +61 -0
- package/dist/lib/mesh/live/company-flag-refresh.js +159 -0
- package/dist/lib/mesh/live/company-reporting-gaps.d.ts +61 -0
- package/dist/lib/mesh/live/company-reporting-gaps.js +238 -0
- package/dist/lib/mesh/live/daemon/duplicate-unit.d.ts +156 -0
- package/dist/lib/mesh/live/daemon/duplicate-unit.js +616 -0
- package/dist/lib/mesh/live/daemon/run.d.ts +13 -0
- package/dist/lib/mesh/live/daemon/run.js +35 -2
- package/dist/lib/mesh/live/direct-drain.js +1 -1
- package/dist/lib/mesh/live/emit.d.ts +3 -0
- package/dist/lib/mesh/live/emit.js +32 -9
- package/dist/lib/mesh/live/flush.d.ts +3 -0
- package/dist/lib/mesh/live/flush.js +115 -32
- package/dist/lib/mesh/live/liveness-carry.d.ts +1 -1
- package/dist/lib/mesh/live/liveness-carry.js +1 -1
- package/dist/lib/mesh/live/reporting-privacy.d.ts +119 -3
- package/dist/lib/mesh/live/reporting-privacy.js +235 -11
- package/dist/lib/mesh/live/spool.d.ts +17 -2
- package/dist/lib/mesh/live/spool.js +22 -6
- package/dist/lib/ontology-graph/legacy/coverage.d.ts +67 -0
- package/dist/lib/ontology-graph/legacy/coverage.js +192 -0
- package/dist/lib/ontology-graph/legacy/index.d.ts +199 -0
- package/dist/lib/ontology-graph/legacy/index.js +1150 -0
- package/dist/lib/ontology-graph/legacy/parse.d.ts +49 -0
- package/dist/lib/ontology-graph/legacy/parse.js +126 -0
- package/dist/lib/ontology-graph/legacy/report.d.ts +14 -0
- package/dist/lib/ontology-graph/legacy/report.js +254 -0
- package/dist/lib/ontology-graph/legacy/verb-map.d.ts +35 -0
- package/dist/lib/ontology-graph/legacy/verb-map.js +84 -0
- package/dist/lib/ontology-graph/lint/checks.d.ts +140 -0
- package/dist/lib/ontology-graph/lint/checks.js +717 -0
- package/dist/lib/ontology-graph/lint/coverage.d.ts +106 -0
- package/dist/lib/ontology-graph/lint/coverage.js +241 -0
- package/dist/lib/ontology-graph/lint/daily-job.d.ts +259 -0
- package/dist/lib/ontology-graph/lint/daily-job.js +595 -0
- package/dist/lib/ontology-graph/lint/index.d.ts +65 -0
- package/dist/lib/ontology-graph/lint/index.js +176 -0
- package/dist/lib/ontology-graph/model/files.d.ts +20 -0
- package/dist/lib/ontology-graph/model/files.js +89 -0
- package/dist/lib/ontology-graph/model/index.d.ts +65 -0
- package/dist/lib/ontology-graph/model/index.js +100 -0
- package/dist/lib/ontology-graph/model/pass-tasks.d.ts +156 -0
- package/dist/lib/ontology-graph/model/pass-tasks.js +345 -0
- package/dist/lib/ontology-graph/model/pass.d.ts +109 -0
- package/dist/lib/ontology-graph/model/pass.js +958 -0
- package/dist/lib/ontology-graph/model/runner.d.ts +50 -0
- package/dist/lib/ontology-graph/model/runner.js +160 -0
- package/dist/lib/ontology-graph/model/step.d.ts +195 -0
- package/dist/lib/ontology-graph/model/step.js +523 -0
- package/dist/lib/ontology-graph/model/tasks.d.ts +183 -0
- package/dist/lib/ontology-graph/model/tasks.js +349 -0
- package/dist/lib/ontology-graph/not-implemented.d.ts +5 -0
- package/dist/lib/ontology-graph/not-implemented.js +8 -0
- package/dist/lib/ontology-graph/org/compare.d.ts +28 -0
- package/dist/lib/ontology-graph/org/compare.js +239 -0
- package/dist/lib/ontology-graph/org/derive.d.ts +65 -0
- package/dist/lib/ontology-graph/org/derive.js +352 -0
- package/dist/lib/ontology-graph/org/index.d.ts +73 -0
- package/dist/lib/ontology-graph/org/index.js +513 -0
- package/dist/lib/ontology-graph/org/load.d.ts +31 -0
- package/dist/lib/ontology-graph/org/load.js +476 -0
- package/dist/lib/ontology-graph/org/role.d.ts +38 -0
- package/dist/lib/ontology-graph/org/role.js +268 -0
- package/dist/lib/ontology-graph/org/structure.d.ts +63 -0
- package/dist/lib/ontology-graph/org/structure.js +437 -0
- package/dist/lib/ontology-graph/org/text.d.ts +95 -0
- package/dist/lib/ontology-graph/org/text.js +315 -0
- package/dist/lib/ontology-graph/org/types.d.ts +452 -0
- package/dist/lib/ontology-graph/org/types.js +80 -0
- package/dist/lib/ontology-graph/pages/index.d.ts +64 -0
- package/dist/lib/ontology-graph/pages/index.js +193 -0
- package/dist/lib/ontology-graph/pages/render.d.ts +142 -0
- package/dist/lib/ontology-graph/pages/render.js +682 -0
- package/dist/lib/ontology-graph/proposals/apply.d.ts +109 -0
- package/dist/lib/ontology-graph/proposals/apply.js +468 -0
- package/dist/lib/ontology-graph/proposals/group.d.ts +63 -0
- package/dist/lib/ontology-graph/proposals/group.js +232 -0
- package/dist/lib/ontology-graph/proposals/index.d.ts +173 -0
- package/dist/lib/ontology-graph/proposals/index.js +474 -0
- package/dist/lib/ontology-graph/proposals/model.d.ts +162 -0
- package/dist/lib/ontology-graph/proposals/model.js +196 -0
- package/dist/lib/ontology-graph/proposals/org-chart.d.ts +82 -0
- package/dist/lib/ontology-graph/proposals/org-chart.js +340 -0
- package/dist/lib/ontology-graph/read/brief.d.ts +26 -0
- package/dist/lib/ontology-graph/read/brief.js +251 -0
- package/dist/lib/ontology-graph/read/graph-index.d.ts +76 -0
- package/dist/lib/ontology-graph/read/graph-index.js +198 -0
- package/dist/lib/ontology-graph/read/index.d.ts +59 -0
- package/dist/lib/ontology-graph/read/index.js +792 -0
- package/dist/lib/ontology-graph/read/member.d.ts +7 -0
- package/dist/lib/ontology-graph/read/member.js +34 -0
- package/dist/lib/ontology-graph/read/names.d.ts +44 -0
- package/dist/lib/ontology-graph/read/names.js +292 -0
- package/dist/lib/ontology-graph/read/reader-setting.d.ts +19 -0
- package/dist/lib/ontology-graph/read/reader-setting.js +62 -0
- package/dist/lib/ontology-graph/read/test-graph.d.ts +37 -0
- package/dist/lib/ontology-graph/read/test-graph.js +150 -0
- package/dist/lib/ontology-graph/replay/buffered-store.d.ts +33 -0
- package/dist/lib/ontology-graph/replay/buffered-store.js +50 -0
- package/dist/lib/ontology-graph/replay/config.d.ts +41 -0
- package/dist/lib/ontology-graph/replay/config.js +105 -0
- package/dist/lib/ontology-graph/replay/engine.d.ts +100 -0
- package/dist/lib/ontology-graph/replay/engine.js +1174 -0
- package/dist/lib/ontology-graph/replay/extractors.d.ts +59 -0
- package/dist/lib/ontology-graph/replay/extractors.js +1555 -0
- package/dist/lib/ontology-graph/replay/index.d.ts +90 -0
- package/dist/lib/ontology-graph/replay/index.js +220 -0
- package/dist/lib/ontology-graph/replay/listing-manifest.d.ts +66 -0
- package/dist/lib/ontology-graph/replay/listing-manifest.js +242 -0
- package/dist/lib/ontology-graph/replay/member.d.ts +19 -0
- package/dist/lib/ontology-graph/replay/member.js +65 -0
- package/dist/lib/ontology-graph/replay/mentions.d.ts +58 -0
- package/dist/lib/ontology-graph/replay/mentions.js +239 -0
- package/dist/lib/ontology-graph/replay/readers.d.ts +98 -0
- package/dist/lib/ontology-graph/replay/readers.js +1251 -0
- package/dist/lib/ontology-graph/replay/rules.d.ts +127 -0
- package/dist/lib/ontology-graph/replay/rules.js +374 -0
- package/dist/lib/ontology-graph/replay/scope.d.ts +35 -0
- package/dist/lib/ontology-graph/replay/scope.js +115 -0
- package/dist/lib/ontology-graph/replay/transcripts.d.ts +39 -0
- package/dist/lib/ontology-graph/replay/transcripts.js +224 -0
- package/dist/lib/ontology-graph/replay/util.d.ts +55 -0
- package/dist/lib/ontology-graph/replay/util.js +256 -0
- package/dist/lib/ontology-graph/resolve/entity-index.d.ts +159 -0
- package/dist/lib/ontology-graph/resolve/entity-index.js +652 -0
- package/dist/lib/ontology-graph/resolve/index.d.ts +113 -0
- package/dist/lib/ontology-graph/resolve/index.js +214 -0
- package/dist/lib/ontology-graph/resolve/machine.d.ts +34 -0
- package/dist/lib/ontology-graph/resolve/machine.js +95 -0
- package/dist/lib/ontology-graph/resolve/normalize.d.ts +35 -0
- package/dist/lib/ontology-graph/resolve/normalize.js +124 -0
- package/dist/lib/ontology-graph/resolve/org-chart.d.ts +107 -0
- package/dist/lib/ontology-graph/resolve/org-chart.js +578 -0
- package/dist/lib/ontology-graph/resolve/org-layer.d.ts +78 -0
- package/dist/lib/ontology-graph/resolve/org-layer.js +159 -0
- package/dist/lib/ontology-graph/resolve/org-seed.d.ts +141 -0
- package/dist/lib/ontology-graph/resolve/org-seed.js +631 -0
- package/dist/lib/ontology-graph/resolve/org-sources.d.ts +165 -0
- package/dist/lib/ontology-graph/resolve/org-sources.js +634 -0
- package/dist/lib/ontology-graph/resolve/roster.d.ts +76 -0
- package/dist/lib/ontology-graph/resolve/roster.js +226 -0
- package/dist/lib/ontology-graph/resolve/seed.d.ts +267 -0
- package/dist/lib/ontology-graph/resolve/seed.js +1198 -0
- package/dist/lib/ontology-graph/resolve/upsert.d.ts +58 -0
- package/dist/lib/ontology-graph/resolve/upsert.js +139 -0
- package/dist/lib/ontology-graph/resolve/writer.d.ts +128 -0
- package/dist/lib/ontology-graph/resolve/writer.js +351 -0
- package/dist/lib/ontology-graph/schema/authority.d.ts +73 -0
- package/dist/lib/ontology-graph/schema/authority.js +157 -0
- package/dist/lib/ontology-graph/schema/default-schema.d.ts +10 -0
- package/dist/lib/ontology-graph/schema/default-schema.js +242 -0
- package/dist/lib/ontology-graph/schema/grammar.d.ts +57 -0
- package/dist/lib/ontology-graph/schema/grammar.js +409 -0
- package/dist/lib/ontology-graph/schema/index.d.ts +12 -0
- package/dist/lib/ontology-graph/schema/index.js +10 -0
- package/dist/lib/ontology-graph/schema/validate.d.ts +13 -0
- package/dist/lib/ontology-graph/schema/validate.js +132 -0
- package/dist/lib/ontology-graph/store/files.d.ts +34 -0
- package/dist/lib/ontology-graph/store/files.js +207 -0
- package/dist/lib/ontology-graph/store/fs.d.ts +30 -0
- package/dist/lib/ontology-graph/store/fs.js +101 -0
- package/dist/lib/ontology-graph/store/graph-store.d.ts +14 -0
- package/dist/lib/ontology-graph/store/graph-store.js +520 -0
- package/dist/lib/ontology-graph/store/index.d.ts +18 -0
- package/dist/lib/ontology-graph/store/index.js +15 -0
- package/dist/lib/ontology-graph/store/ingest.d.ts +37 -0
- package/dist/lib/ontology-graph/store/ingest.js +323 -0
- package/dist/lib/ontology-graph/store/paths.d.ts +114 -0
- package/dist/lib/ontology-graph/store/paths.js +232 -0
- package/dist/lib/ontology-graph/types.d.ts +1183 -0
- package/dist/lib/ontology-graph/types.js +131 -0
- package/dist/lib/secret-scan/gitleaks-rules.generated.js +1 -1
- package/dist/utils/frontmatter.d.ts +14 -0
- package/dist/utils/frontmatter.js +18 -0
- package/dist/utils/interactive-caller.js +1 -1
- package/package.json +8 -8
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The file-backed GraphStore. Markdown files are canonical. A store instance
|
|
3
|
+
* keeps the files it has read in memory, which is safe because one process
|
|
4
|
+
* writes a graph folder at a time; open a new store to see another process's
|
|
5
|
+
* writes.
|
|
6
|
+
*/
|
|
7
|
+
import * as fs from "node:fs";
|
|
8
|
+
import * as path from "node:path";
|
|
9
|
+
import { GRAPH_ENTITY_TYPES, REPLAY_INPUT_KINDS, RowRefusedError, } from "../types.js";
|
|
10
|
+
import { DEFAULT_SCHEMA, parseSchemaYaml, renderSchemaYaml } from "../schema/default-schema.js";
|
|
11
|
+
import { compareRows, isAudienceKey, isEntityRef, isIsoDate, isSourceRef, parseEntityRef, rowId } from "../schema/grammar.js";
|
|
12
|
+
import { validateEntityUpsert, validateRow } from "../schema/validate.js";
|
|
13
|
+
import { rowCoversDay } from "../schema/authority.js";
|
|
14
|
+
import { LOG_HEADER, formatLogEntry, parseEntityFile, parseFactsFile, parseLog, renderEntityFile, renderFactsFile, sortedUnique, } from "./files.js";
|
|
15
|
+
import { GraphFileError, appendJsonLine, appendText, atomicWriteFile, listDir, readJsonLines, readTextIfExists } from "./fs.js";
|
|
16
|
+
import { GRAPH_PATHS, assertGraphCompany, audienceDir, entityFile, factsFile, graphPath } from "./paths.js";
|
|
17
|
+
const compareText = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
18
|
+
/** Two decimals, the precision the row grammar keeps. */
|
|
19
|
+
/** `[[ontology/entities/person/x`, `x]]`, or a path into the old entity folder. */
|
|
20
|
+
function isLinkRemnant(alias) {
|
|
21
|
+
return alias.includes("[[") || alias.includes("]]") || /(^|\/)ontology\/entities\//.test(alias);
|
|
22
|
+
}
|
|
23
|
+
function normalizeRow(row) {
|
|
24
|
+
if (row.kind === "T") {
|
|
25
|
+
return { kind: "T", date: row.date, signalType: row.signalType, text: row.text, src: row.src, ...(row.by !== undefined ? { by: row.by } : {}) };
|
|
26
|
+
}
|
|
27
|
+
const stamps = {
|
|
28
|
+
valid: row.valid.to === undefined ? { from: row.valid.from } : { from: row.valid.from, to: row.valid.to },
|
|
29
|
+
src: row.src,
|
|
30
|
+
learned: row.learned,
|
|
31
|
+
by: row.by,
|
|
32
|
+
conf: Math.round(row.conf * 100) / 100,
|
|
33
|
+
...(row.closed ? { closed: { on: row.closed.on, by: row.closed.by } } : {}),
|
|
34
|
+
};
|
|
35
|
+
if (row.kind === "F")
|
|
36
|
+
return { kind: "F", field: row.field, value: row.value, ...stamps };
|
|
37
|
+
return { kind: "E", verb: row.verb, target: row.target, ...stamps, ...(row.was !== undefined ? { was: row.was } : {}) };
|
|
38
|
+
}
|
|
39
|
+
const LEDGER_KEYS = [
|
|
40
|
+
"run", "command", "day", "inputs", "rowsAdded", "rowsClosed", "entitiesCreated", "modelCalls", "cacheHits",
|
|
41
|
+
"inputTokens", "outputTokens", "pending", "seconds", "at", "counters",
|
|
42
|
+
];
|
|
43
|
+
class FileGraphStore {
|
|
44
|
+
root;
|
|
45
|
+
defaultSchema;
|
|
46
|
+
cachedSchema = null;
|
|
47
|
+
entities = new Map();
|
|
48
|
+
facts = new Map();
|
|
49
|
+
dirty = null;
|
|
50
|
+
// Batch state (beginBatch .. flushBatch): what is held in memory and not yet on disk.
|
|
51
|
+
batching = false;
|
|
52
|
+
heldFiles = new Map();
|
|
53
|
+
heldLog = [];
|
|
54
|
+
heldDirty = new Set();
|
|
55
|
+
heldRows = 0;
|
|
56
|
+
heldClosures = 0;
|
|
57
|
+
constructor(root, options) {
|
|
58
|
+
this.root = path.resolve(root);
|
|
59
|
+
this.defaultSchema = options.schema ?? DEFAULT_SCHEMA;
|
|
60
|
+
}
|
|
61
|
+
// -- layout and schema ----------------------------------------------------
|
|
62
|
+
init() {
|
|
63
|
+
for (const dir of [GRAPH_PATHS.entities, GRAPH_PATHS.facts, GRAPH_PATHS.pages, "proposals", GRAPH_PATHS.state, GRAPH_PATHS.cache]) {
|
|
64
|
+
fs.mkdirSync(graphPath(this.root, dir), { recursive: true });
|
|
65
|
+
}
|
|
66
|
+
const schemaFile = graphPath(this.root, GRAPH_PATHS.schema);
|
|
67
|
+
if (!fs.existsSync(schemaFile))
|
|
68
|
+
atomicWriteFile(schemaFile, renderSchemaYaml(this.defaultSchema));
|
|
69
|
+
const logFile = graphPath(this.root, GRAPH_PATHS.log);
|
|
70
|
+
if (!fs.existsSync(logFile))
|
|
71
|
+
atomicWriteFile(logFile, LOG_HEADER);
|
|
72
|
+
}
|
|
73
|
+
schema() {
|
|
74
|
+
if (this.cachedSchema)
|
|
75
|
+
return this.cachedSchema;
|
|
76
|
+
const text = readTextIfExists(graphPath(this.root, GRAPH_PATHS.schema));
|
|
77
|
+
this.cachedSchema = text === null ? this.defaultSchema : parseSchemaYaml(text);
|
|
78
|
+
return this.cachedSchema;
|
|
79
|
+
}
|
|
80
|
+
// -- entities -------------------------------------------------------------
|
|
81
|
+
readEntity(ref) {
|
|
82
|
+
if (!isEntityRef(ref))
|
|
83
|
+
return null;
|
|
84
|
+
const cached = this.entities.get(ref);
|
|
85
|
+
if (cached !== undefined)
|
|
86
|
+
return cached;
|
|
87
|
+
const file = entityFile(this.root, ref);
|
|
88
|
+
const text = readTextIfExists(file);
|
|
89
|
+
const entity = text === null ? null : parseEntityFile(text, file);
|
|
90
|
+
if (entity && entity.id !== ref)
|
|
91
|
+
throw new GraphFileError(file, `holds ${entity.id}, expected ${ref}`);
|
|
92
|
+
this.entities.set(ref, entity);
|
|
93
|
+
return entity;
|
|
94
|
+
}
|
|
95
|
+
upsertEntity(input) {
|
|
96
|
+
const refusals = validateEntityUpsert(this.schema(), input);
|
|
97
|
+
if (refusals.length > 0)
|
|
98
|
+
throw new RowRefusedError(`${String(input.type)}/${String(input.slug)}`, refusals);
|
|
99
|
+
const ref = `${input.type}/${input.slug}`;
|
|
100
|
+
const existing = this.readEntity(ref);
|
|
101
|
+
const name = input.canonical_name.trim();
|
|
102
|
+
const keys = (field) => sortedUnique([...(existing?.keys[field] ?? []), ...(input.keys?.[field] ?? [])]);
|
|
103
|
+
// The first name an entity was given stays its canonical name; later names become aliases.
|
|
104
|
+
const canonical = existing?.canonical_name ?? name;
|
|
105
|
+
const entity = {
|
|
106
|
+
id: ref,
|
|
107
|
+
type: input.type,
|
|
108
|
+
canonical_name: canonical,
|
|
109
|
+
slug: input.slug,
|
|
110
|
+
// A wiki link or a file path left over from an old entity file is not a name anybody goes by.
|
|
111
|
+
aliases: sortedUnique([...(existing?.aliases ?? []), ...(input.aliases ?? []), name]).filter((alias) => alias !== canonical && !isLinkRemnant(alias)),
|
|
112
|
+
keys: {
|
|
113
|
+
email: keys("email").map((value) => value.toLowerCase()).filter((value, index, all) => all.indexOf(value) === index).sort(compareText),
|
|
114
|
+
slack_id: keys("slack_id"),
|
|
115
|
+
external_ids: keys("external_ids"),
|
|
116
|
+
web_domains: keys("web_domains").map((value) => value.toLowerCase()).filter((value, index, all) => all.indexOf(value) === index).sort(compareText),
|
|
117
|
+
},
|
|
118
|
+
first_seen: existing && existing.first_seen < input.seen ? existing.first_seen : input.seen,
|
|
119
|
+
last_updated: existing && existing.last_updated > input.seen ? existing.last_updated : input.seen,
|
|
120
|
+
legacy_ids: sortedUnique([...(existing?.legacy_ids ?? []), ...(input.legacy_ids ?? [])]),
|
|
121
|
+
};
|
|
122
|
+
const changed = atomicWriteFile(entityFile(this.root, ref), renderEntityFile(entity));
|
|
123
|
+
this.entities.set(ref, entity);
|
|
124
|
+
return { entity, created: existing === null, changed };
|
|
125
|
+
}
|
|
126
|
+
listEntities(type) {
|
|
127
|
+
const types = type ? [type] : [...GRAPH_ENTITY_TYPES].sort(compareText);
|
|
128
|
+
const refs = [];
|
|
129
|
+
for (const entry of types) {
|
|
130
|
+
for (const name of listDir(path.join(graphPath(this.root, GRAPH_PATHS.entities), entry))) {
|
|
131
|
+
if (!name.endsWith(".md") || name.startsWith("."))
|
|
132
|
+
continue;
|
|
133
|
+
const ref = `${entry}/${name.slice(0, -3)}`;
|
|
134
|
+
if (isEntityRef(ref))
|
|
135
|
+
refs.push(ref);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return refs;
|
|
139
|
+
}
|
|
140
|
+
removeEntity(ref) {
|
|
141
|
+
if (!isEntityRef(ref))
|
|
142
|
+
return false;
|
|
143
|
+
const file = entityFile(this.root, ref);
|
|
144
|
+
const existed = fs.existsSync(file);
|
|
145
|
+
fs.rmSync(file, { force: true });
|
|
146
|
+
this.entities.set(ref, null);
|
|
147
|
+
return existed;
|
|
148
|
+
}
|
|
149
|
+
// -- rows -----------------------------------------------------------------
|
|
150
|
+
listAudiences() {
|
|
151
|
+
const onDisk = listDir(graphPath(this.root, GRAPH_PATHS.facts))
|
|
152
|
+
.filter((name) => name.startsWith("@") && isAudienceKey(name.slice(1)))
|
|
153
|
+
.map((name) => name.slice(1));
|
|
154
|
+
if (this.heldFiles.size === 0)
|
|
155
|
+
return onDisk;
|
|
156
|
+
const all = new Set(onDisk);
|
|
157
|
+
for (const held of this.heldFiles.values())
|
|
158
|
+
all.add(held.audience);
|
|
159
|
+
return [...all].sort(compareText);
|
|
160
|
+
}
|
|
161
|
+
/** Rows of one facts file. `strict` refuses a file with unreadable row lines, so a rewrite can never drop them. */
|
|
162
|
+
load(ref, audience, strict) {
|
|
163
|
+
const file = factsFile(this.root, audience, ref);
|
|
164
|
+
const cached = this.facts.get(file);
|
|
165
|
+
if (cached)
|
|
166
|
+
return cached;
|
|
167
|
+
const rows = new Map();
|
|
168
|
+
const text = readTextIfExists(file);
|
|
169
|
+
if (text !== null) {
|
|
170
|
+
const parsed = parseFactsFile(text);
|
|
171
|
+
if (parsed.errors.length > 0) {
|
|
172
|
+
if (strict) {
|
|
173
|
+
const first = parsed.errors[0];
|
|
174
|
+
throw new GraphFileError(file, `${first.refusal.code}: ${first.refusal.message}`, first.line);
|
|
175
|
+
}
|
|
176
|
+
// A lenient read is not cached: a later write must see the errors.
|
|
177
|
+
for (const entry of parsed.rows)
|
|
178
|
+
rows.set(entry.id, entry.row);
|
|
179
|
+
return rows;
|
|
180
|
+
}
|
|
181
|
+
for (const entry of parsed.rows)
|
|
182
|
+
rows.set(entry.id, entry.row);
|
|
183
|
+
}
|
|
184
|
+
this.facts.set(file, rows);
|
|
185
|
+
return rows;
|
|
186
|
+
}
|
|
187
|
+
save(ref, audience, rows) {
|
|
188
|
+
const file = factsFile(this.root, audience, ref);
|
|
189
|
+
if (this.batching) {
|
|
190
|
+
// Held: the file is written once, by flushBatch.
|
|
191
|
+
this.facts.set(file, rows);
|
|
192
|
+
this.heldFiles.set(file, { ref, audience });
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
atomicWriteFile(file, renderFactsFile(ref, audience, [...rows.values()]));
|
|
196
|
+
this.facts.set(file, rows);
|
|
197
|
+
}
|
|
198
|
+
readRows(ref, options = {}) {
|
|
199
|
+
if (!isEntityRef(ref))
|
|
200
|
+
return [];
|
|
201
|
+
const audiences = [...new Set(options.audiences ?? this.listAudiences())].filter(isAudienceKey).sort(compareText);
|
|
202
|
+
const out = [];
|
|
203
|
+
for (const audience of audiences) {
|
|
204
|
+
const stored = [];
|
|
205
|
+
for (const [id, row] of this.load(ref, audience, false)) {
|
|
206
|
+
if (options.kinds && !options.kinds.includes(row.kind))
|
|
207
|
+
continue;
|
|
208
|
+
if (row.kind === "T") {
|
|
209
|
+
if (options.asOf !== undefined && row.date > options.asOf)
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
if (options.asOf !== undefined && !rowCoversDay(row, options.asOf))
|
|
214
|
+
continue;
|
|
215
|
+
if (options.openOnly && row.valid.to !== undefined)
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
stored.push({ id, entity: ref, audience, row });
|
|
219
|
+
}
|
|
220
|
+
stored.sort((a, b) => compareRows(a.row, b.row) || compareText(a.id, b.id));
|
|
221
|
+
out.push(...stored);
|
|
222
|
+
}
|
|
223
|
+
return out;
|
|
224
|
+
}
|
|
225
|
+
writeRows(ref, audience, rows) {
|
|
226
|
+
const schema = this.schema();
|
|
227
|
+
const refusals = [];
|
|
228
|
+
for (const row of rows)
|
|
229
|
+
refusals.push(...validateRow(schema, ref, row, audience));
|
|
230
|
+
if (refusals.length > 0)
|
|
231
|
+
throw new RowRefusedError(String(ref), refusals);
|
|
232
|
+
const current = this.load(ref, audience, true);
|
|
233
|
+
// In a batch the rows are already validated and nothing is written before the flush, so no copy is taken.
|
|
234
|
+
const next = this.batching ? current : new Map(current);
|
|
235
|
+
const ids = [];
|
|
236
|
+
let added = 0;
|
|
237
|
+
let duplicate = 0;
|
|
238
|
+
let originsSet = 0;
|
|
239
|
+
for (const raw of rows) {
|
|
240
|
+
const row = normalizeRow(raw);
|
|
241
|
+
const id = rowId(row);
|
|
242
|
+
ids.push(id);
|
|
243
|
+
const present = next.get(id);
|
|
244
|
+
if (present) {
|
|
245
|
+
duplicate += 1;
|
|
246
|
+
// A timeline line that names who wrote it tells more than the same line without: keep the origin.
|
|
247
|
+
if (present.kind === "T" && row.kind === "T" && present.by === undefined && row.by !== undefined) {
|
|
248
|
+
next.set(id, row);
|
|
249
|
+
originsSet += 1;
|
|
250
|
+
}
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
next.set(id, row);
|
|
254
|
+
added += 1;
|
|
255
|
+
}
|
|
256
|
+
if (added > 0 || originsSet > 0) {
|
|
257
|
+
this.save(ref, audience, next);
|
|
258
|
+
if (this.batching)
|
|
259
|
+
this.heldRows += added;
|
|
260
|
+
}
|
|
261
|
+
return { added, duplicate, ids };
|
|
262
|
+
}
|
|
263
|
+
closeRow(ref, audience, id, closure) {
|
|
264
|
+
if (!isEntityRef(ref) || !isAudienceKey(audience))
|
|
265
|
+
return false;
|
|
266
|
+
const current = this.load(ref, audience, true);
|
|
267
|
+
const row = current.get(id);
|
|
268
|
+
if (!row || row.kind === "T" || row.closed || row.valid.to !== undefined)
|
|
269
|
+
return false;
|
|
270
|
+
const refuse = (message) => {
|
|
271
|
+
throw new RowRefusedError(ref, [{ code: "bad_closure", message }]);
|
|
272
|
+
};
|
|
273
|
+
if (!isIsoDate(closure?.on))
|
|
274
|
+
refuse(`closed date is not YYYY-MM-DD: "${String(closure?.on)}"`);
|
|
275
|
+
if (!isSourceRef(closure.by))
|
|
276
|
+
refuse(`closed by is not a source ref: "${String(closure.by)}"`);
|
|
277
|
+
if (closure.on < row.valid.from)
|
|
278
|
+
refuse(`cannot close on ${closure.on}, before valid from ${row.valid.from}`);
|
|
279
|
+
const next = this.batching ? current : new Map(current);
|
|
280
|
+
next.set(id, { ...row, valid: { from: row.valid.from, to: closure.on }, closed: { on: closure.on, by: closure.by } });
|
|
281
|
+
this.save(ref, audience, next);
|
|
282
|
+
if (this.batching)
|
|
283
|
+
this.heldClosures += 1;
|
|
284
|
+
return true;
|
|
285
|
+
}
|
|
286
|
+
reopenRow(ref, audience, id) {
|
|
287
|
+
if (!isEntityRef(ref) || !isAudienceKey(audience))
|
|
288
|
+
return false;
|
|
289
|
+
const current = this.load(ref, audience, true);
|
|
290
|
+
const row = current.get(id);
|
|
291
|
+
if (!row || row.kind === "T" || !row.closed)
|
|
292
|
+
return false;
|
|
293
|
+
const { closed: _closed, ...open } = row;
|
|
294
|
+
const next = this.batching ? current : new Map(current);
|
|
295
|
+
next.set(id, { ...open, valid: { from: row.valid.from } });
|
|
296
|
+
this.save(ref, audience, next);
|
|
297
|
+
return true;
|
|
298
|
+
}
|
|
299
|
+
removeRows(ref, audience, ids) {
|
|
300
|
+
if (!isEntityRef(ref) || !isAudienceKey(audience))
|
|
301
|
+
return 0;
|
|
302
|
+
const current = this.load(ref, audience, true);
|
|
303
|
+
const next = this.batching ? current : new Map(current);
|
|
304
|
+
let removed = 0;
|
|
305
|
+
for (const id of ids)
|
|
306
|
+
if (next.delete(id))
|
|
307
|
+
removed += 1;
|
|
308
|
+
if (removed > 0)
|
|
309
|
+
this.save(ref, audience, next);
|
|
310
|
+
return removed;
|
|
311
|
+
}
|
|
312
|
+
listEntitiesWithRows(audiences) {
|
|
313
|
+
const refs = new Set();
|
|
314
|
+
for (const audience of audiences ?? this.listAudiences()) {
|
|
315
|
+
if (!isAudienceKey(audience))
|
|
316
|
+
continue;
|
|
317
|
+
const dir = audienceDir(this.root, audience);
|
|
318
|
+
for (const type of listDir(dir)) {
|
|
319
|
+
for (const name of listDir(path.join(dir, type))) {
|
|
320
|
+
if (!name.endsWith(".md") || name.startsWith("."))
|
|
321
|
+
continue;
|
|
322
|
+
const ref = `${type}/${name.slice(0, -3)}`;
|
|
323
|
+
if (isEntityRef(ref))
|
|
324
|
+
refs.add(ref);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
for (const held of this.heldFiles.values()) {
|
|
329
|
+
if (!audiences || audiences.includes(held.audience))
|
|
330
|
+
refs.add(held.ref);
|
|
331
|
+
}
|
|
332
|
+
return [...refs].sort(compareText);
|
|
333
|
+
}
|
|
334
|
+
// -- batch ----------------------------------------------------------------
|
|
335
|
+
beginBatch() {
|
|
336
|
+
this.batching = true;
|
|
337
|
+
}
|
|
338
|
+
batchRows() {
|
|
339
|
+
return this.heldRows;
|
|
340
|
+
}
|
|
341
|
+
flushBatch(options = {}) {
|
|
342
|
+
const started = Date.now();
|
|
343
|
+
const result = { files: 0, rows: this.heldRows, closures: this.heldClosures, logEntries: this.heldLog.length, ms: 0 };
|
|
344
|
+
// Facts files in a fixed order: audience, then entity.
|
|
345
|
+
const held = [...this.heldFiles.entries()].sort(([, a], [, b]) => compareText(a.audience, b.audience) || compareText(a.ref, b.ref));
|
|
346
|
+
for (const [file, { ref, audience }] of held) {
|
|
347
|
+
const rows = this.facts.get(file);
|
|
348
|
+
if (rows && atomicWriteFile(file, renderFactsFile(ref, audience, [...rows.values()]), { knownChanged: true }))
|
|
349
|
+
result.files += 1;
|
|
350
|
+
this.heldFiles.delete(file);
|
|
351
|
+
}
|
|
352
|
+
this.heldRows = 0;
|
|
353
|
+
this.heldClosures = 0;
|
|
354
|
+
if (this.heldDirty.size > 0) {
|
|
355
|
+
const refs = [...this.heldDirty].sort(compareText);
|
|
356
|
+
this.heldDirty.clear();
|
|
357
|
+
this.writeDirty(refs);
|
|
358
|
+
}
|
|
359
|
+
if (this.heldLog.length > 0) {
|
|
360
|
+
const file = graphPath(this.root, GRAPH_PATHS.log);
|
|
361
|
+
const prefix = fs.existsSync(file) ? "" : LOG_HEADER;
|
|
362
|
+
const text = this.heldLog.map((entry) => formatLogEntry(entry)).join("");
|
|
363
|
+
this.heldLog = [];
|
|
364
|
+
appendText(file, prefix + text);
|
|
365
|
+
}
|
|
366
|
+
if (options.end)
|
|
367
|
+
this.batching = false;
|
|
368
|
+
result.ms = Date.now() - started;
|
|
369
|
+
return result;
|
|
370
|
+
}
|
|
371
|
+
// -- log, ledger, cursor --------------------------------------------------
|
|
372
|
+
appendLog(entry) {
|
|
373
|
+
if (!isIsoDate(entry.date))
|
|
374
|
+
throw new Error(`ontology graph: log date is not YYYY-MM-DD: ${String(entry.date)}`);
|
|
375
|
+
if (typeof entry.subject !== "string" || entry.subject.trim() === "" || /[\r\n]/.test(entry.subject)) {
|
|
376
|
+
throw new Error("ontology graph: a log entry needs a one-line subject");
|
|
377
|
+
}
|
|
378
|
+
if (this.batching) {
|
|
379
|
+
this.heldLog.push(entry);
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
const file = graphPath(this.root, GRAPH_PATHS.log);
|
|
383
|
+
const prefix = fs.existsSync(file) ? "" : LOG_HEADER;
|
|
384
|
+
appendText(file, prefix + formatLogEntry(entry));
|
|
385
|
+
}
|
|
386
|
+
readLog(limit) {
|
|
387
|
+
const text = readTextIfExists(graphPath(this.root, GRAPH_PATHS.log));
|
|
388
|
+
const entries = text === null ? [] : parseLog(text);
|
|
389
|
+
if (this.heldLog.length > 0)
|
|
390
|
+
entries.push(...this.heldLog);
|
|
391
|
+
return limit === undefined ? entries : limit <= 0 ? [] : entries.slice(-limit);
|
|
392
|
+
}
|
|
393
|
+
appendLedger(entry) {
|
|
394
|
+
const ordered = {};
|
|
395
|
+
for (const key of LEDGER_KEYS)
|
|
396
|
+
if (entry[key] !== undefined)
|
|
397
|
+
ordered[key] = entry[key];
|
|
398
|
+
for (const key of ["rowsAdded", "rowsClosed", "entitiesCreated", "modelCalls", "cacheHits", "inputTokens", "outputTokens", "pending", "seconds"]) {
|
|
399
|
+
if (typeof entry[key] !== "number" || !Number.isFinite(entry[key]) || entry[key] < 0) {
|
|
400
|
+
throw new Error(`ontology graph: ledger ${key} must be a number, 0 or more`);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
if (typeof entry.run !== "string" || entry.run === "")
|
|
404
|
+
throw new Error("ontology graph: a ledger entry needs a run id");
|
|
405
|
+
appendJsonLine(graphPath(this.root, GRAPH_PATHS.ledger), ordered);
|
|
406
|
+
}
|
|
407
|
+
readLedger(run) {
|
|
408
|
+
const entries = readJsonLines(graphPath(this.root, GRAPH_PATHS.ledger));
|
|
409
|
+
return run === undefined ? entries : entries.filter((entry) => entry.run === run);
|
|
410
|
+
}
|
|
411
|
+
readCursor() {
|
|
412
|
+
const file = graphPath(this.root, GRAPH_PATHS.cursor);
|
|
413
|
+
const text = readTextIfExists(file);
|
|
414
|
+
if (text === null)
|
|
415
|
+
return { days: {}, legacyImported: false };
|
|
416
|
+
let raw;
|
|
417
|
+
try {
|
|
418
|
+
raw = JSON.parse(text);
|
|
419
|
+
}
|
|
420
|
+
catch {
|
|
421
|
+
throw new GraphFileError(file, "not valid JSON");
|
|
422
|
+
}
|
|
423
|
+
const value = (raw ?? {});
|
|
424
|
+
const days = {};
|
|
425
|
+
for (const kind of REPLAY_INPUT_KINDS) {
|
|
426
|
+
const day = value.days?.[kind];
|
|
427
|
+
if (day !== undefined) {
|
|
428
|
+
if (!isIsoDate(day))
|
|
429
|
+
throw new GraphFileError(file, `days.${kind} is not a date`);
|
|
430
|
+
days[kind] = day;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
return {
|
|
434
|
+
days,
|
|
435
|
+
legacyImported: value.legacyImported === true,
|
|
436
|
+
...(typeof value.run === "string" ? { run: value.run } : {}),
|
|
437
|
+
...(typeof value.updatedAt === "string" ? { updatedAt: value.updatedAt } : {}),
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
writeCursor(cursor) {
|
|
441
|
+
const days = {};
|
|
442
|
+
for (const kind of REPLAY_INPUT_KINDS) {
|
|
443
|
+
const day = cursor.days[kind];
|
|
444
|
+
if (day === undefined)
|
|
445
|
+
continue;
|
|
446
|
+
if (!isIsoDate(day))
|
|
447
|
+
throw new Error(`ontology graph: cursor day for ${kind} is not YYYY-MM-DD: ${String(day)}`);
|
|
448
|
+
days[kind] = day;
|
|
449
|
+
}
|
|
450
|
+
const ordered = {
|
|
451
|
+
days,
|
|
452
|
+
legacyImported: cursor.legacyImported === true,
|
|
453
|
+
...(cursor.run !== undefined ? { run: cursor.run } : {}),
|
|
454
|
+
...(cursor.updatedAt !== undefined ? { updatedAt: cursor.updatedAt } : {}),
|
|
455
|
+
};
|
|
456
|
+
atomicWriteFile(graphPath(this.root, GRAPH_PATHS.cursor), `${JSON.stringify(ordered, null, 2)}\n`);
|
|
457
|
+
}
|
|
458
|
+
// -- dirty pages ----------------------------------------------------------
|
|
459
|
+
/** Read from disk every time: the page compiler may clear the list through another store instance. */
|
|
460
|
+
loadDirty() {
|
|
461
|
+
const text = readTextIfExists(graphPath(this.root, GRAPH_PATHS.dirty)) ?? "";
|
|
462
|
+
this.dirty = new Set(text.split("\n").filter(isEntityRef));
|
|
463
|
+
return this.dirty;
|
|
464
|
+
}
|
|
465
|
+
markDirty(refs) {
|
|
466
|
+
if (this.batching) {
|
|
467
|
+
for (const ref of refs)
|
|
468
|
+
if (parseEntityRef(ref) !== null)
|
|
469
|
+
this.heldDirty.add(ref);
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
this.writeDirty(refs);
|
|
473
|
+
}
|
|
474
|
+
writeDirty(refs) {
|
|
475
|
+
const dirty = this.loadDirty();
|
|
476
|
+
const fresh = sortedUnique(refs.filter((ref) => parseEntityRef(ref) !== null && !dirty.has(ref)));
|
|
477
|
+
if (fresh.length === 0)
|
|
478
|
+
return;
|
|
479
|
+
for (const ref of fresh)
|
|
480
|
+
dirty.add(ref);
|
|
481
|
+
appendText(graphPath(this.root, GRAPH_PATHS.dirty), `${fresh.join("\n")}\n`);
|
|
482
|
+
}
|
|
483
|
+
readDirty() {
|
|
484
|
+
const dirty = this.loadDirty();
|
|
485
|
+
if (this.heldDirty.size === 0)
|
|
486
|
+
return [...dirty].sort(compareText);
|
|
487
|
+
return [...new Set([...dirty, ...this.heldDirty])].sort(compareText);
|
|
488
|
+
}
|
|
489
|
+
clearDirty(refs) {
|
|
490
|
+
if (refs === undefined)
|
|
491
|
+
this.heldDirty.clear();
|
|
492
|
+
else
|
|
493
|
+
for (const ref of refs)
|
|
494
|
+
this.heldDirty.delete(ref);
|
|
495
|
+
const dirty = this.loadDirty();
|
|
496
|
+
if (refs === undefined)
|
|
497
|
+
dirty.clear();
|
|
498
|
+
else
|
|
499
|
+
for (const ref of refs)
|
|
500
|
+
dirty.delete(ref);
|
|
501
|
+
const left = [...dirty].sort(compareText);
|
|
502
|
+
atomicWriteFile(graphPath(this.root, GRAPH_PATHS.dirty), left.length > 0 ? `${left.join("\n")}\n` : "");
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
/** Opens the graph folder at `graphRoot`. Nothing is created until `init()` or the first write. */
|
|
506
|
+
export function openGraphStore(graphRoot, options = {}) {
|
|
507
|
+
if (typeof graphRoot !== "string" || graphRoot.trim() === "")
|
|
508
|
+
throw new Error("ontology graph: a graph root is required");
|
|
509
|
+
// The old entity folder is the cloud-authoritative baseline. The graph never lives in it.
|
|
510
|
+
const segments = path.resolve(graphRoot).split(path.sep);
|
|
511
|
+
for (let index = 0; index + 1 < segments.length; index += 1) {
|
|
512
|
+
if (segments[index] === "ontology" && segments[index + 1] === "entities") {
|
|
513
|
+
throw new Error("ontology graph: the graph root may not be inside ontology/entities");
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
if (options.company !== undefined)
|
|
517
|
+
assertGraphCompany(graphRoot, options.company);
|
|
518
|
+
return new FileGraphStore(graphRoot, options);
|
|
519
|
+
}
|
|
520
|
+
//# sourceMappingURL=graph-store.js.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WP-A: the store. Markdown files are canonical; see ../CONTRACT.md.
|
|
3
|
+
*
|
|
4
|
+
* - `openGraphStore(root)`: the file-backed GraphStore (graph-store.ts).
|
|
5
|
+
* - `applyIngest(store, batch)`: one input's rows, inverses, closures, dirty
|
|
6
|
+
* marks and log entry (ingest.ts).
|
|
7
|
+
* - File primitives other packages use for the files they own (fs.ts).
|
|
8
|
+
* - Paths and the graph root resolver (paths.ts).
|
|
9
|
+
*/
|
|
10
|
+
export { openGraphStore } from "./graph-store.js";
|
|
11
|
+
export type { OpenGraphStoreOptions } from "./graph-store.js";
|
|
12
|
+
export { appendUnresolved, applyIngest, ledgerTotals, readUnresolved } from "./ingest.js";
|
|
13
|
+
export { GraphFileError, appendJsonLine, appendText, atomicWriteFile, listDir, readJsonLines, readTextIfExists } from "./fs.js";
|
|
14
|
+
export { LOG_HEADER, formatLogEntry, parseEntityFile, parseFactsFile, parseLog, renderEntityFile, renderFactsFile, sortRows, } from "./files.js";
|
|
15
|
+
export type { ParsedFactsFile } from "./files.js";
|
|
16
|
+
export { GRAPH_PATHS, GRAPH_ROOT_ENV, GRAPH_ROOT_RELATIVE, GraphCompanyMismatchError, GraphCompanyStampError, GraphCompanyUnclaimedError, assertGraphCompany, assertGraphWritableFor, audienceDir, cacheFile, claimGraphRootFor, defaultGraphRoot, entityFile, factsFile, graphPath, graphRootFor, heldAudienceKeys, pageFile, readGraphCompany, resolveGraphRoot, stampGraphCompany, } from "./paths.js";
|
|
17
|
+
export type { ResolveGraphRootInput } from "./paths.js";
|
|
18
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WP-A: the store. Markdown files are canonical; see ../CONTRACT.md.
|
|
3
|
+
*
|
|
4
|
+
* - `openGraphStore(root)`: the file-backed GraphStore (graph-store.ts).
|
|
5
|
+
* - `applyIngest(store, batch)`: one input's rows, inverses, closures, dirty
|
|
6
|
+
* marks and log entry (ingest.ts).
|
|
7
|
+
* - File primitives other packages use for the files they own (fs.ts).
|
|
8
|
+
* - Paths and the graph root resolver (paths.ts).
|
|
9
|
+
*/
|
|
10
|
+
export { openGraphStore } from "./graph-store.js";
|
|
11
|
+
export { appendUnresolved, applyIngest, ledgerTotals, readUnresolved } from "./ingest.js";
|
|
12
|
+
export { GraphFileError, appendJsonLine, appendText, atomicWriteFile, listDir, readJsonLines, readTextIfExists } from "./fs.js";
|
|
13
|
+
export { LOG_HEADER, formatLogEntry, parseEntityFile, parseFactsFile, parseLog, renderEntityFile, renderFactsFile, sortRows, } from "./files.js";
|
|
14
|
+
export { GRAPH_PATHS, GRAPH_ROOT_ENV, GRAPH_ROOT_RELATIVE, GraphCompanyMismatchError, GraphCompanyStampError, GraphCompanyUnclaimedError, assertGraphCompany, assertGraphWritableFor, audienceDir, cacheFile, claimGraphRootFor, defaultGraphRoot, entityFile, factsFile, graphPath, graphRootFor, heldAudienceKeys, pageFile, readGraphCompany, resolveGraphRoot, stampGraphCompany, } from "./paths.js";
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* applyIngest: one input's batch becomes rows on every entity it touches
|
|
3
|
+
* (design 2.9). Extractors hand over forward rows only; this file writes the
|
|
4
|
+
* inverse of every edge on the edge's target, closes superseded single-valued
|
|
5
|
+
* rows and their inverses, marks touched pages dirty, records unresolved
|
|
6
|
+
* mentions and appends one log entry.
|
|
7
|
+
*
|
|
8
|
+
* The whole batch is validated before the first write. Rows are placed in
|
|
9
|
+
* memory first and flushed once per facts file, so an entity file is
|
|
10
|
+
* rewritten at most twice per ingest.
|
|
11
|
+
*/
|
|
12
|
+
import { type GraphStore, type IngestBatch, type IngestResult, type LedgerEntry, type LedgerTotals, type UnresolvedMention } from "../types.js";
|
|
13
|
+
/**
|
|
14
|
+
* Appends to `.state/unresolved.jsonl`. A mention already recorded (same
|
|
15
|
+
* text, reason, source, date and audience) is not written again, so a replay
|
|
16
|
+
* adds nothing. Returns how many lines were written.
|
|
17
|
+
*/
|
|
18
|
+
export declare function appendUnresolved(store: GraphStore, mentions: readonly UnresolvedMention[]): number;
|
|
19
|
+
export declare function readUnresolved(store: GraphStore): UnresolvedMention[];
|
|
20
|
+
/** Sums ledger entries (for the end-of-run report). */
|
|
21
|
+
export declare function ledgerTotals(entries: readonly LedgerEntry[]): LedgerTotals;
|
|
22
|
+
/**
|
|
23
|
+
* Applies one input's batch: upserts its entities, writes its rows, writes the
|
|
24
|
+
* inverse of every edge on the edge's target, closes superseded single-valued
|
|
25
|
+
* rows (and their inverses), marks touched pages dirty, records unresolved
|
|
26
|
+
* mentions and appends one log entry. Applying the same batch twice adds 0
|
|
27
|
+
* rows and closes 0 rows, and the second run appends no log entry. Throws
|
|
28
|
+
* RowRefusedError before writing anything if any row is invalid.
|
|
29
|
+
*
|
|
30
|
+
* Authority (design 2.7): a row of lower authority never closes an open row
|
|
31
|
+
* of higher authority. It is written closed on its own day and reported in
|
|
32
|
+
* `contradictions`, so a single-valued key holds one open row at most
|
|
33
|
+
* (correction C8). A row that repeats the value an open row of the same or
|
|
34
|
+
* higher authority already states is counted as a duplicate and not written.
|
|
35
|
+
*/
|
|
36
|
+
export declare function applyIngest(store: GraphStore, batch: IngestBatch): IngestResult;
|
|
37
|
+
//# sourceMappingURL=ingest.d.ts.map
|