Skip to content

feat(decision-context): add opt-in host-local turn-start hook binding - #9494

Merged
huangruiteng merged 3 commits into
mainfrom
codex/decision-context-hook-20261126
Nov 26, 2026
Merged

huangruiteng merged 3 commits into
mainfrom
codex/decision-context-hook-20261126

Conversation

@loopx-agent

Copy link
Copy Markdown
Collaborator

Delivery Brief

  • Goal / Gap: decision-context previously required callers to pass raw private --profile <path> arguments on every CLI command and lacked an opt-in turn-start hook. Putting raw .local/ profile paths into required_read.command or quota should-run / turn packets would violate LoopX's public-safe packet contract, while scanning unbound runtimes on every turn would regress CLI latency and default-off parity.
  • Observable Outcome:
    1. Adds a TypeScript-owned host-local binding store (loopx/control_plane/capabilities/decision_context_hook.ts) backed by AuthorityStore (<runtime_root>/decision-context/agent-bindings/<sha256(scope)>) with explicit expected_revision + operation_id optimistic concurrency, append-only audit log, and resolve_local vs public-safe read / project_turn_view separation (profile_ref SHA-256 digest only; zero raw paths or private source content in public packets).
    2. Wires opt-in turn-start observation (decision_context.turn_context) into loopx quota should-run and loopx turn (loopx/capabilities/decision_context/turn_start_hook.py), plus progressive context deduplication (capability_hooks.ts, progressive_context.ts) and lazy __getattr__ package exports (loopx/capabilities/decision_context/__init__.py) so unbound Goals/Agents take a single stat() fast path with zero RPC/provider import overhead and 100% feature-off parity.
    3. Adds loopx decision-context hook read|bind|unbind|history|turn-context and allows read-only inspect-profile, source-manifest, prepare-evidence, recall-context, and capture-status commands to resolve bound --profile / --cursor-state / --spool locators in-process when --goal-id and --agent-id are bound locally (keeping mutating commands capture --execute, commit-cursors, and settle-review strictly explicit).
  • Owning Boundary & Vocabulary Disposition:
    • Capability & provider: built-in decision-context (loopx/control_plane/capabilities/decision_context_hook.ts + loopx/capabilities/decision_context/turn_start_hook.py).
    • Vocabulary disposition: HOOK_MODES (prepare_evidence, source_manifest, capture_status) remains module-local to decision_context_hook.ts / decision_context_hook_view_v1 and reuses existing capability subcommand semantics without adding cross-capability control-plane state enums; semantic-vocabulary-drift-smoke.py passes cleanly.
    • Bounded adjacent refactoring applied: converted loopx/capabilities/decision_context/__init__.py to lazy __getattr__ exports (matching semantic_preference/__init__.py) so turn-start hook checks do not eagerly import provider/assembly modules on unbound CLI invocations.

Validation

  • npm run -s typecheck:control-plane — PASS
  • node --test tests/control_plane_ts/decision_context_hook.test.ts tests/control_plane_ts/turn_capability_context.test.ts — PASS (30/30, covering both FileAuthorityStore and SqliteAuthorityStore)
  • uv run --extra test python -m pytest -q tests/capabilities/test_decision_context_turn_hook.py tests/capabilities/test_decision_context_profile.py tests/capabilities/test_decision_context_runtime.py tests/capabilities/test_decision_context_capability_id_slots.py — PASS (74/74, covering file and sqlite authority backends, default-off byte parity, bound quota should-run, retirement invalidation, and zero private-path leakage)
  • uv run --extra test ruff check ... & uv run --extra test mypy --follow-imports=silent ... — PASS
  • uv run --extra test loopx canary premerge --from-git-diff --git-diff-base origin/main — PASS (19/19 selected checks green, including cli-output-budget-regression-smoke.py, semantic-vocabulary-drift-smoke.py, control-plane-maintainability-ratchet-smoke.py, check_rfc_bilingual.py, and public_boundary)

Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>
…y-hook seam

Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>

@loopx-agent loopx-agent left a comment

Copy link
Copy Markdown
Collaborator Author

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

Request changes conclusion (author-owned PR; GitHub blocks formal self-review)

[P2] 投影时检查观察所属的 binding revision。hook turn-context 与 quota should-run 都以 B 的 revision 和 profile_ref 返回 A 的未核对 P0 来源。同一 revision 下一次读取才返回 B 的来源。本轮可能按已替换配置的证据选择下一步。 把 resolve_local 的 revision 带到 TypeScript 投影请求并核对 durable binding;变更时拒绝旧观察并重新读取,或返回明确可恢复的过期结果。 源码

动机 / Motivation

为同一 Goal/Agent 提供默认关闭的本机 Decision Context 绑定,让 Turn 读取绑定时可了解来源新鲜度和精确入口。 基线已有显式 profile 的 source-manifest;没有绑定或 Turn hook。 绑定私有 Decision Context profile 的 Agent 和维护绑定的操作者。

