@frumu/tandem-panel 0.4.13 → 0.4.15

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,159 @@
1
+ const DEFAULT_CAPABILITY_CACHE_TTL_MS = 45_000;
2
+
3
+ let _cache = {
4
+ value: null,
5
+ expiresAt: 0,
6
+ };
7
+
8
+ let _lastReported = {
9
+ aca_available: null,
10
+ engine_healthy: null,
11
+ };
12
+
13
+ const _metrics = {
14
+ detect_duration_ms: 0,
15
+ detect_ok: false,
16
+ last_detect_at_ms: 0,
17
+ aca_probe_error_counts: {
18
+ aca_not_configured: 0,
19
+ aca_endpoint_not_found: 0,
20
+ aca_probe_timeout: 0,
21
+ aca_probe_error: 0,
22
+ aca_health_failed_xxx: 0,
23
+ },
24
+ };
25
+
26
+ function logCapabilityTransition(next) {
27
+ const prev = _lastReported;
28
+ const ts = new Date().toISOString();
29
+ if (prev.aca_available !== next.aca_integration) {
30
+ console.log(`[Capabilities] ${ts} ACA integration: ${prev.aca_available ?? "unknown"} → ${next.aca_integration} (reason: ${next.aca_reason || "n/a"})`);
31
+ }
32
+ if (prev.engine_healthy !== next.engine_healthy) {
33
+ console.log(`[Capabilities] ${ts} Engine healthy: ${prev.engine_healthy ?? "unknown"} → ${next.engine_healthy}`);
34
+ }
35
+ _lastReported = { aca_available: next.aca_integration, engine_healthy: next.engine_healthy };
36
+ }
37
+
38
+ function incrementProbeError(reason) {
39
+ const bucket = reason in _metrics.aca_probe_error_counts
40
+ ? reason
41
+ : reason.match(/^aca_health_failed_\d+$/) ? "aca_health_failed_xxx" : null;
42
+ if (bucket) {
43
+ _metrics.aca_probe_error_counts[bucket] += 1;
44
+ }
45
+ }
46
+
47
+ export function createCapabilitiesHandler(deps) {
48
+ const {
49
+ PROBE_TIMEOUT_MS = 5_000,
50
+ ACA_BASE_URL,
51
+ ACA_HEALTH_PATH = "/health",
52
+ engineHealth,
53
+ cacheTtlMs = DEFAULT_CAPABILITY_CACHE_TTL_MS,
54
+ } = deps;
55
+
56
+ async function probeAca() {
57
+ const base = String(ACA_BASE_URL || "").trim();
58
+ if (!base) {
59
+ incrementProbeError("aca_not_configured");
60
+ return { available: false, reason: "aca_not_configured" };
61
+ }
62
+ const target = `${base.replace(/\/+$/, "")}${ACA_HEALTH_PATH}`;
63
+ const controller = new AbortController();
64
+ const timer = setTimeout(() => controller.abort(), PROBE_TIMEOUT_MS);
65
+ try {
66
+ const res = await fetch(target, {
67
+ method: "GET",
68
+ signal: controller.signal,
69
+ headers: { Accept: "application/json" },
70
+ });
71
+ clearTimeout(timer);
72
+ if (res.ok) return { available: true, reason: "" };
73
+ if (res.status === 404 || res.status === 405) {
74
+ incrementProbeError("aca_endpoint_not_found");
75
+ return { available: false, reason: "aca_endpoint_not_found" };
76
+ }
77
+ incrementProbeError(`aca_health_failed_${res.status}`);
78
+ return { available: false, reason: `aca_health_failed_${res.status}` };
79
+ } catch (err) {
80
+ clearTimeout(timer);
81
+ const msg = String(err?.message || "");
82
+ if (msg.includes("abort")) {
83
+ incrementProbeError("aca_probe_timeout");
84
+ return { available: false, reason: "aca_probe_timeout" };
85
+ }
86
+ incrementProbeError("aca_probe_error");
87
+ return { available: false, reason: "aca_probe_error" };
88
+ }
89
+ }
90
+
91
+ async function probeEngineFeatures(engineOk) {
92
+ if (!engineOk) {
93
+ return { coding_workflows: false, missions: false, agent_teams: false, coder: false };
94
+ }
95
+ return {
96
+ coding_workflows: true,
97
+ missions: true,
98
+ agent_teams: true,
99
+ coder: true,
100
+ };
101
+ }
102
+
103
+ return async function handleCapabilities(req, res) {
104
+ const now = Date.now();
105
+ if (_cache.value && now < _cache.expiresAt) {
106
+ deps.sendJson(res, 200, _cache.value);
107
+ return;
108
+ }
109
+
110
+ const t0 = Date.now();
111
+ const health = await engineHealth().catch(() => null);
112
+ const engineOk = !!(health?.engine?.ready || health?.engine?.healthy);
113
+ const aca = await probeAca();
114
+ const features = await probeEngineFeatures(engineOk);
115
+ const durationMs = Date.now() - t0;
116
+
117
+ _metrics.detect_duration_ms = durationMs;
118
+ _metrics.detect_ok = true;
119
+ _metrics.last_detect_at_ms = now;
120
+
121
+ const result = {
122
+ aca_integration: aca.available,
123
+ aca_reason: aca.reason,
124
+ coding_workflows: features.coding_workflows,
125
+ missions: features.missions,
126
+ agent_teams: features.agent_teams,
127
+ coder: features.coder,
128
+ engine_healthy: engineOk,
129
+ cached_at_ms: now,
130
+ _internal: {
131
+ capability_detect_duration_ms: durationMs,
132
+ },
133
+ };
134
+
135
+ logCapabilityTransition(result);
136
+
137
+ _cache.value = result;
138
+ _cache.expiresAt = now + cacheTtlMs;
139
+
140
+ deps.sendJson(res, 200, result);
141
+ };
142
+ }
143
+
144
+ export function getCapabilitiesMetrics() {
145
+ return {
146
+ ..._metrics,
147
+ aca_probe_error_counts: { ..._metrics.aca_probe_error_counts },
148
+ };
149
+ }
150
+
151
+ export function resetCapabilitiesCache() {
152
+ _cache.value = null;
153
+ _cache.expiresAt = 0;
154
+ }
155
+
156
+ export function resetCapabilitiesState() {
157
+ _lastReported.aca_available = null;
158
+ _lastReported.engine_healthy = null;
159
+ }
@@ -2,6 +2,17 @@ import { readFile, readdir, stat } from "fs/promises";
2
2
  import { resolve } from "path";
