@camstack/addon-pipeline-orchestrator 1.2.297 → 1.2.298

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/index.mjs CHANGED
@@ -2,7 +2,7 @@ import { createHash, randomUUID } from "node:crypto";
2
2
  import fs from "node:fs";
3
3
  import path from "node:path";
4
4
  import { fileURLToPath } from "node:url";
5
- //#region ../types/dist/event-category-B_0XEcLF.mjs
5
+ //#region ../types/dist/event-category-DaIZi00R.mjs
6
6
  var EventCategory = /* @__PURE__ */ function(EventCategory) {
7
7
  EventCategory["SystemBoot"] = "system.boot";
8
8
  EventCategory["SystemAddonsReady"] = "system.addons-ready";
@@ -81,6 +81,13 @@ var EventCategory = /* @__PURE__ */ function(EventCategory) {
81
81
  */
82
82
  EventCategory["AddonLoadFailed"] = "addon.load-failed";
83
83
  /**
84
+ * A keychain value was stored or removed (D787). Carries the ref, its
85
+ * owner and the new version — NEVER the value. A process holding the ref
86
+ * drops its cached copy and the owning `BaseAddon` re-resolves its config.
87
+ * Telemetry (D8): a process that misses it still revalidates by version.
88
+ */
89
+ EventCategory["KeychainChanged"] = "keychain.changed";
90
+ /**
84
91
  * Addon recovered from a previous failure. Emitted when an addon
85
92
  * transitions from `failed` back to `healthy` (typically via the
86
93
  * monitor's auto-retry loop, or after manual `addons.retryLoad`).
@@ -5373,7 +5380,7 @@ var ZodIssueCode = {
5373
5380
  var ZodFirstPartyTypeKind;
5374
5381
  ZodFirstPartyTypeKind || (ZodFirstPartyTypeKind = {});
5375
5382
  //#endregion
5376
- //#region ../types/dist/sleep-BNeXenvr.mjs
5383
+ //#region ../types/dist/sleep-Cr5YOk3m.mjs
5377
5384
  /**
5378
5385
  * The audio chunk plane's byte format, and the ONE expansion from a coded
5379
5386
  * window to float samples (D455).
@@ -5452,6 +5459,97 @@ function buildAlawTable() {
5452
5459
  }
5453
5460
  buildUlawTable();
5454
5461
  buildAlawTable();
5462
+ /** Is this field's stored value a credential? */
5463
+ function isSecretConfigField(field) {
5464
+ if (field.type === "password") return true;
5465
+ return "secret" in field && field.secret === true;
5466
+ }
5467
+ /**
5468
+ * Is this field's value stored in the KEYCHAIN (D787)? Every secret field is,
5469
+ * by its TYPE, unless it opts out with `storage: 'inline'` — never by its name.
5470
+ */
5471
+ function isKeychainStoredField(field) {
5472
+ if (!isSecretConfigField(field)) return false;
5473
+ if (field.type === "textarea" && field.isJson === true) return false;
5474
+ return !("storage" in field && field.storage === "inline");
5475
+ }
5476
+ /**
5477
+ * What a settings blob holds where a secret used to be.
5478
+ *
5479
+ * A keychain-stored field never holds its value. It holds a self-describing
5480
+ * MARKER, `{"$secret":"secret://…","v":1}` — an object, never a string, so no
5481
+ * code can mistake it for a credential and no form can send it to a camera as
5482
+ * a password. The empty state is `null` / absent, never `''`.
5483
+ *
5484
+ * The same shapes travel to and from the admin UI:
5485
+ *
5486
+ * - read: `hydrateSchema` hands a secret field a {@link SecretFieldView}
5487
+ * (`{ $secret, v, set: true }`) or `null` — the browser learns whether a
5488
+ * value is set, never the value.
5489
+ * - write: an untouched field submits the view back unchanged (KEEP); a typed
5490
+ * value is a plain non-empty string (CHANGE); the Clear button submits
5491
+ * {@link SECRET_CLEAR} (CLEAR). `''`, `null` and the legacy redaction tokens
5492
+ * are KEEP — an empty string is what a defaults-fallback pushes back, and
5493
+ * treating it as "clear" is how a save destroyed a stored credential before.
5494
+ *
5495
+ * Pure and browser-safe.
5496
+ */
5497
+ var SECRET_CLEAR = { $secretClear: true };
5498
+ /** The `'***'` token D634 redaction writes for non-admins. */
5499
+ var LEGACY_DEVICE_REDACTION = "***";
5500
+ function isRecord$1(value) {
5501
+ return typeof value === "object" && value !== null && !Array.isArray(value);
5502
+ }
5503
+ function makeSecretMarker(ref) {
5504
+ return {
5505
+ $secret: ref,
5506
+ v: 1
5507
+ };
5508
+ }
5509
+ /** A stored marker: an object whose `$secret` is a non-empty string. */
5510
+ function isSecretMarker(value) {
5511
+ if (!isRecord$1(value)) return false;
5512
+ const ref = value["$secret"];
5513
+ return typeof ref === "string" && ref.length > 0;
5514
+ }
5515
+ function isSecretClear(value) {
5516
+ return isRecord$1(value) && value["$secretClear"] === true;
5517
+ }
5518
+ /**
5519
+ * The UI view of a stored secret field value: a marker becomes "set", inline
5520
+ * plaintext becomes "set (inline)", nothing becomes `null`. A value never
5521
+ * survives this function.
5522
+ */
5523
+ function toSecretFieldView(stored) {
5524
+ if (isSecretMarker(stored)) return {
5525
+ $secret: stored.$secret,
5526
+ v: 1,
5527
+ set: true
5528
+ };
5529
+ if (typeof stored === "string" && stored.length > 0) return {
5530
+ $secret: null,
5531
+ v: 1,
5532
+ set: true,
5533
+ inline: true
5534
+ };
5535
+ return null;
5536
+ }
5537
+ /**
5538
+ * Classify an incoming value for a secret path. Only a non-empty string that is
5539
+ * not a redaction token sets a value; only {@link SECRET_CLEAR} clears.
5540
+ * Everything else — a marker (ours or anyone's), a UI view, `''`, `null`,
5541
+ * `undefined`, a redaction token, a non-string — keeps what is stored.
5542
+ */
5543
+ function classifySecretWrite(incoming) {
5544
+ if (isSecretClear(incoming)) return { kind: "clear" };
5545
+ if (typeof incoming !== "string") return { kind: "keep" };
5546
+ if (incoming.length === 0) return { kind: "keep" };
5547
+ if (incoming === "__camstack_redacted__" || incoming === LEGACY_DEVICE_REDACTION) return { kind: "keep" };
5548
+ return {
5549
+ kind: "set",
5550
+ value: incoming
5551
+ };
5552
+ }
5455
5553
  Object.fromEntries([
5456
5554
  {
5457
5555
  id: "overview",
@@ -5623,33 +5721,19 @@ Object.fromEntries([
5623
5721
  function isValuelessField(field) {
5624
5722
  return field.type === "separator" || field.type === "info" || field.type === "qr-code" || field.type === "button" || field.type === "object-array" || field.type === "widget" || field.type === "addon-action-button" || field.type === "device-action-button";
5625
5723
  }
5626
- /**
5627
- * Merge a `ConfigUISchema` with a raw `values` record into a
5628
- * `ConfigUISchemaWithValues`. Used by every `get*Settings` backend endpoint
5629
- * before returning to the admin UI. Groups are walked recursively.
5630
- *
5631
- * For each leaf field:
5632
- * - structural fields (`separator`, `info`, `button`) pass through unchanged
5633
- * - other fields get `value = values[key] ?? field.default ?? null`
5634
- *
5635
- * Unknown keys in `values` (keys not declared in the schema) are silently
5636
- * ignored — they don't contribute to the output. Missing keys fall back to
5637
- * the schema default or `null`. This mirrors the old backend behaviour where
5638
- * `FormBuilder` rendered `values[key] ?? field.default`.
5639
- */
5640
- function hydrateSchema(schema, values) {
5724
+ function hydrateSchema(schema, values, options = {}) {
5641
5725
  return {
5642
5726
  ...schema.tabs ? { tabs: [...schema.tabs] } : {},
5643
5727
  sections: schema.sections.map((section) => ({
5644
5728
  ...section,
5645
- fields: section.fields.map((field) => hydrateField(field, values))
5729
+ fields: section.fields.map((field) => hydrateField(field, values, options))
5646
5730
  }))
5647
5731
  };
5648
5732
  }
5649
- function hydrateField(field, values) {
5733
+ function hydrateField(field, values, options) {
5650
5734
  if (isValuelessField(field)) return field;
5651
5735
  if (field.type === "group") {
5652
- const hydratedChildren = field.fields.map((child) => hydrateField(child, values));
5736
+ const hydratedChildren = field.fields.map((child) => hydrateField(child, values, options));
5653
5737
  return {
5654
5738
  ...field,
5655
5739
  fields: hydratedChildren
@@ -5658,7 +5742,7 @@ function hydrateField(field, values) {
5658
5742
  if (field.type === "sub-tabs") {
5659
5743
  const hydratedTabs = field.tabs.map((tab) => ({
5660
5744
  ...tab,
5661
- fields: tab.fields.map((child) => hydrateField(child, values))
5745
+ fields: tab.fields.map((child) => hydrateField(child, values, options))
5662
5746
  }));
5663
5747
  return {
5664
5748
  ...field,
@@ -5668,6 +5752,33 @@ function hydrateField(field, values) {
5668
5752
  const key = field.key;
5669
5753
  const storedValue = Object.prototype.hasOwnProperty.call(values, key) ? values[key] : void 0;
5670
5754
  const defaultValue = field.default;
5755
+ const asView = (v) => isSecretMarker(v) || options.inlineSecretsAsViews === true && v !== void 0;
5756
+ if (isKeychainStoredField(field) && asView(storedValue)) return {
5757
+ ...field,
5758
+ value: toSecretFieldView(storedValue)
5759
+ };
5760
+ if (isKeychainStoredField(field) && options.inlineSecretsAsViews === true) return {
5761
+ ...field,
5762
+ value: null
5763
+ };
5764
+ if (field.type === "editable-array" && Array.isArray(storedValue)) {
5765
+ const secretItemKeys = field.itemFields.filter((item) => "key" in item && isKeychainStoredField(item)).map((item) => "key" in item ? item.key : "");
5766
+ if (secretItemKeys.length > 0) {
5767
+ const rows = storedValue.map((row) => {
5768
+ if (row === null || typeof row !== "object" || Array.isArray(row)) return row;
5769
+ const out = { ...row };
5770
+ for (const k of secretItemKeys) {
5771
+ const v = Reflect.get(row, k);
5772
+ if (asView(v)) out[k] = toSecretFieldView(v);
5773
+ }
5774
+ return out;
5775
+ });
5776
+ return {
5777
+ ...field,
5778
+ value: rows
5779
+ };
5780
+ }
5781
+ }
5671
5782
  if (field.multiple) {
5672
5783
  const stored = Array.isArray(storedValue) ? storedValue : storedValue !== void 0 && storedValue !== null ? [storedValue] : [];
5673
5784
  const itemFallback = field.multiple.itemDefault !== void 0 ? field.multiple.itemDefault : defaultValue !== void 0 ? defaultValue : typeOf(field) === "string" ? "" : null;
@@ -5926,6 +6037,431 @@ function projectStore(store, perNodeKeys, nodeId) {
5926
6037
  }
5927
6038
  return out;
5928
6039
  }
6040
+ var GLOBAL_SCOPE = { kind: "global" };
6041
+ /** The scope every addon-wide (not per-node, not per-device) setting lives in. */
6042
+ function globalSecretScope() {
6043
+ return GLOBAL_SCOPE;
6044
+ }
6045
+ function encodeSegment(value) {
6046
+ return encodeURIComponent(value);
6047
+ }
6048
+ /**
6049
+ * The field path of a value inside an array item, addressed by the item's id:
6050
+ * `arrayItemFieldPath('providers', 'authentik', 'clientSecret')` →
6051
+ * `providers[authentik].clientSecret`.
6052
+ */
6053
+ function arrayItemFieldPath(arrayKey, itemId, fieldKey) {
6054
+ return `${arrayKey}[${encodeSegment(itemId)}].${fieldKey}`;
6055
+ }
6056
+ /** A write to a secret path could not be stored in the keychain, so it was refused. */
6057
+ var KeychainWriteRefusedError = class extends Error {
6058
+ fieldPath;
6059
+ constructor(fieldPath, cause) {
6060
+ super(`secret "${fieldPath}" was not saved: the keychain could not store it (${cause instanceof Error ? cause.message : String(cause)}). Nothing was written.`);
6061
+ this.fieldPath = fieldPath;
6062
+ this.name = "KeychainWriteRefusedError";
6063
+ }
6064
+ };
6065
+ var KEEP = { kind: "keep" };
6066
+ function isRecord(value) {
6067
+ return typeof value === "object" && value !== null && !Array.isArray(value);
6068
+ }
6069
+ function defaultMintId() {
6070
+ return globalThis.crypto.randomUUID();
6071
+ }
6072
+ /** Every marker ref anywhere under `value`. */
6073
+ function collectSecretMarkerRefs(value, out = []) {
6074
+ if (isSecretMarker(value)) {
6075
+ out.push(value.$secret);
6076
+ return out;
6077
+ }
6078
+ if (Array.isArray(value)) for (const item of value) collectSecretMarkerRefs(item, out);
6079
+ else if (isRecord(value)) for (const child of Object.values(value)) collectSecretMarkerRefs(child, out);
6080
+ return out;
6081
+ }
6082
+ async function storedValueEquals(secrets, marker, candidate) {
6083
+ const resolved = (await secrets.resolve([marker.$secret]))[marker.$secret];
6084
+ return resolved?.kind === "value" && resolved.value === candidate;
6085
+ }
6086
+ async function writeLeaf(ctx, incoming, stored, fieldPath) {
6087
+ const intent = classifySecretWrite(incoming);
6088
+ const ref = ctx.secrets.ref(ctx.scope, fieldPath);
6089
+ const storedIsOurs = isSecretMarker(stored) && stored.$secret === ref;
6090
+ if (intent.kind === "keep") return KEEP;
6091
+ if (intent.kind === "set" && !ctx.operatorWrite && typeof stored === "string" && stored === intent.value) return KEEP;
6092
+ if (intent.kind === "clear") {
6093
+ if (storedIsOurs) ctx.deletes.push(ref);
6094
+ return {
6095
+ kind: "value",
6096
+ value: null
6097
+ };
6098
+ }
6099
+ if (storedIsOurs && await storedValueEquals(ctx.secrets, stored, intent.value)) return {
6100
+ kind: "value",
6101
+ value: stored
6102
+ };
6103
+ try {
6104
+ await ctx.secrets.put(ref, intent.value);
6105
+ } catch (err) {
6106
+ throw new KeychainWriteRefusedError(fieldPath, err);
6107
+ }
6108
+ return {
6109
+ kind: "value",
6110
+ value: makeSecretMarker(ref)
6111
+ };
6112
+ }
6113
+ /** Apply a member result onto `out`: KEEP restores the stored member, or removes it. */
6114
+ function applyMember(out, key, result, storedParent) {
6115
+ if (result.kind === "value") {
6116
+ out[key] = result.value;
6117
+ return;
6118
+ }
6119
+ if (Object.prototype.hasOwnProperty.call(storedParent, key)) out[key] = storedParent[key];
6120
+ else delete out[key];
6121
+ }
6122
+ async function writeMembers(ctx, children, incoming, stored, pathOf) {
6123
+ const out = { ...incoming };
6124
+ for (const [member, node] of children) applyMember(out, member, await writeNode(ctx, node, incoming[member], stored[member], pathOf(member)), stored);
6125
+ return out;
6126
+ }
6127
+ function rowId$1(row, idKey) {
6128
+ const raw = row[idKey];
6129
+ if (typeof raw === "string" && raw.length > 0) return raw;
6130
+ if (typeof raw === "number" && Number.isFinite(raw)) return String(raw);
6131
+ return null;
6132
+ }
6133
+ async function writeArray(ctx, node, incoming, stored, fieldPath) {
6134
+ const storedRows = Array.isArray(stored) ? stored.filter(isRecord) : [];
6135
+ if (!Array.isArray(incoming)) {
6136
+ ctx.deletes.push(...collectSecretMarkerRefs(storedRows));
6137
+ return {
6138
+ kind: "value",
6139
+ value: incoming
6140
+ };
6141
+ }
6142
+ const storedById = /* @__PURE__ */ new Map();
6143
+ for (const row of storedRows) {
6144
+ const id = rowId$1(row, node.idKey);
6145
+ if (id !== null) storedById.set(id, row);
6146
+ }
6147
+ const kept = /* @__PURE__ */ new Set();
6148
+ const rows = [];
6149
+ for (const row of incoming) {
6150
+ if (!isRecord(row)) {
6151
+ rows.push(row);
6152
+ continue;
6153
+ }
6154
+ const id = rowId$1(row, node.idKey) ?? ctx.mintId();
6155
+ kept.add(id);
6156
+ const withId = {
6157
+ ...row,
6158
+ [node.idKey]: row[node.idKey] ?? id
6159
+ };
6160
+ if (rowId$1(withId, node.idKey) === null) withId[node.idKey] = id;
6161
+ const storedRow = storedById.get(id) ?? {};
6162
+ rows.push(await writeMembers(ctx, node.item, withId, storedRow, (member) => arrayItemFieldPath(fieldPath, id, member)));
6163
+ }
6164
+ for (const [id, row] of storedById) if (!kept.has(id)) ctx.deletes.push(...collectSecretMarkerRefs(row));
6165
+ return {
6166
+ kind: "value",
6167
+ value: rows
6168
+ };
6169
+ }
6170
+ async function writeNode(ctx, node, incoming, stored, fieldPath) {
6171
+ switch (node.kind) {
6172
+ case "leaf": return writeLeaf(ctx, incoming, stored, fieldPath);
6173
+ case "array": return writeArray(ctx, node, incoming, stored, fieldPath);
6174
+ case "object": {
6175
+ if (!isRecord(incoming)) {
6176
+ if (incoming === void 0) return KEEP;
6177
+ ctx.deletes.push(...collectSecretMarkerRefs(stored));
6178
+ return {
6179
+ kind: "value",
6180
+ value: incoming
6181
+ };
6182
+ }
6183
+ const storedObj = isRecord(stored) ? stored : {};
6184
+ return {
6185
+ kind: "value",
6186
+ value: await writeMembers(ctx, node.children, incoming, storedObj, (m) => `${fieldPath}.${m}`)
6187
+ };
6188
+ }
6189
+ }
6190
+ }
6191
+ /** Split a perNode-scoped key (`key@nodeId`). */
6192
+ function splitNodeKey(key) {
6193
+ const at = key.indexOf("@");
6194
+ return at <= 0 ? {
6195
+ base: key,
6196
+ nodeId: null
6197
+ } : {
6198
+ base: key.slice(0, at),
6199
+ nodeId: key.slice(at + 1)
6200
+ };
6201
+ }
6202
+ /** Rewrite a settings patch so no secret value reaches the blob. See the file doc. */
6203
+ async function transformSecretWrite(input) {
6204
+ const deletes = [];
6205
+ const base = {
6206
+ secrets: input.secrets,
6207
+ mintId: input.mintId ?? defaultMintId,
6208
+ deletes,
6209
+ operatorWrite: input.operatorWrite === true
6210
+ };
6211
+ const out = {};
6212
+ for (const [key, incoming] of Object.entries(input.patch)) {
6213
+ const { base: baseKey, nodeId } = splitNodeKey(key);
6214
+ const node = input.tree.get(baseKey);
6215
+ if (node === void 0) {
6216
+ out[key] = incoming;
6217
+ continue;
6218
+ }
6219
+ const scope = nodeId !== null && nodeId.length > 0 ? {
6220
+ kind: "node",
6221
+ nodeId
6222
+ } : input.scope ?? globalSecretScope();
6223
+ const result = await writeNode({
6224
+ ...base,
6225
+ scope
6226
+ }, node, incoming, input.stored[key], baseKey);
6227
+ if (result.kind === "value") out[key] = result.value;
6228
+ }
6229
+ return {
6230
+ patch: out,
6231
+ afterWrite: async () => {
6232
+ if (deletes.length === 0) return { orphaned: [] };
6233
+ try {
6234
+ await input.secrets.delete([...new Set(deletes)]);
6235
+ return { orphaned: [] };
6236
+ } catch {
6237
+ return { orphaned: [...new Set(deletes)] };
6238
+ }
6239
+ }
6240
+ };
6241
+ }
6242
+ /**
6243
+ * Replace every marker in `value` with its keychain value. Returns the SAME
6244
+ * reference when there is no marker (no allocation, no keychain call).
6245
+ */
6246
+ async function resolveSecretMarkers(value, secrets) {
6247
+ const refs = collectSecretMarkerRefs(value);
6248
+ if (refs.length === 0) return {
6249
+ value,
6250
+ unresolved: []
6251
+ };
6252
+ const answers = await secrets.resolve(refs);
6253
+ const unresolved = [];
6254
+ for (const ref of new Set(refs)) {
6255
+ const answer = answers[ref];
6256
+ if (answer === void 0) unresolved.push({
6257
+ ref,
6258
+ reason: "not-initialized"
6259
+ });
6260
+ else if (answer.kind === "unavailable") unresolved.push({
6261
+ ref,
6262
+ reason: answer.reason
6263
+ });
6264
+ else if (answer.kind === "missing" || answer.kind === "forbidden") unresolved.push({
6265
+ ref,
6266
+ reason: answer.kind
6267
+ });
6268
+ }
6269
+ const substitute = (v) => {
6270
+ if (isSecretMarker(v)) {
6271
+ const answer = answers[v.$secret];
6272
+ return answer?.kind === "value" ? {
6273
+ keep: true,
6274
+ value: answer.value
6275
+ } : {
6276
+ keep: false,
6277
+ value: void 0
6278
+ };
6279
+ }
6280
+ if (Array.isArray(v)) {
6281
+ const items = [];
6282
+ for (const item of v) {
6283
+ const s = substitute(item);
6284
+ if (s.keep) items.push(s.value);
6285
+ }
6286
+ return {
6287
+ keep: true,
6288
+ value: items
6289
+ };
6290
+ }
6291
+ if (isRecord(v)) {
6292
+ const out = {};
6293
+ for (const [k, child] of Object.entries(v)) {
6294
+ const s = substitute(child);
6295
+ if (s.keep) out[k] = s.value;
6296
+ }
6297
+ return {
6298
+ keep: true,
6299
+ value: out
6300
+ };
6301
+ }
6302
+ return {
6303
+ keep: true,
6304
+ value: v
6305
+ };
6306
+ };
6307
+ return {
6308
+ value: substitute(value).value,
6309
+ unresolved
6310
+ };
6311
+ }
6312
+ function touchesTree(tree, patch) {
6313
+ if (tree.size === 0) return false;
6314
+ for (const key of Object.keys(patch)) {
6315
+ const at = key.indexOf("@");
6316
+ if (tree.has(at > 0 ? key.slice(0, at) : key)) return true;
6317
+ }
6318
+ return false;
6319
+ }
6320
+ /** The blob as stored now; a failed read REFUSES the write instead of guessing. */
6321
+ async function readStoredAddonBlob(raw) {
6322
+ const threeWay = raw.readAddonStoreResult;
6323
+ if (threeWay === void 0) return raw.readAddonStore();
6324
+ const result = await threeWay.call(raw);
6325
+ if (result.kind === "failed") throw new Error(`settings write refused: the stored blob could not be read (${result.error})`);
6326
+ return result.kind === "missing" ? {} : result.value;
6327
+ }
6328
+ function createSecretAwareSettings(raw, opts) {
6329
+ const chains = /* @__PURE__ */ new Map();
6330
+ const serialize = (key, fn) => {
6331
+ const next = (chains.get(key) ?? Promise.resolve()).then(fn, fn);
6332
+ const tail = next.catch(() => void 0);
6333
+ chains.set(key, tail);
6334
+ tail.then(() => {
6335
+ if (chains.get(key) === tail) chains.delete(key);
6336
+ });
6337
+ return next;
6338
+ };
6339
+ const write = async (chainKey, tree, scope, patch, readStored, rawWrite) => {
6340
+ if (!touchesTree(tree, patch)) return rawWrite(patch);
6341
+ return serialize(chainKey, async () => {
6342
+ const stored = await readStored();
6343
+ const result = await transformSecretWrite({
6344
+ tree,
6345
+ secrets: opts.secrets,
6346
+ patch,
6347
+ stored,
6348
+ ...scope !== void 0 ? { scope } : {},
6349
+ operatorWrite: opts.isOperatorWrite?.() === true
6350
+ });
6351
+ if (Object.keys(result.patch).length > 0) await rawWrite(result.patch);
6352
+ const { orphaned } = await result.afterWrite();
6353
+ if (orphaned.length > 0) opts.onOrphaned(orphaned);
6354
+ });
6355
+ };
6356
+ const wrapped = {
6357
+ writeAddonStore: (patch) => write("addon", opts.addonTree(), void 0, patch, () => readStoredAddonBlob(raw), (p) => raw.writeAddonStore(p)),
6358
+ writeDeviceStore: (deviceId, patch) => write(`device:${deviceId}`, opts.deviceTree(), {
6359
+ kind: "device",
6360
+ deviceId
6361
+ }, patch, () => raw.readDeviceStore(deviceId), (p) => raw.writeDeviceStore(deviceId, p))
6362
+ };
6363
+ return new Proxy(raw, { get(target, prop, receiver) {
6364
+ if (prop === "writeAddonStore") return wrapped.writeAddonStore;
6365
+ if (prop === "writeDeviceStore") return wrapped.writeDeviceStore;
6366
+ const value = Reflect.get(target, prop, receiver);
6367
+ return typeof value === "function" ? value.bind(target) : value;
6368
+ } });
6369
+ }
6370
+ /** The context the addon sees: identical, except that its settings writes are secret-aware. */
6371
+ function withSecretAwareSettings(context, opts) {
6372
+ const raw = context.settings;
6373
+ if (raw === void 0) return context;
6374
+ const settings = createSecretAwareSettings(raw, opts);
6375
+ return new Proxy(context, { get(target, prop, receiver) {
6376
+ if (prop === "settings") return settings;
6377
+ return Reflect.get(target, prop, receiver);
6378
+ } });
6379
+ }
6380
+ var LEAF = { kind: "leaf" };
6381
+ var DEFAULT_ITEM_ID_KEY = "id";
6382
+ function collectFields(fields, out) {
6383
+ for (const field of fields) {
6384
+ if (field.type === "group") {
6385
+ collectFields(field.fields, out);
6386
+ continue;
6387
+ }
6388
+ if (field.type === "sub-tabs") {
6389
+ for (const tab of field.tabs) collectFields(tab.fields, out);
6390
+ continue;
6391
+ }
6392
+ if (field.type === "editable-array") {
6393
+ const item = /* @__PURE__ */ new Map();
6394
+ for (const itemField of field.itemFields) if ("key" in itemField && isKeychainStoredField(itemField)) item.set(itemField.key, LEAF);
6395
+ if (item.size > 0) out.set(field.key, {
6396
+ kind: "array",
6397
+ idKey: field.itemIdKey ?? DEFAULT_ITEM_ID_KEY,
6398
+ item
6399
+ });
6400
+ continue;
6401
+ }
6402
+ if (!("key" in field) || typeof field.key !== "string") continue;
6403
+ if (isKeychainStoredField(field)) out.set(field.key, LEAF);
6404
+ }
6405
+ }
6406
+ /** The secret tree a settings schema declares. */
6407
+ function secretTreeFromSchema(schema) {
6408
+ const out = /* @__PURE__ */ new Map();
6409
+ if (schema === null) return out;
6410
+ for (const section of schema.sections) collectFields(section.fields, out);
6411
+ return out;
6412
+ }
6413
+ /** Parse `a.b[id].c` into segments; throws on a malformed path (a declaration bug). */
6414
+ function parseSecretStatePath(path) {
6415
+ const segments = [];
6416
+ for (const raw of path.split(".")) {
6417
+ const m = /^([^[\]]+)(?:\[([^[\]]+)\])?$/.exec(raw);
6418
+ if (m === null || m[1] === void 0) throw new Error(`secretStatePaths: malformed path "${path}"`);
6419
+ segments.push(m[2] !== void 0 ? {
6420
+ key: m[1],
6421
+ idKey: m[2]
6422
+ } : { key: m[1] });
6423
+ }
6424
+ if (segments.length === 0) throw new Error(`secretStatePaths: empty path`);
6425
+ if (segments[segments.length - 1]?.idKey !== void 0) throw new Error(`secretStatePaths: "${path}" ends on an array — name the member that is secret`);
6426
+ return segments;
6427
+ }
6428
+ function insert(into, segments) {
6429
+ const [head, ...rest] = segments;
6430
+ if (head === void 0) return;
6431
+ if (rest.length === 0) {
6432
+ into.set(head.key, LEAF);
6433
+ return;
6434
+ }
6435
+ const existing = into.get(head.key);
6436
+ if (head.idKey !== void 0) {
6437
+ const item = new Map(existing?.kind === "array" ? existing.item : []);
6438
+ insert(item, rest);
6439
+ into.set(head.key, {
6440
+ kind: "array",
6441
+ idKey: head.idKey,
6442
+ item
6443
+ });
6444
+ return;
6445
+ }
6446
+ const children = new Map(existing?.kind === "object" ? existing.children : []);
6447
+ insert(children, rest);
6448
+ into.set(head.key, {
6449
+ kind: "object",
6450
+ children
6451
+ });
6452
+ }
6453
+ /** Add hidden-state paths (`secretStatePaths`) to a schema-derived tree. */
6454
+ function mergeSecretStatePaths(tree, paths) {
6455
+ if (paths.length === 0) return tree;
6456
+ const out = new Map(tree);
6457
+ for (const path of paths) insert(out, parseSecretStatePath(path));
6458
+ return out;
6459
+ }
6460
+ /** First retry of a secret the keychain could not serve; doubles to {@link SECRET_RETRY_MAX_MS}. */
6461
+ var SECRET_RETRY_FIRST_MS = 5e3;
6462
+ var SECRET_RETRY_MAX_MS = 5 * 6e4;
6463
+ /** Coalesces a burst of `keychain.changed` events into one re-resolve. */
6464
+ var SECRET_CHANGE_DEBOUNCE_MS = 250;
5929
6465
  /**
5930
6466
  * Base class for CamStack addons. Eliminates settings boilerplate:
5931
6467
  *
@@ -5995,6 +6531,25 @@ var BaseAddon = class {
5995
6531
  settingsStoreReady = false;
5996
6532
  /** Default config values. Provided via constructor. */
5997
6533
  defaults;
6534
+ /**
6535
+ * Hidden-state paths that hold secrets (D787) — values the addon writes
6536
+ * itself (`updateGlobalSettings`, `state()`) that no form field declares.
6537
+ * Stored in the keychain exactly like a `password` field. Syntax:
6538
+ * `'snapshotSigningKey'`, `'identity.pincode'`, `'tokens[userId].accessToken'`
6539
+ * (`key[<id field>]` addresses array items by id, never by index).
6540
+ */
6541
+ secretStatePaths = [];
6542
+ _addonSecretTree = null;
6543
+ _deviceSecretTree = null;
6544
+ /** Refs the last resolved config pointed at — a change to one re-resolves. */
6545
+ _configSecretRefs = /* @__PURE__ */ new Set();
6546
+ _unresolvedSecrets = [];
6547
+ _secretRetryTimer = null;
6548
+ _secretRetryDelayMs = SECRET_RETRY_FIRST_MS;
6549
+ _secretChangeTimer = null;
6550
+ _secretChangeUnsub = null;
6551
+ /** >0 while an OPERATOR's settings write is being applied. */
6552
+ _operatorWriteDepth = 0;
5998
6553
  constructor(defaults) {
5999
6554
  this.defaults = defaults;
6000
6555
  this._config = { ...defaults };
@@ -6030,8 +6585,18 @@ var BaseAddon = class {
6030
6585
  return this._config;
6031
6586
  }
6032
6587
  async initialize(context) {
6033
- this._ctx = context;
6588
+ const secrets = context.secrets;
6589
+ this._ctx = secrets !== void 0 ? withSecretAwareSettings(context, {
6590
+ secrets,
6591
+ addonTree: () => this.addonSecretTree(),
6592
+ deviceTree: () => this.deviceSecretTree(),
6593
+ onOrphaned: (refs) => context.logger.warn("keychain refs dropped by a settings write could not be deleted", { meta: { refs: [...refs] } }),
6594
+ isOperatorWrite: () => this._operatorWriteDepth > 0
6595
+ }) : context;
6034
6596
  await this.resolveConfig();
6597
+ if (secrets !== void 0) this._secretChangeUnsub = secrets.onChange((ref) => {
6598
+ if (this._configSecretRefs.has(ref)) this.scheduleSecretReresolve(SECRET_CHANGE_DEBOUNCE_MS);
6599
+ });
6035
6600
  const result = await this.onInitialize();
6036
6601
  this.emitLifecycle(EventCategory.AddonStarted);
6037
6602
  const normalized = normalizeAddonInitResult(result);
@@ -6064,6 +6629,12 @@ var BaseAddon = class {
6064
6629
  */
6065
6630
  async onHubReachable() {}
6066
6631
  async shutdown() {
6632
+ this._secretChangeUnsub?.();
6633
+ this._secretChangeUnsub = null;
6634
+ if (this._secretRetryTimer !== null) clearTimeout(this._secretRetryTimer);
6635
+ if (this._secretChangeTimer !== null) clearTimeout(this._secretChangeTimer);
6636
+ this._secretRetryTimer = null;
6637
+ this._secretChangeTimer = null;
6067
6638
  this.emitLifecycle(EventCategory.AddonStopped);
6068
6639
  if (this.autoEmitReadiness) this.emitReadinessForProviders("down");
6069
6640
  await this.onShutdown();
@@ -6142,7 +6713,7 @@ var BaseAddon = class {
6142
6713
  return hydrateSchema(schema, overlay ? {
6143
6714
  ...projected,
6144
6715
  ...overlay
6145
- } : projected);
6716
+ } : projected, { inlineSecretsAsViews: true });
6146
6717
  }
6147
6718
  /**
6148
6719
  * The integration-level view of this addon's settings: exactly the sections
@@ -6191,7 +6762,7 @@ var BaseAddon = class {
6191
6762
  return hydrateSchema({
6192
6763
  ...schema,
6193
6764
  sections
6194
- }, projected);
6765
+ }, projected, { inlineSecretsAsViews: true });
6195
6766
  }
6196
6767
  /**
6197
6768
  * The raw addon store PROJECTED onto the target node's bare per-node keys:
@@ -6245,18 +6816,50 @@ var BaseAddon = class {
6245
6816
  value: result.value[key]
6246
6817
  };
6247
6818
  }
6819
+ /**
6820
+ * The OPERATOR's write — a settings form or an admin API call, dispatched by
6821
+ * the kernel. Identical to {@link updateGlobalSettings}, except that a secret
6822
+ * the operator sends is stored as sent even when it equals a pre-keychain
6823
+ * inline value: the operator re-entering a password is a change, an addon
6824
+ * writing back what it read is not (D787).
6825
+ */
6826
+ async updateGlobalSettingsAsOperator(patch, nodeId) {
6827
+ this._operatorWriteDepth++;
6828
+ try {
6829
+ await this.applyGlobalPatch(patch, nodeId);
6830
+ } finally {
6831
+ this._operatorWriteDepth--;
6832
+ }
6833
+ }
6248
6834
  async updateGlobalSettings(patch, nodeId) {
6835
+ await this.applyGlobalPatch(patch, nodeId);
6836
+ }
6837
+ /**
6838
+ * REMOVE stored secrets — their keychain values and their markers (D787).
6839
+ *
6840
+ * Writing `''` to a secret field KEEPS what is stored (an empty string is
6841
+ * what a defaults fallback writes back, and it must never destroy a
6842
+ * credential), so code that means "forget this token" says so here. Without
6843
+ * a keychain (an older kernel) it writes `''`, as such code always did.
6844
+ */
6845
+ async clearSecrets(keys, nodeId) {
6846
+ const tree = this.addonSecretTree();
6847
+ const keychain = this._ctx?.secrets !== void 0;
6848
+ const patch = {};
6849
+ for (const key of keys) patch[key] = keychain && tree.has(key) ? SECRET_CLEAR : "";
6850
+ await this.applyGlobalPatch(patch, nodeId);
6851
+ }
6852
+ async applyGlobalPatch(barePatch, nodeId) {
6249
6853
  const keys = this.perNodeKeys();
6250
6854
  const localNode = normalizeNodeId(this._ctx?.kernel?.localNodeId);
6251
6855
  const target = normalizeNodeId(nodeId ?? this._ctx?.kernel?.localNodeId);
6252
- const barePatch = patch;
6253
6856
  const scoped = keys.size > 0 ? scopePatch(barePatch, keys, target) : barePatch;
6254
6857
  await this._ctx?.settings?.writeAddonStore(scoped);
6255
6858
  if (target !== localNode) return;
6256
6859
  await this.resolveConfig();
6257
6860
  await this.onConfigChanged();
6258
6861
  this.emitLifecycle(EventCategory.AddonUpdated, { level: "global" });
6259
- this.maybeAutoRestart(patch, this.globalSettingsSchema());
6862
+ this.maybeAutoRestart(Object.keys(barePatch), this.globalSettingsSchema());
6260
6863
  }
6261
6864
  /**
6262
6865
  * The set of field keys the global settings schema declares `perNode: true`
@@ -6281,7 +6884,7 @@ var BaseAddon = class {
6281
6884
  * time to return its response before the addon is torn down and
6282
6885
  * re-initialised by `AddonRegistryService.restartAddon`.
6283
6886
  */
6284
- maybeAutoRestart(patch, schema) {
6887
+ maybeAutoRestart(patchKeys, schema) {
6285
6888
  if (!schema) return;
6286
6889
  const restartKeys = /* @__PURE__ */ new Set();
6287
6890
  for (const section of schema.sections) for (const field of section.fields) {
@@ -6289,13 +6892,13 @@ var BaseAddon = class {
6289
6892
  if (field.requiresRestart) restartKeys.add(field.key);
6290
6893
  }
6291
6894
  if (restartKeys.size === 0) return;
6292
- if (!Object.keys(patch).some((k) => restartKeys.has(k))) return;
6895
+ if (!patchKeys.some((k) => restartKeys.has(k))) return;
6293
6896
  const ctx = this._ctx;
6294
6897
  if (!ctx) return;
6295
6898
  const addonId = ctx.id;
6296
6899
  setImmediate(() => {
6297
6900
  ctx.api.addons?.restartAddon?.mutate({ addonId }).then(() => {
6298
- ctx.logger.info("addon auto-restart triggered by restart-required setting change", { meta: { changedFields: Object.keys(patch).filter((k) => restartKeys.has(k)) } });
6901
+ ctx.logger.info("addon auto-restart triggered by restart-required setting change", { meta: { changedFields: patchKeys.filter((k) => restartKeys.has(k)) } });
6299
6902
  }).catch((err) => {
6300
6903
  ctx.logger.error("addon auto-restart failed", { meta: { error: err instanceof Error ? err.message : String(err) } });
6301
6904
  });
@@ -6304,7 +6907,7 @@ var BaseAddon = class {
6304
6907
  async getDeviceSettings(deviceId) {
6305
6908
  const schema = this.deviceSettingsSchema();
6306
6909
  if (!schema) return { sections: [] };
6307
- return hydrateSchema(schema, await this._ctx?.settings?.readDeviceStore(deviceId) ?? {});
6910
+ return hydrateSchema(schema, await this._ctx?.settings?.readDeviceStore(deviceId) ?? {}, { inlineSecretsAsViews: true });
6308
6911
  }
6309
6912
  async updateDeviceSettings(deviceId, patch) {
6310
6913
  await this._ctx?.settings?.writeDeviceStore(deviceId, patch);
@@ -6434,7 +7037,7 @@ var BaseAddon = class {
6434
7037
  * selection at boot (absent scoped key ⇒ the constructor default wins).
6435
7038
  */
6436
7039
  async resolveConfig() {
6437
- const stored = await this.readAddonStoreWithRetry();
7040
+ const stored = await this.resolveStoredSecrets(await this.readAddonStoreWithRetry());
6438
7041
  const perNode = this.perNodeKeys();
6439
7042
  const localNode = normalizeNodeId(this._ctx?.kernel?.localNodeId);
6440
7043
  const resolved = { ...this.defaults };
@@ -6459,7 +7062,7 @@ var BaseAddon = class {
6459
7062
  key,
6460
7063
  schema,
6461
7064
  fallback,
6462
- read: () => this.readAddonStoreWithRetry(),
7065
+ read: async () => this.resolveStoredSecrets(await this.readAddonStoreWithRetry()),
6463
7066
  write: async (patch) => {
6464
7067
  await this._ctx?.settings?.writeAddonStore(patch);
6465
7068
  },
@@ -6476,7 +7079,7 @@ var BaseAddon = class {
6476
7079
  key,
6477
7080
  schema,
6478
7081
  fallback,
6479
- read: async () => await this._ctx?.settings?.readDeviceStore(deviceId) ?? {},
7082
+ read: async () => this.resolveStoredSecrets(await this._ctx?.settings?.readDeviceStore(deviceId) ?? {}, false),
6480
7083
  write: async (patch) => {
6481
7084
  await this._ctx?.settings?.writeDeviceStore(deviceId, patch);
6482
7085
  },
@@ -6488,6 +7091,70 @@ var BaseAddon = class {
6488
7091
  } })
6489
7092
  });
6490
7093
  }
7094
+ /** The addon store's secret tree: schema fields by TYPE, plus `secretStatePaths`. */
7095
+ addonSecretTree() {
7096
+ this._addonSecretTree ??= mergeSecretStatePaths(secretTreeFromSchema(this.globalSettingsSchema()), this.secretStatePaths);
7097
+ return this._addonSecretTree;
7098
+ }
7099
+ deviceSecretTree() {
7100
+ this._deviceSecretTree ??= secretTreeFromSchema(this.deviceSettingsSchema());
7101
+ return this._deviceSecretTree;
7102
+ }
7103
+ /**
7104
+ * Secrets of this addon's stored config that could not be resolved on the
7105
+ * last read, with why. Non-empty means the addon is running WITHOUT them —
7106
+ * an addon whose function needs one should report itself degraded rather
7107
+ * than start on an empty credential.
7108
+ */
7109
+ get unresolvedSecrets() {
7110
+ return this._unresolvedSecrets;
7111
+ }
7112
+ /**
7113
+ * Substitute every keychain marker in a stored blob with its value. With no
7114
+ * keychain (no `ctx.secrets`) or no marker, the blob comes back unchanged.
7115
+ * An unresolvable value is LEFT OUT (never `''`), logged by ref — never by
7116
+ * value — and, when the cause can pass, retried with backoff. Nothing is
7117
+ * ever written back from here.
7118
+ */
7119
+ async resolveStoredSecrets(blob, trackForConfig = true) {
7120
+ const secrets = this._ctx?.secrets;
7121
+ if (secrets === void 0) return blob;
7122
+ const refs = collectSecretMarkerRefs(blob);
7123
+ if (trackForConfig) this._configSecretRefs = new Set(refs);
7124
+ if (refs.length === 0) {
7125
+ if (trackForConfig) this._unresolvedSecrets = [];
7126
+ return blob;
7127
+ }
7128
+ const { value, unresolved } = await resolveSecretMarkers(blob, secrets);
7129
+ if (trackForConfig) {
7130
+ this._unresolvedSecrets = unresolved;
7131
+ if (unresolved.length > 0) {
7132
+ this._ctx?.logger?.error?.("secret unavailable — running without it, will retry", { meta: { secrets: unresolved.map((u) => ({
7133
+ ref: u.ref,
7134
+ reason: u.reason
7135
+ })) } });
7136
+ if (unresolved.some((u) => u.reason !== "missing" && u.reason !== "forbidden")) {
7137
+ this.scheduleSecretReresolve(this._secretRetryDelayMs);
7138
+ this._secretRetryDelayMs = Math.min(this._secretRetryDelayMs * 2, SECRET_RETRY_MAX_MS);
7139
+ }
7140
+ } else this._secretRetryDelayMs = SECRET_RETRY_FIRST_MS;
7141
+ }
7142
+ return value;
7143
+ }
7144
+ /** Re-resolve the config (a keychain-only change, or a retry) and apply it. */
7145
+ scheduleSecretReresolve(delayMs) {
7146
+ const timerOf = delayMs === SECRET_CHANGE_DEBOUNCE_MS ? "_secretChangeTimer" : "_secretRetryTimer";
7147
+ if (this[timerOf] !== null) return;
7148
+ const timer = setTimeout(() => {
7149
+ this[timerOf] = null;
7150
+ if (this._ctx === null) return;
7151
+ this.resolveConfig().then(() => this.onConfigChanged()).catch((err) => {
7152
+ this._ctx?.logger?.error?.("re-resolving secrets failed", { meta: { error: err instanceof Error ? err.message : String(err) } });
7153
+ });
7154
+ }, delayMs);
7155
+ timer.unref?.();
7156
+ this[timerOf] = timer;
7157
+ }
6491
7158
  /**
6492
7159
  * Wrap `ctx.settings.readAddonStore()` with a short retry budget so a
6493
7160
  * transient settings-store outage (mid-restart of sqlite-settings, tsx-watch
@@ -15067,6 +15734,196 @@ id: union([string(), number()]) }), {
15067
15734
  kind: "mutation",
15068
15735
  auth: "admin"
15069
15736
  });
15737
+ /**
15738
+ * `keychain` — the hub's encrypted store of secret values (D787).
15739
+ *
15740
+ * Settings blobs never hold a secret: they hold a marker
15741
+ * `{"$secret":"secret://<owner>/<scope>/<fieldPath>","v":1}` and the value
15742
+ * lives here, AES-256-GCM under a data key that a master key wraps. This cap
15743
+ * is the OWNER's door to its own values: every method is owner-checked
15744
+ * against the CALLER, never against anything the caller claims.
15745
+ *
15746
+ * Who the caller is: the transport stamps `callerAddonId` into the args of a
15747
+ * call that crossed a process (the parent `LocalChildRegistry` from the UDS
15748
+ * connection, the agent from its child's connection carried in
15749
+ * `$hub-cap-fwd`'s envelope). A stamp always overwrites; a wire call with no
15750
+ * stamp has its claim stripped. Only a hub in-process caller may state its own
15751
+ * identity — `ctx.secrets` does that with the addon id closed over. This is
15752
+ * identity against BUGS and misrouting, not against a malicious addon (every
15753
+ * runner is the same OS user): see the threat model in
15754
+ * `docs/design/2026-10-08-secrets-keychain.md`.
15755
+ *
15756
+ * `callerAddonId` is deliberately NOT in any input schema: the tRPC router
15757
+ * parses the input, so a session can never supply it, and the internal mount
15758
+ * floors every method to admin anyway (D240).
15759
+ *
15760
+ * Internal: addons reach it through `ctx.secrets`, never directly.
15761
+ */
15762
+ /** Why a value cannot be served right now. Never folded into "missing". */
15763
+ var KeychainUnavailableReasonSchema = _enum([
15764
+ "key-missing",
15765
+ "wrong-key",
15766
+ "key-file-unreadable",
15767
+ "store-unavailable",
15768
+ "not-initialized",
15769
+ "decrypt-failed"
15770
+ ]);
15771
+ var KeychainStateSchema = discriminatedUnion("state", [object({ state: literal("ready") }), object({
15772
+ state: literal("locked"),
15773
+ reason: KeychainUnavailableReasonSchema
15774
+ })]);
15775
+ var KeychainResolveEntrySchema = discriminatedUnion("kind", [
15776
+ object({
15777
+ kind: literal("value"),
15778
+ value: string(),
15779
+ version: number().int()
15780
+ }),
15781
+ object({ kind: literal("missing") }),
15782
+ object({
15783
+ kind: literal("unavailable"),
15784
+ reason: KeychainUnavailableReasonSchema
15785
+ }),
15786
+ object({ kind: literal("forbidden") })
15787
+ ]);
15788
+ var KeychainResolveResultSchema = record(string(), KeychainResolveEntrySchema);
15789
+ /** Metadata of one stored value — never the value. */
15790
+ var KeychainSecretMetaSchema = object({
15791
+ ref: string(),
15792
+ owner: string(),
15793
+ /** Formatted scope (`global`, `node/<id>`, `device/<id>`, …). */
15794
+ scope: string(),
15795
+ fieldPath: string(),
15796
+ version: number().int(),
15797
+ createdAt: number(),
15798
+ updatedAt: number(),
15799
+ /** `addon:<id>` | `user:<id>` | `system`. */
15800
+ updatedBy: string(),
15801
+ /** Coarse — written at most once an hour per ref. `null` = never read. */
15802
+ lastReadAt: number().nullable()
15803
+ });
15804
+ var RefListSchema = array(string().min(1)).max(512);
15805
+ method(object({
15806
+ ref: string().min(1),
15807
+ value: string().min(1)
15808
+ }), object({
15809
+ ref: string(),
15810
+ version: number().int()
15811
+ }), {
15812
+ kind: "mutation",
15813
+ auth: "admin"
15814
+ }), method(object({ refs: RefListSchema }), KeychainResolveResultSchema, { auth: "admin" }), method(object({ refs: RefListSchema }), object({ deleted: number().int() }), {
15815
+ kind: "mutation",
15816
+ auth: "admin"
15817
+ }), method(object({}), array(KeychainSecretMetaSchema).readonly(), { auth: "admin" }), method(object({}), KeychainStateSchema, { auth: "admin" });
15818
+ var KeychainKeySourceSchema = object({
15819
+ kind: _enum([
15820
+ "config-file",
15821
+ "previous-config-file",
15822
+ "platform",
15823
+ "default-file"
15824
+ ]),
15825
+ /** File sources only — a path is not a secret. */
15826
+ path: string().optional(),
15827
+ /**
15828
+ * True when a whole-volume backup of the data directory carries this key
15829
+ * next to the database it protects (the default file). The page warns.
15830
+ */
15831
+ sharesBackupWithDatabase: boolean()
15832
+ });
15833
+ var KeychainStatusSchema = object({
15834
+ keychain: KeychainStateSchema,
15835
+ /** The source of the master key in use; `null` while locked. */
15836
+ keySource: KeychainKeySourceSchema.nullable(),
15837
+ /** The source configured to be primary — differs from `keySource` while a rotation is pending. */
15838
+ configuredSource: KeychainKeySourceSchema,
15839
+ /** Short fingerprint of the master key in use (`HMAC(KEK, label)`, 16 hex). */
15840
+ fingerprint: string().nullable(),
15841
+ activeKeyId: string().nullable(),
15842
+ /**
15843
+ * The keychain unlocked with a NON-primary source: the configured primary
15844
+ * key is not the one that wraps the data key yet. `rotateMasterKey` moves it.
15845
+ */
15846
+ rotationPending: boolean(),
15847
+ /** Wrapped copies of the data key under a previous master key, kept until purged. */
15848
+ retiredWraps: number().int(),
15849
+ recoveryKeyDownloadedAt: number().nullable(),
15850
+ secretCount: number().int()
15851
+ });
15852
+ /**
15853
+ * Whether a stored value is still pointed at by its owner's settings.
15854
+ * `unknown` is a first-class answer: a scope whose blob could not be read is
15855
+ * never reported as an orphan (and never deleted as one).
15856
+ */
15857
+ var KeychainSecretUsageSchema = _enum([
15858
+ "in-use",
15859
+ "orphan",
15860
+ "unknown"
15861
+ ]);
15862
+ var KeychainAdminSecretSchema = KeychainSecretMetaSchema.extend({ usage: KeychainSecretUsageSchema });
15863
+ var KeychainAuditActionSchema = _enum([
15864
+ "put",
15865
+ "delete",
15866
+ "reveal",
15867
+ "rotate-master-key",
15868
+ "purge-retired-keys",
15869
+ "download-recovery-key",
15870
+ "delete-orphans",
15871
+ "reset",
15872
+ "initialize"
15873
+ ]);
15874
+ var KeychainAuditEntrySchema = object({
15875
+ id: string(),
15876
+ at: number(),
15877
+ action: KeychainAuditActionSchema,
15878
+ /** `addon:<id>` | `user:<id>` | `system`. */
15879
+ actor: string(),
15880
+ ref: string().nullable(),
15881
+ detail: string().nullable()
15882
+ });
15883
+ method(object({}), KeychainStatusSchema, { auth: "admin" }), method(object({}), array(KeychainAdminSecretSchema).readonly(), { auth: "admin" }), method(object({ limit: number().int().min(1).max(500).optional() }), array(KeychainAuditEntrySchema).readonly(), { auth: "admin" }), method(object({ ref: string().min(1) }), object({
15884
+ value: string(),
15885
+ version: number().int()
15886
+ }), {
15887
+ kind: "mutation",
15888
+ auth: "admin",
15889
+ caller: "required"
15890
+ }), method(object({ refs: array(string().min(1)).max(1e3) }), object({
15891
+ deleted: array(string()).readonly(),
15892
+ kept: array(object({
15893
+ ref: string(),
15894
+ usage: KeychainSecretUsageSchema
15895
+ })).readonly()
15896
+ }), {
15897
+ kind: "mutation",
15898
+ auth: "admin",
15899
+ caller: "required"
15900
+ }), method(object({}), object({
15901
+ fingerprint: string(),
15902
+ keySource: KeychainKeySourceSchema
15903
+ }), {
15904
+ kind: "mutation",
15905
+ auth: "admin",
15906
+ caller: "required"
15907
+ }), method(object({}), object({ purged: number().int() }), {
15908
+ kind: "mutation",
15909
+ auth: "admin",
15910
+ caller: "required"
15911
+ }), method(object({}), object({
15912
+ key: string(),
15913
+ fingerprint: string(),
15914
+ keySource: KeychainKeySourceSchema
15915
+ }), {
15916
+ kind: "mutation",
15917
+ auth: "admin",
15918
+ caller: "required"
15919
+ }), method(object({ confirm: literal("RESET KEYCHAIN") }), object({
15920
+ archivedCollection: string().nullable(),
15921
+ archived: number().int()
15922
+ }), {
15923
+ kind: "mutation",
15924
+ auth: "admin",
15925
+ caller: "required"
15926
+ });
15070
15927
  method(_void(), object({
15071
15928
  id: string(),
15072
15929
  name: string(),
@@ -15884,6 +16741,10 @@ var DeviceInfoSchema = object({
15884
16741
  isCamera: boolean(),
15885
16742
  /** Current config values — serializable snapshot */
15886
16743
  config: record(string(), unknown()),
16744
+ /** Paths inside `config` the device's schema declares secret (D783) —
16745
+ * what the non-admin redaction withholds. Paths, never values. Absent
16746
+ * when the owning runner has not reported them. */
16747
+ secretConfigPaths: array(string()).optional(),
15887
16748
  /** Hardware + identity blob (manufacturer / model / firmware / sn /
15888
16749
  * uid / mac / …). Populated by drivers; editable via setMetadata. */
15889
16750
  metadata: record(string(), unknown()).nullable().optional(),
@@ -16089,12 +16950,22 @@ var DeviceRegisterPayloadSchema = object({
16089
16950
  * call (workers re-register on init), so probe-driven updates
16090
16951
  * (e.g. PTZ flag flipping after the abilities probe) propagate. */
16091
16952
  features: array(string()).optional(),
16092
- config: record(string(), unknown())
16953
+ config: record(string(), unknown()),
16954
+ /** Config paths the device's own schema DECLARES secret
16955
+ * (`.meta({ secret: true })` — dotted, `[]` for array items, e.g.
16956
+ * `connection.password`). Re-published on every register; the non-admin
16957
+ * redaction withholds them at any depth (D783). Optional: an older runner
16958
+ * omits it and the keyword backstop applies. */
16959
+ secretConfigPaths: array(string()).optional()
16093
16960
  });
16094
16961
  var DeviceRemovePayloadSchema = object({ deviceId: number() });
16095
16962
  var DevicePersistConfigPayloadSchema = object({
16096
16963
  deviceId: number(),
16097
- data: record(string(), unknown())
16964
+ data: record(string(), unknown()),
16965
+ /** The paths the device's schema declares secret (D783), when the caller
16966
+ * knows them — those values are kept in the keychain, not the blob (D787).
16967
+ * Absent: the paths the device last reported at `registerDevice` apply. */
16968
+ secretConfigPaths: array(string()).optional()
16098
16969
  });
16099
16970
  /** What a migration actually did, per switch. `unreachable` is a first-class
16100
16971
  * answer: a camera that could not be asked is not a camera that was silenced. */
@@ -17748,7 +18619,14 @@ var LoginMethodContributionSchema = discriminatedUnion("kind", [
17748
18619
  icon: string().optional(),
17749
18620
  /** Addon-owned HTTP route the button navigates to (GET). */
17750
18621
  startUrl: string(),
17751
- stage: LoginStageEnum
18622
+ stage: LoginStageEnum,
18623
+ /**
18624
+ * The provider names an external identity a signed-in user can link to
18625
+ * their own account (D782): `auth.listLinkableProviders` /
18626
+ * `beginExternalLink` offer exactly the redirect methods that set this,
18627
+ * whatever their id. Absent / false — a login button only (magic-link).
18628
+ */
18629
+ linkable: boolean().optional()
17752
18630
  }),
17753
18631
  object({
17754
18632
  kind: literal("widget"),
@@ -23116,8 +23994,23 @@ var RetrainMacroClassSchema = _enum([
23116
23994
  ]);
23117
23995
  /** A subject to learn, or a phantom to unlearn (taught by OMISSION). */
23118
23996
  var RetrainAnnotationKindSchema = _enum(["subject", "model_error"]);
23119
- /** Did a human draw this box, or did the assist propose it? */
23120
- var RetrainAnnotationSourceSchema = _enum(["operator", "assist"]);
23997
+ /**
23998
+ * Where a box came from. A machine-sourced box is a DRAFT on the page until
23999
+ * the operator confirms it; once saved it keeps its source as provenance.
24000
+ *
24001
+ * - `operator` — drawn by hand.
24002
+ * - `assist` — proposed by a detector run on the copy (the camera's own
24003
+ * model, or a larger teacher model).
24004
+ * - `pipeline` — the box the live pipeline actually drew on that frame, read
24005
+ * back from the track store (never a re-run).
24006
+ * - `llm` — proposed by a vision-capable LLM profile.
24007
+ */
24008
+ var RetrainAnnotationSourceSchema = _enum([
24009
+ "operator",
24010
+ "assist",
24011
+ "pipeline",
24012
+ "llm"
24013
+ ]);
23121
24014
  /** Normalised `[0,1]` rectangle against the FULL frame — the canonical form. */
23122
24015
  var RetrainBboxSchema = object({
23123
24016
  x: number(),
@@ -23135,7 +24028,8 @@ var RetrainBboxSchema = object({
23135
24028
  */
23136
24029
  var RetrainAnnotationSchema = object({
23137
24030
  id: string(),
23138
- trackId: string(),
24031
+ /** The frame's track. Absent on a recording still, which has none. */
24032
+ trackId: string().optional(),
23139
24033
  deviceId: number(),
23140
24034
  /** The COPY in retrain storage — never the source track's media key. */
23141
24035
  mediaKey: string(),
@@ -23184,18 +24078,63 @@ var RetrainFrameCandidateSchema = object({
23184
24078
  * fail, whatever became of the original. */
23185
24079
  copied: boolean()
23186
24080
  });
24081
+ /**
24082
+ * One box the live pipeline drew on a copied frame — what a correction starts
24083
+ * from. Derived when the frame is listed, from the track store: the subject
24084
+ * box recorded on the source media row when there is one, else the track
24085
+ * position nearest the frame's capture time. Never stored as an annotation
24086
+ * until the operator confirms it.
24087
+ */
24088
+ var RetrainPipelineBoxSchema = object({
24089
+ /** Normalised `[0,1]` against the full frame. */
24090
+ bbox: RetrainBboxSchema,
24091
+ macroClass: RetrainMacroClassSchema,
24092
+ label: string().optional(),
24093
+ subLabel: string().optional(),
24094
+ /** The detection score of the position used, when it was recorded. */
24095
+ score: number().optional(),
24096
+ /** The root model that produced the track, when it was recorded. */
24097
+ modelId: string().optional(),
24098
+ trackId: string(),
24099
+ /** `subject-box`: recorded on this very frame. `position`: the track's
24100
+ * position nearest the capture time, `deltaMs` away. */
24101
+ basis: _enum(["subject-box", "position"]),
24102
+ deltaMs: number()
24103
+ });
24104
+ /**
24105
+ * Where a retrain frame came from: a TRACK's stored media (copy-on-select),
24106
+ * or the RECORDING at an instant — the frame a model saw nothing in has no
24107
+ * track at all.
24108
+ */
24109
+ var RetrainFrameOriginSchema = _enum(["track", "recording"]);
23187
24110
  /** A frame the dataset OWNS: bytes copied at selection time. */
23188
24111
  var RetrainFrameSchema = object({
23189
24112
  frameId: string(),
23190
24113
  deviceId: number(),
23191
- trackId: string(),
23192
- /** Provenance only. It may already point at nothing — that is expected. */
23193
- sourceMediaId: number().int(),
24114
+ origin: RetrainFrameOriginSchema,
24115
+ /** The track it was copied from. Absent on a recording still. */
24116
+ trackId: string().optional(),
24117
+ /** Provenance only. It may already point at nothing — that is expected.
24118
+ * Absent on a recording still. */
24119
+ sourceMediaId: number().int().optional(),
24120
+ /** A recording still: the playhead instant it was taken at. */
24121
+ epochMs: number().optional(),
23194
24122
  sourceKind: MediaFileKindEnum,
23195
24123
  sizeBytes: number().int(),
23196
24124
  width: number().int(),
23197
24125
  height: number().int(),
23198
- copiedAt: number()
24126
+ copiedAt: number(),
24127
+ /** When the CAMERA captured the frame. Absent on a copy taken before D785. */
24128
+ capturedAt: number().optional(),
24129
+ /** The operator's "every subject in this frame is boxed". Detector
24130
+ * evaluation keeps only complete frames: an unboxed subject would count as
24131
+ * a false positive. */
24132
+ complete: boolean(),
24133
+ /** The boxes the pipeline drew on this frame. Present on list answers;
24134
+ * empty when nothing the track store holds can be placed on it. */
24135
+ pipelineBoxes: array(RetrainPipelineBoxSchema).readonly().optional(),
24136
+ /** The named collections this frame belongs to. Present on list answers. */
24137
+ collectionIds: array(string()).readonly().optional()
23199
24138
  });
23200
24139
  /** Why a copy-on-select could not be honoured — named, never a silent skip. */
23201
24140
  var RetrainCopyRefusalSchema = _enum([
@@ -23216,6 +24155,53 @@ var RetrainFrameListSchema = object({
23216
24155
  /** What the page pre-selects — the native key frame when one survives. */
23217
24156
  autoPickMediaId: number().int().optional()
23218
24157
  });
24158
+ /**
24159
+ * Which frames an annotated export carries. Every field narrows; an empty
24160
+ * filter is every annotated copy. A dataset VERSION is a frozen `frameIds`
24161
+ * list owned by whoever trains (the training orchestrator), so `frameIds` is
24162
+ * the primitive a version export uses; `batchId` re-selects what an earlier
24163
+ * export carried (`exportedInBatch`).
24164
+ */
24165
+ var RetrainExportFilterSchema = object({
24166
+ deviceIds: array(number()).optional(),
24167
+ frameIds: array(string()).max(2e4).optional(),
24168
+ /** Only frames in at least one of these named collections. */
24169
+ collectionIds: array(string()).max(200).optional(),
24170
+ batchId: string().optional(),
24171
+ /** Only frames the operator marked complete (all subjects boxed). */
24172
+ completeOnly: boolean().optional()
24173
+ });
24174
+ /**
24175
+ * `frames`: the full frame copies, boxes in `annotations.json`.
24176
+ * `crops`: additionally one JPEG per subject, cut with the runtime detail-crop
24177
+ * convention — the classifier layout.
24178
+ */
24179
+ var RetrainExportLayoutSchema = _enum(["frames", "crops"]);
24180
+ /**
24181
+ * Where `writeRetrainArchive` streams the tar: one HTTP request whose body is
24182
+ * the archive, sent chunk by chunk. Covers a presigned PUT and a provider
24183
+ * addon's data-plane upload route alike. Headers are sent verbatim and never
24184
+ * logged.
24185
+ */
24186
+ var RetrainArchiveTargetSchema = object({
24187
+ kind: literal("http"),
24188
+ method: _enum(["PUT", "POST"]),
24189
+ url: string().url(),
24190
+ headers: record(string(), string()).optional()
24191
+ });
24192
+ var RetrainArchiveResultSchema = object({
24193
+ batchId: string(),
24194
+ frames: number().int(),
24195
+ crops: number().int(),
24196
+ annotations: number().int(),
24197
+ /** Frames planned but not shipped (unreadable copy, path too long). */
24198
+ missing: number().int(),
24199
+ bytes: number().int(),
24200
+ /** sha256 of the exact bytes sent. */
24201
+ sha256: string(),
24202
+ /** The target's HTTP status. */
24203
+ status: number().int()
24204
+ });
23219
24205
  /** What the operator asked the assist to look for. */
23220
24206
  var RetrainAssistSubjectSchema = discriminatedUnion("kind", [object({
23221
24207
  kind: literal("package"),
@@ -23245,6 +24231,109 @@ var RetrainAssistResultSchema = discriminatedUnion("kind", [object({
23245
24231
  reason: string(),
23246
24232
  detail: string().optional()
23247
24233
  })]);
24234
+ /** A named set over the retrain corpus — membership only, it owns no pixels. */
24235
+ var RetrainCollectionSchema = object({
24236
+ id: string(),
24237
+ name: string(),
24238
+ createdAt: number(),
24239
+ frameCount: number().int()
24240
+ });
24241
+ var RetrainCollectionCreateResultSchema = discriminatedUnion("kind", [object({
24242
+ kind: literal("created"),
24243
+ collection: RetrainCollectionSchema
24244
+ }), object({
24245
+ kind: literal("refused"),
24246
+ reason: _enum(["empty-name", "duplicate-name"])
24247
+ })]);
24248
+ var RetrainCollectionRenameResultSchema = discriminatedUnion("kind", [object({
24249
+ kind: literal("renamed"),
24250
+ collection: RetrainCollectionSchema
24251
+ }), object({
24252
+ kind: literal("refused"),
24253
+ reason: _enum([
24254
+ "empty-name",
24255
+ "duplicate-name",
24256
+ "unknown-collection"
24257
+ ])
24258
+ })]);
24259
+ /**
24260
+ * The outcome of adding the frame at a recording instant. Every refusal is
24261
+ * named: `still-unavailable` (not recording, a gap, or an undecodable frame —
24262
+ * `detail` says which), `still-busy` (the recorder is at its decode bound; try
24263
+ * again), `unreadable-image`, `write-failed`, `unknown-collection`.
24264
+ */
24265
+ var AddRetrainFrameFromRecordingResultSchema = discriminatedUnion("kind", [object({
24266
+ kind: literal("added"),
24267
+ collectionId: string(),
24268
+ frame: RetrainFrameSchema,
24269
+ /** The moment was already in the dataset; nothing was decoded. */
24270
+ reused: boolean()
24271
+ }), object({
24272
+ kind: literal("refused"),
24273
+ reason: _enum([
24274
+ "unknown-collection",
24275
+ "still-unavailable",
24276
+ "still-busy",
24277
+ "unreadable-image",
24278
+ "write-failed"
24279
+ ]),
24280
+ detail: string().optional()
24281
+ })]);
24282
+ /** A vision LLM profile the AI assist may use, and where it sends the frame. */
24283
+ var RetrainAssistProfileSchema = object({
24284
+ id: string(),
24285
+ name: string(),
24286
+ model: string(),
24287
+ kind: string(),
24288
+ /** `true` when the frame would leave the house (a cloud vendor, or an
24289
+ * endpoint that is not on the local network). The page confirms each such
24290
+ * run, and the provider refuses one without `allowCloud`. */
24291
+ leavesHouse: boolean()
24292
+ });
24293
+ /** A box the AI assist proposed: a draft, plus the model's own reason. */
24294
+ var RetrainLlmProposalSchema = RetrainAnnotationDraftSchema.extend({ reason: string() });
24295
+ /** What the AI assist said about a box that was already on the canvas. */
24296
+ var RetrainLlmReviewSchema = object({
24297
+ /** The `ref` the page sent the box under. */
24298
+ ref: string(),
24299
+ verdict: _enum([
24300
+ "confirmed",
24301
+ "wrong-class",
24302
+ "not-an-object",
24303
+ "uncertain"
24304
+ ]),
24305
+ suggestedClass: RetrainMacroClassSchema.optional(),
24306
+ reason: string()
24307
+ });
24308
+ /**
24309
+ * The AI assist's answer. `proposed` carries drafts (`source: 'llm'`, the
24310
+ * model and its confidence) and reviews; items the model got wrong (a box off
24311
+ * the frame, an unasked class, an unknown ref) are dropped and counted in
24312
+ * `rejected`. A whole answer that is not the schema is `malformed-answer`.
24313
+ */
24314
+ var RetrainLlmAssistResultSchema = discriminatedUnion("kind", [object({
24315
+ kind: literal("proposed"),
24316
+ profileId: string(),
24317
+ model: string(),
24318
+ /** The frame was sent outside the house for this answer. */
24319
+ leftHouse: boolean(),
24320
+ proposals: array(RetrainLlmProposalSchema).readonly(),
24321
+ reviews: array(RetrainLlmReviewSchema).readonly(),
24322
+ rejected: number().int()
24323
+ }), object({
24324
+ kind: literal("refused"),
24325
+ reason: _enum([
24326
+ "unknown-frame",
24327
+ "unreadable-frame",
24328
+ "unknown-profile",
24329
+ "profile-disabled",
24330
+ "no-vision",
24331
+ "cloud-not-confirmed",
24332
+ "llm-error",
24333
+ "malformed-answer"
24334
+ ]),
24335
+ detail: string().optional()
24336
+ })]);
23248
24337
  /** The outcome of a lifecycle move owned by the retrain page. */
23249
24338
  var RetrainTransitionResultSchema = object({
23250
24339
  trackId: string(),
@@ -23962,7 +25051,7 @@ DeviceType.Camera, method(object({ deviceId: number() }), array(TrackSchema).rea
23962
25051
  auth: "admin"
23963
25052
  }), method(object({
23964
25053
  deviceId: number(),
23965
- trackId: string(),
25054
+ trackId: string().optional(),
23966
25055
  frameId: string()
23967
25056
  }), object({
23968
25057
  removed: boolean(),
@@ -23979,7 +25068,8 @@ DeviceType.Camera, method(object({ deviceId: number() }), array(TrackSchema).rea
23979
25068
  auth: "admin"
23980
25069
  }), method(object({
23981
25070
  deviceId: number(),
23982
- trackId: string(),
25071
+ /** The frame's track, when it has one — checked against the frame. */
25072
+ trackId: string().optional(),
23983
25073
  frameId: string(),
23984
25074
  subject: RetrainAssistSubjectSchema,
23985
25075
  /** Which node runs it. Absent ⇒ wherever an unowned call lands. */
@@ -23987,12 +25077,16 @@ DeviceType.Camera, method(object({ deviceId: number() }), array(TrackSchema).rea
23987
25077
  }), RetrainAssistResultSchema, {
23988
25078
  kind: "mutation",
23989
25079
  auth: "admin"
23990
- }), method(object({ trackId: string() }), array(RetrainAnnotationSchema).readonly(), {
25080
+ }), method(object({
25081
+ trackId: string().optional(),
25082
+ frameIds: array(string()).max(2e3).optional()
25083
+ }), array(RetrainAnnotationSchema).readonly(), {
23991
25084
  kind: "query",
23992
25085
  auth: "admin"
23993
25086
  }), method(object({
23994
25087
  deviceId: number(),
23995
- trackId: string(),
25088
+ /** The frame's track, when it has one — checked against the frame. */
25089
+ trackId: string().optional(),
23996
25090
  frameId: string(),
23997
25091
  annotations: array(RetrainAnnotationDraftSchema)
23998
25092
  }), array(RetrainAnnotationSchema).readonly(), {
@@ -24010,7 +25104,91 @@ DeviceType.Camera, method(object({ deviceId: number() }), array(TrackSchema).rea
24010
25104
  }), RetrainTransitionResultSchema, {
24011
25105
  kind: "mutation",
24012
25106
  auth: "admin"
24013
- }), method(object({ deviceIds: array(number()).optional() }), object({ url: string() }), {
25107
+ }), method(RetrainExportFilterSchema.extend({ layout: RetrainExportLayoutSchema.optional() }), object({ url: string() }), {
25108
+ kind: "query",
25109
+ auth: "admin"
25110
+ }), method(object({
25111
+ deviceId: number(),
25112
+ trackId: string().optional(),
25113
+ frameId: string(),
25114
+ complete: boolean()
25115
+ }), RetrainFrameSchema, {
25116
+ kind: "mutation",
25117
+ auth: "admin"
25118
+ }), method(object({
25119
+ filter: RetrainExportFilterSchema,
25120
+ layout: RetrainExportLayoutSchema,
25121
+ target: RetrainArchiveTargetSchema
25122
+ }), RetrainArchiveResultSchema, {
25123
+ kind: "mutation",
25124
+ auth: "admin",
25125
+ timeoutMs: 60 * 6e4
25126
+ }), method(object({}), array(RetrainAssistProfileSchema).readonly(), {
25127
+ kind: "query",
25128
+ auth: "admin"
25129
+ }), method(object({
25130
+ deviceId: number(),
25131
+ trackId: string().optional(),
25132
+ frameId: string(),
25133
+ profileId: string(),
25134
+ classes: array(RetrainMacroClassSchema).min(1),
25135
+ boxes: array(object({
25136
+ ref: string(),
25137
+ bbox: RetrainBboxSchema,
25138
+ macroClass: RetrainMacroClassSchema,
25139
+ label: string().optional()
25140
+ })).max(50),
25141
+ allowCloud: boolean()
25142
+ }), RetrainLlmAssistResultSchema, {
25143
+ kind: "mutation",
25144
+ auth: "admin",
25145
+ timeoutMs: 18e4
25146
+ }), method(object({ name: string().max(120) }), RetrainCollectionCreateResultSchema, {
25147
+ kind: "mutation",
25148
+ auth: "admin"
25149
+ }), method(object({}), array(RetrainCollectionSchema).readonly(), {
25150
+ kind: "query",
25151
+ auth: "admin"
25152
+ }), method(object({
25153
+ id: string(),
25154
+ name: string().max(120)
25155
+ }), RetrainCollectionRenameResultSchema, {
25156
+ kind: "mutation",
25157
+ auth: "admin"
25158
+ }), method(object({ id: string() }), object({
25159
+ deleted: boolean(),
25160
+ removedFrames: number().int()
25161
+ }), {
25162
+ kind: "mutation",
25163
+ auth: "admin"
25164
+ }), method(object({
25165
+ collectionId: string(),
25166
+ deviceId: number(),
25167
+ epochMs: number()
25168
+ }), AddRetrainFrameFromRecordingResultSchema, {
25169
+ kind: "mutation",
25170
+ auth: "admin",
25171
+ timeoutMs: 6e4
25172
+ }), method(object({
25173
+ collectionId: string(),
25174
+ frameIds: array(string()).min(1).max(500)
25175
+ }), object({
25176
+ added: number().int(),
25177
+ alreadyMember: number().int(),
25178
+ unknownFrameIds: array(string()).readonly()
25179
+ }), {
25180
+ kind: "mutation",
25181
+ auth: "admin"
25182
+ }), method(object({
25183
+ collectionId: string(),
25184
+ frameId: string()
25185
+ }), object({
25186
+ removed: boolean(),
25187
+ deletedFrame: boolean()
25188
+ }), {
25189
+ kind: "mutation",
25190
+ auth: "admin"
25191
+ }), method(object({ collectionId: string() }), array(RetrainFrameSchema).readonly(), {
24014
25192
  kind: "query",
24015
25193
  auth: "admin"
24016
25194
  }), method(object({
@@ -24838,13 +26016,31 @@ var RecommendationReasonSchema = discriminatedUnion("kind", [
24838
26016
  object({
24839
26017
  kind: literal("no-family-build"),
24840
26018
  format: ModelFormatSchema$1
26019
+ }),
26020
+ object({
26021
+ kind: literal("kept-custom-pin"),
26022
+ modelId: string(),
26023
+ deviceKey: string()
24841
26024
  })
24842
26025
  ]);
26026
+ /** Why a device is left exactly as it is, whatever the plan would pick (D786). */
26027
+ var KeptReasonSchema = _enum(["custom-pin"]);
26028
+ /**
26029
+ * Where a choice's latency came from (D791): this node's own benchmark
26030
+ * result, or the hardware-class table. Measured on the node beats the table.
26031
+ */
26032
+ var LatencySourceSchema = discriminatedUnion("kind", [object({ kind: literal("table") }), object({
26033
+ kind: literal("node-benchmark"),
26034
+ resultId: string(),
26035
+ measuredAtMs: number()
26036
+ })]);
24843
26037
  var DeviceModelChoiceSchema = object({
24844
26038
  modelId: string(),
24845
26039
  p50Ms: number(),
24846
26040
  fps: number(),
24847
26041
  measured: boolean(),
26042
+ /** Absent on a plan from before D791 — read as the table. */
26043
+ source: LatencySourceSchema.optional(),
24848
26044
  /** Parallel sessions at the camera rate with the headroom kept free: ⌊fps × (1 − headroom) / camera fps⌋. */
24849
26045
  capacitySessions: number(),
24850
26046
  personRecall: number()
@@ -24855,6 +26051,8 @@ var DeviceRecommendationSchema = object({
24855
26051
  enabled: boolean(),
24856
26052
  available: boolean(),
24857
26053
  chosen: DeviceModelChoiceSchema.nullable(),
26054
+ /** Present when the device is kept as is: Apply writes nothing on it. */
26055
+ kept: KeptReasonSchema.optional(),
24858
26056
  reasons: array(RecommendationReasonSchema).readonly()
24859
26057
  });
24860
26058
  var NodeRecommendationSchema = object({
@@ -24908,6 +26106,17 @@ var AutoTuneWarningSchema = discriminatedUnion("kind", [
24908
26106
  kind: literal("coral-enabled"),
24909
26107
  nodeId: string(),
24910
26108
  deviceKey: string()
26109
+ }),
26110
+ object({
26111
+ kind: literal("custom-pin-kept"),
26112
+ nodeId: string(),
26113
+ deviceKey: string().nullable(),
26114
+ deviceId: number().nullable(),
26115
+ modelId: string()
26116
+ }),
26117
+ object({
26118
+ kind: literal("custom-registry-unreadable"),
26119
+ error: string()
24911
26120
  })
24912
26121
  ]);
24913
26122
  var AutoTuneOptionsSchema = object({
@@ -25098,13 +26307,14 @@ var SuggestSharesResultSchema = object({
25098
26307
  nodes: array(SuggestedNodeShareSchema).readonly()
25099
26308
  });
25100
26309
  /**
25101
- * Where the node and device shares come from (D750, D755).
25102
- * - `hardware` (default) — computed by the system from each node's static
25103
- * capacity (its accelerators at their Auto tune models, from the measured
25104
- * latency table), recomputed whenever a node or device joins, leaves, is
25105
- * switched on or off, or changes model. No load input.
25106
- * - `benchmark` — the same, from each node's measured capacity (node
25107
- * benchmark results); a node without results falls back to `hardware`.
26310
+ * Where the node and device shares come from (D750, D755, D791).
26311
+ * - `benchmark` (default since D791) — each node's capacity as its own node
26312
+ * benchmark measured it; a node without fresh results is sized from
26313
+ * `hardware` and says so. Measured on the node beats the table.
26314
+ * - `hardware` — computed from each node's static capacity only (its
26315
+ * accelerators at their Auto tune models, from the latency table).
26316
+ * Both are recomputed whenever a node or device joins, leaves, is switched
26317
+ * on or off, changes model, or a benchmark result is written. No load input.
25108
26318
  * - `manual` — the operator's stored sliders.
25109
26319
  * Either way a node or device that cannot take sessions gives its share to
25110
26320
  * the others in proportion; the stored values are never rewritten for that.
@@ -25114,7 +26324,7 @@ var ShareSourceSchema = _enum([
25114
26324
  "benchmark",
25115
26325
  "manual"
25116
26326
  ]);
25117
- var DEFAULT_SHARE_SOURCE = "hardware";
26327
+ var DEFAULT_SHARE_SOURCE = "benchmark";
25118
26328
  /** `hardware` or `benchmark`: the system computes the shares. */
25119
26329
  function isAutomaticShareSource(source) {
25120
26330
  return source !== "manual";
@@ -25128,6 +26338,7 @@ var SHARE_SOURCE_SETTING_KEY = "shareSource";
25128
26338
  * D755 split it) reads as `hardware` — that is what it computed.
25129
26339
  */
25130
26340
  function parseShareSource(value) {
26341
+ if (value === "auto") return "hardware";
25131
26342
  const parsed = ShareSourceSchema.safeParse(value);
25132
26343
  return parsed.success ? parsed.data : DEFAULT_SHARE_SOURCE;
25133
26344
  }
@@ -25170,6 +26381,363 @@ var EffectiveSharesSchema = object({
25170
26381
  nodeWeights: record(string(), number()),
25171
26382
  deviceWeights: record(string(), record(string(), number()))
25172
26383
  });
26384
+ /**
26385
+ * Wire shapes of the node benchmark (D791): the operator's plan, the
26386
+ * preflight the confirmation shows, the run the orchestrator owns, what the
26387
+ * measuring runner reports, and the result rows Auto tune and the automatic
26388
+ * shares read.
26389
+ *
26390
+ * ONE node at a time, owned by the pipeline orchestrator. While a run is
26391
+ * active the cluster is in BENCHMARK MODE for that node: it is not a
26392
+ * detection candidate, camera pins naming it are ignored so its sessions move
26393
+ * elsewhere, and both come back when the mode ends — after a crash or a hub
26394
+ * restart too (a persisted manifest, reconciled against the registry).
26395
+ *
26396
+ * Two measurements, both on the node, driven by its own runner (frames never
26397
+ * cross a process at frame rate, D9):
26398
+ * - `device` — one accelerator × one candidate detector, root only, open
26399
+ * loop: how many streams at the camera rate it carries inside the frame
26400
+ * interval;
26401
+ * - `pipeline` — the chosen detector plus the operator's detail steps
26402
+ * (faces, plates, classifiers, CLIP…), on varied frames from the bench
26403
+ * clip: how many parallel sessions the node carries before latency, drops,
26404
+ * CPU, RAM or the event loop give out.
26405
+ */
26406
+ /** Defaults and bounds, one place. */
26407
+ var NODE_BENCHMARK_DEFAULTS = {
26408
+ fpsPerStream: 10,
26409
+ budgetMinutes: 45,
26410
+ minBudgetMinutes: 10,
26411
+ maxBudgetMinutes: 120,
26412
+ /** A result older than this no longer beats the table. */
26413
+ staleAfterDays: 30,
26414
+ /** Warm-in per ramp step (not accounted). */
26415
+ settleSec: 5,
26416
+ /** Accounted window per ramp step. */
26417
+ measureSec: 20,
26418
+ /** Longest the drain may take before the run is refused. */
26419
+ drainTimeoutSec: 120,
26420
+ /** Candidate models per device in one run. */
26421
+ maxModelsPerDevice: 6
26422
+ };
26423
+ /** The two measurements a run can make. */
26424
+ var NodeBenchmarkPhaseSchema = _enum(["device", "pipeline"]);
26425
+ var NodeBenchmarkDevicePlanSchema = object({
26426
+ /** `<backend>:<device>` — `openvino:npu`, `coreml:all`, `cpu`. */
26427
+ deviceKey: string().min(1),
26428
+ /**
26429
+ * Root detector candidates for the `device` measurement, in the order
26430
+ * measured. The FIRST is the one the `pipeline` measurement runs.
26431
+ */
26432
+ modelIds: array(string().min(1)).min(1).max(NODE_BENCHMARK_DEFAULTS.maxModelsPerDevice)
26433
+ });
26434
+ /** What the operator confirmed. */
26435
+ var NodeBenchmarkPlanSchema = object({
26436
+ nodeId: string().min(1),
26437
+ devices: array(NodeBenchmarkDevicePlanSchema).min(1),
26438
+ /** Which measurements run. At least one. */
26439
+ phases: array(NodeBenchmarkPhaseSchema).min(1).default(["device", "pipeline"]),
26440
+ /**
26441
+ * Detail steps (catalog step ids under the root: faces, plates,
26442
+ * classifiers, CLIP…) the `pipeline` measurement enables. The root
26443
+ * detector always runs.
26444
+ */
26445
+ steps: array(string().min(1)).default([]),
26446
+ /** Offered rate of one stream / session. */
26447
+ fpsPerStream: number().int().min(1).max(30).default(NODE_BENCHMARK_DEFAULTS.fpsPerStream),
26448
+ /** Hard deadline: the mode ends and the cluster is restored when it passes. */
26449
+ budgetMinutes: number().int().min(NODE_BENCHMARK_DEFAULTS.minBudgetMinutes).max(NODE_BENCHMARK_DEFAULTS.maxBudgetMinutes).default(NODE_BENCHMARK_DEFAULTS.budgetMinutes),
26450
+ /** Keep the models the run downloaded (default: delete what was not installed before). */
26451
+ keepDownloadedModels: boolean().default(false),
26452
+ /** Start although the other nodes' capacity does not cover the sessions running now. */
26453
+ acceptInsufficientHeadroom: boolean().default(false)
26454
+ });
26455
+ var NodeBenchmarkRefusalSchema = object({
26456
+ kind: _enum([
26457
+ "benchmark-active",
26458
+ "node-offline",
26459
+ "unknown-device",
26460
+ "no-phase",
26461
+ "insufficient-headroom"
26462
+ ]),
26463
+ detail: string(),
26464
+ overridable: boolean()
26465
+ });
26466
+ /** A camera whose detection leaves the node for the run. */
26467
+ var NodeBenchmarkMovingCameraSchema = object({
26468
+ deviceId: number(),
26469
+ /** Placed on the node now (a standing attach or a running session). */
26470
+ placedOnNode: boolean(),
26471
+ /** A node pin naming this node, ignored for the run. */
26472
+ pinnedToNode: boolean()
26473
+ });
26474
+ /** A camera that cannot leave: no other node can obtain its frames. */
26475
+ var NodeBenchmarkStayingCameraSchema = object({
26476
+ deviceId: number(),
26477
+ reason: literal("only-frame-source")
26478
+ });
26479
+ var NodeBenchmarkPreflightSchema = object({
26480
+ nodeId: string(),
26481
+ online: boolean(),
26482
+ moving: array(NodeBenchmarkMovingCameraSchema).readonly(),
26483
+ /** Keep running on the node during the run; the result is flagged contaminated. */
26484
+ staying: array(NodeBenchmarkStayingCameraSchema).readonly(),
26485
+ estimatedDurationMs: number(),
26486
+ /** The run ends and the cluster is restored at the latest after this long. */
26487
+ budgetMs: number(),
26488
+ phases: array(NodeBenchmarkPhaseSchema).readonly(),
26489
+ steps: array(string()).readonly(),
26490
+ devices: array(NodeBenchmarkDevicePlanSchema).readonly(),
26491
+ /** Parallel sessions the cluster runs now, and what the other nodes can carry. */
26492
+ headroom: object({
26493
+ sessionsNow: number(),
26494
+ otherNodesCapacitySessions: number().nullable()
26495
+ }),
26496
+ refusals: array(NodeBenchmarkRefusalSchema).readonly(),
26497
+ canStart: boolean()
26498
+ });
26499
+ /** Where the measuring runner is. */
26500
+ var NodeBenchProgressSchema = object({
26501
+ phase: _enum([
26502
+ "preparing",
26503
+ "device",
26504
+ "pipeline",
26505
+ "cleanup"
26506
+ ]),
26507
+ deviceKey: string().nullable(),
26508
+ modelId: string().nullable(),
26509
+ /** Streams (device) or sessions (pipeline) of the step running now. */
26510
+ load: number().nullable(),
26511
+ cellsDone: number(),
26512
+ cellsTotal: number(),
26513
+ message: string()
26514
+ });
26515
+ /** What limited a ramp. `none` = the ramp ran out of steps still passing. */
26516
+ var NodeBenchLimitSchema = _enum([
26517
+ "p95",
26518
+ "drops",
26519
+ "errors",
26520
+ "stall",
26521
+ "cpu",
26522
+ "ram",
26523
+ "none"
26524
+ ]);
26525
+ var NodeBenchRampStepSchema = object({
26526
+ load: number(),
26527
+ pass: boolean(),
26528
+ p95Ms: number(),
26529
+ dropRatio: number(),
26530
+ /** Completed frames per second over the accounted window. */
26531
+ fps: number(),
26532
+ cpuPct: number().nullable(),
26533
+ ramFreePct: number().nullable(),
26534
+ failReasons: array(string()).readonly()
26535
+ });
26536
+ var NodeBenchCellSchema = discriminatedUnion("phase", [object({
26537
+ phase: literal("device"),
26538
+ deviceKey: string(),
26539
+ modelId: string(),
26540
+ /** Single-stream latency (the K = 1 step). */
26541
+ p50Ms: number(),
26542
+ p95Ms: number(),
26543
+ p99Ms: number(),
26544
+ /** Frames per second sustained at the last passing step. */
26545
+ fps: number(),
26546
+ /** Streams at the camera rate the device carried inside the frame interval; 0 = not even one. */
26547
+ kMax: number(),
26548
+ limitedBy: NodeBenchLimitSchema,
26549
+ /** First call (model load + warm-up). */
26550
+ warmupMs: number(),
26551
+ ramp: array(NodeBenchRampStepSchema).readonly()
26552
+ }), object({
26553
+ phase: literal("pipeline"),
26554
+ deviceKey: string(),
26555
+ modelId: string(),
26556
+ steps: array(string()).readonly(),
26557
+ /** Parallel sessions at the camera rate the node carried; 0 = not even one. */
26558
+ maxSessions: number(),
26559
+ limitedBy: NodeBenchLimitSchema,
26560
+ /** Detail steps that produced at least one result on the clip — a step that never fired measured nothing. */
26561
+ stepsFired: array(string()).readonly(),
26562
+ warmupMs: number(),
26563
+ ramp: array(NodeBenchRampStepSchema).readonly()
26564
+ })]);
26565
+ /**
26566
+ * What a result was measured ON. A result whose fingerprint no longer matches
26567
+ * the node no longer beats the table.
26568
+ */
26569
+ var NodeBenchFingerprintSchema = object({
26570
+ pipelineVersion: string().nullable(),
26571
+ cpuModel: string().nullable(),
26572
+ ramGb: number().nullable(),
26573
+ os: string().nullable(),
26574
+ /** Per device: the pool the cells ran on (runtime + tuning). */
26575
+ pools: record(string(), object({
26576
+ runtime: string(),
26577
+ numWorkers: number(),
26578
+ concurrency: number(),
26579
+ batchMode: string()
26580
+ })).default({})
26581
+ });
26582
+ /** The frames the run used. */
26583
+ var NodeBenchClipSchema = object({
26584
+ /** `camstack-bench-v1` or `reference-images` (the fallback when the clip is not reachable). */
26585
+ id: string(),
26586
+ revision: string().nullable(),
26587
+ frames: number(),
26588
+ width: number(),
26589
+ height: number()
26590
+ });
26591
+ var NodeBenchRunnerInputSchema = object({
26592
+ runId: string().min(1),
26593
+ devices: array(NodeBenchmarkDevicePlanSchema).min(1),
26594
+ phases: array(NodeBenchmarkPhaseSchema).min(1),
26595
+ /** The full detail step tree root for the `pipeline` measurement is built on the node from these ids. */
26596
+ steps: array(string()).default([]),
26597
+ fpsPerStream: number().int().min(1).max(30),
26598
+ /** The runner stops on its own past this instant, whatever the hub does. */
26599
+ deadlineAtMs: number(),
26600
+ settleSec: number().min(0).max(60).default(NODE_BENCHMARK_DEFAULTS.settleSec),
26601
+ measureSec: number().min(2).max(300).default(NODE_BENCHMARK_DEFAULTS.measureSec),
26602
+ keepDownloadedModels: boolean().default(false)
26603
+ });
26604
+ var NodeBenchRunnerStateSchema = _enum([
26605
+ "running",
26606
+ "done",
26607
+ "failed",
26608
+ "aborted"
26609
+ ]);
26610
+ var NodeBenchRunnerStatusSchema = object({
26611
+ runId: string(),
26612
+ nodeId: string(),
26613
+ state: NodeBenchRunnerStateSchema,
26614
+ progress: NodeBenchProgressSchema.nullable(),
26615
+ /** Every finished cell so far — a run stopped half way keeps what it measured. */
26616
+ cells: array(NodeBenchCellSchema).readonly(),
26617
+ fingerprint: NodeBenchFingerprintSchema.nullable(),
26618
+ clip: NodeBenchClipSchema.nullable(),
26619
+ /** Cameras seen running on the node during the run (they share its pools). */
26620
+ activeCameraIds: array(number()).readonly(),
26621
+ /** Models the run downloaded and then deleted (not installed before). */
26622
+ deletedModels: array(string()).readonly(),
26623
+ error: string().nullable(),
26624
+ startedAtMs: number(),
26625
+ finishedAtMs: number().nullable()
26626
+ });
26627
+ var NodeBenchmarkStateSchema = _enum([
26628
+ "draining",
26629
+ "measuring",
26630
+ "restoring",
26631
+ "done",
26632
+ "failed",
26633
+ "aborted"
26634
+ ]);
26635
+ /** Why a run ended. */
26636
+ var NodeBenchmarkEndReasonSchema = _enum([
26637
+ "completed",
26638
+ "operator-abort",
26639
+ "deadline",
26640
+ "node-lost",
26641
+ "runner-lost",
26642
+ "hub-restart",
26643
+ "drain-timeout",
26644
+ "failed"
26645
+ ]);
26646
+ var NodeBenchmarkRunSchema = object({
26647
+ runId: string(),
26648
+ nodeId: string(),
26649
+ state: NodeBenchmarkStateSchema,
26650
+ plan: NodeBenchmarkPlanSchema,
26651
+ startedAtMs: number(),
26652
+ deadlineAtMs: number(),
26653
+ finishedAtMs: number().nullable(),
26654
+ endReason: NodeBenchmarkEndReasonSchema.nullable(),
26655
+ error: string().nullable(),
26656
+ /** Cameras moved off the node for the run. */
26657
+ moved: array(number()).readonly(),
26658
+ /** Cameras that stayed (their only frame source is the node). */
26659
+ staying: array(number()).readonly(),
26660
+ /** Something else ran on the node's pools during the run. */
26661
+ contaminated: boolean(),
26662
+ progress: NodeBenchProgressSchema.nullable(),
26663
+ resultIds: array(string()).readonly()
26664
+ });
26665
+ /** `getNodeBenchmarkStatus`: the active run (or the last one), and whether benchmark mode is on. */
26666
+ var NodeBenchmarkStatusSchema = object({
26667
+ /** The node the cluster is in benchmark mode for; null = none. */
26668
+ modeNodeId: string().nullable(),
26669
+ run: NodeBenchmarkRunSchema.nullable()
26670
+ });
26671
+ /**
26672
+ * One stored measurement — the only authority Auto tune and the automatic
26673
+ * shares read measured capacity from. Written by the orchestrator when a run
26674
+ * ends, never by anyone else.
26675
+ */
26676
+ var NodeBenchmarkResultSchema = object({
26677
+ id: string(),
26678
+ runId: string(),
26679
+ nodeId: string(),
26680
+ cell: NodeBenchCellSchema,
26681
+ fpsPerStream: number(),
26682
+ fingerprint: NodeBenchFingerprintSchema.nullable(),
26683
+ clip: NodeBenchClipSchema.nullable(),
26684
+ /** Other work shared the node's pools (a camera that could not move, or the hub's own duties). */
26685
+ contaminated: boolean(),
26686
+ stayingCameras: array(number()).readonly(),
26687
+ /** The run ended early; the cell itself completed. */
26688
+ partialRun: boolean(),
26689
+ measuredAtMs: number()
26690
+ });
26691
+ var ListNodeBenchmarkResultsInputSchema = object({
26692
+ nodeId: string().optional(),
26693
+ deviceKey: string().optional()
26694
+ });
26695
+ /** A result with its standing today: does it still beat the table? */
26696
+ var NodeBenchmarkResultViewSchema = NodeBenchmarkResultSchema.extend({
26697
+ /** `fresh` beats the table; `stale` falls back to it; `superseded` = a newer row covers the same cell. */
26698
+ standing: _enum([
26699
+ "fresh",
26700
+ "stale",
26701
+ "superseded"
26702
+ ]),
26703
+ staleWhy: _enum([
26704
+ "age",
26705
+ "runtime",
26706
+ "pool-tuning"
26707
+ ]).nullable()
26708
+ });
26709
+ /** The manifest persisted BEFORE benchmark mode changes anything; its presence means "restore". */
26710
+ var NodeBenchmarkManifestSchema = object({
26711
+ runId: string(),
26712
+ nodeId: string(),
26713
+ startedAtMs: number(),
26714
+ deadlineAtMs: number(),
26715
+ plan: NodeBenchmarkPlanSchema,
26716
+ moved: array(number()).readonly(),
26717
+ staying: array(number()).readonly()
26718
+ });
26719
+ /** Candidate detectors per device, for the plan editor. */
26720
+ var NodeBenchmarkCandidatesSchema = object({
26721
+ nodeId: string(),
26722
+ reachable: boolean(),
26723
+ devices: array(object({
26724
+ deviceKey: string(),
26725
+ enabled: boolean(),
26726
+ available: boolean(),
26727
+ /** Ordered: what Auto tune would pick first, then the rest of the family by accuracy. */
26728
+ suggested: array(string()).readonly(),
26729
+ /** Every root detector the device has a build for. */
26730
+ all: array(string()).readonly(),
26731
+ /** The model the device runs now, when pinned. */
26732
+ currentModelId: string().nullable()
26733
+ })).readonly(),
26734
+ /** Detail steps the node's catalog offers under the root (for the `pipeline` measurement). */
26735
+ steps: array(object({
26736
+ id: string(),
26737
+ name: string(),
26738
+ slot: string()
26739
+ })).readonly()
26740
+ });
25173
26741
  object({
25174
26742
  activeCameras: number(),
25175
26743
  throttledCameras: number(),
@@ -26486,7 +28054,16 @@ method(RunnerCameraConfigSchema, object({ success: literal(true) }), { kind: "mu
26486
28054
  }), ParkedTrackFrameSchema.nullable()), method(object({
26487
28055
  deviceId: number(),
26488
28056
  trackId: string()
26489
- }), object({ released: number().int().nonnegative() }), { kind: "mutation" });
28057
+ }), object({ released: number().int().nonnegative() }), { kind: "mutation" }), method(NodeBenchRunnerInputSchema, object({
28058
+ accepted: literal(true),
28059
+ runId: string()
28060
+ }), {
28061
+ kind: "mutation",
28062
+ auth: "admin"
28063
+ }), method(object({ runId: string() }), NodeBenchRunnerStatusSchema.nullable(), { auth: "admin" }), method(object({ runId: string() }), object({ aborted: boolean() }), {
28064
+ kind: "mutation",
28065
+ auth: "admin"
28066
+ });
26490
28067
  var CameraPipelineConfigSchema = object({
26491
28068
  engine: PipelineEngineChoiceSchema.optional(),
26492
28069
  steps: array(PipelineStepInputSchema).readonly(),
@@ -27412,6 +28989,51 @@ var pipelineOrchestratorCapability = {
27412
28989
  kind: "mutation",
27413
28990
  auth: "admin"
27414
28991
  }),
28992
+ /**
28993
+ * Node benchmark — what a run of this plan would do, without doing it:
28994
+ * the cameras that leave the node, those that cannot (the node is their
28995
+ * only frame source; they stay and flag the run), the estimated duration
28996
+ * and every reason it may not start. The confirmation shows this.
28997
+ */
28998
+ getNodeBenchmarkPreflight: method(NodeBenchmarkPlanSchema, NodeBenchmarkPreflightSchema, { auth: "admin" }),
28999
+ /**
29000
+ * Node benchmark — start ONE run (cluster-wide lock). The orchestrator
29001
+ * persists a restore manifest, enters benchmark mode for the node (out of
29002
+ * placement, its camera pins ignored, nothing rewritten), drains it, drives
29003
+ * the node's own runner, then restores — always, including after an
29004
+ * abort, a lost node or a hub restart. Answers at once; poll
29005
+ * `getNodeBenchmarkStatus`. Refused runs carry the preflight.
29006
+ */
29007
+ startNodeBenchmark: method(NodeBenchmarkPlanSchema, discriminatedUnion("kind", [object({
29008
+ kind: literal("started"),
29009
+ runId: string()
29010
+ }), object({
29011
+ kind: literal("refused"),
29012
+ preflight: NodeBenchmarkPreflightSchema
29013
+ })]), {
29014
+ kind: "mutation",
29015
+ auth: "admin"
29016
+ }),
29017
+ /** Node benchmark — the active run (or the last one) and the node in benchmark mode. */
29018
+ getNodeBenchmarkStatus: method(_void(), NodeBenchmarkStatusSchema, { auth: "admin" }),
29019
+ /** Node benchmark — stop the active run; the cluster is restored as it was. */
29020
+ abortNodeBenchmark: method(object({ runId: string() }), object({ aborted: boolean() }), {
29021
+ kind: "mutation",
29022
+ auth: "admin"
29023
+ }),
29024
+ /**
29025
+ * Node benchmark — root detector candidates per device (what Auto tune
29026
+ * would pick first, then the family) and the detail steps the node's
29027
+ * catalog offers, for the plan editor.
29028
+ */
29029
+ getNodeBenchmarkCandidates: method(object({ nodeId: string() }), NodeBenchmarkCandidatesSchema, { auth: "admin" }),
29030
+ /** Node benchmark — stored results, newest first, each with its standing today. */
29031
+ listNodeBenchmarkResults: method(ListNodeBenchmarkResultsInputSchema, array(NodeBenchmarkResultViewSchema).readonly(), { auth: "admin" }),
29032
+ /** Node benchmark — delete one result; Auto tune and the shares stop reading it. */
29033
+ deleteNodeBenchmarkResult: method(object({ id: string() }), object({ deleted: boolean() }), {
29034
+ kind: "mutation",
29035
+ auth: "admin"
29036
+ }),
27415
29037
  /** Read one camera's settings. Null when never touched (inherits agent defaults fully). */
27416
29038
  getCameraSettings: method(object({ deviceId: number() }), CameraPipelineSettingsSchema.nullable()),
27417
29039
  /** Set or clear the 3-state toggle for one (camera, addonId). Pass `enabled: null` to clear and revert to agent default. */
@@ -28164,7 +29786,19 @@ var SsoBridgeClaimsSchema = object({
28164
29786
  * editing a descriptor must not retroactively extend or shorten a link the
28165
29787
  * operator already consented to.
28166
29788
  */
28167
- refreshTtl: union([number().int().positive(), literal("never")]).optional()
29789
+ refreshTtl: union([number().int().positive(), literal("never")]).optional(),
29790
+ /**
29791
+ * Opaque handle of a hub-side LINK intent (D782). Set by an OIDC callback
29792
+ * that was started from a signed-in user's "Link" button: the finish then
29793
+ * links this identity to the intent's user instead of signing anyone in.
29794
+ * The handle carries no user id — the hub resolves it, single-use.
29795
+ */
29796
+ linkIntent: string().optional(),
29797
+ /**
29798
+ * Verified `iss` of the id_token behind an OIDC login bridge (D782). A
29799
+ * subject is unique only per issuer, so a link is bound to it.
29800
+ */
29801
+ issuer: string().optional()
28168
29802
  });
28169
29803
  method(object({
28170
29804
  claims: SsoBridgeClaimsSchema,
@@ -30070,7 +31704,22 @@ DeviceType.Camera, method(object({ deviceId: number().int().nonnegative() }), ar
30070
31704
  * field their hints were silently stripped by the schema and the
30071
31705
  * adaptive ladder started blind on the client's display size.
30072
31706
  */
30073
- hints: webrtcClientHintsSchema.optional()
31707
+ hints: webrtcClientHintsSchema.optional(),
31708
+ /**
31709
+ * Talk-back (D771): relay the PEER's audio to the device's speaker
31710
+ * through the `intercom` cap's push-talk triple. When `true` and the
31711
+ * offer lets the peer send audio, the answer's audio is `sendrecv`
31712
+ * and every inbound frame goes to `intercom.pushTalkAudio` for this
31713
+ * `deviceId`; absent/false answers `sendonly`, as for every viewer.
31714
+ *
31715
+ * TRUSTED-CALLER ONLY. Set by the Alexa export for an Echo on a camera
31716
+ * with `intercom`, when the linked user may control that camera. The
31717
+ * hub STRIPS it from every request-bound call (a browser), exactly as
31718
+ * it overwrites `relayOnly` — a viewer's talk permission is the
31719
+ * intercom cap's own surface (share tokens gate it there), and this
31720
+ * flag must not become a second door to the camera's speaker.
31721
+ */
31722
+ talkback: boolean().optional()
30074
31723
  }), object({
30075
31724
  sessionId: string(),
30076
31725
  sdpAnswer: string()
@@ -32582,6 +34231,9 @@ var CreateIntegrationInputSchema = object({
32582
34231
  enabled: boolean().optional(),
32583
34232
  info: record(string(), unknown()).optional(),
32584
34233
  settings: record(string(), unknown()).optional(),
34234
+ /** Keys of `settings` the form's schema declares secret (by type) — kept in
34235
+ * the keychain, not the table (D787). */
34236
+ secretKeys: array(string()).optional(),
32585
34237
  skipRestart: boolean().optional()
32586
34238
  });
32587
34239
  var UpdateIntegrationInputSchema = object({
@@ -32605,7 +34257,9 @@ method(_void(), array(IntegrationWithStateSchema)), method(object({ id: string()
32605
34257
  auth: "admin"
32606
34258
  }), method(object({ id: string() }), record(string(), unknown()), { auth: "admin" }), method(object({
32607
34259
  id: string(),
32608
- settings: record(string(), unknown())
34260
+ settings: record(string(), unknown()),
34261
+ /** Keys the form's schema declares secret (by type) — keychain-stored (D787). */
34262
+ secretKeys: array(string()).optional()
32609
34263
  }), object({ success: boolean() }), {
32610
34264
  kind: "mutation",
32611
34265
  auth: "admin"
@@ -38169,6 +39823,32 @@ var TotpStatusSchema = object({
38169
39823
  /** Null when no row exists OR the row is still pending confirmation. */
38170
39824
  confirmedAt: number().nullable()
38171
39825
  });
39826
+ /**
39827
+ * user-management — singleton capability for user accounts, API keys,
39828
+ * and scoped tokens.
39829
+ *
39830
+ * v2 model: every method gated by `auth: 'admin'`. Admin-only delegation
39831
+ * — the subset check on `setUserScopes` / `createScopedToken` is moot
39832
+ * because non-admins never reach these. If/when we open non-admin
39833
+ * delegation, structural caller-scope subset check goes inside the
39834
+ * provider.
39835
+ */
39836
+ var ExternalIdentitySchema = object({
39837
+ /** Login-method id of the provider instance, e.g. `auth-oidc/authentik`. */
39838
+ provider: string(),
39839
+ /** The IdP's stable subject (`sub`) for this account. */
39840
+ subject: string(),
39841
+ /** The verified `iss` of the IdP the identity was linked under — `sub` is unique only per issuer. */
39842
+ issuer: string(),
39843
+ userId: string(),
39844
+ /** What the IdP called the account at link time (username / email) — display only. */
39845
+ label: string().optional(),
39846
+ linkedAt: number()
39847
+ });
39848
+ var ExternalIdentityKeySchema = object({
39849
+ provider: string().min(1),
39850
+ subject: string().min(1)
39851
+ });
38172
39852
  method(_void(), array(UserSummarySchema), { auth: "admin" }), method(CreateUserInputSchema, UserSummarySchema, {
38173
39853
  kind: "mutation",
38174
39854
  auth: "admin",
@@ -38282,7 +39962,22 @@ method(_void(), array(UserSummarySchema), { auth: "admin" }), method(CreateUserI
38282
39962
  userId: string(),
38283
39963
  username: string(),
38284
39964
  scopes: array(TokenScopeSchema)
38285
- }).nullable(), { access: "view" }), method(_void(), array(OauthSessionSummarySchema), { auth: "admin" }), method(object({ id: string() }), object({ success: boolean() }), {
39965
+ }).nullable(), { access: "view" }), method(object({ userId: string() }), array(ExternalIdentitySchema), { auth: "admin" }), method(ExternalIdentityKeySchema, object({
39966
+ userId: string(),
39967
+ issuer: string()
39968
+ }).nullable(), { auth: "admin" }), method(ExternalIdentityKeySchema.extend({
39969
+ userId: string(),
39970
+ issuer: string().min(1),
39971
+ label: string().optional()
39972
+ }), object({ success: literal(true) }), {
39973
+ kind: "mutation",
39974
+ auth: "admin",
39975
+ access: "create"
39976
+ }), method(ExternalIdentityKeySchema.extend({ userId: string() }), object({ success: boolean() }), {
39977
+ kind: "mutation",
39978
+ auth: "admin",
39979
+ access: "delete"
39980
+ }), method(_void(), array(OauthSessionSummarySchema), { auth: "admin" }), method(object({ id: string() }), object({ success: boolean() }), {
38286
39981
  kind: "mutation",
38287
39982
  auth: "admin",
38288
39983
  access: "delete"
@@ -41380,6 +43075,90 @@ Object.freeze({
41380
43075
  addonId: null,
41381
43076
  access: "create"
41382
43077
  },
43078
+ "keychain.delete": {
43079
+ capName: "keychain",
43080
+ capScope: "system",
43081
+ addonId: null,
43082
+ access: "delete"
43083
+ },
43084
+ "keychain.getState": {
43085
+ capName: "keychain",
43086
+ capScope: "system",
43087
+ addonId: null,
43088
+ access: "view"
43089
+ },
43090
+ "keychain.listMeta": {
43091
+ capName: "keychain",
43092
+ capScope: "system",
43093
+ addonId: null,
43094
+ access: "view"
43095
+ },
43096
+ "keychain.put": {
43097
+ capName: "keychain",
43098
+ capScope: "system",
43099
+ addonId: null,
43100
+ access: "create"
43101
+ },
43102
+ "keychain.resolve": {
43103
+ capName: "keychain",
43104
+ capScope: "system",
43105
+ addonId: null,
43106
+ access: "view"
43107
+ },
43108
+ "keychainAdmin.deleteOrphans": {
43109
+ capName: "keychain-admin",
43110
+ capScope: "system",
43111
+ addonId: null,
43112
+ access: "delete"
43113
+ },
43114
+ "keychainAdmin.downloadRecoveryKey": {
43115
+ capName: "keychain-admin",
43116
+ capScope: "system",
43117
+ addonId: null,
43118
+ access: "create"
43119
+ },
43120
+ "keychainAdmin.listAudit": {
43121
+ capName: "keychain-admin",
43122
+ capScope: "system",
43123
+ addonId: null,
43124
+ access: "view"
43125
+ },
43126
+ "keychainAdmin.listSecrets": {
43127
+ capName: "keychain-admin",
43128
+ capScope: "system",
43129
+ addonId: null,
43130
+ access: "view"
43131
+ },
43132
+ "keychainAdmin.purgeRetiredKeys": {
43133
+ capName: "keychain-admin",
43134
+ capScope: "system",
43135
+ addonId: null,
43136
+ access: "delete"
43137
+ },
43138
+ "keychainAdmin.resetKeychain": {
43139
+ capName: "keychain-admin",
43140
+ capScope: "system",
43141
+ addonId: null,
43142
+ access: "delete"
43143
+ },
43144
+ "keychainAdmin.reveal": {
43145
+ capName: "keychain-admin",
43146
+ capScope: "system",
43147
+ addonId: null,
43148
+ access: "create"
43149
+ },
43150
+ "keychainAdmin.rotateMasterKey": {
43151
+ capName: "keychain-admin",
43152
+ capScope: "system",
43153
+ addonId: null,
43154
+ access: "create"
43155
+ },
43156
+ "keychainAdmin.status": {
43157
+ capName: "keychain-admin",
43158
+ capScope: "system",
43159
+ addonId: null,
43160
+ access: "view"
43161
+ },
41383
43162
  "lawnMowerControl.dock": {
41384
43163
  capName: "lawn-mower-control",
41385
43164
  capScope: "device",
@@ -42754,6 +44533,18 @@ Object.freeze({
42754
44533
  addonId: null,
42755
44534
  access: "create"
42756
44535
  },
44536
+ "pipelineAnalytics.addRetrainFrameFromRecording": {
44537
+ capName: "pipeline-analytics",
44538
+ capScope: "device",
44539
+ addonId: null,
44540
+ access: "create"
44541
+ },
44542
+ "pipelineAnalytics.addRetrainFramesToCollection": {
44543
+ capName: "pipeline-analytics",
44544
+ capScope: "device",
44545
+ addonId: null,
44546
+ access: "create"
44547
+ },
42757
44548
  "pipelineAnalytics.applyDeviceSettingsPatch": {
42758
44549
  capName: "pipeline-analytics",
42759
44550
  capScope: "device",
@@ -42802,12 +44593,24 @@ Object.freeze({
42802
44593
  addonId: null,
42803
44594
  access: "view"
42804
44595
  },
44596
+ "pipelineAnalytics.createRetrainCollection": {
44597
+ capName: "pipeline-analytics",
44598
+ capScope: "device",
44599
+ addonId: null,
44600
+ access: "create"
44601
+ },
42805
44602
  "pipelineAnalytics.deleteDeviceEvents": {
42806
44603
  capName: "pipeline-analytics",
42807
44604
  capScope: "device",
42808
44605
  addonId: null,
42809
44606
  access: "delete"
42810
44607
  },
44608
+ "pipelineAnalytics.deleteRetrainCollection": {
44609
+ capName: "pipeline-analytics",
44610
+ capScope: "device",
44611
+ addonId: null,
44612
+ access: "delete"
44613
+ },
42811
44614
  "pipelineAnalytics.deleteTracks": {
42812
44615
  capName: "pipeline-analytics",
42813
44616
  capScope: "device",
@@ -43012,6 +44815,24 @@ Object.freeze({
43012
44815
  addonId: null,
43013
44816
  access: "view"
43014
44817
  },
44818
+ "pipelineAnalytics.listRetrainAssistProfiles": {
44819
+ capName: "pipeline-analytics",
44820
+ capScope: "device",
44821
+ addonId: null,
44822
+ access: "view"
44823
+ },
44824
+ "pipelineAnalytics.listRetrainCollectionFrames": {
44825
+ capName: "pipeline-analytics",
44826
+ capScope: "device",
44827
+ addonId: null,
44828
+ access: "view"
44829
+ },
44830
+ "pipelineAnalytics.listRetrainCollections": {
44831
+ capName: "pipeline-analytics",
44832
+ capScope: "device",
44833
+ addonId: null,
44834
+ access: "view"
44835
+ },
43015
44836
  "pipelineAnalytics.listRetrainFrames": {
43016
44837
  capName: "pipeline-analytics",
43017
44838
  capScope: "device",
@@ -43060,6 +44881,12 @@ Object.freeze({
43060
44881
  addonId: null,
43061
44882
  access: "create"
43062
44883
  },
44884
+ "pipelineAnalytics.proposeRetrainAnnotationsWithLlm": {
44885
+ capName: "pipeline-analytics",
44886
+ capScope: "device",
44887
+ addonId: null,
44888
+ access: "create"
44889
+ },
43063
44890
  "pipelineAnalytics.pruneEvents": {
43064
44891
  capName: "pipeline-analytics",
43065
44892
  capScope: "device",
@@ -43114,6 +44941,18 @@ Object.freeze({
43114
44941
  addonId: null,
43115
44942
  access: "create"
43116
44943
  },
44944
+ "pipelineAnalytics.removeRetrainFrameFromCollection": {
44945
+ capName: "pipeline-analytics",
44946
+ capScope: "device",
44947
+ addonId: null,
44948
+ access: "delete"
44949
+ },
44950
+ "pipelineAnalytics.renameRetrainCollection": {
44951
+ capName: "pipeline-analytics",
44952
+ capScope: "device",
44953
+ addonId: null,
44954
+ access: "create"
44955
+ },
43117
44956
  "pipelineAnalytics.restageRetrainTrack": {
43118
44957
  capName: "pipeline-analytics",
43119
44958
  capScope: "device",
@@ -43144,6 +44983,12 @@ Object.freeze({
43144
44983
  addonId: null,
43145
44984
  access: "create"
43146
44985
  },
44986
+ "pipelineAnalytics.setRetrainFrameComplete": {
44987
+ capName: "pipeline-analytics",
44988
+ capScope: "device",
44989
+ addonId: null,
44990
+ access: "create"
44991
+ },
43147
44992
  "pipelineAnalytics.setTrackFlags": {
43148
44993
  capName: "pipeline-analytics",
43149
44994
  capScope: "device",
@@ -43180,6 +45025,12 @@ Object.freeze({
43180
45025
  addonId: null,
43181
45026
  access: "delete"
43182
45027
  },
45028
+ "pipelineAnalytics.writeRetrainArchive": {
45029
+ capName: "pipeline-analytics",
45030
+ capScope: "device",
45031
+ addonId: null,
45032
+ access: "create"
45033
+ },
43183
45034
  "pipelineExecutor.cacheFrameInPool": {
43184
45035
  capName: "pipeline-executor",
43185
45036
  capScope: "system",
@@ -43390,6 +45241,12 @@ Object.freeze({
43390
45241
  addonId: null,
43391
45242
  access: "view"
43392
45243
  },
45244
+ "pipelineOrchestrator.abortNodeBenchmark": {
45245
+ capName: "pipeline-orchestrator",
45246
+ capScope: "system",
45247
+ addonId: null,
45248
+ access: "create"
45249
+ },
43393
45250
  "pipelineOrchestrator.applyAutoTune": {
43394
45251
  capName: "pipeline-orchestrator",
43395
45252
  capScope: "system",
@@ -43414,6 +45271,12 @@ Object.freeze({
43414
45271
  addonId: null,
43415
45272
  access: "create"
43416
45273
  },
45274
+ "pipelineOrchestrator.deleteNodeBenchmarkResult": {
45275
+ capName: "pipeline-orchestrator",
45276
+ capScope: "system",
45277
+ addonId: null,
45278
+ access: "delete"
45279
+ },
43417
45280
  "pipelineOrchestrator.getAgentLoad": {
43418
45281
  capName: "pipeline-orchestrator",
43419
45282
  capScope: "system",
@@ -43522,6 +45385,24 @@ Object.freeze({
43522
45385
  addonId: null,
43523
45386
  access: "view"
43524
45387
  },
45388
+ "pipelineOrchestrator.getNodeBenchmarkCandidates": {
45389
+ capName: "pipeline-orchestrator",
45390
+ capScope: "system",
45391
+ addonId: null,
45392
+ access: "view"
45393
+ },
45394
+ "pipelineOrchestrator.getNodeBenchmarkPreflight": {
45395
+ capName: "pipeline-orchestrator",
45396
+ capScope: "system",
45397
+ addonId: null,
45398
+ access: "view"
45399
+ },
45400
+ "pipelineOrchestrator.getNodeBenchmarkStatus": {
45401
+ capName: "pipeline-orchestrator",
45402
+ capScope: "system",
45403
+ addonId: null,
45404
+ access: "view"
45405
+ },
43525
45406
  "pipelineOrchestrator.getNodeInferenceDevices": {
43526
45407
  capName: "pipeline-orchestrator",
43527
45408
  capScope: "system",
@@ -43564,6 +45445,12 @@ Object.freeze({
43564
45445
  addonId: null,
43565
45446
  access: "view"
43566
45447
  },
45448
+ "pipelineOrchestrator.listNodeBenchmarkResults": {
45449
+ capName: "pipeline-orchestrator",
45450
+ capScope: "system",
45451
+ addonId: null,
45452
+ access: "view"
45453
+ },
43567
45454
  "pipelineOrchestrator.pauseForStorageMigration": {
43568
45455
  capName: "pipeline-orchestrator",
43569
45456
  capScope: "system",
@@ -43708,6 +45595,12 @@ Object.freeze({
43708
45595
  addonId: null,
43709
45596
  access: "create"
43710
45597
  },
45598
+ "pipelineOrchestrator.startNodeBenchmark": {
45599
+ capName: "pipeline-orchestrator",
45600
+ capScope: "system",
45601
+ addonId: null,
45602
+ access: "create"
45603
+ },
43711
45604
  "pipelineOrchestrator.suggestShares": {
43712
45605
  capName: "pipeline-orchestrator",
43713
45606
  capScope: "system",
@@ -43726,6 +45619,12 @@ Object.freeze({
43726
45619
  addonId: null,
43727
45620
  access: "create"
43728
45621
  },
45622
+ "pipelineRunner.abortNodeBench": {
45623
+ capName: "pipeline-runner",
45624
+ capScope: "system",
45625
+ addonId: null,
45626
+ access: "create"
45627
+ },
43729
45628
  "pipelineRunner.attachCamera": {
43730
45629
  capName: "pipeline-runner",
43731
45630
  capScope: "system",
@@ -43774,6 +45673,12 @@ Object.freeze({
43774
45673
  addonId: null,
43775
45674
  access: "view"
43776
45675
  },
45676
+ "pipelineRunner.getNodeBenchStatus": {
45677
+ capName: "pipeline-runner",
45678
+ capScope: "system",
45679
+ addonId: null,
45680
+ access: "view"
45681
+ },
43777
45682
  "pipelineRunner.getParkedTrackFrame": {
43778
45683
  capName: "pipeline-runner",
43779
45684
  capScope: "system",
@@ -43816,6 +45721,12 @@ Object.freeze({
43816
45721
  addonId: null,
43817
45722
  access: "create"
43818
45723
  },
45724
+ "pipelineRunner.startNodeBench": {
45725
+ capName: "pipeline-runner",
45726
+ capScope: "system",
45727
+ addonId: null,
45728
+ access: "create"
45729
+ },
43819
45730
  "plateGallery.assignPlate": {
43820
45731
  capName: "plate-gallery",
43821
45732
  capScope: "system",
@@ -45532,18 +47443,36 @@ Object.freeze({
45532
47443
  addonId: null,
45533
47444
  access: "delete"
45534
47445
  },
47446
+ "userManagement.findUserByExternalIdentity": {
47447
+ capName: "user-management",
47448
+ capScope: "system",
47449
+ addonId: null,
47450
+ access: "view"
47451
+ },
45535
47452
  "userManagement.getTotpStatus": {
45536
47453
  capName: "user-management",
45537
47454
  capScope: "system",
45538
47455
  addonId: null,
45539
47456
  access: "view"
45540
47457
  },
47458
+ "userManagement.linkExternalIdentity": {
47459
+ capName: "user-management",
47460
+ capScope: "system",
47461
+ addonId: null,
47462
+ access: "create"
47463
+ },
45541
47464
  "userManagement.listApiKeys": {
45542
47465
  capName: "user-management",
45543
47466
  capScope: "system",
45544
47467
  addonId: null,
45545
47468
  access: "view"
45546
47469
  },
47470
+ "userManagement.listExternalIdentities": {
47471
+ capName: "user-management",
47472
+ capScope: "system",
47473
+ addonId: null,
47474
+ access: "view"
47475
+ },
45547
47476
  "userManagement.listOauthSessions": {
45548
47477
  capName: "user-management",
45549
47478
  capScope: "system",
@@ -45622,6 +47551,12 @@ Object.freeze({
45622
47551
  addonId: null,
45623
47552
  access: "create"
45624
47553
  },
47554
+ "userManagement.unlinkExternalIdentity": {
47555
+ capName: "user-management",
47556
+ capScope: "system",
47557
+ addonId: null,
47558
+ access: "delete"
47559
+ },
45625
47560
  "userManagement.updateUser": {
45626
47561
  capName: "user-management",
45627
47562
  capScope: "system",
@@ -48749,6 +50684,11 @@ Object.freeze({
48749
50684
  form: "single",
48750
50685
  optional: false
48751
50686
  }],
50687
+ "pipelineAnalytics.addRetrainFrameFromRecording": [{
50688
+ name: "deviceId",
50689
+ form: "single",
50690
+ optional: false
50691
+ }],
48752
50692
  "pipelineAnalytics.applyDeviceSettingsPatch": [{
48753
50693
  name: "deviceId",
48754
50694
  form: "single",
@@ -48939,6 +50879,11 @@ Object.freeze({
48939
50879
  form: "single",
48940
50880
  optional: false
48941
50881
  }],
50882
+ "pipelineAnalytics.proposeRetrainAnnotationsWithLlm": [{
50883
+ name: "deviceId",
50884
+ form: "single",
50885
+ optional: false
50886
+ }],
48942
50887
  "pipelineAnalytics.pruneEventsBefore": [{
48943
50888
  name: "deviceId",
48944
50889
  form: "single",
@@ -48989,6 +50934,11 @@ Object.freeze({
48989
50934
  form: "single",
48990
50935
  optional: false
48991
50936
  }],
50937
+ "pipelineAnalytics.setRetrainFrameComplete": [{
50938
+ name: "deviceId",
50939
+ form: "single",
50940
+ optional: false
50941
+ }],
48992
50942
  "pipelineAnalytics.setTrackFlags": [{
48993
50943
  name: "deviceId",
48994
50944
  form: "single",
@@ -51421,6 +53371,22 @@ function capacitySessionsOf(fps, headroom, cameraFps) {
51421
53371
  if (!(fps > 0) || !(cameraFps > 0)) return 0;
51422
53372
  return Math.max(0, Math.floor(fps * (1 - headroom) / cameraFps + 1e-9));
51423
53373
  }
53374
+ /** The table column with the node's own measurements laid over it. */
53375
+ function overlayMeasured(table, measured) {
53376
+ if (measured === void 0 || Object.keys(measured).length === 0) return table;
53377
+ const out = { ...table };
53378
+ for (const [modelId, m] of Object.entries(measured)) out[modelId] = {
53379
+ p50Ms: m.p50Ms,
53380
+ fps: m.fps,
53381
+ measured: true,
53382
+ source: {
53383
+ kind: "node-benchmark",
53384
+ resultId: m.resultId,
53385
+ measuredAtMs: m.measuredAtMs
53386
+ }
53387
+ };
53388
+ return out;
53389
+ }
51424
53390
  function round(value, digits = 3) {
51425
53391
  const f = 10 ** digits;
51426
53392
  return Math.round(value * f) / f;
@@ -51493,6 +53459,7 @@ function choiceOn(ctx, modelId, sizing) {
51493
53459
  p50Ms: cell.p50Ms,
51494
53460
  fps: cell.fps,
51495
53461
  measured: cell.measured,
53462
+ source: cell.source ?? { kind: "table" },
51496
53463
  capacitySessions: sessionsOn(cell, sizing),
51497
53464
  personRecall: DETECTOR_ACCURACY[modelId]?.personRecall ?? 0
51498
53465
  };
@@ -51541,11 +53508,14 @@ function recommendNode(input) {
51541
53508
  if (node.hardware === null) return skippedNode(node, { kind: "no-hardware-facts" }, null);
51542
53509
  const hardware = node.hardware;
51543
53510
  const { hardwareClass, cpuModel } = classifyInferenceHardware(hardware);
51544
- const classTable = perf[hardwareClass];
51545
- if (hardwareClass === "unknown" || classTable === void 0) return skippedNode(node, {
53511
+ const measured = input.measured ?? {};
53512
+ const hasMeasured = Object.values(measured).some((m) => Object.keys(m).length > 0);
53513
+ const knownTable = hardwareClass === "unknown" ? void 0 : perf[hardwareClass];
53514
+ if (knownTable === void 0 && !hasMeasured) return skippedNode(node, {
51546
53515
  kind: "no-measured-data",
51547
53516
  hardwareClass
51548
53517
  }, hardwareClass);
53518
+ const classTable = knownTable ?? {};
51549
53519
  const devices = node.devices.toSorted((a, b) => a.key.localeCompare(b.key));
51550
53520
  const contexts = /* @__PURE__ */ new Map();
51551
53521
  const early = /* @__PURE__ */ new Map();
@@ -51563,7 +53533,7 @@ function recommendNode(input) {
51563
53533
  early.set(device.key, [{ kind: "device-unavailable" }]);
51564
53534
  continue;
51565
53535
  }
51566
- const column = accelerator === null ? void 0 : classTable[accelerator];
53536
+ const column = accelerator === null ? void 0 : overlayMeasured(classTable[accelerator], measured[device.key]);
51567
53537
  if (accelerator === null || column === void 0) {
51568
53538
  early.set(device.key, [{
51569
53539
  kind: "no-family-build",
@@ -51578,13 +53548,28 @@ function recommendNode(input) {
51578
53548
  });
51579
53549
  }
51580
53550
  const isCpu = (a) => a === "x86-cpu" || a === "arm-cpu";
51581
- const accelerators = [...contexts.values()].filter((c) => !isCpu(c.accelerator));
51582
- const cpus = [...contexts.values()].filter((c) => isCpu(c.accelerator));
53551
+ const keptByKey = /* @__PURE__ */ new Map();
53552
+ const keptReasonOf = (group) => {
53553
+ const pinned = group.find((c) => c.device.customPinModelId !== void 0);
53554
+ if (pinned?.device.customPinModelId === void 0) return null;
53555
+ const reason = {
53556
+ kind: "kept-custom-pin",
53557
+ modelId: pinned.device.customPinModelId,
53558
+ deviceKey: pinned.device.key
53559
+ };
53560
+ for (const c of group) keptByKey.set(c.device.key, [reason]);
53561
+ return reason;
53562
+ };
53563
+ const acceleratorGroup = [...contexts.values()].filter((c) => !isCpu(c.accelerator));
53564
+ const acceleratorKept = keptReasonOf(acceleratorGroup);
53565
+ const accelerators = acceleratorKept === null ? acceleratorGroup : [];
53566
+ const cpus = [...contexts.values()].filter((c) => isCpu(c.accelerator)).filter((c) => keptReasonOf([c]) === null);
51583
53567
  const chosenByKey = /* @__PURE__ */ new Map();
51584
53568
  const reasonsByKey = /* @__PURE__ */ new Map();
51585
53569
  const nodeReasons = [];
51586
53570
  let acceleratorModelId = null;
51587
53571
  const groupPick = accelerators.length > 0 ? pickForGroup(accelerators, node.formatsByModel, sizing) : null;
53572
+ if (acceleratorKept !== null) nodeReasons.push(acceleratorKept);
51588
53573
  if (groupPick !== null) {
51589
53574
  acceleratorModelId = groupPick.modelId;
51590
53575
  nodeReasons.push(...groupPick.reasons);
@@ -51628,7 +53613,8 @@ function recommendNode(input) {
51628
53613
  enabled: device.enabled,
51629
53614
  available: device.available,
51630
53615
  chosen: chosenByKey.get(device.key) ?? null,
51631
- reasons: early.get(device.key) ?? reasonsByKey.get(device.key) ?? []
53616
+ ...keptByKey.has(device.key) ? { kept: "custom-pin" } : {},
53617
+ reasons: early.get(device.key) ?? keptByKey.get(device.key) ?? reasonsByKey.get(device.key) ?? []
51632
53618
  })),
51633
53619
  reasons: nodeReasons
51634
53620
  };
@@ -51740,24 +53726,6 @@ function resolveEffectiveShares(input) {
51740
53726
  deviceWeights
51741
53727
  };
51742
53728
  }
51743
- /**
51744
- * Automatic shares from CAPACITY (D755). Pure.
51745
- *
51746
- * A node's share is its capacity in parallel sessions over the cluster's; a
51747
- * device's share inside its node follows its throughput. There is no load
51748
- * input: nothing here depends on how many sessions ran, only on what each
51749
- * node can carry.
51750
- *
51751
- * Two places a capacity can come from:
51752
- * - `hardware` — each enabled accelerator at its Auto tune model, from the
51753
- * measured latency table: ⌊fps × (1 − headroom) / camera fps⌋ sessions;
51754
- * - `benchmark` — the node's own measured max sessions per device, read
51755
- * through a {@link NodeCapacitySource}. A node whose results do not cover
51756
- * every enabled device it would be sized on is sized from `hardware`
51757
- * instead, and the answer says so per node — never a silent mix.
51758
- */
51759
- /** A source that has no results — the benchmark store before it exists. */
51760
- var NO_MEASURED_CAPACITY = { readMeasuredCapacity: async () => /* @__PURE__ */ new Map() };
51761
53729
  /** Integer shares summing to 100 (largest remainder); ties broken by key. */
51762
53730
  function toPercentShares(weights) {
51763
53731
  const total = [...weights.values()].reduce((a, b) => a + b, 0);
@@ -51872,6 +53840,258 @@ function computeCapacityShares(input) {
51872
53840
  };
51873
53841
  }
51874
53842
  /**
53843
+ * Benchmark mode (D791) — the pure rules of "one node under test". Pure.
53844
+ *
53845
+ * While the cluster is in benchmark mode for node N:
53846
+ * - N is not a detection candidate for any camera that another node can
53847
+ * serve ({@link eligibleUnderBenchmark});
53848
+ * - a node pin naming N is ignored for such a camera
53849
+ * ({@link pinUnderBenchmark}) — the stored pin is never rewritten, so it
53850
+ * is "restored" the moment the mode ends;
53851
+ * - a camera whose ONLY frame source is N stays on N (never blind) and the
53852
+ * run is flagged contaminated.
53853
+ */
53854
+ /** The detection candidates of one camera while `benchNodeId` is under test. */
53855
+ function eligibleUnderBenchmark(eligible, benchNodeId) {
53856
+ if (benchNodeId === null || !eligible.includes(benchNodeId)) return eligible;
53857
+ const others = eligible.filter((n) => n !== benchNodeId);
53858
+ return others.length > 0 ? others : eligible;
53859
+ }
53860
+ /** A camera's node pin while `benchNodeId` is under test (null = no pin). */
53861
+ function pinUnderBenchmark(pin, benchNodeId, eligible) {
53862
+ if (pin === null || benchNodeId === null || pin !== benchNodeId) return pin;
53863
+ return eligible.some((n) => n !== benchNodeId) ? null : pin;
53864
+ }
53865
+ /** Which cameras leave the node for the run, and which cannot. */
53866
+ function classifyCamerasForBenchmark(nodeId, cameras) {
53867
+ const moving = [];
53868
+ const staying = [];
53869
+ for (const cam of cameras.toSorted((a, b) => a.deviceId - b.deviceId)) {
53870
+ const placedOnNode = cam.placedNodeId === nodeId;
53871
+ const pinnedToNode = cam.nodePin === nodeId;
53872
+ if (!placedOnNode && !pinnedToNode) continue;
53873
+ if (!cam.frameSourceNodes.some((n) => n !== nodeId)) staying.push({
53874
+ deviceId: cam.deviceId,
53875
+ reason: "only-frame-source"
53876
+ });
53877
+ else moving.push({
53878
+ deviceId: cam.deviceId,
53879
+ placedOnNode,
53880
+ pinnedToNode
53881
+ });
53882
+ }
53883
+ return {
53884
+ moving,
53885
+ staying
53886
+ };
53887
+ }
53888
+ /** Ramp steps the runner tries per cell, at most. */
53889
+ var DEVICE_RAMP = [
53890
+ 1,
53891
+ 2,
53892
+ 4,
53893
+ 6,
53894
+ 8,
53895
+ 12,
53896
+ 16,
53897
+ 24,
53898
+ 32
53899
+ ];
53900
+ var PIPELINE_RAMP = [
53901
+ 1,
53902
+ 2,
53903
+ 3,
53904
+ 4,
53905
+ 6,
53906
+ 8,
53907
+ 10,
53908
+ 12,
53909
+ 16
53910
+ ];
53911
+ /** Fixed costs around the measurement: drain, clip, warm-ups, restore. */
53912
+ var OVERHEAD_MS = 3 * 6e4;
53913
+ var WARMUP_PER_CELL_MS = 2e4;
53914
+ /**
53915
+ * An upper bound on how long the run takes: every ramp step of every cell,
53916
+ * plus the drain, the clip and the restore — capped at the budget, which is
53917
+ * the hard deadline anyway. A ramp usually stops well before its last step.
53918
+ */
53919
+ function estimateNodeBenchmarkDurationMs(plan, timing = {}) {
53920
+ const stepMs = ((timing.settleSec ?? NODE_BENCHMARK_DEFAULTS.settleSec) + (timing.measureSec ?? NODE_BENCHMARK_DEFAULTS.measureSec)) * 1e3;
53921
+ let total = OVERHEAD_MS;
53922
+ if (plan.phases.includes("device")) for (const device of plan.devices) total += device.modelIds.length * (WARMUP_PER_CELL_MS + DEVICE_RAMP.length * stepMs);
53923
+ if (plan.phases.includes("pipeline")) total += plan.devices.length * (WARMUP_PER_CELL_MS + PIPELINE_RAMP.length * stepMs);
53924
+ return Math.min(total, plan.budgetMinutes * 6e4);
53925
+ }
53926
+ /** Why a run cannot start, in the order the operator should read them. */
53927
+ function nodeBenchmarkRefusals(input) {
53928
+ const out = [];
53929
+ if (input.activeRunNodeId !== null) out.push({
53930
+ kind: "benchmark-active",
53931
+ detail: `A benchmark of ${input.activeRunNodeId} is running — one node at a time.`,
53932
+ overridable: false
53933
+ });
53934
+ if (!input.online) out.push({
53935
+ kind: "node-offline",
53936
+ detail: `${input.plan.nodeId} is not online.`,
53937
+ overridable: false
53938
+ });
53939
+ const unknown = input.plan.devices.map((d) => d.deviceKey).filter((k) => !input.knownDeviceKeys.includes(k));
53940
+ if (unknown.length > 0) out.push({
53941
+ kind: "unknown-device",
53942
+ detail: `${input.plan.nodeId} reports no device ${unknown.join(", ")}.`,
53943
+ overridable: false
53944
+ });
53945
+ if (input.plan.phases.length === 0) out.push({
53946
+ kind: "no-phase",
53947
+ detail: "Choose at least one measurement.",
53948
+ overridable: false
53949
+ });
53950
+ const capacity = input.otherNodesCapacitySessions;
53951
+ if (capacity !== null && input.sessionsNow > capacity && !input.plan.acceptInsufficientHeadroom) out.push({
53952
+ kind: "insufficient-headroom",
53953
+ detail: `The cluster runs ${input.sessionsNow} sessions now; the other nodes can carry ${capacity}.`,
53954
+ overridable: true
53955
+ });
53956
+ return out;
53957
+ }
53958
+ /** The cell a row measures — two rows with the same key measure the same thing. */
53959
+ function resultCellKey(row) {
53960
+ const { cell } = row;
53961
+ const steps = cell.phase === "pipeline" ? cell.steps.toSorted().join(",") : "";
53962
+ return [
53963
+ row.nodeId,
53964
+ cell.deviceKey,
53965
+ cell.phase,
53966
+ cell.modelId,
53967
+ steps
53968
+ ].join("|");
53969
+ }
53970
+ function staleWhy(row, ctx) {
53971
+ const bound = ctx.staleAfterMs ?? NODE_BENCHMARK_DEFAULTS.staleAfterDays * 864e5;
53972
+ if (ctx.nowMs - row.measuredAtMs > bound) return "age";
53973
+ const now = ctx.currentPools[row.nodeId]?.[row.cell.deviceKey];
53974
+ const then = row.fingerprint?.pools[row.cell.deviceKey];
53975
+ if (now === void 0 || then === void 0) return null;
53976
+ if (now.runtime !== then.runtime) return "runtime";
53977
+ if (now.numWorkers !== then.numWorkers || now.concurrency !== then.concurrency || now.batchMode !== then.batchMode) return "pool-tuning";
53978
+ return null;
53979
+ }
53980
+ /** The standing of every row, by id. */
53981
+ function resultStandings(rows, ctx) {
53982
+ const newest = /* @__PURE__ */ new Map();
53983
+ for (const row of rows) {
53984
+ const key = resultCellKey(row);
53985
+ const held = newest.get(key);
53986
+ if (held === void 0 || row.measuredAtMs > held.measuredAtMs) newest.set(key, row);
53987
+ }
53988
+ const out = /* @__PURE__ */ new Map();
53989
+ for (const row of rows) {
53990
+ if (newest.get(resultCellKey(row))?.id !== row.id) {
53991
+ out.set(row.id, {
53992
+ standing: "superseded",
53993
+ staleWhy: null
53994
+ });
53995
+ continue;
53996
+ }
53997
+ const why = staleWhy(row, ctx);
53998
+ out.set(row.id, why === null ? {
53999
+ standing: "fresh",
54000
+ staleWhy: null
54001
+ } : {
54002
+ standing: "stale",
54003
+ staleWhy: why
54004
+ });
54005
+ }
54006
+ return out;
54007
+ }
54008
+ /** Rows that beat the table today. */
54009
+ function freshResults(rows, ctx) {
54010
+ const standings = resultStandings(rows, ctx);
54011
+ return rows.filter((r) => standings.get(r.id)?.standing === "fresh");
54012
+ }
54013
+ /** deviceKey → modelId → the fresh `device` measurement on `nodeId`. */
54014
+ function measuredDetectorCells(rows, nodeId, ctx) {
54015
+ const out = {};
54016
+ for (const row of freshResults(rows, ctx)) {
54017
+ if (row.nodeId !== nodeId || row.cell.phase !== "device") continue;
54018
+ const perDevice = out[row.cell.deviceKey] ?? {};
54019
+ perDevice[row.cell.modelId] = {
54020
+ p50Ms: Math.round(row.cell.p50Ms * 100) / 100,
54021
+ fps: row.cell.fps,
54022
+ resultId: row.id,
54023
+ measuredAtMs: row.measuredAtMs
54024
+ };
54025
+ out[row.cell.deviceKey] = perDevice;
54026
+ }
54027
+ return out;
54028
+ }
54029
+ /**
54030
+ * Per node, each device at its newest fresh `pipeline` measurement — the
54031
+ * `NodeCapacitySource` answer the automatic shares read.
54032
+ */
54033
+ function measuredCapacityFromResults(rows, nodeIds, ctx) {
54034
+ const byNode = /* @__PURE__ */ new Map();
54035
+ for (const row of freshResults(rows, ctx)) {
54036
+ if (!nodeIds.includes(row.nodeId) || row.cell.phase !== "pipeline") continue;
54037
+ const devices = byNode.get(row.nodeId) ?? /* @__PURE__ */ new Map();
54038
+ const held = devices.get(row.cell.deviceKey);
54039
+ if (held === void 0 || row.measuredAtMs > held.measuredAtMs) devices.set(row.cell.deviceKey, row);
54040
+ byNode.set(row.nodeId, devices);
54041
+ }
54042
+ const out = /* @__PURE__ */ new Map();
54043
+ for (const nodeId of nodeIds) {
54044
+ const devices = byNode.get(nodeId);
54045
+ if (devices === void 0) continue;
54046
+ out.set(nodeId, {
54047
+ nodeId,
54048
+ devices: [...devices.values()].toSorted((a, b) => a.cell.deviceKey.localeCompare(b.cell.deviceKey)).map((r) => ({
54049
+ deviceKey: r.cell.deviceKey,
54050
+ maxSessions: r.cell.phase === "pipeline" ? r.cell.maxSessions : 0,
54051
+ modelId: r.cell.modelId,
54052
+ measuredAtMs: r.measuredAtMs
54053
+ }))
54054
+ });
54055
+ }
54056
+ return out;
54057
+ }
54058
+ var BENCH_ROOT_STEP_ID = "object-detection";
54059
+ function slotAddons(schema, slot) {
54060
+ return schema.slots.find((s) => s.id === slot)?.addons ?? [];
54061
+ }
54062
+ var feeds = (from, to) => to.inputClasses.some((c) => from.outputClasses.includes(c));
54063
+ function layout(schema) {
54064
+ const root = slotAddons(schema, "detector").find((a) => a.id === BENCH_ROOT_STEP_ID);
54065
+ if (root === void 0) return null;
54066
+ const classifiers = slotAddons(schema, "classifier");
54067
+ const croppers = slotAddons(schema, "cropper").filter((c) => feeds(root, c)).map((cropper) => ({
54068
+ cropper,
54069
+ recognizers: classifiers.filter((k) => feeds(cropper, k))
54070
+ }));
54071
+ const underCropper = new Set(croppers.flatMap((c) => c.recognizers.map((r) => r.id)));
54072
+ return {
54073
+ root,
54074
+ croppers,
54075
+ classifiers: classifiers.filter((k) => feeds(root, k) && !underCropper.has(k.id)),
54076
+ refiners: slotAddons(schema, "refiner")
54077
+ };
54078
+ }
54079
+ /** The detail steps the operator can choose, in tree order. */
54080
+ function listBenchDetailSteps(schema) {
54081
+ const l = layout(schema);
54082
+ if (l === null) return [];
54083
+ const of = (a) => ({
54084
+ id: a.id,
54085
+ name: a.name,
54086
+ slot: a.slot
54087
+ });
54088
+ return [
54089
+ ...l.croppers.flatMap((c) => [of(c.cropper), ...c.recognizers.map(of)]),
54090
+ ...l.classifiers.map(of),
54091
+ ...l.refiners.map(of)
54092
+ ];
54093
+ }
54094
+ /**
51875
54095
  * THE detail-crop convention — the single derivation of the rectangle a
51876
54096
  * detail/enrichment step (clip-embedding, face-detection, plate-detection…)
51877
54097
  * is fed.
@@ -52215,6 +54435,959 @@ function deviceBackendToFormat(backend) {
52215
54435
  return DEVICE_BACKEND_TO_FORMAT[backend] ?? "onnx";
52216
54436
  }
52217
54437
  //#endregion
54438
+ //#region src/durable/durable-ledger.ts
54439
+ /** Default reseed cap — every current consumer's row set is installation-bounded. */
54440
+ var DEFAULT_LOAD_LIMIT = 1e5;
54441
+ var DurableLedger = class DurableLedger {
54442
+ mirror = /* @__PURE__ */ new Map();
54443
+ /** Whether ANY load has read the store successfully — see {@link hasAnswered}. */
54444
+ answered = false;
54445
+ spec;
54446
+ store;
54447
+ logger;
54448
+ constructor(deps) {
54449
+ this.spec = deps.spec;
54450
+ this.store = deps.store;
54451
+ this.logger = deps.logger;
54452
+ }
54453
+ /**
54454
+ * Register the collection. MUST run at boot, before any read or write: the
54455
+ * SQLite backend answers 412 for an undeclared collection and takes the whole
54456
+ * runner down with it (the addon-ai boot-crash lesson).
54457
+ */
54458
+ static declare(store, spec) {
54459
+ return store.declareCollection.mutate({
54460
+ collection: spec.collection,
54461
+ columns: [...spec.columns],
54462
+ ...spec.indexes !== void 0 ? { indexes: [...spec.indexes] } : {}
54463
+ });
54464
+ }
54465
+ declare() {
54466
+ return DurableLedger.declare(this.store, this.spec);
54467
+ }
54468
+ /** The collection this ledger owns — for the caller's own log lines. */
54469
+ get collection() {
54470
+ return this.spec.collection;
54471
+ }
54472
+ /**
54473
+ * Boot reseed. Replaces the mirror with what the store holds and returns the
54474
+ * rows, so a caller that must hydrate something else (a watcher, a registry)
54475
+ * gets them without a second read.
54476
+ *
54477
+ * **A failure returns what is already mirrored** rather than clearing it —
54478
+ * see contract rule 2. The count is worth logging out loud at the call site:
54479
+ * "loaded 0" after a container recreate is the one line that explains a
54480
+ * silent flood.
54481
+ */
54482
+ async load() {
54483
+ try {
54484
+ const records = await this.store.query.query({
54485
+ collection: this.spec.collection,
54486
+ filter: { limit: this.spec.loadLimit ?? DEFAULT_LOAD_LIMIT }
54487
+ });
54488
+ const next = /* @__PURE__ */ new Map();
54489
+ let skipped = 0;
54490
+ for (const record of records) {
54491
+ const row = this.spec.fromRecord(record.id, record.data);
54492
+ if (row === null) {
54493
+ skipped += 1;
54494
+ continue;
54495
+ }
54496
+ next.set(this.spec.keyOf(row), row);
54497
+ }
54498
+ this.mirror.clear();
54499
+ for (const [key, row] of next) this.mirror.set(key, row);
54500
+ this.answered = true;
54501
+ if (skipped > 0) this.logger.warn("durable rows skipped as malformed — they gate NOTHING", { meta: {
54502
+ collection: this.spec.collection,
54503
+ skipped
54504
+ } });
54505
+ return [...this.mirror.values()];
54506
+ } catch (err) {
54507
+ this.logger.warn("durable load failed — keeping the state already in memory", { meta: {
54508
+ collection: this.spec.collection,
54509
+ error: String(err),
54510
+ held: this.mirror.size
54511
+ } });
54512
+ return [...this.mirror.values()];
54513
+ }
54514
+ }
54515
+ /**
54516
+ * Whether the mirror is an ANSWER from the store: true once any load has
54517
+ * succeeded, and never false again (a later failed load keeps that answer).
54518
+ * Before it, an empty mirror is "not read yet", never "the store is empty" —
54519
+ * {@link load} swallows its failure, so a caller that must tell the two apart
54520
+ * asks here (D642 review I1).
54521
+ */
54522
+ hasAnswered() {
54523
+ return this.answered;
54524
+ }
54525
+ /** Every mirrored row, insertion-ordered. */
54526
+ snapshot() {
54527
+ return [...this.mirror.values()];
54528
+ }
54529
+ /** The row currently held for a key, if any. Pure RAM — never I/O. */
54530
+ get(key) {
54531
+ return this.mirror.get(key);
54532
+ }
54533
+ has(key) {
54534
+ return this.mirror.has(key);
54535
+ }
54536
+ get size() {
54537
+ return this.mirror.size;
54538
+ }
54539
+ /**
54540
+ * Judge ONE observation against what the ledger already accepted, and advance
54541
+ * it.
54542
+ *
54543
+ * Synchronous on purpose: the verdict is a function of the in-RAM mirror
54544
+ * alone, so a decision can never be gated on an I/O that might fail (D49).
54545
+ * The durable write is kicked off behind it and its failure changes no
54546
+ * verdict.
54547
+ *
54548
+ * `no-flip` leaves the held row UNTOUCHED — including any timestamp it
54549
+ * carries, which therefore means "when this key last CHANGED", not "when it
54550
+ * last spoke". That is the timestamp anyone reading the table wants.
54551
+ */
54552
+ observe(row) {
54553
+ const equalFact = this.spec.equalFact;
54554
+ if (equalFact === void 0) throw new Error(`DurableLedger(${this.spec.collection}): observe() requires the spec to declare equalFact`);
54555
+ const key = this.spec.keyOf(row);
54556
+ const held = this.mirror.get(key);
54557
+ if (held !== void 0 && equalFact(held, row)) return "no-flip";
54558
+ this.mirror.set(key, row);
54559
+ this.persist(row);
54560
+ return held === void 0 ? "seeded" : "flip";
54561
+ }
54562
+ /**
54563
+ * Upsert a row without a verdict — the write path for a ledger whose owner
54564
+ * has already decided the value changed.
54565
+ *
54566
+ * The order of the mirror advance and the durable write is the spec's
54567
+ * {@link DurableWriteMode}, not the call site's: two call sites that
54568
+ * disagreed about it would be two different durability guarantees on one
54569
+ * collection.
54570
+ */
54571
+ async put(row) {
54572
+ const key = this.spec.keyOf(row);
54573
+ if (this.spec.writeMode === "write-behind") {
54574
+ this.mirror.set(key, row);
54575
+ await this.persist(row);
54576
+ return;
54577
+ }
54578
+ await this.store.set.mutate({
54579
+ collection: this.spec.collection,
54580
+ key,
54581
+ value: this.spec.toValue(row)
54582
+ });
54583
+ this.mirror.set(key, row);
54584
+ }
54585
+ /**
54586
+ * Advance the mirror WITHOUT persisting, for an owner that deliberately
54587
+ * coalesces its writes.
54588
+ *
54589
+ * The stationary registry is the reason this exists: a parked car is
54590
+ * re-confirmed on every processed frame (5–30 Hz), and persisting each
54591
+ * confirmation would offer thousands of commits a day to the checkpoint
54592
+ * lottery (D96) to maintain a handful of rows. It stages the advance and
54593
+ * flushes on its 5-minute sweep — 288 writes a day instead of ~10⁶.
54594
+ *
54595
+ * **The cost is stated, not hidden**: a staged value that is never flushed
54596
+ * is lost on a crash. An owner may only stage a field whose staleness its
54597
+ * own TTL absorbs. Anything that GATES work must go through {@link put} or
54598
+ * {@link observe}.
54599
+ */
54600
+ stage(row) {
54601
+ this.mirror.set(this.spec.keyOf(row), row);
54602
+ }
54603
+ /**
54604
+ * Drop a key from the MIRROR only — the durable row survives.
54605
+ *
54606
+ * What a scope-unbind needs: this process stops holding the value, and a
54607
+ * rebind reloads it from the store. Deliberately distinct from
54608
+ * {@link forget}, which deletes; conflating the two is how an unbind turns
54609
+ * into a wipe.
54610
+ */
54611
+ evict(key) {
54612
+ this.mirror.delete(key);
54613
+ }
54614
+ /**
54615
+ * Drop one key, mirror and row. Best-effort on the durable half: a failed
54616
+ * delete leaves a row that the next load will re-mirror, which is a stale
54617
+ * value rather than a lost one.
54618
+ */
54619
+ async forget(key) {
54620
+ this.mirror.delete(key);
54621
+ try {
54622
+ await this.store.delete.mutate({
54623
+ collection: this.spec.collection,
54624
+ key
54625
+ });
54626
+ } catch (err) {
54627
+ this.logger.debug("durable delete failed", { meta: {
54628
+ collection: this.spec.collection,
54629
+ key,
54630
+ error: String(err)
54631
+ } });
54632
+ }
54633
+ }
54634
+ /**
54635
+ * Drop every mirrored key NOT in `activeKeys`. Returns how many rows went.
54636
+ *
54637
+ * **The caller must hold an AUTHORITATIVE active set.** A prune driven by a
54638
+ * fallible read is work destroyed on an error (D49/D130) — that is why this
54639
+ * is a method a feature opts into rather than a policy the primitive runs.
54640
+ * Best-effort per row: a failed delete keeps the key (retried next prune)
54641
+ * rather than aborting the sweep.
54642
+ */
54643
+ async pruneExcept(activeKeys) {
54644
+ let pruned = 0;
54645
+ for (const key of [...this.mirror.keys()]) {
54646
+ if (activeKeys.has(key)) continue;
54647
+ try {
54648
+ await this.store.delete.mutate({
54649
+ collection: this.spec.collection,
54650
+ key
54651
+ });
54652
+ this.mirror.delete(key);
54653
+ pruned += 1;
54654
+ } catch (err) {
54655
+ this.logger.debug("durable prune delete failed", { meta: {
54656
+ collection: this.spec.collection,
54657
+ key,
54658
+ error: String(err)
54659
+ } });
54660
+ }
54661
+ }
54662
+ return pruned;
54663
+ }
54664
+ /**
54665
+ * Write-behind durable upsert. Best-effort and logged, never thrown at the
54666
+ * decision path: the mirror already holds the truth for this process, and the
54667
+ * worst a lost write can do is one silent re-seed after the next restart.
54668
+ */
54669
+ async persist(row) {
54670
+ const deviceId = this.spec.deviceIdOf?.(row);
54671
+ try {
54672
+ await this.store.set.mutate({
54673
+ collection: this.spec.collection,
54674
+ key: this.spec.keyOf(row),
54675
+ value: this.spec.toValue(row)
54676
+ });
54677
+ } catch (err) {
54678
+ this.logger.warn("durable persist failed — this key may re-seed on boot", {
54679
+ ...deviceId !== void 0 ? { tags: { deviceId } } : {},
54680
+ meta: {
54681
+ collection: this.spec.collection,
54682
+ key: this.spec.keyOf(row),
54683
+ error: String(err)
54684
+ }
54685
+ });
54686
+ }
54687
+ }
54688
+ };
54689
+ //#endregion
54690
+ //#region src/node-benchmark/node-benchmark-store.ts
54691
+ /**
54692
+ * The node benchmark's three durable collections (D791), all owned by the
54693
+ * orchestrator — the placement authority that enters and leaves benchmark
54694
+ * mode, and the one writer of the results Auto tune and the shares read.
54695
+ *
54696
+ * - the MANIFEST: one row, written BEFORE benchmark mode changes anything
54697
+ * and removed only after the cluster is restored. Its presence at boot or
54698
+ * on a reconcile with no live run means "restore now".
54699
+ * - the RUNS: what each run did and why it ended (bounded history).
54700
+ * - the RESULTS: one row per measured cell.
54701
+ */
54702
+ /**
54703
+ * @durable class=ledger owner=pipeline-orchestrator
54704
+ * write="once when benchmark mode is entered (write-through, BEFORE any change), removed once the cluster is restored"
54705
+ * retention="at most one row (key `active`); its presence means a restore is owed"
54706
+ */
54707
+ var NODE_BENCHMARK_MODE_COLLECTION = "pipeline-orchestrator:node-benchmark-mode";
54708
+ /**
54709
+ * @durable class=audit owner=pipeline-orchestrator
54710
+ * write="one row per run, rewritten when the run ends"
54711
+ * retention="the newest 50 runs; older rows are removed when a run is recorded"
54712
+ */
54713
+ var NODE_BENCHMARK_RUNS_COLLECTION = "pipeline-orchestrator:node-benchmark-runs";
54714
+ /**
54715
+ * @durable class=ledger owner=pipeline-orchestrator
54716
+ * write="one row per measured cell, written when its run ends; deleted by the operator"
54717
+ * retention="the newest 500 rows; a row older than the stale bound stays listed but no longer beats the table"
54718
+ */
54719
+ var NODE_BENCHMARK_RESULTS_COLLECTION = "pipeline-orchestrator:node-benchmarks";
54720
+ var JSON_ROW_COLUMNS = [
54721
+ {
54722
+ name: "id",
54723
+ type: "TEXT",
54724
+ primaryKey: true,
54725
+ notNull: true
54726
+ },
54727
+ {
54728
+ name: "body",
54729
+ type: "JSON",
54730
+ notNull: true
54731
+ },
54732
+ {
54733
+ name: "at",
54734
+ type: "INTEGER",
54735
+ notNull: true
54736
+ }
54737
+ ];
54738
+ var MANIFEST_KEY = "active";
54739
+ function jsonSpec(collection, schema, keyOf, at) {
54740
+ return {
54741
+ collection,
54742
+ columns: JSON_ROW_COLUMNS,
54743
+ writeMode: "write-through",
54744
+ keyOf,
54745
+ toValue: (row) => ({
54746
+ body: row,
54747
+ at: at(row)
54748
+ }),
54749
+ fromRecord: (_key, data) => {
54750
+ const parsed = schema.safeParse(data["body"]);
54751
+ return parsed.success ? parsed.data : null;
54752
+ }
54753
+ };
54754
+ }
54755
+ var MANIFEST_SPEC = jsonSpec(NODE_BENCHMARK_MODE_COLLECTION, NodeBenchmarkManifestSchema, () => MANIFEST_KEY, (m) => m.startedAtMs);
54756
+ var RUNS_SPEC = jsonSpec(NODE_BENCHMARK_RUNS_COLLECTION, NodeBenchmarkRunSchema, (r) => r.runId, (r) => r.startedAtMs);
54757
+ var RESULTS_SPEC = jsonSpec(NODE_BENCHMARK_RESULTS_COLLECTION, NodeBenchmarkResultSchema, (r) => r.id, (r) => r.measuredAtMs);
54758
+ /** Keep the newest `max` rows by `at`; the rest are the ones to drop. */
54759
+ function overflow(rows, max, at) {
54760
+ if (rows.length <= max) return [];
54761
+ return rows.toSorted((a, b) => at(b) - at(a)).slice(max);
54762
+ }
54763
+ var NodeBenchmarkStore = class {
54764
+ manifest;
54765
+ runs;
54766
+ results;
54767
+ constructor(deps) {
54768
+ this.manifest = new DurableLedger({
54769
+ spec: MANIFEST_SPEC,
54770
+ ...deps
54771
+ });
54772
+ this.runs = new DurableLedger({
54773
+ spec: RUNS_SPEC,
54774
+ ...deps
54775
+ });
54776
+ this.results = new DurableLedger({
54777
+ spec: RESULTS_SPEC,
54778
+ ...deps
54779
+ });
54780
+ }
54781
+ static async declare(store) {
54782
+ await DurableLedger.declare(store, MANIFEST_SPEC);
54783
+ await DurableLedger.declare(store, RUNS_SPEC);
54784
+ await DurableLedger.declare(store, RESULTS_SPEC);
54785
+ }
54786
+ async load() {
54787
+ await this.manifest.load();
54788
+ await this.runs.load();
54789
+ await this.results.load();
54790
+ }
54791
+ readManifest() {
54792
+ return this.manifest.get(MANIFEST_KEY) ?? null;
54793
+ }
54794
+ writeManifest(manifest) {
54795
+ return this.manifest.put(manifest);
54796
+ }
54797
+ clearManifest() {
54798
+ return this.manifest.forget(MANIFEST_KEY);
54799
+ }
54800
+ async recordRun(run) {
54801
+ await this.runs.put(run);
54802
+ for (const old of overflow(this.runs.snapshot(), 50, (r) => r.startedAtMs)) await this.runs.forget(old.runId);
54803
+ }
54804
+ listRuns() {
54805
+ return this.runs.snapshot().toSorted((a, b) => b.startedAtMs - a.startedAtMs);
54806
+ }
54807
+ async writeResults(rows) {
54808
+ for (const row of rows) await this.results.put(row);
54809
+ for (const old of overflow(this.results.snapshot(), 500, (r) => r.measuredAtMs)) await this.results.forget(old.id);
54810
+ }
54811
+ listResults() {
54812
+ return this.results.snapshot().toSorted((a, b) => b.measuredAtMs - a.measuredAtMs);
54813
+ }
54814
+ async deleteResult(id) {
54815
+ if (!this.results.has(id)) return false;
54816
+ await this.results.forget(id);
54817
+ return true;
54818
+ }
54819
+ };
54820
+ //#endregion
54821
+ //#region src/node-benchmark/node-benchmark-service.ts
54822
+ /**
54823
+ * The node benchmark lifecycle (D791) — the orchestrator owns it because it
54824
+ * is the placement authority: entering benchmark mode is a placement change,
54825
+ * and only the owner of placement can promise to undo it.
54826
+ *
54827
+ * start ─► preflight ─► MANIFEST (persisted first) ─► mode on ─► draining
54828
+ * ─► measuring (the node's runner, polled) ─► restoring ─► done | failed | aborted
54829
+ *
54830
+ * Invariants:
54831
+ * - ONE run cluster-wide; it does not matter which runner the request
54832
+ * reached — the orchestrator drives the target node's runner by nodePin.
54833
+ * - The manifest is written BEFORE anything changes and removed only AFTER
54834
+ * the cluster is restored. Its presence without a live run — at boot, or
54835
+ * on any reconcile tick — means "restore now" (registry-driven, never a
54836
+ * timer or an event that may be lost).
54837
+ * - Restoring always runs (`finally`): mode off, cameras pinned to the node
54838
+ * re-placed, the runner told to stop, the results written, the run
54839
+ * recorded. An abort leaves the cluster as it was.
54840
+ * - Nothing the operator owns is written: no detect flag, no pin, no cap.
54841
+ * Pins are ignored by the mode, never rewritten, so ending the mode IS the
54842
+ * restore of the pins.
54843
+ */
54844
+ var DEFAULT_POLL_MS = 3e3;
54845
+ /** After the deadline the runner gets this long to stop before the restore is forced. */
54846
+ var DEADLINE_GRACE_MS = 2 * 6e4;
54847
+ /** The node whose duties never stop for a benchmark: its numbers always share the CPU. */
54848
+ var HUB_NODE_ID = "hub";
54849
+ function toRun(manifest) {
54850
+ return {
54851
+ runId: manifest.runId,
54852
+ nodeId: manifest.nodeId,
54853
+ state: "draining",
54854
+ plan: manifest.plan,
54855
+ startedAtMs: manifest.startedAtMs,
54856
+ deadlineAtMs: manifest.deadlineAtMs,
54857
+ finishedAtMs: null,
54858
+ endReason: null,
54859
+ error: null,
54860
+ moved: manifest.moved,
54861
+ staying: manifest.staying,
54862
+ contaminated: manifest.staying.length > 0 || manifest.nodeId === HUB_NODE_ID,
54863
+ progress: null,
54864
+ resultIds: []
54865
+ };
54866
+ }
54867
+ /** Pipeline cells whose requested steps did not all fire measured something else — dropped. */
54868
+ function publishableCells(cells, requestedSteps) {
54869
+ const unfired = /* @__PURE__ */ new Set();
54870
+ return {
54871
+ kept: cells.filter((cell) => {
54872
+ if (cell.phase !== "pipeline") return true;
54873
+ const missing = requestedSteps.filter((s) => !cell.stepsFired.includes(s));
54874
+ for (const s of missing) unfired.add(s);
54875
+ return missing.length === 0;
54876
+ }),
54877
+ unfired: [...unfired].toSorted()
54878
+ };
54879
+ }
54880
+ var NodeBenchmarkService = class {
54881
+ deps;
54882
+ active = null;
54883
+ modeNodeIdValue = null;
54884
+ lastRun = null;
54885
+ restoring = null;
54886
+ constructor(deps) {
54887
+ this.deps = deps;
54888
+ }
54889
+ now() {
54890
+ return this.deps.now?.() ?? Date.now();
54891
+ }
54892
+ sleep(ms) {
54893
+ if (this.deps.sleep) return this.deps.sleep(ms);
54894
+ return new Promise((resolve) => {
54895
+ setTimeout(resolve, ms).unref?.();
54896
+ });
54897
+ }
54898
+ /** The node the cluster is in benchmark mode for (null = none). Read by placement. */
54899
+ modeNodeId() {
54900
+ return this.modeNodeIdValue;
54901
+ }
54902
+ setMode(nodeId) {
54903
+ if (this.modeNodeIdValue === nodeId) return;
54904
+ this.modeNodeIdValue = nodeId;
54905
+ this.deps.logger.info(nodeId === null ? "benchmark mode off" : "benchmark mode on — node out of placement", { meta: { nodeId } });
54906
+ this.deps.onModeChanged?.(nodeId);
54907
+ }
54908
+ status() {
54909
+ const run = this.active?.run ?? this.lastRun ?? this.deps.persistence.listRuns()[0] ?? null;
54910
+ return {
54911
+ modeNodeId: this.modeNodeIdValue,
54912
+ run
54913
+ };
54914
+ }
54915
+ async preflight(planInput) {
54916
+ const plan = NodeBenchmarkPlanSchema.parse(planInput);
54917
+ const online = this.deps.isNodeOnline(plan.nodeId);
54918
+ const knownDeviceKeys = online ? await this.deps.listDeviceKeys(plan.nodeId) : [];
54919
+ const { moving, staying } = classifyCamerasForBenchmark(plan.nodeId, await this.deps.cameraFacts());
54920
+ const headroom = await this.deps.headroom(plan.nodeId);
54921
+ const refusals = nodeBenchmarkRefusals({
54922
+ plan,
54923
+ activeRunNodeId: this.active?.run.nodeId ?? this.modeNodeIdValue,
54924
+ online,
54925
+ knownDeviceKeys,
54926
+ sessionsNow: headroom.sessionsNow,
54927
+ otherNodesCapacitySessions: headroom.otherNodesCapacitySessions
54928
+ });
54929
+ return {
54930
+ nodeId: plan.nodeId,
54931
+ online,
54932
+ moving,
54933
+ staying,
54934
+ estimatedDurationMs: estimateNodeBenchmarkDurationMs(plan),
54935
+ budgetMs: plan.budgetMinutes * 6e4,
54936
+ phases: plan.phases,
54937
+ steps: plan.steps,
54938
+ devices: plan.devices,
54939
+ headroom,
54940
+ refusals,
54941
+ canStart: refusals.length === 0
54942
+ };
54943
+ }
54944
+ /** Start a run, or say why not (the preflight with its refusals). */
54945
+ async start(planInput) {
54946
+ const preflight = await this.preflight(planInput);
54947
+ if (!preflight.canStart || this.active !== null || this.modeNodeIdValue !== null) return {
54948
+ kind: "refused",
54949
+ preflight: preflight.canStart ? {
54950
+ ...preflight,
54951
+ canStart: false,
54952
+ refusals: [{
54953
+ kind: "benchmark-active",
54954
+ detail: "Another benchmark started first — one node at a time.",
54955
+ overridable: false
54956
+ }]
54957
+ } : preflight
54958
+ };
54959
+ const plan = NodeBenchmarkPlanSchema.parse(planInput);
54960
+ const startedAtMs = this.now();
54961
+ const manifest = {
54962
+ runId: this.deps.newRunId?.() ?? `nb-${startedAtMs.toString(36)}`,
54963
+ nodeId: plan.nodeId,
54964
+ startedAtMs,
54965
+ deadlineAtMs: startedAtMs + plan.budgetMinutes * 6e4,
54966
+ plan,
54967
+ moved: preflight.moving.map((c) => c.deviceId),
54968
+ staying: preflight.staying.map((c) => c.deviceId)
54969
+ };
54970
+ const run = toRun(manifest);
54971
+ this.active = {
54972
+ run,
54973
+ abortRequested: null
54974
+ };
54975
+ try {
54976
+ await this.deps.persistence.writeManifest(manifest);
54977
+ } catch (err) {
54978
+ this.active = null;
54979
+ throw new Error(`node benchmark: could not persist the restore manifest — nothing changed (${errMsg$2(err)})`, { cause: err });
54980
+ }
54981
+ this.setMode(plan.nodeId);
54982
+ this.deps.logger.info("node benchmark started", { meta: {
54983
+ runId: run.runId,
54984
+ nodeId: run.nodeId,
54985
+ moving: manifest.moved.length,
54986
+ staying: manifest.staying.length,
54987
+ deadlineAtMs: manifest.deadlineAtMs
54988
+ } });
54989
+ this.execute(manifest);
54990
+ return {
54991
+ kind: "started",
54992
+ runId: run.runId
54993
+ };
54994
+ }
54995
+ /** Ask the active run to stop; the restore follows. */
54996
+ abort(runId) {
54997
+ if (this.active === null || this.active.run.runId !== runId) return { aborted: false };
54998
+ this.active.abortRequested ??= "operator-abort";
54999
+ this.deps.logger.info("node benchmark abort requested", { meta: { runId } });
55000
+ return { aborted: true };
55001
+ }
55002
+ update(patch) {
55003
+ if (this.active === null) return;
55004
+ this.active = {
55005
+ ...this.active,
55006
+ run: {
55007
+ ...this.active.run,
55008
+ ...patch
55009
+ }
55010
+ };
55011
+ }
55012
+ stopReason(manifest) {
55013
+ if (this.active?.abortRequested) return this.active.abortRequested;
55014
+ if (this.now() >= manifest.deadlineAtMs) return "deadline";
55015
+ if (!this.deps.isNodeOnline(manifest.nodeId)) return "node-lost";
55016
+ return null;
55017
+ }
55018
+ async drainPhase(manifest) {
55019
+ this.update({ state: "draining" });
55020
+ const moved = new Set(manifest.moved);
55021
+ if (moved.size > 0) await this.deps.drain(moved);
55022
+ const timeout = this.deps.drainTimeoutMs ?? NODE_BENCHMARK_DEFAULTS.drainTimeoutSec * 1e3;
55023
+ const until = this.now() + timeout;
55024
+ const staying = new Set(manifest.staying);
55025
+ for (;;) {
55026
+ const stop = this.stopReason(manifest);
55027
+ if (stop !== null) return stop;
55028
+ const left = (await this.deps.camerasOnNode(manifest.nodeId)).filter((id) => !staying.has(id));
55029
+ if (left.length === 0) return null;
55030
+ if (this.now() >= until) {
55031
+ this.deps.logger.warn("node benchmark: drain timed out — cameras still on the node", { meta: {
55032
+ runId: manifest.runId,
55033
+ nodeId: manifest.nodeId,
55034
+ left
55035
+ } });
55036
+ return "drain-timeout";
55037
+ }
55038
+ await this.deps.drain(new Set(left));
55039
+ await this.sleep(this.deps.pollIntervalMs ?? DEFAULT_POLL_MS);
55040
+ }
55041
+ }
55042
+ async measurePhase(manifest) {
55043
+ this.update({ state: "measuring" });
55044
+ const { plan } = manifest;
55045
+ await this.deps.runner.start(manifest.nodeId, {
55046
+ runId: manifest.runId,
55047
+ devices: plan.devices,
55048
+ phases: plan.phases,
55049
+ steps: plan.steps,
55050
+ fpsPerStream: plan.fpsPerStream,
55051
+ deadlineAtMs: manifest.deadlineAtMs,
55052
+ keepDownloadedModels: plan.keepDownloadedModels
55053
+ });
55054
+ let last = null;
55055
+ let unknown = 0;
55056
+ const lostAfter = this.deps.runnerLostAfter ?? 3;
55057
+ for (;;) {
55058
+ await this.sleep(this.deps.pollIntervalMs ?? DEFAULT_POLL_MS);
55059
+ let status = null;
55060
+ try {
55061
+ status = await this.deps.runner.status(manifest.nodeId, manifest.runId);
55062
+ } catch (err) {
55063
+ this.deps.logger.debug("node benchmark: runner status read failed", { meta: {
55064
+ runId: manifest.runId,
55065
+ error: errMsg$2(err)
55066
+ } });
55067
+ }
55068
+ if (status === null) {
55069
+ unknown += 1;
55070
+ if (!this.deps.isNodeOnline(manifest.nodeId)) return {
55071
+ reason: "node-lost",
55072
+ status: last
55073
+ };
55074
+ if (unknown >= lostAfter) return {
55075
+ reason: "runner-lost",
55076
+ status: last
55077
+ };
55078
+ } else {
55079
+ unknown = 0;
55080
+ last = status;
55081
+ this.update({
55082
+ progress: status.progress,
55083
+ contaminated: this.active?.run.contaminated === true || status.activeCameraIds.length > 0
55084
+ });
55085
+ if (status.state === "done") return {
55086
+ reason: "completed",
55087
+ status
55088
+ };
55089
+ if (status.state === "failed") return {
55090
+ reason: "failed",
55091
+ status
55092
+ };
55093
+ if (status.state === "aborted") return {
55094
+ reason: this.active?.abortRequested ?? "deadline",
55095
+ status
55096
+ };
55097
+ }
55098
+ const stop = this.stopReason(manifest);
55099
+ if (stop !== null) {
55100
+ await this.deps.runner.abort(manifest.nodeId, manifest.runId).catch(() => void 0);
55101
+ return {
55102
+ reason: stop,
55103
+ status: await this.deps.runner.status(manifest.nodeId, manifest.runId).catch(() => null) ?? last
55104
+ };
55105
+ }
55106
+ }
55107
+ }
55108
+ async execute(manifest) {
55109
+ let reason = "failed";
55110
+ let status = null;
55111
+ let error = null;
55112
+ try {
55113
+ const drained = await this.drainPhase(manifest);
55114
+ if (drained !== null) reason = drained;
55115
+ else {
55116
+ const measured = await this.measurePhase(manifest);
55117
+ reason = measured.reason;
55118
+ status = measured.status;
55119
+ if (reason === "failed") error = status?.error ?? "the runner failed";
55120
+ }
55121
+ } catch (err) {
55122
+ reason = this.active?.abortRequested ?? "failed";
55123
+ error = errMsg$2(err);
55124
+ this.deps.logger.warn("node benchmark failed", { meta: {
55125
+ runId: manifest.runId,
55126
+ error
55127
+ } });
55128
+ await this.deps.runner.abort(manifest.nodeId, manifest.runId).catch(() => void 0);
55129
+ } finally {
55130
+ await this.restore(manifest, reason, status, error);
55131
+ }
55132
+ }
55133
+ /**
55134
+ * Leave benchmark mode and put the cluster back. Idempotent; every step is
55135
+ * attempted whatever the previous one did.
55136
+ */
55137
+ async restore(manifest, reason, status, error) {
55138
+ this.update({ state: "restoring" });
55139
+ this.setMode(null);
55140
+ const pinnedBack = new Set(manifest.moved);
55141
+ try {
55142
+ if (pinnedBack.size > 0) await this.deps.restorePins(pinnedBack);
55143
+ } catch (err) {
55144
+ this.deps.logger.warn("node benchmark: re-placing the moved cameras failed — the reconcile retries", { meta: {
55145
+ runId: manifest.runId,
55146
+ error: errMsg$2(err)
55147
+ } });
55148
+ }
55149
+ const finishedAtMs = this.now();
55150
+ const base = this.active?.run ?? toRun(manifest);
55151
+ const contaminated = base.contaminated || (status?.activeCameraIds.length ?? 0) > 0;
55152
+ const { kept, unfired } = publishableCells(status?.cells ?? [], manifest.plan.steps);
55153
+ const rows = kept.map((cell, i) => ({
55154
+ id: `${manifest.runId}:${i}`,
55155
+ runId: manifest.runId,
55156
+ nodeId: manifest.nodeId,
55157
+ cell,
55158
+ fpsPerStream: manifest.plan.fpsPerStream,
55159
+ fingerprint: status?.fingerprint ?? null,
55160
+ clip: status?.clip ?? null,
55161
+ contaminated,
55162
+ stayingCameras: manifest.staying,
55163
+ partialRun: reason !== "completed",
55164
+ measuredAtMs: finishedAtMs
55165
+ }));
55166
+ const notes = [error, unfired.length > 0 ? `Not stored: the pipeline measurement's steps ${unfired.join(", ")} never produced a result on the clip.` : null].filter((n) => n !== null);
55167
+ try {
55168
+ if (rows.length > 0) await this.deps.persistence.writeResults(rows);
55169
+ } catch (err) {
55170
+ notes.push(`Results not stored: ${errMsg$2(err)}`);
55171
+ }
55172
+ const run = {
55173
+ ...base,
55174
+ state: reason === "completed" ? "done" : reason === "failed" ? "failed" : "aborted",
55175
+ finishedAtMs,
55176
+ endReason: reason,
55177
+ error: notes.length > 0 ? notes.join(" ") : null,
55178
+ contaminated,
55179
+ resultIds: rows.map((r) => r.id)
55180
+ };
55181
+ try {
55182
+ await this.deps.persistence.recordRun(run);
55183
+ } catch (err) {
55184
+ this.deps.logger.warn("node benchmark: run record not stored", { meta: {
55185
+ runId: run.runId,
55186
+ error: errMsg$2(err)
55187
+ } });
55188
+ }
55189
+ await this.deps.persistence.clearManifest().catch(() => void 0);
55190
+ this.lastRun = run;
55191
+ this.active = null;
55192
+ this.deps.logger.info("node benchmark ended", { meta: {
55193
+ runId: run.runId,
55194
+ nodeId: run.nodeId,
55195
+ endReason: reason,
55196
+ results: rows.length,
55197
+ contaminated,
55198
+ error: run.error
55199
+ } });
55200
+ if (rows.length > 0) this.deps.onResultsWritten(manifest.nodeId);
55201
+ }
55202
+ /**
55203
+ * Registry-driven recovery: a manifest with no live run means the run died
55204
+ * with its process (hub restart) — restore now. A live run far past its
55205
+ * deadline is forced out of the mode. Called at boot and on every tick.
55206
+ */
55207
+ async reconcile(trigger) {
55208
+ if (this.restoring !== null) return this.restoring;
55209
+ const work = async () => {
55210
+ if (this.active !== null) {
55211
+ const { run } = this.active;
55212
+ if (this.now() > run.deadlineAtMs + DEADLINE_GRACE_MS && this.modeNodeIdValue !== null) {
55213
+ this.deps.logger.warn("node benchmark: past its deadline and still not restored — forcing benchmark mode off", { meta: {
55214
+ runId: run.runId,
55215
+ nodeId: run.nodeId
55216
+ } });
55217
+ this.active.abortRequested ??= "deadline";
55218
+ this.setMode(null);
55219
+ }
55220
+ return;
55221
+ }
55222
+ if (this.modeNodeIdValue !== null) this.setMode(null);
55223
+ const manifest = this.deps.persistence.readManifest();
55224
+ if (manifest === null) return;
55225
+ const known = this.deps.persistence.listRuns().find((r) => r.runId === manifest.runId);
55226
+ if (known !== void 0 && known.finishedAtMs !== null) {
55227
+ await this.deps.persistence.clearManifest().catch(() => void 0);
55228
+ return;
55229
+ }
55230
+ this.deps.logger.warn("node benchmark: restore owed from an interrupted run", { meta: {
55231
+ runId: manifest.runId,
55232
+ nodeId: manifest.nodeId,
55233
+ trigger
55234
+ } });
55235
+ await this.deps.runner.abort(manifest.nodeId, manifest.runId).catch(() => void 0);
55236
+ this.active = {
55237
+ run: toRun(manifest),
55238
+ abortRequested: "hub-restart"
55239
+ };
55240
+ await this.restore(manifest, trigger === "boot" ? "hub-restart" : "runner-lost", null, null);
55241
+ };
55242
+ this.restoring = work().finally(() => {
55243
+ this.restoring = null;
55244
+ });
55245
+ return this.restoring;
55246
+ }
55247
+ };
55248
+ //#endregion
55249
+ //#region src/node-benchmark/node-benchmark-wiring.ts
55250
+ /**
55251
+ * Binds the node benchmark service (D791) to the orchestrator's real
55252
+ * controllers: the placement ledger and topology for the camera facts, a
55253
+ * SCOPED rebalance for the drain and the restore, the shares for headroom,
55254
+ * and the target node's runner through `nodePin` — whichever runner the
55255
+ * operator's request first reached, the measurement runs on the node chosen.
55256
+ */
55257
+ function makeRunnerClient(ctx) {
55258
+ return {
55259
+ start: async (nodeId, input) => {
55260
+ await ctx().api.pipelineRunner.startNodeBench.mutate(input, nodePin(nodeId));
55261
+ },
55262
+ status: async (nodeId, runId) => ctx().api.pipelineRunner.getNodeBenchStatus.query({ runId }, nodePin(nodeId)),
55263
+ abort: async (nodeId, runId) => {
55264
+ await ctx().api.pipelineRunner.abortNodeBench.mutate({ runId }, nodePin(nodeId));
55265
+ }
55266
+ };
55267
+ }
55268
+ /** Every camera the orchestrator places, as the preflight reads it (benchmark ignored). */
55269
+ async function collectCameraFacts(deps) {
55270
+ const out = [];
55271
+ for (const deviceId of deps.ledger.listConfigIds()) out.push({
55272
+ deviceId,
55273
+ placedNodeId: deps.ledger.getAssignment(deviceId)?.agentNodeId ?? deps.activeSessionNode(deviceId),
55274
+ nodePin: await deps.readStoredPipelinePin(deviceId),
55275
+ frameSourceNodes: deps.topology.detectionEligibleNodesIgnoringBenchmark(deviceId)
55276
+ });
55277
+ return out;
55278
+ }
55279
+ function createNodeBenchmarkService(deps) {
55280
+ const logger = deps.ctx().logger;
55281
+ return new NodeBenchmarkService({
55282
+ logger,
55283
+ persistence: deps.persistence,
55284
+ runner: makeRunnerClient(deps.ctx),
55285
+ isNodeOnline: (nodeId) => deps.topology.collectRunnerNodeIds().includes(nodeId),
55286
+ listDeviceKeys: deps.listDeviceKeys,
55287
+ cameraFacts: () => collectCameraFacts(deps),
55288
+ headroom: async (nodeId) => {
55289
+ let other = null;
55290
+ try {
55291
+ other = (await deps.suggestShares()).nodes.filter((n) => n.nodeId !== nodeId && n.skippedReason === null).reduce((sum, n) => sum + n.capacitySessions, 0);
55292
+ } catch (err) {
55293
+ logger.debug("node benchmark: capacity read failed — headroom unknown", { meta: { error: errMsg$2(err) } });
55294
+ }
55295
+ return {
55296
+ sessionsNow: deps.sessionsNow(),
55297
+ otherNodesCapacitySessions: other
55298
+ };
55299
+ },
55300
+ drain: async (deviceIds) => {
55301
+ const result = await deps.rebalance({
55302
+ minImprovement: 0,
55303
+ onlyDeviceIds: deviceIds
55304
+ });
55305
+ logger.info("node benchmark: drain pass", { meta: {
55306
+ migrated: result.migrated,
55307
+ skipped: result.skipped.length
55308
+ } });
55309
+ },
55310
+ camerasOnNode: async (nodeId) => deps.ledger.listAssignments().filter((a) => a.agentNodeId === nodeId).map((a) => a.deviceId).concat(deps.ledger.listConfigIds().filter((id) => deps.ledger.getAssignment(id) === null && deps.activeSessionNode(id) === nodeId)),
55311
+ restorePins: async (deviceIds) => {
55312
+ const result = await deps.rebalance({
55313
+ minImprovement: 0,
55314
+ onlyDeviceIds: deviceIds
55315
+ });
55316
+ logger.info("node benchmark: cameras re-placed after the run", { meta: {
55317
+ migrated: result.migrated,
55318
+ skipped: result.skipped.length
55319
+ } });
55320
+ },
55321
+ onResultsWritten: deps.onResultsWritten,
55322
+ ...deps.pollIntervalMs !== void 0 ? { pollIntervalMs: deps.pollIntervalMs } : {}
55323
+ });
55324
+ }
55325
+ //#endregion
55326
+ //#region src/node-benchmark/node-benchmark-reads.ts
55327
+ /**
55328
+ * Read paths of the node benchmark (D791): the plan editor's candidates and
55329
+ * the results with their standing today. Pure over injected reads.
55330
+ */
55331
+ var DETECTOR_STEP_ID$1 = "object-detection";
55332
+ /** The family Auto tune picks from (D745) — the suggestions follow it. */
55333
+ var SUGGESTED_FAMILY = "yolov9";
55334
+ var MAX_SUGGESTED = 3;
55335
+ function detectorFormats(catalog) {
55336
+ const out = /* @__PURE__ */ new Map();
55337
+ for (const slot of catalog?.slots ?? []) for (const addon of slot.addons) {
55338
+ if (addon.id !== DETECTOR_STEP_ID$1) continue;
55339
+ for (const model of addon.models) {
55340
+ if (model.legacy === true) continue;
55341
+ out.set(model.id, MODEL_FORMATS.filter((f) => f in model.formats));
55342
+ }
55343
+ }
55344
+ return out;
55345
+ }
55346
+ /** Candidates per device: Auto tune's pick first, then the family by person recall. */
55347
+ async function readNodeBenchmarkCandidates(deps) {
55348
+ const devices = await deps.readDevices(deps.nodeId);
55349
+ const catalog = await deps.readCatalog(deps.nodeId).catch(() => null);
55350
+ let picks = {};
55351
+ try {
55352
+ picks = await deps.autoTunePicks(deps.nodeId);
55353
+ } catch (err) {
55354
+ deps.logger.debug("node benchmark candidates: Auto tune pick unavailable", { meta: {
55355
+ nodeId: deps.nodeId,
55356
+ error: errMsg$2(err)
55357
+ } });
55358
+ }
55359
+ const formats = detectorFormats(catalog);
55360
+ const family = Object.keys(DETECTOR_ACCURACY).filter((id) => id.startsWith(SUGGESTED_FAMILY)).toSorted((a, b) => (DETECTOR_ACCURACY[b]?.personRecall ?? 0) - (DETECTOR_ACCURACY[a]?.personRecall ?? 0) || a.localeCompare(b));
55361
+ return {
55362
+ nodeId: deps.nodeId,
55363
+ reachable: devices.reachable,
55364
+ devices: devices.devices.map((d) => {
55365
+ const runs = (id) => (formats.get(id) ?? []).includes(d.format);
55366
+ const all = [...formats.keys()].filter(runs).toSorted();
55367
+ const pick = picks[d.key];
55368
+ const ordered = [...pick !== void 0 && runs(pick) ? [pick] : [], ...family.filter((id) => runs(id) && id !== pick)];
55369
+ return {
55370
+ deviceKey: d.key,
55371
+ enabled: d.enabled,
55372
+ available: d.available,
55373
+ suggested: ordered.slice(0, MAX_SUGGESTED),
55374
+ all,
55375
+ currentModelId: d.steps?.[DETECTOR_STEP_ID$1]?.modelId ?? d.defaultModelId ?? null
55376
+ };
55377
+ }),
55378
+ steps: catalog === null ? [] : listBenchDetailSteps(catalog)
55379
+ };
55380
+ }
55381
+ /** Results filtered, newest first, each with its standing today. */
55382
+ function listNodeBenchmarkResultViews(rows, input, ctx) {
55383
+ const standings = resultStandings(rows, ctx);
55384
+ return rows.filter((r) => input.nodeId === void 0 || r.nodeId === input.nodeId).filter((r) => input.deviceKey === void 0 || r.cell.deviceKey === input.deviceKey).toSorted((a, b) => b.measuredAtMs - a.measuredAtMs).map((r) => ({
55385
+ ...r,
55386
+ standing: standings.get(r.id)?.standing ?? "stale",
55387
+ staleWhy: standings.get(r.id)?.staleWhy ?? null
55388
+ }));
55389
+ }
55390
+ //#endregion
52218
55391
  //#region src/cluster-model-choice.ts
52219
55392
  /**
52220
55393
  * The owner's answer to "which model did the operator choose for this
@@ -58764,258 +61937,6 @@ function buildRunnerConfig(base, overrides) {
58764
61937
  preRollInferenceFrames: config.preRollInferenceFrames
58765
61938
  };
58766
61939
  }
58767
- //#endregion
58768
- //#region src/durable/durable-ledger.ts
58769
- /** Default reseed cap — every current consumer's row set is installation-bounded. */
58770
- var DEFAULT_LOAD_LIMIT = 1e5;
58771
- var DurableLedger = class DurableLedger {
58772
- mirror = /* @__PURE__ */ new Map();
58773
- /** Whether ANY load has read the store successfully — see {@link hasAnswered}. */
58774
- answered = false;
58775
- spec;
58776
- store;
58777
- logger;
58778
- constructor(deps) {
58779
- this.spec = deps.spec;
58780
- this.store = deps.store;
58781
- this.logger = deps.logger;
58782
- }
58783
- /**
58784
- * Register the collection. MUST run at boot, before any read or write: the
58785
- * SQLite backend answers 412 for an undeclared collection and takes the whole
58786
- * runner down with it (the addon-ai boot-crash lesson).
58787
- */
58788
- static declare(store, spec) {
58789
- return store.declareCollection.mutate({
58790
- collection: spec.collection,
58791
- columns: [...spec.columns],
58792
- ...spec.indexes !== void 0 ? { indexes: [...spec.indexes] } : {}
58793
- });
58794
- }
58795
- declare() {
58796
- return DurableLedger.declare(this.store, this.spec);
58797
- }
58798
- /** The collection this ledger owns — for the caller's own log lines. */
58799
- get collection() {
58800
- return this.spec.collection;
58801
- }
58802
- /**
58803
- * Boot reseed. Replaces the mirror with what the store holds and returns the
58804
- * rows, so a caller that must hydrate something else (a watcher, a registry)
58805
- * gets them without a second read.
58806
- *
58807
- * **A failure returns what is already mirrored** rather than clearing it —
58808
- * see contract rule 2. The count is worth logging out loud at the call site:
58809
- * "loaded 0" after a container recreate is the one line that explains a
58810
- * silent flood.
58811
- */
58812
- async load() {
58813
- try {
58814
- const records = await this.store.query.query({
58815
- collection: this.spec.collection,
58816
- filter: { limit: this.spec.loadLimit ?? DEFAULT_LOAD_LIMIT }
58817
- });
58818
- const next = /* @__PURE__ */ new Map();
58819
- let skipped = 0;
58820
- for (const record of records) {
58821
- const row = this.spec.fromRecord(record.id, record.data);
58822
- if (row === null) {
58823
- skipped += 1;
58824
- continue;
58825
- }
58826
- next.set(this.spec.keyOf(row), row);
58827
- }
58828
- this.mirror.clear();
58829
- for (const [key, row] of next) this.mirror.set(key, row);
58830
- this.answered = true;
58831
- if (skipped > 0) this.logger.warn("durable rows skipped as malformed — they gate NOTHING", { meta: {
58832
- collection: this.spec.collection,
58833
- skipped
58834
- } });
58835
- return [...this.mirror.values()];
58836
- } catch (err) {
58837
- this.logger.warn("durable load failed — keeping the state already in memory", { meta: {
58838
- collection: this.spec.collection,
58839
- error: String(err),
58840
- held: this.mirror.size
58841
- } });
58842
- return [...this.mirror.values()];
58843
- }
58844
- }
58845
- /**
58846
- * Whether the mirror is an ANSWER from the store: true once any load has
58847
- * succeeded, and never false again (a later failed load keeps that answer).
58848
- * Before it, an empty mirror is "not read yet", never "the store is empty" —
58849
- * {@link load} swallows its failure, so a caller that must tell the two apart
58850
- * asks here (D642 review I1).
58851
- */
58852
- hasAnswered() {
58853
- return this.answered;
58854
- }
58855
- /** Every mirrored row, insertion-ordered. */
58856
- snapshot() {
58857
- return [...this.mirror.values()];
58858
- }
58859
- /** The row currently held for a key, if any. Pure RAM — never I/O. */
58860
- get(key) {
58861
- return this.mirror.get(key);
58862
- }
58863
- has(key) {
58864
- return this.mirror.has(key);
58865
- }
58866
- get size() {
58867
- return this.mirror.size;
58868
- }
58869
- /**
58870
- * Judge ONE observation against what the ledger already accepted, and advance
58871
- * it.
58872
- *
58873
- * Synchronous on purpose: the verdict is a function of the in-RAM mirror
58874
- * alone, so a decision can never be gated on an I/O that might fail (D49).
58875
- * The durable write is kicked off behind it and its failure changes no
58876
- * verdict.
58877
- *
58878
- * `no-flip` leaves the held row UNTOUCHED — including any timestamp it
58879
- * carries, which therefore means "when this key last CHANGED", not "when it
58880
- * last spoke". That is the timestamp anyone reading the table wants.
58881
- */
58882
- observe(row) {
58883
- const equalFact = this.spec.equalFact;
58884
- if (equalFact === void 0) throw new Error(`DurableLedger(${this.spec.collection}): observe() requires the spec to declare equalFact`);
58885
- const key = this.spec.keyOf(row);
58886
- const held = this.mirror.get(key);
58887
- if (held !== void 0 && equalFact(held, row)) return "no-flip";
58888
- this.mirror.set(key, row);
58889
- this.persist(row);
58890
- return held === void 0 ? "seeded" : "flip";
58891
- }
58892
- /**
58893
- * Upsert a row without a verdict — the write path for a ledger whose owner
58894
- * has already decided the value changed.
58895
- *
58896
- * The order of the mirror advance and the durable write is the spec's
58897
- * {@link DurableWriteMode}, not the call site's: two call sites that
58898
- * disagreed about it would be two different durability guarantees on one
58899
- * collection.
58900
- */
58901
- async put(row) {
58902
- const key = this.spec.keyOf(row);
58903
- if (this.spec.writeMode === "write-behind") {
58904
- this.mirror.set(key, row);
58905
- await this.persist(row);
58906
- return;
58907
- }
58908
- await this.store.set.mutate({
58909
- collection: this.spec.collection,
58910
- key,
58911
- value: this.spec.toValue(row)
58912
- });
58913
- this.mirror.set(key, row);
58914
- }
58915
- /**
58916
- * Advance the mirror WITHOUT persisting, for an owner that deliberately
58917
- * coalesces its writes.
58918
- *
58919
- * The stationary registry is the reason this exists: a parked car is
58920
- * re-confirmed on every processed frame (5–30 Hz), and persisting each
58921
- * confirmation would offer thousands of commits a day to the checkpoint
58922
- * lottery (D96) to maintain a handful of rows. It stages the advance and
58923
- * flushes on its 5-minute sweep — 288 writes a day instead of ~10⁶.
58924
- *
58925
- * **The cost is stated, not hidden**: a staged value that is never flushed
58926
- * is lost on a crash. An owner may only stage a field whose staleness its
58927
- * own TTL absorbs. Anything that GATES work must go through {@link put} or
58928
- * {@link observe}.
58929
- */
58930
- stage(row) {
58931
- this.mirror.set(this.spec.keyOf(row), row);
58932
- }
58933
- /**
58934
- * Drop a key from the MIRROR only — the durable row survives.
58935
- *
58936
- * What a scope-unbind needs: this process stops holding the value, and a
58937
- * rebind reloads it from the store. Deliberately distinct from
58938
- * {@link forget}, which deletes; conflating the two is how an unbind turns
58939
- * into a wipe.
58940
- */
58941
- evict(key) {
58942
- this.mirror.delete(key);
58943
- }
58944
- /**
58945
- * Drop one key, mirror and row. Best-effort on the durable half: a failed
58946
- * delete leaves a row that the next load will re-mirror, which is a stale
58947
- * value rather than a lost one.
58948
- */
58949
- async forget(key) {
58950
- this.mirror.delete(key);
58951
- try {
58952
- await this.store.delete.mutate({
58953
- collection: this.spec.collection,
58954
- key
58955
- });
58956
- } catch (err) {
58957
- this.logger.debug("durable delete failed", { meta: {
58958
- collection: this.spec.collection,
58959
- key,
58960
- error: String(err)
58961
- } });
58962
- }
58963
- }
58964
- /**
58965
- * Drop every mirrored key NOT in `activeKeys`. Returns how many rows went.
58966
- *
58967
- * **The caller must hold an AUTHORITATIVE active set.** A prune driven by a
58968
- * fallible read is work destroyed on an error (D49/D130) — that is why this
58969
- * is a method a feature opts into rather than a policy the primitive runs.
58970
- * Best-effort per row: a failed delete keeps the key (retried next prune)
58971
- * rather than aborting the sweep.
58972
- */
58973
- async pruneExcept(activeKeys) {
58974
- let pruned = 0;
58975
- for (const key of [...this.mirror.keys()]) {
58976
- if (activeKeys.has(key)) continue;
58977
- try {
58978
- await this.store.delete.mutate({
58979
- collection: this.spec.collection,
58980
- key
58981
- });
58982
- this.mirror.delete(key);
58983
- pruned += 1;
58984
- } catch (err) {
58985
- this.logger.debug("durable prune delete failed", { meta: {
58986
- collection: this.spec.collection,
58987
- key,
58988
- error: String(err)
58989
- } });
58990
- }
58991
- }
58992
- return pruned;
58993
- }
58994
- /**
58995
- * Write-behind durable upsert. Best-effort and logged, never thrown at the
58996
- * decision path: the mirror already holds the truth for this process, and the
58997
- * worst a lost write can do is one silent re-seed after the next restart.
58998
- */
58999
- async persist(row) {
59000
- const deviceId = this.spec.deviceIdOf?.(row);
59001
- try {
59002
- await this.store.set.mutate({
59003
- collection: this.spec.collection,
59004
- key: this.spec.keyOf(row),
59005
- value: this.spec.toValue(row)
59006
- });
59007
- } catch (err) {
59008
- this.logger.warn("durable persist failed — this key may re-seed on boot", {
59009
- ...deviceId !== void 0 ? { tags: { deviceId } } : {},
59010
- meta: {
59011
- collection: this.spec.collection,
59012
- key: this.spec.keyOf(row),
59013
- error: String(err)
59014
- }
59015
- });
59016
- }
59017
- }
59018
- };
59019
61940
  var DEVICE_FEATURES_SPEC = {
59020
61941
  collection: "pipeline-orchestrator:device-features",
59021
61942
  columns: [
@@ -61372,16 +64293,20 @@ var PlacementService = class {
61372
64293
  let migrated = 0;
61373
64294
  const skipped = [];
61374
64295
  for (const [deviceId, config] of this.deps.ledger.configEntries()) {
64296
+ if (opts?.onlyDeviceIds !== void 0 && !opts.onlyDeviceIds.has(deviceId)) continue;
61375
64297
  if (this.deps.isIdleSessionCamera(deviceId)) continue;
61376
64298
  const current = this.deps.ledger.getAssignment(deviceId);
61377
- if (current?.pinned) continue;
64299
+ const eligibleNodes = this.deps.topology.detectionEligibleNodes(deviceId);
64300
+ const benchNode = this.deps.benchmarkNodeId?.() ?? null;
64301
+ const pinnedOffBenchNode = current !== null && current.agentNodeId === benchNode && !eligibleNodes.includes(current.agentNodeId);
64302
+ if (current?.pinned && !pinnedOffBenchNode) continue;
61378
64303
  const preferredAgent = await this.deps.readPipelinePin(deviceId);
61379
64304
  const decision = balance({
61380
64305
  nodes: talliedLoads(),
61381
64306
  preferredAgent,
61382
64307
  nodeCaps,
61383
64308
  weights: nodeWeights,
61384
- eligibleNodes: this.deps.topology.detectionEligibleNodes(deviceId)
64309
+ eligibleNodes
61385
64310
  });
61386
64311
  if (!decision) continue;
61387
64312
  if (decision.kind === "pending") {
@@ -61389,7 +64314,7 @@ var PlacementService = class {
61389
64314
  continue;
61390
64315
  }
61391
64316
  if (current && current.agentNodeId === decision.agentNodeId) continue;
61392
- if (current && minImprovement > 0) {
64317
+ if (current && minImprovement > 0 && eligibleNodes.includes(current.agentNodeId)) {
61393
64318
  const tallied = talliedLoads();
61394
64319
  const currentLoad = tallied.find((l) => l.nodeId === current.agentNodeId);
61395
64320
  const targetLoad = tallied.find((l) => l.nodeId === decision.agentNodeId);
@@ -63313,7 +66238,17 @@ var NodeTopologyService = class {
63313
66238
  remoteSourcingNodes: this._remoteSourcingNodes
63314
66239
  });
63315
66240
  }
66241
+ /**
66242
+ * The nodes that may run this camera's detection. While the cluster is in
66243
+ * benchmark mode for a node (D791), that node is left out — unless it is
66244
+ * the camera's only frame source, in which case the camera stays (never
66245
+ * blind) and the run is flagged contaminated.
66246
+ */
63316
66247
  detectionEligibleNodes(deviceId) {
66248
+ return eligibleUnderBenchmark(this.detectionEligibleNodesIgnoringBenchmark(deviceId), this.deps.benchmarkNodeId?.() ?? null);
66249
+ }
66250
+ /** {@link detectionEligibleNodes} as if no benchmark ran — the camera's real candidates. */
66251
+ detectionEligibleNodesIgnoringBenchmark(deviceId) {
63317
66252
  const frameSource = this.frameSourceNodes(deviceId);
63318
66253
  return this._enabledNodes.filter((nodeId) => frameSource.includes(nodeId));
63319
66254
  }
@@ -68882,7 +71817,8 @@ async function buildOrchestratorControllers(deps) {
68882
71817
  eventBus: deps.ctx().eventBus,
68883
71818
  logger: deps.ctx().logger,
68884
71819
  localNodeId: () => localNodeId,
68885
- addonId: () => deps.ctx().id
71820
+ addonId: () => deps.ctx().id,
71821
+ benchmarkNodeId: () => deps.benchmarkNodeId?.() ?? null
68886
71822
  });
68887
71823
  deps.setTopology(topology);
68888
71824
  let motionSliceGate = null;
@@ -69054,6 +71990,7 @@ async function buildOrchestratorControllers(deps) {
69054
71990
  return selection.kind === "selected" ? selection.camStreamId : null;
69055
71991
  };
69056
71992
  const placement = new PlacementService({
71993
+ benchmarkNodeId: () => deps.benchmarkNodeId?.() ?? null,
69057
71994
  ctx: () => deps.ctx(),
69058
71995
  ctxIfReady: () => deps.ctxIfReady(),
69059
71996
  ledger,
@@ -70289,6 +73226,9 @@ var pipelineOrchestratorWidgets = [{
70289
73226
  * is gone the node's recommended model runs.
70290
73227
  * - never: `stepToggles`, devices' enablement, pins of
70291
73228
  * embedding steps (cluster-scoped, changing one forces a re-index).
73229
+ * - never anything on a KEPT (node, device) — a custom-registry detector pin,
73230
+ * or an accelerator of its group — and never a camera's detector override
73231
+ * that pins a custom model (a canary), with its thresholds (D786).
70292
73232
  */
70293
73233
  /** The root detector step. */
70294
73234
  var DETECTOR_STEP_ID = "object-detection";
@@ -70296,6 +73236,17 @@ var DETECTOR_STEP_ID = "object-detection";
70296
73236
  function isModelPinKept(stepId) {
70297
73237
  return stepId.endsWith("-embedding");
70298
73238
  }
73239
+ /** The `keptDevices` key of one (node, device). */
73240
+ function keptDeviceKey(nodeId, deviceKey) {
73241
+ return `${nodeId}|${deviceKey}`;
73242
+ }
73243
+ function isKeptDevice(input, nodeId, deviceKey) {
73244
+ return input.keptDevices?.has(keptDeviceKey(nodeId, deviceKey)) === true;
73245
+ }
73246
+ /** A root-detector entry that pins a custom model — a canary, kept whole. */
73247
+ function isCustomDetectorPin(input, stepId, modelId) {
73248
+ return stepId === "object-detection" && modelId !== void 0 && input.isCustomModel?.(modelId) === true;
73249
+ }
70299
73250
  function boundKeysFor(input, stepId) {
70300
73251
  return input.boundKeysByStep[stepId] ?? [];
70301
73252
  }
@@ -70355,6 +73306,10 @@ function applyNodeAutoTune(blob, input) {
70355
73306
  const nextDevices = {};
70356
73307
  for (const deviceKey of deviceKeys) {
70357
73308
  const device = stored[deviceKey];
73309
+ if (isKeptDevice(input, nodeId, deviceKey)) {
73310
+ if (device !== void 0) nextDevices[deviceKey] = device;
73311
+ continue;
73312
+ }
70358
73313
  const target = targets.get(deviceKey);
70359
73314
  const steps = { ...device?.steps };
70360
73315
  if (target !== void 0 && steps["object-detection"] === void 0) steps[DETECTOR_STEP_ID] = {};
@@ -70399,6 +73354,7 @@ function applyNodeAutoTune(blob, input) {
70399
73354
  /** Scrub a wholesale snapshot tree; collects one clear per changed step. */
70400
73355
  function scrubTree(steps, input, onClear) {
70401
73356
  return steps.map((step) => {
73357
+ if (isCustomDetectorPin(input, step.addonId, step.modelId)) return step;
70402
73358
  const children = step.children ? scrubTree(step.children, input, onClear) : void 0;
70403
73359
  const inScope = step.addonId === "object-detection" || pinInScope(step.addonId);
70404
73360
  const { kept, removed } = stripBound(step.settings, boundKeysFor(input, step.addonId));
@@ -70432,8 +73388,16 @@ function applyCameraAutoTune(blob, input) {
70432
73388
  }
70433
73389
  const nextByDevice = {};
70434
73390
  for (const [deviceKey, byStep] of Object.entries(byDevice)) {
73391
+ if (isKeptDevice(input, nodeId, deviceKey)) {
73392
+ nextByDevice[deviceKey] = byStep;
73393
+ continue;
73394
+ }
70435
73395
  const nextByStep = {};
70436
73396
  for (const [stepId, patch] of Object.entries(byStep)) {
73397
+ if (isCustomDetectorPin(input, stepId, patch.modelId)) {
73398
+ nextByStep[stepId] = patch;
73399
+ continue;
73400
+ }
70437
73401
  const inScope = stepId === "object-detection" || pinInScope(stepId);
70438
73402
  const { kept, removed } = stripBound(patch.settings, boundKeysFor(input, stepId));
70439
73403
  const dropPin = inScope && patch.modelId !== void 0;
@@ -70522,6 +73486,13 @@ function applyCameraAutoTune(blob, input) {
70522
73486
  * 5. re-dispatch the cameras of every touched node.
70523
73487
  * A failure after phase 2 leaves "overrides cleared, previous node model",
70524
73488
  * which is a valid configuration (the defaults run); re-running converges.
73489
+ *
73490
+ * Custom models are never overwritten (D786): a (node, device) whose root
73491
+ * detector is pinned to a custom-registry model is KEPT by the recommender, and
73492
+ * the diff writes nothing there; a camera override pinning one (a canary) is
73493
+ * kept with its thresholds, and its per-camera detector thresholds are not
73494
+ * cleared. The plan warns about each. A registry that does not answer keeps
73495
+ * every pin outside the measured catalog — unknown is never "not custom".
70525
73496
  */
70526
73497
  function minSessionsOf(input) {
70527
73498
  return input.minSessionsPerDevice ?? 2;
@@ -70554,6 +73525,46 @@ function boundKeysOf(catalogs) {
70554
73525
  }
70555
73526
  return Object.fromEntries([...byStep].map(([step, set]) => [step, [...set].toSorted()]));
70556
73527
  }
73528
+ /** `node|deviceKey → custom model` of every stored root-detector pin to a custom model. */
73529
+ function storedCustomPins(agent, isCustomModel) {
73530
+ const out = /* @__PURE__ */ new Map();
73531
+ for (const [nodeId, entry] of Object.entries(agent)) for (const [deviceKey, device] of Object.entries(entry?.inferenceDevices ?? {})) {
73532
+ const modelId = device?.steps?.[DETECTOR_STEP_ID]?.modelId;
73533
+ if (modelId !== void 0 && isCustomModel(modelId)) out.set(keptDeviceKey(nodeId, deviceKey), modelId);
73534
+ }
73535
+ return out;
73536
+ }
73537
+ /** Every camera override that pins a custom root detector (a canary). */
73538
+ function cameraCustomPins(cameras, isCustomModel) {
73539
+ const out = [];
73540
+ for (const [key, camera] of Object.entries(cameras)) {
73541
+ const deviceId = Number(key);
73542
+ for (const [nodeId, byDevice] of Object.entries(camera?.stepOverridesByDevice ?? {})) for (const [deviceKey, byStep] of Object.entries(byDevice)) {
73543
+ const modelId = byStep[DETECTOR_STEP_ID]?.modelId;
73544
+ if (modelId !== void 0 && isCustomModel(modelId)) out.push({
73545
+ deviceId,
73546
+ nodeId,
73547
+ deviceKey,
73548
+ modelId
73549
+ });
73550
+ }
73551
+ for (const [nodeId, snapshot] of Object.entries(camera?.pipelineByAgent ?? {})) {
73552
+ const walk = (steps) => {
73553
+ for (const step of steps) {
73554
+ if (step.addonId === "object-detection" && step.modelId !== void 0 && isCustomModel(step.modelId)) out.push({
73555
+ deviceId,
73556
+ nodeId,
73557
+ deviceKey: null,
73558
+ modelId: step.modelId
73559
+ });
73560
+ if (step.children) walk(step.children);
73561
+ }
73562
+ };
73563
+ walk(snapshot.steps);
73564
+ }
73565
+ }
73566
+ return out.toSorted((a, b) => a.deviceId - b.deviceId || a.nodeId.localeCompare(b.nodeId));
73567
+ }
70557
73568
  var AutoTuneService = class {
70558
73569
  deps;
70559
73570
  applying = false;
@@ -70591,8 +73602,13 @@ var AutoTuneService = class {
70591
73602
  minSessionsPerDevice
70592
73603
  });
70593
73604
  }
70594
- /** Gather every node and recommend. Nothing but hardware, catalog and the sizing goes in. */
70595
- async recommend(headroom, minSessionsPerDevice) {
73605
+ /**
73606
+ * Gather every node and recommend. Nothing but hardware, catalog, the sizing
73607
+ * and — for a plan — the custom pins goes in. `customPins` maps
73608
+ * `node|deviceKey` to the custom model pinned there; shares pass none
73609
+ * (capacity is a property of the hardware, not of what it runs).
73610
+ */
73611
+ async recommend(headroom, minSessionsPerDevice, customPins = /* @__PURE__ */ new Map()) {
70596
73612
  const nodeIds = [...new Set(["hub", ...this.deps.runnerNodeIds()])].toSorted();
70597
73613
  const gathered = await Promise.all(nodeIds.map(async (nodeId) => {
70598
73614
  const [devices, hardware, catalog] = await Promise.all([
@@ -70611,30 +73627,49 @@ var AutoTuneService = class {
70611
73627
  nodeId: g.nodeId,
70612
73628
  reachable: g.devices.reachable && g.catalog !== null,
70613
73629
  hardware: g.hardware,
70614
- devices: g.devices.devices.map((d) => ({
70615
- key: d.key,
70616
- format: d.format,
70617
- enabled: d.enabled,
70618
- available: d.available
70619
- })),
73630
+ devices: g.devices.devices.map((d) => {
73631
+ const pinned = customPins.get(keptDeviceKey(g.nodeId, d.key));
73632
+ return {
73633
+ key: d.key,
73634
+ format: d.format,
73635
+ enabled: d.enabled,
73636
+ available: d.available,
73637
+ ...pinned !== void 0 ? { customPinModelId: pinned } : {}
73638
+ };
73639
+ }),
70620
73640
  formatsByModel: formatsByModelOf(g.catalog)
70621
73641
  }));
70622
73642
  const observedFps = await this.deps.cameraFps();
70623
73643
  const cameraFps = observedFps !== null && observedFps > 0 ? observedFps : 10;
73644
+ let measured = /* @__PURE__ */ new Map();
73645
+ try {
73646
+ measured = await this.deps.measuredLatency?.(nodeIds) ?? /* @__PURE__ */ new Map();
73647
+ } catch (err) {
73648
+ this.deps.logger.warn("auto tune: node benchmark results unreadable — the table decides", { meta: { error: errMsg(err) } });
73649
+ }
70624
73650
  return {
70625
73651
  gathered,
70626
73652
  nodes,
70627
- recommendations: nodes.map((node) => recommendNode({
70628
- node,
70629
- headroom,
70630
- minSessionsPerDevice,
70631
- cameraFps
70632
- })),
73653
+ recommendations: nodes.map((node) => {
73654
+ const own = measured.get(node.nodeId);
73655
+ return recommendNode({
73656
+ node,
73657
+ headroom,
73658
+ minSessionsPerDevice,
73659
+ cameraFps,
73660
+ ...own !== void 0 ? { measured: own } : {}
73661
+ });
73662
+ }),
70633
73663
  cameraFps
70634
73664
  };
70635
73665
  }
70636
73666
  async computePlan(headroom, minSessionsPerDevice) {
70637
- const { gathered, nodes, recommendations, cameraFps } = await this.recommend(headroom, minSessionsPerDevice);
73667
+ const warnings = [];
73668
+ const isCustomModel = await this.customModelPredicate(warnings);
73669
+ const agentSettings = await this.deps.readAgentSettings();
73670
+ const cameraSettings = await this.deps.readCameraSettings();
73671
+ const customPins = storedCustomPins(agentSettings, isCustomModel);
73672
+ const { gathered, nodes, recommendations, cameraFps } = await this.recommend(headroom, minSessionsPerDevice, customPins);
70638
73673
  const touchedNodes = new Set(recommendations.filter((r) => !r.skipped).map((r) => r.nodeId));
70639
73674
  const targets = /* @__PURE__ */ new Map();
70640
73675
  const formats = /* @__PURE__ */ new Map();
@@ -70656,18 +73691,40 @@ var AutoTuneService = class {
70656
73691
  targets.set(rec.nodeId, list);
70657
73692
  formats.set(rec.nodeId, byKey);
70658
73693
  }
73694
+ const keptDevices = new Set(customPins.keys());
73695
+ for (const rec of recommendations) for (const d of rec.devices) if (d.kept !== void 0) keptDevices.add(keptDeviceKey(rec.nodeId, d.deviceKey));
70659
73696
  const diffInput = {
70660
73697
  touchedNodes,
70661
73698
  targets,
70662
- boundKeysByStep: boundKeysOf(gathered.map((g) => g.catalog))
73699
+ boundKeysByStep: boundKeysOf(gathered.map((g) => g.catalog)),
73700
+ keptDevices,
73701
+ isCustomModel
70663
73702
  };
70664
- const nodeChanges = applyNodeAutoTune(await this.deps.readAgentSettings(), diffInput).changes;
70665
- const cameraClears = applyCameraAutoTune(await this.deps.readCameraSettings(), diffInput).clears;
70666
- const warnings = [];
73703
+ const nodeChanges = applyNodeAutoTune(agentSettings, diffInput).changes;
73704
+ const cameraClears = applyCameraAutoTune(cameraSettings, diffInput).clears;
73705
+ const canaries = cameraCustomPins(cameraSettings, isCustomModel);
73706
+ for (const [key, modelId] of [...customPins].toSorted(([a], [b]) => a.localeCompare(b))) {
73707
+ const [nodeId = "", deviceKey = ""] = key.split("|");
73708
+ warnings.push({
73709
+ kind: "custom-pin-kept",
73710
+ nodeId,
73711
+ deviceKey,
73712
+ deviceId: null,
73713
+ modelId
73714
+ });
73715
+ }
73716
+ for (const c of canaries) warnings.push({
73717
+ kind: "custom-pin-kept",
73718
+ nodeId: c.nodeId,
73719
+ deviceKey: c.deviceKey,
73720
+ deviceId: c.deviceId,
73721
+ modelId: c.modelId
73722
+ });
73723
+ const canaryCameras = new Set(canaries.map((c) => c.deviceId));
70667
73724
  let detectorStoreClears = [];
70668
73725
  if (touchedNodes.size > 0) try {
70669
73726
  const answer = await this.deps.detectorStore({ dryRun: true });
70670
- detectorStoreClears = answer.entries.map((e) => ({
73727
+ detectorStoreClears = answer.entries.filter((e) => !canaryCameras.has(e.deviceId)).map((e) => ({
70671
73728
  deviceId: e.deviceId,
70672
73729
  keys: [...e.keys]
70673
73730
  }));
@@ -70701,7 +73758,11 @@ var AutoTuneService = class {
70701
73758
  picks: recommendations.map((r) => ({
70702
73759
  nodeId: r.nodeId,
70703
73760
  skipped: r.skipped,
70704
- devices: r.devices.map((d) => [d.deviceKey, d.chosen?.modelId ?? null])
73761
+ devices: r.devices.map((d) => [
73762
+ d.deviceKey,
73763
+ d.chosen?.modelId ?? null,
73764
+ d.kept ?? null
73765
+ ])
70705
73766
  })),
70706
73767
  nodeChanges,
70707
73768
  cameraClears,
@@ -70722,6 +73783,25 @@ var AutoTuneService = class {
70722
73783
  formats
70723
73784
  };
70724
73785
  }
73786
+ /**
73787
+ * "Is this model custom?" from the registry. A registry that does not
73788
+ * answer is NOT an empty registry: every pin outside the measured catalog
73789
+ * is then treated as custom, and the plan says so.
73790
+ */
73791
+ async customModelPredicate(warnings) {
73792
+ try {
73793
+ const ids = await this.deps.readCustomModelIds();
73794
+ return (modelId) => ids.has(modelId);
73795
+ } catch (err) {
73796
+ const error = errMsg(err);
73797
+ this.deps.logger.warn("auto tune: custom-model registry unreadable — every pin outside the measured catalog is kept", { meta: { error } });
73798
+ warnings.push({
73799
+ kind: "custom-registry-unreadable",
73800
+ error
73801
+ });
73802
+ return (modelId) => !(modelId in DETECTOR_ACCURACY);
73803
+ }
73804
+ }
70725
73805
  async apply(input) {
70726
73806
  if (this.applying) return {
70727
73807
  kind: "refused",
@@ -71340,6 +74420,12 @@ var PipelineOrchestratorAddon = class PipelineOrchestratorAddon extends BaseAddo
71340
74420
  readShareSource = () => DEFAULT_SHARE_SOURCE;
71341
74421
  autoTuneService = null;
71342
74422
  effectiveSharesService = null;
74423
+ /** The node benchmark lifecycle (D791); null before init. */
74424
+ nodeBenchmark = null;
74425
+ nodeBenchmarkStore = null;
74426
+ nodeBenchmarkTimer = null;
74427
+ /** How often the active run's runner is polled (a test seam). */
74428
+ nodeBenchmarkPollMs = 3e3;
71343
74429
  /**
71344
74430
  * Single owner of the shared placement state (`assignments`/`cameraConfigs`/
71345
74431
  * `pendingReasons`/`dispatchingDevices`) — see `placement-ledger.ts` (S6
@@ -71617,6 +74703,7 @@ var PipelineOrchestratorAddon = class PipelineOrchestratorAddon extends BaseAddo
71617
74703
  rebalance: (opts) => this.rebalance(opts),
71618
74704
  setCameraPipelineForAgent: (input) => this.setCameraPipelineForAgent(input),
71619
74705
  maintenancePaused: () => this.storageMigrationLeaseId !== null,
74706
+ benchmarkNodeId: () => this.nodeBenchmark?.modeNodeId() ?? null,
71620
74707
  placementStats: this.placementStats,
71621
74708
  setTopology: (topology) => {
71622
74709
  this.topology = topology;
@@ -71666,6 +74753,7 @@ var PipelineOrchestratorAddon = class PipelineOrchestratorAddon extends BaseAddo
71666
74753
  this.autoRebalanceTimer = controllers.autoRebalanceTimer;
71667
74754
  this.nodeStressLts = controllers.nodeStressLts;
71668
74755
  this.nodeStressLtsTimer = controllers.nodeStressLtsTimer;
74756
+ await this.initNodeBenchmark();
71669
74757
  const widgetsProvider = { listWidgets: async () => pipelineOrchestratorWidgets };
71670
74758
  this.clusterModelChoiceReady = true;
71671
74759
  return {
@@ -71752,6 +74840,10 @@ var PipelineOrchestratorAddon = class PipelineOrchestratorAddon extends BaseAddo
71752
74840
  clearInterval(this.nodeStressLtsTimer);
71753
74841
  this.nodeStressLtsTimer = null;
71754
74842
  }
74843
+ if (this.nodeBenchmarkTimer !== null) {
74844
+ clearInterval(this.nodeBenchmarkTimer);
74845
+ this.nodeBenchmarkTimer = null;
74846
+ }
71755
74847
  this.nodeStressLts = null;
71756
74848
  this.unsubOrchestratorSubscriptions?.();
71757
74849
  this.unsubOrchestratorSubscriptions = null;
@@ -71977,6 +75069,10 @@ var PipelineOrchestratorAddon = class PipelineOrchestratorAddon extends BaseAddo
71977
75069
  * node id, or `null` when neither key holds a pin (auto-balance).
71978
75070
  */
71979
75071
  async readPipelinePin(deviceId) {
75072
+ return pinUnderBenchmark(await this.readStoredPipelinePin(deviceId), this.nodeBenchmark?.modeNodeId() ?? null, this.topology?.detectionEligibleNodesIgnoringBenchmark(deviceId) ?? []);
75073
+ }
75074
+ /** The stored node pin as written — see {@link readPipelinePin} for the placement view. */
75075
+ async readStoredPipelinePin(deviceId) {
71980
75076
  if (!this.ctx?.settings) return null;
71981
75077
  try {
71982
75078
  const settings = await this.ctx.settings.readDeviceStore(deviceId);
@@ -72740,7 +75836,8 @@ var PipelineOrchestratorAddon = class PipelineOrchestratorAddon extends BaseAddo
72740
75836
  rewriteAgentSettings: (transform) => store.rewriteAgentSettingsMap(transform),
72741
75837
  rewriteCameraSettings: (transform) => store.rewriteCameraSettingsMap(transform),
72742
75838
  cameraFps: async () => this.autoTuneCameraFps(),
72743
- measuredCapacity: NO_MEASURED_CAPACITY,
75839
+ measuredCapacity: { readMeasuredCapacity: (nodeIds) => this.readMeasuredCapacity(nodeIds) },
75840
+ measuredLatency: (nodeIds) => this.readMeasuredLatency(nodeIds),
72744
75841
  detectorStore: async (input) => {
72745
75842
  const api = this.ctx.api;
72746
75843
  if (!api) throw new Error("ctx.api unavailable");
@@ -72759,6 +75856,12 @@ var PipelineOrchestratorAddon = class PipelineOrchestratorAddon extends BaseAddo
72759
75856
  format
72760
75857
  }, nodePin(nodeId));
72761
75858
  },
75859
+ readCustomModelIds: async () => {
75860
+ const api = this.ctx.api;
75861
+ if (!api) throw new Error("ctx.api unavailable");
75862
+ const models = await api.customModelRegistry.listModels.query();
75863
+ return new Set(models.map((m) => m.entry.id));
75864
+ },
72762
75865
  invalidateCatalog: () => store.invalidateCatalog(),
72763
75866
  redispatch: (nodeId) => this.placement.redispatchCamerasOnAgent(nodeId),
72764
75867
  now: () => Date.now()
@@ -72867,6 +75970,166 @@ var PipelineOrchestratorAddon = class PipelineOrchestratorAddon extends BaseAddo
72867
75970
  } });
72868
75971
  });
72869
75972
  }
75973
+ /** The benchmark reconcile cadence — a restore owed is never older than this. */
75974
+ static NODE_BENCHMARK_RECONCILE_MS = 3e4;
75975
+ /** How long a node's answered pool tuning is reused for result standings. */
75976
+ static CURRENT_POOL_TTL_MS = 6e4;
75977
+ currentPoolCache = /* @__PURE__ */ new Map();
75978
+ /**
75979
+ * Build the benchmark lifecycle, then reconcile its manifest: a run that
75980
+ * died with the previous process is restored NOW, before anything else
75981
+ * can trust the cluster's placement. Never fails the boot.
75982
+ */
75983
+ async initNodeBenchmark() {
75984
+ const store = new NodeBenchmarkStore({
75985
+ store: this.ctx.api.settingsStore,
75986
+ logger: this.ctx.logger
75987
+ });
75988
+ try {
75989
+ await NodeBenchmarkStore.declare(this.ctx.api.settingsStore);
75990
+ await store.load();
75991
+ } catch (err) {
75992
+ this.ctx.logger.warn("node benchmark: store unavailable at boot — runs refused until it answers", { meta: { error: errMsg$2(err) } });
75993
+ }
75994
+ this.nodeBenchmarkStore = store;
75995
+ this.nodeBenchmark = createNodeBenchmarkService({
75996
+ ctx: () => this.ctx,
75997
+ persistence: store,
75998
+ ledger: this.ledger,
75999
+ topology: this.topology,
76000
+ rebalance: (opts) => this.placement.rebalance(opts),
76001
+ activeSessionNode: (deviceId) => this.session.activeSessionNode(deviceId),
76002
+ readStoredPipelinePin: (deviceId) => this.readStoredPipelinePin(deviceId),
76003
+ listDeviceKeys: async (nodeId) => (await this.getNodeInferenceDevices({ nodeId })).devices.map((d) => d.key),
76004
+ sessionsNow: () => this.placementStats.snapshot(6e4).sessionsNow,
76005
+ suggestShares: () => this.autoTune().suggestShares({
76006
+ headroom: AUTO_SHARE_HEADROOM,
76007
+ source: "benchmark"
76008
+ }),
76009
+ pollIntervalMs: this.nodeBenchmarkPollMs,
76010
+ onResultsWritten: (nodeId) => {
76011
+ this.currentPoolCache.clear();
76012
+ this.noteShareChange(`benchmark-results:${nodeId}`);
76013
+ }
76014
+ });
76015
+ await this.nodeBenchmark.reconcile("boot").catch((err) => {
76016
+ this.ctx.logger.warn("node benchmark: boot reconcile failed — retried on the next tick", { meta: { error: errMsg$2(err) } });
76017
+ });
76018
+ this.nodeBenchmarkTimer = setInterval(() => {
76019
+ this.nodeBenchmark?.reconcile("tick").catch((err) => {
76020
+ this.ctx.logger.debug("node benchmark: reconcile tick failed", { meta: { error: errMsg$2(err) } });
76021
+ });
76022
+ }, PipelineOrchestratorAddon.NODE_BENCHMARK_RECONCILE_MS);
76023
+ this.nodeBenchmarkTimer.unref?.();
76024
+ }
76025
+ benchmarkService() {
76026
+ if (this.nodeBenchmark === null) throw new Error("node benchmark: not initialised");
76027
+ return this.nodeBenchmark;
76028
+ }
76029
+ async getNodeBenchmarkPreflight(input) {
76030
+ return this.benchmarkService().preflight(input);
76031
+ }
76032
+ async startNodeBenchmark(input) {
76033
+ return this.benchmarkService().start(input);
76034
+ }
76035
+ async getNodeBenchmarkStatus() {
76036
+ return this.benchmarkService().status();
76037
+ }
76038
+ async abortNodeBenchmark(input) {
76039
+ return this.benchmarkService().abort(input.runId);
76040
+ }
76041
+ async getNodeBenchmarkCandidates(input) {
76042
+ return readNodeBenchmarkCandidates({
76043
+ nodeId: input.nodeId,
76044
+ readDevices: (nodeId) => this.getNodeInferenceDevices({ nodeId }),
76045
+ readCatalog: (nodeId) => this.settingsStore.readCatalogForNode(nodeId),
76046
+ autoTunePicks: async (nodeId) => {
76047
+ const node = (await this.autoTune().plan({ headroom: AUTO_SHARE_HEADROOM })).nodes.find((n) => n.nodeId === nodeId);
76048
+ const out = {};
76049
+ for (const d of node?.devices ?? []) if (d.chosen !== null) out[d.deviceKey] = d.chosen.modelId;
76050
+ return out;
76051
+ },
76052
+ logger: this.ctx.logger
76053
+ });
76054
+ }
76055
+ async listNodeBenchmarkResults(input) {
76056
+ const rows = this.nodeBenchmarkStore?.listResults() ?? [];
76057
+ return listNodeBenchmarkResultViews(rows, input, {
76058
+ nowMs: Date.now(),
76059
+ currentPools: await this.readCurrentPools(rows)
76060
+ });
76061
+ }
76062
+ async deleteNodeBenchmarkResult(input) {
76063
+ const deleted = await this.nodeBenchmarkStore?.deleteResult(input.id) ?? false;
76064
+ if (deleted) {
76065
+ this.ctx.logger.info("node benchmark result deleted", { meta: { id: input.id } });
76066
+ this.noteShareChange("benchmark-result-deleted");
76067
+ }
76068
+ return { deleted };
76069
+ }
76070
+ /**
76071
+ * The pool each (node, device) of `rows` runs today, for the fingerprint
76072
+ * check. Unanswered = unknown, which never makes a result stale.
76073
+ */
76074
+ async readCurrentPools(rows) {
76075
+ const out = {};
76076
+ const api = this.ctx.api;
76077
+ const pairs = new Set(rows.map((r) => `${r.nodeId}\u0000${r.cell.deviceKey}`));
76078
+ const now = Date.now();
76079
+ await Promise.all([...pairs].map(async (key) => {
76080
+ const [nodeId = "", deviceKey = ""] = key.split("\0");
76081
+ const hit = this.currentPoolCache.get(key);
76082
+ let pool;
76083
+ if (hit !== void 0 && now - hit.at < PipelineOrchestratorAddon.CURRENT_POOL_TTL_MS) pool = hit.pool;
76084
+ else {
76085
+ pool = null;
76086
+ if (this.topology?.collectRunnerNodeIds().includes(nodeId)) try {
76087
+ const t = await api.pipelineExecutor.getEffectiveTuning.query({ deviceKey }, nodePin(nodeId));
76088
+ pool = {
76089
+ runtime: t.runtime,
76090
+ numWorkers: t.numWorkers,
76091
+ concurrency: t.concurrency,
76092
+ batchMode: t.batchMode
76093
+ };
76094
+ } catch {
76095
+ pool = null;
76096
+ }
76097
+ this.currentPoolCache.set(key, {
76098
+ at: now,
76099
+ pool
76100
+ });
76101
+ }
76102
+ if (pool !== null) out[nodeId] = {
76103
+ ...out[nodeId],
76104
+ [deviceKey]: pool
76105
+ };
76106
+ }));
76107
+ return out;
76108
+ }
76109
+ /** Each node's fresh detector measurements — what Auto tune lays over the table. */
76110
+ async readMeasuredLatency(nodeIds) {
76111
+ const rows = this.nodeBenchmarkStore?.listResults() ?? [];
76112
+ const out = /* @__PURE__ */ new Map();
76113
+ if (rows.length === 0) return out;
76114
+ const ctx = {
76115
+ nowMs: Date.now(),
76116
+ currentPools: await this.readCurrentPools(rows)
76117
+ };
76118
+ for (const nodeId of nodeIds) {
76119
+ const cells = measuredDetectorCells(rows, nodeId, ctx);
76120
+ if (Object.keys(cells).length > 0) out.set(nodeId, cells);
76121
+ }
76122
+ return out;
76123
+ }
76124
+ /** The measured capacity the automatic shares read (D755 `NodeCapacitySource`). */
76125
+ async readMeasuredCapacity(nodeIds) {
76126
+ const rows = this.nodeBenchmarkStore?.listResults() ?? [];
76127
+ if (rows.length === 0) return /* @__PURE__ */ new Map();
76128
+ return measuredCapacityFromResults(rows, nodeIds, {
76129
+ nowMs: Date.now(),
76130
+ currentPools: await this.readCurrentPools(rows)
76131
+ });
76132
+ }
72870
76133
  async getEffectiveShares() {
72871
76134
  const service = this.effectiveShares();
72872
76135
  const nodeIds = [...new Set(["hub", ...this.topology.collectRunnerNodeIds()])].toSorted();