diff --git a/AGENTS.md b/AGENTS.md index 47d70e3a..c07760f3 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -43,7 +43,7 @@ UniDesk 是一个以主 server 为统一入口的分布式工作平台;本文 - `bun scripts/cli.ts deploy check/plan/apply [--file deploy.json|--env dev|prod] [--service ]`:按根目录 `deploy.json` 或 `origin/master:deploy.json#environments.` 的服务 repo 和 commit 期望状态校验或更新用户服务;`--env dev` 开放 D601 `backend-core` rollout、reviewed registry artifact consumers 和 D601 direct consumer validation,`findjob`/`pipeline` 是 D601 direct pull-only 样板,`met-nonlinear` dry-run blocked,`k3sctl-adapter` supervisor-only,`code-queue` prod unsupported,规则见 `docs/reference/deploy.md` 与 `docs/reference/dev-environment.md`。 - `bun scripts/cli.ts dev-env validate [--manifest path] [--kubectl-dry-run]` / `dev-env prewarm-images`:离线校验 D601 `unidesk-dev` 生产隔离护栏和 dev workload manifests,或把开发底座基础镜像预热到 D601 原生 k3s containerd,规则见 `docs/reference/deploy.md` 与 `docs/reference/microservices.md`。 - `bun scripts/cli.ts artifact-registry plan|render|status|health|install|deploy-backend-core|deploy-service`:管理 D601 host-managed CNCF Distribution registry,并通过短生命周期 relay 或 D601 pull/import 做 commit-pinned pull-only artifact CD;`deploy-backend-core` 是 deprecated 兼容名,`findjob`/`pipeline` 支持 D601 direct dev/prod,`met-nonlinear` 和 `k3sctl-adapter` 只给受限计划路径,`code-queue` 只支持 dev,规则见 `docs/reference/artifact-registry.md`。 -- `bun scripts/cli.ts gh auth status|issue ...|pr list|view|create|comment` / `bun scripts/code-queue-pr-preflight-example.ts`:通过 REST 执行安全 GitHub issue 读写、脱敏 auth/status 诊断、body-file Markdown 写入、#24 指挥简报新增时间线 ClaudeQQ 通知、escape 扫描、只读 cleanup-plan 和 #20 board-audit、PR 创建/评论 dry-run 与 runner PR preflight;`gh pr merge` 当前仍结构化拒绝,规则见 `docs/reference/cli.md` 和 `docs/reference/code-queue-supervision.md`。 +- `bun scripts/cli.ts gh auth status|issue ...|pr list|view|create|comment` / `bun scripts/code-queue-pr-preflight-example.ts`:通过 REST 执行安全 GitHub issue 读写、脱敏 auth/status 诊断、body-file Markdown 写入、#24 指挥简报新增时间线 ClaudeQQ 通知、escape 扫描、只读 cleanup-plan、#20 board-audit 和 #20 board-row list/get/update dry-run/并发保护、PR 创建/评论 dry-run 与 runner PR preflight;`gh pr merge` 当前仍结构化拒绝,规则见 `docs/reference/cli.md` 和 `docs/reference/code-queue-supervision.md`。 - `bun scripts/cli.ts ci install/status/run/publish-backend-core/publish-user-service/run-dev-e2e/logs`:在 D601 原生 k3s 上安装和运行 Tekton CI,支持每 commit 检查、Code Queue 只读性能门禁、`CI.json` catalog 驱动的 backend-core 与 user-service commit-pinned 镜像发布和手动触发的 `origin/master:deploy.json#environments.dev` 临时 namespace e2e;catalog/producer/consumer 分工见 `docs/reference/cicd-standardization.md`,`run-dev-e2e` 的 Git 控制 runner、短 launcher 和 no-CD 边界见 `docs/reference/dev-ci-runner.md`,Tekton 规则见 `docs/reference/ci.md`。 - `bun scripts/cli.ts codex deploy `:旧 Code Queue 兼容部署入口已禁用,原因是它会绕过受控部署边界直连 D601 部署 Code Queue;规则见 `docs/reference/codex-deploy.md`。 - `bun scripts/cli.ts codex submit [prompt] [--prompt-file path|--prompt-stdin] [--queue ]` / `codex pr-preflight [--remote]`:前者通过 backend-core 私有代理提交 Code Queue 任务,`--dry-run` 会给出 MiniMax/GPT/人工路由建议但不改写 payload;后者只读检查 D601 scheduler/runner 的 GitHub token、egress 和 PR 能力,PR 型派单前必须使用,规则见 `docs/reference/cli.md` 和 `docs/reference/code-queue-supervision.md`。 diff --git a/TEST.md b/TEST.md index baf3a9d8..6b4c7ef2 100644 --- a/TEST.md +++ b/TEST.md @@ -141,4 +141,4 @@ ## T27 GitHub Issue/Comment 换行转义卫生扫描 -阅读 `AGENTS.md` 和 `docs/reference/cli.md`,然后用 cli 手动测试以下内容:运行 `bun scripts/cli.ts gh help`,确认 help 中包含 `gh issue create --title --body-file <file> [--label label[,label...]]...`、`gh issue scan-escape` 和 `gh issue cleanup-plan`,notes 中明确推荐 `--body-file`、quoted heredoc 和只读 cleanup-plan。运行 `bun scripts/gh-cli-issue-guard-contract-test.ts`,确认 mock GitHub 覆盖污染命中、说明性 `\n` 命中不误报、短 body/null body guard、body-file dry-run 写入路径、`issue create --label cli,infra --label ops --dry-run` labels 解析和 request plan、真实 create REST payload labels、missing label 的结构化 `validation-failed`、comment-id/body-id 定位和 cleanupSuggestions。对真实仓库只允许运行 `bun scripts/cli.ts gh issue scan-escape --repo pikasTech/unidesk --limit <N> --dry-run` 或 `bun scripts/cli.ts gh issue cleanup-plan --repo pikasTech/unidesk --limit <N>`,确认输出 JSON 中包含 `classification=suspected-pollution|explanatory-mention|risk`、`bodyKind`、`bodyId`、`issueNumber`、`issueId`、`commentId`、`cleanupSuggestions` 和 diff-like preview;不得运行真实历史评论清理、不得改写 #20/#24 正文,除非另有明确人工指令并先审阅 `--body-file` dry-run。 +阅读 `AGENTS.md` 和 `docs/reference/cli.md`,然后用 cli 手动测试以下内容:运行 `bun scripts/cli.ts gh help`,确认 help 中包含 `gh issue create --title <title> --body-file <file> [--label label[,label...]]...`、`gh issue scan-escape`、`gh issue cleanup-plan`、`gh issue board-row list` 和 `gh issue board-row update`,notes 中明确推荐 `--body-file`、quoted heredoc、只读 cleanup-plan、board-row update 默认 dry-run 和 `--expect-body-sha`/`--expect-updated-at` 并发保护。运行 `bun scripts/gh-cli-issue-guard-contract-test.ts`,确认 mock GitHub 覆盖污染命中、说明性 `\n` 命中不误报、短 body/null body guard、body-file dry-run 写入路径、`issue create --label cli,infra --label ops --dry-run` labels 解析和 request plan、真实 create REST payload labels、missing label 的结构化 `validation-failed`、comment-id/body-id 定位和 cleanupSuggestions、board-row list/get 复用 #20 表格解析、board-row update 给出 old/new row、body SHA、guard 结果、表格管道转义、默认 dry-run 不写入、带 `--expect-body-sha` 时只对 mock server PATCH、以及 board-row move 结构化 unsupported。对真实仓库只允许运行 `bun scripts/cli.ts gh issue scan-escape --repo pikasTech/unidesk --limit <N> --dry-run`、`bun scripts/cli.ts gh issue cleanup-plan --repo pikasTech/unidesk --limit <N>`、`bun scripts/cli.ts gh issue board-row list --repo pikasTech/unidesk --board-issue 20 --state open --dry-run` 或 `bun scripts/cli.ts gh issue board-row get <issueNumber> --repo pikasTech/unidesk --board-issue 20` 这类只读命令;不得运行真实历史评论清理、不得真实改写 #20/#24 正文,除非另有明确人工指令并先审阅 dry-run 输出和 body SHA。 diff --git a/docs/reference/cli.md b/docs/reference/cli.md index d62d57f7..428a0437 100644 --- a/docs/reference/cli.md +++ b/docs/reference/cli.md @@ -35,6 +35,7 @@ CLI 可以从 `master` 快速演进,但必须兼容 `deploy.json` 固定的 CI - `gh issue update <number> --mode replace|append --body-file <file>` 是正文更新主入口,`edit` 保留为兼容别名。`replace` 用文件正文替换现有 body;`append` 先读取当前 body,再按 UTF-8 文件字节追加,保留真实换行、反引号和 Markdown 表格。更新默认拒绝字面量 `null`、空白正文和过短正文;只有真实需要写短正文时才允许显式加 `--allow-short-body`,返回 JSON 会报告该风险。#20 总看板和 #24 指挥简报是长期 body-only issue,`--body-profile auto` 会按 issue number 自动启用结构 guard:#20 必须包含 `## 看板(OPEN)`,#24 必须包含 `## 常驻观察与长期建议`;也可显式使用 `--body-profile code-queue-board|commander-brief`。`--dry-run` 不 PATCH GitHub,输出新正文长度、SHA、关键标题检查结果、字面量 `\n`、反引号、Markdown 表格和 shell 污染信号;若环境里有 `GH_TOKEN` 或 `GITHUB_TOKEN`,dry-run 还会只读抓取旧正文长度、SHA 和 `updatedAt` 作为更新前对照。正式写入可带 `--expect-updated-at <updated_at>` 或 `--expect-body-sha <sha256>`,CLI 会先读当前 issue,匹配后才 PATCH,防止旧缓存覆盖新正文。 - `gh issue edit 24 --body-file <file> --notify-claudeqq-brief-diff [--dry-run]` 是指挥简报 #24 的通知入口。正式执行会先读取 GitHub 上 #24 旧正文并通过 #24 body profile guard,再从 `--body-file` 读取新正文;随后先 PATCH issue 主体,再把本次新增的 `## 更新 YYYY-MM-DD HH:MM 北京时间` 段落发送给 ClaudeQQ,ClaudeQQ 失败不会回滚 issue 正文,失败只体现在返回 JSON 的 `claudeqq.ok=false` 和结构化 `degradedReason`。带通知 flag 的 `--dry-run` 不 PATCH、不发送;它按新正文做发送预览,并在输出中标明非 dry-run 才会读取旧正文做可靠 diff。默认 ClaudeQQ 目标是私聊 `645275593`,默认 base URL 是 UniDesk 受控入口 `http://backend-core:8080/api/microservices/claudeqq/proxy`,可用 `UNIDESK_COMMANDER_BRIEF_CLAUDEQQ_ENABLED`、`UNIDESK_COMMANDER_BRIEF_CLAUDEQQ_BASE_URL`、`UNIDESK_COMMANDER_BRIEF_CLAUDEQQ_TARGET_TYPE`、`UNIDESK_COMMANDER_BRIEF_CLAUDEQQ_USER_ID`、`UNIDESK_COMMANDER_BRIEF_CLAUDEQQ_GROUP_ID` 和 `UNIDESK_COMMANDER_BRIEF_CLAUDEQQ_TIMEOUT_MS` 覆盖。 - `gh issue board-audit [--repo owner/name] [--board-issue 20] [--limit N] [--known-meta-issue N[,N...]] [--ignore-issue N[,N...]] [--dry-run]` 是 #20 长期总看板只读覆盖审计入口,默认 repo 为 `pikasTech/unidesk`、board issue 为 `20`、输出 JSON 且不 PATCH/POST/DELETE GitHub。它读取 board body、GitHub open issue 列表和 closed issue 列表,对比 OPEN/CLOSED Markdown 表格并输出 `missingOpenIssues`、`closedInOpenRows`、`missingClosedRows`、`openInClosedRows`、`rowValidationWarnings`、`ignoredIssues` 和 `recommendedActions`。当表格里存在 Issue 列时,row.issueNumber 优先取 Issue cell 中第一个指向 `/issues/<N>` 的 Markdown link,找不到时取开头的 `#N`;同一 Issue cell 里主引用后面的标题说明引用(例如 `#20 总看板`、`基于 #4`)不触发 `multiple-issue-references`。没有 Issue 列的旧表格仍回退到整行 issue 提取,并保留多 issue 引用告警。`相关 Code Queue 任务`/`relatedTask` 列允许 `—`、`-`、`n/a`、`无任务` 等无关联任务占位表示 closed 历史/治理项没有 Code Queue task;这个放宽不适用于 branch、acceptance 或 progress。默认把 #20 和 #24 作为 `known-meta` 治理/简报 issue 忽略;需要扩展治理项用 `--known-meta-issue`,临时排除业务 issue 用 `--ignore-issue`。指挥官发现总看板可能漏行时,应先跑 board audit 获取结构化结果,再决定是否人工编辑 #20,而不是只靠 grep。 +- `gh issue board-row list --board-issue 20 [--state open|closed|all] [--dry-run]`、`gh issue board-row get <issueNumber> --board-issue 20` 和 `gh issue board-row update <issueNumber> --board-issue 20 --field progress|status|validation|branch|tasks|focus --value <text> [--dry-run] [--expect-updated-at ts|--expect-body-sha sha256]` 是 #20 看板表格单行结构化入口。list/get 复用 board-audit parser,只读返回 row、cells、fields、section、lineNumber、bodySha 和 rowValidationWarnings。update 只替换命中的一行里一个单元格,返回 old/new row、old/new body SHA、body guard、request plan 和 parser 结果;默认没有并发期望时即使不写 `--dry-run` 也只做 dry-run,正式 PATCH 必须带 `--expect-body-sha` 或 `--expect-updated-at`。字段映射固定为:`branch` -> Branch,`progress` -> 进度,`status`/`validation` -> 验收状态,`tasks` -> 相关 Code Queue 任务,`focus` -> 当前关注点。单元格值中的 Markdown 表格管道会转义为 `\|`,真实换行会折叠为空格,避免新增字面量 `\n` 污染。`gh issue board-row move|delete` 第一阶段结构化返回 `unsupported-command`,因为 OPEN/CLOSED 表间移动和删行还需要明确插入位置、重复行和归档策略。 - `gh issue scan-escape [--repo owner/name] [--limit N] [--dry-run]` 只读扫描 issue 主体和 comments 中的字面量 `\n`、可疑 `\t`、shell newline escape、escaped backtick、ANSI escape 字符串、短 body、blank body 和 null body。输出固定 JSON,`findings` 会带 `bodyKind=issue-body|comment-body`、`issueNumber`、`issueId`、`commentId`、`lineNumber`、`column`、`kind`、`snippet` 和 `classification=suspected-pollution|explanatory-mention|risk`,用于区分说明性提到 `\n` 和疑似污染;`cleanupSuggestions` 只给 dry-run 清理建议、body/comment 定位和 diff-like preview,不 PATCH、不 DELETE、不真实清理历史 comment。`gh issue cleanup-plan` 是同一只读能力的别名,默认 `dryRun=true`。`gh pr list|read|view [--json ...]` 提供 REST 列表和详情,PR 字段白名单是 `body,title,state,number,url,author,head,base,draft,createdAt,updatedAt`。`gh pr create --title <title> --body-file <file>|--body <text> --base <branch> --head <branch> [--draft] [--dry-run]`、`gh pr update <number> --mode replace|append --body-file <file>|--body <text> [--title ...] [--dry-run]`、`gh pr comment create <number> --body-file <file>|--body <text> [--dry-run]`、`gh pr comment delete <commentId> [--dry-run]`、`gh pr close|reopen <number> [--dry-run]` 是 PR CRUD/生命周期入口。`pr create --dry-run` 只输出 planned operation,不访问 GitHub;非 dry-run 创建前会校验 repo、base、head 和 compare ahead 状态,成功时返回 PR number/url。`pr update --mode append` 会先读取当前 PR body 再追加正文。`gh pr delete <number>` 和 `gh pr merge` 本阶段不开放,始终结构化返回 `unsupported-command`;PR 生命周期删除语义请使用 `close`。 - PR dry-run/probe 的最小手动序列是:`bun scripts/cli.ts gh auth status --repo pikasTech/unidesk` 只读检查 token 来源、GitHub REST egress、repo 可见性和 issue read;`bun scripts/cli.ts gh pr create --repo pikasTech/unidesk --title <title> --body-file <file> --base master --head <head> --dry-run` 检查创建计划;`bun scripts/cli.ts gh pr list --repo pikasTech/unidesk --limit 5 --json number,title,state,url,head,base` 和 `bun scripts/cli.ts gh pr read <number> --repo pikasTech/unidesk --json body,title,state,head,base` 做只读 PR 观察;`bun scripts/cli.ts gh pr comment <number> --repo pikasTech/unidesk --body-file <file> --dry-run` 检查评论计划;`bun scripts/cli.ts gh pr merge <number> --repo pikasTech/unidesk` 必须失败并返回结构化 `unsupported-command`。Code Queue runner 可用 `bun scripts/code-queue-pr-preflight-example.ts --repo pikasTech/unidesk --base master --head <head> --comment-pr <number>` 一次性跑只读 auth status 与 PR create/comment dry-run;该脚本不得输出 token 值,也不会创建、评论或 merge PR。 - `ci install|status|run|publish-backend-core|publish-user-service|run-dev-e2e|logs` 管理 D601 原生 k3s 上的 Tekton CI。`run` 手动创建每 commit 检查和 Code Queue 只读性能门禁;`publish-backend-core` 与 `publish-user-service` 从 pushed Git commit 构建并发布 `127.0.0.1:5000/unidesk/<service>:<commit>` commit-pinned artifacts,输出 `artifactSummary`(含 `serviceId`、`sourceCommit`、`sourceRepo`、`dockerfile`、`imageRef`、`tag`、`digest`、`digestRef`),但不部署生产;`run-dev-e2e` 的 Git 控制 runner、短 launcher、host fetch 边界、临时 smoke namespace 和 no-CD 规则只在 `docs/reference/dev-ci-runner.md` 定义;Tekton CI 通用规则见 `docs/reference/ci.md`。 diff --git a/docs/reference/code-queue-supervision.md b/docs/reference/code-queue-supervision.md index 214ba90e..78c77902 100644 --- a/docs/reference/code-queue-supervision.md +++ b/docs/reference/code-queue-supervision.md @@ -95,7 +95,7 @@ issue 内容必须自包含,至少写清楚背景、外部收益、当前观 如果某个 worker 任务需要依赖 GitHub issue 内容,但 runner 的 issue 可达性尚未被单独验证,指挥官不能默认 worker 已能读取该 issue。此时 worker prompt 必须直接内嵌完整需求、约束和验收点,issue URL 只能作为辅助引用。若要把 issue 作为任务输入源,先单独做可达性探测,再决定是否把 issue 作为常规前置条件。 -GitHub issue/PR 操作应优先使用 UniDesk CLI 的安全入口:`bun scripts/cli.ts gh auth status`、`gh issue list/read/view/create/update/comment create/comment delete/close/reopen/scan-escape/cleanup-plan/board-audit`、`gh pr list/read/view/create/update/comment create/comment delete/close/reopen`。该入口默认 repo 是 `pikasTech/unidesk`,支持 `--repo owner/name`,输出稳定 JSON,并把 `missing-binary`、`missing-token`、`auth-failed`、`network-proxy-failed`、`permission-denied`、`repo-not-found`、`repo-forbidden`、`issue-not-found`、`pr-not-found`、`scope-insufficient`、`validation-failed`、`invalid-response`、`unsupported-command` 等失败原因结构化。失败对象必须包含 `runnerDisposition=infra-blocked|business-failed`,runner 应用它区分基础设施阻塞和业务/参数失败。`gh issue list --state open --limit N --json number,title,state,url` 是有界 issue 发现入口,`--state` 只接受 `open|closed|all`,list 字段白名单是 `number,title,state,url,updatedAt,createdAt,author,labels`;未知 state 或未知字段必须失败,不能静默返回空数组。`gh issue read <number> --json body` 是 canonical 入口,正文仍应从 `.data.issue.body` 读取;`view` 只保留为兼容别名。未知 `--json` 字段必须失败,不得让调用方把空正文误判为读取成功。`gh issue scan-escape --limit N [--dry-run]` 与 `gh issue cleanup-plan` 只读扫描 issue body/comments 的字面量 `\n`、shell escape、短 body、blank/null body,输出 `classification=suspected-pollution|explanatory-mention|risk`、body/comment id、预览和清理建议;说明性提到 `\n` 不应被当成污染,cleanup-plan 永远不真实清理历史评论。`gh issue board-audit --board-issue 20 --limit N --dry-run` 只读对比 GitHub open/closed issue 列表与 #20 的 OPEN/CLOSED Markdown 表格,输出 `missingOpenIssues`、`closedInOpenRows`、`missingClosedRows`、`rowValidationWarnings`、`ignoredIssues` 和 `recommendedActions`。存在 Issue 列时,row key 优先来自 Issue cell 的主 Markdown issue link `/issues/<N>`,其次是开头 `#N`;主引用后的标题说明引用不触发多 issue 告警。没有 Issue 列的旧表格仍使用整行 fallback 并保留 `multiple-issue-references`。`相关 Code Queue 任务` 可用 `—`、`-` 或等价无任务占位表达无关联 task,但 branch、acceptance 和 progress 仍必须填写真实值;默认把 #20/#24 作为 known-meta 忽略。指挥官发现 #20 可能漏跟 open issue 或分表错误时,应先跑 board audit 取得结构化缺口,再人工审阅和编辑 #20,不要只靠 grep。`gh pr list|read|view --json ...` 支持 `body,title,state,number,url,author,head,base,draft,createdAt,updatedAt` 字段白名单。issue/PR 创建、更新、评论、评论删除、关闭和重开使用 GitHub REST API;只要有 `GH_TOKEN` 或 `GITHUB_TOKEN`,就不依赖系统 `gh` binary。`gh` binary 只作为状态探测和 `gh auth token` fallback,不是写操作的主路径。GitHub 不支持 issue/PR 硬删除,`gh issue delete` 和 `gh pr delete` 必须结构化返回 `unsupported-command`;生命周期删除语义使用 `close`。`gh pr merge` 仍然不开放。 +GitHub issue/PR 操作应优先使用 UniDesk CLI 的安全入口:`bun scripts/cli.ts gh auth status`、`gh issue list/read/view/create/update/comment create/comment delete/close/reopen/scan-escape/cleanup-plan/board-audit/board-row list/board-row get/board-row update`、`gh pr list/read/view/create/update/comment create/comment delete/close/reopen`。该入口默认 repo 是 `pikasTech/unidesk`,支持 `--repo owner/name`,输出稳定 JSON,并把 `missing-binary`、`missing-token`、`auth-failed`、`network-proxy-failed`、`permission-denied`、`repo-not-found`、`repo-forbidden`、`issue-not-found`、`pr-not-found`、`scope-insufficient`、`validation-failed`、`invalid-response`、`unsupported-command` 等失败原因结构化。失败对象必须包含 `runnerDisposition=infra-blocked|business-failed`,runner 应用它区分基础设施阻塞和业务/参数失败。`gh issue list --state open --limit N --json number,title,state,url` 是有界 issue 发现入口,`--state` 只接受 `open|closed|all`,list 字段白名单是 `number,title,state,url,updatedAt,createdAt,author,labels`;未知 state 或未知字段必须失败,不能静默返回空数组。`gh issue read <number> --json body` 是 canonical 入口,正文仍应从 `.data.issue.body` 读取;`view` 只保留为兼容别名。未知 `--json` 字段必须失败,不得让调用方把空正文误判为读取成功。`gh issue scan-escape --limit N [--dry-run]` 与 `gh issue cleanup-plan` 只读扫描 issue body/comments 的字面量 `\n`、shell escape、短 body、blank/null body,输出 `classification=suspected-pollution|explanatory-mention|risk`、body/comment id、预览和清理建议;说明性提到 `\n` 不应被当成污染,cleanup-plan 永远不真实清理历史评论。`gh issue board-audit --board-issue 20 --limit N --dry-run` 只读对比 GitHub open/closed issue 列表与 #20 的 OPEN/CLOSED Markdown 表格,输出 `missingOpenIssues`、`closedInOpenRows`、`missingClosedRows`、`rowValidationWarnings`、`ignoredIssues` 和 `recommendedActions`。存在 Issue 列时,row key 优先来自 Issue cell 的主 Markdown issue link `/issues/<N>`,其次是开头 `#N`;主引用后的标题说明引用不触发多 issue 告警。没有 Issue 列的旧表格仍使用整行 fallback 并保留 `multiple-issue-references`。`相关 Code Queue 任务` 可用 `—`、`-` 或等价无任务占位表达无关联 task,但 branch、acceptance 和 progress 仍必须填写真实值;默认把 #20/#24 作为 known-meta 忽略。指挥官发现 #20 可能漏跟 open issue 或分表错误时,应先跑 board audit 取得结构化缺口,再人工审阅和编辑 #20,不要只靠 grep。`gh issue board-row list --board-issue 20 --state open|closed|all` 和 `gh issue board-row get <issueNumber> --board-issue 20` 复用同一个 parser 读取表格行;`gh issue board-row update <issueNumber> --board-issue 20 --field progress|status|validation|branch|tasks|focus --value <text>` 只替换一行一个单元格,输出 old/new row、body SHA、body guard 和 request plan,且默认 dry-run,正式写入必须带 `--expect-body-sha` 或 `--expect-updated-at`。字段映射中 `status`/`validation` 都指向 `验收状态`,`tasks` 指向 `相关 Code Queue 任务`,`focus` 指向 `当前关注点`;单元格管道会转义、真实换行会折叠为空格,避免新增字面量 `\n`。`board-row move/delete` 暂时结构化 unsupported。`gh pr list|read|view --json ...` 支持 `body,title,state,number,url,author,head,base,draft,createdAt,updatedAt` 字段白名单。issue/PR 创建、更新、评论、评论删除、关闭和重开使用 GitHub REST API;只要有 `GH_TOKEN` 或 `GITHUB_TOKEN`,就不依赖系统 `gh` binary。`gh` binary 只作为状态探测和 `gh auth token` fallback,不是写操作的主路径。GitHub 不支持 issue/PR 硬删除,`gh issue delete` 和 `gh pr delete` 必须结构化返回 `unsupported-command`;生命周期删除语义使用 `close`。`gh pr merge` 仍然不开放。 CLI 是短 shout 的需求原语,不是长驻服务器进程。CLI 功能不好用、兼容性不足、安全 guard 不够或输出不利于 runner/指挥官使用时,应默认创建 GitHub issue 并用 Code Queue 推进;这类 CLI 问题走 `master`、remote commit、轻量 contract test 和文档更新,不套用 backend-core、Code Queue runtime 这类运行态服务的重部署门禁。若 CLI 缺陷已经阻塞当前指挥,可以先做最小安全绕行,同时把长期修复写入 issue 并派单。 diff --git a/scripts/gh-cli-issue-guard-contract-test.ts b/scripts/gh-cli-issue-guard-contract-test.ts index 095fd040..c335b428 100644 --- a/scripts/gh-cli-issue-guard-contract-test.ts +++ b/scripts/gh-cli-issue-guard-contract-test.ts @@ -465,8 +465,11 @@ export async function runGhCliIssueGuardContract(): Promise<JsonRecord> { assertCondition(usage.some((line) => line.includes("gh issue list")), "gh help should list issue list", { usage }); assertCondition(usage.some((line) => line.includes("gh issue read")), "gh help should list issue read", { usage }); assertCondition(usage.some((line) => line.includes("gh issue view")), "gh help should list issue view", { usage }); + assertCondition(usage.some((line) => line.includes("gh issue board-row list")), "gh help should list board-row list", { usage }); + assertCondition(usage.some((line) => line.includes("gh issue board-row update")), "gh help should list board-row update", { usage }); assertCondition(notes.some((line) => line.includes("canonical read path")), "gh help should state issue read is canonical", { notes }); assertCondition(notes.some((line) => line.includes("compatibility alias")), "gh help should state issue view is alias", { notes }); + assertCondition(notes.some((line) => line.includes("board-row update changes one table cell")), "gh help should describe board-row update safety", { notes }); const mock = await startMockGitHub(); const tmp = mkdtempSync(join(tmpdir(), "unidesk-gh-issue-guard-")); @@ -566,6 +569,74 @@ export async function runGhCliIssueGuardContract(): Promise<JsonRecord> { const legacyBoardWriteCount = mock.requests.slice(legacyBoardAuditRequestCountBefore).filter((request) => request.method === "PATCH" || request.method === "DELETE" || request.method === "POST").length; assertCondition(legacyBoardWriteCount === 0, "legacy board-audit must not write GitHub", { requests: mock.requests.slice(legacyBoardAuditRequestCountBefore) }); + const boardRowListRequestCountBefore = mock.requests.length; + const boardRowList = await runCli(["gh", "issue", "board-row", "list", "--repo", "pikasTech/unidesk", "--board-issue", "20", "--state", "open", "--dry-run"], env); + assertCondition(boardRowList.status === 0, "board-row list should succeed", boardRowList.json ?? { stdout: boardRowList.stdout, stderr: boardRowList.stderr }); + const boardRowListData = dataOf(boardRowList.json ?? {}); + assertCondition(boardRowListData.command === "issue board-row list" && boardRowListData.readOnly === true && boardRowListData.dryRun === true, "board-row list should be read-only", boardRowListData); + assertCondition(boardRowListData.state === "open" && boardRowListData.count === 4, "board-row list should filter OPEN rows", boardRowListData); + const boardRowListRows = boardRowListData.rows as JsonRecord[]; + assertCondition(Array.isArray(boardRowListRows) && boardRowListRows.some((row) => row.issueNumber === 45), "board-row list should use primary markdown issue link row keys", boardRowListRows); + const listWriteCount = mock.requests.slice(boardRowListRequestCountBefore).filter((request) => request.method === "PATCH" || request.method === "DELETE" || request.method === "POST").length; + assertCondition(listWriteCount === 0, "board-row list must not write GitHub", { requests: mock.requests.slice(boardRowListRequestCountBefore) }); + + const boardRowGet = await runCli(["gh", "issue", "board-row", "get", "35", "--repo", "pikasTech/unidesk", "--board-issue", "20"], env); + assertCondition(boardRowGet.status === 0, "board-row get should succeed", boardRowGet.json ?? { stdout: boardRowGet.stdout, stderr: boardRowGet.stderr }); + const boardRowGetData = dataOf(boardRowGet.json ?? {}); + const boardRowGetRow = boardRowGetData.row as JsonRecord; + const boardRowGetFields = boardRowGetRow.fields as JsonRecord; + assertCondition(boardRowGetRow.issueNumber === 35 && boardRowGetRow.section === "open", "board-row get should return the target row", boardRowGetData); + assertCondition(boardRowGetFields.branch === "master" && boardRowGetFields.status === "pass" && boardRowGetFields.validation === "pass" && boardRowGetFields.tasks === "cq-35" && String(boardRowGetFields.focus ?? "").includes("当前关注点"), "board-row get should expose canonical field aliases", boardRowGetFields); + + const boardRowDryRunRequestCountBefore = mock.requests.length; + const boardRowDryRun = await runCli(["gh", "issue", "board-row", "update", "35", "--repo", "pikasTech/unidesk", "--board-issue", "20", "--field", "focus", "--value", "复核 A | B\nsecond line"], env); + assertCondition(boardRowDryRun.status === 0, "board-row update should default to dry-run without concurrency expectation", boardRowDryRun.json ?? { stdout: boardRowDryRun.stdout, stderr: boardRowDryRun.stderr }); + const boardRowDryRunData = dataOf(boardRowDryRun.json ?? {}); + assertCondition(boardRowDryRunData.command === "issue board-row update" && boardRowDryRunData.dryRun === true && boardRowDryRunData.planned === true, "board-row update should default to dry-run", boardRowDryRunData); + const dryRunUpdate = boardRowDryRunData.update as JsonRecord; + assertCondition(dryRunUpdate.oldRow === "| #35 | master | pass | cq-35 | 当前关注点:#19 / #26 / #30 | doing |", "board-row dry-run should expose old row", dryRunUpdate); + assertCondition(dryRunUpdate.newRow === "| #35 | master | pass | cq-35 | 复核 A \\| B second line | doing |", "board-row dry-run should escape table pipes and fold cell newlines", dryRunUpdate); + const dryRunGuard = boardRowDryRunData.guard as JsonRecord; + assertCondition(dryRunGuard.ok === true, "board-row dry-run should include body guard result", dryRunGuard); + const dryRunSafety = boardRowDryRunData.bodyOnlySafety as JsonRecord; + const dryRunOldBody = dryRunSafety.oldBody as JsonRecord; + const dryRunNewBody = dryRunSafety.newBody as JsonRecord; + assertCondition(typeof dryRunOldBody.bodySha === "string" && String(dryRunOldBody.bodySha).length === 64, "board-row dry-run should expose old body sha", dryRunSafety); + assertCondition(dryRunNewBody.containsLiteralBackslashN === false && dryRunNewBody.shellPollution && typeof dryRunNewBody.shellPollution === "object", "board-row dry-run must not introduce literal backslash-n pollution", dryRunNewBody); + const defaultDryRunWriteCount = mock.requests.slice(boardRowDryRunRequestCountBefore).filter((request) => request.method === "PATCH").length; + assertCondition(defaultDryRunWriteCount === 0, "board-row default dry-run must not PATCH GitHub", { requests: mock.requests.slice(boardRowDryRunRequestCountBefore) }); + + const boardRowValidationDryRun = await runCli(["gh", "issue", "board-row", "update", "35", "--repo", "pikasTech/unidesk", "--board-issue", "20", "--field", "validation", "--value", "manual pass", "--dry-run"], env); + assertCondition(boardRowValidationDryRun.status === 0, "board-row update validation alias should dry-run", boardRowValidationDryRun.json ?? { stdout: boardRowValidationDryRun.stdout, stderr: boardRowValidationDryRun.stderr }); + const boardRowValidationData = dataOf(boardRowValidationDryRun.json ?? {}); + const validationUpdate = boardRowValidationData.update as JsonRecord; + assertCondition(validationUpdate.targetColumn === "acceptance" && validationUpdate.targetColumnIndex === 2, "validation field should map to 验收状态/acceptance column", validationUpdate); + + const boardRowPollutedValue = await runCli(["gh", "issue", "board-row", "update", "35", "--repo", "pikasTech/unidesk", "--board-issue", "20", "--field", "focus", "--value", "bad\\nvalue", "--dry-run"], env); + assertCondition(boardRowPollutedValue.status !== 0, "board-row update should reject literal backslash-n values", boardRowPollutedValue.json ?? { stdout: boardRowPollutedValue.stdout, stderr: boardRowPollutedValue.stderr }); + const boardRowPollutedData = failedDataOf(boardRowPollutedValue.json ?? {}); + const boardRowPollutedDetails = boardRowPollutedData.details as JsonRecord; + assertCondition(boardRowPollutedData.degradedReason === "validation-failed" && String(boardRowPollutedDetails.message ?? "").includes("--value contains literal shell escape"), "board-row polluted value should fail before planning", boardRowPollutedData); + + const boardRowPatchRequestCountBefore = mock.requests.length; + const oldBodySha = String(dryRunOldBody.bodySha); + const boardRowPatch = await runCli(["gh", "issue", "board-row", "update", "35", "--repo", "pikasTech/unidesk", "--board-issue", "20", "--field", "focus", "--value", "复核 A | B\nsecond line", "--expect-body-sha", oldBodySha], env); + assertCondition(boardRowPatch.status === 0, "board-row update with expect body sha should PATCH", boardRowPatch.json ?? { stdout: boardRowPatch.stdout, stderr: boardRowPatch.stderr }); + const boardRowPatchData = dataOf(boardRowPatch.json ?? {}); + assertCondition(boardRowPatchData.dryRun === false && boardRowPatchData.rest === true, "board-row patch should report a real REST update", boardRowPatchData); + const boardRowPatchRequests = mock.requests.slice(boardRowPatchRequestCountBefore).filter((request) => request.method === "PATCH" && request.url === "/repos/pikasTech/unidesk/issues/20"); + assertCondition(boardRowPatchRequests.length === 1, "board-row patch should send exactly one PATCH", { requests: mock.requests.slice(boardRowPatchRequestCountBefore) }); + const boardRowPatchPayload = JSON.parse(boardRowPatchRequests[0]?.body ?? "{}") as JsonRecord; + assertCondition(typeof boardRowPatchPayload.body === "string", "board-row patch payload should carry body string", boardRowPatchPayload); + const patchedBody = String(boardRowPatchPayload.body ?? ""); + assertCondition(patchedBody.includes("| #35 | master | pass | cq-35 | 复核 A \\| B second line | doing |"), "board-row patch payload should contain escaped updated row", patchedBody); + assertCondition(!patchedBody.includes("\\n"), "board-row patch payload should not add literal backslash-n pollution to markdown body", patchedBody); + + const boardRowMove = await runCli(["gh", "issue", "board-row", "move", "35", "--repo", "pikasTech/unidesk", "--board-issue", "20"], env); + assertCondition(boardRowMove.status !== 0, "board-row move should be structured unsupported in first phase", boardRowMove.json ?? { stdout: boardRowMove.stdout, stderr: boardRowMove.stderr }); + const boardRowMoveData = failedDataOf(boardRowMove.json ?? {}); + assertCondition(boardRowMoveData.degradedReason === "unsupported-command" && boardRowMoveData.runnerDisposition === "business-failed", "board-row move unsupported should be structured business failure", boardRowMoveData); + const badListField = await runCli(["gh", "issue", "list", "--repo", "pikasTech/unidesk", "--json", "number,body"], env); assertCondition(badListField.status !== 0, "issue list unsupported --json field should fail", badListField.json ?? { stdout: badListField.stdout }); const badListFieldData = failedDataOf(badListField.json ?? {}); @@ -742,6 +813,11 @@ export async function runGhCliIssueGuardContract(): Promise<JsonRecord> { "issue scan-escape classifies pollution, explanatory mentions, and body risks", "issue cleanup-plan remains dry-run with body/comment cleanup suggestions", "issue board-audit reports missing open rows, closed/open section mismatches, missing closed rows, meta ignores, and row validation warnings without writes", + "issue board-row list/get expose parsed #20 rows without writes", + "issue board-row update defaults to dry-run, reports old/new row, body SHA, guard result, and does not introduce literal backslash-n", + "issue board-row update rejects literal backslash-n cell values", + "issue board-row update escapes markdown table pipes and performs guarded PATCH with --expect-body-sha", + "issue board-row move is structurally unsupported in the first phase", "issue create dry-run parses repeated/comma labels and exposes request plan", "issue create sends labels through REST and preserves GitHub validation errors for missing labels", "issue list unsupported fields and states fail structurally", diff --git a/scripts/src/gh.ts b/scripts/src/gh.ts index d915773f..d56b8aea 100644 --- a/scripts/src/gh.ts +++ b/scripts/src/gh.ts @@ -15,6 +15,7 @@ const CODE_QUEUE_BOARD_TARGET_ISSUE = 20; const COMMANDER_BRIEF_TARGET_ISSUE = 24; const DEFAULT_BOARD_KNOWN_META_ISSUES = [CODE_QUEUE_BOARD_TARGET_ISSUE, COMMANDER_BRIEF_TARGET_ISSUE] as const; const BOARD_AUDIT_REQUIRED_COLUMNS = ["branch", "acceptance", "relatedTask", "progress"] as const; +const BOARD_ROW_FIELDS = ["progress", "status", "validation", "branch", "tasks", "focus"] as const; const ISSUE_VIEW_JSON_FIELDS = ["body", "title", "state", "comments", "number", "url", "author", "createdAt", "updatedAt"] as const; const ISSUE_LIST_JSON_FIELDS = ["number", "title", "state", "url", "updatedAt", "createdAt", "author", "labels"] as const; const PR_JSON_FIELDS = ["body", "title", "state", "number", "url", "author", "head", "base", "draft", "createdAt", "updatedAt"] as const; @@ -48,6 +49,8 @@ type EscapeBodyKind = "issue-body" | "comment-body"; type CleanupSuggestionAction = "rewrite-issue-body-with-body-file" | "review-comment-manually" | "review-body-length" | "no-cleanup-needed"; type BoardSectionKind = "open" | "closed"; type BoardRequiredColumn = typeof BOARD_AUDIT_REQUIRED_COLUMNS[number]; +type BoardColumnKind = BoardRequiredColumn | "focus"; +type BoardRowField = typeof BOARD_ROW_FIELDS[number]; type GitHubDegradedReason = | "missing-binary" @@ -225,6 +228,19 @@ interface BoardRowValidationWarning { rowPreview: string; } +interface BoardRowLookup { + ok: true; + section: BoardTableSection; + row: BoardTableRow; + matches: Array<{ section: BoardTableSection; row: BoardTableRow }>; +} + +interface BoardRowUpdatePlanResult { + ok: true; + plan: Record<string, unknown>; + newBody: string; +} + interface GitHubCommandResult { ok: boolean; repo: string; @@ -235,6 +251,8 @@ interface GitHubCommandResult { [key: string]: unknown; } +type GitHubCommandFailure = GitHubCommandResult & { ok: false }; + interface GitHubTokenProbe { present: boolean; source: "GH_TOKEN" | "GITHUB_TOKEN" | "gh-auth-token" | null; @@ -267,6 +285,8 @@ interface GitHubOptions { expectUpdatedAt?: string; expectBodySha?: string; bodyProfile: IssueBodyProfileOption; + boardRowField?: BoardRowField; + boardRowValue?: string; } interface GitHubErrorPayload { @@ -433,6 +453,11 @@ function positiveIntegerOption(args: string[], name: string, defaultValue: numbe return Math.min(value, maxValue); } +function validateEnumValue<T extends string>(name: string, raw: string, allowedValues: readonly T[]): T { + if ((allowedValues as readonly string[]).includes(raw)) return raw as T; + throw new Error(`unsupported ${name} ${raw}; supported values: ${allowedValues.join(",")}`); +} + function validateJsonFields<T extends string>(command: string, requested: string[] | undefined, allowedFields: readonly T[]): T[] | undefined { if (requested === undefined) return undefined; const allowed = new Set<string>(allowedFields); @@ -481,8 +506,14 @@ function parseIssueBodyProfile(args: string[]): IssueBodyProfileOption { throw new Error(`unsupported --body-profile ${raw}; supported profiles: auto, code-queue-board, commander-brief`); } +function parseBoardRowField(args: string[]): BoardRowField | undefined { + const raw = optionValue(args, "--field"); + if (raw === undefined) return undefined; + return validateEnumValue("--field", raw, BOARD_ROW_FIELDS); +} + function validateKnownOptions(args: string[]): void { - const valueOptions = new Set(["--repo", "--limit", "--board-issue", "--known-meta-issue", "--ignore-issue", "--title", "--body-file", "--body", "--base", "--head", "--json", "--state", "--mode", "--expect-updated-at", "--expect-body-sha", "--body-profile", "--label"]); + const valueOptions = new Set(["--repo", "--limit", "--board-issue", "--known-meta-issue", "--ignore-issue", "--title", "--body-file", "--body", "--base", "--head", "--json", "--state", "--mode", "--expect-updated-at", "--expect-body-sha", "--body-profile", "--label", "--field", "--value"]); const flagOptions = new Set(["--dry-run", "--draft", "--notify-claudeqq-brief-diff", "--allow-short-body"]); for (let index = 0; index < args.length; index += 1) { const arg = args[index]; @@ -524,6 +555,8 @@ function parseOptions(args: string[]): GitHubOptions { expectUpdatedAt: optionValue(args, "--expect-updated-at"), expectBodySha: optionValue(args, "--expect-body-sha"), bodyProfile: parseIssueBodyProfile(args), + boardRowField: parseBoardRowField(args), + boardRowValue: optionValue(args, "--value"), }; } @@ -1232,15 +1265,43 @@ function normalizeBoardHeader(header: string): string { return stripMarkdownInline(header).toLowerCase().replace(/\s+/g, ""); } -function boardHeaderColumnKind(header: string): BoardRequiredColumn | null { +function boardHeaderColumnKind(header: string): BoardColumnKind | null { const normalized = normalizeBoardHeader(header); if (["branch", "分支", "目标分支", "工作分支"].includes(normalized)) return "branch"; if (["acceptance", "验收", "验收状态", "验收结果", "验收标准"].includes(normalized)) return "acceptance"; if (["relatedtask", "task", "codequeue", "codequeuetask", "相关任务", "任务", "codequeue任务", "cq任务", "相关codequeue任务", "相关codequeuetask"].includes(normalized)) return "relatedTask"; + if (["focus", "currentfocus", "关注点", "当前关注点", "当前focus"].includes(normalized)) return "focus"; if (["progress", "进度", "状态", "当前进度"].includes(normalized)) return "progress"; return null; } +function boardColumnIndexMap(headers: string[]): Map<BoardColumnKind, number> { + const map = new Map<BoardColumnKind, number>(); + headers.forEach((header, headerIndex) => { + const kind = boardHeaderColumnKind(header); + if (kind !== null && !map.has(kind)) map.set(kind, headerIndex); + }); + return map; +} + +function boardRequiredColumnIndexMap(headers: string[]): Map<BoardRequiredColumn, number> { + const map = new Map<BoardRequiredColumn, number>(); + const allColumns = boardColumnIndexMap(headers); + for (const column of BOARD_AUDIT_REQUIRED_COLUMNS) { + const index = allColumns.get(column); + if (index !== undefined) map.set(column, index); + } + return map; +} + +function boardColumnKindForField(field: BoardRowField): BoardColumnKind { + if (field === "branch") return "branch"; + if (field === "tasks") return "relatedTask"; + if (field === "focus") return "focus"; + if (field === "progress") return "progress"; + return "acceptance"; +} + function boardIssueColumnIndex(headers: string[]): number | null { for (let index = 0; index < headers.length; index += 1) { const normalized = normalizeBoardHeader(headers[index]); @@ -1252,7 +1313,20 @@ function boardIssueColumnIndex(headers: string[]): number | null { function markdownCells(line: string): string[] { const trimmed = line.trim(); const withoutOuterPipes = trimmed.replace(/^\|/u, "").replace(/\|$/u, ""); - return withoutOuterPipes.split("|").map((cell) => cell.trim()); + const cells: string[] = []; + let current = ""; + for (let index = 0; index < withoutOuterPipes.length; index += 1) { + const char = withoutOuterPipes[index]; + const previous = withoutOuterPipes[index - 1]; + if (char === "|" && previous !== "\\") { + cells.push(current.trim().replace(/\\\|/g, "|")); + current = ""; + continue; + } + current += char; + } + cells.push(current.trim().replace(/\\\|/g, "|")); + return cells; } function isMarkdownTableSeparator(line: string): boolean { @@ -1364,11 +1438,7 @@ function parseBoardTables(body: string): { sections: BoardTableSection[]; warnin const separator = lines[index + 1]; if (separator === undefined || !separator.trim().startsWith("|") || !isMarkdownTableSeparator(separator)) continue; const headers = markdownCells(lines[index]); - const columnMap = new Map<BoardRequiredColumn, number>(); - headers.forEach((header, headerIndex) => { - const kind = boardHeaderColumnKind(header); - if (kind !== null && !columnMap.has(kind)) columnMap.set(kind, headerIndex); - }); + const columnMap = boardRequiredColumnIndexMap(headers); const issueColumnIndex = boardIssueColumnIndex(headers); const rows: BoardTableRow[] = []; let rowIndex = index + 2; @@ -1460,6 +1530,342 @@ function boardRowsByIssue(rows: BoardTableRow[]): Map<number, BoardTableRow[]> { return map; } +function boardRowFieldValues(section: BoardTableSection, row: BoardTableRow): Record<BoardRowField, string | null> { + const columnMap = boardColumnIndexMap(section.headers); + const valueFor = (kind: BoardColumnKind): string | null => { + const index = columnMap.get(kind); + return index === undefined ? null : row.cells[index] ?? ""; + }; + const acceptance = valueFor("acceptance"); + return { + progress: valueFor("progress"), + status: acceptance, + validation: acceptance, + branch: valueFor("branch"), + tasks: valueFor("relatedTask"), + focus: valueFor("focus"), + }; +} + +function boardRowSummary(section: BoardTableSection, row: BoardTableRow): Record<string, unknown> { + return { + issueNumber: row.issueNumber, + section: row.section, + lineNumber: row.lineNumber, + heading: section.heading, + headers: section.headers, + title: row.title, + cells: row.cells, + fields: boardRowFieldValues(section, row), + raw: row.raw, + rowPreview: preview(row.raw), + }; +} + +function boardRowsForState(sections: BoardTableSection[], state: IssueListState): Array<{ section: BoardTableSection; row: BoardTableRow }> { + return sections + .filter((section) => state === "all" || section.kind === state) + .flatMap((section) => section.rows.map((row) => ({ section, row }))); +} + +function findBoardRow( + repo: string, + sections: BoardTableSection[], + issueNumber: number, + state: IssueListState = "all", +): BoardRowLookup | GitHubCommandFailure { + const matches = boardRowsForState(sections, state).filter(({ row }) => row.issueNumber === issueNumber); + if (matches.length === 0) { + return validationError("issue board-row", repo, `board row for issue #${issueNumber} was not found`, { + issueNumber, + state, + availableIssueNumbers: boardRowsForState(sections, state) + .map(({ row }) => row.issueNumber) + .filter((value): value is number => value !== null) + .sort((a, b) => a - b), + }) as GitHubCommandFailure; + } + if (matches.length > 1) { + return validationError("issue board-row", repo, `board row for issue #${issueNumber} is ambiguous`, { + issueNumber, + state, + matches: matches.map(({ section, row }) => ({ section: section.kind, lineNumber: row.lineNumber, rowPreview: preview(row.raw) })), + }) as GitHubCommandFailure; + } + return { ok: true, ...matches[0], matches }; +} + +function escapeMarkdownTableCell(value: string): string { + return value + .replace(/\r\n/g, " ") + .replace(/[\r\n]+/g, " ") + .replace(/\|/g, "\\|") + .trim(); +} + +function boardRowValuePollutionEvidence(value: string): string[] { + return shellPollutionEvidence(value).filter((item) => item !== "bare-carriage-return"); +} + +function renderMarkdownTableRow(cells: string[]): string { + return `| ${cells.map(escapeMarkdownTableCell).join(" | ")} |`; +} + +function replaceBoardBodyLine(body: string, lineNumber: number, newLine: string): string { + const normalized = normalizeNewlines(body); + const lines = normalized.split("\n"); + if (lineNumber <= 0 || lineNumber > lines.length) throw new Error(`line ${lineNumber} is outside the board body`); + lines[lineNumber - 1] = newLine; + return lines.join("\n"); +} + +function boardBodySafetyIssue(repo: string, boardIssueNumber: number, body: string, options: GitHubOptions): GitHubCommandResult | null { + const guardOptions: GitHubOptions = { ...options, bodyProfile: boardIssueNumber === CODE_QUEUE_BOARD_TARGET_ISSUE ? "code-queue-board" : options.bodyProfile }; + return validateIssueBodyGuard(repo, boardIssueNumber, body, guardOptions); +} + +function boardBodyGuardSummary(boardIssueNumber: number, body: string, options: GitHubOptions): Record<string, unknown> { + const guardOptions: GitHubOptions = { ...options, bodyProfile: boardIssueNumber === CODE_QUEUE_BOARD_TARGET_ISSUE ? "code-queue-board" : options.bodyProfile }; + return issueEditGuardSummary(boardIssueNumber, body, guardOptions); +} + +function boardRowUpdatePlan( + repo: string, + boardIssue: GitHubIssue, + issueNumber: number, + field: BoardRowField, + value: string, + parsed: { sections: BoardTableSection[]; warnings: BoardRowValidationWarning[] }, +): BoardRowUpdatePlanResult | GitHubCommandFailure { + const found = findBoardRow(repo, parsed.sections, issueNumber); + if (found.ok === false) return found; + const { section, row } = found; + const targetColumn = boardColumnKindForField(field); + const columnMap = boardColumnIndexMap(section.headers); + const columnIndex = columnMap.get(targetColumn); + if (columnIndex === undefined) { + return validationError("issue board-row update", repo, `board table does not contain a column for --field ${field}`, { + issueNumber, + field, + targetColumn, + section: section.kind, + headers: section.headers, + supportedFields: BOARD_ROW_FIELDS.slice(), + }) as GitHubCommandFailure; + } + const oldCells = row.cells.slice(); + const newCells = oldCells.slice(); + while (newCells.length < section.headers.length) newCells.push(""); + newCells[columnIndex] = value; + const newRow = renderMarkdownTableRow(newCells); + const oldBody = boardIssue.body ?? ""; + let newBody: string; + try { + newBody = replaceBoardBodyLine(oldBody, row.lineNumber, newRow); + } catch (error) { + return validationError("issue board-row update", repo, error instanceof Error ? error.message : String(error), { + issueNumber, + field, + lineNumber: row.lineNumber, + }) as GitHubCommandFailure; + } + return { + ok: true, + newBody, + plan: { + repo, + boardIssue: boardIssue.number, + issueNumber, + field, + targetColumn, + targetColumnIndex: columnIndex, + section: section.kind, + lineNumber: row.lineNumber, + oldRow: row.raw, + newRow, + oldCells, + newCells, + oldValue: oldCells[columnIndex] ?? "", + newValue: value, + row: { + old: boardRowSummary(section, row), + new: { + ...boardRowSummary(section, { ...row, raw: newRow, cells: newCells }), + updatedField: field, + }, + }, + body: { + oldBodyChars: oldBody.length, + oldBodySha: bodySha(oldBody), + newBodyChars: newBody.length, + newBodySha: bodySha(newBody), + changed: oldBody !== newBody, + }, + tableParser: { + reused: "parseBoardTables", + rowValidationWarnings: parsed.warnings.length, + }, + request: { + method: "PATCH", + path: `/repos/{owner}/{repo}/issues/${boardIssue.number}`, + body: { bodyChars: newBody.length }, + }, + }, + }; +} + +async function issueBoardRowList(repo: string, token: string, options: GitHubOptions): Promise<GitHubCommandResult> { + const commandName = "issue board-row list"; + const boardIssue = await getIssue(token, repo, options.boardIssue); + if (isGitHubError(boardIssue)) return commandError(commandName, repo, boardIssue, { boardIssue: options.boardIssue }); + const parsed = parseBoardTables(boardIssue.body ?? ""); + const rows = boardRowsForState(parsed.sections, options.listState); + return { + ok: true, + command: commandName, + repo, + dryRun: true, + readOnly: true, + boardIssue: { + number: boardIssue.number, + title: boardIssue.title, + state: boardIssue.state, + url: boardIssue.html_url, + bodyChars: (boardIssue.body ?? "").length, + bodySha: bodySha(boardIssue.body ?? ""), + updatedAt: boardIssue.updated_at ?? null, + }, + state: options.listState, + count: rows.length, + rows: rows.map(({ section, row }) => boardRowSummary(section, row)), + sections: parsed.sections.map((section) => ({ kind: section.kind, heading: section.heading, headingLine: section.headingLine, headerLine: section.headerLine, headers: section.headers, rows: section.rows.length })), + rowValidationWarnings: parsed.warnings, + note: "Read-only board row list; no issue body was edited.", + }; +} + +async function issueBoardRowGet(repo: string, token: string, issueNumber: number, options: GitHubOptions): Promise<GitHubCommandResult> { + const commandName = "issue board-row get"; + const boardIssue = await getIssue(token, repo, options.boardIssue); + if (isGitHubError(boardIssue)) return commandError(commandName, repo, boardIssue, { boardIssue: options.boardIssue, issueNumber }); + const parsed = parseBoardTables(boardIssue.body ?? ""); + const found = findBoardRow(repo, parsed.sections, issueNumber); + if (found.ok === false) return { ...found, command: commandName, repo, boardIssue: options.boardIssue }; + return { + ok: true, + command: commandName, + repo, + dryRun: true, + readOnly: true, + boardIssue: { + number: boardIssue.number, + title: boardIssue.title, + state: boardIssue.state, + url: boardIssue.html_url, + bodyChars: (boardIssue.body ?? "").length, + bodySha: bodySha(boardIssue.body ?? ""), + updatedAt: boardIssue.updated_at ?? null, + }, + issueNumber, + row: boardRowSummary(found.section, found.row), + rowValidationWarnings: parsed.warnings.filter((warning) => warning.issueNumber === issueNumber), + note: "Read-only board row get; no issue body was edited.", + }; +} + +async function issueBoardRowUpdate(repo: string, token: string, issueNumber: number, options: GitHubOptions): Promise<GitHubCommandResult> { + const commandName = "issue board-row update"; + if (options.boardRowField === undefined) return validationError(commandName, repo, "issue board-row update requires --field progress|status|validation|branch|tasks|focus", { supportedFields: BOARD_ROW_FIELDS.slice() }); + if (options.boardRowValue === undefined) return validationError(commandName, repo, "issue board-row update requires --value <text>", { issueNumber, field: options.boardRowField }); + const valuePollutionEvidence = boardRowValuePollutionEvidence(options.boardRowValue); + if (valuePollutionEvidence.length > 0) { + return validationError(commandName, repo, "issue board-row update --value contains literal shell escape text that would pollute the board cell; pass real newlines or plain text instead", { + issueNumber, + field: options.boardRowField, + valuePreview: preview(options.boardRowValue), + shellPollution: { suspected: true, evidence: valuePollutionEvidence }, + }); + } + const concurrencyOptionError = assertConcurrencyOptions(options); + if (concurrencyOptionError !== null) return { ...concurrencyOptionError, command: commandName, repo }; + + const boardIssue = await getIssue(token, repo, options.boardIssue); + if (isGitHubError(boardIssue)) return commandError(commandName, repo, boardIssue, { boardIssue: options.boardIssue, issueNumber }); + const parsed = parseBoardTables(boardIssue.body ?? ""); + const planned = boardRowUpdatePlan(repo, boardIssue, issueNumber, options.boardRowField, options.boardRowValue, parsed); + if (planned.ok === false) return planned; + const guardError = boardBodySafetyIssue(repo, options.boardIssue, planned.newBody, options); + const guard = boardBodyGuardSummary(options.boardIssue, planned.newBody, options); + const effectiveDryRun = options.dryRun || (options.expectBodySha === undefined && options.expectUpdatedAt === undefined); + const base = { + ok: true, + command: commandName, + repo, + boardIssue: { + number: boardIssue.number, + title: boardIssue.title, + state: boardIssue.state, + url: boardIssue.html_url, + bodyChars: (boardIssue.body ?? "").length, + bodySha: bodySha(boardIssue.body ?? ""), + updatedAt: boardIssue.updated_at ?? null, + }, + issueNumber, + dryRun: effectiveDryRun, + planned: true, + update: planned.plan, + guard, + bodyOnlySafety: { + oldBody: { + fetched: true, + bodyChars: (boardIssue.body ?? "").length, + bodySha: bodySha(boardIssue.body ?? ""), + updatedAt: boardIssue.updated_at ?? null, + }, + newBody: { + bodyChars: planned.newBody.length, + bodySha: bodySha(planned.newBody), + ...bodySafetySignals(planned.newBody), + }, + }, + concurrency: { + expectUpdatedAt: options.expectUpdatedAt ?? null, + expectBodySha: options.expectBodySha ?? null, + note: "non-dry-run board-row update requires --expect-body-sha or --expect-updated-at and checks the current board issue before PATCH", + }, + }; + if (guardError !== null) return { ...guardError, command: commandName, update: planned.plan, guard }; + if (effectiveDryRun) { + return { + ...base, + dryRun: true, + wouldPatch: { issueNumber: options.boardIssue, bodySha: bodySha(planned.newBody), bodyChars: planned.newBody.length }, + note: options.expectBodySha === undefined && options.expectUpdatedAt === undefined + ? "Default dry-run because no concurrency expectation was supplied; no GitHub issue body was modified." + : "Dry-run only; no GitHub issue body was modified.", + }; + } + const concurrencyError = validateIssueConcurrency(repo, options.boardIssue, boardIssue, options); + if (concurrencyError !== null) return { ...base, ...concurrencyError }; + const { owner, name } = repoParts(repo); + const issue = await githubRequest<GitHubIssue>(token, "PATCH", `/repos/${owner}/${name}/issues/${options.boardIssue}`, { body: planned.newBody }); + if (isGitHubError(issue)) return commandError(commandName, repo, issue, { issueNumber, update: planned.plan }); + return { + ...base, + dryRun: false, + planned: false, + issue: issueSummary(issue), + concurrency: { + checked: true, + oldIssueUpdatedAt: boardIssue.updated_at ?? null, + oldBodySha: bodySha(boardIssue.body ?? ""), + expectUpdatedAt: options.expectUpdatedAt ?? null, + expectBodySha: options.expectBodySha ?? null, + }, + rest: true, + }; +} + function ignoredIssueList(issues: BoardIssueEntry[], ignoreMap: Map<number, "known-meta" | "ignored">): BoardIgnoredIssue[] { return sortedIssueEntries(issues.filter((issue) => ignoreMap.has(issue.number))).map((issue) => ({ number: issue.number, @@ -2921,6 +3327,10 @@ export function ghHelp(): unknown { "bun scripts/cli.ts gh issue scan-escape [--repo owner/name] [--limit N] [--dry-run]", "bun scripts/cli.ts gh issue cleanup-plan [--repo owner/name] [--limit N] [--dry-run]", "bun scripts/cli.ts gh issue board-audit [--repo owner/name] [--board-issue 20] [--limit N] [--known-meta-issue N[,N...]] [--ignore-issue N[,N...]] [--dry-run]", + "bun scripts/cli.ts gh issue board-row list [--repo owner/name] --board-issue 20 [--state open|closed|all] [--dry-run]", + "bun scripts/cli.ts gh issue board-row get <issueNumber> [--repo owner/name] --board-issue 20", + "bun scripts/cli.ts gh issue board-row update <issueNumber> [--repo owner/name] --board-issue 20 --field progress|status|validation|branch|tasks|focus --value <text> [--dry-run] [--expect-updated-at ts|--expect-body-sha sha256]", + "bun scripts/cli.ts gh issue board-row move|delete <issueNumber> [unsupported in first phase]", "bun scripts/cli.ts gh pr list [--repo owner/name] [--limit N] [--json number,title,state,url,updatedAt,createdAt,author,head,base,draft]", "bun scripts/cli.ts gh pr read <number> [--repo owner/name] [--json body,title,state,head,base,draft]", "bun scripts/cli.ts gh pr view <number> [--repo owner/name] [compatibility alias for pr read]", @@ -2948,6 +3358,8 @@ export function ghHelp(): unknown { "For JSON request bodies in other CLI namespaces, prefer --body-file or --body-stdin over long inline shell arguments; GitHub Markdown writes intentionally use --body-file only.", "issue scan-escape classifies literal \\n findings as suspected-pollution, explanatory-mention, or risk, and emits cleanupSuggestions with body/comment ids plus diff-like previews. cleanup-plan is an alias that remains dry-run/read-only.", "issue board-audit is read-only and defaults to repo pikasTech/unidesk plus board issue #20. It compares GitHub open/closed issue lists with the board OPEN/CLOSED tables and reports missingOpenIssues, closedInOpenRows, missingClosedRows, rowValidationWarnings, ignoredIssues, and recommendedActions. When an Issue column exists, row.issueNumber is taken from that column; #20 and #24 are known meta issues by default.", + "issue board-row list/get reuse the board-audit table parser and are read-only. board-row update changes one table cell by issue number, returns old/new row, body SHA, body guard and request plan, and defaults to dry-run unless --expect-updated-at or --expect-body-sha is supplied for the guarded PATCH. Field aliases map status and validation to the 验收状态 column, tasks to 相关 Code Queue 任务, and focus to 当前关注点.", + "issue board-row move/delete are structured unsupported in this phase; open/closed relocation and row removal need explicit table placement and duplicate-row semantics before they write.", "issue edit 24 --notify-claudeqq-brief-diff reads the old issue body, PATCHes the new body, and sends only newly added '## 更新 ... 北京时间' sections to ClaudeQQ; ClaudeQQ failure does not roll back GitHub.", "Commander brief ClaudeQQ defaults to private target 645275593 through backend-core /api/microservices/claudeqq/proxy; UNIDESK_COMMANDER_BRIEF_CLAUDEQQ_* env vars can override target, base URL, timeout, and enabled state.", "comment delete is supported because GitHub supports deleting issue comments; issue/pr hard delete is unsupported and close is the lifecycle alternative.", @@ -2974,9 +3386,9 @@ export async function runGhCommand(args: string[]): Promise<GitHubCommandResult const command = [top, sub].filter((value): value is string => value !== undefined).join(" ") || "gh"; return validationError(command, options.repo, "--notify-claudeqq-brief-diff is only supported by gh issue edit 24"); } - if (optionWasProvided(args, "--state") && !(top === "issue" && sub === "list")) { + if (optionWasProvided(args, "--state") && !(top === "issue" && (sub === "list" || sub === "board-row" && third === "list"))) { const command = [top, sub].filter((value): value is string => value !== undefined).join(" ") || "gh"; - return validationError(command, options.repo, "--state is only supported by gh issue list"); + return validationError(command, options.repo, "--state is only supported by gh issue list and gh issue board-row list"); } if (optionWasProvided(args, "--json") && !(top === "issue" && (isIssueReadCommand(sub) || sub === "list"))) { const command = [top, sub].filter((value): value is string => value !== undefined).join(" ") || "gh"; @@ -2988,13 +3400,21 @@ export async function runGhCommand(args: string[]): Promise<GitHubCommandResult const command = [top, sub].filter((value): value is string => value !== undefined).join(" ") || "gh"; return validationError(command, options.repo, "--mode is only supported by gh issue update/edit and gh pr update"); } - if ((options.allowShortBody || options.expectUpdatedAt !== undefined || options.expectBodySha !== undefined || optionWasProvided(args, "--body-profile")) && !(top === "issue" && (sub === "edit" || sub === "update"))) { + if ((options.allowShortBody || options.expectUpdatedAt !== undefined || options.expectBodySha !== undefined || optionWasProvided(args, "--body-profile")) && !(top === "issue" && (sub === "edit" || sub === "update" || sub === "board-row" && third === "update"))) { const command = [top, sub].filter((value): value is string => value !== undefined).join(" ") || "gh"; - return validationError(command, options.repo, "--allow-short-body, --expect-updated-at, --expect-body-sha, and --body-profile are only supported by gh issue update/edit"); + return validationError(command, options.repo, "--allow-short-body, --expect-updated-at, --expect-body-sha, and --body-profile are only supported by gh issue update/edit and gh issue board-row update"); } - if ((optionWasProvided(args, "--board-issue") || optionWasProvided(args, "--known-meta-issue") || optionWasProvided(args, "--ignore-issue")) && !(top === "issue" && sub === "board-audit")) { + if ((optionWasProvided(args, "--board-issue") || optionWasProvided(args, "--known-meta-issue") || optionWasProvided(args, "--ignore-issue")) && !(top === "issue" && (sub === "board-audit" || sub === "board-row"))) { const command = [top, sub].filter((value): value is string => value !== undefined).join(" ") || "gh"; - return validationError(command, options.repo, "--board-issue, --known-meta-issue, and --ignore-issue are only supported by gh issue board-audit"); + return validationError(command, options.repo, "--board-issue, --known-meta-issue, and --ignore-issue are only supported by gh issue board-audit and --board-issue by gh issue board-row"); + } + if ((optionWasProvided(args, "--known-meta-issue") || optionWasProvided(args, "--ignore-issue")) && !(top === "issue" && sub === "board-audit")) { + const command = [top, sub, third].filter((value): value is string => value !== undefined).join(" ") || "gh"; + return validationError(command, options.repo, "--known-meta-issue and --ignore-issue are only supported by gh issue board-audit"); + } + if ((optionWasProvided(args, "--field") || optionWasProvided(args, "--value")) && !(top === "issue" && sub === "board-row" && third === "update")) { + const command = [top, sub, third].filter((value): value is string => value !== undefined).join(" ") || "gh"; + return validationError(command, options.repo, "--field and --value are only supported by gh issue board-row update"); } if (optionWasProvided(args, "--label") && !(top === "issue" && sub === "create")) { const command = [top, sub].filter((value): value is string => value !== undefined).join(" ") || "gh"; @@ -3036,6 +3456,33 @@ export async function runGhCommand(args: string[]): Promise<GitHubCommandResult if (missing !== null || token === null) return missing ?? authRequired(options.repo, "issue board-audit", { present: false, source: null, ghFallbackAttempted: true }); return issueBoardAudit(options.repo, token, options); } + if (sub === "board-row") { + const action = third; + const commandName = `issue board-row ${action ?? ""}`.trim(); + if (action === "move") { + return unsupportedCommand(commandName, options.repo, "board-row move is deferred in the first phase; open/closed row relocation needs explicit destination-section insertion rules, duplicate-row handling, and audit-safe closed/open semantics before it can write.", { + boardIssue: options.boardIssue, + boundary: "row list/get/update are supported; move between OPEN/CLOSED tables remains unsupported and performs no write", + }); + } + if (action === "delete") { + return unsupportedCommand(commandName, options.repo, "board-row delete is deferred in the first phase; row removal needs an explicit archival/delete policy and duplicate-row guard before it can write.", { + boardIssue: options.boardIssue, + boundary: "row list/get/update are supported; delete remains unsupported and performs no write", + }); + } + if (action !== "list" && action !== "get" && action !== "update") { + return unsupportedCommand(commandName, options.repo, "board-row supported commands are list, get, update, and structured unsupported move/delete."); + } + const { token, probe } = resolveToken(true); + const missing = authRequired(options.repo, commandName, probe); + if (missing !== null || token === null) return missing ?? authRequired(options.repo, commandName, { present: false, source: null, ghFallbackAttempted: true }); + if (action === "list") return issueBoardRowList(options.repo, token, options); + const issueNumber = parseNumberForCommand(options.repo, args[3], commandName); + if (typeof issueNumber !== "number") return issueNumber; + if (action === "get") return issueBoardRowGet(options.repo, token, issueNumber, options); + return issueBoardRowUpdate(options.repo, token, issueNumber, options); + } if (options.dryRun) { if (sub === "create") return issueCreate(options.repo, "", options); if (sub === "edit") {