fullstackgtm 0.21.2 → 0.23.1

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/dist/cli.js CHANGED
@@ -25,8 +25,12 @@ import { computeScaleIndex, scaleReportToText } from "./marketScale.js";
25
25
  import { buildWorksheet, classifyMarket } from "./marketClassify.js";
26
26
  import { marketMapToHtml, marketMapToMarkdown } from "./marketReport.js";
27
27
  import { DEFAULT_RUBRIC, detectProviderFromKey, extractInsightsLlm, parseRubric, resolveLlmCredential, scoreCallLlm, validateLlmKey, } from "./llm.js";
28
+ import { buildEnrichPlan, createFileEnrichRunStore, DEFAULT_STALE_DAYS, ENRICH_CONFIG_FILE_NAME, enrichRunId, inferIngestObjectType, latestStamps, loadEnrichConfig, parseCsv, resolveCrmField, selectStaleWork, stagedSourceRecords, staleDaysFor, } from "./enrich.js";
29
+ import { apolloPullKeysForAppend, apolloPullKeysForRefresh, createApolloClient, pullApolloRecords, } from "./enrichApollo.js";
28
30
  import { resolveRecord } from "./resolve.js";
29
31
  import { buildBulkUpdatePlan } from "./bulkUpdate.js";
32
+ import { buildDedupePlan } from "./dedupe.js";
33
+ import { buildReassignPlans } from "./reassign.js";
30
34
  import { suggestValues } from "./suggest.js";
31
35
  function usage() {
32
36
  return `FullStackGTM — audit GTM data across providers, propose reviewable patch plans,
@@ -39,7 +43,8 @@ Usage:
39
43
  fullstackgtm login salesforce --device --client-id <consumer key> [--login-url <url>]
40
44
  fullstackgtm login salesforce --instance-url <url> [--no-validate]
41
45
  fullstackgtm login stripe [--no-validate]
42
- fullstackgtm login anthropic | openai store an LLM API key for call parse/score\n fullstackgtm logout <hubspot|salesforce|stripe|anthropic|openai|broker>
46
+ fullstackgtm login anthropic | openai store an LLM API key for call parse/score
47
+ fullstackgtm login apollo store an Apollo API key for enrich pulls\n fullstackgtm logout <hubspot|salesforce|stripe|anthropic|openai|apollo|broker>
43
48
 
44
49
  Secrets (tokens, client secrets) are NEVER passed as flags — they leak via
45
50
  the process list and shell history. Pipe them on stdin or enter them at the
@@ -79,7 +84,16 @@ Usage:
79
84
  against the stored capture it cites before it's accepted — then
80
85
  compute deterministic front states and drift, render the field
81
86
  report. refresh = capture → classify → drift → report in one step
82
- fullstackgtm bulk-update <account|contact|deal> --where <expr> [--where ] (--set <field>=<value> [--set ] | --archive | --create-task <text>) [--require <field>=<value> …] [--guard <object>:<where>[;<where>]:<none|some> …] [source options] [--save] [--json] [--out <path>]
87
+ fullstackgtm enrich append [--source apollo] [--objects companies,contacts] [--save] [--config <path>] [source options]
88
+ fullstackgtm enrich refresh [--source apollo] [--stale-days <n>] [--save] [--config <path>] [source options]
89
+ fullstackgtm enrich ingest <file.csv|payload.json> --source clay [--run-label <label>]
90
+ fullstackgtm enrich status [--runs] [--source <id>] [--json]
91
+ governed enrichment: pull (Apollo) or stage (Clay) third-party
92
+ data, match it to CRM records deterministically, and emit a
93
+ fill-blanks-only patch plan through the normal dry-run →
94
+ approve → apply gate. refresh re-checks stale stamped fields
95
+ and proposes updates only where the source value changed.
96
+ fullstackgtm bulk-update <account|contact|deal> --where <expr> [--where …] (--set <field>=<value> [--set …] | --archive [--force-archive-duplicates] | --create-task <text>) [--require <field>=<value> …] [--guard <object>:<where>[;<where>]:<none|some> …] [source options] [--save] [--json] [--out <path>]
83
97
  governed generic writes: filter the snapshot
84
98
  (field=value, field!=value, field~substr, field!~substr,
85
99
  field:empty, field:notempty, '|' = any-of; canonical fields
@@ -91,6 +105,35 @@ Usage:
91
105
  apply time (incl. mid-apply rechecks); equality filters
92
106
  double as preconditions; per-record ops apply
93
107
  all-or-nothing; guards assert cross-record conditions.
108
+ --set <field>=from:<sourceField> derives the value PER
109
+ RECORD from the snapshot (relational sources like
110
+ account.ownerId included); records whose source is empty
111
+ are skipped and counted, never guessed. --archive refuses
112
+ records that share their identity key (account domain /
113
+ contact email) with another record — merge those with
114
+ \`dedupe\` instead, or --force-archive-duplicates.
115
+ fullstackgtm dedupe <account|contact|deal> --key <domain|email|name> [--keep richest|oldest] [source options] [--reason <text>] [--max-operations <n>] [--save] [--json] [--out <path>]
116
+ find duplicate groups by normalized identity key and build
117
+ a dry-run plan of merge_records operations — one per group,
118
+ deterministic survivor (richest = most populated data
119
+ fields, ties to lowest id; oldest = lowest id). Approve and
120
+ apply like any plan; merges are IRREVERSIBLE on apply.
121
+ fullstackgtm reassign --from <ownerId> --to <ownerId> [--objects account,contact,deal] [--where <expr> …] [--except-deal-stage <stage>] [--include-closed-deals] [source options] [--save] [--json] [--out <path>]
122
+ ownership handoff playbook: one bulk-update-style plan per
123
+ object type (ownerId=<from> → <to>). Extra --where scoping
124
+ is account-lifted for deals/contacts (domain~.de becomes
125
+ account.domain~.de); --except-deal-stage <stage> excludes
126
+ deals in that stage AND every record whose account has an
127
+ open deal in it, re-verified per record at apply time.
128
+ Deal plans cover open deals only unless
129
+ --include-closed-deals.
130
+ fullstackgtm fix --rule <ruleId> --provider <name> [--min-confidence high|low] [--include-creates] [--today <iso>] [--yes]
131
+ one-shot composite: audit ONE rule → save the plan →
132
+ suggest values → approve only suggestion-backed operations
133
+ meeting the confidence bar (plus operations that need no
134
+ value) → with --yes, apply through the provider and print a
135
+ stage-by-stage summary. Without --yes it stops after
136
+ approval and prints the apply command.
94
137
  fullstackgtm suggest --plan-id <id> | --plan <path> [source options] [--json] [--out <path>]
95
138
  derive values for requires_human_* placeholders
96
139
  from snapshot evidence, with confidence + reasons
@@ -1072,6 +1115,423 @@ recomputed deterministically on every invocation — never stored.`);
1072
1115
  }