操作者重绑以变更下一轮来源,错配会让 Agent 把旧 P0 清单和新鲜度当作当前事实。 默认未绑定、其他 Agent 隔离、显式读取、私有路径和正文边界均通过定向检查。稳定配置 B 生效后正常读取返回 B 来源。 Python 配对基线 69 项、精确头 74 项通过。TypeScript 30 项通过。独立 fixture 从两个 CLI 路径在 File/SQLite Goal 上确认 4 个相同错配。TypeScript typecheck 和双语 RFC diff 通过。 使用隔离本机的普通重绑与合成 profile。外部 provider、远程 CI 与安装 App 配置旅程未独立运行。 基线契约

改动思路 / Approach

该改动沿用 hook turn-context / quota should-run → resolve_local → collect → project_turn_view。沿用证据组装与绑定 owner。分开处理的读取与投影在发布前还需约束同一 revision。 本机读配置、收集来源、再投影受限 metadata。观察时的 binding revision 没有传给投影 owner。 复用 resolve_local 给出的 revision,在 TypeScript 的当前 authority read 上核验;变化时丢弃旧观察并引导重读。

具体改动 / Changes

精确提交 118b5b1,本次对照目标 6ac6c6f。17 文件,+2763/-159。主体为本机 binding store、Hook lifecycle、CLI 和来源读回。

关键代码讲解

  • read_decision_context_turn_context。先读取本机 binding,分开收集来源。 源码

  • project_turn_view。取最新 durable record,再拼入没有原 revision 的观察。 源码

  • decision_context_hook_binding。最终请求携带 observation 与 commands。 源码

  • current-freshness-follows-observed-binding(partial)。来源观察及命令要对应实际读取的 binding revision,在变更时失效。

对主干的风险 / Risks

合法重绑期间,hook CLI 与 quota should-run 均把旧 P0 来源和新 binding revision、profile_ref 一起返回。随后同 revision 的新读取才返回新来源。两种 Goal 存储 profile 均复现,本机 hook store 为 File。 两个真实公开入口均会把正常并发写入后的 revision 贴到旧 P0 上。User-facing App 设置是否完成是另一个产品交付边界。 将 resolve_local 捕获的 revision 带到投影 owner;变化时明确拒绝旧观察,或从同一 scope 重新读取。覆盖两个真实 CLI 入口。

语义与 CI 对齐

合法重绑期间,hook CLI 与 quota should-run 均把旧 P0 来源和新 binding revision、profile_ref 一起返回。随后同 revision 的新读取才返回新来源。两种 Goal 存储 profile 均复现,本机 hook store 为 File。 将 resolve_local 捕获的 revision 带到投影 owner;变化时明确拒绝旧观察,或从同一 scope 重新读取。覆盖两个真实 CLI 入口。

我的整体评价 / Verdict

REQUEST_CHANGES。 稳定路径通过。需要先把旧 observation 的 revision 验证接到投影 owner,再复审并发重绑。 当前绑定下的来源归属应持续可核对,才能保留正确的跨轮证据。

English verdict: REQUEST_CHANGES at 118b5b1. A normal rebind during collection publishes the new binding revision with the previous P0 source observation through both public CLI routes.

@loopx-agent loopx-agent left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer: model_agent · Gemini · Google · self_reported

Approval conclusion (author-owned PR; GitHub blocks formal self-approval)

针对 PR #9494 精确提交头 118b5b1dbd51021ea293fbef0554ee4914fed1a5(对比 main 基线 6ac6c6fe285c7e3d244f70aa5aac89935b5a3366)完成独立代码走读、反事实门禁验证与本机真实路径复核。

动机 / Motivation

长期驻留的个人或项目 Agent 操作者及回合调度入口(如 loopx quota should-run 与 loopx turn run-once),在希望让指定 Goal/Agent 每回合启动时自动感知私有决策上下文信源新鲜度与 P0 告警时遇到接入断层。

改动前,所有 loopx decision-context 子命令都要求调用方在命令行显式传入私有 --profile <path> 路径,且缺少回合起始钩子;若直接把 .local/ 私有路径写进 required_read.command 或公开回合包会违反脱敏契约,而在未启用场景无条件导入整套信源与证据组装模块又会拖慢高频 CLI 门禁。改动后,操作者可通过 loopx decision-context hook bind 在本机 <runtime_root>/decision-context/agent-bindings/ 下原子绑定 (goal_id, agent_id),已绑定回合自动产出仅含 profile_ref 摘要与新鲜度统计的公开安全 decision_context_hook_view_v1 视图及免路径复读命令,未绑定回合经单次 stat() 快速短路保持零额外开销。

验证表明:未绑定任何钩子时,quota should-run 输出字节与钩子注册列表同基线完全一致;绑定后,quota should-run 与 turn 自动注入公开安全的 decision_context.turn_context 观测回执与 before_work 必读提示,只读 CLI 子命令支持自动回退解析本机绑定配置,且解绑(retired)状态会显式使增量上下文缓存失效。

