@fitlab-ai/agent-infra 0.8.2 → 0.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/dist/bin/cli.js +1 -1
  2. package/dist/lib/cp.js +1 -1
  3. package/dist/lib/decide.js +2 -2
  4. package/dist/lib/defaults.json +1 -0
  5. package/dist/lib/init.js +7 -7
  6. package/dist/lib/merge.js +1 -1
  7. package/dist/lib/prompt.js +1 -1
  8. package/dist/lib/run/index.js +7 -7
  9. package/dist/lib/run/prompt.js +1 -1
  10. package/dist/lib/sandbox/capture.js +5 -5
  11. package/dist/lib/sandbox/clipboard/bridge.js +48 -13
  12. package/dist/lib/sandbox/clipboard/index.js +5 -6
  13. package/dist/lib/sandbox/clipboard/linux.js +91 -0
  14. package/dist/lib/sandbox/clipboard/paths.js +1 -1
  15. package/dist/lib/sandbox/clipboard/win32.js +144 -0
  16. package/dist/lib/sandbox/commands/create.js +67 -65
  17. package/dist/lib/sandbox/commands/enter.js +9 -9
  18. package/dist/lib/sandbox/commands/list-running.js +2 -2
  19. package/dist/lib/sandbox/commands/ls.js +7 -7
  20. package/dist/lib/sandbox/commands/prune.js +8 -8
  21. package/dist/lib/sandbox/commands/rebuild.js +38 -50
  22. package/dist/lib/sandbox/commands/refresh.js +2 -2
  23. package/dist/lib/sandbox/commands/rm.js +11 -11
  24. package/dist/lib/sandbox/commands/show.js +4 -4
  25. package/dist/lib/sandbox/commands/start.js +4 -4
  26. package/dist/lib/sandbox/commands/vm.js +4 -4
  27. package/dist/lib/sandbox/config.js +10 -4
  28. package/dist/lib/sandbox/constants.js +4 -1
  29. package/dist/lib/sandbox/credentials.js +1 -1
  30. package/dist/lib/sandbox/dotfiles.js +1 -1
  31. package/dist/lib/sandbox/engine.js +3 -3
  32. package/dist/lib/sandbox/engines/index.js +5 -5
  33. package/dist/lib/sandbox/engines/wsl2-paths.js +1 -1
  34. package/dist/lib/sandbox/image-build.js +80 -0
  35. package/dist/lib/sandbox/image-prune.js +2 -2
  36. package/dist/lib/sandbox/index.js +10 -10
  37. package/dist/lib/sandbox/managed-fs.js +1 -1
  38. package/dist/lib/sandbox/readme-scaffold.js +1 -1
  39. package/dist/lib/sandbox/tools.js +2 -2
  40. package/dist/lib/server/adapters/feishu/index.js +10 -3
  41. package/dist/lib/server/adapters/feishu/renderer.js +88 -0
  42. package/dist/lib/server/adapters/feishu/transport.js +2 -13
  43. package/dist/lib/server/daemon.js +64 -43
  44. package/dist/lib/server/display.js +83 -0
  45. package/dist/lib/server/index.js +2 -2
  46. package/dist/lib/server/process-control.js +2 -2
  47. package/dist/lib/server/protocol.js +2 -2
  48. package/dist/lib/server/streamer.js +5 -4
  49. package/dist/lib/task/commands/cat.js +2 -2
  50. package/dist/lib/task/commands/decisions.js +4 -4
  51. package/dist/lib/task/commands/files.js +3 -3
  52. package/dist/lib/task/commands/grep.js +3 -3
  53. package/dist/lib/task/commands/issue-body.js +4 -4
  54. package/dist/lib/task/commands/log.js +5 -5
  55. package/dist/lib/task/commands/ls.js +3 -3
  56. package/dist/lib/task/commands/show.js +1 -1
  57. package/dist/lib/task/commands/status.js +71 -25
  58. package/dist/lib/task/index.js +9 -9
  59. package/dist/lib/task/resolve-ref.js +1 -1
  60. package/dist/lib/task/workflow-warnings.js +94 -0
  61. package/dist/lib/update.js +4 -4
  62. package/lib/defaults.json +1 -0
  63. package/lib/sandbox/clipboard/bridge.ts +50 -8
  64. package/lib/sandbox/clipboard/index.ts +5 -6
  65. package/lib/sandbox/clipboard/linux.ts +124 -0
  66. package/lib/sandbox/clipboard/win32.ts +173 -0
  67. package/lib/sandbox/commands/create.ts +84 -64
  68. package/lib/sandbox/commands/rebuild.ts +42 -54
  69. package/lib/sandbox/config.ts +13 -1
  70. package/lib/sandbox/constants.ts +4 -0
  71. package/lib/sandbox/image-build.ts +134 -0
  72. package/lib/server/adapters/_contract.ts +3 -0
  73. package/lib/server/adapters/feishu/index.ts +11 -3
  74. package/lib/server/adapters/feishu/renderer.ts +99 -0
  75. package/lib/server/adapters/feishu/transport.ts +5 -18
  76. package/lib/server/daemon.ts +74 -38
  77. package/lib/server/display.ts +136 -0
  78. package/lib/server/streamer.ts +5 -4
  79. package/lib/task/commands/log.ts +3 -3
  80. package/lib/task/commands/status.ts +102 -21
  81. package/lib/task/workflow-warnings.ts +121 -0
  82. package/package.json +2 -2
  83. package/templates/.agents/rules/create-issue.github.en.md +3 -3
  84. package/templates/.agents/rules/create-issue.github.zh-CN.md +3 -3
  85. package/templates/.agents/rules/issue-pr-commands.github.en.md +7 -2
  86. package/templates/.agents/rules/issue-pr-commands.github.zh-CN.md +7 -2
  87. package/templates/.agents/rules/issue-sync.github.en.md +13 -0
  88. package/templates/.agents/rules/issue-sync.github.zh-CN.md +13 -0
  89. package/templates/.agents/rules/next-step-output.en.md +15 -1
  90. package/templates/.agents/rules/next-step-output.zh-CN.md +15 -1
  91. package/templates/.agents/rules/pr-sync.github.en.md +17 -2
  92. package/templates/.agents/rules/pr-sync.github.zh-CN.md +17 -2
  93. package/templates/.agents/rules/task-management.en.md +1 -1
  94. package/templates/.agents/rules/task-management.zh-CN.md +1 -1
  95. package/templates/.agents/scripts/platform-adapters/platform-sync.github.js +57 -0
  96. package/templates/.agents/scripts/validate-artifact.js +120 -0
  97. package/templates/.agents/scripts/workflow-warnings.js +290 -0
  98. package/templates/.agents/skills/complete-manual-validation/SKILL.en.md +118 -0
  99. package/templates/.agents/skills/complete-manual-validation/SKILL.zh-CN.md +118 -0
  100. package/templates/.agents/skills/complete-manual-validation/config/verify.en.json +49 -0
  101. package/templates/.agents/skills/complete-manual-validation/config/verify.zh-CN.json +49 -0
  102. package/templates/.agents/skills/complete-manual-validation/reference/report-template.en.md +48 -0
  103. package/templates/.agents/skills/complete-manual-validation/reference/report-template.zh-CN.md +48 -0
  104. package/templates/.agents/skills/complete-manual-validation/reference/summary-update.en.md +60 -0
  105. package/templates/.agents/skills/complete-manual-validation/reference/summary-update.zh-CN.md +87 -0
  106. package/templates/.agents/skills/create-pr/SKILL.en.md +2 -0
  107. package/templates/.agents/skills/create-pr/SKILL.zh-CN.md +2 -0
  108. package/templates/.agents/skills/create-task/SKILL.en.md +4 -1
  109. package/templates/.agents/skills/create-task/SKILL.zh-CN.md +4 -1
  110. package/templates/.agents/skills/update-agent-infra/scripts/sync-templates.js +7 -0
  111. package/templates/.agents/templates/task.en.md +7 -0
  112. package/templates/.agents/templates/task.zh-CN.md +7 -0
  113. package/templates/.claude/commands/complete-manual-validation.en.md +9 -0
  114. package/templates/.claude/commands/complete-manual-validation.zh-CN.md +9 -0
  115. package/templates/.gemini/commands/_project_/complete-manual-validation.en.toml +8 -0
  116. package/templates/.gemini/commands/_project_/complete-manual-validation.zh-CN.toml +8 -0
  117. package/templates/.github/workflows/metadata-sync.yml +5 -0
  118. package/templates/.opencode/commands/complete-manual-validation.en.md +11 -0
  119. package/templates/.opencode/commands/complete-manual-validation.zh-CN.md +11 -0
