Skip to content

fix(quota): reject hard-linked quota artifacts inside the runs directory - #9466

Merged
huangruiteng merged 2 commits into
mainfrom
cursor/quota-hardlink-artifacts-e2d4
Nov 26, 2026
Merged

huangruiteng merged 2 commits into
mainfrom
cursor/quota-hardlink-artifacts-e2d4

Conversation

@Duang777

@Duang777 Duang777 commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Goal And Delivered Outcome

  • Outcome basis / optional anchor: self-discovered quota-artifact hole next to fix(quota): reject symlink quota artifacts inside the runs directory #9420, rechecked on 57c4c43b5243003c6da1988730c49ac2267626f7
  • Goal/source and gap: fix(quota): reject symlink quota artifacts inside the runs directory #9420 rejects a symlink whose real path leaves the runs directory. A hard link is still a regular file, and realpath / Path.resolve stays on the in-directory name, so an outside zero-slot file that repeats the commit identity was still a readable zero debit.
  • Observable before → after, with the validation row that proves it: before the fix, the new Python observations returned reason None (a readable zero spend, unreadable_event_count == 0). The TypeScript reader returned slots: 0, and receipt replay accepted the hard link. After the fix, the Python ledger reports artifact_missing, spent_slots == 0, unreadable_event_count == 1, and automatic-turn quota_status is blocked_health. The TypeScript reader returns null. Receipt replay rejects the alias with malformed_transaction_receipt. The index row and the outside bytes stay unchanged, and the in-directory name remains a hard link.
  • Issue/task and intended base: no linked issue. Base is main. This PR does not claim or close an existing issue.

动机 / Motivation

#9420 关掉了符号链接:lstat 必须是普通文件,且 resolve() / realpath 必须留在 Goal 的 runs 目录里,否则事件是 artifact_missing,自动执行进入 blocked_health。硬链接仍然是普通文件(S_ISREG / lstat().isFile() 为真),真实路径也停在目录内的那个名字上,所以上述检查会通过。把 runs 目录里的消费记录换成指向目录外、同一 commit identity、slots: 0 的硬链接,仍会被当成可读的零消耗;目录外的那个名字以后还可以被改写,而不必再碰 runs 目录。

Python 滚动账本因此丢掉这笔扣减,Goal 仍可自动执行。TypeScript 的 readQuotaSpendEventFromRun 会把这个零事件交回结算回读和 void。回执重放里的 rejectSymlinkPath 只拒绝符号链接,所以同一别名也能通过回执准入。

改动思路 / Approach

配额产物的预期形态是独占的普通文件:nlink === 1。官方运行时复制用的是 shutil.copytree / fs.cp(新 inode),不是硬链接。

  • Python loopx/control_plane/quota/ledger_readback.py:_regular_artifact_inside_runs_dir 与 _relocated_run_artifact_path 在 lstat 为普通文件之后再要求 st_nlink == 1。否则返回 None。调用方已经把这映射为 artifact_missing。不改写索引,也不改写目录外的文件。
  • TypeScript readQuotaSpendEventFromRun:对候选路径做 lstat,要求 isFile() 且 nlink === 1,然后再要求 realpath 仍在 runs 目录内。路径真实位置离开目录时,仍抛出原来的 quota spend json_path must stay inside the goal runs directory。ENOENT 仍返回 null。硬链接按目录内不可读/缺失处理,返回 null,不把零事件交回去。
  • relocatedRunArtifactPath 同样要求 nlink === 1,因此复制后的同名硬链接不会被当成可迁移的产物。
  • 回执路径上的 rejectSymlinkPath 仍先拒绝符号链接。普通文件且 nlink !== 1 时,用现有的 malformed_transaction_receipt 拒绝,消息为 must not be a hard link。没有新增 lease owner,也没有新的公共错误码。

Author Declaration

