Long-running work accumulates material: the original goal, later constraints, attempted routes, failed experiments and current progress. Sending everything again on each continuation repeats more and more content; keeping only a summary can make the original basis of a decision difficult to recover.
+Context delivery
An Agent needs a way to look things up, too
Read necessary facts first, reuse verified structures within a compaction cycle, and recover the complete baseline when continuity is lost.
Long-running work accumulates material: the original goal, later constraints, attempted routes, failed experiments and current progress. Sending everything again on each continuation repeats more and more content; keeping only a summary can make the original basis of a decision difficult to recover.
Selective JSON Pointer lookup by itself is straightforward—it folds long-tail history not needed for the immediate decision. The core mechanism in LoopX's context delivery is compaction-cycle-bounded structured delta context: complete sources are saved first, unchanged typed subtrees are reused exactly against a verified baseline within the active context_epoch, and any host compaction or digest mismatch atomically resets to a fresh complete baseline rather than chaining dangling deltas.
Evidence: separate context comparisons, cycle deltas and task ablation
Four mechanisms, with separate measurement baselines
@@ -111,6 +111,101 @@Eight tasks: single-layer payloads (Progr
Basis and source: the published eight task rows sum to 7,285,951 B / 22,408,690 B = 32.5% for Progressive across all 213 wakes. Delta and first-screen shares of approximately 35.8% and 3.0% / 5.0% cover only the 112 in-cycle wakes. Their Full denominator was published rounded as 12.53 MB; it is not the 213-wake total. This is an aggregate recalculation, not a new experiment or an independent audit of per-wake logs. Public aggregate data and provenance.
Implementation status: #8266 remains an open C1 proposal. The final merged scope of #8304 preserves precision while reusing ordering and a verified digest. These cohort bytes do not establish a shipped unified Controller/Worker gain.
+Ten-task controlled comparison: Context ON vs Context OFF under fixed Explore + Stride
+Cutting packet bytes only matters if long-horizon task performance holds up or improves. From the EdgeBench 10-task evaluation (2026-11-12 22:40 CST snapshot), we isolate the controlled two-arm contrast holding the same Explore + Stride harness fixed: Explore + context-on (progressive reading + compaction-cycle structured delta enabled) vs Explore + context-off (both disabled), testing only what switching Context Delivery on vs off changes within that chassis.
-
+
- Ten-task tally (7 ahead or slight edge vs 3 OFF ahead): under fixed
Explore + Stride, enabling Context Delivery putsExplore + context-onahead of or slightly ahead ofcontext-offon 7 of 10 tasks—including Borden (80 vs 45, +77.8%), ANN (7,159 vs 3,746 QPS, +91.1%), and Grid (2,197 vs 2,311cost, −114)—while explicitly preserving all 3 counterexamples wherecontext-offscores higher.
+ - Strictly duration-matched ~18h pairs (4 tasks within the 15-minute tolerance): 3 wins to 1—Borden rises from
45to80(+35 pts / +77.8%); Grid total cost (lower is better) improves from2,311to2,197(−114 / −4.9%, +2.35 normalized pts); Rust (both17h57m) shows a slight native edge forcontext-on(27.1801 vs 27.1757); the matched 18h counterexample is Portfolio (both18h00m,13.62 vs 39.44, OFF wins).
+ - Historical Stride slice & incomplete-duration pairs (6 tasks, runtimes stated explicitly): using the matching Stride pair from the historical seven-arm ANN cohort,
context-onreaches7,159 QPSvscontext-off3,746 QPS(+91.1%, +33.51 normalized pts); FFmpeg reaches2.7086 vs 1.9929(+35.9%, OFF stopped early pending rerun); VLIW cycles (lower is better) improve to1,042 vs 1,097(−55 cycles / −5.0%, OFF historical 14h45m); Ad reaches49.67B vs 49.63B(ON 18h00m vs OFF 8h47m host-startup hang). The two remaining counterexamples are Codec (ON full 18h00m68.89vs OFF70.79in 12h38m despite a model-service block) and OpenTTD (both Explore arms <18h, ON30,266,729vs OFF33,090,430).
+
Reconciling two figures with the ablation section below: (1) this section's ANN pair (3,746 vs 7,159 QPS) comes from the two Stride-enabled Explore arms in the seven-arm cohort (Explore + Stride + context-off / on); #ablation-evidence separately lists the without-Stride Explore baseline (Explore · Context off 5,766 and Explore · Context on 6,020) from that study, so the two tables describe distinct arm pairs. (2) Grid here (2,197 vs 2,311) reports the completed ~18h pair within 15-minute tolerance (with context-on via same-session continuation), whereas #ablation-evidence retains the earlier unmatched-duration “Historical Grid” counterexample (Off 2054 < On+Stride 2088) for historical continuity.
EdgeBench 10-task controlled score comparison (fixed Explore + Stride: Context ON vs OFF)
ON − OFF delta, showing arm runtime status. B: Native-point summary including task #10 Borden (no /100 scale, matched ~18h: 80 vs 45), key native deltas, and counterexample disclosures.Expand the 10-task controlled score and runtime table
+| Task (metric & direction) | +Duration parity | +Explore + context-on Native (/100 · duration · valid) |
+Explore + context-off Native (/100 · duration · valid) |
+ON vs OFF delta | +
|---|---|---|---|---|
| Borden (score ↑) | +Matched ~18h | +80 (— · 18h02m · 17 valid) | +45 (— · 17h59m · 17 valid) | ++35 (+77.8%) | +
| Grid (total cost ↓) | +Matched ~18h | +2,197 (67.20 · 17h57m · 17 valid) | +2,311 (64.85 · 17h59m · 13 valid) | +−114 (−4.9%, +2.35 pts) | +
| Rust (score ↑) | +Matched ~18h | +27.1801 (27.18 · 17h57m · 10 valid) | +27.1757 (27.18 · 17h57m · 10 valid) | ++0.0044 (tied / slight edge) | +
| Portfolio (score / 100 ↑) | +Matched 18h counterex. | +13.62 (13.62 · 18h00m · 17 valid) | +39.44 (39.44 · 18h00m · 17 valid) | +−25.82 pts (OFF wins) | +
| ANN (QPS ↑) | +7-arm Stride cohort | +7,159 (43.17 · 6 valid) | +3,746 (9.66 · 4 valid) | ++3,413 (+91.1%, +33.51 pts) | +
| FFmpeg (score ↑) | +OFF early stop | +2.7086 (16.17 · 18h01m · 16 valid) | +1.9929 (11.19 · early stop · 6 valid) | ++0.7157 (+35.9%, +4.98 pts) | +
| VLIW (cycles ↓) | +OFF 14h45m hist. | +1,042 (97.25 · 17h57m · 14 valid) | +1,097 (93.82 · 14h45m · 12 valid) | +−55 cycles (−5.0%, +3.43 pts) | +
| Ad (total score ↑) | +OFF 8h47m hang | +49,673,040,827 (79.30 · 18h00m · 16 valid) | +49,631,904,944 (78.33 · 8h47m · 7 valid) | ++41.14M (+0.97 pts) | +
| Codec (score ↑) | +OFF 12h38m counterex. | +68.89 (68.89 · 18h00m · 13 valid) | +70.79 (70.79 · 12h38m · 11 valid) | +−1.90 pts (OFF wins) | +
| OpenTTD (score ↑) | +Both <18h counterex. | +30,266,729 (62.02 · 10h47m · 10 valid) | +33,090,430 (62.99 · early stop · 7 valid) | +−2.82M (−0.97 pts, OFF wins) | +
Measurement and scope boundary: these are C1 mixed-cohort exploratory observations (active solver duration accumulated after excluding downtime). This section holds Explore + Stride constant solely to examine the directional effect of toggling context delivery (Context ON vs Context OFF) within the harness; because resumed sessions and incomplete durations remain, it does not establish an isolated C2 causal proof. Whole-system iso-budget Pareto comparisons against zero-overhead external baselines belong to a separate question addressed in Layer 2 of EdgeBench feedback and memory. Structured records live in public aggregate data.
Multi-task and layered ablation: benefit chain and boundary counterexamples
In EdgeBench ANN vector search optimization (gpt-6.1-sol / xhigh, 18h, subject to Recall@10 ≥ 0.95) and cross-task ablations, we separate Context Delivery (progressive selection and cycle deltas) from Stride (evidence-conditioned experiment sizing), auditing selected control-packet bytes, main-session model input tokens, and local/early-window counterexamples side by side (reconciled from archived trajectories and receipts by @shangzh0):
Multi-task ablation and layered accounting
Recall@10 ≥ 0.95). Bottom (B): Layered control-packet byte and main-session input token savings vs Context off (left, negative) alongside disclosed local/early-window overhead and cross-task counterexamples (right, positive). Open image for full size.LoopX · Blog
Engineering the long run.
Design, engineering, and field notes on long-horizon agents.
Context delivery · Mechanisms and examples
· Updated November 12
An Agent needs a way to look things up, too
Read progressive views and compaction-cycle deltas with their own measurement baselines: 32.5% across 213 wakes, −74.25% on ANN in-cycle wakes, and ≈ 3.0% / 5.0% eight-task first-read entry. Compaction resets the complete baseline; these cohorts are separate comparisons.
Read articleContext delivery · Mechanisms and examples
· Updated November 14
An Agent needs a way to look things up, too
Compare 10-task controlled Explore + Stride scores under Context ON vs OFF (7/10 ahead or edged out—Borden 80 vs 45, ANN 7,159 vs 3,746—with Portfolio/Codec/OpenTTD counterexamples retained), alongside separate 32.5% Progressive, −74.25% ANN in-cycle delta, and ≈ 3.0% / 5.0% first-read baselines.
Read articleResearch notes · EdgeBench and LHTB
Agent 做长任务,资料会越来越多:原目标、后来追加的约束、试过的方案、失败的实验、当前做到哪里。每次继续都把整份材料再塞一遍,重复会不断累积;只留一段摘要,又可能找不回决定所依赖的原话。
+Agent 做长任务,资料会越来越多:原目标、后来追加的约束、试过的方案、失败的实验、当前做到哪里。每次继续都把整份材料再塞一遍,重复会不断累积;只留一段摘要,又可能找不回决定所依赖的原话。
按 JSON Pointer 渐进选读本身并不复杂,主要用来折叠当前决定暂不需要的历史长尾;LoopX 上下文交付真正的机制重心,是受 compaction 周期约束的结构化差分上下文(compaction-cycle structured delta):完整来源先落盘存证,在同一个已核验的 context_epoch 内精确复用未变化的结构子树、只编码本轮结构增删改;一旦宿主发生上下文压缩或基线摘要失配,立即原子切换回新的完整基线,绝不悬空叠加旧差分。
先看数据:独立上下文对照、周期内结构化差分与任务消融
四种机制,分别核对测量基线
@@ -111,6 +111,101 @@八题对照:区分单层载荷(Progr
口径与来源:上表逐题数据复算自已公开的八题表,Progressive 合计为 7,285,951 B / 22,408,690 B = 32.5%,覆盖全部 213 次唤醒。Delta 与首屏的 35.8%、3.0% / 5.0% 仅对应 112 次周期内唤醒;其 Full 分母原文仅报告约 12.53 MB,不能与 213 次总量混用。这里没有新增实验,也未独立核对逐 wake 原始记录。公开聚合数据与来源。
实现状态:#8266 仍是开放的 C1 提案;#8304 的最终合入范围是保留精度的排序与摘要复用。上述字节数据不归因为统一 Controller/Worker 已上线的收益。
+十题同构两臂效果对比:固定 Explore + Stride 下的 Context ON 与 Context OFF
+在压降上下文传输载荷的同时,我们进一步检验这套交付机制在长程求解中的实际得分表现:从 EdgeBench 十题实测(2026-11-12 22:40 CST 快照)中取出固定同一 Explore + Stride 框架的两臂对照:Explore + context-on(开启 Progressive 按需读取与周期内结构化 Delta)对比 Explore + context-off(两项均关闭),专门检验 Context Delivery 单一开关在同构底盘下的组内表现。
-
+
- 十题整体盘面(7 题领先或微弱占先 vs 3 题 OFF 领先):固定
Explore + Stride开启 Context Delivery 后,10 题中有 7 题取得更高或微弱占先的成绩(包括 Borden 80 vs 45 [+77.8%]、ANN 7,159 vs 3,746 QPS [+91.1%]、Grid 2,197 vs 2,311 [−114 代价]);同时完整保留 3 项 Context OFF 领先的反例,不做选择性截取。
+ - 严格等时长约 18h 配对(4 题,15 分钟容差内):3 胜 1 负——
Borden从 45 升至 80(+35 分 / +77.8%);Grid代价(越低越好)从 2,311 降至 2,197(−114 / −4.9%,+2.35 百分分);Rust(均 17h57m)维持在 27.1801 vs 27.1757 的微弱占先;等时长反例则是Portfolio(均为 18h00m,13.62 vs 39.44,OFF 领先)。
+ - 历史同构与非等时长对照(6 题,逐题标明实际运行时间):
ANN取历史七臂中同开 Stride 的两臂配置,context-on达 7,159 QPS 对context-off3,746 QPS(+91.1%,+33.51 百分分);FFmpeg2.7086 vs 1.9929(+35.9%,OFF 早停待重跑);VLIW周期数(越低越好)1,042 vs 1,097(−55 周期 / −5.0%,OFF 沿用 14h45m 历史轮);Ad49.67B vs 49.63B(ON 满 18h00m,OFF 因宿主启动空挂仅累计 8h47m)。另外两项反例为Codec(ON 满 18h00m 得 68.89,不敌模型服务拦截下仅跑 12h38m 的 OFF 70.79)与OpenTTD(两臂均未满 18h,ON 30,266,729 vs OFF 33,090,430)。
+
与下方消融表两处口径衔接说明:(1)本节 ANN 对照 3,746 vs 7,159 QPS 来自历史七臂里同开 Stride 的两臂(Explore + Stride + context-off / on);下文 #ablation-evidence 另行列出该研究中未开 Stride 的 Explore 基线(Explore · Context off 5,766 与 Explore · Context on 6,020),二者对照条件不同,互不冲突。(2)本节 Grid(2,197 vs 2,311)反映 15 分钟容差内均跑满 ~18h 的最新完成对比(其中 context-on 沿同一会话续跑);下文仍保留更早一轮未匹配时长与资源的「Historical Grid」反例(Off 2054 < On+Stride 2088)作历史记录。
EdgeBench 十题同构两臂效果对照(固定 Explore + Stride:Context ON vs OFF)
ON − OFF 分差排序(已方向归一化为越高越优),标出各自时长状态。B:无百分制标度的第 10 题 Borden(等时长 ~18h,80 vs 45)、关键原生分增幅,以及 3 项负向反例口径。展开十题同构两臂逐题分数与运行时长明细
+| 题目(指标与方向) | +时长配对状态 | +Explore + context-on 原生(/100 · 时长 · 有效次) |
+Explore + context-off 原生(/100 · 时长 · 有效次) |
+ON vs OFF 差值 | +
|---|---|---|---|---|
| Borden(原生分 ↑) | +等时长 ~18h | +80(— · 18h02m · 17 次) | +45(— · 17h59m · 17 次) | ++35(+77.8%) | +
| Grid(原生总分 ↓) | +等时长 ~18h | +2,197(67.20 · 17h57m · 17 次) | +2,311(64.85 · 17h59m · 13 次) | +−114(−4.9%, +2.35 分) | +
| Rust(原生分 ↑) | +等时长 ~18h | +27.1801(27.18 · 17h57m · 10 次) | +27.1757(27.18 · 17h57m · 10 次) | ++0.0044(微先 / 持平) | +
| Portfolio(原生分 / 100 ↑) | +等时长 18h 反例 | +13.62(13.62 · 18h00m · 17 次) | +39.44(39.44 · 18h00m · 17 次) | +−25.82 分(OFF 胜) | +
| ANN(QPS ↑) | +历史七臂 Stride | +7,159(43.17 · 6 次) | +3,746(9.66 · 4 次) | ++3,413(+91.1%, +33.51 分) | +
| FFmpeg(原生分 ↑) | +OFF 早停待重跑 | +2.7086(16.17 · 18h01m · 16 次) | +1.9929(11.19 · 早停 · 6 次) | ++0.7157(+35.9%, +4.98 分) | +
| VLIW(周期数 ↓) | +OFF 14h45m 历史 | +1,042(97.25 · 17h57m · 14 次) | +1,097(93.82 · 14h45m · 12 次) | +−55 周期(−5.0%, +3.43 分) | +
| Ad(原生总分 ↑) | +OFF 8h47m 空挂 | +49,673,040,827(79.30 · 18h00m · 16 次) | +49,631,904,944(78.33 · 8h47m · 7 次) | ++41.14M(+0.97 分) | +
| Codec(原生分 ↑) | +OFF 12h38m 反例 | +68.89(68.89 · 18h00m · 13 次) | +70.79(70.79 · 12h38m · 11 次) | +−1.90 分(OFF 胜) | +
| OpenTTD(原生分 ↑) | +两臂均未满 反例 | +30,266,729(62.02 · 10h47m · 10 次) | +33,090,430(62.99 · 早停 · 7 次) | +−2.82M(−0.97 分, OFF 胜) | +
实验口径与边界说明:以上属于 C1 多轮探索观测(有效运行时间按求解进程扣除停机后累计)。本节固定 Explore + Stride 框架,仅隔离检验开启上下文交付(Context ON vs Context OFF)在组内的方向性表现;因存在续跑会话与部分未补满时长差异,不单独构成剥离全部交互项后的纯 C2 因果证明。控制面整体相对零协议税外部基线的等预算帕累托对比属另一层独立议题,详见 《EdgeBench 反馈与记忆》中的第二层目标讨论。原始聚合结构化数据见 公开聚合数据。
真实模型与多题消融实证:收益链路与边界反例
在 EdgeBench ANN 向量检索优化(gpt-6.1-sol / xhigh,18h,约束 Recall@10 ≥ 0.95)与跨题消融中,我们将 Context Delivery(渐进读取与周期差分)与 Stride(基于证据差分的步长控制)拆开对照,同时统计控制包选中字节、主会话模型输入 Token 与局部/早窗反例(基于 @shangzh0 归档轨迹与控制面回执复算):
多题消融与分层计量审计
Recall@10 ≥ 0.95)。下图(B):相对 Context off 的分层字节与主会话输入 Token 节省(左,负值)及诚实披露的局部/早窗开销与跨题反例(右,正值)。点击查看原图。LoopX · 博客
长程工作的工程实践。
关于长程 Agent 的设计、工程与实践。
Context delivery · 机制与实例
· 11 月 12 日更新
Agent 读上下文,也需要一套“查书”的方法
分别核对渐进视图与周期内差分的测量基线:213 次唤醒保留 32.5%,ANN 周期内增量轮再减 74.25%,八题首读入口约 3.0% / 5.0%。这些队列独立比较,压缩或失配即恢复完整基线。
阅读全文Context delivery · 机制与实例
· 11 月 14 日更新
Agent 读上下文,也需要一套“查书”的方法
拆解十题同构两臂(固定 Explore + Stride)下的 Context ON vs OFF 跑分对比(7/10 题占先、Borden 80 vs 45、ANN 7,159 vs 3,746,并保留 Portfolio/Codec/OpenTTD 反例),同时核对渐进视图 32.5%、ANN 周期内增量轮 −74.25% 与首读 3.0% / 5.0% 的载荷基线。
阅读全文研究札记 · EdgeBench 与 LHTB
',
+ `Both published editions must expose the controlled Context ON vs OFF score section: ${locale}`,
+ );
+ assertIncludes(
+ article,
+ `stride-context-on-vs-off-${langSuffix}.svg`,
+ `Both published editions must embed the controlled Context ON vs OFF chart: ${langSuffix}`,
+ );
+ const twoArmSvg = await readFile(
+ resolve(blogDir, `images/context-delivery/stride-context-on-vs-off-${langSuffix}.svg`),
+ "utf8",
+ );
+ const twoArmSection = article.split('')[1]?.split('')[0] ?? "";
+ const twoArmTable = twoArmSection.match(/([\s\S]*?)<\/tbody>/)?.[1];
+ const twoArmRows = [...(twoArmTable ?? "").matchAll(/([\s\S]*?)<\/tr>/g)];
+ deepStrictEqual(
+ twoArmRows.length,
+ twoArm.tasks.length,
+ `Controlled two-arm score table must retain all 10 task rows including counterexamples: ${locale}`,
+ );
+ for (const requiredToken of ["7,159", "3,746", "2,197", "2,311", "27.1801", "13.62", "39.44", "68.89", "70.79"]) {
+ assertIncludes(twoArmSection, requiredToken, `Controlled score section must preserve ${requiredToken} (${locale})`);
+ }
+ for (const isolatedText of [twoArmSection, twoArmSvg, JSON.stringify(twoArm)]) {
+ if (/four-arm|four arm|四臂/i.test(isolatedText)) {
+ throw new Error(`Controlled two-arm section must remain isolated without four-arm framing (${locale})`);
+ }
+ }
+ for (const publicText of [article, twoArmSvg, JSON.stringify(audit)]) {
+ if (/sforge/i.test(publicText)) {
+ throw new Error(`Internal benchmark name leaked into public context-delivery blog artifact (${locale})`);
+ }
+ }
if (/7,479,469|33\.4%|69\.9 KB|57\.1ms/.test(article))
throw new Error("Unreconciled totals or unsupported C1 attribution reappeared");
}
diff --git a/examples/coordination/remote-authority-client.ts b/examples/coordination/remote-authority-client.ts
index 9485c66d48..21232642b7 100644
--- a/examples/coordination/remote-authority-client.ts
+++ b/examples/coordination/remote-authority-client.ts
@@ -15,5 +15,5 @@ const result = await callRemoteAuthority(values.endpoint, process.env.LOOPX_AUTH
decodeRemoteAuthorityRequest(JSON.parse(raw)), {mode: values.recover ? "recover" : "execute",
...(values.ca ? {ca: await readFile(values.ca)} : {})});
process.stdout.write(`${JSON.stringify(result)}\n`);
-if (!["found", "missing", "result"].includes(String(result.status)) ||
+if (!["found", "missing", "result", "snapshot"].includes(String(result.status)) ||
result.status === "result" && !["applied", "no_change", "replayed", "recovered"].includes(String((result.result as Record)?.status))) process.exitCode = 1;
diff --git a/examples/loopx-chat-actions-smoke.py b/examples/loopx-chat-actions-smoke.py
index 63b921ef5e..d0ac017b36 100644
--- a/examples/loopx-chat-actions-smoke.py
+++ b/examples/loopx-chat-actions-smoke.py
@@ -105,25 +105,43 @@ def __init__(self, chat_store: ChatSessionStore) -> None:
def open_session(self, **kwargs: object) -> tuple[dict[str, object], bool]:
self.opened_sessions.append(dict(kwargs))
- existing = self.chat_store.latest_session(
+ managed_agent = kwargs.get("managed_agent_id")
+ channel = f"agent.{managed_agent}" if managed_agent is not None else str(kwargs["channel_id"])
+ session_id = kwargs.get("session_id")
+ existing = self.chat_store.load_session(str(session_id)) if session_id is not None else self.chat_store.latest_session(
goal_id=str(kwargs["goal_id"]),
agent_id=str(kwargs["agent_id"]),
- channel_id=str(kwargs["channel_id"]),
+ channel_id=channel,
)
if existing is not None:
return existing, True
session = self.chat_store.create_session(
goal_id=str(kwargs["goal_id"]),
+ goal_instance_id=kwargs.get("goal_instance_id"),
+ **({"session_id": str(session_id)} if session_id is not None else {}),
+ **({"managed_agent_id": str(managed_agent), "managed_workspace_path": str(Path(str(kwargs["work_dir"])).resolve())}
+ if managed_agent is not None else {}),
agent_id=str(kwargs["agent_id"]),
adapter_kind="fake",
upstream_thread_id="thread-goal-create",
- channel_id=str(kwargs["channel_id"]),
+ upstream_mode="chat" if kwargs["agent_id"] == "codex" else "default",
+ channel_id=channel,
)
return session, False
def submit_turn(self, **kwargs: object) -> tuple[dict[str, object], bool]:
self.submissions.append(dict(kwargs))
- return ({"turn_id": "turn-correction-1", "status": "queued"}, True)
+ session_id = str(kwargs["session_id"])
+ turn, created = self.chat_store.create_turn(session_id,
+ client_turn_id=str(kwargs["client_turn_id"]), message=str(kwargs["message"]))
+ if created:
+ # The synchronous fake finishes its ordinary reply through the real
+ # Chat completion owner; it never accepts a Goal or Task outcome.
+ self.chat_store.update_turn(session_id, str(turn["turn_id"]),
+ status="completing", started_at=turn["created_at"], completed_at=turn["created_at"],
+ upstream_turn_id=f"synthetic-{turn['turn_id']}", response={"message": "Synthetic ordinary reply."})
+ turn = self.chat_store.finalize_managed_turn_completion(session_id, str(turn["turn_id"]))
+ return turn, created
def capabilities(self) -> list[dict[str, object]]:
return [
@@ -369,7 +387,8 @@ def assert_http_action_api(root: Path) -> None:
)
assert code == 202, correction_applied
assert correction_applied["proposal"]["status"] == "applied", correction_applied
- assert correction_applied["turn"]["turn_id"] == "turn-correction-1", correction_applied
+ original_correction = chat_store.turn_for_client(session["session_id"], str(runtime_controller.submissions[1]["client_turn_id"]))
+ assert correction_applied["turn"]["turn_id"] == original_correction["turn_id"], correction_applied
assert runtime_controller.submissions[1]["session_id"] == session["session_id"]
goal_proposal = previews["goal.create"]
diff --git a/examples/personal-workspace-browser/goal-draft.mjs b/examples/personal-workspace-browser/goal-draft.mjs
index 1054c665de..e775b92f33 100644
--- a/examples/personal-workspace-browser/goal-draft.mjs
+++ b/examples/personal-workspace-browser/goal-draft.mjs
@@ -28,6 +28,10 @@ export const goalDraftScenario = {
const requests = api.turnRequests.length;
await card.getByRole("button", {name: "资本支出变化", exact: true}).click();
assert.equal(await composer.inputValue(), "资本支出变化");
+ await composer.fill("保留现金流的判断。");
+ await card.getByRole("button", {name: "资本支出变化", exact: true}).click();
+ assert.equal(await composer.inputValue(), "保留现金流的判断。\n资本支出变化",
+ "An existing suggestion appends to the current draft");
assert.equal(api.turnRequests.length, requests, "Suggestion auto-sent a request");
assert.equal(api.actionPreviews.length, 0);
assert.equal(api.durableWriteCount, writes);
@@ -39,6 +43,10 @@ export const goalDraftScenario = {
assert.equal(api.durableWriteCount, writes, "Preparing a complete draft cannot create a Goal");
await card.getByRole("button", {name: "重新准备", exact: true}).click();
await card.getByRole("button", {name: "创建并开始", exact: true}).waitFor();
+ await card.locator(".personal-goal-creation-execution").getByText("只读", {exact: true}).waitFor();
+ await card.locator(".personal-goal-creation-execution").getByText("无", {exact: true}).waitFor();
+ assert.equal(api.actionPreviews.at(-1).normalized_parameters.start_execution, true,
+ "A fresh Codex Create and start explicitly requests execution");
assert.equal(await page.locator('.personal-proposal-row[data-action-kind="goal.create"]').count(), 0,
"The visible draft owns one confirmation instead of a duplicate action card");
await card.getByText("codex", {exact: true}).waitFor();
@@ -71,13 +79,17 @@ export const goalDraftScenario = {
await objective.fill(`\u0085${draft.objective}\u001c`);
await boundary.fill(draft.execution_boundary);
await form.getByLabel("完成标准", {exact: true}).fill("资本支出对照表与原始来源");
+ await form.getByLabel("执行权限", {exact: true}).selectOption("workspace_write_on_confirmation");
await form.getByRole("button", {name: "检查配置"}).click();
await page.getByText("确认执行", {exact: true}).waitFor();
const preview = api.actionPreviews.at(-1);
assert.equal(preview.action_kind, "goal.create");
assert.equal(preview.normalized_parameters.title, draft.objective);
assert.equal(preview.normalized_parameters.workspace_ref, "current");
- assert.equal(preview.normalized_parameters.permission, "read_only");
+ assert.equal(preview.normalized_parameters.permission, "workspace_write_on_confirmation");
+ assert.equal(preview.normalized_parameters.start_execution, true);
+ await page.locator(".personal-context-drawer .personal-goal-creation-execution")
+ .getByText("所选工作区的所有文件", {exact: true}).waitFor();
assert.equal(preview.normalized_parameters.heartbeat.enabled, false);
assert.equal(api.durableWriteCount, writes);
await page.getByRole("button", {name: "关闭", exact: true}).click();
diff --git a/examples/personal-workspace-browser/goal-workspace-backend.py b/examples/personal-workspace-browser/goal-workspace-backend.py
index 8b4ba4d2e6..7406701a69 100644
--- a/examples/personal-workspace-browser/goal-workspace-backend.py
+++ b/examples/personal-workspace-browser/goal-workspace-backend.py
@@ -30,7 +30,23 @@
registry.write_text(json.dumps(data), encoding="utf-8")
runtime = Path(data["common_runtime_root"])
store, sessions = ChatActionStore(runtime / "chat/actions"), ChatSessionStore(runtime)
-controller = fixture["FakeRuntimeController"](sessions)
+
+
+class WorkspaceRuntimeController(fixture["FakeRuntimeController"]):
+ def __init__(self, store: ChatSessionStore, registry_path: Path, runtime_root: Path) -> None:
+ super().__init__(store)
+ self.store = store
+ self.registry_path = registry_path
+ self.coordination_runtime_root = runtime_root
+
+ def resume_session_queue(self, *, session_id: str, work_dir: Path, objective: str) -> None:
+ # Observe the queue target at the synthetic dispatch boundary.
+ session = self.store.load_session(session_id)
+ assert session is not None
+ assert Path(session["managed_workspace_path"]) == Path(work_dir)
+
+
+controller = WorkspaceRuntimeController(sessions, registry, runtime)
# One Dashboard-configured workspace that no Goal uses yet.
configured = root / "docs-site"
configured.mkdir()
diff --git a/examples/personal-workspace-browser/goal-workspace-selection.mjs b/examples/personal-workspace-browser/goal-workspace-selection.mjs
index 5b9a03f2a1..bfca50ca3a 100644
--- a/examples/personal-workspace-browser/goal-workspace-selection.mjs
+++ b/examples/personal-workspace-browser/goal-workspace-selection.mjs
@@ -91,10 +91,18 @@ runpy.run_path(sys.argv[0], run_name="__main__")
assert.equal(await page.getByText(backend.second, {exact: false}).count(), 0);
assert.equal(JSON.parse(await readFile(backend.registry, "utf8")).goals.length, 2, "Preview created a Goal");
const confirm = page.getByRole("button", {name: /创建 Goal.*开始首轮/});
+ const confirmation = page.waitForResponse((response) =>
+ response.request().method() === "POST" && new URL(response.url()).pathname.endsWith("/apply"));
await confirm.click();
- await page.waitForFunction(() => document.body.innerText.includes("进入新 Goal"));
+ assert.ok((await confirmation).ok(), JSON.stringify(applied[0]));
assert.equal(applied.length, 1);
- assert.equal(applied[0].proposal.receipt.projection_verified, true);
+ const receipt = applied[0].proposal.receipt;
+ assert.equal(receipt.projection_verified, true);
+ assert.equal(receipt.original_task_request.todo_id, receipt.resource_ids.todo_ids[0]);
+ assert.equal(receipt.original_task_request.session_id, receipt.resource_ids.session_id);
+ assert.equal(receipt.original_task_request.permission, "read_only");
+ assert.deepEqual(receipt.original_task_request.write_scope, []);
+ await page.getByRole("button", {name: "进入新 Goal", exact: true}).waitFor();
const goalId = requests.at(-1).normalized_parameters.goal_id;
const registry = JSON.parse(await readFile(backend.registry, "utf8"));
const created = registry.goals.find(goal => goal.id === goalId);
diff --git a/examples/personal-workspace-browser/typed-actions.mjs b/examples/personal-workspace-browser/typed-actions.mjs
index a4dc8fb3cf..fe06151219 100644
--- a/examples/personal-workspace-browser/typed-actions.mjs
+++ b/examples/personal-workspace-browser/typed-actions.mjs
@@ -325,13 +325,17 @@ export const typedActionsScenario = {
}
const writesBeforeSelection = api.durableWriteCount;
await drawer.getByRole("button", { name: /Workspace 1/ }).click();
- await page.getByRole("button", { name: "创建 Goal 并开始首轮", exact: true }).waitFor({ state: "visible" });
+ const create = drawer.getByRole("button", { name: "创建 Goal", exact: true });
+ await create.waitFor({ state: "visible" });
const regenerated = api.actionPreviews.at(-1);
if (regenerated?.normalized_parameters.workspace_ref !== "workspace-fixture") {
throw new Error(`Stored workspace selection did not regenerate the Goal preview: ${JSON.stringify(regenerated)}`);
}
+ if (regenerated.normalized_parameters.start_execution !== storedWorkspaceGate.normalized_parameters.start_execution) {
+ throw new Error("Workspace selection changed the saved execution intent");
+ }
if (api.durableWriteCount !== writesBeforeSelection) throw new Error("Selecting a workspace applied without confirmation");
- await page.getByRole("button", { name: "创建 Goal 并开始首轮", exact: true }).click();
+ await create.click();
await page.getByText("已应用,LoopX 状态将刷新。", { exact: true }).waitFor({ state: "visible" });
if (!api.actionApplies.includes(regenerated.proposalId)) throw new Error("Confirmation did not apply the regenerated preview");
if (api.durableWriteCount !== writesBeforeSelection + 1) throw new Error("Confirmation did not write exactly one Goal");
diff --git a/loopx/agent_onboarding.py b/loopx/agent_onboarding.py
index 7bacb923ab..bee609f34b 100644
--- a/loopx/agent_onboarding.py
+++ b/loopx/agent_onboarding.py
@@ -482,10 +482,12 @@ def build_agent_onboarding_packet(
registry_path,
resolved_goal_id,
)
+ activation_registry = str(registry_path) if canonical_agent_type == "antigravity-app" else None
host_loop_activation = build_host_loop_activation_packet(
agent_type=canonical_agent_type,
goal_id=resolved_goal_id,
cli_bin=cli_bin,
+ registry=activation_registry,
agent_id=agent_id,
registered_agents=registered_agents,
available_capabilities=available_capabilities,
@@ -516,6 +518,7 @@ def build_agent_onboarding_packet(
render_quota_guard_command(
resolved_goal_id,
cli_bin=cli_bin,
+ registry_path=activation_registry,
agent_id=str(selected_agent_id) if selected_agent_id else None,
available_capabilities=normalized_available_capabilities,
**scheduler_command_binding_for_agent_type(canonical_agent_type),
diff --git a/loopx/agy_goal_mode/__init__.py b/loopx/agy_goal_mode/__init__.py
index bb2c73536d..da60243cca 100644
--- a/loopx/agy_goal_mode/__init__.py
+++ b/loopx/agy_goal_mode/__init__.py
@@ -14,6 +14,8 @@
from pathlib import Path
from typing import Any
+from ..command_invocation import loopx_cli_argv
+
AGY_INSTALL_SURFACE = "agy"
ANTIGRAVITY_APP_INSTALL_SURFACE = "antigravity-app"
ANTIGRAVITY_APP_AGENT_TYPE = "antigravity-app"
@@ -111,38 +113,21 @@ def build_antigravity_sidecar_template(
goal_id: str = "",
agent_id: str | None = None,
runtime_root: str | None = None,
+ registry: str | None = None,
cli_bin: str = "loopx",
interval_seconds: int = 180,
) -> dict[str, Any]:
"""Build a public-safe Antigravity 2.0 ``sidecar.json`` periodic heartbeat template."""
- quota_args = [cli_bin, "--format", "json"]
+ cli_args = [*loopx_cli_argv(cli_bin), "--format", "json"]
if runtime_root:
- quota_args.extend(["--runtime-root", runtime_root])
- quota_args.extend(
- [
- "quota",
- "should-run",
- "--goal-id",
- goal_id,
- "--runtime-profile",
- "antigravity_app",
- ]
- )
+ cli_args.extend(["--runtime-root", runtime_root])
+ if registry:
+ cli_args.extend(["--registry", registry])
+ profile_args = ["--goal-id", goal_id, "--runtime-profile", "antigravity_app"]
+ quota_args = [*cli_args, "quota", "should-run", *profile_args]
if agent_id:
quota_args.extend(["--agent-id", agent_id])
- heartbeat_args = [cli_bin, "--format", "json"]
- if runtime_root:
- heartbeat_args.extend(["--runtime-root", runtime_root])
- heartbeat_args.extend(
- [
- "heartbeat-prompt",
- "--thin",
- "--goal-id",
- goal_id,
- "--runtime-profile",
- "antigravity_app",
- ]
- )
+ heartbeat_args = [*cli_args, "heartbeat-prompt", "--thin", *profile_args]
if agent_id:
heartbeat_args.extend(["--agent-id", agent_id])
bootstrap_prompt = (
@@ -364,6 +349,7 @@ def antigravity_app_activation_extras(
goal_id: str = "",
agent_id: str | None = None,
runtime_root: str | None = None,
+ registry: str | None = None,
cli_bin: str = "loopx",
) -> dict[str, Any]:
"""Keyword overrides for ``antigravity_app_activation``'s facade call."""
@@ -371,6 +357,7 @@ def antigravity_app_activation_extras(
goal_id=goal_id,
agent_id=agent_id,
runtime_root=runtime_root,
+ registry=registry,
cli_bin=cli_bin,
)
hooks_template = build_antigravity_hooks_template(
@@ -378,6 +365,7 @@ def antigravity_app_activation_extras(
goal_id=goal_id,
agent_id=agent_id,
runtime_root=runtime_root,
+ registry=registry,
resolve_local_executable=False,
)
wake_prompt = sidecar_template["sidecar"]["loopx-heartbeat"]["bootstrap_prompt"]
diff --git a/loopx/agy_goal_mode/host_runtime.py b/loopx/agy_goal_mode/host_runtime.py
index 6cd4f5b265..96e207b865 100644
--- a/loopx/agy_goal_mode/host_runtime.py
+++ b/loopx/agy_goal_mode/host_runtime.py
@@ -17,6 +17,7 @@
import time
from typing import Any
+from ..command_invocation import loopx_cli_argv
from ..entrypoint import pin_utf8_stdio
from ..file_lock import exclusive_file_lock
@@ -41,7 +42,7 @@ def retryable_native_error(error: Any) -> bool:
def _core_args(args: argparse.Namespace, command: list[str]) -> list[str]:
- result = [args.cli_bin, "--format", "json"]
+ result = [*loopx_cli_argv(args.cli_bin), "--format", "json"]
for flag, value in (
("--registry", args.registry),
("--runtime-root", args.runtime_root),
diff --git a/loopx/attached_session.py b/loopx/attached_session.py
index 6ba0bdeeff..7c0f7ba627 100644
--- a/loopx/attached_session.py
+++ b/loopx/attached_session.py
@@ -767,7 +767,10 @@ def _enqueue_attached_agent_turn_under_grant(
def enqueue_attached_agent_turn(*, store: ChatSessionStore, registry_path: Path | None,
session_id: str, client_turn_id: str, message: str, origin: str,
- external_agent_target: dict[str, Any] | None = None) -> tuple[dict[str, Any], bool]:
+ external_agent_target: dict[str, Any] | None = None,
+ attachments: object = None) -> tuple[dict[str, Any], bool]:
+ if attachments:
+ raise ValueError("attached host session queue does not yet accept attachments")
kwargs = dict(store=store, registry_path=registry_path, session_id=session_id,
client_turn_id=client_turn_id, message=message, origin=origin,
external_agent_target=external_agent_target)
diff --git a/loopx/bootstrap_command_pack.py b/loopx/bootstrap_command_pack.py
index 1c92b1409a..8451873ba5 100644
--- a/loopx/bootstrap_command_pack.py
+++ b/loopx/bootstrap_command_pack.py
@@ -846,6 +846,7 @@ def build_loopx_bootstrap_command_pack(
registry_path=registry_path,
runtime_root_arg=runtime_root_arg,
)
+ activation_registry = str(registry_path) if agent_type == "antigravity-app" else None
thread_binding = resolve_thread_agent_binding(
registry_goal,
host_surface=host_surface,
@@ -890,6 +891,7 @@ def build_loopx_bootstrap_command_pack(
goal_id=resolved_goal_id,
cli_bin=cli_bin,
runtime_root=command_runtime_root,
+ registry=activation_registry,
identity_runtime_root=identity_runtime_root,
agent_id=effective_agent_id,
registered_agents=registered_agents,
@@ -923,6 +925,7 @@ def build_loopx_bootstrap_command_pack(
resolved_goal_id,
cli_bin=cli_bin,
runtime_root=command_runtime_root,
+ registry_path=activation_registry,
agent_id=str(selected_agent_id) if selected_agent_id else None,
available_capabilities=available_capabilities,
begin_turn=guided_start_begins_turn,
diff --git a/loopx/canary/module_metric_baseline.json b/loopx/canary/module_metric_baseline.json
index cc89abf093..c42a77b450 100644
--- a/loopx/canary/module_metric_baseline.json
+++ b/loopx/canary/module_metric_baseline.json
@@ -39,7 +39,7 @@
"loopx/chat_runtime.py": {
"any_count": 38,
"dict_any_count": 0,
- "lines": 2503
+ "lines": 2524
},
"loopx/chat_server.py": {
"any_count": 25,
@@ -84,7 +84,7 @@
"lines": 2760
},
"loopx/extensions/lark/private_conversations.py": {
- "lines": 2191
+ "lines": 2583
},
"loopx/heartbeat_prompt.py": {
"any_count": 17,
diff --git a/loopx/capabilities/configuration_ui.py b/loopx/capabilities/configuration_ui.py
index 9be1689c3d..e991c2d0e9 100644
--- a/loopx/capabilities/configuration_ui.py
+++ b/loopx/capabilities/configuration_ui.py
@@ -83,8 +83,15 @@ def capability_configuration_editor(
# The steward namespace owns its closed vocabulary, so the editor offers the
# same values instead of restating them: a form cannot submit an executor or
# effort the owning namespace would reject.
- steward_endpoints, steward_efforts = _steward_executor_editor_options()
- from .native_chat.machine_defaults import codex_profile_options
+ steward_endpoints, steward_efforts = (
+ _steward_executor_editor_options()
+ if capability_id in ("codex_chat", "steward_executor") else ([], [])
+ )
+ codex_profiles: list[str] = []
+ if capability_id == "codex_chat":
+ from .native_chat.machine_defaults import codex_profile_options
+
+ codex_profiles = codex_profile_options()
definitions: dict[str, dict[str, Any]] = {
"context_delivery": {
"supported_scopes": ["machine"], "writable_scopes": ["machine"],
@@ -100,7 +107,7 @@ def capability_configuration_editor(
"supported_scopes": ["machine"], "writable_scopes": ["machine"],
"fields": [
_field("codex_profile", "Codex configuration profile", "select", nullable=True,
- options=codex_profile_options(), description="Existing native account/configuration home; credentials are not copied."),
+ options=codex_profiles, description="Existing native account/configuration home; credentials are not copied."),
_field("model", "Conversation model", "text", nullable=True,
description="For example gpt-6.1-sol. Blank inherits this Codex profile's default."),
_field("reasoning_effort", "Reasoning effort", "select", nullable=True,
diff --git a/loopx/capabilities/content_ops/reference.py b/loopx/capabilities/content_ops/reference.py
index 3a25ebcef2..d9650bb331 100644
--- a/loopx/capabilities/content_ops/reference.py
+++ b/loopx/capabilities/content_ops/reference.py
@@ -9,7 +9,6 @@
from typing import Any
from ...control_plane.effect_runtime import EffectRuntimeRejected, EffectRuntimeStartupError, effect_runtime_result
-from ..material_lifecycle.inventory import build_material_store_inventory
def content_reference_operation(operation: str, request: Mapping[str, Any]) -> dict[str, Any]:
@@ -28,6 +27,8 @@ def content_reference_operation(operation: str, request: Mapping[str, Any]) -> d
def inspect_reference_materials(*, library_path: Path, goal_id: str, store_id: str, observed_at: str) -> dict[str, Any]:
"""Inventory the original bytes; never infer lifecycle state or claim a backup."""
+ from ..material_lifecycle.inventory import build_material_store_inventory
+
source = library_path.read_bytes()
library = json.loads(source)
projection = content_reference_operation("search", {"library": library})
diff --git a/loopx/capabilities/issue_fix/pr_wait_hook.py b/loopx/capabilities/issue_fix/pr_wait_hook.py
index 74689d73dc..5eb7569006 100644
--- a/loopx/capabilities/issue_fix/pr_wait_hook.py
+++ b/loopx/capabilities/issue_fix/pr_wait_hook.py
@@ -13,7 +13,6 @@
TURN_START_HOOK_RESULT_SCHEMA_VERSION,
TurnStartHookRegistration,
)
-from .pr_wait_reconcile import reconcile_pr_wait_dependencies
PR_WAIT_OBSERVATION_HOOK_ID = "issue_fix.pr_wait_observation"
@@ -91,6 +90,9 @@ def produce() -> dict[str, Any]:
"external_read_count": 0, "state_written": False,
"summary_lines": ["Observation permission source is unavailable; provider not contacted."]}}
try:
+ # Optional provider loading follows the invocation-time permission recheck.
+ from .pr_wait_reconcile import reconcile_pr_wait_dependencies
+
readback = reconcile_pr_wait_dependencies(registry_path=registry_path,
runtime_root_arg=str(runtime_root), goal_id=goal_id, agent_id=agent_id,
fetch_timeout_seconds=fetch_timeout_seconds)
diff --git a/loopx/capabilities/issue_fix/reviewer_recommendation.py b/loopx/capabilities/issue_fix/reviewer_recommendation.py
index 1db50658ac..df2e7cf395 100644
--- a/loopx/capabilities/issue_fix/reviewer_recommendation.py
+++ b/loopx/capabilities/issue_fix/reviewer_recommendation.py
@@ -13,6 +13,10 @@
from ...control_plane.runtime.public_safety import public_safe_compact_text
from ...public_safe_text import COMPACT_LABEL_PATTERN as REVIEWER_SOURCE_ID_PATTERN
+from .repository_context import (
+ FRESHNESS_STATES as REVIEWER_SOURCE_FRESHNESS_STATES,
+ TRUST_LEVELS as REVIEWER_SOURCE_TRUST_LEVELS,
+)
ISSUE_FIX_REVIEWER_RECOMMENDATION_SCHEMA_VERSION = (
@@ -37,8 +41,6 @@
re.IGNORECASE,
)
REVIEWER_SOURCE_KINDS = {"maintainer_map"}
-REVIEWER_SOURCE_TRUST_LEVELS = {"authoritative", "verified", "advisory"}
-REVIEWER_SOURCE_FRESHNESS_STATES = {"current", "stale", "unknown"}
REVIEWER_SOURCE_MATCH_KINDS = {
"path_prefix",
"path_glob",
diff --git a/loopx/capabilities/native_chat/codex_adapter.py b/loopx/capabilities/native_chat/codex_adapter.py
index ca62c0f7b1..7fd5dd5d58 100644
--- a/loopx/capabilities/native_chat/codex_adapter.py
+++ b/loopx/capabilities/native_chat/codex_adapter.py
@@ -5,7 +5,9 @@
"""
from __future__ import annotations
-from dataclasses import dataclass, replace
+from contextlib import contextmanager
+from copy import deepcopy
+from dataclasses import dataclass, field, replace
import hashlib
import json
import logging
@@ -737,6 +739,7 @@ def turns() -> list[dict[str, Any]]:
class CodexAppServerAdapter:
session: CodexChatAgentSession
goal_driver: CodexGoalDriver | None = None
+ _managed_read_turn_guard: Callable[[], None] | None = field(default=None, repr=False)
def completed_work_observation(self, *, thread_ids: list[str], turn_id: str,
known_turn_ids: set[str] | None = None) -> dict[str, Any] | None:
@@ -1004,7 +1007,32 @@ def capabilities(self) -> dict[str, Any]:
}
def start_turn(self, message: str, event_sink: Callable[[str, dict[str, Any]], None]) -> dict[str, Any]:
- return self.session.send(message, on_event=event_sink)
+ return self.session.send(message, on_event=event_sink, **self._ordinary_turn_arguments())
+
+ def _ordinary_turn_arguments(self):
+ check = self._managed_read_turn_guard
+ if check is None:
+ return {}
+ previous_guard = self.session._launch_guard
+ @contextmanager
+ def read_turn():
+ check()
+ if previous_guard is None:
+ yield
+ else:
+ with previous_guard():
+ check()
+ yield
+ # Native sandboxPolicy is sticky across subsequent Turns. Reassert the
+ # current managed Chat read boundary without changing Session defaults.
+ return {"sandbox_policy": {"type": "readOnly", "networkAccess": False},
+ "turn_start_guard": read_turn}
+
+ def start_native_task_turn(self, message: str,
+ event_sink: Callable[[str, dict[str, Any]], None], policy: dict[str, object]) -> dict[str, Any]:
+ """Transport one Core-admitted Task through its existing launch fence."""
+ return self.session.send(message, on_event=event_sink, _native_task=True,
+ sandbox_policy=deepcopy(policy), turn_start_guard=self.session._launch_guard)
def steer_turn(self, message: str, expected_turn_id: str, client_message_id: str | None = None) -> str:
return self.session.steer(message, expected_turn_id=expected_turn_id,
@@ -1021,7 +1049,8 @@ def start_turn_with_attachments(
event_sink: Callable[[str, dict[str, Any]], None],
attachments: list[dict[str, Any]],
) -> dict[str, Any]:
- return self.session.send(message, attachments=attachments, on_event=event_sink)
+ return self.session.send(message, attachments=attachments, on_event=event_sink,
+ **self._ordinary_turn_arguments())
def interrupt_turn(self, turn_id: str | None = None) -> None:
driver = self.goal_driver
@@ -1053,6 +1082,60 @@ def start_attachment_turn(adapter: Any, session: dict[str, Any], turn: dict[str,
return adapter.start_turn_with_attachments(message, event_sink, attachments)
+def prepare_native_turn_text(session: CodexChatAgentSession, user_message: str, *,
+ output_schema, project_collaboration: bool, native_task: bool,
+ sandbox_policy, turn_start_guard):
+ """Preserve Chat input validation; adapt only the fenced Task invocation.
+
+ The Core caller owns Task/lease/permission admission. This transport cannot
+ manufacture that grant from a policy dictionary or a model/browser payload.
+ """
+ text = str(user_message or "")
+ if not text.strip():
+ raise ValueError("user message is required")
+ if native_task:
+ workspace_policy = {"type": "workspaceWrite", "writableRoots": [str(session.work_dir.resolve())],
+ "networkAccess": False, "excludeTmpdirEnvVar": True, "excludeSlashTmp": True}
+ if (turn_start_guard is None or session._launch_guard is None
+ or turn_start_guard is not session._launch_guard
+ or sandbox_policy not in ({"type": "readOnly", "networkAccess": False}, workspace_policy)
+ or session.execution_mode or session.read_only_review or session.project_context is not None
+ or session.permissions_profile is not None or session.sandbox != "read-only"):
+ raise ValueError("native Task transport requires the original read-only Session, bound workspace policy and current host launch guard")
+ elif sandbox_policy is not None and sandbox_policy != {"type": "readOnly", "networkAccess": False}:
+ raise ValueError("an ordinary Chat Turn cannot install a native execution policy")
+ if sandbox_policy is not None and session.permissions_profile is not None:
+ raise ValueError("a native sandbox policy cannot replace an existing named permissions profile")
+ if session.read_only_review and not session.metadata_review and not session.answer_review:
+ text = (
+ "Review the existing results of the interrupted original request below. "
+ "Its earlier execution may have already changed files or external systems. "
+ "Use the original native history and current local artifacts to verify what actually happened. "
+ "This Turn is read-only: do not re-execute the business request, write, send, delegate or start background work. "
+ "Return a clear answer to the original user, distinguishing verified results from remaining gaps. "
+ "If verification is incomplete, report the concrete limitation; do not claim completion. "
+ "Original request: " + text
+ )
+ if project_collaboration:
+ if (session.execution_mode or session.read_only_review or output_schema is not None
+ or not session.project_context or session.project_context.get("audience") != "bound_owner"):
+ raise ValueError("original project collaboration requires its existing bound project session")
+ from ...control_plane.effect_runtime import effect_runtime_result
+ output_schema = effect_runtime_result("collaboration.project.response_schema", {})
+ if output_schema is not None and not session.execution_mode and not project_collaboration and not native_task:
+ raise ValueError("structured Turn output requires an execution session")
+ return text, output_schema
+
+
+def native_runtime_context(controller: ChatRuntimeController, *, goal_id: str | None,
+ objective: str, manager_profile: Mapping[str, object] | None,
+ project_coordination: bool, execution_mode: bool) -> dict[str, str]:
+ """Compose the shared native adapter inputs in their construction owner."""
+ return {"objective": controller._session_objective(goal_id=goal_id, objective=objective,
+ project_coordination=project_coordination and not execution_mode),
+ "runtime_profile": str(manager_profile["runtime_profile"]) if manager_profile else "restricted"}
+
+
def start_runtime_adapter(controller: ChatRuntimeController, *, work_dir: Path, goal_id: str | None,
native_context: Mapping[str, str], manager_profile: Mapping[str, object] | None,
model_config: Mapping[str, str | None], history: list[dict[str, str]] | None,
@@ -1061,13 +1144,16 @@ def start_runtime_adapter(controller: ChatRuntimeController, *, work_dir: Path,
read_only_review: bool, metadata_review_context: dict[str, object] | None,
answer_review_context: dict[str, object] | None,
history_check: Callable[[], None] | None = None,
- working_continuation: NativeWorkingContinuation | None = None) -> CodexAppServerAdapter:
+ working_continuation: NativeWorkingContinuation | None = None,
+ check_preparation: bool = False,
+ managed_read_default: bool = False) -> CodexAppServerAdapter:
"""Build the native adapter from already-authorized runtime arguments."""
from ...chat_manager import MANAGER_AGENT_GOAL_ID
from ..manager_context.inspection import READ_TOOL, CONTEXT_READ_TOOL
from ...chat_loopx_mode import TOOL as COLLABORATION_TOOL
+ from ...chat.coordination import check_preparation_tool
review_context = metadata_review_context or answer_review_context
- return CodexAppServerAdapter.start(
+ adapter = CodexAppServerAdapter.start(
codex_bin=controller.codex_bin,
account_runtime_root=controller.coordination_runtime_root,
account_consumer="steward" if manager_profile is not None or review_context is not None else "worker",
@@ -1101,8 +1187,13 @@ def start_runtime_adapter(controller: ChatRuntimeController, *, work_dir: Path,
service_tier=model_config.get("service_tier"),
dynamic_tools=[READ_TOOL] if answer_review_context else (
[READ_TOOL] if goal_id == MANAGER_AGENT_GOAL_ID
- else [CONTEXT_READ_TOOL, *([COLLABORATION_TOOL] if loopx_tools else [])]
+ else [CONTEXT_READ_TOOL, *([COLLABORATION_TOOL] if loopx_tools else
+ [check_preparation_tool()] if check_preparation and resume_thread_id is None
+ and working_continuation is None else [])]
) if not execution_mode and not read_only_review and (
goal_id == MANAGER_AGENT_GOAL_ID or project_coordination
) else None,
)
+ if managed_read_default:
+ adapter.session._managed_goal_read_default = True
+ return adapter
diff --git a/loopx/capabilities/native_chat/creation_queue.py b/loopx/capabilities/native_chat/creation_queue.py
new file mode 100644
index 0000000000..ff7753938d
--- /dev/null
+++ b/loopx/capabilities/native_chat/creation_queue.py
@@ -0,0 +1,131 @@
+"""IO for the original creation request; the existing Session queue executes it."""
+from pathlib import Path
+from typing import Any
+import logging
+
+def creation_request_facts(controller: Any, session_id: str, *, proposal_id: str | None = None,
+ basis: dict[str, Any] | None = None) -> dict[str, Any] | None:
+ actions = getattr(controller, "original_task_action_store", None)
+ if actions is None:
+ return None
+ session = controller.store.load_session(session_id)
+ if not session or not session.get("managed_agent_id"):
+ return None
+ candidates = [actions.load(proposal_id)] if proposal_id is not None else actions.list()
+ candidates = [row for row in candidates if isinstance(row, dict)
+ and row.get("action_kind") == "goal.create"
+ and (row.get("normalized_parameters") or {}).get("start_execution") is True
+ and (row.get("normalized_parameters") or {}).get("goal_id") == session.get("goal_id")
+ and (((row.get("checkpoint") or {}).get("steps") or {}).get("first_session_opened") or {}).get(
+ "session_id") == session_id]
+ if not candidates:
+ return None
+ # Candidate filtering is IO narrowing, not authorization. Only explicit
+ # same-Session source candidates load native/canonical owners below.
+ from .steward_managed_mode import StewardManagedMode
+ from ...control_plane.effect_runtime import effect_runtime_result, EffectRuntimeRejected
+ from ...control_plane.goals.acceptance import inspect_goal_acceptance
+
+ goal = StewardManagedMode(controller, None)._goal(session.get("goal_id"))
+ if goal is None:
+ return None
+ # This registered creation identity is the action owner's proposal key,
+ # not a label or a parsed operation prefix.
+ source = proposal_id if proposal_id is not None else goal.get("creation_operation_id")
+ if not isinstance(source, str) or not source:
+ return None
+ proposal = next((row for row in candidates if row["proposal_id"] == source), None)
+ if (proposal is None or proposal.get("action_kind") != "goal.create"
+ or (proposal.get("normalized_parameters") or {}).get("start_execution") is not True):
+ return None
+ if basis is None:
+ basis = inspect_goal_acceptance(registry_path=controller.registry_path,
+ runtime_root=str(controller.coordination_runtime_root), goal_id=goal["goal_id"],
+ agent_id=session["managed_agent_id"], validation_workspace_path=Path(goal["workspace_path"]))
+ try:
+ selected = effect_runtime_result("goal.task.creation_selection", {"basis": basis})
+ except EffectRuntimeRejected as exc:
+ raise ValueError(str(exc)) from exc
+ return {"proposal": proposal, "session": session, "goal": goal, "current_original_task": selected}
+
+
+def project_creation_request(facts: dict[str, Any] | None) -> dict[str, Any] | None:
+ if facts is None:
+ return None
+ from ...control_plane.effect_runtime import effect_runtime_result, EffectRuntimeRejected
+ try:
+ request = effect_runtime_result("goal.creation.execution_request", facts)
+ except EffectRuntimeRejected as exc:
+ raise ValueError(str(exc)) from exc
+ if request is not None and not isinstance(request, dict):
+ raise ValueError("original creation request projection returned an invalid result")
+ return request
+
+
+def consume_creation_request(controller: Any, session_id: str) -> bool:
+ """Return whether this creation needs attention, never replaying its Turn."""
+ facts = creation_request_facts(controller, session_id)
+ if facts is None or facts["proposal"].get("status") not in ("applying", "applied"):
+ return False
+ if not isinstance(((facts["proposal"].get("checkpoint") or {}).get("steps") or {}).get(
+ "original_task_requested"), dict):
+ return False
+ try:
+ request = project_creation_request(facts)
+ if request is None:
+ return True # The same creation has not committed its request yet.
+ from .steward_managed_mode import ManagedSessionTurn
+ return ManagedSessionTurn(controller).consume_creation_request(request)
+ except (ValueError, RuntimeError) as exc:
+ # Missing/changed checks or source authority leave the saved request
+ # open. A content-free action/Turn signal or restart rechecks it.
+ logging.getLogger(__name__).debug("Original creation request retained: %s", exc)
+ return True
+
+
+def queued_turn_unstarted(turn: dict[str, Any]) -> bool:
+ """Unknown native publication facts cannot authorize a queue dispatch."""
+ return (turn.get("status") == "queued" and "_acceptance" not in turn
+ and "started_at" in turn and turn["started_at"] is None
+ and "upstream_turn_id" in turn and turn["upstream_turn_id"] is None
+ and type(turn.get("delta_count")) is int and turn["delta_count"] == 0)
+
+
+def resume_creation_requests(controller: Any) -> None:
+ """Discover only saved requests at service setup; hints grant no execution."""
+ actions = getattr(controller, "original_task_action_store", None)
+ if actions is None or controller.closed.is_set():
+ return
+ for proposal in actions.list():
+ if (proposal.get("action_kind") != "goal.create"
+ or (proposal.get("normalized_parameters") or {}).get("start_execution") is not True):
+ continue
+ request = (proposal.get("receipt") or {}).get("original_task_request")
+ if not isinstance(request, dict):
+ continue
+ session_id = request.get("session_id")
+ if not isinstance(session_id, str):
+ continue
+ try:
+ facts = creation_request_facts(controller, session_id, proposal_id=proposal["proposal_id"])
+ qualified = project_creation_request(facts)
+ if qualified is None or qualified != request:
+ continue
+ existing = controller.store.turn_for_client(session_id, "managed." + qualified["operation_id"])
+ if existing is not None:
+ if not queued_turn_unstarted(existing):
+ continue
+ from .steward_managed_mode import ManagedSessionTurn
+ expected = ManagedSessionTurn(controller)._request(session_id, qualified["agent_id"],
+ qualified["operation_id"], qualified["todo_id"])
+ expected["creation_request"] = qualified
+ if existing.get("loopx_request") != expected:
+ continue
+ session = facts["session"]
+ except (KeyError, OSError, ValueError, RuntimeError):
+ # A malformed candidate cannot wake another Session's queue. Its
+ # current-source refusal remains available through the Task GET.
+ continue
+ controller.resume_session_queue(session_id=session_id,
+ work_dir=Path(session["managed_workspace_path"]),
+ objective="Continue the original Goal's requested Task.")
diff --git a/loopx/capabilities/native_chat/external_conversations.py b/loopx/capabilities/native_chat/external_conversations.py
index f3c547215c..081e83bb6c 100644
--- a/loopx/capabilities/native_chat/external_conversations.py
+++ b/loopx/capabilities/native_chat/external_conversations.py
@@ -27,7 +27,7 @@
from ...file_lock import exclusive_file_lock, try_exclusive_file_lock
from . import result_review_evidence
from .codex_context import checked_session_home
-from .file_discovery import FileChatDiscovery, later_equivalent_source_input, source_control_fields
+from .file_discovery import FileChatDiscovery, external_source_route_path, later_equivalent_source_input, source_control_fields
# Request journals are named by their 24-hex request_ref; lock metadata and
# other siblings in this directory are not requests.
@@ -548,7 +548,7 @@ def source_requests() -> list[dict[str, Any]]:
# route already owns the fence, leave this request unchanged for
# the existing worker's next fresh visit, rather than admitting
# from an observation that waited behind that route.
- with try_exclusive_file_lock(self.root / "source-fences" / f"{row['binding_id']}.{row['source']['source_ref']}.json", operation="route_external_chat_request") as acquired:
+ with try_exclusive_file_lock(external_source_route_path(self.root, row["binding_id"], row["source"]["source_ref"]), operation="route_external_chat_request") as acquired:
if acquired is None:
return row
current = self.bindings.read()
@@ -740,7 +740,7 @@ def _recover_native_result(self, path: Path, row: dict[str, Any], session: dict[
proof = recovered.get("native_observation")
selected = self.bindings.resolve(binding_id=row["binding_id"], **row["source"], include_observation=True)
source = source_observation(selected)
- with try_exclusive_file_lock(self.root / "source-fences" / f"{row['binding_id']}.{row['source']['source_ref']}.json",
+ with try_exclusive_file_lock(external_source_route_path(self.root, row["binding_id"], row["source"]["source_ref"]),
operation="route_external_chat_request") as acquired:
if acquired is None:
return row
@@ -826,8 +826,7 @@ def admit_history_answer(self, *, binding_id: str, source: dict[str, Any], reque
raise ValueError("invalid historical answer source")
path = self.root / f"{request_ref}.json"
with exclusive_file_lock(path, operation="admit_external_chat_request"):
- with exclusive_file_lock(self.root / "source-fences" / f"{binding_id}.{source['source_ref']}.json",
- operation="route_external_chat_request"):
+ with exclusive_file_lock(external_source_route_path(self.root, binding_id, source["source_ref"]), operation="route_external_chat_request"):
selected = self.bindings.resolve(binding_id=binding_id, **source, include_observation=True)
if source_preflight(deepcopy(selected)) is not None:
raise ValueError("source preflight must verify or raise")
@@ -1391,7 +1390,8 @@ def admit(self, *, binding_id: str, source: dict[str, Any], request_ref: str,
# A duplicate waits on its own request before taking the route fence;
# it must not hold up other messages while native cancellation runs.
with exclusive_file_lock(path, operation="admit_external_chat_request"):
- with exclusive_file_lock(self.root / "source-fences" / f"{binding_id}.{source['source_ref']}.json", operation="route_external_chat_request"):
+ with exclusive_file_lock(external_source_route_path(self.root, binding_id, source["source_ref"]),
+ operation="route_external_chat_request"):
# Resolve and verify the source inside both fences before
# creating or replaying any canonical request.
selected = resolve_external_admission_source(self.bindings, binding_id, source, source_preflight)
diff --git a/loopx/capabilities/native_chat/file_discovery.py b/loopx/capabilities/native_chat/file_discovery.py
index 7fd1a5a27f..1454dde4b8 100644
--- a/loopx/capabilities/native_chat/file_discovery.py
+++ b/loopx/capabilities/native_chat/file_discovery.py
@@ -11,6 +11,11 @@
from typing import Any
+def external_source_route_path(root: Path, binding_id: str, source_ref: str) -> Path:
+ """Locate the original source's cross-process admission fence."""
+ return root / "source-fences" / f"{binding_id}.{source_ref}.json"
+
+
def _ordinary_request_text_digest(row: dict[str, Any]) -> str | None:
"""Match ordinary private text without retaining its body in discovery."""
message = row.get("message")
diff --git a/loopx/capabilities/native_chat/queue_api.py b/loopx/capabilities/native_chat/queue_api.py
index 17be78f6d3..805fab86cb 100644
--- a/loopx/capabilities/native_chat/queue_api.py
+++ b/loopx/capabilities/native_chat/queue_api.py
@@ -1,11 +1,42 @@
-"""HTTP projection and explicit steering of the existing persisted queue."""
+"""HTTP transport for original Session Tasks and the existing persisted queue."""
from typing import Any
+from urllib.parse import parse_qs, urlparse
from ...chat_store import CHAT_STORE_SCHEMA_VERSION
class ChatQueueRequestMixin:
+ def _managed_native_turn(self, session_id: str, *, execute: bool = False) -> None:
+ from .steward_managed_mode import ManagedSessionTurn
+ service = ManagedSessionTurn(self.server.runtime_controller)
+ try:
+ if execute:
+ body = self._read_json()
+ if set(body) - {"agent_id", "operation_id", "todo_id"}:
+ raise ValueError("unsupported managed Turn arguments")
+ turn, created = service.submit(session_id, **body)
+ return self._send_json({"ok": True, "session_id": session_id, "turn": turn, "created": created}, status=202)
+ query = parse_qs(urlparse(self.path).query, keep_blank_values=True)
+ sources = query.get("calendar_task_sources", ["false"])
+ if sources not in (["true"], ["false"]):
+ raise ValueError("calendar_task_sources must be one true or false value")
+ include_sources = sources == ["true"]
+ original_task = query.get("original_task", ["false"])
+ if original_task not in (["true"], ["false"]):
+ raise ValueError("original_task must be one true or false value")
+ include_original_task = original_task == ["true"]
+ if (include_sources or include_original_task) and (set(query) - {"calendar_task_sources", "original_task", "agent_id", "operation_id"}
+ or any(len(values) != 1 for values in query.values())):
+ raise ValueError("select one original managed Session and Agent for Task sources")
+ self._send_json(service.inspect(session_id, agent_id=query.get("agent_id", [None])[0],
+ operation_id=query.get("operation_id", [None])[0], include_calendar_task_sources=include_sources,
+ include_original_task=include_original_task))
+ except KeyError:
+ self._send_error("chat session was not found", status=404)
+ except (TypeError, ValueError, RuntimeError) as exc:
+ self._send_error(str(exc), status=409, error_code="managed_turn_unavailable")
+
def _dispatch_chat_queue(self, path: str, *, apply: bool) -> bool:
parts = path.strip("/").split("/")
if parts[:3] != ["api", "chat", "sessions"] or len(parts) < 5 or parts[4] != "queue":
diff --git a/loopx/capabilities/native_chat/session_queue.py b/loopx/capabilities/native_chat/session_queue.py
new file mode 100644
index 0000000000..217e882154
--- /dev/null
+++ b/loopx/capabilities/native_chat/session_queue.py
@@ -0,0 +1,170 @@
+"""The existing same-Session FIFO, preparation and recovery consumer."""
+from pathlib import Path
+from typing import Any
+import threading
+
+from ...chat_store import CHAT_SESSION_MODE_ATTACHED, TERMINAL_TURN_STATES
+
+
+def drain_session_queue(controller: Any, *, session_id: str, work_dir: Path, objective: str) -> None:
+ wakeup = threading.Event()
+ with controller.turn_release_wakeup(wakeup):
+ _drain_session_queue_events(controller, session_id=session_id, work_dir=work_dir,
+ objective=objective, wakeup=wakeup)
+
+
+def _drain_session_queue_events(controller: Any, *, session_id: str, work_dir: Path,
+ objective: str, wakeup: threading.Event) -> None:
+ try:
+ while not controller.closed.is_set():
+ # Register and clear before reading durable state. A signal
+ # during this read remains visible when a pending request waits.
+ wakeup.clear()
+ session = controller.store.load_session(session_id)
+ if (
+ controller.closed.is_set()
+ or session is None
+ or session.get("status") == "closed"
+ or session.get("session_mode") == CHAT_SESSION_MODE_ATTACHED
+ ):
+ return
+ turn = None
+ active_turn_id = str(session.get("active_turn_id") or "")
+ if active_turn_id:
+ try:
+ controller.recover_exited_turn(session_id, active_turn_id)
+ except OSError:
+ # Storage can still be unavailable. Retry only the
+ # non-executing lifecycle repair, never the old input.
+ controller.closed.wait(0.25)
+ continue
+ active = controller.store.load_turn(session_id, active_turn_id)
+ if active and isinstance((active.get("loopx_request") or {}).get("creation_request"), dict):
+ if active.get("status") == "queued":
+ from .creation_queue import consume_creation_request
+ try:
+ pending = consume_creation_request(controller, session_id)
+ except (OSError, ValueError, RuntimeError):
+ pending = True
+ if not pending:
+ # The native worker can terminalize after this
+ # queue cached its queued state. Reconcile the
+ # current FIFO instead of retiring with ordinary
+ # messages stranded behind the released operation.
+ current = controller.store.load_session(session_id)
+ latest = controller.store.load_turn(session_id, active_turn_id)
+ if current is None:
+ return
+ if (current.get("active_turn_id") != active_turn_id
+ or latest and latest.get("status") in TERMINAL_TURN_STATES):
+ continue
+ # A still-active unknown operation grants no
+ # dispatch. Keep its worker until a state signal.
+ wakeup.wait()
+ continue
+ if active.get("status") not in TERMINAL_TURN_STATES:
+ # An admitted or unknown native operation retains
+ # its original worker/result; never restore it as an
+ # ordinary queued message or start another model.
+ wakeup.wait()
+ continue
+ if (active and active.get("status") == "queued" and "_acceptance" not in active
+ and active.get("started_at") is None and active.get("upstream_turn_id") is None
+ and active.get("delta_count") == 0):
+ # A crash can leave the queue claim before native
+ # dispatch. Keep that exact claim and pass through the
+ # normal preparation and execution fences below.
+ turn = active
+ else:
+ with controller.lock:
+ attached = controller.adapters.get(session_id)
+ if attached is None or not attached.healthcheck():
+ try:
+ controller._ensure_adapter(
+ session,
+ work_dir=work_dir,
+ objective=objective,
+ )
+ except Exception as exc:
+ controller._fail_queue_preparation(session_id, active_turn_id, exc)
+ else:
+ continue
+ active = controller.store.load_turn(session_id, active_turn_id)
+ if active and active.get("status") not in TERMINAL_TURN_STATES:
+ controller.closed.wait(0.05)
+ continue
+ controller.store.release_active_turn(session_id, active_turn_id)
+ refreshed = controller.store.load_session(session_id)
+ if refreshed is None:
+ return
+ with controller.lock:
+ controller.session_queue_wakeups.discard(session_id)
+ if turn is None:
+ turn = controller.store.claim_next_queued_turn(session_id)
+ if turn is None:
+ from .creation_queue import consume_creation_request
+ try:
+ pending = consume_creation_request(controller, session_id)
+ except (OSError, ValueError, RuntimeError):
+ pending = True
+ if pending:
+ # The request lives in its creation receipt. Existing
+ # action/Turn signals and startup discovery re-read it;
+ # pending checks do not create a failed native Turn.
+ wakeup.wait()
+ continue
+ with controller.lock:
+ if session_id in controller.session_queue_wakeups:
+ continue
+ worker = controller.session_queue_threads.get(session_id)
+ if worker is threading.current_thread():
+ controller.session_queue_workers.discard(session_id)
+ controller.session_queue_threads.pop(session_id, None)
+ return
+ turn_id = str(turn["turn_id"])
+ try:
+ if turn.get("origin") == "peer" or "collaboration_request" in turn:
+ from .conversation_bindings import OriginalConversationRequests
+ OriginalConversationRequests(controller).validate_turn(session_id, turn)
+ # An empty queue needs no provider preparation. Claim real
+ # work first, keeping the existing enqueue/retirement fence
+ # above and the Turn's execution/stop checks below.
+ current = controller.store.load_session(session_id)
+ if current is None or current.get("active_turn_id") != turn_id:
+ return
+ adapter = controller._ensure_adapter(
+ current,
+ work_dir=work_dir,
+ objective=objective,
+ )
+ if (controller.store.load_session(session_id) or {}).get("active_turn_id") != turn_id:
+ return
+ except Exception as exc:
+ # The claimed request still owns a terminal outcome when
+ # workspace preparation or adapter restoration fails.
+ controller._fail_queue_preparation(session_id, turn_id, exc)
+ continue
+ done_event = threading.Event()
+ with controller.lock:
+ controller.turn_done_events[(session_id, turn_id)] = done_event
+ try:
+ controller._run_turn(
+ session_id=session_id,
+ turn_id=turn_id,
+ message=str(turn.get("message") or ""),
+ attachments=turn.get("attachments") or [],
+ adapter=adapter,
+ done_event=done_event,
+ )
+ except OSError:
+ # _run_turn has released its execution fence/done event.
+ # A failed error checkpoint must not kill this queue worker;
+ # the next iteration reconciles the original durable state.
+ controller.closed.wait(0.25)
+ finally:
+ with controller.lock:
+ worker = controller.session_queue_threads.get(session_id)
+ if worker is threading.current_thread():
+ controller.session_queue_workers.discard(session_id)
+ controller.session_queue_threads.pop(session_id, None)
+ controller.session_queue_wakeups.discard(session_id)
diff --git a/loopx/capabilities/native_chat/steward_managed_mode.py b/loopx/capabilities/native_chat/steward_managed_mode.py
index 5eea668732..bc5cf51d86 100644
--- a/loopx/capabilities/native_chat/steward_managed_mode.py
+++ b/loopx/capabilities/native_chat/steward_managed_mode.py
@@ -292,11 +292,23 @@ def admit(self, request: dict[str, Any], *, turn: dict[str, Any] | None = None,
if request.get("work_kind") == "scheduled_task":
from .session_calendar_tasks import SessionCalendarTasks
report["scheduled_task"] = SessionCalendarTasks(self.controller).admission(request)
+ creation = {}
+ if "creation_request" in request:
+ from .creation_queue import creation_request_facts
+ retained = request["creation_request"]
+ if not isinstance(retained, dict) or not isinstance(retained.get("creation_operation_id"), str):
+ raise ValueError("the original creation request is unavailable")
+ facts = creation_request_facts(self.controller, actor["session_id"],
+ proposal_id=retained["creation_operation_id"])
+ if facts is None:
+ raise ValueError("the original creation request source is unavailable")
+ creation = {"creation_proposal": facts["proposal"],
+ "current_original_task": facts["current_original_task"]}
return ChatConversationBindings._core("collaboration.session.managed_turn.admit", {
"session": session, "request": request, "turn": turn,
"goal": StewardManagedMode(self.controller, None)._goal(actor["goal_id"]),
"goal_active": bool(goal and goal.get("status") == "active" and not goal_is_stopped(goal)),
- "monitor_todo": monitor, **report,
+ "monitor_todo": monitor, **report, **creation,
**({"report_settlement_recovery": settlement_recovery} if settlement_recovery is not None else {}),
**({"report_selection_recovery": selection_recovery} if selection_recovery is not None else {}),
**StewardTurnHost(self.controller).report_quota_basis(request),
@@ -304,7 +316,8 @@ def admit(self, request: dict[str, Any], *, turn: dict[str, Any] | None = None,
"queued_turn_count": len(self.store.queued_turns(actor["session_id"]))})
def inspect(self, session_id: str, *, agent_id: str | None = None,
- operation_id: str | None = None, include_calendar_task_sources: bool = False) -> dict[str, Any]:
+ operation_id: str | None = None, include_calendar_task_sources: bool = False,
+ include_original_task: bool = False) -> dict[str, Any]:
session = self.store.load_session(session_id)
if not session:
raise KeyError("chat session was not found")
@@ -322,6 +335,10 @@ def inspect(self, session_id: str, *, agent_id: str | None = None,
result["available"] = True
except ValueError as exc:
result["reason"] = str(exc)
+ if include_original_task:
+ if agent_id is not None and agent_id != session.get("managed_agent_id"):
+ raise ValueError("select the original registered Agent for Task entry")
+ result["task"] = self._original_task_entry(session_id)
if session.get("managed_agent_id"):
from .steward_reports import ManagedSessionReports
from .session_calendar_tasks import SessionCalendarTasks
@@ -336,14 +353,67 @@ def inspect(self, session_id: str, *, agent_id: str | None = None,
result["calendar_task_sources"] = SessionCalendarTasks(self.controller).source_options(session_id)
return result
+ def creation_request(self, session_id: str) -> dict[str, Any] | None:
+ from .creation_queue import creation_request_facts, project_creation_request
+ return project_creation_request(creation_request_facts(self.controller, session_id))
+
+ def _original_task_entry(self, session_id: str) -> dict[str, Any]:
+ """Observe the existing creation/validation owners without any effects.
+
+ The returned plan does not claim a Task or authorize a model. Native
+ submit still rechecks current admission and every validation source.
+ """
+ from ...control_plane.effect_runtime import effect_runtime_result
+ from ...control_plane.goals.acceptance import inspect_goal_acceptance
+
+ session = self.store.load_session(session_id) or {}
+ request = self._request(session_id, session.get("managed_agent_id") or "", "original-task-preview")
+ actor = request["actor"]
+ unavailable: dict[str, Any] = {"todo_id": None, "state": "unbound", "source": None,
+ "canonical_done": False}
+ try:
+ creation = self.creation_request(session_id)
+ if creation is not None:
+ request = self._request(session_id, creation["agent_id"], creation["operation_id"], creation["todo_id"])
+ request["creation_request"] = creation
+ retained_turn = self.store.turn_for_client(session_id, "managed." + creation["operation_id"])
+ unavailable.update(creation_request=creation, creation_turn=None)
+ if retained_turn is not None and retained_turn.get("loopx_request") != request:
+ raise ValueError("the original creation operation belongs to another managed request")
+ unavailable["creation_turn"] = retained_turn
+ self.admit(request)
+ basis = inspect_goal_acceptance(registry_path=self.controller.registry_path,
+ runtime_root=str(self.controller.coordination_runtime_root), goal_id=actor["goal_id"],
+ agent_id=actor["agent_id"], validation_workspace_path=Path(actor["workspace_path"]))
+ selected = effect_runtime_result("goal.task.creation_selection", {"basis": basis})
+ if selected["todo_id"] is None:
+ return {**unavailable, "reason": selected["reason"]}
+ todo_id = selected["todo_id"]
+ unavailable["todo_id"] = todo_id
+ request["todo_id"] = todo_id
+ self.admit(request)
+ plan = self._validation_source(request, todo_id)["plan"]
+ # Return only the display observation; commands, pins and private
+ # declarations stay with their existing owner.
+ self.admit(request)
+ return {**{key: plan[key] for key in ("todo_id", "state", "source", "observation", "canonical_done")},
+ **{key: unavailable[key] for key in ("creation_request", "creation_turn") if key in unavailable}}
+ except (OSError, ValueError, RuntimeError) as exc:
+ reason = ("the original task source is temporarily unavailable" if isinstance(exc, OSError)
+ else str(exc))
+ return {**unavailable, "reason": reason}
+
def submit(self, session_id: str, *, agent_id: str, operation_id: str,
todo_id: str | None = None, monitor_deadline: str | None = None,
report_window: dict[str, Any] | None = None,
- scheduled_window: dict[str, Any] | None = None) -> tuple[dict[str, Any], bool]:
+ scheduled_window: dict[str, Any] | None = None,
+ _creation_request: dict[str, Any] | None = None) -> tuple[dict[str, Any], bool]:
from ...attached_session import managed_session_goal_commit
controller = self.controller
with controller._session_adapter_lock(session_id):
request = self._request(session_id, agent_id, operation_id, todo_id)
+ if _creation_request is not None:
+ request["creation_request"] = _creation_request
if scheduled_window is not None:
if report_window is not None or monitor_deadline is not None or not todo_id:
raise ValueError("a calendar task requires its original finite Todo")
@@ -410,29 +480,110 @@ def submit(self, session_id: str, *, agent_id: str, operation_id: str,
registry_path=controller.registry_path, session_id=session_id)
with commit():
admitted = self.admit(request, selection_recovery=selection_recovery)
+ if _creation_request is not None:
+ self._validation_source(request, _creation_request["todo_id"])
+ if not self._creation_allowance(request):
+ raise ValueError("the original creation task has no current preparation admission")
accepted = self.store.accept_managed_turn(session_id, client_turn_id=client_id,
message="Advance the currently admitted native LoopX work.",
display_message="生成本期定时报告。" if report_window is not None else
"执行本期定时任务。" if scheduled_window is not None else
"检查当前 Goal 原生任务状态。" if monitor_deadline is not None else "推进一次 LoopX 任务。",
origin="native_loopx", loopx_execution=True, loopx_request=request)
- try:
- session = self.store.load_session(session_id)
- if admitted["observation_only"]:
- from .steward_monitor import StewardObservationAdapter
- adapter = StewardObservationAdapter(session.get("upstream_thread_id"))
- else:
- adapter = controller._ensure_adapter_locked(session, work_dir=Path(admitted["workspace_path"]),
- objective="Advance this Goal's authorized work.", accepted_turn_id=accepted.turn["turn_id"])
- if Path(adapter.session.work_dir).resolve() != Path(admitted["workspace_path"]).resolve():
- raise ValueError("the original managed conversation workspace changed")
- controller._start_accepted_turn_worker(session_id=session_id, turn_id=accepted.turn["turn_id"],
- message=accepted.turn["message"], attachments=[], adapter=adapter, loopx_execution=True)
- except Exception as exc:
- controller._fail_queue_preparation(session_id, accepted.turn["turn_id"], exc)
- raise
+ self._dispatch_accepted_turn(accepted.turn, admitted)
return accepted.turn, accepted.created
+ def _dispatch_accepted_turn(self, turn: dict[str, Any], admitted: dict[str, Any]) -> None:
+ session_id = turn["session_id"]
+ request = turn.get("loopx_request") or {}
+ if "creation_request" in request:
+ from ...attached_session import managed_session_goal_commit
+ with managed_session_goal_commit(store=self.store, registry_path=self.controller.registry_path,
+ session_id=session_id)():
+ admitted = self.admit(request, turn=turn)
+ self._validation_source(request, request["todo_id"])
+ if not self._creation_allowance(request):
+ return # Retain the accepted, unstarted operation for a fresh state signal.
+ try:
+ session = self.store.load_session(session_id)
+ if admitted["observation_only"]:
+ from .steward_monitor import StewardObservationAdapter
+ adapter = StewardObservationAdapter(session.get("upstream_thread_id"))
+ else:
+ adapter = self.controller._ensure_adapter_locked(session,
+ work_dir=Path(admitted["workspace_path"]), objective="Advance this Goal's authorized work.",
+ accepted_turn_id=turn["turn_id"])
+ if Path(adapter.session.work_dir).resolve() != Path(admitted["workspace_path"]).resolve():
+ raise ValueError("the original managed conversation workspace changed")
+ self.controller._start_accepted_turn_worker(session_id=session_id, turn_id=turn["turn_id"],
+ message=turn["message"], attachments=[], adapter=adapter, loopx_execution=True)
+ except Exception as exc:
+ self.controller._fail_queue_preparation(session_id, turn["turn_id"], exc)
+ raise
+
+ def consume_creation_request(self, creation: dict[str, Any]) -> bool:
+ """Consume one saved request through the existing managed acceptance owner."""
+ from ...attached_session import managed_session_goal_commit
+ from .creation_queue import queued_turn_unstarted
+ session_id = creation["session_id"]
+ request = self._request(session_id, creation["agent_id"], creation["operation_id"], creation["todo_id"])
+ request["creation_request"] = creation
+ with self.controller._session_adapter_lock(session_id):
+ existing = self.store.turn_for_client(session_id, "managed." + creation["operation_id"])
+ if existing is not None:
+ if existing.get("loopx_request") != request:
+ raise ValueError("the original creation operation belongs to another managed request")
+ if not queued_turn_unstarted(existing):
+ return False # Unknown, failed and completed operations are never re-executed.
+ with managed_session_goal_commit(store=self.store, registry_path=self.controller.registry_path,
+ session_id=session_id)():
+ admitted = self.admit(request, turn=existing)
+ self._validation_source(request, creation["todo_id"])
+ if not self._creation_allowance(request):
+ return True
+ self._dispatch_accepted_turn(existing, admitted)
+ return True
+ self.admit(request)
+ self._validation_source(request, creation["todo_id"])
+ if not self._creation_allowance(request):
+ return True
+ self.submit(session_id, agent_id=creation["agent_id"], operation_id=creation["operation_id"],
+ todo_id=creation["todo_id"], _creation_request=creation)
+ return True
+
+ def _creation_allowance(self, request: dict[str, Any]) -> bool:
+ """Read the existing typed selected-work admission, without binding or effects.
+
+ Preparation may require the native owner to claim a lease. Its final
+ admission and launch guards still own execution after this observation.
+ """
+ from ...status import collect_status, AUTONOMOUS_REPLAN_PERIODIC_LOOKBACK
+ from ...control_plane.quota.live_decision import build_live_quota_should_run_decision
+ from ...control_plane.scheduler.execution_context import scheduler_execution_context_for_turn
+ from ...control_plane.work_items.action_selection_contract import current_action_selection_admission
+ from ...control_plane.goals.first_party_host_admission import capture_first_party_host_goal_ref
+ actor = request["actor"]
+ root = self.controller.coordination_runtime_root
+ status = collect_status(registry_path=self.controller.registry_path,
+ runtime_root_override=str(root), limit=AUTONOMOUS_REPLAN_PERIODIC_LOOKBACK,
+ goal_id=actor["goal_id"], agent_lane_id=actor["agent_id"],
+ available_capabilities=None, include_presentation_projections=False)
+ decision = build_live_quota_should_run_decision(status,
+ goal_id=actor["goal_id"], agent_id=actor["agent_id"], available_capabilities=None,
+ include_scheduler_detail=False, codex_app_current_rrule=None,
+ registry_path=self.controller.registry_path, runtime_root=root,
+ requested_action_todo_id=request["todo_id"], turn_instance_id=request["turn_instance_id"],
+ workspace_path=Path(actor["workspace_path"]),
+ goal_ref=capture_first_party_host_goal_ref(registry_path=self.controller.registry_path,
+ goal_id=actor["goal_id"]),
+ scheduler_execution_context=scheduler_execution_context_for_turn(
+ host="codex-app-server", execution_mode="isolated-headless"))
+ agent = (decision.get("interaction_contract") or {}).get("agent_channel") or {}
+ _, admitted = current_action_selection_admission(decision,
+ requested_todo_id=request["todo_id"], agent_must_attempt=agent.get("must_attempt") is True,
+ agent_delivery_allowed=agent.get("delivery_allowed"))
+ return admitted
+
def _validation_source(self, request: dict[str, Any], todo_id: str) -> dict[str, Any]:
from ...control_plane.effect_runtime import effect_runtime_result
from ...control_plane.goals.acceptance import inspect_goal_acceptance, validation_effect_files_current
diff --git a/loopx/capabilities/native_chat/steward_turn.py b/loopx/capabilities/native_chat/steward_turn.py
index b6f68efa18..96083a9160 100644
--- a/loopx/capabilities/native_chat/steward_turn.py
+++ b/loopx/capabilities/native_chat/steward_turn.py
@@ -819,7 +819,14 @@ def launch():
adapter.session._launch_guard = launch
try:
- response = adapter.start_turn(prompt, event_sink)
+ if managed_turn and not governance_work:
+ from ...control_plane.effect_runtime import effect_runtime_result
+ policy = effect_runtime_result("collaboration.session.managed_turn.sandbox_policy", {
+ "host": request["host"], "actor": actor, "authority": authority,
+ "validation": managed_turn.validation_basis["plan"], "task_lease": managed_lease})
+ response = adapter.start_native_task_turn(prompt, event_sink, policy)
+ else:
+ response = adapter.start_turn(prompt, event_sink)
finally:
adapter.session._launch_guard = previous_guard
current() # Revocation/stop/retarget after a long model call wins.
diff --git a/loopx/capabilities/periodic_report/pending_intent.py b/loopx/capabilities/periodic_report/pending_intent.py
index c1164f0093..6c3e746d2a 100644
--- a/loopx/capabilities/periodic_report/pending_intent.py
+++ b/loopx/capabilities/periodic_report/pending_intent.py
@@ -19,7 +19,6 @@
from ...control_plane.digest_envelope import sha256_envelope
from ...history import load_registry
from .field_shapes import REPORT_IDENTITY_PATTERN
-from .todo_source import read_report_todo_source
from ...registry import (
atomic_write_json,
find_registry_goal,
@@ -37,11 +36,6 @@
request_entry_for_intent,
settle_periodic_report_request,
)
-from .project_progress_snapshot import build_project_progress_snapshot
-from .incremental import (
- build_periodic_report_publication_candidate,
- write_periodic_report_publication_candidate,
-)
from .machine_defaults import (
build_periodic_report_delivery_authority,
resolve_active_goal_periodic_report_subscription,
@@ -175,6 +169,8 @@ def _superseding_approval_revision(
approval_scope = str(receipt.get("approval_scope") or "")
if receipt.get("status") != "approval_pending" or not approval_scope:
return None
+ from .todo_source import read_report_todo_source
+
_, items = read_report_todo_source(
registry_path=registry_path, goal_id=goal_id, runtime_root=runtime_root
)
@@ -513,6 +509,8 @@ def _progress_facts(
available_capabilities: Any = None,
allow_empty: bool = False,
) -> list[dict[str, Any]]:
+ from .project_progress_snapshot import build_project_progress_snapshot
+
from ...control_plane.todos.todo_index import (
MAX_TODO_INDEX_ROLLOUT_EVENTS_PER_GOAL,
)
@@ -1317,6 +1315,11 @@ def _consume_pending_periodic_report_intent(
if not execute:
return result
+ from .incremental import (
+ build_periodic_report_publication_candidate,
+ write_periodic_report_publication_candidate,
+ )
+
receipt_path = _receipt_path(
runtime_root,
goal_id,
diff --git a/loopx/chat/coordination.py b/loopx/chat/coordination.py
index 1f47003fcd..f6f57f06eb 100644
--- a/loopx/chat/coordination.py
+++ b/loopx/chat/coordination.py
@@ -6,6 +6,14 @@
from __future__ import annotations
+from copy import deepcopy
+import hashlib
+import os
+from pathlib import Path
+import shutil
+import stat
+import sys
+
PROJECT_COORDINATION_GUIDANCE = (
"This conversation concerns only the selected LoopX Goal. Read its current work, "
"member commitments and evidence before suggesting changes. Use loopx_context_read "
@@ -32,6 +40,220 @@
PROJECT_CONTEXT_VERSION = 2
+def check_preparation_tool():
+ """Project the existing proposal action for a newly bound managed thread."""
+ from ..chat_loopx_mode import TOOL
+ tool = deepcopy(TOOL)
+ tool["description"] = (
+ "Prepare a reviewable delivery check for this original registered Goal and Agent. "
+ "Use action=prepare_check with a stable operation_id and the existing compact "
+ "acceptance document: outcome_id and criteria [{id, validation_argv, oracle}]. "
+ "Read retained requirements with loopx_context_read first. Select an existing "
+ "independent checker from this Goal workspace using [python3, relative.py] or "
+ "[relative/executable], without options or extra arguments. The host pins its "
+ "actual source bytes and verifies python3 resolves to the host interpreter. Each oracle "
+ "is {kind: owner_pinned_oracle, source_id, input_refs, output_refs}; name the "
+ "explicit delivery files in output_refs. Do not invent a checker or replace "
+ "owner intent. The host supplies Goal, Agent, workspace and runtime identity. "
+ "Preparation saves only a goal.acceptance.check proposal. Explain its purpose "
+ "and effects for local-owner review; it neither configures acceptance nor runs "
+ "checks, claims work or launches a Turn. The owner judges whether the checker "
+ "addresses the requested outcome before applying it."
+ )
+ properties = tool["inputSchema"]["properties"]
+ tool["inputSchema"]["properties"] = {
+ key: properties[key] for key in ("action", "operation_id", "document")
+ }
+ tool["inputSchema"]["properties"]["document"]["description"] = (
+ "Existing compact acceptance document with outcome_id, criteria and optional scope. "
+ "Each criterion selects an existing requirement id, supported validation_argv, "
+ "and owner_pinned_oracle with explicit source_id, input_refs and output_refs. "
+ "Optional validation_files must match actual host source pins."
+ )
+ tool["inputSchema"]["properties"]["action"]["enum"] = ["prepare_check"]
+ tool["inputSchema"]["required"] = ["action", "operation_id", "document"]
+ return tool
+
+
+def _workspace_file(workspace: Path, ref: str, *, required: bool) -> str | None:
+ """Observe a regular relative file without following any symlink component.
+
+ Missing delivery files are legal during preparation. Descriptor-relative
+ opens keep a replaced parent from redirecting a source read outside the
+ original workspace; source digests are host observations, not verdicts.
+ """
+ directory_only = getattr(os, "O_DIRECTORY", None)
+ no_follow = getattr(os, "O_NOFOLLOW", None)
+ nonblock = getattr(os, "O_NONBLOCK", None)
+ if (
+ os.open not in os.supports_dir_fd
+ or directory_only is None
+ or no_follow is None
+ or nonblock is None
+ ):
+ raise ValueError("this host cannot safely inspect workspace checker sources")
+ if (not isinstance(ref, str) or not ref or "\\" in ref or "\0" in ref
+ or ref.startswith("/") or any(part in {"", ".", ".."} for part in ref.split("/"))):
+ raise ValueError("checker files must remain relative to the original workspace")
+ descriptors = []
+ try:
+ directory_flags = os.O_RDONLY | directory_only | no_follow
+ parent = os.open(workspace, directory_flags)
+ descriptors.append(parent)
+ parts = ref.split("/")
+ for part in parts[:-1]:
+ parent = os.open(part, directory_flags, dir_fd=parent)
+ descriptors.append(parent)
+ source = os.open(parts[-1], os.O_RDONLY | no_follow | nonblock, dir_fd=parent)
+ descriptors.append(source)
+ if not stat.S_ISREG(os.fstat(source).st_mode):
+ raise ValueError("checker sources and present artifacts must be regular files")
+ if not required:
+ return None
+ digest = hashlib.sha256()
+ while chunk := os.read(source, 1024 * 1024):
+ digest.update(chunk)
+ return digest.hexdigest()
+ except FileNotFoundError:
+ if not required:
+ return None
+ raise ValueError("the selected independent checker source is unavailable") from None
+ except OSError:
+ raise ValueError("checker sources and present artifacts must be readable regular files without symlinks") from None
+ finally:
+ for descriptor in reversed(descriptors):
+ os.close(descriptor)
+
+
+class _GoalSessionCheckPreparation:
+ """Per-Turn source IO over the existing typed identity and acceptance owners."""
+ def __init__(self, controller, adapter, session, turn_id, read):
+ self.controller, self.adapter, self.read = controller, adapter, read
+ self.session_id, self.turn_id = session["session_id"], turn_id
+ self.workspace = Path(adapter.session.work_dir)
+ self.runtime_root = controller.coordination_runtime_root
+ self.registry_path = controller.registry_path
+ self.original = deepcopy(session)
+ self.identity = self._current_identity()
+
+ def _current_identity(self):
+ from ..capabilities.native_chat.steward_managed_mode import registered_managed_session_identity
+ from ..control_plane.collaboration import conversation_scope
+ controller = self.controller
+ session = controller.store.load_session(self.session_id) or {}
+ turn = controller.store.load_turn(self.session_id, self.turn_id) or {}
+ fields = ("session_id", "goal_id", "goal_instance_id", "managed_agent_id",
+ "managed_workspace_path", "agent_id", "executor_endpoint_id", "adapter_kind",
+ "session_mode", "upstream_mode", "upstream_thread_id", "channel_id",
+ "project_context", "steward_context", "codex_home", "loopx_tools", "loopx_mode")
+ if (controller.closed.is_set() or controller.registry_path != self.registry_path
+ or controller.coordination_runtime_root != self.runtime_root
+ or any(session.get(key) != self.original.get(key) for key in fields)
+ or session.get("active_turn_id") != self.turn_id
+ or turn.get("session_id") != self.session_id or turn.get("turn_id") != self.turn_id
+ or turn.get("status") not in {"starting", "running"} or turn.get("origin") != "web"
+ or turn.get("loopx_execution") or turn.get("loopx_request")
+ or "collaboration_request" in turn or session.get("upstream_mode") != "chat"
+ or session.get("loopx_tools") or (session.get("loopx_mode") or {}).get("enabled")
+ or self.adapter.session.thread_id != session.get("upstream_thread_id")
+ or self.adapter.session.execution_mode or self.adapter.session.read_only_review
+ or getattr(self.adapter, "goal_driver", None) is not None
+ or Path(self.adapter.session.work_dir) != self.workspace
+ or conversation_scope(session, origin="web")["kind"] != "owner_goal"):
+ raise ValueError("the original local-owner managed Session and active read-only Turn are unavailable")
+ return registered_managed_session_identity(registry_path=self.registry_path,
+ goal_id=session.get("goal_id"), agent_id=session.get("managed_agent_id"),
+ workspace=self.workspace, session=session)
+
+ def _current(self):
+ if self._current_identity() != self.identity:
+ raise ValueError("the original managed Goal lifetime or Agent scope changed")
+
+ @staticmethod
+ def _python_interpreter():
+ executable = shutil.which("python3")
+ if executable is None or Path(executable).resolve() != Path(sys.executable).resolve():
+ raise ValueError("the supported python3 token is unavailable for this host interpreter")
+
+ def _document(self, document):
+ # Accepted document syntax, requirements, scope and oracle semantics
+ # remain with the TS owner. Only invocation/source IO is narrowed here.
+ document = deepcopy(document)
+ criteria = document.get("criteria")
+ if not isinstance(criteria, list) or not 1 <= len(criteria) <= 64:
+ raise ValueError("select bounded existing acceptance criteria and independent checker sources")
+ sources = {}
+ for criterion in criteria:
+ if not isinstance(criterion, dict):
+ raise ValueError("a check requires its existing criterion and checker source")
+ argv = criterion.get("validation_argv")
+ if (not isinstance(argv, list) or not argv or any(not isinstance(arg, str) for arg in argv)):
+ raise ValueError("select an immediate Python script or direct pinned executable")
+ if len(argv) == 2 and argv[0] in {"python", "python3", sys.executable} and argv[1].endswith(".py"):
+ source = argv[1]
+ self._python_interpreter()
+ criterion["validation_argv"] = ["python3", source]
+ elif len(argv) == 1 and "/" in argv[0]:
+ source = argv[0]
+ else:
+ raise ValueError("checker invocation supports only an immediate Python script or direct source, without options or extra arguments")
+ oracle = criterion.get("oracle")
+ if not isinstance(oracle, dict) or oracle.get("kind") != "owner_pinned_oracle":
+ raise ValueError("select an existing owner-pinned independent checker with explicit oracle outputs")
+ inputs, outputs = oracle.get("input_refs"), oracle.get("output_refs")
+ if (not isinstance(inputs, list) or not isinstance(outputs, list)
+ or len(inputs) > 20 or not 1 <= len(outputs) <= 20):
+ raise ValueError("an independent checker requires bounded explicit input and output refs")
+ if source in inputs or source in outputs:
+ raise ValueError("the independent checker source cannot be a checked input or delivery output")
+ self._current()
+ digest = _workspace_file(self.workspace, source, required=True)
+ self._current()
+ if source in sources and sources[source] != digest:
+ raise ValueError("the checker source changed during preparation")
+ pins = [{"path": source, "sha256": digest}]
+ if "validation_files" in criterion and criterion["validation_files"] != pins:
+ raise ValueError("the claimed checker source pin does not match the original workspace")
+ criterion["validation_files"] = pins
+ sources[source] = digest
+ for ref in [*inputs, *outputs]:
+ self._current()
+ _workspace_file(self.workspace, ref, required=False)
+ self._current()
+ # Reobserve the sources before publishing an immutable proposal; never
+ # refresh saved intent when a checker changed after a lost reply.
+ for source, digest in sources.items():
+ self._current()
+ if _workspace_file(self.workspace, source, required=True) != digest:
+ raise ValueError("the checker source changed during preparation")
+ self._current()
+ if any(row["validation_argv"][0] == "python3" for row in criteria):
+ self._python_interpreter()
+ return document
+
+ def __call__(self, tool, arguments):
+ from ..chat_loopx_mode import TOOL
+ if tool != TOOL["name"]:
+ return self.read(tool, arguments)
+ from ..chat_action_store import ActionConflictError
+ from ..control_plane.goals.acceptance_actions import prepare_acceptance_check
+ try:
+ self._current()
+ if (not isinstance(arguments, dict) or set(arguments) != {"action", "operation_id", "document"}
+ or arguments["action"] != "prepare_check" or not isinstance(arguments["document"], dict)
+ or not isinstance(arguments["operation_id"], str) or not arguments["operation_id"].strip()):
+ raise ValueError("prepare_check accepts only its stable operation_id and acceptance document; the host supplies actor and source identity")
+ document = self._document(arguments["document"])
+ self._current()
+ return prepare_acceptance_check(registry_path=self.registry_path,
+ runtime_root=str(self.runtime_root), goal_id=self.original["goal_id"],
+ agent_id=self.original["managed_agent_id"], document=document,
+ operation_id=arguments["operation_id"], execute=True)
+ except (ActionConflictError, ValueError) as exc:
+ return {"ok": False, "error": "collaboration_request_rejected",
+ "next_action": f"Check preparation was refused ({exc}). Read the original Goal and saved proposal; retry an operation only with unchanged intent and sources. A new intent needs a new operation id. Preparation cannot change authority or run the check."}
+
+
def apply_context_handoff(
controller, scope, session, turn, response, context, *, execution_allowed
):
@@ -212,6 +434,16 @@ def scope_valid() -> bool:
),
)
adapter.session.read_tool_handler = inspection.read
+ adapter._managed_read_turn_guard = None
+ if (session.get("managed_agent_id") and turn.get("origin") == "web"
+ and not turn.get("loopx_request") and not turn.get("loopx_execution")
+ and not session.get("loopx_tools") and not (session.get("loopx_mode") or {}).get("enabled")
+ and not adapter.session.execution_mode and not adapter.session.read_only_review
+ and getattr(adapter, "goal_driver", None) is None
+ and scope["kind"] == "owner_goal" and session.get("upstream_mode") == "chat"):
+ adapter.session.read_tool_handler = _GoalSessionCheckPreparation(
+ controller, adapter, session, turn_id, inspection.read)
+ adapter._managed_read_turn_guard = adapter.session.read_tool_handler._current
context["evidence_sources"] = inspection.sources()
context = manager_index(context, status_on_demand=status_on_demand)
from ..capabilities.native_chat.external_conversations import ChatExternalConversations
diff --git a/loopx/chat/goal_creation_execution.py b/loopx/chat/goal_creation_execution.py
new file mode 100644
index 0000000000..298332cbdb
--- /dev/null
+++ b/loopx/chat/goal_creation_execution.py
@@ -0,0 +1,99 @@
+"""IO in the existing confirmed Goal-creation saga, not a new execution owner."""
+from __future__ import annotations
+
+from typing import Any
+
+from ..agent_registry import load_goal_from_registry
+from ..boundary_authority import build_checkpointed_boundary_authority_entry
+from ..chat_action_store import ActionConflictError
+from ..configure_goal import configure_goal
+from ..control_plane.runtime.time import now_utc
+
+
+def retain_creation_write_approval(service: Any, proposal_id: str, review: dict[str, Any] | None) -> None:
+ """Publish an explicit reviewed grant once and recover its exact readback.
+
+ The typed creation review chooses scope. This IO adapter uses the existing
+ configuration/authority owners and never refreshes a lost or revoked grant.
+ """
+ if not review or not review["writeScope"]:
+ return
+ proposal = service.store.load(proposal_id)
+ goal_id = proposal["normalized_parameters"]["goal_id"]
+ goal = load_goal_from_registry(service.registry_path, goal_id)
+ if goal is None or goal.get("creation_operation_id") != proposal_id:
+ raise ActionConflictError("The write approval belongs to the original Goal creation")
+ steps = (proposal.get("checkpoint") or {}).get("steps", {})
+ prepared = steps.get("boundary_authority_prepared")
+ if prepared is None:
+ entry = build_checkpointed_boundary_authority_entry(
+ write_scopes=review["writeScope"], source="goal_creation_review",
+ decision_id=proposal_id, recorded_at=now_utc().isoformat(),
+ )
+ prepared = {"goal_id": goal_id, "goal_instance_id": goal.get("goal_instance_id"), "authority": entry}
+ service.store.save_checkpoint(proposal_id, step="boundary_authority_prepared", receipt=prepared)
+ entry = prepared.get("authority")
+ if (prepared.get("goal_id") != goal_id or prepared.get("goal_instance_id") != goal.get("goal_instance_id")
+ or not isinstance(entry, dict) or entry.get("write_scope") != review["writeScope"]
+ or entry.get("decision_id") != proposal_id or entry.get("source") != "goal_creation_review"
+ or entry.get("status") != "active" or entry.get("decision") != "approve"):
+ raise ActionConflictError("The original reviewed write approval changed; retain its recorded operation")
+
+ def observe() -> tuple[dict[str, Any], list[dict[str, Any]]]:
+ current = load_goal_from_registry(service.registry_path, goal_id)
+ if (current is None or current.get("creation_operation_id") != proposal_id
+ or current.get("goal_instance_id") != prepared["goal_instance_id"]):
+ raise ActionConflictError("The original Goal lifetime changed before approval readback")
+ coordination = current.get("coordination") or {}
+ entries = coordination.get("checkpointed_boundary_authority") or []
+ if not isinstance(entries, list):
+ raise ActionConflictError("The original boundary authority is unreadable")
+ return coordination, [row for row in entries if isinstance(row, dict) and row.get("decision_id") == proposal_id]
+
+ coordination, matches = observe()
+ if not matches:
+ if steps.get("boundary_authority_write_started") is not None or steps.get("boundary_authority_recorded") is not None:
+ raise ActionConflictError("The original approval publication is unconfirmed or revoked; read back that operation before proceeding")
+ service.store.save_checkpoint(proposal_id, step="boundary_authority_write_started",
+ receipt={"goal_id": goal_id, "decision_id": proposal_id})
+ configure_goal(registry_path=service.registry_path, goal_id=goal_id,
+ write_scope=review["writeScope"], boundary_authority_scopes=entry["write_scope"],
+ boundary_authority_source=entry["source"], boundary_authority_decision_id=entry["decision_id"],
+ boundary_authority_recorded_at=entry["recorded_at"], execute=True)
+ coordination, matches = observe()
+ if (matches != [entry] or any(scope not in (coordination.get("write_scope") or []) for scope in review["writeScope"])):
+ raise ActionConflictError("The original write approval or declared scope changed; do not refresh it from a creation replay")
+ service.store.save_checkpoint(proposal_id, step="boundary_authority_recorded", receipt={
+ "outcome": "boundary_authority_recorded", **prepared,
+ })
+
+
+def retain_original_task_request(service: Any, proposal_id: str, session: dict[str, Any]) -> dict[str, Any] | None:
+ """Persist the same reviewed request before queueing, without executing it."""
+ from ..capabilities.native_chat.steward_managed_mode import StewardManagedMode
+ from ..control_plane.effect_runtime import effect_runtime_result
+ from ..control_plane.goals.acceptance import inspect_goal_acceptance
+ proposal = service.store.load(proposal_id)
+ if proposal["normalized_parameters"].get("start_execution") is not True:
+ return None
+ controller = service.runtime_controller
+ basis = inspect_goal_acceptance(registry_path=service.registry_path,
+ runtime_root=str(controller.coordination_runtime_root), goal_id=session["goal_id"],
+ agent_id=session["managed_agent_id"])
+ selection = effect_runtime_result("goal.task.creation_selection", {"basis": basis})
+ request = effect_runtime_result("goal.creation.execution_request.prepare", {
+ "proposal": proposal, "session": session,
+ "goal": StewardManagedMode(controller, None)._goal(session["goal_id"]),
+ "current_original_task": selection,
+ })
+ prior = ((proposal.get("checkpoint") or {}).get("steps") or {}).get("original_task_requested")
+ receipt = {"outcome": "original_task_requested", **request, "source": {
+ "request_digest": proposal["request_digest"],
+ "expected_state_fingerprint": proposal["expected_state_fingerprint"],
+ "upstream_thread_id": session["upstream_thread_id"],
+ }}
+ if prior is not None and prior != receipt:
+ raise ActionConflictError("The retained original Task execution request changed")
+ if prior is None:
+ service.store.save_checkpoint(proposal_id, step="original_task_requested", receipt=receipt)
+ return request
diff --git a/loopx/chat/session_open.py b/loopx/chat/session_open.py
index c61db67895..167eff8947 100644
--- a/loopx/chat/session_open.py
+++ b/loopx/chat/session_open.py
@@ -20,15 +20,140 @@
from __future__ import annotations
from pathlib import Path
-from typing import Any
+from typing import TYPE_CHECKING, Any, Mapping
from ..chat_manager import open_manager_session
+if TYPE_CHECKING:
+ from ..chat_action_store import ChatActionStore
+ from ..chat_runtime import ChatRuntimeController
+ from ..chat_store import ChatSessionStore
+
# The executor a Goal-scoped session runs on when the caller makes no explicit
# pick. The steward channel does not use this: it resolves its own default.
DEFAULT_GOAL_AGENT_ID = "codex"
+def require_regular_session_paths(chat_store: ChatSessionStore, session_id: str) -> None:
+ """A redirected or incomplete reservation cannot identify an original."""
+ directory, metadata = chat_store._session_dir(session_id), chat_store._session_path(session_id)
+ if (directory.is_symlink() or metadata.is_symlink()
+ or directory.exists() and not directory.is_dir()
+ or metadata.exists() and not metadata.is_file()):
+ raise ValueError("The original Session reservation is unavailable; restore its regular metadata")
+
+
+def read_goal_creation_session(
+ action_store: ChatActionStore,
+ chat_store: ChatSessionStore | None,
+ *,
+ proposal_id: str,
+ session_id: str,
+ goal_id: str,
+ agent_id: str,
+ workspace: Path,
+ goal_instance_id: str | None,
+ expected_managed_agent: str | None = None,
+) -> dict[str, Any]:
+ """Read the original metadata against the creation's fresh checkpoint."""
+ from ..control_plane.collaboration import conversation_scope
+
+ if chat_store is not None:
+ require_regular_session_paths(chat_store, session_id)
+ session = chat_store.load_session(session_id) if chat_store is not None else None
+ scope = conversation_scope(session) if session is not None else {}
+ if (session is None or session.get("session_id") != session_id or session.get("goal_id") != goal_id
+ or session.get("agent_id") != agent_id
+ or scope.get("goal_ids") != [goal_id] or scope.get("kind") != "owner_goal"):
+ raise ValueError("The original first Session is unavailable; restore it before retrying")
+ if expected_managed_agent is not None and session.get("managed_agent_id") != expected_managed_agent:
+ raise ValueError("The original registered Agent Session identity changed")
+ recorded_proposal = action_store.load(proposal_id)
+ if recorded_proposal is None:
+ raise ValueError("The original creation checkpoint is unavailable")
+ recorded = ((recorded_proposal.get("checkpoint") or {}).get("steps") or {}).get("first_session_opened") or {}
+ for field in ("channel_id", "managed_agent_id", "goal_instance_id"):
+ if field in recorded and recorded[field] != session.get(field):
+ raise ValueError("The original first Session route changed; restore its recorded identity")
+ if session.get("managed_agent_id") is not None and (
+ session["managed_agent_id"] != agent_id
+ or session.get("goal_instance_id") != goal_instance_id
+ or session.get("managed_workspace_path") != str(workspace)
+ ):
+ raise ValueError("The original registered Agent Session identity changed")
+ return session
+
+
+def recover_goal_creation_session(
+ chat_store: ChatSessionStore,
+ *,
+ client_turn_id: str,
+ goal_id: str,
+ agent_id: str,
+) -> dict[str, Any] | None:
+ """Recover a unique published original, preferring its original client Turn."""
+ from ..control_plane.collaboration import conversation_scope
+
+ candidates = []
+ for directory in chat_store.sessions_root.iterdir():
+ if not directory.is_dir() and not directory.is_symlink():
+ continue
+ require_regular_session_paths(chat_store, directory.name)
+ if not chat_store._session_path(directory.name).is_file():
+ raise ValueError("The original Session reservation is unconfirmed; restore its metadata")
+ row = chat_store.load_session(directory.name)
+ if row is None or row.get("session_id") != directory.name:
+ raise ValueError("Original Session discovery is unreadable; restore it before retrying")
+ if (row.get("goal_id") == goal_id and row.get("agent_id") == agent_id
+ and conversation_scope(row).get("kind") == "owner_goal"):
+ candidates.append(row)
+ matches = [row for row in candidates if chat_store.turn_for_client(row["session_id"], client_turn_id) is not None]
+ if len(matches) > 1 or not matches and len(candidates) > 1:
+ raise ValueError("The original first Session is ambiguous; restore its recorded operation")
+ if matches:
+ return matches[0]
+ if candidates:
+ raise ValueError("The original first Session opening is unconfirmed; restore its exact checkpoint")
+ return None
+
+
+def read_exact_original_session(
+ controller: ChatRuntimeController,
+ *,
+ session_id: str,
+ selected_scope: Mapping[str, object],
+ agent_id: str,
+ work_dir: Path,
+ requested_model: str | None = None,
+ requested_effort: str | None = None,
+) -> dict[str, Any] | None:
+ """Read one exact opening under the caller's existing Session opening lock."""
+ from ..capabilities.native_chat.steward_managed_mode import require_managed_agent_session
+ from ..chat_store import CHAT_SESSION_MODE_MANAGED, RESUMABLE_SESSION_STATES
+ from ..control_plane.collaboration import conversation_scope
+
+ require_regular_session_paths(controller.store, session_id)
+ original = controller.store.load_session(session_id)
+ if original is not None:
+ if (original.get("session_id") != session_id
+ or original.get("goal_id") != selected_scope.get("goal_id") or original.get("agent_id") != agent_id
+ or original.get("executor_endpoint_id") != agent_id
+ or original.get("channel_id") != selected_scope.get("channel_id")
+ or original.get("managed_agent_id") != selected_scope.get("managed_agent_id")
+ or original.get("goal_instance_id") != selected_scope.get("goal_instance_id")
+ or original.get("session_mode") != CHAT_SESSION_MODE_MANAGED
+ or conversation_scope(original)["kind"] != "owner_goal"
+ or original.get("status") not in RESUMABLE_SESSION_STATES
+ or requested_model and original.get("agent_model_id") != requested_model
+ or requested_effort and original.get("agent_reasoning_effort") != requested_effort):
+ raise ValueError("the original Goal Session opening identity changed")
+ require_managed_agent_session(controller, original, work_dir)
+ return original
+ if controller.store._session_dir(session_id).exists():
+ raise ValueError("the original Goal Session is unreadable; restore it before retrying")
+ return None
+
+
def open_chat_session(
*,
controller: Any,
@@ -97,3 +222,19 @@ def open_chat_session(
agent_model_id=agent_model_id or None,
**({"agent_reasoning_effort": agent_reasoning_effort} if agent_reasoning_effort is not None else {}),
)
+
+
+def goal_creation_display_message(store: ChatSessionStore, *, session_id: str, client_turn_id: str, objective: str) -> str:
+ """Show confirmed intent on creation; preserve the original display on retry.
+
+ The execution request remains unchanged. An old recorded message is never
+ rewritten using the Goal's later objective or a new presentation default.
+ """
+ turn = store.turn_for_client(session_id, client_turn_id)
+ if turn is None:
+ return objective
+ messages = [row for row in store.messages(session_id, strict=True)
+ if row.get("turn_id") == turn["turn_id"] and row.get("role") == "user"]
+ if len(messages) != 1 or not isinstance(messages[0].get("text"), str):
+ raise ValueError("The original first Turn display is unavailable; restore its recorded message")
+ return messages[0]["text"]
diff --git a/loopx/chat_action_normalization.py b/loopx/chat_action_normalization.py
index bb67f17b15..ebdfc9f9a8 100644
--- a/loopx/chat_action_normalization.py
+++ b/loopx/chat_action_normalization.py
@@ -546,6 +546,7 @@ def _normalize(
"stop_condition",
"initial_todos",
"native_token_budget",
+ "start_execution",
},
)
goal_id = _opaque(values.get("goal_id"), field="goal_id")
@@ -569,11 +570,13 @@ def _normalize(
"objective",
"completion_criteria",
"execution_boundary",
- "permission",
"stop_condition",
):
if values.get(field):
result[field] = _text(values[field], field=field, limit=1000)
+ if "permission" in values or "start_execution" in values:
+ from .control_plane.effect_runtime import effect_runtime_result
+ result.update(effect_runtime_result("goal.creation.execution.normalize", values))
for field in ("agent_id", "workspace_ref"):
if values.get(field):
result[field] = _opaque(values[field], field=field)
diff --git a/loopx/chat_actions.py b/loopx/chat_actions.py
index ecd46562ea..f743e72841 100644
--- a/loopx/chat_actions.py
+++ b/loopx/chat_actions.py
@@ -242,6 +242,8 @@ def __init__(
self.registry_path = Path(registry_path).expanduser().resolve()
self.chat_store = chat_store
self.runtime_controller = runtime_controller
+ if runtime_controller is not None:
+ runtime_controller.original_task_action_store = store
# The runtime root the serving Chat selected (its --runtime-root). A
# creation on a registry that does not exist yet has no recorded root
# to follow, so the first Goal must persist where the service already
@@ -668,7 +670,8 @@ def require_first_turn_readback(turn: dict[str, Any]) -> None:
),
None,
)
- progress = effect_runtime_result("presentation.action_review_plan.compile", {"proposal": proposal}).get("creationProgress") or {}
+ creation_plan = effect_runtime_result("presentation.action_review_plan.compile", {"proposal": proposal})
+ progress = creation_plan.get("creationProgress") or {}
if (proposal.get("checkpoint") or {}).get("steps") and not progress:
raise ValueError("Goal creation checkpoints do not match the original request; inspect the recorded operation")
project, source_goal = self._project_for_goal_create(proposal, progress=progress)
@@ -787,6 +790,9 @@ def require_first_turn_readback(turn: dict[str, Any]) -> None:
},
)
todo_ids: list[str] = list(progress.get("todoIds") or [])
+ if creation_plan.get("creationExecution"):
+ from .chat.goal_creation_execution import retain_creation_write_approval
+ retain_creation_write_approval(self, proposal_id, creation_plan["creationExecution"])
# Canonical File/SQLite own replay after a committed write loses its
# response. Historical target-only creation retains its old adapter.
pending_todos = (parameters.get("initial_todos") or []) if "todoIds" not in progress else []
@@ -803,6 +809,8 @@ def require_first_turn_readback(turn: dict[str, Any]) -> None:
agent_id=agent_id or None,
dry_run=False,
operation_id=f"goal-create:{proposal_id}:todo:{index}" if authority is not None else None,
+ **({"required_write_scopes": creation_plan["creationExecution"]["writeScope"]}
+ if creation_plan.get("creationExecution", {}).get("writeScope") else {}),
)
if todo.get("todo_id"):
todo_ids.append(str(todo["todo_id"]))
@@ -842,6 +850,14 @@ def require_first_turn_readback(turn: dict[str, Any]) -> None:
raise ValueError(
"Goal projection did not retain the selected Agent binding"
)
+ from functools import partial
+ from .chat.session_open import read_goal_creation_session, recover_goal_creation_session, require_regular_session_paths, goal_creation_display_message
+
+ read_original_session = partial(read_goal_creation_session,
+ self.store, self.chat_store or getattr(self.runtime_controller, "store", None),
+ proposal_id=proposal_id, goal_id=goal_id, agent_id=agent_id,
+ workspace=project, goal_instance_id=projected.get("goal_instance_id"))
+
turn_result: dict[str, Any] | None = None
session_id = ""
first_turn_message = (f"/goal start --tokens {parameters['native_token_budget']} {objective}"
@@ -850,22 +866,44 @@ def require_first_turn_readback(turn: dict[str, Any]) -> None:
f"首个 Todo:{';'.join(str(item) for item in (parameters.get('initial_todos') or [])[:3]) or '按目标边界建立首个可验证进展'}。"
"然后直接推进并报告可验证结果;遇到权限边界时停止并提出明确 Gate。"
))
+ opening_session_id = _digest({"goal_create": proposal_id, "goal_id": goal_id})[:32]
+ expected_managed_agent = (creation_plan.get("creationSession") or {}).get("managedAgentId")
+ turn_store = self.chat_store or getattr(self.runtime_controller, "store", None)
+ if not progress.get("firstTurn") and not progress.get("sessionId") and turn_store is not None:
+ require_regular_session_paths(turn_store, opening_session_id)
+ original = turn_store.load_session(opening_session_id)
+ if original is not None:
+ read_original_session(session_id=opening_session_id, expected_managed_agent=expected_managed_agent)
+ elif turn_store._session_dir(opening_session_id).exists():
+ raise ValueError("The original first Session is unreadable; restore it before retrying")
+ elif existing_goal is not None:
+ # Historical opening responses carried no operation-bound id.
+ # Recover only an unambiguous original; never choose latest.
+ original = recover_goal_creation_session(turn_store,
+ client_turn_id=f"goal-start-{proposal_id}", goal_id=goal_id, agent_id=agent_id)
+ if original is not None:
+ read_original_session(session_id=original["session_id"])
+ progress = {**progress, "sessionId": original["session_id"]}
if progress.get("firstTurn"):
session_id = progress["firstTurn"]["sessionId"]
turn_id = progress["firstTurn"]["turnId"]
turn_store = self.chat_store or getattr(self.runtime_controller, "store", None)
- session = turn_store.load_session(session_id) if turn_store is not None else None
- turn = turn_store.load_turn(session_id, turn_id) if session is not None else None
- if session is None or session.get("goal_id") != goal_id or session.get("agent_id") != agent_id or turn is None:
+ session = read_original_session(session_id=session_id)
+ turn = turn_store.load_turn(session_id, turn_id) if turn_store is not None else None
+ if turn is None:
raise ValueError("The recorded first Turn is unavailable; restore its original Session instead of starting another")
+ if (turn.get("message") != first_turn_message
+ or turn.get("client_turn_id") != f"goal-start-{proposal_id}"):
+ raise ValueError("The original first Turn request changed; retain its recorded operation")
require_first_turn_readback(turn)
turn_result = {"turn_id": turn_id, "status": str(turn["status"]), "created": False}
elif progress.get("sessionId"):
turn_store = self.chat_store or getattr(self.runtime_controller, "store", None)
if turn_store is None:
raise ValueError("The original first Session is unavailable; restore it before retrying")
+ original_session = read_original_session(session_id=progress["sessionId"])
session, turn = self._read_task_start(
- turn_store, goal_id=goal_id, agent_id=agent_id, channel_id=f"goal.{goal_id}",
+ turn_store, goal_id=goal_id, agent_id=agent_id, channel_id=original_session["channel_id"],
client_turn_id=f"goal-start-{proposal_id}", checkpoint={"session_id": progress["sessionId"]},
)
if turn is not None:
@@ -924,9 +962,7 @@ def require_first_turn_readback(turn: dict[str, Any]) -> None:
}
if agent_id and self.runtime_controller is not None and first_turn_gate is None and turn_result is None:
if progress.get("sessionId"):
- session = self.runtime_controller.store.load_session(progress["sessionId"])
- if session is None or session.get("goal_id") != goal_id or session.get("agent_id") != agent_id:
- raise ValueError("The original first Session is unavailable; restore it before retrying")
+ session = read_original_session(session_id=progress["sessionId"])
else:
session, _resumed = self.runtime_controller.open_session(
goal_id=goal_id,
@@ -938,19 +974,34 @@ def require_first_turn_readback(turn: dict[str, Any]) -> None:
agent_id=agent_id,
work_dir=project,
objective=objective,
- mode="resume_latest",
+ mode="new",
+ session_id=opening_session_id,
channel_id=f"goal.{goal_id}",
agent_goal_id=goal_id,
+ **({"managed_agent_id": creation_plan["creationSession"]["managedAgentId"]}
+ if creation_plan.get("creationSession") else {}),
)
session_id = _opaque(session.get("session_id"), field="session_id")
+ if not progress.get("sessionId") and session_id != opening_session_id:
+ raise ValueError("The original first Session opening returned another operation")
+ session = read_original_session(session_id=session_id,
+ expected_managed_agent=expected_managed_agent if not progress.get("sessionId") else None)
self.store.save_checkpoint(proposal_id, step="first_session_opened",
- receipt={"outcome": "first_session_opened", "session_id": session_id})
+ receipt={"outcome": "first_session_opened", "session_id": session_id,
+ "channel_id": session["channel_id"],
+ **({"managed_agent_id": session["managed_agent_id"],
+ "goal_instance_id": session.get("goal_instance_id")}
+ if session.get("managed_agent_id") is not None else {}),
+ **({"upstream_thread_id": session.get("upstream_thread_id")}
+ if creation_plan.get("creationExecution") else {})})
first_turn, created = self.runtime_controller.submit_turn(
session_id=session_id,
client_turn_id=f"goal-start-{proposal_id}",
message=first_turn_message,
work_dir=project,
objective=objective,
+ **({"display_message": goal_creation_display_message(turn_store, session_id=session_id,
+ client_turn_id=f"goal-start-{proposal_id}", objective=objective)} if expected_managed_agent else {}),
)
require_first_turn_readback(first_turn)
turn_result = {
@@ -973,6 +1024,18 @@ def require_first_turn_readback(turn: dict[str, Any]) -> None:
step="first_turn_gated",
receipt={"outcome": "first_turn_gated", "gate": first_turn_gate},
)
+ if creation_plan.get("creationExecution"):
+ # An applied receipt is immutable. Keep this confirmed start
+ # retryable until its original Session/request is retained;
+ # quota denial cannot silently discard the requested work.
+ raise ValueError("The original Goal and Task are retained, but their first Turn admission is held. "
+ + first_turn_gate["summary"] + " Retry this same creation when its allowance is ready.")
+ if session_id:
+ session = read_original_session(session_id=session_id)
+ original_task_request = None
+ if session_id and parameters.get("start_execution") is True:
+ from .chat.goal_creation_execution import retain_original_task_request
+ original_task_request = retain_original_task_request(self, proposal_id, session)
child_gate: dict[str, Any] | None = first_turn_gate
if heartbeat.get("enabled") is True:
heartbeat_parameters = {
@@ -1016,18 +1079,24 @@ def require_first_turn_readback(turn: dict[str, Any]) -> None:
**({"agent_id": agent_id} if agent_id else {}),
**({"todo_ids": todo_ids} if todo_ids else {}),
**({"session_id": session_id} if session_id else {}),
+ **({"managed_agent_id": session["managed_agent_id"], "channel_id": session["channel_id"],
+ "goal_instance_id": session.get("goal_instance_id")}
+ if session_id and session.get("managed_agent_id") is not None else {}),
**({"turn_id": turn_result["turn_id"]} if turn_result else {}),
},
**({"child_gate": child_gate} if child_gate else {}),
"step_receipts": (
(self.store.load(proposal_id) or {}).get("checkpoint") or {}
).get("steps", {}),
+ **({"original_task_request": original_task_request} if original_task_request is not None else {}),
}
stored = self.store.apply(
proposal_id,
current_state_fingerprint=str(proposal["expected_state_fingerprint"]),
receipt=receipt,
)
+ if original_task_request is not None:
+ self.runtime_controller.resume_session_queue(session_id=session_id, work_dir=project, objective=objective)
return {"proposal": stored, "turn": turn_result, "gate": child_gate}
def _apply_agent_bind(
@@ -1544,6 +1613,18 @@ def regenerate(self, proposal_id: str) -> dict[str, Any]:
def apply(self, proposal_id: str, *, steward_context: dict[str, Any] | None = None,
steward_confirmed_at: str | None = None) -> dict[str, Any]:
+ result = self._apply_owner_action(proposal_id, steward_context=steward_context,
+ steward_confirmed_at=steward_confirmed_at)
+ # A committed action changes current facts. Existing queue readers may
+ # reobserve their retained requests; this content-free hint performs no
+ # transport, Task admission, permission change or new submission.
+ wakeup = getattr(self.runtime_controller, "turn_release_wakeup", None)
+ if (result.get("proposal") or {}).get("status") == "applied" and wakeup is not None:
+ wakeup.notify()
+ return result
+
+ def _apply_owner_action(self, proposal_id: str, *, steward_context: dict[str, object] | None = None,
+ steward_confirmed_at: str | None = None) -> dict[str, object]:
proposal = self.store.load(proposal_id)
if proposal is None:
raise KeyError("typed Chat action proposal was not found")
@@ -1570,7 +1651,7 @@ def apply(self, proposal_id: str, *, steward_context: dict[str, Any] | None = No
},
)
parameters = proposal.get("normalized_parameters")
- if (proposal.get("action_kind") == "todo.create" and isinstance(parameters, dict)
+ if (proposal.get("action_kind") in {"todo.create", "goal.create"} and isinstance(parameters, dict)
and parameters.get("start_execution") is True):
key = _opaque(proposal_id, field="proposal_id")
with try_exclusive_file_lock(
diff --git a/loopx/chat_agent.py b/loopx/chat_agent.py
index 951c6832b8..033b57975a 100644
--- a/loopx/chat_agent.py
+++ b/loopx/chat_agent.py
@@ -720,6 +720,8 @@ class CodexChatAgentSession:
_launch_guard: Callable[[], AbstractContextManager[None]] | None = field(
default=None, repr=False
)
+ _native_task_policy_used: bool = field(default=False, repr=False)
+ _managed_goal_read_default: bool = field(default=False, repr=False)
@classmethod
def start(
@@ -1734,28 +1736,15 @@ def send(
on_native_item: Callable[[dict[str, Any]], None] | None = None,
turn_start_guard: Callable[[], AbstractContextManager[None]] | None = None,
check_running: Callable[[], None] | None = None,
+ _native_task: bool = False,
+ sandbox_policy: dict[str, object] | None = None,
) -> dict[str, Any]:
- text = str(user_message or "")
- if not text.strip():
- raise ValueError("user message is required")
- if self.read_only_review and not self.metadata_review and not self.answer_review:
- text = (
- "Review the existing results of the interrupted original request below. "
- "Its earlier execution may have already changed files or external systems. "
- "Use the original native history and current local artifacts to verify what actually happened. "
- "This Turn is read-only: do not re-execute the business request, write, send, delegate or start background work. "
- "Return a clear answer to the original user, distinguishing verified results from remaining gaps. "
- "If verification is incomplete, report the concrete limitation; do not claim completion. "
- "Original request: " + text
- )
- if project_collaboration:
- if (self.execution_mode or self.read_only_review or output_schema is not None
- or not self.project_context or self.project_context.get("audience") != "bound_owner"):
- raise ValueError("original project collaboration requires its existing bound project session")
- from .control_plane.effect_runtime import effect_runtime_result
- output_schema = effect_runtime_result("collaboration.project.response_schema", {})
- if output_schema is not None and not self.execution_mode and not project_collaboration:
- raise ValueError("structured Turn output requires an execution session")
+ if not _native_task and (self._native_task_policy_used or self._managed_goal_read_default) and sandbox_policy is None:
+ sandbox_policy = {"type": "readOnly", "networkAccess": False}
+ from .capabilities.native_chat.codex_adapter import prepare_native_turn_text
+ text, output_schema = prepare_native_turn_text(self, user_message, output_schema=output_schema,
+ project_collaboration=project_collaboration, native_task=_native_task,
+ sandbox_policy=sandbox_policy, turn_start_guard=turn_start_guard)
with self._request_id_lock:
request_id = self.next_request_id
self.next_request_id += 1
@@ -1763,7 +1752,7 @@ def send(
text = _turn_prompt(
text,
context_summary=self.context_summary,
- execution_mode=self.execution_mode,
+ execution_mode=self.execution_mode or _native_task,
runtime_profile=self.runtime_profile,
project_work=self.project_context is not None and self.sandbox == "workspace-write",
public_source_read_context=self._public_source_read_context,
@@ -1777,6 +1766,10 @@ def send(
started_at = time.monotonic()
start_timeout = min(self.idle_timeout_sec, self.hard_timeout_sec
if self.hard_timeout_sec is not None else self.idle_timeout_sec)
+ if _native_task:
+ # Publication may be uncertain. Every subsequent ordinary send
+ # must narrow the sticky native policy, even after an exception.
+ self._native_task_policy_used = True
turn_result = self._request(
"turn/start",
{
@@ -1788,9 +1781,13 @@ def send(
# Chat renders native summaries, but a resumed thread may have
# disabled them. Request the public summary on each Chat Turn;
# execution hosts retain their own verbosity configuration.
- **({"summary": "concise"} if not self.execution_mode else {}),
+ **({"summary": "concise"} if not (self.execution_mode or _native_task) else {}),
"approvalPolicy": "never",
**({"permissions": self.permissions_profile} if self.permissions_profile else {}),
+ **({"sandboxPolicy": sandbox_policy} if sandbox_policy is not None else {}),
+ **({"runtimeWorkspaceRoots": [str(self.work_dir.resolve())]}
+ if _native_task and sandbox_policy is not None
+ and sandbox_policy.get("type") == "workspaceWrite" else {}),
**(
{"outputSchema": output_schema} if output_schema is not None else {}
),
diff --git a/loopx/chat_ingress.py b/loopx/chat_ingress.py
index 286c7aa01e..0d180d9703 100644
--- a/loopx/chat_ingress.py
+++ b/loopx/chat_ingress.py
@@ -247,6 +247,11 @@ def read(path):
and str(payload.get("turn_id") or "") != active_turn_id
):
rows.append(payload)
+ # The ordinary read has no selectable Turn when storage has no queued
+ # candidates. Explicit capacity and strict reads still use the owner.
+ if not rows and not strict and not include_reserved and capacity_at is None:
+ return []
+
from .control_plane.chat_turn_acceptance import queued_chat_turn_ids
selected = set(queued_chat_turn_ids(rows, active_turn_id, include_reserved=include_reserved, capacity_at=capacity_at))
return sorted(
diff --git a/loopx/chat_runtime.py b/loopx/chat_runtime.py
index c018072005..beb4d4a687 100644
--- a/loopx/chat_runtime.py
+++ b/loopx/chat_runtime.py
@@ -24,6 +24,7 @@
steward_machine_defaults,
)
from .chat.coordination import PROJECT_COORDINATION_GUIDANCE, PROJECT_CONTEXT_VERSION, apply_context_handoff
+from .chat.session_open import read_exact_original_session
from .capabilities.native_chat import codex_context, project_context as project_context_policy
from .capabilities.native_chat.codex_adapter import (
CodexAppServerAdapter, NativeHistoryRestoration, NativeWorkingContinuation, start_attachment_turn,
@@ -59,6 +60,7 @@
from .file_lock import (
LOCK_HOLDER_DEAD, LOCK_HOLDER_LIVE, LOCK_HOLDER_RELEASED,
_lock_id, lock_holder_host_label, lock_holder_liveness, process_is_alive, try_exclusive_file_lock,
+ exclusive_cross_runtime_file_lock,
)
from .kiro_cli_goal_mode import (
KIRO_CLI_BIN,
@@ -299,6 +301,7 @@ def __init__(
self.session_queue_workers: set[str] = set()
self.session_queue_threads: dict[str, threading.Thread] = {}
self.session_queue_wakeups: set[str] = set()
+ self.original_task_action_store = None
self.closed = threading.Event()
from .chat_loopx_mode import ChatLoopXMode
self.loopx_mode = ChatLoopXMode(self)
@@ -372,6 +375,8 @@ def _start_adapter(
manager_runtime: Mapping[str, Any] | None = None,
project_coordination: bool = False,
loopx_tools: bool = False,
+ check_preparation: bool = False,
+ managed_read_default: bool = False,
executor_model: Mapping[str, str | None] | None = None,
project_context: dict[str, str] | None = None,
agent_config: dict[str, str] | None = None,
@@ -410,11 +415,9 @@ def _start_adapter(
manager_runtime=manager_runtime, executor_model=executor_model,
agent_config=agent_config, execution_mode=execution_mode,
)
- native_context = {
- "objective": self._session_objective(goal_id=goal_id, objective=objective,
- project_coordination=project_coordination and not execution_mode),
- "runtime_profile": str(manager_profile["runtime_profile"]) if manager_profile else "restricted",
- }
+ from .capabilities.native_chat.codex_adapter import native_runtime_context, start_runtime_adapter
+ native_context = native_runtime_context(self, goal_id=goal_id, objective=objective,
+ manager_profile=manager_profile, project_coordination=project_coordination, execution_mode=execution_mode)
if agent_id == "antigravity":
from .capabilities.native_chat.antigravity_adapter import AntigravityAdapter
if execution_mode or project_context is not None or read_only_review:
@@ -431,11 +434,12 @@ def _start_adapter(
hard_timeout_sec=self.hard_timeout_sec,
)
if agent_id == "codex":
- from .capabilities.native_chat.codex_adapter import start_runtime_adapter
return start_runtime_adapter(self, work_dir=work_dir, goal_id=goal_id, native_context=native_context,
manager_profile=manager_profile, model_config=model_config, history=history,
resume_thread_id=resume_thread_id, execution_mode=execution_mode, project_context=project_context,
project_coordination=project_coordination, loopx_tools=loopx_tools,
+ check_preparation=check_preparation,
+ managed_read_default=managed_read_default,
codex_home_override=codex_home_override, read_only_review=read_only_review,
metadata_review_context=metadata_review_context, answer_review_context=answer_review_context,
history_check=history_check, working_continuation=working_continuation)
@@ -521,6 +525,7 @@ def open_session(
agent_goal_id: str | None = None,
goal_instance_id: str | None = None,
managed_agent_id: str | None = None,
+ session_id: str | None = None,
manager_executor_allocation: Mapping[str, Any] | None = None,
project_ref: str | None = None,
conversation_binding_id: str | None = None,
@@ -541,6 +546,8 @@ def open_session(
error_code="unsupported_configuration", gate=None)
if mode not in {"resume_latest", "new"}:
raise ValueError("mode must be resume_latest or new")
+ if session_id is not None and mode != "new":
+ raise ValueError("an exact Session opening requires mode=new")
selected_channel = channel_id or f"goal.{goal_id}"
project_context = None
steward_context = None
@@ -593,9 +600,24 @@ def open_session(
goal_id = MANAGER_AGENT_GOAL_ID
route_goal_id = "*" if is_manager_channel(selected_channel) else goal_id
route_key = (route_goal_id, agent_id, selected_channel)
+ if session_id is not None:
+ if not isinstance(session_id, str) or session_id != session_id.strip() or session_id in {".", ".."}:
+ raise ValueError("session_id must retain its exact non-empty opaque identity")
+ session_id = _opaque_id(session_id, field="session_id")
+ if (project_context is not None or steward_context is not None or is_manager_channel(selected_channel)
+ or goal_id is None or agent_goal_id is not None and agent_goal_id != goal_id
+ or conversation_scope(selected_scope)["kind"] != "owner_goal"):
+ raise ValueError("an exact Session opening requires its original local Goal route")
with self.lock:
route_lock = self.session_open_locks.setdefault(route_key, threading.Lock())
- with route_lock:
+ with route_lock, (exclusive_cross_runtime_file_lock(
+ self.store.root / "session-open-locks" / session_id, operation="open_original_goal_session")
+ if session_id is not None else nullcontext()):
+ if session_id is not None:
+ original = read_exact_original_session(self, session_id=session_id, selected_scope=selected_scope,
+ agent_id=agent_id, work_dir=work_dir, requested_model=agent_model_id, requested_effort=agent_reasoning_effort)
+ if original is not None:
+ return original, True
latest = None
if not is_manager_channel(selected_channel) and project_context is None:
exact_attached, strict_profile = select_current_attached_session(
@@ -618,7 +640,7 @@ def open_session(
error_code="source_session_managed_chat_unsupported",
gate=None,
)
- if mode == "resume_latest":
+ if mode == "resume_latest" and session_id is None:
latest = self.store.latest_session(
goal_id=None if is_manager_channel(selected_channel) else goal_id,
agent_id=agent_id,
@@ -674,6 +696,8 @@ def open_session(
objective=objective,
execution_mode=selected_channel.startswith("task."),
project_coordination=conversation_scope(selected_scope)["kind"] == "owner_goal",
+ check_preparation=managed_agent_id is not None,
+ managed_read_default=managed_agent_id is not None,
manager_runtime=manager_runtime,
project_context=project_context,
executor_model=alloc.manager_executor_model(manager_executor_allocation),
@@ -686,6 +710,7 @@ def open_session(
require_managed_agent_session(self, selected_scope, work_dir)
observed_settings = self._observed_model_settings(adapter, agent_reasoning_effort, agent_model_id)
persisted = self.store.create_session(
+ **({"session_id": session_id} if session_id is not None else {}),
goal_id=goal_id,
goal_instance_id=goal_instance_id,
**({"managed_agent_id": managed_agent_id, "managed_workspace_path": str(work_dir.resolve())} if managed_agent_id is not None else {}),
@@ -1020,6 +1045,7 @@ def check_route_configuration():
execution_mode=str(session.get("channel_id") or "").startswith("task."),
project_coordination=conversation_scope(session)["kind"] == "owner_goal",
loopx_tools=session.get("loopx_tools") is True,
+ managed_read_default=session.get("managed_agent_id") is not None,
executor_model=(alloc.manager_executor_model(model_allocation)
if model_allocation is not None else alloc.restored_executor_model(session)),
manager_runtime=manager_runtime,
@@ -1169,6 +1195,7 @@ def submit_turn(
loopx_execution: bool = False,
loopx_request: dict[str, object] | None = None,
_goal_commit: ManagedSessionGoalCommit | None = None,
+ display_message: str | None = None,
) -> tuple[dict[str, Any], bool]:
commit = _goal_commit or nullcontext
session = self.store.load_session(session_id)
@@ -1189,16 +1216,9 @@ def submit_turn(
if parse_native_goal_command(message) is not None:
validate_goal_chat(session, attachments)
if session.get("session_mode") == CHAT_SESSION_MODE_ATTACHED:
- if attachments:
- raise ValueError("attached host session queue does not yet accept attachments")
return enqueue_attached_agent_turn(
- store=self.store,
- registry_path=self.registry_path,
- session_id=session_id,
- client_turn_id=client_turn_id,
- message=message,
- origin="web",
- )
+ store=self.store, registry_path=self.registry_path, session_id=session_id,
+ client_turn_id=client_turn_id, message=message, origin="web", attachments=attachments)
with self._session_adapter_lock(session_id):
session = self.store.load_session(session_id)
if session is None or session.get("status") == "closed":
@@ -1236,7 +1256,7 @@ def submit_turn(
"wake": "成员结果已验收,继续推进 LoopX 模式。",
}.get(str((loopx_request or {}).get("operation")), "恢复 LoopX 模式。")
if loopx_execution
- else None
+ else display_message
),
loopx_execution=loopx_execution,
loopx_request=loopx_request,
@@ -1534,6 +1554,7 @@ def resume_session_queue(
return
if session_id in self.session_queue_workers:
self.session_queue_wakeups.add(session_id)
+ self.turn_release_wakeup.notify()
return
worker = threading.Thread(
target=self._drain_session_queue,
@@ -1548,123 +1569,13 @@ def resume_session_queue(
self.session_queue_threads[session_id] = worker
worker.start()
- def _drain_session_queue(
- self,
- *,
- session_id: str,
- work_dir: Path,
- objective: str,
- ) -> None:
- try:
- while not self.closed.is_set():
- session = self.store.load_session(session_id)
- if (
- self.closed.is_set()
- or session is None
- or session.get("status") == "closed"
- or session.get("session_mode") == CHAT_SESSION_MODE_ATTACHED
- ):
- return
- turn = None
- active_turn_id = str(session.get("active_turn_id") or "")
- if active_turn_id:
- try:
- self.recover_exited_turn(session_id, active_turn_id)
- except OSError:
- # Storage can still be unavailable. Retry only the
- # non-executing lifecycle repair, never the old input.
- self.closed.wait(0.25)
- continue
- active = self.store.load_turn(session_id, active_turn_id)
- if (active and active.get("status") == "queued" and "_acceptance" not in active
- and active.get("started_at") is None and active.get("upstream_turn_id") is None
- and active.get("delta_count") == 0):
- # A crash can leave the queue claim before native
- # dispatch. Keep that exact claim and pass through the
- # normal preparation and execution fences below.
- turn = active
- else:
- with self.lock:
- attached = self.adapters.get(session_id)
- if attached is None or not attached.healthcheck():
- try:
- self._ensure_adapter(
- session,
- work_dir=work_dir,
- objective=objective,
- )
- except Exception as exc:
- self._fail_queue_preparation(session_id, active_turn_id, exc)
- else:
- continue
- active = self.store.load_turn(session_id, active_turn_id)
- if active and active.get("status") not in TERMINAL_TURN_STATES:
- self.closed.wait(0.05)
- continue
- self.store.release_active_turn(session_id, active_turn_id)
- refreshed = self.store.load_session(session_id)
- if refreshed is None:
- return
- with self.lock:
- self.session_queue_wakeups.discard(session_id)
- if turn is None:
- turn = self.store.claim_next_queued_turn(session_id)
- if turn is None:
- with self.lock:
- if session_id in self.session_queue_wakeups:
- continue
- worker = self.session_queue_threads.get(session_id)
- if worker is threading.current_thread():
- self.session_queue_workers.discard(session_id)
- self.session_queue_threads.pop(session_id, None)
- return
- turn_id = str(turn["turn_id"])
- try:
- if turn.get("origin") == "peer" or "collaboration_request" in turn:
- from .capabilities.native_chat.conversation_bindings import OriginalConversationRequests
- OriginalConversationRequests(self).validate_turn(session_id, turn)
- # An empty queue needs no provider preparation. Claim real
- # work first, keeping the existing enqueue/retirement fence
- # above and the Turn's execution/stop checks below.
- current = self.store.load_session(session_id)
- if current is None or current.get("active_turn_id") != turn_id:
- return
- adapter = self._ensure_adapter(
- current,
- work_dir=work_dir,
- objective=objective,
- )
- if (self.store.load_session(session_id) or {}).get("active_turn_id") != turn_id:
- return
- except Exception as exc:
- # The claimed request still owns a terminal outcome when
- # workspace preparation or adapter restoration fails.
- self._fail_queue_preparation(session_id, turn_id, exc)
- continue
- done_event = threading.Event()
- with self.lock:
- self.turn_done_events[(session_id, turn_id)] = done_event
- try:
- self._run_turn(
- session_id=session_id,
- turn_id=turn_id,
- message=str(turn.get("message") or ""),
- attachments=turn.get("attachments") or [],
- adapter=adapter,
- done_event=done_event,
- )
- except OSError:
- # _run_turn has released its execution fence/done event.
- # A failed error checkpoint must not kill this queue worker;
- # the next iteration reconciles the original durable state.
- self.closed.wait(0.25)
- finally:
- with self.lock:
- worker = self.session_queue_threads.get(session_id)
- if worker is threading.current_thread():
- self.session_queue_workers.discard(session_id)
- self.session_queue_threads.pop(session_id, None)
- self.session_queue_wakeups.discard(session_id)
+ def resume_creation_requests(self) -> None:
+ from .capabilities.native_chat.creation_queue import resume_creation_requests
+ resume_creation_requests(self)
+
+ def _drain_session_queue(self, *, session_id: str, work_dir: Path, objective: str) -> None:
+ from .capabilities.native_chat.session_queue import drain_session_queue
+ drain_session_queue(self, session_id=session_id, work_dir=work_dir, objective=objective)
def _fail_queue_preparation(
self, session_id: str, turn_id: str, error: Exception,
@@ -2484,6 +2395,7 @@ def resume_session(self, *, session_id: str, work_dir: Path, objective: str) ->
def close(self) -> None:
self.closed.set()
+ self.turn_release_wakeup.notify()
with self.lock:
adapters = list(self.adapters.values())
event_buffers = list(self.turn_event_buffers.values())
diff --git a/loopx/chat_server.py b/loopx/chat_server.py
index 18151c789d..ab9a4425ea 100644
--- a/loopx/chat_server.py
+++ b/loopx/chat_server.py
@@ -953,31 +953,6 @@ def _session_snapshot(self, session_id: str) -> None:
except OSError:
self._send_error("Chat history is temporarily unavailable", status=503)
- def _managed_native_turn(self, session_id: str, *, execute: bool = False) -> None:
- from .capabilities.native_chat.steward_managed_mode import ManagedSessionTurn
- service = ManagedSessionTurn(self.server.runtime_controller)
- try:
- if execute:
- body = self._read_json()
- if set(body) - {"agent_id", "operation_id", "todo_id"}:
- raise ValueError("unsupported managed Turn arguments")
- turn, created = service.submit(session_id, **body)
- return self._send_json({"ok": True, "session_id": session_id, "turn": turn, "created": created}, status=202)
- query = parse_qs(urlparse(self.path).query, keep_blank_values=True)
- sources = query.get("calendar_task_sources", ["false"])
- if sources not in (["true"], ["false"]):
- raise ValueError("calendar_task_sources must be one true or false value")
- include_sources = sources == ["true"]
- if include_sources and (set(query) - {"calendar_task_sources", "agent_id", "operation_id"}
- or any(len(values) != 1 for values in query.values())):
- raise ValueError("select one original managed Session and Agent for Task sources")
- self._send_json(service.inspect(session_id, agent_id=query.get("agent_id", [None])[0],
- operation_id=query.get("operation_id", [None])[0], include_calendar_task_sources=include_sources))
- except KeyError:
- self._send_error("chat session was not found", status=404)
- except (TypeError, ValueError, RuntimeError) as exc:
- self._send_error(str(exc), status=409, error_code="managed_turn_unavailable")
-
def _list_sessions(self) -> None:
query = parse_qs(urlparse(self.path).query, keep_blank_values=True)
if "managed_agent_id" in query:
@@ -2032,6 +2007,9 @@ def _wake_goal_context(session: dict[str, object]) -> dict[str, object]:
server.delegation_wake_service = DelegationWakeService(
server.runtime_controller, goal_context=_wake_goal_context
).start()
+ # Resume only retained, explicitly confirmed creation requests through the
+ # existing Session queue. Its owner rechecks source, native work and gates.
+ server.runtime_controller.resume_creation_requests()
url = f"http://{host}:{port}{DEFAULT_CHAT_PATH}"
print(f"Serving LoopX Chat at {url}", flush=True)
print(f"Agent boundary: local adapters, project grant {project_workspace_grant}, approval policy never", flush=True)
diff --git a/loopx/cli.py b/loopx/cli.py
index bc31a8c02f..8df03d42e9 100644
--- a/loopx/cli.py
+++ b/loopx/cli.py
@@ -117,6 +117,7 @@
handle_support_control_command,
handle_task_lease_command,
handle_authority_shadow_command,
+ handle_postgres_command,
handle_version_command,
handle_usage_ping_command,
handle_host_mode_plan_command,
@@ -128,6 +129,7 @@
register_canary_commands,
register_coordination_shadow_command,
register_authority_archive_command,
+ register_postgres_commands,
register_capability_commands,
register_doctor_command,
register_dreaming_commands,
@@ -809,6 +811,16 @@ def _main(argv: list[str] | None = None) -> int:
if authority_shadow_result is not None:
return authority_shadow_result
+ postgres_result = handle_postgres_command(
+ args,
+ registry_path=registry_path,
+ runtime_root_arg=args.runtime_root,
+ output_format=output_format,
+ print_payload=print_payload,
+ )
+ if postgres_result is not None:
+ return postgres_result
+
shared_goal_alignment_result = handle_shared_goal_alignment_command(
args,
registry_path=registry_path,
diff --git a/loopx/cli_commands/__init__.py b/loopx/cli_commands/__init__.py
index ec347f4467..72ae204ec6 100644
--- a/loopx/cli_commands/__init__.py
+++ b/loopx/cli_commands/__init__.py
@@ -46,6 +46,7 @@ def _load_exports() -> None:
)
from .canary import handle_canary_command, register_canary_commands
from .authority_archive import handle_authority_archive_command, register_authority_archive_command
+ from .postgres import handle_postgres_command, register_postgres_commands
from .coordination_shadow import (
handle_coordination_shadow_command,
register_coordination_shadow_command,
@@ -210,6 +211,7 @@ def _load_exports() -> None:
"handle_dash_command",
"handle_project_lifecycle_command",
"handle_project_command",
+ "handle_postgres_command",
"handle_pr_review_command",
"handle_deepresearch_command",
"handle_quota_command",
@@ -243,6 +245,7 @@ def _load_exports() -> None:
"register_canary_commands",
"register_coordination_shadow_command",
"register_authority_archive_command",
+ "register_postgres_commands",
"register_capability_commands",
"register_extension_commands",
"register_doctor_command",
diff --git a/loopx/cli_commands/postgres.py b/loopx/cli_commands/postgres.py
new file mode 100644
index 0000000000..ef8e64ea12
--- /dev/null
+++ b/loopx/cli_commands/postgres.py
@@ -0,0 +1,178 @@
+"""CLI commands for PostgreSQL authority administration, schema/role setup, and genesis seed export."""
+from __future__ import annotations
+
+import argparse
+import os
+from collections.abc import Callable
+from pathlib import Path
+
+from ..control_plane.effect_runtime import effect_runtime_result
+from ..paths import resolve_runtime_root
+from ..control_plane.projects.registry_codec import load_registry
+
+
+def register_postgres_commands(
+ subparsers: argparse._SubParsersAction[argparse.ArgumentParser],
+ add_subcommand_format: Callable[[argparse.ArgumentParser], None],
+) -> None:
+ parser = subparsers.add_parser(
+ "postgres",
+ help="PostgreSQL authority schema setup, restricted role assembly, and genesis seed export.",
+ )
+ add_subcommand_format(parser)
+ actions = parser.add_subparsers(dest="postgres_action", required=True)
+
+ # 1. setup: Admin PG Schema and restricted role assembly
+ setup_parser = actions.add_parser(
+ "setup",
+ help="Explicitly assemble PostgreSQL schema, tables, RLS policies, metadata and optional restricted role.",
+ )
+ setup_parser.add_argument(
+ "--database-url",
+ default=os.environ.get("LOOPX_POSTGRES_URL", ""),
+ help="PostgreSQL connection string (or LOOPX_POSTGRES_URL).",
+ )
+ setup_parser.add_argument(
+ "--store-identity",
+ help="Optional explicit store identity (postgresql:<32 lowercase hex>). Generated if omitted.",
+ )
+ setup_parser.add_argument(
+ "--runtime-role",
+ help="Optional runtime role to provision within the schema transaction.",
+ )
+ setup_parser.add_argument(
+ "--execute",
+ action="store_true",
+ help="Execute the schema and role setup against the database.",
+ )
+
+ # 2. local-to-pg-seed / seed: Read-only local-to-pg-seed genesis snapshot sanitizer and exporter
+ for seed_cmd in ("local-to-pg-seed", "seed"):
+ seed_parser = actions.add_parser(
+ seed_cmd,
+ help="Export a local goal into a sanitized cursor=1 genesis seed (stripping expired leases, paths, .local/, Feishu IDs) and optionally seed target PG.",
+ )
+ seed_parser.add_argument("--goal-id", required=True, help="Goal ID to export.")
+ seed_parser.add_argument(
+ "--tenant-id",
+ default="default",
+ help="Target tenant ID for the PostgreSQL store (default: default).",
+ )
+ seed_parser.add_argument(
+ "--database-url",
+ default=os.environ.get("LOOPX_POSTGRES_URL", ""),
+ help="Optional target PostgreSQL connection string for direct seeding.",
+ )
+ seed_parser.add_argument(
+ "--store-identity",
+ help="Optionally pin the already installed target PostgreSQL store identity.",
+ )
+ seed_parser.add_argument(
+ "--output",
+ type=Path,
+ help="Optional file to create or reuse if it contains the identical seed JSON.",
+ )
+ seed_parser.add_argument(
+ "--execute",
+ action="store_true",
+ help="Execute the seed commit to target PostgreSQL; otherwise read-only preview/export.",
+ )
+
+
+def handle_postgres_command(
+ args: argparse.Namespace,
+ *,
+ registry_path: Path,
+ runtime_root_arg: str | None,
+ print_payload: Callable[[dict[str, object], str, Callable[[dict[str, object]], str]], None],
+ output_format: Callable[..., str],
+) -> int | None:
+ if args.command != "postgres":
+ return None
+
+ action = args.postgres_action
+ if action == "setup":
+ database_url = args.database_url or os.environ.get("LOOPX_POSTGRES_URL", "")
+ if not database_url:
+ payload = {
+ "status": "failed",
+ "reason": "--database-url or LOOPX_POSTGRES_URL environment variable is required for setup",
+ }
+ print_payload(payload, output_format(args), lambda v: f"Postgres setup: failed\n{v.get('reason')}")
+ return 1
+
+ if not args.execute:
+ payload = {
+ "status": "dry_run",
+ "schema_version": "loopx_postgresql_authority_store_v1",
+ "database_url_configured": bool(database_url),
+ "store_identity": args.store_identity or "",
+ "runtime_role": args.runtime_role or None,
+ "note": "Pass --execute to actually install schema and assemble roles in PostgreSQL",
+ }
+ print_payload(payload, output_format(args), lambda v: f"Postgres setup (dry-run):\n{v}")
+ return 0
+
+ request = {
+ "databaseUrl": database_url,
+ "storeIdentity": args.store_identity,
+ "runtimeRole": args.runtime_role,
+ }
+ try:
+ result = effect_runtime_result(
+ "coordination.postgresql.setup", request, timeout=120.0, retry_safe=False
+ )
+ except Exception as error:
+ result = {"status": "failed", "reason": str(error)}
+
+ print_payload(
+ result,
+ output_format(args),
+ lambda v: (
+ f"PostgreSQL setup: {v.get('status')}\n"
+ f"Schema version: {v.get('schema_version')}\n"
+ f"Store identity: {v.get('store_identity')}\n"
+ f"Runtime role: {v.get('runtime_role')}\n"
+ f"{v.get('reason', '')}"
+ ),
+ )
+ return 0 if result.get("status") == "installed" else 1
+
+ elif action in ("local-to-pg-seed", "seed"):
+ registry = load_registry(registry_path)
+ resolved_root = str(resolve_runtime_root(registry, runtime_root_arg, registry_path=registry_path))
+
+ database_url = args.database_url or os.environ.get("LOOPX_POSTGRES_URL", "")
+ output_path = str(args.output.expanduser().absolute()) if args.output else None
+
+ request = {
+ "runtimeRoot": resolved_root,
+ "goalId": args.goal_id,
+ "tenantId": args.tenant_id,
+ "outputPath": output_path,
+ "databaseUrl": database_url if args.execute else None,
+ "storeIdentity": args.store_identity,
+ "execute": args.execute,
+ }
+ try:
+ result = effect_runtime_result(
+ "coordination.postgresql.seed", request, timeout=180.0, retry_safe=False
+ )
+ except Exception as error:
+ result = {"status": "failed", "reason": str(error)}
+
+ print_payload(
+ result,
+ output_format(args),
+ lambda v: (
+ f"PostgreSQL genesis seed: {v.get('status')}\n"
+ f"Goal: {v.get('goal_id')} (tenant: {v.get('tenant_id')})\n"
+ f"Cursor: {v.get('cursor')} (revision: {v.get('provider_revision')})\n"
+ f"Seed digest: {v.get('seed_digest')}\n"
+ f"Output: {v.get('output_path') or 'memory'}\n"
+ f"{v.get('reason', '')}"
+ ),
+ )
+ return 0 if result.get("status") in ("exported", "seeded") else 1
+
+ return None
diff --git a/loopx/cli_commands/quota.py b/loopx/cli_commands/quota.py
index 213e79a511..0516351b53 100644
--- a/loopx/cli_commands/quota.py
+++ b/loopx/cli_commands/quota.py
@@ -612,6 +612,13 @@ def _record_non_guard_quota_event(
),
details=rollout_details,
allow_failed=args.quota_command == "should-run",
+ idempotency_fields=(
+ ["goal_id", "event_kind", "agent_id", "run_id"]
+ + (["todo_id"] if rollout_todo_id else [])
+ + (["goal_ref"] if goal_ref is not None else [])
+ if args.quota_command == "spend-slot" and spend_turn_instance_id
+ else None
+ ),
)
if args.quota_command == "spend-slot" and spend_turn_instance_id:
assert context is not None # A settlement requires a prepared authority context.
diff --git a/loopx/cli_commands/quota_action_selection.py b/loopx/cli_commands/quota_action_selection.py
index 7b59cfc3cb..52e4be56e0 100644
--- a/loopx/cli_commands/quota_action_selection.py
+++ b/loopx/cli_commands/quota_action_selection.py
@@ -5,6 +5,7 @@
from dataclasses import dataclass
from pathlib import Path
+from ..control_plane.quota.effective_action import EffectiveAction
from ..control_plane.quota.error_codes import (
HeartbeatReceiptIdentityConflictError,
QuotaActionSelectionConflictError,
@@ -166,6 +167,10 @@ def _requested_quota_action_selection_preflight(
"current selected Todo: explicitly requested Todo differs"
)
return None
+ if payload.get("effective_action") == EffectiveAction.UNSETTLED_HOST_TURN_RECOVERY.value:
+ # Recovery preempts the choice. Keep the exact recovery actions in the
+ # response; the rollout owner leaves this Turn unbound until reentry.
+ return None
interaction = payload.get("interaction_contract")
agent_channel = (
interaction.get("agent_channel") if isinstance(interaction, Mapping) else None
diff --git a/loopx/cli_commands/quota_monitor_poll.py b/loopx/cli_commands/quota_monitor_poll.py
index 030e6c353e..49b58f44b8 100644
--- a/loopx/cli_commands/quota_monitor_poll.py
+++ b/loopx/cli_commands/quota_monitor_poll.py
@@ -13,6 +13,7 @@
find_heartbeat_receipt,
heartbeat_receipt_settlement_todo_id,
)
+from ..control_plane.scheduler.arbitration import monitor_quiet_stall_observation_required
from ..control_plane.scheduler.execution_context import (
SchedulerExecutionContextResolution,
)
@@ -201,10 +202,9 @@ def record_automatic_heartbeat_stall(
turn_instance_id=turn_id,
goal_ref=goal_ref,
)
- if (
- payload.get("effective_action")
- != EffectiveAction.MONITOR_QUIET_SKIP.value
- and existing_stall is None
+ if existing_stall is None and (
+ payload.get("effective_action") != EffectiveAction.MONITOR_QUIET_SKIP.value
+ or not monitor_quiet_stall_observation_required(payload)
):
return payload, status_payload, cache_metadata, "not_applicable"
from ..capabilities.explore.composition_frontier import (
diff --git a/loopx/cli_commands/quota_scheduler_followup.py b/loopx/cli_commands/quota_scheduler_followup.py
index a8ebbe64a1..8bbe506bba 100644
--- a/loopx/cli_commands/quota_scheduler_followup.py
+++ b/loopx/cli_commands/quota_scheduler_followup.py
@@ -228,6 +228,7 @@ def build_scheduler_followup_payload(
failed_rrule=args.failed_rrule,
observed_host_rrule=observed_rrule,
failure_kind=args.failure_kind,
+ turn_instance_id=turn_instance_id,
)
return record_quota_scheduler_ack(
status_payload,
@@ -248,4 +249,5 @@ def build_scheduler_followup_payload(
scheduler_execution_context=scheduler_context,
operator_inbox_urgency_projector=operator_inbox_urgency_projector,
before_decision=before_decision,
+ turn_instance_id=turn_instance_id,
)
diff --git a/loopx/cli_commands/turn.py b/loopx/cli_commands/turn.py
index 7bbf6e0205..8f5f220dfa 100644
--- a/loopx/cli_commands/turn.py
+++ b/loopx/cli_commands/turn.py
@@ -20,7 +20,6 @@
build_loopx_turn_plan,
load_loopx_turn_plan_from_journal,
)
-from .turn_decision import build_fresh_turn_decision_owner
from .turn_inspection import handle_turn_journal_inspection
from .turn_registration import register_turn_commands as register_turn_commands
from .turn_rendering import (
@@ -310,6 +309,8 @@ def execution_preparer(decision: Mapping[str, Any]) -> str | None:
from ..capabilities.explore.turn_context import turn_context_projector
def read_decision_owner():
+ from .turn_decision import build_fresh_turn_decision_owner
+
return build_fresh_turn_decision_owner(
args,
registry_path=registry_path,
diff --git a/loopx/cli_commands/turn_decision.py b/loopx/cli_commands/turn_decision.py
index 1c36db7f4f..edc067f02b 100644
--- a/loopx/cli_commands/turn_decision.py
+++ b/loopx/cli_commands/turn_decision.py
@@ -358,6 +358,7 @@ def current_turn_host_launch_decision(
args: argparse.Namespace, *, registry_path: Path, runtime_root: Path,
identity: SettlementIdentity,
planned_effective_action: object,
+ planned_selected_work_basis: str | None,
scheduler_execution_context: Mapping[str, Any],
operator_inbox_urgency_projector: Callable[..., dict[str, Any]],
turn_start_hook_dispatch: Mapping[str, Any] | None = None,
@@ -416,7 +417,7 @@ def current_turn_host_launch_decision(
plan = build_loopx_turn_plan(
current_contract, host=args.host, execution_mode=args.execution_mode,
scheduler_owner=args.scheduler_owner, turn_instance_id=identity.turn_instance_id,
- goal_ref=goal_ref,
+ goal_ref=goal_ref, include_selected_work_basis=planned_selected_work_basis is not None,
)
route = plan["route"]
if route.get("would_invoke_host") is not True:
@@ -425,6 +426,8 @@ def current_turn_host_launch_decision(
raise FirstPartyHostRuntimeRejected("current_turn_selection_changed")
if (identity.replan_obligation_id or identity.request_subject is not None) and plan["transaction"]["settlement_plan"]["identity"] != identity.as_dict():
raise FirstPartyHostRuntimeRejected("current_turn_selection_changed")
+ if planned_selected_work_basis is not None and plan.get("_selected_work_basis") != planned_selected_work_basis:
+ raise FirstPartyHostRuntimeRejected("current_turn_work_changed")
return launch
diff --git a/loopx/cli_commands/turn_run_once.py b/loopx/cli_commands/turn_run_once.py
index b93a5df82f..fa28ea033e 100644
--- a/loopx/cli_commands/turn_run_once.py
+++ b/loopx/cli_commands/turn_run_once.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import argparse
+from functools import partial
import sys as _sys
from collections.abc import Callable, Mapping
from pathlib import Path
@@ -767,18 +768,24 @@ def scheduler(_spend_payload: dict[str, object]) -> dict[str, object]:
host_runner: Callable[[Mapping[str, Any]], dict[str, Any]] | None = None
session_binding_resolver = None
- if args.host == "codex-app-server":
- def native_launch_decision(current_registry: Path) -> Mapping[str, Any]:
- return current_turn_host_launch_decision(args, registry_path=current_registry,
- runtime_root=runtime_root, identity=settlement_identity,
- planned_effective_action=execution_context["effective_action"],
- scheduler_execution_context=payload.get("scheduler_execution_context") or {},
- operator_inbox_urgency_projector=operator_inbox_urgency_projector,
- turn_start_hook_dispatch=turn_start_hook_dispatch, goal_ref=goal_ref)
+ def current_launch_decision(current_registry: Path) -> Mapping[str, Any]:
+ # Native and stdio transports recheck the original work at launch.
+ # A global route checks its source even if the mirror is unchanged.
+ return current_turn_host_launch_decision(
+ args, registry_path=current_registry, runtime_root=runtime_root,
+ identity=settlement_identity,
+ planned_effective_action=execution_context["effective_action"],
+ planned_selected_work_basis=execution_context.get("selected_work_basis"),
+ scheduler_execution_context=payload.get("scheduler_execution_context") or {},
+ operator_inbox_urgency_projector=operator_inbox_urgency_projector,
+ turn_start_hook_dispatch=turn_start_hook_dispatch, goal_ref=goal_ref,
+ )
+
+ if args.host == "codex-app-server":
native_admission = (goal_admission or FirstPartyHostGoalAdmission.for_plan(
registry_path=registry_path, goal_id=args.goal_id, planned_goal_ref=goal_ref,
- )).with_launch_decision(native_launch_decision)
+ )).with_launch_decision(current_launch_decision)
def run_native_host(request: Mapping[str, Any]) -> dict[str, Any]:
return native_host_runner(request, lambda: native_admission.launch_lifetime(
@@ -787,22 +794,6 @@ def run_native_host(request: Mapping[str, Any]) -> dict[str, Any]:
host_runner = run_native_host
session_binding_resolver = native_session_binding_resolver
elif args.host == "codex-cli":
-
- def current_launch_decision(current_registry: Path) -> Mapping[str, Any]:
- # Revalidate the already admitted identity through the same
- # owner. In a global route the source is checked first, even
- # when its mirror still advertises old quota or health.
- return current_turn_host_launch_decision(
- args, registry_path=current_registry, runtime_root=runtime_root,
- identity=settlement_identity,
- planned_effective_action=execution_context["effective_action"],
- scheduler_execution_context=payload.get("scheduler_execution_context") or {},
- operator_inbox_urgency_projector=operator_inbox_urgency_projector,
- # Keep this Turn's already dispatched required reads at launch.
- turn_start_hook_dispatch=turn_start_hook_dispatch,
- goal_ref=goal_ref,
- )
-
host_runner = build_codex_cli_host_runner(
args, runtime_root=runtime_root, project=project,
registry_path=registry_path, goal_ref=goal_ref,
@@ -872,6 +863,11 @@ def on_managed_start_admitted() -> None:
semantic_replan_guard_scoped=replan_guard_scoped,
semantic_replan_obligation_id=replan_obligation_id,
semantic_replan_capability_guard=capability_guard,
+ quota_spend_source=(
+ quota_spend_source_for_execution_context(payload.get("scheduler_execution_context"))
+ if settlement_identity.replan_obligation_id or settlement_identity.request_subject is not None
+ else None
+ ),
goal_ref=goal_ref,
)
@@ -919,6 +915,10 @@ def on_managed_start_admitted() -> None:
admit_start=managed_cadence.admit if args.execute else None,
confirm_start=managed_cadence.confirm if args.execute else None,
goal_admission=goal_admission,
+ host_launch_guard=(partial(
+ goal_admission.with_launch_decision(current_launch_decision).launch_lifetime,
+ operation="turn_external_host_launch",
+ ) if raw_argv is not None and goal_admission is not None else None),
)
if args.execute:
result.update(drain_committed_turn_post_writeback_hooks(
diff --git a/loopx/cli_runtime.py b/loopx/cli_runtime.py
index 70edc94748..217d412541 100644
--- a/loopx/cli_runtime.py
+++ b/loopx/cli_runtime.py
@@ -62,6 +62,7 @@
"sync-global",
"uninstall-project",
"version",
+ "postgres",
"host-mode-plan",
"migrate-local-state",
}
@@ -77,7 +78,7 @@
"todo", "quota", "task-lease", "change-window", "delegation", "turn", "doctor", "commands", "version",
"bootstrap", "connect", "configure-goal", "start-goal", "bootstrap-command-pack",
"register-agent", "bind-agent-thread",
- "authority-archive", "coordination-shadow", "extension", "slash-commands",
+ "authority-archive", "postgres", "coordination-shadow", "extension", "slash-commands",
"manager-inbox",
"agent-context", "agent-directory", "agent-capabilities",
"handoff", "handoff-mode",
@@ -492,6 +493,10 @@ def _build_selected_parser(command: str) -> LoopXArgumentParser:
from .cli_commands.authority_archive import register_authority_archive_command
register_authority_archive_command(subparsers, add_subcommand_format)
+ elif command == "postgres":
+ from .cli_commands.postgres import register_postgres_commands
+
+ register_postgres_commands(subparsers, add_subcommand_format)
elif command == "coordination-shadow":
from .cli_commands.coordination_shadow import register_coordination_shadow_command
@@ -665,6 +670,13 @@ def _dispatch_common_command(
args, registry_path=registry_path, runtime_root_arg=args.runtime_root,
output_format=output_format, print_payload=print_payload,
)
+ if args.command == "postgres":
+ from .cli_commands.postgres import handle_postgres_command
+
+ return handle_postgres_command(
+ args, registry_path=registry_path, runtime_root_arg=args.runtime_root,
+ output_format=output_format, print_payload=print_payload,
+ )
if args.command == "coordination-shadow":
from .cli_commands.coordination_shadow import handle_coordination_shadow_command
diff --git a/loopx/control_plane/collaboration/steward_managed_mode.ts b/loopx/control_plane/collaboration/steward_managed_mode.ts
index 947ea1c2a3..2a9fa3fc40 100644
--- a/loopx/control_plane/collaboration/steward_managed_mode.ts
+++ b/loopx/control_plane/collaboration/steward_managed_mode.ts
@@ -1,18 +1,21 @@
-/** Source-bound preparation of one native execution identity for a steward.
+/** Source-bound managed Session identity and native host policy.
*
- * This owner prepares/binds identity only. Goal bootstrap, Agent registration,
- * scheduling and model execution retain their existing owners. In particular,
- * a prepared identity never advertises a running monitor or an enabled host.
+ * Identity preparation grants no execution. Host policy maps only the original
+ * already-admitted Task and existing approval. Goal bootstrap, registration,
+ * scheduling and execution retain their owners; a prepared identity never
+ * advertises a running monitor or enabled host.
*/
import { createHash } from "node:crypto";
+import {posix, win32} from "node:path";
import type { JsonObject } from "../runtime_decode.ts";
-import { requireJsonObject, requireNonEmptyString } from "../runtime_decode.ts";
+import { jsonObject, requireJsonObject, requireNonEmptyString } from "../runtime_decode.ts";
import { EffectRuntimeRequestError } from "../effect_runtime_errors.ts";
import { ENVELOPED_SHA256_PATTERN } from "../content_digest.ts";
import {boundConversationExecutorMatches, goalAgentConversationIdentity, resolveConversationScope} from "./conversation_scope.ts";
import {readTurnSelectionRejection} from "../turn_driver/selection_rejection.ts";
import {EffectiveAction} from "../quota/effective_action.generated.ts";
-import {turnPlanWorkSource} from "../quota/turn_envelope.ts";
+import {projectTurnBoundary, turnPlanWorkSource} from "../quota/turn_envelope.ts";
+import {projectGoalCreationExecutionRequest} from "../goals/creation_execution.ts";
/** A stopped observation may get one new Chat carrier, never a new native
* observation identity. The host independently reads the old worker lock,
@@ -278,6 +281,7 @@ export function admitManagedSessionTurn(params: JsonObject): JsonObject {
const reportWork = request.work_kind === "report";
const scheduledWork = request.work_kind === "scheduled_task";
const requestFields = ["operation", "host", "actor", "turn_instance_id", "todo_id", "validation_command", "work_kind",
+ ...(request.creation_request === undefined ? [] : ["creation_request"]),
...(observationOnly ? ["monitor_deadline", "monitor_lease_version", "monitor_restart_recovery"] : []),
...(reportWork ? ["report_window_id", "report_subscription_revision", "report_quota_recovery"] : []),
...(scheduledWork ? ["schedule_window_id", "schedule_subscription_revision"] : [])];
@@ -288,6 +292,18 @@ export function admitManagedSessionTurn(params: JsonObject): JsonObject {
|| request.todo_id !== null && (typeof request.todo_id !== "string" || !/^todo_[A-Za-z0-9_-]{1,100}$/.test(request.todo_id))) {
throw new EffectRuntimeRequestError("managed work uses the native task's existing validation, without caller commands");
}
+ if (request.creation_request !== undefined) {
+ const original = projectGoalCreationExecutionRequest({proposal: params.creation_proposal,
+ session, goal, current_original_task: params.current_original_task});
+ const retained = requireJsonObject(request.creation_request, "original creation execution request");
+ if (request.work_kind !== "native_turn" || original === null
+ || Object.keys(retained).length !== Object.keys(original).length
+ || Object.entries(original).some(([key, value]) => JSON.stringify(retained[key]) !== JSON.stringify(value))
+ || request.todo_id !== original.todo_id
+ || request.turn_instance_id !== `managed-session.${original.operation_id}`) {
+ throw new EffectRuntimeRequestError("native execution must retain its exact confirmed original creation request");
+ }
+ }
if (observationOnly) {
const monitor = requireJsonObject(params.monitor_todo, "original managed Session monitor");
if (monitor.target_key !== `managed-session-watch.${session.session_id}`
@@ -356,8 +372,13 @@ export function admitManagedSessionTurn(params: JsonObject): JsonObject {
throw new EffectRuntimeRequestError("scheduled work requires its original explicit task, validation, Session and calendar window");
}
}
+ const creation = jsonObject(request.creation_request);
+ const queuedCreation = turn !== null && creation !== null && turn.status === "queued"
+ && turn.started_at === null && turn.upstream_turn_id === null && turn.delta_count === 0
+ && !Object.hasOwn(turn, "_acceptance")
+ && turn.client_turn_id === `managed.${creation.operation_id}`;
if (turn ? turn.session_id !== session.session_id || turn.turn_id !== session.active_turn_id
- || !["starting", "running"].includes(String(turn.status)) || turn.origin !== "native_loopx"
+ || !(["starting", "running"].includes(String(turn.status)) || queuedCreation) || turn.origin !== "native_loopx"
|| turn.loopx_execution !== true || JSON.stringify(turn.loopx_request) !== JSON.stringify(request)
: session.active_turn_id || params.queued_turn_count !== 0) {
throw new EffectRuntimeRequestError("the original conversation is busy or no longer owns this Turn; retain its current work");
@@ -732,3 +753,48 @@ export function bindStewardManagedActor(params: JsonObject): JsonObject {
goal_id: current.goalId, goal_instance_id: goal.goal_instance_id, agent_id: AGENT_ID,
session_id: current.sessionId, source_ref: current.source, creation_operation_id: current.creation});
}
+
+/** Adapt a current, already admitted native Task to the host's existing
+ * sandbox vocabulary. A Task selection or check confirmation grants no writes.
+ * Only the existing whole-workspace approval is expressible here. The current
+ * Task lease cannot enlarge it. Narrower scopes remain read-only; the native
+ * transport pins the same workspace roots in the guarded Task-start frame.
+ */
+export function canonicalNativeTaskSandboxPolicy(params: JsonObject): JsonObject {
+ const actor = requireJsonObject(params.actor, "original native Task actor");
+ const authority = requireJsonObject(params.authority, "admitted Task authority");
+ const validation = requireJsonObject(params.validation, "current Task validation plan");
+ const identity = requireJsonObject(authority._work_identity, "checked original work identity");
+ const preparation = requireJsonObject(authority.execution_preparation, "original Task execution preparation");
+ if (!(params.host === "codex-app-server" && actor.schema_version === "loopx_managed_session_actor_v0"
+ && typeof actor.workspace_path === "string" && actor.workspace_path.length > 0
+ && identity.goal_id === actor.goal_id && identity.agent_id === actor.agent_id
+ && identity.todo_id === validation.todo_id && preparation.todo_id === validation.todo_id
+ && preparation.agent_id === actor.agent_id && preparation.state === "ready"
+ && preparation.delivery_allowed === true && authority.should_run === true
+ && authority.normal_delivery_allowed === true && validation.state === "ready")) {
+ throw new EffectRuntimeRequestError("native host policy requires the original admitted Task, validation and execution lease");
+ }
+ const approval = jsonObject(projectTurnBoundary(authority).checkpointed_boundary_authority);
+ const readonly = {type: "readOnly", networkAccess: false};
+ if (approval?.schema_version !== "checkpointed_boundary_authority_v0"
+ || !Array.isArray(approval.active_write_scope) || approval.active_write_scope.length === 0) return readonly;
+ const lease = requireJsonObject(params.task_lease, "current original Task write lease");
+ if (lease.schema_version !== "task_lease_v0" || lease.status !== "active"
+ || lease.goal_id !== actor.goal_id || lease.todo_id !== validation.todo_id || lease.owner !== actor.agent_id
+ || !Number.isSafeInteger(lease.version) || Number(lease.version) < 1
+ || !Array.isArray(lease.write_scopes)) {
+ throw new EffectRuntimeRequestError("native Task writes require the same current execution lease");
+ }
+ const paths = actor.workspace_path.startsWith("/") ? posix : win32;
+ if (!paths.isAbsolute(actor.workspace_path)) {
+ throw new EffectRuntimeRequestError("native filesystem policy requires the original absolute workspace");
+ }
+ const workspace = paths.resolve(actor.workspace_path);
+ // Empty legacy inventories retain their already explicit Goal-wide grant.
+ // Fresh writable creations declare the reviewed scope on their original Task.
+ if (!approval.active_write_scope.includes("**")
+ || lease.write_scopes.length > 0 && !lease.write_scopes.includes("**")) return readonly;
+ return {type: "workspaceWrite", writableRoots: [workspace], networkAccess: false,
+ excludeTmpdirEnvVar: true, excludeSlashTmp: true};
+}
diff --git a/loopx/control_plane/coordination/authority_backup.ts b/loopx/control_plane/coordination/authority_backup.ts
index ef12c15d79..d8aee72cdd 100644
--- a/loopx/control_plane/coordination/authority_backup.ts
+++ b/loopx/control_plane/coordination/authority_backup.ts
@@ -8,7 +8,7 @@ import {requireJsonObject} from "../runtime_decode.ts";
import {isRecoveryDigest, type RecoveryComponentState} from "../state_recovery_contract.ts";
import {canonicalAuthoritySha256, requireAuthorityStoreId} from "./authority_store_codec.ts";
import {exportAuthorityArchive} from "./authority_archive.ts";
-import {withVerifiedAuthorityArchive} from "./authority_archive_read.ts";
+import {archiveCursor, withVerifiedAuthorityArchive} from "./authority_archive_read.ts";
import {canonicalTodoCollection} from "./local_authority_read.ts";
import {decodeLocalAuthoritySelection, localAuthorityProviderPaths, openRuntimeAuthorityStore,
type LocalAuthorityProviderDependencies} from "./local_authority_provider.ts";
@@ -120,6 +120,7 @@ export function decodeAuthorityBackupCapture(value: unknown): JsonObject {
}
const archive = requireJsonObject(row.archive, "authority archive summary");
const binding = requireJsonObject(row.dependencies, "authority capture dependencies");
+ archiveCursor(archive.commits);
if (archive.goal_id !== goalId || !isRecoveryDigest(archive.archive_sha256) ||
!Object.hasOwn(binding, "selector_sha256") || !Object.hasOwn(binding, "writer_fence_sha256") ||
Object.values(binding).some(value => value !== null && !isRecoveryDigest(value))) {
@@ -147,6 +148,10 @@ export async function inspectAuthorityBackup(dataRoot: string, row: JsonObject):
// then supplies its private verified copy to the Goal read-model owner.
return withVerifiedAuthorityArchive(join(directory, "authority.jsonl"), String(expected.archive_sha256), async archive => {
if (canonicalAuthoritySha256({...archive.summary}) !== canonicalAuthoritySha256(expected)) throw new Error("authority capture summary mismatch");
+ const commitCount = Number(archive.summary.commits);
+ if (!Number.isSafeInteger(commitCount) || String(commitCount) !== archive.summary.commits) {
+ throw new Error("authority history count cannot be represented in the recovery report");
+ }
const selector = await sourceBytes(join(directory, "selector.json"));
if (selector !== null) {
const selection = decodeLocalAuthoritySelection(JSON.parse(selector.toString("utf8")), goalId);
@@ -160,7 +165,7 @@ export async function inspectAuthorityBackup(dataRoot: string, row: JsonObject):
const collection = canonicalTodoCollection(head, goalId, true);
return {provider: archive.summary.source_provider, head_digest: archive.summary.projection_sha256,
provider_revision: archive.summary.source_provider_revision, cursor: archive.summary.commits,
- history_commits: Number(archive.summary.commits), todo_count: collection.projection.todo_ids.length,
+ history_commits: commitCount, todo_count: collection.projection.todo_ids.length,
historical_lease_count: collection.leaseIndex?.lease_todo_ids.length ?? 0,
capture_started_at: row.started_at, capture_completed_at: row.completed_at};
});
diff --git a/loopx/control_plane/coordination/authority_state_log.ts b/loopx/control_plane/coordination/authority_state_log.ts
index 154c6e113b..4d39784211 100644
--- a/loopx/control_plane/coordination/authority_state_log.ts
+++ b/loopx/control_plane/coordination/authority_state_log.ts
@@ -206,7 +206,7 @@ export class AuthorityStateReplay {
this.#state = canonicalAuthorityObject(projection, "authority replay state");
}
- apply(delta: AuthorityStateDelta): void {
+ apply(delta: unknown): void {
let next = this.#state;
// Decode before applying and publish only after the whole batch succeeds.
// A rejected suffix cannot leave a half-applied replay frontier.
diff --git a/loopx/control_plane/coordination/postgresql_authority_seed.ts b/loopx/control_plane/coordination/postgresql_authority_seed.ts
new file mode 100644
index 0000000000..e41aed182e
--- /dev/null
+++ b/loopx/control_plane/coordination/postgresql_authority_seed.ts
@@ -0,0 +1,645 @@
+/**
+ * Administrative PostgreSQL schema setup, restricted role assembly, and
+ * local-to-pg-seed genesis snapshot sanitizer and exporter.
+ *
+ * Requirements:
+ * 1. Administrator-explicit PG schema and restricted role assembly.
+ * 2. Read-only local-to-pg-seed snapshot sanitization (cursor=1 clean seed):
+ * - Strips expired leases
+ * - Strips local absolute paths (/Users/..., /home/..., /private/...)
+ * - Strips .local/ paths and private material references
+ * - Strips Feishu/Lark user/chat/app/union IDs (ou_*, oc_*, cli_*, on_*)
+ * - Fail-closed if any privacy/credential pattern remains.
+ */
+import { randomBytes, randomUUID } from "node:crypto";
+import { constants } from "node:fs";
+import { link, lstat, open, unlink } from "node:fs/promises";
+import { dirname, join } from "node:path";
+import type { JsonObject } from "../effect_program.ts";
+import {
+ canonicalAuthorityObject,
+ canonicalAuthoritySha256,
+ isAuthorityJsonObject,
+ requireAuthorityStoreId,
+} from "./authority_store_codec.ts";
+import {
+ POSTGRESQL_RUNTIME_ROLE_PATTERN,
+ POSTGRESQL_SCHEMA_VERSION,
+ POSTGRESQL_STORE_IDENTITY_PATTERN,
+ installPostgreSqlAuthorityStoreSchema,
+ PostgreSqlAuthorityStore,
+ type PostgreSqlAuthorityDatabase,
+ type PostgreSqlAuthorityConnection,
+} from "./postgresql_authority_store.ts";
+import { syncAuthorityDirectory } from "./file_authority_store.ts";
+import { openLocalAuthorityStoreHandle } from "./local_authority_provider.ts";
+
+export const PG_GENESIS_SEED_SCHEMA_VERSION = "loopx_pg_genesis_seed_v0";
+
+// Privacy token / ID patterns
+const FEISHU_ID_PATTERNS = [
+ /(?]+/g,
+];
+
+const SECRET_PATTERNS = [
+ /\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9_]{36,255}\b/,
+ /\b(?:sk|pk)_(?:live|test)_[0-9a-zA-Z]{24,}\b/,
+ /\bsk-[a-zA-Z0-9_-]{20,}\b/,
+ /\bAKIA[0-9A-Z]{16}\b/,
+ /\bBearer\s+[A-Za-z0-9\-._~+/]+=*\b/i,
+ /\b(?:password|passwd|secret|api_key|access_token|private_key)\s*[:=]\s*["']?[^\s"']{8,}["']?/i,
+];
+
+export interface GenesisSanitizationReport {
+ leases_stripped: number;
+ private_materials_stripped: number;
+ paths_redacted: number;
+ feishu_ids_redacted: number;
+ todos_sanitized: number;
+}
+
+export class GenesisPrivacyBoundaryViolation extends Error {
+ readonly reasonCode = "genesis_privacy_boundary_violation";
+ constructor(message: string) {
+ super(message);
+ this.name = "GenesisPrivacyBoundaryViolation";
+ }
+}
+
+export function sanitizeText(
+ text: string,
+ report: GenesisSanitizationReport,
+): string {
+ let result = text;
+
+ // Redact Feishu IDs
+ for (const pattern of FEISHU_ID_PATTERNS) {
+ result = result.replace(pattern, () => {
+ report.feishu_ids_redacted += 1;
+ return "[feishu_id_redacted]";
+ });
+ }
+
+ // Redact local absolute paths in text
+ for (const pattern of LOCAL_PATH_PATTERNS) {
+ result = result.replace(pattern, () => {
+ report.paths_redacted += 1;
+ return "[local_path_redacted]";
+ });
+ }
+
+ // Remove .local/ references
+ if (isLocalReference(result)) {
+ result = result.replace(/\.local[\\/][^\s)"']*/g, () => {
+ report.paths_redacted += 1;
+ return "[local_scope_redacted]";
+ });
+ }
+
+ return result;
+}
+
+export function sanitizeObjectRecursively(
+ val: unknown,
+ report: GenesisSanitizationReport,
+): unknown {
+ if (typeof val === "string") {
+ return sanitizeText(val, report);
+ }
+ if (Array.isArray(val)) {
+ return val.map((item) => sanitizeObjectRecursively(item, report));
+ }
+ if (val !== null && typeof val === "object") {
+ return Object.fromEntries(Object.entries(val).map(([key, value]) =>
+ [key, sanitizeObjectRecursively(value, report)]));
+ }
+ return val;
+}
+
+function isLocalReference(value: string): boolean {
+ return /\.local[\\/]/.test(value);
+}
+
+function matchesRedaction(pattern: RegExp, value: string): boolean {
+ // search starts at zero even for the global patterns used by replace.
+ return value.search(pattern) !== -1;
+}
+
+export function scanForbiddenPrivacyTokens(value: unknown, path = "projection"): void {
+ if (typeof value === "string") {
+ for (const pattern of SECRET_PATTERNS) {
+ if (pattern.test(value)) {
+ throw new GenesisPrivacyBoundaryViolation(
+ `Privacy boundary violation at ${path}: matched sensitive credential pattern`,
+ );
+ }
+ }
+ if (isLocalReference(value)) {
+ throw new GenesisPrivacyBoundaryViolation(
+ `Privacy boundary violation at ${path}: contains raw unredacted .local/ reference`,
+ );
+ }
+ for (const pattern of LOCAL_PATH_PATTERNS) {
+ if (matchesRedaction(pattern, value)) {
+ throw new GenesisPrivacyBoundaryViolation(
+ `Privacy boundary violation at ${path}: contains raw unredacted local absolute path`,
+ );
+ }
+ }
+ for (const pattern of FEISHU_ID_PATTERNS) {
+ if (matchesRedaction(pattern, value)) {
+ throw new GenesisPrivacyBoundaryViolation(
+ `Privacy boundary violation at ${path}: contains raw unredacted Feishu ID`,
+ );
+ }
+ }
+ return;
+ }
+
+ if (Array.isArray(value)) {
+ for (let i = 0; i < value.length; i++) {
+ scanForbiddenPrivacyTokens(value[i], `${path}[${i}]`);
+ }
+ return;
+ }
+
+ if (value !== null && typeof value === "object") {
+ for (const [index, [key, field]] of Object.entries(value).entries()) {
+ // A field name can itself be a private ID or secret. Never echo it.
+ const location = path + "{" + index + "}";
+ scanForbiddenPrivacyTokens(key, location);
+ scanForbiddenPrivacyTokens(field, location);
+ }
+ }
+}
+
+/**
+ * Sanitizes a projection for genesis PG export.
+ * Resets metadata, strips expired leases, sanitizes paths and Feishu IDs,
+ * strips private materials, and runs a fail-closed boundary scan.
+ */
+export function sanitizeGenesisSeedProjection(rawProjection: JsonObject): {
+ projection: JsonObject;
+ report: GenesisSanitizationReport;
+} {
+ const report: GenesisSanitizationReport = {
+ leases_stripped: 0,
+ private_materials_stripped: 0,
+ paths_redacted: 0,
+ feishu_ids_redacted: 0,
+ todos_sanitized: 0,
+ };
+
+ const projection = structuredClone(rawProjection);
+
+ // 1. Strip expired leases (support both leases array and task_leases object/array)
+ const nowMs = Date.now();
+ if (Array.isArray(projection.leases)) {
+ const originalLeaseCount = projection.leases.length;
+ const retainedLeases = (projection.leases as JsonObject[]).filter((lease) => {
+ if (!lease || typeof lease !== "object") return false;
+ const expiresAt = lease.expires_at;
+ if (typeof expiresAt === "number" && expiresAt <= nowMs) return false;
+ if (typeof expiresAt === "string") {
+ const parsed = Date.parse(expiresAt);
+ if (!isNaN(parsed) && parsed <= nowMs) return false;
+ }
+ return true;
+ });
+ projection.leases = retainedLeases;
+ report.leases_stripped += originalLeaseCount - retainedLeases.length;
+ }
+ if (projection.task_leases && typeof projection.task_leases === "object") {
+ const taskLeases = projection.task_leases as JsonObject;
+ if (taskLeases.leases && typeof taskLeases.leases === "object" && !Array.isArray(taskLeases.leases)) {
+ const remaining: Record = Object.create(null);
+ for (const [k, v] of Object.entries(taskLeases.leases as Record)) {
+ if (!v || typeof v !== "object") continue;
+ const expiresAt = v.expires_at;
+ if (typeof expiresAt === "number" && expiresAt <= nowMs) {
+ report.leases_stripped += 1;
+ continue;
+ }
+ if (typeof expiresAt === "string") {
+ const parsed = Date.parse(expiresAt);
+ if (!isNaN(parsed) && parsed <= nowMs) {
+ report.leases_stripped += 1;
+ continue;
+ }
+ }
+ remaining[k] = v;
+ }
+ taskLeases.leases = remaining;
+ }
+ }
+
+ // 2. Strip private materials (support project_materials and registered_materials)
+ for (const matKey of ["project_materials", "registered_materials"] as const) {
+ if (Array.isArray(projection[matKey])) {
+ const originalMaterialCount = (projection[matKey] as JsonObject[]).length;
+ projection[matKey] = (
+ projection[matKey] as JsonObject[]
+ ).filter((material) => {
+ if (!material || typeof material !== "object") return false;
+ return !privateMaterial(material);
+ });
+ report.private_materials_stripped +=
+ originalMaterialCount - (projection[matKey] as JsonObject[]).length;
+ }
+ }
+
+ // The direction registry, scope digest and usage receipts are one lineage.
+ // A redacted registry cannot retain source attestations or be re-signed as
+ // original evidence in a synthetic seed.
+ const registry = projection.authority_registry;
+ if (isAuthorityJsonObject(registry) && isAuthorityJsonObject(registry.project_materials)) {
+ const materials = Object.values(registry.project_materials);
+ const removed = materials.filter(row => isAuthorityJsonObject(row) && privateMaterial(row)).length;
+ const sanitizedRegistry = sanitizeObjectRecursively(registry, {...report});
+ if (removed > 0 || canonicalAuthoritySha256(sanitizedRegistry) !== canonicalAuthoritySha256(registry)) {
+ report.private_materials_stripped += removed;
+ delete projection.authority_registry;
+ delete projection.goal_direction_scope;
+ delete projection.material_usage_receipts;
+ }
+ }
+
+ // 3. Sanitize Todos (required_write_scopes, text, title, note, evidence)
+ if (Array.isArray(projection.todos)) {
+ for (const todo of projection.todos as JsonObject[]) {
+ if (!todo || typeof todo !== "object") continue;
+ report.todos_sanitized += 1;
+
+ // Sanitize required_write_scopes: strip .local/ and make paths relative
+ if (Array.isArray(todo.required_write_scopes)) {
+ todo.required_write_scopes = (
+ todo.required_write_scopes as string[]
+ )
+ .filter((scope) => typeof scope === "string" && !isLocalReference(scope))
+ .map((scope) => {
+ // Strip machine-specific user prefix
+ for (const pattern of LOCAL_PATH_PATTERNS) {
+ if (matchesRedaction(pattern, scope)) {
+ report.paths_redacted += 1;
+ // Convert e.g. /Users/.../loopx/packages/foo -> packages/foo
+ const parts = scope.split("/");
+ const loopxIdx = parts.indexOf("loopx");
+ if (loopxIdx >= 0 && loopxIdx < parts.length - 1) {
+ return parts.slice(loopxIdx + 1).join("/");
+ }
+ return parts[parts.length - 1] ?? "";
+ }
+ }
+ return scope;
+ });
+ }
+ }
+ }
+
+ // 4. Sanitize topic_authority and direction
+ if (Array.isArray(projection.topic_authority)) {
+ projection.topic_authority = (
+ projection.topic_authority as JsonObject[]
+ ).filter((topic) => {
+ if (!topic || typeof topic !== "object") return false;
+ const boundary = String(topic.boundary ?? "");
+ return boundary !== "local_private" && boundary !== "private";
+ });
+ }
+
+ // 5. Recursively sanitize all strings (Feishu IDs, local paths, .local references)
+ const sanitized = sanitizeObjectRecursively(projection, report) as JsonObject;
+
+ // 6. Fail-closed scan: any remaining credential or sensitive token triggers immediate failure
+ scanForbiddenPrivacyTokens(sanitized, "projection");
+
+ return { projection: sanitized, report };
+}
+
+function privateMaterial(material: JsonObject): boolean {
+ const boundary = String(material.boundary ?? material.visibility ?? "");
+ return ["local_private", "private", "restricted"].includes(boundary) ||
+ isLocalReference(String(material.source_url ?? material.url ?? "")) ||
+ isLocalReference(String(material.source_path ?? material.path ?? ""));
+}
+
+export interface PostgreSqlAdminSetupOptions {
+ database?: PostgreSqlAuthorityDatabase;
+ databaseUrl?: string;
+ storeIdentity?: string;
+ runtimeRole?: string;
+}
+
+export interface PostgreSqlAdminSetupResult {
+ status: "installed" | "failed";
+ schema_version: string;
+ store_identity: string;
+ runtime_role: string | null;
+ reason?: string;
+}
+
+async function resolveDatabase(options: {
+ database?: PostgreSqlAuthorityDatabase;
+ targetDatabase?: PostgreSqlAuthorityDatabase;
+ databaseUrl?: string;
+}): Promise<{ database: PostgreSqlAuthorityDatabase; close: () => Promise } | null> {
+ const db = options.database ?? options.targetDatabase;
+ if (db) {
+ return { database: db, close: async () => {} };
+ }
+ if (options.databaseUrl) {
+ const { Pool } = await import("pg");
+ const pool = new Pool({ connectionString: options.databaseUrl });
+ const database: PostgreSqlAuthorityDatabase = {
+ connect: async () => {
+ const client = await pool.connect();
+ return {
+ query: async (text: string, values?: readonly unknown[]) =>
+ await client.query(text, values ? [...values] : undefined),
+ release: (error?: Error) => client.release(error),
+ };
+ },
+ };
+ return { database, close: async () => await pool.end() };
+ }
+ return null;
+}
+
+/**
+ * Administrative explicit schema and restricted role assembly.
+ */
+export async function setupPostgreSqlAuthoritySchemaAndRole(
+ databaseOrOptions: PostgreSqlAuthorityDatabase | PostgreSqlAdminSetupOptions,
+ maybeOptions?: PostgreSqlAdminSetupOptions,
+): Promise {
+ const options: PostgreSqlAdminSetupOptions =
+ databaseOrOptions && "connect" in databaseOrOptions
+ ? { ...maybeOptions, database: databaseOrOptions as PostgreSqlAuthorityDatabase }
+ : (databaseOrOptions as PostgreSqlAdminSetupOptions);
+
+ const storeIdentity =
+ options.storeIdentity ??
+ `postgresql:${randomBytes(16).toString("hex").toLowerCase()}`;
+
+ if (!POSTGRESQL_STORE_IDENTITY_PATTERN.test(storeIdentity)) {
+ return {
+ status: "failed",
+ schema_version: POSTGRESQL_SCHEMA_VERSION,
+ store_identity: storeIdentity,
+ runtime_role: options.runtimeRole ?? null,
+ reason: "PostgreSQL store identity must match postgresql:<32 lowercase hex>",
+ };
+ }
+
+ const role = options.runtimeRole?.trim();
+ if (role !== undefined && !POSTGRESQL_RUNTIME_ROLE_PATTERN.test(role)) {
+ return {
+ status: "failed",
+ schema_version: POSTGRESQL_SCHEMA_VERSION,
+ store_identity: storeIdentity,
+ runtime_role: null,
+ reason: "runtime role must be a valid lowercase SQL identifier",
+ };
+ }
+
+ const resolved = await resolveDatabase(options);
+ if (!resolved) {
+ return {
+ status: "failed",
+ schema_version: POSTGRESQL_SCHEMA_VERSION,
+ store_identity: storeIdentity,
+ runtime_role: options.runtimeRole ?? null,
+ reason: "PostgreSQL database or databaseUrl must be provided for schema setup",
+ };
+ }
+
+ try {
+ await installPostgreSqlAuthorityStoreSchema(resolved.database, storeIdentity, role);
+
+ return {
+ status: "installed",
+ schema_version: POSTGRESQL_SCHEMA_VERSION,
+ store_identity: storeIdentity,
+ runtime_role: role ?? null,
+ };
+ } catch (error) {
+ return {
+ status: "failed",
+ schema_version: POSTGRESQL_SCHEMA_VERSION,
+ store_identity: storeIdentity,
+ runtime_role: options.runtimeRole ?? null,
+ reason: error instanceof Error ? error.message : String(error),
+ };
+ } finally {
+ await resolved.close();
+ }
+}
+
+export interface LocalToPgSeedExportOptions {
+ runtimeRoot: string;
+ goalId: string;
+ tenantId?: string;
+ outputPath?: string;
+ localStore?: import("./authority_store.ts").AuthorityStore;
+ targetDatabase?: PostgreSqlAuthorityDatabase;
+ databaseUrl?: string;
+ storeIdentity?: string;
+ execute?: boolean;
+}
+
+export interface LocalToPgSeedExportResult {
+ status: "exported" | "seeded" | "failed";
+ schema_version: string;
+ goal_id: string;
+ tenant_id: string;
+ cursor: string;
+ provider_revision: string;
+ operation_id: string;
+ seed_digest?: string;
+ sanitization_report?: GenesisSanitizationReport;
+ output_path?: string;
+ reason?: string;
+ seed_payload?: JsonObject;
+}
+
+class GenesisSeedOutputConflict extends Error {
+ constructor() {
+ super("Genesis seed output must be absent or an identical regular file");
+ }
+}
+
+async function existingSeedMatches(path: string, expected: Buffer): Promise {
+ let declared;
+ try {
+ declared = await lstat(path);
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return false;
+ throw error;
+ }
+ if (!declared.isFile() || declared.size !== expected.length) throw new GenesisSeedOutputConflict();
+ const file = await open(path, constants.O_RDONLY | (constants.O_NONBLOCK ?? 0) | (constants.O_NOFOLLOW ?? 0));
+ try {
+ const stat = await file.stat();
+ if (!stat.isFile() || stat.dev !== declared.dev || stat.ino !== declared.ino) throw new GenesisSeedOutputConflict();
+ let offset = 0;
+ for await (const part of file.createReadStream({autoClose: false})) {
+ if (!part.equals(expected.subarray(offset, offset + part.length))) {
+ throw new GenesisSeedOutputConflict();
+ }
+ offset += part.length;
+ }
+ if (offset !== expected.length) throw new GenesisSeedOutputConflict();
+ return true;
+ } finally {
+ await file.close();
+ }
+}
+
+async function writeSeedOutput(path: string, bytes: Buffer): Promise {
+ const directory = dirname(path);
+ if (!await existingSeedMatches(path, bytes)) {
+ // Publish a complete private file without replacing a concurrent writer's
+ // output. A concurrent replay may reuse the winner's identical bytes.
+ const temporary = join(directory, `.loopx-seed-${randomUUID()}.partial`);
+ const file = await open(temporary, "wx", 0o600);
+ try {
+ try {
+ await file.writeFile(bytes);
+ await file.sync();
+ } finally {
+ await file.close();
+ }
+ try {
+ await link(temporary, path);
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code !== "EEXIST" ||
+ !await existingSeedMatches(path, bytes)) throw error;
+ }
+ } finally {
+ await unlink(temporary);
+ }
+ }
+ await syncAuthorityDirectory(directory);
+}
+
+/**
+ * Exports a local authority store into a sanitized genesis seed (cursor=1).
+ * When targetDatabase and execute=true are provided, atomically commits into PG.
+ */
+export async function exportLocalToPgGenesisSeed(
+ options: LocalToPgSeedExportOptions,
+): Promise {
+ const tenantId = options.tenantId ?? "default";
+ const operationId = `${options.goalId}-genesis-seed`;
+ const base = {
+ schema_version: PG_GENESIS_SEED_SCHEMA_VERSION,
+ goal_id: options.goalId,
+ tenant_id: tenantId,
+ cursor: "1",
+ provider_revision: "1",
+ operation_id: operationId,
+ };
+ let scopeValid = false;
+ const failed = (reason: string): LocalToPgSeedExportResult =>
+ ({...base, ...(!scopeValid ? {goal_id: "", tenant_id: "", operation_id: ""} : {}), status: "failed", reason});
+
+ try {
+ for (const value of [options.goalId, tenantId, operationId]) {
+ requireAuthorityStoreId(value, "genesis scope id");
+ scanForbiddenPrivacyTokens(value, "genesis scope");
+ }
+ scopeValid = true;
+ if (options.execute && !options.targetDatabase && !options.databaseUrl) {
+ return failed("Executing a genesis seed requires a target PostgreSQL database");
+ }
+ if (options.storeIdentity !== undefined &&
+ !POSTGRESQL_STORE_IDENTITY_PATTERN.test(options.storeIdentity)) {
+ return failed("Target PostgreSQL store identity is invalid");
+ }
+
+ const store = options.localStore ??
+ (await openLocalAuthorityStoreHandle(options.runtimeRoot, options.goalId, {}, {existingOnly: true})).store;
+ if (store.providerKind === "postgresql") return failed("Genesis export requires a local File or SQLite source");
+ const loaded = await store.loadAuthority();
+ if (loaded.status !== "loaded") return failed("Local authority head is not available");
+
+ const {projection, report} = sanitizeGenesisSeedProjection(loaded.head);
+ const seedDigest = canonicalAuthoritySha256(projection);
+ const seedPayload: JsonObject = {
+ ...base, seed_digest: seedDigest, sanitization_report: {...report}, projection,
+ };
+ const exported = {
+ ...base, seed_digest: seedDigest, sanitization_report: report,
+ output_path: options.outputPath, seed_payload: seedPayload,
+ };
+ if (options.outputPath) {
+ await writeSeedOutput(options.outputPath, Buffer.from(JSON.stringify(seedPayload, null, 2)));
+ }
+ // Export never opens the target or installs anything in either store.
+ if (!options.execute) return {...exported, status: "exported"};
+
+ const resolved = await resolveDatabase(options);
+ if (!resolved) return failed("Target PostgreSQL database is not available for execution");
+ try {
+ // Discover the provisioned incarnation if the caller did not pin one.
+ // Subsequent writes check this identity inside the commit's rotation lock.
+ const scope = {tenant_id: tenantId, goal_id: options.goalId};
+ const identity = await new PostgreSqlAuthorityStore(resolved.database, {
+ ...scope, expected_store_identity: options.storeIdentity,
+ }).storeIdentity();
+ if (identity.status !== "available") return failed("Target PostgreSQL store identity is unavailable or mismatched");
+ const pgStore = new PostgreSqlAuthorityStore(resolved.database, {
+ ...scope, expected_store_identity: identity.store_identity,
+ });
+
+ const original = async () => {
+ const receipt = await pgStore.readReceipt(operationId);
+ if (receipt.status !== "found" || receipt.cursor !== "1" || receipt.receipts.length !== 1) return null;
+ const record = receipt.receipts[0];
+ return record.schema_version === "genesis_seed_receipt_v0" &&
+ record.source_goal_id === options.goalId && record.seed_digest === seedDigest ? receipt : null;
+ };
+ const existing = await pgStore.loadAuthority();
+ if (existing.status === "loaded") {
+ const receipt = await original();
+ return receipt ? {...exported, status: "seeded", provider_revision: receipt.provider_revision}
+ : failed("Target PostgreSQL goal already has a different committed authority");
+ }
+ if (existing.status !== "missing") return failed("Target PostgreSQL authority state is unavailable");
+
+ const committed = await pgStore.commitAuthority({
+ operation_id: operationId,
+ expected_provider_revision: null,
+ events: [],
+ receipts: [{
+ schema_version: "genesis_seed_receipt_v0",
+ source: "local-to-pg-seed",
+ source_goal_id: options.goalId,
+ seed_digest: seedDigest,
+ sanitization_report: {...report},
+ }],
+ next_projection: projection,
+ });
+ if (committed.status === "applied") {
+ return {...exported, status: "seeded", provider_revision: committed.provider_revision};
+ }
+ if (committed.status === "ambiguous" || committed.status === "conflict") {
+ const receipt = await original();
+ if (receipt) return {...exported, status: "seeded", provider_revision: receipt.provider_revision};
+ if (committed.status === "ambiguous") {
+ return failed("PostgreSQL seed outcome is unknown; retry the same seed to resolve the original receipt");
+ }
+ }
+ return failed(`PostgreSQL genesis seed was not applied (${committed.status})`);
+ } finally {
+ await resolved.close();
+ }
+ } catch (error) {
+ return failed(error instanceof GenesisPrivacyBoundaryViolation || error instanceof GenesisSeedOutputConflict
+ ? error.message : "Genesis seed could not be prepared or written");
+ }
+}
diff --git a/loopx/control_plane/coordination/postgresql_authority_store.ts b/loopx/control_plane/coordination/postgresql_authority_store.ts
index 2285ff4f83..d0c7146f79 100644
--- a/loopx/control_plane/coordination/postgresql_authority_store.ts
+++ b/loopx/control_plane/coordination/postgresql_authority_store.ts
@@ -1,12 +1,20 @@
import {AuthorityJournalScan} from "./authority_journal_scan.ts";
import type { JsonObject } from "../effect_program.ts";
+import {
+ authorityStateCheckpointCursor,
+ authorityStateDelta,
+ isAuthorityStateCheckpoint,
+ AuthorityStateReplay,
+} from "./authority_state_log.ts";
import type {
AuthorityStore,
AuthorityStoreCommit,
AuthorityStoreCommitResult,
+ AuthorityStoreCommittedTransaction,
AuthorityStoreIdentityResult,
AuthorityStoreLoadResult,
AuthorityStoreReadFailure,
+ AuthorityStoreReceiptBatchResult,
AuthorityStoreReceiptResult,
AuthorityStoreReceiptSnapshotResult,
AuthorityStoreScanResult,
@@ -16,6 +24,7 @@ import {
canonicalAuthorityBytes,
canonicalAuthorityObject,
canonicalAuthorityObjectList,
+ canonicalAuthoritySha256,
normalizeAuthorityStoreCommit,
parseAuthorityCursor,
requireAuthorityStoreId,
@@ -24,7 +33,9 @@ import {
export const POSTGRESQL_STORE_IDENTITY_PATTERN = /^postgresql:[0-9a-f]{32}$/;
const POSTGRESQL_PROVIDER_REVISION_PATTERN = /^postgresql:([0-9a-f]{32}):([1-9]\d*)$/;
const POSTGRESQL_STORE_IDENTITY_LOCK_KEY = "5522768808912313929";
-export const POSTGRESQL_SCHEMA_VERSION = "loopx_postgresql_authority_store_v0";
+const FULL_PROJECTION_SCHEMA_VERSION = "loopx_postgresql_authority_store_v0";
+export const POSTGRESQL_SCHEMA_VERSION = "loopx_postgresql_authority_store_v1";
+export const POSTGRESQL_RUNTIME_ROLE_PATTERN = /^[a-z][a-z0-9_]{1,62}$/;
export const DEFAULT_POSTGRESQL_MAX_COMMIT_BYTES = 16 * 1024 * 1024;
/**
@@ -48,6 +59,8 @@ export interface PostgreSqlAuthorityDatabase {
export interface PostgreSqlAuthorityStoreOptions {
tenant_id: string;
goal_id: string;
+ /** Bind an explicit target even when the expected Goal revision is empty. */
+ expected_store_identity?: string;
/**
* Maximum canonical bytes admitted for one atomic commit. Deployments may
* lower this envelope; promotion still requires separately measured
@@ -62,15 +75,6 @@ interface HeadRow extends JsonObject {
head: JsonObject | null;
}
-interface TransactionRow extends JsonObject {
- cursor: string;
- provider_revision: string;
- operation_id: string;
- projection: JsonObject;
- events: JsonObject[];
- receipts: JsonObject[];
-}
-
export const POSTGRESQL_AUTHORITY_STORE_SCHEMA_SQL = `
CREATE SCHEMA IF NOT EXISTS loopx_control_plane;
@@ -93,13 +97,28 @@ CREATE TABLE IF NOT EXISTS loopx_control_plane.authority_heads (
(cursor > 0 AND provider_revision > 0 AND head IS NOT NULL))
);
+CREATE TABLE IF NOT EXISTS loopx_control_plane.authority_checkpoints (
+ tenant_id text NOT NULL,
+ goal_id text NOT NULL,
+ cursor bigint NOT NULL CHECK (cursor > 0),
+ projection jsonb NOT NULL,
+ projection_digest text NOT NULL CHECK (length(projection_digest) = 64),
+ created_at timestamptz NOT NULL DEFAULT transaction_timestamp(),
+ PRIMARY KEY (tenant_id, goal_id, cursor),
+ FOREIGN KEY (tenant_id, goal_id)
+ REFERENCES loopx_control_plane.authority_heads (tenant_id, goal_id)
+ ON DELETE CASCADE
+);
+
CREATE TABLE IF NOT EXISTS loopx_control_plane.authority_commits (
tenant_id text NOT NULL,
goal_id text NOT NULL,
cursor bigint NOT NULL CHECK (cursor > 0),
provider_revision bigint NOT NULL CHECK (provider_revision > 0),
operation_id text NOT NULL,
- projection jsonb NOT NULL,
+ delta jsonb,
+ projection jsonb,
+ projection_digest text,
committed_at timestamptz NOT NULL DEFAULT transaction_timestamp(),
PRIMARY KEY (tenant_id, goal_id, cursor),
UNIQUE (tenant_id, goal_id, operation_id),
@@ -134,6 +153,8 @@ CREATE TABLE IF NOT EXISTS loopx_control_plane.authority_receipts (
ALTER TABLE loopx_control_plane.authority_heads ENABLE ROW LEVEL SECURITY;
ALTER TABLE loopx_control_plane.authority_heads FORCE ROW LEVEL SECURITY;
+ALTER TABLE loopx_control_plane.authority_checkpoints ENABLE ROW LEVEL SECURITY;
+ALTER TABLE loopx_control_plane.authority_checkpoints FORCE ROW LEVEL SECURITY;
ALTER TABLE loopx_control_plane.authority_commits ENABLE ROW LEVEL SECURITY;
ALTER TABLE loopx_control_plane.authority_commits FORCE ROW LEVEL SECURITY;
ALTER TABLE loopx_control_plane.authority_events ENABLE ROW LEVEL SECURITY;
@@ -148,6 +169,13 @@ CREATE POLICY authority_heads_tenant_scope
USING (tenant_id = current_setting('loopx.tenant_id', TRUE))
WITH CHECK (tenant_id = current_setting('loopx.tenant_id', TRUE));
+DROP POLICY IF EXISTS authority_checkpoints_tenant_scope
+ ON loopx_control_plane.authority_checkpoints;
+CREATE POLICY authority_checkpoints_tenant_scope
+ ON loopx_control_plane.authority_checkpoints
+ USING (tenant_id = current_setting('loopx.tenant_id', TRUE))
+ WITH CHECK (tenant_id = current_setting('loopx.tenant_id', TRUE));
+
DROP POLICY IF EXISTS authority_commits_tenant_scope
ON loopx_control_plane.authority_commits;
CREATE POLICY authority_commits_tenant_scope
@@ -182,7 +210,11 @@ const SELECT_TRANSACTION_COLUMNS_SQL = `
SELECT commit.cursor::text AS cursor,
commit.provider_revision::text AS provider_revision,
commit.operation_id,
+ commit.delta,
commit.projection,
+ commit.projection_digest,
+ checkpoint.projection AS checkpoint_projection,
+ checkpoint.projection_digest AS checkpoint_digest,
COALESCE((
SELECT jsonb_agg(event.event ORDER BY event.event_index)
FROM loopx_control_plane.authority_events AS event
@@ -198,6 +230,23 @@ SELECT commit.cursor::text AS cursor,
AND receipt.cursor = commit.cursor
), '[]'::jsonb) AS receipts
FROM loopx_control_plane.authority_commits AS commit
+LEFT JOIN loopx_control_plane.authority_checkpoints AS checkpoint
+ ON checkpoint.tenant_id = commit.tenant_id AND checkpoint.goal_id = commit.goal_id
+ AND checkpoint.cursor = commit.cursor
+`;
+
+const SELECT_RECEIPT_COLUMNS_SQL = `
+SELECT commit.cursor::text AS cursor,
+ commit.provider_revision::text AS provider_revision,
+ commit.operation_id,
+ COALESCE((
+ SELECT jsonb_agg(receipt.receipt ORDER BY receipt.receipt_index)
+ FROM loopx_control_plane.authority_receipts AS receipt
+ WHERE receipt.tenant_id = commit.tenant_id
+ AND receipt.goal_id = commit.goal_id
+ AND receipt.cursor = commit.cursor
+ ), '[]'::jsonb) AS receipts
+FROM loopx_control_plane.authority_commits AS commit
`;
function rows(result: { rows: readonly unknown[] }): readonly JsonObject[] {
@@ -240,23 +289,6 @@ function decodeHeadRow(value: JsonObject): HeadRow {
};
}
-function decodeTransactionRow(value: JsonObject): TransactionRow {
- const cursor = requiredRowString(value, "cursor");
- const providerRevision = requiredRowString(value, "provider_revision");
- parseAuthorityCursor(cursor);
- if (!/^[1-9]\d*$/.test(providerRevision)) {
- throw new AuthorityStoreProtocolError("PostgreSQL provider revision is invalid");
- }
- return {
- cursor,
- provider_revision: providerRevision,
- operation_id: requiredRowString(value, "operation_id"),
- projection: canonicalAuthorityObject(value.projection, "transaction projection"),
- events: canonicalAuthorityObjectList(value.events, "transaction events"),
- receipts: canonicalAuthorityObjectList(value.receipts, "transaction receipts"),
- };
-}
-
function providerRevisionToken(storeIdentity: string, revision: string): string {
if (!POSTGRESQL_STORE_IDENTITY_PATTERN.test(storeIdentity)) {
throw new AuthorityStoreProtocolError("PostgreSQL store identity is invalid");
@@ -267,6 +299,16 @@ function providerRevisionToken(storeIdentity: string, revision: string): string
return `${storeIdentity}:${revision}`;
}
+function decodeReceipt(row: JsonObject, storeIdentity: string): Extract {
+ const cursor = requiredRowString(row, "cursor");
+ if (parseAuthorityCursor(cursor) < 1n) {
+ throw new AuthorityStoreProtocolError("PostgreSQL receipt cursor must be positive");
+ }
+ return {status: "found", cursor, provider_revision: providerRevisionToken(
+ storeIdentity, requiredRowString(row, "provider_revision"),
+ ), receipts: canonicalAuthorityObjectList(row.receipts, "receipts")};
+}
+
function parseExpectedRevision(value: string | null): {
store_identity: string;
revision: string;
@@ -372,23 +414,31 @@ async function lockStoreIdentityTransaction(
}
/**
- * Administrative schema installation. Call this only from the authenticated
- * service deployment path with a newly minted database-incarnation identity.
+ * Administrative schema installation and provisioning under one transaction.
+ * A new store uses a new identity. A format upgrade binds the current identity
+ * and keeps the existing journal access available through the checkpoint table.
*/
export async function installPostgreSqlAuthorityStoreSchema(
database: PostgreSqlAuthorityDatabase,
storeIdentity: string,
+ runtimeRole?: string,
): Promise {
if (!POSTGRESQL_STORE_IDENTITY_PATTERN.test(storeIdentity)) {
throw new AuthorityStoreProtocolError(
"PostgreSQL store identity must match postgresql:<32 lowercase hex>",
);
}
+ if (runtimeRole !== undefined && !POSTGRESQL_RUNTIME_ROLE_PATTERN.test(runtimeRole)) {
+ throw new AuthorityStoreProtocolError("runtime role must be a valid lowercase SQL identifier");
+ }
const connection = await database.connect();
let commitStarted = false;
let releaseError: Error | undefined;
try {
await connection.query("BEGIN");
+ // Fence old writers before publishing the first journal format they
+ // cannot read. Rotation and ordinary commits take the same lock.
+ await lockStoreIdentityTransaction(connection, "exclusive");
await connection.query(POSTGRESQL_AUTHORITY_STORE_SCHEMA_SQL);
await connection.query(
`INSERT INTO loopx_control_plane.authority_store_metadata
@@ -404,13 +454,79 @@ export async function installPostgreSqlAuthorityStoreSchema(
FOR UPDATE`,
), "PostgreSQL store metadata");
if (
- metadata === null || metadata.schema_version !== POSTGRESQL_SCHEMA_VERSION ||
- metadata.store_identity !== storeIdentity
+ metadata === null || metadata.store_identity !== storeIdentity ||
+ (metadata.schema_version !== POSTGRESQL_SCHEMA_VERSION &&
+ metadata.schema_version !== FULL_PROJECTION_SCHEMA_VERSION)
) {
throw new AuthorityStoreProtocolError(
"PostgreSQL store identity does not match the installed database incarnation",
);
}
+ if (metadata.schema_version === FULL_PROJECTION_SCHEMA_VERSION) {
+ // The released v0 schema required every projection. A nullable v0
+ // journal is the unsupported pre-release format; do not bless it as v1.
+ const releasedLayout = oneRow(await connection.query(
+ `SELECT attnotnull FROM pg_attribute
+ WHERE attrelid = 'loopx_control_plane.authority_commits'::regclass
+ AND attname = 'projection' AND NOT attisdropped`,
+ ), "PostgreSQL v0 projection layout");
+ if (releasedLayout?.attnotnull !== true) {
+ throw new AuthorityStoreProtocolError("PostgreSQL v0 journal layout is not the released full-projection format");
+ }
+ // The checkpoint is part of the same append-only journal. Preserve
+ // table grants, including group grants, without copying history
+ // mutation privileges or grant options.
+ await connection.query(`
+ DO $migration$
+ DECLARE journal_grant record;
+ BEGIN
+ FOR journal_grant IN
+ SELECT DISTINCT acl.grantee, acl.privilege_type
+ FROM pg_catalog.pg_class AS journal
+ CROSS JOIN LATERAL pg_catalog.aclexplode(journal.relacl) AS acl
+ WHERE journal.oid = 'loopx_control_plane.authority_commits'::regclass
+ AND acl.privilege_type IN ('SELECT', 'INSERT')
+ LOOP
+ EXECUTE pg_catalog.format(
+ 'GRANT %s ON loopx_control_plane.authority_checkpoints TO %s',
+ journal_grant.privilege_type,
+ CASE WHEN journal_grant.grantee = 0 THEN 'PUBLIC'
+ ELSE pg_catalog.quote_ident(pg_catalog.pg_get_userbyid(journal_grant.grantee))
+ END
+ );
+ END LOOP;
+ END
+ $migration$;`);
+ }
+ await connection.query(
+ `ALTER TABLE loopx_control_plane.authority_commits ADD COLUMN IF NOT EXISTS delta jsonb;
+ ALTER TABLE loopx_control_plane.authority_commits ADD COLUMN IF NOT EXISTS projection_digest text;
+ ALTER TABLE loopx_control_plane.authority_commits ALTER COLUMN projection DROP NOT NULL;`,
+ );
+ if (runtimeRole !== undefined) {
+ await connection.query(`
+ DO $role$
+ BEGIN
+ IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = '${runtimeRole}') THEN
+ CREATE ROLE "${runtimeRole}" NOLOGIN;
+ END IF;
+ END
+ $role$;
+ GRANT USAGE ON SCHEMA loopx_control_plane TO "${runtimeRole}";
+ GRANT SELECT ON loopx_control_plane.authority_store_metadata TO "${runtimeRole}";
+ GRANT SELECT, INSERT, UPDATE ON loopx_control_plane.authority_heads TO "${runtimeRole}";
+ GRANT SELECT, INSERT ON
+ loopx_control_plane.authority_checkpoints,
+ loopx_control_plane.authority_commits,
+ loopx_control_plane.authority_events,
+ loopx_control_plane.authority_receipts TO "${runtimeRole}";`);
+ }
+ if (metadata.schema_version === FULL_PROJECTION_SCHEMA_VERSION) {
+ await connection.query(
+ "UPDATE loopx_control_plane.authority_store_metadata SET schema_version = $1 WHERE singleton = TRUE",
+ [POSTGRESQL_SCHEMA_VERSION],
+ );
+ }
commitStarted = true;
await connection.query("COMMIT");
} catch (error) {
@@ -562,6 +678,7 @@ export class PostgreSqlAuthorityStore implements AuthorityStore {
readonly tenantId: string;
readonly goalId: string;
readonly maxCommitBytes: number;
+ readonly #expectedStoreIdentity: string | undefined;
constructor(
database: PostgreSqlAuthorityDatabase,
@@ -570,6 +687,11 @@ export class PostgreSqlAuthorityStore implements AuthorityStore {
this.database = database;
this.tenantId = requireAuthorityStoreId(options.tenant_id, "tenant id");
this.goalId = requireAuthorityStoreId(options.goal_id, "goal id");
+ this.#expectedStoreIdentity = options.expected_store_identity;
+ if (this.#expectedStoreIdentity !== undefined &&
+ !POSTGRESQL_STORE_IDENTITY_PATTERN.test(this.#expectedStoreIdentity)) {
+ throw new AuthorityStoreProtocolError("expected PostgreSQL store identity is invalid");
+ }
this.maxCommitBytes = options.max_commit_bytes ?? DEFAULT_POSTGRESQL_MAX_COMMIT_BYTES;
if (!Number.isSafeInteger(this.maxCommitBytes) || this.maxCommitBytes < 1) {
throw new AuthorityStoreProtocolError(
@@ -582,6 +704,14 @@ export class PostgreSqlAuthorityStore implements AuthorityStore {
return await this.database.connect();
}
+ private async readStoreIdentity(connection: PostgreSqlAuthorityConnection): Promise {
+ const identity = await requireStoreIdentity(connection);
+ if (this.#expectedStoreIdentity !== undefined && identity !== this.#expectedStoreIdentity) {
+ throw new AuthorityStoreProtocolError("PostgreSQL store identity does not match the expected database incarnation");
+ }
+ return identity;
+ }
+
private async readInTenantTransaction(
operation: (connection: PostgreSqlAuthorityConnection) => Promise,
): Promise {
@@ -618,7 +748,7 @@ export class PostgreSqlAuthorityStore implements AuthorityStore {
let releaseError: Error | undefined;
try {
connection = await this.connect();
- return { status: "available", store_identity: await requireStoreIdentity(connection) };
+ return { status: "available", store_identity: await this.readStoreIdentity(connection) };
} catch (error) {
releaseError = asError(error);
const failure = readFailure(error);
@@ -633,7 +763,7 @@ export class PostgreSqlAuthorityStore implements AuthorityStore {
async loadAuthority(): Promise {
try {
return await this.readInTenantTransaction(async (connection) => {
- const storeIdentity = await requireStoreIdentity(connection);
+ const storeIdentity = await this.readStoreIdentity(connection);
const value = oneRow(
await connection.query(SELECT_HEAD_SQL, [this.tenantId, this.goalId]),
"PostgreSQL authority head",
@@ -694,7 +824,7 @@ export class PostgreSqlAuthorityStore implements AuthorityStore {
// Keep the identity lock ahead of the per-Goal head lock. Shared mode
// preserves cross-Goal concurrency while rotation takes exclusive mode.
await lockStoreIdentityTransaction(connection, "shared");
- const storeIdentity = await requireStoreIdentity(connection);
+ const storeIdentity = await this.readStoreIdentity(connection);
await connection.query(
`INSERT INTO loopx_control_plane.authority_heads
(tenant_id, goal_id, provider_revision, cursor, head)
@@ -746,17 +876,40 @@ export class PostgreSqlAuthorityStore implements AuthorityStore {
const nextRevision = (BigInt(current.provider_revision) + 1n).toString();
const nextCursor = (BigInt(current.cursor) + 1n).toString();
+ const isCheckpoint = isAuthorityStateCheckpoint(BigInt(nextCursor));
+ const projectionDigest = canonicalAuthoritySha256(normalized.next_projection);
+
+ if (isCheckpoint) {
+ await connection.query(
+ `INSERT INTO loopx_control_plane.authority_checkpoints
+ (tenant_id, goal_id, cursor, projection, projection_digest)
+ VALUES ($1, $2, $3::bigint, $4::jsonb, $5)`,
+ [
+ this.tenantId,
+ this.goalId,
+ nextCursor,
+ JSON.stringify(normalized.next_projection),
+ projectionDigest,
+ ],
+ );
+ }
+
+ const delta = isCheckpoint
+ ? null
+ : JSON.stringify(authorityStateDelta(current.head ?? {}, normalized.next_projection));
+
await connection.query(
`INSERT INTO loopx_control_plane.authority_commits
- (tenant_id, goal_id, cursor, provider_revision, operation_id, projection)
- VALUES ($1, $2, $3::bigint, $4::bigint, $5, $6::jsonb)`,
+ (tenant_id, goal_id, cursor, provider_revision, operation_id, delta, projection_digest)
+ VALUES ($1, $2, $3::bigint, $4::bigint, $5, $6::jsonb, $7)`,
[
this.tenantId,
this.goalId,
nextCursor,
nextRevision,
normalized.operation_id,
- JSON.stringify(normalized.next_projection),
+ delta,
+ projectionDigest,
],
);
for (const [index, event] of normalized.events.entries()) {
@@ -843,23 +996,17 @@ export class PostgreSqlAuthorityStore implements AuthorityStore {
}
try {
return await this.readInTenantTransaction(async (connection) => {
- const storeIdentity = await requireStoreIdentity(connection);
+ const storeIdentity = await this.readStoreIdentity(connection);
const value = oneRow(await connection.query(
- `${SELECT_TRANSACTION_COLUMNS_SQL}
+ `${SELECT_RECEIPT_COLUMNS_SQL}
WHERE commit.tenant_id = $1 AND commit.goal_id = $2 AND commit.operation_id = $3`,
[this.tenantId, this.goalId, normalized],
), "PostgreSQL authority receipt");
if (value === null) return {status: "snapshot", store_identity: storeIdentity,
receipt: {status: "missing"}} as const;
- const transaction = decodeTransactionRow(value);
return {
status: "snapshot", store_identity: storeIdentity,
- receipt: {
- status: "found",
- cursor: transaction.cursor,
- provider_revision: providerRevisionToken(storeIdentity, transaction.provider_revision),
- receipts: structuredClone(transaction.receipts),
- },
+ receipt: decodeReceipt(value, storeIdentity),
} as const;
});
} catch (error) {
@@ -867,6 +1014,30 @@ export class PostgreSqlAuthorityStore implements AuthorityStore {
}
}
+ async readReceipts(operationIds: readonly string[]): Promise {
+ try {
+ if (!Array.isArray(operationIds) || operationIds.length < 1 || operationIds.length > 64) {
+ throw new AuthorityStoreProtocolError("receipt batch requires 1..64 operations");
+ }
+ for (const id of operationIds) requireAuthorityStoreId(id, "operation id");
+ return await this.readInTenantTransaction(async connection => {
+ const identity = await this.readStoreIdentity(connection);
+ const values = rows(await connection.query(
+ `${SELECT_RECEIPT_COLUMNS_SQL}
+ WHERE commit.tenant_id = $1 AND commit.goal_id = $2 AND commit.operation_id = ANY($3::text[])`,
+ [this.tenantId, this.goalId, [...new Set(operationIds)]],
+ ));
+ const byOperation = new Map(values.map(row => [
+ requiredRowString(row, "operation_id"), decodeReceipt(row, identity),
+ ]));
+ return {status: "receipts", results: operationIds.map(id =>
+ structuredClone(byOperation.get(id) ?? {status: "missing"}))};
+ });
+ } catch (error) {
+ return readFailure(error);
+ }
+ }
+
async scanCommitted(
afterCursor: string | null,
limit: number,
@@ -875,26 +1046,99 @@ export class PostgreSqlAuthorityStore implements AuthorityStore {
if (!(scan instanceof AuthorityJournalScan)) return scan;
try {
return await this.readInTenantTransaction(async (connection) => {
- const storeIdentity = await requireStoreIdentity(connection);
+ const storeIdentity = await this.readStoreIdentity(connection);
const current = oneRow(
await connection.query(SELECT_HEAD_SQL, [this.tenantId, this.goalId]),
"PostgreSQL authority head",
);
if (current === null) return scan.page([], null);
const head = decodeHeadRow(current);
- const snapshot = head.head === null ? null : {cursor: head.cursor,
- provider_revision: providerRevisionToken(storeIdentity, head.provider_revision), head: head.head};
+ const snapshot = head.head === null ? null : {
+ cursor: head.cursor,
+ provider_revision: providerRevisionToken(storeIdentity, head.provider_revision),
+ head: head.head,
+ };
const range = scan.rangeFailure(snapshot?.cursor ?? null);
if (range) return range;
- const result = rows(await connection.query(
- `${SELECT_TRANSACTION_COLUMNS_SQL}
- WHERE commit.tenant_id = $1 AND commit.goal_id = $2 AND commit.cursor > $3::bigint
- ORDER BY commit.cursor
- LIMIT $4`,
- [this.tenantId, this.goalId, scan.offset.toString(), (BigInt(limit) + 1n).toString()],
- )).map(decodeTransactionRow);
- const transactions = result.map(value => ({...value,
- provider_revision: providerRevisionToken(storeIdentity, value.provider_revision)}));
+
+ const headCursorBigInt = BigInt(head.cursor);
+ const fromCursor = scan.offset + 1n;
+ if (fromCursor > headCursorBigInt) {
+ return scan.page([], snapshot);
+ }
+ const requestedCount = BigInt(limit) + 1n;
+ const remaining = headCursorBigInt - scan.offset;
+ const countToFetch = remaining < requestedCount ? remaining : requestedCount;
+ const toCursor = fromCursor + countToFetch - 1n;
+
+ // Fetch at most one checkpoint window before the visible page. Validate
+ // that prefix too: a missing row could otherwise silently change replay.
+ const checkpointCursor = authorityStateCheckpointCursor(fromCursor);
+ const rawCommits = rows(
+ await connection.query(
+ `${SELECT_TRANSACTION_COLUMNS_SQL}
+ WHERE commit.tenant_id = $1 AND commit.goal_id = $2
+ AND commit.cursor >= $3::bigint AND commit.cursor <= $4::bigint
+ ORDER BY commit.cursor`,
+ [this.tenantId, this.goalId, checkpointCursor.toString(), toCursor.toString()],
+ ),
+ );
+
+ if (BigInt(rawCommits.length) !== toCursor - checkpointCursor + 1n) {
+ throw new AuthorityStoreProtocolError("PostgreSQL authority commit window is incomplete");
+ }
+
+ let replay: AuthorityStateReplay | null = null;
+ const transactions: AuthorityStoreCommittedTransaction[] = [];
+ for (const [index, raw] of rawCommits.entries()) {
+ const cursor = requiredRowString(raw, "cursor");
+ const rowCursor = parseAuthorityCursor(cursor);
+ if (rowCursor !== checkpointCursor + BigInt(index)) {
+ throw new AuthorityStoreProtocolError("PostgreSQL authority commit window is not contiguous");
+ }
+
+ const digest = raw.projection_digest;
+ if (raw.projection !== null) {
+ // Pre-upgrade rows retained the full projection. Their original
+ // operation, event and receipt identities are unchanged.
+ if (raw.delta !== null || digest !== null || raw.checkpoint_projection !== null) {
+ throw new AuthorityStoreProtocolError("PostgreSQL legacy commit has a conflicting replay record");
+ }
+ replay = new AuthorityStateReplay(raw.projection);
+ } else {
+ if (isAuthorityStateCheckpoint(rowCursor)) {
+ if (raw.checkpoint_projection === null || raw.delta !== null ||
+ digest !== raw.checkpoint_digest) {
+ throw new AuthorityStoreProtocolError("PostgreSQL commit has a missing or inconsistent checkpoint");
+ }
+ replay = new AuthorityStateReplay(raw.checkpoint_projection);
+ } else {
+ if (replay === null || raw.delta === null || raw.checkpoint_projection !== null) {
+ throw new AuthorityStoreProtocolError("PostgreSQL commit has a missing or inconsistent delta");
+ }
+ replay.apply(raw.delta);
+ }
+ if (typeof digest !== "string" || replay.stateDigest() !== digest) {
+ throw new AuthorityStoreProtocolError("PostgreSQL commit projection digest mismatch");
+ }
+ }
+
+ const providerRevision = providerRevisionToken(
+ storeIdentity, requiredRowString(raw, "provider_revision"),
+ );
+ const operationId = requireAuthorityStoreId(raw.operation_id, "PostgreSQL operation id");
+ if (rowCursor >= fromCursor) {
+ transactions.push({
+ cursor,
+ provider_revision: providerRevision,
+ operation_id: operationId,
+ projection: replay.snapshot(),
+ events: canonicalAuthorityObjectList(raw.events, "events"),
+ receipts: canonicalAuthorityObjectList(raw.receipts, "receipts"),
+ });
+ }
+ }
+
return scan.page(transactions, snapshot);
});
} catch (error) {
diff --git a/loopx/control_plane/coordination/remote_authority_contract.ts b/loopx/control_plane/coordination/remote_authority_contract.ts
index 638d89461a..d37ef5f7bb 100644
--- a/loopx/control_plane/coordination/remote_authority_contract.ts
+++ b/loopx/control_plane/coordination/remote_authority_contract.ts
@@ -1,16 +1,21 @@
/** Candidate wire contract. Clients submit intentions, never authority facts. */
import type {JsonObject} from "../effect_program.ts";
-import {canonicalAuthorityObject, canonicalAuthoritySha256, hasExactAuthorityKeys,
+import {canonicalAuthorityObject, canonicalAuthoritySha256,
requireAuthorityStoreId} from "./authority_store_codec.ts";
import {POSTGRESQL_STORE_IDENTITY_PATTERN} from "./postgresql_authority_store.ts";
import {taskLeaseOperationIdentity} from "../work_items/task_lease_operation_identity.ts";
import {normalizeTodoId, normalizeGoalId, normalizeOwner} from "../work_items/task_lease_acquire.ts";
import {BARE_SHA256_PATTERN} from "../content_digest.ts";
import {parseIsoTimestamp} from "../runtime_timestamp.ts";
+import {normalizeTodoResumeWhen, TODO_RESUME_NORMALIZE_REQUEST_SCHEMA_VERSION} from "../todos/resume_condition.ts";
+import {normalizeTodoPriority} from "../todos/priority.ts";
export const REMOTE_AUTHORITY_REQUEST = "loopx_remote_authority_request_v0";
export const REMOTE_AUTHORITY_RESPONSE = "loopx_remote_authority_response_v0";
-export const REMOTE_AUTHORITY_OPERATIONS = ["read", "claim", "progress", "renew", "release"] as const;
+export const REMOTE_AUTHORITY_OPERATIONS = [
+ "read", "claim", "progress", "renew", "release",
+ "complete", "block", "defer", "todo_add", "snapshot",
+] as const;
export type RemoteAuthorityOperation = typeof REMOTE_AUTHORITY_OPERATIONS[number];
export interface RemoteAuthorityScope {
tenant_id: string;
@@ -20,14 +25,32 @@ export interface RemoteAuthorityScope {
}
interface Base extends RemoteAuthorityScope {
schema_version: typeof REMOTE_AUTHORITY_REQUEST;
- todo_id: string;
+ todo_id?: string;
}
export type RemoteAuthorityRequest = Base & (
- | {operation: "read"}
- | {operation: "claim"; request_id: string}
- | {operation: "progress"; request_id: string; claim_request_id: string; note: string}
- | {operation: "renew"; idempotency_key: string; expected_version: number; ttl_seconds: number}
- | {operation: "release"; idempotency_key: string; expected_version: number}
+ | {operation: "read"; todo_id: string}
+ | {operation: "claim"; todo_id: string; request_id: string}
+ | {operation: "progress"; todo_id: string; request_id: string; claim_request_id: string; note: string}
+ | {operation: "renew"; todo_id: string; idempotency_key: string; expected_version: number; ttl_seconds: number}
+ | {operation: "release"; todo_id: string; idempotency_key: string; expected_version: number}
+ | {operation: "complete"; todo_id: string; request_id: string; claim_request_id: string; evidence: string; note?: string; no_followup?: boolean}
+ | {operation: "block"; todo_id: string; request_id: string; claim_request_id?: string; reason: string; resume_when?: string; note?: string}
+ | {operation: "defer"; todo_id: string; request_id: string; claim_request_id?: string; reason: string; resume_when: string; note?: string}
+ | {
+ operation: "todo_add";
+ todo_id: string;
+ request_id: string;
+ text: string;
+ role?: "user" | "agent";
+ priority?: string;
+ task_class?: string;
+ action_kind?: string;
+ task_repository?: string;
+ required_write_scopes?: readonly string[];
+ claimed_by?: string;
+ note?: string;
+ }
+ | {operation: "snapshot"; todo_id?: string}
);
export type RemoteAuthorityMode = "execute" | "recover";
export function remoteAuthorityId(value: unknown, name: string): string {
@@ -35,27 +58,104 @@ export function remoteAuthorityId(value: unknown, name: string): string {
if (id.length > 200 || /[\x00-\x20\x7f]/u.test(id)) throw new TypeError(`Invalid ${name}`);
return id;
}
+
+const MANDATORY_KEYS: Record = {
+ read: ["todo_id"],
+ claim: ["todo_id", "request_id"],
+ progress: ["todo_id", "request_id", "claim_request_id", "note"],
+ renew: ["todo_id", "ttl_seconds", "idempotency_key", "expected_version"],
+ release: ["todo_id", "idempotency_key", "expected_version"],
+ complete: ["todo_id", "request_id", "claim_request_id", "evidence"],
+ block: ["todo_id", "request_id", "reason"],
+ defer: ["todo_id", "request_id", "reason", "resume_when"],
+ todo_add: ["todo_id", "request_id", "text"],
+ snapshot: [],
+};
+const OPTIONAL_KEYS: Record = {
+ read: [],
+ claim: [],
+ progress: [],
+ renew: [],
+ release: [],
+ complete: ["note", "no_followup"],
+ block: ["claim_request_id", "resume_when", "note"],
+ defer: ["claim_request_id", "note"],
+ todo_add: ["role", "priority", "task_class", "action_kind", "task_repository", "required_write_scopes", "claimed_by", "note"],
+ snapshot: ["todo_id"],
+};
+
export function decodeRemoteAuthorityRequest(raw: unknown): RemoteAuthorityRequest {
const value = canonicalAuthorityObject(raw, "remote authority request");
- const keys = ["schema_version", "tenant_id", "goal_id", "actor_id", "store_identity", "todo_id", "operation"];
if (value.schema_version !== REMOTE_AUTHORITY_REQUEST ||
- !REMOTE_AUTHORITY_OPERATIONS.includes(value.operation as RemoteAuthorityOperation)) throw new TypeError("Invalid request schema or operation");
- switch (value.operation) {
- case "claim": keys.push("request_id"); break;
- case "progress": keys.push("request_id", "claim_request_id", "note"); break;
- case "renew": keys.push("ttl_seconds", "idempotency_key", "expected_version"); break;
- case "release": keys.push("idempotency_key", "expected_version"); break;
- }
- if (!hasExactAuthorityKeys(value, keys)) throw new TypeError("Unexpected or missing request fields");
- for (const key of ["tenant_id", "goal_id", "actor_id", "todo_id", "request_id", "claim_request_id", "idempotency_key"]) {
- if (key in value) remoteAuthorityId(value[key], key);
- }
- if (normalizeTodoId(value.todo_id) !== value.todo_id || normalizeGoalId(value.goal_id) !== value.goal_id ||
- normalizeOwner(value.actor_id) !== value.actor_id) throw new TypeError("Use canonical task, Goal and actor identities");
- if (typeof value.store_identity !== "string" || !POSTGRESQL_STORE_IDENTITY_PATTERN.test(value.store_identity)) throw new TypeError("Invalid store identity");
- if ("expected_version" in value && (!Number.isSafeInteger(value.expected_version) || Number(value.expected_version) < 0)) throw new TypeError("Invalid lease version");
- if ("ttl_seconds" in value && (!Number.isSafeInteger(value.ttl_seconds) || Number(value.ttl_seconds) < 1 || Number(value.ttl_seconds) > 3600)) throw new TypeError("Invalid lease duration");
- if (value.operation === "progress" && (typeof value.note !== "string" || !value.note.trim() || Buffer.byteLength(value.note) > 4096)) throw new TypeError("Progress requires a bounded note");
+ !REMOTE_AUTHORITY_OPERATIONS.includes(value.operation as RemoteAuthorityOperation)) {
+ throw new TypeError("Invalid request schema or operation");
+ }
+ const op = value.operation as RemoteAuthorityOperation;
+ const baseKeys = ["schema_version", "tenant_id", "goal_id", "actor_id", "store_identity", "operation"];
+ const required = [...baseKeys, ...MANDATORY_KEYS[op]];
+ for (const key of required) {
+ if (!Object.hasOwn(value, key)) throw new TypeError(`Unexpected or missing request fields: missing ${key}`);
+ }
+ const allowed = new Set([...required, ...OPTIONAL_KEYS[op]]);
+ for (const key of Object.keys(value)) {
+ if (!allowed.has(key)) throw new TypeError(`Unexpected or missing request fields: unexpected ${key}`);
+ }
+ for (const key of ["tenant_id", "goal_id", "actor_id", "todo_id", "request_id", "claim_request_id", "idempotency_key", "claimed_by"]) {
+ if (Object.hasOwn(value, key)) remoteAuthorityId(value[key], key);
+ }
+ if (typeof value.todo_id === "string" && normalizeTodoId(value.todo_id) !== value.todo_id) {
+ throw new TypeError("Use canonical task, Goal and actor identities");
+ }
+ if (normalizeGoalId(value.goal_id) !== value.goal_id || normalizeOwner(value.actor_id) !== value.actor_id) {
+ throw new TypeError("Use canonical task, Goal and actor identities");
+ }
+ if (typeof value.store_identity !== "string" || !POSTGRESQL_STORE_IDENTITY_PATTERN.test(value.store_identity)) {
+ throw new TypeError("Invalid store identity");
+ }
+ if ("expected_version" in value && (!Number.isSafeInteger(value.expected_version) || Number(value.expected_version) < 0)) {
+ throw new TypeError("Invalid lease version");
+ }
+ if ("ttl_seconds" in value && (!Number.isSafeInteger(value.ttl_seconds) || Number(value.ttl_seconds) < 1 || Number(value.ttl_seconds) > 3600)) {
+ throw new TypeError("Invalid lease duration");
+ }
+ if ("note" in value && (typeof value.note !== "string" || !value.note.trim() || Buffer.byteLength(value.note) > 4096)) {
+ throw new TypeError("Note requires a bounded non-empty string");
+ }
+ if ("no_followup" in value && typeof value.no_followup !== "boolean") {
+ throw new TypeError("no_followup must be a boolean");
+ }
+ if ("evidence" in value && (typeof value.evidence !== "string" || !value.evidence.trim() || Buffer.byteLength(value.evidence) > 16 * 1024)) {
+ throw new TypeError("Evidence requires a bounded non-empty string");
+ }
+ if ("reason" in value && (typeof value.reason !== "string" || !value.reason.trim() || Buffer.byteLength(value.reason) > 4096)) {
+ throw new TypeError("Reason requires a bounded non-empty string");
+ }
+ if ("resume_when" in value && (typeof value.resume_when !== "string" || Buffer.byteLength(value.resume_when) > 1024 ||
+ normalizeTodoResumeWhen({schema_version: TODO_RESUME_NORMALIZE_REQUEST_SCHEMA_VERSION, resume_when: value.resume_when}) === null)) {
+ throw new TypeError("A bounded structured Todo resume condition is required");
+ }
+ if ("text" in value && (typeof value.text !== "string" || !value.text.trim() || Buffer.byteLength(value.text) > 4096)) {
+ throw new TypeError("Text requires a bounded non-empty string");
+ }
+ if ("priority" in value && (typeof value.priority !== "string" || normalizeTodoPriority(value.priority) !== value.priority)) {
+ throw new TypeError("Invalid priority");
+ }
+ if ("role" in value && value.role !== "user" && value.role !== "agent") {
+ throw new TypeError("Invalid role");
+ }
+ if ("claimed_by" in value && normalizeOwner(value.claimed_by) !== value.claimed_by) {
+ throw new TypeError("Invalid claimed_by");
+ }
+ if ("required_write_scopes" in value) {
+ if (!Array.isArray(value.required_write_scopes) || value.required_write_scopes.some(s => typeof s !== "string" || !s.trim() || s.length > 256)) {
+ throw new TypeError("Invalid required_write_scopes");
+ }
+ }
+ for (const strField of ["task_class", "action_kind", "task_repository"]) {
+ if (strField in value && (typeof value[strField] !== "string" || !value[strField].trim() || value[strField].length > 256)) {
+ throw new TypeError(`Invalid ${strField}`);
+ }
+ }
return value as unknown as RemoteAuthorityRequest;
}
@@ -78,6 +178,13 @@ export function remoteAuthorityResponse(request: unknown, mode: RemoteAuthorityM
request_sha256: canonicalAuthoritySha256({mode, request})};
}
+export function remoteAuthoritySnapshotDigest(request: RemoteAuthorityScope, revision: unknown,
+ todos: readonly unknown[], leases: readonly unknown[]): string {
+ return canonicalAuthoritySha256({schema_version: "loopx_goal_snapshot_receipt_v0",
+ tenant_id: request.tenant_id, goal_id: request.goal_id, store_identity: request.store_identity,
+ provider_revision: revision, todos, leases});
+}
+
function responseRequires(condition: unknown): asserts condition {
if (!condition) throw new TypeError("Invalid remote authority response evidence");
}
@@ -112,6 +219,36 @@ function responseLease(raw: unknown, request: RemoteAuthorityRequest, mutation:
}
return lease;
}
+
+function responseTodo(raw: unknown, todoId?: string): string {
+ const todo = canonicalAuthorityObject(raw, "response Todo");
+ const id = normalizeTodoId(todo.todo_id);
+ responseRequires(todo.todo_id === id && (todoId === undefined || id === todoId));
+ remoteAuthorityId(todo.status, "Todo status");
+ if (todo.claimed_by != null) responseRequires(typeof todo.claimed_by === "string" && normalizeOwner(todo.claimed_by) === todo.claimed_by);
+ if (todo.note !== undefined) responseRequires(todo.note === null || typeof todo.note === "string");
+ if (todo.note_omitted !== undefined) responseRequires(todo.note_omitted === true && !Object.hasOwn(todo, "note"));
+ return id;
+}
+
+function responseSnapshot(value: JsonObject, request: Extract): void {
+ responseRequires(value.status === "snapshot" && value.goal_id === request.goal_id && value.store_identity === request.store_identity &&
+ Array.isArray(value.todos) && Array.isArray(value.leases));
+ responseRevision(value.provider_revision, request);
+ const todoIds = value.todos.map(todo => responseTodo(todo, request.todo_id));
+ const todoSet = new Set(todoIds);
+ responseRequires(todoSet.size === todoIds.length);
+ const leaseIds = new Set();
+ for (const raw of value.leases) {
+ const lease = responseLease(raw, request, false);
+ const id = normalizeTodoId(lease.todo_id);
+ responseRequires(lease.todo_id === id && lease.goal_id === request.goal_id && todoSet.has(id) &&
+ !leaseIds.has(id) && !Object.hasOwn(lease, "idempotency_key"));
+ leaseIds.add(id);
+ }
+ responseRequires(value.receipt_sha256 === remoteAuthoritySnapshotDigest(request, value.provider_revision, value.todos, value.leases));
+}
+
function responseCommand(raw: unknown, request: RemoteAuthorityRequest, operationId: string, historical: boolean): void {
const result = canonicalAuthorityObject(raw, "command response");
if (!historical) {
@@ -128,11 +265,25 @@ function responseCommand(raw: unknown, request: RemoteAuthorityRequest, operatio
}
responseRequires(typeof result.changed === "boolean");
if (result.operation_id !== undefined) responseRequires(result.operation_id === operationId);
- if (request.operation === "claim" || request.operation === "progress") responseRequires(result.todo_id === request.todo_id);
+ if (request.operation !== "renew" && request.operation !== "release") responseRequires(result.todo_id === request.todo_id);
if (request.operation === "claim") {
- responseRequires(result.claimed_by === request.actor_id && result.claim_operation_id === operationId);
+ responseRequires(result.claimed_by === request.actor_id && result.claim_operation_id === operationId &&
+ result.handoff_mode === "hard_lease");
+ responseLease(result.lease, request, true);
+ }
+ if (request.operation === "complete") {
+ responseRequires(typeof result.completed_at === "string" && parseIsoTimestamp(result.completed_at) !== null);
+ remoteAuthorityId(result.completion_receipt_id, "completion receipt id");
}
- if (request.operation !== "progress") {
+ if (request.operation === "todo_add") responseTodo(result.todo, request.todo_id);
+ if ((request.operation === "block" || request.operation === "defer") && result.changed === true) {
+ const transition = canonicalAuthorityObject(request.operation === "block"
+ ? result.blocked_lifecycle_transition : result.deferred_transition, "Todo lifecycle transition");
+ responseRequires(transition.kind === (request.operation === "block" ? "todo_blocked" : "todo_deferred") &&
+ transition.execution_authority_granted === false &&
+ ["absent", "released", "already_released"].some(state => transition.lease_retirement === state));
+ }
+ if (request.operation === "renew" || request.operation === "release") {
responseRequires(result.handoff_mode === "hard_lease");
if (request.operation === "release" && result.missing === true) {
responseRequires(request.expected_version === 0 && result.changed === false &&
@@ -155,8 +306,8 @@ export function decodeRemoteAuthorityResponse(raw: unknown, request: RemoteAutho
responseRequires(typeof value.status === "string");
if (["rejected", "unavailable", "ambiguous"].includes(value.status)) {
remoteAuthorityId(value.reason_code, "response reason");
- responseRequires(value.status !== "ambiguous" || mode === "execute" && request.operation !== "read");
- } else if (mode === "recover" && request.operation !== "read") {
+ responseRequires(value.status !== "ambiguous" || mode === "execute" && request.operation !== "read" && request.operation !== "snapshot");
+ } else if (mode === "recover" && request.operation !== "read" && request.operation !== "snapshot") {
const id = responseOperationId(value.operation_id, request);
if (value.status !== "missing") {
responseRequires(value.status === "found" && value.historical_only === true &&
@@ -169,16 +320,13 @@ export function decodeRemoteAuthorityResponse(raw: unknown, request: RemoteAutho
if (value.status !== "missing") {
responseRequires(value.status === "found");
responseRevision(value.provider_revision, request);
- const todo = canonicalAuthorityObject(value.todo, "read Todo");
- responseRequires(todo.todo_id === request.todo_id);
- remoteAuthorityId(todo.status, "Todo status");
- if (todo.claimed_by != null) responseRequires(typeof todo.claimed_by === "string" && normalizeOwner(todo.claimed_by) === todo.claimed_by);
- if (todo.note !== undefined) responseRequires(todo.note === null || typeof todo.note === "string");
- if (todo.note_omitted !== undefined) responseRequires(todo.note_omitted === true && !Object.hasOwn(todo, "note"));
+ responseTodo(value.todo, request.todo_id);
if (value.lease !== null) responseLease(value.lease, request, false);
}
+ } else if (mode === "execute" && request.operation === "snapshot") {
+ responseSnapshot(value, request);
} else {
- responseRequires(mode === "execute" && request.operation !== "read" && value.status === "result");
+ responseRequires(mode === "execute" && request.operation !== "read" && request.operation !== "snapshot" && value.status === "result");
responseCommand(value.result, request, responseOperationId(value.operation_id, request), false);
}
return value;
diff --git a/loopx/control_plane/coordination/remote_authority_gateway.py b/loopx/control_plane/coordination/remote_authority_gateway.py
new file mode 100644
index 0000000000..bc15e6e7b6
--- /dev/null
+++ b/loopx/control_plane/coordination/remote_authority_gateway.py
@@ -0,0 +1,77 @@
+"""Private configuration and read-only observation for the remote authority candidate."""
+
+from __future__ import annotations
+
+import json
+import os
+import subprocess
+from collections.abc import Mapping
+from pathlib import Path
+from typing import Any
+
+REMOTE_AUTHORITY_GATEWAY_SCHEMA = "loopx_remote_authority_gateway_v0"
+_REQUIRED_BINDING = ("endpoint", "tenant_id", "store_identity", "actor_id", "probe_todo_id")
+
+
+def remote_authority_config(
+ goal: Mapping[str, Any] | None,
+) -> Mapping[str, Any] | None:
+ """Opt in only through the selected registered Goal, never an environment default."""
+ cfg = goal.get("remote_authority") if goal is not None else None
+ return cfg if isinstance(cfg, Mapping) and cfg.get("enabled") is True else None
+
+
+def _binding_complete(cfg: Mapping[str, Any]) -> bool:
+ return all(isinstance(cfg.get(key), str) and bool(cfg[key].strip()) for key in _REQUIRED_BINDING)
+
+
+def get_remote_authority_projection(goal_id: str, cfg: Mapping[str, Any]) -> dict[str, Any]:
+ """Describe configuration without portraying it as connectivity or exposing its binding."""
+ return {
+ "schema_version": REMOTE_AUTHORITY_GATEWAY_SCHEMA,
+ "goal_id": goal_id,
+ "enabled": True,
+ "handoff_mode": "hard_lease",
+ "access": "opt_in_authenticated_only",
+ "status": "unverified" if _binding_complete(cfg) else "unconfigured",
+ }
+
+
+def check_remote_authority_gateway_status(
+ goal_id: str,
+ agent_id: str | None,
+ cfg: Mapping[str, Any],
+) -> dict[str, Any]:
+ """Read one known remote Todo through the existing TLS client; return no remote content."""
+ projection = get_remote_authority_projection(goal_id, cfg)
+ if projection["status"] == "unconfigured":
+ return {**projection, "reason_code": "remote_authority_binding_incomplete"}
+ if agent_id != cfg["actor_id"]:
+ return {**projection, "status": "unavailable", "reason_code": "remote_authority_actor_mismatch"}
+ credential = os.environ.get("LOOPX_AUTHORITY_CREDENTIAL") or os.environ.get("LOOPX_REMOTE_AUTHORITY_CREDENTIAL")
+ if not credential:
+ return {**projection, "status": "unavailable", "reason_code": "remote_authority_credential_required"}
+ from ..effect_runtime import EffectRuntimeStartupError, _node_executable
+
+ try:
+ # The user-owned registry supplies the origin and optional private CA.
+ # Pass the credential and binding through the private pipe, not argv.
+ bridge = Path(__file__).with_name("remote_authority_probe.ts")
+ completed = subprocess.run(
+ [_node_executable(), "--no-warnings", "--experimental-strip-types", str(bridge)],
+ input=json.dumps({
+ **{key: cfg[key] for key in _REQUIRED_BINDING},
+ "goal_id": goal_id, "credential": credential, "ca_file": cfg.get("ca_file"),
+ }),
+ env={key: os.environ[key] for key in ("PATH", "SystemRoot", "WINDIR", "TMPDIR", "TEMP")
+ if key in os.environ},
+ text=True,
+ capture_output=True,
+ check=True,
+ timeout=5,
+ )
+ if completed.stdout == "verified\n":
+ return {**projection, "status": "verified"}
+ except (EffectRuntimeStartupError, OSError, subprocess.SubprocessError):
+ pass
+ return {**projection, "status": "unavailable", "reason_code": "remote_authority_read_unverified"}
diff --git a/loopx/control_plane/coordination/remote_authority_http.ts b/loopx/control_plane/coordination/remote_authority_http.ts
index 20e8e50c1e..55aca59b90 100644
--- a/loopx/control_plane/coordination/remote_authority_http.ts
+++ b/loopx/control_plane/coordination/remote_authority_http.ts
@@ -49,7 +49,7 @@ export function createRemoteAuthorityServer(service: RemoteAuthorityService,
const encoded = JSON.stringify(result);
if (Buffer.byteLength(encoded) > MAX_RESPONSE_BYTES) {
response.end(JSON.stringify(remoteAuthorityResponse(decoded, mode, {status:
- mode === "recover" || decoded.operation === "read" ? "unavailable" : "ambiguous",
+ mode === "recover" || decoded.operation === "read" || decoded.operation === "snapshot" ? "unavailable" : "ambiguous",
reason_code: "remote_authority_response_too_large"})));
} else response.end(encoded);
} catch {
@@ -102,7 +102,7 @@ export async function callRemoteAuthority(endpoint: string, credential: string,
const timeout = options.timeoutMs ?? 15000;
if (!Number.isSafeInteger(timeout) || timeout < 1 || timeout > 120000) throw new TypeError("Invalid request deadline");
const uncertain = () => remoteAuthorityResponse(request, mode, {status:
- mode === "recover" || request.operation === "read" ? "unavailable" : "ambiguous",
+ mode === "recover" || request.operation === "read" || request.operation === "snapshot" ? "unavailable" : "ambiguous",
reason_code: "remote_authority_transport_uncertain"});
return new Promise(resolve => {
const body = JSON.stringify(request);
diff --git a/loopx/control_plane/coordination/remote_authority_probe.ts b/loopx/control_plane/coordination/remote_authority_probe.ts
new file mode 100644
index 0000000000..a21846231e
--- /dev/null
+++ b/loopx/control_plane/coordination/remote_authority_probe.ts
@@ -0,0 +1,40 @@
+/** Private stdin bridge to the same scoped TLS client used by task commands. */
+import {open} from "node:fs/promises";
+import {isAbsolute} from "node:path";
+import {canonicalAuthorityObject} from "./authority_store_codec.ts";
+import {decodeRemoteAuthorityRequest, REMOTE_AUTHORITY_REQUEST} from "./remote_authority_contract.ts";
+import {callRemoteAuthority} from "./remote_authority_http.ts";
+
+async function readCa(path: unknown): Promise {
+ if (path === null || path === undefined) return undefined;
+ if (typeof path !== "string" || !isAbsolute(path)) throw new TypeError("Invalid CA path");
+ const file = await open(path, "r");
+ try {
+ const info = await file.stat();
+ if (!info.isFile() || info.size === 0 || info.size > 64 * 1024) throw new TypeError("Invalid CA file");
+ const bytes = Buffer.alloc(info.size);
+ if ((await file.read(bytes, 0, bytes.length, 0)).bytesRead !== bytes.length) throw new TypeError("Incomplete CA file");
+ return bytes;
+ } finally {await file.close();}
+}
+
+try {
+ let input = "";
+ process.stdin.setEncoding("utf8");
+ for await (const chunk of process.stdin) {
+ input += chunk;
+ if (Buffer.byteLength(input) > 16 * 1024) throw new TypeError("Invalid probe input");
+ }
+ const cfg = canonicalAuthorityObject(JSON.parse(input), "remote authority probe");
+ if (typeof cfg.endpoint !== "string" || typeof cfg.credential !== "string") throw new TypeError("Invalid probe binding");
+ const request = decodeRemoteAuthorityRequest({
+ schema_version: REMOTE_AUTHORITY_REQUEST, operation: "read",
+ tenant_id: cfg.tenant_id, goal_id: cfg.goal_id, actor_id: cfg.actor_id,
+ store_identity: cfg.store_identity, todo_id: cfg.probe_todo_id,
+ });
+ const response = await callRemoteAuthority(cfg.endpoint, cfg.credential, request, {ca: await readCa(cfg.ca_file), timeoutMs: 3000});
+ // A missing Todo is not proof of usable access to the expected authority.
+ process.stdout.write(response.status === "found" ? "verified\n" : "unavailable\n");
+} catch {
+ process.stdout.write("unavailable\n");
+}
diff --git a/loopx/control_plane/coordination/remote_authority_service.ts b/loopx/control_plane/coordination/remote_authority_service.ts
index 7a490e0919..75f045e7e0 100644
--- a/loopx/control_plane/coordination/remote_authority_service.ts
+++ b/loopx/control_plane/coordination/remote_authority_service.ts
@@ -6,10 +6,13 @@ import {PostgreSqlAuthorityService} from "./postgresql_authority_service.ts";
import type {PostgreSqlAuthorityDatabase} from "./postgresql_authority_store.ts";
import {executeCoordinationTodoClaim} from "./todo_claim.ts";
import {executeCoordinationTodoUpdate} from "./todo_update.ts";
+import {executeCoordinationTodoCreate} from "./todo_create.ts";
+import {executeCoordinationTodoTerminalLifecycle, type CoordinationTodoTerminalLifecycleInput} from "./todo_terminal_lifecycle.ts";
import {executeCanonicalTaskLeaseLifecycle} from "./task_lease_lifecycle.ts";
import {leaseVersion, leaseEpoch} from "../work_items/task_lease_acquire.ts";
+import {TODO_DOMAIN_ITEM_SCHEMA} from "./coordination_state_contract.ts";
import {indexCoordinationProjection, validateCoordinationTodoReadModel} from "./coordination_projection.ts";
-import {decodeRemoteAuthorityRequest, remoteAuthorityOperationId, remoteAuthorityResponse,
+import {decodeRemoteAuthorityRequest, remoteAuthorityOperationId, remoteAuthorityResponse, remoteAuthoritySnapshotDigest,
type RemoteAuthorityMode, type RemoteAuthorityRequest} from "./remote_authority_contract.ts";
export type RemoteAuthorityAdmission =
@@ -68,6 +71,10 @@ function pinnedStore(store: AuthorityStore, identity: string, admission: JsonObj
if (commit.operation_id !== operationId || commit.expected_provider_revision === null ||
!matches(commit.expected_provider_revision)) return {status: "failed",
reason_code: "remote_authority_commit_binding_invalid", reason: "Command does not match admitted authority"};
+ if (commit.next_projection.handoff_mode !== "hard_lease") return {
+ status: "failed", reason_code: "remote_authority_requires_hard_lease",
+ reason: "The committing authority must use hard leases",
+ };
// Metadata shares the existing business receipt transaction; no second ledger.
return store.commitAuthority({...commit, receipts: commit.receipts.map(receipt =>
({...receipt, remote_admission: admission}))});
@@ -75,12 +82,23 @@ function pinnedStore(store: AuthorityStore, identity: string, admission: JsonObj
};
}
+function projectReadTodo(todo: JsonObject, includePlanning: boolean): JsonObject {
+ const fields = ["todo_id", "status", "claimed_by", "note", ...(includePlanning ? ["role", "priority"] : [])];
+ const output = Object.fromEntries(fields.filter(field => Object.hasOwn(todo, field)).map(field => [field, todo[field]]));
+ if (Object.hasOwn(output, "note") && Buffer.byteLength(JSON.stringify(output.note)) > MAX_READ_NOTE_BYTES) {
+ delete output.note;
+ output.note_omitted = true;
+ }
+ return output;
+}
+
/** Deliberately bounded response projection: no SQL errors, paths, arbitrary
* canonical fields, workspace observations or provider credentials cross here. */
function commandResult(result: JsonObject): JsonObject {
const output: JsonObject = {};
for (const key of ["status", "reason_code", "changed", "todo_id", "claimed_by", "operation_id",
- "claim_operation_id", "provider_revision", "cursor", "handoff_mode", "released", "renewed", "missing"]) {
+ "claim_operation_id", "provider_revision", "cursor", "handoff_mode", "released", "renewed", "missing",
+ "completed", "completed_at", "completion_receipt_id"]) {
if (Object.hasOwn(result, key)) output[key] = result[key];
}
if (result.lease && typeof result.lease === "object" && !Array.isArray(result.lease)) {
@@ -89,6 +107,18 @@ function commandResult(result: JsonObject): JsonObject {
"acquired_at", "updated_at", "expires_at"].filter(k => k in lease).map(k => [k, lease[k]])),
version: leaseVersion(lease), lease_epoch: leaseEpoch(lease)};
}
+ if (result.todo && typeof result.todo === "object" && !Array.isArray(result.todo)) {
+ const todo = result.todo as JsonObject;
+ output.todo = Object.fromEntries(["todo_id", "status", "role", "priority", "claimed_by", "note"].filter(k => k in todo).map(k => [k, todo[k]]));
+ }
+ for (const key of ["blocked_lifecycle_transition", "deferred_transition"]) {
+ const raw = result[key];
+ if (raw && typeof raw === "object" && !Array.isArray(raw)) {
+ const transition = raw as JsonObject;
+ output[key] = Object.fromEntries(["kind", "lease_retirement", "execution_authority_granted"]
+ .filter(field => Object.hasOwn(transition, field)).map(field => [field, transition[field]]));
+ }
+ }
return output;
}
@@ -101,7 +131,7 @@ export class RemoteAuthorityService {
try { request = decodeRemoteAuthorityRequest(raw); }
catch { return {status: "rejected", reason_code: "invalid_remote_authority_request"}; }
const reply = (payload: JsonObject) => remoteAuthorityResponse(request, mode, payload);
- if (mode !== "execute" && mode !== "recover" || mode === "recover" && request.operation === "read") {
+ if (mode !== "execute" && mode !== "recover" || mode === "recover" && (request.operation === "read" || request.operation === "snapshot")) {
return reply({status: "rejected", reason_code: "invalid_remote_authority_mode"});
}
let admission: RemoteAuthorityAdmission;
@@ -125,7 +155,7 @@ export class RemoteAuthorityService {
const store = pinnedStore(opened.store, request.store_identity, binding, operationId);
let dispatched = false;
try {
- const previous = request.operation === "read" ? null : await store.readReceipt(operationId);
+ const previous = (request.operation === "read" || request.operation === "snapshot") ? null : await store.readReceipt(operationId);
if (previous?.status === "found") {
const original = previous.receipts[0];
const recorded = original?.remote_admission as JsonObject | undefined;
@@ -150,17 +180,35 @@ export class RemoteAuthorityService {
if (request.operation === "read") {
const todo = index.todos.get(request.todo_id), lease = index.leases.get(request.todo_id);
if (!todo) return reply({status: "missing"});
- const projectedTodo = Object.fromEntries(["todo_id", "status", "claimed_by", "note"].filter(k => k in todo).map(k => [k, todo[k]]));
- // Notes authored through other canonical routes can exceed the remote
- // write limit. Preserve the small lease facts needed for renew/release.
- if (Object.hasOwn(projectedTodo, "note") && Buffer.byteLength(JSON.stringify(projectedTodo.note)) > MAX_READ_NOTE_BYTES) {
- delete projectedTodo.note; projectedTodo.note_omitted = true;
- }
return reply({status: "found", provider_revision: head.provider_revision,
- todo: projectedTodo,
+ todo: projectReadTodo(todo, false),
lease: lease ? {...Object.fromEntries(["owner", "status", "expires_at"].filter(k => k in lease).map(k => [k, lease[k]])),
version: leaseVersion(lease), lease_epoch: leaseEpoch(lease)} : null});
}
+ if (request.operation === "snapshot") {
+ const todos = index.todo_ids
+ .filter(id => !request.todo_id || id === request.todo_id)
+ .map(id => projectReadTodo(index.todos.get(id)!, true));
+ const leases = index.lease_todo_ids
+ .filter(id => !request.todo_id || id === request.todo_id)
+ .map(id => {
+ const l = index.leases.get(id)!;
+ return {
+ ...Object.fromEntries(["goal_id", "todo_id", "owner", "status", "expires_at"].filter(k => k in l).map(k => [k, l[k]])),
+ version: leaseVersion(l),
+ lease_epoch: leaseEpoch(l),
+ };
+ });
+ return reply({
+ status: "snapshot",
+ provider_revision: head.provider_revision,
+ store_identity: request.store_identity,
+ goal_id: request.goal_id,
+ receipt_sha256: remoteAuthoritySnapshotDigest(request, head.provider_revision, todos, leases),
+ todos,
+ leases,
+ });
+ }
if (head.head.handoff_mode !== "hard_lease") return reply({status: "rejected", reason_code: "remote_authority_requires_hard_lease"});
const now = this.#dependencies.now?.() ?? new Date();
if (!Number.isFinite(now.valueOf())) return reply({status: "unavailable", reason_code: "remote_authority_clock_unavailable"});
@@ -175,7 +223,69 @@ export class RemoteAuthorityService {
operation_id: operationId, actor_agent_id: request.actor_id, expected_role: "agent", dry_run: false,
claim_operation_id: remoteAuthorityOperationId(request, admission.principal_id, request.claim_request_id),
patch: {note: request.note}, clear_fields: []});
- else result = await executeCanonicalTaskLeaseLifecycle(store, {...common, operation: request.operation,
+ else if (request.operation === "complete") {
+ const claimOpId = remoteAuthorityOperationId(request, admission.principal_id, request.claim_request_id);
+ const input: CoordinationTodoTerminalLifecycleInput = {
+ goal_id: request.goal_id, todo_id: request.todo_id, command: "complete",
+ expected_role: "agent", actor_agent_id: request.actor_id,
+ registered_agents: admission.registered_agents, lifecycle_grants: [], authority_reason: null,
+ decision_outcome: null, operation_identity: {kind: "explicit", operation_id: operationId},
+ claim_operation_id: claimOpId, lease_idempotency_key: null, lease_expected_version: null,
+ allow_user_gate_auto_acquire: false, requested_no_followup: request.no_followup === true,
+ requested_completion_turn_key: null, requested_completion_identity_source: null,
+ linked_successor_todo_ids: [], successor_intents: [], note: request.note ?? null,
+ evidence: request.evidence, reason: null, clear_claim: true, validation_declaration: null,
+ validation_receipt: null, completion_policy_request: null, dry_run: false, now,
+ };
+ result = await executeCoordinationTodoTerminalLifecycle(store, input);
+ // Older complete requests implied no_followup. Use that intent only to
+ // replay an already committed receipt for the exact authenticated request.
+ if (previous?.status === "found" && request.no_followup === undefined &&
+ result.reason_code === "coordination_operation_identity_mismatch") {
+ result = await executeCoordinationTodoTerminalLifecycle(store, {...input, requested_no_followup: true});
+ }
+ if (result.status === "applied") result.completed = true;
+ } else if (request.operation === "block" || request.operation === "defer") {
+ const claimOpId = request.claim_request_id
+ ? remoteAuthorityOperationId(request, admission.principal_id, request.claim_request_id)
+ : undefined;
+ result = await executeCoordinationTodoUpdate(store, {
+ ...common, operation_id: operationId, actor_agent_id: request.actor_id,
+ expected_role: "agent", dry_run: false, ...(claimOpId ? {claim_operation_id: claimOpId} : {}),
+ patch: {...(request.note ? {note: request.note} : {})}, clear_fields: [],
+ planning_intent: {
+ status: request.operation === "block" ? "blocked" : "deferred",
+ reason: request.reason,
+ ...(request.resume_when ? {resume_when: request.resume_when} : {}),
+ },
+ });
+ } else if (request.operation === "todo_add") {
+ const todoRecord = {
+ schema_version: TODO_DOMAIN_ITEM_SCHEMA,
+ todo_id: request.todo_id,
+ role: request.role ?? "agent",
+ status: "open",
+ done: false,
+ text: request.text,
+ archive_state: "active",
+ ...(request.priority ? {priority: request.priority} : {}),
+ ...(request.task_class ? {task_class: request.task_class} : {}),
+ ...(request.action_kind ? {action_kind: request.action_kind} : {}),
+ ...(request.task_repository ? {task_repository: request.task_repository} : {}),
+ ...(request.required_write_scopes ? {required_write_scopes: [...request.required_write_scopes]} : {}),
+ ...(request.claimed_by ? {claimed_by: request.claimed_by} : {}),
+ ...(request.note ? {note: request.note} : {}),
+ };
+ result = await executeCoordinationTodoCreate(store, {
+ goal_id: request.goal_id, todo: todoRecord, actor_agent_id: request.actor_id,
+ registered_agents: admission.registered_agents, operation_id: operationId, dry_run: false, now,
+ });
+ // The create owner can return a different, semantically duplicate
+ // Todo. That is a valid local no-op but not proof of this requested id.
+ if (result.status === "no_change" && result.todo_id !== request.todo_id) {
+ result = {status: "failed", reason_code: "remote_authority_todo_id_not_created"};
+ }
+ } else result = await executeCanonicalTaskLeaseLifecycle(store, {...common, operation: request.operation,
owner: request.actor_id, idempotency_key: request.idempotency_key, required_handoff_mode: "hard_lease",
expected_version: request.expected_version, ttl_seconds: request.operation === "renew" ? request.ttl_seconds : null});
return reply({status: "result", operation_id: operationId, result: commandResult(result)});
diff --git a/loopx/control_plane/effect_runtime_handlers.ts b/loopx/control_plane/effect_runtime_handlers.ts
index c4f9f80b2e..648fb9bbd8 100644
--- a/loopx/control_plane/effect_runtime_handlers.ts
+++ b/loopx/control_plane/effect_runtime_handlers.ts
@@ -363,6 +363,10 @@ export function createEffectRuntimeHandlers(
["goal.first_party_host_runtime.decide", lazyHandler(() => import("./goals/first_party_host_runtime.ts"), ({decideFirstPartyHostRuntime}) => decideFirstPartyHostRuntime)],
["goal.chat_session.lifecycle.decide", lazyHandler(() => import("./goals/chat_session_lifecycle.ts"), ({decideChatSessionLifecycle}) => decideChatSessionLifecycle)],
["goal.task.validation_plan", lazyHandler(() => import("./goals/task_validation_plan.ts"), ({canonicalTaskValidationPlan}) => canonicalTaskValidationPlan)],
+ ["goal.task.creation_selection", lazyHandler(() => import("./goals/task_validation_plan.ts"), ({originalCreationTaskSelection}) => originalCreationTaskSelection)],
+ ["goal.creation.execution.normalize", lazyHandler(() => import("./goals/creation_execution_review.ts"), ({normalizeGoalCreationExecution}) => normalizeGoalCreationExecution)],
+ ["goal.creation.execution_request.prepare", lazyHandler(() => import("./goals/creation_execution.ts"), ({prepareGoalCreationExecutionRequest}) => prepareGoalCreationExecutionRequest)],
+ ["goal.creation.execution_request", lazyHandler(() => import("./goals/creation_execution.ts"), ({projectGoalCreationExecutionRequest}) => projectGoalCreationExecutionRequest)],
["goal.acceptance.oracle.plan", lazyHandler(() => import("./goals/acceptance_oracle.ts"), ({planAcceptanceOracle}) => planAcceptanceOracle)],
["goal.acceptance.oracle.observe", lazyHandler(() => import("./goals/acceptance_oracle.ts"), ({observeAcceptanceOracles}) => observeAcceptanceOracles)],
["goal.acceptance.refine", lazyHandler(() => import("./goals/acceptance_authority.ts"), ({refineLocalGoalAcceptanceOutcome}) => refineLocalGoalAcceptanceOutcome)],
@@ -404,6 +408,20 @@ export function createEffectRuntimeHandlers(
["coordination.runtime_shadow.rollback", lazyHandler(() => Promise.all([import("./coordination/runtime_shadow.ts"), import("./coordination/source_transfer.ts")]), ([{rollbackCoordinationRuntimeShadow}, {withCoordinationSourceTransfer}]) => withCoordinationSourceTransfer("coordination.runtime_shadow.rollback", rollbackCoordinationRuntimeShadow))],
["coordination.local_authority.promote", lazyHandler(() => import("./coordination/local_authority_runtime.ts"), ({promoteLocalCoordinationAuthority}) => promoteLocalCoordinationAuthority)],
["coordination.authority_archive.manage", lazyHandler(() => import("./coordination/local_authority_archive.ts"), ({manageLocalAuthorityArchive}) => manageLocalAuthorityArchive)],
+ ["coordination.postgresql.setup", lazyHandler(() => import("./coordination/postgresql_authority_seed.ts"), ({setupPostgreSqlAuthoritySchemaAndRole}) => params => setupPostgreSqlAuthoritySchemaAndRole({
+ databaseUrl: requiredString(params.databaseUrl, "databaseUrl"),
+ storeIdentity: optionalString(params.storeIdentity, "storeIdentity") ?? undefined,
+ runtimeRole: optionalString(params.runtimeRole, "runtimeRole") ?? undefined,
+ }))],
+ ["coordination.postgresql.seed", lazyHandler(() => import("./coordination/postgresql_authority_seed.ts"), ({exportLocalToPgGenesisSeed}) => params => exportLocalToPgGenesisSeed({
+ runtimeRoot: requiredString(params.runtimeRoot, "runtimeRoot"),
+ goalId: requiredString(params.goalId, "goalId"),
+ tenantId: optionalString(params.tenantId, "tenantId") ?? undefined,
+ outputPath: optionalString(params.outputPath, "outputPath") ?? undefined,
+ databaseUrl: optionalString(params.databaseUrl, "databaseUrl") ?? undefined,
+ storeIdentity: optionalString(params.storeIdentity, "storeIdentity") ?? undefined,
+ execute: requireBoolean(params.execute, "execute"),
+ }))],
["configuration.backup", lazyHandler(() => import("./configuration_backup.ts"), ({configurationBackupOperation}) => configurationBackupOperation)],
["runtime.checkpoint_artifacts", lazyHandler(() => import("./runtime/checkpoint_artifacts.ts"), ({checkpointArtifacts}) => checkpointArtifacts)],
["state.recovery.archive.verify", lazyHandler(() => import("./state_recovery.ts"), ({verifyStateRecoveryArchive}) => verifyStateRecoveryArchive)],
@@ -618,6 +636,7 @@ export function createEffectRuntimeHandlers(
["collaboration.steward.managed_mode.executor", lazyHandler(() => import("./collaboration/steward_managed_mode.ts"), ({observeStewardNativeExecutor}) => observeStewardNativeExecutor)],
["collaboration.steward.managed_turn.admit", lazyHandler(() => import("./collaboration/steward_managed_mode.ts"), ({admitStewardManagedTurn}) => admitStewardManagedTurn)],
["collaboration.session.managed_turn.admit", lazyHandler(() => import("./collaboration/steward_managed_mode.ts"), ({admitManagedSessionTurn}) => admitManagedSessionTurn)],
+ ["collaboration.session.managed_turn.sandbox_policy", lazyHandler(() => import("./collaboration/steward_managed_mode.ts"), ({canonicalNativeTaskSandboxPolicy}) => canonicalNativeTaskSandboxPolicy)],
["collaboration.session.managed_identity", lazyHandler(() => import("./collaboration/steward_managed_mode.ts"), ({admitManagedSessionIdentity}) => admitManagedSessionIdentity)],
["collaboration.steward.monitor.plan", lazyHandler(() => import("./collaboration/steward_managed_mode.ts"), ({planStewardMonitor}) => planStewardMonitor)],
["collaboration.steward.report.plan", lazyHandler(() => import("./collaboration/steward_reports.ts"), ({planStewardReport}) => planStewardReport)],
diff --git a/loopx/control_plane/goals/creation_execution.ts b/loopx/control_plane/goals/creation_execution.ts
new file mode 100644
index 0000000000..c1bd0c3000
--- /dev/null
+++ b/loopx/control_plane/goals/creation_execution.ts
@@ -0,0 +1,119 @@
+/** Owner-confirmed Goal creation adapts to the existing original-Task queue.
+ * This request is not Task admission, checker adoption or a host write grant.
+ * Historical creations without explicit start_execution retain their behavior.
+ */
+import {createHash} from "node:crypto";
+import type {JsonObject} from "../runtime_decode.ts";
+import {jsonObject, requireJsonObject} from "../runtime_decode.ts";
+import {EffectRuntimeRequestError} from "../effect_runtime_errors.ts";
+import {goalCreationExecutionReview} from "./creation_execution_review.ts";
+import {canonicalAuthoritySha256} from "../coordination/authority_store_codec.ts";
+
+function verifyOriginalCreationSource(proposal: JsonObject): void {
+ const request: JsonObject = {};
+ for (const key of ["action_kind", "summary", "normalized_parameters", "context", "expected_state_fingerprint",
+ "permission_classification", "validation_evidence", "available_transitions"]) {
+ requireThat(Object.hasOwn(proposal, key), "original creation request source is incomplete");
+ request[key] = proposal[key];
+ }
+ for (const key of ["canonical_update_basis", "workspace_binding"]) {
+ if (Object.hasOwn(proposal, key)) request[key] = proposal[key];
+ }
+ requireThat(typeof proposal.request_digest === "string" && proposal.request_digest.length > 0
+ && canonicalAuthoritySha256(request) === proposal.request_digest,
+ "original creation request source changed");
+}
+
+function requireThat(condition: unknown, message: string): asserts condition {
+ if (!condition) throw new EffectRuntimeRequestError(message);
+}
+
+/** Prepare only after this same creation has retained its Goal, original Todo
+ * and Session. The action owner persists the returned value in its checkpoint.
+ */
+export function prepareGoalCreationExecutionRequest(params: JsonObject): JsonObject | null {
+ const proposal = requireJsonObject(params.proposal, "original Goal creation");
+ const parameters = requireJsonObject(proposal.normalized_parameters, "original creation parameters");
+ const review = goalCreationExecutionReview(parameters);
+ if (!review) return null;
+ verifyOriginalCreationSource(proposal);
+ const session = requireJsonObject(params.session, "original creation Session");
+ const goal = requireJsonObject(params.goal, "original registered Goal");
+ const selected = requireJsonObject(params.current_original_task, "current original Task selection");
+ const steps = requireJsonObject(requireJsonObject(proposal.checkpoint, "original creation checkpoint").steps,
+ "original creation steps");
+ const workspace = requireJsonObject(steps.workspace_validated, "original workspace checkpoint");
+ const bound = requireJsonObject(steps.goal_bootstrapped, "original Goal checkpoint");
+ const agent = requireJsonObject(steps.agent_bound, "original Agent checkpoint");
+ const todos = requireJsonObject(steps.todos_created, "original Task checkpoint");
+ const opened = requireJsonObject(steps.first_session_opened, "original Session checkpoint");
+ const proposalId = proposal.proposal_id, todoId = selected.todo_id;
+ requireThat(proposal.action_kind === "goal.create" && typeof proposalId === "string"
+ && proposalId.length > 0 && proposalId.length <= 80
+ && typeof proposal.expected_state_fingerprint === "string" && proposal.expected_state_fingerprint.length > 0
+ && ["applying", "applied"].includes(String(proposal.status))
+ && Object.hasOwn(goal, "goal_instance_id") && (goal.goal_instance_id === null
+ || typeof goal.goal_instance_id === "string" && goal.goal_instance_id.length > 0)
+ && goal.goal_id === parameters.goal_id && goal.creation_operation_id === proposalId
+ && Array.isArray(goal.registered_agents) && goal.registered_agents.includes(parameters.agent_id)
+ && typeof goal.workspace_path === "string" && goal.workspace_path.length > 0
+ && workspace.outcome === "workspace_validated"
+ && workspace.workspace_digest === createHash("sha256").update(JSON.stringify(goal.workspace_path)).digest("hex")
+ && bound.outcome === "goal_bootstrapped" && bound.goal_id === goal.goal_id
+ && agent.outcome === "agent_bound" && agent.goal_id === goal.goal_id && agent.agent_id === parameters.agent_id
+ && selected.reason === null && typeof todoId === "string" && todoId.length > 0
+ && todos.outcome === "todos_created" && Array.isArray(todos.todo_ids)
+ && todos.todo_ids.length === 1 && todos.todo_ids[0] === todoId
+ && Array.isArray(parameters.initial_todos) && parameters.initial_todos.length === 1
+ && session.goal_id === goal.goal_id && (session.goal_instance_id ?? null) === goal.goal_instance_id
+ && session.managed_agent_id === parameters.agent_id && session.agent_id === "codex"
+ && session.executor_endpoint_id === "codex" && session.session_mode === "managed_runtime"
+ && session.upstream_mode === "chat" && session.managed_workspace_path === goal.workspace_path
+ && typeof session.session_id === "string" && session.session_id.length > 0
+ && session.channel_id === `agent.${parameters.agent_id}`
+ && opened.outcome === "first_session_opened" && opened.session_id === session.session_id
+ && opened.managed_agent_id === session.managed_agent_id && opened.goal_instance_id === goal.goal_instance_id
+ && typeof session.upstream_thread_id === "string" && session.upstream_thread_id.length > 0
+ && opened.upstream_thread_id === session.upstream_thread_id,
+ "original Task request requires the same retained creation, workspace, Goal lifetime, Agent, Task and Session");
+ return {creation_operation_id: proposalId, operation_id: `goal-create-${proposalId}`,
+ goal_id: goal.goal_id, goal_instance_id: goal.goal_instance_id, agent_id: parameters.agent_id,
+ session_id: session.session_id, todo_id: todoId, permission: review.permission, write_scope: review.writeScope};
+}
+
+/** Read the explicit committed request; absence is never inferred from an old
+ * button label or Chat prose. The executor separately rechecks live admission.
+ */
+export function projectGoalCreationExecutionRequest(params: JsonObject): JsonObject | null {
+ const proposal = requireJsonObject(params.proposal, "original Goal creation");
+ const parameters = requireJsonObject(proposal.normalized_parameters, "original creation parameters");
+ if (parameters.start_execution === undefined || parameters.start_execution === false) return null;
+ if (proposal.status !== "applied") return null;
+ const request = prepareGoalCreationExecutionRequest(params);
+ requireThat(request !== null, "original Task execution request is unavailable");
+ const steps = requireJsonObject(requireJsonObject(proposal.checkpoint, "original creation checkpoint").steps,
+ "original creation steps");
+ const checkpoint = requireJsonObject(steps.original_task_requested, "original Task request checkpoint");
+ const receipt = requireJsonObject(proposal.receipt, "original creation receipt");
+ const resources = requireJsonObject(receipt.resource_ids, "original creation resources");
+ const retained = jsonObject(receipt.original_task_request);
+ const equal = (value: JsonObject | null): boolean => value !== null
+ && Object.keys(value).length === Object.keys(request).length
+ && Object.entries(request).every(([key, expected]) => JSON.stringify(value[key]) === JSON.stringify(expected));
+ const {outcome, source, ...checkpointRequest} = checkpoint;
+ const originalSource = requireJsonObject(source, "original confirmed creation source");
+ const committedSteps = requireJsonObject(receipt.step_receipts, "committed creation checkpoints");
+ requireThat(outcome === "original_task_requested" && equal(checkpointRequest) && equal(retained)
+ && Object.keys(originalSource).length === 3
+ && originalSource.request_digest === proposal.request_digest
+ && originalSource.expected_state_fingerprint === proposal.expected_state_fingerprint
+ && originalSource.upstream_thread_id === requireJsonObject(params.session, "original creation Session").upstream_thread_id
+ && canonicalAuthoritySha256(committedSteps.original_task_requested) === canonicalAuthoritySha256(checkpoint)
+ && receipt.outcome === "goal_created" && receipt.projection_verified === true
+ && resources.goal_id === request.goal_id && resources.goal_instance_id === request.goal_instance_id
+ && resources.agent_id === request.agent_id && resources.managed_agent_id === request.agent_id
+ && resources.session_id === request.session_id && resources.creation_operation_id === request.creation_operation_id
+ && Array.isArray(resources.todo_ids) && resources.todo_ids.length === 1 && resources.todo_ids[0] === request.todo_id,
+ "original Task request checkpoint and committed creation receipt must match exactly");
+ return request;
+}
diff --git a/loopx/control_plane/goals/creation_execution_review.ts b/loopx/control_plane/goals/creation_execution_review.ts
new file mode 100644
index 0000000000..d694d19494
--- /dev/null
+++ b/loopx/control_plane/goals/creation_execution_review.ts
@@ -0,0 +1,47 @@
+/** Pure Goal creation intent and permission review shared with presentation.
+ * The existing action owner confirms this request; native Task admission and
+ * filesystem authority still require their current canonical owners.
+ */
+import type {JsonObject} from "../runtime_decode.ts";
+import {EffectRuntimeRequestError} from "../effect_runtime_errors.ts";
+
+export type GoalCreationPermission = "read_only" | "workspace_write_on_confirmation";
+export type GoalCreationExecutionReview = {
+ requested: true; permission: GoalCreationPermission; writeScope: string[];
+};
+
+export function normalizeGoalCreationExecution(params: JsonObject): JsonObject {
+ const result: JsonObject = {};
+ if (params.permission !== undefined) {
+ if (params.permission !== "read_only" && params.permission !== "workspace_write_on_confirmation") {
+ throw new EffectRuntimeRequestError("permission must select the existing Goal creation read or workspace-write review");
+ }
+ result.permission = params.permission;
+ }
+ if (params.start_execution !== undefined) {
+ if (typeof params.start_execution !== "boolean") {
+ throw new EffectRuntimeRequestError("start_execution must be true or false");
+ }
+ result.start_execution = params.start_execution;
+ }
+ if (params.start_execution === true) {
+ if (params.agent_id !== "codex" || params.permission === undefined) {
+ throw new EffectRuntimeRequestError("original Task start requires the Codex Agent and an explicit permission choice");
+ }
+ if (params.native_token_budget != null) {
+ throw new EffectRuntimeRequestError("select either the LoopX original Task or native host Goal activation for this creation");
+ }
+ if (!Array.isArray(params.initial_todos) || params.initial_todos.length !== 1
+ || typeof params.completion_criteria !== "string" || !params.completion_criteria.trim()) {
+ throw new EffectRuntimeRequestError("original Task start requires one initial Task and its completion criteria");
+ }
+ }
+ return result;
+}
+
+export function goalCreationExecutionReview(parameters: JsonObject): GoalCreationExecutionReview | undefined {
+ if (parameters.start_execution === undefined || parameters.start_execution === false) return undefined;
+ const normalized = normalizeGoalCreationExecution(parameters);
+ return {requested: true, permission: normalized.permission as GoalCreationPermission,
+ writeScope: normalized.permission === "workspace_write_on_confirmation" ? ["**"] : []};
+}
diff --git a/loopx/control_plane/goals/task_validation_plan.ts b/loopx/control_plane/goals/task_validation_plan.ts
index 895e573f68..63d89a9596 100644
--- a/loopx/control_plane/goals/task_validation_plan.ts
+++ b/loopx/control_plane/goals/task_validation_plan.ts
@@ -3,7 +3,7 @@
* pure plan; caller/model input is not authority and the plan grants no execution
* or write permission. */
import type {JsonObject} from "../effect_program.ts";
-import {requireJsonObject} from "../runtime_decode.ts";
+import {jsonObject, requireJsonObject} from "../runtime_decode.ts";
import {EffectRuntimeRequestError} from "../effect_runtime_errors.ts";
import {canonicalAuthoritySha256} from "../coordination/authority_store_codec.ts";
import {acceptanceValidationEffects, type AcceptanceCompletionRequirements} from "./acceptance_contract.ts";
@@ -14,6 +14,25 @@ function requireThat(ok: unknown, message: string): asserts ok {
if (!ok) throw new EffectRuntimeRequestError(message);
}
+/** A display selection from retained creation work, never a newest/first Todo
+ * heuristic or an execution grant. The host must qualify the selected Todo's
+ * current validation through canonicalTaskValidationPlan before advertising it.
+ */
+export function originalCreationTaskSelection(params: JsonObject): JsonObject {
+ const basis = requireJsonObject(params.basis, "current Goal acceptance basis");
+ requireThat(basis.status === "loaded" && typeof basis.provider_revision === "string"
+ && basis.provider_revision.length > 0, "current Goal acceptance basis required");
+ const contract = requireJsonObject(basis.goal_acceptance_contract, "current Goal acceptance projection");
+ const outcomes = jsonObject(contract.outcome_requirements);
+ if (!outcomes || !Array.isArray(outcomes.outcomes)) return {todo_id: null, reason: "original_task_unavailable"};
+ const candidates: unknown[] = outcomes.outcomes;
+ const original = candidates.map(jsonObject).filter((row): row is JsonObject => row?.id === "goal-creation");
+ if (original.length !== 1 || !Array.isArray(original[0].todo_ids)
+ || original[0].todo_ids.length !== 1 || typeof original[0].todo_ids[0] !== "string"
+ || !original[0].todo_ids[0]) return {todo_id: null, reason: "original_task_unavailable"};
+ return {todo_id: original[0].todo_ids[0], reason: null};
+}
+
/** An explicit null requirement means owner-resolved out-of-scope/disabled
* checks. Missing, unbound or stale owner acceptance must never fall back to a
* Todo-only declaration. Required Todo checks remain cumulative with Goal checks. */
diff --git a/loopx/control_plane/heartbeat/builder.py b/loopx/control_plane/heartbeat/builder.py
index 23796716e6..2f1e587d57 100644
--- a/loopx/control_plane/heartbeat/builder.py
+++ b/loopx/control_plane/heartbeat/builder.py
@@ -6,7 +6,7 @@
from pathlib import Path
from typing import Any
-from ...agent_registry import normalize_registered_agents
+from ...agent_registry import load_goal_from_registry, normalize_registered_agents
from ...ark_managed_agent_host import build_ark_managed_agent_host_contract
from ...execution_profile import (
TURN_GRANULARITY_FINE,
@@ -660,6 +660,14 @@ def build_heartbeat_prompt(
if fine_grained:
payload["turn_granularity"] = TURN_GRANULARITY_FINE
payload["turn_mode"] = "fine_grained"
+ if registry_path:
+ from ..coordination.remote_authority_gateway import (
+ get_remote_authority_projection,
+ remote_authority_config,
+ )
+
+ if cfg := remote_authority_config(load_goal_from_registry(Path(registry_path), goal_id)):
+ payload["remote_authority"] = get_remote_authority_projection(goal_id, cfg)
payload["agent_model"] = AgentRuntimeModel.PEER_V1.value
return _prune_prompt_metadata(payload, thin=thin, brief=brief)
diff --git a/loopx/control_plane/presentation/action_review_plan.ts b/loopx/control_plane/presentation/action_review_plan.ts
index ec6059c58c..00ca24d4a6 100644
--- a/loopx/control_plane/presentation/action_review_plan.ts
+++ b/loopx/control_plane/presentation/action_review_plan.ts
@@ -1,6 +1,7 @@
import type {DecisionOutcome, ResumeState} from "../todos/user_completion_types.js";
import {BARE_SHA256_PATTERN} from "../content_digest.ts";
import {goalCreationOutcome, type GoalCreationOutcome} from "../goals/creation_outcome.ts";
+import {goalCreationExecutionReview, type GoalCreationExecutionReview} from "../goals/creation_execution_review.ts";
export type ActionReviewIdentity = {
schemaVersion: "action_review_plan_v0";
@@ -96,6 +97,12 @@ export type ActionReviewPlan = ActionReviewIdentity & ActionReviewState & {
/** Recorded creation effects, not evidence of a completed Goal or model Turn. */
creationProgress?: GoalCreationProgress;
creationOutcome?: GoalCreationOutcome;
+ /** New Session routing only. Recorded Sessions recover through creationProgress;
+ * this identity decision does not certify Task acceptance or execution. */
+ creationSession?: {managedAgentId: string};
+ /** Explicit fresh creation intent; admission and effective approval are
+ * independently checked before the original native Task is dispatched. */
+ creationExecution?: GoalCreationExecutionReview;
/** Accepted dispatch bound to its original Turn. It does not establish a result. */
taskExecution?: { goalId: string; todoId: string; sessionId: string; turnId: string };
};
@@ -541,12 +548,27 @@ export function compileActionReviewPlan(proposalValue: unknown, nowMs?: number):
const operationFrame = compileOperationReviewFrame(proposal, nowMs);
const reviewCardFrame = compileReviewCardFrame(proposal);
const decisionFrame = compileDecisionReviewFrame(proposal);
+ const parameters = objectValue(proposal.normalized_parameters);
const creationProgress = goalCreationProgress(proposal);
+ const creationExecution = proposal.action_kind === "goal.create" && parameters
+ ? goalCreationExecutionReview(parameters) : undefined;
// Older already-dispatched creations retain their original semantics. A
// failed write retries its original CAS even after a response was lost.
const creationOutcome = creationProgress?.todoIds && (!creationProgress.sessionId && !creationProgress.firstTurn
|| creationProgress.completionBasis)
- ? goalCreationOutcome(identity.proposalId, objectValue(proposal.normalized_parameters) ?? {}, creationProgress.todoIds) : undefined;
+ ? goalCreationOutcome(identity.proposalId, parameters ?? {}, creationProgress.todoIds) : undefined;
+ const creationSteps = objectValue(objectValue(proposal.checkpoint)?.steps);
+ // A persisted Session/Turn, including a malformed checkpoint, always owns
+ // recovery. Never infer a new managed identity from its old creation fields.
+ const creationSession = proposal.action_kind === "goal.create" && parameters?.agent_id === "codex"
+ && parameters.native_token_budget == null && proposal.status !== "applied"
+ && !creationProgress?.sessionId && !creationProgress?.firstTurn
+ && !Object.hasOwn(proposal, "first_turn_readback")
+ && (proposal.checkpoint == null || creationSteps !== null
+ && (Object.keys(creationSteps).length === 0 || creationProgress !== undefined))
+ && !Object.hasOwn(creationSteps ?? {}, "first_session_opened")
+ && !Object.hasOwn(creationSteps ?? {}, "first_turn_started")
+ ? {managedAgentId: "codex"} : undefined;
const finish = (state: ActionReviewState): ActionReviewPlan => ({
...identity,
...state,
@@ -555,6 +577,8 @@ export function compileActionReviewPlan(proposalValue: unknown, nowMs?: number):
...(decisionFrame ? { decisionFrame } : {}),
...(creationProgress ? { creationProgress } : {}),
...(creationOutcome ? { creationOutcome } : {}),
+ ...(creationSession ? { creationSession } : {}),
+ ...(creationExecution ? { creationExecution } : {}),
});
const held = (
interaction: "gated" | "refresh" | "repair" | "pending" | "completed" | "inactive",
@@ -589,7 +613,6 @@ export function compileActionReviewPlan(proposalValue: unknown, nowMs?: number):
return taskExecution ? {...completed, taskExecution} : completed;
}
const basis = objectValue(proposal.canonical_update_basis);
- const parameters = objectValue(proposal.normalized_parameters);
// Recovery keeps the frozen workspace and committed effects. The server
// revalidates their identity and reuses native Todo/Turn idempotency owners.
if (proposal.action_kind === "goal.create" && ["failed", "applying"].includes(String(proposal.status))
diff --git a/loopx/control_plane/quota/accounting_artifact_transaction.ts b/loopx/control_plane/quota/accounting_artifact_transaction.ts
index 576deb4323..54867f699d 100644
--- a/loopx/control_plane/quota/accounting_artifact_transaction.ts
+++ b/loopx/control_plane/quota/accounting_artifact_transaction.ts
@@ -1,6 +1,6 @@
import { createHash } from "node:crypto";
-import { access, lstat, readFile } from "node:fs/promises";
-import { basename, dirname, extname, join, resolve } from "node:path";
+import { access, lstat, readFile, realpath } from "node:fs/promises";
+import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep } from "node:path";
import type { JsonObject } from "../effect_program.ts";
import { EffectRuntimeRequestError } from "../effect_runtime_errors.ts";
@@ -109,6 +109,7 @@ export interface QuotaAccountingArtifactCommitRequest {
expectedIndexDigest: string | null;
goalRef?: JsonObject;
indexLockHeld?: boolean;
+ matchesRetainedRequest?: (record: JsonObject) => boolean;
prepare: (
context: QuotaAccountingArtifactPrepareContext,
) =>
@@ -507,6 +508,59 @@ export function resolveQuotaAccountingEffect(
return { kind: "absent" };
}
+export async function readQuotaSpendEventFromRun(
+ runsDir: string,
+ run: JsonObject,
+ goalId: string,
+): Promise {
+ const inline = jsonObject(run.quota_event);
+ if (inline) return inline;
+ if (typeof run.json_path !== "string" || !run.json_path.trim()) return null;
+ let targetPath: string;
+ try {
+ targetPath = await realpath(resolve(run.json_path.trim()));
+ } catch (error) {
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") {
+ return null;
+ }
+ throw error;
+ }
+ const relativePath = relative(await realpath(runsDir), targetPath);
+ if (
+ !relativePath ||
+ relativePath === ".." ||
+ relativePath.startsWith(`..${sep}`) ||
+ isAbsolute(relativePath)
+ ) {
+ throw new EffectRuntimeRequestError(
+ "quota spend json_path must stay inside the goal runs directory",
+ );
+ }
+ let content: string;
+ try {
+ content = await readFile(targetPath, "utf8");
+ } catch (error) {
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") return null;
+ throw error;
+ }
+ let value: unknown;
+ try {
+ value = JSON.parse(content);
+ } catch {
+ throw new EffectRuntimeRequestError("quota spend JSON artifact is malformed");
+ }
+ const record = requiredObject(value, "quota spend JSON artifact");
+ if (
+ record.classification !== contractFor("spend").classification ||
+ (record.goal_id !== undefined && record.goal_id !== goalId)
+ ) {
+ throw new EffectRuntimeRequestError(
+ "quota spend JSON artifact identity does not match its index row",
+ );
+ }
+ return jsonObject(record.quota_event);
+}
+
export async function lookupQuotaAccountingReplay(
kind: QuotaAccountingArtifactKind,
indexPath: string,
@@ -920,7 +974,8 @@ export async function commitQuotaAccountingArtifactTransaction(
}
if (
existingReceipt.effect_id !== request.effectId ||
- existingReceipt.request_digest !== request.requestDigest
+ (existingReceipt.request_digest !== request.requestDigest &&
+ request.matchesRetainedRequest?.(existingReceipt.record) !== true)
) {
return {
status: "conflict",
diff --git a/loopx/control_plane/quota/heartbeat_receipt.py b/loopx/control_plane/quota/heartbeat_receipt.py
index 8eaf717a38..b228abd519 100644
--- a/loopx/control_plane/quota/heartbeat_receipt.py
+++ b/loopx/control_plane/quota/heartbeat_receipt.py
@@ -17,6 +17,7 @@
from .settlement_workspace_causality import (
delivery_workspace_causality_from_event_details,
)
+from .spend_sources import TURN_SCOPED_SLOT_SPEND_SOURCES
HEARTBEAT_RECEIPT_SCHEMA_VERSION = "heartbeat_quota_receipt_v0"
@@ -204,6 +205,7 @@ def ensure_turn_heartbeat_settlement_receipt(
semantic_replan_guard_scoped: bool,
semantic_replan_obligation_id: str | None,
semantic_replan_capability_guard: Mapping[str, object] | None = None,
+ quota_spend_source: str | None = None,
goal_ref: Mapping[str, object] | None = None,
before_bind: Callable[[], None] | None = None,
) -> dict[str, object]:
@@ -224,6 +226,8 @@ def ensure_turn_heartbeat_settlement_receipt(
):
raise ValueError("Turn semantic replan obligation id is malformed")
+ if quota_spend_source is not None and quota_spend_source not in TURN_SCOPED_SLOT_SPEND_SOURCES:
+ raise ValueError("Turn quota spend source must be heartbeat or visible-goal")
log_path = rollout_event_log_path(runtime_root, identity.goal_id)
log_path.parent.mkdir(parents=True, exist_ok=True)
with exclusive_cross_runtime_file_lock(
@@ -248,6 +252,15 @@ def ensure_turn_heartbeat_settlement_receipt(
raise HeartbeatReceiptIdentityConflictError(
"Turn heartbeat receipt belongs to another settlement identity"
)
+ previous_details = effective.get("details") if effective is not None else None
+ previous_source = (
+ previous_details.get("quota_spend_source")
+ if observed is not None and isinstance(previous_details, Mapping) else None
+ )
+ if previous_source is not None and quota_spend_source not in (None, previous_source):
+ raise HeartbeatReceiptIdentityConflictError(
+ "Turn heartbeat receipt belongs to another quota spend source"
+ )
if before_bind is not None:
# The existing cross-runtime binding lock serializes the competing
# identity before transport-specific start admission. Its owner may
@@ -311,6 +324,8 @@ def ensure_turn_heartbeat_settlement_receipt(
details["semantic_replan_capability_id"] = semantic_replan_capability_guard["capability_id"]
details["semantic_replan_gap_id"] = semantic_replan_capability_guard["gap_id"]
details["semantic_replan_frontier_revision"] = semantic_replan_capability_guard["frontier_revision"]
+ if (source := previous_source or quota_spend_source) is not None:
+ details["quota_spend_source"] = source
source_event_id = (
str(effective.get("event_id") or "").strip()
if effective is not None
diff --git a/loopx/control_plane/quota/live_decision.py b/loopx/control_plane/quota/live_decision.py
index 5498b6ea28..cfe569dcfe 100644
--- a/loopx/control_plane/quota/live_decision.py
+++ b/loopx/control_plane/quota/live_decision.py
@@ -7,7 +7,7 @@
from typing import Any
from ...quota import build_quota_should_run
-from ...agent_registry import load_goal_from_registry
+from ...agent_registry import load_goal_from_registry, registered_agent_ids_for_goal
from ..agent_context import project_agent_context, project_goal_agent_context
from ..runtime.time import now_utc_iso
from ..goals.goal_frontier.fallback_source import live_fallback_authority_items
@@ -688,6 +688,9 @@ def build_live_quota_should_run_decision(
) -> dict[str, Any]:
"""Build one live CLI decision while keeping host observation injectable."""
resolved_context = resolve_scheduler_execution_context(scheduler_execution_context)
+ from ..coordination.remote_authority_gateway import remote_authority_config
+
+ registered_goal = load_goal_from_registry(registry_path, goal_id)
app_automation_applicable = (
resolved_context.ok
and resolved_context.context is not None
@@ -725,6 +728,20 @@ def build_live_quota_should_run_decision(
**decision_status_payload,
"bounded_research_frontier": dict(frontier),
}
+ remote_config = remote_authority_config(registered_goal)
+ if remote_config is not None:
+ # Use the same hard-pause precedence and keep fresh local selection
+ # outside the remote route, even when a TLS read succeeds.
+ return _build_remote_authority_wait_decision(
+ decision_status_payload, configured_goal=registered_goal or {},
+ goal_id=goal_id, agent_id=agent_id, remote_config=remote_config,
+ registry_path=registry_path, runtime_root=runtime_root, goal_ref=goal_ref,
+ scheduler_context=resolved_context, available_capabilities=available_capabilities,
+ include_scheduler_detail=include_scheduler_detail,
+ codex_app_rrule=observed_rrule, codex_app_automation_id=observed_automation_id or None,
+ turn_instance_id=turn_instance_id, receipt_bound_todo_id=receipt_bound_todo_id,
+ receipt_bound_replan_obligation_id=receipt_bound_replan_obligation_id,
+ )
settlement_readback = read_heartbeat_settlement(
runtime_root,
goal_id=goal_id,
@@ -754,6 +771,7 @@ def build_live_quota_should_run_decision(
)
if (
receipt_bound_guard_fields["receipt_bound_replan_guard_scoped"]
+ and receipt_bound_replay_phase is ReceiptBoundReplayPhase.OPEN
and receipt_bound_replan_obligation_id
and agent_id
):
@@ -939,7 +957,7 @@ def build_live_quota_should_run_decision(
"agent_id": agent_id,
"todo_id": (payload.get("selected_todo") or {}).get("todo_id"),
}
- goal = load_goal_from_registry(registry_path, goal_id)
+ goal = registered_goal
if goal is not None:
context = project_goal_agent_context(
phase="before_plan",
@@ -977,3 +995,91 @@ def build_live_quota_should_run_decision(
hook_dispatch=turn_start_hook_dispatch, context_navigation=context_navigation,
inline_goal_source=inline_goal_source)
return payload
+
+
+def _build_remote_authority_wait_decision(
+ status_payload: dict[str, Any], *, configured_goal: dict[str, Any],
+ goal_id: str, agent_id: str | None, remote_config: Mapping[str, Any],
+ registry_path: Path, runtime_root: Path, goal_ref: Mapping[str, Any] | None,
+ scheduler_context: SchedulerExecutionContextResolution,
+ available_capabilities: list[str] | None, turn_instance_id: str | None,
+ include_scheduler_detail: bool, codex_app_rrule: str, codex_app_automation_id: str | None,
+ receipt_bound_todo_id: str | None, receipt_bound_replan_obligation_id: str | None,
+) -> dict[str, Any]:
+ from ...quota import _build_quota_plan_for_goal
+ from ..coordination.remote_authority_gateway import (
+ check_remote_authority_gateway_status,
+ get_remote_authority_projection,
+ )
+ from .decision_summary import quota_plan_items
+ from .settlement_precedence import settled_replay_fields
+ from .should_run import build_quota_paused_should_run_payload
+ from .states import quota_item_is_paused
+
+ plan, health_ok = _build_quota_plan_for_goal(status_payload, goal_id=goal_id)
+ item = next((row for row in quota_plan_items(plan) if row.get("goal_id") == goal_id), None)
+ if item is not None and quota_item_is_paused(item):
+ return build_quota_paused_should_run_payload(
+ status_payload, safe_goal_id=goal_id, requested_agent_id=agent_id, item=item, plan=plan,
+ goal_health_ok=health_ok, include_scheduler_detail=include_scheduler_detail,
+ codex_app_current_rrule=codex_app_rrule, codex_app_automation_id=codex_app_automation_id,
+ resolved_scheduler_context=scheduler_context, runtime_root=runtime_root, goal_ref=goal_ref,
+ )
+ scope_matches = (
+ (goal_ref is None or goal_ref.get("goal_instance_id") == configured_goal.get("goal_instance_id"))
+ and agent_id in registered_agent_ids_for_goal(configured_goal)
+ )
+ readback = read_heartbeat_settlement(
+ runtime_root, goal_id=goal_id, agent_id=agent_id, todo_id=receipt_bound_todo_id,
+ turn_instance_id=turn_instance_id, registry_path=registry_path, goal_ref=goal_ref,
+ replan_obligation_id=None if receipt_bound_todo_id else receipt_bound_replan_obligation_id,
+ ) if scope_matches and turn_instance_id else None
+ settled = readback is not None and readback.replay_phase is ReceiptBoundReplayPhase.SETTLED
+ payload: dict[str, Any] = {
+ "ok": True, "mode": "should-run", "goal_id": goal_id, "agent_id": agent_id,
+ "should_run": False, "normal_delivery_allowed": False, "requires_user_action": False,
+ }
+ if goal_ref is not None:
+ payload["goal_ref"] = dict(goal_ref)
+ if scope_matches and not settled:
+ if (receipt_bound_todo_id and agent_id and turn_instance_id and readback is not None
+ and readback.replay_phase is ReceiptBoundReplayPhase.SETTLEMENT_PENDING):
+ attach_settlement_progress(payload, readback, registry_path=registry_path,
+ runtime_root=runtime_root, goal_ref=goal_ref)
+ if apply_receipt_bound_wait_recovery(
+ payload, registry_path=registry_path, runtime_root=runtime_root, goal_id=goal_id,
+ agent_id=agent_id, todo_id=receipt_bound_todo_id, turn_instance_id=turn_instance_id,
+ available_capabilities=available_capabilities, scheduler_execution_context=scheduler_context,
+ monitor_phase=readback.monitor_phase.value if readback.monitor_phase else None,
+ settlement_progress=readback.progress, replay_phase=readback.replay_phase,
+ heartbeat_receipt=readback.heartbeat_receipt,
+ ):
+ return payload
+ if apply_unsettled_host_turn_recovery_if_required(
+ payload, registry_path=registry_path, runtime_root=runtime_root, goal_id=goal_id,
+ agent_id=agent_id, current_turn_instance_id=turn_instance_id,
+ available_capabilities=available_capabilities, scheduler_execution_context=scheduler_context,
+ goal_ref=goal_ref,
+ ):
+ return payload
+
+ if settled:
+ payload.update(settled_replay_fields())
+ else:
+ gateway = (check_remote_authority_gateway_status(goal_id, agent_id, remote_config) if scope_matches else {
+ **get_remote_authority_projection(goal_id, remote_config),
+ "status": "unavailable", "reason_code": "remote_authority_local_scope_mismatch",
+ })
+ reason_code = gateway.get("reason_code", "remote_authority_execution_not_admitted")
+ reason = ("Remote authority read verified; shared Turn admission is unavailable"
+ if gateway["status"] == "verified" else "Waiting for a verified read from the configured remote authority")
+ action = EffectiveAction.BLOCKED_WAIT.value
+ payload.update(decision="wait", state="waiting", effective_action=action,
+ remote_gateway=gateway, reason_code=reason_code, reason=reason, recommended_action=reason,
+ execution_obligation={"kind": action, "must_attempt_work": False, "delivery_allowed": False,
+ "reason": reason, "spend_policy": "no quota spend for remote authority observation"})
+ payload["interaction_contract"] = build_interaction_contract(
+ payload, available_capabilities=available_capabilities, scheduler_execution_context=scheduler_context,
+ turn_instance_id=turn_instance_id, registry_path=str(registry_path), runtime_root=str(runtime_root),
+ )
+ return payload
diff --git a/loopx/control_plane/quota/prior_turn_closeout.ts b/loopx/control_plane/quota/prior_turn_closeout.ts
index 723f548e78..67841d77f8 100644
--- a/loopx/control_plane/quota/prior_turn_closeout.ts
+++ b/loopx/control_plane/quota/prior_turn_closeout.ts
@@ -14,6 +14,7 @@ import {
heartbeatReceiptBinding,
heartbeatReceiptDetails,
heartbeatReceiptFactFromEvent,
+ normalizeHeartbeatReplanObligationId,
selectEffectiveHeartbeatReceipt,
type HeartbeatReceiptBinding,
type HeartbeatReceiptFact,
@@ -30,12 +31,20 @@ export interface PriorHostTurnCloseoutCandidate extends JsonObject {
/** The Todo id or autonomous replan obligation id, per `binding_kind`. */
binding_id: string;
settlement_effect_id: string | null;
+ /** The effective guard admitted a replan under this Todo's settlement identity. */
+ admitted_todo_replan?: true;
+}
+
+interface CloseoutCandidateCheck {
+ candidate: PriorHostTurnCloseoutCandidate;
+ requireWritebackForRecovery: boolean;
}
function candidate(
binding: HeartbeatReceiptBinding,
turnInstanceId: string,
fact: HeartbeatReceiptFact,
+ admittedTodoReplan: boolean,
): PriorHostTurnCloseoutCandidate {
return {
prior_turn_instance_id: turnInstanceId,
@@ -43,21 +52,26 @@ function candidate(
binding_kind: binding.binding_kind,
binding_id: binding.binding_id,
settlement_effect_id: binding.settlement_effect_id,
+ ...(admittedTodoReplan ? {admitted_todo_replan: true as const} : {}),
};
}
function admitsAutonomousReplan(event: JsonObject, fact: HeartbeatReceiptFact): boolean {
- if (fact.replan_obligation_id === null || fact.settlement_effect_id === null) return false;
const guard = heartbeatReceiptDetails(event);
- return guard.semantic_replan_obligation_id === fact.replan_obligation_id
+ const obligationId = normalizeHeartbeatReplanObligationId(guard.semantic_replan_obligation_id);
+ return fact.settlement_effect_id !== null && obligationId !== null
+ && (fact.todo_id !== null || obligationId === fact.replan_obligation_id)
&& guard.interaction_mode === "autonomous_replan" && guard.ok === true && guard.should_run === true
&& guard.must_attempt_work === true && guard.delivery_allowed === false && guard.quiet_noop_allowed === false;
}
/**
- * Select prior Turns eligible for closeout readback. Guard preflight requires
- * an explicit closeout marker. Refresh recovery can also discover an admitted
- * replan-only binding without making it a later Turn's admission prerequisite.
+ * Select prior Turns eligible for closeout readback. A Todo-bound replan
+ * carries the Todo settlement identity and a separate semantic obligation.
+ * It did not deliver a Todo, so its closeout marker can be false. Fresh
+ * admission requires a verified outstanding writeback for this binding.
+ * Explicit pending-closeout discovery can also find admitted replans before
+ * writeback, whether their settlement identity is a Todo or an obligation.
*
* Every prior Turn is identity-validated, so a conflicting receipt fails the
* read instead of being hidden behind a newer Turn. Results are newest first
@@ -74,8 +88,8 @@ export function selectCloseoutCandidates(
goalId: string,
agentId: string,
excludeTurnInstanceId: string | null,
- { includeAdmittedReplans } = { includeAdmittedReplans: false },
-): { candidates: PriorHostTurnCloseoutCandidate[]; turnsValidated: number } {
+ { includeUnwrittenReplans } = { includeUnwrittenReplans: false },
+): { candidates: CloseoutCandidateCheck[]; turnsValidated: number } {
const perTurn = new Map();
const firstRecorded = new Map();
for (const event of receipts) {
@@ -100,7 +114,7 @@ export function selectCloseoutCandidates(
}
}
- const candidates: PriorHostTurnCloseoutCandidate[] = [];
+ const candidates: CloseoutCandidateCheck[] = [];
const currentPosition = excludeTurnInstanceId === null
? undefined : firstRecorded.get(excludeTurnInstanceId);
let validated = 0;
@@ -112,10 +126,14 @@ export function selectCloseoutCandidates(
// must not conceal a conflicting persisted settlement binding.
if (currentPosition !== undefined && firstRecorded.get(turnInstanceId)! >= currentPosition) continue;
const fact = heartbeatReceiptFactFromEvent(effective);
- if (!fact.closeout_required && !(includeAdmittedReplans && admitsAutonomousReplan(effective, fact))) continue;
const binding = heartbeatReceiptBinding(goalId, agentId, fact);
if (binding === null) continue;
- candidates.push(candidate(binding, turnInstanceId, fact));
+ const admittedReplan = admitsAutonomousReplan(effective, fact);
+ const admittedTodoReplan = admittedReplan && binding.binding_kind === "todo";
+ if (!fact.closeout_required && !admittedTodoReplan &&
+ !(includeUnwrittenReplans && admittedReplan)) continue;
+ candidates.push({candidate: candidate(binding, turnInstanceId, fact, admittedTodoReplan),
+ requireWritebackForRecovery: !fact.closeout_required && !includeUnwrittenReplans});
}
return {candidates, turnsValidated: validated};
}
@@ -147,14 +165,18 @@ function bundleFailed(readback: JsonObject, step: string): boolean {
/**
* The closeout receipts a candidate's settlement readback still lacks, or
- * null when that readback already validates the Turn's settlement.
+ * null when the Turn's settlement validates, or undefined when a candidate
+ * without a closeout marker has no verified writeback.
*/
-export function missingCloseoutReceipts(readback: JsonObject): string[] | null {
+export function missingCloseoutReceipts(
+ readback: JsonObject, requireWriteback: boolean,
+): string[] | null | undefined {
if (readback.found === true && !bundleFailed(readback, "settlement")) {
return null;
}
const missingReceipts: string[] = [];
if (readback.found !== true || bundleFailed(readback, "writeback")) {
+ if (requireWriteback) return undefined;
missingReceipts.push(WRITEBACK_RECEIPT);
}
if (readback.found !== true || bundleFailed(readback, "spend")) {
@@ -162,3 +184,8 @@ export function missingCloseoutReceipts(readback: JsonObject): string[] | null {
}
return missingReceipts;
}
+
+/** Only an exact writeback can leave the original debit as the sole debt. */
+export function awaitingQuotaSpend(missing: readonly string[]): boolean {
+ return missing.length === 1 && missing[0] === SPEND_RECEIPT;
+}
diff --git a/loopx/control_plane/quota/scheduler_ack.py b/loopx/control_plane/quota/scheduler_ack.py
index b9bf45a87d..e973714e4d 100644
--- a/loopx/control_plane/quota/scheduler_ack.py
+++ b/loopx/control_plane/quota/scheduler_ack.py
@@ -184,14 +184,25 @@ def _run_native_followup(
host_facts: dict[str, Any],
use_current_hint: bool,
reason_summary: str | None,
+ turn_instance_id: str | None = None,
) -> dict[str, Any]:
- """Decision-free compatibility transport for in-process Python callers."""
+ """Decision-free compatibility transport for in-process Python callers.
+ A receipt-bound Turn keeps the native transaction as the single owner of
+ the missing/stale/superseded receipt fence: it rejects before any
+ scheduler mutation instead of rebuilding a second staleness rule here.
+ """
+
+ receipt_turn_instance_id = (
+ str(turn_instance_id).strip() or None
+ if turn_instance_id is not None
+ else None
+ )
request = {
"schema_version": _FOLLOWUP_REQUEST_SCHEMA,
"runtime_root": str(runtime_root),
- "turn_instance_id": None,
- "require_heartbeat_receipt": False,
+ "turn_instance_id": receipt_turn_instance_id,
+ "require_heartbeat_receipt": receipt_turn_instance_id is not None,
"before": before,
"host_facts": host_facts,
"use_current_hint": use_current_hint,
@@ -298,6 +309,7 @@ def record_quota_scheduler_ack_for_decision(
generated_at: str | None = None,
use_current_hint: bool = False,
host_match_observed: bool = False,
+ turn_instance_id: str | None = None,
) -> dict[str, Any]:
state_key = _followup_state_key(before, surface=surface, state_key=state_key)
safe_agent_id = normalize_todo_claimed_by(agent_id)
@@ -367,6 +379,7 @@ def record_quota_scheduler_ack_for_decision(
host_facts=facts,
use_current_hint=use_current_hint,
reason_summary=reason_summary,
+ turn_instance_id=turn_instance_id,
)
except ValueError as exc:
return _failure_payload(
@@ -396,6 +409,7 @@ def record_quota_scheduler_failure_for_decision(
observed_host_rrule: str | None = None,
failure_kind: str = "host_tool_failure",
generated_at: str | None = None,
+ turn_instance_id: str | None = None,
) -> dict[str, Any]:
state_key = _followup_state_key(before, surface=surface, state_key=state_key)
safe_agent_id = normalize_todo_claimed_by(agent_id)
@@ -443,6 +457,7 @@ def record_quota_scheduler_failure_for_decision(
host_facts=facts,
use_current_hint=False,
reason_summary=None,
+ turn_instance_id=turn_instance_id,
)
except ValueError as exc:
return _failure_payload(
diff --git a/loopx/control_plane/quota/settlement.py b/loopx/control_plane/quota/settlement.py
index 389bda1c0f..f0563d3fe5 100644
--- a/loopx/control_plane/quota/settlement.py
+++ b/loopx/control_plane/quota/settlement.py
@@ -152,6 +152,7 @@ class QuotaSpendPrecheck:
settlement: SettlementResult[dict[str, Any]]
progress: dict[str, Any] | None
spend_run_present: bool
+ spend_request_rejection: SettlementResult[dict[str, Any]] | None
@dataclass(frozen=True, slots=True)
@@ -424,6 +425,7 @@ def read_quota_spend_precheck(
allow_unbound_binding: bool = False,
registry_path: Path | None = None,
goal_ref: Mapping[str, Any] | None = None,
+ spend_request: Mapping[str, Any] | None = None,
) -> QuotaSpendPrecheck | None:
"""Read the identity and replay controls before building a fresh preview."""
@@ -438,6 +440,7 @@ def read_quota_spend_precheck(
"infer_turn_instance_id": infer_turn_instance_id,
"allow_unbound_binding": allow_unbound_binding,
"spend_precheck": True,
+ **({"spend_request": dict(spend_request)} if spend_request is not None else {}),
},
registry_path=registry_path,
goal_ref=goal_ref,
@@ -452,6 +455,10 @@ def read_quota_spend_precheck(
settlement=_readback_result(payload.get("settlement")),
progress=_optional_readback_record(payload.get("progress")),
spend_run_present=spend_run_present,
+ spend_request_rejection=(
+ _readback_result(payload["spend_request_rejection"])
+ if "spend_request_rejection" in payload else None
+ ),
)
diff --git a/loopx/control_plane/quota/settlement_readback.ts b/loopx/control_plane/quota/settlement_readback.ts
index 827f1b0ee4..10bd891ae3 100644
--- a/loopx/control_plane/quota/settlement_readback.ts
+++ b/loopx/control_plane/quota/settlement_readback.ts
@@ -22,6 +22,7 @@ import {
} from "../effect_program.ts";
import { EffectRuntimeRequestError } from "../effect_runtime_errors.ts";
import { hasMonitorPollTransaction } from "./monitor_poll_commit.ts";
+import { readQuotaSpendEventFromRun } from "./accounting_artifact_transaction.ts";
import {
jsonObject,
requireJsonObject,
@@ -1110,7 +1111,7 @@ function owedPriorTurnFromSnapshot(
snapshot: QuotaSettlementReadbackSnapshot,
agentId: string,
turnInstanceId: string | null,
- options?: { includeAdmittedReplans: boolean },
+ options?: { includeUnwrittenReplans: boolean },
): OwedPriorTurn | null {
const receipts = heartbeatReceiptsFromEvents(snapshot.events, request.goal_id, agentId)
.filter((event) => quotaOwnerOwnsProjection(request.owner, event.goal_ref));
@@ -1121,7 +1122,7 @@ function owedPriorTurnFromSnapshot(
turnInstanceId,
options,
);
- for (const selected of candidates) {
+ for (const {candidate: selected, requireWritebackForRecovery} of candidates) {
// A committed monitor poll is its own exact no-spend closeout. This is the
// same acceptance rule the next-Turn preflight applies; using only the
// advancement writeback/spend chain here incorrectly made every later
@@ -1149,9 +1150,9 @@ function owedPriorTurnFromSnapshot(
},
snapshot,
false,
- ))
+ ), requireWritebackForRecovery)
: null;
- if (missingReceipts !== null) {
+ if (missingReceipts != null) {
return { ...selected, missing_receipts: missingReceipts };
}
}
@@ -1169,7 +1170,7 @@ function readPendingCloseoutFromRequest(
}
const notFound = {schema_version: QUOTA_SETTLEMENT_READBACK_RESULT_SCHEMA, found: false};
const turn = request.turn_instance_id ?? owedPriorTurnFromSnapshot(
- request, snapshot, explicitAgentId, null, { includeAdmittedReplans: true },
+ request, snapshot, explicitAgentId, null, { includeUnwrittenReplans: true },
)?.prior_turn_instance_id;
if (!turn) return notFound;
// Selection, original binding validation and receipt progress share this
@@ -1489,10 +1490,17 @@ export async function readQuotaSettlement(value: unknown): Promise {
return prepareBlockedWait(value);
}
const request = decodeRequest(value);
- const spendPrecheck = jsonObject(value)?.spend_precheck;
+ const input = requireJsonObject(value, "quota settlement readback request");
+ const spendPrecheck = input.spend_precheck;
if (spendPrecheck !== undefined && typeof spendPrecheck !== "boolean") {
throw new EffectRuntimeRequestError("spend_precheck must be a boolean");
}
+ const spendRequest = input.spend_request === undefined ? null
+ : requireJsonObject(input.spend_request, "spend_request");
+ if (spendRequest !== null && (!spendPrecheck ||
+ !Number.isSafeInteger(spendRequest.slots) || typeof spendRequest.source !== "string")) {
+ throw new EffectRuntimeRequestError("spend_request requires spend_precheck, integer slots and a source");
+ }
if (spendPrecheck) request.include_record_bodies = true;
const withOwner = request.borrow_source_admission
? withBorrowedQuotaAccountingOwner
@@ -1518,14 +1526,60 @@ export async function readQuotaSettlement(value: unknown): Promise {
// only its receipts and whether a spend Run already needs receipt repair.
const settlement = requireJsonObject(result.settlement, "quota settlement bundle");
const outcome = requireJsonObject(settlement.result, "quota settlement result");
+ const spendRun = jsonObject(result.spend_run);
+ const spendRejection = spendRequest && identity
+ ? await originalSpendRejection(request, result, spendRequest, spendRun)
+ : null;
return {
schema_version: result.schema_version,
found: true,
identity: result.identity,
settlement: {...settlement, result: {...outcome, value: null}},
progress: result.progress,
- spend_run_present: jsonObject(result.spend_run) !== null,
+ spend_run_present: spendRun !== null,
+ ...(spendRejection === null ? {} : {spend_request_rejection: bundle(spendRejection)}),
};
},
);
}
+
+async function originalSpendRejection(
+ request: ReadbackRequest, readback: JsonObject, proposed: JsonObject,
+ spendRun: JsonObject | null,
+): Promise | null> {
+ const progress = requireJsonObject(readback.progress, "quota settlement progress");
+ const guard = jsonObject(readback.heartbeat_receipt);
+ const authorizedSource = guard ? details(guard).quota_spend_source : null;
+ // Older ledgers can have a rollout receipt but no readable artifact. Keep
+ // their receipt facts available and never repair an already mismatched debit.
+ const spendEvent = jsonObject(readback.spend_event);
+ const receipt = spendEvent ? details(spendEvent) : {};
+ const retained = spendRun === null ? null : await readQuotaSpendEventFromRun(
+ join(request.runtime_root, "goals", request.goal_id, "runs"), spendRun, request.goal_id,
+ );
+ const recordedSlots = retained?.slots ?? (receipt.slots === "" ? undefined : receipt.slots);
+ const recordedSource = retained?.source ?? (receipt.source === "" ? undefined : receipt.source);
+ const fallbackSource = recordedSource === "heartbeat" || recordedSource === "visible-goal"
+ ? recordedSource : progress.quota_spend_source;
+ const source = authorizedSource ?? fallbackSource;
+ // Every admitted CLI settlement plan authorizes one slot and the original
+ // source. The same check gates initial spend, receipt repair, and replay.
+ if (proposed.slots !== 1 || proposed.source !== source) {
+ return settlementFailed({
+ kind: "quota_spend_rejected", step_kind: "quota_spend",
+ reason: `original Turn settlement requires --slots 1 --source ${String(source)}`,
+ });
+ }
+ const slotsConflict = recordedSlots !== undefined && recordedSlots !== proposed.slots;
+ const sourceConflict = recordedSource !== undefined && recordedSource !== source;
+ // Historical complete receipts may lack the amount projection. A partial
+ // debit needs the original charge facts before we can certify its receipt.
+ const unverifiableRepair = spendRun !== null && spendEvent === null &&
+ (recordedSlots === undefined || recordedSource === undefined);
+ return slotsConflict || sourceConflict || unverifiableRepair ? settlementFailed({
+ kind: "quota_spend_rejected", step_kind: "quota_spend",
+ reason: unverifiableRepair
+ ? "the recorded debit has no verifiable billing terms for receipt repair"
+ : "the recorded debit differs from the original Turn settlement plan",
+ }) : null;
+}
diff --git a/loopx/control_plane/quota/should_run_prepare.py b/loopx/control_plane/quota/should_run_prepare.py
index 0d856b0265..d22528a4d8 100644
--- a/loopx/control_plane/quota/should_run_prepare.py
+++ b/loopx/control_plane/quota/should_run_prepare.py
@@ -260,6 +260,11 @@ def _prepare_quota_replan_context(
replan_explore_reader: Callable[[str, str | None], Mapping[str, Any] | None] | None,
) -> tuple[dict[str, Any], dict[str, Any] | None]:
"""Project the current frontier and contextualize its receipt-bound review."""
+ # Once the settlement owner verifies writeback, an old review's sources
+ # cannot be prerequisites for spending its receipt (or replaying it).
+ reconstruct_review = receipt_bound_replay_phase not in (
+ ReceiptBoundReplayPhase.SETTLEMENT_PENDING, ReceiptBoundReplayPhase.SETTLED,
+ )
goal_frontier_context = build_goal_frontier_projection_context_from_status(
replan_explore_reader=replan_explore_reader,
goal_id=safe_goal_id,
@@ -277,19 +282,23 @@ def _prepare_quota_replan_context(
registered_agent_ids=registered_agent_ids,
goal_status=str(registry_goal.get("status") or ""),
agent_profile=_quota_agent_profile(agent_identity),
- receipt_bound_replan_obligation_id=receipt_bound_replan_obligation_id,
+ receipt_bound_replan_obligation_id=(
+ receipt_bound_replan_obligation_id if reconstruct_review else None
+ ),
receipt_bound_guard_recorded_at=receipt_bound_guard_recorded_at,
)
replan_obligation = (
None
if receipt_bound_replay_phase is ReceiptBoundReplayPhase.SETTLED
else _preserve_receipt_bound_replan_obligation(
- goal_frontier_context.get("replan_obligation"),
+ goal_frontier_context.get("replan_obligation") if reconstruct_review else None,
receipt_bound_replan_obligation_id,
guard_scoped=receipt_bound_replan_guard_scoped,
agent_id=agent_frontier_id,
replay_phase=receipt_bound_replay_phase,
- transition_candidates=goal_frontier_context.get("replan_transition_candidates"),
+ transition_candidates=(
+ goal_frontier_context.get("replan_transition_candidates") if reconstruct_review else None
+ ),
stride_strategy=goal_frontier_context["stride_strategy"],
receipt_review=receipt_bound_replan_review,
goal_id=safe_goal_id,
diff --git a/loopx/control_plane/quota/spend_commit.ts b/loopx/control_plane/quota/spend_commit.ts
index 7a4f7eab4a..4f201a0190 100644
--- a/loopx/control_plane/quota/spend_commit.ts
+++ b/loopx/control_plane/quota/spend_commit.ts
@@ -361,6 +361,43 @@ function requestDigest(request: QuotaSpendCommitRequest): string {
}));
}
+function matchesRetainedSettlementSpend(
+ request: QuotaSpendCommitRequest,
+ proposedRecord: JsonObject,
+ retainedRecord: JsonObject,
+): boolean {
+ const identity = jsonObject(proposedRecord.settlement_identity);
+ const turnId = optionalString(identity?.turn_instance_id, "settlement_identity.turn_instance_id");
+ const effectId = optionalString(identity?.effect_id, "settlement_identity.effect_id");
+ if (!turnId || !effectId ||
+ (request.source !== "heartbeat" && request.source !== "visible-goal") ||
+ request.preview.effect_ref != null || request.preview.delivery_completion_spend !== true ||
+ request.effect_id !== `${effectId}#quota_spend` ||
+ identity?.goal_id !== request.goal_id || identity?.agent_id !== request.resolved_agent_id ||
+ identity?.turn_instance_id !== proposedRecord.turn_instance_id ||
+ (identity?.todo_id || null) !== (proposedRecord.todo_id ?? null) ||
+ (identity?.replan_obligation_id ?? null) !== (proposedRecord.replan_obligation_id ?? null)) {
+ return false;
+ }
+ const proposed = requiredObject(proposedRecord.quota_event, "proposed spend event");
+ const retained = requiredObject(retainedRecord.quota_event, "retained spend event");
+ // The live quota projection can change between two attempts to settle one
+ // validated writeback. The original identity and accounting facts cannot.
+ return retainedRecord.classification === QUOTA_SLOT_SPENT_CLASSIFICATION &&
+ retainedRecord.goal_id === request.goal_id &&
+ retainedRecord.agent_id === request.resolved_agent_id &&
+ canonicalJson(retainedRecord.settlement_identity) === canonicalJson(identity) &&
+ retained.source === proposed.source && retained.slots === proposed.slots &&
+ typeof proposed.delivery_run_generated_at === "string" &&
+ typeof proposed.delivery_run_classification === "string" &&
+ proposed.delivery_run_agent_id === identity.agent_id &&
+ retained.delivery_run_generated_at === proposed.delivery_run_generated_at &&
+ retained.delivery_run_classification === proposed.delivery_run_classification &&
+ retained.delivery_run_agent_id === proposed.delivery_run_agent_id &&
+ canonicalJson(retained.delivery_workspace) === canonicalJson(proposed.delivery_workspace) &&
+ retained.delivery_workspace_validated === proposed.delivery_workspace_validated;
+}
+
function positiveSlots(preview: JsonObject): number {
const value = preview.slots;
if (typeof value !== "number" || !Number.isInteger(value) || value < 1) {
@@ -851,6 +888,7 @@ export async function evaluateQuotaSpendCommit(
expectedIndexDigest: request.expected_index_digest,
...(goalRef === null ? {} : { goalRef }),
indexLockHeld,
+ matchesRetainedRequest: (retained) => matchesRetainedSettlementSpend(request, record, retained),
prepare: ({ jsonPath, markdownPath, indexPath: lockedIndexPath }) => {
const payload = payloadFor(
request,
@@ -915,6 +953,7 @@ export async function evaluateQuotaSpendCommit(
appended: repaired,
idempotent_replay: !repaired,
transaction_repaired: repaired,
+ ...(outcome.receipt.record.settlement_identity ? {receipt_repair_required: true} : {}),
reason: repaired
? "quota spend commit repaired its prepared durable transaction"
: "quota spend commit replayed for the same effect identity",
diff --git a/loopx/control_plane/quota/turn_envelope.ts b/loopx/control_plane/quota/turn_envelope.ts
index b4ed32dd2d..7d5bdc2f3a 100644
--- a/loopx/control_plane/quota/turn_envelope.ts
+++ b/loopx/control_plane/quota/turn_envelope.ts
@@ -1136,7 +1136,7 @@ function requiredReads(interaction: JsonObject, payload: JsonObject): JsonObject
return result;
}
-function boundary(payload: JsonObject): JsonObject {
+export function projectTurnBoundary(payload: JsonObject): JsonObject {
const source = object(payload.goal_boundary);
const result: JsonObject = {
rule: scalarString(source.rule, "goal_boundary.rule", "stay_in_scope_or_stop"),
@@ -1482,7 +1482,7 @@ function actionProjection(payload: JsonObject, deferSettlement = true, precision
user,
required_reads: requiredReads(interaction, payload),
replan_action_packet: replanPacket,
- boundary: boundary(payload),
+ boundary: projectTurnBoundary(payload),
execution_policy: executionPolicy(payload),
writeback: reads && deferSettlement ? deferredReplanWriteback(writeback) : writeback,
scheduler: schedulerValue,
@@ -1957,6 +1957,7 @@ export function completePlanningFacts(request: JsonObject): JsonObject {
return {...lineage, work, source_decision_hash: decisionSourceHash(decision),
action_source_hash: canonicalHash(turnEnvelopeActionSignatureDocument(work)),
scoped_settlement_identity: scopedIdentity, scoped_identity_error: identityError,
+ ...(request.include_selected_work_basis === true ? {selected_work_basis: selectedWorkLaunchBasis(decision)} : {}),
...(request.inspect_plan === true &&
(lineage.route === "ready_for_host" || lineage.route === "replan_required")
? {inspection_paths: turnPlanInspectionPaths(decision, lineage.route)} : {})};
@@ -2010,6 +2011,19 @@ function completeTaskTextSha256(decision: JsonObject, todoId: unknown): string |
return capturedTodoTextSha256(declared, context, decision.recommended_action, todoId);
}
+/** Launch rechecks the admitted task, not the provider revision or the bounded
+ * action signature. Progress, diagnostics and receipt changes are not edits to
+ * the work. Losing the verified body on a fresh read is not a match. */
+function selectedWorkLaunchBasis(decision: JsonObject, verifiedTextSha256?: string | null): string | null {
+ const todoId = selectedWorkTodoId(decision);
+ const selected = object(decision.selected_todo);
+ if (!todoId || todoId !== selected.todo_id) return null;
+ const textSha256 = verifiedTextSha256 === undefined ? completeTaskTextSha256(decision, todoId) : verifiedTextSha256;
+ return textSha256 === null ? null : canonicalHash({todo_id: todoId, text_sha256: textSha256,
+ required_write_scopes: selected.required_write_scopes ?? [],
+ required_capabilities: selected.required_capabilities ?? []});
+}
+
export function composeCompleteTurnDecision(value: unknown): JsonObject {
const request = requireJsonObject(value, "complete Turn decision request");
const payload = requireJsonObject(request.payload, "complete Turn decision source");
@@ -2655,8 +2669,9 @@ export function evaluateTurnEnvelope(value: unknown): JsonObject {
const {decision, projection} = completeHostSource(request);
const work = {selected_todo: object(projection.action).selected_todo,
task_orchestration_contract: projection.task_orchestration_contract};
- return {selected_todo: selectedWorkTodo(work),
- sha256: completeTaskTextSha256(decision, selectedWorkTodoId(work) ?? ""),
+ const sha256 = completeTaskTextSha256(decision, selectedWorkTodoId(work) ?? "");
+ return {selected_todo: selectedWorkTodo(work), sha256,
+ selected_work_basis: selectedWorkLaunchBasis(decision, sha256),
effective_action: decision.effective_action ?? null,
writeback: object(projection.writeback),
task_lease_proof: object(object(decision.execution_preparation).lease_proof),
diff --git a/loopx/control_plane/quota/unsettled_host_turn_recovery.ts b/loopx/control_plane/quota/unsettled_host_turn_recovery.ts
index 9cb4f9b55d..935729ec1f 100644
--- a/loopx/control_plane/quota/unsettled_host_turn_recovery.ts
+++ b/loopx/control_plane/quota/unsettled_host_turn_recovery.ts
@@ -33,6 +33,7 @@ import {
type HeartbeatReceiptBinding,
} from "./heartbeat_receipt_identity.ts";
import {
+ awaitingQuotaSpend,
candidateReadbackBinding,
MISSING_RECEIPT_NAMES,
missingCloseoutReceipts,
@@ -164,27 +165,21 @@ async function preflightPriorHostTurnCloseoutForOwner(
request.agent_id,
request.exclude_turn_instance_id,
);
- if (candidates.length === 0) {
- return {
- schema_version: PRIOR_HOST_TURN_CLOSEOUT_PREFLIGHT_RESULT_SCHEMA,
- status: "none",
- reason: "no_prior_turn_requires_closeout",
- turns_validated: turnsValidated,
- };
- }
- const settlementSnapshot = await readQuotaSettlementSnapshot(
- request.runtime_root,
- request.goal_id,
- rolloutSnapshot,
- );
+ // Resolve one snapshot on the first candidate. The empty path needs only
+ // the guard log, and unwritten replans may all drop out after validation.
+ let settlementSnapshot: Awaited> | undefined;
let newestSettledTurn: string | null = null;
let newestAcceptedCloseout: AcceptedCloseout = "validated_writeback_and_quota_spend";
- for (const selected of candidates) {
+ for (const {candidate: selected, requireWritebackForRecovery} of candidates) {
+ settlementSnapshot ??= await readQuotaSettlementSnapshot(
+ request.runtime_root, request.goal_id, rolloutSnapshot,
+ );
const readback = readAdmittedQuotaSettlementFromSnapshot(
settlementReadbackRequest(request, selected),
settlementSnapshot,
);
- const missingReceipts = missingCloseoutReceipts(readback);
+ const missingReceipts = missingCloseoutReceipts(readback, requireWritebackForRecovery);
+ if (missingReceipts === undefined) continue;
if (missingReceipts === null) {
// A newer settled Turn cannot hide an older missing closeout. Keep
// scanning in persisted newest-first order until the first unsettled
@@ -218,10 +213,12 @@ async function preflightPriorHostTurnCloseoutForOwner(
return {
schema_version: PRIOR_HOST_TURN_CLOSEOUT_PREFLIGHT_RESULT_SCHEMA,
status: "none",
- reason: "prior_turn_settlement_validated",
turns_validated: turnsValidated,
- prior_turn_instance_id: newestSettledTurn,
- accepted_closeout: newestAcceptedCloseout,
+ ...(newestSettledTurn === null ? {reason: "no_prior_turn_requires_closeout"} : {
+ reason: "prior_turn_settlement_validated",
+ prior_turn_instance_id: newestSettledTurn,
+ accepted_closeout: newestAcceptedCloseout,
+ }),
};
}
@@ -257,6 +254,13 @@ function decodeCandidate(value: unknown): PriorHostTurnCloseoutCandidate {
"prior host Turn recovery candidate autonomous replan binding is malformed",
);
}
+ const settlementEffectId = optionalHeartbeatString(raw.settlement_effect_id);
+ if (raw.admitted_todo_replan !== undefined &&
+ (raw.admitted_todo_replan !== true || bindingKind !== "todo" || settlementEffectId === null)) {
+ throw new EffectRuntimeRequestError(
+ "prior host Turn recovery candidate has an invalid Todo-bound replan fact",
+ );
+ }
return {
prior_turn_instance_id: requireNonEmptyString(
raw.prior_turn_instance_id,
@@ -265,7 +269,8 @@ function decodeCandidate(value: unknown): PriorHostTurnCloseoutCandidate {
event_id: optionalHeartbeatString(raw.event_id),
binding_kind: bindingKind,
binding_id: bindingId,
- settlement_effect_id: optionalHeartbeatString(raw.settlement_effect_id),
+ settlement_effect_id: settlementEffectId,
+ ...(raw.admitted_todo_replan === true ? {admitted_todo_replan: true as const} : {}),
};
}
@@ -368,8 +373,9 @@ function recoveryRepair(
bindingKind: HeartbeatReceiptBinding["binding_kind"],
todo: CandidateTodoFacts | null,
): RecoveryRepair {
- if (bindingKind !== "todo" || todo === null) return "lifecycle";
- if (todo.task_class === MONITOR_TASK_CLASS) return "monitor_poll";
+ if (bindingKind !== "todo") return "lifecycle";
+ if (todo?.task_class === MONITOR_TASK_CLASS) return "monitor_poll";
+ if (todo === null) return "lifecycle";
// Missing receipts do not prove an external dependency. Re-enter
// the original guard to recheck eligibility and retain its settlement id.
// This is a recovery route, never an accepted closeout or delivery grant.
@@ -420,7 +426,9 @@ function recoveryObligation(
*
* The accepted closeout order is policy: a validated settlement wins, then an
* exactly committed monitor-poll closeout, then a typed lifecycle transition.
- * Anything else is a recovery obligation with the exact missing receipts named.
+ * For a Todo-bound autonomous replan, a verified writeback keeps its debit
+ * obligation even when the Todo has changed. Ordinary lifecycle closeouts
+ * retain their existing meaning.
*/
export function reduceUnsettledHostTurnRecovery(value: unknown): JsonObject {
const request = requireJsonObject(value, "prior host Turn recovery request");
@@ -465,7 +473,8 @@ export function reduceUnsettledHostTurnRecovery(value: unknown): JsonObject {
};
}
- const lifecycleCloseout = acceptedLifecycleCloseout(todo);
+ const replanSpendOwed = selected.admitted_todo_replan === true && awaitingQuotaSpend(missingReceipts);
+ const lifecycleCloseout = replanSpendOwed ? null : acceptedLifecycleCloseout(todo);
if (lifecycleCloseout !== null) {
return {
schema_version: UNSETTLED_HOST_TURN_RECOVERY_RESULT_SCHEMA,
@@ -494,7 +503,7 @@ export function reduceUnsettledHostTurnRecovery(value: unknown): JsonObject {
recovery.binding_target_key = todo.target_key;
recovery.binding_cadence = todo.cadence;
}
- const repair = recoveryRepair(selected.binding_kind, todo);
+ const repair = replanSpendOwed ? "resume_prior_turn" : recoveryRepair(selected.binding_kind, todo);
// The typed repair lane lets the CLI renderer project its command list
// without re-deriving which closeout family the Turn belongs to.
recovery.repair = repair;
diff --git a/loopx/control_plane/quota/void_commit.ts b/loopx/control_plane/quota/void_commit.ts
index abb056decf..ba49176d59 100644
--- a/loopx/control_plane/quota/void_commit.ts
+++ b/loopx/control_plane/quota/void_commit.ts
@@ -1,6 +1,6 @@
import { createHash } from "node:crypto";
-import { readFile, realpath } from "node:fs/promises";
-import { basename, isAbsolute, join, relative, resolve, sep } from "node:path";
+import { readFile } from "node:fs/promises";
+import { basename, isAbsolute, join } from "node:path";
import type { JsonObject } from "../effect_program.ts";
import { EffectRuntimeRequestError } from "../effect_runtime_errors.ts";
@@ -9,6 +9,7 @@ import {
nextQuotaAccountingArtifactPaths,
parseQuotaAccountingIndex,
quotaAccountingIndexDigest,
+ readQuotaSpendEventFromRun,
renderQuotaSlotMarkdown,
type QuotaAccountingArtifactPrepareContext,
type QuotaAccountingArtifactPreparation,
@@ -365,59 +366,6 @@ function compactDecision(value: unknown): JsonObject {
};
}
-async function readTargetEvent(
- runsDir: string,
- run: JsonObject,
- goalId: string,
-): Promise {
- const inline = jsonObject(run.quota_event);
- if (inline) return inline;
- if (typeof run.json_path !== "string" || !run.json_path.trim()) return null;
- let targetPath: string;
- try {
- targetPath = await realpath(resolve(run.json_path.trim()));
- } catch (error) {
- if (error instanceof Error && "code" in error && error.code === "ENOENT") {
- return null;
- }
- throw error;
- }
- const relativePath = relative(await realpath(runsDir), targetPath);
- if (
- !relativePath ||
- relativePath === ".." ||
- relativePath.startsWith(`..${sep}`) ||
- isAbsolute(relativePath)
- ) {
- throw new EffectRuntimeRequestError(
- "quota spend json_path must stay inside the goal runs directory",
- );
- }
- let content: string;
- try {
- content = await readFile(targetPath, "utf8");
- } catch (error) {
- if (error instanceof Error && "code" in error && error.code === "ENOENT") return null;
- throw error;
- }
- let value: unknown;
- try {
- value = JSON.parse(content);
- } catch {
- throw new EffectRuntimeRequestError("quota spend JSON artifact is malformed");
- }
- const record = requiredObject(value, "quota spend JSON artifact");
- if (
- record.classification !== QUOTA_SLOT_SPENT_CLASSIFICATION ||
- (record.goal_id !== undefined && record.goal_id !== goalId)
- ) {
- throw new EffectRuntimeRequestError(
- "quota spend JSON artifact identity does not match its index row",
- );
- }
- return jsonObject(record.quota_event);
-}
-
async function findTargetSpend(
runsDir: string,
records: readonly JsonObject[],
@@ -429,7 +377,7 @@ async function findTargetSpend(
if (String(run.goal_id || goalId) !== goalId) continue;
if (String(run.generated_at ?? "") !== generatedAt) continue;
if (run.classification !== QUOTA_SLOT_SPENT_CLASSIFICATION) continue;
- const event = await readTargetEvent(runsDir, run, goalId);
+ const event = await readQuotaSpendEventFromRun(runsDir, run, goalId);
if (event?.event_type !== QUOTA_SLOT_SPENT_CLASSIFICATION) continue;
requireQuotaOwnerProjection(owner, run.goal_ref, "quota void target");
requireQuotaOwnerProjection(
diff --git a/loopx/control_plane/scheduler/arbitration.py b/loopx/control_plane/scheduler/arbitration.py
index d9ee86f085..400f669fe3 100644
--- a/loopx/control_plane/scheduler/arbitration.py
+++ b/loopx/control_plane/scheduler/arbitration.py
@@ -6,6 +6,7 @@
from typing import Any
from ..agents.agent_scope_frontier import AGENT_SCOPE_FRONTIER_SCHEMA_VERSION, AgentScopeFrontierAction
+from ..effect_runtime import effect_runtime_result
from ..goals.goal_vision_wait import exact_blocked_successor_wait_state
SCHEDULER_ARBITRATION_SCHEMA_VERSION = "scheduler_arbitration_v0"
@@ -110,3 +111,14 @@ def _scheduler_arbitration_facts(
"vision_wait_state": wait or None, "scope_wait_todo": scoped_wait,
"heartbeat_receipt": payload.get("heartbeat_receipt")})
return facts
+
+
+def monitor_quiet_stall_observation_required(payload: dict[str, Any]) -> bool:
+ """Keep automatic stall supervision unless typed arbitration proves a future wait."""
+ if not exact_blocked_successor_wait_state(payload):
+ return True
+ result = effect_runtime_result(
+ "work_item.blocked_wait_observation.project",
+ _scheduler_arbitration_facts(payload.get("interaction_contract"), payload=payload),
+ )
+ return result["required"] is not False
diff --git a/loopx/control_plane/scheduler/state.py b/loopx/control_plane/scheduler/state.py
index c00c5a6d84..cfeecac179 100644
--- a/loopx/control_plane/scheduler/state.py
+++ b/loopx/control_plane/scheduler/state.py
@@ -252,6 +252,7 @@ def _store_result(
agent_id: str,
surface: str,
state_key: str,
+ migrate_legacy: bool | None = None,
) -> Mapping[str, Any]:
params: dict[str, Any] = {
"schema_version": SCHEDULER_STATE_STORE_REQUEST_SCHEMA,
@@ -265,6 +266,8 @@ def _store_result(
}
if state is not None:
params["state"] = state
+ if migrate_legacy is not None:
+ params["migrate_legacy"] = migrate_legacy
try:
result = effect_runtime_result(f"scheduler.state.{operation}", params)
except EffectRuntimeRejected as exc:
@@ -287,6 +290,7 @@ def load_scheduler_state(
surface: str = CODEX_APP_SURFACE,
state_key: str = CODEX_APP_STATEFUL_BACKOFF_STATE_KEY,
read_observer: SchedulerStateReadObserver | None = None,
+ migrate_legacy: bool | None = None,
) -> dict[str, Any] | None:
if not agent_id:
return None
@@ -298,6 +302,7 @@ def load_scheduler_state(
agent_id=agent_id,
surface=surface,
state_key=state_key,
+ migrate_legacy=migrate_legacy,
)
state = result.get("state")
if state is not None and not isinstance(state, dict):
@@ -317,6 +322,8 @@ def load_app_automation_scheduler_state(
surface: str,
read_observer: SchedulerStateReadObserver | None = None,
) -> dict[str, Any] | None:
+ # Hint reconstruction may precede a preview or receipt rejection.
+ # Legacy migration belongs to the authorized native commit under lock.
current = load_scheduler_state(
runtime_root,
goal_id=goal_id,
@@ -324,6 +331,7 @@ def load_app_automation_scheduler_state(
surface=surface,
state_key=APP_AUTOMATION_STATEFUL_BACKOFF_STATE_KEY,
read_observer=read_observer,
+ migrate_legacy=False,
)
if current is not None or surface != CODEX_APP_SURFACE:
return current
@@ -337,6 +345,7 @@ def load_app_automation_scheduler_state(
surface=surface,
state_key=LEGACY_CODEX_APP_STATEFUL_BACKOFF_STATE_KEY,
read_observer=read_observer,
+ migrate_legacy=False,
)
diff --git a/loopx/control_plane/state_recovery.ts b/loopx/control_plane/state_recovery.ts
index c18d21ce8e..2bb65dfc82 100644
--- a/loopx/control_plane/state_recovery.ts
+++ b/loopx/control_plane/state_recovery.ts
@@ -18,7 +18,6 @@ import {STATE_RECOVERY_AUDIT_SCHEMA, decodeRecoveryMeasurements, decodeStateReco
const MAX_GOALS = 128;
const MAX_SQLITE_FILES = 128;
-const MAX_COMMITS = 20_000;
const hash = (value: string) => createHash("sha256").update(value).digest("hex");
const digest = isRecoveryDigest;
class RecoveryInputError extends Error {
@@ -72,11 +71,6 @@ function authorityCaptures(value: unknown, roots: Map | n
const goalId = String(row.goal_id);
check(!rows.has(goalId) && roots !== null && row.runtime_archive_path === (roots.get(goalId) ?? null),
"authority_capture_dependency_mismatch");
- if (row.state === "readable") {
- const archive = requireJsonObject(row.archive, "authority capture summary");
- check(typeof archive.commits === "string" && /^[1-9]\d*$/.test(archive.commits) &&
- BigInt(archive.commits) <= BigInt(MAX_COMMITS), "authority_history_capacity");
- }
rows.set(goalId, row);
}
return rows;
@@ -203,40 +197,48 @@ async function inspectGoal(root: string | null, goalId: string, standaloneSqlite
const handle = await openLocalAuthorityStoreHandle(root, goalId, {}, {existingOnly: true});
const loaded = await handle.store.loadAuthority();
check(loaded.status === "loaded", "authority_head_unavailable");
- check(parseAuthorityCursor(loaded.cursor) <= BigInt(MAX_COMMITS), "authority_history_capacity");
+ const frontier = parseAuthorityCursor(loaded.cursor);
+ check(frontier > 0n && frontier <= BigInt(Number.MAX_SAFE_INTEGER), "authority_history_count_unrepresentable");
+ const identity = await handle.store.storeIdentity();
+ check(identity.status === "available", "authority_identity_unavailable");
// Byte-valid storage alone is not a readable Goal work model. Reuse the
// same full-record, lease and acceptance decoder as canonical Todo reads.
const collection = canonicalTodoCollection(loaded.head, goalId, true);
let cursor: string | null = null;
- let count = 0;
+ let count = 0n;
let final: JsonObject | null = null;
let finalRevision: string | null = null;
- while (true) {
- const page = await handle.store.scanCommitted(cursor, 200);
+ while (count < frontier) {
+ const limit = Number(frontier - count > 200n ? 200n : frontier - count);
+ const page = await handle.store.scanCommitted(cursor, limit);
check(page.status === "page", "authority_history_unavailable");
- count += page.transactions.length;
- check(count <= MAX_COMMITS, "authority_history_capacity");
- for (const row of page.transactions) { final = row.projection; finalRevision = row.provider_revision; }
- if (!page.has_more) break;
- check(page.next_cursor !== null && page.next_cursor !== cursor && page.transactions.length > 0,
+ check(page.transactions.length === limit, "authority_history_cursor_invalid");
+ for (const row of page.transactions) {
+ count += 1n;
+ check(parseAuthorityCursor(row.cursor) === count, "authority_history_cursor_invalid");
+ final = row.projection;
+ finalRevision = row.provider_revision;
+ }
+ check(page.next_cursor === count.toString() && page.has_more === (count < frontier),
"authority_history_cursor_invalid");
cursor = page.next_cursor;
}
- check(String(count) === loaded.cursor && final !== null && finalRevision === loaded.provider_revision
- && canonicalAuthoritySha256(final) === canonicalAuthoritySha256(loaded.head), "authority_history_head_mismatch");
+ const headDigest = canonicalAuthoritySha256(loaded.head);
+ check(final !== null && finalRevision === loaded.provider_revision &&
+ canonicalAuthoritySha256(final) === headDigest, "authority_history_head_mismatch");
const readback = await handle.store.loadAuthority();
- check(readback.status === "loaded" && readback.provider_revision === loaded.provider_revision,
+ const readbackIdentity = await handle.store.storeIdentity();
+ check(readback.status === "loaded" && readback.cursor === loaded.cursor &&
+ readback.provider_revision === loaded.provider_revision && canonicalAuthoritySha256(readback.head) === headDigest &&
+ readbackIdentity.status === "available" && readbackIdentity.store_identity === identity.store_identity,
"authority_changed_during_audit");
return component(id, "coordination_authority", "readable", "historical_head_and_history_readable",
"review_goal_identity_before_adoption", {...goalFacts, provider: handle.provider,
- head_digest: canonicalAuthoritySha256(loaded.head), history_commits: count,
+ head_digest: headDigest, history_commits: Number(count),
provider_revision: loaded.provider_revision, cursor: loaded.cursor,
todo_count: collection.projection.todo_ids.length,
historical_lease_count: collection.leaseIndex?.lease_todo_ids.length ?? 0}, ["configuration"]);
} catch (error) {
- // Inspection profile overflow aborts publication; it is not evidence that
- // this owner's captured state is merely unknown or unreadable.
- if (error instanceof RecoveryInputError && error.code === "authority_history_capacity") throw error;
return component(id, "coordination_authority", "unknown",
error instanceof RecoveryInputError ? error.code : "authority_source_unavailable",
"inspect_captured_authority_with_owner", goalFacts, ["configuration"]);
@@ -327,11 +329,9 @@ async function auditOwners(value: unknown): Promise {
check(capture?.state === "readable", "authority_capture_unavailable");
check(capture.configuration_sha256 === configuration.sha256, "authority_capture_dependency_mismatch");
const facts = await inspectAuthorityBackup(dataRoot, capture);
- check(Number(facts.history_commits) <= MAX_COMMITS, "authority_history_capacity");
components.push(component(id, "coordination_authority", "readable", "historical_head_and_history_readable",
"review_goal_identity_before_adoption", {...goalFacts, ...facts}, ["configuration"]));
} catch (error) {
- if (error instanceof RecoveryInputError && error.code === "authority_history_capacity") throw error;
components.push(component(id, "coordination_authority", "unknown", "authority_capture_unavailable",
"inspect_captured_authority_with_owner", goalFacts, ["configuration"]));
}
diff --git a/loopx/control_plane/turn_driver/driver.py b/loopx/control_plane/turn_driver/driver.py
index 375a73ccff..5594207935 100644
--- a/loopx/control_plane/turn_driver/driver.py
+++ b/loopx/control_plane/turn_driver/driver.py
@@ -418,6 +418,21 @@ def _bind_host_child_operations(
return bound
+def _complete_plan_source_fields(
+ work_contract: object, projection: Mapping[str, Any], original_envelope: Mapping[str, Any] | None,
+) -> dict[str, Any]:
+ fields = {"work_contract": work_contract, "source_decision_hash": projection["source_decision_hash"]}
+ for key in ("inspection_paths", "selected_work_basis"):
+ if key in projection:
+ fields[f"_{key}"] = projection[key]
+ if original_envelope is not None:
+ # Carry the reader of the original capture from a transitional pair.
+ header = captured_turn_host_input({"turn_envelope": original_envelope})
+ if "detail_ref" in header:
+ fields["detail_ref"] = header["detail_ref"]
+ return fields
+
+
def build_loopx_turn_plan(
turn_envelope: Mapping[str, Any],
*,
@@ -431,6 +446,7 @@ def build_loopx_turn_plan(
goal_ref: Mapping[str, object] | None = None,
work_contract: object = _ABSENT_WORK_CONTRACT,
inspect_plan: bool = False,
+ include_selected_work_basis: bool = False,
) -> dict[str, Any]:
"""Plan from one complete source; retain the recorded v0 input codec.
@@ -483,6 +499,7 @@ def build_loopx_turn_plan(
projection = _turn_envelope_result("planning_facts", payload=work_contract,
turn_instance_id=turn_instance_id,
**({"inspect_plan": True} if inspect_plan else {}),
+ **({"include_selected_work_basis": True} if include_selected_work_basis else {}),
**({} if source_only_input else {"envelope": supplied}))
envelope = _mapping(projection["work"])
else:
@@ -609,16 +626,9 @@ def build_loopx_turn_plan(
),
}
if current:
- payload["work_contract"] = work_contract
- payload["source_decision_hash"] = projection["source_decision_hash"]
- if "inspection_paths" in projection:
- payload["_inspection_paths"] = projection["inspection_paths"]
- if not source_only_input:
- # Older callers may already carry an original quota capture. Keep
- # its exact header without retaining that caller's second source.
- header = captured_turn_host_input({"turn_envelope": supplied})
- if "detail_ref" in header:
- payload["detail_ref"] = header["detail_ref"]
+ payload.update(_complete_plan_source_fields(
+ work_contract, projection, None if source_only_input else supplied,
+ ))
else:
payload["turn_envelope"] = envelope
if child_operations:
diff --git a/loopx/control_plane/turn_driver/executor.py b/loopx/control_plane/turn_driver/executor.py
index a5e68b24ff..73675259e1 100644
--- a/loopx/control_plane/turn_driver/executor.py
+++ b/loopx/control_plane/turn_driver/executor.py
@@ -38,7 +38,11 @@
normalize_reward_memory_reflection_validation,
reward_memory_reflection_digest,
)
-from ..quota.turn_envelope import turn_plan_host_source
+from ..quota.turn_envelope import (
+ COMPLETE_TURN_HOST_REQUEST_SCHEMA,
+ captured_turn_host_input,
+ turn_plan_host_source,
+)
from .driver import (
COMPLETE_TURN_PLAN_SCHEMA_VERSION,
selected_turn_todo, turn_host_lineage,
@@ -156,6 +160,10 @@
def build_loopx_turn_host_request(plan: Mapping[str, Any]) -> dict[str, Any]:
"""Deliver the plan's original source; decode saved v0 plans as recorded."""
+ return _complete_loopx_turn_host_request(_assemble_loopx_turn_host_request(plan))
+
+
+def _assemble_loopx_turn_host_request(plan: Mapping[str, Any]) -> dict[str, Any]:
boundary = plan.get("boundary")
if isinstance(boundary, Mapping) and boundary.get("inspection_only") is True:
raise ValueError("LoopX Turn CLI inspection requires the explicit run-once execution surface")
@@ -197,6 +205,10 @@ def build_loopx_turn_host_request(plan: Mapping[str, Any]) -> dict[str, Any]:
if configured_native_child_limit(request) is not None:
request["turn_instance_id"] = transaction.get("turn_instance_id") or turn_key
+ return request
+
+
+def _complete_loopx_turn_host_request(request: dict[str, Any]) -> dict[str, Any]:
if "work_contract" in request:
from .host_candidate import complete_host_input
@@ -204,6 +216,20 @@ def build_loopx_turn_host_request(plan: Mapping[str, Any]) -> dict[str, Any]:
return request
+def _build_staged_dispatch_request(plan: Mapping[str, Any]) -> dict[str, Any]:
+ """Assemble staged input for the immediate mandatory dispatch check."""
+ request = _assemble_loopx_turn_host_request(plan)
+ if (plan.get("schema_version") == COMPLETE_TURN_PLAN_SCHEMA_VERSION
+ and request.get("schema_version") == COMPLETE_TURN_HOST_REQUEST_SCHEMA
+ and "work_contract" in request and "turn_envelope" not in request):
+ # All assembly guards and projections still run on the staged aliases.
+ # This carrier's existing conversion only copies the top-level mapping;
+ # host_context returns no facts used by it. The caller checks this exact
+ # source with host_dispatch_context before admission or publication.
+ return captured_turn_host_input(request)
+ return _complete_loopx_turn_host_request(request)
+
+
def _bounded_public_text(
result: Mapping[str, Any],
field: str,
@@ -851,6 +877,7 @@ def _run_host(
argv: Sequence[str],
project: Path,
timeout_seconds: float | None,
+ launch_guard: Callable[[], AbstractContextManager[None]] | None = None,
) -> dict[str, Any]:
# Dispatch has checked the source and its internal effect adapter before
# journal/cadence admission. External stdin gets the same single complete
@@ -874,7 +901,10 @@ def count_stderr(text: str) -> None:
stdout_limit_bytes=HOST_RESULT_MAX_BYTES,
on_stdout=stdout.append,
on_stderr=count_stderr,
+ launch_guard=launch_guard,
)
+ except FirstPartyHostRuntimeRejected:
+ raise
except (OSError, RuntimeError, ValueError) as exc:
return {"ok": False, "reason": type(exc).__name__, "returncode": None}
if observed["outcome"] == "output_limit":
@@ -985,6 +1015,7 @@ def _host_result_stage(
confirm_start: Callable[[], None] | None = None,
usage_runtime_root: Path | None = None,
usage_goal_id: str = "",
+ host_launch_guard: Callable[[], AbstractContextManager[None]] | None = None,
) -> tuple[dict[str, Any] | None, list[str], dict[str, Any] | None]:
completed_phases = list(journal.get("completed_phases") or [])
from_journal = "typed_result" in completed_phases
@@ -1007,22 +1038,32 @@ def _host_result_stage(
confirm_start()
from ...usage_goal import observe_goal_execution
- with observe_goal_execution(
- usage_runtime_root or project,
- usage_goal_id,
- host=str((plan.get("host") or {}).get("kind") or "unknown"),
- ):
- host_observation = (
- _run_host_runner(request, runner=host_runner)
- if host_runner is not None
- else _run_host(
- request,
- argv=argv or [],
- project=project,
- timeout_seconds=timeout_seconds,
+ rejected_launch = None
+ try:
+ with observe_goal_execution(
+ usage_runtime_root or project,
+ usage_goal_id,
+ host=str((plan.get("host") or {}).get("kind") or "unknown"),
+ ):
+ host_observation = (
+ _run_host_runner(request, runner=host_runner)
+ if host_runner is not None
+ else _run_host(
+ request,
+ argv=argv or [],
+ project=project,
+ timeout_seconds=timeout_seconds,
+ launch_guard=host_launch_guard,
+ )
)
- )
- effects["host_invoked"] = True
+ except FirstPartyHostRuntimeRejected as exc:
+ # Stdio checks the Goal only before spawn. A native Host can reject
+ # after dispatch too, so retain reconciliation for other failures.
+ if host_runner is not None and exc.code != "current_turn_work_changed":
+ raise
+ rejected_launch = exc.code
+ host_observation = {"ok": False, "reason": str(exc), "failure_kind": "contract_rejected"}
+ effects["host_invoked"] = rejected_launch is None
if not host_observation.get("ok"):
failure = _host_failure(
plan,
@@ -1048,17 +1089,10 @@ def _host_result_stage(
else:
journal.pop("host_recovery", None)
persist_journal(journal)
- return (
- None,
- [],
- execution_payload(
- plan,
- journal,
- execute=True,
- replayed=False,
- effects=effects,
- ),
- )
+ terminal = execution_payload(plan, journal, execute=True, replayed=False, effects=effects)
+ if rejected_launch is not None:
+ terminal["error_code"] = rejected_launch
+ return None, [], terminal
result = dict(host_observation["value"])
completed_phases = list(TRANSACTION_PHASES[:2])
@@ -1549,6 +1583,7 @@ def run_loopx_turn_once(
admit_start: Callable[[Mapping[str, Any]], dict[str, Any]] | None = None,
confirm_start: Callable[[], None] | None = None,
goal_admission: FirstPartyHostGoalAdmission | None = None,
+ host_launch_guard: Callable[[], AbstractContextManager[None]] | None = None,
) -> dict[str, Any]:
argv, host_projection = _prepare_host_invocation(plan, host_argv=host_argv, host_runner=host_runner)
transaction_plan = (
@@ -1706,7 +1741,7 @@ def persist_source_journal(source_admission: Mapping[str, Any]) -> None:
# must not put otherwise eligible work into an interval wait.
# Saved typed results retain their original finalization path.
complete_turn_host_source(
- build_loopx_turn_host_request(capture.plan) if capture is not None else request,
+ _build_staged_dispatch_request(capture.plan) if capture is not None else request,
operation="host_dispatch_context",
)
admission = None
@@ -1820,6 +1855,7 @@ def finish_recovery(payload: dict[str, Any]) -> dict[str, Any]:
persist_journal=persist_journal,
effects=effects,
confirm_start=confirm_start if reserved_start else None,
+ host_launch_guard=host_launch_guard,
)
if terminal is not None:
return finish_recovery(terminal)
diff --git a/loopx/control_plane/work_items/interaction_contract.ts b/loopx/control_plane/work_items/interaction_contract.ts
index 84b4e4bb96..ef03a88d9b 100644
--- a/loopx/control_plane/work_items/interaction_contract.ts
+++ b/loopx/control_plane/work_items/interaction_contract.ts
@@ -59,6 +59,11 @@ function pendingAutomaticSuccessor(request: JsonObject, wait: JsonObject | null)
* This read model never relaxes an existing Turn binding or settlement fence.
* Unknown dependencies retain their bounded observation/replan policy. */
export function projectBlockedSuccessorWaitObservation(request: JsonObject): JsonObject {
+ if (request.effective_action === EffectiveAction.MONITOR_QUIET_SKIP) {
+ // The automatic heartbeat must distinguish a proven future clock from a
+ // quiet Monitor result that still counts towards the blocked-wait limit.
+ return {required: projectSchedulerArbitration(request).scheduled_recheck_after_seconds === undefined};
+ }
const wait = exactBlockedSuccessorWait(request);
return {required: !!wait && pendingAutomaticSuccessor(request, wait) === null};
}
diff --git a/loopx/control_plane/work_items/replan_semantics.ts b/loopx/control_plane/work_items/replan_semantics.ts
index 333467ebfe..894d37ba9d 100644
--- a/loopx/control_plane/work_items/replan_semantics.ts
+++ b/loopx/control_plane/work_items/replan_semantics.ts
@@ -276,12 +276,14 @@ function preserveReceiptBoundObligation(request: JsonObject): JsonObject {
![null, "open", "settlement_pending"].includes(request.replay_phase as string | null)) {
throw new EffectRuntimeRequestError("receipt-bound replan facts are malformed");
}
+ const writebackCommitted = request.replay_phase === "settlement_pending";
let original: JsonObject | null = null;
let openVisionReview: JsonObject | null = null;
let recoveredLifecycle: JsonObject | null = null;
let delta: JsonObject | null = null;
let historicalAckRecorded = false;
- for (const value of request.transition_candidates) {
+ const candidates = writebackCommitted ? [] : request.transition_candidates;
+ for (const value of candidates) {
const candidate = object(value);
const obligation = object(candidate.obligation);
if (request.guard_scoped && object(candidate.source_obligation).resolution_mode === "todo_lifecycle_settlement") {
@@ -319,7 +321,6 @@ function preserveReceiptBoundObligation(request: JsonObject): JsonObject {
break;
}
}
- const writebackCommitted = request.replay_phase === "settlement_pending";
if (delta !== null || writebackCommitted) {
const reason = writebackCommitted
? "The original Turn writeback is verified. Settle its existing quota receipt once; do not repeat work or select its successor."
diff --git a/loopx/extensions/lark/cli_resolution.py b/loopx/extensions/lark/cli_resolution.py
index 01bb1fa8c0..acac8668a6 100644
--- a/loopx/extensions/lark/cli_resolution.py
+++ b/loopx/extensions/lark/cli_resolution.py
@@ -12,6 +12,7 @@
import shutil
import subprocess
from collections.abc import Callable, Mapping, Sequence
+from contextlib import nullcontext
from dataclasses import dataclass
from pathlib import Path
from typing import Any
@@ -261,6 +262,8 @@ def run(
args: list[str],
cwd: Path | None = None,
timeout: float | None = None,
+ *,
+ dispatch_guard=None,
) -> dict[str, Any]:
if not resolution.available:
raise LarkCliUnavailableError(resolution.error_code or "lark_cli_not_installed")
@@ -272,14 +275,25 @@ def run(
env["PATH"] = os.pathsep.join(
[parent, *(part for part in current if part != parent)]
)
- completed = subprocess_run(
- args,
- cwd=str(cwd) if cwd else None,
- timeout=timeout,
- capture_output=True,
- env=env,
- text=True,
- )
+ if dispatch_guard is not None:
+ with dispatch_guard():
+ process = subprocess.Popen(
+ args, cwd=str(cwd) if cwd else None, env=env, text=True,
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+ )
+ with process:
+ try:
+ stdout, stderr = process.communicate(timeout=timeout)
+ except BaseException:
+ process.kill()
+ process.communicate()
+ raise
+ completed = subprocess.CompletedProcess(args, process.returncode, stdout, stderr)
+ else:
+ completed = subprocess_run(
+ args, cwd=str(cwd) if cwd else None, timeout=timeout,
+ capture_output=True, env=env, text=True,
+ )
return {
"returncode": completed.returncode,
"stdout": completed.stdout,
@@ -287,4 +301,14 @@ def run(
"timed_out": False,
}
+ run.dispatch_guard = True
return run
+
+
+def run_lark_command_with_dispatch_guard(runner, args, cwd, timeout, guard):
+ """Keep the native authority fence through CLI spawn, never response IO."""
+ if getattr(runner, "dispatch_guard", False) is True:
+ return runner(args, cwd, timeout, dispatch_guard=guard)
+ # Injected in-process runners execute synchronously and own no subprocess.
+ with (guard() if guard is not None else nullcontext()):
+ return runner(args, cwd, timeout)
diff --git a/loopx/extensions/lark/private_conversations.py b/loopx/extensions/lark/private_conversations.py
index 50a2de7eeb..33d14cb766 100644
--- a/loopx/extensions/lark/private_conversations.py
+++ b/loopx/extensions/lark/private_conversations.py
@@ -5,7 +5,9 @@
"""
from __future__ import annotations
+import base64
import hashlib
+import http.client
import json
import re
import subprocess
@@ -13,6 +15,7 @@
from collections.abc import Callable, Iterator, Mapping, Sequence
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime, timezone
+from functools import cached_property, partial
from itertools import tee
from pathlib import Path
from threading import Lock
@@ -25,6 +28,7 @@
)
from ...capabilities.native_chat.conversation_bindings import ChatConversationBindings
from ...capabilities.native_chat.file_discovery import FileChatDiscovery, file_change_fingerprint
+from ...chat_attachments import CHAT_IMAGE_MAX_COUNT, normalize_chat_image_attachments
from ...chat_store import CHAT_MESSAGE_SCHEMA_VERSION, ChatSessionStore, TERMINAL_TURN_STATES, _atomic_write_json, _read_json
from ...control_plane.collaboration import conversation_reply_context
from ...control_plane.effect_runtime import effect_runtime_request_scope
@@ -35,14 +39,15 @@
from .event_inbox import acknowledge_lark_event_inbox, ingest_lark_event_inbox
from .event_collector_runtime import enrich_lark_event_reply_context
from .goal_channel_transport import call, json_payload, lark_args, lark_provider_mention_identities, record_event_timing
-from .identity_shapes import LARK_APP_ID_PATTERN as APP_ID_PATTERN
+from .identity_shapes import LARK_APP_ID_PATTERN as APP_ID_PATTERN, LARK_MESSAGE_ID_PATTERN
from .inbox_reply import _message, reply_lark_event_inbox, update_lark_inbox_reply, verify_lark_inbox_reply
from .manager_context import manager_failure_reply
from .inbox_reactions import (_reconcile_outbound_input_reactions,
mark_lark_event_inbox_processing, mark_lark_event_inbox_received)
-from .outbound import present_lark_conversation_text as _presentation_text
+from .outbound import normalize_lark_outbound_text, present_lark_conversation_text as _presentation_text
from .private_images import private_message_caption, private_message_images
-from .private_delivery_discovery import PrivateDeliveryDiscovery, prepared_steward_binding_waits
+from .private_delivery_discovery import (PrivateDeliveryDiscovery, allowed_answer_image_urls, answer_image_source_matches,
+ answer_media_dispatch, historical_image_reply_matches, prepared_steward_binding_waits, resumable_answer_image_session)
from .private_ingress import PrivateSourceUnavailable, private_source_text, steward_source_preflight
from .private_ingress import project_private_reply_history as project_private_reply_history
from . import private_steward_triage as triage
@@ -54,7 +59,90 @@
# Reserve edits for a final/closing notice; continue long Turns on a new draft.
PROGRESS_EDIT_BUDGET = 12
CHAT_CONTROL_COMMANDS = {"/status": "status", "/help": "help", "/new": "new", "/stop": "stop"}
+ANSWER_IMAGE_RETRY_SECONDS = 300
+ANSWER_IMAGE_SOURCE_TIMEOUT = 5
_UNSUPPORTED_MEDIA_LABELS = {"audio": "音频", "file": "文件", "media": "视频"}
+_MARKDOWN_CODE_SPAN = re.compile(r"((?\n]+)>|([^\s<>()]+))(?:[ \t]+(?:"[^"\n]*"|'[^'\n]*'))?\s*\)""")
+
+
+def _private_answer_images(text: str) -> tuple[str, list[str]]:
+ """Remove image previews from provider prose, leaving the canonical answer alone."""
+ lines, targets = [], []
+ fence = None
+ for line in text.splitlines(keepends=True):
+ marker = re.match(r"^ {0,3}(\x60{3,}|~{3,})(.*)$", line)
+ if marker and fence is None:
+ fence = marker[1]
+ elif marker and fence and marker[1][0] == fence[0] and len(marker[1]) >= len(fence) and not marker[2].strip():
+ fence = None
+ elif not fence and not line.startswith((" ", "\t")):
+ def link(match: re.Match[str]) -> str:
+ url = match[2] or match[3]
+ if url not in targets:
+ targets.append(url)
+ caption = match[1] or f"图片 {targets.index(url) + 1}"
+ return f"[{caption}](<{url}>)" if url.startswith("https://") and " " in url else (
+ f"[{caption}]({url})" if url.startswith("https://") else caption)
+
+ start, chunks = 0, []
+ for span in _MARKDOWN_CODE_SPAN.finditer(line):
+ chunks.extend((_ANSWER_IMAGE.sub(link, line[start:span.start()]), span[0]))
+ start = span.end()
+ line = "".join((*chunks, _ANSWER_IMAGE.sub(link, line[start:])))
+ lines.append(line)
+ return "".join(lines), targets
+
+
+def _private_answer_image(url: str, *, allowed_urls: set[str], deadline: float, index: int) -> dict[str, Any]:
+ # Reuse anonymous HTTPS address pinning, redirect checks and byte limits.
+ # No local file, signed-in browser or model tool authority is involved.
+ from urllib.parse import urlsplit
+
+ from ..public_source_reader import fetch
+
+ host = urlsplit(url).hostname
+ same_host_urls = {candidate for candidate in allowed_urls if urlsplit(candidate).hostname == host}
+ _, mime, data = fetch(url, deadline=deadline, allowed_urls=same_host_urls)
+ signatures = {"image/png": b"\x89PNG\r\n\x1a\n", "image/jpeg": b"\xff\xd8\xff", "image/gif": b"GIF8"}
+ if not ((mime in signatures and data.startswith(signatures[mime]))
+ or (mime == "image/webp" and data[:4] == b"RIFF" and data[8:12] == b"WEBP")):
+ raise ValueError("unsupported original image")
+ return {"id": f"reply-image-{index}", "name": f"original-{index}.{mime.split('/')[1]}",
+ "mime_type": mime, "size": len(data), "data_url": f"data:{mime};base64," + base64.b64encode(data).decode()}
+
+
+def _read_answer_images(source: str, targets: list[str], cached: list | None,
+ delivery: Mapping[str, Any]) -> list[dict[str, Any]]:
+ if (delivery.get("started") and not delivery.get("attempt")) or (delivery.get("attempt") and cached is None):
+ raise ValueError("image delivery acknowledgement or frozen bytes are unavailable")
+ deadline = time.monotonic() + 15
+ allowed_urls = allowed_answer_image_urls(source, targets, deadline=deadline)
+ if not allowed_urls:
+ raise ValueError("image was not referenced by the original source")
+ images = normalize_chat_image_attachments([
+ _private_answer_image(url, allowed_urls=allowed_urls, deadline=deadline, index=index)
+ for index, url in enumerate(targets, 1)])
+ # Older queued bytes were fetched before redirect authorization. Keep
+ # any recorded provider intent tied to the same bytes.
+ if cached is not None and cached != images:
+ raise ValueError("unverified cached image bytes changed")
+ return images
+
+
+def _is_answer_image_followup(record: Mapping[str, Any]) -> bool:
+ if record.get("status") != "accepted" or not isinstance(record.get("answer_images"), dict):
+ return False
+ terminal = record.get("deliveries", {}).get(presentation_phase(record, "terminal"), {})
+ return terminal.get("verified") is True
+
+
+def _answer_image_retry_waits(record: Mapping[str, Any], now: float) -> bool:
+ if not _is_answer_image_followup(record):
+ return False
+ state = record["answer_images"]
+ retry_at = state.get("next_check_at")
+ return state.get("status") != "delivered" and type(retry_at) in (int, float) and retry_at > now
def _unsupported_media_label(row: Mapping[str, Any]) -> str | None:
@@ -233,6 +321,7 @@ def __init__(self, *, controller: Any, runtime_root: Path, runner: Any, cli_bin:
self.steward_callback_readiness: dict[tuple[str, str], Callable[[], bool]] = {}
self.root = controller.store.root / "lark-private-deliveries"
self._context_recovery_probe_at: dict[Path, float] = {}
+ self._historical_image_probe_at: dict[Path, float] = {}
self._delivery_discovery = PrivateDeliveryDiscovery(self.root)
self._body_discovery = PrivateDeliveryDiscovery(self.root)
self._web_projection_scan_at = 0.0
@@ -289,6 +378,10 @@ def configured_profile_apps(self, *, cli_bin: str | None = None) -> dict[str, st
apps.setdefault(app_id, app_id)
return {profile: app_id for profile, app_id in apps.items() if APP_ID_PATTERN.fullmatch(app_id)}
+ @cached_property
+ def _health_delivery_discovery(self) -> FileChatDiscovery:
+ return FileChatDiscovery()
+
@effect_runtime_request_scope()
def health(self) -> dict[str, dict[str, int]]:
health: dict[str, dict[str, int]] = {}
@@ -308,13 +401,36 @@ def observe_completed() -> None:
count(row)
completed.clear()
+ def health_fields(_path: Path, row: dict[str, Any]) -> dict[str, Any] | None:
+ # A delivered body may still be waiting for original image recovery.
+ if (row["status"] != "delivered"
+ or (row.get("historical_answer_images") or {}).get("status") == "pending"):
+ return None
+ deliveries = row.get("deliveries")
+ if not isinstance(deliveries, dict) or not all(
+ isinstance(phase, dict) for phase in deliveries.values()
+ ):
+ return None
+ # Settings uses only correlation and retry metadata. Never retain
+ # delivered bodies or use these fields for admission or sending.
+ fields = ("binding_id", "status", "request_ref", "source", "session_id", "turn_id")
+ return {key: row[key] for key in fields if key in row} | {
+ "deliveries": {key: {"started": bool(phase.get("started")), "verified": bool(phase.get("verified"))}
+ for key, phase in deliveries.items()},
+ }
+
def delivered_requests() -> Iterator[dict[str, Any]]:
- for path in [*self.root.glob("*.json"), *self.root.glob("web-projections/*.json")]:
- row = _read_json(path)
+ paths = [*self.root.glob("*.json"), *self.root.glob("web-projections/*.json")]
+ for path, row in self._health_delivery_discovery.rows(
+ self.root, paths, read=_read_json, project=health_fields,
+ ):
health.setdefault(row["binding_id"], {"pending_count": 0, "recovery_count": 0})
- if row["status"] != "delivered" and triage.pending(row):
+ if row["status"] != "delivered":
+ if triage.pending(row):
+ count(row)
+ elif (row.get("historical_answer_images") or {}).get("status") == "pending":
count(row)
- elif row["status"] == "delivered" and path.parent == self.root and row.get("session_id") and row.get("turn_id"):
+ elif path.parent == self.root and row.get("session_id") and row.get("turn_id"):
yield row
# Pair the original receipts with the current Core correlation and Turn.
@@ -343,14 +459,13 @@ def _group_source_message(self, profile: str, message_id: str) -> Mapping[str, A
return None
return _message(payload, message_id)
- def _group_source_pair(self, profile: str, message_id: str,
- root_id: str) -> tuple[Mapping[str, Any] | None, Mapping[str, Any] | None]:
- # Callers bound these IDs to an admitted topic or a frozen Core stop.
- # This is readonly scheduling, not authority; raw mget retains
- # body/ancestry/mentions and the current Core selection validates them.
- result = call(self.runner, lark_args(cli_bin=self.cli_bin, profile=profile,
+ def _original_message_pair(self, profile: str, message_id: str, other_id: str, *,
+ runner=None) -> tuple[Mapping[str, Any] | None, Mapping[str, Any] | None]:
+ # Read both admitted IDs at once. Callers verify the returned envelopes
+ # and the current Core selection before using either as authority.
+ result = call(runner or self.runner, lark_args(cli_bin=self.cli_bin, profile=profile,
tail=["api", "GET", "/open-apis/im/v1/messages/mget", "--params",
- json.dumps({"message_ids": [message_id, root_id]}), "--as", "bot"]))
+ json.dumps({"message_ids": [message_id, other_id]}), "--as", "bot"]))
payload = json_payload(result)
if result.get("returncode") != 0 or payload.get("code", 0) != 0 or payload.get("ok") is False:
return None, None
@@ -358,10 +473,17 @@ def _group_source_pair(self, profile: str, message_id: str,
items = data.get("items") if isinstance(data, Mapping) else None
if (not isinstance(items, list) or len(items) != 2
or any(not isinstance(item, Mapping) or not isinstance(item.get("message_id"), str) for item in items)
- or {item.get("message_id") for item in items} != {message_id, root_id}):
+ or {item.get("message_id") for item in items} != {message_id, other_id}):
return None, None
messages = {item["message_id"]: item for item in items}
- return messages[message_id], messages[root_id]
+ return messages[message_id], messages[other_id]
+
+ def _read_answer_image_source(self, args: list[str], cwd: Path | None, _timeout: float):
+ return self.runner(args, cwd, ANSWER_IMAGE_SOURCE_TIMEOUT)
+
+ def _group_source_pair(self, profile: str, message_id: str,
+ root_id: str) -> tuple[Mapping[str, Any] | None, Mapping[str, Any] | None]:
+ return self._original_message_pair(profile, message_id, root_id)
def _completed_stop_root(self, record: Mapping[str, Any], binding: Mapping[str, Any]) -> str | None:
"""Read scheduling only; fresh Core resolution still owns authorization."""
@@ -568,18 +690,23 @@ def sources() -> tuple[Mapping[str, Any] | None, Mapping[str, Any] | None]:
pass
return None
- def _source_verified(self, record: dict[str, Any], *, sender_identity: dict[str, str] | None = None) -> bool:
- return self._source_message(record, render_content=False, sender_identity=sender_identity) is not None
+ def _source_verified(self, record: dict[str, Any], *, sender_identity: dict[str, str] | None = None,
+ expected_text: str | None = None) -> bool:
+ message = self._source_message(record, render_content=False, sender_identity=sender_identity)
+ return message is not None and (expected_text is None or (
+ message.get("deleted") is not True and message.get("msg_type", message.get("message_type")) == "text"
+ and private_source_text(message) == expected_text))
def _source_authority(self, record: dict[str, Any], profile: str, chat_id: str,
- message_id: str) -> tuple[str | None, bool]:
+ message_id: str, *, expected_text: str | None = None) -> tuple[str | None, bool]:
"""Verify one exact outbound operation; retain no identity or grant."""
if (profile != record["profile"] or chat_id != record["event"]["chat_id"]
or message_id != record["event"]["message_id"]):
return None, False
identity: dict[str, str] = {}
record_event_timing(profile, message_id, "outbound_source_check_started")
- verified = self._source_verified(record, sender_identity=identity)
+ verified = self._source_verified(record, sender_identity=identity,
+ **({"expected_text": expected_text} if expected_text is not None else {}))
record_event_timing(profile, message_id, "outbound_source_check_returned")
return identity.get("bot_display_name"), verified
@@ -1124,7 +1251,8 @@ def _feedback(self, path: Path, record: dict[str, Any], *, inbox: Callable[[], P
def _deliver(self, path: Path, record: dict[str, Any], phase: str, text: str, *, inbox: Callable[[], Path],
edit_only: bool = False, authorization_check: Callable[[], bool] | None = None,
reply_placement: str | None = None, delivery_id: str | None = None,
- finalize_source: bool = True, image_attachments: list[dict[str, Any]] | None = None) -> bool:
+ finalize_source: bool = True, image_attachments: list[dict[str, Any]] | None = None,
+ historical_source_check: Callable[[], bool] | None = None) -> bool:
if not text:
return False
if reply_placement is not None and (record["event"].get("chat_type") != "p2p"
@@ -1133,7 +1261,10 @@ def _deliver(self, path: Path, record: dict[str, Any], phase: str, text: str, *,
if authorization_check is not None and not authorization_check():
return False
kind = phase.split(".", 1)[0]
+ if historical_source_check is not None and phase != "answer_images.historical":
+ raise ValueError("historical source verification requires a media repair")
phase = presentation_phase(record, phase)
+ expected_text = record["event"]["content"] if kind == "answer_images" else None
phase_state = record["deliveries"].get(phase, {})
if phase_state.get("verified"):
return True
@@ -1144,26 +1275,52 @@ def _deliver(self, path: Path, record: dict[str, Any], phase: str, text: str, *,
text = _presentation_text(text)
if phase_state.get("text") not in (None, text):
raise ValueError("delivery content changed after an attempt")
- if image_attachments and (kind != "web" or reply_placement != "chat_root" or edit_only):
- raise ValueError("App image projection requires its original private outbox")
+ if image_attachments and (authorization_check is None or edit_only
+ or not ((kind == "web" and reply_placement == "chat_root")
+ or (kind == "answer_images" and record["event"].get("chat_type") == "p2p"
+ and record["source"].get("private_human_message") is True))):
+ raise ValueError("Image projection requires its original private outbox")
record_event_timing(record["profile"], record["event"]["message_id"], "delivery_started")
config = inbox()
record_event_timing(record["profile"], record["event"]["message_id"], "delivery_inbox_ready")
- def image_authorized() -> bool:
- if authorization_check is None or not authorization_check():
- return False
+ def provider_images_current() -> bool:
+ if historical_source_check is not None:
+ return historical_source_check()
+ source = record["event"]
+ ref = source["message_id"]
+ args = lark_args(cli_bin=self.cli_bin, profile=record["profile"],
+ tail=["api", "GET", f"/open-apis/im/v1/messages/{ref}", "--as", "bot"])
+ reply = call(self._read_answer_image_source, args)
+ payload = json_payload(reply)
+ return (reply.get("returncode") == 0 and payload.get("code", 0) == 0 and payload.get("ok") is not False
+ and answer_image_source_matches(_message(payload, ref), source))
+
+ def verified_source() -> bool:
+ if historical_source_check is not None:
+ return historical_source_check()
name, verified = self._source_authority(record, record["profile"],
- record["event"]["chat_id"], record["event"]["message_id"])
+ record["event"]["chat_id"], record["event"]["message_id"], expected_text=expected_text)
return bool(name) and verified
+ def image_authorized() -> bool:
+ # A provider read can overlap Stop, Close, or an edit to the old reply.
+ return authorization_check is not None and (provider_images_current() and authorization_check()
+ if kind == "answer_images" else authorization_check() and verified_source())
+
image_keys = ()
if image_attachments:
+ resource_scope = (f"{record['binding_id']}:{record['request_ref']}:{phase}"
+ if kind == "answer_images" else record["binding_id"])
image_keys = uploaded_chat_images(root=self.runtime_root, profile=record["profile"],
- provider_ref=record["binding_id"], attachments=image_attachments, private_transport=self,
- before_upload=image_authorized, verify_only=bool(phase_state.get("started") or phase_state.get("attempt")))
+ provider_ref=resource_scope, attachments=image_attachments, private_transport=self,
+ before_upload=image_authorized, dispatch_source_check=provider_images_current,
+ verify_only=bool(phase_state.get("started") or phase_state.get("attempt")),
+ dispatch_guard=partial(answer_media_dispatch, self.core, record) if kind == "answer_images" else None)
if phase_state.get("image_keys", list(image_keys)) != list(image_keys):
raise ValueError("App image delivery resources changed after an attempt")
phase_state["image_keys"] = list(image_keys)
+ record["deliveries"][phase] = phase_state
+ _atomic_write_json(path, record)
kwargs = dict(project=self.runtime_root, config_path=config,
message_id=record["event"]["message_id"], text=text, runner=self._reply_runner,
**({"image_keys": image_keys} if image_keys else {}),
@@ -1171,7 +1328,7 @@ def image_authorized() -> bool:
**({"delivery_id": delivery_id} if delivery_id is not None else {}),
finalize_reactions=finalize_source and not edit_only and kind not in {"admission", "progress", "periodic_report"} and not (kind == "terminal" and record.get("commission_resources")),
source_authority_verifier=lambda profile, chat, message: (
- self._source_authority(record, profile, chat, message)
+ self._source_authority(record, profile, chat, message, expected_text=expected_text)
if authorization_check is None or authorization_check() else (None, False)))
progress = record["deliveries"].get(presentation_phase(record, "progress")) or {}
stream = presentation_stream(record)
@@ -1188,13 +1345,10 @@ def image_authorized() -> bool:
raise ValueError("a new private message cannot replace an existing source reply")
def before_send(_intent: str) -> dict[str, bool]:
- # Provider preview is external IO. Recheck the original source and
- # current Core grant afterwards, including every resumed rich part.
+ # Recheck source and grant after preview, including resumed parts.
if authorization_check is not None and not authorization_check():
return {"continue_delivery": False}
- name, verified = self._source_authority(record, record["profile"],
- record["event"]["chat_id"], record["event"]["message_id"])
- if not name or not verified or (authorization_check is not None and not authorization_check()):
+ if not verified_source() or (authorization_check is not None and not authorization_check()):
return {"continue_delivery": False}
phase_state.update(text=text, started=True)
record["deliveries"][phase] = phase_state
@@ -1223,7 +1377,7 @@ def edit_attempt(value: Any) -> None:
phase_state.pop(key, None)
record["deliveries"][phase] = phase_state
_atomic_write_json(path, record)
- return self._deliver(path, record, phase, text, inbox=inbox)
+ return self._deliver(path, record, phase, text, inbox=inbox, finalize_source=finalize_source)
if not edit_only and result.get("blocker") in {"provider_update_too_large", "provider_update_format_unsupported"}:
# Do not truncate long/mention-bearing final answers to fit a
# draft. Close it, then use the established full final sender.
@@ -1232,7 +1386,7 @@ def edit_attempt(value: Any) -> None:
phase_state.pop("edit_from", None)
record["deliveries"][phase] = phase_state
_atomic_write_json(path, record)
- return self._deliver(path, record, phase, text, inbox=inbox)
+ return self._deliver(path, record, phase, text, inbox=inbox, finalize_source=finalize_source)
elif phase_state.get("attempt"):
result = verify_lark_inbox_reply(**kwargs, attempt=phase_state["attempt"], resume_parts=True,
before_send=before_send)
@@ -1274,8 +1428,224 @@ def attempt(value: Any) -> None:
record_event_timing(record["profile"], record["event"]["message_id"], "delivery_returned")
return bool(phase_state["verified"])
+ def _deliver_completed_answer(self, path: Path, record: dict[str, Any], text: str, *,
+ inbox: Callable[[], Path]) -> bool:
+ phase = presentation_phase(record, "terminal")
+ media_phase = "answer_images." + phase
+ old = record["deliveries"].get(phase) or {}
+ if old.get("started") and not record.get("answer_images"):
+ return self._deliver(path, record, "terminal", text, inbox=inbox)
+ if record["event"].get("chat_type") != "p2p" or record["source"].get("private_human_message") is not True:
+ return self._deliver(path, record, "terminal", text, inbox=inbox)
+ body, targets = _private_answer_images(text)
+ if not targets:
+ return self._deliver(path, record, "terminal", text, inbox=inbox)
+
+ def authorized() -> bool:
+ native = self.core.read_request(record["request_ref"])
+ same_request = all(native.get(key) == record[key] for key in ("binding_id", "source", "session_id", "turn_id"))
+ if native.get("status") != "accepted" or not same_request:
+ return False
+ turn = self.core.controller.store.load_turn(native["session_id"], native["turn_id"])
+ return resumable_answer_image_session(self.core, native, turn) is not None
+
+ state = record.setdefault("answer_images", {})
+ # Hold the source and the exact completed body fixed across retries.
+ digest = hashlib.sha256(text.encode()).hexdigest()
+ if state.setdefault("answer_sha256", digest) != digest:
+ return False
+ if old.get("verified") is not True:
+ state.setdefault("status", "pending")
+ _atomic_write_json(path, record)
+ self._deliver(path, record, "terminal", body + "\n\n原图会另发;有链接的图名可打开原始来源。",
+ inbox=inbox, finalize_source=False)
+ # Release the body worker before the pending image work.
+ return False
+ if state.get("status") == "delivered" and record["deliveries"].get(media_phase, {}).get("images_verified") is True:
+ return True
+ now = datetime.now(timezone.utc).timestamp()
+ if _answer_image_retry_waits(record, now):
+ return False
+ state.update(status="pending", next_check_at=now + ANSWER_IMAGE_RETRY_SECONDS)
+ _atomic_write_json(path, record)
+ try:
+ event = record["event"]
+ origin = event.get("content")
+ if (len(targets) > CHAT_IMAGE_MAX_COUNT or event.get("message_type") != "text"
+ or not isinstance(origin, str) or not authorized() or not self._source_authority(
+ record, record["profile"], event["chat_id"], event["message_id"], expected_text=origin)[1]):
+ raise ValueError("answer image source unavailable or not authorized")
+ images = state.get("attachments")
+ if images is None or state.get("source_urls_verified") is not True:
+ images = state["attachments"] = _read_answer_images(origin, targets, images,
+ record["deliveries"].get(media_phase, {}))
+ state["source_urls_verified"] = True
+ _atomic_write_json(path, record)
+ if self._deliver(path, record, media_phase, "原图(按正文顺序)", inbox=inbox,
+ authorization_check=authorized, finalize_source=False,
+ image_attachments=images):
+ if self.reaction_feedback and not _reconcile_outbound_input_reactions(
+ project=self.runtime_root, config_path=inbox(), message_id=record["event"]["message_id"],
+ processing=False, complete=True, before_write=authorized, runner=self._reply_runner).get("ok"):
+ return False
+ state.update(status="delivered")
+ state.pop("attachments", None)
+ _atomic_write_json(path, record)
+ return True
+ except (KeyError, ValueError, OSError, http.client.HTTPException):
+ pass
+ state["status"] = "unverified"
+ _atomic_write_json(path, record)
+ self._deliver(path, record, "answer_image_notice." + phase,
+ "正文已送达,图片尚未核验送达。有链接的图名可打开原始来源。",
+ inbox=inbox, authorization_check=authorized, finalize_source=False)
+ return False
+
+ def _historical_image_source(self, record: Mapping[str, Any], plan: Mapping[str, Any]) -> dict:
+ event = record["event"]
+ terminal = record["deliveries"]["terminal"]
+ if (record["status"] != "delivered" or record.get("context_recovery")
+ or event.get("chat_type") != "p2p" or event.get("message_type") != "text"
+ or record["source"].get("private_human_message") is not True
+ or record["binding_id"] != plan["binding_id"] or record["turn_id"] != plan["turn_id"]
+ or terminal.get("verified") is not True or terminal.get("image_keys")
+ or terminal.get("attempt", {}).get("message_ref") != plan["message_id"]):
+ raise ValueError("original text-only reply unavailable")
+ native = self.core.read_request(record["request_ref"])
+ if (native.get("status") != "accepted" or native.get("origin") != "lark"
+ or native.get("command") is not None or native.get("agent_target")
+ or native.get("delivery_verified") is not True
+ or any(native.get(key) != record.get(key)
+ for key in ("request_ref", "binding_id", "source", "session_id", "turn_id"))):
+ raise ValueError("original request no longer matches")
+ turn = self.core.controller.store.load_turn(record["session_id"], record["turn_id"]) or {}
+ if (not isinstance(answer := (turn.get("response") or {}).get("message"), str)
+ or hashlib.sha256(answer.encode()).hexdigest() != plan["answer_sha256"]
+ or terminal.get("text") != _presentation_text(answer)
+ or not _private_answer_images(answer)[1]
+ or (session := resumable_answer_image_session(self.core, native, turn)) is None):
+ raise ValueError("original completed answer or its authority changed")
+ return session
+
+ def _historical_image_plan(self, record: Mapping[str, Any], plan: Mapping[str, Any]) -> dict:
+ from ..public_source_reader import public_url
+
+ if set(plan) != {"binding_id", "turn_id", "message_id", "answer_sha256", "images"}:
+ raise ValueError("invalid original image repair fields")
+ if (not isinstance(plan["message_id"], str) or not LARK_MESSAGE_ID_PATTERN.fullmatch(plan["message_id"])
+ or not isinstance(plan["answer_sha256"], str)
+ or not re.fullmatch(r"[a-f0-9]{64}", plan["answer_sha256"])
+ or not isinstance(plan["images"], list) or not 1 <= len(plan["images"]) <= CHAT_IMAGE_MAX_COUNT):
+ raise ValueError("invalid original image repair target")
+ images = []
+ for image in plan["images"]:
+ if (not isinstance(image, dict) or set(image) != {"url", "sha256"}
+ or not isinstance(image["sha256"], str) or not re.fullmatch(r"[a-f0-9]{64}", image["sha256"])
+ or not isinstance(image["url"], str)):
+ raise ValueError("each original image requires a URL and its SHA-256")
+ images.append({"url": public_url(image["url"]), "sha256": image["sha256"]})
+ if len({image["url"] for image in images}) != len(images):
+ raise ValueError("duplicate original image URL")
+ self._historical_image_source(record, plan)
+ return {**plan, "images": images}
+
+ def _load_historical_image_plan(self, path: Path, record: dict[str, Any]) -> dict | None:
+ if record.get("historical_answer_images") is not None:
+ return record["historical_answer_images"]
+ if not re.fullmatch(r"[a-f0-9]{24}", path.stem):
+ raise ValueError("invalid original request reference")
+ # This is a private local operator input, scoped to the existing receipt.
+ # It is never loaded from a project, provider message or model response.
+ spec = self.root / "historical-answer-images" / path.name
+ try:
+ with spec.open("rb") as handle:
+ raw = handle.read(40_001)
+ except FileNotFoundError:
+ return None
+ if len(raw) > 40_000:
+ raise ValueError("original image repair descriptor exceeds its size limit")
+ plan = json.loads(raw)
+ if not isinstance(plan, dict) or path.stem != record.get("request_ref"):
+ raise ValueError("invalid original image repair descriptor")
+ plan = self._historical_image_plan(record, plan)
+ state = record["historical_answer_images"] = {"plan": plan, "status": "pending"}
+ _atomic_write_json(path, record)
+ return state
+
+ def _reconcile_historical_answer_images(self, path: Path) -> int:
+ with exclusive_file_lock(path, operation="deliver_private_chat_request"):
+ record = _read_json(path)
+ try:
+ state = self._load_historical_image_plan(path, record)
+ except (KeyError, ValueError, OSError):
+ return 0
+ now = datetime.now(timezone.utc).timestamp()
+ if not state or state.get("status") != "pending" or state.get("next_check_at", 0) > now:
+ return 0
+ state["next_check_at"] = now + ANSWER_IMAGE_RETRY_SECONDS
+ _atomic_write_json(path, record)
+ plan = state["plan"]
+ try:
+ session = self._historical_image_source(record, plan)
+ selected = self.bindings.resolve(binding_id=plan["binding_id"], session=session,
+ session_context=session.get("project_context") or session.get("steward_context"),
+ include_observation=True, **record["source"])
+ event = record["event"]
+ observation = selected["observation"]
+ if selected["binding"]["transport_ref"] != record["profile"]:
+ return 0
+ original_text = normalize_lark_outbound_text(
+ record["deliveries"]["terminal"]["text"], limit=None, preserve_format=True)
+ def original_pair_matches() -> bool:
+ source, reply = self._original_message_pair(record["profile"], event["message_id"], plan["message_id"],
+ runner=self._read_answer_image_source)
+ return answer_image_source_matches(source, event) and historical_image_reply_matches(
+ reply, event["chat_id"], observation, original_text)
+
+ if not original_pair_matches():
+ return 0
+ phase = "answer_images.historical"
+ attachments = state.get("attachments")
+ recheck_sources = attachments is None or state.get("source_urls_verified") is not True
+ if recheck_sources:
+ attachments = _read_answer_images(event["content"], [item["url"] for item in plan["images"]],
+ attachments, record["deliveries"].get(phase, {}))
+ digests = [hashlib.sha256(base64.b64decode(
+ picture["data_url"].split(",", 1)[1], validate=True)).hexdigest() for picture in attachments]
+ if digests != [item["sha256"] for item in plan["images"]]:
+ return 0
+ if recheck_sources:
+ state.update(attachments=attachments, source_urls_verified=True)
+ _atomic_write_json(path, record)
+ def authorized() -> bool:
+ try:
+ return (selected["binding"] in self.bindings.read()["bindings"]
+ and bool(self._historical_image_source(record, plan)))
+ except (KeyError, OSError, ValueError):
+ return False
+
+ if not self._deliver(path, record, phase, "原图(按原文顺序)",
+ inbox=lambda: self._inbox(record, observation=observation),
+ authorization_check=authorized, historical_source_check=original_pair_matches,
+ finalize_source=False,
+ reply_placement="chat_root", image_attachments=attachments):
+ return 0
+ media = record["deliveries"][phase]
+ output = call(self.runner, lark_args(cli_bin=self.cli_bin, profile=record["profile"], tail=[
+ "im", "+messages-mget", "--message-ids", media["attempt"]["message_ref"],
+ "--as", "bot", "--no-reactions", "--format", "json"]))
+ sent = _message(json_payload(output), media["attempt"]["message_ref"]) if output.get("returncode") == 0 else None
+ if not historical_image_reply_matches(sent, event["chat_id"], observation, media["text"], tuple(media["image_keys"])):
+ return 0
+ except (KeyError, ValueError, OSError, http.client.HTTPException):
+ return 0
+ state.update(status="delivered")
+ state.pop("attachments", None)
+ _atomic_write_json(path, record)
+ return 1
+
def _edit_progress(self, path: Path, record: dict[str, Any], text: str, *, inbox: Callable[[], Path], finalizing: bool = False) -> bool:
- text = _presentation_text(text)
+ text = _presentation_text(_private_answer_images(text)[0] if record["event"].get("chat_type") == "p2p" else text)
progress = record["deliveries"].get(presentation_phase(record, "progress")) or {}
if not progress.get("verified"):
return self._deliver(path, record, "progress", text, inbox=inbox)
@@ -1453,7 +1823,7 @@ def inbox() -> Path:
def pending_body_paths(self) -> list[Path]:
"""Find unsent bodies without waiting for historical recovery probes."""
paths = [path for path, row, _ in self._body_discovery.rows()
- if row["status"] != "delivered" and triage.pending(row)]
+ if row["status"] != "delivered" and triage.pending(row) and not _is_answer_image_followup(row)]
return [*paths, *self._pending_web_projection_paths()]
@effect_runtime_request_scope()
@@ -1461,8 +1831,16 @@ def pending_delivery_paths(self) -> list[Path]:
"""The durable transport store is the queue; no in-memory admission."""
paths = []
recovery_candidate = self.core.context_recovery_observer()
+ now = datetime.now(timezone.utc).timestamp()
+ planned_repairs = {item.name for item in (self.root / "historical-answer-images").glob("*.json")}
for path, row, repairs in self._delivery_discovery.rows():
- if row["status"] != "delivered" and triage.pending(row):
+ if row["status"] != "delivered":
+ if triage.pending(row) and not _answer_image_retry_waits(row, now):
+ paths.append(path)
+ elif row.get("historical_images_due_at") is not None and row["historical_images_due_at"] <= now:
+ paths.append(path)
+ elif (not row.get("historical_images_admitted") and path.name in planned_repairs
+ and time.monotonic() >= self._historical_image_probe_at.get(path, 0)):
paths.append(path)
elif (row.get("session_id") and row.get("turn_id")
and time.monotonic() >= self._context_recovery_probe_at.get(path, 0)):
@@ -1480,7 +1858,10 @@ def is_recovery_path(self, path: Path) -> bool:
A verified old notice may need a native recovery probe, but must not
precede an unsent body just because both have zero worker attempts.
"""
- return path.parent != self.root / "web-projections" and _read_json(path).get("status") == "delivered"
+ if path.parent == self.root / "web-projections":
+ return False
+ row = _read_json(path)
+ return row.get("status") == "delivered" or _is_answer_image_followup(row)
def _pending_web_projection_paths(self) -> list[Path]:
paths = []
@@ -1916,6 +2297,15 @@ def reconcile_request(self, path: Path) -> int:
row = _read_json(path)
if "triage" in row:
return triage.reconcile(self, path)
+ historical_images = row.get("historical_answer_images")
+ if (row.get("status") == "delivered" and (
+ (historical_images or {}).get("status") == "pending"
+ or (historical_images is None and (self.root / "historical-answer-images" / path.name).is_file()))):
+ if historical_images is None:
+ self._historical_image_probe_at[path] = time.monotonic() + 60
+ return self._reconcile_historical_answer_images(path)
+ if _answer_image_retry_waits(row, datetime.now(timezone.utc).timestamp()):
+ return 0
record_event_timing(row["profile"], row["event"]["message_id"], "reconciliation_started")
if row["status"] == "delivered" and row.get("session_id") and row.get("turn_id"):
# Discovery may find more candidates than the shared pool can run.
@@ -2012,6 +2402,7 @@ def reconcile_request(self, path: Path) -> int:
config: Path | None = None
selected: dict[str, Any] | None = None
control_receipt_first = False
+ completed_answer = False
def inbox() -> Path:
nonlocal config
@@ -2095,6 +2486,7 @@ def inbox() -> Path:
"本次执行超时,原会话已保留;请发送 /status 查看状态后再决定是否重试。" if turn["status"] == "timed_out" else
"本次未能完成,原会话已保留。")
if turn["status"] == "completed":
+ completed_answer = True
response = self.core.todo_confirmation_reply(record["request_ref"], turn.get("response") or {})
else:
if record["status"] != "rejected" and not control_receipt_first:
@@ -2103,7 +2495,8 @@ def inbox() -> Path:
or _command_text(str(record.get("response_code") or "")) or str(record.get("response") or ""))
if record.get("status_snapshot"):
response = _status_text(record["status_snapshot"], help_requested=native.get("command") == "help")
- if self._deliver(path, record, "terminal", response, inbox=inbox):
+ if (self._deliver_completed_answer(path, record, response, inbox=inbox) if completed_answer else
+ self._deliver(path, record, "terminal", response, inbox=inbox)):
if control_receipt_first:
# The verified result is the useful control receipt.
# Preserve the usual idempotent reaction, after it.
diff --git a/loopx/extensions/lark/private_delivery_discovery.py b/loopx/extensions/lark/private_delivery_discovery.py
index 68f14e5bf9..67d54f423e 100644
--- a/loopx/extensions/lark/private_delivery_discovery.py
+++ b/loopx/extensions/lark/private_delivery_discovery.py
@@ -1,14 +1,135 @@
-"""Read-only scheduling hints for the existing private-message reply worker."""
+"""Read-only source and scheduling checks for private-message delivery."""
from __future__ import annotations
-from collections.abc import Iterator
+import json
+import re
+from collections.abc import Callable, Iterator, Mapping
+from contextlib import ExitStack, contextmanager
from pathlib import Path
-from typing import Any, cast
+from typing import TYPE_CHECKING, Any, cast
from ...chat_store import RESUMABLE_SESSION_STATES, ChatSessionStore, _read_json
-from ...capabilities.native_chat.file_discovery import FileChatDiscovery, file_change_fingerprint
+from ...capabilities.native_chat.file_discovery import FileChatDiscovery, external_source_route_path, file_change_fingerprint
+from ...file_lock import try_exclusive_file_lock
+from .presentation.markdown_post import lark_markdown_readback_matches
+from .private_ingress import private_source_text
from .private_progress import legacy_recovery_repairs
+if TYPE_CHECKING:
+ from ...capabilities.native_chat.external_conversations import ChatExternalConversations
+
+
+_SOURCE_URL = re.compile(r"https://[^\s<>()\[\]\",。;!?]+")
+
+
+def allowed_answer_image_urls(source: str, targets: list[str], *, deadline: float) -> set[str]:
+ """Authorize exact owner URLs and images listed by the original same-host article."""
+ from urllib.parse import urlsplit
+
+ from ..public_source_reader import article, public_url
+
+ def candidate(url: str) -> str | None:
+ try:
+ return public_url(url) if not urlsplit(url).query else None
+ except ValueError:
+ return None
+
+ owner_links = {url for link in _SOURCE_URL.findall(source) if (url := candidate(link))}
+ wanted = {candidate(target) for target in targets}
+ if None in wanted or not owner_links:
+ return set()
+ if wanted <= owner_links:
+ return owner_links
+ if len(owner_links) != 1:
+ return set()
+ [original] = owner_links
+ host = urlsplit(original).hostname
+ if any(urlsplit(url).hostname != host for url in wanted - owner_links):
+ return set()
+ observed = article(original, deadline=deadline, allowed_urls={original})
+ if observed.get("ok") is not True:
+ return set()
+ listed = {url for item in observed["images"]
+ if (url := candidate(item["url"])) and urlsplit(url).hostname == host}
+ allowed_urls = owner_links | listed
+ return allowed_urls if wanted <= allowed_urls else set()
+
+
+def answer_image_source_matches(message: Mapping | None, event: Mapping) -> bool:
+ if message is None or message.get("deleted") is True or message.get("chat_id") != event["chat_id"]:
+ return False
+ sender = message.get("sender")
+ return (isinstance(sender, Mapping) and sender.get("sender_type") == "user"
+ and bool(event["sender_id"]) and sender.get("id") == event["sender_id"]
+ and message.get("msg_type", message.get("message_type")) == "text"
+ and private_source_text(message) == event["content"])
+
+
+def historical_image_reply_matches(message: Mapping | None, chat_id: str, observation: Mapping,
+ text: str, keys: tuple[str, ...] = ()) -> bool:
+ if message is None or message.get("deleted") is True or message.get("chat_id") != chat_id:
+ return False
+ sender = message.get("sender")
+ if (not isinstance(sender, Mapping) or sender.get("sender_type") != "app"
+ or not sender.get("id") or sender["id"] not in (observation["bot_app_id"], observation["bot_open_id"])):
+ return False
+ raw_body = message.get("body")
+ if isinstance(raw_body, Mapping) and isinstance(raw_body.get("content"), str):
+ try:
+ body = json.loads(raw_body["content"])
+ except ValueError:
+ return False
+ # Raw mget keeps authored Markdown in content_v2 and a rendered preview
+ # in content. Match the authored body through the existing wire verifier.
+ if isinstance(body, Mapping) and "content_v2" in body:
+ locale = {"title": body.get("title", ""), "content": body["content_v2"]}
+ message = {**message, "body": {"content": {"zh_cn": locale}}}
+ return lark_markdown_readback_matches(text=text, message=message, image_keys=keys)
+
+
+def resumable_answer_image_session(
+ core: ChatExternalConversations, request: dict[str, Any], turn: dict[str, Any] | None
+) -> dict[str, Any] | None:
+ """Apply source controls before reading the current Session."""
+ store = core.controller.store
+ session_id, turn_id = request["session_id"], request["turn_id"]
+ if (turn is None or turn.get("status") != "completed"
+ or core._source_stop_observed(request, turn, store.events_after(session_id, turn_id, None))):
+ return None
+ session = store.load_session(session_id)
+ return session if session is not None and session.get("status") in RESUMABLE_SESSION_STATES else None
+
+
+@contextmanager
+def answer_media_dispatch(core: ChatExternalConversations, request: dict[str, Any],
+ source_is_current: Callable[[], bool]) -> Iterator[None]:
+ """Check the provider source at dispatch under the original local controls."""
+ store = core.controller.store
+ session_id, turn_id = request["session_id"], request["turn_id"]
+ route = external_source_route_path(core.root, request["binding_id"], request["source"]["source_ref"])
+ # Ingress takes the route before its binding and Session operations.
+ # Contention leaves the original control writer free to finish first.
+ with ExitStack() as fences:
+ for path in (route, core.bindings.path, store._session_path(session_id)):
+ if fences.enter_context(try_exclusive_file_lock(path, operation="dispatch_external_answer_media")) is None:
+ raise ValueError("original media authority is being updated")
+ if not any(binding.get("enabled") is True and binding["binding_id"] == request["binding_id"]
+ and binding["transport_ref"] == request["profile"] for binding in core.bindings.read()["bindings"]):
+ raise ValueError("original media binding unavailable")
+ native = core.read_request(request["request_ref"])
+ if (native.get("status") != "accepted"
+ or any(native.get(key) != request[key] for key in ("binding_id", "source", "session_id", "turn_id"))
+ or resumable_answer_image_session(core, native, store.load_turn(session_id, turn_id)) is None):
+ raise ValueError("original Session or source no longer authorizes an upload")
+ # Feishu has no conditional image upload tied to a message revision.
+ # Do the final provider read here, with these local locks held through
+ # CLI start. Edits made later are checked again before posting the images.
+ if not source_is_current():
+ raise ValueError("original upload source changed")
+ if resumable_answer_image_session(core, native, store.load_turn(session_id, turn_id)) is None:
+ raise ValueError("original Session or source changed during upload authorization")
+ yield
+
def prepared_steward_binding_waits(
request: dict[str, Any], bindings: list[dict[str, Any]], store: ChatSessionStore
@@ -85,6 +206,11 @@ def _observe(self, path: Path) -> tuple[dict[str, Any], bool]:
("status", "request_ref", "binding_id", "session_id", "turn_id")}
view.update(agent_target=bool(row.get("agent_target")),
event={"chat_type": row.get("event", {}).get("chat_type")})
+ historic_images = row.get("historical_answer_images") or {}
+ if historic_images:
+ view["historical_images_admitted"] = True
+ if historic_images.get("status") == "pending":
+ view["historical_images_due_at"] = historic_images.get("next_check_at", 0)
if before is not None and file_change_fingerprint(path) == before:
self._completed[path] = (before, view, repairs)
return view, repairs
diff --git a/loopx/extensions/lark/return_files.py b/loopx/extensions/lark/return_files.py
index e503900a57..d5f652ace6 100644
--- a/loopx/extensions/lark/return_files.py
+++ b/loopx/extensions/lark/return_files.py
@@ -2,14 +2,18 @@
from __future__ import annotations
-import hashlib
import base64
+import errno
+import hashlib
import json
import re
import subprocess
+from contextlib import ExitStack, contextmanager
+from functools import partial
from pathlib import Path
from tempfile import TemporaryDirectory
+from .cli_resolution import run_lark_command_with_dispatch_guard
from .inbox_reply import _default_runner, _json_object, _message
from .outbound import normalize_lark_outbound_text
from .presentation.markdown_post import lark_markdown_readback_attachment_keys, lark_markdown_readback_matches
@@ -24,12 +28,31 @@
# shapes are stated once here instead of being rebuilt at each use site.
RESULT_FILE_KEY_PATTERN = re.compile(r"file_[A-Za-z0-9_-]{1,240}")
CHAT_IMAGE_KEY_PATTERN = re.compile(r"img_[A-Za-z0-9_-]{1,240}")
+LARK_APP_SCOPE_NOT_APPLIED = 99991672
+
+def _image_upload_scope_rejected(result):
+ """Recognize the CLI's explicit App rejection before resource creation."""
+ if result.get("returncode") != 3 or str(result.get("stdout") or "").strip():
+ return False
+ rejected = _json_object(result.get("stderr"))
+ error = rejected.get("error")
+ return (
+ rejected.get("ok") is False and rejected.get("identity") == "bot"
+ and isinstance(error, dict) and error.get("identity") == "bot"
+ and error.get("type") == "authorization"
+ and error.get("subtype") == "app_scope_not_applied"
+ and error.get("code") == LARK_APP_SCOPE_NOT_APPLIED
+ )
-def _call(args, cwd, *, runner, private_transport):
+
+def _call(args, cwd, *, runner, private_transport, dispatch_guard=None):
try:
if private_transport is not None:
- return private_transport.runner([private_transport.cli_bin, *args[1:]], cwd, 30)
+ command = [private_transport.cli_bin, *args[1:]]
+ if dispatch_guard is not None:
+ return run_lark_command_with_dispatch_guard(private_transport.runner, command, cwd, 30, dispatch_guard)
+ return private_transport.runner(command, cwd, 30)
return runner(args) if runner is not None else _default_runner(args, cwd=cwd)
except (OSError, subprocess.SubprocessError):
return {"returncode": 1}
@@ -59,20 +82,56 @@ def _chat_images(attachments):
def uploaded_chat_images(*, root, profile, provider_ref, attachments, private_transport,
- before_upload, verify_only=False):
+ before_upload, verify_only=False, dispatch_guard=None, dispatch_source_check=None):
"""Reuse resource IO; never regenerate an unknown upload or sent intent."""
+ if dispatch_guard is not None and dispatch_source_check is None:
+ raise ValueError("guarded image upload requires current source verification")
return _uploaded_resources(root=root, profile=profile, provider_ref=provider_ref,
attachments=_chat_images(attachments), cache_dir="lark-chat-images", resource="images",
key_field="image_key", key_pattern=CHAT_IMAGE_KEY_PATTERN,
read_bytes=lambda image: image["raw"],
upload_data=lambda _image: {"image_type": "message"}, runner=None,
private_transport=private_transport, before_upload=before_upload, verify_only=verify_only,
- fence_upload=True)
+ fence_upload=True, dispatch_guard=dispatch_guard, dispatch_source_check=dispatch_source_check)
+
+
+def _restore_unstarted_upload(cache, saved):
+ if saved is None:
+ cache.unlink()
+ else:
+ _write(cache, saved)
+
+
+@contextmanager
+def _authorize_upload_dispatch(guard, source_check, cache, saved, *, guards_spawn):
+ unstarted = False
+ try:
+ with ExitStack() as authority:
+ try:
+ authority.enter_context(guard(source_check))
+ except (KeyError, OSError, ValueError) as exc:
+ unstarted = True
+ raise ValueError("original upload authority unavailable") from exc
+ try:
+ yield
+ except OSError as exc:
+ # The native guard surrounds Popen only. EMFILE during pipe
+ # setup or exec leaves no CLI running. A synchronous runner
+ # can instead raise the same error after a provider write.
+ unstarted = guards_spawn and exc.errno in (
+ errno.ENOENT, errno.ENOTDIR, errno.EACCES, errno.EPERM, errno.ENOEXEC, errno.EMFILE,
+ )
+ raise
+ finally:
+ # The resource lock is still held. Release the authority handles
+ # first so restoring a previous rejection can open its temp file.
+ if unstarted:
+ _restore_unstarted_upload(cache, saved)
def _uploaded_resources(*, root, profile, provider_ref, attachments, cache_dir, resource,
key_field, key_pattern, read_bytes, upload_data, runner, private_transport,
- before_upload, verify_only, fence_upload=False):
+ before_upload, verify_only, fence_upload=False, dispatch_guard=None, dispatch_source_check=None):
keys = []
for attachment in attachments:
identity = {"profile": profile, "provider_ref": provider_ref,
@@ -81,10 +140,14 @@ def _uploaded_resources(*, root, profile, provider_ref, attachments, cache_dir,
with exclusive_file_lock(cache):
saved = _read(cache) if cache.exists() else None
if saved is not None:
- if saved.get("identity") != identity or not key_pattern.fullmatch(str(saved.get(key_field, ""))):
+ if saved.get("identity") != identity:
+ raise ValueError("resource upload identity conflict or acknowledgement unavailable")
+ if key_pattern.fullmatch(str(saved.get(key_field, ""))):
+ keys.append(saved[key_field])
+ continue
+ # Prior timeouts and missing acknowledgements remain fenced.
+ if not (fence_upload and saved == {"identity": identity, "upload_rejected_scope": LARK_APP_SCOPE_NOT_APPLIED}):
raise ValueError("resource upload identity conflict or acknowledgement unavailable")
- keys.append(saved[key_field])
- continue
if verify_only:
raise ValueError("sent result file resource record unavailable")
raw = read_bytes(attachment)
@@ -103,9 +166,20 @@ def _uploaded_resources(*, root, profile, provider_ref, attachments, cache_dir,
raise ValueError("result file return authority revoked")
if fence_upload:
_write(cache, {"identity": identity, "upload_started": True})
- uploaded = _call(args, cwd, runner=runner, private_transport=private_transport)
+ guard = None
+ if dispatch_guard is not None:
+ guard = partial(
+ _authorize_upload_dispatch, dispatch_guard, dispatch_source_check, cache, saved,
+ guards_spawn=getattr(private_transport.runner, "dispatch_guard", False) is True,
+ )
+ uploaded = _call(args, cwd, runner=runner, private_transport=private_transport,
+ dispatch_guard=guard)
+ if fence_upload and _image_upload_scope_rejected(uploaded):
+ _write(cache, {"identity": identity, "upload_rejected_scope": LARK_APP_SCOPE_NOT_APPLIED})
+ raise ValueError("result file upload unavailable")
payload = _json_object(uploaded.get("stdout"))
- key = (payload.get("data") or {}).get(key_field)
+ data = payload.get("data")
+ key = data.get(key_field) if isinstance(data, dict) else None
if uploaded.get("returncode") != 0 or payload.get("ok") is False or not key_pattern.fullmatch(str(key or "")):
raise ValueError("result file upload unavailable")
_write(cache, {"identity": identity, key_field: key})
diff --git a/loopx/extensions/public_source_reader.py b/loopx/extensions/public_source_reader.py
index 8ee09b60fb..beb90b44bd 100644
--- a/loopx/extensions/public_source_reader.py
+++ b/loopx/extensions/public_source_reader.py
@@ -21,6 +21,7 @@
import sys
import threading
import time
+from collections.abc import Collection
from html.parser import HTMLParser
from urllib.parse import urljoin, urlsplit, urlunsplit
@@ -101,11 +102,15 @@ def connect(self) -> None:
raise
-def fetch(value: str, *, deadline: float) -> tuple[str, str, bytes]:
+def fetch(value: str, *, deadline: float,
+ allowed_urls: Collection[str] | None = None) -> tuple[str, str, bytes]:
+ """Fetch anonymously, checking any caller's exact public URLs before DNS."""
url = public_url(value)
for _ in range(6):
if time.monotonic() >= deadline:
raise TimeoutError("source timeout")
+ if allowed_urls is not None and url not in allowed_urls:
+ raise ValueError("public source URL not authorized")
u = urlsplit(url)
addresses = public_addresses(u.hostname or "")
for index, address in enumerate(addresses):
@@ -236,9 +241,10 @@ def _text_page_parameters(offset: int, expected_sha256: str | None) -> None:
def article(url: str, *, deadline: float, offset: int = 0,
- expected_sha256: str | None = None) -> dict[str, object]:
+ expected_sha256: str | None = None,
+ allowed_urls: Collection[str] | None = None) -> dict[str, object]:
_text_page_parameters(offset, expected_sha256)
- observed, mime, body = fetch(url, deadline=deadline)
+ observed, mime, body = fetch(url, deadline=deadline, allowed_urls=allowed_urls)
digest = hashlib.sha256(body).hexdigest()
if expected_sha256 is not None and digest != expected_sha256:
return {"ok": False, "error": "public_source_changed"}
diff --git a/loopx/host_loop_activation.py b/loopx/host_loop_activation.py
index 20558dcc73..9943c049ee 100644
--- a/loopx/host_loop_activation.py
+++ b/loopx/host_loop_activation.py
@@ -524,6 +524,7 @@ def _heartbeat_commands(
agent_type: str,
cli_bin: str,
runtime_root: str | None = None,
+ registry: str | None = None,
agent_id: str | None,
available_capabilities: list[str] | None = None,
) -> dict[str, str]:
@@ -558,6 +559,7 @@ def _heartbeat_commands(
goal_id,
cli_bin=cli_bin,
runtime_root=runtime_root,
+ registry_path=registry,
agent_id=agent_id,
agent_scope=agent_scope,
available_capabilities=available_capabilities,
@@ -568,6 +570,7 @@ def _heartbeat_commands(
goal_id,
cli_bin=cli_bin,
runtime_root=runtime_root,
+ registry_path=registry,
agent_id=agent_id,
agent_scope=agent_scope,
available_capabilities=available_capabilities,
@@ -1241,6 +1244,7 @@ def build_host_loop_activation_packet(
goal_id: str,
cli_bin: str = "loopx",
runtime_root: str | None = None,
+ registry: str | None = None,
identity_runtime_root: str | None = None,
agent_id: str | None = None,
registered_agents: list[str] | None = None,
@@ -1266,6 +1270,7 @@ def build_host_loop_activation_packet(
agent_type=canonical,
cli_bin=cli_bin,
runtime_root=runtime_root,
+ registry=registry,
agent_id=str(selected_agent_id) if selected_agent_id else None,
available_capabilities=normalized_available_capabilities,
)
@@ -1328,6 +1333,7 @@ def build_host_loop_activation_packet(
goal_id=goal_id,
agent_id=str(selected_agent_id) if selected_agent_id else None,
runtime_root=runtime_root,
+ registry=registry,
)
elif canonical == "kiro-cli":
surface = kiro_cli_activation(commands, cli_bin)
@@ -1349,6 +1355,7 @@ def build_host_loop_activation_packet(
agent_type=canonical,
cli_bin=cli_bin,
runtime_root=runtime_root,
+ registry=registry,
agent_id=candidate,
available_capabilities=normalized_available_capabilities,
)
diff --git a/loopx/hosts/skill_facade.py b/loopx/hosts/skill_facade.py
index ef32d65f9e..bef38874b3 100644
--- a/loopx/hosts/skill_facade.py
+++ b/loopx/hosts/skill_facade.py
@@ -171,6 +171,7 @@ def antigravity_app_activation(
goal_id: str = "",
agent_id: str | None = None,
runtime_root: str | None = None,
+ registry: str | None = None,
) -> dict[str, Any]:
return skill_facade_cli_activation(
commands,
@@ -183,6 +184,7 @@ def antigravity_app_activation(
goal_id=goal_id,
agent_id=agent_id,
runtime_root=runtime_root,
+ registry=registry,
cli_bin=cli_bin,
),
)
diff --git a/loopx/project_prompt.py b/loopx/project_prompt.py
index de407238af..a76933bbaf 100644
--- a/loopx/project_prompt.py
+++ b/loopx/project_prompt.py
@@ -316,6 +316,7 @@ def render_heartbeat_prompt_command(
*,
cli_bin: str = "loopx",
runtime_root: str | Path | None = None,
+ registry_path: str | Path | None = None,
agent_id: str | None = None,
agent_scope: str = "Codex CLI /goal visible TUI loop",
body: str = "thin",
@@ -337,8 +338,11 @@ def render_heartbeat_prompt_command(
if visible_goal_host
else ""
)
+ prefix = render_cli_command_prefix(
+ cli_bin=cli_bin, runtime_root=runtime_root, registry_path=registry_path
+ )
return (
- f"{render_cli_command_prefix(cli_bin=cli_bin, runtime_root=runtime_root)} "
+ f"{prefix} "
f"heartbeat-prompt --{shell_arg(body)} "
f"--goal-id {shell_arg(goal_id)}{agent_arg}{scope_arg}{capability_args}"
f"{scheduler_args}{visible_goal_arg}" + (" --bootstrap" if bootstrap else "")
@@ -350,6 +354,7 @@ def render_heartbeat_prompt_json_command(
*,
cli_bin: str = "loopx",
runtime_root: str | Path | None = None,
+ registry_path: str | Path | None = None,
agent_id: str | None = None,
agent_scope: str = "Codex CLI /goal visible TUI loop",
body: str = "thin",
@@ -371,8 +376,11 @@ def render_heartbeat_prompt_json_command(
if visible_goal_host
else ""
)
+ prefix = render_cli_command_prefix(
+ cli_bin=cli_bin, runtime_root=runtime_root, registry_path=registry_path
+ )
return (
- f"{render_cli_command_prefix(cli_bin=cli_bin, runtime_root=runtime_root)} "
+ f"{prefix} "
f"--format json heartbeat-prompt --{shell_arg(body)} "
f"--goal-id {shell_arg(goal_id)}{agent_arg}{scope_arg}{capability_args}"
f"{scheduler_args}{visible_goal_arg}" + (" --bootstrap" if bootstrap else "")
diff --git a/loopx/quota.py b/loopx/quota.py
index 450115daaf..4dac35fb65 100644
--- a/loopx/quota.py
+++ b/loopx/quota.py
@@ -1244,6 +1244,7 @@ def record_quota_scheduler_ack(
| None = None,
operator_inbox_urgency_projector: Callable[..., dict[str, Any]] | None = None,
before_decision: Mapping[str, Any] | None = None,
+ turn_instance_id: str | None = None,
) -> dict[str, Any]:
safe_goal_id = _validate_goal_id_path_segment(str(goal_id or ""))
safe_agent_id = normalize_todo_claimed_by(agent_id)
@@ -1278,6 +1279,7 @@ def record_quota_scheduler_ack(
reason_summary=reason_summary,
use_current_hint=use_current_hint,
host_match_observed=host_match_observed,
+ turn_instance_id=turn_instance_id,
)
@@ -1642,7 +1644,7 @@ def spend_quota_slot(
"goal_id": safe_goal_id,
"reason": "effect_ref must be a non-empty string when provided",
}
- if normalized_effect_ref:
+ if normalized_effect_ref and not turn_instance_id:
if not raw_runtime_root:
return {
"ok": False,
@@ -1709,6 +1711,7 @@ def spend_quota_slot(
and not replan_obligation_id
)
settlement_readback = None
+ spend_request = {"slots": slots, "source": source}
if (
not turn_instance_id
and raw_runtime_root
@@ -1727,6 +1730,7 @@ def spend_quota_slot(
allow_unbound_binding=recover_unbound_visible_goal,
registry_path=registry_path,
goal_ref=goal_ref,
+ spend_request=spend_request,
)
inferred_result = (
settlement_readback.identity
@@ -1765,6 +1769,7 @@ def spend_quota_slot(
replan_obligation_id=replan_obligation_id,
registry_path=registry_path,
goal_ref=goal_ref,
+ spend_request=spend_request,
)
if settlement_readback is None:
raise RuntimeError("exact settlement readback unexpectedly returned not-found")
@@ -1784,6 +1789,18 @@ def spend_quota_slot(
"settlement_result": settlement_result_payload(guard_result),
}
identity = guard_result.value
+ refusal = settlement_readback.spend_request_rejection
+ if refusal is not None:
+ return {
+ "ok": False,
+ "mode": "spend-slot",
+ "dry_run": not execute,
+ "appended": False,
+ "goal_id": safe_goal_id,
+ "settlement_identity": identity.as_dict(),
+ "settlement_result": settlement_result_payload(refusal),
+ "reason": refusal.failure.reason if refusal.failure else "invalid original Turn spend",
+ }
spent_result = settlement_readback.settlement
if spent_result.failure is None:
return {
diff --git a/loopx/usage_goal.py b/loopx/usage_goal.py
index ca3d6af5b3..287b103acd 100644
--- a/loopx/usage_goal.py
+++ b/loopx/usage_goal.py
@@ -20,6 +20,9 @@
@contextmanager
def observe_goal_execution(runtime_root: Path, goal_id: str, *, host: str = "unknown") -> Iterator[None]:
+ if usage_ping._environment_suppressed():
+ yield
+ return
stop = threading.Event()
publish = None
try:
@@ -28,10 +31,7 @@ def observe_goal_execution(runtime_root: Path, goal_id: str, *, host: str = "unk
path = usage_ping.state_path()
state = json.loads(path.read_text(encoding="utf-8"))
generation = state.get("generation")
- if (goal_id and generation and state.get("consent") != "disabled"
- and os.environ.get("LOOPX_USAGE_PING") != "0"
- and os.environ.get("DO_NOT_TRACK") != "1"
- and os.environ.get("CI") != "true"):
+ if goal_id and generation and state.get("consent") != "disabled":
key = hashlib.sha256(json.dumps([generation, str(runtime_root.resolve()), goal_id]).encode()).hexdigest()
wall = time.time_ns() // 1_000_000
origin = time.monotonic()
@@ -101,12 +101,12 @@ def observe_quota_cycle(*, registry_path: Path, runtime_root: Path, goal_id: str
at: int, host: str) -> None:
"""Detach binding discovery and session metadata lookup from quota latency."""
try:
+ if usage_ping._environment_suppressed():
+ return
import sys
state = json.loads(usage_ping.state_path().read_text(encoding="utf-8"))
if state.get("consent") == "disabled" or not state.get("generation"):
return
- if os.environ.get("LOOPX_USAGE_PING") == "0" or os.environ.get("DO_NOT_TRACK") == "1" or os.environ.get("CI") == "true":
- return
usage_ping._detach({"registry": str(registry_path), "runtime": str(runtime_root),
"goal": goal_id, "agent": agent_id, "turn": turn_id,
"phase": phase, "at": at, "host": host,
@@ -155,6 +155,8 @@ def _bound_codex_session(registry_path: Path, goal_id: str, agent_id: str | None
def _dispatch_cycle(request) -> None:
+ if usage_ping._environment_suppressed():
+ return
path = Path(request["path"])
state = json.loads(path.read_text(encoding="utf-8"))
if state.get("consent") == "disabled" or state.get("generation") != request["generation"]:
diff --git a/loopx/usage_ping.py b/loopx/usage_ping.py
index 32bf72caa9..6f05f9aa3f 100644
--- a/loopx/usage_ping.py
+++ b/loopx/usage_ping.py
@@ -8,6 +8,7 @@
import sys
import tempfile
import time
+from collections.abc import Mapping
from contextvars import ContextVar
from datetime import datetime, timezone
from pathlib import Path
@@ -139,12 +140,25 @@ def control(action: str, path: Path | None = None, **fields: Any) -> dict[str, A
return payload
+def _environment_suppressed(environment: Mapping[str, str] | None = None) -> bool:
+ """Negative-only host hint; the TS owner still decides every observation."""
+ env = os.environ if environment is None else environment
+ # Match ECMAScript String.trim, including FEFF and excluding U+0085.
+ whitespace = (" \t\n\r\v\f\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005"
+ "\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000\ufeff")
+ return (
+ env.get("LOOPX_USAGE_PING", "").strip(whitespace).lower() in {"0", "false", "no", "off"}
+ or env.get("DO_NOT_TRACK", "").strip(whitespace) not in {"", "0"}
+ or env.get("CI", "").strip(whitespace).lower() not in {"", "0", "false"}
+ )
+
+
def begin(command: str) -> tuple[str, float] | None:
"""Read a small host hint; do not start a synchronous Node process on warm commands."""
_observation.set({})
# Negative-only scheduling hints for common unattended environments. These
# cannot authorize collection; TS still checks every supported switch value.
- if os.environ.get("LOOPX_USAGE_PING") == "0" or os.environ.get("DO_NOT_TRACK") == "1" or os.environ.get("CI") == "true":
+ if _environment_suppressed():
return None
# Whole-runtime migration includes this machine state. Detached observation
# would invalidate its preview or rollback receipt even with other hosts stopped.
@@ -239,6 +253,8 @@ def _pin_caller_paths(request: dict[str, Any]) -> dict[str, Any]:
def _detach(request: dict[str, Any], *, command: list[str] | None = None) -> None:
+ if _environment_suppressed():
+ return
request = _pin_caller_paths(request)
kwargs: dict[str, Any] = {"stdin": subprocess.PIPE, "stdout": subprocess.DEVNULL,
"stderr": subprocess.DEVNULL, "close_fds": True,
diff --git a/pyproject.toml b/pyproject.toml
index b128a7989d..fdc6cddab9 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -98,7 +98,7 @@ include = ["loopx*"]
"*" = ["README.md", "README.zh-CN.md", "docs/*.md", "docs/protocols/*.md"]
"loopx.capabilities.content_ops" = ["templates/*.json", "experiences/*.json"]
"loopx.capabilities.pr_review_queue" = ["experiences/*/*/*.json", "experiences/*.md"]
-"loopx.capabilities.manager_context" = ["skills/loopx-manager/SKILL.md"]
+"loopx.capabilities.manager_context" = ["*.ts", "skills/loopx-manager/SKILL.md"]
"loopx.extensions.lark" = ["extension.toml", "README.md", "docs/*.md"]
"loopx.extensions.openviking_periodic_report" = ["extension.toml"]
"loopx.extensions.openviking_semantic_preference" = ["extension.toml"]
diff --git a/tests/architecture/test_issue_fix_source_vocabulary_owner.py b/tests/architecture/test_issue_fix_source_vocabulary_owner.py
new file mode 100644
index 0000000000..88f2a9cc5e
--- /dev/null
+++ b/tests/architecture/test_issue_fix_source_vocabulary_owner.py
@@ -0,0 +1,165 @@
+"""The issue_fix source trust/freshness vocabularies are stated once.
+
+`repository_context.py` owns the closed sets every issue_fix source payload is
+validated against. `reviewer_recommendation.py` used to restate both under its
+own names, so a vocabulary change had two places to land and a drift would let
+the two validators disagree about the same `trust`/`freshness` fields.
+
+`REVIEWER_SOURCE_KINDS` stays local on purpose: reviewer sources accept only
+`maintainer_map`, a deliberately narrower set than the repository-context one.
+The same freshness triple also appears in `reward_memory/candidate_review.py`,
+a different capability outside this guard's scope.
+"""
+
+from __future__ import annotations
+
+import ast
+from pathlib import Path
+
+import pytest
+
+from loopx.capabilities.issue_fix import repository_context, reviewer_recommendation
+
+REPOSITORY_ROOT = Path(__file__).resolve().parents[2]
+OWNER_MODULE = "loopx/capabilities/issue_fix/repository_context.py"
+
+VOCABULARIES = {
+ "TRUST_LEVELS": repository_context.TRUST_LEVELS,
+ "FRESHNESS_STATES": repository_context.FRESHNESS_STATES,
+}
+
+
+def _string_set(node: ast.expr) -> frozenset[str] | None:
+ if not isinstance(node, ast.Set):
+ return None
+ values = []
+ for element in node.elts:
+ if not isinstance(element, ast.Constant) or not isinstance(element.value, str):
+ return None
+ values.append(element.value)
+ return frozenset(values)
+
+
+def test_the_vocabularies_are_stated_only_by_the_owner() -> None:
+ # A census by value, not by name: a renamed local copy is the same drift.
+ targets = {frozenset(values) for values in VOCABULARIES.values()}
+ found: dict[str, list[str]] = {}
+ package = REPOSITORY_ROOT / "loopx/capabilities/issue_fix"
+ for path in sorted(package.glob("*.py")):
+ relative = path.relative_to(REPOSITORY_ROOT).as_posix()
+ tree = ast.parse(path.read_text(encoding="utf-8"))
+ for node in ast.walk(tree):
+ if not isinstance(node, ast.Assign) or len(node.targets) != 1:
+ continue
+ target = node.targets[0]
+ if not isinstance(target, ast.Name):
+ continue
+ if _string_set(node.value) in targets:
+ found.setdefault(relative, []).append(target.id)
+ assert {f: sorted(names) for f, names in found.items()} == {
+ OWNER_MODULE: sorted(VOCABULARIES)
+ }
+
+
+def test_reviewer_sources_read_the_owner_objects() -> None:
+ assert (
+ reviewer_recommendation.REVIEWER_SOURCE_TRUST_LEVELS
+ is repository_context.TRUST_LEVELS
+ )
+ assert (
+ reviewer_recommendation.REVIEWER_SOURCE_FRESHNESS_STATES
+ is repository_context.FRESHNESS_STATES
+ )
+
+
+def _context_input(*, trust: str, freshness: str) -> dict:
+ return {
+ "schema_version": "issue_fix_repository_context_input_v0",
+ "repository_revision": "abc123",
+ "sources": [
+ {
+ "source_id": "agents-map",
+ "source_kind": "maintainer_map",
+ "reference": "AGENTS.md",
+ "trust": trust,
+ "freshness": freshness,
+ "supports": ["change_scope"],
+ }
+ ],
+ }
+
+
+def _reviewer_input(*, trust: str, freshness: str) -> dict:
+ return {
+ "schema_version": "issue_fix_reviewer_sources_input_v0",
+ "sources": [
+ {
+ "source_id": "codeowners-map",
+ "source_kind": "maintainer_map",
+ "reference": ".github/CODEOWNERS",
+ "trust": trust,
+ "freshness": freshness,
+ "observed_at": "2026-11-15T00:00:00Z",
+ "routes": [
+ {
+ "route_id": "core-route",
+ "match_kind": "path_prefix",
+ "pattern": "loopx/",
+ "primary_reviewers": ["@maintainer"],
+ "fallback_reviewers": [],
+ }
+ ],
+ }
+ ],
+ }
+
+
+def test_repository_context_accepts_every_owner_value() -> None:
+ for trust in sorted(repository_context.TRUST_LEVELS):
+ for freshness in sorted(repository_context.FRESHNESS_STATES):
+ packet = repository_context.build_issue_fix_repository_context_packet(
+ repo="loopx",
+ issue_ref="issue-1",
+ context_input=_context_input(trust=trust, freshness=freshness),
+ )
+ assert packet["ok"] is True
+ assert packet["sources"][0]["trust"] == trust
+ assert packet["sources"][0]["freshness"] == freshness
+
+
+def test_reviewer_sources_accept_every_owner_value() -> None:
+ for trust in sorted(repository_context.TRUST_LEVELS):
+ for freshness in sorted(repository_context.FRESHNESS_STATES):
+ packet = (
+ reviewer_recommendation.build_issue_fix_reviewer_recommendation_packet(
+ repo_path="/nonexistent",
+ reviewer_sources_input=_reviewer_input(
+ trust=trust, freshness=freshness
+ ),
+ )
+ )
+ assert packet["ok"] is True
+ assert packet["reviewer_source_count"] == 1
+
+
+@pytest.mark.parametrize("field", ["trust", "freshness"])
+def test_repository_context_rejects_values_outside_the_owner_set(field: str) -> None:
+ payload = _context_input(trust="verified", freshness="stale")
+ payload["sources"][0][field] = "bogus"
+ with pytest.raises(ValueError, match=field):
+ repository_context.build_issue_fix_repository_context_packet(
+ repo="loopx",
+ issue_ref="issue-1",
+ context_input=payload,
+ )
+
+
+@pytest.mark.parametrize("field", ["trust", "freshness"])
+def test_reviewer_sources_reject_values_outside_the_owner_set(field: str) -> None:
+ payload = _reviewer_input(trust="verified", freshness="stale")
+ payload["sources"][0][field] = "bogus"
+ with pytest.raises(ValueError, match=field):
+ reviewer_recommendation.build_issue_fix_reviewer_recommendation_packet(
+ repo_path="/nonexistent",
+ reviewer_sources_input=payload,
+ )
diff --git a/tests/architecture/test_posix_flag_portability.py b/tests/architecture/test_posix_flag_portability.py
index 86a8da7427..7b42b11b61 100644
--- a/tests/architecture/test_posix_flag_portability.py
+++ b/tests/architecture/test_posix_flag_portability.py
@@ -96,16 +96,13 @@ def test_no_module_exceeds_its_bare_posix_flag_ceiling() -> None:
observed = observed_ceiling()
exceeded = {
- path: {"ceiling": ceiling.get(path, 0), "observed": uses}
+ path: [f"{line}: {what}" for line, what in uses]
for path, uses in observed.items()
if len(uses) > ceiling.get(path, 0)
}
assert not exceeded, (
"bare POSIX-only os flags grew: "
- + json.dumps(
- {path: [f"{line}: {what}" for line, what in uses] for path, uses in exceeded.items()},
- indent=2,
- )
+ + json.dumps(exceeded, indent=2)
+ ". Read the flag with `getattr(os, \"\", 0)` the way the peer, completion "
"and decision-capture readers do, so a host without it refuses or degrades on its "
"own terms instead of raising AttributeError first. Raising a ceiling in "
diff --git a/tests/capabilities/test_capability_configuration_ui.py b/tests/capabilities/test_capability_configuration_ui.py
index c9eee87560..fce9397390 100644
--- a/tests/capabilities/test_capability_configuration_ui.py
+++ b/tests/capabilities/test_capability_configuration_ui.py
@@ -1,5 +1,9 @@
from __future__ import annotations
+import json
+import subprocess
+import sys
+
import pytest
from loopx.capabilities.configuration_ui import (
@@ -19,6 +23,123 @@
)
+@pytest.fixture(autouse=True)
+def isolated_native_profile_discovery(tmp_path, monkeypatch):
+ from loopx import codex_app_thread_activity
+
+ discover = codex_app_thread_activity.codex_homes
+ monkeypatch.setattr(
+ codex_app_thread_activity, "codex_homes",
+ lambda: discover(env={}, user_home=tmp_path),
+ )
+
+
+@pytest.mark.parametrize("capability_id", [
+ "context_delivery", "goal_capability_organization", "goal_storage",
+ "todo_replan_cadence", "periodic_report", "manager_runtime",
+ "steward_executor", "multi_subagent", "peer_task_coordination",
+ "explore_graph", "explore_harness", "change_quality_qualification",
+ "progress_review", "pull_request_review", "local_authority_shadow",
+ "coordination_runtime_shadow", "lark_kanban_heartbeat_sync", "reward_memory",
+ "lark_event_inbox", "unregistered-editor", "",
+])
+def test_editor_does_not_load_unselected_provider(capability_id, tmp_path):
+ source = '''import importlib.abc,json,sys
+capability=sys.argv[1]
+blocked=['loopx.capabilities.native_chat']
+if capability!='steward_executor':blocked.append('loopx.capabilities.steward_executor')
+class UnavailableProvider(importlib.abc.MetaPathFinder):
+ def find_spec(self,fullname,path=None,target=None):
+ if any(fullname==name or fullname.startswith(name+'.') for name in blocked):
+ raise ImportError('unselected provider unavailable')
+sys.meta_path.insert(0,UnavailableProvider())
+from loopx.capabilities.configuration_ui import capability_configuration_editor,resolve_capability_configuration
+editor=capability_configuration_editor(capability)
+assert editor['schema_version']=='capability_configuration_editor_v0'
+assert not any(any(name==root or name.startswith(root+'.') for root in blocked) for name in sys.modules)
+if capability=='periodic_report':
+ resolved=resolve_capability_configuration(capability,capability_default={'enabled':False,'timezone':'UTC'})
+ assert resolved['source']=='capability_default' and resolved['configuration']=={'enabled':False,'timezone':'UTC'}
+print(json.dumps(editor))
+'''
+ result = subprocess.run(
+ [sys.executable, "-I", "-c", source, capability_id],
+ cwd=tmp_path, capture_output=True, text=True, timeout=30,
+ )
+ assert result.returncode == 0, result.stderr
+ editor = json.loads(result.stdout)
+ if capability_id in ("unregistered-editor", ""):
+ assert editor == {
+ "schema_version": "capability_configuration_editor_v0",
+ "editable": False, "supported_scopes": [], "writable_scopes": [],
+ "fields": [],
+ "read_only_reason": "No Dashboard editor is registered for this capability.",
+ }
+
+
+def test_codex_editor_uses_existing_profile_owner_and_nullable_options(tmp_path):
+ from loopx.capabilities.steward_executor.machine_defaults import steward_reasoning_efforts
+
+ (tmp_path / ".codex").mkdir()
+ (tmp_path / ".codex-blue").mkdir()
+ (tmp_path / ".codex-red").mkdir()
+ (tmp_path / ".codex_bad").mkdir()
+ fields = {row["key"]: row for row in capability_configuration_editor("codex_chat")["fields"]}
+ assert fields["codex_profile"]["options"] == ["codex", "codex-blue", "codex-red"]
+ assert fields["codex_profile"]["nullable"] is True
+ assert fields["reasoning_effort"]["options"] == list(steward_reasoning_efforts())
+ assert fields["model"]["nullable"] is True
+ assert fields["service_tier"]["nullable"] is True
+
+
+@pytest.mark.parametrize("capability_id,expected", [
+ ("codex_chat", ["endpoints", "efforts", "profiles"]),
+ ("steward_executor", ["endpoints", "efforts"]),
+])
+def test_selected_editor_preserves_existing_owner_order(capability_id, expected, monkeypatch):
+ from loopx import codex_app_thread_activity
+ from loopx.capabilities.steward_executor import machine_defaults
+
+ calls = []
+
+ def observed(owner, name, label):
+ original = getattr(owner, name)
+
+ def call():
+ calls.append(label)
+ return original()
+
+ monkeypatch.setattr(owner, name, call)
+
+ observed(machine_defaults, "steward_executor_endpoints", "endpoints")
+ observed(machine_defaults, "steward_reasoning_efforts", "efforts")
+ observed(codex_app_thread_activity, "codex_homes", "profiles")
+ capability_configuration_editor(capability_id)
+ assert calls == expected
+
+
+@pytest.mark.parametrize("capability_id,provider", [
+ ("codex_chat", "native_chat"), ("steward_executor", "steward_executor"),
+])
+def test_editor_exposes_selected_provider_unavailability(capability_id, provider, tmp_path):
+ source = '''import importlib.abc,sys
+class UnavailableProvider(importlib.abc.MetaPathFinder):
+ def find_spec(self,fullname,path=None,target=None):
+ root='loopx.capabilities.'+sys.argv[2]
+ if fullname==root or fullname.startswith(root+'.'):raise ImportError('selected provider unavailable')
+sys.meta_path.insert(0,UnavailableProvider())
+from loopx.capabilities.configuration_ui import capability_configuration_editor
+try:capability_configuration_editor(sys.argv[1])
+except ImportError as error:assert str(error)=='selected provider unavailable'
+else:raise AssertionError('selected provider failure was hidden')
+'''
+ result = subprocess.run(
+ [sys.executable, "-I", "-c", source, capability_id, provider],
+ cwd=tmp_path, capture_output=True, text=True, timeout=30,
+ )
+ assert result.returncode == 0, result.stderr
+
+
def test_machine_catalog_only_uses_dashboard_supported_editor_kinds() -> None:
"""A Goal-only descriptor must not make the whole machine page unreadable."""
diff --git a/tests/capabilities/test_pr_wait_lifecycle.py b/tests/capabilities/test_pr_wait_lifecycle.py
index 89951dabed..780783adf9 100644
--- a/tests/capabilities/test_pr_wait_lifecycle.py
+++ b/tests/capabilities/test_pr_wait_lifecycle.py
@@ -249,21 +249,19 @@ def read(inspector):
@pytest.mark.parametrize("provider", ["file", "sqlite"])
def test_lost_observation_response_refreshes_cached_readiness(tmp_path, monkeypatch, provider):
- from loopx.capabilities.issue_fix import pr_wait_hook
-
registry, runtime, _, todo_id, argv = _native_wait_turn(tmp_path, monkeypatch, provider)
# Capture the old pending projection before permission is configured.
_invoke_guard(argv, "capture-pending", "--write-projection-cache")
_declare_observation(registry)
monkeypatch.setattr(pr_lifecycle, "fetch_github_pr_lifecycle_payload", lambda *a, **kw: {
"url": "https://github.com/owner/repo/pull/42", "state": "MERGED", "mergedAt": "2026-10-01T01:12:00Z"})
- original = pr_wait_hook.reconcile_pr_wait_dependencies
+ original = pr_wait_reconcile.reconcile_pr_wait_dependencies
def response_lost(**kwargs):
original(**kwargs)
raise OSError("response lost after local commit")
- monkeypatch.setattr(pr_wait_hook, "reconcile_pr_wait_dependencies", response_lost)
+ monkeypatch.setattr(pr_wait_reconcile, "reconcile_pr_wait_dependencies", response_lost)
result = _invoke_guard(argv, "lost-response", "--use-projection-cache")
readback = _observation(result)
assert readback["external_read_count"] is None and readback["state_written"] is None
@@ -291,8 +289,6 @@ def test_unavailable_permission_source_never_contacts_provider(tmp_path, monkeyp
@pytest.mark.parametrize("provider", ["file", "sqlite"])
def test_full_source_selection_is_independent_of_bounded_hook_diagnostics(tmp_path, monkeypatch, provider):
- from loopx.capabilities.issue_fix import pr_wait_hook
-
isolate_sqlite_runtime(tmp_path, monkeypatch)
lines = tuple(line for number in range(1, 501) for line in (
f"- [ ] [P2] Continue dependency {number}.",
@@ -301,7 +297,7 @@ def test_full_source_selection_is_independent_of_bounded_hook_diagnostics(tmp_pa
_, runtime, registry = _write_live_fixture(tmp_path, provider=provider, extra_agent_todo_lines=lines)
_declare_observation(registry)
calls, full_results = [], []
- original = pr_wait_hook.reconcile_pr_wait_dependencies
+ original = pr_wait_reconcile.reconcile_pr_wait_dependencies
def capture(**kwargs):
result = original(**kwargs)
@@ -312,7 +308,7 @@ def fetch(reference, **kwargs):
calls.append(reference)
return {"url": f"https://github.com/{reference['repo']}/pull/{reference['number']}", "state": "OPEN"}
- monkeypatch.setattr(pr_wait_hook, "reconcile_pr_wait_dependencies", capture)
+ monkeypatch.setattr(pr_wait_reconcile, "reconcile_pr_wait_dependencies", capture)
monkeypatch.setattr(pr_lifecycle, "fetch_github_pr_lifecycle_payload", fetch)
dispatch = extend_turn_start_dispatch({}, registry_path=registry, runtime_root=runtime,
goal_id="loopx-turn-fixture", agent_id="codex-fixture", available=None)
diff --git a/tests/control_plane/cli_output_fixture.py b/tests/control_plane/cli_output_fixture.py
index 59ef10a4a2..31e72de027 100644
--- a/tests/control_plane/cli_output_fixture.py
+++ b/tests/control_plane/cli_output_fixture.py
@@ -7,12 +7,37 @@
import json
import os
from pathlib import Path
+import re
import shlex
import shutil
import subprocess
import tempfile
+_POWERSHELL_READER_WORDS = re.compile(r"'(?:''|[^'])*'|[^'\s]+")
+_POWERSHELL_DOUBLED_QUOTE = re.compile(r"(['\u2018\u2019\u201a\u201b])\1")
+_DIRECT_POWERSHELL_READER = re.compile(r"^([A-Za-z0-9_./-]+) '")
+
+
+def _reader_argv(command: str) -> list[str]:
+ """Decode the literal-only form of the generated read command into argv."""
+ direct = _DIRECT_POWERSHELL_READER.match(command)
+ if not command.startswith("& ") and not (
+ direct and (os.name == "nt" or direct[1].lower().endswith(".ps1"))
+ ):
+ return shlex.split(command)
+ # The host renderer uses '& ' only for a quoted executable. It also
+ # selects PowerShell for .ps1 on POSIX. shlex drops its doubled quotes.
+ literal_command = command.removeprefix("& ")
+ words = _POWERSHELL_READER_WORDS.findall(literal_command)
+ assert " ".join(words) == literal_command, "unexpected syntax in the generated reader command"
+ return [
+ _POWERSHELL_DOUBLED_QUOTE.sub(r"\1", word[1:-1])
+ if word.startswith("'") and word.endswith("'") else word
+ for word in words
+ ]
+
+
def budget_fixture_path(root: Path) -> Path:
suffix = hashlib.sha256(str(root).encode("utf-8")).hexdigest()[:12]
if os.name == "nt":
@@ -61,7 +86,7 @@ def read_pinned_cli_body(command: str) -> tuple[str, int, int]:
reader_bytes = calls = 0
identity = total = digest = None
while True:
- words = shlex.split(command)
+ words = _reader_argv(command)
quota = words.index("quota")
if words[quota + 1] != "context-read":
raise AssertionError("context recovery must be a read, never another guard")
@@ -75,6 +100,10 @@ def read_pinned_cli_body(command: str) -> tuple[str, int, int]:
if identity is None:
identity = scope
assert scope == identity, ("context continuation changed its source or reader route", scope, identity)
+ if words[0].lower().endswith(".ps1"):
+ from loopx.command_invocation import command_argv
+
+ words = command_argv(Path(words[0]), words[1:])
result = subprocess.run(words, capture_output=True, text=True, encoding="utf-8",
errors="strict", check=False, timeout=30)
assert result.returncode == 0, result.stdout + result.stderr
diff --git a/tests/control_plane/test_cli_output_differential.py b/tests/control_plane/test_cli_output_differential.py
index 8cd6b165e9..6942daece2 100644
--- a/tests/control_plane/test_cli_output_differential.py
+++ b/tests/control_plane/test_cli_output_differential.py
@@ -2,6 +2,7 @@
import copy
import json
+import shutil
import sys
import pytest
@@ -24,6 +25,74 @@
)
+def test_pinned_reader_follows_a_powershell_command_with_literal_paths(tmp_path, monkeypatch):
+ import hashlib
+ import os
+ from pathlib import Path
+
+ from loopx import install_contract
+ from tests.control_plane.cli_output_fixture import read_pinned_cli_body
+
+ executable = Path(sys.executable)
+ if os.name != "nt":
+ # A path requiring PowerShell's call operator reproduces the native
+ # command shape even when the reader itself runs on POSIX.
+ launcher = tmp_path / "test Python"
+ launcher.symlink_to(executable)
+ executable = launcher
+ repository = Path(__file__).resolve().parents[2]
+ monkeypatch.setenv("PYTHONPATH", str(repository))
+ monkeypatch.setattr(install_contract, "_is_windows_host", lambda: True)
+ source = tmp_path / "owner's ‘saved capture’.json"
+ payload = {"requirements": "保留原要求和停止条件。🙂" * 40, "turn": "original"}
+ raw = json.dumps(payload, ensure_ascii=False).encode()
+ source.write_bytes(raw)
+ command = install_contract.render_host_cli_command([
+ str(executable), "-m", "loopx.entrypoint", "--format", "json",
+ "quota", "context-read", "--capture-file", str(source),
+ "--capture-sha256", hashlib.sha256(raw).hexdigest(),
+ "--page-bytes", "256", "--max-page-output-bytes", "4000",
+ ])
+ assert command.startswith("& ")
+
+ body, calls, reader_bytes = read_pinned_cli_body(command)
+ assert body.encode() == source.read_bytes() == raw
+ assert calls > 1 and reader_bytes > len(raw)
+
+
+def test_pinned_reader_follows_a_relative_powershell_launcher(tmp_path, monkeypatch):
+ import hashlib
+ import os
+ from pathlib import Path
+
+ from loopx.command_invocation import loopx_cli_argv
+ from loopx.control_plane.turn_driver.decision_capture import context_capture_read_command
+ from tests.control_plane.cli_output_fixture import read_pinned_cli_body
+
+ if os.name != "nt" and shutil.which("pwsh") is None:
+ pytest.skip("Requires PowerShell 7")
+ repository = Path(__file__).resolve().parents[2]
+ monkeypatch.chdir(tmp_path)
+ launcher_arg = "./bin/loopx.ps1"
+ launcher = tmp_path / launcher_arg
+ launcher.parent.mkdir()
+ shutil.copyfile(repository / "scripts" / "loopx.ps1", launcher)
+ pointer = tmp_path / "release.json"
+ pointer.write_text(json.dumps({"release_root": str(repository), "python": sys.executable}), encoding="utf-8")
+ monkeypatch.setenv("LOOPX_CURRENT_RELEASE_FILE", str(pointer))
+ source = tmp_path / "owner's ‘saved capture’.json"
+ raw = json.dumps({"requirements": "完整的验收条件🙂" * 30}, ensure_ascii=False).encode("utf-8")
+ source.write_bytes(raw)
+ digest = hashlib.sha256(raw).hexdigest()
+ route = loopx_cli_argv(launcher_arg)
+ command = context_capture_read_command(source, digest, reader_argv=route,
+ page_bytes=256, max_page_output_bytes=4000)
+ assert command.startswith(launcher_arg + " '")
+
+ body, calls, reader_bytes = read_pinned_cli_body(command)
+ assert body.encode("utf-8") == raw and calls > 1 and reader_bytes > len(raw)
+
+
def test_probe_process_failure_preserves_utf8_diagnostics(tmp_path):
import runpy
from pathlib import Path
diff --git a/tests/control_plane/test_creation_allowance_hold.py b/tests/control_plane/test_creation_allowance_hold.py
new file mode 100644
index 0000000000..6793d22b5d
--- /dev/null
+++ b/tests/control_plane/test_creation_allowance_hold.py
@@ -0,0 +1,340 @@
+"""Saved creation allowance uses real HTTP, quota and canonical File/SQLite.
+
+The native transport is scripted; the existing six-check oracle is unchanged.
+"""
+from copy import deepcopy
+from contextlib import contextmanager
+import hashlib
+import json
+import threading
+
+import pytest
+
+from test_creation_task_queue import (
+ create_requested_task, restarted_host, task_row, wait_for_creation_turn,
+)
+from test_original_task_entry import cart_host as cart_host
+from test_new_goal_storage_defaults import environment as environment
+from loopx.capabilities.native_chat.steward_managed_mode import ManagedSessionTurn
+from loopx.configure_goal import configure_goal
+
+
+@pytest.fixture
+def consumption(monkeypatch):
+ condition, calls = threading.Condition(), []
+ consume = ManagedSessionTurn.consume_creation_request
+
+ def observe(self, creation):
+ try:
+ return consume(self, creation)
+ finally:
+ with condition:
+ calls.append(deepcopy(creation))
+ condition.notify_all()
+
+ monkeypatch.setattr(ManagedSessionTurn, "consume_creation_request", observe)
+
+ def after(previous):
+ with condition:
+ assert condition.wait_for(lambda: len(calls) > previous, timeout=15)
+
+ return calls, after
+
+
+def quota(controller, value):
+ result = configure_goal(registry_path=controller.registry_path,
+ goal_id="cart-repair", quota_compute=value, execute=True)
+ assert result["written"] is True, result
+
+
+def paused_request(host, consumption):
+ _, request, controller, capture, project, _ = host
+ creation, ids, check = create_requested_task(host)
+ saved = creation["receipt"]["original_task_request"]
+ quota(controller, 0)
+ (project / "execute-native").write_text("valid")
+ before = capture.read_bytes()
+ count = len(consumption[0])
+ status, result = request(f"/api/actions/{check['proposal_id']}/apply", {})
+ assert status == 200 and result["turn"] is None, result
+ consumption[1](count)
+ original = controller.store.turn_for_client(ids["session_id"], "managed." + saved["operation_id"])
+ if original is not None:
+ original = controller.wait_for_turn(session_id=ids["session_id"], turn_id=original["turn_id"], timeout_sec=20)
+ assert original is None, original
+ assert capture.read_bytes() == before
+ return creation, ids, saved, before
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("resume", ["signal", "restart"])
+def test_saved_creation_waits_for_actual_quota_then_runs_same_operation_once(
+ environment, cart_host, consumption, provider, resume
+):
+ environment[0](provider)
+ actions, request, controller, capture, project, runtime = cart_host
+ creation, ids, saved, before = paused_request(cart_host, consumption)
+ sid, todo = ids["session_id"], saved["todo_id"]
+ original_session = controller.store.load_session(sid)
+ checker_hash = hashlib.sha256((project / "checks.py").read_bytes()).hexdigest()
+ assert task_row(runtime, todo)["status"] == "open"
+ for value in (0, 1):
+ if value:
+ quota(controller, value)
+ status, view = request(f"/api/chat/sessions/{sid}/native-turn?original_task=true&agent_id=codex")
+ assert status == 200 and view["task"]["creation_request"] == saved, view
+ assert view["task"]["creation_turn"] is None
+ assert controller.store.turn_for_client(sid, "managed." + saved["operation_id"]) is None
+ assert capture.read_bytes() == before
+ if resume == "signal":
+ controller.turn_release_wakeup.notify()
+ turn = wait_for_creation_turn(controller, sid, saved["operation_id"])
+ finished = controller.wait_for_turn(session_id=sid, turn_id=turn["turn_id"], timeout_sec=20)
+ else:
+ with restarted_host(cart_host) as (restored, _):
+ turn = wait_for_creation_turn(restored, sid, saved["operation_id"])
+ finished = restored.wait_for_turn(session_id=sid, turn_id=turn["turn_id"], timeout_sec=30)
+ assert restored.store.load_session(sid)["upstream_thread_id"] == original_session["upstream_thread_id"]
+ restored.turn_release_wakeup.notify()
+ restored.resume_creation_requests()
+ assert restored.store.turn_for_client(sid, "managed." + saved["operation_id"])["turn_id"] == turn["turn_id"]
+ assert finished["response"]["native_loopx"]["status"] == "committed", finished
+ assert finished["loopx_request"]["creation_request"] == saved
+ assert finished["response"]["native_loopx"]["effects"]["quota_spent"] is True
+ assert task_row(runtime, todo)["status"] == "done"
+ assert hashlib.sha256((project / "checks.py").read_bytes()).hexdigest() == checker_hash
+ calls = [json.loads(line) for line in capture.read_bytes()[len(before):].splitlines()]
+ assert sum(row.get("method") == "turn/start" for row in calls) == 1
+ assert not any(row.get("method") == "thread/start" for row in calls)
+ assert actions.load(creation["proposal_id"])["receipt"]["original_task_request"] == saved
+
+
+def assert_pending(host, saved, before):
+ _, _, controller, capture, _, runtime = host
+ assert controller.store.turn_for_client(saved["session_id"], "managed." + saved["operation_id"]) is None
+ assert capture.read_bytes() == before
+ assert task_row(runtime, saved["todo_id"])["status"] == "open"
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("source", ["missing", "changed"])
+def test_actual_validation_source_refusal_retains_unstarted_creation(
+ environment, cart_host, consumption, provider, source
+):
+ environment[0](provider)
+ _, _, controller, _, project, _ = cart_host
+ _, _, saved, before = paused_request(cart_host, consumption)
+ checker = project / "checks.py"
+ if source == "missing":
+ checker.unlink()
+ else:
+ checker.write_text(checker.read_text() + "\n# A changed verifier requires new owner approval.\n")
+ quota(controller, 1)
+ count = len(consumption[0])
+ controller.turn_release_wakeup.notify()
+ consumption[1](count)
+ assert_pending(cart_host, saved, before)
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_goal_commit_guard_refusal_leaves_saved_creation_pending(
+ environment, cart_host, consumption, monkeypatch, provider
+):
+ from loopx import attached_session
+ environment[0](provider)
+ _, _, controller, _, _, _ = cart_host
+ _, _, saved, before = paused_request(cart_host, consumption)
+
+ @contextmanager
+ def refuse():
+ raise ValueError("the original Goal commit guard refused publication")
+ yield
+
+ monkeypatch.setattr(attached_session, "managed_session_goal_commit", lambda **_: refuse)
+ quota(controller, 1)
+ count = len(consumption[0])
+ controller.turn_release_wakeup.notify()
+ consumption[1](count)
+ assert_pending(cart_host, saved, before)
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_quota_drop_after_acceptance_keeps_same_turn_queued_until_fresh_admission(
+ environment, cart_host, consumption, monkeypatch, provider
+):
+ environment[0](provider)
+ _, _, controller, capture, _, runtime = cart_host
+ _, _, saved, before = paused_request(cart_host, consumption)
+ accept = controller.store.accept_managed_turn
+ dropped = False
+
+ def quota_drop(*args, **kwargs):
+ nonlocal dropped
+ accepted = accept(*args, **kwargs)
+ if not dropped and kwargs.get("client_turn_id") == "managed." + saved["operation_id"]:
+ dropped = True
+ quota(controller, 0)
+ return accepted
+
+ monkeypatch.setattr(controller.store, "accept_managed_turn", quota_drop)
+ quota(controller, 1)
+ count = len(consumption[0])
+ controller.turn_release_wakeup.notify()
+ consumption[1](count)
+ queued = controller.store.turn_for_client(saved["session_id"], "managed." + saved["operation_id"])
+ assert queued is not None and queued["status"] == "queued", queued
+ assert queued["started_at"] is None and queued["upstream_turn_id"] is None
+ assert queued["delta_count"] == 0 and "_acceptance" not in queued
+ assert capture.read_bytes() == before
+ assert task_row(runtime, saved["todo_id"])["status"] == "open"
+ count = len(consumption[0])
+ controller.turn_release_wakeup.notify()
+ consumption[1](count)
+ held = controller.store.turn_for_client(saved["session_id"], "managed." + saved["operation_id"])
+ assert held["turn_id"] == queued["turn_id"] and held["status"] == "queued", held
+ assert held["started_at"] is None and held["upstream_turn_id"] is None
+ assert capture.read_bytes() == before
+ quota(controller, 1)
+ controller.turn_release_wakeup.notify()
+ finished = controller.wait_for_turn(session_id=saved["session_id"], turn_id=queued["turn_id"], timeout_sec=20)
+ assert finished["response"]["native_loopx"]["status"] == "committed", finished
+ assert finished["loopx_request"]["creation_request"] == saved
+ assert controller.store.turn_for_client(saved["session_id"], "managed." + saved["operation_id"])["turn_id"] == queued["turn_id"]
+ calls = [json.loads(line) for line in capture.read_bytes()[len(before):].splitlines()]
+ assert sum(row.get("method") == "turn/start" for row in calls) == 1
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_goal_wide_run_cannot_admit_another_todo_for_saved_creation(
+ environment, cart_host, consumption, monkeypatch, provider
+):
+ from loopx.control_plane.quota import live_decision
+ environment[0](provider)
+ _, _, controller, _, _, _ = cart_host
+ _, _, saved, before = paused_request(cart_host, consumption)
+ build = live_decision.build_live_quota_should_run_decision
+
+ def different_selected_todo(*args, **kwargs):
+ decision = build(*args, **kwargs)
+ assert decision["ok"] is True and decision["should_run"] is True
+ decision["selected_todo"]["todo_id"] = "todo_independent_work"
+ return decision
+
+ monkeypatch.setattr(live_decision, "build_live_quota_should_run_decision", different_selected_todo)
+ quota(controller, 1)
+ count = len(consumption[0])
+ controller.turn_release_wakeup.notify()
+ consumption[1](count)
+ assert_pending(cart_host, saved, before)
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_actual_dependency_wait_cannot_borrow_another_runnable_todo(
+ environment, cart_host, consumption, provider
+):
+ from loopx.capabilities.native_chat.steward_commands import native_command
+ environment[0](provider)
+ _, _, controller, capture, _, runtime = cart_host
+ _, _, saved, before = paused_request(cart_host, consumption)
+ known = {row["todo_id"] for row in native_command(controller,
+ ["todo", "list", "--goal-id", "cart-repair"])["todos"]}
+ native_command(controller, ["todo", "add", "--goal-id", "cart-repair", "--role", "agent",
+ "--text", "Complete the independent prerequisite", "--claimed-by", "codex"])
+ current = native_command(controller, ["todo", "list", "--goal-id", "cart-repair"])["todos"]
+ prerequisite, = {row["todo_id"] for row in current} - known
+ native_command(controller, ["todo", "update", "--goal-id", "cart-repair", "--todo-id", saved["todo_id"],
+ "--agent-id", "codex", "--status", "deferred", "--reason", "Wait for the actual prerequisite",
+ "--resume-when", "todo_done:" + prerequisite])
+ quota(controller, 1)
+ owner = ManagedSessionTurn(controller)
+ request = owner._request(saved["session_id"], saved["agent_id"], saved["operation_id"], saved["todo_id"])
+ assert owner._creation_allowance(request) is False
+ count = len(consumption[0])
+ controller.turn_release_wakeup.notify()
+ consumption[1](count)
+ assert controller.store.turn_for_client(saved["session_id"], "managed." + saved["operation_id"]) is None
+ assert capture.read_bytes() == before
+ assert task_row(runtime, saved["todo_id"])["status"] == "deferred"
+ assert task_row(runtime, prerequisite)["status"] == "open"
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_source_exception_after_acceptance_preserves_original_unstarted_turn(
+ environment, cart_host, consumption, monkeypatch, provider
+):
+ environment[0](provider)
+ _, _, controller, capture, project, _ = cart_host
+ _, _, saved, before = paused_request(cart_host, consumption)
+ checker = project / "checks.py"
+ checker_bytes = checker.read_bytes()
+ accept = controller.store.accept_managed_turn
+
+ def missing_after_accept(*args, **kwargs):
+ accepted = accept(*args, **kwargs)
+ if kwargs.get("client_turn_id") == "managed." + saved["operation_id"]:
+ checker.unlink()
+ return accepted
+
+ monkeypatch.setattr(controller.store, "accept_managed_turn", missing_after_accept)
+ quota(controller, 1)
+ count = len(consumption[0])
+ controller.turn_release_wakeup.notify()
+ consumption[1](count)
+ queued = controller.store.turn_for_client(saved["session_id"], "managed." + saved["operation_id"])
+ assert queued is not None and queued["status"] == "queued", queued
+ assert queued["started_at"] is None and queued["upstream_turn_id"] is None
+ assert queued["delta_count"] == 0 and "_acceptance" not in queued
+ assert capture.read_bytes() == before
+ count = len(consumption[0])
+ controller.turn_release_wakeup.notify()
+ consumption[1](count)
+ held = controller.store.turn_for_client(saved["session_id"], "managed." + saved["operation_id"])
+ assert held["turn_id"] == queued["turn_id"] and held["status"] == "queued", held
+ assert held["started_at"] is None and held["upstream_turn_id"] is None
+ assert capture.read_bytes() == before
+ checker.write_bytes(checker_bytes)
+ controller.turn_release_wakeup.notify()
+ finished = controller.wait_for_turn(session_id=saved["session_id"], turn_id=queued["turn_id"], timeout_sec=20)
+ assert finished["response"]["native_loopx"]["status"] == "committed", finished
+ assert finished["loopx_request"]["creation_request"] == saved
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_final_native_quota_refusal_cannot_be_replaced_by_preparation_read(
+ environment, cart_host, consumption, monkeypatch, provider
+):
+ environment[0](provider)
+ _, _, controller, capture, _, runtime = cart_host
+ _, _, saved, before = paused_request(cart_host, consumption)
+ native = controller.adapters[saved["session_id"]].session
+ write = native._write
+ guard_boundary_reached = False
+
+ def pause_before_native_guard(payload, **kwargs):
+ nonlocal guard_boundary_reached
+ if payload.get("method") == "turn/start":
+ guard_boundary_reached = True
+ quota(controller, 0)
+ return write(payload, **kwargs)
+
+ monkeypatch.setattr(native, "_write", pause_before_native_guard)
+ quota(controller, 1)
+ controller.turn_release_wakeup.notify()
+ turn = wait_for_creation_turn(controller, saved["session_id"], saved["operation_id"])
+ finished = controller.wait_for_turn(session_id=saved["session_id"], turn_id=turn["turn_id"], timeout_sec=20)
+ result = finished["response"]["native_loopx"]
+ assert guard_boundary_reached is True
+ assert result["ok"] is False and result["error_code"] == "stale_goal_authority", result
+ assert all(value is None for value in result["effects"].values()), result
+ recovery = result["journal_observation"]["recovery_decision"]
+ assert recovery["action"] == "blocked" and recovery["can_continue"] is False
+ assert recovery["reinvoke_host"] is False and recovery["reason"] == "host_outcome_unknown"
+ assert task_row(runtime, saved["todo_id"])["status"] == "open"
+ calls = [json.loads(line) for line in capture.read_bytes()[len(before):].splitlines()]
+ assert not any(row.get("method") == "turn/start" for row in calls)
+ after = capture.read_bytes()
+ quota(controller, 1)
+ controller.turn_release_wakeup.notify()
+ controller.resume_creation_requests()
+ assert controller.store.turn_for_client(saved["session_id"], "managed." + saved["operation_id"])["turn_id"] == turn["turn_id"]
+ assert capture.read_bytes() == after
diff --git a/tests/control_plane/test_creation_initial_admission_hold.py b/tests/control_plane/test_creation_initial_admission_hold.py
new file mode 100644
index 0000000000..2e5f1ac06b
--- /dev/null
+++ b/tests/control_plane/test_creation_initial_admission_hold.py
@@ -0,0 +1,82 @@
+"""A confirmed start stays recoverable when its real first admission is held."""
+import json
+
+import pytest
+
+from test_creation_task_queue import cart_host as cart_host
+from test_creation_task_queue import environment as environment
+from test_creation_task_queue import preview_requested_task
+from loopx.agent_registry import load_goal_from_registry
+from loopx.configure_goal import configure_goal
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("requested", [True, False])
+def test_confirmed_creation_retries_original_work_after_initial_quota_hold(environment, cart_host, monkeypatch, provider, requested):
+ import loopx.chat_actions as creation_owner
+ environment[0](provider)
+ actions, request, controller, capture, _, _ = cart_host
+ configure = creation_owner.configure_goal
+ def pause_after_real_agent_binding(**kwargs):
+ result = configure(**kwargs)
+ if kwargs.get("goal_id") == "cart-repair" and kwargs.get("registered_agents") == ["codex"]:
+ configure_goal(registry_path=controller.registry_path, goal_id="cart-repair",
+ quota_compute=0, execute=True)
+ return result
+ proposal = preview_requested_task(cart_host, requested=requested)
+ pid = proposal["proposal_id"]
+ path = f"/api/actions/{pid}/apply"
+ with monkeypatch.context() as held:
+ held.setattr(creation_owner, "configure_goal", pause_after_real_agent_binding)
+ status, refused = request(path, {})
+ if not requested:
+ assert status == 200 and refused["proposal"]["status"] == "applied", refused
+ assert "original_task_request" not in refused["proposal"]["receipt"]
+ assert "session_id" not in refused["proposal"]["receipt"]["resource_ids"]
+ assert refused["proposal"]["receipt"]["child_gate"]["kind"] == "goal_quota_gate"
+ assert controller.store.list_sessions(goal_id="cart-repair") == []
+ controller.resume_creation_requests()
+ assert not controller.session_queue_workers
+ return
+ assert status == 400, refused
+ retained = actions.load(pid)
+ assert retained["status"] == "failed" and retained["receipt"] is None
+ assert retained["normalized_parameters"]["start_execution"] is True
+ steps = retained["checkpoint"]["steps"]
+ assert steps["first_turn_gated"]["gate"]["kind"] == "goal_quota_gate"
+ assert "first_session_opened" not in steps and "original_task_requested" not in steps
+ assert controller.store.list_sessions(goal_id="cart-repair") == []
+ before = capture.read_bytes() if capture.exists() else b""
+ assert not any(json.loads(row).get("method") == "turn/start" for row in before.splitlines())
+ goal = load_goal_from_registry(controller.registry_path, "cart-repair")
+ original_grants = goal["coordination"]["checkpointed_boundary_authority"]
+ assert len(original_grants) == 1 and original_grants[0]["decision_id"] == pid
+ original_todos = steps["todos_created"]["todo_ids"]
+ controller.resume_creation_requests()
+ assert not controller.session_queue_workers
+ assert (capture.read_bytes() if capture.exists() else b"") == before
+
+ # Restore through the real quota owner; retry the original confirmation.
+ # The test never manufactures quota permission or another creation intent.
+ configure_goal(registry_path=controller.registry_path, goal_id="cart-repair",
+ quota_compute=1, execute=True)
+ status, resumed = request(path, {})
+ assert status == 202, resumed
+ committed = resumed["proposal"]
+ ids = committed["receipt"]["resource_ids"]
+ assert committed["proposal_id"] == pid and ids["todo_ids"] == original_todos
+ assert ids["creation_operation_id"] == pid
+ assert committed["request_digest"] == retained["request_digest"]
+ assert committed["receipt"]["original_task_request"]["session_id"] == ids["session_id"]
+ assert committed["receipt"]["original_task_request"]["todo_id"] == original_todos[0]
+ first = controller.wait_for_turn(session_id=ids["session_id"], turn_id=ids["turn_id"], timeout_sec=20)
+ assert first["status"] == "completed"
+ assert len(controller.store.list_sessions(goal_id="cart-repair")) == 1
+ assert load_goal_from_registry(controller.registry_path, "cart-repair")["coordination"][
+ "checkpointed_boundary_authority"] == original_grants
+ after = capture.read_bytes()
+ calls = [json.loads(row) for row in after.splitlines()]
+ assert sum(row.get("method") == "thread/start" for row in calls) == 1
+ assert sum(row.get("method") == "turn/start" for row in calls) == 1
+ assert request(path, {})[1]["proposal"]["receipt"] == committed["receipt"]
+ assert capture.read_bytes() == after
diff --git a/tests/control_plane/test_creation_task_queue.py b/tests/control_plane/test_creation_task_queue.py
new file mode 100644
index 0000000000..050a2a643e
--- /dev/null
+++ b/tests/control_plane/test_creation_task_queue.py
@@ -0,0 +1,453 @@
+"""An explicit creation request reaches the original Task through its Session queue.
+
+HTTP/Chat/Core and File/SQLite are real. The existing six-check oracle remains
+independent; the native protocol and artifact editing are scripted transport.
+"""
+from contextlib import contextmanager
+import hashlib
+import http.client
+import json
+import subprocess
+import sys
+import threading
+import time
+
+import pytest
+
+from test_original_task_entry import cart_host as cart_host
+from test_new_goal_storage_defaults import environment as environment
+from loopx.chat_actions import ChatActionService
+from loopx.chat_runtime import ChatRuntimeController
+from loopx.chat_server import ChatHTTPServer, ChatRequestHandler
+from loopx.control_plane.chat_turn_acceptance import CHAT_TURN_ACCEPTANCE_CAPSULE_SCHEMA
+from loopx.control_plane.coordination.local_authority import read_canonical_todos_if_promoted
+from loopx.capabilities.native_chat.project_context import ChatProjectContexts
+
+
+def preview_requested_task(host, *, requested=True):
+ request = host[1]
+ parameters = {"goal_id": "cart-repair", "title": "Cart repair",
+ "objective": "Repair cart.py according to README.md and unchanged checks.py.",
+ "completion_criteria": "The six existing checks pass without changing checks.py.",
+ "agent_id": "codex", "workspace_ref": "current", "heartbeat": {"enabled": False},
+ "initial_todos": ["Repair cart.py to pass the six existing behavioral checks."]}
+ if requested:
+ parameters.update(start_execution=True, permission="workspace_write_on_confirmation")
+ status, preview = request("/api/actions/preview", {"action_kind": "goal.create",
+ "summary": "Repair the cart using its existing checks", "idempotency_key": "cart-queue-request",
+ "context": {"kind": "goal_channel", "goal_id": "workspace"}, "normalized_parameters": parameters})
+ assert status == 201, preview
+ return preview["proposal"]
+
+
+def create_requested_task(host, *, requested=True):
+ actions, request, controller, _, _, _ = host
+ proposal = preview_requested_task(host, requested=requested)
+ status, applied = request(f"/api/actions/{proposal['proposal_id']}/apply", {})
+ assert status == 202, applied
+ ids = applied["proposal"]["receipt"]["resource_ids"]
+ first = controller.wait_for_turn(session_id=ids["session_id"], turn_id=ids["turn_id"], timeout_sec=20)
+ assert first["status"] == "completed", first
+ proposals = actions.list(goal_id="cart-repair", context_kind="goal")
+ checks = [row for row in proposals if row["action_kind"] == "goal.acceptance.check"]
+ assert len(checks) == 1, first
+ return actions.load(proposal["proposal_id"]), ids, checks[0]
+
+
+def wait_for_creation_turn(controller, session_id, operation_id, *, timeout=15):
+ deadline = time.monotonic() + timeout
+ while time.monotonic() < deadline:
+ turn = controller.store.turn_for_client(session_id, "managed." + operation_id)
+ if turn is not None:
+ return turn
+ time.sleep(0.02)
+ raise AssertionError("the original Session queue did not consume the saved creation request")
+
+
+def task_row(runtime, todo_id):
+ authority = read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="cart-repair", include_leases=True)
+ return next(row for row in authority["todos"] if row["todo_id"] == todo_id)
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("mode", ["valid", "failed"])
+def test_creation_queue_consumes_only_the_saved_request_and_returns_original_result(environment, cart_host, monkeypatch, provider, mode):
+ environment[0](provider)
+ actions, request, controller, capture, project, runtime = cart_host
+ creation, ids, check = create_requested_task(cart_host)
+ saved = creation["receipt"]["original_task_request"]
+ sid, todo = ids["session_id"], ids["todo_ids"][0]
+ assert saved["session_id"] == sid and saved["todo_id"] == todo
+ assert saved["operation_id"] == "goal-create-" + creation["proposal_id"]
+ original_session = controller.store.load_session(sid)
+ history = controller.store.messages(sid)
+ checker_hash = hashlib.sha256((project / "checks.py").read_bytes()).hexdigest()
+ before = capture.read_bytes()
+ for _ in range(2):
+ status, observed = request(f"/api/chat/sessions/{sid}/native-turn?original_task=true&agent_id=codex")
+ assert status == 200, observed
+ assert observed["task"]["creation_request"] == saved
+ assert observed["task"]["creation_turn"] is None
+ assert observed["task"]["state"] == "unbound"
+ assert capture.read_bytes() == before and task_row(runtime, todo)["status"] == "open"
+ (project / "execute-native").write_text(mode)
+ status, adopted = request(f"/api/actions/{check['proposal_id']}/apply", {})
+ assert status == 200 and adopted["turn"] is None, adopted
+ assert adopted["proposal"]["receipt"]["checks_passed"] is False
+ turn = wait_for_creation_turn(controller, sid, saved["operation_id"])
+ finished = controller.wait_for_turn(session_id=sid, turn_id=turn["turn_id"], timeout_sec=20)
+ result = finished["response"]["native_loopx"]
+ assert result["status"] == ("committed" if mode == "valid" else "failed"), result
+ assert result["effects"]["quota_spent"] is (mode == "valid")
+ assert task_row(runtime, todo)["status"] == ("done" if mode == "valid" else "open")
+ assert finished["loopx_request"]["creation_request"] == saved
+ calls = [json.loads(line) for line in capture.read_bytes()[len(before):].splitlines()]
+ assert sum(row.get("method") == "turn/start" for row in calls) == 1
+ assert not any(row.get("method") == "thread/start" for row in calls)
+ assert controller.store.load_session(sid)["upstream_thread_id"] == original_session["upstream_thread_id"]
+ assert controller.store.messages(sid)[:len(history)] == history
+ assert hashlib.sha256((project / "checks.py").read_bytes()).hexdigest() == checker_hash
+ after = capture.read_bytes()
+ for thread in list(controller.session_queue_threads.values()):
+ thread.join(timeout=2)
+ with monkeypatch.context() as stopped:
+ def unexpected_queue(**_kwargs):
+ raise AssertionError("a terminal original operation must not wake another queue worker")
+ stopped.setattr(controller, "resume_session_queue", unexpected_queue)
+ controller.resume_creation_requests()
+ assert capture.read_bytes() == after
+ assert controller.store.turn_for_client(sid, "managed." + saved["operation_id"])["turn_id"] == turn["turn_id"]
+ status, replay = request(f"/api/actions/{creation['proposal_id']}/apply", {})
+ assert status in (200, 202) and replay["proposal"]["receipt"]["original_task_request"] == saved
+ assert capture.read_bytes() == after
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_historical_creation_and_check_adoption_do_not_authorize_a_native_task(environment, cart_host, monkeypatch, provider):
+ import builtins
+ environment[0](provider)
+ _, request, controller, capture, project, runtime = cart_host
+ original_import = builtins.__import__
+ def legacy_creation_import(name, *args, **kwargs):
+ if name.endswith("chat.goal_creation_execution"):
+ raise AssertionError("historical creation loaded the optional original-Task execution adapter")
+ return original_import(name, *args, **kwargs)
+ with monkeypatch.context() as legacy:
+ legacy.setattr(builtins, "__import__", legacy_creation_import)
+ creation, ids, check = create_requested_task(cart_host, requested=False)
+ assert "original_task_request" not in creation["receipt"]
+ sid = ids["session_id"]
+ before = capture.read_bytes()
+ (project / "execute-native").write_text("valid")
+ status, adopted = request(f"/api/actions/{check['proposal_id']}/apply", {})
+ assert status == 200 and adopted["turn"] is None, adopted
+ controller.resume_creation_requests()
+ status, observed = request(f"/api/chat/sessions/{sid}/native-turn?original_task=true&agent_id=codex")
+ assert status == 200 and "creation_request" not in observed["task"], observed
+ assert capture.read_bytes() == before
+ assert task_row(runtime, ids["todo_ids"][0])["status"] == "open"
+
+
+@contextmanager
+def restarted_host(host):
+ actions, _, prior, _, project, runtime = host
+ prior.close()
+ controller = ChatRuntimeController(store=prior.store, codex_bin=prior.codex_bin,
+ registry_path=prior.registry_path, project_contexts=ChatProjectContexts([project]))
+ service = ChatActionService(store=actions, registry_path=prior.registry_path,
+ runtime_controller=controller, chat_store=prior.store)
+ server = ChatHTTPServer(("127.0.0.1", 0), ChatRequestHandler)
+ server.verbose = False
+ server.registry_path, server.runtime_root = prior.registry_path, runtime
+ server.runtime_controller, server.action_store, server.action_service = controller, actions, service
+ thread = threading.Thread(target=server.serve_forever, daemon=True)
+ thread.start()
+ controller.resume_creation_requests()
+ def request(path):
+ connection = http.client.HTTPConnection(*server.server_address, timeout=30)
+ try:
+ connection.request("GET", path)
+ response = connection.getresponse()
+ return response.status, json.loads(response.read())
+ finally:
+ connection.close()
+ try:
+ yield controller, request
+ finally:
+ server.shutdown()
+ thread.join(timeout=5)
+ server.server_close()
+ controller.close()
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_restart_resumes_the_same_accepted_unstarted_creation_turn(environment, cart_host, monkeypatch, provider):
+ environment[0](provider)
+ _, request, controller, capture, project, _ = cart_host
+ creation, ids, check = create_requested_task(cart_host)
+ saved = creation["receipt"]["original_task_request"]
+ original_start = controller._start_accepted_turn_worker
+ def lost_dispatch(**kwargs):
+ if kwargs["loopx_execution"]:
+ return False
+ return original_start(**kwargs)
+ monkeypatch.setattr(controller, "_start_accepted_turn_worker", lost_dispatch)
+ (project / "execute-native").write_text("valid")
+ before = capture.read_bytes()
+ assert request(f"/api/actions/{check['proposal_id']}/apply", {})[0] == 200
+ queued = wait_for_creation_turn(controller, ids["session_id"], saved["operation_id"])
+ assert queued["status"] == "queued" and queued.get("started_at") is None
+ assert capture.read_bytes() == before
+ thread_id = controller.store.load_session(ids["session_id"])["upstream_thread_id"]
+ with restarted_host(cart_host) as (restored, read):
+ finished = restored.wait_for_turn(session_id=ids["session_id"], turn_id=queued["turn_id"], timeout_sec=30)
+ assert finished["response"]["native_loopx"]["status"] == "committed", finished
+ assert restored.store.load_session(ids["session_id"])["upstream_thread_id"] == thread_id
+ status, observed = read(f"/api/chat/sessions/{ids['session_id']}/native-turn?original_task=true&agent_id=codex")
+ assert status == 200 and observed["task"]["creation_turn"]["turn_id"] == queued["turn_id"], observed
+ calls = [json.loads(line) for line in capture.read_bytes()[len(before):].splitlines()]
+ assert sum(row.get("method") == "turn/start" for row in calls) == 1
+ assert not any(row.get("method") == "thread/start" for row in calls)
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_failed_creation_dispatch_is_retained_without_another_model(environment, cart_host, monkeypatch, provider):
+ environment[0](provider)
+ _, request, controller, capture, project, runtime = cart_host
+ creation, ids, check = create_requested_task(cart_host)
+ saved = creation["receipt"]["original_task_request"]
+ def fail_dispatch(**_kwargs):
+ raise OSError("synthetic worker start failure")
+ monkeypatch.setattr(controller, "_start_accepted_turn_worker", fail_dispatch)
+ (project / "execute-native").write_text("valid")
+ before = capture.read_bytes()
+ assert request(f"/api/actions/{check['proposal_id']}/apply", {})[0] == 200
+ turn = wait_for_creation_turn(controller, ids["session_id"], saved["operation_id"])
+ deadline = time.monotonic() + 5
+ while time.monotonic() < deadline and controller.store.load_turn(ids["session_id"], turn["turn_id"])["status"] != "failed":
+ time.sleep(0.02)
+ failed = controller.store.load_turn(ids["session_id"], turn["turn_id"])
+ assert failed["status"] == "failed", failed
+ controller.resume_creation_requests()
+ for thread in list(controller.session_queue_threads.values()):
+ thread.join(timeout=2)
+ assert capture.read_bytes() == before
+ assert controller.store.turn_for_client(ids["session_id"], "managed." + saved["operation_id"])["turn_id"] == turn["turn_id"]
+ assert task_row(runtime, saved["todo_id"])["status"] == "open"
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("change", ["foreign-session", "fingerprint", "upstream-thread", "missing-thread-pin"])
+def test_startup_rejects_a_changed_source_before_waking_any_session_queue(environment, cart_host, monkeypatch, provider, change):
+ environment[0](provider)
+ actions, request, controller, capture, project, _ = cart_host
+ creation, ids, _ = create_requested_task(cart_host)
+ before = capture.read_bytes()
+ payload = actions._read()
+ source = payload["proposals"][creation["proposal_id"]]
+ if change == "foreign-session":
+ foreign = controller.store.create_session(goal_id="cart-repair", managed_agent_id="codex",
+ managed_workspace_path=str(project.resolve()), goal_instance_id=ids["goal_instance_id"],
+ channel_id="agent.codex", agent_id="codex", executor_endpoint_id="codex",
+ adapter_kind="codex_app_server", upstream_thread_id="foreign-thread", upstream_mode="chat")
+ source["receipt"]["original_task_request"]["session_id"] = foreign["session_id"]
+ elif change == "fingerprint":
+ source["expected_state_fingerprint"] = "changed-confirmed-source"
+ elif change == "upstream-thread":
+ # Keep every public creation descriptor unchanged while replacing the
+ # actual native history owner. A Session id alone cannot certify the
+ # original conversation, even when Goal/Task/lifetime still match.
+ path = controller.store._session_path(ids["session_id"])
+ session = json.loads(path.read_text())
+ session["upstream_thread_id"] = "replacement-native-thread"
+ path.write_text(json.dumps(session))
+ assert source["receipt"]["original_task_request"] == creation["receipt"]["original_task_request"]
+ else:
+ # Missing WIP provenance is unknown; restart cannot upgrade it from
+ # whichever native binding currently occupies the original Session.
+ source["checkpoint"]["steps"]["first_session_opened"].pop("upstream_thread_id")
+ actions._write(payload) # Only corrupt this disposable attack fixture.
+ wakes = []
+ monkeypatch.setattr(controller, "resume_session_queue", lambda **kwargs: wakes.append(kwargs))
+ controller.resume_creation_requests()
+ assert wakes == []
+ status, observed = request(f"/api/chat/sessions/{ids['session_id']}/native-turn?original_task=true&agent_id=codex")
+ assert status == 200 and observed["task"]["state"] == "unbound", observed
+ assert observed["task"]["reason"] and "creation_request" not in observed["task"]
+ assert capture.read_bytes() == before
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_no_intent_empty_queue_does_not_read_canonical_creation_acceptance(environment, cart_host, monkeypatch, provider):
+ environment[0](provider)
+ _, _, controller, capture, project, _ = cart_host
+ _, ids, _ = create_requested_task(cart_host, requested=False)
+ before = capture.read_bytes()
+ def unexpected_acceptance(**_kwargs):
+ raise AssertionError("an empty ordinary queue must not discover canonical creation acceptance")
+ monkeypatch.setattr("loopx.control_plane.goals.acceptance.inspect_goal_acceptance", unexpected_acceptance)
+ controller.resume_session_queue(session_id=ids["session_id"], work_dir=project,
+ objective="Continue the existing conversation.")
+ for thread in list(controller.session_queue_threads.values()):
+ thread.join(timeout=2)
+ assert ids["session_id"] not in controller.session_queue_workers
+ assert capture.read_bytes() == before
+ cold_read = subprocess.run([sys.executable, "-c", '''
+import builtins
+import sys
+import threading
+from pathlib import Path
+from types import SimpleNamespace
+from loopx.chat_action_store import ChatActionStore
+from loopx.chat_store import ChatSessionStore
+from loopx.capabilities.native_chat.creation_queue import consume_creation_request, resume_creation_requests
+store = ChatSessionStore(Path(sys.argv[1]), startup_maintenance=False)
+actions = ChatActionStore(Path(sys.argv[2]))
+original_import = builtins.__import__
+def feature_off_import(name, *args, **kwargs):
+ if name.endswith(("steward_managed_mode", "control_plane.effect_runtime", "control_plane.goals.acceptance")):
+ raise AssertionError("feature-off queue imported a new creation admission owner: " + name)
+ return original_import(name, *args, **kwargs)
+def unexpected_wake(**kwargs):
+ raise AssertionError("a historical no-intent creation woke a native queue")
+builtins.__import__ = feature_off_import
+controller = SimpleNamespace(store=store, original_task_action_store=actions,
+ closed=threading.Event(), resume_session_queue=unexpected_wake)
+assert consume_creation_request(controller, sys.argv[3]) is False
+resume_creation_requests(controller)
+''', str(controller.store.root.parent), str(controller.original_task_action_store.root), ids["session_id"]],
+ capture_output=True, text=True, timeout=15)
+ assert cold_read.returncode == 0, cold_read.stdout + cold_read.stderr
+ assert capture.read_bytes() == before
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("change", ["missing-publication", "unfinished-acceptance", "already-started", "foreign-request"])
+def test_startup_cannot_redispatch_an_unknown_or_foreign_queued_operation(environment, cart_host, monkeypatch, provider, change):
+ environment[0](provider)
+ _, request, controller, capture, project, _ = cart_host
+ creation, ids, check = create_requested_task(cart_host)
+ saved = creation["receipt"]["original_task_request"]
+ original_start = controller._start_accepted_turn_worker
+ dispatch_prepared = threading.Event()
+ def held_dispatch(**kwargs):
+ if kwargs["loopx_execution"]:
+ dispatch_prepared.set()
+ return False
+ return original_start(**kwargs)
+ monkeypatch.setattr(controller, "_start_accepted_turn_worker", held_dispatch)
+ (project / "execute-native").write_text("valid")
+ before = capture.read_bytes()
+ assert request(f"/api/actions/{check['proposal_id']}/apply", {})[0] == 200
+ queued = wait_for_creation_turn(controller, ids["session_id"], saved["operation_id"])
+ assert dispatch_prepared.wait(timeout=10)
+ # An accepted Turn can be discovered before acceptance settles. Corrupt
+ # only the fully prepared crash input under its existing Session fence,
+ # so a concurrent acceptance write cannot replace the attack fixture.
+ with controller._session_adapter_lock(ids["session_id"]):
+ if change == "already-started":
+ controller.store.update_turn(ids["session_id"], queued["turn_id"],
+ status="starting", started_at="2026-11-15T00:00:00Z")
+ else:
+ path = controller.store._turn_path(ids["session_id"], queued["turn_id"])
+ raw = json.loads(path.read_text())
+ assert "_acceptance" not in raw
+ if change == "missing-publication":
+ raw.pop("upstream_turn_id")
+ elif change == "unfinished-acceptance":
+ raw["_acceptance"] = {"schema_version": CHAT_TURN_ACCEPTANCE_CAPSULE_SCHEMA, "phase": "prepared"}
+ else:
+ raw["loopx_request"]["creation_request"]["session_id"] = "foreign-session"
+ path.write_text(json.dumps(raw)) # Only this disposable corruption input.
+ wakes = []
+ monkeypatch.setattr(controller, "resume_session_queue", lambda **kwargs: wakes.append(kwargs))
+ controller.resume_creation_requests()
+ assert wakes == []
+ assert capture.read_bytes() == before
+ assert controller.store.turn_for_client(ids["session_id"], "managed." + saved["operation_id"])["turn_id"] == queued["turn_id"]
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_pending_creation_rechecks_only_after_a_content_free_signal(environment, cart_host, monkeypatch, provider):
+ from loopx.capabilities.native_chat.steward_managed_mode import ManagedSessionTurn
+ environment[0](provider)
+ _, _, controller, _, _, _ = cart_host
+ original = ManagedSessionTurn._validation_source
+ attempted = threading.Event()
+ count = []
+ def read_current(self, *args, **kwargs):
+ try:
+ return original(self, *args, **kwargs)
+ finally:
+ if "_drain_session_queue" in threading.current_thread().name:
+ count.append(1)
+ attempted.set()
+ monkeypatch.setattr(ManagedSessionTurn, "_validation_source", read_current)
+ creation, ids, _ = create_requested_task(cart_host)
+ assert attempted.wait(timeout=10)
+ time.sleep(0.1) # Let the original Turn's terminal signal coalesce.
+ before = len(count)
+ time.sleep(2.2) # An old two-second polling implementation would reread.
+ assert len(count) == before
+ controller.turn_release_wakeup.notify()
+ deadline = time.monotonic() + 5
+ while time.monotonic() < deadline and len(count) == before:
+ time.sleep(0.02)
+ assert len(count) > before
+ assert controller.store.turn_for_client(ids["session_id"],
+ "managed." + creation["receipt"]["original_task_request"]["operation_id"]) is None
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_creation_terminal_during_queue_read_keeps_the_original_ordinary_followup(environment, cart_host, monkeypatch, provider):
+ from loopx.capabilities.native_chat.steward_managed_mode import ManagedSessionTurn
+ environment[0](provider)
+ _, request, controller, capture, project, runtime = cart_host
+ # Retain real acceptance and FIFO state while controlling only dispatch.
+ # The queue worker must observe the native completion at the second read,
+ # after it has already cached the original queued operation.
+ monkeypatch.setattr(controller, "resume_session_queue", lambda **_kwargs: None)
+ start = controller._start_accepted_turn_worker
+ monkeypatch.setattr(controller, "_start_accepted_turn_worker",
+ lambda **kwargs: False if kwargs["loopx_execution"] else start(**kwargs))
+ creation, ids, check = create_requested_task(cart_host)
+ saved = creation["receipt"]["original_task_request"]
+ sid = ids["session_id"]
+ assert request(f"/api/actions/{check['proposal_id']}/apply", {})[0] == 200
+ native, created = ManagedSessionTurn(controller).submit(sid,
+ agent_id=saved["agent_id"], operation_id=saved["operation_id"],
+ todo_id=saved["todo_id"], _creation_request=saved)
+ assert created and native["status"] == "queued"
+ followup, created = controller.enqueue_turn(session_id=sid,
+ client_turn_id="ordinary-after-original", message="Read the original results.",
+ work_dir=project, objective="Retain the original cart repair.", origin="web")
+ assert created and followup["status"] == "queued"
+ before = capture.read_bytes()
+ original_read = controller.store.turn_for_client
+ terminalized = []
+ def finish_native_before_consumer_read(session_id, client_id):
+ if not terminalized and session_id == sid and client_id == "managed." + saved["operation_id"]:
+ terminalized.append(native["turn_id"])
+ controller._fail_queue_preparation(sid, native["turn_id"],
+ OSError("synthetic native worker exit before consumer read"))
+ return original_read(session_id, client_id)
+ monkeypatch.setattr(controller.store, "turn_for_client", finish_native_before_consumer_read)
+ worker = threading.Thread(target=controller._drain_session_queue,
+ kwargs={"session_id": sid, "work_dir": project, "objective": "Retain the original cart repair."})
+ controller.session_queue_workers.add(sid)
+ controller.session_queue_threads[sid] = worker
+ worker.start()
+ worker.join(timeout=20)
+ assert not worker.is_alive(), "the original queue did not reconcile terminal work"
+ assert terminalized == [native["turn_id"]]
+ assert controller.store.load_turn(sid, native["turn_id"])["status"] == "failed"
+ assert controller.store.load_turn(sid, followup["turn_id"])["status"] == "completed"
+ assert controller.store.load_session(sid)["active_turn_id"] is None
+ assert sid not in controller.session_queue_workers
+ calls = [json.loads(line) for line in capture.read_bytes()[len(before):].splitlines()]
+ starts = [row for row in calls if row.get("method") == "turn/start"]
+ assert len(starts) == 1 # Only the already requested ordinary follow-up.
+ assert starts[0]["params"]["sandboxPolicy"] == {"type": "readOnly", "networkAccess": False}
+ assert "runtimeWorkspaceRoots" not in starts[0]["params"]
+ assert not any(row.get("method") == "thread/start" for row in calls)
+ assert task_row(runtime, saved["todo_id"])["status"] == "open"
diff --git a/tests/control_plane/test_dated_vision_wait_cli.py b/tests/control_plane/test_dated_vision_wait_cli.py
index 54645e7b36..74bd62229d 100644
--- a/tests/control_plane/test_dated_vision_wait_cli.py
+++ b/tests/control_plane/test_dated_vision_wait_cli.py
@@ -194,6 +194,78 @@ def test_multiple_future_waits_keep_the_original_unbound_turn_quiet(
assert not any(run.get("classification") in ("quota_monitor_poll", "quota_slot_spent") for run in runs)
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("scenario", ["before_due", "successor_due", "monitor_due", "unproven"])
+def test_watch_only_monitor_preserves_dated_successor_wait(
+ tmp_path, isolated_effect, monkeypatch, provider, scenario,
+):
+ past = "2026-01-01T00:00:00Z"
+ monitor = "todo_watch_only"
+ monitor_due = past if scenario == "monitor_due" else FUTURE.removeprefix("resume_at:")
+ resume = f"resume_at:{past}" if scenario == "successor_due" else FUTURE
+ if scenario == "unproven":
+ resume = "capacity_available:external_runner"
+ registry, runtime, state = fixture(tmp_path, resume)
+ review = "todo_independent_review"
+ with state.open("a", encoding="utf-8") as output:
+ output.write(
+ "\n- [ ] [P1] Keep the independent review at its own scheduled instant.\n"
+ f" \n"
+ "\n- [ ] [P1] Observe feedback at its own scheduled instant.\n"
+ f" \n"
+ )
+ vision = runtime / f"goals/{GOAL}/runs/vision.json"
+ run = json.loads(vision.read_text())
+ run["agent_vision"]["todo_delta"] = [f"retain:{TODO}", f"retain:{review}"]
+ vision.write_text(json.dumps(run), encoding="utf-8")
+ index = vision.parent / "index.jsonl"
+ index.write_text(json.dumps({**run, "json_path": str(vision)}) + "\n", encoding="utf-8")
+ if provider == "sqlite":
+ monkeypatch.setenv("NODE_OPTIONS", os.environ.get("NODE_OPTIONS", "") + " --experimental-sqlite")
+ initialize_canonical_source_fixture(runtime, GOAL, state_path=state, provider=provider)
+ original = state.read_bytes()
+ args = ("quota", "should-run", "--codex-app", "--goal-id", GOAL, "--agent-id", AGENT,
+ "--available-capability", "network", "--available-capability", "external_evidence_poll",
+ "--turn-instance-id")
+ rc, first = cli(registry, runtime, *args, "watch-first")
+ assert rc == 0, first
+ if scenario == "successor_due":
+ assert first["effective_action"] == "successor_replan_required"
+ assert first["selected_todo"]["todo_id"] == TODO
+ elif scenario == "monitor_due":
+ assert first["effective_action"] == "normal_run"
+ assert first["work_lane_contract"]["lane"] == "continuous_monitor"
+ assert first["selected_todo"]["todo_id"] == monitor
+ elif scenario == "before_due":
+ assert first["heartbeat_receipt"]["status"] == "committed"
+ rc, replay = cli(registry, runtime, *args, "watch-first")
+ assert rc == 0, replay
+ assert replay["heartbeat_receipt"]["status"] == "replayed"
+ rc, later = cli(registry, runtime, *args, "watch-second")
+ assert rc == 0, later
+ for guard in (first, replay, later):
+ assert guard["effective_action"] == "monitor_quiet_skip"
+ receipt = guard["heartbeat_receipt"]
+ assert receipt["stall_observation"] == "not_applicable"
+ assert "settlement_identity" not in receipt
+ assert guard["goal_frontier_projection"]["replan_required"] is False
+ assert guard["interaction_contract"]["agent_channel"]["quiet_noop_allowed"] is True
+ assert guard["vision_wait_state"]["resume_when"] == FUTURE
+ else:
+ assert first["heartbeat_receipt"]["stall_observation"] == "appended"
+ rc, replay = cli(registry, runtime, *args, "watch-first")
+ assert rc == 0, replay
+ assert replay["heartbeat_receipt"]["status"] == "replayed"
+ assert state.read_bytes() == original
+ runs = [json.loads(line) for line in index.read_text().splitlines()]
+ assert not any(run.get("classification") == "quota_slot_spent" for run in runs)
+ assert sum(run.get("classification") == "quota_monitor_poll" for run in runs) == (scenario == "unproven")
+
+
def test_due_vision_wait_restores_explicit_resume(tmp_path, isolated_effect):
registry, runtime, state = fixture(tmp_path, "resume_at:2026-01-01T00:00:00Z")
initialize_canonical_source_fixture(runtime, GOAL, state_path=state, provider="sqlite")
diff --git a/tests/control_plane/test_declared_terminal_settlement_cli.py b/tests/control_plane/test_declared_terminal_settlement_cli.py
index 3f7db27b93..f262d12ffa 100644
--- a/tests/control_plane/test_declared_terminal_settlement_cli.py
+++ b/tests/control_plane/test_declared_terminal_settlement_cli.py
@@ -275,9 +275,13 @@ def test_original_lease_and_failed_validator_remain_required_before_accounting(
@pytest.mark.parametrize("closeout_source", ["turn", "lifecycle"])
def test_no_followup_transition_recovers_bound_review_through_writeback_and_single_debit(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, provider: str, historical_terminals: int,
- closeout_source: str,
+ closeout_source: str, capsys: pytest.CaptureFixture[str],
) -> None:
- project, _, runtime, _, run, lease, marker, _, _ = _fixture(
+ from loopx.control_plane.effect_runtime import EffectRuntimeRejected
+ from loopx.control_plane.work_items import lifecycle_recovery
+ from loopx.entrypoint import main as cli_main
+
+ _, workspace, runtime, registry, run, lease, marker, _, _ = _fixture(
tmp_path, monkeypatch, provider, historical_terminals=historical_terminals,
)
def call(*args: str, expected: int = 0):
@@ -303,6 +307,25 @@ def call(*args: str, expected: int = 0):
turn = "turn-original-lifecycle-review"
guard = ("quota", "should-run", "--runtime-profile", "generic_cli", "--goal-id", cli.GOAL_ID,
"--agent-id", cli.AGENT_ID, "--turn-instance-id", turn, *capabilities)
+ exercise_source_failure = historical_terminals == 0 and closeout_source == "lifecycle"
+
+ def reenter_with_unavailable_source(expected: int = 0):
+ project_semantics = lifecycle_recovery.project_replan_semantics
+
+ def read_semantics(params):
+ if params.get("operation") == "lifecycle_source":
+ raise EffectRuntimeRejected("Original lifecycle review source is unavailable")
+ return project_semantics(params)
+
+ with monkeypatch.context() as patch:
+ patch.chdir(workspace)
+ patch.setattr(lifecycle_recovery, "project_replan_semantics", read_semantics)
+ code = cli_main(["--format", "json", "--registry", str(registry), "--runtime-root", str(runtime),
+ *guard, "--context-delivery", "off"])
+ payload = json.loads(capsys.readouterr().out)
+ assert code == expected, payload
+ return payload
+
admitted = call(*guard)
identity = admitted["heartbeat_receipt"]["settlement_identity"]
assert admitted["autonomous_replan_obligation"]["triggers"][0]["todo_id"] == cli.TODO_ID
@@ -319,6 +342,10 @@ def call(*args: str, expected: int = 0):
assert recovered.get("selected_todo") is None
assert recovered["replan_action_packet"]["settlement_only"] is True
assert recovered["replan_action_packet"]["writeback_contract"]["preferred_input"] == "evidence_linked_vision_path"
+ if exercise_source_failure:
+ rejected = reenter_with_unavailable_source(expected=1)
+ assert "Original lifecycle review source is unavailable" in str(rejected)
+ assert "settlement_owed" not in rejected
replan_binding = ("--agent-id", cli.AGENT_ID, "--replan-obligation-id", identity["replan_obligation_id"],
"--turn-instance-id", turn)
spend = ("quota", "spend-slot", "--goal-id", cli.GOAL_ID, *replan_binding,
@@ -343,10 +370,33 @@ def call(*args: str, expected: int = 0):
assert refreshed["settlement_progress"]["state"] == "spend_required"
pending = call(*guard)
assert pending["heartbeat_receipt"]["settlement_identity"] == identity
- assert pending["settlement_progress"]["state"] == "spend_required"
- assert call(*spend)["appended"] is True
- assert call(*spend)["appended"] is False
+ assert pending["settlement_progress"] == refreshed["settlement_progress"]
+ assert pending["replan_action_packet"]["required_outcome"] == "quota_spend_receipt"
+ assert pending["normal_delivery_allowed"] is False
+ assert pending.get("selected_todo") is None
+ if exercise_source_failure:
+ recovered_debit = reenter_with_unavailable_source()
+ assert recovered_debit["heartbeat_receipt"]["settlement_identity"] == identity
+ assert recovered_debit["settlement_owed"] == pending["settlement_owed"]
+ assert recovered_debit.get("selected_todo") is None
+ pending = recovered_debit
+ owed = pending["settlement_owed"]
+ assert owed["effect_id"] == identity["effect_id"]
+ assert owed["kind"] == "quota_spend"
+ assert cli._spend_run_count(runtime) == 1
+ projected_spend = cli._projected_cli_args(owed["command"], turn_instance_id=turn)
+ if exercise_source_failure:
+ foreign_spend = tuple("turn-without-original-writeback" if token == turn else token
+ for token in projected_spend)
+ assert call(*foreign_spend, expected=1).get("appended") is not True
+ assert cli._spend_run_count(runtime) == 1
+ assert call(*projected_spend)["appended"] is True
+ assert call(*projected_spend)["appended"] is False
assert call(*guard)["effective_action"] == "heartbeat_settled_skip"
+ if exercise_source_failure:
+ replayed = reenter_with_unavailable_source()
+ assert replayed["effective_action"] == "heartbeat_settled_skip"
+ assert "settlement_owed" not in replayed
after = call("todo", "list", "--goal-id", cli.GOAL_ID, "--todo-id", cli.TODO_ID)["todo"]
for field in ("completed_at", "completion_turn_key", "completion_validation_sha256"):
assert before[field] == after[field]
diff --git a/tests/control_plane/test_goal_create_completion.py b/tests/control_plane/test_goal_create_completion.py
index 0b0642a022..d105a7dbe8 100644
--- a/tests/control_plane/test_goal_create_completion.py
+++ b/tests/control_plane/test_goal_create_completion.py
@@ -254,6 +254,12 @@ def assert_retained_then_submit(*args, **kwargs):
status, recovered = request(f"/api/actions/{proposal['proposal_id']}/apply", {})
assert status == 202, recovered
ids = recovered["proposal"]["receipt"]["resource_ids"]
+ original_session = chat.load_session(ids["session_id"])
+ assert original_session["managed_agent_id"] == "codex"
+ assert original_session["channel_id"] == "agent.codex"
+ assert ids["managed_agent_id"] == original_session["managed_agent_id"]
+ assert ids["channel_id"] == original_session["channel_id"]
+ assert ids["goal_instance_id"] == original_session.get("goal_instance_id")
if confirmed is not None:
assert ids["session_id"] == sid and ids["turn_id"] == confirmed["turn_id"]
assert admission_calls == [], "confirmed first work is readback, not a new admission"
diff --git a/tests/control_plane/test_goal_create_session.py b/tests/control_plane/test_goal_create_session.py
new file mode 100644
index 0000000000..a776e38722
--- /dev/null
+++ b/tests/control_plane/test_goal_create_session.py
@@ -0,0 +1,384 @@
+"""Goal creation reaches its original Agent conversation and Task owners.
+
+The Chat/Core/providers are real; the app-server/model is synthetic. Explicit
+owner checks below qualify the integration, not automatic checker production.
+"""
+import hashlib
+import json
+import runpy
+import sys
+from pathlib import Path
+
+import pytest
+
+from test_new_goal_storage_defaults import environment as environment, app as app
+from test_goal_create_completion import preview_goal, inspect
+from loopx.chat_runtime import ChatRuntimeController
+from loopx.chat_store import ChatSessionStore
+from loopx.capabilities.native_chat.project_context import ChatProjectContexts
+from loopx.capabilities.native_chat.steward_managed_mode import ManagedSessionTurn
+from loopx.control_plane.coordination.local_authority import read_canonical_todos_if_promoted
+
+
+@pytest.fixture
+def host(environment, app):
+ _, _, _, project, runtime = environment
+ actions, request, service = app
+ capture, binary = runtime / "requests.jsonl", runtime / "synthetic-codex"
+ source = runpy.run_path(str(Path(__file__).parents[2] / "examples/loopx-chat-runtime-smoke.py"))["FAKE_CODEX"]
+ binary.write_text(source.replace(' method = request.get("method")',
+ f' with open({str(capture)!r}, "a") as stream:\n stream.write(json.dumps(request) + "\\n")\n'
+ ' method = request.get("method")'))
+ binary.chmod(0o700)
+ chat = ChatSessionStore(runtime / "chat")
+ controller = ChatRuntimeController(store=chat, codex_bin=str(binary), registry_path=service.registry_path,
+ project_contexts=ChatProjectContexts([project]))
+ service.chat_store, service.runtime_controller = chat, controller
+ try:
+ yield actions, request, controller, capture, project, runtime
+ finally:
+ controller.close()
+
+
+def create(host):
+ _, request, controller, *_ = host
+ proposal = preview_goal(request)
+ status, result = request(f"/api/actions/{proposal['proposal_id']}/apply", {})
+ assert status == 202, result
+ ids = result["proposal"]["receipt"]["resource_ids"]
+ first = controller.wait_for_turn(session_id=ids["session_id"], turn_id=ids["turn_id"], timeout_sec=10)
+ assert first["status"] == "completed", first
+ return proposal, ids, first
+
+
+def test_creation_display_preserves_user_intent_and_the_internal_execution_request(host):
+ proposal, ids, first = create(host)
+ store = host[2].store
+ assert store.messages(ids["session_id"])[0]["text"] == proposal["normalized_parameters"]["objective"]
+ assert first["message"].startswith("开始推进 Goal")
+
+
+def test_creation_display_recovery_retains_the_original_historical_message(host):
+ from loopx.chat.session_open import goal_creation_display_message
+ create(host)
+ store, project = host[2].store, host[4]
+ session, _ = host[2].open_session(goal_id="report", agent_id="codex", work_dir=project,
+ objective="Historical objective", mode="new", channel_id="goal.report", agent_goal_id="report")
+ turn, _ = store.create_turn(session["session_id"], client_turn_id="historical-first",
+ message="Historical internal execution request", display_message="The original confirmed request")
+ assert goal_creation_display_message(store, session_id=session["session_id"],
+ client_turn_id="historical-first", objective="Later objective") == "The original confirmed request"
+ assert store.load_turn(session["session_id"], turn["turn_id"])["message"] == "Historical internal execution request"
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_first_goal_retains_ordinary_messages_in_the_registered_session(environment, host, provider):
+ configure, *_ = environment
+ configure(provider)
+ _, request, controller, capture, project, runtime = host
+ proposal, ids, first = create(host)
+ chat, sid = controller.store, ids["session_id"]
+ original = chat.load_session(sid)
+ assert original["channel_id"] == "agent.codex" and original["managed_agent_id"] == "codex"
+ assert original["managed_workspace_path"] == str(project)
+ assert first.get("loopx_execution") is not True and "native_loopx" not in first["response"]
+ assert controller.adapters[sid].session.sandbox == "read-only"
+ history = chat.messages(sid)
+ followup, created = controller.submit_turn(session_id=sid, client_turn_id="original-followup",
+ message="Retain the introduction while preparing the appendix.", work_dir=project, objective="Deliver the report.")
+ assert created
+ completed = controller.wait_for_turn(session_id=sid, turn_id=followup["turn_id"], timeout_sec=10)
+ assert completed["status"] == "completed" and "native_loopx" not in completed["response"]
+ assert chat.messages(sid)[:len(history)] == history
+ assert chat.load_session(sid)["upstream_thread_id"] == original["upstream_thread_id"]
+ calls = [json.loads(line) for line in capture.read_text().splitlines()]
+ assert sum(row["method"] == "thread/start" for row in calls) == 1
+ assert sum(row["method"] == "turn/start" for row in calls) == 2
+ before_replay = capture.read_bytes()
+ status, replay = request(f"/api/actions/{proposal['proposal_id']}/apply", {})
+ assert status == 202 and replay["proposal"]["receipt"]["resource_ids"] == ids
+ assert capture.read_bytes() == before_replay and len(chat.list_sessions(goal_id="report")) == 1
+ authority = read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="report", include_leases=True)
+ assert [(todo["todo_id"], todo["status"]) for todo in authority["todos"]] == [(ids["todo_ids"][0], "open")]
+ assert inspect(project, runtime)["goal_acceptance_contract"]["outcome_requirements"]["outcomes"][0]["requirements"][0]["status"] == "unverified"
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("window", ["simple", "competing", "historical", "historical_competing", "historical_missing_metadata"])
+def test_lost_session_open_response_reuses_the_original_registered_route(environment, host, monkeypatch, provider, window):
+ configure, *_ = environment
+ configure(provider)
+ actions, request, controller, capture, *_ = host
+ proposal = preview_goal(request)
+ open_session = controller.open_session
+ opening = {}
+ def lost_response(**kwargs):
+ if window.startswith("historical"):
+ kwargs.pop("managed_agent_id", None)
+ kwargs.pop("session_id", None)
+ session, _ = open_session(**kwargs)
+ opening.update(session=session, arguments=kwargs)
+ raise TimeoutError("Session opening response lost before the checkpoint")
+ monkeypatch.setattr(controller, "open_session", lost_response)
+ path = f"/api/actions/{proposal['proposal_id']}/apply"
+ assert request(path, {})[0] == 424
+ sid = controller.store.list_sessions(goal_id="report")[0]["session_id"]
+ assert "first_session_opened" not in actions.load(proposal["proposal_id"])["checkpoint"]["steps"]
+ monkeypatch.setattr(controller, "open_session", open_session)
+ if window == "historical_missing_metadata":
+ path_to_metadata = controller.store._session_path(sid)
+ path_to_metadata.rename(path_to_metadata.with_suffix(".saved"))
+ if window.endswith("competing"):
+ arguments = {**opening["arguments"], "mode": "new"}
+ arguments.pop("session_id", None)
+ other, _ = open_session(**arguments)
+ assert other["session_id"] != sid
+ status, recovered = request(path, {})
+ if window.startswith("historical"):
+ assert status == 400 and ("ambiguous" if window == "historical_competing" else "unconfirmed") in recovered["error"], recovered
+ assert controller.store.turn_for_client(sid, f"goal-start-{proposal['proposal_id']}") is None
+ return
+ assert status == 202, recovered
+ ids = recovered["proposal"]["receipt"]["resource_ids"]
+ assert ids["session_id"] == sid
+ assert ("managed_agent_id" in ids) is not window.startswith("historical")
+ assert controller.wait_for_turn(session_id=sid, turn_id=ids["turn_id"], timeout_sec=10)["status"] == "completed"
+ assert len(controller.store.list_sessions(goal_id="report")) == (2 if window == "competing" else 1)
+ calls = [json.loads(line) for line in capture.read_text().splitlines()]
+ assert sum(row["method"] == "thread/start" for row in calls) == (2 if window == "competing" else 1)
+ assert sum(row["method"] == "turn/start" for row in calls) == 1
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_new_open_checks_the_persisted_session_before_certifying_its_route(environment, host, monkeypatch, provider):
+ configure, *_ = environment
+ configure(provider)
+ actions, request, controller, capture, *_ = host
+ proposal = preview_goal(request)
+ open_session = controller.open_session
+ original = {}
+ def changed_before_response(**kwargs):
+ session, resumed = open_session(**kwargs)
+ original.update(session)
+ stored = {**session, "channel_id": "goal.report", "managed_agent_id": None}
+ stored.pop("managed_workspace_path", None)
+ controller.store._session_path(session["session_id"]).write_text(json.dumps(stored))
+ return session, resumed
+ monkeypatch.setattr(controller, "open_session", changed_before_response)
+ status, failed = request(f"/api/actions/{proposal['proposal_id']}/apply", {})
+ assert status == 400 and "Session identity changed" in failed["error"], failed
+ assert "first_session_opened" not in actions.load(proposal["proposal_id"])["checkpoint"]["steps"]
+ assert controller.store.turn_for_client(original["session_id"], f"goal-start-{proposal['proposal_id']}") is None
+ calls = [json.loads(line) for line in capture.read_text().splitlines()]
+ assert not [row for row in calls if row["method"] == "turn/start"]
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_explicit_native_budget_retains_the_legacy_goal_channel(environment, host, monkeypatch, provider):
+ configure, *_ = environment
+ configure(provider)
+ actions, request, controller, capture, *_ = host
+ proposal = preview_goal(request, native_token_budget=100)
+ save = actions.save_checkpoint
+ def interrupt(*args, **kwargs):
+ result = save(*args, **kwargs)
+ if kwargs.get("step") == "first_session_opened":
+ raise OSError("Stop the synthetic host before its explicit /goal request")
+ return result
+ monkeypatch.setattr(actions, "save_checkpoint", interrupt)
+ status, result = request(f"/api/actions/{proposal['proposal_id']}/apply", {})
+ assert status == 424, result
+ sid = actions.load(proposal["proposal_id"])["checkpoint"]["steps"]["first_session_opened"]["session_id"]
+ session = controller.store.load_session(sid)
+ assert session["channel_id"] == "goal.report" and session.get("managed_agent_id") is None
+ assert controller.store.turn_for_client(sid, f"goal-start-{proposal['proposal_id']}") is None
+ assert not [json.loads(line) for line in capture.read_text().splitlines() if json.loads(line)["method"] == "turn/start"]
+
+
+def test_concurrent_session_opening_reuses_one_published_identity_and_no_new_adapter_on_replay(host):
+ from concurrent.futures import ThreadPoolExecutor
+
+ _, _, controller, capture, project, _ = host
+ create(host)
+ secondary = ChatRuntimeController(store=controller.store, codex_bin=controller.codex_bin,
+ registry_path=controller.registry_path, project_contexts=ChatProjectContexts([project]))
+ arguments = dict(goal_id="report", agent_id="codex", managed_agent_id="codex",
+ work_dir=project, objective="Deliver the report.", mode="new", session_id="concurrent-original")
+ before = capture.read_bytes()
+ try:
+ with ThreadPoolExecutor(max_workers=2) as workers:
+ opened = list(workers.map(lambda runtime: runtime.open_session(**arguments), [controller, secondary]))
+ assert [row[0]["session_id"] for row in opened] == ["concurrent-original", "concurrent-original"]
+ assert sorted(row[1] for row in opened) == [False, True]
+ assert opened[0][0] == opened[1][0]
+ calls = [json.loads(line) for line in capture.read_bytes()[len(before):].splitlines()]
+ assert sum(row["method"] == "thread/start" for row in calls) == 1
+ before_replay = capture.read_bytes()
+ original = controller.store.load_session("concurrent-original")
+ result, resumed = secondary.open_session(**arguments)
+ assert resumed and result == original and capture.read_bytes() == before_replay
+ finally:
+ secondary.close()
+
+
+@pytest.mark.parametrize("reservation", ["directory", "dangling-link", "metadata-link"])
+def test_unknown_physical_session_reservation_never_starts_another_native_thread(host, reservation):
+ _, _, controller, capture, project, _ = host
+ create(host)
+ directory = controller.store._session_dir("unknown-opening")
+ if reservation == "directory":
+ directory.mkdir()
+ elif reservation == "dangling-link":
+ directory.symlink_to(directory.parent / "missing-target", target_is_directory=True)
+ else:
+ directory.mkdir()
+ controller.store._session_path("unknown-opening").symlink_to(directory.parent / "missing-metadata.json")
+ before = capture.read_bytes()
+ with pytest.raises(ValueError, match="unreadable|unavailable"):
+ controller.open_session(goal_id="report", agent_id="codex", managed_agent_id="codex",
+ work_dir=project, objective="Deliver the report.", mode="new", session_id="unknown-opening")
+ assert capture.read_bytes() == before
+
+
+def test_exact_session_opening_cannot_select_the_resume_latest_branch(host):
+ _, _, controller, capture, project, _ = host
+ create(host)
+ before = capture.read_bytes()
+ with pytest.raises(ValueError, match="mode=new"):
+ controller.open_session(goal_id="report", agent_id="codex", managed_agent_id="codex",
+ work_dir=project, objective="Deliver the report.", mode="resume_latest", session_id="another-opening")
+ assert capture.read_bytes() == before
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("legacy", [False, True], ids=["registered", "historical-legacy"])
+def test_interrupted_first_session_recovers_its_recorded_route(environment, host, monkeypatch, provider, legacy):
+ configure, *_ = environment
+ configure(provider)
+ actions, request, controller, capture, *_ = host
+ proposal = preview_goal(request)
+ save, open_session = actions.save_checkpoint, controller.open_session
+ if legacy:
+ def old_open(**kwargs):
+ # Stop today's producer before opening, then restore a checkpoint
+ # genuinely written by the historical unbound Session producer.
+ raise OSError("Restore the original historical Session checkpoint")
+ monkeypatch.setattr(controller, "open_session", old_open)
+ def interrupt(*args, **kwargs):
+ result = save(*args, **kwargs)
+ if kwargs.get("step") == "first_session_opened":
+ raise OSError("Interrupted before dispatch")
+ return result
+ monkeypatch.setattr(actions, "save_checkpoint", interrupt)
+ path = f"/api/actions/{proposal['proposal_id']}/apply"
+ assert request(path, {})[0] == 424
+ if legacy:
+ project = host[4]
+ session, _ = open_session(goal_id="report", agent_id="codex", work_dir=project,
+ objective="Deliver the report.", mode="new", channel_id="goal.report", agent_goal_id="report")
+ save(proposal["proposal_id"], step="first_session_opened",
+ receipt={"outcome": "first_session_opened", "session_id": session["session_id"]})
+ recorded = actions.load(proposal["proposal_id"])["checkpoint"]["steps"]["first_session_opened"]
+ sid = recorded["session_id"]
+ original = controller.store.load_session(sid)
+ assert original["channel_id"] == ("goal.report" if legacy else "agent.codex")
+ monkeypatch.setattr(actions, "save_checkpoint", save)
+ monkeypatch.setattr(controller, "open_session", open_session)
+ status, resumed = request(path, {})
+ assert status == 202, resumed
+ ids = resumed["proposal"]["receipt"]["resource_ids"]
+ assert ids["session_id"] == sid
+ turn = controller.wait_for_turn(session_id=sid, turn_id=ids["turn_id"], timeout_sec=10)
+ assert turn["status"] == "completed"
+ assert ("managed_agent_id" in ids) is not legacy
+ assert controller.store.load_session(sid)["channel_id"] == original["channel_id"]
+ calls = [json.loads(line) for line in capture.read_text().splitlines()]
+ assert sum(row["method"] == "thread/start" for row in calls) == 1
+ assert sum(row["method"] == "turn/start" for row in calls) == 1
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("field,value", [("channel_id", "goal.report"), ("managed_agent_id", "other-owner"),
+ ("goal_instance_id", "replaced-lifetime"), ("managed_workspace_path", "/unavailable-workspace")])
+def test_recovery_refuses_a_changed_original_session_route(environment, host, monkeypatch, provider, field, value):
+ configure, *_ = environment
+ configure(provider)
+ actions, request, controller, capture, *_ = host
+ proposal = preview_goal(request)
+ save = actions.save_checkpoint
+ def interrupt(*args, **kwargs):
+ result = save(*args, **kwargs)
+ if kwargs.get("step") == "first_session_opened":
+ raise OSError("Interrupted before dispatch")
+ return result
+ monkeypatch.setattr(actions, "save_checkpoint", interrupt)
+ path = f"/api/actions/{proposal['proposal_id']}/apply"
+ assert request(path, {})[0] == 424
+ monkeypatch.setattr(actions, "save_checkpoint", save)
+ sid = actions.load(proposal["proposal_id"])["checkpoint"]["steps"]["first_session_opened"]["session_id"]
+ session = controller.store.load_session(sid)
+ controller.store._session_path(sid).write_text(json.dumps({**session, field: value}))
+ before = capture.read_bytes()
+ status, failed = request(path, {})
+ assert status == 400, failed
+ assert "Session" in failed["error"]
+ assert capture.read_bytes() == before and len(controller.store.list_sessions(goal_id="report")) == 1
+ assert controller.store.turn_for_client(sid, f"goal-start-{proposal['proposal_id']}") is None
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("checks", ["valid", "missing", "failed"])
+def test_created_session_adopts_original_task_only_with_independent_owner_check(environment, host, provider, checks):
+ from loopx.control_plane.goals.acceptance import configure_goal_acceptance
+
+ configure, *_ = environment
+ configure(provider)
+ _, _, controller, capture, project, runtime = host
+ _, ids, _ = create(host)
+ sid, todo = ids["session_id"], ids["todo_ids"][0]
+ original = controller.store.load_session(sid)
+ history = controller.store.messages(sid)
+ artifact, checker = project / "report.txt", project / "check-report.py"
+ expected = "Introduction\nAppendix\n"
+ checker.write_text("from pathlib import Path\nassert Path('report.txt').read_text() == 'Introduction\\nAppendix\\n'\n")
+ if checks != "missing":
+ basis = inspect(project, runtime)
+ configure_goal_acceptance(registry_path=controller.registry_path, runtime_root=str(runtime), goal_id="report",
+ expected_provider_revision=basis["provider_revision"], execute=True,
+ document={"scope": {"kind": "selected_work", "todo_ids": [todo]}, "objective": "Deliver both report sections",
+ "non_goals": [], "criteria": [{"id": "complete-report", "description": "Both approved sections are present",
+ "validation_argv": [sys.executable, "check-report.py"], "validation_timeout_seconds": 5,
+ "validation_files": [{"path": "check-report.py", "sha256": hashlib.sha256(checker.read_bytes()).hexdigest()}]}],
+ "bindings": [{"todo_id": todo, "criterion_ids": ["complete-report"]}]})
+ candidate = {"result_kind": "validated_completion", "classification": "implementation",
+ "summary": "The report includes both sections.", "next_action": "Read back the original report.",
+ "delivery_batch_scale": "implementation", "delivery_outcome": "outcome_progress",
+ "vision_unchanged_reason": "The original approved report remains the current task.",
+ "work_evidence": ["report.txt"], "validation_evidence": ["The independent checker reads both original sections"]}
+ binary = Path(controller.codex_bin)
+ source = binary.read_text().replace('"message": "Runtime response."', '"message": ' + repr(json.dumps(candidate)))
+ artifact_content = expected if checks != "failed" else "Introduction\n"
+ source = source.replace(' turn_count += 1',
+ f' open({str(artifact)!r}, "w").write({artifact_content!r})\n turn_count += 1')
+ binary.write_text(source)
+ controller.adapters.pop(sid).close_session()
+ before = capture.read_bytes()
+ native = ManagedSessionTurn(controller)
+ accepted, created = native.submit(sid, agent_id="codex", operation_id="original-task", todo_id=todo)
+ assert created
+ completed = controller.wait_for_turn(session_id=sid, turn_id=accepted["turn_id"], timeout_sec=15)
+ result = completed["response"]["native_loopx"]
+ assert result["status"] == {"valid": "committed", "missing": "stopped", "failed": "failed"}[checks], result
+ assert result["effects"]["quota_spent"] is (checks == "valid")
+ assert result["effects"]["state_written"] is (checks == "valid")
+ authority = read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="report", include_leases=True)
+ assert next(row for row in authority["todos"] if row["todo_id"] == todo)["status"] == ("done" if checks == "valid" else "open")
+ calls = [json.loads(line) for line in capture.read_bytes()[len(before):].splitlines()]
+ assert sum(row["method"] == "turn/start" for row in calls) == (0 if checks == "missing" else 1)
+ assert not [row for row in calls if row["method"] == "thread/start"]
+ assert controller.store.messages(sid)[:len(history)] == history
+ assert controller.store.load_session(sid)["upstream_thread_id"] == original["upstream_thread_id"]
+ assert len(controller.store.list_sessions(goal_id="report")) == 1
+ after = capture.read_bytes()
+ replay, created = native.submit(sid, agent_id="codex", operation_id="original-task", todo_id=todo)
+ assert not created and replay["turn_id"] == accepted["turn_id"] and capture.read_bytes() == after
diff --git a/tests/control_plane/test_goal_session_check_preparation.py b/tests/control_plane/test_goal_session_check_preparation.py
new file mode 100644
index 0000000000..02a6bebcc8
--- /dev/null
+++ b/tests/control_plane/test_goal_session_check_preparation.py
@@ -0,0 +1,915 @@
+"""Original managed Chat preparation over real File/SQLite acceptance owners.
+
+The native model transport is synthetic. Preparation pins preexisting sources
+and persists a proposal without configuring acceptance or running a checker.
+"""
+
+from copy import deepcopy
+from contextlib import contextmanager, nullcontext
+import hashlib
+import json
+import queue
+import sys
+from types import SimpleNamespace
+
+import pytest
+
+from loopx.chat.coordination import prepare_turn_context, check_preparation_tool
+from loopx.chat_agent import CodexChatAgentSession
+from loopx.chat_runtime import ChatRuntimeController, CodexAppServerAdapter
+from loopx.chat_store import ChatSessionStore
+from loopx.chat_action_store import ChatActionStore
+from loopx.control_plane.collaboration import conversation_scope
+from loopx.control_plane.goals.acceptance import refine_goal_outcome
+from test_goal_acceptance_cli import acceptance_goal as acceptance_goal
+
+
+@pytest.fixture
+def original_check(acceptance_goal, monkeypatch):
+ project, _, cli, _ = acceptance_goal
+ root, registry = project.parent / "runtime", project.parent / "registry.json"
+ refined = refine_goal_outcome(
+ registry_path=registry,
+ runtime_root=str(root),
+ goal_id="goal-acceptance",
+ agent_id="agent-a",
+ operation_id="original-owner-outcome",
+ expected_provider_revision=cli("inspect")[1]["provider_revision"],
+ execute=True,
+ refinement={
+ "outcome_id": "goal-creation",
+ "owner_agent_id": "agent-a",
+ "objective": "Produce a usable export",
+ "todo_ids": ["todo_export"],
+ "requirements": [
+ {
+ "id": "completion",
+ "description": "The exported artifact has the expected content",
+ }
+ ],
+ "source": {
+ "ref": "goal-create:fixture:completion",
+ "summary": "Owner-approved completion criteria",
+ },
+ },
+ )
+ assert refined["status"] == "applied", refined
+ checker = project / "check.py"
+ checker.write_text(
+ "from pathlib import Path\nassert Path('artifact.txt').read_text() == 'accepted'\n"
+ )
+ store = ChatSessionStore(root)
+ runtime = ChatRuntimeController(
+ store=store, codex_bin="unused", registry_path=registry
+ )
+ session = store.create_session(
+ goal_id="goal-acceptance",
+ managed_agent_id="agent-a",
+ managed_workspace_path=str(project.resolve()),
+ channel_id="agent.agent-a",
+ agent_id="codex",
+ executor_endpoint_id="codex",
+ adapter_kind="codex_app_server",
+ upstream_thread_id="original-thread",
+ upstream_mode="chat",
+ )
+ turn, _ = store.create_turn(
+ session["session_id"],
+ client_turn_id="prepare-original-check",
+ message="Prepare the independent delivery check for review.",
+ )
+ store.update_turn(session["session_id"], turn["turn_id"], status="running")
+ store.update_session(session["session_id"], active_turn_id=turn["turn_id"])
+ native = CodexChatAgentSession(
+ process=SimpleNamespace(poll=lambda: None),
+ messages=queue.Queue(),
+ thread_id="original-thread",
+ work_dir=project,
+ )
+ monkeypatch.setattr(native, "close", lambda: None)
+ adapter = CodexAppServerAdapter(native)
+ runtime.adapters[session["session_id"]] = adapter
+ session = store.load_session(session["session_id"])
+ prepared_adapter, context = prepare_turn_context(
+ runtime,
+ adapter,
+ session,
+ turn["turn_id"],
+ lambda *_: None,
+ scope=conversation_scope(session, origin="web"),
+ )
+ assert prepared_adapter is adapter and context["scope"] == "owner_goal"
+ document = {
+ "outcome_id": "goal-creation",
+ "criteria": [
+ {
+ "id": "completion",
+ "validation_argv": ["python3", "check.py"],
+ "validation_timeout_seconds": 5,
+ "oracle": {
+ "kind": "owner_pinned_oracle",
+ "source_id": "export-checker",
+ "input_refs": [],
+ "output_refs": ["artifact.txt"],
+ },
+ }
+ ],
+ }
+
+ def call(**updates):
+ return native.read_tool_handler(
+ "loopx_collaboration",
+ {
+ "action": "prepare_check",
+ "operation_id": "original-check",
+ "document": document,
+ **updates,
+ },
+ )
+
+ try:
+ yield SimpleNamespace(
+ project=project,
+ checker=checker,
+ root=root,
+ registry=registry,
+ store=store,
+ runtime=runtime,
+ session=session,
+ turn=turn,
+ native=native,
+ adapter=adapter,
+ document=document,
+ call=call,
+ cli=cli,
+ )
+ finally:
+ runtime.close()
+
+
+def test_original_read_only_turn_prepares_one_host_pinned_proposal(original_check):
+ fixture = original_check
+ before = fixture.cli("inspect")[1]
+ session_before = fixture.store.load_session(fixture.session["session_id"])
+ source_before = fixture.checker.read_bytes()
+ result = fixture.call()
+ assert result["ok"] and result["prepared"] and result["executed"] is False, (
+ result.get("next_action", result)
+ )
+ proposal = result["proposal"]
+ parameters = proposal["normalized_parameters"]
+ assert parameters["goal_id"] == fixture.session["goal_id"]
+ assert parameters["prepared_by"] == "agent-a"
+ criterion = parameters["document"]["criteria"][0]
+ assert criterion["validation_argv"] == ["python3", "check.py"]
+ assert criterion["validation_files"] == [
+ {"path": "check.py", "sha256": hashlib.sha256(source_before).hexdigest()}
+ ]
+ assert criterion["oracle"]["output_refs"] == ["artifact.txt"]
+ assert not (fixture.project / "artifact.txt").exists(), (
+ "Preparation must allow unfinished delivery"
+ )
+ assert fixture.call()["proposal"] == proposal
+ assert fixture.cli("inspect")[1] == before
+ assert fixture.store.load_session(fixture.session["session_id"]) == session_before
+ assert fixture.checker.read_bytes() == source_before
+ assert len(ChatActionStore(fixture.root / "chat/actions").list()) == 1
+
+
+@pytest.mark.parametrize(
+ "updates",
+ [
+ {"goal_id": "another-goal"},
+ {"agent_id": "other-agent"},
+ {"prepared_by": "other-agent"},
+ {"workspace": "another-workspace"},
+ {"runtime_root": "another-runtime"},
+ {"source_id": "remote"},
+ {"action": "start"},
+ {"action": "outcomes"},
+ {"operation_id": ""},
+ ],
+)
+def test_caller_cannot_replace_host_identity_or_action(original_check, updates):
+ assert original_check.call(**updates)["error"] == "collaboration_request_rejected"
+ assert ChatActionStore(original_check.root / "chat/actions").list() == []
+
+
+@pytest.mark.parametrize(
+ "change",
+ [
+ "agent",
+ "workspace",
+ "thread",
+ "channel",
+ "closed",
+ "other-turn",
+ "terminal",
+ "external",
+ "native",
+ "legacy",
+ "review",
+ "lifetime",
+ "registration",
+ "registry-workspace",
+ "goal-stopped",
+ ],
+)
+def test_preparation_rechecks_original_session_turn_and_registration(
+ original_check, change
+):
+ fixture = original_check
+ sid, tid = fixture.session["session_id"], fixture.turn["turn_id"]
+ fields = {
+ "agent": {"managed_agent_id": "other-agent"},
+ "workspace": {"managed_workspace_path": "other"},
+ "thread": {"upstream_thread_id": "replacement"},
+ "channel": {"channel_id": "goal.other"},
+ "closed": {"status": "closed"},
+ "other-turn": {"active_turn_id": "another-turn"},
+ "legacy": {"loopx_mode": {"enabled": True}},
+ }
+ if change in fields:
+ if change in {"agent", "workspace", "channel"}:
+ # These fields are immutable through the public API. Corrupt only
+ # this disposable input to qualify defensive source readback.
+ from loopx.chat_store import _atomic_write_json
+
+ path = fixture.store._session_path(sid)
+ _atomic_write_json(
+ path, {**fixture.store.load_session(sid), **fields[change]}
+ )
+ else:
+ fixture.store.update_session(sid, **fields[change])
+ elif change in {"terminal", "external", "native"}:
+ if change == "external":
+ from loopx.chat_store import _atomic_write_json
+
+ _atomic_write_json(
+ fixture.store._turn_path(sid, tid),
+ {**fixture.store.load_turn(sid, tid), "origin": "lark"},
+ )
+ else:
+ fixture.store.update_turn(
+ sid,
+ tid,
+ **(
+ {"status": "completed"}
+ if change == "terminal"
+ else {"loopx_execution": True}
+ ),
+ )
+ elif change == "review":
+ fixture.native.read_only_review = True
+ else:
+ registry = json.loads(fixture.registry.read_text())
+ goal = registry["goals"][0]
+ if change == "lifetime":
+ goal["goal_instance_id"] = "another-lifetime"
+ elif change == "registration":
+ goal["coordination"]["registered_agents"] = []
+ elif change == "registry-workspace":
+ goal["repo"] = str(fixture.project.parent)
+ else:
+ goal["status"] = "paused"
+ fixture.registry.write_text(json.dumps(registry))
+ result = fixture.call()
+ assert result["error"] == "collaboration_request_rejected", result
+ assert ChatActionStore(fixture.root / "chat/actions").list() == []
+
+
+@pytest.mark.parametrize(
+ "change",
+ [
+ "claimed-pin",
+ "inline-python",
+ "python-options",
+ "extra-argv",
+ "other-interpreter",
+ "absolute",
+ "parent",
+ "symlink",
+ "symlink-parent",
+ "missing",
+ "directory",
+ "source-output",
+ "missing-outputs",
+ "output-symlink",
+ ],
+)
+def test_preparation_rejects_unusable_or_unpinned_checker_sources(
+ original_check, change
+):
+ fixture = original_check
+ document = deepcopy(fixture.document)
+ row = document["criteria"][0]
+ if change == "claimed-pin":
+ row["validation_files"] = [{"path": "check.py", "sha256": "0" * 64}]
+ elif change == "inline-python":
+ row["validation_argv"] = ["python3", "-c", "pass"]
+ elif change == "python-options":
+ row["validation_argv"] = ["python3", "-I", "check.py"]
+ elif change == "extra-argv":
+ row["validation_argv"].append("--ignore-failure")
+ elif change == "other-interpreter":
+ row["validation_argv"] = ["arbitrary-command", "check.py"]
+ elif change == "absolute":
+ row["validation_argv"][1] = str(fixture.checker)
+ elif change == "parent":
+ row["validation_argv"][1] = "../project/check.py"
+ elif change in {"symlink", "missing", "directory"}:
+ fixture.checker.unlink()
+ if change == "symlink":
+ fixture.checker.symlink_to(fixture.project.parent / "acceptance.json")
+ elif change == "directory":
+ fixture.checker.mkdir()
+ elif change == "symlink-parent":
+ (fixture.project / "linked").symlink_to(
+ fixture.project, target_is_directory=True
+ )
+ row["validation_argv"][1] = "linked/check.py"
+ elif change == "source-output":
+ row["oracle"]["output_refs"] = ["check.py"]
+ elif change == "missing-outputs":
+ row["oracle"]["output_refs"] = []
+ else:
+ (fixture.project / "artifact.txt").symlink_to(fixture.checker)
+ result = fixture.call(document=document)
+ assert result["error"] == "collaboration_request_rejected", result
+ assert ChatActionStore(fixture.root / "chat/actions").list() == []
+
+
+def test_scope_change_during_source_read_is_refused_before_proposal(
+ original_check, monkeypatch
+):
+ import loopx.chat.coordination as coordination
+
+ observe = coordination._workspace_file
+
+ def replace_scope(*args, **kwargs):
+ result = observe(*args, **kwargs)
+ original_check.store.update_session(
+ original_check.session["session_id"], active_turn_id="later-turn"
+ )
+ return result
+
+ monkeypatch.setattr(coordination, "_workspace_file", replace_scope)
+ assert original_check.call()["error"] == "collaboration_request_rejected"
+ assert ChatActionStore(original_check.root / "chat/actions").list() == []
+
+
+def test_changed_source_retry_preserves_saved_intent_and_original_pin(original_check):
+ fixture = original_check
+ original = fixture.call()["proposal"]
+ saved_pin = original["normalized_parameters"]["document"]["criteria"][0][
+ "validation_files"
+ ]
+ source = fixture.checker.read_bytes()
+ fixture.checker.write_bytes(source + b"# A different source version\n")
+ refused = fixture.call()
+ assert refused["error"] == "collaboration_request_rejected"
+ proposals = ChatActionStore(fixture.root / "chat/actions").list()
+ assert proposals == [original]
+ assert (
+ proposals[0]["normalized_parameters"]["document"]["criteria"][0][
+ "validation_files"
+ ]
+ == saved_pin
+ )
+ fixture.checker.write_bytes(source)
+ assert fixture.call()["proposal"] == original
+
+
+@pytest.mark.parametrize("interpreter", [None, "/usr/bin/another-python"])
+def test_unavailable_host_interpreter_refuses_preparation(
+ original_check, monkeypatch, interpreter
+):
+ monkeypatch.setattr(
+ "loopx.chat.coordination.shutil.which", lambda _name: interpreter
+ )
+ result = original_check.call()
+ assert result["error"] == "collaboration_request_rejected"
+ assert "host interpreter" in result["next_action"]
+ assert ChatActionStore(original_check.root / "chat/actions").list() == []
+
+
+@pytest.mark.parametrize("direct", [False, True])
+def test_source_changed_during_read_is_refused_before_proposal(
+ original_check, monkeypatch, direct
+):
+ import loopx.chat.coordination as coordination
+
+ observe = coordination._workspace_file
+ document = deepcopy(original_check.document)
+ selected_source = original_check.checker
+ if direct:
+ selected_source = original_check.project / "checks/verify"
+ selected_source.parent.mkdir()
+ selected_source.write_bytes(original_check.checker.read_bytes())
+ selected_source.chmod(0o755)
+ document["criteria"][0]["validation_argv"] = ["checks/verify"]
+ changed = False
+
+ def change_after_read(*args, **kwargs):
+ nonlocal changed
+ result = observe(*args, **kwargs)
+ if kwargs.get("required") and not changed:
+ changed = True
+ selected_source.write_text("raise SystemExit(0)\n")
+ return result
+
+ monkeypatch.setattr(coordination, "_workspace_file", change_after_read)
+ result = original_check.call(document=document)
+ assert result["error"] == "collaboration_request_rejected"
+ assert "source changed during preparation" in result["next_action"]
+ assert ChatActionStore(original_check.root / "chat/actions").list() == []
+
+
+def test_first_of_two_checker_sources_is_reobserved_before_proposal(
+ original_check, monkeypatch
+):
+ import loopx.chat.coordination as coordination
+
+ fixture = original_check
+ refine_goal_outcome(
+ registry_path=fixture.registry,
+ runtime_root=str(fixture.root),
+ goal_id=fixture.session["goal_id"],
+ agent_id="agent-a",
+ operation_id="additional-owner-requirement",
+ expected_provider_revision=fixture.cli("inspect")[1]["provider_revision"],
+ execute=True,
+ refinement={
+ "outcome_id": "goal-creation",
+ "requirements": [
+ {
+ "id": "additional",
+ "description": "The additional independent check succeeds",
+ }
+ ],
+ "source": {
+ "ref": "message:additional-owner-direction",
+ "summary": "The owner added another requirement",
+ },
+ },
+ )
+ (fixture.project / "additional.py").write_text(
+ "from pathlib import Path\nassert Path('artifact.txt').read_text() == 'accepted'\n"
+ )
+ document = deepcopy(fixture.document)
+ additional = deepcopy(document["criteria"][0])
+ additional.update(id="additional", validation_argv=["python3", "additional.py"])
+ additional["oracle"]["source_id"] = "additional-checker"
+ document["criteria"].append(additional)
+ observe = coordination._workspace_file
+
+ def replace_first_after_second(workspace, ref, **kwargs):
+ digest = observe(workspace, ref, **kwargs)
+ if ref == "additional.py" and kwargs["required"]:
+ fixture.checker.write_text("raise SystemExit(0)\n")
+ return digest
+
+ monkeypatch.setattr(coordination, "_workspace_file", replace_first_after_second)
+ result = fixture.call(document=document)
+ assert result["error"] == "collaboration_request_rejected"
+ assert "source changed during preparation" in result["next_action"]
+ assert ChatActionStore(fixture.root / "chat/actions").list() == []
+
+
+def test_unavailable_safe_file_observation_is_explicitly_refused(
+ original_check, monkeypatch
+):
+ monkeypatch.setattr("loopx.chat.coordination.os.supports_dir_fd", set())
+ result = original_check.call()
+ assert result["error"] == "collaboration_request_rejected"
+ assert "cannot safely inspect workspace checker sources" in result["next_action"]
+ assert ChatActionStore(original_check.root / "chat/actions").list() == []
+
+
+def test_direct_existing_checker_and_matching_claimed_pin_are_supported(original_check):
+ fixture = original_check
+ checks = fixture.project / "checks"
+ checks.mkdir()
+ executable = checks / "verify"
+ executable.write_text(
+ "#!/usr/bin/env python3\nfrom pathlib import Path\nassert Path('artifact.txt').read_text() == 'accepted'\n"
+ )
+ executable.chmod(0o755)
+ document = deepcopy(fixture.document)
+ row = document["criteria"][0]
+ row["validation_argv"] = ["checks/verify"]
+ row["validation_files"] = [
+ {
+ "path": "checks/verify",
+ "sha256": hashlib.sha256(executable.read_bytes()).hexdigest(),
+ }
+ ]
+ result = fixture.call(document=document)
+ assert result["prepared"] and result["executed"] is False, result
+ assert result["proposal"]["normalized_parameters"]["document"] == document
+
+
+@pytest.mark.parametrize("token", ["python", sys.executable])
+def test_supported_python_aliases_resolve_to_public_safe_host_token(
+ original_check, token
+):
+ document = deepcopy(original_check.document)
+ document["criteria"][0]["validation_argv"][0] = token
+ result = original_check.call(document=document)
+ assert result["prepared"] and result["executed"] is False, result
+ row = result["proposal"]["normalized_parameters"]["document"]["criteria"][0]
+ assert row["validation_argv"] == ["python3", "check.py"]
+
+
+def test_outer_dispatcher_preserves_read_owner_and_rejects_actor_override_in_document(
+ original_check,
+):
+ fixture = original_check
+ handler = fixture.native.read_tool_handler
+ arguments = {"view": "goals", "source_id": "remote"}
+ assert handler("loopx_context_read", arguments) == handler.read(
+ "loopx_context_read", arguments
+ )
+ document = {**fixture.document, "goal_id": "another-goal"}
+ assert fixture.call(document=document)["error"] == "collaboration_request_rejected"
+ assert ChatActionStore(fixture.root / "chat/actions").list() == []
+
+
+@pytest.mark.parametrize(
+ "managed,resume,legacy,execution,review,manager,expected",
+ [
+ (
+ True,
+ None,
+ False,
+ False,
+ False,
+ False,
+ ["loopx_context_read", "loopx_collaboration"],
+ ),
+ (False, None, False, False, False, False, ["loopx_context_read"]),
+ (True, "existing-thread", False, False, False, False, ["loopx_context_read"]),
+ (
+ False,
+ None,
+ True,
+ False,
+ False,
+ False,
+ ["loopx_context_read", "loopx_collaboration"],
+ ),
+ (True, None, False, True, False, False, []),
+ (True, None, False, False, True, False, []),
+ (True, None, False, False, False, True, ["loopx_manager_read"]),
+ ],
+)
+def test_prepare_projection_is_only_for_new_default_managed_threads(
+ tmp_path, monkeypatch, managed, resume, legacy, execution, review, manager, expected
+):
+ from loopx.capabilities.native_chat.codex_adapter import start_runtime_adapter
+
+ captured = []
+ monkeypatch.setattr(
+ CodexAppServerAdapter, "start", lambda **kwargs: captured.append(kwargs)
+ )
+ controller = SimpleNamespace(
+ codex_bin="unused",
+ coordination_runtime_root=tmp_path,
+ codex_home=None,
+ startup_timeout_sec=2,
+ idle_timeout_sec=2,
+ hard_timeout_sec=2,
+ )
+ start_runtime_adapter(
+ controller,
+ work_dir=tmp_path,
+ goal_id="loopx-manager" if manager else "goal-fixture",
+ native_context={"objective": "Observe", "runtime_profile": "restricted"},
+ manager_profile=None,
+ model_config={},
+ history=None,
+ resume_thread_id=resume,
+ execution_mode=execution,
+ project_context=None,
+ project_coordination=not manager,
+ loopx_tools=legacy,
+ check_preparation=managed,
+ codex_home_override=None,
+ read_only_review=review,
+ metadata_review_context=None,
+ answer_review_context=None,
+ )
+ tools = captured[0]["dynamic_tools"] or []
+ assert [tool["name"] for tool in tools] == expected
+ if (
+ managed
+ and not resume
+ and not legacy
+ and not execution
+ and not review
+ and not manager
+ ):
+ assert tools[-1]["inputSchema"]["properties"]["action"]["enum"] == [
+ "prepare_check"
+ ]
+ assert set(tools[-1]["inputSchema"]["properties"]) == {
+ "action",
+ "operation_id",
+ "document",
+ }
+ if legacy:
+ assert "start" in tools[-1]["inputSchema"]["properties"]["action"]["enum"]
+
+
+def test_prepare_projection_does_not_mutate_existing_collaboration_tool():
+ from loopx.chat_loopx_mode import TOOL
+
+ before = deepcopy(TOOL)
+ check_preparation_tool()
+ assert TOOL == before
+
+
+def _capture_native_dispatch(fixture, monkeypatch):
+ """Run the real send/request event loop with a synthetic guarded wire."""
+ captured = []
+
+ def wire(payload, *, write_guard=None, deadline=None):
+ with write_guard() if write_guard is not None else nullcontext():
+ captured.append(deepcopy(payload))
+ fixture.native.messages.put(
+ {"id": payload["id"], "result": {"turn": {"id": "transport-turn"}}}
+ )
+ fixture.native.messages.put(
+ {"method": "item/agentMessage/delta", "params": {"delta": "Ready."}}
+ )
+ fixture.native.messages.put(
+ {
+ "method": "turn/completed",
+ "params": {"turn": {"status": "completed"}},
+ }
+ )
+
+ monkeypatch.setattr(fixture.native, "_write", wire)
+ return captured
+
+
+def _native_task_policy(fixture):
+ return {
+ "type": "workspaceWrite",
+ "writableRoots": [str(fixture.project.resolve())],
+ "networkAccess": False,
+ "excludeTmpdirEnvVar": True,
+ "excludeSlashTmp": True,
+ }
+
+
+@pytest.mark.parametrize("writable", [False, True])
+def test_fenced_native_task_dispatch_then_ordinary_read_reset_preserves_session_defaults(
+ original_check, monkeypatch, writable
+):
+ fixture = original_check
+ captured = _capture_native_dispatch(fixture, monkeypatch)
+ guarded = []
+
+ @contextmanager
+ def admitted_host_write():
+ # This synthetic guard checks original transport identity only. The
+ # actual Core Task/lease/authority path is qualified by the HTTP suite.
+ fixture.native.read_tool_handler._current()
+ guarded.append("entered")
+ yield
+
+ fixture.native._launch_guard = admitted_host_write
+ before = (fixture.native.execution_mode, fixture.native.sandbox)
+ policy = (
+ _native_task_policy(fixture)
+ if writable
+ else {"type": "readOnly", "networkAccess": False}
+ )
+ fixture.adapter.start_native_task_turn(
+ "Perform the confirmed Task.", lambda *_: None, policy
+ )
+ native = captured[-1]["params"]
+ assert native["sandboxPolicy"] == policy
+ assert "permissions" not in native
+ if writable:
+ assert native["runtimeWorkspaceRoots"] == [str(fixture.project.resolve())]
+ else:
+ assert "runtimeWorkspaceRoots" not in native
+ assert (
+ "You are the execution agent for a confirmed LoopX Task"
+ in native["input"][0]["text"]
+ )
+ assert "Do not edit files, mutate LoopX state" not in native["input"][0]["text"]
+ assert guarded == ["entered"]
+ fixture.adapter.start_turn("Prepare the check for review.", lambda *_: None)
+ ordinary = captured[-1]["params"]
+ assert ordinary["sandboxPolicy"] == {"type": "readOnly", "networkAccess": False}
+ assert "runtimeWorkspaceRoots" not in ordinary
+ assert "Do not edit files, mutate LoopX state" in ordinary["input"][0]["text"]
+ fixture.adapter.start_turn_with_attachments(
+ "Read the original attachment.", lambda *_: None, []
+ )
+ assert captured[-1]["params"]["sandboxPolicy"] == {
+ "type": "readOnly",
+ "networkAccess": False,
+ }
+ assert (fixture.native.execution_mode, fixture.native.sandbox) == before
+ fixture.native.send("Read from the original direct source.")
+ assert captured[-1]["params"]["sandboxPolicy"] == {
+ "type": "readOnly",
+ "networkAccess": False,
+ }
+ assert all(frame["params"]["threadId"] == "original-thread" for frame in captured)
+ assert fixture.native._launch_guard is admitted_host_write
+
+
+@pytest.mark.parametrize(
+ "invalid", ["no-guard", "wrong-workspace", "extra-root", "network", "named-profile", "review"]
+)
+def test_native_task_transport_refuses_unbound_policy_or_missing_host_fence(
+ original_check, monkeypatch, invalid
+):
+ fixture = original_check
+ captured = _capture_native_dispatch(fixture, monkeypatch)
+ policy = _native_task_policy(fixture)
+
+ @contextmanager
+ def admitted_host_write():
+ yield
+
+ fixture.native._launch_guard = admitted_host_write
+ if invalid == "no-guard":
+ fixture.native._launch_guard = None
+ elif invalid == "wrong-workspace":
+ policy["writableRoots"] = [str(fixture.project.parent.resolve())]
+ elif invalid == "extra-root":
+ policy["writableRoots"].append(str(fixture.project.parent.resolve()))
+ elif invalid == "network":
+ policy["networkAccess"] = True
+ elif invalid == "named-profile":
+ fixture.native.permissions_profile = "existing-owner-profile"
+ else:
+ fixture.native.read_only_review = True
+ with pytest.raises(ValueError, match="current host launch guard"):
+ fixture.adapter.start_native_task_turn(
+ "Perform the Task.", lambda *_: None, policy
+ )
+ assert captured == []
+ assert (
+ fixture.native.execution_mode is False and fixture.native.sandbox == "read-only"
+ )
+ assert fixture.native._native_task_policy_used is False
+
+
+def test_revoked_host_launch_guard_refuses_native_frame_and_ordinary_turn_cannot_widen_policy(
+ original_check, monkeypatch
+):
+ fixture = original_check
+ captured = _capture_native_dispatch(fixture, monkeypatch)
+
+ @contextmanager
+ def revoked_host_write():
+ raise ValueError("the original Task authority was revoked")
+ yield
+
+ fixture.native._launch_guard = revoked_host_write
+ with pytest.raises(ValueError, match="authority was revoked"):
+ fixture.adapter.start_native_task_turn(
+ "Perform the Task.", lambda *_: None, _native_task_policy(fixture)
+ )
+ assert captured == []
+ with pytest.raises(ValueError, match="ordinary Chat Turn"):
+ fixture.native.send(
+ "Ordinary request.", sandbox_policy=_native_task_policy(fixture)
+ )
+ assert captured == []
+
+
+def test_uncertain_native_publication_retains_ordinary_policy_reset_requirement(
+ original_check, monkeypatch
+):
+ fixture = original_check
+ captured = _capture_native_dispatch(fixture, monkeypatch)
+ request = fixture.native._request
+
+ @contextmanager
+ def admitted_host_write():
+ yield
+
+ fixture.native._launch_guard = admitted_host_write
+
+ def uncertain_publication(*args, **kwargs):
+ raise OSError("native publication is unconfirmed")
+
+ monkeypatch.setattr(fixture.native, "_request", uncertain_publication)
+ with pytest.raises(OSError, match="publication is unconfirmed"):
+ fixture.adapter.start_native_task_turn(
+ "Perform the Task.", lambda *_: None, _native_task_policy(fixture)
+ )
+ assert fixture.native._native_task_policy_used is True and captured == []
+ monkeypatch.setattr(fixture.native, "_request", request)
+ fixture.native.send("Read the original evidence after recovery.")
+ assert captured[-1]["params"]["sandboxPolicy"] == {
+ "type": "readOnly",
+ "networkAccess": False,
+ }
+ assert captured[-1]["params"]["threadId"] == "original-thread"
+ assert (
+ fixture.native.execution_mode is False and fixture.native.sandbox == "read-only"
+ )
+
+
+@pytest.mark.parametrize("cold", [False, True], ids=["new", "cold-resume"])
+def test_verified_managed_session_construction_binds_direct_read_default_without_native_history(
+ original_check, monkeypatch, cold
+):
+ from loopx.chat.coordination import PROJECT_CONTEXT_VERSION
+
+ fixture = original_check
+ starts = []
+
+ def start(**kwargs):
+ starts.append(kwargs)
+ native = CodexChatAgentSession(
+ process=SimpleNamespace(poll=lambda: None),
+ messages=queue.Queue(),
+ thread_id=kwargs.get("resume_thread_id") or "new-managed-thread",
+ work_dir=kwargs["work_dir"],
+ )
+ monkeypatch.setattr(native, "close", lambda: None)
+ return CodexAppServerAdapter(native)
+
+ monkeypatch.setattr(CodexAppServerAdapter, "start", start)
+ monkeypatch.setattr(
+ fixture.runtime,
+ "capabilities",
+ lambda: [
+ {"agent_id": "codex", "available": True, "adapter_kind": "codex_app_server"}
+ ],
+ )
+ if cold:
+ fixture.store.update_turn(
+ fixture.session["session_id"], fixture.turn["turn_id"], status="completed"
+ )
+ fixture.store.update_session(
+ fixture.session["session_id"],
+ active_turn_id=None,
+ coordination_context_version=PROJECT_CONTEXT_VERSION,
+ )
+ fixture.runtime.adapters.pop(fixture.session["session_id"])
+ session = fixture.store.load_session(fixture.session["session_id"])
+ adapter = fixture.runtime._ensure_adapter(
+ session, work_dir=fixture.project, objective="Produce a usable export"
+ )
+ assert starts[-1]["resume_thread_id"] == "original-thread"
+ assert adapter.upstream_thread_id == "original-thread"
+ assert [tool["name"] for tool in starts[-1]["dynamic_tools"]] == [
+ "loopx_context_read"
+ ]
+ else:
+ session, reused = fixture.runtime.open_session(
+ goal_id=fixture.session["goal_id"],
+ agent_id="codex",
+ managed_agent_id="agent-a",
+ work_dir=fixture.project,
+ objective="Produce a usable export",
+ mode="new",
+ )
+ assert not reused
+ adapter = fixture.runtime.adapters[session["session_id"]]
+ assert adapter.session._managed_goal_read_default is True
+ assert adapter.session._native_task_policy_used is False
+ wire = _capture_native_dispatch(
+ SimpleNamespace(native=adapter.session), monkeypatch
+ )
+ adapter.session.send("Read the original source.")
+ assert wire[-1]["params"]["sandboxPolicy"] == {
+ "type": "readOnly",
+ "networkAccess": False,
+ }
+ assert wire[-1]["params"]["threadId"] == adapter.upstream_thread_id
+ assert "managed_read_default" not in fixture.store.load_session(
+ session["session_id"]
+ )
+ assert "_managed_goal_read_default" not in fixture.store.load_session(
+ session["session_id"]
+ )
+ assert (
+ adapter.session.execution_mode is False
+ and adapter.session.sandbox == "read-only"
+ )
+
+
+def test_unbound_legacy_session_direct_send_preserves_default_native_payload(
+ original_check, monkeypatch
+):
+ fixture = original_check
+ assert fixture.native._managed_goal_read_default is False
+ assert fixture.native._native_task_policy_used is False
+ wire = _capture_native_dispatch(fixture, monkeypatch)
+ fixture.native.send("Read the source in this legacy transport.")
+ assert "sandboxPolicy" not in wire[-1]["params"]
+ assert (
+ fixture.native.execution_mode is False and fixture.native.sandbox == "read-only"
+ )
diff --git a/tests/control_plane/test_host_current_admission.py b/tests/control_plane/test_host_current_admission.py
index 3fb995e6d1..f3819ec0e2 100644
--- a/tests/control_plane/test_host_current_admission.py
+++ b/tests/control_plane/test_host_current_admission.py
@@ -3,6 +3,7 @@
import json
from pathlib import Path
+import sys
import pytest
@@ -94,3 +95,128 @@ def lose_state_before_start(**kwargs):
assert not state.exists()
index = runtime / "goals/loopx-turn-fixture/runs/index.jsonl"
assert not index.exists() or "quota_slot_spent" not in index.read_text()
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("change", ["task_text", "write_scope", "progress_note"])
+@pytest.mark.parametrize("host", ["exec", "operation"])
+def test_native_launch_rechecks_same_todo_after_planning(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, provider: str, change: str, host: str,
+) -> None:
+ runtime, registry, native, model, arguments = _fixture(tmp_path, monkeypatch, provider)
+ project = registry.parent.parent
+ if host == "exec":
+ executable, model = _fake_codex(tmp_path)
+ executable.write_text(executable.read_text().replace(
+ "args = sys.argv[1:]", f"pathlib.Path({str(native)!r}).write_text('native')\nargs = sys.argv[1:]", 1,
+ ))
+ monkeypatch.setenv("FAKE_CODEX_LOG", str(model))
+ arguments.remove("--codex-operation-tools")
+ arguments[arguments.index("--codex-bin") + 1] = str(executable)
+ old = _quota(registry, runtime, project)
+ assert old["should_run"] is True, old
+ todo_id = old["selected_todo"]["todo_id"]
+ mutation = {
+ "task_text": ["--text", "[P0] Verify the replacement fixture before any work."],
+ "write_scope": ["--required-write-scope", "docs/evidence/**"],
+ "progress_note": ["--note", "Independent observation received."],
+ }[change]
+ host_module = codex_cli if host == "exec" else codex_operation_host
+ original_input = host_module.prepare_codex_cli_input
+ readbacks = []
+
+ def edit_after_planning(request):
+ code, result = _cli([
+ "--registry", str(registry), "--runtime-root", str(runtime), "--format", "json",
+ "todo", "update", "--goal-id", "loopx-turn-fixture", "--agent-id", "codex-fixture",
+ "--todo-id", todo_id, *mutation,
+ ])
+ assert code == 0 and result["ok"], result
+ current = _quota(registry, runtime, project)
+ assert current["should_run"] is True and current["effective_action"] == old["effective_action"], current
+ assert current["selected_todo"]["todo_id"] == todo_id
+ readbacks.append((result, _bindings(runtime)))
+ return original_input(request)
+
+ monkeypatch.setattr(host_module, "prepare_codex_cli_input", edit_after_planning)
+ code, payload = _cli([*arguments, "--execute"])
+ assert len(readbacks) == 1
+ if change == "progress_note":
+ assert native.exists() and model.exists(), payload
+ else:
+ # Operation mode already started app-server; it must not start a
+ # model Turn or publish its Session binding on the stale request.
+ assert native.exists() is (host == "operation") and not model.exists(), payload
+ assert code == 1 and "current_turn_work_changed" in json.dumps(payload), payload
+ assert _bindings(runtime) == readbacks[0][1]
+ index = runtime / "goals/loopx-turn-fixture/runs/index.jsonl"
+ assert not index.exists() or "quota_slot_spent" not in index.read_text()
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("edit_task", [False, True])
+def test_stdio_retry_checks_original_task_before_launch(tmp_path, monkeypatch, provider, edit_task):
+ from loopx.control_plane.coordination.runtime_shadow import build_todo_runtime_shadow_projection
+ from loopx.control_plane.todos.active_state_todo_parser import parse_active_state_todos
+ from tests.control_plane.canonical_authority_fixture import initialize_canonical_authority, isolate_sqlite_runtime
+ from tests.control_plane.test_turn_host_decision_capture import _fixture_module
+
+ isolate_sqlite_runtime(tmp_path, monkeypatch)
+ fixture = _fixture_module()
+ project, runtime, workspace, registry = fixture._write_fixture(tmp_path, turn_count=2)
+ state = project / ".codex/goals" / fixture.GOAL_ID / "ACTIVE_GOAL_STATE.md"
+ todos = parse_active_state_todos(state.read_text(), state_path=state, item_limit=None)
+ initialize_canonical_authority(runtime, fixture.GOAL_ID,
+ build_todo_runtime_shadow_projection(goal_id=fixture.GOAL_ID, handoff_mode="soft_claim",
+ todos=todos["agent_todos"]["items"]), state_path=state, provider=provider)
+ marker = workspace / "process-count"
+ runner = tmp_path / "runner.py"
+ runner.write_text(f'''import json
+from pathlib import Path
+
+def run_dsh_turn(**options):
+ workspace = Path(options["workspace"])
+ marker = workspace / "process-count"
+ first = not marker.exists()
+ marker.write_text("1" if first else "2")
+ if first:
+ raise TimeoutError("Fixture failed before producing a result")
+ (workspace / {fixture.MARKER_NAME!r}).write_text({fixture._marker_value(1)!r})
+ return json.dumps({{"result_kind": "validated_progress", "classification": "checked_work",
+ "summary": "The original work passed independent validation.", "next_action": "Continue.",
+ "work_evidence": ["artifact:marker"], "validation_evidence": ["fixture:validate-marker"]}})
+''', encoding="utf-8")
+ prefix = ["--registry", str(registry), "--runtime-root", str(runtime), "--format", "json"]
+ subject = ["--goal-id", fixture.GOAL_ID, "--agent-id", fixture.AGENT_ID]
+ adapter = [sys.executable, "-m", "loopx.dsh_goal_mode", "--workspace", str(workspace),
+ "--dsh-runner", str(runner), "--dsh-home", str(tmp_path / "dsh-home")]
+ validator = [sys.executable, "-c", "from pathlib import Path; import json,sys; json.load(sys.stdin); "
+ f"assert Path({fixture.MARKER_NAME!r}).read_text() == {fixture._marker_value(1)!r}"]
+ argv = [*prefix, "turn", "run-once", *subject, "--host", "generic-cli", "--project", str(workspace),
+ "--execution-mode", "isolated-headless", "--no-global-sync", "--timeout-seconds", "20",
+ "--host-adapter-request-schema", "loopx_turn_host_request_v1",
+ "--host-adapter-command-json", json.dumps(adapter),
+ "--validation-command-json", json.dumps(validator), "--execute"]
+ code, first = fixture._run_cli([*argv, "--turn-instance-id", "bound-stdio-retry"])
+ assert code == 1 and first["status"] == "failed", first
+ assert marker.read_text() == "1" and fixture._quota_spend_count(runtime) == 0
+ if edit_task:
+ binding = [*subject, "--todo-id", fixture.TODO_ID]
+ code, before = fixture._run_cli([*prefix, "todo", "list", *binding])
+ assert code == 0
+ code, changed = fixture._run_cli([*prefix, "todo", "update", *binding, "--execute",
+ "--text", before["todo"]["text"] + " Follow the replacement requirement.",
+ "--update-operation-id", "change-captured-work",
+ "--update-expected-provider-revision", before["authority_read"]["provider_revision"]])
+ assert code == 0 and changed["changed"], changed
+ recovery = [*argv, "--resume-turn-key", first["resume_turn_key"]]
+ code, result = fixture._run_cli([*recovery, "--retry-failed-turn"])
+ if edit_task:
+ assert code == 1 and result["error_code"] == "current_turn_work_changed", result
+ assert not any(result["effects"].values())
+ else:
+ assert code == 0 and result["status"] == "committed", result
+ _, replay = fixture._run_cli(recovery)
+ assert replay["replayed"] and not any(replay["effects"].values())
+ assert marker.read_text() == ("1" if edit_task else "2")
+ assert fixture._quota_spend_count(runtime) == (0 if edit_task else 1)
diff --git a/tests/control_plane/test_new_goal_storage_defaults.py b/tests/control_plane/test_new_goal_storage_defaults.py
index 4492d76e97..b294eec86a 100644
--- a/tests/control_plane/test_new_goal_storage_defaults.py
+++ b/tests/control_plane/test_new_goal_storage_defaults.py
@@ -589,7 +589,7 @@ def interrupt_after_session(*args, **kwargs):
elif refusal == "foreign_session":
# The public store forbids rebinding a Goal Session. Model an
# independently corrupted file in this disposable fixture only.
- with pytest.raises(ValueError, match="only the owner manager"):
+ with pytest.raises(ValueError, match="a registered Agent Session cannot change its original Goal"):
chat.update_session(session_id, goal_id="another-goal")
chat._session_path(session_id).write_text(json.dumps({**session, "goal_id": "another-goal"}))
controller = controller_for(runtime / "missing-codex" if refusal == "missing_host" else fake)
@@ -625,7 +625,8 @@ def interrupt_after_session(*args, **kwargs):
controller.open_session(goal_id="pre-first-turn", agent_id="codex",
goal_instance_id=session.get("goal_instance_id"), work_dir=project,
objective=preview["proposal"]["normalized_parameters"]["objective"],
- mode="resume_latest", channel_id="goal.pre-first-turn")
+ mode="resume_latest", channel_id=session["channel_id"],
+ managed_agent_id=session["managed_agent_id"])
assert chat.turn_for_client(session_id, client_turn_id) == pending
assert len(chat.list_sessions(goal_id="pre-first-turn")) == 1
elif refusal == "terminal_before_start":
diff --git a/tests/control_plane/test_original_task_entry.py b/tests/control_plane/test_original_task_entry.py
new file mode 100644
index 0000000000..72eabf8b73
--- /dev/null
+++ b/tests/control_plane/test_original_task_entry.py
@@ -0,0 +1,235 @@
+"""Original Chat request reaches the existing repair checker and Task owner.
+
+The production HTTP/Chat/Core/File/SQLite owners execute; model interpretation
+and editing are scripted. The six preexisting repair checks remain independent.
+"""
+import hashlib
+import http.client
+import json
+import runpy
+import shutil
+import subprocess
+import sys
+import threading
+from pathlib import Path
+
+import pytest
+
+from test_new_goal_storage_defaults import environment as environment
+from loopx.chat_action_store import ChatActionStore
+from loopx.chat_actions import ChatActionService
+from loopx.chat_runtime import ChatRuntimeController
+from loopx.chat_server import ChatHTTPServer, ChatRequestHandler
+from loopx.chat_store import ChatSessionStore
+from loopx.capabilities.native_chat.project_context import ChatProjectContexts
+from loopx.control_plane.coordination.local_authority import read_canonical_todos_if_promoted
+from loopx.control_plane.goals.acceptance import inspect_goal_acceptance
+
+
+@pytest.fixture
+def cart_host(environment, monkeypatch):
+ _, bootstrap, _, project, runtime = environment
+ bootstrap("workspace")
+ exercise = Path(__file__).parents[2] / "examples/bugfix-loop"
+ for name in ("checks.py", "cart.py", "README.md"):
+ shutil.copy2(exercise / name, project / name)
+ capture, binary = runtime / "requests.jsonl", runtime / "synthetic-codex"
+ from loopx.control_plane.turn_driver import host_candidate
+ extract = host_candidate.extract_turn_authority
+ def capture_authority(*args, **kwargs):
+ authority = extract(*args, **kwargs)
+ (runtime / "native-authority.json").write_text(json.dumps(authority))
+ return authority
+ monkeypatch.setattr(host_candidate, "extract_turn_authority", capture_authority)
+ source = runpy.run_path(str(Path(__file__).parents[2] / "examples/loopx-chat-runtime-smoke.py"))["FAKE_CODEX"]
+ source = source.replace(' method = request.get("method")',
+ f' with open({str(capture)!r}, "a") as stream:\n stream.write(json.dumps(request) + "\\n")\n'
+ ' method = request.get("method")')
+ # The model proposes a same-workspace existing checker, with no absolute interpreter,
+ # source hash, Task id or Goal/Agent routing supplied as execution authority.
+ document = {"outcome_id": "goal-creation", "criteria": [{"id": "completion",
+ "validation_argv": ["python3", "checks.py"], "validation_timeout_seconds": 5,
+ "oracle": {"kind": "owner_pinned_oracle", "source_id": "bugfix-cart",
+ "input_refs": [], "output_refs": ["cart.py"]}}]}
+ source = source.replace(' elif method == "turn/start":',
+ ' elif request_id == 900 and method is None:\n'
+ ' evidence = json.loads(request["result"]["contentItems"][0]["text"])\n'
+ ' response = "" + json.dumps({"schema_version": "loopx_chat_agent_response_v0",\n'
+ ' "message": "The existing six-check repair plan is ready for owner review.", "proposals": [], "gate": None}) + " "\n'
+ ' print(json.dumps({"method": "item/agentMessage/delta", "params": {"threadId": "durable-thread", "turnId": active_turn, "delta": response}}), flush=True)\n'
+ ' print(json.dumps({"method": "turn/completed", "params": {"threadId": "durable-thread", "turn": {"id": active_turn}}}), flush=True)\n'
+ ' turn_status[active_turn] = "completed"\n'
+ ' continue\n'
+ ' elif method == "turn/start":')
+ source = source.replace(" response = 'Visible runtime response.",
+ f' if not __import__("pathlib").Path({str(project / "execute-native")!r}).exists():\n'
+ ' print(json.dumps({"id": 900, "method": "item/tool/call", "params": {\n'
+ ' "threadId": "durable-thread", "turnId": active_turn, "tool": "loopx_collaboration",\n'
+ ' "arguments": ' + repr({"action": "prepare_check", "operation_id": "original-cart-check", "document": document}) + '}}), flush=True)\n'
+ ' continue\n'
+ f' mode = __import__("pathlib").Path({str(project / "execute-native")!r}).read_text()\n'
+ ' if mode == "valid":\n'
+ f' assert request["params"].get("sandboxPolicy") == {{"type": "workspaceWrite", "writableRoots": [{str(project.resolve())!r}], "networkAccess": False, "excludeTmpdirEnvVar": True, "excludeSlashTmp": True}}\n'
+ f' assert request["params"].get("runtimeWorkspaceRoots") == [{str(project.resolve())!r}]\n'
+ f' path = __import__("pathlib").Path({str(project / "cart.py")!r})\n'
+ ' cart = path.read_text().replace(" if subtotal > 10_000:", " if not prices or subtotal >= 10_000:")\n'
+ ' path.write_text(cart)\n'
+ " response = 'Visible runtime response.")
+ candidate = {"result_kind": "validated_completion", "classification": "implementation",
+ "summary": "Repaired the empty-cart and free-shipping boundaries.",
+ "next_action": "Read back the original cart artifact and independent checks.",
+ "delivery_batch_scale": "implementation", "delivery_outcome": "outcome_progress",
+ "vision_unchanged_reason": "The original six-check cart repair remains the current task.",
+ "work_evidence": ["cart.py"], "validation_evidence": ["Unchanged checks.py verifies six original behaviors"]}
+ source = source.replace('"message": "Runtime response."', '"message": ' + repr(json.dumps(candidate)))
+ binary.write_text(source)
+ compile(source, str(binary), "exec")
+ binary.chmod(0o700)
+ store = ChatSessionStore(runtime / "chat")
+ registry = project / ".loopx/registry.json"
+ controller = ChatRuntimeController(store=store, codex_bin=str(binary), registry_path=registry,
+ project_contexts=ChatProjectContexts([project]))
+ actions = ChatActionStore(runtime / "chat/actions")
+ service = ChatActionService(store=actions, registry_path=registry,
+ runtime_controller=controller, chat_store=store)
+ server = ChatHTTPServer(("127.0.0.1", 0), ChatRequestHandler)
+ server.verbose = False
+ server.registry_path, server.runtime_root = registry, runtime
+ server.runtime_controller, server.action_store, server.action_service = controller, actions, service
+ thread = threading.Thread(target=server.serve_forever, daemon=True)
+ thread.start()
+
+ def request(path, body=None):
+ connection = http.client.HTTPConnection(*server.server_address, timeout=30)
+ try:
+ connection.request("GET" if body is None else "POST", path,
+ None if body is None else json.dumps(body), {"Content-Type": "application/json"})
+ response = connection.getresponse()
+ return response.status, json.loads(response.read())
+ finally:
+ connection.close()
+
+ try:
+ yield actions, request, controller, capture, project, runtime
+ finally:
+ server.shutdown()
+ thread.join(timeout=5)
+ server.server_close()
+ controller.close()
+
+
+def create_cart(cart_host):
+ _, request, controller, _, project, runtime = cart_host
+ status, preview = request("/api/actions/preview", {"action_kind": "goal.create",
+ "summary": "Repair the cart using its existing checks", "idempotency_key": "cart-repair-request",
+ "context": {"kind": "goal_channel", "goal_id": "workspace"}, "normalized_parameters": {
+ "goal_id": "cart-repair", "title": "Cart repair",
+ "objective": "Repair cart.py in this exercise workspace according to README.md and unchanged checks.py.",
+ "completion_criteria": "The six existing checks pass without changing checks.py.",
+ "agent_id": "codex", "workspace_ref": "current", "heartbeat": {"enabled": False},
+ "initial_todos": ["Repair cart.py to pass the six existing behavioral checks."]}})
+ assert status == 201, preview
+ proposal = preview["proposal"]
+ status, applied = request(f"/api/actions/{proposal['proposal_id']}/apply", {})
+ assert status == 202, applied
+ ids = applied["proposal"]["receipt"]["resource_ids"]
+ first = controller.wait_for_turn(session_id=ids["session_id"], turn_id=ids["turn_id"], timeout_sec=15)
+ assert first["status"] == "completed", first
+ messages = controller.store.messages(ids["session_id"])
+ assert messages[0]["text"] == proposal["normalized_parameters"]["objective"]
+ assert first["message"].startswith("开始推进 Goal"), "execution identity retains its original internal request"
+ basis = inspect_goal_acceptance(registry_path=controller.registry_path, runtime_root=str(runtime),
+ goal_id="cart-repair", agent_id="codex")
+ assert not basis.get("completion_requirements")
+ return proposal, ids, first
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("mode", ["valid", "unconfirmed", "changed-source", "failed", "read-only"])
+def test_original_http_task_reuses_requested_checker_and_session(environment, cart_host, provider, mode):
+ configure, *_ = environment
+ configure(provider)
+ actions, request, controller, capture, project, runtime = cart_host
+ _, ids, first = create_cart(cart_host)
+ sid, todo = ids["session_id"], ids["todo_ids"][0]
+ session, history = controller.store.load_session(sid), controller.store.messages(sid)
+ checker_hash = hashlib.sha256((project / "checks.py").read_bytes()).hexdigest()
+ proposals = actions.list(goal_id="cart-repair", context_kind="goal")
+ checks = [row for row in proposals if row["action_kind"] == "goal.acceptance.check"]
+ assert len(checks) == 1, {"first": first, "proposals": proposals}
+ check = checks[0]
+ criterion = check["normalized_parameters"]["document"]["criteria"][0]
+ assert criterion["validation_argv"] == ["python3", "checks.py"]
+ assert Path(shutil.which("python3")).resolve() == Path(sys.executable).resolve()
+ assert criterion["validation_files"] == [{"path": "checks.py", "sha256": checker_hash}]
+ assert check["status"] == "preview_ready" and not first.get("loopx_execution")
+ calls_before = capture.read_bytes()
+ status, unavailable = request(f"/api/chat/sessions/{sid}/native-turn?original_task=true&agent_id=codex")
+ assert status == 200 and unavailable["task"]["state"] == "unbound", unavailable
+ if mode != "unconfirmed":
+ status, adopted = request(f"/api/actions/{check['proposal_id']}/apply", {})
+ assert status == 200 and adopted["proposal"]["receipt"]["checks_passed"] is False, adopted
+ assert capture.read_bytes() == calls_before, "check confirmation cannot launch a model/Task"
+ status, observed = request(f"/api/chat/sessions/{sid}/native-turn?original_task=true&agent_id=codex")
+ assert status == 200 and observed["task"]["state"] == "ready", observed
+ assert observed["task"]["todo_id"] == todo and not observed["task"]["canonical_done"]
+ assert "effects" not in observed["task"]
+ if mode == "changed-source":
+ (project / "checks.py").write_text("raise SystemExit(0)\n")
+ if mode in {"valid", "failed"}:
+ # An explicit, existing owner approval is separate from check adoption
+ # and native Task selection. Default new Goals do not gain write rights.
+ approved = subprocess.run([sys.executable, "-m", "loopx.entrypoint",
+ "--registry", str(controller.registry_path), "--runtime-root", str(runtime), "--format", "json",
+ "configure-goal", "--goal-id", "cart-repair", "--write-scope", "**",
+ "--boundary-authority-scope", "**", "--boundary-authority-source", "owner-approved-exercise",
+ "--boundary-authority-decision-id", "cart-workspace", "--execute"],
+ capture_output=True, text=True, timeout=20)
+ assert approved.returncode == 0, approved.stdout + approved.stderr
+ (project / "execute-native").write_text(mode)
+ status, accepted = request(f"/api/chat/sessions/{sid}/native-turn", {
+ "agent_id": "codex", "operation_id": "original-cart-execution", "todo_id": todo})
+ assert status == 202, accepted
+ turn = controller.wait_for_turn(session_id=sid, turn_id=accepted["turn"]["turn_id"], timeout_sec=20)
+ result = turn["response"]["native_loopx"]
+ assert result["status"] == {"valid": "committed", "unconfirmed": "stopped",
+ "changed-source": "stopped", "failed": "failed", "read-only": "failed"}[mode], result
+ assert result["effects"]["quota_spent"] is (mode == "valid")
+ assert result["effects"]["state_written"] is (mode == "valid")
+ authority = read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="cart-repair", include_leases=True)
+ assert next(row for row in authority["todos"] if row["todo_id"] == todo)["status"] == ("done" if mode == "valid" else "open")
+ calls = [json.loads(line) for line in capture.read_bytes()[len(calls_before):].splitlines()]
+ native_calls = [row for row in calls if row.get("method") == "turn/start"]
+ assert len(native_calls) == (1 if mode in {"valid", "failed", "read-only"} else 0)
+ if native_calls:
+ assert native_calls[0]["params"]["sandboxPolicy"]["type"] == ("readOnly" if mode == "read-only" else "workspaceWrite")
+ if mode == "read-only":
+ assert "runtimeWorkspaceRoots" not in native_calls[0]["params"]
+ else:
+ assert native_calls[0]["params"]["runtimeWorkspaceRoots"] == [str(project.resolve())]
+ assert controller.adapters[sid].session.sandbox == "read-only"
+ assert controller.adapters[sid].session.execution_mode is False
+ assert not any(row.get("method") == "thread/start" for row in calls)
+ assert controller.store.load_session(sid)["upstream_thread_id"] == session["upstream_thread_id"]
+ assert controller.store.messages(sid)[:len(history)] == history
+ after = capture.read_bytes()
+ status, replay = request(f"/api/chat/sessions/{sid}/native-turn", {
+ "agent_id": "codex", "operation_id": "original-cart-execution", "todo_id": todo})
+ assert status == 202 and not replay["created"] and replay["turn"]["turn_id"] == turn["turn_id"]
+ assert capture.read_bytes() == after
+ if mode == "valid":
+ (project / "execute-native").write_text("ordinary")
+ followup, created = controller.submit_turn(session_id=sid, client_turn_id="ordinary-after-task",
+ message="Explain the repaired behavior without changing the files.", work_dir=project,
+ objective="Retain the original cart repair.")
+ assert created
+ answered = controller.wait_for_turn(session_id=sid, turn_id=followup["turn_id"], timeout_sec=10)
+ assert answered["status"] == "completed" and "native_loopx" not in answered["response"]
+ later = [json.loads(line) for line in capture.read_bytes()[len(after):].splitlines()]
+ next_calls = [row for row in later if row.get("method") == "turn/start"]
+ assert len(next_calls) == 1 and next_calls[0]["params"]["sandboxPolicy"] == {
+ "type": "readOnly", "networkAccess": False}
+ assert not any(row.get("method") == "thread/start" for row in later)
+ check_run = subprocess.run([sys.executable, "checks.py"], cwd=project, capture_output=True, text=True, timeout=10)
+ assert check_run.returncode == 0 and "Ran 6 tests" in check_run.stderr
+ assert hashlib.sha256((project / "checks.py").read_bytes()).hexdigest() == checker_hash
diff --git a/tests/control_plane/test_quota_settlement_cli.py b/tests/control_plane/test_quota_settlement_cli.py
index e747385d8c..e5a420ed67 100644
--- a/tests/control_plane/test_quota_settlement_cli.py
+++ b/tests/control_plane/test_quota_settlement_cli.py
@@ -7136,8 +7136,9 @@ def test_unbound_visible_goal_todoless_replan_reenters_through_guided_turn(
assert {path: path.read_bytes() for path in source_paths} == original_files
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
def test_autonomous_replan_semantic_delta_keeps_accountable_receipt_chain(
- tmp_path: Path,
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, provider: str,
) -> None:
"""Regression for #3528's real Todo-bound replan path.
@@ -7147,12 +7148,15 @@ def test_autonomous_replan_semantic_delta_keeps_accountable_receipt_chain(
instead of being mislabeled as a noop after it discharges the obligation.
"""
+ from canonical_authority_fixture import isolate_sqlite_runtime
+
+ isolate_sqlite_runtime(tmp_path, monkeypatch)
project, runtime, registry_path = _write_fixture(tmp_path)
- _configure_selected_todo_replan_fixture(project, registry_path)
+ _configure_selected_todo_replan_fixture(project, registry_path, stride_strategy="off")
_initialize_git_checkout(project)
turn_instance_id = "turn-autonomous-replan-semantic-1"
- _initialize_fixture_authority(project, runtime)
+ _initialize_fixture_authority(project, runtime, provider=provider)
guard_rc, guard = _run_cli(
registry_path,
runtime,
@@ -7235,6 +7239,9 @@ def test_autonomous_replan_semantic_delta_keeps_accountable_receipt_chain(
assert [
receipt["step_kind"] for receipt in refresh["settlement_result"]["receipts"]
] == ["validation", "durable_writeback"]
+ original = guard["heartbeat_receipt"]["settlement_identity"]
+ assert refresh["settlement_identity"] == original
+ assert _spend_run_count(runtime) == 0
replay_rc, replay = _run_cli(
registry_path,
@@ -7246,23 +7253,98 @@ def test_autonomous_replan_semantic_delta_keeps_accountable_receipt_chain(
assert replay["appended"] is False
assert _classification_count(runtime, "validated_progress") == 1
- next_guard_rc, next_guard = _run_cli(
- registry_path,
- runtime,
- "quota",
- "should-run",
- "--codex-app",
- "--goal-id",
- GOAL_ID,
- "--agent-id",
- AGENT_ID,
- "--turn-instance-id",
- "turn-after-autonomous-replan-semantic-1",
- )
+ # A new guard can see the replan has ended before the original debit is
+ # durable. It must still expose recovery instead of another execution.
+ next_turn = "turn-after-autonomous-replan-semantic-1"
+ next_args = ("quota", "should-run", "--codex-app", "--goal-id", GOAL_ID,
+ "--agent-id", AGENT_ID, "--turn-instance-id", next_turn)
+ next_guard_rc, next_guard = _run_cli(registry_path, runtime, *next_args)
assert next_guard_rc == 0, next_guard
- assert next_guard.get("autonomous_replan_obligation") is None
- assert next_guard.get("decision") != "autonomous_replan_required"
- assert obligation_id not in json.dumps(next_guard, sort_keys=True)
+ assert next_guard["effective_action"] == "unsettled_host_turn_recovery", next_guard
+ assert next_guard["normal_delivery_allowed"] is False
+ recovery = next_guard["unsettled_host_turn_recovery"]
+ assert recovery["prior_turn_instance_id"] == turn_instance_id
+ assert recovery["binding_id"] == SELECTED_REPLAN_TODO_ID
+ assert recovery["settlement_effect_id"] == original["effect_id"]
+ assert recovery["missing_receipts"] == ["quota_spend_receipt"]
+ assert all(not row["details"].get("settlement_effect_id")
+ for row in _heartbeat_receipt_events(runtime, next_turn))
+
+ explicit_rc, explicit = _run_cli(registry_path, runtime, *next_args, "--todo-id", SELECTED_REPLAN_TODO_ID)
+ assert explicit_rc == 0 and explicit["effective_action"] == "unsettled_host_turn_recovery", explicit
+ assert explicit["unsettled_host_turn_recovery"] == recovery
+ assert "settlement_identity" not in explicit["heartbeat_receipt"]
+ actions = explicit["interaction_contract"]["cli_channel"]["next_cli_actions"]
+ original_guard = next(command for command in actions if turn_instance_id in shlex.split(command))
+ rc, recovering = _run_generated_cli(original_guard, registry_path=registry_path, cwd=project)
+ assert rc == 0 and recovering["normal_delivery_allowed"] is False, recovering
+ owed = recovering["settlement_owed"]
+ assert owed["command"] == refresh["settlement_owed"]["command"]
+ assert owed["effect_id"] == original["effect_id"]
+
+ def reject_changed_spend() -> None:
+ for flag, replacement in (("--slots", "2"), ("--source", "visible-goal")):
+ tokens = shlex.split(owed["command"])
+ tokens[tokens.index(flag) + 1] = replacement
+ refused_rc, refused = _run_generated_cli(
+ shlex.join(tokens), registry_path=registry_path, cwd=project,
+ )
+ assert refused_rc == 1 and refused["ok"] is False, refused
+ assert refused["appended"] is False
+ assert refused["settlement_result"]["failure"]["kind"] == "quota_spend_rejected"
+
+ reject_changed_spend()
+ assert _spend_run_count(runtime) == 0
+ from concurrent.futures import ThreadPoolExecutor
+
+ def settle() -> dict[str, Any]:
+ rc, result = _run_generated_cli(owed["command"], registry_path=registry_path, cwd=project)
+ if rc != 0:
+ pytest.fail(f"Original settlement failed: {result.get('error_code')}: {result.get('reason') or result.get('error')}")
+ assert result["settlement_identity"] == original
+ return result
+
+ with ThreadPoolExecutor(max_workers=2) as pool:
+ results = [pool.submit(settle), pool.submit(settle)]
+ attempts = [result.result() for result in results]
+ assert sorted(attempt["appended"] for attempt in attempts) == [False, True]
+ assert all(
+ attempt["settlement_result"]["ok"]
+ and [receipt["step_kind"] for receipt in attempt["settlement_result"]["receipts"]]
+ == ["validation", "durable_writeback", "quota_spend"]
+ for attempt in attempts
+ )
+ rollout_log = runtime / "goals" / GOAL_ID / "rollout-event-log.jsonl"
+ events = [json.loads(line) for line in rollout_log.read_text(encoding="utf-8").splitlines()]
+ assert sum(
+ event["event_kind"] == "quota_spend" and event.get("run_id") == turn_instance_id
+ for event in events
+ ) == 1
+ assert _spend_run_count(runtime) == 1
+ reject_changed_spend()
+ # Simulate the debit committing before its rollout receipt. Refused
+ # requests must not fill the missing receipt with different billing terms.
+ partial_log = "".join(json.dumps(event) + "\n" for event in events if event["event_kind"] != "quota_spend")
+ rollout_log.write_text(partial_log, encoding="utf-8")
+ reject_changed_spend()
+ assert rollout_log.read_text(encoding="utf-8") == partial_log
+ repaired = settle()
+ assert repaired["appended"] is False and repaired["receipt_repaired"] is True
+ assert repaired["settlement_result"]["ok"] is True
+ repaired_events = [json.loads(line) for line in rollout_log.read_text(encoding="utf-8").splitlines()]
+ assert sum(
+ event["event_kind"] == "quota_spend"
+ and event["details"]["settlement_effect_id"] == original["effect_id"]
+ for event in repaired_events
+ ) == 1
+ assert _spend_run_count(runtime) == 1
+ assert _classification_count(runtime, "validated_progress") == 1
+ rc, admitted = _run_cli(registry_path, runtime, *next_args, "--todo-id", SELECTED_REPLAN_TODO_ID)
+ assert rc == 0 and admitted["normal_delivery_allowed"], admitted
+ assert admitted["heartbeat_receipt"]["settlement_identity"]["turn_instance_id"] == next_turn
+ assert admitted["heartbeat_receipt"]["settlement_identity"]["todo_id"] == SELECTED_REPLAN_TODO_ID
+ assert admitted["heartbeat_receipt"]["settlement_identity"]["effect_id"] != original["effect_id"]
+ assert obligation_id not in json.dumps(admitted, sort_keys=True)
def test_open_replan_rejects_missing_semantic_delta_before_durable_write(
diff --git a/tests/control_plane/test_remote_authority_gateway.py b/tests/control_plane/test_remote_authority_gateway.py
new file mode 100644
index 0000000000..a597f39431
--- /dev/null
+++ b/tests/control_plane/test_remote_authority_gateway.py
@@ -0,0 +1,61 @@
+"""The real local CLI does not admit remote work or alter default-off decisions."""
+
+from __future__ import annotations
+
+import json
+
+import pytest
+
+from examples.control_plane.quota_plan_fixtures import SCOPED_AGENT_ID, write_cli_fixture
+from loopx.control_plane.testing.canary_harness import run_json_cli
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_local_cli_ignores_disabled_and_other_goal_remote_binding(tmp_path, monkeypatch, provider):
+ registry, runtime, _ = write_cli_fixture(tmp_path, scoped_agents=True, provider=provider)
+ # Old environment-only canary inputs cannot activate an unconfigured target.
+ monkeypatch.setenv("LOOPX_REMOTE_AUTHORITY_CANARY_ENABLED", "1")
+ monkeypatch.setenv("LOOPX_AUTHORITY_CREDENTIAL", "unused-synthetic-credential-" + "a" * 32)
+ goal_id = "throttled-half"
+ scope = ("--goal-id", goal_id, "--agent-id", SCOPED_AGENT_ID)
+
+ def cli(*args):
+ return run_json_cli(*args, registry_path=registry, runtime_root=runtime)
+
+ def read():
+ prompt = cli("heartbeat-prompt", "--thin", "--codex-app", *scope)
+ assert "remote_authority" not in prompt
+ decision = cli("quota", "should-run", "--runtime-profile", "outer_controller", *scope)
+ assert "remote_gateway" not in decision
+ assert decision["ok"] is True
+ return tuple(decision.get(key) for key in ("decision", "effective_action", "should_run", "reason"))
+
+ def read_local(goal):
+ decision = cli(
+ "quota", "should-run", "--runtime-profile", "outer_controller",
+ "--goal-id", goal, "--agent-id", SCOPED_AGENT_ID,
+ )
+ assert "remote_gateway" not in decision
+ return tuple(decision.get(key) for key in ("decision", "effective_action", "should_run"))
+
+ local = read()
+ assert local[:3] == ("skip", "throttled_skip", False), local
+ assert read_local("half-speed") == ("run", "normal_run", True)
+ document = json.loads(registry.read_text())
+ cfg = {"enabled": True, "endpoint": "https://127.0.0.1:1", "tenant_id": "other-synthetic-tenant",
+ "actor_id": SCOPED_AGENT_ID, "store_identity": "postgresql:" + "a" * 32, "probe_todo_id": "todo_probe"}
+ for goal in document["goals"]:
+ if goal["id"] == goal_id:
+ goal["remote_authority"] = {**cfg, "enabled": False}
+ if goal["id"] == "full-speed":
+ goal["remote_authority"] = cfg
+ registry.write_text(json.dumps(document), encoding="utf-8")
+ assert read() == local
+ assert read_local("half-speed") == ("run", "normal_run", True)
+
+ other = cli("heartbeat-prompt", "--full", "--codex-app",
+ "--goal-id", "full-speed", "--agent-id", SCOPED_AGENT_ID)
+ assert other["remote_authority"]["status"] == "unverified"
+ for key in ("endpoint", "tenant_id", "actor_id", "store_identity", "probe_todo_id"):
+ assert key not in other["remote_authority"]
+ assert cfg["endpoint"] not in other["task_body"] and cfg["tenant_id"] not in other["task_body"]
diff --git a/tests/control_plane/test_scheduler_ack_current_host_binding.py b/tests/control_plane/test_scheduler_ack_current_host_binding.py
index 000d5f9ab3..bbed41e852 100644
--- a/tests/control_plane/test_scheduler_ack_current_host_binding.py
+++ b/tests/control_plane/test_scheduler_ack_current_host_binding.py
@@ -442,6 +442,134 @@ def test_scheduler_fail_current_rejects_missing_turn_receipt_without_state_write
assert not state_path.exists()
+def _scheduler_state_files(runtime_root: Path) -> list[Path]:
+ return [
+ path
+ for goal_dir in sorted((runtime_root / "goals").glob(f"{GOAL_ID}*"))
+ for path in sorted((goal_dir / "scheduler-state").rglob("*.json"))
+ ]
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("operation", ["scheduler-ack-current", "scheduler-fail-current"])
+def test_scheduler_followup_gates_state_migration(
+ tmp_path: Path, provider: str, operation: str,
+) -> None:
+ registry_path, runtime_root, project = write_cli_fixture(
+ tmp_path / "fixture", scoped_agents=True, provider=provider,
+ )
+
+ def call(command: str, turn: str, *flags: str):
+ return run_json_cli_result(
+ "quota", command, "--goal-id", GOAL_ID, "--agent-id", SCOPED_AGENT_ID,
+ "--codex-app", "--app-automation-current-rrule", "FREQ=MINUTELY;INTERVAL=5",
+ "--turn-instance-id", turn, *flags,
+ registry_path=registry_path, runtime_root=runtime_root, cwd=project,
+ )
+
+ stale, current = "heartbeat-older", "heartbeat-current"
+ for turn in (stale, current):
+ rc, guard = call("should-run", turn)
+ assert rc == 0 and guard["ok"] is True
+ assert guard["heartbeat_receipt"]["turn_instance_id"] == turn
+ app = guard["scheduler_hint"]["app_automation"]
+ key = APP_AUTOMATION_STATEFUL_BACKOFF_STATE_KEY
+ flags = ["--state-key", key]
+ if operation == "scheduler-fail-current":
+ flags.extend(["--failed-rrule", app["recommended_rrule"]])
+
+ def reject_stale(*execution: str) -> None:
+ rc, payload = call(operation, stale, *flags, *execution)
+ assert rc == 1 and payload["ok"] is False
+ assert payload["status"] == "heartbeat_receipt_stale"
+ assert payload["state"] == "blocked_receipt"
+ assert payload["error_code"] == "SCHEDULER_FOLLOWUP_HEARTBEAT_RECEIPT_STALE"
+ assert payload["write_performed"] is False
+ assert payload["scheduler_state_mutated"] is False
+ assert payload["quota_spend_performed"] is False
+
+ reject_stale("--execute")
+ assert _scheduler_state_files(runtime_root) == []
+
+ canonical = scheduler_state_path(
+ runtime_root, goal_id=GOAL_ID, agent_id=SCOPED_AGENT_ID, state_key=key,
+ )
+ legacy = (
+ runtime_root / "goals" / GOAL_ID / "scheduler-state"
+ / SCOPED_AGENT_ID / "codex_app" / canonical.name
+ )
+ assert legacy != canonical
+ legacy.parent.mkdir(parents=True, exist_ok=True)
+ legacy.write_text(json.dumps(build_scheduler_state(
+ goal_id=GOAL_ID, agent_id=SCOPED_AGENT_ID, state_key=key,
+ reset_token="earlier-upgrade", identity_signature="earlier-host",
+ progression_index=0, progression_minutes=[3, 10, 30],
+ last_applied_rrule="FREQ=MINUTELY;INTERVAL=5",
+ updated_at="2026-01-01T00:00:00Z",
+ )), encoding="utf-8")
+ original = legacy.read_bytes()
+
+ for execution in ([], ["--execute"]):
+ reject_stale(*execution)
+ assert legacy.read_bytes() == original
+ assert not canonical.exists()
+ rc, preview = call(operation, current, *flags)
+ assert rc == 0 and preview["ok"] is True
+ assert legacy.read_bytes() == original and not canonical.exists()
+
+ rc, committed = call(operation, current, *flags, "--execute")
+ assert rc == 0 and committed["ok"] is True
+ assert committed["scheduler_state_mutated"] is True
+ assert canonical.exists() and not legacy.exists()
+
+
+def test_scheduler_ack_current_routes_the_newest_turn_receipt(
+ tmp_path: Path,
+) -> None:
+ registry_path, runtime_root, project = write_cli_fixture(
+ tmp_path / "fixture",
+ scoped_agents=True,
+ )
+
+ seeded_rc, seeded = run_json_cli_result(
+ "quota",
+ "should-run",
+ "--goal-id",
+ GOAL_ID,
+ "--agent-id",
+ SCOPED_AGENT_ID,
+ "--codex-app",
+ "--turn-instance-id",
+ "heartbeat-newest",
+ registry_path=registry_path,
+ runtime_root=runtime_root,
+ cwd=project,
+ )
+ assert seeded_rc == 0
+ assert seeded["ok"] is True
+
+ returncode, payload = run_json_cli_result(
+ "quota",
+ "scheduler-ack-current",
+ "--goal-id",
+ GOAL_ID,
+ "--agent-id",
+ SCOPED_AGENT_ID,
+ "--codex-app",
+ "--turn-instance-id",
+ "heartbeat-newest",
+ "--execute",
+ registry_path=registry_path,
+ runtime_root=runtime_root,
+ cwd=project,
+ )
+
+ assert returncode == 0
+ assert payload["ok"] is True
+ assert payload["scheduler_state_mutated"] is True
+ assert Path(payload["scheduler_state_path"]).exists()
+
+
def test_trae_app_scheduler_failure_does_not_read_codex_automation_store(
tmp_path: Path,
monkeypatch,
diff --git a/tests/control_plane/test_turn_host_decision_capture.py b/tests/control_plane/test_turn_host_decision_capture.py
index e965463074..648f63cdcd 100644
--- a/tests/control_plane/test_turn_host_decision_capture.py
+++ b/tests/control_plane/test_turn_host_decision_capture.py
@@ -221,6 +221,340 @@ def execute():
assert state["eligibility"]["state"] == "waiting"
+@pytest.mark.parametrize("mutation,expected", [
+ ("inspection_key_route", "LoopX Turn CLI inspection requires the explicit run-once execution surface"),
+ ("key_route", "LoopX Turn plan has no valid transaction turn_key"),
+ ("route_false", "staged route refused"),
+ ("route_integer", "staged route refused"),
+ ("route_string", "staged route refused"),
+ ("route_none", "staged route refused"),
+ ("route_mapping", "staged route refused"),
+ ("route_fallback", "LoopX Turn route is not host executable"),
+])
+def test_staged_capture_rechecks_aliased_plan_guards_before_effects(
+ complete_host_request, tmp_path, monkeypatch, mutation, expected,
+):
+ from loopx.control_plane.turn_driver import executor
+ from loopx.control_plane.turn_driver.driver import build_loopx_turn_plan
+
+ request, original = complete_host_request
+ plan = build_loopx_turn_plan(request["turn_envelope"], work_contract=original,
+ host="generic-cli", execution_mode="isolated-headless",
+ turn_instance_id="staged-alias-guards")
+ runtime = tmp_path / "staged-alias-runtime"
+ captures = []
+ effects = []
+ original_stage = executor.stage_turn_decision_capture
+
+ def stage_then_mutate(*args, **kwargs):
+ capture = original_stage(*args, **kwargs)
+ captures.append(capture)
+ # A frozen capture still shares nested mutable plan fields. Mutate
+ # only after the real source binding, so the incoming checks pass.
+ if mutation == "inspection_key_route":
+ capture.plan["boundary"]["inspection_only"] = True
+ if mutation in {"inspection_key_route", "key_route"}:
+ capture.plan["transaction"]["turn_key"] = "invalid turn key"
+ route_flag = {
+ "route_integer": 1, "route_string": "true", "route_none": None,
+ "route_mapping": {"enabled": True},
+ }.get(mutation, False)
+ capture.plan["route"]["would_invoke_host"] = route_flag
+ capture.plan["error"] = "" if mutation == "route_fallback" else "staged route refused"
+ return capture
+
+ def unexpected_effect(*args, **kwargs):
+ effects.append((args, kwargs))
+ raise AssertionError("a staged guard refusal must precede execution effects")
+
+ monkeypatch.setattr(executor, "stage_turn_decision_capture", stage_then_mutate)
+ with pytest.raises(ValueError) as refused:
+ executor.run_loopx_turn_once(plan, host_runner=unexpected_effect,
+ project=tmp_path / "project", runtime_root=runtime, goal_id=original["goal_id"],
+ timeout_seconds=5, execute=True, writeback=unexpected_effect,
+ spend=unexpected_effect, scheduler=unexpected_effect, admit_start=unexpected_effect,
+ confirm_start=unexpected_effect, decision_capture_payload=original,
+ require_decision_capture=True)
+ assert str(refused.value) == expected
+ assert len(captures) == 1 and effects == []
+ assert not captures[0].path.exists()
+ journal = turn_journal_path(runtime, goal_id=original["goal_id"],
+ turn_key=request["turn_key"])
+ assert not journal.exists()
+
+
+@pytest.mark.parametrize("mutation,expected", [
+ ("contract", "complete Turn decision content integrity mismatch"),
+ ("source_hash", "complete Host request differs from captured source"),
+ ("capture_key", "complete Host request capture binding differs"),
+ ("adapter", "complete Turn plan requires one source without an executor adapter"),
+])
+def test_staged_capture_rechecks_source_and_header_before_effects(
+ complete_host_request, tmp_path, monkeypatch, mutation, expected,
+):
+ from copy import deepcopy
+ from loopx.control_plane.turn_driver import executor
+ from loopx.control_plane.turn_driver.driver import build_loopx_turn_plan
+
+ request, original = complete_host_request
+ original = deepcopy(original)
+ plan = build_loopx_turn_plan(request["turn_envelope"], work_contract=original,
+ host="generic-cli", execution_mode="isolated-headless",
+ turn_instance_id="staged-source-guards")
+ captures = []
+ original_stage = executor.stage_turn_decision_capture
+
+ def stage_then_mutate(*args, **kwargs):
+ capture = original_stage(*args, **kwargs)
+ captures.append(capture)
+ if mutation == "contract":
+ capture.plan["work_contract"]["agent_identity"]["agent_id"] = "changed-agent"
+ elif mutation == "source_hash":
+ capture.plan["source_decision_hash"] = "sha256:" + "0" * 64
+ elif mutation == "capture_key":
+ capture.plan["detail_ref"]["captured_decision"]["turn_key"] = "sha256:" + "0" * 64
+ else:
+ capture.plan["turn_envelope"] = request["turn_envelope"]
+ return capture
+
+ def unexpected_effect(*_args, **_kwargs):
+ raise AssertionError("a staged source refusal must precede execution effects")
+
+ runtime = tmp_path / "staged-source-runtime"
+ monkeypatch.setattr(executor, "stage_turn_decision_capture", stage_then_mutate)
+ with pytest.raises((ValueError, RuntimeError)) as refused:
+ executor.run_loopx_turn_once(plan, host_runner=unexpected_effect,
+ project=tmp_path / "project", runtime_root=runtime, goal_id=original["goal_id"],
+ timeout_seconds=5, execute=True, writeback=unexpected_effect,
+ spend=unexpected_effect, scheduler=unexpected_effect, admit_start=unexpected_effect,
+ decision_capture_payload=original, require_decision_capture=True)
+ assert str(refused.value) == expected
+ assert len(captures) == 1 and not captures[0].path.exists()
+ assert not turn_journal_path(runtime, goal_id=original["goal_id"],
+ turn_key=request["turn_key"]).exists()
+
+
+@pytest.mark.parametrize("optional_context", [False, True])
+def test_staged_capture_preserves_optional_bridge_startup_diagnostic(
+ complete_host_request, tmp_path, monkeypatch, optional_context,
+):
+ from loopx.control_plane import effect_runtime
+ from loopx.control_plane.runtime.node_probe import NodeProbe, NodeProbeOutcome
+ from loopx.control_plane.turn_driver import executor
+ from loopx.control_plane.turn_driver.driver import build_loopx_turn_plan
+
+ request, original = complete_host_request
+ plan = build_loopx_turn_plan(request["turn_envelope"], work_contract=original,
+ host="generic-cli", execution_mode="isolated-headless",
+ turn_instance_id="staged-bridge-failure")
+ original_carrier = json.dumps(plan, ensure_ascii=False)
+ original_stage = executor.stage_turn_decision_capture
+ original_request = effect_runtime.effect_runtime_request
+ captures, operations, probes = [], [], []
+
+ def observe_request(method, params, **kwargs):
+ operations.append((method, params.get("operation")))
+ return original_request(method, params, **kwargs)
+
+ def missing_node(**_kwargs):
+ probes.append("missing")
+ return NodeProbe(NodeProbeOutcome.MISSING)
+
+ def stage_then_lose_bridge(*args, **kwargs):
+ capture = original_stage(*args, **kwargs)
+ captures.append(capture)
+ if optional_context:
+ capture.plan["turn_context_contributions"] = [{
+ "hook_id": "synthetic-context", "readback_slots": [],
+ }]
+ # A new disposable locator selects the real startup failure path;
+ # no serving runtime or active developer process is retired.
+ monkeypatch.setattr(effect_runtime, "_runtime_dir", lambda: tmp_path / "missing-node-runtime")
+ monkeypatch.setattr(effect_runtime, "_probe_node", missing_node)
+ monkeypatch.setattr(effect_runtime, "effect_runtime_request", observe_request)
+ return capture
+
+ def unexpected_effect(*_args, **_kwargs):
+ raise AssertionError("bridge startup refusal must precede execution effects")
+
+ runtime = tmp_path / "staged-bridge-runtime"
+ monkeypatch.setattr(executor, "stage_turn_decision_capture", stage_then_lose_bridge)
+ with pytest.raises(effect_runtime.EffectRuntimeNodeProbeError) as refused:
+ executor.run_loopx_turn_once(plan, host_runner=unexpected_effect,
+ project=tmp_path / "project", runtime_root=runtime, goal_id=original["goal_id"],
+ timeout_seconds=5, execute=True, writeback=unexpected_effect,
+ spend=unexpected_effect, scheduler=unexpected_effect, admit_start=unexpected_effect,
+ decision_capture_payload=original, require_decision_capture=True)
+ assert refused.value.diagnostic_code == "node_unavailable"
+ assert str(refused.value) == "LoopX Effect runtime requires Node.js 22.22.3 or newer on PATH"
+ assert len(probes) == (2 if optional_context else 1)
+ assert len(operations) == len(probes)
+ if optional_context:
+ assert operations[0] == ("quota.turn_envelope.evaluate", "host_context_binding")
+ assert operations[-1][0] == "quota.turn_envelope.evaluate"
+ assert operations[-1][1] in {"host_context", "host_dispatch_context"}
+ assert len(captures) == 1 and not captures[0].path.exists()
+ assert not turn_journal_path(runtime, goal_id=original["goal_id"],
+ turn_key=request["turn_key"]).exists()
+ assert json.dumps(plan, ensure_ascii=False) == original_carrier
+ assert json.dumps(captures[0].observation, ensure_ascii=False) == json.dumps(original, ensure_ascii=False)
+ assert not list(runtime.rglob("*.jsonl"))
+
+
+@pytest.mark.parametrize("current,child_limit", [(True, 2), (True, True), (False, 2)])
+def test_staged_dispatch_preserves_complete_request_values(
+ complete_host_request, tmp_path, monkeypatch, current, child_limit,
+):
+ from copy import deepcopy
+ from loopx.control_plane.quota import turn_envelope
+ from loopx.control_plane.turn_driver import executor
+ from loopx.control_plane.turn_driver.driver import build_loopx_turn_plan
+
+ _, original = complete_host_request
+ source = deepcopy({key: value for key, value in original.items()
+ if key not in {"schema_version", "contract_binding"}})
+ source["future_task_data"] = {"numbers": [2**60 + 1, 1.0, -0.0],
+ "text": "Preserve all original requirements. \n保留原要求。"}
+ source["interaction_contract"]["agent_context"] = {"contributions": [
+ {"capability_id": "multi_subagent", "facts": {"max_children": child_limit}},
+ ]}
+ contract = turn_envelope.compose_turn_decision(source)
+ envelope = turn_envelope.build_turn_envelope(contract)
+ plan = build_loopx_turn_plan(envelope, host="generic-cli", execution_mode="isolated-headless",
+ turn_instance_id="staged-request-values", **({"work_contract": contract} if current else {}))
+ if not current:
+ # Retain the published transitional adapter decoder as a control.
+ plan["work_contract"] = contract
+ plan["goal_ref"] = {"goal_id": original["goal_id"], "instance_id": "synthetic-instance"}
+ plan["delegation_context"] = {"allowed": False, "detail": "preserve delegation"}
+ plan["child_operations"] = [{"operation": "synthetic-child-observation"}]
+ plan["subagent_execution_topology"] = {"state": "synthetic-topology"}
+ captured, dispatched, admissions = [], [], []
+ original_stage = executor.stage_turn_decision_capture
+ original_check = turn_envelope.complete_turn_host_source
+
+ def observe_stage(*args, **kwargs):
+ capture = original_stage(*args, **kwargs)
+ captured.append(capture)
+ return capture
+
+ def observe_dispatch(request, **kwargs):
+ if kwargs.get("operation") == "host_dispatch_context":
+ dispatched.append(request)
+ return original_check(request, **kwargs)
+
+ def deny_start(admission):
+ admissions.append(admission)
+ return {"admitted": False, "reason": "synthetic interval wait"}
+
+ def unexpected_effect(*_args, **_kwargs):
+ raise AssertionError("interval wait must not publish or execute")
+
+ runtime = tmp_path / "staged-values-runtime"
+ monkeypatch.setattr(executor, "stage_turn_decision_capture", observe_stage)
+ monkeypatch.setattr(turn_envelope, "complete_turn_host_source", observe_dispatch)
+ result = executor.run_loopx_turn_once(plan, host_runner=unexpected_effect,
+ project=tmp_path / "project", runtime_root=runtime, goal_id=original["goal_id"],
+ timeout_seconds=5, execute=True, writeback=unexpected_effect,
+ spend=unexpected_effect, scheduler=unexpected_effect, admit_start=deny_start,
+ decision_capture_payload=contract, require_decision_capture=True)
+ assert result["status"] == "interval_wait"
+ assert len(captured) == len(dispatched) == len(admissions) == 1
+ request = dispatched[0]
+ assert request["schema_version"] == "loopx_turn_host_request_v1"
+ assert "turn_envelope" not in request
+ assert request["turn_key"] == plan["transaction"]["turn_key"]
+ assert request["route"] == plan["route"]["kind"]
+ assert request["session"] == plan["session"]
+ assert request["result_contract"] == {"schema_version": "loopx_turn_result_v0",
+ "completed_phases": ["host_execute", "typed_result"], "stdout": "one public-safe JSON object"}
+ assert request["goal_ref"] == plan["goal_ref"] and request["goal_ref"] is not plan["goal_ref"]
+ for field in ("delegation_context", "child_operations", "subagent_execution_topology"):
+ assert request[field] == plan[field]
+ assert request.get("turn_instance_id") == (None if child_limit is True else "staged-request-values")
+ assert json.dumps(request["work_contract"], ensure_ascii=False) == json.dumps(contract, ensure_ascii=False)
+ assert request["source_decision_hash"] == contract["contract_binding"]["content_hash"]
+ capture_ref = request["detail_ref"]["captured_decision"]
+ assert capture_ref["path"] == str(captured[0].path)
+ assert capture_ref["turn_key"] == request["turn_key"]
+ assert capture_ref["source_hash_ref"] == "$.source_decision_hash"
+ assert not captured[0].path.exists()
+ assert not turn_journal_path(runtime, goal_id=original["goal_id"], turn_key=request["turn_key"]).exists()
+
+
+@pytest.mark.parametrize("optional_context", [False, True])
+@pytest.mark.parametrize("failure", ["snapshot_oversized", "transport_permission"])
+def test_staged_capture_preserves_bridge_transport_failure(
+ complete_host_request, tmp_path, monkeypatch, optional_context, failure,
+):
+ from loopx.control_plane import effect_runtime
+ from loopx.control_plane.turn_driver import executor
+ from loopx.control_plane.turn_driver.driver import build_loopx_turn_plan
+
+ request, original = complete_host_request
+ plan = build_loopx_turn_plan(request["turn_envelope"], work_contract=original,
+ host="generic-cli", execution_mode="isolated-headless",
+ turn_instance_id="staged-transport-failure")
+ original_carrier = json.dumps(plan, ensure_ascii=False)
+ original_stage = executor.stage_turn_decision_capture
+ original_request = effect_runtime.effect_runtime_request
+ captures, operations = [], []
+
+ def observe_request(method, params, **kwargs):
+ operations.append((method, params.get("operation"), kwargs.get("large_local_snapshot", False)))
+ return original_request(method, params, **kwargs)
+
+ def denied_connection(*_args, **_kwargs):
+ raise PermissionError("synthetic transport denial")
+
+ def stage_then_fail_transport(*args, **kwargs):
+ capture = original_stage(*args, **kwargs)
+ captures.append(capture)
+ if optional_context:
+ capture.plan["turn_context_contributions"] = [{
+ "hook_id": "synthetic-context", "readback_slots": [],
+ }]
+ monkeypatch.setattr(effect_runtime, "effect_runtime_request", observe_request)
+ if failure == "snapshot_oversized":
+ # Exercise the existing encoder and offloaded-snapshot fallback
+ # with bounded local limits, without allocating a huge fixture.
+ monkeypatch.setattr(effect_runtime, "MAX_REQUEST_BYTES", 1024)
+ monkeypatch.setattr(effect_runtime, "MAX_LOCAL_SNAPSHOT_BYTES", 512)
+ else:
+ monkeypatch.setattr(effect_runtime.socket, "create_connection", denied_connection)
+ return capture
+
+ def unexpected_effect(*_args, **_kwargs):
+ raise AssertionError("transport refusal must precede execution effects")
+
+ runtime = tmp_path / "staged-transport-runtime"
+ monkeypatch.setattr(executor, "stage_turn_decision_capture", stage_then_fail_transport)
+ with pytest.raises((effect_runtime.EffectRuntimeRejected, effect_runtime.EffectRuntimeHostPermissionError)) as refused:
+ executor.run_loopx_turn_once(plan, host_runner=unexpected_effect,
+ project=tmp_path / "project", runtime_root=runtime, goal_id=original["goal_id"],
+ timeout_seconds=5, execute=True, writeback=unexpected_effect,
+ spend=unexpected_effect, scheduler=unexpected_effect, admit_start=unexpected_effect,
+ decision_capture_payload=original, require_decision_capture=True)
+ if failure == "snapshot_oversized":
+ assert refused.value.diagnostic_code == "request_too_large"
+ assert str(refused.value) == "TypeScript Effect runtime local snapshot is oversized"
+ assert [item[2] for item in operations] == ([False, True] * (2 if optional_context else 1))
+ else:
+ assert refused.value.diagnostic_code == "runtime_host_permission_denied"
+ assert str(refused.value) == ("Host permission denied access to the local TypeScript Effect runtime "
+ "before request dispatch; no capability operation was executed")
+ assert [item[2] for item in operations] == ([False] * (2 if optional_context else 1))
+ if optional_context:
+ assert operations[0][:2] == ("quota.turn_envelope.evaluate", "host_context_binding")
+ assert operations[-1][0] == "quota.turn_envelope.evaluate"
+ assert operations[-1][1] in {"host_context", "host_dispatch_context"}
+ assert len(captures) == 1 and not captures[0].path.exists()
+ assert not turn_journal_path(runtime, goal_id=original["goal_id"], turn_key=request["turn_key"]).exists()
+ assert json.dumps(plan, ensure_ascii=False) == original_carrier
+ assert json.dumps(captures[0].observation, ensure_ascii=False) == json.dumps(original, ensure_ascii=False)
+ assert not list(runtime.rglob("*.jsonl"))
+
+
def test_typed_result_recovery_does_not_start_a_pending_goal_host(
complete_host_request, tmp_path,
):
diff --git a/tests/control_plane_ts/action_review_plan_session.test.ts b/tests/control_plane_ts/action_review_plan_session.test.ts
new file mode 100644
index 0000000000..6691ce9110
--- /dev/null
+++ b/tests/control_plane_ts/action_review_plan_session.test.ts
@@ -0,0 +1,99 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+import {compileActionReviewPlan} from "../../loopx/control_plane/presentation/action_review_plan.ts";
+
+const proposal = {proposal_id: "new-goal", expected_state_fingerprint: "reviewed-source",
+ action_kind: "goal.create", permission_classification: "durable_write", status: "preview_ready",
+ normalized_parameters: {goal_id: "new-goal", title: "New Goal", agent_id: "codex",
+ initial_todos: ["Deliver the original requested work"]}};
+const steps = {workspace_validated: {outcome: "workspace_validated", workspace_digest: "a".repeat(64)},
+ goal_bootstrapped: {outcome: "goal_bootstrapped", goal_id: "new-goal"},
+ agent_bound: {outcome: "agent_bound", goal_id: "new-goal", agent_id: "codex"},
+ todos_created: {outcome: "todos_created", todo_ids: ["todo-original"]}};
+
+test("a new default Codex Goal chooses managed Agent routing before its first Session", () => {
+ for (const checkpoint of [undefined, null, {steps: {}}, {steps}]) {
+ const plan = compileActionReviewPlan({...proposal, checkpoint});
+ assert.deepEqual(plan.creationSession, {managedAgentId: "codex"});
+ assert.equal(plan.canApply, true);
+ assert.equal(plan.creationProgress?.sessionId, undefined);
+ assert.equal(plan.creationProgress?.firstTurn, undefined);
+ }
+ assert.deepEqual(compileActionReviewPlan({...proposal, normalized_parameters: {
+ ...proposal.normalized_parameters, native_token_budget: null}}).creationSession, {managedAgentId: "codex"});
+});
+
+test("explicit budgets and other endpoints preserve the legacy first Session route", () => {
+ // Invalid explicit budget shapes are rejected upstream; a pure projection
+ // must not turn them into a managed default through truthiness.
+ for (const native_token_budget of [1, 10000, 0, false, "", "10000", {}, []]) {
+ assert.equal(Object.hasOwn(compileActionReviewPlan({...proposal, normalized_parameters: {
+ ...proposal.normalized_parameters, native_token_budget}}), "creationSession"), false);
+ }
+ for (const agent_id of ["antigravity", "claude-code", "other-codex", "codex@worker", " codex ", "", null, undefined]) {
+ assert.equal(Object.hasOwn(compileActionReviewPlan({...proposal, normalized_parameters: {
+ ...proposal.normalized_parameters, agent_id}}), "creationSession"), false);
+ }
+ assert.equal(compileActionReviewPlan({...proposal, action_kind: "todo.create"}).creationSession, undefined);
+});
+
+test("bootstrap retry chooses a new route only while no Session has been recorded", () => {
+ for (const status of ["failed", "applying"]) {
+ const plan = compileActionReviewPlan({...proposal, status, checkpoint: {steps}});
+ assert.equal(plan.retryOriginal, true);
+ assert.deepEqual(plan.creationSession, {managedAgentId: "codex"});
+ assert.deepEqual(plan.creationProgress?.todoIds, ["todo-original"]);
+ }
+});
+
+test("recorded legacy Session and first Turn retain their original recovery identity", () => {
+ const first_session_opened = {outcome: "first_session_opened", session_id: "original-session"};
+ const first_turn_started = {outcome: "first_turn_started", session_id: "original-session", turn_id: "original-turn"};
+ for (const recorded of [{first_session_opened}, {first_turn_started}, {first_session_opened, first_turn_started}]) {
+ const plan = compileActionReviewPlan({...proposal, status: "failed", checkpoint: {steps: {...steps, ...recorded}}});
+ assert.equal(plan.retryOriginal, true);
+ assert.equal(Object.hasOwn(plan, "creationSession"), false);
+ if ("first_session_opened" in recorded) assert.equal(plan.creationProgress?.sessionId, "original-session");
+ if ("first_turn_started" in recorded)
+ assert.deepEqual(plan.creationProgress?.firstTurn, {sessionId: "original-session", turnId: "original-turn"});
+ }
+});
+
+test("malformed recorded checkpoints cannot be treated as a new default Session", () => {
+ for (const value of [undefined, null, false, [], {}, {session_id: "original-session"}]) {
+ for (const field of ["first_session_opened", "first_turn_started"]) {
+ const plan = compileActionReviewPlan({...proposal, status: "failed", checkpoint: {steps: {...steps, [field]: value}}});
+ assert.equal(Object.hasOwn(plan, "creationSession"), false);
+ }
+ }
+ for (const checkpoint of [[], false, {}, {steps: null}, {steps: []},
+ {steps: {...steps, goal_bootstrapped: {...steps.goal_bootstrapped, goal_id: "other"}}},
+ {steps: {...steps, first_session_opened: {outcome: "first_session_opened", session_id: "original-session"},
+ first_turn_started: {outcome: "first_turn_started", session_id: "other-session", turn_id: "turn"}}}]) {
+ assert.equal(compileActionReviewPlan({...proposal, status: "failed", checkpoint}).creationSession, undefined);
+ }
+});
+
+test("terminal or persisted first-Turn readback does not advertise another Session", () => {
+ assert.equal(compileActionReviewPlan({...proposal, status: "applied", receipt: {projection_verified: true}}).creationSession, undefined);
+ for (const first_turn_readback of [null, {}, {status: "queued"},
+ {status: "failed", completed_at: "2026-01-01T00:00:01Z", started_at: null, upstream_turn_id: null}]) {
+ assert.equal(compileActionReviewPlan({...proposal, first_turn_readback}).creationSession, undefined);
+ }
+});
+
+test("managed routing leaves requirement retention and action authority independent", () => {
+ const input = {...proposal, normalized_parameters: {...proposal.normalized_parameters,
+ completion_criteria: "Keep the original acceptance boundary"}, checkpoint: {steps}};
+ const plan = compileActionReviewPlan(input);
+ assert.deepEqual(plan.creationSession, {managedAgentId: "codex"});
+ assert.deepEqual(plan.creationOutcome?.refinement.requirements,
+ [{id: "completion", description: "Keep the original acceptance boundary"}]);
+ assert.equal(Object.hasOwn(plan, "taskExecution"), false);
+ assert.equal(Object.hasOwn(plan, "taskAccepted"), false);
+ for (const status of ["gated", "stale", "rejected"]) {
+ const held = compileActionReviewPlan({...input, status});
+ assert.equal(held.canApply, false);
+ assert.equal(held.retryOriginal, undefined);
+ }
+});
diff --git a/tests/control_plane_ts/coordination_runtime_shadow.test.ts b/tests/control_plane_ts/coordination_runtime_shadow.test.ts
index c59a4bd26c..2050a0a27e 100644
--- a/tests/control_plane_ts/coordination_runtime_shadow.test.ts
+++ b/tests/control_plane_ts/coordination_runtime_shadow.test.ts
@@ -377,7 +377,9 @@ for (const scenario of [
]),
], {encoding: "utf8", timeout: 60_000});
assert.equal(result.status, 0, result.stderr);
- const observed = JSON.parse(result.stdout.trim().split("\n").at(-1));
+ const resultLine = result.stdout.trim().split("\n").at(-1);
+ assert.ok(resultLine, "the source lease probe must report its observation");
+ const observed = JSON.parse(resultLine);
assert.equal(observed.status === "failed" ? observed.reason_code : observed.status, scenario.expected);
const passes = scenario.expected === "applied" ? 2 : 1;
assert.equal(observed.lstatCalls, 2 * passes);
diff --git a/tests/control_plane_ts/goal_creation_execution_review.test.ts b/tests/control_plane_ts/goal_creation_execution_review.test.ts
new file mode 100644
index 0000000000..0806702f84
--- /dev/null
+++ b/tests/control_plane_ts/goal_creation_execution_review.test.ts
@@ -0,0 +1,29 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+import {goalCreationExecutionReview, normalizeGoalCreationExecution} from "../../loopx/control_plane/goals/creation_execution_review.ts";
+
+const original = () => ({agent_id: "codex", start_execution: true, permission: "read_only",
+ completion_criteria: "The existing independent check accepts the requested result.", initial_todos: ["Complete the original work."]});
+
+test("old creation and explicit no-start retain no executable request", () => {
+ assert.deepEqual(normalizeGoalCreationExecution({agent_id: "codex"}), {});
+ assert.equal(goalCreationExecutionReview({permission: "read_only"}), undefined);
+ assert.equal(goalCreationExecutionReview({...original(), start_execution: false}), undefined);
+});
+
+test("an explicit read-only request starts work without granting writes", () => {
+ assert.deepEqual(goalCreationExecutionReview(original()), {requested: true, permission: "read_only", writeScope: []});
+});
+
+test("only the existing explicit workspace-write review selects the whole workspace", () => {
+ assert.deepEqual(goalCreationExecutionReview({...original(), permission: "workspace_write_on_confirmation"}),
+ {requested: true, permission: "workspace_write_on_confirmation", writeScope: ["**"]});
+});
+
+for (const changed of [{permission: "write"}, {permission: ""}, {permission: null}, {start_execution: "true"},
+ {agent_id: "claude-code"}, {native_token_budget: 1000}, {initial_todos: []}, {initial_todos: ["one", "two"]},
+ {completion_criteria: ""}]) {
+ test(`invalid or competing creation intent ${JSON.stringify(changed)} is rejected before creation`, () => {
+ assert.throws(() => normalizeGoalCreationExecution({...original(), ...changed}));
+ });
+}
diff --git a/tests/control_plane_ts/original_creation_task_selection.test.ts b/tests/control_plane_ts/original_creation_task_selection.test.ts
new file mode 100644
index 0000000000..5076fed28a
--- /dev/null
+++ b/tests/control_plane_ts/original_creation_task_selection.test.ts
@@ -0,0 +1,93 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+import {originalCreationTaskSelection} from "../../loopx/control_plane/goals/task_validation_plan.ts";
+
+import {canonicalNativeTaskSandboxPolicy} from "../../loopx/control_plane/collaboration/steward_managed_mode.ts";
+
+const basis = (outcomes: unknown[]) => ({status: "loaded", provider_revision: "current",
+ goal_acceptance_contract: {outcome_requirements: {outcomes}}});
+
+test("original Task comes from retained creation work, independently of later outcomes", () => {
+ assert.deepEqual(originalCreationTaskSelection({basis: basis([
+ {id: "later", todo_ids: ["newest"]}, {id: "goal-creation", todo_ids: ["original"]},
+ ])}), {todo_id: "original", reason: null});
+});
+
+for (const [name, outcomes] of [
+ ["missing origin", [{id: "later", todo_ids: ["newest"]}]],
+ ["multiple initial Tasks", [{id: "goal-creation", todo_ids: ["first", "second"]}]],
+ ["removed work", [{id: "goal-creation", todo_ids: []}]],
+ ["duplicated origin", [{id: "goal-creation", todo_ids: ["one"]}, {id: "goal-creation", todo_ids: ["two"]}]],
+] as const) test(`${name} never selects the first or newest Task`, () => {
+ assert.equal(originalCreationTaskSelection({basis: basis([...outcomes])}).todo_id, null);
+});
+
+test("unavailable canonical authority is not a display selection", () => {
+ assert.throws(() => originalCreationTaskSelection({basis: {...basis([]), status: "unavailable"}}),
+ /current Goal acceptance basis/);
+});
+
+const policyInput = () => ({host: "codex-app-server", actor: {schema_version: "loopx_managed_session_actor_v0", goal_id: "goal",
+ agent_id: "agent", workspace_path: "/exercise"}, validation: {todo_id: "original", state: "ready"},
+task_lease: {schema_version: "task_lease_v0", status: "active", goal_id: "goal", todo_id: "original",
+ owner: "agent", version: 1, write_scopes: [] as string[]},
+authority: {_work_identity: {goal_id: "goal", agent_id: "agent", todo_id: "original"},
+ execution_preparation: {todo_id: "original", agent_id: "agent", state: "ready", delivery_allowed: true},
+ should_run: true, normal_delivery_allowed: true, goal_boundary: {}}});
+
+test("a native Task or its confirmed checks do not grant workspace writes", () => {
+ assert.deepEqual(canonicalNativeTaskSandboxPolicy(policyInput()), {type: "readOnly", networkAccess: false});
+});
+
+test("workspace approval cannot widen the current Task lease", () => {
+ const input = policyInput();
+ input.authority.goal_boundary = {checkpointed_boundary_authority: {
+ schema_version: "checkpointed_boundary_authority_v0", active_count: 1, active_write_scope: ["**"]}};
+ input.task_lease.write_scopes = ["cart.py"];
+ assert.deepEqual(canonicalNativeTaskSandboxPolicy(input), {type: "readOnly", networkAccess: false});
+ input.task_lease.todo_id = "other";
+ assert.throws(() => canonicalNativeTaskSandboxPolicy(input), /same current execution lease/);
+});
+
+for (const scope of ["cart.py", "outputs/**"]) test(`narrow ${scope} approval cannot grant the whole native workspace`, () => {
+ const input = policyInput();
+ input.authority.goal_boundary = {checkpointed_boundary_authority: {
+ schema_version: "checkpointed_boundary_authority_v0", active_count: 1, active_write_scope: [scope]}};
+ assert.deepEqual(canonicalNativeTaskSandboxPolicy(input), {type: "readOnly", networkAccess: false});
+});
+
+test("the original admitted Task uses the already approved workspace with network and temp writes disabled", () => {
+ const input = policyInput();
+ input.authority.goal_boundary = {checkpointed_boundary_authority: {
+ schema_version: "checkpointed_boundary_authority_v0", active_count: 1, active_write_scope: ["**"]}};
+ const expected = {type: "workspaceWrite", writableRoots: ["/exercise"], networkAccess: false,
+ excludeTmpdirEnvVar: true, excludeSlashTmp: true};
+ assert.deepEqual(canonicalNativeTaskSandboxPolicy(input), expected);
+ input.task_lease.write_scopes = ["**"];
+ assert.deepEqual(canonicalNativeTaskSandboxPolicy(input), expected);
+});
+
+for (const scope of ["src/*.py", "src/[ab].py", "../outside/**", "/outside/file"]) {
+ test(`unrepresentable ${scope} approval is never converted to a broader directory`, () => {
+ const input = policyInput();
+ input.authority.goal_boundary = {checkpointed_boundary_authority: {
+ schema_version: "checkpointed_boundary_authority_v0", active_count: 1, active_write_scope: [scope]}};
+ assert.deepEqual(canonicalNativeTaskSandboxPolicy(input), {type: "readOnly", networkAccess: false});
+ });
+}
+
+for (const count of [undefined, 0]) test(`an approval without an active count of ${count} grants no workspace writes`, () => {
+ const input = policyInput();
+ input.authority.goal_boundary = {checkpointed_boundary_authority: {
+ schema_version: "checkpointed_boundary_authority_v0", active_count: count, active_write_scope: ["**"]}};
+ assert.deepEqual(canonicalNativeTaskSandboxPolicy(input), {type: "readOnly", networkAccess: false});
+});
+
+test("Task substitution and an unavailable execution lease cannot yield host permissions", () => {
+ const substituted = policyInput();
+ substituted.authority._work_identity.todo_id = "other";
+ assert.throws(() => canonicalNativeTaskSandboxPolicy(substituted), /original admitted Task/);
+ const unavailable = policyInput();
+ unavailable.authority.execution_preparation.state = "unavailable";
+ assert.throws(() => canonicalNativeTaskSandboxPolicy(unavailable), /execution lease/);
+});
diff --git a/tests/control_plane_ts/postgresql_authority_seed.test.ts b/tests/control_plane_ts/postgresql_authority_seed.test.ts
new file mode 100644
index 0000000000..5c3fb39c88
--- /dev/null
+++ b/tests/control_plane_ts/postgresql_authority_seed.test.ts
@@ -0,0 +1,255 @@
+import assert from "node:assert/strict";
+import { mkdtemp, readFile, rm } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import test from "node:test";
+import {
+ exportLocalToPgGenesisSeed,
+ sanitizeGenesisSeedProjection,
+ scanForbiddenPrivacyTokens,
+ setupPostgreSqlAuthoritySchemaAndRole,
+} from "../../loopx/control_plane/coordination/postgresql_authority_seed.ts";
+import { FileAuthorityStore } from "../../loopx/control_plane/coordination/file_authority_store.ts";
+import type {PostgreSqlAuthorityDatabase, PostgreSqlAuthorityConnection} from "../../loopx/control_plane/coordination/postgresql_authority_store.ts";
+
+test("sanitizeGenesisSeedProjection strips expired leases", () => {
+ const past = new Date(Date.now() - 3600_000).toISOString();
+ const future = new Date(Date.now() + 3600_000).toISOString();
+
+ const projection = {
+ schema_version: "loopx_authority_projection_v0",
+ goal_id: "test-goal",
+ task_leases: {
+ leases: {
+ "lease-expired": {
+ lease_id: "lease-expired",
+ holder: "agent-1",
+ expires_at: past,
+ },
+ "lease-active": {
+ lease_id: "lease-active",
+ holder: "agent-2",
+ expires_at: future,
+ },
+ },
+ },
+ };
+
+ const { projection: sanitized, report } = sanitizeGenesisSeedProjection(projection);
+ const leases = (sanitized.task_leases as { leases?: Record })?.leases ?? {};
+ assert.equal(leases["lease-expired"], undefined);
+ assert.ok(leases["lease-active"] !== undefined);
+ assert.equal(report.leases_stripped, 1);
+});
+
+test("sanitizeGenesisSeedProjection redacts local paths and .local references", () => {
+ const projection = {
+ schema_version: "loopx_authority_projection_v0",
+ goal_id: "test-goal",
+ runtime_root: "/Users/alice/Developer/loopx/.local/runtime",
+ project_root: "/Users/alice/Developer/my-repo",
+ registered_materials: [
+ {
+ material_id: "mat-1",
+ path: "/Users/alice/Developer/my-repo/docs/readme.md",
+ boundary: "public",
+ },
+ {
+ material_id: "mat-2",
+ path: "/Users/alice/.local/notes/secret.md",
+ boundary: "local_private",
+ },
+ ],
+ };
+
+ const { projection: sanitized, report } = sanitizeGenesisSeedProjection(projection);
+ // .local path should be redacted
+ assert.ok(!JSON.stringify(sanitized).includes("/Users/alice/.local"));
+ assert.ok(!JSON.stringify(sanitized).includes("/Users/alice/Developer"));
+ // private boundary material must be stripped
+ const materials = (sanitized.registered_materials as Array<{ material_id: string }>) ?? [];
+ assert.equal(materials.length, 1);
+ assert.equal(materials[0].material_id, "mat-1");
+ assert.equal(report.private_materials_stripped, 1);
+ assert.ok(report.paths_redacted > 0);
+});
+
+test("sanitizeGenesisSeedProjection redacts Feishu IDs", () => {
+ const feishuUser = "ou_abcdef1234567890abcdef12345678";
+ const feishuChat = "oc_1234567890abcdef1234567890abcd";
+ const projection = {
+ schema_version: "loopx_authority_projection_v0",
+ goal_id: "test-goal",
+ owner_identity: feishuUser,
+ channel: {
+ chat_id: feishuChat,
+ app_id: "cli_a1b2c3d4e5f6g7h8",
+ },
+ };
+
+ const { projection: sanitized, report } = sanitizeGenesisSeedProjection(projection);
+ const serialized = JSON.stringify(sanitized);
+ assert.ok(!serialized.includes(feishuUser));
+ assert.ok(!serialized.includes(feishuChat));
+ assert.ok(!serialized.includes("cli_a1b2c3d4e5f6g7h8"));
+ assert.ok(serialized.includes("[feishu_id_redacted]"));
+ assert.ok(report.feishu_ids_redacted >= 3);
+});
+
+test("scanForbiddenPrivacyTokens fails closed on sensitive credentials", () => {
+ const clean = {
+ goal_id: "clean-goal",
+ objective: "Just a clean goal with safe public description",
+ };
+ assert.doesNotThrow(() => scanForbiddenPrivacyTokens(clean));
+
+ const withSecret = {
+ goal_id: "leaky-goal",
+ apiKey: ["sk-", "fixture", "0".repeat(32)].join(""),
+ };
+ assert.throws(() => scanForbiddenPrivacyTokens(withSecret), /Privacy boundary violation/);
+
+ const withGithubToken = {
+ token: ["ghp", "fixture".repeat(6)].join("_"),
+ };
+ assert.throws(() => scanForbiddenPrivacyTokens(withGithubToken), /Privacy boundary violation/);
+
+ const privateKey = "ou_123456abcdef";
+ for (let i = 0; i < 2; i++) {
+ assert.throws(() => sanitizeGenesisSeedProjection({[privateKey]: {safe: true}}), error => {
+ assert.ok(error instanceof Error);
+ return /Privacy boundary violation/.test(error.message) && !error.message.includes(privateKey);
+ });
+ }
+ assert.throws(() => sanitizeGenesisSeedProjection({[withGithubToken.token]: 1}), /Privacy boundary violation/);
+ const assignment = ["password", "fixture-only-value"].join("=");
+ assert.throws(() => sanitizeGenesisSeedProjection({payload: assignment}), /Privacy boundary violation/);
+});
+
+test("setupPostgreSqlAuthoritySchemaAndRole installs DDL, RLS, and restricted role", async () => {
+ const executedStatements: string[] = [];
+ let connects = 0;
+ const storeId = "postgresql:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
+ const connection: PostgreSqlAuthorityConnection = {
+ query: async (sql: string, params?: readonly unknown[]) => {
+ executedStatements.push(sql);
+ if (sql.includes("FROM loopx_control_plane.authority_store_metadata")) {
+ return {
+ rows: [{schema_version: "loopx_postgresql_authority_store_v1", store_identity: storeId}],
+ rowCount: 1,
+ };
+ }
+ return {rows: [], rowCount: 1};
+ },
+ release: () => {},
+ };
+
+ const database: PostgreSqlAuthorityDatabase = {
+ connect: async () => {
+ connects += 1;
+ return connection;
+ },
+ };
+
+ const result = await setupPostgreSqlAuthoritySchemaAndRole(database, {
+ storeIdentity: storeId,
+ runtimeRole: "loopx_runtime_worker",
+ });
+
+ assert.equal(result.status, "installed");
+ assert.equal(result.store_identity, "postgresql:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
+ assert.equal(result.runtime_role, "loopx_runtime_worker");
+ assert.equal(connects, 1);
+
+ // Verify key DDL statements were executed
+ assert.ok(executedStatements.some(s => s.includes("CREATE SCHEMA IF NOT EXISTS loopx_control_plane")));
+ assert.ok(executedStatements.some(s => s.includes("CREATE TABLE IF NOT EXISTS loopx_control_plane.authority_checkpoints")));
+ assert.ok(executedStatements.some(s => s.includes("CREATE TABLE IF NOT EXISTS loopx_control_plane.authority_commits")));
+ assert.ok(executedStatements.some(s => s.includes("CREATE ROLE \"loopx_runtime_worker\"")));
+ assert.ok(executedStatements.some(s => s.includes("GRANT USAGE ON SCHEMA loopx_control_plane TO \"loopx_runtime_worker\"")));
+ const grants = executedStatements.findIndex(s =>
+ /GRANT SELECT, INSERT ON\s+loopx_control_plane\.authority_checkpoints/.test(s));
+ assert.ok(grants > executedStatements.indexOf("BEGIN") && grants < executedStatements.indexOf("COMMIT"));
+});
+
+test("exportLocalToPgGenesisSeed exports sanitized cursor=1 genesis seed JSON", async () => {
+ const root = await mkdtemp(join(tmpdir(), "genesis-seed-test-"));
+ try {
+ const goalId = "export-goal";
+ const storePath = join(root, "goals", goalId, "authority");
+ const store = new FileAuthorityStore(storePath, goalId);
+
+ const past = new Date(Date.now() - 3600_000).toISOString();
+ const future = new Date(Date.now() + 3600_000).toISOString();
+
+ const commit1 = await store.commitAuthority({
+ operation_id: "op-1",
+ expected_provider_revision: null,
+ events: [],
+ receipts: [],
+ next_projection: {
+ schema_version: "loopx_authority_projection_v0",
+ goal_id: goalId,
+ leases: [
+ { todo_id: "todo-1", expires_at: past, owner: "agent-old" },
+ { todo_id: "todo-2", expires_at: future, owner: "agent-active" },
+ ],
+ todos: [
+ {
+ id: "todo-1",
+ title: "Task with Feishu ID ou_1234567890abcdef1234567890",
+ required_write_scopes: ["/Users/developer/loopx/packages/core", ".local/temp"],
+ },
+ ],
+ authority_registry: {
+ project_materials: {internal: {boundary: "local_private", purpose: "private synthetic fixture"}},
+ topic_authority: {internal: ["internal"]},
+ },
+ goal_direction_scope: {enabled: true},
+ material_usage_receipts: [{purpose: "private usage witness"}],
+ },
+ });
+ assert.equal(commit1.status, "applied");
+
+ const outputPath = join(root, "genesis-seed.json");
+ const sourceBefore = await store.loadAuthority();
+ let attemptedTargetConnection = false;
+ const result = await exportLocalToPgGenesisSeed({
+ runtimeRoot: root,
+ goalId,
+ localStore: store,
+ outputPath,
+ tenantId: "custom-tenant",
+ targetDatabase: {connect: async () => {
+ attemptedTargetConnection = true;
+ throw new Error("preview must never connect");
+ }},
+ });
+ if (result.status !== "exported") {
+ console.error("Export failed:", result);
+ }
+
+ assert.equal(result.status, "exported");
+ assert.equal(result.cursor, "1");
+ assert.equal(result.goal_id, goalId);
+ assert.equal(result.tenant_id, "custom-tenant");
+ assert.ok(result.seed_digest);
+ assert.equal(result.sanitization_report?.leases_stripped, 1);
+ assert.equal(result.sanitization_report?.private_materials_stripped, 1);
+ assert.equal(attemptedTargetConnection, false);
+ assert.deepEqual(await store.loadAuthority(), sourceBefore);
+
+ const writtenJson = JSON.parse(await readFile(outputPath, "utf8"));
+ assert.equal(writtenJson.schema_version, "loopx_pg_genesis_seed_v0");
+ assert.equal(writtenJson.cursor, "1");
+ assert.equal(writtenJson.projection.leases.length, 1);
+ assert.equal(writtenJson.projection.leases[0].todo_id, "todo-2");
+ assert.ok(!JSON.stringify(writtenJson).includes("ou_1234567890abcdef1234567890"));
+ assert.ok(!JSON.stringify(writtenJson).includes(".local/temp"));
+ assert.equal(writtenJson.projection.authority_registry, undefined);
+ assert.equal(writtenJson.projection.goal_direction_scope, undefined);
+ assert.equal(writtenJson.projection.material_usage_receipts, undefined);
+ } finally {
+ await rm(root, { recursive: true, force: true });
+ }
+});
diff --git a/tests/control_plane_ts/postgresql_authority_store.integration.test.ts b/tests/control_plane_ts/postgresql_authority_store.integration.test.ts
index aac6b5f029..7d5d7d1bbf 100644
--- a/tests/control_plane_ts/postgresql_authority_store.integration.test.ts
+++ b/tests/control_plane_ts/postgresql_authority_store.integration.test.ts
@@ -26,6 +26,8 @@ import {canonicalReadRoot} from "./local_promotion_fixture.ts";
import {coordinationTodoReadModel} from "../../loopx/control_plane/coordination/coordination_projection.ts";
import {inspectGoalAcceptance, refineGoalAcceptanceOutcome} from "../../loopx/control_plane/goals/acceptance_authority.ts";
import {authorityProjectionFixture} from "./authority_projection_fixture.ts";
+import {exportLocalToPgGenesisSeed, setupPostgreSqlAuthoritySchemaAndRole} from "../../loopx/control_plane/coordination/postgresql_authority_seed.ts";
+import {FileAuthorityStore} from "../../loopx/control_plane/coordination/file_authority_store.ts";
import type {JsonObject} from "../../loopx/control_plane/effect_program.ts";
import {
authorityStoreCommitFixture as commit,
@@ -37,6 +39,7 @@ const pool = connectionString ? new Pool({ connectionString, max: 12 }) : null;
const STORE_IDENTITY = `postgresql:${"b".repeat(32)}`;
const TENANT_SCOPED_TABLES = [
"authority_heads",
+ "authority_checkpoints",
"authority_commits",
"authority_events",
"authority_receipts",
@@ -59,20 +62,11 @@ function quotedRole(role: string): string {
}
async function createRuntimeRole(role: string): Promise {
- const identifier = quotedRole(role);
- await pool!.query(`CREATE ROLE ${identifier} NOLOGIN`);
- await pool!.query(`GRANT USAGE ON SCHEMA loopx_control_plane TO ${identifier}`);
- await pool!.query(
- `GRANT SELECT ON loopx_control_plane.authority_store_metadata TO ${identifier}`,
- );
- await pool!.query(
- `GRANT SELECT, INSERT, UPDATE ON loopx_control_plane.authority_heads TO ${identifier}`,
- );
- await pool!.query(
- `GRANT SELECT, INSERT ON loopx_control_plane.authority_commits,
- loopx_control_plane.authority_events,
- loopx_control_plane.authority_receipts TO ${identifier}`,
- );
+ const result = await setupPostgreSqlAuthoritySchemaAndRole(database!, {
+ storeIdentity: STORE_IDENTITY,
+ runtimeRole: role,
+ });
+ assert.equal(result.status, "installed", result.reason);
}
function databaseForRuntimeRole(value: Pool, role: string): PostgreSqlAuthorityDatabase {
@@ -690,10 +684,53 @@ if (database && installed) {
tenant_id: secondTenant,
goal_id: goalId,
});
- assert.equal((await first.commitAuthority(commit(null, "operation-a", 1, 1))).status, "applied");
+ const root = await mkdtemp(join(tmpdir(), "pg-genesis-source-"));
+ t.after(() => rm(root, {recursive: true, force: true}));
+ const source = new FileAuthorityStore(root, goalId);
+ const synthetic = {goal_id: goalId, counter: 1, detail: "stable fixture"};
+ assert.equal((await source.commitAuthority({
+ operation_id: "local-fixture", expected_provider_revision: null,
+ events: [], receipts: [], next_projection: synthetic,
+ })).status, "applied");
+ const seed = {runtimeRoot: root, goalId, tenantId: firstTenant, localStore: source,
+ outputPath: join(root, "seed.json"),
+ storeIdentity: STORE_IDENTITY, targetDatabase: runtimeDatabase, execute: true};
+ const genesis = await exportLocalToPgGenesisSeed(seed);
+ assert.equal(genesis.status, "seeded", genesis.reason);
+ const originalSeedReceipt = await first.readReceipt(genesis.operation_id);
+ assert.equal(originalSeedReceipt.status, "found");
+ const retried = await exportLocalToPgGenesisSeed(seed);
+ assert.equal(retried.status, "seeded", retried.reason);
+ assert.equal(retried.provider_revision, genesis.provider_revision);
+ assert.equal(retried.seed_digest, genesis.seed_digest);
+ assert.deepEqual(await first.readReceipt(genesis.operation_id), originalSeedReceipt);
+ assert.equal((await exportLocalToPgGenesisSeed({...seed, storeIdentity: `postgresql:${"d".repeat(32)}`})).status, "failed");
assert.equal((await second.commitAuthority(commit(null, "operation-b", 1, 1))).status, "applied");
- assert.equal((await first.loadAuthority()).status, "loaded");
assert.equal((await second.readReceipt("operation-b")).status, "found");
+ let revision = genesis.provider_revision;
+ for (let cursor = 2; cursor <= 67; cursor++) {
+ const result = await first.commitAuthority({
+ expected_provider_revision: revision,
+ operation_id: `append-${cursor}`,
+ events: [{counter: cursor}],
+ receipts: [{verified_cursor: cursor}],
+ next_projection: {...synthetic, counter: cursor},
+ });
+ assert.equal(result.status, "applied", JSON.stringify(result));
+ if (result.status === "applied") revision = result.provider_revision;
+ }
+ const page = await first.scanCommitted("62", 4);
+ assert.equal(page.status, "page");
+ if (page.status !== "page") assert.fail(JSON.stringify(page));
+ assert.deepEqual(page.transactions.map(row => row.projection.counter), [63, 64, 65, 66]);
+ assert.deepEqual(page.transactions[2]?.events, [{counter: 65}]);
+ assert.deepEqual(page.transactions[2]?.receipts, [{verified_cursor: 65}]);
+ assert.equal(page.has_more, true);
+ assert.equal((await first.readReceipt("append-65")).status, "found");
+ const original = await first.readReceipt("append-65");
+ assert.deepEqual(await first.readReceipts(["append-65", "operation-b", "append-65"]), {
+ status: "receipts", results: [original, {status: "missing"}, original],
+ });
await queryAsRuntimeRole(runtimePool, role, async (client) => {
for (const table of TENANT_SCOPED_TABLES) {
@@ -716,6 +753,14 @@ if (database && installed) {
}
await client.query("ROLLBACK");
+ for (const statement of [
+ "UPDATE loopx_control_plane.authority_checkpoints SET projection = '{}'::jsonb",
+ "DELETE FROM loopx_control_plane.authority_checkpoints",
+ "UPDATE loopx_control_plane.authority_commits SET projection_digest = ''",
+ "DELETE FROM loopx_control_plane.authority_commits",
+ "CREATE TABLE loopx_control_plane.forbidden (id text)",
+ ]) await assert.rejects(client.query(statement), /permission denied/);
+
await client.query("BEGIN");
await client.query(
"SELECT set_config('loopx.tenant_id', $1, TRUE)",
@@ -742,6 +787,100 @@ if (database && installed) {
);
});
});
+
+ test("PostgreSQL v0 upgrade preserves restricted history access and rejects corrupt prefixes", async t => {
+ const suffix = randomUUID().replaceAll("-", "");
+ const name = `upgrade_${suffix}`;
+ const role = `worker_${suffix}`;
+ const grantRole = `journal_${suffix}`;
+ const url = new URL(connectionString!);
+ url.pathname = `/${name}`;
+ const legacy = new Pool({connectionString: url.href});
+ await pool!.query(`CREATE DATABASE ${name}`);
+ t.after(async () => {
+ await legacy.end();
+ await pool!.query(`DROP DATABASE ${name} WITH (FORCE)`);
+ await pool!.query(`DROP ROLE IF EXISTS ${quotedRole(role)}, ${quotedRole(grantRole)}`);
+ });
+ await legacy.query(POSTGRESQL_AUTHORITY_STORE_SCHEMA_SQL);
+ // Restore the released v0 shape in the disposable database. There are no
+ // v1 rows or checkpoints at this point.
+ await legacy.query(`
+ DROP TABLE loopx_control_plane.authority_checkpoints;
+ ALTER TABLE loopx_control_plane.authority_commits
+ DROP COLUMN delta, DROP COLUMN projection_digest, ALTER COLUMN projection SET NOT NULL;
+ INSERT INTO loopx_control_plane.authority_store_metadata (schema_version, store_identity)
+ VALUES ('loopx_postgresql_authority_store_v0', '${STORE_IDENTITY}');
+ CREATE ROLE ${quotedRole(grantRole)} NOLOGIN;
+ CREATE ROLE ${quotedRole(role)} NOLOGIN;
+ GRANT ${quotedRole(grantRole)} TO ${quotedRole(role)};
+ GRANT USAGE ON SCHEMA loopx_control_plane TO ${quotedRole(role)};
+ GRANT SELECT ON loopx_control_plane.authority_store_metadata TO ${quotedRole(role)};
+ GRANT SELECT, INSERT, UPDATE ON loopx_control_plane.authority_heads TO ${quotedRole(role)};
+ GRANT SELECT, INSERT ON loopx_control_plane.authority_commits TO ${quotedRole(grantRole)};
+ GRANT SELECT, INSERT ON loopx_control_plane.authority_events,
+ loopx_control_plane.authority_receipts TO ${quotedRole(role)};
+ `);
+ const tenantId = `tenant-${randomUUID()}`;
+ const goalId = `goal-${randomUUID()}`;
+ const store = new PostgreSqlAuthorityStore(databaseForRuntimeRole(legacy, role), {tenant_id: tenantId, goal_id: goalId});
+ const projection = (counter: number) => ({goal_id: goalId, counter});
+ const scope = [tenantId, goalId];
+ await legacy.query(
+ `INSERT INTO loopx_control_plane.authority_heads (tenant_id, goal_id, provider_revision, cursor, head)
+ VALUES ($1, $2, 2, 2, $3)`, [...scope, projection(2)]);
+ for (const cursor of [1, 2]) {
+ await legacy.query(
+ `INSERT INTO loopx_control_plane.authority_commits
+ (tenant_id, goal_id, cursor, provider_revision, operation_id, projection)
+ VALUES ($1, $2, $3, $3, $4, $5)`, [...scope, cursor, `legacy-${cursor}`, projection(cursor)]);
+ }
+ await legacy.query(
+ `INSERT INTO loopx_control_plane.authority_receipts (tenant_id, goal_id, cursor, receipt_index, receipt)
+ VALUES ($1, $2, 2, 0, $3)`, [...scope, {before_upgrade: true}]);
+ const upgraded = await setupPostgreSqlAuthoritySchemaAndRole(databaseFromPool(legacy), {storeIdentity: STORE_IDENTITY});
+ assert.equal(upgraded.status, "installed", upgraded.reason);
+ const original = await store.readReceipt("legacy-2");
+ assert.equal(original.status, "found");
+ if (original.status !== "found") assert.fail(JSON.stringify(original));
+ assert.equal(original.cursor, "2");
+ assert.equal(original.provider_revision, `${STORE_IDENTITY}:2`);
+ assert.deepEqual(original.receipts, [{before_upgrade: true}]);
+ await queryAsRuntimeRole(legacy, role, async (client) => {
+ const result = await client.query(`
+ SELECT
+ has_table_privilege('loopx_control_plane.authority_checkpoints', 'SELECT') AS can_read,
+ has_table_privilege('loopx_control_plane.authority_checkpoints', 'INSERT') AS can_append,
+ has_table_privilege('loopx_control_plane.authority_checkpoints', 'UPDATE,DELETE') AS can_mutate
+ `);
+ assert.deepEqual(result.rows, [{can_read: true, can_append: true, can_mutate: false}]);
+ });
+ let revision = `${STORE_IDENTITY}:2`;
+ for (const cursor of [3, 4, 5]) {
+ const result = await store.commitAuthority({operation_id: `new-${cursor}`,
+ expected_provider_revision: revision, next_projection: projection(cursor),
+ events: [], receipts: [{after_upgrade: cursor}]});
+ if (result.status !== "applied") assert.fail(JSON.stringify(result));
+ revision = result.provider_revision;
+ }
+ assert.deepEqual(await store.readReceipt("legacy-2"), original);
+ const page = await store.scanCommitted("1", 3);
+ if (page.status !== "page") assert.fail(JSON.stringify(page));
+ assert.deepEqual(page.transactions.map(row => row.projection), [2, 3, 4].map(projection));
+
+ const tamper = {schema_version: "authority_state_delta_v0", operations: [{op: "set", path: ["counter"], value: 999}]};
+ await legacy.query(
+ `UPDATE loopx_control_plane.authority_commits SET delta=$3
+ WHERE tenant_id=$1 AND goal_id=$2 AND cursor=3`, [...scope, tamper]);
+ const corrupt = await store.scanCommitted("3", 1);
+ assert.equal(corrupt.status, "failed");
+ assert.match("reason" in corrupt ? corrupt.reason : "", /digest mismatch/);
+ await legacy.query(
+ "DELETE FROM loopx_control_plane.authority_commits WHERE tenant_id=$1 AND goal_id=$2 AND cursor=3", scope);
+ const missing = await store.scanCommitted("3", 1);
+ assert.equal(missing.status, "failed");
+ assert.match("reason" in missing ? missing.reason : "", /window is incomplete/);
+ });
} else {
test("PostgreSQL authority-store integration (set LOOPX_TEST_POSTGRES_URL)", { skip: true }, () => {});
}
@@ -836,7 +975,7 @@ test("PostgreSQL provider evicts and awaits cleanup-uncertain read connections",
}
if (text.includes("authority_store_metadata")) {
return {
- rows: [{ schema_version: "loopx_postgresql_authority_store_v0", store_identity: STORE_IDENTITY }],
+ rows: [{ schema_version: "loopx_postgresql_authority_store_v1", store_identity: STORE_IDENTITY }],
rowCount: 1,
};
}
diff --git a/tests/control_plane_ts/quota_spend_commit.test.ts b/tests/control_plane_ts/quota_spend_commit.test.ts
index 553c25ed95..dac67a0493 100644
--- a/tests/control_plane_ts/quota_spend_commit.test.ts
+++ b/tests/control_plane_ts/quota_spend_commit.test.ts
@@ -23,6 +23,7 @@ import {
EffectRuntimeRequestError,
} from "../../loopx/control_plane/effect_runtime_errors.ts";
import { requireJsonObject } from "../../loopx/control_plane/runtime_decode.ts";
+import { settlementIdentityPayload } from "../../loopx/control_plane/effect_program.ts";
import {
parseQuotaAccountingOwner,
withQuotaAccountingOwner,
@@ -1087,6 +1088,52 @@ test("effect identity and index CAS reject drift and racing writers", async (t)
assert.equal(fresh.status, "written");
});
+test("concurrent settlement retries share one debit across quota observation changes", async (t) => {
+ const runtimeRoot = await tempRuntime(t);
+ const identity = settlementIdentityPayload({
+ goal_id: goalId, agent_id: "codex-main-control", todo_id: "todo_original",
+ turn_instance_id: "turn-original",
+ });
+ const stable = {
+ settlement_identity: identity,
+ turn_instance_id: identity.turn_instance_id,
+ todo_id: identity.todo_id,
+ delivery_completion_spend: true,
+ delivery_run_generated_at: "2026-08-25T11:59:00+08:00",
+ delivery_run_classification: "validated_progress",
+ delivery_run_agent_id: identity.agent_id,
+ delivery_workspace: {workspace_identity: "git:github.com/example/fixture"},
+ delivery_workspace_validated: true,
+ };
+ const spendRequest = (spentSlots: number, slots = 1) => request(runtimeRoot, {
+ effect_id: null,
+ preview: preview({...stable, slots, would_throttle: spentSlots > 0}),
+ before: decision(spentSlots),
+ after: decision(spentSlots + slots),
+ });
+ const outcomes = await Promise.all([
+ evaluateQuotaSpendCommit(spendRequest(0)),
+ evaluateQuotaSpendCommit(spendRequest(1)),
+ ]);
+ assert.deepEqual(outcomes.map((result) => result.status).sort(), ["replayed", "written"]);
+ const committed = outcomes.find((result) => result.written)!;
+ const indexPath = String(committed.payload.index_path);
+ const originalRows = await readFile(indexPath, "utf8");
+ assert.equal(originalRows.trim().split("\n").length, 1);
+ assert.deepEqual(outcomes[0]?.record, outcomes[1]?.record);
+
+ const changedSpend = await evaluateQuotaSpendCommit(spendRequest(1, 2));
+ assert.equal(changedSpend.reason_code, "effect_id_conflict");
+ const changedSource = await evaluateQuotaSpendCommit({...spendRequest(1), source: "visible-goal"});
+ assert.equal(changedSource.reason_code, "effect_id_conflict");
+ const replacedWriteback = await evaluateQuotaSpendCommit({
+ ...spendRequest(1),
+ preview: preview({...stable, delivery_run_generated_at: "2026-08-25T11:58:00+08:00"}),
+ });
+ assert.equal(replacedWriteback.reason_code, "effect_id_conflict");
+ assert.equal(await readFile(indexPath, "utf8"), originalRows);
+});
+
test("concurrent exact-effect retries serialize to one spend", async (t) => {
const runtimeRoot = await tempRuntime(t);
const params = request(runtimeRoot);
diff --git a/tests/control_plane_ts/remote_authority.integration.test.ts b/tests/control_plane_ts/remote_authority.integration.test.ts
index 7d864c37b0..23c1cf5205 100644
--- a/tests/control_plane_ts/remote_authority.integration.test.ts
+++ b/tests/control_plane_ts/remote_authority.integration.test.ts
@@ -2,7 +2,10 @@ import assert from "node:assert/strict";
import test from "node:test";
import {createServer, request as httpsRequest} from "node:https";
import type {AddressInfo} from "node:net";
-import {writeFile} from "node:fs/promises";
+import {mkdir, writeFile} from "node:fs/promises";
+import {join} from "node:path";
+import {execFile} from "node:child_process";
+import {promisify} from "node:util";
import {Client} from "pg";
import type {JsonObject} from "../../loopx/control_plane/effect_program.ts";
import {RemoteAuthorityService} from "../../loopx/control_plane/coordination/remote_authority_service.ts";
@@ -12,12 +15,86 @@ import {PostgreSqlAuthorityStore, rotatePostgreSqlAuthorityStoreIdentity, type P
import {remoteAuthorityOperationId, type RemoteAuthorityRequest} from "../../loopx/control_plane/coordination/remote_authority_contract.ts";
import {canonicalAuthoritySha256} from "../../loopx/control_plane/coordination/authority_store_codec.ts";
import {postgresRemoteFixture, claimRequest, credentials, serviceIdentity, servicePolicy} from "./remote_authority_fixture.ts";
+import {openLocalAuthorityStore} from "../../loopx/control_plane/coordination/local_authority_provider.ts";
+import {engageLegacyCoordinationWriterFence} from "../../loopx/control_plane/coordination/legacy_writer_fence.ts";
+import {authorityProjectionFixture} from "./authority_projection_fixture.ts";
+import {resolveTestPython} from "../../scripts/test-python.mjs";
import {registerRemoteAuthorityModeConformance} from "./remote_authority_mode_conformance.ts";
import {registerRemoteAuthorityLifecycleConformance} from "./remote_authority_lifecycle_conformance.ts";
const connectionString = process.env.LOOPX_TEST_POSTGRES_URL;
registerRemoteAuthorityModeConformance(connectionString);
registerRemoteAuthorityLifecycleConformance(connectionString);
+test("quota CLI verifies the exact remote binding and never grants local execution", {skip: !connectionString}, async t => {
+ const fixture = await postgresRemoteFixture(t, connectionString!);
+ await fixture.seed(["todo_gateway_probe"], {todo_gateway_probe: "private-synthetic-note"});
+ const initialHead = await fixture.store.loadAuthority();
+ const running = await fixture.start();
+ const python = resolveTestPython();
+ const invoke = promisify(execFile);
+ const workspace = join(fixture.root, "workspace"), runtime = join(fixture.root, "runtime");
+ const registry = join(workspace, "registry.json"), state = join(workspace, "STATE.md");
+ await mkdir(workspace);
+ await writeFile(state, "---\nstatus: active\nupdated_at: 2026-01-01T00:00:00Z\n---\n# Synthetic Goal\n");
+ // The candidate observes a remote authority alongside its established
+ // private host state. It does not auto-initialize or migrate a Goal.
+ const local = await openLocalAuthorityStore(runtime, "goal-one");
+ const localHead = authorityProjectionFixture("goal-one", [], [], "native", {handoff_mode: "soft_claim"});
+ const localSeed = await local.commitAuthority({operation_id: "gateway-local-fixture",
+ expected_provider_revision: null, next_projection: localHead, events: [], receipts: []});
+ assert.equal(localSeed.status, "applied");
+ if (localSeed.status !== "applied") throw new Error("local fixture did not initialize");
+ const fence = await engageLegacyCoordinationWriterFence({schema_version: "loopx_legacy_coordination_writer_fence_engage_request_v0",
+ runtime_root: runtime, goal_id: "goal-one", state_path: state, fence: {
+ schema_version: "loopx_legacy_coordination_writer_fence_v0", state: "engaged", goal_id: "goal-one",
+ fence_id: "gateway-local-fixture", source_version: "gateway-local-fixture",
+ source_projection_sha256: canonicalAuthoritySha256(localHead), expected_shadow_provider_revision: localSeed.provider_revision,
+ }});
+ assert.equal(fence.status, "applied");
+ const cfg = {enabled: true, endpoint: running.endpoint, actor_id: "actor-a",
+ tenant_id: "tenant-one", store_identity: serviceIdentity, probe_todo_id: "todo_gateway_probe", ca_file: fixture.certPath};
+ const env: NodeJS.ProcessEnv = {...process.env, LOOPX_CONTEXT_DELIVERY: "off", LOOPX_AUTHORITY_CREDENTIAL: credentials.a};
+ delete env.LOOPX_TEST_POSTGRES_URL;
+ const writeBinding = async (overrides: JsonObject = {}) => writeFile(registry, JSON.stringify({
+ schema_version: "0.1", common_runtime_root: runtime,
+ goals: [{id: "goal-one", status: "active", repo: workspace, state_file: "STATE.md",
+ coordination: {agent_model: "peer_v1", registered_agents: ["actor-a", "actor-b"]},
+ quota: {compute: 1}, remote_authority: {...cfg, ...overrides}}],
+ }));
+ const args = ["-m", "loopx.cli", "--registry", registry, "--runtime-root", runtime, "--format", "json"];
+ const scope = ["--goal-id", "goal-one", "--agent-id", "actor-a"];
+ async function check(expected: string, credential = credentials.a) {
+ const {stdout} = await invoke(python, [...args, "quota", "should-run", "--runtime-profile", "outer_controller", ...scope],
+ {env: {...env, LOOPX_AUTHORITY_CREDENTIAL: credential}, timeout: 25_000, maxBuffer: 1024 * 1024});
+ const decision = JSON.parse(stdout);
+ assert.equal(decision.remote_gateway?.status, expected, JSON.stringify({
+ reason: decision.reason, code: decision.reason_code, fields: Object.keys(decision),
+ }));
+ assert.equal(decision.effective_action, "blocked_wait");
+ assert.equal(decision.should_run, false);
+ assert.equal(decision.normal_delivery_allowed, false);
+ assert.equal(decision.interaction_contract.agent_channel.must_attempt, false);
+ assert.equal(decision.interaction_contract.agent_channel.delivery_allowed, false);
+ assert.equal(decision.interaction_contract.cli_channel.spend_allowed_now, false);
+ assert(!decision.selected_todo);
+ for (const value of [cfg.endpoint, cfg.tenant_id, cfg.store_identity, cfg.ca_file, credential, "private-synthetic-note"]) {
+ assert(!stdout.includes(value));
+ }
+ }
+ await writeBinding(); await check("verified");
+ await check("unavailable", credentials.b); // Valid credential, wrong actor authorization.
+ for (const binding of [{tenant_id: "wrong-tenant"}, {store_identity: `postgresql:${"f".repeat(32)}`},
+ {ca_file: null}, {probe_todo_id: "todo_missing"}, {actor_id: "actor-b"}]) {
+ await writeBinding(binding); await check("unavailable");
+ }
+ await writeBinding();
+ await fixture.writePolicy({...servicePolicy(), principals: []}); await check("unavailable");
+ await fixture.writePolicy(); await check("verified");
+ await running.stop(); await check("unavailable");
+ await fixture.start(running.port); await check("verified");
+ assert.deepEqual(await fixture.store.loadAuthority(), initialHead, "gateway observations never change the remote head");
+});
+
test("remote authority real TLS/PostgreSQL task journey and recovery", {skip: !connectionString}, async t => {
const fixture = await postgresRemoteFixture(t, connectionString!);
let running = await fixture.start();
diff --git a/tests/control_plane_ts/remote_authority.test.ts b/tests/control_plane_ts/remote_authority.test.ts
index c2583d70ac..e65dae3eb3 100644
--- a/tests/control_plane_ts/remote_authority.test.ts
+++ b/tests/control_plane_ts/remote_authority.test.ts
@@ -10,6 +10,7 @@ import {join} from "node:path";
import type {JsonObject} from "../../loopx/control_plane/effect_program.ts";
import {decodeRemoteAuthorityRequest, decodeRemoteAuthorityResponse, remoteAuthorityOperationId,
remoteAuthorityResponse} from "../../loopx/control_plane/coordination/remote_authority_contract.ts";
+import {canonicalAuthoritySha256} from "../../loopx/control_plane/coordination/authority_store_codec.ts";
import {decodeRemoteAuthorityPolicy, remoteAuthorityPolicyAdmission, readRemoteAuthorityPolicy} from "../../loopx/control_plane/coordination/remote_authority_policy.ts";
import {RemoteAuthorityService} from "../../loopx/control_plane/coordination/remote_authority_service.ts";
import {callRemoteAuthority, createRemoteAuthorityServer} from "../../loopx/control_plane/coordination/remote_authority_http.ts";
@@ -201,3 +202,174 @@ test("a body completed after shutdown starts cannot dispatch a new command", asy
await stopping;
assert.equal(admissions, 0);
});
+
+test("complete, block, defer, todo_add and snapshot request decoding validates typed lifecycle semantics", () => {
+ const base = {
+ schema_version: "loopx_remote_authority_request_v0" as const,
+ tenant_id: "tenant-one",
+ goal_id: "goal-one",
+ actor_id: "actor-a",
+ store_identity: `postgresql:${"c".repeat(32)}`,
+ todo_id: "todo_lifecycle_fixture",
+ };
+
+ // Complete
+ const completeReq = {
+ ...base,
+ operation: "complete" as const,
+ request_id: "req-complete-1",
+ claim_request_id: "req-claim-1",
+ evidence: "sha256:abcd verified delivery receipt",
+ note: "optional completion note",
+ };
+ assert.deepEqual(decodeRemoteAuthorityRequest(completeReq), completeReq);
+ assert.throws(() => decodeRemoteAuthorityRequest({...completeReq, evidence: ""}));
+ assert.throws(() => decodeRemoteAuthorityRequest({...completeReq, claim_request_id: ""}));
+ assert.throws(() => decodeRemoteAuthorityRequest({...completeReq, claim_request_id: 12}));
+ assert.throws(() => decodeRemoteAuthorityRequest({...completeReq, request_id: null}));
+
+ // Block
+ const blockReq = {
+ ...base,
+ operation: "block" as const,
+ request_id: "req-block-1",
+ reason: "waiting on upstream schema",
+ resume_when: "todo_done:todo_schema_fixture",
+ note: "paused temporarily",
+ };
+ assert.deepEqual(decodeRemoteAuthorityRequest(blockReq), blockReq);
+ assert.throws(() => decodeRemoteAuthorityRequest({...blockReq, reason: ""}));
+ assert.throws(() => decodeRemoteAuthorityRequest({...blockReq, resume_when: "schema_applied"}));
+
+ // Defer
+ const deferReq = {
+ ...base,
+ operation: "defer" as const,
+ request_id: "req-defer-1",
+ reason: "resuming next week",
+ resume_when: "resume_at:2026-11-20T00:00:00Z",
+ };
+ assert.deepEqual(decodeRemoteAuthorityRequest(deferReq), deferReq);
+ assert.throws(() => decodeRemoteAuthorityRequest({...deferReq, resume_when: ""}));
+
+ // Todo add
+ const addReq = {
+ ...base,
+ operation: "todo_add" as const,
+ request_id: "req-add-1",
+ text: "[P1] new remote task",
+ priority: "P1",
+ role: "agent" as const,
+ task_class: "advancement_task",
+ required_write_scopes: ["loopx/control_plane/**"],
+ };
+ assert.deepEqual(decodeRemoteAuthorityRequest(addReq), addReq);
+ assert.throws(() => decodeRemoteAuthorityRequest({...addReq, priority: "P9"}));
+ assert.throws(() => decodeRemoteAuthorityRequest({...addReq, role: "invalid"}));
+
+ // Snapshot (todo_id is optional)
+ const snapshotGoal = {
+ schema_version: "loopx_remote_authority_request_v0" as const,
+ tenant_id: "tenant-one",
+ goal_id: "goal-one",
+ actor_id: "actor-a",
+ store_identity: `postgresql:${"c".repeat(32)}`,
+ operation: "snapshot" as const,
+ };
+ assert.deepEqual(decodeRemoteAuthorityRequest(snapshotGoal), snapshotGoal);
+ const snapshotTodo = {...snapshotGoal, todo_id: "todo_lifecycle_fixture"};
+ assert.deepEqual(decodeRemoteAuthorityRequest(snapshotTodo), snapshotTodo);
+ assert.throws(() => decodeRemoteAuthorityRequest({...snapshotTodo, todo_id: false}));
+});
+
+test("policy admits complete, block, defer, todo_add and snapshot grants and rejects ungranted operations", () => {
+ const policy = servicePolicy();
+ const grant = policy.principals[0].grants[0];
+ grant.operations = ["read", "claim", "progress", "renew", "release", "complete", "block", "defer", "todo_add", "snapshot"];
+ const decodedPolicy = decodeRemoteAuthorityPolicy(policy);
+
+ const base = {
+ schema_version: "loopx_remote_authority_request_v0" as const,
+ tenant_id: "tenant-one",
+ goal_id: "goal-one",
+ actor_id: "actor-a",
+ store_identity: `postgresql:${"c".repeat(32)}`,
+ todo_id: "todo_one",
+ };
+
+ const completeReq = decodeRemoteAuthorityRequest({
+ ...base, operation: "complete", request_id: "req-comp", claim_request_id: "req-claim", evidence: "evidence-1",
+ });
+ const snapshotReq = decodeRemoteAuthorityRequest({
+ ...base, operation: "snapshot",
+ });
+
+ const admComplete = remoteAuthorityPolicyAdmission(decodedPolicy, credentials.a, completeReq);
+ assert.equal(admComplete.status, "allowed");
+
+ const admSnapshot = remoteAuthorityPolicyAdmission(decodedPolicy, credentials.a, snapshotReq);
+ assert.equal(admSnapshot.status, "allowed");
+
+ // Default-deny check: principal b does not have "complete" granted
+ policy.principals[1].grants[0].operations = ["read", "claim"];
+ const decodedPolicyWithRestrictedB = decodeRemoteAuthorityPolicy(policy);
+ assert.equal(policy.principals[1].grants[0].operations.includes("complete" as any), false);
+ const admDenied = remoteAuthorityPolicyAdmission(decodedPolicyWithRestrictedB, credentials.b, {
+ ...completeReq, actor_id: "actor-b",
+ });
+ assert.equal(admDenied.status, "rejected");
+ assert.equal(admDenied.reason_code, "remote_authority_scope_denied");
+});
+
+test("decodeRemoteAuthorityResponse validates snapshot and complete response payloads", () => {
+ const base = {
+ schema_version: "loopx_remote_authority_request_v0" as const,
+ tenant_id: "tenant-one",
+ goal_id: "goal-one",
+ actor_id: "actor-a",
+ store_identity: `postgresql:${"c".repeat(32)}`,
+ todo_id: "todo_one",
+ };
+ const snapshotReq = decodeRemoteAuthorityRequest({...base, operation: "snapshot"});
+ const snapResponse = (todos: JsonObject[], leases: JsonObject[] = []) => {
+ const facts = {provider_revision: `${base.store_identity}:10`, store_identity: base.store_identity,
+ goal_id: base.goal_id, todos, leases};
+ return remoteAuthorityResponse(snapshotReq, "execute", {status: "snapshot", ...facts,
+ receipt_sha256: canonicalAuthoritySha256({schema_version: "loopx_goal_snapshot_receipt_v0",
+ tenant_id: base.tenant_id, ...facts})});
+ };
+ const todo = {todo_id: "todo_one", status: "open", role: "agent"};
+ const valid = snapResponse([todo]);
+ assert.deepEqual(decodeRemoteAuthorityResponse(valid, snapshotReq, "execute"), valid);
+ assert.throws(() => decodeRemoteAuthorityResponse({...valid, receipt_sha256: "a".repeat(64)}, snapshotReq, "execute"));
+ assert.throws(() => decodeRemoteAuthorityResponse(snapResponse([{...todo, todo_id: "todo_wrong"}]), snapshotReq, "execute"));
+ assert.throws(() => decodeRemoteAuthorityResponse(snapResponse([todo, todo]), snapshotReq, "execute"));
+ const lease = {todo_id: "todo_one", goal_id: base.goal_id, owner: base.actor_id, status: "released",
+ version: 1, lease_epoch: 1};
+ assert.throws(() => decodeRemoteAuthorityResponse(snapResponse([todo], [{...lease, goal_id: "wrong-goal"}]), snapshotReq, "execute"));
+ assert.throws(() => decodeRemoteAuthorityResponse(snapResponse([], [lease]), snapshotReq, "execute"));
+ assert.throws(() => decodeRemoteAuthorityResponse(snapResponse([todo], [{...lease, idempotency_key: "must-not-leak"}]), snapshotReq, "execute"));
+ assert.throws(() => decodeRemoteAuthorityResponse(valid, snapshotReq, "recover"));
+
+ const completeReq = decodeRemoteAuthorityRequest({
+ ...base, operation: "complete", request_id: "req-c", claim_request_id: "req-cl", evidence: "proof",
+ });
+ const opId = remoteAuthorityOperationId(completeReq, "principal-a");
+ const compResponse = remoteAuthorityResponse(completeReq, "execute", {
+ status: "result",
+ operation_id: opId,
+ result: {
+ status: "applied",
+ changed: true,
+ completed_at: "2026-12-01T12:00:00Z",
+ completion_receipt_id: "receipt-1",
+ todo_id: "todo_one",
+ provider_revision: `${base.store_identity}:11`,
+ cursor: "11",
+ },
+ });
+ assert.deepEqual(decodeRemoteAuthorityResponse(compResponse, completeReq, "execute"), compResponse);
+ const incomplete = structuredClone(compResponse);
+ delete (incomplete.result as JsonObject).completion_receipt_id;
+ assert.throws(() => decodeRemoteAuthorityResponse(incomplete, completeReq, "execute"));
+});
diff --git a/tests/control_plane_ts/remote_authority_fixture.ts b/tests/control_plane_ts/remote_authority_fixture.ts
index 5b4a0e9b77..93125f5136 100644
--- a/tests/control_plane_ts/remote_authority_fixture.ts
+++ b/tests/control_plane_ts/remote_authority_fixture.ts
@@ -28,9 +28,9 @@ export function servicePolicy(identity = serviceIdentity) {
principals: ["a", "b"].map(actor => ({principal_id: `principal-${actor}`,
credential_sha256: createHash("sha256").update(credentials[actor as "a" | "b"]).digest("hex"),
grants: [{tenant_id: "tenant-one", goal_id: "goal-one", actor_id: `actor-${actor}`,
- operations: ["read", "claim", "progress", "renew", "release"]}]}))};
+ operations: ["read", "claim", "progress", "renew", "release", "complete", "block", "defer", "todo_add", "snapshot"]}]}))};
}
-export function claimRequest(actor: "a" | "b", todo = "todo_one", id = `claim-${todo}`): RemoteAuthorityRequest {
+export function claimRequest(actor: "a" | "b", todo = "todo_one", id = `claim-${todo}`): Extract {
return {schema_version: REMOTE_AUTHORITY_REQUEST, tenant_id: "tenant-one", goal_id: "goal-one",
actor_id: `actor-${actor}`, store_identity: serviceIdentity, todo_id: todo, operation: "claim", request_id: id};
}
diff --git a/tests/control_plane_ts/remote_authority_lifecycle_conformance.ts b/tests/control_plane_ts/remote_authority_lifecycle_conformance.ts
index 877c69313e..37f8271eac 100644
--- a/tests/control_plane_ts/remote_authority_lifecycle_conformance.ts
+++ b/tests/control_plane_ts/remote_authority_lifecycle_conformance.ts
@@ -2,9 +2,10 @@ import assert from "node:assert/strict";
import test from "node:test";
import type {JsonObject} from "../../loopx/control_plane/effect_program.ts";
import {prepareCoordinationProjectionCommit, type CoordinationProjectionMutation} from "../../loopx/control_plane/coordination/coordination_projection.ts";
+import {executeCoordinationTodoTerminalLifecycle} from "../../loopx/control_plane/coordination/todo_terminal_lifecycle.ts";
import {executeCanonicalTaskLeaseLifecycle} from "../../loopx/control_plane/coordination/task_lease_lifecycle.ts";
import type {RemoteAuthorityRequest} from "../../loopx/control_plane/coordination/remote_authority_contract.ts";
-import {postgresRemoteFixture, claimRequest} from "./remote_authority_fixture.ts";
+import {postgresRemoteFixture, claimRequest, serviceIdentity} from "./remote_authority_fixture.ts";
function lifecycle(todo: string, operation: "renew" | "release", version = 0): RemoteAuthorityRequest {
const {request_id: _, ...scope} = claimRequest("a", todo) as Extract;
@@ -89,4 +90,226 @@ export function registerRemoteAuthorityLifecycleConformance(connectionString: st
assert.equal((recovered.value.result as JsonObject).missing, true);
assert.deepEqual(await fixture.store.loadAuthority(), committed, "replay and recovery retain the original no-op receipt");
});
+
+ test("remote completion keeps a recorded successor available to the next actor", {skip: !connectionString}, async t => {
+ const fixture = await postgresRemoteFixture(t, connectionString!);
+ const running = await fixture.start();
+ await fixture.seed(["todo_remote", "todo_local", "todo_next_remote", "todo_next_local"]);
+ const seed = await fixture.store.loadAuthority();
+ assert.equal(seed.status, "loaded");
+ if (seed.status !== "loaded") return;
+ const links: Record = {
+ todo_remote: {successor_todo_ids: ["todo_next_remote"]},
+ todo_local: {successor_todo_ids: ["todo_next_local"]},
+ todo_next_remote: {resume_when: "todo_done:todo_remote"},
+ todo_next_local: {resume_when: "todo_done:todo_local"},
+ };
+ const mutations: CoordinationProjectionMutation[] = (seed.head.todos as JsonObject[]).map(todo => ({
+ kind: "todo_upsert", todo: {...todo, ...links[String(todo.todo_id)]},
+ }));
+ const linked = await fixture.store.commitAuthority(prepareCoordinationProjectionCommit({goal_id: "goal-one",
+ operation_id: "existing-successors", expected_provider_revision: seed.provider_revision, projection: seed.head, mutations}));
+ assert.equal(linked.status, "applied");
+
+ const client = (actor: "a" | "b", request: RemoteAuthorityRequest, recover = false) =>
+ fixture.client(running.endpoint, actor, request, recover);
+ const localClaim = await client("a", claimRequest("a", "todo_local"));
+ assert.equal(localClaim.code, 0);
+ const control = await executeCoordinationTodoTerminalLifecycle(fixture.store, {
+ goal_id: "goal-one", todo_id: "todo_local", command: "complete",
+ expected_role: "agent", actor_agent_id: "actor-a", registered_agents: ["actor-a", "actor-b"],
+ lifecycle_grants: [], authority_reason: null, decision_outcome: null,
+ operation_identity: {kind: "explicit", operation_id: "finish-local"},
+ claim_operation_id: String((localClaim.value.result as JsonObject).claim_operation_id),
+ lease_idempotency_key: null, lease_expected_version: null, allow_user_gate_auto_acquire: false,
+ requested_no_followup: false, requested_completion_turn_key: null, requested_completion_identity_source: null,
+ linked_successor_todo_ids: [], successor_intents: [], note: null, evidence: "verified fixture result",
+ reason: null, clear_claim: true, validation_declaration: null, validation_receipt: null,
+ completion_policy_request: null, dry_run: false, now: new Date(),
+ });
+ assert.equal(control.status, "applied");
+ assert.equal(control.completion_continuation, "successor");
+
+ const next = claimRequest("b", "todo_next_remote");
+ const pending = await client("b", next);
+ assert.equal(pending.code, 1);
+ assert.equal((pending.value.result as JsonObject).reason_code, "todo_dependency_pending");
+ const {request_id: claim_request_id, ...scope} = claimRequest("a", "todo_remote");
+ assert.equal((await client("a", {...scope, request_id: claim_request_id})).code, 0);
+ const command = {...scope, operation: "complete" as const, request_id: "finish-remote",
+ claim_request_id, evidence: "verified fixture result"};
+ const finished = await client("a", command);
+ assert.equal(finished.code, 0, JSON.stringify(finished.value));
+ assert.equal((finished.value.result as JsonObject).status, "applied");
+ const receipt = (finished.value.result as JsonObject).completion_receipt_id;
+
+ const committed = await fixture.store.loadAuthority();
+ assert.equal(committed.status, "loaded");
+ if (committed.status !== "loaded") return;
+ const todo = (committed.head.todos as JsonObject[]).find(row => row.todo_id === command.todo_id);
+ assert.equal(todo?.completion_continuation, control.completion_continuation);
+ assert.deepEqual(todo?.successor_todo_ids, [next.todo_id]);
+ assert.equal(todo?.completion_receipt_id, receipt);
+ const replay = await client("a", command);
+ assert.equal(replay.code, 0);
+ assert.equal((replay.value.result as JsonObject).status, "replayed");
+ const recovered = await client("a", command, true);
+ assert.equal(recovered.code, 0);
+ assert.equal(recovered.value.historical_only, true);
+ assert.equal((recovered.value.result as JsonObject).completion_receipt_id, receipt);
+ const changedIntent = await client("a", {...command, no_followup: true});
+ assert.equal(changedIntent.code, 1);
+ assert.equal(changedIntent.value.reason_code, "remote_authority_operation_mismatch");
+ assert.deepEqual(await fixture.store.loadAuthority(), committed);
+
+ const {request_id: _, ...reader} = claimRequest("b", command.todo_id);
+ const observed = await client("b", {...reader, operation: "read"});
+ assert.equal(observed.code, 0);
+ assert.equal((observed.value.todo as JsonObject).status, "done");
+ assert.equal((observed.value.lease as JsonObject).status, "released");
+ assert.equal((await client("b", next)).code, 0);
+ });
+
+ test("remote authority full lifecycle: snapshot, todo_add, complete, block, and defer", {skip: !connectionString}, async t => {
+ const fixture = await postgresRemoteFixture(t, connectionString!);
+ const running = await fixture.start();
+ await fixture.seed(["todo_for_complete", "todo_for_terminal", "todo_for_block", "todo_for_defer"]);
+
+ const baseScope = {
+ schema_version: "loopx_remote_authority_request_v0" as const,
+ tenant_id: "tenant-one",
+ goal_id: "goal-one",
+ actor_id: "actor-a",
+ store_identity: serviceIdentity,
+ };
+
+ // 1. Snapshot
+ const snap = await fixture.client(running.endpoint, "a", {
+ ...baseScope,
+ operation: "snapshot",
+ });
+ assert.equal(snap.code, 0);
+ assert.equal(snap.value.status, "snapshot");
+ assert(Array.isArray(snap.value.todos));
+ assert(Array.isArray(snap.value.leases));
+ assert.equal(typeof snap.value.receipt_sha256, "string");
+
+ // 2. Todo add
+ const added = await fixture.client(running.endpoint, "a", {
+ ...baseScope,
+ todo_id: "todo_created_remotely",
+ operation: "todo_add",
+ request_id: "req-add-remotely",
+ text: "[P1] remotely created task",
+ priority: "P1",
+ role: "agent",
+ });
+ assert.equal(added.code, 0);
+ assert.equal((added.value.result as JsonObject).status, "applied");
+ assert.equal((added.value.result as JsonObject).todo_id, "todo_created_remotely");
+
+ // 3. Claim -> Complete
+ const claimComp = await fixture.client(running.endpoint, "a", {
+ ...baseScope,
+ todo_id: "todo_for_complete",
+ operation: "claim",
+ request_id: "claim-for-comp",
+ });
+ assert.equal(claimComp.code, 0);
+ assert.equal((claimComp.value.result as JsonObject).status, "applied");
+
+ const comp = await fixture.client(running.endpoint, "a", {
+ ...baseScope,
+ todo_id: "todo_for_complete",
+ operation: "complete",
+ request_id: "complete-req-1",
+ claim_request_id: "claim-for-comp",
+ evidence: "sha256:1111 delivery validated",
+ });
+ assert.equal(comp.code, 0);
+ assert.equal((comp.value.result as JsonObject).status, "applied");
+ assert.equal((comp.value.result as JsonObject).completed, true);
+
+ const {request_id: terminalClaimId, ...terminalScope} = claimRequest("a", "todo_for_terminal");
+ assert.equal((await fixture.client(running.endpoint, "a", {...terminalScope, request_id: terminalClaimId})).code, 0);
+ const terminal = await fixture.client(running.endpoint, "a", {
+ ...terminalScope, operation: "complete", request_id: "finish-terminal", claim_request_id: terminalClaimId,
+ evidence: "verified fixture result", no_followup: true,
+ });
+ assert.equal(terminal.code, 0);
+ const completedHead = await fixture.store.loadAuthority();
+ assert.equal(completedHead.status, "loaded");
+ if (completedHead.status !== "loaded") return;
+ const completedTodos = completedHead.head.todos as JsonObject[];
+ assert.equal(completedTodos.find(row => row.todo_id === "todo_for_complete")?.completion_continuation, "active_goal");
+ assert.equal(completedTodos.find(row => row.todo_id === "todo_for_terminal")?.completion_continuation, "no_followup");
+
+ // 4. Claim -> Block
+ const claimBlock = await fixture.client(running.endpoint, "a", {
+ ...baseScope,
+ todo_id: "todo_for_block",
+ operation: "claim",
+ request_id: "claim-for-block",
+ });
+ assert.equal(claimBlock.code, 0);
+
+ const block = await fixture.client(running.endpoint, "a", {
+ ...baseScope,
+ todo_id: "todo_for_block",
+ operation: "block",
+ request_id: "block-req-1",
+ claim_request_id: "claim-for-block",
+ reason: "external dependency blocked",
+ });
+ assert.equal(block.code, 0);
+ assert.equal((block.value.result as JsonObject).status, "applied");
+
+ // 5. Claim -> Defer
+ const claimDefer = await fixture.client(running.endpoint, "a", {
+ ...baseScope,
+ todo_id: "todo_for_defer",
+ operation: "claim",
+ request_id: "claim-for-defer",
+ });
+ assert.equal(claimDefer.code, 0);
+
+ const defer = await fixture.client(running.endpoint, "a", {
+ ...baseScope,
+ todo_id: "todo_for_defer",
+ operation: "defer",
+ request_id: "defer-req-1",
+ claim_request_id: "claim-for-defer",
+ reason: "deferred until milestone",
+ resume_when: "resume_at:2026-12-01T00:00:00Z",
+ });
+ assert.equal(defer.code, 0);
+ assert.equal((defer.value.result as JsonObject).status, "applied");
+ });
+
+ test("snapshot is bounded, targeted, and read only across independent clients", {skip: !connectionString}, async t => {
+ const fixture = await postgresRemoteFixture(t, connectionString!);
+ const running = await fixture.start();
+ const ids = Array.from({length: 24}, (_, i) => `todo_snapshot_${String(i).padStart(2, "0")}`);
+ await fixture.seed(ids, Object.fromEntries(ids.map(id => [id, "x".repeat(3100)])));
+ const initialHead = await fixture.store.loadAuthority();
+ const {request_id: _, todo_id, ...base} = claimRequest("a", ids[0]);
+ const wide: RemoteAuthorityRequest = {...base, operation: "snapshot"};
+ const tooLarge = await fixture.client(running.endpoint, "a", wide);
+ assert.equal(tooLarge.code, 1);
+ assert.equal(tooLarge.value.status, "unavailable");
+ assert.equal(tooLarge.value.reason_code, "remote_authority_response_too_large");
+
+ const targeted: RemoteAuthorityRequest = {...wide, todo_id};
+ const first = await fixture.client(running.endpoint, "a", targeted);
+ const second = await fixture.client(running.endpoint, "b", {...targeted, actor_id: "actor-b"});
+ assert.equal(first.code, 0); assert.equal(second.code, 0);
+ assert.deepEqual((first.value.todos as JsonObject[]).map(todo => todo.todo_id), [todo_id]);
+ assert.equal(first.value.receipt_sha256, second.value.receipt_sha256);
+ const wrongGoal = await fixture.client(running.endpoint, "a", {...targeted, goal_id: "another-goal"});
+ assert.equal(wrongGoal.code, 1); assert.equal(wrongGoal.value.status, "rejected");
+ const query = await fixture.client(running.endpoint, "a", targeted, true);
+ assert.equal(query.code, 1); assert.equal(query.value.status, "rejected");
+ assert.deepEqual(await fixture.store.loadAuthority(), initialHead);
+ });
+
}
diff --git a/tests/control_plane_ts/remote_authority_mode_conformance.ts b/tests/control_plane_ts/remote_authority_mode_conformance.ts
index 3d9cebc6c9..ea5dc9b6ea 100644
--- a/tests/control_plane_ts/remote_authority_mode_conformance.ts
+++ b/tests/control_plane_ts/remote_authority_mode_conformance.ts
@@ -16,7 +16,7 @@ export function registerRemoteAuthorityModeConformance(connectionString: string
await t.test(`mode change ${timing} cannot commit a remote ${operation} under soft mode`, async child => {
const claim = claimRequest("a", `todo_${operation}_${timing.replaceAll("-", "_")}`);
const {request_id: _, ...scope} = claim as Extract;
- const request: RemoteAuthorityRequest = operation === "claim" ? claim
+ const request = operation === "claim" ? claim
: {...scope, operation, idempotency_key: "missing-lease-key", expected_version: 0};
await fixture.seed([request.todo_id]);
let switched = false, headReads = 0, commitAttempts = 0;
diff --git a/tests/control_plane_ts/replan_semantics.test.ts b/tests/control_plane_ts/replan_semantics.test.ts
index d1b226b408..73a0a76d37 100644
--- a/tests/control_plane_ts/replan_semantics.test.ts
+++ b/tests/control_plane_ts/replan_semantics.test.ts
@@ -661,6 +661,13 @@ test("a later durable exact ACK settles only its original receipt-bound Turn", (
assert.deepEqual(packet.semantic_delta, delta);
assert.equal((packet.writeback_contract as JsonObject).preferred_input, "exact_historical_ack");
+ const pending = projectReplanSemantics({operation: "receipt_bound_obligation", ...request,
+ current_obligation: null, replay_phase: "settlement_pending"}).obligation as JsonObject;
+ const remaining = pending.settlement_action_packet as JsonObject;
+ assert.equal(remaining.required_outcome, "quota_spend_receipt");
+ assert.equal(remaining.semantic_delta, null);
+ assert.equal(remaining.successor_todo_id, null);
+
for (const missingGuard of [{guard_scoped: false}, {goal_id: null}, {agent_id: null},
{guard_recorded_at: null}, {guard_turn_instance_id: null}]) {
const unbound = {...request, ...missingGuard};
@@ -794,6 +801,15 @@ test(`${recovery} restores only its exact admitted review and still owes Vision`
assert.equal(Object.hasOwn(writeback.vision_authoring as JsonObject, "continuation_review"), false);
assert.equal(projectReplanSemantics({operation: "qualify", obligation: result}).accepted, false);
assert.equal(projectReplanSemantics({operation: "qualify", obligation: result, agent_vision: vision}).accepted, true);
+ // The same lifecycle evidence still exists after writeback. Reentry follows
+ // the verified settlement phase while leaving the next frontier unselected.
+ const pending = projectReplanSemantics({...request, replay_phase: "settlement_pending",
+ current_obligation: {obligation_id: "replan-2222222222222222"}}).obligation as JsonObject;
+ assert.equal(pending.obligation_id, id);
+ assert.deepEqual(pending.todo_actions, []);
+ const remaining = pending.settlement_action_packet as JsonObject;
+ assert.equal(remaining.decision, "settlement_pending");
+ assert.equal(remaining.required_outcome, "quota_spend_receipt");
const historical = {...row, todo_id: "todo_historical", completion_turn_key: "historical-completion",
completed_at: "2026-07-31T23:59:00Z", updated_at: "2026-07-31T23:59:01Z"};
for (const items of [[historical, row], [row, historical]]) {
diff --git a/tests/control_plane_ts/state_recovery.test.ts b/tests/control_plane_ts/state_recovery.test.ts
index ff39eff2a7..1274105e61 100644
--- a/tests/control_plane_ts/state_recovery.test.ts
+++ b/tests/control_plane_ts/state_recovery.test.ts
@@ -69,12 +69,12 @@ test("captured runtime bindings are optional data and must name included inert r
}
});
-test("native capture metadata cannot change its Goal/configuration dependency or exceed the historical capacity", () => {
+test("native capture metadata binds its dependencies and accepts a large claimed history", () => {
const original = request();
const path = `${authorityBackupDirectory("goal-a")}/authority.jsonl`;
const capture = {goal_id: "goal-a", state: "readable", runtime_archive_path: "runtime-root",
configuration_sha256: "d".repeat(64), started_at: "2026-10-15T00:00:00.000Z", completed_at: "2026-10-15T00:00:01.000Z",
- archive_path: path, archive: {goal_id: "goal-a", commits: "1", archive_sha256: "e".repeat(64)},
+ archive_path: path, archive: {goal_id: "goal-a", commits: "20001", archive_sha256: "e".repeat(64)},
dependencies: {selector_sha256: null, writer_fence_sha256: null}};
const artifact = {...member, archive_path: path};
original.files = [artifact];
@@ -84,12 +84,13 @@ test("native capture metadata cannot change its Goal/configuration dependency or
Object.assign(manifest.execution, {authority_captures: [capture], file_members: [artifact],
configuration_backup: {sha256: "d".repeat(64)}});
}
- // The logical archive seal is deliberately different from the member byte
- // digest: each has its own verifier and must never be substituted for the other.
+ // This validates the manifest claim. The archive reader verifies the actual
+ // cursor and seal, independently of the member byte digest.
assert.equal(verifyStateRecoveryManifest(original).ok, true);
for (const delta of [{runtime_archive_path: null}, {runtime_archive_path: "other"},
{configuration_sha256: "f".repeat(64)}, {completed_at: "2026-10-14T00:00:00.000Z"},
- {archive_path: "../outside"}, {archive: {...capture.archive, commits: "20001"}}]) {
+ {archive_path: "../outside"}, {archive: {...capture.archive, commits: "0"}},
+ {archive: {...capture.archive, commits: "020001"}}, {archive: {...capture.archive, commits: 20001}}]) {
const input = structuredClone(original);
for (const manifest of [input.external_manifest, input.embedded_manifest]) {
Object.assign((manifest.execution as any).authority_captures[0], delta);
diff --git a/tests/control_plane_ts/unsettled_host_turn_recovery.test.ts b/tests/control_plane_ts/unsettled_host_turn_recovery.test.ts
index 8287c5fc62..e787055478 100644
--- a/tests/control_plane_ts/unsettled_host_turn_recovery.test.ts
+++ b/tests/control_plane_ts/unsettled_host_turn_recovery.test.ts
@@ -413,6 +413,27 @@ test("a receipt that does not opt in never becomes a recovery obligation", async
}
});
+test("an admitted Todo-bound replan without verified writeback does not hold a later guard", async () => {
+ const admitted = {
+ ...closeoutRequired("turn-replan", "todo_alpha"), closeout_required: false,
+ semantic_replan_obligation_id: `replan-${"b".repeat(16)}`,
+ ok: true, should_run: true, interaction_mode: "autonomous_replan",
+ must_attempt_work: true, delivery_allowed: false, quiet_noop_allowed: false,
+ };
+ const runtime = await runtimeWith([
+ receipt("turn-normal", closeoutRequired("turn-normal", "todo_beta")),
+ receipt("turn-replan", admitted),
+ ]);
+ const replanOnly = await runtimeWith([receipt("turn-replan", admitted)]);
+ try {
+ assert.equal(candidateFrom(await preflight(runtime.root)).prior_turn_instance_id, "turn-normal");
+ assert.equal((await preflight(replanOnly.root)).reason, "no_prior_turn_requires_closeout");
+ } finally {
+ await runtime.close();
+ await replanOnly.close();
+ }
+});
+
test("the current Turn, other goals, other agents, and other event kinds are ignored", async () => {
const runtime = await runtimeWith([
receipt("current-turn", closeoutRequired("current-turn", "todo_current")),
@@ -852,6 +873,17 @@ test("typed lifecycle closeouts are accepted and other open Todos are not", asyn
assert.equal(verdict.status, "none");
assert.equal(verdict.accepted_closeout, accepted);
}
+ const ordinaryPending = reduce(candidate, ["quota_spend_receipt"], READ_TODO(todo));
+ assert.equal(ordinaryPending.status, verdict.status);
+ assert.equal(ordinaryPending.accepted_closeout, verdict.accepted_closeout);
+
+ const replanPending = reduce(
+ {...candidate, admitted_todo_replan: true},
+ ["quota_spend_receipt"],
+ READ_TODO(todo),
+ );
+ assert.equal(replanPending.status, "recovery_required");
+ assert.equal((replanPending.recovery as JsonObject).repair, "resume_prior_turn");
}
} finally {
await runtime.close();
diff --git a/tests/extensions/test_lark_cli_resolution.py b/tests/extensions/test_lark_cli_resolution.py
index 85ef88ef55..0eef3e3b45 100644
--- a/tests/extensions/test_lark_cli_resolution.py
+++ b/tests/extensions/test_lark_cli_resolution.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import os
+from contextlib import contextmanager
from pathlib import Path
from typing import Any
@@ -9,6 +10,7 @@
LarkCliResolution,
build_lark_command_runner,
resolve_lark_cli,
+ run_lark_command_with_dispatch_guard,
)
@@ -192,6 +194,53 @@ def fake_run(args: list[str], **kwargs: Any) -> Any:
]
+def test_guarded_cli_spawns_inside_authority_and_waits_outside(tmp_path, monkeypatch):
+ import subprocess
+ import sys
+
+ real_popen = subprocess.Popen
+ states = []
+ held = False
+
+ @contextmanager
+ def guard():
+ nonlocal held
+ held = True
+ try:
+ yield
+ finally:
+ held = False
+
+ class ObservedProcess:
+ def __init__(self, *args, **kwargs):
+ states.append(("spawn", held))
+ self.process = real_popen(*args, **kwargs)
+
+ def __enter__(self):
+ self.process.__enter__()
+ return self
+
+ def __exit__(self, *args):
+ return self.process.__exit__(*args)
+
+ @property
+ def returncode(self):
+ return self.process.returncode
+
+ def communicate(self, *, timeout):
+ states.append(("wait", held))
+ return self.process.communicate(timeout=timeout)
+
+ monkeypatch.setattr(subprocess, "Popen", ObservedProcess)
+ resolution = LarkCliResolution(sys.executable, True, "fixture", None, None)
+ runner = build_lark_command_runner(resolution)
+ result = run_lark_command_with_dispatch_guard(
+ runner, [sys.executable, "-c", "print('result')"], tmp_path, 5, guard,
+ )
+ assert result["returncode"] == 0 and result["stdout"] == "result\n"
+ assert states == [("spawn", True), ("wait", False)]
+
+
def test_resolver_source_preserves_public_boundary_contract() -> None:
source = Path(__file__).resolve().parents[2] / "loopx/extensions/lark/cli_resolution.py"
diff --git a/tests/presentation/test_distribution_build.py b/tests/presentation/test_distribution_build.py
index acd0405c40..eb233f8957 100644
--- a/tests/presentation/test_distribution_build.py
+++ b/tests/presentation/test_distribution_build.py
@@ -13,6 +13,7 @@
import shutil
import subprocess
import sys
+import tomllib
import zipfile
import pytest
@@ -120,6 +121,30 @@ def wheel(source, output, *, keep_temp=False, skip_build=False):
}
+def test_manager_discovery_projection_ships_with_its_skill(checkout, tmp_path):
+ """The installed lazy handler must have its original typed dependency."""
+ package = "loopx.capabilities.manager_context"
+ data = tomllib.loads((ROOT / "pyproject.toml").read_text())["tool"]["setuptools"]["package-data"]
+ with (checkout / "pyproject.toml").open("a") as configuration:
+ configuration.write(f'"{package}"={json.dumps(data[package])}\n')
+ for name in ("loopx/capabilities", "loopx/capabilities/manager_context"):
+ directory = checkout / name
+ directory.mkdir(parents=True, exist_ok=True)
+ (directory / "__init__.py").write_text("", encoding="utf-8")
+ resources = (
+ "loopx/capabilities/manager_context/discovery_projection.ts",
+ "loopx/capabilities/manager_context/skills/loopx-manager/SKILL.md",
+ )
+ for name in resources:
+ target = checkout / name
+ target.parent.mkdir(parents=True, exist_ok=True)
+ shutil.copyfile(ROOT / name, target)
+ packaged = wheel(checkout, tmp_path / "manager-wheel")
+ for name in resources:
+ assert name in packaged, f"installed manager discovery dependency missing: {name}"
+ assert packaged[name] == (ROOT / name).read_bytes()
+
+
@pytest.mark.parametrize("keep_temp", [False, True])
def test_reused_build_removes_deleted_sources_and_keeps_current_resources(
checkout, tmp_path, keep_temp
diff --git a/tests/test_agy_host_surface.py b/tests/test_agy_host_surface.py
index 54599097c7..e7e2a1e44a 100644
--- a/tests/test_agy_host_surface.py
+++ b/tests/test_agy_host_surface.py
@@ -9,12 +9,16 @@
from __future__ import annotations
+import json
import os
from pathlib import Path
+import shlex
import pytest
from host_surface_cli_probes import (
+ connected_project,
onboarding_setup_command_installs,
+ run_cli,
selection_gate_offers_surface,
start_goal_accepts_surface,
)
@@ -52,6 +56,61 @@
HOST_SURFACE = "agy"
+@pytest.mark.parametrize(
+ "entry", [
+ ("bootstrap-command-pack", "--host-surface", "antigravity-app"),
+ ("agent-onboard", "--agent-type", "antigravity-app"),
+ ],
+)
+def test_antigravity_generated_commands_read_the_project_registry_from_another_directory(
+ tmp_path: Path, entry: tuple[str, ...],
+) -> None:
+ project = connected_project(tmp_path / "project with spaces")
+ registry = project / ".loopx" / "registry.json"
+ runtime = tmp_path / "isolated-runtime"
+ state = project / "ACTIVE_GOAL_STATE.md"
+ state.write_text("# Goal\n\nUse the registered project state.\n", encoding="utf-8")
+ data = json.loads(registry.read_text())
+ data["common_runtime_root"] = str(runtime)
+ data["goals"][0].update(
+ state_file=state.name,
+ coordination={"registered_agents": ["probe-agent"], "agent_model": "peer_v1"},
+ )
+ registry.write_text(json.dumps(data), encoding="utf-8")
+ foreign = tmp_path / "other"
+ foreign.mkdir()
+ # The host may be in another project which happens to use the same Goal id.
+ other = connected_project(foreign)
+ foreign_registry = other / ".loopx" / "registry.json"
+ foreign_registry.write_text(json.dumps({
+ "goals": [{**data["goals"][0], "repo": str(other)}],
+ }), encoding="utf-8")
+ env = {**os.environ, "LOOPX_RUNTIME_ROOT": str(runtime), "LOOPX_USAGE_PING": "0"}
+ env.pop("LOOPX_REGISTRY", None)
+ result = run_cli(
+ *entry, "--project", str(project), "--goal-id", "surface-goal",
+ "--agent-id", "probe-agent", cwd=other, env=env,
+ )
+ assert result.returncode == 0, result.stderr
+ activation = json.loads(result.stdout)["host_loop_activation"]
+ assert activation["activation_allowed"]
+ mutation = activation["host_mutation"]
+ sidecar = mutation["sidecar_template"]
+ prompt = sidecar["sidecar_json"]["args"][-1]
+ bootstrap, = [line for line in prompt.splitlines() if " heartbeat-prompt " in line]
+ for command in (bootstrap, activation["activation_input_command"]):
+ argv = shlex.split(command)
+ result = run_cli(*argv[1:], cwd=other, env=env)
+ assert result.returncode == 0, result.stdout + result.stderr
+ payload = json.loads(result.stdout)
+ assert payload["ok"]
+ guard = next(line for line in payload["task_body"].splitlines() if " quota should-run " in line)
+ guard_argv = shlex.split(guard)
+ assert Path(guard_argv[guard_argv.index("--registry") + 1]) == registry
+ hook = shlex.split(mutation["hooks_template"]["loopx"]["Stop"][0]["command"])
+ assert hook[hook.index("--registry") + 1] == str(registry)
+
+
def test_start_goal_accepts_the_agy_host_surface(tmp_path: Path) -> None:
payload = start_goal_accepts_surface(HOST_SURFACE, tmp_path)
activation = payload["command_pack"]["host_loop_activation"]
diff --git a/tests/test_cli_entrypoint.py b/tests/test_cli_entrypoint.py
index 8f6cd1092e..e71e2a9a04 100644
--- a/tests/test_cli_entrypoint.py
+++ b/tests/test_cli_entrypoint.py
@@ -661,7 +661,8 @@ def test_heartbeat_chain_routes_do_not_load_the_chat_layer(tmp_path, monkeypatch
initialize_canonical_source_fixture(runtime, "perf-goal", state_path=state, provider="sqlite")
argv = ["--registry", str(registry), "--runtime-root", str(runtime), "--format", "json", *command]
result = run_cli_main("loopx.entrypoint", argv,
- forbidden_modules=("loopx.cli", "loopx.chat_manager", "loopx.chat_agent", "loopx.chat_store"))
+ forbidden_modules=("loopx.cli", "loopx.chat_manager", "loopx.chat_agent", "loopx.chat_store",
+ "loopx.capabilities.issue_fix.pr_wait_reconcile"))
assert result.returncode == 0, result.stderr
assert json.loads(result.stdout)["ok"] is True
@@ -700,6 +701,10 @@ def test_capability_hook_imports_defer_unselected_facade_exports() -> None:
"issue_fix.periodic_report",
"issue_fix.pr_description",
"issue_fix.reward_memory",
+ "issue_fix.pr_wait_reconcile",
+ "issue_fix.pr_lifecycle",
+ "issue_fix.metadata_preview",
+ "content_ops.surface",
"reward_memory.codex_app_outcome",
"reward_memory.dogfood",
"reward_memory.evaluation",
@@ -719,6 +724,75 @@ def test_capability_hook_imports_defer_unselected_facade_exports() -> None:
assert completed.returncode == 0, completed.stderr
+@pytest.mark.parametrize("permission", ["denied", "revoked", "source-unavailable", "recheck-unavailable", "granted"])
+def test_pr_wait_provider_import_obeys_permission_and_failure_owner(permission):
+ script = f"""
+import importlib.abc
+import sys
+from pathlib import Path
+
+attempts = []
+class UnavailableProvider(importlib.abc.MetaPathFinder):
+ def find_spec(self, fullname, path=None, target=None):
+ if fullname == "loopx.capabilities.issue_fix.pr_wait_reconcile":
+ attempts.append(fullname)
+ raise ImportError("optional observation provider unavailable")
+
+sys.meta_path.insert(0, UnavailableProvider())
+from loopx.capabilities.issue_fix import pr_wait_hook
+
+permission = {permission!r}
+grant = ["external_evidence_poll"]
+states = iter({{
+ "denied": [[]], "revoked": [grant, []],
+ "source-unavailable": [OSError("permission source unavailable")] * 2,
+ "recheck-unavailable": [grant, OSError("permission source unavailable")],
+ "granted": [grant, grant],
+}}[permission])
+checks = []
+def availability(*args):
+ value = next(states)
+ checks.append(value)
+ if isinstance(value, Exception):
+ raise value
+ return {{"effective": value}}
+
+pr_wait_hook._availability = availability
+hook = pr_wait_hook.turn_start_hook(registry_path=Path("unused"), runtime_root=Path("unused"),
+ goal_id="fixture", agent_id="fixture", available=None)
+assert attempts == [], attempts
+if permission == "denied":
+ assert hook is None and len(checks) == 1
+else:
+ assert hook is not None
+ result = hook.producer()
+ assert len(checks) == 2
+ assert result["external_writes_performed"] is False
+ if permission == "granted":
+ assert len(attempts) == 1
+ assert result["status"] == "failed"
+ assert result["error_code"] == "dependency_observation_failed"
+ assert result["external_reads_performed"] is None
+ assert result["local_private_state_mutated"] is None
+ assert result["readback"]["external_read_count"] is None
+ assert result["readback"]["state_written"] is None
+ assert result["readback"]["error_category"] == "ImportError"
+ else:
+ assert attempts == []
+ assert result["external_reads_performed"] is False
+ assert result["local_private_state_mutated"] is False
+ assert result["status"] == ("not_applicable" if permission == "revoked" else "unavailable")
+ if permission != "revoked":
+ assert result["error_code"] == "capability_source_unavailable"
+ assert result["readback"]["external_read_count"] == 0
+for name in ("pr_wait_reconcile", "pr_lifecycle", "metadata_preview"):
+ assert "loopx.capabilities.issue_fix." + name not in sys.modules
+assert "loopx.capabilities.content_ops.surface" not in sys.modules
+"""
+ completed = run_isolated_script(script)
+ assert completed.returncode == 0, completed.stderr
+
+
def test_quota_facade_lazy_exports_preserve_owner_identity() -> None:
script = """
from importlib import import_module
diff --git a/tests/test_core_distribution.py b/tests/test_core_distribution.py
index fac7542fd6..9938c4f9e4 100644
--- a/tests/test_core_distribution.py
+++ b/tests/test_core_distribution.py
@@ -67,12 +67,36 @@ def test_core_staging_excludes_workspace_and_reuses_kernel_bytes(tmp_path):
@pytest.fixture
-def core_python():
+def core_python(tmp_path):
value = os.environ.get("LOOPX_CORE_TEST_PYTHON")
if not value:
pytest.skip("set LOOPX_CORE_TEST_PYTHON to a clean installed core environment")
executable = Path(value).absolute()
assert executable.is_file(), executable
+ # Every selected qualification caller must use the installed Core owner,
+ # including when the separate identity test was not selected.
+ script = '''import importlib.metadata as m,json,loopx
+from pathlib import Path
+installed=m.distribution('loopx-core')
+assert installed.version==loopx.__version__
+try:m.distribution('loopx')
+except m.PackageNotFoundError:pass
+else:raise AssertionError('the ordinary distribution must not be installed')
+direct_url=json.loads(installed.read_text('direct_url.json') or '{}')
+assert not direct_url.get('dir_info',{}).get('editable',False),'editable Core is not installed qualification'
+recorded=next((entry for entry in installed.files or () if entry.as_posix()=='loopx/__init__.py'),None)
+assert recorded is not None,'the installed Core RECORD must own loopx/__init__.py'
+package=Path(loopx.__file__).resolve()
+record_path=Path(installed.locate_file(recorded)).resolve()
+assert record_path==package,('imported namespace is not owned by installed Core',str(package),str(record_path))
+assert package.is_relative_to(Path(installed.locate_file('')).resolve()),'Core namespace resolves outside its installed location'
+'''
+ probe = subprocess.run([str(executable), "-I", "-c", script], cwd=tmp_path,
+ env=_environment(str(executable)), capture_output=True, text=True, timeout=30)
+ assert probe.returncode == 0, (
+ "LOOPX_CORE_TEST_PYTHON must select a clean installed loopx-core distribution",
+ probe.stdout, probe.stderr,
+ )
return str(executable)
@@ -104,12 +128,8 @@ def _run(python, arguments, cwd, *, environment=None, expected=0):
def test_installed_core_identity_help_and_unavailable_workspace(core_python, tmp_path):
- script = '''import importlib.metadata as m,importlib.util as u,json,loopx
+ script = '''import importlib.util as u,json,loopx
from pathlib import Path
-assert m.version('loopx-core')==loopx.__version__
-try:m.distribution('loopx')
-except m.PackageNotFoundError:pass
-else:raise AssertionError('the ordinary distribution must not be installed')
for name in ('loopx.cli','loopx.chat','loopx.web','loopx.canary'):assert u.find_spec(name) is None,name
from loopx.release_manifest import _distribution_runtime_fingerprint
assert _distribution_runtime_fingerprint() is not None
diff --git a/tests/test_lark_private_conversations.py b/tests/test_lark_private_conversations.py
index 339dc618ad..1c9d0c59bf 100644
--- a/tests/test_lark_private_conversations.py
+++ b/tests/test_lark_private_conversations.py
@@ -7,6 +7,7 @@
import threading
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
+from io import BytesIO
from types import SimpleNamespace
import pytest
@@ -1016,6 +1017,679 @@ def connect(fixture):
runner=provider, cli_bin="lark-cli")
+@pytest.fixture
+def source_https(monkeypatch):
+ from loopx.extensions import public_source_reader
+
+ requests, resolutions = [], []
+
+ def resolve(host):
+ resolutions.append(host)
+ return ["8.8.8.8"]
+
+ def serve(response_for):
+ class Socket:
+ def settimeout(self, value):
+ pass
+
+ def makefile(self, *args):
+ return BytesIO(self.wire_response)
+
+ class Connection:
+ def __init__(self, host, address, timeout):
+ self.host = host
+ self.sock = Socket()
+
+ def request(self, method, path, *, headers):
+ assert method == "GET"
+ assert set(headers) == {"Accept-Encoding", "User-Agent"}
+ url = f"https://{self.host}{path}"
+ requests.append(url)
+ redirect, mime, body = response_for(url)
+ if redirect:
+ self.sock.wire_response = f"HTTP/1.1 302 Found\r\nLocation: {redirect}\r\nContent-Length: 0\r\n\r\n".encode()
+ else:
+ headers = f"HTTP/1.1 200 OK\r\nContent-Type: {mime}\r\nContent-Length: {len(body)}\r\n\r\n"
+ self.sock.wire_response = headers.encode() + body
+
+ def getresponse(self):
+ response = http.client.HTTPResponse(self.sock)
+ response.begin()
+ return response
+
+ def close(self):
+ pass
+
+ monkeypatch.setattr(public_source_reader, "public_addresses", resolve)
+ monkeypatch.setattr(public_source_reader, "_PinnedHTTPS", Connection)
+ return requests, resolutions
+
+ return serve
+
+
+@pytest.mark.parametrize("fault", ["none", "foreign_source", "unlisted_image", "changed_request",
+ "corrupt_readback", "legacy_cached_readback", "upload_denied", "listed_redirect", "same_host_redirect",
+ "foreign_redirect", "foreign_round_trip", "query_redirect", "article_redirect", "stopped", "stopped_after_fetch",
+ "closed_on_upload_preview", "stopped_on_upload_source_read", "closed_on_upload_source_read",
+ "closed_on_source_controls_read", "stopped_before_upload_intent", "closed_before_upload_intent",
+ "edited_after_upload_intent"])
+def test_private_completed_answer_returns_image_bytes_instead_of_a_remote_placeholder(ordinary, source_https, monkeypatch, fault): # noqa: F811
+ from test_chat_image_attachments import PNG_BYTES
+ from test_lark_app_image_projection import ImageIO
+
+ store, runtime, provider, transport = connect(ordinary)
+ capture, fake = ordinary[4:6]
+ url = ("https://unrelated.example" if fault == "foreign_source" else "https://source.example") + "/chart.png"
+ authored = f'Original explanation.\n\n'
+ script = fake.read_text()
+ default = '"message": "Runtime response.",'
+ assert default in script
+ fake.write_text(script.replace(default,
+ f'"message": {authored!r} if turn_count == 1 else "Next original reply.",', 1))
+ article_url = "https://source.example/article"
+ stopped = fault in ("stopped", "stopped_after_fetch", "stopped_on_upload_source_read", "stopped_before_upload_intent")
+ closed = fault in ("closed_on_upload_preview", "closed_on_upload_source_read", "closed_on_source_controls_read",
+ "closed_before_upload_intent")
+ redirect_targets = {
+ "listed_redirect": "/canonical.png",
+ "same_host_redirect": "/unlisted.png",
+ "foreign_redirect": "https://unrelated.example/foreign.png",
+ "foreign_round_trip": "https://unrelated.example/foreign.png",
+ "query_redirect": "/chart.png?private=fixture",
+ }
+
+ def respond(target):
+ if target == article_url and fault == "article_redirect":
+ return "/other-article", None, b""
+ if target in (article_url, "https://source.example/other-article"):
+ basename = "different-chart.png" if fault == "unlisted_image" else "chart.png"
+ listings = f'
'
+ if fault == "listed_redirect":
+ listings += '
'
+ return None, "text/html", listings.encode()
+ if target == url and fault in redirect_targets:
+ return redirect_targets[fault], None, b""
+ if target == "https://unrelated.example/foreign.png" and fault == "foreign_round_trip":
+ return "https://source.example/unlisted.png", None, b""
+ if target == url and fault == "stopped_after_fetch":
+ transport.admit("notes-app", provider.event("notes-app", "stop-during-image-fetch", "/stop"))
+ return None, "image/png", PNG_BYTES
+
+ fetched, resolutions = source_https(respond)
+ images_delivered = fault in ("none", "listed_redirect")
+ io = ImageIO(provider)
+ io.corrupt_download = fault in ("corrupt_readback", "legacy_cached_readback")
+ upload_previewed = False
+ control_committed = False
+ source_edited_after_intent = False
+ if fault == "edited_after_upload_intent":
+ from loopx.extensions.lark import return_files
+
+ write_receipt = return_files._write
+
+ def edit_after_upload_intent(path, value):
+ nonlocal source_edited_after_intent
+ write_receipt(path, value)
+ if value.get("upload_started") and not source_edited_after_intent:
+ assert json.loads(path.read_text())["upload_started"] is True
+ provider.messages[source["message_id"]]["content"] = "The source changed after upload intent."
+ source_edited_after_intent = True
+
+ monkeypatch.setattr(return_files, "_write", edit_after_upload_intent)
+
+ if fault in ("stopped_before_upload_intent", "closed_before_upload_intent"):
+ from loopx.extensions.lark import return_files
+ write_receipt = return_files._write
+
+ def control_before_upload_intent(path, value):
+ nonlocal control_committed
+ if value.get("upload_started") and not control_committed:
+ control_committed = True
+ if stopped:
+ transport.admit("notes-app", provider.event("notes-app", "stop-before-resource-intent", "/stop"))
+ assert any(row.get("command") == "stop" for row in transport.core.source_controls())
+ else:
+ assert runtime.close_session(sid)
+ assert store.load_session(sid)["status"] == "closed"
+ return write_receipt(path, value)
+
+ monkeypatch.setattr(return_files, "_write", control_before_upload_intent)
+
+ if fault == "closed_on_source_controls_read":
+ read_controls = transport.core.source_controls
+
+ def close_during_control_read():
+ nonlocal control_committed
+ if upload_previewed and not control_committed:
+ control_committed = True
+ assert runtime.close_session(sid)
+ assert store.load_session(sid)["status"] == "closed"
+ return read_controls()
+
+ monkeypatch.setattr(transport.core, "source_controls", close_during_control_read)
+
+ def media(args, cwd=None, timeout=None):
+ nonlocal upload_previewed, control_committed
+ if "images" in args and "create" in args and "--dry-run" in args:
+ upload_previewed = True
+ if fault == "closed_on_upload_preview":
+ runtime.close_session(sid)
+ if (fault in ("stopped_on_upload_source_read", "closed_on_upload_source_read")
+ and upload_previewed and not control_committed and (
+ ("+messages-mget" in args and source["message_id"] in args) or
+ f"/open-apis/im/v1/messages/{source['message_id']}" in args)):
+ control_committed = True
+ if stopped:
+ transport.admit("notes-app", provider.event("notes-app", "stop-during-source-read", "/stop"))
+ assert any(control.get("command") == "stop" for control in transport.core.source_controls())
+ else:
+ runtime.close_session(sid)
+ assert store.load_session(sid)["status"] == "closed"
+ if fault == "upload_denied" and "images" in args and "create" in args and "--dry-run" not in args:
+ return {"returncode": 3, "stderr": '{"ok":false,"error":{"code":99991672}}'}
+ return io(args, cwd, timeout)
+
+ transport.runner = media
+ try:
+ source = provider.event("notes-app", "answer-with-original-image", "Read https://source.example/article")
+ transport.admit("notes-app", source)
+ original = transport.core.pending()[0]
+ sid, tid, ref = (original[key] for key in ("session_id", "turn_id", "request_ref"))
+ completed = runtime.wait_for_turn(session_id=sid, turn_id=tid, timeout_sec=10)
+ assert completed["status"] == "completed"
+ if fault == "changed_request":
+ provider.messages[source["message_id"]]["content"] = "Source URL removed."
+ path = transport.root / f"{ref}.json"
+ assert path in transport.pending_body_paths()
+ assert transport.reconcile_request(path) == 0
+ text_only = json.loads(path.read_text())
+ assert text_only["deliveries"]["terminal"]["verified"] is True
+ assert transport.core.read_request(ref).get("delivery_verified", False) is False
+ assert fetched == [] and io.uploads == 0
+ assert path not in transport.pending_body_paths()
+ assert path in transport.pending_delivery_paths() and transport.is_recovery_path(path)
+ if fault == "stopped":
+ transport.admit("notes-app", provider.event("notes-app", "stop-before-image-work", "/stop"))
+ assert transport.reconcile_request(path) == int(images_delivered)
+ saved = json.loads(path.read_text())
+ assert saved["answer_images"]["status"] == ("delivered" if images_delivered else "unverified")
+ out = [(mid, row, json.loads(row["body"]["content"])["zh_cn"]["content"])
+ for mid, row in provider.messages.items() if mid.startswith("om_out_") and row["msg_type"] == "post"]
+ assert all(row["chat_id"] == source["chat_id"] for _, row, _ in out)
+ assert any("[Source chart](" in nodes[0][0]["text"] for _, _, nodes in out)
+ assert all(not any("![Source chart]" in node.get("text", "") for line in nodes for node in line) for _, _, nodes in out)
+ if fault in ("stopped_on_upload_source_read", "closed_on_upload_source_read", "closed_on_source_controls_read",
+ "stopped_before_upload_intent", "closed_before_upload_intent"):
+ assert control_committed
+ if fault in ("stopped_before_upload_intent", "closed_before_upload_intent"):
+ from loopx.extensions.lark.return_files import _root
+ assert list((_root(transport.runtime_root) / "lark-chat-images").glob("*.json")) == []
+ if fault == "edited_after_upload_intent":
+ from loopx.extensions.lark.return_files import _root
+
+ assert source_edited_after_intent and io.uploads == 0
+ assert list((_root(transport.runtime_root) / "lark-chat-images").glob("*.json")) == []
+ assert "answer_images.terminal" not in saved["deliveries"]
+ if stopped or closed:
+ assert io.uploads == 0 and transport.core.read_request(ref).get("delivery_verified", False) is False
+ assert not any(name.startswith(("answer_images.", "answer_image_notice.")) for name in saved["deliveries"])
+ if stopped:
+ assert any(control.get("command") == "stop" for control in transport.core.source_controls())
+ else:
+ assert store.load_session(sid)["status"] == "closed"
+ assert transport.core.read_request(ref).get("delivery_verified", False) is images_delivered
+ native_images = [node["image_key"] for _, _, nodes in out for line in nodes for node in line if node["tag"] == "img"]
+ assert native_images == list(io.resources)
+ assert all(io.resources[k] == PNG_BYTES for k in native_images)
+ expected_requests = {
+ "foreign_source": [], "changed_request": [], "stopped": [], "unlisted_image": [article_url],
+ "article_redirect": [article_url], "listed_redirect": [article_url, url, "https://source.example/canonical.png"],
+ }.get(fault, [article_url, url])
+ assert fetched == expected_requests
+ assert resolutions == ["source.example"] * len(expected_requests)
+ if fault in {"same_host_redirect", "foreign_redirect", "foreign_round_trip", "query_redirect", "article_redirect"}:
+ assert io.uploads == 0 and native_images == []
+ assert ("图片尚未核验送达" in str(out)) == (not images_delivered and not stopped and not closed)
+ if fault in ("corrupt_readback", "legacy_cached_readback"):
+ io.corrupt_download = False
+ from loopx.chat_store import _atomic_write_json
+ restarted = LarkPrivateConversations(controller=runtime, runtime_root=transport.runtime_root,
+ runner=media, cli_bin="lark-cli")
+ writes = list(provider.writes)
+ assert path not in restarted.pending_body_paths()
+ assert path not in restarted.pending_delivery_paths()
+ assert restarted.reconcile_request(path) == 0
+ assert provider.writes == writes and io.uploads == 1 and fetched == [article_url, url]
+ saved["answer_images"]["next_check_at"] = 0
+ if fault == "legacy_cached_readback":
+ saved["answer_images"].pop("source_urls_verified")
+ _atomic_write_json(path, saved)
+ assert path in restarted.pending_delivery_paths()
+ assert restarted.reconcile_request(path) == 1
+ assert transport.core.read_request(ref)["delivery_verified"] is True
+ assert provider.writes == writes and io.uploads == 1
+ assert fetched == [article_url, url] * (2 if fault == "legacy_cached_readback" else 1)
+ expected_turns = 1
+ if fault == "upload_denied":
+ followup = provider.event("notes-app", "text-after-image-denial", "Answer the next question")
+ retry = transport.admit("notes-app", followup)
+ assert retry["status"] == "durably_accepted"
+ later_ref = identity_ref(transport._binding("notes-app")["provider_ref"], followup["message_id"])
+ later = transport.core.read_request(later_ref)
+ assert later["session_id"] == sid
+ assert runtime.wait_for_turn(session_id=sid, turn_id=later["turn_id"], timeout_sec=10)["status"] == "completed"
+ later_path = transport.root / f"{later_ref}.json"
+ assert path not in transport.pending_body_paths() and path not in transport.pending_delivery_paths()
+ assert later_path in transport.pending_body_paths()
+ assert transport.reconcile_request(later_path) == 1
+ assert transport.core.read_request(later_ref)["delivery_verified"] is True
+ assert transport.core.read_request(ref).get("delivery_verified", False) is False
+ expected_turns = 2
+ packets = [json.loads(line) for line in capture.read_text().splitlines()]
+ assert sum(packet.get("method") == "turn/start" for packet in packets) == expected_turns
+ assert any(row["text"].endswith(authored) for row in store.messages(sid) if row["role"] == "agent")
+ finally:
+ runtime.close()
+
+
+@pytest.mark.parametrize("fault", ["readback", "raw_paired_post", "edited_source", "edited_original_reply", "edit_on_image_get",
+ "edit_on_upload_source_read", "edit_on_next_upload_source_read", "edit_on_send_preview", "edit_on_send_source_read",
+ "edit_reply_after_upload_intent", "edit_source_on_final_pair_read", "edit_source_on_dispatch_lock",
+ "foreign_sender", "changed_bytes", "stop", "unknown_send", "upload_denied", "upload_scope_then_granted",
+ "upload_scope_with_conflicting_stdout", "upload_cli_missing", "upload_cli_fd_limit", "upload_ack_missing",
+ "closed_on_source_controls_read",
+ "unlisted_redirect", "unlisted_cached_redirect"])
+def test_original_worker_queues_only_media_for_a_verified_legacy_answer(ordinary, monkeypatch, source_https, fault): # noqa: F811
+ from hashlib import sha256
+ from loopx.chat_store import _atomic_write_json
+ from test_chat_image_attachments import PNG_BYTES
+ from test_lark_app_image_projection import ImageIO
+
+ resource = pytest.importorskip("resource") if fault == "upload_cli_fd_limit" else None
+ store, runtime, provider, transport = connect(ordinary)
+ fake = ordinary[5]
+ origin = "https://source.example/article"
+ urls = [f"https://source.example/figure-{i}.png" for i in range(1, 5)]
+ figures = {url: PNG_BYTES + bytes([i]) for i, url in enumerate(urls)}
+ answer = f"Original explanation.\n\n\n\n"
+ script = fake.read_text()
+ marker = '"message": "Runtime response.",'
+ assert marker in script
+ fake.write_text(script.replace(marker, f'"message": {answer!r},', 1))
+ io = ImageIO(provider)
+ edited_body = None
+ late_source_edit = False
+ committed_resource_path = None
+ upload_previews = 0
+ send_preview_seen = False
+ denied_uploads = 0
+ scope_granted = False
+ executable_restored = False
+
+ def edit_original_reply():
+ nonlocal edited_body
+ edited_body = {"content": json.dumps({"zh_cn": {"content": [[{"tag": "text", "text": "The original answer was edited."}]]}})}
+ provider.messages[prior_id]["body"] = edited_body
+
+ def provider_io(args, cwd=None, timeout=None):
+ nonlocal upload_previews, send_preview_seen, denied_uploads, late_source_edit
+ image_upload = "images" in args and "create" in args and "--dry-run" not in args
+ pair_read = "/open-apis/im/v1/messages/mget" in args and "--params" in args
+ source_read = (("+messages-mget" in args and event["message_id"] in args)
+ or (pair_read and event["message_id"] in json.loads(args[args.index("--params") + 1])["message_ids"]))
+ if fault == "upload_denied" and image_upload:
+ provider.calls.append(list(args))
+ return {"returncode": 3, "stderr": '{"ok":false,"error":{"type":"authorization","code":99991672}}'}
+ typed_scope_fault = fault in ("upload_scope_then_granted", "upload_scope_with_conflicting_stdout")
+ if image_upload and typed_scope_fault and io.uploads == 1 and not scope_granted:
+ provider.calls.append(list(args))
+ denied_uploads += 1
+ return {"returncode": 3,
+ "stderr": json.dumps({"ok": False, "identity": "bot", "error": {
+ "type": "authorization", "subtype": "app_scope_not_applied", "code": 99991672,
+ "identity": "bot"}}),
+ "stdout": "invalid provider acknowledgement" if fault == "upload_scope_with_conflicting_stdout" else ""}
+ if (fault == "edit_source_on_final_pair_read" and committed_resource_path is not None
+ and not late_source_edit
+ and (("+messages-mget" in args and prior_id in args)
+ or pair_read)):
+ assert json.loads(committed_resource_path.read_text())["upload_started"] is True
+ provider.messages[event["message_id"]]["content"] = "The source changed during the final reply read."
+ late_source_edit = True
+ uploads_before_edit = {"edit_on_upload_source_read": 0, "edit_on_next_upload_source_read": 1}
+ if (fault in uploads_before_edit and upload_previews == uploads_before_edit[fault] + 1 and edited_body is None
+ and io.uploads == uploads_before_edit[fault]
+ and source_read):
+ edit_original_reply()
+ if (fault == "edit_on_send_source_read" and send_preview_seen and edited_body is None
+ and source_read):
+ edit_original_reply()
+ result = io(args, cwd, timeout)
+ if pair_read and fault == "raw_paired_post":
+ body = json.loads(result["stdout"])
+ historical = next(row for row in body["data"]["items"] if row["message_id"] == prior_id)
+ original_rows = json.loads(historical["body"]["content"])["zh_cn"]["content"]
+ historical["body"] = {"content": json.dumps({
+ "title": "", "content_v2": original_rows,
+ "content": [[{"tag": "text", "text": "A rendered preview only."}]],
+ })}
+ result = {**result, "stdout": json.dumps(body)}
+ if "images" in args and "create" in args and "--dry-run" in args:
+ upload_previews += 1
+ if "+messages-send" in args and "--dry-run" in args and "--content" in args:
+ if "原图(按原文顺序)" in args[args.index("--content") + 1]:
+ send_preview_seen = True
+ if fault == "edit_on_send_preview":
+ edit_original_reply()
+ return result
+
+ runner = provider_io
+ launch_faults = {"upload_cli_missing", "upload_cli_fd_limit"}
+ upload_faults = launch_faults | {"upload_ack_missing"}
+ if fault in upload_faults:
+ from loopx.extensions.lark.cli_resolution import LarkCliResolution, build_lark_command_runner
+
+ native_runner = build_lark_command_runner(LarkCliResolution(sys.executable, True, "fixture", None, None))
+ missing_executable = str(fake.with_name("missing-lark-cli"))
+ if resource is not None:
+ import errno
+ import subprocess
+
+ popen = subprocess.Popen
+
+ def spawn(command, *args, **kwargs):
+ if command != [sys.executable, "-c", "pass"] or executable_restored:
+ return popen(command, *args, **kwargs)
+ # Exhaust descriptors only at real Popen pipe creation,
+ # after the source check and durable upload marker.
+ limits = resource.getrlimit(resource.RLIMIT_NOFILE)
+ try:
+ resource.setrlimit(resource.RLIMIT_NOFILE, (0, limits[1]))
+ return popen(command, *args, **kwargs)
+ except OSError as exc:
+ assert exc.errno == errno.EMFILE
+ raise
+ finally:
+ resource.setrlimit(resource.RLIMIT_NOFILE, limits)
+
+ monkeypatch.setattr(subprocess, "Popen", spawn)
+
+ def guarded_provider(args, cwd=None, timeout=None, *, dispatch_guard=None):
+ if dispatch_guard is not None:
+ # Use the real subprocess boundary with a synthetic provider:
+ # an unavailable executable cannot reach resource creation.
+ command = ([missing_executable] if fault == "upload_cli_missing" and not executable_restored
+ else [sys.executable, "-c", "pass"])
+ assert native_runner(command, cwd, timeout, dispatch_guard=dispatch_guard)["returncode"] == 0
+ return provider_io(args, cwd, timeout)
+
+ guarded_provider.dispatch_guard = True
+ runner = guarded_provider
+
+ transport.runner = runner
+
+ unlisted = "https://source.example/unlisted.png"
+
+ def respond(url):
+ if url == origin:
+ return None, "text/html", "".join(f'
' for url in urls).encode()
+ if fault == "edit_on_image_get":
+ edit_original_reply()
+ if fault in ("unlisted_redirect", "unlisted_cached_redirect") and url == urls[-1]:
+ return "/unlisted.png", None, b""
+ return None, "image/png", figures[urls[-1] if url == unlisted else url]
+
+ fetched, resolutions = source_https(respond)
+
+ def restart():
+ return LarkPrivateConversations(controller=runtime, runtime_root=transport.runtime_root,
+ runner=runner, cli_bin="lark-cli")
+
+ try:
+ event = provider.event("notes-app", "historical-image-answer", f"Read {origin}")
+ assert transport.admit("notes-app", event)["status"] == "durably_accepted"
+ original = transport.core.pending()[0]
+ sid, tid, ref = (original[key] for key in ("session_id", "turn_id", "request_ref"))
+ assert runtime.wait_for_turn(session_id=sid, turn_id=tid, timeout_sec=10)["status"] == "completed"
+ path = transport.root / f"{ref}.json"
+ # Produce an actual legacy reply and provider receipt before installing
+ # the media-only repair. The stored answer and Core are real.
+ with monkeypatch.context() as legacy:
+ legacy.setattr(transport, "_deliver_completed_answer", lambda p, row, text, *, inbox:
+ transport._deliver(p, row, "terminal", text, inbox=inbox))
+ assert transport.reconcile_request(path) == 1
+ saved = json.loads(path.read_text())
+ terminal = saved["deliveries"]["terminal"]
+ prior_id = terminal["attempt"]["message_ref"]
+ prior_body = json.dumps(provider.messages[prior_id]["body"])
+ core = (transport.core.read_request(ref), store.load_turn(sid, tid), store.messages(sid))
+ writes = len(provider.writes)
+ worker = restart()
+ assert path not in worker.pending_delivery_paths()
+ assert worker.reconcile_request(path) == 0 and io.uploads == 0
+
+ if fault == "closed_on_source_controls_read":
+ read_controls = worker.core.source_controls
+
+ def close_during_control_read():
+ if upload_previews and store.load_session(sid)["status"] != "closed":
+ assert runtime.close_session(sid)
+ return read_controls()
+
+ monkeypatch.setattr(worker.core, "source_controls", close_during_control_read)
+
+ plan = {"binding_id": saved["binding_id"], "turn_id": tid, "message_id": prior_id,
+ "answer_sha256": sha256(answer.encode()).hexdigest(),
+ "images": [{"url": url, "sha256": sha256(figures[url]).hexdigest()} for url in urls]}
+ manifest = worker.root / "historical-answer-images" / path.name
+ _atomic_write_json(manifest, plan)
+ if fault == "unlisted_cached_redirect":
+ import base64
+ from loopx.chat_attachments import normalize_chat_image_attachments
+
+ legacy_attachments = normalize_chat_image_attachments([
+ {"id": f"reply-image-{index}", "name": f"original-{index}.png", "mime_type": "image/png",
+ "data_url": "data:image/png;base64," + base64.b64encode(data).decode(), "size": len(data)}
+ for index, data in enumerate(figures.values(), 1)])
+ saved["historical_answer_images"] = {"plan": plan, "status": "pending", "attachments": legacy_attachments}
+ _atomic_write_json(path, saved)
+ assert path not in worker.pending_body_paths()
+ assert path in worker.pending_delivery_paths() and worker.is_recovery_path(path)
+ if fault == "edited_source":
+ provider.messages[event["message_id"]]["content"] = "Changed source."
+ elif fault == "edited_original_reply":
+ edit_original_reply()
+ elif fault == "foreign_sender":
+ provider.messages[prior_id]["sender"]["id"] = provider.profile_apps["steward-app"]
+ elif fault == "changed_bytes":
+ figures[urls[-1]] = PNG_BYTES
+ elif fault == "stop":
+ # The authoritative Stop predates any possible file recovery.
+ control = {**core[0], "command": "stop"}
+ _atomic_write_json(transport.core.root / ("d" * 24 + ".json"), control)
+ elif fault == "readback":
+ io.corrupt_download = True
+ elif fault == "unknown_send":
+ provider.lose_next_reply = True
+ elif fault == "upload_ack_missing":
+ io.unknown_upload = True
+ elif fault in {"edit_reply_after_upload_intent", "edit_source_on_final_pair_read", "edit_source_on_dispatch_lock"}:
+ from loopx.extensions.lark import return_files
+
+ write_receipt = return_files._write
+
+ def edit_after_upload_intent(path, value):
+ nonlocal committed_resource_path
+ write_receipt(path, value)
+ if value.get("upload_started") and committed_resource_path is None:
+ committed_resource_path = path
+ if fault == "edit_reply_after_upload_intent":
+ assert json.loads(path.read_text())["upload_started"] is True
+ edit_original_reply()
+
+ monkeypatch.setattr(return_files, "_write", edit_after_upload_intent)
+ if fault == "edit_source_on_dispatch_lock":
+ from loopx.extensions.lark import private_delivery_discovery
+
+ enter_lock = private_delivery_discovery.try_exclusive_file_lock
+
+ def edit_source_before_lock(path, **kwargs):
+ nonlocal late_source_edit
+ if kwargs.get("operation") == "dispatch_external_answer_media" and not late_source_edit:
+ assert json.loads(committed_resource_path.read_text())["upload_started"] is True
+ provider.messages[event["message_id"]]["content"] = "The original changed before the dispatch lock."
+ late_source_edit = True
+ return enter_lock(path, **kwargs)
+
+ monkeypatch.setattr(private_delivery_discovery, "try_exclusive_file_lock", edit_source_before_lock)
+
+ if fault == "raw_paired_post":
+ assert worker.reconcile_request(path) == 1
+ assert io.uploads == 4 and len(provider.writes) == writes + 1
+ assert json.loads(path.read_text())["deliveries"]["answer_images.historical"]["images_verified"] is True
+ assert worker.reconcile_request(path) == 0 and io.uploads == 4
+ assert core == (transport.core.read_request(ref), store.load_turn(sid, tid), store.messages(sid))
+ return
+ assert worker.reconcile_request(path) == 0
+ if fault == "closed_on_source_controls_read":
+ assert upload_previews == 1 and store.load_session(sid)["status"] == "closed"
+ if fault in upload_faults:
+ # Only the attempt that could not start is safe to retry.
+ assert io.uploads == int(fault == "upload_ack_missing")
+ assert len(provider.writes) == writes
+ pending = json.loads(path.read_text())
+ assert pending["historical_answer_images"]["status"] == "pending"
+ assert "answer_images.historical" not in pending["deliveries"]
+ pending["historical_answer_images"]["next_check_at"] = 0
+ _atomic_write_json(path, pending)
+ executable_restored = True
+ io.unknown_upload = False
+ worker = restart()
+ recovered = fault in launch_faults
+ assert worker.reconcile_request(path) == int(recovered)
+ post_state = json.loads(path.read_text())
+ assert post_state["historical_answer_images"]["status"] == ("delivered" if recovered else "pending")
+ if recovered:
+ media = post_state["deliveries"]["answer_images.historical"]
+ post = provider.messages[media["attempt"]["message_ref"]]
+ assert post["chat_id"] == event["chat_id"]
+ assert post["sender"]["id"] == provider.profile_apps["notes-app"]
+ assert media["images_verified"] and io.downloads == 4
+ assert [io.resources[key] for key in media["image_keys"]] == list(figures.values())
+ assert worker.reconcile_request(path) == 0
+ assert io.uploads == (4 if recovered else 1)
+ assert len(provider.writes) == writes + int(recovered)
+ elif fault in {"readback", "unknown_send", "upload_scope_then_granted", "upload_scope_with_conflicting_stdout"}:
+ pending = json.loads(path.read_text())
+ assert pending["historical_answer_images"]["status"] == "pending"
+ if fault.startswith("upload_scope_"):
+ assert denied_uploads == 1 and io.uploads == 1 and len(provider.writes) == writes
+ assert "answer_images.historical" not in pending["deliveries"]
+ scope_granted = True
+ else:
+ assert io.uploads == 4 and len(provider.writes) == writes + 1
+ worker = restart()
+ assert path not in worker.pending_delivery_paths()
+ pending["historical_answer_images"]["next_check_at"] = 0
+ _atomic_write_json(path, pending)
+ io.corrupt_download = False
+ assert path in worker.pending_delivery_paths()
+ delivered = fault in ("readback", "upload_scope_then_granted")
+ assert worker.reconcile_request(path) == int(delivered)
+ if fault == "upload_scope_with_conflicting_stdout":
+ assert denied_uploads == 1 and io.uploads == 1 and len(provider.writes) == writes
+ assert json.loads(path.read_text())["historical_answer_images"]["status"] == "pending"
+ return
+ assert io.uploads == 4 and len(provider.writes) == writes + 1
+ row = json.loads(path.read_text())
+ assert row["historical_answer_images"]["status"] == ("delivered" if delivered else "pending")
+ outputs = [m for m in provider.messages.values() if m["msg_type"] == "post"
+ and "原图(按原文顺序)" in m["body"]["content"]]
+ assert len(outputs) == 1
+ post = outputs[0]
+ assert post["chat_id"] == event["chat_id"] and post["sender"]["id"] == provider.profile_apps["notes-app"]
+ lines = json.loads(post["body"]["content"])["zh_cn"]["content"]
+ keys = [node["image_key"] for line in lines for node in line if node["tag"] == "img"]
+ assert len(keys) == 4 and [io.resources[key] for key in keys] == list(figures.values())
+ assert worker.reconcile_request(path) == 0
+ else:
+ expected_uploads = {"edit_on_send_preview": 4, "edit_on_send_source_read": 4,
+ "edit_on_next_upload_source_read": 1}
+ assert io.uploads == expected_uploads.get(fault, 0) and len(provider.writes) == writes
+ if fault == "edit_reply_after_upload_intent":
+ from loopx.extensions.lark.return_files import _root
+
+ assert edited_body is not None
+ assert list((_root(worker.runtime_root) / "lark-chat-images").glob("*.json")) == []
+ if fault in {"edit_source_on_final_pair_read", "edit_source_on_dispatch_lock"}:
+ assert late_source_edit and not committed_resource_path.exists()
+ assert "answer_images.historical" not in json.loads(path.read_text())["deliveries"]
+ if fault in {"edit_on_upload_source_read", "edit_on_next_upload_source_read", "edit_on_send_source_read",
+ "edit_reply_after_upload_intent"}:
+ assert edited_body is not None
+ pending = json.loads(path.read_text())
+ pending["historical_answer_images"]["next_check_at"] = 0
+ _atomic_write_json(path, pending)
+ assert restart().reconcile_request(path) == 0
+ assert io.uploads == expected_uploads.get(fault, 0) and len(provider.writes) == writes
+ if fault in ("unlisted_redirect", "unlisted_cached_redirect"):
+ assert fetched == [origin, *urls] and resolutions == ["source.example"] * len(fetched)
+ assert unlisted not in fetched
+ if fault == "upload_denied":
+ pending = json.loads(path.read_text())
+ assert pending["historical_answer_images"]["status"] == "pending"
+ assert "answer_images.historical" not in pending["deliveries"]
+ assert any("images" in cmd and "create" in cmd and "--dry-run" not in cmd for cmd in provider.calls)
+ assert provider.messages[prior_id]["body"] == (edited_body if edited_body is not None else json.loads(prior_body))
+ assert json.loads(path.read_text())["deliveries"]["terminal"] == terminal
+ assert core == (transport.core.read_request(ref), store.load_turn(sid, tid), store.messages(sid))
+ assert sum(json.loads(line).get("method") == "turn/start" for line in ordinary[4].read_text().splitlines()) == 1
+ finally:
+ runtime.close()
+
+
+def test_private_delivery_keeps_retired_steward_cards_out_of_image_recovery(
+ ordinary, # noqa: F811
+):
+ from loopx.chat_store import _atomic_write_json
+
+ store, runtime, provider, transport = connect(ordinary)
+ bindings = transport.bindings
+ workspace = runtime.project_contexts.available()[0]["project_ref"]
+ binding = bindings.configure(transport_ref="steward-app", project_ref=workspace,
+ executor_endpoint_id="codex", context_kind="steward")
+ try:
+ event = provider.event("steward-app", "retired-card-before-image-repair", "/triage")
+ assert transport.admit("steward-app", event)["status"] == "triage_queued"
+ ref = identity_ref(binding["provider_ref"], event["message_id"])
+ card = transport.root / f"{ref}.json"
+ assert card in transport.pending_body_paths()
+ assert card in transport.pending_delivery_paths()
+ assert transport.health()[binding["binding_id"]]["pending_count"] == 1
+
+ provider.messages[event["message_id"]]["content"] = "/status"
+ assert transport.reconcile_request(card) == 0
+ retired = json.loads(card.read_text())
+ assert retired["triage"]["retired"] == "original_source_changed"
+
+ # Even an orphaned descriptor with this filename cannot turn an
+ # upstream transport-only record into image or model recovery work.
+ _atomic_write_json(transport.root / "historical-answer-images" / card.name, {})
+ worker = LarkPrivateConversations(controller=runtime, runtime_root=transport.runtime_root,
+ runner=provider, cli_bin="lark-cli")
+ assert card not in worker.pending_body_paths()
+ assert card not in worker.pending_delivery_paths()
+ assert worker.health()[binding["binding_id"]]["pending_count"] == 0
+ assert not (worker.core.root / card.name).exists()
+
+ assert worker.reconcile_request(card) == 0
+ assert json.loads(card.read_text()) == retired
+ assert not store.list_sessions()
+ assert not provider.writes
+ finally:
+ runtime.close()
+
+
@pytest.mark.parametrize("case", ["normal", "invalid-metadata", "metadata-only", "unfinished", "source-changed", "closed", "lost-ack", "message-crash",
"historical", "historical-source-changed", "historical-stop"])
def test_original_worker_recovers_native_result_without_replay(tmp_path, language_server, monkeypatch, case): # noqa: F811
@@ -2362,6 +3036,7 @@ def counted_revision():
def test_settings_health_reuses_core_correlation_but_observes_current_work(tmp_path, monkeypatch):
from loopx.capabilities.native_chat import external_conversations
from loopx.chat_store import _atomic_write_json
+ from loopx.extensions.lark import private_conversations
from test_private_chat_backlog_projection import fixture_history
store, core, transport, rows = fixture_history(tmp_path, count=2)
@@ -2381,19 +3056,29 @@ def test_settings_health_reuses_core_correlation_but_observes_current_work(tmp_p
_atomic_write_json(partial_path, {**initial_partial, "deliveries": {
"terminal": {"started": True, "verified": False}}})
read_request = external_conversations._read_json
+ read_delivery = private_conversations._read_json
request_reads = []
+ delivery_reads = []
def observe(path):
request_reads.append(path)
return read_request(path)
+ def observe_delivery(path):
+ delivery_reads.append(path)
+ return read_delivery(path)
+
monkeypatch.setattr(external_conversations, "_read_json", observe)
+ monkeypatch.setattr(private_conversations, "_read_json", observe_delivery)
assert transport.health()[binding] == {"pending_count": 1, "recovery_count": 1}
assert set(request_reads) == {partial_request, completed_request}
+ assert set(delivery_reads) == {partial_path, completed_path}
request_reads.clear()
+ delivery_reads.clear()
assert transport.health()[binding] == {"pending_count": 1, "recovery_count": 1}
# Windows has no reliable ctime mutation stamp; its reader stays fresh.
assert set(request_reads) == ({partial_request, completed_request} if sys.platform == "win32" else set())
+ assert set(delivery_reads) == ({partial_path, completed_path} if sys.platform == "win32" else set())
request_reads.clear()
# Independent native updates must not wait for the Lark receipt to change.
@@ -2417,6 +3102,13 @@ def observe(path):
completed_path.unlink()
assert transport.health()[binding] == {"pending_count": 0, "recovery_count": 0}
+ # A delivered body can still have an outstanding historical image reply.
+ _atomic_write_json(partial_path, {**initial_partial, "historical_answer_images": {"status": "pending"}})
+ assert transport.health()[binding] == {"pending_count": 1, "recovery_count": 0}
+ assert transport.health()[binding] == {"pending_count": 1, "recovery_count": 0}
+ _atomic_write_json(partial_path, {**initial_partial, "historical_answer_images": {"status": "delivered"}})
+ assert transport.health()[binding] == {"pending_count": 0, "recovery_count": 0}
+
@pytest.mark.parametrize("scenario", ["concurrent-close", "transient-session-read"])
def test_settings_health_uses_current_session_for_each_batch(ordinary, monkeypatch, scenario): # noqa: F811
@@ -4608,7 +5300,7 @@ def request(path, body=None):
def test_http_stop_recovery_reuses_source_revision_without_reusing_turn_state(ordinary, monkeypatch): # noqa: F811
from loopx.chat_server import ChatHTTPServer, ChatRequestHandler
- from loopx.control_plane import effect_runtime
+ from loopx.control_plane import chat_turn_acceptance, effect_runtime
store, runtime, provider, transport = connect(ordinary)
binding = transport._binding("notes-app")
@@ -4626,9 +5318,11 @@ def queue(name, binding_id):
original_turns = [store._turn_path(sid, turn["turn_id"]).read_bytes() for turn in [owed, authorized]]
native_before = ordinary[4].read_bytes() if ordinary[4].exists() else None
revision_reads = []
+ queue_selections = []
observed = threading.local()
fingerprint = effect_runtime._runtime_fingerprint
stop_recoveries = ChatConversationBindings.stop_recoveries
+ select_queue = chat_turn_acceptance.queued_chat_turn_ids
def counted_fingerprint():
if getattr(observed, "stop_recovery", False):
@@ -4642,8 +5336,14 @@ def observed_stop_recoveries(self, current):
finally:
observed.stop_recovery = False
+ def read_queue(turns, *args, **kwargs):
+ if getattr(observed, "stop_recovery", False):
+ queue_selections.append(len(turns))
+ return select_queue(turns, *args, **kwargs)
+
monkeypatch.setattr(effect_runtime, "_runtime_fingerprint", counted_fingerprint)
monkeypatch.setattr(ChatConversationBindings, "stop_recoveries", observed_stop_recoveries)
+ monkeypatch.setattr(chat_turn_acceptance, "queued_chat_turn_ids", read_queue)
server = ChatHTTPServer(("127.0.0.1", 0), ChatRequestHandler)
server.chat_store, server.runtime_controller, server.verbose = store, runtime, False
server.lark_private_conversations = transport
@@ -4653,6 +5353,7 @@ def observed_stop_recoveries(self, current):
def snapshot():
revision_reads.clear()
+ queue_selections.clear()
connection = http.client.HTTPConnection(*server.server_address, timeout=15)
try:
connection.request("GET", "/api/chat/lark/private-conversations")
@@ -4660,6 +5361,7 @@ def snapshot():
assert response.status == 200
result = json.loads(response.read())
assert len(revision_reads) <= 1 # At most one source inventory, rather than one per Session.
+ assert len(queue_selections) == 1 and queue_selections[0] > 0
return result
finally:
connection.close()
diff --git a/tests/test_native_steward_turn_host.py b/tests/test_native_steward_turn_host.py
index 4d0deb96d6..3c11caab85 100644
--- a/tests/test_native_steward_turn_host.py
+++ b/tests/test_native_steward_turn_host.py
@@ -940,6 +940,41 @@ def test_native_turn_uses_original_app_server_and_kernel_without_a_new_session(s
assert capture.read_bytes() == calls
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_managed_task_edit_after_planning_does_not_start_native_turn(steward, tmp_path, monkeypatch, provider):
+ from loopx.cli_commands import turn_run_once
+
+ service, session, actor, todo_id, _, capture = managed_fixture(steward, tmp_path, monkeypatch, provider)
+ store, runtime, *_ = steward
+ original_request = turn_run_once.prepare_turn_host_request
+ revised = "[P1] Check the corrected readiness condition before continuing."
+ edits = []
+
+ def edit_after_planning(*args, **kwargs):
+ prepared = original_request(*args, **kwargs)
+ assert args[0].host == "codex-app-server"
+ edits.append(cli(runtime, "todo", "update", "--goal-id", actor["goal_id"],
+ "--todo-id", todo_id, "--agent-id", actor["agent_id"], "--text", revised))
+ return prepared
+
+ monkeypatch.setattr(turn_run_once, "prepare_turn_host_request", edit_after_planning)
+ before = capture.read_bytes()
+ accepted, created = service.submit(session["session_id"], agent_id=actor["agent_id"],
+ operation_id="changed-task-before-native-launch", todo_id=todo_id)
+ assert created
+ completed = runtime.wait_for_turn(session_id=accepted["session_id"], turn_id=accepted["turn_id"], timeout_sec=90)
+ result = completed["response"]["native_loopx"]
+ assert len(edits) == 1 and edits[0]["ok"]
+ assert not result["ok"] and "current_turn_work_changed" in json.dumps(result), result
+ calls = [json.loads(line) for line in capture.read_bytes()[len(before):].splitlines()]
+ assert all(row["method"] != "turn/start" for row in calls)
+ assert store.load_session(session["session_id"])["upstream_thread_id"] == session["upstream_thread_id"]
+ current = cli(runtime, "todo", "list", "--goal-id", actor["goal_id"], "--todo-id", todo_id)["todo"]
+ assert current["text"] == revised and not current["done"]
+ index = Path(runtime.coordination_runtime_root) / "goals" / actor["goal_id"] / "runs/index.jsonl"
+ assert not index.exists() or "quota_slot_spent" not in index.read_text()
+
+
def test_unselected_or_unavailable_work_does_not_invoke_the_model(steward, tmp_path, monkeypatch):
sid, actor, _ = prepare(steward, "sqlite", tmp_path, monkeypatch)
store, runtime, _, _, binding, capture, _ = steward
diff --git a/tests/test_postgres_cli.py b/tests/test_postgres_cli.py
new file mode 100644
index 0000000000..d4eba70622
--- /dev/null
+++ b/tests/test_postgres_cli.py
@@ -0,0 +1,168 @@
+"""Unit and integration tests for loopx postgres CLI commands."""
+import json
+import subprocess
+import sys
+from pathlib import Path
+
+REPO = Path(__file__).resolve().parents[1]
+
+
+def test_postgres_setup_dry_run():
+ process = subprocess.run(
+ [
+ sys.executable,
+ "-m",
+ "loopx.entrypoint",
+ "--format",
+ "json",
+ "postgres",
+ "setup",
+ "--database-url",
+ "postgresql://mock_user:mock_pass@localhost:5432/mock_db",
+ "--runtime-role",
+ "loopx_worker",
+ ],
+ cwd=REPO,
+ capture_output=True,
+ text=True,
+ timeout=30,
+ check=False,
+ )
+ assert process.returncode == 0, process.stderr
+ payload = json.loads(process.stdout)
+ assert payload["status"] == "dry_run"
+ assert payload["schema_version"] == "loopx_postgresql_authority_store_v1"
+ assert payload["runtime_role"] == "loopx_worker"
+ assert payload["database_url_configured"] is True
+
+
+def test_postgres_setup_missing_url(monkeypatch):
+ monkeypatch.delenv("LOOPX_POSTGRES_URL", raising=False)
+ process = subprocess.run(
+ [
+ sys.executable,
+ "-m",
+ "loopx.entrypoint",
+ "--format",
+ "json",
+ "postgres",
+ "setup",
+ ],
+ cwd=REPO,
+ capture_output=True,
+ text=True,
+ timeout=30,
+ check=False,
+ )
+ assert process.returncode == 1
+ payload = json.loads(process.stdout)
+ assert payload["status"] == "failed"
+ assert "required" in payload.get("reason", "").lower()
+
+
+def test_postgres_local_to_pg_seed_export(tmp_path):
+ runtime = tmp_path / "runtime"
+ registry = tmp_path / "registry.json"
+ state = tmp_path / "state.md"
+ goal = "seed-export-goal"
+
+ runtime.mkdir(parents=True, exist_ok=True)
+ state.write_text("# Synthetic state for seed export\n")
+ registry.write_text(
+ json.dumps(
+ {
+ "common_runtime_root": str(runtime),
+ "goals": [{"id": goal, "repo": str(tmp_path), "state_file": state.name}],
+ }
+ )
+ )
+
+ # Initialize local authority store using FileAuthorityStore via Node
+ module = REPO / "loopx/control_plane/coordination/file_authority_store.ts"
+ store_dir = runtime / "authority" / "file-v0"
+ store_dir.mkdir(parents=True, exist_ok=True)
+
+ script = (
+ f"import {{FileAuthorityStore}} from {json.dumps(module.as_uri())};"
+ "const store = new FileAuthorityStore(process.argv[1], process.argv[2]);"
+ "await store.commitAuthority({"
+ " operation_id: 'op-init',"
+ " expected_provider_revision: null,"
+ " events: [],"
+ " receipts: [],"
+ " next_projection: {"
+ " schema_version: 'loopx_authority_projection_v0',"
+ " goal_id: process.argv[2],"
+ " owner_identity: 'ou_1234567890abcdef1234567890',"
+ " channel: { chat_id: 'oc_abcdef1234567890abcdef12345678' },"
+ " todos: ["
+ " {"
+ " id: 'todo-1',"
+ " title: 'Work on feature with .local/path and Feishu contact',"
+ " required_write_scopes: ['/Users/test/Developer/loopx/packages/core']"
+ " }"
+ " ]"
+ " }"
+ "});"
+ )
+ subprocess.run(
+ ["node", "--no-warnings", "--experimental-strip-types", "--input-type=module", "-e", script, str(store_dir), goal],
+ capture_output=True,
+ text=True,
+ timeout=30,
+ check=True,
+ )
+
+ output_seed = tmp_path / "genesis_seed.json"
+ command = [
+ sys.executable, "-m", "loopx.entrypoint", "--registry", str(registry),
+ "--format", "json", "postgres", "local-to-pg-seed", "--goal-id", goal,
+ "--tenant-id", "tenant-alpha", "--output", str(output_seed),
+ ]
+
+ def export():
+ return subprocess.run(command, cwd=REPO, capture_output=True, text=True, timeout=45, check=False)
+
+ process = export()
+ assert process.returncode == 0, process.stderr
+ payload = json.loads(process.stdout)
+ assert payload["status"] == "exported"
+ assert payload["cursor"] == "1"
+ assert payload["goal_id"] == goal
+ assert payload["tenant_id"] == "tenant-alpha"
+ assert bool(payload.get("seed_digest"))
+
+ assert output_seed.is_file()
+ seed_content = json.loads(output_seed.read_text())
+ assert seed_content["schema_version"] == "loopx_pg_genesis_seed_v0"
+ assert seed_content["cursor"] == "1"
+ # Verify Feishu IDs and local paths were sanitized
+ raw_str = json.dumps(seed_content)
+ assert "ou_1234567890abcdef1234567890" not in raw_str
+ assert "oc_abcdef1234567890abcdef12345678" not in raw_str
+ assert "/Users/test/Developer" not in raw_str
+
+ original = output_seed.read_bytes()
+ inode = output_seed.stat().st_ino
+ repeated = export()
+ assert repeated.returncode == 0, repeated.stderr
+ assert json.loads(repeated.stdout)["seed_digest"] == payload["seed_digest"]
+ assert output_seed.stat().st_ino == inode
+ assert output_seed.read_bytes() == original
+
+ different = original.replace(b"tenant-alpha", b"tenant-other", 1)
+ assert different != original and len(different) == len(original)
+ output_seed.write_bytes(different)
+ refused = export()
+ assert refused.returncode == 1
+ assert "identical regular file" in json.loads(refused.stdout)["reason"]
+ assert output_seed.read_bytes() == different
+
+ alternative = tmp_path / "seed_target.json"
+ alternative.write_bytes(original)
+ output_seed.unlink()
+ output_seed.symlink_to(alternative)
+ symlink = export()
+ assert symlink.returncode == 1
+ assert output_seed.is_symlink()
+ assert alternative.read_bytes() == original
diff --git a/tests/test_public_source_reader.py b/tests/test_public_source_reader.py
index b461adc648..3bd6fa77c9 100644
--- a/tests/test_public_source_reader.py
+++ b/tests/test_public_source_reader.py
@@ -70,6 +70,26 @@ def test_github_diff_transport_failure_recovers_canonical_source_in_same_budget(
for _, _, _, _, headers in state["attempts"])
+def test_github_diff_recovery_honors_exact_source_scope_before_dns(github_diff_transport):
+ state = github_diff_transport
+ original = "https://github.com/example/project/pull/42.diff"
+ with pytest.raises(ValueError, match="public source URL not authorized"):
+ reader.fetch(original, deadline=110.0, allowed_urls={original})
+ assert state["dns"] == ["github.com"]
+ assert [attempt[0] for attempt in state["attempts"]] == ["github.com", "github.com"]
+ assert len(state["closed"]) == 2
+
+
+def test_public_url_scope_rejects_initial_destination_before_dns(monkeypatch):
+ def resolve(host):
+ pytest.fail(f"unapproved destination resolved: {host}")
+
+ monkeypatch.setattr(reader, "public_addresses", resolve)
+ with pytest.raises(ValueError, match="public source URL not authorized"):
+ reader.fetch("https://source.example/unlisted.png", deadline=time.monotonic() + 10,
+ allowed_urls={"https://source.example/listed.png"})
+
+
@pytest.mark.parametrize("host", ["github.com", "patch-diff.githubusercontent.com"])
def test_github_diff_recovery_rejects_mixed_dns_before_connection(github_diff_transport, host):
state = github_diff_transport
diff --git a/tests/test_python_ci_workflow.py b/tests/test_python_ci_workflow.py
index b4f5e70407..ab82c69360 100644
--- a/tests/test_python_ci_workflow.py
+++ b/tests/test_python_ci_workflow.py
@@ -351,7 +351,7 @@ def test_windows_lifecycle_suite_references_existing_tests() -> None:
assert "tests/control_plane/test_cli_output_differential.py" in args
assert set(args[args.index("-k") + 1].split(" or ")) == {
"stable_budget_fixture", "long_path_budget",
- "probe_process_failure_preserves_utf8_diagnostics",
+ "probe_process_failure_preserves_utf8_diagnostics", "pinned_reader_follows",
}
diff --git a/tests/test_state_recovery.py b/tests/test_state_recovery.py
index 8251aabf6e..b3d6718f5f 100644
--- a/tests/test_state_recovery.py
+++ b/tests/test_state_recovery.py
@@ -578,7 +578,7 @@ def test_offline_worker_timeout_does_not_leave_a_copy_or_stop_shared_runtime(tmp
assert normal["activation_performed"] is False
-def test_real_sqlite_history_limit_is_complete_and_overflow_refuses_publication(tmp_path):
+def test_real_sqlite_history_above_20000_is_verified_through_outer_backup(tmp_path):
registry, runtime, state = promoted_create_fixture(tmp_path, provider="sqlite")
project = state.parents[3]
owner = Path(__file__).parents[1] / "loopx/control_plane/coordination"
@@ -615,25 +615,24 @@ def test_real_sqlite_history_limit_is_complete_and_overflow_refuses_publication(
if(readback.status!=='loaded'||Number(readback.cursor)!==r.last)throw new Error('Fixture readback mismatch');
process.stdout.write(JSON.stringify(audit));
"""
- for first, last, expected in [(2, 20000, "readable"), (20001, 20001, "refused")]:
+ for first, last in [(2, 20000), (20001, 20001)]:
seeded = subprocess.run(["node", "--no-warnings", "--experimental-sqlite", "--experimental-strip-types",
"--input-type=module", "-e", script], input=json.dumps({"root": str(runtime), "first": first, "last": last}),
text=True, capture_output=True, check=True, timeout=120)
- assert json.loads(seeded.stdout)["commits"] == last
+ verified = json.loads(seeded.stdout)
+ assert verified["commits"] == last
backup = execute_state_backup_plan(build_state_backup_plan(project=project, runtime_root=runtime,
registry_path=registry, output_dir=tmp_path / "backups", backup_id=f"history-{last}",
include_registry_projects=False, include_automations=False, include_skills=False))
destination = tmp_path / f"inert-{last}"
code, result = _cli("backup-state", "verify", "--archive", backup["archive_path"],
"--manifest", backup["manifest_path"], "--destination", destination, "--execute")
- if expected == "readable":
- assert code == 0, result
- [goal] = [row for row in result["components"] if row["owner_kind"] == "coordination_authority"]
- assert goal["state"] == "readable" and goal["facts"]["history_commits"] == 20000
- else:
- assert code == 1 and result["error_code"] == "authority_history_capacity", result
- assert not destination.exists()
- assert not list(tmp_path.glob(".loopx-recovery-*"))
+ assert code == 0, result
+ [goal] = [row for row in result["components"] if row["owner_kind"] == "coordination_authority"]
+ assert goal["state"] == "readable"
+ assert goal["facts"]["history_commits"] == last
+ assert goal["facts"]["cursor"] == str(last)
+ assert (destination / "recovery-audit.json").is_file()
def test_digest_refusal_precedes_tar_decoding_and_writes_nothing(tmp_path):
diff --git a/tests/test_usage_goal.py b/tests/test_usage_goal.py
index 24685cc58a..f4b98a293d 100644
--- a/tests/test_usage_goal.py
+++ b/tests/test_usage_goal.py
@@ -86,6 +86,48 @@ def test_disabled_observer_starts_no_worker_or_process(tmp_path, monkeypatch):
pass
+@pytest.mark.parametrize("switch,value", [
+ ("LOOPX_USAGE_PING", "off"), ("DO_NOT_TRACK", "yes"),
+ ("DO_NOT_TRACK", "false"), ("CI", "1"), ("CI", " YES "),
+])
+@pytest.mark.parametrize("entrypoint", ["host", "quota", "direct_cycle"])
+def test_environment_opt_out_skips_host_and_binding_work(tmp_path, monkeypatch, switch, value, entrypoint):
+ monkeypatch.setattr(usage_ping, "select_default_runtime_root", lambda: tmp_path)
+ for name in ("CI", "DO_NOT_TRACK", "LOOPX_USAGE_PING"):
+ monkeypatch.delenv(name, raising=False)
+ monkeypatch.setenv(switch, value)
+ usage_ping.state_path().write_text(json.dumps({"generation": "fixture", "consent": "enabled"}))
+ workers, sends, bindings = [], [], []
+ monkeypatch.setattr(threading.Thread, "start", lambda worker: workers.append(worker.name))
+ monkeypatch.setattr(usage_ping, "_detach", sends.append)
+ if entrypoint == "host":
+ error = ValueError("original Host failure")
+ with pytest.raises(ValueError) as caught:
+ with usage_goal.observe_goal_execution(tmp_path, "fixture-goal"):
+ raise error
+ assert caught.value is error
+ else:
+ registry, rollout = _bound_fixture(tmp_path, monkeypatch)
+ original = usage_goal._bound_codex_session
+
+ def read_binding(*args):
+ result = original(*args)
+ bindings.append(result)
+ return result
+
+ monkeypatch.setattr(usage_goal, "_bound_codex_session", read_binding)
+ if entrypoint == "quota":
+ usage_goal.observe_quota_cycle(registry_path=registry, runtime_root=tmp_path, goal_id="goal",
+ agent_id="agent", turn_id="turn", phase="start", at=0, host="codex-app")
+ else:
+ usage_goal._dispatch_cycle({"path": str(usage_ping.state_path()), "generation": "fixture",
+ "registry": str(registry), "runtime": str(tmp_path), "goal": "goal", "agent": "agent",
+ "turn": "turn", "phase": "start", "at": 0, "host": "codex-app"})
+ assert workers == []
+ assert sends == []
+ assert bindings == []
+
+
def test_periodic_observation_does_not_need_turn_completion(tmp_path, monkeypatch):
monkeypatch.setattr(usage_ping, "select_default_runtime_root", lambda: tmp_path)
usage_ping.state_path().write_text(json.dumps({"generation": "fixture", "consent": "enabled"}))
diff --git a/tests/test_usage_ping.py b/tests/test_usage_ping.py
index 960bb87a3d..5a6995a166 100644
--- a/tests/test_usage_ping.py
+++ b/tests/test_usage_ping.py
@@ -6,6 +6,7 @@
import os
import runpy
import select
+import shutil
import socket
import subprocess
import sys
@@ -310,6 +311,90 @@ def test_first_disclosure_respects_policy_overrides(isolated, monkeypatch, capsy
assert capsys.readouterr().err == ''
+@pytest.mark.parametrize('switch,value', [
+ ('LOOPX_USAGE_PING', 'off'), ('LOOPX_USAGE_PING', ' FALSE '),
+ ('LOOPX_USAGE_PING', '\ufeffOFF\ufeff'), ('DO_NOT_TRACK', 'yes'),
+ ('DO_NOT_TRACK', 'false'), ('CI', '1'), ('CI', ' YES '),
+])
+@pytest.mark.parametrize('current_notice', [False, True])
+def test_environment_opt_out_skips_automatic_cli_transport(
+ isolated, monkeypatch, capsys, switch, value, current_notice,
+):
+ # Real business dispatch and enabled hints prevent missing state or consent
+ # from masking automatic disclosure or warm-command transport attempts.
+ usage_ping.state_path().write_text(json.dumps({
+ 'generation': 'fixture', 'consent': 'enabled',
+ 'notice': {'version': 6 if current_notice else 0},
+ }))
+ monkeypatch.setenv(switch, value)
+ attempts = []
+
+ def unavailable_command():
+ attempts.append('transport')
+ raise OSError('fixture transport unavailable')
+
+ monkeypatch.setattr(usage_ping, '_command', unavailable_command)
+ assert main(['version', '--format', 'json']) == 0
+ captured = capsys.readouterr()
+ assert json.loads(captured.out)['ok'] is True
+ assert captured.err == ''
+ assert attempts == []
+
+
+@pytest.mark.parametrize('switch,value', [
+ ('LOOPX_USAGE_PING', 'off'), ('DO_NOT_TRACK', 'yes'), ('CI', '1'),
+])
+@pytest.mark.parametrize('producer', ['finish', 'verified_return', 'direct', 'python_child'])
+def test_environment_opt_out_fences_late_or_direct_observation(
+ isolated, monkeypatch, switch, value, producer,
+):
+ usage_ping.state_path().write_text(json.dumps({
+ 'generation': 'fixture', 'consent': 'enabled', 'notice': {'version': 6},
+ }))
+ monkeypatch.setenv(switch, value)
+ monkeypatch.setattr(subprocess, 'Popen', lambda *a, **k: pytest.fail('opt-out spawned a child'))
+ monkeypatch.setattr(usage_ping, '_pin_caller_paths', lambda *a: pytest.fail('opt-out resolved caller paths'))
+ if producer == 'finish':
+ usage_ping.finish(('fixture', time.monotonic()), 'version', 0)
+ elif producer == 'verified_return':
+ usage_ping.observe_verified_return()
+ else:
+ usage_ping._detach({'path': 'caller-relative-state'}, command=(
+ [sys.executable, '-P', '-m', 'loopx.usage_goal'] if producer == 'python_child' else None
+ ))
+
+
+def test_environment_hint_matches_the_typescript_owner():
+ node = shutil.which('node')
+ if node is None:
+ pytest.skip('TypeScript owner parity requires the supported Node runtime')
+ # DNT differs from CI/PING. JS trims FEFF but not U+0085; Python's default
+ # strip has the opposite behavior for these two characters.
+ values = ['', '0', '1', 'false', 'true', 'no', 'off', 'yes', 'unknown',
+ ' FALSE ', ' OFF ', ' YES ', '\ufeff0\ufeff', '\ufeffOFF\ufeff',
+ '\ufefffalse\ufeff', '\u00850\u0085', '\u0085off\u0085']
+ environments = [{}] + [{key: value} for key in ('LOOPX_USAGE_PING', 'DO_NOT_TRACK', 'CI') for value in values]
+ environments += [
+ {'LOOPX_USAGE_PING': 'yes', 'DO_NOT_TRACK': '0', 'CI': 'false'},
+ {'LOOPX_USAGE_PING': 'off', 'DO_NOT_TRACK': 'yes', 'CI': '1'},
+ ]
+ owner = (Path(usage_ping.__file__).parent / 'control_plane/runtime/usage_statistics.ts').as_uri()
+ script = f'''import {{ blockedBy }} from {json.dumps(owner)};
+let input=''; for await (const chunk of process.stdin) input+=chunk;
+const reasons=JSON.parse(input).map(env=>blockedBy({{consent:'disabled'}},
+ {{env,version:'1.0.0',python:'3.13',channel:'source'}},'unused'));
+process.stdout.write(JSON.stringify(reasons));'''
+ result = subprocess.run([node, '--no-warnings', '--experimental-strip-types', '--input-type=module', '-e', script],
+ input=json.dumps(environments), capture_output=True, text=True, timeout=20, check=True)
+ reasons = json.loads(result.stdout)
+ assert len(reasons) == len(environments)
+ for env, reason in zip(environments, reasons):
+ # Disabled consent makes the existing owner return before filesystem or
+ # sending when no environment suppressor exists; this does not enable it.
+ expected = reason in {'LOOPX_USAGE_PING', 'DO_NOT_TRACK', 'CI'}
+ assert usage_ping._environment_suppressed(env) is expected, (env, reason)
+
+
def test_real_agent_cli_keeps_json_clean_and_sends_only_after_disclosure(isolated, collector, monkeypatch):
endpoint, received, accepted, release = collector
monkeypatch.setenv('LOOPX_USAGE_PING_ENDPOINT', endpoint)
')[0] ?? "";
+ const twoArmTable = twoArmSection.match(/([\s\S]*?)<\/tbody>/)?.[1];
+ const twoArmRows = [...(twoArmTable ?? "").matchAll(/([\s\S]*?)<\/tr>/g)];
+ deepStrictEqual(
+ twoArmRows.length,
+ twoArm.tasks.length,
+ `Controlled two-arm score table must retain all 10 task rows including counterexamples: ${locale}`,
+ );
+ for (const requiredToken of ["7,159", "3,746", "2,197", "2,311", "27.1801", "13.62", "39.44", "68.89", "70.79"]) {
+ assertIncludes(twoArmSection, requiredToken, `Controlled score section must preserve ${requiredToken} (${locale})`);
+ }
+ for (const isolatedText of [twoArmSection, twoArmSvg, JSON.stringify(twoArm)]) {
+ if (/four-arm|four arm|四臂/i.test(isolatedText)) {
+ throw new Error(`Controlled two-arm section must remain isolated without four-arm framing (${locale})`);
+ }
+ }
+ for (const publicText of [article, twoArmSvg, JSON.stringify(audit)]) {
+ if (/sforge/i.test(publicText)) {
+ throw new Error(`Internal benchmark name leaked into public context-delivery blog artifact (${locale})`);
+ }
+ }
if (/7,479,469|33\.4%|69\.9 KB|57\.1ms/.test(article))
throw new Error("Unreconciled totals or unsupported C1 attribution reappeared");
}
diff --git a/examples/coordination/remote-authority-client.ts b/examples/coordination/remote-authority-client.ts
index 9485c66d48..21232642b7 100644
--- a/examples/coordination/remote-authority-client.ts
+++ b/examples/coordination/remote-authority-client.ts
@@ -15,5 +15,5 @@ const result = await callRemoteAuthority(values.endpoint, process.env.LOOPX_AUTH
decodeRemoteAuthorityRequest(JSON.parse(raw)), {mode: values.recover ? "recover" : "execute",
...(values.ca ? {ca: await readFile(values.ca)} : {})});
process.stdout.write(`${JSON.stringify(result)}\n`);
-if (!["found", "missing", "result"].includes(String(result.status)) ||
+if (!["found", "missing", "result", "snapshot"].includes(String(result.status)) ||
result.status === "result" && !["applied", "no_change", "replayed", "recovered"].includes(String((result.result as Record)?.status))) process.exitCode = 1;
diff --git a/examples/loopx-chat-actions-smoke.py b/examples/loopx-chat-actions-smoke.py
index 63b921ef5e..d0ac017b36 100644
--- a/examples/loopx-chat-actions-smoke.py
+++ b/examples/loopx-chat-actions-smoke.py
@@ -105,25 +105,43 @@ def __init__(self, chat_store: ChatSessionStore) -> None:
def open_session(self, **kwargs: object) -> tuple[dict[str, object], bool]:
self.opened_sessions.append(dict(kwargs))
- existing = self.chat_store.latest_session(
+ managed_agent = kwargs.get("managed_agent_id")
+ channel = f"agent.{managed_agent}" if managed_agent is not None else str(kwargs["channel_id"])
+ session_id = kwargs.get("session_id")
+ existing = self.chat_store.load_session(str(session_id)) if session_id is not None else self.chat_store.latest_session(
goal_id=str(kwargs["goal_id"]),
agent_id=str(kwargs["agent_id"]),
- channel_id=str(kwargs["channel_id"]),
+ channel_id=channel,
)
if existing is not None:
return existing, True
session = self.chat_store.create_session(
goal_id=str(kwargs["goal_id"]),
+ goal_instance_id=kwargs.get("goal_instance_id"),
+ **({"session_id": str(session_id)} if session_id is not None else {}),
+ **({"managed_agent_id": str(managed_agent), "managed_workspace_path": str(Path(str(kwargs["work_dir"])).resolve())}
+ if managed_agent is not None else {}),
agent_id=str(kwargs["agent_id"]),
adapter_kind="fake",
upstream_thread_id="thread-goal-create",
- channel_id=str(kwargs["channel_id"]),
+ upstream_mode="chat" if kwargs["agent_id"] == "codex" else "default",
+ channel_id=channel,
)
return session, False
def submit_turn(self, **kwargs: object) -> tuple[dict[str, object], bool]:
self.submissions.append(dict(kwargs))
- return ({"turn_id": "turn-correction-1", "status": "queued"}, True)
+ session_id = str(kwargs["session_id"])
+ turn, created = self.chat_store.create_turn(session_id,
+ client_turn_id=str(kwargs["client_turn_id"]), message=str(kwargs["message"]))
+ if created:
+ # The synchronous fake finishes its ordinary reply through the real
+ # Chat completion owner; it never accepts a Goal or Task outcome.
+ self.chat_store.update_turn(session_id, str(turn["turn_id"]),
+ status="completing", started_at=turn["created_at"], completed_at=turn["created_at"],
+ upstream_turn_id=f"synthetic-{turn['turn_id']}", response={"message": "Synthetic ordinary reply."})
+ turn = self.chat_store.finalize_managed_turn_completion(session_id, str(turn["turn_id"]))
+ return turn, created
def capabilities(self) -> list[dict[str, object]]:
return [
@@ -369,7 +387,8 @@ def assert_http_action_api(root: Path) -> None:
)
assert code == 202, correction_applied
assert correction_applied["proposal"]["status"] == "applied", correction_applied
- assert correction_applied["turn"]["turn_id"] == "turn-correction-1", correction_applied
+ original_correction = chat_store.turn_for_client(session["session_id"], str(runtime_controller.submissions[1]["client_turn_id"]))
+ assert correction_applied["turn"]["turn_id"] == original_correction["turn_id"], correction_applied
assert runtime_controller.submissions[1]["session_id"] == session["session_id"]
goal_proposal = previews["goal.create"]
diff --git a/examples/personal-workspace-browser/goal-draft.mjs b/examples/personal-workspace-browser/goal-draft.mjs
index 1054c665de..e775b92f33 100644
--- a/examples/personal-workspace-browser/goal-draft.mjs
+++ b/examples/personal-workspace-browser/goal-draft.mjs
@@ -28,6 +28,10 @@ export const goalDraftScenario = {
const requests = api.turnRequests.length;
await card.getByRole("button", {name: "资本支出变化", exact: true}).click();
assert.equal(await composer.inputValue(), "资本支出变化");
+ await composer.fill("保留现金流的判断。");
+ await card.getByRole("button", {name: "资本支出变化", exact: true}).click();
+ assert.equal(await composer.inputValue(), "保留现金流的判断。\n资本支出变化",
+ "An existing suggestion appends to the current draft");
assert.equal(api.turnRequests.length, requests, "Suggestion auto-sent a request");
assert.equal(api.actionPreviews.length, 0);
assert.equal(api.durableWriteCount, writes);
@@ -39,6 +43,10 @@ export const goalDraftScenario = {
assert.equal(api.durableWriteCount, writes, "Preparing a complete draft cannot create a Goal");
await card.getByRole("button", {name: "重新准备", exact: true}).click();
await card.getByRole("button", {name: "创建并开始", exact: true}).waitFor();
+ await card.locator(".personal-goal-creation-execution").getByText("只读", {exact: true}).waitFor();
+ await card.locator(".personal-goal-creation-execution").getByText("无", {exact: true}).waitFor();
+ assert.equal(api.actionPreviews.at(-1).normalized_parameters.start_execution, true,
+ "A fresh Codex Create and start explicitly requests execution");
assert.equal(await page.locator('.personal-proposal-row[data-action-kind="goal.create"]').count(), 0,
"The visible draft owns one confirmation instead of a duplicate action card");
await card.getByText("codex", {exact: true}).waitFor();
@@ -71,13 +79,17 @@ export const goalDraftScenario = {
await objective.fill(`\u0085${draft.objective}\u001c`);
await boundary.fill(draft.execution_boundary);
await form.getByLabel("完成标准", {exact: true}).fill("资本支出对照表与原始来源");
+ await form.getByLabel("执行权限", {exact: true}).selectOption("workspace_write_on_confirmation");
await form.getByRole("button", {name: "检查配置"}).click();
await page.getByText("确认执行", {exact: true}).waitFor();
const preview = api.actionPreviews.at(-1);
assert.equal(preview.action_kind, "goal.create");
assert.equal(preview.normalized_parameters.title, draft.objective);
assert.equal(preview.normalized_parameters.workspace_ref, "current");
- assert.equal(preview.normalized_parameters.permission, "read_only");
+ assert.equal(preview.normalized_parameters.permission, "workspace_write_on_confirmation");
+ assert.equal(preview.normalized_parameters.start_execution, true);
+ await page.locator(".personal-context-drawer .personal-goal-creation-execution")
+ .getByText("所选工作区的所有文件", {exact: true}).waitFor();
assert.equal(preview.normalized_parameters.heartbeat.enabled, false);
assert.equal(api.durableWriteCount, writes);
await page.getByRole("button", {name: "关闭", exact: true}).click();
diff --git a/examples/personal-workspace-browser/goal-workspace-backend.py b/examples/personal-workspace-browser/goal-workspace-backend.py
index 8b4ba4d2e6..7406701a69 100644
--- a/examples/personal-workspace-browser/goal-workspace-backend.py
+++ b/examples/personal-workspace-browser/goal-workspace-backend.py
@@ -30,7 +30,23 @@
registry.write_text(json.dumps(data), encoding="utf-8")
runtime = Path(data["common_runtime_root"])
store, sessions = ChatActionStore(runtime / "chat/actions"), ChatSessionStore(runtime)
-controller = fixture["FakeRuntimeController"](sessions)
+
+
+class WorkspaceRuntimeController(fixture["FakeRuntimeController"]):
+ def __init__(self, store: ChatSessionStore, registry_path: Path, runtime_root: Path) -> None:
+ super().__init__(store)
+ self.store = store
+ self.registry_path = registry_path
+ self.coordination_runtime_root = runtime_root
+
+ def resume_session_queue(self, *, session_id: str, work_dir: Path, objective: str) -> None:
+ # Observe the queue target at the synthetic dispatch boundary.
+ session = self.store.load_session(session_id)
+ assert session is not None
+ assert Path(session["managed_workspace_path"]) == Path(work_dir)
+
+
+controller = WorkspaceRuntimeController(sessions, registry, runtime)
# One Dashboard-configured workspace that no Goal uses yet.
configured = root / "docs-site"
configured.mkdir()
diff --git a/examples/personal-workspace-browser/goal-workspace-selection.mjs b/examples/personal-workspace-browser/goal-workspace-selection.mjs
index 5b9a03f2a1..bfca50ca3a 100644
--- a/examples/personal-workspace-browser/goal-workspace-selection.mjs
+++ b/examples/personal-workspace-browser/goal-workspace-selection.mjs
@@ -91,10 +91,18 @@ runpy.run_path(sys.argv[0], run_name="__main__")
assert.equal(await page.getByText(backend.second, {exact: false}).count(), 0);
assert.equal(JSON.parse(await readFile(backend.registry, "utf8")).goals.length, 2, "Preview created a Goal");
const confirm = page.getByRole("button", {name: /创建 Goal.*开始首轮/});
+ const confirmation = page.waitForResponse((response) =>
+ response.request().method() === "POST" && new URL(response.url()).pathname.endsWith("/apply"));
await confirm.click();
- await page.waitForFunction(() => document.body.innerText.includes("进入新 Goal"));
+ assert.ok((await confirmation).ok(), JSON.stringify(applied[0]));
assert.equal(applied.length, 1);
- assert.equal(applied[0].proposal.receipt.projection_verified, true);
+ const receipt = applied[0].proposal.receipt;
+ assert.equal(receipt.projection_verified, true);
+ assert.equal(receipt.original_task_request.todo_id, receipt.resource_ids.todo_ids[0]);
+ assert.equal(receipt.original_task_request.session_id, receipt.resource_ids.session_id);
+ assert.equal(receipt.original_task_request.permission, "read_only");
+ assert.deepEqual(receipt.original_task_request.write_scope, []);
+ await page.getByRole("button", {name: "进入新 Goal", exact: true}).waitFor();
const goalId = requests.at(-1).normalized_parameters.goal_id;
const registry = JSON.parse(await readFile(backend.registry, "utf8"));
const created = registry.goals.find(goal => goal.id === goalId);
diff --git a/examples/personal-workspace-browser/typed-actions.mjs b/examples/personal-workspace-browser/typed-actions.mjs
index a4dc8fb3cf..fe06151219 100644
--- a/examples/personal-workspace-browser/typed-actions.mjs
+++ b/examples/personal-workspace-browser/typed-actions.mjs
@@ -325,13 +325,17 @@ export const typedActionsScenario = {
}
const writesBeforeSelection = api.durableWriteCount;
await drawer.getByRole("button", { name: /Workspace 1/ }).click();
- await page.getByRole("button", { name: "创建 Goal 并开始首轮", exact: true }).waitFor({ state: "visible" });
+ const create = drawer.getByRole("button", { name: "创建 Goal", exact: true });
+ await create.waitFor({ state: "visible" });
const regenerated = api.actionPreviews.at(-1);
if (regenerated?.normalized_parameters.workspace_ref !== "workspace-fixture") {
throw new Error(`Stored workspace selection did not regenerate the Goal preview: ${JSON.stringify(regenerated)}`);
}
+ if (regenerated.normalized_parameters.start_execution !== storedWorkspaceGate.normalized_parameters.start_execution) {
+ throw new Error("Workspace selection changed the saved execution intent");
+ }
if (api.durableWriteCount !== writesBeforeSelection) throw new Error("Selecting a workspace applied without confirmation");
- await page.getByRole("button", { name: "创建 Goal 并开始首轮", exact: true }).click();
+ await create.click();
await page.getByText("已应用,LoopX 状态将刷新。", { exact: true }).waitFor({ state: "visible" });
if (!api.actionApplies.includes(regenerated.proposalId)) throw new Error("Confirmation did not apply the regenerated preview");
if (api.durableWriteCount !== writesBeforeSelection + 1) throw new Error("Confirmation did not write exactly one Goal");
diff --git a/loopx/agent_onboarding.py b/loopx/agent_onboarding.py
index 7bacb923ab..bee609f34b 100644
--- a/loopx/agent_onboarding.py
+++ b/loopx/agent_onboarding.py
@@ -482,10 +482,12 @@ def build_agent_onboarding_packet(
registry_path,
resolved_goal_id,
)
+ activation_registry = str(registry_path) if canonical_agent_type == "antigravity-app" else None
host_loop_activation = build_host_loop_activation_packet(
agent_type=canonical_agent_type,
goal_id=resolved_goal_id,
cli_bin=cli_bin,
+ registry=activation_registry,
agent_id=agent_id,
registered_agents=registered_agents,
available_capabilities=available_capabilities,
@@ -516,6 +518,7 @@ def build_agent_onboarding_packet(
render_quota_guard_command(
resolved_goal_id,
cli_bin=cli_bin,
+ registry_path=activation_registry,
agent_id=str(selected_agent_id) if selected_agent_id else None,
available_capabilities=normalized_available_capabilities,
**scheduler_command_binding_for_agent_type(canonical_agent_type),
diff --git a/loopx/agy_goal_mode/__init__.py b/loopx/agy_goal_mode/__init__.py
index bb2c73536d..da60243cca 100644
--- a/loopx/agy_goal_mode/__init__.py
+++ b/loopx/agy_goal_mode/__init__.py
@@ -14,6 +14,8 @@
from pathlib import Path
from typing import Any
+from ..command_invocation import loopx_cli_argv
+
AGY_INSTALL_SURFACE = "agy"
ANTIGRAVITY_APP_INSTALL_SURFACE = "antigravity-app"
ANTIGRAVITY_APP_AGENT_TYPE = "antigravity-app"
@@ -111,38 +113,21 @@ def build_antigravity_sidecar_template(
goal_id: str = "",
agent_id: str | None = None,
runtime_root: str | None = None,
+ registry: str | None = None,
cli_bin: str = "loopx",
interval_seconds: int = 180,
) -> dict[str, Any]:
"""Build a public-safe Antigravity 2.0 ``sidecar.json`` periodic heartbeat template."""
- quota_args = [cli_bin, "--format", "json"]
+ cli_args = [*loopx_cli_argv(cli_bin), "--format", "json"]
if runtime_root:
- quota_args.extend(["--runtime-root", runtime_root])
- quota_args.extend(
- [
- "quota",
- "should-run",
- "--goal-id",
- goal_id,
- "--runtime-profile",
- "antigravity_app",
- ]
- )
+ cli_args.extend(["--runtime-root", runtime_root])
+ if registry:
+ cli_args.extend(["--registry", registry])
+ profile_args = ["--goal-id", goal_id, "--runtime-profile", "antigravity_app"]
+ quota_args = [*cli_args, "quota", "should-run", *profile_args]
if agent_id:
quota_args.extend(["--agent-id", agent_id])
- heartbeat_args = [cli_bin, "--format", "json"]
- if runtime_root:
- heartbeat_args.extend(["--runtime-root", runtime_root])
- heartbeat_args.extend(
- [
- "heartbeat-prompt",
- "--thin",
- "--goal-id",
- goal_id,
- "--runtime-profile",
- "antigravity_app",
- ]
- )
+ heartbeat_args = [*cli_args, "heartbeat-prompt", "--thin", *profile_args]
if agent_id:
heartbeat_args.extend(["--agent-id", agent_id])
bootstrap_prompt = (
@@ -364,6 +349,7 @@ def antigravity_app_activation_extras(
goal_id: str = "",
agent_id: str | None = None,
runtime_root: str | None = None,
+ registry: str | None = None,
cli_bin: str = "loopx",
) -> dict[str, Any]:
"""Keyword overrides for ``antigravity_app_activation``'s facade call."""
@@ -371,6 +357,7 @@ def antigravity_app_activation_extras(
goal_id=goal_id,
agent_id=agent_id,
runtime_root=runtime_root,
+ registry=registry,
cli_bin=cli_bin,
)
hooks_template = build_antigravity_hooks_template(
@@ -378,6 +365,7 @@ def antigravity_app_activation_extras(
goal_id=goal_id,
agent_id=agent_id,
runtime_root=runtime_root,
+ registry=registry,
resolve_local_executable=False,
)
wake_prompt = sidecar_template["sidecar"]["loopx-heartbeat"]["bootstrap_prompt"]
diff --git a/loopx/agy_goal_mode/host_runtime.py b/loopx/agy_goal_mode/host_runtime.py
index 6cd4f5b265..96e207b865 100644
--- a/loopx/agy_goal_mode/host_runtime.py
+++ b/loopx/agy_goal_mode/host_runtime.py
@@ -17,6 +17,7 @@
import time
from typing import Any
+from ..command_invocation import loopx_cli_argv
from ..entrypoint import pin_utf8_stdio
from ..file_lock import exclusive_file_lock
@@ -41,7 +42,7 @@ def retryable_native_error(error: Any) -> bool:
def _core_args(args: argparse.Namespace, command: list[str]) -> list[str]:
- result = [args.cli_bin, "--format", "json"]
+ result = [*loopx_cli_argv(args.cli_bin), "--format", "json"]
for flag, value in (
("--registry", args.registry),
("--runtime-root", args.runtime_root),
diff --git a/loopx/attached_session.py b/loopx/attached_session.py
index 6ba0bdeeff..7c0f7ba627 100644
--- a/loopx/attached_session.py
+++ b/loopx/attached_session.py
@@ -767,7 +767,10 @@ def _enqueue_attached_agent_turn_under_grant(
def enqueue_attached_agent_turn(*, store: ChatSessionStore, registry_path: Path | None,
session_id: str, client_turn_id: str, message: str, origin: str,
- external_agent_target: dict[str, Any] | None = None) -> tuple[dict[str, Any], bool]:
+ external_agent_target: dict[str, Any] | None = None,
+ attachments: object = None) -> tuple[dict[str, Any], bool]:
+ if attachments:
+ raise ValueError("attached host session queue does not yet accept attachments")
kwargs = dict(store=store, registry_path=registry_path, session_id=session_id,
client_turn_id=client_turn_id, message=message, origin=origin,
external_agent_target=external_agent_target)
diff --git a/loopx/bootstrap_command_pack.py b/loopx/bootstrap_command_pack.py
index 1c92b1409a..8451873ba5 100644
--- a/loopx/bootstrap_command_pack.py
+++ b/loopx/bootstrap_command_pack.py
@@ -846,6 +846,7 @@ def build_loopx_bootstrap_command_pack(
registry_path=registry_path,
runtime_root_arg=runtime_root_arg,
)
+ activation_registry = str(registry_path) if agent_type == "antigravity-app" else None
thread_binding = resolve_thread_agent_binding(
registry_goal,
host_surface=host_surface,
@@ -890,6 +891,7 @@ def build_loopx_bootstrap_command_pack(
goal_id=resolved_goal_id,
cli_bin=cli_bin,
runtime_root=command_runtime_root,
+ registry=activation_registry,
identity_runtime_root=identity_runtime_root,
agent_id=effective_agent_id,
registered_agents=registered_agents,
@@ -923,6 +925,7 @@ def build_loopx_bootstrap_command_pack(
resolved_goal_id,
cli_bin=cli_bin,
runtime_root=command_runtime_root,
+ registry_path=activation_registry,
agent_id=str(selected_agent_id) if selected_agent_id else None,
available_capabilities=available_capabilities,
begin_turn=guided_start_begins_turn,
diff --git a/loopx/canary/module_metric_baseline.json b/loopx/canary/module_metric_baseline.json
index cc89abf093..c42a77b450 100644
--- a/loopx/canary/module_metric_baseline.json
+++ b/loopx/canary/module_metric_baseline.json
@@ -39,7 +39,7 @@
"loopx/chat_runtime.py": {
"any_count": 38,
"dict_any_count": 0,
- "lines": 2503
+ "lines": 2524
},
"loopx/chat_server.py": {
"any_count": 25,
@@ -84,7 +84,7 @@
"lines": 2760
},
"loopx/extensions/lark/private_conversations.py": {
- "lines": 2191
+ "lines": 2583
},
"loopx/heartbeat_prompt.py": {
"any_count": 17,
diff --git a/loopx/capabilities/configuration_ui.py b/loopx/capabilities/configuration_ui.py
index 9be1689c3d..e991c2d0e9 100644
--- a/loopx/capabilities/configuration_ui.py
+++ b/loopx/capabilities/configuration_ui.py
@@ -83,8 +83,15 @@ def capability_configuration_editor(
# The steward namespace owns its closed vocabulary, so the editor offers the
# same values instead of restating them: a form cannot submit an executor or
# effort the owning namespace would reject.
- steward_endpoints, steward_efforts = _steward_executor_editor_options()
- from .native_chat.machine_defaults import codex_profile_options
+ steward_endpoints, steward_efforts = (
+ _steward_executor_editor_options()
+ if capability_id in ("codex_chat", "steward_executor") else ([], [])
+ )
+ codex_profiles: list[str] = []
+ if capability_id == "codex_chat":
+ from .native_chat.machine_defaults import codex_profile_options
+
+ codex_profiles = codex_profile_options()
definitions: dict[str, dict[str, Any]] = {
"context_delivery": {
"supported_scopes": ["machine"], "writable_scopes": ["machine"],
@@ -100,7 +107,7 @@ def capability_configuration_editor(
"supported_scopes": ["machine"], "writable_scopes": ["machine"],
"fields": [
_field("codex_profile", "Codex configuration profile", "select", nullable=True,
- options=codex_profile_options(), description="Existing native account/configuration home; credentials are not copied."),
+ options=codex_profiles, description="Existing native account/configuration home; credentials are not copied."),
_field("model", "Conversation model", "text", nullable=True,
description="For example gpt-6.1-sol. Blank inherits this Codex profile's default."),
_field("reasoning_effort", "Reasoning effort", "select", nullable=True,
diff --git a/loopx/capabilities/content_ops/reference.py b/loopx/capabilities/content_ops/reference.py
index 3a25ebcef2..d9650bb331 100644
--- a/loopx/capabilities/content_ops/reference.py
+++ b/loopx/capabilities/content_ops/reference.py
@@ -9,7 +9,6 @@
from typing import Any
from ...control_plane.effect_runtime import EffectRuntimeRejected, EffectRuntimeStartupError, effect_runtime_result
-from ..material_lifecycle.inventory import build_material_store_inventory
def content_reference_operation(operation: str, request: Mapping[str, Any]) -> dict[str, Any]:
@@ -28,6 +27,8 @@ def content_reference_operation(operation: str, request: Mapping[str, Any]) -> d
def inspect_reference_materials(*, library_path: Path, goal_id: str, store_id: str, observed_at: str) -> dict[str, Any]:
"""Inventory the original bytes; never infer lifecycle state or claim a backup."""
+ from ..material_lifecycle.inventory import build_material_store_inventory
+
source = library_path.read_bytes()
library = json.loads(source)
projection = content_reference_operation("search", {"library": library})
diff --git a/loopx/capabilities/issue_fix/pr_wait_hook.py b/loopx/capabilities/issue_fix/pr_wait_hook.py
index 74689d73dc..5eb7569006 100644
--- a/loopx/capabilities/issue_fix/pr_wait_hook.py
+++ b/loopx/capabilities/issue_fix/pr_wait_hook.py
@@ -13,7 +13,6 @@
TURN_START_HOOK_RESULT_SCHEMA_VERSION,
TurnStartHookRegistration,
)
-from .pr_wait_reconcile import reconcile_pr_wait_dependencies
PR_WAIT_OBSERVATION_HOOK_ID = "issue_fix.pr_wait_observation"
@@ -91,6 +90,9 @@ def produce() -> dict[str, Any]:
"external_read_count": 0, "state_written": False,
"summary_lines": ["Observation permission source is unavailable; provider not contacted."]}}
try:
+ # Optional provider loading follows the invocation-time permission recheck.
+ from .pr_wait_reconcile import reconcile_pr_wait_dependencies
+
readback = reconcile_pr_wait_dependencies(registry_path=registry_path,
runtime_root_arg=str(runtime_root), goal_id=goal_id, agent_id=agent_id,
fetch_timeout_seconds=fetch_timeout_seconds)
diff --git a/loopx/capabilities/issue_fix/reviewer_recommendation.py b/loopx/capabilities/issue_fix/reviewer_recommendation.py
index 1db50658ac..df2e7cf395 100644
--- a/loopx/capabilities/issue_fix/reviewer_recommendation.py
+++ b/loopx/capabilities/issue_fix/reviewer_recommendation.py
@@ -13,6 +13,10 @@
from ...control_plane.runtime.public_safety import public_safe_compact_text
from ...public_safe_text import COMPACT_LABEL_PATTERN as REVIEWER_SOURCE_ID_PATTERN
+from .repository_context import (
+ FRESHNESS_STATES as REVIEWER_SOURCE_FRESHNESS_STATES,
+ TRUST_LEVELS as REVIEWER_SOURCE_TRUST_LEVELS,
+)
ISSUE_FIX_REVIEWER_RECOMMENDATION_SCHEMA_VERSION = (
@@ -37,8 +41,6 @@
re.IGNORECASE,
)
REVIEWER_SOURCE_KINDS = {"maintainer_map"}
-REVIEWER_SOURCE_TRUST_LEVELS = {"authoritative", "verified", "advisory"}
-REVIEWER_SOURCE_FRESHNESS_STATES = {"current", "stale", "unknown"}
REVIEWER_SOURCE_MATCH_KINDS = {
"path_prefix",
"path_glob",
diff --git a/loopx/capabilities/native_chat/codex_adapter.py b/loopx/capabilities/native_chat/codex_adapter.py
index ca62c0f7b1..7fd5dd5d58 100644
--- a/loopx/capabilities/native_chat/codex_adapter.py
+++ b/loopx/capabilities/native_chat/codex_adapter.py
@@ -5,7 +5,9 @@
"""
from __future__ import annotations
-from dataclasses import dataclass, replace
+from contextlib import contextmanager
+from copy import deepcopy
+from dataclasses import dataclass, field, replace
import hashlib
import json
import logging
@@ -737,6 +739,7 @@ def turns() -> list[dict[str, Any]]:
class CodexAppServerAdapter:
session: CodexChatAgentSession
goal_driver: CodexGoalDriver | None = None
+ _managed_read_turn_guard: Callable[[], None] | None = field(default=None, repr=False)
def completed_work_observation(self, *, thread_ids: list[str], turn_id: str,
known_turn_ids: set[str] | None = None) -> dict[str, Any] | None:
@@ -1004,7 +1007,32 @@ def capabilities(self) -> dict[str, Any]:
}
def start_turn(self, message: str, event_sink: Callable[[str, dict[str, Any]], None]) -> dict[str, Any]:
- return self.session.send(message, on_event=event_sink)
+ return self.session.send(message, on_event=event_sink, **self._ordinary_turn_arguments())
+
+ def _ordinary_turn_arguments(self):
+ check = self._managed_read_turn_guard
+ if check is None:
+ return {}
+ previous_guard = self.session._launch_guard
+ @contextmanager
+ def read_turn():
+ check()
+ if previous_guard is None:
+ yield
+ else:
+ with previous_guard():
+ check()
+ yield
+ # Native sandboxPolicy is sticky across subsequent Turns. Reassert the
+ # current managed Chat read boundary without changing Session defaults.
+ return {"sandbox_policy": {"type": "readOnly", "networkAccess": False},
+ "turn_start_guard": read_turn}
+
+ def start_native_task_turn(self, message: str,
+ event_sink: Callable[[str, dict[str, Any]], None], policy: dict[str, object]) -> dict[str, Any]:
+ """Transport one Core-admitted Task through its existing launch fence."""
+ return self.session.send(message, on_event=event_sink, _native_task=True,
+ sandbox_policy=deepcopy(policy), turn_start_guard=self.session._launch_guard)
def steer_turn(self, message: str, expected_turn_id: str, client_message_id: str | None = None) -> str:
return self.session.steer(message, expected_turn_id=expected_turn_id,
@@ -1021,7 +1049,8 @@ def start_turn_with_attachments(
event_sink: Callable[[str, dict[str, Any]], None],
attachments: list[dict[str, Any]],
) -> dict[str, Any]:
- return self.session.send(message, attachments=attachments, on_event=event_sink)
+ return self.session.send(message, attachments=attachments, on_event=event_sink,
+ **self._ordinary_turn_arguments())
def interrupt_turn(self, turn_id: str | None = None) -> None:
driver = self.goal_driver
@@ -1053,6 +1082,60 @@ def start_attachment_turn(adapter: Any, session: dict[str, Any], turn: dict[str,
return adapter.start_turn_with_attachments(message, event_sink, attachments)
+def prepare_native_turn_text(session: CodexChatAgentSession, user_message: str, *,
+ output_schema, project_collaboration: bool, native_task: bool,
+ sandbox_policy, turn_start_guard):
+ """Preserve Chat input validation; adapt only the fenced Task invocation.
+
+ The Core caller owns Task/lease/permission admission. This transport cannot
+ manufacture that grant from a policy dictionary or a model/browser payload.
+ """
+ text = str(user_message or "")
+ if not text.strip():
+ raise ValueError("user message is required")
+ if native_task:
+ workspace_policy = {"type": "workspaceWrite", "writableRoots": [str(session.work_dir.resolve())],
+ "networkAccess": False, "excludeTmpdirEnvVar": True, "excludeSlashTmp": True}
+ if (turn_start_guard is None or session._launch_guard is None
+ or turn_start_guard is not session._launch_guard
+ or sandbox_policy not in ({"type": "readOnly", "networkAccess": False}, workspace_policy)
+ or session.execution_mode or session.read_only_review or session.project_context is not None
+ or session.permissions_profile is not None or session.sandbox != "read-only"):
+ raise ValueError("native Task transport requires the original read-only Session, bound workspace policy and current host launch guard")
+ elif sandbox_policy is not None and sandbox_policy != {"type": "readOnly", "networkAccess": False}:
+ raise ValueError("an ordinary Chat Turn cannot install a native execution policy")
+ if sandbox_policy is not None and session.permissions_profile is not None:
+ raise ValueError("a native sandbox policy cannot replace an existing named permissions profile")
+ if session.read_only_review and not session.metadata_review and not session.answer_review:
+ text = (
+ "Review the existing results of the interrupted original request below. "
+ "Its earlier execution may have already changed files or external systems. "
+ "Use the original native history and current local artifacts to verify what actually happened. "
+ "This Turn is read-only: do not re-execute the business request, write, send, delegate or start background work. "
+ "Return a clear answer to the original user, distinguishing verified results from remaining gaps. "
+ "If verification is incomplete, report the concrete limitation; do not claim completion. "
+ "Original request: " + text
+ )
+ if project_collaboration:
+ if (session.execution_mode or session.read_only_review or output_schema is not None
+ or not session.project_context or session.project_context.get("audience") != "bound_owner"):
+ raise ValueError("original project collaboration requires its existing bound project session")
+ from ...control_plane.effect_runtime import effect_runtime_result
+ output_schema = effect_runtime_result("collaboration.project.response_schema", {})
+ if output_schema is not None and not session.execution_mode and not project_collaboration and not native_task:
+ raise ValueError("structured Turn output requires an execution session")
+ return text, output_schema
+
+
+def native_runtime_context(controller: ChatRuntimeController, *, goal_id: str | None,
+ objective: str, manager_profile: Mapping[str, object] | None,
+ project_coordination: bool, execution_mode: bool) -> dict[str, str]:
+ """Compose the shared native adapter inputs in their construction owner."""
+ return {"objective": controller._session_objective(goal_id=goal_id, objective=objective,
+ project_coordination=project_coordination and not execution_mode),
+ "runtime_profile": str(manager_profile["runtime_profile"]) if manager_profile else "restricted"}
+
+
def start_runtime_adapter(controller: ChatRuntimeController, *, work_dir: Path, goal_id: str | None,
native_context: Mapping[str, str], manager_profile: Mapping[str, object] | None,
model_config: Mapping[str, str | None], history: list[dict[str, str]] | None,
@@ -1061,13 +1144,16 @@ def start_runtime_adapter(controller: ChatRuntimeController, *, work_dir: Path,
read_only_review: bool, metadata_review_context: dict[str, object] | None,
answer_review_context: dict[str, object] | None,
history_check: Callable[[], None] | None = None,
- working_continuation: NativeWorkingContinuation | None = None) -> CodexAppServerAdapter:
+ working_continuation: NativeWorkingContinuation | None = None,
+ check_preparation: bool = False,
+ managed_read_default: bool = False) -> CodexAppServerAdapter:
"""Build the native adapter from already-authorized runtime arguments."""
from ...chat_manager import MANAGER_AGENT_GOAL_ID
from ..manager_context.inspection import READ_TOOL, CONTEXT_READ_TOOL
from ...chat_loopx_mode import TOOL as COLLABORATION_TOOL
+ from ...chat.coordination import check_preparation_tool
review_context = metadata_review_context or answer_review_context
- return CodexAppServerAdapter.start(
+ adapter = CodexAppServerAdapter.start(
codex_bin=controller.codex_bin,
account_runtime_root=controller.coordination_runtime_root,
account_consumer="steward" if manager_profile is not None or review_context is not None else "worker",
@@ -1101,8 +1187,13 @@ def start_runtime_adapter(controller: ChatRuntimeController, *, work_dir: Path,
service_tier=model_config.get("service_tier"),
dynamic_tools=[READ_TOOL] if answer_review_context else (
[READ_TOOL] if goal_id == MANAGER_AGENT_GOAL_ID
- else [CONTEXT_READ_TOOL, *([COLLABORATION_TOOL] if loopx_tools else [])]
+ else [CONTEXT_READ_TOOL, *([COLLABORATION_TOOL] if loopx_tools else
+ [check_preparation_tool()] if check_preparation and resume_thread_id is None
+ and working_continuation is None else [])]
) if not execution_mode and not read_only_review and (
goal_id == MANAGER_AGENT_GOAL_ID or project_coordination
) else None,
)
+ if managed_read_default:
+ adapter.session._managed_goal_read_default = True
+ return adapter
diff --git a/loopx/capabilities/native_chat/creation_queue.py b/loopx/capabilities/native_chat/creation_queue.py
new file mode 100644
index 0000000000..ff7753938d
--- /dev/null
+++ b/loopx/capabilities/native_chat/creation_queue.py
@@ -0,0 +1,131 @@
+"""IO for the original creation request; the existing Session queue executes it."""
+from pathlib import Path
+from typing import Any
+import logging
+
+def creation_request_facts(controller: Any, session_id: str, *, proposal_id: str | None = None,
+ basis: dict[str, Any] | None = None) -> dict[str, Any] | None:
+ actions = getattr(controller, "original_task_action_store", None)
+ if actions is None:
+ return None
+ session = controller.store.load_session(session_id)
+ if not session or not session.get("managed_agent_id"):
+ return None
+ candidates = [actions.load(proposal_id)] if proposal_id is not None else actions.list()
+ candidates = [row for row in candidates if isinstance(row, dict)
+ and row.get("action_kind") == "goal.create"
+ and (row.get("normalized_parameters") or {}).get("start_execution") is True
+ and (row.get("normalized_parameters") or {}).get("goal_id") == session.get("goal_id")
+ and (((row.get("checkpoint") or {}).get("steps") or {}).get("first_session_opened") or {}).get(
+ "session_id") == session_id]
+ if not candidates:
+ return None
+ # Candidate filtering is IO narrowing, not authorization. Only explicit
+ # same-Session source candidates load native/canonical owners below.
+ from .steward_managed_mode import StewardManagedMode
+ from ...control_plane.effect_runtime import effect_runtime_result, EffectRuntimeRejected
+ from ...control_plane.goals.acceptance import inspect_goal_acceptance
+
+ goal = StewardManagedMode(controller, None)._goal(session.get("goal_id"))
+ if goal is None:
+ return None
+ # This registered creation identity is the action owner's proposal key,
+ # not a label or a parsed operation prefix.
+ source = proposal_id if proposal_id is not None else goal.get("creation_operation_id")
+ if not isinstance(source, str) or not source:
+ return None
+ proposal = next((row for row in candidates if row["proposal_id"] == source), None)
+ if (proposal is None or proposal.get("action_kind") != "goal.create"
+ or (proposal.get("normalized_parameters") or {}).get("start_execution") is not True):
+ return None
+ if basis is None:
+ basis = inspect_goal_acceptance(registry_path=controller.registry_path,
+ runtime_root=str(controller.coordination_runtime_root), goal_id=goal["goal_id"],
+ agent_id=session["managed_agent_id"], validation_workspace_path=Path(goal["workspace_path"]))
+ try:
+ selected = effect_runtime_result("goal.task.creation_selection", {"basis": basis})
+ except EffectRuntimeRejected as exc:
+ raise ValueError(str(exc)) from exc
+ return {"proposal": proposal, "session": session, "goal": goal, "current_original_task": selected}
+
+
+def project_creation_request(facts: dict[str, Any] | None) -> dict[str, Any] | None:
+ if facts is None:
+ return None
+ from ...control_plane.effect_runtime import effect_runtime_result, EffectRuntimeRejected
+ try:
+ request = effect_runtime_result("goal.creation.execution_request", facts)
+ except EffectRuntimeRejected as exc:
+ raise ValueError(str(exc)) from exc
+ if request is not None and not isinstance(request, dict):
+ raise ValueError("original creation request projection returned an invalid result")
+ return request
+
+
+def consume_creation_request(controller: Any, session_id: str) -> bool:
+ """Return whether this creation needs attention, never replaying its Turn."""
+ facts = creation_request_facts(controller, session_id)
+ if facts is None or facts["proposal"].get("status") not in ("applying", "applied"):
+ return False
+ if not isinstance(((facts["proposal"].get("checkpoint") or {}).get("steps") or {}).get(
+ "original_task_requested"), dict):
+ return False
+ try:
+ request = project_creation_request(facts)
+ if request is None:
+ return True # The same creation has not committed its request yet.
+ from .steward_managed_mode import ManagedSessionTurn
+ return ManagedSessionTurn(controller).consume_creation_request(request)
+ except (ValueError, RuntimeError) as exc:
+ # Missing/changed checks or source authority leave the saved request
+ # open. A content-free action/Turn signal or restart rechecks it.
+ logging.getLogger(__name__).debug("Original creation request retained: %s", exc)
+ return True
+
+
+def queued_turn_unstarted(turn: dict[str, Any]) -> bool:
+ """Unknown native publication facts cannot authorize a queue dispatch."""
+ return (turn.get("status") == "queued" and "_acceptance" not in turn
+ and "started_at" in turn and turn["started_at"] is None
+ and "upstream_turn_id" in turn and turn["upstream_turn_id"] is None
+ and type(turn.get("delta_count")) is int and turn["delta_count"] == 0)
+
+
+def resume_creation_requests(controller: Any) -> None:
+ """Discover only saved requests at service setup; hints grant no execution."""
+ actions = getattr(controller, "original_task_action_store", None)
+ if actions is None or controller.closed.is_set():
+ return
+ for proposal in actions.list():
+ if (proposal.get("action_kind") != "goal.create"
+ or (proposal.get("normalized_parameters") or {}).get("start_execution") is not True):
+ continue
+ request = (proposal.get("receipt") or {}).get("original_task_request")
+ if not isinstance(request, dict):
+ continue
+ session_id = request.get("session_id")
+ if not isinstance(session_id, str):
+ continue
+ try:
+ facts = creation_request_facts(controller, session_id, proposal_id=proposal["proposal_id"])
+ qualified = project_creation_request(facts)
+ if qualified is None or qualified != request:
+ continue
+ existing = controller.store.turn_for_client(session_id, "managed." + qualified["operation_id"])
+ if existing is not None:
+ if not queued_turn_unstarted(existing):
+ continue
+ from .steward_managed_mode import ManagedSessionTurn
+ expected = ManagedSessionTurn(controller)._request(session_id, qualified["agent_id"],
+ qualified["operation_id"], qualified["todo_id"])
+ expected["creation_request"] = qualified
+ if existing.get("loopx_request") != expected:
+ continue
+ session = facts["session"]
+ except (KeyError, OSError, ValueError, RuntimeError):
+ # A malformed candidate cannot wake another Session's queue. Its
+ # current-source refusal remains available through the Task GET.
+ continue
+ controller.resume_session_queue(session_id=session_id,
+ work_dir=Path(session["managed_workspace_path"]),
+ objective="Continue the original Goal's requested Task.")
diff --git a/loopx/capabilities/native_chat/external_conversations.py b/loopx/capabilities/native_chat/external_conversations.py
index f3c547215c..081e83bb6c 100644
--- a/loopx/capabilities/native_chat/external_conversations.py
+++ b/loopx/capabilities/native_chat/external_conversations.py
@@ -27,7 +27,7 @@
from ...file_lock import exclusive_file_lock, try_exclusive_file_lock
from . import result_review_evidence
from .codex_context import checked_session_home
-from .file_discovery import FileChatDiscovery, later_equivalent_source_input, source_control_fields
+from .file_discovery import FileChatDiscovery, external_source_route_path, later_equivalent_source_input, source_control_fields
# Request journals are named by their 24-hex request_ref; lock metadata and
# other siblings in this directory are not requests.
@@ -548,7 +548,7 @@ def source_requests() -> list[dict[str, Any]]:
# route already owns the fence, leave this request unchanged for
# the existing worker's next fresh visit, rather than admitting
# from an observation that waited behind that route.
- with try_exclusive_file_lock(self.root / "source-fences" / f"{row['binding_id']}.{row['source']['source_ref']}.json", operation="route_external_chat_request") as acquired:
+ with try_exclusive_file_lock(external_source_route_path(self.root, row["binding_id"], row["source"]["source_ref"]), operation="route_external_chat_request") as acquired:
if acquired is None:
return row
current = self.bindings.read()
@@ -740,7 +740,7 @@ def _recover_native_result(self, path: Path, row: dict[str, Any], session: dict[
proof = recovered.get("native_observation")
selected = self.bindings.resolve(binding_id=row["binding_id"], **row["source"], include_observation=True)
source = source_observation(selected)
- with try_exclusive_file_lock(self.root / "source-fences" / f"{row['binding_id']}.{row['source']['source_ref']}.json",
+ with try_exclusive_file_lock(external_source_route_path(self.root, row["binding_id"], row["source"]["source_ref"]),
operation="route_external_chat_request") as acquired:
if acquired is None:
return row
@@ -826,8 +826,7 @@ def admit_history_answer(self, *, binding_id: str, source: dict[str, Any], reque
raise ValueError("invalid historical answer source")
path = self.root / f"{request_ref}.json"
with exclusive_file_lock(path, operation="admit_external_chat_request"):
- with exclusive_file_lock(self.root / "source-fences" / f"{binding_id}.{source['source_ref']}.json",
- operation="route_external_chat_request"):
+ with exclusive_file_lock(external_source_route_path(self.root, binding_id, source["source_ref"]), operation="route_external_chat_request"):
selected = self.bindings.resolve(binding_id=binding_id, **source, include_observation=True)
if source_preflight(deepcopy(selected)) is not None:
raise ValueError("source preflight must verify or raise")
@@ -1391,7 +1390,8 @@ def admit(self, *, binding_id: str, source: dict[str, Any], request_ref: str,
# A duplicate waits on its own request before taking the route fence;
# it must not hold up other messages while native cancellation runs.
with exclusive_file_lock(path, operation="admit_external_chat_request"):
- with exclusive_file_lock(self.root / "source-fences" / f"{binding_id}.{source['source_ref']}.json", operation="route_external_chat_request"):
+ with exclusive_file_lock(external_source_route_path(self.root, binding_id, source["source_ref"]),
+ operation="route_external_chat_request"):
# Resolve and verify the source inside both fences before
# creating or replaying any canonical request.
selected = resolve_external_admission_source(self.bindings, binding_id, source, source_preflight)
diff --git a/loopx/capabilities/native_chat/file_discovery.py b/loopx/capabilities/native_chat/file_discovery.py
index 7fd1a5a27f..1454dde4b8 100644
--- a/loopx/capabilities/native_chat/file_discovery.py
+++ b/loopx/capabilities/native_chat/file_discovery.py
@@ -11,6 +11,11 @@
from typing import Any
+def external_source_route_path(root: Path, binding_id: str, source_ref: str) -> Path:
+ """Locate the original source's cross-process admission fence."""
+ return root / "source-fences" / f"{binding_id}.{source_ref}.json"
+
+
def _ordinary_request_text_digest(row: dict[str, Any]) -> str | None:
"""Match ordinary private text without retaining its body in discovery."""
message = row.get("message")
diff --git a/loopx/capabilities/native_chat/queue_api.py b/loopx/capabilities/native_chat/queue_api.py
index 17be78f6d3..805fab86cb 100644
--- a/loopx/capabilities/native_chat/queue_api.py
+++ b/loopx/capabilities/native_chat/queue_api.py
@@ -1,11 +1,42 @@
-"""HTTP projection and explicit steering of the existing persisted queue."""
+"""HTTP transport for original Session Tasks and the existing persisted queue."""
from typing import Any
+from urllib.parse import parse_qs, urlparse
from ...chat_store import CHAT_STORE_SCHEMA_VERSION
class ChatQueueRequestMixin:
+ def _managed_native_turn(self, session_id: str, *, execute: bool = False) -> None:
+ from .steward_managed_mode import ManagedSessionTurn
+ service = ManagedSessionTurn(self.server.runtime_controller)
+ try:
+ if execute:
+ body = self._read_json()
+ if set(body) - {"agent_id", "operation_id", "todo_id"}:
+ raise ValueError("unsupported managed Turn arguments")
+ turn, created = service.submit(session_id, **body)
+ return self._send_json({"ok": True, "session_id": session_id, "turn": turn, "created": created}, status=202)
+ query = parse_qs(urlparse(self.path).query, keep_blank_values=True)
+ sources = query.get("calendar_task_sources", ["false"])
+ if sources not in (["true"], ["false"]):
+ raise ValueError("calendar_task_sources must be one true or false value")
+ include_sources = sources == ["true"]
+ original_task = query.get("original_task", ["false"])
+ if original_task not in (["true"], ["false"]):
+ raise ValueError("original_task must be one true or false value")
+ include_original_task = original_task == ["true"]
+ if (include_sources or include_original_task) and (set(query) - {"calendar_task_sources", "original_task", "agent_id", "operation_id"}
+ or any(len(values) != 1 for values in query.values())):
+ raise ValueError("select one original managed Session and Agent for Task sources")
+ self._send_json(service.inspect(session_id, agent_id=query.get("agent_id", [None])[0],
+ operation_id=query.get("operation_id", [None])[0], include_calendar_task_sources=include_sources,
+ include_original_task=include_original_task))
+ except KeyError:
+ self._send_error("chat session was not found", status=404)
+ except (TypeError, ValueError, RuntimeError) as exc:
+ self._send_error(str(exc), status=409, error_code="managed_turn_unavailable")
+
def _dispatch_chat_queue(self, path: str, *, apply: bool) -> bool:
parts = path.strip("/").split("/")
if parts[:3] != ["api", "chat", "sessions"] or len(parts) < 5 or parts[4] != "queue":
diff --git a/loopx/capabilities/native_chat/session_queue.py b/loopx/capabilities/native_chat/session_queue.py
new file mode 100644
index 0000000000..217e882154
--- /dev/null
+++ b/loopx/capabilities/native_chat/session_queue.py
@@ -0,0 +1,170 @@
+"""The existing same-Session FIFO, preparation and recovery consumer."""
+from pathlib import Path
+from typing import Any
+import threading
+
+from ...chat_store import CHAT_SESSION_MODE_ATTACHED, TERMINAL_TURN_STATES
+
+
+def drain_session_queue(controller: Any, *, session_id: str, work_dir: Path, objective: str) -> None:
+ wakeup = threading.Event()
+ with controller.turn_release_wakeup(wakeup):
+ _drain_session_queue_events(controller, session_id=session_id, work_dir=work_dir,
+ objective=objective, wakeup=wakeup)
+
+
+def _drain_session_queue_events(controller: Any, *, session_id: str, work_dir: Path,
+ objective: str, wakeup: threading.Event) -> None:
+ try:
+ while not controller.closed.is_set():
+ # Register and clear before reading durable state. A signal
+ # during this read remains visible when a pending request waits.
+ wakeup.clear()
+ session = controller.store.load_session(session_id)
+ if (
+ controller.closed.is_set()
+ or session is None
+ or session.get("status") == "closed"
+ or session.get("session_mode") == CHAT_SESSION_MODE_ATTACHED
+ ):
+ return
+ turn = None
+ active_turn_id = str(session.get("active_turn_id") or "")
+ if active_turn_id:
+ try:
+ controller.recover_exited_turn(session_id, active_turn_id)
+ except OSError:
+ # Storage can still be unavailable. Retry only the
+ # non-executing lifecycle repair, never the old input.
+ controller.closed.wait(0.25)
+ continue
+ active = controller.store.load_turn(session_id, active_turn_id)
+ if active and isinstance((active.get("loopx_request") or {}).get("creation_request"), dict):
+ if active.get("status") == "queued":
+ from .creation_queue import consume_creation_request
+ try:
+ pending = consume_creation_request(controller, session_id)
+ except (OSError, ValueError, RuntimeError):
+ pending = True
+ if not pending:
+ # The native worker can terminalize after this
+ # queue cached its queued state. Reconcile the
+ # current FIFO instead of retiring with ordinary
+ # messages stranded behind the released operation.
+ current = controller.store.load_session(session_id)
+ latest = controller.store.load_turn(session_id, active_turn_id)
+ if current is None:
+ return
+ if (current.get("active_turn_id") != active_turn_id
+ or latest and latest.get("status") in TERMINAL_TURN_STATES):
+ continue
+ # A still-active unknown operation grants no
+ # dispatch. Keep its worker until a state signal.
+ wakeup.wait()
+ continue
+ if active.get("status") not in TERMINAL_TURN_STATES:
+ # An admitted or unknown native operation retains
+ # its original worker/result; never restore it as an
+ # ordinary queued message or start another model.
+ wakeup.wait()
+ continue
+ if (active and active.get("status") == "queued" and "_acceptance" not in active
+ and active.get("started_at") is None and active.get("upstream_turn_id") is None
+ and active.get("delta_count") == 0):
+ # A crash can leave the queue claim before native
+ # dispatch. Keep that exact claim and pass through the
+ # normal preparation and execution fences below.
+ turn = active
+ else:
+ with controller.lock:
+ attached = controller.adapters.get(session_id)
+ if attached is None or not attached.healthcheck():
+ try:
+ controller._ensure_adapter(
+ session,
+ work_dir=work_dir,
+ objective=objective,
+ )
+ except Exception as exc:
+ controller._fail_queue_preparation(session_id, active_turn_id, exc)
+ else:
+ continue
+ active = controller.store.load_turn(session_id, active_turn_id)
+ if active and active.get("status") not in TERMINAL_TURN_STATES:
+ controller.closed.wait(0.05)
+ continue
+ controller.store.release_active_turn(session_id, active_turn_id)
+ refreshed = controller.store.load_session(session_id)
+ if refreshed is None:
+ return
+ with controller.lock:
+ controller.session_queue_wakeups.discard(session_id)
+ if turn is None:
+ turn = controller.store.claim_next_queued_turn(session_id)
+ if turn is None:
+ from .creation_queue import consume_creation_request
+ try:
+ pending = consume_creation_request(controller, session_id)
+ except (OSError, ValueError, RuntimeError):
+ pending = True
+ if pending:
+ # The request lives in its creation receipt. Existing
+ # action/Turn signals and startup discovery re-read it;
+ # pending checks do not create a failed native Turn.
+ wakeup.wait()
+ continue
+ with controller.lock:
+ if session_id in controller.session_queue_wakeups:
+ continue
+ worker = controller.session_queue_threads.get(session_id)
+ if worker is threading.current_thread():
+ controller.session_queue_workers.discard(session_id)
+ controller.session_queue_threads.pop(session_id, None)
+ return
+ turn_id = str(turn["turn_id"])
+ try:
+ if turn.get("origin") == "peer" or "collaboration_request" in turn:
+ from .conversation_bindings import OriginalConversationRequests
+ OriginalConversationRequests(controller).validate_turn(session_id, turn)
+ # An empty queue needs no provider preparation. Claim real
+ # work first, keeping the existing enqueue/retirement fence
+ # above and the Turn's execution/stop checks below.
+ current = controller.store.load_session(session_id)
+ if current is None or current.get("active_turn_id") != turn_id:
+ return
+ adapter = controller._ensure_adapter(
+ current,
+ work_dir=work_dir,
+ objective=objective,
+ )
+ if (controller.store.load_session(session_id) or {}).get("active_turn_id") != turn_id:
+ return
+ except Exception as exc:
+ # The claimed request still owns a terminal outcome when
+ # workspace preparation or adapter restoration fails.
+ controller._fail_queue_preparation(session_id, turn_id, exc)
+ continue
+ done_event = threading.Event()
+ with controller.lock:
+ controller.turn_done_events[(session_id, turn_id)] = done_event
+ try:
+ controller._run_turn(
+ session_id=session_id,
+ turn_id=turn_id,
+ message=str(turn.get("message") or ""),
+ attachments=turn.get("attachments") or [],
+ adapter=adapter,
+ done_event=done_event,
+ )
+ except OSError:
+ # _run_turn has released its execution fence/done event.
+ # A failed error checkpoint must not kill this queue worker;
+ # the next iteration reconciles the original durable state.
+ controller.closed.wait(0.25)
+ finally:
+ with controller.lock:
+ worker = controller.session_queue_threads.get(session_id)
+ if worker is threading.current_thread():
+ controller.session_queue_workers.discard(session_id)
+ controller.session_queue_threads.pop(session_id, None)
+ controller.session_queue_wakeups.discard(session_id)
diff --git a/loopx/capabilities/native_chat/steward_managed_mode.py b/loopx/capabilities/native_chat/steward_managed_mode.py
index 5eea668732..bc5cf51d86 100644
--- a/loopx/capabilities/native_chat/steward_managed_mode.py
+++ b/loopx/capabilities/native_chat/steward_managed_mode.py
@@ -292,11 +292,23 @@ def admit(self, request: dict[str, Any], *, turn: dict[str, Any] | None = None,
if request.get("work_kind") == "scheduled_task":
from .session_calendar_tasks import SessionCalendarTasks
report["scheduled_task"] = SessionCalendarTasks(self.controller).admission(request)
+ creation = {}
+ if "creation_request" in request:
+ from .creation_queue import creation_request_facts
+ retained = request["creation_request"]
+ if not isinstance(retained, dict) or not isinstance(retained.get("creation_operation_id"), str):
+ raise ValueError("the original creation request is unavailable")
+ facts = creation_request_facts(self.controller, actor["session_id"],
+ proposal_id=retained["creation_operation_id"])
+ if facts is None:
+ raise ValueError("the original creation request source is unavailable")
+ creation = {"creation_proposal": facts["proposal"],
+ "current_original_task": facts["current_original_task"]}
return ChatConversationBindings._core("collaboration.session.managed_turn.admit", {
"session": session, "request": request, "turn": turn,
"goal": StewardManagedMode(self.controller, None)._goal(actor["goal_id"]),
"goal_active": bool(goal and goal.get("status") == "active" and not goal_is_stopped(goal)),
- "monitor_todo": monitor, **report,
+ "monitor_todo": monitor, **report, **creation,
**({"report_settlement_recovery": settlement_recovery} if settlement_recovery is not None else {}),
**({"report_selection_recovery": selection_recovery} if selection_recovery is not None else {}),
**StewardTurnHost(self.controller).report_quota_basis(request),
@@ -304,7 +316,8 @@ def admit(self, request: dict[str, Any], *, turn: dict[str, Any] | None = None,
"queued_turn_count": len(self.store.queued_turns(actor["session_id"]))})
def inspect(self, session_id: str, *, agent_id: str | None = None,
- operation_id: str | None = None, include_calendar_task_sources: bool = False) -> dict[str, Any]:
+ operation_id: str | None = None, include_calendar_task_sources: bool = False,
+ include_original_task: bool = False) -> dict[str, Any]:
session = self.store.load_session(session_id)
if not session:
raise KeyError("chat session was not found")
@@ -322,6 +335,10 @@ def inspect(self, session_id: str, *, agent_id: str | None = None,
result["available"] = True
except ValueError as exc:
result["reason"] = str(exc)
+ if include_original_task:
+ if agent_id is not None and agent_id != session.get("managed_agent_id"):
+ raise ValueError("select the original registered Agent for Task entry")
+ result["task"] = self._original_task_entry(session_id)
if session.get("managed_agent_id"):
from .steward_reports import ManagedSessionReports
from .session_calendar_tasks import SessionCalendarTasks
@@ -336,14 +353,67 @@ def inspect(self, session_id: str, *, agent_id: str | None = None,
result["calendar_task_sources"] = SessionCalendarTasks(self.controller).source_options(session_id)
return result
+ def creation_request(self, session_id: str) -> dict[str, Any] | None:
+ from .creation_queue import creation_request_facts, project_creation_request
+ return project_creation_request(creation_request_facts(self.controller, session_id))
+
+ def _original_task_entry(self, session_id: str) -> dict[str, Any]:
+ """Observe the existing creation/validation owners without any effects.
+
+ The returned plan does not claim a Task or authorize a model. Native
+ submit still rechecks current admission and every validation source.
+ """
+ from ...control_plane.effect_runtime import effect_runtime_result
+ from ...control_plane.goals.acceptance import inspect_goal_acceptance
+
+ session = self.store.load_session(session_id) or {}
+ request = self._request(session_id, session.get("managed_agent_id") or "", "original-task-preview")
+ actor = request["actor"]
+ unavailable: dict[str, Any] = {"todo_id": None, "state": "unbound", "source": None,
+ "canonical_done": False}
+ try:
+ creation = self.creation_request(session_id)
+ if creation is not None:
+ request = self._request(session_id, creation["agent_id"], creation["operation_id"], creation["todo_id"])
+ request["creation_request"] = creation
+ retained_turn = self.store.turn_for_client(session_id, "managed." + creation["operation_id"])
+ unavailable.update(creation_request=creation, creation_turn=None)
+ if retained_turn is not None and retained_turn.get("loopx_request") != request:
+ raise ValueError("the original creation operation belongs to another managed request")
+ unavailable["creation_turn"] = retained_turn
+ self.admit(request)
+ basis = inspect_goal_acceptance(registry_path=self.controller.registry_path,
+ runtime_root=str(self.controller.coordination_runtime_root), goal_id=actor["goal_id"],
+ agent_id=actor["agent_id"], validation_workspace_path=Path(actor["workspace_path"]))
+ selected = effect_runtime_result("goal.task.creation_selection", {"basis": basis})
+ if selected["todo_id"] is None:
+ return {**unavailable, "reason": selected["reason"]}
+ todo_id = selected["todo_id"]
+ unavailable["todo_id"] = todo_id
+ request["todo_id"] = todo_id
+ self.admit(request)
+ plan = self._validation_source(request, todo_id)["plan"]
+ # Return only the display observation; commands, pins and private
+ # declarations stay with their existing owner.
+ self.admit(request)
+ return {**{key: plan[key] for key in ("todo_id", "state", "source", "observation", "canonical_done")},
+ **{key: unavailable[key] for key in ("creation_request", "creation_turn") if key in unavailable}}
+ except (OSError, ValueError, RuntimeError) as exc:
+ reason = ("the original task source is temporarily unavailable" if isinstance(exc, OSError)
+ else str(exc))
+ return {**unavailable, "reason": reason}
+
def submit(self, session_id: str, *, agent_id: str, operation_id: str,
todo_id: str | None = None, monitor_deadline: str | None = None,
report_window: dict[str, Any] | None = None,
- scheduled_window: dict[str, Any] | None = None) -> tuple[dict[str, Any], bool]:
+ scheduled_window: dict[str, Any] | None = None,
+ _creation_request: dict[str, Any] | None = None) -> tuple[dict[str, Any], bool]:
from ...attached_session import managed_session_goal_commit
controller = self.controller
with controller._session_adapter_lock(session_id):
request = self._request(session_id, agent_id, operation_id, todo_id)
+ if _creation_request is not None:
+ request["creation_request"] = _creation_request
if scheduled_window is not None:
if report_window is not None or monitor_deadline is not None or not todo_id:
raise ValueError("a calendar task requires its original finite Todo")
@@ -410,29 +480,110 @@ def submit(self, session_id: str, *, agent_id: str, operation_id: str,
registry_path=controller.registry_path, session_id=session_id)
with commit():
admitted = self.admit(request, selection_recovery=selection_recovery)
+ if _creation_request is not None:
+ self._validation_source(request, _creation_request["todo_id"])
+ if not self._creation_allowance(request):
+ raise ValueError("the original creation task has no current preparation admission")
accepted = self.store.accept_managed_turn(session_id, client_turn_id=client_id,
message="Advance the currently admitted native LoopX work.",
display_message="生成本期定时报告。" if report_window is not None else
"执行本期定时任务。" if scheduled_window is not None else
"检查当前 Goal 原生任务状态。" if monitor_deadline is not None else "推进一次 LoopX 任务。",
origin="native_loopx", loopx_execution=True, loopx_request=request)
- try:
- session = self.store.load_session(session_id)
- if admitted["observation_only"]:
- from .steward_monitor import StewardObservationAdapter
- adapter = StewardObservationAdapter(session.get("upstream_thread_id"))
- else:
- adapter = controller._ensure_adapter_locked(session, work_dir=Path(admitted["workspace_path"]),
- objective="Advance this Goal's authorized work.", accepted_turn_id=accepted.turn["turn_id"])
- if Path(adapter.session.work_dir).resolve() != Path(admitted["workspace_path"]).resolve():
- raise ValueError("the original managed conversation workspace changed")
- controller._start_accepted_turn_worker(session_id=session_id, turn_id=accepted.turn["turn_id"],
- message=accepted.turn["message"], attachments=[], adapter=adapter, loopx_execution=True)
- except Exception as exc:
- controller._fail_queue_preparation(session_id, accepted.turn["turn_id"], exc)
- raise
+ self._dispatch_accepted_turn(accepted.turn, admitted)
return accepted.turn, accepted.created
+ def _dispatch_accepted_turn(self, turn: dict[str, Any], admitted: dict[str, Any]) -> None:
+ session_id = turn["session_id"]
+ request = turn.get("loopx_request") or {}
+ if "creation_request" in request:
+ from ...attached_session import managed_session_goal_commit
+ with managed_session_goal_commit(store=self.store, registry_path=self.controller.registry_path,
+ session_id=session_id)():
+ admitted = self.admit(request, turn=turn)
+ self._validation_source(request, request["todo_id"])
+ if not self._creation_allowance(request):
+ return # Retain the accepted, unstarted operation for a fresh state signal.
+ try:
+ session = self.store.load_session(session_id)
+ if admitted["observation_only"]:
+ from .steward_monitor import StewardObservationAdapter
+ adapter = StewardObservationAdapter(session.get("upstream_thread_id"))
+ else:
+ adapter = self.controller._ensure_adapter_locked(session,
+ work_dir=Path(admitted["workspace_path"]), objective="Advance this Goal's authorized work.",
+ accepted_turn_id=turn["turn_id"])
+ if Path(adapter.session.work_dir).resolve() != Path(admitted["workspace_path"]).resolve():
+ raise ValueError("the original managed conversation workspace changed")
+ self.controller._start_accepted_turn_worker(session_id=session_id, turn_id=turn["turn_id"],
+ message=turn["message"], attachments=[], adapter=adapter, loopx_execution=True)
+ except Exception as exc:
+ self.controller._fail_queue_preparation(session_id, turn["turn_id"], exc)
+ raise
+
+ def consume_creation_request(self, creation: dict[str, Any]) -> bool:
+ """Consume one saved request through the existing managed acceptance owner."""
+ from ...attached_session import managed_session_goal_commit
+ from .creation_queue import queued_turn_unstarted
+ session_id = creation["session_id"]
+ request = self._request(session_id, creation["agent_id"], creation["operation_id"], creation["todo_id"])
+ request["creation_request"] = creation
+ with self.controller._session_adapter_lock(session_id):
+ existing = self.store.turn_for_client(session_id, "managed." + creation["operation_id"])
+ if existing is not None:
+ if existing.get("loopx_request") != request:
+ raise ValueError("the original creation operation belongs to another managed request")
+ if not queued_turn_unstarted(existing):
+ return False # Unknown, failed and completed operations are never re-executed.
+ with managed_session_goal_commit(store=self.store, registry_path=self.controller.registry_path,
+ session_id=session_id)():
+ admitted = self.admit(request, turn=existing)
+ self._validation_source(request, creation["todo_id"])
+ if not self._creation_allowance(request):
+ return True
+ self._dispatch_accepted_turn(existing, admitted)
+ return True
+ self.admit(request)
+ self._validation_source(request, creation["todo_id"])
+ if not self._creation_allowance(request):
+ return True
+ self.submit(session_id, agent_id=creation["agent_id"], operation_id=creation["operation_id"],
+ todo_id=creation["todo_id"], _creation_request=creation)
+ return True
+
+ def _creation_allowance(self, request: dict[str, Any]) -> bool:
+ """Read the existing typed selected-work admission, without binding or effects.
+
+ Preparation may require the native owner to claim a lease. Its final
+ admission and launch guards still own execution after this observation.
+ """
+ from ...status import collect_status, AUTONOMOUS_REPLAN_PERIODIC_LOOKBACK
+ from ...control_plane.quota.live_decision import build_live_quota_should_run_decision
+ from ...control_plane.scheduler.execution_context import scheduler_execution_context_for_turn
+ from ...control_plane.work_items.action_selection_contract import current_action_selection_admission
+ from ...control_plane.goals.first_party_host_admission import capture_first_party_host_goal_ref
+ actor = request["actor"]
+ root = self.controller.coordination_runtime_root
+ status = collect_status(registry_path=self.controller.registry_path,
+ runtime_root_override=str(root), limit=AUTONOMOUS_REPLAN_PERIODIC_LOOKBACK,
+ goal_id=actor["goal_id"], agent_lane_id=actor["agent_id"],
+ available_capabilities=None, include_presentation_projections=False)
+ decision = build_live_quota_should_run_decision(status,
+ goal_id=actor["goal_id"], agent_id=actor["agent_id"], available_capabilities=None,
+ include_scheduler_detail=False, codex_app_current_rrule=None,
+ registry_path=self.controller.registry_path, runtime_root=root,
+ requested_action_todo_id=request["todo_id"], turn_instance_id=request["turn_instance_id"],
+ workspace_path=Path(actor["workspace_path"]),
+ goal_ref=capture_first_party_host_goal_ref(registry_path=self.controller.registry_path,
+ goal_id=actor["goal_id"]),
+ scheduler_execution_context=scheduler_execution_context_for_turn(
+ host="codex-app-server", execution_mode="isolated-headless"))
+ agent = (decision.get("interaction_contract") or {}).get("agent_channel") or {}
+ _, admitted = current_action_selection_admission(decision,
+ requested_todo_id=request["todo_id"], agent_must_attempt=agent.get("must_attempt") is True,
+ agent_delivery_allowed=agent.get("delivery_allowed"))
+ return admitted
+
def _validation_source(self, request: dict[str, Any], todo_id: str) -> dict[str, Any]:
from ...control_plane.effect_runtime import effect_runtime_result
from ...control_plane.goals.acceptance import inspect_goal_acceptance, validation_effect_files_current
diff --git a/loopx/capabilities/native_chat/steward_turn.py b/loopx/capabilities/native_chat/steward_turn.py
index b6f68efa18..96083a9160 100644
--- a/loopx/capabilities/native_chat/steward_turn.py
+++ b/loopx/capabilities/native_chat/steward_turn.py
@@ -819,7 +819,14 @@ def launch():
adapter.session._launch_guard = launch
try:
- response = adapter.start_turn(prompt, event_sink)
+ if managed_turn and not governance_work:
+ from ...control_plane.effect_runtime import effect_runtime_result
+ policy = effect_runtime_result("collaboration.session.managed_turn.sandbox_policy", {
+ "host": request["host"], "actor": actor, "authority": authority,
+ "validation": managed_turn.validation_basis["plan"], "task_lease": managed_lease})
+ response = adapter.start_native_task_turn(prompt, event_sink, policy)
+ else:
+ response = adapter.start_turn(prompt, event_sink)
finally:
adapter.session._launch_guard = previous_guard
current() # Revocation/stop/retarget after a long model call wins.
diff --git a/loopx/capabilities/periodic_report/pending_intent.py b/loopx/capabilities/periodic_report/pending_intent.py
index c1164f0093..6c3e746d2a 100644
--- a/loopx/capabilities/periodic_report/pending_intent.py
+++ b/loopx/capabilities/periodic_report/pending_intent.py
@@ -19,7 +19,6 @@
from ...control_plane.digest_envelope import sha256_envelope
from ...history import load_registry
from .field_shapes import REPORT_IDENTITY_PATTERN
-from .todo_source import read_report_todo_source
from ...registry import (
atomic_write_json,
find_registry_goal,
@@ -37,11 +36,6 @@
request_entry_for_intent,
settle_periodic_report_request,
)
-from .project_progress_snapshot import build_project_progress_snapshot
-from .incremental import (
- build_periodic_report_publication_candidate,
- write_periodic_report_publication_candidate,
-)
from .machine_defaults import (
build_periodic_report_delivery_authority,
resolve_active_goal_periodic_report_subscription,
@@ -175,6 +169,8 @@ def _superseding_approval_revision(
approval_scope = str(receipt.get("approval_scope") or "")
if receipt.get("status") != "approval_pending" or not approval_scope:
return None
+ from .todo_source import read_report_todo_source
+
_, items = read_report_todo_source(
registry_path=registry_path, goal_id=goal_id, runtime_root=runtime_root
)
@@ -513,6 +509,8 @@ def _progress_facts(
available_capabilities: Any = None,
allow_empty: bool = False,
) -> list[dict[str, Any]]:
+ from .project_progress_snapshot import build_project_progress_snapshot
+
from ...control_plane.todos.todo_index import (
MAX_TODO_INDEX_ROLLOUT_EVENTS_PER_GOAL,
)
@@ -1317,6 +1315,11 @@ def _consume_pending_periodic_report_intent(
if not execute:
return result
+ from .incremental import (
+ build_periodic_report_publication_candidate,
+ write_periodic_report_publication_candidate,
+ )
+
receipt_path = _receipt_path(
runtime_root,
goal_id,
diff --git a/loopx/chat/coordination.py b/loopx/chat/coordination.py
index 1f47003fcd..f6f57f06eb 100644
--- a/loopx/chat/coordination.py
+++ b/loopx/chat/coordination.py
@@ -6,6 +6,14 @@
from __future__ import annotations
+from copy import deepcopy
+import hashlib
+import os
+from pathlib import Path
+import shutil
+import stat
+import sys
+
PROJECT_COORDINATION_GUIDANCE = (
"This conversation concerns only the selected LoopX Goal. Read its current work, "
"member commitments and evidence before suggesting changes. Use loopx_context_read "
@@ -32,6 +40,220 @@
PROJECT_CONTEXT_VERSION = 2
+def check_preparation_tool():
+ """Project the existing proposal action for a newly bound managed thread."""
+ from ..chat_loopx_mode import TOOL
+ tool = deepcopy(TOOL)
+ tool["description"] = (
+ "Prepare a reviewable delivery check for this original registered Goal and Agent. "
+ "Use action=prepare_check with a stable operation_id and the existing compact "
+ "acceptance document: outcome_id and criteria [{id, validation_argv, oracle}]. "
+ "Read retained requirements with loopx_context_read first. Select an existing "
+ "independent checker from this Goal workspace using [python3, relative.py] or "
+ "[relative/executable], without options or extra arguments. The host pins its "
+ "actual source bytes and verifies python3 resolves to the host interpreter. Each oracle "
+ "is {kind: owner_pinned_oracle, source_id, input_refs, output_refs}; name the "
+ "explicit delivery files in output_refs. Do not invent a checker or replace "
+ "owner intent. The host supplies Goal, Agent, workspace and runtime identity. "
+ "Preparation saves only a goal.acceptance.check proposal. Explain its purpose "
+ "and effects for local-owner review; it neither configures acceptance nor runs "
+ "checks, claims work or launches a Turn. The owner judges whether the checker "
+ "addresses the requested outcome before applying it."
+ )
+ properties = tool["inputSchema"]["properties"]
+ tool["inputSchema"]["properties"] = {
+ key: properties[key] for key in ("action", "operation_id", "document")
+ }
+ tool["inputSchema"]["properties"]["document"]["description"] = (
+ "Existing compact acceptance document with outcome_id, criteria and optional scope. "
+ "Each criterion selects an existing requirement id, supported validation_argv, "
+ "and owner_pinned_oracle with explicit source_id, input_refs and output_refs. "
+ "Optional validation_files must match actual host source pins."
+ )
+ tool["inputSchema"]["properties"]["action"]["enum"] = ["prepare_check"]
+ tool["inputSchema"]["required"] = ["action", "operation_id", "document"]
+ return tool
+
+
+def _workspace_file(workspace: Path, ref: str, *, required: bool) -> str | None:
+ """Observe a regular relative file without following any symlink component.
+
+ Missing delivery files are legal during preparation. Descriptor-relative
+ opens keep a replaced parent from redirecting a source read outside the
+ original workspace; source digests are host observations, not verdicts.
+ """
+ directory_only = getattr(os, "O_DIRECTORY", None)
+ no_follow = getattr(os, "O_NOFOLLOW", None)
+ nonblock = getattr(os, "O_NONBLOCK", None)
+ if (
+ os.open not in os.supports_dir_fd
+ or directory_only is None
+ or no_follow is None
+ or nonblock is None
+ ):
+ raise ValueError("this host cannot safely inspect workspace checker sources")
+ if (not isinstance(ref, str) or not ref or "\\" in ref or "\0" in ref
+ or ref.startswith("/") or any(part in {"", ".", ".."} for part in ref.split("/"))):
+ raise ValueError("checker files must remain relative to the original workspace")
+ descriptors = []
+ try:
+ directory_flags = os.O_RDONLY | directory_only | no_follow
+ parent = os.open(workspace, directory_flags)
+ descriptors.append(parent)
+ parts = ref.split("/")
+ for part in parts[:-1]:
+ parent = os.open(part, directory_flags, dir_fd=parent)
+ descriptors.append(parent)
+ source = os.open(parts[-1], os.O_RDONLY | no_follow | nonblock, dir_fd=parent)
+ descriptors.append(source)
+ if not stat.S_ISREG(os.fstat(source).st_mode):
+ raise ValueError("checker sources and present artifacts must be regular files")
+ if not required:
+ return None
+ digest = hashlib.sha256()
+ while chunk := os.read(source, 1024 * 1024):
+ digest.update(chunk)
+ return digest.hexdigest()
+ except FileNotFoundError:
+ if not required:
+ return None
+ raise ValueError("the selected independent checker source is unavailable") from None
+ except OSError:
+ raise ValueError("checker sources and present artifacts must be readable regular files without symlinks") from None
+ finally:
+ for descriptor in reversed(descriptors):
+ os.close(descriptor)
+
+
+class _GoalSessionCheckPreparation:
+ """Per-Turn source IO over the existing typed identity and acceptance owners."""
+ def __init__(self, controller, adapter, session, turn_id, read):
+ self.controller, self.adapter, self.read = controller, adapter, read
+ self.session_id, self.turn_id = session["session_id"], turn_id
+ self.workspace = Path(adapter.session.work_dir)
+ self.runtime_root = controller.coordination_runtime_root
+ self.registry_path = controller.registry_path
+ self.original = deepcopy(session)
+ self.identity = self._current_identity()
+
+ def _current_identity(self):
+ from ..capabilities.native_chat.steward_managed_mode import registered_managed_session_identity
+ from ..control_plane.collaboration import conversation_scope
+ controller = self.controller
+ session = controller.store.load_session(self.session_id) or {}
+ turn = controller.store.load_turn(self.session_id, self.turn_id) or {}
+ fields = ("session_id", "goal_id", "goal_instance_id", "managed_agent_id",
+ "managed_workspace_path", "agent_id", "executor_endpoint_id", "adapter_kind",
+ "session_mode", "upstream_mode", "upstream_thread_id", "channel_id",
+ "project_context", "steward_context", "codex_home", "loopx_tools", "loopx_mode")
+ if (controller.closed.is_set() or controller.registry_path != self.registry_path
+ or controller.coordination_runtime_root != self.runtime_root
+ or any(session.get(key) != self.original.get(key) for key in fields)
+ or session.get("active_turn_id") != self.turn_id
+ or turn.get("session_id") != self.session_id or turn.get("turn_id") != self.turn_id
+ or turn.get("status") not in {"starting", "running"} or turn.get("origin") != "web"
+ or turn.get("loopx_execution") or turn.get("loopx_request")
+ or "collaboration_request" in turn or session.get("upstream_mode") != "chat"
+ or session.get("loopx_tools") or (session.get("loopx_mode") or {}).get("enabled")
+ or self.adapter.session.thread_id != session.get("upstream_thread_id")
+ or self.adapter.session.execution_mode or self.adapter.session.read_only_review
+ or getattr(self.adapter, "goal_driver", None) is not None
+ or Path(self.adapter.session.work_dir) != self.workspace
+ or conversation_scope(session, origin="web")["kind"] != "owner_goal"):
+ raise ValueError("the original local-owner managed Session and active read-only Turn are unavailable")
+ return registered_managed_session_identity(registry_path=self.registry_path,
+ goal_id=session.get("goal_id"), agent_id=session.get("managed_agent_id"),
+ workspace=self.workspace, session=session)
+
+ def _current(self):
+ if self._current_identity() != self.identity:
+ raise ValueError("the original managed Goal lifetime or Agent scope changed")
+
+ @staticmethod
+ def _python_interpreter():
+ executable = shutil.which("python3")
+ if executable is None or Path(executable).resolve() != Path(sys.executable).resolve():
+ raise ValueError("the supported python3 token is unavailable for this host interpreter")
+
+ def _document(self, document):
+ # Accepted document syntax, requirements, scope and oracle semantics
+ # remain with the TS owner. Only invocation/source IO is narrowed here.
+ document = deepcopy(document)
+ criteria = document.get("criteria")
+ if not isinstance(criteria, list) or not 1 <= len(criteria) <= 64:
+ raise ValueError("select bounded existing acceptance criteria and independent checker sources")
+ sources = {}
+ for criterion in criteria:
+ if not isinstance(criterion, dict):
+ raise ValueError("a check requires its existing criterion and checker source")
+ argv = criterion.get("validation_argv")
+ if (not isinstance(argv, list) or not argv or any(not isinstance(arg, str) for arg in argv)):
+ raise ValueError("select an immediate Python script or direct pinned executable")
+ if len(argv) == 2 and argv[0] in {"python", "python3", sys.executable} and argv[1].endswith(".py"):
+ source = argv[1]
+ self._python_interpreter()
+ criterion["validation_argv"] = ["python3", source]
+ elif len(argv) == 1 and "/" in argv[0]:
+ source = argv[0]
+ else:
+ raise ValueError("checker invocation supports only an immediate Python script or direct source, without options or extra arguments")
+ oracle = criterion.get("oracle")
+ if not isinstance(oracle, dict) or oracle.get("kind") != "owner_pinned_oracle":
+ raise ValueError("select an existing owner-pinned independent checker with explicit oracle outputs")
+ inputs, outputs = oracle.get("input_refs"), oracle.get("output_refs")
+ if (not isinstance(inputs, list) or not isinstance(outputs, list)
+ or len(inputs) > 20 or not 1 <= len(outputs) <= 20):
+ raise ValueError("an independent checker requires bounded explicit input and output refs")
+ if source in inputs or source in outputs:
+ raise ValueError("the independent checker source cannot be a checked input or delivery output")
+ self._current()
+ digest = _workspace_file(self.workspace, source, required=True)
+ self._current()
+ if source in sources and sources[source] != digest:
+ raise ValueError("the checker source changed during preparation")
+ pins = [{"path": source, "sha256": digest}]
+ if "validation_files" in criterion and criterion["validation_files"] != pins:
+ raise ValueError("the claimed checker source pin does not match the original workspace")
+ criterion["validation_files"] = pins
+ sources[source] = digest
+ for ref in [*inputs, *outputs]:
+ self._current()
+ _workspace_file(self.workspace, ref, required=False)
+ self._current()
+ # Reobserve the sources before publishing an immutable proposal; never
+ # refresh saved intent when a checker changed after a lost reply.
+ for source, digest in sources.items():
+ self._current()
+ if _workspace_file(self.workspace, source, required=True) != digest:
+ raise ValueError("the checker source changed during preparation")
+ self._current()
+ if any(row["validation_argv"][0] == "python3" for row in criteria):
+ self._python_interpreter()
+ return document
+
+ def __call__(self, tool, arguments):
+ from ..chat_loopx_mode import TOOL
+ if tool != TOOL["name"]:
+ return self.read(tool, arguments)
+ from ..chat_action_store import ActionConflictError
+ from ..control_plane.goals.acceptance_actions import prepare_acceptance_check
+ try:
+ self._current()
+ if (not isinstance(arguments, dict) or set(arguments) != {"action", "operation_id", "document"}
+ or arguments["action"] != "prepare_check" or not isinstance(arguments["document"], dict)
+ or not isinstance(arguments["operation_id"], str) or not arguments["operation_id"].strip()):
+ raise ValueError("prepare_check accepts only its stable operation_id and acceptance document; the host supplies actor and source identity")
+ document = self._document(arguments["document"])
+ self._current()
+ return prepare_acceptance_check(registry_path=self.registry_path,
+ runtime_root=str(self.runtime_root), goal_id=self.original["goal_id"],
+ agent_id=self.original["managed_agent_id"], document=document,
+ operation_id=arguments["operation_id"], execute=True)
+ except (ActionConflictError, ValueError) as exc:
+ return {"ok": False, "error": "collaboration_request_rejected",
+ "next_action": f"Check preparation was refused ({exc}). Read the original Goal and saved proposal; retry an operation only with unchanged intent and sources. A new intent needs a new operation id. Preparation cannot change authority or run the check."}
+
+
def apply_context_handoff(
controller, scope, session, turn, response, context, *, execution_allowed
):
@@ -212,6 +434,16 @@ def scope_valid() -> bool:
),
)
adapter.session.read_tool_handler = inspection.read
+ adapter._managed_read_turn_guard = None
+ if (session.get("managed_agent_id") and turn.get("origin") == "web"
+ and not turn.get("loopx_request") and not turn.get("loopx_execution")
+ and not session.get("loopx_tools") and not (session.get("loopx_mode") or {}).get("enabled")
+ and not adapter.session.execution_mode and not adapter.session.read_only_review
+ and getattr(adapter, "goal_driver", None) is None
+ and scope["kind"] == "owner_goal" and session.get("upstream_mode") == "chat"):
+ adapter.session.read_tool_handler = _GoalSessionCheckPreparation(
+ controller, adapter, session, turn_id, inspection.read)
+ adapter._managed_read_turn_guard = adapter.session.read_tool_handler._current
context["evidence_sources"] = inspection.sources()
context = manager_index(context, status_on_demand=status_on_demand)
from ..capabilities.native_chat.external_conversations import ChatExternalConversations
diff --git a/loopx/chat/goal_creation_execution.py b/loopx/chat/goal_creation_execution.py
new file mode 100644
index 0000000000..298332cbdb
--- /dev/null
+++ b/loopx/chat/goal_creation_execution.py
@@ -0,0 +1,99 @@
+"""IO in the existing confirmed Goal-creation saga, not a new execution owner."""
+from __future__ import annotations
+
+from typing import Any
+
+from ..agent_registry import load_goal_from_registry
+from ..boundary_authority import build_checkpointed_boundary_authority_entry
+from ..chat_action_store import ActionConflictError
+from ..configure_goal import configure_goal
+from ..control_plane.runtime.time import now_utc
+
+
+def retain_creation_write_approval(service: Any, proposal_id: str, review: dict[str, Any] | None) -> None:
+ """Publish an explicit reviewed grant once and recover its exact readback.
+
+ The typed creation review chooses scope. This IO adapter uses the existing
+ configuration/authority owners and never refreshes a lost or revoked grant.
+ """
+ if not review or not review["writeScope"]:
+ return
+ proposal = service.store.load(proposal_id)
+ goal_id = proposal["normalized_parameters"]["goal_id"]
+ goal = load_goal_from_registry(service.registry_path, goal_id)
+ if goal is None or goal.get("creation_operation_id") != proposal_id:
+ raise ActionConflictError("The write approval belongs to the original Goal creation")
+ steps = (proposal.get("checkpoint") or {}).get("steps", {})
+ prepared = steps.get("boundary_authority_prepared")
+ if prepared is None:
+ entry = build_checkpointed_boundary_authority_entry(
+ write_scopes=review["writeScope"], source="goal_creation_review",
+ decision_id=proposal_id, recorded_at=now_utc().isoformat(),
+ )
+ prepared = {"goal_id": goal_id, "goal_instance_id": goal.get("goal_instance_id"), "authority": entry}
+ service.store.save_checkpoint(proposal_id, step="boundary_authority_prepared", receipt=prepared)
+ entry = prepared.get("authority")
+ if (prepared.get("goal_id") != goal_id or prepared.get("goal_instance_id") != goal.get("goal_instance_id")
+ or not isinstance(entry, dict) or entry.get("write_scope") != review["writeScope"]
+ or entry.get("decision_id") != proposal_id or entry.get("source") != "goal_creation_review"
+ or entry.get("status") != "active" or entry.get("decision") != "approve"):
+ raise ActionConflictError("The original reviewed write approval changed; retain its recorded operation")
+
+ def observe() -> tuple[dict[str, Any], list[dict[str, Any]]]:
+ current = load_goal_from_registry(service.registry_path, goal_id)
+ if (current is None or current.get("creation_operation_id") != proposal_id
+ or current.get("goal_instance_id") != prepared["goal_instance_id"]):
+ raise ActionConflictError("The original Goal lifetime changed before approval readback")
+ coordination = current.get("coordination") or {}
+ entries = coordination.get("checkpointed_boundary_authority") or []
+ if not isinstance(entries, list):
+ raise ActionConflictError("The original boundary authority is unreadable")
+ return coordination, [row for row in entries if isinstance(row, dict) and row.get("decision_id") == proposal_id]
+
+ coordination, matches = observe()
+ if not matches:
+ if steps.get("boundary_authority_write_started") is not None or steps.get("boundary_authority_recorded") is not None:
+ raise ActionConflictError("The original approval publication is unconfirmed or revoked; read back that operation before proceeding")
+ service.store.save_checkpoint(proposal_id, step="boundary_authority_write_started",
+ receipt={"goal_id": goal_id, "decision_id": proposal_id})
+ configure_goal(registry_path=service.registry_path, goal_id=goal_id,
+ write_scope=review["writeScope"], boundary_authority_scopes=entry["write_scope"],
+ boundary_authority_source=entry["source"], boundary_authority_decision_id=entry["decision_id"],
+ boundary_authority_recorded_at=entry["recorded_at"], execute=True)
+ coordination, matches = observe()
+ if (matches != [entry] or any(scope not in (coordination.get("write_scope") or []) for scope in review["writeScope"])):
+ raise ActionConflictError("The original write approval or declared scope changed; do not refresh it from a creation replay")
+ service.store.save_checkpoint(proposal_id, step="boundary_authority_recorded", receipt={
+ "outcome": "boundary_authority_recorded", **prepared,
+ })
+
+
+def retain_original_task_request(service: Any, proposal_id: str, session: dict[str, Any]) -> dict[str, Any] | None:
+ """Persist the same reviewed request before queueing, without executing it."""
+ from ..capabilities.native_chat.steward_managed_mode import StewardManagedMode
+ from ..control_plane.effect_runtime import effect_runtime_result
+ from ..control_plane.goals.acceptance import inspect_goal_acceptance
+ proposal = service.store.load(proposal_id)
+ if proposal["normalized_parameters"].get("start_execution") is not True:
+ return None
+ controller = service.runtime_controller
+ basis = inspect_goal_acceptance(registry_path=service.registry_path,
+ runtime_root=str(controller.coordination_runtime_root), goal_id=session["goal_id"],
+ agent_id=session["managed_agent_id"])
+ selection = effect_runtime_result("goal.task.creation_selection", {"basis": basis})
+ request = effect_runtime_result("goal.creation.execution_request.prepare", {
+ "proposal": proposal, "session": session,
+ "goal": StewardManagedMode(controller, None)._goal(session["goal_id"]),
+ "current_original_task": selection,
+ })
+ prior = ((proposal.get("checkpoint") or {}).get("steps") or {}).get("original_task_requested")
+ receipt = {"outcome": "original_task_requested", **request, "source": {
+ "request_digest": proposal["request_digest"],
+ "expected_state_fingerprint": proposal["expected_state_fingerprint"],
+ "upstream_thread_id": session["upstream_thread_id"],
+ }}
+ if prior is not None and prior != receipt:
+ raise ActionConflictError("The retained original Task execution request changed")
+ if prior is None:
+ service.store.save_checkpoint(proposal_id, step="original_task_requested", receipt=receipt)
+ return request
diff --git a/loopx/chat/session_open.py b/loopx/chat/session_open.py
index c61db67895..167eff8947 100644
--- a/loopx/chat/session_open.py
+++ b/loopx/chat/session_open.py
@@ -20,15 +20,140 @@
from __future__ import annotations
from pathlib import Path
-from typing import Any
+from typing import TYPE_CHECKING, Any, Mapping
from ..chat_manager import open_manager_session
+if TYPE_CHECKING:
+ from ..chat_action_store import ChatActionStore
+ from ..chat_runtime import ChatRuntimeController
+ from ..chat_store import ChatSessionStore
+
# The executor a Goal-scoped session runs on when the caller makes no explicit
# pick. The steward channel does not use this: it resolves its own default.
DEFAULT_GOAL_AGENT_ID = "codex"
+def require_regular_session_paths(chat_store: ChatSessionStore, session_id: str) -> None:
+ """A redirected or incomplete reservation cannot identify an original."""
+ directory, metadata = chat_store._session_dir(session_id), chat_store._session_path(session_id)
+ if (directory.is_symlink() or metadata.is_symlink()
+ or directory.exists() and not directory.is_dir()
+ or metadata.exists() and not metadata.is_file()):
+ raise ValueError("The original Session reservation is unavailable; restore its regular metadata")
+
+
+def read_goal_creation_session(
+ action_store: ChatActionStore,
+ chat_store: ChatSessionStore | None,
+ *,
+ proposal_id: str,
+ session_id: str,
+ goal_id: str,
+ agent_id: str,
+ workspace: Path,
+ goal_instance_id: str | None,
+ expected_managed_agent: str | None = None,
+) -> dict[str, Any]:
+ """Read the original metadata against the creation's fresh checkpoint."""
+ from ..control_plane.collaboration import conversation_scope
+
+ if chat_store is not None:
+ require_regular_session_paths(chat_store, session_id)
+ session = chat_store.load_session(session_id) if chat_store is not None else None
+ scope = conversation_scope(session) if session is not None else {}
+ if (session is None or session.get("session_id") != session_id or session.get("goal_id") != goal_id
+ or session.get("agent_id") != agent_id
+ or scope.get("goal_ids") != [goal_id] or scope.get("kind") != "owner_goal"):
+ raise ValueError("The original first Session is unavailable; restore it before retrying")
+ if expected_managed_agent is not None and session.get("managed_agent_id") != expected_managed_agent:
+ raise ValueError("The original registered Agent Session identity changed")
+ recorded_proposal = action_store.load(proposal_id)
+ if recorded_proposal is None:
+ raise ValueError("The original creation checkpoint is unavailable")
+ recorded = ((recorded_proposal.get("checkpoint") or {}).get("steps") or {}).get("first_session_opened") or {}
+ for field in ("channel_id", "managed_agent_id", "goal_instance_id"):
+ if field in recorded and recorded[field] != session.get(field):
+ raise ValueError("The original first Session route changed; restore its recorded identity")
+ if session.get("managed_agent_id") is not None and (
+ session["managed_agent_id"] != agent_id
+ or session.get("goal_instance_id") != goal_instance_id
+ or session.get("managed_workspace_path") != str(workspace)
+ ):
+ raise ValueError("The original registered Agent Session identity changed")
+ return session
+
+
+def recover_goal_creation_session(
+ chat_store: ChatSessionStore,
+ *,
+ client_turn_id: str,
+ goal_id: str,
+ agent_id: str,
+) -> dict[str, Any] | None:
+ """Recover a unique published original, preferring its original client Turn."""
+ from ..control_plane.collaboration import conversation_scope
+
+ candidates = []
+ for directory in chat_store.sessions_root.iterdir():
+ if not directory.is_dir() and not directory.is_symlink():
+ continue
+ require_regular_session_paths(chat_store, directory.name)
+ if not chat_store._session_path(directory.name).is_file():
+ raise ValueError("The original Session reservation is unconfirmed; restore its metadata")
+ row = chat_store.load_session(directory.name)
+ if row is None or row.get("session_id") != directory.name:
+ raise ValueError("Original Session discovery is unreadable; restore it before retrying")
+ if (row.get("goal_id") == goal_id and row.get("agent_id") == agent_id
+ and conversation_scope(row).get("kind") == "owner_goal"):
+ candidates.append(row)
+ matches = [row for row in candidates if chat_store.turn_for_client(row["session_id"], client_turn_id) is not None]
+ if len(matches) > 1 or not matches and len(candidates) > 1:
+ raise ValueError("The original first Session is ambiguous; restore its recorded operation")
+ if matches:
+ return matches[0]
+ if candidates:
+ raise ValueError("The original first Session opening is unconfirmed; restore its exact checkpoint")
+ return None
+
+
+def read_exact_original_session(
+ controller: ChatRuntimeController,
+ *,
+ session_id: str,
+ selected_scope: Mapping[str, object],
+ agent_id: str,
+ work_dir: Path,
+ requested_model: str | None = None,
+ requested_effort: str | None = None,
+) -> dict[str, Any] | None:
+ """Read one exact opening under the caller's existing Session opening lock."""
+ from ..capabilities.native_chat.steward_managed_mode import require_managed_agent_session
+ from ..chat_store import CHAT_SESSION_MODE_MANAGED, RESUMABLE_SESSION_STATES
+ from ..control_plane.collaboration import conversation_scope
+
+ require_regular_session_paths(controller.store, session_id)
+ original = controller.store.load_session(session_id)
+ if original is not None:
+ if (original.get("session_id") != session_id
+ or original.get("goal_id") != selected_scope.get("goal_id") or original.get("agent_id") != agent_id
+ or original.get("executor_endpoint_id") != agent_id
+ or original.get("channel_id") != selected_scope.get("channel_id")
+ or original.get("managed_agent_id") != selected_scope.get("managed_agent_id")
+ or original.get("goal_instance_id") != selected_scope.get("goal_instance_id")
+ or original.get("session_mode") != CHAT_SESSION_MODE_MANAGED
+ or conversation_scope(original)["kind"] != "owner_goal"
+ or original.get("status") not in RESUMABLE_SESSION_STATES
+ or requested_model and original.get("agent_model_id") != requested_model
+ or requested_effort and original.get("agent_reasoning_effort") != requested_effort):
+ raise ValueError("the original Goal Session opening identity changed")
+ require_managed_agent_session(controller, original, work_dir)
+ return original
+ if controller.store._session_dir(session_id).exists():
+ raise ValueError("the original Goal Session is unreadable; restore it before retrying")
+ return None
+
+
def open_chat_session(
*,
controller: Any,
@@ -97,3 +222,19 @@ def open_chat_session(
agent_model_id=agent_model_id or None,
**({"agent_reasoning_effort": agent_reasoning_effort} if agent_reasoning_effort is not None else {}),
)
+
+
+def goal_creation_display_message(store: ChatSessionStore, *, session_id: str, client_turn_id: str, objective: str) -> str:
+ """Show confirmed intent on creation; preserve the original display on retry.
+
+ The execution request remains unchanged. An old recorded message is never
+ rewritten using the Goal's later objective or a new presentation default.
+ """
+ turn = store.turn_for_client(session_id, client_turn_id)
+ if turn is None:
+ return objective
+ messages = [row for row in store.messages(session_id, strict=True)
+ if row.get("turn_id") == turn["turn_id"] and row.get("role") == "user"]
+ if len(messages) != 1 or not isinstance(messages[0].get("text"), str):
+ raise ValueError("The original first Turn display is unavailable; restore its recorded message")
+ return messages[0]["text"]
diff --git a/loopx/chat_action_normalization.py b/loopx/chat_action_normalization.py
index bb67f17b15..ebdfc9f9a8 100644
--- a/loopx/chat_action_normalization.py
+++ b/loopx/chat_action_normalization.py
@@ -546,6 +546,7 @@ def _normalize(
"stop_condition",
"initial_todos",
"native_token_budget",
+ "start_execution",
},
)
goal_id = _opaque(values.get("goal_id"), field="goal_id")
@@ -569,11 +570,13 @@ def _normalize(
"objective",
"completion_criteria",
"execution_boundary",
- "permission",
"stop_condition",
):
if values.get(field):
result[field] = _text(values[field], field=field, limit=1000)
+ if "permission" in values or "start_execution" in values:
+ from .control_plane.effect_runtime import effect_runtime_result
+ result.update(effect_runtime_result("goal.creation.execution.normalize", values))
for field in ("agent_id", "workspace_ref"):
if values.get(field):
result[field] = _opaque(values[field], field=field)
diff --git a/loopx/chat_actions.py b/loopx/chat_actions.py
index ecd46562ea..f743e72841 100644
--- a/loopx/chat_actions.py
+++ b/loopx/chat_actions.py
@@ -242,6 +242,8 @@ def __init__(
self.registry_path = Path(registry_path).expanduser().resolve()
self.chat_store = chat_store
self.runtime_controller = runtime_controller
+ if runtime_controller is not None:
+ runtime_controller.original_task_action_store = store
# The runtime root the serving Chat selected (its --runtime-root). A
# creation on a registry that does not exist yet has no recorded root
# to follow, so the first Goal must persist where the service already
@@ -668,7 +670,8 @@ def require_first_turn_readback(turn: dict[str, Any]) -> None:
),
None,
)
- progress = effect_runtime_result("presentation.action_review_plan.compile", {"proposal": proposal}).get("creationProgress") or {}
+ creation_plan = effect_runtime_result("presentation.action_review_plan.compile", {"proposal": proposal})
+ progress = creation_plan.get("creationProgress") or {}
if (proposal.get("checkpoint") or {}).get("steps") and not progress:
raise ValueError("Goal creation checkpoints do not match the original request; inspect the recorded operation")
project, source_goal = self._project_for_goal_create(proposal, progress=progress)
@@ -787,6 +790,9 @@ def require_first_turn_readback(turn: dict[str, Any]) -> None:
},
)
todo_ids: list[str] = list(progress.get("todoIds") or [])
+ if creation_plan.get("creationExecution"):
+ from .chat.goal_creation_execution import retain_creation_write_approval
+ retain_creation_write_approval(self, proposal_id, creation_plan["creationExecution"])
# Canonical File/SQLite own replay after a committed write loses its
# response. Historical target-only creation retains its old adapter.
pending_todos = (parameters.get("initial_todos") or []) if "todoIds" not in progress else []
@@ -803,6 +809,8 @@ def require_first_turn_readback(turn: dict[str, Any]) -> None:
agent_id=agent_id or None,
dry_run=False,
operation_id=f"goal-create:{proposal_id}:todo:{index}" if authority is not None else None,
+ **({"required_write_scopes": creation_plan["creationExecution"]["writeScope"]}
+ if creation_plan.get("creationExecution", {}).get("writeScope") else {}),
)
if todo.get("todo_id"):
todo_ids.append(str(todo["todo_id"]))
@@ -842,6 +850,14 @@ def require_first_turn_readback(turn: dict[str, Any]) -> None:
raise ValueError(
"Goal projection did not retain the selected Agent binding"
)
+ from functools import partial
+ from .chat.session_open import read_goal_creation_session, recover_goal_creation_session, require_regular_session_paths, goal_creation_display_message
+
+ read_original_session = partial(read_goal_creation_session,
+ self.store, self.chat_store or getattr(self.runtime_controller, "store", None),
+ proposal_id=proposal_id, goal_id=goal_id, agent_id=agent_id,
+ workspace=project, goal_instance_id=projected.get("goal_instance_id"))
+
turn_result: dict[str, Any] | None = None
session_id = ""
first_turn_message = (f"/goal start --tokens {parameters['native_token_budget']} {objective}"
@@ -850,22 +866,44 @@ def require_first_turn_readback(turn: dict[str, Any]) -> None:
f"首个 Todo:{';'.join(str(item) for item in (parameters.get('initial_todos') or [])[:3]) or '按目标边界建立首个可验证进展'}。"
"然后直接推进并报告可验证结果;遇到权限边界时停止并提出明确 Gate。"
))
+ opening_session_id = _digest({"goal_create": proposal_id, "goal_id": goal_id})[:32]
+ expected_managed_agent = (creation_plan.get("creationSession") or {}).get("managedAgentId")
+ turn_store = self.chat_store or getattr(self.runtime_controller, "store", None)
+ if not progress.get("firstTurn") and not progress.get("sessionId") and turn_store is not None:
+ require_regular_session_paths(turn_store, opening_session_id)
+ original = turn_store.load_session(opening_session_id)
+ if original is not None:
+ read_original_session(session_id=opening_session_id, expected_managed_agent=expected_managed_agent)
+ elif turn_store._session_dir(opening_session_id).exists():
+ raise ValueError("The original first Session is unreadable; restore it before retrying")
+ elif existing_goal is not None:
+ # Historical opening responses carried no operation-bound id.
+ # Recover only an unambiguous original; never choose latest.
+ original = recover_goal_creation_session(turn_store,
+ client_turn_id=f"goal-start-{proposal_id}", goal_id=goal_id, agent_id=agent_id)
+ if original is not None:
+ read_original_session(session_id=original["session_id"])
+ progress = {**progress, "sessionId": original["session_id"]}
if progress.get("firstTurn"):
session_id = progress["firstTurn"]["sessionId"]
turn_id = progress["firstTurn"]["turnId"]
turn_store = self.chat_store or getattr(self.runtime_controller, "store", None)
- session = turn_store.load_session(session_id) if turn_store is not None else None
- turn = turn_store.load_turn(session_id, turn_id) if session is not None else None
- if session is None or session.get("goal_id") != goal_id or session.get("agent_id") != agent_id or turn is None:
+ session = read_original_session(session_id=session_id)
+ turn = turn_store.load_turn(session_id, turn_id) if turn_store is not None else None
+ if turn is None:
raise ValueError("The recorded first Turn is unavailable; restore its original Session instead of starting another")
+ if (turn.get("message") != first_turn_message
+ or turn.get("client_turn_id") != f"goal-start-{proposal_id}"):
+ raise ValueError("The original first Turn request changed; retain its recorded operation")
require_first_turn_readback(turn)
turn_result = {"turn_id": turn_id, "status": str(turn["status"]), "created": False}
elif progress.get("sessionId"):
turn_store = self.chat_store or getattr(self.runtime_controller, "store", None)
if turn_store is None:
raise ValueError("The original first Session is unavailable; restore it before retrying")
+ original_session = read_original_session(session_id=progress["sessionId"])
session, turn = self._read_task_start(
- turn_store, goal_id=goal_id, agent_id=agent_id, channel_id=f"goal.{goal_id}",
+ turn_store, goal_id=goal_id, agent_id=agent_id, channel_id=original_session["channel_id"],
client_turn_id=f"goal-start-{proposal_id}", checkpoint={"session_id": progress["sessionId"]},
)
if turn is not None:
@@ -924,9 +962,7 @@ def require_first_turn_readback(turn: dict[str, Any]) -> None:
}
if agent_id and self.runtime_controller is not None and first_turn_gate is None and turn_result is None:
if progress.get("sessionId"):
- session = self.runtime_controller.store.load_session(progress["sessionId"])
- if session is None or session.get("goal_id") != goal_id or session.get("agent_id") != agent_id:
- raise ValueError("The original first Session is unavailable; restore it before retrying")
+ session = read_original_session(session_id=progress["sessionId"])
else:
session, _resumed = self.runtime_controller.open_session(
goal_id=goal_id,
@@ -938,19 +974,34 @@ def require_first_turn_readback(turn: dict[str, Any]) -> None:
agent_id=agent_id,
work_dir=project,
objective=objective,
- mode="resume_latest",
+ mode="new",
+ session_id=opening_session_id,
channel_id=f"goal.{goal_id}",
agent_goal_id=goal_id,
+ **({"managed_agent_id": creation_plan["creationSession"]["managedAgentId"]}
+ if creation_plan.get("creationSession") else {}),
)
session_id = _opaque(session.get("session_id"), field="session_id")
+ if not progress.get("sessionId") and session_id != opening_session_id:
+ raise ValueError("The original first Session opening returned another operation")
+ session = read_original_session(session_id=session_id,
+ expected_managed_agent=expected_managed_agent if not progress.get("sessionId") else None)
self.store.save_checkpoint(proposal_id, step="first_session_opened",
- receipt={"outcome": "first_session_opened", "session_id": session_id})
+ receipt={"outcome": "first_session_opened", "session_id": session_id,
+ "channel_id": session["channel_id"],
+ **({"managed_agent_id": session["managed_agent_id"],
+ "goal_instance_id": session.get("goal_instance_id")}
+ if session.get("managed_agent_id") is not None else {}),
+ **({"upstream_thread_id": session.get("upstream_thread_id")}
+ if creation_plan.get("creationExecution") else {})})
first_turn, created = self.runtime_controller.submit_turn(
session_id=session_id,
client_turn_id=f"goal-start-{proposal_id}",
message=first_turn_message,
work_dir=project,
objective=objective,
+ **({"display_message": goal_creation_display_message(turn_store, session_id=session_id,
+ client_turn_id=f"goal-start-{proposal_id}", objective=objective)} if expected_managed_agent else {}),
)
require_first_turn_readback(first_turn)
turn_result = {
@@ -973,6 +1024,18 @@ def require_first_turn_readback(turn: dict[str, Any]) -> None:
step="first_turn_gated",
receipt={"outcome": "first_turn_gated", "gate": first_turn_gate},
)
+ if creation_plan.get("creationExecution"):
+ # An applied receipt is immutable. Keep this confirmed start
+ # retryable until its original Session/request is retained;
+ # quota denial cannot silently discard the requested work.
+ raise ValueError("The original Goal and Task are retained, but their first Turn admission is held. "
+ + first_turn_gate["summary"] + " Retry this same creation when its allowance is ready.")
+ if session_id:
+ session = read_original_session(session_id=session_id)
+ original_task_request = None
+ if session_id and parameters.get("start_execution") is True:
+ from .chat.goal_creation_execution import retain_original_task_request
+ original_task_request = retain_original_task_request(self, proposal_id, session)
child_gate: dict[str, Any] | None = first_turn_gate
if heartbeat.get("enabled") is True:
heartbeat_parameters = {
@@ -1016,18 +1079,24 @@ def require_first_turn_readback(turn: dict[str, Any]) -> None:
**({"agent_id": agent_id} if agent_id else {}),
**({"todo_ids": todo_ids} if todo_ids else {}),
**({"session_id": session_id} if session_id else {}),
+ **({"managed_agent_id": session["managed_agent_id"], "channel_id": session["channel_id"],
+ "goal_instance_id": session.get("goal_instance_id")}
+ if session_id and session.get("managed_agent_id") is not None else {}),
**({"turn_id": turn_result["turn_id"]} if turn_result else {}),
},
**({"child_gate": child_gate} if child_gate else {}),
"step_receipts": (
(self.store.load(proposal_id) or {}).get("checkpoint") or {}
).get("steps", {}),
+ **({"original_task_request": original_task_request} if original_task_request is not None else {}),
}
stored = self.store.apply(
proposal_id,
current_state_fingerprint=str(proposal["expected_state_fingerprint"]),
receipt=receipt,
)
+ if original_task_request is not None:
+ self.runtime_controller.resume_session_queue(session_id=session_id, work_dir=project, objective=objective)
return {"proposal": stored, "turn": turn_result, "gate": child_gate}
def _apply_agent_bind(
@@ -1544,6 +1613,18 @@ def regenerate(self, proposal_id: str) -> dict[str, Any]:
def apply(self, proposal_id: str, *, steward_context: dict[str, Any] | None = None,
steward_confirmed_at: str | None = None) -> dict[str, Any]:
+ result = self._apply_owner_action(proposal_id, steward_context=steward_context,
+ steward_confirmed_at=steward_confirmed_at)
+ # A committed action changes current facts. Existing queue readers may
+ # reobserve their retained requests; this content-free hint performs no
+ # transport, Task admission, permission change or new submission.
+ wakeup = getattr(self.runtime_controller, "turn_release_wakeup", None)
+ if (result.get("proposal") or {}).get("status") == "applied" and wakeup is not None:
+ wakeup.notify()
+ return result
+
+ def _apply_owner_action(self, proposal_id: str, *, steward_context: dict[str, object] | None = None,
+ steward_confirmed_at: str | None = None) -> dict[str, object]:
proposal = self.store.load(proposal_id)
if proposal is None:
raise KeyError("typed Chat action proposal was not found")
@@ -1570,7 +1651,7 @@ def apply(self, proposal_id: str, *, steward_context: dict[str, Any] | None = No
},
)
parameters = proposal.get("normalized_parameters")
- if (proposal.get("action_kind") == "todo.create" and isinstance(parameters, dict)
+ if (proposal.get("action_kind") in {"todo.create", "goal.create"} and isinstance(parameters, dict)
and parameters.get("start_execution") is True):
key = _opaque(proposal_id, field="proposal_id")
with try_exclusive_file_lock(
diff --git a/loopx/chat_agent.py b/loopx/chat_agent.py
index 951c6832b8..033b57975a 100644
--- a/loopx/chat_agent.py
+++ b/loopx/chat_agent.py
@@ -720,6 +720,8 @@ class CodexChatAgentSession:
_launch_guard: Callable[[], AbstractContextManager[None]] | None = field(
default=None, repr=False
)
+ _native_task_policy_used: bool = field(default=False, repr=False)
+ _managed_goal_read_default: bool = field(default=False, repr=False)
@classmethod
def start(
@@ -1734,28 +1736,15 @@ def send(
on_native_item: Callable[[dict[str, Any]], None] | None = None,
turn_start_guard: Callable[[], AbstractContextManager[None]] | None = None,
check_running: Callable[[], None] | None = None,
+ _native_task: bool = False,
+ sandbox_policy: dict[str, object] | None = None,
) -> dict[str, Any]:
- text = str(user_message or "")
- if not text.strip():
- raise ValueError("user message is required")
- if self.read_only_review and not self.metadata_review and not self.answer_review:
- text = (
- "Review the existing results of the interrupted original request below. "
- "Its earlier execution may have already changed files or external systems. "
- "Use the original native history and current local artifacts to verify what actually happened. "
- "This Turn is read-only: do not re-execute the business request, write, send, delegate or start background work. "
- "Return a clear answer to the original user, distinguishing verified results from remaining gaps. "
- "If verification is incomplete, report the concrete limitation; do not claim completion. "
- "Original request: " + text
- )
- if project_collaboration:
- if (self.execution_mode or self.read_only_review or output_schema is not None
- or not self.project_context or self.project_context.get("audience") != "bound_owner"):
- raise ValueError("original project collaboration requires its existing bound project session")
- from .control_plane.effect_runtime import effect_runtime_result
- output_schema = effect_runtime_result("collaboration.project.response_schema", {})
- if output_schema is not None and not self.execution_mode and not project_collaboration:
- raise ValueError("structured Turn output requires an execution session")
+ if not _native_task and (self._native_task_policy_used or self._managed_goal_read_default) and sandbox_policy is None:
+ sandbox_policy = {"type": "readOnly", "networkAccess": False}
+ from .capabilities.native_chat.codex_adapter import prepare_native_turn_text
+ text, output_schema = prepare_native_turn_text(self, user_message, output_schema=output_schema,
+ project_collaboration=project_collaboration, native_task=_native_task,
+ sandbox_policy=sandbox_policy, turn_start_guard=turn_start_guard)
with self._request_id_lock:
request_id = self.next_request_id
self.next_request_id += 1
@@ -1763,7 +1752,7 @@ def send(
text = _turn_prompt(
text,
context_summary=self.context_summary,
- execution_mode=self.execution_mode,
+ execution_mode=self.execution_mode or _native_task,
runtime_profile=self.runtime_profile,
project_work=self.project_context is not None and self.sandbox == "workspace-write",
public_source_read_context=self._public_source_read_context,
@@ -1777,6 +1766,10 @@ def send(
started_at = time.monotonic()
start_timeout = min(self.idle_timeout_sec, self.hard_timeout_sec
if self.hard_timeout_sec is not None else self.idle_timeout_sec)
+ if _native_task:
+ # Publication may be uncertain. Every subsequent ordinary send
+ # must narrow the sticky native policy, even after an exception.
+ self._native_task_policy_used = True
turn_result = self._request(
"turn/start",
{
@@ -1788,9 +1781,13 @@ def send(
# Chat renders native summaries, but a resumed thread may have
# disabled them. Request the public summary on each Chat Turn;
# execution hosts retain their own verbosity configuration.
- **({"summary": "concise"} if not self.execution_mode else {}),
+ **({"summary": "concise"} if not (self.execution_mode or _native_task) else {}),
"approvalPolicy": "never",
**({"permissions": self.permissions_profile} if self.permissions_profile else {}),
+ **({"sandboxPolicy": sandbox_policy} if sandbox_policy is not None else {}),
+ **({"runtimeWorkspaceRoots": [str(self.work_dir.resolve())]}
+ if _native_task and sandbox_policy is not None
+ and sandbox_policy.get("type") == "workspaceWrite" else {}),
**(
{"outputSchema": output_schema} if output_schema is not None else {}
),
diff --git a/loopx/chat_ingress.py b/loopx/chat_ingress.py
index 286c7aa01e..0d180d9703 100644
--- a/loopx/chat_ingress.py
+++ b/loopx/chat_ingress.py
@@ -247,6 +247,11 @@ def read(path):
and str(payload.get("turn_id") or "") != active_turn_id
):
rows.append(payload)
+ # The ordinary read has no selectable Turn when storage has no queued
+ # candidates. Explicit capacity and strict reads still use the owner.
+ if not rows and not strict and not include_reserved and capacity_at is None:
+ return []
+
from .control_plane.chat_turn_acceptance import queued_chat_turn_ids
selected = set(queued_chat_turn_ids(rows, active_turn_id, include_reserved=include_reserved, capacity_at=capacity_at))
return sorted(
diff --git a/loopx/chat_runtime.py b/loopx/chat_runtime.py
index c018072005..beb4d4a687 100644
--- a/loopx/chat_runtime.py
+++ b/loopx/chat_runtime.py
@@ -24,6 +24,7 @@
steward_machine_defaults,
)
from .chat.coordination import PROJECT_COORDINATION_GUIDANCE, PROJECT_CONTEXT_VERSION, apply_context_handoff
+from .chat.session_open import read_exact_original_session
from .capabilities.native_chat import codex_context, project_context as project_context_policy
from .capabilities.native_chat.codex_adapter import (
CodexAppServerAdapter, NativeHistoryRestoration, NativeWorkingContinuation, start_attachment_turn,
@@ -59,6 +60,7 @@
from .file_lock import (
LOCK_HOLDER_DEAD, LOCK_HOLDER_LIVE, LOCK_HOLDER_RELEASED,
_lock_id, lock_holder_host_label, lock_holder_liveness, process_is_alive, try_exclusive_file_lock,
+ exclusive_cross_runtime_file_lock,
)
from .kiro_cli_goal_mode import (
KIRO_CLI_BIN,
@@ -299,6 +301,7 @@ def __init__(
self.session_queue_workers: set[str] = set()
self.session_queue_threads: dict[str, threading.Thread] = {}
self.session_queue_wakeups: set[str] = set()
+ self.original_task_action_store = None
self.closed = threading.Event()
from .chat_loopx_mode import ChatLoopXMode
self.loopx_mode = ChatLoopXMode(self)
@@ -372,6 +375,8 @@ def _start_adapter(
manager_runtime: Mapping[str, Any] | None = None,
project_coordination: bool = False,
loopx_tools: bool = False,
+ check_preparation: bool = False,
+ managed_read_default: bool = False,
executor_model: Mapping[str, str | None] | None = None,
project_context: dict[str, str] | None = None,
agent_config: dict[str, str] | None = None,
@@ -410,11 +415,9 @@ def _start_adapter(
manager_runtime=manager_runtime, executor_model=executor_model,
agent_config=agent_config, execution_mode=execution_mode,
)
- native_context = {
- "objective": self._session_objective(goal_id=goal_id, objective=objective,
- project_coordination=project_coordination and not execution_mode),
- "runtime_profile": str(manager_profile["runtime_profile"]) if manager_profile else "restricted",
- }
+ from .capabilities.native_chat.codex_adapter import native_runtime_context, start_runtime_adapter
+ native_context = native_runtime_context(self, goal_id=goal_id, objective=objective,
+ manager_profile=manager_profile, project_coordination=project_coordination, execution_mode=execution_mode)
if agent_id == "antigravity":
from .capabilities.native_chat.antigravity_adapter import AntigravityAdapter
if execution_mode or project_context is not None or read_only_review:
@@ -431,11 +434,12 @@ def _start_adapter(
hard_timeout_sec=self.hard_timeout_sec,
)
if agent_id == "codex":
- from .capabilities.native_chat.codex_adapter import start_runtime_adapter
return start_runtime_adapter(self, work_dir=work_dir, goal_id=goal_id, native_context=native_context,
manager_profile=manager_profile, model_config=model_config, history=history,
resume_thread_id=resume_thread_id, execution_mode=execution_mode, project_context=project_context,
project_coordination=project_coordination, loopx_tools=loopx_tools,
+ check_preparation=check_preparation,
+ managed_read_default=managed_read_default,
codex_home_override=codex_home_override, read_only_review=read_only_review,
metadata_review_context=metadata_review_context, answer_review_context=answer_review_context,
history_check=history_check, working_continuation=working_continuation)
@@ -521,6 +525,7 @@ def open_session(
agent_goal_id: str | None = None,
goal_instance_id: str | None = None,
managed_agent_id: str | None = None,
+ session_id: str | None = None,
manager_executor_allocation: Mapping[str, Any] | None = None,
project_ref: str | None = None,
conversation_binding_id: str | None = None,
@@ -541,6 +546,8 @@ def open_session(
error_code="unsupported_configuration", gate=None)
if mode not in {"resume_latest", "new"}:
raise ValueError("mode must be resume_latest or new")
+ if session_id is not None and mode != "new":
+ raise ValueError("an exact Session opening requires mode=new")
selected_channel = channel_id or f"goal.{goal_id}"
project_context = None
steward_context = None
@@ -593,9 +600,24 @@ def open_session(
goal_id = MANAGER_AGENT_GOAL_ID
route_goal_id = "*" if is_manager_channel(selected_channel) else goal_id
route_key = (route_goal_id, agent_id, selected_channel)
+ if session_id is not None:
+ if not isinstance(session_id, str) or session_id != session_id.strip() or session_id in {".", ".."}:
+ raise ValueError("session_id must retain its exact non-empty opaque identity")
+ session_id = _opaque_id(session_id, field="session_id")
+ if (project_context is not None or steward_context is not None or is_manager_channel(selected_channel)
+ or goal_id is None or agent_goal_id is not None and agent_goal_id != goal_id
+ or conversation_scope(selected_scope)["kind"] != "owner_goal"):
+ raise ValueError("an exact Session opening requires its original local Goal route")
with self.lock:
route_lock = self.session_open_locks.setdefault(route_key, threading.Lock())
- with route_lock:
+ with route_lock, (exclusive_cross_runtime_file_lock(
+ self.store.root / "session-open-locks" / session_id, operation="open_original_goal_session")
+ if session_id is not None else nullcontext()):
+ if session_id is not None:
+ original = read_exact_original_session(self, session_id=session_id, selected_scope=selected_scope,
+ agent_id=agent_id, work_dir=work_dir, requested_model=agent_model_id, requested_effort=agent_reasoning_effort)
+ if original is not None:
+ return original, True
latest = None
if not is_manager_channel(selected_channel) and project_context is None:
exact_attached, strict_profile = select_current_attached_session(
@@ -618,7 +640,7 @@ def open_session(
error_code="source_session_managed_chat_unsupported",
gate=None,
)
- if mode == "resume_latest":
+ if mode == "resume_latest" and session_id is None:
latest = self.store.latest_session(
goal_id=None if is_manager_channel(selected_channel) else goal_id,
agent_id=agent_id,
@@ -674,6 +696,8 @@ def open_session(
objective=objective,
execution_mode=selected_channel.startswith("task."),
project_coordination=conversation_scope(selected_scope)["kind"] == "owner_goal",
+ check_preparation=managed_agent_id is not None,
+ managed_read_default=managed_agent_id is not None,
manager_runtime=manager_runtime,
project_context=project_context,
executor_model=alloc.manager_executor_model(manager_executor_allocation),
@@ -686,6 +710,7 @@ def open_session(
require_managed_agent_session(self, selected_scope, work_dir)
observed_settings = self._observed_model_settings(adapter, agent_reasoning_effort, agent_model_id)
persisted = self.store.create_session(
+ **({"session_id": session_id} if session_id is not None else {}),
goal_id=goal_id,
goal_instance_id=goal_instance_id,
**({"managed_agent_id": managed_agent_id, "managed_workspace_path": str(work_dir.resolve())} if managed_agent_id is not None else {}),
@@ -1020,6 +1045,7 @@ def check_route_configuration():
execution_mode=str(session.get("channel_id") or "").startswith("task."),
project_coordination=conversation_scope(session)["kind"] == "owner_goal",
loopx_tools=session.get("loopx_tools") is True,
+ managed_read_default=session.get("managed_agent_id") is not None,
executor_model=(alloc.manager_executor_model(model_allocation)
if model_allocation is not None else alloc.restored_executor_model(session)),
manager_runtime=manager_runtime,
@@ -1169,6 +1195,7 @@ def submit_turn(
loopx_execution: bool = False,
loopx_request: dict[str, object] | None = None,
_goal_commit: ManagedSessionGoalCommit | None = None,
+ display_message: str | None = None,
) -> tuple[dict[str, Any], bool]:
commit = _goal_commit or nullcontext
session = self.store.load_session(session_id)
@@ -1189,16 +1216,9 @@ def submit_turn(
if parse_native_goal_command(message) is not None:
validate_goal_chat(session, attachments)
if session.get("session_mode") == CHAT_SESSION_MODE_ATTACHED:
- if attachments:
- raise ValueError("attached host session queue does not yet accept attachments")
return enqueue_attached_agent_turn(
- store=self.store,
- registry_path=self.registry_path,
- session_id=session_id,
- client_turn_id=client_turn_id,
- message=message,
- origin="web",
- )
+ store=self.store, registry_path=self.registry_path, session_id=session_id,
+ client_turn_id=client_turn_id, message=message, origin="web", attachments=attachments)
with self._session_adapter_lock(session_id):
session = self.store.load_session(session_id)
if session is None or session.get("status") == "closed":
@@ -1236,7 +1256,7 @@ def submit_turn(
"wake": "成员结果已验收,继续推进 LoopX 模式。",
}.get(str((loopx_request or {}).get("operation")), "恢复 LoopX 模式。")
if loopx_execution
- else None
+ else display_message
),
loopx_execution=loopx_execution,
loopx_request=loopx_request,
@@ -1534,6 +1554,7 @@ def resume_session_queue(
return
if session_id in self.session_queue_workers:
self.session_queue_wakeups.add(session_id)
+ self.turn_release_wakeup.notify()
return
worker = threading.Thread(
target=self._drain_session_queue,
@@ -1548,123 +1569,13 @@ def resume_session_queue(
self.session_queue_threads[session_id] = worker
worker.start()
- def _drain_session_queue(
- self,
- *,
- session_id: str,
- work_dir: Path,
- objective: str,
- ) -> None:
- try:
- while not self.closed.is_set():
- session = self.store.load_session(session_id)
- if (
- self.closed.is_set()
- or session is None
- or session.get("status") == "closed"
- or session.get("session_mode") == CHAT_SESSION_MODE_ATTACHED
- ):
- return
- turn = None
- active_turn_id = str(session.get("active_turn_id") or "")
- if active_turn_id:
- try:
- self.recover_exited_turn(session_id, active_turn_id)
- except OSError:
- # Storage can still be unavailable. Retry only the
- # non-executing lifecycle repair, never the old input.
- self.closed.wait(0.25)
- continue
- active = self.store.load_turn(session_id, active_turn_id)
- if (active and active.get("status") == "queued" and "_acceptance" not in active
- and active.get("started_at") is None and active.get("upstream_turn_id") is None
- and active.get("delta_count") == 0):
- # A crash can leave the queue claim before native
- # dispatch. Keep that exact claim and pass through the
- # normal preparation and execution fences below.
- turn = active
- else:
- with self.lock:
- attached = self.adapters.get(session_id)
- if attached is None or not attached.healthcheck():
- try:
- self._ensure_adapter(
- session,
- work_dir=work_dir,
- objective=objective,
- )
- except Exception as exc:
- self._fail_queue_preparation(session_id, active_turn_id, exc)
- else:
- continue
- active = self.store.load_turn(session_id, active_turn_id)
- if active and active.get("status") not in TERMINAL_TURN_STATES:
- self.closed.wait(0.05)
- continue
- self.store.release_active_turn(session_id, active_turn_id)
- refreshed = self.store.load_session(session_id)
- if refreshed is None:
- return
- with self.lock:
- self.session_queue_wakeups.discard(session_id)
- if turn is None:
- turn = self.store.claim_next_queued_turn(session_id)
- if turn is None:
- with self.lock:
- if session_id in self.session_queue_wakeups:
- continue
- worker = self.session_queue_threads.get(session_id)
- if worker is threading.current_thread():
- self.session_queue_workers.discard(session_id)
- self.session_queue_threads.pop(session_id, None)
- return
- turn_id = str(turn["turn_id"])
- try:
- if turn.get("origin") == "peer" or "collaboration_request" in turn:
- from .capabilities.native_chat.conversation_bindings import OriginalConversationRequests
- OriginalConversationRequests(self).validate_turn(session_id, turn)
- # An empty queue needs no provider preparation. Claim real
- # work first, keeping the existing enqueue/retirement fence
- # above and the Turn's execution/stop checks below.
- current = self.store.load_session(session_id)
- if current is None or current.get("active_turn_id") != turn_id:
- return
- adapter = self._ensure_adapter(
- current,
- work_dir=work_dir,
- objective=objective,
- )
- if (self.store.load_session(session_id) or {}).get("active_turn_id") != turn_id:
- return
- except Exception as exc:
- # The claimed request still owns a terminal outcome when
- # workspace preparation or adapter restoration fails.
- self._fail_queue_preparation(session_id, turn_id, exc)
- continue
- done_event = threading.Event()
- with self.lock:
- self.turn_done_events[(session_id, turn_id)] = done_event
- try:
- self._run_turn(
- session_id=session_id,
- turn_id=turn_id,
- message=str(turn.get("message") or ""),
- attachments=turn.get("attachments") or [],
- adapter=adapter,
- done_event=done_event,
- )
- except OSError:
- # _run_turn has released its execution fence/done event.
- # A failed error checkpoint must not kill this queue worker;
- # the next iteration reconciles the original durable state.
- self.closed.wait(0.25)
- finally:
- with self.lock:
- worker = self.session_queue_threads.get(session_id)
- if worker is threading.current_thread():
- self.session_queue_workers.discard(session_id)
- self.session_queue_threads.pop(session_id, None)
- self.session_queue_wakeups.discard(session_id)
+ def resume_creation_requests(self) -> None:
+ from .capabilities.native_chat.creation_queue import resume_creation_requests
+ resume_creation_requests(self)
+
+ def _drain_session_queue(self, *, session_id: str, work_dir: Path, objective: str) -> None:
+ from .capabilities.native_chat.session_queue import drain_session_queue
+ drain_session_queue(self, session_id=session_id, work_dir=work_dir, objective=objective)
def _fail_queue_preparation(
self, session_id: str, turn_id: str, error: Exception,
@@ -2484,6 +2395,7 @@ def resume_session(self, *, session_id: str, work_dir: Path, objective: str) ->
def close(self) -> None:
self.closed.set()
+ self.turn_release_wakeup.notify()
with self.lock:
adapters = list(self.adapters.values())
event_buffers = list(self.turn_event_buffers.values())
diff --git a/loopx/chat_server.py b/loopx/chat_server.py
index 18151c789d..ab9a4425ea 100644
--- a/loopx/chat_server.py
+++ b/loopx/chat_server.py
@@ -953,31 +953,6 @@ def _session_snapshot(self, session_id: str) -> None:
except OSError:
self._send_error("Chat history is temporarily unavailable", status=503)
- def _managed_native_turn(self, session_id: str, *, execute: bool = False) -> None:
- from .capabilities.native_chat.steward_managed_mode import ManagedSessionTurn
- service = ManagedSessionTurn(self.server.runtime_controller)
- try:
- if execute:
- body = self._read_json()
- if set(body) - {"agent_id", "operation_id", "todo_id"}:
- raise ValueError("unsupported managed Turn arguments")
- turn, created = service.submit(session_id, **body)
- return self._send_json({"ok": True, "session_id": session_id, "turn": turn, "created": created}, status=202)
- query = parse_qs(urlparse(self.path).query, keep_blank_values=True)
- sources = query.get("calendar_task_sources", ["false"])
- if sources not in (["true"], ["false"]):
- raise ValueError("calendar_task_sources must be one true or false value")
- include_sources = sources == ["true"]
- if include_sources and (set(query) - {"calendar_task_sources", "agent_id", "operation_id"}
- or any(len(values) != 1 for values in query.values())):
- raise ValueError("select one original managed Session and Agent for Task sources")
- self._send_json(service.inspect(session_id, agent_id=query.get("agent_id", [None])[0],
- operation_id=query.get("operation_id", [None])[0], include_calendar_task_sources=include_sources))
- except KeyError:
- self._send_error("chat session was not found", status=404)
- except (TypeError, ValueError, RuntimeError) as exc:
- self._send_error(str(exc), status=409, error_code="managed_turn_unavailable")
-
def _list_sessions(self) -> None:
query = parse_qs(urlparse(self.path).query, keep_blank_values=True)
if "managed_agent_id" in query:
@@ -2032,6 +2007,9 @@ def _wake_goal_context(session: dict[str, object]) -> dict[str, object]:
server.delegation_wake_service = DelegationWakeService(
server.runtime_controller, goal_context=_wake_goal_context
).start()
+ # Resume only retained, explicitly confirmed creation requests through the
+ # existing Session queue. Its owner rechecks source, native work and gates.
+ server.runtime_controller.resume_creation_requests()
url = f"http://{host}:{port}{DEFAULT_CHAT_PATH}"
print(f"Serving LoopX Chat at {url}", flush=True)
print(f"Agent boundary: local adapters, project grant {project_workspace_grant}, approval policy never", flush=True)
diff --git a/loopx/cli.py b/loopx/cli.py
index bc31a8c02f..8df03d42e9 100644
--- a/loopx/cli.py
+++ b/loopx/cli.py
@@ -117,6 +117,7 @@
handle_support_control_command,
handle_task_lease_command,
handle_authority_shadow_command,
+ handle_postgres_command,
handle_version_command,
handle_usage_ping_command,
handle_host_mode_plan_command,
@@ -128,6 +129,7 @@
register_canary_commands,
register_coordination_shadow_command,
register_authority_archive_command,
+ register_postgres_commands,
register_capability_commands,
register_doctor_command,
register_dreaming_commands,
@@ -809,6 +811,16 @@ def _main(argv: list[str] | None = None) -> int:
if authority_shadow_result is not None:
return authority_shadow_result
+ postgres_result = handle_postgres_command(
+ args,
+ registry_path=registry_path,
+ runtime_root_arg=args.runtime_root,
+ output_format=output_format,
+ print_payload=print_payload,
+ )
+ if postgres_result is not None:
+ return postgres_result
+
shared_goal_alignment_result = handle_shared_goal_alignment_command(
args,
registry_path=registry_path,
diff --git a/loopx/cli_commands/__init__.py b/loopx/cli_commands/__init__.py
index ec347f4467..72ae204ec6 100644
--- a/loopx/cli_commands/__init__.py
+++ b/loopx/cli_commands/__init__.py
@@ -46,6 +46,7 @@ def _load_exports() -> None:
)
from .canary import handle_canary_command, register_canary_commands
from .authority_archive import handle_authority_archive_command, register_authority_archive_command
+ from .postgres import handle_postgres_command, register_postgres_commands
from .coordination_shadow import (
handle_coordination_shadow_command,
register_coordination_shadow_command,
@@ -210,6 +211,7 @@ def _load_exports() -> None:
"handle_dash_command",
"handle_project_lifecycle_command",
"handle_project_command",
+ "handle_postgres_command",
"handle_pr_review_command",
"handle_deepresearch_command",
"handle_quota_command",
@@ -243,6 +245,7 @@ def _load_exports() -> None:
"register_canary_commands",
"register_coordination_shadow_command",
"register_authority_archive_command",
+ "register_postgres_commands",
"register_capability_commands",
"register_extension_commands",
"register_doctor_command",
diff --git a/loopx/cli_commands/postgres.py b/loopx/cli_commands/postgres.py
new file mode 100644
index 0000000000..ef8e64ea12
--- /dev/null
+++ b/loopx/cli_commands/postgres.py
@@ -0,0 +1,178 @@
+"""CLI commands for PostgreSQL authority administration, schema/role setup, and genesis seed export."""
+from __future__ import annotations
+
+import argparse
+import os
+from collections.abc import Callable
+from pathlib import Path
+
+from ..control_plane.effect_runtime import effect_runtime_result
+from ..paths import resolve_runtime_root
+from ..control_plane.projects.registry_codec import load_registry
+
+
+def register_postgres_commands(
+ subparsers: argparse._SubParsersAction[argparse.ArgumentParser],
+ add_subcommand_format: Callable[[argparse.ArgumentParser], None],
+) -> None:
+ parser = subparsers.add_parser(
+ "postgres",
+ help="PostgreSQL authority schema setup, restricted role assembly, and genesis seed export.",
+ )
+ add_subcommand_format(parser)
+ actions = parser.add_subparsers(dest="postgres_action", required=True)
+
+ # 1. setup: Admin PG Schema and restricted role assembly
+ setup_parser = actions.add_parser(
+ "setup",
+ help="Explicitly assemble PostgreSQL schema, tables, RLS policies, metadata and optional restricted role.",
+ )
+ setup_parser.add_argument(
+ "--database-url",
+ default=os.environ.get("LOOPX_POSTGRES_URL", ""),
+ help="PostgreSQL connection string (or LOOPX_POSTGRES_URL).",
+ )
+ setup_parser.add_argument(
+ "--store-identity",
+ help="Optional explicit store identity (postgresql:<32 lowercase hex>). Generated if omitted.",
+ )
+ setup_parser.add_argument(
+ "--runtime-role",
+ help="Optional runtime role to provision within the schema transaction.",
+ )
+ setup_parser.add_argument(
+ "--execute",
+ action="store_true",
+ help="Execute the schema and role setup against the database.",
+ )
+
+ # 2. local-to-pg-seed / seed: Read-only local-to-pg-seed genesis snapshot sanitizer and exporter
+ for seed_cmd in ("local-to-pg-seed", "seed"):
+ seed_parser = actions.add_parser(
+ seed_cmd,
+ help="Export a local goal into a sanitized cursor=1 genesis seed (stripping expired leases, paths, .local/, Feishu IDs) and optionally seed target PG.",
+ )
+ seed_parser.add_argument("--goal-id", required=True, help="Goal ID to export.")
+ seed_parser.add_argument(
+ "--tenant-id",
+ default="default",
+ help="Target tenant ID for the PostgreSQL store (default: default).",
+ )
+ seed_parser.add_argument(
+ "--database-url",
+ default=os.environ.get("LOOPX_POSTGRES_URL", ""),
+ help="Optional target PostgreSQL connection string for direct seeding.",
+ )
+ seed_parser.add_argument(
+ "--store-identity",
+ help="Optionally pin the already installed target PostgreSQL store identity.",
+ )
+ seed_parser.add_argument(
+ "--output",
+ type=Path,
+ help="Optional file to create or reuse if it contains the identical seed JSON.",
+ )
+ seed_parser.add_argument(
+ "--execute",
+ action="store_true",
+ help="Execute the seed commit to target PostgreSQL; otherwise read-only preview/export.",
+ )
+
+
+def handle_postgres_command(
+ args: argparse.Namespace,
+ *,
+ registry_path: Path,
+ runtime_root_arg: str | None,
+ print_payload: Callable[[dict[str, object], str, Callable[[dict[str, object]], str]], None],
+ output_format: Callable[..., str],
+) -> int | None:
+ if args.command != "postgres":
+ return None
+
+ action = args.postgres_action
+ if action == "setup":
+ database_url = args.database_url or os.environ.get("LOOPX_POSTGRES_URL", "")
+ if not database_url:
+ payload = {
+ "status": "failed",
+ "reason": "--database-url or LOOPX_POSTGRES_URL environment variable is required for setup",
+ }
+ print_payload(payload, output_format(args), lambda v: f"Postgres setup: failed\n{v.get('reason')}")
+ return 1
+
+ if not args.execute:
+ payload = {
+ "status": "dry_run",
+ "schema_version": "loopx_postgresql_authority_store_v1",
+ "database_url_configured": bool(database_url),
+ "store_identity": args.store_identity or "",
+ "runtime_role": args.runtime_role or None,
+ "note": "Pass --execute to actually install schema and assemble roles in PostgreSQL",
+ }
+ print_payload(payload, output_format(args), lambda v: f"Postgres setup (dry-run):\n{v}")
+ return 0
+
+ request = {
+ "databaseUrl": database_url,
+ "storeIdentity": args.store_identity,
+ "runtimeRole": args.runtime_role,
+ }
+ try:
+ result = effect_runtime_result(
+ "coordination.postgresql.setup", request, timeout=120.0, retry_safe=False
+ )
+ except Exception as error:
+ result = {"status": "failed", "reason": str(error)}
+
+ print_payload(
+ result,
+ output_format(args),
+ lambda v: (
+ f"PostgreSQL setup: {v.get('status')}\n"
+ f"Schema version: {v.get('schema_version')}\n"
+ f"Store identity: {v.get('store_identity')}\n"
+ f"Runtime role: {v.get('runtime_role')}\n"
+ f"{v.get('reason', '')}"
+ ),
+ )
+ return 0 if result.get("status") == "installed" else 1
+
+ elif action in ("local-to-pg-seed", "seed"):
+ registry = load_registry(registry_path)
+ resolved_root = str(resolve_runtime_root(registry, runtime_root_arg, registry_path=registry_path))
+
+ database_url = args.database_url or os.environ.get("LOOPX_POSTGRES_URL", "")
+ output_path = str(args.output.expanduser().absolute()) if args.output else None
+
+ request = {
+ "runtimeRoot": resolved_root,
+ "goalId": args.goal_id,
+ "tenantId": args.tenant_id,
+ "outputPath": output_path,
+ "databaseUrl": database_url if args.execute else None,
+ "storeIdentity": args.store_identity,
+ "execute": args.execute,
+ }
+ try:
+ result = effect_runtime_result(
+ "coordination.postgresql.seed", request, timeout=180.0, retry_safe=False
+ )
+ except Exception as error:
+ result = {"status": "failed", "reason": str(error)}
+
+ print_payload(
+ result,
+ output_format(args),
+ lambda v: (
+ f"PostgreSQL genesis seed: {v.get('status')}\n"
+ f"Goal: {v.get('goal_id')} (tenant: {v.get('tenant_id')})\n"
+ f"Cursor: {v.get('cursor')} (revision: {v.get('provider_revision')})\n"
+ f"Seed digest: {v.get('seed_digest')}\n"
+ f"Output: {v.get('output_path') or 'memory'}\n"
+ f"{v.get('reason', '')}"
+ ),
+ )
+ return 0 if result.get("status") in ("exported", "seeded") else 1
+
+ return None
diff --git a/loopx/cli_commands/quota.py b/loopx/cli_commands/quota.py
index 213e79a511..0516351b53 100644
--- a/loopx/cli_commands/quota.py
+++ b/loopx/cli_commands/quota.py
@@ -612,6 +612,13 @@ def _record_non_guard_quota_event(
),
details=rollout_details,
allow_failed=args.quota_command == "should-run",
+ idempotency_fields=(
+ ["goal_id", "event_kind", "agent_id", "run_id"]
+ + (["todo_id"] if rollout_todo_id else [])
+ + (["goal_ref"] if goal_ref is not None else [])
+ if args.quota_command == "spend-slot" and spend_turn_instance_id
+ else None
+ ),
)
if args.quota_command == "spend-slot" and spend_turn_instance_id:
assert context is not None # A settlement requires a prepared authority context.
diff --git a/loopx/cli_commands/quota_action_selection.py b/loopx/cli_commands/quota_action_selection.py
index 7b59cfc3cb..52e4be56e0 100644
--- a/loopx/cli_commands/quota_action_selection.py
+++ b/loopx/cli_commands/quota_action_selection.py
@@ -5,6 +5,7 @@
from dataclasses import dataclass
from pathlib import Path
+from ..control_plane.quota.effective_action import EffectiveAction
from ..control_plane.quota.error_codes import (
HeartbeatReceiptIdentityConflictError,
QuotaActionSelectionConflictError,
@@ -166,6 +167,10 @@ def _requested_quota_action_selection_preflight(
"current selected Todo: explicitly requested Todo differs"
)
return None
+ if payload.get("effective_action") == EffectiveAction.UNSETTLED_HOST_TURN_RECOVERY.value:
+ # Recovery preempts the choice. Keep the exact recovery actions in the
+ # response; the rollout owner leaves this Turn unbound until reentry.
+ return None
interaction = payload.get("interaction_contract")
agent_channel = (
interaction.get("agent_channel") if isinstance(interaction, Mapping) else None
diff --git a/loopx/cli_commands/quota_monitor_poll.py b/loopx/cli_commands/quota_monitor_poll.py
index 030e6c353e..49b58f44b8 100644
--- a/loopx/cli_commands/quota_monitor_poll.py
+++ b/loopx/cli_commands/quota_monitor_poll.py
@@ -13,6 +13,7 @@
find_heartbeat_receipt,
heartbeat_receipt_settlement_todo_id,
)
+from ..control_plane.scheduler.arbitration import monitor_quiet_stall_observation_required
from ..control_plane.scheduler.execution_context import (
SchedulerExecutionContextResolution,
)
@@ -201,10 +202,9 @@ def record_automatic_heartbeat_stall(
turn_instance_id=turn_id,
goal_ref=goal_ref,
)
- if (
- payload.get("effective_action")
- != EffectiveAction.MONITOR_QUIET_SKIP.value
- and existing_stall is None
+ if existing_stall is None and (
+ payload.get("effective_action") != EffectiveAction.MONITOR_QUIET_SKIP.value
+ or not monitor_quiet_stall_observation_required(payload)
):
return payload, status_payload, cache_metadata, "not_applicable"
from ..capabilities.explore.composition_frontier import (
diff --git a/loopx/cli_commands/quota_scheduler_followup.py b/loopx/cli_commands/quota_scheduler_followup.py
index a8ebbe64a1..8bbe506bba 100644
--- a/loopx/cli_commands/quota_scheduler_followup.py
+++ b/loopx/cli_commands/quota_scheduler_followup.py
@@ -228,6 +228,7 @@ def build_scheduler_followup_payload(
failed_rrule=args.failed_rrule,
observed_host_rrule=observed_rrule,
failure_kind=args.failure_kind,
+ turn_instance_id=turn_instance_id,
)
return record_quota_scheduler_ack(
status_payload,
@@ -248,4 +249,5 @@ def build_scheduler_followup_payload(
scheduler_execution_context=scheduler_context,
operator_inbox_urgency_projector=operator_inbox_urgency_projector,
before_decision=before_decision,
+ turn_instance_id=turn_instance_id,
)
diff --git a/loopx/cli_commands/turn.py b/loopx/cli_commands/turn.py
index 7bbf6e0205..8f5f220dfa 100644
--- a/loopx/cli_commands/turn.py
+++ b/loopx/cli_commands/turn.py
@@ -20,7 +20,6 @@
build_loopx_turn_plan,
load_loopx_turn_plan_from_journal,
)
-from .turn_decision import build_fresh_turn_decision_owner
from .turn_inspection import handle_turn_journal_inspection
from .turn_registration import register_turn_commands as register_turn_commands
from .turn_rendering import (
@@ -310,6 +309,8 @@ def execution_preparer(decision: Mapping[str, Any]) -> str | None:
from ..capabilities.explore.turn_context import turn_context_projector
def read_decision_owner():
+ from .turn_decision import build_fresh_turn_decision_owner
+
return build_fresh_turn_decision_owner(
args,
registry_path=registry_path,
diff --git a/loopx/cli_commands/turn_decision.py b/loopx/cli_commands/turn_decision.py
index 1c36db7f4f..edc067f02b 100644
--- a/loopx/cli_commands/turn_decision.py
+++ b/loopx/cli_commands/turn_decision.py
@@ -358,6 +358,7 @@ def current_turn_host_launch_decision(
args: argparse.Namespace, *, registry_path: Path, runtime_root: Path,
identity: SettlementIdentity,
planned_effective_action: object,
+ planned_selected_work_basis: str | None,
scheduler_execution_context: Mapping[str, Any],
operator_inbox_urgency_projector: Callable[..., dict[str, Any]],
turn_start_hook_dispatch: Mapping[str, Any] | None = None,
@@ -416,7 +417,7 @@ def current_turn_host_launch_decision(
plan = build_loopx_turn_plan(
current_contract, host=args.host, execution_mode=args.execution_mode,
scheduler_owner=args.scheduler_owner, turn_instance_id=identity.turn_instance_id,
- goal_ref=goal_ref,
+ goal_ref=goal_ref, include_selected_work_basis=planned_selected_work_basis is not None,
)
route = plan["route"]
if route.get("would_invoke_host") is not True:
@@ -425,6 +426,8 @@ def current_turn_host_launch_decision(
raise FirstPartyHostRuntimeRejected("current_turn_selection_changed")
if (identity.replan_obligation_id or identity.request_subject is not None) and plan["transaction"]["settlement_plan"]["identity"] != identity.as_dict():
raise FirstPartyHostRuntimeRejected("current_turn_selection_changed")
+ if planned_selected_work_basis is not None and plan.get("_selected_work_basis") != planned_selected_work_basis:
+ raise FirstPartyHostRuntimeRejected("current_turn_work_changed")
return launch
diff --git a/loopx/cli_commands/turn_run_once.py b/loopx/cli_commands/turn_run_once.py
index b93a5df82f..fa28ea033e 100644
--- a/loopx/cli_commands/turn_run_once.py
+++ b/loopx/cli_commands/turn_run_once.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import argparse
+from functools import partial
import sys as _sys
from collections.abc import Callable, Mapping
from pathlib import Path
@@ -767,18 +768,24 @@ def scheduler(_spend_payload: dict[str, object]) -> dict[str, object]:
host_runner: Callable[[Mapping[str, Any]], dict[str, Any]] | None = None
session_binding_resolver = None
- if args.host == "codex-app-server":
- def native_launch_decision(current_registry: Path) -> Mapping[str, Any]:
- return current_turn_host_launch_decision(args, registry_path=current_registry,
- runtime_root=runtime_root, identity=settlement_identity,
- planned_effective_action=execution_context["effective_action"],
- scheduler_execution_context=payload.get("scheduler_execution_context") or {},
- operator_inbox_urgency_projector=operator_inbox_urgency_projector,
- turn_start_hook_dispatch=turn_start_hook_dispatch, goal_ref=goal_ref)
+ def current_launch_decision(current_registry: Path) -> Mapping[str, Any]:
+ # Native and stdio transports recheck the original work at launch.
+ # A global route checks its source even if the mirror is unchanged.
+ return current_turn_host_launch_decision(
+ args, registry_path=current_registry, runtime_root=runtime_root,
+ identity=settlement_identity,
+ planned_effective_action=execution_context["effective_action"],
+ planned_selected_work_basis=execution_context.get("selected_work_basis"),
+ scheduler_execution_context=payload.get("scheduler_execution_context") or {},
+ operator_inbox_urgency_projector=operator_inbox_urgency_projector,
+ turn_start_hook_dispatch=turn_start_hook_dispatch, goal_ref=goal_ref,
+ )
+
+ if args.host == "codex-app-server":
native_admission = (goal_admission or FirstPartyHostGoalAdmission.for_plan(
registry_path=registry_path, goal_id=args.goal_id, planned_goal_ref=goal_ref,
- )).with_launch_decision(native_launch_decision)
+ )).with_launch_decision(current_launch_decision)
def run_native_host(request: Mapping[str, Any]) -> dict[str, Any]:
return native_host_runner(request, lambda: native_admission.launch_lifetime(
@@ -787,22 +794,6 @@ def run_native_host(request: Mapping[str, Any]) -> dict[str, Any]:
host_runner = run_native_host
session_binding_resolver = native_session_binding_resolver
elif args.host == "codex-cli":
-
- def current_launch_decision(current_registry: Path) -> Mapping[str, Any]:
- # Revalidate the already admitted identity through the same
- # owner. In a global route the source is checked first, even
- # when its mirror still advertises old quota or health.
- return current_turn_host_launch_decision(
- args, registry_path=current_registry, runtime_root=runtime_root,
- identity=settlement_identity,
- planned_effective_action=execution_context["effective_action"],
- scheduler_execution_context=payload.get("scheduler_execution_context") or {},
- operator_inbox_urgency_projector=operator_inbox_urgency_projector,
- # Keep this Turn's already dispatched required reads at launch.
- turn_start_hook_dispatch=turn_start_hook_dispatch,
- goal_ref=goal_ref,
- )
-
host_runner = build_codex_cli_host_runner(
args, runtime_root=runtime_root, project=project,
registry_path=registry_path, goal_ref=goal_ref,
@@ -872,6 +863,11 @@ def on_managed_start_admitted() -> None:
semantic_replan_guard_scoped=replan_guard_scoped,
semantic_replan_obligation_id=replan_obligation_id,
semantic_replan_capability_guard=capability_guard,
+ quota_spend_source=(
+ quota_spend_source_for_execution_context(payload.get("scheduler_execution_context"))
+ if settlement_identity.replan_obligation_id or settlement_identity.request_subject is not None
+ else None
+ ),
goal_ref=goal_ref,
)
@@ -919,6 +915,10 @@ def on_managed_start_admitted() -> None:
admit_start=managed_cadence.admit if args.execute else None,
confirm_start=managed_cadence.confirm if args.execute else None,
goal_admission=goal_admission,
+ host_launch_guard=(partial(
+ goal_admission.with_launch_decision(current_launch_decision).launch_lifetime,
+ operation="turn_external_host_launch",
+ ) if raw_argv is not None and goal_admission is not None else None),
)
if args.execute:
result.update(drain_committed_turn_post_writeback_hooks(
diff --git a/loopx/cli_runtime.py b/loopx/cli_runtime.py
index 70edc94748..217d412541 100644
--- a/loopx/cli_runtime.py
+++ b/loopx/cli_runtime.py
@@ -62,6 +62,7 @@
"sync-global",
"uninstall-project",
"version",
+ "postgres",
"host-mode-plan",
"migrate-local-state",
}
@@ -77,7 +78,7 @@
"todo", "quota", "task-lease", "change-window", "delegation", "turn", "doctor", "commands", "version",
"bootstrap", "connect", "configure-goal", "start-goal", "bootstrap-command-pack",
"register-agent", "bind-agent-thread",
- "authority-archive", "coordination-shadow", "extension", "slash-commands",
+ "authority-archive", "postgres", "coordination-shadow", "extension", "slash-commands",
"manager-inbox",
"agent-context", "agent-directory", "agent-capabilities",
"handoff", "handoff-mode",
@@ -492,6 +493,10 @@ def _build_selected_parser(command: str) -> LoopXArgumentParser:
from .cli_commands.authority_archive import register_authority_archive_command
register_authority_archive_command(subparsers, add_subcommand_format)
+ elif command == "postgres":
+ from .cli_commands.postgres import register_postgres_commands
+
+ register_postgres_commands(subparsers, add_subcommand_format)
elif command == "coordination-shadow":
from .cli_commands.coordination_shadow import register_coordination_shadow_command
@@ -665,6 +670,13 @@ def _dispatch_common_command(
args, registry_path=registry_path, runtime_root_arg=args.runtime_root,
output_format=output_format, print_payload=print_payload,
)
+ if args.command == "postgres":
+ from .cli_commands.postgres import handle_postgres_command
+
+ return handle_postgres_command(
+ args, registry_path=registry_path, runtime_root_arg=args.runtime_root,
+ output_format=output_format, print_payload=print_payload,
+ )
if args.command == "coordination-shadow":
from .cli_commands.coordination_shadow import handle_coordination_shadow_command
diff --git a/loopx/control_plane/collaboration/steward_managed_mode.ts b/loopx/control_plane/collaboration/steward_managed_mode.ts
index 947ea1c2a3..2a9fa3fc40 100644
--- a/loopx/control_plane/collaboration/steward_managed_mode.ts
+++ b/loopx/control_plane/collaboration/steward_managed_mode.ts
@@ -1,18 +1,21 @@
-/** Source-bound preparation of one native execution identity for a steward.
+/** Source-bound managed Session identity and native host policy.
*
- * This owner prepares/binds identity only. Goal bootstrap, Agent registration,
- * scheduling and model execution retain their existing owners. In particular,
- * a prepared identity never advertises a running monitor or an enabled host.
+ * Identity preparation grants no execution. Host policy maps only the original
+ * already-admitted Task and existing approval. Goal bootstrap, registration,
+ * scheduling and execution retain their owners; a prepared identity never
+ * advertises a running monitor or enabled host.
*/
import { createHash } from "node:crypto";
+import {posix, win32} from "node:path";
import type { JsonObject } from "../runtime_decode.ts";
-import { requireJsonObject, requireNonEmptyString } from "../runtime_decode.ts";
+import { jsonObject, requireJsonObject, requireNonEmptyString } from "../runtime_decode.ts";
import { EffectRuntimeRequestError } from "../effect_runtime_errors.ts";
import { ENVELOPED_SHA256_PATTERN } from "../content_digest.ts";
import {boundConversationExecutorMatches, goalAgentConversationIdentity, resolveConversationScope} from "./conversation_scope.ts";
import {readTurnSelectionRejection} from "../turn_driver/selection_rejection.ts";
import {EffectiveAction} from "../quota/effective_action.generated.ts";
-import {turnPlanWorkSource} from "../quota/turn_envelope.ts";
+import {projectTurnBoundary, turnPlanWorkSource} from "../quota/turn_envelope.ts";
+import {projectGoalCreationExecutionRequest} from "../goals/creation_execution.ts";
/** A stopped observation may get one new Chat carrier, never a new native
* observation identity. The host independently reads the old worker lock,
@@ -278,6 +281,7 @@ export function admitManagedSessionTurn(params: JsonObject): JsonObject {
const reportWork = request.work_kind === "report";
const scheduledWork = request.work_kind === "scheduled_task";
const requestFields = ["operation", "host", "actor", "turn_instance_id", "todo_id", "validation_command", "work_kind",
+ ...(request.creation_request === undefined ? [] : ["creation_request"]),
...(observationOnly ? ["monitor_deadline", "monitor_lease_version", "monitor_restart_recovery"] : []),
...(reportWork ? ["report_window_id", "report_subscription_revision", "report_quota_recovery"] : []),
...(scheduledWork ? ["schedule_window_id", "schedule_subscription_revision"] : [])];
@@ -288,6 +292,18 @@ export function admitManagedSessionTurn(params: JsonObject): JsonObject {
|| request.todo_id !== null && (typeof request.todo_id !== "string" || !/^todo_[A-Za-z0-9_-]{1,100}$/.test(request.todo_id))) {
throw new EffectRuntimeRequestError("managed work uses the native task's existing validation, without caller commands");
}
+ if (request.creation_request !== undefined) {
+ const original = projectGoalCreationExecutionRequest({proposal: params.creation_proposal,
+ session, goal, current_original_task: params.current_original_task});
+ const retained = requireJsonObject(request.creation_request, "original creation execution request");
+ if (request.work_kind !== "native_turn" || original === null
+ || Object.keys(retained).length !== Object.keys(original).length
+ || Object.entries(original).some(([key, value]) => JSON.stringify(retained[key]) !== JSON.stringify(value))
+ || request.todo_id !== original.todo_id
+ || request.turn_instance_id !== `managed-session.${original.operation_id}`) {
+ throw new EffectRuntimeRequestError("native execution must retain its exact confirmed original creation request");
+ }
+ }
if (observationOnly) {
const monitor = requireJsonObject(params.monitor_todo, "original managed Session monitor");
if (monitor.target_key !== `managed-session-watch.${session.session_id}`
@@ -356,8 +372,13 @@ export function admitManagedSessionTurn(params: JsonObject): JsonObject {
throw new EffectRuntimeRequestError("scheduled work requires its original explicit task, validation, Session and calendar window");
}
}
+ const creation = jsonObject(request.creation_request);
+ const queuedCreation = turn !== null && creation !== null && turn.status === "queued"
+ && turn.started_at === null && turn.upstream_turn_id === null && turn.delta_count === 0
+ && !Object.hasOwn(turn, "_acceptance")
+ && turn.client_turn_id === `managed.${creation.operation_id}`;
if (turn ? turn.session_id !== session.session_id || turn.turn_id !== session.active_turn_id
- || !["starting", "running"].includes(String(turn.status)) || turn.origin !== "native_loopx"
+ || !(["starting", "running"].includes(String(turn.status)) || queuedCreation) || turn.origin !== "native_loopx"
|| turn.loopx_execution !== true || JSON.stringify(turn.loopx_request) !== JSON.stringify(request)
: session.active_turn_id || params.queued_turn_count !== 0) {
throw new EffectRuntimeRequestError("the original conversation is busy or no longer owns this Turn; retain its current work");
@@ -732,3 +753,48 @@ export function bindStewardManagedActor(params: JsonObject): JsonObject {
goal_id: current.goalId, goal_instance_id: goal.goal_instance_id, agent_id: AGENT_ID,
session_id: current.sessionId, source_ref: current.source, creation_operation_id: current.creation});
}
+
+/** Adapt a current, already admitted native Task to the host's existing
+ * sandbox vocabulary. A Task selection or check confirmation grants no writes.
+ * Only the existing whole-workspace approval is expressible here. The current
+ * Task lease cannot enlarge it. Narrower scopes remain read-only; the native
+ * transport pins the same workspace roots in the guarded Task-start frame.
+ */
+export function canonicalNativeTaskSandboxPolicy(params: JsonObject): JsonObject {
+ const actor = requireJsonObject(params.actor, "original native Task actor");
+ const authority = requireJsonObject(params.authority, "admitted Task authority");
+ const validation = requireJsonObject(params.validation, "current Task validation plan");
+ const identity = requireJsonObject(authority._work_identity, "checked original work identity");
+ const preparation = requireJsonObject(authority.execution_preparation, "original Task execution preparation");
+ if (!(params.host === "codex-app-server" && actor.schema_version === "loopx_managed_session_actor_v0"
+ && typeof actor.workspace_path === "string" && actor.workspace_path.length > 0
+ && identity.goal_id === actor.goal_id && identity.agent_id === actor.agent_id
+ && identity.todo_id === validation.todo_id && preparation.todo_id === validation.todo_id
+ && preparation.agent_id === actor.agent_id && preparation.state === "ready"
+ && preparation.delivery_allowed === true && authority.should_run === true
+ && authority.normal_delivery_allowed === true && validation.state === "ready")) {
+ throw new EffectRuntimeRequestError("native host policy requires the original admitted Task, validation and execution lease");
+ }
+ const approval = jsonObject(projectTurnBoundary(authority).checkpointed_boundary_authority);
+ const readonly = {type: "readOnly", networkAccess: false};
+ if (approval?.schema_version !== "checkpointed_boundary_authority_v0"
+ || !Array.isArray(approval.active_write_scope) || approval.active_write_scope.length === 0) return readonly;
+ const lease = requireJsonObject(params.task_lease, "current original Task write lease");
+ if (lease.schema_version !== "task_lease_v0" || lease.status !== "active"
+ || lease.goal_id !== actor.goal_id || lease.todo_id !== validation.todo_id || lease.owner !== actor.agent_id
+ || !Number.isSafeInteger(lease.version) || Number(lease.version) < 1
+ || !Array.isArray(lease.write_scopes)) {
+ throw new EffectRuntimeRequestError("native Task writes require the same current execution lease");
+ }
+ const paths = actor.workspace_path.startsWith("/") ? posix : win32;
+ if (!paths.isAbsolute(actor.workspace_path)) {
+ throw new EffectRuntimeRequestError("native filesystem policy requires the original absolute workspace");
+ }
+ const workspace = paths.resolve(actor.workspace_path);
+ // Empty legacy inventories retain their already explicit Goal-wide grant.
+ // Fresh writable creations declare the reviewed scope on their original Task.
+ if (!approval.active_write_scope.includes("**")
+ || lease.write_scopes.length > 0 && !lease.write_scopes.includes("**")) return readonly;
+ return {type: "workspaceWrite", writableRoots: [workspace], networkAccess: false,
+ excludeTmpdirEnvVar: true, excludeSlashTmp: true};
+}
diff --git a/loopx/control_plane/coordination/authority_backup.ts b/loopx/control_plane/coordination/authority_backup.ts
index ef12c15d79..d8aee72cdd 100644
--- a/loopx/control_plane/coordination/authority_backup.ts
+++ b/loopx/control_plane/coordination/authority_backup.ts
@@ -8,7 +8,7 @@ import {requireJsonObject} from "../runtime_decode.ts";
import {isRecoveryDigest, type RecoveryComponentState} from "../state_recovery_contract.ts";
import {canonicalAuthoritySha256, requireAuthorityStoreId} from "./authority_store_codec.ts";
import {exportAuthorityArchive} from "./authority_archive.ts";
-import {withVerifiedAuthorityArchive} from "./authority_archive_read.ts";
+import {archiveCursor, withVerifiedAuthorityArchive} from "./authority_archive_read.ts";
import {canonicalTodoCollection} from "./local_authority_read.ts";
import {decodeLocalAuthoritySelection, localAuthorityProviderPaths, openRuntimeAuthorityStore,
type LocalAuthorityProviderDependencies} from "./local_authority_provider.ts";
@@ -120,6 +120,7 @@ export function decodeAuthorityBackupCapture(value: unknown): JsonObject {
}
const archive = requireJsonObject(row.archive, "authority archive summary");
const binding = requireJsonObject(row.dependencies, "authority capture dependencies");
+ archiveCursor(archive.commits);
if (archive.goal_id !== goalId || !isRecoveryDigest(archive.archive_sha256) ||
!Object.hasOwn(binding, "selector_sha256") || !Object.hasOwn(binding, "writer_fence_sha256") ||
Object.values(binding).some(value => value !== null && !isRecoveryDigest(value))) {
@@ -147,6 +148,10 @@ export async function inspectAuthorityBackup(dataRoot: string, row: JsonObject):
// then supplies its private verified copy to the Goal read-model owner.
return withVerifiedAuthorityArchive(join(directory, "authority.jsonl"), String(expected.archive_sha256), async archive => {
if (canonicalAuthoritySha256({...archive.summary}) !== canonicalAuthoritySha256(expected)) throw new Error("authority capture summary mismatch");
+ const commitCount = Number(archive.summary.commits);
+ if (!Number.isSafeInteger(commitCount) || String(commitCount) !== archive.summary.commits) {
+ throw new Error("authority history count cannot be represented in the recovery report");
+ }
const selector = await sourceBytes(join(directory, "selector.json"));
if (selector !== null) {
const selection = decodeLocalAuthoritySelection(JSON.parse(selector.toString("utf8")), goalId);
@@ -160,7 +165,7 @@ export async function inspectAuthorityBackup(dataRoot: string, row: JsonObject):
const collection = canonicalTodoCollection(head, goalId, true);
return {provider: archive.summary.source_provider, head_digest: archive.summary.projection_sha256,
provider_revision: archive.summary.source_provider_revision, cursor: archive.summary.commits,
- history_commits: Number(archive.summary.commits), todo_count: collection.projection.todo_ids.length,
+ history_commits: commitCount, todo_count: collection.projection.todo_ids.length,
historical_lease_count: collection.leaseIndex?.lease_todo_ids.length ?? 0,
capture_started_at: row.started_at, capture_completed_at: row.completed_at};
});
diff --git a/loopx/control_plane/coordination/authority_state_log.ts b/loopx/control_plane/coordination/authority_state_log.ts
index 154c6e113b..4d39784211 100644
--- a/loopx/control_plane/coordination/authority_state_log.ts
+++ b/loopx/control_plane/coordination/authority_state_log.ts
@@ -206,7 +206,7 @@ export class AuthorityStateReplay {
this.#state = canonicalAuthorityObject(projection, "authority replay state");
}
- apply(delta: AuthorityStateDelta): void {
+ apply(delta: unknown): void {
let next = this.#state;
// Decode before applying and publish only after the whole batch succeeds.
// A rejected suffix cannot leave a half-applied replay frontier.
diff --git a/loopx/control_plane/coordination/postgresql_authority_seed.ts b/loopx/control_plane/coordination/postgresql_authority_seed.ts
new file mode 100644
index 0000000000..e41aed182e
--- /dev/null
+++ b/loopx/control_plane/coordination/postgresql_authority_seed.ts
@@ -0,0 +1,645 @@
+/**
+ * Administrative PostgreSQL schema setup, restricted role assembly, and
+ * local-to-pg-seed genesis snapshot sanitizer and exporter.
+ *
+ * Requirements:
+ * 1. Administrator-explicit PG schema and restricted role assembly.
+ * 2. Read-only local-to-pg-seed snapshot sanitization (cursor=1 clean seed):
+ * - Strips expired leases
+ * - Strips local absolute paths (/Users/..., /home/..., /private/...)
+ * - Strips .local/ paths and private material references
+ * - Strips Feishu/Lark user/chat/app/union IDs (ou_*, oc_*, cli_*, on_*)
+ * - Fail-closed if any privacy/credential pattern remains.
+ */
+import { randomBytes, randomUUID } from "node:crypto";
+import { constants } from "node:fs";
+import { link, lstat, open, unlink } from "node:fs/promises";
+import { dirname, join } from "node:path";
+import type { JsonObject } from "../effect_program.ts";
+import {
+ canonicalAuthorityObject,
+ canonicalAuthoritySha256,
+ isAuthorityJsonObject,
+ requireAuthorityStoreId,
+} from "./authority_store_codec.ts";
+import {
+ POSTGRESQL_RUNTIME_ROLE_PATTERN,
+ POSTGRESQL_SCHEMA_VERSION,
+ POSTGRESQL_STORE_IDENTITY_PATTERN,
+ installPostgreSqlAuthorityStoreSchema,
+ PostgreSqlAuthorityStore,
+ type PostgreSqlAuthorityDatabase,
+ type PostgreSqlAuthorityConnection,
+} from "./postgresql_authority_store.ts";
+import { syncAuthorityDirectory } from "./file_authority_store.ts";
+import { openLocalAuthorityStoreHandle } from "./local_authority_provider.ts";
+
+export const PG_GENESIS_SEED_SCHEMA_VERSION = "loopx_pg_genesis_seed_v0";
+
+// Privacy token / ID patterns
+const FEISHU_ID_PATTERNS = [
+ /(?]+/g,
+];
+
+const SECRET_PATTERNS = [
+ /\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9_]{36,255}\b/,
+ /\b(?:sk|pk)_(?:live|test)_[0-9a-zA-Z]{24,}\b/,
+ /\bsk-[a-zA-Z0-9_-]{20,}\b/,
+ /\bAKIA[0-9A-Z]{16}\b/,
+ /\bBearer\s+[A-Za-z0-9\-._~+/]+=*\b/i,
+ /\b(?:password|passwd|secret|api_key|access_token|private_key)\s*[:=]\s*["']?[^\s"']{8,}["']?/i,
+];
+
+export interface GenesisSanitizationReport {
+ leases_stripped: number;
+ private_materials_stripped: number;
+ paths_redacted: number;
+ feishu_ids_redacted: number;
+ todos_sanitized: number;
+}
+
+export class GenesisPrivacyBoundaryViolation extends Error {
+ readonly reasonCode = "genesis_privacy_boundary_violation";
+ constructor(message: string) {
+ super(message);
+ this.name = "GenesisPrivacyBoundaryViolation";
+ }
+}
+
+export function sanitizeText(
+ text: string,
+ report: GenesisSanitizationReport,
+): string {
+ let result = text;
+
+ // Redact Feishu IDs
+ for (const pattern of FEISHU_ID_PATTERNS) {
+ result = result.replace(pattern, () => {
+ report.feishu_ids_redacted += 1;
+ return "[feishu_id_redacted]";
+ });
+ }
+
+ // Redact local absolute paths in text
+ for (const pattern of LOCAL_PATH_PATTERNS) {
+ result = result.replace(pattern, () => {
+ report.paths_redacted += 1;
+ return "[local_path_redacted]";
+ });
+ }
+
+ // Remove .local/ references
+ if (isLocalReference(result)) {
+ result = result.replace(/\.local[\\/][^\s)"']*/g, () => {
+ report.paths_redacted += 1;
+ return "[local_scope_redacted]";
+ });
+ }
+
+ return result;
+}
+
+export function sanitizeObjectRecursively(
+ val: unknown,
+ report: GenesisSanitizationReport,
+): unknown {
+ if (typeof val === "string") {
+ return sanitizeText(val, report);
+ }
+ if (Array.isArray(val)) {
+ return val.map((item) => sanitizeObjectRecursively(item, report));
+ }
+ if (val !== null && typeof val === "object") {
+ return Object.fromEntries(Object.entries(val).map(([key, value]) =>
+ [key, sanitizeObjectRecursively(value, report)]));
+ }
+ return val;
+}
+
+function isLocalReference(value: string): boolean {
+ return /\.local[\\/]/.test(value);
+}
+
+function matchesRedaction(pattern: RegExp, value: string): boolean {
+ // search starts at zero even for the global patterns used by replace.
+ return value.search(pattern) !== -1;
+}
+
+export function scanForbiddenPrivacyTokens(value: unknown, path = "projection"): void {
+ if (typeof value === "string") {
+ for (const pattern of SECRET_PATTERNS) {
+ if (pattern.test(value)) {
+ throw new GenesisPrivacyBoundaryViolation(
+ `Privacy boundary violation at ${path}: matched sensitive credential pattern`,
+ );
+ }
+ }
+ if (isLocalReference(value)) {
+ throw new GenesisPrivacyBoundaryViolation(
+ `Privacy boundary violation at ${path}: contains raw unredacted .local/ reference`,
+ );
+ }
+ for (const pattern of LOCAL_PATH_PATTERNS) {
+ if (matchesRedaction(pattern, value)) {
+ throw new GenesisPrivacyBoundaryViolation(
+ `Privacy boundary violation at ${path}: contains raw unredacted local absolute path`,
+ );
+ }
+ }
+ for (const pattern of FEISHU_ID_PATTERNS) {
+ if (matchesRedaction(pattern, value)) {
+ throw new GenesisPrivacyBoundaryViolation(
+ `Privacy boundary violation at ${path}: contains raw unredacted Feishu ID`,
+ );
+ }
+ }
+ return;
+ }
+
+ if (Array.isArray(value)) {
+ for (let i = 0; i < value.length; i++) {
+ scanForbiddenPrivacyTokens(value[i], `${path}[${i}]`);
+ }
+ return;
+ }
+
+ if (value !== null && typeof value === "object") {
+ for (const [index, [key, field]] of Object.entries(value).entries()) {
+ // A field name can itself be a private ID or secret. Never echo it.
+ const location = path + "{" + index + "}";
+ scanForbiddenPrivacyTokens(key, location);
+ scanForbiddenPrivacyTokens(field, location);
+ }
+ }
+}
+
+/**
+ * Sanitizes a projection for genesis PG export.
+ * Resets metadata, strips expired leases, sanitizes paths and Feishu IDs,
+ * strips private materials, and runs a fail-closed boundary scan.
+ */
+export function sanitizeGenesisSeedProjection(rawProjection: JsonObject): {
+ projection: JsonObject;
+ report: GenesisSanitizationReport;
+} {
+ const report: GenesisSanitizationReport = {
+ leases_stripped: 0,
+ private_materials_stripped: 0,
+ paths_redacted: 0,
+ feishu_ids_redacted: 0,
+ todos_sanitized: 0,
+ };
+
+ const projection = structuredClone(rawProjection);
+
+ // 1. Strip expired leases (support both leases array and task_leases object/array)
+ const nowMs = Date.now();
+ if (Array.isArray(projection.leases)) {
+ const originalLeaseCount = projection.leases.length;
+ const retainedLeases = (projection.leases as JsonObject[]).filter((lease) => {
+ if (!lease || typeof lease !== "object") return false;
+ const expiresAt = lease.expires_at;
+ if (typeof expiresAt === "number" && expiresAt <= nowMs) return false;
+ if (typeof expiresAt === "string") {
+ const parsed = Date.parse(expiresAt);
+ if (!isNaN(parsed) && parsed <= nowMs) return false;
+ }
+ return true;
+ });
+ projection.leases = retainedLeases;
+ report.leases_stripped += originalLeaseCount - retainedLeases.length;
+ }
+ if (projection.task_leases && typeof projection.task_leases === "object") {
+ const taskLeases = projection.task_leases as JsonObject;
+ if (taskLeases.leases && typeof taskLeases.leases === "object" && !Array.isArray(taskLeases.leases)) {
+ const remaining: Record = Object.create(null);
+ for (const [k, v] of Object.entries(taskLeases.leases as Record)) {
+ if (!v || typeof v !== "object") continue;
+ const expiresAt = v.expires_at;
+ if (typeof expiresAt === "number" && expiresAt <= nowMs) {
+ report.leases_stripped += 1;
+ continue;
+ }
+ if (typeof expiresAt === "string") {
+ const parsed = Date.parse(expiresAt);
+ if (!isNaN(parsed) && parsed <= nowMs) {
+ report.leases_stripped += 1;
+ continue;
+ }
+ }
+ remaining[k] = v;
+ }
+ taskLeases.leases = remaining;
+ }
+ }
+
+ // 2. Strip private materials (support project_materials and registered_materials)
+ for (const matKey of ["project_materials", "registered_materials"] as const) {
+ if (Array.isArray(projection[matKey])) {
+ const originalMaterialCount = (projection[matKey] as JsonObject[]).length;
+ projection[matKey] = (
+ projection[matKey] as JsonObject[]
+ ).filter((material) => {
+ if (!material || typeof material !== "object") return false;
+ return !privateMaterial(material);
+ });
+ report.private_materials_stripped +=
+ originalMaterialCount - (projection[matKey] as JsonObject[]).length;
+ }
+ }
+
+ // The direction registry, scope digest and usage receipts are one lineage.
+ // A redacted registry cannot retain source attestations or be re-signed as
+ // original evidence in a synthetic seed.
+ const registry = projection.authority_registry;
+ if (isAuthorityJsonObject(registry) && isAuthorityJsonObject(registry.project_materials)) {
+ const materials = Object.values(registry.project_materials);
+ const removed = materials.filter(row => isAuthorityJsonObject(row) && privateMaterial(row)).length;
+ const sanitizedRegistry = sanitizeObjectRecursively(registry, {...report});
+ if (removed > 0 || canonicalAuthoritySha256(sanitizedRegistry) !== canonicalAuthoritySha256(registry)) {
+ report.private_materials_stripped += removed;
+ delete projection.authority_registry;
+ delete projection.goal_direction_scope;
+ delete projection.material_usage_receipts;
+ }
+ }
+
+ // 3. Sanitize Todos (required_write_scopes, text, title, note, evidence)
+ if (Array.isArray(projection.todos)) {
+ for (const todo of projection.todos as JsonObject[]) {
+ if (!todo || typeof todo !== "object") continue;
+ report.todos_sanitized += 1;
+
+ // Sanitize required_write_scopes: strip .local/ and make paths relative
+ if (Array.isArray(todo.required_write_scopes)) {
+ todo.required_write_scopes = (
+ todo.required_write_scopes as string[]
+ )
+ .filter((scope) => typeof scope === "string" && !isLocalReference(scope))
+ .map((scope) => {
+ // Strip machine-specific user prefix
+ for (const pattern of LOCAL_PATH_PATTERNS) {
+ if (matchesRedaction(pattern, scope)) {
+ report.paths_redacted += 1;
+ // Convert e.g. /Users/.../loopx/packages/foo -> packages/foo
+ const parts = scope.split("/");
+ const loopxIdx = parts.indexOf("loopx");
+ if (loopxIdx >= 0 && loopxIdx < parts.length - 1) {
+ return parts.slice(loopxIdx + 1).join("/");
+ }
+ return parts[parts.length - 1] ?? "";
+ }
+ }
+ return scope;
+ });
+ }
+ }
+ }
+
+ // 4. Sanitize topic_authority and direction
+ if (Array.isArray(projection.topic_authority)) {
+ projection.topic_authority = (
+ projection.topic_authority as JsonObject[]
+ ).filter((topic) => {
+ if (!topic || typeof topic !== "object") return false;
+ const boundary = String(topic.boundary ?? "");
+ return boundary !== "local_private" && boundary !== "private";
+ });
+ }
+
+ // 5. Recursively sanitize all strings (Feishu IDs, local paths, .local references)
+ const sanitized = sanitizeObjectRecursively(projection, report) as JsonObject;
+
+ // 6. Fail-closed scan: any remaining credential or sensitive token triggers immediate failure
+ scanForbiddenPrivacyTokens(sanitized, "projection");
+
+ return { projection: sanitized, report };
+}
+
+function privateMaterial(material: JsonObject): boolean {
+ const boundary = String(material.boundary ?? material.visibility ?? "");
+ return ["local_private", "private", "restricted"].includes(boundary) ||
+ isLocalReference(String(material.source_url ?? material.url ?? "")) ||
+ isLocalReference(String(material.source_path ?? material.path ?? ""));
+}
+
+export interface PostgreSqlAdminSetupOptions {
+ database?: PostgreSqlAuthorityDatabase;
+ databaseUrl?: string;
+ storeIdentity?: string;
+ runtimeRole?: string;
+}
+
+export interface PostgreSqlAdminSetupResult {
+ status: "installed" | "failed";
+ schema_version: string;
+ store_identity: string;
+ runtime_role: string | null;
+ reason?: string;
+}
+
+async function resolveDatabase(options: {
+ database?: PostgreSqlAuthorityDatabase;
+ targetDatabase?: PostgreSqlAuthorityDatabase;
+ databaseUrl?: string;
+}): Promise<{ database: PostgreSqlAuthorityDatabase; close: () => Promise } | null> {
+ const db = options.database ?? options.targetDatabase;
+ if (db) {
+ return { database: db, close: async () => {} };
+ }
+ if (options.databaseUrl) {
+ const { Pool } = await import("pg");
+ const pool = new Pool({ connectionString: options.databaseUrl });
+ const database: PostgreSqlAuthorityDatabase = {
+ connect: async () => {
+ const client = await pool.connect();
+ return {
+ query: async (text: string, values?: readonly unknown[]) =>
+ await client.query(text, values ? [...values] : undefined),
+ release: (error?: Error) => client.release(error),
+ };
+ },
+ };
+ return { database, close: async () => await pool.end() };
+ }
+ return null;
+}
+
+/**
+ * Administrative explicit schema and restricted role assembly.
+ */
+export async function setupPostgreSqlAuthoritySchemaAndRole(
+ databaseOrOptions: PostgreSqlAuthorityDatabase | PostgreSqlAdminSetupOptions,
+ maybeOptions?: PostgreSqlAdminSetupOptions,
+): Promise {
+ const options: PostgreSqlAdminSetupOptions =
+ databaseOrOptions && "connect" in databaseOrOptions
+ ? { ...maybeOptions, database: databaseOrOptions as PostgreSqlAuthorityDatabase }
+ : (databaseOrOptions as PostgreSqlAdminSetupOptions);
+
+ const storeIdentity =
+ options.storeIdentity ??
+ `postgresql:${randomBytes(16).toString("hex").toLowerCase()}`;
+
+ if (!POSTGRESQL_STORE_IDENTITY_PATTERN.test(storeIdentity)) {
+ return {
+ status: "failed",
+ schema_version: POSTGRESQL_SCHEMA_VERSION,
+ store_identity: storeIdentity,
+ runtime_role: options.runtimeRole ?? null,
+ reason: "PostgreSQL store identity must match postgresql:<32 lowercase hex>",
+ };
+ }
+
+ const role = options.runtimeRole?.trim();
+ if (role !== undefined && !POSTGRESQL_RUNTIME_ROLE_PATTERN.test(role)) {
+ return {
+ status: "failed",
+ schema_version: POSTGRESQL_SCHEMA_VERSION,
+ store_identity: storeIdentity,
+ runtime_role: null,
+ reason: "runtime role must be a valid lowercase SQL identifier",
+ };
+ }
+
+ const resolved = await resolveDatabase(options);
+ if (!resolved) {
+ return {
+ status: "failed",
+ schema_version: POSTGRESQL_SCHEMA_VERSION,
+ store_identity: storeIdentity,
+ runtime_role: options.runtimeRole ?? null,
+ reason: "PostgreSQL database or databaseUrl must be provided for schema setup",
+ };
+ }
+
+ try {
+ await installPostgreSqlAuthorityStoreSchema(resolved.database, storeIdentity, role);
+
+ return {
+ status: "installed",
+ schema_version: POSTGRESQL_SCHEMA_VERSION,
+ store_identity: storeIdentity,
+ runtime_role: role ?? null,
+ };
+ } catch (error) {
+ return {
+ status: "failed",
+ schema_version: POSTGRESQL_SCHEMA_VERSION,
+ store_identity: storeIdentity,
+ runtime_role: options.runtimeRole ?? null,
+ reason: error instanceof Error ? error.message : String(error),
+ };
+ } finally {
+ await resolved.close();
+ }
+}
+
+export interface LocalToPgSeedExportOptions {
+ runtimeRoot: string;
+ goalId: string;
+ tenantId?: string;
+ outputPath?: string;
+ localStore?: import("./authority_store.ts").AuthorityStore;
+ targetDatabase?: PostgreSqlAuthorityDatabase;
+ databaseUrl?: string;
+ storeIdentity?: string;
+ execute?: boolean;
+}
+
+export interface LocalToPgSeedExportResult {
+ status: "exported" | "seeded" | "failed";
+ schema_version: string;
+ goal_id: string;
+ tenant_id: string;
+ cursor: string;
+ provider_revision: string;
+ operation_id: string;
+ seed_digest?: string;
+ sanitization_report?: GenesisSanitizationReport;
+ output_path?: string;
+ reason?: string;
+ seed_payload?: JsonObject;
+}
+
+class GenesisSeedOutputConflict extends Error {
+ constructor() {
+ super("Genesis seed output must be absent or an identical regular file");
+ }
+}
+
+async function existingSeedMatches(path: string, expected: Buffer): Promise {
+ let declared;
+ try {
+ declared = await lstat(path);
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return false;
+ throw error;
+ }
+ if (!declared.isFile() || declared.size !== expected.length) throw new GenesisSeedOutputConflict();
+ const file = await open(path, constants.O_RDONLY | (constants.O_NONBLOCK ?? 0) | (constants.O_NOFOLLOW ?? 0));
+ try {
+ const stat = await file.stat();
+ if (!stat.isFile() || stat.dev !== declared.dev || stat.ino !== declared.ino) throw new GenesisSeedOutputConflict();
+ let offset = 0;
+ for await (const part of file.createReadStream({autoClose: false})) {
+ if (!part.equals(expected.subarray(offset, offset + part.length))) {
+ throw new GenesisSeedOutputConflict();
+ }
+ offset += part.length;
+ }
+ if (offset !== expected.length) throw new GenesisSeedOutputConflict();
+ return true;
+ } finally {
+ await file.close();
+ }
+}
+
+async function writeSeedOutput(path: string, bytes: Buffer): Promise {
+ const directory = dirname(path);
+ if (!await existingSeedMatches(path, bytes)) {
+ // Publish a complete private file without replacing a concurrent writer's
+ // output. A concurrent replay may reuse the winner's identical bytes.
+ const temporary = join(directory, `.loopx-seed-${randomUUID()}.partial`);
+ const file = await open(temporary, "wx", 0o600);
+ try {
+ try {
+ await file.writeFile(bytes);
+ await file.sync();
+ } finally {
+ await file.close();
+ }
+ try {
+ await link(temporary, path);
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code !== "EEXIST" ||
+ !await existingSeedMatches(path, bytes)) throw error;
+ }
+ } finally {
+ await unlink(temporary);
+ }
+ }
+ await syncAuthorityDirectory(directory);
+}
+
+/**
+ * Exports a local authority store into a sanitized genesis seed (cursor=1).
+ * When targetDatabase and execute=true are provided, atomically commits into PG.
+ */
+export async function exportLocalToPgGenesisSeed(
+ options: LocalToPgSeedExportOptions,
+): Promise {
+ const tenantId = options.tenantId ?? "default";
+ const operationId = `${options.goalId}-genesis-seed`;
+ const base = {
+ schema_version: PG_GENESIS_SEED_SCHEMA_VERSION,
+ goal_id: options.goalId,
+ tenant_id: tenantId,
+ cursor: "1",
+ provider_revision: "1",
+ operation_id: operationId,
+ };
+ let scopeValid = false;
+ const failed = (reason: string): LocalToPgSeedExportResult =>
+ ({...base, ...(!scopeValid ? {goal_id: "", tenant_id: "", operation_id: ""} : {}), status: "failed", reason});
+
+ try {
+ for (const value of [options.goalId, tenantId, operationId]) {
+ requireAuthorityStoreId(value, "genesis scope id");
+ scanForbiddenPrivacyTokens(value, "genesis scope");
+ }
+ scopeValid = true;
+ if (options.execute && !options.targetDatabase && !options.databaseUrl) {
+ return failed("Executing a genesis seed requires a target PostgreSQL database");
+ }
+ if (options.storeIdentity !== undefined &&
+ !POSTGRESQL_STORE_IDENTITY_PATTERN.test(options.storeIdentity)) {
+ return failed("Target PostgreSQL store identity is invalid");
+ }
+
+ const store = options.localStore ??
+ (await openLocalAuthorityStoreHandle(options.runtimeRoot, options.goalId, {}, {existingOnly: true})).store;
+ if (store.providerKind === "postgresql") return failed("Genesis export requires a local File or SQLite source");
+ const loaded = await store.loadAuthority();
+ if (loaded.status !== "loaded") return failed("Local authority head is not available");
+
+ const {projection, report} = sanitizeGenesisSeedProjection(loaded.head);
+ const seedDigest = canonicalAuthoritySha256(projection);
+ const seedPayload: JsonObject = {
+ ...base, seed_digest: seedDigest, sanitization_report: {...report}, projection,
+ };
+ const exported = {
+ ...base, seed_digest: seedDigest, sanitization_report: report,
+ output_path: options.outputPath, seed_payload: seedPayload,
+ };
+ if (options.outputPath) {
+ await writeSeedOutput(options.outputPath, Buffer.from(JSON.stringify(seedPayload, null, 2)));
+ }
+ // Export never opens the target or installs anything in either store.
+ if (!options.execute) return {...exported, status: "exported"};
+
+ const resolved = await resolveDatabase(options);
+ if (!resolved) return failed("Target PostgreSQL database is not available for execution");
+ try {
+ // Discover the provisioned incarnation if the caller did not pin one.
+ // Subsequent writes check this identity inside the commit's rotation lock.
+ const scope = {tenant_id: tenantId, goal_id: options.goalId};
+ const identity = await new PostgreSqlAuthorityStore(resolved.database, {
+ ...scope, expected_store_identity: options.storeIdentity,
+ }).storeIdentity();
+ if (identity.status !== "available") return failed("Target PostgreSQL store identity is unavailable or mismatched");
+ const pgStore = new PostgreSqlAuthorityStore(resolved.database, {
+ ...scope, expected_store_identity: identity.store_identity,
+ });
+
+ const original = async () => {
+ const receipt = await pgStore.readReceipt(operationId);
+ if (receipt.status !== "found" || receipt.cursor !== "1" || receipt.receipts.length !== 1) return null;
+ const record = receipt.receipts[0];
+ return record.schema_version === "genesis_seed_receipt_v0" &&
+ record.source_goal_id === options.goalId && record.seed_digest === seedDigest ? receipt : null;
+ };
+ const existing = await pgStore.loadAuthority();
+ if (existing.status === "loaded") {
+ const receipt = await original();
+ return receipt ? {...exported, status: "seeded", provider_revision: receipt.provider_revision}
+ : failed("Target PostgreSQL goal already has a different committed authority");
+ }
+ if (existing.status !== "missing") return failed("Target PostgreSQL authority state is unavailable");
+
+ const committed = await pgStore.commitAuthority({
+ operation_id: operationId,
+ expected_provider_revision: null,
+ events: [],
+ receipts: [{
+ schema_version: "genesis_seed_receipt_v0",
+ source: "local-to-pg-seed",
+ source_goal_id: options.goalId,
+ seed_digest: seedDigest,
+ sanitization_report: {...report},
+ }],
+ next_projection: projection,
+ });
+ if (committed.status === "applied") {
+ return {...exported, status: "seeded", provider_revision: committed.provider_revision};
+ }
+ if (committed.status === "ambiguous" || committed.status === "conflict") {
+ const receipt = await original();
+ if (receipt) return {...exported, status: "seeded", provider_revision: receipt.provider_revision};
+ if (committed.status === "ambiguous") {
+ return failed("PostgreSQL seed outcome is unknown; retry the same seed to resolve the original receipt");
+ }
+ }
+ return failed(`PostgreSQL genesis seed was not applied (${committed.status})`);
+ } finally {
+ await resolved.close();
+ }
+ } catch (error) {
+ return failed(error instanceof GenesisPrivacyBoundaryViolation || error instanceof GenesisSeedOutputConflict
+ ? error.message : "Genesis seed could not be prepared or written");
+ }
+}
diff --git a/loopx/control_plane/coordination/postgresql_authority_store.ts b/loopx/control_plane/coordination/postgresql_authority_store.ts
index 2285ff4f83..d0c7146f79 100644
--- a/loopx/control_plane/coordination/postgresql_authority_store.ts
+++ b/loopx/control_plane/coordination/postgresql_authority_store.ts
@@ -1,12 +1,20 @@
import {AuthorityJournalScan} from "./authority_journal_scan.ts";
import type { JsonObject } from "../effect_program.ts";
+import {
+ authorityStateCheckpointCursor,
+ authorityStateDelta,
+ isAuthorityStateCheckpoint,
+ AuthorityStateReplay,
+} from "./authority_state_log.ts";
import type {
AuthorityStore,
AuthorityStoreCommit,
AuthorityStoreCommitResult,
+ AuthorityStoreCommittedTransaction,
AuthorityStoreIdentityResult,
AuthorityStoreLoadResult,
AuthorityStoreReadFailure,
+ AuthorityStoreReceiptBatchResult,
AuthorityStoreReceiptResult,
AuthorityStoreReceiptSnapshotResult,
AuthorityStoreScanResult,
@@ -16,6 +24,7 @@ import {
canonicalAuthorityBytes,
canonicalAuthorityObject,
canonicalAuthorityObjectList,
+ canonicalAuthoritySha256,
normalizeAuthorityStoreCommit,
parseAuthorityCursor,
requireAuthorityStoreId,
@@ -24,7 +33,9 @@ import {
export const POSTGRESQL_STORE_IDENTITY_PATTERN = /^postgresql:[0-9a-f]{32}$/;
const POSTGRESQL_PROVIDER_REVISION_PATTERN = /^postgresql:([0-9a-f]{32}):([1-9]\d*)$/;
const POSTGRESQL_STORE_IDENTITY_LOCK_KEY = "5522768808912313929";
-export const POSTGRESQL_SCHEMA_VERSION = "loopx_postgresql_authority_store_v0";
+const FULL_PROJECTION_SCHEMA_VERSION = "loopx_postgresql_authority_store_v0";
+export const POSTGRESQL_SCHEMA_VERSION = "loopx_postgresql_authority_store_v1";
+export const POSTGRESQL_RUNTIME_ROLE_PATTERN = /^[a-z][a-z0-9_]{1,62}$/;
export const DEFAULT_POSTGRESQL_MAX_COMMIT_BYTES = 16 * 1024 * 1024;
/**
@@ -48,6 +59,8 @@ export interface PostgreSqlAuthorityDatabase {
export interface PostgreSqlAuthorityStoreOptions {
tenant_id: string;
goal_id: string;
+ /** Bind an explicit target even when the expected Goal revision is empty. */
+ expected_store_identity?: string;
/**
* Maximum canonical bytes admitted for one atomic commit. Deployments may
* lower this envelope; promotion still requires separately measured
@@ -62,15 +75,6 @@ interface HeadRow extends JsonObject {
head: JsonObject | null;
}
-interface TransactionRow extends JsonObject {
- cursor: string;
- provider_revision: string;
- operation_id: string;
- projection: JsonObject;
- events: JsonObject[];
- receipts: JsonObject[];
-}
-
export const POSTGRESQL_AUTHORITY_STORE_SCHEMA_SQL = `
CREATE SCHEMA IF NOT EXISTS loopx_control_plane;
@@ -93,13 +97,28 @@ CREATE TABLE IF NOT EXISTS loopx_control_plane.authority_heads (
(cursor > 0 AND provider_revision > 0 AND head IS NOT NULL))
);
+CREATE TABLE IF NOT EXISTS loopx_control_plane.authority_checkpoints (
+ tenant_id text NOT NULL,
+ goal_id text NOT NULL,
+ cursor bigint NOT NULL CHECK (cursor > 0),
+ projection jsonb NOT NULL,
+ projection_digest text NOT NULL CHECK (length(projection_digest) = 64),
+ created_at timestamptz NOT NULL DEFAULT transaction_timestamp(),
+ PRIMARY KEY (tenant_id, goal_id, cursor),
+ FOREIGN KEY (tenant_id, goal_id)
+ REFERENCES loopx_control_plane.authority_heads (tenant_id, goal_id)
+ ON DELETE CASCADE
+);
+
CREATE TABLE IF NOT EXISTS loopx_control_plane.authority_commits (
tenant_id text NOT NULL,
goal_id text NOT NULL,
cursor bigint NOT NULL CHECK (cursor > 0),
provider_revision bigint NOT NULL CHECK (provider_revision > 0),
operation_id text NOT NULL,
- projection jsonb NOT NULL,
+ delta jsonb,
+ projection jsonb,
+ projection_digest text,
committed_at timestamptz NOT NULL DEFAULT transaction_timestamp(),
PRIMARY KEY (tenant_id, goal_id, cursor),
UNIQUE (tenant_id, goal_id, operation_id),
@@ -134,6 +153,8 @@ CREATE TABLE IF NOT EXISTS loopx_control_plane.authority_receipts (
ALTER TABLE loopx_control_plane.authority_heads ENABLE ROW LEVEL SECURITY;
ALTER TABLE loopx_control_plane.authority_heads FORCE ROW LEVEL SECURITY;
+ALTER TABLE loopx_control_plane.authority_checkpoints ENABLE ROW LEVEL SECURITY;
+ALTER TABLE loopx_control_plane.authority_checkpoints FORCE ROW LEVEL SECURITY;
ALTER TABLE loopx_control_plane.authority_commits ENABLE ROW LEVEL SECURITY;
ALTER TABLE loopx_control_plane.authority_commits FORCE ROW LEVEL SECURITY;
ALTER TABLE loopx_control_plane.authority_events ENABLE ROW LEVEL SECURITY;
@@ -148,6 +169,13 @@ CREATE POLICY authority_heads_tenant_scope
USING (tenant_id = current_setting('loopx.tenant_id', TRUE))
WITH CHECK (tenant_id = current_setting('loopx.tenant_id', TRUE));
+DROP POLICY IF EXISTS authority_checkpoints_tenant_scope
+ ON loopx_control_plane.authority_checkpoints;
+CREATE POLICY authority_checkpoints_tenant_scope
+ ON loopx_control_plane.authority_checkpoints
+ USING (tenant_id = current_setting('loopx.tenant_id', TRUE))
+ WITH CHECK (tenant_id = current_setting('loopx.tenant_id', TRUE));
+
DROP POLICY IF EXISTS authority_commits_tenant_scope
ON loopx_control_plane.authority_commits;
CREATE POLICY authority_commits_tenant_scope
@@ -182,7 +210,11 @@ const SELECT_TRANSACTION_COLUMNS_SQL = `
SELECT commit.cursor::text AS cursor,
commit.provider_revision::text AS provider_revision,
commit.operation_id,
+ commit.delta,
commit.projection,
+ commit.projection_digest,
+ checkpoint.projection AS checkpoint_projection,
+ checkpoint.projection_digest AS checkpoint_digest,
COALESCE((
SELECT jsonb_agg(event.event ORDER BY event.event_index)
FROM loopx_control_plane.authority_events AS event
@@ -198,6 +230,23 @@ SELECT commit.cursor::text AS cursor,
AND receipt.cursor = commit.cursor
), '[]'::jsonb) AS receipts
FROM loopx_control_plane.authority_commits AS commit
+LEFT JOIN loopx_control_plane.authority_checkpoints AS checkpoint
+ ON checkpoint.tenant_id = commit.tenant_id AND checkpoint.goal_id = commit.goal_id
+ AND checkpoint.cursor = commit.cursor
+`;
+
+const SELECT_RECEIPT_COLUMNS_SQL = `
+SELECT commit.cursor::text AS cursor,
+ commit.provider_revision::text AS provider_revision,
+ commit.operation_id,
+ COALESCE((
+ SELECT jsonb_agg(receipt.receipt ORDER BY receipt.receipt_index)
+ FROM loopx_control_plane.authority_receipts AS receipt
+ WHERE receipt.tenant_id = commit.tenant_id
+ AND receipt.goal_id = commit.goal_id
+ AND receipt.cursor = commit.cursor
+ ), '[]'::jsonb) AS receipts
+FROM loopx_control_plane.authority_commits AS commit
`;
function rows(result: { rows: readonly unknown[] }): readonly JsonObject[] {
@@ -240,23 +289,6 @@ function decodeHeadRow(value: JsonObject): HeadRow {
};
}
-function decodeTransactionRow(value: JsonObject): TransactionRow {
- const cursor = requiredRowString(value, "cursor");
- const providerRevision = requiredRowString(value, "provider_revision");
- parseAuthorityCursor(cursor);
- if (!/^[1-9]\d*$/.test(providerRevision)) {
- throw new AuthorityStoreProtocolError("PostgreSQL provider revision is invalid");
- }
- return {
- cursor,
- provider_revision: providerRevision,
- operation_id: requiredRowString(value, "operation_id"),
- projection: canonicalAuthorityObject(value.projection, "transaction projection"),
- events: canonicalAuthorityObjectList(value.events, "transaction events"),
- receipts: canonicalAuthorityObjectList(value.receipts, "transaction receipts"),
- };
-}
-
function providerRevisionToken(storeIdentity: string, revision: string): string {
if (!POSTGRESQL_STORE_IDENTITY_PATTERN.test(storeIdentity)) {
throw new AuthorityStoreProtocolError("PostgreSQL store identity is invalid");
@@ -267,6 +299,16 @@ function providerRevisionToken(storeIdentity: string, revision: string): string
return `${storeIdentity}:${revision}`;
}
+function decodeReceipt(row: JsonObject, storeIdentity: string): Extract {
+ const cursor = requiredRowString(row, "cursor");
+ if (parseAuthorityCursor(cursor) < 1n) {
+ throw new AuthorityStoreProtocolError("PostgreSQL receipt cursor must be positive");
+ }
+ return {status: "found", cursor, provider_revision: providerRevisionToken(
+ storeIdentity, requiredRowString(row, "provider_revision"),
+ ), receipts: canonicalAuthorityObjectList(row.receipts, "receipts")};
+}
+
function parseExpectedRevision(value: string | null): {
store_identity: string;
revision: string;
@@ -372,23 +414,31 @@ async function lockStoreIdentityTransaction(
}
/**
- * Administrative schema installation. Call this only from the authenticated
- * service deployment path with a newly minted database-incarnation identity.
+ * Administrative schema installation and provisioning under one transaction.
+ * A new store uses a new identity. A format upgrade binds the current identity
+ * and keeps the existing journal access available through the checkpoint table.
*/
export async function installPostgreSqlAuthorityStoreSchema(
database: PostgreSqlAuthorityDatabase,
storeIdentity: string,
+ runtimeRole?: string,
): Promise {
if (!POSTGRESQL_STORE_IDENTITY_PATTERN.test(storeIdentity)) {
throw new AuthorityStoreProtocolError(
"PostgreSQL store identity must match postgresql:<32 lowercase hex>",
);
}
+ if (runtimeRole !== undefined && !POSTGRESQL_RUNTIME_ROLE_PATTERN.test(runtimeRole)) {
+ throw new AuthorityStoreProtocolError("runtime role must be a valid lowercase SQL identifier");
+ }
const connection = await database.connect();
let commitStarted = false;
let releaseError: Error | undefined;
try {
await connection.query("BEGIN");
+ // Fence old writers before publishing the first journal format they
+ // cannot read. Rotation and ordinary commits take the same lock.
+ await lockStoreIdentityTransaction(connection, "exclusive");
await connection.query(POSTGRESQL_AUTHORITY_STORE_SCHEMA_SQL);
await connection.query(
`INSERT INTO loopx_control_plane.authority_store_metadata
@@ -404,13 +454,79 @@ export async function installPostgreSqlAuthorityStoreSchema(
FOR UPDATE`,
), "PostgreSQL store metadata");
if (
- metadata === null || metadata.schema_version !== POSTGRESQL_SCHEMA_VERSION ||
- metadata.store_identity !== storeIdentity
+ metadata === null || metadata.store_identity !== storeIdentity ||
+ (metadata.schema_version !== POSTGRESQL_SCHEMA_VERSION &&
+ metadata.schema_version !== FULL_PROJECTION_SCHEMA_VERSION)
) {
throw new AuthorityStoreProtocolError(
"PostgreSQL store identity does not match the installed database incarnation",
);
}
+ if (metadata.schema_version === FULL_PROJECTION_SCHEMA_VERSION) {
+ // The released v0 schema required every projection. A nullable v0
+ // journal is the unsupported pre-release format; do not bless it as v1.
+ const releasedLayout = oneRow(await connection.query(
+ `SELECT attnotnull FROM pg_attribute
+ WHERE attrelid = 'loopx_control_plane.authority_commits'::regclass
+ AND attname = 'projection' AND NOT attisdropped`,
+ ), "PostgreSQL v0 projection layout");
+ if (releasedLayout?.attnotnull !== true) {
+ throw new AuthorityStoreProtocolError("PostgreSQL v0 journal layout is not the released full-projection format");
+ }
+ // The checkpoint is part of the same append-only journal. Preserve
+ // table grants, including group grants, without copying history
+ // mutation privileges or grant options.
+ await connection.query(`
+ DO $migration$
+ DECLARE journal_grant record;
+ BEGIN
+ FOR journal_grant IN
+ SELECT DISTINCT acl.grantee, acl.privilege_type
+ FROM pg_catalog.pg_class AS journal
+ CROSS JOIN LATERAL pg_catalog.aclexplode(journal.relacl) AS acl
+ WHERE journal.oid = 'loopx_control_plane.authority_commits'::regclass
+ AND acl.privilege_type IN ('SELECT', 'INSERT')
+ LOOP
+ EXECUTE pg_catalog.format(
+ 'GRANT %s ON loopx_control_plane.authority_checkpoints TO %s',
+ journal_grant.privilege_type,
+ CASE WHEN journal_grant.grantee = 0 THEN 'PUBLIC'
+ ELSE pg_catalog.quote_ident(pg_catalog.pg_get_userbyid(journal_grant.grantee))
+ END
+ );
+ END LOOP;
+ END
+ $migration$;`);
+ }
+ await connection.query(
+ `ALTER TABLE loopx_control_plane.authority_commits ADD COLUMN IF NOT EXISTS delta jsonb;
+ ALTER TABLE loopx_control_plane.authority_commits ADD COLUMN IF NOT EXISTS projection_digest text;
+ ALTER TABLE loopx_control_plane.authority_commits ALTER COLUMN projection DROP NOT NULL;`,
+ );
+ if (runtimeRole !== undefined) {
+ await connection.query(`
+ DO $role$
+ BEGIN
+ IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = '${runtimeRole}') THEN
+ CREATE ROLE "${runtimeRole}" NOLOGIN;
+ END IF;
+ END
+ $role$;
+ GRANT USAGE ON SCHEMA loopx_control_plane TO "${runtimeRole}";
+ GRANT SELECT ON loopx_control_plane.authority_store_metadata TO "${runtimeRole}";
+ GRANT SELECT, INSERT, UPDATE ON loopx_control_plane.authority_heads TO "${runtimeRole}";
+ GRANT SELECT, INSERT ON
+ loopx_control_plane.authority_checkpoints,
+ loopx_control_plane.authority_commits,
+ loopx_control_plane.authority_events,
+ loopx_control_plane.authority_receipts TO "${runtimeRole}";`);
+ }
+ if (metadata.schema_version === FULL_PROJECTION_SCHEMA_VERSION) {
+ await connection.query(
+ "UPDATE loopx_control_plane.authority_store_metadata SET schema_version = $1 WHERE singleton = TRUE",
+ [POSTGRESQL_SCHEMA_VERSION],
+ );
+ }
commitStarted = true;
await connection.query("COMMIT");
} catch (error) {
@@ -562,6 +678,7 @@ export class PostgreSqlAuthorityStore implements AuthorityStore {
readonly tenantId: string;
readonly goalId: string;
readonly maxCommitBytes: number;
+ readonly #expectedStoreIdentity: string | undefined;
constructor(
database: PostgreSqlAuthorityDatabase,
@@ -570,6 +687,11 @@ export class PostgreSqlAuthorityStore implements AuthorityStore {
this.database = database;
this.tenantId = requireAuthorityStoreId(options.tenant_id, "tenant id");
this.goalId = requireAuthorityStoreId(options.goal_id, "goal id");
+ this.#expectedStoreIdentity = options.expected_store_identity;
+ if (this.#expectedStoreIdentity !== undefined &&
+ !POSTGRESQL_STORE_IDENTITY_PATTERN.test(this.#expectedStoreIdentity)) {
+ throw new AuthorityStoreProtocolError("expected PostgreSQL store identity is invalid");
+ }
this.maxCommitBytes = options.max_commit_bytes ?? DEFAULT_POSTGRESQL_MAX_COMMIT_BYTES;
if (!Number.isSafeInteger(this.maxCommitBytes) || this.maxCommitBytes < 1) {
throw new AuthorityStoreProtocolError(
@@ -582,6 +704,14 @@ export class PostgreSqlAuthorityStore implements AuthorityStore {
return await this.database.connect();
}
+ private async readStoreIdentity(connection: PostgreSqlAuthorityConnection): Promise {
+ const identity = await requireStoreIdentity(connection);
+ if (this.#expectedStoreIdentity !== undefined && identity !== this.#expectedStoreIdentity) {
+ throw new AuthorityStoreProtocolError("PostgreSQL store identity does not match the expected database incarnation");
+ }
+ return identity;
+ }
+
private async readInTenantTransaction(
operation: (connection: PostgreSqlAuthorityConnection) => Promise,
): Promise {
@@ -618,7 +748,7 @@ export class PostgreSqlAuthorityStore implements AuthorityStore {
let releaseError: Error | undefined;
try {
connection = await this.connect();
- return { status: "available", store_identity: await requireStoreIdentity(connection) };
+ return { status: "available", store_identity: await this.readStoreIdentity(connection) };
} catch (error) {
releaseError = asError(error);
const failure = readFailure(error);
@@ -633,7 +763,7 @@ export class PostgreSqlAuthorityStore implements AuthorityStore {
async loadAuthority(): Promise {
try {
return await this.readInTenantTransaction(async (connection) => {
- const storeIdentity = await requireStoreIdentity(connection);
+ const storeIdentity = await this.readStoreIdentity(connection);
const value = oneRow(
await connection.query(SELECT_HEAD_SQL, [this.tenantId, this.goalId]),
"PostgreSQL authority head",
@@ -694,7 +824,7 @@ export class PostgreSqlAuthorityStore implements AuthorityStore {
// Keep the identity lock ahead of the per-Goal head lock. Shared mode
// preserves cross-Goal concurrency while rotation takes exclusive mode.
await lockStoreIdentityTransaction(connection, "shared");
- const storeIdentity = await requireStoreIdentity(connection);
+ const storeIdentity = await this.readStoreIdentity(connection);
await connection.query(
`INSERT INTO loopx_control_plane.authority_heads
(tenant_id, goal_id, provider_revision, cursor, head)
@@ -746,17 +876,40 @@ export class PostgreSqlAuthorityStore implements AuthorityStore {
const nextRevision = (BigInt(current.provider_revision) + 1n).toString();
const nextCursor = (BigInt(current.cursor) + 1n).toString();
+ const isCheckpoint = isAuthorityStateCheckpoint(BigInt(nextCursor));
+ const projectionDigest = canonicalAuthoritySha256(normalized.next_projection);
+
+ if (isCheckpoint) {
+ await connection.query(
+ `INSERT INTO loopx_control_plane.authority_checkpoints
+ (tenant_id, goal_id, cursor, projection, projection_digest)
+ VALUES ($1, $2, $3::bigint, $4::jsonb, $5)`,
+ [
+ this.tenantId,
+ this.goalId,
+ nextCursor,
+ JSON.stringify(normalized.next_projection),
+ projectionDigest,
+ ],
+ );
+ }
+
+ const delta = isCheckpoint
+ ? null
+ : JSON.stringify(authorityStateDelta(current.head ?? {}, normalized.next_projection));
+
await connection.query(
`INSERT INTO loopx_control_plane.authority_commits
- (tenant_id, goal_id, cursor, provider_revision, operation_id, projection)
- VALUES ($1, $2, $3::bigint, $4::bigint, $5, $6::jsonb)`,
+ (tenant_id, goal_id, cursor, provider_revision, operation_id, delta, projection_digest)
+ VALUES ($1, $2, $3::bigint, $4::bigint, $5, $6::jsonb, $7)`,
[
this.tenantId,
this.goalId,
nextCursor,
nextRevision,
normalized.operation_id,
- JSON.stringify(normalized.next_projection),
+ delta,
+ projectionDigest,
],
);
for (const [index, event] of normalized.events.entries()) {
@@ -843,23 +996,17 @@ export class PostgreSqlAuthorityStore implements AuthorityStore {
}
try {
return await this.readInTenantTransaction(async (connection) => {
- const storeIdentity = await requireStoreIdentity(connection);
+ const storeIdentity = await this.readStoreIdentity(connection);
const value = oneRow(await connection.query(
- `${SELECT_TRANSACTION_COLUMNS_SQL}
+ `${SELECT_RECEIPT_COLUMNS_SQL}
WHERE commit.tenant_id = $1 AND commit.goal_id = $2 AND commit.operation_id = $3`,
[this.tenantId, this.goalId, normalized],
), "PostgreSQL authority receipt");
if (value === null) return {status: "snapshot", store_identity: storeIdentity,
receipt: {status: "missing"}} as const;
- const transaction = decodeTransactionRow(value);
return {
status: "snapshot", store_identity: storeIdentity,
- receipt: {
- status: "found",
- cursor: transaction.cursor,
- provider_revision: providerRevisionToken(storeIdentity, transaction.provider_revision),
- receipts: structuredClone(transaction.receipts),
- },
+ receipt: decodeReceipt(value, storeIdentity),
} as const;
});
} catch (error) {
@@ -867,6 +1014,30 @@ export class PostgreSqlAuthorityStore implements AuthorityStore {
}
}
+ async readReceipts(operationIds: readonly string[]): Promise {
+ try {
+ if (!Array.isArray(operationIds) || operationIds.length < 1 || operationIds.length > 64) {
+ throw new AuthorityStoreProtocolError("receipt batch requires 1..64 operations");
+ }
+ for (const id of operationIds) requireAuthorityStoreId(id, "operation id");
+ return await this.readInTenantTransaction(async connection => {
+ const identity = await this.readStoreIdentity(connection);
+ const values = rows(await connection.query(
+ `${SELECT_RECEIPT_COLUMNS_SQL}
+ WHERE commit.tenant_id = $1 AND commit.goal_id = $2 AND commit.operation_id = ANY($3::text[])`,
+ [this.tenantId, this.goalId, [...new Set(operationIds)]],
+ ));
+ const byOperation = new Map(values.map(row => [
+ requiredRowString(row, "operation_id"), decodeReceipt(row, identity),
+ ]));
+ return {status: "receipts", results: operationIds.map(id =>
+ structuredClone(byOperation.get(id) ?? {status: "missing"}))};
+ });
+ } catch (error) {
+ return readFailure(error);
+ }
+ }
+
async scanCommitted(
afterCursor: string | null,
limit: number,
@@ -875,26 +1046,99 @@ export class PostgreSqlAuthorityStore implements AuthorityStore {
if (!(scan instanceof AuthorityJournalScan)) return scan;
try {
return await this.readInTenantTransaction(async (connection) => {
- const storeIdentity = await requireStoreIdentity(connection);
+ const storeIdentity = await this.readStoreIdentity(connection);
const current = oneRow(
await connection.query(SELECT_HEAD_SQL, [this.tenantId, this.goalId]),
"PostgreSQL authority head",
);
if (current === null) return scan.page([], null);
const head = decodeHeadRow(current);
- const snapshot = head.head === null ? null : {cursor: head.cursor,
- provider_revision: providerRevisionToken(storeIdentity, head.provider_revision), head: head.head};
+ const snapshot = head.head === null ? null : {
+ cursor: head.cursor,
+ provider_revision: providerRevisionToken(storeIdentity, head.provider_revision),
+ head: head.head,
+ };
const range = scan.rangeFailure(snapshot?.cursor ?? null);
if (range) return range;
- const result = rows(await connection.query(
- `${SELECT_TRANSACTION_COLUMNS_SQL}
- WHERE commit.tenant_id = $1 AND commit.goal_id = $2 AND commit.cursor > $3::bigint
- ORDER BY commit.cursor
- LIMIT $4`,
- [this.tenantId, this.goalId, scan.offset.toString(), (BigInt(limit) + 1n).toString()],
- )).map(decodeTransactionRow);
- const transactions = result.map(value => ({...value,
- provider_revision: providerRevisionToken(storeIdentity, value.provider_revision)}));
+
+ const headCursorBigInt = BigInt(head.cursor);
+ const fromCursor = scan.offset + 1n;
+ if (fromCursor > headCursorBigInt) {
+ return scan.page([], snapshot);
+ }
+ const requestedCount = BigInt(limit) + 1n;
+ const remaining = headCursorBigInt - scan.offset;
+ const countToFetch = remaining < requestedCount ? remaining : requestedCount;
+ const toCursor = fromCursor + countToFetch - 1n;
+
+ // Fetch at most one checkpoint window before the visible page. Validate
+ // that prefix too: a missing row could otherwise silently change replay.
+ const checkpointCursor = authorityStateCheckpointCursor(fromCursor);
+ const rawCommits = rows(
+ await connection.query(
+ `${SELECT_TRANSACTION_COLUMNS_SQL}
+ WHERE commit.tenant_id = $1 AND commit.goal_id = $2
+ AND commit.cursor >= $3::bigint AND commit.cursor <= $4::bigint
+ ORDER BY commit.cursor`,
+ [this.tenantId, this.goalId, checkpointCursor.toString(), toCursor.toString()],
+ ),
+ );
+
+ if (BigInt(rawCommits.length) !== toCursor - checkpointCursor + 1n) {
+ throw new AuthorityStoreProtocolError("PostgreSQL authority commit window is incomplete");
+ }
+
+ let replay: AuthorityStateReplay | null = null;
+ const transactions: AuthorityStoreCommittedTransaction[] = [];
+ for (const [index, raw] of rawCommits.entries()) {
+ const cursor = requiredRowString(raw, "cursor");
+ const rowCursor = parseAuthorityCursor(cursor);
+ if (rowCursor !== checkpointCursor + BigInt(index)) {
+ throw new AuthorityStoreProtocolError("PostgreSQL authority commit window is not contiguous");
+ }
+
+ const digest = raw.projection_digest;
+ if (raw.projection !== null) {
+ // Pre-upgrade rows retained the full projection. Their original
+ // operation, event and receipt identities are unchanged.
+ if (raw.delta !== null || digest !== null || raw.checkpoint_projection !== null) {
+ throw new AuthorityStoreProtocolError("PostgreSQL legacy commit has a conflicting replay record");
+ }
+ replay = new AuthorityStateReplay(raw.projection);
+ } else {
+ if (isAuthorityStateCheckpoint(rowCursor)) {
+ if (raw.checkpoint_projection === null || raw.delta !== null ||
+ digest !== raw.checkpoint_digest) {
+ throw new AuthorityStoreProtocolError("PostgreSQL commit has a missing or inconsistent checkpoint");
+ }
+ replay = new AuthorityStateReplay(raw.checkpoint_projection);
+ } else {
+ if (replay === null || raw.delta === null || raw.checkpoint_projection !== null) {
+ throw new AuthorityStoreProtocolError("PostgreSQL commit has a missing or inconsistent delta");
+ }
+ replay.apply(raw.delta);
+ }
+ if (typeof digest !== "string" || replay.stateDigest() !== digest) {
+ throw new AuthorityStoreProtocolError("PostgreSQL commit projection digest mismatch");
+ }
+ }
+
+ const providerRevision = providerRevisionToken(
+ storeIdentity, requiredRowString(raw, "provider_revision"),
+ );
+ const operationId = requireAuthorityStoreId(raw.operation_id, "PostgreSQL operation id");
+ if (rowCursor >= fromCursor) {
+ transactions.push({
+ cursor,
+ provider_revision: providerRevision,
+ operation_id: operationId,
+ projection: replay.snapshot(),
+ events: canonicalAuthorityObjectList(raw.events, "events"),
+ receipts: canonicalAuthorityObjectList(raw.receipts, "receipts"),
+ });
+ }
+ }
+
return scan.page(transactions, snapshot);
});
} catch (error) {
diff --git a/loopx/control_plane/coordination/remote_authority_contract.ts b/loopx/control_plane/coordination/remote_authority_contract.ts
index 638d89461a..d37ef5f7bb 100644
--- a/loopx/control_plane/coordination/remote_authority_contract.ts
+++ b/loopx/control_plane/coordination/remote_authority_contract.ts
@@ -1,16 +1,21 @@
/** Candidate wire contract. Clients submit intentions, never authority facts. */
import type {JsonObject} from "../effect_program.ts";
-import {canonicalAuthorityObject, canonicalAuthoritySha256, hasExactAuthorityKeys,
+import {canonicalAuthorityObject, canonicalAuthoritySha256,
requireAuthorityStoreId} from "./authority_store_codec.ts";
import {POSTGRESQL_STORE_IDENTITY_PATTERN} from "./postgresql_authority_store.ts";
import {taskLeaseOperationIdentity} from "../work_items/task_lease_operation_identity.ts";
import {normalizeTodoId, normalizeGoalId, normalizeOwner} from "../work_items/task_lease_acquire.ts";
import {BARE_SHA256_PATTERN} from "../content_digest.ts";
import {parseIsoTimestamp} from "../runtime_timestamp.ts";
+import {normalizeTodoResumeWhen, TODO_RESUME_NORMALIZE_REQUEST_SCHEMA_VERSION} from "../todos/resume_condition.ts";
+import {normalizeTodoPriority} from "../todos/priority.ts";
export const REMOTE_AUTHORITY_REQUEST = "loopx_remote_authority_request_v0";
export const REMOTE_AUTHORITY_RESPONSE = "loopx_remote_authority_response_v0";
-export const REMOTE_AUTHORITY_OPERATIONS = ["read", "claim", "progress", "renew", "release"] as const;
+export const REMOTE_AUTHORITY_OPERATIONS = [
+ "read", "claim", "progress", "renew", "release",
+ "complete", "block", "defer", "todo_add", "snapshot",
+] as const;
export type RemoteAuthorityOperation = typeof REMOTE_AUTHORITY_OPERATIONS[number];
export interface RemoteAuthorityScope {
tenant_id: string;
@@ -20,14 +25,32 @@ export interface RemoteAuthorityScope {
}
interface Base extends RemoteAuthorityScope {
schema_version: typeof REMOTE_AUTHORITY_REQUEST;
- todo_id: string;
+ todo_id?: string;
}
export type RemoteAuthorityRequest = Base & (
- | {operation: "read"}
- | {operation: "claim"; request_id: string}
- | {operation: "progress"; request_id: string; claim_request_id: string; note: string}
- | {operation: "renew"; idempotency_key: string; expected_version: number; ttl_seconds: number}
- | {operation: "release"; idempotency_key: string; expected_version: number}
+ | {operation: "read"; todo_id: string}
+ | {operation: "claim"; todo_id: string; request_id: string}
+ | {operation: "progress"; todo_id: string; request_id: string; claim_request_id: string; note: string}
+ | {operation: "renew"; todo_id: string; idempotency_key: string; expected_version: number; ttl_seconds: number}
+ | {operation: "release"; todo_id: string; idempotency_key: string; expected_version: number}
+ | {operation: "complete"; todo_id: string; request_id: string; claim_request_id: string; evidence: string; note?: string; no_followup?: boolean}
+ | {operation: "block"; todo_id: string; request_id: string; claim_request_id?: string; reason: string; resume_when?: string; note?: string}
+ | {operation: "defer"; todo_id: string; request_id: string; claim_request_id?: string; reason: string; resume_when: string; note?: string}
+ | {
+ operation: "todo_add";
+ todo_id: string;
+ request_id: string;
+ text: string;
+ role?: "user" | "agent";
+ priority?: string;
+ task_class?: string;
+ action_kind?: string;
+ task_repository?: string;
+ required_write_scopes?: readonly string[];
+ claimed_by?: string;
+ note?: string;
+ }
+ | {operation: "snapshot"; todo_id?: string}
);
export type RemoteAuthorityMode = "execute" | "recover";
export function remoteAuthorityId(value: unknown, name: string): string {
@@ -35,27 +58,104 @@ export function remoteAuthorityId(value: unknown, name: string): string {
if (id.length > 200 || /[\x00-\x20\x7f]/u.test(id)) throw new TypeError(`Invalid ${name}`);
return id;
}
+
+const MANDATORY_KEYS: Record = {
+ read: ["todo_id"],
+ claim: ["todo_id", "request_id"],
+ progress: ["todo_id", "request_id", "claim_request_id", "note"],
+ renew: ["todo_id", "ttl_seconds", "idempotency_key", "expected_version"],
+ release: ["todo_id", "idempotency_key", "expected_version"],
+ complete: ["todo_id", "request_id", "claim_request_id", "evidence"],
+ block: ["todo_id", "request_id", "reason"],
+ defer: ["todo_id", "request_id", "reason", "resume_when"],
+ todo_add: ["todo_id", "request_id", "text"],
+ snapshot: [],
+};
+const OPTIONAL_KEYS: Record = {
+ read: [],
+ claim: [],
+ progress: [],
+ renew: [],
+ release: [],
+ complete: ["note", "no_followup"],
+ block: ["claim_request_id", "resume_when", "note"],
+ defer: ["claim_request_id", "note"],
+ todo_add: ["role", "priority", "task_class", "action_kind", "task_repository", "required_write_scopes", "claimed_by", "note"],
+ snapshot: ["todo_id"],
+};
+
export function decodeRemoteAuthorityRequest(raw: unknown): RemoteAuthorityRequest {
const value = canonicalAuthorityObject(raw, "remote authority request");
- const keys = ["schema_version", "tenant_id", "goal_id", "actor_id", "store_identity", "todo_id", "operation"];
if (value.schema_version !== REMOTE_AUTHORITY_REQUEST ||
- !REMOTE_AUTHORITY_OPERATIONS.includes(value.operation as RemoteAuthorityOperation)) throw new TypeError("Invalid request schema or operation");
- switch (value.operation) {
- case "claim": keys.push("request_id"); break;
- case "progress": keys.push("request_id", "claim_request_id", "note"); break;
- case "renew": keys.push("ttl_seconds", "idempotency_key", "expected_version"); break;
- case "release": keys.push("idempotency_key", "expected_version"); break;
- }
- if (!hasExactAuthorityKeys(value, keys)) throw new TypeError("Unexpected or missing request fields");
- for (const key of ["tenant_id", "goal_id", "actor_id", "todo_id", "request_id", "claim_request_id", "idempotency_key"]) {
- if (key in value) remoteAuthorityId(value[key], key);
- }
- if (normalizeTodoId(value.todo_id) !== value.todo_id || normalizeGoalId(value.goal_id) !== value.goal_id ||
- normalizeOwner(value.actor_id) !== value.actor_id) throw new TypeError("Use canonical task, Goal and actor identities");
- if (typeof value.store_identity !== "string" || !POSTGRESQL_STORE_IDENTITY_PATTERN.test(value.store_identity)) throw new TypeError("Invalid store identity");
- if ("expected_version" in value && (!Number.isSafeInteger(value.expected_version) || Number(value.expected_version) < 0)) throw new TypeError("Invalid lease version");
- if ("ttl_seconds" in value && (!Number.isSafeInteger(value.ttl_seconds) || Number(value.ttl_seconds) < 1 || Number(value.ttl_seconds) > 3600)) throw new TypeError("Invalid lease duration");
- if (value.operation === "progress" && (typeof value.note !== "string" || !value.note.trim() || Buffer.byteLength(value.note) > 4096)) throw new TypeError("Progress requires a bounded note");
+ !REMOTE_AUTHORITY_OPERATIONS.includes(value.operation as RemoteAuthorityOperation)) {
+ throw new TypeError("Invalid request schema or operation");
+ }
+ const op = value.operation as RemoteAuthorityOperation;
+ const baseKeys = ["schema_version", "tenant_id", "goal_id", "actor_id", "store_identity", "operation"];
+ const required = [...baseKeys, ...MANDATORY_KEYS[op]];
+ for (const key of required) {
+ if (!Object.hasOwn(value, key)) throw new TypeError(`Unexpected or missing request fields: missing ${key}`);
+ }
+ const allowed = new Set([...required, ...OPTIONAL_KEYS[op]]);
+ for (const key of Object.keys(value)) {
+ if (!allowed.has(key)) throw new TypeError(`Unexpected or missing request fields: unexpected ${key}`);
+ }
+ for (const key of ["tenant_id", "goal_id", "actor_id", "todo_id", "request_id", "claim_request_id", "idempotency_key", "claimed_by"]) {
+ if (Object.hasOwn(value, key)) remoteAuthorityId(value[key], key);
+ }
+ if (typeof value.todo_id === "string" && normalizeTodoId(value.todo_id) !== value.todo_id) {
+ throw new TypeError("Use canonical task, Goal and actor identities");
+ }
+ if (normalizeGoalId(value.goal_id) !== value.goal_id || normalizeOwner(value.actor_id) !== value.actor_id) {
+ throw new TypeError("Use canonical task, Goal and actor identities");
+ }
+ if (typeof value.store_identity !== "string" || !POSTGRESQL_STORE_IDENTITY_PATTERN.test(value.store_identity)) {
+ throw new TypeError("Invalid store identity");
+ }
+ if ("expected_version" in value && (!Number.isSafeInteger(value.expected_version) || Number(value.expected_version) < 0)) {
+ throw new TypeError("Invalid lease version");
+ }
+ if ("ttl_seconds" in value && (!Number.isSafeInteger(value.ttl_seconds) || Number(value.ttl_seconds) < 1 || Number(value.ttl_seconds) > 3600)) {
+ throw new TypeError("Invalid lease duration");
+ }
+ if ("note" in value && (typeof value.note !== "string" || !value.note.trim() || Buffer.byteLength(value.note) > 4096)) {
+ throw new TypeError("Note requires a bounded non-empty string");
+ }
+ if ("no_followup" in value && typeof value.no_followup !== "boolean") {
+ throw new TypeError("no_followup must be a boolean");
+ }
+ if ("evidence" in value && (typeof value.evidence !== "string" || !value.evidence.trim() || Buffer.byteLength(value.evidence) > 16 * 1024)) {
+ throw new TypeError("Evidence requires a bounded non-empty string");
+ }
+ if ("reason" in value && (typeof value.reason !== "string" || !value.reason.trim() || Buffer.byteLength(value.reason) > 4096)) {
+ throw new TypeError("Reason requires a bounded non-empty string");
+ }
+ if ("resume_when" in value && (typeof value.resume_when !== "string" || Buffer.byteLength(value.resume_when) > 1024 ||
+ normalizeTodoResumeWhen({schema_version: TODO_RESUME_NORMALIZE_REQUEST_SCHEMA_VERSION, resume_when: value.resume_when}) === null)) {
+ throw new TypeError("A bounded structured Todo resume condition is required");
+ }
+ if ("text" in value && (typeof value.text !== "string" || !value.text.trim() || Buffer.byteLength(value.text) > 4096)) {
+ throw new TypeError("Text requires a bounded non-empty string");
+ }
+ if ("priority" in value && (typeof value.priority !== "string" || normalizeTodoPriority(value.priority) !== value.priority)) {
+ throw new TypeError("Invalid priority");
+ }
+ if ("role" in value && value.role !== "user" && value.role !== "agent") {
+ throw new TypeError("Invalid role");
+ }
+ if ("claimed_by" in value && normalizeOwner(value.claimed_by) !== value.claimed_by) {
+ throw new TypeError("Invalid claimed_by");
+ }
+ if ("required_write_scopes" in value) {
+ if (!Array.isArray(value.required_write_scopes) || value.required_write_scopes.some(s => typeof s !== "string" || !s.trim() || s.length > 256)) {
+ throw new TypeError("Invalid required_write_scopes");
+ }
+ }
+ for (const strField of ["task_class", "action_kind", "task_repository"]) {
+ if (strField in value && (typeof value[strField] !== "string" || !value[strField].trim() || value[strField].length > 256)) {
+ throw new TypeError(`Invalid ${strField}`);
+ }
+ }
return value as unknown as RemoteAuthorityRequest;
}
@@ -78,6 +178,13 @@ export function remoteAuthorityResponse(request: unknown, mode: RemoteAuthorityM
request_sha256: canonicalAuthoritySha256({mode, request})};
}
+export function remoteAuthoritySnapshotDigest(request: RemoteAuthorityScope, revision: unknown,
+ todos: readonly unknown[], leases: readonly unknown[]): string {
+ return canonicalAuthoritySha256({schema_version: "loopx_goal_snapshot_receipt_v0",
+ tenant_id: request.tenant_id, goal_id: request.goal_id, store_identity: request.store_identity,
+ provider_revision: revision, todos, leases});
+}
+
function responseRequires(condition: unknown): asserts condition {
if (!condition) throw new TypeError("Invalid remote authority response evidence");
}
@@ -112,6 +219,36 @@ function responseLease(raw: unknown, request: RemoteAuthorityRequest, mutation:
}
return lease;
}
+
+function responseTodo(raw: unknown, todoId?: string): string {
+ const todo = canonicalAuthorityObject(raw, "response Todo");
+ const id = normalizeTodoId(todo.todo_id);
+ responseRequires(todo.todo_id === id && (todoId === undefined || id === todoId));
+ remoteAuthorityId(todo.status, "Todo status");
+ if (todo.claimed_by != null) responseRequires(typeof todo.claimed_by === "string" && normalizeOwner(todo.claimed_by) === todo.claimed_by);
+ if (todo.note !== undefined) responseRequires(todo.note === null || typeof todo.note === "string");
+ if (todo.note_omitted !== undefined) responseRequires(todo.note_omitted === true && !Object.hasOwn(todo, "note"));
+ return id;
+}
+
+function responseSnapshot(value: JsonObject, request: Extract): void {
+ responseRequires(value.status === "snapshot" && value.goal_id === request.goal_id && value.store_identity === request.store_identity &&
+ Array.isArray(value.todos) && Array.isArray(value.leases));
+ responseRevision(value.provider_revision, request);
+ const todoIds = value.todos.map(todo => responseTodo(todo, request.todo_id));
+ const todoSet = new Set(todoIds);
+ responseRequires(todoSet.size === todoIds.length);
+ const leaseIds = new Set();
+ for (const raw of value.leases) {
+ const lease = responseLease(raw, request, false);
+ const id = normalizeTodoId(lease.todo_id);
+ responseRequires(lease.todo_id === id && lease.goal_id === request.goal_id && todoSet.has(id) &&
+ !leaseIds.has(id) && !Object.hasOwn(lease, "idempotency_key"));
+ leaseIds.add(id);
+ }
+ responseRequires(value.receipt_sha256 === remoteAuthoritySnapshotDigest(request, value.provider_revision, value.todos, value.leases));
+}
+
function responseCommand(raw: unknown, request: RemoteAuthorityRequest, operationId: string, historical: boolean): void {
const result = canonicalAuthorityObject(raw, "command response");
if (!historical) {
@@ -128,11 +265,25 @@ function responseCommand(raw: unknown, request: RemoteAuthorityRequest, operatio
}
responseRequires(typeof result.changed === "boolean");
if (result.operation_id !== undefined) responseRequires(result.operation_id === operationId);
- if (request.operation === "claim" || request.operation === "progress") responseRequires(result.todo_id === request.todo_id);
+ if (request.operation !== "renew" && request.operation !== "release") responseRequires(result.todo_id === request.todo_id);
if (request.operation === "claim") {
- responseRequires(result.claimed_by === request.actor_id && result.claim_operation_id === operationId);
+ responseRequires(result.claimed_by === request.actor_id && result.claim_operation_id === operationId &&
+ result.handoff_mode === "hard_lease");
+ responseLease(result.lease, request, true);
+ }
+ if (request.operation === "complete") {
+ responseRequires(typeof result.completed_at === "string" && parseIsoTimestamp(result.completed_at) !== null);
+ remoteAuthorityId(result.completion_receipt_id, "completion receipt id");
}
- if (request.operation !== "progress") {
+ if (request.operation === "todo_add") responseTodo(result.todo, request.todo_id);
+ if ((request.operation === "block" || request.operation === "defer") && result.changed === true) {
+ const transition = canonicalAuthorityObject(request.operation === "block"
+ ? result.blocked_lifecycle_transition : result.deferred_transition, "Todo lifecycle transition");
+ responseRequires(transition.kind === (request.operation === "block" ? "todo_blocked" : "todo_deferred") &&
+ transition.execution_authority_granted === false &&
+ ["absent", "released", "already_released"].some(state => transition.lease_retirement === state));
+ }
+ if (request.operation === "renew" || request.operation === "release") {
responseRequires(result.handoff_mode === "hard_lease");
if (request.operation === "release" && result.missing === true) {
responseRequires(request.expected_version === 0 && result.changed === false &&
@@ -155,8 +306,8 @@ export function decodeRemoteAuthorityResponse(raw: unknown, request: RemoteAutho
responseRequires(typeof value.status === "string");
if (["rejected", "unavailable", "ambiguous"].includes(value.status)) {
remoteAuthorityId(value.reason_code, "response reason");
- responseRequires(value.status !== "ambiguous" || mode === "execute" && request.operation !== "read");
- } else if (mode === "recover" && request.operation !== "read") {
+ responseRequires(value.status !== "ambiguous" || mode === "execute" && request.operation !== "read" && request.operation !== "snapshot");
+ } else if (mode === "recover" && request.operation !== "read" && request.operation !== "snapshot") {
const id = responseOperationId(value.operation_id, request);
if (value.status !== "missing") {
responseRequires(value.status === "found" && value.historical_only === true &&
@@ -169,16 +320,13 @@ export function decodeRemoteAuthorityResponse(raw: unknown, request: RemoteAutho
if (value.status !== "missing") {
responseRequires(value.status === "found");
responseRevision(value.provider_revision, request);
- const todo = canonicalAuthorityObject(value.todo, "read Todo");
- responseRequires(todo.todo_id === request.todo_id);
- remoteAuthorityId(todo.status, "Todo status");
- if (todo.claimed_by != null) responseRequires(typeof todo.claimed_by === "string" && normalizeOwner(todo.claimed_by) === todo.claimed_by);
- if (todo.note !== undefined) responseRequires(todo.note === null || typeof todo.note === "string");
- if (todo.note_omitted !== undefined) responseRequires(todo.note_omitted === true && !Object.hasOwn(todo, "note"));
+ responseTodo(value.todo, request.todo_id);
if (value.lease !== null) responseLease(value.lease, request, false);
}
+ } else if (mode === "execute" && request.operation === "snapshot") {
+ responseSnapshot(value, request);
} else {
- responseRequires(mode === "execute" && request.operation !== "read" && value.status === "result");
+ responseRequires(mode === "execute" && request.operation !== "read" && request.operation !== "snapshot" && value.status === "result");
responseCommand(value.result, request, responseOperationId(value.operation_id, request), false);
}
return value;
diff --git a/loopx/control_plane/coordination/remote_authority_gateway.py b/loopx/control_plane/coordination/remote_authority_gateway.py
new file mode 100644
index 0000000000..bc15e6e7b6
--- /dev/null
+++ b/loopx/control_plane/coordination/remote_authority_gateway.py
@@ -0,0 +1,77 @@
+"""Private configuration and read-only observation for the remote authority candidate."""
+
+from __future__ import annotations
+
+import json
+import os
+import subprocess
+from collections.abc import Mapping
+from pathlib import Path
+from typing import Any
+
+REMOTE_AUTHORITY_GATEWAY_SCHEMA = "loopx_remote_authority_gateway_v0"
+_REQUIRED_BINDING = ("endpoint", "tenant_id", "store_identity", "actor_id", "probe_todo_id")
+
+
+def remote_authority_config(
+ goal: Mapping[str, Any] | None,
+) -> Mapping[str, Any] | None:
+ """Opt in only through the selected registered Goal, never an environment default."""
+ cfg = goal.get("remote_authority") if goal is not None else None
+ return cfg if isinstance(cfg, Mapping) and cfg.get("enabled") is True else None
+
+
+def _binding_complete(cfg: Mapping[str, Any]) -> bool:
+ return all(isinstance(cfg.get(key), str) and bool(cfg[key].strip()) for key in _REQUIRED_BINDING)
+
+
+def get_remote_authority_projection(goal_id: str, cfg: Mapping[str, Any]) -> dict[str, Any]:
+ """Describe configuration without portraying it as connectivity or exposing its binding."""
+ return {
+ "schema_version": REMOTE_AUTHORITY_GATEWAY_SCHEMA,
+ "goal_id": goal_id,
+ "enabled": True,
+ "handoff_mode": "hard_lease",
+ "access": "opt_in_authenticated_only",
+ "status": "unverified" if _binding_complete(cfg) else "unconfigured",
+ }
+
+
+def check_remote_authority_gateway_status(
+ goal_id: str,
+ agent_id: str | None,
+ cfg: Mapping[str, Any],
+) -> dict[str, Any]:
+ """Read one known remote Todo through the existing TLS client; return no remote content."""
+ projection = get_remote_authority_projection(goal_id, cfg)
+ if projection["status"] == "unconfigured":
+ return {**projection, "reason_code": "remote_authority_binding_incomplete"}
+ if agent_id != cfg["actor_id"]:
+ return {**projection, "status": "unavailable", "reason_code": "remote_authority_actor_mismatch"}
+ credential = os.environ.get("LOOPX_AUTHORITY_CREDENTIAL") or os.environ.get("LOOPX_REMOTE_AUTHORITY_CREDENTIAL")
+ if not credential:
+ return {**projection, "status": "unavailable", "reason_code": "remote_authority_credential_required"}
+ from ..effect_runtime import EffectRuntimeStartupError, _node_executable
+
+ try:
+ # The user-owned registry supplies the origin and optional private CA.
+ # Pass the credential and binding through the private pipe, not argv.
+ bridge = Path(__file__).with_name("remote_authority_probe.ts")
+ completed = subprocess.run(
+ [_node_executable(), "--no-warnings", "--experimental-strip-types", str(bridge)],
+ input=json.dumps({
+ **{key: cfg[key] for key in _REQUIRED_BINDING},
+ "goal_id": goal_id, "credential": credential, "ca_file": cfg.get("ca_file"),
+ }),
+ env={key: os.environ[key] for key in ("PATH", "SystemRoot", "WINDIR", "TMPDIR", "TEMP")
+ if key in os.environ},
+ text=True,
+ capture_output=True,
+ check=True,
+ timeout=5,
+ )
+ if completed.stdout == "verified\n":
+ return {**projection, "status": "verified"}
+ except (EffectRuntimeStartupError, OSError, subprocess.SubprocessError):
+ pass
+ return {**projection, "status": "unavailable", "reason_code": "remote_authority_read_unverified"}
diff --git a/loopx/control_plane/coordination/remote_authority_http.ts b/loopx/control_plane/coordination/remote_authority_http.ts
index 20e8e50c1e..55aca59b90 100644
--- a/loopx/control_plane/coordination/remote_authority_http.ts
+++ b/loopx/control_plane/coordination/remote_authority_http.ts
@@ -49,7 +49,7 @@ export function createRemoteAuthorityServer(service: RemoteAuthorityService,
const encoded = JSON.stringify(result);
if (Buffer.byteLength(encoded) > MAX_RESPONSE_BYTES) {
response.end(JSON.stringify(remoteAuthorityResponse(decoded, mode, {status:
- mode === "recover" || decoded.operation === "read" ? "unavailable" : "ambiguous",
+ mode === "recover" || decoded.operation === "read" || decoded.operation === "snapshot" ? "unavailable" : "ambiguous",
reason_code: "remote_authority_response_too_large"})));
} else response.end(encoded);
} catch {
@@ -102,7 +102,7 @@ export async function callRemoteAuthority(endpoint: string, credential: string,
const timeout = options.timeoutMs ?? 15000;
if (!Number.isSafeInteger(timeout) || timeout < 1 || timeout > 120000) throw new TypeError("Invalid request deadline");
const uncertain = () => remoteAuthorityResponse(request, mode, {status:
- mode === "recover" || request.operation === "read" ? "unavailable" : "ambiguous",
+ mode === "recover" || request.operation === "read" || request.operation === "snapshot" ? "unavailable" : "ambiguous",
reason_code: "remote_authority_transport_uncertain"});
return new Promise(resolve => {
const body = JSON.stringify(request);
diff --git a/loopx/control_plane/coordination/remote_authority_probe.ts b/loopx/control_plane/coordination/remote_authority_probe.ts
new file mode 100644
index 0000000000..a21846231e
--- /dev/null
+++ b/loopx/control_plane/coordination/remote_authority_probe.ts
@@ -0,0 +1,40 @@
+/** Private stdin bridge to the same scoped TLS client used by task commands. */
+import {open} from "node:fs/promises";
+import {isAbsolute} from "node:path";
+import {canonicalAuthorityObject} from "./authority_store_codec.ts";
+import {decodeRemoteAuthorityRequest, REMOTE_AUTHORITY_REQUEST} from "./remote_authority_contract.ts";
+import {callRemoteAuthority} from "./remote_authority_http.ts";
+
+async function readCa(path: unknown): Promise {
+ if (path === null || path === undefined) return undefined;
+ if (typeof path !== "string" || !isAbsolute(path)) throw new TypeError("Invalid CA path");
+ const file = await open(path, "r");
+ try {
+ const info = await file.stat();
+ if (!info.isFile() || info.size === 0 || info.size > 64 * 1024) throw new TypeError("Invalid CA file");
+ const bytes = Buffer.alloc(info.size);
+ if ((await file.read(bytes, 0, bytes.length, 0)).bytesRead !== bytes.length) throw new TypeError("Incomplete CA file");
+ return bytes;
+ } finally {await file.close();}
+}
+
+try {
+ let input = "";
+ process.stdin.setEncoding("utf8");
+ for await (const chunk of process.stdin) {
+ input += chunk;
+ if (Buffer.byteLength(input) > 16 * 1024) throw new TypeError("Invalid probe input");
+ }
+ const cfg = canonicalAuthorityObject(JSON.parse(input), "remote authority probe");
+ if (typeof cfg.endpoint !== "string" || typeof cfg.credential !== "string") throw new TypeError("Invalid probe binding");
+ const request = decodeRemoteAuthorityRequest({
+ schema_version: REMOTE_AUTHORITY_REQUEST, operation: "read",
+ tenant_id: cfg.tenant_id, goal_id: cfg.goal_id, actor_id: cfg.actor_id,
+ store_identity: cfg.store_identity, todo_id: cfg.probe_todo_id,
+ });
+ const response = await callRemoteAuthority(cfg.endpoint, cfg.credential, request, {ca: await readCa(cfg.ca_file), timeoutMs: 3000});
+ // A missing Todo is not proof of usable access to the expected authority.
+ process.stdout.write(response.status === "found" ? "verified\n" : "unavailable\n");
+} catch {
+ process.stdout.write("unavailable\n");
+}
diff --git a/loopx/control_plane/coordination/remote_authority_service.ts b/loopx/control_plane/coordination/remote_authority_service.ts
index 7a490e0919..75f045e7e0 100644
--- a/loopx/control_plane/coordination/remote_authority_service.ts
+++ b/loopx/control_plane/coordination/remote_authority_service.ts
@@ -6,10 +6,13 @@ import {PostgreSqlAuthorityService} from "./postgresql_authority_service.ts";
import type {PostgreSqlAuthorityDatabase} from "./postgresql_authority_store.ts";
import {executeCoordinationTodoClaim} from "./todo_claim.ts";
import {executeCoordinationTodoUpdate} from "./todo_update.ts";
+import {executeCoordinationTodoCreate} from "./todo_create.ts";
+import {executeCoordinationTodoTerminalLifecycle, type CoordinationTodoTerminalLifecycleInput} from "./todo_terminal_lifecycle.ts";
import {executeCanonicalTaskLeaseLifecycle} from "./task_lease_lifecycle.ts";
import {leaseVersion, leaseEpoch} from "../work_items/task_lease_acquire.ts";
+import {TODO_DOMAIN_ITEM_SCHEMA} from "./coordination_state_contract.ts";
import {indexCoordinationProjection, validateCoordinationTodoReadModel} from "./coordination_projection.ts";
-import {decodeRemoteAuthorityRequest, remoteAuthorityOperationId, remoteAuthorityResponse,
+import {decodeRemoteAuthorityRequest, remoteAuthorityOperationId, remoteAuthorityResponse, remoteAuthoritySnapshotDigest,
type RemoteAuthorityMode, type RemoteAuthorityRequest} from "./remote_authority_contract.ts";
export type RemoteAuthorityAdmission =
@@ -68,6 +71,10 @@ function pinnedStore(store: AuthorityStore, identity: string, admission: JsonObj
if (commit.operation_id !== operationId || commit.expected_provider_revision === null ||
!matches(commit.expected_provider_revision)) return {status: "failed",
reason_code: "remote_authority_commit_binding_invalid", reason: "Command does not match admitted authority"};
+ if (commit.next_projection.handoff_mode !== "hard_lease") return {
+ status: "failed", reason_code: "remote_authority_requires_hard_lease",
+ reason: "The committing authority must use hard leases",
+ };
// Metadata shares the existing business receipt transaction; no second ledger.
return store.commitAuthority({...commit, receipts: commit.receipts.map(receipt =>
({...receipt, remote_admission: admission}))});
@@ -75,12 +82,23 @@ function pinnedStore(store: AuthorityStore, identity: string, admission: JsonObj
};
}
+function projectReadTodo(todo: JsonObject, includePlanning: boolean): JsonObject {
+ const fields = ["todo_id", "status", "claimed_by", "note", ...(includePlanning ? ["role", "priority"] : [])];
+ const output = Object.fromEntries(fields.filter(field => Object.hasOwn(todo, field)).map(field => [field, todo[field]]));
+ if (Object.hasOwn(output, "note") && Buffer.byteLength(JSON.stringify(output.note)) > MAX_READ_NOTE_BYTES) {
+ delete output.note;
+ output.note_omitted = true;
+ }
+ return output;
+}
+
/** Deliberately bounded response projection: no SQL errors, paths, arbitrary
* canonical fields, workspace observations or provider credentials cross here. */
function commandResult(result: JsonObject): JsonObject {
const output: JsonObject = {};
for (const key of ["status", "reason_code", "changed", "todo_id", "claimed_by", "operation_id",
- "claim_operation_id", "provider_revision", "cursor", "handoff_mode", "released", "renewed", "missing"]) {
+ "claim_operation_id", "provider_revision", "cursor", "handoff_mode", "released", "renewed", "missing",
+ "completed", "completed_at", "completion_receipt_id"]) {
if (Object.hasOwn(result, key)) output[key] = result[key];
}
if (result.lease && typeof result.lease === "object" && !Array.isArray(result.lease)) {
@@ -89,6 +107,18 @@ function commandResult(result: JsonObject): JsonObject {
"acquired_at", "updated_at", "expires_at"].filter(k => k in lease).map(k => [k, lease[k]])),
version: leaseVersion(lease), lease_epoch: leaseEpoch(lease)};
}
+ if (result.todo && typeof result.todo === "object" && !Array.isArray(result.todo)) {
+ const todo = result.todo as JsonObject;
+ output.todo = Object.fromEntries(["todo_id", "status", "role", "priority", "claimed_by", "note"].filter(k => k in todo).map(k => [k, todo[k]]));
+ }
+ for (const key of ["blocked_lifecycle_transition", "deferred_transition"]) {
+ const raw = result[key];
+ if (raw && typeof raw === "object" && !Array.isArray(raw)) {
+ const transition = raw as JsonObject;
+ output[key] = Object.fromEntries(["kind", "lease_retirement", "execution_authority_granted"]
+ .filter(field => Object.hasOwn(transition, field)).map(field => [field, transition[field]]));
+ }
+ }
return output;
}
@@ -101,7 +131,7 @@ export class RemoteAuthorityService {
try { request = decodeRemoteAuthorityRequest(raw); }
catch { return {status: "rejected", reason_code: "invalid_remote_authority_request"}; }
const reply = (payload: JsonObject) => remoteAuthorityResponse(request, mode, payload);
- if (mode !== "execute" && mode !== "recover" || mode === "recover" && request.operation === "read") {
+ if (mode !== "execute" && mode !== "recover" || mode === "recover" && (request.operation === "read" || request.operation === "snapshot")) {
return reply({status: "rejected", reason_code: "invalid_remote_authority_mode"});
}
let admission: RemoteAuthorityAdmission;
@@ -125,7 +155,7 @@ export class RemoteAuthorityService {
const store = pinnedStore(opened.store, request.store_identity, binding, operationId);
let dispatched = false;
try {
- const previous = request.operation === "read" ? null : await store.readReceipt(operationId);
+ const previous = (request.operation === "read" || request.operation === "snapshot") ? null : await store.readReceipt(operationId);
if (previous?.status === "found") {
const original = previous.receipts[0];
const recorded = original?.remote_admission as JsonObject | undefined;
@@ -150,17 +180,35 @@ export class RemoteAuthorityService {
if (request.operation === "read") {
const todo = index.todos.get(request.todo_id), lease = index.leases.get(request.todo_id);
if (!todo) return reply({status: "missing"});
- const projectedTodo = Object.fromEntries(["todo_id", "status", "claimed_by", "note"].filter(k => k in todo).map(k => [k, todo[k]]));
- // Notes authored through other canonical routes can exceed the remote
- // write limit. Preserve the small lease facts needed for renew/release.
- if (Object.hasOwn(projectedTodo, "note") && Buffer.byteLength(JSON.stringify(projectedTodo.note)) > MAX_READ_NOTE_BYTES) {
- delete projectedTodo.note; projectedTodo.note_omitted = true;
- }
return reply({status: "found", provider_revision: head.provider_revision,
- todo: projectedTodo,
+ todo: projectReadTodo(todo, false),
lease: lease ? {...Object.fromEntries(["owner", "status", "expires_at"].filter(k => k in lease).map(k => [k, lease[k]])),
version: leaseVersion(lease), lease_epoch: leaseEpoch(lease)} : null});
}
+ if (request.operation === "snapshot") {
+ const todos = index.todo_ids
+ .filter(id => !request.todo_id || id === request.todo_id)
+ .map(id => projectReadTodo(index.todos.get(id)!, true));
+ const leases = index.lease_todo_ids
+ .filter(id => !request.todo_id || id === request.todo_id)
+ .map(id => {
+ const l = index.leases.get(id)!;
+ return {
+ ...Object.fromEntries(["goal_id", "todo_id", "owner", "status", "expires_at"].filter(k => k in l).map(k => [k, l[k]])),
+ version: leaseVersion(l),
+ lease_epoch: leaseEpoch(l),
+ };
+ });
+ return reply({
+ status: "snapshot",
+ provider_revision: head.provider_revision,
+ store_identity: request.store_identity,
+ goal_id: request.goal_id,
+ receipt_sha256: remoteAuthoritySnapshotDigest(request, head.provider_revision, todos, leases),
+ todos,
+ leases,
+ });
+ }
if (head.head.handoff_mode !== "hard_lease") return reply({status: "rejected", reason_code: "remote_authority_requires_hard_lease"});
const now = this.#dependencies.now?.() ?? new Date();
if (!Number.isFinite(now.valueOf())) return reply({status: "unavailable", reason_code: "remote_authority_clock_unavailable"});
@@ -175,7 +223,69 @@ export class RemoteAuthorityService {
operation_id: operationId, actor_agent_id: request.actor_id, expected_role: "agent", dry_run: false,
claim_operation_id: remoteAuthorityOperationId(request, admission.principal_id, request.claim_request_id),
patch: {note: request.note}, clear_fields: []});
- else result = await executeCanonicalTaskLeaseLifecycle(store, {...common, operation: request.operation,
+ else if (request.operation === "complete") {
+ const claimOpId = remoteAuthorityOperationId(request, admission.principal_id, request.claim_request_id);
+ const input: CoordinationTodoTerminalLifecycleInput = {
+ goal_id: request.goal_id, todo_id: request.todo_id, command: "complete",
+ expected_role: "agent", actor_agent_id: request.actor_id,
+ registered_agents: admission.registered_agents, lifecycle_grants: [], authority_reason: null,
+ decision_outcome: null, operation_identity: {kind: "explicit", operation_id: operationId},
+ claim_operation_id: claimOpId, lease_idempotency_key: null, lease_expected_version: null,
+ allow_user_gate_auto_acquire: false, requested_no_followup: request.no_followup === true,
+ requested_completion_turn_key: null, requested_completion_identity_source: null,
+ linked_successor_todo_ids: [], successor_intents: [], note: request.note ?? null,
+ evidence: request.evidence, reason: null, clear_claim: true, validation_declaration: null,
+ validation_receipt: null, completion_policy_request: null, dry_run: false, now,
+ };
+ result = await executeCoordinationTodoTerminalLifecycle(store, input);
+ // Older complete requests implied no_followup. Use that intent only to
+ // replay an already committed receipt for the exact authenticated request.
+ if (previous?.status === "found" && request.no_followup === undefined &&
+ result.reason_code === "coordination_operation_identity_mismatch") {
+ result = await executeCoordinationTodoTerminalLifecycle(store, {...input, requested_no_followup: true});
+ }
+ if (result.status === "applied") result.completed = true;
+ } else if (request.operation === "block" || request.operation === "defer") {
+ const claimOpId = request.claim_request_id
+ ? remoteAuthorityOperationId(request, admission.principal_id, request.claim_request_id)
+ : undefined;
+ result = await executeCoordinationTodoUpdate(store, {
+ ...common, operation_id: operationId, actor_agent_id: request.actor_id,
+ expected_role: "agent", dry_run: false, ...(claimOpId ? {claim_operation_id: claimOpId} : {}),
+ patch: {...(request.note ? {note: request.note} : {})}, clear_fields: [],
+ planning_intent: {
+ status: request.operation === "block" ? "blocked" : "deferred",
+ reason: request.reason,
+ ...(request.resume_when ? {resume_when: request.resume_when} : {}),
+ },
+ });
+ } else if (request.operation === "todo_add") {
+ const todoRecord = {
+ schema_version: TODO_DOMAIN_ITEM_SCHEMA,
+ todo_id: request.todo_id,
+ role: request.role ?? "agent",
+ status: "open",
+ done: false,
+ text: request.text,
+ archive_state: "active",
+ ...(request.priority ? {priority: request.priority} : {}),
+ ...(request.task_class ? {task_class: request.task_class} : {}),
+ ...(request.action_kind ? {action_kind: request.action_kind} : {}),
+ ...(request.task_repository ? {task_repository: request.task_repository} : {}),
+ ...(request.required_write_scopes ? {required_write_scopes: [...request.required_write_scopes]} : {}),
+ ...(request.claimed_by ? {claimed_by: request.claimed_by} : {}),
+ ...(request.note ? {note: request.note} : {}),
+ };
+ result = await executeCoordinationTodoCreate(store, {
+ goal_id: request.goal_id, todo: todoRecord, actor_agent_id: request.actor_id,
+ registered_agents: admission.registered_agents, operation_id: operationId, dry_run: false, now,
+ });
+ // The create owner can return a different, semantically duplicate
+ // Todo. That is a valid local no-op but not proof of this requested id.
+ if (result.status === "no_change" && result.todo_id !== request.todo_id) {
+ result = {status: "failed", reason_code: "remote_authority_todo_id_not_created"};
+ }
+ } else result = await executeCanonicalTaskLeaseLifecycle(store, {...common, operation: request.operation,
owner: request.actor_id, idempotency_key: request.idempotency_key, required_handoff_mode: "hard_lease",
expected_version: request.expected_version, ttl_seconds: request.operation === "renew" ? request.ttl_seconds : null});
return reply({status: "result", operation_id: operationId, result: commandResult(result)});
diff --git a/loopx/control_plane/effect_runtime_handlers.ts b/loopx/control_plane/effect_runtime_handlers.ts
index c4f9f80b2e..648fb9bbd8 100644
--- a/loopx/control_plane/effect_runtime_handlers.ts
+++ b/loopx/control_plane/effect_runtime_handlers.ts
@@ -363,6 +363,10 @@ export function createEffectRuntimeHandlers(
["goal.first_party_host_runtime.decide", lazyHandler(() => import("./goals/first_party_host_runtime.ts"), ({decideFirstPartyHostRuntime}) => decideFirstPartyHostRuntime)],
["goal.chat_session.lifecycle.decide", lazyHandler(() => import("./goals/chat_session_lifecycle.ts"), ({decideChatSessionLifecycle}) => decideChatSessionLifecycle)],
["goal.task.validation_plan", lazyHandler(() => import("./goals/task_validation_plan.ts"), ({canonicalTaskValidationPlan}) => canonicalTaskValidationPlan)],
+ ["goal.task.creation_selection", lazyHandler(() => import("./goals/task_validation_plan.ts"), ({originalCreationTaskSelection}) => originalCreationTaskSelection)],
+ ["goal.creation.execution.normalize", lazyHandler(() => import("./goals/creation_execution_review.ts"), ({normalizeGoalCreationExecution}) => normalizeGoalCreationExecution)],
+ ["goal.creation.execution_request.prepare", lazyHandler(() => import("./goals/creation_execution.ts"), ({prepareGoalCreationExecutionRequest}) => prepareGoalCreationExecutionRequest)],
+ ["goal.creation.execution_request", lazyHandler(() => import("./goals/creation_execution.ts"), ({projectGoalCreationExecutionRequest}) => projectGoalCreationExecutionRequest)],
["goal.acceptance.oracle.plan", lazyHandler(() => import("./goals/acceptance_oracle.ts"), ({planAcceptanceOracle}) => planAcceptanceOracle)],
["goal.acceptance.oracle.observe", lazyHandler(() => import("./goals/acceptance_oracle.ts"), ({observeAcceptanceOracles}) => observeAcceptanceOracles)],
["goal.acceptance.refine", lazyHandler(() => import("./goals/acceptance_authority.ts"), ({refineLocalGoalAcceptanceOutcome}) => refineLocalGoalAcceptanceOutcome)],
@@ -404,6 +408,20 @@ export function createEffectRuntimeHandlers(
["coordination.runtime_shadow.rollback", lazyHandler(() => Promise.all([import("./coordination/runtime_shadow.ts"), import("./coordination/source_transfer.ts")]), ([{rollbackCoordinationRuntimeShadow}, {withCoordinationSourceTransfer}]) => withCoordinationSourceTransfer("coordination.runtime_shadow.rollback", rollbackCoordinationRuntimeShadow))],
["coordination.local_authority.promote", lazyHandler(() => import("./coordination/local_authority_runtime.ts"), ({promoteLocalCoordinationAuthority}) => promoteLocalCoordinationAuthority)],
["coordination.authority_archive.manage", lazyHandler(() => import("./coordination/local_authority_archive.ts"), ({manageLocalAuthorityArchive}) => manageLocalAuthorityArchive)],
+ ["coordination.postgresql.setup", lazyHandler(() => import("./coordination/postgresql_authority_seed.ts"), ({setupPostgreSqlAuthoritySchemaAndRole}) => params => setupPostgreSqlAuthoritySchemaAndRole({
+ databaseUrl: requiredString(params.databaseUrl, "databaseUrl"),
+ storeIdentity: optionalString(params.storeIdentity, "storeIdentity") ?? undefined,
+ runtimeRole: optionalString(params.runtimeRole, "runtimeRole") ?? undefined,
+ }))],
+ ["coordination.postgresql.seed", lazyHandler(() => import("./coordination/postgresql_authority_seed.ts"), ({exportLocalToPgGenesisSeed}) => params => exportLocalToPgGenesisSeed({
+ runtimeRoot: requiredString(params.runtimeRoot, "runtimeRoot"),
+ goalId: requiredString(params.goalId, "goalId"),
+ tenantId: optionalString(params.tenantId, "tenantId") ?? undefined,
+ outputPath: optionalString(params.outputPath, "outputPath") ?? undefined,
+ databaseUrl: optionalString(params.databaseUrl, "databaseUrl") ?? undefined,
+ storeIdentity: optionalString(params.storeIdentity, "storeIdentity") ?? undefined,
+ execute: requireBoolean(params.execute, "execute"),
+ }))],
["configuration.backup", lazyHandler(() => import("./configuration_backup.ts"), ({configurationBackupOperation}) => configurationBackupOperation)],
["runtime.checkpoint_artifacts", lazyHandler(() => import("./runtime/checkpoint_artifacts.ts"), ({checkpointArtifacts}) => checkpointArtifacts)],
["state.recovery.archive.verify", lazyHandler(() => import("./state_recovery.ts"), ({verifyStateRecoveryArchive}) => verifyStateRecoveryArchive)],
@@ -618,6 +636,7 @@ export function createEffectRuntimeHandlers(
["collaboration.steward.managed_mode.executor", lazyHandler(() => import("./collaboration/steward_managed_mode.ts"), ({observeStewardNativeExecutor}) => observeStewardNativeExecutor)],
["collaboration.steward.managed_turn.admit", lazyHandler(() => import("./collaboration/steward_managed_mode.ts"), ({admitStewardManagedTurn}) => admitStewardManagedTurn)],
["collaboration.session.managed_turn.admit", lazyHandler(() => import("./collaboration/steward_managed_mode.ts"), ({admitManagedSessionTurn}) => admitManagedSessionTurn)],
+ ["collaboration.session.managed_turn.sandbox_policy", lazyHandler(() => import("./collaboration/steward_managed_mode.ts"), ({canonicalNativeTaskSandboxPolicy}) => canonicalNativeTaskSandboxPolicy)],
["collaboration.session.managed_identity", lazyHandler(() => import("./collaboration/steward_managed_mode.ts"), ({admitManagedSessionIdentity}) => admitManagedSessionIdentity)],
["collaboration.steward.monitor.plan", lazyHandler(() => import("./collaboration/steward_managed_mode.ts"), ({planStewardMonitor}) => planStewardMonitor)],
["collaboration.steward.report.plan", lazyHandler(() => import("./collaboration/steward_reports.ts"), ({planStewardReport}) => planStewardReport)],
diff --git a/loopx/control_plane/goals/creation_execution.ts b/loopx/control_plane/goals/creation_execution.ts
new file mode 100644
index 0000000000..c1bd0c3000
--- /dev/null
+++ b/loopx/control_plane/goals/creation_execution.ts
@@ -0,0 +1,119 @@
+/** Owner-confirmed Goal creation adapts to the existing original-Task queue.
+ * This request is not Task admission, checker adoption or a host write grant.
+ * Historical creations without explicit start_execution retain their behavior.
+ */
+import {createHash} from "node:crypto";
+import type {JsonObject} from "../runtime_decode.ts";
+import {jsonObject, requireJsonObject} from "../runtime_decode.ts";
+import {EffectRuntimeRequestError} from "../effect_runtime_errors.ts";
+import {goalCreationExecutionReview} from "./creation_execution_review.ts";
+import {canonicalAuthoritySha256} from "../coordination/authority_store_codec.ts";
+
+function verifyOriginalCreationSource(proposal: JsonObject): void {
+ const request: JsonObject = {};
+ for (const key of ["action_kind", "summary", "normalized_parameters", "context", "expected_state_fingerprint",
+ "permission_classification", "validation_evidence", "available_transitions"]) {
+ requireThat(Object.hasOwn(proposal, key), "original creation request source is incomplete");
+ request[key] = proposal[key];
+ }
+ for (const key of ["canonical_update_basis", "workspace_binding"]) {
+ if (Object.hasOwn(proposal, key)) request[key] = proposal[key];
+ }
+ requireThat(typeof proposal.request_digest === "string" && proposal.request_digest.length > 0
+ && canonicalAuthoritySha256(request) === proposal.request_digest,
+ "original creation request source changed");
+}
+
+function requireThat(condition: unknown, message: string): asserts condition {
+ if (!condition) throw new EffectRuntimeRequestError(message);
+}
+
+/** Prepare only after this same creation has retained its Goal, original Todo
+ * and Session. The action owner persists the returned value in its checkpoint.
+ */
+export function prepareGoalCreationExecutionRequest(params: JsonObject): JsonObject | null {
+ const proposal = requireJsonObject(params.proposal, "original Goal creation");
+ const parameters = requireJsonObject(proposal.normalized_parameters, "original creation parameters");
+ const review = goalCreationExecutionReview(parameters);
+ if (!review) return null;
+ verifyOriginalCreationSource(proposal);
+ const session = requireJsonObject(params.session, "original creation Session");
+ const goal = requireJsonObject(params.goal, "original registered Goal");
+ const selected = requireJsonObject(params.current_original_task, "current original Task selection");
+ const steps = requireJsonObject(requireJsonObject(proposal.checkpoint, "original creation checkpoint").steps,
+ "original creation steps");
+ const workspace = requireJsonObject(steps.workspace_validated, "original workspace checkpoint");
+ const bound = requireJsonObject(steps.goal_bootstrapped, "original Goal checkpoint");
+ const agent = requireJsonObject(steps.agent_bound, "original Agent checkpoint");
+ const todos = requireJsonObject(steps.todos_created, "original Task checkpoint");
+ const opened = requireJsonObject(steps.first_session_opened, "original Session checkpoint");
+ const proposalId = proposal.proposal_id, todoId = selected.todo_id;
+ requireThat(proposal.action_kind === "goal.create" && typeof proposalId === "string"
+ && proposalId.length > 0 && proposalId.length <= 80
+ && typeof proposal.expected_state_fingerprint === "string" && proposal.expected_state_fingerprint.length > 0
+ && ["applying", "applied"].includes(String(proposal.status))
+ && Object.hasOwn(goal, "goal_instance_id") && (goal.goal_instance_id === null
+ || typeof goal.goal_instance_id === "string" && goal.goal_instance_id.length > 0)
+ && goal.goal_id === parameters.goal_id && goal.creation_operation_id === proposalId
+ && Array.isArray(goal.registered_agents) && goal.registered_agents.includes(parameters.agent_id)
+ && typeof goal.workspace_path === "string" && goal.workspace_path.length > 0
+ && workspace.outcome === "workspace_validated"
+ && workspace.workspace_digest === createHash("sha256").update(JSON.stringify(goal.workspace_path)).digest("hex")
+ && bound.outcome === "goal_bootstrapped" && bound.goal_id === goal.goal_id
+ && agent.outcome === "agent_bound" && agent.goal_id === goal.goal_id && agent.agent_id === parameters.agent_id
+ && selected.reason === null && typeof todoId === "string" && todoId.length > 0
+ && todos.outcome === "todos_created" && Array.isArray(todos.todo_ids)
+ && todos.todo_ids.length === 1 && todos.todo_ids[0] === todoId
+ && Array.isArray(parameters.initial_todos) && parameters.initial_todos.length === 1
+ && session.goal_id === goal.goal_id && (session.goal_instance_id ?? null) === goal.goal_instance_id
+ && session.managed_agent_id === parameters.agent_id && session.agent_id === "codex"
+ && session.executor_endpoint_id === "codex" && session.session_mode === "managed_runtime"
+ && session.upstream_mode === "chat" && session.managed_workspace_path === goal.workspace_path
+ && typeof session.session_id === "string" && session.session_id.length > 0
+ && session.channel_id === `agent.${parameters.agent_id}`
+ && opened.outcome === "first_session_opened" && opened.session_id === session.session_id
+ && opened.managed_agent_id === session.managed_agent_id && opened.goal_instance_id === goal.goal_instance_id
+ && typeof session.upstream_thread_id === "string" && session.upstream_thread_id.length > 0
+ && opened.upstream_thread_id === session.upstream_thread_id,
+ "original Task request requires the same retained creation, workspace, Goal lifetime, Agent, Task and Session");
+ return {creation_operation_id: proposalId, operation_id: `goal-create-${proposalId}`,
+ goal_id: goal.goal_id, goal_instance_id: goal.goal_instance_id, agent_id: parameters.agent_id,
+ session_id: session.session_id, todo_id: todoId, permission: review.permission, write_scope: review.writeScope};
+}
+
+/** Read the explicit committed request; absence is never inferred from an old
+ * button label or Chat prose. The executor separately rechecks live admission.
+ */
+export function projectGoalCreationExecutionRequest(params: JsonObject): JsonObject | null {
+ const proposal = requireJsonObject(params.proposal, "original Goal creation");
+ const parameters = requireJsonObject(proposal.normalized_parameters, "original creation parameters");
+ if (parameters.start_execution === undefined || parameters.start_execution === false) return null;
+ if (proposal.status !== "applied") return null;
+ const request = prepareGoalCreationExecutionRequest(params);
+ requireThat(request !== null, "original Task execution request is unavailable");
+ const steps = requireJsonObject(requireJsonObject(proposal.checkpoint, "original creation checkpoint").steps,
+ "original creation steps");
+ const checkpoint = requireJsonObject(steps.original_task_requested, "original Task request checkpoint");
+ const receipt = requireJsonObject(proposal.receipt, "original creation receipt");
+ const resources = requireJsonObject(receipt.resource_ids, "original creation resources");
+ const retained = jsonObject(receipt.original_task_request);
+ const equal = (value: JsonObject | null): boolean => value !== null
+ && Object.keys(value).length === Object.keys(request).length
+ && Object.entries(request).every(([key, expected]) => JSON.stringify(value[key]) === JSON.stringify(expected));
+ const {outcome, source, ...checkpointRequest} = checkpoint;
+ const originalSource = requireJsonObject(source, "original confirmed creation source");
+ const committedSteps = requireJsonObject(receipt.step_receipts, "committed creation checkpoints");
+ requireThat(outcome === "original_task_requested" && equal(checkpointRequest) && equal(retained)
+ && Object.keys(originalSource).length === 3
+ && originalSource.request_digest === proposal.request_digest
+ && originalSource.expected_state_fingerprint === proposal.expected_state_fingerprint
+ && originalSource.upstream_thread_id === requireJsonObject(params.session, "original creation Session").upstream_thread_id
+ && canonicalAuthoritySha256(committedSteps.original_task_requested) === canonicalAuthoritySha256(checkpoint)
+ && receipt.outcome === "goal_created" && receipt.projection_verified === true
+ && resources.goal_id === request.goal_id && resources.goal_instance_id === request.goal_instance_id
+ && resources.agent_id === request.agent_id && resources.managed_agent_id === request.agent_id
+ && resources.session_id === request.session_id && resources.creation_operation_id === request.creation_operation_id
+ && Array.isArray(resources.todo_ids) && resources.todo_ids.length === 1 && resources.todo_ids[0] === request.todo_id,
+ "original Task request checkpoint and committed creation receipt must match exactly");
+ return request;
+}
diff --git a/loopx/control_plane/goals/creation_execution_review.ts b/loopx/control_plane/goals/creation_execution_review.ts
new file mode 100644
index 0000000000..d694d19494
--- /dev/null
+++ b/loopx/control_plane/goals/creation_execution_review.ts
@@ -0,0 +1,47 @@
+/** Pure Goal creation intent and permission review shared with presentation.
+ * The existing action owner confirms this request; native Task admission and
+ * filesystem authority still require their current canonical owners.
+ */
+import type {JsonObject} from "../runtime_decode.ts";
+import {EffectRuntimeRequestError} from "../effect_runtime_errors.ts";
+
+export type GoalCreationPermission = "read_only" | "workspace_write_on_confirmation";
+export type GoalCreationExecutionReview = {
+ requested: true; permission: GoalCreationPermission; writeScope: string[];
+};
+
+export function normalizeGoalCreationExecution(params: JsonObject): JsonObject {
+ const result: JsonObject = {};
+ if (params.permission !== undefined) {
+ if (params.permission !== "read_only" && params.permission !== "workspace_write_on_confirmation") {
+ throw new EffectRuntimeRequestError("permission must select the existing Goal creation read or workspace-write review");
+ }
+ result.permission = params.permission;
+ }
+ if (params.start_execution !== undefined) {
+ if (typeof params.start_execution !== "boolean") {
+ throw new EffectRuntimeRequestError("start_execution must be true or false");
+ }
+ result.start_execution = params.start_execution;
+ }
+ if (params.start_execution === true) {
+ if (params.agent_id !== "codex" || params.permission === undefined) {
+ throw new EffectRuntimeRequestError("original Task start requires the Codex Agent and an explicit permission choice");
+ }
+ if (params.native_token_budget != null) {
+ throw new EffectRuntimeRequestError("select either the LoopX original Task or native host Goal activation for this creation");
+ }
+ if (!Array.isArray(params.initial_todos) || params.initial_todos.length !== 1
+ || typeof params.completion_criteria !== "string" || !params.completion_criteria.trim()) {
+ throw new EffectRuntimeRequestError("original Task start requires one initial Task and its completion criteria");
+ }
+ }
+ return result;
+}
+
+export function goalCreationExecutionReview(parameters: JsonObject): GoalCreationExecutionReview | undefined {
+ if (parameters.start_execution === undefined || parameters.start_execution === false) return undefined;
+ const normalized = normalizeGoalCreationExecution(parameters);
+ return {requested: true, permission: normalized.permission as GoalCreationPermission,
+ writeScope: normalized.permission === "workspace_write_on_confirmation" ? ["**"] : []};
+}
diff --git a/loopx/control_plane/goals/task_validation_plan.ts b/loopx/control_plane/goals/task_validation_plan.ts
index 895e573f68..63d89a9596 100644
--- a/loopx/control_plane/goals/task_validation_plan.ts
+++ b/loopx/control_plane/goals/task_validation_plan.ts
@@ -3,7 +3,7 @@
* pure plan; caller/model input is not authority and the plan grants no execution
* or write permission. */
import type {JsonObject} from "../effect_program.ts";
-import {requireJsonObject} from "../runtime_decode.ts";
+import {jsonObject, requireJsonObject} from "../runtime_decode.ts";
import {EffectRuntimeRequestError} from "../effect_runtime_errors.ts";
import {canonicalAuthoritySha256} from "../coordination/authority_store_codec.ts";
import {acceptanceValidationEffects, type AcceptanceCompletionRequirements} from "./acceptance_contract.ts";
@@ -14,6 +14,25 @@ function requireThat(ok: unknown, message: string): asserts ok {
if (!ok) throw new EffectRuntimeRequestError(message);
}
+/** A display selection from retained creation work, never a newest/first Todo
+ * heuristic or an execution grant. The host must qualify the selected Todo's
+ * current validation through canonicalTaskValidationPlan before advertising it.
+ */
+export function originalCreationTaskSelection(params: JsonObject): JsonObject {
+ const basis = requireJsonObject(params.basis, "current Goal acceptance basis");
+ requireThat(basis.status === "loaded" && typeof basis.provider_revision === "string"
+ && basis.provider_revision.length > 0, "current Goal acceptance basis required");
+ const contract = requireJsonObject(basis.goal_acceptance_contract, "current Goal acceptance projection");
+ const outcomes = jsonObject(contract.outcome_requirements);
+ if (!outcomes || !Array.isArray(outcomes.outcomes)) return {todo_id: null, reason: "original_task_unavailable"};
+ const candidates: unknown[] = outcomes.outcomes;
+ const original = candidates.map(jsonObject).filter((row): row is JsonObject => row?.id === "goal-creation");
+ if (original.length !== 1 || !Array.isArray(original[0].todo_ids)
+ || original[0].todo_ids.length !== 1 || typeof original[0].todo_ids[0] !== "string"
+ || !original[0].todo_ids[0]) return {todo_id: null, reason: "original_task_unavailable"};
+ return {todo_id: original[0].todo_ids[0], reason: null};
+}
+
/** An explicit null requirement means owner-resolved out-of-scope/disabled
* checks. Missing, unbound or stale owner acceptance must never fall back to a
* Todo-only declaration. Required Todo checks remain cumulative with Goal checks. */
diff --git a/loopx/control_plane/heartbeat/builder.py b/loopx/control_plane/heartbeat/builder.py
index 23796716e6..2f1e587d57 100644
--- a/loopx/control_plane/heartbeat/builder.py
+++ b/loopx/control_plane/heartbeat/builder.py
@@ -6,7 +6,7 @@
from pathlib import Path
from typing import Any
-from ...agent_registry import normalize_registered_agents
+from ...agent_registry import load_goal_from_registry, normalize_registered_agents
from ...ark_managed_agent_host import build_ark_managed_agent_host_contract
from ...execution_profile import (
TURN_GRANULARITY_FINE,
@@ -660,6 +660,14 @@ def build_heartbeat_prompt(
if fine_grained:
payload["turn_granularity"] = TURN_GRANULARITY_FINE
payload["turn_mode"] = "fine_grained"
+ if registry_path:
+ from ..coordination.remote_authority_gateway import (
+ get_remote_authority_projection,
+ remote_authority_config,
+ )
+
+ if cfg := remote_authority_config(load_goal_from_registry(Path(registry_path), goal_id)):
+ payload["remote_authority"] = get_remote_authority_projection(goal_id, cfg)
payload["agent_model"] = AgentRuntimeModel.PEER_V1.value
return _prune_prompt_metadata(payload, thin=thin, brief=brief)
diff --git a/loopx/control_plane/presentation/action_review_plan.ts b/loopx/control_plane/presentation/action_review_plan.ts
index ec6059c58c..00ca24d4a6 100644
--- a/loopx/control_plane/presentation/action_review_plan.ts
+++ b/loopx/control_plane/presentation/action_review_plan.ts
@@ -1,6 +1,7 @@
import type {DecisionOutcome, ResumeState} from "../todos/user_completion_types.js";
import {BARE_SHA256_PATTERN} from "../content_digest.ts";
import {goalCreationOutcome, type GoalCreationOutcome} from "../goals/creation_outcome.ts";
+import {goalCreationExecutionReview, type GoalCreationExecutionReview} from "../goals/creation_execution_review.ts";
export type ActionReviewIdentity = {
schemaVersion: "action_review_plan_v0";
@@ -96,6 +97,12 @@ export type ActionReviewPlan = ActionReviewIdentity & ActionReviewState & {
/** Recorded creation effects, not evidence of a completed Goal or model Turn. */
creationProgress?: GoalCreationProgress;
creationOutcome?: GoalCreationOutcome;
+ /** New Session routing only. Recorded Sessions recover through creationProgress;
+ * this identity decision does not certify Task acceptance or execution. */
+ creationSession?: {managedAgentId: string};
+ /** Explicit fresh creation intent; admission and effective approval are
+ * independently checked before the original native Task is dispatched. */
+ creationExecution?: GoalCreationExecutionReview;
/** Accepted dispatch bound to its original Turn. It does not establish a result. */
taskExecution?: { goalId: string; todoId: string; sessionId: string; turnId: string };
};
@@ -541,12 +548,27 @@ export function compileActionReviewPlan(proposalValue: unknown, nowMs?: number):
const operationFrame = compileOperationReviewFrame(proposal, nowMs);
const reviewCardFrame = compileReviewCardFrame(proposal);
const decisionFrame = compileDecisionReviewFrame(proposal);
+ const parameters = objectValue(proposal.normalized_parameters);
const creationProgress = goalCreationProgress(proposal);
+ const creationExecution = proposal.action_kind === "goal.create" && parameters
+ ? goalCreationExecutionReview(parameters) : undefined;
// Older already-dispatched creations retain their original semantics. A
// failed write retries its original CAS even after a response was lost.
const creationOutcome = creationProgress?.todoIds && (!creationProgress.sessionId && !creationProgress.firstTurn
|| creationProgress.completionBasis)
- ? goalCreationOutcome(identity.proposalId, objectValue(proposal.normalized_parameters) ?? {}, creationProgress.todoIds) : undefined;
+ ? goalCreationOutcome(identity.proposalId, parameters ?? {}, creationProgress.todoIds) : undefined;
+ const creationSteps = objectValue(objectValue(proposal.checkpoint)?.steps);
+ // A persisted Session/Turn, including a malformed checkpoint, always owns
+ // recovery. Never infer a new managed identity from its old creation fields.
+ const creationSession = proposal.action_kind === "goal.create" && parameters?.agent_id === "codex"
+ && parameters.native_token_budget == null && proposal.status !== "applied"
+ && !creationProgress?.sessionId && !creationProgress?.firstTurn
+ && !Object.hasOwn(proposal, "first_turn_readback")
+ && (proposal.checkpoint == null || creationSteps !== null
+ && (Object.keys(creationSteps).length === 0 || creationProgress !== undefined))
+ && !Object.hasOwn(creationSteps ?? {}, "first_session_opened")
+ && !Object.hasOwn(creationSteps ?? {}, "first_turn_started")
+ ? {managedAgentId: "codex"} : undefined;
const finish = (state: ActionReviewState): ActionReviewPlan => ({
...identity,
...state,
@@ -555,6 +577,8 @@ export function compileActionReviewPlan(proposalValue: unknown, nowMs?: number):
...(decisionFrame ? { decisionFrame } : {}),
...(creationProgress ? { creationProgress } : {}),
...(creationOutcome ? { creationOutcome } : {}),
+ ...(creationSession ? { creationSession } : {}),
+ ...(creationExecution ? { creationExecution } : {}),
});
const held = (
interaction: "gated" | "refresh" | "repair" | "pending" | "completed" | "inactive",
@@ -589,7 +613,6 @@ export function compileActionReviewPlan(proposalValue: unknown, nowMs?: number):
return taskExecution ? {...completed, taskExecution} : completed;
}
const basis = objectValue(proposal.canonical_update_basis);
- const parameters = objectValue(proposal.normalized_parameters);
// Recovery keeps the frozen workspace and committed effects. The server
// revalidates their identity and reuses native Todo/Turn idempotency owners.
if (proposal.action_kind === "goal.create" && ["failed", "applying"].includes(String(proposal.status))
diff --git a/loopx/control_plane/quota/accounting_artifact_transaction.ts b/loopx/control_plane/quota/accounting_artifact_transaction.ts
index 576deb4323..54867f699d 100644
--- a/loopx/control_plane/quota/accounting_artifact_transaction.ts
+++ b/loopx/control_plane/quota/accounting_artifact_transaction.ts
@@ -1,6 +1,6 @@
import { createHash } from "node:crypto";
-import { access, lstat, readFile } from "node:fs/promises";
-import { basename, dirname, extname, join, resolve } from "node:path";
+import { access, lstat, readFile, realpath } from "node:fs/promises";
+import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep } from "node:path";
import type { JsonObject } from "../effect_program.ts";
import { EffectRuntimeRequestError } from "../effect_runtime_errors.ts";
@@ -109,6 +109,7 @@ export interface QuotaAccountingArtifactCommitRequest {
expectedIndexDigest: string | null;
goalRef?: JsonObject;
indexLockHeld?: boolean;
+ matchesRetainedRequest?: (record: JsonObject) => boolean;
prepare: (
context: QuotaAccountingArtifactPrepareContext,
) =>
@@ -507,6 +508,59 @@ export function resolveQuotaAccountingEffect(
return { kind: "absent" };
}
+export async function readQuotaSpendEventFromRun(
+ runsDir: string,
+ run: JsonObject,
+ goalId: string,
+): Promise {
+ const inline = jsonObject(run.quota_event);
+ if (inline) return inline;
+ if (typeof run.json_path !== "string" || !run.json_path.trim()) return null;
+ let targetPath: string;
+ try {
+ targetPath = await realpath(resolve(run.json_path.trim()));
+ } catch (error) {
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") {
+ return null;
+ }
+ throw error;
+ }
+ const relativePath = relative(await realpath(runsDir), targetPath);
+ if (
+ !relativePath ||
+ relativePath === ".." ||
+ relativePath.startsWith(`..${sep}`) ||
+ isAbsolute(relativePath)
+ ) {
+ throw new EffectRuntimeRequestError(
+ "quota spend json_path must stay inside the goal runs directory",
+ );
+ }
+ let content: string;
+ try {
+ content = await readFile(targetPath, "utf8");
+ } catch (error) {
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") return null;
+ throw error;
+ }
+ let value: unknown;
+ try {
+ value = JSON.parse(content);
+ } catch {
+ throw new EffectRuntimeRequestError("quota spend JSON artifact is malformed");
+ }
+ const record = requiredObject(value, "quota spend JSON artifact");
+ if (
+ record.classification !== contractFor("spend").classification ||
+ (record.goal_id !== undefined && record.goal_id !== goalId)
+ ) {
+ throw new EffectRuntimeRequestError(
+ "quota spend JSON artifact identity does not match its index row",
+ );
+ }
+ return jsonObject(record.quota_event);
+}
+
export async function lookupQuotaAccountingReplay(
kind: QuotaAccountingArtifactKind,
indexPath: string,
@@ -920,7 +974,8 @@ export async function commitQuotaAccountingArtifactTransaction(
}
if (
existingReceipt.effect_id !== request.effectId ||
- existingReceipt.request_digest !== request.requestDigest
+ (existingReceipt.request_digest !== request.requestDigest &&
+ request.matchesRetainedRequest?.(existingReceipt.record) !== true)
) {
return {
status: "conflict",
diff --git a/loopx/control_plane/quota/heartbeat_receipt.py b/loopx/control_plane/quota/heartbeat_receipt.py
index 8eaf717a38..b228abd519 100644
--- a/loopx/control_plane/quota/heartbeat_receipt.py
+++ b/loopx/control_plane/quota/heartbeat_receipt.py
@@ -17,6 +17,7 @@
from .settlement_workspace_causality import (
delivery_workspace_causality_from_event_details,
)
+from .spend_sources import TURN_SCOPED_SLOT_SPEND_SOURCES
HEARTBEAT_RECEIPT_SCHEMA_VERSION = "heartbeat_quota_receipt_v0"
@@ -204,6 +205,7 @@ def ensure_turn_heartbeat_settlement_receipt(
semantic_replan_guard_scoped: bool,
semantic_replan_obligation_id: str | None,
semantic_replan_capability_guard: Mapping[str, object] | None = None,
+ quota_spend_source: str | None = None,
goal_ref: Mapping[str, object] | None = None,
before_bind: Callable[[], None] | None = None,
) -> dict[str, object]:
@@ -224,6 +226,8 @@ def ensure_turn_heartbeat_settlement_receipt(
):
raise ValueError("Turn semantic replan obligation id is malformed")
+ if quota_spend_source is not None and quota_spend_source not in TURN_SCOPED_SLOT_SPEND_SOURCES:
+ raise ValueError("Turn quota spend source must be heartbeat or visible-goal")
log_path = rollout_event_log_path(runtime_root, identity.goal_id)
log_path.parent.mkdir(parents=True, exist_ok=True)
with exclusive_cross_runtime_file_lock(
@@ -248,6 +252,15 @@ def ensure_turn_heartbeat_settlement_receipt(
raise HeartbeatReceiptIdentityConflictError(
"Turn heartbeat receipt belongs to another settlement identity"
)
+ previous_details = effective.get("details") if effective is not None else None
+ previous_source = (
+ previous_details.get("quota_spend_source")
+ if observed is not None and isinstance(previous_details, Mapping) else None
+ )
+ if previous_source is not None and quota_spend_source not in (None, previous_source):
+ raise HeartbeatReceiptIdentityConflictError(
+ "Turn heartbeat receipt belongs to another quota spend source"
+ )
if before_bind is not None:
# The existing cross-runtime binding lock serializes the competing
# identity before transport-specific start admission. Its owner may
@@ -311,6 +324,8 @@ def ensure_turn_heartbeat_settlement_receipt(
details["semantic_replan_capability_id"] = semantic_replan_capability_guard["capability_id"]
details["semantic_replan_gap_id"] = semantic_replan_capability_guard["gap_id"]
details["semantic_replan_frontier_revision"] = semantic_replan_capability_guard["frontier_revision"]
+ if (source := previous_source or quota_spend_source) is not None:
+ details["quota_spend_source"] = source
source_event_id = (
str(effective.get("event_id") or "").strip()
if effective is not None
diff --git a/loopx/control_plane/quota/live_decision.py b/loopx/control_plane/quota/live_decision.py
index 5498b6ea28..cfe569dcfe 100644
--- a/loopx/control_plane/quota/live_decision.py
+++ b/loopx/control_plane/quota/live_decision.py
@@ -7,7 +7,7 @@
from typing import Any
from ...quota import build_quota_should_run
-from ...agent_registry import load_goal_from_registry
+from ...agent_registry import load_goal_from_registry, registered_agent_ids_for_goal
from ..agent_context import project_agent_context, project_goal_agent_context
from ..runtime.time import now_utc_iso
from ..goals.goal_frontier.fallback_source import live_fallback_authority_items
@@ -688,6 +688,9 @@ def build_live_quota_should_run_decision(
) -> dict[str, Any]:
"""Build one live CLI decision while keeping host observation injectable."""
resolved_context = resolve_scheduler_execution_context(scheduler_execution_context)
+ from ..coordination.remote_authority_gateway import remote_authority_config
+
+ registered_goal = load_goal_from_registry(registry_path, goal_id)
app_automation_applicable = (
resolved_context.ok
and resolved_context.context is not None
@@ -725,6 +728,20 @@ def build_live_quota_should_run_decision(
**decision_status_payload,
"bounded_research_frontier": dict(frontier),
}
+ remote_config = remote_authority_config(registered_goal)
+ if remote_config is not None:
+ # Use the same hard-pause precedence and keep fresh local selection
+ # outside the remote route, even when a TLS read succeeds.
+ return _build_remote_authority_wait_decision(
+ decision_status_payload, configured_goal=registered_goal or {},
+ goal_id=goal_id, agent_id=agent_id, remote_config=remote_config,
+ registry_path=registry_path, runtime_root=runtime_root, goal_ref=goal_ref,
+ scheduler_context=resolved_context, available_capabilities=available_capabilities,
+ include_scheduler_detail=include_scheduler_detail,
+ codex_app_rrule=observed_rrule, codex_app_automation_id=observed_automation_id or None,
+ turn_instance_id=turn_instance_id, receipt_bound_todo_id=receipt_bound_todo_id,
+ receipt_bound_replan_obligation_id=receipt_bound_replan_obligation_id,
+ )
settlement_readback = read_heartbeat_settlement(
runtime_root,
goal_id=goal_id,
@@ -754,6 +771,7 @@ def build_live_quota_should_run_decision(
)
if (
receipt_bound_guard_fields["receipt_bound_replan_guard_scoped"]
+ and receipt_bound_replay_phase is ReceiptBoundReplayPhase.OPEN
and receipt_bound_replan_obligation_id
and agent_id
):
@@ -939,7 +957,7 @@ def build_live_quota_should_run_decision(
"agent_id": agent_id,
"todo_id": (payload.get("selected_todo") or {}).get("todo_id"),
}
- goal = load_goal_from_registry(registry_path, goal_id)
+ goal = registered_goal
if goal is not None:
context = project_goal_agent_context(
phase="before_plan",
@@ -977,3 +995,91 @@ def build_live_quota_should_run_decision(
hook_dispatch=turn_start_hook_dispatch, context_navigation=context_navigation,
inline_goal_source=inline_goal_source)
return payload
+
+
+def _build_remote_authority_wait_decision(
+ status_payload: dict[str, Any], *, configured_goal: dict[str, Any],
+ goal_id: str, agent_id: str | None, remote_config: Mapping[str, Any],
+ registry_path: Path, runtime_root: Path, goal_ref: Mapping[str, Any] | None,
+ scheduler_context: SchedulerExecutionContextResolution,
+ available_capabilities: list[str] | None, turn_instance_id: str | None,
+ include_scheduler_detail: bool, codex_app_rrule: str, codex_app_automation_id: str | None,
+ receipt_bound_todo_id: str | None, receipt_bound_replan_obligation_id: str | None,
+) -> dict[str, Any]:
+ from ...quota import _build_quota_plan_for_goal
+ from ..coordination.remote_authority_gateway import (
+ check_remote_authority_gateway_status,
+ get_remote_authority_projection,
+ )
+ from .decision_summary import quota_plan_items
+ from .settlement_precedence import settled_replay_fields
+ from .should_run import build_quota_paused_should_run_payload
+ from .states import quota_item_is_paused
+
+ plan, health_ok = _build_quota_plan_for_goal(status_payload, goal_id=goal_id)
+ item = next((row for row in quota_plan_items(plan) if row.get("goal_id") == goal_id), None)
+ if item is not None and quota_item_is_paused(item):
+ return build_quota_paused_should_run_payload(
+ status_payload, safe_goal_id=goal_id, requested_agent_id=agent_id, item=item, plan=plan,
+ goal_health_ok=health_ok, include_scheduler_detail=include_scheduler_detail,
+ codex_app_current_rrule=codex_app_rrule, codex_app_automation_id=codex_app_automation_id,
+ resolved_scheduler_context=scheduler_context, runtime_root=runtime_root, goal_ref=goal_ref,
+ )
+ scope_matches = (
+ (goal_ref is None or goal_ref.get("goal_instance_id") == configured_goal.get("goal_instance_id"))
+ and agent_id in registered_agent_ids_for_goal(configured_goal)
+ )
+ readback = read_heartbeat_settlement(
+ runtime_root, goal_id=goal_id, agent_id=agent_id, todo_id=receipt_bound_todo_id,
+ turn_instance_id=turn_instance_id, registry_path=registry_path, goal_ref=goal_ref,
+ replan_obligation_id=None if receipt_bound_todo_id else receipt_bound_replan_obligation_id,
+ ) if scope_matches and turn_instance_id else None
+ settled = readback is not None and readback.replay_phase is ReceiptBoundReplayPhase.SETTLED
+ payload: dict[str, Any] = {
+ "ok": True, "mode": "should-run", "goal_id": goal_id, "agent_id": agent_id,
+ "should_run": False, "normal_delivery_allowed": False, "requires_user_action": False,
+ }
+ if goal_ref is not None:
+ payload["goal_ref"] = dict(goal_ref)
+ if scope_matches and not settled:
+ if (receipt_bound_todo_id and agent_id and turn_instance_id and readback is not None
+ and readback.replay_phase is ReceiptBoundReplayPhase.SETTLEMENT_PENDING):
+ attach_settlement_progress(payload, readback, registry_path=registry_path,
+ runtime_root=runtime_root, goal_ref=goal_ref)
+ if apply_receipt_bound_wait_recovery(
+ payload, registry_path=registry_path, runtime_root=runtime_root, goal_id=goal_id,
+ agent_id=agent_id, todo_id=receipt_bound_todo_id, turn_instance_id=turn_instance_id,
+ available_capabilities=available_capabilities, scheduler_execution_context=scheduler_context,
+ monitor_phase=readback.monitor_phase.value if readback.monitor_phase else None,
+ settlement_progress=readback.progress, replay_phase=readback.replay_phase,
+ heartbeat_receipt=readback.heartbeat_receipt,
+ ):
+ return payload
+ if apply_unsettled_host_turn_recovery_if_required(
+ payload, registry_path=registry_path, runtime_root=runtime_root, goal_id=goal_id,
+ agent_id=agent_id, current_turn_instance_id=turn_instance_id,
+ available_capabilities=available_capabilities, scheduler_execution_context=scheduler_context,
+ goal_ref=goal_ref,
+ ):
+ return payload
+
+ if settled:
+ payload.update(settled_replay_fields())
+ else:
+ gateway = (check_remote_authority_gateway_status(goal_id, agent_id, remote_config) if scope_matches else {
+ **get_remote_authority_projection(goal_id, remote_config),
+ "status": "unavailable", "reason_code": "remote_authority_local_scope_mismatch",
+ })
+ reason_code = gateway.get("reason_code", "remote_authority_execution_not_admitted")
+ reason = ("Remote authority read verified; shared Turn admission is unavailable"
+ if gateway["status"] == "verified" else "Waiting for a verified read from the configured remote authority")
+ action = EffectiveAction.BLOCKED_WAIT.value
+ payload.update(decision="wait", state="waiting", effective_action=action,
+ remote_gateway=gateway, reason_code=reason_code, reason=reason, recommended_action=reason,
+ execution_obligation={"kind": action, "must_attempt_work": False, "delivery_allowed": False,
+ "reason": reason, "spend_policy": "no quota spend for remote authority observation"})
+ payload["interaction_contract"] = build_interaction_contract(
+ payload, available_capabilities=available_capabilities, scheduler_execution_context=scheduler_context,
+ turn_instance_id=turn_instance_id, registry_path=str(registry_path), runtime_root=str(runtime_root),
+ )
+ return payload
diff --git a/loopx/control_plane/quota/prior_turn_closeout.ts b/loopx/control_plane/quota/prior_turn_closeout.ts
index 723f548e78..67841d77f8 100644
--- a/loopx/control_plane/quota/prior_turn_closeout.ts
+++ b/loopx/control_plane/quota/prior_turn_closeout.ts
@@ -14,6 +14,7 @@ import {
heartbeatReceiptBinding,
heartbeatReceiptDetails,
heartbeatReceiptFactFromEvent,
+ normalizeHeartbeatReplanObligationId,
selectEffectiveHeartbeatReceipt,
type HeartbeatReceiptBinding,
type HeartbeatReceiptFact,
@@ -30,12 +31,20 @@ export interface PriorHostTurnCloseoutCandidate extends JsonObject {
/** The Todo id or autonomous replan obligation id, per `binding_kind`. */
binding_id: string;
settlement_effect_id: string | null;
+ /** The effective guard admitted a replan under this Todo's settlement identity. */
+ admitted_todo_replan?: true;
+}
+
+interface CloseoutCandidateCheck {
+ candidate: PriorHostTurnCloseoutCandidate;
+ requireWritebackForRecovery: boolean;
}
function candidate(
binding: HeartbeatReceiptBinding,
turnInstanceId: string,
fact: HeartbeatReceiptFact,
+ admittedTodoReplan: boolean,
): PriorHostTurnCloseoutCandidate {
return {
prior_turn_instance_id: turnInstanceId,
@@ -43,21 +52,26 @@ function candidate(
binding_kind: binding.binding_kind,
binding_id: binding.binding_id,
settlement_effect_id: binding.settlement_effect_id,
+ ...(admittedTodoReplan ? {admitted_todo_replan: true as const} : {}),
};
}
function admitsAutonomousReplan(event: JsonObject, fact: HeartbeatReceiptFact): boolean {
- if (fact.replan_obligation_id === null || fact.settlement_effect_id === null) return false;
const guard = heartbeatReceiptDetails(event);
- return guard.semantic_replan_obligation_id === fact.replan_obligation_id
+ const obligationId = normalizeHeartbeatReplanObligationId(guard.semantic_replan_obligation_id);
+ return fact.settlement_effect_id !== null && obligationId !== null
+ && (fact.todo_id !== null || obligationId === fact.replan_obligation_id)
&& guard.interaction_mode === "autonomous_replan" && guard.ok === true && guard.should_run === true
&& guard.must_attempt_work === true && guard.delivery_allowed === false && guard.quiet_noop_allowed === false;
}
/**
- * Select prior Turns eligible for closeout readback. Guard preflight requires
- * an explicit closeout marker. Refresh recovery can also discover an admitted
- * replan-only binding without making it a later Turn's admission prerequisite.
+ * Select prior Turns eligible for closeout readback. A Todo-bound replan
+ * carries the Todo settlement identity and a separate semantic obligation.
+ * It did not deliver a Todo, so its closeout marker can be false. Fresh
+ * admission requires a verified outstanding writeback for this binding.
+ * Explicit pending-closeout discovery can also find admitted replans before
+ * writeback, whether their settlement identity is a Todo or an obligation.
*
* Every prior Turn is identity-validated, so a conflicting receipt fails the
* read instead of being hidden behind a newer Turn. Results are newest first
@@ -74,8 +88,8 @@ export function selectCloseoutCandidates(
goalId: string,
agentId: string,
excludeTurnInstanceId: string | null,
- { includeAdmittedReplans } = { includeAdmittedReplans: false },
-): { candidates: PriorHostTurnCloseoutCandidate[]; turnsValidated: number } {
+ { includeUnwrittenReplans } = { includeUnwrittenReplans: false },
+): { candidates: CloseoutCandidateCheck[]; turnsValidated: number } {
const perTurn = new Map();
const firstRecorded = new Map();
for (const event of receipts) {
@@ -100,7 +114,7 @@ export function selectCloseoutCandidates(
}
}
- const candidates: PriorHostTurnCloseoutCandidate[] = [];
+ const candidates: CloseoutCandidateCheck[] = [];
const currentPosition = excludeTurnInstanceId === null
? undefined : firstRecorded.get(excludeTurnInstanceId);
let validated = 0;
@@ -112,10 +126,14 @@ export function selectCloseoutCandidates(
// must not conceal a conflicting persisted settlement binding.
if (currentPosition !== undefined && firstRecorded.get(turnInstanceId)! >= currentPosition) continue;
const fact = heartbeatReceiptFactFromEvent(effective);
- if (!fact.closeout_required && !(includeAdmittedReplans && admitsAutonomousReplan(effective, fact))) continue;
const binding = heartbeatReceiptBinding(goalId, agentId, fact);
if (binding === null) continue;
- candidates.push(candidate(binding, turnInstanceId, fact));
+ const admittedReplan = admitsAutonomousReplan(effective, fact);
+ const admittedTodoReplan = admittedReplan && binding.binding_kind === "todo";
+ if (!fact.closeout_required && !admittedTodoReplan &&
+ !(includeUnwrittenReplans && admittedReplan)) continue;
+ candidates.push({candidate: candidate(binding, turnInstanceId, fact, admittedTodoReplan),
+ requireWritebackForRecovery: !fact.closeout_required && !includeUnwrittenReplans});
}
return {candidates, turnsValidated: validated};
}
@@ -147,14 +165,18 @@ function bundleFailed(readback: JsonObject, step: string): boolean {
/**
* The closeout receipts a candidate's settlement readback still lacks, or
- * null when that readback already validates the Turn's settlement.
+ * null when the Turn's settlement validates, or undefined when a candidate
+ * without a closeout marker has no verified writeback.
*/
-export function missingCloseoutReceipts(readback: JsonObject): string[] | null {
+export function missingCloseoutReceipts(
+ readback: JsonObject, requireWriteback: boolean,
+): string[] | null | undefined {
if (readback.found === true && !bundleFailed(readback, "settlement")) {
return null;
}
const missingReceipts: string[] = [];
if (readback.found !== true || bundleFailed(readback, "writeback")) {
+ if (requireWriteback) return undefined;
missingReceipts.push(WRITEBACK_RECEIPT);
}
if (readback.found !== true || bundleFailed(readback, "spend")) {
@@ -162,3 +184,8 @@ export function missingCloseoutReceipts(readback: JsonObject): string[] | null {
}
return missingReceipts;
}
+
+/** Only an exact writeback can leave the original debit as the sole debt. */
+export function awaitingQuotaSpend(missing: readonly string[]): boolean {
+ return missing.length === 1 && missing[0] === SPEND_RECEIPT;
+}
diff --git a/loopx/control_plane/quota/scheduler_ack.py b/loopx/control_plane/quota/scheduler_ack.py
index b9bf45a87d..e973714e4d 100644
--- a/loopx/control_plane/quota/scheduler_ack.py
+++ b/loopx/control_plane/quota/scheduler_ack.py
@@ -184,14 +184,25 @@ def _run_native_followup(
host_facts: dict[str, Any],
use_current_hint: bool,
reason_summary: str | None,
+ turn_instance_id: str | None = None,
) -> dict[str, Any]:
- """Decision-free compatibility transport for in-process Python callers."""
+ """Decision-free compatibility transport for in-process Python callers.
+ A receipt-bound Turn keeps the native transaction as the single owner of
+ the missing/stale/superseded receipt fence: it rejects before any
+ scheduler mutation instead of rebuilding a second staleness rule here.
+ """
+
+ receipt_turn_instance_id = (
+ str(turn_instance_id).strip() or None
+ if turn_instance_id is not None
+ else None
+ )
request = {
"schema_version": _FOLLOWUP_REQUEST_SCHEMA,
"runtime_root": str(runtime_root),
- "turn_instance_id": None,
- "require_heartbeat_receipt": False,
+ "turn_instance_id": receipt_turn_instance_id,
+ "require_heartbeat_receipt": receipt_turn_instance_id is not None,
"before": before,
"host_facts": host_facts,
"use_current_hint": use_current_hint,
@@ -298,6 +309,7 @@ def record_quota_scheduler_ack_for_decision(
generated_at: str | None = None,
use_current_hint: bool = False,
host_match_observed: bool = False,
+ turn_instance_id: str | None = None,
) -> dict[str, Any]:
state_key = _followup_state_key(before, surface=surface, state_key=state_key)
safe_agent_id = normalize_todo_claimed_by(agent_id)
@@ -367,6 +379,7 @@ def record_quota_scheduler_ack_for_decision(
host_facts=facts,
use_current_hint=use_current_hint,
reason_summary=reason_summary,
+ turn_instance_id=turn_instance_id,
)
except ValueError as exc:
return _failure_payload(
@@ -396,6 +409,7 @@ def record_quota_scheduler_failure_for_decision(
observed_host_rrule: str | None = None,
failure_kind: str = "host_tool_failure",
generated_at: str | None = None,
+ turn_instance_id: str | None = None,
) -> dict[str, Any]:
state_key = _followup_state_key(before, surface=surface, state_key=state_key)
safe_agent_id = normalize_todo_claimed_by(agent_id)
@@ -443,6 +457,7 @@ def record_quota_scheduler_failure_for_decision(
host_facts=facts,
use_current_hint=False,
reason_summary=None,
+ turn_instance_id=turn_instance_id,
)
except ValueError as exc:
return _failure_payload(
diff --git a/loopx/control_plane/quota/settlement.py b/loopx/control_plane/quota/settlement.py
index 389bda1c0f..f0563d3fe5 100644
--- a/loopx/control_plane/quota/settlement.py
+++ b/loopx/control_plane/quota/settlement.py
@@ -152,6 +152,7 @@ class QuotaSpendPrecheck:
settlement: SettlementResult[dict[str, Any]]
progress: dict[str, Any] | None
spend_run_present: bool
+ spend_request_rejection: SettlementResult[dict[str, Any]] | None
@dataclass(frozen=True, slots=True)
@@ -424,6 +425,7 @@ def read_quota_spend_precheck(
allow_unbound_binding: bool = False,
registry_path: Path | None = None,
goal_ref: Mapping[str, Any] | None = None,
+ spend_request: Mapping[str, Any] | None = None,
) -> QuotaSpendPrecheck | None:
"""Read the identity and replay controls before building a fresh preview."""
@@ -438,6 +440,7 @@ def read_quota_spend_precheck(
"infer_turn_instance_id": infer_turn_instance_id,
"allow_unbound_binding": allow_unbound_binding,
"spend_precheck": True,
+ **({"spend_request": dict(spend_request)} if spend_request is not None else {}),
},
registry_path=registry_path,
goal_ref=goal_ref,
@@ -452,6 +455,10 @@ def read_quota_spend_precheck(
settlement=_readback_result(payload.get("settlement")),
progress=_optional_readback_record(payload.get("progress")),
spend_run_present=spend_run_present,
+ spend_request_rejection=(
+ _readback_result(payload["spend_request_rejection"])
+ if "spend_request_rejection" in payload else None
+ ),
)
diff --git a/loopx/control_plane/quota/settlement_readback.ts b/loopx/control_plane/quota/settlement_readback.ts
index 827f1b0ee4..10bd891ae3 100644
--- a/loopx/control_plane/quota/settlement_readback.ts
+++ b/loopx/control_plane/quota/settlement_readback.ts
@@ -22,6 +22,7 @@ import {
} from "../effect_program.ts";
import { EffectRuntimeRequestError } from "../effect_runtime_errors.ts";
import { hasMonitorPollTransaction } from "./monitor_poll_commit.ts";
+import { readQuotaSpendEventFromRun } from "./accounting_artifact_transaction.ts";
import {
jsonObject,
requireJsonObject,
@@ -1110,7 +1111,7 @@ function owedPriorTurnFromSnapshot(
snapshot: QuotaSettlementReadbackSnapshot,
agentId: string,
turnInstanceId: string | null,
- options?: { includeAdmittedReplans: boolean },
+ options?: { includeUnwrittenReplans: boolean },
): OwedPriorTurn | null {
const receipts = heartbeatReceiptsFromEvents(snapshot.events, request.goal_id, agentId)
.filter((event) => quotaOwnerOwnsProjection(request.owner, event.goal_ref));
@@ -1121,7 +1122,7 @@ function owedPriorTurnFromSnapshot(
turnInstanceId,
options,
);
- for (const selected of candidates) {
+ for (const {candidate: selected, requireWritebackForRecovery} of candidates) {
// A committed monitor poll is its own exact no-spend closeout. This is the
// same acceptance rule the next-Turn preflight applies; using only the
// advancement writeback/spend chain here incorrectly made every later
@@ -1149,9 +1150,9 @@ function owedPriorTurnFromSnapshot(
},
snapshot,
false,
- ))
+ ), requireWritebackForRecovery)
: null;
- if (missingReceipts !== null) {
+ if (missingReceipts != null) {
return { ...selected, missing_receipts: missingReceipts };
}
}
@@ -1169,7 +1170,7 @@ function readPendingCloseoutFromRequest(
}
const notFound = {schema_version: QUOTA_SETTLEMENT_READBACK_RESULT_SCHEMA, found: false};
const turn = request.turn_instance_id ?? owedPriorTurnFromSnapshot(
- request, snapshot, explicitAgentId, null, { includeAdmittedReplans: true },
+ request, snapshot, explicitAgentId, null, { includeUnwrittenReplans: true },
)?.prior_turn_instance_id;
if (!turn) return notFound;
// Selection, original binding validation and receipt progress share this
@@ -1489,10 +1490,17 @@ export async function readQuotaSettlement(value: unknown): Promise {
return prepareBlockedWait(value);
}
const request = decodeRequest(value);
- const spendPrecheck = jsonObject(value)?.spend_precheck;
+ const input = requireJsonObject(value, "quota settlement readback request");
+ const spendPrecheck = input.spend_precheck;
if (spendPrecheck !== undefined && typeof spendPrecheck !== "boolean") {
throw new EffectRuntimeRequestError("spend_precheck must be a boolean");
}
+ const spendRequest = input.spend_request === undefined ? null
+ : requireJsonObject(input.spend_request, "spend_request");
+ if (spendRequest !== null && (!spendPrecheck ||
+ !Number.isSafeInteger(spendRequest.slots) || typeof spendRequest.source !== "string")) {
+ throw new EffectRuntimeRequestError("spend_request requires spend_precheck, integer slots and a source");
+ }
if (spendPrecheck) request.include_record_bodies = true;
const withOwner = request.borrow_source_admission
? withBorrowedQuotaAccountingOwner
@@ -1518,14 +1526,60 @@ export async function readQuotaSettlement(value: unknown): Promise {
// only its receipts and whether a spend Run already needs receipt repair.
const settlement = requireJsonObject(result.settlement, "quota settlement bundle");
const outcome = requireJsonObject(settlement.result, "quota settlement result");
+ const spendRun = jsonObject(result.spend_run);
+ const spendRejection = spendRequest && identity
+ ? await originalSpendRejection(request, result, spendRequest, spendRun)
+ : null;
return {
schema_version: result.schema_version,
found: true,
identity: result.identity,
settlement: {...settlement, result: {...outcome, value: null}},
progress: result.progress,
- spend_run_present: jsonObject(result.spend_run) !== null,
+ spend_run_present: spendRun !== null,
+ ...(spendRejection === null ? {} : {spend_request_rejection: bundle(spendRejection)}),
};
},
);
}
+
+async function originalSpendRejection(
+ request: ReadbackRequest, readback: JsonObject, proposed: JsonObject,
+ spendRun: JsonObject | null,
+): Promise | null> {
+ const progress = requireJsonObject(readback.progress, "quota settlement progress");
+ const guard = jsonObject(readback.heartbeat_receipt);
+ const authorizedSource = guard ? details(guard).quota_spend_source : null;
+ // Older ledgers can have a rollout receipt but no readable artifact. Keep
+ // their receipt facts available and never repair an already mismatched debit.
+ const spendEvent = jsonObject(readback.spend_event);
+ const receipt = spendEvent ? details(spendEvent) : {};
+ const retained = spendRun === null ? null : await readQuotaSpendEventFromRun(
+ join(request.runtime_root, "goals", request.goal_id, "runs"), spendRun, request.goal_id,
+ );
+ const recordedSlots = retained?.slots ?? (receipt.slots === "" ? undefined : receipt.slots);
+ const recordedSource = retained?.source ?? (receipt.source === "" ? undefined : receipt.source);
+ const fallbackSource = recordedSource === "heartbeat" || recordedSource === "visible-goal"
+ ? recordedSource : progress.quota_spend_source;
+ const source = authorizedSource ?? fallbackSource;
+ // Every admitted CLI settlement plan authorizes one slot and the original
+ // source. The same check gates initial spend, receipt repair, and replay.
+ if (proposed.slots !== 1 || proposed.source !== source) {
+ return settlementFailed({
+ kind: "quota_spend_rejected", step_kind: "quota_spend",
+ reason: `original Turn settlement requires --slots 1 --source ${String(source)}`,
+ });
+ }
+ const slotsConflict = recordedSlots !== undefined && recordedSlots !== proposed.slots;
+ const sourceConflict = recordedSource !== undefined && recordedSource !== source;
+ // Historical complete receipts may lack the amount projection. A partial
+ // debit needs the original charge facts before we can certify its receipt.
+ const unverifiableRepair = spendRun !== null && spendEvent === null &&
+ (recordedSlots === undefined || recordedSource === undefined);
+ return slotsConflict || sourceConflict || unverifiableRepair ? settlementFailed({
+ kind: "quota_spend_rejected", step_kind: "quota_spend",
+ reason: unverifiableRepair
+ ? "the recorded debit has no verifiable billing terms for receipt repair"
+ : "the recorded debit differs from the original Turn settlement plan",
+ }) : null;
+}
diff --git a/loopx/control_plane/quota/should_run_prepare.py b/loopx/control_plane/quota/should_run_prepare.py
index 0d856b0265..d22528a4d8 100644
--- a/loopx/control_plane/quota/should_run_prepare.py
+++ b/loopx/control_plane/quota/should_run_prepare.py
@@ -260,6 +260,11 @@ def _prepare_quota_replan_context(
replan_explore_reader: Callable[[str, str | None], Mapping[str, Any] | None] | None,
) -> tuple[dict[str, Any], dict[str, Any] | None]:
"""Project the current frontier and contextualize its receipt-bound review."""
+ # Once the settlement owner verifies writeback, an old review's sources
+ # cannot be prerequisites for spending its receipt (or replaying it).
+ reconstruct_review = receipt_bound_replay_phase not in (
+ ReceiptBoundReplayPhase.SETTLEMENT_PENDING, ReceiptBoundReplayPhase.SETTLED,
+ )
goal_frontier_context = build_goal_frontier_projection_context_from_status(
replan_explore_reader=replan_explore_reader,
goal_id=safe_goal_id,
@@ -277,19 +282,23 @@ def _prepare_quota_replan_context(
registered_agent_ids=registered_agent_ids,
goal_status=str(registry_goal.get("status") or ""),
agent_profile=_quota_agent_profile(agent_identity),
- receipt_bound_replan_obligation_id=receipt_bound_replan_obligation_id,
+ receipt_bound_replan_obligation_id=(
+ receipt_bound_replan_obligation_id if reconstruct_review else None
+ ),
receipt_bound_guard_recorded_at=receipt_bound_guard_recorded_at,
)
replan_obligation = (
None
if receipt_bound_replay_phase is ReceiptBoundReplayPhase.SETTLED
else _preserve_receipt_bound_replan_obligation(
- goal_frontier_context.get("replan_obligation"),
+ goal_frontier_context.get("replan_obligation") if reconstruct_review else None,
receipt_bound_replan_obligation_id,
guard_scoped=receipt_bound_replan_guard_scoped,
agent_id=agent_frontier_id,
replay_phase=receipt_bound_replay_phase,
- transition_candidates=goal_frontier_context.get("replan_transition_candidates"),
+ transition_candidates=(
+ goal_frontier_context.get("replan_transition_candidates") if reconstruct_review else None
+ ),
stride_strategy=goal_frontier_context["stride_strategy"],
receipt_review=receipt_bound_replan_review,
goal_id=safe_goal_id,
diff --git a/loopx/control_plane/quota/spend_commit.ts b/loopx/control_plane/quota/spend_commit.ts
index 7a4f7eab4a..4f201a0190 100644
--- a/loopx/control_plane/quota/spend_commit.ts
+++ b/loopx/control_plane/quota/spend_commit.ts
@@ -361,6 +361,43 @@ function requestDigest(request: QuotaSpendCommitRequest): string {
}));
}
+function matchesRetainedSettlementSpend(
+ request: QuotaSpendCommitRequest,
+ proposedRecord: JsonObject,
+ retainedRecord: JsonObject,
+): boolean {
+ const identity = jsonObject(proposedRecord.settlement_identity);
+ const turnId = optionalString(identity?.turn_instance_id, "settlement_identity.turn_instance_id");
+ const effectId = optionalString(identity?.effect_id, "settlement_identity.effect_id");
+ if (!turnId || !effectId ||
+ (request.source !== "heartbeat" && request.source !== "visible-goal") ||
+ request.preview.effect_ref != null || request.preview.delivery_completion_spend !== true ||
+ request.effect_id !== `${effectId}#quota_spend` ||
+ identity?.goal_id !== request.goal_id || identity?.agent_id !== request.resolved_agent_id ||
+ identity?.turn_instance_id !== proposedRecord.turn_instance_id ||
+ (identity?.todo_id || null) !== (proposedRecord.todo_id ?? null) ||
+ (identity?.replan_obligation_id ?? null) !== (proposedRecord.replan_obligation_id ?? null)) {
+ return false;
+ }
+ const proposed = requiredObject(proposedRecord.quota_event, "proposed spend event");
+ const retained = requiredObject(retainedRecord.quota_event, "retained spend event");
+ // The live quota projection can change between two attempts to settle one
+ // validated writeback. The original identity and accounting facts cannot.
+ return retainedRecord.classification === QUOTA_SLOT_SPENT_CLASSIFICATION &&
+ retainedRecord.goal_id === request.goal_id &&
+ retainedRecord.agent_id === request.resolved_agent_id &&
+ canonicalJson(retainedRecord.settlement_identity) === canonicalJson(identity) &&
+ retained.source === proposed.source && retained.slots === proposed.slots &&
+ typeof proposed.delivery_run_generated_at === "string" &&
+ typeof proposed.delivery_run_classification === "string" &&
+ proposed.delivery_run_agent_id === identity.agent_id &&
+ retained.delivery_run_generated_at === proposed.delivery_run_generated_at &&
+ retained.delivery_run_classification === proposed.delivery_run_classification &&
+ retained.delivery_run_agent_id === proposed.delivery_run_agent_id &&
+ canonicalJson(retained.delivery_workspace) === canonicalJson(proposed.delivery_workspace) &&
+ retained.delivery_workspace_validated === proposed.delivery_workspace_validated;
+}
+
function positiveSlots(preview: JsonObject): number {
const value = preview.slots;
if (typeof value !== "number" || !Number.isInteger(value) || value < 1) {
@@ -851,6 +888,7 @@ export async function evaluateQuotaSpendCommit(
expectedIndexDigest: request.expected_index_digest,
...(goalRef === null ? {} : { goalRef }),
indexLockHeld,
+ matchesRetainedRequest: (retained) => matchesRetainedSettlementSpend(request, record, retained),
prepare: ({ jsonPath, markdownPath, indexPath: lockedIndexPath }) => {
const payload = payloadFor(
request,
@@ -915,6 +953,7 @@ export async function evaluateQuotaSpendCommit(
appended: repaired,
idempotent_replay: !repaired,
transaction_repaired: repaired,
+ ...(outcome.receipt.record.settlement_identity ? {receipt_repair_required: true} : {}),
reason: repaired
? "quota spend commit repaired its prepared durable transaction"
: "quota spend commit replayed for the same effect identity",
diff --git a/loopx/control_plane/quota/turn_envelope.ts b/loopx/control_plane/quota/turn_envelope.ts
index b4ed32dd2d..7d5bdc2f3a 100644
--- a/loopx/control_plane/quota/turn_envelope.ts
+++ b/loopx/control_plane/quota/turn_envelope.ts
@@ -1136,7 +1136,7 @@ function requiredReads(interaction: JsonObject, payload: JsonObject): JsonObject
return result;
}
-function boundary(payload: JsonObject): JsonObject {
+export function projectTurnBoundary(payload: JsonObject): JsonObject {
const source = object(payload.goal_boundary);
const result: JsonObject = {
rule: scalarString(source.rule, "goal_boundary.rule", "stay_in_scope_or_stop"),
@@ -1482,7 +1482,7 @@ function actionProjection(payload: JsonObject, deferSettlement = true, precision
user,
required_reads: requiredReads(interaction, payload),
replan_action_packet: replanPacket,
- boundary: boundary(payload),
+ boundary: projectTurnBoundary(payload),
execution_policy: executionPolicy(payload),
writeback: reads && deferSettlement ? deferredReplanWriteback(writeback) : writeback,
scheduler: schedulerValue,
@@ -1957,6 +1957,7 @@ export function completePlanningFacts(request: JsonObject): JsonObject {
return {...lineage, work, source_decision_hash: decisionSourceHash(decision),
action_source_hash: canonicalHash(turnEnvelopeActionSignatureDocument(work)),
scoped_settlement_identity: scopedIdentity, scoped_identity_error: identityError,
+ ...(request.include_selected_work_basis === true ? {selected_work_basis: selectedWorkLaunchBasis(decision)} : {}),
...(request.inspect_plan === true &&
(lineage.route === "ready_for_host" || lineage.route === "replan_required")
? {inspection_paths: turnPlanInspectionPaths(decision, lineage.route)} : {})};
@@ -2010,6 +2011,19 @@ function completeTaskTextSha256(decision: JsonObject, todoId: unknown): string |
return capturedTodoTextSha256(declared, context, decision.recommended_action, todoId);
}
+/** Launch rechecks the admitted task, not the provider revision or the bounded
+ * action signature. Progress, diagnostics and receipt changes are not edits to
+ * the work. Losing the verified body on a fresh read is not a match. */
+function selectedWorkLaunchBasis(decision: JsonObject, verifiedTextSha256?: string | null): string | null {
+ const todoId = selectedWorkTodoId(decision);
+ const selected = object(decision.selected_todo);
+ if (!todoId || todoId !== selected.todo_id) return null;
+ const textSha256 = verifiedTextSha256 === undefined ? completeTaskTextSha256(decision, todoId) : verifiedTextSha256;
+ return textSha256 === null ? null : canonicalHash({todo_id: todoId, text_sha256: textSha256,
+ required_write_scopes: selected.required_write_scopes ?? [],
+ required_capabilities: selected.required_capabilities ?? []});
+}
+
export function composeCompleteTurnDecision(value: unknown): JsonObject {
const request = requireJsonObject(value, "complete Turn decision request");
const payload = requireJsonObject(request.payload, "complete Turn decision source");
@@ -2655,8 +2669,9 @@ export function evaluateTurnEnvelope(value: unknown): JsonObject {
const {decision, projection} = completeHostSource(request);
const work = {selected_todo: object(projection.action).selected_todo,
task_orchestration_contract: projection.task_orchestration_contract};
- return {selected_todo: selectedWorkTodo(work),
- sha256: completeTaskTextSha256(decision, selectedWorkTodoId(work) ?? ""),
+ const sha256 = completeTaskTextSha256(decision, selectedWorkTodoId(work) ?? "");
+ return {selected_todo: selectedWorkTodo(work), sha256,
+ selected_work_basis: selectedWorkLaunchBasis(decision, sha256),
effective_action: decision.effective_action ?? null,
writeback: object(projection.writeback),
task_lease_proof: object(object(decision.execution_preparation).lease_proof),
diff --git a/loopx/control_plane/quota/unsettled_host_turn_recovery.ts b/loopx/control_plane/quota/unsettled_host_turn_recovery.ts
index 9cb4f9b55d..935729ec1f 100644
--- a/loopx/control_plane/quota/unsettled_host_turn_recovery.ts
+++ b/loopx/control_plane/quota/unsettled_host_turn_recovery.ts
@@ -33,6 +33,7 @@ import {
type HeartbeatReceiptBinding,
} from "./heartbeat_receipt_identity.ts";
import {
+ awaitingQuotaSpend,
candidateReadbackBinding,
MISSING_RECEIPT_NAMES,
missingCloseoutReceipts,
@@ -164,27 +165,21 @@ async function preflightPriorHostTurnCloseoutForOwner(
request.agent_id,
request.exclude_turn_instance_id,
);
- if (candidates.length === 0) {
- return {
- schema_version: PRIOR_HOST_TURN_CLOSEOUT_PREFLIGHT_RESULT_SCHEMA,
- status: "none",
- reason: "no_prior_turn_requires_closeout",
- turns_validated: turnsValidated,
- };
- }
- const settlementSnapshot = await readQuotaSettlementSnapshot(
- request.runtime_root,
- request.goal_id,
- rolloutSnapshot,
- );
+ // Resolve one snapshot on the first candidate. The empty path needs only
+ // the guard log, and unwritten replans may all drop out after validation.
+ let settlementSnapshot: Awaited> | undefined;
let newestSettledTurn: string | null = null;
let newestAcceptedCloseout: AcceptedCloseout = "validated_writeback_and_quota_spend";
- for (const selected of candidates) {
+ for (const {candidate: selected, requireWritebackForRecovery} of candidates) {
+ settlementSnapshot ??= await readQuotaSettlementSnapshot(
+ request.runtime_root, request.goal_id, rolloutSnapshot,
+ );
const readback = readAdmittedQuotaSettlementFromSnapshot(
settlementReadbackRequest(request, selected),
settlementSnapshot,
);
- const missingReceipts = missingCloseoutReceipts(readback);
+ const missingReceipts = missingCloseoutReceipts(readback, requireWritebackForRecovery);
+ if (missingReceipts === undefined) continue;
if (missingReceipts === null) {
// A newer settled Turn cannot hide an older missing closeout. Keep
// scanning in persisted newest-first order until the first unsettled
@@ -218,10 +213,12 @@ async function preflightPriorHostTurnCloseoutForOwner(
return {
schema_version: PRIOR_HOST_TURN_CLOSEOUT_PREFLIGHT_RESULT_SCHEMA,
status: "none",
- reason: "prior_turn_settlement_validated",
turns_validated: turnsValidated,
- prior_turn_instance_id: newestSettledTurn,
- accepted_closeout: newestAcceptedCloseout,
+ ...(newestSettledTurn === null ? {reason: "no_prior_turn_requires_closeout"} : {
+ reason: "prior_turn_settlement_validated",
+ prior_turn_instance_id: newestSettledTurn,
+ accepted_closeout: newestAcceptedCloseout,
+ }),
};
}
@@ -257,6 +254,13 @@ function decodeCandidate(value: unknown): PriorHostTurnCloseoutCandidate {
"prior host Turn recovery candidate autonomous replan binding is malformed",
);
}
+ const settlementEffectId = optionalHeartbeatString(raw.settlement_effect_id);
+ if (raw.admitted_todo_replan !== undefined &&
+ (raw.admitted_todo_replan !== true || bindingKind !== "todo" || settlementEffectId === null)) {
+ throw new EffectRuntimeRequestError(
+ "prior host Turn recovery candidate has an invalid Todo-bound replan fact",
+ );
+ }
return {
prior_turn_instance_id: requireNonEmptyString(
raw.prior_turn_instance_id,
@@ -265,7 +269,8 @@ function decodeCandidate(value: unknown): PriorHostTurnCloseoutCandidate {
event_id: optionalHeartbeatString(raw.event_id),
binding_kind: bindingKind,
binding_id: bindingId,
- settlement_effect_id: optionalHeartbeatString(raw.settlement_effect_id),
+ settlement_effect_id: settlementEffectId,
+ ...(raw.admitted_todo_replan === true ? {admitted_todo_replan: true as const} : {}),
};
}
@@ -368,8 +373,9 @@ function recoveryRepair(
bindingKind: HeartbeatReceiptBinding["binding_kind"],
todo: CandidateTodoFacts | null,
): RecoveryRepair {
- if (bindingKind !== "todo" || todo === null) return "lifecycle";
- if (todo.task_class === MONITOR_TASK_CLASS) return "monitor_poll";
+ if (bindingKind !== "todo") return "lifecycle";
+ if (todo?.task_class === MONITOR_TASK_CLASS) return "monitor_poll";
+ if (todo === null) return "lifecycle";
// Missing receipts do not prove an external dependency. Re-enter
// the original guard to recheck eligibility and retain its settlement id.
// This is a recovery route, never an accepted closeout or delivery grant.
@@ -420,7 +426,9 @@ function recoveryObligation(
*
* The accepted closeout order is policy: a validated settlement wins, then an
* exactly committed monitor-poll closeout, then a typed lifecycle transition.
- * Anything else is a recovery obligation with the exact missing receipts named.
+ * For a Todo-bound autonomous replan, a verified writeback keeps its debit
+ * obligation even when the Todo has changed. Ordinary lifecycle closeouts
+ * retain their existing meaning.
*/
export function reduceUnsettledHostTurnRecovery(value: unknown): JsonObject {
const request = requireJsonObject(value, "prior host Turn recovery request");
@@ -465,7 +473,8 @@ export function reduceUnsettledHostTurnRecovery(value: unknown): JsonObject {
};
}
- const lifecycleCloseout = acceptedLifecycleCloseout(todo);
+ const replanSpendOwed = selected.admitted_todo_replan === true && awaitingQuotaSpend(missingReceipts);
+ const lifecycleCloseout = replanSpendOwed ? null : acceptedLifecycleCloseout(todo);
if (lifecycleCloseout !== null) {
return {
schema_version: UNSETTLED_HOST_TURN_RECOVERY_RESULT_SCHEMA,
@@ -494,7 +503,7 @@ export function reduceUnsettledHostTurnRecovery(value: unknown): JsonObject {
recovery.binding_target_key = todo.target_key;
recovery.binding_cadence = todo.cadence;
}
- const repair = recoveryRepair(selected.binding_kind, todo);
+ const repair = replanSpendOwed ? "resume_prior_turn" : recoveryRepair(selected.binding_kind, todo);
// The typed repair lane lets the CLI renderer project its command list
// without re-deriving which closeout family the Turn belongs to.
recovery.repair = repair;
diff --git a/loopx/control_plane/quota/void_commit.ts b/loopx/control_plane/quota/void_commit.ts
index abb056decf..ba49176d59 100644
--- a/loopx/control_plane/quota/void_commit.ts
+++ b/loopx/control_plane/quota/void_commit.ts
@@ -1,6 +1,6 @@
import { createHash } from "node:crypto";
-import { readFile, realpath } from "node:fs/promises";
-import { basename, isAbsolute, join, relative, resolve, sep } from "node:path";
+import { readFile } from "node:fs/promises";
+import { basename, isAbsolute, join } from "node:path";
import type { JsonObject } from "../effect_program.ts";
import { EffectRuntimeRequestError } from "../effect_runtime_errors.ts";
@@ -9,6 +9,7 @@ import {
nextQuotaAccountingArtifactPaths,
parseQuotaAccountingIndex,
quotaAccountingIndexDigest,
+ readQuotaSpendEventFromRun,
renderQuotaSlotMarkdown,
type QuotaAccountingArtifactPrepareContext,
type QuotaAccountingArtifactPreparation,
@@ -365,59 +366,6 @@ function compactDecision(value: unknown): JsonObject {
};
}
-async function readTargetEvent(
- runsDir: string,
- run: JsonObject,
- goalId: string,
-): Promise {
- const inline = jsonObject(run.quota_event);
- if (inline) return inline;
- if (typeof run.json_path !== "string" || !run.json_path.trim()) return null;
- let targetPath: string;
- try {
- targetPath = await realpath(resolve(run.json_path.trim()));
- } catch (error) {
- if (error instanceof Error && "code" in error && error.code === "ENOENT") {
- return null;
- }
- throw error;
- }
- const relativePath = relative(await realpath(runsDir), targetPath);
- if (
- !relativePath ||
- relativePath === ".." ||
- relativePath.startsWith(`..${sep}`) ||
- isAbsolute(relativePath)
- ) {
- throw new EffectRuntimeRequestError(
- "quota spend json_path must stay inside the goal runs directory",
- );
- }
- let content: string;
- try {
- content = await readFile(targetPath, "utf8");
- } catch (error) {
- if (error instanceof Error && "code" in error && error.code === "ENOENT") return null;
- throw error;
- }
- let value: unknown;
- try {
- value = JSON.parse(content);
- } catch {
- throw new EffectRuntimeRequestError("quota spend JSON artifact is malformed");
- }
- const record = requiredObject(value, "quota spend JSON artifact");
- if (
- record.classification !== QUOTA_SLOT_SPENT_CLASSIFICATION ||
- (record.goal_id !== undefined && record.goal_id !== goalId)
- ) {
- throw new EffectRuntimeRequestError(
- "quota spend JSON artifact identity does not match its index row",
- );
- }
- return jsonObject(record.quota_event);
-}
-
async function findTargetSpend(
runsDir: string,
records: readonly JsonObject[],
@@ -429,7 +377,7 @@ async function findTargetSpend(
if (String(run.goal_id || goalId) !== goalId) continue;
if (String(run.generated_at ?? "") !== generatedAt) continue;
if (run.classification !== QUOTA_SLOT_SPENT_CLASSIFICATION) continue;
- const event = await readTargetEvent(runsDir, run, goalId);
+ const event = await readQuotaSpendEventFromRun(runsDir, run, goalId);
if (event?.event_type !== QUOTA_SLOT_SPENT_CLASSIFICATION) continue;
requireQuotaOwnerProjection(owner, run.goal_ref, "quota void target");
requireQuotaOwnerProjection(
diff --git a/loopx/control_plane/scheduler/arbitration.py b/loopx/control_plane/scheduler/arbitration.py
index d9ee86f085..400f669fe3 100644
--- a/loopx/control_plane/scheduler/arbitration.py
+++ b/loopx/control_plane/scheduler/arbitration.py
@@ -6,6 +6,7 @@
from typing import Any
from ..agents.agent_scope_frontier import AGENT_SCOPE_FRONTIER_SCHEMA_VERSION, AgentScopeFrontierAction
+from ..effect_runtime import effect_runtime_result
from ..goals.goal_vision_wait import exact_blocked_successor_wait_state
SCHEDULER_ARBITRATION_SCHEMA_VERSION = "scheduler_arbitration_v0"
@@ -110,3 +111,14 @@ def _scheduler_arbitration_facts(
"vision_wait_state": wait or None, "scope_wait_todo": scoped_wait,
"heartbeat_receipt": payload.get("heartbeat_receipt")})
return facts
+
+
+def monitor_quiet_stall_observation_required(payload: dict[str, Any]) -> bool:
+ """Keep automatic stall supervision unless typed arbitration proves a future wait."""
+ if not exact_blocked_successor_wait_state(payload):
+ return True
+ result = effect_runtime_result(
+ "work_item.blocked_wait_observation.project",
+ _scheduler_arbitration_facts(payload.get("interaction_contract"), payload=payload),
+ )
+ return result["required"] is not False
diff --git a/loopx/control_plane/scheduler/state.py b/loopx/control_plane/scheduler/state.py
index c00c5a6d84..cfeecac179 100644
--- a/loopx/control_plane/scheduler/state.py
+++ b/loopx/control_plane/scheduler/state.py
@@ -252,6 +252,7 @@ def _store_result(
agent_id: str,
surface: str,
state_key: str,
+ migrate_legacy: bool | None = None,
) -> Mapping[str, Any]:
params: dict[str, Any] = {
"schema_version": SCHEDULER_STATE_STORE_REQUEST_SCHEMA,
@@ -265,6 +266,8 @@ def _store_result(
}
if state is not None:
params["state"] = state
+ if migrate_legacy is not None:
+ params["migrate_legacy"] = migrate_legacy
try:
result = effect_runtime_result(f"scheduler.state.{operation}", params)
except EffectRuntimeRejected as exc:
@@ -287,6 +290,7 @@ def load_scheduler_state(
surface: str = CODEX_APP_SURFACE,
state_key: str = CODEX_APP_STATEFUL_BACKOFF_STATE_KEY,
read_observer: SchedulerStateReadObserver | None = None,
+ migrate_legacy: bool | None = None,
) -> dict[str, Any] | None:
if not agent_id:
return None
@@ -298,6 +302,7 @@ def load_scheduler_state(
agent_id=agent_id,
surface=surface,
state_key=state_key,
+ migrate_legacy=migrate_legacy,
)
state = result.get("state")
if state is not None and not isinstance(state, dict):
@@ -317,6 +322,8 @@ def load_app_automation_scheduler_state(
surface: str,
read_observer: SchedulerStateReadObserver | None = None,
) -> dict[str, Any] | None:
+ # Hint reconstruction may precede a preview or receipt rejection.
+ # Legacy migration belongs to the authorized native commit under lock.
current = load_scheduler_state(
runtime_root,
goal_id=goal_id,
@@ -324,6 +331,7 @@ def load_app_automation_scheduler_state(
surface=surface,
state_key=APP_AUTOMATION_STATEFUL_BACKOFF_STATE_KEY,
read_observer=read_observer,
+ migrate_legacy=False,
)
if current is not None or surface != CODEX_APP_SURFACE:
return current
@@ -337,6 +345,7 @@ def load_app_automation_scheduler_state(
surface=surface,
state_key=LEGACY_CODEX_APP_STATEFUL_BACKOFF_STATE_KEY,
read_observer=read_observer,
+ migrate_legacy=False,
)
diff --git a/loopx/control_plane/state_recovery.ts b/loopx/control_plane/state_recovery.ts
index c18d21ce8e..2bb65dfc82 100644
--- a/loopx/control_plane/state_recovery.ts
+++ b/loopx/control_plane/state_recovery.ts
@@ -18,7 +18,6 @@ import {STATE_RECOVERY_AUDIT_SCHEMA, decodeRecoveryMeasurements, decodeStateReco
const MAX_GOALS = 128;
const MAX_SQLITE_FILES = 128;
-const MAX_COMMITS = 20_000;
const hash = (value: string) => createHash("sha256").update(value).digest("hex");
const digest = isRecoveryDigest;
class RecoveryInputError extends Error {
@@ -72,11 +71,6 @@ function authorityCaptures(value: unknown, roots: Map | n
const goalId = String(row.goal_id);
check(!rows.has(goalId) && roots !== null && row.runtime_archive_path === (roots.get(goalId) ?? null),
"authority_capture_dependency_mismatch");
- if (row.state === "readable") {
- const archive = requireJsonObject(row.archive, "authority capture summary");
- check(typeof archive.commits === "string" && /^[1-9]\d*$/.test(archive.commits) &&
- BigInt(archive.commits) <= BigInt(MAX_COMMITS), "authority_history_capacity");
- }
rows.set(goalId, row);
}
return rows;
@@ -203,40 +197,48 @@ async function inspectGoal(root: string | null, goalId: string, standaloneSqlite
const handle = await openLocalAuthorityStoreHandle(root, goalId, {}, {existingOnly: true});
const loaded = await handle.store.loadAuthority();
check(loaded.status === "loaded", "authority_head_unavailable");
- check(parseAuthorityCursor(loaded.cursor) <= BigInt(MAX_COMMITS), "authority_history_capacity");
+ const frontier = parseAuthorityCursor(loaded.cursor);
+ check(frontier > 0n && frontier <= BigInt(Number.MAX_SAFE_INTEGER), "authority_history_count_unrepresentable");
+ const identity = await handle.store.storeIdentity();
+ check(identity.status === "available", "authority_identity_unavailable");
// Byte-valid storage alone is not a readable Goal work model. Reuse the
// same full-record, lease and acceptance decoder as canonical Todo reads.
const collection = canonicalTodoCollection(loaded.head, goalId, true);
let cursor: string | null = null;
- let count = 0;
+ let count = 0n;
let final: JsonObject | null = null;
let finalRevision: string | null = null;
- while (true) {
- const page = await handle.store.scanCommitted(cursor, 200);
+ while (count < frontier) {
+ const limit = Number(frontier - count > 200n ? 200n : frontier - count);
+ const page = await handle.store.scanCommitted(cursor, limit);
check(page.status === "page", "authority_history_unavailable");
- count += page.transactions.length;
- check(count <= MAX_COMMITS, "authority_history_capacity");
- for (const row of page.transactions) { final = row.projection; finalRevision = row.provider_revision; }
- if (!page.has_more) break;
- check(page.next_cursor !== null && page.next_cursor !== cursor && page.transactions.length > 0,
+ check(page.transactions.length === limit, "authority_history_cursor_invalid");
+ for (const row of page.transactions) {
+ count += 1n;
+ check(parseAuthorityCursor(row.cursor) === count, "authority_history_cursor_invalid");
+ final = row.projection;
+ finalRevision = row.provider_revision;
+ }
+ check(page.next_cursor === count.toString() && page.has_more === (count < frontier),
"authority_history_cursor_invalid");
cursor = page.next_cursor;
}
- check(String(count) === loaded.cursor && final !== null && finalRevision === loaded.provider_revision
- && canonicalAuthoritySha256(final) === canonicalAuthoritySha256(loaded.head), "authority_history_head_mismatch");
+ const headDigest = canonicalAuthoritySha256(loaded.head);
+ check(final !== null && finalRevision === loaded.provider_revision &&
+ canonicalAuthoritySha256(final) === headDigest, "authority_history_head_mismatch");
const readback = await handle.store.loadAuthority();
- check(readback.status === "loaded" && readback.provider_revision === loaded.provider_revision,
+ const readbackIdentity = await handle.store.storeIdentity();
+ check(readback.status === "loaded" && readback.cursor === loaded.cursor &&
+ readback.provider_revision === loaded.provider_revision && canonicalAuthoritySha256(readback.head) === headDigest &&
+ readbackIdentity.status === "available" && readbackIdentity.store_identity === identity.store_identity,
"authority_changed_during_audit");
return component(id, "coordination_authority", "readable", "historical_head_and_history_readable",
"review_goal_identity_before_adoption", {...goalFacts, provider: handle.provider,
- head_digest: canonicalAuthoritySha256(loaded.head), history_commits: count,
+ head_digest: headDigest, history_commits: Number(count),
provider_revision: loaded.provider_revision, cursor: loaded.cursor,
todo_count: collection.projection.todo_ids.length,
historical_lease_count: collection.leaseIndex?.lease_todo_ids.length ?? 0}, ["configuration"]);
} catch (error) {
- // Inspection profile overflow aborts publication; it is not evidence that
- // this owner's captured state is merely unknown or unreadable.
- if (error instanceof RecoveryInputError && error.code === "authority_history_capacity") throw error;
return component(id, "coordination_authority", "unknown",
error instanceof RecoveryInputError ? error.code : "authority_source_unavailable",
"inspect_captured_authority_with_owner", goalFacts, ["configuration"]);
@@ -327,11 +329,9 @@ async function auditOwners(value: unknown): Promise {
check(capture?.state === "readable", "authority_capture_unavailable");
check(capture.configuration_sha256 === configuration.sha256, "authority_capture_dependency_mismatch");
const facts = await inspectAuthorityBackup(dataRoot, capture);
- check(Number(facts.history_commits) <= MAX_COMMITS, "authority_history_capacity");
components.push(component(id, "coordination_authority", "readable", "historical_head_and_history_readable",
"review_goal_identity_before_adoption", {...goalFacts, ...facts}, ["configuration"]));
} catch (error) {
- if (error instanceof RecoveryInputError && error.code === "authority_history_capacity") throw error;
components.push(component(id, "coordination_authority", "unknown", "authority_capture_unavailable",
"inspect_captured_authority_with_owner", goalFacts, ["configuration"]));
}
diff --git a/loopx/control_plane/turn_driver/driver.py b/loopx/control_plane/turn_driver/driver.py
index 375a73ccff..5594207935 100644
--- a/loopx/control_plane/turn_driver/driver.py
+++ b/loopx/control_plane/turn_driver/driver.py
@@ -418,6 +418,21 @@ def _bind_host_child_operations(
return bound
+def _complete_plan_source_fields(
+ work_contract: object, projection: Mapping[str, Any], original_envelope: Mapping[str, Any] | None,
+) -> dict[str, Any]:
+ fields = {"work_contract": work_contract, "source_decision_hash": projection["source_decision_hash"]}
+ for key in ("inspection_paths", "selected_work_basis"):
+ if key in projection:
+ fields[f"_{key}"] = projection[key]
+ if original_envelope is not None:
+ # Carry the reader of the original capture from a transitional pair.
+ header = captured_turn_host_input({"turn_envelope": original_envelope})
+ if "detail_ref" in header:
+ fields["detail_ref"] = header["detail_ref"]
+ return fields
+
+
def build_loopx_turn_plan(
turn_envelope: Mapping[str, Any],
*,
@@ -431,6 +446,7 @@ def build_loopx_turn_plan(
goal_ref: Mapping[str, object] | None = None,
work_contract: object = _ABSENT_WORK_CONTRACT,
inspect_plan: bool = False,
+ include_selected_work_basis: bool = False,
) -> dict[str, Any]:
"""Plan from one complete source; retain the recorded v0 input codec.
@@ -483,6 +499,7 @@ def build_loopx_turn_plan(
projection = _turn_envelope_result("planning_facts", payload=work_contract,
turn_instance_id=turn_instance_id,
**({"inspect_plan": True} if inspect_plan else {}),
+ **({"include_selected_work_basis": True} if include_selected_work_basis else {}),
**({} if source_only_input else {"envelope": supplied}))
envelope = _mapping(projection["work"])
else:
@@ -609,16 +626,9 @@ def build_loopx_turn_plan(
),
}
if current:
- payload["work_contract"] = work_contract
- payload["source_decision_hash"] = projection["source_decision_hash"]
- if "inspection_paths" in projection:
- payload["_inspection_paths"] = projection["inspection_paths"]
- if not source_only_input:
- # Older callers may already carry an original quota capture. Keep
- # its exact header without retaining that caller's second source.
- header = captured_turn_host_input({"turn_envelope": supplied})
- if "detail_ref" in header:
- payload["detail_ref"] = header["detail_ref"]
+ payload.update(_complete_plan_source_fields(
+ work_contract, projection, None if source_only_input else supplied,
+ ))
else:
payload["turn_envelope"] = envelope
if child_operations:
diff --git a/loopx/control_plane/turn_driver/executor.py b/loopx/control_plane/turn_driver/executor.py
index a5e68b24ff..73675259e1 100644
--- a/loopx/control_plane/turn_driver/executor.py
+++ b/loopx/control_plane/turn_driver/executor.py
@@ -38,7 +38,11 @@
normalize_reward_memory_reflection_validation,
reward_memory_reflection_digest,
)
-from ..quota.turn_envelope import turn_plan_host_source
+from ..quota.turn_envelope import (
+ COMPLETE_TURN_HOST_REQUEST_SCHEMA,
+ captured_turn_host_input,
+ turn_plan_host_source,
+)
from .driver import (
COMPLETE_TURN_PLAN_SCHEMA_VERSION,
selected_turn_todo, turn_host_lineage,
@@ -156,6 +160,10 @@
def build_loopx_turn_host_request(plan: Mapping[str, Any]) -> dict[str, Any]:
"""Deliver the plan's original source; decode saved v0 plans as recorded."""
+ return _complete_loopx_turn_host_request(_assemble_loopx_turn_host_request(plan))
+
+
+def _assemble_loopx_turn_host_request(plan: Mapping[str, Any]) -> dict[str, Any]:
boundary = plan.get("boundary")
if isinstance(boundary, Mapping) and boundary.get("inspection_only") is True:
raise ValueError("LoopX Turn CLI inspection requires the explicit run-once execution surface")
@@ -197,6 +205,10 @@ def build_loopx_turn_host_request(plan: Mapping[str, Any]) -> dict[str, Any]:
if configured_native_child_limit(request) is not None:
request["turn_instance_id"] = transaction.get("turn_instance_id") or turn_key
+ return request
+
+
+def _complete_loopx_turn_host_request(request: dict[str, Any]) -> dict[str, Any]:
if "work_contract" in request:
from .host_candidate import complete_host_input
@@ -204,6 +216,20 @@ def build_loopx_turn_host_request(plan: Mapping[str, Any]) -> dict[str, Any]:
return request
+def _build_staged_dispatch_request(plan: Mapping[str, Any]) -> dict[str, Any]:
+ """Assemble staged input for the immediate mandatory dispatch check."""
+ request = _assemble_loopx_turn_host_request(plan)
+ if (plan.get("schema_version") == COMPLETE_TURN_PLAN_SCHEMA_VERSION
+ and request.get("schema_version") == COMPLETE_TURN_HOST_REQUEST_SCHEMA
+ and "work_contract" in request and "turn_envelope" not in request):
+ # All assembly guards and projections still run on the staged aliases.
+ # This carrier's existing conversion only copies the top-level mapping;
+ # host_context returns no facts used by it. The caller checks this exact
+ # source with host_dispatch_context before admission or publication.
+ return captured_turn_host_input(request)
+ return _complete_loopx_turn_host_request(request)
+
+
def _bounded_public_text(
result: Mapping[str, Any],
field: str,
@@ -851,6 +877,7 @@ def _run_host(
argv: Sequence[str],
project: Path,
timeout_seconds: float | None,
+ launch_guard: Callable[[], AbstractContextManager[None]] | None = None,
) -> dict[str, Any]:
# Dispatch has checked the source and its internal effect adapter before
# journal/cadence admission. External stdin gets the same single complete
@@ -874,7 +901,10 @@ def count_stderr(text: str) -> None:
stdout_limit_bytes=HOST_RESULT_MAX_BYTES,
on_stdout=stdout.append,
on_stderr=count_stderr,
+ launch_guard=launch_guard,
)
+ except FirstPartyHostRuntimeRejected:
+ raise
except (OSError, RuntimeError, ValueError) as exc:
return {"ok": False, "reason": type(exc).__name__, "returncode": None}
if observed["outcome"] == "output_limit":
@@ -985,6 +1015,7 @@ def _host_result_stage(
confirm_start: Callable[[], None] | None = None,
usage_runtime_root: Path | None = None,
usage_goal_id: str = "",
+ host_launch_guard: Callable[[], AbstractContextManager[None]] | None = None,
) -> tuple[dict[str, Any] | None, list[str], dict[str, Any] | None]:
completed_phases = list(journal.get("completed_phases") or [])
from_journal = "typed_result" in completed_phases
@@ -1007,22 +1038,32 @@ def _host_result_stage(
confirm_start()
from ...usage_goal import observe_goal_execution
- with observe_goal_execution(
- usage_runtime_root or project,
- usage_goal_id,
- host=str((plan.get("host") or {}).get("kind") or "unknown"),
- ):
- host_observation = (
- _run_host_runner(request, runner=host_runner)
- if host_runner is not None
- else _run_host(
- request,
- argv=argv or [],
- project=project,
- timeout_seconds=timeout_seconds,
+ rejected_launch = None
+ try:
+ with observe_goal_execution(
+ usage_runtime_root or project,
+ usage_goal_id,
+ host=str((plan.get("host") or {}).get("kind") or "unknown"),
+ ):
+ host_observation = (
+ _run_host_runner(request, runner=host_runner)
+ if host_runner is not None
+ else _run_host(
+ request,
+ argv=argv or [],
+ project=project,
+ timeout_seconds=timeout_seconds,
+ launch_guard=host_launch_guard,
+ )
)
- )
- effects["host_invoked"] = True
+ except FirstPartyHostRuntimeRejected as exc:
+ # Stdio checks the Goal only before spawn. A native Host can reject
+ # after dispatch too, so retain reconciliation for other failures.
+ if host_runner is not None and exc.code != "current_turn_work_changed":
+ raise
+ rejected_launch = exc.code
+ host_observation = {"ok": False, "reason": str(exc), "failure_kind": "contract_rejected"}
+ effects["host_invoked"] = rejected_launch is None
if not host_observation.get("ok"):
failure = _host_failure(
plan,
@@ -1048,17 +1089,10 @@ def _host_result_stage(
else:
journal.pop("host_recovery", None)
persist_journal(journal)
- return (
- None,
- [],
- execution_payload(
- plan,
- journal,
- execute=True,
- replayed=False,
- effects=effects,
- ),
- )
+ terminal = execution_payload(plan, journal, execute=True, replayed=False, effects=effects)
+ if rejected_launch is not None:
+ terminal["error_code"] = rejected_launch
+ return None, [], terminal
result = dict(host_observation["value"])
completed_phases = list(TRANSACTION_PHASES[:2])
@@ -1549,6 +1583,7 @@ def run_loopx_turn_once(
admit_start: Callable[[Mapping[str, Any]], dict[str, Any]] | None = None,
confirm_start: Callable[[], None] | None = None,
goal_admission: FirstPartyHostGoalAdmission | None = None,
+ host_launch_guard: Callable[[], AbstractContextManager[None]] | None = None,
) -> dict[str, Any]:
argv, host_projection = _prepare_host_invocation(plan, host_argv=host_argv, host_runner=host_runner)
transaction_plan = (
@@ -1706,7 +1741,7 @@ def persist_source_journal(source_admission: Mapping[str, Any]) -> None:
# must not put otherwise eligible work into an interval wait.
# Saved typed results retain their original finalization path.
complete_turn_host_source(
- build_loopx_turn_host_request(capture.plan) if capture is not None else request,
+ _build_staged_dispatch_request(capture.plan) if capture is not None else request,
operation="host_dispatch_context",
)
admission = None
@@ -1820,6 +1855,7 @@ def finish_recovery(payload: dict[str, Any]) -> dict[str, Any]:
persist_journal=persist_journal,
effects=effects,
confirm_start=confirm_start if reserved_start else None,
+ host_launch_guard=host_launch_guard,
)
if terminal is not None:
return finish_recovery(terminal)
diff --git a/loopx/control_plane/work_items/interaction_contract.ts b/loopx/control_plane/work_items/interaction_contract.ts
index 84b4e4bb96..ef03a88d9b 100644
--- a/loopx/control_plane/work_items/interaction_contract.ts
+++ b/loopx/control_plane/work_items/interaction_contract.ts
@@ -59,6 +59,11 @@ function pendingAutomaticSuccessor(request: JsonObject, wait: JsonObject | null)
* This read model never relaxes an existing Turn binding or settlement fence.
* Unknown dependencies retain their bounded observation/replan policy. */
export function projectBlockedSuccessorWaitObservation(request: JsonObject): JsonObject {
+ if (request.effective_action === EffectiveAction.MONITOR_QUIET_SKIP) {
+ // The automatic heartbeat must distinguish a proven future clock from a
+ // quiet Monitor result that still counts towards the blocked-wait limit.
+ return {required: projectSchedulerArbitration(request).scheduled_recheck_after_seconds === undefined};
+ }
const wait = exactBlockedSuccessorWait(request);
return {required: !!wait && pendingAutomaticSuccessor(request, wait) === null};
}
diff --git a/loopx/control_plane/work_items/replan_semantics.ts b/loopx/control_plane/work_items/replan_semantics.ts
index 333467ebfe..894d37ba9d 100644
--- a/loopx/control_plane/work_items/replan_semantics.ts
+++ b/loopx/control_plane/work_items/replan_semantics.ts
@@ -276,12 +276,14 @@ function preserveReceiptBoundObligation(request: JsonObject): JsonObject {
![null, "open", "settlement_pending"].includes(request.replay_phase as string | null)) {
throw new EffectRuntimeRequestError("receipt-bound replan facts are malformed");
}
+ const writebackCommitted = request.replay_phase === "settlement_pending";
let original: JsonObject | null = null;
let openVisionReview: JsonObject | null = null;
let recoveredLifecycle: JsonObject | null = null;
let delta: JsonObject | null = null;
let historicalAckRecorded = false;
- for (const value of request.transition_candidates) {
+ const candidates = writebackCommitted ? [] : request.transition_candidates;
+ for (const value of candidates) {
const candidate = object(value);
const obligation = object(candidate.obligation);
if (request.guard_scoped && object(candidate.source_obligation).resolution_mode === "todo_lifecycle_settlement") {
@@ -319,7 +321,6 @@ function preserveReceiptBoundObligation(request: JsonObject): JsonObject {
break;
}
}
- const writebackCommitted = request.replay_phase === "settlement_pending";
if (delta !== null || writebackCommitted) {
const reason = writebackCommitted
? "The original Turn writeback is verified. Settle its existing quota receipt once; do not repeat work or select its successor."
diff --git a/loopx/extensions/lark/cli_resolution.py b/loopx/extensions/lark/cli_resolution.py
index 01bb1fa8c0..acac8668a6 100644
--- a/loopx/extensions/lark/cli_resolution.py
+++ b/loopx/extensions/lark/cli_resolution.py
@@ -12,6 +12,7 @@
import shutil
import subprocess
from collections.abc import Callable, Mapping, Sequence
+from contextlib import nullcontext
from dataclasses import dataclass
from pathlib import Path
from typing import Any
@@ -261,6 +262,8 @@ def run(
args: list[str],
cwd: Path | None = None,
timeout: float | None = None,
+ *,
+ dispatch_guard=None,
) -> dict[str, Any]:
if not resolution.available:
raise LarkCliUnavailableError(resolution.error_code or "lark_cli_not_installed")
@@ -272,14 +275,25 @@ def run(
env["PATH"] = os.pathsep.join(
[parent, *(part for part in current if part != parent)]
)
- completed = subprocess_run(
- args,
- cwd=str(cwd) if cwd else None,
- timeout=timeout,
- capture_output=True,
- env=env,
- text=True,
- )
+ if dispatch_guard is not None:
+ with dispatch_guard():
+ process = subprocess.Popen(
+ args, cwd=str(cwd) if cwd else None, env=env, text=True,
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+ )
+ with process:
+ try:
+ stdout, stderr = process.communicate(timeout=timeout)
+ except BaseException:
+ process.kill()
+ process.communicate()
+ raise
+ completed = subprocess.CompletedProcess(args, process.returncode, stdout, stderr)
+ else:
+ completed = subprocess_run(
+ args, cwd=str(cwd) if cwd else None, timeout=timeout,
+ capture_output=True, env=env, text=True,
+ )
return {
"returncode": completed.returncode,
"stdout": completed.stdout,
@@ -287,4 +301,14 @@ def run(
"timed_out": False,
}
+ run.dispatch_guard = True
return run
+
+
+def run_lark_command_with_dispatch_guard(runner, args, cwd, timeout, guard):
+ """Keep the native authority fence through CLI spawn, never response IO."""
+ if getattr(runner, "dispatch_guard", False) is True:
+ return runner(args, cwd, timeout, dispatch_guard=guard)
+ # Injected in-process runners execute synchronously and own no subprocess.
+ with (guard() if guard is not None else nullcontext()):
+ return runner(args, cwd, timeout)
diff --git a/loopx/extensions/lark/private_conversations.py b/loopx/extensions/lark/private_conversations.py
index 50a2de7eeb..33d14cb766 100644
--- a/loopx/extensions/lark/private_conversations.py
+++ b/loopx/extensions/lark/private_conversations.py
@@ -5,7 +5,9 @@
"""
from __future__ import annotations
+import base64
import hashlib
+import http.client
import json
import re
import subprocess
@@ -13,6 +15,7 @@
from collections.abc import Callable, Iterator, Mapping, Sequence
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime, timezone
+from functools import cached_property, partial
from itertools import tee
from pathlib import Path
from threading import Lock
@@ -25,6 +28,7 @@
)
from ...capabilities.native_chat.conversation_bindings import ChatConversationBindings
from ...capabilities.native_chat.file_discovery import FileChatDiscovery, file_change_fingerprint
+from ...chat_attachments import CHAT_IMAGE_MAX_COUNT, normalize_chat_image_attachments
from ...chat_store import CHAT_MESSAGE_SCHEMA_VERSION, ChatSessionStore, TERMINAL_TURN_STATES, _atomic_write_json, _read_json
from ...control_plane.collaboration import conversation_reply_context
from ...control_plane.effect_runtime import effect_runtime_request_scope
@@ -35,14 +39,15 @@
from .event_inbox import acknowledge_lark_event_inbox, ingest_lark_event_inbox
from .event_collector_runtime import enrich_lark_event_reply_context
from .goal_channel_transport import call, json_payload, lark_args, lark_provider_mention_identities, record_event_timing
-from .identity_shapes import LARK_APP_ID_PATTERN as APP_ID_PATTERN
+from .identity_shapes import LARK_APP_ID_PATTERN as APP_ID_PATTERN, LARK_MESSAGE_ID_PATTERN
from .inbox_reply import _message, reply_lark_event_inbox, update_lark_inbox_reply, verify_lark_inbox_reply
from .manager_context import manager_failure_reply
from .inbox_reactions import (_reconcile_outbound_input_reactions,
mark_lark_event_inbox_processing, mark_lark_event_inbox_received)
-from .outbound import present_lark_conversation_text as _presentation_text
+from .outbound import normalize_lark_outbound_text, present_lark_conversation_text as _presentation_text
from .private_images import private_message_caption, private_message_images
-from .private_delivery_discovery import PrivateDeliveryDiscovery, prepared_steward_binding_waits
+from .private_delivery_discovery import (PrivateDeliveryDiscovery, allowed_answer_image_urls, answer_image_source_matches,
+ answer_media_dispatch, historical_image_reply_matches, prepared_steward_binding_waits, resumable_answer_image_session)
from .private_ingress import PrivateSourceUnavailable, private_source_text, steward_source_preflight
from .private_ingress import project_private_reply_history as project_private_reply_history
from . import private_steward_triage as triage
@@ -54,7 +59,90 @@
# Reserve edits for a final/closing notice; continue long Turns on a new draft.
PROGRESS_EDIT_BUDGET = 12
CHAT_CONTROL_COMMANDS = {"/status": "status", "/help": "help", "/new": "new", "/stop": "stop"}
+ANSWER_IMAGE_RETRY_SECONDS = 300
+ANSWER_IMAGE_SOURCE_TIMEOUT = 5
_UNSUPPORTED_MEDIA_LABELS = {"audio": "音频", "file": "文件", "media": "视频"}
+_MARKDOWN_CODE_SPAN = re.compile(r"((?\n]+)>|([^\s<>()]+))(?:[ \t]+(?:"[^"\n]*"|'[^'\n]*'))?\s*\)""")
+
+
+def _private_answer_images(text: str) -> tuple[str, list[str]]:
+ """Remove image previews from provider prose, leaving the canonical answer alone."""
+ lines, targets = [], []
+ fence = None
+ for line in text.splitlines(keepends=True):
+ marker = re.match(r"^ {0,3}(\x60{3,}|~{3,})(.*)$", line)
+ if marker and fence is None:
+ fence = marker[1]
+ elif marker and fence and marker[1][0] == fence[0] and len(marker[1]) >= len(fence) and not marker[2].strip():
+ fence = None
+ elif not fence and not line.startswith((" ", "\t")):
+ def link(match: re.Match[str]) -> str:
+ url = match[2] or match[3]
+ if url not in targets:
+ targets.append(url)
+ caption = match[1] or f"图片 {targets.index(url) + 1}"
+ return f"[{caption}](<{url}>)" if url.startswith("https://") and " " in url else (
+ f"[{caption}]({url})" if url.startswith("https://") else caption)
+
+ start, chunks = 0, []
+ for span in _MARKDOWN_CODE_SPAN.finditer(line):
+ chunks.extend((_ANSWER_IMAGE.sub(link, line[start:span.start()]), span[0]))
+ start = span.end()
+ line = "".join((*chunks, _ANSWER_IMAGE.sub(link, line[start:])))
+ lines.append(line)
+ return "".join(lines), targets
+
+
+def _private_answer_image(url: str, *, allowed_urls: set[str], deadline: float, index: int) -> dict[str, Any]:
+ # Reuse anonymous HTTPS address pinning, redirect checks and byte limits.
+ # No local file, signed-in browser or model tool authority is involved.
+ from urllib.parse import urlsplit
+
+ from ..public_source_reader import fetch
+
+ host = urlsplit(url).hostname
+ same_host_urls = {candidate for candidate in allowed_urls if urlsplit(candidate).hostname == host}
+ _, mime, data = fetch(url, deadline=deadline, allowed_urls=same_host_urls)
+ signatures = {"image/png": b"\x89PNG\r\n\x1a\n", "image/jpeg": b"\xff\xd8\xff", "image/gif": b"GIF8"}
+ if not ((mime in signatures and data.startswith(signatures[mime]))
+ or (mime == "image/webp" and data[:4] == b"RIFF" and data[8:12] == b"WEBP")):
+ raise ValueError("unsupported original image")
+ return {"id": f"reply-image-{index}", "name": f"original-{index}.{mime.split('/')[1]}",
+ "mime_type": mime, "size": len(data), "data_url": f"data:{mime};base64," + base64.b64encode(data).decode()}
+
+
+def _read_answer_images(source: str, targets: list[str], cached: list | None,
+ delivery: Mapping[str, Any]) -> list[dict[str, Any]]:
+ if (delivery.get("started") and not delivery.get("attempt")) or (delivery.get("attempt") and cached is None):
+ raise ValueError("image delivery acknowledgement or frozen bytes are unavailable")
+ deadline = time.monotonic() + 15
+ allowed_urls = allowed_answer_image_urls(source, targets, deadline=deadline)
+ if not allowed_urls:
+ raise ValueError("image was not referenced by the original source")
+ images = normalize_chat_image_attachments([
+ _private_answer_image(url, allowed_urls=allowed_urls, deadline=deadline, index=index)
+ for index, url in enumerate(targets, 1)])
+ # Older queued bytes were fetched before redirect authorization. Keep
+ # any recorded provider intent tied to the same bytes.
+ if cached is not None and cached != images:
+ raise ValueError("unverified cached image bytes changed")
+ return images
+
+
+def _is_answer_image_followup(record: Mapping[str, Any]) -> bool:
+ if record.get("status") != "accepted" or not isinstance(record.get("answer_images"), dict):
+ return False
+ terminal = record.get("deliveries", {}).get(presentation_phase(record, "terminal"), {})
+ return terminal.get("verified") is True
+
+
+def _answer_image_retry_waits(record: Mapping[str, Any], now: float) -> bool:
+ if not _is_answer_image_followup(record):
+ return False
+ state = record["answer_images"]
+ retry_at = state.get("next_check_at")
+ return state.get("status") != "delivered" and type(retry_at) in (int, float) and retry_at > now
def _unsupported_media_label(row: Mapping[str, Any]) -> str | None:
@@ -233,6 +321,7 @@ def __init__(self, *, controller: Any, runtime_root: Path, runner: Any, cli_bin:
self.steward_callback_readiness: dict[tuple[str, str], Callable[[], bool]] = {}
self.root = controller.store.root / "lark-private-deliveries"
self._context_recovery_probe_at: dict[Path, float] = {}
+ self._historical_image_probe_at: dict[Path, float] = {}
self._delivery_discovery = PrivateDeliveryDiscovery(self.root)
self._body_discovery = PrivateDeliveryDiscovery(self.root)
self._web_projection_scan_at = 0.0
@@ -289,6 +378,10 @@ def configured_profile_apps(self, *, cli_bin: str | None = None) -> dict[str, st
apps.setdefault(app_id, app_id)
return {profile: app_id for profile, app_id in apps.items() if APP_ID_PATTERN.fullmatch(app_id)}
+ @cached_property
+ def _health_delivery_discovery(self) -> FileChatDiscovery:
+ return FileChatDiscovery()
+
@effect_runtime_request_scope()
def health(self) -> dict[str, dict[str, int]]:
health: dict[str, dict[str, int]] = {}
@@ -308,13 +401,36 @@ def observe_completed() -> None:
count(row)
completed.clear()
+ def health_fields(_path: Path, row: dict[str, Any]) -> dict[str, Any] | None:
+ # A delivered body may still be waiting for original image recovery.
+ if (row["status"] != "delivered"
+ or (row.get("historical_answer_images") or {}).get("status") == "pending"):
+ return None
+ deliveries = row.get("deliveries")
+ if not isinstance(deliveries, dict) or not all(
+ isinstance(phase, dict) for phase in deliveries.values()
+ ):
+ return None
+ # Settings uses only correlation and retry metadata. Never retain
+ # delivered bodies or use these fields for admission or sending.
+ fields = ("binding_id", "status", "request_ref", "source", "session_id", "turn_id")
+ return {key: row[key] for key in fields if key in row} | {
+ "deliveries": {key: {"started": bool(phase.get("started")), "verified": bool(phase.get("verified"))}
+ for key, phase in deliveries.items()},
+ }
+
def delivered_requests() -> Iterator[dict[str, Any]]:
- for path in [*self.root.glob("*.json"), *self.root.glob("web-projections/*.json")]:
- row = _read_json(path)
+ paths = [*self.root.glob("*.json"), *self.root.glob("web-projections/*.json")]
+ for path, row in self._health_delivery_discovery.rows(
+ self.root, paths, read=_read_json, project=health_fields,
+ ):
health.setdefault(row["binding_id"], {"pending_count": 0, "recovery_count": 0})
- if row["status"] != "delivered" and triage.pending(row):
+ if row["status"] != "delivered":
+ if triage.pending(row):
+ count(row)
+ elif (row.get("historical_answer_images") or {}).get("status") == "pending":
count(row)
- elif row["status"] == "delivered" and path.parent == self.root and row.get("session_id") and row.get("turn_id"):
+ elif path.parent == self.root and row.get("session_id") and row.get("turn_id"):
yield row
# Pair the original receipts with the current Core correlation and Turn.
@@ -343,14 +459,13 @@ def _group_source_message(self, profile: str, message_id: str) -> Mapping[str, A
return None
return _message(payload, message_id)
- def _group_source_pair(self, profile: str, message_id: str,
- root_id: str) -> tuple[Mapping[str, Any] | None, Mapping[str, Any] | None]:
- # Callers bound these IDs to an admitted topic or a frozen Core stop.
- # This is readonly scheduling, not authority; raw mget retains
- # body/ancestry/mentions and the current Core selection validates them.
- result = call(self.runner, lark_args(cli_bin=self.cli_bin, profile=profile,
+ def _original_message_pair(self, profile: str, message_id: str, other_id: str, *,
+ runner=None) -> tuple[Mapping[str, Any] | None, Mapping[str, Any] | None]:
+ # Read both admitted IDs at once. Callers verify the returned envelopes
+ # and the current Core selection before using either as authority.
+ result = call(runner or self.runner, lark_args(cli_bin=self.cli_bin, profile=profile,
tail=["api", "GET", "/open-apis/im/v1/messages/mget", "--params",
- json.dumps({"message_ids": [message_id, root_id]}), "--as", "bot"]))
+ json.dumps({"message_ids": [message_id, other_id]}), "--as", "bot"]))
payload = json_payload(result)
if result.get("returncode") != 0 or payload.get("code", 0) != 0 or payload.get("ok") is False:
return None, None
@@ -358,10 +473,17 @@ def _group_source_pair(self, profile: str, message_id: str,
items = data.get("items") if isinstance(data, Mapping) else None
if (not isinstance(items, list) or len(items) != 2
or any(not isinstance(item, Mapping) or not isinstance(item.get("message_id"), str) for item in items)
- or {item.get("message_id") for item in items} != {message_id, root_id}):
+ or {item.get("message_id") for item in items} != {message_id, other_id}):
return None, None
messages = {item["message_id"]: item for item in items}
- return messages[message_id], messages[root_id]
+ return messages[message_id], messages[other_id]
+
+ def _read_answer_image_source(self, args: list[str], cwd: Path | None, _timeout: float):
+ return self.runner(args, cwd, ANSWER_IMAGE_SOURCE_TIMEOUT)
+
+ def _group_source_pair(self, profile: str, message_id: str,
+ root_id: str) -> tuple[Mapping[str, Any] | None, Mapping[str, Any] | None]:
+ return self._original_message_pair(profile, message_id, root_id)
def _completed_stop_root(self, record: Mapping[str, Any], binding: Mapping[str, Any]) -> str | None:
"""Read scheduling only; fresh Core resolution still owns authorization."""
@@ -568,18 +690,23 @@ def sources() -> tuple[Mapping[str, Any] | None, Mapping[str, Any] | None]:
pass
return None
- def _source_verified(self, record: dict[str, Any], *, sender_identity: dict[str, str] | None = None) -> bool:
- return self._source_message(record, render_content=False, sender_identity=sender_identity) is not None
+ def _source_verified(self, record: dict[str, Any], *, sender_identity: dict[str, str] | None = None,
+ expected_text: str | None = None) -> bool:
+ message = self._source_message(record, render_content=False, sender_identity=sender_identity)
+ return message is not None and (expected_text is None or (
+ message.get("deleted") is not True and message.get("msg_type", message.get("message_type")) == "text"
+ and private_source_text(message) == expected_text))
def _source_authority(self, record: dict[str, Any], profile: str, chat_id: str,
- message_id: str) -> tuple[str | None, bool]:
+ message_id: str, *, expected_text: str | None = None) -> tuple[str | None, bool]:
"""Verify one exact outbound operation; retain no identity or grant."""
if (profile != record["profile"] or chat_id != record["event"]["chat_id"]
or message_id != record["event"]["message_id"]):
return None, False
identity: dict[str, str] = {}
record_event_timing(profile, message_id, "outbound_source_check_started")
- verified = self._source_verified(record, sender_identity=identity)
+ verified = self._source_verified(record, sender_identity=identity,
+ **({"expected_text": expected_text} if expected_text is not None else {}))
record_event_timing(profile, message_id, "outbound_source_check_returned")
return identity.get("bot_display_name"), verified
@@ -1124,7 +1251,8 @@ def _feedback(self, path: Path, record: dict[str, Any], *, inbox: Callable[[], P
def _deliver(self, path: Path, record: dict[str, Any], phase: str, text: str, *, inbox: Callable[[], Path],
edit_only: bool = False, authorization_check: Callable[[], bool] | None = None,
reply_placement: str | None = None, delivery_id: str | None = None,
- finalize_source: bool = True, image_attachments: list[dict[str, Any]] | None = None) -> bool:
+ finalize_source: bool = True, image_attachments: list[dict[str, Any]] | None = None,
+ historical_source_check: Callable[[], bool] | None = None) -> bool:
if not text:
return False
if reply_placement is not None and (record["event"].get("chat_type") != "p2p"
@@ -1133,7 +1261,10 @@ def _deliver(self, path: Path, record: dict[str, Any], phase: str, text: str, *,
if authorization_check is not None and not authorization_check():
return False
kind = phase.split(".", 1)[0]
+ if historical_source_check is not None and phase != "answer_images.historical":
+ raise ValueError("historical source verification requires a media repair")
phase = presentation_phase(record, phase)
+ expected_text = record["event"]["content"] if kind == "answer_images" else None
phase_state = record["deliveries"].get(phase, {})
if phase_state.get("verified"):
return True
@@ -1144,26 +1275,52 @@ def _deliver(self, path: Path, record: dict[str, Any], phase: str, text: str, *,
text = _presentation_text(text)
if phase_state.get("text") not in (None, text):
raise ValueError("delivery content changed after an attempt")
- if image_attachments and (kind != "web" or reply_placement != "chat_root" or edit_only):
- raise ValueError("App image projection requires its original private outbox")
+ if image_attachments and (authorization_check is None or edit_only
+ or not ((kind == "web" and reply_placement == "chat_root")
+ or (kind == "answer_images" and record["event"].get("chat_type") == "p2p"
+ and record["source"].get("private_human_message") is True))):
+ raise ValueError("Image projection requires its original private outbox")
record_event_timing(record["profile"], record["event"]["message_id"], "delivery_started")
config = inbox()
record_event_timing(record["profile"], record["event"]["message_id"], "delivery_inbox_ready")
- def image_authorized() -> bool:
- if authorization_check is None or not authorization_check():
- return False
+ def provider_images_current() -> bool:
+ if historical_source_check is not None:
+ return historical_source_check()
+ source = record["event"]
+ ref = source["message_id"]
+ args = lark_args(cli_bin=self.cli_bin, profile=record["profile"],
+ tail=["api", "GET", f"/open-apis/im/v1/messages/{ref}", "--as", "bot"])
+ reply = call(self._read_answer_image_source, args)
+ payload = json_payload(reply)
+ return (reply.get("returncode") == 0 and payload.get("code", 0) == 0 and payload.get("ok") is not False
+ and answer_image_source_matches(_message(payload, ref), source))
+
+ def verified_source() -> bool:
+ if historical_source_check is not None:
+ return historical_source_check()
name, verified = self._source_authority(record, record["profile"],
- record["event"]["chat_id"], record["event"]["message_id"])
+ record["event"]["chat_id"], record["event"]["message_id"], expected_text=expected_text)
return bool(name) and verified
+ def image_authorized() -> bool:
+ # A provider read can overlap Stop, Close, or an edit to the old reply.
+ return authorization_check is not None and (provider_images_current() and authorization_check()
+ if kind == "answer_images" else authorization_check() and verified_source())
+
image_keys = ()
if image_attachments:
+ resource_scope = (f"{record['binding_id']}:{record['request_ref']}:{phase}"
+ if kind == "answer_images" else record["binding_id"])
image_keys = uploaded_chat_images(root=self.runtime_root, profile=record["profile"],
- provider_ref=record["binding_id"], attachments=image_attachments, private_transport=self,
- before_upload=image_authorized, verify_only=bool(phase_state.get("started") or phase_state.get("attempt")))
+ provider_ref=resource_scope, attachments=image_attachments, private_transport=self,
+ before_upload=image_authorized, dispatch_source_check=provider_images_current,
+ verify_only=bool(phase_state.get("started") or phase_state.get("attempt")),
+ dispatch_guard=partial(answer_media_dispatch, self.core, record) if kind == "answer_images" else None)
if phase_state.get("image_keys", list(image_keys)) != list(image_keys):
raise ValueError("App image delivery resources changed after an attempt")
phase_state["image_keys"] = list(image_keys)
+ record["deliveries"][phase] = phase_state
+ _atomic_write_json(path, record)
kwargs = dict(project=self.runtime_root, config_path=config,
message_id=record["event"]["message_id"], text=text, runner=self._reply_runner,
**({"image_keys": image_keys} if image_keys else {}),
@@ -1171,7 +1328,7 @@ def image_authorized() -> bool:
**({"delivery_id": delivery_id} if delivery_id is not None else {}),
finalize_reactions=finalize_source and not edit_only and kind not in {"admission", "progress", "periodic_report"} and not (kind == "terminal" and record.get("commission_resources")),
source_authority_verifier=lambda profile, chat, message: (
- self._source_authority(record, profile, chat, message)
+ self._source_authority(record, profile, chat, message, expected_text=expected_text)
if authorization_check is None or authorization_check() else (None, False)))
progress = record["deliveries"].get(presentation_phase(record, "progress")) or {}
stream = presentation_stream(record)
@@ -1188,13 +1345,10 @@ def image_authorized() -> bool:
raise ValueError("a new private message cannot replace an existing source reply")
def before_send(_intent: str) -> dict[str, bool]:
- # Provider preview is external IO. Recheck the original source and
- # current Core grant afterwards, including every resumed rich part.
+ # Recheck source and grant after preview, including resumed parts.
if authorization_check is not None and not authorization_check():
return {"continue_delivery": False}
- name, verified = self._source_authority(record, record["profile"],
- record["event"]["chat_id"], record["event"]["message_id"])
- if not name or not verified or (authorization_check is not None and not authorization_check()):
+ if not verified_source() or (authorization_check is not None and not authorization_check()):
return {"continue_delivery": False}
phase_state.update(text=text, started=True)
record["deliveries"][phase] = phase_state
@@ -1223,7 +1377,7 @@ def edit_attempt(value: Any) -> None:
phase_state.pop(key, None)
record["deliveries"][phase] = phase_state
_atomic_write_json(path, record)
- return self._deliver(path, record, phase, text, inbox=inbox)
+ return self._deliver(path, record, phase, text, inbox=inbox, finalize_source=finalize_source)
if not edit_only and result.get("blocker") in {"provider_update_too_large", "provider_update_format_unsupported"}:
# Do not truncate long/mention-bearing final answers to fit a
# draft. Close it, then use the established full final sender.
@@ -1232,7 +1386,7 @@ def edit_attempt(value: Any) -> None:
phase_state.pop("edit_from", None)
record["deliveries"][phase] = phase_state
_atomic_write_json(path, record)
- return self._deliver(path, record, phase, text, inbox=inbox)
+ return self._deliver(path, record, phase, text, inbox=inbox, finalize_source=finalize_source)
elif phase_state.get("attempt"):
result = verify_lark_inbox_reply(**kwargs, attempt=phase_state["attempt"], resume_parts=True,
before_send=before_send)
@@ -1274,8 +1428,224 @@ def attempt(value: Any) -> None:
record_event_timing(record["profile"], record["event"]["message_id"], "delivery_returned")
return bool(phase_state["verified"])
+ def _deliver_completed_answer(self, path: Path, record: dict[str, Any], text: str, *,
+ inbox: Callable[[], Path]) -> bool:
+ phase = presentation_phase(record, "terminal")
+ media_phase = "answer_images." + phase
+ old = record["deliveries"].get(phase) or {}
+ if old.get("started") and not record.get("answer_images"):
+ return self._deliver(path, record, "terminal", text, inbox=inbox)
+ if record["event"].get("chat_type") != "p2p" or record["source"].get("private_human_message") is not True:
+ return self._deliver(path, record, "terminal", text, inbox=inbox)
+ body, targets = _private_answer_images(text)
+ if not targets:
+ return self._deliver(path, record, "terminal", text, inbox=inbox)
+
+ def authorized() -> bool:
+ native = self.core.read_request(record["request_ref"])
+ same_request = all(native.get(key) == record[key] for key in ("binding_id", "source", "session_id", "turn_id"))
+ if native.get("status") != "accepted" or not same_request:
+ return False
+ turn = self.core.controller.store.load_turn(native["session_id"], native["turn_id"])
+ return resumable_answer_image_session(self.core, native, turn) is not None
+
+ state = record.setdefault("answer_images", {})
+ # Hold the source and the exact completed body fixed across retries.
+ digest = hashlib.sha256(text.encode()).hexdigest()
+ if state.setdefault("answer_sha256", digest) != digest:
+ return False
+ if old.get("verified") is not True:
+ state.setdefault("status", "pending")
+ _atomic_write_json(path, record)
+ self._deliver(path, record, "terminal", body + "\n\n原图会另发;有链接的图名可打开原始来源。",
+ inbox=inbox, finalize_source=False)
+ # Release the body worker before the pending image work.
+ return False
+ if state.get("status") == "delivered" and record["deliveries"].get(media_phase, {}).get("images_verified") is True:
+ return True
+ now = datetime.now(timezone.utc).timestamp()
+ if _answer_image_retry_waits(record, now):
+ return False
+ state.update(status="pending", next_check_at=now + ANSWER_IMAGE_RETRY_SECONDS)
+ _atomic_write_json(path, record)
+ try:
+ event = record["event"]
+ origin = event.get("content")
+ if (len(targets) > CHAT_IMAGE_MAX_COUNT or event.get("message_type") != "text"
+ or not isinstance(origin, str) or not authorized() or not self._source_authority(
+ record, record["profile"], event["chat_id"], event["message_id"], expected_text=origin)[1]):
+ raise ValueError("answer image source unavailable or not authorized")
+ images = state.get("attachments")
+ if images is None or state.get("source_urls_verified") is not True:
+ images = state["attachments"] = _read_answer_images(origin, targets, images,
+ record["deliveries"].get(media_phase, {}))
+ state["source_urls_verified"] = True
+ _atomic_write_json(path, record)
+ if self._deliver(path, record, media_phase, "原图(按正文顺序)", inbox=inbox,
+ authorization_check=authorized, finalize_source=False,
+ image_attachments=images):
+ if self.reaction_feedback and not _reconcile_outbound_input_reactions(
+ project=self.runtime_root, config_path=inbox(), message_id=record["event"]["message_id"],
+ processing=False, complete=True, before_write=authorized, runner=self._reply_runner).get("ok"):
+ return False
+ state.update(status="delivered")
+ state.pop("attachments", None)
+ _atomic_write_json(path, record)
+ return True
+ except (KeyError, ValueError, OSError, http.client.HTTPException):
+ pass
+ state["status"] = "unverified"
+ _atomic_write_json(path, record)
+ self._deliver(path, record, "answer_image_notice." + phase,
+ "正文已送达,图片尚未核验送达。有链接的图名可打开原始来源。",
+ inbox=inbox, authorization_check=authorized, finalize_source=False)
+ return False
+
+ def _historical_image_source(self, record: Mapping[str, Any], plan: Mapping[str, Any]) -> dict:
+ event = record["event"]
+ terminal = record["deliveries"]["terminal"]
+ if (record["status"] != "delivered" or record.get("context_recovery")
+ or event.get("chat_type") != "p2p" or event.get("message_type") != "text"
+ or record["source"].get("private_human_message") is not True
+ or record["binding_id"] != plan["binding_id"] or record["turn_id"] != plan["turn_id"]
+ or terminal.get("verified") is not True or terminal.get("image_keys")
+ or terminal.get("attempt", {}).get("message_ref") != plan["message_id"]):
+ raise ValueError("original text-only reply unavailable")
+ native = self.core.read_request(record["request_ref"])
+ if (native.get("status") != "accepted" or native.get("origin") != "lark"
+ or native.get("command") is not None or native.get("agent_target")
+ or native.get("delivery_verified") is not True
+ or any(native.get(key) != record.get(key)
+ for key in ("request_ref", "binding_id", "source", "session_id", "turn_id"))):
+ raise ValueError("original request no longer matches")
+ turn = self.core.controller.store.load_turn(record["session_id"], record["turn_id"]) or {}
+ if (not isinstance(answer := (turn.get("response") or {}).get("message"), str)
+ or hashlib.sha256(answer.encode()).hexdigest() != plan["answer_sha256"]
+ or terminal.get("text") != _presentation_text(answer)
+ or not _private_answer_images(answer)[1]
+ or (session := resumable_answer_image_session(self.core, native, turn)) is None):
+ raise ValueError("original completed answer or its authority changed")
+ return session
+
+ def _historical_image_plan(self, record: Mapping[str, Any], plan: Mapping[str, Any]) -> dict:
+ from ..public_source_reader import public_url
+
+ if set(plan) != {"binding_id", "turn_id", "message_id", "answer_sha256", "images"}:
+ raise ValueError("invalid original image repair fields")
+ if (not isinstance(plan["message_id"], str) or not LARK_MESSAGE_ID_PATTERN.fullmatch(plan["message_id"])
+ or not isinstance(plan["answer_sha256"], str)
+ or not re.fullmatch(r"[a-f0-9]{64}", plan["answer_sha256"])
+ or not isinstance(plan["images"], list) or not 1 <= len(plan["images"]) <= CHAT_IMAGE_MAX_COUNT):
+ raise ValueError("invalid original image repair target")
+ images = []
+ for image in plan["images"]:
+ if (not isinstance(image, dict) or set(image) != {"url", "sha256"}
+ or not isinstance(image["sha256"], str) or not re.fullmatch(r"[a-f0-9]{64}", image["sha256"])
+ or not isinstance(image["url"], str)):
+ raise ValueError("each original image requires a URL and its SHA-256")
+ images.append({"url": public_url(image["url"]), "sha256": image["sha256"]})
+ if len({image["url"] for image in images}) != len(images):
+ raise ValueError("duplicate original image URL")
+ self._historical_image_source(record, plan)
+ return {**plan, "images": images}
+
+ def _load_historical_image_plan(self, path: Path, record: dict[str, Any]) -> dict | None:
+ if record.get("historical_answer_images") is not None:
+ return record["historical_answer_images"]
+ if not re.fullmatch(r"[a-f0-9]{24}", path.stem):
+ raise ValueError("invalid original request reference")
+ # This is a private local operator input, scoped to the existing receipt.
+ # It is never loaded from a project, provider message or model response.
+ spec = self.root / "historical-answer-images" / path.name
+ try:
+ with spec.open("rb") as handle:
+ raw = handle.read(40_001)
+ except FileNotFoundError:
+ return None
+ if len(raw) > 40_000:
+ raise ValueError("original image repair descriptor exceeds its size limit")
+ plan = json.loads(raw)
+ if not isinstance(plan, dict) or path.stem != record.get("request_ref"):
+ raise ValueError("invalid original image repair descriptor")
+ plan = self._historical_image_plan(record, plan)
+ state = record["historical_answer_images"] = {"plan": plan, "status": "pending"}
+ _atomic_write_json(path, record)
+ return state
+
+ def _reconcile_historical_answer_images(self, path: Path) -> int:
+ with exclusive_file_lock(path, operation="deliver_private_chat_request"):
+ record = _read_json(path)
+ try:
+ state = self._load_historical_image_plan(path, record)
+ except (KeyError, ValueError, OSError):
+ return 0
+ now = datetime.now(timezone.utc).timestamp()
+ if not state or state.get("status") != "pending" or state.get("next_check_at", 0) > now:
+ return 0
+ state["next_check_at"] = now + ANSWER_IMAGE_RETRY_SECONDS
+ _atomic_write_json(path, record)
+ plan = state["plan"]
+ try:
+ session = self._historical_image_source(record, plan)
+ selected = self.bindings.resolve(binding_id=plan["binding_id"], session=session,
+ session_context=session.get("project_context") or session.get("steward_context"),
+ include_observation=True, **record["source"])
+ event = record["event"]
+ observation = selected["observation"]
+ if selected["binding"]["transport_ref"] != record["profile"]:
+ return 0
+ original_text = normalize_lark_outbound_text(
+ record["deliveries"]["terminal"]["text"], limit=None, preserve_format=True)
+ def original_pair_matches() -> bool:
+ source, reply = self._original_message_pair(record["profile"], event["message_id"], plan["message_id"],
+ runner=self._read_answer_image_source)
+ return answer_image_source_matches(source, event) and historical_image_reply_matches(
+ reply, event["chat_id"], observation, original_text)
+
+ if not original_pair_matches():
+ return 0
+ phase = "answer_images.historical"
+ attachments = state.get("attachments")
+ recheck_sources = attachments is None or state.get("source_urls_verified") is not True
+ if recheck_sources:
+ attachments = _read_answer_images(event["content"], [item["url"] for item in plan["images"]],
+ attachments, record["deliveries"].get(phase, {}))
+ digests = [hashlib.sha256(base64.b64decode(
+ picture["data_url"].split(",", 1)[1], validate=True)).hexdigest() for picture in attachments]
+ if digests != [item["sha256"] for item in plan["images"]]:
+ return 0
+ if recheck_sources:
+ state.update(attachments=attachments, source_urls_verified=True)
+ _atomic_write_json(path, record)
+ def authorized() -> bool:
+ try:
+ return (selected["binding"] in self.bindings.read()["bindings"]
+ and bool(self._historical_image_source(record, plan)))
+ except (KeyError, OSError, ValueError):
+ return False
+
+ if not self._deliver(path, record, phase, "原图(按原文顺序)",
+ inbox=lambda: self._inbox(record, observation=observation),
+ authorization_check=authorized, historical_source_check=original_pair_matches,
+ finalize_source=False,
+ reply_placement="chat_root", image_attachments=attachments):
+ return 0
+ media = record["deliveries"][phase]
+ output = call(self.runner, lark_args(cli_bin=self.cli_bin, profile=record["profile"], tail=[
+ "im", "+messages-mget", "--message-ids", media["attempt"]["message_ref"],
+ "--as", "bot", "--no-reactions", "--format", "json"]))
+ sent = _message(json_payload(output), media["attempt"]["message_ref"]) if output.get("returncode") == 0 else None
+ if not historical_image_reply_matches(sent, event["chat_id"], observation, media["text"], tuple(media["image_keys"])):
+ return 0
+ except (KeyError, ValueError, OSError, http.client.HTTPException):
+ return 0
+ state.update(status="delivered")
+ state.pop("attachments", None)
+ _atomic_write_json(path, record)
+ return 1
+
def _edit_progress(self, path: Path, record: dict[str, Any], text: str, *, inbox: Callable[[], Path], finalizing: bool = False) -> bool:
- text = _presentation_text(text)
+ text = _presentation_text(_private_answer_images(text)[0] if record["event"].get("chat_type") == "p2p" else text)
progress = record["deliveries"].get(presentation_phase(record, "progress")) or {}
if not progress.get("verified"):
return self._deliver(path, record, "progress", text, inbox=inbox)
@@ -1453,7 +1823,7 @@ def inbox() -> Path:
def pending_body_paths(self) -> list[Path]:
"""Find unsent bodies without waiting for historical recovery probes."""
paths = [path for path, row, _ in self._body_discovery.rows()
- if row["status"] != "delivered" and triage.pending(row)]
+ if row["status"] != "delivered" and triage.pending(row) and not _is_answer_image_followup(row)]
return [*paths, *self._pending_web_projection_paths()]
@effect_runtime_request_scope()
@@ -1461,8 +1831,16 @@ def pending_delivery_paths(self) -> list[Path]:
"""The durable transport store is the queue; no in-memory admission."""
paths = []
recovery_candidate = self.core.context_recovery_observer()
+ now = datetime.now(timezone.utc).timestamp()
+ planned_repairs = {item.name for item in (self.root / "historical-answer-images").glob("*.json")}
for path, row, repairs in self._delivery_discovery.rows():
- if row["status"] != "delivered" and triage.pending(row):
+ if row["status"] != "delivered":
+ if triage.pending(row) and not _answer_image_retry_waits(row, now):
+ paths.append(path)
+ elif row.get("historical_images_due_at") is not None and row["historical_images_due_at"] <= now:
+ paths.append(path)
+ elif (not row.get("historical_images_admitted") and path.name in planned_repairs
+ and time.monotonic() >= self._historical_image_probe_at.get(path, 0)):
paths.append(path)
elif (row.get("session_id") and row.get("turn_id")
and time.monotonic() >= self._context_recovery_probe_at.get(path, 0)):
@@ -1480,7 +1858,10 @@ def is_recovery_path(self, path: Path) -> bool:
A verified old notice may need a native recovery probe, but must not
precede an unsent body just because both have zero worker attempts.
"""
- return path.parent != self.root / "web-projections" and _read_json(path).get("status") == "delivered"
+ if path.parent == self.root / "web-projections":
+ return False
+ row = _read_json(path)
+ return row.get("status") == "delivered" or _is_answer_image_followup(row)
def _pending_web_projection_paths(self) -> list[Path]:
paths = []
@@ -1916,6 +2297,15 @@ def reconcile_request(self, path: Path) -> int:
row = _read_json(path)
if "triage" in row:
return triage.reconcile(self, path)
+ historical_images = row.get("historical_answer_images")
+ if (row.get("status") == "delivered" and (
+ (historical_images or {}).get("status") == "pending"
+ or (historical_images is None and (self.root / "historical-answer-images" / path.name).is_file()))):
+ if historical_images is None:
+ self._historical_image_probe_at[path] = time.monotonic() + 60
+ return self._reconcile_historical_answer_images(path)
+ if _answer_image_retry_waits(row, datetime.now(timezone.utc).timestamp()):
+ return 0
record_event_timing(row["profile"], row["event"]["message_id"], "reconciliation_started")
if row["status"] == "delivered" and row.get("session_id") and row.get("turn_id"):
# Discovery may find more candidates than the shared pool can run.
@@ -2012,6 +2402,7 @@ def reconcile_request(self, path: Path) -> int:
config: Path | None = None
selected: dict[str, Any] | None = None
control_receipt_first = False
+ completed_answer = False
def inbox() -> Path:
nonlocal config
@@ -2095,6 +2486,7 @@ def inbox() -> Path:
"本次执行超时,原会话已保留;请发送 /status 查看状态后再决定是否重试。" if turn["status"] == "timed_out" else
"本次未能完成,原会话已保留。")
if turn["status"] == "completed":
+ completed_answer = True
response = self.core.todo_confirmation_reply(record["request_ref"], turn.get("response") or {})
else:
if record["status"] != "rejected" and not control_receipt_first:
@@ -2103,7 +2495,8 @@ def inbox() -> Path:
or _command_text(str(record.get("response_code") or "")) or str(record.get("response") or ""))
if record.get("status_snapshot"):
response = _status_text(record["status_snapshot"], help_requested=native.get("command") == "help")
- if self._deliver(path, record, "terminal", response, inbox=inbox):
+ if (self._deliver_completed_answer(path, record, response, inbox=inbox) if completed_answer else
+ self._deliver(path, record, "terminal", response, inbox=inbox)):
if control_receipt_first:
# The verified result is the useful control receipt.
# Preserve the usual idempotent reaction, after it.
diff --git a/loopx/extensions/lark/private_delivery_discovery.py b/loopx/extensions/lark/private_delivery_discovery.py
index 68f14e5bf9..67d54f423e 100644
--- a/loopx/extensions/lark/private_delivery_discovery.py
+++ b/loopx/extensions/lark/private_delivery_discovery.py
@@ -1,14 +1,135 @@
-"""Read-only scheduling hints for the existing private-message reply worker."""
+"""Read-only source and scheduling checks for private-message delivery."""
from __future__ import annotations
-from collections.abc import Iterator
+import json
+import re
+from collections.abc import Callable, Iterator, Mapping
+from contextlib import ExitStack, contextmanager
from pathlib import Path
-from typing import Any, cast
+from typing import TYPE_CHECKING, Any, cast
from ...chat_store import RESUMABLE_SESSION_STATES, ChatSessionStore, _read_json
-from ...capabilities.native_chat.file_discovery import FileChatDiscovery, file_change_fingerprint
+from ...capabilities.native_chat.file_discovery import FileChatDiscovery, external_source_route_path, file_change_fingerprint
+from ...file_lock import try_exclusive_file_lock
+from .presentation.markdown_post import lark_markdown_readback_matches
+from .private_ingress import private_source_text
from .private_progress import legacy_recovery_repairs
+if TYPE_CHECKING:
+ from ...capabilities.native_chat.external_conversations import ChatExternalConversations
+
+
+_SOURCE_URL = re.compile(r"https://[^\s<>()\[\]\",。;!?]+")
+
+
+def allowed_answer_image_urls(source: str, targets: list[str], *, deadline: float) -> set[str]:
+ """Authorize exact owner URLs and images listed by the original same-host article."""
+ from urllib.parse import urlsplit
+
+ from ..public_source_reader import article, public_url
+
+ def candidate(url: str) -> str | None:
+ try:
+ return public_url(url) if not urlsplit(url).query else None
+ except ValueError:
+ return None
+
+ owner_links = {url for link in _SOURCE_URL.findall(source) if (url := candidate(link))}
+ wanted = {candidate(target) for target in targets}
+ if None in wanted or not owner_links:
+ return set()
+ if wanted <= owner_links:
+ return owner_links
+ if len(owner_links) != 1:
+ return set()
+ [original] = owner_links
+ host = urlsplit(original).hostname
+ if any(urlsplit(url).hostname != host for url in wanted - owner_links):
+ return set()
+ observed = article(original, deadline=deadline, allowed_urls={original})
+ if observed.get("ok") is not True:
+ return set()
+ listed = {url for item in observed["images"]
+ if (url := candidate(item["url"])) and urlsplit(url).hostname == host}
+ allowed_urls = owner_links | listed
+ return allowed_urls if wanted <= allowed_urls else set()
+
+
+def answer_image_source_matches(message: Mapping | None, event: Mapping) -> bool:
+ if message is None or message.get("deleted") is True or message.get("chat_id") != event["chat_id"]:
+ return False
+ sender = message.get("sender")
+ return (isinstance(sender, Mapping) and sender.get("sender_type") == "user"
+ and bool(event["sender_id"]) and sender.get("id") == event["sender_id"]
+ and message.get("msg_type", message.get("message_type")) == "text"
+ and private_source_text(message) == event["content"])
+
+
+def historical_image_reply_matches(message: Mapping | None, chat_id: str, observation: Mapping,
+ text: str, keys: tuple[str, ...] = ()) -> bool:
+ if message is None or message.get("deleted") is True or message.get("chat_id") != chat_id:
+ return False
+ sender = message.get("sender")
+ if (not isinstance(sender, Mapping) or sender.get("sender_type") != "app"
+ or not sender.get("id") or sender["id"] not in (observation["bot_app_id"], observation["bot_open_id"])):
+ return False
+ raw_body = message.get("body")
+ if isinstance(raw_body, Mapping) and isinstance(raw_body.get("content"), str):
+ try:
+ body = json.loads(raw_body["content"])
+ except ValueError:
+ return False
+ # Raw mget keeps authored Markdown in content_v2 and a rendered preview
+ # in content. Match the authored body through the existing wire verifier.
+ if isinstance(body, Mapping) and "content_v2" in body:
+ locale = {"title": body.get("title", ""), "content": body["content_v2"]}
+ message = {**message, "body": {"content": {"zh_cn": locale}}}
+ return lark_markdown_readback_matches(text=text, message=message, image_keys=keys)
+
+
+def resumable_answer_image_session(
+ core: ChatExternalConversations, request: dict[str, Any], turn: dict[str, Any] | None
+) -> dict[str, Any] | None:
+ """Apply source controls before reading the current Session."""
+ store = core.controller.store
+ session_id, turn_id = request["session_id"], request["turn_id"]
+ if (turn is None or turn.get("status") != "completed"
+ or core._source_stop_observed(request, turn, store.events_after(session_id, turn_id, None))):
+ return None
+ session = store.load_session(session_id)
+ return session if session is not None and session.get("status") in RESUMABLE_SESSION_STATES else None
+
+
+@contextmanager
+def answer_media_dispatch(core: ChatExternalConversations, request: dict[str, Any],
+ source_is_current: Callable[[], bool]) -> Iterator[None]:
+ """Check the provider source at dispatch under the original local controls."""
+ store = core.controller.store
+ session_id, turn_id = request["session_id"], request["turn_id"]
+ route = external_source_route_path(core.root, request["binding_id"], request["source"]["source_ref"])
+ # Ingress takes the route before its binding and Session operations.
+ # Contention leaves the original control writer free to finish first.
+ with ExitStack() as fences:
+ for path in (route, core.bindings.path, store._session_path(session_id)):
+ if fences.enter_context(try_exclusive_file_lock(path, operation="dispatch_external_answer_media")) is None:
+ raise ValueError("original media authority is being updated")
+ if not any(binding.get("enabled") is True and binding["binding_id"] == request["binding_id"]
+ and binding["transport_ref"] == request["profile"] for binding in core.bindings.read()["bindings"]):
+ raise ValueError("original media binding unavailable")
+ native = core.read_request(request["request_ref"])
+ if (native.get("status") != "accepted"
+ or any(native.get(key) != request[key] for key in ("binding_id", "source", "session_id", "turn_id"))
+ or resumable_answer_image_session(core, native, store.load_turn(session_id, turn_id)) is None):
+ raise ValueError("original Session or source no longer authorizes an upload")
+ # Feishu has no conditional image upload tied to a message revision.
+ # Do the final provider read here, with these local locks held through
+ # CLI start. Edits made later are checked again before posting the images.
+ if not source_is_current():
+ raise ValueError("original upload source changed")
+ if resumable_answer_image_session(core, native, store.load_turn(session_id, turn_id)) is None:
+ raise ValueError("original Session or source changed during upload authorization")
+ yield
+
def prepared_steward_binding_waits(
request: dict[str, Any], bindings: list[dict[str, Any]], store: ChatSessionStore
@@ -85,6 +206,11 @@ def _observe(self, path: Path) -> tuple[dict[str, Any], bool]:
("status", "request_ref", "binding_id", "session_id", "turn_id")}
view.update(agent_target=bool(row.get("agent_target")),
event={"chat_type": row.get("event", {}).get("chat_type")})
+ historic_images = row.get("historical_answer_images") or {}
+ if historic_images:
+ view["historical_images_admitted"] = True
+ if historic_images.get("status") == "pending":
+ view["historical_images_due_at"] = historic_images.get("next_check_at", 0)
if before is not None and file_change_fingerprint(path) == before:
self._completed[path] = (before, view, repairs)
return view, repairs
diff --git a/loopx/extensions/lark/return_files.py b/loopx/extensions/lark/return_files.py
index e503900a57..d5f652ace6 100644
--- a/loopx/extensions/lark/return_files.py
+++ b/loopx/extensions/lark/return_files.py
@@ -2,14 +2,18 @@
from __future__ import annotations
-import hashlib
import base64
+import errno
+import hashlib
import json
import re
import subprocess
+from contextlib import ExitStack, contextmanager
+from functools import partial
from pathlib import Path
from tempfile import TemporaryDirectory
+from .cli_resolution import run_lark_command_with_dispatch_guard
from .inbox_reply import _default_runner, _json_object, _message
from .outbound import normalize_lark_outbound_text
from .presentation.markdown_post import lark_markdown_readback_attachment_keys, lark_markdown_readback_matches
@@ -24,12 +28,31 @@
# shapes are stated once here instead of being rebuilt at each use site.
RESULT_FILE_KEY_PATTERN = re.compile(r"file_[A-Za-z0-9_-]{1,240}")
CHAT_IMAGE_KEY_PATTERN = re.compile(r"img_[A-Za-z0-9_-]{1,240}")
+LARK_APP_SCOPE_NOT_APPLIED = 99991672
+
+def _image_upload_scope_rejected(result):
+ """Recognize the CLI's explicit App rejection before resource creation."""
+ if result.get("returncode") != 3 or str(result.get("stdout") or "").strip():
+ return False
+ rejected = _json_object(result.get("stderr"))
+ error = rejected.get("error")
+ return (
+ rejected.get("ok") is False and rejected.get("identity") == "bot"
+ and isinstance(error, dict) and error.get("identity") == "bot"
+ and error.get("type") == "authorization"
+ and error.get("subtype") == "app_scope_not_applied"
+ and error.get("code") == LARK_APP_SCOPE_NOT_APPLIED
+ )
-def _call(args, cwd, *, runner, private_transport):
+
+def _call(args, cwd, *, runner, private_transport, dispatch_guard=None):
try:
if private_transport is not None:
- return private_transport.runner([private_transport.cli_bin, *args[1:]], cwd, 30)
+ command = [private_transport.cli_bin, *args[1:]]
+ if dispatch_guard is not None:
+ return run_lark_command_with_dispatch_guard(private_transport.runner, command, cwd, 30, dispatch_guard)
+ return private_transport.runner(command, cwd, 30)
return runner(args) if runner is not None else _default_runner(args, cwd=cwd)
except (OSError, subprocess.SubprocessError):
return {"returncode": 1}
@@ -59,20 +82,56 @@ def _chat_images(attachments):
def uploaded_chat_images(*, root, profile, provider_ref, attachments, private_transport,
- before_upload, verify_only=False):
+ before_upload, verify_only=False, dispatch_guard=None, dispatch_source_check=None):
"""Reuse resource IO; never regenerate an unknown upload or sent intent."""
+ if dispatch_guard is not None and dispatch_source_check is None:
+ raise ValueError("guarded image upload requires current source verification")
return _uploaded_resources(root=root, profile=profile, provider_ref=provider_ref,
attachments=_chat_images(attachments), cache_dir="lark-chat-images", resource="images",
key_field="image_key", key_pattern=CHAT_IMAGE_KEY_PATTERN,
read_bytes=lambda image: image["raw"],
upload_data=lambda _image: {"image_type": "message"}, runner=None,
private_transport=private_transport, before_upload=before_upload, verify_only=verify_only,
- fence_upload=True)
+ fence_upload=True, dispatch_guard=dispatch_guard, dispatch_source_check=dispatch_source_check)
+
+
+def _restore_unstarted_upload(cache, saved):
+ if saved is None:
+ cache.unlink()
+ else:
+ _write(cache, saved)
+
+
+@contextmanager
+def _authorize_upload_dispatch(guard, source_check, cache, saved, *, guards_spawn):
+ unstarted = False
+ try:
+ with ExitStack() as authority:
+ try:
+ authority.enter_context(guard(source_check))
+ except (KeyError, OSError, ValueError) as exc:
+ unstarted = True
+ raise ValueError("original upload authority unavailable") from exc
+ try:
+ yield
+ except OSError as exc:
+ # The native guard surrounds Popen only. EMFILE during pipe
+ # setup or exec leaves no CLI running. A synchronous runner
+ # can instead raise the same error after a provider write.
+ unstarted = guards_spawn and exc.errno in (
+ errno.ENOENT, errno.ENOTDIR, errno.EACCES, errno.EPERM, errno.ENOEXEC, errno.EMFILE,
+ )
+ raise
+ finally:
+ # The resource lock is still held. Release the authority handles
+ # first so restoring a previous rejection can open its temp file.
+ if unstarted:
+ _restore_unstarted_upload(cache, saved)
def _uploaded_resources(*, root, profile, provider_ref, attachments, cache_dir, resource,
key_field, key_pattern, read_bytes, upload_data, runner, private_transport,
- before_upload, verify_only, fence_upload=False):
+ before_upload, verify_only, fence_upload=False, dispatch_guard=None, dispatch_source_check=None):
keys = []
for attachment in attachments:
identity = {"profile": profile, "provider_ref": provider_ref,
@@ -81,10 +140,14 @@ def _uploaded_resources(*, root, profile, provider_ref, attachments, cache_dir,
with exclusive_file_lock(cache):
saved = _read(cache) if cache.exists() else None
if saved is not None:
- if saved.get("identity") != identity or not key_pattern.fullmatch(str(saved.get(key_field, ""))):
+ if saved.get("identity") != identity:
+ raise ValueError("resource upload identity conflict or acknowledgement unavailable")
+ if key_pattern.fullmatch(str(saved.get(key_field, ""))):
+ keys.append(saved[key_field])
+ continue
+ # Prior timeouts and missing acknowledgements remain fenced.
+ if not (fence_upload and saved == {"identity": identity, "upload_rejected_scope": LARK_APP_SCOPE_NOT_APPLIED}):
raise ValueError("resource upload identity conflict or acknowledgement unavailable")
- keys.append(saved[key_field])
- continue
if verify_only:
raise ValueError("sent result file resource record unavailable")
raw = read_bytes(attachment)
@@ -103,9 +166,20 @@ def _uploaded_resources(*, root, profile, provider_ref, attachments, cache_dir,
raise ValueError("result file return authority revoked")
if fence_upload:
_write(cache, {"identity": identity, "upload_started": True})
- uploaded = _call(args, cwd, runner=runner, private_transport=private_transport)
+ guard = None
+ if dispatch_guard is not None:
+ guard = partial(
+ _authorize_upload_dispatch, dispatch_guard, dispatch_source_check, cache, saved,
+ guards_spawn=getattr(private_transport.runner, "dispatch_guard", False) is True,
+ )
+ uploaded = _call(args, cwd, runner=runner, private_transport=private_transport,
+ dispatch_guard=guard)
+ if fence_upload and _image_upload_scope_rejected(uploaded):
+ _write(cache, {"identity": identity, "upload_rejected_scope": LARK_APP_SCOPE_NOT_APPLIED})
+ raise ValueError("result file upload unavailable")
payload = _json_object(uploaded.get("stdout"))
- key = (payload.get("data") or {}).get(key_field)
+ data = payload.get("data")
+ key = data.get(key_field) if isinstance(data, dict) else None
if uploaded.get("returncode") != 0 or payload.get("ok") is False or not key_pattern.fullmatch(str(key or "")):
raise ValueError("result file upload unavailable")
_write(cache, {"identity": identity, key_field: key})
diff --git a/loopx/extensions/public_source_reader.py b/loopx/extensions/public_source_reader.py
index 8ee09b60fb..beb90b44bd 100644
--- a/loopx/extensions/public_source_reader.py
+++ b/loopx/extensions/public_source_reader.py
@@ -21,6 +21,7 @@
import sys
import threading
import time
+from collections.abc import Collection
from html.parser import HTMLParser
from urllib.parse import urljoin, urlsplit, urlunsplit
@@ -101,11 +102,15 @@ def connect(self) -> None:
raise
-def fetch(value: str, *, deadline: float) -> tuple[str, str, bytes]:
+def fetch(value: str, *, deadline: float,
+ allowed_urls: Collection[str] | None = None) -> tuple[str, str, bytes]:
+ """Fetch anonymously, checking any caller's exact public URLs before DNS."""
url = public_url(value)
for _ in range(6):
if time.monotonic() >= deadline:
raise TimeoutError("source timeout")
+ if allowed_urls is not None and url not in allowed_urls:
+ raise ValueError("public source URL not authorized")
u = urlsplit(url)
addresses = public_addresses(u.hostname or "")
for index, address in enumerate(addresses):
@@ -236,9 +241,10 @@ def _text_page_parameters(offset: int, expected_sha256: str | None) -> None:
def article(url: str, *, deadline: float, offset: int = 0,
- expected_sha256: str | None = None) -> dict[str, object]:
+ expected_sha256: str | None = None,
+ allowed_urls: Collection[str] | None = None) -> dict[str, object]:
_text_page_parameters(offset, expected_sha256)
- observed, mime, body = fetch(url, deadline=deadline)
+ observed, mime, body = fetch(url, deadline=deadline, allowed_urls=allowed_urls)
digest = hashlib.sha256(body).hexdigest()
if expected_sha256 is not None and digest != expected_sha256:
return {"ok": False, "error": "public_source_changed"}
diff --git a/loopx/host_loop_activation.py b/loopx/host_loop_activation.py
index 20558dcc73..9943c049ee 100644
--- a/loopx/host_loop_activation.py
+++ b/loopx/host_loop_activation.py
@@ -524,6 +524,7 @@ def _heartbeat_commands(
agent_type: str,
cli_bin: str,
runtime_root: str | None = None,
+ registry: str | None = None,
agent_id: str | None,
available_capabilities: list[str] | None = None,
) -> dict[str, str]:
@@ -558,6 +559,7 @@ def _heartbeat_commands(
goal_id,
cli_bin=cli_bin,
runtime_root=runtime_root,
+ registry_path=registry,
agent_id=agent_id,
agent_scope=agent_scope,
available_capabilities=available_capabilities,
@@ -568,6 +570,7 @@ def _heartbeat_commands(
goal_id,
cli_bin=cli_bin,
runtime_root=runtime_root,
+ registry_path=registry,
agent_id=agent_id,
agent_scope=agent_scope,
available_capabilities=available_capabilities,
@@ -1241,6 +1244,7 @@ def build_host_loop_activation_packet(
goal_id: str,
cli_bin: str = "loopx",
runtime_root: str | None = None,
+ registry: str | None = None,
identity_runtime_root: str | None = None,
agent_id: str | None = None,
registered_agents: list[str] | None = None,
@@ -1266,6 +1270,7 @@ def build_host_loop_activation_packet(
agent_type=canonical,
cli_bin=cli_bin,
runtime_root=runtime_root,
+ registry=registry,
agent_id=str(selected_agent_id) if selected_agent_id else None,
available_capabilities=normalized_available_capabilities,
)
@@ -1328,6 +1333,7 @@ def build_host_loop_activation_packet(
goal_id=goal_id,
agent_id=str(selected_agent_id) if selected_agent_id else None,
runtime_root=runtime_root,
+ registry=registry,
)
elif canonical == "kiro-cli":
surface = kiro_cli_activation(commands, cli_bin)
@@ -1349,6 +1355,7 @@ def build_host_loop_activation_packet(
agent_type=canonical,
cli_bin=cli_bin,
runtime_root=runtime_root,
+ registry=registry,
agent_id=candidate,
available_capabilities=normalized_available_capabilities,
)
diff --git a/loopx/hosts/skill_facade.py b/loopx/hosts/skill_facade.py
index ef32d65f9e..bef38874b3 100644
--- a/loopx/hosts/skill_facade.py
+++ b/loopx/hosts/skill_facade.py
@@ -171,6 +171,7 @@ def antigravity_app_activation(
goal_id: str = "",
agent_id: str | None = None,
runtime_root: str | None = None,
+ registry: str | None = None,
) -> dict[str, Any]:
return skill_facade_cli_activation(
commands,
@@ -183,6 +184,7 @@ def antigravity_app_activation(
goal_id=goal_id,
agent_id=agent_id,
runtime_root=runtime_root,
+ registry=registry,
cli_bin=cli_bin,
),
)
diff --git a/loopx/project_prompt.py b/loopx/project_prompt.py
index de407238af..a76933bbaf 100644
--- a/loopx/project_prompt.py
+++ b/loopx/project_prompt.py
@@ -316,6 +316,7 @@ def render_heartbeat_prompt_command(
*,
cli_bin: str = "loopx",
runtime_root: str | Path | None = None,
+ registry_path: str | Path | None = None,
agent_id: str | None = None,
agent_scope: str = "Codex CLI /goal visible TUI loop",
body: str = "thin",
@@ -337,8 +338,11 @@ def render_heartbeat_prompt_command(
if visible_goal_host
else ""
)
+ prefix = render_cli_command_prefix(
+ cli_bin=cli_bin, runtime_root=runtime_root, registry_path=registry_path
+ )
return (
- f"{render_cli_command_prefix(cli_bin=cli_bin, runtime_root=runtime_root)} "
+ f"{prefix} "
f"heartbeat-prompt --{shell_arg(body)} "
f"--goal-id {shell_arg(goal_id)}{agent_arg}{scope_arg}{capability_args}"
f"{scheduler_args}{visible_goal_arg}" + (" --bootstrap" if bootstrap else "")
@@ -350,6 +354,7 @@ def render_heartbeat_prompt_json_command(
*,
cli_bin: str = "loopx",
runtime_root: str | Path | None = None,
+ registry_path: str | Path | None = None,
agent_id: str | None = None,
agent_scope: str = "Codex CLI /goal visible TUI loop",
body: str = "thin",
@@ -371,8 +376,11 @@ def render_heartbeat_prompt_json_command(
if visible_goal_host
else ""
)
+ prefix = render_cli_command_prefix(
+ cli_bin=cli_bin, runtime_root=runtime_root, registry_path=registry_path
+ )
return (
- f"{render_cli_command_prefix(cli_bin=cli_bin, runtime_root=runtime_root)} "
+ f"{prefix} "
f"--format json heartbeat-prompt --{shell_arg(body)} "
f"--goal-id {shell_arg(goal_id)}{agent_arg}{scope_arg}{capability_args}"
f"{scheduler_args}{visible_goal_arg}" + (" --bootstrap" if bootstrap else "")
diff --git a/loopx/quota.py b/loopx/quota.py
index 450115daaf..4dac35fb65 100644
--- a/loopx/quota.py
+++ b/loopx/quota.py
@@ -1244,6 +1244,7 @@ def record_quota_scheduler_ack(
| None = None,
operator_inbox_urgency_projector: Callable[..., dict[str, Any]] | None = None,
before_decision: Mapping[str, Any] | None = None,
+ turn_instance_id: str | None = None,
) -> dict[str, Any]:
safe_goal_id = _validate_goal_id_path_segment(str(goal_id or ""))
safe_agent_id = normalize_todo_claimed_by(agent_id)
@@ -1278,6 +1279,7 @@ def record_quota_scheduler_ack(
reason_summary=reason_summary,
use_current_hint=use_current_hint,
host_match_observed=host_match_observed,
+ turn_instance_id=turn_instance_id,
)
@@ -1642,7 +1644,7 @@ def spend_quota_slot(
"goal_id": safe_goal_id,
"reason": "effect_ref must be a non-empty string when provided",
}
- if normalized_effect_ref:
+ if normalized_effect_ref and not turn_instance_id:
if not raw_runtime_root:
return {
"ok": False,
@@ -1709,6 +1711,7 @@ def spend_quota_slot(
and not replan_obligation_id
)
settlement_readback = None
+ spend_request = {"slots": slots, "source": source}
if (
not turn_instance_id
and raw_runtime_root
@@ -1727,6 +1730,7 @@ def spend_quota_slot(
allow_unbound_binding=recover_unbound_visible_goal,
registry_path=registry_path,
goal_ref=goal_ref,
+ spend_request=spend_request,
)
inferred_result = (
settlement_readback.identity
@@ -1765,6 +1769,7 @@ def spend_quota_slot(
replan_obligation_id=replan_obligation_id,
registry_path=registry_path,
goal_ref=goal_ref,
+ spend_request=spend_request,
)
if settlement_readback is None:
raise RuntimeError("exact settlement readback unexpectedly returned not-found")
@@ -1784,6 +1789,18 @@ def spend_quota_slot(
"settlement_result": settlement_result_payload(guard_result),
}
identity = guard_result.value
+ refusal = settlement_readback.spend_request_rejection
+ if refusal is not None:
+ return {
+ "ok": False,
+ "mode": "spend-slot",
+ "dry_run": not execute,
+ "appended": False,
+ "goal_id": safe_goal_id,
+ "settlement_identity": identity.as_dict(),
+ "settlement_result": settlement_result_payload(refusal),
+ "reason": refusal.failure.reason if refusal.failure else "invalid original Turn spend",
+ }
spent_result = settlement_readback.settlement
if spent_result.failure is None:
return {
diff --git a/loopx/usage_goal.py b/loopx/usage_goal.py
index ca3d6af5b3..287b103acd 100644
--- a/loopx/usage_goal.py
+++ b/loopx/usage_goal.py
@@ -20,6 +20,9 @@
@contextmanager
def observe_goal_execution(runtime_root: Path, goal_id: str, *, host: str = "unknown") -> Iterator[None]:
+ if usage_ping._environment_suppressed():
+ yield
+ return
stop = threading.Event()
publish = None
try:
@@ -28,10 +31,7 @@ def observe_goal_execution(runtime_root: Path, goal_id: str, *, host: str = "unk
path = usage_ping.state_path()
state = json.loads(path.read_text(encoding="utf-8"))
generation = state.get("generation")
- if (goal_id and generation and state.get("consent") != "disabled"
- and os.environ.get("LOOPX_USAGE_PING") != "0"
- and os.environ.get("DO_NOT_TRACK") != "1"
- and os.environ.get("CI") != "true"):
+ if goal_id and generation and state.get("consent") != "disabled":
key = hashlib.sha256(json.dumps([generation, str(runtime_root.resolve()), goal_id]).encode()).hexdigest()
wall = time.time_ns() // 1_000_000
origin = time.monotonic()
@@ -101,12 +101,12 @@ def observe_quota_cycle(*, registry_path: Path, runtime_root: Path, goal_id: str
at: int, host: str) -> None:
"""Detach binding discovery and session metadata lookup from quota latency."""
try:
+ if usage_ping._environment_suppressed():
+ return
import sys
state = json.loads(usage_ping.state_path().read_text(encoding="utf-8"))
if state.get("consent") == "disabled" or not state.get("generation"):
return
- if os.environ.get("LOOPX_USAGE_PING") == "0" or os.environ.get("DO_NOT_TRACK") == "1" or os.environ.get("CI") == "true":
- return
usage_ping._detach({"registry": str(registry_path), "runtime": str(runtime_root),
"goal": goal_id, "agent": agent_id, "turn": turn_id,
"phase": phase, "at": at, "host": host,
@@ -155,6 +155,8 @@ def _bound_codex_session(registry_path: Path, goal_id: str, agent_id: str | None
def _dispatch_cycle(request) -> None:
+ if usage_ping._environment_suppressed():
+ return
path = Path(request["path"])
state = json.loads(path.read_text(encoding="utf-8"))
if state.get("consent") == "disabled" or state.get("generation") != request["generation"]:
diff --git a/loopx/usage_ping.py b/loopx/usage_ping.py
index 32bf72caa9..6f05f9aa3f 100644
--- a/loopx/usage_ping.py
+++ b/loopx/usage_ping.py
@@ -8,6 +8,7 @@
import sys
import tempfile
import time
+from collections.abc import Mapping
from contextvars import ContextVar
from datetime import datetime, timezone
from pathlib import Path
@@ -139,12 +140,25 @@ def control(action: str, path: Path | None = None, **fields: Any) -> dict[str, A
return payload
+def _environment_suppressed(environment: Mapping[str, str] | None = None) -> bool:
+ """Negative-only host hint; the TS owner still decides every observation."""
+ env = os.environ if environment is None else environment
+ # Match ECMAScript String.trim, including FEFF and excluding U+0085.
+ whitespace = (" \t\n\r\v\f\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005"
+ "\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000\ufeff")
+ return (
+ env.get("LOOPX_USAGE_PING", "").strip(whitespace).lower() in {"0", "false", "no", "off"}
+ or env.get("DO_NOT_TRACK", "").strip(whitespace) not in {"", "0"}
+ or env.get("CI", "").strip(whitespace).lower() not in {"", "0", "false"}
+ )
+
+
def begin(command: str) -> tuple[str, float] | None:
"""Read a small host hint; do not start a synchronous Node process on warm commands."""
_observation.set({})
# Negative-only scheduling hints for common unattended environments. These
# cannot authorize collection; TS still checks every supported switch value.
- if os.environ.get("LOOPX_USAGE_PING") == "0" or os.environ.get("DO_NOT_TRACK") == "1" or os.environ.get("CI") == "true":
+ if _environment_suppressed():
return None
# Whole-runtime migration includes this machine state. Detached observation
# would invalidate its preview or rollback receipt even with other hosts stopped.
@@ -239,6 +253,8 @@ def _pin_caller_paths(request: dict[str, Any]) -> dict[str, Any]:
def _detach(request: dict[str, Any], *, command: list[str] | None = None) -> None:
+ if _environment_suppressed():
+ return
request = _pin_caller_paths(request)
kwargs: dict[str, Any] = {"stdin": subprocess.PIPE, "stdout": subprocess.DEVNULL,
"stderr": subprocess.DEVNULL, "close_fds": True,
diff --git a/pyproject.toml b/pyproject.toml
index b128a7989d..fdc6cddab9 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -98,7 +98,7 @@ include = ["loopx*"]
"*" = ["README.md", "README.zh-CN.md", "docs/*.md", "docs/protocols/*.md"]
"loopx.capabilities.content_ops" = ["templates/*.json", "experiences/*.json"]
"loopx.capabilities.pr_review_queue" = ["experiences/*/*/*.json", "experiences/*.md"]
-"loopx.capabilities.manager_context" = ["skills/loopx-manager/SKILL.md"]
+"loopx.capabilities.manager_context" = ["*.ts", "skills/loopx-manager/SKILL.md"]
"loopx.extensions.lark" = ["extension.toml", "README.md", "docs/*.md"]
"loopx.extensions.openviking_periodic_report" = ["extension.toml"]
"loopx.extensions.openviking_semantic_preference" = ["extension.toml"]
diff --git a/tests/architecture/test_issue_fix_source_vocabulary_owner.py b/tests/architecture/test_issue_fix_source_vocabulary_owner.py
new file mode 100644
index 0000000000..88f2a9cc5e
--- /dev/null
+++ b/tests/architecture/test_issue_fix_source_vocabulary_owner.py
@@ -0,0 +1,165 @@
+"""The issue_fix source trust/freshness vocabularies are stated once.
+
+`repository_context.py` owns the closed sets every issue_fix source payload is
+validated against. `reviewer_recommendation.py` used to restate both under its
+own names, so a vocabulary change had two places to land and a drift would let
+the two validators disagree about the same `trust`/`freshness` fields.
+
+`REVIEWER_SOURCE_KINDS` stays local on purpose: reviewer sources accept only
+`maintainer_map`, a deliberately narrower set than the repository-context one.
+The same freshness triple also appears in `reward_memory/candidate_review.py`,
+a different capability outside this guard's scope.
+"""
+
+from __future__ import annotations
+
+import ast
+from pathlib import Path
+
+import pytest
+
+from loopx.capabilities.issue_fix import repository_context, reviewer_recommendation
+
+REPOSITORY_ROOT = Path(__file__).resolve().parents[2]
+OWNER_MODULE = "loopx/capabilities/issue_fix/repository_context.py"
+
+VOCABULARIES = {
+ "TRUST_LEVELS": repository_context.TRUST_LEVELS,
+ "FRESHNESS_STATES": repository_context.FRESHNESS_STATES,
+}
+
+
+def _string_set(node: ast.expr) -> frozenset[str] | None:
+ if not isinstance(node, ast.Set):
+ return None
+ values = []
+ for element in node.elts:
+ if not isinstance(element, ast.Constant) or not isinstance(element.value, str):
+ return None
+ values.append(element.value)
+ return frozenset(values)
+
+
+def test_the_vocabularies_are_stated_only_by_the_owner() -> None:
+ # A census by value, not by name: a renamed local copy is the same drift.
+ targets = {frozenset(values) for values in VOCABULARIES.values()}
+ found: dict[str, list[str]] = {}
+ package = REPOSITORY_ROOT / "loopx/capabilities/issue_fix"
+ for path in sorted(package.glob("*.py")):
+ relative = path.relative_to(REPOSITORY_ROOT).as_posix()
+ tree = ast.parse(path.read_text(encoding="utf-8"))
+ for node in ast.walk(tree):
+ if not isinstance(node, ast.Assign) or len(node.targets) != 1:
+ continue
+ target = node.targets[0]
+ if not isinstance(target, ast.Name):
+ continue
+ if _string_set(node.value) in targets:
+ found.setdefault(relative, []).append(target.id)
+ assert {f: sorted(names) for f, names in found.items()} == {
+ OWNER_MODULE: sorted(VOCABULARIES)
+ }
+
+
+def test_reviewer_sources_read_the_owner_objects() -> None:
+ assert (
+ reviewer_recommendation.REVIEWER_SOURCE_TRUST_LEVELS
+ is repository_context.TRUST_LEVELS
+ )
+ assert (
+ reviewer_recommendation.REVIEWER_SOURCE_FRESHNESS_STATES
+ is repository_context.FRESHNESS_STATES
+ )
+
+
+def _context_input(*, trust: str, freshness: str) -> dict:
+ return {
+ "schema_version": "issue_fix_repository_context_input_v0",
+ "repository_revision": "abc123",
+ "sources": [
+ {
+ "source_id": "agents-map",
+ "source_kind": "maintainer_map",
+ "reference": "AGENTS.md",
+ "trust": trust,
+ "freshness": freshness,
+ "supports": ["change_scope"],
+ }
+ ],
+ }
+
+
+def _reviewer_input(*, trust: str, freshness: str) -> dict:
+ return {
+ "schema_version": "issue_fix_reviewer_sources_input_v0",
+ "sources": [
+ {
+ "source_id": "codeowners-map",
+ "source_kind": "maintainer_map",
+ "reference": ".github/CODEOWNERS",
+ "trust": trust,
+ "freshness": freshness,
+ "observed_at": "2026-11-15T00:00:00Z",
+ "routes": [
+ {
+ "route_id": "core-route",
+ "match_kind": "path_prefix",
+ "pattern": "loopx/",
+ "primary_reviewers": ["@maintainer"],
+ "fallback_reviewers": [],
+ }
+ ],
+ }
+ ],
+ }
+
+
+def test_repository_context_accepts_every_owner_value() -> None:
+ for trust in sorted(repository_context.TRUST_LEVELS):
+ for freshness in sorted(repository_context.FRESHNESS_STATES):
+ packet = repository_context.build_issue_fix_repository_context_packet(
+ repo="loopx",
+ issue_ref="issue-1",
+ context_input=_context_input(trust=trust, freshness=freshness),
+ )
+ assert packet["ok"] is True
+ assert packet["sources"][0]["trust"] == trust
+ assert packet["sources"][0]["freshness"] == freshness
+
+
+def test_reviewer_sources_accept_every_owner_value() -> None:
+ for trust in sorted(repository_context.TRUST_LEVELS):
+ for freshness in sorted(repository_context.FRESHNESS_STATES):
+ packet = (
+ reviewer_recommendation.build_issue_fix_reviewer_recommendation_packet(
+ repo_path="/nonexistent",
+ reviewer_sources_input=_reviewer_input(
+ trust=trust, freshness=freshness
+ ),
+ )
+ )
+ assert packet["ok"] is True
+ assert packet["reviewer_source_count"] == 1
+
+
+@pytest.mark.parametrize("field", ["trust", "freshness"])
+def test_repository_context_rejects_values_outside_the_owner_set(field: str) -> None:
+ payload = _context_input(trust="verified", freshness="stale")
+ payload["sources"][0][field] = "bogus"
+ with pytest.raises(ValueError, match=field):
+ repository_context.build_issue_fix_repository_context_packet(
+ repo="loopx",
+ issue_ref="issue-1",
+ context_input=payload,
+ )
+
+
+@pytest.mark.parametrize("field", ["trust", "freshness"])
+def test_reviewer_sources_reject_values_outside_the_owner_set(field: str) -> None:
+ payload = _reviewer_input(trust="verified", freshness="stale")
+ payload["sources"][0][field] = "bogus"
+ with pytest.raises(ValueError, match=field):
+ reviewer_recommendation.build_issue_fix_reviewer_recommendation_packet(
+ repo_path="/nonexistent",
+ reviewer_sources_input=payload,
+ )
diff --git a/tests/architecture/test_posix_flag_portability.py b/tests/architecture/test_posix_flag_portability.py
index 86a8da7427..7b42b11b61 100644
--- a/tests/architecture/test_posix_flag_portability.py
+++ b/tests/architecture/test_posix_flag_portability.py
@@ -96,16 +96,13 @@ def test_no_module_exceeds_its_bare_posix_flag_ceiling() -> None:
observed = observed_ceiling()
exceeded = {
- path: {"ceiling": ceiling.get(path, 0), "observed": uses}
+ path: [f"{line}: {what}" for line, what in uses]
for path, uses in observed.items()
if len(uses) > ceiling.get(path, 0)
}
assert not exceeded, (
"bare POSIX-only os flags grew: "
- + json.dumps(
- {path: [f"{line}: {what}" for line, what in uses] for path, uses in exceeded.items()},
- indent=2,
- )
+ + json.dumps(exceeded, indent=2)
+ ". Read the flag with `getattr(os, \"\", 0)` the way the peer, completion "
"and decision-capture readers do, so a host without it refuses or degrades on its "
"own terms instead of raising AttributeError first. Raising a ceiling in "
diff --git a/tests/capabilities/test_capability_configuration_ui.py b/tests/capabilities/test_capability_configuration_ui.py
index c9eee87560..fce9397390 100644
--- a/tests/capabilities/test_capability_configuration_ui.py
+++ b/tests/capabilities/test_capability_configuration_ui.py
@@ -1,5 +1,9 @@
from __future__ import annotations
+import json
+import subprocess
+import sys
+
import pytest
from loopx.capabilities.configuration_ui import (
@@ -19,6 +23,123 @@
)
+@pytest.fixture(autouse=True)
+def isolated_native_profile_discovery(tmp_path, monkeypatch):
+ from loopx import codex_app_thread_activity
+
+ discover = codex_app_thread_activity.codex_homes
+ monkeypatch.setattr(
+ codex_app_thread_activity, "codex_homes",
+ lambda: discover(env={}, user_home=tmp_path),
+ )
+
+
+@pytest.mark.parametrize("capability_id", [
+ "context_delivery", "goal_capability_organization", "goal_storage",
+ "todo_replan_cadence", "periodic_report", "manager_runtime",
+ "steward_executor", "multi_subagent", "peer_task_coordination",
+ "explore_graph", "explore_harness", "change_quality_qualification",
+ "progress_review", "pull_request_review", "local_authority_shadow",
+ "coordination_runtime_shadow", "lark_kanban_heartbeat_sync", "reward_memory",
+ "lark_event_inbox", "unregistered-editor", "",
+])
+def test_editor_does_not_load_unselected_provider(capability_id, tmp_path):
+ source = '''import importlib.abc,json,sys
+capability=sys.argv[1]
+blocked=['loopx.capabilities.native_chat']
+if capability!='steward_executor':blocked.append('loopx.capabilities.steward_executor')
+class UnavailableProvider(importlib.abc.MetaPathFinder):
+ def find_spec(self,fullname,path=None,target=None):
+ if any(fullname==name or fullname.startswith(name+'.') for name in blocked):
+ raise ImportError('unselected provider unavailable')
+sys.meta_path.insert(0,UnavailableProvider())
+from loopx.capabilities.configuration_ui import capability_configuration_editor,resolve_capability_configuration
+editor=capability_configuration_editor(capability)
+assert editor['schema_version']=='capability_configuration_editor_v0'
+assert not any(any(name==root or name.startswith(root+'.') for root in blocked) for name in sys.modules)
+if capability=='periodic_report':
+ resolved=resolve_capability_configuration(capability,capability_default={'enabled':False,'timezone':'UTC'})
+ assert resolved['source']=='capability_default' and resolved['configuration']=={'enabled':False,'timezone':'UTC'}
+print(json.dumps(editor))
+'''
+ result = subprocess.run(
+ [sys.executable, "-I", "-c", source, capability_id],
+ cwd=tmp_path, capture_output=True, text=True, timeout=30,
+ )
+ assert result.returncode == 0, result.stderr
+ editor = json.loads(result.stdout)
+ if capability_id in ("unregistered-editor", ""):
+ assert editor == {
+ "schema_version": "capability_configuration_editor_v0",
+ "editable": False, "supported_scopes": [], "writable_scopes": [],
+ "fields": [],
+ "read_only_reason": "No Dashboard editor is registered for this capability.",
+ }
+
+
+def test_codex_editor_uses_existing_profile_owner_and_nullable_options(tmp_path):
+ from loopx.capabilities.steward_executor.machine_defaults import steward_reasoning_efforts
+
+ (tmp_path / ".codex").mkdir()
+ (tmp_path / ".codex-blue").mkdir()
+ (tmp_path / ".codex-red").mkdir()
+ (tmp_path / ".codex_bad").mkdir()
+ fields = {row["key"]: row for row in capability_configuration_editor("codex_chat")["fields"]}
+ assert fields["codex_profile"]["options"] == ["codex", "codex-blue", "codex-red"]
+ assert fields["codex_profile"]["nullable"] is True
+ assert fields["reasoning_effort"]["options"] == list(steward_reasoning_efforts())
+ assert fields["model"]["nullable"] is True
+ assert fields["service_tier"]["nullable"] is True
+
+
+@pytest.mark.parametrize("capability_id,expected", [
+ ("codex_chat", ["endpoints", "efforts", "profiles"]),
+ ("steward_executor", ["endpoints", "efforts"]),
+])
+def test_selected_editor_preserves_existing_owner_order(capability_id, expected, monkeypatch):
+ from loopx import codex_app_thread_activity
+ from loopx.capabilities.steward_executor import machine_defaults
+
+ calls = []
+
+ def observed(owner, name, label):
+ original = getattr(owner, name)
+
+ def call():
+ calls.append(label)
+ return original()
+
+ monkeypatch.setattr(owner, name, call)
+
+ observed(machine_defaults, "steward_executor_endpoints", "endpoints")
+ observed(machine_defaults, "steward_reasoning_efforts", "efforts")
+ observed(codex_app_thread_activity, "codex_homes", "profiles")
+ capability_configuration_editor(capability_id)
+ assert calls == expected
+
+
+@pytest.mark.parametrize("capability_id,provider", [
+ ("codex_chat", "native_chat"), ("steward_executor", "steward_executor"),
+])
+def test_editor_exposes_selected_provider_unavailability(capability_id, provider, tmp_path):
+ source = '''import importlib.abc,sys
+class UnavailableProvider(importlib.abc.MetaPathFinder):
+ def find_spec(self,fullname,path=None,target=None):
+ root='loopx.capabilities.'+sys.argv[2]
+ if fullname==root or fullname.startswith(root+'.'):raise ImportError('selected provider unavailable')
+sys.meta_path.insert(0,UnavailableProvider())
+from loopx.capabilities.configuration_ui import capability_configuration_editor
+try:capability_configuration_editor(sys.argv[1])
+except ImportError as error:assert str(error)=='selected provider unavailable'
+else:raise AssertionError('selected provider failure was hidden')
+'''
+ result = subprocess.run(
+ [sys.executable, "-I", "-c", source, capability_id, provider],
+ cwd=tmp_path, capture_output=True, text=True, timeout=30,
+ )
+ assert result.returncode == 0, result.stderr
+
+
def test_machine_catalog_only_uses_dashboard_supported_editor_kinds() -> None:
"""A Goal-only descriptor must not make the whole machine page unreadable."""
diff --git a/tests/capabilities/test_pr_wait_lifecycle.py b/tests/capabilities/test_pr_wait_lifecycle.py
index 89951dabed..780783adf9 100644
--- a/tests/capabilities/test_pr_wait_lifecycle.py
+++ b/tests/capabilities/test_pr_wait_lifecycle.py
@@ -249,21 +249,19 @@ def read(inspector):
@pytest.mark.parametrize("provider", ["file", "sqlite"])
def test_lost_observation_response_refreshes_cached_readiness(tmp_path, monkeypatch, provider):
- from loopx.capabilities.issue_fix import pr_wait_hook
-
registry, runtime, _, todo_id, argv = _native_wait_turn(tmp_path, monkeypatch, provider)
# Capture the old pending projection before permission is configured.
_invoke_guard(argv, "capture-pending", "--write-projection-cache")
_declare_observation(registry)
monkeypatch.setattr(pr_lifecycle, "fetch_github_pr_lifecycle_payload", lambda *a, **kw: {
"url": "https://github.com/owner/repo/pull/42", "state": "MERGED", "mergedAt": "2026-10-01T01:12:00Z"})
- original = pr_wait_hook.reconcile_pr_wait_dependencies
+ original = pr_wait_reconcile.reconcile_pr_wait_dependencies
def response_lost(**kwargs):
original(**kwargs)
raise OSError("response lost after local commit")
- monkeypatch.setattr(pr_wait_hook, "reconcile_pr_wait_dependencies", response_lost)
+ monkeypatch.setattr(pr_wait_reconcile, "reconcile_pr_wait_dependencies", response_lost)
result = _invoke_guard(argv, "lost-response", "--use-projection-cache")
readback = _observation(result)
assert readback["external_read_count"] is None and readback["state_written"] is None
@@ -291,8 +289,6 @@ def test_unavailable_permission_source_never_contacts_provider(tmp_path, monkeyp
@pytest.mark.parametrize("provider", ["file", "sqlite"])
def test_full_source_selection_is_independent_of_bounded_hook_diagnostics(tmp_path, monkeypatch, provider):
- from loopx.capabilities.issue_fix import pr_wait_hook
-
isolate_sqlite_runtime(tmp_path, monkeypatch)
lines = tuple(line for number in range(1, 501) for line in (
f"- [ ] [P2] Continue dependency {number}.",
@@ -301,7 +297,7 @@ def test_full_source_selection_is_independent_of_bounded_hook_diagnostics(tmp_pa
_, runtime, registry = _write_live_fixture(tmp_path, provider=provider, extra_agent_todo_lines=lines)
_declare_observation(registry)
calls, full_results = [], []
- original = pr_wait_hook.reconcile_pr_wait_dependencies
+ original = pr_wait_reconcile.reconcile_pr_wait_dependencies
def capture(**kwargs):
result = original(**kwargs)
@@ -312,7 +308,7 @@ def fetch(reference, **kwargs):
calls.append(reference)
return {"url": f"https://github.com/{reference['repo']}/pull/{reference['number']}", "state": "OPEN"}
- monkeypatch.setattr(pr_wait_hook, "reconcile_pr_wait_dependencies", capture)
+ monkeypatch.setattr(pr_wait_reconcile, "reconcile_pr_wait_dependencies", capture)
monkeypatch.setattr(pr_lifecycle, "fetch_github_pr_lifecycle_payload", fetch)
dispatch = extend_turn_start_dispatch({}, registry_path=registry, runtime_root=runtime,
goal_id="loopx-turn-fixture", agent_id="codex-fixture", available=None)
diff --git a/tests/control_plane/cli_output_fixture.py b/tests/control_plane/cli_output_fixture.py
index 59ef10a4a2..31e72de027 100644
--- a/tests/control_plane/cli_output_fixture.py
+++ b/tests/control_plane/cli_output_fixture.py
@@ -7,12 +7,37 @@
import json
import os
from pathlib import Path
+import re
import shlex
import shutil
import subprocess
import tempfile
+_POWERSHELL_READER_WORDS = re.compile(r"'(?:''|[^'])*'|[^'\s]+")
+_POWERSHELL_DOUBLED_QUOTE = re.compile(r"(['\u2018\u2019\u201a\u201b])\1")
+_DIRECT_POWERSHELL_READER = re.compile(r"^([A-Za-z0-9_./-]+) '")
+
+
+def _reader_argv(command: str) -> list[str]:
+ """Decode the literal-only form of the generated read command into argv."""
+ direct = _DIRECT_POWERSHELL_READER.match(command)
+ if not command.startswith("& ") and not (
+ direct and (os.name == "nt" or direct[1].lower().endswith(".ps1"))
+ ):
+ return shlex.split(command)
+ # The host renderer uses '& ' only for a quoted executable. It also
+ # selects PowerShell for .ps1 on POSIX. shlex drops its doubled quotes.
+ literal_command = command.removeprefix("& ")
+ words = _POWERSHELL_READER_WORDS.findall(literal_command)
+ assert " ".join(words) == literal_command, "unexpected syntax in the generated reader command"
+ return [
+ _POWERSHELL_DOUBLED_QUOTE.sub(r"\1", word[1:-1])
+ if word.startswith("'") and word.endswith("'") else word
+ for word in words
+ ]
+
+
def budget_fixture_path(root: Path) -> Path:
suffix = hashlib.sha256(str(root).encode("utf-8")).hexdigest()[:12]
if os.name == "nt":
@@ -61,7 +86,7 @@ def read_pinned_cli_body(command: str) -> tuple[str, int, int]:
reader_bytes = calls = 0
identity = total = digest = None
while True:
- words = shlex.split(command)
+ words = _reader_argv(command)
quota = words.index("quota")
if words[quota + 1] != "context-read":
raise AssertionError("context recovery must be a read, never another guard")
@@ -75,6 +100,10 @@ def read_pinned_cli_body(command: str) -> tuple[str, int, int]:
if identity is None:
identity = scope
assert scope == identity, ("context continuation changed its source or reader route", scope, identity)
+ if words[0].lower().endswith(".ps1"):
+ from loopx.command_invocation import command_argv
+
+ words = command_argv(Path(words[0]), words[1:])
result = subprocess.run(words, capture_output=True, text=True, encoding="utf-8",
errors="strict", check=False, timeout=30)
assert result.returncode == 0, result.stdout + result.stderr
diff --git a/tests/control_plane/test_cli_output_differential.py b/tests/control_plane/test_cli_output_differential.py
index 8cd6b165e9..6942daece2 100644
--- a/tests/control_plane/test_cli_output_differential.py
+++ b/tests/control_plane/test_cli_output_differential.py
@@ -2,6 +2,7 @@
import copy
import json
+import shutil
import sys
import pytest
@@ -24,6 +25,74 @@
)
+def test_pinned_reader_follows_a_powershell_command_with_literal_paths(tmp_path, monkeypatch):
+ import hashlib
+ import os
+ from pathlib import Path
+
+ from loopx import install_contract
+ from tests.control_plane.cli_output_fixture import read_pinned_cli_body
+
+ executable = Path(sys.executable)
+ if os.name != "nt":
+ # A path requiring PowerShell's call operator reproduces the native
+ # command shape even when the reader itself runs on POSIX.
+ launcher = tmp_path / "test Python"
+ launcher.symlink_to(executable)
+ executable = launcher
+ repository = Path(__file__).resolve().parents[2]
+ monkeypatch.setenv("PYTHONPATH", str(repository))
+ monkeypatch.setattr(install_contract, "_is_windows_host", lambda: True)
+ source = tmp_path / "owner's ‘saved capture’.json"
+ payload = {"requirements": "保留原要求和停止条件。🙂" * 40, "turn": "original"}
+ raw = json.dumps(payload, ensure_ascii=False).encode()
+ source.write_bytes(raw)
+ command = install_contract.render_host_cli_command([
+ str(executable), "-m", "loopx.entrypoint", "--format", "json",
+ "quota", "context-read", "--capture-file", str(source),
+ "--capture-sha256", hashlib.sha256(raw).hexdigest(),
+ "--page-bytes", "256", "--max-page-output-bytes", "4000",
+ ])
+ assert command.startswith("& ")
+
+ body, calls, reader_bytes = read_pinned_cli_body(command)
+ assert body.encode() == source.read_bytes() == raw
+ assert calls > 1 and reader_bytes > len(raw)
+
+
+def test_pinned_reader_follows_a_relative_powershell_launcher(tmp_path, monkeypatch):
+ import hashlib
+ import os
+ from pathlib import Path
+
+ from loopx.command_invocation import loopx_cli_argv
+ from loopx.control_plane.turn_driver.decision_capture import context_capture_read_command
+ from tests.control_plane.cli_output_fixture import read_pinned_cli_body
+
+ if os.name != "nt" and shutil.which("pwsh") is None:
+ pytest.skip("Requires PowerShell 7")
+ repository = Path(__file__).resolve().parents[2]
+ monkeypatch.chdir(tmp_path)
+ launcher_arg = "./bin/loopx.ps1"
+ launcher = tmp_path / launcher_arg
+ launcher.parent.mkdir()
+ shutil.copyfile(repository / "scripts" / "loopx.ps1", launcher)
+ pointer = tmp_path / "release.json"
+ pointer.write_text(json.dumps({"release_root": str(repository), "python": sys.executable}), encoding="utf-8")
+ monkeypatch.setenv("LOOPX_CURRENT_RELEASE_FILE", str(pointer))
+ source = tmp_path / "owner's ‘saved capture’.json"
+ raw = json.dumps({"requirements": "完整的验收条件🙂" * 30}, ensure_ascii=False).encode("utf-8")
+ source.write_bytes(raw)
+ digest = hashlib.sha256(raw).hexdigest()
+ route = loopx_cli_argv(launcher_arg)
+ command = context_capture_read_command(source, digest, reader_argv=route,
+ page_bytes=256, max_page_output_bytes=4000)
+ assert command.startswith(launcher_arg + " '")
+
+ body, calls, reader_bytes = read_pinned_cli_body(command)
+ assert body.encode("utf-8") == raw and calls > 1 and reader_bytes > len(raw)
+
+
def test_probe_process_failure_preserves_utf8_diagnostics(tmp_path):
import runpy
from pathlib import Path
diff --git a/tests/control_plane/test_creation_allowance_hold.py b/tests/control_plane/test_creation_allowance_hold.py
new file mode 100644
index 0000000000..6793d22b5d
--- /dev/null
+++ b/tests/control_plane/test_creation_allowance_hold.py
@@ -0,0 +1,340 @@
+"""Saved creation allowance uses real HTTP, quota and canonical File/SQLite.
+
+The native transport is scripted; the existing six-check oracle is unchanged.
+"""
+from copy import deepcopy
+from contextlib import contextmanager
+import hashlib
+import json
+import threading
+
+import pytest
+
+from test_creation_task_queue import (
+ create_requested_task, restarted_host, task_row, wait_for_creation_turn,
+)
+from test_original_task_entry import cart_host as cart_host
+from test_new_goal_storage_defaults import environment as environment
+from loopx.capabilities.native_chat.steward_managed_mode import ManagedSessionTurn
+from loopx.configure_goal import configure_goal
+
+
+@pytest.fixture
+def consumption(monkeypatch):
+ condition, calls = threading.Condition(), []
+ consume = ManagedSessionTurn.consume_creation_request
+
+ def observe(self, creation):
+ try:
+ return consume(self, creation)
+ finally:
+ with condition:
+ calls.append(deepcopy(creation))
+ condition.notify_all()
+
+ monkeypatch.setattr(ManagedSessionTurn, "consume_creation_request", observe)
+
+ def after(previous):
+ with condition:
+ assert condition.wait_for(lambda: len(calls) > previous, timeout=15)
+
+ return calls, after
+
+
+def quota(controller, value):
+ result = configure_goal(registry_path=controller.registry_path,
+ goal_id="cart-repair", quota_compute=value, execute=True)
+ assert result["written"] is True, result
+
+
+def paused_request(host, consumption):
+ _, request, controller, capture, project, _ = host
+ creation, ids, check = create_requested_task(host)
+ saved = creation["receipt"]["original_task_request"]
+ quota(controller, 0)
+ (project / "execute-native").write_text("valid")
+ before = capture.read_bytes()
+ count = len(consumption[0])
+ status, result = request(f"/api/actions/{check['proposal_id']}/apply", {})
+ assert status == 200 and result["turn"] is None, result
+ consumption[1](count)
+ original = controller.store.turn_for_client(ids["session_id"], "managed." + saved["operation_id"])
+ if original is not None:
+ original = controller.wait_for_turn(session_id=ids["session_id"], turn_id=original["turn_id"], timeout_sec=20)
+ assert original is None, original
+ assert capture.read_bytes() == before
+ return creation, ids, saved, before
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("resume", ["signal", "restart"])
+def test_saved_creation_waits_for_actual_quota_then_runs_same_operation_once(
+ environment, cart_host, consumption, provider, resume
+):
+ environment[0](provider)
+ actions, request, controller, capture, project, runtime = cart_host
+ creation, ids, saved, before = paused_request(cart_host, consumption)
+ sid, todo = ids["session_id"], saved["todo_id"]
+ original_session = controller.store.load_session(sid)
+ checker_hash = hashlib.sha256((project / "checks.py").read_bytes()).hexdigest()
+ assert task_row(runtime, todo)["status"] == "open"
+ for value in (0, 1):
+ if value:
+ quota(controller, value)
+ status, view = request(f"/api/chat/sessions/{sid}/native-turn?original_task=true&agent_id=codex")
+ assert status == 200 and view["task"]["creation_request"] == saved, view
+ assert view["task"]["creation_turn"] is None
+ assert controller.store.turn_for_client(sid, "managed." + saved["operation_id"]) is None
+ assert capture.read_bytes() == before
+ if resume == "signal":
+ controller.turn_release_wakeup.notify()
+ turn = wait_for_creation_turn(controller, sid, saved["operation_id"])
+ finished = controller.wait_for_turn(session_id=sid, turn_id=turn["turn_id"], timeout_sec=20)
+ else:
+ with restarted_host(cart_host) as (restored, _):
+ turn = wait_for_creation_turn(restored, sid, saved["operation_id"])
+ finished = restored.wait_for_turn(session_id=sid, turn_id=turn["turn_id"], timeout_sec=30)
+ assert restored.store.load_session(sid)["upstream_thread_id"] == original_session["upstream_thread_id"]
+ restored.turn_release_wakeup.notify()
+ restored.resume_creation_requests()
+ assert restored.store.turn_for_client(sid, "managed." + saved["operation_id"])["turn_id"] == turn["turn_id"]
+ assert finished["response"]["native_loopx"]["status"] == "committed", finished
+ assert finished["loopx_request"]["creation_request"] == saved
+ assert finished["response"]["native_loopx"]["effects"]["quota_spent"] is True
+ assert task_row(runtime, todo)["status"] == "done"
+ assert hashlib.sha256((project / "checks.py").read_bytes()).hexdigest() == checker_hash
+ calls = [json.loads(line) for line in capture.read_bytes()[len(before):].splitlines()]
+ assert sum(row.get("method") == "turn/start" for row in calls) == 1
+ assert not any(row.get("method") == "thread/start" for row in calls)
+ assert actions.load(creation["proposal_id"])["receipt"]["original_task_request"] == saved
+
+
+def assert_pending(host, saved, before):
+ _, _, controller, capture, _, runtime = host
+ assert controller.store.turn_for_client(saved["session_id"], "managed." + saved["operation_id"]) is None
+ assert capture.read_bytes() == before
+ assert task_row(runtime, saved["todo_id"])["status"] == "open"
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("source", ["missing", "changed"])
+def test_actual_validation_source_refusal_retains_unstarted_creation(
+ environment, cart_host, consumption, provider, source
+):
+ environment[0](provider)
+ _, _, controller, _, project, _ = cart_host
+ _, _, saved, before = paused_request(cart_host, consumption)
+ checker = project / "checks.py"
+ if source == "missing":
+ checker.unlink()
+ else:
+ checker.write_text(checker.read_text() + "\n# A changed verifier requires new owner approval.\n")
+ quota(controller, 1)
+ count = len(consumption[0])
+ controller.turn_release_wakeup.notify()
+ consumption[1](count)
+ assert_pending(cart_host, saved, before)
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_goal_commit_guard_refusal_leaves_saved_creation_pending(
+ environment, cart_host, consumption, monkeypatch, provider
+):
+ from loopx import attached_session
+ environment[0](provider)
+ _, _, controller, _, _, _ = cart_host
+ _, _, saved, before = paused_request(cart_host, consumption)
+
+ @contextmanager
+ def refuse():
+ raise ValueError("the original Goal commit guard refused publication")
+ yield
+
+ monkeypatch.setattr(attached_session, "managed_session_goal_commit", lambda **_: refuse)
+ quota(controller, 1)
+ count = len(consumption[0])
+ controller.turn_release_wakeup.notify()
+ consumption[1](count)
+ assert_pending(cart_host, saved, before)
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_quota_drop_after_acceptance_keeps_same_turn_queued_until_fresh_admission(
+ environment, cart_host, consumption, monkeypatch, provider
+):
+ environment[0](provider)
+ _, _, controller, capture, _, runtime = cart_host
+ _, _, saved, before = paused_request(cart_host, consumption)
+ accept = controller.store.accept_managed_turn
+ dropped = False
+
+ def quota_drop(*args, **kwargs):
+ nonlocal dropped
+ accepted = accept(*args, **kwargs)
+ if not dropped and kwargs.get("client_turn_id") == "managed." + saved["operation_id"]:
+ dropped = True
+ quota(controller, 0)
+ return accepted
+
+ monkeypatch.setattr(controller.store, "accept_managed_turn", quota_drop)
+ quota(controller, 1)
+ count = len(consumption[0])
+ controller.turn_release_wakeup.notify()
+ consumption[1](count)
+ queued = controller.store.turn_for_client(saved["session_id"], "managed." + saved["operation_id"])
+ assert queued is not None and queued["status"] == "queued", queued
+ assert queued["started_at"] is None and queued["upstream_turn_id"] is None
+ assert queued["delta_count"] == 0 and "_acceptance" not in queued
+ assert capture.read_bytes() == before
+ assert task_row(runtime, saved["todo_id"])["status"] == "open"
+ count = len(consumption[0])
+ controller.turn_release_wakeup.notify()
+ consumption[1](count)
+ held = controller.store.turn_for_client(saved["session_id"], "managed." + saved["operation_id"])
+ assert held["turn_id"] == queued["turn_id"] and held["status"] == "queued", held
+ assert held["started_at"] is None and held["upstream_turn_id"] is None
+ assert capture.read_bytes() == before
+ quota(controller, 1)
+ controller.turn_release_wakeup.notify()
+ finished = controller.wait_for_turn(session_id=saved["session_id"], turn_id=queued["turn_id"], timeout_sec=20)
+ assert finished["response"]["native_loopx"]["status"] == "committed", finished
+ assert finished["loopx_request"]["creation_request"] == saved
+ assert controller.store.turn_for_client(saved["session_id"], "managed." + saved["operation_id"])["turn_id"] == queued["turn_id"]
+ calls = [json.loads(line) for line in capture.read_bytes()[len(before):].splitlines()]
+ assert sum(row.get("method") == "turn/start" for row in calls) == 1
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_goal_wide_run_cannot_admit_another_todo_for_saved_creation(
+ environment, cart_host, consumption, monkeypatch, provider
+):
+ from loopx.control_plane.quota import live_decision
+ environment[0](provider)
+ _, _, controller, _, _, _ = cart_host
+ _, _, saved, before = paused_request(cart_host, consumption)
+ build = live_decision.build_live_quota_should_run_decision
+
+ def different_selected_todo(*args, **kwargs):
+ decision = build(*args, **kwargs)
+ assert decision["ok"] is True and decision["should_run"] is True
+ decision["selected_todo"]["todo_id"] = "todo_independent_work"
+ return decision
+
+ monkeypatch.setattr(live_decision, "build_live_quota_should_run_decision", different_selected_todo)
+ quota(controller, 1)
+ count = len(consumption[0])
+ controller.turn_release_wakeup.notify()
+ consumption[1](count)
+ assert_pending(cart_host, saved, before)
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_actual_dependency_wait_cannot_borrow_another_runnable_todo(
+ environment, cart_host, consumption, provider
+):
+ from loopx.capabilities.native_chat.steward_commands import native_command
+ environment[0](provider)
+ _, _, controller, capture, _, runtime = cart_host
+ _, _, saved, before = paused_request(cart_host, consumption)
+ known = {row["todo_id"] for row in native_command(controller,
+ ["todo", "list", "--goal-id", "cart-repair"])["todos"]}
+ native_command(controller, ["todo", "add", "--goal-id", "cart-repair", "--role", "agent",
+ "--text", "Complete the independent prerequisite", "--claimed-by", "codex"])
+ current = native_command(controller, ["todo", "list", "--goal-id", "cart-repair"])["todos"]
+ prerequisite, = {row["todo_id"] for row in current} - known
+ native_command(controller, ["todo", "update", "--goal-id", "cart-repair", "--todo-id", saved["todo_id"],
+ "--agent-id", "codex", "--status", "deferred", "--reason", "Wait for the actual prerequisite",
+ "--resume-when", "todo_done:" + prerequisite])
+ quota(controller, 1)
+ owner = ManagedSessionTurn(controller)
+ request = owner._request(saved["session_id"], saved["agent_id"], saved["operation_id"], saved["todo_id"])
+ assert owner._creation_allowance(request) is False
+ count = len(consumption[0])
+ controller.turn_release_wakeup.notify()
+ consumption[1](count)
+ assert controller.store.turn_for_client(saved["session_id"], "managed." + saved["operation_id"]) is None
+ assert capture.read_bytes() == before
+ assert task_row(runtime, saved["todo_id"])["status"] == "deferred"
+ assert task_row(runtime, prerequisite)["status"] == "open"
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_source_exception_after_acceptance_preserves_original_unstarted_turn(
+ environment, cart_host, consumption, monkeypatch, provider
+):
+ environment[0](provider)
+ _, _, controller, capture, project, _ = cart_host
+ _, _, saved, before = paused_request(cart_host, consumption)
+ checker = project / "checks.py"
+ checker_bytes = checker.read_bytes()
+ accept = controller.store.accept_managed_turn
+
+ def missing_after_accept(*args, **kwargs):
+ accepted = accept(*args, **kwargs)
+ if kwargs.get("client_turn_id") == "managed." + saved["operation_id"]:
+ checker.unlink()
+ return accepted
+
+ monkeypatch.setattr(controller.store, "accept_managed_turn", missing_after_accept)
+ quota(controller, 1)
+ count = len(consumption[0])
+ controller.turn_release_wakeup.notify()
+ consumption[1](count)
+ queued = controller.store.turn_for_client(saved["session_id"], "managed." + saved["operation_id"])
+ assert queued is not None and queued["status"] == "queued", queued
+ assert queued["started_at"] is None and queued["upstream_turn_id"] is None
+ assert queued["delta_count"] == 0 and "_acceptance" not in queued
+ assert capture.read_bytes() == before
+ count = len(consumption[0])
+ controller.turn_release_wakeup.notify()
+ consumption[1](count)
+ held = controller.store.turn_for_client(saved["session_id"], "managed." + saved["operation_id"])
+ assert held["turn_id"] == queued["turn_id"] and held["status"] == "queued", held
+ assert held["started_at"] is None and held["upstream_turn_id"] is None
+ assert capture.read_bytes() == before
+ checker.write_bytes(checker_bytes)
+ controller.turn_release_wakeup.notify()
+ finished = controller.wait_for_turn(session_id=saved["session_id"], turn_id=queued["turn_id"], timeout_sec=20)
+ assert finished["response"]["native_loopx"]["status"] == "committed", finished
+ assert finished["loopx_request"]["creation_request"] == saved
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_final_native_quota_refusal_cannot_be_replaced_by_preparation_read(
+ environment, cart_host, consumption, monkeypatch, provider
+):
+ environment[0](provider)
+ _, _, controller, capture, _, runtime = cart_host
+ _, _, saved, before = paused_request(cart_host, consumption)
+ native = controller.adapters[saved["session_id"]].session
+ write = native._write
+ guard_boundary_reached = False
+
+ def pause_before_native_guard(payload, **kwargs):
+ nonlocal guard_boundary_reached
+ if payload.get("method") == "turn/start":
+ guard_boundary_reached = True
+ quota(controller, 0)
+ return write(payload, **kwargs)
+
+ monkeypatch.setattr(native, "_write", pause_before_native_guard)
+ quota(controller, 1)
+ controller.turn_release_wakeup.notify()
+ turn = wait_for_creation_turn(controller, saved["session_id"], saved["operation_id"])
+ finished = controller.wait_for_turn(session_id=saved["session_id"], turn_id=turn["turn_id"], timeout_sec=20)
+ result = finished["response"]["native_loopx"]
+ assert guard_boundary_reached is True
+ assert result["ok"] is False and result["error_code"] == "stale_goal_authority", result
+ assert all(value is None for value in result["effects"].values()), result
+ recovery = result["journal_observation"]["recovery_decision"]
+ assert recovery["action"] == "blocked" and recovery["can_continue"] is False
+ assert recovery["reinvoke_host"] is False and recovery["reason"] == "host_outcome_unknown"
+ assert task_row(runtime, saved["todo_id"])["status"] == "open"
+ calls = [json.loads(line) for line in capture.read_bytes()[len(before):].splitlines()]
+ assert not any(row.get("method") == "turn/start" for row in calls)
+ after = capture.read_bytes()
+ quota(controller, 1)
+ controller.turn_release_wakeup.notify()
+ controller.resume_creation_requests()
+ assert controller.store.turn_for_client(saved["session_id"], "managed." + saved["operation_id"])["turn_id"] == turn["turn_id"]
+ assert capture.read_bytes() == after
diff --git a/tests/control_plane/test_creation_initial_admission_hold.py b/tests/control_plane/test_creation_initial_admission_hold.py
new file mode 100644
index 0000000000..2e5f1ac06b
--- /dev/null
+++ b/tests/control_plane/test_creation_initial_admission_hold.py
@@ -0,0 +1,82 @@
+"""A confirmed start stays recoverable when its real first admission is held."""
+import json
+
+import pytest
+
+from test_creation_task_queue import cart_host as cart_host
+from test_creation_task_queue import environment as environment
+from test_creation_task_queue import preview_requested_task
+from loopx.agent_registry import load_goal_from_registry
+from loopx.configure_goal import configure_goal
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("requested", [True, False])
+def test_confirmed_creation_retries_original_work_after_initial_quota_hold(environment, cart_host, monkeypatch, provider, requested):
+ import loopx.chat_actions as creation_owner
+ environment[0](provider)
+ actions, request, controller, capture, _, _ = cart_host
+ configure = creation_owner.configure_goal
+ def pause_after_real_agent_binding(**kwargs):
+ result = configure(**kwargs)
+ if kwargs.get("goal_id") == "cart-repair" and kwargs.get("registered_agents") == ["codex"]:
+ configure_goal(registry_path=controller.registry_path, goal_id="cart-repair",
+ quota_compute=0, execute=True)
+ return result
+ proposal = preview_requested_task(cart_host, requested=requested)
+ pid = proposal["proposal_id"]
+ path = f"/api/actions/{pid}/apply"
+ with monkeypatch.context() as held:
+ held.setattr(creation_owner, "configure_goal", pause_after_real_agent_binding)
+ status, refused = request(path, {})
+ if not requested:
+ assert status == 200 and refused["proposal"]["status"] == "applied", refused
+ assert "original_task_request" not in refused["proposal"]["receipt"]
+ assert "session_id" not in refused["proposal"]["receipt"]["resource_ids"]
+ assert refused["proposal"]["receipt"]["child_gate"]["kind"] == "goal_quota_gate"
+ assert controller.store.list_sessions(goal_id="cart-repair") == []
+ controller.resume_creation_requests()
+ assert not controller.session_queue_workers
+ return
+ assert status == 400, refused
+ retained = actions.load(pid)
+ assert retained["status"] == "failed" and retained["receipt"] is None
+ assert retained["normalized_parameters"]["start_execution"] is True
+ steps = retained["checkpoint"]["steps"]
+ assert steps["first_turn_gated"]["gate"]["kind"] == "goal_quota_gate"
+ assert "first_session_opened" not in steps and "original_task_requested" not in steps
+ assert controller.store.list_sessions(goal_id="cart-repair") == []
+ before = capture.read_bytes() if capture.exists() else b""
+ assert not any(json.loads(row).get("method") == "turn/start" for row in before.splitlines())
+ goal = load_goal_from_registry(controller.registry_path, "cart-repair")
+ original_grants = goal["coordination"]["checkpointed_boundary_authority"]
+ assert len(original_grants) == 1 and original_grants[0]["decision_id"] == pid
+ original_todos = steps["todos_created"]["todo_ids"]
+ controller.resume_creation_requests()
+ assert not controller.session_queue_workers
+ assert (capture.read_bytes() if capture.exists() else b"") == before
+
+ # Restore through the real quota owner; retry the original confirmation.
+ # The test never manufactures quota permission or another creation intent.
+ configure_goal(registry_path=controller.registry_path, goal_id="cart-repair",
+ quota_compute=1, execute=True)
+ status, resumed = request(path, {})
+ assert status == 202, resumed
+ committed = resumed["proposal"]
+ ids = committed["receipt"]["resource_ids"]
+ assert committed["proposal_id"] == pid and ids["todo_ids"] == original_todos
+ assert ids["creation_operation_id"] == pid
+ assert committed["request_digest"] == retained["request_digest"]
+ assert committed["receipt"]["original_task_request"]["session_id"] == ids["session_id"]
+ assert committed["receipt"]["original_task_request"]["todo_id"] == original_todos[0]
+ first = controller.wait_for_turn(session_id=ids["session_id"], turn_id=ids["turn_id"], timeout_sec=20)
+ assert first["status"] == "completed"
+ assert len(controller.store.list_sessions(goal_id="cart-repair")) == 1
+ assert load_goal_from_registry(controller.registry_path, "cart-repair")["coordination"][
+ "checkpointed_boundary_authority"] == original_grants
+ after = capture.read_bytes()
+ calls = [json.loads(row) for row in after.splitlines()]
+ assert sum(row.get("method") == "thread/start" for row in calls) == 1
+ assert sum(row.get("method") == "turn/start" for row in calls) == 1
+ assert request(path, {})[1]["proposal"]["receipt"] == committed["receipt"]
+ assert capture.read_bytes() == after
diff --git a/tests/control_plane/test_creation_task_queue.py b/tests/control_plane/test_creation_task_queue.py
new file mode 100644
index 0000000000..050a2a643e
--- /dev/null
+++ b/tests/control_plane/test_creation_task_queue.py
@@ -0,0 +1,453 @@
+"""An explicit creation request reaches the original Task through its Session queue.
+
+HTTP/Chat/Core and File/SQLite are real. The existing six-check oracle remains
+independent; the native protocol and artifact editing are scripted transport.
+"""
+from contextlib import contextmanager
+import hashlib
+import http.client
+import json
+import subprocess
+import sys
+import threading
+import time
+
+import pytest
+
+from test_original_task_entry import cart_host as cart_host
+from test_new_goal_storage_defaults import environment as environment
+from loopx.chat_actions import ChatActionService
+from loopx.chat_runtime import ChatRuntimeController
+from loopx.chat_server import ChatHTTPServer, ChatRequestHandler
+from loopx.control_plane.chat_turn_acceptance import CHAT_TURN_ACCEPTANCE_CAPSULE_SCHEMA
+from loopx.control_plane.coordination.local_authority import read_canonical_todos_if_promoted
+from loopx.capabilities.native_chat.project_context import ChatProjectContexts
+
+
+def preview_requested_task(host, *, requested=True):
+ request = host[1]
+ parameters = {"goal_id": "cart-repair", "title": "Cart repair",
+ "objective": "Repair cart.py according to README.md and unchanged checks.py.",
+ "completion_criteria": "The six existing checks pass without changing checks.py.",
+ "agent_id": "codex", "workspace_ref": "current", "heartbeat": {"enabled": False},
+ "initial_todos": ["Repair cart.py to pass the six existing behavioral checks."]}
+ if requested:
+ parameters.update(start_execution=True, permission="workspace_write_on_confirmation")
+ status, preview = request("/api/actions/preview", {"action_kind": "goal.create",
+ "summary": "Repair the cart using its existing checks", "idempotency_key": "cart-queue-request",
+ "context": {"kind": "goal_channel", "goal_id": "workspace"}, "normalized_parameters": parameters})
+ assert status == 201, preview
+ return preview["proposal"]
+
+
+def create_requested_task(host, *, requested=True):
+ actions, request, controller, _, _, _ = host
+ proposal = preview_requested_task(host, requested=requested)
+ status, applied = request(f"/api/actions/{proposal['proposal_id']}/apply", {})
+ assert status == 202, applied
+ ids = applied["proposal"]["receipt"]["resource_ids"]
+ first = controller.wait_for_turn(session_id=ids["session_id"], turn_id=ids["turn_id"], timeout_sec=20)
+ assert first["status"] == "completed", first
+ proposals = actions.list(goal_id="cart-repair", context_kind="goal")
+ checks = [row for row in proposals if row["action_kind"] == "goal.acceptance.check"]
+ assert len(checks) == 1, first
+ return actions.load(proposal["proposal_id"]), ids, checks[0]
+
+
+def wait_for_creation_turn(controller, session_id, operation_id, *, timeout=15):
+ deadline = time.monotonic() + timeout
+ while time.monotonic() < deadline:
+ turn = controller.store.turn_for_client(session_id, "managed." + operation_id)
+ if turn is not None:
+ return turn
+ time.sleep(0.02)
+ raise AssertionError("the original Session queue did not consume the saved creation request")
+
+
+def task_row(runtime, todo_id):
+ authority = read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="cart-repair", include_leases=True)
+ return next(row for row in authority["todos"] if row["todo_id"] == todo_id)
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("mode", ["valid", "failed"])
+def test_creation_queue_consumes_only_the_saved_request_and_returns_original_result(environment, cart_host, monkeypatch, provider, mode):
+ environment[0](provider)
+ actions, request, controller, capture, project, runtime = cart_host
+ creation, ids, check = create_requested_task(cart_host)
+ saved = creation["receipt"]["original_task_request"]
+ sid, todo = ids["session_id"], ids["todo_ids"][0]
+ assert saved["session_id"] == sid and saved["todo_id"] == todo
+ assert saved["operation_id"] == "goal-create-" + creation["proposal_id"]
+ original_session = controller.store.load_session(sid)
+ history = controller.store.messages(sid)
+ checker_hash = hashlib.sha256((project / "checks.py").read_bytes()).hexdigest()
+ before = capture.read_bytes()
+ for _ in range(2):
+ status, observed = request(f"/api/chat/sessions/{sid}/native-turn?original_task=true&agent_id=codex")
+ assert status == 200, observed
+ assert observed["task"]["creation_request"] == saved
+ assert observed["task"]["creation_turn"] is None
+ assert observed["task"]["state"] == "unbound"
+ assert capture.read_bytes() == before and task_row(runtime, todo)["status"] == "open"
+ (project / "execute-native").write_text(mode)
+ status, adopted = request(f"/api/actions/{check['proposal_id']}/apply", {})
+ assert status == 200 and adopted["turn"] is None, adopted
+ assert adopted["proposal"]["receipt"]["checks_passed"] is False
+ turn = wait_for_creation_turn(controller, sid, saved["operation_id"])
+ finished = controller.wait_for_turn(session_id=sid, turn_id=turn["turn_id"], timeout_sec=20)
+ result = finished["response"]["native_loopx"]
+ assert result["status"] == ("committed" if mode == "valid" else "failed"), result
+ assert result["effects"]["quota_spent"] is (mode == "valid")
+ assert task_row(runtime, todo)["status"] == ("done" if mode == "valid" else "open")
+ assert finished["loopx_request"]["creation_request"] == saved
+ calls = [json.loads(line) for line in capture.read_bytes()[len(before):].splitlines()]
+ assert sum(row.get("method") == "turn/start" for row in calls) == 1
+ assert not any(row.get("method") == "thread/start" for row in calls)
+ assert controller.store.load_session(sid)["upstream_thread_id"] == original_session["upstream_thread_id"]
+ assert controller.store.messages(sid)[:len(history)] == history
+ assert hashlib.sha256((project / "checks.py").read_bytes()).hexdigest() == checker_hash
+ after = capture.read_bytes()
+ for thread in list(controller.session_queue_threads.values()):
+ thread.join(timeout=2)
+ with monkeypatch.context() as stopped:
+ def unexpected_queue(**_kwargs):
+ raise AssertionError("a terminal original operation must not wake another queue worker")
+ stopped.setattr(controller, "resume_session_queue", unexpected_queue)
+ controller.resume_creation_requests()
+ assert capture.read_bytes() == after
+ assert controller.store.turn_for_client(sid, "managed." + saved["operation_id"])["turn_id"] == turn["turn_id"]
+ status, replay = request(f"/api/actions/{creation['proposal_id']}/apply", {})
+ assert status in (200, 202) and replay["proposal"]["receipt"]["original_task_request"] == saved
+ assert capture.read_bytes() == after
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_historical_creation_and_check_adoption_do_not_authorize_a_native_task(environment, cart_host, monkeypatch, provider):
+ import builtins
+ environment[0](provider)
+ _, request, controller, capture, project, runtime = cart_host
+ original_import = builtins.__import__
+ def legacy_creation_import(name, *args, **kwargs):
+ if name.endswith("chat.goal_creation_execution"):
+ raise AssertionError("historical creation loaded the optional original-Task execution adapter")
+ return original_import(name, *args, **kwargs)
+ with monkeypatch.context() as legacy:
+ legacy.setattr(builtins, "__import__", legacy_creation_import)
+ creation, ids, check = create_requested_task(cart_host, requested=False)
+ assert "original_task_request" not in creation["receipt"]
+ sid = ids["session_id"]
+ before = capture.read_bytes()
+ (project / "execute-native").write_text("valid")
+ status, adopted = request(f"/api/actions/{check['proposal_id']}/apply", {})
+ assert status == 200 and adopted["turn"] is None, adopted
+ controller.resume_creation_requests()
+ status, observed = request(f"/api/chat/sessions/{sid}/native-turn?original_task=true&agent_id=codex")
+ assert status == 200 and "creation_request" not in observed["task"], observed
+ assert capture.read_bytes() == before
+ assert task_row(runtime, ids["todo_ids"][0])["status"] == "open"
+
+
+@contextmanager
+def restarted_host(host):
+ actions, _, prior, _, project, runtime = host
+ prior.close()
+ controller = ChatRuntimeController(store=prior.store, codex_bin=prior.codex_bin,
+ registry_path=prior.registry_path, project_contexts=ChatProjectContexts([project]))
+ service = ChatActionService(store=actions, registry_path=prior.registry_path,
+ runtime_controller=controller, chat_store=prior.store)
+ server = ChatHTTPServer(("127.0.0.1", 0), ChatRequestHandler)
+ server.verbose = False
+ server.registry_path, server.runtime_root = prior.registry_path, runtime
+ server.runtime_controller, server.action_store, server.action_service = controller, actions, service
+ thread = threading.Thread(target=server.serve_forever, daemon=True)
+ thread.start()
+ controller.resume_creation_requests()
+ def request(path):
+ connection = http.client.HTTPConnection(*server.server_address, timeout=30)
+ try:
+ connection.request("GET", path)
+ response = connection.getresponse()
+ return response.status, json.loads(response.read())
+ finally:
+ connection.close()
+ try:
+ yield controller, request
+ finally:
+ server.shutdown()
+ thread.join(timeout=5)
+ server.server_close()
+ controller.close()
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_restart_resumes_the_same_accepted_unstarted_creation_turn(environment, cart_host, monkeypatch, provider):
+ environment[0](provider)
+ _, request, controller, capture, project, _ = cart_host
+ creation, ids, check = create_requested_task(cart_host)
+ saved = creation["receipt"]["original_task_request"]
+ original_start = controller._start_accepted_turn_worker
+ def lost_dispatch(**kwargs):
+ if kwargs["loopx_execution"]:
+ return False
+ return original_start(**kwargs)
+ monkeypatch.setattr(controller, "_start_accepted_turn_worker", lost_dispatch)
+ (project / "execute-native").write_text("valid")
+ before = capture.read_bytes()
+ assert request(f"/api/actions/{check['proposal_id']}/apply", {})[0] == 200
+ queued = wait_for_creation_turn(controller, ids["session_id"], saved["operation_id"])
+ assert queued["status"] == "queued" and queued.get("started_at") is None
+ assert capture.read_bytes() == before
+ thread_id = controller.store.load_session(ids["session_id"])["upstream_thread_id"]
+ with restarted_host(cart_host) as (restored, read):
+ finished = restored.wait_for_turn(session_id=ids["session_id"], turn_id=queued["turn_id"], timeout_sec=30)
+ assert finished["response"]["native_loopx"]["status"] == "committed", finished
+ assert restored.store.load_session(ids["session_id"])["upstream_thread_id"] == thread_id
+ status, observed = read(f"/api/chat/sessions/{ids['session_id']}/native-turn?original_task=true&agent_id=codex")
+ assert status == 200 and observed["task"]["creation_turn"]["turn_id"] == queued["turn_id"], observed
+ calls = [json.loads(line) for line in capture.read_bytes()[len(before):].splitlines()]
+ assert sum(row.get("method") == "turn/start" for row in calls) == 1
+ assert not any(row.get("method") == "thread/start" for row in calls)
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_failed_creation_dispatch_is_retained_without_another_model(environment, cart_host, monkeypatch, provider):
+ environment[0](provider)
+ _, request, controller, capture, project, runtime = cart_host
+ creation, ids, check = create_requested_task(cart_host)
+ saved = creation["receipt"]["original_task_request"]
+ def fail_dispatch(**_kwargs):
+ raise OSError("synthetic worker start failure")
+ monkeypatch.setattr(controller, "_start_accepted_turn_worker", fail_dispatch)
+ (project / "execute-native").write_text("valid")
+ before = capture.read_bytes()
+ assert request(f"/api/actions/{check['proposal_id']}/apply", {})[0] == 200
+ turn = wait_for_creation_turn(controller, ids["session_id"], saved["operation_id"])
+ deadline = time.monotonic() + 5
+ while time.monotonic() < deadline and controller.store.load_turn(ids["session_id"], turn["turn_id"])["status"] != "failed":
+ time.sleep(0.02)
+ failed = controller.store.load_turn(ids["session_id"], turn["turn_id"])
+ assert failed["status"] == "failed", failed
+ controller.resume_creation_requests()
+ for thread in list(controller.session_queue_threads.values()):
+ thread.join(timeout=2)
+ assert capture.read_bytes() == before
+ assert controller.store.turn_for_client(ids["session_id"], "managed." + saved["operation_id"])["turn_id"] == turn["turn_id"]
+ assert task_row(runtime, saved["todo_id"])["status"] == "open"
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("change", ["foreign-session", "fingerprint", "upstream-thread", "missing-thread-pin"])
+def test_startup_rejects_a_changed_source_before_waking_any_session_queue(environment, cart_host, monkeypatch, provider, change):
+ environment[0](provider)
+ actions, request, controller, capture, project, _ = cart_host
+ creation, ids, _ = create_requested_task(cart_host)
+ before = capture.read_bytes()
+ payload = actions._read()
+ source = payload["proposals"][creation["proposal_id"]]
+ if change == "foreign-session":
+ foreign = controller.store.create_session(goal_id="cart-repair", managed_agent_id="codex",
+ managed_workspace_path=str(project.resolve()), goal_instance_id=ids["goal_instance_id"],
+ channel_id="agent.codex", agent_id="codex", executor_endpoint_id="codex",
+ adapter_kind="codex_app_server", upstream_thread_id="foreign-thread", upstream_mode="chat")
+ source["receipt"]["original_task_request"]["session_id"] = foreign["session_id"]
+ elif change == "fingerprint":
+ source["expected_state_fingerprint"] = "changed-confirmed-source"
+ elif change == "upstream-thread":
+ # Keep every public creation descriptor unchanged while replacing the
+ # actual native history owner. A Session id alone cannot certify the
+ # original conversation, even when Goal/Task/lifetime still match.
+ path = controller.store._session_path(ids["session_id"])
+ session = json.loads(path.read_text())
+ session["upstream_thread_id"] = "replacement-native-thread"
+ path.write_text(json.dumps(session))
+ assert source["receipt"]["original_task_request"] == creation["receipt"]["original_task_request"]
+ else:
+ # Missing WIP provenance is unknown; restart cannot upgrade it from
+ # whichever native binding currently occupies the original Session.
+ source["checkpoint"]["steps"]["first_session_opened"].pop("upstream_thread_id")
+ actions._write(payload) # Only corrupt this disposable attack fixture.
+ wakes = []
+ monkeypatch.setattr(controller, "resume_session_queue", lambda **kwargs: wakes.append(kwargs))
+ controller.resume_creation_requests()
+ assert wakes == []
+ status, observed = request(f"/api/chat/sessions/{ids['session_id']}/native-turn?original_task=true&agent_id=codex")
+ assert status == 200 and observed["task"]["state"] == "unbound", observed
+ assert observed["task"]["reason"] and "creation_request" not in observed["task"]
+ assert capture.read_bytes() == before
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_no_intent_empty_queue_does_not_read_canonical_creation_acceptance(environment, cart_host, monkeypatch, provider):
+ environment[0](provider)
+ _, _, controller, capture, project, _ = cart_host
+ _, ids, _ = create_requested_task(cart_host, requested=False)
+ before = capture.read_bytes()
+ def unexpected_acceptance(**_kwargs):
+ raise AssertionError("an empty ordinary queue must not discover canonical creation acceptance")
+ monkeypatch.setattr("loopx.control_plane.goals.acceptance.inspect_goal_acceptance", unexpected_acceptance)
+ controller.resume_session_queue(session_id=ids["session_id"], work_dir=project,
+ objective="Continue the existing conversation.")
+ for thread in list(controller.session_queue_threads.values()):
+ thread.join(timeout=2)
+ assert ids["session_id"] not in controller.session_queue_workers
+ assert capture.read_bytes() == before
+ cold_read = subprocess.run([sys.executable, "-c", '''
+import builtins
+import sys
+import threading
+from pathlib import Path
+from types import SimpleNamespace
+from loopx.chat_action_store import ChatActionStore
+from loopx.chat_store import ChatSessionStore
+from loopx.capabilities.native_chat.creation_queue import consume_creation_request, resume_creation_requests
+store = ChatSessionStore(Path(sys.argv[1]), startup_maintenance=False)
+actions = ChatActionStore(Path(sys.argv[2]))
+original_import = builtins.__import__
+def feature_off_import(name, *args, **kwargs):
+ if name.endswith(("steward_managed_mode", "control_plane.effect_runtime", "control_plane.goals.acceptance")):
+ raise AssertionError("feature-off queue imported a new creation admission owner: " + name)
+ return original_import(name, *args, **kwargs)
+def unexpected_wake(**kwargs):
+ raise AssertionError("a historical no-intent creation woke a native queue")
+builtins.__import__ = feature_off_import
+controller = SimpleNamespace(store=store, original_task_action_store=actions,
+ closed=threading.Event(), resume_session_queue=unexpected_wake)
+assert consume_creation_request(controller, sys.argv[3]) is False
+resume_creation_requests(controller)
+''', str(controller.store.root.parent), str(controller.original_task_action_store.root), ids["session_id"]],
+ capture_output=True, text=True, timeout=15)
+ assert cold_read.returncode == 0, cold_read.stdout + cold_read.stderr
+ assert capture.read_bytes() == before
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("change", ["missing-publication", "unfinished-acceptance", "already-started", "foreign-request"])
+def test_startup_cannot_redispatch_an_unknown_or_foreign_queued_operation(environment, cart_host, monkeypatch, provider, change):
+ environment[0](provider)
+ _, request, controller, capture, project, _ = cart_host
+ creation, ids, check = create_requested_task(cart_host)
+ saved = creation["receipt"]["original_task_request"]
+ original_start = controller._start_accepted_turn_worker
+ dispatch_prepared = threading.Event()
+ def held_dispatch(**kwargs):
+ if kwargs["loopx_execution"]:
+ dispatch_prepared.set()
+ return False
+ return original_start(**kwargs)
+ monkeypatch.setattr(controller, "_start_accepted_turn_worker", held_dispatch)
+ (project / "execute-native").write_text("valid")
+ before = capture.read_bytes()
+ assert request(f"/api/actions/{check['proposal_id']}/apply", {})[0] == 200
+ queued = wait_for_creation_turn(controller, ids["session_id"], saved["operation_id"])
+ assert dispatch_prepared.wait(timeout=10)
+ # An accepted Turn can be discovered before acceptance settles. Corrupt
+ # only the fully prepared crash input under its existing Session fence,
+ # so a concurrent acceptance write cannot replace the attack fixture.
+ with controller._session_adapter_lock(ids["session_id"]):
+ if change == "already-started":
+ controller.store.update_turn(ids["session_id"], queued["turn_id"],
+ status="starting", started_at="2026-11-15T00:00:00Z")
+ else:
+ path = controller.store._turn_path(ids["session_id"], queued["turn_id"])
+ raw = json.loads(path.read_text())
+ assert "_acceptance" not in raw
+ if change == "missing-publication":
+ raw.pop("upstream_turn_id")
+ elif change == "unfinished-acceptance":
+ raw["_acceptance"] = {"schema_version": CHAT_TURN_ACCEPTANCE_CAPSULE_SCHEMA, "phase": "prepared"}
+ else:
+ raw["loopx_request"]["creation_request"]["session_id"] = "foreign-session"
+ path.write_text(json.dumps(raw)) # Only this disposable corruption input.
+ wakes = []
+ monkeypatch.setattr(controller, "resume_session_queue", lambda **kwargs: wakes.append(kwargs))
+ controller.resume_creation_requests()
+ assert wakes == []
+ assert capture.read_bytes() == before
+ assert controller.store.turn_for_client(ids["session_id"], "managed." + saved["operation_id"])["turn_id"] == queued["turn_id"]
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_pending_creation_rechecks_only_after_a_content_free_signal(environment, cart_host, monkeypatch, provider):
+ from loopx.capabilities.native_chat.steward_managed_mode import ManagedSessionTurn
+ environment[0](provider)
+ _, _, controller, _, _, _ = cart_host
+ original = ManagedSessionTurn._validation_source
+ attempted = threading.Event()
+ count = []
+ def read_current(self, *args, **kwargs):
+ try:
+ return original(self, *args, **kwargs)
+ finally:
+ if "_drain_session_queue" in threading.current_thread().name:
+ count.append(1)
+ attempted.set()
+ monkeypatch.setattr(ManagedSessionTurn, "_validation_source", read_current)
+ creation, ids, _ = create_requested_task(cart_host)
+ assert attempted.wait(timeout=10)
+ time.sleep(0.1) # Let the original Turn's terminal signal coalesce.
+ before = len(count)
+ time.sleep(2.2) # An old two-second polling implementation would reread.
+ assert len(count) == before
+ controller.turn_release_wakeup.notify()
+ deadline = time.monotonic() + 5
+ while time.monotonic() < deadline and len(count) == before:
+ time.sleep(0.02)
+ assert len(count) > before
+ assert controller.store.turn_for_client(ids["session_id"],
+ "managed." + creation["receipt"]["original_task_request"]["operation_id"]) is None
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_creation_terminal_during_queue_read_keeps_the_original_ordinary_followup(environment, cart_host, monkeypatch, provider):
+ from loopx.capabilities.native_chat.steward_managed_mode import ManagedSessionTurn
+ environment[0](provider)
+ _, request, controller, capture, project, runtime = cart_host
+ # Retain real acceptance and FIFO state while controlling only dispatch.
+ # The queue worker must observe the native completion at the second read,
+ # after it has already cached the original queued operation.
+ monkeypatch.setattr(controller, "resume_session_queue", lambda **_kwargs: None)
+ start = controller._start_accepted_turn_worker
+ monkeypatch.setattr(controller, "_start_accepted_turn_worker",
+ lambda **kwargs: False if kwargs["loopx_execution"] else start(**kwargs))
+ creation, ids, check = create_requested_task(cart_host)
+ saved = creation["receipt"]["original_task_request"]
+ sid = ids["session_id"]
+ assert request(f"/api/actions/{check['proposal_id']}/apply", {})[0] == 200
+ native, created = ManagedSessionTurn(controller).submit(sid,
+ agent_id=saved["agent_id"], operation_id=saved["operation_id"],
+ todo_id=saved["todo_id"], _creation_request=saved)
+ assert created and native["status"] == "queued"
+ followup, created = controller.enqueue_turn(session_id=sid,
+ client_turn_id="ordinary-after-original", message="Read the original results.",
+ work_dir=project, objective="Retain the original cart repair.", origin="web")
+ assert created and followup["status"] == "queued"
+ before = capture.read_bytes()
+ original_read = controller.store.turn_for_client
+ terminalized = []
+ def finish_native_before_consumer_read(session_id, client_id):
+ if not terminalized and session_id == sid and client_id == "managed." + saved["operation_id"]:
+ terminalized.append(native["turn_id"])
+ controller._fail_queue_preparation(sid, native["turn_id"],
+ OSError("synthetic native worker exit before consumer read"))
+ return original_read(session_id, client_id)
+ monkeypatch.setattr(controller.store, "turn_for_client", finish_native_before_consumer_read)
+ worker = threading.Thread(target=controller._drain_session_queue,
+ kwargs={"session_id": sid, "work_dir": project, "objective": "Retain the original cart repair."})
+ controller.session_queue_workers.add(sid)
+ controller.session_queue_threads[sid] = worker
+ worker.start()
+ worker.join(timeout=20)
+ assert not worker.is_alive(), "the original queue did not reconcile terminal work"
+ assert terminalized == [native["turn_id"]]
+ assert controller.store.load_turn(sid, native["turn_id"])["status"] == "failed"
+ assert controller.store.load_turn(sid, followup["turn_id"])["status"] == "completed"
+ assert controller.store.load_session(sid)["active_turn_id"] is None
+ assert sid not in controller.session_queue_workers
+ calls = [json.loads(line) for line in capture.read_bytes()[len(before):].splitlines()]
+ starts = [row for row in calls if row.get("method") == "turn/start"]
+ assert len(starts) == 1 # Only the already requested ordinary follow-up.
+ assert starts[0]["params"]["sandboxPolicy"] == {"type": "readOnly", "networkAccess": False}
+ assert "runtimeWorkspaceRoots" not in starts[0]["params"]
+ assert not any(row.get("method") == "thread/start" for row in calls)
+ assert task_row(runtime, saved["todo_id"])["status"] == "open"
diff --git a/tests/control_plane/test_dated_vision_wait_cli.py b/tests/control_plane/test_dated_vision_wait_cli.py
index 54645e7b36..74bd62229d 100644
--- a/tests/control_plane/test_dated_vision_wait_cli.py
+++ b/tests/control_plane/test_dated_vision_wait_cli.py
@@ -194,6 +194,78 @@ def test_multiple_future_waits_keep_the_original_unbound_turn_quiet(
assert not any(run.get("classification") in ("quota_monitor_poll", "quota_slot_spent") for run in runs)
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("scenario", ["before_due", "successor_due", "monitor_due", "unproven"])
+def test_watch_only_monitor_preserves_dated_successor_wait(
+ tmp_path, isolated_effect, monkeypatch, provider, scenario,
+):
+ past = "2026-01-01T00:00:00Z"
+ monitor = "todo_watch_only"
+ monitor_due = past if scenario == "monitor_due" else FUTURE.removeprefix("resume_at:")
+ resume = f"resume_at:{past}" if scenario == "successor_due" else FUTURE
+ if scenario == "unproven":
+ resume = "capacity_available:external_runner"
+ registry, runtime, state = fixture(tmp_path, resume)
+ review = "todo_independent_review"
+ with state.open("a", encoding="utf-8") as output:
+ output.write(
+ "\n- [ ] [P1] Keep the independent review at its own scheduled instant.\n"
+ f" \n"
+ "\n- [ ] [P1] Observe feedback at its own scheduled instant.\n"
+ f" \n"
+ )
+ vision = runtime / f"goals/{GOAL}/runs/vision.json"
+ run = json.loads(vision.read_text())
+ run["agent_vision"]["todo_delta"] = [f"retain:{TODO}", f"retain:{review}"]
+ vision.write_text(json.dumps(run), encoding="utf-8")
+ index = vision.parent / "index.jsonl"
+ index.write_text(json.dumps({**run, "json_path": str(vision)}) + "\n", encoding="utf-8")
+ if provider == "sqlite":
+ monkeypatch.setenv("NODE_OPTIONS", os.environ.get("NODE_OPTIONS", "") + " --experimental-sqlite")
+ initialize_canonical_source_fixture(runtime, GOAL, state_path=state, provider=provider)
+ original = state.read_bytes()
+ args = ("quota", "should-run", "--codex-app", "--goal-id", GOAL, "--agent-id", AGENT,
+ "--available-capability", "network", "--available-capability", "external_evidence_poll",
+ "--turn-instance-id")
+ rc, first = cli(registry, runtime, *args, "watch-first")
+ assert rc == 0, first
+ if scenario == "successor_due":
+ assert first["effective_action"] == "successor_replan_required"
+ assert first["selected_todo"]["todo_id"] == TODO
+ elif scenario == "monitor_due":
+ assert first["effective_action"] == "normal_run"
+ assert first["work_lane_contract"]["lane"] == "continuous_monitor"
+ assert first["selected_todo"]["todo_id"] == monitor
+ elif scenario == "before_due":
+ assert first["heartbeat_receipt"]["status"] == "committed"
+ rc, replay = cli(registry, runtime, *args, "watch-first")
+ assert rc == 0, replay
+ assert replay["heartbeat_receipt"]["status"] == "replayed"
+ rc, later = cli(registry, runtime, *args, "watch-second")
+ assert rc == 0, later
+ for guard in (first, replay, later):
+ assert guard["effective_action"] == "monitor_quiet_skip"
+ receipt = guard["heartbeat_receipt"]
+ assert receipt["stall_observation"] == "not_applicable"
+ assert "settlement_identity" not in receipt
+ assert guard["goal_frontier_projection"]["replan_required"] is False
+ assert guard["interaction_contract"]["agent_channel"]["quiet_noop_allowed"] is True
+ assert guard["vision_wait_state"]["resume_when"] == FUTURE
+ else:
+ assert first["heartbeat_receipt"]["stall_observation"] == "appended"
+ rc, replay = cli(registry, runtime, *args, "watch-first")
+ assert rc == 0, replay
+ assert replay["heartbeat_receipt"]["status"] == "replayed"
+ assert state.read_bytes() == original
+ runs = [json.loads(line) for line in index.read_text().splitlines()]
+ assert not any(run.get("classification") == "quota_slot_spent" for run in runs)
+ assert sum(run.get("classification") == "quota_monitor_poll" for run in runs) == (scenario == "unproven")
+
+
def test_due_vision_wait_restores_explicit_resume(tmp_path, isolated_effect):
registry, runtime, state = fixture(tmp_path, "resume_at:2026-01-01T00:00:00Z")
initialize_canonical_source_fixture(runtime, GOAL, state_path=state, provider="sqlite")
diff --git a/tests/control_plane/test_declared_terminal_settlement_cli.py b/tests/control_plane/test_declared_terminal_settlement_cli.py
index 3f7db27b93..f262d12ffa 100644
--- a/tests/control_plane/test_declared_terminal_settlement_cli.py
+++ b/tests/control_plane/test_declared_terminal_settlement_cli.py
@@ -275,9 +275,13 @@ def test_original_lease_and_failed_validator_remain_required_before_accounting(
@pytest.mark.parametrize("closeout_source", ["turn", "lifecycle"])
def test_no_followup_transition_recovers_bound_review_through_writeback_and_single_debit(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, provider: str, historical_terminals: int,
- closeout_source: str,
+ closeout_source: str, capsys: pytest.CaptureFixture[str],
) -> None:
- project, _, runtime, _, run, lease, marker, _, _ = _fixture(
+ from loopx.control_plane.effect_runtime import EffectRuntimeRejected
+ from loopx.control_plane.work_items import lifecycle_recovery
+ from loopx.entrypoint import main as cli_main
+
+ _, workspace, runtime, registry, run, lease, marker, _, _ = _fixture(
tmp_path, monkeypatch, provider, historical_terminals=historical_terminals,
)
def call(*args: str, expected: int = 0):
@@ -303,6 +307,25 @@ def call(*args: str, expected: int = 0):
turn = "turn-original-lifecycle-review"
guard = ("quota", "should-run", "--runtime-profile", "generic_cli", "--goal-id", cli.GOAL_ID,
"--agent-id", cli.AGENT_ID, "--turn-instance-id", turn, *capabilities)
+ exercise_source_failure = historical_terminals == 0 and closeout_source == "lifecycle"
+
+ def reenter_with_unavailable_source(expected: int = 0):
+ project_semantics = lifecycle_recovery.project_replan_semantics
+
+ def read_semantics(params):
+ if params.get("operation") == "lifecycle_source":
+ raise EffectRuntimeRejected("Original lifecycle review source is unavailable")
+ return project_semantics(params)
+
+ with monkeypatch.context() as patch:
+ patch.chdir(workspace)
+ patch.setattr(lifecycle_recovery, "project_replan_semantics", read_semantics)
+ code = cli_main(["--format", "json", "--registry", str(registry), "--runtime-root", str(runtime),
+ *guard, "--context-delivery", "off"])
+ payload = json.loads(capsys.readouterr().out)
+ assert code == expected, payload
+ return payload
+
admitted = call(*guard)
identity = admitted["heartbeat_receipt"]["settlement_identity"]
assert admitted["autonomous_replan_obligation"]["triggers"][0]["todo_id"] == cli.TODO_ID
@@ -319,6 +342,10 @@ def call(*args: str, expected: int = 0):
assert recovered.get("selected_todo") is None
assert recovered["replan_action_packet"]["settlement_only"] is True
assert recovered["replan_action_packet"]["writeback_contract"]["preferred_input"] == "evidence_linked_vision_path"
+ if exercise_source_failure:
+ rejected = reenter_with_unavailable_source(expected=1)
+ assert "Original lifecycle review source is unavailable" in str(rejected)
+ assert "settlement_owed" not in rejected
replan_binding = ("--agent-id", cli.AGENT_ID, "--replan-obligation-id", identity["replan_obligation_id"],
"--turn-instance-id", turn)
spend = ("quota", "spend-slot", "--goal-id", cli.GOAL_ID, *replan_binding,
@@ -343,10 +370,33 @@ def call(*args: str, expected: int = 0):
assert refreshed["settlement_progress"]["state"] == "spend_required"
pending = call(*guard)
assert pending["heartbeat_receipt"]["settlement_identity"] == identity
- assert pending["settlement_progress"]["state"] == "spend_required"
- assert call(*spend)["appended"] is True
- assert call(*spend)["appended"] is False
+ assert pending["settlement_progress"] == refreshed["settlement_progress"]
+ assert pending["replan_action_packet"]["required_outcome"] == "quota_spend_receipt"
+ assert pending["normal_delivery_allowed"] is False
+ assert pending.get("selected_todo") is None
+ if exercise_source_failure:
+ recovered_debit = reenter_with_unavailable_source()
+ assert recovered_debit["heartbeat_receipt"]["settlement_identity"] == identity
+ assert recovered_debit["settlement_owed"] == pending["settlement_owed"]
+ assert recovered_debit.get("selected_todo") is None
+ pending = recovered_debit
+ owed = pending["settlement_owed"]
+ assert owed["effect_id"] == identity["effect_id"]
+ assert owed["kind"] == "quota_spend"
+ assert cli._spend_run_count(runtime) == 1
+ projected_spend = cli._projected_cli_args(owed["command"], turn_instance_id=turn)
+ if exercise_source_failure:
+ foreign_spend = tuple("turn-without-original-writeback" if token == turn else token
+ for token in projected_spend)
+ assert call(*foreign_spend, expected=1).get("appended") is not True
+ assert cli._spend_run_count(runtime) == 1
+ assert call(*projected_spend)["appended"] is True
+ assert call(*projected_spend)["appended"] is False
assert call(*guard)["effective_action"] == "heartbeat_settled_skip"
+ if exercise_source_failure:
+ replayed = reenter_with_unavailable_source()
+ assert replayed["effective_action"] == "heartbeat_settled_skip"
+ assert "settlement_owed" not in replayed
after = call("todo", "list", "--goal-id", cli.GOAL_ID, "--todo-id", cli.TODO_ID)["todo"]
for field in ("completed_at", "completion_turn_key", "completion_validation_sha256"):
assert before[field] == after[field]
diff --git a/tests/control_plane/test_goal_create_completion.py b/tests/control_plane/test_goal_create_completion.py
index 0b0642a022..d105a7dbe8 100644
--- a/tests/control_plane/test_goal_create_completion.py
+++ b/tests/control_plane/test_goal_create_completion.py
@@ -254,6 +254,12 @@ def assert_retained_then_submit(*args, **kwargs):
status, recovered = request(f"/api/actions/{proposal['proposal_id']}/apply", {})
assert status == 202, recovered
ids = recovered["proposal"]["receipt"]["resource_ids"]
+ original_session = chat.load_session(ids["session_id"])
+ assert original_session["managed_agent_id"] == "codex"
+ assert original_session["channel_id"] == "agent.codex"
+ assert ids["managed_agent_id"] == original_session["managed_agent_id"]
+ assert ids["channel_id"] == original_session["channel_id"]
+ assert ids["goal_instance_id"] == original_session.get("goal_instance_id")
if confirmed is not None:
assert ids["session_id"] == sid and ids["turn_id"] == confirmed["turn_id"]
assert admission_calls == [], "confirmed first work is readback, not a new admission"
diff --git a/tests/control_plane/test_goal_create_session.py b/tests/control_plane/test_goal_create_session.py
new file mode 100644
index 0000000000..a776e38722
--- /dev/null
+++ b/tests/control_plane/test_goal_create_session.py
@@ -0,0 +1,384 @@
+"""Goal creation reaches its original Agent conversation and Task owners.
+
+The Chat/Core/providers are real; the app-server/model is synthetic. Explicit
+owner checks below qualify the integration, not automatic checker production.
+"""
+import hashlib
+import json
+import runpy
+import sys
+from pathlib import Path
+
+import pytest
+
+from test_new_goal_storage_defaults import environment as environment, app as app
+from test_goal_create_completion import preview_goal, inspect
+from loopx.chat_runtime import ChatRuntimeController
+from loopx.chat_store import ChatSessionStore
+from loopx.capabilities.native_chat.project_context import ChatProjectContexts
+from loopx.capabilities.native_chat.steward_managed_mode import ManagedSessionTurn
+from loopx.control_plane.coordination.local_authority import read_canonical_todos_if_promoted
+
+
+@pytest.fixture
+def host(environment, app):
+ _, _, _, project, runtime = environment
+ actions, request, service = app
+ capture, binary = runtime / "requests.jsonl", runtime / "synthetic-codex"
+ source = runpy.run_path(str(Path(__file__).parents[2] / "examples/loopx-chat-runtime-smoke.py"))["FAKE_CODEX"]
+ binary.write_text(source.replace(' method = request.get("method")',
+ f' with open({str(capture)!r}, "a") as stream:\n stream.write(json.dumps(request) + "\\n")\n'
+ ' method = request.get("method")'))
+ binary.chmod(0o700)
+ chat = ChatSessionStore(runtime / "chat")
+ controller = ChatRuntimeController(store=chat, codex_bin=str(binary), registry_path=service.registry_path,
+ project_contexts=ChatProjectContexts([project]))
+ service.chat_store, service.runtime_controller = chat, controller
+ try:
+ yield actions, request, controller, capture, project, runtime
+ finally:
+ controller.close()
+
+
+def create(host):
+ _, request, controller, *_ = host
+ proposal = preview_goal(request)
+ status, result = request(f"/api/actions/{proposal['proposal_id']}/apply", {})
+ assert status == 202, result
+ ids = result["proposal"]["receipt"]["resource_ids"]
+ first = controller.wait_for_turn(session_id=ids["session_id"], turn_id=ids["turn_id"], timeout_sec=10)
+ assert first["status"] == "completed", first
+ return proposal, ids, first
+
+
+def test_creation_display_preserves_user_intent_and_the_internal_execution_request(host):
+ proposal, ids, first = create(host)
+ store = host[2].store
+ assert store.messages(ids["session_id"])[0]["text"] == proposal["normalized_parameters"]["objective"]
+ assert first["message"].startswith("开始推进 Goal")
+
+
+def test_creation_display_recovery_retains_the_original_historical_message(host):
+ from loopx.chat.session_open import goal_creation_display_message
+ create(host)
+ store, project = host[2].store, host[4]
+ session, _ = host[2].open_session(goal_id="report", agent_id="codex", work_dir=project,
+ objective="Historical objective", mode="new", channel_id="goal.report", agent_goal_id="report")
+ turn, _ = store.create_turn(session["session_id"], client_turn_id="historical-first",
+ message="Historical internal execution request", display_message="The original confirmed request")
+ assert goal_creation_display_message(store, session_id=session["session_id"],
+ client_turn_id="historical-first", objective="Later objective") == "The original confirmed request"
+ assert store.load_turn(session["session_id"], turn["turn_id"])["message"] == "Historical internal execution request"
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_first_goal_retains_ordinary_messages_in_the_registered_session(environment, host, provider):
+ configure, *_ = environment
+ configure(provider)
+ _, request, controller, capture, project, runtime = host
+ proposal, ids, first = create(host)
+ chat, sid = controller.store, ids["session_id"]
+ original = chat.load_session(sid)
+ assert original["channel_id"] == "agent.codex" and original["managed_agent_id"] == "codex"
+ assert original["managed_workspace_path"] == str(project)
+ assert first.get("loopx_execution") is not True and "native_loopx" not in first["response"]
+ assert controller.adapters[sid].session.sandbox == "read-only"
+ history = chat.messages(sid)
+ followup, created = controller.submit_turn(session_id=sid, client_turn_id="original-followup",
+ message="Retain the introduction while preparing the appendix.", work_dir=project, objective="Deliver the report.")
+ assert created
+ completed = controller.wait_for_turn(session_id=sid, turn_id=followup["turn_id"], timeout_sec=10)
+ assert completed["status"] == "completed" and "native_loopx" not in completed["response"]
+ assert chat.messages(sid)[:len(history)] == history
+ assert chat.load_session(sid)["upstream_thread_id"] == original["upstream_thread_id"]
+ calls = [json.loads(line) for line in capture.read_text().splitlines()]
+ assert sum(row["method"] == "thread/start" for row in calls) == 1
+ assert sum(row["method"] == "turn/start" for row in calls) == 2
+ before_replay = capture.read_bytes()
+ status, replay = request(f"/api/actions/{proposal['proposal_id']}/apply", {})
+ assert status == 202 and replay["proposal"]["receipt"]["resource_ids"] == ids
+ assert capture.read_bytes() == before_replay and len(chat.list_sessions(goal_id="report")) == 1
+ authority = read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="report", include_leases=True)
+ assert [(todo["todo_id"], todo["status"]) for todo in authority["todos"]] == [(ids["todo_ids"][0], "open")]
+ assert inspect(project, runtime)["goal_acceptance_contract"]["outcome_requirements"]["outcomes"][0]["requirements"][0]["status"] == "unverified"
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("window", ["simple", "competing", "historical", "historical_competing", "historical_missing_metadata"])
+def test_lost_session_open_response_reuses_the_original_registered_route(environment, host, monkeypatch, provider, window):
+ configure, *_ = environment
+ configure(provider)
+ actions, request, controller, capture, *_ = host
+ proposal = preview_goal(request)
+ open_session = controller.open_session
+ opening = {}
+ def lost_response(**kwargs):
+ if window.startswith("historical"):
+ kwargs.pop("managed_agent_id", None)
+ kwargs.pop("session_id", None)
+ session, _ = open_session(**kwargs)
+ opening.update(session=session, arguments=kwargs)
+ raise TimeoutError("Session opening response lost before the checkpoint")
+ monkeypatch.setattr(controller, "open_session", lost_response)
+ path = f"/api/actions/{proposal['proposal_id']}/apply"
+ assert request(path, {})[0] == 424
+ sid = controller.store.list_sessions(goal_id="report")[0]["session_id"]
+ assert "first_session_opened" not in actions.load(proposal["proposal_id"])["checkpoint"]["steps"]
+ monkeypatch.setattr(controller, "open_session", open_session)
+ if window == "historical_missing_metadata":
+ path_to_metadata = controller.store._session_path(sid)
+ path_to_metadata.rename(path_to_metadata.with_suffix(".saved"))
+ if window.endswith("competing"):
+ arguments = {**opening["arguments"], "mode": "new"}
+ arguments.pop("session_id", None)
+ other, _ = open_session(**arguments)
+ assert other["session_id"] != sid
+ status, recovered = request(path, {})
+ if window.startswith("historical"):
+ assert status == 400 and ("ambiguous" if window == "historical_competing" else "unconfirmed") in recovered["error"], recovered
+ assert controller.store.turn_for_client(sid, f"goal-start-{proposal['proposal_id']}") is None
+ return
+ assert status == 202, recovered
+ ids = recovered["proposal"]["receipt"]["resource_ids"]
+ assert ids["session_id"] == sid
+ assert ("managed_agent_id" in ids) is not window.startswith("historical")
+ assert controller.wait_for_turn(session_id=sid, turn_id=ids["turn_id"], timeout_sec=10)["status"] == "completed"
+ assert len(controller.store.list_sessions(goal_id="report")) == (2 if window == "competing" else 1)
+ calls = [json.loads(line) for line in capture.read_text().splitlines()]
+ assert sum(row["method"] == "thread/start" for row in calls) == (2 if window == "competing" else 1)
+ assert sum(row["method"] == "turn/start" for row in calls) == 1
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_new_open_checks_the_persisted_session_before_certifying_its_route(environment, host, monkeypatch, provider):
+ configure, *_ = environment
+ configure(provider)
+ actions, request, controller, capture, *_ = host
+ proposal = preview_goal(request)
+ open_session = controller.open_session
+ original = {}
+ def changed_before_response(**kwargs):
+ session, resumed = open_session(**kwargs)
+ original.update(session)
+ stored = {**session, "channel_id": "goal.report", "managed_agent_id": None}
+ stored.pop("managed_workspace_path", None)
+ controller.store._session_path(session["session_id"]).write_text(json.dumps(stored))
+ return session, resumed
+ monkeypatch.setattr(controller, "open_session", changed_before_response)
+ status, failed = request(f"/api/actions/{proposal['proposal_id']}/apply", {})
+ assert status == 400 and "Session identity changed" in failed["error"], failed
+ assert "first_session_opened" not in actions.load(proposal["proposal_id"])["checkpoint"]["steps"]
+ assert controller.store.turn_for_client(original["session_id"], f"goal-start-{proposal['proposal_id']}") is None
+ calls = [json.loads(line) for line in capture.read_text().splitlines()]
+ assert not [row for row in calls if row["method"] == "turn/start"]
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_explicit_native_budget_retains_the_legacy_goal_channel(environment, host, monkeypatch, provider):
+ configure, *_ = environment
+ configure(provider)
+ actions, request, controller, capture, *_ = host
+ proposal = preview_goal(request, native_token_budget=100)
+ save = actions.save_checkpoint
+ def interrupt(*args, **kwargs):
+ result = save(*args, **kwargs)
+ if kwargs.get("step") == "first_session_opened":
+ raise OSError("Stop the synthetic host before its explicit /goal request")
+ return result
+ monkeypatch.setattr(actions, "save_checkpoint", interrupt)
+ status, result = request(f"/api/actions/{proposal['proposal_id']}/apply", {})
+ assert status == 424, result
+ sid = actions.load(proposal["proposal_id"])["checkpoint"]["steps"]["first_session_opened"]["session_id"]
+ session = controller.store.load_session(sid)
+ assert session["channel_id"] == "goal.report" and session.get("managed_agent_id") is None
+ assert controller.store.turn_for_client(sid, f"goal-start-{proposal['proposal_id']}") is None
+ assert not [json.loads(line) for line in capture.read_text().splitlines() if json.loads(line)["method"] == "turn/start"]
+
+
+def test_concurrent_session_opening_reuses_one_published_identity_and_no_new_adapter_on_replay(host):
+ from concurrent.futures import ThreadPoolExecutor
+
+ _, _, controller, capture, project, _ = host
+ create(host)
+ secondary = ChatRuntimeController(store=controller.store, codex_bin=controller.codex_bin,
+ registry_path=controller.registry_path, project_contexts=ChatProjectContexts([project]))
+ arguments = dict(goal_id="report", agent_id="codex", managed_agent_id="codex",
+ work_dir=project, objective="Deliver the report.", mode="new", session_id="concurrent-original")
+ before = capture.read_bytes()
+ try:
+ with ThreadPoolExecutor(max_workers=2) as workers:
+ opened = list(workers.map(lambda runtime: runtime.open_session(**arguments), [controller, secondary]))
+ assert [row[0]["session_id"] for row in opened] == ["concurrent-original", "concurrent-original"]
+ assert sorted(row[1] for row in opened) == [False, True]
+ assert opened[0][0] == opened[1][0]
+ calls = [json.loads(line) for line in capture.read_bytes()[len(before):].splitlines()]
+ assert sum(row["method"] == "thread/start" for row in calls) == 1
+ before_replay = capture.read_bytes()
+ original = controller.store.load_session("concurrent-original")
+ result, resumed = secondary.open_session(**arguments)
+ assert resumed and result == original and capture.read_bytes() == before_replay
+ finally:
+ secondary.close()
+
+
+@pytest.mark.parametrize("reservation", ["directory", "dangling-link", "metadata-link"])
+def test_unknown_physical_session_reservation_never_starts_another_native_thread(host, reservation):
+ _, _, controller, capture, project, _ = host
+ create(host)
+ directory = controller.store._session_dir("unknown-opening")
+ if reservation == "directory":
+ directory.mkdir()
+ elif reservation == "dangling-link":
+ directory.symlink_to(directory.parent / "missing-target", target_is_directory=True)
+ else:
+ directory.mkdir()
+ controller.store._session_path("unknown-opening").symlink_to(directory.parent / "missing-metadata.json")
+ before = capture.read_bytes()
+ with pytest.raises(ValueError, match="unreadable|unavailable"):
+ controller.open_session(goal_id="report", agent_id="codex", managed_agent_id="codex",
+ work_dir=project, objective="Deliver the report.", mode="new", session_id="unknown-opening")
+ assert capture.read_bytes() == before
+
+
+def test_exact_session_opening_cannot_select_the_resume_latest_branch(host):
+ _, _, controller, capture, project, _ = host
+ create(host)
+ before = capture.read_bytes()
+ with pytest.raises(ValueError, match="mode=new"):
+ controller.open_session(goal_id="report", agent_id="codex", managed_agent_id="codex",
+ work_dir=project, objective="Deliver the report.", mode="resume_latest", session_id="another-opening")
+ assert capture.read_bytes() == before
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("legacy", [False, True], ids=["registered", "historical-legacy"])
+def test_interrupted_first_session_recovers_its_recorded_route(environment, host, monkeypatch, provider, legacy):
+ configure, *_ = environment
+ configure(provider)
+ actions, request, controller, capture, *_ = host
+ proposal = preview_goal(request)
+ save, open_session = actions.save_checkpoint, controller.open_session
+ if legacy:
+ def old_open(**kwargs):
+ # Stop today's producer before opening, then restore a checkpoint
+ # genuinely written by the historical unbound Session producer.
+ raise OSError("Restore the original historical Session checkpoint")
+ monkeypatch.setattr(controller, "open_session", old_open)
+ def interrupt(*args, **kwargs):
+ result = save(*args, **kwargs)
+ if kwargs.get("step") == "first_session_opened":
+ raise OSError("Interrupted before dispatch")
+ return result
+ monkeypatch.setattr(actions, "save_checkpoint", interrupt)
+ path = f"/api/actions/{proposal['proposal_id']}/apply"
+ assert request(path, {})[0] == 424
+ if legacy:
+ project = host[4]
+ session, _ = open_session(goal_id="report", agent_id="codex", work_dir=project,
+ objective="Deliver the report.", mode="new", channel_id="goal.report", agent_goal_id="report")
+ save(proposal["proposal_id"], step="first_session_opened",
+ receipt={"outcome": "first_session_opened", "session_id": session["session_id"]})
+ recorded = actions.load(proposal["proposal_id"])["checkpoint"]["steps"]["first_session_opened"]
+ sid = recorded["session_id"]
+ original = controller.store.load_session(sid)
+ assert original["channel_id"] == ("goal.report" if legacy else "agent.codex")
+ monkeypatch.setattr(actions, "save_checkpoint", save)
+ monkeypatch.setattr(controller, "open_session", open_session)
+ status, resumed = request(path, {})
+ assert status == 202, resumed
+ ids = resumed["proposal"]["receipt"]["resource_ids"]
+ assert ids["session_id"] == sid
+ turn = controller.wait_for_turn(session_id=sid, turn_id=ids["turn_id"], timeout_sec=10)
+ assert turn["status"] == "completed"
+ assert ("managed_agent_id" in ids) is not legacy
+ assert controller.store.load_session(sid)["channel_id"] == original["channel_id"]
+ calls = [json.loads(line) for line in capture.read_text().splitlines()]
+ assert sum(row["method"] == "thread/start" for row in calls) == 1
+ assert sum(row["method"] == "turn/start" for row in calls) == 1
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("field,value", [("channel_id", "goal.report"), ("managed_agent_id", "other-owner"),
+ ("goal_instance_id", "replaced-lifetime"), ("managed_workspace_path", "/unavailable-workspace")])
+def test_recovery_refuses_a_changed_original_session_route(environment, host, monkeypatch, provider, field, value):
+ configure, *_ = environment
+ configure(provider)
+ actions, request, controller, capture, *_ = host
+ proposal = preview_goal(request)
+ save = actions.save_checkpoint
+ def interrupt(*args, **kwargs):
+ result = save(*args, **kwargs)
+ if kwargs.get("step") == "first_session_opened":
+ raise OSError("Interrupted before dispatch")
+ return result
+ monkeypatch.setattr(actions, "save_checkpoint", interrupt)
+ path = f"/api/actions/{proposal['proposal_id']}/apply"
+ assert request(path, {})[0] == 424
+ monkeypatch.setattr(actions, "save_checkpoint", save)
+ sid = actions.load(proposal["proposal_id"])["checkpoint"]["steps"]["first_session_opened"]["session_id"]
+ session = controller.store.load_session(sid)
+ controller.store._session_path(sid).write_text(json.dumps({**session, field: value}))
+ before = capture.read_bytes()
+ status, failed = request(path, {})
+ assert status == 400, failed
+ assert "Session" in failed["error"]
+ assert capture.read_bytes() == before and len(controller.store.list_sessions(goal_id="report")) == 1
+ assert controller.store.turn_for_client(sid, f"goal-start-{proposal['proposal_id']}") is None
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("checks", ["valid", "missing", "failed"])
+def test_created_session_adopts_original_task_only_with_independent_owner_check(environment, host, provider, checks):
+ from loopx.control_plane.goals.acceptance import configure_goal_acceptance
+
+ configure, *_ = environment
+ configure(provider)
+ _, _, controller, capture, project, runtime = host
+ _, ids, _ = create(host)
+ sid, todo = ids["session_id"], ids["todo_ids"][0]
+ original = controller.store.load_session(sid)
+ history = controller.store.messages(sid)
+ artifact, checker = project / "report.txt", project / "check-report.py"
+ expected = "Introduction\nAppendix\n"
+ checker.write_text("from pathlib import Path\nassert Path('report.txt').read_text() == 'Introduction\\nAppendix\\n'\n")
+ if checks != "missing":
+ basis = inspect(project, runtime)
+ configure_goal_acceptance(registry_path=controller.registry_path, runtime_root=str(runtime), goal_id="report",
+ expected_provider_revision=basis["provider_revision"], execute=True,
+ document={"scope": {"kind": "selected_work", "todo_ids": [todo]}, "objective": "Deliver both report sections",
+ "non_goals": [], "criteria": [{"id": "complete-report", "description": "Both approved sections are present",
+ "validation_argv": [sys.executable, "check-report.py"], "validation_timeout_seconds": 5,
+ "validation_files": [{"path": "check-report.py", "sha256": hashlib.sha256(checker.read_bytes()).hexdigest()}]}],
+ "bindings": [{"todo_id": todo, "criterion_ids": ["complete-report"]}]})
+ candidate = {"result_kind": "validated_completion", "classification": "implementation",
+ "summary": "The report includes both sections.", "next_action": "Read back the original report.",
+ "delivery_batch_scale": "implementation", "delivery_outcome": "outcome_progress",
+ "vision_unchanged_reason": "The original approved report remains the current task.",
+ "work_evidence": ["report.txt"], "validation_evidence": ["The independent checker reads both original sections"]}
+ binary = Path(controller.codex_bin)
+ source = binary.read_text().replace('"message": "Runtime response."', '"message": ' + repr(json.dumps(candidate)))
+ artifact_content = expected if checks != "failed" else "Introduction\n"
+ source = source.replace(' turn_count += 1',
+ f' open({str(artifact)!r}, "w").write({artifact_content!r})\n turn_count += 1')
+ binary.write_text(source)
+ controller.adapters.pop(sid).close_session()
+ before = capture.read_bytes()
+ native = ManagedSessionTurn(controller)
+ accepted, created = native.submit(sid, agent_id="codex", operation_id="original-task", todo_id=todo)
+ assert created
+ completed = controller.wait_for_turn(session_id=sid, turn_id=accepted["turn_id"], timeout_sec=15)
+ result = completed["response"]["native_loopx"]
+ assert result["status"] == {"valid": "committed", "missing": "stopped", "failed": "failed"}[checks], result
+ assert result["effects"]["quota_spent"] is (checks == "valid")
+ assert result["effects"]["state_written"] is (checks == "valid")
+ authority = read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="report", include_leases=True)
+ assert next(row for row in authority["todos"] if row["todo_id"] == todo)["status"] == ("done" if checks == "valid" else "open")
+ calls = [json.loads(line) for line in capture.read_bytes()[len(before):].splitlines()]
+ assert sum(row["method"] == "turn/start" for row in calls) == (0 if checks == "missing" else 1)
+ assert not [row for row in calls if row["method"] == "thread/start"]
+ assert controller.store.messages(sid)[:len(history)] == history
+ assert controller.store.load_session(sid)["upstream_thread_id"] == original["upstream_thread_id"]
+ assert len(controller.store.list_sessions(goal_id="report")) == 1
+ after = capture.read_bytes()
+ replay, created = native.submit(sid, agent_id="codex", operation_id="original-task", todo_id=todo)
+ assert not created and replay["turn_id"] == accepted["turn_id"] and capture.read_bytes() == after
diff --git a/tests/control_plane/test_goal_session_check_preparation.py b/tests/control_plane/test_goal_session_check_preparation.py
new file mode 100644
index 0000000000..02a6bebcc8
--- /dev/null
+++ b/tests/control_plane/test_goal_session_check_preparation.py
@@ -0,0 +1,915 @@
+"""Original managed Chat preparation over real File/SQLite acceptance owners.
+
+The native model transport is synthetic. Preparation pins preexisting sources
+and persists a proposal without configuring acceptance or running a checker.
+"""
+
+from copy import deepcopy
+from contextlib import contextmanager, nullcontext
+import hashlib
+import json
+import queue
+import sys
+from types import SimpleNamespace
+
+import pytest
+
+from loopx.chat.coordination import prepare_turn_context, check_preparation_tool
+from loopx.chat_agent import CodexChatAgentSession
+from loopx.chat_runtime import ChatRuntimeController, CodexAppServerAdapter
+from loopx.chat_store import ChatSessionStore
+from loopx.chat_action_store import ChatActionStore
+from loopx.control_plane.collaboration import conversation_scope
+from loopx.control_plane.goals.acceptance import refine_goal_outcome
+from test_goal_acceptance_cli import acceptance_goal as acceptance_goal
+
+
+@pytest.fixture
+def original_check(acceptance_goal, monkeypatch):
+ project, _, cli, _ = acceptance_goal
+ root, registry = project.parent / "runtime", project.parent / "registry.json"
+ refined = refine_goal_outcome(
+ registry_path=registry,
+ runtime_root=str(root),
+ goal_id="goal-acceptance",
+ agent_id="agent-a",
+ operation_id="original-owner-outcome",
+ expected_provider_revision=cli("inspect")[1]["provider_revision"],
+ execute=True,
+ refinement={
+ "outcome_id": "goal-creation",
+ "owner_agent_id": "agent-a",
+ "objective": "Produce a usable export",
+ "todo_ids": ["todo_export"],
+ "requirements": [
+ {
+ "id": "completion",
+ "description": "The exported artifact has the expected content",
+ }
+ ],
+ "source": {
+ "ref": "goal-create:fixture:completion",
+ "summary": "Owner-approved completion criteria",
+ },
+ },
+ )
+ assert refined["status"] == "applied", refined
+ checker = project / "check.py"
+ checker.write_text(
+ "from pathlib import Path\nassert Path('artifact.txt').read_text() == 'accepted'\n"
+ )
+ store = ChatSessionStore(root)
+ runtime = ChatRuntimeController(
+ store=store, codex_bin="unused", registry_path=registry
+ )
+ session = store.create_session(
+ goal_id="goal-acceptance",
+ managed_agent_id="agent-a",
+ managed_workspace_path=str(project.resolve()),
+ channel_id="agent.agent-a",
+ agent_id="codex",
+ executor_endpoint_id="codex",
+ adapter_kind="codex_app_server",
+ upstream_thread_id="original-thread",
+ upstream_mode="chat",
+ )
+ turn, _ = store.create_turn(
+ session["session_id"],
+ client_turn_id="prepare-original-check",
+ message="Prepare the independent delivery check for review.",
+ )
+ store.update_turn(session["session_id"], turn["turn_id"], status="running")
+ store.update_session(session["session_id"], active_turn_id=turn["turn_id"])
+ native = CodexChatAgentSession(
+ process=SimpleNamespace(poll=lambda: None),
+ messages=queue.Queue(),
+ thread_id="original-thread",
+ work_dir=project,
+ )
+ monkeypatch.setattr(native, "close", lambda: None)
+ adapter = CodexAppServerAdapter(native)
+ runtime.adapters[session["session_id"]] = adapter
+ session = store.load_session(session["session_id"])
+ prepared_adapter, context = prepare_turn_context(
+ runtime,
+ adapter,
+ session,
+ turn["turn_id"],
+ lambda *_: None,
+ scope=conversation_scope(session, origin="web"),
+ )
+ assert prepared_adapter is adapter and context["scope"] == "owner_goal"
+ document = {
+ "outcome_id": "goal-creation",
+ "criteria": [
+ {
+ "id": "completion",
+ "validation_argv": ["python3", "check.py"],
+ "validation_timeout_seconds": 5,
+ "oracle": {
+ "kind": "owner_pinned_oracle",
+ "source_id": "export-checker",
+ "input_refs": [],
+ "output_refs": ["artifact.txt"],
+ },
+ }
+ ],
+ }
+
+ def call(**updates):
+ return native.read_tool_handler(
+ "loopx_collaboration",
+ {
+ "action": "prepare_check",
+ "operation_id": "original-check",
+ "document": document,
+ **updates,
+ },
+ )
+
+ try:
+ yield SimpleNamespace(
+ project=project,
+ checker=checker,
+ root=root,
+ registry=registry,
+ store=store,
+ runtime=runtime,
+ session=session,
+ turn=turn,
+ native=native,
+ adapter=adapter,
+ document=document,
+ call=call,
+ cli=cli,
+ )
+ finally:
+ runtime.close()
+
+
+def test_original_read_only_turn_prepares_one_host_pinned_proposal(original_check):
+ fixture = original_check
+ before = fixture.cli("inspect")[1]
+ session_before = fixture.store.load_session(fixture.session["session_id"])
+ source_before = fixture.checker.read_bytes()
+ result = fixture.call()
+ assert result["ok"] and result["prepared"] and result["executed"] is False, (
+ result.get("next_action", result)
+ )
+ proposal = result["proposal"]
+ parameters = proposal["normalized_parameters"]
+ assert parameters["goal_id"] == fixture.session["goal_id"]
+ assert parameters["prepared_by"] == "agent-a"
+ criterion = parameters["document"]["criteria"][0]
+ assert criterion["validation_argv"] == ["python3", "check.py"]
+ assert criterion["validation_files"] == [
+ {"path": "check.py", "sha256": hashlib.sha256(source_before).hexdigest()}
+ ]
+ assert criterion["oracle"]["output_refs"] == ["artifact.txt"]
+ assert not (fixture.project / "artifact.txt").exists(), (
+ "Preparation must allow unfinished delivery"
+ )
+ assert fixture.call()["proposal"] == proposal
+ assert fixture.cli("inspect")[1] == before
+ assert fixture.store.load_session(fixture.session["session_id"]) == session_before
+ assert fixture.checker.read_bytes() == source_before
+ assert len(ChatActionStore(fixture.root / "chat/actions").list()) == 1
+
+
+@pytest.mark.parametrize(
+ "updates",
+ [
+ {"goal_id": "another-goal"},
+ {"agent_id": "other-agent"},
+ {"prepared_by": "other-agent"},
+ {"workspace": "another-workspace"},
+ {"runtime_root": "another-runtime"},
+ {"source_id": "remote"},
+ {"action": "start"},
+ {"action": "outcomes"},
+ {"operation_id": ""},
+ ],
+)
+def test_caller_cannot_replace_host_identity_or_action(original_check, updates):
+ assert original_check.call(**updates)["error"] == "collaboration_request_rejected"
+ assert ChatActionStore(original_check.root / "chat/actions").list() == []
+
+
+@pytest.mark.parametrize(
+ "change",
+ [
+ "agent",
+ "workspace",
+ "thread",
+ "channel",
+ "closed",
+ "other-turn",
+ "terminal",
+ "external",
+ "native",
+ "legacy",
+ "review",
+ "lifetime",
+ "registration",
+ "registry-workspace",
+ "goal-stopped",
+ ],
+)
+def test_preparation_rechecks_original_session_turn_and_registration(
+ original_check, change
+):
+ fixture = original_check
+ sid, tid = fixture.session["session_id"], fixture.turn["turn_id"]
+ fields = {
+ "agent": {"managed_agent_id": "other-agent"},
+ "workspace": {"managed_workspace_path": "other"},
+ "thread": {"upstream_thread_id": "replacement"},
+ "channel": {"channel_id": "goal.other"},
+ "closed": {"status": "closed"},
+ "other-turn": {"active_turn_id": "another-turn"},
+ "legacy": {"loopx_mode": {"enabled": True}},
+ }
+ if change in fields:
+ if change in {"agent", "workspace", "channel"}:
+ # These fields are immutable through the public API. Corrupt only
+ # this disposable input to qualify defensive source readback.
+ from loopx.chat_store import _atomic_write_json
+
+ path = fixture.store._session_path(sid)
+ _atomic_write_json(
+ path, {**fixture.store.load_session(sid), **fields[change]}
+ )
+ else:
+ fixture.store.update_session(sid, **fields[change])
+ elif change in {"terminal", "external", "native"}:
+ if change == "external":
+ from loopx.chat_store import _atomic_write_json
+
+ _atomic_write_json(
+ fixture.store._turn_path(sid, tid),
+ {**fixture.store.load_turn(sid, tid), "origin": "lark"},
+ )
+ else:
+ fixture.store.update_turn(
+ sid,
+ tid,
+ **(
+ {"status": "completed"}
+ if change == "terminal"
+ else {"loopx_execution": True}
+ ),
+ )
+ elif change == "review":
+ fixture.native.read_only_review = True
+ else:
+ registry = json.loads(fixture.registry.read_text())
+ goal = registry["goals"][0]
+ if change == "lifetime":
+ goal["goal_instance_id"] = "another-lifetime"
+ elif change == "registration":
+ goal["coordination"]["registered_agents"] = []
+ elif change == "registry-workspace":
+ goal["repo"] = str(fixture.project.parent)
+ else:
+ goal["status"] = "paused"
+ fixture.registry.write_text(json.dumps(registry))
+ result = fixture.call()
+ assert result["error"] == "collaboration_request_rejected", result
+ assert ChatActionStore(fixture.root / "chat/actions").list() == []
+
+
+@pytest.mark.parametrize(
+ "change",
+ [
+ "claimed-pin",
+ "inline-python",
+ "python-options",
+ "extra-argv",
+ "other-interpreter",
+ "absolute",
+ "parent",
+ "symlink",
+ "symlink-parent",
+ "missing",
+ "directory",
+ "source-output",
+ "missing-outputs",
+ "output-symlink",
+ ],
+)
+def test_preparation_rejects_unusable_or_unpinned_checker_sources(
+ original_check, change
+):
+ fixture = original_check
+ document = deepcopy(fixture.document)
+ row = document["criteria"][0]
+ if change == "claimed-pin":
+ row["validation_files"] = [{"path": "check.py", "sha256": "0" * 64}]
+ elif change == "inline-python":
+ row["validation_argv"] = ["python3", "-c", "pass"]
+ elif change == "python-options":
+ row["validation_argv"] = ["python3", "-I", "check.py"]
+ elif change == "extra-argv":
+ row["validation_argv"].append("--ignore-failure")
+ elif change == "other-interpreter":
+ row["validation_argv"] = ["arbitrary-command", "check.py"]
+ elif change == "absolute":
+ row["validation_argv"][1] = str(fixture.checker)
+ elif change == "parent":
+ row["validation_argv"][1] = "../project/check.py"
+ elif change in {"symlink", "missing", "directory"}:
+ fixture.checker.unlink()
+ if change == "symlink":
+ fixture.checker.symlink_to(fixture.project.parent / "acceptance.json")
+ elif change == "directory":
+ fixture.checker.mkdir()
+ elif change == "symlink-parent":
+ (fixture.project / "linked").symlink_to(
+ fixture.project, target_is_directory=True
+ )
+ row["validation_argv"][1] = "linked/check.py"
+ elif change == "source-output":
+ row["oracle"]["output_refs"] = ["check.py"]
+ elif change == "missing-outputs":
+ row["oracle"]["output_refs"] = []
+ else:
+ (fixture.project / "artifact.txt").symlink_to(fixture.checker)
+ result = fixture.call(document=document)
+ assert result["error"] == "collaboration_request_rejected", result
+ assert ChatActionStore(fixture.root / "chat/actions").list() == []
+
+
+def test_scope_change_during_source_read_is_refused_before_proposal(
+ original_check, monkeypatch
+):
+ import loopx.chat.coordination as coordination
+
+ observe = coordination._workspace_file
+
+ def replace_scope(*args, **kwargs):
+ result = observe(*args, **kwargs)
+ original_check.store.update_session(
+ original_check.session["session_id"], active_turn_id="later-turn"
+ )
+ return result
+
+ monkeypatch.setattr(coordination, "_workspace_file", replace_scope)
+ assert original_check.call()["error"] == "collaboration_request_rejected"
+ assert ChatActionStore(original_check.root / "chat/actions").list() == []
+
+
+def test_changed_source_retry_preserves_saved_intent_and_original_pin(original_check):
+ fixture = original_check
+ original = fixture.call()["proposal"]
+ saved_pin = original["normalized_parameters"]["document"]["criteria"][0][
+ "validation_files"
+ ]
+ source = fixture.checker.read_bytes()
+ fixture.checker.write_bytes(source + b"# A different source version\n")
+ refused = fixture.call()
+ assert refused["error"] == "collaboration_request_rejected"
+ proposals = ChatActionStore(fixture.root / "chat/actions").list()
+ assert proposals == [original]
+ assert (
+ proposals[0]["normalized_parameters"]["document"]["criteria"][0][
+ "validation_files"
+ ]
+ == saved_pin
+ )
+ fixture.checker.write_bytes(source)
+ assert fixture.call()["proposal"] == original
+
+
+@pytest.mark.parametrize("interpreter", [None, "/usr/bin/another-python"])
+def test_unavailable_host_interpreter_refuses_preparation(
+ original_check, monkeypatch, interpreter
+):
+ monkeypatch.setattr(
+ "loopx.chat.coordination.shutil.which", lambda _name: interpreter
+ )
+ result = original_check.call()
+ assert result["error"] == "collaboration_request_rejected"
+ assert "host interpreter" in result["next_action"]
+ assert ChatActionStore(original_check.root / "chat/actions").list() == []
+
+
+@pytest.mark.parametrize("direct", [False, True])
+def test_source_changed_during_read_is_refused_before_proposal(
+ original_check, monkeypatch, direct
+):
+ import loopx.chat.coordination as coordination
+
+ observe = coordination._workspace_file
+ document = deepcopy(original_check.document)
+ selected_source = original_check.checker
+ if direct:
+ selected_source = original_check.project / "checks/verify"
+ selected_source.parent.mkdir()
+ selected_source.write_bytes(original_check.checker.read_bytes())
+ selected_source.chmod(0o755)
+ document["criteria"][0]["validation_argv"] = ["checks/verify"]
+ changed = False
+
+ def change_after_read(*args, **kwargs):
+ nonlocal changed
+ result = observe(*args, **kwargs)
+ if kwargs.get("required") and not changed:
+ changed = True
+ selected_source.write_text("raise SystemExit(0)\n")
+ return result
+
+ monkeypatch.setattr(coordination, "_workspace_file", change_after_read)
+ result = original_check.call(document=document)
+ assert result["error"] == "collaboration_request_rejected"
+ assert "source changed during preparation" in result["next_action"]
+ assert ChatActionStore(original_check.root / "chat/actions").list() == []
+
+
+def test_first_of_two_checker_sources_is_reobserved_before_proposal(
+ original_check, monkeypatch
+):
+ import loopx.chat.coordination as coordination
+
+ fixture = original_check
+ refine_goal_outcome(
+ registry_path=fixture.registry,
+ runtime_root=str(fixture.root),
+ goal_id=fixture.session["goal_id"],
+ agent_id="agent-a",
+ operation_id="additional-owner-requirement",
+ expected_provider_revision=fixture.cli("inspect")[1]["provider_revision"],
+ execute=True,
+ refinement={
+ "outcome_id": "goal-creation",
+ "requirements": [
+ {
+ "id": "additional",
+ "description": "The additional independent check succeeds",
+ }
+ ],
+ "source": {
+ "ref": "message:additional-owner-direction",
+ "summary": "The owner added another requirement",
+ },
+ },
+ )
+ (fixture.project / "additional.py").write_text(
+ "from pathlib import Path\nassert Path('artifact.txt').read_text() == 'accepted'\n"
+ )
+ document = deepcopy(fixture.document)
+ additional = deepcopy(document["criteria"][0])
+ additional.update(id="additional", validation_argv=["python3", "additional.py"])
+ additional["oracle"]["source_id"] = "additional-checker"
+ document["criteria"].append(additional)
+ observe = coordination._workspace_file
+
+ def replace_first_after_second(workspace, ref, **kwargs):
+ digest = observe(workspace, ref, **kwargs)
+ if ref == "additional.py" and kwargs["required"]:
+ fixture.checker.write_text("raise SystemExit(0)\n")
+ return digest
+
+ monkeypatch.setattr(coordination, "_workspace_file", replace_first_after_second)
+ result = fixture.call(document=document)
+ assert result["error"] == "collaboration_request_rejected"
+ assert "source changed during preparation" in result["next_action"]
+ assert ChatActionStore(fixture.root / "chat/actions").list() == []
+
+
+def test_unavailable_safe_file_observation_is_explicitly_refused(
+ original_check, monkeypatch
+):
+ monkeypatch.setattr("loopx.chat.coordination.os.supports_dir_fd", set())
+ result = original_check.call()
+ assert result["error"] == "collaboration_request_rejected"
+ assert "cannot safely inspect workspace checker sources" in result["next_action"]
+ assert ChatActionStore(original_check.root / "chat/actions").list() == []
+
+
+def test_direct_existing_checker_and_matching_claimed_pin_are_supported(original_check):
+ fixture = original_check
+ checks = fixture.project / "checks"
+ checks.mkdir()
+ executable = checks / "verify"
+ executable.write_text(
+ "#!/usr/bin/env python3\nfrom pathlib import Path\nassert Path('artifact.txt').read_text() == 'accepted'\n"
+ )
+ executable.chmod(0o755)
+ document = deepcopy(fixture.document)
+ row = document["criteria"][0]
+ row["validation_argv"] = ["checks/verify"]
+ row["validation_files"] = [
+ {
+ "path": "checks/verify",
+ "sha256": hashlib.sha256(executable.read_bytes()).hexdigest(),
+ }
+ ]
+ result = fixture.call(document=document)
+ assert result["prepared"] and result["executed"] is False, result
+ assert result["proposal"]["normalized_parameters"]["document"] == document
+
+
+@pytest.mark.parametrize("token", ["python", sys.executable])
+def test_supported_python_aliases_resolve_to_public_safe_host_token(
+ original_check, token
+):
+ document = deepcopy(original_check.document)
+ document["criteria"][0]["validation_argv"][0] = token
+ result = original_check.call(document=document)
+ assert result["prepared"] and result["executed"] is False, result
+ row = result["proposal"]["normalized_parameters"]["document"]["criteria"][0]
+ assert row["validation_argv"] == ["python3", "check.py"]
+
+
+def test_outer_dispatcher_preserves_read_owner_and_rejects_actor_override_in_document(
+ original_check,
+):
+ fixture = original_check
+ handler = fixture.native.read_tool_handler
+ arguments = {"view": "goals", "source_id": "remote"}
+ assert handler("loopx_context_read", arguments) == handler.read(
+ "loopx_context_read", arguments
+ )
+ document = {**fixture.document, "goal_id": "another-goal"}
+ assert fixture.call(document=document)["error"] == "collaboration_request_rejected"
+ assert ChatActionStore(fixture.root / "chat/actions").list() == []
+
+
+@pytest.mark.parametrize(
+ "managed,resume,legacy,execution,review,manager,expected",
+ [
+ (
+ True,
+ None,
+ False,
+ False,
+ False,
+ False,
+ ["loopx_context_read", "loopx_collaboration"],
+ ),
+ (False, None, False, False, False, False, ["loopx_context_read"]),
+ (True, "existing-thread", False, False, False, False, ["loopx_context_read"]),
+ (
+ False,
+ None,
+ True,
+ False,
+ False,
+ False,
+ ["loopx_context_read", "loopx_collaboration"],
+ ),
+ (True, None, False, True, False, False, []),
+ (True, None, False, False, True, False, []),
+ (True, None, False, False, False, True, ["loopx_manager_read"]),
+ ],
+)
+def test_prepare_projection_is_only_for_new_default_managed_threads(
+ tmp_path, monkeypatch, managed, resume, legacy, execution, review, manager, expected
+):
+ from loopx.capabilities.native_chat.codex_adapter import start_runtime_adapter
+
+ captured = []
+ monkeypatch.setattr(
+ CodexAppServerAdapter, "start", lambda **kwargs: captured.append(kwargs)
+ )
+ controller = SimpleNamespace(
+ codex_bin="unused",
+ coordination_runtime_root=tmp_path,
+ codex_home=None,
+ startup_timeout_sec=2,
+ idle_timeout_sec=2,
+ hard_timeout_sec=2,
+ )
+ start_runtime_adapter(
+ controller,
+ work_dir=tmp_path,
+ goal_id="loopx-manager" if manager else "goal-fixture",
+ native_context={"objective": "Observe", "runtime_profile": "restricted"},
+ manager_profile=None,
+ model_config={},
+ history=None,
+ resume_thread_id=resume,
+ execution_mode=execution,
+ project_context=None,
+ project_coordination=not manager,
+ loopx_tools=legacy,
+ check_preparation=managed,
+ codex_home_override=None,
+ read_only_review=review,
+ metadata_review_context=None,
+ answer_review_context=None,
+ )
+ tools = captured[0]["dynamic_tools"] or []
+ assert [tool["name"] for tool in tools] == expected
+ if (
+ managed
+ and not resume
+ and not legacy
+ and not execution
+ and not review
+ and not manager
+ ):
+ assert tools[-1]["inputSchema"]["properties"]["action"]["enum"] == [
+ "prepare_check"
+ ]
+ assert set(tools[-1]["inputSchema"]["properties"]) == {
+ "action",
+ "operation_id",
+ "document",
+ }
+ if legacy:
+ assert "start" in tools[-1]["inputSchema"]["properties"]["action"]["enum"]
+
+
+def test_prepare_projection_does_not_mutate_existing_collaboration_tool():
+ from loopx.chat_loopx_mode import TOOL
+
+ before = deepcopy(TOOL)
+ check_preparation_tool()
+ assert TOOL == before
+
+
+def _capture_native_dispatch(fixture, monkeypatch):
+ """Run the real send/request event loop with a synthetic guarded wire."""
+ captured = []
+
+ def wire(payload, *, write_guard=None, deadline=None):
+ with write_guard() if write_guard is not None else nullcontext():
+ captured.append(deepcopy(payload))
+ fixture.native.messages.put(
+ {"id": payload["id"], "result": {"turn": {"id": "transport-turn"}}}
+ )
+ fixture.native.messages.put(
+ {"method": "item/agentMessage/delta", "params": {"delta": "Ready."}}
+ )
+ fixture.native.messages.put(
+ {
+ "method": "turn/completed",
+ "params": {"turn": {"status": "completed"}},
+ }
+ )
+
+ monkeypatch.setattr(fixture.native, "_write", wire)
+ return captured
+
+
+def _native_task_policy(fixture):
+ return {
+ "type": "workspaceWrite",
+ "writableRoots": [str(fixture.project.resolve())],
+ "networkAccess": False,
+ "excludeTmpdirEnvVar": True,
+ "excludeSlashTmp": True,
+ }
+
+
+@pytest.mark.parametrize("writable", [False, True])
+def test_fenced_native_task_dispatch_then_ordinary_read_reset_preserves_session_defaults(
+ original_check, monkeypatch, writable
+):
+ fixture = original_check
+ captured = _capture_native_dispatch(fixture, monkeypatch)
+ guarded = []
+
+ @contextmanager
+ def admitted_host_write():
+ # This synthetic guard checks original transport identity only. The
+ # actual Core Task/lease/authority path is qualified by the HTTP suite.
+ fixture.native.read_tool_handler._current()
+ guarded.append("entered")
+ yield
+
+ fixture.native._launch_guard = admitted_host_write
+ before = (fixture.native.execution_mode, fixture.native.sandbox)
+ policy = (
+ _native_task_policy(fixture)
+ if writable
+ else {"type": "readOnly", "networkAccess": False}
+ )
+ fixture.adapter.start_native_task_turn(
+ "Perform the confirmed Task.", lambda *_: None, policy
+ )
+ native = captured[-1]["params"]
+ assert native["sandboxPolicy"] == policy
+ assert "permissions" not in native
+ if writable:
+ assert native["runtimeWorkspaceRoots"] == [str(fixture.project.resolve())]
+ else:
+ assert "runtimeWorkspaceRoots" not in native
+ assert (
+ "You are the execution agent for a confirmed LoopX Task"
+ in native["input"][0]["text"]
+ )
+ assert "Do not edit files, mutate LoopX state" not in native["input"][0]["text"]
+ assert guarded == ["entered"]
+ fixture.adapter.start_turn("Prepare the check for review.", lambda *_: None)
+ ordinary = captured[-1]["params"]
+ assert ordinary["sandboxPolicy"] == {"type": "readOnly", "networkAccess": False}
+ assert "runtimeWorkspaceRoots" not in ordinary
+ assert "Do not edit files, mutate LoopX state" in ordinary["input"][0]["text"]
+ fixture.adapter.start_turn_with_attachments(
+ "Read the original attachment.", lambda *_: None, []
+ )
+ assert captured[-1]["params"]["sandboxPolicy"] == {
+ "type": "readOnly",
+ "networkAccess": False,
+ }
+ assert (fixture.native.execution_mode, fixture.native.sandbox) == before
+ fixture.native.send("Read from the original direct source.")
+ assert captured[-1]["params"]["sandboxPolicy"] == {
+ "type": "readOnly",
+ "networkAccess": False,
+ }
+ assert all(frame["params"]["threadId"] == "original-thread" for frame in captured)
+ assert fixture.native._launch_guard is admitted_host_write
+
+
+@pytest.mark.parametrize(
+ "invalid", ["no-guard", "wrong-workspace", "extra-root", "network", "named-profile", "review"]
+)
+def test_native_task_transport_refuses_unbound_policy_or_missing_host_fence(
+ original_check, monkeypatch, invalid
+):
+ fixture = original_check
+ captured = _capture_native_dispatch(fixture, monkeypatch)
+ policy = _native_task_policy(fixture)
+
+ @contextmanager
+ def admitted_host_write():
+ yield
+
+ fixture.native._launch_guard = admitted_host_write
+ if invalid == "no-guard":
+ fixture.native._launch_guard = None
+ elif invalid == "wrong-workspace":
+ policy["writableRoots"] = [str(fixture.project.parent.resolve())]
+ elif invalid == "extra-root":
+ policy["writableRoots"].append(str(fixture.project.parent.resolve()))
+ elif invalid == "network":
+ policy["networkAccess"] = True
+ elif invalid == "named-profile":
+ fixture.native.permissions_profile = "existing-owner-profile"
+ else:
+ fixture.native.read_only_review = True
+ with pytest.raises(ValueError, match="current host launch guard"):
+ fixture.adapter.start_native_task_turn(
+ "Perform the Task.", lambda *_: None, policy
+ )
+ assert captured == []
+ assert (
+ fixture.native.execution_mode is False and fixture.native.sandbox == "read-only"
+ )
+ assert fixture.native._native_task_policy_used is False
+
+
+def test_revoked_host_launch_guard_refuses_native_frame_and_ordinary_turn_cannot_widen_policy(
+ original_check, monkeypatch
+):
+ fixture = original_check
+ captured = _capture_native_dispatch(fixture, monkeypatch)
+
+ @contextmanager
+ def revoked_host_write():
+ raise ValueError("the original Task authority was revoked")
+ yield
+
+ fixture.native._launch_guard = revoked_host_write
+ with pytest.raises(ValueError, match="authority was revoked"):
+ fixture.adapter.start_native_task_turn(
+ "Perform the Task.", lambda *_: None, _native_task_policy(fixture)
+ )
+ assert captured == []
+ with pytest.raises(ValueError, match="ordinary Chat Turn"):
+ fixture.native.send(
+ "Ordinary request.", sandbox_policy=_native_task_policy(fixture)
+ )
+ assert captured == []
+
+
+def test_uncertain_native_publication_retains_ordinary_policy_reset_requirement(
+ original_check, monkeypatch
+):
+ fixture = original_check
+ captured = _capture_native_dispatch(fixture, monkeypatch)
+ request = fixture.native._request
+
+ @contextmanager
+ def admitted_host_write():
+ yield
+
+ fixture.native._launch_guard = admitted_host_write
+
+ def uncertain_publication(*args, **kwargs):
+ raise OSError("native publication is unconfirmed")
+
+ monkeypatch.setattr(fixture.native, "_request", uncertain_publication)
+ with pytest.raises(OSError, match="publication is unconfirmed"):
+ fixture.adapter.start_native_task_turn(
+ "Perform the Task.", lambda *_: None, _native_task_policy(fixture)
+ )
+ assert fixture.native._native_task_policy_used is True and captured == []
+ monkeypatch.setattr(fixture.native, "_request", request)
+ fixture.native.send("Read the original evidence after recovery.")
+ assert captured[-1]["params"]["sandboxPolicy"] == {
+ "type": "readOnly",
+ "networkAccess": False,
+ }
+ assert captured[-1]["params"]["threadId"] == "original-thread"
+ assert (
+ fixture.native.execution_mode is False and fixture.native.sandbox == "read-only"
+ )
+
+
+@pytest.mark.parametrize("cold", [False, True], ids=["new", "cold-resume"])
+def test_verified_managed_session_construction_binds_direct_read_default_without_native_history(
+ original_check, monkeypatch, cold
+):
+ from loopx.chat.coordination import PROJECT_CONTEXT_VERSION
+
+ fixture = original_check
+ starts = []
+
+ def start(**kwargs):
+ starts.append(kwargs)
+ native = CodexChatAgentSession(
+ process=SimpleNamespace(poll=lambda: None),
+ messages=queue.Queue(),
+ thread_id=kwargs.get("resume_thread_id") or "new-managed-thread",
+ work_dir=kwargs["work_dir"],
+ )
+ monkeypatch.setattr(native, "close", lambda: None)
+ return CodexAppServerAdapter(native)
+
+ monkeypatch.setattr(CodexAppServerAdapter, "start", start)
+ monkeypatch.setattr(
+ fixture.runtime,
+ "capabilities",
+ lambda: [
+ {"agent_id": "codex", "available": True, "adapter_kind": "codex_app_server"}
+ ],
+ )
+ if cold:
+ fixture.store.update_turn(
+ fixture.session["session_id"], fixture.turn["turn_id"], status="completed"
+ )
+ fixture.store.update_session(
+ fixture.session["session_id"],
+ active_turn_id=None,
+ coordination_context_version=PROJECT_CONTEXT_VERSION,
+ )
+ fixture.runtime.adapters.pop(fixture.session["session_id"])
+ session = fixture.store.load_session(fixture.session["session_id"])
+ adapter = fixture.runtime._ensure_adapter(
+ session, work_dir=fixture.project, objective="Produce a usable export"
+ )
+ assert starts[-1]["resume_thread_id"] == "original-thread"
+ assert adapter.upstream_thread_id == "original-thread"
+ assert [tool["name"] for tool in starts[-1]["dynamic_tools"]] == [
+ "loopx_context_read"
+ ]
+ else:
+ session, reused = fixture.runtime.open_session(
+ goal_id=fixture.session["goal_id"],
+ agent_id="codex",
+ managed_agent_id="agent-a",
+ work_dir=fixture.project,
+ objective="Produce a usable export",
+ mode="new",
+ )
+ assert not reused
+ adapter = fixture.runtime.adapters[session["session_id"]]
+ assert adapter.session._managed_goal_read_default is True
+ assert adapter.session._native_task_policy_used is False
+ wire = _capture_native_dispatch(
+ SimpleNamespace(native=adapter.session), monkeypatch
+ )
+ adapter.session.send("Read the original source.")
+ assert wire[-1]["params"]["sandboxPolicy"] == {
+ "type": "readOnly",
+ "networkAccess": False,
+ }
+ assert wire[-1]["params"]["threadId"] == adapter.upstream_thread_id
+ assert "managed_read_default" not in fixture.store.load_session(
+ session["session_id"]
+ )
+ assert "_managed_goal_read_default" not in fixture.store.load_session(
+ session["session_id"]
+ )
+ assert (
+ adapter.session.execution_mode is False
+ and adapter.session.sandbox == "read-only"
+ )
+
+
+def test_unbound_legacy_session_direct_send_preserves_default_native_payload(
+ original_check, monkeypatch
+):
+ fixture = original_check
+ assert fixture.native._managed_goal_read_default is False
+ assert fixture.native._native_task_policy_used is False
+ wire = _capture_native_dispatch(fixture, monkeypatch)
+ fixture.native.send("Read the source in this legacy transport.")
+ assert "sandboxPolicy" not in wire[-1]["params"]
+ assert (
+ fixture.native.execution_mode is False and fixture.native.sandbox == "read-only"
+ )
diff --git a/tests/control_plane/test_host_current_admission.py b/tests/control_plane/test_host_current_admission.py
index 3fb995e6d1..f3819ec0e2 100644
--- a/tests/control_plane/test_host_current_admission.py
+++ b/tests/control_plane/test_host_current_admission.py
@@ -3,6 +3,7 @@
import json
from pathlib import Path
+import sys
import pytest
@@ -94,3 +95,128 @@ def lose_state_before_start(**kwargs):
assert not state.exists()
index = runtime / "goals/loopx-turn-fixture/runs/index.jsonl"
assert not index.exists() or "quota_slot_spent" not in index.read_text()
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("change", ["task_text", "write_scope", "progress_note"])
+@pytest.mark.parametrize("host", ["exec", "operation"])
+def test_native_launch_rechecks_same_todo_after_planning(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, provider: str, change: str, host: str,
+) -> None:
+ runtime, registry, native, model, arguments = _fixture(tmp_path, monkeypatch, provider)
+ project = registry.parent.parent
+ if host == "exec":
+ executable, model = _fake_codex(tmp_path)
+ executable.write_text(executable.read_text().replace(
+ "args = sys.argv[1:]", f"pathlib.Path({str(native)!r}).write_text('native')\nargs = sys.argv[1:]", 1,
+ ))
+ monkeypatch.setenv("FAKE_CODEX_LOG", str(model))
+ arguments.remove("--codex-operation-tools")
+ arguments[arguments.index("--codex-bin") + 1] = str(executable)
+ old = _quota(registry, runtime, project)
+ assert old["should_run"] is True, old
+ todo_id = old["selected_todo"]["todo_id"]
+ mutation = {
+ "task_text": ["--text", "[P0] Verify the replacement fixture before any work."],
+ "write_scope": ["--required-write-scope", "docs/evidence/**"],
+ "progress_note": ["--note", "Independent observation received."],
+ }[change]
+ host_module = codex_cli if host == "exec" else codex_operation_host
+ original_input = host_module.prepare_codex_cli_input
+ readbacks = []
+
+ def edit_after_planning(request):
+ code, result = _cli([
+ "--registry", str(registry), "--runtime-root", str(runtime), "--format", "json",
+ "todo", "update", "--goal-id", "loopx-turn-fixture", "--agent-id", "codex-fixture",
+ "--todo-id", todo_id, *mutation,
+ ])
+ assert code == 0 and result["ok"], result
+ current = _quota(registry, runtime, project)
+ assert current["should_run"] is True and current["effective_action"] == old["effective_action"], current
+ assert current["selected_todo"]["todo_id"] == todo_id
+ readbacks.append((result, _bindings(runtime)))
+ return original_input(request)
+
+ monkeypatch.setattr(host_module, "prepare_codex_cli_input", edit_after_planning)
+ code, payload = _cli([*arguments, "--execute"])
+ assert len(readbacks) == 1
+ if change == "progress_note":
+ assert native.exists() and model.exists(), payload
+ else:
+ # Operation mode already started app-server; it must not start a
+ # model Turn or publish its Session binding on the stale request.
+ assert native.exists() is (host == "operation") and not model.exists(), payload
+ assert code == 1 and "current_turn_work_changed" in json.dumps(payload), payload
+ assert _bindings(runtime) == readbacks[0][1]
+ index = runtime / "goals/loopx-turn-fixture/runs/index.jsonl"
+ assert not index.exists() or "quota_slot_spent" not in index.read_text()
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("edit_task", [False, True])
+def test_stdio_retry_checks_original_task_before_launch(tmp_path, monkeypatch, provider, edit_task):
+ from loopx.control_plane.coordination.runtime_shadow import build_todo_runtime_shadow_projection
+ from loopx.control_plane.todos.active_state_todo_parser import parse_active_state_todos
+ from tests.control_plane.canonical_authority_fixture import initialize_canonical_authority, isolate_sqlite_runtime
+ from tests.control_plane.test_turn_host_decision_capture import _fixture_module
+
+ isolate_sqlite_runtime(tmp_path, monkeypatch)
+ fixture = _fixture_module()
+ project, runtime, workspace, registry = fixture._write_fixture(tmp_path, turn_count=2)
+ state = project / ".codex/goals" / fixture.GOAL_ID / "ACTIVE_GOAL_STATE.md"
+ todos = parse_active_state_todos(state.read_text(), state_path=state, item_limit=None)
+ initialize_canonical_authority(runtime, fixture.GOAL_ID,
+ build_todo_runtime_shadow_projection(goal_id=fixture.GOAL_ID, handoff_mode="soft_claim",
+ todos=todos["agent_todos"]["items"]), state_path=state, provider=provider)
+ marker = workspace / "process-count"
+ runner = tmp_path / "runner.py"
+ runner.write_text(f'''import json
+from pathlib import Path
+
+def run_dsh_turn(**options):
+ workspace = Path(options["workspace"])
+ marker = workspace / "process-count"
+ first = not marker.exists()
+ marker.write_text("1" if first else "2")
+ if first:
+ raise TimeoutError("Fixture failed before producing a result")
+ (workspace / {fixture.MARKER_NAME!r}).write_text({fixture._marker_value(1)!r})
+ return json.dumps({{"result_kind": "validated_progress", "classification": "checked_work",
+ "summary": "The original work passed independent validation.", "next_action": "Continue.",
+ "work_evidence": ["artifact:marker"], "validation_evidence": ["fixture:validate-marker"]}})
+''', encoding="utf-8")
+ prefix = ["--registry", str(registry), "--runtime-root", str(runtime), "--format", "json"]
+ subject = ["--goal-id", fixture.GOAL_ID, "--agent-id", fixture.AGENT_ID]
+ adapter = [sys.executable, "-m", "loopx.dsh_goal_mode", "--workspace", str(workspace),
+ "--dsh-runner", str(runner), "--dsh-home", str(tmp_path / "dsh-home")]
+ validator = [sys.executable, "-c", "from pathlib import Path; import json,sys; json.load(sys.stdin); "
+ f"assert Path({fixture.MARKER_NAME!r}).read_text() == {fixture._marker_value(1)!r}"]
+ argv = [*prefix, "turn", "run-once", *subject, "--host", "generic-cli", "--project", str(workspace),
+ "--execution-mode", "isolated-headless", "--no-global-sync", "--timeout-seconds", "20",
+ "--host-adapter-request-schema", "loopx_turn_host_request_v1",
+ "--host-adapter-command-json", json.dumps(adapter),
+ "--validation-command-json", json.dumps(validator), "--execute"]
+ code, first = fixture._run_cli([*argv, "--turn-instance-id", "bound-stdio-retry"])
+ assert code == 1 and first["status"] == "failed", first
+ assert marker.read_text() == "1" and fixture._quota_spend_count(runtime) == 0
+ if edit_task:
+ binding = [*subject, "--todo-id", fixture.TODO_ID]
+ code, before = fixture._run_cli([*prefix, "todo", "list", *binding])
+ assert code == 0
+ code, changed = fixture._run_cli([*prefix, "todo", "update", *binding, "--execute",
+ "--text", before["todo"]["text"] + " Follow the replacement requirement.",
+ "--update-operation-id", "change-captured-work",
+ "--update-expected-provider-revision", before["authority_read"]["provider_revision"]])
+ assert code == 0 and changed["changed"], changed
+ recovery = [*argv, "--resume-turn-key", first["resume_turn_key"]]
+ code, result = fixture._run_cli([*recovery, "--retry-failed-turn"])
+ if edit_task:
+ assert code == 1 and result["error_code"] == "current_turn_work_changed", result
+ assert not any(result["effects"].values())
+ else:
+ assert code == 0 and result["status"] == "committed", result
+ _, replay = fixture._run_cli(recovery)
+ assert replay["replayed"] and not any(replay["effects"].values())
+ assert marker.read_text() == ("1" if edit_task else "2")
+ assert fixture._quota_spend_count(runtime) == (0 if edit_task else 1)
diff --git a/tests/control_plane/test_new_goal_storage_defaults.py b/tests/control_plane/test_new_goal_storage_defaults.py
index 4492d76e97..b294eec86a 100644
--- a/tests/control_plane/test_new_goal_storage_defaults.py
+++ b/tests/control_plane/test_new_goal_storage_defaults.py
@@ -589,7 +589,7 @@ def interrupt_after_session(*args, **kwargs):
elif refusal == "foreign_session":
# The public store forbids rebinding a Goal Session. Model an
# independently corrupted file in this disposable fixture only.
- with pytest.raises(ValueError, match="only the owner manager"):
+ with pytest.raises(ValueError, match="a registered Agent Session cannot change its original Goal"):
chat.update_session(session_id, goal_id="another-goal")
chat._session_path(session_id).write_text(json.dumps({**session, "goal_id": "another-goal"}))
controller = controller_for(runtime / "missing-codex" if refusal == "missing_host" else fake)
@@ -625,7 +625,8 @@ def interrupt_after_session(*args, **kwargs):
controller.open_session(goal_id="pre-first-turn", agent_id="codex",
goal_instance_id=session.get("goal_instance_id"), work_dir=project,
objective=preview["proposal"]["normalized_parameters"]["objective"],
- mode="resume_latest", channel_id="goal.pre-first-turn")
+ mode="resume_latest", channel_id=session["channel_id"],
+ managed_agent_id=session["managed_agent_id"])
assert chat.turn_for_client(session_id, client_turn_id) == pending
assert len(chat.list_sessions(goal_id="pre-first-turn")) == 1
elif refusal == "terminal_before_start":
diff --git a/tests/control_plane/test_original_task_entry.py b/tests/control_plane/test_original_task_entry.py
new file mode 100644
index 0000000000..72eabf8b73
--- /dev/null
+++ b/tests/control_plane/test_original_task_entry.py
@@ -0,0 +1,235 @@
+"""Original Chat request reaches the existing repair checker and Task owner.
+
+The production HTTP/Chat/Core/File/SQLite owners execute; model interpretation
+and editing are scripted. The six preexisting repair checks remain independent.
+"""
+import hashlib
+import http.client
+import json
+import runpy
+import shutil
+import subprocess
+import sys
+import threading
+from pathlib import Path
+
+import pytest
+
+from test_new_goal_storage_defaults import environment as environment
+from loopx.chat_action_store import ChatActionStore
+from loopx.chat_actions import ChatActionService
+from loopx.chat_runtime import ChatRuntimeController
+from loopx.chat_server import ChatHTTPServer, ChatRequestHandler
+from loopx.chat_store import ChatSessionStore
+from loopx.capabilities.native_chat.project_context import ChatProjectContexts
+from loopx.control_plane.coordination.local_authority import read_canonical_todos_if_promoted
+from loopx.control_plane.goals.acceptance import inspect_goal_acceptance
+
+
+@pytest.fixture
+def cart_host(environment, monkeypatch):
+ _, bootstrap, _, project, runtime = environment
+ bootstrap("workspace")
+ exercise = Path(__file__).parents[2] / "examples/bugfix-loop"
+ for name in ("checks.py", "cart.py", "README.md"):
+ shutil.copy2(exercise / name, project / name)
+ capture, binary = runtime / "requests.jsonl", runtime / "synthetic-codex"
+ from loopx.control_plane.turn_driver import host_candidate
+ extract = host_candidate.extract_turn_authority
+ def capture_authority(*args, **kwargs):
+ authority = extract(*args, **kwargs)
+ (runtime / "native-authority.json").write_text(json.dumps(authority))
+ return authority
+ monkeypatch.setattr(host_candidate, "extract_turn_authority", capture_authority)
+ source = runpy.run_path(str(Path(__file__).parents[2] / "examples/loopx-chat-runtime-smoke.py"))["FAKE_CODEX"]
+ source = source.replace(' method = request.get("method")',
+ f' with open({str(capture)!r}, "a") as stream:\n stream.write(json.dumps(request) + "\\n")\n'
+ ' method = request.get("method")')
+ # The model proposes a same-workspace existing checker, with no absolute interpreter,
+ # source hash, Task id or Goal/Agent routing supplied as execution authority.
+ document = {"outcome_id": "goal-creation", "criteria": [{"id": "completion",
+ "validation_argv": ["python3", "checks.py"], "validation_timeout_seconds": 5,
+ "oracle": {"kind": "owner_pinned_oracle", "source_id": "bugfix-cart",
+ "input_refs": [], "output_refs": ["cart.py"]}}]}
+ source = source.replace(' elif method == "turn/start":',
+ ' elif request_id == 900 and method is None:\n'
+ ' evidence = json.loads(request["result"]["contentItems"][0]["text"])\n'
+ ' response = "" + json.dumps({"schema_version": "loopx_chat_agent_response_v0",\n'
+ ' "message": "The existing six-check repair plan is ready for owner review.", "proposals": [], "gate": None}) + " "\n'
+ ' print(json.dumps({"method": "item/agentMessage/delta", "params": {"threadId": "durable-thread", "turnId": active_turn, "delta": response}}), flush=True)\n'
+ ' print(json.dumps({"method": "turn/completed", "params": {"threadId": "durable-thread", "turn": {"id": active_turn}}}), flush=True)\n'
+ ' turn_status[active_turn] = "completed"\n'
+ ' continue\n'
+ ' elif method == "turn/start":')
+ source = source.replace(" response = 'Visible runtime response.",
+ f' if not __import__("pathlib").Path({str(project / "execute-native")!r}).exists():\n'
+ ' print(json.dumps({"id": 900, "method": "item/tool/call", "params": {\n'
+ ' "threadId": "durable-thread", "turnId": active_turn, "tool": "loopx_collaboration",\n'
+ ' "arguments": ' + repr({"action": "prepare_check", "operation_id": "original-cart-check", "document": document}) + '}}), flush=True)\n'
+ ' continue\n'
+ f' mode = __import__("pathlib").Path({str(project / "execute-native")!r}).read_text()\n'
+ ' if mode == "valid":\n'
+ f' assert request["params"].get("sandboxPolicy") == {{"type": "workspaceWrite", "writableRoots": [{str(project.resolve())!r}], "networkAccess": False, "excludeTmpdirEnvVar": True, "excludeSlashTmp": True}}\n'
+ f' assert request["params"].get("runtimeWorkspaceRoots") == [{str(project.resolve())!r}]\n'
+ f' path = __import__("pathlib").Path({str(project / "cart.py")!r})\n'
+ ' cart = path.read_text().replace(" if subtotal > 10_000:", " if not prices or subtotal >= 10_000:")\n'
+ ' path.write_text(cart)\n'
+ " response = 'Visible runtime response.")
+ candidate = {"result_kind": "validated_completion", "classification": "implementation",
+ "summary": "Repaired the empty-cart and free-shipping boundaries.",
+ "next_action": "Read back the original cart artifact and independent checks.",
+ "delivery_batch_scale": "implementation", "delivery_outcome": "outcome_progress",
+ "vision_unchanged_reason": "The original six-check cart repair remains the current task.",
+ "work_evidence": ["cart.py"], "validation_evidence": ["Unchanged checks.py verifies six original behaviors"]}
+ source = source.replace('"message": "Runtime response."', '"message": ' + repr(json.dumps(candidate)))
+ binary.write_text(source)
+ compile(source, str(binary), "exec")
+ binary.chmod(0o700)
+ store = ChatSessionStore(runtime / "chat")
+ registry = project / ".loopx/registry.json"
+ controller = ChatRuntimeController(store=store, codex_bin=str(binary), registry_path=registry,
+ project_contexts=ChatProjectContexts([project]))
+ actions = ChatActionStore(runtime / "chat/actions")
+ service = ChatActionService(store=actions, registry_path=registry,
+ runtime_controller=controller, chat_store=store)
+ server = ChatHTTPServer(("127.0.0.1", 0), ChatRequestHandler)
+ server.verbose = False
+ server.registry_path, server.runtime_root = registry, runtime
+ server.runtime_controller, server.action_store, server.action_service = controller, actions, service
+ thread = threading.Thread(target=server.serve_forever, daemon=True)
+ thread.start()
+
+ def request(path, body=None):
+ connection = http.client.HTTPConnection(*server.server_address, timeout=30)
+ try:
+ connection.request("GET" if body is None else "POST", path,
+ None if body is None else json.dumps(body), {"Content-Type": "application/json"})
+ response = connection.getresponse()
+ return response.status, json.loads(response.read())
+ finally:
+ connection.close()
+
+ try:
+ yield actions, request, controller, capture, project, runtime
+ finally:
+ server.shutdown()
+ thread.join(timeout=5)
+ server.server_close()
+ controller.close()
+
+
+def create_cart(cart_host):
+ _, request, controller, _, project, runtime = cart_host
+ status, preview = request("/api/actions/preview", {"action_kind": "goal.create",
+ "summary": "Repair the cart using its existing checks", "idempotency_key": "cart-repair-request",
+ "context": {"kind": "goal_channel", "goal_id": "workspace"}, "normalized_parameters": {
+ "goal_id": "cart-repair", "title": "Cart repair",
+ "objective": "Repair cart.py in this exercise workspace according to README.md and unchanged checks.py.",
+ "completion_criteria": "The six existing checks pass without changing checks.py.",
+ "agent_id": "codex", "workspace_ref": "current", "heartbeat": {"enabled": False},
+ "initial_todos": ["Repair cart.py to pass the six existing behavioral checks."]}})
+ assert status == 201, preview
+ proposal = preview["proposal"]
+ status, applied = request(f"/api/actions/{proposal['proposal_id']}/apply", {})
+ assert status == 202, applied
+ ids = applied["proposal"]["receipt"]["resource_ids"]
+ first = controller.wait_for_turn(session_id=ids["session_id"], turn_id=ids["turn_id"], timeout_sec=15)
+ assert first["status"] == "completed", first
+ messages = controller.store.messages(ids["session_id"])
+ assert messages[0]["text"] == proposal["normalized_parameters"]["objective"]
+ assert first["message"].startswith("开始推进 Goal"), "execution identity retains its original internal request"
+ basis = inspect_goal_acceptance(registry_path=controller.registry_path, runtime_root=str(runtime),
+ goal_id="cart-repair", agent_id="codex")
+ assert not basis.get("completion_requirements")
+ return proposal, ids, first
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("mode", ["valid", "unconfirmed", "changed-source", "failed", "read-only"])
+def test_original_http_task_reuses_requested_checker_and_session(environment, cart_host, provider, mode):
+ configure, *_ = environment
+ configure(provider)
+ actions, request, controller, capture, project, runtime = cart_host
+ _, ids, first = create_cart(cart_host)
+ sid, todo = ids["session_id"], ids["todo_ids"][0]
+ session, history = controller.store.load_session(sid), controller.store.messages(sid)
+ checker_hash = hashlib.sha256((project / "checks.py").read_bytes()).hexdigest()
+ proposals = actions.list(goal_id="cart-repair", context_kind="goal")
+ checks = [row for row in proposals if row["action_kind"] == "goal.acceptance.check"]
+ assert len(checks) == 1, {"first": first, "proposals": proposals}
+ check = checks[0]
+ criterion = check["normalized_parameters"]["document"]["criteria"][0]
+ assert criterion["validation_argv"] == ["python3", "checks.py"]
+ assert Path(shutil.which("python3")).resolve() == Path(sys.executable).resolve()
+ assert criterion["validation_files"] == [{"path": "checks.py", "sha256": checker_hash}]
+ assert check["status"] == "preview_ready" and not first.get("loopx_execution")
+ calls_before = capture.read_bytes()
+ status, unavailable = request(f"/api/chat/sessions/{sid}/native-turn?original_task=true&agent_id=codex")
+ assert status == 200 and unavailable["task"]["state"] == "unbound", unavailable
+ if mode != "unconfirmed":
+ status, adopted = request(f"/api/actions/{check['proposal_id']}/apply", {})
+ assert status == 200 and adopted["proposal"]["receipt"]["checks_passed"] is False, adopted
+ assert capture.read_bytes() == calls_before, "check confirmation cannot launch a model/Task"
+ status, observed = request(f"/api/chat/sessions/{sid}/native-turn?original_task=true&agent_id=codex")
+ assert status == 200 and observed["task"]["state"] == "ready", observed
+ assert observed["task"]["todo_id"] == todo and not observed["task"]["canonical_done"]
+ assert "effects" not in observed["task"]
+ if mode == "changed-source":
+ (project / "checks.py").write_text("raise SystemExit(0)\n")
+ if mode in {"valid", "failed"}:
+ # An explicit, existing owner approval is separate from check adoption
+ # and native Task selection. Default new Goals do not gain write rights.
+ approved = subprocess.run([sys.executable, "-m", "loopx.entrypoint",
+ "--registry", str(controller.registry_path), "--runtime-root", str(runtime), "--format", "json",
+ "configure-goal", "--goal-id", "cart-repair", "--write-scope", "**",
+ "--boundary-authority-scope", "**", "--boundary-authority-source", "owner-approved-exercise",
+ "--boundary-authority-decision-id", "cart-workspace", "--execute"],
+ capture_output=True, text=True, timeout=20)
+ assert approved.returncode == 0, approved.stdout + approved.stderr
+ (project / "execute-native").write_text(mode)
+ status, accepted = request(f"/api/chat/sessions/{sid}/native-turn", {
+ "agent_id": "codex", "operation_id": "original-cart-execution", "todo_id": todo})
+ assert status == 202, accepted
+ turn = controller.wait_for_turn(session_id=sid, turn_id=accepted["turn"]["turn_id"], timeout_sec=20)
+ result = turn["response"]["native_loopx"]
+ assert result["status"] == {"valid": "committed", "unconfirmed": "stopped",
+ "changed-source": "stopped", "failed": "failed", "read-only": "failed"}[mode], result
+ assert result["effects"]["quota_spent"] is (mode == "valid")
+ assert result["effects"]["state_written"] is (mode == "valid")
+ authority = read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="cart-repair", include_leases=True)
+ assert next(row for row in authority["todos"] if row["todo_id"] == todo)["status"] == ("done" if mode == "valid" else "open")
+ calls = [json.loads(line) for line in capture.read_bytes()[len(calls_before):].splitlines()]
+ native_calls = [row for row in calls if row.get("method") == "turn/start"]
+ assert len(native_calls) == (1 if mode in {"valid", "failed", "read-only"} else 0)
+ if native_calls:
+ assert native_calls[0]["params"]["sandboxPolicy"]["type"] == ("readOnly" if mode == "read-only" else "workspaceWrite")
+ if mode == "read-only":
+ assert "runtimeWorkspaceRoots" not in native_calls[0]["params"]
+ else:
+ assert native_calls[0]["params"]["runtimeWorkspaceRoots"] == [str(project.resolve())]
+ assert controller.adapters[sid].session.sandbox == "read-only"
+ assert controller.adapters[sid].session.execution_mode is False
+ assert not any(row.get("method") == "thread/start" for row in calls)
+ assert controller.store.load_session(sid)["upstream_thread_id"] == session["upstream_thread_id"]
+ assert controller.store.messages(sid)[:len(history)] == history
+ after = capture.read_bytes()
+ status, replay = request(f"/api/chat/sessions/{sid}/native-turn", {
+ "agent_id": "codex", "operation_id": "original-cart-execution", "todo_id": todo})
+ assert status == 202 and not replay["created"] and replay["turn"]["turn_id"] == turn["turn_id"]
+ assert capture.read_bytes() == after
+ if mode == "valid":
+ (project / "execute-native").write_text("ordinary")
+ followup, created = controller.submit_turn(session_id=sid, client_turn_id="ordinary-after-task",
+ message="Explain the repaired behavior without changing the files.", work_dir=project,
+ objective="Retain the original cart repair.")
+ assert created
+ answered = controller.wait_for_turn(session_id=sid, turn_id=followup["turn_id"], timeout_sec=10)
+ assert answered["status"] == "completed" and "native_loopx" not in answered["response"]
+ later = [json.loads(line) for line in capture.read_bytes()[len(after):].splitlines()]
+ next_calls = [row for row in later if row.get("method") == "turn/start"]
+ assert len(next_calls) == 1 and next_calls[0]["params"]["sandboxPolicy"] == {
+ "type": "readOnly", "networkAccess": False}
+ assert not any(row.get("method") == "thread/start" for row in later)
+ check_run = subprocess.run([sys.executable, "checks.py"], cwd=project, capture_output=True, text=True, timeout=10)
+ assert check_run.returncode == 0 and "Ran 6 tests" in check_run.stderr
+ assert hashlib.sha256((project / "checks.py").read_bytes()).hexdigest() == checker_hash
diff --git a/tests/control_plane/test_quota_settlement_cli.py b/tests/control_plane/test_quota_settlement_cli.py
index e747385d8c..e5a420ed67 100644
--- a/tests/control_plane/test_quota_settlement_cli.py
+++ b/tests/control_plane/test_quota_settlement_cli.py
@@ -7136,8 +7136,9 @@ def test_unbound_visible_goal_todoless_replan_reenters_through_guided_turn(
assert {path: path.read_bytes() for path in source_paths} == original_files
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
def test_autonomous_replan_semantic_delta_keeps_accountable_receipt_chain(
- tmp_path: Path,
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, provider: str,
) -> None:
"""Regression for #3528's real Todo-bound replan path.
@@ -7147,12 +7148,15 @@ def test_autonomous_replan_semantic_delta_keeps_accountable_receipt_chain(
instead of being mislabeled as a noop after it discharges the obligation.
"""
+ from canonical_authority_fixture import isolate_sqlite_runtime
+
+ isolate_sqlite_runtime(tmp_path, monkeypatch)
project, runtime, registry_path = _write_fixture(tmp_path)
- _configure_selected_todo_replan_fixture(project, registry_path)
+ _configure_selected_todo_replan_fixture(project, registry_path, stride_strategy="off")
_initialize_git_checkout(project)
turn_instance_id = "turn-autonomous-replan-semantic-1"
- _initialize_fixture_authority(project, runtime)
+ _initialize_fixture_authority(project, runtime, provider=provider)
guard_rc, guard = _run_cli(
registry_path,
runtime,
@@ -7235,6 +7239,9 @@ def test_autonomous_replan_semantic_delta_keeps_accountable_receipt_chain(
assert [
receipt["step_kind"] for receipt in refresh["settlement_result"]["receipts"]
] == ["validation", "durable_writeback"]
+ original = guard["heartbeat_receipt"]["settlement_identity"]
+ assert refresh["settlement_identity"] == original
+ assert _spend_run_count(runtime) == 0
replay_rc, replay = _run_cli(
registry_path,
@@ -7246,23 +7253,98 @@ def test_autonomous_replan_semantic_delta_keeps_accountable_receipt_chain(
assert replay["appended"] is False
assert _classification_count(runtime, "validated_progress") == 1
- next_guard_rc, next_guard = _run_cli(
- registry_path,
- runtime,
- "quota",
- "should-run",
- "--codex-app",
- "--goal-id",
- GOAL_ID,
- "--agent-id",
- AGENT_ID,
- "--turn-instance-id",
- "turn-after-autonomous-replan-semantic-1",
- )
+ # A new guard can see the replan has ended before the original debit is
+ # durable. It must still expose recovery instead of another execution.
+ next_turn = "turn-after-autonomous-replan-semantic-1"
+ next_args = ("quota", "should-run", "--codex-app", "--goal-id", GOAL_ID,
+ "--agent-id", AGENT_ID, "--turn-instance-id", next_turn)
+ next_guard_rc, next_guard = _run_cli(registry_path, runtime, *next_args)
assert next_guard_rc == 0, next_guard
- assert next_guard.get("autonomous_replan_obligation") is None
- assert next_guard.get("decision") != "autonomous_replan_required"
- assert obligation_id not in json.dumps(next_guard, sort_keys=True)
+ assert next_guard["effective_action"] == "unsettled_host_turn_recovery", next_guard
+ assert next_guard["normal_delivery_allowed"] is False
+ recovery = next_guard["unsettled_host_turn_recovery"]
+ assert recovery["prior_turn_instance_id"] == turn_instance_id
+ assert recovery["binding_id"] == SELECTED_REPLAN_TODO_ID
+ assert recovery["settlement_effect_id"] == original["effect_id"]
+ assert recovery["missing_receipts"] == ["quota_spend_receipt"]
+ assert all(not row["details"].get("settlement_effect_id")
+ for row in _heartbeat_receipt_events(runtime, next_turn))
+
+ explicit_rc, explicit = _run_cli(registry_path, runtime, *next_args, "--todo-id", SELECTED_REPLAN_TODO_ID)
+ assert explicit_rc == 0 and explicit["effective_action"] == "unsettled_host_turn_recovery", explicit
+ assert explicit["unsettled_host_turn_recovery"] == recovery
+ assert "settlement_identity" not in explicit["heartbeat_receipt"]
+ actions = explicit["interaction_contract"]["cli_channel"]["next_cli_actions"]
+ original_guard = next(command for command in actions if turn_instance_id in shlex.split(command))
+ rc, recovering = _run_generated_cli(original_guard, registry_path=registry_path, cwd=project)
+ assert rc == 0 and recovering["normal_delivery_allowed"] is False, recovering
+ owed = recovering["settlement_owed"]
+ assert owed["command"] == refresh["settlement_owed"]["command"]
+ assert owed["effect_id"] == original["effect_id"]
+
+ def reject_changed_spend() -> None:
+ for flag, replacement in (("--slots", "2"), ("--source", "visible-goal")):
+ tokens = shlex.split(owed["command"])
+ tokens[tokens.index(flag) + 1] = replacement
+ refused_rc, refused = _run_generated_cli(
+ shlex.join(tokens), registry_path=registry_path, cwd=project,
+ )
+ assert refused_rc == 1 and refused["ok"] is False, refused
+ assert refused["appended"] is False
+ assert refused["settlement_result"]["failure"]["kind"] == "quota_spend_rejected"
+
+ reject_changed_spend()
+ assert _spend_run_count(runtime) == 0
+ from concurrent.futures import ThreadPoolExecutor
+
+ def settle() -> dict[str, Any]:
+ rc, result = _run_generated_cli(owed["command"], registry_path=registry_path, cwd=project)
+ if rc != 0:
+ pytest.fail(f"Original settlement failed: {result.get('error_code')}: {result.get('reason') or result.get('error')}")
+ assert result["settlement_identity"] == original
+ return result
+
+ with ThreadPoolExecutor(max_workers=2) as pool:
+ results = [pool.submit(settle), pool.submit(settle)]
+ attempts = [result.result() for result in results]
+ assert sorted(attempt["appended"] for attempt in attempts) == [False, True]
+ assert all(
+ attempt["settlement_result"]["ok"]
+ and [receipt["step_kind"] for receipt in attempt["settlement_result"]["receipts"]]
+ == ["validation", "durable_writeback", "quota_spend"]
+ for attempt in attempts
+ )
+ rollout_log = runtime / "goals" / GOAL_ID / "rollout-event-log.jsonl"
+ events = [json.loads(line) for line in rollout_log.read_text(encoding="utf-8").splitlines()]
+ assert sum(
+ event["event_kind"] == "quota_spend" and event.get("run_id") == turn_instance_id
+ for event in events
+ ) == 1
+ assert _spend_run_count(runtime) == 1
+ reject_changed_spend()
+ # Simulate the debit committing before its rollout receipt. Refused
+ # requests must not fill the missing receipt with different billing terms.
+ partial_log = "".join(json.dumps(event) + "\n" for event in events if event["event_kind"] != "quota_spend")
+ rollout_log.write_text(partial_log, encoding="utf-8")
+ reject_changed_spend()
+ assert rollout_log.read_text(encoding="utf-8") == partial_log
+ repaired = settle()
+ assert repaired["appended"] is False and repaired["receipt_repaired"] is True
+ assert repaired["settlement_result"]["ok"] is True
+ repaired_events = [json.loads(line) for line in rollout_log.read_text(encoding="utf-8").splitlines()]
+ assert sum(
+ event["event_kind"] == "quota_spend"
+ and event["details"]["settlement_effect_id"] == original["effect_id"]
+ for event in repaired_events
+ ) == 1
+ assert _spend_run_count(runtime) == 1
+ assert _classification_count(runtime, "validated_progress") == 1
+ rc, admitted = _run_cli(registry_path, runtime, *next_args, "--todo-id", SELECTED_REPLAN_TODO_ID)
+ assert rc == 0 and admitted["normal_delivery_allowed"], admitted
+ assert admitted["heartbeat_receipt"]["settlement_identity"]["turn_instance_id"] == next_turn
+ assert admitted["heartbeat_receipt"]["settlement_identity"]["todo_id"] == SELECTED_REPLAN_TODO_ID
+ assert admitted["heartbeat_receipt"]["settlement_identity"]["effect_id"] != original["effect_id"]
+ assert obligation_id not in json.dumps(admitted, sort_keys=True)
def test_open_replan_rejects_missing_semantic_delta_before_durable_write(
diff --git a/tests/control_plane/test_remote_authority_gateway.py b/tests/control_plane/test_remote_authority_gateway.py
new file mode 100644
index 0000000000..a597f39431
--- /dev/null
+++ b/tests/control_plane/test_remote_authority_gateway.py
@@ -0,0 +1,61 @@
+"""The real local CLI does not admit remote work or alter default-off decisions."""
+
+from __future__ import annotations
+
+import json
+
+import pytest
+
+from examples.control_plane.quota_plan_fixtures import SCOPED_AGENT_ID, write_cli_fixture
+from loopx.control_plane.testing.canary_harness import run_json_cli
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_local_cli_ignores_disabled_and_other_goal_remote_binding(tmp_path, monkeypatch, provider):
+ registry, runtime, _ = write_cli_fixture(tmp_path, scoped_agents=True, provider=provider)
+ # Old environment-only canary inputs cannot activate an unconfigured target.
+ monkeypatch.setenv("LOOPX_REMOTE_AUTHORITY_CANARY_ENABLED", "1")
+ monkeypatch.setenv("LOOPX_AUTHORITY_CREDENTIAL", "unused-synthetic-credential-" + "a" * 32)
+ goal_id = "throttled-half"
+ scope = ("--goal-id", goal_id, "--agent-id", SCOPED_AGENT_ID)
+
+ def cli(*args):
+ return run_json_cli(*args, registry_path=registry, runtime_root=runtime)
+
+ def read():
+ prompt = cli("heartbeat-prompt", "--thin", "--codex-app", *scope)
+ assert "remote_authority" not in prompt
+ decision = cli("quota", "should-run", "--runtime-profile", "outer_controller", *scope)
+ assert "remote_gateway" not in decision
+ assert decision["ok"] is True
+ return tuple(decision.get(key) for key in ("decision", "effective_action", "should_run", "reason"))
+
+ def read_local(goal):
+ decision = cli(
+ "quota", "should-run", "--runtime-profile", "outer_controller",
+ "--goal-id", goal, "--agent-id", SCOPED_AGENT_ID,
+ )
+ assert "remote_gateway" not in decision
+ return tuple(decision.get(key) for key in ("decision", "effective_action", "should_run"))
+
+ local = read()
+ assert local[:3] == ("skip", "throttled_skip", False), local
+ assert read_local("half-speed") == ("run", "normal_run", True)
+ document = json.loads(registry.read_text())
+ cfg = {"enabled": True, "endpoint": "https://127.0.0.1:1", "tenant_id": "other-synthetic-tenant",
+ "actor_id": SCOPED_AGENT_ID, "store_identity": "postgresql:" + "a" * 32, "probe_todo_id": "todo_probe"}
+ for goal in document["goals"]:
+ if goal["id"] == goal_id:
+ goal["remote_authority"] = {**cfg, "enabled": False}
+ if goal["id"] == "full-speed":
+ goal["remote_authority"] = cfg
+ registry.write_text(json.dumps(document), encoding="utf-8")
+ assert read() == local
+ assert read_local("half-speed") == ("run", "normal_run", True)
+
+ other = cli("heartbeat-prompt", "--full", "--codex-app",
+ "--goal-id", "full-speed", "--agent-id", SCOPED_AGENT_ID)
+ assert other["remote_authority"]["status"] == "unverified"
+ for key in ("endpoint", "tenant_id", "actor_id", "store_identity", "probe_todo_id"):
+ assert key not in other["remote_authority"]
+ assert cfg["endpoint"] not in other["task_body"] and cfg["tenant_id"] not in other["task_body"]
diff --git a/tests/control_plane/test_scheduler_ack_current_host_binding.py b/tests/control_plane/test_scheduler_ack_current_host_binding.py
index 000d5f9ab3..bbed41e852 100644
--- a/tests/control_plane/test_scheduler_ack_current_host_binding.py
+++ b/tests/control_plane/test_scheduler_ack_current_host_binding.py
@@ -442,6 +442,134 @@ def test_scheduler_fail_current_rejects_missing_turn_receipt_without_state_write
assert not state_path.exists()
+def _scheduler_state_files(runtime_root: Path) -> list[Path]:
+ return [
+ path
+ for goal_dir in sorted((runtime_root / "goals").glob(f"{GOAL_ID}*"))
+ for path in sorted((goal_dir / "scheduler-state").rglob("*.json"))
+ ]
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("operation", ["scheduler-ack-current", "scheduler-fail-current"])
+def test_scheduler_followup_gates_state_migration(
+ tmp_path: Path, provider: str, operation: str,
+) -> None:
+ registry_path, runtime_root, project = write_cli_fixture(
+ tmp_path / "fixture", scoped_agents=True, provider=provider,
+ )
+
+ def call(command: str, turn: str, *flags: str):
+ return run_json_cli_result(
+ "quota", command, "--goal-id", GOAL_ID, "--agent-id", SCOPED_AGENT_ID,
+ "--codex-app", "--app-automation-current-rrule", "FREQ=MINUTELY;INTERVAL=5",
+ "--turn-instance-id", turn, *flags,
+ registry_path=registry_path, runtime_root=runtime_root, cwd=project,
+ )
+
+ stale, current = "heartbeat-older", "heartbeat-current"
+ for turn in (stale, current):
+ rc, guard = call("should-run", turn)
+ assert rc == 0 and guard["ok"] is True
+ assert guard["heartbeat_receipt"]["turn_instance_id"] == turn
+ app = guard["scheduler_hint"]["app_automation"]
+ key = APP_AUTOMATION_STATEFUL_BACKOFF_STATE_KEY
+ flags = ["--state-key", key]
+ if operation == "scheduler-fail-current":
+ flags.extend(["--failed-rrule", app["recommended_rrule"]])
+
+ def reject_stale(*execution: str) -> None:
+ rc, payload = call(operation, stale, *flags, *execution)
+ assert rc == 1 and payload["ok"] is False
+ assert payload["status"] == "heartbeat_receipt_stale"
+ assert payload["state"] == "blocked_receipt"
+ assert payload["error_code"] == "SCHEDULER_FOLLOWUP_HEARTBEAT_RECEIPT_STALE"
+ assert payload["write_performed"] is False
+ assert payload["scheduler_state_mutated"] is False
+ assert payload["quota_spend_performed"] is False
+
+ reject_stale("--execute")
+ assert _scheduler_state_files(runtime_root) == []
+
+ canonical = scheduler_state_path(
+ runtime_root, goal_id=GOAL_ID, agent_id=SCOPED_AGENT_ID, state_key=key,
+ )
+ legacy = (
+ runtime_root / "goals" / GOAL_ID / "scheduler-state"
+ / SCOPED_AGENT_ID / "codex_app" / canonical.name
+ )
+ assert legacy != canonical
+ legacy.parent.mkdir(parents=True, exist_ok=True)
+ legacy.write_text(json.dumps(build_scheduler_state(
+ goal_id=GOAL_ID, agent_id=SCOPED_AGENT_ID, state_key=key,
+ reset_token="earlier-upgrade", identity_signature="earlier-host",
+ progression_index=0, progression_minutes=[3, 10, 30],
+ last_applied_rrule="FREQ=MINUTELY;INTERVAL=5",
+ updated_at="2026-01-01T00:00:00Z",
+ )), encoding="utf-8")
+ original = legacy.read_bytes()
+
+ for execution in ([], ["--execute"]):
+ reject_stale(*execution)
+ assert legacy.read_bytes() == original
+ assert not canonical.exists()
+ rc, preview = call(operation, current, *flags)
+ assert rc == 0 and preview["ok"] is True
+ assert legacy.read_bytes() == original and not canonical.exists()
+
+ rc, committed = call(operation, current, *flags, "--execute")
+ assert rc == 0 and committed["ok"] is True
+ assert committed["scheduler_state_mutated"] is True
+ assert canonical.exists() and not legacy.exists()
+
+
+def test_scheduler_ack_current_routes_the_newest_turn_receipt(
+ tmp_path: Path,
+) -> None:
+ registry_path, runtime_root, project = write_cli_fixture(
+ tmp_path / "fixture",
+ scoped_agents=True,
+ )
+
+ seeded_rc, seeded = run_json_cli_result(
+ "quota",
+ "should-run",
+ "--goal-id",
+ GOAL_ID,
+ "--agent-id",
+ SCOPED_AGENT_ID,
+ "--codex-app",
+ "--turn-instance-id",
+ "heartbeat-newest",
+ registry_path=registry_path,
+ runtime_root=runtime_root,
+ cwd=project,
+ )
+ assert seeded_rc == 0
+ assert seeded["ok"] is True
+
+ returncode, payload = run_json_cli_result(
+ "quota",
+ "scheduler-ack-current",
+ "--goal-id",
+ GOAL_ID,
+ "--agent-id",
+ SCOPED_AGENT_ID,
+ "--codex-app",
+ "--turn-instance-id",
+ "heartbeat-newest",
+ "--execute",
+ registry_path=registry_path,
+ runtime_root=runtime_root,
+ cwd=project,
+ )
+
+ assert returncode == 0
+ assert payload["ok"] is True
+ assert payload["scheduler_state_mutated"] is True
+ assert Path(payload["scheduler_state_path"]).exists()
+
+
def test_trae_app_scheduler_failure_does_not_read_codex_automation_store(
tmp_path: Path,
monkeypatch,
diff --git a/tests/control_plane/test_turn_host_decision_capture.py b/tests/control_plane/test_turn_host_decision_capture.py
index e965463074..648f63cdcd 100644
--- a/tests/control_plane/test_turn_host_decision_capture.py
+++ b/tests/control_plane/test_turn_host_decision_capture.py
@@ -221,6 +221,340 @@ def execute():
assert state["eligibility"]["state"] == "waiting"
+@pytest.mark.parametrize("mutation,expected", [
+ ("inspection_key_route", "LoopX Turn CLI inspection requires the explicit run-once execution surface"),
+ ("key_route", "LoopX Turn plan has no valid transaction turn_key"),
+ ("route_false", "staged route refused"),
+ ("route_integer", "staged route refused"),
+ ("route_string", "staged route refused"),
+ ("route_none", "staged route refused"),
+ ("route_mapping", "staged route refused"),
+ ("route_fallback", "LoopX Turn route is not host executable"),
+])
+def test_staged_capture_rechecks_aliased_plan_guards_before_effects(
+ complete_host_request, tmp_path, monkeypatch, mutation, expected,
+):
+ from loopx.control_plane.turn_driver import executor
+ from loopx.control_plane.turn_driver.driver import build_loopx_turn_plan
+
+ request, original = complete_host_request
+ plan = build_loopx_turn_plan(request["turn_envelope"], work_contract=original,
+ host="generic-cli", execution_mode="isolated-headless",
+ turn_instance_id="staged-alias-guards")
+ runtime = tmp_path / "staged-alias-runtime"
+ captures = []
+ effects = []
+ original_stage = executor.stage_turn_decision_capture
+
+ def stage_then_mutate(*args, **kwargs):
+ capture = original_stage(*args, **kwargs)
+ captures.append(capture)
+ # A frozen capture still shares nested mutable plan fields. Mutate
+ # only after the real source binding, so the incoming checks pass.
+ if mutation == "inspection_key_route":
+ capture.plan["boundary"]["inspection_only"] = True
+ if mutation in {"inspection_key_route", "key_route"}:
+ capture.plan["transaction"]["turn_key"] = "invalid turn key"
+ route_flag = {
+ "route_integer": 1, "route_string": "true", "route_none": None,
+ "route_mapping": {"enabled": True},
+ }.get(mutation, False)
+ capture.plan["route"]["would_invoke_host"] = route_flag
+ capture.plan["error"] = "" if mutation == "route_fallback" else "staged route refused"
+ return capture
+
+ def unexpected_effect(*args, **kwargs):
+ effects.append((args, kwargs))
+ raise AssertionError("a staged guard refusal must precede execution effects")
+
+ monkeypatch.setattr(executor, "stage_turn_decision_capture", stage_then_mutate)
+ with pytest.raises(ValueError) as refused:
+ executor.run_loopx_turn_once(plan, host_runner=unexpected_effect,
+ project=tmp_path / "project", runtime_root=runtime, goal_id=original["goal_id"],
+ timeout_seconds=5, execute=True, writeback=unexpected_effect,
+ spend=unexpected_effect, scheduler=unexpected_effect, admit_start=unexpected_effect,
+ confirm_start=unexpected_effect, decision_capture_payload=original,
+ require_decision_capture=True)
+ assert str(refused.value) == expected
+ assert len(captures) == 1 and effects == []
+ assert not captures[0].path.exists()
+ journal = turn_journal_path(runtime, goal_id=original["goal_id"],
+ turn_key=request["turn_key"])
+ assert not journal.exists()
+
+
+@pytest.mark.parametrize("mutation,expected", [
+ ("contract", "complete Turn decision content integrity mismatch"),
+ ("source_hash", "complete Host request differs from captured source"),
+ ("capture_key", "complete Host request capture binding differs"),
+ ("adapter", "complete Turn plan requires one source without an executor adapter"),
+])
+def test_staged_capture_rechecks_source_and_header_before_effects(
+ complete_host_request, tmp_path, monkeypatch, mutation, expected,
+):
+ from copy import deepcopy
+ from loopx.control_plane.turn_driver import executor
+ from loopx.control_plane.turn_driver.driver import build_loopx_turn_plan
+
+ request, original = complete_host_request
+ original = deepcopy(original)
+ plan = build_loopx_turn_plan(request["turn_envelope"], work_contract=original,
+ host="generic-cli", execution_mode="isolated-headless",
+ turn_instance_id="staged-source-guards")
+ captures = []
+ original_stage = executor.stage_turn_decision_capture
+
+ def stage_then_mutate(*args, **kwargs):
+ capture = original_stage(*args, **kwargs)
+ captures.append(capture)
+ if mutation == "contract":
+ capture.plan["work_contract"]["agent_identity"]["agent_id"] = "changed-agent"
+ elif mutation == "source_hash":
+ capture.plan["source_decision_hash"] = "sha256:" + "0" * 64
+ elif mutation == "capture_key":
+ capture.plan["detail_ref"]["captured_decision"]["turn_key"] = "sha256:" + "0" * 64
+ else:
+ capture.plan["turn_envelope"] = request["turn_envelope"]
+ return capture
+
+ def unexpected_effect(*_args, **_kwargs):
+ raise AssertionError("a staged source refusal must precede execution effects")
+
+ runtime = tmp_path / "staged-source-runtime"
+ monkeypatch.setattr(executor, "stage_turn_decision_capture", stage_then_mutate)
+ with pytest.raises((ValueError, RuntimeError)) as refused:
+ executor.run_loopx_turn_once(plan, host_runner=unexpected_effect,
+ project=tmp_path / "project", runtime_root=runtime, goal_id=original["goal_id"],
+ timeout_seconds=5, execute=True, writeback=unexpected_effect,
+ spend=unexpected_effect, scheduler=unexpected_effect, admit_start=unexpected_effect,
+ decision_capture_payload=original, require_decision_capture=True)
+ assert str(refused.value) == expected
+ assert len(captures) == 1 and not captures[0].path.exists()
+ assert not turn_journal_path(runtime, goal_id=original["goal_id"],
+ turn_key=request["turn_key"]).exists()
+
+
+@pytest.mark.parametrize("optional_context", [False, True])
+def test_staged_capture_preserves_optional_bridge_startup_diagnostic(
+ complete_host_request, tmp_path, monkeypatch, optional_context,
+):
+ from loopx.control_plane import effect_runtime
+ from loopx.control_plane.runtime.node_probe import NodeProbe, NodeProbeOutcome
+ from loopx.control_plane.turn_driver import executor
+ from loopx.control_plane.turn_driver.driver import build_loopx_turn_plan
+
+ request, original = complete_host_request
+ plan = build_loopx_turn_plan(request["turn_envelope"], work_contract=original,
+ host="generic-cli", execution_mode="isolated-headless",
+ turn_instance_id="staged-bridge-failure")
+ original_carrier = json.dumps(plan, ensure_ascii=False)
+ original_stage = executor.stage_turn_decision_capture
+ original_request = effect_runtime.effect_runtime_request
+ captures, operations, probes = [], [], []
+
+ def observe_request(method, params, **kwargs):
+ operations.append((method, params.get("operation")))
+ return original_request(method, params, **kwargs)
+
+ def missing_node(**_kwargs):
+ probes.append("missing")
+ return NodeProbe(NodeProbeOutcome.MISSING)
+
+ def stage_then_lose_bridge(*args, **kwargs):
+ capture = original_stage(*args, **kwargs)
+ captures.append(capture)
+ if optional_context:
+ capture.plan["turn_context_contributions"] = [{
+ "hook_id": "synthetic-context", "readback_slots": [],
+ }]
+ # A new disposable locator selects the real startup failure path;
+ # no serving runtime or active developer process is retired.
+ monkeypatch.setattr(effect_runtime, "_runtime_dir", lambda: tmp_path / "missing-node-runtime")
+ monkeypatch.setattr(effect_runtime, "_probe_node", missing_node)
+ monkeypatch.setattr(effect_runtime, "effect_runtime_request", observe_request)
+ return capture
+
+ def unexpected_effect(*_args, **_kwargs):
+ raise AssertionError("bridge startup refusal must precede execution effects")
+
+ runtime = tmp_path / "staged-bridge-runtime"
+ monkeypatch.setattr(executor, "stage_turn_decision_capture", stage_then_lose_bridge)
+ with pytest.raises(effect_runtime.EffectRuntimeNodeProbeError) as refused:
+ executor.run_loopx_turn_once(plan, host_runner=unexpected_effect,
+ project=tmp_path / "project", runtime_root=runtime, goal_id=original["goal_id"],
+ timeout_seconds=5, execute=True, writeback=unexpected_effect,
+ spend=unexpected_effect, scheduler=unexpected_effect, admit_start=unexpected_effect,
+ decision_capture_payload=original, require_decision_capture=True)
+ assert refused.value.diagnostic_code == "node_unavailable"
+ assert str(refused.value) == "LoopX Effect runtime requires Node.js 22.22.3 or newer on PATH"
+ assert len(probes) == (2 if optional_context else 1)
+ assert len(operations) == len(probes)
+ if optional_context:
+ assert operations[0] == ("quota.turn_envelope.evaluate", "host_context_binding")
+ assert operations[-1][0] == "quota.turn_envelope.evaluate"
+ assert operations[-1][1] in {"host_context", "host_dispatch_context"}
+ assert len(captures) == 1 and not captures[0].path.exists()
+ assert not turn_journal_path(runtime, goal_id=original["goal_id"],
+ turn_key=request["turn_key"]).exists()
+ assert json.dumps(plan, ensure_ascii=False) == original_carrier
+ assert json.dumps(captures[0].observation, ensure_ascii=False) == json.dumps(original, ensure_ascii=False)
+ assert not list(runtime.rglob("*.jsonl"))
+
+
+@pytest.mark.parametrize("current,child_limit", [(True, 2), (True, True), (False, 2)])
+def test_staged_dispatch_preserves_complete_request_values(
+ complete_host_request, tmp_path, monkeypatch, current, child_limit,
+):
+ from copy import deepcopy
+ from loopx.control_plane.quota import turn_envelope
+ from loopx.control_plane.turn_driver import executor
+ from loopx.control_plane.turn_driver.driver import build_loopx_turn_plan
+
+ _, original = complete_host_request
+ source = deepcopy({key: value for key, value in original.items()
+ if key not in {"schema_version", "contract_binding"}})
+ source["future_task_data"] = {"numbers": [2**60 + 1, 1.0, -0.0],
+ "text": "Preserve all original requirements. \n保留原要求。"}
+ source["interaction_contract"]["agent_context"] = {"contributions": [
+ {"capability_id": "multi_subagent", "facts": {"max_children": child_limit}},
+ ]}
+ contract = turn_envelope.compose_turn_decision(source)
+ envelope = turn_envelope.build_turn_envelope(contract)
+ plan = build_loopx_turn_plan(envelope, host="generic-cli", execution_mode="isolated-headless",
+ turn_instance_id="staged-request-values", **({"work_contract": contract} if current else {}))
+ if not current:
+ # Retain the published transitional adapter decoder as a control.
+ plan["work_contract"] = contract
+ plan["goal_ref"] = {"goal_id": original["goal_id"], "instance_id": "synthetic-instance"}
+ plan["delegation_context"] = {"allowed": False, "detail": "preserve delegation"}
+ plan["child_operations"] = [{"operation": "synthetic-child-observation"}]
+ plan["subagent_execution_topology"] = {"state": "synthetic-topology"}
+ captured, dispatched, admissions = [], [], []
+ original_stage = executor.stage_turn_decision_capture
+ original_check = turn_envelope.complete_turn_host_source
+
+ def observe_stage(*args, **kwargs):
+ capture = original_stage(*args, **kwargs)
+ captured.append(capture)
+ return capture
+
+ def observe_dispatch(request, **kwargs):
+ if kwargs.get("operation") == "host_dispatch_context":
+ dispatched.append(request)
+ return original_check(request, **kwargs)
+
+ def deny_start(admission):
+ admissions.append(admission)
+ return {"admitted": False, "reason": "synthetic interval wait"}
+
+ def unexpected_effect(*_args, **_kwargs):
+ raise AssertionError("interval wait must not publish or execute")
+
+ runtime = tmp_path / "staged-values-runtime"
+ monkeypatch.setattr(executor, "stage_turn_decision_capture", observe_stage)
+ monkeypatch.setattr(turn_envelope, "complete_turn_host_source", observe_dispatch)
+ result = executor.run_loopx_turn_once(plan, host_runner=unexpected_effect,
+ project=tmp_path / "project", runtime_root=runtime, goal_id=original["goal_id"],
+ timeout_seconds=5, execute=True, writeback=unexpected_effect,
+ spend=unexpected_effect, scheduler=unexpected_effect, admit_start=deny_start,
+ decision_capture_payload=contract, require_decision_capture=True)
+ assert result["status"] == "interval_wait"
+ assert len(captured) == len(dispatched) == len(admissions) == 1
+ request = dispatched[0]
+ assert request["schema_version"] == "loopx_turn_host_request_v1"
+ assert "turn_envelope" not in request
+ assert request["turn_key"] == plan["transaction"]["turn_key"]
+ assert request["route"] == plan["route"]["kind"]
+ assert request["session"] == plan["session"]
+ assert request["result_contract"] == {"schema_version": "loopx_turn_result_v0",
+ "completed_phases": ["host_execute", "typed_result"], "stdout": "one public-safe JSON object"}
+ assert request["goal_ref"] == plan["goal_ref"] and request["goal_ref"] is not plan["goal_ref"]
+ for field in ("delegation_context", "child_operations", "subagent_execution_topology"):
+ assert request[field] == plan[field]
+ assert request.get("turn_instance_id") == (None if child_limit is True else "staged-request-values")
+ assert json.dumps(request["work_contract"], ensure_ascii=False) == json.dumps(contract, ensure_ascii=False)
+ assert request["source_decision_hash"] == contract["contract_binding"]["content_hash"]
+ capture_ref = request["detail_ref"]["captured_decision"]
+ assert capture_ref["path"] == str(captured[0].path)
+ assert capture_ref["turn_key"] == request["turn_key"]
+ assert capture_ref["source_hash_ref"] == "$.source_decision_hash"
+ assert not captured[0].path.exists()
+ assert not turn_journal_path(runtime, goal_id=original["goal_id"], turn_key=request["turn_key"]).exists()
+
+
+@pytest.mark.parametrize("optional_context", [False, True])
+@pytest.mark.parametrize("failure", ["snapshot_oversized", "transport_permission"])
+def test_staged_capture_preserves_bridge_transport_failure(
+ complete_host_request, tmp_path, monkeypatch, optional_context, failure,
+):
+ from loopx.control_plane import effect_runtime
+ from loopx.control_plane.turn_driver import executor
+ from loopx.control_plane.turn_driver.driver import build_loopx_turn_plan
+
+ request, original = complete_host_request
+ plan = build_loopx_turn_plan(request["turn_envelope"], work_contract=original,
+ host="generic-cli", execution_mode="isolated-headless",
+ turn_instance_id="staged-transport-failure")
+ original_carrier = json.dumps(plan, ensure_ascii=False)
+ original_stage = executor.stage_turn_decision_capture
+ original_request = effect_runtime.effect_runtime_request
+ captures, operations = [], []
+
+ def observe_request(method, params, **kwargs):
+ operations.append((method, params.get("operation"), kwargs.get("large_local_snapshot", False)))
+ return original_request(method, params, **kwargs)
+
+ def denied_connection(*_args, **_kwargs):
+ raise PermissionError("synthetic transport denial")
+
+ def stage_then_fail_transport(*args, **kwargs):
+ capture = original_stage(*args, **kwargs)
+ captures.append(capture)
+ if optional_context:
+ capture.plan["turn_context_contributions"] = [{
+ "hook_id": "synthetic-context", "readback_slots": [],
+ }]
+ monkeypatch.setattr(effect_runtime, "effect_runtime_request", observe_request)
+ if failure == "snapshot_oversized":
+ # Exercise the existing encoder and offloaded-snapshot fallback
+ # with bounded local limits, without allocating a huge fixture.
+ monkeypatch.setattr(effect_runtime, "MAX_REQUEST_BYTES", 1024)
+ monkeypatch.setattr(effect_runtime, "MAX_LOCAL_SNAPSHOT_BYTES", 512)
+ else:
+ monkeypatch.setattr(effect_runtime.socket, "create_connection", denied_connection)
+ return capture
+
+ def unexpected_effect(*_args, **_kwargs):
+ raise AssertionError("transport refusal must precede execution effects")
+
+ runtime = tmp_path / "staged-transport-runtime"
+ monkeypatch.setattr(executor, "stage_turn_decision_capture", stage_then_fail_transport)
+ with pytest.raises((effect_runtime.EffectRuntimeRejected, effect_runtime.EffectRuntimeHostPermissionError)) as refused:
+ executor.run_loopx_turn_once(plan, host_runner=unexpected_effect,
+ project=tmp_path / "project", runtime_root=runtime, goal_id=original["goal_id"],
+ timeout_seconds=5, execute=True, writeback=unexpected_effect,
+ spend=unexpected_effect, scheduler=unexpected_effect, admit_start=unexpected_effect,
+ decision_capture_payload=original, require_decision_capture=True)
+ if failure == "snapshot_oversized":
+ assert refused.value.diagnostic_code == "request_too_large"
+ assert str(refused.value) == "TypeScript Effect runtime local snapshot is oversized"
+ assert [item[2] for item in operations] == ([False, True] * (2 if optional_context else 1))
+ else:
+ assert refused.value.diagnostic_code == "runtime_host_permission_denied"
+ assert str(refused.value) == ("Host permission denied access to the local TypeScript Effect runtime "
+ "before request dispatch; no capability operation was executed")
+ assert [item[2] for item in operations] == ([False] * (2 if optional_context else 1))
+ if optional_context:
+ assert operations[0][:2] == ("quota.turn_envelope.evaluate", "host_context_binding")
+ assert operations[-1][0] == "quota.turn_envelope.evaluate"
+ assert operations[-1][1] in {"host_context", "host_dispatch_context"}
+ assert len(captures) == 1 and not captures[0].path.exists()
+ assert not turn_journal_path(runtime, goal_id=original["goal_id"], turn_key=request["turn_key"]).exists()
+ assert json.dumps(plan, ensure_ascii=False) == original_carrier
+ assert json.dumps(captures[0].observation, ensure_ascii=False) == json.dumps(original, ensure_ascii=False)
+ assert not list(runtime.rglob("*.jsonl"))
+
+
def test_typed_result_recovery_does_not_start_a_pending_goal_host(
complete_host_request, tmp_path,
):
diff --git a/tests/control_plane_ts/action_review_plan_session.test.ts b/tests/control_plane_ts/action_review_plan_session.test.ts
new file mode 100644
index 0000000000..6691ce9110
--- /dev/null
+++ b/tests/control_plane_ts/action_review_plan_session.test.ts
@@ -0,0 +1,99 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+import {compileActionReviewPlan} from "../../loopx/control_plane/presentation/action_review_plan.ts";
+
+const proposal = {proposal_id: "new-goal", expected_state_fingerprint: "reviewed-source",
+ action_kind: "goal.create", permission_classification: "durable_write", status: "preview_ready",
+ normalized_parameters: {goal_id: "new-goal", title: "New Goal", agent_id: "codex",
+ initial_todos: ["Deliver the original requested work"]}};
+const steps = {workspace_validated: {outcome: "workspace_validated", workspace_digest: "a".repeat(64)},
+ goal_bootstrapped: {outcome: "goal_bootstrapped", goal_id: "new-goal"},
+ agent_bound: {outcome: "agent_bound", goal_id: "new-goal", agent_id: "codex"},
+ todos_created: {outcome: "todos_created", todo_ids: ["todo-original"]}};
+
+test("a new default Codex Goal chooses managed Agent routing before its first Session", () => {
+ for (const checkpoint of [undefined, null, {steps: {}}, {steps}]) {
+ const plan = compileActionReviewPlan({...proposal, checkpoint});
+ assert.deepEqual(plan.creationSession, {managedAgentId: "codex"});
+ assert.equal(plan.canApply, true);
+ assert.equal(plan.creationProgress?.sessionId, undefined);
+ assert.equal(plan.creationProgress?.firstTurn, undefined);
+ }
+ assert.deepEqual(compileActionReviewPlan({...proposal, normalized_parameters: {
+ ...proposal.normalized_parameters, native_token_budget: null}}).creationSession, {managedAgentId: "codex"});
+});
+
+test("explicit budgets and other endpoints preserve the legacy first Session route", () => {
+ // Invalid explicit budget shapes are rejected upstream; a pure projection
+ // must not turn them into a managed default through truthiness.
+ for (const native_token_budget of [1, 10000, 0, false, "", "10000", {}, []]) {
+ assert.equal(Object.hasOwn(compileActionReviewPlan({...proposal, normalized_parameters: {
+ ...proposal.normalized_parameters, native_token_budget}}), "creationSession"), false);
+ }
+ for (const agent_id of ["antigravity", "claude-code", "other-codex", "codex@worker", " codex ", "", null, undefined]) {
+ assert.equal(Object.hasOwn(compileActionReviewPlan({...proposal, normalized_parameters: {
+ ...proposal.normalized_parameters, agent_id}}), "creationSession"), false);
+ }
+ assert.equal(compileActionReviewPlan({...proposal, action_kind: "todo.create"}).creationSession, undefined);
+});
+
+test("bootstrap retry chooses a new route only while no Session has been recorded", () => {
+ for (const status of ["failed", "applying"]) {
+ const plan = compileActionReviewPlan({...proposal, status, checkpoint: {steps}});
+ assert.equal(plan.retryOriginal, true);
+ assert.deepEqual(plan.creationSession, {managedAgentId: "codex"});
+ assert.deepEqual(plan.creationProgress?.todoIds, ["todo-original"]);
+ }
+});
+
+test("recorded legacy Session and first Turn retain their original recovery identity", () => {
+ const first_session_opened = {outcome: "first_session_opened", session_id: "original-session"};
+ const first_turn_started = {outcome: "first_turn_started", session_id: "original-session", turn_id: "original-turn"};
+ for (const recorded of [{first_session_opened}, {first_turn_started}, {first_session_opened, first_turn_started}]) {
+ const plan = compileActionReviewPlan({...proposal, status: "failed", checkpoint: {steps: {...steps, ...recorded}}});
+ assert.equal(plan.retryOriginal, true);
+ assert.equal(Object.hasOwn(plan, "creationSession"), false);
+ if ("first_session_opened" in recorded) assert.equal(plan.creationProgress?.sessionId, "original-session");
+ if ("first_turn_started" in recorded)
+ assert.deepEqual(plan.creationProgress?.firstTurn, {sessionId: "original-session", turnId: "original-turn"});
+ }
+});
+
+test("malformed recorded checkpoints cannot be treated as a new default Session", () => {
+ for (const value of [undefined, null, false, [], {}, {session_id: "original-session"}]) {
+ for (const field of ["first_session_opened", "first_turn_started"]) {
+ const plan = compileActionReviewPlan({...proposal, status: "failed", checkpoint: {steps: {...steps, [field]: value}}});
+ assert.equal(Object.hasOwn(plan, "creationSession"), false);
+ }
+ }
+ for (const checkpoint of [[], false, {}, {steps: null}, {steps: []},
+ {steps: {...steps, goal_bootstrapped: {...steps.goal_bootstrapped, goal_id: "other"}}},
+ {steps: {...steps, first_session_opened: {outcome: "first_session_opened", session_id: "original-session"},
+ first_turn_started: {outcome: "first_turn_started", session_id: "other-session", turn_id: "turn"}}}]) {
+ assert.equal(compileActionReviewPlan({...proposal, status: "failed", checkpoint}).creationSession, undefined);
+ }
+});
+
+test("terminal or persisted first-Turn readback does not advertise another Session", () => {
+ assert.equal(compileActionReviewPlan({...proposal, status: "applied", receipt: {projection_verified: true}}).creationSession, undefined);
+ for (const first_turn_readback of [null, {}, {status: "queued"},
+ {status: "failed", completed_at: "2026-01-01T00:00:01Z", started_at: null, upstream_turn_id: null}]) {
+ assert.equal(compileActionReviewPlan({...proposal, first_turn_readback}).creationSession, undefined);
+ }
+});
+
+test("managed routing leaves requirement retention and action authority independent", () => {
+ const input = {...proposal, normalized_parameters: {...proposal.normalized_parameters,
+ completion_criteria: "Keep the original acceptance boundary"}, checkpoint: {steps}};
+ const plan = compileActionReviewPlan(input);
+ assert.deepEqual(plan.creationSession, {managedAgentId: "codex"});
+ assert.deepEqual(plan.creationOutcome?.refinement.requirements,
+ [{id: "completion", description: "Keep the original acceptance boundary"}]);
+ assert.equal(Object.hasOwn(plan, "taskExecution"), false);
+ assert.equal(Object.hasOwn(plan, "taskAccepted"), false);
+ for (const status of ["gated", "stale", "rejected"]) {
+ const held = compileActionReviewPlan({...input, status});
+ assert.equal(held.canApply, false);
+ assert.equal(held.retryOriginal, undefined);
+ }
+});
diff --git a/tests/control_plane_ts/coordination_runtime_shadow.test.ts b/tests/control_plane_ts/coordination_runtime_shadow.test.ts
index c59a4bd26c..2050a0a27e 100644
--- a/tests/control_plane_ts/coordination_runtime_shadow.test.ts
+++ b/tests/control_plane_ts/coordination_runtime_shadow.test.ts
@@ -377,7 +377,9 @@ for (const scenario of [
]),
], {encoding: "utf8", timeout: 60_000});
assert.equal(result.status, 0, result.stderr);
- const observed = JSON.parse(result.stdout.trim().split("\n").at(-1));
+ const resultLine = result.stdout.trim().split("\n").at(-1);
+ assert.ok(resultLine, "the source lease probe must report its observation");
+ const observed = JSON.parse(resultLine);
assert.equal(observed.status === "failed" ? observed.reason_code : observed.status, scenario.expected);
const passes = scenario.expected === "applied" ? 2 : 1;
assert.equal(observed.lstatCalls, 2 * passes);
diff --git a/tests/control_plane_ts/goal_creation_execution_review.test.ts b/tests/control_plane_ts/goal_creation_execution_review.test.ts
new file mode 100644
index 0000000000..0806702f84
--- /dev/null
+++ b/tests/control_plane_ts/goal_creation_execution_review.test.ts
@@ -0,0 +1,29 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+import {goalCreationExecutionReview, normalizeGoalCreationExecution} from "../../loopx/control_plane/goals/creation_execution_review.ts";
+
+const original = () => ({agent_id: "codex", start_execution: true, permission: "read_only",
+ completion_criteria: "The existing independent check accepts the requested result.", initial_todos: ["Complete the original work."]});
+
+test("old creation and explicit no-start retain no executable request", () => {
+ assert.deepEqual(normalizeGoalCreationExecution({agent_id: "codex"}), {});
+ assert.equal(goalCreationExecutionReview({permission: "read_only"}), undefined);
+ assert.equal(goalCreationExecutionReview({...original(), start_execution: false}), undefined);
+});
+
+test("an explicit read-only request starts work without granting writes", () => {
+ assert.deepEqual(goalCreationExecutionReview(original()), {requested: true, permission: "read_only", writeScope: []});
+});
+
+test("only the existing explicit workspace-write review selects the whole workspace", () => {
+ assert.deepEqual(goalCreationExecutionReview({...original(), permission: "workspace_write_on_confirmation"}),
+ {requested: true, permission: "workspace_write_on_confirmation", writeScope: ["**"]});
+});
+
+for (const changed of [{permission: "write"}, {permission: ""}, {permission: null}, {start_execution: "true"},
+ {agent_id: "claude-code"}, {native_token_budget: 1000}, {initial_todos: []}, {initial_todos: ["one", "two"]},
+ {completion_criteria: ""}]) {
+ test(`invalid or competing creation intent ${JSON.stringify(changed)} is rejected before creation`, () => {
+ assert.throws(() => normalizeGoalCreationExecution({...original(), ...changed}));
+ });
+}
diff --git a/tests/control_plane_ts/original_creation_task_selection.test.ts b/tests/control_plane_ts/original_creation_task_selection.test.ts
new file mode 100644
index 0000000000..5076fed28a
--- /dev/null
+++ b/tests/control_plane_ts/original_creation_task_selection.test.ts
@@ -0,0 +1,93 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+import {originalCreationTaskSelection} from "../../loopx/control_plane/goals/task_validation_plan.ts";
+
+import {canonicalNativeTaskSandboxPolicy} from "../../loopx/control_plane/collaboration/steward_managed_mode.ts";
+
+const basis = (outcomes: unknown[]) => ({status: "loaded", provider_revision: "current",
+ goal_acceptance_contract: {outcome_requirements: {outcomes}}});
+
+test("original Task comes from retained creation work, independently of later outcomes", () => {
+ assert.deepEqual(originalCreationTaskSelection({basis: basis([
+ {id: "later", todo_ids: ["newest"]}, {id: "goal-creation", todo_ids: ["original"]},
+ ])}), {todo_id: "original", reason: null});
+});
+
+for (const [name, outcomes] of [
+ ["missing origin", [{id: "later", todo_ids: ["newest"]}]],
+ ["multiple initial Tasks", [{id: "goal-creation", todo_ids: ["first", "second"]}]],
+ ["removed work", [{id: "goal-creation", todo_ids: []}]],
+ ["duplicated origin", [{id: "goal-creation", todo_ids: ["one"]}, {id: "goal-creation", todo_ids: ["two"]}]],
+] as const) test(`${name} never selects the first or newest Task`, () => {
+ assert.equal(originalCreationTaskSelection({basis: basis([...outcomes])}).todo_id, null);
+});
+
+test("unavailable canonical authority is not a display selection", () => {
+ assert.throws(() => originalCreationTaskSelection({basis: {...basis([]), status: "unavailable"}}),
+ /current Goal acceptance basis/);
+});
+
+const policyInput = () => ({host: "codex-app-server", actor: {schema_version: "loopx_managed_session_actor_v0", goal_id: "goal",
+ agent_id: "agent", workspace_path: "/exercise"}, validation: {todo_id: "original", state: "ready"},
+task_lease: {schema_version: "task_lease_v0", status: "active", goal_id: "goal", todo_id: "original",
+ owner: "agent", version: 1, write_scopes: [] as string[]},
+authority: {_work_identity: {goal_id: "goal", agent_id: "agent", todo_id: "original"},
+ execution_preparation: {todo_id: "original", agent_id: "agent", state: "ready", delivery_allowed: true},
+ should_run: true, normal_delivery_allowed: true, goal_boundary: {}}});
+
+test("a native Task or its confirmed checks do not grant workspace writes", () => {
+ assert.deepEqual(canonicalNativeTaskSandboxPolicy(policyInput()), {type: "readOnly", networkAccess: false});
+});
+
+test("workspace approval cannot widen the current Task lease", () => {
+ const input = policyInput();
+ input.authority.goal_boundary = {checkpointed_boundary_authority: {
+ schema_version: "checkpointed_boundary_authority_v0", active_count: 1, active_write_scope: ["**"]}};
+ input.task_lease.write_scopes = ["cart.py"];
+ assert.deepEqual(canonicalNativeTaskSandboxPolicy(input), {type: "readOnly", networkAccess: false});
+ input.task_lease.todo_id = "other";
+ assert.throws(() => canonicalNativeTaskSandboxPolicy(input), /same current execution lease/);
+});
+
+for (const scope of ["cart.py", "outputs/**"]) test(`narrow ${scope} approval cannot grant the whole native workspace`, () => {
+ const input = policyInput();
+ input.authority.goal_boundary = {checkpointed_boundary_authority: {
+ schema_version: "checkpointed_boundary_authority_v0", active_count: 1, active_write_scope: [scope]}};
+ assert.deepEqual(canonicalNativeTaskSandboxPolicy(input), {type: "readOnly", networkAccess: false});
+});
+
+test("the original admitted Task uses the already approved workspace with network and temp writes disabled", () => {
+ const input = policyInput();
+ input.authority.goal_boundary = {checkpointed_boundary_authority: {
+ schema_version: "checkpointed_boundary_authority_v0", active_count: 1, active_write_scope: ["**"]}};
+ const expected = {type: "workspaceWrite", writableRoots: ["/exercise"], networkAccess: false,
+ excludeTmpdirEnvVar: true, excludeSlashTmp: true};
+ assert.deepEqual(canonicalNativeTaskSandboxPolicy(input), expected);
+ input.task_lease.write_scopes = ["**"];
+ assert.deepEqual(canonicalNativeTaskSandboxPolicy(input), expected);
+});
+
+for (const scope of ["src/*.py", "src/[ab].py", "../outside/**", "/outside/file"]) {
+ test(`unrepresentable ${scope} approval is never converted to a broader directory`, () => {
+ const input = policyInput();
+ input.authority.goal_boundary = {checkpointed_boundary_authority: {
+ schema_version: "checkpointed_boundary_authority_v0", active_count: 1, active_write_scope: [scope]}};
+ assert.deepEqual(canonicalNativeTaskSandboxPolicy(input), {type: "readOnly", networkAccess: false});
+ });
+}
+
+for (const count of [undefined, 0]) test(`an approval without an active count of ${count} grants no workspace writes`, () => {
+ const input = policyInput();
+ input.authority.goal_boundary = {checkpointed_boundary_authority: {
+ schema_version: "checkpointed_boundary_authority_v0", active_count: count, active_write_scope: ["**"]}};
+ assert.deepEqual(canonicalNativeTaskSandboxPolicy(input), {type: "readOnly", networkAccess: false});
+});
+
+test("Task substitution and an unavailable execution lease cannot yield host permissions", () => {
+ const substituted = policyInput();
+ substituted.authority._work_identity.todo_id = "other";
+ assert.throws(() => canonicalNativeTaskSandboxPolicy(substituted), /original admitted Task/);
+ const unavailable = policyInput();
+ unavailable.authority.execution_preparation.state = "unavailable";
+ assert.throws(() => canonicalNativeTaskSandboxPolicy(unavailable), /execution lease/);
+});
diff --git a/tests/control_plane_ts/postgresql_authority_seed.test.ts b/tests/control_plane_ts/postgresql_authority_seed.test.ts
new file mode 100644
index 0000000000..5c3fb39c88
--- /dev/null
+++ b/tests/control_plane_ts/postgresql_authority_seed.test.ts
@@ -0,0 +1,255 @@
+import assert from "node:assert/strict";
+import { mkdtemp, readFile, rm } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import test from "node:test";
+import {
+ exportLocalToPgGenesisSeed,
+ sanitizeGenesisSeedProjection,
+ scanForbiddenPrivacyTokens,
+ setupPostgreSqlAuthoritySchemaAndRole,
+} from "../../loopx/control_plane/coordination/postgresql_authority_seed.ts";
+import { FileAuthorityStore } from "../../loopx/control_plane/coordination/file_authority_store.ts";
+import type {PostgreSqlAuthorityDatabase, PostgreSqlAuthorityConnection} from "../../loopx/control_plane/coordination/postgresql_authority_store.ts";
+
+test("sanitizeGenesisSeedProjection strips expired leases", () => {
+ const past = new Date(Date.now() - 3600_000).toISOString();
+ const future = new Date(Date.now() + 3600_000).toISOString();
+
+ const projection = {
+ schema_version: "loopx_authority_projection_v0",
+ goal_id: "test-goal",
+ task_leases: {
+ leases: {
+ "lease-expired": {
+ lease_id: "lease-expired",
+ holder: "agent-1",
+ expires_at: past,
+ },
+ "lease-active": {
+ lease_id: "lease-active",
+ holder: "agent-2",
+ expires_at: future,
+ },
+ },
+ },
+ };
+
+ const { projection: sanitized, report } = sanitizeGenesisSeedProjection(projection);
+ const leases = (sanitized.task_leases as { leases?: Record })?.leases ?? {};
+ assert.equal(leases["lease-expired"], undefined);
+ assert.ok(leases["lease-active"] !== undefined);
+ assert.equal(report.leases_stripped, 1);
+});
+
+test("sanitizeGenesisSeedProjection redacts local paths and .local references", () => {
+ const projection = {
+ schema_version: "loopx_authority_projection_v0",
+ goal_id: "test-goal",
+ runtime_root: "/Users/alice/Developer/loopx/.local/runtime",
+ project_root: "/Users/alice/Developer/my-repo",
+ registered_materials: [
+ {
+ material_id: "mat-1",
+ path: "/Users/alice/Developer/my-repo/docs/readme.md",
+ boundary: "public",
+ },
+ {
+ material_id: "mat-2",
+ path: "/Users/alice/.local/notes/secret.md",
+ boundary: "local_private",
+ },
+ ],
+ };
+
+ const { projection: sanitized, report } = sanitizeGenesisSeedProjection(projection);
+ // .local path should be redacted
+ assert.ok(!JSON.stringify(sanitized).includes("/Users/alice/.local"));
+ assert.ok(!JSON.stringify(sanitized).includes("/Users/alice/Developer"));
+ // private boundary material must be stripped
+ const materials = (sanitized.registered_materials as Array<{ material_id: string }>) ?? [];
+ assert.equal(materials.length, 1);
+ assert.equal(materials[0].material_id, "mat-1");
+ assert.equal(report.private_materials_stripped, 1);
+ assert.ok(report.paths_redacted > 0);
+});
+
+test("sanitizeGenesisSeedProjection redacts Feishu IDs", () => {
+ const feishuUser = "ou_abcdef1234567890abcdef12345678";
+ const feishuChat = "oc_1234567890abcdef1234567890abcd";
+ const projection = {
+ schema_version: "loopx_authority_projection_v0",
+ goal_id: "test-goal",
+ owner_identity: feishuUser,
+ channel: {
+ chat_id: feishuChat,
+ app_id: "cli_a1b2c3d4e5f6g7h8",
+ },
+ };
+
+ const { projection: sanitized, report } = sanitizeGenesisSeedProjection(projection);
+ const serialized = JSON.stringify(sanitized);
+ assert.ok(!serialized.includes(feishuUser));
+ assert.ok(!serialized.includes(feishuChat));
+ assert.ok(!serialized.includes("cli_a1b2c3d4e5f6g7h8"));
+ assert.ok(serialized.includes("[feishu_id_redacted]"));
+ assert.ok(report.feishu_ids_redacted >= 3);
+});
+
+test("scanForbiddenPrivacyTokens fails closed on sensitive credentials", () => {
+ const clean = {
+ goal_id: "clean-goal",
+ objective: "Just a clean goal with safe public description",
+ };
+ assert.doesNotThrow(() => scanForbiddenPrivacyTokens(clean));
+
+ const withSecret = {
+ goal_id: "leaky-goal",
+ apiKey: ["sk-", "fixture", "0".repeat(32)].join(""),
+ };
+ assert.throws(() => scanForbiddenPrivacyTokens(withSecret), /Privacy boundary violation/);
+
+ const withGithubToken = {
+ token: ["ghp", "fixture".repeat(6)].join("_"),
+ };
+ assert.throws(() => scanForbiddenPrivacyTokens(withGithubToken), /Privacy boundary violation/);
+
+ const privateKey = "ou_123456abcdef";
+ for (let i = 0; i < 2; i++) {
+ assert.throws(() => sanitizeGenesisSeedProjection({[privateKey]: {safe: true}}), error => {
+ assert.ok(error instanceof Error);
+ return /Privacy boundary violation/.test(error.message) && !error.message.includes(privateKey);
+ });
+ }
+ assert.throws(() => sanitizeGenesisSeedProjection({[withGithubToken.token]: 1}), /Privacy boundary violation/);
+ const assignment = ["password", "fixture-only-value"].join("=");
+ assert.throws(() => sanitizeGenesisSeedProjection({payload: assignment}), /Privacy boundary violation/);
+});
+
+test("setupPostgreSqlAuthoritySchemaAndRole installs DDL, RLS, and restricted role", async () => {
+ const executedStatements: string[] = [];
+ let connects = 0;
+ const storeId = "postgresql:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
+ const connection: PostgreSqlAuthorityConnection = {
+ query: async (sql: string, params?: readonly unknown[]) => {
+ executedStatements.push(sql);
+ if (sql.includes("FROM loopx_control_plane.authority_store_metadata")) {
+ return {
+ rows: [{schema_version: "loopx_postgresql_authority_store_v1", store_identity: storeId}],
+ rowCount: 1,
+ };
+ }
+ return {rows: [], rowCount: 1};
+ },
+ release: () => {},
+ };
+
+ const database: PostgreSqlAuthorityDatabase = {
+ connect: async () => {
+ connects += 1;
+ return connection;
+ },
+ };
+
+ const result = await setupPostgreSqlAuthoritySchemaAndRole(database, {
+ storeIdentity: storeId,
+ runtimeRole: "loopx_runtime_worker",
+ });
+
+ assert.equal(result.status, "installed");
+ assert.equal(result.store_identity, "postgresql:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
+ assert.equal(result.runtime_role, "loopx_runtime_worker");
+ assert.equal(connects, 1);
+
+ // Verify key DDL statements were executed
+ assert.ok(executedStatements.some(s => s.includes("CREATE SCHEMA IF NOT EXISTS loopx_control_plane")));
+ assert.ok(executedStatements.some(s => s.includes("CREATE TABLE IF NOT EXISTS loopx_control_plane.authority_checkpoints")));
+ assert.ok(executedStatements.some(s => s.includes("CREATE TABLE IF NOT EXISTS loopx_control_plane.authority_commits")));
+ assert.ok(executedStatements.some(s => s.includes("CREATE ROLE \"loopx_runtime_worker\"")));
+ assert.ok(executedStatements.some(s => s.includes("GRANT USAGE ON SCHEMA loopx_control_plane TO \"loopx_runtime_worker\"")));
+ const grants = executedStatements.findIndex(s =>
+ /GRANT SELECT, INSERT ON\s+loopx_control_plane\.authority_checkpoints/.test(s));
+ assert.ok(grants > executedStatements.indexOf("BEGIN") && grants < executedStatements.indexOf("COMMIT"));
+});
+
+test("exportLocalToPgGenesisSeed exports sanitized cursor=1 genesis seed JSON", async () => {
+ const root = await mkdtemp(join(tmpdir(), "genesis-seed-test-"));
+ try {
+ const goalId = "export-goal";
+ const storePath = join(root, "goals", goalId, "authority");
+ const store = new FileAuthorityStore(storePath, goalId);
+
+ const past = new Date(Date.now() - 3600_000).toISOString();
+ const future = new Date(Date.now() + 3600_000).toISOString();
+
+ const commit1 = await store.commitAuthority({
+ operation_id: "op-1",
+ expected_provider_revision: null,
+ events: [],
+ receipts: [],
+ next_projection: {
+ schema_version: "loopx_authority_projection_v0",
+ goal_id: goalId,
+ leases: [
+ { todo_id: "todo-1", expires_at: past, owner: "agent-old" },
+ { todo_id: "todo-2", expires_at: future, owner: "agent-active" },
+ ],
+ todos: [
+ {
+ id: "todo-1",
+ title: "Task with Feishu ID ou_1234567890abcdef1234567890",
+ required_write_scopes: ["/Users/developer/loopx/packages/core", ".local/temp"],
+ },
+ ],
+ authority_registry: {
+ project_materials: {internal: {boundary: "local_private", purpose: "private synthetic fixture"}},
+ topic_authority: {internal: ["internal"]},
+ },
+ goal_direction_scope: {enabled: true},
+ material_usage_receipts: [{purpose: "private usage witness"}],
+ },
+ });
+ assert.equal(commit1.status, "applied");
+
+ const outputPath = join(root, "genesis-seed.json");
+ const sourceBefore = await store.loadAuthority();
+ let attemptedTargetConnection = false;
+ const result = await exportLocalToPgGenesisSeed({
+ runtimeRoot: root,
+ goalId,
+ localStore: store,
+ outputPath,
+ tenantId: "custom-tenant",
+ targetDatabase: {connect: async () => {
+ attemptedTargetConnection = true;
+ throw new Error("preview must never connect");
+ }},
+ });
+ if (result.status !== "exported") {
+ console.error("Export failed:", result);
+ }
+
+ assert.equal(result.status, "exported");
+ assert.equal(result.cursor, "1");
+ assert.equal(result.goal_id, goalId);
+ assert.equal(result.tenant_id, "custom-tenant");
+ assert.ok(result.seed_digest);
+ assert.equal(result.sanitization_report?.leases_stripped, 1);
+ assert.equal(result.sanitization_report?.private_materials_stripped, 1);
+ assert.equal(attemptedTargetConnection, false);
+ assert.deepEqual(await store.loadAuthority(), sourceBefore);
+
+ const writtenJson = JSON.parse(await readFile(outputPath, "utf8"));
+ assert.equal(writtenJson.schema_version, "loopx_pg_genesis_seed_v0");
+ assert.equal(writtenJson.cursor, "1");
+ assert.equal(writtenJson.projection.leases.length, 1);
+ assert.equal(writtenJson.projection.leases[0].todo_id, "todo-2");
+ assert.ok(!JSON.stringify(writtenJson).includes("ou_1234567890abcdef1234567890"));
+ assert.ok(!JSON.stringify(writtenJson).includes(".local/temp"));
+ assert.equal(writtenJson.projection.authority_registry, undefined);
+ assert.equal(writtenJson.projection.goal_direction_scope, undefined);
+ assert.equal(writtenJson.projection.material_usage_receipts, undefined);
+ } finally {
+ await rm(root, { recursive: true, force: true });
+ }
+});
diff --git a/tests/control_plane_ts/postgresql_authority_store.integration.test.ts b/tests/control_plane_ts/postgresql_authority_store.integration.test.ts
index aac6b5f029..7d5d7d1bbf 100644
--- a/tests/control_plane_ts/postgresql_authority_store.integration.test.ts
+++ b/tests/control_plane_ts/postgresql_authority_store.integration.test.ts
@@ -26,6 +26,8 @@ import {canonicalReadRoot} from "./local_promotion_fixture.ts";
import {coordinationTodoReadModel} from "../../loopx/control_plane/coordination/coordination_projection.ts";
import {inspectGoalAcceptance, refineGoalAcceptanceOutcome} from "../../loopx/control_plane/goals/acceptance_authority.ts";
import {authorityProjectionFixture} from "./authority_projection_fixture.ts";
+import {exportLocalToPgGenesisSeed, setupPostgreSqlAuthoritySchemaAndRole} from "../../loopx/control_plane/coordination/postgresql_authority_seed.ts";
+import {FileAuthorityStore} from "../../loopx/control_plane/coordination/file_authority_store.ts";
import type {JsonObject} from "../../loopx/control_plane/effect_program.ts";
import {
authorityStoreCommitFixture as commit,
@@ -37,6 +39,7 @@ const pool = connectionString ? new Pool({ connectionString, max: 12 }) : null;
const STORE_IDENTITY = `postgresql:${"b".repeat(32)}`;
const TENANT_SCOPED_TABLES = [
"authority_heads",
+ "authority_checkpoints",
"authority_commits",
"authority_events",
"authority_receipts",
@@ -59,20 +62,11 @@ function quotedRole(role: string): string {
}
async function createRuntimeRole(role: string): Promise {
- const identifier = quotedRole(role);
- await pool!.query(`CREATE ROLE ${identifier} NOLOGIN`);
- await pool!.query(`GRANT USAGE ON SCHEMA loopx_control_plane TO ${identifier}`);
- await pool!.query(
- `GRANT SELECT ON loopx_control_plane.authority_store_metadata TO ${identifier}`,
- );
- await pool!.query(
- `GRANT SELECT, INSERT, UPDATE ON loopx_control_plane.authority_heads TO ${identifier}`,
- );
- await pool!.query(
- `GRANT SELECT, INSERT ON loopx_control_plane.authority_commits,
- loopx_control_plane.authority_events,
- loopx_control_plane.authority_receipts TO ${identifier}`,
- );
+ const result = await setupPostgreSqlAuthoritySchemaAndRole(database!, {
+ storeIdentity: STORE_IDENTITY,
+ runtimeRole: role,
+ });
+ assert.equal(result.status, "installed", result.reason);
}
function databaseForRuntimeRole(value: Pool, role: string): PostgreSqlAuthorityDatabase {
@@ -690,10 +684,53 @@ if (database && installed) {
tenant_id: secondTenant,
goal_id: goalId,
});
- assert.equal((await first.commitAuthority(commit(null, "operation-a", 1, 1))).status, "applied");
+ const root = await mkdtemp(join(tmpdir(), "pg-genesis-source-"));
+ t.after(() => rm(root, {recursive: true, force: true}));
+ const source = new FileAuthorityStore(root, goalId);
+ const synthetic = {goal_id: goalId, counter: 1, detail: "stable fixture"};
+ assert.equal((await source.commitAuthority({
+ operation_id: "local-fixture", expected_provider_revision: null,
+ events: [], receipts: [], next_projection: synthetic,
+ })).status, "applied");
+ const seed = {runtimeRoot: root, goalId, tenantId: firstTenant, localStore: source,
+ outputPath: join(root, "seed.json"),
+ storeIdentity: STORE_IDENTITY, targetDatabase: runtimeDatabase, execute: true};
+ const genesis = await exportLocalToPgGenesisSeed(seed);
+ assert.equal(genesis.status, "seeded", genesis.reason);
+ const originalSeedReceipt = await first.readReceipt(genesis.operation_id);
+ assert.equal(originalSeedReceipt.status, "found");
+ const retried = await exportLocalToPgGenesisSeed(seed);
+ assert.equal(retried.status, "seeded", retried.reason);
+ assert.equal(retried.provider_revision, genesis.provider_revision);
+ assert.equal(retried.seed_digest, genesis.seed_digest);
+ assert.deepEqual(await first.readReceipt(genesis.operation_id), originalSeedReceipt);
+ assert.equal((await exportLocalToPgGenesisSeed({...seed, storeIdentity: `postgresql:${"d".repeat(32)}`})).status, "failed");
assert.equal((await second.commitAuthority(commit(null, "operation-b", 1, 1))).status, "applied");
- assert.equal((await first.loadAuthority()).status, "loaded");
assert.equal((await second.readReceipt("operation-b")).status, "found");
+ let revision = genesis.provider_revision;
+ for (let cursor = 2; cursor <= 67; cursor++) {
+ const result = await first.commitAuthority({
+ expected_provider_revision: revision,
+ operation_id: `append-${cursor}`,
+ events: [{counter: cursor}],
+ receipts: [{verified_cursor: cursor}],
+ next_projection: {...synthetic, counter: cursor},
+ });
+ assert.equal(result.status, "applied", JSON.stringify(result));
+ if (result.status === "applied") revision = result.provider_revision;
+ }
+ const page = await first.scanCommitted("62", 4);
+ assert.equal(page.status, "page");
+ if (page.status !== "page") assert.fail(JSON.stringify(page));
+ assert.deepEqual(page.transactions.map(row => row.projection.counter), [63, 64, 65, 66]);
+ assert.deepEqual(page.transactions[2]?.events, [{counter: 65}]);
+ assert.deepEqual(page.transactions[2]?.receipts, [{verified_cursor: 65}]);
+ assert.equal(page.has_more, true);
+ assert.equal((await first.readReceipt("append-65")).status, "found");
+ const original = await first.readReceipt("append-65");
+ assert.deepEqual(await first.readReceipts(["append-65", "operation-b", "append-65"]), {
+ status: "receipts", results: [original, {status: "missing"}, original],
+ });
await queryAsRuntimeRole(runtimePool, role, async (client) => {
for (const table of TENANT_SCOPED_TABLES) {
@@ -716,6 +753,14 @@ if (database && installed) {
}
await client.query("ROLLBACK");
+ for (const statement of [
+ "UPDATE loopx_control_plane.authority_checkpoints SET projection = '{}'::jsonb",
+ "DELETE FROM loopx_control_plane.authority_checkpoints",
+ "UPDATE loopx_control_plane.authority_commits SET projection_digest = ''",
+ "DELETE FROM loopx_control_plane.authority_commits",
+ "CREATE TABLE loopx_control_plane.forbidden (id text)",
+ ]) await assert.rejects(client.query(statement), /permission denied/);
+
await client.query("BEGIN");
await client.query(
"SELECT set_config('loopx.tenant_id', $1, TRUE)",
@@ -742,6 +787,100 @@ if (database && installed) {
);
});
});
+
+ test("PostgreSQL v0 upgrade preserves restricted history access and rejects corrupt prefixes", async t => {
+ const suffix = randomUUID().replaceAll("-", "");
+ const name = `upgrade_${suffix}`;
+ const role = `worker_${suffix}`;
+ const grantRole = `journal_${suffix}`;
+ const url = new URL(connectionString!);
+ url.pathname = `/${name}`;
+ const legacy = new Pool({connectionString: url.href});
+ await pool!.query(`CREATE DATABASE ${name}`);
+ t.after(async () => {
+ await legacy.end();
+ await pool!.query(`DROP DATABASE ${name} WITH (FORCE)`);
+ await pool!.query(`DROP ROLE IF EXISTS ${quotedRole(role)}, ${quotedRole(grantRole)}`);
+ });
+ await legacy.query(POSTGRESQL_AUTHORITY_STORE_SCHEMA_SQL);
+ // Restore the released v0 shape in the disposable database. There are no
+ // v1 rows or checkpoints at this point.
+ await legacy.query(`
+ DROP TABLE loopx_control_plane.authority_checkpoints;
+ ALTER TABLE loopx_control_plane.authority_commits
+ DROP COLUMN delta, DROP COLUMN projection_digest, ALTER COLUMN projection SET NOT NULL;
+ INSERT INTO loopx_control_plane.authority_store_metadata (schema_version, store_identity)
+ VALUES ('loopx_postgresql_authority_store_v0', '${STORE_IDENTITY}');
+ CREATE ROLE ${quotedRole(grantRole)} NOLOGIN;
+ CREATE ROLE ${quotedRole(role)} NOLOGIN;
+ GRANT ${quotedRole(grantRole)} TO ${quotedRole(role)};
+ GRANT USAGE ON SCHEMA loopx_control_plane TO ${quotedRole(role)};
+ GRANT SELECT ON loopx_control_plane.authority_store_metadata TO ${quotedRole(role)};
+ GRANT SELECT, INSERT, UPDATE ON loopx_control_plane.authority_heads TO ${quotedRole(role)};
+ GRANT SELECT, INSERT ON loopx_control_plane.authority_commits TO ${quotedRole(grantRole)};
+ GRANT SELECT, INSERT ON loopx_control_plane.authority_events,
+ loopx_control_plane.authority_receipts TO ${quotedRole(role)};
+ `);
+ const tenantId = `tenant-${randomUUID()}`;
+ const goalId = `goal-${randomUUID()}`;
+ const store = new PostgreSqlAuthorityStore(databaseForRuntimeRole(legacy, role), {tenant_id: tenantId, goal_id: goalId});
+ const projection = (counter: number) => ({goal_id: goalId, counter});
+ const scope = [tenantId, goalId];
+ await legacy.query(
+ `INSERT INTO loopx_control_plane.authority_heads (tenant_id, goal_id, provider_revision, cursor, head)
+ VALUES ($1, $2, 2, 2, $3)`, [...scope, projection(2)]);
+ for (const cursor of [1, 2]) {
+ await legacy.query(
+ `INSERT INTO loopx_control_plane.authority_commits
+ (tenant_id, goal_id, cursor, provider_revision, operation_id, projection)
+ VALUES ($1, $2, $3, $3, $4, $5)`, [...scope, cursor, `legacy-${cursor}`, projection(cursor)]);
+ }
+ await legacy.query(
+ `INSERT INTO loopx_control_plane.authority_receipts (tenant_id, goal_id, cursor, receipt_index, receipt)
+ VALUES ($1, $2, 2, 0, $3)`, [...scope, {before_upgrade: true}]);
+ const upgraded = await setupPostgreSqlAuthoritySchemaAndRole(databaseFromPool(legacy), {storeIdentity: STORE_IDENTITY});
+ assert.equal(upgraded.status, "installed", upgraded.reason);
+ const original = await store.readReceipt("legacy-2");
+ assert.equal(original.status, "found");
+ if (original.status !== "found") assert.fail(JSON.stringify(original));
+ assert.equal(original.cursor, "2");
+ assert.equal(original.provider_revision, `${STORE_IDENTITY}:2`);
+ assert.deepEqual(original.receipts, [{before_upgrade: true}]);
+ await queryAsRuntimeRole(legacy, role, async (client) => {
+ const result = await client.query(`
+ SELECT
+ has_table_privilege('loopx_control_plane.authority_checkpoints', 'SELECT') AS can_read,
+ has_table_privilege('loopx_control_plane.authority_checkpoints', 'INSERT') AS can_append,
+ has_table_privilege('loopx_control_plane.authority_checkpoints', 'UPDATE,DELETE') AS can_mutate
+ `);
+ assert.deepEqual(result.rows, [{can_read: true, can_append: true, can_mutate: false}]);
+ });
+ let revision = `${STORE_IDENTITY}:2`;
+ for (const cursor of [3, 4, 5]) {
+ const result = await store.commitAuthority({operation_id: `new-${cursor}`,
+ expected_provider_revision: revision, next_projection: projection(cursor),
+ events: [], receipts: [{after_upgrade: cursor}]});
+ if (result.status !== "applied") assert.fail(JSON.stringify(result));
+ revision = result.provider_revision;
+ }
+ assert.deepEqual(await store.readReceipt("legacy-2"), original);
+ const page = await store.scanCommitted("1", 3);
+ if (page.status !== "page") assert.fail(JSON.stringify(page));
+ assert.deepEqual(page.transactions.map(row => row.projection), [2, 3, 4].map(projection));
+
+ const tamper = {schema_version: "authority_state_delta_v0", operations: [{op: "set", path: ["counter"], value: 999}]};
+ await legacy.query(
+ `UPDATE loopx_control_plane.authority_commits SET delta=$3
+ WHERE tenant_id=$1 AND goal_id=$2 AND cursor=3`, [...scope, tamper]);
+ const corrupt = await store.scanCommitted("3", 1);
+ assert.equal(corrupt.status, "failed");
+ assert.match("reason" in corrupt ? corrupt.reason : "", /digest mismatch/);
+ await legacy.query(
+ "DELETE FROM loopx_control_plane.authority_commits WHERE tenant_id=$1 AND goal_id=$2 AND cursor=3", scope);
+ const missing = await store.scanCommitted("3", 1);
+ assert.equal(missing.status, "failed");
+ assert.match("reason" in missing ? missing.reason : "", /window is incomplete/);
+ });
} else {
test("PostgreSQL authority-store integration (set LOOPX_TEST_POSTGRES_URL)", { skip: true }, () => {});
}
@@ -836,7 +975,7 @@ test("PostgreSQL provider evicts and awaits cleanup-uncertain read connections",
}
if (text.includes("authority_store_metadata")) {
return {
- rows: [{ schema_version: "loopx_postgresql_authority_store_v0", store_identity: STORE_IDENTITY }],
+ rows: [{ schema_version: "loopx_postgresql_authority_store_v1", store_identity: STORE_IDENTITY }],
rowCount: 1,
};
}
diff --git a/tests/control_plane_ts/quota_spend_commit.test.ts b/tests/control_plane_ts/quota_spend_commit.test.ts
index 553c25ed95..dac67a0493 100644
--- a/tests/control_plane_ts/quota_spend_commit.test.ts
+++ b/tests/control_plane_ts/quota_spend_commit.test.ts
@@ -23,6 +23,7 @@ import {
EffectRuntimeRequestError,
} from "../../loopx/control_plane/effect_runtime_errors.ts";
import { requireJsonObject } from "../../loopx/control_plane/runtime_decode.ts";
+import { settlementIdentityPayload } from "../../loopx/control_plane/effect_program.ts";
import {
parseQuotaAccountingOwner,
withQuotaAccountingOwner,
@@ -1087,6 +1088,52 @@ test("effect identity and index CAS reject drift and racing writers", async (t)
assert.equal(fresh.status, "written");
});
+test("concurrent settlement retries share one debit across quota observation changes", async (t) => {
+ const runtimeRoot = await tempRuntime(t);
+ const identity = settlementIdentityPayload({
+ goal_id: goalId, agent_id: "codex-main-control", todo_id: "todo_original",
+ turn_instance_id: "turn-original",
+ });
+ const stable = {
+ settlement_identity: identity,
+ turn_instance_id: identity.turn_instance_id,
+ todo_id: identity.todo_id,
+ delivery_completion_spend: true,
+ delivery_run_generated_at: "2026-08-25T11:59:00+08:00",
+ delivery_run_classification: "validated_progress",
+ delivery_run_agent_id: identity.agent_id,
+ delivery_workspace: {workspace_identity: "git:github.com/example/fixture"},
+ delivery_workspace_validated: true,
+ };
+ const spendRequest = (spentSlots: number, slots = 1) => request(runtimeRoot, {
+ effect_id: null,
+ preview: preview({...stable, slots, would_throttle: spentSlots > 0}),
+ before: decision(spentSlots),
+ after: decision(spentSlots + slots),
+ });
+ const outcomes = await Promise.all([
+ evaluateQuotaSpendCommit(spendRequest(0)),
+ evaluateQuotaSpendCommit(spendRequest(1)),
+ ]);
+ assert.deepEqual(outcomes.map((result) => result.status).sort(), ["replayed", "written"]);
+ const committed = outcomes.find((result) => result.written)!;
+ const indexPath = String(committed.payload.index_path);
+ const originalRows = await readFile(indexPath, "utf8");
+ assert.equal(originalRows.trim().split("\n").length, 1);
+ assert.deepEqual(outcomes[0]?.record, outcomes[1]?.record);
+
+ const changedSpend = await evaluateQuotaSpendCommit(spendRequest(1, 2));
+ assert.equal(changedSpend.reason_code, "effect_id_conflict");
+ const changedSource = await evaluateQuotaSpendCommit({...spendRequest(1), source: "visible-goal"});
+ assert.equal(changedSource.reason_code, "effect_id_conflict");
+ const replacedWriteback = await evaluateQuotaSpendCommit({
+ ...spendRequest(1),
+ preview: preview({...stable, delivery_run_generated_at: "2026-08-25T11:58:00+08:00"}),
+ });
+ assert.equal(replacedWriteback.reason_code, "effect_id_conflict");
+ assert.equal(await readFile(indexPath, "utf8"), originalRows);
+});
+
test("concurrent exact-effect retries serialize to one spend", async (t) => {
const runtimeRoot = await tempRuntime(t);
const params = request(runtimeRoot);
diff --git a/tests/control_plane_ts/remote_authority.integration.test.ts b/tests/control_plane_ts/remote_authority.integration.test.ts
index 7d864c37b0..23c1cf5205 100644
--- a/tests/control_plane_ts/remote_authority.integration.test.ts
+++ b/tests/control_plane_ts/remote_authority.integration.test.ts
@@ -2,7 +2,10 @@ import assert from "node:assert/strict";
import test from "node:test";
import {createServer, request as httpsRequest} from "node:https";
import type {AddressInfo} from "node:net";
-import {writeFile} from "node:fs/promises";
+import {mkdir, writeFile} from "node:fs/promises";
+import {join} from "node:path";
+import {execFile} from "node:child_process";
+import {promisify} from "node:util";
import {Client} from "pg";
import type {JsonObject} from "../../loopx/control_plane/effect_program.ts";
import {RemoteAuthorityService} from "../../loopx/control_plane/coordination/remote_authority_service.ts";
@@ -12,12 +15,86 @@ import {PostgreSqlAuthorityStore, rotatePostgreSqlAuthorityStoreIdentity, type P
import {remoteAuthorityOperationId, type RemoteAuthorityRequest} from "../../loopx/control_plane/coordination/remote_authority_contract.ts";
import {canonicalAuthoritySha256} from "../../loopx/control_plane/coordination/authority_store_codec.ts";
import {postgresRemoteFixture, claimRequest, credentials, serviceIdentity, servicePolicy} from "./remote_authority_fixture.ts";
+import {openLocalAuthorityStore} from "../../loopx/control_plane/coordination/local_authority_provider.ts";
+import {engageLegacyCoordinationWriterFence} from "../../loopx/control_plane/coordination/legacy_writer_fence.ts";
+import {authorityProjectionFixture} from "./authority_projection_fixture.ts";
+import {resolveTestPython} from "../../scripts/test-python.mjs";
import {registerRemoteAuthorityModeConformance} from "./remote_authority_mode_conformance.ts";
import {registerRemoteAuthorityLifecycleConformance} from "./remote_authority_lifecycle_conformance.ts";
const connectionString = process.env.LOOPX_TEST_POSTGRES_URL;
registerRemoteAuthorityModeConformance(connectionString);
registerRemoteAuthorityLifecycleConformance(connectionString);
+test("quota CLI verifies the exact remote binding and never grants local execution", {skip: !connectionString}, async t => {
+ const fixture = await postgresRemoteFixture(t, connectionString!);
+ await fixture.seed(["todo_gateway_probe"], {todo_gateway_probe: "private-synthetic-note"});
+ const initialHead = await fixture.store.loadAuthority();
+ const running = await fixture.start();
+ const python = resolveTestPython();
+ const invoke = promisify(execFile);
+ const workspace = join(fixture.root, "workspace"), runtime = join(fixture.root, "runtime");
+ const registry = join(workspace, "registry.json"), state = join(workspace, "STATE.md");
+ await mkdir(workspace);
+ await writeFile(state, "---\nstatus: active\nupdated_at: 2026-01-01T00:00:00Z\n---\n# Synthetic Goal\n");
+ // The candidate observes a remote authority alongside its established
+ // private host state. It does not auto-initialize or migrate a Goal.
+ const local = await openLocalAuthorityStore(runtime, "goal-one");
+ const localHead = authorityProjectionFixture("goal-one", [], [], "native", {handoff_mode: "soft_claim"});
+ const localSeed = await local.commitAuthority({operation_id: "gateway-local-fixture",
+ expected_provider_revision: null, next_projection: localHead, events: [], receipts: []});
+ assert.equal(localSeed.status, "applied");
+ if (localSeed.status !== "applied") throw new Error("local fixture did not initialize");
+ const fence = await engageLegacyCoordinationWriterFence({schema_version: "loopx_legacy_coordination_writer_fence_engage_request_v0",
+ runtime_root: runtime, goal_id: "goal-one", state_path: state, fence: {
+ schema_version: "loopx_legacy_coordination_writer_fence_v0", state: "engaged", goal_id: "goal-one",
+ fence_id: "gateway-local-fixture", source_version: "gateway-local-fixture",
+ source_projection_sha256: canonicalAuthoritySha256(localHead), expected_shadow_provider_revision: localSeed.provider_revision,
+ }});
+ assert.equal(fence.status, "applied");
+ const cfg = {enabled: true, endpoint: running.endpoint, actor_id: "actor-a",
+ tenant_id: "tenant-one", store_identity: serviceIdentity, probe_todo_id: "todo_gateway_probe", ca_file: fixture.certPath};
+ const env: NodeJS.ProcessEnv = {...process.env, LOOPX_CONTEXT_DELIVERY: "off", LOOPX_AUTHORITY_CREDENTIAL: credentials.a};
+ delete env.LOOPX_TEST_POSTGRES_URL;
+ const writeBinding = async (overrides: JsonObject = {}) => writeFile(registry, JSON.stringify({
+ schema_version: "0.1", common_runtime_root: runtime,
+ goals: [{id: "goal-one", status: "active", repo: workspace, state_file: "STATE.md",
+ coordination: {agent_model: "peer_v1", registered_agents: ["actor-a", "actor-b"]},
+ quota: {compute: 1}, remote_authority: {...cfg, ...overrides}}],
+ }));
+ const args = ["-m", "loopx.cli", "--registry", registry, "--runtime-root", runtime, "--format", "json"];
+ const scope = ["--goal-id", "goal-one", "--agent-id", "actor-a"];
+ async function check(expected: string, credential = credentials.a) {
+ const {stdout} = await invoke(python, [...args, "quota", "should-run", "--runtime-profile", "outer_controller", ...scope],
+ {env: {...env, LOOPX_AUTHORITY_CREDENTIAL: credential}, timeout: 25_000, maxBuffer: 1024 * 1024});
+ const decision = JSON.parse(stdout);
+ assert.equal(decision.remote_gateway?.status, expected, JSON.stringify({
+ reason: decision.reason, code: decision.reason_code, fields: Object.keys(decision),
+ }));
+ assert.equal(decision.effective_action, "blocked_wait");
+ assert.equal(decision.should_run, false);
+ assert.equal(decision.normal_delivery_allowed, false);
+ assert.equal(decision.interaction_contract.agent_channel.must_attempt, false);
+ assert.equal(decision.interaction_contract.agent_channel.delivery_allowed, false);
+ assert.equal(decision.interaction_contract.cli_channel.spend_allowed_now, false);
+ assert(!decision.selected_todo);
+ for (const value of [cfg.endpoint, cfg.tenant_id, cfg.store_identity, cfg.ca_file, credential, "private-synthetic-note"]) {
+ assert(!stdout.includes(value));
+ }
+ }
+ await writeBinding(); await check("verified");
+ await check("unavailable", credentials.b); // Valid credential, wrong actor authorization.
+ for (const binding of [{tenant_id: "wrong-tenant"}, {store_identity: `postgresql:${"f".repeat(32)}`},
+ {ca_file: null}, {probe_todo_id: "todo_missing"}, {actor_id: "actor-b"}]) {
+ await writeBinding(binding); await check("unavailable");
+ }
+ await writeBinding();
+ await fixture.writePolicy({...servicePolicy(), principals: []}); await check("unavailable");
+ await fixture.writePolicy(); await check("verified");
+ await running.stop(); await check("unavailable");
+ await fixture.start(running.port); await check("verified");
+ assert.deepEqual(await fixture.store.loadAuthority(), initialHead, "gateway observations never change the remote head");
+});
+
test("remote authority real TLS/PostgreSQL task journey and recovery", {skip: !connectionString}, async t => {
const fixture = await postgresRemoteFixture(t, connectionString!);
let running = await fixture.start();
diff --git a/tests/control_plane_ts/remote_authority.test.ts b/tests/control_plane_ts/remote_authority.test.ts
index c2583d70ac..e65dae3eb3 100644
--- a/tests/control_plane_ts/remote_authority.test.ts
+++ b/tests/control_plane_ts/remote_authority.test.ts
@@ -10,6 +10,7 @@ import {join} from "node:path";
import type {JsonObject} from "../../loopx/control_plane/effect_program.ts";
import {decodeRemoteAuthorityRequest, decodeRemoteAuthorityResponse, remoteAuthorityOperationId,
remoteAuthorityResponse} from "../../loopx/control_plane/coordination/remote_authority_contract.ts";
+import {canonicalAuthoritySha256} from "../../loopx/control_plane/coordination/authority_store_codec.ts";
import {decodeRemoteAuthorityPolicy, remoteAuthorityPolicyAdmission, readRemoteAuthorityPolicy} from "../../loopx/control_plane/coordination/remote_authority_policy.ts";
import {RemoteAuthorityService} from "../../loopx/control_plane/coordination/remote_authority_service.ts";
import {callRemoteAuthority, createRemoteAuthorityServer} from "../../loopx/control_plane/coordination/remote_authority_http.ts";
@@ -201,3 +202,174 @@ test("a body completed after shutdown starts cannot dispatch a new command", asy
await stopping;
assert.equal(admissions, 0);
});
+
+test("complete, block, defer, todo_add and snapshot request decoding validates typed lifecycle semantics", () => {
+ const base = {
+ schema_version: "loopx_remote_authority_request_v0" as const,
+ tenant_id: "tenant-one",
+ goal_id: "goal-one",
+ actor_id: "actor-a",
+ store_identity: `postgresql:${"c".repeat(32)}`,
+ todo_id: "todo_lifecycle_fixture",
+ };
+
+ // Complete
+ const completeReq = {
+ ...base,
+ operation: "complete" as const,
+ request_id: "req-complete-1",
+ claim_request_id: "req-claim-1",
+ evidence: "sha256:abcd verified delivery receipt",
+ note: "optional completion note",
+ };
+ assert.deepEqual(decodeRemoteAuthorityRequest(completeReq), completeReq);
+ assert.throws(() => decodeRemoteAuthorityRequest({...completeReq, evidence: ""}));
+ assert.throws(() => decodeRemoteAuthorityRequest({...completeReq, claim_request_id: ""}));
+ assert.throws(() => decodeRemoteAuthorityRequest({...completeReq, claim_request_id: 12}));
+ assert.throws(() => decodeRemoteAuthorityRequest({...completeReq, request_id: null}));
+
+ // Block
+ const blockReq = {
+ ...base,
+ operation: "block" as const,
+ request_id: "req-block-1",
+ reason: "waiting on upstream schema",
+ resume_when: "todo_done:todo_schema_fixture",
+ note: "paused temporarily",
+ };
+ assert.deepEqual(decodeRemoteAuthorityRequest(blockReq), blockReq);
+ assert.throws(() => decodeRemoteAuthorityRequest({...blockReq, reason: ""}));
+ assert.throws(() => decodeRemoteAuthorityRequest({...blockReq, resume_when: "schema_applied"}));
+
+ // Defer
+ const deferReq = {
+ ...base,
+ operation: "defer" as const,
+ request_id: "req-defer-1",
+ reason: "resuming next week",
+ resume_when: "resume_at:2026-11-20T00:00:00Z",
+ };
+ assert.deepEqual(decodeRemoteAuthorityRequest(deferReq), deferReq);
+ assert.throws(() => decodeRemoteAuthorityRequest({...deferReq, resume_when: ""}));
+
+ // Todo add
+ const addReq = {
+ ...base,
+ operation: "todo_add" as const,
+ request_id: "req-add-1",
+ text: "[P1] new remote task",
+ priority: "P1",
+ role: "agent" as const,
+ task_class: "advancement_task",
+ required_write_scopes: ["loopx/control_plane/**"],
+ };
+ assert.deepEqual(decodeRemoteAuthorityRequest(addReq), addReq);
+ assert.throws(() => decodeRemoteAuthorityRequest({...addReq, priority: "P9"}));
+ assert.throws(() => decodeRemoteAuthorityRequest({...addReq, role: "invalid"}));
+
+ // Snapshot (todo_id is optional)
+ const snapshotGoal = {
+ schema_version: "loopx_remote_authority_request_v0" as const,
+ tenant_id: "tenant-one",
+ goal_id: "goal-one",
+ actor_id: "actor-a",
+ store_identity: `postgresql:${"c".repeat(32)}`,
+ operation: "snapshot" as const,
+ };
+ assert.deepEqual(decodeRemoteAuthorityRequest(snapshotGoal), snapshotGoal);
+ const snapshotTodo = {...snapshotGoal, todo_id: "todo_lifecycle_fixture"};
+ assert.deepEqual(decodeRemoteAuthorityRequest(snapshotTodo), snapshotTodo);
+ assert.throws(() => decodeRemoteAuthorityRequest({...snapshotTodo, todo_id: false}));
+});
+
+test("policy admits complete, block, defer, todo_add and snapshot grants and rejects ungranted operations", () => {
+ const policy = servicePolicy();
+ const grant = policy.principals[0].grants[0];
+ grant.operations = ["read", "claim", "progress", "renew", "release", "complete", "block", "defer", "todo_add", "snapshot"];
+ const decodedPolicy = decodeRemoteAuthorityPolicy(policy);
+
+ const base = {
+ schema_version: "loopx_remote_authority_request_v0" as const,
+ tenant_id: "tenant-one",
+ goal_id: "goal-one",
+ actor_id: "actor-a",
+ store_identity: `postgresql:${"c".repeat(32)}`,
+ todo_id: "todo_one",
+ };
+
+ const completeReq = decodeRemoteAuthorityRequest({
+ ...base, operation: "complete", request_id: "req-comp", claim_request_id: "req-claim", evidence: "evidence-1",
+ });
+ const snapshotReq = decodeRemoteAuthorityRequest({
+ ...base, operation: "snapshot",
+ });
+
+ const admComplete = remoteAuthorityPolicyAdmission(decodedPolicy, credentials.a, completeReq);
+ assert.equal(admComplete.status, "allowed");
+
+ const admSnapshot = remoteAuthorityPolicyAdmission(decodedPolicy, credentials.a, snapshotReq);
+ assert.equal(admSnapshot.status, "allowed");
+
+ // Default-deny check: principal b does not have "complete" granted
+ policy.principals[1].grants[0].operations = ["read", "claim"];
+ const decodedPolicyWithRestrictedB = decodeRemoteAuthorityPolicy(policy);
+ assert.equal(policy.principals[1].grants[0].operations.includes("complete" as any), false);
+ const admDenied = remoteAuthorityPolicyAdmission(decodedPolicyWithRestrictedB, credentials.b, {
+ ...completeReq, actor_id: "actor-b",
+ });
+ assert.equal(admDenied.status, "rejected");
+ assert.equal(admDenied.reason_code, "remote_authority_scope_denied");
+});
+
+test("decodeRemoteAuthorityResponse validates snapshot and complete response payloads", () => {
+ const base = {
+ schema_version: "loopx_remote_authority_request_v0" as const,
+ tenant_id: "tenant-one",
+ goal_id: "goal-one",
+ actor_id: "actor-a",
+ store_identity: `postgresql:${"c".repeat(32)}`,
+ todo_id: "todo_one",
+ };
+ const snapshotReq = decodeRemoteAuthorityRequest({...base, operation: "snapshot"});
+ const snapResponse = (todos: JsonObject[], leases: JsonObject[] = []) => {
+ const facts = {provider_revision: `${base.store_identity}:10`, store_identity: base.store_identity,
+ goal_id: base.goal_id, todos, leases};
+ return remoteAuthorityResponse(snapshotReq, "execute", {status: "snapshot", ...facts,
+ receipt_sha256: canonicalAuthoritySha256({schema_version: "loopx_goal_snapshot_receipt_v0",
+ tenant_id: base.tenant_id, ...facts})});
+ };
+ const todo = {todo_id: "todo_one", status: "open", role: "agent"};
+ const valid = snapResponse([todo]);
+ assert.deepEqual(decodeRemoteAuthorityResponse(valid, snapshotReq, "execute"), valid);
+ assert.throws(() => decodeRemoteAuthorityResponse({...valid, receipt_sha256: "a".repeat(64)}, snapshotReq, "execute"));
+ assert.throws(() => decodeRemoteAuthorityResponse(snapResponse([{...todo, todo_id: "todo_wrong"}]), snapshotReq, "execute"));
+ assert.throws(() => decodeRemoteAuthorityResponse(snapResponse([todo, todo]), snapshotReq, "execute"));
+ const lease = {todo_id: "todo_one", goal_id: base.goal_id, owner: base.actor_id, status: "released",
+ version: 1, lease_epoch: 1};
+ assert.throws(() => decodeRemoteAuthorityResponse(snapResponse([todo], [{...lease, goal_id: "wrong-goal"}]), snapshotReq, "execute"));
+ assert.throws(() => decodeRemoteAuthorityResponse(snapResponse([], [lease]), snapshotReq, "execute"));
+ assert.throws(() => decodeRemoteAuthorityResponse(snapResponse([todo], [{...lease, idempotency_key: "must-not-leak"}]), snapshotReq, "execute"));
+ assert.throws(() => decodeRemoteAuthorityResponse(valid, snapshotReq, "recover"));
+
+ const completeReq = decodeRemoteAuthorityRequest({
+ ...base, operation: "complete", request_id: "req-c", claim_request_id: "req-cl", evidence: "proof",
+ });
+ const opId = remoteAuthorityOperationId(completeReq, "principal-a");
+ const compResponse = remoteAuthorityResponse(completeReq, "execute", {
+ status: "result",
+ operation_id: opId,
+ result: {
+ status: "applied",
+ changed: true,
+ completed_at: "2026-12-01T12:00:00Z",
+ completion_receipt_id: "receipt-1",
+ todo_id: "todo_one",
+ provider_revision: `${base.store_identity}:11`,
+ cursor: "11",
+ },
+ });
+ assert.deepEqual(decodeRemoteAuthorityResponse(compResponse, completeReq, "execute"), compResponse);
+ const incomplete = structuredClone(compResponse);
+ delete (incomplete.result as JsonObject).completion_receipt_id;
+ assert.throws(() => decodeRemoteAuthorityResponse(incomplete, completeReq, "execute"));
+});
diff --git a/tests/control_plane_ts/remote_authority_fixture.ts b/tests/control_plane_ts/remote_authority_fixture.ts
index 5b4a0e9b77..93125f5136 100644
--- a/tests/control_plane_ts/remote_authority_fixture.ts
+++ b/tests/control_plane_ts/remote_authority_fixture.ts
@@ -28,9 +28,9 @@ export function servicePolicy(identity = serviceIdentity) {
principals: ["a", "b"].map(actor => ({principal_id: `principal-${actor}`,
credential_sha256: createHash("sha256").update(credentials[actor as "a" | "b"]).digest("hex"),
grants: [{tenant_id: "tenant-one", goal_id: "goal-one", actor_id: `actor-${actor}`,
- operations: ["read", "claim", "progress", "renew", "release"]}]}))};
+ operations: ["read", "claim", "progress", "renew", "release", "complete", "block", "defer", "todo_add", "snapshot"]}]}))};
}
-export function claimRequest(actor: "a" | "b", todo = "todo_one", id = `claim-${todo}`): RemoteAuthorityRequest {
+export function claimRequest(actor: "a" | "b", todo = "todo_one", id = `claim-${todo}`): Extract {
return {schema_version: REMOTE_AUTHORITY_REQUEST, tenant_id: "tenant-one", goal_id: "goal-one",
actor_id: `actor-${actor}`, store_identity: serviceIdentity, todo_id: todo, operation: "claim", request_id: id};
}
diff --git a/tests/control_plane_ts/remote_authority_lifecycle_conformance.ts b/tests/control_plane_ts/remote_authority_lifecycle_conformance.ts
index 877c69313e..37f8271eac 100644
--- a/tests/control_plane_ts/remote_authority_lifecycle_conformance.ts
+++ b/tests/control_plane_ts/remote_authority_lifecycle_conformance.ts
@@ -2,9 +2,10 @@ import assert from "node:assert/strict";
import test from "node:test";
import type {JsonObject} from "../../loopx/control_plane/effect_program.ts";
import {prepareCoordinationProjectionCommit, type CoordinationProjectionMutation} from "../../loopx/control_plane/coordination/coordination_projection.ts";
+import {executeCoordinationTodoTerminalLifecycle} from "../../loopx/control_plane/coordination/todo_terminal_lifecycle.ts";
import {executeCanonicalTaskLeaseLifecycle} from "../../loopx/control_plane/coordination/task_lease_lifecycle.ts";
import type {RemoteAuthorityRequest} from "../../loopx/control_plane/coordination/remote_authority_contract.ts";
-import {postgresRemoteFixture, claimRequest} from "./remote_authority_fixture.ts";
+import {postgresRemoteFixture, claimRequest, serviceIdentity} from "./remote_authority_fixture.ts";
function lifecycle(todo: string, operation: "renew" | "release", version = 0): RemoteAuthorityRequest {
const {request_id: _, ...scope} = claimRequest("a", todo) as Extract;
@@ -89,4 +90,226 @@ export function registerRemoteAuthorityLifecycleConformance(connectionString: st
assert.equal((recovered.value.result as JsonObject).missing, true);
assert.deepEqual(await fixture.store.loadAuthority(), committed, "replay and recovery retain the original no-op receipt");
});
+
+ test("remote completion keeps a recorded successor available to the next actor", {skip: !connectionString}, async t => {
+ const fixture = await postgresRemoteFixture(t, connectionString!);
+ const running = await fixture.start();
+ await fixture.seed(["todo_remote", "todo_local", "todo_next_remote", "todo_next_local"]);
+ const seed = await fixture.store.loadAuthority();
+ assert.equal(seed.status, "loaded");
+ if (seed.status !== "loaded") return;
+ const links: Record = {
+ todo_remote: {successor_todo_ids: ["todo_next_remote"]},
+ todo_local: {successor_todo_ids: ["todo_next_local"]},
+ todo_next_remote: {resume_when: "todo_done:todo_remote"},
+ todo_next_local: {resume_when: "todo_done:todo_local"},
+ };
+ const mutations: CoordinationProjectionMutation[] = (seed.head.todos as JsonObject[]).map(todo => ({
+ kind: "todo_upsert", todo: {...todo, ...links[String(todo.todo_id)]},
+ }));
+ const linked = await fixture.store.commitAuthority(prepareCoordinationProjectionCommit({goal_id: "goal-one",
+ operation_id: "existing-successors", expected_provider_revision: seed.provider_revision, projection: seed.head, mutations}));
+ assert.equal(linked.status, "applied");
+
+ const client = (actor: "a" | "b", request: RemoteAuthorityRequest, recover = false) =>
+ fixture.client(running.endpoint, actor, request, recover);
+ const localClaim = await client("a", claimRequest("a", "todo_local"));
+ assert.equal(localClaim.code, 0);
+ const control = await executeCoordinationTodoTerminalLifecycle(fixture.store, {
+ goal_id: "goal-one", todo_id: "todo_local", command: "complete",
+ expected_role: "agent", actor_agent_id: "actor-a", registered_agents: ["actor-a", "actor-b"],
+ lifecycle_grants: [], authority_reason: null, decision_outcome: null,
+ operation_identity: {kind: "explicit", operation_id: "finish-local"},
+ claim_operation_id: String((localClaim.value.result as JsonObject).claim_operation_id),
+ lease_idempotency_key: null, lease_expected_version: null, allow_user_gate_auto_acquire: false,
+ requested_no_followup: false, requested_completion_turn_key: null, requested_completion_identity_source: null,
+ linked_successor_todo_ids: [], successor_intents: [], note: null, evidence: "verified fixture result",
+ reason: null, clear_claim: true, validation_declaration: null, validation_receipt: null,
+ completion_policy_request: null, dry_run: false, now: new Date(),
+ });
+ assert.equal(control.status, "applied");
+ assert.equal(control.completion_continuation, "successor");
+
+ const next = claimRequest("b", "todo_next_remote");
+ const pending = await client("b", next);
+ assert.equal(pending.code, 1);
+ assert.equal((pending.value.result as JsonObject).reason_code, "todo_dependency_pending");
+ const {request_id: claim_request_id, ...scope} = claimRequest("a", "todo_remote");
+ assert.equal((await client("a", {...scope, request_id: claim_request_id})).code, 0);
+ const command = {...scope, operation: "complete" as const, request_id: "finish-remote",
+ claim_request_id, evidence: "verified fixture result"};
+ const finished = await client("a", command);
+ assert.equal(finished.code, 0, JSON.stringify(finished.value));
+ assert.equal((finished.value.result as JsonObject).status, "applied");
+ const receipt = (finished.value.result as JsonObject).completion_receipt_id;
+
+ const committed = await fixture.store.loadAuthority();
+ assert.equal(committed.status, "loaded");
+ if (committed.status !== "loaded") return;
+ const todo = (committed.head.todos as JsonObject[]).find(row => row.todo_id === command.todo_id);
+ assert.equal(todo?.completion_continuation, control.completion_continuation);
+ assert.deepEqual(todo?.successor_todo_ids, [next.todo_id]);
+ assert.equal(todo?.completion_receipt_id, receipt);
+ const replay = await client("a", command);
+ assert.equal(replay.code, 0);
+ assert.equal((replay.value.result as JsonObject).status, "replayed");
+ const recovered = await client("a", command, true);
+ assert.equal(recovered.code, 0);
+ assert.equal(recovered.value.historical_only, true);
+ assert.equal((recovered.value.result as JsonObject).completion_receipt_id, receipt);
+ const changedIntent = await client("a", {...command, no_followup: true});
+ assert.equal(changedIntent.code, 1);
+ assert.equal(changedIntent.value.reason_code, "remote_authority_operation_mismatch");
+ assert.deepEqual(await fixture.store.loadAuthority(), committed);
+
+ const {request_id: _, ...reader} = claimRequest("b", command.todo_id);
+ const observed = await client("b", {...reader, operation: "read"});
+ assert.equal(observed.code, 0);
+ assert.equal((observed.value.todo as JsonObject).status, "done");
+ assert.equal((observed.value.lease as JsonObject).status, "released");
+ assert.equal((await client("b", next)).code, 0);
+ });
+
+ test("remote authority full lifecycle: snapshot, todo_add, complete, block, and defer", {skip: !connectionString}, async t => {
+ const fixture = await postgresRemoteFixture(t, connectionString!);
+ const running = await fixture.start();
+ await fixture.seed(["todo_for_complete", "todo_for_terminal", "todo_for_block", "todo_for_defer"]);
+
+ const baseScope = {
+ schema_version: "loopx_remote_authority_request_v0" as const,
+ tenant_id: "tenant-one",
+ goal_id: "goal-one",
+ actor_id: "actor-a",
+ store_identity: serviceIdentity,
+ };
+
+ // 1. Snapshot
+ const snap = await fixture.client(running.endpoint, "a", {
+ ...baseScope,
+ operation: "snapshot",
+ });
+ assert.equal(snap.code, 0);
+ assert.equal(snap.value.status, "snapshot");
+ assert(Array.isArray(snap.value.todos));
+ assert(Array.isArray(snap.value.leases));
+ assert.equal(typeof snap.value.receipt_sha256, "string");
+
+ // 2. Todo add
+ const added = await fixture.client(running.endpoint, "a", {
+ ...baseScope,
+ todo_id: "todo_created_remotely",
+ operation: "todo_add",
+ request_id: "req-add-remotely",
+ text: "[P1] remotely created task",
+ priority: "P1",
+ role: "agent",
+ });
+ assert.equal(added.code, 0);
+ assert.equal((added.value.result as JsonObject).status, "applied");
+ assert.equal((added.value.result as JsonObject).todo_id, "todo_created_remotely");
+
+ // 3. Claim -> Complete
+ const claimComp = await fixture.client(running.endpoint, "a", {
+ ...baseScope,
+ todo_id: "todo_for_complete",
+ operation: "claim",
+ request_id: "claim-for-comp",
+ });
+ assert.equal(claimComp.code, 0);
+ assert.equal((claimComp.value.result as JsonObject).status, "applied");
+
+ const comp = await fixture.client(running.endpoint, "a", {
+ ...baseScope,
+ todo_id: "todo_for_complete",
+ operation: "complete",
+ request_id: "complete-req-1",
+ claim_request_id: "claim-for-comp",
+ evidence: "sha256:1111 delivery validated",
+ });
+ assert.equal(comp.code, 0);
+ assert.equal((comp.value.result as JsonObject).status, "applied");
+ assert.equal((comp.value.result as JsonObject).completed, true);
+
+ const {request_id: terminalClaimId, ...terminalScope} = claimRequest("a", "todo_for_terminal");
+ assert.equal((await fixture.client(running.endpoint, "a", {...terminalScope, request_id: terminalClaimId})).code, 0);
+ const terminal = await fixture.client(running.endpoint, "a", {
+ ...terminalScope, operation: "complete", request_id: "finish-terminal", claim_request_id: terminalClaimId,
+ evidence: "verified fixture result", no_followup: true,
+ });
+ assert.equal(terminal.code, 0);
+ const completedHead = await fixture.store.loadAuthority();
+ assert.equal(completedHead.status, "loaded");
+ if (completedHead.status !== "loaded") return;
+ const completedTodos = completedHead.head.todos as JsonObject[];
+ assert.equal(completedTodos.find(row => row.todo_id === "todo_for_complete")?.completion_continuation, "active_goal");
+ assert.equal(completedTodos.find(row => row.todo_id === "todo_for_terminal")?.completion_continuation, "no_followup");
+
+ // 4. Claim -> Block
+ const claimBlock = await fixture.client(running.endpoint, "a", {
+ ...baseScope,
+ todo_id: "todo_for_block",
+ operation: "claim",
+ request_id: "claim-for-block",
+ });
+ assert.equal(claimBlock.code, 0);
+
+ const block = await fixture.client(running.endpoint, "a", {
+ ...baseScope,
+ todo_id: "todo_for_block",
+ operation: "block",
+ request_id: "block-req-1",
+ claim_request_id: "claim-for-block",
+ reason: "external dependency blocked",
+ });
+ assert.equal(block.code, 0);
+ assert.equal((block.value.result as JsonObject).status, "applied");
+
+ // 5. Claim -> Defer
+ const claimDefer = await fixture.client(running.endpoint, "a", {
+ ...baseScope,
+ todo_id: "todo_for_defer",
+ operation: "claim",
+ request_id: "claim-for-defer",
+ });
+ assert.equal(claimDefer.code, 0);
+
+ const defer = await fixture.client(running.endpoint, "a", {
+ ...baseScope,
+ todo_id: "todo_for_defer",
+ operation: "defer",
+ request_id: "defer-req-1",
+ claim_request_id: "claim-for-defer",
+ reason: "deferred until milestone",
+ resume_when: "resume_at:2026-12-01T00:00:00Z",
+ });
+ assert.equal(defer.code, 0);
+ assert.equal((defer.value.result as JsonObject).status, "applied");
+ });
+
+ test("snapshot is bounded, targeted, and read only across independent clients", {skip: !connectionString}, async t => {
+ const fixture = await postgresRemoteFixture(t, connectionString!);
+ const running = await fixture.start();
+ const ids = Array.from({length: 24}, (_, i) => `todo_snapshot_${String(i).padStart(2, "0")}`);
+ await fixture.seed(ids, Object.fromEntries(ids.map(id => [id, "x".repeat(3100)])));
+ const initialHead = await fixture.store.loadAuthority();
+ const {request_id: _, todo_id, ...base} = claimRequest("a", ids[0]);
+ const wide: RemoteAuthorityRequest = {...base, operation: "snapshot"};
+ const tooLarge = await fixture.client(running.endpoint, "a", wide);
+ assert.equal(tooLarge.code, 1);
+ assert.equal(tooLarge.value.status, "unavailable");
+ assert.equal(tooLarge.value.reason_code, "remote_authority_response_too_large");
+
+ const targeted: RemoteAuthorityRequest = {...wide, todo_id};
+ const first = await fixture.client(running.endpoint, "a", targeted);
+ const second = await fixture.client(running.endpoint, "b", {...targeted, actor_id: "actor-b"});
+ assert.equal(first.code, 0); assert.equal(second.code, 0);
+ assert.deepEqual((first.value.todos as JsonObject[]).map(todo => todo.todo_id), [todo_id]);
+ assert.equal(first.value.receipt_sha256, second.value.receipt_sha256);
+ const wrongGoal = await fixture.client(running.endpoint, "a", {...targeted, goal_id: "another-goal"});
+ assert.equal(wrongGoal.code, 1); assert.equal(wrongGoal.value.status, "rejected");
+ const query = await fixture.client(running.endpoint, "a", targeted, true);
+ assert.equal(query.code, 1); assert.equal(query.value.status, "rejected");
+ assert.deepEqual(await fixture.store.loadAuthority(), initialHead);
+ });
+
}
diff --git a/tests/control_plane_ts/remote_authority_mode_conformance.ts b/tests/control_plane_ts/remote_authority_mode_conformance.ts
index 3d9cebc6c9..ea5dc9b6ea 100644
--- a/tests/control_plane_ts/remote_authority_mode_conformance.ts
+++ b/tests/control_plane_ts/remote_authority_mode_conformance.ts
@@ -16,7 +16,7 @@ export function registerRemoteAuthorityModeConformance(connectionString: string
await t.test(`mode change ${timing} cannot commit a remote ${operation} under soft mode`, async child => {
const claim = claimRequest("a", `todo_${operation}_${timing.replaceAll("-", "_")}`);
const {request_id: _, ...scope} = claim as Extract;
- const request: RemoteAuthorityRequest = operation === "claim" ? claim
+ const request = operation === "claim" ? claim
: {...scope, operation, idempotency_key: "missing-lease-key", expected_version: 0};
await fixture.seed([request.todo_id]);
let switched = false, headReads = 0, commitAttempts = 0;
diff --git a/tests/control_plane_ts/replan_semantics.test.ts b/tests/control_plane_ts/replan_semantics.test.ts
index d1b226b408..73a0a76d37 100644
--- a/tests/control_plane_ts/replan_semantics.test.ts
+++ b/tests/control_plane_ts/replan_semantics.test.ts
@@ -661,6 +661,13 @@ test("a later durable exact ACK settles only its original receipt-bound Turn", (
assert.deepEqual(packet.semantic_delta, delta);
assert.equal((packet.writeback_contract as JsonObject).preferred_input, "exact_historical_ack");
+ const pending = projectReplanSemantics({operation: "receipt_bound_obligation", ...request,
+ current_obligation: null, replay_phase: "settlement_pending"}).obligation as JsonObject;
+ const remaining = pending.settlement_action_packet as JsonObject;
+ assert.equal(remaining.required_outcome, "quota_spend_receipt");
+ assert.equal(remaining.semantic_delta, null);
+ assert.equal(remaining.successor_todo_id, null);
+
for (const missingGuard of [{guard_scoped: false}, {goal_id: null}, {agent_id: null},
{guard_recorded_at: null}, {guard_turn_instance_id: null}]) {
const unbound = {...request, ...missingGuard};
@@ -794,6 +801,15 @@ test(`${recovery} restores only its exact admitted review and still owes Vision`
assert.equal(Object.hasOwn(writeback.vision_authoring as JsonObject, "continuation_review"), false);
assert.equal(projectReplanSemantics({operation: "qualify", obligation: result}).accepted, false);
assert.equal(projectReplanSemantics({operation: "qualify", obligation: result, agent_vision: vision}).accepted, true);
+ // The same lifecycle evidence still exists after writeback. Reentry follows
+ // the verified settlement phase while leaving the next frontier unselected.
+ const pending = projectReplanSemantics({...request, replay_phase: "settlement_pending",
+ current_obligation: {obligation_id: "replan-2222222222222222"}}).obligation as JsonObject;
+ assert.equal(pending.obligation_id, id);
+ assert.deepEqual(pending.todo_actions, []);
+ const remaining = pending.settlement_action_packet as JsonObject;
+ assert.equal(remaining.decision, "settlement_pending");
+ assert.equal(remaining.required_outcome, "quota_spend_receipt");
const historical = {...row, todo_id: "todo_historical", completion_turn_key: "historical-completion",
completed_at: "2026-07-31T23:59:00Z", updated_at: "2026-07-31T23:59:01Z"};
for (const items of [[historical, row], [row, historical]]) {
diff --git a/tests/control_plane_ts/state_recovery.test.ts b/tests/control_plane_ts/state_recovery.test.ts
index ff39eff2a7..1274105e61 100644
--- a/tests/control_plane_ts/state_recovery.test.ts
+++ b/tests/control_plane_ts/state_recovery.test.ts
@@ -69,12 +69,12 @@ test("captured runtime bindings are optional data and must name included inert r
}
});
-test("native capture metadata cannot change its Goal/configuration dependency or exceed the historical capacity", () => {
+test("native capture metadata binds its dependencies and accepts a large claimed history", () => {
const original = request();
const path = `${authorityBackupDirectory("goal-a")}/authority.jsonl`;
const capture = {goal_id: "goal-a", state: "readable", runtime_archive_path: "runtime-root",
configuration_sha256: "d".repeat(64), started_at: "2026-10-15T00:00:00.000Z", completed_at: "2026-10-15T00:00:01.000Z",
- archive_path: path, archive: {goal_id: "goal-a", commits: "1", archive_sha256: "e".repeat(64)},
+ archive_path: path, archive: {goal_id: "goal-a", commits: "20001", archive_sha256: "e".repeat(64)},
dependencies: {selector_sha256: null, writer_fence_sha256: null}};
const artifact = {...member, archive_path: path};
original.files = [artifact];
@@ -84,12 +84,13 @@ test("native capture metadata cannot change its Goal/configuration dependency or
Object.assign(manifest.execution, {authority_captures: [capture], file_members: [artifact],
configuration_backup: {sha256: "d".repeat(64)}});
}
- // The logical archive seal is deliberately different from the member byte
- // digest: each has its own verifier and must never be substituted for the other.
+ // This validates the manifest claim. The archive reader verifies the actual
+ // cursor and seal, independently of the member byte digest.
assert.equal(verifyStateRecoveryManifest(original).ok, true);
for (const delta of [{runtime_archive_path: null}, {runtime_archive_path: "other"},
{configuration_sha256: "f".repeat(64)}, {completed_at: "2026-10-14T00:00:00.000Z"},
- {archive_path: "../outside"}, {archive: {...capture.archive, commits: "20001"}}]) {
+ {archive_path: "../outside"}, {archive: {...capture.archive, commits: "0"}},
+ {archive: {...capture.archive, commits: "020001"}}, {archive: {...capture.archive, commits: 20001}}]) {
const input = structuredClone(original);
for (const manifest of [input.external_manifest, input.embedded_manifest]) {
Object.assign((manifest.execution as any).authority_captures[0], delta);
diff --git a/tests/control_plane_ts/unsettled_host_turn_recovery.test.ts b/tests/control_plane_ts/unsettled_host_turn_recovery.test.ts
index 8287c5fc62..e787055478 100644
--- a/tests/control_plane_ts/unsettled_host_turn_recovery.test.ts
+++ b/tests/control_plane_ts/unsettled_host_turn_recovery.test.ts
@@ -413,6 +413,27 @@ test("a receipt that does not opt in never becomes a recovery obligation", async
}
});
+test("an admitted Todo-bound replan without verified writeback does not hold a later guard", async () => {
+ const admitted = {
+ ...closeoutRequired("turn-replan", "todo_alpha"), closeout_required: false,
+ semantic_replan_obligation_id: `replan-${"b".repeat(16)}`,
+ ok: true, should_run: true, interaction_mode: "autonomous_replan",
+ must_attempt_work: true, delivery_allowed: false, quiet_noop_allowed: false,
+ };
+ const runtime = await runtimeWith([
+ receipt("turn-normal", closeoutRequired("turn-normal", "todo_beta")),
+ receipt("turn-replan", admitted),
+ ]);
+ const replanOnly = await runtimeWith([receipt("turn-replan", admitted)]);
+ try {
+ assert.equal(candidateFrom(await preflight(runtime.root)).prior_turn_instance_id, "turn-normal");
+ assert.equal((await preflight(replanOnly.root)).reason, "no_prior_turn_requires_closeout");
+ } finally {
+ await runtime.close();
+ await replanOnly.close();
+ }
+});
+
test("the current Turn, other goals, other agents, and other event kinds are ignored", async () => {
const runtime = await runtimeWith([
receipt("current-turn", closeoutRequired("current-turn", "todo_current")),
@@ -852,6 +873,17 @@ test("typed lifecycle closeouts are accepted and other open Todos are not", asyn
assert.equal(verdict.status, "none");
assert.equal(verdict.accepted_closeout, accepted);
}
+ const ordinaryPending = reduce(candidate, ["quota_spend_receipt"], READ_TODO(todo));
+ assert.equal(ordinaryPending.status, verdict.status);
+ assert.equal(ordinaryPending.accepted_closeout, verdict.accepted_closeout);
+
+ const replanPending = reduce(
+ {...candidate, admitted_todo_replan: true},
+ ["quota_spend_receipt"],
+ READ_TODO(todo),
+ );
+ assert.equal(replanPending.status, "recovery_required");
+ assert.equal((replanPending.recovery as JsonObject).repair, "resume_prior_turn");
}
} finally {
await runtime.close();
diff --git a/tests/extensions/test_lark_cli_resolution.py b/tests/extensions/test_lark_cli_resolution.py
index 85ef88ef55..0eef3e3b45 100644
--- a/tests/extensions/test_lark_cli_resolution.py
+++ b/tests/extensions/test_lark_cli_resolution.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import os
+from contextlib import contextmanager
from pathlib import Path
from typing import Any
@@ -9,6 +10,7 @@
LarkCliResolution,
build_lark_command_runner,
resolve_lark_cli,
+ run_lark_command_with_dispatch_guard,
)
@@ -192,6 +194,53 @@ def fake_run(args: list[str], **kwargs: Any) -> Any:
]
+def test_guarded_cli_spawns_inside_authority_and_waits_outside(tmp_path, monkeypatch):
+ import subprocess
+ import sys
+
+ real_popen = subprocess.Popen
+ states = []
+ held = False
+
+ @contextmanager
+ def guard():
+ nonlocal held
+ held = True
+ try:
+ yield
+ finally:
+ held = False
+
+ class ObservedProcess:
+ def __init__(self, *args, **kwargs):
+ states.append(("spawn", held))
+ self.process = real_popen(*args, **kwargs)
+
+ def __enter__(self):
+ self.process.__enter__()
+ return self
+
+ def __exit__(self, *args):
+ return self.process.__exit__(*args)
+
+ @property
+ def returncode(self):
+ return self.process.returncode
+
+ def communicate(self, *, timeout):
+ states.append(("wait", held))
+ return self.process.communicate(timeout=timeout)
+
+ monkeypatch.setattr(subprocess, "Popen", ObservedProcess)
+ resolution = LarkCliResolution(sys.executable, True, "fixture", None, None)
+ runner = build_lark_command_runner(resolution)
+ result = run_lark_command_with_dispatch_guard(
+ runner, [sys.executable, "-c", "print('result')"], tmp_path, 5, guard,
+ )
+ assert result["returncode"] == 0 and result["stdout"] == "result\n"
+ assert states == [("spawn", True), ("wait", False)]
+
+
def test_resolver_source_preserves_public_boundary_contract() -> None:
source = Path(__file__).resolve().parents[2] / "loopx/extensions/lark/cli_resolution.py"
diff --git a/tests/presentation/test_distribution_build.py b/tests/presentation/test_distribution_build.py
index acd0405c40..eb233f8957 100644
--- a/tests/presentation/test_distribution_build.py
+++ b/tests/presentation/test_distribution_build.py
@@ -13,6 +13,7 @@
import shutil
import subprocess
import sys
+import tomllib
import zipfile
import pytest
@@ -120,6 +121,30 @@ def wheel(source, output, *, keep_temp=False, skip_build=False):
}
+def test_manager_discovery_projection_ships_with_its_skill(checkout, tmp_path):
+ """The installed lazy handler must have its original typed dependency."""
+ package = "loopx.capabilities.manager_context"
+ data = tomllib.loads((ROOT / "pyproject.toml").read_text())["tool"]["setuptools"]["package-data"]
+ with (checkout / "pyproject.toml").open("a") as configuration:
+ configuration.write(f'"{package}"={json.dumps(data[package])}\n')
+ for name in ("loopx/capabilities", "loopx/capabilities/manager_context"):
+ directory = checkout / name
+ directory.mkdir(parents=True, exist_ok=True)
+ (directory / "__init__.py").write_text("", encoding="utf-8")
+ resources = (
+ "loopx/capabilities/manager_context/discovery_projection.ts",
+ "loopx/capabilities/manager_context/skills/loopx-manager/SKILL.md",
+ )
+ for name in resources:
+ target = checkout / name
+ target.parent.mkdir(parents=True, exist_ok=True)
+ shutil.copyfile(ROOT / name, target)
+ packaged = wheel(checkout, tmp_path / "manager-wheel")
+ for name in resources:
+ assert name in packaged, f"installed manager discovery dependency missing: {name}"
+ assert packaged[name] == (ROOT / name).read_bytes()
+
+
@pytest.mark.parametrize("keep_temp", [False, True])
def test_reused_build_removes_deleted_sources_and_keeps_current_resources(
checkout, tmp_path, keep_temp
diff --git a/tests/test_agy_host_surface.py b/tests/test_agy_host_surface.py
index 54599097c7..e7e2a1e44a 100644
--- a/tests/test_agy_host_surface.py
+++ b/tests/test_agy_host_surface.py
@@ -9,12 +9,16 @@
from __future__ import annotations
+import json
import os
from pathlib import Path
+import shlex
import pytest
from host_surface_cli_probes import (
+ connected_project,
onboarding_setup_command_installs,
+ run_cli,
selection_gate_offers_surface,
start_goal_accepts_surface,
)
@@ -52,6 +56,61 @@
HOST_SURFACE = "agy"
+@pytest.mark.parametrize(
+ "entry", [
+ ("bootstrap-command-pack", "--host-surface", "antigravity-app"),
+ ("agent-onboard", "--agent-type", "antigravity-app"),
+ ],
+)
+def test_antigravity_generated_commands_read_the_project_registry_from_another_directory(
+ tmp_path: Path, entry: tuple[str, ...],
+) -> None:
+ project = connected_project(tmp_path / "project with spaces")
+ registry = project / ".loopx" / "registry.json"
+ runtime = tmp_path / "isolated-runtime"
+ state = project / "ACTIVE_GOAL_STATE.md"
+ state.write_text("# Goal\n\nUse the registered project state.\n", encoding="utf-8")
+ data = json.loads(registry.read_text())
+ data["common_runtime_root"] = str(runtime)
+ data["goals"][0].update(
+ state_file=state.name,
+ coordination={"registered_agents": ["probe-agent"], "agent_model": "peer_v1"},
+ )
+ registry.write_text(json.dumps(data), encoding="utf-8")
+ foreign = tmp_path / "other"
+ foreign.mkdir()
+ # The host may be in another project which happens to use the same Goal id.
+ other = connected_project(foreign)
+ foreign_registry = other / ".loopx" / "registry.json"
+ foreign_registry.write_text(json.dumps({
+ "goals": [{**data["goals"][0], "repo": str(other)}],
+ }), encoding="utf-8")
+ env = {**os.environ, "LOOPX_RUNTIME_ROOT": str(runtime), "LOOPX_USAGE_PING": "0"}
+ env.pop("LOOPX_REGISTRY", None)
+ result = run_cli(
+ *entry, "--project", str(project), "--goal-id", "surface-goal",
+ "--agent-id", "probe-agent", cwd=other, env=env,
+ )
+ assert result.returncode == 0, result.stderr
+ activation = json.loads(result.stdout)["host_loop_activation"]
+ assert activation["activation_allowed"]
+ mutation = activation["host_mutation"]
+ sidecar = mutation["sidecar_template"]
+ prompt = sidecar["sidecar_json"]["args"][-1]
+ bootstrap, = [line for line in prompt.splitlines() if " heartbeat-prompt " in line]
+ for command in (bootstrap, activation["activation_input_command"]):
+ argv = shlex.split(command)
+ result = run_cli(*argv[1:], cwd=other, env=env)
+ assert result.returncode == 0, result.stdout + result.stderr
+ payload = json.loads(result.stdout)
+ assert payload["ok"]
+ guard = next(line for line in payload["task_body"].splitlines() if " quota should-run " in line)
+ guard_argv = shlex.split(guard)
+ assert Path(guard_argv[guard_argv.index("--registry") + 1]) == registry
+ hook = shlex.split(mutation["hooks_template"]["loopx"]["Stop"][0]["command"])
+ assert hook[hook.index("--registry") + 1] == str(registry)
+
+
def test_start_goal_accepts_the_agy_host_surface(tmp_path: Path) -> None:
payload = start_goal_accepts_surface(HOST_SURFACE, tmp_path)
activation = payload["command_pack"]["host_loop_activation"]
diff --git a/tests/test_cli_entrypoint.py b/tests/test_cli_entrypoint.py
index 8f6cd1092e..e71e2a9a04 100644
--- a/tests/test_cli_entrypoint.py
+++ b/tests/test_cli_entrypoint.py
@@ -661,7 +661,8 @@ def test_heartbeat_chain_routes_do_not_load_the_chat_layer(tmp_path, monkeypatch
initialize_canonical_source_fixture(runtime, "perf-goal", state_path=state, provider="sqlite")
argv = ["--registry", str(registry), "--runtime-root", str(runtime), "--format", "json", *command]
result = run_cli_main("loopx.entrypoint", argv,
- forbidden_modules=("loopx.cli", "loopx.chat_manager", "loopx.chat_agent", "loopx.chat_store"))
+ forbidden_modules=("loopx.cli", "loopx.chat_manager", "loopx.chat_agent", "loopx.chat_store",
+ "loopx.capabilities.issue_fix.pr_wait_reconcile"))
assert result.returncode == 0, result.stderr
assert json.loads(result.stdout)["ok"] is True
@@ -700,6 +701,10 @@ def test_capability_hook_imports_defer_unselected_facade_exports() -> None:
"issue_fix.periodic_report",
"issue_fix.pr_description",
"issue_fix.reward_memory",
+ "issue_fix.pr_wait_reconcile",
+ "issue_fix.pr_lifecycle",
+ "issue_fix.metadata_preview",
+ "content_ops.surface",
"reward_memory.codex_app_outcome",
"reward_memory.dogfood",
"reward_memory.evaluation",
@@ -719,6 +724,75 @@ def test_capability_hook_imports_defer_unselected_facade_exports() -> None:
assert completed.returncode == 0, completed.stderr
+@pytest.mark.parametrize("permission", ["denied", "revoked", "source-unavailable", "recheck-unavailable", "granted"])
+def test_pr_wait_provider_import_obeys_permission_and_failure_owner(permission):
+ script = f"""
+import importlib.abc
+import sys
+from pathlib import Path
+
+attempts = []
+class UnavailableProvider(importlib.abc.MetaPathFinder):
+ def find_spec(self, fullname, path=None, target=None):
+ if fullname == "loopx.capabilities.issue_fix.pr_wait_reconcile":
+ attempts.append(fullname)
+ raise ImportError("optional observation provider unavailable")
+
+sys.meta_path.insert(0, UnavailableProvider())
+from loopx.capabilities.issue_fix import pr_wait_hook
+
+permission = {permission!r}
+grant = ["external_evidence_poll"]
+states = iter({{
+ "denied": [[]], "revoked": [grant, []],
+ "source-unavailable": [OSError("permission source unavailable")] * 2,
+ "recheck-unavailable": [grant, OSError("permission source unavailable")],
+ "granted": [grant, grant],
+}}[permission])
+checks = []
+def availability(*args):
+ value = next(states)
+ checks.append(value)
+ if isinstance(value, Exception):
+ raise value
+ return {{"effective": value}}
+
+pr_wait_hook._availability = availability
+hook = pr_wait_hook.turn_start_hook(registry_path=Path("unused"), runtime_root=Path("unused"),
+ goal_id="fixture", agent_id="fixture", available=None)
+assert attempts == [], attempts
+if permission == "denied":
+ assert hook is None and len(checks) == 1
+else:
+ assert hook is not None
+ result = hook.producer()
+ assert len(checks) == 2
+ assert result["external_writes_performed"] is False
+ if permission == "granted":
+ assert len(attempts) == 1
+ assert result["status"] == "failed"
+ assert result["error_code"] == "dependency_observation_failed"
+ assert result["external_reads_performed"] is None
+ assert result["local_private_state_mutated"] is None
+ assert result["readback"]["external_read_count"] is None
+ assert result["readback"]["state_written"] is None
+ assert result["readback"]["error_category"] == "ImportError"
+ else:
+ assert attempts == []
+ assert result["external_reads_performed"] is False
+ assert result["local_private_state_mutated"] is False
+ assert result["status"] == ("not_applicable" if permission == "revoked" else "unavailable")
+ if permission != "revoked":
+ assert result["error_code"] == "capability_source_unavailable"
+ assert result["readback"]["external_read_count"] == 0
+for name in ("pr_wait_reconcile", "pr_lifecycle", "metadata_preview"):
+ assert "loopx.capabilities.issue_fix." + name not in sys.modules
+assert "loopx.capabilities.content_ops.surface" not in sys.modules
+"""
+ completed = run_isolated_script(script)
+ assert completed.returncode == 0, completed.stderr
+
+
def test_quota_facade_lazy_exports_preserve_owner_identity() -> None:
script = """
from importlib import import_module
diff --git a/tests/test_core_distribution.py b/tests/test_core_distribution.py
index fac7542fd6..9938c4f9e4 100644
--- a/tests/test_core_distribution.py
+++ b/tests/test_core_distribution.py
@@ -67,12 +67,36 @@ def test_core_staging_excludes_workspace_and_reuses_kernel_bytes(tmp_path):
@pytest.fixture
-def core_python():
+def core_python(tmp_path):
value = os.environ.get("LOOPX_CORE_TEST_PYTHON")
if not value:
pytest.skip("set LOOPX_CORE_TEST_PYTHON to a clean installed core environment")
executable = Path(value).absolute()
assert executable.is_file(), executable
+ # Every selected qualification caller must use the installed Core owner,
+ # including when the separate identity test was not selected.
+ script = '''import importlib.metadata as m,json,loopx
+from pathlib import Path
+installed=m.distribution('loopx-core')
+assert installed.version==loopx.__version__
+try:m.distribution('loopx')
+except m.PackageNotFoundError:pass
+else:raise AssertionError('the ordinary distribution must not be installed')
+direct_url=json.loads(installed.read_text('direct_url.json') or '{}')
+assert not direct_url.get('dir_info',{}).get('editable',False),'editable Core is not installed qualification'
+recorded=next((entry for entry in installed.files or () if entry.as_posix()=='loopx/__init__.py'),None)
+assert recorded is not None,'the installed Core RECORD must own loopx/__init__.py'
+package=Path(loopx.__file__).resolve()
+record_path=Path(installed.locate_file(recorded)).resolve()
+assert record_path==package,('imported namespace is not owned by installed Core',str(package),str(record_path))
+assert package.is_relative_to(Path(installed.locate_file('')).resolve()),'Core namespace resolves outside its installed location'
+'''
+ probe = subprocess.run([str(executable), "-I", "-c", script], cwd=tmp_path,
+ env=_environment(str(executable)), capture_output=True, text=True, timeout=30)
+ assert probe.returncode == 0, (
+ "LOOPX_CORE_TEST_PYTHON must select a clean installed loopx-core distribution",
+ probe.stdout, probe.stderr,
+ )
return str(executable)
@@ -104,12 +128,8 @@ def _run(python, arguments, cwd, *, environment=None, expected=0):
def test_installed_core_identity_help_and_unavailable_workspace(core_python, tmp_path):
- script = '''import importlib.metadata as m,importlib.util as u,json,loopx
+ script = '''import importlib.util as u,json,loopx
from pathlib import Path
-assert m.version('loopx-core')==loopx.__version__
-try:m.distribution('loopx')
-except m.PackageNotFoundError:pass
-else:raise AssertionError('the ordinary distribution must not be installed')
for name in ('loopx.cli','loopx.chat','loopx.web','loopx.canary'):assert u.find_spec(name) is None,name
from loopx.release_manifest import _distribution_runtime_fingerprint
assert _distribution_runtime_fingerprint() is not None
diff --git a/tests/test_lark_private_conversations.py b/tests/test_lark_private_conversations.py
index 339dc618ad..1c9d0c59bf 100644
--- a/tests/test_lark_private_conversations.py
+++ b/tests/test_lark_private_conversations.py
@@ -7,6 +7,7 @@
import threading
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
+from io import BytesIO
from types import SimpleNamespace
import pytest
@@ -1016,6 +1017,679 @@ def connect(fixture):
runner=provider, cli_bin="lark-cli")
+@pytest.fixture
+def source_https(monkeypatch):
+ from loopx.extensions import public_source_reader
+
+ requests, resolutions = [], []
+
+ def resolve(host):
+ resolutions.append(host)
+ return ["8.8.8.8"]
+
+ def serve(response_for):
+ class Socket:
+ def settimeout(self, value):
+ pass
+
+ def makefile(self, *args):
+ return BytesIO(self.wire_response)
+
+ class Connection:
+ def __init__(self, host, address, timeout):
+ self.host = host
+ self.sock = Socket()
+
+ def request(self, method, path, *, headers):
+ assert method == "GET"
+ assert set(headers) == {"Accept-Encoding", "User-Agent"}
+ url = f"https://{self.host}{path}"
+ requests.append(url)
+ redirect, mime, body = response_for(url)
+ if redirect:
+ self.sock.wire_response = f"HTTP/1.1 302 Found\r\nLocation: {redirect}\r\nContent-Length: 0\r\n\r\n".encode()
+ else:
+ headers = f"HTTP/1.1 200 OK\r\nContent-Type: {mime}\r\nContent-Length: {len(body)}\r\n\r\n"
+ self.sock.wire_response = headers.encode() + body
+
+ def getresponse(self):
+ response = http.client.HTTPResponse(self.sock)
+ response.begin()
+ return response
+
+ def close(self):
+ pass
+
+ monkeypatch.setattr(public_source_reader, "public_addresses", resolve)
+ monkeypatch.setattr(public_source_reader, "_PinnedHTTPS", Connection)
+ return requests, resolutions
+
+ return serve
+
+
+@pytest.mark.parametrize("fault", ["none", "foreign_source", "unlisted_image", "changed_request",
+ "corrupt_readback", "legacy_cached_readback", "upload_denied", "listed_redirect", "same_host_redirect",
+ "foreign_redirect", "foreign_round_trip", "query_redirect", "article_redirect", "stopped", "stopped_after_fetch",
+ "closed_on_upload_preview", "stopped_on_upload_source_read", "closed_on_upload_source_read",
+ "closed_on_source_controls_read", "stopped_before_upload_intent", "closed_before_upload_intent",
+ "edited_after_upload_intent"])
+def test_private_completed_answer_returns_image_bytes_instead_of_a_remote_placeholder(ordinary, source_https, monkeypatch, fault): # noqa: F811
+ from test_chat_image_attachments import PNG_BYTES
+ from test_lark_app_image_projection import ImageIO
+
+ store, runtime, provider, transport = connect(ordinary)
+ capture, fake = ordinary[4:6]
+ url = ("https://unrelated.example" if fault == "foreign_source" else "https://source.example") + "/chart.png"
+ authored = f'Original explanation.\n\n'
+ script = fake.read_text()
+ default = '"message": "Runtime response.",'
+ assert default in script
+ fake.write_text(script.replace(default,
+ f'"message": {authored!r} if turn_count == 1 else "Next original reply.",', 1))
+ article_url = "https://source.example/article"
+ stopped = fault in ("stopped", "stopped_after_fetch", "stopped_on_upload_source_read", "stopped_before_upload_intent")
+ closed = fault in ("closed_on_upload_preview", "closed_on_upload_source_read", "closed_on_source_controls_read",
+ "closed_before_upload_intent")
+ redirect_targets = {
+ "listed_redirect": "/canonical.png",
+ "same_host_redirect": "/unlisted.png",
+ "foreign_redirect": "https://unrelated.example/foreign.png",
+ "foreign_round_trip": "https://unrelated.example/foreign.png",
+ "query_redirect": "/chart.png?private=fixture",
+ }
+
+ def respond(target):
+ if target == article_url and fault == "article_redirect":
+ return "/other-article", None, b""
+ if target in (article_url, "https://source.example/other-article"):
+ basename = "different-chart.png" if fault == "unlisted_image" else "chart.png"
+ listings = f'
'
+ if fault == "listed_redirect":
+ listings += '
'
+ return None, "text/html", listings.encode()
+ if target == url and fault in redirect_targets:
+ return redirect_targets[fault], None, b""
+ if target == "https://unrelated.example/foreign.png" and fault == "foreign_round_trip":
+ return "https://source.example/unlisted.png", None, b""
+ if target == url and fault == "stopped_after_fetch":
+ transport.admit("notes-app", provider.event("notes-app", "stop-during-image-fetch", "/stop"))
+ return None, "image/png", PNG_BYTES
+
+ fetched, resolutions = source_https(respond)
+ images_delivered = fault in ("none", "listed_redirect")
+ io = ImageIO(provider)
+ io.corrupt_download = fault in ("corrupt_readback", "legacy_cached_readback")
+ upload_previewed = False
+ control_committed = False
+ source_edited_after_intent = False
+ if fault == "edited_after_upload_intent":
+ from loopx.extensions.lark import return_files
+
+ write_receipt = return_files._write
+
+ def edit_after_upload_intent(path, value):
+ nonlocal source_edited_after_intent
+ write_receipt(path, value)
+ if value.get("upload_started") and not source_edited_after_intent:
+ assert json.loads(path.read_text())["upload_started"] is True
+ provider.messages[source["message_id"]]["content"] = "The source changed after upload intent."
+ source_edited_after_intent = True
+
+ monkeypatch.setattr(return_files, "_write", edit_after_upload_intent)
+
+ if fault in ("stopped_before_upload_intent", "closed_before_upload_intent"):
+ from loopx.extensions.lark import return_files
+ write_receipt = return_files._write
+
+ def control_before_upload_intent(path, value):
+ nonlocal control_committed
+ if value.get("upload_started") and not control_committed:
+ control_committed = True
+ if stopped:
+ transport.admit("notes-app", provider.event("notes-app", "stop-before-resource-intent", "/stop"))
+ assert any(row.get("command") == "stop" for row in transport.core.source_controls())
+ else:
+ assert runtime.close_session(sid)
+ assert store.load_session(sid)["status"] == "closed"
+ return write_receipt(path, value)
+
+ monkeypatch.setattr(return_files, "_write", control_before_upload_intent)
+
+ if fault == "closed_on_source_controls_read":
+ read_controls = transport.core.source_controls
+
+ def close_during_control_read():
+ nonlocal control_committed
+ if upload_previewed and not control_committed:
+ control_committed = True
+ assert runtime.close_session(sid)
+ assert store.load_session(sid)["status"] == "closed"
+ return read_controls()
+
+ monkeypatch.setattr(transport.core, "source_controls", close_during_control_read)
+
+ def media(args, cwd=None, timeout=None):
+ nonlocal upload_previewed, control_committed
+ if "images" in args and "create" in args and "--dry-run" in args:
+ upload_previewed = True
+ if fault == "closed_on_upload_preview":
+ runtime.close_session(sid)
+ if (fault in ("stopped_on_upload_source_read", "closed_on_upload_source_read")
+ and upload_previewed and not control_committed and (
+ ("+messages-mget" in args and source["message_id"] in args) or
+ f"/open-apis/im/v1/messages/{source['message_id']}" in args)):
+ control_committed = True
+ if stopped:
+ transport.admit("notes-app", provider.event("notes-app", "stop-during-source-read", "/stop"))
+ assert any(control.get("command") == "stop" for control in transport.core.source_controls())
+ else:
+ runtime.close_session(sid)
+ assert store.load_session(sid)["status"] == "closed"
+ if fault == "upload_denied" and "images" in args and "create" in args and "--dry-run" not in args:
+ return {"returncode": 3, "stderr": '{"ok":false,"error":{"code":99991672}}'}
+ return io(args, cwd, timeout)
+
+ transport.runner = media
+ try:
+ source = provider.event("notes-app", "answer-with-original-image", "Read https://source.example/article")
+ transport.admit("notes-app", source)
+ original = transport.core.pending()[0]
+ sid, tid, ref = (original[key] for key in ("session_id", "turn_id", "request_ref"))
+ completed = runtime.wait_for_turn(session_id=sid, turn_id=tid, timeout_sec=10)
+ assert completed["status"] == "completed"
+ if fault == "changed_request":
+ provider.messages[source["message_id"]]["content"] = "Source URL removed."
+ path = transport.root / f"{ref}.json"
+ assert path in transport.pending_body_paths()
+ assert transport.reconcile_request(path) == 0
+ text_only = json.loads(path.read_text())
+ assert text_only["deliveries"]["terminal"]["verified"] is True
+ assert transport.core.read_request(ref).get("delivery_verified", False) is False
+ assert fetched == [] and io.uploads == 0
+ assert path not in transport.pending_body_paths()
+ assert path in transport.pending_delivery_paths() and transport.is_recovery_path(path)
+ if fault == "stopped":
+ transport.admit("notes-app", provider.event("notes-app", "stop-before-image-work", "/stop"))
+ assert transport.reconcile_request(path) == int(images_delivered)
+ saved = json.loads(path.read_text())
+ assert saved["answer_images"]["status"] == ("delivered" if images_delivered else "unverified")
+ out = [(mid, row, json.loads(row["body"]["content"])["zh_cn"]["content"])
+ for mid, row in provider.messages.items() if mid.startswith("om_out_") and row["msg_type"] == "post"]
+ assert all(row["chat_id"] == source["chat_id"] for _, row, _ in out)
+ assert any("[Source chart](" in nodes[0][0]["text"] for _, _, nodes in out)
+ assert all(not any("![Source chart]" in node.get("text", "") for line in nodes for node in line) for _, _, nodes in out)
+ if fault in ("stopped_on_upload_source_read", "closed_on_upload_source_read", "closed_on_source_controls_read",
+ "stopped_before_upload_intent", "closed_before_upload_intent"):
+ assert control_committed
+ if fault in ("stopped_before_upload_intent", "closed_before_upload_intent"):
+ from loopx.extensions.lark.return_files import _root
+ assert list((_root(transport.runtime_root) / "lark-chat-images").glob("*.json")) == []
+ if fault == "edited_after_upload_intent":
+ from loopx.extensions.lark.return_files import _root
+
+ assert source_edited_after_intent and io.uploads == 0
+ assert list((_root(transport.runtime_root) / "lark-chat-images").glob("*.json")) == []
+ assert "answer_images.terminal" not in saved["deliveries"]
+ if stopped or closed:
+ assert io.uploads == 0 and transport.core.read_request(ref).get("delivery_verified", False) is False
+ assert not any(name.startswith(("answer_images.", "answer_image_notice.")) for name in saved["deliveries"])
+ if stopped:
+ assert any(control.get("command") == "stop" for control in transport.core.source_controls())
+ else:
+ assert store.load_session(sid)["status"] == "closed"
+ assert transport.core.read_request(ref).get("delivery_verified", False) is images_delivered
+ native_images = [node["image_key"] for _, _, nodes in out for line in nodes for node in line if node["tag"] == "img"]
+ assert native_images == list(io.resources)
+ assert all(io.resources[k] == PNG_BYTES for k in native_images)
+ expected_requests = {
+ "foreign_source": [], "changed_request": [], "stopped": [], "unlisted_image": [article_url],
+ "article_redirect": [article_url], "listed_redirect": [article_url, url, "https://source.example/canonical.png"],
+ }.get(fault, [article_url, url])
+ assert fetched == expected_requests
+ assert resolutions == ["source.example"] * len(expected_requests)
+ if fault in {"same_host_redirect", "foreign_redirect", "foreign_round_trip", "query_redirect", "article_redirect"}:
+ assert io.uploads == 0 and native_images == []
+ assert ("图片尚未核验送达" in str(out)) == (not images_delivered and not stopped and not closed)
+ if fault in ("corrupt_readback", "legacy_cached_readback"):
+ io.corrupt_download = False
+ from loopx.chat_store import _atomic_write_json
+ restarted = LarkPrivateConversations(controller=runtime, runtime_root=transport.runtime_root,
+ runner=media, cli_bin="lark-cli")
+ writes = list(provider.writes)
+ assert path not in restarted.pending_body_paths()
+ assert path not in restarted.pending_delivery_paths()
+ assert restarted.reconcile_request(path) == 0
+ assert provider.writes == writes and io.uploads == 1 and fetched == [article_url, url]
+ saved["answer_images"]["next_check_at"] = 0
+ if fault == "legacy_cached_readback":
+ saved["answer_images"].pop("source_urls_verified")
+ _atomic_write_json(path, saved)
+ assert path in restarted.pending_delivery_paths()
+ assert restarted.reconcile_request(path) == 1
+ assert transport.core.read_request(ref)["delivery_verified"] is True
+ assert provider.writes == writes and io.uploads == 1
+ assert fetched == [article_url, url] * (2 if fault == "legacy_cached_readback" else 1)
+ expected_turns = 1
+ if fault == "upload_denied":
+ followup = provider.event("notes-app", "text-after-image-denial", "Answer the next question")
+ retry = transport.admit("notes-app", followup)
+ assert retry["status"] == "durably_accepted"
+ later_ref = identity_ref(transport._binding("notes-app")["provider_ref"], followup["message_id"])
+ later = transport.core.read_request(later_ref)
+ assert later["session_id"] == sid
+ assert runtime.wait_for_turn(session_id=sid, turn_id=later["turn_id"], timeout_sec=10)["status"] == "completed"
+ later_path = transport.root / f"{later_ref}.json"
+ assert path not in transport.pending_body_paths() and path not in transport.pending_delivery_paths()
+ assert later_path in transport.pending_body_paths()
+ assert transport.reconcile_request(later_path) == 1
+ assert transport.core.read_request(later_ref)["delivery_verified"] is True
+ assert transport.core.read_request(ref).get("delivery_verified", False) is False
+ expected_turns = 2
+ packets = [json.loads(line) for line in capture.read_text().splitlines()]
+ assert sum(packet.get("method") == "turn/start" for packet in packets) == expected_turns
+ assert any(row["text"].endswith(authored) for row in store.messages(sid) if row["role"] == "agent")
+ finally:
+ runtime.close()
+
+
+@pytest.mark.parametrize("fault", ["readback", "raw_paired_post", "edited_source", "edited_original_reply", "edit_on_image_get",
+ "edit_on_upload_source_read", "edit_on_next_upload_source_read", "edit_on_send_preview", "edit_on_send_source_read",
+ "edit_reply_after_upload_intent", "edit_source_on_final_pair_read", "edit_source_on_dispatch_lock",
+ "foreign_sender", "changed_bytes", "stop", "unknown_send", "upload_denied", "upload_scope_then_granted",
+ "upload_scope_with_conflicting_stdout", "upload_cli_missing", "upload_cli_fd_limit", "upload_ack_missing",
+ "closed_on_source_controls_read",
+ "unlisted_redirect", "unlisted_cached_redirect"])
+def test_original_worker_queues_only_media_for_a_verified_legacy_answer(ordinary, monkeypatch, source_https, fault): # noqa: F811
+ from hashlib import sha256
+ from loopx.chat_store import _atomic_write_json
+ from test_chat_image_attachments import PNG_BYTES
+ from test_lark_app_image_projection import ImageIO
+
+ resource = pytest.importorskip("resource") if fault == "upload_cli_fd_limit" else None
+ store, runtime, provider, transport = connect(ordinary)
+ fake = ordinary[5]
+ origin = "https://source.example/article"
+ urls = [f"https://source.example/figure-{i}.png" for i in range(1, 5)]
+ figures = {url: PNG_BYTES + bytes([i]) for i, url in enumerate(urls)}
+ answer = f"Original explanation.\n\n\n\n"
+ script = fake.read_text()
+ marker = '"message": "Runtime response.",'
+ assert marker in script
+ fake.write_text(script.replace(marker, f'"message": {answer!r},', 1))
+ io = ImageIO(provider)
+ edited_body = None
+ late_source_edit = False
+ committed_resource_path = None
+ upload_previews = 0
+ send_preview_seen = False
+ denied_uploads = 0
+ scope_granted = False
+ executable_restored = False
+
+ def edit_original_reply():
+ nonlocal edited_body
+ edited_body = {"content": json.dumps({"zh_cn": {"content": [[{"tag": "text", "text": "The original answer was edited."}]]}})}
+ provider.messages[prior_id]["body"] = edited_body
+
+ def provider_io(args, cwd=None, timeout=None):
+ nonlocal upload_previews, send_preview_seen, denied_uploads, late_source_edit
+ image_upload = "images" in args and "create" in args and "--dry-run" not in args
+ pair_read = "/open-apis/im/v1/messages/mget" in args and "--params" in args
+ source_read = (("+messages-mget" in args and event["message_id"] in args)
+ or (pair_read and event["message_id"] in json.loads(args[args.index("--params") + 1])["message_ids"]))
+ if fault == "upload_denied" and image_upload:
+ provider.calls.append(list(args))
+ return {"returncode": 3, "stderr": '{"ok":false,"error":{"type":"authorization","code":99991672}}'}
+ typed_scope_fault = fault in ("upload_scope_then_granted", "upload_scope_with_conflicting_stdout")
+ if image_upload and typed_scope_fault and io.uploads == 1 and not scope_granted:
+ provider.calls.append(list(args))
+ denied_uploads += 1
+ return {"returncode": 3,
+ "stderr": json.dumps({"ok": False, "identity": "bot", "error": {
+ "type": "authorization", "subtype": "app_scope_not_applied", "code": 99991672,
+ "identity": "bot"}}),
+ "stdout": "invalid provider acknowledgement" if fault == "upload_scope_with_conflicting_stdout" else ""}
+ if (fault == "edit_source_on_final_pair_read" and committed_resource_path is not None
+ and not late_source_edit
+ and (("+messages-mget" in args and prior_id in args)
+ or pair_read)):
+ assert json.loads(committed_resource_path.read_text())["upload_started"] is True
+ provider.messages[event["message_id"]]["content"] = "The source changed during the final reply read."
+ late_source_edit = True
+ uploads_before_edit = {"edit_on_upload_source_read": 0, "edit_on_next_upload_source_read": 1}
+ if (fault in uploads_before_edit and upload_previews == uploads_before_edit[fault] + 1 and edited_body is None
+ and io.uploads == uploads_before_edit[fault]
+ and source_read):
+ edit_original_reply()
+ if (fault == "edit_on_send_source_read" and send_preview_seen and edited_body is None
+ and source_read):
+ edit_original_reply()
+ result = io(args, cwd, timeout)
+ if pair_read and fault == "raw_paired_post":
+ body = json.loads(result["stdout"])
+ historical = next(row for row in body["data"]["items"] if row["message_id"] == prior_id)
+ original_rows = json.loads(historical["body"]["content"])["zh_cn"]["content"]
+ historical["body"] = {"content": json.dumps({
+ "title": "", "content_v2": original_rows,
+ "content": [[{"tag": "text", "text": "A rendered preview only."}]],
+ })}
+ result = {**result, "stdout": json.dumps(body)}
+ if "images" in args and "create" in args and "--dry-run" in args:
+ upload_previews += 1
+ if "+messages-send" in args and "--dry-run" in args and "--content" in args:
+ if "原图(按原文顺序)" in args[args.index("--content") + 1]:
+ send_preview_seen = True
+ if fault == "edit_on_send_preview":
+ edit_original_reply()
+ return result
+
+ runner = provider_io
+ launch_faults = {"upload_cli_missing", "upload_cli_fd_limit"}
+ upload_faults = launch_faults | {"upload_ack_missing"}
+ if fault in upload_faults:
+ from loopx.extensions.lark.cli_resolution import LarkCliResolution, build_lark_command_runner
+
+ native_runner = build_lark_command_runner(LarkCliResolution(sys.executable, True, "fixture", None, None))
+ missing_executable = str(fake.with_name("missing-lark-cli"))
+ if resource is not None:
+ import errno
+ import subprocess
+
+ popen = subprocess.Popen
+
+ def spawn(command, *args, **kwargs):
+ if command != [sys.executable, "-c", "pass"] or executable_restored:
+ return popen(command, *args, **kwargs)
+ # Exhaust descriptors only at real Popen pipe creation,
+ # after the source check and durable upload marker.
+ limits = resource.getrlimit(resource.RLIMIT_NOFILE)
+ try:
+ resource.setrlimit(resource.RLIMIT_NOFILE, (0, limits[1]))
+ return popen(command, *args, **kwargs)
+ except OSError as exc:
+ assert exc.errno == errno.EMFILE
+ raise
+ finally:
+ resource.setrlimit(resource.RLIMIT_NOFILE, limits)
+
+ monkeypatch.setattr(subprocess, "Popen", spawn)
+
+ def guarded_provider(args, cwd=None, timeout=None, *, dispatch_guard=None):
+ if dispatch_guard is not None:
+ # Use the real subprocess boundary with a synthetic provider:
+ # an unavailable executable cannot reach resource creation.
+ command = ([missing_executable] if fault == "upload_cli_missing" and not executable_restored
+ else [sys.executable, "-c", "pass"])
+ assert native_runner(command, cwd, timeout, dispatch_guard=dispatch_guard)["returncode"] == 0
+ return provider_io(args, cwd, timeout)
+
+ guarded_provider.dispatch_guard = True
+ runner = guarded_provider
+
+ transport.runner = runner
+
+ unlisted = "https://source.example/unlisted.png"
+
+ def respond(url):
+ if url == origin:
+ return None, "text/html", "".join(f'
' for url in urls).encode()
+ if fault == "edit_on_image_get":
+ edit_original_reply()
+ if fault in ("unlisted_redirect", "unlisted_cached_redirect") and url == urls[-1]:
+ return "/unlisted.png", None, b""
+ return None, "image/png", figures[urls[-1] if url == unlisted else url]
+
+ fetched, resolutions = source_https(respond)
+
+ def restart():
+ return LarkPrivateConversations(controller=runtime, runtime_root=transport.runtime_root,
+ runner=runner, cli_bin="lark-cli")
+
+ try:
+ event = provider.event("notes-app", "historical-image-answer", f"Read {origin}")
+ assert transport.admit("notes-app", event)["status"] == "durably_accepted"
+ original = transport.core.pending()[0]
+ sid, tid, ref = (original[key] for key in ("session_id", "turn_id", "request_ref"))
+ assert runtime.wait_for_turn(session_id=sid, turn_id=tid, timeout_sec=10)["status"] == "completed"
+ path = transport.root / f"{ref}.json"
+ # Produce an actual legacy reply and provider receipt before installing
+ # the media-only repair. The stored answer and Core are real.
+ with monkeypatch.context() as legacy:
+ legacy.setattr(transport, "_deliver_completed_answer", lambda p, row, text, *, inbox:
+ transport._deliver(p, row, "terminal", text, inbox=inbox))
+ assert transport.reconcile_request(path) == 1
+ saved = json.loads(path.read_text())
+ terminal = saved["deliveries"]["terminal"]
+ prior_id = terminal["attempt"]["message_ref"]
+ prior_body = json.dumps(provider.messages[prior_id]["body"])
+ core = (transport.core.read_request(ref), store.load_turn(sid, tid), store.messages(sid))
+ writes = len(provider.writes)
+ worker = restart()
+ assert path not in worker.pending_delivery_paths()
+ assert worker.reconcile_request(path) == 0 and io.uploads == 0
+
+ if fault == "closed_on_source_controls_read":
+ read_controls = worker.core.source_controls
+
+ def close_during_control_read():
+ if upload_previews and store.load_session(sid)["status"] != "closed":
+ assert runtime.close_session(sid)
+ return read_controls()
+
+ monkeypatch.setattr(worker.core, "source_controls", close_during_control_read)
+
+ plan = {"binding_id": saved["binding_id"], "turn_id": tid, "message_id": prior_id,
+ "answer_sha256": sha256(answer.encode()).hexdigest(),
+ "images": [{"url": url, "sha256": sha256(figures[url]).hexdigest()} for url in urls]}
+ manifest = worker.root / "historical-answer-images" / path.name
+ _atomic_write_json(manifest, plan)
+ if fault == "unlisted_cached_redirect":
+ import base64
+ from loopx.chat_attachments import normalize_chat_image_attachments
+
+ legacy_attachments = normalize_chat_image_attachments([
+ {"id": f"reply-image-{index}", "name": f"original-{index}.png", "mime_type": "image/png",
+ "data_url": "data:image/png;base64," + base64.b64encode(data).decode(), "size": len(data)}
+ for index, data in enumerate(figures.values(), 1)])
+ saved["historical_answer_images"] = {"plan": plan, "status": "pending", "attachments": legacy_attachments}
+ _atomic_write_json(path, saved)
+ assert path not in worker.pending_body_paths()
+ assert path in worker.pending_delivery_paths() and worker.is_recovery_path(path)
+ if fault == "edited_source":
+ provider.messages[event["message_id"]]["content"] = "Changed source."
+ elif fault == "edited_original_reply":
+ edit_original_reply()
+ elif fault == "foreign_sender":
+ provider.messages[prior_id]["sender"]["id"] = provider.profile_apps["steward-app"]
+ elif fault == "changed_bytes":
+ figures[urls[-1]] = PNG_BYTES
+ elif fault == "stop":
+ # The authoritative Stop predates any possible file recovery.
+ control = {**core[0], "command": "stop"}
+ _atomic_write_json(transport.core.root / ("d" * 24 + ".json"), control)
+ elif fault == "readback":
+ io.corrupt_download = True
+ elif fault == "unknown_send":
+ provider.lose_next_reply = True
+ elif fault == "upload_ack_missing":
+ io.unknown_upload = True
+ elif fault in {"edit_reply_after_upload_intent", "edit_source_on_final_pair_read", "edit_source_on_dispatch_lock"}:
+ from loopx.extensions.lark import return_files
+
+ write_receipt = return_files._write
+
+ def edit_after_upload_intent(path, value):
+ nonlocal committed_resource_path
+ write_receipt(path, value)
+ if value.get("upload_started") and committed_resource_path is None:
+ committed_resource_path = path
+ if fault == "edit_reply_after_upload_intent":
+ assert json.loads(path.read_text())["upload_started"] is True
+ edit_original_reply()
+
+ monkeypatch.setattr(return_files, "_write", edit_after_upload_intent)
+ if fault == "edit_source_on_dispatch_lock":
+ from loopx.extensions.lark import private_delivery_discovery
+
+ enter_lock = private_delivery_discovery.try_exclusive_file_lock
+
+ def edit_source_before_lock(path, **kwargs):
+ nonlocal late_source_edit
+ if kwargs.get("operation") == "dispatch_external_answer_media" and not late_source_edit:
+ assert json.loads(committed_resource_path.read_text())["upload_started"] is True
+ provider.messages[event["message_id"]]["content"] = "The original changed before the dispatch lock."
+ late_source_edit = True
+ return enter_lock(path, **kwargs)
+
+ monkeypatch.setattr(private_delivery_discovery, "try_exclusive_file_lock", edit_source_before_lock)
+
+ if fault == "raw_paired_post":
+ assert worker.reconcile_request(path) == 1
+ assert io.uploads == 4 and len(provider.writes) == writes + 1
+ assert json.loads(path.read_text())["deliveries"]["answer_images.historical"]["images_verified"] is True
+ assert worker.reconcile_request(path) == 0 and io.uploads == 4
+ assert core == (transport.core.read_request(ref), store.load_turn(sid, tid), store.messages(sid))
+ return
+ assert worker.reconcile_request(path) == 0
+ if fault == "closed_on_source_controls_read":
+ assert upload_previews == 1 and store.load_session(sid)["status"] == "closed"
+ if fault in upload_faults:
+ # Only the attempt that could not start is safe to retry.
+ assert io.uploads == int(fault == "upload_ack_missing")
+ assert len(provider.writes) == writes
+ pending = json.loads(path.read_text())
+ assert pending["historical_answer_images"]["status"] == "pending"
+ assert "answer_images.historical" not in pending["deliveries"]
+ pending["historical_answer_images"]["next_check_at"] = 0
+ _atomic_write_json(path, pending)
+ executable_restored = True
+ io.unknown_upload = False
+ worker = restart()
+ recovered = fault in launch_faults
+ assert worker.reconcile_request(path) == int(recovered)
+ post_state = json.loads(path.read_text())
+ assert post_state["historical_answer_images"]["status"] == ("delivered" if recovered else "pending")
+ if recovered:
+ media = post_state["deliveries"]["answer_images.historical"]
+ post = provider.messages[media["attempt"]["message_ref"]]
+ assert post["chat_id"] == event["chat_id"]
+ assert post["sender"]["id"] == provider.profile_apps["notes-app"]
+ assert media["images_verified"] and io.downloads == 4
+ assert [io.resources[key] for key in media["image_keys"]] == list(figures.values())
+ assert worker.reconcile_request(path) == 0
+ assert io.uploads == (4 if recovered else 1)
+ assert len(provider.writes) == writes + int(recovered)
+ elif fault in {"readback", "unknown_send", "upload_scope_then_granted", "upload_scope_with_conflicting_stdout"}:
+ pending = json.loads(path.read_text())
+ assert pending["historical_answer_images"]["status"] == "pending"
+ if fault.startswith("upload_scope_"):
+ assert denied_uploads == 1 and io.uploads == 1 and len(provider.writes) == writes
+ assert "answer_images.historical" not in pending["deliveries"]
+ scope_granted = True
+ else:
+ assert io.uploads == 4 and len(provider.writes) == writes + 1
+ worker = restart()
+ assert path not in worker.pending_delivery_paths()
+ pending["historical_answer_images"]["next_check_at"] = 0
+ _atomic_write_json(path, pending)
+ io.corrupt_download = False
+ assert path in worker.pending_delivery_paths()
+ delivered = fault in ("readback", "upload_scope_then_granted")
+ assert worker.reconcile_request(path) == int(delivered)
+ if fault == "upload_scope_with_conflicting_stdout":
+ assert denied_uploads == 1 and io.uploads == 1 and len(provider.writes) == writes
+ assert json.loads(path.read_text())["historical_answer_images"]["status"] == "pending"
+ return
+ assert io.uploads == 4 and len(provider.writes) == writes + 1
+ row = json.loads(path.read_text())
+ assert row["historical_answer_images"]["status"] == ("delivered" if delivered else "pending")
+ outputs = [m for m in provider.messages.values() if m["msg_type"] == "post"
+ and "原图(按原文顺序)" in m["body"]["content"]]
+ assert len(outputs) == 1
+ post = outputs[0]
+ assert post["chat_id"] == event["chat_id"] and post["sender"]["id"] == provider.profile_apps["notes-app"]
+ lines = json.loads(post["body"]["content"])["zh_cn"]["content"]
+ keys = [node["image_key"] for line in lines for node in line if node["tag"] == "img"]
+ assert len(keys) == 4 and [io.resources[key] for key in keys] == list(figures.values())
+ assert worker.reconcile_request(path) == 0
+ else:
+ expected_uploads = {"edit_on_send_preview": 4, "edit_on_send_source_read": 4,
+ "edit_on_next_upload_source_read": 1}
+ assert io.uploads == expected_uploads.get(fault, 0) and len(provider.writes) == writes
+ if fault == "edit_reply_after_upload_intent":
+ from loopx.extensions.lark.return_files import _root
+
+ assert edited_body is not None
+ assert list((_root(worker.runtime_root) / "lark-chat-images").glob("*.json")) == []
+ if fault in {"edit_source_on_final_pair_read", "edit_source_on_dispatch_lock"}:
+ assert late_source_edit and not committed_resource_path.exists()
+ assert "answer_images.historical" not in json.loads(path.read_text())["deliveries"]
+ if fault in {"edit_on_upload_source_read", "edit_on_next_upload_source_read", "edit_on_send_source_read",
+ "edit_reply_after_upload_intent"}:
+ assert edited_body is not None
+ pending = json.loads(path.read_text())
+ pending["historical_answer_images"]["next_check_at"] = 0
+ _atomic_write_json(path, pending)
+ assert restart().reconcile_request(path) == 0
+ assert io.uploads == expected_uploads.get(fault, 0) and len(provider.writes) == writes
+ if fault in ("unlisted_redirect", "unlisted_cached_redirect"):
+ assert fetched == [origin, *urls] and resolutions == ["source.example"] * len(fetched)
+ assert unlisted not in fetched
+ if fault == "upload_denied":
+ pending = json.loads(path.read_text())
+ assert pending["historical_answer_images"]["status"] == "pending"
+ assert "answer_images.historical" not in pending["deliveries"]
+ assert any("images" in cmd and "create" in cmd and "--dry-run" not in cmd for cmd in provider.calls)
+ assert provider.messages[prior_id]["body"] == (edited_body if edited_body is not None else json.loads(prior_body))
+ assert json.loads(path.read_text())["deliveries"]["terminal"] == terminal
+ assert core == (transport.core.read_request(ref), store.load_turn(sid, tid), store.messages(sid))
+ assert sum(json.loads(line).get("method") == "turn/start" for line in ordinary[4].read_text().splitlines()) == 1
+ finally:
+ runtime.close()
+
+
+def test_private_delivery_keeps_retired_steward_cards_out_of_image_recovery(
+ ordinary, # noqa: F811
+):
+ from loopx.chat_store import _atomic_write_json
+
+ store, runtime, provider, transport = connect(ordinary)
+ bindings = transport.bindings
+ workspace = runtime.project_contexts.available()[0]["project_ref"]
+ binding = bindings.configure(transport_ref="steward-app", project_ref=workspace,
+ executor_endpoint_id="codex", context_kind="steward")
+ try:
+ event = provider.event("steward-app", "retired-card-before-image-repair", "/triage")
+ assert transport.admit("steward-app", event)["status"] == "triage_queued"
+ ref = identity_ref(binding["provider_ref"], event["message_id"])
+ card = transport.root / f"{ref}.json"
+ assert card in transport.pending_body_paths()
+ assert card in transport.pending_delivery_paths()
+ assert transport.health()[binding["binding_id"]]["pending_count"] == 1
+
+ provider.messages[event["message_id"]]["content"] = "/status"
+ assert transport.reconcile_request(card) == 0
+ retired = json.loads(card.read_text())
+ assert retired["triage"]["retired"] == "original_source_changed"
+
+ # Even an orphaned descriptor with this filename cannot turn an
+ # upstream transport-only record into image or model recovery work.
+ _atomic_write_json(transport.root / "historical-answer-images" / card.name, {})
+ worker = LarkPrivateConversations(controller=runtime, runtime_root=transport.runtime_root,
+ runner=provider, cli_bin="lark-cli")
+ assert card not in worker.pending_body_paths()
+ assert card not in worker.pending_delivery_paths()
+ assert worker.health()[binding["binding_id"]]["pending_count"] == 0
+ assert not (worker.core.root / card.name).exists()
+
+ assert worker.reconcile_request(card) == 0
+ assert json.loads(card.read_text()) == retired
+ assert not store.list_sessions()
+ assert not provider.writes
+ finally:
+ runtime.close()
+
+
@pytest.mark.parametrize("case", ["normal", "invalid-metadata", "metadata-only", "unfinished", "source-changed", "closed", "lost-ack", "message-crash",
"historical", "historical-source-changed", "historical-stop"])
def test_original_worker_recovers_native_result_without_replay(tmp_path, language_server, monkeypatch, case): # noqa: F811
@@ -2362,6 +3036,7 @@ def counted_revision():
def test_settings_health_reuses_core_correlation_but_observes_current_work(tmp_path, monkeypatch):
from loopx.capabilities.native_chat import external_conversations
from loopx.chat_store import _atomic_write_json
+ from loopx.extensions.lark import private_conversations
from test_private_chat_backlog_projection import fixture_history
store, core, transport, rows = fixture_history(tmp_path, count=2)
@@ -2381,19 +3056,29 @@ def test_settings_health_reuses_core_correlation_but_observes_current_work(tmp_p
_atomic_write_json(partial_path, {**initial_partial, "deliveries": {
"terminal": {"started": True, "verified": False}}})
read_request = external_conversations._read_json
+ read_delivery = private_conversations._read_json
request_reads = []
+ delivery_reads = []
def observe(path):
request_reads.append(path)
return read_request(path)
+ def observe_delivery(path):
+ delivery_reads.append(path)
+ return read_delivery(path)
+
monkeypatch.setattr(external_conversations, "_read_json", observe)
+ monkeypatch.setattr(private_conversations, "_read_json", observe_delivery)
assert transport.health()[binding] == {"pending_count": 1, "recovery_count": 1}
assert set(request_reads) == {partial_request, completed_request}
+ assert set(delivery_reads) == {partial_path, completed_path}
request_reads.clear()
+ delivery_reads.clear()
assert transport.health()[binding] == {"pending_count": 1, "recovery_count": 1}
# Windows has no reliable ctime mutation stamp; its reader stays fresh.
assert set(request_reads) == ({partial_request, completed_request} if sys.platform == "win32" else set())
+ assert set(delivery_reads) == ({partial_path, completed_path} if sys.platform == "win32" else set())
request_reads.clear()
# Independent native updates must not wait for the Lark receipt to change.
@@ -2417,6 +3102,13 @@ def observe(path):
completed_path.unlink()
assert transport.health()[binding] == {"pending_count": 0, "recovery_count": 0}
+ # A delivered body can still have an outstanding historical image reply.
+ _atomic_write_json(partial_path, {**initial_partial, "historical_answer_images": {"status": "pending"}})
+ assert transport.health()[binding] == {"pending_count": 1, "recovery_count": 0}
+ assert transport.health()[binding] == {"pending_count": 1, "recovery_count": 0}
+ _atomic_write_json(partial_path, {**initial_partial, "historical_answer_images": {"status": "delivered"}})
+ assert transport.health()[binding] == {"pending_count": 0, "recovery_count": 0}
+
@pytest.mark.parametrize("scenario", ["concurrent-close", "transient-session-read"])
def test_settings_health_uses_current_session_for_each_batch(ordinary, monkeypatch, scenario): # noqa: F811
@@ -4608,7 +5300,7 @@ def request(path, body=None):
def test_http_stop_recovery_reuses_source_revision_without_reusing_turn_state(ordinary, monkeypatch): # noqa: F811
from loopx.chat_server import ChatHTTPServer, ChatRequestHandler
- from loopx.control_plane import effect_runtime
+ from loopx.control_plane import chat_turn_acceptance, effect_runtime
store, runtime, provider, transport = connect(ordinary)
binding = transport._binding("notes-app")
@@ -4626,9 +5318,11 @@ def queue(name, binding_id):
original_turns = [store._turn_path(sid, turn["turn_id"]).read_bytes() for turn in [owed, authorized]]
native_before = ordinary[4].read_bytes() if ordinary[4].exists() else None
revision_reads = []
+ queue_selections = []
observed = threading.local()
fingerprint = effect_runtime._runtime_fingerprint
stop_recoveries = ChatConversationBindings.stop_recoveries
+ select_queue = chat_turn_acceptance.queued_chat_turn_ids
def counted_fingerprint():
if getattr(observed, "stop_recovery", False):
@@ -4642,8 +5336,14 @@ def observed_stop_recoveries(self, current):
finally:
observed.stop_recovery = False
+ def read_queue(turns, *args, **kwargs):
+ if getattr(observed, "stop_recovery", False):
+ queue_selections.append(len(turns))
+ return select_queue(turns, *args, **kwargs)
+
monkeypatch.setattr(effect_runtime, "_runtime_fingerprint", counted_fingerprint)
monkeypatch.setattr(ChatConversationBindings, "stop_recoveries", observed_stop_recoveries)
+ monkeypatch.setattr(chat_turn_acceptance, "queued_chat_turn_ids", read_queue)
server = ChatHTTPServer(("127.0.0.1", 0), ChatRequestHandler)
server.chat_store, server.runtime_controller, server.verbose = store, runtime, False
server.lark_private_conversations = transport
@@ -4653,6 +5353,7 @@ def observed_stop_recoveries(self, current):
def snapshot():
revision_reads.clear()
+ queue_selections.clear()
connection = http.client.HTTPConnection(*server.server_address, timeout=15)
try:
connection.request("GET", "/api/chat/lark/private-conversations")
@@ -4660,6 +5361,7 @@ def snapshot():
assert response.status == 200
result = json.loads(response.read())
assert len(revision_reads) <= 1 # At most one source inventory, rather than one per Session.
+ assert len(queue_selections) == 1 and queue_selections[0] > 0
return result
finally:
connection.close()
diff --git a/tests/test_native_steward_turn_host.py b/tests/test_native_steward_turn_host.py
index 4d0deb96d6..3c11caab85 100644
--- a/tests/test_native_steward_turn_host.py
+++ b/tests/test_native_steward_turn_host.py
@@ -940,6 +940,41 @@ def test_native_turn_uses_original_app_server_and_kernel_without_a_new_session(s
assert capture.read_bytes() == calls
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_managed_task_edit_after_planning_does_not_start_native_turn(steward, tmp_path, monkeypatch, provider):
+ from loopx.cli_commands import turn_run_once
+
+ service, session, actor, todo_id, _, capture = managed_fixture(steward, tmp_path, monkeypatch, provider)
+ store, runtime, *_ = steward
+ original_request = turn_run_once.prepare_turn_host_request
+ revised = "[P1] Check the corrected readiness condition before continuing."
+ edits = []
+
+ def edit_after_planning(*args, **kwargs):
+ prepared = original_request(*args, **kwargs)
+ assert args[0].host == "codex-app-server"
+ edits.append(cli(runtime, "todo", "update", "--goal-id", actor["goal_id"],
+ "--todo-id", todo_id, "--agent-id", actor["agent_id"], "--text", revised))
+ return prepared
+
+ monkeypatch.setattr(turn_run_once, "prepare_turn_host_request", edit_after_planning)
+ before = capture.read_bytes()
+ accepted, created = service.submit(session["session_id"], agent_id=actor["agent_id"],
+ operation_id="changed-task-before-native-launch", todo_id=todo_id)
+ assert created
+ completed = runtime.wait_for_turn(session_id=accepted["session_id"], turn_id=accepted["turn_id"], timeout_sec=90)
+ result = completed["response"]["native_loopx"]
+ assert len(edits) == 1 and edits[0]["ok"]
+ assert not result["ok"] and "current_turn_work_changed" in json.dumps(result), result
+ calls = [json.loads(line) for line in capture.read_bytes()[len(before):].splitlines()]
+ assert all(row["method"] != "turn/start" for row in calls)
+ assert store.load_session(session["session_id"])["upstream_thread_id"] == session["upstream_thread_id"]
+ current = cli(runtime, "todo", "list", "--goal-id", actor["goal_id"], "--todo-id", todo_id)["todo"]
+ assert current["text"] == revised and not current["done"]
+ index = Path(runtime.coordination_runtime_root) / "goals" / actor["goal_id"] / "runs/index.jsonl"
+ assert not index.exists() or "quota_slot_spent" not in index.read_text()
+
+
def test_unselected_or_unavailable_work_does_not_invoke_the_model(steward, tmp_path, monkeypatch):
sid, actor, _ = prepare(steward, "sqlite", tmp_path, monkeypatch)
store, runtime, _, _, binding, capture, _ = steward
diff --git a/tests/test_postgres_cli.py b/tests/test_postgres_cli.py
new file mode 100644
index 0000000000..d4eba70622
--- /dev/null
+++ b/tests/test_postgres_cli.py
@@ -0,0 +1,168 @@
+"""Unit and integration tests for loopx postgres CLI commands."""
+import json
+import subprocess
+import sys
+from pathlib import Path
+
+REPO = Path(__file__).resolve().parents[1]
+
+
+def test_postgres_setup_dry_run():
+ process = subprocess.run(
+ [
+ sys.executable,
+ "-m",
+ "loopx.entrypoint",
+ "--format",
+ "json",
+ "postgres",
+ "setup",
+ "--database-url",
+ "postgresql://mock_user:mock_pass@localhost:5432/mock_db",
+ "--runtime-role",
+ "loopx_worker",
+ ],
+ cwd=REPO,
+ capture_output=True,
+ text=True,
+ timeout=30,
+ check=False,
+ )
+ assert process.returncode == 0, process.stderr
+ payload = json.loads(process.stdout)
+ assert payload["status"] == "dry_run"
+ assert payload["schema_version"] == "loopx_postgresql_authority_store_v1"
+ assert payload["runtime_role"] == "loopx_worker"
+ assert payload["database_url_configured"] is True
+
+
+def test_postgres_setup_missing_url(monkeypatch):
+ monkeypatch.delenv("LOOPX_POSTGRES_URL", raising=False)
+ process = subprocess.run(
+ [
+ sys.executable,
+ "-m",
+ "loopx.entrypoint",
+ "--format",
+ "json",
+ "postgres",
+ "setup",
+ ],
+ cwd=REPO,
+ capture_output=True,
+ text=True,
+ timeout=30,
+ check=False,
+ )
+ assert process.returncode == 1
+ payload = json.loads(process.stdout)
+ assert payload["status"] == "failed"
+ assert "required" in payload.get("reason", "").lower()
+
+
+def test_postgres_local_to_pg_seed_export(tmp_path):
+ runtime = tmp_path / "runtime"
+ registry = tmp_path / "registry.json"
+ state = tmp_path / "state.md"
+ goal = "seed-export-goal"
+
+ runtime.mkdir(parents=True, exist_ok=True)
+ state.write_text("# Synthetic state for seed export\n")
+ registry.write_text(
+ json.dumps(
+ {
+ "common_runtime_root": str(runtime),
+ "goals": [{"id": goal, "repo": str(tmp_path), "state_file": state.name}],
+ }
+ )
+ )
+
+ # Initialize local authority store using FileAuthorityStore via Node
+ module = REPO / "loopx/control_plane/coordination/file_authority_store.ts"
+ store_dir = runtime / "authority" / "file-v0"
+ store_dir.mkdir(parents=True, exist_ok=True)
+
+ script = (
+ f"import {{FileAuthorityStore}} from {json.dumps(module.as_uri())};"
+ "const store = new FileAuthorityStore(process.argv[1], process.argv[2]);"
+ "await store.commitAuthority({"
+ " operation_id: 'op-init',"
+ " expected_provider_revision: null,"
+ " events: [],"
+ " receipts: [],"
+ " next_projection: {"
+ " schema_version: 'loopx_authority_projection_v0',"
+ " goal_id: process.argv[2],"
+ " owner_identity: 'ou_1234567890abcdef1234567890',"
+ " channel: { chat_id: 'oc_abcdef1234567890abcdef12345678' },"
+ " todos: ["
+ " {"
+ " id: 'todo-1',"
+ " title: 'Work on feature with .local/path and Feishu contact',"
+ " required_write_scopes: ['/Users/test/Developer/loopx/packages/core']"
+ " }"
+ " ]"
+ " }"
+ "});"
+ )
+ subprocess.run(
+ ["node", "--no-warnings", "--experimental-strip-types", "--input-type=module", "-e", script, str(store_dir), goal],
+ capture_output=True,
+ text=True,
+ timeout=30,
+ check=True,
+ )
+
+ output_seed = tmp_path / "genesis_seed.json"
+ command = [
+ sys.executable, "-m", "loopx.entrypoint", "--registry", str(registry),
+ "--format", "json", "postgres", "local-to-pg-seed", "--goal-id", goal,
+ "--tenant-id", "tenant-alpha", "--output", str(output_seed),
+ ]
+
+ def export():
+ return subprocess.run(command, cwd=REPO, capture_output=True, text=True, timeout=45, check=False)
+
+ process = export()
+ assert process.returncode == 0, process.stderr
+ payload = json.loads(process.stdout)
+ assert payload["status"] == "exported"
+ assert payload["cursor"] == "1"
+ assert payload["goal_id"] == goal
+ assert payload["tenant_id"] == "tenant-alpha"
+ assert bool(payload.get("seed_digest"))
+
+ assert output_seed.is_file()
+ seed_content = json.loads(output_seed.read_text())
+ assert seed_content["schema_version"] == "loopx_pg_genesis_seed_v0"
+ assert seed_content["cursor"] == "1"
+ # Verify Feishu IDs and local paths were sanitized
+ raw_str = json.dumps(seed_content)
+ assert "ou_1234567890abcdef1234567890" not in raw_str
+ assert "oc_abcdef1234567890abcdef12345678" not in raw_str
+ assert "/Users/test/Developer" not in raw_str
+
+ original = output_seed.read_bytes()
+ inode = output_seed.stat().st_ino
+ repeated = export()
+ assert repeated.returncode == 0, repeated.stderr
+ assert json.loads(repeated.stdout)["seed_digest"] == payload["seed_digest"]
+ assert output_seed.stat().st_ino == inode
+ assert output_seed.read_bytes() == original
+
+ different = original.replace(b"tenant-alpha", b"tenant-other", 1)
+ assert different != original and len(different) == len(original)
+ output_seed.write_bytes(different)
+ refused = export()
+ assert refused.returncode == 1
+ assert "identical regular file" in json.loads(refused.stdout)["reason"]
+ assert output_seed.read_bytes() == different
+
+ alternative = tmp_path / "seed_target.json"
+ alternative.write_bytes(original)
+ output_seed.unlink()
+ output_seed.symlink_to(alternative)
+ symlink = export()
+ assert symlink.returncode == 1
+ assert output_seed.is_symlink()
+ assert alternative.read_bytes() == original
diff --git a/tests/test_public_source_reader.py b/tests/test_public_source_reader.py
index b461adc648..3bd6fa77c9 100644
--- a/tests/test_public_source_reader.py
+++ b/tests/test_public_source_reader.py
@@ -70,6 +70,26 @@ def test_github_diff_transport_failure_recovers_canonical_source_in_same_budget(
for _, _, _, _, headers in state["attempts"])
+def test_github_diff_recovery_honors_exact_source_scope_before_dns(github_diff_transport):
+ state = github_diff_transport
+ original = "https://github.com/example/project/pull/42.diff"
+ with pytest.raises(ValueError, match="public source URL not authorized"):
+ reader.fetch(original, deadline=110.0, allowed_urls={original})
+ assert state["dns"] == ["github.com"]
+ assert [attempt[0] for attempt in state["attempts"]] == ["github.com", "github.com"]
+ assert len(state["closed"]) == 2
+
+
+def test_public_url_scope_rejects_initial_destination_before_dns(monkeypatch):
+ def resolve(host):
+ pytest.fail(f"unapproved destination resolved: {host}")
+
+ monkeypatch.setattr(reader, "public_addresses", resolve)
+ with pytest.raises(ValueError, match="public source URL not authorized"):
+ reader.fetch("https://source.example/unlisted.png", deadline=time.monotonic() + 10,
+ allowed_urls={"https://source.example/listed.png"})
+
+
@pytest.mark.parametrize("host", ["github.com", "patch-diff.githubusercontent.com"])
def test_github_diff_recovery_rejects_mixed_dns_before_connection(github_diff_transport, host):
state = github_diff_transport
diff --git a/tests/test_python_ci_workflow.py b/tests/test_python_ci_workflow.py
index b4f5e70407..ab82c69360 100644
--- a/tests/test_python_ci_workflow.py
+++ b/tests/test_python_ci_workflow.py
@@ -351,7 +351,7 @@ def test_windows_lifecycle_suite_references_existing_tests() -> None:
assert "tests/control_plane/test_cli_output_differential.py" in args
assert set(args[args.index("-k") + 1].split(" or ")) == {
"stable_budget_fixture", "long_path_budget",
- "probe_process_failure_preserves_utf8_diagnostics",
+ "probe_process_failure_preserves_utf8_diagnostics", "pinned_reader_follows",
}
diff --git a/tests/test_state_recovery.py b/tests/test_state_recovery.py
index 8251aabf6e..b3d6718f5f 100644
--- a/tests/test_state_recovery.py
+++ b/tests/test_state_recovery.py
@@ -578,7 +578,7 @@ def test_offline_worker_timeout_does_not_leave_a_copy_or_stop_shared_runtime(tmp
assert normal["activation_performed"] is False
-def test_real_sqlite_history_limit_is_complete_and_overflow_refuses_publication(tmp_path):
+def test_real_sqlite_history_above_20000_is_verified_through_outer_backup(tmp_path):
registry, runtime, state = promoted_create_fixture(tmp_path, provider="sqlite")
project = state.parents[3]
owner = Path(__file__).parents[1] / "loopx/control_plane/coordination"
@@ -615,25 +615,24 @@ def test_real_sqlite_history_limit_is_complete_and_overflow_refuses_publication(
if(readback.status!=='loaded'||Number(readback.cursor)!==r.last)throw new Error('Fixture readback mismatch');
process.stdout.write(JSON.stringify(audit));
"""
- for first, last, expected in [(2, 20000, "readable"), (20001, 20001, "refused")]:
+ for first, last in [(2, 20000), (20001, 20001)]:
seeded = subprocess.run(["node", "--no-warnings", "--experimental-sqlite", "--experimental-strip-types",
"--input-type=module", "-e", script], input=json.dumps({"root": str(runtime), "first": first, "last": last}),
text=True, capture_output=True, check=True, timeout=120)
- assert json.loads(seeded.stdout)["commits"] == last
+ verified = json.loads(seeded.stdout)
+ assert verified["commits"] == last
backup = execute_state_backup_plan(build_state_backup_plan(project=project, runtime_root=runtime,
registry_path=registry, output_dir=tmp_path / "backups", backup_id=f"history-{last}",
include_registry_projects=False, include_automations=False, include_skills=False))
destination = tmp_path / f"inert-{last}"
code, result = _cli("backup-state", "verify", "--archive", backup["archive_path"],
"--manifest", backup["manifest_path"], "--destination", destination, "--execute")
- if expected == "readable":
- assert code == 0, result
- [goal] = [row for row in result["components"] if row["owner_kind"] == "coordination_authority"]
- assert goal["state"] == "readable" and goal["facts"]["history_commits"] == 20000
- else:
- assert code == 1 and result["error_code"] == "authority_history_capacity", result
- assert not destination.exists()
- assert not list(tmp_path.glob(".loopx-recovery-*"))
+ assert code == 0, result
+ [goal] = [row for row in result["components"] if row["owner_kind"] == "coordination_authority"]
+ assert goal["state"] == "readable"
+ assert goal["facts"]["history_commits"] == last
+ assert goal["facts"]["cursor"] == str(last)
+ assert (destination / "recovery-audit.json").is_file()
def test_digest_refusal_precedes_tar_decoding_and_writes_nothing(tmp_path):
diff --git a/tests/test_usage_goal.py b/tests/test_usage_goal.py
index 24685cc58a..f4b98a293d 100644
--- a/tests/test_usage_goal.py
+++ b/tests/test_usage_goal.py
@@ -86,6 +86,48 @@ def test_disabled_observer_starts_no_worker_or_process(tmp_path, monkeypatch):
pass
+@pytest.mark.parametrize("switch,value", [
+ ("LOOPX_USAGE_PING", "off"), ("DO_NOT_TRACK", "yes"),
+ ("DO_NOT_TRACK", "false"), ("CI", "1"), ("CI", " YES "),
+])
+@pytest.mark.parametrize("entrypoint", ["host", "quota", "direct_cycle"])
+def test_environment_opt_out_skips_host_and_binding_work(tmp_path, monkeypatch, switch, value, entrypoint):
+ monkeypatch.setattr(usage_ping, "select_default_runtime_root", lambda: tmp_path)
+ for name in ("CI", "DO_NOT_TRACK", "LOOPX_USAGE_PING"):
+ monkeypatch.delenv(name, raising=False)
+ monkeypatch.setenv(switch, value)
+ usage_ping.state_path().write_text(json.dumps({"generation": "fixture", "consent": "enabled"}))
+ workers, sends, bindings = [], [], []
+ monkeypatch.setattr(threading.Thread, "start", lambda worker: workers.append(worker.name))
+ monkeypatch.setattr(usage_ping, "_detach", sends.append)
+ if entrypoint == "host":
+ error = ValueError("original Host failure")
+ with pytest.raises(ValueError) as caught:
+ with usage_goal.observe_goal_execution(tmp_path, "fixture-goal"):
+ raise error
+ assert caught.value is error
+ else:
+ registry, rollout = _bound_fixture(tmp_path, monkeypatch)
+ original = usage_goal._bound_codex_session
+
+ def read_binding(*args):
+ result = original(*args)
+ bindings.append(result)
+ return result
+
+ monkeypatch.setattr(usage_goal, "_bound_codex_session", read_binding)
+ if entrypoint == "quota":
+ usage_goal.observe_quota_cycle(registry_path=registry, runtime_root=tmp_path, goal_id="goal",
+ agent_id="agent", turn_id="turn", phase="start", at=0, host="codex-app")
+ else:
+ usage_goal._dispatch_cycle({"path": str(usage_ping.state_path()), "generation": "fixture",
+ "registry": str(registry), "runtime": str(tmp_path), "goal": "goal", "agent": "agent",
+ "turn": "turn", "phase": "start", "at": 0, "host": "codex-app"})
+ assert workers == []
+ assert sends == []
+ assert bindings == []
+
+
def test_periodic_observation_does_not_need_turn_completion(tmp_path, monkeypatch):
monkeypatch.setattr(usage_ping, "select_default_runtime_root", lambda: tmp_path)
usage_ping.state_path().write_text(json.dumps({"generation": "fixture", "consent": "enabled"}))
diff --git a/tests/test_usage_ping.py b/tests/test_usage_ping.py
index 960bb87a3d..5a6995a166 100644
--- a/tests/test_usage_ping.py
+++ b/tests/test_usage_ping.py
@@ -6,6 +6,7 @@
import os
import runpy
import select
+import shutil
import socket
import subprocess
import sys
@@ -310,6 +311,90 @@ def test_first_disclosure_respects_policy_overrides(isolated, monkeypatch, capsy
assert capsys.readouterr().err == ''
+@pytest.mark.parametrize('switch,value', [
+ ('LOOPX_USAGE_PING', 'off'), ('LOOPX_USAGE_PING', ' FALSE '),
+ ('LOOPX_USAGE_PING', '\ufeffOFF\ufeff'), ('DO_NOT_TRACK', 'yes'),
+ ('DO_NOT_TRACK', 'false'), ('CI', '1'), ('CI', ' YES '),
+])
+@pytest.mark.parametrize('current_notice', [False, True])
+def test_environment_opt_out_skips_automatic_cli_transport(
+ isolated, monkeypatch, capsys, switch, value, current_notice,
+):
+ # Real business dispatch and enabled hints prevent missing state or consent
+ # from masking automatic disclosure or warm-command transport attempts.
+ usage_ping.state_path().write_text(json.dumps({
+ 'generation': 'fixture', 'consent': 'enabled',
+ 'notice': {'version': 6 if current_notice else 0},
+ }))
+ monkeypatch.setenv(switch, value)
+ attempts = []
+
+ def unavailable_command():
+ attempts.append('transport')
+ raise OSError('fixture transport unavailable')
+
+ monkeypatch.setattr(usage_ping, '_command', unavailable_command)
+ assert main(['version', '--format', 'json']) == 0
+ captured = capsys.readouterr()
+ assert json.loads(captured.out)['ok'] is True
+ assert captured.err == ''
+ assert attempts == []
+
+
+@pytest.mark.parametrize('switch,value', [
+ ('LOOPX_USAGE_PING', 'off'), ('DO_NOT_TRACK', 'yes'), ('CI', '1'),
+])
+@pytest.mark.parametrize('producer', ['finish', 'verified_return', 'direct', 'python_child'])
+def test_environment_opt_out_fences_late_or_direct_observation(
+ isolated, monkeypatch, switch, value, producer,
+):
+ usage_ping.state_path().write_text(json.dumps({
+ 'generation': 'fixture', 'consent': 'enabled', 'notice': {'version': 6},
+ }))
+ monkeypatch.setenv(switch, value)
+ monkeypatch.setattr(subprocess, 'Popen', lambda *a, **k: pytest.fail('opt-out spawned a child'))
+ monkeypatch.setattr(usage_ping, '_pin_caller_paths', lambda *a: pytest.fail('opt-out resolved caller paths'))
+ if producer == 'finish':
+ usage_ping.finish(('fixture', time.monotonic()), 'version', 0)
+ elif producer == 'verified_return':
+ usage_ping.observe_verified_return()
+ else:
+ usage_ping._detach({'path': 'caller-relative-state'}, command=(
+ [sys.executable, '-P', '-m', 'loopx.usage_goal'] if producer == 'python_child' else None
+ ))
+
+
+def test_environment_hint_matches_the_typescript_owner():
+ node = shutil.which('node')
+ if node is None:
+ pytest.skip('TypeScript owner parity requires the supported Node runtime')
+ # DNT differs from CI/PING. JS trims FEFF but not U+0085; Python's default
+ # strip has the opposite behavior for these two characters.
+ values = ['', '0', '1', 'false', 'true', 'no', 'off', 'yes', 'unknown',
+ ' FALSE ', ' OFF ', ' YES ', '\ufeff0\ufeff', '\ufeffOFF\ufeff',
+ '\ufefffalse\ufeff', '\u00850\u0085', '\u0085off\u0085']
+ environments = [{}] + [{key: value} for key in ('LOOPX_USAGE_PING', 'DO_NOT_TRACK', 'CI') for value in values]
+ environments += [
+ {'LOOPX_USAGE_PING': 'yes', 'DO_NOT_TRACK': '0', 'CI': 'false'},
+ {'LOOPX_USAGE_PING': 'off', 'DO_NOT_TRACK': 'yes', 'CI': '1'},
+ ]
+ owner = (Path(usage_ping.__file__).parent / 'control_plane/runtime/usage_statistics.ts').as_uri()
+ script = f'''import {{ blockedBy }} from {json.dumps(owner)};
+let input=''; for await (const chunk of process.stdin) input+=chunk;
+const reasons=JSON.parse(input).map(env=>blockedBy({{consent:'disabled'}},
+ {{env,version:'1.0.0',python:'3.13',channel:'source'}},'unused'));
+process.stdout.write(JSON.stringify(reasons));'''
+ result = subprocess.run([node, '--no-warnings', '--experimental-strip-types', '--input-type=module', '-e', script],
+ input=json.dumps(environments), capture_output=True, text=True, timeout=20, check=True)
+ reasons = json.loads(result.stdout)
+ assert len(reasons) == len(environments)
+ for env, reason in zip(environments, reasons):
+ # Disabled consent makes the existing owner return before filesystem or
+ # sending when no environment suppressor exists; this does not enable it.
+ expected = reason in {'LOOPX_USAGE_PING', 'DO_NOT_TRACK', 'CI'}
+ assert usage_ping._environment_suppressed(env) is expected, (env, reason)
+
+
def test_real_agent_cli_keeps_json_clean_and_sends_only_after_disclosure(isolated, collector, monkeypatch):
endpoint, received, accepted, release = collector
monkeypatch.setenv('LOOPX_USAGE_PING_ENDPOINT', endpoint)
'
+ return None, "text/html", listings.encode()
+ if target == url and fault in redirect_targets:
+ return redirect_targets[fault], None, b""
+ if target == "https://unrelated.example/foreign.png" and fault == "foreign_round_trip":
+ return "https://source.example/unlisted.png", None, b""
+ if target == url and fault == "stopped_after_fetch":
+ transport.admit("notes-app", provider.event("notes-app", "stop-during-image-fetch", "/stop"))
+ return None, "image/png", PNG_BYTES
+
+ fetched, resolutions = source_https(respond)
+ images_delivered = fault in ("none", "listed_redirect")
+ io = ImageIO(provider)
+ io.corrupt_download = fault in ("corrupt_readback", "legacy_cached_readback")
+ upload_previewed = False
+ control_committed = False
+ source_edited_after_intent = False
+ if fault == "edited_after_upload_intent":
+ from loopx.extensions.lark import return_files
+
+ write_receipt = return_files._write
+
+ def edit_after_upload_intent(path, value):
+ nonlocal source_edited_after_intent
+ write_receipt(path, value)
+ if value.get("upload_started") and not source_edited_after_intent:
+ assert json.loads(path.read_text())["upload_started"] is True
+ provider.messages[source["message_id"]]["content"] = "The source changed after upload intent."
+ source_edited_after_intent = True
+
+ monkeypatch.setattr(return_files, "_write", edit_after_upload_intent)
+
+ if fault in ("stopped_before_upload_intent", "closed_before_upload_intent"):
+ from loopx.extensions.lark import return_files
+ write_receipt = return_files._write
+
+ def control_before_upload_intent(path, value):
+ nonlocal control_committed
+ if value.get("upload_started") and not control_committed:
+ control_committed = True
+ if stopped:
+ transport.admit("notes-app", provider.event("notes-app", "stop-before-resource-intent", "/stop"))
+ assert any(row.get("command") == "stop" for row in transport.core.source_controls())
+ else:
+ assert runtime.close_session(sid)
+ assert store.load_session(sid)["status"] == "closed"
+ return write_receipt(path, value)
+
+ monkeypatch.setattr(return_files, "_write", control_before_upload_intent)
+
+ if fault == "closed_on_source_controls_read":
+ read_controls = transport.core.source_controls
+
+ def close_during_control_read():
+ nonlocal control_committed
+ if upload_previewed and not control_committed:
+ control_committed = True
+ assert runtime.close_session(sid)
+ assert store.load_session(sid)["status"] == "closed"
+ return read_controls()
+
+ monkeypatch.setattr(transport.core, "source_controls", close_during_control_read)
+
+ def media(args, cwd=None, timeout=None):
+ nonlocal upload_previewed, control_committed
+ if "images" in args and "create" in args and "--dry-run" in args:
+ upload_previewed = True
+ if fault == "closed_on_upload_preview":
+ runtime.close_session(sid)
+ if (fault in ("stopped_on_upload_source_read", "closed_on_upload_source_read")
+ and upload_previewed and not control_committed and (
+ ("+messages-mget" in args and source["message_id"] in args) or
+ f"/open-apis/im/v1/messages/{source['message_id']}" in args)):
+ control_committed = True
+ if stopped:
+ transport.admit("notes-app", provider.event("notes-app", "stop-during-source-read", "/stop"))
+ assert any(control.get("command") == "stop" for control in transport.core.source_controls())
+ else:
+ runtime.close_session(sid)
+ assert store.load_session(sid)["status"] == "closed"
+ if fault == "upload_denied" and "images" in args and "create" in args and "--dry-run" not in args:
+ return {"returncode": 3, "stderr": '{"ok":false,"error":{"code":99991672}}'}
+ return io(args, cwd, timeout)
+
+ transport.runner = media
+ try:
+ source = provider.event("notes-app", "answer-with-original-image", "Read https://source.example/article")
+ transport.admit("notes-app", source)
+ original = transport.core.pending()[0]
+ sid, tid, ref = (original[key] for key in ("session_id", "turn_id", "request_ref"))
+ completed = runtime.wait_for_turn(session_id=sid, turn_id=tid, timeout_sec=10)
+ assert completed["status"] == "completed"
+ if fault == "changed_request":
+ provider.messages[source["message_id"]]["content"] = "Source URL removed."
+ path = transport.root / f"{ref}.json"
+ assert path in transport.pending_body_paths()
+ assert transport.reconcile_request(path) == 0
+ text_only = json.loads(path.read_text())
+ assert text_only["deliveries"]["terminal"]["verified"] is True
+ assert transport.core.read_request(ref).get("delivery_verified", False) is False
+ assert fetched == [] and io.uploads == 0
+ assert path not in transport.pending_body_paths()
+ assert path in transport.pending_delivery_paths() and transport.is_recovery_path(path)
+ if fault == "stopped":
+ transport.admit("notes-app", provider.event("notes-app", "stop-before-image-work", "/stop"))
+ assert transport.reconcile_request(path) == int(images_delivered)
+ saved = json.loads(path.read_text())
+ assert saved["answer_images"]["status"] == ("delivered" if images_delivered else "unverified")
+ out = [(mid, row, json.loads(row["body"]["content"])["zh_cn"]["content"])
+ for mid, row in provider.messages.items() if mid.startswith("om_out_") and row["msg_type"] == "post"]
+ assert all(row["chat_id"] == source["chat_id"] for _, row, _ in out)
+ assert any("[Source chart](" in nodes[0][0]["text"] for _, _, nodes in out)
+ assert all(not any("![Source chart]" in node.get("text", "") for line in nodes for node in line) for _, _, nodes in out)
+ if fault in ("stopped_on_upload_source_read", "closed_on_upload_source_read", "closed_on_source_controls_read",
+ "stopped_before_upload_intent", "closed_before_upload_intent"):
+ assert control_committed
+ if fault in ("stopped_before_upload_intent", "closed_before_upload_intent"):
+ from loopx.extensions.lark.return_files import _root
+ assert list((_root(transport.runtime_root) / "lark-chat-images").glob("*.json")) == []
+ if fault == "edited_after_upload_intent":
+ from loopx.extensions.lark.return_files import _root
+
+ assert source_edited_after_intent and io.uploads == 0
+ assert list((_root(transport.runtime_root) / "lark-chat-images").glob("*.json")) == []
+ assert "answer_images.terminal" not in saved["deliveries"]
+ if stopped or closed:
+ assert io.uploads == 0 and transport.core.read_request(ref).get("delivery_verified", False) is False
+ assert not any(name.startswith(("answer_images.", "answer_image_notice.")) for name in saved["deliveries"])
+ if stopped:
+ assert any(control.get("command") == "stop" for control in transport.core.source_controls())
+ else:
+ assert store.load_session(sid)["status"] == "closed"
+ assert transport.core.read_request(ref).get("delivery_verified", False) is images_delivered
+ native_images = [node["image_key"] for _, _, nodes in out for line in nodes for node in line if node["tag"] == "img"]
+ assert native_images == list(io.resources)
+ assert all(io.resources[k] == PNG_BYTES for k in native_images)
+ expected_requests = {
+ "foreign_source": [], "changed_request": [], "stopped": [], "unlisted_image": [article_url],
+ "article_redirect": [article_url], "listed_redirect": [article_url, url, "https://source.example/canonical.png"],
+ }.get(fault, [article_url, url])
+ assert fetched == expected_requests
+ assert resolutions == ["source.example"] * len(expected_requests)
+ if fault in {"same_host_redirect", "foreign_redirect", "foreign_round_trip", "query_redirect", "article_redirect"}:
+ assert io.uploads == 0 and native_images == []
+ assert ("图片尚未核验送达" in str(out)) == (not images_delivered and not stopped and not closed)
+ if fault in ("corrupt_readback", "legacy_cached_readback"):
+ io.corrupt_download = False
+ from loopx.chat_store import _atomic_write_json
+ restarted = LarkPrivateConversations(controller=runtime, runtime_root=transport.runtime_root,
+ runner=media, cli_bin="lark-cli")
+ writes = list(provider.writes)
+ assert path not in restarted.pending_body_paths()
+ assert path not in restarted.pending_delivery_paths()
+ assert restarted.reconcile_request(path) == 0
+ assert provider.writes == writes and io.uploads == 1 and fetched == [article_url, url]
+ saved["answer_images"]["next_check_at"] = 0
+ if fault == "legacy_cached_readback":
+ saved["answer_images"].pop("source_urls_verified")
+ _atomic_write_json(path, saved)
+ assert path in restarted.pending_delivery_paths()
+ assert restarted.reconcile_request(path) == 1
+ assert transport.core.read_request(ref)["delivery_verified"] is True
+ assert provider.writes == writes and io.uploads == 1
+ assert fetched == [article_url, url] * (2 if fault == "legacy_cached_readback" else 1)
+ expected_turns = 1
+ if fault == "upload_denied":
+ followup = provider.event("notes-app", "text-after-image-denial", "Answer the next question")
+ retry = transport.admit("notes-app", followup)
+ assert retry["status"] == "durably_accepted"
+ later_ref = identity_ref(transport._binding("notes-app")["provider_ref"], followup["message_id"])
+ later = transport.core.read_request(later_ref)
+ assert later["session_id"] == sid
+ assert runtime.wait_for_turn(session_id=sid, turn_id=later["turn_id"], timeout_sec=10)["status"] == "completed"
+ later_path = transport.root / f"{later_ref}.json"
+ assert path not in transport.pending_body_paths() and path not in transport.pending_delivery_paths()
+ assert later_path in transport.pending_body_paths()
+ assert transport.reconcile_request(later_path) == 1
+ assert transport.core.read_request(later_ref)["delivery_verified"] is True
+ assert transport.core.read_request(ref).get("delivery_verified", False) is False
+ expected_turns = 2
+ packets = [json.loads(line) for line in capture.read_text().splitlines()]
+ assert sum(packet.get("method") == "turn/start" for packet in packets) == expected_turns
+ assert any(row["text"].endswith(authored) for row in store.messages(sid) if row["role"] == "agent")
+ finally:
+ runtime.close()
+
+
+@pytest.mark.parametrize("fault", ["readback", "raw_paired_post", "edited_source", "edited_original_reply", "edit_on_image_get",
+ "edit_on_upload_source_read", "edit_on_next_upload_source_read", "edit_on_send_preview", "edit_on_send_source_read",
+ "edit_reply_after_upload_intent", "edit_source_on_final_pair_read", "edit_source_on_dispatch_lock",
+ "foreign_sender", "changed_bytes", "stop", "unknown_send", "upload_denied", "upload_scope_then_granted",
+ "upload_scope_with_conflicting_stdout", "upload_cli_missing", "upload_cli_fd_limit", "upload_ack_missing",
+ "closed_on_source_controls_read",
+ "unlisted_redirect", "unlisted_cached_redirect"])
+def test_original_worker_queues_only_media_for_a_verified_legacy_answer(ordinary, monkeypatch, source_https, fault): # noqa: F811
+ from hashlib import sha256
+ from loopx.chat_store import _atomic_write_json
+ from test_chat_image_attachments import PNG_BYTES
+ from test_lark_app_image_projection import ImageIO
+
+ resource = pytest.importorskip("resource") if fault == "upload_cli_fd_limit" else None
+ store, runtime, provider, transport = connect(ordinary)
+ fake = ordinary[5]
+ origin = "https://source.example/article"
+ urls = [f"https://source.example/figure-{i}.png" for i in range(1, 5)]
+ figures = {url: PNG_BYTES + bytes([i]) for i, url in enumerate(urls)}
+ answer = f"Original explanation.\n\n\n\n"
+ script = fake.read_text()
+ marker = '"message": "Runtime response.",'
+ assert marker in script
+ fake.write_text(script.replace(marker, f'"message": {answer!r},', 1))
+ io = ImageIO(provider)
+ edited_body = None
+ late_source_edit = False
+ committed_resource_path = None
+ upload_previews = 0
+ send_preview_seen = False
+ denied_uploads = 0
+ scope_granted = False
+ executable_restored = False
+
+ def edit_original_reply():
+ nonlocal edited_body
+ edited_body = {"content": json.dumps({"zh_cn": {"content": [[{"tag": "text", "text": "The original answer was edited."}]]}})}
+ provider.messages[prior_id]["body"] = edited_body
+
+ def provider_io(args, cwd=None, timeout=None):
+ nonlocal upload_previews, send_preview_seen, denied_uploads, late_source_edit
+ image_upload = "images" in args and "create" in args and "--dry-run" not in args
+ pair_read = "/open-apis/im/v1/messages/mget" in args and "--params" in args
+ source_read = (("+messages-mget" in args and event["message_id"] in args)
+ or (pair_read and event["message_id"] in json.loads(args[args.index("--params") + 1])["message_ids"]))
+ if fault == "upload_denied" and image_upload:
+ provider.calls.append(list(args))
+ return {"returncode": 3, "stderr": '{"ok":false,"error":{"type":"authorization","code":99991672}}'}
+ typed_scope_fault = fault in ("upload_scope_then_granted", "upload_scope_with_conflicting_stdout")
+ if image_upload and typed_scope_fault and io.uploads == 1 and not scope_granted:
+ provider.calls.append(list(args))
+ denied_uploads += 1
+ return {"returncode": 3,
+ "stderr": json.dumps({"ok": False, "identity": "bot", "error": {
+ "type": "authorization", "subtype": "app_scope_not_applied", "code": 99991672,
+ "identity": "bot"}}),
+ "stdout": "invalid provider acknowledgement" if fault == "upload_scope_with_conflicting_stdout" else ""}
+ if (fault == "edit_source_on_final_pair_read" and committed_resource_path is not None
+ and not late_source_edit
+ and (("+messages-mget" in args and prior_id in args)
+ or pair_read)):
+ assert json.loads(committed_resource_path.read_text())["upload_started"] is True
+ provider.messages[event["message_id"]]["content"] = "The source changed during the final reply read."
+ late_source_edit = True
+ uploads_before_edit = {"edit_on_upload_source_read": 0, "edit_on_next_upload_source_read": 1}
+ if (fault in uploads_before_edit and upload_previews == uploads_before_edit[fault] + 1 and edited_body is None
+ and io.uploads == uploads_before_edit[fault]
+ and source_read):
+ edit_original_reply()
+ if (fault == "edit_on_send_source_read" and send_preview_seen and edited_body is None
+ and source_read):
+ edit_original_reply()
+ result = io(args, cwd, timeout)
+ if pair_read and fault == "raw_paired_post":
+ body = json.loads(result["stdout"])
+ historical = next(row for row in body["data"]["items"] if row["message_id"] == prior_id)
+ original_rows = json.loads(historical["body"]["content"])["zh_cn"]["content"]
+ historical["body"] = {"content": json.dumps({
+ "title": "", "content_v2": original_rows,
+ "content": [[{"tag": "text", "text": "A rendered preview only."}]],
+ })}
+ result = {**result, "stdout": json.dumps(body)}
+ if "images" in args and "create" in args and "--dry-run" in args:
+ upload_previews += 1
+ if "+messages-send" in args and "--dry-run" in args and "--content" in args:
+ if "原图(按原文顺序)" in args[args.index("--content") + 1]:
+ send_preview_seen = True
+ if fault == "edit_on_send_preview":
+ edit_original_reply()
+ return result
+
+ runner = provider_io
+ launch_faults = {"upload_cli_missing", "upload_cli_fd_limit"}
+ upload_faults = launch_faults | {"upload_ack_missing"}
+ if fault in upload_faults:
+ from loopx.extensions.lark.cli_resolution import LarkCliResolution, build_lark_command_runner
+
+ native_runner = build_lark_command_runner(LarkCliResolution(sys.executable, True, "fixture", None, None))
+ missing_executable = str(fake.with_name("missing-lark-cli"))
+ if resource is not None:
+ import errno
+ import subprocess
+
+ popen = subprocess.Popen
+
+ def spawn(command, *args, **kwargs):
+ if command != [sys.executable, "-c", "pass"] or executable_restored:
+ return popen(command, *args, **kwargs)
+ # Exhaust descriptors only at real Popen pipe creation,
+ # after the source check and durable upload marker.
+ limits = resource.getrlimit(resource.RLIMIT_NOFILE)
+ try:
+ resource.setrlimit(resource.RLIMIT_NOFILE, (0, limits[1]))
+ return popen(command, *args, **kwargs)
+ except OSError as exc:
+ assert exc.errno == errno.EMFILE
+ raise
+ finally:
+ resource.setrlimit(resource.RLIMIT_NOFILE, limits)
+
+ monkeypatch.setattr(subprocess, "Popen", spawn)
+
+ def guarded_provider(args, cwd=None, timeout=None, *, dispatch_guard=None):
+ if dispatch_guard is not None:
+ # Use the real subprocess boundary with a synthetic provider:
+ # an unavailable executable cannot reach resource creation.
+ command = ([missing_executable] if fault == "upload_cli_missing" and not executable_restored
+ else [sys.executable, "-c", "pass"])
+ assert native_runner(command, cwd, timeout, dispatch_guard=dispatch_guard)["returncode"] == 0
+ return provider_io(args, cwd, timeout)
+
+ guarded_provider.dispatch_guard = True
+ runner = guarded_provider
+
+ transport.runner = runner
+
+ unlisted = "https://source.example/unlisted.png"
+
+ def respond(url):
+ if url == origin:
+ return None, "text/html", "".join(f'