Repository navigation
feat(storage): establish kind-bound Headless execution authority - #1354
Conversation
Astro-Han
left a comment
There was a problem hiding this comment.
I went through the full diff and verified the body's claims against the code, including running the storage, headless, cli, runtime, and runtime-host suites (all green) and checking the lock semantics in fs-native-extensions (flock on macOS, F_OFD on Linux, so cross-process and intra-process exclusion both hold and stale locks die with the process). The substrate is well built: the three-layer pattern (branded capability -> branded lease with per-op revalidation -> frozen facade) is uniform, the adversarial tests are real (forged facades, mid-inspection root swaps, forked writer processes, torn tails), and every fail-closed claim in the body has both code and a regression test behind it. Three P2s and some smaller items.
P2: maka inspect regresses on the default path for existing unmarked roots, with an unactionable error.
The new withInspectCommandStores goes through discoverMarkedStorageRoot (inspect-command.ts:223), so an unmarked root fails with root_unmarked (root-authority.ts:754), which bubbles up as a raw stack trace. Desktop main never calls resolveStorageRoot, so the only thing that marks an existing production root today is the CLI bootstrap (runtime-bootstrap.ts:173). The affected set is narrow: desktop-only users who run maka inspect before ever launching the new TUI or maka run. But that set is exactly the users least able to decode the error, and the implicit migration ("run the TUI once") is written down nowhere: not in the error text, not in docs. Fail-closed is the right call; the error just needs to say what to do, and the migration path needs one written home.
P2: first-touch branding stamps any unmarked root with the caller's kind, without looking at the contents.
ensureRootMarker writes whatever kind the first caller declares (root-authority.ts:652-700). Right now every existing root is unmarked, so a headless command pointed at the wrong path (a mistyped --store or --out aimed at an interactive workspace) brands it headless, and the TUI then refuses to start with root_kind_mismatch. Recovery is deleting the marker by hand, which is undocumented. The in-process side is solid (a live capability detects the swap as root_identity_changed, verified), so this is about the first-touch moment only. If first-touch semantics stay, the risk deserves a written acknowledgment; a minimal content heuristic (e.g., presence of sessions/) would catch the common misfire.
P2: pre-locator headless roots are cut off hard, and the cut is written down nowhere.
Old ledgers are safeFileId(id).jsonl; new ones are sha256 locators with an identity header. Consequences: inspecting or exporting an old task run silently reports not_found (readEventRecords returns [] on ENOENT, task-run-store.ts:183-188, so the data looks gone); on a mixed root where old and new files coexist, listTaskRunIds throws on the old filename (task-run-store.ts:289) and matrix resume hard-fails as a whole; export directory names changed from exports/<rawId> to exports/<locator>, breaking external scripts that reference old paths. Headless roots are benchmark artifacts, so the blast radius is limited, but "this is a deliberate hard cut with no migration" should be stated somewhere: code comment, changelog, or the PR body's compat section.
P3s:
- The two "optional evidence read" sites now use different error policies:
runExperimentWithStoragefails the whole experiment on anyreadRunerror, while task-agent-controller.ts:637 rethrows only authority errors. If intentional, one comment would save the next reader a head-scratch. - Per-op lease revalidation costs roughly six syscalls plus a marker parse (confirmRootSnapshot, root-authority.ts:628-648), and headless appends add a ledger lock, header read, and tail scan per event. Fine on today's paths; worth a written cost contract before hotter domains (usage, memory) copy the pattern.
maka eval task-run inspect/exportdon't wrap authority errors, so unmarked or wrong-kind roots print raw stack traces there too (cli.ts:216, :250).- The body says "complete malformed records fail closed", but the code quarantines them as
event_corruptwith a projection warning instead of rejecting the read (task-run-store.ts:200-211). The code behavior is arguably better; the prose should match it.
What this slice owes the later slices and M5 (none of it written down yet):
- A raw-factory quarantine plan:
createSessionStore/createAgentRunStore/createRuntimeEventStore/createArtifactStoreremain exported and are still used by desktop main and the CLI bootstrap. Headless has a dependency-guard test; the storage export surface and the desktop/cli side have no equivalent. - The marker migration note for existing desktop roots (see the inspect P2 above).
- A deprecation note for the pre-locator ledger and export directory formats.
- Whether headless roots get cross-process ownership: the headless lease never deactivates and has no root-level lock, so two concurrent
maka evalprocesses can still interleave execution-store writes (pre-existing, but M5 should state the answer). - The headless counterpart to the bounded-shutdown lifecycle: interactive leases have close-drain, headless writers have no
close()at all.
On the question that matters most for the next slices: yes, this pattern is uniform enough to copy. The Pick<> lease-bound facade for the headless artifact store (headless-storage.ts:132-149) is a ready-made template for narrowed surfaces. The two things to pin down before the pattern replicates are the revalidation cost contract and a rule for when in-memory fakes that bypass the lease are acceptable.
简体中文
我完整过了一遍 diff,并对照代码验证了 PR 描述里的声明,包括跑通 storage、headless、cli、runtime、runtime-host 五个套件(全部通过),以及核对 fs-native-extensions 的锁语义(macOS 用 flock、Linux 用 F_OFD,跨进程与同进程互斥都成立,stale lock 随进程死亡释放)。这个基板质量不错:三层模式(带品牌的 capability -> 带品牌且每次操作重校验的 lease -> 冻结 facade)一致;对抗性测试是真实的(伪造 facade、inspect 中途置换 root、fork 出来的 writer 进程、torn tail);body 里每条 fail-closed 声明都有代码和回归测试支撑。有三个 P2 和一些较小的项。
P2:maka inspect 在默认路径上对现存未打标 root 回归,且错误不可行动。
新的 withInspectCommandStores 走 discoverMarkedStorageRoot(inspect-command.ts:223),未打标 root 会以 root_unmarked 失败(root-authority.ts:754),并以 raw stack trace 冒泡出来。desktop main 从不调用 resolveStorageRoot,所以现存生产 root 目前唯一的打标点是 CLI bootstrap(runtime-bootstrap.ts:173)。受影响面很窄:只用过 desktop app、在跑新版 TUI 或 maka run 之前先执行 maka inspect 的用户。但这批用户恰好最看不懂这个报错,而隐式迁移路径("跑一次 TUI 即可")没有写在任何地方:不在错误文案里,也不在文档里。fail-closed 是对的,错误文案需要告诉用户怎么办,迁移路径需要一个书面的家。
P2:first-touch 打标不看目录内容,任何未打标 root 会被第一个调用者烙上其 kind。
ensureRootMarker 直接写入第一个调用者声明的 kind(root-authority.ts:652-700)。当前所有现存 root 都是未打标的,所以一个指错路径的 headless 命令(--store 或 --out 误指到 interactive workspace)会把它烙成 headless,之后 TUI 启动抛 root_kind_mismatch。恢复手段是手动删 marker,未文档化。进程内一侧是扎实的(活 capability 能检出置换,报 root_identity_changed,已验证),所以问题只在 first-touch 这一刻。如果保留 first-touch 语义,这个风险值得书面确认;一个最小内容启发式(比如是否存在 sessions/)能挡住常见的误指。
P2:locator 化之前的 headless root 被硬切断,而这个切断没有写在任何地方。
旧 ledger 是 safeFileId(id).jsonl,新的是 sha256 locator 加 identity header。后果:inspect/export 旧 task run 会静默报 not_found(readEventRecords 对 ENOENT 返回 [],task-run-store.ts:183-188,数据看起来消失了);新旧文件共存的混合 root 上,listTaskRunIds 遇到旧文件名直接抛错(task-run-store.ts:289),matrix resume 整体硬失败;export 目录从 exports/<rawId> 变成 exports/<locator>,引用旧路径的外部脚本会断。headless root 是 benchmark 产物,影响面有限,但"这是有意的硬切断、不做迁移"应该写在某个地方:代码注释、changelog 或 PR body 的兼容性说明。
P3:
- 两处"可选证据读取"现在错误策略不同:
runExperimentWithStorage对任何readRun错误都让整个 experiment 失败,而 task-agent-controller.ts:637 只对 authority 错误 rethrow。如果是有意的,一行注释能省掉后来者的困惑。 - 每次操作的 lease 重校验大约六次 syscall 加一次 marker parse(confirmRootSnapshot,root-authority.ts:628-648),headless append 每个事件再加 ledger 锁、header 读取和尾部扫描。今天的路径没问题;更热的 domain(usage、memory)复制这个模式之前,值得写下成本契约。
maka eval task-run inspect/export没有包装 authority 错误,未打标或 kind 错误的 root 在那里也会打 raw stack trace(cli.ts:216、:250)。- body 说"complete malformed records fail closed",代码实际是把它们隔离为
event_corrupt并在 projection 里记 warning,而不是拒绝读取(task-run-store.ts:200-211)。代码行为可以说更好,但文案应该对齐。
这个 slice 欠后续 slice 和 M5 的账(目前都没有写下来):
- raw factory 的 quarantine 计划:
createSessionStore/createAgentRunStore/createRuntimeEventStore/createArtifactStore仍在导出,desktop main 和 CLI bootstrap 还在用。headless 内部有依赖守卫测试,storage 导出面和 desktop/cli 一侧没有等价物。 - 现存 desktop root 的 marker 迁移说明(见上面 inspect 那条 P2)。
- pre-locator ledger 与 export 目录格式的 deprecation 说明。
- headless root 是否需要跨进程 ownership:headless lease 永不失活、没有 root 级锁,两个并发的
maka eval进程仍可能交错写 execution store(升级前即存在,但 M5 应该给出明确答案)。 - bounded-shutdown 生命周期的 headless 对应物:interactive lease 有 close-drain,headless writer 连
close()都没有。
对后续 slice 最重要的问题,结论是肯定的:这个模式足够一致,可以直接复制。headless artifact store 的 Pick<> lease-bound facade(headless-storage.ts:132-149)就是现成的收窄 surface 模板。模式复制之前需要钉死两件事:重校验的成本契约,以及"绕过 lease 的 in-memory fake 何时合法"的规则。
|
@Astro-Han Thank you for the detailed review. I agree that the current Desktop marker gap and the raw CLI stacks are real current-state issues, independent of compatibility with old data. I addressed them as follows:
I did not add a Pre-locator TaskRun ledgers and legacy export paths remain an intentional hard cut. The project and this format are not released, so I documented that choice instead of adding migration, dual-read, or alias behavior. The revalidation cost question remains measurement-driven: before this pattern is copied into a genuinely hot domain, that slice should benchmark its access path rather than weaken the current authority boundary speculatively. 简体中文@Astro-Han 感谢这份细致的审查。我认同 Desktop 当前缺少 marker、CLI 会冒出 raw stack 是真实的当前状态问题,并不依赖旧数据兼容这一前提。 本轮作了以下修正:
我没有增加基于 pre-locator TaskRun ledger 与旧 export path 仍是有意的 hard cut。项目及该格式尚未发布,因此这里只把决策写清,不增加 migration、dual-read 或 alias。revalidation 成本则继续以测量为依据:将该模式复制到真正的 hot domain 前,应由对应 slice benchmark 实际访问路径,而不是现在推测性地削弱 authority boundary。 |
|
LGTM |
English
Summary
This PR establishes the complete kind-bound Storage authority and Headless execution boundary tracked by #1167:
interactiveorheadlessroot lease, and every operation revalidates that lease before reaching the filesystem;maka inspectdiscovers the persisted root kind without mutating the root, acquires the appropriate read authority, and renders expected authority failures as actionable CLI errors rather than fatal stacks.Boundary and decisions
Root kind is a storage fact, not a caller hint.
discoverMarkedStorageRoot()reads and authenticates an existing marker without initializing or repairing the target. A caller cannot inspect an unmarked directory as Headless or temporarily rewrite a marker to select a different owner.Initialization belongs to the writer: the first writer establishes an unmarked root's declared kind, while read paths never initialize or relabel it. Desktop now initializes its shared workspace as
interactivebefore opening its current raw Stores, and E2E reseeding restores the same marker after replacing the workspace. This establishes root identity without changing Desktop's pre-M5 writer ownership. Kind is not inferred from directory-shape heuristics because Interactive and Headless roots share execution Store layout.Execution Store facades are capability-derived rather than structurally trusted. The Storage package creates and authenticates them, binds their methods to the root lease, and keeps the Interactive and Headless types distinct. This closes the path where a correctly shaped object or a raw Store could bypass kind and lease checks.
HeadlessStorageWriteris the production composition boundary for Headless execution. It groups the TaskRun ledger, execution Stores, and the bounded artifact surface under one Headless write lease. Consumers receive the aggregate or a narrowed reader/writer interface; they do not reopen ownership through a path string.TaskRun durability is part of this slice because Headless cannot have a complete writer boundary while its canonical ledger remains process-local. Raw task IDs no longer become path segments. The durable header binds the requested identity to the hashed locator, the native lock serializes independent processes, and repair is limited to an unterminated tail before the next append. Complete malformed event records are retained as
event_corruptentries and surfaced as projection warnings; malformed headers, locator mismatches, and cross-run event identities fail closed.This is an intentional hard cut for an unreleased format. Pre-locator TaskRun ledgers and legacy export paths have no migration, dual-read path, or compatibility alias; development artifacts using those layouts must be regenerated.
Inspection remains observational. Headless inspection opens the authenticated read aggregate; Interactive inspection acquires the existing shared reader lease. Runtime inspection helpers accept narrowed reader contracts and preserve authority failures instead of downgrading them to missing optional evidence.
maka inspectandmaka eval task-run inspect/exportreport an incorrect or uninitialized root with command-specific recovery guidance.Scope
This PR does not move Interactive domains into the Runtime Host, add protocol operations, change Desktop production ownership, or implement the bounded-shutdown foundation. Existing production Interactive entrypoints remain the sole Interactive writers. The shutdown foundation remains the next prerequisite before extracting any Host-owned domain slice.
Validation
Real filesystem and process tests cover kind separation, forged/stale capabilities, observational discovery, independent TaskRun writers, durable identity, torn-tail repair, Headless producer/consumer wiring, production producer-to-CLI-inspect flow, and actionable authority diagnostics. Desktop fixture coverage verifies that workspace replacement preserves the Interactive root marker. Storage, Runtime, Headless, CLI, and Desktop checks pass together with root build, typecheck, lint, and format checks.
Tracked by #1167. Architectural context: #853.
简体中文
概要
本 PR 完整建立 #1167 所追踪的 kind-bound Storage authority 与 Headless execution boundary:
interactive或headlessroot lease 打开,并且每次操作在访问文件系统前都会重新验证 lease;maka inspect在不修改 root 的前提下发现已持久化的 root kind,取得相应 read authority,并将预期内的 authority failure 呈现为可行动的 CLI 错误,而不是 fatal stack。边界与决策
root kind 是 storage fact,而不是 caller hint。
discoverMarkedStorageRoot()只读取并认证已有 marker,不会初始化或修复目标目录。调用方不能把未标记目录当作 Headless inspect,也不能通过临时改写 marker 选择另一个 owner。初始化属于 writer:第一个 writer 为未标记 root 建立其声明的 kind,而 read path 永远不会初始化或改标。Desktop 现在会在打开现有 raw Store 之前将共享 workspace 初始化为
interactive;E2E fixture 替换 workspace 后也会恢复同一 marker。这只建立 root identity,不改变 M5 前 Desktop 的 writer ownership。Interactive 与 Headless root 共用 execution Store 目录布局,因此 kind 不通过目录形状启发式推断。Execution Store facade 来自 capability,而不是依赖结构类型判断。Storage package 负责创建和认证 facade,将每个方法绑定到 root lease,并在类型上区分 Interactive 与 Headless。这关闭了“形状正确的对象”或 raw Store 绕过 kind/lease 检查的路径。
HeadlessStorageWriter是 production Headless execution 的组合边界。它在同一个 Headless write lease 下组合 TaskRun ledger、execution Stores 与有界 artifact surface。消费者接收 aggregate 或收窄后的 reader/writer interface,不会再通过 path string 重新打开 ownership。TaskRun durability 属于本 slice,因为 canonical ledger 如果仍只有进程内互斥,Headless writer boundary 就不完整。raw task ID 不再直接成为路径片段;durable header 会把请求 identity 绑定到 hash locator,native lock 会串行化独立进程,repair 只允许在下一次 append 前修复未终止的 tail。完整但 malformed 的 event record 会保留为
event_corrupt并以 projection warning 呈现;malformed header、locator mismatch 与跨 TaskRun event identity 则保持 fail closed。这是针对未发布格式的有意 hard cut。pre-locator TaskRun ledger 与旧 export path 不提供 migration、dual-read path 或兼容 alias;采用旧布局的开发期产物需要重新生成。
inspect 保持 observational。Headless inspect 打开经过认证的 read aggregate;Interactive inspect 取得已有的 shared reader lease。Runtime inspect helper 接收收窄后的 reader contract,并保留 authority failure,不再把它降级为 optional evidence 缺失。
maka inspect与maka eval task-run inspect/export会针对错误或未初始化的 root 给出与命令对应的恢复提示。范围
本 PR 不把 Interactive 领域迁入 Runtime Host,不增加 protocol operation,不改变 Desktop production ownership,也不实现 bounded-shutdown foundation。现有 production Interactive entrypoint 仍是唯一 Interactive writer。shutdown foundation 继续作为任何 Host-owned domain slice 开始前的下一项前置条件。
验证
真实文件系统与进程测试覆盖 kind 隔离、伪造或 stale capability、observational discovery、独立 TaskRun writer、durable identity、torn-tail repair、Headless producer/consumer 接线、production producer 到 CLI inspect 的完整路径,以及可行动的 authority 诊断。Desktop fixture 覆盖会验证 workspace 被替换后仍保留 Interactive root marker。Storage、Runtime、Headless、CLI 与 Desktop 检查通过,同时根级 build、typecheck、lint 与 format check 通过。
由 #1167 跟踪;架构背景见 #853。