Implemented against

  • Specification and revision: no written specification; the request in this PR is the basis. fix(quota): reject symlink quota artifacts inside the runs directory #9420 already requires a regular file whose real path stays inside the runs directory. This change adds the exclusive-directory-entry rule (nlink == 1) on that same reader, on the TypeScript spend reader, and on receipt replay.
  • Criteria:
Criterion (spec clause) Disposition Symbol / path Test or command
An in-directory hard link to an outside zero-slot artifact with the same commit identity is not a readable zero spend implemented ledger_readback.py _regular_artifact_inside_runs_dir; accounting_artifact_transaction.ts readQuotaSpendEventFromRun test_in_directory_hard_link_cannot_zero_a_quota_debit; readQuotaSpendEventFromRun does not accept an in-directory hard link as a zero spend
A copied runs file that is a hard link to an outside zero-slot artifact is not a substitute debit implemented _relocated_run_artifact_path; readQuotaSpendEventFromRun; relocatedRunArtifactPath test_relocated_hard_link_cannot_zero_a_quota_debit; relocated reader and receipt tests
A receipt json_path that is a hard link to an outside alias is rejected with the existing receipt code, and symlink rejection stays in place implemented rejectSymlinkPath spend receipt replay rejects an in-directory hard link to an outside alias; existing symlink receipt tests
  • Self-check before submission: on unmodified main, the new tests failed as a readable zero spend (reason is None, TypeScript event slots: 0, receipt replay resolved). After the fix they pass. Outside bytes and the hard link itself are left in place. Callers that do not pass a runs directory still keep the stored path. No UI change. This is a source-tested proposal, not an installed-product claim. No merge authorization.

Scope And Continuation

  • Completed scope and remaining work: complete within this scope for hard-linked quota JSON artifacts on the Python rolling ledger and the TypeScript spend/receipt readers that open a known runs directory. A caller that does not know the runs directory still opens the stored path. Worktree-lease hard-link checks are a different boundary and are not part of this change.
  • Slice boundary / successor: N/A. The symlink hole from fix(quota): reject symlink quota artifacts inside the runs directory #9420 and this hard-link hole are the same reader contract.

Validation

  • Tested revision: 837357e1a67833411a573ba09d9cd28325895f7c
  • Exact-head refresh: merged latest main (7f7297a790771dab08e2444700f4d76699169950); targeted quota gates and standard premerge were rerun on the reviewed head. Python quota coverage: 140 passed; TypeScript spend coverage: 36 passed; void/settlement readback: 192 passed; premerge: 16 direct/catalog/risk checks passed with 0 failures.
  • Run state: finished
  • Input classes: synthetic
Check kind Result Public-safe evidence / limitation
regression_parity passed Before the fix, uv run --extra test python -m pytest tests/control_plane/test_quota_ledger_relocated_runtime.py::test_in_directory_hard_link_cannot_zero_a_quota_debit tests/control_plane/test_quota_ledger_relocated_runtime.py::test_relocated_hard_link_cannot_zero_a_quota_debit -q --tb=short failed: 2 failed, both assert None == 'artifact_missing'. Before the fix, node --no-warnings --experimental-sqlite --experimental-strip-types --test --test-name-pattern "hard link" tests/control_plane_ts/quota_spend_commit.test.ts failed: 3 failed, 0 passed (in-directory and relocated readers returned slots: 0; receipt replay was not rejected). The later relocated-receipt test, run against the unfixed reader, failed with Missing expected rejection (1 failed, 0 passed).
unit passed After the fix, the relocated Python file: 8 passed. Combined tests/control_plane/test_quota_ledger_relocated_runtime.py, test_quota_ledger_integrity.py, test_quota_slot_accounting.py, test_quota_rolling_window_projection.py, test_history_relocated_run_artifacts.py, and test_usage_summary.py: 140 passed. tests/control_plane_ts/quota_spend_commit.test.ts: 36 passed, 0 failed. quota_void_commit.test.ts and quota_settlement_readback.test.ts: 192 passed, 0 failed.
static passed uv run --extra test ruff check loopx/control_plane/quota/ledger_readback.py tests/control_plane/test_quota_ledger_relocated_runtime.py. npm run typecheck:control-plane (tsc --project tsconfig.control-plane.json --noEmit) exited 0. .venv/bin/loopx canary premerge --from-git-diff --git-diff-base origin/main on this change set: passed, changed_files=4, diff check, RFC bilingual check, py_compile, 8 catalog canaries, 8 risk-profile smokes, failures=0. Passing premerge is not merge authority.
  • Coverage and gaps: Linux only. Windows was not exercised. A filesystem that reports nlink == 1 for a hard link would still hide the alias. Callers that do not supply the current runs directory are unchanged. No UI change.

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: quota ledger readback. A known runs directory must fail closed unless the indexed artifact is an exclusive regular file whose real path stays inside that directory.

