@jacebenson/jsn 5.5.10 → 5.5.11-dev.241.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.
Files changed (51) hide show
  1. package/README.md +25 -0
  2. package/package.json +1 -1
  3. package/src/access-analyzer.js +92 -0
  4. package/src/app.js +5 -0
  5. package/src/auth/credential-store.js +3 -3
  6. package/src/auth/windows-credential-store.ps1 +1 -1
  7. package/src/cli-adapter.js +53 -3
  8. package/src/cli.js +17 -3
  9. package/src/command-audience.js +3 -1
  10. package/src/command-families.js +10 -3
  11. package/src/commands/_generic.js +15 -5
  12. package/src/commands/_simple.js +14 -1
  13. package/src/commands/_ticket.js +7 -1
  14. package/src/commands/access-analyzer.js +37 -0
  15. package/src/commands/approvals.js +1 -1
  16. package/src/commands/atf.js +1 -1
  17. package/src/commands/auth.js +6 -0
  18. package/src/commands/cases.js +3 -0
  19. package/src/commands/catalog.js +1 -1
  20. package/src/commands/cmdb.js +3 -0
  21. package/src/commands/decisiontables.js +1 -1
  22. package/src/commands/discover.js +108 -8
  23. package/src/commands/document.js +31 -0
  24. package/src/commands/domains.js +1 -1
  25. package/src/commands/graphql.js +1 -1
  26. package/src/commands/groupmembers.js +1 -1
  27. package/src/commands/grouproles.js +1 -1
  28. package/src/commands/groups.js +1 -1
  29. package/src/commands/logs.js +3 -0
  30. package/src/commands/records.js +21 -0
  31. package/src/commands/rest.js +1 -1
  32. package/src/commands/scan.js +40 -5
  33. package/src/commands/scopes.js +1 -1
  34. package/src/commands/scrapi.js +1 -1
  35. package/src/commands/table-behavior.js +5 -0
  36. package/src/commands/tickets.js +9 -1
  37. package/src/commands/transactions.js +3 -0
  38. package/src/commands/updatesets.js +1 -1
  39. package/src/commands/users.js +1 -1
  40. package/src/context.js +19 -0
  41. package/src/help.js +3 -1
  42. package/src/instance-documentation.js +237 -0
  43. package/src/intended-context.js +30 -3
  44. package/src/output.js +15 -3
  45. package/src/query-cache.js +187 -0
  46. package/src/query-metadata.js +189 -0
  47. package/src/query-parse.js +201 -0
  48. package/src/query-validate.js +282 -0
  49. package/src/scan.js +92 -17
  50. package/src/sdk.js +3 -0
  51. package/src/table-behavior.js +1 -1
package/src/scan.js CHANGED
@@ -100,6 +100,34 @@ export async function scanAjax(sdk, method, parameters) {
100
100
  }
101
101
 
102
102
  const TERMINAL = new Set(['successful', 'failed', 'cancelled']);
103
+ // Execution tracker types that belong to Instance Scan. Exact matches only:
104
+ // no aliases or prefix matching, so unrelated trackers cannot be read as scans.
105
+ const SCAN_TRACKER_TYPES = new Set(['instance_scan', 'point_scan']);
106
+
107
+ // Read the execution back-reference from a result row without guessing: a
108
+ // non-empty value that is not a full sys_id is still "present, but not ours".
109
+ // Strings are checked by type so the legacy String.prototype.link is never read
110
+ // as a reference.
111
+ function hasResultBackReference(value) {
112
+ if (typeof value === 'string') return value !== '';
113
+ if (typeof value === 'object' && value !== null) return Boolean(raw(value) || value.link);
114
+ return false;
115
+ }
116
+
117
+ function resultBackReference(value) {
118
+ if (typeof value === 'object' && value !== null) return referenceId(value);
119
+ return isSysId(String(value ?? '')) ? String(value) : '';
120
+ }
121
+ // 'malformed' means a payload exists but could not be read as a reference.
122
+ // Parsing a reference is not verification: only results can verify linkage.
123
+ function resultLink(value) {
124
+ const text = raw(value);
125
+ if (!text) return { state: 'missing', id: '' };
126
+ try {
127
+ const id = referenceId(JSON.parse(text).result_id);
128
+ return id ? { state: 'linked', id } : { state: 'malformed', id: '' };
129
+ } catch { return { state: 'malformed', id: '' }; }
130
+ }
103
131
  const STATES = { '0': 'pending', '1': 'running', '2': 'successful', '3': 'failed', '4': 'cancelled',
104
132
  pending: 'pending', running: 'running', successful: 'successful', failed: 'failed',
105
133
  cancelled: 'cancelled', canceled: 'cancelled' };