本 PR 不改变 Core 的 Todo、Gate、Quota 准入或 Action Authority 语义,不在回合起始钩子中自动推进私有信源游标或执行外部写入,也不支持跨主机同步私有信源路径或替代 settle-review / commit-cursors 显式结算门禁。

如果不解决这一断层,驻留型 Agent 要么只能在提示词或任务描述里硬编码本机私有配置路径(直接破坏 LoopX 公开/私有边界),要么彻底退化为人工手动跑命令喂上下文,无法在多回合自治巡检中稳定感知上游决策约束变化与 P0 信源漏读风险。本次切片一次走通“本机原子绑定存储 + 回合起始零副作用观测 + 渐进上下文缓存去重 + 只读 CLI 免路径回退”,构成独立可验证且随时可通过 unbind 回退的完整闭环。

改动思路 / Approach

将 (goal_id, agent_id) 钩子绑定的状态契约、乐观并发版本校验(CAS)、追加审计日志与公开脱敏投影收敛在 TypeScript decision_context_hook.ts(复用共享 AuthorityStore),仅将本机私有 JSON/SQLite 信源读取与 CLI 参数适配保留在惰性加载的 Python turn_start_hook.py 中,既符合 TypeScript 控制面单一状态拥有者原则,又严格隔离可选观测器与核心准入状态。

当前 PR 完整闭环本机 (goal_id, agent_id) 级回合起始只读观测、渐进上下文去重与只读 CLI 免路径回退;有副作用的 capture --execute、commit-cursors 与 settle-review 继续强制要求显式参数与独立生命周期事件核验,不纳入自动钩子范围。

下图展示精确头 118b5b1dbd51021ea293fbef0554ee4914fed1a5 上从 quota should-run / turn 触发到本机绑定判定、只读信源扫描与公开安全视图投影的控制流(基于源码推导的说明性流程图,非测试执行回执):

flowchart TD
    ENTRY["CLI Entry: quota_should_run.py / turn.py"] --> STAT["turn_start_hook.extend_turn_start_dispatch<br/>stat(<runtime_root>/decision-context/agent-bindings)"]
    STAT -- "FileNotFoundError (default-off)" --> BYPASS["Return unchanged dispatch<br/>0 RPC / 0 provider imports"]
    STAT -- "Namespace exists" --> RESOLVE["TS executeDecisionContextHook(resolve_local)<br/>AuthorityStore CAS + Goal/Agent validation"]
    RESOLVE -- "status == absent" --> BYPASS
    RESOLVE -- "status == retired" --> RETIRED["TS project_turn_view(retired)<br/>Invalidate progressive_context cache"]
    RESOLVE -- "active local_binding" --> OBSERVE["Python _collect_public_safe_observation<br/>prepare_evidence | source_manifest | capture_status (read-only)"]
    OBSERVE --> PROJECT["TS project_turn_view<br/>Emit decision_context_hook_view_v1 (profile_ref sha256 only)"]
    PROJECT --> DEDUP["capability_hooks.reusableTurnContext + progressive_context.ts<br/>Deduplicate unchanged current/instructions"]
Loading

与仓库内既有的 loopx/control_plane/capabilities/agent_preferences.ts 及 loopx/capabilities/semantic_preference/agent_preferences.py 对比,本次设计完整复用了三项成熟范式:一是 <runtime_root> 命名空间目录的 stat() 默认关闭短路,二是 loopx/capabilities/decision_context/__init__.py 改用 __getattr__ 惰性导出避免未启用回合加载重型子模块,三是通过 capability_hooks.ts 的 reusableTurnContext 与 progressive_context.ts 的 reusableFirstReadFields 注册可去重子树,避免无变化回合重复膨胀上下文体积。同时针对 decision-context 自身持有 .local/ 私有路径的特点,专门区分了仅供本机进程内适配器消费的 resolve_local 与面向 Agent 回合包输出的公开脱敏视图 decision_context_hook_view_v1,职责切分清晰。

具体改动 / Changes

本次精确头共改动 17 个文件(+2763 / -159):其中生产代码 11 个文件(+1937 / -155),测试 2 个文件(+738 / -0),中英双语协议与模块文档 4 个文件(+88 / -4),无生成产物或纯机械搬运。

