Skip to content

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

Open
Duang777 wants to merge 1 commit into
mainfrom
cursor/quota-hardlink-artifacts-e2d4
Open

Duang777 wants to merge 1 commit into
mainfrom
cursor/quota-hardlink-artifacts-e2d4

Conversation

@Duang777

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: 7e53d02e2778afffa5d96dfeb5abcc3001ddcb90
  • 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.

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