@nebulacomponents/citable 1.5.1 → 1.7.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.
Files changed (142) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/README.md +13 -6
  3. package/dist/universal/.agents/skills/citable/SKILL.md +18 -2
  4. package/dist/universal/.agents/skills/citable/VERSION +1 -1
  5. package/dist/universal/.agents/skills/citable/commands/measurement.md +31 -1
  6. package/dist/universal/.agents/skills/citable/manifest.json +12 -8
  7. package/dist/universal/.agents/skills/citable/references/evidence-authority.md +43 -0
  8. package/dist/universal/.agents/skills/citable/schemas/bing-webmaster.schema.json +25 -0
  9. package/dist/universal/.agents/skills/citable/schemas/connection.schema.json +1 -0
  10. package/dist/universal/.agents/skills/citable/schemas/crawler-identity.schema.json +42 -0
  11. package/dist/universal/.agents/skills/citable/schemas/evidence-authority.schema.json +15 -0
  12. package/dist/universal/.agents/skills/citable/schemas/observation.schema.json +3 -2
  13. package/dist/universal/.claude/skills/citable/SKILL.md +18 -2
  14. package/dist/universal/.claude/skills/citable/VERSION +1 -1
  15. package/dist/universal/.claude/skills/citable/commands/measurement.md +31 -1
  16. package/dist/universal/.claude/skills/citable/manifest.json +12 -8
  17. package/dist/universal/.claude/skills/citable/references/evidence-authority.md +43 -0
  18. package/dist/universal/.claude/skills/citable/schemas/bing-webmaster.schema.json +25 -0
  19. package/dist/universal/.claude/skills/citable/schemas/connection.schema.json +1 -0
  20. package/dist/universal/.claude/skills/citable/schemas/crawler-identity.schema.json +42 -0
  21. package/dist/universal/.claude/skills/citable/schemas/evidence-authority.schema.json +15 -0
  22. package/dist/universal/.claude/skills/citable/schemas/observation.schema.json +3 -2
  23. package/dist/universal/.cursor/skills/citable/SKILL.md +18 -2
  24. package/dist/universal/.cursor/skills/citable/VERSION +1 -1
  25. package/dist/universal/.cursor/skills/citable/commands/measurement.md +31 -1
  26. package/dist/universal/.cursor/skills/citable/manifest.json +12 -8
  27. package/dist/universal/.cursor/skills/citable/references/evidence-authority.md +43 -0
  28. package/dist/universal/.cursor/skills/citable/schemas/bing-webmaster.schema.json +25 -0
  29. package/dist/universal/.cursor/skills/citable/schemas/connection.schema.json +1 -0
  30. package/dist/universal/.cursor/skills/citable/schemas/crawler-identity.schema.json +42 -0
  31. package/dist/universal/.cursor/skills/citable/schemas/evidence-authority.schema.json +15 -0
  32. package/dist/universal/.cursor/skills/citable/schemas/observation.schema.json +3 -2
  33. package/dist/universal/.gemini/skills/citable/SKILL.md +18 -2
  34. package/dist/universal/.gemini/skills/citable/VERSION +1 -1
  35. package/dist/universal/.gemini/skills/citable/commands/measurement.md +31 -1
  36. package/dist/universal/.gemini/skills/citable/manifest.json +12 -8
  37. package/dist/universal/.gemini/skills/citable/references/evidence-authority.md +43 -0
  38. package/dist/universal/.gemini/skills/citable/schemas/bing-webmaster.schema.json +25 -0
  39. package/dist/universal/.gemini/skills/citable/schemas/connection.schema.json +1 -0
  40. package/dist/universal/.gemini/skills/citable/schemas/crawler-identity.schema.json +42 -0
  41. package/dist/universal/.gemini/skills/citable/schemas/evidence-authority.schema.json +15 -0
  42. package/dist/universal/.gemini/skills/citable/schemas/observation.schema.json +3 -2
  43. package/dist/universal/.github/skills/citable/SKILL.md +18 -2
  44. package/dist/universal/.github/skills/citable/VERSION +1 -1
  45. package/dist/universal/.github/skills/citable/commands/measurement.md +31 -1
  46. package/dist/universal/.github/skills/citable/manifest.json +12 -8
  47. package/dist/universal/.github/skills/citable/references/evidence-authority.md +43 -0
  48. package/dist/universal/.github/skills/citable/schemas/bing-webmaster.schema.json +25 -0
  49. package/dist/universal/.github/skills/citable/schemas/connection.schema.json +1 -0
  50. package/dist/universal/.github/skills/citable/schemas/crawler-identity.schema.json +42 -0
  51. package/dist/universal/.github/skills/citable/schemas/evidence-authority.schema.json +15 -0
  52. package/dist/universal/.github/skills/citable/schemas/observation.schema.json +3 -2
  53. package/dist/universal/.kiro/skills/citable/SKILL.md +18 -2
  54. package/dist/universal/.kiro/skills/citable/VERSION +1 -1
  55. package/dist/universal/.kiro/skills/citable/commands/measurement.md +31 -1
  56. package/dist/universal/.kiro/skills/citable/manifest.json +12 -8
  57. package/dist/universal/.kiro/skills/citable/references/evidence-authority.md +43 -0
  58. package/dist/universal/.kiro/skills/citable/schemas/bing-webmaster.schema.json +25 -0
  59. package/dist/universal/.kiro/skills/citable/schemas/connection.schema.json +1 -0
  60. package/dist/universal/.kiro/skills/citable/schemas/crawler-identity.schema.json +42 -0
  61. package/dist/universal/.kiro/skills/citable/schemas/evidence-authority.schema.json +15 -0
  62. package/dist/universal/.kiro/skills/citable/schemas/observation.schema.json +3 -2
  63. package/dist/universal/.opencode/skills/citable/SKILL.md +18 -2
  64. package/dist/universal/.opencode/skills/citable/VERSION +1 -1
  65. package/dist/universal/.opencode/skills/citable/commands/measurement.md +31 -1
  66. package/dist/universal/.opencode/skills/citable/manifest.json +12 -8
  67. package/dist/universal/.opencode/skills/citable/references/evidence-authority.md +43 -0
  68. package/dist/universal/.opencode/skills/citable/schemas/bing-webmaster.schema.json +25 -0
  69. package/dist/universal/.opencode/skills/citable/schemas/connection.schema.json +1 -0
  70. package/dist/universal/.opencode/skills/citable/schemas/crawler-identity.schema.json +42 -0
  71. package/dist/universal/.opencode/skills/citable/schemas/evidence-authority.schema.json +15 -0
  72. package/dist/universal/.opencode/skills/citable/schemas/observation.schema.json +3 -2
  73. package/dist/universal/.pi/agent/skills/citable/SKILL.md +18 -2
  74. package/dist/universal/.pi/agent/skills/citable/VERSION +1 -1
  75. package/dist/universal/.pi/agent/skills/citable/commands/measurement.md +31 -1
  76. package/dist/universal/.pi/agent/skills/citable/manifest.json +12 -8
  77. package/dist/universal/.pi/agent/skills/citable/references/evidence-authority.md +43 -0
  78. package/dist/universal/.pi/agent/skills/citable/schemas/bing-webmaster.schema.json +25 -0
  79. package/dist/universal/.pi/agent/skills/citable/schemas/connection.schema.json +1 -0
  80. package/dist/universal/.pi/agent/skills/citable/schemas/crawler-identity.schema.json +42 -0
  81. package/dist/universal/.pi/agent/skills/citable/schemas/evidence-authority.schema.json +15 -0
  82. package/dist/universal/.pi/agent/skills/citable/schemas/observation.schema.json +3 -2
  83. package/dist/universal/.qoder/skills/citable/SKILL.md +18 -2
  84. package/dist/universal/.qoder/skills/citable/VERSION +1 -1
  85. package/dist/universal/.qoder/skills/citable/commands/measurement.md +31 -1
  86. package/dist/universal/.qoder/skills/citable/manifest.json +12 -8
  87. package/dist/universal/.qoder/skills/citable/references/evidence-authority.md +43 -0
  88. package/dist/universal/.qoder/skills/citable/schemas/bing-webmaster.schema.json +25 -0
  89. package/dist/universal/.qoder/skills/citable/schemas/connection.schema.json +1 -0
  90. package/dist/universal/.qoder/skills/citable/schemas/crawler-identity.schema.json +42 -0
  91. package/dist/universal/.qoder/skills/citable/schemas/evidence-authority.schema.json +15 -0
  92. package/dist/universal/.qoder/skills/citable/schemas/observation.schema.json +3 -2
  93. package/dist/universal/.rovodev/skills/citable/SKILL.md +18 -2
  94. package/dist/universal/.rovodev/skills/citable/VERSION +1 -1
  95. package/dist/universal/.rovodev/skills/citable/commands/measurement.md +31 -1
  96. package/dist/universal/.rovodev/skills/citable/manifest.json +12 -8
  97. package/dist/universal/.rovodev/skills/citable/references/evidence-authority.md +43 -0
  98. package/dist/universal/.rovodev/skills/citable/schemas/bing-webmaster.schema.json +25 -0
  99. package/dist/universal/.rovodev/skills/citable/schemas/connection.schema.json +1 -0
  100. package/dist/universal/.rovodev/skills/citable/schemas/crawler-identity.schema.json +42 -0
  101. package/dist/universal/.rovodev/skills/citable/schemas/evidence-authority.schema.json +15 -0
  102. package/dist/universal/.rovodev/skills/citable/schemas/observation.schema.json +3 -2
  103. package/dist/universal/.trae/skills/citable/SKILL.md +18 -2
  104. package/dist/universal/.trae/skills/citable/VERSION +1 -1
  105. package/dist/universal/.trae/skills/citable/commands/measurement.md +31 -1
  106. package/dist/universal/.trae/skills/citable/manifest.json +12 -8
  107. package/dist/universal/.trae/skills/citable/references/evidence-authority.md +43 -0
  108. package/dist/universal/.trae/skills/citable/schemas/bing-webmaster.schema.json +25 -0
  109. package/dist/universal/.trae/skills/citable/schemas/connection.schema.json +1 -0
  110. package/dist/universal/.trae/skills/citable/schemas/crawler-identity.schema.json +42 -0
  111. package/dist/universal/.trae/skills/citable/schemas/evidence-authority.schema.json +15 -0
  112. package/dist/universal/.trae/skills/citable/schemas/observation.schema.json +3 -2
  113. package/dist/universal/.trae-cn/skills/citable/SKILL.md +18 -2
  114. package/dist/universal/.trae-cn/skills/citable/VERSION +1 -1
  115. package/dist/universal/.trae-cn/skills/citable/commands/measurement.md +31 -1
  116. package/dist/universal/.trae-cn/skills/citable/manifest.json +12 -8
  117. package/dist/universal/.trae-cn/skills/citable/references/evidence-authority.md +43 -0
  118. package/dist/universal/.trae-cn/skills/citable/schemas/bing-webmaster.schema.json +25 -0
  119. package/dist/universal/.trae-cn/skills/citable/schemas/connection.schema.json +1 -0
  120. package/dist/universal/.trae-cn/skills/citable/schemas/crawler-identity.schema.json +42 -0
  121. package/dist/universal/.trae-cn/skills/citable/schemas/evidence-authority.schema.json +15 -0
  122. package/dist/universal/.trae-cn/skills/citable/schemas/observation.schema.json +3 -2
  123. package/dist/universal/manifest.json +147 -99
  124. package/package.json +1 -1
  125. package/schemas/bing-webmaster.schema.json +25 -0
  126. package/schemas/connection.schema.json +1 -0
  127. package/schemas/crawler-identity.schema.json +42 -0
  128. package/schemas/evidence-authority.schema.json +15 -0
  129. package/schemas/observation.schema.json +3 -2
  130. package/skill/SKILL.md +18 -2
  131. package/skill/commands/measurement.md +31 -1
  132. package/skill/references/evidence-authority.md +43 -0
  133. package/src/cli/index.js +44 -1
  134. package/src/commands/connect.js +90 -0
  135. package/src/commands/observe.js +81 -22
  136. package/src/connectors/ga4.js +59 -0
  137. package/src/connectors/gsc.js +41 -0
  138. package/src/connectors/http.js +19 -0
  139. package/src/connectors/index.js +14 -0
  140. package/src/observations/authority.js +13 -0
  141. package/src/observations/common.js +3 -1
  142. package/src/observations/crawlerIdentity.js +60 -0
