@camstack/server 1.2.74 → 1.2.76

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.
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.seedBuiltinsFromClosure = seedBuiltinsFromClosure;
37
+ /**
38
+ * Self-heal the agent's builtins package before the infra scan.
39
+ *
40
+ * `bootCoreAddons` scans `config.addonsDir` and nothing else, the image seed
41
+ * ships no `@camstack/system`, and the running closure's copy is NOT loaded
42
+ * as an addon — so an agent whose `/data/addons/@camstack/system` is missing
43
+ * (greenfield container, an operator cleanup, a failed deploy) boots with
44
+ * zero infrastructure. Before D87 that produced a silently gutted node; with
45
+ * the D87 guard it produces a crash-loop. Both are wrong answers to a
46
+ * question the node can answer itself: the closure it is RUNNING carries the
47
+ * exact builtins package it needs.
48
+ *
49
+ * The copy is files-list shaped — `package.json` + `dist` — and NEVER
50
+ * `node_modules`: a nested `node_modules/@camstack/*` carries its own
51
+ * `package.json`s, the addon scan discovers those too, and the same addon
52
+ * initializing twice is a boot abort (measured live 2026-08-08, twice, two
53
+ * different vehicles).
54
+ */
55
+ const fs = __importStar(require("node:fs"));
56
+ const path = __importStar(require("node:path"));
57
+ function seedBuiltinsFromClosure(addonsDir, log = console.log, resolveClosurePkg = () => require.resolve('@camstack/system/package.json')) {
58
+ const target = path.join(addonsDir, '@camstack', 'system');
59
+ if (fs.existsSync(path.join(target, 'package.json')))
60
+ return 'present';
61
+ let closurePkgJson;
62
+ try {
63
+ closurePkgJson = resolveClosurePkg();
64
+ }
65
+ catch {
66
+ log('[Agent] builtins seed: @camstack/system not resolvable from the closure — cannot self-heal');
67
+ return 'unavailable';
68
+ }
69
+ const closureRoot = path.dirname(closurePkgJson);
70
+ const closureDist = path.join(closureRoot, 'dist');
71
+ if (!fs.existsSync(closureDist)) {
72
+ log(`[Agent] builtins seed: closure copy at ${closureRoot} has no dist — cannot self-heal`);
73
+ return 'unavailable';
74
+ }
75
+ // The WHOLE package, node_modules included. A deps-free copy was tried
76
+ // first and failed live (2026-08-08): the builtins' dist requires native
77
+ // deps (better-sqlite3) that resolve relative to the COPY, so the addon
78
+ // scan logged "Failed to scan" and the guard aborted with "no addon
79
+ // under /data/addons" — while the log right above it said the seed had
80
+ // run. Nested `node_modules` are safe: the scan reads one level of
81
+ // `addonsDir/@scope/*`, never inside a package. (The double-registration
82
+ // the deps-free copy was guarding against was actually the per-capability
83
+ // init loop — fixed by `planInfraBoot` — not nested discovery.)
84
+ fs.mkdirSync(path.dirname(target), { recursive: true });
85
+ fs.cpSync(closureRoot, target, { recursive: true });
86
+ log(`[Agent] builtins seed: @camstack/system was missing under ${addonsDir} — ` +
87
+ `seeded the full package from the running closure (${closureRoot})`);
88
+ return 'seeded';
89
+ }
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.planInfraBoot = planInfraBoot;
4
+ exports.planToResolutions = planToResolutions;
5
+ /**
6
+ * `log-destination` prefers `hub-forwarder` (ships agent logs to the hub)
7
+ * over a local winston — mirrors the historical inline choice.
8
+ */
9
+ function pickCandidate(capName, candidates) {
10
+ if (capName === 'log-destination') {
11
+ return candidates.find((a) => a.id === 'hub-forwarder') ?? candidates[0];
12
+ }
13
+ return candidates[0];
14
+ }
15
+ function planInfraBoot(addons, infraList) {
16
+ const initialized = new Set();
17
+ const steps = [];
18
+ for (const infra of infraList) {
19
+ const candidates = addons.filter((a) => a.capabilities.includes(infra.name));
20
+ const chosen = pickCandidate(infra.name, candidates);
21
+ if (!chosen) {
22
+ steps.push({
23
+ capName: infra.name,
24
+ required: infra.required,
25
+ addonId: null,
26
+ initialize: false,
27
+ });
28
+ continue;
29
+ }
30
+ const first = !initialized.has(chosen.id);
31
+ if (first)
32
+ initialized.add(chosen.id);
33
+ steps.push({
34
+ capName: infra.name,
35
+ required: infra.required,
36
+ addonId: chosen.id,
37
+ initialize: first,
38
+ });
39
+ }
40
+ return steps;
41
+ }
42
+ /** Project a plan onto the guard's resolution shape. */
43
+ function planToResolutions(steps) {
44
+ return steps.map((s) => ({ name: s.capName, required: s.required, addonId: s.addonId }));
45
+ }
@@ -46,6 +46,8 @@ const agent_config_js_1 = require("./agent-config.js");
46
46
  const agent_update_service_js_1 = require("./agent-update-service.js");
