Skip to content

fix(runtime): reject hard-linked JSONL append targets - #9579

Open
Duang777 wants to merge 1 commit into
mainfrom
cursor/reject-hardlinked-jsonl-append-3116
Open

Duang777 wants to merge 1 commit into
mainfrom
cursor/reject-hardlinked-jsonl-append-3116

Conversation

@Duang777

Copy link
Copy Markdown
Collaborator

Goal And Delivered Outcome

  • Outcome basis / optional anchor: self-found defect on current main. No issue is claimed or closed.
  • Goal/source and gap: shared JSONL append followed hard links and symbolic links, so quota, checkpoint, and closeout history could be appended onto an inode that also lives outside the goal runs directory. atomicWriteText / durableWriteJson already retarget the directory entry. PRs fix(quota): reject hard-linked quota artifacts inside the runs directory #9466 and fix(control-plane): reject hard-linked checkpoint artifacts #9512 closed exclusive-file holes on some artifact readers and writers, and accounting already rejects a hard-linked run index, but appendJsonLine / appendJsonLineSync and the checkpoint, monitor-poll, and closeout history appends did not.
  • Observable before → after, with the validation row that proves it: before, an outside sentinel hard-linked or symlinked to the append target gained the new JSON line, and inspectCheckpointReplay accepted a hard-linked or symlinked index.jsonl as committed history. After, those appends and that replay fail closed, the outside sentinel is unchanged, and an exclusive file is still created and still accepts a row after an unterminated tail.
  • Issue/task and intended base: none. Base is main at 811c1a1f66652d3fc3b9830d467e14b45b7423c1.

Author Declaration

Implemented against

  • Specification and revision: no written specification; the request in this PR is the basis
  • Criteria:
Criterion (spec clause) Disposition Symbol / path Test or command
Append only to an exclusive regular file; reject hard links (nlink === 1) and symbolic links (O_NOFOLLOW when available, plus lstat / fstat) implemented appendJsonLine, appendJsonLineSync in loopx/control_plane/effect_runtime_io.ts tests/control_plane_ts/effect_runtime_io.test.ts
Creating a new file and appending after an unterminated tail on an exclusive file stay allowed implemented same helpers same file, existing unterminated-tail tests plus create-and-append tests
A caller that appends run history must not write through a hard-linked index.jsonl implemented evaluateQuotaMonitorPollCommit via the shared helper tests/control_plane_ts/quota_monitor_poll_commit.test.ts
Checkpoint index read must fail closed on a hard-linked or symlinked index.jsonl so replay cannot trust shared history implemented indexBytes in loopx/control_plane/goals/checkpoint_commit.ts tests/control_plane_ts/checkpoint_commit.test.ts
Callers that map append failure to an unknown commit outcome still fail closed implemented commitCheckpoint already maps any append throw to checkpoint_commit_unknown; the helper throws before writing covered by the helper rejection; no contract loosened
Closeout history reader fail-closed on a pre-seeded shared index or event log deferred rows in loopx/control_plane/quota/execution_loss_closeout.ts not changed; append itself now fails closed when the row is absent
  • Self-check before submission: reproduced write-through on unfixed main, confirmed the new tests failed before the fix and passed after, ran the touched suites plus quota spend/void commits, and typechecked the control-plane project. Did not run ruff (no Python diff) or loopx canary premerge.

Scope And Continuation

  • Completed scope and remaining work: the shared append helper rejects a non-exclusive target before any byte is written. Checkpoint replay no longer treats a hard-linked or symlinked run index as local history. Closeout's rows() reader can still treat a pre-seeded hard-linked or symlinked index or event log as already recorded and skip the append. That reader was left unchanged.
  • Slice boundary / successor: complete for the append write-through and the checkpoint index reader. The closeout skip-if-present reader is the remaining gap; no issue was opened.

Validation

  • Tested revision: 715ca39a2df31d1d053a70030051457de1d76db9
  • Run state: finished
  • Input classes: synthetic