@@ -7,6 +7,7 @@
7
7
  当前调用方:
8
8
  - `create-pr`:首次创建或更新 PR 摘要评论
9
9
  - `commit`:在已有 PR 上按需刷新摘要评论
10
+ - `complete-manual-validation`:人工验证完成后原地更新摘要评论,并写入可被后续聚合复用的验证产物
10
11
 
11
12
  如果后续 skill 也需要刷新 PR 摘要,先在对应 skill 的 reference 中引用本 rule,再补充该 skill 自身的触发条件和失败语义。
12
13
 
@@ -26,17 +27,22 @@
26
27
  - `plan.md` 或最新 `plan-r{N}.md`
27
28
  - `code.md` 或最新 `code-r{N}.md`
28
29
  - `review-code.md` 或最新 `review-code-r{N}.md`
30
+ - `manual-validation.md` 或最新 `manual-validation-r{N}.md`
29
31
 
30
32
  聚合规则:
31
33
  - 从 `plan*` 提取 2-4 条自包含的关键技术决策
32
34
  - 用 `review-code*` 与 `code*` 构建审查历程表
33
35
  - 从 `code*` 提取测试结果摘要
36
+ - 如果最新 `manual-validation*` 产物结论为通过,需人工校验段落优先渲染为 `### ✅ 人工验证已通过`,并摘要验证时间、验证范围和验证说明
34
37
  - 某一类产物缺失时,按“无该阶段数据”处理并继续生成
35
38
  - 需人工校验段落:只收进入 code 阶段后仍需人实际执行或判断、AI 无法自行关闭的校验点。
