Skip to content

fix(runtime): identify workspaces by UUID marker - #1369

Merged
likun666661 merged 4 commits into
apache:mainfrom
YayoiNanoka:fix/workspace_UUID
Jul 23, 2026
Merged

likun666661 merged 4 commits into
apache:mainfrom
YayoiNanoka:fix/workspace_UUID

Conversation

@YayoiNanoka

Copy link
Copy Markdown
Contributor
展开中文说明

概要

  • 使用带版本的 UUID marker 替代基于 dev、ino 和路径的 workspace identity
  • workspace 路径变化后仍允许 safe-boundary resume,同时保留执行前的 UUID 重校验
  • 为 workspace marker 和 storage-root marker 增加显式的导入收养机制
  • 通过 marker alias 迁移旧的 fs:{dev}:{ino}:{path} AgentRun identity
  • 删除 pendingCwdReminder 提示词注入 workaround,同时保证旧 session 仍可读取

实现细节

Workspace identity 现在保存在 .maka-workspace.json 中,格式为 workspace:v1:<uuid>。Marker 使用原子方式创建,并对文件大小、格式和 identity 冲突进行严格校验。

Workspace 路径仍会保留在诊断信息中,但不再决定 continuation 能否恢复。Runtime 在 backend 执行前仍会重新校验 UUID,因此如果 marker 在规划完成后被替换,恢复会安全失败。

导入收养机制会保留复制过来的 workspace UUID。对于尚未包含 marker 的旧 bundle,importer 可以显式登记原来的 filesystem anchor。Storage root 也增加了独立的显式收养入口:保留原有 rootId,同时将解压目录的新 dev 和 ino 写回 marker。普通 storage-root resolution 的行为不变,在未经过显式收养前仍会拒绝复制出来的 root。

CLI/Desktop 的 /move 现在只持久化新的 cwd。修改 cwd 本来就会使当前 backend 失效,因此下一轮会按照更新后的 session header 重建 backend。旧 session 文件中的 pendingCwdReminder 仍可读取,但 normalize 时会丢弃该字段。

兼容性与安全性

  • 继续使用 AgentRun header 现有的 workspaceIdentity 字段
  • 匹配的旧 filesystem anchor 会迁移到 UUID identity
  • 冲突或格式错误的 marker 会安全失败
  • storage-root adoption 必须提供预期的持久化 rootId
  • workspace marker 经复制或归档后仍保留原 UUID 和文件内容
  • 旧 pendingCwdReminder 字段可读取,并在下次规范化写入时移除

测试

  • Runtime continuation 与 SessionManager:231 项通过
  • CLI session driver:41 项通过
  • Workspace marker 创建、归档保留、导入收养、旧 identity 迁移和冲突测试
  • Storage-root 导入收养和冲突测试
  • 旧 session normalize 兼容测试
  • Core、Storage、Runtime、CLI 和 Desktop 主进程类型检查
  • Biome lint 与 git diff --check

尚未运行完整的 Desktop Playwright E2E。现有 E2E 中也还没有 tar/untar 跨 sandbox resume 场景。

Summary

  • replace filesystem-based workspace identity (dev / ino / path) with a versioned UUID marker
  • allow safe-boundary resume after workspace path changes while retaining UUID revalidation before execution
  • add explicit import adoption for workspace and storage-root markers
  • migrate legacy fs:{dev}:{ino}:{path} AgentRun identities through marker aliases
  • remove the pendingCwdReminder prompt-injection workaround while preserving legacy session readability

Details

Workspace identity is now stored in .maka-workspace.json as workspace:v1:<uuid>. Marker creation is atomic, bounded, and rejects malformed or conflicting markers.

Workspace paths remain available for diagnostics but no longer determine whether a continuation can resume. Runtime still revalidates the UUID immediately before backend execution, so replacing the marker after planning fails closed.

Import adoption preserves copied workspace UUIDs. Pre-marker imports may explicitly register their legacy filesystem anchor. Storage roots receive a separate explicit adoption path that preserves rootId while rebinding the extracted directory’s host-local dev and ino. Normal storage-root resolution continues to reject copied roots until that import boundary is invoked.

The CLI/Desktop /move path now persists only the new cwd. Updating cwd already invalidates the active backend, so the next turn rebuilds it from the updated session header. Existing session files containing pendingCwdReminder remain readable, but normalization drops the legacy field.