Check kind Result Public-safe evidence / limitation
regression_parity failed On unfixed main, the new hard-link and symlink cases failed before any product change: four appendJsonLine / appendJsonLineSync cases and the monitor-poll commit case reported Missing expected rejection; two inspectCheckpointReplay index cases reported Missing expected exception. A separate reproduction showed the outside sentinel gaining the JSON line and checkpoint replay returning success.
unit passed After the fix, node --no-warnings --experimental-sqlite --experimental-strip-types --test --test-concurrency=4 on effect_runtime_io.test.ts, checkpoint_commit.test.ts, quota_monitor_poll_commit.test.ts, quota_spend_commit.test.ts, and quota_void_commit.test.ts: 115 tests, 115 passed, 0 failed.
static passed npx tsc --project tsconfig.control-plane.json --noEmit exited 0.
static not_run Ruff not run; this diff has no Python. loopx canary premerge not run.
  • Coverage and gaps: the helper tests cover hard-link refusal, symlink refusal, new exclusive files, and the existing unterminated-tail append. The monitor-poll test is the caller-level append. The checkpoint tests cover the index reader. Closeout's pre-seeded shared-history skip is untested and unchanged. Hard-link cases skip when the platform cannot create them. No real backend was required.

See validation disclosure guidance.

Frontend / Visual Evidence

  • UI impact: none
  • Before: N/A
  • After: N/A
  • First viewport and preview: N/A
  • States and viewports shown: N/A
  • Source data: none
  • Attention review: N/A
  • Craft review: N/A

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Refactoring (no functional changes)
  • Documentation update
  • Test update

LoopX Area

  • Control plane (goals, todos, quota, scheduler, registry, runtime)
  • Benchmark boundary (adapters, runners, verifiers, scoring, evidence)
  • Capability or extension (providers, adapters, skills)
  • Public docs or presentation surface (README, protocols, dashboard)
  • Build, packaging, installer, or CI
  • Host or runtime integration

Technical Direction

  • Direction / acceptance reference, when applicable: Core control-plane hardening. Runs-directory history stays an exclusive regular file, matching the accounting artifact contract.

Shared-authority RFC fixture impact

N/A

  • Production-scale fixture schema: N/A
  • Semantic dimensions changed, or reviewed no-impact rationale: N/A
  • Provider conformance arms run: N/A
  • Read-only legacy/file/PostgreSQL three-arm rehearsal (required for promotion, runtime-routing, or compatibility-projection changes): N/A

Boundary Checklist

  • Neither the diff nor this PR body/comments/attachments disclose private state, credentials, raw traces or verifier output, internal links, or local machine paths (including .loopx/, .codex/goals/, and live ACTIVE_GOAL_STATE.md).
  • I did not duplicate maintainer-owned benchmark work unless a maintainer split out a public issue for it.
  • I kept the change scoped to the linked issue/task.
  • Existing English/Chinese RFC pairs (including ledger entries) both have a diff, and the local premerge check python3 scripts/check_rfc_bilingual.py --base origin/main --head HEAD passes, or no paired RFC changed.
  • I completed the visual evidence section for UI changes, or marked UI impact none.
  • Every commit includes a DCO Signed-off-by trailer (git commit -s).

Residual risks

  • A hard link created after fstat and before the append write can still alias the inode. O_NOFOLLOW closes a symlink swap on platforms that provide it; where it is absent, lstat then open still has that window.
  • Windows nlink is not a reliable hard-link count, and tests skip hard-link cases when link fails (EPERM, ENOTSUP, EOPNOTSUPP, EXDEV, ENOSYS).
  • execution_loss_closeout still reads index.jsonl and the goal event log without an exclusive-file check. The append now fails closed when the row is absent, but a pre-seeded matching row can still be trusted and the append skipped.

Written by: duang777 (@Duang777)

Open in Web Open in Cursor 

appendJsonLine and appendJsonLineSync opened history with a+ and followed
hard links and symbolic links, so quota, checkpoint, and closeout appends
could mutate an inode outside the goal runs directory. Open only an
exclusive regular file, and fail closed when checkpoint history is shared.

Signed-off-by: Duang777 <duangjl007@gmail.com>
@BigDataDZ

Copy link
Copy Markdown
Contributor

独立评审(native Windows)——代码与实测均通过,暂不给正式批准

精确核对 head:715ca39a2,基线 main 811c1a1f6。

代码评审

  • 追加层(effect_runtime_io.ts)与复验层(checkpoint_commit.ts 的 indexBytes)用同一套三段式栅栏:lstat 先拒符号链接 / nlink !== 1 / 非常规文件;O_NOFOLLOW 打开;对打开后的句柄重新验证独占性,并与 lstat 计划做 dev/ino 连续性比对——检查-打开竞态被封闭。创建路径双向处理竞态(EEXIST → 无创建重试;ENOENT → 带创建重试)。dev/ino 为 0 的异常文件系统跳过比对,合理。
  • Windows 可用性:NTFS 支持硬链接,Node 在 Windows 上报告真实的 nlink 与 dev/ino,fs.link 无需特权——本检查在 Windows 上同样有效,不是 POSIX-only。
  • 既有行为保留:新文件创建、未终结尾后续行(原有测试继续通过)。