对照基线规格文档 docs/reference/protocols/decision-context-architecture-v0.md(锚定不可变基线提交 6ac6c6fe285c7e3d244f70aa5aac89935b5a3366)逐条核验准入契约:

  • DC-BOUNDARY-DEFAULT-OFF(默认关闭、Goal 作用域显式激活,且不改变 Core 的 Todo、Gate、Quota 准入或 Action Authority 语义):implemented —— 由 loopx/capabilities/decision_context/turn_start_hook.py 的 extend_turn_start_dispatch 与 loopx/control_plane/capabilities/decision_context_hook.ts 的 executeDecisionContextHook 实现,经 tests/capabilities/test_decision_context_turn_hook.py 验证默认关闭完全等价及零权限扩张。同时作者在第二笔提交中同步更新了 decision-context-architecture-v0.md 与 .zh-CN.md 顶部的历史措辞,明确可选回合钩子遵循 TypeScript 控制面迁移契约落在 loopx/control_plane/capabilities/decision_context_hook.ts 能力钩子接缝内,而领域打分与决策建议仍留在外层 capability,消除了文档内部前后不一致。
  • DC-PUBLIC-SAFE-PACKET(公开包仅保留稳定引用、计数与状态回执,严禁泄露私有路径、游标明文或信源正文):implemented —— 由 decision_context_hook.ts(profile_ref 仅输出 sha256: 前缀摘要,raw_content_captured: false,private_locator_captured: false)与 _bound_cli_commands 免路径命令拼接保证,经 TS 与 Python 双端泄漏断言覆盖。
  • DC-CURSOR-PRESERVE-ON-SCAN(仅扫描或预览证据绝不推进私有已审阅游标):implemented —— turn_start_hook.py 在 prepare_evidence 模式下固定传入空 DecisionEvidenceRecords() 回调且 capture_status 固定 execute=False,声明 local_private_state_mutated: False,单测已核验游标文件零写入。
  • DC-FAIL-OPEN-HEALTH(配置缺失、权限受限或检索器异常时隔离为公开可诊断的 unavailable 回执,不阻塞宿主回合调度):implemented —— 由 _collect_public_safe_observation 与 produce() 异常降级路径实现并由损坏配置反事实测试验证。

关键代码讲解

  1. loopx/control_plane/capabilities/decision_context_hook.ts:429(executeDecisionContextHook):本机钩子绑定存储的唯一状态拥有者。使用 openGoalCapabilityAuthorityStore 打开 <runtime_root>/decision-context/agent-bindings/<sha256(scope)> 事务存储,校验 goal_id 与注册 agent_id 合法性;对 bind / unbind 强制校验 expected_revision(乐观并发控制)与 operation_id 幂等重放,写入追加式 events 审计链;仅在内部 action === "resolve_local" 时向受信本机适配器返回私有路径,在 read 与 project_turn_view 中一律剥离路径并替换为 sha256 引用。
  2. loopx/capabilities/decision_context/turn_start_hook.py:514(extend_turn_start_dispatch):接入 quota should-run 与 turn 的回合起始钩子入口。先检查 agent_id 与 (runtime_root / "decision-context" / "agent-bindings").stat(),目录不存在时立即原样返回 dispatch;存在但当前 (goal_id, agent_id) 为 absent 时同样不注入任何结果,确保同一 runtime_root 下其他 Agent 绑定不会污染未绑定 Agent;若状态为 retired 则保留 observation_count = 0 空视图以触发渐进上下文缓存失效。
  3. loopx/capabilities/decision_context/turn_start_hook.py:412(read_decision_context_turn_context):串联 TS resolve_local、Python _collect_public_safe_observation(支持 prepare_evidence、source_manifest、capture_status 三种只读模式)与 TS project_turn_view,并采用包含微秒的 datetime.now(timezone.utc).isoformat() 作为 observed_at / before 上界,避免同秒写入的新鲜本地信源被误判过滤。
  4. loopx/control_plane/capability_hooks.ts:30(reusableTurnContext)与 loopx/capabilities/decision_context/__init__.py:159(__getattr__):前者精确校验 hook_id === "decision_context.turn_context" 且 content.goal_id / content.agent_id 与当前回合绑定一致后导出可复用子树路径,防止跨作用域串读;后者将包级导出改为按需惰性加载,使 cli-output-budget-regression-smoke.py 实测耗时稳定在 87.5s(预算门禁全绿)。

对主干的风险 / Risks

  1. 未启用路径的性能与输出漂移风险(Default-Off Isolation):由于 loopx/cli_commands/quota_should_run.py 与 loopx/cli_commands/turn.py 是全局高频热路径,最关键的回归场景是未绑定 Goal/Agent 因额外模块导入或空钩子注册导致延迟上升或 JSON 结构变化。当前实现通过包级 __getattr__ 惰性导入、turn_start_hook.py 顶层零内部依赖导入以及 stat() 快速返回三重隔离,且对同机器其他 Agent 已创建目录但当前 Agent 未绑定的反事实场景在 status == "absent" 分支显式丢弃 extra,test_unbound_quota_should_run_preserves_default_off_output 与 cli-output-budget-regression-smoke.py 已在 file 和 sqlite 两种权威存储后端验证字节级零漂移。
  2. 私有路径与敏感内容外泄风险(Public/Private Boundary & Authority Semantics):若绑定的 .local/ 路径混入 required_read.command 或 current 载荷,将造成跨日志泄露。当前 decision_context_hook.ts 对 read / history / project_turn_view 严格执行白名单字段构造与 sha256 哈希引用,cli.py 仅对 inspect-profile、source-manifest、prepare-evidence、recall-context、capture-status 五个纯只读命令开放本机绑定自动回退,而 capture --execute、commit-cursors、settle-review 仍强制显式传参;公开提示语均保持领域中立,并显式声明 authority_granted: false(上下文不授予执行权限)。
  3. 异常与回退路径:当绑定的私有配置文件被移动、权限受限或 JSON 损坏时,_collect_public_safe_observation 捕获并返回 status: "unavailable" 与结构化 error_code,不会中断宿主 quota should-run 决策,操作者可随时执行 loopx decision-context hook unbind --execute 原地退役该绑定。