36
39
  - **准入边界**:校验结论依赖真实环境、权限、账号、外部系统或人工判断,且无法通过 agent 重跑测试、补充检查或继续修复自行关闭。
37
40
  - **来源**:`review-code*` 的「人工校验项」,以及 `code*` 中满足上述边界的校验点。
38
41
  - **写法**:每条保留项至少写明「校验什么 + 定位(文件/改动/范围)+ 为什么只能由人校验」。
39
- - **空集渲染**:无保留项时,不要使用 ⚠️ 告警样式(会让人误以为有问题)。整段降级渲染为:标题 `### ✅ 无需人工校验`,正文一行 `本次改动无需人工确认事项。`,不带条目列表。有保留项时才用 `### ⚠️ 需人工校验` 标题 + 条目列表。
42
+ - **渲染优先级**:
43
+ 1. 最新 `manual-validation*` 产物结论为通过 -> 标题 `### ✅ 人工验证已通过` + 验证摘要。
44
+ 2. 没有通过产物且有保留项 -> 标题 `### ⚠️ 需人工校验` + 条目列表。
45
+ 3. 没有通过产物且无保留项 -> 标题 `### ✅ 无需人工校验`,正文一行 `本次改动无需人工确认事项。`,不带条目列表。
40
46
 
41
47
  ## 评论体模板
42
48
 
@@ -72,7 +78,7 @@
72
78
  *由 {agent} 自动生成 · 内部追踪:{task-id}*
73
79
  ```
74
80
 
75
- > `{manual-validation-section}` 按上文「需人工校验段落」聚合规则渲染:有保留项 → `### ⚠️ 需人工校验` 标题 + 引用说明 + 条目列表;无保留项 → `### ✅ 无需人工校验` 标题 + 一行 `本次改动无需人工确认事项。`(不带 ⚠️、不带列表)。
81
+ > `{manual-validation-section}` 按上文「需人工校验段落」聚合规则渲染:已通过产物 → `### ✅ 人工验证已通过` 标题 + 验证摘要;有保留项 → `### ⚠️ 需人工校验` 标题 + 引用说明 + 条目列表;无保留项 → `### ✅ 无需人工校验` 标题 + 一行 `本次改动无需人工确认事项。`(不带 ⚠️、不带列表)。
76
82
 
77
83
  ## 评论查找与更新
78
84
 
@@ -111,6 +117,15 @@ EOF
111
117
  | `gh api` GET/PATCH/POST 失败 | 输出警告并继续;是否阻塞当前 skill 由调用方决定 |
112
118
  | `pr_number` 指向的 PR 不存在 | 输出 `PR #{pr-number} not found` 警告并继续 |
113
119
 
120
+ 当调用方存在 `{task-id}` / task 目录且 GET/PATCH/POST 失败时,记录 Workflow Warning:
121
+
122
+ ```bash
123
+ node .agents/scripts/workflow-warnings.js add .agents/workspace/active/{task-id} \
124
+ --step pr-sync --severity ACTION_REQUIRED --code COMMENT_SYNC_FAILED \
125
+ --target "pr-summary" --message "{reason}" \
126
+ --action "修复 GitHub API / 网络问题后重跑触发 PR 摘要同步的 workflow 步骤"
127
+ ```
128
+
114
129
  ## 结果回传
115
130
 
116
131
  统一回传以下结果之一,供调用方在 Activity Log 或用户输出中复用:
@@ -52,7 +52,7 @@ Map user intent to the corresponding workflow command:
52
52
 
53
53
  **Pairing and rendering** (`ai task log`): a started entry pairs with the next same-`{base}` done entry onto one row (repeated executions of the same base pair FIFO by ascending time). The STARTED column shows the start time, DONE the completion time; started with no done = in progress (DONE shows `(in progress)`); done with no started (legacy logs) = a standalone completed row. All three shapes are valid and never error.
54
54
 
55
- **Human counts** (`ai task log`): review step rows fold two human counts into the NOTE verdict text, comma-separated, right after `blockers/major/minor`, with fixed English labels `Manual-validation: {e}, Human-decision: {h}`. `Human-decision` (`{h}`) maps canonical step prefixes (`Review Analysis` / `Review Plan` / `Review Code`) to `analysis` / `plan` / `code`, then counts current rows in `## Review Disagreement Ledger` / `## 审查分歧账本` for that stage with `status {needs-human-decision, human-decided}`. `Manual-validation` (`{e}`) parses the `Manual-validation: {n}` source field from the review done note (normalized to the same field when displayed), defaulting to `0`. Non-review rows carry no human counts.
55
+ **Human counts** (`ai task log`): review step rows fold two human counts into the NOTE verdict text, comma-separated, right after `blockers/major/minor`, with fixed English labels `Manual-validation: {e}, Human-decision: {h}`. `Human-decision` (`{h}`) maps canonical step prefixes (`Review Analysis` / `Review Plan` / `Review Code`) to `analysis` / `plan` / `code`, then counts current pending rows in `## Review Disagreement Ledger` / `## 审查分歧账本` for that stage with `status = needs-human-decision`, excluding rows already marked `human-decided`. `Manual-validation` (`{e}`) parses the `Manual-validation: {n}` source field from the review done note (normalized to the same field when displayed), defaulting to `0`. Non-review rows carry no human counts.
56
56
 
