feat(dsh): harden health decisions and issue transport

This commit is contained in:
阿白叔 committed 2026-10-31 09:50:35 +08:00
1 parent 736b582bc1
commit eee4e6bde9
18 files changed
+592 -11

No files matched your search

@@ -0,0 +1,107 @@
# DSH 生产化加固实施计划
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:executing-plans to implement this plan task-by-task.
**Goal:** Add local health visibility, append-only incident decision records, and fixed-target issue credential rotation without weakening DSH execution gates.
**Architecture:** Add three Qt-free bounded stores/services, then expose read-only health and decision data through the existing operations context and right dock. Issue publishing obtains credentials through a fixed-target transport abstraction and retains the current direct Git credential path only as an explicitly reported compatibility fallback.
**Tech Stack:** Python 3.12, SQLite/atomic JSON, PySide6, existing Harness 0.2.1-rc.5 plugin protocol, pytest and Node verification scripts.
**Spec:** `docs/superpowers/specs/2026-10-31-dsh-production-hardening.md`
## Global Constraints
- Work only in `.worktrees/codex-dsh` on branch `codex/dsh`.
- Do not print, stage, or commit provider keys, issue tokens, or ignored bootstrap files.
- Do not change automatic-session snapshot-only admission, native confirmations, safe pause semantics, schedule idle requirement, or original queue resume rules.
- Every new operation must be bounded, fixed-target, auditable, and read-only unless it is an existing confirmed issue submission.
- Use controlled fixtures for tests; never write a real user CAD project.
---
### Task 1: Health snapshot service
**Files:**
- Create: `src/dx_productivity/dsh/health.py`
- Create: `tests/dsh/test_health.py`
**Interfaces:**
- Produces `HealthStore.snapshot(service, *, now=None) -> dict` with `status`, `checked_utc`, and bounded `checks`.
- Each check has `name`, `status` (`ok|degraded|blocked`), `code`, `message`, and optional `updated_utc`.
- [x] Write tests for no provider, healthy local stores, stale ticket cache, active worker, and credential rotation warning.
- [x] Run the focused tests and confirm the new module is missing or tests fail.
- [x] Implement deterministic local checks without network or CAD calls; provider availability is based on configured metadata, not a live model request.
- [x] Bound all messages and scrub secrets before returning values.
- [x] Run `pytest -q tests/dsh/test_health.py` and confirm all pass.
### Task 2: Append-only decision log
**Files:**
- Create: `src/dx_productivity/dsh/decision_log.py`
- Create: `tests/dsh/test_decision_log.py`
- Modify: `src/dx_productivity/dsh/incidents.py`
- Modify: `src/dx_productivity/trial_task_center/ui/dsh_operations.py`
**Interfaces:**
- Produces `DecisionLog(data_root).append(record) -> dict`, `.list(limit=20) -> list[dict]`, and `.verify(sequence=None) -> dict`.
- `append` stores a SHA256-linked JSON line with `sequence`, `previous_hash`, `record_hash`, `incident_id`, `trigger`, `evidence_refs`, `gates`, `candidate_action`, `authorized_scope`, `outcome`, and `human_takeover`.
- [x] Write tests proving append order, hash verification, restart recovery, bounded output, and tamper detection.
- [x] Run focused tests and observe expected failure.
- [x] Implement append-only atomic writes; reject malformed identifiers and scrub all free text.
- [x] Record diagnosis gates and actual result after each automatic intervention; never record model prose as verification.
- [x] Add recent decision rows to operations context and experience view.
- [x] Run focused tests and existing incident/UI tests.
### Task 3: Fixed-target issue transport
**Files:**
- Create: `src/dx_productivity/dsh/issue_transport.py`
- Create: `tests/dsh/test_issue_transport.py`
- Modify: `src/dx_productivity/dsh/issue_credentials.py`
- Modify: `src/dx_productivity/dsh/git_issues.py`
- Modify: `src/dx_productivity/dsh/ticket_tracking.py`
**Interfaces:**
- Produces `IssueTransport(config_path).credentials(target, *, now=None) -> dict` with only in-memory `headers`, `source`, `expires_utc`, and `rotation_required`.
- Supports `broker_url` plus instance ID and a short-lived exchange response; direct bootstrap/Git credential fallback is tagged and never cached as a token.
- [x] Write tests for fixed target validation before credentials, broker success, broker expiry, direct fallback warning, and no-token cache/log output.
- [x] Run focused tests and observe expected failure.
- [x] Implement strict HTTPS host/path validation and bounded broker request with no redirects; hash or omit instance identifiers in logs.
- [x] Make Git issue publishing request fresh transport credentials for each submission and preserve existing idempotency marker behavior.
- [x] Keep read-only ticket tracking compatible with the transport and expose credential source/expiry only.
- [x] Run focused issue, ticket, and credential tests.
### Task 4: Health and decisions in the Harness dock
**Files:**
- Modify: `src/dx_productivity/trial_task_center/ui/dsh_operations.py`
- Modify: `src/dx_productivity/dsh/native_runtime.py`
- Modify: `integration/dsh/task-plugin/model_results.mjs`
- Modify: `integration/dsh/task-plugin/client.js`
- Modify: `tests/ui/test_dsh_operations.py`
- Modify: `tools/verify_dsh_model_results.mjs`
**Interfaces:**
- Operations context adds `health` and `decisions` arrays with bounded rows.
- Existing operations actions gain `health` and `decisions` read operations; no new write capability is registered.
- [x] Add failing UI/model projection tests for bounded health and decision data.
- [x] Implement read operations through the existing background observation lane and refresh button.
- [x] Add overview cards and a compact decision timeline without remounting the dock or changing the selected task.
- [x] Verify model output keeps IDs, hashes, and unknown values explicit and omits credentials.
- [x] Run Node projection, dock startup, UI, and runtime guard checks.
### Task 5: Full verification and documentation
**Files:**
- Modify: `docs/user/dsh-ai-supervision.md`
- Modify: `docs/testing/2026-10-31-dsh-production-hardening.md`
- [x] Run focused DSH tests, existing relevant UI/lifecycle tests, Node checks, `git diff --check`, and credential scan.
- [x] Run real two-process Qt/CDP acceptance using controlled fixtures.
- [x] Record exact artifact paths, limits, and whether a broker was available.
- [x] Commit only intended files on `codex/dsh`; verify clean status and commit ID.
@@ -0,0 +1,47 @@
# DSH 生产化加固设计
## 目标
在现有 DSH 七项能力和原生确认边界之上,补齐运行健康、自动处理决策审计和工单凭据生命周期,降低上线后的不可观测、误判和凭据泄露风险。
## 设计边界
- 模型仍不能直接写 CAD、绕过原生确认、扩大重试额度或提交任意 URL。
- 排期仍要求任务中心打开且空闲;异常处理仍只使用已授权的原执行上下文。
- 健康信息只读;不向远端发送工程源文件、模型隐藏思维或凭据。
- 工单提交保持固定仓库和事件标记去重;优先使用中转服务短期令牌,直连凭据只作为明确配置的兼容路径。
## 组件
### 运行健康中心
新增 `dsh/health.py`,收集模型提供方、Harness 子进程、任务中心工作线程、运行队列、排期、证据目录和工单同步的本地状态。每项返回 `ok`、`degraded` 或 `blocked`、更新时间、错误码和可执行建议;不得把异常文本中的凭据带入结果。健康读取为只读快照,不能触发模型请求、CAD 操作或工单写入。
右侧概览增加健康卡片和“重新检查”按钮。检查使用现有后台读取通道,活动执行期间不阻塞执行器;展示“模型未配置”“等待原生确认”“队列待核验”等真实边界。
### 异常决策记录
新增 `dsh/decision_log.py`,对每次自动介入保存不可变的决策记录:事件编号、触发来源、证据引用、门禁结果、候选动作、实际授权范围、结果和人工接管状态。保存结构化摘要,不保存隐藏思维;每条记录有 SHA256 链接和序号,追加失败不得覆盖旧记录。
自动诊断和 DSH 右侧经验页展示最近记录。模型建议、工单关闭和“已解决”文字仍不能单独把记录标为已验证;验证必须来自持久化执行证据。
### 工单凭据生命周期
新增 `dsh/issue_transport.py`,支持固定仓库的中转模式:客户端只保存中转地址和安装实例标识,通过一次性或短期令牌换取服务端签发的 issue-only 访问凭据;令牌只用于固定服务器、仓库、事件标记和工单创建/附件上传。缓存只保存凭据来源、过期时间和错误码,不保存访问令牌。
兼容模式继续读取本机 Git credential,但预置静态令牌只作为开发/验收 fallback,并在健康中心标记为 `credential_rotation_required`。任何目标校验都必须在获取凭据前完成。
## 错误处理
- 健康检查失败返回脱敏错误码和最近一次成功时间,不能让后台轮询打断执行。
- 决策记录写入失败时保留内存结果,禁止把“未记录”显示为“已完成”;结束后补写并提示人工导出证据。
- 中转服务超时只进入待提交队列,不能自动重复 CAD 执行;使用事件编号和请求幂等键去重。
- 令牌过期、仓库身份不匹配和附件失败分别呈现,均保留本地证据包。
## 验收标准
1. 健康接口和右侧概览能在无模型、运行中、排期阻断、工单离线四种状态下给出正确状态。
2. 自动介入至少生成一条追加式决策记录,重启后可恢复并校验哈希,旧记录不可被覆盖。
3. 中转凭据不进入模型上下文、日志、缓存和工单正文;过期后只读健康状态变为降级。
4. 固定目标校验失败时不读取任何凭据、不发起网络请求。
5. 既有 DSH 回归和两进程 Qt/CDP 验收保持通过。
@@ -0,0 +1,21 @@
# DSH 生产化加固验证记录
本轮在 `codex/dsh` 继续加固健康检查、自动处理决策审计和固定目标工单凭据传输。所有验证使用临时状态目录、受控任务服务和无写入 CAD fixture。
## 已完成的验证
- 健康快照:未配置模型、后台工作线程、状态目录、工单缓存和预置凭据轮换提示均有对应状态;输出不包含令牌。
- 决策日志:追加记录、重启读取、SHA256 链校验、篡改检测和有界输出通过。
- 工单传输:固定目标在凭据读取前校验;中转短期凭据不落盘;中转失败标记降级;预置凭据仅作为兼容 fallback。
- UI:概览显示健康摘要和最近决策,刷新走既有后台观察通道,不改变选中任务和执行器占用。
- 原有事件诊断、工单只读同步、DSH 操作页和 Qt UI 回归保持通过。
完整相关回归通过 **287 项**:203 项 DSH/UI/队列/打包回归、82 项存储与监管回归、2 项执行反馈回归;另有 50 项本轮定向测试通过(其中包含上述回归的重叠覆盖)。
真实模型与两进程 Qt/CDP 验收报告:[report.json](../../build/dsh-operations-hardening-verified/20261031-093920/report.json)。两次应用进程均正常退出,原队列重启续跑、排期授权执行一次、工单只读同步、健康概览和七页签点击均通过;受控执行器共 3 次,`realCadStarted=false`、`remoteIssuesCreated=false`、源 fixture 哈希未变化。
截图:[初始运行页](../../build/dsh-operations-hardening-verified/20261031-093920/initial-desktop.png)、[重启后工单页](../../build/dsh-operations-hardening-verified/20261031-093920/after-restart-operations.png)。
## 当前验收边界
本轮没有部署真实中转服务,因此 broker 只用受控 HTTP 响应验证协议和凭据不落盘行为;生产部署仍需配置固定服务器、实例注册、短期令牌过期和吊销策略。没有改变真实 CAD 写入门禁,也没有构建新 EXE。
+4 -2
View File
@@ -57,6 +57,8 @@ Harness 使用任务中心数据目录下独立的 `dsh/harness-home` 保存会
右侧保留官方三栏界面,在“任务监管”内部提供 **概览、运行、影响、排期、经验、工单、用量** 七个页签。默认进入概览。各页按钮直接请求任务中心,原生窗口负责需要确认的操作;不需要发送特定模型提示或记忆工具名。
概览页还显示 DSH 运行健康和最近自动处理决策。健康状态只检查本地配置、任务中心后台工作线程、状态目录、工单缓存和凭据来源,不会为了探活调用模型、CAD 或远端写接口。决策记录包含事件、证据引用、门禁、授权范围、实际结果和人工接管标记,并通过追加式 SHA256 链保存;模型文字、工单关闭和“已解决”描述都不会单独成为修复证明。
### 运行与重启恢复
**运行**页显示实际执行所属任务、运行编号、阶段、耗时、当前进度、已保存/失败/未命中数量及安全暂停请求。即使左侧选中其他任务,活动执行仍显示真实所有者。请求暂停后,执行器在当前项安全收尾后停止;等待期间仍显示执行中。
@@ -101,9 +103,9 @@ Harness 使用任务中心数据目录下独立的 `dsh/harness-home` 保存会
每次自动介入在数据目录 `dsh/incidents/<事件编号>/` 保存:前后任务快照、脱敏诊断包、事件时间线、规则门禁明细、模型可见会话、尝试动作、实际核验结果、工单草稿和带 SHA256 清单的 `evidence.zip`。保留可审阅的诊断说明,不要求模型输出隐藏思维过程;“取证”只收集本工具已有的日志与运行记录,不扫描无关网络流量、工程源文件或用户文件。
工单默认提交到 `https://git.simgreek.com/Skyworth_Digital_Hardware/dxdesigner-part-replacement`。随客户端预填专用上报账户 `dsh-issue-reporter` 的 `write:issue` 令牌,其他电脑无需安装 Git、登录或提供个人凭据。该账户为非管理员受限账户,专用团队仅包含本仓库,只有工单写权限;代码、拉取请求、发布、组织、包和 Actions 权限均未授予。令牌不交给模型或网页。
工单默认提交到 `https://git.simgreek.com/Skyworth_Digital_Hardware/dxdesigner-part-replacement`。生产环境优先配置固定仓库的中转地址:客户端使用安装实例标识换取短期 `write:issue` 凭据,令牌只在内存中使用,不进入模型、缓存、日志或工单正文。中转不可用时才进入本机 Git credential 或预置令牌兼容路径,概览健康会标记“需要轮换凭据”。预置账户必须是非管理员、仅绑定本仓库的 `write:issue` 权限,并支持服务端吊销和轮换。
预填资源为 `integration/dsh/issue-bootstrap.json`,已加入 Git 忽略;打包时放入 `dsh-runtime/issue-bootstrap.json`,可跨电脑使用,不依赖开发电脑的 DPAPI。管理员侧的账户密码和令牌恢复资料单独保存在本机 DPAPI 文件,不能随包分发。没有预填资源时,DSH 发布打包会明确报错,避免生成缺少上报身份的安装包。
开发/验收预填资源为 `integration/dsh/issue-bootstrap.json`,已加入 Git 忽略;生产打包不应依赖它,而应配置中转地址和实例注册流程。管理员侧的账户密码和令牌恢复资料单独保存在本机 DPAPI 文件,不能随包分发。没有中转或明确兼容凭据时,工单保留本地证据并显示待提交状态。
`dsh/issue-config.json` 可覆盖启用状态与目标。预填令牌仅用于其绑定的服务器和仓库;自定义其他目标不会携带该令牌,而沿用该目标的本机 Git 凭据。受限共享令牌可在 Git 服务端吊销;吊销后保留证据待补交,并需更新分发资源。
+13 -2
View File
@@ -24,11 +24,22 @@ window.__ModuleLoader__.load({id:'@dx/dsh-task',factory:require=>{
border:0,borderRadius:10,background:'transparent',color:'var(--dsw-alias-label-primary)',cursor:'pointer',font:'inherit'}},
h(IconSettingsOutlineMedium,{size:18}),wide&&'工具设置');
}
function OverviewPanel({data,notice,request}){
function OverviewPanel({data,notice,request,perform}){
const section=(title,text)=>h('section',{style:{marginBottom:24}},h('h3',{style:{fontSize:14,fontWeight:500,marginBottom:10}},title),
h('div',{style:{whiteSpace:'pre-wrap',color:'var(--dsw-alias-label-secondary)',lineHeight:1.8}},text));
const health=data?.operations?.health;
const decisions=data?.operations?.decisions||[];
const healthText=health?.checks?.map(row=>`${row.name}: ${row.status} · ${row.message}`).join('\n')||'正在读取运行健康…';
return h('div',{'data-testid':'dx-task-overview'},
section('当前任务',data?(data.task_name||data.task_id||'尚未选择任务'):'读取中…'),
h('section',{'data-testid':'dx-health-summary',style:{marginBottom:24}},
h('h3',{style:{fontSize:14,fontWeight:500,marginBottom:10}},'运行健康 · '+(health?.status||'读取中')),
h('div',{style:{whiteSpace:'pre-wrap',color:'var(--dsw-alias-label-secondary)',lineHeight:1.8}},healthText),
h('button',{type:'button',onClick:()=>perform({operation:'health'}),style:{...buttonStyle,marginTop:8}},'重新检查健康状态')),
h('section',{'data-testid':'dx-decision-timeline',style:{marginBottom:24}},
h('h3',{style:{fontSize:14,fontWeight:500,marginBottom:10}},'自动处理决策记录'),
decisions.length?decisions.slice(0,5).map(row=>h('p',{key:row.record_hash,style:{fontSize:12,lineHeight:1.7,margin:'6px 0'}},`#${row.sequence} · ${row.trigger} · ${row.outcome} · ${row.human_takeover?'人工接管':'自动策略'}`)):h('p',{style:{fontSize:12,color:'var(--dsw-alias-label-secondary)'}},'暂无自动处理记录。'),
h('button',{type:'button',onClick:()=>perform({operation:'decisions'}),style:{...buttonStyle,marginTop:8}},'刷新决策记录')),
data?.lifecycle?.request_id&&h('section',{'data-testid':'dx-task-lifecycle',style:{marginBottom:24}},
h('h3',{style:{fontSize:14,fontWeight:500}},data.lifecycle.title||'对话任务操作'),
h('p',{'data-testid':'dx-lifecycle-status'},({checking:'正在核对真实任务',awaiting_confirmation:'请核对原生确认窗口',
@@ -235,7 +246,7 @@ window.__ModuleLoader__.load({id:'@dx/dsh-task',factory:require=>{
h('div',{role:'tablist','aria-label':'任务监管',style:{display:'flex',flexWrap:'wrap',gap:4,marginBottom:18,paddingBottom:10,borderBottom:border}},
...tabs.map(([id,title])=>h('button',{key:id,id:'dx-tab-'+id,role:'tab',type:'button','data-testid':'dx-operations-tab-'+id,'aria-selected':tab===id,'aria-controls':'dx-pane-'+id,onClick:()=>setTab(id),style:{...buttonStyle,border:tab===id?border:'1px solid transparent',fontWeight:tab===id?600:400,color:tab===id?'var(--dsw-alias-label-primary)':secondary}},title))),
h('div',{role:'tabpanel',id:'dx-pane-'+tab,'aria-labelledby':'dx-tab-'+tab},
tab==='overview'?h(OverviewPanel,{data,notice,request}):
tab==='overview'?h(OverviewPanel,{data,notice,request,perform}):
tab==='running'?h(RunningPanel,{operations,perform,pending:pending||operations.busy,request}):
tab==='impact'?h(ImpactPanel,{report:operations.impact,scope,perform,pending:pending||operations.busy}):
tab==='schedules'?h(SchedulesPanel,{operations,scope,perform,pending:pending||operations.busy}):
+1 -1
View File
@@ -44,7 +44,7 @@ export function registerLifecycle(ctx){
{name:'dx_task_execute',capability:'execute',parameters:{operation:{type:'string',required:true,enum:['start','pause','resume']},task_ids:{type:'array',items:{type:'string'},description:ids.description},run_id:{type:'string',description:'For resume only, use the real saved run ID from execution history. Original frozen scope/order and existing checkpoint are inspected, then natively confirmed.'}},
description:'Start or continue the existing serial execution queue for an explicit ordered list of real task IDs. Prechecks and native confirmation are required; actual execution can replace and save engineering files using existing backup, interruption and verification gates. Waits for real results including saved, failed and no-match counts. For pause, provide exactly the active run scope; requests safe stopping after the current item and returns pause_requested, not a claim that stopping has completed. No force-stop, no new CAD scripting or hidden retry. Interactive conversations only.'},
{name:'dx_task_operations',capability:'operations',parameters:{
operation:{type:'string',required:true,enum:['refresh','history','impact','experience','tickets','refresh_ticket','schedule','schedule_dialog','cancel_schedule','run_schedule','resume']},
operation:{type:'string',required:true,enum:['refresh','history','health','decisions','impact','experience','tickets','refresh_ticket','schedule','schedule_dialog','cancel_schedule','run_schedule','resume']},
task_ids:{type:'array',items:{type:'string'},description:ids.description},run_id:{type:'string'},schedule_id:{type:'string'},incident_id:{type:'string'},issue_code:{type:'string'},
title:{type:'string'},priority:{type:'string',enum:['low','normal','high','urgent']},
not_before:{type:'string',description:'For schedule: earliest run time as ISO 8601 with explicit timezone offset, e.g. 2026-10-30T18:00:00+08:00. Ask when ambiguous; never guess a date.'},
@@ -9,6 +9,8 @@ const experience=row=>({...fields(row,['incident_id','issue_code','verified','ve
summary:short(row.summary,1200),message:short(row.message)});
const ticket=row=>({...fields(row,['incident_id','number','url','state','status','last_sync_at','last_attempt_at','error','reason']),
comments:row.comments?.slice(-3).map(c=>({...fields(c,['id','created_at','updated_at','author']),body:short(c.body)}))});
const health=row=>({...fields(row,['status','checked_utc']),checks:(row.checks||[]).slice(0,10).map(check=>fields(check,['name','status','code','message','updated_utc']))});
const decision=row=>fields(row,['sequence','at_utc','previous_hash','record_hash','incident_id','trigger','evidence_refs','gates','candidate_action','authorized_scope','outcome','human_takeover']);
function impact(report){
if(!report||typeof report!=='object')return report;
const rows=report.rows||[];
@@ -29,13 +31,15 @@ export function compactResult(value){
if(result.experiences)result.experiences=result.experiences.slice(0,5).map(experience);
if(result.tickets)result.tickets=result.tickets.slice(0,5).map(ticket);
if(result.ticket)result.ticket=ticket(result.ticket);
if(result.health)result.health=health(result.health);
if(result.decisions)result.decisions=result.decisions.slice(0,10).map(decision);
return JSON.parse(JSON.stringify(result));
}
export function modelSnapshot(value){
const result=fields(value,['task_id','revision','task_name','status','progress','rules_count','issue','issue_code','events','action_result']);
const ops=value.operations||{};
result.operations=compactResult({busy:ops.busy===true,live_run:ops.live_run,last_execution:ops.last_execution,
runs:ops.runs||[],schedules:ops.schedules||[],experiences:ops.experiences||[],tickets:ops.tickets||[]});
runs:ops.runs||[],schedules:ops.schedules||[],experiences:ops.experiences||[],tickets:ops.tickets||[],health:ops.health,decisions:ops.decisions||[]});
if(value.automation?.incident_id)result.automation=fields(value.automation,['incident_id','task_id','issue_code','status','attempts','verification']);
if(value.lifecycle?.status)result.lifecycle=fields(value.lifecycle,['request_id','status','task_ids','run_id']);
return result;
+85
View File
@@ -0,0 +1,85 @@
"""Append-only, hash-linked records for automatic DSH decisions."""
from __future__ import annotations
from datetime import datetime, timezone
from hashlib import sha256
import json
from pathlib import Path
import re
from .evidence import scrub
_ID = re.compile(r"^[0-9a-f]{24}$")
_MAX = 2000
def _bounded(value, limit=1000):
value = scrub(value)
if isinstance(value, str):
return value[:limit]
if isinstance(value, list):
return [_bounded(item, limit) for item in value[:20]]
if isinstance(value, dict):
return {str(key)[:80]: _bounded(item, limit) for key, item in list(value.items())[:40]}
return value
class DecisionLog:
def __init__(self, data_root):
self.path = Path(data_root) / "dsh" / "decisions.jsonl"
def append(self, record):
if not isinstance(record, dict) or not _ID.fullmatch(str(record.get("incident_id", ""))):
raise ValueError("incident_id")
rows = self.list(limit=_MAX)
previous = rows[-1]["record_hash"] if rows else ""
value = {"sequence": len(rows) + 1, "at_utc": datetime.now(timezone.utc).isoformat(),
"previous_hash": previous, "incident_id": record["incident_id"],
"trigger": _bounded(record.get("trigger", ""), 160),
"evidence_refs": _bounded(record.get("evidence_refs", []), 300),
"gates": _bounded(record.get("gates", {}), 300),
"candidate_action": _bounded(record.get("candidate_action", "none"), 160),
"authorized_scope": _bounded(record.get("authorized_scope", {}), 600),
"outcome": _bounded(record.get("outcome", "unknown"), 160),
"human_takeover": bool(record.get("human_takeover", False))}
payload = json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":"))
value["record_hash"] = sha256(payload.encode("utf8")).hexdigest()
self.path.parent.mkdir(parents=True, exist_ok=True)
with self.path.open("a", encoding="utf8") as stream:
stream.write(json.dumps(value, ensure_ascii=False, sort_keys=True) + "\n")
stream.flush()
return dict(value)
def list(self, limit=20):
if not isinstance(limit, int) or limit <= 0:
return []
rows = []
try:
with self.path.open(encoding="utf8") as stream:
for line in stream:
if len(rows) >= min(limit, _MAX):
break
try:
value = json.loads(line)
except (ValueError, TypeError):
continue
if isinstance(value, dict) and _ID.fullmatch(str(value.get("incident_id", ""))):
rows.append(_bounded(value, 2000))
except OSError:
return []
return rows
def verify(self, sequence=None):
rows = self.list(limit=_MAX)
previous = ""
checked = 0
for value in rows:
if sequence is not None and value.get("sequence") > sequence:
break
expected = value.get("record_hash")
body = {key: item for key, item in value.items() if key != "record_hash"}
payload = json.dumps(body, ensure_ascii=False, sort_keys=True, separators=(",", ":"))
if value.get("previous_hash") != previous or expected != sha256(payload.encode("utf8")).hexdigest():
return {"valid": False, "checked": checked, "error": "hash_mismatch"}
previous, checked = expected, checked + 1
return {"valid": True, "checked": checked, "last_hash": previous}
+4
View File
@@ -13,6 +13,7 @@ from uuid import uuid4
from .evidence import scrub
from .issue_credentials import issue_target, load_bootstrap
from .issue_transport import IssueTransport
class NoRedirect(urllib.request.HTTPRedirectHandler):
@@ -34,6 +35,9 @@ class GitIssuePublisher:
owner, repo = config['repository'].split('/')
if not all(value and all(c.isalnum() or c in '-_.' for c in value) and value not in {'.', '..'} for value in (owner, repo)):
raise ValueError('invalid_issue_repository')
transport = IssueTransport(self.config_path).credentials(config)
if transport.get('headers'):
return config, transport['headers'], transport.get('source', 'issue_transport')
bootstrap = load_bootstrap()
if (bootstrap and bootstrap['server'].rstrip('/') == config['server'].rstrip('/')
and bootstrap['repository'] == config['repository']):
+70
View File
@@ -0,0 +1,70 @@
"""Read-only, bounded health checks for the DSH assistant."""
from __future__ import annotations
from datetime import datetime, timezone
import json
from pathlib import Path
from .evidence import scrub
from .issue_credentials import load_bootstrap
def _now():
return datetime.now(timezone.utc).isoformat(timespec="seconds")
class HealthStore:
def __init__(self, data_root):
self.data_root = Path(data_root)
def snapshot(self, service, *, now=None):
checked = now.isoformat(timespec="seconds") if now is not None else _now()
checks = [self._provider(), self._execution(service), self._stores(), self._tickets(), self._credentials()]
status = "ok" if all(row["status"] == "ok" for row in checks) else (
"blocked" if any(row["status"] == "blocked" for row in checks) else "degraded")
return scrub({"status": status, "checked_utc": checked, "checks": checks})
def _provider(self):
path = self.data_root / "dsh" / "provider.json"
try:
value = json.loads(path.read_text(encoding="utf8"))
configured = isinstance(value, dict) and bool(value.get("base_url")) and bool(value.get("default_model"))
except (OSError, ValueError, TypeError):
configured = False
return {"name": "provider", "status": "ok" if configured else "degraded",
"code": "ready" if configured else "provider_unconfigured",
"message": "模型提供方已配置" if configured else "尚未配置模型提供方"}
def _execution(self, service):
active = bool(getattr(service, "_workers", set()))
return {"name": "execution", "status": "degraded" if active else "ok",
"code": "worker_active" if active else "idle",
"message": "任务中心有后台操作" if active else "任务中心空闲"}
def _stores(self):
dsh = self.data_root / "dsh"
try:
if dsh.exists() and not dsh.is_dir():
raise OSError("state path is not a directory")
# Health is observational: do not create files or alter timestamps.
return {"name": "stores", "status": "ok", "code": "ready", "message": "本地状态目录可用"}
except OSError:
return {"name": "stores", "status": "blocked", "code": "store_unavailable", "message": "本地状态目录不可写"}
def _tickets(self):
cache = self.data_root / "dsh" / "ticket-cache"
if not cache.exists():
return {"name": "tickets", "status": "ok", "code": "no_cache", "message": "暂无工单同步缓存"}
try:
count = sum(1 for path in cache.iterdir() if path.is_file() and path.suffix == ".json")
except OSError:
return {"name": "tickets", "status": "degraded", "code": "ticket_cache_unreadable", "message": "工单缓存不可读"}
return {"name": "tickets", "status": "ok", "code": "cache_available", "message": f"已有 {min(count, 100)} 条工单缓存"}
def _credentials(self):
value = load_bootstrap()
if value:
return {"name": "issue_credentials", "status": "degraded", "code": "credential_rotation_required",
"message": "当前使用预置工单凭据,建议切换短期中转凭据"}
return {"name": "issue_credentials", "status": "ok", "code": "machine_credential_or_disabled",
"message": "未发现预置工单凭据"}
+29 -1
View File
@@ -9,6 +9,7 @@ import re
from threading import Event, Lock
from .evidence import IncidentEvidence, scrub, write_json
from .decision_log import DecisionLog
from .rules import RETRYABLE_CODES
from .snapshot import TaskSnapshot
@@ -35,6 +36,28 @@ class IncidentRunner:
def __init__(self, data_root, service, analyze, publish, notify=lambda row: None):
self.data_root, self.service = Path(data_root), service
self.analyze, self.publish, self.notify = analyze, publish, notify
self.decisions = DecisionLog(self.data_root)
def _record_decision(self, record, evidence, *, outcome, human_takeover=False):
refs = []
for path in sorted(evidence.directory.iterdir()):
if path.is_file() and path.name in {'snapshot.json', 'snapshot-after.json', 'gates.json',
'actions.json', 'verification.json', 'diagnose.json', 'reflect.json', 'evidence.zip'}:
refs.append(path.relative_to(self.data_root).as_posix())
try:
value = self.decisions.append({
'incident_id': record['incident_id'], 'trigger': record.get('issue_code', ''),
'evidence_refs': refs, 'gates': record.get('gates', {}),
'candidate_action': 'retry_failed' if record.get('attempts') else 'none',
'authorized_scope': {'task_id': record.get('task_id'), 'run_id': record.get('run_id')},
'outcome': outcome, 'human_takeover': human_takeover})
record['decision_sequence'] = value['sequence']
record['decision_hash'] = value['record_hash']
except Exception as error:
# Evidence and the incident state remain authoritative; make the
# missing append visible rather than claiming an auditable result.
record['decision_log_status'] = 'unavailable'
evidence.event('decision_log_unavailable', error=type(error).__name__)
def run(self, task_id, issue_code, message, *, run_id='observation', execution_authorized=False, stop_event=None):
with _RUN_LOCK:
@@ -136,6 +159,8 @@ class IncidentRunner:
if p.queue_complete and not p.failed_items and not current.interrupted and not outcome.paused:
update('resolved')
evidence.capture('diagnostics-after')
self._record_decision(record, evidence, outcome='resolved')
write_json(state_path, record)
evidence.pack(record)
return record
except Exception as error:
@@ -158,7 +183,10 @@ class IncidentRunner:
update('pending_issue')
self._draft(record, evidence)
archive = evidence.pack(record)
return self._publish(record, evidence, archive)
result = self._publish(record, evidence, archive)
self._record_decision(record, evidence, outcome=record.get('status', 'pending_issue'))
write_json(state_path, record)
return result
def _snapshot(self, task_id, issue_code, message):
try:
@@ -0,0 +1,60 @@
"""Fixed-target issue credentials with optional short-lived broker exchange."""
from __future__ import annotations
from datetime import datetime, timedelta, timezone
import json
from pathlib import Path
import urllib.parse
import urllib.request
from .issue_credentials import issue_target, load_bootstrap
def _post_broker(url, payload, timeout=10):
request = urllib.request.Request(url, data=json.dumps(payload).encode("utf8"),
headers={"Content-Type": "application/json", "Accept": "application/json"})
with urllib.request.build_opener(_NoRedirect()).open(request, timeout=timeout) as response:
return json.loads(response.read(64 * 1024))
class _NoRedirect(urllib.request.HTTPRedirectHandler):
def redirect_request(self, *args, **kwargs):
return None
class IssueTransport:
def __init__(self, config_path):
self.config_path = Path(config_path)
def credentials(self, target, *, now=None):
config = issue_target(self.config_path)
if not self._same_target(config, target):
return {"source": "invalid_target", "rotation_required": False}
broker = config.get("broker_url")
if broker:
try:
parsed = urllib.parse.urlsplit(str(broker))
if parsed.scheme != "https" or not parsed.hostname:
raise ValueError("invalid_broker_url")
response = _post_broker(broker, {"server": target["server"], "repository": target["repository"],
"instance_id": str(config.get("instance_id", ""))[:128]})
token = response.get("token")
expires = int(response.get("expires_in", 0))
if not isinstance(token, str) or not token or not 30 <= expires <= 3600:
raise ValueError("invalid_broker_response")
stamp = now or datetime.now(timezone.utc)
return {"headers": {"Authorization": "token " + token}, "source": "broker",
"expires_utc": (stamp + timedelta(seconds=expires)).isoformat(), "rotation_required": False}
except Exception as error:
return {"source": "broker_unavailable", "error": type(error).__name__, "rotation_required": True}
bootstrap = load_bootstrap()
if bootstrap and self._same_target(bootstrap, target):
return {"headers": {"Authorization": "token " + bootstrap["token"]},
"source": "prefilled_issue_token", "expires_utc": None, "rotation_required": True}
return {"source": "credential_unavailable", "rotation_required": True}
@staticmethod
def _same_target(left, right):
return (isinstance(left, dict) and isinstance(right, dict)
and str(left.get("server", "")).rstrip("/") == str(right.get("server", "")).rstrip("/")
and left.get("repository") == right.get("repository"))
@@ -17,6 +17,8 @@ from ...dsh.scheduling import ScheduleStore
from ...dsh.experience import ExperienceStore
from ...dsh.ticket_tracking import TicketTracker
from ...dsh.impact import execution_impact
from ...dsh.health import HealthStore
from ...dsh.decision_log import DecisionLog
from ..errors import TaskCenterError
from .dsh_task_lifecycle import DshLifecycleDialog
from .workers import ServiceWorker
@@ -66,11 +68,14 @@ class DshOperationsController(QObject):
self.schedules = ScheduleStore(self.root)
self.experiences = ExperienceStore(self.root)
self.tickets = TicketTracker(self.root)
self.health = HealthStore(self.root)
self.decisions = DecisionLog(self.root)
self.dialog = None
self.worker = None
self.request_id = None
self.results = {}
self.cache = {'runs': [], 'schedules': [], 'experiences': [], 'tickets': [], 'activity': {}}
self.cache = {'runs': [], 'schedules': [], 'experiences': [], 'tickets': [], 'decisions': [],
'health': {'status': 'degraded', 'checks': []}, 'activity': {}}
self.timer = QTimer(self)
self.timer.setInterval(15000)
self.timer.timeout.connect(self.tick)
@@ -161,7 +166,8 @@ class DshOperationsController(QObject):
self.tickets.refresh(candidates[0]['incident_id'])
tickets = self.tickets.list_tickets()
return {'runs': runs, 'schedules': self.schedules.evaluate(service),
'experiences': self.experiences.refresh()[:20], 'tickets': tickets[:20]}
'experiences': self.experiences.refresh()[:20], 'tickets': tickets[:20],
'health': self.health.snapshot(service), 'decisions': self.decisions.list(20)}
def refresh(self, *, synchronize_tickets=False):
if self.window._closed or self.window._workers or self.busy or self._other_dialog():
@@ -230,13 +236,13 @@ class DshOperationsController(QObject):
self.request_id = key
self._state('checking')
allowed = {
'refresh': set(), 'history': set(), 'impact': {'task_ids'}, 'experience': {'issue_code'}, 'tickets': set(),
'refresh': set(), 'history': set(), 'health': set(), 'decisions': set(), 'impact': {'task_ids'}, 'experience': {'issue_code'}, 'tickets': set(),
'refresh_ticket': {'incident_id'}, 'schedule_dialog': {'task_ids'}, 'cancel_schedule': {'schedule_id'},
'run_schedule': {'schedule_id'}, 'open_incident': {'incident_id'},
'schedule': {'task_ids', 'priority', 'not_before', 'due_at', 'dependencies', 'auto_start', 'idle_only', 'title'}}
if op not in allowed or set(spec) - ({'operation'} | allowed[op]):
self._failed(key, ValueError('unknown operation fields')); return
if op in {'refresh', 'history', 'tickets'}:
if op in {'refresh', 'history', 'health', 'decisions', 'tickets'}:
def read(value):
self.cache.update(value)
self._respond(key, status='read', **value)
+33
View File
@@ -0,0 +1,33 @@
import json
import pytest
from dx_productivity.dsh.decision_log import DecisionLog
def _row(incident='a' * 24, outcome='pending_issue'):
return {'incident_id': incident, 'trigger': 'execution_failed', 'evidence_refs': ['dsh/incidents/x/gates.json'],
'gates': {'execution_authorized': True, 'retry_budget_available': False},
'candidate_action': 'none', 'authorized_scope': {'task_ids': ['task-1']}, 'outcome': outcome,
'human_takeover': False}
def test_decision_log_is_hash_linked_and_survives_restart(tmp_path):
store = DecisionLog(tmp_path)
first = store.append(_row())
second = DecisionLog(tmp_path).append(_row('b' * 24, 'resolved'))
assert first['sequence'] == 1
assert second['previous_hash'] == first['record_hash']
assert DecisionLog(tmp_path).verify()['valid'] is True
assert [row['sequence'] for row in DecisionLog(tmp_path).list()] == [1, 2]
def test_decision_log_detects_tampering_and_bounds_text(tmp_path):
store = DecisionLog(tmp_path)
store.append(_row())
path = tmp_path / 'dsh' / 'decisions.jsonl'
raw = path.read_text(encoding='utf8').replace('execution_failed', 'x' * 5000)
path.write_text(raw, encoding='utf8')
assert DecisionLog(tmp_path).verify()['valid'] is False
with pytest.raises(ValueError, match='incident_id'):
store.append(_row('bad'))
+42
View File
@@ -0,0 +1,42 @@
import json
from pathlib import Path
from dx_productivity.dsh.health import HealthStore
class _Service:
_workers = set()
def test_health_snapshot_reports_unconfigured_provider_and_local_stores(tmp_path, monkeypatch):
monkeypatch.delenv('DSH_API_BASE', raising=False)
value = HealthStore(tmp_path).snapshot(_Service())
checks = {row['name']: row for row in value['checks']}
assert value['status'] == 'degraded'
assert checks['provider']['status'] == 'degraded'
assert checks['provider']['code'] == 'provider_unconfigured'
assert checks['stores']['status'] == 'ok'
def test_health_snapshot_marks_active_worker_and_static_issue_token(tmp_path, monkeypatch):
class Busy(_Service):
_workers = {object()}
provider = tmp_path / 'dsh' / 'provider.json'
provider.parent.mkdir(parents=True)
provider.write_text(json.dumps({'base_url': 'https://example.test/v1', 'default_model': 'm'}), encoding='utf8')
monkeypatch.setattr('dx_productivity.dsh.health.load_bootstrap', lambda: {'token': 'secret', 'username': 'u', 'scopes': ['write:issue'], 'server': 'https://git.test', 'repository': 'o/r'})
value = HealthStore(tmp_path).snapshot(Busy())
checks = {row['name']: row for row in value['checks']}
assert checks['execution']['status'] == 'degraded'
assert checks['execution']['code'] == 'worker_active'
assert checks['issue_credentials']['code'] == 'credential_rotation_required'
assert 'secret' not in json.dumps(value)
def test_health_snapshot_reports_stale_ticket_cache(tmp_path):
cache = tmp_path / 'dsh' / 'ticket-cache'
cache.mkdir(parents=True)
value = HealthStore(tmp_path).snapshot(_Service())
checks = {row['name']: row for row in value['checks']}
assert checks['tickets']['status'] == 'ok'
+3
View File
@@ -53,6 +53,9 @@ 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
decisions = (tmp_path / 'dsh' / 'decisions.jsonl').read_text(encoding='utf8').splitlines()
assert decisions
assert json.loads(decisions[-1])['incident_id'] == first['incident_id']
def test_uncertain_results_never_replay_even_when_model_requests_retry(tmp_path):
+46
View File
@@ -0,0 +1,46 @@
import json
from dx_productivity.dsh.issue_transport import IssueTransport
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')
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'
assert value['headers']['Authorization'] == 'token short-secret'
assert value['rotation_required'] is False
cache = list(tmp_path.rglob('*'))
assert all('short-secret' not in p.read_text(encoding='utf8', errors='ignore') for p in cache if p.is_file())
def test_transport_marks_bootstrap_fallback_for_rotation(tmp_path, monkeypatch):
config = tmp_path / 'issue-config.json'
config.write_text(json.dumps(TARGET), encoding='utf8')
monkeypatch.setattr('dx_productivity.dsh.issue_transport.load_bootstrap', lambda: {**TARGET, 'token': 'static-secret', 'username': 'u', 'scopes': ['write:issue']})
value = IssueTransport(config).credentials(TARGET)
assert value['source'] == 'prefilled_issue_token'
assert value['rotation_required'] is True
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')
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'
assert 'headers' not in value
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')
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'}
assert source == 'broker'
+12
View File
@@ -68,6 +68,18 @@ def test_history_read_busy_state_is_published_and_rejection_is_visible(ui, monke
assert view.operations['busy'] is False
def test_operations_context_publishes_health_and_decision_timeline(ui, monkeypatch, qtbot):
view = setup_view(ui, monkeypatch)
controller = ui.window._dsh_operations
controller.decisions.append({'incident_id': 'a' * 24, 'trigger': 'worker_error',
'evidence_refs': [], 'gates': {}, 'candidate_action': 'none',
'authorized_scope': {}, 'outcome': 'pending_issue'})
controller.refresh()
qtbot.waitUntil(lambda: controller.worker is None, timeout=5000)
assert 'health' in view.operations and isinstance(view.operations['health']['checks'], list)
assert view.operations['decisions'][0]['incident_id'] == 'a' * 24
def test_stopping_generation_does_not_request_pause_of_confirmed_task(ui, monkeypatch):
view = setup_view(ui, monkeypatch); ctl=ui.window._dsh_lifecycle
task_id=ui.tasks[0].task_id; ui.window._begin_run(task_id)