1073
1116
  throw new Error(`Unknown market subcommand: ${subcommand} (try: init, capture, classify, worksheet, observe, fronts, axes, overlay, scale, report, refresh)`);
1074
1117
  }
1118
+ /**
1119
+ * The enrich layer: governed append/refresh of third-party data (Apollo pull,
1120
+ * Clay ingest) into the CRM through the normal dry-run → approval → apply
1121
+ * contract. State lives in the profile-scoped run store (checkpoint,
1122
+ * staleness ledger, observability in one); scheduling belongs to the
1123
+ * horizontal scheduler — enrich owns no cron logic.
1124
+ */
1125
+ async function enrichCommand(args) {
1126
+ const [subcommand, ...rest] = args;
1127
+ // Catch --help BEFORE config load, credential resolution, or any network
1128
+ // call (the 0.14.1/0.18 bug class — `enrich append --help` executing a
1129
+ // paid Apollo pull would be its worst recurrence).
1130
+ if (!subcommand || subcommand === "--help" || subcommand === "-h" || rest.includes("--help") || rest.includes("-h")) {
1131
+ console.log(`Usage:
1132
+ enrich append [--source apollo] [--objects companies,contacts] [--save] [--config <path>]
1133
+ [source options] [--run-label <label>] [--json]
1134
+ enrich refresh [--source apollo] [--stale-days <n>] [--save] [--config <path>]
1135
+ [source options] [--run-label <label>] [--json]
1136
+ enrich ingest <file.csv|payload.json> --source clay [--run-label <label>] [--objects companies|contacts] [--config <path>]
1137
+ enrich status [--runs] [--source <id>] [--config <path>] [--json]
1138
+
1139
+ append pulls from an api source (Apollo — BYO key via \`login apollo\` or
1140
+ APOLLO_API_KEY) or reads data staged by \`enrich ingest\` (Clay CSV exports,
1141
+ webhook payload JSON), matches source records to CRM records via the ordered
1142
+ match keys in enrich.config.json (unique hit wins; zero hits falls through to
1143
+ the next key; multiple hits skip or flow into the suggest chain, per
1144
+ onAmbiguous), and emits a fill-blanks-only patch plan. Without --save it
1145
+ prints the dry-run diff and writes NOTHING; with --save the plan lands in the
1146
+ plan store as needs_approval and the run (counts, per-field enrichedAt stamps,
1147
+ resume cursor) lands in the profile's enrich run store. From there the normal
1148
+ chain takes over: plans approve → apply.
1149
+
1150
+ refresh computes its work set from the run-store stamps — fields enrich
1151
+ itself wrote, opted in with "refresh": true, older than the staleness window
1152
+ (--stale-days overrides per-field staleDays and policy.defaultStaleDays) —
1153
+ re-fetches the source, and proposes updates only where the source value
1154
+ actually changed. Every operation carries beforeValue, so apply-time
1155
+ compare-and-set rejects writes over a CRM that moved underneath the plan.
1156
+
1157
+ Conflict policy (MVP): "never" — enrich only fills blank fields and only
1158
+ re-touches fields its own ledger proves it stamped. system-only and always
1159
+ are phase 2. Recurring execution is the scheduler's job; enrich has no cron.`);
1160
+ return;
1161
+ }
1162
+ if (!["append", "refresh", "ingest", "status"].includes(subcommand)) {
1163
+ throw new Error(`Unknown enrich subcommand: ${subcommand} (try: append, refresh, ingest, status)`);
1164
+ }
1165
+ const configPath = () => resolve(process.cwd(), option(rest, "--config") ?? ENRICH_CONFIG_FILE_NAME);
1166
+ const store = createFileEnrichRunStore();
1167
+ if (subcommand === "status") {
1168
+ await enrichStatus(store, rest, configPath());
1169
+ return;
1170
+ }
1171
+ const config = loadEnrichConfig(configPath());
1172
+ if (subcommand === "ingest") {
1173
+ await enrichIngest(store, config, rest);
1174
+ return;
1175
+ }
1176
+ const mode = subcommand;
1177
+ const source = resolveEnrichSource(config, rest);
1178
+ const sourceConfig = config.sources[source];
1179
+ const save = rest.includes("--save");
1180
+ const today = new Date().toISOString().slice(0, 10);
1181
+ // Refresh work set comes from the staleness ledger, before any fetch.
1182
+ const allRuns = await store.list();
1183
+ let workSet = [];
1184
+ if (mode === "refresh") {
1185
+ const staleDaysOverride = numericOption(rest, "--stale-days");
1186
+ workSet = selectStaleWork(config, allRuns, source, { staleDaysOverride });
1187
+ if (workSet.length === 0) {
1188
+ const stamped = latestStamps(allRuns, source).size;
1189
+ console.log(stamped === 0
1190
+ ? `Nothing to refresh: no ${source} enrichment stamps yet. Run \`enrich append --source ${source} --save\` first.`
1191
+ : `Nothing to refresh: all ${stamped} stamped field(s) from ${source} are within their staleness window.`);
1192
+ return;
1193
+ }
1194
+ }
1195
+ const snapshot = await readSnapshot(rest);
1196
+ // Assemble source records: api pull (checkpointed when --save) or staged ingest data.
1197
+ let run = null;
1198
+ let records;
1199
+ let missCount = 0;
1200
+ if (sourceConfig.kind === "api") {
1201
+ const objectTypes = parseEnrichObjects(rest, config, source);
1202
+ const fieldsFor = (objectType) => (config.fields[objectType] ?? []).filter((field) => field.from[source] !== undefined);
1203
+ const pullKeys = mode === "append"
1204
+ ? apolloPullKeysForAppend(snapshot, objectTypes, (objectType, record) => fieldsFor(objectType).some((field) => {
1205
+ const value = record[resolveCrmField(objectType, field.crm)];
1206
+ return value === undefined || value === null || String(value).trim() === "";
1207
+ }))
1208
+ : apolloPullKeysForRefresh(snapshot, workSet);
1209
+ if (pullKeys.length === 0) {
1210
+ console.log(mode === "append"
1211
+ ? "Nothing to enrich: no records with a blank mapped field and a pull key (companies need a domain, contacts an email)."
1212
+ : "Nothing to refresh: no stale records carry a pull key (companies need a domain, contacts an email).");
1213
+ return;
1214
+ }
1215
+ const client = createApolloClient({
1216
+ getApiKey: () => apolloApiKey(),
1217
+ apiBaseUrl: process.env.APOLLO_API_BASE_URL,
1218
+ });
1219
+ if (save) {
1220
+ run = await openEnrichRun(store, source, mode, option(rest, "--run-label"), today);
1221
+ if (run.cursor) {
1222
+ console.error(`Resuming interrupted run ${run.runLabel} from cursor ${run.cursor} (${run.pulled?.length ?? 0} record(s) already pulled).`);
1223
+ }
1224
+ }
1225
+ const result = await pullApolloRecords(client, pullKeys, {
1226
+ resumeAfter: run?.cursor ?? null,
1227
+ onProgress: run
1228
+ ? async (progress) => {
1229
+ run.cursor = progress.lastKeyValue;
1230
+ if (progress.record)
1231
+ run.pulled = [...(run.pulled ?? []), progress.record];
1232
+ if (progress.miss)
1233
+ run.missedKeys = [...(run.missedKeys ?? []), progress.miss.value];
1234
+ await store.update(run);
1235
+ }
1236
+ : undefined,
1237
+ });
1238
+ records = run ? [...(run.pulled ?? [])] : result.records;
1239
+ missCount = run ? (run.missedKeys?.length ?? 0) : result.misses.length;
1240
+ }
1241
+ else {
1242
+ const stagedLabel = option(rest, "--staged-run");
1243
+ const stagedRun = stagedLabel
1244
+ ? await store.get(stagedLabel)
1245
+ : await store.latest({ source, mode: "ingest" });
1246
+ if (!stagedRun || stagedRun.mode !== "ingest") {
1247
+ throw new Error(`No staged data for source "${source}". Stage it first: fullstackgtm enrich ingest <file.csv|payload.json> --source ${source}`);
1248
+ }
1249
+ records = stagedSourceRecords(config, source, stagedRun);
1250
+ if (save)
1251
+ run = await openEnrichRun(store, source, mode, option(rest, "--run-label"), today);
1252
+ }
1253
+ const result = buildEnrichPlan({
1254
+ config,
1255
+ source,
1256
+ mode,
1257
+ snapshot,
1258
+ records,
1259
+ workSet: mode === "refresh" ? workSet : undefined,
1260
+ runLabel: run?.runLabel ?? `${mode}-${source}-${today}`,
1261
+ });
1262
+ // Pull keys the source had no data for count as fetched-but-unmatched.
1263
+ result.counts.fetched += missCount;
1264
+ result.counts.unmatched += missCount;
1265
+ if (!save) {
1266
+ if (rest.includes("--json")) {
1267
+ console.log(JSON.stringify(result.plan, null, 2));
1268
+ }
1269
+ else {
1270
+ console.log(patchPlanToMarkdown(result.plan));
1271
+ console.log(formatEnrichCounts(result.counts, result.ambiguities.length));
1272
+ console.log("\nDry run — nothing written. Re-run with --save to persist the plan and the run record.");
1273
+ }
1274
+ return;
1275
+ }
1276
+ // --save: persist the plan (when it proposes anything) and finalize the run.
1277
+ const planIds = [];
1278
+ if (result.plan.operations.length > 0) {
1279
+ await createFilePlanStore().save(result.plan);
1280
+ planIds.push(result.plan.id);
1281
+ }
1282
+ const finalized = {
1283
+ ...run,
1284
+ completedAt: new Date().toISOString(),
1285
+ cursor: null,
1286
+ counts: result.counts,
1287
+ planIds: [...(run.planIds ?? []), ...planIds],
1288
+ stamps: [...(run.stamps ?? []), ...result.stamps],
1289
+ ambiguities: result.ambiguities,
1290
+ };
1291
+ await store.update(finalized);
1292
+ console.log(formatEnrichCounts(result.counts, result.ambiguities.length));
1293
+ if (planIds.length > 0) {
1294
+ console.log(`Saved plan ${result.plan.id} (run ${finalized.runLabel}). Review with \`fullstackgtm plans show ${result.plan.id}\`, ` +
1295
+ `approve with \`fullstackgtm plans approve ${result.plan.id} --operations <ids|all>\`, then ` +
1296
+ `\`fullstackgtm apply --plan-id ${result.plan.id} --provider <name>\`.`);
1297
+ }
1298
+ else {
1299
+ console.log(`Run ${finalized.runLabel} recorded; no operations to propose.`);
1300
+ }
1301
+ }
1302
+ function formatEnrichCounts(counts, ambiguities) {
1303
+ return (`Source records: ${counts.fetched} fetched · ${counts.matched} matched · ` +
1304
+ `${counts.unmatched} unmatched · ${counts.ambiguous} ambiguous (${ambiguities} collision(s) recorded) · ` +
1305
+ `${counts.opsEmitted} operation(s) proposed`);
1306
+ }
1307
+ function resolveEnrichSource(config, rest) {
1308
+ const requested = option(rest, "--source");
1309
+ const declared = Object.keys(config.sources);
1310
+ if (requested) {
1311
+ if (!config.sources[requested]) {
1312
+ throw new Error(`Unknown enrich source "${requested}" (declared: ${declared.join(", ")})`);
1313
+ }
1314
+ return requested;
1315
+ }
1316
+ if (declared.length === 1)
1317
+ return declared[0];
1318
+ if (config.sources.apollo)
1319
+ return "apollo";
1320
+ throw new Error(`Multiple sources declared (${declared.join(", ")}) — pass --source <id>`);
1321
+ }
1322
+ function parseEnrichObjects(rest, config, source) {
1323
+ const configured = ["company", "contact"].filter((objectType) => (config.fields[objectType] ?? []).some((field) => field.from[source] !== undefined));
1324
+ const flag = option(rest, "--objects");
1325
+ if (!flag) {
1326
+ if (configured.length === 0) {
1327
+ throw new Error(`No fields map from source "${source}" — add "from": { "${source}": ... } entries to the config.`);
1328
+ }
1329
+ return configured;
1330
+ }
1331
+ const requested = Array.from(new Set(flag.split(",").map((part) => parseSingleObjectType(part))));
1332
+ for (const objectType of requested) {
1333
+ if (!configured.includes(objectType)) {
1334
+ throw new Error(`--objects ${flag}: no ${objectType} fields map from source "${source}" in the config.`);
1335
+ }
1336
+ }
1337
+ return requested;
1338
+ }
1339
+ function apolloApiKey() {
1340
+ if (process.env.APOLLO_API_KEY)
1341
+ return process.env.APOLLO_API_KEY;
1342
+ const stored = getCredential("apollo");
1343
+ if (stored)
1344
+ return stored.accessToken;
1345
+ throw new Error('No Apollo credentials. Run `echo "$APOLLO_API_KEY" | fullstackgtm login apollo` once, or set APOLLO_API_KEY.');
1346
+ }
1347
+ /**
1348
+ * Open (or resume) a saved run. An interrupted run — same label, same source
1349
+ * and mode, never completed — is resumed from its cursor; a completed run
1350
+ * with the default label gets a -2/-3 suffix (runs are append-only).
1351
+ */
1352
+ async function openEnrichRun(store, source, mode, requestedLabel, today) {
1353
+ const baseLabel = requestedLabel ?? `${mode}-${source}-${today}`;
1354
+ let label = baseLabel;
1355
+ for (let suffix = 2;; suffix += 1) {
1356
+ const existing = await store.get(label);
1357
+ if (!existing)
1358
+ break;
1359
+ if (existing.source === source && existing.mode === mode && existing.completedAt === null) {
1360
+ return existing; // resume the interrupted run
1361
+ }
1362
+ if (requestedLabel) {
1363
+ throw new Error(`Run "${requestedLabel}" already exists and is completed — enrich runs are append-only.`);
1364
+ }
1365
+ label = `${baseLabel}-${suffix}`;
1366
+ }
1367
+ return store.append({
1368
+ id: enrichRunId(source, label),
1369
+ runLabel: label,
1370
+ source,
1371
+ mode,
1372
+ startedAt: new Date().toISOString(),
1373
+ completedAt: null,
1374
+ cursor: null,
1375
+ counts: { fetched: 0, matched: 0, unmatched: 0, ambiguous: 0, opsEmitted: 0 },
1376
+ planIds: [],
1377
+ stamps: [],
1378
+ });
1379
+ }
1380
+ async function enrichIngest(store, config, rest) {
1381
+ const file = rest.find((arg) => !arg.startsWith("--") && !isOptionValue(rest, arg));
1382
+ if (!file)
1383
+ throw new Error("Usage: fullstackgtm enrich ingest <file.csv|payload.json> --source <id> [--run-label <label>]");
1384
+ const source = option(rest, "--source");
1385
+ if (!source)
1386
+ throw new Error("enrich ingest requires --source <id> (the ingest source the data belongs to)");
1387
+ const sourceConfig = config.sources[source];
1388
+ if (!sourceConfig) {
1389
+ throw new Error(`Unknown enrich source "${source}" (declared: ${Object.keys(config.sources).join(", ")})`);
1390
+ }
1391
+ if (sourceConfig.kind !== "ingest") {
1392
+ throw new Error(`Source "${source}" is kind "${sourceConfig.kind}" — only ingest sources accept staged data.`);
1393
+ }
1394
+ const raw = readFileSync(resolve(process.cwd(), file), "utf8");
1395
+ let rows;
1396
+ const isCsv = file.toLowerCase().endsWith(".csv") || sourceConfig.format === "csv";
1397
+ if (isCsv && !file.toLowerCase().endsWith(".json")) {
1398
+ rows = parseCsv(raw);
1399
+ }
1400
+ else {
1401
+ const parsed = JSON.parse(raw);
1402
+ if (Array.isArray(parsed))
1403
+ rows = parsed;
1404
+ else if (parsed && typeof parsed === "object" && Array.isArray(parsed.rows)) {
1405
+ rows = parsed.rows;
1406
+ }
1407
+ else if (parsed && typeof parsed === "object") {
1408
+ rows = [parsed];
1409
+ }
1410
+ else {
1411
+ throw new Error(`${file}: expected a JSON array, an object, or { "rows": [...] }`);
1412
+ }
1413
+ }
1414
+ if (rows.length === 0)
1415
+ throw new Error(`${file}: no rows to stage`);
1416
+ const objectsFlag = option(rest, "--objects");
1417
+ const objectType = objectsFlag
1418
+ ? parseSingleObjectType(objectsFlag)
1419
+ : inferIngestObjectType(config, source, rows);
1420
+ const today = new Date().toISOString().slice(0, 10);
1421
+ const baseLabel = option(rest, "--run-label") ?? `ingest-${source}-${today}`;
1422
+ let label = baseLabel;
1423
+ for (let suffix = 2; await store.get(label); suffix += 1) {
1424
+ if (option(rest, "--run-label")) {
1425
+ throw new Error(`Run "${baseLabel}" already exists — enrich runs are append-only; pick a new --run-label.`);
1426
+ }
1427
+ label = `${baseLabel}-${suffix}`;
1428
+ }
1429
+ const now = new Date().toISOString();
1430
+ await store.append({
1431
+ id: enrichRunId(source, label),
1432
+ runLabel: label,
1433
+ source,
1434
+ mode: "ingest",
1435
+ startedAt: now,
1436
+ completedAt: now,
1437
+ cursor: null,
1438
+ counts: { fetched: rows.length, matched: 0, unmatched: 0, ambiguous: 0, opsEmitted: 0 },
1439
+ planIds: [],
1440
+ stamps: [],
1441
+ staged: rows,
1442
+ stagedObjectType: objectType,
1443
+ });
1444
+ console.log(`Staged ${rows.length} ${objectType} row(s) from ${file} as run ${label}. ` +
1445
+ `Next: fullstackgtm enrich append --source ${source} [source options] [--save]`);
1446
+ }
1447
+ function parseSingleObjectType(value) {
1448
+ const normalized = value.trim().toLowerCase();
1449
+ if (normalized === "companies" || normalized === "company")
1450
+ return "company";
1451
+ if (normalized === "contacts" || normalized === "contact")
1452
+ return "contact";
1453
+ throw new Error(`--objects must be companies or contacts (got "${value}")`);
1454
+ }
1455
+ async function enrichStatus(store, rest, configFile) {
1456
+ const sourceFilter = option(rest, "--source");
1457
+ const allRuns = (await store.list()).filter((run) => !sourceFilter || run.source === sourceFilter);
1458
+ if (allRuns.length === 0) {
1459
+ console.log(sourceFilter
1460
+ ? `No enrich runs for source "${sourceFilter}".`
1461
+ : "No enrich runs yet. Start with `fullstackgtm enrich append --save` or stage data with `enrich ingest`.");
1462
+ return;
1463
+ }
1464
+ // Staleness windows come from the config when one is readable; status must
1465
+ // not REQUIRE a config (the run store alone is enough to report on).
1466
+ let config = null;
1467
+ if (existsSync(configFile)) {
1468
+ try {
1469
+ config = loadEnrichConfig(configFile);
1470
+ }
1471
+ catch {
1472
+ config = null;
1473
+ }
1474
+ }
1475
+ const now = Date.now();
1476
+ const sources = Array.from(new Set(allRuns.map((run) => run.source)));
1477
+ const report = sources.map((source) => {
1478
+ const runs = allRuns.filter((run) => run.source === source);
1479
+ const last = runs[runs.length - 1];
1480
+ const interrupted = runs.filter((run) => run.completedAt === null);
1481
+ const stamps = Array.from(latestStamps(runs, source).values());
1482
+ const ages = stamps.map((stamp) => (now - Date.parse(stamp.enrichedAt)) / 86_400_000);
1483
+ const staleness = stamps.map((stamp, index) => {
1484
+ const windowDays = config
1485
+ ? staleDaysFor(config, stamp.objectType, stamp.field)
1486
+ : DEFAULT_STALE_DAYS;
1487
+ return ages[index] > windowDays;
1488
+ });
1489
+ return {
1490
+ source,
1491
+ runs: runs.length,
1492
+ lastRun: {
1493
+ runLabel: last.runLabel,
1494
+ mode: last.mode,
1495
+ startedAt: last.startedAt,
1496
+ completedAt: last.completedAt,
1497
+ counts: last.counts,
1498
+ planIds: last.planIds,
1499
+ },
1500
+ interrupted: interrupted.map((run) => ({ runLabel: run.runLabel, cursor: run.cursor })),
1501
+ stamps: {
1502
+ total: stamps.length,
1503
+ stale: staleness.filter(Boolean).length,
1504
+ oldestDays: ages.length ? Math.round(Math.max(...ages)) : null,
1505
+ newestDays: ages.length ? Math.round(Math.min(...ages)) : null,
1506
+ windowSource: config ? "enrich.config.json" : `default ${DEFAULT_STALE_DAYS}d`,
1507
+ },
1508
+ };
1509
+ });
1510
+ if (rest.includes("--json")) {
1511
+ console.log(JSON.stringify({ sources: report, runs: rest.includes("--runs") ? allRuns : undefined }, null, 2));
1512
+ return;
1513
+ }
1514
+ for (const entry of report) {
1515
+ const last = entry.lastRun;
1516
+ console.log(`${entry.source} — ${entry.runs} run(s)`);
1517
+ console.log(` last: ${last.runLabel} (${last.mode}) ${last.completedAt ? `completed ${last.completedAt}` : "INTERRUPTED"}` +
1518
+ ` · ${last.counts.fetched} fetched, ${last.counts.matched} matched, ${last.counts.unmatched} unmatched,` +
1519
+ ` ${last.counts.ambiguous} ambiguous, ${last.counts.opsEmitted} ops` +
1520
+ (last.planIds.length ? ` · plans: ${last.planIds.join(", ")}` : ""));
1521
+ for (const run of entry.interrupted) {
1522
+ console.log(` interrupted: ${run.runLabel} at cursor ${run.cursor ?? "(start)"} — re-run with --save to resume`);
1523
+ }
1524
+ console.log(` stamps: ${entry.stamps.total} field(s) enriched · ${entry.stamps.stale} stale (window: ${entry.stamps.windowSource})` +
1525
+ (entry.stamps.total ? ` · age ${entry.stamps.newestDays}–${entry.stamps.oldestDays}d` : ""));
1526
+ }
1527
+ if (rest.includes("--runs")) {
1528
+ console.log("");
1529
+ for (const run of allRuns) {
1530
+ console.log(`${run.runLabel} ${run.source.padEnd(8)} ${run.mode.padEnd(8)} ${run.completedAt ? "done" : "interrupted"}` +
1531
+ ` ${run.counts.opsEmitted} ops ${run.stamps.length} stamps${run.staged ? ` ${run.staged.length} staged` : ""}`);
1532
+ }
1533
+ }
1534
+ }
1075
1535
  /**
1076
1536
  * The resolve gate: exit 0 = safe to create, exit 2 = match found (exists or
1077
1537
  * ambiguous — do NOT blind-create), exit 1 = error. Built for sync jobs and
@@ -1128,27 +1588,194 @@ async function bulkUpdateCommand(args) {
1128
1588
  where,
1129
1589
  set: Object.keys(set).length > 0 ? set : undefined,
1130
1590
  archive: rest.includes("--archive"),
1591
+ forceArchiveDuplicates: rest.includes("--force-archive-duplicates"),
1131
1592
  createTask: option(rest, "--create-task") ?? undefined,
1132
1593
  require: repeatedOption(rest, "--require"),
1133
1594
  guard: repeatedOption(rest, "--guard"),
1134
1595
  reason: option(rest, "--reason") ?? undefined,
1135
1596
  maxOperations: numericOption(rest, "--max-operations"),
1136
1597
  });
1137
- const out = option(rest, "--out");
1598
+ await emitPlan(plan, rest);
1599
+ }
1600
+ /** Shared plan output plumbing: --out, --save (with the approve/apply hint), --json or markdown. */
1601
+ async function emitPlan(plan, args) {
1602
+ const out = option(args, "--out");
1138
1603
  if (out) {
1139
1604
  writeFileSync(resolve(process.cwd(), out), `${JSON.stringify(plan, null, 2)}\n`);
1140
1605
  }
1141
- if (rest.includes("--save")) {
1606
+ if (args.includes("--save")) {
1142
1607
  await createFilePlanStore().save(plan);
1143
1608
  console.error(`Saved plan ${plan.id} (${plan.operations.length} operations). Review with \`fullstackgtm plans show ${plan.id}\`, approve with \`fullstackgtm plans approve ${plan.id} --operations <ids|all>\`, then \`fullstackgtm apply --plan-id ${plan.id} --provider <name>\`.`);
1144
1609
  }
1145
- if (rest.includes("--json")) {
1610
+ if (args.includes("--json")) {
1146
1611
  console.log(JSON.stringify(plan, null, 2));
1147
1612
  }
1148
1613
  else {
1149
1614
  console.log(patchPlanToMarkdown(plan));
1150
1615
  }
1151
1616
  }
1617
+ /**
1618
+ * Governed duplicate cleanup: group by a normalized identity key, propose one
1619
+ * merge_records per duplicate group with a deterministic survivor. Never
1620
+ * writes — approve and apply the plan like any audit plan.
1621
+ */
1622
+ async function dedupeCommand(args) {
1623
+ const [objectType, ...rest] = args;
1624
+ if (!objectType || !["account", "contact", "deal"].includes(objectType)) {
1625
+ throw new Error("Usage: fullstackgtm dedupe <account|contact|deal> --key <domain|email|name> [--keep richest|oldest] [source options] [--reason <text>] [--max-operations <n>] [--save] [--out <path>] [--json]");
1626
+ }
1627
+ const key = option(rest, "--key");
1628
+ if (!key || !["domain", "email", "name"].includes(key)) {
1629
+ throw new Error("dedupe requires --key <domain|email|name> (the identity field duplicates share).");
1630
+ }
1631
+ const keep = option(rest, "--keep") ?? undefined;
1632
+ const snapshot = await readSnapshot(rest);
1633
+ const plan = buildDedupePlan(snapshot, {
1634
+ objectType: objectType,
1635
+ key: key,
1636
+ keep: (keep ?? undefined),
1637
+ reason: option(rest, "--reason") ?? undefined,
1638
+ maxOperations: numericOption(rest, "--max-operations"),
1639
+ });
1640
+ await emitPlan(plan, rest);
1641
+ }
1642
+ /**
1643
+ * Ownership handoff playbook: compile one bulk-update-style plan per object
1644
+ * type. Each plan carries its full filter, so eligibility (including the
1645
+ * --except-deal-stage exclusion) is re-verified per record at apply time.
1646
+ */
1647
+ async function reassignCommand(args) {
1648
+ const from = option(args, "--from");
1649
+ const to = option(args, "--to");
1650
+ if (!from || !to) {
1651
+ throw new Error("Usage: fullstackgtm reassign --from <ownerId> --to <ownerId> [--objects account,contact,deal] [--where <expr> …] [--except-deal-stage <stage>] [--include-closed-deals] [source options] [--reason <text>] [--max-operations <n>] [--save] [--out <path>] [--json]");
1652
+ }
1653
+ const objects = option(args, "--objects")
1654
+ ?.split(",")
1655
+ .map((value) => value.trim())
1656
+ .filter(Boolean);
1657
+ const snapshot = await readSnapshot(args);
1658
+ const plans = buildReassignPlans(snapshot, {
1659
+ fromOwnerId: from,
1660
+ toOwnerId: to,
1661
+ objects,
1662
+ where: repeatedOption(args, "--where"),
1663
+ exceptDealStage: option(args, "--except-deal-stage") ?? undefined,
1664
+ includeClosedDeals: args.includes("--include-closed-deals"),
1665
+ reason: option(args, "--reason") ?? undefined,
1666
+ maxOperations: numericOption(args, "--max-operations"),
1667
+ });
1668
+ const out = option(args, "--out");
1669
+ if (out) {
1670
+ writeFileSync(resolve(process.cwd(), out), `${JSON.stringify(plans, null, 2)}\n`);
1671
+ }
1672
+ if (args.includes("--json")) {
1673
+ console.log(JSON.stringify(plans, null, 2));
1674
+ return;
1675
+ }
1676
+ const store = args.includes("--save") ? createFilePlanStore() : null;
1677
+ for (const plan of plans) {
1678
+ if (store)
1679
+ await store.save(plan);
1680
+ console.log(`${plan.id} ${String(plan.operations.length).padStart(3)} operation(s) ${plan.title}`);
1681
+ console.log(` ${plan.summary}`);
1682
+ }
1683
+ if (store) {
1684
+ console.log(`\nSaved ${plans.length} plan(s). For each: \`fullstackgtm plans show <id>\`, \`fullstackgtm plans approve <id> --operations <ids|all>\`, then \`fullstackgtm apply --plan-id <id> --provider <name>\`.`);
1685
+ }
1686
+ else {
1687
+ console.log("\nDry run only — re-run with --save to store the plans for approval.");
1688
+ }
1689
+ }
1690
+ /**
1691
+ * One-shot composite for a single audit rule: audit → save → suggest →
1692
+ * approve only suggestion-backed operations meeting the confidence bar (plus
1693
+ * operations that carry concrete values and need no human input) → apply
1694
+ * (only with --yes). Every stage goes through the same gates as the manual
1695
+ * chain; placeholder values below the bar stay unapproved.
1696
+ */
1697
+ async function fixCommand(args) {
1698
+ const ruleId = option(args, "--rule");
1699
+ const provider = option(args, "--provider");
1700
+ if (!ruleId || !provider) {
1701
+ throw new Error("Usage: fullstackgtm fix --rule <ruleId> --provider <name> [--min-confidence high|low] [--include-creates] [--today <iso>] [--yes]");
1702
+ }
1703
+ const minConfidence = option(args, "--min-confidence") ?? "high";
1704
+ if (!["high", "low"].includes(minConfidence)) {
1705
+ throw new Error("--min-confidence must be high or low");
1706
+ }
1707
+ const includeCreates = args.includes("--include-creates");
1708
+ const loaded = loadConfig(option(args, "--config") ?? undefined);
1709
+ const configured = await resolveConfiguredRules(loaded);
1710
+ const rule = configured.find((candidate) => candidate.id === ruleId);
1711
+ if (!rule) {
1712
+ throw new Error(`Unknown rule: ${ruleId}. Available rules: ${configured.map((r) => r.id).join(", ")}`);
1713
+ }
1714
+ const policy = mergePolicy(defaultPolicy(), loaded?.config);
1715
+ const today = option(args, "--today");
1716
+ if (today)
1717
+ policy.today = today;
1718
+ const snapshot = await readSnapshot(args);
1719
+ const plan = auditSnapshot(snapshot, policy, [rule]);
1720
+ if (plan.operations.length === 0) {
1721
+ console.log(`fix ${ruleId}: audit proposed 0 operations — nothing to fix.`);
1722
+ return;
1723
+ }
1724
+ const store = createFilePlanStore();
1725
+ await store.save(plan);
1726
+ const suggestions = suggestValues(plan, snapshot);
1727
+ const accepted = new Set(minConfidence === "low" ? ["high", "low"] : ["high"]);
1728
+ const overrides = {};
1729
+ let belowBar = 0;
1730
+ for (const suggestion of suggestions) {
1731
+ if (suggestion.suggestedValue &&
1732
+ (accepted.has(suggestion.confidence) || (includeCreates && suggestion.confidence === "create"))) {
1733
+ overrides[suggestion.operationId] = suggestion.suggestedValue;
1734
+ }
1735
+ else {
1736
+ belowBar += 1;
1737
+ }
1738
+ }
1739
+ // Approve operations whose placeholder got a qualifying suggested value,
1740
+ // plus operations that already carry a concrete value (no human input
1741
+ // needed — nothing to guess). Everything else stays unapproved.
1742
+ const placeholderIds = new Set(suggestions.map((suggestion) => suggestion.operationId));
1743
+ const approvedIds = plan.operations
1744
+ .map((operation) => operation.id)
1745
+ .filter((id) => overrides[id] !== undefined || !placeholderIds.has(id));
1746
+ const lines = [
1747
+ `fix ${ruleId} via ${provider}:`,
1748
+ ` proposed: ${plan.operations.length} operation(s) — plan ${plan.id} (saved)`,
1749
+ ` suggested: ${Object.keys(overrides).length} value(s) at ${minConfidence}+ confidence${includeCreates ? " (creates included)" : ""}${belowBar > 0 ? `; ${belowBar} below the bar (left unapproved)` : ""}`,
1750
+ ` approved: ${approvedIds.length} of ${plan.operations.length}`,
1751
+ ];
1752
+ if (approvedIds.length === 0) {
1753
+ lines.push(" applied: 0 — no operation met the confidence bar");
1754
+ console.log(lines.join("\n"));
1755
+ console.log(`\nWiden with --min-confidence low / --include-creates, or approve manually: \`fullstackgtm plans approve ${plan.id} --operations <ids> --value <opId>=<value>\`.`);
1756
+ return;
1757
+ }
1758
+ await store.approveOperations(plan.id, approvedIds, overrides);
1759
+ if (!args.includes("--yes")) {
1760
+ lines.push(" applied: 0 (stopped before apply — pass --yes to write)");
1761
+ console.log(lines.join("\n"));
1762
+ console.log(`\nApply with:\n fullstackgtm apply --plan-id ${plan.id} --provider ${provider}`);
1763
+ return;
1764
+ }
1765
+ const connector = await connectorFor(provider, args);
1766
+ const run = await applyPatchPlan(connector, plan, {
1767
+ approvedOperationIds: approvedIds,
1768
+ valueOverrides: overrides,
1769
+ });
1770
+ await store.recordRun(plan.id, run);
1771
+ const counts = { applied: 0, conflict: 0, skipped: 0, failed: 0 };
1772
+ for (const result of run.results)
1773
+ counts[result.status] = (counts[result.status] ?? 0) + 1;
1774
+ lines.push(` applied: ${counts.applied} · conflicts: ${counts.conflict} · skipped: ${counts.skipped} · failed: ${counts.failed}`);
1775
+ console.log(lines.join("\n"));
1776
+ if (run.status === "failed")
1777
+ process.exitCode = 1;
1778
+ }
1152
1779
  async function suggest(args) {
1153
1780
  const planId = option(args, "--plan-id");
1154
1781
  const planPath = option(args, "--plan");
@@ -1652,8 +2279,27 @@ async function login(args) {
1652
2279
  console.log(`Stored ${provider} API key in ${credentialsPath()}. \`fullstackgtm call parse\` and \`call score\` use it automatically.`);
1653
2280
  return;
1654
2281
  }
2282
+ if (provider === "apollo") {
2283
+ rejectArgvSecret(args, "--token", "--key", "--api-key");
2284
+ const key = await readSecret("Apollo API key");
2285
+ if (!key)
2286
+ throw new Error("No Apollo key provided.");
2287
+ if (!args.includes("--no-validate")) {
2288
+ const response = await fetch("https://api.apollo.io/api/v1/auth/health", {
2289
+ headers: { "X-Api-Key": key, Accept: "application/json" },
2290
+ });
2291
+ if (!response.ok) {
2292
+ throw new Error(`Apollo rejected the key: ${safeStatus(response)}`);
2293
+ }
2294
+ console.log("Key accepted by the Apollo API.");
2295
+ }
2296
+ const stamp = new Date().toISOString();
2297
+ storeCredential("apollo", { kind: "api_key", accessToken: key, createdAt: stamp, updatedAt: stamp });
2298
+ console.log(`Stored Apollo API key in ${credentialsPath()}. \`fullstackgtm enrich append|refresh\` use it automatically.`);
2299
+ return;
2300
+ }
1655
2301
  if (provider !== "hubspot") {
1656
- throw new Error("login supports: hubspot, salesforce, stripe, anthropic, openai, or --via <hosted url>. Usage: fullstackgtm login <provider> | fullstackgtm login --via https://gtm.example.com");
2302
+ throw new Error("login supports: hubspot, salesforce, stripe, anthropic, openai, apollo, or --via <hosted url>. Usage: fullstackgtm login <provider> | fullstackgtm login --via https://gtm.example.com");
1657
2303
  }
1658
2304
  const now = new Date().toISOString();
1659
2305
  if (args.includes("--oauth")) {
@@ -1860,8 +2506,8 @@ export async function runCli(argv) {
1860
2506
  }
1861
2507
  // Commands without bespoke help fall back to the top-level usage on --help
1862
2508
  // instead of executing (audit used to silently run the sample audit).
1863
- // call/market/bulk-update print their own richer help.
1864
- if (!["call", "market", "bulk-update"].includes(command) && (args.includes("--help") || args.includes("-h"))) {
2509
+ // call/market/enrich/bulk-update print their own richer help.
2510
+ if (!["call", "market", "enrich", "bulk-update"].includes(command) && (args.includes("--help") || args.includes("-h"))) {
1865
2511
  console.log(usage());
1866
2512
  return;
1867
2513
  }
@@ -1909,10 +2555,26 @@ export async function runCli(argv) {
1909
2555
  await bulkUpdateCommand(args);
1910
2556
  return;
1911
2557
  }
2558
+ if (command === "dedupe") {
2559
+ await dedupeCommand(args);
2560
+ return;
2561
+ }
2562
+ if (command === "reassign") {
2563
+ await reassignCommand(args);
2564
+ return;
2565
+ }
2566
+ if (command === "fix") {
2567
+ await fixCommand(args);
2568
+ return;
2569
+ }
1912
2570
  if (command === "market") {
1913
2571
  await marketCommand(args);
1914
2572
  return;
1915
2573
  }
2574
+ if (command === "enrich") {
2575
+ await enrichCommand(args);
2576
+ return;
2577
+ }
1916
2578
  if (command === "profiles") {
1917
2579
  profilesCommand(args);
1918
2580
  return;