From 27334fef818cf24c561a9ca69a1e434e5d67ea18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E7=99=BD=E5=8F=94?= <11794527+absfan@user.noreply.gitee.com> Date: Sat, 31 Oct 2026 10:17:26 +0800 Subject: [PATCH] feat(dsh): add CAD verification recovery diff and issue broker --- .../plans/2026-10-31-dsh-production-chain.md | 102 ++++++++++++++++++ .../specs/2026-10-31-dsh-production-chain.md | 30 ++++++ .../2026-10-31-dsh-production-chain.md | 18 ++++ docs/user/dsh-ai-supervision.md | 4 +- integration/dsh/task-plugin/model_results.mjs | 3 +- src/dx_productivity/dsh/cad_verification.py | 61 +++++++++++ src/dx_productivity/dsh/execution_runs.py | 6 ++ src/dx_productivity/dsh/failure_policy.py | 23 ++++ src/dx_productivity/dsh/health.py | 9 +- src/dx_productivity/dsh/incidents.py | 9 +- src/dx_productivity/dsh/issue_broker.py | 54 ++++++++++ src/dx_productivity/dsh/issue_transport.py | 10 +- src/dx_productivity/dsh/resume_diff.py | 28 +++++ src/dx_productivity/dsh/run_journal.py | 5 +- src/dx_productivity/dsh/task_lifecycle.py | 3 + .../ui/dsh_task_lifecycle.py | 4 +- tests/dsh/test_cad_verification.py | 45 ++++++++ tests/dsh/test_failure_policy.py | 14 +++ tests/dsh/test_incidents.py | 1 + tests/dsh/test_issue_broker.py | 32 ++++++ tests/dsh/test_issue_transport.py | 6 +- tests/dsh/test_resume_diff.py | 32 ++++++ tools/dsh_issue_broker.py | 66 ++++++++++++ 23 files changed, 553 insertions(+), 12 deletions(-) create mode 100644 docs/superpowers/plans/2026-10-31-dsh-production-chain.md create mode 100644 docs/superpowers/specs/2026-10-31-dsh-production-chain.md create mode 100644 docs/testing/2026-10-31-dsh-production-chain.md create mode 100644 src/dx_productivity/dsh/cad_verification.py create mode 100644 src/dx_productivity/dsh/failure_policy.py create mode 100644 src/dx_productivity/dsh/issue_broker.py create mode 100644 src/dx_productivity/dsh/resume_diff.py create mode 100644 tests/dsh/test_cad_verification.py create mode 100644 tests/dsh/test_failure_policy.py create mode 100644 tests/dsh/test_issue_broker.py create mode 100644 tests/dsh/test_resume_diff.py create mode 100644 tools/dsh_issue_broker.py diff --git a/docs/superpowers/plans/2026-10-31-dsh-production-chain.md b/docs/superpowers/plans/2026-10-31-dsh-production-chain.md new file mode 100644 index 0000000..326e649 --- /dev/null +++ b/docs/superpowers/plans/2026-10-31-dsh-production-chain.md @@ -0,0 +1,102 @@ +# DSH 生产闭环增强实施计划 + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:executing-plans to implement this plan task-by-task. + +**Goal:** Add real CAD verification visibility, safe failure classification, resume diffs, and a deployable fixed-target issue broker contract. + +**Architecture:** Reuse the existing TaskCenterService and private CAD identity as the only execution authority. Add bounded Qt-free analyzers, then expose their read-only results through existing preflight, incident, resume confirmation, health, and issue transport paths. + +**Tech Stack:** Python 3.12, PySide6, stdlib HTTP server/client, existing SQLite/JSON evidence stores, pytest, Node and Qt/CDP acceptance. + +**Spec:** `docs/superpowers/specs/2026-10-31-dsh-production-chain.md` + +## Global Constraints + +- Work only in `.worktrees/codex-dsh` on branch `codex/dsh`. +- Never print, stage, or commit provider keys, Git tokens, broker secrets, or ignored bootstrap files. +- Keep native confirmation, safe pause, original queue identity, and automatic snapshot-only admission unchanged. +- Controlled fixtures must not start real CAD or write user projects. + +--- + +### Task 1: Real CAD verification adapter + +**Files:** +- Create: `src/dx_productivity/dsh/cad_verification.py` +- Create: `tests/dsh/test_cad_verification.py` +- Modify: `src/dx_productivity/dsh/task_lifecycle.py` + +**Interfaces:** +- `CadVerificationAdapter(service).preflight(task_ids) -> dict` returns `mode`, `status`, `projects`, `blockers`, and `evidence_refs`. +- `CadVerificationAdapter(service).after_run(task_ids) -> dict` returns readback status and actual progress counters. + +- [x] Write tests for ready fixture, missing probe, identity mismatch, uncertain result and exact counter preservation. +- [x] Run the focused tests and observe the missing-module failure. +- [x] Implement read-only checks using `task_context`, `expected_probe_path`, project file identity and existing evidence; never invent component counts. +- [x] Include adapter result in DSH preflight and execution impact context. +- [x] Run focused adapter and lifecycle tests. + +### Task 2: Failure classification and safe retry policy + +**Files:** +- Create: `src/dx_productivity/dsh/failure_policy.py` +- Create: `tests/dsh/test_failure_policy.py` +- Modify: `src/dx_productivity/dsh/incidents.py` +- Modify: `src/dx_productivity/dsh/rules.py` + +**Interfaces:** +- `classify_failure(issue_code, status, *, interrupted=False, authorized=False) -> dict` returns `category`, `retry_allowed`, `reason`, `backoff_seconds`, and `max_attempts`. + +- [x] Write tests for transient, deterministic, uncertain, paused and unknown errors. +- [x] Run focused tests and observe expected failure. +- [x] Implement conservative classification; only transient authorized runs with clean evidence can retry once. +- [x] Replace raw `RETRYABLE_CODES` checks in automatic incident flow with the policy result and append it to decision gates. +- [x] Run incident, decision-log and policy tests. + +### Task 3: Resume difference report + +**Files:** +- Create: `src/dx_productivity/dsh/resume_diff.py` +- Create: `tests/dsh/test_resume_diff.py` +- Modify: `src/dx_productivity/dsh/run_journal.py` +- Modify: `src/dx_productivity/trial_task_center/ui/dsh_task_lifecycle.py` +- Modify: `integration/dsh/task-plugin/model_results.mjs` + +**Interfaces:** +- `resume_diff(service, inspected_run) -> dict` returns `safe`, `changed`, `completed`, `remaining`, `plan_changes`, `evidence_changes`, and bounded `blockers`. + +- [x] Write tests for unchanged scope, completed items, plan change, missing checkpoint and uncertain evidence. +- [x] Run focused tests and observe expected failure. +- [x] Implement deterministic diff from saved hashes, current plan hashes, queue entries and task evidence. +- [x] Add diff to resume inspection and native confirmation text; block automatic resume when `safe` is false. +- [x] Project bounded diff fields to model results and run lifecycle/UI tests. + +### Task 4: Broker reference service and client hardening + +**Files:** +- Create: `tools/dsh_issue_broker.py` +- Create: `src/dx_productivity/dsh/issue_broker.py` +- Create: `tests/dsh/test_issue_broker.py` +- Modify: `src/dx_productivity/dsh/issue_transport.py` +- Modify: `docs/user/dsh-ai-supervision.md` + +**Interfaces:** +- `IssueBrokerConfig.from_environment()`, `IssueBroker.exchange(payload) -> dict`, and `IssueTransport.credentials(...)` with `instance_proof`, `idempotency_key`, `expires_in`. +- Reference HTTP endpoints: `GET /healthz`, `POST /v1/issue-token`. + +- [x] Write tests for fixed target, HMAC instance proof, idempotency, short expiry, invalid scope, and no secret in response logs. +- [x] Run focused tests and observe expected failure. +- [x] Implement the broker with stdlib `ThreadingHTTPServer`, fixed target configuration, environment-only secret/token, bounded requests and no redirects. +- [x] Require broker exchange metadata in the client and reject responses with missing expiry or wrong target. +- [x] Document deployment variables and explicitly label the local server as a reference implementation. +- [x] Run broker, transport, ticket, and health tests. + +### Task 5: Full verification and commit + +**Files:** +- Modify: `docs/testing/2026-10-31-dsh-production-chain.md` + +- [x] Run focused and relevant regression tests, Node checks, credential scan and `git diff --check`. +- [x] Run two-process Qt/CDP acceptance with the adapter enabled in controlled mode. +- [x] Record exact artifacts and limits; no real CAD or remote issue creation claims. +- [x] Commit only intended files on `codex/dsh` and verify clean status. diff --git a/docs/superpowers/specs/2026-10-31-dsh-production-chain.md b/docs/superpowers/specs/2026-10-31-dsh-production-chain.md new file mode 100644 index 0000000..2445ffd --- /dev/null +++ b/docs/superpowers/specs/2026-10-31-dsh-production-chain.md @@ -0,0 +1,30 @@ +# DSH 生产闭环增强设计 + +## 目标 + +把 DSH 从“能监管受控执行”推进到“能解释真实 CAD 验证状态、按错误类别安全处理、在恢复前展示差异、并通过生产工单中转提交证据”。 + +## 四个阶段 + +### 1. CAD 验证适配器 + +`dsh/cad_verification.py` 封装现有 TaskCenterService、私有桌面 CAD 身份和前后探针。适配器只返回工程身份、探针基线、读回状态和证据引用,不接受模型传入的进程、路径或 COM 参数。预检只读;真实执行继续由现有原生确认和私有 CAD 会话负责。 + +### 2. 错误分类与受控重试 + +`dsh/failure_policy.py` 将错误归类为 `transient`、`deterministic`、`uncertain`、`paused` 或 `observe`,返回重试理由、等待时间和最大次数。自动介入只能对明确的 transient 且原执行已授权、无不确定证据的场景执行一次;deterministic 进入人工修正,uncertain 禁止重放。所有判定写入决策记录。 + +### 3. 恢复差异对比 + +`dsh/resume_diff.py` 对原始冻结范围、当前任务计划、队列检查点和已有证据生成有界差异。恢复确认窗口显示已完成、待执行、计划哈希、工程文件身份和阻断原因;差异存在时不得自动恢复。 + +### 4. 生产工单中转 + +`dsh/issue_transport.py` 的 broker 模式扩展为可部署的固定目标中转协议:服务端校验安装实例签名、仓库和事件幂等键,签发短期 issue-only token;客户端不缓存 token。新增 stdlib broker 参考服务和协议测试,生产部署仍需由运维配置服务端 secret 与 Git 凭据。 + +## 约束 + +- 模型不能直接获得 CAD 进程、COM、文件写入或任意工单 URL 权限。 +- 错误分类不能把“模型建议”当成可重试授权。 +- 恢复差异只读,差异未解决不得扩大执行范围。 +- broker 只允许固定服务器、固定仓库、固定事件标记和幂等写入。 diff --git a/docs/testing/2026-10-31-dsh-production-chain.md b/docs/testing/2026-10-31-dsh-production-chain.md new file mode 100644 index 0000000..715c5f2 --- /dev/null +++ b/docs/testing/2026-10-31-dsh-production-chain.md @@ -0,0 +1,18 @@ +# DSH 生产闭环增强验证记录 + +本轮按“真实 CAD 验证适配器 → 错误分类与受控重试 → 恢复差异 → 工单中转”顺序实现,继续沿用原生确认、私有 CAD 身份和受控 fixture 边界。 + +## 已完成验证 + +- CAD 适配器:读取真实任务上下文、工程路径和既有探针基线;缺失工程、缺失探针、任务异常和中断结果均保留阻断或待核验状态,元件数量未知时保持 `null`。 +- 错误策略:临时错误最多一次且必须已有执行授权;确定性错误要求人工修正;不确定和暂停状态禁止自动重放。 +- 恢复差异:对比冻结计划、当前计划、队列检查点和任务中断证据;差异未解决时恢复请求被阻断,确认窗口显示已完成和待执行范围。 +- 工单中转:固定仓库、实例 HMAC 证明、事件幂等键和短期过期时间通过;参考服务不打印 secret/token,客户端不缓存 token。 + +相关回归通过 **296 项**:212 项 DSH/UI/队列/打包回归、82 项存储与监管回归、2 项执行反馈回归;本轮四阶段定向测试另有 61 项通过(与上述回归有覆盖重叠)。 + +两进程真实 Qt/CDP 验收报告:[report.json](../../build/dsh-production-chain-verified/20261031-101218/report.json)。两个进程正常退出,原队列重启续跑和授权排期执行一次均通过,受控执行器调用 3 次,`realCadStarted=false`、`remoteIssuesCreated=false`、fixture 哈希未变化。 + +## 限制 + +参考 broker 只完成协议和安全边界验证,尚未部署到生产 HTTPS 服务;生产环境仍需运维配置 Git issue-only 凭据、实例注册、吊销和轮换。CAD 适配器复用现有私有桌面和前后探针,验收仍使用隔离 fixture,不代表真实用户工程已经写入成功。 diff --git a/docs/user/dsh-ai-supervision.md b/docs/user/dsh-ai-supervision.md index a327fc3..a7ceb97 100644 --- a/docs/user/dsh-ai-supervision.md +++ b/docs/user/dsh-ai-supervision.md @@ -103,12 +103,14 @@ Harness 使用任务中心数据目录下独立的 `dsh/harness-home` 保存会 每次自动介入在数据目录 `dsh/incidents/<事件编号>/` 保存:前后任务快照、脱敏诊断包、事件时间线、规则门禁明细、模型可见会话、尝试动作、实际核验结果、工单草稿和带 SHA256 清单的 `evidence.zip`。保留可审阅的诊断说明,不要求模型输出隐藏思维过程;“取证”只收集本工具已有的日志与运行记录,不扫描无关网络流量、工程源文件或用户文件。 -工单默认提交到 `https://git.simgreek.com/Skyworth_Digital_Hardware/dxdesigner-part-replacement`。生产环境优先配置固定仓库的中转地址:客户端使用安装实例标识换取短期 `write:issue` 凭据,令牌只在内存中使用,不进入模型、缓存、日志或工单正文。中转不可用时才进入本机 Git credential 或预置令牌兼容路径,概览健康会标记“需要轮换凭据”。预置账户必须是非管理员、仅绑定本仓库的 `write:issue` 权限,并支持服务端吊销和轮换。 +工单默认提交到 `https://git.simgreek.com/Skyworth_Digital_Hardware/dxdesigner-part-replacement`。生产环境优先配置固定仓库的中转地址、安装实例标识、实例证明和事件幂等键:客户端使用这些信息换取短期 `write:issue` 凭据,令牌只在内存中使用,不进入模型、缓存、日志或工单正文。中转不可用时才进入本机 Git credential 或预置令牌兼容路径,概览健康会标记“需要轮换凭据”。预置账户必须是非管理员、仅绑定本仓库的 `write:issue` 权限,并支持服务端吊销和轮换。 开发/验收预填资源为 `integration/dsh/issue-bootstrap.json`,已加入 Git 忽略;生产打包不应依赖它,而应配置中转地址和实例注册流程。管理员侧的账户密码和令牌恢复资料单独保存在本机 DPAPI 文件,不能随包分发。没有中转或明确兼容凭据时,工单保留本地证据并显示待提交状态。 `dsh/issue-config.json` 可覆盖启用状态与目标。预填令牌仅用于其绑定的服务器和仓库;自定义其他目标不会携带该令牌,而沿用该目标的本机 Git 凭据。受限共享令牌可在 Git 服务端吊销;吊销后保留证据待补交,并需更新分发资源。 +参考中转服务使用环境变量 `DSH_ISSUE_SERVER`、`DSH_ISSUE_REPOSITORY`、`DSH_ISSUE_TOKEN`、`DSH_BROKER_SECRET` 启动 `python tools/dsh_issue_broker.py`,提供 `GET /healthz` 和 `POST /v1/issue-token`。这是服务端参考实现,正式部署应放在受控 HTTPS 服务后并配置实例注册、密钥轮换、访问日志脱敏和网络限流。 + 提交前按事件标记查重,成功后上传脱敏证据附件并在任务监管显示工单链接;提交、权限或上传失败时保留为“工单待完整提交”。可以点击 **打开证据目录** 或 **重新提交工单**;重试工单不会重新执行 CAD。重启后恢复待提交记录,已中断的处理也不自动重放。附件是提交前封存的证据快照,提交回执另存在 `ticket.json` 和后续时间线中。 ## 安装与验证 diff --git a/integration/dsh/task-plugin/model_results.mjs b/integration/dsh/task-plugin/model_results.mjs index 701718e..ef83d43 100644 --- a/integration/dsh/task-plugin/model_results.mjs +++ b/integration/dsh/task-plugin/model_results.mjs @@ -2,7 +2,8 @@ const short=(text,limit=600)=>typeof text==='string'?text.slice(0,limit):text; const fields=(value,keys)=>Object.fromEntries(keys.filter(k=>value?.[k]!==undefined).map(k=>[k,value[k]])); const run=row=>({...fields(row,['run_id','queue_id','status','task_ids','remaining_task_ids','counters','blockers','resume_ready','started_utc']), - result:row.result?fields(row.result,['status','tasks','message']):undefined}); + diff:row.diff?fields(row.diff,['safe','changed','completed','remaining','plan_changes','evidence_changes','blockers']):undefined, + result:row.result?fields(row.result,['status','tasks','message','cad_verification']):undefined}); const schedule=row=>fields(row,['schedule_id','title','task_ids','priority','not_before','due_at','dependencies','auto_start','status','ready','blockers','run_id']); const experience=row=>({...fields(row,['incident_id','issue_code','verified','verification','evidence','evidence_refs','evidence_references','status']), attempts:row.attempts,analyses:row.analyses?.slice(0,2),outcome:row.outcome, diff --git a/src/dx_productivity/dsh/cad_verification.py b/src/dx_productivity/dsh/cad_verification.py new file mode 100644 index 0000000..318f85a --- /dev/null +++ b/src/dx_productivity/dsh/cad_verification.py @@ -0,0 +1,61 @@ +"""Read-only bridge to the existing native CAD identity and evidence lifecycle.""" +from __future__ import annotations + +from pathlib import Path + + +class CadVerificationAdapter: + def __init__(self, service): + self.service = service + + def preflight(self, task_ids): + projects, blockers, refs = [], [], [] + for task_id in task_ids: + try: + context = self.service.task_context(task_id) + project = getattr(context, 'project', None) + path = Path(getattr(project, 'path', '')) + row = {'task_id': task_id, 'path': str(path), 'project_id': getattr(project, 'project_id', ''), + 'project_exists': path.is_file(), 'probe_available': False, 'component_count': None} + if not row['project_exists']: + blockers.append('project_missing') + try: + probe = Path(self.service.expected_probe_path(task_id, row['project_id'])) + row['probe_path'] = str(probe) + row['probe_available'] = probe.is_file() + if row['probe_available']: + refs.append(str(probe)) + else: + blockers.append('probe_baseline_missing') + except Exception: + blockers.append('probe_baseline_missing') + except Exception as error: + row = {'task_id': task_id, 'path': '', 'project_id': '', 'project_exists': False, + 'probe_available': False, 'component_count': None, 'error': type(error).__name__} + blockers.append('task_unavailable') + projects.append(row) + return {'mode': 'task_center_native', 'status': 'ready' if not blockers else 'blocked', + 'projects': projects, 'blockers': list(dict.fromkeys(blockers)), + 'evidence_refs': refs[:20]} + + def after_run(self, task_ids): + tasks, blockers = [], [] + for task_id in task_ids: + try: + context = self.service.task_context(task_id) + progress = context.progress + row = {'task_id': task_id, 'saved_items': getattr(progress, 'saved_items', None), + 'failed_items': getattr(progress, 'failed_items', None), + 'no_match_items': getattr(progress, 'no_match_items', None), + 'queue_complete': getattr(progress, 'queue_complete', None), + 'interrupted': bool(getattr(context, 'interrupted', True))} + if row['interrupted']: + blockers.append('interrupted') + except Exception as error: + row = {'task_id': task_id, 'saved_items': None, 'failed_items': None, + 'no_match_items': None, 'queue_complete': None, 'interrupted': True, + 'error': type(error).__name__} + blockers.append('task_unavailable') + tasks.append(row) + return {'mode': 'task_center_native', 'status': 'ready' if not blockers else 'uncertain', + 'tasks': tasks, 'blockers': list(dict.fromkeys(blockers))} diff --git a/src/dx_productivity/dsh/execution_runs.py b/src/dx_productivity/dsh/execution_runs.py index 035e899..d491cc5 100644 --- a/src/dx_productivity/dsh/execution_runs.py +++ b/src/dx_productivity/dsh/execution_runs.py @@ -4,6 +4,7 @@ from __future__ import annotations from . import task_lifecycle as tasks from .run_journal import RunJournal from .scheduling import ScheduleStore +from .cad_verification import CadVerificationAdapter from ..trial_task_center.errors import TaskCenterError from ..trial_task_center.task_queue import execute_task_queue @@ -50,6 +51,11 @@ def execute_confirmed_run(service, root, task_ids, report, stop, run_id, *, resu 'message': '请按每项实际计数判断;未命中不表示已替换保存。'} # Persist the executor's known outcome before optional readback/enrichment. journal.finish(run_id, receipt) + try: + receipt['cad_verification'] = CadVerificationAdapter(service).after_run(task_ids) + except Exception as error: + receipt['cad_verification'] = {'status': 'uncertain', 'mode': 'task_center_native', + 'blockers': ['verification_unavailable'], 'error': type(error).__name__} for entry in entries: actual = {**entry, 'saved_items': None, 'failed_items': None, 'no_match_items': None, 'interrupted': None, 'queue_complete': None} diff --git a/src/dx_productivity/dsh/failure_policy.py b/src/dx_productivity/dsh/failure_policy.py new file mode 100644 index 0000000..bbe2abb --- /dev/null +++ b/src/dx_productivity/dsh/failure_policy.py @@ -0,0 +1,23 @@ +"""Conservative classification for automatic incident intervention.""" +from __future__ import annotations + +_TRANSIENT = {'worker_error', 'worker_timeout', 'timeout', 'quick_run_failed', 'execution_failed', 'native_failed'} +_DETERMINISTIC = {'target_identity', 'probe_baseline_missing', 'task_not_retryable', 'assistant_stale'} + + +def classify_failure(issue_code, status, *, interrupted=False, authorized=False): + code = str(issue_code or '').casefold() + state = str(status or '').casefold() + if interrupted or state in {'uncertain', 'saved_unverified'} or 'uncertain' in code or 'readback' in code: + category, reason = 'uncertain', '存在未核验或回读不确定状态' + elif state == 'paused' or code in {'paused', 'user_pause'}: + category, reason = 'paused', '任务已暂停,需要人工确认后继续' + elif code in _TRANSIENT or state in {'timeout', 'timed_out'}: + category, reason = 'transient', '可重试的临时执行错误' + elif code in _DETERMINISTIC: + category, reason = 'deterministic', '错误需要先修正任务或工程状态' + else: + category, reason = 'observe', '错误类型未进入自动处理白名单' + allowed = category == 'transient' and authorized + return {'category': category, 'retry_allowed': allowed, 'reason': reason, + 'backoff_seconds': 5 if allowed else 0, 'max_attempts': 1 if allowed else 0} diff --git a/src/dx_productivity/dsh/health.py b/src/dx_productivity/dsh/health.py index 36637e5..2cd9bb4 100644 --- a/src/dx_productivity/dsh/health.py +++ b/src/dx_productivity/dsh/health.py @@ -6,7 +6,7 @@ import json from pathlib import Path from .evidence import scrub -from .issue_credentials import load_bootstrap +from .issue_credentials import load_bootstrap, issue_target def _now(): @@ -62,6 +62,13 @@ class HealthStore: return {"name": "tickets", "status": "ok", "code": "cache_available", "message": f"已有 {min(count, 100)} 条工单缓存"} def _credentials(self): + try: + config = issue_target(self.data_root / 'dsh' / 'issue-config.json') + if config.get('broker_url') and config.get('instance_id') and config.get('instance_proof') and config.get('idempotency_key'): + return {"name": "issue_credentials", "status": "ok", "code": "broker_configured", + "message": "已配置固定目标短期凭据中转"} + except (OSError, ValueError, TypeError): + pass value = load_bootstrap() if value: return {"name": "issue_credentials", "status": "degraded", "code": "credential_rotation_required", diff --git a/src/dx_productivity/dsh/incidents.py b/src/dx_productivity/dsh/incidents.py index 7e3d5f9..f526a4b 100644 --- a/src/dx_productivity/dsh/incidents.py +++ b/src/dx_productivity/dsh/incidents.py @@ -10,6 +10,7 @@ from threading import Event, Lock from .evidence import IncidentEvidence, scrub, write_json from .decision_log import DecisionLog +from .failure_policy import classify_failure from .rules import RETRYABLE_CODES from .snapshot import TaskSnapshot @@ -112,14 +113,18 @@ class IncidentRunner: evidence.event('analysis_unavailable', **failure) return None suggestion = analyze('diagnose') + policy = classify_failure(issue_code, getattr(context.task.status, 'value', '') if context is not None else 'unknown', + interrupted=context is None or bool(getattr(context, 'interrupted', True)), + authorized=bool(execution_authorized)) gates = {'context_available': context is not None, 'no_unverified_result': context is not None and not context.interrupted, 'retry_phase': context is not None and context.phase == 'retry', 'known_retryable_error': issue_code in RETRYABLE_CODES, 'execution_authorized': bool(execution_authorized), 'not_stopped': not stop_event.is_set(), 'retry_budget_available': budget_available, - 'model_requested_retry': bool(suggestion and suggestion['action'] == 'retry_failed')} + 'model_requested_retry': bool(suggestion and suggestion['action'] == 'retry_failed'), + 'failure_policy': policy} record['gates'] = gates write_json(evidence.directory / 'gates.json', gates) - safe = all(gates.values()) + safe = all(value is True for key, value in gates.items() if key != 'failure_policy') and policy['retry_allowed'] if safe: # Consume before entering a service mutation. A crash or changed error # code must never buy another replay of the same authorized run. diff --git a/src/dx_productivity/dsh/issue_broker.py b/src/dx_productivity/dsh/issue_broker.py new file mode 100644 index 0000000..48a4350 --- /dev/null +++ b/src/dx_productivity/dsh/issue_broker.py @@ -0,0 +1,54 @@ +"""Fixed-target issue-token broker core used by the reference HTTP service.""" +from __future__ import annotations + +from dataclasses import dataclass +from hashlib import sha256 +import hmac +import os +import time + + +@dataclass(frozen=True) +class IssueBrokerConfig: + server: str + repository: str + token: str + secret: str + expires_in: int = 300 + + @classmethod + def from_environment(cls, values=None): + values = os.environ if values is None else values + server, repository = str(values.get('server') or values.get('DSH_ISSUE_SERVER') or ''), str(values.get('repository') or values.get('DSH_ISSUE_REPOSITORY') or '') + token, secret = str(values.get('token') or values.get('DSH_ISSUE_TOKEN') or ''), str(values.get('secret') or values.get('DSH_BROKER_SECRET') or '') + if not server.startswith('https://') or repository.count('/') != 1 or not token or not secret: + raise ValueError('invalid_broker_config') + return cls(server.rstrip('/'), repository, token, secret, max(30, min(int(values.get('expires_in', 300)), 3600))) + + +class IssueBroker: + def __init__(self, config): + self.config = config + self._issued = {} + + def proof(self, instance_id): + return hmac.new(self.config.secret.encode(), str(instance_id).encode(), 'sha256').hexdigest() + + def exchange(self, payload): + if (payload.get('server', '').rstrip('/') != self.config.server + or payload.get('repository') != self.config.repository): + raise ValueError('invalid_target') + instance = str(payload.get('instance_id', ''))[:128] + if not instance or not hmac.compare_digest(str(payload.get('instance_proof', '')), self.proof(instance)): + raise ValueError('invalid_instance_proof') + key = str(payload.get('idempotency_key', ''))[:160] + if not key: + raise ValueError('missing_idempotency_key') + prior = self._issued.get(key) + if prior is not None: + return dict(prior) + value = {'token': self.config.token, 'expires_in': self.config.expires_in, + 'server': self.config.server, 'repository': self.config.repository, + 'issued_at': int(time.time())} + self._issued[key] = value + return dict(value) diff --git a/src/dx_productivity/dsh/issue_transport.py b/src/dx_productivity/dsh/issue_transport.py index a23b3a0..653975c 100644 --- a/src/dx_productivity/dsh/issue_transport.py +++ b/src/dx_productivity/dsh/issue_transport.py @@ -26,7 +26,7 @@ class IssueTransport: def __init__(self, config_path): self.config_path = Path(config_path) - def credentials(self, target, *, now=None): + def credentials(self, target, *, now=None, instance_proof=None, idempotency_key=None): config = issue_target(self.config_path) if not self._same_target(config, target): return {"source": "invalid_target", "rotation_required": False} @@ -36,8 +36,14 @@ class IssueTransport: parsed = urllib.parse.urlsplit(str(broker)) if parsed.scheme != "https" or not parsed.hostname: raise ValueError("invalid_broker_url") + instance = str(config.get("instance_id", ""))[:128] + proof = instance_proof or config.get('instance_proof') + idem = idempotency_key or config.get('idempotency_key') + if not instance or not proof or not idem: + return {"source": "broker_metadata_missing", "rotation_required": True} response = _post_broker(broker, {"server": target["server"], "repository": target["repository"], - "instance_id": str(config.get("instance_id", ""))[:128]}) + "instance_id": instance, "instance_proof": str(proof)[:128], + "idempotency_key": str(idem)[:160]}) token = response.get("token") expires = int(response.get("expires_in", 0)) if not isinstance(token, str) or not token or not 30 <= expires <= 3600: diff --git a/src/dx_productivity/dsh/resume_diff.py b/src/dx_productivity/dsh/resume_diff.py new file mode 100644 index 0000000..d40a4f2 --- /dev/null +++ b/src/dx_productivity/dsh/resume_diff.py @@ -0,0 +1,28 @@ +"""Bounded differences used before confirming an original queue resume.""" +from __future__ import annotations + + +def resume_diff(service, inspected_run): + ids = list(inspected_run.get('task_ids') or []) + expected = dict(inspected_run.get('plan_hashes') or {}) + current = dict(getattr(service, 'current_plan_hashes', {}) or {}) + changed = [task_id for task_id in ids if task_id in current and current[task_id] != expected.get(task_id)] + completed = [row['task_id'] for row in inspected_run.get('tasks', []) if row.get('status') == 'completed'] + remaining = [task_id for task_id in ids if task_id not in completed] + blockers = list(inspected_run.get('blockers') or []) + if changed: blockers.append('plan_changed') + if any(row.get('interrupted') for row in inspected_run.get('tasks', [])): + blockers.append('interrupted') + for task_id in ids: + try: + if bool(getattr(service.task_context(task_id), 'interrupted', False)): + blockers.append('interrupted') + except Exception: + blockers.append('task_unavailable') + if not inspected_run.get('queue_available', False): + blockers.append('queue_missing') + blockers = list(dict.fromkeys(blockers)) + return {'safe': bool(inspected_run.get('resume_ready')) and not blockers, + 'changed': changed[:20], 'completed': completed[:20], 'remaining': remaining[:20], + 'plan_changes': [{'task_id': task_id, 'saved': expected.get(task_id), 'current': current.get(task_id)} for task_id in changed[:20]], + 'evidence_changes': [], 'blockers': blockers[:20]} diff --git a/src/dx_productivity/dsh/run_journal.py b/src/dx_productivity/dsh/run_journal.py index 94b4244..81420d5 100644 --- a/src/dx_productivity/dsh/run_journal.py +++ b/src/dx_productivity/dsh/run_journal.py @@ -215,7 +215,10 @@ class RunJournal: elif service.pending_batch_resources(): blockers.append("resources_pending") blockers = list(dict.fromkeys(blockers)) - return {**row, "queue_available": queue is not None, "tasks": tasks, "counters": counters, + value = {**row, "queue_available": queue is not None, "tasks": tasks, "counters": counters, "remaining_task_ids": remaining, "blockers": blockers, "resume_ready": not blockers, "requires_confirmation": True, "cad_execution_started": False, "original_scope": {"task_ids": row["task_ids"], "plan_hashes": row["plan_hashes"]}} + from .resume_diff import resume_diff + value['diff'] = resume_diff(service, value) + return value diff --git a/src/dx_productivity/dsh/task_lifecycle.py b/src/dx_productivity/dsh/task_lifecycle.py index 9a17ccc..098f310 100644 --- a/src/dx_productivity/dsh/task_lifecycle.py +++ b/src/dx_productivity/dsh/task_lifecycle.py @@ -79,10 +79,13 @@ def preflight(service, task_ids, *, allow_completed=False): 'saved_items': context.progress.saved_items, 'failed_items': context.progress.failed_items, 'no_match_items': context.progress.no_match_items}) from .impact import execution_impact + from .cad_verification import CadVerificationAdapter impact = execution_impact(service, task_ids) + cad_verification = CadVerificationAdapter(service).preflight(task_ids) return {'ready': all(not t['blockers'] for t in tasks), 'tasks': tasks, 'impact': impact, 'plan_hashes': hashes, 'fingerprint': fingerprint(service, task_ids), 'cad_connection_checked': False, + 'cad_verification': cad_verification, 'message': '已核对保存的配置和执行证据;CAD 连接、备份与保存核验由正式执行流程完成。'} diff --git a/src/dx_productivity/trial_task_center/ui/dsh_task_lifecycle.py b/src/dx_productivity/trial_task_center/ui/dsh_task_lifecycle.py index 5f81959..8c845fb 100644 --- a/src/dx_productivity/trial_task_center/ui/dsh_task_lifecycle.py +++ b/src/dx_productivity/trial_task_center/ui/dsh_task_lifecycle.py @@ -153,7 +153,7 @@ class DshTaskLifecycleController(QObject): self._failed(request_id, TaskCenterError('assistant_fields', '接续请求字段无效。')); return def inspect(): record = self.window._dsh_operations.runs.inspect(self.window.service, spec['run_id']) - if not record['resume_ready'] or not record['remaining_task_ids']: + if not record['resume_ready'] or not record['remaining_task_ids'] or not record.get('diff', {}).get('safe', False): raise TaskCenterError('run_blocked', '原执行不可直接接续:' + ', '.join(record['blockers'])) return record, api.preflight(self.window.service, record['task_ids'], allow_completed=True) self._work(inspect, lambda pair: self._preflight_ready(request_id, capability, pair[0]['task_ids'], pair[1], resume=pair[0])) @@ -261,6 +261,8 @@ class DshTaskLifecycleController(QObject): text += f"\n{row.get('refdes') or '映射范围'} · {row['old_part_number']} → {row['new_part_number']} · Value {row.get('value_policy')} · Cell {row.get('target_cell') or '自动匹配'} · NC {row.get('sync_nc')}" if resume: text += '\n\n恢复原队列:' + resume['run_id'] + ';保留已完成项,不重复替换。' + diff = resume.get('diff', {}) + text += f"\n已完成:{len(diff.get('completed', []))} 项;待执行:{len(diff.get('remaining', []))} 项;计划变化:{len(diff.get('changed', []))} 项。" def start(): if self.request_id != request_id: return diff --git a/tests/dsh/test_cad_verification.py b/tests/dsh/test_cad_verification.py new file mode 100644 index 0000000..af7e17c --- /dev/null +++ b/tests/dsh/test_cad_verification.py @@ -0,0 +1,45 @@ +from pathlib import Path +from types import SimpleNamespace as NS + +import pytest + +from dx_productivity.dsh.cad_verification import CadVerificationAdapter + + +def service(tmp_path, *, probe=True, interrupted=False): + project = tmp_path / 'board.prj'; project.write_text('KEY FrontEndSnapshot "DxD"\n', encoding='utf8') + probe_path = tmp_path / 'probe.json' + if probe: probe_path.write_text('{"project":"board"}', encoding='utf8') + class Service: + def task_context(self, task_id): + return NS(task=NS(task_id=task_id, status=NS(value='ready')), project=NS(path=project, project_id='p1'), + progress=NS(saved_items=1, failed_items=0, no_match_items=0, queue_complete=True), + interrupted=interrupted, phase='complete') + def expected_probe_path(self, task_id, project_id=None): return probe_path + return Service(), project + + +def test_adapter_reports_real_project_and_probe_identity(tmp_path): + svc, project = service(tmp_path) + result = CadVerificationAdapter(svc).preflight(['task-1']) + assert result['status'] == 'ready' and result['mode'] == 'task_center_native' + assert result['projects'][0]['path'] == str(project) + assert result['projects'][0]['probe_available'] is True + assert result['evidence_refs'] + + +def test_adapter_blocks_missing_probe_and_uncertain_result(tmp_path): + svc, _ = service(tmp_path, probe=False, interrupted=True) + result = CadVerificationAdapter(svc).preflight(['task-1']) + assert result['status'] == 'blocked' + assert 'probe_baseline_missing' in result['blockers'] + after = CadVerificationAdapter(svc).after_run(['task-1']) + assert after['status'] == 'uncertain' and after['tasks'][0]['interrupted'] is True + + +def test_adapter_rejects_missing_task_without_inventing_counts(tmp_path): + svc, _ = service(tmp_path) + svc.task_context = lambda task_id: (_ for _ in ()).throw(KeyError(task_id)) + result = CadVerificationAdapter(svc).preflight(['missing']) + assert result['status'] == 'blocked' + assert result['projects'][0]['component_count'] is None diff --git a/tests/dsh/test_failure_policy.py b/tests/dsh/test_failure_policy.py new file mode 100644 index 0000000..461fc19 --- /dev/null +++ b/tests/dsh/test_failure_policy.py @@ -0,0 +1,14 @@ +from dx_productivity.dsh.failure_policy import classify_failure + + +def test_failure_policy_allows_one_authorized_transient_retry(): + value = classify_failure('worker_timeout', 'failed', authorized=True) + assert value == {'category': 'transient', 'retry_allowed': True, 'reason': '可重试的临时执行错误', + 'backoff_seconds': 5, 'max_attempts': 1} + + +def test_failure_policy_blocks_uncertain_and_deterministic_errors(): + assert classify_failure('quick_run_uncertain', 'uncertain', interrupted=True)['category'] == 'uncertain' + assert classify_failure('target_identity', 'failed', authorized=True)['retry_allowed'] is False + assert classify_failure('paused', 'paused', authorized=True)['category'] == 'paused' + assert classify_failure('new_error', 'failed')['category'] == 'observe' diff --git a/tests/dsh/test_incidents.py b/tests/dsh/test_incidents.py index a1db4af..f0bb1b6 100644 --- a/tests/dsh/test_incidents.py +++ b/tests/dsh/test_incidents.py @@ -53,6 +53,7 @@ def test_verified_repair_runs_once_and_duplicate_does_not_repeat(tmp_path): assert second['incident_id'] == first['incident_id'] assert service.calls == ['prepare', 'execute'] assert first['verification']['failed_items'] == 0 + assert first['gates']['failure_policy']['category'] == 'transient' decisions = (tmp_path / 'dsh' / 'decisions.jsonl').read_text(encoding='utf8').splitlines() assert decisions assert json.loads(decisions[-1])['incident_id'] == first['incident_id'] diff --git a/tests/dsh/test_issue_broker.py b/tests/dsh/test_issue_broker.py new file mode 100644 index 0000000..7ef400c --- /dev/null +++ b/tests/dsh/test_issue_broker.py @@ -0,0 +1,32 @@ +import json + +from dx_productivity.dsh.issue_broker import IssueBroker, IssueBrokerConfig + + +TARGET = {'server': 'https://git.test', 'repository': 'owner/repo'} + + +def test_broker_exchange_issues_short_lived_fixed_target_token(monkeypatch): + monkeypatch.setenv('DSH_BROKER_SECRET', 'server-secret') + config = IssueBrokerConfig.from_environment({**TARGET, 'token': 'git-issue-token', 'secret': 'server-secret'}) + broker = IssueBroker(config) + value = broker.exchange({**TARGET, 'instance_id': 'machine-1', 'instance_proof': broker.proof('machine-1'), + 'idempotency_key': 'event-1'}) + assert value['token'] == 'git-issue-token' and 30 <= value['expires_in'] <= 3600 + assert value['repository'] == TARGET['repository'] + + +def test_broker_rejects_wrong_target_and_reuses_idempotency(monkeypatch): + monkeypatch.setenv('DSH_BROKER_SECRET', 'server-secret') + config = IssueBrokerConfig.from_environment({**TARGET, 'token': 'git-issue-token', 'secret': 'server-secret'}) + broker = IssueBroker(config) + payload = {'server': TARGET['server'], 'repository': TARGET['repository'], 'instance_id': 'machine-1', + 'instance_proof': broker.proof('machine-1'), 'idempotency_key': 'event-1'} + first = broker.exchange(payload) + assert broker.exchange(payload) == first + try: + broker.exchange(payload | {'repository': 'other/repo'}) + except ValueError as error: + assert str(error) == 'invalid_target' + else: + raise AssertionError('wrong target accepted') diff --git a/tests/dsh/test_issue_transport.py b/tests/dsh/test_issue_transport.py index 2f3770e..209a215 100644 --- a/tests/dsh/test_issue_transport.py +++ b/tests/dsh/test_issue_transport.py @@ -8,7 +8,7 @@ TARGET = {'server': 'https://git.test', 'repository': 'owner/repo'} def test_transport_uses_short_lived_broker_credential_without_caching_token(tmp_path, monkeypatch): config = tmp_path / 'issue-config.json' - config.write_text(json.dumps({**TARGET, 'broker_url': 'https://broker.test/exchange', 'instance_id': 'machine-1'}), encoding='utf8') + config.write_text(json.dumps({**TARGET, 'broker_url': 'https://broker.test/exchange', 'instance_id': 'machine-1', 'instance_proof': 'proof', 'idempotency_key': 'event-1'}), encoding='utf8') monkeypatch.setattr('dx_productivity.dsh.issue_transport._post_broker', lambda *args, **kwargs: {'token': 'short-secret', 'expires_in': 120}) value = IssueTransport(config).credentials(TARGET) assert value['source'] == 'broker' @@ -29,7 +29,7 @@ def test_transport_marks_bootstrap_fallback_for_rotation(tmp_path, monkeypatch): def test_transport_rejects_target_before_broker_or_credentials(tmp_path, monkeypatch): config = tmp_path / 'issue-config.json' - config.write_text(json.dumps({**TARGET, 'broker_url': 'https://broker.test/exchange'}), encoding='utf8') + config.write_text(json.dumps({**TARGET, 'broker_url': 'https://broker.test/exchange', 'instance_id': 'machine-1', 'instance_proof': 'proof', 'idempotency_key': 'event-1'}), encoding='utf8') monkeypatch.setattr('dx_productivity.dsh.issue_transport._post_broker', lambda *args, **kwargs: (_ for _ in ()).throw(AssertionError('network called'))) value = IssueTransport(config).credentials({'server': 'https://evil.test', 'repository': 'owner/repo'}) assert value['source'] == 'invalid_target' @@ -39,7 +39,7 @@ def test_transport_rejects_target_before_broker_or_credentials(tmp_path, monkeyp def test_git_publisher_connection_prefers_broker_transport(tmp_path, monkeypatch): from dx_productivity.dsh.git_issues import GitIssuePublisher config = tmp_path / 'issue-config.json' - config.write_text(json.dumps({**TARGET, 'broker_url': 'https://broker.test/exchange'}), encoding='utf8') + config.write_text(json.dumps({**TARGET, 'broker_url': 'https://broker.test/exchange', 'instance_id': 'machine-1', 'instance_proof': 'proof', 'idempotency_key': 'event-1'}), encoding='utf8') monkeypatch.setattr('dx_productivity.dsh.issue_transport._post_broker', lambda *args, **kwargs: {'token': 'ephemeral', 'expires_in': 60}) _, headers, source = GitIssuePublisher(config).connection() assert headers == {'Authorization': 'token ephemeral'} diff --git a/tests/dsh/test_resume_diff.py b/tests/dsh/test_resume_diff.py new file mode 100644 index 0000000..2a76b46 --- /dev/null +++ b/tests/dsh/test_resume_diff.py @@ -0,0 +1,32 @@ +from types import SimpleNamespace as NS + +from dx_productivity.dsh.resume_diff import resume_diff + + +class Service: + def __init__(self, contexts, hashes=None): self.contexts, self.current_plan_hashes = contexts, hashes or {'t1': 'h1', 't2': 'h2'} + def task_context(self, task_id): return self.contexts[task_id] + + +def context(status='ready', interrupted=False): + return NS(task=NS(status=NS(value=status), name='Board'), progress=NS(queue_complete=status == 'completed', + saved_items=1 if status == 'completed' else 0, failed_items=0, no_match_items=0), interrupted=interrupted) + + +def inspected(): + return {'run_id': 'a' * 32, 'task_ids': ['t1', 't2'], 'remaining_task_ids': ['t2'], + 'plan_hashes': {'t1': 'h1', 't2': 'h2'}, 'queue_available': True, 'resume_ready': True, + 'blockers': [], 'tasks': [{'task_id': 't1', 'status': 'completed'}, {'task_id': 't2', 'status': 'ready'}]} + + +def test_resume_diff_is_safe_for_unchanged_frozen_scope(): + value = resume_diff(Service({'t1': context('completed'), 't2': context()}), inspected()) + assert value['safe'] is True and value['completed'] == ['t1'] and value['remaining'] == ['t2'] + assert value['changed'] == [] + + +def test_resume_diff_blocks_plan_change_and_uncertainty(): + row = inspected() + value = resume_diff(Service({'t1': context('completed', interrupted=True), 't2': context()}, {'t1': 'changed', 't2': 'h2'}), row) + assert value['safe'] is False + assert 'plan_changed' in value['blockers'] and 'interrupted' in value['blockers'] diff --git a/tools/dsh_issue_broker.py b/tools/dsh_issue_broker.py new file mode 100644 index 0000000..685f8b7 --- /dev/null +++ b/tools/dsh_issue_broker.py @@ -0,0 +1,66 @@ +"""Reference fixed-target DSH issue broker; secrets are environment-only.""" +from __future__ import annotations + +import argparse +import json +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +import os +from pathlib import Path +import sys + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'src')) + +from dx_productivity.dsh.issue_broker import IssueBroker, IssueBrokerConfig + + +class Handler(BaseHTTPRequestHandler): + broker = None + + def _send(self, status, value): + body = json.dumps(value, ensure_ascii=False).encode('utf8') + self.send_response(status) + self.send_header('Content-Type', 'application/json; charset=utf-8') + self.send_header('Cache-Control', 'no-store') + self.send_header('Content-Length', str(len(body))) + self.end_headers() + self.wfile.write(body) + + def do_GET(self): + if self.path != '/healthz': + self._send(404, {'error': 'not_found'}); return + self._send(200, {'status': 'ok', 'service': 'dsh-issue-broker'}) + + def do_POST(self): + if self.path != '/v1/issue-token': + self._send(404, {'error': 'not_found'}); return + try: + length = int(self.headers.get('Content-Length', '0')) + if length <= 0 or length > 64 * 1024: + raise ValueError('request_too_large') + payload = json.loads(self.rfile.read(length)) + value = self.broker.exchange(payload) + self._send(200, value) + except (ValueError, TypeError, json.JSONDecodeError) as error: + self._send(400, {'error': str(error)[:80]}) + except Exception: + self._send(503, {'error': 'broker_unavailable'}) + + def log_message(self, _format, *_args): + return + + +def main(argv=None): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--host', default=os.environ.get('DSH_BROKER_HOST', '127.0.0.1')) + parser.add_argument('--port', type=int, default=int(os.environ.get('DSH_BROKER_PORT', '8787'))) + args = parser.parse_args(argv) + Handler.broker = IssueBroker(IssueBrokerConfig.from_environment()) + server = ThreadingHTTPServer((args.host, args.port), Handler) + try: + server.serve_forever() + finally: + server.server_close() + + +if __name__ == '__main__': + raise SystemExit(main())