57
57
  **Gate** (`checkActivityLog`): when computing the "latest action / freshness" it skips `[started]` lines (ascending-order and format checks still cover every line), so a started marker never satisfies a skill's `expected_action_pattern`.
58
58
 
@@ -56,7 +56,7 @@
56
56
 
57
57
  **配对与渲染**(`ai task log`):按 `{基名}` 把 started 与其后最近的同名 done 配成一行(同基名多次执行按时间升序 FIFO 配对)。STARTED 列显示 started 时间、DONE 列显示 done 时间;只有 started 无 done = 进行中(DONE 显示 `(in progress)`);只有 done 无 started(历史日志)= 单态完成行。三种形态都合法、不报错。
58
58
 
59
- **人工计数**(`ai task log`):审查步骤行把两项人工计数并入 NOTE 的 verdict 文本,逗号分隔、紧随 `blockers/major/minor`,固定英文标签 `Manual-validation: {e}, Human-decision: {h}`。`Human-decision`(`{h}`)按 canonical 步骤名前缀(`Review Analysis` / `Review Plan` / `Review Code`)映射到 `analysis` / `plan` / `code`,统计 `## 审查分歧账本` 中对应阶段 `status {needs-human-decision, human-decided}` 的当前累计数量;`Manual-validation`(`{e}`)解析 review done note 中的 `Manual-validation: {n}` 源字段(展示时归一化为同一字段),缺失为 `0`。非审查步骤不附加人工计数。
59
+ **人工计数**(`ai task log`):审查步骤行把两项人工计数并入 NOTE 的 verdict 文本,逗号分隔、紧随 `blockers/major/minor`,固定英文标签 `Manual-validation: {e}, Human-decision: {h}`。`Human-decision`(`{h}`)按 canonical 步骤名前缀(`Review Analysis` / `Review Plan` / `Review Code`)映射到 `analysis` / `plan` / `code`,统计 `## 审查分歧账本` 中对应阶段 `status = needs-human-decision` 的当前待裁决数量,不包含已 `human-decided` 的行;`Manual-validation`(`{e}`)解析 review done note 中的 `Manual-validation: {n}` 源字段(展示时归一化为同一字段),缺失为 `0`。非审查步骤不附加人工计数。
60
60
 
61
61
  **gate**(`checkActivityLog`):计算「最新 action / freshness」时跳过 `[started]` 行(升序与格式校验仍覆盖全部行),故 started 标记不会污染各 SKILL 的 `expected_action_pattern`。
62
62
 
