# Independent task: settle only from effect evidence / 独立任务：依据效果证据结算

## English

### A changed overrun requirement and a red baseline

The worked runtime demonstrates shared admission, execution fencing and the old settlement contract: unknown hold, safe no-effect release and receipt settlement when actual units do not exceed the reservation. Its public policy in runtime.py rejects actual units above the reservation with ValueError('settlement_policy_change_required') and leaves the reservation in place. This changed requirement adds a bound overrun decision that preserves the hold, records observed debt and freezes new tenant-budget admission.

Implement the full contract below in settlement_student.py and let the actual runtime finish/recovery path use it. The supplied file raises NotImplementedError and the worked policy does not implement the new overrun decision. Copying or importing only that old policy must still fail the changed checks. Studying the public source is allowed; disclose any reference/AI assistance. Fixed checks cannot establish independent authorship, and the later reviewer-observed fresh requirement must change a further condition.

Use a fresh [source package](https://ai.lwkjob.com/downloads/agent-senior-runtime-v1.zip), enter its root and preserve PACKAGE_MANIFEST.json and the initial source digests. Before writing code, run:

```sh
python3 -B senior_lab.py verify-learner --student settlement_student.py --work work/learner-before
```

The unimplemented template must produce a contract failure. Preserve its report and process exit code; an import/setup failure is not the intended red baseline. Implement the function in your own copy, retain the source diff and disclose assistance. Do not alter the verifier, fixed expected outcomes, worked reference, runtime accounting/authorization guards, merchant ledger or provenance files to obtain a pass.

### Complete function contract

The payload is an object with exactly reserved_units, actual_units and evidence. reserved_units is a strict positive integer. evidence is exactly one of unknown, confirmed_absent or confirmed_receipt. For confirmed_receipt, actual_units is a strict nonnegative integer; for the other evidence values it is exactly None. bool, float, NaN, strings as numbers, missing/extra fields and other shapes are invalid. Invalid input raises ValueError('invalid_settlement_input'). Do not mutate the input.

Return an object with exactly action, charge_units and release_units. Both units are strict nonnegative integers. Apply these rules:

1. unknown → action hold, charge_units 0, release_units 0. The units remain reserved. The zero output means no settled charge is being applied, not that unknown work had zero cost.
2. confirmed_absent → action release, charge_units 0, release_units equal to the reservation. This evidence must be established by the runtime under a current fenced claim; a timeout or an unauthenticated JSON statement cannot supply it.
3. confirmed_receipt with actual_units at most the reservation → action settle, charge_units equal to actual_units, release_units equal to reserved_units minus actual_units. Equality and actual zero are valid.
4. confirmed_receipt with actual_units greater than the reservation → action overrun, charge_units equal to actual_units, release_units 0. Preserve the hold and freeze further tenant-budget admission for review. This is observed synthetic cost awaiting resolution, not completed settlement. The runtime records actual_units but does not add this overrun to spent yet; spent=0 cannot be interpreted as no cost.

```json
{
  "unknown": {
    "input": {"reserved_units": 5, "actual_units": null, "evidence": "unknown"},
    "expected": {"action": "hold", "charge_units": 0, "release_units": 0}
  },
  "confirmed_absent": {
    "input": {"reserved_units": 5, "actual_units": null, "evidence": "confirmed_absent"},
    "expected": {"action": "release", "charge_units": 0, "release_units": 5}
  },
  "confirmed_receipt": {
    "input": {"reserved_units": 5, "actual_units": 3, "evidence": "confirmed_receipt"},
    "expected": {"action": "settle", "charge_units": 3, "release_units": 2}
  },
  "overrun": {
    "input": {"reserved_units": 5, "actual_units": 6, "evidence": "confirmed_receipt"},
    "expected": {"action": "overrun", "charge_units": 6, "release_units": 0}
  },
  "invalid": {
    "input": {"reserved_units": true, "actual_units": null, "evidence": "unknown"},
    "error": "ValueError:invalid_settlement_input"
  }
}
```

### Which component may supply the evidence?

The runtime supplies the reservation from its stored current task. The local merchant stores effect receipts and synthetic actual-unit facts. For confirmed receipt, the runtime binds the receipt to the same task, tenant, owner and original operation key and compares it to the merchant ledger; it checks actual units against the merchant cost record. The HTTP server records fixed synthetic cost 3 in the business transaction. Pure input actual zero is legal, but a runtime actual zero mismatching that record cannot settle; a missing usage record also rejects. Current runtime ACL and merchant identity are both checked. Model output, worker-declared cost or historical approval alone is insufficient.

For confirmed absence, the current claim must fence prior attempts and the runtime checks that no receipt exists while holding the same SQLite write transaction. This local arrangement makes the absence decision safe with respect to the fenced local merchant writes. A remote HTTP service without the shared transaction/fence would need its own terminal/no-effect protocol; a point-in-time empty lookup alone would not prove that an in-flight old write cannot still commit.

The function is a bounded decision policy. The runtime owns trusted evidence validation, current claim, accounting and atomic state mutation. Your result must actually control those mutations. Adding a helper that the finish/recovery path never calls is not an integrated submission.

### Prove behavior at the real hook

After implementing your function, run:

```sh
python3 -B senior_lab.py verify-learner --student settlement_student.py --work work/learner-after
python3 -B senior_lab.py demo --work work/worked-regression
python3 -B senior_lab.py load --work work/load-regression --tasks 12 --workers 4
```

Inspect both function cases and the actual runtime/merchant state. Unknown must retain its reservation and occupied slot across a reopened connection. A confirmed receipt must settle once, release the original reservation/slot and preserve the original operation key. Safe confirmed absence must release without inventing a merchant effect. Overrun must retain the hold, record actual units and freeze admission; malformed usage cannot become a zero-cost success. Repeated settlement/recovery and a stale worker must not charge, release or create an effect again.

In a separate source copy, replace unknown hold with release of the reservation. The unknown integration checks must fail; inspect the incorrect task/budget/slot record rather than changing an expected outcome. Restore your implementation and rerun. Also explain why replacing only the local fence cannot protect an external merchant that does not verify it.

### Deliver a source-bound change, not a screenshot

Submit initial failed report and exit code, original/source digests, personal diff, passed-after report, current learner-module digest, actual worker/runtime events, before/after reservation/budget records and merchant receipt/effect audit. Add the worked regression, declared load inputs and measured timing scope. Record every fixture/configuration change and which owner supplies its values. Import only modules you trust; the exercise is not a hostile-code sandbox.

Write trigger → first conflicting record → violated invariant → personal source repair → same-input rerun → residual limit. Then complete the two decision records and incident/handoff tasks in the [ADR guide](https://ai.lwkjob.com/lab/senior-runtime/ADR_GUIDE.md). A migration proposal remains proposed until its source and rehearsal exist. A reviewer must still provide a complete fresh requirement and observe a personal change; fixed public cases do not supply that observation.

Leave real-provider behavior, production capacity, reviewer observations and operational collaboration pending in the [assessment worksheet](https://ai.lwkjob.com/lab/capstone/ASSESSMENT_TEMPLATE.json). This task contributes to the full [responsibility path](https://ai.lwkjob.com/lab/capstone/CAREER_PATHS.md); it is not a senior job certification.

## 中文

### 超额变化需求与红基线

引导运行时示范共享准入、执行 fencing 及旧结算合同：unknown 保留、安全无效果释放、实际单位不超过预留时据回执结算。runtime.py 的公开旧政策在 actual 超出预留时抛 ValueError('settlement_policy_change_required')，保留原预留。变化需求增加绑定的 overrun 决定，保留 hold、记录观测债务、冻结租户后续预算准入。

在 settlement_student.py 实现下方完整合同，让实际完成/恢复路径调用它。交付模板抛 NotImplementedError，旧政策没有实现新超额决定，仅复制或导入它仍须在变化检查失败。可以学习公开源码，须披露参考/AI 协助；固定检查不能证明独立作者身份，后续评审观察还须改变另一完整条件。

另解压一份[源码包](https://ai.lwkjob.com/downloads/agent-senior-runtime-v1.zip)，进入包根目录，保存 PACKAGE_MANIFEST.json 与初始源码摘要。编码前运行：

```sh
python3 -B senior_lab.py verify-learner --student settlement_student.py --work work/learner-before
```

未实现模板须产生合同失败；保留报告和进程退出码，导入/环境失败不是规定的红基线。在本人副本实现函数、保留 diff、注明协助。不能改验收器、固定预期、参考实现、运行时记账/授权守卫、商家账本或来源文件来获得通过。

### 完整函数合同

payload 为仅包含 reserved_units、actual_units、evidence 的对象。reserved_units 为严格正整数。evidence 仅为 unknown、confirmed_absent、confirmed_receipt。confirmed_receipt 时 actual_units 为严格非负整数，另外两种证据时精确为 None。bool、浮点数、NaN、数字字符串、缺失/多余字段或其他形状均非法，抛 ValueError('invalid_settlement_input')；输入不得修改。

精确返回仅含 action、charge_units、release_units 的对象；两项 units 都是严格非负整数，按下列规则：

1. unknown → action=hold、charge_units=0、release_units=0，保留预留。零输出表示暂不应用结算扣费，不表示未知工作零成本。
2. confirmed_absent → action=release、charge_units=0、release_units=预留值。这项证据须由运行时在当前 fenced claim 下确定，超时或未认证 JSON 不能提供它。
3. confirmed_receipt 且 actual_units<=预留 → action=settle、charge_units=actual_units、release_units=reserved_units-actual_units。等号及 actual 为零合法。
4. confirmed_receipt 且 actual_units>预留 → action=overrun、charge_units=actual_units、release_units=0，保留 hold，冻结租户后续预算准入待处理。这是已观察的合成成本待处理，不是完成结算。运行时记录 actual_units，但尚未把 overrun 加入 spent；spent=0 不能解读为无成本。

```json
{
  "unknown": {
    "input": {"reserved_units": 5, "actual_units": null, "evidence": "unknown"},
    "expected": {"action": "hold", "charge_units": 0, "release_units": 0}
  },
  "confirmed_absent": {
    "input": {"reserved_units": 5, "actual_units": null, "evidence": "confirmed_absent"},
    "expected": {"action": "release", "charge_units": 0, "release_units": 5}
  },
  "confirmed_receipt": {
    "input": {"reserved_units": 5, "actual_units": 3, "evidence": "confirmed_receipt"},
    "expected": {"action": "settle", "charge_units": 3, "release_units": 2}
  },
  "overrun": {
    "input": {"reserved_units": 5, "actual_units": 6, "evidence": "confirmed_receipt"},
    "expected": {"action": "overrun", "charge_units": 6, "release_units": 0}
  },
  "invalid": {
    "input": {"reserved_units": true, "actual_units": null, "evidence": "unknown"},
    "error": "ValueError:invalid_settlement_input"
  }
}
```

### 谁能提供效果证据？

运行时从已存的当前任务提供预留。商家记录效果回执与实际合成单位。确认回执时，运行时将回执绑定同一任务、租户、owner 和原操作键，与商家账本比较；实际单位须匹配商家成本记录。HTTP 服务在业务事务内记固定合成成本 3。纯函数 actual=0 合法，但运行时 actual=0 与该记录不符就不能结算，usage 记录缺失也拒绝；当前 runtime ACL 与商家身份均须核验。模型输出、worker 自报成本或历史批准均不足。

确认无效果时，当前 claim 须 fence 先前尝试，运行时在同一 SQLite 写事务锁内检查无回执。本机布置让无效果决定相对于已 fence 的本机商家写安全；远端 HTTP 服务若没有共享事务/fence，需要自己的终态/无效果协议，某一时刻查无记录不能单独证明旧请求不会稍后提交。

本人函数是有界决策政策；可信证据核验、当前 claim、记账和原子状态变更由运行时负责。本人返回必须实际控制变更，完成/恢复路径从未调用的新 helper 不算集成。

### 在真实 hook 证明行为

实现后运行：

```sh
python3 -B senior_lab.py verify-learner --student settlement_student.py --work work/learner-after
python3 -B senior_lab.py demo --work work/worked-regression
python3 -B senior_lab.py load --work work/load-regression --tasks 12 --workers 4
```

同时查看函数案例与真实运行时/商家状态。unknown 跨连接重开仍保留预留和槽；确认回执只结算一次，释放原预留/槽并保留原 key；安全确认无效果只释放，不造商家效果。overrun 保留 hold、记录实际单位并冻结准入，畸形 usage 不能成为零成本成功。重复结算/恢复及旧 worker 不能再扣费、再释放或再造效果。

另在代码副本将 unknown 的 hold 改成释放预留，对应 unknown 集成检查须失败；查看错误任务/预算/槽记录，不能改预期。恢复本人实现后重跑。说明为何仅改本地 fence 不能保护不核验它的外部商家。

### 交源码绑定变化，不只交截图

交初次失败报告与退出码、原/本人源码摘要、个人 diff、修后报告、当前学员模块摘要、实际 worker/运行时事件、前后预留/预算及商家回执/效果审计；补引导回归、声明的负载输入和实测时间范围。记录所有夹具/配置变更及其值的负责人。只导入信任的模块，实验不是恶意代码沙箱。

写触发→首个冲突记录→违反不变量→本人源码修复→同输入重跑→剩余边界，再做[ADR 指南](https://ai.lwkjob.com/lab/senior-runtime/ADR_GUIDE.md)的两份决策、事故和交接任务。迁移方案在有源码与演练前仍为 proposed。评审还须提供完整新需求并观察本人变化，固定公开案例不提供该观察。

[评审记录](https://ai.lwkjob.com/lab/capstone/ASSESSMENT_TEMPLATE.json)中真实提供方、生产容量、评审观察、运维协作保持 pending。本任务贡献完整[职责路径](https://ai.lwkjob.com/lab/capstone/CAREER_PATHS.md)中的一部分，不认证资深岗位。