@@ -0,0 +1,59 @@
1
+ import { providerRequest } from './http.js';
2
+
3
+ const DATA = 'https://analyticsdata.googleapis.com/v1beta';
4
+ const ADMIN = 'https://analyticsadmin.googleapis.com/v1beta';
5
+ const METRICS = {
6
+ sessions: { unit: 'count', value_type: 'integer' },
7
+ engagedSessions: { unit: 'count', value_type: 'integer' },
8
+ keyEvents: { unit: 'count', value_type: 'number' },
9
+ totalRevenue: { unit: 'currency', value_type: 'number' },
10
+ };
11
+ const DIMENSIONS = new Set(['date', 'landingPagePlusQueryString', 'sessionDefaultChannelGroup', 'country', 'deviceCategory']);
12
+
13
+ export const ga4Connector = {
14
+ provider: 'ga4', defaultCredentialEnv: 'GA4_ACCESS_TOKEN', readOnlyScopes: ['https://www.googleapis.com/auth/analytics.readonly'],
15
+ describeMetrics() { return METRICS; },
16
+ async discoverProperties(context) {
17
+ const properties = [];
18
+ let pageToken;
19
+ do {
20
+ const query = `?pageSize=200${pageToken ? `&pageToken=${encodeURIComponent(pageToken)}` : ''}`;
21
+ const result = await providerRequest(`${ADMIN}/accountSummaries${query}`, context);
22
+ for (const account of result.accountSummaries || []) for (const property of account.propertySummaries || []) {
23
+ properties.push({ property_id: property.property.replace(/^properties\//, ''), display_name: property.displayName, account: account.account });
24
+ }
25
+ pageToken = result.nextPageToken;
26
+ } while (pageToken);
27
+ return properties;
28
+ },
29
+ async validateConnection(connection, context) {
30
+ const properties = await this.discoverProperties(context);
31
+ return { valid: properties.some((item) => item.property_id === String(connection.property_id)), properties };
32
+ },
33
+ async sync(connection, metrics, { startDate, endDate, ...context }) {
34
+ const dimensions = [...new Set(metrics.flatMap((metric) => metric.dimensions).filter((item) => DIMENSIONS.has(item)))];
35
+ if (!dimensions.includes('date')) dimensions.unshift('date');
36
+ if (!dimensions.includes('landingPagePlusQueryString')) dimensions.push('landingPagePlusQueryString');
37
+ const rows = [];
38
+ const limitations = [];
39
+ let offset = 0;
40
+ do {
41
+ const result = await providerRequest(`${DATA}/properties/${encodeURIComponent(connection.property_id)}:runReport`, {
42
+ ...context, method: 'POST', body: {
43
+ dateRanges: [{ startDate, endDate }], dimensions: dimensions.map((name) => ({ name })),
44
+ metrics: metrics.map((metric) => ({ name: metric.external_name })), limit: '100000', offset: String(offset),
45
+ dimensionFilter: { filter: { fieldName: 'sessionDefaultChannelGroup', stringFilter: { matchType: 'EXACT', value: 'Organic Search' } } },
46
+ },
47
+ });
48
+ for (const row of result.rows || []) {
49
+ const values = Object.fromEntries(dimensions.map((name, index) => [name === 'landingPagePlusQueryString' ? 'url' : name, row.dimensionValues[index]?.value]));
50
+ const date = values.date?.replace(/^(\d{4})(\d{2})(\d{2})$/, '$1-$2-$3');
51
+ rows.push(...metrics.map((metric, index) => ({ metric, value: Number(row.metricValues[index]?.value), dimensions: values, observed_at: `${date}T00:00:00.000Z` })));
52
+ }
53
+ if (result.metadata?.dataLossFromOtherRow) limitations.push('Provider reports data loss from the (other) row.');
54
+ offset += (result.rows || []).length;
55
+ if (!result.rowCount || offset >= result.rowCount || !(result.rows || []).length) break;
56
+ } while (true);
57
+ return { rows, cursor: endDate, limitations: [...new Set([...limitations, 'GA4 reporting identity, thresholding, attribution, and consent configuration affect results.', 'Sync is filtered to Organic Search sessions.'])] };
58
+ },
59
+ };
@@ -0,0 +1,41 @@
1
+ import { providerRequest } from './http.js';
2
+
3
+ const BASE = 'https://www.googleapis.com/webmasters/v3';
4
+ const METRICS = {
5
+ clicks: { unit: 'count', value_type: 'integer' },
6
+ impressions: { unit: 'count', value_type: 'integer' },
7
+ ctr: { unit: 'ratio', value_type: 'number' },
8
+ position: { unit: 'position', value_type: 'number' },
9
+ };
10
+ const DIMENSIONS = new Set(['date', 'query', 'page', 'country', 'device', 'searchAppearance']);
11
+
12
+ export const gscConnector = {
13
+ provider: 'gsc', defaultCredentialEnv: 'GSC_ACCESS_TOKEN', readOnlyScopes: ['https://www.googleapis.com/auth/webmasters.readonly'],
14
+ describeMetrics() { return METRICS; },
15
+ async discoverProperties(context) {
16
+ const result = await providerRequest(`${BASE}/sites`, context);
17
+ return (result.siteEntry || []).map((item) => ({ property_id: item.siteUrl, permission: item.permissionLevel }));
18
+ },
19
+ async validateConnection(connection, context) {
20
+ const properties = await this.discoverProperties(context);
21
+ return { valid: properties.some((item) => item.property_id === connection.property_id), properties };
22
+ },
23
+ async sync(connection, metrics, { startDate, endDate, ...context }) {
24
+ const dimensions = [...new Set(metrics.flatMap((metric) => metric.dimensions).filter((item) => DIMENSIONS.has(item)))];
25
+ if (!dimensions.includes('date')) dimensions.unshift('date');
26
+ const rows = [];
27
+ let startRow = 0;
28
+ do {
29
+ const result = await providerRequest(`${BASE}/sites/${encodeURIComponent(connection.property_id)}/searchAnalytics/query`, {
30
+ ...context, method: 'POST', body: { startDate, endDate, dimensions, rowLimit: 25000, startRow, dataState: 'final' },
31
+ });
32
+ for (const row of result.rows || []) {
33
+ const values = Object.fromEntries(dimensions.map((name, index) => [name === 'page' ? 'url' : name, row.keys[index]]));
34
+ for (const metric of metrics) rows.push({ metric, value: row[metric.external_name], dimensions: values, observed_at: `${values.date}T00:00:00.000Z` });
35
+ }
36
+ if ((result.rows || []).length < 25000) break;
37
+ startRow += 25000;
38
+ } while (true);
39
+ return { rows, cursor: endDate, limitations: ['Search Console privacy filtering and aggregation apply.', 'Search Analytics does not guarantee every data row; the API can return top rows only.', 'Final data can still be revised by the provider.'] };
40
+ },
41
+ };
@@ -0,0 +1,19 @@
1
+ const RETRYABLE = new Set([429, 500, 502, 503, 504]);
2
+
3
+ export async function providerRequest(url, { token, fetchImpl = globalThis.fetch, method = 'GET', body, attempts = 3 } = {}) {
4
+ if (!token) throw Object.assign(new Error('authorization is not configured'), { connectorState: 'not_configured' });
5
+ let response;
6
+ for (let attempt = 1; attempt <= attempts; attempt++) {
7
+ response = await fetchImpl(url, {
8
+ method,
9
+ headers: { authorization: `Bearer ${token}`, accept: 'application/json', ...(body ? { 'content-type': 'application/json' } : {}) },
10
+ body: body ? JSON.stringify(body) : undefined,
11
+ });
12
+ if (response.ok) return response.json();
13
+ const detail = (await response.text()).slice(0, 300);
14
+ if (!RETRYABLE.has(response.status) || attempt === attempts) {
15
+ const states = { 401: 'authorization_expired', 403: 'permission_denied', 429: 'quota_limited' };
16
+ throw Object.assign(new Error(`provider returned ${response.status}: ${detail}`), { connectorState: states[response.status] || 'temporarily_unavailable' });
17
+ }
18
+ }
19
+ }
@@ -0,0 +1,14 @@
1
+ import { gscConnector } from './gsc.js';
2
+ import { ga4Connector } from './ga4.js';
3
+
4
+ const CONNECTORS = new Map([gscConnector, ga4Connector].map((connector) => [connector.provider, connector]));
5
+
6
+ export function getConnector(provider) {
7
+ const connector = CONNECTORS.get(String(provider).toLowerCase());
8
+ if (!connector) throw new Error(`unsupported connector provider: ${provider}`);
9
+ return connector;
10
+ }
11
+
12
+ export function listConnectors() {
13
+ return [...CONNECTORS.values()].map((item) => ({ provider: item.provider, credential_env: item.defaultCredentialEnv, scopes: item.readOnlyScopes, metrics: Object.keys(item.describeMetrics()) }));
14
+ }
@@ -0,0 +1,13 @@
1
+ const DEFAULTS = {
2
+ live_api: ['provider_published', 'direct_api', 'transport_authenticated', 'unknown'],
3
+ browser: ['synthetic', 'browser_capture', 'checksum_protected_only', 'single_observation'],
4
+ owner_import: ['owner_controlled', 'owner_export', 'checksum_protected_only', 'unknown'],
5
+ synthetic_fetch: ['synthetic', 'synthetic_probe', 'checksum_protected_only', 'single_observation'],
6
+ static_analysis: ['inferred', 'static_analysis', 'checksum_protected_only', 'single_observation'],
7
+ human_review: ['owner_controlled', 'manual_entry', 'owner_attested', 'single_observation'],
8
+ };
9
+
10
+ export function evidenceAuthority(method, overrides = {}) {
11
+ const [source_authority, collection_authority, authenticity_status, representativeness] = DEFAULTS[method] || DEFAULTS.static_analysis;
12
+ return { source_authority, collection_authority, authenticity_status, representativeness, ...overrides };
13
+ }
@@ -3,6 +3,7 @@ import path from 'node:path';
3
3
  import { createRun } from '../evidence/run.js';
4
4
  import { nowIso, readJson, sha256 } from '../shared/io.js';
5
5
  import { validateAgainst } from '../shared/schemaValidator.js';
6
+ import { evidenceAuthority } from './authority.js';
6
7
 
7
8
  export function readInput(input) {
8
9
  if (!input) throw new Error('--input <json> is required for this collector');
@@ -10,12 +11,13 @@ export function readInput(input) {
10
11
  return { raw: fs.readFileSync(input, 'utf8'), value: readJson(input), file: path.resolve(input) };
11
12
  }
12
13
 
13
- export function envelope(kind, data, { method, source, state = 'observed', confidence = 'confirmed', limitations = [], raw } = {}) {
14
+ export function envelope(kind, data, { method, source, state = 'observed', confidence = 'confirmed', limitations = [], authority, raw } = {}) {
14
15
  const evidence = raw ?? JSON.stringify(data);
15
16
  const item = {
16
17
  observation_id: `OBS-${kind.toUpperCase()}-${sha256(evidence).slice(0, 16)}`,
17
18
  kind, state, collected_at: nowIso(), collection_method: method,
18
19
  confidence, source, evidence_hash: sha256(evidence), data, limitations,
20
+ authority: evidenceAuthority(method, authority),
19
21
  };
20
22
  const check = validateAgainst('observation.schema.json', item);
21
23
  if (!check.valid) throw new Error(`observation violates contract: ${check.errors.join('; ')}`);
@@ -0,0 +1,60 @@
1
+ import net from 'node:net';
2
+ import { sha256 } from '../shared/io.js';
3
+ import { validateAgainst } from '../shared/schemaValidator.js';
4
+
5
+ function cidrMembership(sourceIp, cidrs) {
6
+ if (!sourceIp) return 'not_tested';
7
+ const family = net.isIP(sourceIp);
8
+ if (!family) return 'invalid';
9
+ const block = new net.BlockList();
10
+ for (const cidr of cidrs || []) {
11
+ const [network, prefixText] = String(cidr).split('/');
12
+ const prefix = Number(prefixText), rangeFamily = net.isIP(network);
13
+ if (rangeFamily === family && Number.isInteger(prefix)) block.addSubnet(network, prefix, family === 4 ? 'ipv4' : 'ipv6');
14
+ }
15
+ return block.check(sourceIp, family === 4 ? 'ipv4' : 'ipv6') ? 'matched' : 'not_matched';
16
+ }
17
+
18
+ export function crawlerIdentity(row, { ranges = {}, rangeSources = {}, collector = 'owner_import' } = {}) {
19
+ const ua = row.user_agent, cidrs = ranges[ua] || [], source = rangeSources[ua] || {};
20
+ const sourceSnapshot = source.source_snapshot;
21
+ const expectedChecksum = sourceSnapshot == null ? null : sha256(typeof sourceSnapshot === 'string' ? sourceSnapshot : JSON.stringify(sourceSnapshot));
22
+ const rangeSource = {
23
+ url: source.url || null,
24
+ retrieved_at: source.retrieved_at || null,
25
+ authority: source.authority || 'unknown',
26
+ checksum: source.checksum || null,
27
+ checksum_scope: sourceSnapshot == null ? 'not_captured' : 'source_snapshot',
28
+ checksum_valid: Boolean(source.checksum && expectedChecksum && source.checksum === expectedChecksum),
29
+ };
30
+ const membership = cidrMembership(row.source_ip, cidrs);
31
+ const dns = { status: row.dns_status || 'not_tested', method: row.dns_method || null };
32
+ const edge = { observed: row.edge_observed === true, decision: row.edge_decision || null };
33
+ const origin = { observed: row.origin_observed === true, status: row.origin_status ?? null };
34
+ const stages = ['declared'];
35
+ if (row.synthetic === true) stages.push('synthetically_observed');
36
+ if (membership === 'matched') stages.push('range_matched');
37
+ if (dns.status === 'verified') stages.push('dns_verified');
38
+ if (edge.observed) stages.push('edge_observed');
39
+ if (origin.observed) stages.push('origin_observed');
40
+ const contradictory = membership === 'not_matched' || dns.status === 'failed' || (row.claimed_verified === true && (!rangeSource.checksum_valid || membership !== 'matched'));
41
+ const complete = identityProvider(row, source) !== 'unknown' && row.synthetic !== true && rangeSource.url && rangeSource.retrieved_at && rangeSource.authority === 'provider_published' && rangeSource.checksum_scope === 'source_snapshot' && rangeSource.checksum_valid && membership === 'matched' && dns.status === 'verified' && edge.observed && origin.observed;
42
+ if (contradictory) stages.push('contradictory');
43
+ else if (complete) stages.push('fully_verified');
44
+ else stages.push('insufficient_evidence');
45
+ const precedence = ['contradictory', 'fully_verified', 'origin_observed', 'edge_observed', 'dns_verified', 'range_matched', 'synthetically_observed', 'declared'];
46
+ const verificationStatus = precedence.find((state) => stages.includes(state));
47
+ const identity = {
48
+ provider: identityProvider(row, source), declared_user_agent: ua,
49
+ observed_source_ip: row.source_ip || null, range_source: rangeSource,
50
+ cidr_membership: membership, dns, edge, origin, region: row.region || null,
51
+ collector, stages, verification_status: verificationStatus,
52
+ };
53
+ const check = validateAgainst('crawler-identity.schema.json', identity);
54
+ if (!check.valid) throw new Error(`crawler identity violates contract: ${check.errors.join('; ')}`);
55
+ return identity;
56
+ }
57
+
58
+ function identityProvider(row, source) {
59
+ return row.provider || source.provider || 'unknown';
60
+ }