# Independent requirement: bound observation age / 独立需求：限制观察年龄

## English

### Preserve a red baseline before writing code

The guided cache integration already demonstrates revocation, policy/data version changes, expiry and restart. This different requirement is **not implemented** in integrated/observation_student.py. The supplied CLI and Agent hook provide the plumbing; your function's decision must control the actual get_order path under real local merchant HTTP. A standalone function test without that path is insufficient.

Use a new extracted [project source package](https://ai.lwkjob.com/downloads/agent-project-integration-v1.zip). Before changing source, preserve the manifest and original digests and run:

```sh
python3 integration_demo.py verify-learner --module integrated/observation_student.py --work work/learner-before --out work/learner-before/report.json
python3 -m json.tool work/learner-before/report.json
```

The unimplemented template must produce a failed report and exit 1. An environment/setup error is not a correct rejection. Keep that failed-before report unchanged. Implement assess_observation in your own source copy, disclose any assistance and retain the diff; do not change cases, expected outputs, the verifier, merchant checks or original baseline files.

### Complete input and output contract

Implement assess_observation(payload). The payload must be an object with exactly observed_at_ms, now_ms and max_age_ms. Each value is a nonnegative integer; bool, float, NaN, missing/extra fields and other types are invalid. now_ms must be at least observed_at_ms. Any invalid shape or future observation raises ValueError('observation_shape'). Do not mutate the input.

For a valid payload, age is now_ms minus observed_at_ms. If age is at most max_age_ms, return exactly {"status":"accept"}; if age is greater, return exactly {"status":"refresh"}. No additional fields or truthy substitute values are allowed. Zero age and zero limit are valid; equality accepts.

```json
{
  "accepted_boundary": {
    "input": {"observed_at_ms": 1000, "now_ms": 1020, "max_age_ms": 20},
    "expected": {"status": "accept"}
  },
  "expired_observation": {
    "input": {"observed_at_ms": 1000, "now_ms": 1021, "max_age_ms": 20},
    "expected": {"status": "refresh"}
  },
  "future_observation": {
    "input": {"observed_at_ms": 1000, "now_ms": 999, "max_age_ms": 20},
    "error": "ValueError:observation_shape"
  }
}
```

### Where the time values come from

The application records observed_at_ms immediately before each local HTTP request using monotonic_ns divided by 1,000,000 with integer division. It records now_ms after the response arrives, before reading a cached result or returning the new order body, using the same process clock. The age conservatively includes request time. These values do not come from the model, caller JSON, merchant wall clock or a cached prior-process timestamp.

Monotonic readings from different processes are not used for comparison. This task does not persist observation-age values across restart. A new reader starts a new current merchant observation using its own clock; the cache row remains persistent under the existing version/expiry contract. The verification fixtures inject a declared logical clock to exercise age conditions reproducibly. Those values are authored conditions, not measured HTTP latency or an SLA result.

### Actual tool-path behavior to verify

The supplied hook applies your decision after both authoritative snapshot reads and order-body fills. accept lets the existing current-authority/cache checks continue. refresh repeats the same HTTP endpoint at most once, recording a fresh request-start value. If that second observation is also too old, the Agent raises LabError('observation_too_old') and returns no order result. An invalid observation decision must not bypass checks or release a result.

An old authoritative snapshot cannot be used to inspect/release the cached order while waiting for the retry. An old body-fill result cannot be saved as a new cache value or returned as the order. A retried request is authenticated again: revocation must deny access, and a changed order must bind the new fill to the current data version. Approval and execution retain the merchant's current-authority checks. This bounded retry narrows the observation window; it does not eliminate every cross-request TOCTOU race.

### Required acceptance and delivery

Preserve the original six-task baseline and guided demonstration. The learner verifier must exercise legal/equal-boundary/too-old inputs, invalid shapes, an over-age snapshot retry before cache access, an over-age fill retry before storage/return, a second over-age rejection, current revocation on retry, data-version change on retry and a new reader using a persistent cache row. Inspect each actual trace and result, including rejected paths; printed pass text is not evidence.

After implementing your own module, run:

```sh
python3 integration_demo.py verify-learner --module integrated/observation_student.py --work work/learner-after --out work/learner-after/report.json
python3 integration_demo.py demo --work work/guided-regression --out work/guided-regression/report.json
python3 integration_demo.py regression --work work/original-regression --out work/original-regression/report.json
python3 -m unittest -v test_integration.py
```

Deliver the original red report, personal source/diff/digest, passed-after report, actual request/trace/persistence records and original regression. Explain which delayed response was discarded, which response replaced it and why no discarded data escaped. In a separate source copy, replace your age decision with unconditional accept; the corresponding over-age checks must fail. Restore your source and rerun. Record trigger → evidence → violated invariant → source repair → same-input rerun → remaining limits in a one-page incident note.

An evaluator then supplies one complete fresh requirement and observes a personal modification. Public learner cases are fixed practice; they are not that unseen observation. Keep the real-provider, reviewer-observed and production states pending in the [blank assessment worksheet](https://ai.lwkjob.com/lab/capstone/ASSESSMENT_TEMPLATE.json). This bounded function plus actual tool-path checks contributes to a project delivery; it does not replace the full [responsibility paths](https://ai.lwkjob.com/lab/capstone/CAREER_PATHS.md).

## 中文

### 编码前保留红基线

引导缓存集成已示范撤权、政策/数据版本变化、失效和重启。这个不同需求在 integrated/observation_student.py 中**尚未实现**。交付的 CLI 与 Agent hook 提供接线，本人函数的决定必须控制真实本机商家 HTTP 下的实际 get_order 路径；只测独立纯函数不足以验收。

另解压一份[项目源码包](https://ai.lwkjob.com/downloads/agent-project-integration-v1.zip)，修改前保存 manifest 与原摘要，运行：

```sh
python3 integration_demo.py verify-learner --module integrated/observation_student.py --work work/learner-before --out work/learner-before/report.json
python3 -m json.tool work/learner-before/report.json
```

未实现模板须报告失败并退出 1，环境或启动错误不算正确拒绝。原样保留修改前报告。在本人副本实现 assess_observation，注明协助并保留 diff；不能改案例、预期结果、验收器、商家守卫或原基线文件。

### 完整输入输出合同

实现 assess_observation(payload)。payload 须为仅含 observed_at_ms、now_ms、max_age_ms 三字段的对象。值均为非负整数，bool、浮点数、NaN、缺失/多余字段或其他类型均无效；now_ms 不得小于 observed_at_ms。形状错误或未来观察抛 ValueError('observation_shape')，输入不得修改。

合法输入的 age=now_ms-observed_at_ms。age<=max_age_ms 时精确返回 {"status":"accept"}，超出则精确返回 {"status":"refresh"}；不接受额外字段或其他真值替代。年龄和上限为零合法，等号接受。

```json
{
  "accepted_boundary": {
    "input": {"observed_at_ms": 1000, "now_ms": 1020, "max_age_ms": 20},
    "expected": {"status": "accept"}
  },
  "expired_observation": {
    "input": {"observed_at_ms": 1000, "now_ms": 1021, "max_age_ms": 20},
    "expected": {"status": "refresh"}
  },
  "future_observation": {
    "input": {"observed_at_ms": 1000, "now_ms": 999, "max_age_ms": 20},
    "error": "ValueError:observation_shape"
  }
}
```

### 时间值由谁提供

应用在每次本机 HTTP 请求之前，以 monotonic_ns 整除 1,000,000 记录 observed_at_ms；响应到达之后、读取缓存或返回新订单 body 之前，以同一进程时钟记录 now_ms。年龄保守包含请求耗时，值不来自模型、调用者 JSON、商家壁钟或上一个进程的缓存时间戳。

不比较不同进程的 monotonic 读数，也不跨重启持久化观察年龄。新 reader 使用自己的时钟重新观察当前商家；缓存行继续按已有版本/失效合同持久化。验收夹具注入明确的逻辑钟，可重复制造年龄条件；它们是作者构造条件，不是实测 HTTP 延迟或 SLA。

### 必须验证的实际工具行为

交付 hook 在商家权威 snapshot 与订单 body-fill 返回后都调用本人决定。accept 进入原当前权限和缓存检查；refresh 至多再请求同一 endpoint 一次，并记录新的请求起点。第二次观察仍超龄时，Agent 抛 LabError('observation_too_old')，不返回订单结果。无效决定不得绕过检查或放行。

重查前不能用超龄权威 snapshot 读取/放行缓存订单；超龄 body-fill 不能保存为新缓存值或返回。重试请求重新认证，撤权须拒绝，订单变化须将新 fill 绑定当前数据版本。商家批准和执行处仍检查当前权限。有限重查只缩短观察窗口，不能消除所有跨请求 TOCTOU 竞态。

### 验收与交付

保留原六任务和引导演示。学员验收须覆盖合法/等号/超龄输入、错误形状、超龄 snapshot 在读缓存前重查、超龄 fill 在保存/返回前重查、二次超龄拒绝、重查时当前撤权、重查时数据版本变化、以及新 reader 使用持久缓存行。查看实际轨迹和返回，拒绝路径也要看，打印 pass 不算证据。

本人实现后运行：

```sh
python3 integration_demo.py verify-learner --module integrated/observation_student.py --work work/learner-after --out work/learner-after/report.json
python3 integration_demo.py demo --work work/guided-regression --out work/guided-regression/report.json
python3 integration_demo.py regression --work work/original-regression --out work/original-regression/report.json
python3 -m unittest -v test_integration.py
```

交原红报告、本人源码/diff/摘要、修后报告、实际请求/轨迹/持久状态及原回归。说明丢弃哪次延迟返回、用哪次新返回替代、为何丢弃数据没有泄漏。另在代码副本将年龄决定改为无条件 accept，对应超龄检查应失败；恢复本人实现后重跑。一页事故说明写触发→证据→违反不变量→源码修复→同输入重跑→剩余边界。

再由评审提供完整新需求并观察本人修改。公开学员案例是固定练习，不是该未知条件观察。[空白评审记录](https://ai.lwkjob.com/lab/capstone/ASSESSMENT_TEMPLATE.json)内真实提供方、评审观察、生产责任仍保持 pending。有界函数加实际工具验证为项目交付贡献一部分证据，不替代完整[职责路径](https://ai.lwkjob.com/lab/capstone/CAREER_PATHS.md)。