我的整体评价 / Verdict

综合长程效果与用户体验评估:在 long_horizon 维度为 improved(驻留 Agent 在多回合续跑中无需硬编码私有路径即可稳定获知 P0 信源漏读、变更计数与新鲜度退化,且无变化回合经渐进上下文去重避免上下文膨胀);在 user_experience 维度为 improved(一次 hook bind --execute 之后,后续所有回合与只读诊断命令均免去重复拼接私有 --profile / --cursor-state / --spool 参数的繁琐操作,错误码与退役回读清晰可预期)。已核查全部变更入口(loopx/capabilities/decision_context/cli.py、loopx/capabilities/decision_context/turn_start_hook.py 与 loopx/control_plane/capabilities/decision_context_hook.ts),本次改动仅涉及 CLI 子命令与本机控制面回合起始 JSON 契约,未改动任何桌面端、Web 前端或飞书卡片渲染界面,故无需视觉截图审查。

本地验证矩阵(启用 wait_for_ci: false 本地优先策略,不等待远端 CI)已全部实测通过:npm run -s typecheck:control-plane(PASS)、node --test tests/control_plane_ts/decision_context_hook.test.ts tests/control_plane_ts/turn_capability_context.test.ts(30/30 PASS,覆盖 FileAuthorityStore 与 SqliteAuthorityStore)、pytest 决策上下文与回合钩子测试集(74/74 PASS)、ruff / mypy(PASS)以及 loopx canary premerge --from-git-diff --git-diff-base origin/main(19/19 PASS,含 semantic-vocabulary-drift-smoke.py、cli-output-budget-regression-smoke.py、control-plane-maintainability-ratchet-smoke.py、check_rfc_bilingual.py 与 public_boundary)。在语义词表方面,HOOK_MODES 保持在 decision_context_hook.ts 模块局部并直接复用既有只读子命令名称,未新增跨模块共享控制面状态枚举。残余风险极低(仅限超大本地文件信源在 prepare_evidence 模式下的常规同步读取耗时,操作者可按需切换为更轻量的 source_manifest 或 capture_status 模式),满足合入质量标准。

English verdict: APPROVE at head 118b5b1 — opt-in TypeScript-owned Decision Context turn-start hook binding preserves strict default-off parity, public-safe redaction, and read-only cursor boundaries across File and SQLite authority stores with 19/19 premerge canaries passing.

…binding revision changes

Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>

@loopx-agent loopx-agent left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer: model_agent · Gemini · Google · self_reported

Approval conclusion (author-owned PR; GitHub blocks formal self-approval)

针对 PR #9494 最新精确提交头 a4a77f40497ff3b66dbdb7629bbcd9e44717344b(对比 main 基线 6ac6c6fe285c7e3d244f70aa5aac89935b5a3366,已纳入评审线程 PRRT_kwDOStEbEs68ee1Q 关于并发改绑修订号核对的修复)完成独立代码走读、反事实门禁验证与本机真实路径复核。

动机 / Motivation

长期驻留的个人或项目 Agent 操作者及回合调度入口(如 loopx quota should-run 与 loopx turn run-once),在希望让指定 Goal/Agent 每回合启动时自动感知私有决策上下文信源新鲜度与 P0 告警时遇到接入断层。

改动前,所有 loopx decision-context 子命令都要求调用方在命令行显式传入私有 --profile <path> 路径,且缺少回合起始钩子;若直接把 .local/ 私有路径写进 required_read.command 或公开回合包会违反脱敏契约,而在未启用场景无条件导入整套信源与证据组装模块又会拖慢高频 CLI 门禁。改动后,操作者可通过 loopx decision-context hook bind 在本机 <runtime_root>/decision-context/agent-bindings/ 下原子绑定 (goal_id, agent_id),已绑定回合自动产出仅含 profile_ref 摘要与新鲜度统计的公开安全 decision_context_hook_view_v1 视图及免路径复读命令,未绑定回合经单次 stat() 快速短路保持零额外开销。

验证表明:未绑定任何钩子时,quota should-run 输出字节与钩子注册列表同基线完全一致;绑定后,quota should-run 与 turn 自动注入公开安全的 decision_context.turn_context 观测回执与 before_work 必读提示,只读 CLI 子命令支持自动回退解析本机绑定配置,并发改绑时通过 expected_revision 拒绝旧观测并自动重读新配置,且解绑(retired)状态显式使增量上下文缓存失效。

