From e9b77186dcd517e36b89e144446300a484e28cc7 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: Fri, 30 Oct 2026 14:05:16 +0800 Subject: [PATCH] feat: prefill isolated issue-only reporter for clients without Git --- .gitignore | 1 + .../2026-10-30-dsh-automatic-intervention.md | 2 +- ...2026-10-30-dsh-prefilled-issue-reporter.md | 45 ++++++ docs/user/dsh-ai-supervision.md | 8 +- integration/dsh/task-plugin/client.js | 2 + packaging/DxPartReplace.spec | 4 + src/dx_productivity/dsh/git_issues.py | 36 +++-- src/dx_productivity/dsh/issue_credentials.py | 37 +++++ src/dx_productivity/dsh/native_runtime.py | 7 +- tests/dsh/test_issue_credentials.py | 72 ++++++++++ tests/dsh/test_native_runtime.py | 22 +++ tools/accept_dsh_desktop.py | 18 ++- tools/dsh_cdp.mjs | 12 +- tools/provision_dsh_issue_reporter.py | 131 ++++++++++++++++++ tools/verify_dsh_issue_reporter.py | 58 ++++++++ 15 files changed, 435 insertions(+), 20 deletions(-) create mode 100644 docs/testing/2026-10-30-dsh-prefilled-issue-reporter.md create mode 100644 src/dx_productivity/dsh/issue_credentials.py create mode 100644 tests/dsh/test_issue_credentials.py create mode 100644 tools/provision_dsh_issue_reporter.py create mode 100644 tools/verify_dsh_issue_reporter.py diff --git a/.gitignore b/.gitignore index eb21e9b..b077e89 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ coverage.xml /.worktrees/ /.superpowers/ node_modules/ +/integration/dsh/issue-bootstrap.json diff --git a/docs/superpowers/specs/2026-10-30-dsh-automatic-intervention.md b/docs/superpowers/specs/2026-10-30-dsh-automatic-intervention.md index f29eb27..eb110a0 100644 --- a/docs/superpowers/specs/2026-10-30-dsh-automatic-intervention.md +++ b/docs/superpowers/specs/2026-10-30-dsh-automatic-intervention.md @@ -10,6 +10,6 @@ AI 只提出重试或不处理。自动重试同时要求:原始错误码在 未解决、模型不可用或响应无效时保存现场和缺失项;模型可用时再做一次复盘,明确已证实事实、候选原因、验证方法和剩余未知。证据含前后诊断包、事件时序、模型可见会话、动作与核验结果、SHA256 清单。只采集本工具白名单诊断,不扫描无关进程、网络或用户文件。 -工单客户端固定使用配置的 Gitea/Forgejo 仓库,凭据通过本机 Git credential helper 获取,禁止交给模型。提交前查重;正文是脱敏摘要和证据清单,附件包含脱敏证据包。权限、网络、上传失败均记录到本地 outbox,保留工单草稿和恢复入口,不谎报提交成功。 +工单客户端默认使用随客户端预填的专用工单账户令牌,scope 仅 write:issue,服务端受限账户和团队仅授予当前仓库的工单权限。无需最终用户安装 Git 或提供个人凭据;令牌不能交给模型或网页。该受限令牌资源不进入 Git 源码,打包时携带,管理员侧发行密码只保存在本机 DPAPI。仅当用户显式改用其他目标时才使用对应目标的本机 Git credential helper,预填令牌不得转发给其他服务器或仓库。提交前查重;正文是脱敏摘要和证据清单,附件包含脱敏证据包。权限、网络、上传失败均记录到本地 outbox,保留工单草稿和恢复入口,不谎报提交成功。 验收覆盖成功修复、不可重试、未核验保护、AI 失败、重复事件、重试后仍失败、工单提交/附件失败;再以真实 API、原生会话与 Qt/CDP 点击验收受控异常,并实际提交一份标记为验收的工单。 diff --git a/docs/testing/2026-10-30-dsh-prefilled-issue-reporter.md b/docs/testing/2026-10-30-dsh-prefilled-issue-reporter.md new file mode 100644 index 0000000..91a6f0b --- /dev/null +++ b/docs/testing/2026-10-30-dsh-prefilled-issue-reporter.md @@ -0,0 +1,45 @@ +# DSH 专用工单令牌与免 Git 验收 + +用户要求创建仅用于工单的令牌并预填,解决其他电脑没有 Git 凭据的问题。 + +## 实际服务端配置 + +服务器:`https://git.simgreek.com`;目标:`Skyworth_Digital_Hardware/dxdesigner-part-replacement`。 + +- 专用账户:`dsh-issue-reporter`,非管理员、restricted、私有可见,不发送欢迎邮件。 +- 专用团队:`dsh-issue-reporters`,ID 10,仅关联当前仓库。 +- 团队单元:`repo.issues=write`;code、pulls、releases、wiki、projects、packages、actions 等均为 none。 +- 账户关闭建组织、Git hook、本地导入能力,建仓库上限为 0。 +- 创建令牌 ID 26,scope 严格为 `write:issue`。本记录不包含令牌值。 + +管理员身份只用于配置账户、团队和令牌。账户密码与发行恢复资料保存在本机 DPAPI 文件;客户端预填资源仅包含受限工单令牌,Git 忽略该资源,打包时加入 dsh-runtime。运行时不将令牌交给 Harness、模型或网页。 + +## 真实权限检查 + +`python tools/verify_dsh_issue_reporter.py`:清空 PATH 且使用不存在的用户配置文件,结果 credential_source 为 prefilled_issue_token。工单列表、评论读取与附件读取均返回 200;代码内容读取、代码写入、发布读取/创建、建仓库、管理员用户与组织团队接口均返回 403。写入否定探针使用空载荷,避免权限回归时创建实际代码或发布。 + +结果保存在 `build/dsh-issue-reporter-permissions.json`;服务端配置的非密摘要在 `build/dsh-issue-reporter-provisioning.json`。 + +## 真实桌面闭环 + +以全新任务中心数据启动,禁止调用 Git 可执行程序,使用预填专用令牌完成自动诊断、工单创建、脱敏附件上传、原生页面显示和退出检查。第一轮还发现并自动上报了 Windows 上下文替换读锁错误;已用真实 Windows 文件共享锁测试复现,沿用项目现有的有界 sharing-violation 重试修复,临时文件在退出路径清理。 + +第一轮证据在 `C:/Users/ABS_Windows/.codex/visualizations/2026/10/30/01a18a4c-fe37-7e12-b79d-be24a38dba29/dsh-prefilled-reporter/`。第二轮服务端建单/附件成功,但验收驱动过早连接空白 Chromium 页面而超时;已改为等待所属本机页面后连接 CDP。 + +最终证据在同级 `dsh-prefilled-reporter-verified/`。结果 `status: passed`、`cleanShutdown: true`、`uncaughtErrors: false`、`unexpectedUiErrors: false`;只有受控异常、credential_source 为 prefilled_issue_token。真实工单 [#8](https://git.simgreek.com/Skyworth_Digital_Hardware/dxdesigner-part-replacement/issues/8) 及附件均由专用账户提交,全程没有调用个人 Git 凭据。另通过专用令牌对 [#6 的定位记录评论](https://git.simgreek.com/Skyworth_Digital_Hardware/dxdesigner-part-replacement/issues/6#issuecomment-81) 完成实际评论写入验证,评论作者为 dsh-issue-reporter。 + +独立下载 #8 附件与本地 ZIP 逐字节一致,SHA256 为 `0c4f56e543bba406b8eab5008e6b07245578b5a30f4dd56706bcc86665e4fcf6`,ZIP 内 manifest 哈希逐项通过。扫描 Git 暂存区与 17 个封存/嵌套证据记录,无预填令牌、发行密码、管理员 Git 凭据或模型 API 密钥明文;实际预填资源确认被 Git 忽略且未跟踪。结果在同一证据目录的 `credential-and-artifact-verification.json`。 + +## 回归 + +```powershell +pytest -q tests/dsh tests/ui tests/packaging/test_dx_part_replace_build.py tests/trial_task_center/test_application.py tests/trial_task_center/test_quick_run.py tests/v1_1/test_task_queue.py tests/v1_1/test_diagnostics_bundle.py +``` + +结果:`119 passed in 58.64s`。新增覆盖无 Git/无用户配置的预填身份、冻结路径跨电脑加载、服务器/仓库绑定、拒绝过宽 scope、显式关闭和真实 Windows 读锁;原队列、诊断导出和自动介入回归通过。 + +最终对预填身份与上下文发布定向复验:`9 passed in 1.03s`。Python 编译与 Node 语法检查通过。 + +## 发布范围 + +已生成真实预填资源并接入打包配置;源码运行和模拟冻结资源路径均验证免 Git 加载。本轮不构建发布 EXE,不把打包配置检查称为 EXE 实机验收。共享令牌的实际权限由服务端 scope、受限账户和仓库团队共同约束,可按 ID 26 吊销;管理员凭据不分发。 diff --git a/docs/user/dsh-ai-supervision.md b/docs/user/dsh-ai-supervision.md index 66b7394..b26fa64 100644 --- a/docs/user/dsh-ai-supervision.md +++ b/docs/user/dsh-ai-supervision.md @@ -33,7 +33,11 @@ Harness 使用任务中心数据目录下独立的 `dsh/harness-home` 保存会 每次自动介入在数据目录 `dsh/incidents/<事件编号>/` 保存:前后任务快照、脱敏诊断包、事件时间线、规则门禁明细、模型可见会话、尝试动作、实际核验结果、工单草稿和带 SHA256 清单的 `evidence.zip`。保留可审阅的诊断说明,不要求模型输出隐藏思维过程;“取证”只收集本工具已有的日志与运行记录,不扫描无关网络流量、工程源文件或用户文件。 -本机工单目标已配置为 `https://git.simgreek.com/Skyworth_Digital_Hardware/dxdesigner-part-replacement`。配置文件为 `dsh/issue-config.json`,包含 `enabled`、`server` 和 `repository`;凭据从本机 Git credential helper 获取,不交给模型。该功能需要本机 Git 和可用的仓库凭据。 +工单默认提交到 `https://git.simgreek.com/Skyworth_Digital_Hardware/dxdesigner-part-replacement`。随客户端预填专用上报账户 `dsh-issue-reporter` 的 `write:issue` 令牌,其他电脑无需安装 Git、登录或提供个人凭据。该账户为非管理员受限账户,专用团队仅包含本仓库,只有工单写权限;代码、拉取请求、发布、组织、包和 Actions 权限均未授予。令牌不交给模型或网页。 + +预填资源为 `integration/dsh/issue-bootstrap.json`,已加入 Git 忽略;打包时放入 `dsh-runtime/issue-bootstrap.json`,可跨电脑使用,不依赖开发电脑的 DPAPI。管理员侧的账户密码和令牌恢复资料单独保存在本机 DPAPI 文件,不能随包分发。没有预填资源时,DSH 发布打包会明确报错,避免生成缺少上报身份的安装包。 + +`dsh/issue-config.json` 可覆盖启用状态与目标。预填令牌仅用于其绑定的服务器和仓库;自定义其他目标不会携带该令牌,而沿用该目标的本机 Git 凭据。受限共享令牌可在 Git 服务端吊销;吊销后保留证据待补交,并需更新分发资源。 提交前按事件标记查重,成功后上传脱敏证据附件并在任务监管显示工单链接;提交、权限或上传失败时保留为“工单待完整提交”。可以点击 **打开证据目录** 或 **重新提交工单**;重试工单不会重新执行 CAD。重启后恢复待提交记录,已中断的处理也不自动重放。附件是提交前封存的证据快照,提交回执另存在 `ticket.json` 和后续时间线中。 @@ -58,3 +62,5 @@ python tools/accept_dsh_desktop.py --provider-config "$env:LOCALAPPDATA/DxProduc ``` 2026-10-30 已完成默认模型的真实对话、`dx_task_snapshot` 调用和完整流式回复验证,记录见 `docs/testing/2026-10-30-dsh-native-integration.md`。此验收没有执行 CAD 修改,也不代表已构建发布 EXE。 + +专用工单身份的权限检查:`python tools/verify_dsh_issue_reporter.py`。该命令清空进程 PATH,验证无 Git 环境仍使用预填令牌,并要求代码、发布、建仓库与管理接口被拒绝。维护者首次配置使用 `tools/provision_dsh_issue_reporter.py`;最终用户不运行该脚本。免 Git 的真实桌面验收可运行 `tools/accept_dsh_desktop.py --incident --without-git --provider-config '<模型配置目录>'`。 diff --git a/integration/dsh/task-plugin/client.js b/integration/dsh/task-plugin/client.js index 90c8ef9..5d48f0d 100644 --- a/integration/dsh/task-plugin/client.js +++ b/integration/dsh/task-plugin/client.js @@ -14,6 +14,8 @@ window.__ModuleLoader__.load({id:'@dx/dsh-task',factory:require=>{ h('ul',null,...['异常后自动创建诊断会话,收集证据','已授权执行遇到明确失败时,按规则最多重试一次并核验','无法解决时复盘,保存诊断和完整证据链,尝试提交 Git 工单','手动刷新任务上下文、准备重试或安全暂停'].map(text=>h('li',{key:text},text))), h('h3',null,'模型与数据'), h('p',null,'使用主工具配置的模型,在输入框下方选择。会话保存在本机任务中心数据目录。'), + h('h3',null,'异常工单'), + h('p',null,'随工具预置专用工单上报身份,无需安装 Git 或登录。未解决的异常自动提交项目工单,并上传脱敏诊断证据。'), h('p',null,'工程修改与执行核验由任务中心完成。模型不直接访问工程文件或执行命令。')); } function SettingsLauncher({wide,openSettings}){ diff --git a/packaging/DxPartReplace.spec b/packaging/DxPartReplace.spec index 8157376..13c9696 100644 --- a/packaging/DxPartReplace.spec +++ b/packaging/DxPartReplace.spec @@ -33,7 +33,11 @@ legacy_runtime_datas = [ # The native assistant loads the official web UI; include its pinned Node runtime. dsh_root = project_root / 'integration' / 'dsh' +if (dsh_root / 'issue-bootstrap.json').is_file(): + legacy_runtime_datas.append((str(dsh_root / 'issue-bootstrap.json'), 'dsh-runtime')) if (dsh_root / 'node_modules' / '@deepseek-ai' / 'dsh' / 'package.json').is_file(): + if not (dsh_root / 'issue-bootstrap.json').is_file(): + raise RuntimeError('Provision the DSH issue-only reporter before packaging the native assistant.') legacy_runtime_datas.extend([ (str(dsh_root / 'start.mjs'), 'dsh-runtime'), (str(dsh_root / 'package.json'), 'dsh-runtime'), diff --git a/src/dx_productivity/dsh/git_issues.py b/src/dx_productivity/dsh/git_issues.py index da6a9c8..9e0c1a1 100644 --- a/src/dx_productivity/dsh/git_issues.py +++ b/src/dx_productivity/dsh/git_issues.py @@ -12,6 +12,7 @@ import urllib.request from uuid import uuid4 from .evidence import scrub +from .issue_credentials import issue_target, load_bootstrap class NoRedirect(urllib.request.HTTPRedirectHandler): @@ -23,30 +24,43 @@ class GitIssuePublisher: def __init__(self, config_path: Path): self.config_path = Path(config_path) - def __call__(self, record, archive: Path): - if not self.config_path.is_file(): - return {'status': 'unavailable', 'reason': 'issue_target_not_configured'} - config = json.loads(self.config_path.read_text(encoding='utf8')) + def connection(self): + config = issue_target(self.config_path) if not config.get('enabled'): - return {'status': 'unavailable', 'reason': 'issue_submission_disabled'} + return config, {}, 'issue_submission_disabled' parsed = urllib.parse.urlsplit(config['server']) if parsed.scheme != 'https' or not parsed.hostname or parsed.username or parsed.password or parsed.path not in {'', '/'}: raise ValueError('invalid_issue_server') 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') + bootstrap = load_bootstrap() + if (bootstrap and bootstrap['server'].rstrip('/') == config['server'].rstrip('/') + and bootstrap['repository'] == config['repository']): + return config, {'Authorization': 'token ' + bootstrap['token']}, 'prefilled_issue_token' env = os.environ | {'GIT_TERMINAL_PROMPT': '0', 'GCM_INTERACTIVE': 'never'} - credential = subprocess.run(['git', 'credential', 'fill'], input=f'protocol=https\nhost={parsed.netloc}\n\n', - text=True, capture_output=True, env=env, timeout=20, check=False, - creationflags=subprocess.CREATE_NO_WINDOW if os.name == 'nt' else 0) + try: + credential = subprocess.run(['git', 'credential', 'fill'], input=f'protocol=https\nhost={parsed.netloc}\n\n', + text=True, capture_output=True, env=env, timeout=20, check=False, + creationflags=subprocess.CREATE_NO_WINDOW if os.name == 'nt' else 0) + except (FileNotFoundError, subprocess.TimeoutExpired): + return config, {}, 'git_not_installed' fields = dict(line.split('=', 1) for line in credential.stdout.splitlines() if '=' in line) if credential.returncode or not fields.get('password'): - return {'status': 'unavailable', 'reason': 'git_credential_unavailable'} + return config, {}, 'git_credential_unavailable' authorization = 'Basic ' + base64.b64encode((fields.get('username', '') + ':' + fields['password']).encode()).decode() + return config, {'Authorization': authorization}, 'machine_git_credential' + + def __call__(self, record, archive: Path): + config, authentication, source = self.connection() + if not authentication: + return {'status': 'unavailable', 'reason': source} + parsed = urllib.parse.urlsplit(config['server']) + owner, repo = config['repository'].split('/') opener = urllib.request.build_opener(NoRedirect()) api = f'{parsed.scheme}://{parsed.netloc}/api/v1/repos/{owner}/{repo}' def request(path, payload=None, content_type='application/json'): - headers = {'Authorization': authorization, 'Accept': 'application/json'} + headers = authentication | {'Accept': 'application/json'} if payload is not None: headers['Content-Type'] = content_type call = urllib.request.Request(api + path, data=payload, headers=headers) @@ -60,7 +74,7 @@ class GitIssuePublisher: body = (Path(record['evidence_dir']) / 'issue.md').read_text(encoding='utf8') issue = request('/issues', json.dumps({'title': scrub(title), 'body': scrub(body)}, ensure_ascii=False).encode()) result = {'status': 'submitted', 'url': issue['html_url'], 'number': issue['number'], - 'attachment_status': 'pending'} + 'attachment_status': 'pending', 'credential_source': source} # Existing issue and attachment are reused when an interrupted upload is retried. try: attachments = request(f"/issues/{issue['number']}/assets") diff --git a/src/dx_productivity/dsh/issue_credentials.py b/src/dx_productivity/dsh/issue_credentials.py new file mode 100644 index 0000000..ca781af --- /dev/null +++ b/src/dx_productivity/dsh/issue_credentials.py @@ -0,0 +1,37 @@ +"""Portable issue-only reporter credential, bound to its exact issue target.""" +from __future__ import annotations + +import json +from pathlib import Path +import sys + +DEFAULT_TARGET = {'enabled': True, 'server': 'https://git.simgreek.com', + 'repository': 'Skyworth_Digital_Hardware/dxdesigner-part-replacement'} + + +def bootstrap_path() -> Path: + if getattr(sys, 'frozen', False): + return Path(sys._MEIPASS) / 'dsh-runtime' / 'issue-bootstrap.json' + return Path(__file__).resolve().parents[3] / 'integration' / 'dsh' / 'issue-bootstrap.json' + + +def load_bootstrap() -> dict: + try: + row = json.loads(bootstrap_path().read_text(encoding='utf8')) + if not isinstance(row, dict): + return {} + if row.get('scopes') != ['write:issue'] or not row.get('token') or not row.get('username'): + return {} + if not row.get('server', '').startswith('https://') or len(row.get('repository', '').split('/')) != 2: + return {} + return row + except (OSError, ValueError, TypeError): + return {} + + +def issue_target(config_path: Path) -> dict: + bootstrap = load_bootstrap() + target = DEFAULT_TARGET | {key: bootstrap[key] for key in ('server', 'repository') if key in bootstrap} + if config_path.is_file(): + target.update(json.loads(config_path.read_text(encoding='utf8'))) + return target diff --git a/src/dx_productivity/dsh/native_runtime.py b/src/dx_productivity/dsh/native_runtime.py index 19abcff..c063b84 100644 --- a/src/dx_productivity/dsh/native_runtime.py +++ b/src/dx_productivity/dsh/native_runtime.py @@ -9,6 +9,7 @@ import sys from uuid import uuid4 from .snapshot import TaskSnapshot +from ..trial_task_center.path_access import retry_sharing_violation HARNESS_VERSION = '0.2.1-rc.5' @@ -48,5 +49,9 @@ def write_context(data_root: Path, snapshot: TaskSnapshot, *, events=(), action_ 'task_name': str(task_name)[:128], 'automation': automation or {}} temporary = data_root / ('context-' + uuid4().hex + '.tmp') temporary.write_text(json.dumps(value, ensure_ascii=False), encoding='utf8') - os.replace(temporary, data_root / 'task-context.json') + destination = data_root / 'task-context.json' + try: + retry_sharing_violation(lambda: os.replace(temporary, destination), target=destination) + finally: + temporary.unlink(missing_ok=True) return value diff --git a/tests/dsh/test_issue_credentials.py b/tests/dsh/test_issue_credentials.py new file mode 100644 index 0000000..31794a9 --- /dev/null +++ b/tests/dsh/test_issue_credentials.py @@ -0,0 +1,72 @@ +import json +from pathlib import Path +import subprocess +from unittest.mock import Mock + +from dx_productivity.dsh import issue_credentials +from dx_productivity.dsh.git_issues import GitIssuePublisher + + +def bootstrap(tmp_path, monkeypatch): + path = tmp_path / 'issue-bootstrap.json' + path.write_text(json.dumps({'server': 'https://git.example', 'repository': 'org/tool', + 'username': 'dsh-issue-reporter', 'token': 'test-issue-token', 'scopes': ['write:issue']}), encoding='utf8') + monkeypatch.setattr(issue_credentials, 'bootstrap_path', lambda: path) + return path + + +def test_other_machine_uses_prefilled_token_without_git_or_local_config(tmp_path, monkeypatch): + bootstrap(tmp_path, monkeypatch) + monkeypatch.setattr(subprocess, 'run', Mock(side_effect=AssertionError('Git must not be called'))) + publisher = GitIssuePublisher(tmp_path / 'fresh-user' / 'issue-config.json') + config, headers, source = publisher.connection() + assert config['repository'] == 'org/tool' + assert headers == {'Authorization': 'token test-issue-token'} + assert source == 'prefilled_issue_token' + + +def test_prefilled_token_cannot_be_sent_to_another_server_or_repository(tmp_path, monkeypatch): + bootstrap(tmp_path, monkeypatch) + path = tmp_path / 'issue-config.json' + path.write_text(json.dumps({'server': 'https://other.example', 'repository': 'other/project', 'enabled': True})) + monkeypatch.setattr(subprocess, 'run', Mock(side_effect=FileNotFoundError('git absent'))) + config, headers, source = GitIssuePublisher(path).connection() + assert not headers + assert source == 'git_not_installed' + + +def test_missing_git_becomes_actionable_status_without_losing_incident(tmp_path, monkeypatch): + monkeypatch.setattr(issue_credentials, 'bootstrap_path', lambda: tmp_path / 'absent.json') + monkeypatch.setattr(subprocess, 'run', Mock(side_effect=FileNotFoundError('git absent'))) + result = GitIssuePublisher(tmp_path / 'absent-config.json')({}, tmp_path / 'evidence.zip') + assert result['reason'] == 'git_not_installed' + + +def test_explicit_disable_remains_respected_with_prefilled_token(tmp_path, monkeypatch): + bootstrap(tmp_path, monkeypatch) + path = tmp_path / 'issue-config.json' + path.write_text('{"enabled":false}') + result = GitIssuePublisher(path)({}, tmp_path / 'evidence.zip') + assert result['reason'] == 'issue_submission_disabled' + + +def test_bootstrap_refuses_excessive_token_scopes(tmp_path, monkeypatch): + path = bootstrap(tmp_path, monkeypatch) + row = json.loads(path.read_text()); row['scopes'] = ['all'] + path.write_text(json.dumps(row)) + assert issue_credentials.load_bootstrap() == {} + + +def test_packaged_bootstrap_works_on_another_computer_without_dpapi_or_git(tmp_path, monkeypatch): + import sys + bundle = tmp_path / 'dsh-runtime' + bundle.mkdir() + (bundle / 'issue-bootstrap.json').write_text(json.dumps({'server': 'https://git.example', + 'repository': 'org/tool', 'username': 'dsh-issue-reporter', 'token': 'test-portable-issue-token', + 'scopes': ['write:issue']})) + monkeypatch.setattr(sys, 'frozen', True, raising=False) + monkeypatch.setattr(sys, '_MEIPASS', str(tmp_path), raising=False) + monkeypatch.setattr(subprocess, 'run', Mock(side_effect=AssertionError('Git must not be used'))) + config, headers, source = GitIssuePublisher(tmp_path / 'new-user-data/issue-config.json').connection() + assert source == 'prefilled_issue_token' + assert headers['Authorization'] == 'token test-portable-issue-token' diff --git a/tests/dsh/test_native_runtime.py b/tests/dsh/test_native_runtime.py index 7234bd0..4eb63ad 100644 --- a/tests/dsh/test_native_runtime.py +++ b/tests/dsh/test_native_runtime.py @@ -17,3 +17,25 @@ def test_launch_does_not_inherit_unrelated_credentials_or_user_harness_home(tmp_ assert 'OPENAI_API_KEY' not in env assert 'DSH_HOME' not in env assert env['DX_DSH_HOME'].startswith(str(tmp_path)) + + +def test_context_publication_survives_a_real_windows_reader_lock(tmp_path): + import ctypes + from ctypes import wintypes + from threading import Timer + write_context(tmp_path, TaskSnapshot('t', 'draft')) + kernel = ctypes.WinDLL('kernel32', use_last_error=True) + kernel.CreateFileW.argtypes = (wintypes.LPCWSTR, wintypes.DWORD, wintypes.DWORD, ctypes.c_void_p, + wintypes.DWORD, wintypes.DWORD, wintypes.HANDLE) + kernel.CreateFileW.restype = wintypes.HANDLE + kernel.CloseHandle.argtypes = (wintypes.HANDLE,) + handle = kernel.CreateFileW(str(tmp_path / 'task-context.json'), 0x80000000, 3, None, 3, 0, None) + assert handle != ctypes.c_void_p(-1).value + release = Timer(.03, lambda: kernel.CloseHandle(handle)) + release.start() + try: + value = write_context(tmp_path, TaskSnapshot('t', 'failed', issue_code='controlled')) + assert json.loads((tmp_path / 'task-context.json').read_text())['revision'] == value['revision'] + assert not list(tmp_path.glob('context-*.tmp')) + finally: + release.join() diff --git a/tools/accept_dsh_desktop.py b/tools/accept_dsh_desktop.py index 956c60c..b1ff464 100644 --- a/tools/accept_dsh_desktop.py +++ b/tools/accept_dsh_desktop.py @@ -22,6 +22,7 @@ def main(): parser.add_argument('--live-model', action='store_true') parser.add_argument('--incident', action='store_true', help='Inject a read-only controlled failure and verify automatic diagnosis and Git escalation') parser.add_argument('--outbox-only', action='store_true', help='Verify failed publication stays in local outbox without creating another real issue') + parser.add_argument('--without-git', action='store_true', help='Simulate a client with no Git credentials or executable') args = parser.parse_args() output = args.output.resolve() output.mkdir(parents=True, exist_ok=True) @@ -32,8 +33,14 @@ def main(): shutil.copy2(args.provider_config / name, data / 'dsh' / name) if args.incident: (data / 'dsh').mkdir(parents=True, exist_ok=True) - (data / 'dsh/issue-config.json').write_text(json.dumps({'enabled': not args.outbox_only, 'acceptance': True, - 'server': 'https://git.simgreek.com', 'repository': 'Skyworth_Digital_Hardware/dxdesigner-part-replacement'}), encoding='utf8') + (data / 'dsh/issue-config.json').write_text(json.dumps({'enabled': not args.outbox_only, 'acceptance': True}), encoding='utf8') + if args.without_git: + original_run = subprocess.run + def no_git_run(command, *positional, **keywords): + if isinstance(command, (list, tuple)) and Path(str(command[0])).stem.casefold() == 'git': + raise FileNotFoundError('Git is not installed in this acceptance client') + return original_run(command, *positional, **keywords) + subprocess.run = no_git_run os.environ['QTWEBENGINE_REMOTE_DEBUGGING'] = f'127.0.0.1:{args.port}' os.environ.pop('QT_QPA_PLATFORM', None) from PySide6.QtCore import QTimer, Qt @@ -122,9 +129,13 @@ def main(): report['checks'].append('native composer draft survived direct navigation') if args.incident: row = window._dsh_automation.latest + assert row['issue_code'] == 'dsh_acceptance_failure', 'Unexpected UI error replaced controlled incident' assert row['status'] == ('pending_issue' if args.outbox_only else 'escalated') and row['attempts'] == 0 if not args.outbox_only: assert row['ticket']['attachment_status'] == 'uploaded' + if args.without_git: + assert row['ticket']['credential_source'] == 'prefilled_issue_token' + report['checks'].append('client without Git used only the prefilled issue reporter token') report['incident'] = row report['checks'].append('real native automatic diagnosis and reflection, evidence and ' + ('retryable local issue outbox' if args.outbox_only else 'Git issue attachment') + ' completed without CAD mutations') @@ -151,6 +162,7 @@ def main(): log = (data / 'logs/application.log').read_text(encoding='utf8') report['cleanShutdown'] = 'application_clean_shutdown exit_code=0' in log report['uncaughtErrors'] = 'uncaught_main_thread' in log + report['unexpectedUiErrors'] = 'operation=ui message=' in log if args.live_model or args.incident: audit_file = data / 'dsh/harness-home/task-tool-audit.jsonl' audit = [json.loads(line) for line in audit_file.read_text(encoding='utf8').splitlines()] if audit_file.is_file() else [] @@ -158,7 +170,7 @@ def main(): report['modelToolSucceeded'] = any(row['name'] == 'dx_task_snapshot' and row['is_error'] is False for row in audit) (output / 'desktop-report.json').write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding='utf8') print(json.dumps(report, ensure_ascii=True)) - return 0 if report['cleanShutdown'] and not report['uncaughtErrors'] and (not (args.live_model or args.incident) or report['modelToolSucceeded']) else 1 + return 0 if report['cleanShutdown'] and not report['uncaughtErrors'] and not report['unexpectedUiErrors'] and (not (args.live_model or args.incident) or report['modelToolSucceeded']) else 1 if __name__ == '__main__': diff --git a/tools/dsh_cdp.mjs b/tools/dsh_cdp.mjs index d6f4cb6..7e6134f 100644 --- a/tools/dsh_cdp.mjs +++ b/tools/dsh_cdp.mjs @@ -2,9 +2,15 @@ import {writeFile} from 'node:fs/promises'; export async function connect(port=9226) { - const targets=await fetch(`http://127.0.0.1:${port}/json`).then(r=>r.json()); - const target=targets.find(x=>x.type==='page'); - if(!target) throw Error('The embedded DSH page has not opened'); + let target; + const deadline=Date.now()+45000; + while(Date.now()r.json()); + target=targets.find(x=>x.type==='page'&&x.url?.startsWith('http://127.0.0.1:')); + if(target)break; + await new Promise(resolve=>setTimeout(resolve,100)); + } + if(!target) throw Error('The embedded DSH page did not navigate to the owned local runtime'); const socket=new WebSocket(target.webSocketDebuggerUrl); await new Promise((resolve,reject)=>{socket.onopen=resolve;socket.onerror=reject;}); let sequence=0; diff --git a/tools/provision_dsh_issue_reporter.py b/tools/provision_dsh_issue_reporter.py new file mode 100644 index 0000000..9764ac5 --- /dev/null +++ b/tools/provision_dsh_issue_reporter.py @@ -0,0 +1,131 @@ +"""Provision the issue-only reporter. Never print or commit its credentials.""" +from __future__ import annotations + +import base64 +from datetime import datetime, timezone +import json +import os +from pathlib import Path +import secrets +import subprocess +import sys +import urllib.error +import urllib.request + +REPO = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(REPO / 'src')) +SERVER = 'https://git.simgreek.com' +ORG = 'Skyworth_Digital_Hardware' +PROJECT = 'dxdesigner-part-replacement' +BOT = 'dsh-issue-reporter' +TEAM = 'dsh-issue-reporters' + + +def main(): + import win32crypt + from dx_productivity.trial_task_center.application import resolve_data_root + root = resolve_data_root(None) / 'dsh' + root.mkdir(parents=True, exist_ok=True) + state_path = root / 'issue-reporter-issuer.dpapi' + if state_path.is_file(): + _, plain = win32crypt.CryptUnprotectData(state_path.read_bytes(), None, None, None, 0) + state = json.loads(plain) + else: + state = {'username': BOT, 'password': secrets.token_urlsafe(48)} + def save_state(): + blob = win32crypt.CryptProtectData(json.dumps(state).encode(), 'DSH issue reporter issuer', None, None, None, 0) + temporary = state_path.with_suffix('.tmp') + temporary.write_bytes(blob) + os.replace(temporary, state_path) + # Save issuer recovery before any remote creation; never overwrite another user's account. + new_state = not state_path.is_file() + r = subprocess.run(['git', 'credential', 'fill'], input='protocol=https\nhost=git.simgreek.com\n\n', + text=True, capture_output=True, timeout=20, env=os.environ | {'GIT_TERMINAL_PROMPT': '0', 'GCM_INTERACTIVE': 'never'}, + creationflags=subprocess.CREATE_NO_WINDOW if os.name == 'nt' else 0) + credential = dict(line.split('=', 1) for line in r.stdout.splitlines() if '=' in line) + assert r.returncode == 0 and credential.get('password'), 'Administrator credential unavailable' + admin_auth = 'Basic ' + base64.b64encode((credential.get('username', '') + ':' + credential['password']).encode()).decode() + def api(route, method='GET', data=None, auth=admin_auth): + request = urllib.request.Request(SERVER + '/api/v1' + route, + data=None if data is None else json.dumps(data).encode(), method=method, + headers={'Authorization': auth, 'Content-Type': 'application/json'}) + try: + with urllib.request.urlopen(request, timeout=25) as response: + value = response.read() + return json.loads(value) if value else None + except urllib.error.HTTPError as error: + try: + message = json.loads(error.read()).get('message', '') + except (ValueError, AttributeError): + message = '' + for secret in (state.get('password'), state.get('token'), credential.get('password')): + if secret: + message = message.replace(secret, '') + error.dsh_detail = {'route': route, 'method': method, 'message': message[:350]} + raise + assert api('/user')['is_admin'], 'Site administrator required for isolated reporter account' + exists = False + try: + api('/users/' + BOT); exists = True + except urllib.error.HTTPError as error: + if error.code != 404: + raise + if new_state and exists: + raise RuntimeError('Reporter username exists without local issuer state; refusing to change that account') + save_state() + if not exists: + api('/admin/users', 'POST', {'username': BOT, 'email': BOT + '@users.noreply.git.simgreek.com', + 'full_name': 'DSH 工单上报', 'password': state['password'], 'must_change_password': False, + 'restricted': True, 'visibility': 'private', 'send_notify': False}) + api('/admin/users/' + BOT, 'PATCH', {'login_name': BOT, 'restricted': True, 'admin': False, + 'allow_create_organization': False, 'allow_git_hook': False, 'allow_import_local': False, + 'max_repo_creation': 0, 'visibility': 'private'}) + teams = api('/orgs/' + ORG + '/teams') + team = next((row for row in teams if row['name'] == TEAM), None) + units = {'repo.issues': 'write', **{name: 'none' for name in + ['repo.code', 'repo.pulls', 'repo.releases', 'repo.wiki', 'repo.ext_wiki', 'repo.ext_issues', + 'repo.projects', 'repo.packages', 'repo.actions']}} + options = {'name': TEAM, 'description': 'DSH reporter: only this project issues, comments and attachments', + 'permission': 'write', 'units_map': units, 'includes_all_repositories': False, + 'can_create_org_repo': False, 'visibility': 'private'} + if team is None: + team = api('/orgs/' + ORG + '/teams', 'POST', options) + else: + # Refuse to modify a non-dedicated team or broaden an existing membership. + members = api(f"/teams/{team['id']}/members") + assert all(row['login'] == BOT for row in members), 'Reporter team has other members' + team = api(f"/teams/{team['id']}", 'PATCH', options) + repos = api(f"/teams/{team['id']}/repos") + assert all(row['full_name'] == ORG + '/' + PROJECT for row in repos), 'Reporter team includes other projects' + api(f"/teams/{team['id']}/repos/{ORG}/{PROJECT}", 'PUT') + api(f"/teams/{team['id']}/members/{BOT}", 'PUT') + state['team_id'] = team['id'] + save_state() + bot_auth = 'Basic ' + base64.b64encode((BOT + ':' + state['password']).encode()).decode() + if not state.get('token'): + token = api('/users/' + BOT + '/tokens', 'POST', + {'name': 'dsh-tool-issue-only-' + datetime.now(timezone.utc).strftime('%Y%m%d'), 'scopes': ['write:issue']}, bot_auth) + assert token['scopes'] == ['write:issue'], 'Unexpected token permissions' + state.update(token=token['sha1'], token_id=token['id'], scopes=token['scopes']) + save_state() + team = api(f"/teams/{team['id']}") + assert team['units_map'].get('repo.issues') == 'write' + assert all(level == 'none' for unit, level in team['units_map'].items() if unit != 'repo.issues') + bootstrap = {'server': SERVER, 'repository': ORG + '/' + PROJECT, 'username': BOT, + 'token': state['token'], 'token_id': state['token_id'], 'scopes': ['write:issue']} + output = REPO / 'integration/dsh/issue-bootstrap.json' + output.write_text(json.dumps(bootstrap, indent=2), encoding='utf8') + report = {'status': 'provisioned', 'username': BOT, 'team_id': team['id'], 'token_id': state['token_id'], + 'scopes': ['write:issue'], 'repository': bootstrap['repository'], 'units_map': team['units_map'], + 'restricted_account': True, 'prefilled_bootstrap': True, 'issuer_only_local_dpapi': True} + (REPO / 'build/dsh-issue-reporter-provisioning.json').write_text(json.dumps(report, indent=2), encoding='utf8') + print(json.dumps(report)) + + +if __name__ == '__main__': + try: + main() + except Exception as error: + print(json.dumps({'status': 'failed', 'error': type(error).__name__, 'http_status': getattr(error, 'code', None), + 'detail': getattr(error, 'dsh_detail', {})})) + raise SystemExit(1) diff --git a/tools/verify_dsh_issue_reporter.py b/tools/verify_dsh_issue_reporter.py new file mode 100644 index 0000000..f03c532 --- /dev/null +++ b/tools/verify_dsh_issue_reporter.py @@ -0,0 +1,58 @@ +"""Verify actual issue-only token boundaries without printing the token.""" +from __future__ import annotations + +import json +import os +from pathlib import Path +import sys +import urllib.error +import urllib.request + +REPO = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(REPO / 'src')) + + +def main(): + from dx_productivity.dsh.git_issues import GitIssuePublisher + from dx_productivity.dsh.issue_credentials import load_bootstrap + bootstrap = load_bootstrap() + assert bootstrap and bootstrap['scopes'] == ['write:issue'] + # Empty PATH ensures no Git executable or Git credential helper can be used. + os.environ['PATH'] = '' + config, headers, source = GitIssuePublisher(REPO / 'build/absent-user-issue-config.json').connection() + assert source == 'prefilled_issue_token' + base = config['server'].rstrip('/') + '/api/v1' + target = '/repos/' + config['repository'] + def status(route, method='GET', body=None): + request = urllib.request.Request(base + route, method=method, data=body, + headers=headers | {'Content-Type': 'application/json'}) + try: + with urllib.request.urlopen(request, timeout=20) as response: + return response.status + except urllib.error.HTTPError as error: + return error.code + positive = {name: status(route) for name, route in { + 'list_issues': target + '/issues?limit=1', 'issue_comments': target + '/issues/4/comments', + 'issue_attachments': target + '/issues/4/assets'}.items()} + assert all(value == 200 for value in positive.values()), positive + negative = { + 'read_repository_contents': status(target + '/contents'), + 'read_releases': status(target + '/releases'), + 'read_admin_users': status('/admin/users'), + 'read_organization_teams': status('/orgs/' + config['repository'].split('/')[0] + '/teams'), + # Empty payloads cannot create a repository/release/file even if a + # permission regression admits the route. Require an explicit denial. + 'create_repository': status('/user/repos', 'POST', b'{}'), + 'create_release': status(target + '/releases', 'POST', b'{}'), + 'write_code': status(target + '/contents/__dsh_scope_probe__', 'POST', b'{}'), + } + assert all(value in {401, 403, 404} for value in negative.values()), negative + report = {'status': 'passed', 'username': bootstrap['username'], 'token_id': bootstrap.get('token_id'), + 'scopes': bootstrap['scopes'], 'credential_source': source, 'git_executable_available': False, + 'no_local_user_configuration': True, 'allowed': positive, 'denied': negative} + (REPO / 'build/dsh-issue-reporter-permissions.json').write_text(json.dumps(report, indent=2), encoding='utf8') + print(json.dumps(report)) + + +if __name__ == '__main__': + main()