@tikoci/centrs 0.1.9-next.0 → 0.1.9-next.22

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 (65) hide show
  1. package/CHANGELOG.md +177 -40
  2. package/GLOSSARY.txt +8 -0
  3. package/README.md +12 -8
  4. package/SECURITY.md +11 -3
  5. package/commands/api/README.md +39 -15
  6. package/commands/api/examples.md +44 -6
  7. package/commands/execute/README.md +108 -14
  8. package/commands/execute/examples.md +118 -3
  9. package/commands/explain/README.md +2 -1
  10. package/commands/mcp/README.md +11 -0
  11. package/commands/mcp/examples.md +6 -0
  12. package/commands/retrieve/README.md +30 -13
  13. package/commands/retrieve/examples.md +10 -6
  14. package/commands/transfer/examples.md +9 -0
  15. package/docs/CLI.md +8 -8
  16. package/docs/CONSTITUTION.md +25 -2
  17. package/docs/MATRIX.md +5 -4
  18. package/docs/errors/cdb/empty-group.md +19 -3
  19. package/docs/errors/cdb/empty-selection.md +9 -3
  20. package/docs/errors/cmr/caller-policy-not-enforced.md +46 -0
  21. package/docs/errors/cmr/empty-selection.md +17 -3
  22. package/docs/errors/cmr/rule-selects-every-device.md +32 -0
  23. package/docs/errors/cmr/selection-includes-controller.md +27 -0
  24. package/docs/errors/usage/flag-after-separator.md +34 -0
  25. package/package.json +7 -5
  26. package/src/api-fanout.ts +11 -1
  27. package/src/api.ts +205 -65
  28. package/src/btest.ts +7 -2
  29. package/src/cli/api.ts +30 -16
  30. package/src/cli/common.ts +180 -1
  31. package/src/cli/devices.ts +0 -2
  32. package/src/cli/discover.ts +0 -1
  33. package/src/cli/execute.ts +44 -20
  34. package/src/cli/explain.ts +2 -0
  35. package/src/cli/mcp.ts +0 -1
  36. package/src/cli/retrieve.ts +40 -18
  37. package/src/cli/settings.ts +0 -3
  38. package/src/cli/terminal.ts +0 -2
  39. package/src/cli/transfer.ts +10 -8
  40. package/src/cli/version.ts +87 -0
  41. package/src/cli.ts +5 -3
  42. package/src/core/cmr-fleet.ts +486 -0
  43. package/src/core/cmr.ts +78 -0
  44. package/src/core/envelope.ts +32 -3
  45. package/src/core/error-catalog.ts +25 -3
  46. package/src/core/execute-command.ts +144 -19
  47. package/src/core/fanout.ts +130 -6
  48. package/src/errors.ts +2 -1
  49. package/src/execute-cmr.ts +95 -78
  50. package/src/execute-fanout.ts +11 -1
  51. package/src/execute.ts +105 -23
  52. package/src/index.ts +18 -1
  53. package/src/mcp/safety.ts +11 -0
  54. package/src/mcp/tools.ts +4 -1
  55. package/src/resolver/cdb.ts +13 -1
  56. package/src/resolver/index.ts +4 -0
  57. package/src/resolver/selection.ts +37 -1
  58. package/src/resolver/target.ts +96 -8
  59. package/src/retrieve-fanout.ts +4 -1
  60. package/src/retrieve-follow.ts +29 -33
  61. package/src/retrieve-sample.ts +14 -8
  62. package/src/retrieve-stream.ts +38 -6
  63. package/src/retrieve.ts +73 -59
  64. package/src/transfer-fanout.ts +8 -1
  65. package/src/transfer.ts +15 -19
