fullstackgtm 0.33.0 → 0.37.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +102 -0
- package/README.md +25 -0
- package/dist/acquireMeter.d.ts +67 -0
- package/dist/acquireMeter.js +145 -0
- package/dist/acquireSeen.d.ts +5 -0
- package/dist/acquireSeen.js +54 -0
- package/dist/cli.js +732 -21
- package/dist/connectors/hubspot.js +135 -0
- package/dist/connectors/prospectSources.d.ts +91 -0
- package/dist/connectors/prospectSources.js +227 -0
- package/dist/enrich.d.ts +87 -0
- package/dist/enrich.js +188 -4
- package/dist/format.d.ts +3 -1
- package/dist/format.js +14 -2
- package/dist/health.d.ts +71 -0
- package/dist/health.js +172 -0
- package/dist/icp.d.ts +96 -0
- package/dist/icp.js +256 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/mappings.js +3 -0
- package/dist/marketSourcing.d.ts +25 -0
- package/dist/marketSourcing.js +82 -0
- package/dist/types.d.ts +17 -1
- package/docs/api.md +29 -2
- package/docs/architecture.md +8 -1
- package/docs/dx-punch-list.md +87 -0
- package/llms.txt +16 -0
- package/package.json +1 -1
- package/skills/fullstackgtm/SKILL.md +2 -1
- package/src/acquireMeter.ts +186 -0
- package/src/acquireSeen.ts +57 -0
- package/src/cli.ts +870 -20
- package/src/connectors/hubspot.ts +140 -0
- package/src/connectors/prospectSources.ts +324 -0
- package/src/enrich.ts +275 -3
- package/src/format.ts +20 -2
- package/src/health.ts +238 -0
- package/src/icp.ts +313 -0
- package/src/index.ts +11 -0
- package/src/mappings.ts +3 -0
- package/src/marketSourcing.ts +104 -0
- package/src/types.ts +24 -0
package/src/enrich.ts
CHANGED
|
@@ -2,8 +2,10 @@ import { mkdirSync, readFileSync, readdirSync } from "node:fs";
|
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import { credentialsDir, ensureSecureHomeDir, writeSecureFile } from "./credentials.ts";
|
|
4
4
|
import { HUBSPOT_DEFAULT_FIELD_MAPPINGS } from "./mappings.ts";
|
|
5
|
+
import type { AcquireBudget } from "./acquireMeter.ts";
|
|
5
6
|
import type {
|
|
6
7
|
CanonicalGtmSnapshot,
|
|
8
|
+
CreateRecordPayload,
|
|
7
9
|
GtmEvidence,
|
|
8
10
|
PatchOperation,
|
|
9
11
|
PatchPlan,
|
|
@@ -76,6 +78,42 @@ export type EnrichConfig = {
|
|
|
76
78
|
match: Partial<Record<EnrichObjectType, EnrichMatchConfig>>;
|
|
77
79
|
fields: Partial<Record<EnrichObjectType, EnrichFieldConfig[]>>;
|
|
78
80
|
policy: EnrichPolicyConfig;
|
|
81
|
+
/** Net-new lead creation (`enrich acquire`). Optional; absent = acquire off. */
|
|
82
|
+
acquire?: AcquireConfig;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/** Maps a sourced record to the CRM properties a net-new record is created with. */
|
|
86
|
+
export type AcquireCreateMap = {
|
|
87
|
+
/** CRM property name → source payload path (read via sourceValueAt). */
|
|
88
|
+
properties: Record<string, string>;
|
|
89
|
+
/** Source path whose value becomes the dedupe key (e.g. "email"). */
|
|
90
|
+
matchKey: string;
|
|
91
|
+
/** Optional source path to a company name; the connector resolves-or-creates it. */
|
|
92
|
+
associateCompanyFrom?: string;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Net-new discovery for an API acquire source. `provider` selects the adapter
|
|
97
|
+
* (explorium = prospect search; pipe0 = work-email waterfall). `filters` is the
|
|
98
|
+
* provider-native query; `resolveEmailsWith` chains a second provider to fill
|
|
99
|
+
* real emails (e.g. explorium discovers, pipe0 resolves the work email).
|
|
100
|
+
*/
|
|
101
|
+
export type AcquireDiscoveryConfig = {
|
|
102
|
+
provider: "explorium" | "pipe0";
|
|
103
|
+
filters?: Record<string, unknown>;
|
|
104
|
+
size?: number;
|
|
105
|
+
resolveEmailsWith?: "pipe0";
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export type AcquireConfig = {
|
|
109
|
+
/** Windowed budget enforced by the acquire meter; absent = unmetered. */
|
|
110
|
+
budget?: AcquireBudget;
|
|
111
|
+
/** Estimated provider cost (USD) per created record, by source id. */
|
|
112
|
+
costPerRecord?: Record<string, number>;
|
|
113
|
+
/** How to build a net-new record from a sourced row, per object type. */
|
|
114
|
+
create: Partial<Record<EnrichObjectType, AcquireCreateMap>>;
|
|
115
|
+
/** Net-new discovery params for API sources, by source id. */
|
|
116
|
+
discovery?: Record<string, AcquireDiscoveryConfig>;
|
|
79
117
|
};
|
|
80
118
|
|
|
81
119
|
export const ENRICH_CONFIG_FILE_NAME = "enrich.config.json";
|
|
@@ -91,7 +129,7 @@ const MATCH_KEYS: Record<EnrichObjectType, string[]> = {
|
|
|
91
129
|
};
|
|
92
130
|
|
|
93
131
|
/** API source ids the MVP can pull from. */
|
|
94
|
-
export const SUPPORTED_API_SOURCES = ["apollo"];
|
|
132
|
+
export const SUPPORTED_API_SOURCES = ["apollo", "explorium", "pipe0"];
|
|
95
133
|
|
|
96
134
|
/**
|
|
97
135
|
* Canonical fields enrich may target, plus the HubSpot property spellings the
|
|
@@ -263,11 +301,80 @@ export function parseEnrichConfig(raw: string): EnrichConfig {
|
|
|
263
301
|
}
|
|
264
302
|
}
|
|
265
303
|
}
|
|
266
|
-
|
|
304
|
+
// `fields` drives append/refresh. An acquire-only config (net-new lead
|
|
305
|
+
// creation, no blank-filling) legitimately maps no fields — accept it when
|
|
306
|
+
// an acquire.create mapping is present.
|
|
307
|
+
const hasAcquireCreate = Boolean(
|
|
308
|
+
config.acquire?.create && Object.keys(config.acquire.create).length > 0,
|
|
309
|
+
);
|
|
310
|
+
if (!anyField && !hasAcquireCreate) {
|
|
311
|
+
fail('"fields" maps nothing — add at least one field entry (or an "acquire.create" mapping)');
|
|
312
|
+
}
|
|
267
313
|
|
|
268
314
|
return config as EnrichConfig;
|
|
269
315
|
}
|
|
270
316
|
|
|
317
|
+
/**
|
|
318
|
+
* Built-in zero-config presets for well-known sources, so the common Mode-A
|
|
319
|
+
* loop — "feed a Clay export, get the dedup/routing verdict" — needs no
|
|
320
|
+
* hand-authored config. These are **match-only** (empty `fields`): the value is
|
|
321
|
+
* the matched / unmatched / ambiguous routing and collision detection, not field
|
|
322
|
+
* writes. Drop an `enrich.config.json` next to your run to map fields for actual
|
|
323
|
+
* field enrichment; an explicit config always wins over a preset. Constructed
|
|
324
|
+
* directly (not via `parseEnrichConfig`) so the match-only shape is allowed.
|
|
325
|
+
*/
|
|
326
|
+
export const BUILTIN_ENRICH_PRESETS: Record<string, EnrichConfig> = {
|
|
327
|
+
clay: {
|
|
328
|
+
sources: { clay: { kind: "ingest", format: "csv" } },
|
|
329
|
+
policy: { overwrite: "never", defaultStaleDays: 90 },
|
|
330
|
+
match: {
|
|
331
|
+
company: { keys: ["domain"], onAmbiguous: "skip" },
|
|
332
|
+
contact: { keys: ["email"], onAmbiguous: "skip" },
|
|
333
|
+
},
|
|
334
|
+
fields: {},
|
|
335
|
+
},
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
export function builtinEnrichPreset(source: string | undefined): EnrichConfig | undefined {
|
|
339
|
+
return source ? BUILTIN_ENRICH_PRESETS[source] : undefined;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Zero-config `enrich acquire` preset so every client gets targeted, deduped,
|
|
344
|
+
* metered lead-fill out of the box — no hand-authored enrich.config.json. The
|
|
345
|
+
* ICP (icp.json) supplies the targeting; this supplies sensible plumbing. The
|
|
346
|
+
* create mapping writes the prospect's LinkedIn URL into hs_linkedin_url, so
|
|
347
|
+
* each created contact strengthens future dedup. An explicit config always wins.
|
|
348
|
+
*/
|
|
349
|
+
export function builtinAcquirePreset(source: string | undefined): EnrichConfig | undefined {
|
|
350
|
+
const provider = source ?? "pipe0";
|
|
351
|
+
if (provider !== "pipe0" && provider !== "explorium") return undefined;
|
|
352
|
+
return {
|
|
353
|
+
sources: { [provider]: { kind: "api" } },
|
|
354
|
+
match: { contact: { keys: ["email"], onAmbiguous: "skip" } },
|
|
355
|
+
fields: {},
|
|
356
|
+
policy: { overwrite: "never" },
|
|
357
|
+
acquire: {
|
|
358
|
+
budget: { records: { perDay: 50, perMonth: 500 }, spend: { perDay: 25, perMonth: 250 } },
|
|
359
|
+
costPerRecord: { pipe0: 0.1, explorium: 0.4 },
|
|
360
|
+
discovery: { [provider]: { provider, size: 25 } },
|
|
361
|
+
create: {
|
|
362
|
+
contact: {
|
|
363
|
+
matchKey: "email",
|
|
364
|
+
properties: {
|
|
365
|
+
email: "email",
|
|
366
|
+
firstname: "firstName",
|
|
367
|
+
lastname: "lastName",
|
|
368
|
+
jobtitle: "jobTitle",
|
|
369
|
+
company: "companyName",
|
|
370
|
+
hs_linkedin_url: "linkedin",
|
|
371
|
+
},
|
|
372
|
+
},
|
|
373
|
+
},
|
|
374
|
+
},
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
|
|
271
378
|
export function loadEnrichConfig(path: string): EnrichConfig {
|
|
272
379
|
let raw: string;
|
|
273
380
|
try {
|
|
@@ -634,7 +741,12 @@ export function buildEnrichPlan(options: BuildEnrichPlanOptions): EnrichPlanResu
|
|
|
634
741
|
const fields = (config.fields[record.objectType] ?? []).filter(
|
|
635
742
|
(field) => field.from[source] !== undefined,
|
|
636
743
|
);
|
|
637
|
-
|
|
744
|
+
// Run the matcher whenever the object type has match keys; field mappings
|
|
745
|
+
// only gate whether we EMIT field ops. With no fields (a match-only config,
|
|
746
|
+
// e.g. the built-in Clay preset) this still reports the matched / ambiguous
|
|
747
|
+
// / unmatched routing verdict and records collisions — the Mode-A hygiene
|
|
748
|
+
// gate — it just proposes zero field writes.
|
|
749
|
+
if (!match) {
|
|
638
750
|
counts.unmatched += 1;
|
|
639
751
|
unmatchedSourceIds.push(record.id);
|
|
640
752
|
continue;
|
|
@@ -798,6 +910,166 @@ export function buildEnrichPlan(options: BuildEnrichPlanOptions): EnrichPlanResu
|
|
|
798
910
|
return { plan, counts, stamps, ambiguities, unmatchedSourceIds };
|
|
799
911
|
}
|
|
800
912
|
|
|
913
|
+
// ---------------------------------------------------------------------------
|
|
914
|
+
// Acquire: turn sourced-but-unmatched prospects into net-new lead creates.
|
|
915
|
+
|
|
916
|
+
export type AcquireCounts = {
|
|
917
|
+
fetched: number;
|
|
918
|
+
matched: number;
|
|
919
|
+
ambiguous: number;
|
|
920
|
+
unmatched: number;
|
|
921
|
+
/** create_record ops emitted. */
|
|
922
|
+
created: number;
|
|
923
|
+
/** unmatched rows that would have been created but for the meter ceiling. */
|
|
924
|
+
withheldByMeter: number;
|
|
925
|
+
};
|
|
926
|
+
|
|
927
|
+
export type AcquirePlanResult = {
|
|
928
|
+
plan: PatchPlan;
|
|
929
|
+
counts: AcquireCounts;
|
|
930
|
+
estCostUsd: number;
|
|
931
|
+
};
|
|
932
|
+
|
|
933
|
+
export type BuildAcquirePlanOptions = {
|
|
934
|
+
config: EnrichConfig;
|
|
935
|
+
source: string;
|
|
936
|
+
snapshot: CanonicalGtmSnapshot;
|
|
937
|
+
records: EnrichSourceRecord[];
|
|
938
|
+
runLabel: string;
|
|
939
|
+
/** Meter ceiling: max create ops to emit. null/undefined = unlimited. */
|
|
940
|
+
maxRecords?: number | null;
|
|
941
|
+
now?: () => Date;
|
|
942
|
+
};
|
|
943
|
+
|
|
944
|
+
/**
|
|
945
|
+
* Match each sourced record against the snapshot and route it: matched =
|
|
946
|
+
* already in the CRM (skip), ambiguous = a possible duplicate exists (skip —
|
|
947
|
+
* resolve-first never creates over ambiguity), unmatched = a net-new lead.
|
|
948
|
+
* Unmatched rows with a dedupe key and at least one mapped property become
|
|
949
|
+
* `create_record` operations, capped at `maxRecords` (the meter's headroom).
|
|
950
|
+
* Always a dry-run plan; nothing is written until apply.
|
|
951
|
+
*/
|
|
952
|
+
export function buildAcquirePlan(options: BuildAcquirePlanOptions): AcquirePlanResult {
|
|
953
|
+
const { config, source, snapshot, records, runLabel } = options;
|
|
954
|
+
const nowIso = (options.now ?? (() => new Date()))().toISOString();
|
|
955
|
+
const sourceConfig = config.sources[source];
|
|
956
|
+
if (!sourceConfig) throw new Error(`enrich: source "${source}" is not declared in the config`);
|
|
957
|
+
const acquire = config.acquire;
|
|
958
|
+
if (!acquire) throw new Error('enrich acquire: config has no "acquire" section');
|
|
959
|
+
|
|
960
|
+
const cap = options.maxRecords ?? null;
|
|
961
|
+
const costPerRecord = acquire.costPerRecord?.[source] ?? 0;
|
|
962
|
+
|
|
963
|
+
const operations: PatchOperation[] = [];
|
|
964
|
+
const evidence: GtmEvidence[] = [];
|
|
965
|
+
const counts: AcquireCounts = {
|
|
966
|
+
fetched: records.length,
|
|
967
|
+
matched: 0,
|
|
968
|
+
ambiguous: 0,
|
|
969
|
+
unmatched: 0,
|
|
970
|
+
created: 0,
|
|
971
|
+
withheldByMeter: 0,
|
|
972
|
+
};
|
|
973
|
+
let estCostUsd = 0;
|
|
974
|
+
|
|
975
|
+
for (const record of records) {
|
|
976
|
+
const createMap = acquire.create[record.objectType];
|
|
977
|
+
const match = config.match[record.objectType];
|
|
978
|
+
// No create mapping or no match config for this type — can neither create
|
|
979
|
+
// nor dedupe it safely; count as unmatched and skip.
|
|
980
|
+
if (!createMap || !match) {
|
|
981
|
+
counts.unmatched += 1;
|
|
982
|
+
continue;
|
|
983
|
+
}
|
|
984
|
+
const outcome = matchSourceRecord(snapshot, record.objectType, match.keys, record.keys);
|
|
985
|
+
if (outcome.status === "matched") {
|
|
986
|
+
counts.matched += 1;
|
|
987
|
+
continue;
|
|
988
|
+
}
|
|
989
|
+
if (outcome.status === "ambiguous") {
|
|
990
|
+
// Resolve-first: a possible duplicate exists — never create over it.
|
|
991
|
+
counts.ambiguous += 1;
|
|
992
|
+
continue;
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
counts.unmatched += 1;
|
|
996
|
+
const matchValue = String(record.keys[createMap.matchKey] ?? "").trim();
|
|
997
|
+
if (!matchValue) continue; // no dedupe key → refuse to create (can't resolve-first)
|
|
998
|
+
|
|
999
|
+
if (cap !== null && counts.created >= cap) {
|
|
1000
|
+
counts.withheldByMeter += 1;
|
|
1001
|
+
continue;
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
const properties: Record<string, string> = {};
|
|
1005
|
+
for (const [crmProp, path] of Object.entries(createMap.properties)) {
|
|
1006
|
+
const value = sourceValueAt(record.payload, path);
|
|
1007
|
+
if (!isEmptyValue(value)) properties[crmProp] = String(value);
|
|
1008
|
+
}
|
|
1009
|
+
if (Object.keys(properties).length === 0) continue; // nothing to write
|
|
1010
|
+
|
|
1011
|
+
let associateCompanyName: string | undefined;
|
|
1012
|
+
if (createMap.associateCompanyFrom) {
|
|
1013
|
+
const companyValue = sourceValueAt(record.payload, createMap.associateCompanyFrom);
|
|
1014
|
+
if (!isEmptyValue(companyValue)) associateCompanyName = String(companyValue);
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
const recordEvidence = evidenceFor(
|
|
1018
|
+
source,
|
|
1019
|
+
sourceConfig.kind,
|
|
1020
|
+
sourceConfig.format,
|
|
1021
|
+
record,
|
|
1022
|
+
undefined,
|
|
1023
|
+
nowIso,
|
|
1024
|
+
);
|
|
1025
|
+
evidence.push(recordEvidence);
|
|
1026
|
+
|
|
1027
|
+
const payload: CreateRecordPayload = {
|
|
1028
|
+
properties,
|
|
1029
|
+
matchKey: createMap.matchKey,
|
|
1030
|
+
matchValue,
|
|
1031
|
+
source,
|
|
1032
|
+
estCostUsd: costPerRecord,
|
|
1033
|
+
associateCompanyName,
|
|
1034
|
+
};
|
|
1035
|
+
operations.push({
|
|
1036
|
+
id: `op_acq_${fnv1a(`${source}:${record.objectType}:${matchValue}`)}`,
|
|
1037
|
+
objectType: canonicalObjectType(record.objectType),
|
|
1038
|
+
objectId: `create:${matchValue}`,
|
|
1039
|
+
operation: "create_record",
|
|
1040
|
+
beforeValue: null,
|
|
1041
|
+
afterValue: payload,
|
|
1042
|
+
reason:
|
|
1043
|
+
`${source} sourced net-new ${record.objectType} "${describeSourceRecord(record)}" ` +
|
|
1044
|
+
`(${createMap.matchKey}=${matchValue}); no CRM match — create as a lead.`,
|
|
1045
|
+
sourceRuleOrPolicy: `acquire:${source}`,
|
|
1046
|
+
riskLevel: "medium",
|
|
1047
|
+
approvalRequired: true,
|
|
1048
|
+
rollback: "Archive the created record (it was net-new).",
|
|
1049
|
+
evidenceIds: [recordEvidence.id],
|
|
1050
|
+
});
|
|
1051
|
+
counts.created += 1;
|
|
1052
|
+
estCostUsd += costPerRecord;
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
const plan: PatchPlan = {
|
|
1056
|
+
id: `patch_plan_acq_${fnv1a(`${source}:${runLabel}:${nowIso}`)}`,
|
|
1057
|
+
title: `Acquire leads — ${source}`,
|
|
1058
|
+
createdAt: nowIso,
|
|
1059
|
+
status: operations.length > 0 ? "needs_approval" : "draft",
|
|
1060
|
+
dryRun: true,
|
|
1061
|
+
summary:
|
|
1062
|
+
`${counts.created} net-new lead(s) proposed from ${source} (${counts.fetched} sourced: ` +
|
|
1063
|
+
`${counts.matched} already in CRM, ${counts.ambiguous} ambiguous skipped, ${counts.unmatched} unmatched` +
|
|
1064
|
+
`${counts.withheldByMeter > 0 ? `, ${counts.withheldByMeter} withheld by meter` : ""}). ` +
|
|
1065
|
+
`Est. cost $${estCostUsd.toFixed(2)}.`,
|
|
1066
|
+
findings: [],
|
|
1067
|
+
evidence,
|
|
1068
|
+
operations,
|
|
1069
|
+
};
|
|
1070
|
+
return { plan, counts, estCostUsd };
|
|
1071
|
+
}
|
|
1072
|
+
|
|
801
1073
|
// ---------------------------------------------------------------------------
|
|
802
1074
|
// Staleness: compute the refresh work set from run-store stamps.
|
|
803
1075
|
|
package/src/format.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import type { PatchPlan, PatchPlanRun } from "./types.ts";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
// `summary: true` renders only the header + the Findings-by-Rule table + an
|
|
4
|
+
// operation count, for read verbs like `audit` where the full per-operation
|
|
5
|
+
// dump (1,400+ lines on a real portal) buries the signal. Defaults to the full
|
|
6
|
+
// view so write-preview callers (bulk-update, fix, dedupe, plans show, MCP) —
|
|
7
|
+
// where you approve specific operations — keep every operation's detail.
|
|
8
|
+
export function patchPlanToMarkdown(plan: PatchPlan, opts: { summary?: boolean } = {}) {
|
|
4
9
|
const lines = [
|
|
5
10
|
`# ${plan.title}`,
|
|
6
11
|
"",
|
|
@@ -24,6 +29,19 @@ export function patchPlanToMarkdown(plan: PatchPlan) {
|
|
|
24
29
|
lines.push("");
|
|
25
30
|
}
|
|
26
31
|
|
|
32
|
+
if (opts.summary) {
|
|
33
|
+
const ops = plan.operations.length;
|
|
34
|
+
lines.push(
|
|
35
|
+
ops === 0
|
|
36
|
+
? "No patch operations proposed."
|
|
37
|
+
: `${ops} dry-run patch operation${ops === 1 ? "" : "s"} proposed — nothing written.`,
|
|
38
|
+
"",
|
|
39
|
+
"Full plan detail: re-run with `--full`.",
|
|
40
|
+
"Client-ready report: `fullstackgtm report` (add `--format html` for a printable file).",
|
|
41
|
+
);
|
|
42
|
+
return `${lines.join("\n")}\n`;
|
|
43
|
+
}
|
|
44
|
+
|
|
27
45
|
lines.push("## Findings", "");
|
|
28
46
|
|
|
29
47
|
const findings = plan.pipelineFindings ?? [];
|
|
@@ -107,7 +125,7 @@ export function patchPlanToMarkdown(plan: PatchPlan) {
|
|
|
107
125
|
|
|
108
126
|
lines.push(
|
|
109
127
|
"",
|
|
110
|
-
">
|
|
128
|
+
"> Dry-run plan — read-only. No provider write happens until you approve specific operations and run `apply`; placeholder values are refused without an explicit override. These safety invariants are not beta.",
|
|
111
129
|
);
|
|
112
130
|
return `${lines.join("\n")}\n`;
|
|
113
131
|
}
|
package/src/health.ts
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { appendFileSync, mkdirSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
|
|
4
|
+
import { activeProfile, credentialsDir, ensureSecureHomeDir, writeSecureFile } from "./credentials.ts";
|
|
5
|
+
import type { AuditFindingSeverity, CanonicalGtmSnapshot, PatchPlan } from "./types.ts";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The engagement workspace: a per-client health timeline that accrues from the
|
|
9
|
+
* verb people already run (`audit --save`). State lives in the profile dir
|
|
10
|
+
* (`$FSGTM_HOME[/profiles/<name>]`) alongside credentials and plans, so a
|
|
11
|
+
* consultant working `--profile <client>` builds a continuous record of one
|
|
12
|
+
* org's CRM health over time — not just episodic one-off audits.
|
|
13
|
+
*
|
|
14
|
+
* The score is DETERMINISTIC (no LLM): same plan + same snapshot → same score,
|
|
15
|
+
* so it is stable in CI and comparable run-over-run.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
// Severity weights: a critical finding costs ~3× a warning, ~10× an info.
|
|
19
|
+
const SEVERITY_WEIGHT: Record<AuditFindingSeverity, number> = { info: 1, warning: 3, critical: 10 };
|
|
20
|
+
|
|
21
|
+
export type HealthEntry = {
|
|
22
|
+
/** ISO timestamp of the audit that produced this entry. */
|
|
23
|
+
at: string;
|
|
24
|
+
/** The saved plan this entry summarizes (correlates to plans/ and snapshots/). */
|
|
25
|
+
planId: string;
|
|
26
|
+
/** Deterministic 0–100 hygiene score (higher is healthier). */
|
|
27
|
+
score: number;
|
|
28
|
+
/** Total finding count across all rules. */
|
|
29
|
+
findings: number;
|
|
30
|
+
/** Severity-weighted finding total (drives the score). */
|
|
31
|
+
weightedFindings: number;
|
|
32
|
+
/** Record counts at audit time — the denominator that normalizes the score. */
|
|
33
|
+
records: { accounts: number; contacts: number; deals: number; total: number };
|
|
34
|
+
/** Finding count per rule id. */
|
|
35
|
+
byRule: Record<string, number>;
|
|
36
|
+
/** Finding count per severity. */
|
|
37
|
+
severityCounts: Record<AuditFindingSeverity, number>;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export type HealthRuleDelta = {
|
|
41
|
+
ruleId: string;
|
|
42
|
+
current: number;
|
|
43
|
+
previous: number;
|
|
44
|
+
/** current − previous: positive = more findings (worse), negative = fewer (better). */
|
|
45
|
+
delta: number;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export type HealthRollup = {
|
|
49
|
+
profile: string;
|
|
50
|
+
auditCount: number;
|
|
51
|
+
first: string;
|
|
52
|
+
latest: string;
|
|
53
|
+
current: HealthEntry;
|
|
54
|
+
previous: HealthEntry | null;
|
|
55
|
+
/** current.score − previous.score: positive = improving. null on the first audit. */
|
|
56
|
+
scoreDelta: number | null;
|
|
57
|
+
ruleDeltas: HealthRuleDelta[];
|
|
58
|
+
history: Array<{ at: string; score: number; findings: number }>;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Deterministically score a saved audit. score = 100 / (1 + weighted findings
|
|
63
|
+
* per record): 0 findings → 100; one weighted finding per record → 50; the
|
|
64
|
+
* curve is bounded (0, 100], monotonic, and needs no clamping or magic
|
|
65
|
+
* constants. A messy 50-record CRM scores worse than the same finding count
|
|
66
|
+
* across 5,000 records, which is the point.
|
|
67
|
+
*/
|
|
68
|
+
export function computeHealth(plan: PatchPlan, snapshot: CanonicalGtmSnapshot, at: string): HealthEntry {
|
|
69
|
+
const byRule: Record<string, number> = {};
|
|
70
|
+
const severityCounts: Record<AuditFindingSeverity, number> = { info: 0, warning: 0, critical: 0 };
|
|
71
|
+
let weightedFindings = 0;
|
|
72
|
+
for (const finding of plan.findings) {
|
|
73
|
+
byRule[finding.ruleId] = (byRule[finding.ruleId] ?? 0) + 1;
|
|
74
|
+
severityCounts[finding.severity] += 1;
|
|
75
|
+
weightedFindings += SEVERITY_WEIGHT[finding.severity];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const records = {
|
|
79
|
+
accounts: snapshot.accounts.length,
|
|
80
|
+
contacts: snapshot.contacts.length,
|
|
81
|
+
deals: snapshot.deals.length,
|
|
82
|
+
total: snapshot.accounts.length + snapshot.contacts.length + snapshot.deals.length,
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const penalty = weightedFindings / Math.max(records.total, 1);
|
|
86
|
+
const score = Math.round(100 / (1 + penalty));
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
at,
|
|
90
|
+
planId: plan.id,
|
|
91
|
+
score,
|
|
92
|
+
findings: plan.findings.length,
|
|
93
|
+
weightedFindings,
|
|
94
|
+
records,
|
|
95
|
+
byRule,
|
|
96
|
+
severityCounts,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Roll a timeline up into the current state, the change since last time, and per-rule deltas. */
|
|
101
|
+
export function summarizeHealth(entries: HealthEntry[], profile: string): HealthRollup | null {
|
|
102
|
+
if (entries.length === 0) return null;
|
|
103
|
+
// Oldest → newest, so `current` is the last entry.
|
|
104
|
+
const sorted = [...entries].sort((a, b) => a.at.localeCompare(b.at));
|
|
105
|
+
const current = sorted[sorted.length - 1];
|
|
106
|
+
const previous = sorted.length > 1 ? sorted[sorted.length - 2] : null;
|
|
107
|
+
|
|
108
|
+
const ruleIds = new Set<string>([
|
|
109
|
+
...Object.keys(current.byRule),
|
|
110
|
+
...(previous ? Object.keys(previous.byRule) : []),
|
|
111
|
+
]);
|
|
112
|
+
const ruleDeltas: HealthRuleDelta[] = [...ruleIds]
|
|
113
|
+
.map((ruleId) => {
|
|
114
|
+
const cur = current.byRule[ruleId] ?? 0;
|
|
115
|
+
const prev = previous?.byRule[ruleId] ?? 0;
|
|
116
|
+
return { ruleId, current: cur, previous: prev, delta: cur - prev };
|
|
117
|
+
})
|
|
118
|
+
.sort((a, b) => b.current - a.current || a.ruleId.localeCompare(b.ruleId));
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
profile,
|
|
122
|
+
auditCount: sorted.length,
|
|
123
|
+
first: sorted[0].at,
|
|
124
|
+
latest: current.at,
|
|
125
|
+
current,
|
|
126
|
+
previous,
|
|
127
|
+
scoreDelta: previous ? current.score - previous.score : null,
|
|
128
|
+
ruleDeltas,
|
|
129
|
+
history: sorted.map((entry) => ({ at: entry.at, score: entry.score, findings: entry.findings })),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Sign-based and uniform: ▲ = the number went up, ▼ = down. The reader supplies
|
|
134
|
+
// the meaning (score up = good; findings up = bad) — mixing arrow direction with
|
|
135
|
+
// good/bad reads as contradictory ("▲ -3").
|
|
136
|
+
function arrow(delta: number): string {
|
|
137
|
+
if (delta === 0) return "·";
|
|
138
|
+
return delta > 0 ? "▲" : "▼";
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Human-readable rollup: headline score + trend, then per-rule change since last audit. */
|
|
142
|
+
export function healthToMarkdown(rollup: HealthRollup): string {
|
|
143
|
+
const { current, scoreDelta } = rollup;
|
|
144
|
+
const lines = [`# GTM health — profile \`${rollup.profile}\``, ""];
|
|
145
|
+
|
|
146
|
+
const deltaText =
|
|
147
|
+
scoreDelta === null
|
|
148
|
+
? "(first audit — no prior reading)"
|
|
149
|
+
: `${arrow(scoreDelta)} ${scoreDelta >= 0 ? "+" : ""}${scoreDelta} since the previous audit`;
|
|
150
|
+
lines.push(
|
|
151
|
+
`Score: **${current.score}/100** ${deltaText}`,
|
|
152
|
+
`Audits: ${rollup.auditCount} over ${shortDate(rollup.first)} → ${shortDate(rollup.latest)}`,
|
|
153
|
+
`Findings: ${current.findings} (${current.severityCounts.critical} critical, ${current.severityCounts.warning} warning, ${current.severityCounts.info} info)`,
|
|
154
|
+
`Records: ${current.records.accounts} accounts, ${current.records.contacts} contacts, ${current.records.deals} deals`,
|
|
155
|
+
"",
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
if (rollup.history.length > 1) {
|
|
159
|
+
lines.push("## Trend", "");
|
|
160
|
+
for (const point of rollup.history) {
|
|
161
|
+
const marker = point.at === rollup.latest ? " ← latest" : "";
|
|
162
|
+
lines.push(`- ${shortDate(point.at)} score ${point.score} (${point.findings} findings)${marker}`);
|
|
163
|
+
}
|
|
164
|
+
lines.push("");
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (rollup.ruleDeltas.length > 0) {
|
|
168
|
+
lines.push("## By rule (Δ since previous audit)", "", "| Rule | Findings | Δ |", "| --- | --- | --- |");
|
|
169
|
+
for (const rule of rollup.ruleDeltas) {
|
|
170
|
+
const sign = rule.delta > 0 ? `+${rule.delta}` : `${rule.delta}`;
|
|
171
|
+
const cell = rule.delta === 0 ? "·" : `${arrow(rule.delta)} ${sign}`;
|
|
172
|
+
lines.push(`| ${rule.ruleId} | ${rule.current} | ${cell} |`);
|
|
173
|
+
}
|
|
174
|
+
lines.push("");
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
lines.push("> Score is deterministic: 100 / (1 + severity-weighted findings per record). Lower findings or more clean records ⇒ higher score.");
|
|
178
|
+
return `${lines.join("\n")}\n`;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function shortDate(iso: string): string {
|
|
182
|
+
return iso.slice(0, 10);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// ── Profile-scoped storage ──────────────────────────────────────────────────
|
|
186
|
+
|
|
187
|
+
/** `$FSGTM_HOME[/profiles/<name>]/health.jsonl` — append-only, one entry per audit-save. */
|
|
188
|
+
export function healthFilePath(): string {
|
|
189
|
+
return join(credentialsDir(), "health.jsonl");
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** `$FSGTM_HOME[/profiles/<name>]/snapshots/` — canonical snapshots correlated by plan id. */
|
|
193
|
+
export function snapshotsDir(): string {
|
|
194
|
+
return join(credentialsDir(), "snapshots");
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Append a health entry to the active profile's timeline (0600, owner-only). */
|
|
198
|
+
export function appendHealthEntry(entry: HealthEntry): void {
|
|
199
|
+
ensureSecureHomeDir();
|
|
200
|
+
appendFileSync(healthFilePath(), `${JSON.stringify(entry)}\n`, { mode: 0o600 });
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Read the active profile's health timeline; tolerant of partial/corrupt lines. */
|
|
204
|
+
export function readHealthTimeline(): HealthEntry[] {
|
|
205
|
+
let raw: string;
|
|
206
|
+
try {
|
|
207
|
+
raw = readFileSync(healthFilePath(), "utf8");
|
|
208
|
+
} catch {
|
|
209
|
+
return [];
|
|
210
|
+
}
|
|
211
|
+
const entries: HealthEntry[] = [];
|
|
212
|
+
for (const line of raw.split("\n")) {
|
|
213
|
+
const trimmed = line.trim();
|
|
214
|
+
if (!trimmed) continue;
|
|
215
|
+
try {
|
|
216
|
+
entries.push(JSON.parse(trimmed) as HealthEntry);
|
|
217
|
+
} catch {
|
|
218
|
+
// Skip a torn line rather than failing the whole rollup.
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return entries;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** Persist the snapshot behind a saved audit so the timeline can diff/attribute later. */
|
|
225
|
+
export function saveWorkspaceSnapshot(planId: string, snapshot: CanonicalGtmSnapshot): string {
|
|
226
|
+
if (!/^[\w.-]+$/.test(planId)) throw new Error(`Invalid plan id: ${planId}`);
|
|
227
|
+
ensureSecureHomeDir();
|
|
228
|
+
const dir = snapshotsDir();
|
|
229
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
230
|
+
const path = join(dir, `${planId}.json`);
|
|
231
|
+
writeSecureFile(path, `${JSON.stringify(snapshot)}\n`);
|
|
232
|
+
return path;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** The active profile name, for rollup labeling. */
|
|
236
|
+
export function activeWorkspaceProfile(): string {
|
|
237
|
+
return activeProfile();
|
|
238
|
+
}
|