Compatibility and safety

  • existing AgentRun headers keep using the workspaceIdentity field
  • matching legacy filesystem anchors migrate to the UUID identity
  • conflicting or malformed markers fail closed
  • storage-root adoption requires the expected durable rootId
  • copied workspace markers retain their UUID and archive bytes
  • old pendingCwdReminder fields are accepted on read and removed on the next normalized write

Tests

  • Runtime continuation and SessionManager suite: 231 passed
  • CLI session driver suite: 41 passed
  • workspace marker creation, archive preservation, import adoption, legacy migration, and conflict tests
  • storage-root import adoption and conflict tests
  • legacy session normalization test
  • Core, Storage, Runtime, CLI, and Desktop main-process type checks
  • Biome lint and git diff --check

Full Desktop Playwright E2E was not run. The existing E2E suite does not currently contain a tar/untar cross-sandbox resume scenario.

Closes #1337

@YayoiNanoka
YayoiNanoka marked this pull request as ready for review July 23, 2026 05:35

@likun666661 likun666661 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found one blocking issue in the workspace marker adoption path.

adoptWorkspaceIdentityOnImport can construct a candidate marker that violates the reader's own invariants: at most 32 legacy aliases and at most 4 KiB of serialized marker data. replaceWorkspaceMarker renames that candidate over the durable marker before readWorkspaceMarker validates it.

I reproduced this with a valid marker containing 32 aliases. Adding one distinct legacyWorkspaceIdentity persisted a marker containing 33 aliases; the adoption call then threw invalid_workspace_marker, and every subsequent resolveWorkspaceIdentity call failed on the now-invalid durable marker. The same problem occurs when individually valid aliases produce a serialized marker larger than 4 KiB.

Please validate or bound the complete serialized candidate before replacing the existing marker. A regression test should also verify that a rejected adoption preserves the previous marker bytes exactly.

The overall direction looks sound: UUID revalidation, one-way legacy alias migration, diagnostic-only path changes, explicit storage-root adoption, and removal of the prompt-injection workaround all align with the intended cross-sandbox resume model. The marker write-boundary issue should be fixed before approval.

@YayoiNanoka

Copy link
Copy Markdown
Contributor Author

Thanks for catching this — fixed in fbbfac5f.

The complete marker candidate is now validated before any temporary file is opened or the durable marker is replaced:

  • the full marker shape is checked, including the 32-alias limit, alias validity, and uniqueness
  • the exact JSON + "\n" payload is serialized once and bounded to 4 KiB using its UTF-8 byte length
  • the same validated payload is then written by both the create and replace paths

I also added regression coverage for both failure modes:

  • adding a distinct 33rd alias
  • adding an individually valid alias that pushes the complete serialized marker over 4 KiB

Both tests assert invalid_workspace_marker, byte-for-byte preservation of the previous durable marker, and successful resolution of the original UUID after the rejected adoption.

Validation completed: workspace identity tests 5/5, Storage typecheck, Biome lint/format, and git diff --check. GitHub typecheck is also passing; test and E2E checks are still in progress.

展开中文说明

感谢指出这个问题,已在 fbbfac5f 中修复。

现在会在打开临时文件或替换 durable marker 之前校验完整的 marker candidate:

  • 校验完整 marker 结构,包括最多 32 个 alias、每个 alias 的合法性以及是否重复
  • 只序列化一次最终实际写入的 JSON + "\n",并按 UTF-8 字节数检查不超过 4 KiB
  • create 和 replace 两条路径都会写入同一份已经验证过的内容

同时补充了两种失败场景的回归测试:

  • 已有 32 个 alias 时添加第 33 个不同 alias
  • 新 alias 单独合法,但加入后使完整序列化 marker 超过 4 KiB

两个测试都会验证返回 invalid_workspace_marker、原 durable marker 的字节完全不变,并且拒绝 adoption 后仍能解析出原来的 UUID。

已完成验证:workspace identity 测试 5/5、Storage typecheck、Biome lint/format 以及 git diff --check。GitHub typecheck 也已通过,test 和 E2E 检查仍在运行。

@likun666661
likun666661 merged commit 015f71b into apache:main Jul 23, 2026
3 checks passed
@Astro-Han

Copy link
Copy Markdown
Contributor

A portable UUID marker fixes the immediate identity mismatch caused by path, device, and inode changes. I think the issue can be smaller, though, and the runtime boundary needs one correction before this lands.

