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,274 @@
|
|
|
1
|
+
// M7 — Parent/child execution contract: kontrak admission/derivasi murni
|
|
2
|
+
// untuk KELUARGA DELEGATE + helper perencanaan-cancel bersama (bukan dua
|
|
3
|
+
// validasi berbeda untuk SATU pemakai, dan bukan satu spec untuk SEMUA jalur).
|
|
4
|
+
//
|
|
5
|
+
// Kenapa berkas ini ada (P1 ADR-003, diamendemen ADR-25): delegate_task
|
|
6
|
+
// (src/tools/task.ts) dan autonomous execution (src/task/autonomous-*)
|
|
7
|
+
// masing-masing menemukan ulang warisan cwd/model/sinyal, cap langkah, dan
|
|
8
|
+
// isolasi — tanpa kontrak bersama untuk identitas, capability, deadline,
|
|
9
|
+
// budget, dan detached. Modul ini menyatukan VALIDASI + DERIVASI pra-eksekusi
|
|
10
|
+
// untuk keluarga delegate; eksekusi otonom TIDAK mengonsumsinya (kontraknya
|
|
11
|
+
// mode-less/read-only/default-8 tanpa parent grant tak representable di sini
|
|
12
|
+
// tanpa mengarang artefak otorisasi — lihat ADR-25). Eksekusi aktual tetap
|
|
13
|
+
// milik jalur existing (factory sesi anak + antrean konkurensi + session.run),
|
|
14
|
+
// lifecycle authority milik Kernel (M8), klaim milik TaskStore/Scheduler,
|
|
15
|
+
// recovery milik M12. Satu-satunya pemakai produksi di luar keluarga delegate
|
|
16
|
+
// adalah supervisor (planParentCancellation = perencanaan murni, BUKAN
|
|
17
|
+
// admission).
|
|
18
|
+
//
|
|
19
|
+
// BUKAN: DAG (tree saja — tepat satu parent), nested umum, Execution FSM,
|
|
20
|
+
// supervisor/retry, recovery, scheduler bridge, persistence redesign, kill
|
|
21
|
+
// fisik (M5), capability enforcement (M6 + jail/guard), production wiring
|
|
22
|
+
// (delegate_task TAK DIUBAH di sini — adopsi = M14; autonomous tak pernah
|
|
23
|
+
// mengadopsi admitChild/modeStepCap — ADR-25).
|
|
24
|
+
// Aturan yang dikunci (jangan dilonggarkan tanpa ADR baru):
|
|
25
|
+
// - E_child baru via M1 allocator (tak pernah reuse parent/task/session id).
|
|
26
|
+
// - childGrant ⊆ parentGrant via M6 attenuateGrant (TANPA algoritma kedua).
|
|
27
|
+
// - childDeadline ≤ parentRemaining (clamp, bukan fresh time; tak pernah gain).
|
|
28
|
+
// - Sub-budget subtractive dengan reservation arithmetic; model current =
|
|
29
|
+
// fixed-cap consumptive (evidence: cap explore/plan + hard-cap di task.ts +
|
|
30
|
+
// LIMITS) — overcommit antar sibling dicegah HANYA bila caller merangkai
|
|
31
|
+
// remainingAfter (didokumentasikan jujur, bukan diklaim).
|
|
32
|
+
// - Parent cancel → cancel REQUEST untuk live child; terminal child = NO-OP +
|
|
33
|
+
// audit (tak pernah COMPLETED→CANCELLED). Tak ada mutasi terminal di sini.
|
|
34
|
+
// - Attached default tak boleh silent-outlive parent; detached eksplisit butuh
|
|
35
|
+
// owner + TTL + killPath + audit; lineage_root immutable, cancel_scope
|
|
36
|
+
// independen. Detached ≠ background (kelas lifecycle vs scope — terpisah).
|
|
37
|
+
// - Workspace child di dalam parent (M6 isPathWithin); model routing = metadata
|
|
38
|
+
// (bukan capability). Tak ada import FSM/supervisor/persistence/scheduler/
|
|
39
|
+
// backend-spawn — modul murni (H12-style boundary test menjaganya).
|
|
40
|
+
|
|
41
|
+
import { LIMITS } from "../constants.ts"
|
|
42
|
+
import {
|
|
43
|
+
attenuateGrant,
|
|
44
|
+
type Capability,
|
|
45
|
+
type CapabilityGrant,
|
|
46
|
+
isPathWithin,
|
|
47
|
+
} from "./capability.ts"
|
|
48
|
+
import { createChildCorrelation, type ExecutionCorrelation } from "./execution-id.ts"
|
|
49
|
+
|
|
50
|
+
/** Mode anak yang dikenal current system (evidence: task.ts explore/plan). */
|
|
51
|
+
export type ChildMode = "explore" | "plan"
|
|
52
|
+
|
|
53
|
+
export interface DetachedSpec {
|
|
54
|
+
/** Owner baru yang bertanggung jawab (bukan authority arbitrer user). */
|
|
55
|
+
readonly ownerId: string
|
|
56
|
+
/** TTL ms — tanpa TTL = tolak (tak ada outlive tanpa batas). */
|
|
57
|
+
readonly ttlMs: number
|
|
58
|
+
/** Deskriptor kill path (mis. "owner-cancel", "ttl-sweep"); dieksekusi pemilik, bukan M7. */
|
|
59
|
+
readonly killPath: string
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Canonical child spec — SATU bentuk untuk delegate maupun autonomous. */
|
|
63
|
+
export interface ChildExecutionSpec {
|
|
64
|
+
readonly parent: ExecutionCorrelation
|
|
65
|
+
readonly kind: "child"
|
|
66
|
+
readonly ownerId: string
|
|
67
|
+
readonly mode: ChildMode
|
|
68
|
+
/** Batas langkah yang diminta (default = cap mode). */
|
|
69
|
+
readonly budgetSteps?: number
|
|
70
|
+
/** Sisa langkah parent yang diketahui (bila tak diketahui = fixed-cap saja). */
|
|
71
|
+
readonly parentRemainingSteps?: number
|
|
72
|
+
/** Deadline yang diminta ms (default = sisa parent / cap backend). */
|
|
73
|
+
readonly deadlineMs?: number
|
|
74
|
+
/** Sisa deadline parent ms (bila tak diketahui = cap backend). */
|
|
75
|
+
readonly parentRemainingDeadlineMs?: number
|
|
76
|
+
/** Kapabilitas yang diminta untuk anak (harus ⊆ parent grant). */
|
|
77
|
+
readonly requestedCapabilities: readonly Capability[]
|
|
78
|
+
/** Grant parent sebagai upper bound (wajib). */
|
|
79
|
+
readonly parentGrant: CapabilityGrant
|
|
80
|
+
/** Workspace anak (default = parent workspace yang diteruskan caller). */
|
|
81
|
+
readonly workspaceCwd: string
|
|
82
|
+
/** Workspace parent (batas atas). */
|
|
83
|
+
readonly parentWorkspaceCwd: string
|
|
84
|
+
/** Routing model warisan (metadata, BUKAN capability). */
|
|
85
|
+
readonly modelRouting?: string
|
|
86
|
+
/** Backend hint (mis. "host"); enforcement milik M5 (bukan eksekusi di sini). */
|
|
87
|
+
readonly backendHint?: string
|
|
88
|
+
/** Attached default; detached eksplisit = independen + wajib TTL/owner/kill. */
|
|
89
|
+
readonly detached?: DetachedSpec
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export interface ChildAdmission {
|
|
93
|
+
readonly correlation: ExecutionCorrelation
|
|
94
|
+
readonly grant: CapabilityGrant
|
|
95
|
+
/** Deadline efektif anak (clamp ≤ sisa parent; tak pernah fresh). */
|
|
96
|
+
readonly effectiveDeadlineMs: number | null
|
|
97
|
+
/** Budget langkah efektif anak. */
|
|
98
|
+
readonly budgetSteps: number
|
|
99
|
+
/** Sisa parent sesudah reservasi (bila parentRemainingSteps diketahui). */
|
|
100
|
+
readonly parentRemainingAfter: number | null
|
|
101
|
+
readonly workspaceCwd: string
|
|
102
|
+
readonly cancelScope:
|
|
103
|
+
| { readonly kind: "parent" }
|
|
104
|
+
| { readonly kind: "detached"; readonly owner: string }
|
|
105
|
+
readonly detached: DetachedSpec | null
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function modeStepCap(mode: ChildMode): number {
|
|
109
|
+
// Evidence: SUB_AGENT_BUDGET_EXPLORE/PLAN + hard-cap DEFAULT_MAX_STEPS (task.ts).
|
|
110
|
+
const cap = mode === "explore" ? LIMITS.SUB_AGENT_BUDGET_EXPLORE : LIMITS.SUB_AGENT_BUDGET_PLAN
|
|
111
|
+
return Math.min(cap, LIMITS.DEFAULT_MAX_STEPS)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Validasi + derivasi admission anak (murni, tanpa efek samping), sesuai urutan:
|
|
116
|
+
* parent → identity → capability → deadline → budget → workspace → detached →
|
|
117
|
+
* metadata. Gagal = {admitted:false, reason} (tak pernah throw untuk alasan
|
|
118
|
+
* domain; throw hanya untuk bentuk tak-valid pemrogram).
|
|
119
|
+
*/
|
|
120
|
+
export function admitChild(
|
|
121
|
+
spec: ChildExecutionSpec,
|
|
122
|
+
now: number = Date.now(),
|
|
123
|
+
): { admitted: true; admission: ChildAdmission } | { admitted: false; reason: string } {
|
|
124
|
+
void now
|
|
125
|
+
if (!spec?.parent?.executionId || !spec.parent?.rootExecutionId)
|
|
126
|
+
return { admitted: false, reason: "invalid parent correlation" }
|
|
127
|
+
if (spec.kind !== "child") return { admitted: false, reason: "kind must be 'child'" }
|
|
128
|
+
if (!spec.ownerId) return { admitted: false, reason: "ownerId is required" }
|
|
129
|
+
if (spec.mode !== "explore" && spec.mode !== "plan")
|
|
130
|
+
return { admitted: false, reason: "mode must be explore|plan" }
|
|
131
|
+
if (!spec.parentGrant) return { admitted: false, reason: "parentGrant is required (upper bound)" }
|
|
132
|
+
if (!spec.workspaceCwd || !spec.parentWorkspaceCwd)
|
|
133
|
+
return { admitted: false, reason: "workspaceCwd + parentWorkspaceCwd are required" }
|
|
134
|
+
|
|
135
|
+
// 2. Identity baru (E2 ≠ E1; root diwariskan tanpa rewrite).
|
|
136
|
+
let correlation: ExecutionCorrelation
|
|
137
|
+
try {
|
|
138
|
+
correlation = createChildCorrelation(spec.parent, spec.kind, spec.ownerId)
|
|
139
|
+
} catch {
|
|
140
|
+
return { admitted: false, reason: "invalid parent correlation" }
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// 3. Capability: attenuasi via M6 (tanpa algoritma kedua).
|
|
144
|
+
const attenuated = attenuateGrant(spec.parentGrant, {
|
|
145
|
+
executionId: correlation.executionId,
|
|
146
|
+
ownerId: spec.ownerId,
|
|
147
|
+
capabilities: spec.requestedCapabilities,
|
|
148
|
+
requestedBy: "m7:admitChild",
|
|
149
|
+
})
|
|
150
|
+
if (!("granted" in attenuated))
|
|
151
|
+
return { admitted: false, reason: `capability: ${attenuated.denied}` }
|
|
152
|
+
|
|
153
|
+
// 4. Deadline: ≤ sisa parent; tak diketahui = ceiling backend existing.
|
|
154
|
+
// Evidence ceiling: SUB_AGENT_TIMEOUT_MS (task.ts:259 factory timeout).
|
|
155
|
+
const backendCeiling = LIMITS.SUB_AGENT_TIMEOUT_MS
|
|
156
|
+
let effectiveDeadlineMs: number | null
|
|
157
|
+
if (spec.parentRemainingDeadlineMs !== undefined) {
|
|
158
|
+
if (spec.parentRemainingDeadlineMs <= 0)
|
|
159
|
+
return { admitted: false, reason: "parent deadline already exhausted" }
|
|
160
|
+
const wanted = spec.deadlineMs ?? spec.parentRemainingDeadlineMs
|
|
161
|
+
if (wanted <= 0) return { admitted: false, reason: "invalid deadlineMs" }
|
|
162
|
+
effectiveDeadlineMs = Math.min(wanted, spec.parentRemainingDeadlineMs)
|
|
163
|
+
} else if (spec.deadlineMs !== undefined) {
|
|
164
|
+
if (spec.deadlineMs <= 0) return { admitted: false, reason: "invalid deadlineMs" }
|
|
165
|
+
effectiveDeadlineMs = Math.min(spec.deadlineMs, backendCeiling)
|
|
166
|
+
} else {
|
|
167
|
+
effectiveDeadlineMs = backendCeiling
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// 5. Budget subtractive: fixed-cap consumptive (model current) + reservasi
|
|
171
|
+
// bila sisa diketahui. Tanpa sisa = cap mode (jujur: tanpa proteksi overcommit
|
|
172
|
+
// antar sibling — caller harus merangkai parentRemainingAfter).
|
|
173
|
+
const cap = modeStepCap(spec.mode)
|
|
174
|
+
const wantedSteps = spec.budgetSteps ?? cap
|
|
175
|
+
if (!Number.isFinite(wantedSteps) || wantedSteps <= 0)
|
|
176
|
+
return { admitted: false, reason: "invalid budgetSteps" }
|
|
177
|
+
const boundedSteps = Math.min(Math.floor(wantedSteps), cap)
|
|
178
|
+
let parentRemainingAfter: number | null = null
|
|
179
|
+
let budgetSteps = boundedSteps
|
|
180
|
+
if (spec.parentRemainingSteps !== undefined) {
|
|
181
|
+
if (spec.parentRemainingSteps <= 0)
|
|
182
|
+
return { admitted: false, reason: "parent budget exhausted" }
|
|
183
|
+
budgetSteps = Math.min(boundedSteps, Math.floor(spec.parentRemainingSteps))
|
|
184
|
+
if (budgetSteps <= 0) return { admitted: false, reason: "parent budget exhausted" }
|
|
185
|
+
parentRemainingAfter = spec.parentRemainingSteps - budgetSteps
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// 6. Workspace: di dalam parent (M6; enforcement symlink milik jail).
|
|
189
|
+
let workspaceOk = false
|
|
190
|
+
try {
|
|
191
|
+
workspaceOk = isPathWithin(spec.parentWorkspaceCwd, spec.workspaceCwd)
|
|
192
|
+
} catch {
|
|
193
|
+
workspaceOk = false
|
|
194
|
+
}
|
|
195
|
+
if (!workspaceOk) return { admitted: false, reason: "workspace outside parent scope" }
|
|
196
|
+
|
|
197
|
+
// 7. Detached: eksplisit + lengkap, atau attached default.
|
|
198
|
+
let detached: DetachedSpec | null = null
|
|
199
|
+
let cancelScope: ChildAdmission["cancelScope"] = { kind: "parent" }
|
|
200
|
+
if (spec.detached !== undefined) {
|
|
201
|
+
const d = spec.detached
|
|
202
|
+
if (!d.ownerId || !Number.isFinite(d.ttlMs) || d.ttlMs <= 0 || !d.killPath)
|
|
203
|
+
return { admitted: false, reason: "detached requires ownerId + positive ttlMs + killPath" }
|
|
204
|
+
detached = { ownerId: d.ownerId, ttlMs: d.ttlMs, killPath: d.killPath }
|
|
205
|
+
cancelScope = { kind: "detached", owner: d.ownerId }
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return {
|
|
209
|
+
admitted: true,
|
|
210
|
+
admission: {
|
|
211
|
+
correlation,
|
|
212
|
+
grant: attenuated.granted,
|
|
213
|
+
effectiveDeadlineMs,
|
|
214
|
+
budgetSteps,
|
|
215
|
+
parentRemainingAfter,
|
|
216
|
+
workspaceCwd: spec.workspaceCwd,
|
|
217
|
+
cancelScope,
|
|
218
|
+
detached,
|
|
219
|
+
},
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** True bila detached melewati TTL (observasi; eksekusi kill milik owner). */
|
|
224
|
+
export function isDetachedExpired(
|
|
225
|
+
spawnedAtMs: number,
|
|
226
|
+
ttlMs: number,
|
|
227
|
+
nowMs: number = Date.now(),
|
|
228
|
+
): boolean {
|
|
229
|
+
if (!Number.isFinite(spawnedAtMs) || !Number.isFinite(ttlMs) || ttlMs <= 0) return true
|
|
230
|
+
return nowMs - spawnedAtMs >= ttlMs
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export interface ChildCancelPlan {
|
|
234
|
+
/** Live child → cancel REQUEST (otoritas transisi milik M8, bukan di sini). */
|
|
235
|
+
readonly cancelRequested: readonly string[]
|
|
236
|
+
/** Terminal child → NO-OP + audit (tak pernah mutasi terminal). */
|
|
237
|
+
readonly noopTerminal: readonly string[]
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Propagation planning parent-cancel (murni observasi): terminal tetap terminal;
|
|
242
|
+
* live mendapat cancel request. TAK ADA mutasi state di sini (M8 yang mengeksekusi).
|
|
243
|
+
*/
|
|
244
|
+
export function planParentCancellation(
|
|
245
|
+
children: readonly { executionId: string; terminal: boolean }[],
|
|
246
|
+
): ChildCancelPlan {
|
|
247
|
+
const cancelRequested: string[] = []
|
|
248
|
+
const noopTerminal: string[] = []
|
|
249
|
+
for (const c of children) {
|
|
250
|
+
if (!c || typeof c.executionId !== "string") continue
|
|
251
|
+
if (c.terminal) noopTerminal.push(c.executionId)
|
|
252
|
+
else cancelRequested.push(c.executionId)
|
|
253
|
+
}
|
|
254
|
+
return {
|
|
255
|
+
cancelRequested: Object.freeze(cancelRequested),
|
|
256
|
+
noopTerminal: Object.freeze(noopTerminal),
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** Ringkasan observasi tree (metadata; bukan graph persistence). */
|
|
261
|
+
export function describeChildTree(
|
|
262
|
+
parentExecutionId: string,
|
|
263
|
+
children: readonly { executionId: string; kind: string; detached: boolean }[],
|
|
264
|
+
): {
|
|
265
|
+
readonly parent: string
|
|
266
|
+
readonly count: number
|
|
267
|
+
readonly children: readonly { executionId: string; kind: string; detached: boolean }[]
|
|
268
|
+
} {
|
|
269
|
+
return Object.freeze({
|
|
270
|
+
parent: parentExecutionId,
|
|
271
|
+
count: children.length,
|
|
272
|
+
children: Object.freeze(children.map((c) => Object.freeze({ ...c }))),
|
|
273
|
+
})
|
|
274
|
+
}
|