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,203 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync, rmSync, statSync } from "node:fs";
|
|
2
|
+
import { hostname } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { checkPidToken, processStartToken } from "../agents/state/proc-start.ts";
|
|
5
|
+
import { writePrivateJsonAtomic } from "../storage/atomic-json.ts";
|
|
6
|
+
import {
|
|
7
|
+
type ObservedServiceHealth,
|
|
8
|
+
SUPERVISOR_CONSUMER_SCHEMA_VERSION,
|
|
9
|
+
type SupervisorConsumerRecord,
|
|
10
|
+
type SupervisorServiceStatusRecord,
|
|
11
|
+
} from "./contract.ts";
|
|
12
|
+
import { readSupervisorConsumer, supervisorConsumerPath, supervisorPaths } from "./storage.ts";
|
|
13
|
+
|
|
14
|
+
export function registerSupervisorConsumer(
|
|
15
|
+
coordRoot: string,
|
|
16
|
+
input: { id: string; pid?: number },
|
|
17
|
+
): SupervisorConsumerRecord {
|
|
18
|
+
const pid = input.pid ?? process.pid;
|
|
19
|
+
const startToken = processStartToken(pid);
|
|
20
|
+
const record: SupervisorConsumerRecord = {
|
|
21
|
+
schema_version: SUPERVISOR_CONSUMER_SCHEMA_VERSION,
|
|
22
|
+
id: input.id,
|
|
23
|
+
pid,
|
|
24
|
+
...(startToken ? { start_token: startToken } : {}),
|
|
25
|
+
registered_at: new Date().toISOString(),
|
|
26
|
+
};
|
|
27
|
+
writePrivateJsonAtomic(supervisorConsumerPath(coordRoot, input.id), record);
|
|
28
|
+
return record;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function unregisterSupervisorConsumer(coordRoot: string, id: string): void {
|
|
32
|
+
rmSync(supervisorConsumerPath(coordRoot, id), { force: true });
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function collectServiceHealth(
|
|
36
|
+
coordRoot: string,
|
|
37
|
+
supervisor: SupervisorServiceStatusRecord,
|
|
38
|
+
): { services: ObservedServiceHealth[]; consumers: SupervisorConsumerRecord[] } {
|
|
39
|
+
const consumers = readLiveConsumers(coordRoot);
|
|
40
|
+
const semantic = readDetachedService(
|
|
41
|
+
join(coordRoot, ".harnery", "semantic", "v2", "service.json"),
|
|
42
|
+
"semantic-reader",
|
|
43
|
+
);
|
|
44
|
+
const governor = readDetachedService(
|
|
45
|
+
join(coordRoot, ".harnery", "governor-service", "service.json"),
|
|
46
|
+
"governor",
|
|
47
|
+
);
|
|
48
|
+
const relay = readPresenceRelay(join(coordRoot, ".harnery", "presence", "relay-daemon.json"));
|
|
49
|
+
const dashboard = consumers.find((consumer) => consumer.id === "dashboard");
|
|
50
|
+
const services: ObservedServiceHealth[] = [
|
|
51
|
+
{
|
|
52
|
+
id: "supervisor",
|
|
53
|
+
state:
|
|
54
|
+
supervisor.state === "running"
|
|
55
|
+
? "running"
|
|
56
|
+
: supervisor.state === "error"
|
|
57
|
+
? "stale"
|
|
58
|
+
: "stopped",
|
|
59
|
+
pid: supervisor.pid,
|
|
60
|
+
started_at: supervisor.started_at,
|
|
61
|
+
heartbeat_at: supervisor.heartbeat_at,
|
|
62
|
+
...(supervisor.state === "error" ? { reason: "supervisor-reported-error" } : {}),
|
|
63
|
+
},
|
|
64
|
+
semantic,
|
|
65
|
+
governor,
|
|
66
|
+
relay,
|
|
67
|
+
dashboard
|
|
68
|
+
? {
|
|
69
|
+
id: "dashboard",
|
|
70
|
+
state: "running",
|
|
71
|
+
pid: dashboard.pid,
|
|
72
|
+
started_at: dashboard.registered_at,
|
|
73
|
+
}
|
|
74
|
+
: { id: "dashboard", state: "stopped" },
|
|
75
|
+
];
|
|
76
|
+
return { services, consumers };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function readLiveConsumers(coordRoot: string): SupervisorConsumerRecord[] {
|
|
80
|
+
const directory = supervisorPaths(coordRoot).consumers;
|
|
81
|
+
if (!existsSync(directory)) return [];
|
|
82
|
+
const live: SupervisorConsumerRecord[] = [];
|
|
83
|
+
for (const entry of readdirSync(directory)) {
|
|
84
|
+
if (!entry.endsWith(".json")) continue;
|
|
85
|
+
const path = `${directory}/${entry}`;
|
|
86
|
+
const record = readSupervisorConsumer(path);
|
|
87
|
+
if (!record || !consumerAlive(record)) {
|
|
88
|
+
rmSync(path, { force: true });
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
live.push(record);
|
|
92
|
+
}
|
|
93
|
+
return live.sort((left, right) => left.id.localeCompare(right.id));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function consumerAlive(record: SupervisorConsumerRecord): boolean {
|
|
97
|
+
if (
|
|
98
|
+
record.schema_version !== SUPERVISOR_CONSUMER_SCHEMA_VERSION ||
|
|
99
|
+
!Number.isSafeInteger(record.pid) ||
|
|
100
|
+
record.pid < 1 ||
|
|
101
|
+
typeof record.id !== "string"
|
|
102
|
+
) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
process.kill(record.pid, 0);
|
|
107
|
+
} catch (error) {
|
|
108
|
+
if ((error as NodeJS.ErrnoException).code !== "EPERM") return false;
|
|
109
|
+
}
|
|
110
|
+
return checkPidToken(record.pid, record.start_token) !== "mismatch";
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
interface DetachedServiceRecord {
|
|
114
|
+
pid: number;
|
|
115
|
+
host?: string;
|
|
116
|
+
state?: string;
|
|
117
|
+
started_at?: string;
|
|
118
|
+
heartbeat_at?: string;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function readDetachedService(
|
|
122
|
+
path: string,
|
|
123
|
+
id: "semantic-reader" | "governor",
|
|
124
|
+
): ObservedServiceHealth {
|
|
125
|
+
const record = readBoundedJson<DetachedServiceRecord>(path);
|
|
126
|
+
if (!record || !Number.isSafeInteger(record.pid) || record.pid < 1) {
|
|
127
|
+
return { id, state: "stopped" };
|
|
128
|
+
}
|
|
129
|
+
if (record.state === "stopping" || record.state === "stopped") {
|
|
130
|
+
return projection(id, "stopped", record);
|
|
131
|
+
}
|
|
132
|
+
if (record.host && record.host !== hostname()) {
|
|
133
|
+
const age = record.heartbeat_at
|
|
134
|
+
? Date.now() - Date.parse(record.heartbeat_at)
|
|
135
|
+
: Number.POSITIVE_INFINITY;
|
|
136
|
+
const state = Number.isFinite(age) && age < 120_000 ? "running" : "stale";
|
|
137
|
+
return projection(
|
|
138
|
+
id,
|
|
139
|
+
state,
|
|
140
|
+
record,
|
|
141
|
+
state === "stale" ? "remote-heartbeat-expired" : undefined,
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
const state = pidAlive(record.pid) ? "running" : "stale";
|
|
145
|
+
return projection(
|
|
146
|
+
id,
|
|
147
|
+
state,
|
|
148
|
+
record,
|
|
149
|
+
state === "stale" ? "recorded-running-process-missing" : undefined,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function readPresenceRelay(path: string): ObservedServiceHealth {
|
|
154
|
+
const record = readBoundedJson<{ pid: number; started_at?: string }>(path);
|
|
155
|
+
if (!record || !Number.isSafeInteger(record.pid) || record.pid < 1) {
|
|
156
|
+
return { id: "presence-relay", state: "stopped" };
|
|
157
|
+
}
|
|
158
|
+
const running = pidAlive(record.pid);
|
|
159
|
+
return {
|
|
160
|
+
id: "presence-relay",
|
|
161
|
+
state: running ? "running" : "stale",
|
|
162
|
+
pid: record.pid,
|
|
163
|
+
...(record.started_at ? { started_at: record.started_at } : {}),
|
|
164
|
+
...(!running ? { reason: "recorded-running-process-missing" } : {}),
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function projection(
|
|
169
|
+
id: "semantic-reader" | "governor",
|
|
170
|
+
state: ObservedServiceHealth["state"],
|
|
171
|
+
record: DetachedServiceRecord,
|
|
172
|
+
reason?: string,
|
|
173
|
+
): ObservedServiceHealth {
|
|
174
|
+
return {
|
|
175
|
+
id,
|
|
176
|
+
state,
|
|
177
|
+
pid: record.pid,
|
|
178
|
+
...(record.started_at ? { started_at: record.started_at } : {}),
|
|
179
|
+
...(record.heartbeat_at ? { heartbeat_at: record.heartbeat_at } : {}),
|
|
180
|
+
...(reason ? { reason } : {}),
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function readBoundedJson<T>(path: string): T | undefined {
|
|
185
|
+
if (!existsSync(path)) return undefined;
|
|
186
|
+
try {
|
|
187
|
+
const size = statSync(path).size;
|
|
188
|
+
if (size < 2 || size > 512 * 1_024) return undefined;
|
|
189
|
+
const value = JSON.parse(readFileSync(path, "utf8")) as unknown;
|
|
190
|
+
return value && typeof value === "object" && !Array.isArray(value) ? (value as T) : undefined;
|
|
191
|
+
} catch {
|
|
192
|
+
return undefined;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function pidAlive(pid: number): boolean {
|
|
197
|
+
try {
|
|
198
|
+
process.kill(pid, 0);
|
|
199
|
+
return true;
|
|
200
|
+
} catch (error) {
|
|
201
|
+
return (error as NodeJS.ErrnoException).code === "EPERM";
|
|
202
|
+
}
|
|
203
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { hostname } from "node:os";
|
|
2
|
+
import { checkPidToken } from "../agents/state/proc-start.ts";
|
|
3
|
+
import {
|
|
4
|
+
SUPERVISOR_STATUS_SCHEMA_VERSION,
|
|
5
|
+
type SupervisorServiceStatusRecord,
|
|
6
|
+
type SupervisorStatus,
|
|
7
|
+
} from "./contract.ts";
|
|
8
|
+
import { readSupervisorServiceRecord, supervisorPaths } from "./storage.ts";
|
|
9
|
+
|
|
10
|
+
const STALE_HEARTBEAT_MS = 15_000;
|
|
11
|
+
|
|
12
|
+
export function readSupervisorStatus(coordRoot: string, nowMs = Date.now()): SupervisorStatus {
|
|
13
|
+
const paths = supervisorPaths(coordRoot);
|
|
14
|
+
const record = readSupervisorServiceRecord(coordRoot);
|
|
15
|
+
if (!validRecord(record)) {
|
|
16
|
+
return {
|
|
17
|
+
running: false,
|
|
18
|
+
stale: false,
|
|
19
|
+
status_path: paths.service,
|
|
20
|
+
snapshot_path: paths.snapshot,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
const heartbeatAt = Date.parse(record.heartbeat_at);
|
|
24
|
+
const stale = !Number.isFinite(heartbeatAt) || nowMs - heartbeatAt > STALE_HEARTBEAT_MS;
|
|
25
|
+
const running =
|
|
26
|
+
record.host === hostname() &&
|
|
27
|
+
!stale &&
|
|
28
|
+
(record.state === "starting" || record.state === "running" || record.state === "stopping") &&
|
|
29
|
+
pidStillMatches(record.pid, record.start_token);
|
|
30
|
+
return {
|
|
31
|
+
running,
|
|
32
|
+
stale,
|
|
33
|
+
record,
|
|
34
|
+
status_path: paths.service,
|
|
35
|
+
snapshot_path: paths.snapshot,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function validRecord(
|
|
40
|
+
value: SupervisorServiceStatusRecord | undefined,
|
|
41
|
+
): value is SupervisorServiceStatusRecord {
|
|
42
|
+
return (
|
|
43
|
+
value?.schema_version === SUPERVISOR_STATUS_SCHEMA_VERSION &&
|
|
44
|
+
Number.isSafeInteger(value.pid) &&
|
|
45
|
+
value.pid > 0 &&
|
|
46
|
+
typeof value.host === "string" &&
|
|
47
|
+
typeof value.heartbeat_at === "string" &&
|
|
48
|
+
typeof value.keep_alive === "boolean"
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function pidStillMatches(pid: number, startToken: string | undefined): boolean {
|
|
53
|
+
try {
|
|
54
|
+
process.kill(pid, 0);
|
|
55
|
+
} catch (error) {
|
|
56
|
+
if ((error as NodeJS.ErrnoException).code !== "EPERM") return false;
|
|
57
|
+
}
|
|
58
|
+
return checkPidToken(pid, startToken) !== "mismatch";
|
|
59
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
|
+
import type { CoordinationHealthSnapshot } from "../agents/health.ts";
|
|
4
|
+
import type {
|
|
5
|
+
SupervisorActivitySnapshot,
|
|
6
|
+
SupervisorConsumerRecord,
|
|
7
|
+
SupervisorFindingExplanation,
|
|
8
|
+
SupervisorFindings,
|
|
9
|
+
SupervisorHistory,
|
|
10
|
+
SupervisorLogFeed,
|
|
11
|
+
SupervisorServiceStatusRecord,
|
|
12
|
+
SupervisorSnapshot,
|
|
13
|
+
SupervisorTimeline,
|
|
14
|
+
} from "./contract.ts";
|
|
15
|
+
|
|
16
|
+
export interface SupervisorPaths {
|
|
17
|
+
root: string;
|
|
18
|
+
service: string;
|
|
19
|
+
lease: string;
|
|
20
|
+
stop: string;
|
|
21
|
+
snapshot: string;
|
|
22
|
+
history: string;
|
|
23
|
+
activity: string;
|
|
24
|
+
findings: string;
|
|
25
|
+
timelines: string;
|
|
26
|
+
explanations: string;
|
|
27
|
+
coordination_health: string;
|
|
28
|
+
log_feed: string;
|
|
29
|
+
consumers: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function supervisorPaths(coordRootRaw: string): SupervisorPaths {
|
|
33
|
+
const root = join(resolve(coordRootRaw), ".harnery", "supervisor");
|
|
34
|
+
return {
|
|
35
|
+
root,
|
|
36
|
+
service: join(root, "service.json"),
|
|
37
|
+
lease: join(root, "service.lease.json"),
|
|
38
|
+
stop: join(root, "stop.json"),
|
|
39
|
+
snapshot: join(root, "snapshot.json"),
|
|
40
|
+
history: join(root, "history.json"),
|
|
41
|
+
activity: join(root, "activity.json"),
|
|
42
|
+
findings: join(root, "findings.json"),
|
|
43
|
+
timelines: join(root, "timelines"),
|
|
44
|
+
explanations: join(root, "explanations"),
|
|
45
|
+
coordination_health: join(root, "coordination-health.json"),
|
|
46
|
+
log_feed: join(root, "log-feed.json"),
|
|
47
|
+
consumers: join(root, "consumers"),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function supervisorConsumerPath(coordRoot: string, id: string): string {
|
|
52
|
+
return join(supervisorPaths(coordRoot).consumers, `${safeId(id)}.json`);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function readSupervisorServiceRecord(
|
|
56
|
+
coordRoot: string,
|
|
57
|
+
): SupervisorServiceStatusRecord | undefined {
|
|
58
|
+
return readJson<SupervisorServiceStatusRecord>(supervisorPaths(coordRoot).service);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function readSupervisorSnapshot(coordRoot: string): SupervisorSnapshot | undefined {
|
|
62
|
+
return readJson<SupervisorSnapshot>(supervisorPaths(coordRoot).snapshot);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function readSupervisorHistory(coordRoot: string): SupervisorHistory | undefined {
|
|
66
|
+
return readJson<SupervisorHistory>(supervisorPaths(coordRoot).history);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function readSupervisorActivity(coordRoot: string): SupervisorActivitySnapshot | undefined {
|
|
70
|
+
return readJson<SupervisorActivitySnapshot>(supervisorPaths(coordRoot).activity);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function readSupervisorFindings(coordRoot: string): SupervisorFindings | undefined {
|
|
74
|
+
return readJson<SupervisorFindings>(supervisorPaths(coordRoot).findings);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function readSupervisorTimeline(
|
|
78
|
+
coordRoot: string,
|
|
79
|
+
findingId: string,
|
|
80
|
+
): SupervisorTimeline | undefined {
|
|
81
|
+
return readJson<SupervisorTimeline>(
|
|
82
|
+
join(supervisorPaths(coordRoot).timelines, `${safeId(findingId)}.json`),
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function readSupervisorExplanation(
|
|
87
|
+
coordRoot: string,
|
|
88
|
+
findingId: string,
|
|
89
|
+
): SupervisorFindingExplanation | undefined {
|
|
90
|
+
return readJson<SupervisorFindingExplanation>(
|
|
91
|
+
join(supervisorPaths(coordRoot).explanations, `${safeId(findingId)}.json`),
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function readCoordinationHealthSnapshot(
|
|
96
|
+
coordRoot: string,
|
|
97
|
+
): CoordinationHealthSnapshot | undefined {
|
|
98
|
+
return readJson<CoordinationHealthSnapshot>(supervisorPaths(coordRoot).coordination_health);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function readSupervisorLogFeed(coordRoot: string): SupervisorLogFeed | undefined {
|
|
102
|
+
return readJson<SupervisorLogFeed>(supervisorPaths(coordRoot).log_feed);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function readSupervisorConsumer(path: string): SupervisorConsumerRecord | undefined {
|
|
106
|
+
return readJson<SupervisorConsumerRecord>(path);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function readJson<T>(path: string): T | undefined {
|
|
110
|
+
if (!existsSync(path)) return undefined;
|
|
111
|
+
try {
|
|
112
|
+
const value = JSON.parse(readFileSync(path, "utf8")) as unknown;
|
|
113
|
+
return value && typeof value === "object" && !Array.isArray(value) ? (value as T) : undefined;
|
|
114
|
+
} catch {
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function safeId(value: string): string {
|
|
120
|
+
const normalized = value
|
|
121
|
+
.toLowerCase()
|
|
122
|
+
.replace(/[^a-z0-9_-]+/g, "-")
|
|
123
|
+
.replace(/^-+|-+$/g, "");
|
|
124
|
+
if (!normalized) throw new Error("supervisor consumer id must contain a letter or number");
|
|
125
|
+
return normalized.slice(0, 80);
|
|
126
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
SUPERVISOR_DIAGNOSTIC_LIMITS,
|
|
4
|
+
SUPERVISOR_TIMELINE_SCHEMA_VERSION,
|
|
5
|
+
type SupervisorFinding,
|
|
6
|
+
type SupervisorSourceReference,
|
|
7
|
+
type SupervisorTimeline,
|
|
8
|
+
type SupervisorTimelineEntry,
|
|
9
|
+
} from "./contract.ts";
|
|
10
|
+
|
|
11
|
+
export function buildSupervisorTimeline(
|
|
12
|
+
finding: SupervisorFinding,
|
|
13
|
+
relatedSources: readonly SupervisorSourceReference[] = [],
|
|
14
|
+
): SupervisorTimeline {
|
|
15
|
+
const relatedEntries = clusterRelatedSources(relatedSources);
|
|
16
|
+
const entries: SupervisorTimelineEntry[] = [
|
|
17
|
+
entry("opened", finding.opened_at, finding.summary, finding.primary_source),
|
|
18
|
+
...finding.evidence.map((evidence) => ({
|
|
19
|
+
...entry("observed", evidence.source.observed_at, evidence.summary, evidence.source),
|
|
20
|
+
evidence_id: evidence.id,
|
|
21
|
+
})),
|
|
22
|
+
...relatedEntries,
|
|
23
|
+
...finding.capabilities
|
|
24
|
+
.filter((capability) => capability.state !== "supported")
|
|
25
|
+
.map((capability) =>
|
|
26
|
+
entry(
|
|
27
|
+
"capability",
|
|
28
|
+
finding.observed_at,
|
|
29
|
+
`${capability.source_kind} capability is ${capability.state}${capability.reason_code ? ` (${capability.reason_code})` : ""}.`,
|
|
30
|
+
{
|
|
31
|
+
id: stableId("src", `${finding.id}:${capability.source_kind}:${capability.state}`),
|
|
32
|
+
source_kind: capability.source_kind,
|
|
33
|
+
source_id: capability.reason_code ?? capability.state,
|
|
34
|
+
observed_at: finding.observed_at,
|
|
35
|
+
capability: capability.state,
|
|
36
|
+
},
|
|
37
|
+
),
|
|
38
|
+
),
|
|
39
|
+
...(finding.state === "resolved" && finding.resolved_at
|
|
40
|
+
? [
|
|
41
|
+
entry(
|
|
42
|
+
"resolved",
|
|
43
|
+
finding.resolved_at,
|
|
44
|
+
"The observed condition returned below its threshold.",
|
|
45
|
+
finding.primary_source,
|
|
46
|
+
),
|
|
47
|
+
]
|
|
48
|
+
: []),
|
|
49
|
+
].sort(
|
|
50
|
+
(left, right) =>
|
|
51
|
+
left.occurred_at.localeCompare(right.occurred_at) || left.id.localeCompare(right.id),
|
|
52
|
+
);
|
|
53
|
+
const maxEntries = SUPERVISOR_DIAGNOSTIC_LIMITS.max_timeline_entries;
|
|
54
|
+
const bounded = entries.slice(-maxEntries);
|
|
55
|
+
return {
|
|
56
|
+
schema_version: SUPERVISOR_TIMELINE_SCHEMA_VERSION,
|
|
57
|
+
finding_id: finding.id,
|
|
58
|
+
start_at: bounded[0]?.occurred_at ?? finding.opened_at,
|
|
59
|
+
end_at: bounded[bounded.length - 1]?.occurred_at ?? finding.observed_at,
|
|
60
|
+
max_entries: maxEntries,
|
|
61
|
+
omitted_entries: Math.max(0, entries.length - bounded.length),
|
|
62
|
+
compacted_entries: Math.max(0, relatedSources.length - relatedEntries.length),
|
|
63
|
+
entries: bounded,
|
|
64
|
+
capabilities: finding.capabilities.slice(0, SUPERVISOR_DIAGNOSTIC_LIMITS.max_capabilities),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function entry(
|
|
69
|
+
relation: SupervisorTimelineEntry["relation"],
|
|
70
|
+
occurredAt: string,
|
|
71
|
+
summary: string,
|
|
72
|
+
source: SupervisorSourceReference,
|
|
73
|
+
): SupervisorTimelineEntry {
|
|
74
|
+
return {
|
|
75
|
+
id: stableId("tl", `${relation}\u0000${occurredAt}\u0000${source.id}\u0000${summary}`),
|
|
76
|
+
occurred_at: occurredAt,
|
|
77
|
+
first_occurred_at: occurredAt,
|
|
78
|
+
last_occurred_at: occurredAt,
|
|
79
|
+
occurrence_count: 1,
|
|
80
|
+
relation,
|
|
81
|
+
summary: summary.slice(0, SUPERVISOR_DIAGNOSTIC_LIMITS.max_summary_chars),
|
|
82
|
+
source,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function clusterRelatedSources(
|
|
87
|
+
sources: readonly SupervisorSourceReference[],
|
|
88
|
+
): SupervisorTimelineEntry[] {
|
|
89
|
+
const groups = new Map<
|
|
90
|
+
string,
|
|
91
|
+
{
|
|
92
|
+
first: SupervisorSourceReference;
|
|
93
|
+
last: SupervisorSourceReference;
|
|
94
|
+
count: number;
|
|
95
|
+
}
|
|
96
|
+
>();
|
|
97
|
+
const ordered = [...sources].sort(
|
|
98
|
+
(left, right) =>
|
|
99
|
+
left.observed_at.localeCompare(right.observed_at) || left.id.localeCompare(right.id),
|
|
100
|
+
);
|
|
101
|
+
for (const source of ordered) {
|
|
102
|
+
const key = `${source.source_kind}\u0000${source.source_id}\u0000${source.capability}`;
|
|
103
|
+
const group = groups.get(key);
|
|
104
|
+
if (group) {
|
|
105
|
+
group.last = source;
|
|
106
|
+
group.count += 1;
|
|
107
|
+
} else {
|
|
108
|
+
groups.set(key, { first: source, last: source, count: 1 });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return [...groups.entries()].map(([key, group]) => {
|
|
112
|
+
const firstAt = group.first.observed_at;
|
|
113
|
+
const lastAt = group.last.observed_at;
|
|
114
|
+
const summary =
|
|
115
|
+
group.count === 1
|
|
116
|
+
? `Related ${group.last.source_kind} activity was observed in the same time range.`
|
|
117
|
+
: `${group.count} related ${group.last.source_kind} ${group.last.source_id} observations were recorded in the same time range.`;
|
|
118
|
+
return {
|
|
119
|
+
id: stableId("tl", `related\u0000${key}\u0000${firstAt}\u0000${lastAt}\u0000${group.count}`),
|
|
120
|
+
occurred_at: lastAt,
|
|
121
|
+
first_occurred_at: firstAt,
|
|
122
|
+
last_occurred_at: lastAt,
|
|
123
|
+
occurrence_count: group.count,
|
|
124
|
+
relation: "related",
|
|
125
|
+
summary: summary.slice(0, SUPERVISOR_DIAGNOSTIC_LIMITS.max_summary_chars),
|
|
126
|
+
source: group.last,
|
|
127
|
+
};
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function stableId(prefix: string, value: string): string {
|
|
132
|
+
return `${prefix}_${createHash("sha256").update(value).digest("hex").slice(0, 24)}`;
|
|
133
|
+
}
|