With safe-boundary resume enabled, startTurn() resolves or creates the workspace marker before it creates the AgentRun, stores the user message, or calls the backend. I reproduced two consequences:

  • The first turn in a read-only workspace fails with EACCES.
  • A clean Git repository gains ?? .maka-workspace.json, so /move reports a false dirty warning.

Failure to establish resumability should make a new run non-resumable, not prevent it from running. Actual resume and pre-execution identity revalidation should remain fail closed. The marker owner should also keep its file out of user-visible Git status. Adding separate exceptions to the TUI and Desktop Git checks would duplicate the rule.

Since this format has not shipped, I would remove migration from the old fs:{dev}:{ino}:{path} identity instead of hardening it. legacyAnchors adds persisted state, repeated marker rewrites, alias limits, planner aliases, and Number versus BigInt compatibility. I also reproduced an adoption call publishing a 4123-byte marker that the reader immediately rejects, leaving the workspace marker unusable. Removing pre-release migration reduces the marker to a schema version and UUID and removes this failure path. If legacy migration must stay, the final serialized marker must be validated before publication, and a failed update must preserve the previous marker bytes.

Storage-root adoption is explicitly listed in this issue, but the PR excludes the real Session Bundle importer and nothing in production calls the new API. I would prefer to add adoption with that importer, where the expected root ID, error handling, and end-to-end behavior are known. This is a scope suggestion rather than an implementation finding.

The workspace and storage-root implementations also duplicate the same bounded, no-follow, synced, atomic marker I/O. They have already drifted: workspace temp-file cleanup ownership starts only after the write completes, unlike the existing storage-root path. A small internal storage primitive for marker reads, atomic publication, and cleanup would remove that duplication while leaving each marker's schema and identity rules with its current owner.

A real tar/untar round trip should be required for this issue. The current archive test uses recursive cp, so it does not verify the behavior named in the issue. A safe-boundary clean-Git /move test would also be useful, since it catches the current user-visible warning.

中文意见

我倾向先把这个 issue 收回到用户真正需要的行为:workspace 换了路径或沙箱以后,恢复逻辑仍能认出它。

现在普通 turn 在真正开始前就必须写入 marker。这样有点本末倒置。只读目录会直接无法聊天;普通干净仓库则会多出一个未跟踪文件,导致 /move 把 Maka 自己制造的变化误报成用户改动。我认为更合理的边界是:新 turn 写不了 marker 仍然可以运行,只是这次不支持断点续跑;真正恢复时再严格拒绝不可靠的 identity。marker 对 Git 的影响也应该在创建它的地方统一处理,不要让 TUI 和 Desktop 各自加特判。

项目还没有发布的话,我不太建议继续维护旧的 fs:{dev}:{ino}:{path} 迁移。为了它,现在多了 legacyAnchors、追加和重写、数量与长度限制、planner alias,以及 Number 和 BigInt 的历史差异。收益只是让开发阶段留下的旧 pending run 继续恢复,失败时本来也会安全停住,不会损坏 session 正文。直接删除这条兼容路径,marker 就只剩版本和 UUID,代码和状态都会少很多。

我本地还复现了一个结果:adoption 可以先写入一个 4123-byte 的 marker,然后读端才发现超限。报错时原文件已经被替换,后续每次读取都会继续失败。如果作者决定保留 legacy migration,这里至少必须改成发布前校验,失败时原 marker 不动。

storage-root adoption 的确是 issue 明写的 scope。不过真实的 Session Bundle importer 又被排除在本 PR 之外,目前新增 API 也没有生产调用者。我更倾向等 importer 真正落地时一起实现;到那时 expected root ID、失败处理和端到端测试都有真实上下文。这个属于 scope 建议,可以由 issue 作者决定是否接受。

另外,两类 marker 正在分别维护同一套安全文件操作,包括大小限制、不跟随 symlink、临时文件、fsync、原子发布和失败清理。第二份实现已经出现清理时机不一致。可以收敛一个很小的 storage 内部 primitive,只管 marker 字节的安全读取和原子写入,UUID 与 storage-root 的业务规则仍留在各自模块。

最后,tar/untar 是 issue 明确写出的目标,应该用真实归档往返验证。当前递归 cp 的测试不够。启用 safe-boundary 后跑一次 clean Git repo 的 /move 也值得补上,但优先级低于 tar/untar。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(runtime): identify workspaces by UUID marker instead of fs dev/ino/path

3 participants