3
3
  import { deriveRunBudget, inferStatusFromEvents, mapOrchestratorPath } from "../services/orchestratorService.js";
4
4
 
5
+ const _metrics = {
6
+ streams_active: 0,
7
+ streams_total: 0,
8
+ events_received: 0,
9
+ stream_errors: 0,
10
+ };
11
+
12
+ export function getOrchestratorMetrics() {
13
+ return { ..._metrics };
14
+ }
15
+
5
16
  export function createSwarmApiHandler(deps) {
6
17
  const {
7
18
  PORTAL_PORT,
@@ -852,20 +863,25 @@ export function createSwarmApiHandler(deps) {
852
863
  },
853
864
  });
854
865
  if (upstream.ok && upstream.body) {
866
+ _metrics.streams_active += 1;
867
+ _metrics.streams_total += 1;
855
868
  res.writeHead(200, {
856
869
  "content-type": "text/event-stream",
857
870
  "cache-control": "no-cache",
858
871
  connection: "keep-alive",
859
872
  });
860
- req.on("close", () => upstream.body?.cancel?.().catch?.(() => null));
873
+ req.on("close", () => { _metrics.streams_active = Math.max(0, _metrics.streams_active - 1); });
861
874
  for await (const chunk of upstream.body) {
862
875
  if (res.writableEnded || res.destroyed) break;
863
876
  res.write(chunk);
877
+ _metrics.events_received += 1;
864
878
  }
865
879
  if (!res.writableEnded && !res.destroyed) res.end();
880
+ _metrics.streams_active = Math.max(0, _metrics.streams_active - 1);
866
881
  return true;
867
882
  }
868
883
  } catch {
884
+ _metrics.stream_errors += 1;
869
885
  // fall back to legacy single-run poll bridge below
870
886
  }
871
887
  }
@@ -927,6 +943,7 @@ export function createSwarmApiHandler(deps) {
927
943
  );
928
944
  }
929
945
  } catch {
946
+ _metrics.stream_errors += 1;
930
947
  // ignore transient poll failures
931
948
  }
932
949
  };