Repository navigation
fix(quota): reject hard-linked quota artifacts inside the runs directory - #9466
Conversation
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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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]
上图是依据当前 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.
Goal And Delivered Outcome
57c4c43b5243003c6da1988730c49ac2267626f7realpath/Path.resolvestays on the in-directory name, so an outside zero-slot file that repeats the commit identity was still a readable zero debit.None(a readable zero spend,unreadable_event_count == 0). The TypeScript reader returnedslots: 0, and receipt replay accepted the hard link. After the fix, the Python ledger reportsartifact_missing,spent_slots == 0,unreadable_event_count == 1, and automatic-turnquota_statusisblocked_health. The TypeScript reader returnsnull. Receipt replay rejects the alias withmalformed_transaction_receipt. The index row and the outside bytes stay unchanged, and the in-directory name remains a hard link.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),不是硬链接。loopx/control_plane/quota/ledger_readback.py:_regular_artifact_inside_runs_dir与_relocated_run_artifact_path在lstat为普通文件之后再要求st_nlink == 1。否则返回None。调用方已经把这映射为artifact_missing。不改写索引,也不改写目录外的文件。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
nlink == 1) on that same reader, on the TypeScript spend reader, and on receipt replay.ledger_readback.py_regular_artifact_inside_runs_dir;accounting_artifact_transaction.tsreadQuotaSpendEventFromRuntest_in_directory_hard_link_cannot_zero_a_quota_debit;readQuotaSpendEventFromRun does not accept an in-directory hard link as a zero spend_relocated_run_artifact_path;readQuotaSpendEventFromRun;relocatedRunArtifactPathtest_relocated_hard_link_cannot_zero_a_quota_debit; relocated reader and receipt testsjson_paththat is a hard link to an outside alias is rejected with the existing receipt code, and symlink rejection stays in placerejectSymlinkPathspend receipt replay rejects an in-directory hard link to an outside alias; existing symlink receipt testsmain, the new tests failed as a readable zero spend (reason is None, TypeScript eventslots: 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
Validation
837357e1a67833411a573ba09d9cd28325895f7cmain(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.regression_paritypasseduv 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=shortfailed: 2 failed, bothassert 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.tsfailed: 3 failed, 0 passed (in-directory and relocated readers returnedslots: 0; receipt replay was not rejected). The later relocated-receipt test, run against the unfixed reader, failed withMissing expected rejection(1 failed, 0 passed).unitpassedtests/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, andtest_usage_summary.py: 140 passed.tests/control_plane_ts/quota_spend_commit.test.ts: 36 passed, 0 failed.quota_void_commit.test.tsandquota_settlement_readback.test.ts: 192 passed, 0 failed.staticpasseduv 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/mainon 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.nlink == 1for 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
Type of Change
LoopX Area
Technical Direction
Shared-authority RFC fixture impact
Boundary Checklist
python3 scripts/check_rfc_bilingual.py --base origin/main --head HEADpasses, or no paired RFC changed.none.Signed-off-bytrailer (git commit -s).Written by: duang777 (@Duang777)