@indigoai-us/hq-cli 5.345.247 → 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 +54 -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 +1113 -0
- package/dist/command-catalog.generated.js +1370 -0
- package/dist/command-registration-plan.d.ts +6 -0
- package/dist/command-registration-plan.js +1 -0
- 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/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/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/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/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/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,958 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WP-D: the model pass over a built graph (correction C8). Five tasks, in order:
|
|
3
|
+
*
|
|
4
|
+
* digest meetings and Slack threads that no signal cites, newest first
|
|
5
|
+
* profile role and employer of active people, stage and contacts of customers, from their own timeline
|
|
6
|
+
* retype `related_to` edges that still carry an old typed label
|
|
7
|
+
* supersede which decision replaces which
|
|
8
|
+
* summaries page summaries for entities with 20 or more timeline rows
|
|
9
|
+
*
|
|
10
|
+
* Each task builds items from the graph and the vault, sends the ones that are
|
|
11
|
+
* not cached, checks every answer, and (with `apply`) writes rows through
|
|
12
|
+
* `applyIngest`. Every row it writes is `by agent:<model>`, cites the source of
|
|
13
|
+
* the text it was read from, and exists only if the model's quote is in that
|
|
14
|
+
* text. Replies are cached by content, never by graph id, so running the pass
|
|
15
|
+
* again on a graph rebuilt from the same sources costs nothing.
|
|
16
|
+
*/
|
|
17
|
+
import * as fs from "node:fs";
|
|
18
|
+
import * as path from "node:path";
|
|
19
|
+
import { effectiveFacts, isOpen, isSingleValued, originTier, parseEntityRef, rowId, slugify, validateRow } from "../schema/index.js";
|
|
20
|
+
import { appendJsonLine, applyIngest, openGraphStore } from "../store/index.js";
|
|
21
|
+
import { createResolver } from "../resolve/index.js";
|
|
22
|
+
import { sha256Hex } from "./files.js";
|
|
23
|
+
import { allowedVerbs, summarizePages } from "./tasks.js";
|
|
24
|
+
import { clipText, compactSource, digestSpec, locateQuote, profileSpec, retypeSpec, squash, supersessionSpec, } from "./pass-tasks.js";
|
|
25
|
+
export const PASS_TASKS = ["digest", "profile", "retype", "supersede", "summaries"];
|
|
26
|
+
/**
|
|
27
|
+
* Tasks where the cheapest model was wrong on more than about 1 written row in 10 when its
|
|
28
|
+
* output was graded against the sources (indigo, 2026-11-22), and the model that passed.
|
|
29
|
+
* A caller that wants the measured choice builds a second step with this model and passes it in `taskSteps`.
|
|
30
|
+
*/
|
|
31
|
+
export const PASS_TASK_MODELS = { retype: "sonnet", supersede: "sonnet" };
|
|
32
|
+
/** One line per model-written row: what it rests on. Written beside the graph's other state files. */
|
|
33
|
+
export const MODEL_ROWS_FILE = ".state/model-rows.jsonl";
|
|
34
|
+
const inScope = (snapshot, ref) => snapshot.scope === undefined || snapshot.scope.has(ref);
|
|
35
|
+
/** A map that reads an entry from the store the first time it is asked for. */
|
|
36
|
+
class OnDemand extends Map {
|
|
37
|
+
load;
|
|
38
|
+
constructor(load) {
|
|
39
|
+
super();
|
|
40
|
+
this.load = load;
|
|
41
|
+
}
|
|
42
|
+
get(ref) {
|
|
43
|
+
if (!super.has(ref)) {
|
|
44
|
+
const value = this.load(ref);
|
|
45
|
+
if (value === undefined)
|
|
46
|
+
return undefined;
|
|
47
|
+
super.set(ref, value);
|
|
48
|
+
}
|
|
49
|
+
return super.get(ref);
|
|
50
|
+
}
|
|
51
|
+
has(ref) {
|
|
52
|
+
return this.get(ref) !== undefined;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function today() {
|
|
56
|
+
return new Date().toISOString().slice(0, 10);
|
|
57
|
+
}
|
|
58
|
+
function addDays(day, count) {
|
|
59
|
+
const date = new Date(`${day}T00:00:00Z`);
|
|
60
|
+
date.setUTCDate(date.getUTCDate() + count);
|
|
61
|
+
return date.toISOString().slice(0, 10);
|
|
62
|
+
}
|
|
63
|
+
function typeOf(ref) {
|
|
64
|
+
const parsed = parseEntityRef(ref);
|
|
65
|
+
if (!parsed)
|
|
66
|
+
throw new Error(`ontology graph: not an entity ref: ${String(ref)}`);
|
|
67
|
+
return parsed.type;
|
|
68
|
+
}
|
|
69
|
+
function byCode(a, b) {
|
|
70
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The entities a scoped pass looks at: the ones named, and, because a decision
|
|
74
|
+
* is judged against the other decisions about the same thing, every decision
|
|
75
|
+
* discussed on a named entity or on the topics of a named decision.
|
|
76
|
+
*/
|
|
77
|
+
function expandScope(store, only, audiences) {
|
|
78
|
+
const scope = new Set(only);
|
|
79
|
+
const edges = (ref, verb) => store
|
|
80
|
+
.readRows(ref, { kinds: ["E"], openOnly: true, ...(audiences ? { audiences } : {}) })
|
|
81
|
+
.flatMap((stored) => (stored.row.kind === "E" && stored.row.verb === verb ? [stored.row.target] : []));
|
|
82
|
+
const decisionsOn = (topic) => {
|
|
83
|
+
for (const target of edges(topic, "discussed_in"))
|
|
84
|
+
if (typeOf(target) === "decision")
|
|
85
|
+
scope.add(target);
|
|
86
|
+
};
|
|
87
|
+
for (const ref of only) {
|
|
88
|
+
if (typeOf(ref) !== "decision") {
|
|
89
|
+
decisionsOn(ref);
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
for (const topic of edges(ref, "about")) {
|
|
93
|
+
const type = typeOf(topic);
|
|
94
|
+
if (type !== "person" && type !== "meeting" && type !== "decision")
|
|
95
|
+
decisionsOn(topic);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return scope;
|
|
99
|
+
}
|
|
100
|
+
function loadSnapshot(store, audiences, only) {
|
|
101
|
+
if (only) {
|
|
102
|
+
// A daily update touches a few dozen entities of several thousand: read those, and the rest on demand.
|
|
103
|
+
const entities = new OnDemand((ref) => store.readEntity(ref) ?? undefined);
|
|
104
|
+
const rows = new OnDemand((ref) => (entities.has(ref) ? store.readRows(ref, audiences ? { audiences } : {}) : undefined));
|
|
105
|
+
const ids = new OnDemand((ref) => {
|
|
106
|
+
const held = rows.get(ref);
|
|
107
|
+
return held ? new Set(held.map((stored) => `${stored.audience}:${stored.id}`)) : undefined;
|
|
108
|
+
});
|
|
109
|
+
const scope = expandScope(store, only, audiences);
|
|
110
|
+
for (const ref of scope)
|
|
111
|
+
rows.get(ref);
|
|
112
|
+
return { entities, rows, ids, scope };
|
|
113
|
+
}
|
|
114
|
+
const snapshot = { entities: new Map(), rows: new Map(), ids: new Map() };
|
|
115
|
+
for (const ref of store.listEntities()) {
|
|
116
|
+
const entity = store.readEntity(ref);
|
|
117
|
+
if (!entity)
|
|
118
|
+
continue;
|
|
119
|
+
snapshot.entities.set(ref, entity);
|
|
120
|
+
const rows = store.readRows(ref, audiences ? { audiences } : {});
|
|
121
|
+
snapshot.rows.set(ref, rows);
|
|
122
|
+
snapshot.ids.set(ref, new Set(rows.map((stored) => `${stored.audience}:${stored.id}`)));
|
|
123
|
+
}
|
|
124
|
+
return snapshot;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* True when a record, a rule or a person already states where this person
|
|
128
|
+
* works (an open `works_at` row of that authority). A model's reading of a
|
|
129
|
+
* meeting does not add a second employer to them: a team member who joins a
|
|
130
|
+
* customer's call is not that customer's employee or contact.
|
|
131
|
+
*/
|
|
132
|
+
function employerOnRecord(snapshot, ref) {
|
|
133
|
+
return (snapshot.rows.get(ref) ?? []).some((stored) => stored.row.kind === "E" && stored.row.verb === "works_at" && isOpen(stored.row) && originTier(stored.row.by) <= 3);
|
|
134
|
+
}
|
|
135
|
+
const EMPLOYER_ON_RECORD = "employer left out: a record already states where the person works";
|
|
136
|
+
/** The entities a task looks for work on, with their identity: all of them, or the scope. */
|
|
137
|
+
function scoped(snapshot) {
|
|
138
|
+
if (snapshot.scope === undefined)
|
|
139
|
+
return snapshot.entities;
|
|
140
|
+
const out = [];
|
|
141
|
+
for (const ref of [...snapshot.scope].sort(byCode)) {
|
|
142
|
+
const entity = snapshot.entities.get(ref);
|
|
143
|
+
if (entity)
|
|
144
|
+
out.push([ref, entity]);
|
|
145
|
+
}
|
|
146
|
+
return out;
|
|
147
|
+
}
|
|
148
|
+
function emptyReport(task) {
|
|
149
|
+
return {
|
|
150
|
+
task, candidates: 0, skipped: {},
|
|
151
|
+
stats: { items: 0, cacheHits: 0, calls: 0, ok: 0, pending: 0, parked: 0, usage: { inputTokens: 0, outputTokens: 0 }, estimated: false, seconds: 0 },
|
|
152
|
+
rows: {}, rowsNew: 0, rowsAlreadyPresent: 0, rowsRefused: 0, dropped: 0, unresolved: 0, contradictions: 0, entitiesCreated: 0, perItem: [],
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
function bump(counter, key, by = 1) {
|
|
156
|
+
counter[key] = (counter[key] ?? 0) + by;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Leaves one open row per single-valued fact field on each of these entities.
|
|
160
|
+
* `applyIngest` closes an older row only inside one audience folder, and never
|
|
161
|
+
* lets an agent row close a rule or record row, so a model-written fact can end
|
|
162
|
+
* up open beside another value. Here the answer the contract gives (highest
|
|
163
|
+
* authority, then newest) stays open, and every other open row of the field
|
|
164
|
+
* that is not of higher authority than it is closed, citing the row that won.
|
|
165
|
+
* Rows by a human, a record or a rule are closed only by a row of at least
|
|
166
|
+
* their own authority. Returns the number of rows closed.
|
|
167
|
+
*/
|
|
168
|
+
export function settleSingleValued(store, schema, refs, day, audiences) {
|
|
169
|
+
let closed = 0;
|
|
170
|
+
const touched = [];
|
|
171
|
+
for (const ref of refs) {
|
|
172
|
+
const type = typeOf(ref);
|
|
173
|
+
const rows = store.readRows(ref, audiences ? { audiences } : {});
|
|
174
|
+
const open = rows.filter((stored) => stored.row.kind === "F" && isOpen(stored.row) && isSingleValued(schema, type, stored.row));
|
|
175
|
+
if (open.length < 2)
|
|
176
|
+
continue;
|
|
177
|
+
const winners = new Map(effectiveFacts(schema, type, rows).filter((stored) => isSingleValued(schema, type, stored.row)).map((stored) => [stored.row.field, stored]));
|
|
178
|
+
let changed = false;
|
|
179
|
+
for (const stored of open) {
|
|
180
|
+
const winner = winners.get(stored.row.field);
|
|
181
|
+
if (!winner || winner.id === stored.id || originTier(stored.row.by) < originTier(winner.row.by))
|
|
182
|
+
continue;
|
|
183
|
+
// The same value under a narrower audience than the row that stays: closing the company-wide copy would hide it from the company.
|
|
184
|
+
if (stored.row.value === winner.row.value && stored.audience === "company" && winner.audience !== "company")
|
|
185
|
+
continue;
|
|
186
|
+
// A row that lost on the day it began is closed on that day (an empty range), as the legacy import does.
|
|
187
|
+
const on = winner.row.valid.from > stored.row.valid.from ? winner.row.valid.from : day > stored.row.valid.from ? day : stored.row.valid.from;
|
|
188
|
+
if (store.closeRow(ref, stored.audience, stored.id, { on, by: winner.row.src })) {
|
|
189
|
+
closed += 1;
|
|
190
|
+
changed = true;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (changed)
|
|
194
|
+
touched.push(ref);
|
|
195
|
+
}
|
|
196
|
+
if (touched.length > 0)
|
|
197
|
+
store.markDirty(touched);
|
|
198
|
+
return closed;
|
|
199
|
+
}
|
|
200
|
+
/** Writes checked rows for one input and keeps the report's counts. */
|
|
201
|
+
class RowWriter {
|
|
202
|
+
store;
|
|
203
|
+
schema;
|
|
204
|
+
snapshot;
|
|
205
|
+
report;
|
|
206
|
+
by;
|
|
207
|
+
learned;
|
|
208
|
+
graphRoot;
|
|
209
|
+
citations = [];
|
|
210
|
+
factEntities = new Set();
|
|
211
|
+
constructor(store, schema, snapshot, report, by, learned, graphRoot) {
|
|
212
|
+
this.store = store;
|
|
213
|
+
this.schema = schema;
|
|
214
|
+
this.snapshot = snapshot;
|
|
215
|
+
this.report = report;
|
|
216
|
+
this.by = by;
|
|
217
|
+
this.learned = learned;
|
|
218
|
+
this.graphRoot = graphRoot;
|
|
219
|
+
}
|
|
220
|
+
fact(field, value, from, src, conf = 0.6) {
|
|
221
|
+
return { kind: "F", field, value: clipText(value, 400), valid: { from }, src, learned: this.learned, by: this.by, conf };
|
|
222
|
+
}
|
|
223
|
+
edge(verb, target, from, src, conf = 0.6) {
|
|
224
|
+
return { kind: "E", verb, target, valid: { from }, src, learned: this.learned, by: this.by, conf };
|
|
225
|
+
}
|
|
226
|
+
event(date, signalType, text, src) {
|
|
227
|
+
return { kind: "T", date, signalType, text: clipText(text, 400), src, by: this.by };
|
|
228
|
+
}
|
|
229
|
+
/** Applies one input's rows. Rows the schema refuses are left out and counted; the rest are written. */
|
|
230
|
+
write(input, date, entities, rows) {
|
|
231
|
+
const accepted = [];
|
|
232
|
+
const created = new Set(entities.map((upsert) => `${upsert.type}/${upsert.slug}`));
|
|
233
|
+
const seen = new Set();
|
|
234
|
+
for (const entry of rows) {
|
|
235
|
+
const refusals = validateRow(this.schema, entry.entity, entry.row, entry.audience);
|
|
236
|
+
const targetMissing = entry.row.kind === "E" && !this.snapshot.entities.has(entry.row.target) && !created.has(entry.row.target);
|
|
237
|
+
const subjectMissing = !this.snapshot.entities.has(entry.entity) && !created.has(entry.entity);
|
|
238
|
+
if (refusals.length > 0 || targetMissing || subjectMissing) {
|
|
239
|
+
this.report.rowsRefused += 1;
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
const id = rowId(entry.row);
|
|
243
|
+
const key = `${entry.entity}|${entry.audience}|${id}`;
|
|
244
|
+
if (seen.has(key))
|
|
245
|
+
continue;
|
|
246
|
+
seen.add(key);
|
|
247
|
+
accepted.push({ entity: entry.entity, audience: entry.audience, row: entry.row });
|
|
248
|
+
if (entry.row.kind === "F")
|
|
249
|
+
this.factEntities.add(entry.entity);
|
|
250
|
+
const label = entry.row.kind === "F" ? `F ${entry.row.field}` : entry.row.kind === "E" ? `E ${entry.row.verb}` : `T ${entry.row.signalType}`;
|
|
251
|
+
bump(this.report.rows, label);
|
|
252
|
+
const ids = this.snapshot.ids.get(entry.entity) ?? new Set();
|
|
253
|
+
if (ids.has(`${entry.audience}:${id}`))
|
|
254
|
+
this.report.rowsAlreadyPresent += 1;
|
|
255
|
+
else {
|
|
256
|
+
this.report.rowsNew += 1;
|
|
257
|
+
ids.add(`${entry.audience}:${id}`);
|
|
258
|
+
this.snapshot.ids.set(entry.entity, ids);
|
|
259
|
+
this.citations.push({
|
|
260
|
+
row: id, entity: entry.entity, audience: entry.audience, by: this.by, task: this.report.task, src: entry.row.src,
|
|
261
|
+
...(entry.lines ? { lines: entry.lines } : {}), quote: entry.quote,
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (accepted.length === 0)
|
|
266
|
+
return;
|
|
267
|
+
const result = applyIngest(this.store, { input, date, entities, rows: accepted });
|
|
268
|
+
this.report.contradictions += result.contradictions.length;
|
|
269
|
+
this.report.entitiesCreated += result.entitiesCreated;
|
|
270
|
+
}
|
|
271
|
+
flush() {
|
|
272
|
+
// One open row per single-valued field, whatever audience each row was filed under.
|
|
273
|
+
if (this.factEntities.size > 0) {
|
|
274
|
+
const closed = settleSingleValued(this.store, this.schema, this.factEntities, this.learned);
|
|
275
|
+
if (closed > 0)
|
|
276
|
+
bump(this.report.rows, "closed: another value of the field stays open", closed);
|
|
277
|
+
this.factEntities.clear();
|
|
278
|
+
}
|
|
279
|
+
const file = path.join(this.graphRoot, ...MODEL_ROWS_FILE.split("/"));
|
|
280
|
+
for (const citation of this.citations)
|
|
281
|
+
appendJsonLine(file, citation);
|
|
282
|
+
this.citations.length = 0;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
function record(report, results, stats) {
|
|
286
|
+
report.stats = stats;
|
|
287
|
+
for (const result of results) {
|
|
288
|
+
report.perItem.push({ key: result.itemKey, status: result.status, inputTokens: Math.round(result.usage.inputTokens), outputTokens: Math.round(result.usage.outputTokens) });
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
// ---------------------------------------------------------------------------
|
|
292
|
+
// digest
|
|
293
|
+
// ---------------------------------------------------------------------------
|
|
294
|
+
/** Source keys (`meetings/<id>`, `slack/<id>`) that at least one signal cites. */
|
|
295
|
+
export function signalledSources(companyRoot) {
|
|
296
|
+
const out = new Set();
|
|
297
|
+
const walk = (dir, depth) => {
|
|
298
|
+
let entries;
|
|
299
|
+
try {
|
|
300
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
301
|
+
}
|
|
302
|
+
catch {
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
for (const entry of entries) {
|
|
306
|
+
if (entry.name.startsWith("_") || entry.name.startsWith("."))
|
|
307
|
+
continue;
|
|
308
|
+
const full = path.join(dir, entry.name);
|
|
309
|
+
if (entry.isDirectory()) {
|
|
310
|
+
if (depth < 4)
|
|
311
|
+
walk(full, depth + 1);
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
if (!entry.name.endsWith(".md"))
|
|
315
|
+
continue;
|
|
316
|
+
let head;
|
|
317
|
+
try {
|
|
318
|
+
const fd = fs.openSync(full, "r");
|
|
319
|
+
const buffer = Buffer.alloc(3000);
|
|
320
|
+
const read = fs.readSync(fd, buffer, 0, 3000, 0);
|
|
321
|
+
fs.closeSync(fd);
|
|
322
|
+
head = buffer.toString("utf8", 0, read);
|
|
323
|
+
}
|
|
324
|
+
catch {
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
for (const match of head.matchAll(/sources\/(meetings|slack)\/([A-Za-z0-9._-]+?)\.md/g))
|
|
328
|
+
out.add(`${match[1]}/${match[2]}`);
|
|
329
|
+
for (const match of head.matchAll(/\b(meeting|slack):([A-Za-z0-9-]{20,})/g))
|
|
330
|
+
out.add(`${match[1] === "meeting" ? "meetings" : "slack"}/${match[2]}`);
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
walk(path.join(companyRoot, "signals"), 0);
|
|
334
|
+
return out;
|
|
335
|
+
}
|
|
336
|
+
const SOURCE_PATH = /^source:(sources\/(meetings|slack)\/([A-Za-z0-9._-]+)\.md)$/;
|
|
337
|
+
function digestCandidates(context, snapshot, day, windowDays, report) {
|
|
338
|
+
const signalled = signalledSources(context.companyRoot);
|
|
339
|
+
const found = new Map();
|
|
340
|
+
for (const [ref, rows] of snapshot.rows) {
|
|
341
|
+
if (!inScope(snapshot, ref))
|
|
342
|
+
continue;
|
|
343
|
+
const type = typeOf(ref);
|
|
344
|
+
for (const stored of rows) {
|
|
345
|
+
const match = SOURCE_PATH.exec(stored.row.src);
|
|
346
|
+
if (!match)
|
|
347
|
+
continue;
|
|
348
|
+
const kind = match[2] === "meetings" ? "meeting" : "slack";
|
|
349
|
+
if (kind === "meeting" && type !== "meeting")
|
|
350
|
+
continue;
|
|
351
|
+
let slot = found.get(match[1]);
|
|
352
|
+
if (!slot) {
|
|
353
|
+
slot = { relPath: match[1], key: `${match[2]}/${match[3]}`, kind, audience: stored.audience, date: stored.row.kind === "T" ? stored.row.date : stored.row.valid.from, title: "", touched: new Set() };
|
|
354
|
+
found.set(match[1], slot);
|
|
355
|
+
}
|
|
356
|
+
slot.touched.add(ref);
|
|
357
|
+
if (kind === "meeting") {
|
|
358
|
+
slot.meeting = ref;
|
|
359
|
+
if (stored.row.kind === "F" && stored.row.field === "title")
|
|
360
|
+
slot.title = stored.row.value;
|
|
361
|
+
if (stored.row.kind === "F" && stored.row.field === "date")
|
|
362
|
+
slot.date = stored.row.value;
|
|
363
|
+
}
|
|
364
|
+
else if (stored.row.kind === "T" && slot.title === "")
|
|
365
|
+
slot.title = clipText(stored.row.text, 100);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
report.candidates = found.size;
|
|
369
|
+
const since = addDays(day, -windowDays);
|
|
370
|
+
const out = [];
|
|
371
|
+
for (const slot of [...found.values()].sort((a, b) => byCode(b.date, a.date) || byCode(a.relPath, b.relPath))) {
|
|
372
|
+
if (signalled.has(slot.key)) {
|
|
373
|
+
bump(report.skipped, "has signals");
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
if (slot.date < since) {
|
|
377
|
+
bump(report.skipped, `older than ${windowDays} days`);
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
if (slot.meeting && (snapshot.rows.get(slot.meeting) ?? []).some((stored) => stored.row.src.startsWith("signal:"))) {
|
|
381
|
+
bump(report.skipped, "has signals");
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
let fileText;
|
|
385
|
+
try {
|
|
386
|
+
fileText = fs.readFileSync(path.join(context.companyRoot, ...slot.relPath.split("/")), "utf8");
|
|
387
|
+
}
|
|
388
|
+
catch {
|
|
389
|
+
bump(report.skipped, "file not on this machine");
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
const item = { src: `source:${slot.relPath}`, kind: slot.kind, date: slot.date, title: slot.title, fileText };
|
|
393
|
+
const size = compactSource(fileText).length;
|
|
394
|
+
if (size < (slot.kind === "meeting" ? 300 : 120)) {
|
|
395
|
+
bump(report.skipped, "too short to hold anything");
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
out.push({ item, audience: slot.audience, ...(slot.meeting ? { meeting: slot.meeting } : {}), touched: [...slot.touched].sort(byCode) });
|
|
399
|
+
}
|
|
400
|
+
return out;
|
|
401
|
+
}
|
|
402
|
+
const TOPIC_TYPES = {
|
|
403
|
+
project: ["project", "product"],
|
|
404
|
+
product: ["product", "project"],
|
|
405
|
+
company: ["company"],
|
|
406
|
+
team: ["team"],
|
|
407
|
+
tool: ["tool", "company", "product"],
|
|
408
|
+
concept: ["concept", "project"],
|
|
409
|
+
};
|
|
410
|
+
function nameResolver(store, schema, audiences, report) {
|
|
411
|
+
const resolver = createResolver(store, schema, audiences ? { audiences } : {});
|
|
412
|
+
return (name, types) => {
|
|
413
|
+
const text = name.trim();
|
|
414
|
+
if (text === "")
|
|
415
|
+
return null;
|
|
416
|
+
const resolution = resolver.resolve({ text, types, ...(audiences ? { audiences } : {}) });
|
|
417
|
+
if (resolution.status === "resolved" && resolution.entity && types.includes(typeOf(resolution.entity)))
|
|
418
|
+
return resolution.entity;
|
|
419
|
+
report.unresolved += 1;
|
|
420
|
+
return null;
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
function applyDigest(candidate, digest, snapshot, resolve, writer, report) {
|
|
424
|
+
const { item, audience } = candidate;
|
|
425
|
+
const src = item.src;
|
|
426
|
+
const date = item.date;
|
|
427
|
+
const lines = (quote) => {
|
|
428
|
+
const at = locateQuote(item.fileText, quote);
|
|
429
|
+
return at ? [at.from, at.to] : undefined;
|
|
430
|
+
};
|
|
431
|
+
const rows = [];
|
|
432
|
+
const push = (entity, row, quote) => {
|
|
433
|
+
const span = lines(quote);
|
|
434
|
+
rows.push({ entity, audience, row, quote, ...(span ? { lines: span } : {}) });
|
|
435
|
+
};
|
|
436
|
+
const entities = [];
|
|
437
|
+
// People in the room: a first name said in the meeting means the attendee with that first name, when there is one.
|
|
438
|
+
const attendees = new Map();
|
|
439
|
+
if (candidate.meeting) {
|
|
440
|
+
for (const stored of snapshot.rows.get(candidate.meeting) ?? []) {
|
|
441
|
+
if (stored.row.kind !== "E" || stored.row.verb !== "had_attendee")
|
|
442
|
+
continue;
|
|
443
|
+
const first = squash(snapshot.entities.get(stored.row.target)?.canonical_name ?? "").split(" ")[0];
|
|
444
|
+
if (first)
|
|
445
|
+
attendees.set(first, attendees.has(first) && attendees.get(first) !== stored.row.target ? null : stored.row.target);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
const person = (name) => {
|
|
449
|
+
const flat = squash(name);
|
|
450
|
+
if (flat !== "" && !flat.includes(" ")) {
|
|
451
|
+
const attendee = attendees.get(flat);
|
|
452
|
+
if (attendee)
|
|
453
|
+
return attendee;
|
|
454
|
+
}
|
|
455
|
+
return resolve(name, ["person"]);
|
|
456
|
+
};
|
|
457
|
+
const topics = new Map();
|
|
458
|
+
for (const topic of digest.topics) {
|
|
459
|
+
const ref = resolve(topic.name, TOPIC_TYPES[topic.type] ?? ["project"]);
|
|
460
|
+
if (!ref)
|
|
461
|
+
continue;
|
|
462
|
+
topics.set(squash(topic.name), ref);
|
|
463
|
+
if (candidate.meeting)
|
|
464
|
+
push(candidate.meeting, writer.edge("about", ref, date, src, 0.7), topic.quote);
|
|
465
|
+
}
|
|
466
|
+
const topicRefs = [...new Set(topics.values())];
|
|
467
|
+
if (digest.summary) {
|
|
468
|
+
if (candidate.meeting)
|
|
469
|
+
push(candidate.meeting, writer.event(date, "summary", digest.summary.text, src), digest.summary.quote);
|
|
470
|
+
else
|
|
471
|
+
for (const ref of topicRefs)
|
|
472
|
+
push(ref, writer.event(date, "update", digest.summary.text, src), digest.summary.quote);
|
|
473
|
+
}
|
|
474
|
+
for (const decision of digest.decisions) {
|
|
475
|
+
const slug = `${slugify(clipText(decision.text, 60)).slice(0, 64).replace(/-+$/, "")}-${sha256Hex(`${src}\n${decision.text}`).slice(0, 6)}`;
|
|
476
|
+
if (!/^[a-z0-9]/.test(slug))
|
|
477
|
+
continue;
|
|
478
|
+
const ref = `decision/${slug}`;
|
|
479
|
+
entities.push({ type: "decision", slug, canonical_name: clipText(decision.text, 90), seen: date });
|
|
480
|
+
push(ref, writer.fact("statement", decision.text, date, src), decision.quote);
|
|
481
|
+
push(ref, writer.fact("decided_on", date, date, src), decision.quote);
|
|
482
|
+
push(ref, writer.fact("status", "decided", date, src), decision.quote);
|
|
483
|
+
push(ref, writer.event(date, "decision", decision.text, src), decision.quote);
|
|
484
|
+
if (candidate.meeting)
|
|
485
|
+
push(ref, writer.edge("decided_in", candidate.meeting, date, src, 0.8), decision.quote);
|
|
486
|
+
const topic = topics.get(squash(decision.topic)) ?? (topicRefs.length === 1 ? topicRefs[0] : undefined);
|
|
487
|
+
if (topic) {
|
|
488
|
+
push(ref, writer.edge("about", topic, date, src), decision.quote);
|
|
489
|
+
push(topic, writer.event(date, "decision", decision.text, src), decision.quote);
|
|
490
|
+
}
|
|
491
|
+
const decider = decision.by === "" ? null : person(decision.by);
|
|
492
|
+
if (decider)
|
|
493
|
+
push(ref, writer.edge("decided_by", decider, date, src), decision.quote);
|
|
494
|
+
}
|
|
495
|
+
for (const action of digest.actions) {
|
|
496
|
+
const text = `${action.owner}: ${action.text}`;
|
|
497
|
+
if (candidate.meeting)
|
|
498
|
+
push(candidate.meeting, writer.event(date, "action_item", text, src), action.quote);
|
|
499
|
+
const owner = person(action.owner);
|
|
500
|
+
if (owner)
|
|
501
|
+
push(owner, writer.event(date, "action_item", text, src), action.quote);
|
|
502
|
+
}
|
|
503
|
+
for (const entry of digest.people) {
|
|
504
|
+
const ref = person(entry.name);
|
|
505
|
+
if (!ref)
|
|
506
|
+
continue;
|
|
507
|
+
if (entry.role !== "")
|
|
508
|
+
push(ref, writer.fact("role_title", entry.role, date, src), entry.quote);
|
|
509
|
+
if (entry.employer !== "" && employerOnRecord(snapshot, ref))
|
|
510
|
+
bump(report.skipped, EMPLOYER_ON_RECORD);
|
|
511
|
+
else if (entry.employer !== "") {
|
|
512
|
+
const company = resolve(entry.employer, ["company"]);
|
|
513
|
+
if (company)
|
|
514
|
+
push(ref, writer.edge("works_at", company, date, src), entry.quote);
|
|
515
|
+
else
|
|
516
|
+
push(ref, writer.fact("affiliation", entry.employer, date, src, 0.5), entry.quote);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
report.dropped += digest.dropped;
|
|
520
|
+
writer.write(src, date, entities, rows);
|
|
521
|
+
}
|
|
522
|
+
// ---------------------------------------------------------------------------
|
|
523
|
+
// profile
|
|
524
|
+
// ---------------------------------------------------------------------------
|
|
525
|
+
const ROLE_CUE = /\b(ceo|cto|coo|cfo|cmo|founder|co-?founder|head of|vp\b|vice president|director|manager|engineer|developer|designer|lead\b|owner|partner|president|consultant|advisor|analyst|principal|works (at|for)|joined|hired|from [A-Z]|\([A-Z][^)]{1,40}\))/i;
|
|
526
|
+
const STAGE_CUE = /\b(onboard|signed|contract|paid|invoice|trial|pilot|demo|renew|churn|cancel|prospect|proposal|pricing|customer|client|subscription|kickoff|kick-off)/i;
|
|
527
|
+
function profileCandidates(snapshot, day, report) {
|
|
528
|
+
const out = [];
|
|
529
|
+
const activeSince = addDays(day, -120);
|
|
530
|
+
for (const [ref, entity] of scoped(snapshot)) {
|
|
531
|
+
if (entity.type !== "person" && entity.type !== "company")
|
|
532
|
+
continue;
|
|
533
|
+
const rows = snapshot.rows.get(ref) ?? [];
|
|
534
|
+
const timeline = rows.filter((stored) => stored.row.kind === "T");
|
|
535
|
+
if (timeline.length === 0)
|
|
536
|
+
continue;
|
|
537
|
+
report.candidates += 1;
|
|
538
|
+
const newest = timeline.reduce((max, stored) => (stored.row.date > max ? stored.row.date : max), "");
|
|
539
|
+
if (timeline.length < (entity.type === "person" ? 5 : 3) || newest < activeSince) {
|
|
540
|
+
bump(report.skipped, "not active (few rows or none in 120 days)");
|
|
541
|
+
continue;
|
|
542
|
+
}
|
|
543
|
+
if (entity.type === "company") {
|
|
544
|
+
const relation = rows.filter((stored) => stored.row.kind === "F" && stored.row.field === "relation" && isOpen(stored.row)).map((stored) => stored.row.value);
|
|
545
|
+
if (relation.some((value) => value === "self" || value === "vendor")) {
|
|
546
|
+
bump(report.skipped, "own company or vendor");
|
|
547
|
+
continue;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
const name = squash(entity.canonical_name);
|
|
551
|
+
const first = name.split(" ")[0];
|
|
552
|
+
const cue = entity.type === "person" ? ROLE_CUE : STAGE_CUE;
|
|
553
|
+
const unique = new Map();
|
|
554
|
+
for (const stored of timeline) {
|
|
555
|
+
const flat = squash(stored.row.text);
|
|
556
|
+
if (entity.type === "person" && !(flat.includes(name) || (first.length >= 4 && flat.includes(first))))
|
|
557
|
+
continue;
|
|
558
|
+
const key = flat.slice(0, 80);
|
|
559
|
+
const known = unique.get(key);
|
|
560
|
+
if (!known || stored.row.text.length > known.row.text.length)
|
|
561
|
+
unique.set(key, stored);
|
|
562
|
+
}
|
|
563
|
+
const sorted = [...unique.values()].sort((a, b) => byCode(b.row.date, a.row.date) || byCode(a.id, b.id));
|
|
564
|
+
const withCue = sorted.filter((stored) => cue.test(stored.row.text));
|
|
565
|
+
if (withCue.length === 0) {
|
|
566
|
+
bump(report.skipped, "no line states a role, employer or stage");
|
|
567
|
+
continue;
|
|
568
|
+
}
|
|
569
|
+
const chosen = [...withCue.slice(0, 16), ...sorted.filter((stored) => !cue.test(stored.row.text)).slice(0, 6)].sort((a, b) => byCode(b.row.date, a.row.date) || byCode(a.id, b.id));
|
|
570
|
+
const labelled = new Map();
|
|
571
|
+
const lines = chosen.map((stored, i) => {
|
|
572
|
+
labelled.set(`t${i + 1}`, stored);
|
|
573
|
+
return { label: `t${i + 1}`, date: stored.row.date, text: clipText(stored.row.text, 320) };
|
|
574
|
+
});
|
|
575
|
+
out.push({ candidate: { item: { ref, name: entity.canonical_name, type: entity.type, lines }, rows: labelled }, weight: timeline.length });
|
|
576
|
+
}
|
|
577
|
+
return out.sort((a, b) => b.weight - a.weight || byCode(a.candidate.item.ref, b.candidate.item.ref)).map((entry) => entry.candidate);
|
|
578
|
+
}
|
|
579
|
+
function applyProfile(candidate, profile, snapshot, resolve, writer, report) {
|
|
580
|
+
const ref = candidate.item.ref;
|
|
581
|
+
report.dropped += profile.dropped;
|
|
582
|
+
const cited = (label) => candidate.rows.get(label);
|
|
583
|
+
// One ingest per cited source, so the log entry names the source the rows rest on.
|
|
584
|
+
const bySource = new Map();
|
|
585
|
+
const push = (stored, entity, row, quote) => {
|
|
586
|
+
const slot = bySource.get(stored.row.src) ?? { date: stored.row.date, rows: [] };
|
|
587
|
+
slot.rows.push({ entity, audience: stored.audience, row, quote });
|
|
588
|
+
bySource.set(stored.row.src, slot);
|
|
589
|
+
};
|
|
590
|
+
if (profile.role) {
|
|
591
|
+
const stored = cited(profile.role.cite);
|
|
592
|
+
if (stored)
|
|
593
|
+
push(stored, ref, writer.fact("role_title", profile.role.value, stored.row.date, stored.row.src), profile.role.quote);
|
|
594
|
+
}
|
|
595
|
+
if (profile.employer && employerOnRecord(snapshot, ref))
|
|
596
|
+
bump(report.skipped, EMPLOYER_ON_RECORD);
|
|
597
|
+
else if (profile.employer) {
|
|
598
|
+
const stored = cited(profile.employer.cite);
|
|
599
|
+
if (stored) {
|
|
600
|
+
const company = resolve(profile.employer.value, ["company"]);
|
|
601
|
+
if (company)
|
|
602
|
+
push(stored, ref, writer.edge("works_at", company, stored.row.date, stored.row.src), profile.employer.quote);
|
|
603
|
+
else
|
|
604
|
+
push(stored, ref, writer.fact("affiliation", profile.employer.value, stored.row.date, stored.row.src, 0.5), profile.employer.quote);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
if (profile.stage) {
|
|
608
|
+
const stored = cited(profile.stage.cite);
|
|
609
|
+
if (stored)
|
|
610
|
+
push(stored, ref, writer.fact("stage", profile.stage.value, stored.row.date, stored.row.src), profile.stage.quote);
|
|
611
|
+
}
|
|
612
|
+
for (const contact of profile.contacts) {
|
|
613
|
+
const stored = cited(contact.cite);
|
|
614
|
+
if (!stored)
|
|
615
|
+
continue;
|
|
616
|
+
const who = resolve(contact.value, ["person"]);
|
|
617
|
+
if (!who)
|
|
618
|
+
continue;
|
|
619
|
+
if (employerOnRecord(snapshot, who)) {
|
|
620
|
+
bump(report.skipped, EMPLOYER_ON_RECORD);
|
|
621
|
+
continue;
|
|
622
|
+
}
|
|
623
|
+
push(stored, who, writer.edge("works_at", ref, stored.row.date, stored.row.src), contact.quote);
|
|
624
|
+
const hasRole = (snapshot.rows.get(who) ?? []).some((row) => row.row.kind === "F" && row.row.field === "role_title" && isOpen(row.row));
|
|
625
|
+
if (contact.role !== "" && !hasRole)
|
|
626
|
+
push(stored, who, writer.fact("role_title", contact.role, stored.row.date, stored.row.src, 0.5), contact.quote);
|
|
627
|
+
}
|
|
628
|
+
for (const [src, slot] of bySource)
|
|
629
|
+
writer.write(src, slot.date, [], slot.rows);
|
|
630
|
+
}
|
|
631
|
+
function retypeCandidates(schema, snapshot, report) {
|
|
632
|
+
const typedLabels = new Map();
|
|
633
|
+
for (const entry of schema.verb_map)
|
|
634
|
+
if (entry.action === "verb" && entry.verb)
|
|
635
|
+
typedLabels.set(entry.label.toLowerCase(), entry.verb);
|
|
636
|
+
const seen = new Set();
|
|
637
|
+
const out = [];
|
|
638
|
+
for (const [ref, rows] of [...snapshot.rows].filter(([entry]) => inScope(snapshot, entry)).sort((a, b) => byCode(a[0], b[0]))) {
|
|
639
|
+
for (const stored of rows) {
|
|
640
|
+
const row = stored.row;
|
|
641
|
+
if (row.kind !== "E" || row.verb !== "related_to" || !isOpen(row) || row.was === undefined)
|
|
642
|
+
continue;
|
|
643
|
+
const hint = typedLabels.get(row.was.toLowerCase());
|
|
644
|
+
if (!hint)
|
|
645
|
+
continue;
|
|
646
|
+
const pair = `${[ref, row.target].sort().join("|")}|${row.was.toLowerCase()}`;
|
|
647
|
+
if (seen.has(pair))
|
|
648
|
+
continue;
|
|
649
|
+
seen.add(pair);
|
|
650
|
+
report.candidates += 1;
|
|
651
|
+
const a = snapshot.entities.get(ref);
|
|
652
|
+
const b = snapshot.entities.get(row.target);
|
|
653
|
+
if (!a || !b) {
|
|
654
|
+
bump(report.skipped, "target has no entity file");
|
|
655
|
+
continue;
|
|
656
|
+
}
|
|
657
|
+
const options = [
|
|
658
|
+
...allowedVerbs(schema, a.type, b.type).map((verb) => ({ subject: "a", verb })),
|
|
659
|
+
...allowedVerbs(schema, b.type, a.type).map((verb) => ({ subject: "b", verb })),
|
|
660
|
+
]
|
|
661
|
+
// An inverse pair says one thing twice: keep the reading with the hinted verb, else the one that starts from a.
|
|
662
|
+
.filter((option, _i, all) => !all.some((other) => other.subject !== option.subject && schema.link_types[other.verb]?.inverse === option.verb && (other.verb === hint || (option.verb !== hint && other.subject === "a"))))
|
|
663
|
+
.sort((x, y) => Number(y.verb === hint) - Number(x.verb === hint) || byCode(x.verb, y.verb) || byCode(x.subject, y.subject));
|
|
664
|
+
if (options.length === 0) {
|
|
665
|
+
bump(report.skipped, "the schema allows no typed verb between these types");
|
|
666
|
+
continue;
|
|
667
|
+
}
|
|
668
|
+
const evidence = [];
|
|
669
|
+
const taken = new Set();
|
|
670
|
+
const collect = (from, other) => {
|
|
671
|
+
const needle = squash(other.canonical_name);
|
|
672
|
+
if (needle.length < 4)
|
|
673
|
+
return;
|
|
674
|
+
for (const candidate of snapshot.rows.get(from) ?? []) {
|
|
675
|
+
if (candidate.row.kind !== "T" || taken.has(candidate.id) || !squash(candidate.row.text).includes(needle))
|
|
676
|
+
continue;
|
|
677
|
+
taken.add(candidate.id);
|
|
678
|
+
evidence.push(candidate);
|
|
679
|
+
}
|
|
680
|
+
};
|
|
681
|
+
collect(ref, b);
|
|
682
|
+
collect(row.target, a);
|
|
683
|
+
if (evidence.length === 0) {
|
|
684
|
+
bump(report.skipped, "no dated line names both");
|
|
685
|
+
continue;
|
|
686
|
+
}
|
|
687
|
+
const lines = evidence.sort((x, y) => byCode(y.row.date, x.row.date) || byCode(x.id, y.id)).slice(0, 6);
|
|
688
|
+
out.push({
|
|
689
|
+
item: {
|
|
690
|
+
a: { ref, name: a.canonical_name, type: a.type },
|
|
691
|
+
b: { ref: row.target, name: b.canonical_name, type: b.type },
|
|
692
|
+
rowId: stored.id,
|
|
693
|
+
was: row.was,
|
|
694
|
+
options,
|
|
695
|
+
evidence: lines.map((line) => ({ src: line.row.src, date: line.row.date, text: clipText(line.row.text, 300) })),
|
|
696
|
+
},
|
|
697
|
+
evidenceRows: lines,
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
return out;
|
|
702
|
+
}
|
|
703
|
+
function applyRetype(candidate, answer, store, snapshot, writer, day, report) {
|
|
704
|
+
if (answer.option < 0) {
|
|
705
|
+
bump(report.rows, "left as related_to");
|
|
706
|
+
return;
|
|
707
|
+
}
|
|
708
|
+
const option = candidate.item.options[answer.option];
|
|
709
|
+
const evidence = candidate.evidenceRows[answer.evidence];
|
|
710
|
+
if (!option || !evidence)
|
|
711
|
+
return;
|
|
712
|
+
const a = candidate.item.a.ref;
|
|
713
|
+
const b = candidate.item.b.ref;
|
|
714
|
+
const [subject, target] = option.subject === "a" ? [a, b] : [b, a];
|
|
715
|
+
const before = report.rowsNew + report.rowsAlreadyPresent;
|
|
716
|
+
writer.write(evidence.row.src, evidence.row.date, [], [
|
|
717
|
+
{ entity: subject, audience: evidence.audience, row: writer.edge(option.verb, target, evidence.row.date, evidence.row.src), quote: answer.quote },
|
|
718
|
+
]);
|
|
719
|
+
if (report.rowsNew + report.rowsAlreadyPresent === before)
|
|
720
|
+
return;
|
|
721
|
+
// The typed edge replaces the untyped one on both ends.
|
|
722
|
+
for (const [from, to] of [[a, b], [b, a]]) {
|
|
723
|
+
for (const stored of snapshot.rows.get(from) ?? []) {
|
|
724
|
+
if (stored.row.kind !== "E" || stored.row.verb !== "related_to" || stored.row.target !== to || !isOpen(stored.row) || stored.row.by !== "legacy")
|
|
725
|
+
continue;
|
|
726
|
+
if (store.closeRow(from, stored.audience, stored.id, { on: day, by: evidence.row.src }))
|
|
727
|
+
bump(report.rows, "closed related_to");
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
const SUPERSESSION_GROUP = 10;
|
|
732
|
+
const SUPERSESSION_GROUPS_PER_TOPIC = 3;
|
|
733
|
+
function supersessionCandidates(snapshot, report) {
|
|
734
|
+
const byTopic = new Map();
|
|
735
|
+
for (const [ref, entity] of scoped(snapshot)) {
|
|
736
|
+
if (entity.type !== "decision")
|
|
737
|
+
continue;
|
|
738
|
+
const rows = snapshot.rows.get(ref) ?? [];
|
|
739
|
+
const statement = rows.find((stored) => stored.row.kind === "F" && stored.row.field === "statement" && isOpen(stored.row));
|
|
740
|
+
if (!statement)
|
|
741
|
+
continue;
|
|
742
|
+
report.candidates += 1;
|
|
743
|
+
// A one-line aside ("sure", "skip it") cannot be shown to replace or be replaced by anything.
|
|
744
|
+
if (statement.row.value.trim().split(/\s+/).length < 5) {
|
|
745
|
+
bump(report.skipped, "statement under 5 words");
|
|
746
|
+
continue;
|
|
747
|
+
}
|
|
748
|
+
if (rows.some((stored) => stored.row.kind === "F" && stored.row.field === "status" && isOpen(stored.row) && stored.row.value === "superseded"))
|
|
749
|
+
continue;
|
|
750
|
+
const decidedOn = rows.find((stored) => stored.row.kind === "F" && stored.row.field === "decided_on" && isOpen(stored.row));
|
|
751
|
+
const date = decidedOn && decidedOn.row.kind === "F" && /^\d{4}-\d{2}-\d{2}$/.test(decidedOn.row.value) ? decidedOn.row.value : statement.row.valid.from;
|
|
752
|
+
for (const stored of rows) {
|
|
753
|
+
if (stored.row.kind !== "E" || stored.row.verb !== "about" || !isOpen(stored.row))
|
|
754
|
+
continue;
|
|
755
|
+
const type = typeOf(stored.row.target);
|
|
756
|
+
if (type === "person" || type === "meeting" || type === "decision")
|
|
757
|
+
continue;
|
|
758
|
+
const key = `${stored.row.target}|${statement.audience}`;
|
|
759
|
+
const slot = byTopic.get(key) ?? { topic: stored.row.target, audience: statement.audience, decisions: new Map() };
|
|
760
|
+
slot.decisions.set(ref, { date, statement });
|
|
761
|
+
byTopic.set(key, slot);
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
const out = [];
|
|
765
|
+
for (const slot of byTopic.values()) {
|
|
766
|
+
const sorted = [...slot.decisions.entries()].sort((a, b) => byCode(a[1].date, b[1].date) || byCode(a[0], b[0]));
|
|
767
|
+
if (sorted.length < 2)
|
|
768
|
+
continue;
|
|
769
|
+
const topic = snapshot.entities.get(slot.topic);
|
|
770
|
+
if (!topic)
|
|
771
|
+
continue;
|
|
772
|
+
// Newest groups first; a topic with hundreds of decisions gets its latest three groups.
|
|
773
|
+
const groups = [];
|
|
774
|
+
for (let end = sorted.length; end > 0 && groups.length < SUPERSESSION_GROUPS_PER_TOPIC; end -= SUPERSESSION_GROUP) {
|
|
775
|
+
const group = sorted.slice(Math.max(0, end - SUPERSESSION_GROUP), end);
|
|
776
|
+
if (group.length >= 2)
|
|
777
|
+
groups.push(group);
|
|
778
|
+
}
|
|
779
|
+
for (const group of groups) {
|
|
780
|
+
out.push({
|
|
781
|
+
candidate: {
|
|
782
|
+
item: {
|
|
783
|
+
topicRef: slot.topic,
|
|
784
|
+
topic: topic.canonical_name,
|
|
785
|
+
decisions: group.map(([ref, entry]) => ({ ref, date: entry.date, text: clipText(entry.statement.row.value, 320) })),
|
|
786
|
+
},
|
|
787
|
+
statements: group.map(([, entry]) => entry.statement),
|
|
788
|
+
},
|
|
789
|
+
newest: group[group.length - 1][1].date,
|
|
790
|
+
});
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
return out.sort((a, b) => byCode(b.newest, a.newest) || byCode(a.candidate.item.topicRef, b.candidate.item.topicRef)).map((entry) => entry.candidate);
|
|
794
|
+
}
|
|
795
|
+
function applySupersession(candidate, answer, writer, report) {
|
|
796
|
+
report.dropped += answer.dropped;
|
|
797
|
+
for (const pair of answer.pairs) {
|
|
798
|
+
if (pair.kind === "duplicate") {
|
|
799
|
+
bump(report.rows, "duplicate pair (reported, not merged)");
|
|
800
|
+
continue;
|
|
801
|
+
}
|
|
802
|
+
const newer = candidate.item.decisions[pair.newer];
|
|
803
|
+
const older = candidate.item.decisions[pair.older];
|
|
804
|
+
const statement = candidate.statements[pair.newer];
|
|
805
|
+
if (!newer || !older || !statement)
|
|
806
|
+
continue;
|
|
807
|
+
writer.write(statement.row.src, newer.date, [], [
|
|
808
|
+
// The edge is the record. A status fact by an agent could not close the rule-written `decided`, so none is written.
|
|
809
|
+
{ entity: newer.ref, audience: statement.audience, row: writer.edge("supersedes", older.ref, newer.date, statement.row.src), quote: pair.quote },
|
|
810
|
+
]);
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
// ---------------------------------------------------------------------------
|
|
814
|
+
// The pass
|
|
815
|
+
// ---------------------------------------------------------------------------
|
|
816
|
+
export async function runModelPass(context, options) {
|
|
817
|
+
const started = Date.now();
|
|
818
|
+
const day = options.day ?? today();
|
|
819
|
+
const applyRequested = options.apply !== false;
|
|
820
|
+
const tasks = options.tasks ?? PASS_TASKS;
|
|
821
|
+
// Nothing changed: there is nothing new to ask about.
|
|
822
|
+
if (options.only && options.only.size === 0)
|
|
823
|
+
return { day, model: options.step.model, applied: applyRequested, tasks: [], seconds: 0 };
|
|
824
|
+
const limit = options.limit ?? Number.POSITIVE_INFINITY;
|
|
825
|
+
const reports = [];
|
|
826
|
+
const noteAll = (task, result, item) => options.onResult?.(task, { key: result.itemKey, status: result.status, item, value: result.value ?? null });
|
|
827
|
+
const usable = (status) => status === "ok" || status === "cached";
|
|
828
|
+
for (const task of PASS_TASKS) {
|
|
829
|
+
if (!tasks.includes(task))
|
|
830
|
+
continue;
|
|
831
|
+
const runTask = async (store, apply, tell) => {
|
|
832
|
+
const schema = store.schema();
|
|
833
|
+
const note = tell ? noteAll : () => undefined;
|
|
834
|
+
// Each task reads the graph as the task before it left it.
|
|
835
|
+
const snapshot = loadSnapshot(store, options.audiences, options.only);
|
|
836
|
+
const report = emptyReport(task);
|
|
837
|
+
const step = options.taskSteps?.[task] ?? options.step;
|
|
838
|
+
const by = `agent:${step.model}`;
|
|
839
|
+
const writer = new RowWriter(store, schema, snapshot, report, by, day, context.graphRoot);
|
|
840
|
+
const resolve = nameResolver(store, schema, options.audiences, report);
|
|
841
|
+
if (task === "digest") {
|
|
842
|
+
const candidates = digestCandidates(context, snapshot, day, options.windowDays ?? 90, report).slice(0, limit);
|
|
843
|
+
const run = await step.runBatch(digestSpec(day), candidates.map((candidate) => candidate.item));
|
|
844
|
+
record(report, run.results, run.stats);
|
|
845
|
+
run.results.forEach((result, i) => {
|
|
846
|
+
note(task, result, { src: result.item.src, date: result.item.date, title: result.item.title, kind: result.item.kind });
|
|
847
|
+
if (apply && usable(result.status) && result.value)
|
|
848
|
+
applyDigest(candidates[i], result.value, snapshot, resolve, writer, report);
|
|
849
|
+
});
|
|
850
|
+
}
|
|
851
|
+
else if (task === "profile") {
|
|
852
|
+
const candidates = profileCandidates(snapshot, day, report).slice(0, limit);
|
|
853
|
+
const run = await step.runBatch(profileSpec(day), candidates.map((candidate) => candidate.item));
|
|
854
|
+
record(report, run.results, run.stats);
|
|
855
|
+
run.results.forEach((result, i) => {
|
|
856
|
+
note(task, result, result.item);
|
|
857
|
+
if (apply && usable(result.status) && result.value)
|
|
858
|
+
applyProfile(candidates[i], result.value, snapshot, resolve, writer, report);
|
|
859
|
+
});
|
|
860
|
+
}
|
|
861
|
+
else if (task === "retype") {
|
|
862
|
+
const candidates = retypeCandidates(schema, snapshot, report).slice(0, limit);
|
|
863
|
+
const run = await step.runBatch(retypeSpec(day), candidates.map((candidate) => candidate.item));
|
|
864
|
+
record(report, run.results, run.stats);
|
|
865
|
+
run.results.forEach((result, i) => {
|
|
866
|
+
note(task, result, result.item);
|
|
867
|
+
if (apply && usable(result.status) && result.value)
|
|
868
|
+
applyRetype(candidates[i], result.value, store, snapshot, writer, day, report);
|
|
869
|
+
});
|
|
870
|
+
}
|
|
871
|
+
else if (task === "supersede") {
|
|
872
|
+
const candidates = supersessionCandidates(snapshot, report).slice(0, limit);
|
|
873
|
+
const run = await step.runBatch(supersessionSpec(day), candidates.map((candidate) => candidate.item));
|
|
874
|
+
record(report, run.results, run.stats);
|
|
875
|
+
run.results.forEach((result, i) => {
|
|
876
|
+
note(task, result, result.item);
|
|
877
|
+
if (apply && usable(result.status) && result.value)
|
|
878
|
+
applySupersession(candidates[i], result.value, writer, report);
|
|
879
|
+
});
|
|
880
|
+
}
|
|
881
|
+
else if (options.summaryItems) {
|
|
882
|
+
const minSignals = options.summaryMinSignals ?? 20;
|
|
883
|
+
const counted = [];
|
|
884
|
+
for (const [ref, rows] of snapshot.rows) {
|
|
885
|
+
if (!inScope(snapshot, ref))
|
|
886
|
+
continue;
|
|
887
|
+
const signals = rows.filter((stored) => stored.row.kind === "T").length;
|
|
888
|
+
if (signals > 0)
|
|
889
|
+
report.candidates += 1;
|
|
890
|
+
if (signals >= minSignals)
|
|
891
|
+
counted.push({ ref, signals });
|
|
892
|
+
else if (signals > 0)
|
|
893
|
+
bump(report.skipped, `fewer than ${minSignals} timeline rows`);
|
|
894
|
+
}
|
|
895
|
+
const refs = counted.sort((a, b) => b.signals - a.signals || byCode(a.ref, b.ref)).slice(0, limit).map((entry) => entry.ref);
|
|
896
|
+
const run = await summarizePages(step, options.summaryItems(refs), { day });
|
|
897
|
+
record(report, run.results, run.stats);
|
|
898
|
+
for (const result of run.results) {
|
|
899
|
+
note(task, result, result.item);
|
|
900
|
+
if (usable(result.status) && result.value)
|
|
901
|
+
bump(report.rows, "page summary sentences", result.value.sentences.length);
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
writer.flush();
|
|
905
|
+
return report;
|
|
906
|
+
};
|
|
907
|
+
if (options.store)
|
|
908
|
+
reports.push(await runTask(options.store, applyRequested, true));
|
|
909
|
+
else if (!applyRequested)
|
|
910
|
+
reports.push(await runTask(openGraphStore(context.graphRoot, { company: context.company }), false, true));
|
|
911
|
+
else {
|
|
912
|
+
// Ask first, write after, each on a store opened for the purpose. The model turns can take an hour and a store
|
|
913
|
+
// holds what it read; if the graph folder is rebuilt meanwhile, writing from that store would put old rows back.
|
|
914
|
+
// The second round reads the folder as it is now, gets its answers from the cache, and writes for a few seconds.
|
|
915
|
+
const asked = await runTask(openGraphStore(context.graphRoot, { company: context.company }), false, true);
|
|
916
|
+
const written = await runTask(openGraphStore(context.graphRoot, { company: context.company }), true, false);
|
|
917
|
+
reports.push({
|
|
918
|
+
...written,
|
|
919
|
+
stats: {
|
|
920
|
+
...written.stats,
|
|
921
|
+
cacheHits: asked.stats.cacheHits,
|
|
922
|
+
calls: asked.stats.calls + written.stats.calls,
|
|
923
|
+
ok: asked.stats.ok + written.stats.ok,
|
|
924
|
+
usage: { inputTokens: asked.stats.usage.inputTokens + written.stats.usage.inputTokens, outputTokens: asked.stats.usage.outputTokens + written.stats.usage.outputTokens },
|
|
925
|
+
estimated: asked.stats.estimated || written.stats.estimated,
|
|
926
|
+
seconds: asked.stats.seconds + written.stats.seconds,
|
|
927
|
+
},
|
|
928
|
+
perItem: asked.perItem,
|
|
929
|
+
});
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
return { day, model: options.step.model, applied: applyRequested, tasks: reports, seconds: (Date.now() - started) / 1000 };
|
|
933
|
+
}
|
|
934
|
+
/** Citations of model-written rows, newest record per row. */
|
|
935
|
+
export function readModelRowCitations(graphRoot) {
|
|
936
|
+
const out = new Map();
|
|
937
|
+
let text;
|
|
938
|
+
try {
|
|
939
|
+
text = fs.readFileSync(path.join(graphRoot, ...MODEL_ROWS_FILE.split("/")), "utf8");
|
|
940
|
+
}
|
|
941
|
+
catch {
|
|
942
|
+
return out;
|
|
943
|
+
}
|
|
944
|
+
for (const line of text.split("\n")) {
|
|
945
|
+
if (line.trim() === "")
|
|
946
|
+
continue;
|
|
947
|
+
try {
|
|
948
|
+
const citation = JSON.parse(line);
|
|
949
|
+
if (citation && typeof citation.row === "string")
|
|
950
|
+
out.set(`${citation.entity}|${citation.row}`, citation);
|
|
951
|
+
}
|
|
952
|
+
catch {
|
|
953
|
+
// A torn last line from a killed run.
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
return out;
|
|
957
|
+
}
|
|
958
|
+
//# sourceMappingURL=pass.js.map
|