本 PR 不改变 Core 的 Todo、Gate、Quota 准入或 Action Authority 语义,不在回合起始钩子中自动推进私有信源游标或执行外部写入,也不支持跨主机同步私有信源路径或替代 settle-review / commit-cursors 显式结算门禁。

如果不解决这一断层,驻留型 Agent 要么只能在提示词或任务描述里硬编码本机私有配置路径(直接破坏 LoopX 公开/私有边界),要么彻底退化为人工手动跑命令喂上下文,无法在多回合自治巡检中稳定感知上游决策约束变化与 P0 信源漏读风险。本次切片一次走通“本机原子绑定存储 + 回合起始零副作用观测 + 并发改绑修订号一致性保护 + 渐进上下文缓存去重 + 只读 CLI 免路径回退”,构成独立可验证且随时可通过 unbind 回退的完整闭环。

改动思路 / Approach

将 (goal_id, agent_id) 钩子绑定的状态契约、乐观并发版本校验(CAS)、投影时 expected_revision 一致性核对、追加审计日志与公开脱敏投影收敛在 TypeScript decision_context_hook.ts(复用共享 AuthorityStore),仅将本机私有 JSON/SQLite 信源读取与 CLI 参数适配保留在惰性加载的 Python turn_start_hook.py 中,既符合 TypeScript 控制面单一状态拥有者原则,又严格隔离可选观测器与核心准入状态。

当前 PR 完整闭环本机 (goal_id, agent_id) 级回合起始只读观测、并发改绑防撕裂保护、渐进上下文去重与只读 CLI 免路径回退;有副作用的 capture --execute、commit-cursors 与 settle-review 继续强制要求显式参数与独立生命周期事件核验,不纳入自动钩子范围。

下图展示精确头 a4a77f40497ff3b66dbdb7629bbcd9e44717344b 上从 quota should-run / turn 触发到本机绑定判定、只读信源扫描、expected_revision 防撕裂校验与公开安全视图投影的控制流(基于源码推导的说明性流程图,非测试执行回执):

flowchart TD
    ENTRY["CLI Entry: quota_should_run.py / turn.py"] --> STAT["turn_start_hook.extend_turn_start_dispatch<br/>stat(<runtime_root>/decision-context/agent-bindings)"]
    STAT -- "FileNotFoundError (default-off)" --> BYPASS["Return unchanged dispatch<br/>0 RPC / 0 provider imports"]
    STAT -- "Namespace exists" --> RESOLVE["TS executeDecisionContextHook(resolve_local)<br/>Read revision + local_binding"]
    RESOLVE -- "status == absent" --> BYPASS
    RESOLVE -- "active / retired" --> OBSERVE["Python _collect_public_safe_observation<br/>Read-only scan on resolved local_binding"]
    OBSERVE --> PROJECT["TS project_turn_view(expected_revision)<br/>Verify durable revision == expected_revision"]
    PROJECT -- "revision_conflict (concurrent rebind/unbind)" --> RESOLVE
    PROJECT -- "matched" --> DEDUP["Emit decision_context_hook_view_v1<br/>reusableTurnContext + progressive_context.ts"]
Loading

与仓库内既有的 loopx/control_plane/capabilities/agent_preferences.ts 及 loopx/capabilities/semantic_preference/agent_preferences.py 对比,本次设计复用了 <runtime_root> 目录 stat() 默认关闭短路、loopx/capabilities/decision_context/__init__.py 按需 __getattr__ 惰性导出,以及 capability_hooks.ts / progressive_context.ts 可复用子树去重;同时针对两阶段“先读本地绑定路径、再扫描外部文件、最后回 TS 投影”存在的并发改绑窗口,在 project_turn_view 强制核对 expected_revision === actualRevision,杜绝将旧配置 A 的观测结果挂到新绑定 B 的 revision 与 profile_ref 上。

具体改动 / Changes

本次精确头共改动 17 个文件(+2927 / -159):其中生产代码 11 个文件(+1972 / -155),测试 2 个文件(+867 / -0),中英双语协议与模块文档 4 个文件(+88 / -4),无生成产物或纯机械搬运。

