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
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// Canonical task assignment — Phase 4A.4A.
|
|
2
|
+
//
|
|
3
|
+
// THIS IS NEW ARCHITECTURE.
|
|
4
|
+
//
|
|
5
|
+
// THE PROBLEM
|
|
6
|
+
// `todo_write` may declare id-less items. Canonical synchronization mints real
|
|
7
|
+
// TaskStore ids for them inside one transaction, so TaskStore knows the
|
|
8
|
+
// identity and the plan builder does not. Phase 4A.4 therefore refused to emit
|
|
9
|
+
// `payloadVersion: 2` for any payload that created a task. This module carries
|
|
10
|
+
// the minted identity forward so the SAME operation that created the task can
|
|
11
|
+
// deliver it to plan construction.
|
|
12
|
+
//
|
|
13
|
+
// WHY THE RETURN VALUE IS THE CHANNEL (this is the load-bearing design fact)
|
|
14
|
+
// Two properties of the frozen kernel decide it, and both were verified in
|
|
15
|
+
// `vendor/minicore` before this design was chosen:
|
|
16
|
+
//
|
|
17
|
+
// 1. `ToolContext` (vendor/minicore/src/core/tool.ts:22) carries
|
|
18
|
+
// `signal`, `state`, `cwd`, `permissionMode` and `emit` — and NO tool-call
|
|
19
|
+
// id. A tool therefore cannot name its own operation, so it cannot key any
|
|
20
|
+
// channel it is handed, and a keyed map is the only thing that could keep
|
|
21
|
+
// two in-flight writes apart. This is why the design is NOT a registry.
|
|
22
|
+
// 2. The kernel pairs `call` with `result` inside ONE `execution:completed`
|
|
23
|
+
// event (vendor/minicore/src/core/executor.ts:102), and only the
|
|
24
|
+
// serialized string survives (`serializeContent`).
|
|
25
|
+
//
|
|
26
|
+
// The tool's return value is therefore the only per-operation channel the
|
|
27
|
+
// kernel itself correlates. Consequence, stated plainly: the assignment travels
|
|
28
|
+
// in the same string the model already receives, as one trailing line. There is
|
|
29
|
+
// no hidden state, no module-level map, and no way for operation A's assignment
|
|
30
|
+
// to be read by operation B — the adapter cannot see it at all except through
|
|
31
|
+
// the result of that same call.
|
|
32
|
+
//
|
|
33
|
+
// The rejected alternative was a custom executor that would see the raw
|
|
34
|
+
// (non-serialized) tool return value and emit a separate structured event. That
|
|
35
|
+
// would keep the model output clean, but it means re-implementing the frozen
|
|
36
|
+
// kernel's `runCall` (permission checks, argument validation, abort handling,
|
|
37
|
+
// snapshotting). Duplicating dispatch semantics to save one line of cosmetic
|
|
38
|
+
// noise is a bad trade, so it was not done.
|
|
39
|
+
//
|
|
40
|
+
// FAIL-CLOSED, ALWAYS
|
|
41
|
+
// `decode` returns `undefined` for anything it does not fully understand: a
|
|
42
|
+
// missing trailer, a foreign session, a malformed id, a duplicate, a wrong
|
|
43
|
+
// wire version. An incomplete assignment must never yield `payloadVersion: 2`,
|
|
44
|
+
// so "I don't understand this" and "this is not canonical" are the same answer.
|
|
45
|
+
|
|
46
|
+
import { isTaskId } from "./model.ts"
|
|
47
|
+
|
|
48
|
+
/** Whether the id already existed or was minted by this operation. Kept because
|
|
49
|
+
* the plan builder must be able to tell "confirmed an existing task" from
|
|
50
|
+
* "this operation created it" — the difference is what 4A.4 could not express. */
|
|
51
|
+
export type CanonicalAssignmentKind = "existing" | "new"
|
|
52
|
+
|
|
53
|
+
/** One declared item's canonical identity. */
|
|
54
|
+
export interface CanonicalAssignment {
|
|
55
|
+
taskId: string
|
|
56
|
+
kind: CanonicalAssignmentKind
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** One entry per DECLARED item, in declaration order. Index `i` corresponds to
|
|
60
|
+
* declared item `i` — the order is the contract, so the table is never sorted,
|
|
61
|
+
* deduped or re-derived on the way out. */
|
|
62
|
+
export type CanonicalAssignmentTable = readonly CanonicalAssignment[]
|
|
63
|
+
|
|
64
|
+
const WIRE_VERSION = 1
|
|
65
|
+
const OPEN = "<!-- minicore:canonical-tasks "
|
|
66
|
+
const CLOSE = " -->"
|
|
67
|
+
|
|
68
|
+
/** Serialize the table as a single trailing line.
|
|
69
|
+
*
|
|
70
|
+
* `JSON.stringify` never emits a raw newline, so a session id containing one is
|
|
71
|
+
* escaped rather than splitting the channel across lines. */
|
|
72
|
+
export function encodeCanonicalAssignments(
|
|
73
|
+
sessionId: string,
|
|
74
|
+
table: CanonicalAssignmentTable,
|
|
75
|
+
): string {
|
|
76
|
+
const body = JSON.stringify({
|
|
77
|
+
v: WIRE_VERSION,
|
|
78
|
+
session: sessionId,
|
|
79
|
+
declared: table.map((a) => ({ taskId: a.taskId, kind: a.kind })),
|
|
80
|
+
})
|
|
81
|
+
return `${OPEN}${body}${CLOSE}`
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Recover the table, or `undefined` if anything at all is off.
|
|
85
|
+
*
|
|
86
|
+
* Only the FINAL non-empty line is considered, so ordinary model-visible prose
|
|
87
|
+
* can never be mistaken for the channel. Arity is deliberately NOT checked here:
|
|
88
|
+
* this function knows nothing about the declaration, so the caller compares the
|
|
89
|
+
* returned length against the steps it is about to label. */
|
|
90
|
+
export function decodeCanonicalAssignments(
|
|
91
|
+
content: unknown,
|
|
92
|
+
opts: { sessionId: string },
|
|
93
|
+
): CanonicalAssignmentTable | undefined {
|
|
94
|
+
if (typeof content !== "string" || content.length === 0) return undefined
|
|
95
|
+
|
|
96
|
+
const lines = content.split("\n")
|
|
97
|
+
let last = ""
|
|
98
|
+
for (let i = lines.length - 1; i >= 0; i--) {
|
|
99
|
+
const t = (lines[i] as string).trim()
|
|
100
|
+
if (t.length > 0) {
|
|
101
|
+
last = t
|
|
102
|
+
break
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (!last.startsWith(OPEN) || !last.endsWith(CLOSE)) return undefined
|
|
106
|
+
|
|
107
|
+
let parsed: unknown
|
|
108
|
+
try {
|
|
109
|
+
parsed = JSON.parse(last.slice(OPEN.length, last.length - CLOSE.length))
|
|
110
|
+
} catch {
|
|
111
|
+
return undefined
|
|
112
|
+
}
|
|
113
|
+
if (typeof parsed !== "object" || parsed === null) return undefined
|
|
114
|
+
|
|
115
|
+
const p = parsed as { v?: unknown; session?: unknown; declared?: unknown }
|
|
116
|
+
if (p.v !== WIRE_VERSION) return undefined
|
|
117
|
+
// The session stamp turns cross-session leakage from "argued not to happen"
|
|
118
|
+
// into "detected and rejected".
|
|
119
|
+
if (p.session !== opts.sessionId) return undefined
|
|
120
|
+
if (!Array.isArray(p.declared) || p.declared.length === 0) return undefined
|
|
121
|
+
|
|
122
|
+
const out: CanonicalAssignment[] = []
|
|
123
|
+
const seen = new Set<string>()
|
|
124
|
+
for (const entry of p.declared) {
|
|
125
|
+
if (typeof entry !== "object" || entry === null) return undefined
|
|
126
|
+
const c = entry as { taskId?: unknown; kind?: unknown }
|
|
127
|
+
// `isTaskId` is the single owner of the id format, so the wire format cannot
|
|
128
|
+
// drift away from what TaskStore would accept.
|
|
129
|
+
if (!isTaskId(c.taskId)) return undefined
|
|
130
|
+
if (c.kind !== "existing" && c.kind !== "new") return undefined
|
|
131
|
+
if (seen.has(c.taskId)) return undefined
|
|
132
|
+
seen.add(c.taskId)
|
|
133
|
+
out.push({ taskId: c.taskId, kind: c.kind })
|
|
134
|
+
}
|
|
135
|
+
return out
|
|
136
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
// PHASE 6R — SCHEDULER ADAPTER.
|
|
2
|
+
//
|
|
3
|
+
// The Scheduler already takes an injected `runTurn`. This module supplies a
|
|
4
|
+
// `runTurn` that executes in an AUTONOMOUS EXECUTION CONTEXT rather than in the
|
|
5
|
+
// user's conversation, and it is the ONLY place the two meet.
|
|
6
|
+
//
|
|
7
|
+
// [DESIGN DECISION] Nothing here constructs a `Scheduler` and nothing here is
|
|
8
|
+
// called from production startup. The adapter is a plain factory a composition
|
|
9
|
+
// root would inject; building it does not make the Scheduler reachable, and the
|
|
10
|
+
// production construction count stays 0.
|
|
11
|
+
//
|
|
12
|
+
// [DESIGN DECISION] The mapping from a dispatched work item to a context is
|
|
13
|
+
// TOTAL and EXPLICIT — every field the context needs is present on the work item
|
|
14
|
+
// or the config, and none is guessed. `buildAutonomousRunTurn` refuses to run
|
|
15
|
+
// when a required input is missing rather than substituting a default, because a
|
|
16
|
+
// default here would mean an executor running in the wrong workspace or against
|
|
17
|
+
// the wrong generation.
|
|
18
|
+
|
|
19
|
+
import type { ProductionExecutionRunner } from "../runtime/production-execution.ts"
|
|
20
|
+
import {
|
|
21
|
+
type AutonomousContextConfig,
|
|
22
|
+
type AutonomousContextEvent,
|
|
23
|
+
AutonomousExecutionContext,
|
|
24
|
+
type AutonomousSession,
|
|
25
|
+
type AutonomousSessionSpec,
|
|
26
|
+
type AutonomousTurnResult,
|
|
27
|
+
assertAutonomousToolScope,
|
|
28
|
+
} from "./autonomous-context.ts"
|
|
29
|
+
import type { ExecutionHandle, ExecutionObservation, SchedulerWorkItem } from "./scheduler.ts"
|
|
30
|
+
import type { TaskStore } from "./store.ts"
|
|
31
|
+
|
|
32
|
+
/** The durable facts a Scheduler execution carries, read at claim time. */
|
|
33
|
+
export interface AutonomousExecutionBinding {
|
|
34
|
+
readonly parentSessionId: string
|
|
35
|
+
readonly taskId: string
|
|
36
|
+
readonly execGeneration: number
|
|
37
|
+
readonly sessionIncarnation: number
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface AutonomousAdapterConfig {
|
|
41
|
+
readonly store: TaskStore
|
|
42
|
+
readonly sessionFactory: (spec: AutonomousSessionSpec) => Promise<AutonomousSession>
|
|
43
|
+
readonly tools: readonly { name: string }[]
|
|
44
|
+
readonly provider?: unknown
|
|
45
|
+
readonly model?: string
|
|
46
|
+
readonly maxSteps?: number
|
|
47
|
+
readonly timeoutMs?: number
|
|
48
|
+
/** [DESIGN DECISION] REQUIRED. Never defaulted to `process.cwd()`. */
|
|
49
|
+
readonly cwdFor: (sessionId: string) => string
|
|
50
|
+
readonly onContextEvent?: (event: AutonomousContextEvent) => void
|
|
51
|
+
/**
|
|
52
|
+
* [P1 M15] Admission runtime untuk turn otonom (opsional; default = legacy).
|
|
53
|
+
* Diteruskan apa adanya ke `AutonomousContextConfig` — adapter tak pernah
|
|
54
|
+
* membuat runner sendiri, jadi tak ada jalur kedua ke runtime.
|
|
55
|
+
*/
|
|
56
|
+
readonly executionRunner?: ProductionExecutionRunner
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* [PHASE 6R] Map a Scheduler's own lifecycle result onto the autonomous contract.
|
|
61
|
+
*
|
|
62
|
+
* The Scheduler's `ExecutionObservation` has exactly two shapes, so this mapping
|
|
63
|
+
* is small; the value is that it is EXPLICIT and total rather than an inline
|
|
64
|
+
* ternary, and that it never claims more than it knows.
|
|
65
|
+
*
|
|
66
|
+
* [DESIGN DECISION] `ok` is an observation, never a verdict: a returned turn is
|
|
67
|
+
* not a completed task, and this function must not blur that. The Scheduler's
|
|
68
|
+
* lineage/ownership machinery (6P/6Q) does the actual recording.
|
|
69
|
+
*/
|
|
70
|
+
export function toExecutionObservation(result: AutonomousTurnResult): ExecutionObservation {
|
|
71
|
+
if (result.outcome === "returned") {
|
|
72
|
+
return { kind: "returned", ok: result.ok, detail: result.finalText ?? undefined }
|
|
73
|
+
}
|
|
74
|
+
return { kind: "rejected", detail: `${result.outcome}: ${result.detail ?? "no detail"}` }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* [PHASE 6R] The context for one dispatched work item, or a refusal.
|
|
79
|
+
*
|
|
80
|
+
* Exported separately so a composition root (or a test) can inspect exactly what
|
|
81
|
+
* an execution WOULD look like without running one.
|
|
82
|
+
*/
|
|
83
|
+
export function planAutonomousContext(
|
|
84
|
+
work: SchedulerWorkItem,
|
|
85
|
+
binding: AutonomousExecutionBinding,
|
|
86
|
+
config: AutonomousAdapterConfig,
|
|
87
|
+
): AutonomousContextConfig {
|
|
88
|
+
const missing: string[] = []
|
|
89
|
+
if (!binding.parentSessionId) missing.push("parentSessionId")
|
|
90
|
+
if (!binding.taskId) missing.push("taskId")
|
|
91
|
+
if (!Number.isSafeInteger(binding.execGeneration) || binding.execGeneration < 1) {
|
|
92
|
+
missing.push("execGeneration")
|
|
93
|
+
}
|
|
94
|
+
if (!Number.isSafeInteger(binding.sessionIncarnation) || binding.sessionIncarnation < 1) {
|
|
95
|
+
missing.push("sessionIncarnation")
|
|
96
|
+
}
|
|
97
|
+
if (!config.cwdFor) missing.push("cwdFor")
|
|
98
|
+
if (missing.length > 0) {
|
|
99
|
+
throw new Error(
|
|
100
|
+
`autonomous adapter refused to plan an execution; missing durable binding: ${missing.join(", ")}`,
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
parentSessionId: binding.parentSessionId,
|
|
105
|
+
taskId: binding.taskId,
|
|
106
|
+
taskTitle: work.title,
|
|
107
|
+
instruction: work.instruction,
|
|
108
|
+
execGeneration: binding.execGeneration,
|
|
109
|
+
sessionIncarnation: binding.sessionIncarnation,
|
|
110
|
+
cwd: config.cwdFor(binding.parentSessionId),
|
|
111
|
+
...(config.model ? { model: config.model } : {}),
|
|
112
|
+
...(config.maxSteps ? { maxSteps: config.maxSteps } : {}),
|
|
113
|
+
...(config.timeoutMs ? { timeoutMs: config.timeoutMs } : {}),
|
|
114
|
+
...(config.provider !== undefined ? { provider: config.provider } : {}),
|
|
115
|
+
sessionFactory: config.sessionFactory,
|
|
116
|
+
tools: config.tools,
|
|
117
|
+
...(config.onContextEvent ? { onEvent: config.onContextEvent } : {}),
|
|
118
|
+
...(config.executionRunner ? { executionRunner: config.executionRunner } : {}),
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* [PHASE 6R] Build the `runTurn` the Scheduler will call.
|
|
124
|
+
*
|
|
125
|
+
* The returned function is a `RunTurn`: it receives a `SchedulerWorkItem`,
|
|
126
|
+
* executes it in a private child context, and returns an observation. It holds
|
|
127
|
+
* NO Scheduler state and creates NO global state, so several adapters can exist
|
|
128
|
+
* at once without interfering.
|
|
129
|
+
*/
|
|
130
|
+
export function buildAutonomousRunTurn(
|
|
131
|
+
bindingFor: (taskId: string) => AutonomousExecutionBinding,
|
|
132
|
+
config: AutonomousAdapterConfig,
|
|
133
|
+
): (work: SchedulerWorkItem, active?: ExecutionHandle) => Promise<ExecutionObservation> {
|
|
134
|
+
// Fail at ADAPTER construction, not on first dispatch: a wider tool set is a
|
|
135
|
+
// configuration error and should never reach the point of granting an
|
|
136
|
+
// unattended executor write access.
|
|
137
|
+
assertAutonomousToolScope(config.tools.map((t) => t.name))
|
|
138
|
+
|
|
139
|
+
return async (
|
|
140
|
+
work: SchedulerWorkItem,
|
|
141
|
+
active?: ExecutionHandle,
|
|
142
|
+
): Promise<ExecutionObservation> => {
|
|
143
|
+
// [DESIGN DECISION] A refusal to plan is allowed to THROW. The Scheduler
|
|
144
|
+
// treats a synchronous throw from the bridge as a DISPATCH failure and
|
|
145
|
+
// releases the claim, which is correct: an execution that was never planned
|
|
146
|
+
// must not be recorded as an attempt that happened.
|
|
147
|
+
const context = new AutonomousExecutionContext(
|
|
148
|
+
planAutonomousContext(work, bindingFor(work.taskId), config),
|
|
149
|
+
)
|
|
150
|
+
try {
|
|
151
|
+
// [PHASE 6T] Publish the context's cancel to the lifecycle handle BEFORE
|
|
152
|
+
// initializing, so a session deletion racing startup still lands. `attach`
|
|
153
|
+
// fires immediately if the cancel already happened, which is the whole
|
|
154
|
+
// reason the handle records a request it cannot yet deliver.
|
|
155
|
+
active?.attach(() => context.cancel("session-deleted"))
|
|
156
|
+
const result = await context.execute()
|
|
157
|
+
return toExecutionObservation(result)
|
|
158
|
+
} finally {
|
|
159
|
+
// The context owns the child session, the AbortController and the provider
|
|
160
|
+
// resources. Disposal is unconditional and idempotent, so no path can leak
|
|
161
|
+
// them - including the error path.
|
|
162
|
+
context.dispose()
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Exported for symmetry: the concrete context type the adapter produces. */
|
|
168
|
+
export type { AutonomousExecutionContext }
|