@@ -112,6 +112,7 @@ export function check({ taskDir, config, artifactFile }, shared) {
112
112
  checkCommentMarker,
113
113
  checkPrCommentMarker,
114
114
  checkPrCommentLastCommit,
115
+ checkPrCommentRequiredPatterns,
115
116
  checkCommentContent,
116
117
  checkTaskCommentContent,
117
118
  checkInLabelsComputed,
@@ -158,6 +159,7 @@ function buildSyncContext({ taskDir, config, artifactFile }) {
158
159
  return { earlyReturn: blockedResult(CHECK_TYPE, upstreamRepo.message, "network_error") };
159
160
  }
160
161
  const permissions = detectPermissions(upstreamRepo.value, taskDir);
162
+ const repoOwnerType = detectRepoOwnerType(upstreamRepo.value, taskDir);
161
163
  const expectedValues = resolveExpectedValues(config);
162
164
  if (!expectedValues.ok) {
163
165
  return { earlyReturn: failResult(CHECK_TYPE, expectedValues.message, "check_failed") };
@@ -180,6 +182,7 @@ function buildSyncContext({ taskDir, config, artifactFile }) {
180
182
  issueNumber,
181
183
  prNumber,
182
184
  upstreamRepo: upstreamRepo.value,
185
+ repoOwnerType,
183
186
  hasTriage: permissions.hasTriage,
184
187
  hasPush: permissions.hasPush,
185
188
  expectedStatusLabel: expectedValues.statusLabel,
@@ -553,6 +556,41 @@ function checkPrCommentLastCommit(context, remoteData) {
553
556
  );
554
557
  }
555
558
 
559
+ function checkPrCommentRequiredPatterns(context, remoteData) {
560
+ const patterns = context.config.expected_pr_comment_required_patterns || [];
561
+ if (!Array.isArray(patterns) || patterns.length === 0) {
562
+ return null;
563
+ }
564
+
565
+ if (!context.prMarker) {
566
+ return failResult(CHECK_TYPE,
567
+ "expected_pr_comment_required_patterns requires expected_pr_comment_marker",
568
+ "check_failed"
569
+ );
570
+ }
571
+
572
+ const comment = findCommentByMarker(remoteData.prComments, context.prMarker);
573
+ if (!comment) {
574
+ return failResult(CHECK_TYPE,
575
+ `Expected PR comment marker '${context.prMarker}' not found on PR #${context.prNumber}`,
576
+ "check_failed"
577
+ );
578
+ }
579
+
580
+ const body = String(comment.body || "");
581
+ for (const pattern of patterns) {
582
+ const regex = new RegExp(pattern, "m");
583
+ if (!regex.test(body)) {
584
+ return failResult(CHECK_TYPE,
585
+ `PR #${context.prNumber} summary comment is missing required pattern: ${pattern}`,
586
+ "check_failed"
587
+ );
588
+ }
589
+ }
590
+
591
+ return null;
592
+ }
593
+
556
594
  function checkCommentContent(context, remoteData) {
557
595
  if (!context.config.verify_comment_content) {
558
596
  return null;
@@ -722,6 +760,10 @@ function checkIssueType(context, remoteData) {
722
760
  }
723
761
 
724
762
  if (!remoteData.issueType) {
763
+ if (context.repoOwnerType === "User") {
764
+ return null;
765
+ }
766
+
725
767
  return failResult(CHECK_TYPE,
726
768
  `Issue #${context.issueNumber} has no Issue Type set`,
727
769
  "check_failed"
@@ -1228,6 +1270,21 @@ function detectPermissions(upstreamRepo, taskDir) {
1228
1270
  };
1229
1271
  }
1230
1272
 
1273
+ function detectRepoOwnerType(upstreamRepo, taskDir) {
1274
+ const ownerTypeResult = withRetry(() => ghText([
1275
+ "api",
1276
+ `repos/${upstreamRepo}`,
1277
+ "--jq",
1278
+ ".owner.type // empty"
1279
+ ], taskDir));
1280
+
1281
+ if (!ownerTypeResult.ok) {
1282
+ return "unknown";
1283
+ }
1284
+
1285
+ return ownerTypeResult.value || "unknown";
1286
+ }
1287
+
1231
1288
  function ghJson(args, cwd) {
1232
1289
  const result = ghCommand(args, cwd);
1233
1290
  if (!result.ok) {
@@ -61,6 +61,10 @@ const LEDGER_TERMINAL_OK = new Set(["confirmed", "closed", "human-decided"]);
61
61
  const DEFAULT_MAX_HANDSHAKE_ROUNDS = 3;
62
62
  const POST_REVIEW_COMMIT_STAGE = "post-review-commit";
63
63
  const SHA_PATTERN = /^[0-9a-f]{7,40}$/i;
64
+ const WORKFLOW_WARNING_SECTION_NAMES = ["工作流告警", "Workflow Warnings"];
65
+ const WORKFLOW_WARNING_STATUSES = new Set(["open", "resolved", "ignored"]);
66
+ const WORKFLOW_WARNING_SEVERITIES = new Set(["IMPORTANT", "ACTION_REQUIRED"]);
67
+ const WORKFLOW_WARNING_ID_PATTERN = /^WW-\d+$/;
64
68
 
65
69
  const scriptPath = fileURLToPath(import.meta.url);
66
70
  const repoRoot = path.resolve(path.dirname(scriptPath), "..", "..");
@@ -284,6 +288,11 @@ function checkTaskMeta({ taskDir, config }) {
284
288
  return failResult("task-meta", branchValidationError);
285
289
  }
286
290
 
291
+ const warningValidationErrors = validateWorkflowWarnings(task.content);
292
+ if (warningValidationErrors.length > 0) {
293
+ return failResult("task-meta", `Invalid Workflow Warnings: ${warningValidationErrors.join("; ")}`);
294
+ }
295
+
287
296
  const expectedStep = config.expected_step;
288
297
  if (expectedStep && metadata.current_step !== expectedStep) {
289
298
  return failResult(
@@ -561,6 +570,117 @@ function parseLedgerRows(section) {
561
570
  return rows;
562
571
  }
563
572
 
573
+ function splitMarkdownTableRow(line) {
574
+ let value = String(line || "").trim();
575
+ if (!value.startsWith("|")) {
576
+ return [];
577
+ }
578
+ value = value.replace(/^\|/, "").replace(/\|$/, "");
579
+
580
+ const cells = [];
581
+ let cell = "";
582
+ for (let index = 0; index < value.length; index += 1) {
583
+ const char = value[index];
584
+ if (char === "|" && !isEscapedAt(value, index)) {
585
+ cells.push(unescapeMarkdownTableCell(cell.trim()));
586
+ cell = "";
587
+ continue;
588
+ }
589
+ cell += char;
590
+ }
591
+ cells.push(unescapeMarkdownTableCell(cell.trim()));
592
+ return cells;
593
+ }
594
+
595
+ function unescapeMarkdownTableCell(value) {
596
+ let output = "";
597
+ for (let index = 0; index < value.length; index += 1) {
598
+ const char = value[index];
599
+ const next = value[index + 1];
600
+ if (char === "\\" && (next === "\\" || next === "|")) {
601
+ output += next;
602
+ index += 1;
603
+ continue;
604
+ }
605
+ output += char;
606
+ }
607
+ return output;
608
+ }
609
+
610
+ function isEscapedAt(value, index) {
611
+ let backslashes = 0;
612
+ for (let cursor = index - 1; cursor >= 0 && value[cursor] === "\\"; cursor -= 1) {
613
+ backslashes += 1;
614
+ }
615
+ return backslashes % 2 === 1;
616
+ }
617
+
618
+ function parseWorkflowWarningRows(section) {
619
+ const rows = [];
620
+ for (const rawLine of String(section || "").split(/\r?\n/)) {
621
+ const cells = splitMarkdownTableRow(rawLine);
622
+ if (cells.length === 0) {
623
+ continue;
624
+ }
625
+ if ((cells[0] || "").toLowerCase() === "id") {
626
+ continue;
627
+ }
628
+ if (cells.every((cell) => /^:?-{3,}:?$/.test(cell))) {
629
+ continue;
630
+ }
631
+ rows.push(cells);
632
+ }
633
+ return rows;
634
+ }
635
+
636
+ function validateWorkflowWarnings(content) {
637
+ const section = getSectionContent(content, WORKFLOW_WARNING_SECTION_NAMES);
638
+ if (!section.trim()) {
639
+ return [];
640
+ }
641
+
642
+ const rows = parseWorkflowWarningRows(section);
643
+ const errors = [];
644
+ for (const cells of rows) {
645
+ if (cells.length < 11) {
646
+ errors.push(`malformed row (expected 11 columns): ${cells.join(" | ")}`);
647
+ continue;
648
+ }
649
+ const [id, time, step, severity, code, status, target, message, action, resolvedAt, resolution] = cells;
650
+ if (!WORKFLOW_WARNING_ID_PATTERN.test(id)) {
651
+ errors.push(`${id || "(empty id)"}: invalid id`);
652
+ }
653
+ if (!DATE_TIME_PATTERN.test(time)) {
654
+ errors.push(`${id}: invalid time '${time}'`);
655
+ }
656
+ if (isBlank(step)) {
657
+ errors.push(`${id}: step is required`);
658
+ }
659
+ if (!WORKFLOW_WARNING_SEVERITIES.has(severity)) {
660
+ errors.push(`${id}: illegal severity '${severity}'`);
661
+ }
662
+ if (isBlank(code)) {
663
+ errors.push(`${id}: code is required`);
664
+ }
665
+ if (!WORKFLOW_WARNING_STATUSES.has(status)) {
666
+ errors.push(`${id}: illegal status '${status}'`);
667
+ }
668
+ if (isBlank(target)) {
669
+ errors.push(`${id}: target is required`);
670
+ }
671
+ if (isBlank(message)) {
672
+ errors.push(`${id}: message is required`);
673
+ }
674
+ if (status === "open" && isBlank(action)) {
675
+ errors.push(`${id}: open warning requires action`);
676
+ }
677
+ if ((status === "resolved" || status === "ignored") && (isBlank(resolvedAt) || isBlank(resolution))) {
678
+ errors.push(`${id}: ${status} warning requires resolved_at and resolution`);
679
+ }
680
+ }
681
+ return errors;
682
+ }
683
+
564
684
  function resolveReviewSetting(config, key, fallback) {
565
685
  if (config && config[key] !== undefined && config[key] !== null) {
566
686
  return config[key];
@@ -0,0 +1,290 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import process from "node:process";
4
+
5
+ const SECTION_ZH = "工作流告警";
6
+ const SECTION_EN = "Workflow Warnings";
7
+ const ACTIVITY_ZH = "活动日志";
8
+ const ACTIVITY_EN = "Activity Log";
9
+ const HEADER = "| id | time | step | severity | code | status | target | message | action | resolved_at | resolution |";
10
+ const SEPARATOR = "|----|------|------|----------|------|--------|--------|---------|--------|-------------|------------|";
11
+ const VALID_SEVERITIES = new Set(["IMPORTANT", "ACTION_REQUIRED"]);
12
+ const VALID_STATUSES = new Set(["open", "resolved", "ignored"]);
13
+
14
+ function usage() {
15
+ process.stderr.write([
16
+ "Usage:",
17
+ " node .agents/scripts/workflow-warnings.js add <task-dir> --step <step> --severity <IMPORTANT|ACTION_REQUIRED> --code <code> --target <target> --message <message> --action <action>",
18
+ " node .agents/scripts/workflow-warnings.js set-status <task-dir> --id <WW-N> --status <resolved|ignored> --resolution <reason>",
19
+ " node .agents/scripts/workflow-warnings.js list <task-dir> [--status <status>] [--format json|text]",
20
+ ""
21
+ ].join("\n"));
22
+ process.exit(2);
23
+ }
24
+
25
+ function parseOptions(args) {
26
+ const options = {};
27
+ for (let index = 0; index < args.length; index += 1) {
28
+ const key = args[index];
29
+ if (!key?.startsWith("--")) usage();
30
+ const value = args[index + 1];
31
+ if (value === undefined || value.startsWith("--")) usage();
32
+ options[key.slice(2)] = value;
33
+ index += 1;
34
+ }
35
+ return options;
36
+ }
37
+
38
+ function timestamp() {
39
+ const date = new Date();
40
+ const pad = (value) => String(value).padStart(2, "0");
41
+ const offsetMinutes = -date.getTimezoneOffset();
42
+ const sign = offsetMinutes >= 0 ? "+" : "-";
43
+ const absolute = Math.abs(offsetMinutes);
44
+ return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}${sign}${pad(Math.floor(absolute / 60))}:${pad(absolute % 60)}`;
45
+ }
46
+
47
+ function escapeCell(value) {
48
+ return String(value ?? "").replace(/\\/g, "\\\\").replace(/\|/g, "\\|").replace(/\r?\n/g, " ");
49
+ }
50
+
51
+ function unescapeCell(value) {
52
+ const text = String(value ?? "");
53
+ let output = "";
54
+ for (let index = 0; index < text.length; index += 1) {
55
+ const char = text[index];
56
+ const next = text[index + 1];
57
+ if (char === "\\" && (next === "\\" || next === "|")) {
58
+ output += next;
59
+ index += 1;
60
+ continue;
61
+ }
62
+ output += char;
63
+ }
64
+ return output;
65
+ }
66
+
67
+ function isEscapedAt(value, index) {
68
+ let backslashes = 0;
69
+ for (let cursor = index - 1; cursor >= 0 && value[cursor] === "\\"; cursor -= 1) {
70
+ backslashes += 1;
71
+ }
72
+ return backslashes % 2 === 1;
73
+ }
74
+
75
+ function splitRow(line) {
76
+ let value = String(line || "").trim();
77
+ if (!value.startsWith("|")) return [];
78
+ value = value.replace(/^\|/, "").replace(/\|$/, "");
79
+ const cells = [];
80
+ let cell = "";
81
+ for (let index = 0; index < value.length; index += 1) {
82
+ const char = value[index];
83
+ if (char === "|" && !isEscapedAt(value, index)) {
84
+ cells.push(unescapeCell(cell.trim()));
85
+ cell = "";
86
+ continue;
87
+ }
88
+ cell += char;
89
+ }
90
+ cells.push(unescapeCell(cell.trim()));
91
+ return cells;
92
+ }
93
+
94
+ function rowToWarning(cells, lineIndex) {
95
+ return {
96
+ lineIndex,
97
+ id: cells[0] || "",
98
+ time: cells[1] || "",
99
+ step: cells[2] || "",
100
+ severity: cells[3] || "",
101
+ code: cells[4] || "",
102
+ status: cells[5] || "",
103
+ target: cells[6] || "",
104
+ message: cells[7] || "",
105
+ action: cells[8] || "",
106
+ resolved_at: cells[9] || "",
107
+ resolution: cells[10] || ""
108
+ };
109
+ }
110
+
111
+ function warningToLine(warning) {
112
+ return [
113
+ warning.id,
114
+ warning.time,
115
+ warning.step,
116
+ warning.severity,
117
+ warning.code,
118
+ warning.status,
119
+ warning.target,
120
+ warning.message,
121
+ warning.action,
122
+ warning.resolved_at,
123
+ warning.resolution
124
+ ].map(escapeCell).join(" | ").replace(/^/, "| ").replace(/$/, " |");
125
+ }
126
+
127
+ function findSection(lines) {
128
+ let start = -1;
129
+ for (let index = 0; index < lines.length; index += 1) {
130
+ if (new RegExp(`^##\\s+(${SECTION_ZH}|${SECTION_EN})\\s*$`).test(lines[index].trim())) {
131
+ start = index;
132
+ break;
133
+ }
134
+ }
135
+ if (start === -1) return null;
136
+ let end = lines.length;
137
+ for (let index = start + 1; index < lines.length; index += 1) {
138
+ if (/^##\s+/.test(lines[index])) {
139
+ end = index;
140
+ break;
141
+ }
142
+ }
143
+ return { start, end };
144
+ }
145
+
146
+ function insertSection(content) {
147
+ const lines = content.split(/\r?\n/);
148
+ const existing = findSection(lines);
149
+ if (existing) return { content, section: existing };
150
+
151
+ const activityIndex = lines.findIndex((line) => {
152
+ const trimmed = line.trim();
153
+ return trimmed === `## ${ACTIVITY_ZH}` || trimmed === `## ${ACTIVITY_EN}`;
154
+ });
155
+ const heading = content.includes(`## ${ACTIVITY_EN}`) ? SECTION_EN : SECTION_ZH;
156
+ const block = [`## ${heading}`, "", "<!-- Workflow degradation, platform sync failures, permission gaps, and related events. Keep the header when empty. -->", "", HEADER, SEPARATOR, ""];
157
+ const insertAt = activityIndex >= 0 ? activityIndex : lines.length;
158
+ lines.splice(insertAt, 0, ...block);
159
+ const updated = lines.join("\n");
160
+ return { content: updated, section: findSection(updated.split(/\r?\n/)) };
161
+ }
162
+
163
+ function readTask(taskDir) {
164
+ const taskPath = path.join(taskDir, "task.md");
165
+ if (!fs.existsSync(taskPath)) {
166
+ throw new Error(`Task file not found: ${taskPath}`);
167
+ }
168
+ return { taskPath, content: fs.readFileSync(taskPath, "utf8") };
169
+ }
170
+
171
+ function parseWarnings(content) {
172
+ const lines = content.split(/\r?\n/);
173
+ const section = findSection(lines);
174
+ if (!section) return [];
175
+ const warnings = [];
176
+ for (let index = section.start + 1; index < section.end; index += 1) {
177
+ const cells = splitRow(lines[index]);
178
+ if (cells.length === 0) continue;
179
+ if ((cells[0] || "").toLowerCase() === "id") continue;
180
+ if (cells.every((cell) => /^:?-{3,}:?$/.test(cell))) continue;
181
+ if (cells.length < 11) continue;
182
+ warnings.push(rowToWarning(cells, index));
183
+ }
184
+ return warnings;
185
+ }
186
+
187
+ function nextId(warnings) {
188
+ const max = warnings.reduce((current, warning) => {
189
+ const match = /^WW-(\d+)$/.exec(warning.id);
190
+ return match ? Math.max(current, Number.parseInt(match[1], 10)) : current;
191
+ }, 0);
192
+ return `WW-${max + 1}`;
193
+ }
194
+
195
+ function requireOption(options, key) {
196
+ const value = options[key];
197
+ if (!value) usage();
198
+ return value;
199
+ }
200
+
201
+ function add(taskDir, options) {
202
+ const severity = requireOption(options, "severity");
203
+ if (!VALID_SEVERITIES.has(severity)) throw new Error(`Invalid severity: ${severity}`);
204
+ const step = requireOption(options, "step");
205
+ const code = requireOption(options, "code");
206
+ const target = requireOption(options, "target");
207
+ const message = requireOption(options, "message");
208
+ const action = requireOption(options, "action");
209
+
210
+ const task = readTask(taskDir);
211
+ const inserted = insertSection(task.content);
212
+ const lines = inserted.content.split(/\r?\n/);
213
+ const warnings = parseWarnings(inserted.content);
214
+ const duplicate = warnings.find((warning) =>
215
+ warning.status === "open" &&
216
+ warning.step === step &&
217
+ warning.code === code &&
218
+ warning.target === target
219
+ );
220
+ if (duplicate) {
221
+ process.stdout.write(`${JSON.stringify({ created: false, warning: duplicate }, null, 2)}\n`);
222
+ if (inserted.content !== task.content) fs.writeFileSync(task.taskPath, inserted.content, "utf8");
223
+ return;
224
+ }
225
+
226
+ const warning = {
227
+ id: nextId(warnings),
228
+ time: timestamp(),
229
+ step,
230
+ severity,
231
+ code,
232
+ status: "open",
233
+ target,
234
+ message,
235
+ action,
236
+ resolved_at: "",
237
+ resolution: ""
238
+ };
239
+ lines.splice(inserted.section.end - 1, 0, warningToLine(warning));
240
+ fs.writeFileSync(task.taskPath, lines.join("\n"), "utf8");
241
+ process.stdout.write(`${JSON.stringify({ created: true, warning }, null, 2)}\n`);
242
+ }
243
+
244
+ function setStatus(taskDir, options) {
245
+ const id = requireOption(options, "id");
246
+ const status = requireOption(options, "status");
247
+ if (!["resolved", "ignored"].includes(status)) throw new Error(`Invalid status for set-status: ${status}`);
248
+ const resolution = requireOption(options, "resolution");
249
+ const task = readTask(taskDir);
250
+ const lines = task.content.split(/\r?\n/);
251
+ const warnings = parseWarnings(task.content);
252
+ const warning = warnings.find((candidate) => candidate.id === id);
253
+ if (!warning) throw new Error(`Warning not found: ${id}`);
254
+ warning.status = status;
255
+ warning.resolved_at = timestamp();
256
+ warning.resolution = resolution;
257
+ lines[warning.lineIndex] = warningToLine(warning);
258
+ fs.writeFileSync(task.taskPath, lines.join("\n"), "utf8");
259
+ process.stdout.write(`${JSON.stringify({ updated: true, warning }, null, 2)}\n`);
260
+ }
261
+
262
+ function list(taskDir, options) {
263
+ const format = options.format || "text";
264
+ const status = options.status || "";
265
+ if (format !== "json" && format !== "text") usage();
266
+ const warnings = parseWarnings(readTask(taskDir).content)
267
+ .filter((warning) => !status || warning.status === status)
268
+ .map(({ lineIndex: _lineIndex, ...warning }) => warning);
269
+ if (format === "json") {
270
+ process.stdout.write(`${JSON.stringify({ warnings }, null, 2)}\n`);
271
+ return;
272
+ }
273
+ for (const warning of warnings) {
274
+ process.stdout.write(`${warning.id} [${warning.severity}] ${warning.code} ${warning.target} - ${warning.action}\n`);
275
+ }
276
+ }
277
+
278
+ try {
279
+ const [command, taskDirArg, ...rest] = process.argv.slice(2);
280
+ if (!command || !taskDirArg) usage();
281
+ const taskDir = path.resolve(taskDirArg);
282
+ const options = parseOptions(rest);
283
+ if (command === "add") add(taskDir, options);
284
+ else if (command === "set-status") setStatus(taskDir, options);
285
+ else if (command === "list") list(taskDir, options);
286
+ else usage();
287
+ } catch (error) {
288
+ process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
289
+ process.exit(1);
290
+ }