对照基线规格文档 docs/reference/protocols/decision-context-architecture-v0.md(锚定不可变基线提交 6ac6c6fe285c7e3d244f70aa5aac89935b5a3366)逐条核验准入契约:

  • DC-BOUNDARY-DEFAULT-OFF(默认关闭、Goal 作用域显式激活,且不改变 Core 的 Todo、Gate、Quota 准入或 Action Authority 语义):implemented —— 由 loopx/capabilities/decision_context/turn_start_hook.py 的 extend_turn_start_dispatch 与 loopx/control_plane/capabilities/decision_context_hook.ts 的 executeDecisionContextHook 实现,经 tests/capabilities/test_decision_context_turn_hook.py 验证默认关闭完全等价及零权限扩张,并同步更新中英协议文档顶部的 TypeScript 能力钩子接缝说明。
  • DC-PUBLIC-SAFE-PACKET(公开包仅保留稳定引用、计数与状态回执,严禁泄露私有路径、游标明文或信源正文):implemented —— 由 decision_context_hook.ts(仅输出哈希引用,raw_content_captured: false,private_locator_captured: false)与 _bound_cli_commands 免路径命令保证。
  • DC-CURSOR-PRESERVE-ON-SCAN(仅扫描或预览证据绝不推进私有已审阅游标):implemented —— turn_start_hook.py 在 prepare_evidence 模式下固定传入空 DecisionEvidenceRecords() 回调且 capture_status 固定 execute=False,单测已核验游标文件零写入。
  • DC-FAIL-OPEN-HEALTH(配置缺失、并发冲突超限或检索器异常时隔离为公开可诊断的 unavailable 回执,不阻塞宿主回合调度):implemented —— 由 _collect_public_safe_observation、read_decision_context_turn_context 有界重试与 produce() 异常降级路径共同保障。

关键代码讲解

  1. loopx/control_plane/capabilities/decision_context_hook.ts:429(executeDecisionContextHook):本机钩子绑定存储的唯一状态拥有者。使用 openGoalCapabilityAuthorityStore 管理事务读写,强制校验 bind / unbind 的 expected_revision 与 operation_id 幂等重放;在 project_turn_view(第 495 行)强制要求传入 expected_revision 并与当前持久化 actualRevision 比对,不匹配时立即返回 status: "revision_conflict"(observed_binding_revision_mismatch),彻底阻断并发改绑下的跨版本撕裂视图。
  2. loopx/capabilities/decision_context/turn_start_hook.py:412(read_decision_context_turn_context):串联 TS resolve_local、Python _collect_public_safe_observation 与 TS project_turn_view,携带 expected_revision 并在遇到并发 revision_conflict 时执行最多 3 次有界重读与重采样,确保返回的 observation 与 current.revision / profile_ref 严格同源。
  3. loopx/capabilities/decision_context/turn_start_hook.py:530(extend_turn_start_dispatch):先检查 (runtime_root / "decision-context" / "agent-bindings").stat(),目录不存在或当前 (goal_id, agent_id) 为 absent 时原样返回 dispatch;retired 绑定保留空视图以触发渐进上下文缓存失效。
  4. loopx/control_plane/capability_hooks.ts:30(reusableTurnContext)与 loopx/capabilities/decision_context/__init__.py:159(__getattr__):前者校验精确 goal_id / agent_id 作用域后开放静态指令去重;后者将包级导出改为惰性加载,保障未启用回合零模块导入负担。

对主干的风险 / Risks

  1. 未启用路径的性能与输出漂移风险(Default-Off Isolation):通过包级 __getattr__ 惰性导入、turn_start_hook.py 顶层零内部依赖导入以及 stat() 快速返回三重隔离,且对同机器其他 Agent 已绑定但当前 Agent 未绑定的场景显式过滤,test_default_off_parity_when_unbound 与 cli-output-budget-regression-smoke.py 已在 file 和 sqlite 后端验证零漂移。
  2. 并发改绑撕裂与私有路径外泄风险(Concurrency & Public/Private Boundary):此前首版评审线程指出的“扫描期间并发改绑导致以 B 的 revision 返回 A 的观测”已通过 project_turn_view 的 expected_revision 强校验与 Python 侧有界重读彻底消除,并由 decision_context_hook.test.ts 与 test_turn_context_re_reads_when_binding_revision_changes_mid_observation 双端覆盖;公开载荷与免路径 CLI 命令不含任何 .local/ 路径或信源正文。
  3. 异常与回退路径:当绑定的私有配置文件缺失、损坏或连续 3 次遭遇高频并发改绑冲突时,钩子降级返回 status: "unavailable" 与结构化 error_code,绝不阻塞宿主 quota should-run 或篡改私有游标,操作者可随时执行 hook unbind --execute 退役绑定。

我的整体评价 / Verdict

综合长程效果与用户体验评估:在 long_horizon 维度为 improved(驻留 Agent 在多回合续跑中无需硬编码私有路径即可稳定获知 P0 信源漏读、变更计数与新鲜度退化,并发改绑下具备严格版本一致性);在 user_experience 维度为 improved(一次 hook bind --execute 后,后续回合与 5 个只读 CLI 命令均支持免路径本机解析,错误诊断与退役回读清晰)。已核查全部变更入口(loopx/capabilities/decision_context/cli.py、loopx/capabilities/decision_context/turn_start_hook.py 与 loopx/control_plane/capabilities/decision_context_hook.ts),本次改动仅涉及 CLI 子命令与本机控制面回合起始 JSON 契约,未改动任何桌面端、Web 前端或飞书卡片渲染界面,故无需视觉截图审查。