package/src/index.ts CHANGED
@@ -6,6 +6,8 @@ export {
6
6
  type ApiOutputFormat,
7
7
  type ApiRequest,
8
8
  type ApiRequestSummary,
9
+ type ApiStreamFrameEnvelope,
10
+ type ApiStreamLine,
9
11
  type ApiStreamMeta,
10
12
  type ApiStreamStopReason,
11
13
  type ApiStreamSummary,
@@ -16,6 +18,7 @@ export {
16
18
  apiOutputFormats,
17
19
  apiRequestSummaryFromRequest,
18
20
  apiStream,
21
+ apiVerboseLines,
19
22
  buildApiBody,
20
23
  buildApiErrorEnvelope,
21
24
  buildApiErrorEnvelopeFromResolved,
@@ -81,10 +84,12 @@ export {
81
84
  type EnvelopeValidationMeta,
82
85
  type EnvelopeValidationStage,
83
86
  type FanoutData,
87
+ type FanoutMemberOperation,
84
88
  type FanoutSummary,
85
89
  normalizeWarnings,
86
90
  type SettingSource,
87
91
  type SettingSourceKind,
92
+ type StreamLineEnvelope,
88
93
  type ValidationTrace,
89
94
  type Warning,
90
95
  } from "./core/envelope.ts";
@@ -92,6 +97,8 @@ export {
92
97
  buildFanoutEnvelope,
93
98
  buildFanoutResolveFailure,
94
99
  commonVia,
100
+ compactFanoutTargets,
101
+ expandFanoutTarget,
95
102
  FANOUT_BACKOFF_BASE_MS,
96
103
  FANOUT_CONCURRENCY_DEFAULTS,
97
104
  FANOUT_MAX_RETRIES,
@@ -279,6 +286,7 @@ export {
279
286
  execute,
280
287
  executeEnvelope,
281
288
  executeOutputFormats,
289
+ executeVerboseLines,
282
290
  isWriteShaped,
283
291
  renderExecuteEnvelope,
284
292
  resolveExecuteGlobalContext,
@@ -604,6 +612,7 @@ export {
604
612
  type RetrieveEnvelope,
605
613
  type RetrieveErrorEnvelope,
606
614
  type RetrieveFollowCounts,
615
+ type RetrieveFollowLine,
607
616
  type RetrieveFollowSettings,
608
617
  type RetrieveFollowStopReason,
609
618
  type RetrieveFollowSummary,
@@ -616,15 +625,19 @@ export {
616
625
  type RetrieveSampleSettings,
617
626
  type RetrieveSampleStopReason,
618
627
  type RetrieveSampleSummary,
628
+ type RetrieveStreamLine,
629
+ type RetrieveStreamLineEnvelope,
619
630
  type RetrieveStreamMeta,
620
631
  type RetrieveSuccessEnvelope,
621
632
  type RetrieveWarning,
622
633
  renderRetrieveEnvelope,
623
634
  resolveRetrieveGlobalContext,
635
+ resolveRetrieveOutputFormat,
624
636
  resolveRetrieveRequest,
625
637
  retrieve,
626
638
  retrieveEnvelope,
627
639
  retrieveOutputFormats,
640
+ retrieveVerboseLines,
628
641
  runResolvedRetrieve,
629
642
  validateRetrieveRequestShape,
630
643
  } from "./retrieve.ts";
@@ -650,7 +663,10 @@ export {
650
663
  type RetrieveSampleOptions,
651
664
  retrieveSample,
652
665
  } from "./retrieve-sample.ts";
653
- export { renderRetrieveStreamLine } from "./retrieve-stream.ts";
666
+ export {
667
+ isStreamDataLine,
668
+ renderRetrieveStreamLine,
669
+ } from "./retrieve-stream.ts";
654
670
  export { type RetrieveWaitOptions, retrieveWait } from "./retrieve-wait.ts";
655
671
  export {
656
672
  buildSettingsErrorEnvelope,
@@ -718,6 +734,7 @@ export {
718
734
  transferEnvelope,
719
735
  transferMethods,
720
736
  transferOutputFormats,
737
+ transferVerboseLines,
721
738
  transferVerbs,
722
739
  validateTransferRequestShape,
723
740
  } from "./transfer.ts";
package/src/mcp/safety.ts CHANGED
@@ -19,6 +19,11 @@ import {
19
19
  import { CentrsError } from "../errors.ts";
20
20
  import type { McpWritePolicy } from "../mcp.ts";
21
21
  import { parseCommentKv } from "../resolver/comment-kv.ts";
22
+ import {
23
+ assertNoTargetCredentials,
24
+ assertNoTargetPassword,
25
+ redactTargetInput,
26
+ } from "../resolver/target.ts";
22
27
  import type { CentrsMcpConfig } from "./config.ts";
23
28
 
24
29
  export interface RegisteredTarget {
@@ -68,10 +73,16 @@ export function resolveRegisteredTarget(
68
73
  cdb: LoadedCdb,
69
74
  target: string,
70
75
  ): RegisteredTarget {
76
+ // Before the lookup, whose refusal echoes the target (#478).
77
+ assertNoTargetPassword(target);
71
78
  let entry: DevicesShowItem;
72
79
  try {
73
80
  entry = showDevice({ cdb, target }).data.entry;
74
81
  } catch (error) {
82
+ // Preserve registered identities; mask credential-shaped lookup failures.
83
+ if (redactTargetInput(target) !== target) {
84
+ assertNoTargetCredentials(target);
85
+ }
75
86
  if (error instanceof CentrsError && error.code === "cdb/not-found-target") {
76
87
  throw new CentrsError({
77
88
  code: "cdb/target-not-registered",
package/src/mcp/tools.ts CHANGED
@@ -36,6 +36,7 @@ import {
36
36
  import type { CommentKvUpdate } from "../resolver/comment-kv.ts";
37
37
  import { parseBbox, parseNear } from "../resolver/geo.ts";
38
38
  import { parseDuration } from "../resolver/settings.ts";
39
+ import { redactTargetInput } from "../resolver/target.ts";
39
40
  import { retrieveEnvelope } from "../retrieve.ts";
40
41
  import {
41
42
  buildRetrieveFanoutErrorEnvelope,
@@ -89,7 +90,9 @@ function mcpErrorEnvelope(
89
90
  warnings: [],
90
91
  tips: [],
91
92
  meta: {
92
- target: context.target ? { input: context.target } : {},
93
+ target: context.target
94
+ ? { input: redactTargetInput(context.target) }
95
+ : {},
93
96
  via: null,
94
97
  settings: {},
95
98
  operation: { kind: "mcp", tool },
@@ -44,6 +44,11 @@ import {
44
44
  parseDuration,
45
45
  type ResolvedSetting,
46
46
  } from "./settings.ts";
47
+ import {
48
+ assertNoTargetCredentials,
49
+ assertNoTargetPassword,
50
+ redactTargetInput,
51
+ } from "./target.ts";
47
52
 
48
53
  const ENV_CDB_FILE = "CENTRS_CDB_FILE";
49
54
  const ENV_CDB_PASSWORD = "CENTRS_CDB_PASSWORD";
@@ -161,6 +166,8 @@ export async function resolveCdb(
161
166
  if (!input.targetInput) {
162
167
  return undefined;
163
168
  }
169
+ // Before any lookup: a CDB miss echoes the target it was given (#478).
170
+ assertNoTargetPassword(input.targetInput);
164
171
 
165
172
  const settings = resolveDevicesSettings({
166
173
  cdbFile: input.cdbFile,
@@ -225,6 +232,10 @@ export async function resolveCdb(
225
232
  ...(defaults ? { defaults } : {}),
226
233
  };
227
234
  } catch (error) {
235
+ // Preserve registered identities; mask credential-shaped lookup failures.
236
+ if (redactTargetInput(input.targetInput) !== input.targetInput) {
237
+ assertNoTargetCredentials(input.targetInput);
238
+ }
228
239
  if (error instanceof CentrsError && error.code === "cdb/not-found-target") {
229
240
  // A `__default__` record supplies fallback credentials even when the
230
241
  // target has no record of its own. On the CLI/API this fills creds for
@@ -366,7 +377,8 @@ export function resolutionFromEntry(
366
377
  * ambiguity path). See `commands/devices/README.md` (Identity model).
367
378
  */
368
379
  export function identityFromComment(comment: string, target: string): string {
369
- return parseCommentKv(comment).lookups.identity ?? target;
380
+ // The record's target is shown as its handle; never with a password (#478).
381
+ return parseCommentKv(comment).lookups.identity ?? redactTargetInput(target);
370
382
  }
371
383
 
372
384
  /**
@@ -97,6 +97,7 @@ export {
97
97
  SUPPORTED_DESCRIPTOR_VERSION,
98
98
  } from "./quickchr-provider.ts";
99
99
  export {
100
+ emptyWriteSelectionError,
100
101
  expandSelection,
101
102
  isDefaultRecordTarget,
102
103
  type SelectionExpansion,
@@ -130,10 +131,13 @@ export {
130
131
  toCoreSource,
131
132
  } from "./settings.ts";
132
133
  export {
134
+ assertNoTargetCredentials,
135
+ assertNoTargetPassword,
133
136
  defaultPortForScheme,
134
137
  parseHostCandidate,
135
138
  type ResolvedAuth,
136
139
  type ResolvedTarget,
140
+ redactTargetInput,
137
141
  resolveAuth,
138
142
  resolveTarget,
139
143
  type TargetResolveInput,
@@ -47,6 +47,11 @@ import {
47
47
  matchesNear,
48
48
  type NearPredicate,
49
49
  } from "./geo.ts";
50
+ import {
51
+ assertNoTargetCredentials,
52
+ assertNoTargetPassword,
53
+ redactTargetInput,
54
+ } from "./target.ts";
50
55
 
51
56
  /** One `--where attr=value` device-class clause (exact-match, AND-combined). */
52
57
  export interface SelectionWhereClause {
@@ -106,10 +111,36 @@ export interface SelectionExpansion {
106
111
  /** Members ordered by record index; literals appended in positional order. */
107
112
  targets: readonly SelectionMember[];
108
113
  warnings: readonly ResolverWarning[];
109
- /** True when nothing matched (an empty/unknown selection is not an error). */
114
+ /** True when nothing matched (an error only for a write; see {@link emptyWriteSelectionError}). */
110
115
  empty: boolean;
111
116
  }
112
117
 
118
+ /**
119
+ * An empty selection for a write-shaped fan-out (#460). A read over it is
120
+ * `ok: true` with the expansion's warning, so a poller over an occasionally
121
+ * empty group stays quiet. A write is an error, as RouterOS refuses an empty
122
+ * CMR selection (`no devices selected`): a misspelled group must not look like
123
+ * a successful bulk write. The code is the expansion's own empty warning.
124
+ */
125
+ export function emptyWriteSelectionError(
126
+ expansion: Pick<SelectionExpansion, "warnings">,
127
+ action: string,
128
+ ): CentrsError {
129
+ const warning = expansion.warnings.findLast(
130
+ (w) => w.code === "cdb/empty-group" || w.code === "cdb/empty-selection",
131
+ );
132
+ return new CentrsError({
133
+ code:
134
+ warning?.code === "cdb/empty-group"
135
+ ? "cdb/empty-group"
136
+ : "cdb/empty-selection",
137
+ summary: `${warning?.message ?? "The target selection matched no CDB records."} Nothing ran: a write-shaped ${action} must select at least one router.`,
138
+ remediation:
139
+ "Check the selector against `centrs devices list` and `centrs devices groups` (groups and `--where` values match as written), then re-run. A read over the same selection succeeds with a warning instead.",
140
+ context: { ...warning?.context, write: true },
141
+ });
142
+ }
143
+
113
144
  /** True when a resolved target is the reserved credential-fallback record. */
114
145
  export function isDefaultRecordTarget(target: string): boolean {
115
146
  return target === DEFAULT_RECORD_TARGET;
@@ -249,10 +280,15 @@ export async function expandSelection(
249
280
  const literals: SelectionMember[] = [];
250
281
  const seenLiteral = new Set<string>();
251
282
  for (const positional of selection.positionals) {
283
+ assertNoTargetPassword(positional);
252
284
  try {
253
285
  const shown = showDevice({ cdb, target: positional });
254
286
  indices.add(shown.data.entry.cdbRecordIndex);
255
287
  } catch (error) {
288
+ // Preserve registered identities; mask credential-shaped lookup failures.
289
+ if (redactTargetInput(positional) !== positional) {
290
+ assertNoTargetCredentials(positional);
291
+ }
256
292
  if (
257
293
  error instanceof CentrsError &&
258
294
  error.code === "cdb/not-found-target"
@@ -104,11 +104,12 @@ export function resolveTarget(
104
104
  summary: "No host could be resolved for `retrieve`.",
105
105
  remediation:
106
106
  "Pass a target positional like `centrs retrieve 192.0.2.10 /system/resource --via rest-api` or set `--host` / `CENTRS_HOST`.",
107
- context: { targetInput: input.targetInput },
107
+ context: { targetInput: redactTargetInput(input.targetInput) },
108
108
  });
109
109
  }
110
110
 
111
111
  const rawCandidate = hostSetting.value.trim();
112
+ assertNoTargetCredentials(rawCandidate);
112
113
  const macCandidate = normalizeMac(rawCandidate);
113
114
 
114
115
  // L2 transports (mac-telnet) address the device by **MAC** directly — no IP
@@ -127,11 +128,13 @@ export function resolveTarget(
127
128
  if (!mac) {
128
129
  throw new CentrsError({
129
130
  code: "target/mac-required",
130
- summary: `mac-telnet needs a MAC address target; got "${input.targetInput ?? cdb?.target ?? rawCandidate}".`,
131
+ summary: `mac-telnet needs a MAC address target; got "${redactTargetInput(input.targetInput ?? cdb?.target ?? rawCandidate)}".`,
131
132
  remediation:
132
133
  "Pass the device MAC (aa:bb:cc:dd:ee:ff), or pin an IP transport with `--via native-api`/`rest-api` (add `--resolve arp` for a MAC).",
133
134
  context: {
134
- target: input.targetInput ?? cdb?.target ?? rawCandidate,
135
+ target: redactTargetInput(
136
+ input.targetInput ?? cdb?.target ?? rawCandidate,
137
+ ),
135
138
  via,
136
139
  },
137
140
  });
@@ -178,7 +181,7 @@ export function resolveTarget(
178
181
  const deliveryHost = deliveryHostSetting?.value.trim() || "255.255.255.255";
179
182
  const port = macPortSetting?.value ?? MAC_TELNET_PORT;
180
183
  return {
181
- input: input.targetInput,
184
+ input: redactTargetInput(input.targetInput),
182
185
  host: deliveryHost,
183
186
  mac,
184
187
  port,
@@ -290,7 +293,7 @@ export function resolveTarget(
290
293
  };
291
294
 
292
295
  return {
293
- input: input.targetInput,
296
+ input: redactTargetInput(input.targetInput),
294
297
  // WHATWG keeps an IPv6 hostname bracketed; sockets and ssh need the bare
295
298
  // address (`baseUrl` re-brackets it).
296
299
  host: parsedUrl.hostname.replace(/^\[(.*)\]$/, "$1"),
@@ -471,6 +474,91 @@ export function resolveAuth(
471
474
  };
472
475
  }
473
476
 
477
+ /** An authority with userinfo: `user@host`, `user:pass@host`, with or without a scheme. */
478
+ const URL_USERINFO = /^\s*([a-z][a-z0-9+.-]*:\/\/)?[^/@\s]*@/i;
479
+ /** Leading whitespace and an optional `scheme://`, kept by redaction. */
480
+ const TARGET_PREFIX = /^\s*(?:[a-z][a-z0-9+.-]*:\/\/)?/i;
481
+
482
+ /**
483
+ * Where credentials could sit: a `:` before the LAST `@`, or explicit URL
484
+ * userinfo. Display also masks a percent-encoded colon; lookup validation
485
+ * allows that ambiguous spelling until CDB lookup has run. Bare `user@host`
486
+ * remains a CDB identity candidate. Index arithmetic, so a password holding `/`, `?` or
487
+ * `#` (which ends a URL authority early, CodeRabbit on PR #479) still counts,
488
+ * and nothing can backtrack (CodeQL js/polynomial-redos).
489
+ */
490
+ function targetCredentialsSpan(
491
+ value: string,
492
+ includeEncodedColon = true,
493
+ ): { start: number; at: number } | undefined {
494
+ const at = value.lastIndexOf("@");
495
+ if (at < 0) return undefined;
496
+ const prefix = TARGET_PREFIX.exec(value)?.[0] ?? "";
497
+ const start = prefix.length;
498
+ const urlUserinfo = prefix.includes("://") && URL_USERINFO.test(value);
499
+ const userinfo = value.slice(start, at);
500
+ return userinfo.includes(":") ||
501
+ (includeEncodedColon && /%3a/i.test(userinfo)) ||
502
+ urlUserinfo
503
+ ? { start, at }
504
+ : undefined;
505
+ }
506
+
507
+ /**
508
+ * A target string with any `user:pass@` or explicit URL userinfo replaced by
509
+ * `***@`, for display (#478).
510
+ * A target is echoed in `meta.target.input` and in error messages, so the
511
+ * password part must never reach them, not even when the target is refused.
512
+ * Everything up to the last `@` goes, so an odd password cannot slip past.
513
+ * Other targets, including a CDB identity like `rtr@site`, pass unchanged.
514
+ * An encoded colon is masked conservatively without changing lookup input.
515
+ */
516
+ export function redactTargetInput(value: string): string;
517
+ export function redactTargetInput(
518
+ value: string | undefined,
519
+ ): string | undefined;
520
+ export function redactTargetInput(
521
+ value: string | undefined,
522
+ ): string | undefined {
523
+ if (value === undefined) return value;
524
+ const span = targetCredentialsSpan(value);
525
+ return span
526
+ ? `${value.slice(0, span.start)}***${value.slice(span.at)}`
527
+ : value;
528
+ }
529
+
530
+ function targetCredentialsError(value: string): CentrsError {
531
+ const target = redactTargetInput(value);
532
+ return new CentrsError({
533
+ code: "input/invalid-target",
534
+ summary: `A target must not carry credentials (\`user[:password]@\`): ${target}`,
535
+ remediation:
536
+ "Drop the `user:password@` part and pass the credentials with `--username` and `--password` (or `CENTRS_USERNAME`/`CENTRS_PASSWORD`, or a CDB record); give the target as `host[:port]` or `http(s)://host[:port]`.",
537
+ context: { target },
538
+ });
539
+ }
540
+
541
+ /**
542
+ * Refuse a target that may hold a password, BEFORE any lookup whose miss would
543
+ * echo it (#478): `resolveCdb`, the MCP allowlist, the fan-out selection. A
544
+ * bare `user@host` is left to the lookup; a CDB identity may contain `@`.
545
+ * Explicit URL userinfo is always refused, including percent-encoded forms.
546
+ */
547
+ export function assertNoTargetPassword(value: string): void {
548
+ if (targetCredentialsSpan(value, false)) throw targetCredentialsError(value);
549
+ }
550
+
551
+ /**
552
+ * centrs never reads credentials from a target (#478): `user:pass@host` used
553
+ * to be accepted with its credentials ignored, and echoed the password back.
554
+ * After lookup, refuse any userinfo in the host to dial, from any source.
555
+ */
556
+ export function assertNoTargetCredentials(value: string): void {
557
+ if (targetCredentialsSpan(value) || URL_USERINFO.test(value)) {
558
+ throw targetCredentialsError(value);
559
+ }
560
+ }
561
+
474
562
  export function parseHostCandidate(value: string): URL {
475
563
  const hasScheme = /^https?:\/\//i.test(value);
476
564
  // A bare IPv6 literal (`2001:db8::1`) has no port; URL needs it bracketed.
@@ -484,7 +572,7 @@ export function parseHostCandidate(value: string): URL {
484
572
  if (hasScheme && url.pathname !== "/" && url.pathname !== "") {
485
573
  throw new CentrsError({
486
574
  code: "input/invalid-target",
487
- summary: `Target URL must not include a path. Received: ${value}`,
575
+ summary: `Target URL must not include a path. Received: ${redactTargetInput(value)}`,
488
576
  remediation:
489
577
  "Pass only the RouterOS host or base URL, then provide the RouterOS menu path as the second positional argument.",
490
578
  });
@@ -500,12 +588,12 @@ function invalidTargetError(value: string, cause: unknown): CentrsError {
500
588
  const quickchrGuess = /^quickchr:(.+)$/i.exec(value)?.[1];
501
589
  return new CentrsError({
502
590
  code: "input/invalid-target",
503
- summary: `Target is not a known device or a valid host: ${value}`,
591
+ summary: `Target is not a known device or a valid host: ${redactTargetInput(value)}`,
504
592
  remediation:
505
593
  quickchrGuess !== undefined
506
594
  ? `Select a quickchr VM with the flag: \`--quickchr ${quickchrGuess}\` (there is no \`quickchr:\` target prefix).`
507
595
  : "Pass a CDB target or identity (see `centrs devices list`), an IP or hostname with optional `:port`, or an `http(s)://host[:port]` base URL.",
508
- context: { target: value },
596
+ context: { target: redactTargetInput(value) },
509
597
  cause,
510
598
  });
511
599
  }
@@ -59,6 +59,7 @@ import {
59
59
  expandSelection,
60
60
  isDefaultRecordTarget,
61
61
  loadEnvFileDefaults,
62
+ redactTargetInput,
62
63
  type SelectionExpansion,
63
64
  type SelectionMember,
64
65
  type SelectionResolveInput,
@@ -97,6 +98,8 @@ export interface RetrieveFanoutOperationMeta {
97
98
  concurrency: number;
98
99
  summary: FanoutSummary;
99
100
  request: RetrieveRequestSummary;
101
+ /** Hoisted from the targets when every target used the same login (#461). */
102
+ auth?: { username?: string; passwordProvided: boolean };
100
103
  selection: RetrieveSelectionSummary;
101
104
  }
102
105
 
@@ -143,7 +146,7 @@ function summarizeSelection(
143
146
  where: selection.where.map((clause) => `${clause.key}=${clause.value}`),
144
147
  all: selection.all,
145
148
  default: selection.default,
146
- positionals: selection.positionals,
149
+ positionals: selection.positionals.map((value) => redactTargetInput(value)),
147
150
  quickchr: selection.quickchr,
148
151
  };
149
152
  }
@@ -28,17 +28,22 @@ import {
28
28
  buildRetrieveErrorEnvelope,
29
29
  buildRetrieveErrorEnvelopeFromResolved,
30
30
  type ResolvedRetrieveRequest,
31
- type RetrieveEnvelope,
32
31
  type RetrieveFollowCounts,
32
+ type RetrieveFollowLine,
33
33
  type RetrieveFollowStopReason,
34
34
  type RetrieveFollowSummary,
35
35
  type RetrieveRequest,
36
+ type RetrieveStreamLineEnvelope,
36
37
  type RetrieveStreamMeta,
37
38
  type RetrieveSuccessEnvelope,
38
39
  resolveRetrieveRequest,
39
40
  validateRetrieveRead,
40
41
  } from "./retrieve.ts";
41
- import { failedStreamEnvelope, streamEnvelope } from "./retrieve-stream.ts";
42
+ import {
43
+ failedStreamEnvelope,
44
+ streamEnvelope,
45
+ streamLineEnvelope,
46
+ } from "./retrieve-stream.ts";
42
47
 
43
48
  /** Changes allowed to wait unread before the follow ends (`transport/stream-overflow`). */
44
49
  export const FOLLOW_BUFFER_LIMIT = 50_000;
@@ -52,7 +57,8 @@ export interface RetrieveFollowOptions {
52
57
  bufferLimit?: number;
53
58
  }
54
59
 
55
- type FollowEvent =
60
+ /** `at`: when the reply arrived (host clock), stamped by `push` in the wire tap. */
61
+ type FollowEvent = { at?: number } & (
56
62
  | { type: "listen"; reply: ProtocolTappedReply; version?: number }
57
63
  | { type: "listen-end" }
58
64
  | { type: "snapshot-row"; row: Record<string, string> }
@@ -65,7 +71,8 @@ type FollowEvent =
65
71
  rows: Map<string, Record<string, string>>;
66
72
  }
67
73
  | { type: "stop" }
68
- | { type: "error"; error: unknown };
74
+ | { type: "error"; error: unknown }
75
+ );
69
76
 
70
77
  /**
71
78
  * The wire-ordered event queue: every source feeds it from a synchronous
@@ -144,8 +151,8 @@ class FollowQueue {
144
151
  type FrameMeta = Extract<RetrieveStreamMeta, { kind: "frame" }>;
145
152
 
146
153
  /**
147
- * Follow one menu: yields an optional `notice`, the snapshot frames, `synced`,
148
- * live frames, then exactly one summary (successful, or an error envelope
154
+ * Follow one menu: yields `started` (the full meta and any advice), the
155
+ * snapshot frames, `synced`, live frames, then exactly one summary (successful, or an error envelope
149
156
  * carrying the partial counts). Errors before the follow starts (resolution,
150
157
  * validation, a non-followable menu, a non-native transport) yield one error
151
158
  * envelope without a summary.
@@ -154,7 +161,7 @@ export async function* retrieveFollow(
154
161
  request: RetrieveRequest,
155
162
  env: Record<string, string | undefined> = Bun.env,
156
163
  options: RetrieveFollowOptions = {},
157
- ): AsyncGenerator<RetrieveEnvelope, void, void> {
164
+ ): AsyncGenerator<RetrieveFollowLine, void, void> {
158
165
  const followRequest: RetrieveRequest = { ...request, follow: true };
159
166
  let resolved: ResolvedRetrieveRequest;
160
167
  try {
@@ -269,7 +276,7 @@ async function* runFollow(
269
276
  options: RetrieveFollowOptions,
270
277
  startedAt: number,
271
278
  query?: readonly string[],
272
- ): AsyncGenerator<RetrieveEnvelope, void, void> {
279
+ ): AsyncGenerator<RetrieveFollowLine, void, void> {
273
280
  const follow = resolved.follow ?? { sweepMs: 0 };
274
281
  const bufferLimit = options.bufferLimit ?? FOLLOW_BUFFER_LIMIT;
275
282
  const counts: RetrieveFollowCounts = {
@@ -322,7 +329,7 @@ async function* runFollow(
322
329
  controller.abort();
323
330
  return;
324
331
  }
325
- queue.push(event);
332
+ queue.push({ ...event, at: Date.now() });
326
333
  };
327
334
  const stop = (reason: RetrieveFollowStopReason): void => {
328
335
  stopReason ??= reason;
@@ -529,14 +536,16 @@ async function* runFollow(
529
536
  id: string,
530
537
  source: FrameMeta["source"],
531
538
  row: Record<string, string> | null,
532
- ): RetrieveSuccessEnvelope => {
539
+ at: number | undefined,
540
+ ): RetrieveStreamLineEnvelope => {
533
541
  counts.frames += 1;
534
542
  if (phase === "snapshot") counts.snapshot += 1;
535
543
  else counts.changes += 1;
536
544
  const data = row === null ? null : projectRow(row, resolved.attributes);
537
- return streamEnvelope(resolved, validation, data, {
545
+ return streamLineEnvelope(data, {
538
546
  kind: "frame",
539
547
  index: counts.frames,
548
+ observedAt: new Date(at ?? Date.now()).toISOString(),
540
549
  phase,
541
550
  change,
542
551
  id,
@@ -547,7 +556,7 @@ async function* runFollow(
547
556
  const applyListen = (
548
557
  event: Extract<FollowEvent, { type: "listen" }>,
549
558
  phase: FrameMeta["phase"],
550
- ): RetrieveSuccessEnvelope | undefined => {
559
+ ): RetrieveStreamLineEnvelope | undefined => {
551
560
  const { reply } = event;
552
561
  // `!empty` on a listen is the cancel acknowledgement, never a table state.
553
562
  if (reply.type === "empty") return undefined;
@@ -568,10 +577,10 @@ async function* runFollow(
568
577
  if (reply.attributes[".dead"] === "true") {
569
578
  // A `.dead` for an id never reported is a coalesced add+delete.
570
579
  if (!held.delete(id)) return undefined;
571
- return frame(phase, "removed", id, "listen", null);
580
+ return frame(phase, "removed", id, "listen", null, event.at);
572
581
  }
573
582
  held.add(id);
574
- return frame(phase, "upsert", id, "listen", reply.attributes);
583
+ return frame(phase, "upsert", id, "listen", reply.attributes, event.at);
575
584
  };
576
585
  /**
577
586
  * Between frames of one batch (the A1 replay, a sweep's removals): a stop
@@ -586,15 +595,7 @@ async function* runFollow(
586
595
  follow.count !== undefined && counts.changes >= follow.count;
587
596
 
588
597
  try {
589
- if (tips.length > 0) {
590
- yield streamEnvelope(
591
- resolved,
592
- validation,
593
- null,
594
- { kind: "notice" },
595
- tips,
596
- );
597
- }
598
+ yield streamEnvelope(resolved, validation, null, { kind: "started" }, tips);
598
599
  loop: for (;;) {
599
600
  const event = await queue.next();
600
601
  switch (event.type) {
@@ -620,7 +621,7 @@ async function* runFollow(
620
621
  const id = event.row[".id"];
621
622
  if (id !== undefined) {
622
623
  held.add(id);
623
- yield frame("snapshot", "upsert", id, "print", event.row);
624
+ yield frame("snapshot", "upsert", id, "print", event.row, event.at);
624
625
  }
625
626
  break;
626
627
  }
@@ -660,9 +661,9 @@ async function* runFollow(
660
661
  const phase = counts.synced ? "live" : "snapshot";
661
662
  if (row) {
662
663
  held.add(id);
663
- yield frame(phase, "upsert", id, "membership", row);
664
+ yield frame(phase, "upsert", id, "membership", row, event.at);
664
665
  } else if (held.delete(id)) {
665
- yield frame(phase, "removed", id, "membership", null);
666
+ yield frame(phase, "removed", id, "membership", null, event.at);
666
667
  }
667
668
  if (counts.synced && countReached()) {
668
669
  stopReason ??= "count-reached";
@@ -688,13 +689,8 @@ async function* runFollow(
688
689
  continue;
689
690
  if (haltRequested()) break loop;
690
691
  held.delete(id);
691
- yield frame(
692
- "live",
693
- "removed",
694
- id,
695
- query ? "membership" : "sweep",
696
- null,
697
- );
692
+ // The sweep found it gone, filtered or not (#462).
693
+ yield frame("live", "removed", id, "sweep", null, event.at);
698
694
  if (countReached()) {
699
695
  stopReason ??= "count-reached";
700
696
  break loop;