@indigoai-us/hq-cli 5.345.251 → 5.345.253
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 +153 -0
- package/assets/scaffold/core/scripts/hq-session.sh +9 -0
- package/contracts/host-canary-release.json +9 -3
- package/dist/command-catalog.generated.d.ts +269 -1
- package/dist/command-catalog.generated.js +330 -1
- package/dist/command-registration-plan.d.ts +6 -0
- package/dist/command-registration-plan.js +1 -0
- package/dist/commands/bot.js +7 -2
- package/dist/commands/ci-intake.d.ts +15 -0
- package/dist/commands/ci-intake.js +252 -0
- package/dist/commands/ci-metrics.d.ts +8 -0
- package/dist/commands/ci-metrics.js +55 -0
- package/dist/commands/ci.d.ts +13 -0
- package/dist/commands/ci.js +10 -0
- package/dist/commands/core-bucket-c-work-mesh-offer-native.js +1 -1
- package/dist/commands/core-hq-session-binding.d.ts +140 -0
- package/dist/commands/core-hq-session-binding.js +363 -0
- package/dist/commands/core-hq-session-native.d.ts +23 -0
- package/dist/commands/core-hq-session-native.js +59 -9
- package/dist/commands/core-stop-autosave.js +1 -1
- package/dist/commands/core.js +10 -0
- package/dist/commands/doctor.d.ts +5 -1
- package/dist/commands/doctor.js +42 -5
- package/dist/commands/files.d.ts +62 -0
- package/dist/commands/files.js +250 -9
- package/dist/commands/install-global.js +4 -2
- package/dist/commands/logout.js +4 -0
- package/dist/commands/ontology-graph.d.ts +2 -0
- package/dist/commands/ontology-graph.js +33 -0
- package/dist/commands/report.js +35 -2
- package/dist/commands/storage-gate.js +1 -1
- package/dist/commands/vault-retention-flag.js +2 -1
- package/dist/commands/whoami.js +1 -1
- package/dist/hq-cloud-sync-runner/bin/sync-runner.js +238 -221
- package/dist/hq-cloud-sync-runner/package.json +1 -1
- package/dist/index.js +3 -1
- package/dist/lib/agent-kit/inbox-poll-backoff-flag.js +1 -1
- package/dist/lib/agent-kit/mcp/tools.js +48 -0
- package/dist/lib/bot/fleet-bots-update-pass.js +1 -1
- package/dist/lib/bot/fleet-cron.d.ts +50 -0
- package/dist/lib/bot/fleet-cron.js +173 -32
- package/dist/lib/bot/fleet-local.d.ts +18 -0
- package/dist/lib/bot/fleet-local.js +51 -5
- package/dist/lib/bot/inbox-poll-backoff-flag.js +1 -1
- package/dist/lib/bot/signals-engine-flag.d.ts +1 -1
- package/dist/lib/bot/signals-engine-flag.js +1 -1
- package/dist/lib/cli-invite-nudge.js +1 -1
- package/dist/lib/client-version.d.ts +2 -0
- package/dist/lib/client-version.js +24 -0
- package/dist/lib/company-flag-gate.d.ts +30 -0
- package/dist/lib/company-flag-gate.js +104 -0
- package/dist/lib/core-utils/hook-timeout-debug-context-flag.js +1 -1
- package/dist/lib/daemon/flag.js +2 -1
- package/dist/lib/daemon/legacy-journal-gate.js +3 -1
- package/dist/lib/daemon/run.js +4 -3
- package/dist/lib/daemon/shared-flag-reads.js +1 -1
- package/dist/lib/delivery/migrations.d.ts +32 -1
- package/dist/lib/delivery/migrations.js +69 -8
- package/dist/lib/doctor/checks/host-core.js +22 -4
- package/dist/lib/doctor/checks/jq.d.ts +27 -6
- package/dist/lib/doctor/checks/jq.js +46 -8
- package/dist/lib/doctor/client-health-reason-codes-flag.js +1 -1
- package/dist/lib/doctor/fix/refresh-sync-handoff.d.ts +1 -1
- package/dist/lib/doctor/fix/refresh-sync-handoff.js +1 -1
- package/dist/lib/flag-client-gate-headers.d.ts +38 -0
- package/dist/lib/flag-client-gate-headers.js +62 -0
- package/dist/lib/flag-client-headers.d.ts +15 -0
- package/dist/lib/flag-client-headers.js +25 -0
- package/dist/lib/flag-registry-worker-source.generated.d.ts +1 -1
- package/dist/lib/flag-registry-worker-source.generated.js +1 -1
- package/dist/lib/flag-registry-worker.js +35 -0
- package/dist/lib/flag-registry.d.ts +1 -20
- package/dist/lib/flag-registry.js +5 -81
- package/dist/lib/hook-engine/adapter-core-pin.d.ts +30 -0
- package/dist/lib/hook-engine/adapter-core-pin.js +30 -0
- package/dist/lib/hook-engine/coverage.d.ts +65 -1
- package/dist/lib/hook-engine/coverage.js +173 -8
- package/dist/lib/hook-engine/reconcile.js +3 -2
- package/dist/lib/hot-channels/prompts/signals-pass.md +20 -0
- package/dist/lib/hot-channels/signal-changes.d.ts +228 -0
- package/dist/lib/hot-channels/signal-changes.js +683 -0
- package/dist/lib/hot-channels/signals-pass.d.ts +24 -0
- package/dist/lib/hot-channels/signals-pass.js +232 -12
- package/dist/lib/lanes/outpost-worktree-pruner-flag.d.ts +1 -1
- package/dist/lib/lanes/outpost-worktree-pruner-flag.js +1 -1
- package/dist/lib/lanes/registry-flag.js +2 -1
- package/dist/lib/lanes/spawn.js +11 -1
- package/dist/lib/mesh/live/binding-intent.d.ts +61 -0
- package/dist/lib/mesh/live/binding-intent.js +201 -0
- package/dist/lib/mesh/live/binding-receipt.d.ts +157 -0
- package/dist/lib/mesh/live/binding-receipt.js +365 -0
- package/dist/lib/mesh/live/company-flag-refresh.js +1 -1
- package/dist/lib/mesh/live/daemon/doctor-telemetry.d.ts +39 -0
- package/dist/lib/mesh/live/daemon/doctor-telemetry.js +58 -0
- package/dist/lib/mesh/live/daemon/install.js +2 -1
- package/dist/lib/mesh/live/daemon/presence.js +9 -0
- package/dist/lib/mesh/live/daemon/run.d.ts +3 -0
- package/dist/lib/mesh/live/daemon/run.js +33 -1
- package/dist/lib/ontology-graph/lineage/index.d.ts +401 -0
- package/dist/lib/ontology-graph/lineage/index.js +1027 -0
- package/dist/lib/ontology-graph/model/index.d.ts +54 -0
- package/dist/lib/ontology-graph/model/index.js +112 -0
- package/dist/lib/ontology-graph/model/pass-tasks.d.ts +75 -0
- package/dist/lib/ontology-graph/model/pass-tasks.js +166 -0
- package/dist/lib/ontology-graph/model/pass.d.ts +6 -0
- package/dist/lib/ontology-graph/model/pass.js +9 -2
- package/dist/lib/ontology-graph/model/pending-items.d.ts +8 -0
- package/dist/lib/ontology-graph/model/pending-items.js +24 -1
- package/dist/lib/ontology-graph/model/step.d.ts +1 -1
- package/dist/lib/ontology-graph/model/tasks.d.ts +11 -3
- package/dist/lib/ontology-graph/model/tasks.js +20 -7
- package/dist/lib/ontology-graph/read/brief.js +41 -6
- package/dist/lib/ontology-graph/read/index.d.ts +4 -0
- package/dist/lib/ontology-graph/read/index.js +173 -29
- package/dist/lib/ontology-graph/read/lineage.d.ts +101 -0
- package/dist/lib/ontology-graph/read/lineage.js +262 -0
- package/dist/lib/ontology-graph/replay/buffered-store.js +17 -0
- package/dist/lib/ontology-graph/replay/engine.d.ts +32 -1
- package/dist/lib/ontology-graph/replay/engine.js +213 -8
- package/dist/lib/ontology-graph/replay/extractors.d.ts +26 -0
- package/dist/lib/ontology-graph/replay/extractors.js +25 -1
- package/dist/lib/ontology-graph/replay/index.d.ts +2 -0
- package/dist/lib/ontology-graph/replay/index.js +9 -3
- package/dist/lib/ontology-graph/replay/signal-changes.d.ts +118 -0
- package/dist/lib/ontology-graph/replay/signal-changes.js +687 -0
- package/dist/lib/ontology-graph/schema/decision-status.d.ts +18 -0
- package/dist/lib/ontology-graph/schema/decision-status.js +20 -0
- package/dist/lib/ontology-graph/schema/index.d.ts +1 -0
- package/dist/lib/ontology-graph/schema/index.js +1 -0
- package/dist/lib/ontology-graph/setup/index.js +13 -3
- package/dist/lib/search-index/index.d.ts +33 -1
- package/dist/lib/search-index/index.js +180 -25
- package/dist/lib/search-index/qmd-exit-diagnostics.d.ts +7 -0
- package/dist/lib/search-index/qmd-exit-diagnostics.js +25 -1
- package/dist/lib/signals/adapters-catalog.js +1 -1
- package/dist/lib/smart-ci/cost.d.ts +72 -0
- package/dist/lib/smart-ci/cost.js +120 -0
- package/dist/lib/smart-ci/github.d.ts +112 -0
- package/dist/lib/smart-ci/github.js +267 -0
- package/dist/lib/smart-ci/intake.d.ts +288 -0
- package/dist/lib/smart-ci/intake.js +1078 -0
- package/dist/lib/smart-ci/ledger.d.ts +218 -0
- package/dist/lib/smart-ci/ledger.js +527 -0
- package/dist/lib/smart-ci/metrics.d.ts +137 -0
- package/dist/lib/smart-ci/metrics.js +249 -0
- package/dist/lib/smart-ci/records.d.ts +265 -0
- package/dist/lib/smart-ci/records.js +281 -0
- package/dist/lib/smart-ci/reducer.d.ts +67 -0
- package/dist/lib/smart-ci/reducer.js +261 -0
- package/dist/lib/sync-core/engine-holds.d.ts +15 -0
- package/dist/lib/sync-core/engine-holds.js +104 -11
- package/dist/lib/sync-core/engine-home.d.ts +3 -0
- package/dist/lib/sync-core/engine-home.js +41 -1
- package/dist/lib/sync-core/flag.js +2 -1
- package/dist/lib/sync-core/hand-back.d.ts +1 -0
- package/dist/lib/sync-core/hand-back.js +7 -2
- package/dist/lib/sync-core/legacy-journal-export-child.js +5 -3
- package/dist/lib/sync-core/legacy-journal-export.d.ts +23 -4
- package/dist/lib/sync-core/legacy-journal-export.js +120 -55
- package/dist/lib/sync-core/reconcile.d.ts +2 -1
- package/dist/lib/sync-core/reconcile.js +40 -7
- package/dist/lib/sync-invalid-entity-warning.js +2 -1
- package/dist/lib/windows/install-followup.d.ts +41 -0
- package/dist/lib/windows/install-followup.js +87 -0
- package/dist/lib/windows/jq.d.ts +59 -0
- package/dist/lib/windows/jq.js +154 -3
- package/dist/lib/windows/user-path.d.ts +5 -0
- package/dist/lib/windows/user-path.js +1 -1
- package/dist/lib/work-context/auto-bind.d.ts +12 -0
- package/dist/lib/work-context/auto-bind.js +49 -3
- package/dist/lib/work-context/organize.d.ts +4 -2
- package/dist/lib/work-context/organize.js +18 -8
- package/dist/lib/work-context/reconcile.js +16 -1
- package/dist/main.js +12 -1
- package/dist/utils/client-health-contract.d.ts +30 -0
- package/dist/utils/client-health-contract.js +111 -0
- package/dist/utils/client-health.d.ts +5 -2
- package/dist/utils/client-health.js +74 -5
- package/dist/utils/exclusive-lock.d.ts +5 -1
- package/dist/utils/exclusive-lock.js +9 -2
- package/dist/utils/host-registration.d.ts +7 -0
- package/dist/utils/host-registration.js +10 -1
- package/dist/utils/pid.d.ts +31 -0
- package/dist/utils/pid.js +65 -0
- package/dist/utils/resident-package-preload.js +12 -4
- package/dist/utils/sentry-fingerprint.js +3 -0
- package/package.json +10 -8
- package/scripts/install-hook-engine.mjs +5 -3
|
@@ -0,0 +1,687 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rule `signal-changes`: lineage a signal states is written to the graph
|
|
3
|
+
* without a model.
|
|
4
|
+
*
|
|
5
|
+
* The signals pass may put `changes` on a signal file: the earlier signal it
|
|
6
|
+
* replaces, reverses or withdraws, with words quoted from the source
|
|
7
|
+
* (hot-channels/signal-changes.ts). The signals extractor collects those
|
|
8
|
+
* entries while a day is replayed and the engine keeps every entry it has
|
|
9
|
+
* collected in `.state/signal-changes.json`. `applySignalChanges` takes the
|
|
10
|
+
* whole set and brings the rule's rows in line with it:
|
|
11
|
+
*
|
|
12
|
+
* - `replaces`, `reverses`: a `supersedes` edge from the new decision to the
|
|
13
|
+
* earlier one (the store writes `superseded_by` on the other end), and the
|
|
14
|
+
* earlier decision's status becomes `superseded` or `reversed`.
|
|
15
|
+
* - `withdraws`: the earlier decision's status becomes `withdrawn`. No edge.
|
|
16
|
+
* - `completes` and any other kind: not applied to a decision, counted as
|
|
17
|
+
* `kind_not_applied`.
|
|
18
|
+
*
|
|
19
|
+
* The rows are a function of the set of entries and of the rows other origins
|
|
20
|
+
* wrote, never of the order the entries were read in or of how many times the
|
|
21
|
+
* set was applied. For one earlier decision, the entries that apply are put in
|
|
22
|
+
* order by day, then signal time, then signal id, and the status rows follow
|
|
23
|
+
* that order: each row is valid from its entry's day and is closed on the day
|
|
24
|
+
* of the next entry, with that entry's signal as the closing source; the last
|
|
25
|
+
* one is open. A decision replaced on one day and withdrawn on a later day
|
|
26
|
+
* therefore holds `superseded` (closed) and `withdrawn` (open) however often
|
|
27
|
+
* the graph is rebuilt, and an `update` reaches the same rows as a rebuild.
|
|
28
|
+
*
|
|
29
|
+
* An entry is left alone, and counted, when
|
|
30
|
+
* - its kind is not applied, or its target is not a decision (the graph holds
|
|
31
|
+
* no entity for an action item, a commitment or a risk);
|
|
32
|
+
* - no decision in the same audience has a statement from the target signal;
|
|
33
|
+
* - the target is the new decision itself, or is dated after the new signal;
|
|
34
|
+
* - its edge would close a loop of any length with edges the rule has already
|
|
35
|
+
* accepted in the same audience view (`would_loop`), or two entries of the
|
|
36
|
+
* same day and time replace each other (`mutual_pair`, both left alone);
|
|
37
|
+
* - a person or a record wrote, on or after the entry's day, an open status on
|
|
38
|
+
* the earlier decision that says it stands, such as `decided`
|
|
39
|
+
* (`status_held_by_a_record_or_a_person`): no edge and no status. Rows the
|
|
40
|
+
* rule wrote for that entry before the person or record spoke are closed on
|
|
41
|
+
* the day of that status.
|
|
42
|
+
*
|
|
43
|
+
* When the person's or record's status is older than the entry, or says the
|
|
44
|
+
* decision ended (`endsDecision`: superseded, reversed, withdrawn, replaced)
|
|
45
|
+
* and so agrees with it, the edge is written and the status row is not: a
|
|
46
|
+
* rule does not close a row of higher authority. A model-written `supersedes`
|
|
47
|
+
* edge that points the other way in the entry's own audience is closed on
|
|
48
|
+
* the entry's day and the rule's edge is written; one in another audience is
|
|
49
|
+
* left as it is and counted.
|
|
50
|
+
*
|
|
51
|
+
* An entry its signal no longer states is taken out with its rows, and when
|
|
52
|
+
* no entry on a decision applies any more, the status the first entry closed
|
|
53
|
+
* is open again: the graph holds what a build from the signals as they are
|
|
54
|
+
* now would hold.
|
|
55
|
+
*
|
|
56
|
+
* The set is kept in `.state/signal-changes.json`. A file that is missing on a
|
|
57
|
+
* graph that was replayed before, or that cannot be used (not JSON, another
|
|
58
|
+
* version, an entry the rule cannot apply), is not an error: the engine
|
|
59
|
+
* builds the set again from the signal files and says so.
|
|
60
|
+
*/
|
|
61
|
+
import * as fs from "node:fs";
|
|
62
|
+
import { endsDecision, inverseEdge, isAudienceKey, isEntityRef, isIsoDate, isOpen, isSourceRef, originTier, rowId } from "../schema/index.js";
|
|
63
|
+
import { GRAPH_PATHS, applyIngest, atomicWriteFile, graphPath } from "../store/index.js";
|
|
64
|
+
import { COMPANY_AUDIENCE } from "../types.js";
|
|
65
|
+
export const SIGNAL_CHANGES_RULE = "rule:signal-changes";
|
|
66
|
+
/**
|
|
67
|
+
* Lower than the 0.90 a rule that copies a field from a record gets. The entry
|
|
68
|
+
* the rule applies was written by a model that read a document, and the quote
|
|
69
|
+
* check proves the words are in the source, not that they mean what the model
|
|
70
|
+
* took them to mean. Readers show the origin as `from a signal quote`.
|
|
71
|
+
*/
|
|
72
|
+
export const SIGNAL_CHANGES_CONFIDENCE = 0.75;
|
|
73
|
+
/** Where the engine keeps every entry it has collected, inside the graph folder. */
|
|
74
|
+
export const SIGNAL_CHANGES_STATE_RELATIVE = `${GRAPH_PATHS.state}/signal-changes.json`;
|
|
75
|
+
/** Kinds the rule acts on, and the status each gives the earlier decision. */
|
|
76
|
+
export const CHANGE_STATUS = {
|
|
77
|
+
replaces: "superseded",
|
|
78
|
+
reverses: "reversed",
|
|
79
|
+
withdraws: "withdrawn",
|
|
80
|
+
};
|
|
81
|
+
/** Kinds that link the new decision to the earlier one. */
|
|
82
|
+
const LINKING_KINDS = new Set(["replaces", "reverses"]);
|
|
83
|
+
/** The `changes` list of a signal's frontmatter, with anything malformed left out. */
|
|
84
|
+
export function signalChangesOf(frontmatter) {
|
|
85
|
+
const raw = frontmatter.changes;
|
|
86
|
+
if (!Array.isArray(raw))
|
|
87
|
+
return [];
|
|
88
|
+
const out = [];
|
|
89
|
+
for (const entry of raw) {
|
|
90
|
+
const change = entry;
|
|
91
|
+
if (!change || typeof change !== "object")
|
|
92
|
+
continue;
|
|
93
|
+
if (typeof change.target !== "string" || !/^[a-f0-9]{64}$/.test(change.target) || typeof change.kind !== "string")
|
|
94
|
+
continue;
|
|
95
|
+
out.push({ target: change.target, targetType: typeof change.target_type === "string" ? change.target_type : "", kind: change.kind });
|
|
96
|
+
}
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
/** The time a signal file says it was written, as text that sorts in time order, or empty. */
|
|
100
|
+
export function signalTimeOf(frontmatter) {
|
|
101
|
+
const raw = frontmatter.written_at;
|
|
102
|
+
if (raw instanceof Date)
|
|
103
|
+
return Number.isNaN(raw.getTime()) ? "" : raw.toISOString();
|
|
104
|
+
if (typeof raw !== "string")
|
|
105
|
+
return "";
|
|
106
|
+
const parsed = Date.parse(raw.trim());
|
|
107
|
+
return Number.isNaN(parsed) ? "" : new Date(parsed).toISOString();
|
|
108
|
+
}
|
|
109
|
+
function byCode(a, b) {
|
|
110
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
111
|
+
}
|
|
112
|
+
/** One entry of the set: a signal states one kind of change to one target in one audience. */
|
|
113
|
+
export function signalChangeKey(change) {
|
|
114
|
+
return [change.audience, change.src, change.target, change.targetType, change.kind].join("|");
|
|
115
|
+
}
|
|
116
|
+
/** The order entries are applied in: day, then signal time, then signal id. Never the order they were read in. */
|
|
117
|
+
export function compareSignalChanges(a, b) {
|
|
118
|
+
return (byCode(a.day, b.day) ||
|
|
119
|
+
byCode(a.time ?? "", b.time ?? "") ||
|
|
120
|
+
byCode(a.src, b.src) ||
|
|
121
|
+
byCode(a.target, b.target) ||
|
|
122
|
+
byCode(a.kind, b.kind) ||
|
|
123
|
+
byCode(a.audience, b.audience));
|
|
124
|
+
}
|
|
125
|
+
function normalized(change) {
|
|
126
|
+
return {
|
|
127
|
+
src: change.src,
|
|
128
|
+
day: change.day,
|
|
129
|
+
...(change.time ? { time: change.time } : {}),
|
|
130
|
+
audience: change.audience,
|
|
131
|
+
decision: change.decision,
|
|
132
|
+
target: change.target,
|
|
133
|
+
targetType: change.targetType,
|
|
134
|
+
kind: change.kind,
|
|
135
|
+
...(change.retracted ? { retracted: true } : {}),
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* The set after a day's signals were replayed. `replayed` names every signal
|
|
140
|
+
* of the day (`<audience>|<src>`), with or without entries: an entry the set
|
|
141
|
+
* holds for one of them that the signal no longer states is marked retracted,
|
|
142
|
+
* so its rows are closed rather than left open with nothing behind them.
|
|
143
|
+
*/
|
|
144
|
+
export function mergeSignalChanges(known, replayed, stated) {
|
|
145
|
+
const set = new Map();
|
|
146
|
+
for (const change of known)
|
|
147
|
+
set.set(signalChangeKey(change), normalized(change));
|
|
148
|
+
const now = new Set(stated.map(signalChangeKey));
|
|
149
|
+
for (const [key, change] of set) {
|
|
150
|
+
if (replayed.has(`${change.audience}|${change.src}`) && !now.has(key))
|
|
151
|
+
set.set(key, { ...change, retracted: true });
|
|
152
|
+
}
|
|
153
|
+
for (const change of stated)
|
|
154
|
+
set.set(signalChangeKey(change), normalized({ ...change, retracted: false }));
|
|
155
|
+
return [...set.values()].sort(compareSignalChanges);
|
|
156
|
+
}
|
|
157
|
+
/** The version of the state file this code reads and writes. */
|
|
158
|
+
export const SIGNAL_CHANGES_STATE_VERSION = 1;
|
|
159
|
+
/** The entry when every field is what the rule can apply, else null. Nothing else reaches a row. */
|
|
160
|
+
export function validSignalChange(entry) {
|
|
161
|
+
const change = entry;
|
|
162
|
+
if (!change || typeof change !== "object" || Array.isArray(change))
|
|
163
|
+
return null;
|
|
164
|
+
if (typeof change.src !== "string" || !isSourceRef(change.src) || !/^signal:[0-9a-f]{12}$/.test(change.src))
|
|
165
|
+
return null;
|
|
166
|
+
if (!isIsoDate(change.day) || !isAudienceKey(change.audience))
|
|
167
|
+
return null;
|
|
168
|
+
if (typeof change.target !== "string" || !/^[a-f0-9]{64}$/.test(change.target))
|
|
169
|
+
return null;
|
|
170
|
+
if (typeof change.targetType !== "string" || typeof change.kind !== "string" || change.kind === "")
|
|
171
|
+
return null;
|
|
172
|
+
if (change.decision !== null && !(typeof change.decision === "string" && isEntityRef(change.decision) && change.decision.startsWith("decision/")))
|
|
173
|
+
return null;
|
|
174
|
+
if (change.time !== undefined && (typeof change.time !== "string" || Number.isNaN(Date.parse(change.time))))
|
|
175
|
+
return null;
|
|
176
|
+
if (change.retracted !== undefined && typeof change.retracted !== "boolean")
|
|
177
|
+
return null;
|
|
178
|
+
return normalized(change);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Reads the entries kept in the graph folder. Never throws. A file that is not
|
|
182
|
+
* JSON, has another version, or holds one entry the rule could not apply is
|
|
183
|
+
* `invalid` as a whole: a set with a hole in it would give other rows than the
|
|
184
|
+
* signals state, so the caller builds the set again from the signal files.
|
|
185
|
+
*/
|
|
186
|
+
export function loadSignalChangesState(graphRoot) {
|
|
187
|
+
let text;
|
|
188
|
+
try {
|
|
189
|
+
text = fs.readFileSync(graphPath(graphRoot, SIGNAL_CHANGES_STATE_RELATIVE), "utf8");
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
const code = error.code;
|
|
193
|
+
if (code === "ENOENT" || code === "ENOTDIR")
|
|
194
|
+
return { changes: [], state: "missing" };
|
|
195
|
+
return { changes: [], state: "invalid", problem: `it could not be read (${code ?? "error"})` };
|
|
196
|
+
}
|
|
197
|
+
let parsed;
|
|
198
|
+
try {
|
|
199
|
+
parsed = JSON.parse(text);
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
return { changes: [], state: "invalid", problem: "it is not JSON" };
|
|
203
|
+
}
|
|
204
|
+
const record = parsed;
|
|
205
|
+
if (!record || typeof record !== "object" || Array.isArray(record))
|
|
206
|
+
return { changes: [], state: "invalid", problem: "it is not an object" };
|
|
207
|
+
if (record.version !== SIGNAL_CHANGES_STATE_VERSION)
|
|
208
|
+
return { changes: [], state: "invalid", problem: `its version is ${JSON.stringify(record.version) ?? "missing"}, not ${String(SIGNAL_CHANGES_STATE_VERSION)}` };
|
|
209
|
+
if (!Array.isArray(record.changes))
|
|
210
|
+
return { changes: [], state: "invalid", problem: "it has no list of changes" };
|
|
211
|
+
const out = [];
|
|
212
|
+
for (const [index, entry] of record.changes.entries()) {
|
|
213
|
+
const change = validSignalChange(entry);
|
|
214
|
+
if (!change)
|
|
215
|
+
return { changes: [], state: "invalid", problem: `entry ${String(index + 1)} of ${String(record.changes.length)} is not a change the rule can apply` };
|
|
216
|
+
out.push(change);
|
|
217
|
+
}
|
|
218
|
+
return { changes: out.sort(compareSignalChanges), state: "ok" };
|
|
219
|
+
}
|
|
220
|
+
/** The entries kept in the graph folder. No file, or one that cannot be used: none (see `loadSignalChangesState`). */
|
|
221
|
+
export function readSignalChangesState(graphRoot) {
|
|
222
|
+
return loadSignalChangesState(graphRoot).changes;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Writes the set, sorted, so the file is the same for the same set. The write
|
|
226
|
+
* is a temp file renamed over the target (`atomicWriteFile`), so a reader sees
|
|
227
|
+
* the old set or the new one. Writes nothing when the file already says so.
|
|
228
|
+
*/
|
|
229
|
+
export function writeSignalChangesState(graphRoot, changes) {
|
|
230
|
+
const text = `${JSON.stringify({ version: SIGNAL_CHANGES_STATE_VERSION, changes: [...changes].map(normalized).sort(compareSignalChanges) }, null, 1)}\n`;
|
|
231
|
+
atomicWriteFile(graphPath(graphRoot, SIGNAL_CHANGES_STATE_RELATIVE), text);
|
|
232
|
+
}
|
|
233
|
+
function statementOf(rows, audience, src) {
|
|
234
|
+
return rows.find((stored) => stored.row.kind === "F" && stored.row.field === "statement" && stored.audience === audience && stored.row.src === src);
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* The decision a signal produced, in one audience: the decision whose slug ends
|
|
238
|
+
* with the first six hex of the signal id and whose statement cites the signal.
|
|
239
|
+
*/
|
|
240
|
+
export function decisionOfSignal(store, audience, src, decisions = store.listEntities("decision")) {
|
|
241
|
+
const suffix = `-${src.slice("signal:".length, "signal:".length + 6)}`;
|
|
242
|
+
for (const ref of decisions) {
|
|
243
|
+
if (ref.endsWith(suffix) && statementOf(store.readRows(ref, { audiences: [audience] }), audience, src))
|
|
244
|
+
return ref;
|
|
245
|
+
}
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
function later(a, b) {
|
|
249
|
+
return a > b ? a : b;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Brings the rule's rows in line with the whole set of entries. The result
|
|
253
|
+
* does not depend on the order of `changes` or on what an earlier call wrote:
|
|
254
|
+
* applying the same set again writes nothing. `learned` on a row is its
|
|
255
|
+
* entry's day, so the bytes do not depend on the day of the run; the third
|
|
256
|
+
* argument is accepted for callers of the earlier signature and is not used.
|
|
257
|
+
*/
|
|
258
|
+
export function applySignalChanges(store, changes, _learned) {
|
|
259
|
+
const result = { links: 0, statusClosed: 0, statusWritten: 0, statusBy: {}, modelLinksClosed: 0, modelLinksOtherAudience: 0, rowsRemoved: 0, rowsAdded: 0, rowsClosed: 0, skipped: {}, reasons: new Map(), statusHeld: new Set(), touched: [] };
|
|
260
|
+
const schema = store.schema();
|
|
261
|
+
const ruleTier = originTier(SIGNAL_CHANGES_RULE);
|
|
262
|
+
const touched = new Set();
|
|
263
|
+
const changedRefs = new Set();
|
|
264
|
+
const skip = (key, reason) => {
|
|
265
|
+
if (result.reasons.has(key))
|
|
266
|
+
return;
|
|
267
|
+
result.reasons.set(key, reason);
|
|
268
|
+
result.skipped[reason] = (result.skipped[reason] ?? 0) + 1;
|
|
269
|
+
};
|
|
270
|
+
// One entry per key, in the order they apply.
|
|
271
|
+
// An entry the rule could not apply (a source ref that is not a signal's, a day that is not a day) is counted and
|
|
272
|
+
// left out before it can reach a row or a closure.
|
|
273
|
+
const unique = new Map();
|
|
274
|
+
for (const change of changes) {
|
|
275
|
+
const valid = validSignalChange(change);
|
|
276
|
+
if (valid)
|
|
277
|
+
unique.set(signalChangeKey(valid), valid);
|
|
278
|
+
else
|
|
279
|
+
result.skipped.invalid_entry = (result.skipped.invalid_entry ?? 0) + 1;
|
|
280
|
+
}
|
|
281
|
+
const ordered = [...unique.values()].sort(compareSignalChanges);
|
|
282
|
+
const readTarget = (ref, audience) => store.readRows(ref, { audiences: [audience] });
|
|
283
|
+
const close = (ref, audience, id, closure) => {
|
|
284
|
+
if (!store.closeRow(ref, audience, id, closure))
|
|
285
|
+
return false;
|
|
286
|
+
result.rowsClosed += 1;
|
|
287
|
+
changedRefs.add(ref);
|
|
288
|
+
return true;
|
|
289
|
+
};
|
|
290
|
+
const reopen = (ref, audience, id) => {
|
|
291
|
+
if (!store.reopenRow?.(ref, audience, id))
|
|
292
|
+
return false;
|
|
293
|
+
changedRefs.add(ref);
|
|
294
|
+
return true;
|
|
295
|
+
};
|
|
296
|
+
// ---- 1. which entries name a decision that is in the graph ----
|
|
297
|
+
let decisions = null;
|
|
298
|
+
const resolved = new Map();
|
|
299
|
+
const resolveTarget = (change) => {
|
|
300
|
+
const memo = `${change.audience}|${change.target}`;
|
|
301
|
+
const known = resolved.get(memo);
|
|
302
|
+
if (known !== undefined)
|
|
303
|
+
return known;
|
|
304
|
+
decisions ??= store.listEntities("decision");
|
|
305
|
+
const found = decisionOfSignal(store, change.audience, `signal:${change.target.slice(0, 12)}`, decisions);
|
|
306
|
+
resolved.set(memo, found);
|
|
307
|
+
return found;
|
|
308
|
+
};
|
|
309
|
+
const candidates = [];
|
|
310
|
+
/** Entries that name a decision in the graph and are not applied: the rows the rule wrote for them are closed. */
|
|
311
|
+
const withdrawnEntries = [];
|
|
312
|
+
for (const change of ordered) {
|
|
313
|
+
const key = signalChangeKey(change);
|
|
314
|
+
const status = CHANGE_STATUS[change.kind];
|
|
315
|
+
if (!status) {
|
|
316
|
+
skip(key, "kind_not_applied");
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
if (change.targetType !== "decision") {
|
|
320
|
+
skip(key, "target_not_a_decision");
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
const target = resolveTarget(change);
|
|
324
|
+
if (!target) {
|
|
325
|
+
skip(key, "target_not_in_graph");
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
if (change.retracted) {
|
|
329
|
+
skip(key, "no_longer_stated");
|
|
330
|
+
withdrawnEntries.push({ change, target });
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
if (target === change.decision) {
|
|
334
|
+
skip(key, "target_is_itself");
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
const statement = statementOf(readTarget(target, change.audience), change.audience, `signal:${change.target.slice(0, 12)}`);
|
|
338
|
+
if (statement.row.valid.from > change.day) {
|
|
339
|
+
skip(key, "target_not_older");
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
const linking = LINKING_KINDS.has(change.kind) && change.decision !== null && store.readEntity(change.decision) !== null;
|
|
343
|
+
candidates.push({ change, key, target, status, linking });
|
|
344
|
+
}
|
|
345
|
+
// ---- 2. the open status a person or a record wrote, per earlier decision ----
|
|
346
|
+
const held = new Map();
|
|
347
|
+
const holderOf = (target, audience) => {
|
|
348
|
+
const memo = `${audience}|${target}`;
|
|
349
|
+
const known = held.get(memo);
|
|
350
|
+
if (known !== undefined)
|
|
351
|
+
return known;
|
|
352
|
+
let holder = null;
|
|
353
|
+
for (const stored of readTarget(target, audience)) {
|
|
354
|
+
if (stored.row.kind !== "F" || stored.row.field !== "status" || !isOpen(stored.row) || originTier(stored.row.by) >= ruleTier)
|
|
355
|
+
continue;
|
|
356
|
+
const row = stored;
|
|
357
|
+
if (!holder || row.row.valid.from > holder.row.valid.from || (row.row.valid.from === holder.row.valid.from && row.id < holder.id))
|
|
358
|
+
holder = row;
|
|
359
|
+
}
|
|
360
|
+
held.set(memo, holder);
|
|
361
|
+
return holder;
|
|
362
|
+
};
|
|
363
|
+
// ---- 3. two entries of the same day and time that replace each other: neither is applied ----
|
|
364
|
+
const sameView = (a, b) => a === b || a === COMPANY_AUDIENCE || b === COMPANY_AUDIENCE;
|
|
365
|
+
const mutual = new Set();
|
|
366
|
+
const links = candidates.filter((candidate) => candidate.linking);
|
|
367
|
+
for (const one of links) {
|
|
368
|
+
for (const other of links) {
|
|
369
|
+
if (one === other || one.change.decision !== other.target || other.change.decision !== one.target)
|
|
370
|
+
continue;
|
|
371
|
+
if (!sameView(one.change.audience, other.change.audience))
|
|
372
|
+
continue;
|
|
373
|
+
if (one.change.day === other.change.day && (one.change.time ?? "") === (other.change.time ?? ""))
|
|
374
|
+
mutual.add(one.key);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
// ---- 4. accept in order; an edge that would close a loop is left out ----
|
|
378
|
+
const accepted = [];
|
|
379
|
+
const acceptedEdges = [];
|
|
380
|
+
/** True when `to` is reached from `from` over accepted edges a reader of `view` sees. */
|
|
381
|
+
const reaches = (from, to, view) => {
|
|
382
|
+
const seen = new Set([from]);
|
|
383
|
+
const queue = [from];
|
|
384
|
+
while (queue.length > 0) {
|
|
385
|
+
const at = queue.shift();
|
|
386
|
+
if (at === to)
|
|
387
|
+
return true;
|
|
388
|
+
for (const edge of acceptedEdges) {
|
|
389
|
+
if (edge.from !== at || seen.has(edge.to) || !(edge.audience === COMPANY_AUDIENCE || edge.audience === view))
|
|
390
|
+
continue;
|
|
391
|
+
seen.add(edge.to);
|
|
392
|
+
queue.push(edge.to);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return false;
|
|
396
|
+
};
|
|
397
|
+
/** Model-written edges that point the other way, to close when the entry is applied. */
|
|
398
|
+
const opposite = new Map();
|
|
399
|
+
for (const candidate of candidates) {
|
|
400
|
+
const { change, key, target } = candidate;
|
|
401
|
+
const drop = (reason) => {
|
|
402
|
+
skip(key, reason);
|
|
403
|
+
withdrawnEntries.push({ change, target });
|
|
404
|
+
};
|
|
405
|
+
if (mutual.has(key)) {
|
|
406
|
+
drop("mutual_pair");
|
|
407
|
+
continue;
|
|
408
|
+
}
|
|
409
|
+
// A person's or a record's status on or after the entry's day decides, when it disagrees: it says the decision
|
|
410
|
+
// stands. One that says the decision ended (`endsDecision`) agrees with the entry, and the link is kept.
|
|
411
|
+
const holder = holderOf(target, change.audience);
|
|
412
|
+
if (holder && holder.row.valid.from >= change.day && !endsDecision(holder.row.value)) {
|
|
413
|
+
drop("status_held_by_a_record_or_a_person");
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
if (candidate.linking) {
|
|
417
|
+
const decision = change.decision;
|
|
418
|
+
// A reader of the entry's audience sees it with the company's edges; a company-wide edge is seen in every view.
|
|
419
|
+
const views = change.audience === COMPANY_AUDIENCE ? [...new Set([COMPANY_AUDIENCE, ...acceptedEdges.map((edge) => edge.audience)])] : [change.audience];
|
|
420
|
+
if (views.some((view) => reaches(target, decision, view))) {
|
|
421
|
+
drop("would_loop");
|
|
422
|
+
continue;
|
|
423
|
+
}
|
|
424
|
+
// An open edge in the graph that says the earlier decision replaces the new one.
|
|
425
|
+
const audiences = change.audience === COMPANY_AUDIENCE ? [COMPANY_AUDIENCE] : [COMPANY_AUDIENCE, change.audience];
|
|
426
|
+
const against = store
|
|
427
|
+
.readRows(target, { audiences, kinds: ["E"], openOnly: true })
|
|
428
|
+
.filter((stored) => stored.row.kind === "E" && stored.row.verb === "supersedes" && stored.row.target === decision);
|
|
429
|
+
// A person's or a record's edge stands, and so does one this rule wrote for an entry that is not in the set
|
|
430
|
+
// (its signal is gone). A model's (or the old import's) gives way to the rule.
|
|
431
|
+
if (against.some((stored) => originTier(stored.row.by) <= ruleTier)) {
|
|
432
|
+
drop("would_loop");
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
// Only an edge in the entry's own audience is closed. A private entry never closes a company-wide row or
|
|
436
|
+
// puts a private signal's ref on one; that edge is left as it is and counted.
|
|
437
|
+
const own = against.filter((stored) => stored.audience === change.audience);
|
|
438
|
+
result.modelLinksOtherAudience += against.length - own.length;
|
|
439
|
+
if (own.length > 0)
|
|
440
|
+
opposite.set(key, own);
|
|
441
|
+
acceptedEdges.push({ from: decision, to: target, audience: change.audience });
|
|
442
|
+
}
|
|
443
|
+
accepted.push(candidate);
|
|
444
|
+
}
|
|
445
|
+
// ---- 5. rows ----
|
|
446
|
+
const ingest = new Map();
|
|
447
|
+
const queue = (change, row) => {
|
|
448
|
+
const entry = ingest.get(signalChangeKey(change)) ?? { change, rows: [] };
|
|
449
|
+
entry.rows.push(row);
|
|
450
|
+
ingest.set(signalChangeKey(change), entry);
|
|
451
|
+
};
|
|
452
|
+
const edgeOf = (change, target) => ({
|
|
453
|
+
kind: "E",
|
|
454
|
+
verb: "supersedes",
|
|
455
|
+
target,
|
|
456
|
+
valid: { from: change.day },
|
|
457
|
+
src: change.src,
|
|
458
|
+
learned: change.day,
|
|
459
|
+
by: SIGNAL_CHANGES_RULE,
|
|
460
|
+
conf: SIGNAL_CHANGES_CONFIDENCE,
|
|
461
|
+
});
|
|
462
|
+
const statusOf = (change, status) => ({
|
|
463
|
+
kind: "F",
|
|
464
|
+
field: "status",
|
|
465
|
+
value: status,
|
|
466
|
+
valid: { from: change.day },
|
|
467
|
+
src: change.src,
|
|
468
|
+
learned: change.day,
|
|
469
|
+
by: SIGNAL_CHANGES_RULE,
|
|
470
|
+
conf: SIGNAL_CHANGES_CONFIDENCE,
|
|
471
|
+
});
|
|
472
|
+
/** Closes an edge and the row that says the same from the other end. */
|
|
473
|
+
const closeEdge = (subject, audience, row, closure) => {
|
|
474
|
+
const done = close(subject, audience, rowId(row), closure);
|
|
475
|
+
const inverse = inverseEdge(schema, subject, row);
|
|
476
|
+
if (inverse)
|
|
477
|
+
close(inverse.entity, audience, rowId(inverse.row), closure);
|
|
478
|
+
return done;
|
|
479
|
+
};
|
|
480
|
+
/** True for a closure this rule wrote: by the entry's own signal, or by a status a person or a record wrote on the earlier decision. */
|
|
481
|
+
const ownClosure = (target, change, closed) => {
|
|
482
|
+
if (!closed)
|
|
483
|
+
return false;
|
|
484
|
+
if (closed.by === change.src)
|
|
485
|
+
return true;
|
|
486
|
+
return readTarget(target, change.audience).some((stored) => stored.row.kind === "F" && stored.row.field === "status" && originTier(stored.row.by) < ruleTier && stored.row.src === closed.by);
|
|
487
|
+
};
|
|
488
|
+
// Edges of the entries that apply.
|
|
489
|
+
for (const { change, key, target, linking } of accepted) {
|
|
490
|
+
if (!linking)
|
|
491
|
+
continue;
|
|
492
|
+
const decision = change.decision;
|
|
493
|
+
for (const stored of opposite.get(key) ?? []) {
|
|
494
|
+
if (closeEdge(target, stored.audience, stored.row, { on: later(stored.row.valid.from, change.day), by: change.src }))
|
|
495
|
+
result.modelLinksClosed += 1;
|
|
496
|
+
}
|
|
497
|
+
const edge = edgeOf(change, target);
|
|
498
|
+
const id = rowId(edge);
|
|
499
|
+
const present = store.readRows(decision, { audiences: [change.audience], kinds: ["E"] }).find((stored) => stored.id === id);
|
|
500
|
+
if (!present) {
|
|
501
|
+
queue(change, { entity: decision, audience: change.audience, row: edge });
|
|
502
|
+
}
|
|
503
|
+
else if (!isOpen(present.row) && ownClosure(target, change, present.row.closed)) {
|
|
504
|
+
// Closed by the rule while a person's status stood, or while the entry was not applied: open again.
|
|
505
|
+
// An edge anything else closed (a person's answer to a proposal) stays closed.
|
|
506
|
+
reopen(decision, change.audience, id);
|
|
507
|
+
const inverse = inverseEdge(schema, decision, edge);
|
|
508
|
+
if (inverse)
|
|
509
|
+
reopen(inverse.entity, change.audience, rowId(inverse.row));
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
// Status rows, one earlier decision at a time: every decision an entry of the set names, also one whose entries
|
|
513
|
+
// have all been taken back.
|
|
514
|
+
const byTarget = new Map();
|
|
515
|
+
const groupOf = (target, audience) => {
|
|
516
|
+
const memo = `${audience}|${target}`;
|
|
517
|
+
const group = byTarget.get(memo) ?? { target, audience, applied: [], srcs: new Set() };
|
|
518
|
+
byTarget.set(memo, group);
|
|
519
|
+
return group;
|
|
520
|
+
};
|
|
521
|
+
for (const candidate of candidates)
|
|
522
|
+
groupOf(candidate.target, candidate.change.audience).srcs.add(candidate.change.src);
|
|
523
|
+
for (const { change, target } of withdrawnEntries)
|
|
524
|
+
groupOf(target, change.audience).srcs.add(change.src);
|
|
525
|
+
for (const candidate of accepted)
|
|
526
|
+
groupOf(candidate.target, candidate.change.audience).applied.push(candidate);
|
|
527
|
+
for (const memo of [...byTarget.keys()].sort(byCode)) {
|
|
528
|
+
const { target, audience, applied, srcs } = byTarget.get(memo);
|
|
529
|
+
const holder = holderOf(target, audience);
|
|
530
|
+
const status = () => readTarget(target, audience).filter((stored) => stored.row.kind === "F" && stored.row.field === "status" && stored.audience === audience);
|
|
531
|
+
if (holder) {
|
|
532
|
+
// The person's or record's status is the answer. The rule writes none and leaves none of its own open.
|
|
533
|
+
for (const { key } of applied) {
|
|
534
|
+
result.statusHeld.add(key);
|
|
535
|
+
result.skipped.status_held_by_a_record_or_a_person = (result.skipped.status_held_by_a_record_or_a_person ?? 0) + 1;
|
|
536
|
+
}
|
|
537
|
+
for (const stored of status()) {
|
|
538
|
+
if (stored.row.by !== SIGNAL_CHANGES_RULE || !isOpen(stored.row))
|
|
539
|
+
continue;
|
|
540
|
+
if (close(target, audience, stored.id, { on: later(stored.row.valid.from, holder.row.valid.from), by: holder.row.src }))
|
|
541
|
+
result.statusClosed += 1;
|
|
542
|
+
}
|
|
543
|
+
continue;
|
|
544
|
+
}
|
|
545
|
+
if (applied.length === 0) {
|
|
546
|
+
// Every entry on this decision has been taken back: the status an entry closed (`decided`) is open again, as
|
|
547
|
+
// it would be had the entries never been stated.
|
|
548
|
+
for (const stored of status()) {
|
|
549
|
+
if (stored.row.by === SIGNAL_CHANGES_RULE || isOpen(stored.row) || !stored.row.closed || !srcs.has(stored.row.closed.by))
|
|
550
|
+
continue;
|
|
551
|
+
reopen(target, audience, stored.id);
|
|
552
|
+
}
|
|
553
|
+
continue;
|
|
554
|
+
}
|
|
555
|
+
const wanted = applied.map(({ change, status: value }, index) => {
|
|
556
|
+
const next = applied[index + 1]?.change;
|
|
557
|
+
const row = statusOf(change, value);
|
|
558
|
+
if (next) {
|
|
559
|
+
row.valid = { from: change.day, to: next.day };
|
|
560
|
+
row.closed = { on: next.day, by: next.src };
|
|
561
|
+
}
|
|
562
|
+
return { change, row, id: rowId(row) };
|
|
563
|
+
});
|
|
564
|
+
const wantedIds = new Set(wanted.map((entry) => entry.id));
|
|
565
|
+
const first = applied[0].change;
|
|
566
|
+
// What a rule or a model wrote before (`decided`) ends where the first entry starts.
|
|
567
|
+
// A row an earlier call closed for an entry that is no longer the first one is closed again where it belongs.
|
|
568
|
+
for (const stored of status()) {
|
|
569
|
+
if (wantedIds.has(stored.id) || stored.row.by === SIGNAL_CHANGES_RULE)
|
|
570
|
+
continue;
|
|
571
|
+
const closure = { on: later(stored.row.valid.from, first.day), by: first.src };
|
|
572
|
+
if (!isOpen(stored.row)) {
|
|
573
|
+
const closed = stored.row.closed;
|
|
574
|
+
if (!closed || !srcs.has(closed.by) || (closed.on === closure.on && closed.by === closure.by))
|
|
575
|
+
continue;
|
|
576
|
+
if (!reopen(target, audience, stored.id))
|
|
577
|
+
continue;
|
|
578
|
+
}
|
|
579
|
+
if (close(target, audience, stored.id, closure))
|
|
580
|
+
result.statusClosed += 1;
|
|
581
|
+
}
|
|
582
|
+
for (const { change, row, id } of wanted) {
|
|
583
|
+
const present = status().find((stored) => stored.id === id);
|
|
584
|
+
if (!present) {
|
|
585
|
+
queue(change, { entity: target, audience, row });
|
|
586
|
+
continue;
|
|
587
|
+
}
|
|
588
|
+
const same = present.row.valid.to === row.valid.to && present.row.closed?.on === row.closed?.on && present.row.closed?.by === row.closed?.by;
|
|
589
|
+
if (same)
|
|
590
|
+
continue;
|
|
591
|
+
if (!isOpen(present.row) && !reopen(target, audience, id))
|
|
592
|
+
continue;
|
|
593
|
+
if (row.closed && close(target, audience, id, row.closed))
|
|
594
|
+
result.statusClosed += 1;
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
// Rows the rule wrote for an entry that no longer applies are never left open. An entry its signal no longer
|
|
598
|
+
// states is taken out with its rows, so the graph holds what a build from the signals as they are now would
|
|
599
|
+
// hold. An entry that is still stated and is not applied (a person's status, a loop) keeps its rows, closed.
|
|
600
|
+
const appliedKeys = new Set(accepted.map((candidate) => candidate.key));
|
|
601
|
+
const remove = (ref, audience, id) => {
|
|
602
|
+
if (!store.removeRows || store.removeRows(ref, audience, [id]) === 0)
|
|
603
|
+
return false;
|
|
604
|
+
result.rowsRemoved += 1;
|
|
605
|
+
changedRefs.add(ref);
|
|
606
|
+
return true;
|
|
607
|
+
};
|
|
608
|
+
for (const { change, target } of withdrawnEntries) {
|
|
609
|
+
if (appliedKeys.has(signalChangeKey(change)))
|
|
610
|
+
continue;
|
|
611
|
+
const holder = holderOf(target, change.audience);
|
|
612
|
+
const closure = (from) => (holder ? { on: later(from, holder.row.valid.from), by: holder.row.src } : { on: from, by: change.src });
|
|
613
|
+
const gone = change.retracted === true && typeof store.removeRows === "function";
|
|
614
|
+
if (change.decision !== null && LINKING_KINDS.has(change.kind)) {
|
|
615
|
+
const edge = edgeOf(change, target);
|
|
616
|
+
if (gone) {
|
|
617
|
+
remove(change.decision, change.audience, rowId(edge));
|
|
618
|
+
const inverse = inverseEdge(schema, change.decision, edge);
|
|
619
|
+
if (inverse)
|
|
620
|
+
remove(inverse.entity, change.audience, rowId(inverse.row));
|
|
621
|
+
}
|
|
622
|
+
else {
|
|
623
|
+
closeEdge(change.decision, change.audience, edge, closure(edge.valid.from));
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
const value = CHANGE_STATUS[change.kind];
|
|
627
|
+
if (value) {
|
|
628
|
+
const row = statusOf(change, value);
|
|
629
|
+
if (gone)
|
|
630
|
+
remove(target, change.audience, rowId(row));
|
|
631
|
+
else if (close(target, change.audience, rowId(row), closure(row.valid.from)))
|
|
632
|
+
result.statusClosed += 1;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
// New rows, one ingest per entry under the signal's own source ref, so a row can be traced to the signal.
|
|
636
|
+
const markedBy = new Map();
|
|
637
|
+
for (const key of [...ingest.keys()].sort(byCode)) {
|
|
638
|
+
const { change, rows } = ingest.get(key);
|
|
639
|
+
// What the graph held before this entry is written: a row that is already there is not written again, and is not counted.
|
|
640
|
+
const newStatus = [];
|
|
641
|
+
let newEdge = false;
|
|
642
|
+
for (const row of rows) {
|
|
643
|
+
const id = rowId(row.row);
|
|
644
|
+
if (row.row.kind === "E") {
|
|
645
|
+
if (!store.readRows(row.entity, { audiences: [row.audience], kinds: ["E"] }).some((stored) => stored.id === id))
|
|
646
|
+
newEdge = true;
|
|
647
|
+
continue;
|
|
648
|
+
}
|
|
649
|
+
if (row.row.kind !== "F")
|
|
650
|
+
continue;
|
|
651
|
+
const value = row.row.value;
|
|
652
|
+
const held = store.readRows(row.entity, { kinds: ["F"] }).filter((stored) => stored.row.kind === "F" && stored.row.field === "status");
|
|
653
|
+
if (held.some((stored) => stored.audience === row.audience && stored.id === id))
|
|
654
|
+
continue;
|
|
655
|
+
// The rule's open status of the same value in another audience folder: the decision was counted when that row was written.
|
|
656
|
+
newStatus.push({ entity: row.entity, value, ruleHadIt: held.some((stored) => stored.row.by === SIGNAL_CHANGES_RULE && stored.row.value === value && isOpen(stored.row)) });
|
|
657
|
+
}
|
|
658
|
+
const applied = applyIngest(store, { input: change.src, date: change.day, entities: [], rows });
|
|
659
|
+
result.rowsAdded += applied.rowsAdded;
|
|
660
|
+
result.rowsClosed += applied.rowsClosed;
|
|
661
|
+
for (const row of rows) {
|
|
662
|
+
touched.add(row.entity);
|
|
663
|
+
if (row.row.kind === "E")
|
|
664
|
+
touched.add(row.row.target);
|
|
665
|
+
}
|
|
666
|
+
if (applied.rowsAdded > 0) {
|
|
667
|
+
if (newEdge)
|
|
668
|
+
result.links += 1;
|
|
669
|
+
for (const status of newStatus) {
|
|
670
|
+
result.statusWritten += 1;
|
|
671
|
+
if (status.ruleHadIt)
|
|
672
|
+
continue;
|
|
673
|
+
const marked = markedBy.get(status.value) ?? new Set();
|
|
674
|
+
markedBy.set(status.value, marked.add(status.entity));
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
for (const [value, marked] of markedBy)
|
|
679
|
+
result.statusBy[value] = marked.size;
|
|
680
|
+
if (changedRefs.size > 0)
|
|
681
|
+
store.markDirty([...changedRefs].sort(byCode));
|
|
682
|
+
for (const ref of changedRefs)
|
|
683
|
+
touched.add(ref);
|
|
684
|
+
result.touched = [...touched].sort();
|
|
685
|
+
return result;
|
|
686
|
+
}
|
|
687
|
+
//# sourceMappingURL=signal-changes.js.map
|