Skip to content

fix(history): reject hard-linked run-index append targets - #9605

Open
Duang777 wants to merge 1 commit into
mainfrom
cursor/reject-hardlinked-run-index-ef1d
Open

Duang777 wants to merge 1 commit into
mainfrom
cursor/reject-hardlinked-run-index-ef1d

Conversation

@Duang777

Copy link
Copy Markdown
Collaborator

Goal And Delivered Outcome

  • Outcome basis / optional anchor: defect found by reading current main (d0df4f970abe5d3bcdb573c7f335b1ab0aad70b4). No issue is closed.
  • Goal/source and gap: append_run_index_record opened goals/<goalId>/runs/index.jsonl with open("a+b") and wrote the JSONL row in place. A same-uid hard link or symlink published settlement and projection bytes outside the runs directory. reserve_run_artifact_paths also treated a dangling Markdown symlink as absent, and Path.write_text then created the link target outside the runs directory.
  • Observable before → after, with the validation row that proves it: before, the append returned successfully and the external sentinel contained the new JSONL row; a dangling index symlink was created outside the runs directory; a dangling Markdown symlink received the rendered note. After, the index append raises OSError before any byte is written, the external inode and bytes stay unchanged, and the runs directory entry is not replaced. A missing index is still created as one exclusive regular file at mode 0o600. A dangling Markdown name is skipped, the run is stored on the next exclusive pair inside the runs directory, and create_exclusive_markdown raises ELOOP if it is aimed at that symlink.
  • Issue/task and intended base: no issue. Base is main. Context only: the TypeScript append work in fix(runtime): reject hard-linked JSONL append targets #9579 does not cover this Python writer, so the same write-through would remain after that change. This PR does not edit fix(runtime): reject hard-linked JSONL append targets #9579 or fix(runtime): reject hard-linked private snapshot files #9560.

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
Fail closed when the index is a symlink, a hard link (st_nlink != 1), or not an exclusive regular file implemented loopx/history.py append_run_index_record tests/test_history_run_index_exclusive_append.py
A missing index.jsonl is created as one exclusive regular file at mode 0o600 implemented _open_exclusive_run_index test_missing_index_is_created_as_an_exclusive_private_file
Keep newline repair, append, and JSON encoding for an exclusive file implemented append_run_index_record test_exclusive_index_keeps_newline_repair_and_appends
Do not publish the row by replacing the linked directory entry implemented same inode check in the link tests hard-link and symlink tests
Dangling Markdown symlink must not create a file outside the runs directory implemented reserve_run_artifact_paths, create_exclusive_markdown reservation tests and the writer tests
  • Self-check before submission: reproduced the index and Markdown write-through on unmodified main, then re-ran the focused tests below. Did not run a full-repo pytest or loopx canary premerge. Did not change checkpoint capture, lease JSON, or TypeScript append helpers.

Scope And Continuation

  • Completed scope and remaining work: the Python run-index append fails closed for a linked target, and the reproduced dangling-Markdown write-through no longer leaves the runs directory. Residual paths are listed under coverage.
  • Slice boundary / successor: complete within this scope. The named write-through is closed. Remaining paths already have owners and are not a required successor of this repair.

Validation

  • Tested revision: e68a7c60ac181b1eca09b839f8c9b171b98c6160
  • Run state: finished
  • Input classes: synthetic
