@0xmaxma/claude-gateway 1.3.25 → 1.3.31
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/README.md +47 -1
- package/config.template.json +6 -2
- package/dist/agent/incident-store.d.ts +89 -0
- package/dist/agent/incident-store.d.ts.map +1 -0
- package/dist/agent/incident-store.js +299 -0
- package/dist/agent/incident-store.js.map +1 -0
- package/dist/agent/incident.d.ts +156 -0
- package/dist/agent/incident.d.ts.map +1 -0
- package/dist/agent/incident.js +177 -0
- package/dist/agent/incident.js.map +1 -0
- package/dist/agent/recovery-executor.d.ts +117 -0
- package/dist/agent/recovery-executor.d.ts.map +1 -0
- package/dist/agent/recovery-executor.js +168 -0
- package/dist/agent/recovery-executor.js.map +1 -0
- package/dist/agent/recovery-policy.d.ts +97 -0
- package/dist/agent/recovery-policy.d.ts.map +1 -0
- package/dist/agent/recovery-policy.js +164 -0
- package/dist/agent/recovery-policy.js.map +1 -0
- package/dist/agent/runner.d.ts +44 -0
- package/dist/agent/runner.d.ts.map +1 -1
- package/dist/agent/runner.js +272 -2
- package/dist/agent/runner.js.map +1 -1
- package/dist/agent/safe-mode.d.ts +61 -0
- package/dist/agent/safe-mode.d.ts.map +1 -0
- package/dist/agent/safe-mode.js +102 -0
- package/dist/agent/safe-mode.js.map +1 -0
- package/dist/agent/triage.d.ts +94 -0
- package/dist/agent/triage.d.ts.map +1 -0
- package/dist/agent/triage.js +209 -0
- package/dist/agent/triage.js.map +1 -0
- package/dist/agent/turn-trace.d.ts +120 -0
- package/dist/agent/turn-trace.d.ts.map +1 -0
- package/dist/agent/turn-trace.js +122 -0
- package/dist/agent/turn-trace.js.map +1 -0
- package/dist/api/gateway-router.d.ts +21 -0
- package/dist/api/gateway-router.d.ts.map +1 -1
- package/dist/api/gateway-router.js +58 -17
- package/dist/api/gateway-router.js.map +1 -1
- package/dist/config/migrator.d.ts +4 -0
- package/dist/config/migrator.d.ts.map +1 -1
- package/dist/config/migrator.js +60 -3
- package/dist/config/migrator.js.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/session/process.d.ts +18 -0
- package/dist/session/process.d.ts.map +1 -1
- package/dist/session/process.js +61 -1
- package/dist/session/process.js.map +1 -1
- package/dist/shell/claude-pty-shell.js +59 -0
- package/dist/shell/claude-pty-shell.js.map +1 -1
- package/dist/shell/control-channel.d.ts +74 -0
- package/dist/shell/control-channel.d.ts.map +1 -0
- package/dist/shell/control-channel.js +114 -0
- package/dist/shell/control-channel.js.map +1 -0
- package/dist/types.d.ts +21 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/ui/web-ui.d.ts.map +1 -1
- package/dist/ui/web-ui.js +103 -2
- package/dist/ui/web-ui.js.map +1 -1
- package/mcp/tools/skills/handlers.ts +4 -2
- package/mcp/tools/telegram/receiver-server.ts +163 -0
- package/mcp/tools/telegram/typing.ts +124 -0
- package/package.json +3 -1
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Incident core (Epic #195, Phase 2).
|
|
4
|
+
*
|
|
5
|
+
* Pure decision logic for the incident-reporting layer that sits on top of the
|
|
6
|
+
* turn-trace watchdog (Phase 1). Given a stall incident, this module decides:
|
|
7
|
+
* - its fingerprint (what makes two stalls "the same problem"),
|
|
8
|
+
* - whether/how to escalate a repeat (quiet → repeat → recommend-investigate),
|
|
9
|
+
* - how to scrub evidence before any of it can leave the machine,
|
|
10
|
+
* - how to summarise a set of incidents into a digest line/report.
|
|
11
|
+
*
|
|
12
|
+
* Like turn-trace.ts and orphan-wake.ts, this file performs NO IO and imports
|
|
13
|
+
* no runtime code (only types, which are erased), so every rule here is
|
|
14
|
+
* unit-testable without a filesystem, a clock, or a live session. The store
|
|
15
|
+
* (incident-store.ts) owns persistence and calls into these functions.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.DEFAULT_ESCALATION = exports.REDACTION = void 0;
|
|
19
|
+
exports.computeFingerprint = computeFingerprint;
|
|
20
|
+
exports.fingerprintHash = fingerprintHash;
|
|
21
|
+
exports.decideEscalation = decideEscalation;
|
|
22
|
+
exports.maxEscalationLevel = maxEscalationLevel;
|
|
23
|
+
exports.scrubText = scrubText;
|
|
24
|
+
exports.summarizeIncidents = summarizeIncidents;
|
|
25
|
+
exports.formatDigestLine = formatDigestLine;
|
|
26
|
+
/** Placeholder substituted for any redacted span in a scrubbed export. */
|
|
27
|
+
exports.REDACTION = '‹redacted›';
|
|
28
|
+
/**
|
|
29
|
+
* Compute the dedup fingerprint. Two stalls are "the same problem" when they
|
|
30
|
+
* hit the same pipeline stage with the same failure attribution on the same CLI
|
|
31
|
+
* version — a new CLI version is treated as a distinct problem so a regression
|
|
32
|
+
* introduced by an upgrade does not silently fold into an old fingerprint.
|
|
33
|
+
*/
|
|
34
|
+
function computeFingerprint(input) {
|
|
35
|
+
const cls = input.failureClass ?? 'none';
|
|
36
|
+
const ver = normalizeVersion(input.cliVersion);
|
|
37
|
+
return `${input.stage}:${cls}:${ver}`;
|
|
38
|
+
}
|
|
39
|
+
/** Normalise a version string to a fingerprint-safe token; empty → 'unknown'. */
|
|
40
|
+
function normalizeVersion(v) {
|
|
41
|
+
const t = (v ?? '').trim();
|
|
42
|
+
return t.length > 0 ? t : 'unknown';
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Deterministic, dependency-free hash (FNV-1a → base36). Used to derive a short
|
|
46
|
+
* filesystem-safe prefix for an incident id from its fingerprint. Not used for
|
|
47
|
+
* anything security-sensitive — only stable naming/dedup.
|
|
48
|
+
*/
|
|
49
|
+
function fingerprintHash(fingerprint) {
|
|
50
|
+
let h = 0x811c9dc5;
|
|
51
|
+
for (let i = 0; i < fingerprint.length; i++) {
|
|
52
|
+
h ^= fingerprint.charCodeAt(i);
|
|
53
|
+
// FNV prime multiply, kept in 32-bit range via Math.imul.
|
|
54
|
+
h = Math.imul(h, 0x01000193);
|
|
55
|
+
}
|
|
56
|
+
// >>> 0 to interpret as unsigned before stringifying.
|
|
57
|
+
return (h >>> 0).toString(36);
|
|
58
|
+
}
|
|
59
|
+
exports.DEFAULT_ESCALATION = {
|
|
60
|
+
windowMs: 24 * 60 * 60 * 1000, // 24 h
|
|
61
|
+
investigateThreshold: 3,
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Decide how to escalate given the running occurrence count for a fingerprint
|
|
65
|
+
* and whether the user was already notified at 'investigate' level.
|
|
66
|
+
*
|
|
67
|
+
* - 1st occurrence → quiet notify (one short heads-up)
|
|
68
|
+
* - repeats below threshold → silent increment (no re-notify)
|
|
69
|
+
* - Nth (N ≥ threshold), once → recommend investigation (louder notify)
|
|
70
|
+
* - after that → silent (already recommended)
|
|
71
|
+
*
|
|
72
|
+
* Pure: the caller supplies the count and prior-notify state.
|
|
73
|
+
*/
|
|
74
|
+
function decideEscalation(occurrences, alreadyInvestigateNotified, cfg = exports.DEFAULT_ESCALATION) {
|
|
75
|
+
if (occurrences <= 1) {
|
|
76
|
+
return { level: 'quiet', notify: true };
|
|
77
|
+
}
|
|
78
|
+
if (occurrences >= cfg.investigateThreshold && !alreadyInvestigateNotified) {
|
|
79
|
+
return { level: 'investigate', notify: true };
|
|
80
|
+
}
|
|
81
|
+
return { level: 'repeat', notify: false };
|
|
82
|
+
}
|
|
83
|
+
/** The maximum of two escalation levels (for tracking the highest reached). */
|
|
84
|
+
function maxEscalationLevel(a, b) {
|
|
85
|
+
const rank = { quiet: 0, repeat: 1, investigate: 2 };
|
|
86
|
+
return rank[a] >= rank[b] ? a : b;
|
|
87
|
+
}
|
|
88
|
+
// ─── Scrubbing ──────────────────────────────────────────────────────────────
|
|
89
|
+
/**
|
|
90
|
+
* Patterns for content that must never leave the machine, independent of any
|
|
91
|
+
* caller-supplied literals. Conservative by design: each pattern targets a
|
|
92
|
+
* recognisable secret/PII shape, not free text, to avoid mangling useful
|
|
93
|
+
* diagnostic content. Order matters — more specific patterns run first.
|
|
94
|
+
*/
|
|
95
|
+
const SECRET_PATTERNS = [
|
|
96
|
+
// Telegram bot token: <digits>:<35+ token chars>
|
|
97
|
+
/\b\d{6,}:[A-Za-z0-9_-]{30,}\b/g,
|
|
98
|
+
// Anthropic / OpenAI style keys: sk-... (and sk-ant-...)
|
|
99
|
+
/\bsk-[A-Za-z0-9-]{16,}\b/g,
|
|
100
|
+
// Slack tokens: xox[baprs]-...
|
|
101
|
+
/\bxox[baprs]-[A-Za-z0-9-]{10,}\b/g,
|
|
102
|
+
// GitHub tokens: ghp_/gho_/ghu_/ghs_/ghr_ + 36 chars
|
|
103
|
+
/\bgh[pousr]_[A-Za-z0-9]{20,}\b/g,
|
|
104
|
+
// Bearer tokens in headers
|
|
105
|
+
/\bBearer\s+[A-Za-z0-9._-]{16,}\b/gi,
|
|
106
|
+
// Email addresses
|
|
107
|
+
/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/g,
|
|
108
|
+
];
|
|
109
|
+
/**
|
|
110
|
+
* Scrub text destined for an exported/persisted artifact. Removes:
|
|
111
|
+
* 1. every caller-supplied literal (chat ids, usernames) — matched verbatim,
|
|
112
|
+
* 2. recognisable secret/PII shapes (tokens, keys, emails).
|
|
113
|
+
*
|
|
114
|
+
* Literals are escaped before use so a value like `a.b` cannot act as a regex.
|
|
115
|
+
* Pure and idempotent enough for repeated application (placeholder is inert).
|
|
116
|
+
*/
|
|
117
|
+
function scrubText(text, redactions = []) {
|
|
118
|
+
if (!text)
|
|
119
|
+
return text;
|
|
120
|
+
let out = text;
|
|
121
|
+
// Caller literals first (longest first so a longer id containing a shorter
|
|
122
|
+
// one is fully removed rather than partially).
|
|
123
|
+
const literals = [...new Set(redactions.filter((r) => r && r.length >= 2))].sort((a, b) => b.length - a.length);
|
|
124
|
+
for (const lit of literals) {
|
|
125
|
+
out = out.split(lit).join(exports.REDACTION);
|
|
126
|
+
}
|
|
127
|
+
for (const re of SECRET_PATTERNS) {
|
|
128
|
+
out = out.replace(re, exports.REDACTION);
|
|
129
|
+
}
|
|
130
|
+
return out;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Summarise the incidents whose most-recent occurrence lands in
|
|
134
|
+
* `[now - sinceMs, now]`. Pure — the store supplies the manifests it read.
|
|
135
|
+
*/
|
|
136
|
+
function summarizeIncidents(manifests, sinceMs, now) {
|
|
137
|
+
const cutoff = now - sinceMs;
|
|
138
|
+
const summary = {
|
|
139
|
+
total: 0,
|
|
140
|
+
occurrences: 0,
|
|
141
|
+
openCount: 0,
|
|
142
|
+
byStage: {},
|
|
143
|
+
byFailureClass: {},
|
|
144
|
+
byCliVersion: {},
|
|
145
|
+
};
|
|
146
|
+
for (const m of manifests) {
|
|
147
|
+
if (m.lastAt < cutoff)
|
|
148
|
+
continue;
|
|
149
|
+
summary.total++;
|
|
150
|
+
summary.occurrences += m.occurrences;
|
|
151
|
+
if (m.status === 'open')
|
|
152
|
+
summary.openCount++;
|
|
153
|
+
bump(summary.byStage, m.stage);
|
|
154
|
+
bump(summary.byFailureClass, m.failureClass ?? 'none');
|
|
155
|
+
bump(summary.byCliVersion, m.cliVersion || 'unknown');
|
|
156
|
+
}
|
|
157
|
+
return summary;
|
|
158
|
+
}
|
|
159
|
+
function bump(rec, key) {
|
|
160
|
+
rec[key] = (rec[key] ?? 0) + 1;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* One-line, human-friendly digest. Zero-incident periods produce a short "all
|
|
164
|
+
* clear" line rather than nothing, so the digest itself is a liveness signal.
|
|
165
|
+
*/
|
|
166
|
+
function formatDigestLine(summary, label = 'daily') {
|
|
167
|
+
if (summary.total === 0) {
|
|
168
|
+
return `🩺 ${label} digest: no turn-trace incidents`;
|
|
169
|
+
}
|
|
170
|
+
const stages = Object.entries(summary.byStage)
|
|
171
|
+
.sort((a, b) => b[1] - a[1])
|
|
172
|
+
.map(([s, n]) => `${s}×${n}`)
|
|
173
|
+
.join(', ');
|
|
174
|
+
return (`🩺 ${label} digest: ${summary.total} incident(s), ` +
|
|
175
|
+
`${summary.occurrences} occurrence(s), ${summary.openCount} open — ${stages}`);
|
|
176
|
+
}
|
|
177
|
+
//# sourceMappingURL=incident.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"incident.js","sourceRoot":"","sources":["../../src/agent/incident.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAgFH,gDAQC;AAaD,0CASC;AAiCD,4CAYC;AAGD,gDAMC;AAiCD,8BAeC;AAoBD,gDAwBC;AAUD,4CAYC;AAlRD,0EAA0E;AAC7D,QAAA,SAAS,GAAG,YAAY,CAAA;AAqErC;;;;;GAKG;AACH,SAAgB,kBAAkB,CAAC,KAIlC;IACC,MAAM,GAAG,GAAG,KAAK,CAAC,YAAY,IAAI,MAAM,CAAA;IACxC,MAAM,GAAG,GAAG,gBAAgB,CAAC,KAAK,CAAC,UAAU,CAAC,CAAA;IAC9C,OAAO,GAAG,KAAK,CAAC,KAAK,IAAI,GAAG,IAAI,GAAG,EAAE,CAAA;AACvC,CAAC;AAED,iFAAiF;AACjF,SAAS,gBAAgB,CAAC,CAA4B;IACpD,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAA;IAC1B,OAAO,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;AACrC,CAAC;AAED;;;;GAIG;AACH,SAAgB,eAAe,CAAC,WAAmB;IACjD,IAAI,CAAC,GAAG,UAAU,CAAA;IAClB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,WAAW,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QAC5C,CAAC,IAAI,WAAW,CAAC,UAAU,CAAC,CAAC,CAAC,CAAA;QAC9B,0DAA0D;QAC1D,CAAC,GAAG,IAAI,CAA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BAAgB;QACpD,GAAG,OAAO,CAAC,WAAW,mBAAmB,OAAO,CAAC,SAAS,WAAW,MAAM,EAAE,CAC9E,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recovery executor (Epic #195, Phase 3b).
|
|
3
|
+
*
|
|
4
|
+
* This is the orchestration core that turns a watchdog-detected stall into an
|
|
5
|
+
* actual recovery attempt. It runs in the AGENT RUNNER process — the only place
|
|
6
|
+
* that owns the live control surfaces (session stdin, restart, safe-mode) — but
|
|
7
|
+
* it is written as a PURE function with every side effect injected, so tests
|
|
8
|
+
* drive the full decision path without touching a real session or CLI.
|
|
9
|
+
*
|
|
10
|
+
* Trust + safety model (why the pieces are split the way they are):
|
|
11
|
+
* - The classify step (triage.ts) treats screen text as UNTRUSTED data and
|
|
12
|
+
* validates the model reply against a CLOSED schema.
|
|
13
|
+
* - The decide step (recovery-policy.ts) clamps the proposed action to a
|
|
14
|
+
* per-stage whitelist and enforces a per-turn budget + cooldown.
|
|
15
|
+
* - This execute step maps the *already-validated, already-whitelisted* action
|
|
16
|
+
* to an injected effect. A missing effect is a no-op failure, never a guess.
|
|
17
|
+
*
|
|
18
|
+
* Everything is gated by `autoRecover`: when it is off the executor does nothing
|
|
19
|
+
* but report that it was skipped, so the whole feature ships dark. Safe-mode
|
|
20
|
+
* auto-fallback is deliberately NOT routed through here — it is a reversible
|
|
21
|
+
* backend flip the runner applies on hard PTY failure regardless of this flag.
|
|
22
|
+
*/
|
|
23
|
+
import { type TriageSpawn } from './triage';
|
|
24
|
+
import { type RecoveryAction, type BudgetState, type BudgetConfig } from './recovery-policy';
|
|
25
|
+
import type { RecoveryOutcome } from './incident';
|
|
26
|
+
/**
|
|
27
|
+
* The concrete side effects the executor may invoke. Every method is optional:
|
|
28
|
+
* an action whose effect is not provided is reported as unsupported rather than
|
|
29
|
+
* silently succeeding. Keystroke effects (esc/enter/…) are delivered to the PTY
|
|
30
|
+
* wrapper via the control channel; the restart/backend effects act on the
|
|
31
|
+
* session/receiver. All may be async.
|
|
32
|
+
*/
|
|
33
|
+
export interface RecoveryEffects {
|
|
34
|
+
esc?(): Promise<void> | void;
|
|
35
|
+
escEsc?(): Promise<void> | void;
|
|
36
|
+
enter?(): Promise<void> | void;
|
|
37
|
+
selectOption?(option: number): Promise<void> | void;
|
|
38
|
+
bridgeMenu?(): Promise<void> | void;
|
|
39
|
+
redeliverForward?(): Promise<void> | void;
|
|
40
|
+
restartSession?(): Promise<void> | void;
|
|
41
|
+
restartReceiver?(): Promise<void> | void;
|
|
42
|
+
fallbackHeadless?(): Promise<void> | void;
|
|
43
|
+
/**
|
|
44
|
+
* C1: re-inject the last user message so the user does not have to retype it
|
|
45
|
+
* after a recovery. The implementation MUST guard against duplicates — resend
|
|
46
|
+
* only when the stalled turn produced no output — and returns whether it
|
|
47
|
+
* actually resent. The executor never forces a resend; it only asks.
|
|
48
|
+
*/
|
|
49
|
+
resendLast?(): Promise<boolean> | boolean;
|
|
50
|
+
}
|
|
51
|
+
/** What the watchdog hands the executor for one stalled turn. */
|
|
52
|
+
export interface RecoveryRequest {
|
|
53
|
+
/** Incident id the outcome is recorded against (opaque to the executor). */
|
|
54
|
+
incidentId: string;
|
|
55
|
+
agentId: string;
|
|
56
|
+
chatId: string;
|
|
57
|
+
sessionId: string;
|
|
58
|
+
/** Pipeline stage that stalled (drives the action whitelist). */
|
|
59
|
+
stage: string;
|
|
60
|
+
failureClass: string | null;
|
|
61
|
+
/** Identifies the turn for budget accounting (resets budget when it changes). */
|
|
62
|
+
turnKey: string;
|
|
63
|
+
}
|
|
64
|
+
export interface RecoveryDeps {
|
|
65
|
+
/** Master gate. When false the executor does nothing but report `skipped`. */
|
|
66
|
+
autoRecover: boolean;
|
|
67
|
+
effects: RecoveryEffects;
|
|
68
|
+
now: () => number;
|
|
69
|
+
/** Per-turn budget accounting, injected so it can persist across calls. */
|
|
70
|
+
budget: {
|
|
71
|
+
get(turnKey: string): BudgetState;
|
|
72
|
+
set(state: BudgetState): void;
|
|
73
|
+
config?: BudgetConfig;
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Optional local `claude -p` triage. When omitted, the executor falls back to
|
|
77
|
+
* the deterministic per-stage default action (still whitelist-checked).
|
|
78
|
+
*/
|
|
79
|
+
triageSpawn?: TriageSpawn;
|
|
80
|
+
/**
|
|
81
|
+
* Collect scrubbed evidence for triage (screen snapshot / status text). The
|
|
82
|
+
* caller is responsible for scrubbing; the executor passes it through as data.
|
|
83
|
+
*/
|
|
84
|
+
gatherEvidence?: () => Promise<{
|
|
85
|
+
screenText?: string;
|
|
86
|
+
statusText?: string;
|
|
87
|
+
} | null>;
|
|
88
|
+
/** C1 gate: attempt a guarded resend after a successful unblocking action. */
|
|
89
|
+
resendAfterRecover?: boolean;
|
|
90
|
+
/** Optional structured logger. */
|
|
91
|
+
log?: (msg: string, meta?: Record<string, unknown>) => void;
|
|
92
|
+
}
|
|
93
|
+
/** Full result of one recovery attempt (richer than the persisted schema). */
|
|
94
|
+
export interface RecoveryResult {
|
|
95
|
+
incidentId: string;
|
|
96
|
+
stage: string;
|
|
97
|
+
/** The action actually taken (may be clamped to notify-only). */
|
|
98
|
+
action: RecoveryAction;
|
|
99
|
+
option?: number;
|
|
100
|
+
/** True if an effect was invoked (notify-only / skip / clamp are false). */
|
|
101
|
+
executed: boolean;
|
|
102
|
+
/** True if the invoked effect completed without throwing. */
|
|
103
|
+
ok: boolean;
|
|
104
|
+
reason: string;
|
|
105
|
+
/** True if the last user message was resent (C1). */
|
|
106
|
+
resent: boolean;
|
|
107
|
+
at: number;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Run one recovery attempt. ALWAYS resolves (never throws): any failure degrades
|
|
111
|
+
* to a safe, recorded outcome. The returned RecoveryResult is what the caller
|
|
112
|
+
* persists to the incident bundle and may surface to the user.
|
|
113
|
+
*/
|
|
114
|
+
export declare function runRecovery(req: RecoveryRequest, deps: RecoveryDeps): Promise<RecoveryResult>;
|
|
115
|
+
/** Map a full result to the compact schema persisted in the incident bundle. */
|
|
116
|
+
export declare function toRecoveryOutcome(r: RecoveryResult): RecoveryOutcome;
|
|
117
|
+
//# sourceMappingURL=recovery-executor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recovery-executor.d.ts","sourceRoot":"","sources":["../../src/agent/recovery-executor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAa,KAAK,WAAW,EAAyC,MAAM,UAAU,CAAA;AAC7F,OAAO,EAIL,KAAK,cAAc,EACnB,KAAK,WAAW,EAChB,KAAK,YAAY,EAClB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAEjD;;;;;;GAMG;AACH,MAAM,WAAW,eAAe;IAC9B,GAAG,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAA;IAC5B,MAAM,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAA;IAC/B,KAAK,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAA;IAC9B,YAAY,CAAC,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAA;IACnD,UAAU,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAA;IACnC,gBAAgB,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAA;IACzC,cAAc,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAA;IACvC,eAAe,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAA;IACxC,gBAAgB,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAA;IACzC;;;;;OAKG;IACH,UAAU,CAAC,IAAI,OAAO,CA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ACtB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,4EAA4E;IAC5E,QAAQ,EAAE,OAAO,CAAA;IACjB,6DAA6D;IAC7D,EAAE,EAAE,OAAO,CAAA;IACX,MAAM,EAAE,MAAM,CAAA;IACd,qDAAqD;IACrD,MAAM,EAAE,OAAO,CAAA;IACf,EAAE,EAAE,MAAM,CAAA;CACX;AAED;;;;GAIG;AACH,wBAAsB,WAAW,CAC/B,GAAG,EAAE,eAAe,EACpB,IAAI,EAAE,YAAY,GACjB,OAAO,CAAC,cAAc,CAAC,CA+FzB;AAED,gFAAgF;AAChF,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,cAAc,GAAG,eAAe,CAcpE"}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Recovery executor (Epic #195, Phase 3b).
|
|
4
|
+
*
|
|
5
|
+
* This is the orchestration core that turns a watchdog-detected stall into an
|
|
6
|
+
* actual recovery attempt. It runs in the AGENT RUNNER process — the only place
|
|
7
|
+
* that owns the live control surfaces (session stdin, restart, safe-mode) — but
|
|
8
|
+
* it is written as a PURE function with every side effect injected, so tests
|
|
9
|
+
* drive the full decision path without touching a real session or CLI.
|
|
10
|
+
*
|
|
11
|
+
* Trust + safety model (why the pieces are split the way they are):
|
|
12
|
+
* - The classify step (triage.ts) treats screen text as UNTRUSTED data and
|
|
13
|
+
* validates the model reply against a CLOSED schema.
|
|
14
|
+
* - The decide step (recovery-policy.ts) clamps the proposed action to a
|
|
15
|
+
* per-stage whitelist and enforces a per-turn budget + cooldown.
|
|
16
|
+
* - This execute step maps the *already-validated, already-whitelisted* action
|
|
17
|
+
* to an injected effect. A missing effect is a no-op failure, never a guess.
|
|
18
|
+
*
|
|
19
|
+
* Everything is gated by `autoRecover`: when it is off the executor does nothing
|
|
20
|
+
* but report that it was skipped, so the whole feature ships dark. Safe-mode
|
|
21
|
+
* auto-fallback is deliberately NOT routed through here — it is a reversible
|
|
22
|
+
* backend flip the runner applies on hard PTY failure regardless of this flag.
|
|
23
|
+
*/
|
|
24
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
+
exports.runRecovery = runRecovery;
|
|
26
|
+
exports.toRecoveryOutcome = toRecoveryOutcome;
|
|
27
|
+
const triage_1 = require("./triage");
|
|
28
|
+
const recovery_policy_1 = require("./recovery-policy");
|
|
29
|
+
const ACTION_EFFECT = {
|
|
30
|
+
esc: 'esc',
|
|
31
|
+
'esc-esc': 'escEsc',
|
|
32
|
+
enter: 'enter',
|
|
33
|
+
'select-option': 'selectOption',
|
|
34
|
+
'bridge-menu': 'bridgeMenu',
|
|
35
|
+
'redeliver-forward': 'redeliverForward',
|
|
36
|
+
'restart-session': 'restartSession',
|
|
37
|
+
'restart-receiver': 'restartReceiver',
|
|
38
|
+
'fallback-headless': 'fallbackHeadless',
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Actions after which a guarded resend of the last user message makes sense: the
|
|
42
|
+
* ones that unblock a claude turn which was still waiting for input. Delivery/
|
|
43
|
+
* transport actions (redeliver-forward, restart-receiver) already move the
|
|
44
|
+
* pending output themselves, so a resend there would double-submit.
|
|
45
|
+
*/
|
|
46
|
+
const RESEND_ELIGIBLE = new Set([
|
|
47
|
+
'esc',
|
|
48
|
+
'esc-esc',
|
|
49
|
+
'enter',
|
|
50
|
+
'select-option',
|
|
51
|
+
'bridge-menu',
|
|
52
|
+
'restart-session',
|
|
53
|
+
'fallback-headless',
|
|
54
|
+
]);
|
|
55
|
+
/**
|
|
56
|
+
* Run one recovery attempt. ALWAYS resolves (never throws): any failure degrades
|
|
57
|
+
* to a safe, recorded outcome. The returned RecoveryResult is what the caller
|
|
58
|
+
* persists to the incident bundle and may surface to the user.
|
|
59
|
+
*/
|
|
60
|
+
async function runRecovery(req, deps) {
|
|
61
|
+
const at = deps.now();
|
|
62
|
+
const base = { incidentId: req.incidentId, stage: req.stage, at };
|
|
63
|
+
// Master gate: feature ships dark. Detection/incident/notify already ran in
|
|
64
|
+
// the caller; here we simply do nothing and say so.
|
|
65
|
+
if (!deps.autoRecover) {
|
|
66
|
+
return { ...base, action: 'notify-only', executed: false, ok: true, reason: 'skipped: autoRecover disabled', resent: false };
|
|
67
|
+
}
|
|
68
|
+
// 1) Evidence → 2) triage (classify) → 3) policy (decide). None of these act.
|
|
69
|
+
let verdict = null;
|
|
70
|
+
if (deps.triageSpawn) {
|
|
71
|
+
let bundle = { stage: req.stage, failureClass: req.failureClass };
|
|
72
|
+
try {
|
|
73
|
+
const ev = deps.gatherEvidence ? await deps.gatherEvidence() : null;
|
|
74
|
+
if (ev)
|
|
75
|
+
bundle = { ...bundle, screenText: ev.screenText, statusText: ev.statusText };
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
// Evidence gathering is best-effort; triage can still classify from stage.
|
|
79
|
+
}
|
|
80
|
+
verdict = await (0, triage_1.runTriage)({ spawn: deps.triageSpawn, bundle });
|
|
81
|
+
}
|
|
82
|
+
const plan = (0, recovery_policy_1.selectRecoveryAction)({ stage: req.stage, failureClass: req.failureClass, verdict });
|
|
83
|
+
// notify-only never consumes budget and invokes no effect — the caller owns
|
|
84
|
+
// the user-facing notice, so the executor just records the decision.
|
|
85
|
+
if (plan.action === 'notify-only' || plan.action === 'none') {
|
|
86
|
+
return { ...base, action: 'notify-only', executed: false, ok: true, reason: plan.reason, resent: false };
|
|
87
|
+
}
|
|
88
|
+
// 4) Budget + cooldown. An actionable plan blocked by budget clamps to
|
|
89
|
+
// notify-only (recorded with the action it wanted) and consumes nothing more.
|
|
90
|
+
const cfg = deps.budget.config ?? recovery_policy_1.DEFAULT_BUDGET;
|
|
91
|
+
const verdictBudget = (0, recovery_policy_1.checkBudget)(deps.budget.get(req.turnKey), req.turnKey, at, cfg);
|
|
92
|
+
if (!verdictBudget.allowed) {
|
|
93
|
+
deps.budget.set(verdictBudget.next);
|
|
94
|
+
return {
|
|
95
|
+
...base,
|
|
96
|
+
action: 'notify-only',
|
|
97
|
+
executed: false,
|
|
98
|
+
ok: true,
|
|
99
|
+
reason: `clamped: ${verdictBudget.reason} (wanted ${plan.action})`,
|
|
100
|
+
resent: false,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
deps.budget.set(verdictBudget.next);
|
|
104
|
+
// 5) Execute the whitelisted action via its injected effect.
|
|
105
|
+
const effectKey = ACTION_EFFECT[plan.action];
|
|
106
|
+
const effect = effectKey ? deps.effects[effectKey] : undefined;
|
|
107
|
+
const result = {
|
|
108
|
+
...base,
|
|
109
|
+
action: plan.action,
|
|
110
|
+
option: plan.option,
|
|
111
|
+
executed: false,
|
|
112
|
+
ok: false,
|
|
113
|
+
reason: plan.reason,
|
|
114
|
+
resent: false,
|
|
115
|
+
};
|
|
116
|
+
if (!effect) {
|
|
117
|
+
result.reason = `unsupported: no effect for ${plan.action}`;
|
|
118
|
+
deps.log?.('recovery: unsupported action', { action: plan.action, stage: req.stage });
|
|
119
|
+
return result;
|
|
120
|
+
}
|
|
121
|
+
try {
|
|
122
|
+
if (plan.action === 'select-option' && typeof plan.option === 'number') {
|
|
123
|
+
await effect(plan.option);
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
await effect();
|
|
127
|
+
}
|
|
128
|
+
result.executed = true;
|
|
129
|
+
result.ok = true;
|
|
130
|
+
deps.log?.('recovery: executed', { action: plan.action, stage: req.stage, incidentId: req.incidentId });
|
|
131
|
+
}
|
|
132
|
+
catch (err) {
|
|
133
|
+
result.executed = true;
|
|
134
|
+
result.ok = false;
|
|
135
|
+
result.reason = `effect threw: ${err.message}`;
|
|
136
|
+
deps.log?.('recovery: effect failed', { action: plan.action, error: err.message });
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
// 6) C1 guarded resend. Only after a successful unblocking action, and only if
|
|
140
|
+
// the effect implementation confirms the turn produced no output.
|
|
141
|
+
if (deps.resendAfterRecover && RESEND_ELIGIBLE.has(plan.action) && deps.effects.resendLast) {
|
|
142
|
+
try {
|
|
143
|
+
result.resent = Boolean(await deps.effects.resendLast());
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
result.resent = false;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return result;
|
|
150
|
+
}
|
|
151
|
+
/** Map a full result to the compact schema persisted in the incident bundle. */
|
|
152
|
+
function toRecoveryOutcome(r) {
|
|
153
|
+
const bits = [r.reason];
|
|
154
|
+
if (r.resent)
|
|
155
|
+
bits.push('resent');
|
|
156
|
+
return {
|
|
157
|
+
action: r.option !== undefined ? `${r.action}:${r.option}` : r.action,
|
|
158
|
+
at: r.at,
|
|
159
|
+
// `ok` means "no failure occurred" — true for a successful execution AND for
|
|
160
|
+
// a benign non-action (skipped / notify-only / budget-clamp), false only when
|
|
161
|
+
// an effect was attempted and threw or was unsupported. Using r.ok directly
|
|
162
|
+
// (which is already false only on those failure paths) keeps notify-only
|
|
163
|
+
// decisions from being miscounted as failed recoveries in the digest.
|
|
164
|
+
ok: r.ok,
|
|
165
|
+
detail: bits.join('; '),
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
//# sourceMappingURL=recovery-executor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recovery-executor.js","sourceRoot":"","sources":["../../src/agent/recovery-executor.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;;AAqIH,kCAkGC;AAGD,8CAcC;AAtPD,qCAA6F;AAC7F,uDAO0B;AAgC1B,MAAM,aAAa,GAAuE;IACxF,GAAG,EAAE,KAAK;IACV,SAAS,EAAE,QAAQ;IACnB,KAAK,EAAE,OAAO;IACd,eAAe,EAAE,cAAc;IAC/B,aAAa,EAAE,YAAY;IAC3B,mBAAmB,EAAE,kBAAkB;IACvC,iBAAiB,EAAE,gBAAgB;IACnC,kBAAkB,EAAE,iBAAiB;IACrC,mBAAmB,EAAE,kBAAkB;CACxC,CAAA;AAED;;;;;GAKG;AACH,MAAM,eAAe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AAI,CAAC,IAAI,CAAC,IAAI,CAAC;KACxB,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recovery policy (Epic #195, Phase 3).
|
|
3
|
+
*
|
|
4
|
+
* Turns a (schema-validated) triage verdict into an actual recovery action —
|
|
5
|
+
* but only after enforcing three independent guards, so a compromised or
|
|
6
|
+
* mistaken triage step can never drive an unsafe intervention:
|
|
7
|
+
*
|
|
8
|
+
* 1. Per-stage whitelist. Each pipeline stage permits only a small set of
|
|
9
|
+
* actions (a `dispatch` stall may redeliver or restart the receiver; a
|
|
10
|
+
* `progress` PTY wedge may press esc or restart the session). An action
|
|
11
|
+
* the stage does not permit is clamped to `notify-only`, regardless of
|
|
12
|
+
* what triage proposed.
|
|
13
|
+
* 2. Budget + cooldown. Interventions are capped per turn and spaced by a
|
|
14
|
+
* cooldown, so a flapping stall cannot trigger an unbounded action loop.
|
|
15
|
+
* 3. Safe-mode escalation. Repeated PTY-stage failures recommend flipping the
|
|
16
|
+
* agent to the headless backend instead of retrying the same wedge.
|
|
17
|
+
*
|
|
18
|
+
* All functions are pure — no IO, no clock reads (the caller passes `now`).
|
|
19
|
+
* The executor that actually presses keys / restarts / flips backend lives in
|
|
20
|
+
* the runner; this module decides, it never acts.
|
|
21
|
+
*/
|
|
22
|
+
import type { TriageVerdict, TriageAction } from './triage';
|
|
23
|
+
/** A recovery action the executor understands (same closed vocabulary as triage). */
|
|
24
|
+
export type RecoveryAction = TriageAction;
|
|
25
|
+
/** A selected recovery, ready for the executor. */
|
|
26
|
+
export interface RecoveryPlan {
|
|
27
|
+
action: RecoveryAction;
|
|
28
|
+
/** Present only for `select-option`. */
|
|
29
|
+
option?: number;
|
|
30
|
+
/** Why this action (audit/logging). */
|
|
31
|
+
reason: string;
|
|
32
|
+
}
|
|
33
|
+
/** The safe no-op plan. */
|
|
34
|
+
export declare const NOTIFY_ONLY_PLAN: RecoveryPlan;
|
|
35
|
+
/**
|
|
36
|
+
* Actions permitted per stalled stage. Deliberately conservative: destructive
|
|
37
|
+
* or cross-cutting actions (restart-receiver, fallback-headless) are only
|
|
38
|
+
* allowed where they make sense. `notify-only` is always implicitly allowed.
|
|
39
|
+
*/
|
|
40
|
+
export declare const STAGE_ALLOWED_ACTIONS: Record<string, ReadonlyArray<RecoveryAction>>;
|
|
41
|
+
/**
|
|
42
|
+
* Decide the recovery plan for a stalled stage given an optional triage verdict.
|
|
43
|
+
* If a verdict is supplied, its action is honoured ONLY if the stage permits it;
|
|
44
|
+
* otherwise the plan clamps to notify-only. With no verdict, a conservative
|
|
45
|
+
* default action for the stage is used (still whitelist-checked).
|
|
46
|
+
*/
|
|
47
|
+
export declare function selectRecoveryAction(input: {
|
|
48
|
+
stage: string;
|
|
49
|
+
failureClass: string | null;
|
|
50
|
+
verdict?: TriageVerdict | null;
|
|
51
|
+
}): RecoveryPlan;
|
|
52
|
+
/** Conservative default action when no triage verdict is available. */
|
|
53
|
+
export declare function defaultActionForStage(stage: string): RecoveryAction;
|
|
54
|
+
export declare const DEFAULT_MAX_INTERVENTIONS_PER_TURN = 3;
|
|
55
|
+
export declare const DEFAULT_COOLDOWN_MS = 30000;
|
|
56
|
+
export interface BudgetConfig {
|
|
57
|
+
maxPerTurn: number;
|
|
58
|
+
cooldownMs: number;
|
|
59
|
+
}
|
|
60
|
+
export declare const DEFAULT_BUDGET: BudgetConfig;
|
|
61
|
+
/**
|
|
62
|
+
* Immutable budget state for one turn. A turn is identified by `turnKey`
|
|
63
|
+
* (e.g. chatId + signal timestamp); when the key changes the budget resets.
|
|
64
|
+
*/
|
|
65
|
+
export interface BudgetState {
|
|
66
|
+
turnKey: string;
|
|
67
|
+
attempts: number;
|
|
68
|
+
lastAt: number;
|
|
69
|
+
}
|
|
70
|
+
export declare function initialBudget(turnKey: string): BudgetState;
|
|
71
|
+
export interface BudgetVerdict {
|
|
72
|
+
allowed: boolean;
|
|
73
|
+
reason: string;
|
|
74
|
+
/** The state to persist for the next check (reset if the turn changed). */
|
|
75
|
+
next: BudgetState;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Decide whether an intervention is allowed now, and return the state to keep.
|
|
79
|
+
* Resets the counter when the turn changes; enforces the per-turn cap and the
|
|
80
|
+
* cooldown between attempts. Pure — the caller supplies `now`.
|
|
81
|
+
*/
|
|
82
|
+
export declare function checkBudget(state: BudgetState, turnKey: string, now: number, cfg?: BudgetConfig): BudgetVerdict;
|
|
83
|
+
export declare const DEFAULT_SAFE_MODE_THRESHOLD = 3;
|
|
84
|
+
/**
|
|
85
|
+
* Whether repeated PTY-stage failures should escalate to safe mode (flip the
|
|
86
|
+
* agent to the headless backend). Pure.
|
|
87
|
+
*/
|
|
88
|
+
export declare function shouldEnterSafeMode(consecutivePtyFailures: number, threshold?: number): boolean;
|
|
89
|
+
/**
|
|
90
|
+
* Whether safe mode should be lifted. Safe mode is a reactive fallback; it is
|
|
91
|
+
* cleared explicitly — on a successful PTY turn after a fix, or on user command.
|
|
92
|
+
*/
|
|
93
|
+
export declare function shouldExitSafeMode(input: {
|
|
94
|
+
manualRestore?: boolean;
|
|
95
|
+
healthyPtyTurnObserved?: boolean;
|
|
96
|
+
}): boolean;
|
|
97
|
+
//# sourceMappingURL=recovery-policy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recovery-policy.d.ts","sourceRoot":"","sources":["../../src/agent/recovery-policy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,UAAU,CAAA;AAE3D,qFAAqF;AACrF,MAAM,MAAM,cAAc,GAAG,YAAY,CAAA;AAEzC,mDAAmD;AACnD,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,cAAc,CAAA;IACtB,wCAAwC;IACxC,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,uCAAuC;IACvC,MAAM,EAAE,MAAM,CAA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