minicode-ai 0.11.1 → 0.13.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/CHANGELOG.md +89 -0
- package/LICENSE +21 -21
- package/README.md +4 -4
- package/cli/args.ts +46 -0
- package/cli/commands/acp.ts +163 -10
- package/cli/commands/config.ts +33 -18
- package/cli/commands/daemon.ts +515 -0
- package/cli/commands/exec.ts +145 -53
- package/cli/commands/openwork.ts +986 -0
- package/cli/commands/sessions.ts +4 -1
- package/cli/commands/stats.ts +34 -4
- package/cli/commands.ts +217 -2
- package/cli/index.ts +146 -14
- package/cli/recovery-warning.ts +115 -0
- package/cli/router.ts +32 -1
- package/cli/setup.ts +2332 -171
- package/cli/tui.ts +113 -7
- package/docs/OUTPUT_ARCHITECTURE_AUDIT.md +489 -0
- package/docs/OUTPUT_EVENT_MODEL.md +289 -0
- package/docs/OUTPUT_IMPLEMENTATION_PLAN.md +391 -0
- package/docs/OUTPUT_PROTOCOL_SPEC.md +285 -0
- package/docs/OUTPUT_RENDERING_SPEC.md +248 -0
- package/docs/OUTPUT_UX_RULES.md +449 -0
- package/docs/PLAN_UIUX_V6.md +2 -2
- package/docs/README.md +4 -3
- package/docs/TERMINAL_CONTRACT.md +278 -71
- package/docs/architecture.md +89 -7
- package/docs/changelog.md +1 -1
- package/docs/choosing-mode.md +2 -2
- package/docs/cli.md +9 -5
- package/docs/concepts.md +1 -1
- package/docs/contributing.md +9 -2
- package/docs/environment.md +6 -2
- package/docs/exec.md +46 -5
- package/docs/memory-sessions.md +2 -0
- package/docs/policy-sandbox.md +1 -1
- package/docs/pricing-budget.md +1 -1
- package/docs/quickstart.md +2 -2
- package/docs/repl.md +6 -2
- package/docs/security-model.md +1 -1
- package/docs/security.md +1 -1
- package/docs/skills.md +2 -2
- package/docs/terminal.md +9 -5
- package/docs/tools.md +1 -1
- package/package.json +35 -5
- package/src/app/session.ts +261 -15
- package/src/app/tool-layer.ts +94 -7
- package/src/daemon/approvals.ts +361 -0
- package/src/daemon/capability.ts +145 -0
- package/src/daemon/children.ts +142 -0
- package/src/daemon/client.ts +267 -0
- package/src/daemon/control.ts +350 -0
- package/src/daemon/discovery.ts +234 -0
- package/src/daemon/feed.ts +155 -0
- package/src/daemon/framing.ts +94 -0
- package/src/daemon/host.ts +316 -0
- package/src/daemon/protocol.ts +217 -0
- package/src/daemon/resource.ts +170 -0
- package/src/daemon/sanitize.ts +57 -0
- package/src/daemon/server.ts +627 -0
- package/src/daemon/store.ts +317 -0
- package/src/daemon/subscription.ts +270 -0
- package/src/daemon/telemetry.ts +81 -0
- package/src/hooks/run.ts +12 -1
- package/src/lib/atomic-write.ts +29 -3
- package/src/lib/db-path.ts +26 -0
- package/src/lib/keystore.ts +544 -42
- package/src/lib/net.ts +43 -1
- package/src/lib/safe-open.ts +116 -1
- package/src/lib/session-id.ts +15 -0
- package/src/lsp/client.ts +123 -5
- package/src/mcp/client.ts +139 -17
- package/src/mcp/server.ts +177 -50
- package/src/mcp/transport.ts +16 -19
- package/src/memory/vector.ts +770 -55
- package/src/policy/allowlist.ts +14 -2
- package/src/policy/bash-guard.ts +105 -10
- package/src/policy/compaction.ts +82 -5
- package/src/policy/consequence-engine.ts +220 -0
- package/src/policy/context.ts +132 -0
- package/src/policy/executor.ts +187 -121
- package/src/policy/no-progress.ts +559 -0
- package/src/policy/permission.ts +4 -8
- package/src/policy/pricing.ts +17 -3
- package/src/policy/scrub.ts +69 -2
- package/src/policy/tool-contract.ts +515 -0
- package/src/policy/usage.ts +310 -24
- package/src/policy/verifier.ts +6 -3
- package/src/policy/work-mode.ts +182 -0
- package/src/presentation/adapter.ts +533 -26
- package/src/presentation/events.ts +151 -0
- package/src/presentation/model.ts +180 -27
- package/src/presentation/projection.ts +858 -0
- package/src/presentation/reducer.ts +645 -310
- package/src/presentation/store.ts +12 -7
- package/src/providers/anthropic.ts +18 -10
- package/src/providers/responses.ts +2 -2
- package/src/runtime/capability.ts +409 -0
- package/src/runtime/child-execution.ts +274 -0
- package/src/runtime/composition.ts +958 -0
- package/src/runtime/dispatch.ts +591 -0
- package/src/runtime/execution-backend.ts +632 -0
- package/src/runtime/execution-events.ts +272 -0
- package/src/runtime/execution-id.ts +230 -0
- package/src/runtime/execution-journal.ts +926 -0
- package/src/runtime/execution-kernel.ts +456 -0
- package/src/runtime/execution-registry.ts +379 -0
- package/src/runtime/process-supervision.ts +276 -0
- package/src/runtime/production-execution.ts +551 -0
- package/src/runtime/production-runtime.ts +217 -0
- package/src/runtime/recovery-safety.ts +70 -0
- package/src/runtime/recovery.ts +477 -0
- package/src/runtime/runtime-capabilities.ts +179 -0
- package/src/runtime/runtime-diagnostics.ts +141 -0
- package/src/runtime/runtime-host.ts +365 -0
- package/src/runtime/runtime-status-facts.ts +222 -0
- package/src/runtime/runtime-status-provider.ts +74 -0
- package/src/runtime/runtime-status.ts +459 -0
- package/src/runtime/runtime-task-admission.ts +101 -0
- package/src/runtime/supervisor.ts +444 -0
- package/src/session/authority.ts +168 -0
- package/src/session/checkpoint.ts +88 -1
- package/src/session/context-adapter.ts +251 -0
- package/src/session/context-assembly.ts +242 -0
- package/src/session/context-identity.ts +389 -0
- package/src/session/context-projection.ts +248 -0
- package/src/session/context-selector.ts +500 -0
- package/src/session/cross-session-adoption.ts +249 -0
- package/src/session/full-history-fold.ts +171 -0
- package/src/session/identity.ts +192 -0
- package/src/session/journal.ts +805 -102
- package/src/session/open-work-intent.ts +732 -0
- package/src/session/open-work.ts +767 -0
- package/src/session/pending-tools.ts +94 -0
- package/src/session/persistence.ts +3569 -127
- package/src/session/runtime-header.ts +208 -0
- package/src/session/shadow-git.ts +13 -5
- package/src/session/verification.ts +1623 -0
- package/src/task/assignment.ts +136 -0
- package/src/task/autonomous-adapter.ts +168 -0
- package/src/task/autonomous-context.ts +630 -0
- package/src/task/autonomous-policy.ts +490 -0
- package/src/task/continuity.ts +179 -0
- package/src/task/execution-cancellation.ts +165 -0
- package/src/task/graph-validate.ts +379 -0
- package/src/task/graph.ts +305 -0
- package/src/task/identity.ts +230 -0
- package/src/task/model.ts +417 -0
- package/src/task/production-scheduler.ts +293 -0
- package/src/task/readiness.ts +229 -0
- package/src/task/scheduler-observability.ts +387 -0
- package/src/task/scheduler.ts +1197 -0
- package/src/task/scheduling-policy.ts +165 -0
- package/src/task/session-authority.ts +148 -0
- package/src/task/session-ownership.ts +211 -0
- package/src/task/store.ts +2887 -0
- package/src/task/sync.ts +343 -0
- package/src/task/trigger.ts +260 -0
- package/src/tools/bash.ts +377 -211
- package/src/tools/code_run.ts +1 -1
- package/src/tools/delete_file.ts +1 -1
- package/src/tools/edit.ts +33 -6
- package/src/tools/evidence.ts +30 -0
- package/src/tools/git.ts +4 -4
- package/src/tools/glob.ts +12 -3
- package/src/tools/grep.ts +1 -1
- package/src/tools/lsp.ts +4 -4
- package/src/tools/mcp_call.ts +11 -4
- package/src/tools/memory.ts +153 -18
- package/src/tools/move_file.ts +1 -1
- package/src/tools/patch.ts +1 -1
- package/src/tools/read_file.ts +1 -1
- package/src/tools/read_image.ts +1 -1
- package/src/tools/submit_result.ts +1 -1
- package/src/tools/task.ts +437 -41
- package/src/tools/todo.ts +662 -35
- package/src/tools/web_fetch.ts +4 -2
- package/src/tools/web_search.ts +9 -3
- package/src/tools/write_file.ts +1 -1
- package/src/ui/assistant/simple.ts +123 -50
- package/src/ui/assistant/turn-status.ts +17 -3
- package/src/ui/contract.ts +184 -0
- package/src/ui/footer.ts +52 -19
- package/src/ui/i18n/en.ts +58 -6
- package/src/ui/i18n/id.ts +58 -6
- package/src/ui/input/prompt-engine.ts +120 -25
- package/src/ui/render/errors.ts +39 -1
- package/src/ui/render/markdown-table.ts +455 -0
- package/src/ui/render/markdown.ts +22 -1
- package/src/ui/render/table-grid.ts +184 -0
- package/src/ui/render/table.ts +15 -76
- package/src/ui/runtime/screen.ts +7 -2
- package/src/ui/runtime/statusline.ts +25 -6
- package/src/ui/tui/app.ts +507 -169
- package/src/ui/tui/transcript.ts +531 -89
- package/vendor/minicore/VENDOR.md +1 -1
- package/vendor/minicore/src/core/budget.ts +25 -25
- package/vendor/minicore/src/core/compact.ts +75 -75
- package/vendor/minicore/src/core/errors.ts +59 -59
- package/vendor/minicore/src/core/events.ts +73 -73
- package/vendor/minicore/src/core/executor.ts +139 -139
- package/vendor/minicore/src/core/history.ts +31 -31
- package/vendor/minicore/src/core/index.ts +41 -41
- package/vendor/minicore/src/core/loop.ts +355 -355
- package/vendor/minicore/src/core/permission.ts +22 -22
- package/vendor/minicore/src/core/provider.ts +39 -39
- package/vendor/minicore/src/core/recovery.ts +35 -35
- package/vendor/minicore/src/core/session.ts +327 -327
- package/vendor/minicore/src/core/snapshot.ts +146 -146
- package/vendor/minicore/src/core/tokens.ts +107 -107
- package/vendor/minicore/src/core/tool.ts +115 -115
- package/vendor/minicore/src/core/types.ts +50 -50
- package/vendor/minicore/src/providers/openai-compat.ts +417 -417
- package/AGENTS.md +0 -45
- package/PLAN.md +0 -491
- package/docs/ARCHITECTURE.html +0 -1248
- package/docs/CONTROL-PLANE-MAP.md +0 -139
- package/docs/HARNESS.md +0 -150
- package/docs/SUMMARY.md +0 -52
- package/docs/UI_RENDER_PIPELINE.md +0 -169
- package/docs/USAGE.md +0 -11
|
@@ -5,7 +5,7 @@ Salinan kernel MiniCore agar `bun install` tidak membutuhkan clone sibling
|
|
|
5
5
|
|
|
6
6
|
- source commit: `0d33571047c0ed3778986108fd42e4bde6828bb0`
|
|
7
7
|
- files: 20
|
|
8
|
-
- hash: `
|
|
8
|
+
- hash: `c8d214bb8d5eea86`
|
|
9
9
|
- shipped hash: `78768e71a45254c3` (18 file) — fingerprint file vendor yang ikut paket npm
|
|
10
10
|
(hash di atas mencakup test/fakes.ts yang sengaja tidak ikut paket;
|
|
11
11
|
verifikasi dari paket terbit: hitung hash vendor/minicore di dalam tarball)
|
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
// Token budget policy: pure functions over a budget state. No storage, no side effects.
|
|
2
|
-
|
|
3
|
-
export type PressureLevel = "low" | "medium" | "high" | "critical";
|
|
4
|
-
|
|
5
|
-
export interface BudgetState {
|
|
6
|
-
usedTokens: number;
|
|
7
|
-
limitTokens: number;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export interface BudgetPolicy {
|
|
11
|
-
evaluate(state: BudgetState): PressureLevel;
|
|
12
|
-
shouldCompact(pressure: PressureLevel): boolean;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const defaultBudgetPolicy: BudgetPolicy = {
|
|
16
|
-
evaluate({ usedTokens, limitTokens }) {
|
|
17
|
-
const ratio = usedTokens / Math.max(1, limitTokens);
|
|
18
|
-
if (ratio >= 1) return "critical";
|
|
19
|
-
if (ratio >= 0.9) return "high";
|
|
20
|
-
if (ratio >= 0.75) return "medium";
|
|
21
|
-
return "low";
|
|
22
|
-
},
|
|
23
|
-
shouldCompact(pressure) {
|
|
24
|
-
return pressure === "medium" || pressure === "high" || pressure === "critical";
|
|
25
|
-
},
|
|
1
|
+
// Token budget policy: pure functions over a budget state. No storage, no side effects.
|
|
2
|
+
|
|
3
|
+
export type PressureLevel = "low" | "medium" | "high" | "critical";
|
|
4
|
+
|
|
5
|
+
export interface BudgetState {
|
|
6
|
+
usedTokens: number;
|
|
7
|
+
limitTokens: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface BudgetPolicy {
|
|
11
|
+
evaluate(state: BudgetState): PressureLevel;
|
|
12
|
+
shouldCompact(pressure: PressureLevel): boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const defaultBudgetPolicy: BudgetPolicy = {
|
|
16
|
+
evaluate({ usedTokens, limitTokens }) {
|
|
17
|
+
const ratio = usedTokens / Math.max(1, limitTokens);
|
|
18
|
+
if (ratio >= 1) return "critical";
|
|
19
|
+
if (ratio >= 0.9) return "high";
|
|
20
|
+
if (ratio >= 0.75) return "medium";
|
|
21
|
+
return "low";
|
|
22
|
+
},
|
|
23
|
+
shouldCompact(pressure) {
|
|
24
|
+
return pressure === "medium" || pressure === "high" || pressure === "critical";
|
|
25
|
+
},
|
|
26
26
|
};
|
|
@@ -1,76 +1,76 @@
|
|
|
1
|
-
// Compaction: a deterministic, LLM-free strategy. Replaces old turns with a
|
|
2
|
-
// bounded summary while preserving the recent tail verbatim. It never splits
|
|
3
|
-
// an assistant tool_call message from its following tool results.
|
|
4
|
-
|
|
5
|
-
import type { ContextStore } from "./history.ts";
|
|
6
|
-
import type { Message } from "./types.ts";
|
|
7
|
-
import { contentToText, safeStringify } from "./tokens.ts";
|
|
8
|
-
|
|
9
|
-
export interface CompactionStrategy {
|
|
10
|
-
readonly kind: string;
|
|
11
|
-
compact(store: ContextStore, opts: { keepRecentTurns: number }): readonly Message[];
|
|
12
|
-
/**
|
|
13
|
-
* Optional async compaction. If present, the loop prefers this over `compact`
|
|
14
|
-
* (sync). Must return the new messages array (or same if no compaction needed).
|
|
15
|
-
* The loop expects this to respect `signal` (abort → reject). If it throws,
|
|
16
|
-
* the loop falls back to `compact` (sync mechanical) — never crash the loop.
|
|
17
|
-
* The signal is the turn's abort signal (user abort, session.abort, timeout).
|
|
18
|
-
*/
|
|
19
|
-
compactAsync?(store: ContextStore, opts: { keepRecentTurns: number }, signal: AbortSignal): Promise<readonly Message[]>;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const mechanicalCompaction: CompactionStrategy = {
|
|
23
|
-
kind: "mechanical",
|
|
24
|
-
compact(store, { keepRecentTurns }) {
|
|
25
|
-
const messages = store.messages;
|
|
26
|
-
let kept = 0;
|
|
27
|
-
let turns = 0;
|
|
28
|
-
for (let i = messages.length - 1; i >= 0; i--) {
|
|
29
|
-
kept++;
|
|
30
|
-
if (messages[i]!.role === "user") turns++;
|
|
31
|
-
if (turns >= keepRecentTurns) break;
|
|
32
|
-
}
|
|
33
|
-
// Never leave a tool result dangling: extend the kept region across a
|
|
34
|
-
// tool's call batch (assistant → tool results) so results stay paired.
|
|
35
|
-
while (kept < messages.length) {
|
|
36
|
-
const cut = messages.length - kept;
|
|
37
|
-
const firstKept = messages[cut]!;
|
|
38
|
-
const prev = messages[cut - 1];
|
|
39
|
-
const extendsPair =
|
|
40
|
-
firstKept.role === "tool" &&
|
|
41
|
-
prev !== undefined &&
|
|
42
|
-
(prev.role === "tool" || (prev.role === "assistant" && prev.toolCalls !== undefined && prev.toolCalls.length > 0));
|
|
43
|
-
if (extendsPair) kept++;
|
|
44
|
-
else break;
|
|
45
|
-
}
|
|
46
|
-
if (kept >= messages.length) return messages;
|
|
47
|
-
const prefix = messages.slice(0, messages.length - kept);
|
|
48
|
-
const summary: Message = {
|
|
49
|
-
role: "user",
|
|
50
|
-
content: `Previous context:\n${prefix.map(compactLine).join("\n")}`,
|
|
51
|
-
};
|
|
52
|
-
return [summary, ...messages.slice(-kept)];
|
|
53
|
-
},
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
function compactLine(message: Message): string {
|
|
57
|
-
switch (message.role) {
|
|
58
|
-
case "user":
|
|
59
|
-
return `- user: ${head(contentToText(message.content), 400)}`;
|
|
60
|
-
case "assistant": {
|
|
61
|
-
const calls = (message.toolCalls ?? [])
|
|
62
|
-
.map((call) => `${call.name}(${head(safeStringify(call.args), 80)})`)
|
|
63
|
-
.join(", ");
|
|
64
|
-
return `- assistant${calls ? ` [calls: ${calls}]` : ""}: ${head(contentToText(message.content), 400)}`;
|
|
65
|
-
}
|
|
66
|
-
case "tool":
|
|
67
|
-
if (message.isError) return `- tool(${message.name}): ${head(String(message.content), 200)}`;
|
|
68
|
-
return `- tool(${message.name}): <result omitted>`;
|
|
69
|
-
default:
|
|
70
|
-
return "";
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function head(text: string, max: number): string {
|
|
75
|
-
return text.length <= max ? text : `${text.slice(0, max)}…`;
|
|
1
|
+
// Compaction: a deterministic, LLM-free strategy. Replaces old turns with a
|
|
2
|
+
// bounded summary while preserving the recent tail verbatim. It never splits
|
|
3
|
+
// an assistant tool_call message from its following tool results.
|
|
4
|
+
|
|
5
|
+
import type { ContextStore } from "./history.ts";
|
|
6
|
+
import type { Message } from "./types.ts";
|
|
7
|
+
import { contentToText, safeStringify } from "./tokens.ts";
|
|
8
|
+
|
|
9
|
+
export interface CompactionStrategy {
|
|
10
|
+
readonly kind: string;
|
|
11
|
+
compact(store: ContextStore, opts: { keepRecentTurns: number }): readonly Message[];
|
|
12
|
+
/**
|
|
13
|
+
* Optional async compaction. If present, the loop prefers this over `compact`
|
|
14
|
+
* (sync). Must return the new messages array (or same if no compaction needed).
|
|
15
|
+
* The loop expects this to respect `signal` (abort → reject). If it throws,
|
|
16
|
+
* the loop falls back to `compact` (sync mechanical) — never crash the loop.
|
|
17
|
+
* The signal is the turn's abort signal (user abort, session.abort, timeout).
|
|
18
|
+
*/
|
|
19
|
+
compactAsync?(store: ContextStore, opts: { keepRecentTurns: number }, signal: AbortSignal): Promise<readonly Message[]>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const mechanicalCompaction: CompactionStrategy = {
|
|
23
|
+
kind: "mechanical",
|
|
24
|
+
compact(store, { keepRecentTurns }) {
|
|
25
|
+
const messages = store.messages;
|
|
26
|
+
let kept = 0;
|
|
27
|
+
let turns = 0;
|
|
28
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
29
|
+
kept++;
|
|
30
|
+
if (messages[i]!.role === "user") turns++;
|
|
31
|
+
if (turns >= keepRecentTurns) break;
|
|
32
|
+
}
|
|
33
|
+
// Never leave a tool result dangling: extend the kept region across a
|
|
34
|
+
// tool's call batch (assistant → tool results) so results stay paired.
|
|
35
|
+
while (kept < messages.length) {
|
|
36
|
+
const cut = messages.length - kept;
|
|
37
|
+
const firstKept = messages[cut]!;
|
|
38
|
+
const prev = messages[cut - 1];
|
|
39
|
+
const extendsPair =
|
|
40
|
+
firstKept.role === "tool" &&
|
|
41
|
+
prev !== undefined &&
|
|
42
|
+
(prev.role === "tool" || (prev.role === "assistant" && prev.toolCalls !== undefined && prev.toolCalls.length > 0));
|
|
43
|
+
if (extendsPair) kept++;
|
|
44
|
+
else break;
|
|
45
|
+
}
|
|
46
|
+
if (kept >= messages.length) return messages;
|
|
47
|
+
const prefix = messages.slice(0, messages.length - kept);
|
|
48
|
+
const summary: Message = {
|
|
49
|
+
role: "user",
|
|
50
|
+
content: `Previous context:\n${prefix.map(compactLine).join("\n")}`,
|
|
51
|
+
};
|
|
52
|
+
return [summary, ...messages.slice(-kept)];
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
function compactLine(message: Message): string {
|
|
57
|
+
switch (message.role) {
|
|
58
|
+
case "user":
|
|
59
|
+
return `- user: ${head(contentToText(message.content), 400)}`;
|
|
60
|
+
case "assistant": {
|
|
61
|
+
const calls = (message.toolCalls ?? [])
|
|
62
|
+
.map((call) => `${call.name}(${head(safeStringify(call.args), 80)})`)
|
|
63
|
+
.join(", ");
|
|
64
|
+
return `- assistant${calls ? ` [calls: ${calls}]` : ""}: ${head(contentToText(message.content), 400)}`;
|
|
65
|
+
}
|
|
66
|
+
case "tool":
|
|
67
|
+
if (message.isError) return `- tool(${message.name}): ${head(String(message.content), 200)}`;
|
|
68
|
+
return `- tool(${message.name}): <result omitted>`;
|
|
69
|
+
default:
|
|
70
|
+
return "";
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function head(text: string, max: number): string {
|
|
75
|
+
return text.length <= max ? text : `${text.slice(0, max)}…`;
|
|
76
76
|
}
|
|
@@ -1,60 +1,60 @@
|
|
|
1
|
-
// Provider-agnostic error taxonomy and recovery actions.
|
|
2
|
-
|
|
3
|
-
/** The deterministic reasons a kernel `run()` can settle with. */
|
|
4
|
-
export type AgentErrorKind =
|
|
5
|
-
| "busy"
|
|
6
|
-
| "aborted"
|
|
7
|
-
| "timeout"
|
|
8
|
-
| "max_steps_exceeded"
|
|
9
|
-
| "budget_exceeded"
|
|
10
|
-
| "provider";
|
|
11
|
-
|
|
12
|
-
/** Kernel-side failure, always carrying a `kind` from `AgentErrorKind`. */
|
|
13
|
-
export class AgentError extends Error {
|
|
14
|
-
readonly name = "AgentError";
|
|
15
|
-
constructor(
|
|
16
|
-
readonly kind: AgentErrorKind,
|
|
17
|
-
message: string,
|
|
18
|
-
options?: { cause?: unknown },
|
|
19
|
-
) {
|
|
20
|
-
super(message, options);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Turn an aborted signal into the AgentError it carries. When the kernel
|
|
26
|
-
* aborts a signal with a reason (e.g. a timeout), that reason is preserved so
|
|
27
|
-
* callers can distinguish "timed out" from "user aborted".
|
|
28
|
-
*/
|
|
29
|
-
export function abortError(signal: AbortSignal): AgentError {
|
|
30
|
-
return signal.reason instanceof AgentError ? signal.reason : new AgentError("aborted", "turn aborted");
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/** Normalized provider failure categories produced by adapters. */
|
|
34
|
-
export type ProviderErrorCategory =
|
|
35
|
-
| "rate_limit"
|
|
36
|
-
| "network"
|
|
37
|
-
| "server"
|
|
38
|
-
| "auth"
|
|
39
|
-
| "invalid_request"
|
|
40
|
-
| "context_length_exceeded"
|
|
41
|
-
| "unknown";
|
|
42
|
-
|
|
43
|
-
/** Provider-side failure, normalized by an adapter into a category. */
|
|
44
|
-
export class ProviderError extends Error {
|
|
45
|
-
readonly name = "ProviderError";
|
|
46
|
-
constructor(
|
|
47
|
-
readonly category: ProviderErrorCategory,
|
|
48
|
-
message: string,
|
|
49
|
-
readonly retryAfterMs?: number,
|
|
50
|
-
cause?: unknown,
|
|
51
|
-
) {
|
|
52
|
-
super(message, { cause });
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/** What the recovery policy tells the loop to do after a provider failure. */
|
|
57
|
-
export type RecoveryAction =
|
|
58
|
-
| { type: "retry"; delayMs: number }
|
|
59
|
-
| { type: "force_compact_and_retry" }
|
|
1
|
+
// Provider-agnostic error taxonomy and recovery actions.
|
|
2
|
+
|
|
3
|
+
/** The deterministic reasons a kernel `run()` can settle with. */
|
|
4
|
+
export type AgentErrorKind =
|
|
5
|
+
| "busy"
|
|
6
|
+
| "aborted"
|
|
7
|
+
| "timeout"
|
|
8
|
+
| "max_steps_exceeded"
|
|
9
|
+
| "budget_exceeded"
|
|
10
|
+
| "provider";
|
|
11
|
+
|
|
12
|
+
/** Kernel-side failure, always carrying a `kind` from `AgentErrorKind`. */
|
|
13
|
+
export class AgentError extends Error {
|
|
14
|
+
readonly name = "AgentError";
|
|
15
|
+
constructor(
|
|
16
|
+
readonly kind: AgentErrorKind,
|
|
17
|
+
message: string,
|
|
18
|
+
options?: { cause?: unknown },
|
|
19
|
+
) {
|
|
20
|
+
super(message, options);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Turn an aborted signal into the AgentError it carries. When the kernel
|
|
26
|
+
* aborts a signal with a reason (e.g. a timeout), that reason is preserved so
|
|
27
|
+
* callers can distinguish "timed out" from "user aborted".
|
|
28
|
+
*/
|
|
29
|
+
export function abortError(signal: AbortSignal): AgentError {
|
|
30
|
+
return signal.reason instanceof AgentError ? signal.reason : new AgentError("aborted", "turn aborted");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Normalized provider failure categories produced by adapters. */
|
|
34
|
+
export type ProviderErrorCategory =
|
|
35
|
+
| "rate_limit"
|
|
36
|
+
| "network"
|
|
37
|
+
| "server"
|
|
38
|
+
| "auth"
|
|
39
|
+
| "invalid_request"
|
|
40
|
+
| "context_length_exceeded"
|
|
41
|
+
| "unknown";
|
|
42
|
+
|
|
43
|
+
/** Provider-side failure, normalized by an adapter into a category. */
|
|
44
|
+
export class ProviderError extends Error {
|
|
45
|
+
readonly name = "ProviderError";
|
|
46
|
+
constructor(
|
|
47
|
+
readonly category: ProviderErrorCategory,
|
|
48
|
+
message: string,
|
|
49
|
+
readonly retryAfterMs?: number,
|
|
50
|
+
cause?: unknown,
|
|
51
|
+
) {
|
|
52
|
+
super(message, { cause });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** What the recovery policy tells the loop to do after a provider failure. */
|
|
57
|
+
export type RecoveryAction =
|
|
58
|
+
| { type: "retry"; delayMs: number }
|
|
59
|
+
| { type: "force_compact_and_retry" }
|
|
60
60
|
| { type: "throw" };
|
|
@@ -1,74 +1,74 @@
|
|
|
1
|
-
// Typed event bus for observability and lifecycles.
|
|
2
|
-
|
|
3
|
-
import type { Execution, Step, TurnResult } from "./session.ts";
|
|
4
|
-
|
|
5
|
-
export type AgentEvent =
|
|
6
|
-
| { type: "turn:started"; turn: number }
|
|
7
|
-
| { type: "turn:completed"; result: TurnResult }
|
|
8
|
-
| { type: "step:started"; step: Step }
|
|
9
|
-
| { type: "step:completed"; step: Step }
|
|
10
|
-
| { type: "execution:started"; execution: Execution }
|
|
11
|
-
| { type: "execution:completed"; execution: Execution }
|
|
12
|
-
| { type: "provider:text"; text: string }
|
|
13
|
-
| { type: "provider:extension"; kind: string; data: unknown }
|
|
14
|
-
| { type: "context:compacted"; reason: string };
|
|
15
|
-
|
|
16
|
-
export type AgentEventType = AgentEvent["type"];
|
|
17
|
-
|
|
18
|
-
export interface EventBus {
|
|
19
|
-
on<const T extends AgentEventType | "*">(
|
|
20
|
-
type: T,
|
|
21
|
-
handler: T extends "*" ? (event: AgentEvent) => void : (event: Extract<AgentEvent, { type: T }>) => void,
|
|
22
|
-
): () => void;
|
|
23
|
-
emit(event: AgentEvent): void;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export interface EventBusOptions {
|
|
27
|
-
/**
|
|
28
|
-
* Reports errors thrown by event listeners. Observers must never be able to
|
|
29
|
-
* break the agent: emit catches every handler, reports the failure here, and
|
|
30
|
-
* continues with the remaining listeners. Defaults to console.error.
|
|
31
|
-
*/
|
|
32
|
-
onHandlerError?: (error: unknown, event: AgentEvent) => void;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function createEventBus(options?: EventBusOptions): EventBus {
|
|
36
|
-
const handlers = new Map<string, Set<(event: AgentEvent) => void>>();
|
|
37
|
-
const report =
|
|
38
|
-
options?.onHandlerError ??
|
|
39
|
-
((error: unknown, event: AgentEvent) => {
|
|
40
|
-
console.error(`event handler for "${event.type}" failed`, error);
|
|
41
|
-
});
|
|
42
|
-
return {
|
|
43
|
-
on(type, handler) {
|
|
44
|
-
const wrapped = handler as (event: AgentEvent) => void;
|
|
45
|
-
const set = handlers.get(type) ?? new Set<(event: AgentEvent) => void>();
|
|
46
|
-
set.add(wrapped);
|
|
47
|
-
handlers.set(type, set);
|
|
48
|
-
return () => {
|
|
49
|
-
set.delete(wrapped);
|
|
50
|
-
};
|
|
51
|
-
},
|
|
52
|
-
emit(event) {
|
|
53
|
-
// Per-handler isolation: a crashing observer (UI, logging, metrics) must
|
|
54
|
-
// neither kill the turn nor escape into surrounding try/catches (which
|
|
55
|
-
// used to turn listener crashes into spurious retries). The error is
|
|
56
|
-
// reported and the remaining listeners still run.
|
|
57
|
-
const run = (handler: (event: AgentEvent) => void) => {
|
|
58
|
-
try {
|
|
59
|
-
handler(event);
|
|
60
|
-
} catch (error) {
|
|
61
|
-
try {
|
|
62
|
-
report(error, event);
|
|
63
|
-
} catch {
|
|
64
|
-
// The reporter itself must not be able to break the emit.
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
const set = handlers.get(event.type);
|
|
69
|
-
if (set) for (const handler of set) run(handler);
|
|
70
|
-
const all = handlers.get("*");
|
|
71
|
-
if (all) for (const handler of all) run(handler);
|
|
72
|
-
},
|
|
73
|
-
};
|
|
1
|
+
// Typed event bus for observability and lifecycles.
|
|
2
|
+
|
|
3
|
+
import type { Execution, Step, TurnResult } from "./session.ts";
|
|
4
|
+
|
|
5
|
+
export type AgentEvent =
|
|
6
|
+
| { type: "turn:started"; turn: number }
|
|
7
|
+
| { type: "turn:completed"; result: TurnResult }
|
|
8
|
+
| { type: "step:started"; step: Step }
|
|
9
|
+
| { type: "step:completed"; step: Step }
|
|
10
|
+
| { type: "execution:started"; execution: Execution }
|
|
11
|
+
| { type: "execution:completed"; execution: Execution }
|
|
12
|
+
| { type: "provider:text"; text: string }
|
|
13
|
+
| { type: "provider:extension"; kind: string; data: unknown }
|
|
14
|
+
| { type: "context:compacted"; reason: string };
|
|
15
|
+
|
|
16
|
+
export type AgentEventType = AgentEvent["type"];
|
|
17
|
+
|
|
18
|
+
export interface EventBus {
|
|
19
|
+
on<const T extends AgentEventType | "*">(
|
|
20
|
+
type: T,
|
|
21
|
+
handler: T extends "*" ? (event: AgentEvent) => void : (event: Extract<AgentEvent, { type: T }>) => void,
|
|
22
|
+
): () => void;
|
|
23
|
+
emit(event: AgentEvent): void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface EventBusOptions {
|
|
27
|
+
/**
|
|
28
|
+
* Reports errors thrown by event listeners. Observers must never be able to
|
|
29
|
+
* break the agent: emit catches every handler, reports the failure here, and
|
|
30
|
+
* continues with the remaining listeners. Defaults to console.error.
|
|
31
|
+
*/
|
|
32
|
+
onHandlerError?: (error: unknown, event: AgentEvent) => void;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function createEventBus(options?: EventBusOptions): EventBus {
|
|
36
|
+
const handlers = new Map<string, Set<(event: AgentEvent) => void>>();
|
|
37
|
+
const report =
|
|
38
|
+
options?.onHandlerError ??
|
|
39
|
+
((error: unknown, event: AgentEvent) => {
|
|
40
|
+
console.error(`event handler for "${event.type}" failed`, error);
|
|
41
|
+
});
|
|
42
|
+
return {
|
|
43
|
+
on(type, handler) {
|
|
44
|
+
const wrapped = handler as (event: AgentEvent) => void;
|
|
45
|
+
const set = handlers.get(type) ?? new Set<(event: AgentEvent) => void>();
|
|
46
|
+
set.add(wrapped);
|
|
47
|
+
handlers.set(type, set);
|
|
48
|
+
return () => {
|
|
49
|
+
set.delete(wrapped);
|
|
50
|
+
};
|
|
51
|
+
},
|
|
52
|
+
emit(event) {
|
|
53
|
+
// Per-handler isolation: a crashing observer (UI, logging, metrics) must
|
|
54
|
+
// neither kill the turn nor escape into surrounding try/catches (which
|
|
55
|
+
// used to turn listener crashes into spurious retries). The error is
|
|
56
|
+
// reported and the remaining listeners still run.
|
|
57
|
+
const run = (handler: (event: AgentEvent) => void) => {
|
|
58
|
+
try {
|
|
59
|
+
handler(event);
|
|
60
|
+
} catch (error) {
|
|
61
|
+
try {
|
|
62
|
+
report(error, event);
|
|
63
|
+
} catch {
|
|
64
|
+
// The reporter itself must not be able to break the emit.
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const set = handlers.get(event.type);
|
|
69
|
+
if (set) for (const handler of set) run(handler);
|
|
70
|
+
const all = handlers.get("*");
|
|
71
|
+
if (all) for (const handler of all) run(handler);
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
74
|
}
|