Check kind Result Public-safe evidence / limitation
regression_parity failed on unmodified main, then passed after the fix Before the fix, uv run --extra test python -m pytest -q --no-cov tests/test_history_run_index_exclusive_append.py -k 'not missing_index and not newline_repair' reported 5 failed, 2 deselected. Each index append was treated as success and the external sentinel gained the JSONL row. The dangling Markdown writer test failed because the outside target existed. A separate probe also showed write_reserved_run_artifacts writing the rendered Markdown through that symlink, while a dangling JSON symlink was not followed (O_EXCL advanced to the next stem).
unit passed uv run --extra test python -m pytest -q --no-cov on tests/test_history_run_index_exclusive_append.py, tests/test_history_index_write_serialization.py, tests/test_history_artifact_observation.py, tests/test_history_chronology.py, tests/control_plane/test_run_artifact_reservation.py, tests/control_plane/test_runtime_projection_writer_guards.py, tests/control_plane/test_replan_context_order.py, and tests/architecture/test_posix_flag_portability.py: 99 passed.
static passed uv run --extra test python -m ruff check on the five touched paths: all checks passed.
static passed uv run --extra test python -m mypy --follow-imports=silent on loopx/history.py, loopx/control_plane/runtime/run_artifacts.py, and loopx/control_plane/runtime/runtime_projection_writer.py: no issues. These modules are outside the repository's configured mypy file list.
  • Coverage and gaps: the checks cover direct append, write_reserved_run_artifacts, write_compact_runtime_projection, reservation, newline repair, and create mode. Not run: full-repo pytest and loopx canary premerge. Residual risk: a TOCTOU window remains between lstat and open, and another between fstat and write. O_NOFOLLOW is used when the host provides it. On Windows, where that flag is absent, a symlink or st_nlink != 1 seen by lstat still fails closed, and a reparse point is refused; a link planted after lstat can still be followed. Link tests skip when the filesystem cannot create the link. operator_gate still calls Path.write_text on paths it builds itself. state_refresh still calls Path.write_text, but it reserves through reserve_unique_run_paths, so the dangling Markdown name is skipped first. JSON write_text still replaces the exclusive sentinel this process just created; the dangling JSON symlink probe did not write outside. load_index still follows a symlink when reading. next_run_artifact_paths still uses Path.exists for preview only.

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. No roadmap id; this is the Python run-history writer, not a migration or authority promotion.

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).
Open in Web Open in Cursor 

append_run_index_record opened goals/<goalId>/runs/index.jsonl in place,
so a same-uid hard link or symlink published the JSONL row outside the
runs directory. The append now fails closed unless the path is an
exclusive regular file, and a missing index is still created at mode
0o600. A dangling Markdown symlink is an occupied artifact name and is
not followed.

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

@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

本地运行历史的写入方包括结算、投影和操作回执。 公共 JSONL append 端点直接按路径打开已有文件,普通追加对目标文件的排他性校验不足。 候选在共享追加端点确认独占常规文件,并让选中的历史与投影 Markdown writer 独占创建。 范围是共享 run index 和两个 Markdown 生产路径。保留当前归档格式、锁和正常读回。 正常索引兼容和锁测试在 macOS 执行。异常 descriptor 元数据为受控模拟,Windows 尚未实测。

改动思路 / Approach

共享 history helper 负责追加、补行和索引权限。run_artifacts 负责新文件名和 Markdown 创建,投影仍在原锁下校验读回。 在公共追加所有调用方共享的持久化端点加检查。保留 Markdown 原内容、权限语义及锁。两个 Markdown 生产者使用同一创建函数。 保留现有 owner 和状态合同,按证据限定变更。 主干和候选的正常历史、时间窗、序列化及索引锁各 41 项通过。候选新增的普通创建、已有模式保留和尾行修复三个用例通过。

具体改动 / Changes

关键代码讲解

  • _open_exclusive_run_index。打开后核验包括描述符在内的文件属性。 源码
  • append_run_index_record。原 JSONL 数据和尾部补行。 源码
  • write_reserved_run_artifacts。共享索引锁与历史产物。 源码
  • create_exclusive_markdown。新 Markdown 独占创建。 源码
  • write_compact_runtime_projection。复用 Markdown writer,原索引后置读回。 源码

exclusive-index-append 通过。locked-history-readback 通过。规范修订为 d0df4f9。 验收依据

对主干的风险 / Risks

使用普通单文件与受控 descriptor 元数据验证拒绝分支。出现不可独占状态时写入被拒,文件原内容一致。解除模拟后同一正常追加只写一条。 受控元数据验证了描述符拒绝顺序。更广泛的路径并发和 Windows reparse 分支仍属于未实测范围。

语义与 CI 对齐

不变的调用方经同一索引 helper 获得拒绝语义。新的 Markdown helper 只用于明确列出的生产路径。一般 I/O 失败的索引之前产物写入顺序仍沿用原合同。 Premerge 的 18 项选中检查和 6 项直接检查通过。

我的整体评价 / Verdict

APPROVE。 保留现有 owner 和状态合同,按证据限定变更。 继续复用现有追加 helper、OSError 和相同写锁,避免在调用方另建存储规则。 结论绑定 e68a7c6。

English verdict: APPROVE at e68a7c6. The shared run-index append enforces exclusive regular-file metadata and preserves JSONL append, mode, newline repair and locking behavior. Paired normal history tests and a bounded descriptor-rejection check passed.

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.

2 participants