Shared-authority RFC fixture impact

  • 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.
  • 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).

Written by: duang777 (@Duang777)

Open in Web Open in Cursor 

A quota artifact that is a hard link to an outside zero-slot file still
passes the symlink checks from #9420. Require one exclusive regular file
before the Python ledger or the TypeScript spend reader treats it as a
debit, and reject the same alias when a receipt is replayed.

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

在已知 Goal runs 目录的配额账本及结算回读中,拒绝不符合约定的文件状态,保留正常消费、撤销与重放。

Python 配额状态、TypeScript 结算回读与 void 调用者依赖同一账本完整性约束。

比较正常消费、搬迁后的普通副本、没有新事务字段的旧记录和重放。另用合成文件元数据核对新增拒绝分支。

正常配额消费、撤销和历史回读保留。新增检查对不可接受的文件元数据走既有错误路径。

验证范围为源代码和隔离本地文件。未操作活跃配额账本。

按基线配额完整性合同核对。 状态数据合同

改动思路 / Approach

Python 账本把不可用产物映射为既有完整性失败。TypeScript 读端和已保存的事务回执复用各自现有错误合同。

检查作用于已知 runs 目录。未传目录的旧入口与无事务字段的 inline 记录由原合同处理。

复用现有文件资格判定和账本完整性错误,保留单一的窄防御分支。

具体改动 / Changes

审查提交 7e53d02e2778afffa5d96dfeb5abcc3001ddcb90,分叉基线 57c4c43b5243003c6da1988730c49ac2267626f7。4 个文件 +391/−27。

关键代码讲解

  • _exclusive_regular_file。判断正常文件及单链接计数。 源码

  • _relocated_run_artifact_path。只接受 Goal runs 中符合约定的普通副本。 源码

  • _regular_artifact_inside_runs_dir。为 Python 配额账本验证已知目录中的候选文件。 源码

  • readQuotaSpendEventFromRun。在边界验证后读取已有消费记录。 源码

  • rejectSymlinkPath。复用既有回执错误合同拒绝不合格文件。 源码

  • quota-artifact-integrity(implemented)。不可用的索引产物走既有完整性失败。 对应 _regular_artifact_inside_runs_dir。

  • readable-copy-and-replay(implemented)。普通副本、旧记录和真实精确效果重放保持。 对应 readQuotaSpendEventFromRun。

对主干的风险 / Risks

两版各 2 项真实 File 账本和 4 项 TypeScript 实际文件测试通过,包含正常提交、断点修复、复制后的重放和旧格式对照。

生产 Python 判定对合成多条目元数据、目录及符号条目均拒绝。TypeScript 读取和回执检查也复用同一个独占普通文件判定。

改动的 Python 文件 Ruff 通过,TypeScript control-plane typecheck 和 git diff --check 通过。标准本地预合并门通过 6 项直接检查和 16 项 smoke。

独立测试覆盖正常生产入口和合成文件元数据。作者另报告完整分支测试。远程 CI、安装态和 Windows 未核验。

我的整体评价 / Verdict