@@ -108,19 +136,25 @@ export async function scanStatus(sdk, id, { allowInitializing = false } = {}) {
108
136
  requireId(id);
109
137
  const tracker = await sdk.get('sys_execution_tracker', id);
110
138
  if (!tracker) throw errNotFound('scan execution', id);
111
- if (!raw(tracker.type) && allowInitializing) return { execution_id: id, status: 'pending', terminal: false,
112
- percent_complete: null, message: 'Execution tracker is initializing', result_id: null };
113
- if (raw(tracker.type) === 'sys.instance_scan.queue') return { execution_id: id, status: 'pending', terminal: false,
114
- percent_complete: raw(tracker.percent_complete) ?? null, message: raw(tracker.message) || 'Scan is queued', result_id: null };
115
- if (raw(tracker.type) !== 'instance_scan') throw errUsage('This execution tracker is not an Instance Scan.');
139
+ // Missing or denied identity is not evidence that this is our execution.
140
+ const trackerId = tracker.sys_id === undefined || tracker.sys_id === null ? '' : String(raw(tracker.sys_id));
141
+ if (trackerId !== id) throw errUsage('The execution tracker identity does not match the requested execution.');
142
+ const type = String(raw(tracker.type) ?? '');
143
+ const link = resultLink(tracker.result);
144
+ if (!type && allowInitializing) return { execution_id: id, status: 'pending', terminal: false,
145
+ phase: 'initializing', percent_complete: null, message: 'Execution tracker is initializing', result_id: null,
146
+ execution_type: type, result_link: link.state };
147
+ if (type === 'sys.instance_scan.queue') return { execution_id: id, status: 'pending', terminal: false,
148
+ phase: 'queued', percent_complete: raw(tracker.percent_complete) ?? null, message: raw(tracker.message) || 'Scan is queued',
149
+ result_id: null, execution_type: type, result_link: link.state };
150
+ if (!SCAN_TRACKER_TYPES.has(type)) throw errUsage('This execution tracker is not an Instance Scan.');
116
151
  const state = STATES[String(raw(tracker.state)).toLowerCase()] || 'unknown';
117
- let resultId = '';
118
- if (raw(tracker.result)) {
119
- try { resultId = referenceId(JSON.parse(raw(tracker.result)).result_id); } catch { /* reported as unavailable below */ }
120
- }
121
- return { execution_id: id, status: state, terminal: TERMINAL.has(state),
152
+ // result_link only says the tracker's own result JSON parsed; a result row and
153
+ // its back-reference are verified in scanResults, never here.
154
+ return { execution_id: id, status: state, terminal: TERMINAL.has(state), phase: 'dispatched',
122
155
  percent_complete: raw(tracker.percent_complete) ?? null,
123
- message: raw(tracker.message) || '', result_id: resultId || null };
156
+ message: raw(tracker.message) || '', result_id: link.id || null,
157
+ execution_type: type, result_link: link.state };
124
158
  }
125
159
 
