@camstack/addon-pipeline-orchestrator 1.2.296 → 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.js CHANGED
@@ -30,7 +30,7 @@ node_fs = __toESM(node_fs);
30
30
  let node_path = require("node:path");
31
31
  node_path = __toESM(node_path);
32
32
  let node_url = require("node:url");
33
- //#region ../types/dist/event-category-B_0XEcLF.mjs
33
+ //#region ../types/dist/event-category-DaIZi00R.mjs
34
34
  var EventCategory = /* @__PURE__ */ function(EventCategory) {
35
35
  EventCategory["SystemBoot"] = "system.boot";
36
36
  EventCategory["SystemAddonsReady"] = "system.addons-ready";
@@ -109,6 +109,13 @@ var EventCategory = /* @__PURE__ */ function(EventCategory) {
109
109
  */
110
110
  EventCategory["AddonLoadFailed"] = "addon.load-failed";
111
111
  /**
112
+ * A keychain value was stored or removed (D787). Carries the ref, its
113
+ * owner and the new version — NEVER the value. A process holding the ref
114
+ * drops its cached copy and the owning `BaseAddon` re-resolves its config.
115
+ * Telemetry (D8): a process that misses it still revalidates by version.
116
+ */
117
+ EventCategory["KeychainChanged"] = "keychain.changed";
118
+ /**
112
119
  * Addon recovered from a previous failure. Emitted when an addon
113
120
  * transitions from `failed` back to `healthy` (typically via the
114
121
  * monitor's auto-retry loop, or after manual `addons.retryLoad`).
@@ -5401,7 +5408,7 @@ var ZodIssueCode = {
5401
5408
  var ZodFirstPartyTypeKind;
5402
5409
  ZodFirstPartyTypeKind || (ZodFirstPartyTypeKind = {});
5403
5410
  //#endregion
5404
- //#region ../types/dist/sleep-BNeXenvr.mjs
5411
+ //#region ../types/dist/sleep-Cr5YOk3m.mjs
5405
5412
  /**
5406
5413
  * The audio chunk plane's byte format, and the ONE expansion from a coded
5407
5414
  * window to float samples (D455).
@@ -5480,6 +5487,97 @@ function buildAlawTable() {
5480
5487
  }
5481
5488
  buildUlawTable();
5482
5489
  buildAlawTable();
5490
+ /** Is this field's stored value a credential? */
5491
+ function isSecretConfigField(field) {
5492
+ if (field.type === "password") return true;
5493
+ return "secret" in field && field.secret === true;
5494
+ }
5495
+ /**
5496
+ * Is this field's value stored in the KEYCHAIN (D787)? Every secret field is,
5497
+ * by its TYPE, unless it opts out with `storage: 'inline'` — never by its name.
5498
+ */
5499
+ function isKeychainStoredField(field) {
5500
+ if (!isSecretConfigField(field)) return false;
5501
+ if (field.type === "textarea" && field.isJson === true) return false;
5502
+ return !("storage" in field && field.storage === "inline");
5503
+ }
5504
+ /**
5505
+ * What a settings blob holds where a secret used to be.
5506
+ *
5507
+ * A keychain-stored field never holds its value. It holds a self-describing
5508
+ * MARKER, `{"$secret":"secret://…","v":1}` — an object, never a string, so no
5509
+ * code can mistake it for a credential and no form can send it to a camera as
5510
+ * a password. The empty state is `null` / absent, never `''`.
5511
+ *
5512
+ * The same shapes travel to and from the admin UI:
5513
+ *
5514
+ * - read: `hydrateSchema` hands a secret field a {@link SecretFieldView}
5515
+ * (`{ $secret, v, set: true }`) or `null` — the browser learns whether a
5516
+ * value is set, never the value.
5517
+ * - write: an untouched field submits the view back unchanged (KEEP); a typed
5518
+ * value is a plain non-empty string (CHANGE); the Clear button submits
5519
+ * {@link SECRET_CLEAR} (CLEAR). `''`, `null` and the legacy redaction tokens
5520
+ * are KEEP — an empty string is what a defaults-fallback pushes back, and
5521
+ * treating it as "clear" is how a save destroyed a stored credential before.
5522
+ *
5523
+ * Pure and browser-safe.
5524
+ */
5525
+ var SECRET_CLEAR = { $secretClear: true };
5526
+ /** The `'***'` token D634 redaction writes for non-admins. */
5527
+ var LEGACY_DEVICE_REDACTION = "***";
5528
+ function isRecord$1(value) {
5529
+ return typeof value === "object" && value !== null && !Array.isArray(value);
5530
+ }
5531
+ function makeSecretMarker(ref) {
5532
+ return {
5533
+ $secret: ref,
5534
+ v: 1
5535
+ };
5536
+ }
5537
+ /** A stored marker: an object whose `$secret` is a non-empty string. */
5538
+ function isSecretMarker(value) {
5539
+ if (!isRecord$1(value)) return false;
5540
+ const ref = value["$secret"];
5541
+ return typeof ref === "string" && ref.length > 0;
5542
+ }
5543
+ function isSecretClear(value) {
5544
+ return isRecord$1(value) && value["$secretClear"] === true;
5545
+ }
5546
+ /**
5547
+ * The UI view of a stored secret field value: a marker becomes "set", inline
5548
+ * plaintext becomes "set (inline)", nothing becomes `null`. A value never
5549
+ * survives this function.
5550
+ */
5551
+ function toSecretFieldView(stored) {
5552
+ if (isSecretMarker(stored)) return {
5553
+ $secret: stored.$secret,
5554
+ v: 1,
5555
+ set: true
5556
+ };
5557
+ if (typeof stored === "string" && stored.length > 0) return {
5558
+ $secret: null,
5559
+ v: 1,
5560
+ set: true,
5561
+ inline: true
5562
+ };
5563
+ return null;
5564
+ }
5565
+ /**
5566
+ * Classify an incoming value for a secret path. Only a non-empty string that is
5567
+ * not a redaction token sets a value; only {@link SECRET_CLEAR} clears.
5568
+ * Everything else — a marker (ours or anyone's), a UI view, `''`, `null`,
5569
+ * `undefined`, a redaction token, a non-string — keeps what is stored.
5570
+ */
5571
+ function classifySecretWrite(incoming) {
5572
+ if (isSecretClear(incoming)) return { kind: "clear" };
5573
+ if (typeof incoming !== "string") return { kind: "keep" };
5574
+ if (incoming.length === 0) return { kind: "keep" };
5575
+ if (incoming === "__camstack_redacted__" || incoming === LEGACY_DEVICE_REDACTION) return { kind: "keep" };
5576
+ return {
5577
+ kind: "set",
5578
+ value: incoming
5579
+ };
5580
+ }
5483
5581
  Object.fromEntries([
5484
5582
  {
5485
5583
  id: "overview",
@@ -5651,33 +5749,19 @@ Object.fromEntries([
5651
5749
  function isValuelessField(field) {
5652
5750
  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";
5653
5751
  }
5654
- /**
5655
- * Merge a `ConfigUISchema` with a raw `values` record into a
5656
- * `ConfigUISchemaWithValues`. Used by every `get*Settings` backend endpoint
5657
- * before returning to the admin UI. Groups are walked recursively.
5658
- *
5659
- * For each leaf field:
5660
- * - structural fields (`separator`, `info`, `button`) pass through unchanged
5661
- * - other fields get `value = values[key] ?? field.default ?? null`
5662
- *
5663
- * Unknown keys in `values` (keys not declared in the schema) are silently
5664
- * ignored — they don't contribute to the output. Missing keys fall back to
5665
- * the schema default or `null`. This mirrors the old backend behaviour where
5666
- * `FormBuilder` rendered `values[key] ?? field.default`.
5667
- */
5668
- function hydrateSchema(schema, values) {
5752
+ function hydrateSchema(schema, values, options = {}) {
5669
5753
  return {
5670
5754
  ...schema.tabs ? { tabs: [...schema.tabs] } : {},
5671
5755
  sections: schema.sections.map((section) => ({
5672
5756
  ...section,
5673
- fields: section.fields.map((field) => hydrateField(field, values))
5757
+ fields: section.fields.map((field) => hydrateField(field, values, options))
5674
5758
  }))
5675
5759
  };
5676
5760
  }
5677
- function hydrateField(field, values) {
5761
+ function hydrateField(field, values, options) {
5678
5762
  if (isValuelessField(field)) return field;
5679
5763
  if (field.type === "group") {
5680
- const hydratedChildren = field.fields.map((child) => hydrateField(child, values));
5764
+ const hydratedChildren = field.fields.map((child) => hydrateField(child, values, options));
5681
5765
  return {
5682
5766
  ...field,
5683
5767
  fields: hydratedChildren
@@ -5686,7 +5770,7 @@ function hydrateField(field, values) {
5686
5770
  if (field.type === "sub-tabs") {
5687
5771
  const hydratedTabs = field.tabs.map((tab) => ({
5688
5772
  ...tab,
5689
- fields: tab.fields.map((child) => hydrateField(child, values))
5773
+ fields: tab.fields.map((child) => hydrateField(child, values, options))
5690
5774
  }));
5691
5775
  return {
5692
5776
  ...field,
@@ -5696,6 +5780,33 @@ function hydrateField(field, values) {
5696
5780
  const key = field.key;
5697
5781
  const storedValue = Object.prototype.hasOwnProperty.call(values, key) ? values[key] : void 0;
5698
5782
  const defaultValue = field.default;
5783
+ const asView = (v) => isSecretMarker(v) || options.inlineSecretsAsViews === true && v !== void 0;
5784
+ if (isKeychainStoredField(field) && asView(storedValue)) return {
5785
+ ...field,
5786
+ value: toSecretFieldView(storedValue)
5787
+ };
5788
+ if (isKeychainStoredField(field) && options.inlineSecretsAsViews === true) return {
5789
+ ...field,
5790
+ value: null
5791
+ };
5792
+ if (field.type === "editable-array" && Array.isArray(storedValue)) {
5793
+ const secretItemKeys = field.itemFields.filter((item) => "key" in item && isKeychainStoredField(item)).map((item) => "key" in item ? item.key : "");
5794
+ if (secretItemKeys.length > 0) {
5795
+ const rows = storedValue.map((row) => {
5796
+ if (row === null || typeof row !== "object" || Array.isArray(row)) return row;
5797
+ const out = { ...row };
5798
+ for (const k of secretItemKeys) {
5799
+ const v = Reflect.get(row, k);
5800
+ if (asView(v)) out[k] = toSecretFieldView(v);
5801
+ }
5802
+ return out;
5803
+ });
5804
+ return {
5805
+ ...field,
5806
+ value: rows
5807
+ };
5808
+ }
5809
+ }
5699
5810
  if (field.multiple) {
5700
5811
  const stored = Array.isArray(storedValue) ? storedValue : storedValue !== void 0 && storedValue !== null ? [storedValue] : [];
5701
5812
  const itemFallback = field.multiple.itemDefault !== void 0 ? field.multiple.itemDefault : defaultValue !== void 0 ? defaultValue : typeOf(field) === "string" ? "" : null;
@@ -5954,6 +6065,431 @@ function projectStore(store, perNodeKeys, nodeId) {
5954
6065
  }
5955
6066
  return out;
5956
6067
  }
6068
+ var GLOBAL_SCOPE = { kind: "global" };
6069
+ /** The scope every addon-wide (not per-node, not per-device) setting lives in. */
6070
+ function globalSecretScope() {
6071
+ return GLOBAL_SCOPE;
6072
+ }
6073
+ function encodeSegment(value) {
6074
+ return encodeURIComponent(value);
6075
+ }
6076
+ /**
6077
+ * The field path of a value inside an array item, addressed by the item's id:
6078
+ * `arrayItemFieldPath('providers', 'authentik', 'clientSecret')` →
6079
+ * `providers[authentik].clientSecret`.
6080
+ */
6081
+ function arrayItemFieldPath(arrayKey, itemId, fieldKey) {
6082
+ return `${arrayKey}[${encodeSegment(itemId)}].${fieldKey}`;
6083
+ }
6084
+ /** A write to a secret path could not be stored in the keychain, so it was refused. */
6085
+ var KeychainWriteRefusedError = class extends Error {
6086
+ fieldPath;
6087
+ constructor(fieldPath, cause) {
6088
+ super(`secret "${fieldPath}" was not saved: the keychain could not store it (${cause instanceof Error ? cause.message : String(cause)}). Nothing was written.`);
6089
+ this.fieldPath = fieldPath;
6090
+ this.name = "KeychainWriteRefusedError";
6091
+ }
6092
+ };
6093
+ var KEEP = { kind: "keep" };
6094
+ function isRecord(value) {
6095
+ return typeof value === "object" && value !== null && !Array.isArray(value);
6096
+ }
6097
+ function defaultMintId() {
6098
+ return globalThis.crypto.randomUUID();
6099
+ }
6100
+ /** Every marker ref anywhere under `value`. */
6101
+ function collectSecretMarkerRefs(value, out = []) {
6102
+ if (isSecretMarker(value)) {
6103
+ out.push(value.$secret);
6104
+ return out;
6105
+ }
6106
+ if (Array.isArray(value)) for (const item of value) collectSecretMarkerRefs(item, out);
6107
+ else if (isRecord(value)) for (const child of Object.values(value)) collectSecretMarkerRefs(child, out);
6108
+ return out;
6109
+ }
6110
+ async function storedValueEquals(secrets, marker, candidate) {
6111
+ const resolved = (await secrets.resolve([marker.$secret]))[marker.$secret];
6112
+ return resolved?.kind === "value" && resolved.value === candidate;
6113
+ }
6114
+ async function writeLeaf(ctx, incoming, stored, fieldPath) {
6115
+ const intent = classifySecretWrite(incoming);
6116
+ const ref = ctx.secrets.ref(ctx.scope, fieldPath);
6117
+ const storedIsOurs = isSecretMarker(stored) && stored.$secret === ref;
6118
+ if (intent.kind === "keep") return KEEP;
6119
+ if (intent.kind === "set" && !ctx.operatorWrite && typeof stored === "string" && stored === intent.value) return KEEP;
6120
+ if (intent.kind === "clear") {
6121
+ if (storedIsOurs) ctx.deletes.push(ref);
6122
+ return {
6123
+ kind: "value",
6124
+ value: null
6125
+ };
6126
+ }
6127
+ if (storedIsOurs && await storedValueEquals(ctx.secrets, stored, intent.value)) return {
6128
+ kind: "value",
6129
+ value: stored
6130
+ };
6131
+ try {
6132
+ await ctx.secrets.put(ref, intent.value);
6133
+ } catch (err) {
6134
+ throw new KeychainWriteRefusedError(fieldPath, err);
6135
+ }
6136
+ return {
6137
+ kind: "value",
6138
+ value: makeSecretMarker(ref)
6139
+ };
6140
+ }
6141
+ /** Apply a member result onto `out`: KEEP restores the stored member, or removes it. */
6142
+ function applyMember(out, key, result, storedParent) {
6143
+ if (result.kind === "value") {
6144
+ out[key] = result.value;
6145
+ return;
6146
+ }
6147
+ if (Object.prototype.hasOwnProperty.call(storedParent, key)) out[key] = storedParent[key];
6148
+ else delete out[key];
6149
+ }
6150
+ async function writeMembers(ctx, children, incoming, stored, pathOf) {
6151
+ const out = { ...incoming };
6152
+ for (const [member, node] of children) applyMember(out, member, await writeNode(ctx, node, incoming[member], stored[member], pathOf(member)), stored);
6153
+ return out;
6154
+ }
6155
+ function rowId$1(row, idKey) {
6156
+ const raw = row[idKey];
6157
+ if (typeof raw === "string" && raw.length > 0) return raw;
6158
+ if (typeof raw === "number" && Number.isFinite(raw)) return String(raw);
6159
+ return null;
6160
+ }
6161
+ async function writeArray(ctx, node, incoming, stored, fieldPath) {
6162
+ const storedRows = Array.isArray(stored) ? stored.filter(isRecord) : [];
6163
+ if (!Array.isArray(incoming)) {
6164
+ ctx.deletes.push(...collectSecretMarkerRefs(storedRows));
6165
+ return {
6166
+ kind: "value",
6167
+ value: incoming
6168
+ };
6169
+ }
6170
+ const storedById = /* @__PURE__ */ new Map();
6171
+ for (const row of storedRows) {
6172
+ const id = rowId$1(row, node.idKey);
6173
+ if (id !== null) storedById.set(id, row);
6174
+ }
6175
+ const kept = /* @__PURE__ */ new Set();
6176
+ const rows = [];
6177
+ for (const row of incoming) {
6178
+ if (!isRecord(row)) {
6179
+ rows.push(row);
6180
+ continue;
6181
+ }
6182
+ const id = rowId$1(row, node.idKey) ?? ctx.mintId();
6183
+ kept.add(id);
6184
+ const withId = {
6185
+ ...row,
6186
+ [node.idKey]: row[node.idKey] ?? id
6187
+ };
6188
+ if (rowId$1(withId, node.idKey) === null) withId[node.idKey] = id;
6189
+ const storedRow = storedById.get(id) ?? {};
6190
+ rows.push(await writeMembers(ctx, node.item, withId, storedRow, (member) => arrayItemFieldPath(fieldPath, id, member)));
6191
+ }
6192
+ for (const [id, row] of storedById) if (!kept.has(id)) ctx.deletes.push(...collectSecretMarkerRefs(row));
6193
+ return {
6194
+ kind: "value",
6195
+ value: rows
6196
+ };
6197
+ }
6198
+ async function writeNode(ctx, node, incoming, stored, fieldPath) {
6199
+ switch (node.kind) {
6200
+ case "leaf": return writeLeaf(ctx, incoming, stored, fieldPath);
6201
+ case "array": return writeArray(ctx, node, incoming, stored, fieldPath);
6202
+ case "object": {
6203
+ if (!isRecord(incoming)) {
6204
+ if (incoming === void 0) return KEEP;
6205
+ ctx.deletes.push(...collectSecretMarkerRefs(stored));
6206
+ return {
6207
+ kind: "value",
6208
+ value: incoming
6209
+ };
6210
+ }
6211
+ const storedObj = isRecord(stored) ? stored : {};
6212
+ return {
6213
+ kind: "value",
6214
+ value: await writeMembers(ctx, node.children, incoming, storedObj, (m) => `${fieldPath}.${m}`)
6215
+ };
6216
+ }
6217
+ }
6218
+ }
6219
+ /** Split a perNode-scoped key (`key@nodeId`). */
6220
+ function splitNodeKey(key) {
6221
+ const at = key.indexOf("@");
6222
+ return at <= 0 ? {
6223
+ base: key,
6224
+ nodeId: null
6225
+ } : {
6226
+ base: key.slice(0, at),
6227
+ nodeId: key.slice(at + 1)
6228
+ };
6229
+ }
6230
+ /** Rewrite a settings patch so no secret value reaches the blob. See the file doc. */
6231
+ async function transformSecretWrite(input) {
6232
+ const deletes = [];
6233
+ const base = {
6234
+ secrets: input.secrets,
6235
+ mintId: input.mintId ?? defaultMintId,
6236
+ deletes,
6237
+ operatorWrite: input.operatorWrite === true
6238
+ };
6239
+ const out = {};
6240
+ for (const [key, incoming] of Object.entries(input.patch)) {
6241
+ const { base: baseKey, nodeId } = splitNodeKey(key);
6242
+ const node = input.tree.get(baseKey);
6243
+ if (node === void 0) {
6244
+ out[key] = incoming;
6245
+ continue;
6246
+ }
6247
+ const scope = nodeId !== null && nodeId.length > 0 ? {
6248
+ kind: "node",
6249
+ nodeId
6250
+ } : input.scope ?? globalSecretScope();
6251
+ const result = await writeNode({
6252
+ ...base,
6253
+ scope
6254
+ }, node, incoming, input.stored[key], baseKey);
6255
+ if (result.kind === "value") out[key] = result.value;
6256
+ }
6257
+ return {
6258
+ patch: out,
6259
+ afterWrite: async () => {
6260
+ if (deletes.length === 0) return { orphaned: [] };
6261
+ try {
6262
+ await input.secrets.delete([...new Set(deletes)]);
6263
+ return { orphaned: [] };
6264
+ } catch {
6265
+ return { orphaned: [...new Set(deletes)] };
6266
+ }
6267
+ }
6268
+ };
6269
+ }
6270
+ /**
6271
+ * Replace every marker in `value` with its keychain value. Returns the SAME
6272
+ * reference when there is no marker (no allocation, no keychain call).
6273
+ */
6274
+ async function resolveSecretMarkers(value, secrets) {
6275
+ const refs = collectSecretMarkerRefs(value);
6276
+ if (refs.length === 0) return {
6277
+ value,
6278
+ unresolved: []
6279
+ };
6280
+ const answers = await secrets.resolve(refs);
6281
+ const unresolved = [];
6282
+ for (const ref of new Set(refs)) {
6283
+ const answer = answers[ref];
6284
+ if (answer === void 0) unresolved.push({
6285
+ ref,
6286
+ reason: "not-initialized"
6287
+ });
6288
+ else if (answer.kind === "unavailable") unresolved.push({
6289
+ ref,
6290
+ reason: answer.reason
6291
+ });
6292
+ else if (answer.kind === "missing" || answer.kind === "forbidden") unresolved.push({
6293
+ ref,
6294
+ reason: answer.kind
6295
+ });
6296
+ }
6297
+ const substitute = (v) => {
6298
+ if (isSecretMarker(v)) {
6299
+ const answer = answers[v.$secret];
6300
+ return answer?.kind === "value" ? {
6301
+ keep: true,
6302
+ value: answer.value
6303
+ } : {
6304
+ keep: false,
6305
+ value: void 0
6306
+ };
6307
+ }
6308
+ if (Array.isArray(v)) {
6309
+ const items = [];
6310
+ for (const item of v) {
6311
+ const s = substitute(item);
6312
+ if (s.keep) items.push(s.value);
6313
+ }
6314
+ return {
6315
+ keep: true,
6316
+ value: items
6317
+ };
6318
+ }
6319
+ if (isRecord(v)) {
6320
+ const out = {};
6321
+ for (const [k, child] of Object.entries(v)) {
6322
+ const s = substitute(child);
6323
+ if (s.keep) out[k] = s.value;
6324
+ }
6325
+ return {
6326
+ keep: true,
6327
+ value: out
6328
+ };
6329
+ }
6330
+ return {
6331
+ keep: true,
6332
+ value: v
6333
+ };
6334
+ };
6335
+ return {
6336
+ value: substitute(value).value,
6337
+ unresolved
6338
+ };
6339
+ }
6340
+ function touchesTree(tree, patch) {
6341
+ if (tree.size === 0) return false;
6342
+ for (const key of Object.keys(patch)) {
6343
+ const at = key.indexOf("@");
6344
+ if (tree.has(at > 0 ? key.slice(0, at) : key)) return true;
6345
+ }
6346
+ return false;
6347
+ }
6348
+ /** The blob as stored now; a failed read REFUSES the write instead of guessing. */
6349
+ async function readStoredAddonBlob(raw) {
6350
+ const threeWay = raw.readAddonStoreResult;
6351
+ if (threeWay === void 0) return raw.readAddonStore();
6352
+ const result = await threeWay.call(raw);
6353
+ if (result.kind === "failed") throw new Error(`settings write refused: the stored blob could not be read (${result.error})`);
6354
+ return result.kind === "missing" ? {} : result.value;
6355
+ }
6356
+ function createSecretAwareSettings(raw, opts) {
6357
+ const chains = /* @__PURE__ */ new Map();
6358
+ const serialize = (key, fn) => {
6359
+ const next = (chains.get(key) ?? Promise.resolve()).then(fn, fn);
6360
+ const tail = next.catch(() => void 0);
6361
+ chains.set(key, tail);
6362
+ tail.then(() => {
6363
+ if (chains.get(key) === tail) chains.delete(key);
6364
+ });
6365
+ return next;
6366
+ };
6367
+ const write = async (chainKey, tree, scope, patch, readStored, rawWrite) => {
6368
+ if (!touchesTree(tree, patch)) return rawWrite(patch);
6369
+ return serialize(chainKey, async () => {
6370
+ const stored = await readStored();
6371
+ const result = await transformSecretWrite({
6372
+ tree,
6373
+ secrets: opts.secrets,
6374
+ patch,
6375
+ stored,
6376
+ ...scope !== void 0 ? { scope } : {},
6377
+ operatorWrite: opts.isOperatorWrite?.() === true
6378
+ });
6379
+ if (Object.keys(result.patch).length > 0) await rawWrite(result.patch);
6380
+ const { orphaned } = await result.afterWrite();
6381
+ if (orphaned.length > 0) opts.onOrphaned(orphaned);
6382
+ });
6383
+ };
6384
+ const wrapped = {
6385
+ writeAddonStore: (patch) => write("addon", opts.addonTree(), void 0, patch, () => readStoredAddonBlob(raw), (p) => raw.writeAddonStore(p)),
6386
+ writeDeviceStore: (deviceId, patch) => write(`device:${deviceId}`, opts.deviceTree(), {
6387
+ kind: "device",
6388
+ deviceId
6389
+ }, patch, () => raw.readDeviceStore(deviceId), (p) => raw.writeDeviceStore(deviceId, p))
6390
+ };
6391
+ return new Proxy(raw, { get(target, prop, receiver) {
6392
+ if (prop === "writeAddonStore") return wrapped.writeAddonStore;
6393
+ if (prop === "writeDeviceStore") return wrapped.writeDeviceStore;
6394
+ const value = Reflect.get(target, prop, receiver);
6395
+ return typeof value === "function" ? value.bind(target) : value;
6396
+ } });
6397
+ }
6398
+ /** The context the addon sees: identical, except that its settings writes are secret-aware. */
6399
+ function withSecretAwareSettings(context, opts) {
6400
+ const raw = context.settings;
6401
+ if (raw === void 0) return context;
6402
+ const settings = createSecretAwareSettings(raw, opts);
6403
+ return new Proxy(context, { get(target, prop, receiver) {
6404
+ if (prop === "settings") return settings;
6405
+ return Reflect.get(target, prop, receiver);
6406
+ } });
6407
+ }
6408
+ var LEAF = { kind: "leaf" };
6409
+ var DEFAULT_ITEM_ID_KEY = "id";
6410
+ function collectFields(fields, out) {
6411
+ for (const field of fields) {
6412
+ if (field.type === "group") {
6413
+ collectFields(field.fields, out);
6414
+ continue;
6415
+ }
6416
+ if (field.type === "sub-tabs") {
6417
+ for (const tab of field.tabs) collectFields(tab.fields, out);
6418
+ continue;
6419
+ }
6420
+ if (field.type === "editable-array") {
6421
+ const item = /* @__PURE__ */ new Map();
6422
+ for (const itemField of field.itemFields) if ("key" in itemField && isKeychainStoredField(itemField)) item.set(itemField.key, LEAF);
6423
+ if (item.size > 0) out.set(field.key, {
6424
+ kind: "array",
6425
+ idKey: field.itemIdKey ?? DEFAULT_ITEM_ID_KEY,
6426
+ item
6427
+ });
6428
+ continue;
6429
+ }
6430
+ if (!("key" in field) || typeof field.key !== "string") continue;
6431
+ if (isKeychainStoredField(field)) out.set(field.key, LEAF);
6432
+ }
6433
+ }
6434
+ /** The secret tree a settings schema declares. */
6435
+ function secretTreeFromSchema(schema) {
6436
+ const out = /* @__PURE__ */ new Map();
6437
+ if (schema === null) return out;
6438
+ for (const section of schema.sections) collectFields(section.fields, out);
6439
+ return out;
6440
+ }
6441
+ /** Parse `a.b[id].c` into segments; throws on a malformed path (a declaration bug). */
6442
+ function parseSecretStatePath(path) {
6443
+ const segments = [];
6444
+ for (const raw of path.split(".")) {
6445
+ const m = /^([^[\]]+)(?:\[([^[\]]+)\])?$/.exec(raw);
6446
+ if (m === null || m[1] === void 0) throw new Error(`secretStatePaths: malformed path "${path}"`);
6447
+ segments.push(m[2] !== void 0 ? {
6448
+ key: m[1],
6449
+ idKey: m[2]
6450
+ } : { key: m[1] });
6451
+ }
6452
+ if (segments.length === 0) throw new Error(`secretStatePaths: empty path`);
6453
+ if (segments[segments.length - 1]?.idKey !== void 0) throw new Error(`secretStatePaths: "${path}" ends on an array — name the member that is secret`);
6454
+ return segments;
6455
+ }
6456
+ function insert(into, segments) {
6457
+ const [head, ...rest] = segments;
6458
+ if (head === void 0) return;
6459
+ if (rest.length === 0) {
6460
+ into.set(head.key, LEAF);
6461
+ return;
6462
+ }
6463
+ const existing = into.get(head.key);
6464
+ if (head.idKey !== void 0) {
6465
+ const item = new Map(existing?.kind === "array" ? existing.item : []);
6466
+ insert(item, rest);
6467
+ into.set(head.key, {
6468
+ kind: "array",
6469
+ idKey: head.idKey,
6470
+ item
6471
+ });
6472
+ return;
6473
+ }
6474
+ const children = new Map(existing?.kind === "object" ? existing.children : []);
6475
+ insert(children, rest);
6476
+ into.set(head.key, {
6477
+ kind: "object",
6478
+ children
6479
+ });
6480
+ }
6481
+ /** Add hidden-state paths (`secretStatePaths`) to a schema-derived tree. */
6482
+ function mergeSecretStatePaths(tree, paths) {
6483
+ if (paths.length === 0) return tree;
6484
+ const out = new Map(tree);
6485
+ for (const path of paths) insert(out, parseSecretStatePath(path));
6486
+ return out;
6487
+ }
6488
+ /** First retry of a secret the keychain could not serve; doubles to {@link SECRET_RETRY_MAX_MS}. */
6489
+ var SECRET_RETRY_FIRST_MS = 5e3;
6490
+ var SECRET_RETRY_MAX_MS = 5 * 6e4;
6491
+ /** Coalesces a burst of `keychain.changed` events into one re-resolve. */
6492
+ var SECRET_CHANGE_DEBOUNCE_MS = 250;
5957
6493
  /**
5958
6494
  * Base class for CamStack addons. Eliminates settings boilerplate:
5959
6495
  *
@@ -6023,6 +6559,25 @@ var BaseAddon = class {
6023
6559
  settingsStoreReady = false;
6024
6560
  /** Default config values. Provided via constructor. */
6025
6561
  defaults;
6562
+ /**
6563
+ * Hidden-state paths that hold secrets (D787) — values the addon writes
6564
+ * itself (`updateGlobalSettings`, `state()`) that no form field declares.
6565
+ * Stored in the keychain exactly like a `password` field. Syntax:
6566
+ * `'snapshotSigningKey'`, `'identity.pincode'`, `'tokens[userId].accessToken'`
6567
+ * (`key[<id field>]` addresses array items by id, never by index).
6568
+ */
6569
+ secretStatePaths = [];
6570
+ _addonSecretTree = null;
6571
+ _deviceSecretTree = null;
6572
+ /** Refs the last resolved config pointed at — a change to one re-resolves. */
6573
+ _configSecretRefs = /* @__PURE__ */ new Set();
6574
+ _unresolvedSecrets = [];
6575
+ _secretRetryTimer = null;
6576
+ _secretRetryDelayMs = SECRET_RETRY_FIRST_MS;
6577
+ _secretChangeTimer = null;
6578
+ _secretChangeUnsub = null;
6579
+ /** >0 while an OPERATOR's settings write is being applied. */
6580
+ _operatorWriteDepth = 0;
6026
6581
  constructor(defaults) {
6027
6582
  this.defaults = defaults;
6028
6583
  this._config = { ...defaults };
@@ -6058,8 +6613,18 @@ var BaseAddon = class {
6058
6613
  return this._config;
6059
6614
  }
6060
6615
  async initialize(context) {
6061
- this._ctx = context;
6616
+ const secrets = context.secrets;
6617
+ this._ctx = secrets !== void 0 ? withSecretAwareSettings(context, {
6618
+ secrets,
6619
+ addonTree: () => this.addonSecretTree(),
6620
+ deviceTree: () => this.deviceSecretTree(),
6621
+ onOrphaned: (refs) => context.logger.warn("keychain refs dropped by a settings write could not be deleted", { meta: { refs: [...refs] } }),
6622
+ isOperatorWrite: () => this._operatorWriteDepth > 0
6623
+ }) : context;
6062
6624
  await this.resolveConfig();
6625
+ if (secrets !== void 0) this._secretChangeUnsub = secrets.onChange((ref) => {
6626
+ if (this._configSecretRefs.has(ref)) this.scheduleSecretReresolve(SECRET_CHANGE_DEBOUNCE_MS);
6627
+ });
6063
6628
  const result = await this.onInitialize();
6064
6629
  this.emitLifecycle(EventCategory.AddonStarted);
6065
6630
  const normalized = normalizeAddonInitResult(result);
@@ -6092,6 +6657,12 @@ var BaseAddon = class {
6092
6657
  */
6093
6658
  async onHubReachable() {}
6094
6659
  async shutdown() {
6660
+ this._secretChangeUnsub?.();
6661
+ this._secretChangeUnsub = null;
6662
+ if (this._secretRetryTimer !== null) clearTimeout(this._secretRetryTimer);
6663
+ if (this._secretChangeTimer !== null) clearTimeout(this._secretChangeTimer);
6664
+ this._secretRetryTimer = null;
6665
+ this._secretChangeTimer = null;
6095
6666
  this.emitLifecycle(EventCategory.AddonStopped);
6096
6667
  if (this.autoEmitReadiness) this.emitReadinessForProviders("down");
6097
6668
  await this.onShutdown();
@@ -6170,7 +6741,7 @@ var BaseAddon = class {
6170
6741
  return hydrateSchema(schema, overlay ? {
6171
6742
  ...projected,
6172
6743
  ...overlay
6173
- } : projected);
6744
+ } : projected, { inlineSecretsAsViews: true });
6174
6745
  }
6175
6746
  /**
6176
6747
  * The integration-level view of this addon's settings: exactly the sections
@@ -6219,7 +6790,7 @@ var BaseAddon = class {
6219
6790
  return hydrateSchema({
6220
6791
  ...schema,
6221
6792
  sections
6222
- }, projected);
6793
+ }, projected, { inlineSecretsAsViews: true });
6223
6794
  }
6224
6795
  /**
6225
6796
  * The raw addon store PROJECTED onto the target node's bare per-node keys:
@@ -6273,18 +6844,50 @@ var BaseAddon = class {
6273
6844
  value: result.value[key]
6274
6845
  };
6275
6846
  }
6847
+ /**
6848
+ * The OPERATOR's write — a settings form or an admin API call, dispatched by
6849
+ * the kernel. Identical to {@link updateGlobalSettings}, except that a secret
6850
+ * the operator sends is stored as sent even when it equals a pre-keychain
6851
+ * inline value: the operator re-entering a password is a change, an addon
6852
+ * writing back what it read is not (D787).
6853
+ */
6854
+ async updateGlobalSettingsAsOperator(patch, nodeId) {
6855
+ this._operatorWriteDepth++;
6856
+ try {
6857
+ await this.applyGlobalPatch(patch, nodeId);
6858
+ } finally {
6859
+ this._operatorWriteDepth--;
6860
+ }
6861
+ }
6276
6862
  async updateGlobalSettings(patch, nodeId) {
6863
+ await this.applyGlobalPatch(patch, nodeId);
6864
+ }
6865
+ /**
6866
+ * REMOVE stored secrets — their keychain values and their markers (D787).
6867
+ *
6868
+ * Writing `''` to a secret field KEEPS what is stored (an empty string is
6869
+ * what a defaults fallback writes back, and it must never destroy a
6870
+ * credential), so code that means "forget this token" says so here. Without
6871
+ * a keychain (an older kernel) it writes `''`, as such code always did.
6872
+ */
6873
+ async clearSecrets(keys, nodeId) {
6874
+ const tree = this.addonSecretTree();
6875
+ const keychain = this._ctx?.secrets !== void 0;
6876
+ const patch = {};
6877
+ for (const key of keys) patch[key] = keychain && tree.has(key) ? SECRET_CLEAR : "";
6878
+ await this.applyGlobalPatch(patch, nodeId);
6879
+ }
6880
+ async applyGlobalPatch(barePatch, nodeId) {
6277
6881
  const keys = this.perNodeKeys();
6278
6882
  const localNode = normalizeNodeId(this._ctx?.kernel?.localNodeId);
6279
6883
  const target = normalizeNodeId(nodeId ?? this._ctx?.kernel?.localNodeId);
6280
- const barePatch = patch;
6281
6884
  const scoped = keys.size > 0 ? scopePatch(barePatch, keys, target) : barePatch;
6282
6885
  await this._ctx?.settings?.writeAddonStore(scoped);
6283
6886
  if (target !== localNode) return;
6284
6887
  await this.resolveConfig();
6285
6888
  await this.onConfigChanged();
6286
6889
  this.emitLifecycle(EventCategory.AddonUpdated, { level: "global" });
6287
- this.maybeAutoRestart(patch, this.globalSettingsSchema());
6890
+ this.maybeAutoRestart(Object.keys(barePatch), this.globalSettingsSchema());
6288
6891
  }
6289
6892
  /**
6290
6893
  * The set of field keys the global settings schema declares `perNode: true`
@@ -6309,7 +6912,7 @@ var BaseAddon = class {
6309
6912
  * time to return its response before the addon is torn down and
6310
6913
  * re-initialised by `AddonRegistryService.restartAddon`.
6311
6914
  */
6312
- maybeAutoRestart(patch, schema) {
6915
+ maybeAutoRestart(patchKeys, schema) {
6313
6916
  if (!schema) return;
6314
6917
  const restartKeys = /* @__PURE__ */ new Set();
6315
6918
  for (const section of schema.sections) for (const field of section.fields) {
@@ -6317,13 +6920,13 @@ var BaseAddon = class {
6317
6920
  if (field.requiresRestart) restartKeys.add(field.key);
6318
6921
  }
6319
6922
  if (restartKeys.size === 0) return;
6320
- if (!Object.keys(patch).some((k) => restartKeys.has(k))) return;
6923
+ if (!patchKeys.some((k) => restartKeys.has(k))) return;
6321
6924
  const ctx = this._ctx;
6322
6925
  if (!ctx) return;
6323
6926
  const addonId = ctx.id;
6324
6927
  setImmediate(() => {
6325
6928
  ctx.api.addons?.restartAddon?.mutate({ addonId }).then(() => {
6326
- ctx.logger.info("addon auto-restart triggered by restart-required setting change", { meta: { changedFields: Object.keys(patch).filter((k) => restartKeys.has(k)) } });
6929
+ ctx.logger.info("addon auto-restart triggered by restart-required setting change", { meta: { changedFields: patchKeys.filter((k) => restartKeys.has(k)) } });
6327
6930
  }).catch((err) => {
6328
6931
  ctx.logger.error("addon auto-restart failed", { meta: { error: err instanceof Error ? err.message : String(err) } });
6329
6932
  });
@@ -6332,7 +6935,7 @@ var BaseAddon = class {
6332
6935
  async getDeviceSettings(deviceId) {
6333
6936
  const schema = this.deviceSettingsSchema();
6334
6937
  if (!schema) return { sections: [] };
6335
- return hydrateSchema(schema, await this._ctx?.settings?.readDeviceStore(deviceId) ?? {});
6938
+ return hydrateSchema(schema, await this._ctx?.settings?.readDeviceStore(deviceId) ?? {}, { inlineSecretsAsViews: true });
6336
6939
  }
6337
6940
  async updateDeviceSettings(deviceId, patch) {
6338
6941
  await this._ctx?.settings?.writeDeviceStore(deviceId, patch);
@@ -6462,7 +7065,7 @@ var BaseAddon = class {
6462
7065
  * selection at boot (absent scoped key ⇒ the constructor default wins).
6463
7066
  */
6464
7067
  async resolveConfig() {
6465
- const stored = await this.readAddonStoreWithRetry();
7068
+ const stored = await this.resolveStoredSecrets(await this.readAddonStoreWithRetry());
6466
7069
  const perNode = this.perNodeKeys();
6467
7070
  const localNode = normalizeNodeId(this._ctx?.kernel?.localNodeId);
6468
7071
  const resolved = { ...this.defaults };
@@ -6487,7 +7090,7 @@ var BaseAddon = class {
6487
7090
  key,
6488
7091
  schema,
6489
7092
  fallback,
6490
- read: () => this.readAddonStoreWithRetry(),
7093
+ read: async () => this.resolveStoredSecrets(await this.readAddonStoreWithRetry()),
6491
7094
  write: async (patch) => {
6492
7095
  await this._ctx?.settings?.writeAddonStore(patch);
6493
7096
  },
@@ -6504,7 +7107,7 @@ var BaseAddon = class {
6504
7107
  key,
6505
7108
  schema,
6506
7109
  fallback,
6507
- read: async () => await this._ctx?.settings?.readDeviceStore(deviceId) ?? {},
7110
+ read: async () => this.resolveStoredSecrets(await this._ctx?.settings?.readDeviceStore(deviceId) ?? {}, false),
6508
7111
  write: async (patch) => {
6509
7112
  await this._ctx?.settings?.writeDeviceStore(deviceId, patch);
6510
7113
  },
@@ -6516,6 +7119,70 @@ var BaseAddon = class {
6516
7119
  } })
6517
7120
  });
6518
7121
  }
7122
+ /** The addon store's secret tree: schema fields by TYPE, plus `secretStatePaths`. */
7123
+ addonSecretTree() {
7124
+ this._addonSecretTree ??= mergeSecretStatePaths(secretTreeFromSchema(this.globalSettingsSchema()), this.secretStatePaths);
7125
+ return this._addonSecretTree;
7126
+ }
7127
+ deviceSecretTree() {
7128
+ this._deviceSecretTree ??= secretTreeFromSchema(this.deviceSettingsSchema());
7129
+ return this._deviceSecretTree;
7130
+ }
7131
+ /**
7132
+ * Secrets of this addon's stored config that could not be resolved on the
7133
+ * last read, with why. Non-empty means the addon is running WITHOUT them —
7134
+ * an addon whose function needs one should report itself degraded rather
7135
+ * than start on an empty credential.
7136
+ */
7137
+ get unresolvedSecrets() {
7138
+ return this._unresolvedSecrets;
7139
+ }
7140
+ /**
7141
+ * Substitute every keychain marker in a stored blob with its value. With no
7142
+ * keychain (no `ctx.secrets`) or no marker, the blob comes back unchanged.
7143
+ * An unresolvable value is LEFT OUT (never `''`), logged by ref — never by
7144
+ * value — and, when the cause can pass, retried with backoff. Nothing is
7145
+ * ever written back from here.
7146
+ */
7147
+ async resolveStoredSecrets(blob, trackForConfig = true) {
7148
+ const secrets = this._ctx?.secrets;
7149
+ if (secrets === void 0) return blob;
7150
+ const refs = collectSecretMarkerRefs(blob);
7151
+ if (trackForConfig) this._configSecretRefs = new Set(refs);
7152
+ if (refs.length === 0) {
7153
+ if (trackForConfig) this._unresolvedSecrets = [];
7154
+ return blob;
7155
+ }
7156
+ const { value, unresolved } = await resolveSecretMarkers(blob, secrets);
7157
+ if (trackForConfig) {
7158
+ this._unresolvedSecrets = unresolved;
7159
+ if (unresolved.length > 0) {
7160
+ this._ctx?.logger?.error?.("secret unavailable — running without it, will retry", { meta: { secrets: unresolved.map((u) => ({
7161
+ ref: u.ref,
7162
+ reason: u.reason
7163
+ })) } });
7164
+ if (unresolved.some((u) => u.reason !== "missing" && u.reason !== "forbidden")) {
7165
+ this.scheduleSecretReresolve(this._secretRetryDelayMs);
7166
+ this._secretRetryDelayMs = Math.min(this._secretRetryDelayMs * 2, SECRET_RETRY_MAX_MS);
7167
+ }
7168
+ } else this._secretRetryDelayMs = SECRET_RETRY_FIRST_MS;
7169
+ }
7170
+ return value;
7171
+ }
7172
+ /** Re-resolve the config (a keychain-only change, or a retry) and apply it. */
7173
+ scheduleSecretReresolve(delayMs) {
7174
+ const timerOf = delayMs === SECRET_CHANGE_DEBOUNCE_MS ? "_secretChangeTimer" : "_secretRetryTimer";
7175
+ if (this[timerOf] !== null) return;
7176
+ const timer = setTimeout(() => {
7177
+ this[timerOf] = null;
7178
+ if (this._ctx === null) return;
7179
+ this.resolveConfig().then(() => this.onConfigChanged()).catch((err) => {
7180
+ this._ctx?.logger?.error?.("re-resolving secrets failed", { meta: { error: err instanceof Error ? err.message : String(err) } });
7181
+ });
7182
+ }, delayMs);
7183
+ timer.unref?.();
7184
+ this[timerOf] = timer;
7185
+ }
6519
7186
  /**
6520
7187
  * Wrap `ctx.settings.readAddonStore()` with a short retry budget so a
6521
7188
  * transient settings-store outage (mid-restart of sqlite-settings, tsx-watch
@@ -15095,6 +15762,196 @@ id: union([string(), number()]) }), {
15095
15762
  kind: "mutation",
15096
15763
  auth: "admin"
15097
15764
  });
15765
+ /**
15766
+ * `keychain` — the hub's encrypted store of secret values (D787).
15767
+ *
15768
+ * Settings blobs never hold a secret: they hold a marker
15769
+ * `{"$secret":"secret://<owner>/<scope>/<fieldPath>","v":1}` and the value
15770
+ * lives here, AES-256-GCM under a data key that a master key wraps. This cap
15771
+ * is the OWNER's door to its own values: every method is owner-checked
15772
+ * against the CALLER, never against anything the caller claims.
15773
+ *
15774
+ * Who the caller is: the transport stamps `callerAddonId` into the args of a
15775
+ * call that crossed a process (the parent `LocalChildRegistry` from the UDS
15776
+ * connection, the agent from its child's connection carried in
15777
+ * `$hub-cap-fwd`'s envelope). A stamp always overwrites; a wire call with no
15778
+ * stamp has its claim stripped. Only a hub in-process caller may state its own
15779
+ * identity — `ctx.secrets` does that with the addon id closed over. This is
15780
+ * identity against BUGS and misrouting, not against a malicious addon (every
15781
+ * runner is the same OS user): see the threat model in
15782
+ * `docs/design/2026-10-08-secrets-keychain.md`.
15783
+ *
15784
+ * `callerAddonId` is deliberately NOT in any input schema: the tRPC router
15785
+ * parses the input, so a session can never supply it, and the internal mount
15786
+ * floors every method to admin anyway (D240).
15787
+ *
15788
+ * Internal: addons reach it through `ctx.secrets`, never directly.
15789
+ */
15790
+ /** Why a value cannot be served right now. Never folded into "missing". */
15791
+ var KeychainUnavailableReasonSchema = _enum([
15792
+ "key-missing",
15793
+ "wrong-key",
15794
+ "key-file-unreadable",
15795
+ "store-unavailable",
15796
+ "not-initialized",
15797
+ "decrypt-failed"
15798
+ ]);
15799
+ var KeychainStateSchema = discriminatedUnion("state", [object({ state: literal("ready") }), object({
15800
+ state: literal("locked"),
15801
+ reason: KeychainUnavailableReasonSchema
15802
+ })]);
15803
+ var KeychainResolveEntrySchema = discriminatedUnion("kind", [
15804
+ object({
15805
+ kind: literal("value"),
15806
+ value: string(),
15807
+ version: number().int()
15808
+ }),
15809
+ object({ kind: literal("missing") }),
15810
+ object({
15811
+ kind: literal("unavailable"),
15812
+ reason: KeychainUnavailableReasonSchema
15813
+ }),
15814
+ object({ kind: literal("forbidden") })
15815
+ ]);
15816
+ var KeychainResolveResultSchema = record(string(), KeychainResolveEntrySchema);
15817
+ /** Metadata of one stored value — never the value. */
15818
+ var KeychainSecretMetaSchema = object({
15819
+ ref: string(),
15820
+ owner: string(),
15821
+ /** Formatted scope (`global`, `node/<id>`, `device/<id>`, …). */
15822
+ scope: string(),
15823
+ fieldPath: string(),
15824
+ version: number().int(),
15825
+ createdAt: number(),
15826
+ updatedAt: number(),
15827
+ /** `addon:<id>` | `user:<id>` | `system`. */
15828
+ updatedBy: string(),
15829
+ /** Coarse — written at most once an hour per ref. `null` = never read. */
15830
+ lastReadAt: number().nullable()
15831
+ });
15832
+ var RefListSchema = array(string().min(1)).max(512);
15833
+ method(object({
15834
+ ref: string().min(1),
15835
+ value: string().min(1)
15836
+ }), object({
15837
+ ref: string(),
15838
+ version: number().int()
15839
+ }), {
15840
+ kind: "mutation",
15841
+ auth: "admin"
15842
+ }), method(object({ refs: RefListSchema }), KeychainResolveResultSchema, { auth: "admin" }), method(object({ refs: RefListSchema }), object({ deleted: number().int() }), {
15843
+ kind: "mutation",
15844
+ auth: "admin"
15845
+ }), method(object({}), array(KeychainSecretMetaSchema).readonly(), { auth: "admin" }), method(object({}), KeychainStateSchema, { auth: "admin" });
15846
+ var KeychainKeySourceSchema = object({
15847
+ kind: _enum([
15848
+ "config-file",
15849
+ "previous-config-file",
15850
+ "platform",
15851
+ "default-file"
15852
+ ]),
15853
+ /** File sources only — a path is not a secret. */
15854
+ path: string().optional(),
15855
+ /**
15856
+ * True when a whole-volume backup of the data directory carries this key
15857
+ * next to the database it protects (the default file). The page warns.
15858
+ */
15859
+ sharesBackupWithDatabase: boolean()
15860
+ });
15861
+ var KeychainStatusSchema = object({
15862
+ keychain: KeychainStateSchema,
15863
+ /** The source of the master key in use; `null` while locked. */
15864
+ keySource: KeychainKeySourceSchema.nullable(),
15865
+ /** The source configured to be primary — differs from `keySource` while a rotation is pending. */
15866
+ configuredSource: KeychainKeySourceSchema,
15867
+ /** Short fingerprint of the master key in use (`HMAC(KEK, label)`, 16 hex). */
15868
+ fingerprint: string().nullable(),
15869
+ activeKeyId: string().nullable(),
15870
+ /**
15871
+ * The keychain unlocked with a NON-primary source: the configured primary
15872
+ * key is not the one that wraps the data key yet. `rotateMasterKey` moves it.
15873
+ */
15874
+ rotationPending: boolean(),
15875
+ /** Wrapped copies of the data key under a previous master key, kept until purged. */
15876
+ retiredWraps: number().int(),
15877
+ recoveryKeyDownloadedAt: number().nullable(),
15878
+ secretCount: number().int()
15879
+ });
15880
+ /**
15881
+ * Whether a stored value is still pointed at by its owner's settings.
15882
+ * `unknown` is a first-class answer: a scope whose blob could not be read is
15883
+ * never reported as an orphan (and never deleted as one).
15884
+ */
15885
+ var KeychainSecretUsageSchema = _enum([
15886
+ "in-use",
15887
+ "orphan",
15888
+ "unknown"
15889
+ ]);
15890
+ var KeychainAdminSecretSchema = KeychainSecretMetaSchema.extend({ usage: KeychainSecretUsageSchema });
15891
+ var KeychainAuditActionSchema = _enum([
15892
+ "put",
15893
+ "delete",
15894
+ "reveal",
15895
+ "rotate-master-key",
15896
+ "purge-retired-keys",
15897
+ "download-recovery-key",
15898
+ "delete-orphans",
15899
+ "reset",
15900
+ "initialize"
15901
+ ]);
15902
+ var KeychainAuditEntrySchema = object({
15903
+ id: string(),
15904
+ at: number(),
15905
+ action: KeychainAuditActionSchema,
15906
+ /** `addon:<id>` | `user:<id>` | `system`. */
15907
+ actor: string(),
15908
+ ref: string().nullable(),
15909
+ detail: string().nullable()
15910
+ });
15911
+ 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({
15912
+ value: string(),
15913
+ version: number().int()
15914
+ }), {
15915
+ kind: "mutation",
15916
+ auth: "admin",
15917
+ caller: "required"
15918
+ }), method(object({ refs: array(string().min(1)).max(1e3) }), object({
15919
+ deleted: array(string()).readonly(),
15920
+ kept: array(object({
15921
+ ref: string(),
15922
+ usage: KeychainSecretUsageSchema
15923
+ })).readonly()
15924
+ }), {
15925
+ kind: "mutation",
15926
+ auth: "admin",
15927
+ caller: "required"
15928
+ }), method(object({}), object({
15929
+ fingerprint: string(),
15930
+ keySource: KeychainKeySourceSchema
15931
+ }), {
15932
+ kind: "mutation",
15933
+ auth: "admin",
15934
+ caller: "required"
15935
+ }), method(object({}), object({ purged: number().int() }), {
15936
+ kind: "mutation",
15937
+ auth: "admin",
15938
+ caller: "required"
15939
+ }), method(object({}), object({
15940
+ key: string(),
15941
+ fingerprint: string(),
15942
+ keySource: KeychainKeySourceSchema
15943
+ }), {
15944
+ kind: "mutation",
15945
+ auth: "admin",
15946
+ caller: "required"
15947
+ }), method(object({ confirm: literal("RESET KEYCHAIN") }), object({
15948
+ archivedCollection: string().nullable(),
15949
+ archived: number().int()
15950
+ }), {
15951
+ kind: "mutation",
15952
+ auth: "admin",
15953
+ caller: "required"
15954
+ });
15098
15955
  method(_void(), object({
15099
15956
  id: string(),
15100
15957
  name: string(),
@@ -15912,6 +16769,10 @@ var DeviceInfoSchema = object({
15912
16769
  isCamera: boolean(),
15913
16770
  /** Current config values — serializable snapshot */
15914
16771
  config: record(string(), unknown()),
16772
+ /** Paths inside `config` the device's schema declares secret (D783) —
16773
+ * what the non-admin redaction withholds. Paths, never values. Absent
16774
+ * when the owning runner has not reported them. */
16775
+ secretConfigPaths: array(string()).optional(),
15915
16776
  /** Hardware + identity blob (manufacturer / model / firmware / sn /
15916
16777
  * uid / mac / …). Populated by drivers; editable via setMetadata. */
15917
16778
  metadata: record(string(), unknown()).nullable().optional(),
@@ -16117,12 +16978,22 @@ var DeviceRegisterPayloadSchema = object({
16117
16978
  * call (workers re-register on init), so probe-driven updates
16118
16979
  * (e.g. PTZ flag flipping after the abilities probe) propagate. */
16119
16980
  features: array(string()).optional(),
16120
- config: record(string(), unknown())
16981
+ config: record(string(), unknown()),
16982
+ /** Config paths the device's own schema DECLARES secret
16983
+ * (`.meta({ secret: true })` — dotted, `[]` for array items, e.g.
16984
+ * `connection.password`). Re-published on every register; the non-admin
16985
+ * redaction withholds them at any depth (D783). Optional: an older runner
16986
+ * omits it and the keyword backstop applies. */
16987
+ secretConfigPaths: array(string()).optional()
16121
16988
  });
16122
16989
  var DeviceRemovePayloadSchema = object({ deviceId: number() });
16123
16990
  var DevicePersistConfigPayloadSchema = object({
16124
16991
  deviceId: number(),
16125
- data: record(string(), unknown())
16992
+ data: record(string(), unknown()),
16993
+ /** The paths the device's schema declares secret (D783), when the caller
16994
+ * knows them — those values are kept in the keychain, not the blob (D787).
16995
+ * Absent: the paths the device last reported at `registerDevice` apply. */
16996
+ secretConfigPaths: array(string()).optional()
16126
16997
  });
16127
16998
  /** What a migration actually did, per switch. `unreachable` is a first-class
16128
16999
  * answer: a camera that could not be asked is not a camera that was silenced. */
@@ -17776,7 +18647,14 @@ var LoginMethodContributionSchema = discriminatedUnion("kind", [
17776
18647
  icon: string().optional(),
17777
18648
  /** Addon-owned HTTP route the button navigates to (GET). */
17778
18649
  startUrl: string(),
17779
- stage: LoginStageEnum
18650
+ stage: LoginStageEnum,
18651
+ /**
18652
+ * The provider names an external identity a signed-in user can link to
18653
+ * their own account (D782): `auth.listLinkableProviders` /
18654
+ * `beginExternalLink` offer exactly the redirect methods that set this,
18655
+ * whatever their id. Absent / false — a login button only (magic-link).
18656
+ */
18657
+ linkable: boolean().optional()
17780
18658
  }),
17781
18659
  object({
17782
18660
  kind: literal("widget"),
@@ -23144,8 +24022,23 @@ var RetrainMacroClassSchema = _enum([
23144
24022
  ]);
23145
24023
  /** A subject to learn, or a phantom to unlearn (taught by OMISSION). */
23146
24024
  var RetrainAnnotationKindSchema = _enum(["subject", "model_error"]);
23147
- /** Did a human draw this box, or did the assist propose it? */
23148
- var RetrainAnnotationSourceSchema = _enum(["operator", "assist"]);
24025
+ /**
24026
+ * Where a box came from. A machine-sourced box is a DRAFT on the page until
24027
+ * the operator confirms it; once saved it keeps its source as provenance.
24028
+ *
24029
+ * - `operator` — drawn by hand.
24030
+ * - `assist` — proposed by a detector run on the copy (the camera's own
24031
+ * model, or a larger teacher model).
24032
+ * - `pipeline` — the box the live pipeline actually drew on that frame, read
24033
+ * back from the track store (never a re-run).
24034
+ * - `llm` — proposed by a vision-capable LLM profile.
24035
+ */
24036
+ var RetrainAnnotationSourceSchema = _enum([
24037
+ "operator",
24038
+ "assist",
24039
+ "pipeline",
24040
+ "llm"
24041
+ ]);
23149
24042
  /** Normalised `[0,1]` rectangle against the FULL frame — the canonical form. */
23150
24043
  var RetrainBboxSchema = object({
23151
24044
  x: number(),
@@ -23163,7 +24056,8 @@ var RetrainBboxSchema = object({
23163
24056
  */
23164
24057
  var RetrainAnnotationSchema = object({
23165
24058
  id: string(),
23166
- trackId: string(),
24059
+ /** The frame's track. Absent on a recording still, which has none. */
24060
+ trackId: string().optional(),
23167
24061
  deviceId: number(),
23168
24062
  /** The COPY in retrain storage — never the source track's media key. */
23169
24063
  mediaKey: string(),
@@ -23212,18 +24106,63 @@ var RetrainFrameCandidateSchema = object({
23212
24106
  * fail, whatever became of the original. */
23213
24107
  copied: boolean()
23214
24108
  });
24109
+ /**
24110
+ * One box the live pipeline drew on a copied frame — what a correction starts
24111
+ * from. Derived when the frame is listed, from the track store: the subject
24112
+ * box recorded on the source media row when there is one, else the track
24113
+ * position nearest the frame's capture time. Never stored as an annotation
24114
+ * until the operator confirms it.
24115
+ */
24116
+ var RetrainPipelineBoxSchema = object({
24117
+ /** Normalised `[0,1]` against the full frame. */
24118
+ bbox: RetrainBboxSchema,
24119
+ macroClass: RetrainMacroClassSchema,
24120
+ label: string().optional(),
24121
+ subLabel: string().optional(),
24122
+ /** The detection score of the position used, when it was recorded. */
24123
+ score: number().optional(),
24124
+ /** The root model that produced the track, when it was recorded. */
24125
+ modelId: string().optional(),
24126
+ trackId: string(),
24127
+ /** `subject-box`: recorded on this very frame. `position`: the track's
24128
+ * position nearest the capture time, `deltaMs` away. */
24129
+ basis: _enum(["subject-box", "position"]),
24130
+ deltaMs: number()
24131
+ });
24132
+ /**
24133
+ * Where a retrain frame came from: a TRACK's stored media (copy-on-select),
24134
+ * or the RECORDING at an instant — the frame a model saw nothing in has no
24135
+ * track at all.
24136
+ */
24137
+ var RetrainFrameOriginSchema = _enum(["track", "recording"]);
23215
24138
  /** A frame the dataset OWNS: bytes copied at selection time. */
23216
24139
  var RetrainFrameSchema = object({
23217
24140
  frameId: string(),
23218
24141
  deviceId: number(),
23219
- trackId: string(),
23220
- /** Provenance only. It may already point at nothing — that is expected. */
23221
- sourceMediaId: number().int(),
24142
+ origin: RetrainFrameOriginSchema,
24143
+ /** The track it was copied from. Absent on a recording still. */
24144
+ trackId: string().optional(),
24145
+ /** Provenance only. It may already point at nothing — that is expected.
24146
+ * Absent on a recording still. */
24147
+ sourceMediaId: number().int().optional(),
24148
+ /** A recording still: the playhead instant it was taken at. */
24149
+ epochMs: number().optional(),
23222
24150
  sourceKind: MediaFileKindEnum,
23223
24151
  sizeBytes: number().int(),
23224
24152
  width: number().int(),
23225
24153
  height: number().int(),
23226
- copiedAt: number()
24154
+ copiedAt: number(),
24155
+ /** When the CAMERA captured the frame. Absent on a copy taken before D785. */
24156
+ capturedAt: number().optional(),
24157
+ /** The operator's "every subject in this frame is boxed". Detector
24158
+ * evaluation keeps only complete frames: an unboxed subject would count as
24159
+ * a false positive. */
24160
+ complete: boolean(),
24161
+ /** The boxes the pipeline drew on this frame. Present on list answers;
24162
+ * empty when nothing the track store holds can be placed on it. */
24163
+ pipelineBoxes: array(RetrainPipelineBoxSchema).readonly().optional(),
24164
+ /** The named collections this frame belongs to. Present on list answers. */
24165
+ collectionIds: array(string()).readonly().optional()
23227
24166
  });
23228
24167
  /** Why a copy-on-select could not be honoured — named, never a silent skip. */
23229
24168
  var RetrainCopyRefusalSchema = _enum([
@@ -23244,6 +24183,53 @@ var RetrainFrameListSchema = object({
23244
24183
  /** What the page pre-selects — the native key frame when one survives. */
23245
24184
  autoPickMediaId: number().int().optional()
23246
24185
  });
24186
+ /**
24187
+ * Which frames an annotated export carries. Every field narrows; an empty
24188
+ * filter is every annotated copy. A dataset VERSION is a frozen `frameIds`
24189
+ * list owned by whoever trains (the training orchestrator), so `frameIds` is
24190
+ * the primitive a version export uses; `batchId` re-selects what an earlier
24191
+ * export carried (`exportedInBatch`).
24192
+ */
24193
+ var RetrainExportFilterSchema = object({
24194
+ deviceIds: array(number()).optional(),
24195
+ frameIds: array(string()).max(2e4).optional(),
24196
+ /** Only frames in at least one of these named collections. */
24197
+ collectionIds: array(string()).max(200).optional(),
24198
+ batchId: string().optional(),
24199
+ /** Only frames the operator marked complete (all subjects boxed). */
24200
+ completeOnly: boolean().optional()
24201
+ });
24202
+ /**
24203
+ * `frames`: the full frame copies, boxes in `annotations.json`.
24204
+ * `crops`: additionally one JPEG per subject, cut with the runtime detail-crop
24205
+ * convention — the classifier layout.
24206
+ */
24207
+ var RetrainExportLayoutSchema = _enum(["frames", "crops"]);
24208
+ /**
24209
+ * Where `writeRetrainArchive` streams the tar: one HTTP request whose body is
24210
+ * the archive, sent chunk by chunk. Covers a presigned PUT and a provider
24211
+ * addon's data-plane upload route alike. Headers are sent verbatim and never
24212
+ * logged.
24213
+ */
24214
+ var RetrainArchiveTargetSchema = object({
24215
+ kind: literal("http"),
24216
+ method: _enum(["PUT", "POST"]),
24217
+ url: string().url(),
24218
+ headers: record(string(), string()).optional()
24219
+ });
24220
+ var RetrainArchiveResultSchema = object({
24221
+ batchId: string(),
24222
+ frames: number().int(),
24223
+ crops: number().int(),
24224
+ annotations: number().int(),
24225
+ /** Frames planned but not shipped (unreadable copy, path too long). */
24226
+ missing: number().int(),
24227
+ bytes: number().int(),
24228
+ /** sha256 of the exact bytes sent. */
24229
+ sha256: string(),
24230
+ /** The target's HTTP status. */
24231
+ status: number().int()
24232
+ });
23247
24233
  /** What the operator asked the assist to look for. */
23248
24234
  var RetrainAssistSubjectSchema = discriminatedUnion("kind", [object({
23249
24235
  kind: literal("package"),
@@ -23273,6 +24259,109 @@ var RetrainAssistResultSchema = discriminatedUnion("kind", [object({
23273
24259
  reason: string(),
23274
24260
  detail: string().optional()
23275
24261
  })]);
24262
+ /** A named set over the retrain corpus — membership only, it owns no pixels. */
24263
+ var RetrainCollectionSchema = object({
24264
+ id: string(),
24265
+ name: string(),
24266
+ createdAt: number(),
24267
+ frameCount: number().int()
24268
+ });
24269
+ var RetrainCollectionCreateResultSchema = discriminatedUnion("kind", [object({
24270
+ kind: literal("created"),
24271
+ collection: RetrainCollectionSchema
24272
+ }), object({
24273
+ kind: literal("refused"),
24274
+ reason: _enum(["empty-name", "duplicate-name"])
24275
+ })]);
24276
+ var RetrainCollectionRenameResultSchema = discriminatedUnion("kind", [object({
24277
+ kind: literal("renamed"),
24278
+ collection: RetrainCollectionSchema
24279
+ }), object({
24280
+ kind: literal("refused"),
24281
+ reason: _enum([
24282
+ "empty-name",
24283
+ "duplicate-name",
24284
+ "unknown-collection"
24285
+ ])
24286
+ })]);
24287
+ /**
24288
+ * The outcome of adding the frame at a recording instant. Every refusal is
24289
+ * named: `still-unavailable` (not recording, a gap, or an undecodable frame —
24290
+ * `detail` says which), `still-busy` (the recorder is at its decode bound; try
24291
+ * again), `unreadable-image`, `write-failed`, `unknown-collection`.
24292
+ */
24293
+ var AddRetrainFrameFromRecordingResultSchema = discriminatedUnion("kind", [object({
24294
+ kind: literal("added"),
24295
+ collectionId: string(),
24296
+ frame: RetrainFrameSchema,
24297
+ /** The moment was already in the dataset; nothing was decoded. */
24298
+ reused: boolean()
24299
+ }), object({
24300
+ kind: literal("refused"),
24301
+ reason: _enum([
24302
+ "unknown-collection",
24303
+ "still-unavailable",
24304
+ "still-busy",
24305
+ "unreadable-image",
24306
+ "write-failed"
24307
+ ]),
24308
+ detail: string().optional()
24309
+ })]);
24310
+ /** A vision LLM profile the AI assist may use, and where it sends the frame. */
24311
+ var RetrainAssistProfileSchema = object({
24312
+ id: string(),
24313
+ name: string(),
24314
+ model: string(),
24315
+ kind: string(),
24316
+ /** `true` when the frame would leave the house (a cloud vendor, or an
24317
+ * endpoint that is not on the local network). The page confirms each such
24318
+ * run, and the provider refuses one without `allowCloud`. */
24319
+ leavesHouse: boolean()
24320
+ });
24321
+ /** A box the AI assist proposed: a draft, plus the model's own reason. */
24322
+ var RetrainLlmProposalSchema = RetrainAnnotationDraftSchema.extend({ reason: string() });
24323
+ /** What the AI assist said about a box that was already on the canvas. */
24324
+ var RetrainLlmReviewSchema = object({
24325
+ /** The `ref` the page sent the box under. */
24326
+ ref: string(),
24327
+ verdict: _enum([
24328
+ "confirmed",
24329
+ "wrong-class",
24330
+ "not-an-object",
24331
+ "uncertain"
24332
+ ]),
24333
+ suggestedClass: RetrainMacroClassSchema.optional(),
24334
+ reason: string()
24335
+ });
24336
+ /**
24337
+ * The AI assist's answer. `proposed` carries drafts (`source: 'llm'`, the
24338
+ * model and its confidence) and reviews; items the model got wrong (a box off
24339
+ * the frame, an unasked class, an unknown ref) are dropped and counted in
24340
+ * `rejected`. A whole answer that is not the schema is `malformed-answer`.
24341
+ */
24342
+ var RetrainLlmAssistResultSchema = discriminatedUnion("kind", [object({
24343
+ kind: literal("proposed"),
24344
+ profileId: string(),
24345
+ model: string(),
24346
+ /** The frame was sent outside the house for this answer. */
24347
+ leftHouse: boolean(),
24348
+ proposals: array(RetrainLlmProposalSchema).readonly(),
24349
+ reviews: array(RetrainLlmReviewSchema).readonly(),
24350
+ rejected: number().int()
24351
+ }), object({
24352
+ kind: literal("refused"),
24353
+ reason: _enum([
24354
+ "unknown-frame",
24355
+ "unreadable-frame",
24356
+ "unknown-profile",
24357
+ "profile-disabled",
24358
+ "no-vision",
24359
+ "cloud-not-confirmed",
24360
+ "llm-error",
24361
+ "malformed-answer"
24362
+ ]),
24363
+ detail: string().optional()
24364
+ })]);
23276
24365
  /** The outcome of a lifecycle move owned by the retrain page. */
23277
24366
  var RetrainTransitionResultSchema = object({
23278
24367
  trackId: string(),
@@ -23990,7 +25079,7 @@ DeviceType.Camera, method(object({ deviceId: number() }), array(TrackSchema).rea
23990
25079
  auth: "admin"
23991
25080
  }), method(object({
23992
25081
  deviceId: number(),
23993
- trackId: string(),
25082
+ trackId: string().optional(),
23994
25083
  frameId: string()
23995
25084
  }), object({
23996
25085
  removed: boolean(),
@@ -24007,7 +25096,8 @@ DeviceType.Camera, method(object({ deviceId: number() }), array(TrackSchema).rea
24007
25096
  auth: "admin"
24008
25097
  }), method(object({
24009
25098
  deviceId: number(),
24010
- trackId: string(),
25099
+ /** The frame's track, when it has one — checked against the frame. */
25100
+ trackId: string().optional(),
24011
25101
  frameId: string(),
24012
25102
  subject: RetrainAssistSubjectSchema,
24013
25103
  /** Which node runs it. Absent ⇒ wherever an unowned call lands. */
@@ -24015,12 +25105,16 @@ DeviceType.Camera, method(object({ deviceId: number() }), array(TrackSchema).rea
24015
25105
  }), RetrainAssistResultSchema, {
24016
25106
  kind: "mutation",
24017
25107
  auth: "admin"
24018
- }), method(object({ trackId: string() }), array(RetrainAnnotationSchema).readonly(), {
25108
+ }), method(object({
25109
+ trackId: string().optional(),
25110
+ frameIds: array(string()).max(2e3).optional()
25111
+ }), array(RetrainAnnotationSchema).readonly(), {
24019
25112
  kind: "query",
24020
25113
  auth: "admin"
24021
25114
  }), method(object({
24022
25115
  deviceId: number(),
24023
- trackId: string(),
25116
+ /** The frame's track, when it has one — checked against the frame. */
25117
+ trackId: string().optional(),
24024
25118
  frameId: string(),
24025
25119
  annotations: array(RetrainAnnotationDraftSchema)
24026
25120
  }), array(RetrainAnnotationSchema).readonly(), {
@@ -24038,7 +25132,91 @@ DeviceType.Camera, method(object({ deviceId: number() }), array(TrackSchema).rea
24038
25132
  }), RetrainTransitionResultSchema, {
24039
25133
  kind: "mutation",
24040
25134
  auth: "admin"
24041
- }), method(object({ deviceIds: array(number()).optional() }), object({ url: string() }), {
25135
+ }), method(RetrainExportFilterSchema.extend({ layout: RetrainExportLayoutSchema.optional() }), object({ url: string() }), {
25136
+ kind: "query",
25137
+ auth: "admin"
25138
+ }), method(object({
25139
+ deviceId: number(),
25140
+ trackId: string().optional(),
25141
+ frameId: string(),
25142
+ complete: boolean()
25143
+ }), RetrainFrameSchema, {
25144
+ kind: "mutation",
25145
+ auth: "admin"
25146
+ }), method(object({
25147
+ filter: RetrainExportFilterSchema,
25148
+ layout: RetrainExportLayoutSchema,
25149
+ target: RetrainArchiveTargetSchema
25150
+ }), RetrainArchiveResultSchema, {
25151
+ kind: "mutation",
25152
+ auth: "admin",
25153
+ timeoutMs: 60 * 6e4
25154
+ }), method(object({}), array(RetrainAssistProfileSchema).readonly(), {
25155
+ kind: "query",
25156
+ auth: "admin"
25157
+ }), method(object({
25158
+ deviceId: number(),
25159
+ trackId: string().optional(),
25160
+ frameId: string(),
25161
+ profileId: string(),
25162
+ classes: array(RetrainMacroClassSchema).min(1),
25163
+ boxes: array(object({
25164
+ ref: string(),
25165
+ bbox: RetrainBboxSchema,
25166
+ macroClass: RetrainMacroClassSchema,
25167
+ label: string().optional()
25168
+ })).max(50),
25169
+ allowCloud: boolean()
25170
+ }), RetrainLlmAssistResultSchema, {
25171
+ kind: "mutation",
25172
+ auth: "admin",
25173
+ timeoutMs: 18e4
25174
+ }), method(object({ name: string().max(120) }), RetrainCollectionCreateResultSchema, {
25175
+ kind: "mutation",
25176
+ auth: "admin"
25177
+ }), method(object({}), array(RetrainCollectionSchema).readonly(), {
25178
+ kind: "query",
25179
+ auth: "admin"
25180
+ }), method(object({
25181
+ id: string(),
25182
+ name: string().max(120)
25183
+ }), RetrainCollectionRenameResultSchema, {
25184
+ kind: "mutation",
25185
+ auth: "admin"
25186
+ }), method(object({ id: string() }), object({
25187
+ deleted: boolean(),
25188
+ removedFrames: number().int()
25189
+ }), {
25190
+ kind: "mutation",
25191
+ auth: "admin"
25192
+ }), method(object({
25193
+ collectionId: string(),
25194
+ deviceId: number(),
25195
+ epochMs: number()
25196
+ }), AddRetrainFrameFromRecordingResultSchema, {
25197
+ kind: "mutation",
25198
+ auth: "admin",
25199
+ timeoutMs: 6e4
25200
+ }), method(object({
25201
+ collectionId: string(),
25202
+ frameIds: array(string()).min(1).max(500)
25203
+ }), object({
25204
+ added: number().int(),
25205
+ alreadyMember: number().int(),
25206
+ unknownFrameIds: array(string()).readonly()
25207
+ }), {
25208
+ kind: "mutation",
25209
+ auth: "admin"
25210
+ }), method(object({
25211
+ collectionId: string(),
25212
+ frameId: string()
25213
+ }), object({
25214
+ removed: boolean(),
25215
+ deletedFrame: boolean()
25216
+ }), {
25217
+ kind: "mutation",
25218
+ auth: "admin"
25219
+ }), method(object({ collectionId: string() }), array(RetrainFrameSchema).readonly(), {
24042
25220
  kind: "query",
24043
25221
  auth: "admin"
24044
25222
  }), method(object({
@@ -24866,13 +26044,31 @@ var RecommendationReasonSchema = discriminatedUnion("kind", [
24866
26044
  object({
24867
26045
  kind: literal("no-family-build"),
24868
26046
  format: ModelFormatSchema$1
26047
+ }),
26048
+ object({
26049
+ kind: literal("kept-custom-pin"),
26050
+ modelId: string(),
26051
+ deviceKey: string()
24869
26052
  })
24870
26053
  ]);
26054
+ /** Why a device is left exactly as it is, whatever the plan would pick (D786). */
26055
+ var KeptReasonSchema = _enum(["custom-pin"]);
26056
+ /**
26057
+ * Where a choice's latency came from (D791): this node's own benchmark
26058
+ * result, or the hardware-class table. Measured on the node beats the table.
26059
+ */
26060
+ var LatencySourceSchema = discriminatedUnion("kind", [object({ kind: literal("table") }), object({
26061
+ kind: literal("node-benchmark"),
26062
+ resultId: string(),
26063
+ measuredAtMs: number()
26064
+ })]);
24871
26065
  var DeviceModelChoiceSchema = object({
24872
26066
  modelId: string(),
24873
26067
  p50Ms: number(),
24874
26068
  fps: number(),
24875
26069
  measured: boolean(),
26070
+ /** Absent on a plan from before D791 — read as the table. */
26071
+ source: LatencySourceSchema.optional(),
24876
26072
  /** Parallel sessions at the camera rate with the headroom kept free: ⌊fps × (1 − headroom) / camera fps⌋. */
24877
26073
  capacitySessions: number(),
24878
26074
  personRecall: number()
@@ -24883,6 +26079,8 @@ var DeviceRecommendationSchema = object({
24883
26079
  enabled: boolean(),
24884
26080
  available: boolean(),
24885
26081
  chosen: DeviceModelChoiceSchema.nullable(),
26082
+ /** Present when the device is kept as is: Apply writes nothing on it. */
26083
+ kept: KeptReasonSchema.optional(),
24886
26084
  reasons: array(RecommendationReasonSchema).readonly()
24887
26085
  });
24888
26086
  var NodeRecommendationSchema = object({
@@ -24936,6 +26134,17 @@ var AutoTuneWarningSchema = discriminatedUnion("kind", [
24936
26134
  kind: literal("coral-enabled"),
24937
26135
  nodeId: string(),
24938
26136
  deviceKey: string()
26137
+ }),
26138
+ object({
26139
+ kind: literal("custom-pin-kept"),
26140
+ nodeId: string(),
26141
+ deviceKey: string().nullable(),
26142
+ deviceId: number().nullable(),
26143
+ modelId: string()
26144
+ }),
26145
+ object({
26146
+ kind: literal("custom-registry-unreadable"),
26147
+ error: string()
24939
26148
  })
24940
26149
  ]);
24941
26150
  var AutoTuneOptionsSchema = object({
@@ -25126,13 +26335,14 @@ var SuggestSharesResultSchema = object({
25126
26335
  nodes: array(SuggestedNodeShareSchema).readonly()
25127
26336
  });
25128
26337
  /**
25129
- * Where the node and device shares come from (D750, D755).
25130
- * - `hardware` (default) — computed by the system from each node's static
25131
- * capacity (its accelerators at their Auto tune models, from the measured
25132
- * latency table), recomputed whenever a node or device joins, leaves, is
25133
- * switched on or off, or changes model. No load input.
25134
- * - `benchmark` — the same, from each node's measured capacity (node
25135
- * benchmark results); a node without results falls back to `hardware`.
26338
+ * Where the node and device shares come from (D750, D755, D791).
26339
+ * - `benchmark` (default since D791) — each node's capacity as its own node
26340
+ * benchmark measured it; a node without fresh results is sized from
26341
+ * `hardware` and says so. Measured on the node beats the table.
26342
+ * - `hardware` — computed from each node's static capacity only (its
26343
+ * accelerators at their Auto tune models, from the latency table).
26344
+ * Both are recomputed whenever a node or device joins, leaves, is switched
26345
+ * on or off, changes model, or a benchmark result is written. No load input.
25136
26346
  * - `manual` — the operator's stored sliders.
25137
26347
  * Either way a node or device that cannot take sessions gives its share to
25138
26348
  * the others in proportion; the stored values are never rewritten for that.
@@ -25142,7 +26352,7 @@ var ShareSourceSchema = _enum([
25142
26352
  "benchmark",
25143
26353
  "manual"
25144
26354
  ]);
25145
- var DEFAULT_SHARE_SOURCE = "hardware";
26355
+ var DEFAULT_SHARE_SOURCE = "benchmark";
25146
26356
  /** `hardware` or `benchmark`: the system computes the shares. */
25147
26357
  function isAutomaticShareSource(source) {
25148
26358
  return source !== "manual";
@@ -25156,6 +26366,7 @@ var SHARE_SOURCE_SETTING_KEY = "shareSource";
25156
26366
  * D755 split it) reads as `hardware` — that is what it computed.
25157
26367
  */
25158
26368
  function parseShareSource(value) {
26369
+ if (value === "auto") return "hardware";
25159
26370
  const parsed = ShareSourceSchema.safeParse(value);
25160
26371
  return parsed.success ? parsed.data : DEFAULT_SHARE_SOURCE;
25161
26372
  }
@@ -25198,6 +26409,363 @@ var EffectiveSharesSchema = object({
25198
26409
  nodeWeights: record(string(), number()),
25199
26410
  deviceWeights: record(string(), record(string(), number()))
25200
26411
  });
26412
+ /**
26413
+ * Wire shapes of the node benchmark (D791): the operator's plan, the
26414
+ * preflight the confirmation shows, the run the orchestrator owns, what the
26415
+ * measuring runner reports, and the result rows Auto tune and the automatic
26416
+ * shares read.
26417
+ *
26418
+ * ONE node at a time, owned by the pipeline orchestrator. While a run is
26419
+ * active the cluster is in BENCHMARK MODE for that node: it is not a
26420
+ * detection candidate, camera pins naming it are ignored so its sessions move
26421
+ * elsewhere, and both come back when the mode ends — after a crash or a hub
26422
+ * restart too (a persisted manifest, reconciled against the registry).
26423
+ *
26424
+ * Two measurements, both on the node, driven by its own runner (frames never
26425
+ * cross a process at frame rate, D9):
26426
+ * - `device` — one accelerator × one candidate detector, root only, open
26427
+ * loop: how many streams at the camera rate it carries inside the frame
26428
+ * interval;
26429
+ * - `pipeline` — the chosen detector plus the operator's detail steps
26430
+ * (faces, plates, classifiers, CLIP…), on varied frames from the bench
26431
+ * clip: how many parallel sessions the node carries before latency, drops,
26432
+ * CPU, RAM or the event loop give out.
26433
+ */
26434
+ /** Defaults and bounds, one place. */
26435
+ var NODE_BENCHMARK_DEFAULTS = {
26436
+ fpsPerStream: 10,
26437
+ budgetMinutes: 45,
26438
+ minBudgetMinutes: 10,
26439
+ maxBudgetMinutes: 120,
26440
+ /** A result older than this no longer beats the table. */
26441
+ staleAfterDays: 30,
26442
+ /** Warm-in per ramp step (not accounted). */
26443
+ settleSec: 5,
26444
+ /** Accounted window per ramp step. */
26445
+ measureSec: 20,
26446
+ /** Longest the drain may take before the run is refused. */
26447
+ drainTimeoutSec: 120,
26448
+ /** Candidate models per device in one run. */
26449
+ maxModelsPerDevice: 6
26450
+ };
26451
+ /** The two measurements a run can make. */
26452
+ var NodeBenchmarkPhaseSchema = _enum(["device", "pipeline"]);
26453
+ var NodeBenchmarkDevicePlanSchema = object({
26454
+ /** `<backend>:<device>` — `openvino:npu`, `coreml:all`, `cpu`. */
26455
+ deviceKey: string().min(1),
26456
+ /**
26457
+ * Root detector candidates for the `device` measurement, in the order
26458
+ * measured. The FIRST is the one the `pipeline` measurement runs.
26459
+ */
26460
+ modelIds: array(string().min(1)).min(1).max(NODE_BENCHMARK_DEFAULTS.maxModelsPerDevice)
26461
+ });
26462
+ /** What the operator confirmed. */
26463
+ var NodeBenchmarkPlanSchema = object({
26464
+ nodeId: string().min(1),
26465
+ devices: array(NodeBenchmarkDevicePlanSchema).min(1),
26466
+ /** Which measurements run. At least one. */
26467
+ phases: array(NodeBenchmarkPhaseSchema).min(1).default(["device", "pipeline"]),
26468
+ /**
26469
+ * Detail steps (catalog step ids under the root: faces, plates,
26470
+ * classifiers, CLIP…) the `pipeline` measurement enables. The root
26471
+ * detector always runs.
26472
+ */
26473
+ steps: array(string().min(1)).default([]),
26474
+ /** Offered rate of one stream / session. */
26475
+ fpsPerStream: number().int().min(1).max(30).default(NODE_BENCHMARK_DEFAULTS.fpsPerStream),
26476
+ /** Hard deadline: the mode ends and the cluster is restored when it passes. */
26477
+ budgetMinutes: number().int().min(NODE_BENCHMARK_DEFAULTS.minBudgetMinutes).max(NODE_BENCHMARK_DEFAULTS.maxBudgetMinutes).default(NODE_BENCHMARK_DEFAULTS.budgetMinutes),
26478
+ /** Keep the models the run downloaded (default: delete what was not installed before). */
26479
+ keepDownloadedModels: boolean().default(false),
26480
+ /** Start although the other nodes' capacity does not cover the sessions running now. */
26481
+ acceptInsufficientHeadroom: boolean().default(false)
26482
+ });
26483
+ var NodeBenchmarkRefusalSchema = object({
26484
+ kind: _enum([
26485
+ "benchmark-active",
26486
+ "node-offline",
26487
+ "unknown-device",
26488
+ "no-phase",
26489
+ "insufficient-headroom"
26490
+ ]),
26491
+ detail: string(),
26492
+ overridable: boolean()
26493
+ });
26494
+ /** A camera whose detection leaves the node for the run. */
26495
+ var NodeBenchmarkMovingCameraSchema = object({
26496
+ deviceId: number(),
26497
+ /** Placed on the node now (a standing attach or a running session). */
26498
+ placedOnNode: boolean(),
26499
+ /** A node pin naming this node, ignored for the run. */
26500
+ pinnedToNode: boolean()
26501
+ });
26502
+ /** A camera that cannot leave: no other node can obtain its frames. */
26503
+ var NodeBenchmarkStayingCameraSchema = object({
26504
+ deviceId: number(),
26505
+ reason: literal("only-frame-source")
26506
+ });
26507
+ var NodeBenchmarkPreflightSchema = object({
26508
+ nodeId: string(),
26509
+ online: boolean(),
26510
+ moving: array(NodeBenchmarkMovingCameraSchema).readonly(),
26511
+ /** Keep running on the node during the run; the result is flagged contaminated. */
26512
+ staying: array(NodeBenchmarkStayingCameraSchema).readonly(),
26513
+ estimatedDurationMs: number(),
26514
+ /** The run ends and the cluster is restored at the latest after this long. */
26515
+ budgetMs: number(),
26516
+ phases: array(NodeBenchmarkPhaseSchema).readonly(),
26517
+ steps: array(string()).readonly(),
26518
+ devices: array(NodeBenchmarkDevicePlanSchema).readonly(),
26519
+ /** Parallel sessions the cluster runs now, and what the other nodes can carry. */
26520
+ headroom: object({
26521
+ sessionsNow: number(),
26522
+ otherNodesCapacitySessions: number().nullable()
26523
+ }),
26524
+ refusals: array(NodeBenchmarkRefusalSchema).readonly(),
26525
+ canStart: boolean()
26526
+ });
26527
+ /** Where the measuring runner is. */
26528
+ var NodeBenchProgressSchema = object({
26529
+ phase: _enum([
26530
+ "preparing",
26531
+ "device",
26532
+ "pipeline",
26533
+ "cleanup"
26534
+ ]),
26535
+ deviceKey: string().nullable(),
26536
+ modelId: string().nullable(),
26537
+ /** Streams (device) or sessions (pipeline) of the step running now. */
26538
+ load: number().nullable(),
26539
+ cellsDone: number(),
26540
+ cellsTotal: number(),
26541
+ message: string()
26542
+ });
26543
+ /** What limited a ramp. `none` = the ramp ran out of steps still passing. */
26544
+ var NodeBenchLimitSchema = _enum([
26545
+ "p95",
26546
+ "drops",
26547
+ "errors",
26548
+ "stall",
26549
+ "cpu",
26550
+ "ram",
26551
+ "none"
26552
+ ]);
26553
+ var NodeBenchRampStepSchema = object({
26554
+ load: number(),
26555
+ pass: boolean(),
26556
+ p95Ms: number(),
26557
+ dropRatio: number(),
26558
+ /** Completed frames per second over the accounted window. */
26559
+ fps: number(),
26560
+ cpuPct: number().nullable(),
26561
+ ramFreePct: number().nullable(),
26562
+ failReasons: array(string()).readonly()
26563
+ });
26564
+ var NodeBenchCellSchema = discriminatedUnion("phase", [object({
26565
+ phase: literal("device"),
26566
+ deviceKey: string(),
26567
+ modelId: string(),
26568
+ /** Single-stream latency (the K = 1 step). */
26569
+ p50Ms: number(),
26570
+ p95Ms: number(),
26571
+ p99Ms: number(),
26572
+ /** Frames per second sustained at the last passing step. */
26573
+ fps: number(),
26574
+ /** Streams at the camera rate the device carried inside the frame interval; 0 = not even one. */
26575
+ kMax: number(),
26576
+ limitedBy: NodeBenchLimitSchema,
26577
+ /** First call (model load + warm-up). */
26578
+ warmupMs: number(),
26579
+ ramp: array(NodeBenchRampStepSchema).readonly()
26580
+ }), object({
26581
+ phase: literal("pipeline"),
26582
+ deviceKey: string(),
26583
+ modelId: string(),
26584
+ steps: array(string()).readonly(),
26585
+ /** Parallel sessions at the camera rate the node carried; 0 = not even one. */
26586
+ maxSessions: number(),
26587
+ limitedBy: NodeBenchLimitSchema,
26588
+ /** Detail steps that produced at least one result on the clip — a step that never fired measured nothing. */
26589
+ stepsFired: array(string()).readonly(),
26590
+ warmupMs: number(),
26591
+ ramp: array(NodeBenchRampStepSchema).readonly()
26592
+ })]);
26593
+ /**
26594
+ * What a result was measured ON. A result whose fingerprint no longer matches
26595
+ * the node no longer beats the table.
26596
+ */
26597
+ var NodeBenchFingerprintSchema = object({
26598
+ pipelineVersion: string().nullable(),
26599
+ cpuModel: string().nullable(),
26600
+ ramGb: number().nullable(),
26601
+ os: string().nullable(),
26602
+ /** Per device: the pool the cells ran on (runtime + tuning). */
26603
+ pools: record(string(), object({
26604
+ runtime: string(),
26605
+ numWorkers: number(),
26606
+ concurrency: number(),
26607
+ batchMode: string()
26608
+ })).default({})
26609
+ });
26610
+ /** The frames the run used. */
26611
+ var NodeBenchClipSchema = object({
26612
+ /** `camstack-bench-v1` or `reference-images` (the fallback when the clip is not reachable). */
26613
+ id: string(),
26614
+ revision: string().nullable(),
26615
+ frames: number(),
26616
+ width: number(),
26617
+ height: number()
26618
+ });
26619
+ var NodeBenchRunnerInputSchema = object({
26620
+ runId: string().min(1),
26621
+ devices: array(NodeBenchmarkDevicePlanSchema).min(1),
26622
+ phases: array(NodeBenchmarkPhaseSchema).min(1),
26623
+ /** The full detail step tree root for the `pipeline` measurement is built on the node from these ids. */
26624
+ steps: array(string()).default([]),
26625
+ fpsPerStream: number().int().min(1).max(30),
26626
+ /** The runner stops on its own past this instant, whatever the hub does. */
26627
+ deadlineAtMs: number(),
26628
+ settleSec: number().min(0).max(60).default(NODE_BENCHMARK_DEFAULTS.settleSec),
26629
+ measureSec: number().min(2).max(300).default(NODE_BENCHMARK_DEFAULTS.measureSec),
26630
+ keepDownloadedModels: boolean().default(false)
26631
+ });
26632
+ var NodeBenchRunnerStateSchema = _enum([
26633
+ "running",
26634
+ "done",
26635
+ "failed",
26636
+ "aborted"
26637
+ ]);
26638
+ var NodeBenchRunnerStatusSchema = object({
26639
+ runId: string(),
26640
+ nodeId: string(),
26641
+ state: NodeBenchRunnerStateSchema,
26642
+ progress: NodeBenchProgressSchema.nullable(),
26643
+ /** Every finished cell so far — a run stopped half way keeps what it measured. */
26644
+ cells: array(NodeBenchCellSchema).readonly(),
26645
+ fingerprint: NodeBenchFingerprintSchema.nullable(),
26646
+ clip: NodeBenchClipSchema.nullable(),
26647
+ /** Cameras seen running on the node during the run (they share its pools). */
26648
+ activeCameraIds: array(number()).readonly(),
26649
+ /** Models the run downloaded and then deleted (not installed before). */
26650
+ deletedModels: array(string()).readonly(),
26651
+ error: string().nullable(),
26652
+ startedAtMs: number(),
26653
+ finishedAtMs: number().nullable()
26654
+ });
26655
+ var NodeBenchmarkStateSchema = _enum([
26656
+ "draining",
26657
+ "measuring",
26658
+ "restoring",
26659
+ "done",
26660
+ "failed",
26661
+ "aborted"
26662
+ ]);
26663
+ /** Why a run ended. */
26664
+ var NodeBenchmarkEndReasonSchema = _enum([
26665
+ "completed",
26666
+ "operator-abort",
26667
+ "deadline",
26668
+ "node-lost",
26669
+ "runner-lost",
26670
+ "hub-restart",
26671
+ "drain-timeout",
26672
+ "failed"
26673
+ ]);
26674
+ var NodeBenchmarkRunSchema = object({
26675
+ runId: string(),
26676
+ nodeId: string(),
26677
+ state: NodeBenchmarkStateSchema,
26678
+ plan: NodeBenchmarkPlanSchema,
26679
+ startedAtMs: number(),
26680
+ deadlineAtMs: number(),
26681
+ finishedAtMs: number().nullable(),
26682
+ endReason: NodeBenchmarkEndReasonSchema.nullable(),
26683
+ error: string().nullable(),
26684
+ /** Cameras moved off the node for the run. */
26685
+ moved: array(number()).readonly(),
26686
+ /** Cameras that stayed (their only frame source is the node). */
26687
+ staying: array(number()).readonly(),
26688
+ /** Something else ran on the node's pools during the run. */
26689
+ contaminated: boolean(),
26690
+ progress: NodeBenchProgressSchema.nullable(),
26691
+ resultIds: array(string()).readonly()
26692
+ });
26693
+ /** `getNodeBenchmarkStatus`: the active run (or the last one), and whether benchmark mode is on. */
26694
+ var NodeBenchmarkStatusSchema = object({
26695
+ /** The node the cluster is in benchmark mode for; null = none. */
26696
+ modeNodeId: string().nullable(),
26697
+ run: NodeBenchmarkRunSchema.nullable()
26698
+ });
26699
+ /**
26700
+ * One stored measurement — the only authority Auto tune and the automatic
26701
+ * shares read measured capacity from. Written by the orchestrator when a run
26702
+ * ends, never by anyone else.
26703
+ */
26704
+ var NodeBenchmarkResultSchema = object({
26705
+ id: string(),
26706
+ runId: string(),
26707
+ nodeId: string(),
26708
+ cell: NodeBenchCellSchema,
26709
+ fpsPerStream: number(),
26710
+ fingerprint: NodeBenchFingerprintSchema.nullable(),
26711
+ clip: NodeBenchClipSchema.nullable(),
26712
+ /** Other work shared the node's pools (a camera that could not move, or the hub's own duties). */
26713
+ contaminated: boolean(),
26714
+ stayingCameras: array(number()).readonly(),
26715
+ /** The run ended early; the cell itself completed. */
26716
+ partialRun: boolean(),
26717
+ measuredAtMs: number()
26718
+ });
26719
+ var ListNodeBenchmarkResultsInputSchema = object({
26720
+ nodeId: string().optional(),
26721
+ deviceKey: string().optional()
26722
+ });
26723
+ /** A result with its standing today: does it still beat the table? */
26724
+ var NodeBenchmarkResultViewSchema = NodeBenchmarkResultSchema.extend({
26725
+ /** `fresh` beats the table; `stale` falls back to it; `superseded` = a newer row covers the same cell. */
26726
+ standing: _enum([
26727
+ "fresh",
26728
+ "stale",
26729
+ "superseded"
26730
+ ]),
26731
+ staleWhy: _enum([
26732
+ "age",
26733
+ "runtime",
26734
+ "pool-tuning"
26735
+ ]).nullable()
26736
+ });
26737
+ /** The manifest persisted BEFORE benchmark mode changes anything; its presence means "restore". */
26738
+ var NodeBenchmarkManifestSchema = object({
26739
+ runId: string(),
26740
+ nodeId: string(),
26741
+ startedAtMs: number(),
26742
+ deadlineAtMs: number(),
26743
+ plan: NodeBenchmarkPlanSchema,
26744
+ moved: array(number()).readonly(),
26745
+ staying: array(number()).readonly()
26746
+ });
26747
+ /** Candidate detectors per device, for the plan editor. */
26748
+ var NodeBenchmarkCandidatesSchema = object({
26749
+ nodeId: string(),
26750
+ reachable: boolean(),
26751
+ devices: array(object({
26752
+ deviceKey: string(),
26753
+ enabled: boolean(),
26754
+ available: boolean(),
26755
+ /** Ordered: what Auto tune would pick first, then the rest of the family by accuracy. */
26756
+ suggested: array(string()).readonly(),
26757
+ /** Every root detector the device has a build for. */
26758
+ all: array(string()).readonly(),
26759
+ /** The model the device runs now, when pinned. */
26760
+ currentModelId: string().nullable()
26761
+ })).readonly(),
26762
+ /** Detail steps the node's catalog offers under the root (for the `pipeline` measurement). */
26763
+ steps: array(object({
26764
+ id: string(),
26765
+ name: string(),
26766
+ slot: string()
26767
+ })).readonly()
26768
+ });
25201
26769
  object({
25202
26770
  activeCameras: number(),
25203
26771
  throttledCameras: number(),
@@ -26514,7 +28082,16 @@ method(RunnerCameraConfigSchema, object({ success: literal(true) }), { kind: "mu
26514
28082
  }), ParkedTrackFrameSchema.nullable()), method(object({
26515
28083
  deviceId: number(),
26516
28084
  trackId: string()
26517
- }), object({ released: number().int().nonnegative() }), { kind: "mutation" });
28085
+ }), object({ released: number().int().nonnegative() }), { kind: "mutation" }), method(NodeBenchRunnerInputSchema, object({
28086
+ accepted: literal(true),
28087
+ runId: string()
28088
+ }), {
28089
+ kind: "mutation",
28090
+ auth: "admin"
28091
+ }), method(object({ runId: string() }), NodeBenchRunnerStatusSchema.nullable(), { auth: "admin" }), method(object({ runId: string() }), object({ aborted: boolean() }), {
28092
+ kind: "mutation",
28093
+ auth: "admin"
28094
+ });
26518
28095
  var CameraPipelineConfigSchema = object({
26519
28096
  engine: PipelineEngineChoiceSchema.optional(),
26520
28097
  steps: array(PipelineStepInputSchema).readonly(),
@@ -27440,6 +29017,51 @@ var pipelineOrchestratorCapability = {
27440
29017
  kind: "mutation",
27441
29018
  auth: "admin"
27442
29019
  }),
29020
+ /**
29021
+ * Node benchmark — what a run of this plan would do, without doing it:
29022
+ * the cameras that leave the node, those that cannot (the node is their
29023
+ * only frame source; they stay and flag the run), the estimated duration
29024
+ * and every reason it may not start. The confirmation shows this.
29025
+ */
29026
+ getNodeBenchmarkPreflight: method(NodeBenchmarkPlanSchema, NodeBenchmarkPreflightSchema, { auth: "admin" }),
29027
+ /**
29028
+ * Node benchmark — start ONE run (cluster-wide lock). The orchestrator
29029
+ * persists a restore manifest, enters benchmark mode for the node (out of
29030
+ * placement, its camera pins ignored, nothing rewritten), drains it, drives
29031
+ * the node's own runner, then restores — always, including after an
29032
+ * abort, a lost node or a hub restart. Answers at once; poll
29033
+ * `getNodeBenchmarkStatus`. Refused runs carry the preflight.
29034
+ */
29035
+ startNodeBenchmark: method(NodeBenchmarkPlanSchema, discriminatedUnion("kind", [object({
29036
+ kind: literal("started"),
29037
+ runId: string()
29038
+ }), object({
29039
+ kind: literal("refused"),
29040
+ preflight: NodeBenchmarkPreflightSchema
29041
+ })]), {
29042
+ kind: "mutation",
29043
+ auth: "admin"
29044
+ }),
29045
+ /** Node benchmark — the active run (or the last one) and the node in benchmark mode. */
29046
+ getNodeBenchmarkStatus: method(_void(), NodeBenchmarkStatusSchema, { auth: "admin" }),
29047
+ /** Node benchmark — stop the active run; the cluster is restored as it was. */
29048
+ abortNodeBenchmark: method(object({ runId: string() }), object({ aborted: boolean() }), {
29049
+ kind: "mutation",
29050
+ auth: "admin"
29051
+ }),
29052
+ /**
29053
+ * Node benchmark — root detector candidates per device (what Auto tune
29054
+ * would pick first, then the family) and the detail steps the node's
29055
+ * catalog offers, for the plan editor.
29056
+ */
29057
+ getNodeBenchmarkCandidates: method(object({ nodeId: string() }), NodeBenchmarkCandidatesSchema, { auth: "admin" }),
29058
+ /** Node benchmark — stored results, newest first, each with its standing today. */
29059
+ listNodeBenchmarkResults: method(ListNodeBenchmarkResultsInputSchema, array(NodeBenchmarkResultViewSchema).readonly(), { auth: "admin" }),
29060
+ /** Node benchmark — delete one result; Auto tune and the shares stop reading it. */
29061
+ deleteNodeBenchmarkResult: method(object({ id: string() }), object({ deleted: boolean() }), {
29062
+ kind: "mutation",
29063
+ auth: "admin"
29064
+ }),
27443
29065
  /** Read one camera's settings. Null when never touched (inherits agent defaults fully). */
27444
29066
  getCameraSettings: method(object({ deviceId: number() }), CameraPipelineSettingsSchema.nullable()),
27445
29067
  /** Set or clear the 3-state toggle for one (camera, addonId). Pass `enabled: null` to clear and revert to agent default. */
@@ -28192,7 +29814,19 @@ var SsoBridgeClaimsSchema = object({
28192
29814
  * editing a descriptor must not retroactively extend or shorten a link the
28193
29815
  * operator already consented to.
28194
29816
  */
28195
- refreshTtl: union([number().int().positive(), literal("never")]).optional()
29817
+ refreshTtl: union([number().int().positive(), literal("never")]).optional(),
29818
+ /**
29819
+ * Opaque handle of a hub-side LINK intent (D782). Set by an OIDC callback
29820
+ * that was started from a signed-in user's "Link" button: the finish then
29821
+ * links this identity to the intent's user instead of signing anyone in.
29822
+ * The handle carries no user id — the hub resolves it, single-use.
29823
+ */
29824
+ linkIntent: string().optional(),
29825
+ /**
29826
+ * Verified `iss` of the id_token behind an OIDC login bridge (D782). A
29827
+ * subject is unique only per issuer, so a link is bound to it.
29828
+ */
29829
+ issuer: string().optional()
28196
29830
  });
28197
29831
  method(object({
28198
29832
  claims: SsoBridgeClaimsSchema,
@@ -30098,7 +31732,22 @@ DeviceType.Camera, method(object({ deviceId: number().int().nonnegative() }), ar
30098
31732
  * field their hints were silently stripped by the schema and the
30099
31733
  * adaptive ladder started blind on the client's display size.
30100
31734
  */
30101
- hints: webrtcClientHintsSchema.optional()
31735
+ hints: webrtcClientHintsSchema.optional(),
31736
+ /**
31737
+ * Talk-back (D771): relay the PEER's audio to the device's speaker
31738
+ * through the `intercom` cap's push-talk triple. When `true` and the
31739
+ * offer lets the peer send audio, the answer's audio is `sendrecv`
31740
+ * and every inbound frame goes to `intercom.pushTalkAudio` for this
31741
+ * `deviceId`; absent/false answers `sendonly`, as for every viewer.
31742
+ *
31743
+ * TRUSTED-CALLER ONLY. Set by the Alexa export for an Echo on a camera
31744
+ * with `intercom`, when the linked user may control that camera. The
31745
+ * hub STRIPS it from every request-bound call (a browser), exactly as
31746
+ * it overwrites `relayOnly` — a viewer's talk permission is the
31747
+ * intercom cap's own surface (share tokens gate it there), and this
31748
+ * flag must not become a second door to the camera's speaker.
31749
+ */
31750
+ talkback: boolean().optional()
30102
31751
  }), object({
30103
31752
  sessionId: string(),
30104
31753
  sdpAnswer: string()
@@ -30400,6 +32049,59 @@ var AddonAutoUpdateSchema = ChannelWithInheritSchema;
30400
32049
  var RestartAddonResultSchema = unknown();
30401
32050
  var InstallPackageResultSchema = unknown();
30402
32051
  var ReloadPackagesResultSchema = unknown();
32052
+ /**
32053
+ * What an uninstall did on ONE node.
32054
+ * - `uninstalled` — removed: runner(s) stopped, caps gone, code deleted.
32055
+ * - `not-installed` — the node did not have it (nothing to do).
32056
+ * - `refused` — protected / bootstrap package; nothing touched.
32057
+ * - `failed` — attempted and did not complete; `error` says why.
32058
+ * - `pending-reconnect` — the node is OFFLINE: its record was pruned so the
32059
+ * back-fill never returns the package, and the stale-addon sweep removes the
32060
+ * copy when the node registers again (only once the hub no longer has it).
32061
+ */
32062
+ var UninstallNodeOutcomeSchema = _enum([
32063
+ "uninstalled",
32064
+ "not-installed",
32065
+ "refused",
32066
+ "failed",
32067
+ "pending-reconnect"
32068
+ ]);
32069
+ var UninstallNodeResultSchema = object({
32070
+ nodeId: string(),
32071
+ outcome: UninstallNodeOutcomeSchema,
32072
+ error: string().optional(),
32073
+ addonIds: array(string()).readonly(),
32074
+ runnersStopped: array(string()).readonly(),
32075
+ warnings: array(string()).readonly()
32076
+ });
32077
+ var UninstallPackageResultSchema = object({
32078
+ /** Every targeted node ended `uninstalled`, `not-installed` or `pending-reconnect`. */
32079
+ success: boolean(),
32080
+ packageName: string(),
32081
+ results: array(UninstallNodeResultSchema).readonly()
32082
+ });
32083
+ /**
32084
+ * `unreachable` lists online agents whose status read FAILED — absence from
32085
+ * `installations` is only "not installed" for a node that answered (D315).
32086
+ */
32087
+ var PackageInstallationsSchema = object({
32088
+ installations: array(object({
32089
+ packageName: string(),
32090
+ nodeId: string(),
32091
+ nodeName: string(),
32092
+ isHub: boolean(),
32093
+ isOnline: boolean(),
32094
+ version: string().nullable(),
32095
+ addonIds: array(string()).readonly(),
32096
+ source: _enum(["live", "recorded"]),
32097
+ /** The hub installs this package too. `false` on an agent row = an orphan. */
32098
+ onHub: boolean()
32099
+ })).readonly(),
32100
+ unreachable: array(object({
32101
+ nodeId: string(),
32102
+ error: string()
32103
+ })).readonly()
32104
+ });
30403
32105
  var BulkUpdateItemStatusSchema = _enum([
30404
32106
  "queued",
30405
32107
  "updating",
@@ -30482,10 +32184,14 @@ method(_void(), array(AddonListItemSchema).readonly()), method(object({
30482
32184
  }), method(object({ packageName: string() }), InstallPackageResultSchema, {
30483
32185
  kind: "mutation",
30484
32186
  auth: "admin"
30485
- }), method(_void(), boolean()), method(_void(), array(unknown()).readonly()), method(object({ packageName: string() }), unknown(), {
32187
+ }), method(_void(), boolean()), method(_void(), array(unknown()).readonly()), method(object({
32188
+ packageName: string().min(1),
32189
+ nodeId: string().optional(),
32190
+ everywhere: boolean().optional()
32191
+ }), UninstallPackageResultSchema, {
30486
32192
  kind: "mutation",
30487
32193
  auth: "admin"
30488
- }), method(_void(), ReloadPackagesResultSchema, {
32194
+ }), method(object({ packageName: string().optional() }), PackageInstallationsSchema, { auth: "admin" }), method(_void(), ReloadPackagesResultSchema, {
30489
32195
  kind: "mutation",
30490
32196
  auth: "admin"
30491
32197
  }), method(object({ query: string().optional() }), array(SearchResultSchema)), method(object({ nodeId: string().optional() }), array(PackageUpdateSchema).readonly(), { auth: "admin" }), method(object({
@@ -32553,6 +34259,9 @@ var CreateIntegrationInputSchema = object({
32553
34259
  enabled: boolean().optional(),
32554
34260
  info: record(string(), unknown()).optional(),
32555
34261
  settings: record(string(), unknown()).optional(),
34262
+ /** Keys of `settings` the form's schema declares secret (by type) — kept in
34263
+ * the keychain, not the table (D787). */
34264
+ secretKeys: array(string()).optional(),
32556
34265
  skipRestart: boolean().optional()
32557
34266
  });
32558
34267
  var UpdateIntegrationInputSchema = object({
@@ -32576,7 +34285,9 @@ method(_void(), array(IntegrationWithStateSchema)), method(object({ id: string()
32576
34285
  auth: "admin"
32577
34286
  }), method(object({ id: string() }), record(string(), unknown()), { auth: "admin" }), method(object({
32578
34287
  id: string(),
32579
- settings: record(string(), unknown())
34288
+ settings: record(string(), unknown()),
34289
+ /** Keys the form's schema declares secret (by type) — keychain-stored (D787). */
34290
+ secretKeys: array(string()).optional()
32580
34291
  }), object({ success: boolean() }), {
32581
34292
  kind: "mutation",
32582
34293
  auth: "admin"
@@ -38140,6 +39851,32 @@ var TotpStatusSchema = object({
38140
39851
  /** Null when no row exists OR the row is still pending confirmation. */
38141
39852
  confirmedAt: number().nullable()
38142
39853
  });
39854
+ /**
39855
+ * user-management — singleton capability for user accounts, API keys,
39856
+ * and scoped tokens.
39857
+ *
39858
+ * v2 model: every method gated by `auth: 'admin'`. Admin-only delegation
39859
+ * — the subset check on `setUserScopes` / `createScopedToken` is moot
39860
+ * because non-admins never reach these. If/when we open non-admin
39861
+ * delegation, structural caller-scope subset check goes inside the
39862
+ * provider.
39863
+ */
39864
+ var ExternalIdentitySchema = object({
39865
+ /** Login-method id of the provider instance, e.g. `auth-oidc/authentik`. */
39866
+ provider: string(),
39867
+ /** The IdP's stable subject (`sub`) for this account. */
39868
+ subject: string(),
39869
+ /** The verified `iss` of the IdP the identity was linked under — `sub` is unique only per issuer. */
39870
+ issuer: string(),
39871
+ userId: string(),
39872
+ /** What the IdP called the account at link time (username / email) — display only. */
39873
+ label: string().optional(),
39874
+ linkedAt: number()
39875
+ });
39876
+ var ExternalIdentityKeySchema = object({
39877
+ provider: string().min(1),
39878
+ subject: string().min(1)
39879
+ });
38143
39880
  method(_void(), array(UserSummarySchema), { auth: "admin" }), method(CreateUserInputSchema, UserSummarySchema, {
38144
39881
  kind: "mutation",
38145
39882
  auth: "admin",
@@ -38253,7 +39990,22 @@ method(_void(), array(UserSummarySchema), { auth: "admin" }), method(CreateUserI
38253
39990
  userId: string(),
38254
39991
  username: string(),
38255
39992
  scopes: array(TokenScopeSchema)
38256
- }).nullable(), { access: "view" }), method(_void(), array(OauthSessionSummarySchema), { auth: "admin" }), method(object({ id: string() }), object({ success: boolean() }), {
39993
+ }).nullable(), { access: "view" }), method(object({ userId: string() }), array(ExternalIdentitySchema), { auth: "admin" }), method(ExternalIdentityKeySchema, object({
39994
+ userId: string(),
39995
+ issuer: string()
39996
+ }).nullable(), { auth: "admin" }), method(ExternalIdentityKeySchema.extend({
39997
+ userId: string(),
39998
+ issuer: string().min(1),
39999
+ label: string().optional()
40000
+ }), object({ success: literal(true) }), {
40001
+ kind: "mutation",
40002
+ auth: "admin",
40003
+ access: "create"
40004
+ }), method(ExternalIdentityKeySchema.extend({ userId: string() }), object({ success: boolean() }), {
40005
+ kind: "mutation",
40006
+ auth: "admin",
40007
+ access: "delete"
40008
+ }), method(_void(), array(OauthSessionSummarySchema), { auth: "admin" }), method(object({ id: string() }), object({ success: boolean() }), {
38257
40009
  kind: "mutation",
38258
40010
  auth: "admin",
38259
40011
  access: "delete"
@@ -38885,6 +40637,12 @@ Object.freeze({
38885
40637
  addonId: null,
38886
40638
  access: "view"
38887
40639
  },
40640
+ "addons.listInstallations": {
40641
+ capName: "addons",
40642
+ capScope: "system",
40643
+ addonId: null,
40644
+ access: "view"
40645
+ },
38888
40646
  "addons.listJobs": {
38889
40647
  capName: "addons",
38890
40648
  capScope: "system",
@@ -41345,6 +43103,90 @@ Object.freeze({
41345
43103
  addonId: null,
41346
43104
  access: "create"
41347
43105
  },
43106
+ "keychain.delete": {
43107
+ capName: "keychain",
43108
+ capScope: "system",
43109
+ addonId: null,
43110
+ access: "delete"
43111
+ },
43112
+ "keychain.getState": {
43113
+ capName: "keychain",
43114
+ capScope: "system",
43115
+ addonId: null,
43116
+ access: "view"
43117
+ },
43118
+ "keychain.listMeta": {
43119
+ capName: "keychain",
43120
+ capScope: "system",
43121
+ addonId: null,
43122
+ access: "view"
43123
+ },
43124
+ "keychain.put": {
43125
+ capName: "keychain",
43126
+ capScope: "system",
43127
+ addonId: null,
43128
+ access: "create"
43129
+ },
43130
+ "keychain.resolve": {
43131
+ capName: "keychain",
43132
+ capScope: "system",
43133
+ addonId: null,
43134
+ access: "view"
43135
+ },
43136
+ "keychainAdmin.deleteOrphans": {
43137
+ capName: "keychain-admin",
43138
+ capScope: "system",
43139
+ addonId: null,
43140
+ access: "delete"
43141
+ },
43142
+ "keychainAdmin.downloadRecoveryKey": {
43143
+ capName: "keychain-admin",
43144
+ capScope: "system",
43145
+ addonId: null,
43146
+ access: "create"
43147
+ },
43148
+ "keychainAdmin.listAudit": {
43149
+ capName: "keychain-admin",
43150
+ capScope: "system",
43151
+ addonId: null,
43152
+ access: "view"
43153
+ },
43154
+ "keychainAdmin.listSecrets": {
43155
+ capName: "keychain-admin",
43156
+ capScope: "system",
43157
+ addonId: null,
43158
+ access: "view"
43159
+ },
43160
+ "keychainAdmin.purgeRetiredKeys": {
43161
+ capName: "keychain-admin",
43162
+ capScope: "system",
43163
+ addonId: null,
43164
+ access: "delete"
43165
+ },
43166
+ "keychainAdmin.resetKeychain": {
43167
+ capName: "keychain-admin",
43168
+ capScope: "system",
43169
+ addonId: null,
43170
+ access: "delete"
43171
+ },
43172
+ "keychainAdmin.reveal": {
43173
+ capName: "keychain-admin",
43174
+ capScope: "system",
43175
+ addonId: null,
43176
+ access: "create"
43177
+ },
43178
+ "keychainAdmin.rotateMasterKey": {
43179
+ capName: "keychain-admin",
43180
+ capScope: "system",
43181
+ addonId: null,
43182
+ access: "create"
43183
+ },
43184
+ "keychainAdmin.status": {
43185
+ capName: "keychain-admin",
43186
+ capScope: "system",
43187
+ addonId: null,
43188
+ access: "view"
43189
+ },
41348
43190
  "lawnMowerControl.dock": {
41349
43191
  capName: "lawn-mower-control",
41350
43192
  capScope: "device",
@@ -42719,6 +44561,18 @@ Object.freeze({
42719
44561
  addonId: null,
42720
44562
  access: "create"
42721
44563
  },
44564
+ "pipelineAnalytics.addRetrainFrameFromRecording": {
44565
+ capName: "pipeline-analytics",
44566
+ capScope: "device",
44567
+ addonId: null,
44568
+ access: "create"
44569
+ },
44570
+ "pipelineAnalytics.addRetrainFramesToCollection": {
44571
+ capName: "pipeline-analytics",
44572
+ capScope: "device",
44573
+ addonId: null,
44574
+ access: "create"
44575
+ },
42722
44576
  "pipelineAnalytics.applyDeviceSettingsPatch": {
42723
44577
  capName: "pipeline-analytics",
42724
44578
  capScope: "device",
@@ -42767,12 +44621,24 @@ Object.freeze({
42767
44621
  addonId: null,
42768
44622
  access: "view"
42769
44623
  },
44624
+ "pipelineAnalytics.createRetrainCollection": {
44625
+ capName: "pipeline-analytics",
44626
+ capScope: "device",
44627
+ addonId: null,
44628
+ access: "create"
44629
+ },
42770
44630
  "pipelineAnalytics.deleteDeviceEvents": {
42771
44631
  capName: "pipeline-analytics",
42772
44632
  capScope: "device",
42773
44633
  addonId: null,
42774
44634
  access: "delete"
42775
44635
  },
44636
+ "pipelineAnalytics.deleteRetrainCollection": {
44637
+ capName: "pipeline-analytics",
44638
+ capScope: "device",
44639
+ addonId: null,
44640
+ access: "delete"
44641
+ },
42776
44642
  "pipelineAnalytics.deleteTracks": {
42777
44643
  capName: "pipeline-analytics",
42778
44644
  capScope: "device",
@@ -42977,6 +44843,24 @@ Object.freeze({
42977
44843
  addonId: null,
42978
44844
  access: "view"
42979
44845
  },
44846
+ "pipelineAnalytics.listRetrainAssistProfiles": {
44847
+ capName: "pipeline-analytics",
44848
+ capScope: "device",
44849
+ addonId: null,
44850
+ access: "view"
44851
+ },
44852
+ "pipelineAnalytics.listRetrainCollectionFrames": {
44853
+ capName: "pipeline-analytics",
44854
+ capScope: "device",
44855
+ addonId: null,
44856
+ access: "view"
44857
+ },
44858
+ "pipelineAnalytics.listRetrainCollections": {
44859
+ capName: "pipeline-analytics",
44860
+ capScope: "device",
44861
+ addonId: null,
44862
+ access: "view"
44863
+ },
42980
44864
  "pipelineAnalytics.listRetrainFrames": {
42981
44865
  capName: "pipeline-analytics",
42982
44866
  capScope: "device",
@@ -43025,6 +44909,12 @@ Object.freeze({
43025
44909
  addonId: null,
43026
44910
  access: "create"
43027
44911
  },
44912
+ "pipelineAnalytics.proposeRetrainAnnotationsWithLlm": {
44913
+ capName: "pipeline-analytics",
44914
+ capScope: "device",
44915
+ addonId: null,
44916
+ access: "create"
44917
+ },
43028
44918
  "pipelineAnalytics.pruneEvents": {
43029
44919
  capName: "pipeline-analytics",
43030
44920
  capScope: "device",
@@ -43079,6 +44969,18 @@ Object.freeze({
43079
44969
  addonId: null,
43080
44970
  access: "create"
43081
44971
  },
44972
+ "pipelineAnalytics.removeRetrainFrameFromCollection": {
44973
+ capName: "pipeline-analytics",
44974
+ capScope: "device",
44975
+ addonId: null,
44976
+ access: "delete"
44977
+ },
44978
+ "pipelineAnalytics.renameRetrainCollection": {
44979
+ capName: "pipeline-analytics",
44980
+ capScope: "device",
44981
+ addonId: null,
44982
+ access: "create"
44983
+ },
43082
44984
  "pipelineAnalytics.restageRetrainTrack": {
43083
44985
  capName: "pipeline-analytics",
43084
44986
  capScope: "device",
@@ -43109,6 +45011,12 @@ Object.freeze({
43109
45011
  addonId: null,
43110
45012
  access: "create"
43111
45013
  },
45014
+ "pipelineAnalytics.setRetrainFrameComplete": {
45015
+ capName: "pipeline-analytics",
45016
+ capScope: "device",
45017
+ addonId: null,
45018
+ access: "create"
45019
+ },
43112
45020
  "pipelineAnalytics.setTrackFlags": {
43113
45021
  capName: "pipeline-analytics",
43114
45022
  capScope: "device",
@@ -43145,6 +45053,12 @@ Object.freeze({
43145
45053
  addonId: null,
43146
45054
  access: "delete"
43147
45055
  },
45056
+ "pipelineAnalytics.writeRetrainArchive": {
45057
+ capName: "pipeline-analytics",
45058
+ capScope: "device",
45059
+ addonId: null,
45060
+ access: "create"
45061
+ },
43148
45062
  "pipelineExecutor.cacheFrameInPool": {
43149
45063
  capName: "pipeline-executor",
43150
45064
  capScope: "system",
@@ -43355,6 +45269,12 @@ Object.freeze({
43355
45269
  addonId: null,
43356
45270
  access: "view"
43357
45271
  },
45272
+ "pipelineOrchestrator.abortNodeBenchmark": {
45273
+ capName: "pipeline-orchestrator",
45274
+ capScope: "system",
45275
+ addonId: null,
45276
+ access: "create"
45277
+ },
43358
45278
  "pipelineOrchestrator.applyAutoTune": {
43359
45279
  capName: "pipeline-orchestrator",
43360
45280
  capScope: "system",
@@ -43379,6 +45299,12 @@ Object.freeze({
43379
45299
  addonId: null,
43380
45300
  access: "create"
43381
45301
  },
45302
+ "pipelineOrchestrator.deleteNodeBenchmarkResult": {
45303
+ capName: "pipeline-orchestrator",
45304
+ capScope: "system",
45305
+ addonId: null,
45306
+ access: "delete"
45307
+ },
43382
45308
  "pipelineOrchestrator.getAgentLoad": {
43383
45309
  capName: "pipeline-orchestrator",
43384
45310
  capScope: "system",
@@ -43487,6 +45413,24 @@ Object.freeze({
43487
45413
  addonId: null,
43488
45414
  access: "view"
43489
45415
  },
45416
+ "pipelineOrchestrator.getNodeBenchmarkCandidates": {
45417
+ capName: "pipeline-orchestrator",
45418
+ capScope: "system",
45419
+ addonId: null,
45420
+ access: "view"
45421
+ },
45422
+ "pipelineOrchestrator.getNodeBenchmarkPreflight": {
45423
+ capName: "pipeline-orchestrator",
45424
+ capScope: "system",
45425
+ addonId: null,
45426
+ access: "view"
45427
+ },
45428
+ "pipelineOrchestrator.getNodeBenchmarkStatus": {
45429
+ capName: "pipeline-orchestrator",
45430
+ capScope: "system",
45431
+ addonId: null,
45432
+ access: "view"
45433
+ },
43490
45434
  "pipelineOrchestrator.getNodeInferenceDevices": {
43491
45435
  capName: "pipeline-orchestrator",
43492
45436
  capScope: "system",
@@ -43529,6 +45473,12 @@ Object.freeze({
43529
45473
  addonId: null,
43530
45474
  access: "view"
43531
45475
  },
45476
+ "pipelineOrchestrator.listNodeBenchmarkResults": {
45477
+ capName: "pipeline-orchestrator",
45478
+ capScope: "system",
45479
+ addonId: null,
45480
+ access: "view"
45481
+ },
43532
45482
  "pipelineOrchestrator.pauseForStorageMigration": {
43533
45483
  capName: "pipeline-orchestrator",
43534
45484
  capScope: "system",
@@ -43673,6 +45623,12 @@ Object.freeze({
43673
45623
  addonId: null,
43674
45624
  access: "create"
43675
45625
  },
45626
+ "pipelineOrchestrator.startNodeBenchmark": {
45627
+ capName: "pipeline-orchestrator",
45628
+ capScope: "system",
45629
+ addonId: null,
45630
+ access: "create"
45631
+ },
43676
45632
  "pipelineOrchestrator.suggestShares": {
43677
45633
  capName: "pipeline-orchestrator",
43678
45634
  capScope: "system",
@@ -43691,6 +45647,12 @@ Object.freeze({
43691
45647
  addonId: null,
43692
45648
  access: "create"
43693
45649
  },
45650
+ "pipelineRunner.abortNodeBench": {
45651
+ capName: "pipeline-runner",
45652
+ capScope: "system",
45653
+ addonId: null,
45654
+ access: "create"
45655
+ },
43694
45656
  "pipelineRunner.attachCamera": {
43695
45657
  capName: "pipeline-runner",
43696
45658
  capScope: "system",
@@ -43739,6 +45701,12 @@ Object.freeze({
43739
45701
  addonId: null,
43740
45702
  access: "view"
43741
45703
  },
45704
+ "pipelineRunner.getNodeBenchStatus": {
45705
+ capName: "pipeline-runner",
45706
+ capScope: "system",
45707
+ addonId: null,
45708
+ access: "view"
45709
+ },
43742
45710
  "pipelineRunner.getParkedTrackFrame": {
43743
45711
  capName: "pipeline-runner",
43744
45712
  capScope: "system",
@@ -43781,6 +45749,12 @@ Object.freeze({
43781
45749
  addonId: null,
43782
45750
  access: "create"
43783
45751
  },
45752
+ "pipelineRunner.startNodeBench": {
45753
+ capName: "pipeline-runner",
45754
+ capScope: "system",
45755
+ addonId: null,
45756
+ access: "create"
45757
+ },
43784
45758
  "plateGallery.assignPlate": {
43785
45759
  capName: "plate-gallery",
43786
45760
  capScope: "system",
@@ -45497,18 +47471,36 @@ Object.freeze({
45497
47471
  addonId: null,
45498
47472
  access: "delete"
45499
47473
  },
47474
+ "userManagement.findUserByExternalIdentity": {
47475
+ capName: "user-management",
47476
+ capScope: "system",
47477
+ addonId: null,
47478
+ access: "view"
47479
+ },
45500
47480
  "userManagement.getTotpStatus": {
45501
47481
  capName: "user-management",
45502
47482
  capScope: "system",
45503
47483
  addonId: null,
45504
47484
  access: "view"
45505
47485
  },
47486
+ "userManagement.linkExternalIdentity": {
47487
+ capName: "user-management",
47488
+ capScope: "system",
47489
+ addonId: null,
47490
+ access: "create"
47491
+ },
45506
47492
  "userManagement.listApiKeys": {
45507
47493
  capName: "user-management",
45508
47494
  capScope: "system",
45509
47495
  addonId: null,
45510
47496
  access: "view"
45511
47497
  },
47498
+ "userManagement.listExternalIdentities": {
47499
+ capName: "user-management",
47500
+ capScope: "system",
47501
+ addonId: null,
47502
+ access: "view"
47503
+ },
45512
47504
  "userManagement.listOauthSessions": {
45513
47505
  capName: "user-management",
45514
47506
  capScope: "system",
@@ -45587,6 +47579,12 @@ Object.freeze({
45587
47579
  addonId: null,
45588
47580
  access: "create"
45589
47581
  },
47582
+ "userManagement.unlinkExternalIdentity": {
47583
+ capName: "user-management",
47584
+ capScope: "system",
47585
+ addonId: null,
47586
+ access: "delete"
47587
+ },
45590
47588
  "userManagement.updateUser": {
45591
47589
  capName: "user-management",
45592
47590
  capScope: "system",
@@ -48714,6 +50712,11 @@ Object.freeze({
48714
50712
  form: "single",
48715
50713
  optional: false
48716
50714
  }],
50715
+ "pipelineAnalytics.addRetrainFrameFromRecording": [{
50716
+ name: "deviceId",
50717
+ form: "single",
50718
+ optional: false
50719
+ }],
48717
50720
  "pipelineAnalytics.applyDeviceSettingsPatch": [{
48718
50721
  name: "deviceId",
48719
50722
  form: "single",
@@ -48904,6 +50907,11 @@ Object.freeze({
48904
50907
  form: "single",
48905
50908
  optional: false
48906
50909
  }],
50910
+ "pipelineAnalytics.proposeRetrainAnnotationsWithLlm": [{
50911
+ name: "deviceId",
50912
+ form: "single",
50913
+ optional: false
50914
+ }],
48907
50915
  "pipelineAnalytics.pruneEventsBefore": [{
48908
50916
  name: "deviceId",
48909
50917
  form: "single",
@@ -48954,6 +50962,11 @@ Object.freeze({
48954
50962
  form: "single",
48955
50963
  optional: false
48956
50964
  }],
50965
+ "pipelineAnalytics.setRetrainFrameComplete": [{
50966
+ name: "deviceId",
50967
+ form: "single",
50968
+ optional: false
50969
+ }],
48957
50970
  "pipelineAnalytics.setTrackFlags": [{
48958
50971
  name: "deviceId",
48959
50972
  form: "single",
@@ -51386,6 +53399,22 @@ function capacitySessionsOf(fps, headroom, cameraFps) {
51386
53399
  if (!(fps > 0) || !(cameraFps > 0)) return 0;
51387
53400
  return Math.max(0, Math.floor(fps * (1 - headroom) / cameraFps + 1e-9));
51388
53401
  }
53402
+ /** The table column with the node's own measurements laid over it. */
53403
+ function overlayMeasured(table, measured) {
53404
+ if (measured === void 0 || Object.keys(measured).length === 0) return table;
53405
+ const out = { ...table };
53406
+ for (const [modelId, m] of Object.entries(measured)) out[modelId] = {
53407
+ p50Ms: m.p50Ms,
53408
+ fps: m.fps,
53409
+ measured: true,
53410
+ source: {
53411
+ kind: "node-benchmark",
53412
+ resultId: m.resultId,
53413
+ measuredAtMs: m.measuredAtMs
53414
+ }
53415
+ };
53416
+ return out;
53417
+ }
51389
53418
  function round(value, digits = 3) {
51390
53419
  const f = 10 ** digits;
51391
53420
  return Math.round(value * f) / f;
@@ -51458,6 +53487,7 @@ function choiceOn(ctx, modelId, sizing) {
51458
53487
  p50Ms: cell.p50Ms,
51459
53488
  fps: cell.fps,
51460
53489
  measured: cell.measured,
53490
+ source: cell.source ?? { kind: "table" },
51461
53491
  capacitySessions: sessionsOn(cell, sizing),
51462
53492
  personRecall: DETECTOR_ACCURACY[modelId]?.personRecall ?? 0
51463
53493
  };
@@ -51506,11 +53536,14 @@ function recommendNode(input) {
51506
53536
  if (node.hardware === null) return skippedNode(node, { kind: "no-hardware-facts" }, null);
51507
53537
  const hardware = node.hardware;
51508
53538
  const { hardwareClass, cpuModel } = classifyInferenceHardware(hardware);
51509
- const classTable = perf[hardwareClass];
51510
- if (hardwareClass === "unknown" || classTable === void 0) return skippedNode(node, {
53539
+ const measured = input.measured ?? {};
53540
+ const hasMeasured = Object.values(measured).some((m) => Object.keys(m).length > 0);
53541
+ const knownTable = hardwareClass === "unknown" ? void 0 : perf[hardwareClass];
53542
+ if (knownTable === void 0 && !hasMeasured) return skippedNode(node, {
51511
53543
  kind: "no-measured-data",
51512
53544
  hardwareClass
51513
53545
  }, hardwareClass);
53546
+ const classTable = knownTable ?? {};
51514
53547
  const devices = node.devices.toSorted((a, b) => a.key.localeCompare(b.key));
51515
53548
  const contexts = /* @__PURE__ */ new Map();
51516
53549
  const early = /* @__PURE__ */ new Map();
@@ -51528,7 +53561,7 @@ function recommendNode(input) {
51528
53561
  early.set(device.key, [{ kind: "device-unavailable" }]);
51529
53562
  continue;
51530
53563
  }
51531
- const column = accelerator === null ? void 0 : classTable[accelerator];
53564
+ const column = accelerator === null ? void 0 : overlayMeasured(classTable[accelerator], measured[device.key]);
51532
53565
  if (accelerator === null || column === void 0) {
51533
53566
  early.set(device.key, [{
51534
53567
  kind: "no-family-build",
@@ -51543,13 +53576,28 @@ function recommendNode(input) {
51543
53576
  });
51544
53577
  }
51545
53578
  const isCpu = (a) => a === "x86-cpu" || a === "arm-cpu";
51546
- const accelerators = [...contexts.values()].filter((c) => !isCpu(c.accelerator));
51547
- const cpus = [...contexts.values()].filter((c) => isCpu(c.accelerator));
53579
+ const keptByKey = /* @__PURE__ */ new Map();
53580
+ const keptReasonOf = (group) => {
53581
+ const pinned = group.find((c) => c.device.customPinModelId !== void 0);
53582
+ if (pinned?.device.customPinModelId === void 0) return null;
53583
+ const reason = {
53584
+ kind: "kept-custom-pin",
53585
+ modelId: pinned.device.customPinModelId,
53586
+ deviceKey: pinned.device.key
53587
+ };
53588
+ for (const c of group) keptByKey.set(c.device.key, [reason]);
53589
+ return reason;
53590
+ };
53591
+ const acceleratorGroup = [...contexts.values()].filter((c) => !isCpu(c.accelerator));
53592
+ const acceleratorKept = keptReasonOf(acceleratorGroup);
53593
+ const accelerators = acceleratorKept === null ? acceleratorGroup : [];
53594
+ const cpus = [...contexts.values()].filter((c) => isCpu(c.accelerator)).filter((c) => keptReasonOf([c]) === null);
51548
53595
  const chosenByKey = /* @__PURE__ */ new Map();
51549
53596
  const reasonsByKey = /* @__PURE__ */ new Map();
51550
53597
  const nodeReasons = [];
51551
53598
  let acceleratorModelId = null;
51552
53599
  const groupPick = accelerators.length > 0 ? pickForGroup(accelerators, node.formatsByModel, sizing) : null;
53600
+ if (acceleratorKept !== null) nodeReasons.push(acceleratorKept);
51553
53601
  if (groupPick !== null) {
51554
53602
  acceleratorModelId = groupPick.modelId;
51555
53603
  nodeReasons.push(...groupPick.reasons);
@@ -51593,7 +53641,8 @@ function recommendNode(input) {
51593
53641
  enabled: device.enabled,
51594
53642
  available: device.available,
51595
53643
  chosen: chosenByKey.get(device.key) ?? null,
51596
- reasons: early.get(device.key) ?? reasonsByKey.get(device.key) ?? []
53644
+ ...keptByKey.has(device.key) ? { kept: "custom-pin" } : {},
53645
+ reasons: early.get(device.key) ?? keptByKey.get(device.key) ?? reasonsByKey.get(device.key) ?? []
51597
53646
  })),
51598
53647
  reasons: nodeReasons
51599
53648
  };
@@ -51705,24 +53754,6 @@ function resolveEffectiveShares(input) {
51705
53754
  deviceWeights
51706
53755
  };
51707
53756
  }
51708
- /**
51709
- * Automatic shares from CAPACITY (D755). Pure.
51710
- *
51711
- * A node's share is its capacity in parallel sessions over the cluster's; a
51712
- * device's share inside its node follows its throughput. There is no load
51713
- * input: nothing here depends on how many sessions ran, only on what each
51714
- * node can carry.
51715
- *
51716
- * Two places a capacity can come from:
51717
- * - `hardware` — each enabled accelerator at its Auto tune model, from the
51718
- * measured latency table: ⌊fps × (1 − headroom) / camera fps⌋ sessions;
51719
- * - `benchmark` — the node's own measured max sessions per device, read
51720
- * through a {@link NodeCapacitySource}. A node whose results do not cover
51721
- * every enabled device it would be sized on is sized from `hardware`
51722
- * instead, and the answer says so per node — never a silent mix.
51723
- */
51724
- /** A source that has no results — the benchmark store before it exists. */
51725
- var NO_MEASURED_CAPACITY = { readMeasuredCapacity: async () => /* @__PURE__ */ new Map() };
51726
53757
  /** Integer shares summing to 100 (largest remainder); ties broken by key. */
51727
53758
  function toPercentShares(weights) {
51728
53759
  const total = [...weights.values()].reduce((a, b) => a + b, 0);
@@ -51837,6 +53868,258 @@ function computeCapacityShares(input) {
51837
53868
  };
51838
53869
  }
51839
53870
  /**
53871
+ * Benchmark mode (D791) — the pure rules of "one node under test". Pure.
53872
+ *
53873
+ * While the cluster is in benchmark mode for node N:
53874
+ * - N is not a detection candidate for any camera that another node can
53875
+ * serve ({@link eligibleUnderBenchmark});
53876
+ * - a node pin naming N is ignored for such a camera
53877
+ * ({@link pinUnderBenchmark}) — the stored pin is never rewritten, so it
53878
+ * is "restored" the moment the mode ends;
53879
+ * - a camera whose ONLY frame source is N stays on N (never blind) and the
53880
+ * run is flagged contaminated.
53881
+ */
53882
+ /** The detection candidates of one camera while `benchNodeId` is under test. */
53883
+ function eligibleUnderBenchmark(eligible, benchNodeId) {
53884
+ if (benchNodeId === null || !eligible.includes(benchNodeId)) return eligible;
53885
+ const others = eligible.filter((n) => n !== benchNodeId);
53886
+ return others.length > 0 ? others : eligible;
53887
+ }
53888
+ /** A camera's node pin while `benchNodeId` is under test (null = no pin). */
53889
+ function pinUnderBenchmark(pin, benchNodeId, eligible) {
53890
+ if (pin === null || benchNodeId === null || pin !== benchNodeId) return pin;
53891
+ return eligible.some((n) => n !== benchNodeId) ? null : pin;
53892
+ }
53893
+ /** Which cameras leave the node for the run, and which cannot. */
53894
+ function classifyCamerasForBenchmark(nodeId, cameras) {
53895
+ const moving = [];
53896
+ const staying = [];
53897
+ for (const cam of cameras.toSorted((a, b) => a.deviceId - b.deviceId)) {
53898
+ const placedOnNode = cam.placedNodeId === nodeId;
53899
+ const pinnedToNode = cam.nodePin === nodeId;
53900
+ if (!placedOnNode && !pinnedToNode) continue;
53901
+ if (!cam.frameSourceNodes.some((n) => n !== nodeId)) staying.push({
53902
+ deviceId: cam.deviceId,
53903
+ reason: "only-frame-source"
53904
+ });
53905
+ else moving.push({
53906
+ deviceId: cam.deviceId,
53907
+ placedOnNode,
53908
+ pinnedToNode
53909
+ });
53910
+ }
53911
+ return {
53912
+ moving,
53913
+ staying
53914
+ };
53915
+ }
53916
+ /** Ramp steps the runner tries per cell, at most. */
53917
+ var DEVICE_RAMP = [
53918
+ 1,
53919
+ 2,
53920
+ 4,
53921
+ 6,
53922
+ 8,
53923
+ 12,
53924
+ 16,
53925
+ 24,
53926
+ 32
53927
+ ];
53928
+ var PIPELINE_RAMP = [
53929
+ 1,
53930
+ 2,
53931
+ 3,
53932
+ 4,
53933
+ 6,
53934
+ 8,
53935
+ 10,
53936
+ 12,
53937
+ 16
53938
+ ];
53939
+ /** Fixed costs around the measurement: drain, clip, warm-ups, restore. */
53940
+ var OVERHEAD_MS = 3 * 6e4;
53941
+ var WARMUP_PER_CELL_MS = 2e4;
53942
+ /**
53943
+ * An upper bound on how long the run takes: every ramp step of every cell,
53944
+ * plus the drain, the clip and the restore — capped at the budget, which is
53945
+ * the hard deadline anyway. A ramp usually stops well before its last step.
53946
+ */
53947
+ function estimateNodeBenchmarkDurationMs(plan, timing = {}) {
53948
+ const stepMs = ((timing.settleSec ?? NODE_BENCHMARK_DEFAULTS.settleSec) + (timing.measureSec ?? NODE_BENCHMARK_DEFAULTS.measureSec)) * 1e3;
53949
+ let total = OVERHEAD_MS;
53950
+ if (plan.phases.includes("device")) for (const device of plan.devices) total += device.modelIds.length * (WARMUP_PER_CELL_MS + DEVICE_RAMP.length * stepMs);
53951
+ if (plan.phases.includes("pipeline")) total += plan.devices.length * (WARMUP_PER_CELL_MS + PIPELINE_RAMP.length * stepMs);
53952
+ return Math.min(total, plan.budgetMinutes * 6e4);
53953
+ }
53954
+ /** Why a run cannot start, in the order the operator should read them. */
53955
+ function nodeBenchmarkRefusals(input) {
53956
+ const out = [];
53957
+ if (input.activeRunNodeId !== null) out.push({
53958
+ kind: "benchmark-active",
53959
+ detail: `A benchmark of ${input.activeRunNodeId} is running — one node at a time.`,
53960
+ overridable: false
53961
+ });
53962
+ if (!input.online) out.push({
53963
+ kind: "node-offline",
53964
+ detail: `${input.plan.nodeId} is not online.`,
53965
+ overridable: false
53966
+ });
53967
+ const unknown = input.plan.devices.map((d) => d.deviceKey).filter((k) => !input.knownDeviceKeys.includes(k));
53968
+ if (unknown.length > 0) out.push({
53969
+ kind: "unknown-device",
53970
+ detail: `${input.plan.nodeId} reports no device ${unknown.join(", ")}.`,
53971
+ overridable: false
53972
+ });
53973
+ if (input.plan.phases.length === 0) out.push({
53974
+ kind: "no-phase",
53975
+ detail: "Choose at least one measurement.",
53976
+ overridable: false
53977
+ });
53978
+ const capacity = input.otherNodesCapacitySessions;
53979
+ if (capacity !== null && input.sessionsNow > capacity && !input.plan.acceptInsufficientHeadroom) out.push({
53980
+ kind: "insufficient-headroom",
53981
+ detail: `The cluster runs ${input.sessionsNow} sessions now; the other nodes can carry ${capacity}.`,
53982
+ overridable: true
53983
+ });
53984
+ return out;
53985
+ }
53986
+ /** The cell a row measures — two rows with the same key measure the same thing. */
53987
+ function resultCellKey(row) {
53988
+ const { cell } = row;
53989
+ const steps = cell.phase === "pipeline" ? cell.steps.toSorted().join(",") : "";
53990
+ return [
53991
+ row.nodeId,
53992
+ cell.deviceKey,
53993
+ cell.phase,
53994
+ cell.modelId,
53995
+ steps
53996
+ ].join("|");
53997
+ }
53998
+ function staleWhy(row, ctx) {
53999
+ const bound = ctx.staleAfterMs ?? NODE_BENCHMARK_DEFAULTS.staleAfterDays * 864e5;
54000
+ if (ctx.nowMs - row.measuredAtMs > bound) return "age";
54001
+ const now = ctx.currentPools[row.nodeId]?.[row.cell.deviceKey];
54002
+ const then = row.fingerprint?.pools[row.cell.deviceKey];
54003
+ if (now === void 0 || then === void 0) return null;
54004
+ if (now.runtime !== then.runtime) return "runtime";
54005
+ if (now.numWorkers !== then.numWorkers || now.concurrency !== then.concurrency || now.batchMode !== then.batchMode) return "pool-tuning";
54006
+ return null;
54007
+ }
54008
+ /** The standing of every row, by id. */
54009
+ function resultStandings(rows, ctx) {
54010
+ const newest = /* @__PURE__ */ new Map();
54011
+ for (const row of rows) {
54012
+ const key = resultCellKey(row);
54013
+ const held = newest.get(key);
54014
+ if (held === void 0 || row.measuredAtMs > held.measuredAtMs) newest.set(key, row);
54015
+ }
54016
+ const out = /* @__PURE__ */ new Map();
54017
+ for (const row of rows) {
54018
+ if (newest.get(resultCellKey(row))?.id !== row.id) {
54019
+ out.set(row.id, {
54020
+ standing: "superseded",
54021
+ staleWhy: null
54022
+ });
54023
+ continue;
54024
+ }
54025
+ const why = staleWhy(row, ctx);
54026
+ out.set(row.id, why === null ? {
54027
+ standing: "fresh",
54028
+ staleWhy: null
54029
+ } : {
54030
+ standing: "stale",
54031
+ staleWhy: why
54032
+ });
54033
+ }
54034
+ return out;
54035
+ }
54036
+ /** Rows that beat the table today. */
54037
+ function freshResults(rows, ctx) {
54038
+ const standings = resultStandings(rows, ctx);
54039
+ return rows.filter((r) => standings.get(r.id)?.standing === "fresh");
54040
+ }
54041
+ /** deviceKey → modelId → the fresh `device` measurement on `nodeId`. */
54042
+ function measuredDetectorCells(rows, nodeId, ctx) {
54043
+ const out = {};
54044
+ for (const row of freshResults(rows, ctx)) {
54045
+ if (row.nodeId !== nodeId || row.cell.phase !== "device") continue;
54046
+ const perDevice = out[row.cell.deviceKey] ?? {};
54047
+ perDevice[row.cell.modelId] = {
54048
+ p50Ms: Math.round(row.cell.p50Ms * 100) / 100,
54049
+ fps: row.cell.fps,
54050
+ resultId: row.id,
54051
+ measuredAtMs: row.measuredAtMs
54052
+ };
54053
+ out[row.cell.deviceKey] = perDevice;
54054
+ }
54055
+ return out;
54056
+ }
54057
+ /**
54058
+ * Per node, each device at its newest fresh `pipeline` measurement — the
54059
+ * `NodeCapacitySource` answer the automatic shares read.
54060
+ */
54061
+ function measuredCapacityFromResults(rows, nodeIds, ctx) {
54062
+ const byNode = /* @__PURE__ */ new Map();
54063
+ for (const row of freshResults(rows, ctx)) {
54064
+ if (!nodeIds.includes(row.nodeId) || row.cell.phase !== "pipeline") continue;
54065
+ const devices = byNode.get(row.nodeId) ?? /* @__PURE__ */ new Map();
54066
+ const held = devices.get(row.cell.deviceKey);
54067
+ if (held === void 0 || row.measuredAtMs > held.measuredAtMs) devices.set(row.cell.deviceKey, row);
54068
+ byNode.set(row.nodeId, devices);
54069
+ }
54070
+ const out = /* @__PURE__ */ new Map();
54071
+ for (const nodeId of nodeIds) {
54072
+ const devices = byNode.get(nodeId);
54073
+ if (devices === void 0) continue;
54074
+ out.set(nodeId, {
54075
+ nodeId,
54076
+ devices: [...devices.values()].toSorted((a, b) => a.cell.deviceKey.localeCompare(b.cell.deviceKey)).map((r) => ({
54077
+ deviceKey: r.cell.deviceKey,
54078
+ maxSessions: r.cell.phase === "pipeline" ? r.cell.maxSessions : 0,
54079
+ modelId: r.cell.modelId,
54080
+ measuredAtMs: r.measuredAtMs
54081
+ }))
54082
+ });
54083
+ }
54084
+ return out;
54085
+ }
54086
+ var BENCH_ROOT_STEP_ID = "object-detection";
54087
+ function slotAddons(schema, slot) {
54088
+ return schema.slots.find((s) => s.id === slot)?.addons ?? [];
54089
+ }
54090
+ var feeds = (from, to) => to.inputClasses.some((c) => from.outputClasses.includes(c));
54091
+ function layout(schema) {
54092
+ const root = slotAddons(schema, "detector").find((a) => a.id === BENCH_ROOT_STEP_ID);
54093
+ if (root === void 0) return null;
54094
+ const classifiers = slotAddons(schema, "classifier");
54095
+ const croppers = slotAddons(schema, "cropper").filter((c) => feeds(root, c)).map((cropper) => ({
54096
+ cropper,
54097
+ recognizers: classifiers.filter((k) => feeds(cropper, k))
54098
+ }));
54099
+ const underCropper = new Set(croppers.flatMap((c) => c.recognizers.map((r) => r.id)));
54100
+ return {
54101
+ root,
54102
+ croppers,
54103
+ classifiers: classifiers.filter((k) => feeds(root, k) && !underCropper.has(k.id)),
54104
+ refiners: slotAddons(schema, "refiner")
54105
+ };
54106
+ }
54107
+ /** The detail steps the operator can choose, in tree order. */
54108
+ function listBenchDetailSteps(schema) {
54109
+ const l = layout(schema);
54110
+ if (l === null) return [];
54111
+ const of = (a) => ({
54112
+ id: a.id,
54113
+ name: a.name,
54114
+ slot: a.slot
54115
+ });
54116
+ return [
54117
+ ...l.croppers.flatMap((c) => [of(c.cropper), ...c.recognizers.map(of)]),
54118
+ ...l.classifiers.map(of),
54119
+ ...l.refiners.map(of)
54120
+ ];
54121
+ }
54122
+ /**
51840
54123
  * THE detail-crop convention — the single derivation of the rectangle a
51841
54124
  * detail/enrichment step (clip-embedding, face-detection, plate-detection…)
51842
54125
  * is fed.
@@ -52180,6 +54463,959 @@ function deviceBackendToFormat(backend) {
52180
54463
  return DEVICE_BACKEND_TO_FORMAT[backend] ?? "onnx";
52181
54464
  }
52182
54465
  //#endregion
54466
+ //#region src/durable/durable-ledger.ts
54467
+ /** Default reseed cap — every current consumer's row set is installation-bounded. */
54468
+ var DEFAULT_LOAD_LIMIT = 1e5;
54469
+ var DurableLedger = class DurableLedger {
54470
+ mirror = /* @__PURE__ */ new Map();
54471
+ /** Whether ANY load has read the store successfully — see {@link hasAnswered}. */
54472
+ answered = false;
54473
+ spec;
54474
+ store;
54475
+ logger;
54476
+ constructor(deps) {
54477
+ this.spec = deps.spec;
54478
+ this.store = deps.store;
54479
+ this.logger = deps.logger;
54480
+ }
54481
+ /**
54482
+ * Register the collection. MUST run at boot, before any read or write: the
54483
+ * SQLite backend answers 412 for an undeclared collection and takes the whole
54484
+ * runner down with it (the addon-ai boot-crash lesson).
54485
+ */
54486
+ static declare(store, spec) {
54487
+ return store.declareCollection.mutate({
54488
+ collection: spec.collection,
54489
+ columns: [...spec.columns],
54490
+ ...spec.indexes !== void 0 ? { indexes: [...spec.indexes] } : {}
54491
+ });
54492
+ }
54493
+ declare() {
54494
+ return DurableLedger.declare(this.store, this.spec);
54495
+ }
54496
+ /** The collection this ledger owns — for the caller's own log lines. */
54497
+ get collection() {
54498
+ return this.spec.collection;
54499
+ }
54500
+ /**
54501
+ * Boot reseed. Replaces the mirror with what the store holds and returns the
54502
+ * rows, so a caller that must hydrate something else (a watcher, a registry)
54503
+ * gets them without a second read.
54504
+ *
54505
+ * **A failure returns what is already mirrored** rather than clearing it —
54506
+ * see contract rule 2. The count is worth logging out loud at the call site:
54507
+ * "loaded 0" after a container recreate is the one line that explains a
54508
+ * silent flood.
54509
+ */
54510
+ async load() {
54511
+ try {
54512
+ const records = await this.store.query.query({
54513
+ collection: this.spec.collection,
54514
+ filter: { limit: this.spec.loadLimit ?? DEFAULT_LOAD_LIMIT }
54515
+ });
54516
+ const next = /* @__PURE__ */ new Map();
54517
+ let skipped = 0;
54518
+ for (const record of records) {
54519
+ const row = this.spec.fromRecord(record.id, record.data);
54520
+ if (row === null) {
54521
+ skipped += 1;
54522
+ continue;
54523
+ }
54524
+ next.set(this.spec.keyOf(row), row);
54525
+ }
54526
+ this.mirror.clear();
54527
+ for (const [key, row] of next) this.mirror.set(key, row);
54528
+ this.answered = true;
54529
+ if (skipped > 0) this.logger.warn("durable rows skipped as malformed — they gate NOTHING", { meta: {
54530
+ collection: this.spec.collection,
54531
+ skipped
54532
+ } });
54533
+ return [...this.mirror.values()];
54534
+ } catch (err) {
54535
+ this.logger.warn("durable load failed — keeping the state already in memory", { meta: {
54536
+ collection: this.spec.collection,
54537
+ error: String(err),
54538
+ held: this.mirror.size
54539
+ } });
54540
+ return [...this.mirror.values()];
54541
+ }
54542
+ }
54543
+ /**
54544
+ * Whether the mirror is an ANSWER from the store: true once any load has
54545
+ * succeeded, and never false again (a later failed load keeps that answer).
54546
+ * Before it, an empty mirror is "not read yet", never "the store is empty" —
54547
+ * {@link load} swallows its failure, so a caller that must tell the two apart
54548
+ * asks here (D642 review I1).
54549
+ */
54550
+ hasAnswered() {
54551
+ return this.answered;
54552
+ }
54553
+ /** Every mirrored row, insertion-ordered. */
54554
+ snapshot() {
54555
+ return [...this.mirror.values()];
54556
+ }
54557
+ /** The row currently held for a key, if any. Pure RAM — never I/O. */
54558
+ get(key) {
54559
+ return this.mirror.get(key);
54560
+ }
54561
+ has(key) {
54562
+ return this.mirror.has(key);
54563
+ }
54564
+ get size() {
54565
+ return this.mirror.size;
54566
+ }
54567
+ /**
54568
+ * Judge ONE observation against what the ledger already accepted, and advance
54569
+ * it.
54570
+ *
54571
+ * Synchronous on purpose: the verdict is a function of the in-RAM mirror
54572
+ * alone, so a decision can never be gated on an I/O that might fail (D49).
54573
+ * The durable write is kicked off behind it and its failure changes no
54574
+ * verdict.
54575
+ *
54576
+ * `no-flip` leaves the held row UNTOUCHED — including any timestamp it
54577
+ * carries, which therefore means "when this key last CHANGED", not "when it
54578
+ * last spoke". That is the timestamp anyone reading the table wants.
54579
+ */
54580
+ observe(row) {
54581
+ const equalFact = this.spec.equalFact;
54582
+ if (equalFact === void 0) throw new Error(`DurableLedger(${this.spec.collection}): observe() requires the spec to declare equalFact`);
54583
+ const key = this.spec.keyOf(row);
54584
+ const held = this.mirror.get(key);
54585
+ if (held !== void 0 && equalFact(held, row)) return "no-flip";
54586
+ this.mirror.set(key, row);
54587
+ this.persist(row);
54588
+ return held === void 0 ? "seeded" : "flip";
54589
+ }
54590
+ /**
54591
+ * Upsert a row without a verdict — the write path for a ledger whose owner
54592
+ * has already decided the value changed.
54593
+ *
54594
+ * The order of the mirror advance and the durable write is the spec's
54595
+ * {@link DurableWriteMode}, not the call site's: two call sites that
54596
+ * disagreed about it would be two different durability guarantees on one
54597
+ * collection.
54598
+ */
54599
+ async put(row) {
54600
+ const key = this.spec.keyOf(row);
54601
+ if (this.spec.writeMode === "write-behind") {
54602
+ this.mirror.set(key, row);
54603
+ await this.persist(row);
54604
+ return;
54605
+ }
54606
+ await this.store.set.mutate({
54607
+ collection: this.spec.collection,
54608
+ key,
54609
+ value: this.spec.toValue(row)
54610
+ });
54611
+ this.mirror.set(key, row);
54612
+ }
54613
+ /**
54614
+ * Advance the mirror WITHOUT persisting, for an owner that deliberately
54615
+ * coalesces its writes.
54616
+ *
54617
+ * The stationary registry is the reason this exists: a parked car is
54618
+ * re-confirmed on every processed frame (5–30 Hz), and persisting each
54619
+ * confirmation would offer thousands of commits a day to the checkpoint
54620
+ * lottery (D96) to maintain a handful of rows. It stages the advance and
54621
+ * flushes on its 5-minute sweep — 288 writes a day instead of ~10⁶.
54622
+ *
54623
+ * **The cost is stated, not hidden**: a staged value that is never flushed
54624
+ * is lost on a crash. An owner may only stage a field whose staleness its
54625
+ * own TTL absorbs. Anything that GATES work must go through {@link put} or
54626
+ * {@link observe}.
54627
+ */
54628
+ stage(row) {
54629
+ this.mirror.set(this.spec.keyOf(row), row);
54630
+ }
54631
+ /**
54632
+ * Drop a key from the MIRROR only — the durable row survives.
54633
+ *
54634
+ * What a scope-unbind needs: this process stops holding the value, and a
54635
+ * rebind reloads it from the store. Deliberately distinct from
54636
+ * {@link forget}, which deletes; conflating the two is how an unbind turns
54637
+ * into a wipe.
54638
+ */
54639
+ evict(key) {
54640
+ this.mirror.delete(key);
54641
+ }
54642
+ /**
54643
+ * Drop one key, mirror and row. Best-effort on the durable half: a failed
54644
+ * delete leaves a row that the next load will re-mirror, which is a stale
54645
+ * value rather than a lost one.
54646
+ */
54647
+ async forget(key) {
54648
+ this.mirror.delete(key);
54649
+ try {
54650
+ await this.store.delete.mutate({
54651
+ collection: this.spec.collection,
54652
+ key
54653
+ });
54654
+ } catch (err) {
54655
+ this.logger.debug("durable delete failed", { meta: {
54656
+ collection: this.spec.collection,
54657
+ key,
54658
+ error: String(err)
54659
+ } });
54660
+ }
54661
+ }
54662
+ /**
54663
+ * Drop every mirrored key NOT in `activeKeys`. Returns how many rows went.
54664
+ *
54665
+ * **The caller must hold an AUTHORITATIVE active set.** A prune driven by a
54666
+ * fallible read is work destroyed on an error (D49/D130) — that is why this
54667
+ * is a method a feature opts into rather than a policy the primitive runs.
54668
+ * Best-effort per row: a failed delete keeps the key (retried next prune)
54669
+ * rather than aborting the sweep.
54670
+ */
54671
+ async pruneExcept(activeKeys) {
54672
+ let pruned = 0;
54673
+ for (const key of [...this.mirror.keys()]) {
54674
+ if (activeKeys.has(key)) continue;
54675
+ try {
54676
+ await this.store.delete.mutate({
54677
+ collection: this.spec.collection,
54678
+ key
54679
+ });
54680
+ this.mirror.delete(key);
54681
+ pruned += 1;
54682
+ } catch (err) {
54683
+ this.logger.debug("durable prune delete failed", { meta: {
54684
+ collection: this.spec.collection,
54685
+ key,
54686
+ error: String(err)
54687
+ } });
54688
+ }
54689
+ }
54690
+ return pruned;
54691
+ }
54692
+ /**
54693
+ * Write-behind durable upsert. Best-effort and logged, never thrown at the
54694
+ * decision path: the mirror already holds the truth for this process, and the
54695
+ * worst a lost write can do is one silent re-seed after the next restart.
54696
+ */
54697
+ async persist(row) {
54698
+ const deviceId = this.spec.deviceIdOf?.(row);
54699
+ try {
54700
+ await this.store.set.mutate({
54701
+ collection: this.spec.collection,
54702
+ key: this.spec.keyOf(row),
54703
+ value: this.spec.toValue(row)
54704
+ });
54705
+ } catch (err) {
54706
+ this.logger.warn("durable persist failed — this key may re-seed on boot", {
54707
+ ...deviceId !== void 0 ? { tags: { deviceId } } : {},
54708
+ meta: {
54709
+ collection: this.spec.collection,
54710
+ key: this.spec.keyOf(row),
54711
+ error: String(err)
54712
+ }
54713
+ });
54714
+ }
54715
+ }
54716
+ };
54717
+ //#endregion
54718
+ //#region src/node-benchmark/node-benchmark-store.ts
54719
+ /**
54720
+ * The node benchmark's three durable collections (D791), all owned by the
54721
+ * orchestrator — the placement authority that enters and leaves benchmark
54722
+ * mode, and the one writer of the results Auto tune and the shares read.
54723
+ *
54724
+ * - the MANIFEST: one row, written BEFORE benchmark mode changes anything
54725
+ * and removed only after the cluster is restored. Its presence at boot or
54726
+ * on a reconcile with no live run means "restore now".
54727
+ * - the RUNS: what each run did and why it ended (bounded history).
54728
+ * - the RESULTS: one row per measured cell.
54729
+ */
54730
+ /**
54731
+ * @durable class=ledger owner=pipeline-orchestrator
54732
+ * write="once when benchmark mode is entered (write-through, BEFORE any change), removed once the cluster is restored"
54733
+ * retention="at most one row (key `active`); its presence means a restore is owed"
54734
+ */
54735
+ var NODE_BENCHMARK_MODE_COLLECTION = "pipeline-orchestrator:node-benchmark-mode";
54736
+ /**
54737
+ * @durable class=audit owner=pipeline-orchestrator
54738
+ * write="one row per run, rewritten when the run ends"
54739
+ * retention="the newest 50 runs; older rows are removed when a run is recorded"
54740
+ */
54741
+ var NODE_BENCHMARK_RUNS_COLLECTION = "pipeline-orchestrator:node-benchmark-runs";
54742
+ /**
54743
+ * @durable class=ledger owner=pipeline-orchestrator
54744
+ * write="one row per measured cell, written when its run ends; deleted by the operator"
54745
+ * retention="the newest 500 rows; a row older than the stale bound stays listed but no longer beats the table"
54746
+ */
54747
+ var NODE_BENCHMARK_RESULTS_COLLECTION = "pipeline-orchestrator:node-benchmarks";
54748
+ var JSON_ROW_COLUMNS = [
54749
+ {
54750
+ name: "id",
54751
+ type: "TEXT",
54752
+ primaryKey: true,
54753
+ notNull: true
54754
+ },
54755
+ {
54756
+ name: "body",
54757
+ type: "JSON",
54758
+ notNull: true
54759
+ },
54760
+ {
54761
+ name: "at",
54762
+ type: "INTEGER",
54763
+ notNull: true
54764
+ }
54765
+ ];
54766
+ var MANIFEST_KEY = "active";
54767
+ function jsonSpec(collection, schema, keyOf, at) {
54768
+ return {
54769
+ collection,
54770
+ columns: JSON_ROW_COLUMNS,
54771
+ writeMode: "write-through",
54772
+ keyOf,
54773
+ toValue: (row) => ({
54774
+ body: row,
54775
+ at: at(row)
54776
+ }),
54777
+ fromRecord: (_key, data) => {
54778
+ const parsed = schema.safeParse(data["body"]);
54779
+ return parsed.success ? parsed.data : null;
54780
+ }
54781
+ };
54782
+ }
54783
+ var MANIFEST_SPEC = jsonSpec(NODE_BENCHMARK_MODE_COLLECTION, NodeBenchmarkManifestSchema, () => MANIFEST_KEY, (m) => m.startedAtMs);
54784
+ var RUNS_SPEC = jsonSpec(NODE_BENCHMARK_RUNS_COLLECTION, NodeBenchmarkRunSchema, (r) => r.runId, (r) => r.startedAtMs);
54785
+ var RESULTS_SPEC = jsonSpec(NODE_BENCHMARK_RESULTS_COLLECTION, NodeBenchmarkResultSchema, (r) => r.id, (r) => r.measuredAtMs);
54786
+ /** Keep the newest `max` rows by `at`; the rest are the ones to drop. */
54787
+ function overflow(rows, max, at) {
54788
+ if (rows.length <= max) return [];
54789
+ return rows.toSorted((a, b) => at(b) - at(a)).slice(max);
54790
+ }
54791
+ var NodeBenchmarkStore = class {
54792
+ manifest;
54793
+ runs;
54794
+ results;
54795
+ constructor(deps) {
54796
+ this.manifest = new DurableLedger({
54797
+ spec: MANIFEST_SPEC,
54798
+ ...deps
54799
+ });
54800
+ this.runs = new DurableLedger({
54801
+ spec: RUNS_SPEC,
54802
+ ...deps
54803
+ });
54804
+ this.results = new DurableLedger({
54805
+ spec: RESULTS_SPEC,
54806
+ ...deps
54807
+ });
54808
+ }
54809
+ static async declare(store) {
54810
+ await DurableLedger.declare(store, MANIFEST_SPEC);
54811
+ await DurableLedger.declare(store, RUNS_SPEC);
54812
+ await DurableLedger.declare(store, RESULTS_SPEC);
54813
+ }
54814
+ async load() {
54815
+ await this.manifest.load();
54816
+ await this.runs.load();
54817
+ await this.results.load();
54818
+ }
54819
+ readManifest() {
54820
+ return this.manifest.get(MANIFEST_KEY) ?? null;
54821
+ }
54822
+ writeManifest(manifest) {
54823
+ return this.manifest.put(manifest);
54824
+ }
54825
+ clearManifest() {
54826
+ return this.manifest.forget(MANIFEST_KEY);
54827
+ }
54828
+ async recordRun(run) {
54829
+ await this.runs.put(run);
54830
+ for (const old of overflow(this.runs.snapshot(), 50, (r) => r.startedAtMs)) await this.runs.forget(old.runId);
54831
+ }
54832
+ listRuns() {
54833
+ return this.runs.snapshot().toSorted((a, b) => b.startedAtMs - a.startedAtMs);
54834
+ }
54835
+ async writeResults(rows) {
54836
+ for (const row of rows) await this.results.put(row);
54837
+ for (const old of overflow(this.results.snapshot(), 500, (r) => r.measuredAtMs)) await this.results.forget(old.id);
54838
+ }
54839
+ listResults() {
54840
+ return this.results.snapshot().toSorted((a, b) => b.measuredAtMs - a.measuredAtMs);
54841
+ }
54842
+ async deleteResult(id) {
54843
+ if (!this.results.has(id)) return false;
54844
+ await this.results.forget(id);
54845
+ return true;
54846
+ }
54847
+ };
54848
+ //#endregion
54849
+ //#region src/node-benchmark/node-benchmark-service.ts
54850
+ /**
54851
+ * The node benchmark lifecycle (D791) — the orchestrator owns it because it
54852
+ * is the placement authority: entering benchmark mode is a placement change,
54853
+ * and only the owner of placement can promise to undo it.
54854
+ *
54855
+ * start ─► preflight ─► MANIFEST (persisted first) ─► mode on ─► draining
54856
+ * ─► measuring (the node's runner, polled) ─► restoring ─► done | failed | aborted
54857
+ *
54858
+ * Invariants:
54859
+ * - ONE run cluster-wide; it does not matter which runner the request
54860
+ * reached — the orchestrator drives the target node's runner by nodePin.
54861
+ * - The manifest is written BEFORE anything changes and removed only AFTER
54862
+ * the cluster is restored. Its presence without a live run — at boot, or
54863
+ * on any reconcile tick — means "restore now" (registry-driven, never a
54864
+ * timer or an event that may be lost).
54865
+ * - Restoring always runs (`finally`): mode off, cameras pinned to the node
54866
+ * re-placed, the runner told to stop, the results written, the run
54867
+ * recorded. An abort leaves the cluster as it was.
54868
+ * - Nothing the operator owns is written: no detect flag, no pin, no cap.
54869
+ * Pins are ignored by the mode, never rewritten, so ending the mode IS the
54870
+ * restore of the pins.
54871
+ */
54872
+ var DEFAULT_POLL_MS = 3e3;
54873
+ /** After the deadline the runner gets this long to stop before the restore is forced. */
54874
+ var DEADLINE_GRACE_MS = 2 * 6e4;
54875
+ /** The node whose duties never stop for a benchmark: its numbers always share the CPU. */
54876
+ var HUB_NODE_ID = "hub";
54877
+ function toRun(manifest) {
54878
+ return {
54879
+ runId: manifest.runId,
54880
+ nodeId: manifest.nodeId,
54881
+ state: "draining",
54882
+ plan: manifest.plan,
54883
+ startedAtMs: manifest.startedAtMs,
54884
+ deadlineAtMs: manifest.deadlineAtMs,
54885
+ finishedAtMs: null,
54886
+ endReason: null,
54887
+ error: null,
54888
+ moved: manifest.moved,
54889
+ staying: manifest.staying,
54890
+ contaminated: manifest.staying.length > 0 || manifest.nodeId === HUB_NODE_ID,
54891
+ progress: null,
54892
+ resultIds: []
54893
+ };
54894
+ }
54895
+ /** Pipeline cells whose requested steps did not all fire measured something else — dropped. */
54896
+ function publishableCells(cells, requestedSteps) {
54897
+ const unfired = /* @__PURE__ */ new Set();
54898
+ return {
54899
+ kept: cells.filter((cell) => {
54900
+ if (cell.phase !== "pipeline") return true;
54901
+ const missing = requestedSteps.filter((s) => !cell.stepsFired.includes(s));
54902
+ for (const s of missing) unfired.add(s);
54903
+ return missing.length === 0;
54904
+ }),
54905
+ unfired: [...unfired].toSorted()
54906
+ };
54907
+ }
54908
+ var NodeBenchmarkService = class {
54909
+ deps;
54910
+ active = null;
54911
+ modeNodeIdValue = null;
54912
+ lastRun = null;
54913
+ restoring = null;
54914
+ constructor(deps) {
54915
+ this.deps = deps;
54916
+ }
54917
+ now() {
54918
+ return this.deps.now?.() ?? Date.now();
54919
+ }
54920
+ sleep(ms) {
54921
+ if (this.deps.sleep) return this.deps.sleep(ms);
54922
+ return new Promise((resolve) => {
54923
+ setTimeout(resolve, ms).unref?.();
54924
+ });
54925
+ }
54926
+ /** The node the cluster is in benchmark mode for (null = none). Read by placement. */
54927
+ modeNodeId() {
54928
+ return this.modeNodeIdValue;
54929
+ }
54930
+ setMode(nodeId) {
54931
+ if (this.modeNodeIdValue === nodeId) return;
54932
+ this.modeNodeIdValue = nodeId;
54933
+ this.deps.logger.info(nodeId === null ? "benchmark mode off" : "benchmark mode on — node out of placement", { meta: { nodeId } });
54934
+ this.deps.onModeChanged?.(nodeId);
54935
+ }
54936
+ status() {
54937
+ const run = this.active?.run ?? this.lastRun ?? this.deps.persistence.listRuns()[0] ?? null;
54938
+ return {
54939
+ modeNodeId: this.modeNodeIdValue,
54940
+ run
54941
+ };
54942
+ }
54943
+ async preflight(planInput) {
54944
+ const plan = NodeBenchmarkPlanSchema.parse(planInput);
54945
+ const online = this.deps.isNodeOnline(plan.nodeId);
54946
+ const knownDeviceKeys = online ? await this.deps.listDeviceKeys(plan.nodeId) : [];
54947
+ const { moving, staying } = classifyCamerasForBenchmark(plan.nodeId, await this.deps.cameraFacts());
54948
+ const headroom = await this.deps.headroom(plan.nodeId);
54949
+ const refusals = nodeBenchmarkRefusals({
54950
+ plan,
54951
+ activeRunNodeId: this.active?.run.nodeId ?? this.modeNodeIdValue,
54952
+ online,
54953
+ knownDeviceKeys,
54954
+ sessionsNow: headroom.sessionsNow,
54955
+ otherNodesCapacitySessions: headroom.otherNodesCapacitySessions
54956
+ });
54957
+ return {
54958
+ nodeId: plan.nodeId,
54959
+ online,
54960
+ moving,
54961
+ staying,
54962
+ estimatedDurationMs: estimateNodeBenchmarkDurationMs(plan),
54963
+ budgetMs: plan.budgetMinutes * 6e4,
54964
+ phases: plan.phases,
54965
+ steps: plan.steps,
54966
+ devices: plan.devices,
54967
+ headroom,
54968
+ refusals,
54969
+ canStart: refusals.length === 0
54970
+ };
54971
+ }
54972
+ /** Start a run, or say why not (the preflight with its refusals). */
54973
+ async start(planInput) {
54974
+ const preflight = await this.preflight(planInput);
54975
+ if (!preflight.canStart || this.active !== null || this.modeNodeIdValue !== null) return {
54976
+ kind: "refused",
54977
+ preflight: preflight.canStart ? {
54978
+ ...preflight,
54979
+ canStart: false,
54980
+ refusals: [{
54981
+ kind: "benchmark-active",
54982
+ detail: "Another benchmark started first — one node at a time.",
54983
+ overridable: false
54984
+ }]
54985
+ } : preflight
54986
+ };
54987
+ const plan = NodeBenchmarkPlanSchema.parse(planInput);
54988
+ const startedAtMs = this.now();
54989
+ const manifest = {
54990
+ runId: this.deps.newRunId?.() ?? `nb-${startedAtMs.toString(36)}`,
54991
+ nodeId: plan.nodeId,
54992
+ startedAtMs,
54993
+ deadlineAtMs: startedAtMs + plan.budgetMinutes * 6e4,
54994
+ plan,
54995
+ moved: preflight.moving.map((c) => c.deviceId),
54996
+ staying: preflight.staying.map((c) => c.deviceId)
54997
+ };
54998
+ const run = toRun(manifest);
54999
+ this.active = {
55000
+ run,
55001
+ abortRequested: null
55002
+ };
55003
+ try {
55004
+ await this.deps.persistence.writeManifest(manifest);
55005
+ } catch (err) {
55006
+ this.active = null;
55007
+ throw new Error(`node benchmark: could not persist the restore manifest — nothing changed (${errMsg$2(err)})`, { cause: err });
55008
+ }
55009
+ this.setMode(plan.nodeId);
55010
+ this.deps.logger.info("node benchmark started", { meta: {
55011
+ runId: run.runId,
55012
+ nodeId: run.nodeId,
55013
+ moving: manifest.moved.length,
55014
+ staying: manifest.staying.length,
55015
+ deadlineAtMs: manifest.deadlineAtMs
55016
+ } });
55017
+ this.execute(manifest);
55018
+ return {
55019
+ kind: "started",
55020
+ runId: run.runId
55021
+ };
55022
+ }
55023
+ /** Ask the active run to stop; the restore follows. */
55024
+ abort(runId) {
55025
+ if (this.active === null || this.active.run.runId !== runId) return { aborted: false };
55026
+ this.active.abortRequested ??= "operator-abort";
55027
+ this.deps.logger.info("node benchmark abort requested", { meta: { runId } });
55028
+ return { aborted: true };
55029
+ }
55030
+ update(patch) {
55031
+ if (this.active === null) return;
55032
+ this.active = {
55033
+ ...this.active,
55034
+ run: {
55035
+ ...this.active.run,
55036
+ ...patch
55037
+ }
55038
+ };
55039
+ }
55040
+ stopReason(manifest) {
55041
+ if (this.active?.abortRequested) return this.active.abortRequested;
55042
+ if (this.now() >= manifest.deadlineAtMs) return "deadline";
55043
+ if (!this.deps.isNodeOnline(manifest.nodeId)) return "node-lost";
55044
+ return null;
55045
+ }
55046
+ async drainPhase(manifest) {
55047
+ this.update({ state: "draining" });
55048
+ const moved = new Set(manifest.moved);
55049
+ if (moved.size > 0) await this.deps.drain(moved);
55050
+ const timeout = this.deps.drainTimeoutMs ?? NODE_BENCHMARK_DEFAULTS.drainTimeoutSec * 1e3;
55051
+ const until = this.now() + timeout;
55052
+ const staying = new Set(manifest.staying);
55053
+ for (;;) {
55054
+ const stop = this.stopReason(manifest);
55055
+ if (stop !== null) return stop;
55056
+ const left = (await this.deps.camerasOnNode(manifest.nodeId)).filter((id) => !staying.has(id));
55057
+ if (left.length === 0) return null;
55058
+ if (this.now() >= until) {
55059
+ this.deps.logger.warn("node benchmark: drain timed out — cameras still on the node", { meta: {
55060
+ runId: manifest.runId,
55061
+ nodeId: manifest.nodeId,
55062
+ left
55063
+ } });
55064
+ return "drain-timeout";
55065
+ }
55066
+ await this.deps.drain(new Set(left));
55067
+ await this.sleep(this.deps.pollIntervalMs ?? DEFAULT_POLL_MS);
55068
+ }
55069
+ }
55070
+ async measurePhase(manifest) {
55071
+ this.update({ state: "measuring" });
55072
+ const { plan } = manifest;
55073
+ await this.deps.runner.start(manifest.nodeId, {
55074
+ runId: manifest.runId,
55075
+ devices: plan.devices,
55076
+ phases: plan.phases,
55077
+ steps: plan.steps,
55078
+ fpsPerStream: plan.fpsPerStream,
55079
+ deadlineAtMs: manifest.deadlineAtMs,
55080
+ keepDownloadedModels: plan.keepDownloadedModels
55081
+ });
55082
+ let last = null;
55083
+ let unknown = 0;
55084
+ const lostAfter = this.deps.runnerLostAfter ?? 3;
55085
+ for (;;) {
55086
+ await this.sleep(this.deps.pollIntervalMs ?? DEFAULT_POLL_MS);
55087
+ let status = null;
55088
+ try {
55089
+ status = await this.deps.runner.status(manifest.nodeId, manifest.runId);
55090
+ } catch (err) {
55091
+ this.deps.logger.debug("node benchmark: runner status read failed", { meta: {
55092
+ runId: manifest.runId,
55093
+ error: errMsg$2(err)
55094
+ } });
55095
+ }
55096
+ if (status === null) {
55097
+ unknown += 1;
55098
+ if (!this.deps.isNodeOnline(manifest.nodeId)) return {
55099
+ reason: "node-lost",
55100
+ status: last
55101
+ };
55102
+ if (unknown >= lostAfter) return {
55103
+ reason: "runner-lost",
55104
+ status: last
55105
+ };
55106
+ } else {
55107
+ unknown = 0;
55108
+ last = status;
55109
+ this.update({
55110
+ progress: status.progress,
55111
+ contaminated: this.active?.run.contaminated === true || status.activeCameraIds.length > 0
55112
+ });
55113
+ if (status.state === "done") return {
55114
+ reason: "completed",
55115
+ status
55116
+ };
55117
+ if (status.state === "failed") return {
55118
+ reason: "failed",
55119
+ status
55120
+ };
55121
+ if (status.state === "aborted") return {
55122
+ reason: this.active?.abortRequested ?? "deadline",
55123
+ status
55124
+ };
55125
+ }
55126
+ const stop = this.stopReason(manifest);
55127
+ if (stop !== null) {
55128
+ await this.deps.runner.abort(manifest.nodeId, manifest.runId).catch(() => void 0);
55129
+ return {
55130
+ reason: stop,
55131
+ status: await this.deps.runner.status(manifest.nodeId, manifest.runId).catch(() => null) ?? last
55132
+ };
55133
+ }
55134
+ }
55135
+ }
55136
+ async execute(manifest) {
55137
+ let reason = "failed";
55138
+ let status = null;
55139
+ let error = null;
55140
+ try {
55141
+ const drained = await this.drainPhase(manifest);
55142
+ if (drained !== null) reason = drained;
55143
+ else {
55144
+ const measured = await this.measurePhase(manifest);
55145
+ reason = measured.reason;
55146
+ status = measured.status;
55147
+ if (reason === "failed") error = status?.error ?? "the runner failed";
55148
+ }
55149
+ } catch (err) {
55150
+ reason = this.active?.abortRequested ?? "failed";
55151
+ error = errMsg$2(err);
55152
+ this.deps.logger.warn("node benchmark failed", { meta: {
55153
+ runId: manifest.runId,
55154
+ error
55155
+ } });
55156
+ await this.deps.runner.abort(manifest.nodeId, manifest.runId).catch(() => void 0);
55157
+ } finally {
55158
+ await this.restore(manifest, reason, status, error);
55159
+ }
55160
+ }
55161
+ /**
55162
+ * Leave benchmark mode and put the cluster back. Idempotent; every step is
55163
+ * attempted whatever the previous one did.
55164
+ */
55165
+ async restore(manifest, reason, status, error) {
55166
+ this.update({ state: "restoring" });
55167
+ this.setMode(null);
55168
+ const pinnedBack = new Set(manifest.moved);
55169
+ try {
55170
+ if (pinnedBack.size > 0) await this.deps.restorePins(pinnedBack);
55171
+ } catch (err) {
55172
+ this.deps.logger.warn("node benchmark: re-placing the moved cameras failed — the reconcile retries", { meta: {
55173
+ runId: manifest.runId,
55174
+ error: errMsg$2(err)
55175
+ } });
55176
+ }
55177
+ const finishedAtMs = this.now();
55178
+ const base = this.active?.run ?? toRun(manifest);
55179
+ const contaminated = base.contaminated || (status?.activeCameraIds.length ?? 0) > 0;
55180
+ const { kept, unfired } = publishableCells(status?.cells ?? [], manifest.plan.steps);
55181
+ const rows = kept.map((cell, i) => ({
55182
+ id: `${manifest.runId}:${i}`,
55183
+ runId: manifest.runId,
55184
+ nodeId: manifest.nodeId,
55185
+ cell,
55186
+ fpsPerStream: manifest.plan.fpsPerStream,
55187
+ fingerprint: status?.fingerprint ?? null,
55188
+ clip: status?.clip ?? null,
55189
+ contaminated,
55190
+ stayingCameras: manifest.staying,
55191
+ partialRun: reason !== "completed",
55192
+ measuredAtMs: finishedAtMs
55193
+ }));
55194
+ 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);
55195
+ try {
55196
+ if (rows.length > 0) await this.deps.persistence.writeResults(rows);
55197
+ } catch (err) {
55198
+ notes.push(`Results not stored: ${errMsg$2(err)}`);
55199
+ }
55200
+ const run = {
55201
+ ...base,
55202
+ state: reason === "completed" ? "done" : reason === "failed" ? "failed" : "aborted",
55203
+ finishedAtMs,
55204
+ endReason: reason,
55205
+ error: notes.length > 0 ? notes.join(" ") : null,
55206
+ contaminated,
55207
+ resultIds: rows.map((r) => r.id)
55208
+ };
55209
+ try {
55210
+ await this.deps.persistence.recordRun(run);
55211
+ } catch (err) {
55212
+ this.deps.logger.warn("node benchmark: run record not stored", { meta: {
55213
+ runId: run.runId,
55214
+ error: errMsg$2(err)
55215
+ } });
55216
+ }
55217
+ await this.deps.persistence.clearManifest().catch(() => void 0);
55218
+ this.lastRun = run;
55219
+ this.active = null;
55220
+ this.deps.logger.info("node benchmark ended", { meta: {
55221
+ runId: run.runId,
55222
+ nodeId: run.nodeId,
55223
+ endReason: reason,
55224
+ results: rows.length,
55225
+ contaminated,
55226
+ error: run.error
55227
+ } });
55228
+ if (rows.length > 0) this.deps.onResultsWritten(manifest.nodeId);
55229
+ }
55230
+ /**
55231
+ * Registry-driven recovery: a manifest with no live run means the run died
55232
+ * with its process (hub restart) — restore now. A live run far past its
55233
+ * deadline is forced out of the mode. Called at boot and on every tick.
55234
+ */
55235
+ async reconcile(trigger) {
55236
+ if (this.restoring !== null) return this.restoring;
55237
+ const work = async () => {
55238
+ if (this.active !== null) {
55239
+ const { run } = this.active;
55240
+ if (this.now() > run.deadlineAtMs + DEADLINE_GRACE_MS && this.modeNodeIdValue !== null) {
55241
+ this.deps.logger.warn("node benchmark: past its deadline and still not restored — forcing benchmark mode off", { meta: {
55242
+ runId: run.runId,
55243
+ nodeId: run.nodeId
55244
+ } });
55245
+ this.active.abortRequested ??= "deadline";
55246
+ this.setMode(null);
55247
+ }
55248
+ return;
55249
+ }
55250
+ if (this.modeNodeIdValue !== null) this.setMode(null);
55251
+ const manifest = this.deps.persistence.readManifest();
55252
+ if (manifest === null) return;
55253
+ const known = this.deps.persistence.listRuns().find((r) => r.runId === manifest.runId);
55254
+ if (known !== void 0 && known.finishedAtMs !== null) {
55255
+ await this.deps.persistence.clearManifest().catch(() => void 0);
55256
+ return;
55257
+ }
55258
+ this.deps.logger.warn("node benchmark: restore owed from an interrupted run", { meta: {
55259
+ runId: manifest.runId,
55260
+ nodeId: manifest.nodeId,
55261
+ trigger
55262
+ } });
55263
+ await this.deps.runner.abort(manifest.nodeId, manifest.runId).catch(() => void 0);
55264
+ this.active = {
55265
+ run: toRun(manifest),
55266
+ abortRequested: "hub-restart"
55267
+ };
55268
+ await this.restore(manifest, trigger === "boot" ? "hub-restart" : "runner-lost", null, null);
55269
+ };
55270
+ this.restoring = work().finally(() => {
55271
+ this.restoring = null;
55272
+ });
55273
+ return this.restoring;
55274
+ }
55275
+ };
55276
+ //#endregion
55277
+ //#region src/node-benchmark/node-benchmark-wiring.ts
55278
+ /**
55279
+ * Binds the node benchmark service (D791) to the orchestrator's real
55280
+ * controllers: the placement ledger and topology for the camera facts, a
55281
+ * SCOPED rebalance for the drain and the restore, the shares for headroom,
55282
+ * and the target node's runner through `nodePin` — whichever runner the
55283
+ * operator's request first reached, the measurement runs on the node chosen.
55284
+ */
55285
+ function makeRunnerClient(ctx) {
55286
+ return {
55287
+ start: async (nodeId, input) => {
55288
+ await ctx().api.pipelineRunner.startNodeBench.mutate(input, nodePin(nodeId));
55289
+ },
55290
+ status: async (nodeId, runId) => ctx().api.pipelineRunner.getNodeBenchStatus.query({ runId }, nodePin(nodeId)),
55291
+ abort: async (nodeId, runId) => {
55292
+ await ctx().api.pipelineRunner.abortNodeBench.mutate({ runId }, nodePin(nodeId));
55293
+ }
55294
+ };
55295
+ }
55296
+ /** Every camera the orchestrator places, as the preflight reads it (benchmark ignored). */
55297
+ async function collectCameraFacts(deps) {
55298
+ const out = [];
55299
+ for (const deviceId of deps.ledger.listConfigIds()) out.push({
55300
+ deviceId,
55301
+ placedNodeId: deps.ledger.getAssignment(deviceId)?.agentNodeId ?? deps.activeSessionNode(deviceId),
55302
+ nodePin: await deps.readStoredPipelinePin(deviceId),
55303
+ frameSourceNodes: deps.topology.detectionEligibleNodesIgnoringBenchmark(deviceId)
55304
+ });
55305
+ return out;
55306
+ }
55307
+ function createNodeBenchmarkService(deps) {
55308
+ const logger = deps.ctx().logger;
55309
+ return new NodeBenchmarkService({
55310
+ logger,
55311
+ persistence: deps.persistence,
55312
+ runner: makeRunnerClient(deps.ctx),
55313
+ isNodeOnline: (nodeId) => deps.topology.collectRunnerNodeIds().includes(nodeId),
55314
+ listDeviceKeys: deps.listDeviceKeys,
55315
+ cameraFacts: () => collectCameraFacts(deps),
55316
+ headroom: async (nodeId) => {
55317
+ let other = null;
55318
+ try {
55319
+ other = (await deps.suggestShares()).nodes.filter((n) => n.nodeId !== nodeId && n.skippedReason === null).reduce((sum, n) => sum + n.capacitySessions, 0);
55320
+ } catch (err) {
55321
+ logger.debug("node benchmark: capacity read failed — headroom unknown", { meta: { error: errMsg$2(err) } });
55322
+ }
55323
+ return {
55324
+ sessionsNow: deps.sessionsNow(),
55325
+ otherNodesCapacitySessions: other
55326
+ };
55327
+ },
55328
+ drain: async (deviceIds) => {
55329
+ const result = await deps.rebalance({
55330
+ minImprovement: 0,
55331
+ onlyDeviceIds: deviceIds
55332
+ });
55333
+ logger.info("node benchmark: drain pass", { meta: {
55334
+ migrated: result.migrated,
55335
+ skipped: result.skipped.length
55336
+ } });
55337
+ },
55338
+ 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)),
55339
+ restorePins: async (deviceIds) => {
55340
+ const result = await deps.rebalance({
55341
+ minImprovement: 0,
55342
+ onlyDeviceIds: deviceIds
55343
+ });
55344
+ logger.info("node benchmark: cameras re-placed after the run", { meta: {
55345
+ migrated: result.migrated,
55346
+ skipped: result.skipped.length
55347
+ } });
55348
+ },
55349
+ onResultsWritten: deps.onResultsWritten,
55350
+ ...deps.pollIntervalMs !== void 0 ? { pollIntervalMs: deps.pollIntervalMs } : {}
55351
+ });
55352
+ }
55353
+ //#endregion
55354
+ //#region src/node-benchmark/node-benchmark-reads.ts
55355
+ /**
55356
+ * Read paths of the node benchmark (D791): the plan editor's candidates and
55357
+ * the results with their standing today. Pure over injected reads.
55358
+ */
55359
+ var DETECTOR_STEP_ID$1 = "object-detection";
55360
+ /** The family Auto tune picks from (D745) — the suggestions follow it. */
55361
+ var SUGGESTED_FAMILY = "yolov9";
55362
+ var MAX_SUGGESTED = 3;
55363
+ function detectorFormats(catalog) {
55364
+ const out = /* @__PURE__ */ new Map();
55365
+ for (const slot of catalog?.slots ?? []) for (const addon of slot.addons) {
55366
+ if (addon.id !== DETECTOR_STEP_ID$1) continue;
55367
+ for (const model of addon.models) {
55368
+ if (model.legacy === true) continue;
55369
+ out.set(model.id, MODEL_FORMATS.filter((f) => f in model.formats));
55370
+ }
55371
+ }
55372
+ return out;
55373
+ }
55374
+ /** Candidates per device: Auto tune's pick first, then the family by person recall. */
55375
+ async function readNodeBenchmarkCandidates(deps) {
55376
+ const devices = await deps.readDevices(deps.nodeId);
55377
+ const catalog = await deps.readCatalog(deps.nodeId).catch(() => null);
55378
+ let picks = {};
55379
+ try {
55380
+ picks = await deps.autoTunePicks(deps.nodeId);
55381
+ } catch (err) {
55382
+ deps.logger.debug("node benchmark candidates: Auto tune pick unavailable", { meta: {
55383
+ nodeId: deps.nodeId,
55384
+ error: errMsg$2(err)
55385
+ } });
55386
+ }
55387
+ const formats = detectorFormats(catalog);
55388
+ 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));
55389
+ return {
55390
+ nodeId: deps.nodeId,
55391
+ reachable: devices.reachable,
55392
+ devices: devices.devices.map((d) => {
55393
+ const runs = (id) => (formats.get(id) ?? []).includes(d.format);
55394
+ const all = [...formats.keys()].filter(runs).toSorted();
55395
+ const pick = picks[d.key];
55396
+ const ordered = [...pick !== void 0 && runs(pick) ? [pick] : [], ...family.filter((id) => runs(id) && id !== pick)];
55397
+ return {
55398
+ deviceKey: d.key,
55399
+ enabled: d.enabled,
55400
+ available: d.available,
55401
+ suggested: ordered.slice(0, MAX_SUGGESTED),
55402
+ all,
55403
+ currentModelId: d.steps?.[DETECTOR_STEP_ID$1]?.modelId ?? d.defaultModelId ?? null
55404
+ };
55405
+ }),
55406
+ steps: catalog === null ? [] : listBenchDetailSteps(catalog)
55407
+ };
55408
+ }
55409
+ /** Results filtered, newest first, each with its standing today. */
55410
+ function listNodeBenchmarkResultViews(rows, input, ctx) {
55411
+ const standings = resultStandings(rows, ctx);
55412
+ 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) => ({
55413
+ ...r,
55414
+ standing: standings.get(r.id)?.standing ?? "stale",
55415
+ staleWhy: standings.get(r.id)?.staleWhy ?? null
55416
+ }));
55417
+ }
55418
+ //#endregion
52183
55419
  //#region src/cluster-model-choice.ts
52184
55420
  /**
52185
55421
  * The owner's answer to "which model did the operator choose for this
@@ -58729,258 +61965,6 @@ function buildRunnerConfig(base, overrides) {
58729
61965
  preRollInferenceFrames: config.preRollInferenceFrames
58730
61966
  };
58731
61967
  }
58732
- //#endregion
58733
- //#region src/durable/durable-ledger.ts
58734
- /** Default reseed cap — every current consumer's row set is installation-bounded. */
58735
- var DEFAULT_LOAD_LIMIT = 1e5;
58736
- var DurableLedger = class DurableLedger {
58737
- mirror = /* @__PURE__ */ new Map();
58738
- /** Whether ANY load has read the store successfully — see {@link hasAnswered}. */
58739
- answered = false;
58740
- spec;
58741
- store;
58742
- logger;
58743
- constructor(deps) {
58744
- this.spec = deps.spec;
58745
- this.store = deps.store;
58746
- this.logger = deps.logger;
58747
- }
58748
- /**
58749
- * Register the collection. MUST run at boot, before any read or write: the
58750
- * SQLite backend answers 412 for an undeclared collection and takes the whole
58751
- * runner down with it (the addon-ai boot-crash lesson).
58752
- */
58753
- static declare(store, spec) {
58754
- return store.declareCollection.mutate({
58755
- collection: spec.collection,
58756
- columns: [...spec.columns],
58757
- ...spec.indexes !== void 0 ? { indexes: [...spec.indexes] } : {}
58758
- });
58759
- }
58760
- declare() {
58761
- return DurableLedger.declare(this.store, this.spec);
58762
- }
58763
- /** The collection this ledger owns — for the caller's own log lines. */
58764
- get collection() {
58765
- return this.spec.collection;
58766
- }
58767
- /**
58768
- * Boot reseed. Replaces the mirror with what the store holds and returns the
58769
- * rows, so a caller that must hydrate something else (a watcher, a registry)
58770
- * gets them without a second read.
58771
- *
58772
- * **A failure returns what is already mirrored** rather than clearing it —
58773
- * see contract rule 2. The count is worth logging out loud at the call site:
58774
- * "loaded 0" after a container recreate is the one line that explains a
58775
- * silent flood.
58776
- */
58777
- async load() {
58778
- try {
58779
- const records = await this.store.query.query({
58780
- collection: this.spec.collection,
58781
- filter: { limit: this.spec.loadLimit ?? DEFAULT_LOAD_LIMIT }
58782
- });
58783
- const next = /* @__PURE__ */ new Map();
58784
- let skipped = 0;
58785
- for (const record of records) {
58786
- const row = this.spec.fromRecord(record.id, record.data);
58787
- if (row === null) {
58788
- skipped += 1;
58789
- continue;
58790
- }
58791
- next.set(this.spec.keyOf(row), row);
58792
- }
58793
- this.mirror.clear();
58794
- for (const [key, row] of next) this.mirror.set(key, row);
58795
- this.answered = true;
58796
- if (skipped > 0) this.logger.warn("durable rows skipped as malformed — they gate NOTHING", { meta: {
58797
- collection: this.spec.collection,
58798
- skipped
58799
- } });
58800
- return [...this.mirror.values()];
58801
- } catch (err) {
58802
- this.logger.warn("durable load failed — keeping the state already in memory", { meta: {
58803
- collection: this.spec.collection,
58804
- error: String(err),
58805
- held: this.mirror.size
58806
- } });
58807
- return [...this.mirror.values()];
58808
- }
58809
- }
58810
- /**
58811
- * Whether the mirror is an ANSWER from the store: true once any load has
58812
- * succeeded, and never false again (a later failed load keeps that answer).
58813
- * Before it, an empty mirror is "not read yet", never "the store is empty" —
58814
- * {@link load} swallows its failure, so a caller that must tell the two apart
58815
- * asks here (D642 review I1).
58816
- */
58817
- hasAnswered() {
58818
- return this.answered;
58819
- }
58820
- /** Every mirrored row, insertion-ordered. */
58821
- snapshot() {
58822
- return [...this.mirror.values()];
58823
- }
58824
- /** The row currently held for a key, if any. Pure RAM — never I/O. */
58825
- get(key) {
58826
- return this.mirror.get(key);
58827
- }
58828
- has(key) {
58829
- return this.mirror.has(key);
58830
- }
58831
- get size() {
58832
- return this.mirror.size;
58833
- }
58834
- /**
58835
- * Judge ONE observation against what the ledger already accepted, and advance
58836
- * it.
58837
- *
58838
- * Synchronous on purpose: the verdict is a function of the in-RAM mirror
58839
- * alone, so a decision can never be gated on an I/O that might fail (D49).
58840
- * The durable write is kicked off behind it and its failure changes no
58841
- * verdict.
58842
- *
58843
- * `no-flip` leaves the held row UNTOUCHED — including any timestamp it
58844
- * carries, which therefore means "when this key last CHANGED", not "when it
58845
- * last spoke". That is the timestamp anyone reading the table wants.
58846
- */
58847
- observe(row) {
58848
- const equalFact = this.spec.equalFact;
58849
- if (equalFact === void 0) throw new Error(`DurableLedger(${this.spec.collection}): observe() requires the spec to declare equalFact`);
58850
- const key = this.spec.keyOf(row);
58851
- const held = this.mirror.get(key);
58852
- if (held !== void 0 && equalFact(held, row)) return "no-flip";
58853
- this.mirror.set(key, row);
58854
- this.persist(row);
58855
- return held === void 0 ? "seeded" : "flip";
58856
- }
58857
- /**
58858
- * Upsert a row without a verdict — the write path for a ledger whose owner
58859
- * has already decided the value changed.
58860
- *
58861
- * The order of the mirror advance and the durable write is the spec's
58862
- * {@link DurableWriteMode}, not the call site's: two call sites that
58863
- * disagreed about it would be two different durability guarantees on one
58864
- * collection.
58865
- */
58866
- async put(row) {
58867
- const key = this.spec.keyOf(row);
58868
- if (this.spec.writeMode === "write-behind") {
58869
- this.mirror.set(key, row);
58870
- await this.persist(row);
58871
- return;
58872
- }
58873
- await this.store.set.mutate({
58874
- collection: this.spec.collection,
58875
- key,
58876
- value: this.spec.toValue(row)
58877
- });
58878
- this.mirror.set(key, row);
58879
- }
58880
- /**
58881
- * Advance the mirror WITHOUT persisting, for an owner that deliberately
58882
- * coalesces its writes.
58883
- *
58884
- * The stationary registry is the reason this exists: a parked car is
58885
- * re-confirmed on every processed frame (5–30 Hz), and persisting each
58886
- * confirmation would offer thousands of commits a day to the checkpoint
58887
- * lottery (D96) to maintain a handful of rows. It stages the advance and
58888
- * flushes on its 5-minute sweep — 288 writes a day instead of ~10⁶.
58889
- *
58890
- * **The cost is stated, not hidden**: a staged value that is never flushed
58891
- * is lost on a crash. An owner may only stage a field whose staleness its
58892
- * own TTL absorbs. Anything that GATES work must go through {@link put} or
58893
- * {@link observe}.
58894
- */
58895
- stage(row) {
58896
- this.mirror.set(this.spec.keyOf(row), row);
58897
- }
58898
- /**
58899
- * Drop a key from the MIRROR only — the durable row survives.
58900
- *
58901
- * What a scope-unbind needs: this process stops holding the value, and a
58902
- * rebind reloads it from the store. Deliberately distinct from
58903
- * {@link forget}, which deletes; conflating the two is how an unbind turns
58904
- * into a wipe.
58905
- */
58906
- evict(key) {
58907
- this.mirror.delete(key);
58908
- }
58909
- /**
58910
- * Drop one key, mirror and row. Best-effort on the durable half: a failed
58911
- * delete leaves a row that the next load will re-mirror, which is a stale
58912
- * value rather than a lost one.
58913
- */
58914
- async forget(key) {
58915
- this.mirror.delete(key);
58916
- try {
58917
- await this.store.delete.mutate({
58918
- collection: this.spec.collection,
58919
- key
58920
- });
58921
- } catch (err) {
58922
- this.logger.debug("durable delete failed", { meta: {
58923
- collection: this.spec.collection,
58924
- key,
58925
- error: String(err)
58926
- } });
58927
- }
58928
- }
58929
- /**
58930
- * Drop every mirrored key NOT in `activeKeys`. Returns how many rows went.
58931
- *
58932
- * **The caller must hold an AUTHORITATIVE active set.** A prune driven by a
58933
- * fallible read is work destroyed on an error (D49/D130) — that is why this
58934
- * is a method a feature opts into rather than a policy the primitive runs.
58935
- * Best-effort per row: a failed delete keeps the key (retried next prune)
58936
- * rather than aborting the sweep.
58937
- */
58938
- async pruneExcept(activeKeys) {
58939
- let pruned = 0;
58940
- for (const key of [...this.mirror.keys()]) {
58941
- if (activeKeys.has(key)) continue;
58942
- try {
58943
- await this.store.delete.mutate({
58944
- collection: this.spec.collection,
58945
- key
58946
- });
58947
- this.mirror.delete(key);
58948
- pruned += 1;
58949
- } catch (err) {
58950
- this.logger.debug("durable prune delete failed", { meta: {
58951
- collection: this.spec.collection,
58952
- key,
58953
- error: String(err)
58954
- } });
58955
- }
58956
- }
58957
- return pruned;
58958
- }
58959
- /**
58960
- * Write-behind durable upsert. Best-effort and logged, never thrown at the
58961
- * decision path: the mirror already holds the truth for this process, and the
58962
- * worst a lost write can do is one silent re-seed after the next restart.
58963
- */
58964
- async persist(row) {
58965
- const deviceId = this.spec.deviceIdOf?.(row);
58966
- try {
58967
- await this.store.set.mutate({
58968
- collection: this.spec.collection,
58969
- key: this.spec.keyOf(row),
58970
- value: this.spec.toValue(row)
58971
- });
58972
- } catch (err) {
58973
- this.logger.warn("durable persist failed — this key may re-seed on boot", {
58974
- ...deviceId !== void 0 ? { tags: { deviceId } } : {},
58975
- meta: {
58976
- collection: this.spec.collection,
58977
- key: this.spec.keyOf(row),
58978
- error: String(err)
58979
- }
58980
- });
58981
- }
58982
- }
58983
- };
58984
61968
  var DEVICE_FEATURES_SPEC = {
58985
61969
  collection: "pipeline-orchestrator:device-features",
58986
61970
  columns: [
@@ -61337,16 +64321,20 @@ var PlacementService = class {
61337
64321
  let migrated = 0;
61338
64322
  const skipped = [];
61339
64323
  for (const [deviceId, config] of this.deps.ledger.configEntries()) {
64324
+ if (opts?.onlyDeviceIds !== void 0 && !opts.onlyDeviceIds.has(deviceId)) continue;
61340
64325
  if (this.deps.isIdleSessionCamera(deviceId)) continue;
61341
64326
  const current = this.deps.ledger.getAssignment(deviceId);
61342
- if (current?.pinned) continue;
64327
+ const eligibleNodes = this.deps.topology.detectionEligibleNodes(deviceId);
64328
+ const benchNode = this.deps.benchmarkNodeId?.() ?? null;
64329
+ const pinnedOffBenchNode = current !== null && current.agentNodeId === benchNode && !eligibleNodes.includes(current.agentNodeId);
64330
+ if (current?.pinned && !pinnedOffBenchNode) continue;
61343
64331
  const preferredAgent = await this.deps.readPipelinePin(deviceId);
61344
64332
  const decision = balance({
61345
64333
  nodes: talliedLoads(),
61346
64334
  preferredAgent,
61347
64335
  nodeCaps,
61348
64336
  weights: nodeWeights,
61349
- eligibleNodes: this.deps.topology.detectionEligibleNodes(deviceId)
64337
+ eligibleNodes
61350
64338
  });
61351
64339
  if (!decision) continue;
61352
64340
  if (decision.kind === "pending") {
@@ -61354,7 +64342,7 @@ var PlacementService = class {
61354
64342
  continue;
61355
64343
  }
61356
64344
  if (current && current.agentNodeId === decision.agentNodeId) continue;
61357
- if (current && minImprovement > 0) {
64345
+ if (current && minImprovement > 0 && eligibleNodes.includes(current.agentNodeId)) {
61358
64346
  const tallied = talliedLoads();
61359
64347
  const currentLoad = tallied.find((l) => l.nodeId === current.agentNodeId);
61360
64348
  const targetLoad = tallied.find((l) => l.nodeId === decision.agentNodeId);
@@ -63278,7 +66266,17 @@ var NodeTopologyService = class {
63278
66266
  remoteSourcingNodes: this._remoteSourcingNodes
63279
66267
  });
63280
66268
  }
66269
+ /**
66270
+ * The nodes that may run this camera's detection. While the cluster is in
66271
+ * benchmark mode for a node (D791), that node is left out — unless it is
66272
+ * the camera's only frame source, in which case the camera stays (never
66273
+ * blind) and the run is flagged contaminated.
66274
+ */
63281
66275
  detectionEligibleNodes(deviceId) {
66276
+ return eligibleUnderBenchmark(this.detectionEligibleNodesIgnoringBenchmark(deviceId), this.deps.benchmarkNodeId?.() ?? null);
66277
+ }
66278
+ /** {@link detectionEligibleNodes} as if no benchmark ran — the camera's real candidates. */
66279
+ detectionEligibleNodesIgnoringBenchmark(deviceId) {
63282
66280
  const frameSource = this.frameSourceNodes(deviceId);
63283
66281
  return this._enabledNodes.filter((nodeId) => frameSource.includes(nodeId));
63284
66282
  }
@@ -68847,7 +71845,8 @@ async function buildOrchestratorControllers(deps) {
68847
71845
  eventBus: deps.ctx().eventBus,
68848
71846
  logger: deps.ctx().logger,
68849
71847
  localNodeId: () => localNodeId,
68850
- addonId: () => deps.ctx().id
71848
+ addonId: () => deps.ctx().id,
71849
+ benchmarkNodeId: () => deps.benchmarkNodeId?.() ?? null
68851
71850
  });
68852
71851
  deps.setTopology(topology);
68853
71852
  let motionSliceGate = null;
@@ -69019,6 +72018,7 @@ async function buildOrchestratorControllers(deps) {
69019
72018
  return selection.kind === "selected" ? selection.camStreamId : null;
69020
72019
  };
69021
72020
  const placement = new PlacementService({
72021
+ benchmarkNodeId: () => deps.benchmarkNodeId?.() ?? null,
69022
72022
  ctx: () => deps.ctx(),
69023
72023
  ctxIfReady: () => deps.ctxIfReady(),
69024
72024
  ledger,
@@ -70254,6 +73254,9 @@ var pipelineOrchestratorWidgets = [{
70254
73254
  * is gone the node's recommended model runs.
70255
73255
  * - never: `stepToggles`, devices' enablement, pins of
70256
73256
  * embedding steps (cluster-scoped, changing one forces a re-index).
73257
+ * - never anything on a KEPT (node, device) — a custom-registry detector pin,
73258
+ * or an accelerator of its group — and never a camera's detector override
73259
+ * that pins a custom model (a canary), with its thresholds (D786).
70257
73260
  */
70258
73261
  /** The root detector step. */
70259
73262
  var DETECTOR_STEP_ID = "object-detection";
@@ -70261,6 +73264,17 @@ var DETECTOR_STEP_ID = "object-detection";
70261
73264
  function isModelPinKept(stepId) {
70262
73265
  return stepId.endsWith("-embedding");
70263
73266
  }
73267
+ /** The `keptDevices` key of one (node, device). */
73268
+ function keptDeviceKey(nodeId, deviceKey) {
73269
+ return `${nodeId}|${deviceKey}`;
73270
+ }
73271
+ function isKeptDevice(input, nodeId, deviceKey) {
73272
+ return input.keptDevices?.has(keptDeviceKey(nodeId, deviceKey)) === true;
73273
+ }
73274
+ /** A root-detector entry that pins a custom model — a canary, kept whole. */
73275
+ function isCustomDetectorPin(input, stepId, modelId) {
73276
+ return stepId === "object-detection" && modelId !== void 0 && input.isCustomModel?.(modelId) === true;
73277
+ }
70264
73278
  function boundKeysFor(input, stepId) {
70265
73279
  return input.boundKeysByStep[stepId] ?? [];
70266
73280
  }
@@ -70320,6 +73334,10 @@ function applyNodeAutoTune(blob, input) {
70320
73334
  const nextDevices = {};
70321
73335
  for (const deviceKey of deviceKeys) {
70322
73336
  const device = stored[deviceKey];
73337
+ if (isKeptDevice(input, nodeId, deviceKey)) {
73338
+ if (device !== void 0) nextDevices[deviceKey] = device;
73339
+ continue;
73340
+ }
70323
73341
  const target = targets.get(deviceKey);
70324
73342
  const steps = { ...device?.steps };
70325
73343
  if (target !== void 0 && steps["object-detection"] === void 0) steps[DETECTOR_STEP_ID] = {};
@@ -70364,6 +73382,7 @@ function applyNodeAutoTune(blob, input) {
70364
73382
  /** Scrub a wholesale snapshot tree; collects one clear per changed step. */
70365
73383
  function scrubTree(steps, input, onClear) {
70366
73384
  return steps.map((step) => {
73385
+ if (isCustomDetectorPin(input, step.addonId, step.modelId)) return step;
70367
73386
  const children = step.children ? scrubTree(step.children, input, onClear) : void 0;
70368
73387
  const inScope = step.addonId === "object-detection" || pinInScope(step.addonId);
70369
73388
  const { kept, removed } = stripBound(step.settings, boundKeysFor(input, step.addonId));
@@ -70397,8 +73416,16 @@ function applyCameraAutoTune(blob, input) {
70397
73416
  }
70398
73417
  const nextByDevice = {};
70399
73418
  for (const [deviceKey, byStep] of Object.entries(byDevice)) {
73419
+ if (isKeptDevice(input, nodeId, deviceKey)) {
73420
+ nextByDevice[deviceKey] = byStep;
73421
+ continue;
73422
+ }
70400
73423
  const nextByStep = {};
70401
73424
  for (const [stepId, patch] of Object.entries(byStep)) {
73425
+ if (isCustomDetectorPin(input, stepId, patch.modelId)) {
73426
+ nextByStep[stepId] = patch;
73427
+ continue;
73428
+ }
70402
73429
  const inScope = stepId === "object-detection" || pinInScope(stepId);
70403
73430
  const { kept, removed } = stripBound(patch.settings, boundKeysFor(input, stepId));
70404
73431
  const dropPin = inScope && patch.modelId !== void 0;
@@ -70487,6 +73514,13 @@ function applyCameraAutoTune(blob, input) {
70487
73514
  * 5. re-dispatch the cameras of every touched node.
70488
73515
  * A failure after phase 2 leaves "overrides cleared, previous node model",
70489
73516
  * which is a valid configuration (the defaults run); re-running converges.
73517
+ *
73518
+ * Custom models are never overwritten (D786): a (node, device) whose root
73519
+ * detector is pinned to a custom-registry model is KEPT by the recommender, and
73520
+ * the diff writes nothing there; a camera override pinning one (a canary) is
73521
+ * kept with its thresholds, and its per-camera detector thresholds are not
73522
+ * cleared. The plan warns about each. A registry that does not answer keeps
73523
+ * every pin outside the measured catalog — unknown is never "not custom".
70490
73524
  */
70491
73525
  function minSessionsOf(input) {
70492
73526
  return input.minSessionsPerDevice ?? 2;
@@ -70519,6 +73553,46 @@ function boundKeysOf(catalogs) {
70519
73553
  }
70520
73554
  return Object.fromEntries([...byStep].map(([step, set]) => [step, [...set].toSorted()]));
70521
73555
  }
73556
+ /** `node|deviceKey → custom model` of every stored root-detector pin to a custom model. */
73557
+ function storedCustomPins(agent, isCustomModel) {
73558
+ const out = /* @__PURE__ */ new Map();
73559
+ for (const [nodeId, entry] of Object.entries(agent)) for (const [deviceKey, device] of Object.entries(entry?.inferenceDevices ?? {})) {
73560
+ const modelId = device?.steps?.[DETECTOR_STEP_ID]?.modelId;
73561
+ if (modelId !== void 0 && isCustomModel(modelId)) out.set(keptDeviceKey(nodeId, deviceKey), modelId);
73562
+ }
73563
+ return out;
73564
+ }
73565
+ /** Every camera override that pins a custom root detector (a canary). */
73566
+ function cameraCustomPins(cameras, isCustomModel) {
73567
+ const out = [];
73568
+ for (const [key, camera] of Object.entries(cameras)) {
73569
+ const deviceId = Number(key);
73570
+ for (const [nodeId, byDevice] of Object.entries(camera?.stepOverridesByDevice ?? {})) for (const [deviceKey, byStep] of Object.entries(byDevice)) {
73571
+ const modelId = byStep[DETECTOR_STEP_ID]?.modelId;
73572
+ if (modelId !== void 0 && isCustomModel(modelId)) out.push({
73573
+ deviceId,
73574
+ nodeId,
73575
+ deviceKey,
73576
+ modelId
73577
+ });
73578
+ }
73579
+ for (const [nodeId, snapshot] of Object.entries(camera?.pipelineByAgent ?? {})) {
73580
+ const walk = (steps) => {
73581
+ for (const step of steps) {
73582
+ if (step.addonId === "object-detection" && step.modelId !== void 0 && isCustomModel(step.modelId)) out.push({
73583
+ deviceId,
73584
+ nodeId,
73585
+ deviceKey: null,
73586
+ modelId: step.modelId
73587
+ });
73588
+ if (step.children) walk(step.children);
73589
+ }
73590
+ };
73591
+ walk(snapshot.steps);
73592
+ }
73593
+ }
73594
+ return out.toSorted((a, b) => a.deviceId - b.deviceId || a.nodeId.localeCompare(b.nodeId));
73595
+ }
70522
73596
  var AutoTuneService = class {
70523
73597
  deps;
70524
73598
  applying = false;
@@ -70556,8 +73630,13 @@ var AutoTuneService = class {
70556
73630
  minSessionsPerDevice
70557
73631
  });
70558
73632
  }
70559
- /** Gather every node and recommend. Nothing but hardware, catalog and the sizing goes in. */
70560
- async recommend(headroom, minSessionsPerDevice) {
73633
+ /**
73634
+ * Gather every node and recommend. Nothing but hardware, catalog, the sizing
73635
+ * and — for a plan — the custom pins goes in. `customPins` maps
73636
+ * `node|deviceKey` to the custom model pinned there; shares pass none
73637
+ * (capacity is a property of the hardware, not of what it runs).
73638
+ */
73639
+ async recommend(headroom, minSessionsPerDevice, customPins = /* @__PURE__ */ new Map()) {
70561
73640
  const nodeIds = [...new Set(["hub", ...this.deps.runnerNodeIds()])].toSorted();
70562
73641
  const gathered = await Promise.all(nodeIds.map(async (nodeId) => {
70563
73642
  const [devices, hardware, catalog] = await Promise.all([
@@ -70576,30 +73655,49 @@ var AutoTuneService = class {
70576
73655
  nodeId: g.nodeId,
70577
73656
  reachable: g.devices.reachable && g.catalog !== null,
70578
73657
  hardware: g.hardware,
70579
- devices: g.devices.devices.map((d) => ({
70580
- key: d.key,
70581
- format: d.format,
70582
- enabled: d.enabled,
70583
- available: d.available
70584
- })),
73658
+ devices: g.devices.devices.map((d) => {
73659
+ const pinned = customPins.get(keptDeviceKey(g.nodeId, d.key));
73660
+ return {
73661
+ key: d.key,
73662
+ format: d.format,
73663
+ enabled: d.enabled,
73664
+ available: d.available,
73665
+ ...pinned !== void 0 ? { customPinModelId: pinned } : {}
73666
+ };
73667
+ }),
70585
73668
  formatsByModel: formatsByModelOf(g.catalog)
70586
73669
  }));
70587
73670
  const observedFps = await this.deps.cameraFps();
70588
73671
  const cameraFps = observedFps !== null && observedFps > 0 ? observedFps : 10;
73672
+ let measured = /* @__PURE__ */ new Map();
73673
+ try {
73674
+ measured = await this.deps.measuredLatency?.(nodeIds) ?? /* @__PURE__ */ new Map();
73675
+ } catch (err) {
73676
+ this.deps.logger.warn("auto tune: node benchmark results unreadable — the table decides", { meta: { error: errMsg(err) } });
73677
+ }
70589
73678
  return {
70590
73679
  gathered,
70591
73680
  nodes,
70592
- recommendations: nodes.map((node) => recommendNode({
70593
- node,
70594
- headroom,
70595
- minSessionsPerDevice,
70596
- cameraFps
70597
- })),
73681
+ recommendations: nodes.map((node) => {
73682
+ const own = measured.get(node.nodeId);
73683
+ return recommendNode({
73684
+ node,
73685
+ headroom,
73686
+ minSessionsPerDevice,
73687
+ cameraFps,
73688
+ ...own !== void 0 ? { measured: own } : {}
73689
+ });
73690
+ }),
70598
73691
  cameraFps
70599
73692
  };
70600
73693
  }
70601
73694
  async computePlan(headroom, minSessionsPerDevice) {
70602
- const { gathered, nodes, recommendations, cameraFps } = await this.recommend(headroom, minSessionsPerDevice);
73695
+ const warnings = [];
73696
+ const isCustomModel = await this.customModelPredicate(warnings);
73697
+ const agentSettings = await this.deps.readAgentSettings();
73698
+ const cameraSettings = await this.deps.readCameraSettings();
73699
+ const customPins = storedCustomPins(agentSettings, isCustomModel);
73700
+ const { gathered, nodes, recommendations, cameraFps } = await this.recommend(headroom, minSessionsPerDevice, customPins);
70603
73701
  const touchedNodes = new Set(recommendations.filter((r) => !r.skipped).map((r) => r.nodeId));
70604
73702
  const targets = /* @__PURE__ */ new Map();
70605
73703
  const formats = /* @__PURE__ */ new Map();
@@ -70621,18 +73719,40 @@ var AutoTuneService = class {
70621
73719
  targets.set(rec.nodeId, list);
70622
73720
  formats.set(rec.nodeId, byKey);
70623
73721
  }
73722
+ const keptDevices = new Set(customPins.keys());
73723
+ for (const rec of recommendations) for (const d of rec.devices) if (d.kept !== void 0) keptDevices.add(keptDeviceKey(rec.nodeId, d.deviceKey));
70624
73724
  const diffInput = {
70625
73725
  touchedNodes,
70626
73726
  targets,
70627
- boundKeysByStep: boundKeysOf(gathered.map((g) => g.catalog))
73727
+ boundKeysByStep: boundKeysOf(gathered.map((g) => g.catalog)),
73728
+ keptDevices,
73729
+ isCustomModel
70628
73730
  };
70629
- const nodeChanges = applyNodeAutoTune(await this.deps.readAgentSettings(), diffInput).changes;
70630
- const cameraClears = applyCameraAutoTune(await this.deps.readCameraSettings(), diffInput).clears;
70631
- const warnings = [];
73731
+ const nodeChanges = applyNodeAutoTune(agentSettings, diffInput).changes;
73732
+ const cameraClears = applyCameraAutoTune(cameraSettings, diffInput).clears;
73733
+ const canaries = cameraCustomPins(cameraSettings, isCustomModel);
73734
+ for (const [key, modelId] of [...customPins].toSorted(([a], [b]) => a.localeCompare(b))) {
73735
+ const [nodeId = "", deviceKey = ""] = key.split("|");
73736
+ warnings.push({
73737
+ kind: "custom-pin-kept",
73738
+ nodeId,
73739
+ deviceKey,
73740
+ deviceId: null,
73741
+ modelId
73742
+ });
73743
+ }
73744
+ for (const c of canaries) warnings.push({
73745
+ kind: "custom-pin-kept",
73746
+ nodeId: c.nodeId,
73747
+ deviceKey: c.deviceKey,
73748
+ deviceId: c.deviceId,
73749
+ modelId: c.modelId
73750
+ });
73751
+ const canaryCameras = new Set(canaries.map((c) => c.deviceId));
70632
73752
  let detectorStoreClears = [];
70633
73753
  if (touchedNodes.size > 0) try {
70634
73754
  const answer = await this.deps.detectorStore({ dryRun: true });
70635
- detectorStoreClears = answer.entries.map((e) => ({
73755
+ detectorStoreClears = answer.entries.filter((e) => !canaryCameras.has(e.deviceId)).map((e) => ({
70636
73756
  deviceId: e.deviceId,
70637
73757
  keys: [...e.keys]
70638
73758
  }));
@@ -70666,7 +73786,11 @@ var AutoTuneService = class {
70666
73786
  picks: recommendations.map((r) => ({
70667
73787
  nodeId: r.nodeId,
70668
73788
  skipped: r.skipped,
70669
- devices: r.devices.map((d) => [d.deviceKey, d.chosen?.modelId ?? null])
73789
+ devices: r.devices.map((d) => [
73790
+ d.deviceKey,
73791
+ d.chosen?.modelId ?? null,
73792
+ d.kept ?? null
73793
+ ])
70670
73794
  })),
70671
73795
  nodeChanges,
70672
73796
  cameraClears,
@@ -70687,6 +73811,25 @@ var AutoTuneService = class {
70687
73811
  formats
70688
73812
  };
70689
73813
  }
73814
+ /**
73815
+ * "Is this model custom?" from the registry. A registry that does not
73816
+ * answer is NOT an empty registry: every pin outside the measured catalog
73817
+ * is then treated as custom, and the plan says so.
73818
+ */
73819
+ async customModelPredicate(warnings) {
73820
+ try {
73821
+ const ids = await this.deps.readCustomModelIds();
73822
+ return (modelId) => ids.has(modelId);
73823
+ } catch (err) {
73824
+ const error = errMsg(err);
73825
+ this.deps.logger.warn("auto tune: custom-model registry unreadable — every pin outside the measured catalog is kept", { meta: { error } });
73826
+ warnings.push({
73827
+ kind: "custom-registry-unreadable",
73828
+ error
73829
+ });
73830
+ return (modelId) => !(modelId in DETECTOR_ACCURACY);
73831
+ }
73832
+ }
70690
73833
  async apply(input) {
70691
73834
  if (this.applying) return {
70692
73835
  kind: "refused",
@@ -71305,6 +74448,12 @@ var PipelineOrchestratorAddon = class PipelineOrchestratorAddon extends BaseAddo
71305
74448
  readShareSource = () => DEFAULT_SHARE_SOURCE;
71306
74449
  autoTuneService = null;
71307
74450
  effectiveSharesService = null;
74451
+ /** The node benchmark lifecycle (D791); null before init. */
74452
+ nodeBenchmark = null;
74453
+ nodeBenchmarkStore = null;
74454
+ nodeBenchmarkTimer = null;
74455
+ /** How often the active run's runner is polled (a test seam). */
74456
+ nodeBenchmarkPollMs = 3e3;
71308
74457
  /**
71309
74458
  * Single owner of the shared placement state (`assignments`/`cameraConfigs`/
71310
74459
  * `pendingReasons`/`dispatchingDevices`) — see `placement-ledger.ts` (S6
@@ -71582,6 +74731,7 @@ var PipelineOrchestratorAddon = class PipelineOrchestratorAddon extends BaseAddo
71582
74731
  rebalance: (opts) => this.rebalance(opts),
71583
74732
  setCameraPipelineForAgent: (input) => this.setCameraPipelineForAgent(input),
71584
74733
  maintenancePaused: () => this.storageMigrationLeaseId !== null,
74734
+ benchmarkNodeId: () => this.nodeBenchmark?.modeNodeId() ?? null,
71585
74735
  placementStats: this.placementStats,
71586
74736
  setTopology: (topology) => {
71587
74737
  this.topology = topology;
@@ -71631,6 +74781,7 @@ var PipelineOrchestratorAddon = class PipelineOrchestratorAddon extends BaseAddo
71631
74781
  this.autoRebalanceTimer = controllers.autoRebalanceTimer;
71632
74782
  this.nodeStressLts = controllers.nodeStressLts;
71633
74783
  this.nodeStressLtsTimer = controllers.nodeStressLtsTimer;
74784
+ await this.initNodeBenchmark();
71634
74785
  const widgetsProvider = { listWidgets: async () => pipelineOrchestratorWidgets };
71635
74786
  this.clusterModelChoiceReady = true;
71636
74787
  return {
@@ -71717,6 +74868,10 @@ var PipelineOrchestratorAddon = class PipelineOrchestratorAddon extends BaseAddo
71717
74868
  clearInterval(this.nodeStressLtsTimer);
71718
74869
  this.nodeStressLtsTimer = null;
71719
74870
  }
74871
+ if (this.nodeBenchmarkTimer !== null) {
74872
+ clearInterval(this.nodeBenchmarkTimer);
74873
+ this.nodeBenchmarkTimer = null;
74874
+ }
71720
74875
  this.nodeStressLts = null;
71721
74876
  this.unsubOrchestratorSubscriptions?.();
71722
74877
  this.unsubOrchestratorSubscriptions = null;
@@ -71942,6 +75097,10 @@ var PipelineOrchestratorAddon = class PipelineOrchestratorAddon extends BaseAddo
71942
75097
  * node id, or `null` when neither key holds a pin (auto-balance).
71943
75098
  */
71944
75099
  async readPipelinePin(deviceId) {
75100
+ return pinUnderBenchmark(await this.readStoredPipelinePin(deviceId), this.nodeBenchmark?.modeNodeId() ?? null, this.topology?.detectionEligibleNodesIgnoringBenchmark(deviceId) ?? []);
75101
+ }
75102
+ /** The stored node pin as written — see {@link readPipelinePin} for the placement view. */
75103
+ async readStoredPipelinePin(deviceId) {
71945
75104
  if (!this.ctx?.settings) return null;
71946
75105
  try {
71947
75106
  const settings = await this.ctx.settings.readDeviceStore(deviceId);
@@ -72705,7 +75864,8 @@ var PipelineOrchestratorAddon = class PipelineOrchestratorAddon extends BaseAddo
72705
75864
  rewriteAgentSettings: (transform) => store.rewriteAgentSettingsMap(transform),
72706
75865
  rewriteCameraSettings: (transform) => store.rewriteCameraSettingsMap(transform),
72707
75866
  cameraFps: async () => this.autoTuneCameraFps(),
72708
- measuredCapacity: NO_MEASURED_CAPACITY,
75867
+ measuredCapacity: { readMeasuredCapacity: (nodeIds) => this.readMeasuredCapacity(nodeIds) },
75868
+ measuredLatency: (nodeIds) => this.readMeasuredLatency(nodeIds),
72709
75869
  detectorStore: async (input) => {
72710
75870
  const api = this.ctx.api;
72711
75871
  if (!api) throw new Error("ctx.api unavailable");
@@ -72724,6 +75884,12 @@ var PipelineOrchestratorAddon = class PipelineOrchestratorAddon extends BaseAddo
72724
75884
  format
72725
75885
  }, nodePin(nodeId));
72726
75886
  },
75887
+ readCustomModelIds: async () => {
75888
+ const api = this.ctx.api;
75889
+ if (!api) throw new Error("ctx.api unavailable");
75890
+ const models = await api.customModelRegistry.listModels.query();
75891
+ return new Set(models.map((m) => m.entry.id));
75892
+ },
72727
75893
  invalidateCatalog: () => store.invalidateCatalog(),
72728
75894
  redispatch: (nodeId) => this.placement.redispatchCamerasOnAgent(nodeId),
72729
75895
  now: () => Date.now()
@@ -72832,6 +75998,166 @@ var PipelineOrchestratorAddon = class PipelineOrchestratorAddon extends BaseAddo
72832
75998
  } });
72833
75999
  });
72834
76000
  }
76001
+ /** The benchmark reconcile cadence — a restore owed is never older than this. */
76002
+ static NODE_BENCHMARK_RECONCILE_MS = 3e4;
76003
+ /** How long a node's answered pool tuning is reused for result standings. */
76004
+ static CURRENT_POOL_TTL_MS = 6e4;
76005
+ currentPoolCache = /* @__PURE__ */ new Map();
76006
+ /**
76007
+ * Build the benchmark lifecycle, then reconcile its manifest: a run that
76008
+ * died with the previous process is restored NOW, before anything else
76009
+ * can trust the cluster's placement. Never fails the boot.
76010
+ */
76011
+ async initNodeBenchmark() {
76012
+ const store = new NodeBenchmarkStore({
76013
+ store: this.ctx.api.settingsStore,
76014
+ logger: this.ctx.logger
76015
+ });
76016
+ try {
76017
+ await NodeBenchmarkStore.declare(this.ctx.api.settingsStore);
76018
+ await store.load();
76019
+ } catch (err) {
76020
+ this.ctx.logger.warn("node benchmark: store unavailable at boot — runs refused until it answers", { meta: { error: errMsg$2(err) } });
76021
+ }
76022
+ this.nodeBenchmarkStore = store;
76023
+ this.nodeBenchmark = createNodeBenchmarkService({
76024
+ ctx: () => this.ctx,
76025
+ persistence: store,
76026
+ ledger: this.ledger,
76027
+ topology: this.topology,
76028
+ rebalance: (opts) => this.placement.rebalance(opts),
76029
+ activeSessionNode: (deviceId) => this.session.activeSessionNode(deviceId),
76030
+ readStoredPipelinePin: (deviceId) => this.readStoredPipelinePin(deviceId),
76031
+ listDeviceKeys: async (nodeId) => (await this.getNodeInferenceDevices({ nodeId })).devices.map((d) => d.key),
76032
+ sessionsNow: () => this.placementStats.snapshot(6e4).sessionsNow,
76033
+ suggestShares: () => this.autoTune().suggestShares({
76034
+ headroom: AUTO_SHARE_HEADROOM,
76035
+ source: "benchmark"
76036
+ }),
76037
+ pollIntervalMs: this.nodeBenchmarkPollMs,
76038
+ onResultsWritten: (nodeId) => {
76039
+ this.currentPoolCache.clear();
76040
+ this.noteShareChange(`benchmark-results:${nodeId}`);
76041
+ }
76042
+ });
76043
+ await this.nodeBenchmark.reconcile("boot").catch((err) => {
76044
+ this.ctx.logger.warn("node benchmark: boot reconcile failed — retried on the next tick", { meta: { error: errMsg$2(err) } });
76045
+ });
76046
+ this.nodeBenchmarkTimer = setInterval(() => {
76047
+ this.nodeBenchmark?.reconcile("tick").catch((err) => {
76048
+ this.ctx.logger.debug("node benchmark: reconcile tick failed", { meta: { error: errMsg$2(err) } });
76049
+ });
76050
+ }, PipelineOrchestratorAddon.NODE_BENCHMARK_RECONCILE_MS);
76051
+ this.nodeBenchmarkTimer.unref?.();
76052
+ }
76053
+ benchmarkService() {
76054
+ if (this.nodeBenchmark === null) throw new Error("node benchmark: not initialised");
76055
+ return this.nodeBenchmark;
76056
+ }
76057
+ async getNodeBenchmarkPreflight(input) {
76058
+ return this.benchmarkService().preflight(input);
76059
+ }
76060
+ async startNodeBenchmark(input) {
76061
+ return this.benchmarkService().start(input);
76062
+ }
76063
+ async getNodeBenchmarkStatus() {
76064
+ return this.benchmarkService().status();
76065
+ }
76066
+ async abortNodeBenchmark(input) {
76067
+ return this.benchmarkService().abort(input.runId);
76068
+ }
76069
+ async getNodeBenchmarkCandidates(input) {
76070
+ return readNodeBenchmarkCandidates({
76071
+ nodeId: input.nodeId,
76072
+ readDevices: (nodeId) => this.getNodeInferenceDevices({ nodeId }),
76073
+ readCatalog: (nodeId) => this.settingsStore.readCatalogForNode(nodeId),
76074
+ autoTunePicks: async (nodeId) => {
76075
+ const node = (await this.autoTune().plan({ headroom: AUTO_SHARE_HEADROOM })).nodes.find((n) => n.nodeId === nodeId);
76076
+ const out = {};
76077
+ for (const d of node?.devices ?? []) if (d.chosen !== null) out[d.deviceKey] = d.chosen.modelId;
76078
+ return out;
76079
+ },
76080
+ logger: this.ctx.logger
76081
+ });
76082
+ }
76083
+ async listNodeBenchmarkResults(input) {
76084
+ const rows = this.nodeBenchmarkStore?.listResults() ?? [];
76085
+ return listNodeBenchmarkResultViews(rows, input, {
76086
+ nowMs: Date.now(),
76087
+ currentPools: await this.readCurrentPools(rows)
76088
+ });
76089
+ }
76090
+ async deleteNodeBenchmarkResult(input) {
76091
+ const deleted = await this.nodeBenchmarkStore?.deleteResult(input.id) ?? false;
76092
+ if (deleted) {
76093
+ this.ctx.logger.info("node benchmark result deleted", { meta: { id: input.id } });
76094
+ this.noteShareChange("benchmark-result-deleted");
76095
+ }
76096
+ return { deleted };
76097
+ }
76098
+ /**
76099
+ * The pool each (node, device) of `rows` runs today, for the fingerprint
76100
+ * check. Unanswered = unknown, which never makes a result stale.
76101
+ */
76102
+ async readCurrentPools(rows) {
76103
+ const out = {};
76104
+ const api = this.ctx.api;
76105
+ const pairs = new Set(rows.map((r) => `${r.nodeId}\u0000${r.cell.deviceKey}`));
76106
+ const now = Date.now();
76107
+ await Promise.all([...pairs].map(async (key) => {
76108
+ const [nodeId = "", deviceKey = ""] = key.split("\0");
76109
+ const hit = this.currentPoolCache.get(key);
76110
+ let pool;
76111
+ if (hit !== void 0 && now - hit.at < PipelineOrchestratorAddon.CURRENT_POOL_TTL_MS) pool = hit.pool;
76112
+ else {
76113
+ pool = null;
76114
+ if (this.topology?.collectRunnerNodeIds().includes(nodeId)) try {
76115
+ const t = await api.pipelineExecutor.getEffectiveTuning.query({ deviceKey }, nodePin(nodeId));
76116
+ pool = {
76117
+ runtime: t.runtime,
76118
+ numWorkers: t.numWorkers,
76119
+ concurrency: t.concurrency,
76120
+ batchMode: t.batchMode
76121
+ };
76122
+ } catch {
76123
+ pool = null;
76124
+ }
76125
+ this.currentPoolCache.set(key, {
76126
+ at: now,
76127
+ pool
76128
+ });
76129
+ }
76130
+ if (pool !== null) out[nodeId] = {
76131
+ ...out[nodeId],
76132
+ [deviceKey]: pool
76133
+ };
76134
+ }));
76135
+ return out;
76136
+ }
76137
+ /** Each node's fresh detector measurements — what Auto tune lays over the table. */
76138
+ async readMeasuredLatency(nodeIds) {
76139
+ const rows = this.nodeBenchmarkStore?.listResults() ?? [];
76140
+ const out = /* @__PURE__ */ new Map();
76141
+ if (rows.length === 0) return out;
76142
+ const ctx = {
76143
+ nowMs: Date.now(),
76144
+ currentPools: await this.readCurrentPools(rows)
76145
+ };
76146
+ for (const nodeId of nodeIds) {
76147
+ const cells = measuredDetectorCells(rows, nodeId, ctx);
76148
+ if (Object.keys(cells).length > 0) out.set(nodeId, cells);
76149
+ }
76150
+ return out;
76151
+ }
76152
+ /** The measured capacity the automatic shares read (D755 `NodeCapacitySource`). */
76153
+ async readMeasuredCapacity(nodeIds) {
76154
+ const rows = this.nodeBenchmarkStore?.listResults() ?? [];
76155
+ if (rows.length === 0) return /* @__PURE__ */ new Map();
76156
+ return measuredCapacityFromResults(rows, nodeIds, {
76157
+ nowMs: Date.now(),
76158
+ currentPools: await this.readCurrentPools(rows)
76159
+ });
76160
+ }
72835
76161
  async getEffectiveShares() {
72836
76162
  const service = this.effectiveShares();
72837
76163
  const nodeIds = [...new Set(["hub", ...this.topology.collectRunnerNodeIds()])].toSorted();