APPROVE。 普通产物、复制和重放语义保留。新增文件资格检查沿用已有完整性失败结果。基线与当前的普通文件路径都完成了相同的消费、撤销及回执测试。

English verdict: APPROVE at 7e53d02. Paired ledger and receipt checks preserve ordinary artifacts, relocation, and exact-effect replay. The patch keeps the new file classification within existing ledger integrity boundaries.

Signed-off-by: huangruiteng <huangrt01@163.com>

@huangruiteng huangruiteng 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-5 · OpenAI · self_reported

动机 / Motivation

维护 Goal 配额账本的执行者和运维者会遇到这个问题:runs 目录中的消费记录若被替换成目录外零消耗文件的硬链接,旧版会把这次扣减读成可读的零消耗。

修复前,目录内硬链接仍被当作普通文件并通过 realpath 检查,slots: 0 可以覆盖真实扣减;修复后,只有链接数为 1 的独占普通文件才会被读取,异常别名通过既有 artifact_missing、null 和回执拒绝路径失败关闭。

可观察结果是 Python 账本返回 artifact_missing 并把 quota 状态置为 blocked_health,TypeScript 消费读取返回 null,回执重放返回既有 malformed_transaction_receipt,同时不修改索引、外部字节或硬链接本身。

本 PR 不改变配额数量、调度策略、租约语义或普通复制的数据格式;没有当前 runs 目录的旧调用、不同平台的链接计数差异以及 Windows 文件系统行为留给对应 owner 和后续平台验证。

改动思路 / Approach

修复把“配额产物必须是已知 runs 目录内、真实路径不逃逸且链接数唯一的普通文件”留在既有 Python 账本和 TypeScript 回执读取边界,没有新增状态文件、错误码、租约或第二套 quota authority。

当前 PR 只收紧 Python rolling ledger、TypeScript spend reader 与 receipt replay 对同一份已索引产物的资格判断;普通写入、搬迁副本、历史记录和精确 replay 继续由原 owner 处理。

调用链从 load_quota_event_from_run 或 readQuotaSpendEventFromRun 开始,先确认候选产物属于当前 Goal 的 runs 目录,再检查 lstat 得到的普通文件类型和 nlink == 1,随后仍由现有 identity、commit 和 quota event 校验决定是否计入。回执路径继续由 validateReceiptPaths 与 rejectSymlinkPath 约束;这里只是把同一“独占普通文件”不变量复用于 JSON、Markdown 和 run index。

flowchart LR
  A[Indexed quota row] --> B{Known runs directory?}
  B -- No --> C[Legacy stored path]
  B -- Yes --> D[Resolve same-goal artifact]
  D --> E{lstat regular and nlink = 1?}
  E -- No --> F[artifact_missing / null / malformed receipt]
  E -- Yes --> G[Existing identity and event checks]
  C --> G
  G --> H[Spend projection or replay]
Loading

上图是依据当前 head 源码整理的示意,不是执行收据。最有力的反对意见是:合法备份若让正常产物保持多个链接,也会被拒绝并进入健康阻塞。这个风险被现有产物契约限制在异常或外部改写形态;官方复制路径创建新 inode,相关正向测试确认普通文件、跨 runtime 普通副本与历史回执仍能工作。

具体改动 / Changes

按当前合同 docs/status-data-contract.md 的 revision 7f7297a790771dab08e2444700f4d76699169950 核对。

quota-ledger-integrity 要求:已索引配额事件若无法回读,必须成为 ledger integrity failure,而不是零消耗,并进入 blocked_health。本 head 将硬链接别名归入不可读产物,并保留既有原因与恢复入口。zero-spend-legality 要求:真正可读、结构合法且明确写为 0 的事件仍是合法零贡献。测试先读取正常二槽文件,再替换成外来零槽硬链接,证明拒绝来自文件资格而不是把合法零事件一概拒绝。legacy-without-runs-directory 不在本 PR 范围:没有当前 runs 目录的调用仍保留原路径行为。

