harnery 0.36.0 → 0.37.0
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/commander.js +4 -0
- package/dist/commands/agents.d.ts.map +1 -1
- package/dist/commands/agents.js +87 -46
- package/dist/commands/artifacts.d.ts.map +1 -1
- package/dist/commands/artifacts.js +26 -1
- package/dist/commands/browse.d.ts.map +1 -1
- package/dist/commands/browse.js +11 -0
- package/dist/commands/diagnostics.d.ts +4 -0
- package/dist/commands/diagnostics.d.ts.map +1 -0
- package/dist/commands/diagnostics.js +229 -0
- package/dist/commands/files.d.ts +12 -0
- package/dist/commands/files.d.ts.map +1 -1
- package/dist/commands/files.js +54 -1
- package/dist/commands/governor.d.ts.map +1 -1
- package/dist/commands/governor.js +4 -0
- package/dist/commands/ledger-v3.d.ts.map +1 -1
- package/dist/commands/ledger-v3.js +71 -2
- package/dist/commands/qa-run.d.ts +4 -0
- package/dist/commands/qa-run.d.ts.map +1 -0
- package/dist/commands/qa-run.js +141 -0
- package/dist/commands/resources.d.ts +4 -0
- package/dist/commands/resources.d.ts.map +1 -0
- package/dist/commands/resources.js +29 -0
- package/dist/commands/supervisor.d.ts +4 -0
- package/dist/commands/supervisor.d.ts.map +1 -0
- package/dist/commands/supervisor.js +101 -0
- package/dist/commands/web.d.ts.map +1 -1
- package/dist/commands/web.js +17 -0
- package/dist/commands/workflow.d.ts.map +1 -1
- package/dist/commands/workflow.js +4 -0
- package/dist/core/agents/cli.js +7 -3
- package/dist/core/agents/health.d.ts +17 -0
- package/dist/core/agents/health.d.ts.map +1 -0
- package/dist/core/agents/health.js +109 -0
- package/dist/core/agents/mailbox.d.ts +92 -0
- package/dist/core/agents/mailbox.d.ts.map +1 -0
- package/dist/core/agents/mailbox.js +325 -0
- package/dist/core/agents/reconcile-coordination-v3.d.ts +44 -0
- package/dist/core/agents/reconcile-coordination-v3.d.ts.map +1 -0
- package/dist/core/agents/reconcile-coordination-v3.js +58 -0
- package/dist/core/agents/render/prompt-context.d.ts +5 -0
- package/dist/core/agents/render/prompt-context.d.ts.map +1 -1
- package/dist/core/agents/render/prompt-context.js +34 -13
- package/dist/core/agents/render/session-context.d.ts +8 -0
- package/dist/core/agents/render/session-context.d.ts.map +1 -1
- package/dist/core/agents/render/session-context.js +34 -3
- package/dist/core/agents/rules/commit-conflict.d.ts.map +1 -1
- package/dist/core/agents/rules/commit-conflict.js +50 -1
- package/dist/core/agents/rules/stop-hook.d.ts.map +1 -1
- package/dist/core/agents/rules/stop-hook.js +6 -10
- package/dist/core/agents/session-finalizer-v3.js +4 -1
- package/dist/core/artifacts/index.d.ts +26 -1
- package/dist/core/artifacts/index.d.ts.map +1 -1
- package/dist/core/artifacts/index.js +146 -6
- package/dist/core/config.d.ts +12 -0
- package/dist/core/config.d.ts.map +1 -1
- package/dist/core/config.js +43 -0
- package/dist/core/diagnostics/advice.d.ts +9 -0
- package/dist/core/diagnostics/advice.d.ts.map +1 -0
- package/dist/core/diagnostics/advice.js +112 -0
- package/dist/core/diagnostics/bundle.d.ts +35 -0
- package/dist/core/diagnostics/bundle.d.ts.map +1 -0
- package/dist/core/diagnostics/bundle.js +874 -0
- package/dist/core/diagnostics/comparison.d.ts +5 -0
- package/dist/core/diagnostics/comparison.d.ts.map +1 -0
- package/dist/core/diagnostics/comparison.js +444 -0
- package/dist/core/diagnostics/contract.d.ts +245 -0
- package/dist/core/diagnostics/contract.d.ts.map +1 -0
- package/dist/core/diagnostics/contract.js +18 -0
- package/dist/core/diagnostics/identity.d.ts +3 -0
- package/dist/core/diagnostics/identity.d.ts.map +1 -0
- package/dist/core/diagnostics/identity.js +10 -0
- package/dist/core/diagnostics/index.d.ts +8 -0
- package/dist/core/diagnostics/index.d.ts.map +1 -0
- package/dist/core/diagnostics/index.js +7 -0
- package/dist/core/diagnostics/replay.d.ts +8 -0
- package/dist/core/diagnostics/replay.d.ts.map +1 -0
- package/dist/core/diagnostics/replay.js +213 -0
- package/dist/core/diagnostics/sanitize.d.ts +9 -0
- package/dist/core/diagnostics/sanitize.d.ts.map +1 -0
- package/dist/core/diagnostics/sanitize.js +84 -0
- package/dist/core/events/legacy-storage/compression.d.ts +13 -0
- package/dist/core/events/legacy-storage/compression.d.ts.map +1 -0
- package/dist/core/events/legacy-storage/compression.js +107 -0
- package/dist/core/events/legacy-storage/index.d.ts +1 -0
- package/dist/core/events/legacy-storage/index.d.ts.map +1 -1
- package/dist/core/events/legacy-storage/index.js +1 -0
- package/dist/core/events/v3/archive-retention.d.ts +33 -0
- package/dist/core/events/v3/archive-retention.d.ts.map +1 -0
- package/dist/core/events/v3/archive-retention.js +195 -0
- package/dist/core/events/v3/capabilities.d.ts +1 -1
- package/dist/core/events/v3/capabilities.d.ts.map +1 -1
- package/dist/core/events/v3/capabilities.js +1 -0
- package/dist/core/events/v3/index.d.ts +1 -0
- package/dist/core/events/v3/index.d.ts.map +1 -1
- package/dist/core/events/v3/index.js +1 -0
- package/dist/core/events/v3/live-routing.d.ts.map +1 -1
- package/dist/core/events/v3/live-routing.js +1 -0
- package/dist/core/events/v3/producers/hook-base.d.ts +1 -1
- package/dist/core/events/v3/producers/hook-base.d.ts.map +1 -1
- package/dist/core/events/v3/producers/hook-base.js +13 -2
- package/dist/core/events/v3/producers/intake.d.ts.map +1 -1
- package/dist/core/events/v3/producers/intake.js +1 -0
- package/dist/core/events/v3/producers/recorder.d.ts +16 -0
- package/dist/core/events/v3/producers/recorder.d.ts.map +1 -1
- package/dist/core/events/v3/producers/recorder.js +88 -1
- package/dist/core/hooks/adapter/output.d.ts +8 -0
- package/dist/core/hooks/adapter/output.d.ts.map +1 -1
- package/dist/core/hooks/adapter/output.js +11 -1
- package/dist/core/hooks/cli.js +183 -192
- package/dist/core/hooks/health.d.ts +59 -0
- package/dist/core/hooks/health.d.ts.map +1 -0
- package/dist/core/hooks/health.js +84 -0
- package/dist/core/resources/contract.d.ts +90 -0
- package/dist/core/resources/contract.d.ts.map +1 -0
- package/dist/core/resources/contract.js +2 -0
- package/dist/core/resources/index.d.ts +6 -0
- package/dist/core/resources/index.d.ts.map +1 -0
- package/dist/core/resources/index.js +5 -0
- package/dist/core/resources/sampler.d.ts +27 -0
- package/dist/core/resources/sampler.d.ts.map +1 -0
- package/dist/core/resources/sampler.js +357 -0
- package/dist/core/resources/service-status.d.ts +3 -0
- package/dist/core/resources/service-status.d.ts.map +1 -0
- package/dist/core/resources/service-status.js +48 -0
- package/dist/core/resources/service.d.ts +26 -0
- package/dist/core/resources/service.d.ts.map +1 -0
- package/dist/core/resources/service.js +292 -0
- package/dist/core/resources/storage.d.ts +13 -0
- package/dist/core/resources/storage.d.ts.map +1 -0
- package/dist/core/resources/storage.js +30 -0
- package/dist/core/storage/atomic-json.d.ts +3 -0
- package/dist/core/storage/atomic-json.d.ts.map +1 -0
- package/dist/core/storage/atomic-json.js +15 -0
- package/dist/core/storage/builtins.d.ts.map +1 -1
- package/dist/core/storage/builtins.js +45 -6
- package/dist/core/storage/logger.d.ts +2 -0
- package/dist/core/storage/logger.d.ts.map +1 -1
- package/dist/core/storage/logger.js +2 -0
- package/dist/core/storage/query.d.ts +14 -0
- package/dist/core/storage/query.d.ts.map +1 -1
- package/dist/core/storage/query.js +51 -0
- package/dist/core/supervisor/activity.d.ts +4 -0
- package/dist/core/supervisor/activity.d.ts.map +1 -0
- package/dist/core/supervisor/activity.js +141 -0
- package/dist/core/supervisor/contract.d.ts +268 -0
- package/dist/core/supervisor/contract.d.ts.map +1 -0
- package/dist/core/supervisor/contract.js +35 -0
- package/dist/core/supervisor/explanations.d.ts +4 -0
- package/dist/core/supervisor/explanations.d.ts.map +1 -0
- package/dist/core/supervisor/explanations.js +71 -0
- package/dist/core/supervisor/findings.d.ts +20 -0
- package/dist/core/supervisor/findings.d.ts.map +1 -0
- package/dist/core/supervisor/findings.js +399 -0
- package/dist/core/supervisor/history.d.ts +9 -0
- package/dist/core/supervisor/history.d.ts.map +1 -0
- package/dist/core/supervisor/history.js +46 -0
- package/dist/core/supervisor/hook-health-alerts.d.ts +14 -0
- package/dist/core/supervisor/hook-health-alerts.d.ts.map +1 -0
- package/dist/core/supervisor/hook-health-alerts.js +70 -0
- package/dist/core/supervisor/hook-health-storage.d.ts +5 -0
- package/dist/core/supervisor/hook-health-storage.d.ts.map +1 -0
- package/dist/core/supervisor/hook-health-storage.js +24 -0
- package/dist/core/supervisor/hook-health.d.ts +72 -0
- package/dist/core/supervisor/hook-health.d.ts.map +1 -0
- package/dist/core/supervisor/hook-health.js +203 -0
- package/dist/core/supervisor/hooks.d.ts +10 -0
- package/dist/core/supervisor/hooks.d.ts.map +1 -0
- package/dist/core/supervisor/hooks.js +28 -0
- package/dist/core/supervisor/index.d.ts +15 -0
- package/dist/core/supervisor/index.d.ts.map +1 -0
- package/dist/core/supervisor/index.js +14 -0
- package/dist/core/supervisor/log-feed.d.ts +9 -0
- package/dist/core/supervisor/log-feed.d.ts.map +1 -0
- package/dist/core/supervisor/log-feed.js +103 -0
- package/dist/core/supervisor/service.d.ts +29 -0
- package/dist/core/supervisor/service.d.ts.map +1 -0
- package/dist/core/supervisor/service.js +450 -0
- package/dist/core/supervisor/services.d.ts +11 -0
- package/dist/core/supervisor/services.d.ts.map +1 -0
- package/dist/core/supervisor/services.js +155 -0
- package/dist/core/supervisor/status.d.ts +3 -0
- package/dist/core/supervisor/status.d.ts.map +1 -0
- package/dist/core/supervisor/status.js +48 -0
- package/dist/core/supervisor/storage.d.ts +30 -0
- package/dist/core/supervisor/storage.d.ts.map +1 -0
- package/dist/core/supervisor/storage.js +73 -0
- package/dist/core/supervisor/timeline.d.ts +3 -0
- package/dist/core/supervisor/timeline.d.ts.map +1 -0
- package/dist/core/supervisor/timeline.js +87 -0
- package/dist/core/workflow/admission.d.ts +23 -0
- package/dist/core/workflow/admission.d.ts.map +1 -0
- package/dist/core/workflow/admission.js +136 -0
- package/dist/core/workflow/engine.d.ts.map +1 -1
- package/dist/core/workflow/engine.js +77 -2
- package/dist/core/workflow/index.d.ts +3 -2
- package/dist/core/workflow/index.d.ts.map +1 -1
- package/dist/core/workflow/index.js +2 -1
- package/dist/core/workflow/proof.d.ts +2 -1
- package/dist/core/workflow/proof.d.ts.map +1 -1
- package/dist/core/workflow/proof.js +39 -1
- package/dist/core/workflow/run-state.d.ts +3 -2
- package/dist/core/workflow/run-state.d.ts.map +1 -1
- package/dist/core/workflow/run-state.js +8 -1
- package/dist/core/workflow/types.d.ts +33 -1
- package/dist/core/workflow/types.d.ts.map +1 -1
- package/dist/core/workflow/types.js +2 -1
- package/dist/lib/browser/client.d.ts +5 -0
- package/dist/lib/browser/client.d.ts.map +1 -1
- package/dist/lib/browser/client.js +1 -0
- package/dist/lib/browser/index.d.ts +2 -0
- package/dist/lib/browser/index.d.ts.map +1 -1
- package/dist/lib/browser/index.js +2 -0
- package/dist/lib/browser/qa-run-contracts.d.ts +173 -0
- package/dist/lib/browser/qa-run-contracts.d.ts.map +1 -0
- package/dist/lib/browser/qa-run-contracts.js +226 -0
- package/dist/lib/browser/qa-run.d.ts +51 -0
- package/dist/lib/browser/qa-run.d.ts.map +1 -0
- package/dist/lib/browser/qa-run.js +684 -0
- package/dist/lib/coord-root-id.d.ts +9 -0
- package/dist/lib/coord-root-id.d.ts.map +1 -0
- package/dist/lib/coord-root-id.js +17 -0
- package/dist/lib/instructions/apply.d.ts +3 -1
- package/dist/lib/instructions/apply.d.ts.map +1 -1
- package/dist/lib/instructions/apply.js +74 -3
- package/dist/lib/instructions/templates.d.ts.map +1 -1
- package/dist/lib/instructions/templates.js +6 -0
- package/package.json +12 -1
- package/src/commander.ts +26 -0
- package/src/commands/agents.ts +102 -49
- package/src/commands/artifacts.ts +40 -2
- package/src/commands/browse.ts +15 -0
- package/src/commands/diagnostics.ts +271 -0
- package/src/commands/files.ts +102 -2
- package/src/commands/governor.ts +8 -0
- package/src/commands/ledger-v3.ts +82 -0
- package/src/commands/qa-run.ts +184 -0
- package/src/commands/resources.ts +38 -0
- package/src/commands/supervisor.ts +141 -0
- package/src/commands/web.ts +19 -0
- package/src/commands/workflow.ts +8 -0
- package/src/core/agents/cli.ts +7 -3
- package/src/core/agents/health.ts +142 -0
- package/src/core/agents/mailbox.ts +373 -0
- package/src/core/agents/reconcile-coordination-v3.ts +77 -0
- package/src/core/agents/render/prompt-context.ts +42 -12
- package/src/core/agents/render/session-context.ts +37 -3
- package/src/core/agents/rules/commit-conflict.ts +58 -1
- package/src/core/agents/rules/stop-hook.ts +6 -12
- package/src/core/agents/session-finalizer-v3.ts +4 -1
- package/src/core/artifacts/index.ts +196 -10
- package/src/core/config.ts +111 -2
- package/src/core/diagnostics/advice.ts +147 -0
- package/src/core/diagnostics/bundle.ts +1142 -0
- package/src/core/diagnostics/comparison.ts +565 -0
- package/src/core/diagnostics/contract.ts +311 -0
- package/src/core/diagnostics/identity.ts +11 -0
- package/src/core/diagnostics/index.ts +7 -0
- package/src/core/diagnostics/replay.ts +314 -0
- package/src/core/diagnostics/sanitize.ts +97 -0
- package/src/core/events/legacy-storage/compression.ts +130 -0
- package/src/core/events/legacy-storage/index.ts +1 -0
- package/src/core/events/v3/archive-retention.ts +251 -0
- package/src/core/events/v3/capabilities.ts +2 -0
- package/src/core/events/v3/index.ts +9 -0
- package/src/core/events/v3/live-routing.ts +1 -0
- package/src/core/events/v3/producers/hook-base.ts +14 -2
- package/src/core/events/v3/producers/intake.ts +1 -0
- package/src/core/events/v3/producers/recorder.ts +110 -1
- package/src/core/hooks/adapter/output.ts +12 -1
- package/src/core/hooks/cli.ts +192 -215
- package/src/core/hooks/health.ts +146 -0
- package/src/core/resources/contract.ts +98 -0
- package/src/core/resources/index.ts +5 -0
- package/src/core/resources/sampler.ts +458 -0
- package/src/core/resources/service-status.ts +62 -0
- package/src/core/resources/service.ts +337 -0
- package/src/core/resources/storage.ts +44 -0
- package/src/core/storage/atomic-json.ts +15 -0
- package/src/core/storage/builtins.ts +62 -6
- package/src/core/storage/logger.ts +2 -0
- package/src/core/storage/query.ts +58 -0
- package/src/core/supervisor/activity.ts +193 -0
- package/src/core/supervisor/contract.ts +312 -0
- package/src/core/supervisor/explanations.ts +104 -0
- package/src/core/supervisor/findings.ts +788 -0
- package/src/core/supervisor/history.ts +61 -0
- package/src/core/supervisor/hook-health-alerts.ts +108 -0
- package/src/core/supervisor/hook-health-storage.ts +25 -0
- package/src/core/supervisor/hook-health.ts +301 -0
- package/src/core/supervisor/hooks.ts +35 -0
- package/src/core/supervisor/index.ts +14 -0
- package/src/core/supervisor/log-feed.ts +131 -0
- package/src/core/supervisor/service.ts +511 -0
- package/src/core/supervisor/services.ts +203 -0
- package/src/core/supervisor/status.ts +59 -0
- package/src/core/supervisor/storage.ts +126 -0
- package/src/core/supervisor/timeline.ts +133 -0
- package/src/core/workflow/admission.ts +215 -0
- package/src/core/workflow/engine.ts +93 -1
- package/src/core/workflow/index.ts +10 -0
- package/src/core/workflow/proof.ts +54 -1
- package/src/core/workflow/run-state.ts +13 -1
- package/src/core/workflow/types.ts +39 -1
- package/src/lib/browser/client.ts +6 -0
- package/src/lib/browser/index.ts +29 -0
- package/src/lib/browser/qa-run-contracts.ts +380 -0
- package/src/lib/browser/qa-run.ts +784 -0
- package/src/lib/coord-root-id.ts +23 -0
- package/src/lib/instructions/apply.ts +81 -2
- package/src/lib/instructions/templates.ts +6 -0
|
@@ -0,0 +1,788 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import type { CoordinationHealthSnapshot } from "../agents/health.ts";
|
|
3
|
+
import type {
|
|
4
|
+
ResourceProcessGroup,
|
|
5
|
+
ResourceProcessSample,
|
|
6
|
+
ResourceSnapshot,
|
|
7
|
+
} from "../resources/contract.ts";
|
|
8
|
+
import {
|
|
9
|
+
type ObservedHookHealth,
|
|
10
|
+
type ObservedServiceHealth,
|
|
11
|
+
SUPERVISOR_DIAGNOSTIC_LIMITS,
|
|
12
|
+
SUPERVISOR_FINDING_POLICY,
|
|
13
|
+
SUPERVISOR_FINDING_SCHEMA_VERSION,
|
|
14
|
+
type SupervisorActivitySnapshot,
|
|
15
|
+
type SupervisorCapability,
|
|
16
|
+
type SupervisorFinding,
|
|
17
|
+
type SupervisorFindingAttribution,
|
|
18
|
+
type SupervisorFindingEvidence,
|
|
19
|
+
type SupervisorFindingSeverity,
|
|
20
|
+
type SupervisorFindings,
|
|
21
|
+
type SupervisorFindingWorkloadContext,
|
|
22
|
+
type SupervisorHistory,
|
|
23
|
+
type SupervisorLogFeed,
|
|
24
|
+
type SupervisorSourceReference,
|
|
25
|
+
} from "./contract.ts";
|
|
26
|
+
import type { SupervisorHookHealth } from "./hook-health.ts";
|
|
27
|
+
import { evaluateHookHealthAlerts } from "./hook-health-alerts.ts";
|
|
28
|
+
|
|
29
|
+
const GIB = 1_024 * 1_024 * 1_024;
|
|
30
|
+
const MIB = 1_024 * 1_024;
|
|
31
|
+
const MACHINE_CPU_THRESHOLD = 90;
|
|
32
|
+
const MACHINE_MEMORY_THRESHOLD = 85;
|
|
33
|
+
const MACHINE_SWAP_THRESHOLD = 75;
|
|
34
|
+
const COLLECTOR_CPU_THRESHOLD_MS = 50;
|
|
35
|
+
const PROCESS_MEMORY_THRESHOLD = GIB;
|
|
36
|
+
const GROUP_MEMORY_THRESHOLD = 2 * GIB;
|
|
37
|
+
const OWNER_PROCESS_THRESHOLD = 50;
|
|
38
|
+
const MEMORY_GROWTH_MIN_BYTES = 256 * MIB;
|
|
39
|
+
const MEMORY_GROWTH_RATIO = 1.3;
|
|
40
|
+
const MEMORY_GROWTH_WINDOW_MS = 5 * 60_000;
|
|
41
|
+
|
|
42
|
+
interface Candidate {
|
|
43
|
+
source_kind: string;
|
|
44
|
+
finding_kind: string;
|
|
45
|
+
severity: SupervisorFindingSeverity;
|
|
46
|
+
scope_kind: string;
|
|
47
|
+
scope_id: string;
|
|
48
|
+
summary: string;
|
|
49
|
+
primary_source: SupervisorSourceReference;
|
|
50
|
+
evidence: SupervisorFindingEvidence[];
|
|
51
|
+
capabilities: SupervisorCapability[];
|
|
52
|
+
attribution?: SupervisorFindingAttribution;
|
|
53
|
+
workload_context?: SupervisorFindingWorkloadContext;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface SupervisorFindingEvaluationInput {
|
|
57
|
+
previous?: SupervisorFindings;
|
|
58
|
+
resource: ResourceSnapshot;
|
|
59
|
+
services: readonly ObservedServiceHealth[];
|
|
60
|
+
hooks: readonly ObservedHookHealth[];
|
|
61
|
+
history: SupervisorHistory;
|
|
62
|
+
logFeed: SupervisorLogFeed;
|
|
63
|
+
hookHealth?: SupervisorHookHealth;
|
|
64
|
+
coordination?: CoordinationHealthSnapshot;
|
|
65
|
+
activity?: SupervisorActivitySnapshot;
|
|
66
|
+
now?: Date;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Pure deterministic evaluator used by the live supervisor and replay. */
|
|
70
|
+
export function evaluateSupervisorFindings(
|
|
71
|
+
input: Omit<SupervisorFindingEvaluationInput, "previous">,
|
|
72
|
+
): readonly SupervisorFinding[] {
|
|
73
|
+
return updateSupervisorFindings(input).active;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function updateSupervisorFindings(
|
|
77
|
+
input: SupervisorFindingEvaluationInput,
|
|
78
|
+
): SupervisorFindings {
|
|
79
|
+
const observedAt = (input.now ?? new Date(input.resource.sampled_at)).toISOString();
|
|
80
|
+
const previous = validFindings(input.previous)
|
|
81
|
+
? input.previous
|
|
82
|
+
: {
|
|
83
|
+
schema_version: SUPERVISOR_FINDING_SCHEMA_VERSION,
|
|
84
|
+
max_findings: SUPERVISOR_DIAGNOSTIC_LIMITS.max_findings,
|
|
85
|
+
active: [],
|
|
86
|
+
transitions: [],
|
|
87
|
+
};
|
|
88
|
+
const candidates = evaluateCandidates(input)
|
|
89
|
+
.sort((left, right) => severityRank(right.severity) - severityRank(left.severity))
|
|
90
|
+
.slice(0, SUPERVISOR_DIAGNOSTIC_LIMITS.max_active_findings);
|
|
91
|
+
const priorActive = new Map(previous.active.map((entry) => [entry.fingerprint, entry]));
|
|
92
|
+
const current = new Set<string>();
|
|
93
|
+
const active: SupervisorFinding[] = [];
|
|
94
|
+
const transitions = new Map(previous.transitions.map((entry) => [entry.id, entry]));
|
|
95
|
+
|
|
96
|
+
for (const candidate of candidates) {
|
|
97
|
+
const fingerprint = findingFingerprint(candidate);
|
|
98
|
+
current.add(fingerprint);
|
|
99
|
+
const prior = priorActive.get(fingerprint) ?? recentResolved(previous, fingerprint, observedAt);
|
|
100
|
+
const openedAt = prior?.opened_at ?? candidate.primary_source.observed_at;
|
|
101
|
+
const occurrenceCount = prior
|
|
102
|
+
? prior.state === "resolved"
|
|
103
|
+
? prior.occurrence_count + 1
|
|
104
|
+
: prior.occurrence_count
|
|
105
|
+
: 1;
|
|
106
|
+
const peak = peakEvidence(prior, candidate.evidence, observedAt);
|
|
107
|
+
const finding: SupervisorFinding = {
|
|
108
|
+
schema_version: SUPERVISOR_FINDING_SCHEMA_VERSION,
|
|
109
|
+
id: deterministicId("find", `${fingerprint}\u0000${openedAt}`),
|
|
110
|
+
fingerprint,
|
|
111
|
+
source_kind: candidate.source_kind,
|
|
112
|
+
finding_kind: candidate.finding_kind,
|
|
113
|
+
severity: candidate.severity,
|
|
114
|
+
state: "opened",
|
|
115
|
+
scope_kind: candidate.scope_kind,
|
|
116
|
+
scope_id: candidate.scope_id,
|
|
117
|
+
summary: candidate.summary,
|
|
118
|
+
opened_at: openedAt,
|
|
119
|
+
observed_at: observedAt,
|
|
120
|
+
occurrence_count: occurrenceCount,
|
|
121
|
+
...peak,
|
|
122
|
+
...(candidate.attribution ? { attribution: candidate.attribution } : {}),
|
|
123
|
+
...(candidate.workload_context ? { workload_context: candidate.workload_context } : {}),
|
|
124
|
+
primary_source: candidate.primary_source,
|
|
125
|
+
evidence: candidate.evidence.slice(0, SUPERVISOR_DIAGNOSTIC_LIMITS.max_evidence_per_finding),
|
|
126
|
+
capabilities: boundCapabilities(candidate.capabilities),
|
|
127
|
+
};
|
|
128
|
+
active.push(finding);
|
|
129
|
+
transitions.set(finding.id, finding);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
for (const prior of priorActive.values()) {
|
|
133
|
+
if (current.has(prior.fingerprint)) continue;
|
|
134
|
+
transitions.set(prior.id, {
|
|
135
|
+
...prior,
|
|
136
|
+
state: "resolved",
|
|
137
|
+
observed_at: observedAt,
|
|
138
|
+
resolved_at: observedAt,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return {
|
|
143
|
+
schema_version: SUPERVISOR_FINDING_SCHEMA_VERSION,
|
|
144
|
+
max_findings: SUPERVISOR_DIAGNOSTIC_LIMITS.max_findings,
|
|
145
|
+
active,
|
|
146
|
+
transitions: [...transitions.values()]
|
|
147
|
+
.sort((left, right) => left.observed_at.localeCompare(right.observed_at))
|
|
148
|
+
.slice(-SUPERVISOR_DIAGNOSTIC_LIMITS.max_findings),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function evaluateCandidates(input: {
|
|
153
|
+
resource: ResourceSnapshot;
|
|
154
|
+
services: readonly ObservedServiceHealth[];
|
|
155
|
+
hooks: readonly ObservedHookHealth[];
|
|
156
|
+
history: SupervisorHistory;
|
|
157
|
+
logFeed: SupervisorLogFeed;
|
|
158
|
+
hookHealth?: SupervisorHookHealth;
|
|
159
|
+
coordination?: CoordinationHealthSnapshot;
|
|
160
|
+
activity?: SupervisorActivitySnapshot;
|
|
161
|
+
}): Candidate[] {
|
|
162
|
+
const out: Candidate[] = [];
|
|
163
|
+
const resourceCapability: SupervisorCapability = {
|
|
164
|
+
source_kind: "resource.snapshot",
|
|
165
|
+
state: input.resource.support.state,
|
|
166
|
+
...(input.resource.support.reason
|
|
167
|
+
? { reason_code: "sampler-degraded", detail: input.resource.support.reason }
|
|
168
|
+
: {}),
|
|
169
|
+
};
|
|
170
|
+
const source = resourceSource(input.resource, resourceCapability.state);
|
|
171
|
+
const addMachine = (
|
|
172
|
+
findingKind: string,
|
|
173
|
+
severity: SupervisorFindingSeverity,
|
|
174
|
+
summary: string,
|
|
175
|
+
value: number,
|
|
176
|
+
unit: SupervisorFindingEvidence["unit"],
|
|
177
|
+
) =>
|
|
178
|
+
out.push(
|
|
179
|
+
candidate(findingKind, severity, "machine", "local", summary, source, value, unit, [
|
|
180
|
+
resourceCapability,
|
|
181
|
+
]),
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
const machine = input.resource.machine;
|
|
185
|
+
if (resourceCapability.state !== "supported") {
|
|
186
|
+
out.push(capabilityCandidate(resourceCapability, source));
|
|
187
|
+
}
|
|
188
|
+
if ((machine.cpu_percent ?? 0) >= MACHINE_CPU_THRESHOLD)
|
|
189
|
+
addMachine(
|
|
190
|
+
"machine.cpu-pressure",
|
|
191
|
+
"warning",
|
|
192
|
+
`Machine CPU is ${machine.cpu_percent}%.`,
|
|
193
|
+
machine.cpu_percent!,
|
|
194
|
+
"percent",
|
|
195
|
+
);
|
|
196
|
+
if ((machine.memory_percent ?? 0) >= MACHINE_MEMORY_THRESHOLD)
|
|
197
|
+
addMachine(
|
|
198
|
+
"machine.memory-pressure",
|
|
199
|
+
"critical",
|
|
200
|
+
`Machine memory is ${machine.memory_percent}%.`,
|
|
201
|
+
machine.memory_percent!,
|
|
202
|
+
"percent",
|
|
203
|
+
);
|
|
204
|
+
const swapPercent =
|
|
205
|
+
machine.swap_total_bytes && machine.swap_used_bytes !== null
|
|
206
|
+
? round1((machine.swap_used_bytes / machine.swap_total_bytes) * 100)
|
|
207
|
+
: null;
|
|
208
|
+
if ((swapPercent ?? 0) >= MACHINE_SWAP_THRESHOLD)
|
|
209
|
+
addMachine(
|
|
210
|
+
"machine.swap-pressure",
|
|
211
|
+
"warning",
|
|
212
|
+
`Machine swap is ${swapPercent}% used.`,
|
|
213
|
+
swapPercent!,
|
|
214
|
+
"percent",
|
|
215
|
+
);
|
|
216
|
+
if (input.resource.collector_cpu_ms >= COLLECTOR_CPU_THRESHOLD_MS)
|
|
217
|
+
out.push(
|
|
218
|
+
candidate(
|
|
219
|
+
"supervisor.collector-overhead",
|
|
220
|
+
"warning",
|
|
221
|
+
"service",
|
|
222
|
+
"supervisor",
|
|
223
|
+
`Supervisor resource collection used ${input.resource.collector_cpu_ms} ms of CPU.`,
|
|
224
|
+
source,
|
|
225
|
+
input.resource.collector_cpu_ms,
|
|
226
|
+
"milliseconds",
|
|
227
|
+
[resourceCapability],
|
|
228
|
+
),
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
for (const service of input.services.filter((entry) => entry.state === "stale").slice(0, 5)) {
|
|
232
|
+
const serviceSource = stableSource(
|
|
233
|
+
"service.health",
|
|
234
|
+
service.id,
|
|
235
|
+
service.heartbeat_at ?? input.resource.sampled_at,
|
|
236
|
+
"supported",
|
|
237
|
+
);
|
|
238
|
+
out.push(
|
|
239
|
+
candidate(
|
|
240
|
+
"service.stale",
|
|
241
|
+
"warning",
|
|
242
|
+
"service",
|
|
243
|
+
service.id,
|
|
244
|
+
serviceStaleSummary(service),
|
|
245
|
+
serviceSource,
|
|
246
|
+
undefined,
|
|
247
|
+
undefined,
|
|
248
|
+
[{ source_kind: "service.health", state: "supported" }],
|
|
249
|
+
),
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
for (const hook of input.hooks.filter((entry) => entry.long_running).slice(0, 5)) {
|
|
253
|
+
const hookSource = stableSource(
|
|
254
|
+
"hook.process",
|
|
255
|
+
`${hook.owner_id}:${hook.pid}`,
|
|
256
|
+
input.resource.sampled_at,
|
|
257
|
+
"supported",
|
|
258
|
+
);
|
|
259
|
+
out.push(
|
|
260
|
+
candidate(
|
|
261
|
+
"hook.long-running",
|
|
262
|
+
"warning",
|
|
263
|
+
"hook",
|
|
264
|
+
`${hook.owner_id}:${hook.pid}`,
|
|
265
|
+
`Hook process ${hook.pid} has run for ${hook.age_seconds} seconds.`,
|
|
266
|
+
hookSource,
|
|
267
|
+
hook.age_seconds,
|
|
268
|
+
"seconds",
|
|
269
|
+
[{ source_kind: "hook.process", state: "supported" }],
|
|
270
|
+
),
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
for (const process of input.resource.processes
|
|
274
|
+
.filter((entry) => entry.rss_bytes >= PROCESS_MEMORY_THRESHOLD)
|
|
275
|
+
.slice(0, 5))
|
|
276
|
+
out.push(
|
|
277
|
+
candidate(
|
|
278
|
+
"process.memory-pressure",
|
|
279
|
+
"critical",
|
|
280
|
+
"process",
|
|
281
|
+
process.start_id,
|
|
282
|
+
`Process ${process.pid} uses ${process.rss_bytes} resident bytes.`,
|
|
283
|
+
resourceSource(input.resource, resourceCapability.state, process.start_id),
|
|
284
|
+
process.rss_bytes,
|
|
285
|
+
"bytes",
|
|
286
|
+
[resourceCapability],
|
|
287
|
+
processAttribution(process),
|
|
288
|
+
),
|
|
289
|
+
);
|
|
290
|
+
for (const group of input.resource.groups
|
|
291
|
+
.filter((entry) => entry.rss_bytes >= GROUP_MEMORY_THRESHOLD)
|
|
292
|
+
.slice(0, 5))
|
|
293
|
+
out.push(
|
|
294
|
+
candidate(
|
|
295
|
+
"group.memory-pressure",
|
|
296
|
+
"critical",
|
|
297
|
+
group.kind,
|
|
298
|
+
group.id,
|
|
299
|
+
`${groupLabel(group)} uses ${group.rss_bytes} resident bytes.`,
|
|
300
|
+
source,
|
|
301
|
+
group.rss_bytes,
|
|
302
|
+
"bytes",
|
|
303
|
+
[resourceCapability],
|
|
304
|
+
groupAttribution(group),
|
|
305
|
+
),
|
|
306
|
+
);
|
|
307
|
+
for (const group of input.resource.groups
|
|
308
|
+
.filter((entry) => entry.process_count >= OWNER_PROCESS_THRESHOLD)
|
|
309
|
+
.slice(0, 5))
|
|
310
|
+
out.push(
|
|
311
|
+
candidate(
|
|
312
|
+
"group.process-pressure",
|
|
313
|
+
"warning",
|
|
314
|
+
group.kind,
|
|
315
|
+
group.id,
|
|
316
|
+
`${groupLabel(group)} owns ${group.process_count} visible processes.`,
|
|
317
|
+
source,
|
|
318
|
+
group.process_count,
|
|
319
|
+
"processes",
|
|
320
|
+
[resourceCapability],
|
|
321
|
+
groupAttribution(group),
|
|
322
|
+
),
|
|
323
|
+
);
|
|
324
|
+
out.push(
|
|
325
|
+
...memoryGrowthCandidates(input.resource, input.history, resourceCapability, input.activity),
|
|
326
|
+
);
|
|
327
|
+
|
|
328
|
+
if (input.logFeed.unavailable_families > 0) {
|
|
329
|
+
const logCapability: SupervisorCapability = {
|
|
330
|
+
source_kind: "log.feed",
|
|
331
|
+
state: "partial",
|
|
332
|
+
reason_code: "families-unavailable",
|
|
333
|
+
detail: `${input.logFeed.unavailable_families} log families were unavailable.`,
|
|
334
|
+
};
|
|
335
|
+
out.push(
|
|
336
|
+
capabilityCandidate(
|
|
337
|
+
logCapability,
|
|
338
|
+
stableSource(
|
|
339
|
+
"log.feed",
|
|
340
|
+
"supervisor-log-feed",
|
|
341
|
+
input.logFeed.captured_at,
|
|
342
|
+
logCapability.state,
|
|
343
|
+
undefined,
|
|
344
|
+
input.logFeed.schema_version,
|
|
345
|
+
),
|
|
346
|
+
),
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
if (input.hookHealth) {
|
|
351
|
+
const hookCapability: SupervisorCapability = {
|
|
352
|
+
source_kind: input.hookHealth.capability.source_kind,
|
|
353
|
+
state:
|
|
354
|
+
input.hookHealth.capability.state === "unavailable"
|
|
355
|
+
? "error"
|
|
356
|
+
: input.hookHealth.capability.state,
|
|
357
|
+
...(input.hookHealth.capability.reason
|
|
358
|
+
? {
|
|
359
|
+
reason_code: input.hookHealth.capability.reason,
|
|
360
|
+
detail: input.hookHealth.capability.reason,
|
|
361
|
+
}
|
|
362
|
+
: {}),
|
|
363
|
+
};
|
|
364
|
+
for (const alert of evaluateHookHealthAlerts(input.hookHealth)) {
|
|
365
|
+
const alertSource = stableSource(
|
|
366
|
+
"hook.terminal-log",
|
|
367
|
+
alert.source.id,
|
|
368
|
+
alert.source.observed_at,
|
|
369
|
+
hookCapability.state,
|
|
370
|
+
alert.source.id,
|
|
371
|
+
input.hookHealth.schema_version,
|
|
372
|
+
);
|
|
373
|
+
out.push(
|
|
374
|
+
candidate(
|
|
375
|
+
alert.finding_kind,
|
|
376
|
+
alert.severity,
|
|
377
|
+
"hook",
|
|
378
|
+
alert.scope_id,
|
|
379
|
+
alert.summary,
|
|
380
|
+
alertSource,
|
|
381
|
+
alert.observed_value,
|
|
382
|
+
alert.unit,
|
|
383
|
+
[hookCapability],
|
|
384
|
+
alert.owner_id
|
|
385
|
+
? {
|
|
386
|
+
state: "attributed",
|
|
387
|
+
owner_kind: "agent",
|
|
388
|
+
owner_id: alert.owner_id,
|
|
389
|
+
}
|
|
390
|
+
: { state: "unattributed", reason_code: "no-validated-process-anchor" },
|
|
391
|
+
),
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
if (input.coordination) {
|
|
397
|
+
const coordination = input.coordination;
|
|
398
|
+
if (coordination.capability.state !== "supported") {
|
|
399
|
+
out.push(
|
|
400
|
+
capabilityCandidate(
|
|
401
|
+
coordination.capability,
|
|
402
|
+
stableSource(
|
|
403
|
+
coordination.capability.source_kind,
|
|
404
|
+
coordination.capability.reason_code ?? coordination.capability.state,
|
|
405
|
+
coordination.observed_at,
|
|
406
|
+
coordination.capability.state,
|
|
407
|
+
),
|
|
408
|
+
),
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
for (const diagnostic of coordination.diagnostics.slice(0, 5)) {
|
|
412
|
+
const diagnosticSource = stableSource(
|
|
413
|
+
"coordination.v3",
|
|
414
|
+
diagnostic.event_id ?? `${diagnostic.segment_ordinal}:${diagnostic.byte_offset}`,
|
|
415
|
+
coordination.observed_at,
|
|
416
|
+
coordination.capability.state,
|
|
417
|
+
diagnostic.event_id,
|
|
418
|
+
);
|
|
419
|
+
out.push(
|
|
420
|
+
candidate(
|
|
421
|
+
"coordination.ledger-diagnostic",
|
|
422
|
+
"warning",
|
|
423
|
+
"ledger",
|
|
424
|
+
diagnostic.code,
|
|
425
|
+
`Event ledger reported ${diagnostic.code}.`,
|
|
426
|
+
diagnosticSource,
|
|
427
|
+
1,
|
|
428
|
+
"count",
|
|
429
|
+
[coordination.capability],
|
|
430
|
+
),
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
return out;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
function capabilityCandidate(
|
|
438
|
+
capability: SupervisorCapability,
|
|
439
|
+
source: SupervisorSourceReference,
|
|
440
|
+
): Candidate {
|
|
441
|
+
return candidate(
|
|
442
|
+
"source.capability-degraded",
|
|
443
|
+
"info",
|
|
444
|
+
"source",
|
|
445
|
+
capability.source_kind,
|
|
446
|
+
`${capability.source_kind} capability is ${capability.state}${capability.reason_code ? ` (${capability.reason_code})` : ""}.`,
|
|
447
|
+
source,
|
|
448
|
+
undefined,
|
|
449
|
+
undefined,
|
|
450
|
+
[capability],
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
function memoryGrowthCandidates(
|
|
455
|
+
resource: ResourceSnapshot,
|
|
456
|
+
history: SupervisorHistory,
|
|
457
|
+
capability: SupervisorCapability,
|
|
458
|
+
activity?: SupervisorActivitySnapshot,
|
|
459
|
+
): Candidate[] {
|
|
460
|
+
const newestAt = Date.parse(resource.sampled_at);
|
|
461
|
+
const baseline = history.points.find(
|
|
462
|
+
(point) => newestAt - Date.parse(point.sampled_at) <= MEMORY_GROWTH_WINDOW_MS,
|
|
463
|
+
);
|
|
464
|
+
if (!baseline || newestAt - Date.parse(baseline.sampled_at) < 60_000) return [];
|
|
465
|
+
const priorByKey = new Map(baseline.groups.map((group) => [groupKey(group), group]));
|
|
466
|
+
return resource.groups
|
|
467
|
+
.flatMap((group): Candidate[] => {
|
|
468
|
+
const prior = priorByKey.get(groupKey(group));
|
|
469
|
+
if (!prior || prior.rss_bytes <= 0) return [];
|
|
470
|
+
const delta = group.rss_bytes - prior.rss_bytes;
|
|
471
|
+
if (
|
|
472
|
+
delta < MEMORY_GROWTH_MIN_BYTES ||
|
|
473
|
+
group.rss_bytes / prior.rss_bytes < MEMORY_GROWTH_RATIO
|
|
474
|
+
)
|
|
475
|
+
return [];
|
|
476
|
+
return [
|
|
477
|
+
memoryGrowthCandidate(
|
|
478
|
+
group,
|
|
479
|
+
activity,
|
|
480
|
+
"resource.memory-growth",
|
|
481
|
+
`${groupLabel(group)} grew by ${delta} resident bytes since ${baseline.sampled_at}.`,
|
|
482
|
+
resourceSource(resource, capability.state),
|
|
483
|
+
delta,
|
|
484
|
+
capability,
|
|
485
|
+
),
|
|
486
|
+
];
|
|
487
|
+
})
|
|
488
|
+
.slice(0, 5);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
function memoryGrowthCandidate(
|
|
492
|
+
group: ResourceProcessGroup,
|
|
493
|
+
activity: SupervisorActivitySnapshot | undefined,
|
|
494
|
+
findingKind: string,
|
|
495
|
+
summary: string,
|
|
496
|
+
source: SupervisorSourceReference,
|
|
497
|
+
delta: number,
|
|
498
|
+
resourceCapability: SupervisorCapability,
|
|
499
|
+
): Candidate {
|
|
500
|
+
const workload = workloadContext(group, activity, source.observed_at);
|
|
501
|
+
const severity: SupervisorFindingSeverity =
|
|
502
|
+
workload.context.relationship === "unexpected-idle-growth" ? "critical" : "warning";
|
|
503
|
+
const result = candidate(
|
|
504
|
+
findingKind,
|
|
505
|
+
severity,
|
|
506
|
+
group.kind,
|
|
507
|
+
group.id,
|
|
508
|
+
summary,
|
|
509
|
+
source,
|
|
510
|
+
delta,
|
|
511
|
+
"bytes",
|
|
512
|
+
[resourceCapability, workload.capability],
|
|
513
|
+
groupAttribution(group),
|
|
514
|
+
workload.context,
|
|
515
|
+
);
|
|
516
|
+
if (workload.evidence) result.evidence.push(workload.evidence);
|
|
517
|
+
return result;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
function workloadContext(
|
|
521
|
+
group: ResourceProcessGroup,
|
|
522
|
+
activity: SupervisorActivitySnapshot | undefined,
|
|
523
|
+
observedAt: string,
|
|
524
|
+
): {
|
|
525
|
+
context: SupervisorFindingWorkloadContext;
|
|
526
|
+
capability: SupervisorCapability;
|
|
527
|
+
evidence?: SupervisorFindingEvidence;
|
|
528
|
+
} {
|
|
529
|
+
const capability = activity?.capability ?? {
|
|
530
|
+
source_kind: "coordination.activity-projection",
|
|
531
|
+
state: "unsupported" as const,
|
|
532
|
+
reason_code: "activity-projection-not-captured",
|
|
533
|
+
};
|
|
534
|
+
const matched =
|
|
535
|
+
group.kind === "agent"
|
|
536
|
+
? activity?.entries.find((entry) => entry.scope_id === group.id)
|
|
537
|
+
: undefined;
|
|
538
|
+
if (!matched) {
|
|
539
|
+
return {
|
|
540
|
+
context: {
|
|
541
|
+
relationship: "unknown",
|
|
542
|
+
declared_activity: "unknown",
|
|
543
|
+
task_state: "unknown",
|
|
544
|
+
observed_at: activity?.observed_at ?? observedAt,
|
|
545
|
+
source: stableSource(
|
|
546
|
+
"coordination.activity-projection",
|
|
547
|
+
group.kind === "agent" ? group.id : "not-applicable",
|
|
548
|
+
activity?.observed_at ?? observedAt,
|
|
549
|
+
capability.state,
|
|
550
|
+
undefined,
|
|
551
|
+
activity?.schema_version,
|
|
552
|
+
),
|
|
553
|
+
},
|
|
554
|
+
capability,
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
const relationship =
|
|
558
|
+
matched.declared_activity === "working" && matched.task_state === "active"
|
|
559
|
+
? "active-work"
|
|
560
|
+
: matched.declared_activity === "unknown"
|
|
561
|
+
? "unknown"
|
|
562
|
+
: "unexpected-idle-growth";
|
|
563
|
+
const summary = `Agent declared ${matched.declared_activity} with task state ${matched.task_state}.`;
|
|
564
|
+
return {
|
|
565
|
+
context: {
|
|
566
|
+
relationship,
|
|
567
|
+
declared_activity: matched.declared_activity,
|
|
568
|
+
task_state: matched.task_state,
|
|
569
|
+
observed_at: matched.observed_at,
|
|
570
|
+
source: matched.source,
|
|
571
|
+
},
|
|
572
|
+
capability,
|
|
573
|
+
evidence: {
|
|
574
|
+
id: deterministicId("ev", `${findingKindKey(group)}\u0000${matched.source.id}`),
|
|
575
|
+
source: matched.source,
|
|
576
|
+
summary,
|
|
577
|
+
},
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
function findingKindKey(group: ResourceProcessGroup): string {
|
|
582
|
+
return `resource.memory-growth\u0000${group.kind}\u0000${group.id}`;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
function processAttribution(process: ResourceProcessSample): SupervisorFindingAttribution {
|
|
586
|
+
if (
|
|
587
|
+
(process.owner_kind === "agent" || process.owner_kind === "service") &&
|
|
588
|
+
process.owner_id &&
|
|
589
|
+
process.owner_root_pid !== null
|
|
590
|
+
) {
|
|
591
|
+
return {
|
|
592
|
+
state: "attributed",
|
|
593
|
+
owner_kind: process.owner_kind,
|
|
594
|
+
owner_id: process.owner_id,
|
|
595
|
+
owner_root_pid: process.owner_root_pid,
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
return { state: "unattributed", reason_code: "no-validated-process-anchor" };
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
function groupAttribution(group: ResourceProcessGroup): SupervisorFindingAttribution {
|
|
602
|
+
if (group.kind === "agent" || group.kind === "service") {
|
|
603
|
+
return {
|
|
604
|
+
state: "attributed",
|
|
605
|
+
owner_kind: group.kind,
|
|
606
|
+
owner_id: group.id,
|
|
607
|
+
...(group.root_pids[0] !== undefined ? { owner_root_pid: group.root_pids[0] } : {}),
|
|
608
|
+
};
|
|
609
|
+
}
|
|
610
|
+
return { state: "unattributed", reason_code: "no-validated-process-anchor" };
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
function serviceStaleSummary(service: ObservedServiceHealth): string {
|
|
614
|
+
if (service.reason === "recorded-running-process-missing")
|
|
615
|
+
return `${service.id} was recorded running, but its process is no longer present.`;
|
|
616
|
+
if (service.reason === "remote-heartbeat-expired")
|
|
617
|
+
return `${service.id} has an expired remote heartbeat.`;
|
|
618
|
+
if (service.reason === "supervisor-reported-error") return `${service.id} reported an error.`;
|
|
619
|
+
return `${service.id} has stale liveness evidence.`;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
function candidate(
|
|
623
|
+
findingKind: string,
|
|
624
|
+
severity: SupervisorFindingSeverity,
|
|
625
|
+
scopeKind: string,
|
|
626
|
+
scopeId: string,
|
|
627
|
+
summary: string,
|
|
628
|
+
source: SupervisorSourceReference,
|
|
629
|
+
observedValue: number | undefined,
|
|
630
|
+
unit: SupervisorFindingEvidence["unit"] | undefined,
|
|
631
|
+
capabilities: SupervisorCapability[],
|
|
632
|
+
attribution?: SupervisorFindingAttribution,
|
|
633
|
+
workloadContext?: SupervisorFindingWorkloadContext,
|
|
634
|
+
): Candidate {
|
|
635
|
+
const evidence: SupervisorFindingEvidence = {
|
|
636
|
+
id: deterministicId("ev", `${findingKind}\u0000${scopeKind}\u0000${scopeId}\u0000${source.id}`),
|
|
637
|
+
source,
|
|
638
|
+
summary: summary.slice(0, SUPERVISOR_DIAGNOSTIC_LIMITS.max_summary_chars),
|
|
639
|
+
...(observedValue !== undefined ? { observed_value: observedValue } : {}),
|
|
640
|
+
...(unit ? { unit } : {}),
|
|
641
|
+
};
|
|
642
|
+
return {
|
|
643
|
+
source_kind: source.source_kind,
|
|
644
|
+
finding_kind: findingKind,
|
|
645
|
+
severity,
|
|
646
|
+
scope_kind: scopeKind,
|
|
647
|
+
scope_id: scopeId,
|
|
648
|
+
summary: evidence.summary,
|
|
649
|
+
primary_source: source,
|
|
650
|
+
evidence: [evidence],
|
|
651
|
+
capabilities,
|
|
652
|
+
...(attribution ? { attribution } : {}),
|
|
653
|
+
...(workloadContext ? { workload_context: workloadContext } : {}),
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
function resourceSource(
|
|
658
|
+
resource: ResourceSnapshot,
|
|
659
|
+
capability: SupervisorSourceReference["capability"],
|
|
660
|
+
recordId?: string,
|
|
661
|
+
): SupervisorSourceReference {
|
|
662
|
+
return stableSource(
|
|
663
|
+
"resource.snapshot",
|
|
664
|
+
`${resource.platform}:${resource.namespace}`,
|
|
665
|
+
resource.sampled_at,
|
|
666
|
+
capability,
|
|
667
|
+
recordId,
|
|
668
|
+
resource.schema_version,
|
|
669
|
+
);
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
function stableSource(
|
|
673
|
+
sourceKind: string,
|
|
674
|
+
sourceId: string,
|
|
675
|
+
observedAt: string,
|
|
676
|
+
capability: SupervisorSourceReference["capability"],
|
|
677
|
+
recordId?: string,
|
|
678
|
+
schemaVersion?: number,
|
|
679
|
+
): SupervisorSourceReference {
|
|
680
|
+
return {
|
|
681
|
+
id: deterministicId(
|
|
682
|
+
"src",
|
|
683
|
+
`${sourceKind}\u0000${sourceId}\u0000${recordId ?? ""}\u0000${observedAt}`,
|
|
684
|
+
),
|
|
685
|
+
source_kind: sourceKind,
|
|
686
|
+
source_id: sourceId,
|
|
687
|
+
observed_at: observedAt,
|
|
688
|
+
...(recordId ? { record_id: recordId } : {}),
|
|
689
|
+
...(schemaVersion !== undefined ? { schema_version: schemaVersion } : {}),
|
|
690
|
+
capability,
|
|
691
|
+
};
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
function recentResolved(
|
|
695
|
+
findings: SupervisorFindings,
|
|
696
|
+
fingerprint: string,
|
|
697
|
+
observedAt: string,
|
|
698
|
+
): SupervisorFinding | undefined {
|
|
699
|
+
const observedMs = Date.parse(observedAt);
|
|
700
|
+
return findings.transitions
|
|
701
|
+
.filter(
|
|
702
|
+
(entry) =>
|
|
703
|
+
entry.fingerprint === fingerprint &&
|
|
704
|
+
entry.state === "resolved" &&
|
|
705
|
+
entry.resolved_at !== undefined &&
|
|
706
|
+
observedMs - Date.parse(entry.resolved_at) >= 0 &&
|
|
707
|
+
observedMs - Date.parse(entry.resolved_at) <= SUPERVISOR_FINDING_POLICY.episode_gap_ms,
|
|
708
|
+
)
|
|
709
|
+
.sort((left, right) => (right.resolved_at ?? "").localeCompare(left.resolved_at ?? ""))[0];
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
function peakEvidence(
|
|
713
|
+
prior: SupervisorFinding | undefined,
|
|
714
|
+
evidence: readonly SupervisorFindingEvidence[],
|
|
715
|
+
observedAt: string,
|
|
716
|
+
): Pick<SupervisorFinding, "peak_observed_value" | "peak_observed_at" | "peak_unit"> {
|
|
717
|
+
const current = evidence
|
|
718
|
+
.filter(
|
|
719
|
+
(entry): entry is SupervisorFindingEvidence & { observed_value: number } =>
|
|
720
|
+
entry.observed_value !== undefined,
|
|
721
|
+
)
|
|
722
|
+
.sort((left, right) => right.observed_value - left.observed_value)[0];
|
|
723
|
+
if (
|
|
724
|
+
prior?.peak_observed_value !== undefined &&
|
|
725
|
+
(!current || prior.peak_observed_value >= current.observed_value)
|
|
726
|
+
) {
|
|
727
|
+
return {
|
|
728
|
+
peak_observed_value: prior.peak_observed_value,
|
|
729
|
+
...(prior.peak_observed_at ? { peak_observed_at: prior.peak_observed_at } : {}),
|
|
730
|
+
...(prior.peak_unit ? { peak_unit: prior.peak_unit } : {}),
|
|
731
|
+
};
|
|
732
|
+
}
|
|
733
|
+
return current
|
|
734
|
+
? {
|
|
735
|
+
peak_observed_value: current.observed_value,
|
|
736
|
+
peak_observed_at: current.source.observed_at || observedAt,
|
|
737
|
+
...(current.unit ? { peak_unit: current.unit } : {}),
|
|
738
|
+
}
|
|
739
|
+
: {};
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
function findingFingerprint(candidate: Candidate): string {
|
|
743
|
+
return digest(
|
|
744
|
+
[
|
|
745
|
+
candidate.source_kind,
|
|
746
|
+
candidate.finding_kind,
|
|
747
|
+
candidate.scope_kind,
|
|
748
|
+
candidate.scope_id,
|
|
749
|
+
candidate.primary_source.source_id,
|
|
750
|
+
].join("\u0000"),
|
|
751
|
+
).slice(0, 32);
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
function deterministicId(prefix: string, value: string): string {
|
|
755
|
+
return `${prefix}_${digest(value).slice(0, 24)}`;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
function digest(value: string): string {
|
|
759
|
+
return createHash("sha256").update(value).digest("hex");
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
function validFindings(value: SupervisorFindings | undefined): value is SupervisorFindings {
|
|
763
|
+
return (
|
|
764
|
+
value?.schema_version === SUPERVISOR_FINDING_SCHEMA_VERSION &&
|
|
765
|
+
Array.isArray(value.active) &&
|
|
766
|
+
Array.isArray(value.transitions)
|
|
767
|
+
);
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
function boundCapabilities(capabilities: SupervisorCapability[]): SupervisorCapability[] {
|
|
771
|
+
const unique = new Map(capabilities.map((entry) => [entry.source_kind, entry]));
|
|
772
|
+
return [...unique.values()].slice(0, SUPERVISOR_DIAGNOSTIC_LIMITS.max_capabilities);
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
function groupKey(group: ResourceProcessGroup): string {
|
|
776
|
+
return `${group.kind}:${group.id}`;
|
|
777
|
+
}
|
|
778
|
+
function groupLabel(group: ResourceProcessGroup): string {
|
|
779
|
+
return group.kind === "unattributed"
|
|
780
|
+
? "Unattributed processes"
|
|
781
|
+
: `${group.kind} group ${group.id}`;
|
|
782
|
+
}
|
|
783
|
+
function severityRank(value: SupervisorFindingSeverity): number {
|
|
784
|
+
return value === "critical" ? 3 : value === "warning" ? 2 : 1;
|
|
785
|
+
}
|
|
786
|
+
function round1(value: number): number {
|
|
787
|
+
return Math.round(value * 10) / 10;
|
|
788
|
+
}
|