@ddtcorex/dsh-maestro-core 0.10.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/LICENSE +21 -0
- package/README.md +209 -0
- package/cordis.patch.yml +43 -0
- package/lib/bin.d.ts +2 -0
- package/lib/bin.js +3 -0
- package/lib/boot-guard.d.ts +33 -0
- package/lib/boot-guard.js +68 -0
- package/lib/boot-lock.d.ts +56 -0
- package/lib/boot-lock.js +155 -0
- package/lib/cli.d.ts +31 -0
- package/lib/cli.js +336 -0
- package/lib/client.js +2901 -0
- package/lib/config/index.d.ts +34 -0
- package/lib/config/index.js +62 -0
- package/lib/config/service.d.ts +18 -0
- package/lib/config/service.js +28 -0
- package/lib/config.d.ts +1 -0
- package/lib/config.js +19 -0
- package/lib/daemon-freshness.d.ts +48 -0
- package/lib/daemon-freshness.js +122 -0
- package/lib/debug-agent.d.ts +17 -0
- package/lib/debug-agent.js +144 -0
- package/lib/dsh-session.d.ts +26 -0
- package/lib/dsh-session.js +45 -0
- package/lib/guard/config.d.ts +82 -0
- package/lib/guard/config.js +211 -0
- package/lib/guard/decide.d.ts +30 -0
- package/lib/guard/decide.js +38 -0
- package/lib/guard/full-scan-tool.d.ts +6 -0
- package/lib/guard/full-scan-tool.js +65 -0
- package/lib/guard/index.d.ts +85 -0
- package/lib/guard/index.js +254 -0
- package/lib/guard/journal.d.ts +146 -0
- package/lib/guard/journal.js +398 -0
- package/lib/guard/migrate.d.ts +15 -0
- package/lib/guard/migrate.js +42 -0
- package/lib/guard/parse.d.ts +193 -0
- package/lib/guard/parse.js +1103 -0
- package/lib/guard/paths.d.ts +104 -0
- package/lib/guard/paths.js +254 -0
- package/lib/guard/permission-policy.d.ts +10 -0
- package/lib/guard/permission-policy.js +21 -0
- package/lib/guard/redact.d.ts +25 -0
- package/lib/guard/redact.js +52 -0
- package/lib/guard/rules.d.ts +65 -0
- package/lib/guard/rules.js +832 -0
- package/lib/guard/status-tool.d.ts +54 -0
- package/lib/guard/status-tool.js +195 -0
- package/lib/guard/tiers.d.ts +2 -0
- package/lib/guard/tiers.js +1 -0
- package/lib/guard/validators.d.ts +4 -0
- package/lib/guard/validators.js +45 -0
- package/lib/health-poller.d.ts +90 -0
- package/lib/health-poller.js +430 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.js +7 -0
- package/lib/intents.d.ts +32 -0
- package/lib/intents.js +49 -0
- package/lib/notifier.d.ts +12 -0
- package/lib/notifier.js +62 -0
- package/lib/paths.d.ts +13 -0
- package/lib/paths.js +70 -0
- package/lib/plugin.d.ts +250 -0
- package/lib/plugin.js +1126 -0
- package/lib/preset.d.ts +73 -0
- package/lib/preset.js +158 -0
- package/lib/report.d.ts +11 -0
- package/lib/report.js +58 -0
- package/lib/restart-exec.d.ts +35 -0
- package/lib/restart-exec.js +60 -0
- package/lib/restart-guards.d.ts +39 -0
- package/lib/restart-guards.js +150 -0
- package/lib/restart-tool.d.ts +124 -0
- package/lib/restart-tool.js +539 -0
- package/lib/restart-web.d.ts +26 -0
- package/lib/restart-web.js +58 -0
- package/lib/resume-log.d.ts +24 -0
- package/lib/resume-log.js +18 -0
- package/lib/resume-tools.d.ts +131 -0
- package/lib/resume-tools.js +337 -0
- package/lib/resume.d.ts +33 -0
- package/lib/resume.js +221 -0
- package/lib/scan.d.ts +20 -0
- package/lib/scan.js +71 -0
- package/lib/self-kill-guard.d.ts +81 -0
- package/lib/self-kill-guard.js +208 -0
- package/lib/session-health.d.ts +89 -0
- package/lib/session-health.js +274 -0
- package/lib/session-log-file.d.ts +22 -0
- package/lib/session-log-file.js +74 -0
- package/lib/skill-provider.d.ts +6 -0
- package/lib/skill-provider.js +73 -0
- package/lib/snapshot.d.ts +59 -0
- package/lib/snapshot.js +358 -0
- package/lib/source.d.ts +19 -0
- package/lib/source.js +19 -0
- package/lib/store/index.d.ts +42 -0
- package/lib/store/index.js +340 -0
- package/lib/store/legacy.d.ts +17 -0
- package/lib/store/legacy.js +85 -0
- package/lib/supervisor.d.ts +119 -0
- package/lib/supervisor.js +741 -0
- package/lib/sync/backup-config.d.ts +55 -0
- package/lib/sync/backup-config.js +119 -0
- package/lib/sync/backup-service.d.ts +110 -0
- package/lib/sync/backup-service.js +440 -0
- package/lib/sync/bidirectional.d.ts +39 -0
- package/lib/sync/bidirectional.js +25 -0
- package/lib/sync/cli.d.ts +38 -0
- package/lib/sync/cli.js +536 -0
- package/lib/sync/config.d.ts +77 -0
- package/lib/sync/config.js +90 -0
- package/lib/sync/fingerprint.d.ts +29 -0
- package/lib/sync/fingerprint.js +65 -0
- package/lib/sync/hashing.d.ts +9 -0
- package/lib/sync/hashing.js +47 -0
- package/lib/sync/index.d.ts +6 -0
- package/lib/sync/index.js +634 -0
- package/lib/sync/machine-id.d.ts +29 -0
- package/lib/sync/machine-id.js +66 -0
- package/lib/sync/merge.d.ts +33 -0
- package/lib/sync/merge.js +88 -0
- package/lib/sync/peer-host.d.ts +22 -0
- package/lib/sync/peer-host.js +62 -0
- package/lib/sync/process-runner.d.ts +31 -0
- package/lib/sync/process-runner.js +161 -0
- package/lib/sync/remote-agent.d.ts +42 -0
- package/lib/sync/remote-agent.js +217 -0
- package/lib/sync/remote-cache.d.ts +18 -0
- package/lib/sync/remote-cache.js +60 -0
- package/lib/sync/remote-manifest.d.ts +8 -0
- package/lib/sync/remote-manifest.js +64 -0
- package/lib/sync/s3-object-store.d.ts +38 -0
- package/lib/sync/s3-object-store.js +126 -0
- package/lib/sync/session-merge.d.ts +18 -0
- package/lib/sync/session-merge.js +191 -0
- package/lib/sync/session-plan.d.ts +29 -0
- package/lib/sync/session-plan.js +213 -0
- package/lib/sync/sigv4.d.ts +20 -0
- package/lib/sync/sigv4.js +57 -0
- package/lib/sync/snapshot.d.ts +21 -0
- package/lib/sync/snapshot.js +176 -0
- package/lib/sync/ssh-mux.d.ts +12 -0
- package/lib/sync/ssh-mux.js +48 -0
- package/lib/sync/sync-plan.d.ts +24 -0
- package/lib/sync/sync-plan.js +366 -0
- package/lib/sync/sync-service.d.ts +177 -0
- package/lib/sync/sync-service.js +819 -0
- package/lib/sync/sync-types.d.ts +87 -0
- package/lib/sync/sync-types.js +1 -0
- package/lib/sync/transport.d.ts +75 -0
- package/lib/sync/transport.js +200 -0
- package/lib/sync/tunnel-restore.d.ts +77 -0
- package/lib/sync/tunnel-restore.js +142 -0
- package/lib/sync/validation.d.ts +10 -0
- package/lib/sync/validation.js +83 -0
- package/lib/tool-view.d.ts +62 -0
- package/lib/tool-view.js +70 -0
- package/lib/types/client/auto-reload.d.ts +8 -0
- package/lib/types/client/auto-reload.d.ts.map +1 -0
- package/lib/types/client/config/MaestroSettings.d.ts +25 -0
- package/lib/types/client/config/MaestroSettings.d.ts.map +1 -0
- package/lib/types/client/config/components/BrandMark.d.ts +50 -0
- package/lib/types/client/config/components/BrandMark.d.ts.map +1 -0
- package/lib/types/client/config/guard-view.d.ts +83 -0
- package/lib/types/client/config/guard-view.d.ts.map +1 -0
- package/lib/types/client/config/index.d.ts +20 -0
- package/lib/types/client/config/index.d.ts.map +1 -0
- package/lib/types/client/config/maestro-mark.d.ts +29 -0
- package/lib/types/client/config/maestro-mark.d.ts.map +1 -0
- package/lib/types/client/config/settings-nav-icon.d.ts +21 -0
- package/lib/types/client/config/settings-nav-icon.d.ts.map +1 -0
- package/lib/types/client/index.d.ts +16 -0
- package/lib/types/client/index.d.ts.map +1 -0
- package/lib/types/client/sync/R2SyncPanel.d.ts +6 -0
- package/lib/types/client/sync/R2SyncPanel.d.ts.map +1 -0
- package/lib/types/client/sync/SyncPanel.d.ts +12 -0
- package/lib/types/client/sync/SyncPanel.d.ts.map +1 -0
- package/lib/types/client/sync/confirm-dialog.d.ts +43 -0
- package/lib/types/client/sync/confirm-dialog.d.ts.map +1 -0
- package/lib/types/client/sync/index.d.ts +10 -0
- package/lib/types/client/sync/index.d.ts.map +1 -0
- package/lib/types/client/sync/ui.d.ts +79 -0
- package/lib/types/client/sync/ui.d.ts.map +1 -0
- package/lib/types/client/sync/use-backup.d.ts +50 -0
- package/lib/types/client/sync/use-backup.d.ts.map +1 -0
- package/lib/types/client/sync/use-sync.d.ts +88 -0
- package/lib/types/client/sync/use-sync.d.ts.map +1 -0
- package/package.json +89 -0
- package/skills/dsh-safe-restart/SKILL.md +206 -0
- package/skills/dsh-safe-restart/scripts/restart-dsh-web.sh +468 -0
- package/src/client/auto-reload.ts +104 -0
- package/src/client/config/MaestroSettings.tsx +892 -0
- package/src/client/config/components/BrandMark.tsx +94 -0
- package/src/client/config/guard-view.ts +187 -0
- package/src/client/config/index.tsx +109 -0
- package/src/client/config/maestro-mark.ts +37 -0
- package/src/client/config/settings-nav-icon.ts +68 -0
- package/src/client/index.tsx +33 -0
- package/src/client/sync/R2SyncPanel.tsx +260 -0
- package/src/client/sync/SyncPanel.tsx +422 -0
- package/src/client/sync/confirm-dialog.tsx +202 -0
- package/src/client/sync/index.tsx +336 -0
- package/src/client/sync/ui.tsx +313 -0
- package/src/client/sync/use-backup.ts +207 -0
- package/src/client/sync/use-sync.ts +502 -0
- package/src/host/bin.ts +3 -0
- package/src/host/boot-guard.ts +76 -0
- package/src/host/boot-lock.ts +155 -0
- package/src/host/cli.ts +288 -0
- package/src/host/config/index.ts +78 -0
- package/src/host/config/service.ts +40 -0
- package/src/host/config.ts +18 -0
- package/src/host/daemon-freshness.ts +143 -0
- package/src/host/debug-agent.ts +145 -0
- package/src/host/dsh-session.ts +56 -0
- package/src/host/dsh-skill.d.ts +44 -0
- package/src/host/guard/augment.d.ts +33 -0
- package/src/host/guard/config.ts +244 -0
- package/src/host/guard/decide.ts +40 -0
- package/src/host/guard/full-scan-tool.ts +61 -0
- package/src/host/guard/index.ts +281 -0
- package/src/host/guard/journal.ts +440 -0
- package/src/host/guard/migrate.ts +41 -0
- package/src/host/guard/parse.ts +1150 -0
- package/src/host/guard/paths.ts +239 -0
- package/src/host/guard/permission-policy.ts +15 -0
- package/src/host/guard/redact.ts +64 -0
- package/src/host/guard/rules.ts +891 -0
- package/src/host/guard/status-tool.ts +229 -0
- package/src/host/guard/tiers.ts +2 -0
- package/src/host/guard/validators.ts +29 -0
- package/src/host/health-poller.ts +446 -0
- package/src/host/index.ts +7 -0
- package/src/host/intents.ts +67 -0
- package/src/host/notifier.ts +71 -0
- package/src/host/paths.ts +70 -0
- package/src/host/plugin.ts +1097 -0
- package/src/host/preset.ts +170 -0
- package/src/host/report.ts +71 -0
- package/src/host/restart-exec.ts +81 -0
- package/src/host/restart-guards.ts +150 -0
- package/src/host/restart-tool.ts +483 -0
- package/src/host/restart-web.ts +72 -0
- package/src/host/resume-log.ts +39 -0
- package/src/host/resume-tools.ts +385 -0
- package/src/host/resume.ts +204 -0
- package/src/host/scan.ts +61 -0
- package/src/host/self-kill-guard.ts +216 -0
- package/src/host/session-health.ts +236 -0
- package/src/host/session-log-file.ts +77 -0
- package/src/host/skill-provider.ts +69 -0
- package/src/host/snapshot.ts +337 -0
- package/src/host/source.ts +19 -0
- package/src/host/store/index.ts +348 -0
- package/src/host/store/legacy.ts +96 -0
- package/src/host/supervisor.ts +720 -0
- package/src/host/sync/backup-config.ts +159 -0
- package/src/host/sync/backup-service.ts +453 -0
- package/src/host/sync/bidirectional.ts +66 -0
- package/src/host/sync/cli.ts +539 -0
- package/src/host/sync/config.ts +119 -0
- package/src/host/sync/fingerprint.ts +83 -0
- package/src/host/sync/hashing.ts +58 -0
- package/src/host/sync/index.ts +737 -0
- package/src/host/sync/machine-id.ts +81 -0
- package/src/host/sync/merge.ts +102 -0
- package/src/host/sync/peer-host.ts +65 -0
- package/src/host/sync/process-runner.ts +177 -0
- package/src/host/sync/remote-agent.ts +213 -0
- package/src/host/sync/remote-cache.ts +68 -0
- package/src/host/sync/remote-manifest.ts +70 -0
- package/src/host/sync/s3-object-store.ts +144 -0
- package/src/host/sync/session-merge.ts +220 -0
- package/src/host/sync/session-plan.ts +237 -0
- package/src/host/sync/sigv4.ts +81 -0
- package/src/host/sync/snapshot.ts +162 -0
- package/src/host/sync/ssh-mux.ts +56 -0
- package/src/host/sync/sync-plan.ts +366 -0
- package/src/host/sync/sync-service.ts +855 -0
- package/src/host/sync/sync-types.ts +102 -0
- package/src/host/sync/transport.ts +261 -0
- package/src/host/sync/tunnel-restore.ts +160 -0
- package/src/host/sync/validation.ts +91 -0
- package/src/host/tool-view.ts +98 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type BootLockDeps } from './boot-lock.js';
|
|
2
|
+
/** Boot budget: how long a boot may take before its failures are judged. */
|
|
3
|
+
export declare const DEFAULT_BOOT_GRACE_MS = 180000;
|
|
4
|
+
export interface RestartWebDeps {
|
|
5
|
+
exec?: (cmd: string, opts?: {
|
|
6
|
+
timeout?: number;
|
|
7
|
+
}) => void;
|
|
8
|
+
writeMarker?: (ttlMs?: number) => void;
|
|
9
|
+
serializedRestart?: () => Promise<void>;
|
|
10
|
+
startViaSystemd?: () => void;
|
|
11
|
+
spawnNohup?: () => void | Promise<void>;
|
|
12
|
+
unitExists?: () => boolean;
|
|
13
|
+
bootGraceMs?: number;
|
|
14
|
+
lock?: BootLockDeps;
|
|
15
|
+
}
|
|
16
|
+
export interface RestartWebResult {
|
|
17
|
+
restarted: boolean;
|
|
18
|
+
reason?: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The single implementation of a supervised dsh web restart. Single-flight:
|
|
22
|
+
* boot.lock is held from before the marker is written until the port answers,
|
|
23
|
+
* so a racing tick or a second rollback skips instead of producing the second
|
|
24
|
+
* systemd start that turned the incident's log tail toxic (2026-09-13).
|
|
25
|
+
*/
|
|
26
|
+
export declare function performSingleBootRestart(deps?: RestartWebDeps): Promise<RestartWebResult>;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import * as os from 'node:os';
|
|
2
|
+
import * as path from 'node:path';
|
|
3
|
+
import { buildKillStalePortsCommand, writePlannedRestart } from './restart-guards.js';
|
|
4
|
+
import { serializedSystemdRestart, shouldUseNohupFallback, systemdUnitExists } from './restart-exec.js';
|
|
5
|
+
import { withBootLock } from './boot-lock.js';
|
|
6
|
+
import { resolveDeepseekHarnessDir } from './paths.js';
|
|
7
|
+
/** Boot budget: how long a boot may take before its failures are judged. */
|
|
8
|
+
export const DEFAULT_BOOT_GRACE_MS = 180_000;
|
|
9
|
+
/**
|
|
10
|
+
* The single implementation of a supervised dsh web restart. Single-flight:
|
|
11
|
+
* boot.lock is held from before the marker is written until the port answers,
|
|
12
|
+
* so a racing tick or a second rollback skips instead of producing the second
|
|
13
|
+
* systemd start that turned the incident's log tail toxic (2026-09-13).
|
|
14
|
+
*/
|
|
15
|
+
export async function performSingleBootRestart(deps = {}) {
|
|
16
|
+
const grace = deps.bootGraceMs ?? DEFAULT_BOOT_GRACE_MS;
|
|
17
|
+
const outcome = await withBootLock(async () => {
|
|
18
|
+
// Marker first, and inside the lock: every poll during the boot must know a
|
|
19
|
+
// restart is in flight, and the TTL is the boot budget — not 30 s (D5).
|
|
20
|
+
;
|
|
21
|
+
(deps.writeMarker ?? writePlannedRestart)(grace);
|
|
22
|
+
const { execSync } = await import('node:child_process');
|
|
23
|
+
const exec = deps.exec ?? ((cmd, opts) => {
|
|
24
|
+
execSync(cmd, { timeout: opts?.timeout ?? 15_000, stdio: 'pipe' });
|
|
25
|
+
});
|
|
26
|
+
// A stale MainThread holding :3080/:3082 survives an EADDRINUSE crash with
|
|
27
|
+
// http 200 still served, so the new start would lose the race.
|
|
28
|
+
try {
|
|
29
|
+
exec(buildKillStalePortsCommand(), { timeout: 5000 });
|
|
30
|
+
}
|
|
31
|
+
catch { }
|
|
32
|
+
try {
|
|
33
|
+
await (deps.serializedRestart ?? (() => serializedSystemdRestart()))();
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
catch { }
|
|
37
|
+
try {
|
|
38
|
+
;
|
|
39
|
+
(deps.startViaSystemd ?? (() => exec('systemctl --user start dsh-web.service', { timeout: 15_000 })))();
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
catch { }
|
|
43
|
+
const unitExists = (deps.unitExists ?? systemdUnitExists)();
|
|
44
|
+
if (!shouldUseNohupFallback(unitExists)) {
|
|
45
|
+
throw new Error('systemd manages dsh-web.service but start failed — refusing the direct-node fallback (it would create a second boot)');
|
|
46
|
+
}
|
|
47
|
+
await (deps.spawnNohup ?? defaultNohup)();
|
|
48
|
+
}, deps.lock ?? {});
|
|
49
|
+
if (!outcome.acquired)
|
|
50
|
+
return { restarted: false, reason: 'another boot already holds boot.lock' };
|
|
51
|
+
return { restarted: true };
|
|
52
|
+
}
|
|
53
|
+
async function defaultNohup() {
|
|
54
|
+
const { execSync } = await import('node:child_process');
|
|
55
|
+
const harnessRoot = resolveDeepseekHarnessDir();
|
|
56
|
+
const logPath = path.join(os.homedir(), '.dsh/dsh-web.log');
|
|
57
|
+
execSync(`setsid nohup bash -c 'export NVM_DIR="$HOME/.nvm"; [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"; cd ${JSON.stringify(harnessRoot)} && exec node --import tsx/esm apps/cli/src/bin.ts web --no-open >> ${JSON.stringify(logPath)} 2>&1' &`, { timeout: 5000 });
|
|
58
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Durable out-of-band resume log (`~/.dsh/.supervisor/resume.log.jsonl`).
|
|
3
|
+
*
|
|
4
|
+
* The in-tree supervisor's `ctx.logger` output does not reach `dsh-web.log`
|
|
5
|
+
* (it goes to the per-session console), which made every `agents.resume`
|
|
6
|
+
* failure invisible on real machines — the 2026-09-02 investigation only
|
|
7
|
+
* recovered the pattern ("last successful continue 12:00Z, then 5 restarts
|
|
8
|
+
* with none", `lastResumeProbe: null`, unconsumed intents) by scanning the
|
|
9
|
+
* session logs. This append-only JSONL sidecar gives a machine-local audit
|
|
10
|
+
* trail of resume outcomes that survives no matter where plugin logs land.
|
|
11
|
+
* Never throws: a write failure must not break the resume path.
|
|
12
|
+
*/
|
|
13
|
+
export interface ResumeLogEntry {
|
|
14
|
+
ts: number;
|
|
15
|
+
sessionId?: string;
|
|
16
|
+
/** `resume-retry`: a resume blocked by another owner's write handle, retried. */
|
|
17
|
+
kind: 'scan' | 'resume-failed' | 'resumed' | 'no-agent' | 'resume-retry';
|
|
18
|
+
error?: string;
|
|
19
|
+
detail?: string;
|
|
20
|
+
scanned?: number;
|
|
21
|
+
interrupted?: string[];
|
|
22
|
+
}
|
|
23
|
+
export declare function resumeLogPath(): string;
|
|
24
|
+
export declare function appendResumeLog(entry: ResumeLogEntry): void;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { appendFileSync, mkdirSync, chmodSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { homedir } from 'node:os';
|
|
4
|
+
export function resumeLogPath() {
|
|
5
|
+
return join(homedir(), '.dsh', '.supervisor', 'resume.log.jsonl');
|
|
6
|
+
}
|
|
7
|
+
export function appendResumeLog(entry) {
|
|
8
|
+
try {
|
|
9
|
+
const p = resumeLogPath();
|
|
10
|
+
mkdirSync(join(homedir(), '.dsh', '.supervisor'), { recursive: true });
|
|
11
|
+
appendFileSync(p, JSON.stringify(entry) + '\n', 'utf8');
|
|
12
|
+
try {
|
|
13
|
+
chmodSync(p, 0o600);
|
|
14
|
+
}
|
|
15
|
+
catch { }
|
|
16
|
+
}
|
|
17
|
+
catch { }
|
|
18
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* C2 — deterministic plugin-layer mitigation for resumed sessions that lose
|
|
3
|
+
* core tools after a dsh web restart (`Error: unknown tool "bash"`).
|
|
4
|
+
*
|
|
5
|
+
* The supervisor cannot edit the harness tool layer (hard workspace rule) and
|
|
6
|
+
* the exact layer-rebuild root cause is only observable on the live system
|
|
7
|
+
* (Part D). So when the post-resume probe (C1) reports a core tool missing
|
|
8
|
+
* from the resumed session's SCOPED view, this module runs a host-side
|
|
9
|
+
* mitigation:
|
|
10
|
+
*
|
|
11
|
+
* 1. a short notifier line so the operator sees the loss immediately;
|
|
12
|
+
* 2. a "System:"-prefixed inventory user-message injected into the session
|
|
13
|
+
* (same createUserMessage/followup pattern as the resume-intent message),
|
|
14
|
+
* so the model stops calling the lost tool and uses what remains;
|
|
15
|
+
* 3. optionally (resumeCoreToolPolicy: 'park') records the session id in a
|
|
16
|
+
* module-level parked set exposed through the `maestro_resume_tool_health`
|
|
17
|
+
* host tool + loopback RPC, marking it for manual reopen.
|
|
18
|
+
*
|
|
19
|
+
* The module-level state (`parkedCoreToolLossIds`, `lastResumeProbe`,
|
|
20
|
+
* `resumedSessions`) is per-`dsh web`-process: a host restart clears it, which
|
|
21
|
+
* is exactly right — these are per-boot facts.
|
|
22
|
+
*/
|
|
23
|
+
import { type ToolViewProbe, type ToolViewProbeFn, type ToolScopeResolver, type ToolsLike, type ResumeCoreToolPolicy } from './plugin.js';
|
|
24
|
+
/** Reset the per-process mitigation state — tests + fresh boot reuse. */
|
|
25
|
+
export declare function resetResumeToolHealthState(): void;
|
|
26
|
+
/**
|
|
27
|
+
* The composition outcome of the most recent resumed session. A host with a
|
|
28
|
+
* preset-less agent is the one an operator must act on, so this is reported
|
|
29
|
+
* beside the tool view rather than only journalled.
|
|
30
|
+
*/
|
|
31
|
+
export interface ResumeComposition {
|
|
32
|
+
sessionId: string;
|
|
33
|
+
/** The preset the agent is joined to after verification, or null when it is joined to none. */
|
|
34
|
+
composed: string | null;
|
|
35
|
+
/** Whether the resume path had to re-link the agent to its preset. */
|
|
36
|
+
repaired: boolean;
|
|
37
|
+
/** Repair classification: `already-composed`, `repaired`, `no-preset-recorded`, … */
|
|
38
|
+
reason: string;
|
|
39
|
+
}
|
|
40
|
+
/** Record the composition outcome of one resumed session. */
|
|
41
|
+
export declare function recordResumeComposition(composition: ResumeComposition): void;
|
|
42
|
+
/** Park a session for manual reopen — the operator must reopen it fresh. */
|
|
43
|
+
export declare function parkResumedSession(sessionId: string): void;
|
|
44
|
+
/** Record a session the auto-resume confirmed as resumed (on-demand probe target). */
|
|
45
|
+
export declare function recordResumedSession(sessionId: string): void;
|
|
46
|
+
/** Record the last REAL post-resume tool-view observation ({ missing, visible }). */
|
|
47
|
+
export declare function recordResumeProbe(probe: ToolViewProbe): void;
|
|
48
|
+
/**
|
|
49
|
+
* Operator-facing notify line. The park variant appends the manual-reopen
|
|
50
|
+
* marker so the operator knows the session was NOT auto-continued for tools.
|
|
51
|
+
*/
|
|
52
|
+
export declare function buildCoreToolLossNotifyLine(sessionId: string, missing: string[], policy: ResumeCoreToolPolicy): string;
|
|
53
|
+
/**
|
|
54
|
+
* The SYSTEM message injected into the resumed session. Tells the model the
|
|
55
|
+
* CURRENT inventory (scoped `schemas()` — the tools the session can actually
|
|
56
|
+
* call) so it stops issuing the lost tool instead of looping on unknown-tool
|
|
57
|
+
* errors. Names joined with ', '.
|
|
58
|
+
*/
|
|
59
|
+
export declare function buildToolInventoryMessage(missing: string[], available: string[]): string;
|
|
60
|
+
export interface WarnCoreToolLossDeps {
|
|
61
|
+
/** Loose notifier — default wires to the package notifier (swallows errors). */
|
|
62
|
+
notify?: (line: string) => Promise<void>;
|
|
63
|
+
/** Push a message into the resumed session — default follows the createUserMessage / followup pattern. */
|
|
64
|
+
injectSessionMessage?: (sessionId: string, content: string) => unknown;
|
|
65
|
+
/** Tool registry whose SCOPED schemas() yields the available-tool inventory. Defaults to ctx.tools. */
|
|
66
|
+
tools?: ToolsLike;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* C2 mitigation entry: called by the resume flow when the post-resume probe
|
|
70
|
+
* found a core tool missing. Notifies, injects the inventory message, and —
|
|
71
|
+
* under 'park' policy — records the session id. Only fires on an actual
|
|
72
|
+
* CRITICAL_TOOLS loss; a non-core missing name (e.g. cordis_inspect_query)
|
|
73
|
+
* is a no-op. All seams are injectable for tests; every step is defensive.
|
|
74
|
+
*/
|
|
75
|
+
export declare function warnCoreToolLoss(ctx: any, sessionId: string, scope: unknown, probe: ToolViewProbe, policy: ResumeCoreToolPolicy, opts?: WarnCoreToolLossDeps): Promise<void>;
|
|
76
|
+
export interface ResumeToolHealthSnapshot {
|
|
77
|
+
lastResumeProbe: ToolViewProbe | null;
|
|
78
|
+
lastComposition: ResumeComposition | null;
|
|
79
|
+
parked: string[];
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Build the RPC value. When the tool registry is reachable AND at least one
|
|
83
|
+
* current resumed session still lives, re-probes each session's SCOPED view
|
|
84
|
+
* on demand and records the aggregate as the freshest `lastResumeProbe`;
|
|
85
|
+
* otherwise returns the stored last observation unchanged ('tool registry
|
|
86
|
+
* unreachable -> lastResumeProbe' contract). Session ids whose agent is gone
|
|
87
|
+
* are skipped (they are no longer "current resumed sessions").
|
|
88
|
+
*/
|
|
89
|
+
export declare function snapshotResumeToolHealth(ctx: any, deps?: {
|
|
90
|
+
probeToolView?: ToolViewProbeFn;
|
|
91
|
+
resolveToolScope?: ToolScopeResolver;
|
|
92
|
+
}): ResumeToolHealthSnapshot;
|
|
93
|
+
/**
|
|
94
|
+
* Loopback RPC handler for /dsh-maestro-supervisor-resume-tool-health.
|
|
95
|
+
* Same `{ ok, value | error }` envelope shape as the other supervisor RPCs.
|
|
96
|
+
*/
|
|
97
|
+
export declare function createResumeToolHealthRpcHandler(ctx: any, deps?: {
|
|
98
|
+
probeToolView?: ToolViewProbeFn;
|
|
99
|
+
resolveToolScope?: ToolScopeResolver;
|
|
100
|
+
}): (_endpoint: string, _payload: unknown, _signal: AbortSignal) => Promise<{
|
|
101
|
+
ok: boolean;
|
|
102
|
+
value: ResumeToolHealthSnapshot;
|
|
103
|
+
error?: undefined;
|
|
104
|
+
} | {
|
|
105
|
+
ok: boolean;
|
|
106
|
+
error: {
|
|
107
|
+
code: string;
|
|
108
|
+
message: any;
|
|
109
|
+
};
|
|
110
|
+
value?: undefined;
|
|
111
|
+
}>;
|
|
112
|
+
/** dsh.tools definition for the maestro_resume_tool_health host tool. */
|
|
113
|
+
export declare function makeResumeToolHealthToolDef(ctx: any): any;
|
|
114
|
+
/**
|
|
115
|
+
* dsh.tools definition for the `maestro_repair_session_preset` host tool.
|
|
116
|
+
*
|
|
117
|
+
* The resume path repairs automatically; this tool is the operator's manual
|
|
118
|
+
* handle on the same capability: an agent that already lost its preset (a
|
|
119
|
+
* session resumed before this fix existed, for example) can be re-linked without
|
|
120
|
+
* waiting for the next restart.
|
|
121
|
+
* @param ctx - plugin context providing `agents`, `agentPresets` and the session readers.
|
|
122
|
+
* @returns the tool definition handed to `ctx.tools.register`.
|
|
123
|
+
*/
|
|
124
|
+
export declare function makeRepairPresetToolDef(ctx: any): any;
|
|
125
|
+
/**
|
|
126
|
+
* Register the resume-tool-health RPC handle and the
|
|
127
|
+
* maestro_resume_tool_health host tool. Fail-safe like the other
|
|
128
|
+
* registrations: any registration error is logged, never thrown, and the
|
|
129
|
+
* returned disposer unregisters everything that did succeed.
|
|
130
|
+
*/
|
|
131
|
+
export declare function registerResumeToolHealthService(ctx: any): () => void;
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* C2 — deterministic plugin-layer mitigation for resumed sessions that lose
|
|
3
|
+
* core tools after a dsh web restart (`Error: unknown tool "bash"`).
|
|
4
|
+
*
|
|
5
|
+
* The supervisor cannot edit the harness tool layer (hard workspace rule) and
|
|
6
|
+
* the exact layer-rebuild root cause is only observable on the live system
|
|
7
|
+
* (Part D). So when the post-resume probe (C1) reports a core tool missing
|
|
8
|
+
* from the resumed session's SCOPED view, this module runs a host-side
|
|
9
|
+
* mitigation:
|
|
10
|
+
*
|
|
11
|
+
* 1. a short notifier line so the operator sees the loss immediately;
|
|
12
|
+
* 2. a "System:"-prefixed inventory user-message injected into the session
|
|
13
|
+
* (same createUserMessage/followup pattern as the resume-intent message),
|
|
14
|
+
* so the model stops calling the lost tool and uses what remains;
|
|
15
|
+
* 3. optionally (resumeCoreToolPolicy: 'park') records the session id in a
|
|
16
|
+
* module-level parked set exposed through the `maestro_resume_tool_health`
|
|
17
|
+
* host tool + loopback RPC, marking it for manual reopen.
|
|
18
|
+
*
|
|
19
|
+
* The module-level state (`parkedCoreToolLossIds`, `lastResumeProbe`,
|
|
20
|
+
* `resumedSessions`) is per-`dsh web`-process: a host restart clears it, which
|
|
21
|
+
* is exactly right — these are per-boot facts.
|
|
22
|
+
*/
|
|
23
|
+
import { notify } from './notifier.js';
|
|
24
|
+
import { repairAgentPreset } from './preset.js';
|
|
25
|
+
import { SUPERVISOR_SOURCE_KIND } from './source.js';
|
|
26
|
+
import { probeToolView, defaultResolveToolScope, CRITICAL_TOOLS, } from './plugin.js';
|
|
27
|
+
// --- module-level mitigation state ---------------------------------------------
|
|
28
|
+
let lastResumeProbe = null;
|
|
29
|
+
let lastComposition = null;
|
|
30
|
+
const parkedCoreToolLossIds = new Set();
|
|
31
|
+
const resumedSessions = new Set();
|
|
32
|
+
/** Reset the per-process mitigation state — tests + fresh boot reuse. */
|
|
33
|
+
export function resetResumeToolHealthState() {
|
|
34
|
+
lastResumeProbe = null;
|
|
35
|
+
lastComposition = null;
|
|
36
|
+
parkedCoreToolLossIds.clear();
|
|
37
|
+
resumedSessions.clear();
|
|
38
|
+
}
|
|
39
|
+
/** Record the composition outcome of one resumed session. */
|
|
40
|
+
export function recordResumeComposition(composition) {
|
|
41
|
+
lastComposition = composition;
|
|
42
|
+
}
|
|
43
|
+
/** Park a session for manual reopen — the operator must reopen it fresh. */
|
|
44
|
+
export function parkResumedSession(sessionId) {
|
|
45
|
+
parkedCoreToolLossIds.add(sessionId);
|
|
46
|
+
}
|
|
47
|
+
/** Record a session the auto-resume confirmed as resumed (on-demand probe target). */
|
|
48
|
+
export function recordResumedSession(sessionId) {
|
|
49
|
+
resumedSessions.add(sessionId);
|
|
50
|
+
}
|
|
51
|
+
/** Record the last REAL post-resume tool-view observation ({ missing, visible }). */
|
|
52
|
+
export function recordResumeProbe(probe) {
|
|
53
|
+
lastResumeProbe = probe;
|
|
54
|
+
}
|
|
55
|
+
// --- message builders -----------------------------------------------------------
|
|
56
|
+
/**
|
|
57
|
+
* Operator-facing notify line. The park variant appends the manual-reopen
|
|
58
|
+
* marker so the operator knows the session was NOT auto-continued for tools.
|
|
59
|
+
*/
|
|
60
|
+
export function buildCoreToolLossNotifyLine(sessionId, missing, policy) {
|
|
61
|
+
const base = `[supervisor] resumed ${sessionId}: core tool lost [${missing.join(',')}] — tool view incomplete; reopen session if it persists`;
|
|
62
|
+
return policy === 'park' ? `${base} (manual reopen required)` : base;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The SYSTEM message injected into the resumed session. Tells the model the
|
|
66
|
+
* CURRENT inventory (scoped `schemas()` — the tools the session can actually
|
|
67
|
+
* call) so it stops issuing the lost tool instead of looping on unknown-tool
|
|
68
|
+
* errors. Names joined with ', '.
|
|
69
|
+
*/
|
|
70
|
+
export function buildToolInventoryMessage(missing, available) {
|
|
71
|
+
const names = missing.join(', ');
|
|
72
|
+
const verb = missing.length === 1 ? 'tool is' : 'tools are';
|
|
73
|
+
return `System: the ${names} ${verb} unavailable in this session's restored tool view. Available tools: ${available.join(', ')}. Do not call ${names}; use the available tools.`;
|
|
74
|
+
}
|
|
75
|
+
/** Scoped schemas() = exactly what the session can still call; never the global view. */
|
|
76
|
+
function resolveAvailableToolNames(tools, scope) {
|
|
77
|
+
try {
|
|
78
|
+
const schemas = typeof tools?.schemas === 'function' ? tools.schemas : undefined;
|
|
79
|
+
if (!schemas)
|
|
80
|
+
return [];
|
|
81
|
+
const list = schemas(scope);
|
|
82
|
+
if (!Array.isArray(list))
|
|
83
|
+
return [];
|
|
84
|
+
return list.map((t) => (t && typeof t.name === 'string' ? t.name : '')).filter((n) => n.length > 0);
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
return [];
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Default session-message injection: mirrors the resume-intent push in
|
|
92
|
+
* plugin.ts `resumeInterrupted` — `createUserMessage` (dynamic import so the
|
|
93
|
+
* plugin loads without the LLM package ever installed) + `agent.followup`.
|
|
94
|
+
* Never throws; a missing agent just no-ops.
|
|
95
|
+
*/
|
|
96
|
+
function makeDefaultSessionMessageInjector(ctx) {
|
|
97
|
+
return async (sessionId, content) => {
|
|
98
|
+
try {
|
|
99
|
+
const agents = ctx.get?.('agents') ?? ctx.agents;
|
|
100
|
+
const agent = typeof agents?.get === 'function' ? agents.get(sessionId) : undefined;
|
|
101
|
+
if (typeof agent?.followup !== 'function')
|
|
102
|
+
return;
|
|
103
|
+
const { createUserMessage } = await import('@deepseek-ai/dsh-llm').catch(() => ({
|
|
104
|
+
createUserMessage: (input) => ({ ...input, role: 'user', id: crypto.randomUUID() }),
|
|
105
|
+
}));
|
|
106
|
+
agent.followup(createUserMessage({
|
|
107
|
+
content: [{ type: 'text', text: content }],
|
|
108
|
+
source: { kind: SUPERVISOR_SOURCE_KIND },
|
|
109
|
+
}));
|
|
110
|
+
}
|
|
111
|
+
catch { }
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* C2 mitigation entry: called by the resume flow when the post-resume probe
|
|
116
|
+
* found a core tool missing. Notifies, injects the inventory message, and —
|
|
117
|
+
* under 'park' policy — records the session id. Only fires on an actual
|
|
118
|
+
* CRITICAL_TOOLS loss; a non-core missing name (e.g. cordis_inspect_query)
|
|
119
|
+
* is a no-op. All seams are injectable for tests; every step is defensive.
|
|
120
|
+
*/
|
|
121
|
+
export async function warnCoreToolLoss(ctx, sessionId, scope, probe, policy, opts = {}) {
|
|
122
|
+
const lostCore = probe.missing.filter((n) => CRITICAL_TOOLS.includes(n));
|
|
123
|
+
if (!lostCore.length)
|
|
124
|
+
return;
|
|
125
|
+
const doNotify = opts.notify ?? ((line) => notify(line));
|
|
126
|
+
try {
|
|
127
|
+
await doNotify(buildCoreToolLossNotifyLine(sessionId, lostCore, policy));
|
|
128
|
+
}
|
|
129
|
+
catch { }
|
|
130
|
+
try {
|
|
131
|
+
const tools = opts.tools ?? (ctx.get?.('tools') ?? ctx.tools);
|
|
132
|
+
const doInject = opts.injectSessionMessage ?? makeDefaultSessionMessageInjector(ctx);
|
|
133
|
+
await doInject(sessionId, buildToolInventoryMessage(lostCore, resolveAvailableToolNames(tools, scope)));
|
|
134
|
+
}
|
|
135
|
+
catch { }
|
|
136
|
+
if (policy === 'park')
|
|
137
|
+
parkedCoreToolLossIds.add(sessionId);
|
|
138
|
+
}
|
|
139
|
+
function mergeResumeProbes(base, next) {
|
|
140
|
+
if (!base)
|
|
141
|
+
return next;
|
|
142
|
+
return {
|
|
143
|
+
missing: Array.from(new Set([...base.missing, ...next.missing])),
|
|
144
|
+
visible: base.visible + next.visible,
|
|
145
|
+
registry: base.registry === 'reachable' && next.registry === 'reachable' ? 'reachable' : 'unreachable',
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Build the RPC value. When the tool registry is reachable AND at least one
|
|
150
|
+
* current resumed session still lives, re-probes each session's SCOPED view
|
|
151
|
+
* on demand and records the aggregate as the freshest `lastResumeProbe`;
|
|
152
|
+
* otherwise returns the stored last observation unchanged ('tool registry
|
|
153
|
+
* unreachable -> lastResumeProbe' contract). Session ids whose agent is gone
|
|
154
|
+
* are skipped (they are no longer "current resumed sessions").
|
|
155
|
+
*/
|
|
156
|
+
export function snapshotResumeToolHealth(ctx, deps = {}) {
|
|
157
|
+
const doProbe = deps.probeToolView ?? probeToolView;
|
|
158
|
+
const doResolve = deps.resolveToolScope ?? defaultResolveToolScope;
|
|
159
|
+
const tools = ctx.get?.('tools') ?? ctx.tools;
|
|
160
|
+
let reachable = false;
|
|
161
|
+
let aggregated = null;
|
|
162
|
+
for (const id of resumedSessions) {
|
|
163
|
+
try {
|
|
164
|
+
const agents = ctx.get?.('agents') ?? ctx.agents;
|
|
165
|
+
if (typeof agents?.get === 'function' && agents.get(id) === undefined)
|
|
166
|
+
continue;
|
|
167
|
+
aggregated = mergeResumeProbes(aggregated, doProbe(tools, doResolve(ctx, id)));
|
|
168
|
+
reachable = true;
|
|
169
|
+
}
|
|
170
|
+
catch { }
|
|
171
|
+
}
|
|
172
|
+
if (reachable && aggregated)
|
|
173
|
+
lastResumeProbe = aggregated;
|
|
174
|
+
return { lastResumeProbe, lastComposition, parked: [...parkedCoreToolLossIds] };
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Loopback RPC handler for /dsh-maestro-supervisor-resume-tool-health.
|
|
178
|
+
* Same `{ ok, value | error }` envelope shape as the other supervisor RPCs.
|
|
179
|
+
*/
|
|
180
|
+
export function createResumeToolHealthRpcHandler(ctx, deps = {}) {
|
|
181
|
+
return async (_endpoint, _payload, _signal) => {
|
|
182
|
+
try {
|
|
183
|
+
return { ok: true, value: snapshotResumeToolHealth(ctx, deps) };
|
|
184
|
+
}
|
|
185
|
+
catch (e) {
|
|
186
|
+
return { ok: false, error: { code: 'resume-tool-health-failed', message: e?.message ?? String(e) } };
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
/** dsh.tools definition for the maestro_resume_tool_health host tool. */
|
|
191
|
+
export function makeResumeToolHealthToolDef(ctx) {
|
|
192
|
+
return {
|
|
193
|
+
name: 'maestro_resume_tool_health',
|
|
194
|
+
description: 'Report the tool-view health of sessions auto-resumed after a dsh web restart. ' +
|
|
195
|
+
'Returns the last post-resume probe (missing core tools, visible count) plus the sessions ' +
|
|
196
|
+
'parked for manual reopen under resumeCoreToolPolicy: park. The probe is refreshed on demand ' +
|
|
197
|
+
'against the currently resumed sessions when the tool registry is reachable.',
|
|
198
|
+
parameters: { type: 'object', properties: {}, additionalProperties: false },
|
|
199
|
+
output: {
|
|
200
|
+
schema: {
|
|
201
|
+
type: 'object',
|
|
202
|
+
additionalProperties: true,
|
|
203
|
+
properties: {
|
|
204
|
+
// Null until the first probe of this process (a fresh boot has none),
|
|
205
|
+
// which the validator accepts only as a oneOf branch — it rejects
|
|
206
|
+
// `type` arrays and refuses `type` beside `oneOf`.
|
|
207
|
+
lastResumeProbe: {
|
|
208
|
+
oneOf: [
|
|
209
|
+
{
|
|
210
|
+
type: 'object',
|
|
211
|
+
properties: {
|
|
212
|
+
missing: { type: 'array', items: { type: 'string' } },
|
|
213
|
+
visible: { type: 'number' },
|
|
214
|
+
},
|
|
215
|
+
},
|
|
216
|
+
{ type: 'null' },
|
|
217
|
+
],
|
|
218
|
+
},
|
|
219
|
+
parked: { type: 'array', items: { type: 'string' } },
|
|
220
|
+
},
|
|
221
|
+
},
|
|
222
|
+
render: (_args, value) => {
|
|
223
|
+
const probe = value?.lastResumeProbe;
|
|
224
|
+
const missing = Array.isArray(probe?.missing) && probe.missing.length ? probe.missing.join(',') : 'none';
|
|
225
|
+
const visible = typeof probe?.visible === 'number' ? probe.visible : 'n/a';
|
|
226
|
+
const registry = typeof probe?.registry === 'string' ? probe.registry : 'n/a';
|
|
227
|
+
const composition = value?.lastComposition;
|
|
228
|
+
const composed = composition === null || composition === undefined ? 'unknown' : (composition.composed ?? 'none');
|
|
229
|
+
const repaired = composition?.repaired === true ? 'yes' : 'no';
|
|
230
|
+
return [{
|
|
231
|
+
type: 'text',
|
|
232
|
+
text: `missing=${missing} visible=${visible} registry=${registry} composed=${composed} repaired=${repaired} parked=${Array.isArray(value?.parked) ? value.parked.length : 0}`,
|
|
233
|
+
}];
|
|
234
|
+
},
|
|
235
|
+
},
|
|
236
|
+
execute: async () => snapshotResumeToolHealth(ctx),
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* dsh.tools definition for the `maestro_repair_session_preset` host tool.
|
|
241
|
+
*
|
|
242
|
+
* The resume path repairs automatically; this tool is the operator's manual
|
|
243
|
+
* handle on the same capability: an agent that already lost its preset (a
|
|
244
|
+
* session resumed before this fix existed, for example) can be re-linked without
|
|
245
|
+
* waiting for the next restart.
|
|
246
|
+
* @param ctx - plugin context providing `agents`, `agentPresets` and the session readers.
|
|
247
|
+
* @returns the tool definition handed to `ctx.tools.register`.
|
|
248
|
+
*/
|
|
249
|
+
export function makeRepairPresetToolDef(ctx) {
|
|
250
|
+
return {
|
|
251
|
+
name: 'maestro_repair_session_preset',
|
|
252
|
+
description: "Re-link one live session's agent to the agent preset it records. Use when a resumed session lost its " +
|
|
253
|
+
'tools (every preset tool answers UNKNOWN_TOOL): the agent was published without joining a preset, and ' +
|
|
254
|
+
'recompose restores the full tool set for its next request. Reports whether the agent was repaired, ' +
|
|
255
|
+
'already composed, or could not be (no recorded preset, no live agent, recompose failed).',
|
|
256
|
+
parameters: {
|
|
257
|
+
type: 'object',
|
|
258
|
+
properties: {
|
|
259
|
+
sessionId: { type: 'string', description: 'The session whose live agent should be re-linked to its preset.' },
|
|
260
|
+
},
|
|
261
|
+
required: ['sessionId'],
|
|
262
|
+
additionalProperties: false,
|
|
263
|
+
},
|
|
264
|
+
output: {
|
|
265
|
+
schema: {
|
|
266
|
+
type: 'object',
|
|
267
|
+
additionalProperties: true,
|
|
268
|
+
properties: {
|
|
269
|
+
repaired: { type: 'boolean' },
|
|
270
|
+
presetId: { type: 'string' },
|
|
271
|
+
reason: { type: 'string' },
|
|
272
|
+
composedAfter: { type: 'string' },
|
|
273
|
+
},
|
|
274
|
+
},
|
|
275
|
+
render: (_args, value) => [{
|
|
276
|
+
type: 'text',
|
|
277
|
+
text: `repaired=${value?.repaired === true ? 'yes' : 'no'} reason=${value?.reason ?? 'unknown'} preset=${value?.presetId ?? 'none'}`,
|
|
278
|
+
}],
|
|
279
|
+
},
|
|
280
|
+
execute: async (args) => {
|
|
281
|
+
const sessionId = typeof args?.sessionId === 'string' ? args.sessionId : '';
|
|
282
|
+
if (!sessionId)
|
|
283
|
+
return { ok: false, repaired: false, reason: 'no-preset-recorded' };
|
|
284
|
+
const outcome = await repairAgentPreset(ctx, sessionId);
|
|
285
|
+
return { ok: true, ...outcome };
|
|
286
|
+
},
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Register the resume-tool-health RPC handle and the
|
|
291
|
+
* maestro_resume_tool_health host tool. Fail-safe like the other
|
|
292
|
+
* registrations: any registration error is logged, never thrown, and the
|
|
293
|
+
* returned disposer unregisters everything that did succeed.
|
|
294
|
+
*/
|
|
295
|
+
export function registerResumeToolHealthService(ctx) {
|
|
296
|
+
const disposers = [];
|
|
297
|
+
try {
|
|
298
|
+
const conn = ctx.connection ?? ctx.get?.('connection');
|
|
299
|
+
if (conn?.rpc?.handle) {
|
|
300
|
+
disposers.push(conn.rpc.handle('/dsh-maestro-supervisor-resume-tool-health', createResumeToolHealthRpcHandler(ctx)));
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
catch (e) {
|
|
304
|
+
try {
|
|
305
|
+
ctx.logger?.warn?.(`[supervisor] resume-tool-health RPC registration failed: ${e?.message ?? String(e)}`);
|
|
306
|
+
}
|
|
307
|
+
catch { }
|
|
308
|
+
}
|
|
309
|
+
try {
|
|
310
|
+
if (typeof ctx.tools?.register === 'function') {
|
|
311
|
+
disposers.push(ctx.tools.register(makeResumeToolHealthToolDef(ctx)));
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
catch (e) {
|
|
315
|
+
try {
|
|
316
|
+
ctx.logger?.warn?.(`[supervisor] resume-tool-health tool registration failed: ${e?.message ?? String(e)}`);
|
|
317
|
+
}
|
|
318
|
+
catch { }
|
|
319
|
+
}
|
|
320
|
+
try {
|
|
321
|
+
if (typeof ctx.tools?.register === 'function') {
|
|
322
|
+
disposers.push(ctx.tools.register(makeRepairPresetToolDef(ctx)));
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
catch (e) {
|
|
326
|
+
try {
|
|
327
|
+
ctx.logger?.warn?.(`[supervisor] repair-preset tool registration failed: ${e?.message ?? String(e)}`);
|
|
328
|
+
}
|
|
329
|
+
catch { }
|
|
330
|
+
}
|
|
331
|
+
return () => { for (const d of disposers) {
|
|
332
|
+
try {
|
|
333
|
+
d();
|
|
334
|
+
}
|
|
335
|
+
catch { }
|
|
336
|
+
} };
|
|
337
|
+
}
|
package/lib/resume.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { resolveSessionLogPath } from './session-log-file.js';
|
|
2
|
+
export interface ResumeResult {
|
|
3
|
+
scanned: number;
|
|
4
|
+
interrupted: string[];
|
|
5
|
+
}
|
|
6
|
+
export interface FindInterruptedOpts {
|
|
7
|
+
withinMs?: number;
|
|
8
|
+
sinceMs?: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Resolve the readable session log inside one session directory. Owned by
|
|
12
|
+
* `session-log-file.ts` — the single owner of "which file is the log":
|
|
13
|
+
* generations are discovered, never listed, because enumerating them went
|
|
14
|
+
* blind twice (v3 on 2026-09-11, v4 on 2026-09-23). Re-exported here because
|
|
15
|
+
* `plugin.ts` and `preset.ts` already import it from this module.
|
|
16
|
+
*/
|
|
17
|
+
export { resolveSessionLogPath };
|
|
18
|
+
export declare function findInterrupted(dshHome?: string, opts?: FindInterruptedOpts): Promise<ResumeResult>;
|
|
19
|
+
/**
|
|
20
|
+
* Detect sessions whose raw log ends with a `turn/start` that has no
|
|
21
|
+
* matching `turn/end` anywhere later in the scanned log — a dangling open
|
|
22
|
+
* turn. Unlike {@link findInterrupted}, this needs no prior `persistence.load()`
|
|
23
|
+
* call to have already synthesized a `turn/end interrupted` closer (DSH core
|
|
24
|
+
* only writes that closer when something loads/prepares the specific
|
|
25
|
+
* session — a genuinely fresh crash's raw log has no closer at all, only an
|
|
26
|
+
* open turn). Safe to call ONLY right after a fresh `dsh web` boot, when
|
|
27
|
+
* `dsh web` is the sole live process for these sessions — an open turn found
|
|
28
|
+
* at that moment cannot belong to a still-running generation anywhere else.
|
|
29
|
+
* Callers MUST additionally skip any id that is live in their own current
|
|
30
|
+
* process (e.g. `ctx.sessions.get(id)`) before treating a match as crashed.
|
|
31
|
+
*/
|
|
32
|
+
export declare function findDanglingOpenTurns(dshHome?: string, opts?: FindInterruptedOpts): Promise<ResumeResult>;
|
|
33
|
+
export declare function parseDuration(s: string): number | undefined;
|