本地验证矩阵(启用 wait_for_ci: false 本地优先策略,不等待远端 CI)已全部实测通过:npm run -s typecheck:control-plane(PASS)、node --test tests/control_plane_ts/decision_context_hook.test.ts tests/control_plane_ts/turn_capability_context.test.ts(30/30 PASS,覆盖 FileAuthorityStore 与 SqliteAuthorityStore 及旧修订投影拒绝)、pytest 决策上下文与回合钩子测试集(含并发改绑重读测试全绿)、ruff / mypy(PASS)以及 loopx canary premerge --from-git-diff --git-diff-base origin/main(19/19 PASS)。在语义词表方面,HOOK_MODES 保持模块局部并复用既有只读子命令名称,未新增跨模块控制面枚举。历史评审线程 PRRT_kwDOStEbEs68ee1Q 已在 a4a77f40497ff3b66dbdb7629bbcd9e44717344b 修复并核验关闭,满足合入质量标准。

English verdict: APPROVE at head a4a77f4 — opt-in TypeScript-owned Decision Context turn-start hook binding enforces expected_revision consistency on projection, strict default-off parity, and public-safe redaction across File and SQLite authority stores.

@loopx-agent loopx-agent left a comment

Copy link
Copy Markdown
Collaborator Author

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

Approval conclusion (author-owned PR; GitHub blocks formal self-approval)

动机 / Motivation

为同一 Goal/Agent 提供默认关闭的本机 Decision Context 绑定,让 Turn 读取绑定时可了解来源新鲜度和精确入口。 目标基线无常驻绑定;上一受评候选已提供 opt-in 路径,但能把一次重绑前的观察归到后续 revision。 绑定私有 Decision Context profile 的 Agent 和维护绑定的操作者。

操作者重绑以变更下一轮来源,错配会让 Agent 把旧 P0 清单和新鲜度当作当前事实。 在 File 和 SQLite Goal 夹具上,通过 decision-context hook turn-context、quota should-run 交叉复验。首次收集读到旧配置,CLI 捕获合法重绑后重新读取。四组都在同一新 revision 返回新 P0 来源,与随后稳定读回一致。 独立重绑矩阵 4/4;需改的上次候选 4 组旧反例保留。候选定向 Python 6/6、TypeScript 30/30、双语 RFC 校验通过。生产修改只涉及版本比较及有界重读。 未绑定的同一 File/SQLite Goal 经基线与候选真实 quota CLI 读回,准入结论及必读来源保持一致。 真实 CLI 使用本机私有绑定 store 与隔离 File/SQLite Goal。TS lifecycle 另在 File/SQLite store 上直接验证。安装 App 与远端 CI 未核验。 基线契约

改动思路 / Approach

该改动沿用 hook turn-context / quota should-run → resolve_local → collect → project_turn_view。沿用现有 authority revision 和 typed Hook。TS 拥有绑定与拒绝;Python 负责私有来源 I/O 与有界重读。 解析本机绑定时保留当时 revision。TS 用同一个 durable read 的现有 revision 做精确比较;若快照已改,Python 重解并重收集,最多三次。 保留一次版本比较和有界重读;可共用已退休与活动路径的投影调用参数,属于局部清理。

具体改动 / Changes

精确提交 a4a77f4,本次对照目标 6ac6c6f。全 PR 17 文件 +2927/−159,相对上一轮受评头 4 文件 +222/−58。修正集中在重绑的一致性、目标测试和有界重读。

关键代码讲解

  • read_decision_context_turn_context。在旧观察已失效时重新解析绑定并重新收集。 源码

  • project_turn_view。使用同一次 durable read 的 actualRevision 拒绝不匹配输入。 源码

  • revision_conflict 分支。只重试明确版本冲突,其余结果直接返回。 源码

  • dc-bound-revision-consistency(implemented)。绑定 revision 和来源观察必须同源。未绑定继续保持默认关闭,回执不含私有内容。

对主干的风险 / Risks

上一个候选在四组对照都把旧来源与新 revision 配对;此提交四组均关闭错配。旧 revision 的投影请求被 TypeScript owner 拒绝,未绑定、其他 Agent 隔离、缺配置回执与私有字段约束仍通过。 连续改绑的观测成本按三次预算有界;重复消耗私有来源的生产时延未测。共享退出用旧行为失败,未改入口授权。 保留本次 revision fence 及双层对照用例。

我的整体评价 / Verdict

APPROVE。 旧 P2 的四组反例在此头关闭,默认关闭与私有边界的定向检查通过。 复评确认稳定配置的证据身份可追踪。没有新增安装或合并资格。

English verdict: APPROVE at a4a77f4. The previous four File/SQLite Goal and CLI rebind counterexamples now return the new observation with its exact revision. Opt-in and redacted readback tests passed.

@huangruiteng
huangruiteng merged commit 379b9ea into main Nov 26, 2026
15 of 18 checks passed
@huangruiteng
huangruiteng deleted the codex/decision-context-hook-20261126 branch November 26, 2026 18:39
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