@indigoai-us/hq-cli 5.345.247 → 5.345.250
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 +95 -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 +1198 -0
- package/dist/command-catalog.generated.js +1476 -0
- package/dist/command-registration-plan.d.ts +6 -0
- package/dist/command-registration-plan.js +1 -0
- package/dist/commands/core-job-run-native.js +4 -0
- package/dist/commands/core-knowledge-prefs.d.ts +1 -1
- package/dist/commands/core-knowledge-prefs.js +1 -16
- package/dist/commands/core.js +0 -2
- package/dist/commands/daemon.js +21 -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 +63 -0
- package/dist/commands/ontology-graph.js +470 -0
- package/dist/commands/sync-core.js +6 -0
- package/dist/fast-scaffold.js +1 -3
- package/dist/hq-cloud-sync-runner/bin/sync-runner.js +281 -256
- package/dist/hq-cloud-sync-runner/package.json +1 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.js +48 -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/installed-cli-restart.d.ts +57 -0
- package/dist/lib/daemon/installed-cli-restart.js +114 -0
- 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/legacy-journal-gate.d.ts +32 -0
- package/dist/lib/daemon/legacy-journal-gate.js +102 -0
- package/dist/lib/daemon/machine-checks.d.ts +7 -0
- package/dist/lib/daemon/machine-checks.js +10 -0
- 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.d.ts +3 -1
- package/dist/lib/daemon/run.js +49 -6
- package/dist/lib/daemon/state-file.d.ts +1 -1
- package/dist/lib/daemon/state-file.js +1 -1
- 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-snapshot-file.d.ts +2 -0
- package/dist/lib/flag-snapshot-file.js +118 -48
- package/dist/lib/hook-engine/canary-gate.d.ts +57 -3
- package/dist/lib/hook-engine/canary-gate.js +138 -6
- package/dist/lib/hook-engine/coverage.d.ts +82 -13
- package/dist/lib/hook-engine/coverage.js +214 -18
- package/dist/lib/hook-engine/registration-identity.d.ts +5 -0
- package/dist/lib/hook-engine/registration-identity.js +1 -1
- package/dist/lib/hook-engine/resolve.d.ts +23 -0
- package/dist/lib/hook-engine/resolve.js +48 -1
- package/dist/lib/hook-engine/stable-heal.d.ts +87 -0
- package/dist/lib/hook-engine/stable-heal.js +214 -0
- package/dist/lib/hot-channels/graph-context.d.ts +105 -0
- package/dist/lib/hot-channels/graph-context.js +380 -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 +197 -0
- package/dist/lib/ontology-graph/model/step.js +526 -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 +67 -0
- package/dist/lib/ontology-graph/org/derive.js +359 -0
- package/dist/lib/ontology-graph/org/index.d.ts +102 -0
- package/dist/lib/ontology-graph/org/index.js +630 -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 +97 -0
- package/dist/lib/ontology-graph/org/text.js +321 -0
- package/dist/lib/ontology-graph/org/types.d.ts +457 -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 +144 -0
- package/dist/lib/ontology-graph/pages/render.js +688 -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 +254 -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 +803 -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 +49 -0
- package/dist/lib/ontology-graph/read/names.js +297 -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 +146 -0
- package/dist/lib/ontology-graph/replay/engine.js +1240 -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 +91 -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 +107 -0
- package/dist/lib/ontology-graph/schema/authority.js +186 -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 +1190 -0
- package/dist/lib/ontology-graph/types.js +131 -0
- package/dist/lib/secret-scan/gitleaks-rules.generated.js +1 -1
- package/dist/lib/sync-core/engine-holds.d.ts +1 -1
- package/dist/lib/sync-core/engine-holds.js +12 -2
- package/dist/lib/sync-core/hand-back.d.ts +2 -1
- package/dist/lib/sync-core/hand-back.js +16 -2
- package/dist/lib/sync-core/legacy-journal-export-child.d.ts +2 -0
- package/dist/lib/sync-core/legacy-journal-export-child.js +24 -0
- package/dist/lib/sync-core/legacy-journal-export.d.ts +39 -0
- package/dist/lib/sync-core/legacy-journal-export.js +241 -0
- package/dist/lib/sync-core/legacy-journal-reader.d.ts +15 -0
- package/dist/lib/sync-core/legacy-journal-reader.js +56 -0
- package/dist/lib/sync-core/reconcile.d.ts +3 -0
- package/dist/lib/sync-core/reconcile.js +43 -3
- package/dist/utils/cli-telemetry.d.ts +1 -0
- package/dist/utils/cli-telemetry.js +6 -3
- package/dist/utils/frontmatter.d.ts +14 -0
- package/dist/utils/frontmatter.js +18 -0
- package/dist/utils/hook-engine-rollup.d.ts +96 -0
- package/dist/utils/hook-engine-rollup.js +725 -0
- package/dist/utils/interactive-caller.js +1 -1
- package/package.json +9 -9
- package/scripts/install-hook-engine.mjs +4 -0
- package/assets/scaffold/core/scripts/knowledge-prefs.sh +0 -55
|
@@ -0,0 +1,652 @@
|
|
|
1
|
+
import { SHARED_MAIL_DOMAINS, compactKey, digitSignature, isEmail, jaccard, nameTokens, normalizeDomain, normalizeEmail, normalizeName, slackIdOf, sortedUnique, stripCompanySuffix, trigrams, } from "./normalize.js";
|
|
2
|
+
import { isGroupWord, notAPerson } from "./machine.js";
|
|
3
|
+
export const EMPTY_SLACK_DIRECTORY = { idToEmail: {}, idToName: {} };
|
|
4
|
+
/** A fuzzy candidate below the match threshold is kept for a proposal (or the model) from this score up. */
|
|
5
|
+
export const FUZZY_CANDIDATE_MIN = 0.6;
|
|
6
|
+
const MAX_CANDIDATES = 8;
|
|
7
|
+
/** Names that stand for "somebody", never for one person. */
|
|
8
|
+
const PLACEHOLDER_PERSON_RE = /^(?:unknown|unnamed|anonymous|user|member|teammate|someone|somebody|participant|speaker|guest|attendee|onboardee|new (?:team )?member|team member|hq team member)(?: |$)/;
|
|
9
|
+
function put(map, key, ref, entity) {
|
|
10
|
+
let set = map.get(key);
|
|
11
|
+
if (!set)
|
|
12
|
+
map.set(key, (set = new Set()));
|
|
13
|
+
if (!set.has(ref)) {
|
|
14
|
+
set.add(ref);
|
|
15
|
+
entity.filed.push({ map, key });
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function sortRefs(refs) {
|
|
19
|
+
return sortedUnique(refs);
|
|
20
|
+
}
|
|
21
|
+
/** `legacy:<64 hex>` and `legacy:<12 hex>` are the same key. */
|
|
22
|
+
function externalKey(value) {
|
|
23
|
+
const colon = value.indexOf(":");
|
|
24
|
+
if (colon < 1)
|
|
25
|
+
return "";
|
|
26
|
+
const system = value.slice(0, colon).trim().toLowerCase();
|
|
27
|
+
let id = value.slice(colon + 1).trim();
|
|
28
|
+
if (id === "")
|
|
29
|
+
return "";
|
|
30
|
+
if (system === "legacy")
|
|
31
|
+
id = id.toLowerCase().slice(0, 12);
|
|
32
|
+
if (system === "github")
|
|
33
|
+
id = id.toLowerCase();
|
|
34
|
+
return `${system}:${id}`;
|
|
35
|
+
}
|
|
36
|
+
function pairKey(mentionNorm, candidate) {
|
|
37
|
+
return `${mentionNorm}\u0000${candidate}`;
|
|
38
|
+
}
|
|
39
|
+
export class EntityIndex {
|
|
40
|
+
refusals;
|
|
41
|
+
generic;
|
|
42
|
+
titles;
|
|
43
|
+
titleWords;
|
|
44
|
+
titlePrefixes;
|
|
45
|
+
slack;
|
|
46
|
+
legacyPaths;
|
|
47
|
+
roster = null;
|
|
48
|
+
reserved = new Set();
|
|
49
|
+
machine = {};
|
|
50
|
+
descriptorWords = new Set();
|
|
51
|
+
byRef = new Map();
|
|
52
|
+
keyMap = new Map();
|
|
53
|
+
/** `<type>\0<normalized name>` */
|
|
54
|
+
nameByType = new Map();
|
|
55
|
+
nameAny = new Map();
|
|
56
|
+
aliasAny = new Map();
|
|
57
|
+
/** Non-person names with spaces removed, and company names without legal forms. */
|
|
58
|
+
looseAny = new Map();
|
|
59
|
+
/** First token of a person's canonical name. */
|
|
60
|
+
firstToken = new Map();
|
|
61
|
+
fuzzyEntries = [];
|
|
62
|
+
trigramPostings = new Map();
|
|
63
|
+
adjudications = new Map();
|
|
64
|
+
cache = new Map();
|
|
65
|
+
reasonCounts = new Map();
|
|
66
|
+
constructor(refusals, options = {}) {
|
|
67
|
+
this.refusals = refusals;
|
|
68
|
+
this.generic = new Set(refusals.generic_words.map((word) => normalizeName(word)).filter((word) => word !== ""));
|
|
69
|
+
const titles = refusals.job_titles.map((title) => normalizeName(title)).filter((title) => title !== "");
|
|
70
|
+
this.titles = new Set(titles);
|
|
71
|
+
this.titleWords = new Set(titles.flatMap((title) => nameTokens(title)));
|
|
72
|
+
this.titlePrefixes = titles.filter((title) => title.includes(" ")).map((title) => `${title} `);
|
|
73
|
+
this.slack = options.slackDirectory ?? EMPTY_SLACK_DIRECTORY;
|
|
74
|
+
this.legacyPaths = new Map(options.legacyPaths ?? []);
|
|
75
|
+
}
|
|
76
|
+
get size() {
|
|
77
|
+
return this.byRef.size;
|
|
78
|
+
}
|
|
79
|
+
has(ref) {
|
|
80
|
+
return this.byRef.has(ref);
|
|
81
|
+
}
|
|
82
|
+
get(ref) {
|
|
83
|
+
return this.byRef.get(ref);
|
|
84
|
+
}
|
|
85
|
+
refs() {
|
|
86
|
+
return sortRefs(this.byRef.keys());
|
|
87
|
+
}
|
|
88
|
+
setSlackDirectory(directory) {
|
|
89
|
+
this.slack = directory;
|
|
90
|
+
this.cache = new Map();
|
|
91
|
+
}
|
|
92
|
+
slackDirectory() {
|
|
93
|
+
return this.slack;
|
|
94
|
+
}
|
|
95
|
+
setLegacyPath(oldPath, ref) {
|
|
96
|
+
this.legacyPaths.set(oldPath, ref);
|
|
97
|
+
this.cache = new Map();
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Old `type/slug` paths of the entity files an import is about to read. A
|
|
101
|
+
* new entity never takes one of them as its id unless it is that file's own,
|
|
102
|
+
* so an old path never names two things.
|
|
103
|
+
*/
|
|
104
|
+
reservePaths(paths) {
|
|
105
|
+
for (const oldPath of paths)
|
|
106
|
+
this.reserved.add(oldPath);
|
|
107
|
+
}
|
|
108
|
+
isReserved(ref) {
|
|
109
|
+
return this.reserved.has(ref);
|
|
110
|
+
}
|
|
111
|
+
/** The company's own people, as a record states them. Null until a seed or a kept roster file sets it. */
|
|
112
|
+
setRoster(roster) {
|
|
113
|
+
this.roster = roster;
|
|
114
|
+
this.cache = new Map();
|
|
115
|
+
}
|
|
116
|
+
rosterOf() {
|
|
117
|
+
return this.roster;
|
|
118
|
+
}
|
|
119
|
+
/** Names that are bots or agents in this company, and the company's own words. */
|
|
120
|
+
setMachineCheck(check, descriptorWords = []) {
|
|
121
|
+
this.machine = check;
|
|
122
|
+
this.descriptorWords = new Set(descriptorWords);
|
|
123
|
+
this.cache = new Map();
|
|
124
|
+
}
|
|
125
|
+
machineCheck() {
|
|
126
|
+
return this.machine;
|
|
127
|
+
}
|
|
128
|
+
isTitleWord(word) {
|
|
129
|
+
return this.titleWords.has(word);
|
|
130
|
+
}
|
|
131
|
+
/** Why this candidate is not a person (a bot, a service, a group name), or null. */
|
|
132
|
+
notAPerson(candidate) {
|
|
133
|
+
return notAPerson(candidate, this.machine, (word) => this.titleWords.has(word));
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* The one roster person this name is a variant of (a first name, a
|
|
137
|
+
* misspelling, a cut-short or longer form), with the reason code. Null with
|
|
138
|
+
* no roster, with two roster people that fit, or when the keys contradict.
|
|
139
|
+
*/
|
|
140
|
+
rosterVariant(name, keys) {
|
|
141
|
+
const roster = this.roster;
|
|
142
|
+
if (!roster || roster.size === 0)
|
|
143
|
+
return null;
|
|
144
|
+
return roster.match(name, keys, {
|
|
145
|
+
isDescriptor: (word) => this.titleWords.has(word) || this.descriptorWords.has(word) || isGroupWord(word) || (this.machine.ownWords?.has(word) ?? false),
|
|
146
|
+
firstNameTaken: (word) => [...(this.firstToken.get(word) ?? [])].some((ref) => !roster.has(ref) && (this.byRef.get(ref)?.tokens.length ?? 0) >= 2),
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
/** Adds an entity, or re-indexes it when it is already known (new aliases or keys). */
|
|
150
|
+
add(entity, audiences) {
|
|
151
|
+
const previous = this.byRef.get(entity.id);
|
|
152
|
+
const keptAudiences = previous?.audiences ?? null;
|
|
153
|
+
if (previous)
|
|
154
|
+
this.remove(entity.id);
|
|
155
|
+
const norm = normalizeName(entity.canonical_name);
|
|
156
|
+
const indexed = {
|
|
157
|
+
ref: entity.id,
|
|
158
|
+
type: entity.type,
|
|
159
|
+
name: entity.canonical_name,
|
|
160
|
+
norm,
|
|
161
|
+
tokens: nameTokens(norm),
|
|
162
|
+
aliasNorms: [],
|
|
163
|
+
emails: new Set(),
|
|
164
|
+
slackIds: new Set(),
|
|
165
|
+
externalIds: new Set(),
|
|
166
|
+
domains: new Set(),
|
|
167
|
+
audiences: audiences ? new Set(audiences) : keptAudiences,
|
|
168
|
+
filed: [],
|
|
169
|
+
fuzzyEntries: [],
|
|
170
|
+
};
|
|
171
|
+
this.byRef.set(entity.id, indexed);
|
|
172
|
+
if (norm !== "") {
|
|
173
|
+
put(this.nameByType, `${entity.type}\u0000${norm}`, entity.id, indexed);
|
|
174
|
+
put(this.nameAny, norm, entity.id, indexed);
|
|
175
|
+
this.fileLoose(indexed, norm);
|
|
176
|
+
if (entity.type === "person" && indexed.tokens.length > 0)
|
|
177
|
+
put(this.firstToken, indexed.tokens[0], entity.id, indexed);
|
|
178
|
+
this.fileFuzzy(indexed, norm);
|
|
179
|
+
}
|
|
180
|
+
for (const alias of entity.aliases ?? []) {
|
|
181
|
+
const aliasNorm = normalizeName(alias);
|
|
182
|
+
if (aliasNorm === "" || aliasNorm === norm)
|
|
183
|
+
continue;
|
|
184
|
+
indexed.aliasNorms.push(aliasNorm);
|
|
185
|
+
put(this.aliasAny, aliasNorm, entity.id, indexed);
|
|
186
|
+
if (nameTokens(aliasNorm).length >= 2)
|
|
187
|
+
this.fileFuzzy(indexed, aliasNorm);
|
|
188
|
+
}
|
|
189
|
+
for (const email of entity.keys?.email ?? []) {
|
|
190
|
+
const key = normalizeEmail(email);
|
|
191
|
+
if (!isEmail(key))
|
|
192
|
+
continue;
|
|
193
|
+
indexed.emails.add(key);
|
|
194
|
+
put(this.keyMap, `email:${key}`, entity.id, indexed);
|
|
195
|
+
}
|
|
196
|
+
for (const id of entity.keys?.slack_id ?? []) {
|
|
197
|
+
const slackId = slackIdOf(id);
|
|
198
|
+
if (!slackId)
|
|
199
|
+
continue;
|
|
200
|
+
indexed.slackIds.add(slackId);
|
|
201
|
+
put(this.keyMap, `slack:${slackId}`, entity.id, indexed);
|
|
202
|
+
}
|
|
203
|
+
for (const external of entity.keys?.external_ids ?? []) {
|
|
204
|
+
const key = externalKey(external);
|
|
205
|
+
if (key === "")
|
|
206
|
+
continue;
|
|
207
|
+
indexed.externalIds.add(key);
|
|
208
|
+
put(this.keyMap, `ext:${key}`, entity.id, indexed);
|
|
209
|
+
}
|
|
210
|
+
for (const legacyId of entity.legacy_ids ?? []) {
|
|
211
|
+
const key = externalKey(`legacy:${legacyId}`);
|
|
212
|
+
if (key !== "")
|
|
213
|
+
put(this.keyMap, `ext:${key}`, entity.id, indexed);
|
|
214
|
+
}
|
|
215
|
+
for (const domain of entity.keys?.web_domains ?? []) {
|
|
216
|
+
const host = normalizeDomain(domain);
|
|
217
|
+
if (host === "" || SHARED_MAIL_DOMAINS.has(host))
|
|
218
|
+
continue;
|
|
219
|
+
indexed.domains.add(host);
|
|
220
|
+
put(this.keyMap, `domain:${host}`, entity.id, indexed);
|
|
221
|
+
}
|
|
222
|
+
this.cache = new Map();
|
|
223
|
+
}
|
|
224
|
+
remove(ref) {
|
|
225
|
+
const entity = this.byRef.get(ref);
|
|
226
|
+
if (!entity)
|
|
227
|
+
return;
|
|
228
|
+
for (const { map, key } of entity.filed) {
|
|
229
|
+
const set = map.get(key);
|
|
230
|
+
if (!set)
|
|
231
|
+
continue;
|
|
232
|
+
set.delete(ref);
|
|
233
|
+
if (set.size === 0)
|
|
234
|
+
map.delete(key);
|
|
235
|
+
}
|
|
236
|
+
for (const entry of entity.fuzzyEntries)
|
|
237
|
+
this.fuzzyEntries[entry].ref = null;
|
|
238
|
+
this.byRef.delete(ref);
|
|
239
|
+
this.cache = new Map();
|
|
240
|
+
}
|
|
241
|
+
/** Records that the entity has rows in this audience (for the audience filter). */
|
|
242
|
+
noteAudience(ref, audience) {
|
|
243
|
+
const entity = this.byRef.get(ref);
|
|
244
|
+
if (!entity)
|
|
245
|
+
return;
|
|
246
|
+
if (!entity.audiences)
|
|
247
|
+
entity.audiences = new Set();
|
|
248
|
+
if (!entity.audiences.has(audience)) {
|
|
249
|
+
entity.audiences.add(audience);
|
|
250
|
+
this.cache = new Map();
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
fileLoose(entity, norm) {
|
|
254
|
+
if (entity.type === "person")
|
|
255
|
+
return;
|
|
256
|
+
const compact = compactKey(norm);
|
|
257
|
+
if (compact !== norm)
|
|
258
|
+
put(this.looseAny, `c:${compact}`, entity.ref, entity);
|
|
259
|
+
else
|
|
260
|
+
put(this.looseAny, `c:${norm}`, entity.ref, entity);
|
|
261
|
+
if (entity.type === "company") {
|
|
262
|
+
const bare = stripCompanySuffix(norm);
|
|
263
|
+
put(this.looseAny, `co:${bare}`, entity.ref, entity);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
fuzzyEligible(norm) {
|
|
267
|
+
return (compactKey(norm).length >= this.refusals.fuzzy_min_chars &&
|
|
268
|
+
nameTokens(norm).length >= this.refusals.fuzzy_min_tokens);
|
|
269
|
+
}
|
|
270
|
+
fileFuzzy(entity, norm) {
|
|
271
|
+
if (!this.fuzzyEligible(norm))
|
|
272
|
+
return;
|
|
273
|
+
const grams = trigrams(norm);
|
|
274
|
+
const entry = this.fuzzyEntries.length;
|
|
275
|
+
this.fuzzyEntries.push({ ref: entity.ref, type: entity.type, grams: new Set(grams), digits: digitSignature(norm) });
|
|
276
|
+
entity.fuzzyEntries.push(entry);
|
|
277
|
+
for (const gram of grams) {
|
|
278
|
+
const list = this.trigramPostings.get(gram);
|
|
279
|
+
if (list)
|
|
280
|
+
list.push(entry);
|
|
281
|
+
else
|
|
282
|
+
this.trigramPostings.set(gram, [entry]);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
// -------------------------------------------------------------------------
|
|
286
|
+
// Refusal rules
|
|
287
|
+
// -------------------------------------------------------------------------
|
|
288
|
+
isGeneric(norm) {
|
|
289
|
+
return this.generic.has(norm);
|
|
290
|
+
}
|
|
291
|
+
isPlaceholderPerson(norm) {
|
|
292
|
+
return PLACEHOLDER_PERSON_RE.test(norm);
|
|
293
|
+
}
|
|
294
|
+
/** `CEO`, `Founder & CEO`, `Head of Sales`, `Kevin's CEO`. */
|
|
295
|
+
isJobTitle(text) {
|
|
296
|
+
const norm = normalizeName(text);
|
|
297
|
+
if (norm === "")
|
|
298
|
+
return false;
|
|
299
|
+
if (this.titles.has(norm))
|
|
300
|
+
return true;
|
|
301
|
+
if (this.titlePrefixes.some((prefix) => norm.startsWith(prefix)))
|
|
302
|
+
return true;
|
|
303
|
+
const tokens = nameTokens(norm);
|
|
304
|
+
const filler = new Set(["and", "of", "the", "a"]);
|
|
305
|
+
if (tokens.some((token) => this.titleWords.has(token)) && tokens.every((token) => this.titleWords.has(token) || filler.has(token))) {
|
|
306
|
+
return true;
|
|
307
|
+
}
|
|
308
|
+
const possessive = /^\S+['’]s\s+(.+)$/.exec(text.trim());
|
|
309
|
+
if (possessive) {
|
|
310
|
+
const rest = normalizeName(possessive[1]);
|
|
311
|
+
if (this.titles.has(rest) || this.titlePrefixes.some((prefix) => rest.startsWith(prefix)))
|
|
312
|
+
return true;
|
|
313
|
+
}
|
|
314
|
+
return false;
|
|
315
|
+
}
|
|
316
|
+
isTooShort(norm) {
|
|
317
|
+
return compactKey(norm).length < this.refusals.min_name_length;
|
|
318
|
+
}
|
|
319
|
+
// -------------------------------------------------------------------------
|
|
320
|
+
// Lookups
|
|
321
|
+
// -------------------------------------------------------------------------
|
|
322
|
+
filter(refs, types, audiences) {
|
|
323
|
+
if (!refs)
|
|
324
|
+
return [];
|
|
325
|
+
const out = [];
|
|
326
|
+
for (const ref of refs) {
|
|
327
|
+
const entity = this.byRef.get(ref);
|
|
328
|
+
if (!entity)
|
|
329
|
+
continue;
|
|
330
|
+
if (types && !types.includes(entity.type))
|
|
331
|
+
continue;
|
|
332
|
+
if (audiences && entity.audiences && !audiences.some((audience) => entity.audiences.has(audience)))
|
|
333
|
+
continue;
|
|
334
|
+
out.push(ref);
|
|
335
|
+
}
|
|
336
|
+
return sortRefs(out);
|
|
337
|
+
}
|
|
338
|
+
/** Entities that hold any of these identity keys. */
|
|
339
|
+
byKeys(keys, types, audiences) {
|
|
340
|
+
if (!keys)
|
|
341
|
+
return [];
|
|
342
|
+
const hits = new Set();
|
|
343
|
+
const collect = (key) => {
|
|
344
|
+
for (const ref of this.keyMap.get(key) ?? [])
|
|
345
|
+
hits.add(ref);
|
|
346
|
+
};
|
|
347
|
+
for (const email of keys.email ?? [])
|
|
348
|
+
collect(`email:${normalizeEmail(email)}`);
|
|
349
|
+
for (const id of keys.slack_id ?? []) {
|
|
350
|
+
const slackId = slackIdOf(id);
|
|
351
|
+
if (slackId)
|
|
352
|
+
collect(`slack:${slackId}`);
|
|
353
|
+
}
|
|
354
|
+
for (const external of keys.external_ids ?? []) {
|
|
355
|
+
const key = externalKey(external);
|
|
356
|
+
if (key !== "")
|
|
357
|
+
collect(`ext:${key}`);
|
|
358
|
+
}
|
|
359
|
+
for (const domain of keys.web_domains ?? []) {
|
|
360
|
+
const host = normalizeDomain(domain);
|
|
361
|
+
if (host !== "" && !SHARED_MAIL_DOMAINS.has(host))
|
|
362
|
+
collect(`domain:${host}`);
|
|
363
|
+
}
|
|
364
|
+
return this.filter(hits, types, audiences);
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Entities of one type whose canonical name is the same text after
|
|
368
|
+
* normalization (for non-person types also with spaces or a legal form
|
|
369
|
+
* removed). Ignores the refusal lists: it answers "is this the same label",
|
|
370
|
+
* not "may this mention be linked".
|
|
371
|
+
*/
|
|
372
|
+
findSameName(type, name) {
|
|
373
|
+
const norm = normalizeName(name);
|
|
374
|
+
if (norm === "")
|
|
375
|
+
return [];
|
|
376
|
+
const exact = this.filter(this.nameByType.get(`${type}\u0000${norm}`), [type], undefined);
|
|
377
|
+
if (exact.length > 0 || type === "person")
|
|
378
|
+
return exact;
|
|
379
|
+
return this.loose(norm, [type], undefined);
|
|
380
|
+
}
|
|
381
|
+
loose(norm, types, audiences) {
|
|
382
|
+
const hits = new Set(this.looseAny.get(`c:${compactKey(norm)}`) ?? []);
|
|
383
|
+
for (const ref of this.looseAny.get(`co:${stripCompanySuffix(norm)}`) ?? [])
|
|
384
|
+
hits.add(ref);
|
|
385
|
+
return this.filter(hits, types, audiences);
|
|
386
|
+
}
|
|
387
|
+
/** True when both sides know an email and none is shared. Two such people are never merged automatically. */
|
|
388
|
+
emailsConflict(ref, emails) {
|
|
389
|
+
const entity = this.byRef.get(ref);
|
|
390
|
+
if (!entity || entity.type !== "person" || entity.emails.size === 0)
|
|
391
|
+
return false;
|
|
392
|
+
const mine = (emails ?? []).map((email) => normalizeEmail(email)).filter((email) => isEmail(email));
|
|
393
|
+
if (mine.length === 0)
|
|
394
|
+
return false;
|
|
395
|
+
return !mine.some((email) => entity.emails.has(email));
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* True when both sides carry a record id from the same system and none is
|
|
399
|
+
* shared, or both carry a web domain and none is shared. Two such entities
|
|
400
|
+
* with the same name are two records, and are never merged automatically.
|
|
401
|
+
*/
|
|
402
|
+
recordsConflict(ref, keys) {
|
|
403
|
+
const entity = this.byRef.get(ref);
|
|
404
|
+
if (!entity || !keys)
|
|
405
|
+
return false;
|
|
406
|
+
const mine = new Map();
|
|
407
|
+
for (const external of keys.external_ids ?? []) {
|
|
408
|
+
const key = externalKey(external);
|
|
409
|
+
if (key === "" || key.startsWith("legacy:"))
|
|
410
|
+
continue;
|
|
411
|
+
const system = key.slice(0, key.indexOf(":"));
|
|
412
|
+
mine.set(system, (mine.get(system) ?? new Set()).add(key));
|
|
413
|
+
}
|
|
414
|
+
for (const [system, ids] of mine) {
|
|
415
|
+
const theirs = [...entity.externalIds].filter((key) => key.startsWith(`${system}:`));
|
|
416
|
+
if (theirs.length > 0 && !theirs.some((key) => ids.has(key)))
|
|
417
|
+
return true;
|
|
418
|
+
}
|
|
419
|
+
const domains = (keys.web_domains ?? []).map((domain) => normalizeDomain(domain)).filter((host) => host !== "" && !SHARED_MAIL_DOMAINS.has(host));
|
|
420
|
+
if (domains.length > 0 && entity.domains.size > 0 && !domains.some((host) => entity.domains.has(host)))
|
|
421
|
+
return true;
|
|
422
|
+
return false;
|
|
423
|
+
}
|
|
424
|
+
fuzzy(norm, types, audiences) {
|
|
425
|
+
if (!this.fuzzyEligible(norm))
|
|
426
|
+
return [];
|
|
427
|
+
const grams = trigrams(norm);
|
|
428
|
+
const digits = digitSignature(norm);
|
|
429
|
+
// Prefix filter: a name that shares at least FUZZY_CANDIDATE_MIN of the mention's trigrams must hold one
|
|
430
|
+
// of its rarest few, so only those posting lists are walked.
|
|
431
|
+
const postings = grams
|
|
432
|
+
.map((gram) => this.trigramPostings.get(gram))
|
|
433
|
+
.filter((posting) => posting !== undefined)
|
|
434
|
+
.sort((a, b) => a.length - b.length);
|
|
435
|
+
const needed = Math.ceil(FUZZY_CANDIDATE_MIN * grams.length);
|
|
436
|
+
const probes = grams.length - needed + 1 - (grams.length - postings.length);
|
|
437
|
+
const candidates = new Set();
|
|
438
|
+
for (let n = 0; n < probes && n < postings.length; n += 1)
|
|
439
|
+
for (const entry of postings[n])
|
|
440
|
+
candidates.add(entry);
|
|
441
|
+
const best = new Map();
|
|
442
|
+
for (const entryIndex of candidates) {
|
|
443
|
+
const entry = this.fuzzyEntries[entryIndex];
|
|
444
|
+
if (!entry.ref || entry.digits !== digits)
|
|
445
|
+
continue;
|
|
446
|
+
if (types && !types.includes(entry.type))
|
|
447
|
+
continue;
|
|
448
|
+
let count = 0;
|
|
449
|
+
for (const gram of grams)
|
|
450
|
+
if (entry.grams.has(gram))
|
|
451
|
+
count += 1;
|
|
452
|
+
const score = jaccard(grams.length, entry.grams.size, count);
|
|
453
|
+
if (score < FUZZY_CANDIDATE_MIN)
|
|
454
|
+
continue;
|
|
455
|
+
if ((best.get(entry.ref) ?? 0) < score)
|
|
456
|
+
best.set(entry.ref, score);
|
|
457
|
+
}
|
|
458
|
+
const allowed = new Set(this.filter(best.keys(), types, audiences));
|
|
459
|
+
return [...best]
|
|
460
|
+
.filter(([ref]) => allowed.has(ref))
|
|
461
|
+
.map(([ref, score]) => ({ ref, score }))
|
|
462
|
+
.sort((a, b) => b.score - a.score || (a.ref < b.ref ? -1 : a.ref > b.ref ? 1 : 0));
|
|
463
|
+
}
|
|
464
|
+
// -------------------------------------------------------------------------
|
|
465
|
+
// Fuzzy adjudication (the hook for the model step)
|
|
466
|
+
// -------------------------------------------------------------------------
|
|
467
|
+
/** Records an answer for one fuzzy pair. `same: false` removes the candidate for that mention. */
|
|
468
|
+
setAdjudication(mention, candidate, same) {
|
|
469
|
+
this.adjudications.set(pairKey(normalizeName(mention), candidate), same);
|
|
470
|
+
this.cache = new Map();
|
|
471
|
+
}
|
|
472
|
+
/** Fuzzy pairs for this mention that are below the match threshold and have no answer yet. */
|
|
473
|
+
pendingFuzzyPairs(mention) {
|
|
474
|
+
const resolution = this.resolve(mention);
|
|
475
|
+
if (resolution.status !== "ambiguous" || resolution.reason !== "fuzzy")
|
|
476
|
+
return [];
|
|
477
|
+
const norm = normalizeName(mention.text);
|
|
478
|
+
const scores = new Map(this.fuzzy(norm, mention.types, mention.audiences).map((hit) => [hit.ref, hit.score]));
|
|
479
|
+
return resolution.candidates
|
|
480
|
+
.filter((candidate) => !this.adjudications.has(pairKey(norm, candidate)))
|
|
481
|
+
.map((candidate) => ({
|
|
482
|
+
mention: norm,
|
|
483
|
+
candidate,
|
|
484
|
+
candidateName: this.byRef.get(candidate)?.name ?? candidate,
|
|
485
|
+
score: scores.get(candidate) ?? 0,
|
|
486
|
+
}));
|
|
487
|
+
}
|
|
488
|
+
// -------------------------------------------------------------------------
|
|
489
|
+
// Resolve
|
|
490
|
+
// -------------------------------------------------------------------------
|
|
491
|
+
reasonTotals() {
|
|
492
|
+
return Object.fromEntries([...this.reasonCounts].sort(([a], [b]) => (a < b ? -1 : 1)));
|
|
493
|
+
}
|
|
494
|
+
resolve(mention) {
|
|
495
|
+
const cacheKey = JSON.stringify([mention.text, mention.types ?? null, mention.keys ?? null, mention.audiences ?? null]);
|
|
496
|
+
let resolution = this.cache.get(cacheKey);
|
|
497
|
+
if (!resolution) {
|
|
498
|
+
resolution = this.resolveUncached(mention);
|
|
499
|
+
this.cache.set(cacheKey, resolution);
|
|
500
|
+
}
|
|
501
|
+
this.reasonCounts.set(resolution.reason, (this.reasonCounts.get(resolution.reason) ?? 0) + 1);
|
|
502
|
+
return { ...resolution, candidates: [...resolution.candidates] };
|
|
503
|
+
}
|
|
504
|
+
resolveUncached(mention) {
|
|
505
|
+
const text = (mention.text ?? "").trim();
|
|
506
|
+
const types = mention.types && mention.types.length > 0 ? mention.types : undefined;
|
|
507
|
+
const audiences = mention.audiences && mention.audiences.length > 0 ? mention.audiences : undefined;
|
|
508
|
+
const done = (entity, reason, score) => ({
|
|
509
|
+
status: "resolved",
|
|
510
|
+
entity,
|
|
511
|
+
reason,
|
|
512
|
+
candidates: [entity],
|
|
513
|
+
score,
|
|
514
|
+
});
|
|
515
|
+
const many = (candidates, reason, score, proposeMerge = false) => ({
|
|
516
|
+
status: "ambiguous",
|
|
517
|
+
reason,
|
|
518
|
+
candidates: candidates.slice(0, MAX_CANDIDATES),
|
|
519
|
+
score,
|
|
520
|
+
...(proposeMerge ? { proposeMerge: true } : {}),
|
|
521
|
+
});
|
|
522
|
+
const none = (status, reason) => ({
|
|
523
|
+
status,
|
|
524
|
+
reason,
|
|
525
|
+
candidates: [],
|
|
526
|
+
score: 0,
|
|
527
|
+
});
|
|
528
|
+
// An entity id, or the old path of a legacy entity file, names its entity directly.
|
|
529
|
+
const asPath = text.replace(/^ontology\/entities\//, "").replace(/\.md$/, "");
|
|
530
|
+
if (/^[a-z][a-z-]*\/\S+$/.test(asPath)) {
|
|
531
|
+
const direct = this.byRef.has(asPath) ? asPath : this.legacyPaths.get(asPath);
|
|
532
|
+
if (direct && this.filter([direct], types, audiences).length === 1)
|
|
533
|
+
return done(direct, "identity_key", 1);
|
|
534
|
+
}
|
|
535
|
+
// Pass 1: identity keys, from the mention and from the text itself.
|
|
536
|
+
const slackId = slackIdOf(text);
|
|
537
|
+
const keys = {
|
|
538
|
+
email: [...(mention.keys?.email ?? []), ...(isEmail(text) ? [text] : [])],
|
|
539
|
+
slack_id: [...(mention.keys?.slack_id ?? []), ...(slackId ? [slackId] : [])],
|
|
540
|
+
external_ids: [...(mention.keys?.external_ids ?? [])],
|
|
541
|
+
web_domains: [...(mention.keys?.web_domains ?? [])],
|
|
542
|
+
};
|
|
543
|
+
const keyed = this.byKeys(keys, types, audiences);
|
|
544
|
+
if (keyed.length === 1)
|
|
545
|
+
return done(keyed[0], "identity_key", 1);
|
|
546
|
+
if (keyed.length > 1)
|
|
547
|
+
return many(keyed, "ambiguous_name", 1, true);
|
|
548
|
+
// A Slack id is never a name. It resolves through the id-to-email and
|
|
549
|
+
// id-to-name maps, or it is parked.
|
|
550
|
+
if (slackId) {
|
|
551
|
+
const email = this.slack.idToEmail[slackId];
|
|
552
|
+
if (email) {
|
|
553
|
+
const viaEmail = this.byKeys({ email: [email] }, types ?? ["person"], audiences);
|
|
554
|
+
if (viaEmail.length === 1)
|
|
555
|
+
return done(viaEmail[0], "identity_key", 1);
|
|
556
|
+
}
|
|
557
|
+
const realName = this.slack.idToName[slackId];
|
|
558
|
+
if (realName && !slackIdOf(realName)) {
|
|
559
|
+
const viaName = this.resolveUncached({
|
|
560
|
+
text: realName,
|
|
561
|
+
types: ["person"],
|
|
562
|
+
...(email ? { keys: { email: [email] } } : {}),
|
|
563
|
+
...(audiences ? { audiences } : {}),
|
|
564
|
+
});
|
|
565
|
+
if (viaName.status === "resolved" && (viaName.reason === "exact_name" || viaName.reason === "alias"))
|
|
566
|
+
return viaName;
|
|
567
|
+
if (viaName.status === "ambiguous")
|
|
568
|
+
return { ...viaName, reason: "unknown_slack_id", status: "unresolved" };
|
|
569
|
+
}
|
|
570
|
+
return none("unresolved", "unknown_slack_id");
|
|
571
|
+
}
|
|
572
|
+
if (isEmail(text))
|
|
573
|
+
return none("unresolved", "no_match");
|
|
574
|
+
// Refusal rules.
|
|
575
|
+
const norm = normalizeName(text);
|
|
576
|
+
const personAllowed = !types || types.includes("person");
|
|
577
|
+
if (norm === "")
|
|
578
|
+
return none("refused", "refused_short");
|
|
579
|
+
if (personAllowed && this.isJobTitle(text))
|
|
580
|
+
return none("refused", "refused_job_title");
|
|
581
|
+
if (this.isGeneric(norm))
|
|
582
|
+
return none("refused", "refused_generic");
|
|
583
|
+
if (personAllowed && this.isPlaceholderPerson(norm) && (!types || types.length === 1))
|
|
584
|
+
return none("refused", "refused_generic");
|
|
585
|
+
if (this.isTooShort(norm))
|
|
586
|
+
return none("refused", "refused_short");
|
|
587
|
+
const tokens = nameTokens(norm);
|
|
588
|
+
const exact = this.filter(this.nameAny.get(norm), types, audiences);
|
|
589
|
+
const alias = this.filter(this.aliasAny.get(norm), types, audiences).filter((ref) => !exact.includes(ref));
|
|
590
|
+
// A first name that exactly one of the company's own people has is that person, whoever else shares it.
|
|
591
|
+
const variant = personAllowed && this.roster ? this.rosterVariant(text, keys) : null;
|
|
592
|
+
const variantAllowed = variant !== null && this.filter([variant.ref], types, audiences).length === 1;
|
|
593
|
+
if (variant && variantAllowed && variant.reason === "roster_first_name")
|
|
594
|
+
return done(variant.ref, "alias", 0.9);
|
|
595
|
+
// A single first name shared by two people is never linked.
|
|
596
|
+
if (tokens.length === 1 && personAllowed) {
|
|
597
|
+
const people = new Set(this.filter(this.firstToken.get(norm), ["person"], audiences));
|
|
598
|
+
for (const ref of alias)
|
|
599
|
+
if (this.byRef.get(ref)?.type === "person")
|
|
600
|
+
people.add(ref);
|
|
601
|
+
if (people.size >= 2) {
|
|
602
|
+
const others = [...exact, ...alias].filter((ref) => !people.has(ref));
|
|
603
|
+
return many(sortRefs([...people, ...others]), "ambiguous_first_word", 0.5);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
// Pass 2: exact normalized name, then alias.
|
|
607
|
+
const named = exact.length > 0 ? exact : this.loose(norm, types, audiences);
|
|
608
|
+
if (named.length === 1) {
|
|
609
|
+
if (this.emailsConflict(named[0], keys.email))
|
|
610
|
+
return many(named, "exact_name", 1, true);
|
|
611
|
+
return done(named[0], "exact_name", exact.length > 0 ? 1 : 0.98);
|
|
612
|
+
}
|
|
613
|
+
if (named.length > 1)
|
|
614
|
+
return many(named, "ambiguous_name", 1);
|
|
615
|
+
if (alias.length === 1) {
|
|
616
|
+
if (this.emailsConflict(alias[0], keys.email))
|
|
617
|
+
return many(alias, "alias", 0.95, true);
|
|
618
|
+
return done(alias[0], "alias", 0.95);
|
|
619
|
+
}
|
|
620
|
+
if (alias.length > 1)
|
|
621
|
+
return many(alias, "ambiguous_name", 0.95);
|
|
622
|
+
// A misspelled, cut-short or longer form of one roster person's name, when nothing carries the name itself.
|
|
623
|
+
if (variant && variantAllowed)
|
|
624
|
+
return done(variant.ref, "alias", 0.85);
|
|
625
|
+
// One person carries this first name, but not as a name or alias of their own.
|
|
626
|
+
if (tokens.length === 1 && personAllowed) {
|
|
627
|
+
const people = this.filter(this.firstToken.get(norm), ["person"], audiences);
|
|
628
|
+
if (people.length === 1)
|
|
629
|
+
return many(people, "ambiguous_first_word", 0.5);
|
|
630
|
+
}
|
|
631
|
+
// Pass 3: fuzzy.
|
|
632
|
+
const threshold = this.refusals.fuzzy_min_jaccard;
|
|
633
|
+
const hits = [];
|
|
634
|
+
for (const hit of this.fuzzy(norm, types, audiences)) {
|
|
635
|
+
const answer = this.adjudications.get(pairKey(norm, hit.ref));
|
|
636
|
+
if (answer === true)
|
|
637
|
+
return done(hit.ref, "fuzzy_adjudicated", hit.score);
|
|
638
|
+
if (answer === false)
|
|
639
|
+
continue;
|
|
640
|
+
hits.push(hit);
|
|
641
|
+
}
|
|
642
|
+
if (hits.length > 0) {
|
|
643
|
+
const top = hits[0];
|
|
644
|
+
const clear = top.score >= threshold && (hits.length === 1 || hits[1].score < threshold);
|
|
645
|
+
if (clear && !this.emailsConflict(top.ref, keys.email))
|
|
646
|
+
return done(top.ref, "fuzzy", top.score);
|
|
647
|
+
return many(hits.map((hit) => hit.ref), "fuzzy", top.score, true);
|
|
648
|
+
}
|
|
649
|
+
return none("unresolved", "no_match");
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
//# sourceMappingURL=entity-index.js.map
|