-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.py
More file actions
1141 lines (1028 loc) · 47.7 KB
/
driver.py
File metadata and controls
1141 lines (1028 loc) · 47.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
OpenCode Task Driver — Phase 3 核心实现。
替换 Orchestrator._drive_opencode 的 Phase 2 stub,实现完整的 opencode HTTP 交互。
职责:
1. 创建 opencode session
2. 注入 TaskRequest.messages(noReply=True,仅写入上下文)
3. 按 mode(plan_first / direct_execute)路由 agent 并调用 prompt_async
4. 订阅 SSE /global/event,归一化事件写入 DB(insert_event)
5. 权限请求 → HITL DecisionRequest → 轮询 DB → respond_permission
6. plan_first:捕获 plan → plan_ready 事件 → 等待审批 → 继续 / 中止
7. 收集 artifacts(diff JSON + transcript JSON)
8. 优雅 abort 和超时处理
使用方式:
from worker.adapters.opencode.driver import OpenCodeDriver
driver = OpenCodeDriver(task_id, request, host_port, container_env, db)
await driver.run()
"""
from __future__ import annotations
import asyncio
import fnmatch
import json
import logging
import time
import uuid
from pathlib import Path
from typing import Any, Optional, Sequence
import aiosqlite
from worker.adapters.opencode.client import OpenCodeClient
from worker.adapters.opencode.event_stream import (
extract_diff,
extract_permission_request,
is_session_idle,
normalize_opencode_event,
)
from worker.adapters.opencode.interceptors import (
EventInterceptor,
InterceptorArtifact,
InterceptorEvent,
InterceptorRunner,
TerminalSignal,
)
from worker.config import get_settings
from worker.contract.artifact import Artifact, ArtifactType
from worker.contract.decision import DecisionChoice, DecisionKind, DecisionRequest
from worker.contract.event import TaskEventKind
from worker.contract.exceptions import TaskAbortedError, TaskTimedOutError
from worker.contract.task import TaskMode, TaskRequest, TaskStatus
from worker.observability import metrics
from worker.storage.repo import (
expire_decision,
get_resolved_decision,
insert_artifact,
insert_decision,
insert_event,
update_task_status,
)
logger = logging.getLogger(__name__)
# Agent 路由对齐 ADR-001 / ADR-006 / oh-my-openagent 4.1.2:
# - plan_first → "Prometheus" (规划 agent,read-only 工具集)
# - direct_exec → "Sisyphus" (执行 agent,bash/write/edit/webfetch 全开)
#
# 这两个 agent 名称的可用性由容器入口脚本(docker/worker/entrypoint.sh)在
# opencode serve 启动后通过 GET /agent 验证:oh-my 未加载或 agent 缺失时
# 容器启动失败(非零退出码),不会回退到 opencode 内置 "plan"/"build"。
AGENT_PROMETHEUS = "Prometheus"
AGENT_SISYPHUS = "Sisyphus"
# HITL 决策轮询间隔(秒)
_HITL_POLL_INTERVAL = 2.0
# 默认任务超时(resource_limits.timeout_sec 可覆盖)
_DEFAULT_TIMEOUT_SEC = 1800
# P1-15: 连续用户 reject 阈值。opencode 的 reject 是单次拒绝(工具会再 ask),
# 没有计数上限会让 agent 与 reject 形成死循环直到 timeout。
# 达到阈值后自动 abort 并发 mode_escalation_suggested 事件。
# 计数仅累积**用户主动 reject**;approve 重置为 0;abort/timeout 走各自终态分支。
_REJECT_THRESHOLD = 3
class OpenCodeDriver:
"""驱动容器内 opencode 完成一个任务的完整生命周期。"""
def __init__(
self,
task_id: str,
request: TaskRequest,
host_port: int,
container_env: dict[str, str],
db: aiosqlite.Connection,
interceptors: Sequence[EventInterceptor] = (),
):
"""构造 driver。
Args:
interceptors: W2-1 — 事件拦截器列表,默认空(行为与 W2-1 之前完全一致)。
driver 通过 InterceptorRunner 隔离每个拦截器的错误,
单个抛错不影响 SSE 主循环或兄弟拦截器。
"""
self.task_id = task_id
self.request = request
self.host_port = host_port
self.password = container_env.get("OPENCODE_SERVER_PASSWORD", "")
self.db = db
self.settings = get_settings()
self._runner = InterceptorRunner(interceptors)
# 状态
self.client: Optional[OpenCodeClient] = None
self.session_id: Optional[str] = None
# 中止信号:外部 abort 或超时时设置,SSE 消费循环检测后退出
self._abort_event = asyncio.Event()
# abort 来源(与 TaskAbortedError.reason 对齐):在 _abort_event.set 时一并赋值,
# 由 _run_inner 在抛出 TaskAbortedError 时透传给 queue
self._abort_reason: Optional[str] = None
self._abort_decision_id: Optional[str] = None
# 累积 assistant 文本(plan_first 时用于提取 plan)
self._assistant_buffer: list[str] = []
# plan_first 模式下提取到的计划全文
self._plan_text: Optional[str] = None
# 最近一次 session.diff 内容(artifact 收集时使用)
self._last_diff: list = []
# P1-15:连续 reject 计数,达到 _REJECT_THRESHOLD 时自动 abort 防死循环
self._reject_count: int = 0
# 超时时间
self.timeout_sec = _DEFAULT_TIMEOUT_SEC
if request.resource_limits and request.resource_limits.timeout_sec:
self.timeout_sec = request.resource_limits.timeout_sec
# ── 公开入口 ──────────────────────────────────────────────────────────────
async def run(self) -> None:
"""执行完整 Phase 3 驱动逻辑,由 orchestrator._drive_opencode 调用。
异常分类(由 queue._run_one 路由到不同终态):
- TaskTimedOutError: 超过 timeout_sec → task_timed_out 终态
- TaskAbortedError: HITL 决策中止/拒绝 → task_aborted 终态
- 其它 Exception: 内部错误 → task_failed 终态
W2-1:在 finally 块中先于 client 清理触发拦截器终态分发 + flush。
拦截器异常被 InterceptorRunner 隔离,不影响终态语义。
"""
from worker.observability.logging import set_correlation, clear_correlation
set_correlation(task_id=self.task_id)
client = OpenCodeClient(
host="127.0.0.1",
port=self.host_port,
password=self.password,
)
self.client = client
# W2-1:终态信号默认 completed;下面的 except 分支根据异常类型覆盖
final_status: TaskStatus = TaskStatus.completed
final_reason: Optional[str] = None
try:
async with asyncio.timeout(self.timeout_sec):
await self._run_inner()
except asyncio.TimeoutError:
logger.warning("task %s: timed out after %ds", self.task_id, self.timeout_sec)
if self.session_id:
await client.abort_session(self.session_id)
final_status = TaskStatus.timed_out
final_reason = "timeout"
raise TaskTimedOutError(timeout_sec=self.timeout_sec)
except TaskAbortedError as exc:
final_status = TaskStatus.aborted
final_reason = exc.reason
raise
except TaskTimedOutError:
final_status = TaskStatus.timed_out
final_reason = "timeout"
raise
except Exception as exc: # noqa: BLE001 — 显式分类落 final_status
final_status = TaskStatus.failed
final_reason = type(exc).__name__
raise
finally:
# W2-1:终态信号分发 + flush;任何错误均由 runner 内部隔离
await self._dispatch_terminal_and_flush(final_status, final_reason)
if self.session_id:
await client.delete_session(self.session_id)
await client.aclose()
clear_correlation()
# ── 内部主流程 ─────────────────────────────────────────────────────────────
async def _run_inner(self) -> None:
"""核心驱动流程(已在 asyncio.timeout 包装内)。"""
from worker.observability.logging import set_correlation
client = self.client
# Step 1: 创建 opencode session
session_data = await client.create_session()
session_id = session_data.get("id") or session_data.get("sessionId")
if not session_id:
raise RuntimeError(
f"opencode create_session returned no id: {session_data!r}"
)
self.session_id = session_id
set_correlation(session_id=session_id)
await update_task_status(
self.db, self.task_id, TaskStatus.starting_opencode,
opencode_session_id=session_id,
)
logger.info("task %s: opencode session=%s", self.task_id, session_id)
# Step 2: 注入 TaskRequest.messages(历史上下文,noReply=True)
if self.request.messages:
await self._inject_messages(session_id)
# W2-2:注入消息不会反向触发 SSE 事件,需主动合成给拦截器
# 让 ConversationsWriter 等可以看见用户的初始 prompt。
for msg in self.request.messages:
await self._dispatch_to_interceptors(
normalized_kind=f"initial_{msg.role}_message",
normalized_payload={"role": msg.role, "content": msg.content},
raw_type="<synthesized:initial_message>",
)
# Step 3: 按 mode 决定 agent 并更新状态
mode = self.request.mode
profile = self.request.opencode_profile
model = profile.model if profile else None
if mode == TaskMode.plan_first:
await update_task_status(self.db, self.task_id, TaskStatus.planning)
await insert_event(
self.db, self.task_id, TaskEventKind.task_started,
{"phase": "planning"},
)
agent = AGENT_PROMETHEUS
else:
await update_task_status(self.db, self.task_id, TaskStatus.executing)
await insert_event(
self.db, self.task_id, TaskEventKind.execution_started,
{"mode": mode.value},
)
agent = AGENT_SISYPHUS
# Step 4: 启动 SSE 消费(后台 asyncio Task)+ REST 轮询(并行)
# opencode 1.14.30 的 /global/event 只传心跳;session idle 必须通过
# REST 轮询 GET /session/{id}/message 检测(info.time.completed 有值)
sse_task = asyncio.create_task(
self._consume_sse(session_id),
name=f"sse-{self.task_id[:8]}",
)
poll_task = asyncio.create_task(
self._poll_session_idle(session_id),
name=f"poll-{self.task_id[:8]}",
)
try:
# Step 5: 投递 prompt_async(opencode 开始执行)
prompt_parts = self._build_prompt_parts()
await client.prompt_async(
session_id=session_id,
parts=prompt_parts,
agent=agent,
model=model,
)
logger.info(
"task %s: prompt_async sent (agent=%s, mode=%s)",
self.task_id, agent, mode.value,
)
# Step 6: SSE(错误/权限检测)与 REST 轮询并行,任一完成即退出
done, pending = await asyncio.wait(
[sse_task, poll_task],
return_when=asyncio.FIRST_COMPLETED,
)
# 取消未完成的任务
for t in pending:
t.cancel()
try:
await t
except (asyncio.CancelledError, Exception):
pass
# 检查是否有异常(session.error 由 _consume_sse 抛出)
for t in done:
exc = t.exception()
if exc is not None:
raise exc
except Exception:
for t in [sse_task, poll_task]:
if not t.done():
t.cancel()
try:
await t
except (asyncio.CancelledError, Exception):
pass
raise
# Step 7: plan_first 模式下等待人工审批计划
if mode == TaskMode.plan_first and not self._abort_event.is_set():
await self._handle_plan_approval(session_id)
# Step 8: 收集产物(diff + transcript)
if not self._abort_event.is_set():
await self._collect_artifacts(session_id)
# abort_event 由权限超时或 plan 拒绝设置,抛出让 queue 写 task_aborted
if self._abort_event.is_set():
raise TaskAbortedError(
reason=self._abort_reason or "system",
decision_id=self._abort_decision_id,
)
# ── REST 轮询检测 session idle ────────────────────────────────────────────
async def _poll_session_idle(self, session_id: str) -> None:
"""轮询 GET /session/{id}/message,直到最后一条 assistant 消息完成。
opencode 1.14.30 的 /global/event SSE 只发送心跳;真正的任务完成信号
需要通过检查 message.info.time.completed 字段来检测。
当检测到完成时,此协程正常返回(_run_inner 会取消 _consume_sse)。
"""
poll_interval = 5.0 # 每 5 秒轮询一次
logger.info("task %s: starting REST poll for session idle (interval=%.0fs)",
self.task_id, poll_interval)
while not self._abort_event.is_set():
await asyncio.sleep(poll_interval)
if self._abort_event.is_set():
break
try:
messages = await self.client.get_messages(session_id)
if self._is_session_messages_idle(messages):
logger.info("task %s: session idle detected via REST poll "
"(messages=%d)", self.task_id, len(messages))
return
except asyncio.CancelledError:
raise
except Exception as exc:
logger.debug("task %s: REST poll error: %s", self.task_id, exc)
@staticmethod
def _is_session_messages_idle(messages: list) -> bool:
"""判断 session 消息列表是否表示任务已完成。
opencode 在 assistant 消息完成时会在 info.time.completed 写入时间戳。
检测最后一条 assistant 消息是否已有 completed 时间戳。
"""
if not messages:
return False
for msg in reversed(messages):
info = msg.get("info", {})
if not isinstance(info, dict):
continue
if info.get("role") == "assistant":
time_info = info.get("time", {})
return bool(isinstance(time_info, dict) and time_info.get("completed"))
return False
# ── SSE 消费循环 ──────────────────────────────────────────────────────────
async def _consume_sse(self, session_id: str) -> None:
"""持续消费 opencode SSE 事件直到 session idle 或 abort 信号。
事件处理优先级:
1. abort_event → 退出
2. session.idle / session.status=idle → 退出(任务完成)
3. session.diff → 更新 _last_diff
4. permission 请求 → 派发 _handle_permission Task
5. assistant_delta / tool_call_* → 写入 DB 事件
"""
_pending_perm_ids: set[str] = set() # 防止同一 permission_id 重复处理
# direct_execute 模式:权限请求频率计数(达到阈值时发 mode_escalation_suggested)
_perm_ask_count = 0
_MODE_ESCALATION_THRESHOLD = 3
try:
async for raw_event in self.client.stream_events():
if self._abort_event.is_set():
break
# 检查 session idle(任务完成)
if is_session_idle(raw_event):
logger.info("task %s: session idle → SSE done", self.task_id)
break
# 检查 session.error(prompt_async 失败时快速退出)
if raw_event.get("type") == "session.error":
err_msg = (
raw_event.get("payload", {}).get("error")
or raw_event.get("payload", {}).get("message")
or str(raw_event.get("payload", ""))
)
logger.error(
"task %s: session.error from opencode: %s",
self.task_id, err_msg,
)
raise RuntimeError(f"opencode session.error: {err_msg}")
# 捕获 diff 快照(最新版本)
diff = extract_diff(raw_event)
if diff is not None:
self._last_diff = diff
# 检查权限请求(去重)
perm_req = extract_permission_request(raw_event)
if perm_req:
pid = perm_req["permission_id"]
if pid not in _pending_perm_ids:
_pending_perm_ids.add(pid)
_perm_ask_count += 1
asyncio.create_task(
self._handle_permission(session_id, perm_req),
name=f"perm-{self.task_id[:8]}-{pid}",
)
# direct_execute 模式:频繁 ask 时发 mode_escalation_suggested
if (
self.request.mode == TaskMode.direct_execute
and _perm_ask_count == _MODE_ESCALATION_THRESHOLD
):
await insert_event(
self.db, self.task_id,
TaskEventKind.mode_escalation_suggested,
{
"reason": "repeated_permission_asks",
"ask_count": _perm_ask_count,
"suggestion": "Consider resubmitting with mode=plan_first",
},
)
continue
# 归一化并写入 Worker 事件
norm = normalize_opencode_event(raw_event)
# W2-1:分发给拦截器(在 normalize 之后、insert_event 之前)
# 即使 norm 为 None(心跳/sync)也允许拦截器看 raw_payload
# — 但当前 driver 在 norm is None 时 continue,故只在 norm 非 None
# 时通知拦截器。后续 W2-4 若需要拿到非归一化事件可再扩展。
if norm is not None:
await self._dispatch_to_interceptors(
normalized_kind=norm.kind,
normalized_payload=dict(norm.payload),
raw_type=norm.raw_type,
raw_payload=dict(raw_event.get("payload") or {}),
)
if norm is None:
continue
# 累积 assistant 文本(plan_first 时用于提取 plan)
if norm.kind == "assistant_delta":
text = norm.payload.get("content", "")
if text:
self._assistant_buffer.append(text)
try:
kind = TaskEventKind(norm.kind)
await insert_event(self.db, self.task_id, kind, norm.payload)
except ValueError:
logger.debug(
"task %s: unknown event kind %r, skipping",
self.task_id, norm.kind,
)
except asyncio.CancelledError:
raise
except Exception as exc:
# SSE 断开:记录警告后退出,不向上抛出
# orchestrator 会在 collecting_artifacts 阶段检测最终状态
logger.warning("task %s: SSE stream error: %s", self.task_id, exc)
# plan_first:SSE 完成后将累积文本保存为 plan
if self.request.mode == TaskMode.plan_first and self._assistant_buffer:
self._plan_text = "".join(self._assistant_buffer)
# ── 权限 HITL 处理 ────────────────────────────────────────────────────────
async def _handle_permission(
self,
session_id: str,
perm_req: dict[str, Any],
) -> None:
"""处理 opencode 权限请求的完整 HITL 流程。
流程:
1. 写入 DecisionRequest → DB,发出 hitl_required 事件
2. 更新状态 → awaiting_human
3. 轮询 DB,直到 DecisionResponse 出现或超时
4. 映射 choice → opencode response("once" / "reject")
5. POST respond_permission 回传给 opencode
6. 恢复执行状态
"""
permission_id = perm_req["permission_id"]
tool = perm_req.get("tool", "unknown")
# P1-14:auto_approve 短路——若 hitl_policy.auto_approve 命中此 kind:tool,
# 直接 respond "once" 并写 decision_received(auto_approved=True),
# 跳过 hitl_required / awaiting_human 全流程
matched_pattern = self._match_auto_approve(
DecisionKind.tool_permission, tool,
)
if matched_pattern is not None:
await self._auto_approve_permission(
session_id, permission_id, tool, matched_pattern,
)
return
hitl_policy = self.request.hitl_policy
timeout_sec = hitl_policy.decision_timeout_sec if hitl_policy else 600
on_timeout_action, default_timeout_choice = self._resolve_hitl_timeout_policy()
decision_id = str(uuid.uuid4())
decision_req = DecisionRequest(
decision_id=decision_id,
kind=DecisionKind.tool_permission,
summary=(
f"opencode 请求执行工具:{tool}\n"
f"{perm_req.get('description', '')}"
),
options=[
DecisionChoice.approve,
DecisionChoice.reject,
DecisionChoice.abort,
],
default_on_timeout=default_timeout_choice,
expires_at=None,
context={
"tool": tool,
"permission_id": permission_id,
"args": perm_req.get("args", {}),
"title": perm_req.get("title", ""),
},
)
await insert_decision(self.db, self.task_id, decision_req)
await insert_event(
self.db, self.task_id, TaskEventKind.hitl_required,
{
"decision_id": decision_id,
"kind": DecisionKind.tool_permission.value,
"summary": decision_req.summary,
"permission_id": permission_id,
},
)
await update_task_status(self.db, self.task_id, TaskStatus.awaiting_human)
# 轮询等待决策(P1-11:观测 HITL 等待耗时)
_hitl_t0 = time.monotonic()
resolved = await self._wait_for_decision(decision_id, timeout_sec)
metrics.observe_hitl_wait(time.monotonic() - _hitl_t0)
# 映射 choice → opencode response
opencode_response = "reject" # 保守默认
if resolved is not None and resolved.response is not None:
choice = resolved.response.choice
if choice == DecisionChoice.approve:
opencode_response = "once"
# P1-15:approve 重置连续 reject 计数
self._reject_count = 0
elif choice == DecisionChoice.abort:
self._signal_abort("permission_rejected", decision_id)
opencode_response = "reject"
else:
# reject / revise:用户拒绝本次请求
opencode_response = "reject"
self._reject_count += 1
# P1-15:累积达到阈值即 abort,发 mode_escalation_suggested 提示上游
if self._reject_count >= _REJECT_THRESHOLD:
await insert_event(
self.db, self.task_id,
TaskEventKind.mode_escalation_suggested,
{
"reason": "reject_threshold_exceeded",
"reject_count": self._reject_count,
"threshold": _REJECT_THRESHOLD,
"suggestion": (
"Repeated rejects detected; aborting to break loop. "
"Consider resubmitting with a different mode or "
"scope-limited permissions."
),
},
)
self._signal_abort("reject_threshold_exceeded", decision_id)
logger.warning(
"task %s: reject threshold reached (%d/%d), auto-aborting",
self.task_id, self._reject_count, _REJECT_THRESHOLD,
)
else:
# 超时:标记 DB decision 为 timed_out,发 hitl_timeout 事件,按策略处理
await expire_decision(self.db, decision_id)
await insert_event(
self.db, self.task_id, TaskEventKind.hitl_timeout,
{
"decision_id": decision_id,
"kind": DecisionKind.tool_permission.value,
"permission_id": permission_id,
"on_timeout": on_timeout_action,
"resolved_choice": default_timeout_choice.value,
},
)
if default_timeout_choice == DecisionChoice.abort:
self._signal_abort("hitl_timeout", decision_id)
opencode_response = "reject"
else:
opencode_response = "once"
logger.warning(
"task %s: permission HITL timeout (decision_id=%s, on_timeout=%s, resolved_choice=%s)",
self.task_id,
decision_id,
on_timeout_action,
default_timeout_choice.value,
)
# 回传 opencode
try:
await self.client.respond_permission(session_id, permission_id, opencode_response)
decision_payload = {
"decision_id": decision_id,
"choice": opencode_response,
"permission_id": permission_id,
}
await insert_event(
self.db, self.task_id, TaskEventKind.decision_received,
decision_payload,
)
# W2-1:合成 decision_received 事件分发给拦截器
await self._dispatch_to_interceptors(
normalized_kind=TaskEventKind.decision_received.value,
normalized_payload=decision_payload,
raw_type="<synthesized:decision_received>",
)
except Exception as exc:
logger.warning(
"task %s: respond_permission %s failed: %s",
self.task_id, permission_id, exc,
)
# 恢复执行状态(若未 abort)
if not self._abort_event.is_set():
if self.request.mode == TaskMode.plan_first:
await update_task_status(self.db, self.task_id, TaskStatus.planning)
else:
await update_task_status(self.db, self.task_id, TaskStatus.executing)
# ── plan_first 审批 ───────────────────────────────────────────────────────
async def _handle_plan_approval(self, session_id: str) -> None:
"""plan_first 模式:发出 plan_ready 事件并等待人工审批。
approve → execution_started → 继续使用 Sisyphus 执行
reject / abort → 抛出异常,任务终止
revise → 当前版本等同 reject(Phase 5 再实现完整 revise 流程)
"""
plan_text = self._plan_text or ""
hitl_policy = self.request.hitl_policy
timeout_sec = hitl_policy.decision_timeout_sec if hitl_policy else 600
on_timeout_action, default_timeout_choice = self._resolve_hitl_timeout_policy()
decision_id = str(uuid.uuid4())
# 发出 plan_ready 事件
await insert_event(
self.db, self.task_id, TaskEventKind.plan_ready,
{"plan_text": plan_text, "decision_id": decision_id},
)
# 创建 HITL 审批请求
decision_req = DecisionRequest(
decision_id=decision_id,
kind=DecisionKind.plan_approval,
summary="Prometheus 已生成执行计划,请审批后继续。",
options=[
DecisionChoice.approve,
DecisionChoice.reject,
DecisionChoice.revise,
DecisionChoice.abort,
],
default_on_timeout=default_timeout_choice,
expires_at=None,
context={"plan_text": plan_text},
)
await insert_decision(self.db, self.task_id, decision_req)
await insert_event(
self.db, self.task_id, TaskEventKind.hitl_required,
{
"decision_id": decision_id,
"kind": DecisionKind.plan_approval.value,
"summary": decision_req.summary,
},
)
await update_task_status(self.db, self.task_id, TaskStatus.awaiting_human)
# 轮询等待决策(P1-11:观测 HITL 等待耗时)
_hitl_t0 = time.monotonic()
resolved = await self._wait_for_decision(decision_id, timeout_sec)
metrics.observe_hitl_wait(time.monotonic() - _hitl_t0)
choice_val: Optional[str] = None
if resolved is not None and resolved.response is not None:
choice_val = resolved.response.choice.value
else:
# 超时:标记 DB decision 为 timed_out,发 hitl_timeout 事件
await expire_decision(self.db, decision_id)
choice_val = default_timeout_choice.value
await insert_event(
self.db, self.task_id, TaskEventKind.hitl_timeout,
{
"decision_id": decision_id,
"kind": DecisionKind.plan_approval.value,
"on_timeout": on_timeout_action,
"resolved_choice": choice_val,
},
)
logger.warning(
"task %s: plan approval HITL timeout (decision_id=%s, on_timeout=%s, resolved_choice=%s)",
self.task_id,
decision_id,
on_timeout_action,
choice_val,
)
decision_payload = {"decision_id": decision_id, "choice": choice_val}
await insert_event(
self.db, self.task_id, TaskEventKind.decision_received,
decision_payload,
)
# W2-1:plan_approval 决策合成事件分发给拦截器
await self._dispatch_to_interceptors(
normalized_kind=TaskEventKind.decision_received.value,
normalized_payload=decision_payload,
raw_type="<synthesized:decision_received>",
)
if choice_val == DecisionChoice.approve.value:
# 计划批准 → 切换到 executing,继续用 Sisyphus 执行
await update_task_status(self.db, self.task_id, TaskStatus.executing)
await insert_event(
self.db, self.task_id, TaskEventKind.execution_started,
{"mode": TaskMode.plan_first.value, "plan_approved": True},
)
profile = self.request.opencode_profile
model = profile.model if profile else None
# 重置 assistant buffer,在同一 session 继续监听 SSE
self._assistant_buffer = []
self._plan_text = None
sse_task = asyncio.create_task(
self._consume_sse(session_id),
name=f"sse-exec-{self.task_id[:8]}",
)
try:
await self.client.prompt_async(
session_id=session_id,
parts=[{"type": "text", "text": "/start-work"}],
agent=AGENT_SISYPHUS,
model=model,
)
await sse_task
except Exception:
sse_task.cancel()
try:
await sse_task
except (asyncio.CancelledError, Exception):
pass
raise
else:
# reject / revise / abort → 终止任务
self._signal_abort("plan_rejected", decision_id)
raise TaskAbortedError(
reason="plan_rejected",
message=f"plan rejected/aborted by user (choice={choice_val!r})",
decision_id=decision_id,
)
# ── 产物收集 ──────────────────────────────────────────────────────────────
async def _collect_artifacts(self, session_id: str) -> None:
"""收集 diff 和 transcript 产物,写文件并注册到 DB。"""
import time as _time
settings = self.settings
artifacts_dir = settings.artifacts_dir / self.task_id
artifacts_dir.mkdir(parents=True, exist_ok=True)
now = _time.time()
retention_secs = settings.artifact_retention_days * 86400
# 1. Diff artifact
try:
diff_data = await self.client.get_diff(session_id)
if not diff_data:
diff_data = self._last_diff # 回退到 SSE 中缓存的最后 diff
if diff_data:
diff_path = artifacts_dir / "changes.diff.json"
diff_path.write_text(
json.dumps(diff_data, indent=2, ensure_ascii=False)
)
artifact_id = str(uuid.uuid4())
artifact = Artifact(
artifact_id=artifact_id,
task_id=self.task_id,
type=ArtifactType.diff,
filename="changes.diff.json",
size=diff_path.stat().st_size,
created_at=now,
expires_at=now + retention_secs,
)
await insert_artifact(
self.db, artifact, file_path=str(diff_path)
)
await insert_event(
self.db, self.task_id, TaskEventKind.artifact_ready,
{
"artifact_id": artifact_id,
"type": ArtifactType.diff.value,
"filename": "changes.diff.json",
},
)
logger.info(
"task %s: diff artifact saved (%d bytes)",
self.task_id, diff_path.stat().st_size,
)
except Exception as exc:
logger.warning("task %s: diff artifact failed: %s", self.task_id, exc)
# 2. Transcript artifact
try:
messages = await self.client.get_messages(session_id)
if messages:
transcript_path = artifacts_dir / "transcript.json"
transcript_path.write_text(
json.dumps(messages, indent=2, ensure_ascii=False)
)
artifact_id = str(uuid.uuid4())
artifact = Artifact(
artifact_id=artifact_id,
task_id=self.task_id,
type=ArtifactType.transcript,
filename="transcript.json",
size=transcript_path.stat().st_size,
created_at=now,
expires_at=now + retention_secs,
)
await insert_artifact(
self.db, artifact, file_path=str(transcript_path)
)
await insert_event(
self.db, self.task_id, TaskEventKind.artifact_ready,
{
"artifact_id": artifact_id,
"type": ArtifactType.transcript.value,
"filename": "transcript.json",
},
)
except Exception as exc:
logger.warning("task %s: transcript artifact failed: %s", self.task_id, exc)
# ── 辅助方法 ──────────────────────────────────────────────────────────────
async def _inject_messages(self, session_id: str) -> None:
"""将 TaskRequest.messages 注入 session(noReply=True,仅写上下文)。"""
for msg in self.request.messages:
if msg.role == "user":
await self.client.send_message(
session_id=session_id,
parts=[{"type": "text", "text": msg.content}],
no_reply=True,
)
def _build_prompt_parts(self) -> list[dict[str, Any]]:
"""构建 prompt_async 的 parts 参数。
取 TaskRequest.messages 中最后一条 user 消息作为 prompt 触发内容;
若 messages 为空,使用 "Begin task." 作为默认触发。
"""
if self.request.messages:
last_user = next(
(m for m in reversed(self.request.messages) if m.role == "user"),
None,
)
if last_user:
return [{"type": "text", "text": last_user.content}]
return [{"type": "text", "text": "Begin task."}]
def _signal_abort(self, reason: str, decision_id: Optional[str] = None) -> None:
"""记录 abort 来源并触发 _abort_event。
在多次触发时保留首个 reason(首次决定终态原因),后续仅刷新 decision_id 不变。
"""
if not self._abort_event.is_set():
self._abort_reason = reason
self._abort_decision_id = decision_id
self._abort_event.set()
# ── W2-1: 拦截器分发辅助 ──────────────────────────────────────────────────
async def _dispatch_to_interceptors(
self,
normalized_kind: Optional[str],
normalized_payload: Optional[dict[str, Any]],
raw_type: str,
raw_payload: Optional[dict[str, Any]] = None,
) -> None:
"""构造 InterceptorEvent 并分发给 runner。
decision/permission 路径合成事件时,raw_type 以 ``<synthesized:...>`` 前缀
标记,便于拦截器按需过滤。"""
if not self._runner.interceptors:
return
ic_event = InterceptorEvent(
task_id=self.task_id,
session_id=self.session_id,
normalized_kind=normalized_kind,
normalized_payload=normalized_payload,
raw_type=raw_type,
raw_payload=raw_payload or {},
received_at=time.monotonic(),
)
await self._runner.dispatch_event(ic_event)
async def _dispatch_terminal_and_flush(
self, status: TaskStatus, reason: Optional[str],
) -> None:
"""终态分发 + flush 收集 + 标准登记产物。
三段都包在 try/except 中:拦截器侧任何故障都不传染到 driver 主路径。
"""
if not self._runner.interceptors:
return
signal = TerminalSignal(
task_id=self.task_id,
session_id=self.session_id,
status=status.value,
reason=reason,
ended_at=time.monotonic(),
)
try:
await self._runner.dispatch_terminal(signal)
except Exception: # noqa: BLE001 — runner 内部已隔离;这里再做一层防御
logger.exception("task %s: terminal dispatch outer failure", self.task_id)
try:
artifacts = await self._runner.collect_artifacts()
except Exception: # noqa: BLE001
logger.exception("task %s: collect_artifacts outer failure", self.task_id)
return
for art in artifacts:
try:
await self._register_interceptor_artifact(art)
except Exception:
logger.exception(
"task %s: register interceptor artifact failed (%s)",
self.task_id, art.filename,
)
async def _register_interceptor_artifact(
self, art: InterceptorArtifact,
) -> None:
"""走标准 insert_artifact + artifact_ready 路径登记拦截器产物。
校验:
1. local_path 必须落在 settings.artifacts_dir / task_id 子树内
(复用 P0-8 同款防越权约束)
2. 文件必须存在(否则 stat 抛 → 上层 catch 后跳过)
3. ArtifactType 必须合法
"""
artifacts_root = (self.settings.artifacts_dir / self.task_id).resolve()
target = Path(art.local_path).resolve()
try:
target.relative_to(artifacts_root)
except ValueError:
logger.error(
"task %s: interceptor artifact rejected (path escape): %s outside %s",
self.task_id, target, artifacts_root,
)
return
if not target.exists():
logger.error(
"task %s: interceptor artifact rejected (file missing): %s",
self.task_id, target,
)
return
try:
artifact_type = ArtifactType(art.artifact_type)
except ValueError:
logger.error(
"task %s: interceptor artifact rejected (unknown type): %r",
self.task_id, art.artifact_type,
)
return
size = art.size_bytes if art.size_bytes is not None else target.stat().st_size
now = time.time()
retention_secs = self.settings.artifact_retention_days * 86400
artifact_id = str(uuid.uuid4())