关键代码讲解

  • _exclusive_regular_file 在 Python 边界统一判断 S_ISREG 与 st_nlink == 1,使目录内硬链接不再通过普通文件检查。 精确实现
  • _regular_artifact_inside_runs_dir 继续验证 resolve() 后仍位于 runs 目录,并把失败返回给既有 artifact_missing 路径。 精确实现
  • readQuotaSpendEventFromRun 在读取 JSON 前保存 lstat 结果,既检查真实路径边界,也检查文件与链接计数,硬链接返回 null 而不把零事件交给结算。 精确实现
  • rejectSymlinkPath 在原有符号链接拒绝后,对普通文件复用同一个独占文件判定;违规回执沿用 malformed_transaction_receipt。 精确实现

变更共 4 个文件、+391/-27:两项生产读取修复,两项分别覆盖 Python 真实文件账本和 TypeScript 消费、搬迁、回执重放的回归测试。没有新增模块或持久字段。

对主干的风险 / Risks

最强回归场景是普通配额产物因合法备份而出现 nlink > 1,被新检查判为不可读并让 Goal 进入 blocked_health。触发点集中在已知 runs 目录的产物读取;普通独占文件、跨 runtime 复制后的普通新 inode、历史 inline 记录和精确 effect replay 继续通过。恢复方式是移除异常别名并恢复独占普通文件,随后使用现有账本读回或回执重放;若发现真实环境存在受支持的额外 linked-copy 形态,最小修复应让官方复制 owner 产生独占文件,而不是放宽任意硬链接。

验证在隔离临时目录的真实文件后端执行,没有使用 mock 来决定 postcondition。Python 定向文件 8 项、相关配额链路 140 项和 TypeScript 消费测试 36 项、void/settlement 测试 192 项均通过;control-plane typecheck、Ruff、diff check 和 standard premerge 的 16 项检查通过。远端 CI 当前仍有 queued jobs,因此仅作为诊断项记录,不代替上面的 changed-invariant 证据。

语义与 CI 对齐

当前 head 仍复用现有 quota spend 分类、artifact_missing、blocked_health、malformed_transaction_receipt 和 exact effect identity;新增语义仅是把“独占普通文件”作为既有账本完整性合同的一部分。没有新增公开 actor、authority 或协议词汇,因此无需注册新 vocabulary。所有 pending CI 均保持 UNVERIFIED,合并 readiness 会在发布 review 后单独检查。

我的整体评价 / Verdict

长程效果得到改善:被索引的真实扣减不会再因为 runs 目录中的硬链接别名而静默归零,后续自动执行会按现有健康阻塞规则停下,保留人工可诊断和恢复路径。用户旅程保持原样,没有新增 UI 或额外交互;普通账本读写、状态投影和回执重放的可观察结果不变,只在异常产物上更严格地失败关闭。视觉检查不适用,因为本次没有改变任何渲染界面或 CLI 布局。

本 PR 与 docs/status-data-contract.md 的 ledger integrity 目标一致,且机制规模与问题匹配:生产侧只增加两个小型资格判断,测试覆盖了原先可通过的外部零消耗别名以及必须保持的普通路径。没有发现重复的 authority 或可以进一步删除的新状态层。剩余风险是 Windows 链接计数语义和用户主动保留多链接备份的情形未在本机验证,但它们不阻断当前 Linux 源码与真实临时文件路径的交付判断。

English verdict: APPROVE at 837357e. Reject hard-linked quota artifacts inside a known runs directory while preserving ordinary artifacts, relocated copies, historical records, exact replay, and existing fail-closed diagnostics.

@huangruiteng
huangruiteng merged commit af946a5 into main Nov 26, 2026
11 of 15 checks passed
@huangruiteng
huangruiteng deleted the cursor/quota-hardlink-artifacts-e2d4 branch November 26, 2026 06:50
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