126
160
  async function resolveSuite(sdk, identifier) {
@@ -205,19 +239,44 @@ export async function startScan(sdk, plan) {
205
239
  export async function scanResults(sdk, id, limit = 50) {
206
240
  boundedLimit(limit);
207
241
  const status = await scanStatus(sdk, id);
208
- const data = { ...status, checks_executed: null, findings_count: null, checks: [], findings: [],
242
+ const data = { ...status, checks_executed: null, findings_count: null, findings_retrieved: null,
243
+ checks: [], findings: [], result_verified: false, back_reference: 'absent',
244
+ findings_state: 'unknown', checks_state: 'unknown',
209
245
  incomplete: !status.terminal, unavailable: [], truncated: {}, rerun_full: false, reusable_target: false };
210
246
  if (!status.result_id) { data.unavailable.push('scan_result'); data.incomplete = true; return data; }
211
247
  let result;
212
248
  try {
213
249
  result = await sdk.get('scan_result', status.result_id);
214
250
  if (!result) throw new Error('Result unavailable');
215
- data.scan_number = raw(result.number);
216
- const value = raw(result.finding_count);
217
- const count = Number(value);
218
- if ((typeof value === 'number' || (typeof value === 'string' && /^\d+$/.test(value))) && Number.isInteger(count) && count >= 0) data.findings_count = count;
219
- else { data.unavailable.push('scan_result.finding_count'); data.incomplete = true; }
251
+ // Require the exact linked identity, including when the field is denied.
252
+ const resultId = result.sys_id === undefined || result.sys_id === null ? '' : String(raw(result.sys_id));
253
+ if (resultId !== status.result_id) {
254
+ data.unavailable.push('scan_result.identity');
255
+ data.incomplete = true;
256
+ result = null;
257
+ }
220
258
  } catch { data.unavailable.push('scan_result'); data.incomplete = true; }
259
+ if (result) {
260
+ // The result row must point back to this execution. A point scan always
261
+ // records its execution id; a foreign or absent back-reference means the
262
+ // row cannot be trusted to describe this scan. Only a matching
263
+ // back-reference earns result_verified; a parsed id alone does not.
264
+ const present = hasResultBackReference(result.progress_id);
265
+ const matches = present && resultBackReference(result.progress_id) === status.execution_id;
266
+ data.back_reference = present ? (matches ? 'matched' : 'foreign') : 'absent';
267
+ if ((present && !matches) || (!present && status.execution_type === 'point_scan')) {
268
+ data.unavailable.push('scan_result.back_reference');
269
+ data.incomplete = true;
270
+ result = null;
271
+ } else {
272
+ data.result_verified = matches;
273
+ data.scan_number = raw(result.number);
274
+ const value = raw(result.finding_count);
275
+ const count = Number(value);
276
+ if ((typeof value === 'number' || (typeof value === 'string' && /^\d+$/.test(value))) && Number.isInteger(count) && count >= 0) data.findings_count = count;
277
+ else { data.unavailable.push('scan_result.finding_count'); data.incomplete = true; }
278
+ }
279
+ }
221
280
  for (const [key, table, fields] of [
222
281
  ['checks', 'scan_check_execution', ['check', 'message', 'execution_time']],
223
282
  ['findings', 'scan_finding', ['check', 'source_table', 'source', 'finding_details', 'count']],
@@ -225,11 +284,27 @@ export async function scanResults(sdk, id, limit = 50) {
225
284
  try {
226
285
  const rows = await sdk.listStrict(table, recordListParams({ limit: limit + 1,
227
286
  query: `result=${status.result_id}^ORDERBYsys_id`, fields, displayValue: 'all' }));
287
+ if (!Array.isArray(rows) || rows.some(row => !row || typeof row !== 'object' || Array.isArray(row))) {
288
+ throw new Error('Unreadable scan rows');
289
+ }
228
290
  data[key] = rows.slice(0, limit);
229
291
  data.truncated[key] = rows.length > limit;
292
+ if (key === 'findings') data.findings_retrieved = rows.length;
230
293
  if (key === 'checks' && !data.truncated[key] && status.terminal) data.checks_executed = rows.length;
231
294
  } catch { data.unavailable.push(table); data.incomplete = true; }
232
295
  }
296
+ // Reported counts must reconcile with retrieved rows. A positive count with
297
+ // nothing retrieved, or any other disagreement, is not a clean zero and its
298
+ // cause is not assumed. Truncated pages cannot be reconciled at all.
299
+ if (data.findings_count === null || data.unavailable.includes('scan_finding')) data.findings_state = 'unknown';
300
+ else if (data.truncated.findings) { data.findings_state = 'truncated'; data.incomplete = true; }
301
+ else if (data.findings_count > 0 && data.findings_retrieved === 0) { data.findings_state = 'unretrieved'; data.incomplete = true; }
302
+ else if (data.findings_count !== data.findings_retrieved) { data.findings_state = 'mismatch'; data.incomplete = true; }
303
+ else data.findings_state = 'verified';
304
+ if (data.unavailable.includes('scan_check_execution')) data.checks_state = 'unknown';
305
+ else if (data.truncated.checks) { data.checks_state = 'truncated'; data.incomplete = true; }
306
+ else if (data.checks.length === 0) data.checks_state = 'empty';
307
+ else data.checks_state = 'verified';
233
308
  const comboId = referenceId(result?.combo);
234
309
  if (comboId) {
235
310
  try { const combo = await sdk.get('scan_combo', comboId); if (combo) { data.reusable_target = true; data.rerun_full = !raw(combo.targets); } }
package/src/sdk.js CHANGED
@@ -464,6 +464,7 @@ export class SDKClient {
464
464
  method: 'POST',
465
465
  body: JSON.stringify(data),
466
466
  });
467
+ if (['sys_db_object', 'sys_dictionary', 'sys_dictionary_override'].includes(table)) this.afterMetadataMutation?.(table);
467
468
  return result?.result || null;
468
469
  }
469
470
 
@@ -474,6 +475,7 @@ export class SDKClient {
474
475
  method: 'PUT',
475
476
  body: JSON.stringify(data),
476
477
  });
478
+ if (['sys_db_object', 'sys_dictionary', 'sys_dictionary_override'].includes(table)) this.afterMetadataMutation?.(table);
477
479
  return result?.result || null;
478
480
  }
479
481
 
@@ -481,6 +483,7 @@ export class SDKClient {
481
483
  await this._guardTableMutation(table, sysID);
482
484
  const endpoint = `${this.baseURL}/api/now/table/${table}/${sysID}`;
483
485
  await this.request(endpoint, { method: 'DELETE' });
486
+ if (['sys_db_object', 'sys_dictionary', 'sys_dictionary_override'].includes(table)) this.afterMetadataMutation?.(table);
484
487
  }
485
488
 
486
489
  // Flow Designer actions use full Process Flow documents, not table CRUD.
@@ -2,7 +2,7 @@
2
2
  //
3
3
  // Inventories accessible configuration associated with a table and its
4
4
  // ancestors. It does NOT execute scripts, evaluate conditions, or predict
5
- // which artifact runs for a record. See docs/decisions and GLOSSARY.md.
5
+ // which artifact runs for a record. See docs/decisions and site/docs/maintainers/domain-model.md.
6
6
  //
7
7
  // Design informed by now-sdk-ext-core's TableBehaviorDiscovery (MIT,
8
8
  // Copyright (c) 2026 SoniSoft Inc). This implementation is written for jsn's