实测(native Windows,Node 24.20,NTFS)

  • 缺陷在 main 上真实存在:目录外哨兵硬链接到 index.jsonl 后,对目录内路径执行 appendJsonLineSync 使哨兵副本获得了追加行(2 行,outside_gained_the_appended_row: true)。
  • 本 head 上三个被改测试文件 → 47 passed / 0 failed / 0 skipped,其中硬链接拒绝用例在真实 NTFS 硬链接上执行(不是模拟)。

CI

该 head 上 Python 分片被跳过(kernel-contracts 类型检查失败的连锁,3834 个错误属仓库级、与本 PR 无关),无完整套件结果——按我的惯例不给正式批准,但代码与实测均无发现,机器人评审通过后维护者可放心合并。

未验证:POSIX 运行;O_NOFOLLOW 在 Windows 上不可用时的符号链接拒绝依赖 lstat/fstat 路径(实测覆盖的是硬链接路径,符号链接用例在本机未单独执行)。

@loopx-agent loopx-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewer: model_agent · gpt-6.2-sol · OpenAI · runtime_reported · xhigh

动机 / Motivation

需要依靠当前 Goal 的本地运行记录判断额度、监控和 checkpoint 的操作者。 追加或读取已有运行历史时,路径可能指向共享文件。 当前提交的七个反例均按预期拒绝,测试确认关联文件保持原字节,独占文件的创建与未终止尾部追加通过。 交付范围是共享 JSONL 追加和 checkpoint index 的读回,执行丢失收口的既有匹配读取单独保留。 执行丢失收口对已有匹配行的信任边界仍待单独验证。

改动思路 / Approach

复用现有同步和异步追加入口,打开前检查目录项,打开后校验实际描述符的常规文件、引用数和可用的文件身份,再追加或读取。 追加规则由共享 I/O 层持有,checkpoint 保留自己的不确定提交诊断和原 Turn 读回要求。 交付范围是共享 JSONL 追加和 checkpoint index 的读回,执行丢失收口的既有匹配读取单独保留。 appendJsonLine appendJsonLineSync readExclusiveHistory evaluateQuotaMonitorPollCommit

具体改动 / Changes

精确提交 715ca39,基线 811c1a1。核对了全部 5 个净路径(+381/-13)。两处生产文件处理打开前后身份及已有记录读取,另三处测试覆盖追加、checkpoint 和 monitor-poll。

  • EXCLUSIVE-JOURNAL-APPEND。同一组 115 项测试用于基线时,七个软链、硬链以及 checkpoint 读回反例未按预期拒绝。其余 108 项通过。当前提交的七个反例均按预期拒绝,测试确认关联文件保持原字节,独占文件的创建与未终止尾部追加通过。
  • CHECKPOINT-REPLAY-READBACK。精确头五个 TS 套件 115 项通过、无跳过。两端三份 Python 套件各 80 项通过。checkpoint 保持 checkpoint_commit_unknown 和读回原 Turn 的要求。已有准入、回执、锁和幂等重试继续由现有 owner 处理。

原生 monitor-poll 提交及 checkpoint 读回的反例通过。已有 CLI 侧的收口和额度正反例在基线与精确头各 80 项通过。 控制面 TypeScript 类型检查、Git 净差及标准本地预合并通过。 验收依据

对主干的风险 / Risks

匹配的既有收口历史读取仍在本切片之外。作者已披露描述符检查后目标引用数仍可能变化。两者均不作为已修复范围。 本轮用 macOS/Node 在隔离合成文件验证,未复测 Windows 和并发换址窗口,也未查询远端 CI。 checkpoint 保持 checkpoint_commit_unknown 和读回原 Turn 的要求。已有准入、回执、锁和幂等重试继续由现有 owner 处理。 精确头五个 TS 套件 115 项通过、无跳过。两端三份 Python 套件各 80 项通过。

我的整体评价 / Verdict

APPROVE。 主干 7 个预期拒绝反例在当前提交全部通过。认可追加与 checkpoint 读回这两个交付边界。

本地运行记录的既有 CLI 入口与恢复方式继续可用。

English verdict: APPROVE at 715ca39. The seven scoped regressions fail on the base and pass here. Healthy append, replay, and accounting checks pass. Pre-existing closeout read coverage and post-check races remain outside this fix.

This branch has not been deployed

No deployments
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.

3 participants