47
47
  const server_management_provider_js_1 = require("../api/core/server-management.provider.js");
48
48
  const infra_boot_guard_js_1 = require("./infra-boot-guard.js");
49
+ const infra_boot_plan_js_1 = require("./infra-boot-plan.js");
50
+ const builtins_seed_js_1 = require("./builtins-seed.js");
49
51
  const agent_service_js_1 = require("./agent-service.js");
50
52
  const agent_group_runner_js_1 = require("./agent-group-runner.js");
51
53
  const register_agent_cap_dispatch_js_1 = require("./register-agent-cap-dispatch.js");
@@ -767,6 +769,10 @@ async function startAgent(configPath) {
767
769
  // Core infra addons to load on agent — all infra including log-destination (hub-forwarder)
768
770
  const AGENT_INFRA = system_1.INFRA_CAPABILITIES;
769
771
  async function bootCoreAddons(broker, config, registry, loadedAddons, loggerFactory) {
772
+ // Self-heal FIRST: a missing builtins package is answerable from the
773
+ // running closure (see `builtins-seed.ts`) — without this, the D87 guard
774
+ // below turns a recoverable gap into a crash-loop.
775
+ (0, builtins_seed_js_1.seedBuiltinsFromClosure)(config.addonsDir);
770
776
  // Scan every installed addon package — infra providers may live outside
771
777
  // `@camstack/system` (e.g. `@camstack/addon-platform-probe-native`).
772
778
  const packageDirs = resolveAddonPackageDirs(config.addonsDir);
@@ -784,30 +790,39 @@ async function bootCoreAddons(broker, config, registry, loadedAddons, loggerFact
784
790
  console.warn(`[Agent] Failed to scan ${dir}: ${(0, types_2.errMsg)(err)}`);
785
791
  }
786
792
  }
787
- // Every infra capability and the addon that will serve it — collected as we
788
- // go so `assertRequiredInfraResolved` can name ALL missing required caps in
789
- // one throw once the pass is done (see `infra-boot-guard.ts`).
790
- const resolutions = [];
791
- for (const infra of AGENT_INFRA) {
792
- const candidates = loader.listAddons().filter((a) => a.declaration.capabilities?.some((c) => {
793
- const capName = typeof c === 'string' ? c : c.name;
794
- return capName === infra.name;
795
- }));
796
- // For log-destination, prefer hub-forwarder over winston-logging
797
- const addon = infra.name === 'log-destination'
798
- ? (candidates.find((a) => a.declaration.id === 'hub-forwarder') ?? candidates[0])
799
- : candidates[0];
800
- if (!addon) {
793
+ // Resolution is per-CAPABILITY (the guard names every missing required
794
+ // cap), initialization is per-ADDON: since D44 one addon legitimately
795
+ // serves several infra caps (`storage-orchestrator` owns both `storage`
796
+ // and `settings-store`), and initializing it once per capability
797
+ // re-registers the same provider — a boot abort by the registry's own
798
+ // double-registration error. That combination took both agents down on
799
+ // 2026-08-08 the moment D87 promoted infra failures to fatal. The plan
800
+ // makes the dedupe explicit and testable (see `infra-boot-plan.ts`).
801
+ const byId = new Map(loader.listAddons().map((a) => [a.declaration.id, a]));
802
+ const plan = (0, infra_boot_plan_js_1.planInfraBoot)(loader.listAddons().map((a) => ({
803
+ id: a.declaration.id,
804
+ capabilities: (a.declaration.capabilities ?? []).map((c) => typeof c === 'string' ? c : c.name),
805
+ })), AGENT_INFRA);
806
+ const resolutions = (0, infra_boot_plan_js_1.planToResolutions)(plan);
807
+ for (const step of plan) {
808
+ if (step.addonId === null) {
801
809
  // Say it per-capability (an OPTIONAL miss is the only thing this line
802
- // will ever report once the guard below is in place), then record the
803
- // miss so the required ones abort boot together rather than one at a
804
- // time.
805
- console.error(`[Agent] Infrastructure addon for "${infra.name}" not found`);
806
- resolutions.push({ name: infra.name, required: infra.required, addonId: null });
810
+ // will ever report given the guard below); the required ones abort
811
+ // boot together after the pass.
812
+ console.error(`[Agent] Infrastructure addon for "${step.capName}" not found`);
807
813
  continue;
808
814
  }
815
+ if (!step.initialize) {
816
+ // Served by an addon an earlier capability already booted — the
817
+ // entry exists so a deployment missing it fails loudly, not to boot
818
+ // the addon twice.
819
+ console.log(`[Agent] Infra capability "${step.capName}" served by already-booted "${step.addonId}"`);
820
+ continue;
821
+ }
822
+ const addon = byId.get(step.addonId);
823
+ if (!addon)
824
+ continue;
809
825
  const addonId = addon.declaration.id;
810
- resolutions.push({ name: infra.name, required: infra.required, addonId });
811
826
  try {
812
827
  const instance = new addon.addonClass();
813
828
  // Seed ctx.kernel.storage from whatever storage provider is already
@@ -845,7 +860,7 @@ async function bootCoreAddons(broker, config, registry, loadedAddons, loggerFact
845
860
  catch (err) {
846
861
  const msg = (0, types_2.errMsg)(err);
847
862
  console.error(`[Agent] Failed to initialize core addon "${addonId}": ${msg}`);
848
- if (infra.required) {
863
+ if (step.required) {
849
864
  throw new Error(`Required infrastructure addon "${addonId}" failed: ${msg}`, { cause: err });
850
865
  }
851
866
  }
@@ -8258,6 +8258,24 @@ function createCapRouter_streamBroker(getProvider, createRemoteProxy) {
8258
8258
  // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument
8259
8259
  return p.renderPreBufferClip(methodInput);
8260
8260
  }),
8261
+ produceEventMedia: trpc_middleware_js_1.adminProcedure
8262
+ .input(types_103.streamBrokerCapability.methods.produceEventMedia.input.loose())
8263
+ .output(types_103.streamBrokerCapability.methods.produceEventMedia.output)
8264
+ .mutation(async ({ input, ctx }) => {
8265
+ const { nodeId, ...methodInput } = input;
8266
+ const p = resolveProvider('stream-broker', nodeId, () => getProvider(ctx), createRemoteProxy);
8267
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument
8268
+ return p.produceEventMedia(methodInput);
8269
+ }),
8270
+ fetchEventMedia: trpc_middleware_js_1.adminProcedure
8271
+ .input(types_103.streamBrokerCapability.methods.fetchEventMedia.input.loose())
8272
+ .output(types_103.streamBrokerCapability.methods.fetchEventMedia.output)
8273
+ .mutation(async ({ input, ctx }) => {
8274
+ const { nodeId, ...methodInput } = input;
8275
+ const p = resolveProvider('stream-broker', nodeId, () => getProvider(ctx), createRemoteProxy);
8276
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument
8277
+ return p.fetchEventMedia(methodInput);
8278
+ }),
8261
8279
  listAllCameraStreams: trpc_middleware_js_1.protectedProcedure
8262
8280
  .input(zod_1.z.object({ nodeId: zod_1.z.string().optional() }).optional())
8263
8281
  .output(types_103.streamBrokerCapability.methods.listAllCameraStreams.output)
@@ -3081,8 +3081,29 @@ class AddonRegistryService {
3081
3081
  return;
3082
3082
  let catalog;
3083
3083
  try {
3084
- // Cache-bust so a hot-updated bundle is re-read instead of served
3085
- // from Node's ESM module cache.
3084
+ // ── The query string does NOT bust a CommonJS bundle ──────────────
3085
+ //
3086
+ // Every addon here builds to CJS, and Node's ESM loader serves a CJS
3087
+ // module out of the REQUIRE cache, which is keyed by the resolved
3088
+ // filename with the query stripped. So `?t=<now>` below busts nothing
3089
+ // for them: this function re-registered the catalog captured at hub boot
3090
+ // on every restart, forever.
3091
+ //
3092
+ // The consequence was invisible and total: an EXISTING action kept
3093
+ // working, so nothing looked broken, while a NEWLY added one could never
3094
+ // become reachable without restarting the whole hub process — which
3095
+ // quietly falsifies the reason bridge actions exist ("no codegen, no
3096
+ // republish, no train"). Found 2026-08-08, deploying `nc.injectTestEvent`:
3097
+ // the deploy succeeded, the restart logged "custom actions registered",
3098
+ // and the action 404'd.
3099
+ //
3100
+ // Dropping the addon's own modules from the require cache is what
3101
+ // actually re-reads the bundle. Scoped to the addon directory so no other
3102
+ // addon's (or the hub's own) modules are evicted, and best-effort: a
3103
+ // cache that cannot be walked leaves the previous behaviour rather than
3104
+ // failing the registration.
3105
+ purgeRequireCacheUnder(path.dirname(entryPath));
3106
+ // Kept for a genuinely ESM addon entry, where it IS the mechanism.
3086
3107
  const cacheBustedUrl = `${(0, node_url_1.pathToFileURL)(entryPath).href}?t=${Date.now()}`;
3087
3108
  // A plain `await import()` here is downleveled by tsc (the backend builds
3088
3109
  // with `module: CommonJS`) into a `require()`-based shim. `require()` then
@@ -3136,3 +3157,32 @@ class AddonRegistryService {
3136
3157
  }
3137
3158
  }
3138
3159
  exports.AddonRegistryService = AddonRegistryService;
3160
+ /**
3161
+ * Drop every `require`-cached module that lives under `dir`.
3162
+ *
3163
+ * Node's ESM loader serves a CommonJS module from the require cache, keyed by
3164
+ * the resolved filename — the `?t=` query an ESM import uses to force a re-read
3165
+ * is stripped before that lookup and therefore does nothing. Since every addon
3166
+ * bundle here is CJS, evicting the addon's own entries is what makes a
3167
+ * hot-updated bundle actually load.
3168
+ *
3169
+ * Scoped to one directory on purpose: a blanket cache clear would evict the
3170
+ * hub's own modules and every other addon's, turning a catalog refresh into a
3171
+ * process-wide reload. Best-effort — a cache that cannot be walked leaves the
3172
+ * previous (stale) behaviour rather than failing the caller.
3173
+ */
3174
+ function purgeRequireCacheUnder(dir) {
3175
+ try {
3176
+ const cache = require.cache;
3177
+ if (cache === undefined)
3178
+ return;
3179
+ const prefix = dir.endsWith(path.sep) ? dir : `${dir}${path.sep}`;
3180
+ for (const key of Object.keys(cache)) {
3181
+ if (key.startsWith(prefix))
3182
+ delete cache[key];
3183
+ }
3184
+ }
3185
+ catch {
3186
+ // Non-CJS host, or a frozen cache. The import below still runs.
3187
+ }
3188
+ }
package/dist/launcher.js CHANGED
@@ -278,7 +278,30 @@ async function launch() {
278
278
  // @camstack/system is imported DYNAMICALLY here — AFTER the active framework
279
279
  // dir + NODE_PATH are resolved above — so the correct framework copy is what
280
280
  // gets loaded. Never import it at module top.
281
- const { AddonInstaller, bootstrapSchema, detectWorkspacePackagesDir } = await Promise.resolve().then(() => __importStar(require('@camstack/system')));
281
+ const { AddonInstaller, bootstrapSchema, detectWorkspacePackagesDir, quarantineAddonResidue } = await Promise.resolve().then(() => __importStar(require('@camstack/system')));
282
+ // Residue quarantine — FIRST thing that touches the addon root, before the
283
+ // bootstrap seed writes into it and long before any loader scans it.
284
+ //
285
+ // A directory under `addons/@camstack` whose name is not the package it
286
+ // declares is not an archive: the loader, the agent's boot scan and the
287
+ // install manifest all key on the `package.json` inside, so a rename
288
+ // INSTALLS. On 2026-08-07/08 that shipped `@camstack/system 1.2.3` as the
289
+ // reported version on a node running a 1.2.61 closure, and kept a 17-day-old
290
+ // `better_sqlite3.node` mapped into the live process. Moved, never deleted —
291
+ // the cleanup that deleted one of these gutted an agent the same evening.
292
+ //
293
+ // Guarded with a typeof check for the same reason `reconcileManifest` is: a
294
+ // system-only framework update can swap in a build that predates this.
295
+ if (typeof quarantineAddonResidue === 'function') {
296
+ const residue = quarantineAddonResidue(addonsDir, (msg) => console.log(msg));
297
+ if (residue.quarantined.length > 0 || residue.failed.length > 0) {
298
+ console.log(`[launcher] Addon residue — quarantined ${residue.quarantined.length}, ` +
299
+ `failed ${residue.failed.length}`);
300
+ }
301
+ }
302
+ else {
303
+ console.warn('[launcher] quarantineAddonResidue unavailable — skipping residue quarantine');
304
+ }
282
305
  // Install source resolution:
283
306
  // 1. CAMSTACK_BUNDLED_ADDONS_DIR — set by Electron-packaged builds
284
307
  // to <resourcesPath>/addons. Pre-built addons ship with the
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@camstack/server",
3
- "version": "1.2.74",
3
+ "version": "1.2.76",
4
4
  "private": false,
5
5
  "files": [
6
6
  "dist",
@@ -33,19 +33,19 @@
33
33
  ]
34
34
  },
35
35
  "dependencies": {
36
- "@camstack/addon-admin-ui": "1.2.35",
36
+ "@camstack/addon-admin-ui": "1.2.36",
37
37
  "@camstack/addon-agent-ui": "1.2.10",
38
38
  "@camstack/addon-auth": "1.2.11",
39
39
  "@camstack/addon-decoder-nodeav": "1.2.9",
40
40
  "@camstack/addon-notifiers": "1.2.13",
41
- "@camstack/addon-pipeline": "1.2.46",
42
- "@camstack/addon-pipeline-orchestrator": "1.2.29",
43
- "@camstack/addon-post-analysis": "1.2.51",
41
+ "@camstack/addon-pipeline": "1.2.47",
42
+ "@camstack/addon-pipeline-orchestrator": "1.2.30",
43
+ "@camstack/addon-post-analysis": "1.2.52",
44
44
  "@camstack/sdk": "1.2.10",
45
45
  "@camstack/shm-ring": "1.1.9",
46
- "@camstack/system": "1.2.61",
47
- "@camstack/types": "1.2.45",
48
- "@camstack/ui-library": "1.2.33",
46
+ "@camstack/system": "1.2.62",
47
+ "@camstack/types": "1.2.46",
48
+ "@camstack/ui-library": "1.2.34",
49
49
  "@fastify/compress": "^9.0.0",
50
50
  "@fastify/cookie": "^11.0.2",
51
51
  "@fastify/cors": "^11.2.0",