@pzy560117/codex-harness 0.1.3 → 0.1.5

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 (386) hide show
  1. package/README.md +164 -27
  2. package/bin/harness.js +7 -7
  3. package/lib/commands/doctor.js +12 -12
  4. package/lib/commands/init.js +105 -105
  5. package/lib/commands/run.js +22 -22
  6. package/lib/commands/verify.js +12 -12
  7. package/lib/main.js +62 -62
  8. package/lib/powershell/find-powershell.js +20 -20
  9. package/lib/powershell/invoke-script.js +34 -34
  10. package/lib/project/assert-initialized.js +20 -20
  11. package/lib/project/detect-project-root.js +5 -5
  12. package/lib/release/cache-layout.js +34 -34
  13. package/lib/release/download-release.js +25 -25
  14. package/lib/release/package-source-layout.js +13 -13
  15. package/lib/release/release-config.js +6 -6
  16. package/lib/release/release-manifest.js +76 -76
  17. package/lib/release/resolve-package-source.js +21 -21
  18. package/lib/release/resolve-version.js +26 -26
  19. package/lib/release/unpack-zip.js +25 -25
  20. package/lib/release/verify-sha256.js +16 -16
  21. package/package-source/AGENTS.md +48 -57
  22. package/package-source/PACKAGE.md +7 -7
  23. package/package-source/README.md +85 -81
  24. package/package-source/docs/codex-harness-engineering/templates/AGENTS.md +51 -0
  25. package/package-source/docs/codex-harness-engineering/templates/README.md +21 -20
  26. package/package-source/docs/codex-harness-engineering/templates/bootstrap-codex-harness.ps1 +144 -110
  27. package/package-source/docs/codex-harness-engineering/templates/ci/AGENTS.md +15 -0
  28. package/package-source/docs/codex-harness-engineering/templates/ci/github-action-codex-review.yml +110 -110
  29. package/package-source/docs/codex-harness-engineering/templates/config/AGENTS.md +19 -0
  30. package/package-source/docs/codex-harness-engineering/templates/config/agents/architect.toml +18 -17
  31. package/package-source/docs/codex-harness-engineering/templates/config/agents/backend-worker.toml +15 -14
  32. package/package-source/docs/codex-harness-engineering/templates/config/agents/docs-researcher.toml +13 -12
  33. package/package-source/docs/codex-harness-engineering/templates/config/agents/docs-worker.toml +16 -15
  34. package/package-source/docs/codex-harness-engineering/templates/config/agents/explorer.toml +15 -14
  35. package/package-source/docs/codex-harness-engineering/templates/config/agents/failure-triage.toml +19 -18
  36. package/package-source/docs/codex-harness-engineering/templates/config/agents/frontend-worker.toml +15 -14
  37. package/package-source/docs/codex-harness-engineering/templates/config/agents/harness-writer.toml +16 -15
  38. package/package-source/docs/codex-harness-engineering/templates/config/agents/planner.toml +16 -15
  39. package/package-source/docs/codex-harness-engineering/templates/config/agents/readonly-research.toml +14 -13
  40. package/package-source/docs/codex-harness-engineering/templates/config/agents/reviewer.toml +13 -12
  41. package/package-source/docs/codex-harness-engineering/templates/config/agents/security-reviewer.toml +16 -15
  42. package/package-source/docs/codex-harness-engineering/templates/config/agents/stage1-reviewer.toml +15 -14
  43. package/package-source/docs/codex-harness-engineering/templates/config/agents/stage2-reviewer.toml +16 -15
  44. package/package-source/docs/codex-harness-engineering/templates/config/agents/test-planner.toml +18 -17
  45. package/package-source/docs/codex-harness-engineering/templates/config/agents/test-runner.toml +15 -14
  46. package/package-source/docs/codex-harness-engineering/templates/config/agents/visual-reviewer.toml +16 -15
  47. package/package-source/docs/codex-harness-engineering/templates/config/codex-agent-roles.md +24 -24
  48. package/package-source/docs/codex-harness-engineering/templates/config/codex-config.toml +12 -12
  49. package/package-source/docs/codex-harness-engineering/templates/config/codex-readme.md +6 -6
  50. package/package-source/docs/codex-harness-engineering/templates/config/env-check.ps1 +44 -40
  51. package/package-source/docs/codex-harness-engineering/templates/config/env.example +13 -12
  52. package/package-source/docs/codex-harness-engineering/templates/config/global-AGENTS.md +40 -40
  53. package/package-source/docs/codex-harness-engineering/templates/config/global-config.toml +19 -19
  54. package/package-source/docs/codex-harness-engineering/templates/config/rules/agents.md +118 -115
  55. package/package-source/docs/codex-harness-engineering/templates/config/rules/coding-style.md +57 -74
  56. package/package-source/docs/codex-harness-engineering/templates/config/rules/constitution.md +4 -4
  57. package/package-source/docs/codex-harness-engineering/templates/config/rules/git.rules +41 -41
  58. package/package-source/docs/codex-harness-engineering/templates/config/rules/harness.rules +29 -29
  59. package/package-source/docs/codex-harness-engineering/templates/config/rules/safety.rules +35 -35
  60. package/package-source/docs/codex-harness-engineering/templates/context/AGENTS.md +19 -0
  61. package/package-source/docs/codex-harness-engineering/templates/context/API_MAP.md +18 -0
  62. package/package-source/docs/codex-harness-engineering/templates/context/CHANGELOG_AI.md +11 -0
  63. package/package-source/docs/codex-harness-engineering/templates/context/CURRENT_TASK.md +114 -0
  64. package/package-source/docs/codex-harness-engineering/templates/context/DB_SCHEMA.md +14 -0
  65. package/package-source/docs/codex-harness-engineering/templates/context/DECISIONS.md +12 -0
  66. package/package-source/docs/codex-harness-engineering/templates/context/KNOWN_ISSUES.md +12 -0
  67. package/package-source/docs/codex-harness-engineering/templates/context/PROJECT_CONTEXT.md +14 -0
  68. package/package-source/docs/codex-harness-engineering/templates/context/architecture-brief.md +58 -58
  69. package/package-source/docs/codex-harness-engineering/templates/context/dev-plan.md +89 -83
  70. package/package-source/docs/codex-harness-engineering/templates/context/feature-pack.md +101 -101
  71. package/package-source/docs/codex-harness-engineering/templates/context/repo-map.md +102 -78
  72. package/package-source/docs/codex-harness-engineering/templates/context/service-dependency-matrix.yaml +25 -25
  73. package/package-source/docs/codex-harness-engineering/templates/contracts/AGENTS.md +17 -0
  74. package/package-source/docs/codex-harness-engineering/templates/contracts/README.md +24 -16
  75. package/package-source/docs/codex-harness-engineering/templates/contracts/openapi.yaml +182 -182
  76. package/package-source/docs/codex-harness-engineering/templates/contracts/orval.config.ts +20 -20
  77. package/package-source/docs/codex-harness-engineering/templates/contracts/prism-usage.md +41 -34
  78. package/package-source/docs/codex-harness-engineering/templates/design/AGENTS.md +21 -0
  79. package/package-source/docs/codex-harness-engineering/templates/design/ai-image-brief.md +122 -122
  80. package/package-source/docs/codex-harness-engineering/templates/design/component-map.md +45 -31
  81. package/package-source/docs/codex-harness-engineering/templates/design/design-brief.md +183 -178
  82. package/package-source/docs/codex-harness-engineering/templates/design/design-tokens.json +88 -88
  83. package/package-source/docs/codex-harness-engineering/templates/design/frontend-architecture.md +164 -146
  84. package/package-source/docs/codex-harness-engineering/templates/design/image-to-frontend-spec.md +64 -52
  85. package/package-source/docs/codex-harness-engineering/templates/design/screen-states.md +157 -122
  86. package/package-source/docs/codex-harness-engineering/templates/design/visual-parity-review.md +21 -21
  87. package/package-source/docs/codex-harness-engineering/templates/docs/AGENTS.md +19 -0
  88. package/package-source/docs/codex-harness-engineering/templates/docs/architecture-constraints.md +97 -83
  89. package/package-source/docs/codex-harness-engineering/templates/docs/code-semantics-and-navigation.md +54 -0
  90. package/package-source/docs/codex-harness-engineering/templates/docs/code-style-and-naming.md +116 -0
  91. package/package-source/docs/codex-harness-engineering/templates/docs/directory-structure-template.md +88 -0
  92. package/package-source/docs/codex-harness-engineering/templates/docs/env-and-deployment-template.md +60 -0
  93. package/package-source/docs/codex-harness-engineering/templates/docs/frontend-quality-rules.md +165 -138
  94. package/package-source/docs/codex-harness-engineering/templates/docs/governance-auto-repair.md +82 -80
  95. package/package-source/docs/codex-harness-engineering/templates/docs/harness-architecture.md +78 -78
  96. package/package-source/docs/codex-harness-engineering/templates/docs/install-manifest-governance.md +16 -16
  97. package/package-source/docs/codex-harness-engineering/templates/docs/knowledge-architecture.md +241 -219
  98. package/package-source/docs/codex-harness-engineering/templates/docs/knowledge-import.md +108 -108
  99. package/package-source/docs/codex-harness-engineering/templates/docs/knowledge-lint.md +98 -98
  100. package/package-source/docs/codex-harness-engineering/templates/docs/mcp-knowledge-governance.md +24 -0
  101. package/package-source/docs/codex-harness-engineering/templates/docs/new-project-checklist.md +7 -7
  102. package/package-source/docs/codex-harness-engineering/templates/docs/new-project-usage.md +107 -43
  103. package/package-source/docs/codex-harness-engineering/templates/docs/project-agents-template.md +211 -154
  104. package/package-source/docs/codex-harness-engineering/templates/docs/prompt-knowledge-integration.md +100 -89
  105. package/package-source/docs/codex-harness-engineering/templates/docs/regression-rules.md +47 -45
  106. package/package-source/docs/codex-harness-engineering/templates/docs/requirement-prep-kit/README.md +3 -3
  107. package/package-source/docs/codex-harness-engineering/templates/docs/rule-governance.md +98 -98
  108. package/package-source/docs/codex-harness-engineering/templates/docs/service-dependency-matrix.md +70 -55
  109. package/package-source/docs/codex-harness-engineering/templates/docs/task-session-strategy.md +133 -116
  110. package/package-source/docs/codex-harness-engineering/templates/docs/team-knowledge-sync.md +187 -187
  111. package/package-source/docs/codex-harness-engineering/templates/docs/trace-format.md +44 -32
  112. package/package-source/docs/codex-harness-engineering/templates/governance/AGENTS.md +18 -0
  113. package/package-source/docs/codex-harness-engineering/templates/governance/branch-protection-checklist.md +50 -50
  114. package/package-source/docs/codex-harness-engineering/templates/governance/feedback-evolution-loop.md +55 -55
  115. package/package-source/docs/codex-harness-engineering/templates/governance/retry-budget.yaml +30 -30
  116. package/package-source/docs/codex-harness-engineering/templates/governance/risk-levels.yaml +53 -53
  117. package/package-source/docs/codex-harness-engineering/templates/governance/sandbox-policy.md +13 -13
  118. package/package-source/docs/codex-harness-engineering/templates/hooks/AGENTS.md +15 -0
  119. package/package-source/docs/codex-harness-engineering/templates/hooks/hook-stop-verify.ps1 +171 -118
  120. package/package-source/docs/codex-harness-engineering/templates/hooks/hooks.json +40 -40
  121. package/package-source/docs/codex-harness-engineering/templates/knowledge/AGENTS.md +16 -0
  122. package/package-source/docs/codex-harness-engineering/templates/knowledge/catalog.md +6 -6
  123. package/package-source/docs/codex-harness-engineering/templates/knowledge/decisions/DECISION-HARNESS-001.md +39 -39
  124. package/package-source/docs/codex-harness-engineering/templates/knowledge/guidelines/GUIDELINE-RULES-001.md +30 -30
  125. package/package-source/docs/codex-harness-engineering/templates/knowledge/knowledge-catalog.md +41 -41
  126. package/package-source/docs/codex-harness-engineering/templates/package-assets/.specify/templates/plan-template.md +252 -252
  127. package/package-source/docs/codex-harness-engineering/templates/package-assets/.specify/templates/spec-template.md +145 -145
  128. package/package-source/docs/codex-harness-engineering/templates/package-assets/.specify/templates/tasks-template.md +47 -47
  129. package/package-source/docs/codex-harness-engineering/templates/package-assets/AGENTS.md +19 -0
  130. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/AGENTS.md +25 -0
  131. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/README.md +118 -42
  132. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/START-HERE.md +66 -53
  133. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/agent-ecosystem-practices.md +140 -140
  134. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/best-practices.md +9 -9
  135. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/codex-global-rules-example.md +48 -48
  136. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/AGENTS.md +14 -0
  137. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/goal-templates.md +380 -380
  138. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/AGENTS.md +14 -0
  139. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/README.md +64 -64
  140. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/contracts/openapi.yaml +25 -25
  141. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/contracts/orval.config.ts +20 -20
  142. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/docs/design/ai-image-brief.md +44 -44
  143. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/docs/design/component-map.md +17 -17
  144. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/docs/design/design-brief.md +58 -58
  145. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/docs/design/frontend-architecture.md +106 -106
  146. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/docs/design/image-to-frontend-spec.md +72 -72
  147. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/docs/design/screen-states.md +25 -25
  148. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/docs/design/ui-image-review.md +38 -38
  149. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/docs/product/difficulty-research.md +39 -39
  150. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/docs/product/page-inventory.md +5 -5
  151. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/docs/product/prd-lite.md +41 -41
  152. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/docs/product/requirement-interface-matrix.md +32 -32
  153. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/docs/product/state-matrix.yaml +14 -14
  154. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/packages/api-client/generated/model/Ticket.ts +6 -6
  155. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/packages/api-client/generated/ticket-api.ts +35 -35
  156. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/packages/api-client/http-client.ts +8 -8
  157. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/packages/ui/TicketFilterBar.tsx +72 -72
  158. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/plans/features/ticket-filter.dev-plan.md +48 -48
  159. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/progress.txt +1 -1
  160. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/stories/TicketFilterBar.stories.tsx +33 -33
  161. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/task.json +54 -54
  162. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/examples/ticket-filter-demo/verify.ps1 +42 -42
  163. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/global-rules-and-bootstrap.md +157 -157
  164. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/goal-harness-integration-guide.md +364 -364
  165. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/implementation-flow.md +7 -5
  166. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/implementation-guide.md +25 -25
  167. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/knowledge-surface-map.md +186 -0
  168. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/mode-matrix.md +57 -57
  169. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/new-project-usage.md +176 -0
  170. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/project-agents-template.md +168 -154
  171. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/superpowers-codex-solo-builder-playbook.md +676 -676
  172. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/AGENTS.md +22 -0
  173. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/context/AGENTS.md +14 -0
  174. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/context/API_MAP.md +18 -0
  175. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/context/CURRENT_TASK.md +89 -0
  176. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/context/dev-plan.md +166 -0
  177. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/contracts/AGENTS.md +14 -0
  178. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/contracts/README.md +24 -0
  179. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/contracts/prism-usage.md +41 -0
  180. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/design/AGENTS.md +14 -0
  181. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/design/component-map.md +45 -31
  182. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/design/design-brief.md +183 -178
  183. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/design/design-tokens.json +88 -88
  184. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/design/frontend-architecture.md +164 -146
  185. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/design/image-to-frontend-spec.md +64 -52
  186. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/design/screen-states.md +157 -122
  187. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/design/visual-parity-review.md +21 -21
  188. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/docs/AGENTS.md +14 -0
  189. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/docs/env-and-deployment-template.md +60 -0
  190. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/docs/frontend-quality-rules.md +161 -138
  191. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/docs/regression-rules.md +47 -0
  192. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/docs/trace-format.md +86 -0
  193. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/product/AGENTS.md +14 -0
  194. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/product/prd-lite.md +166 -0
  195. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/product/state-matrix.yaml +116 -0
  196. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/prompts/AGENTS.md +14 -0
  197. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/prompts/implement-one-task.md +150 -0
  198. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/prompts/repair-one-finding.md +70 -0
  199. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/spec/11-security-design.md +43 -0
  200. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/spec/AGENTS.md +14 -0
  201. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/testing/AGENTS.md +14 -0
  202. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/testing/EVIDENCE_PROTOCOL.md +54 -0
  203. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/testing/NATURAL_LANGUAGE_TEST_CASES.md +690 -0
  204. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/testing/REGRESSION_PLAN.md +28 -0
  205. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/testing/TEST_STRATEGY.md +98 -0
  206. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/testing/TRACEABILITY_MATRIX.md +23 -0
  207. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/codex-harness-engineering/templates/testing/verify-matrix.md +51 -0
  208. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/ecc-zh-CN/README.md +10 -10
  209. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/ecc-zh-CN/commands/e2e.md +30 -30
  210. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/external-knowledge/README.md +60 -60
  211. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/external-knowledge/app/Mobile_App_Production_Readiness_Framework_/347/247/273/345/212/250/347/253/257/345/205/250/345/271/263/345/217/260/347/224/237/344/272/247/345/217/257/344/270/212/347/272/277/350/247/204/350/214/203.md +2457 -2457
  212. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/external-knowledge/app/catalog.md +33 -33
  213. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/external-knowledge/architecture.md +358 -358
  214. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/external-knowledge/common/AI_Coding_Production_Control_Framework_AI/345/206/231/345/256/214/347/232/204/344/273/243/347/240/201/345/246/202/344/275/225/344/270/215/345/244/261/346/216/247.md +2055 -2055
  215. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/external-knowledge/common/catalog.md +34 -34
  216. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/external-knowledge/initial-content.md +296 -296
  217. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/external-knowledge/integration-roadmap.md +221 -221
  218. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/harness/architecture-constraints.md +95 -83
  219. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/harness/code-semantics-and-navigation.md +17 -0
  220. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/harness/code-style-and-naming.md +116 -0
  221. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/harness/frontend-quality-rules.md +138 -138
  222. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/harness/governance-auto-repair.md +82 -80
  223. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/harness/mcp-knowledge-governance.md +24 -0
  224. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/harness/service-dependency-matrix.md +55 -55
  225. package/package-source/docs/codex-harness-engineering/templates/package-assets/docs/harness/speckit-requirements-gate.md +124 -124
  226. package/package-source/docs/codex-harness-engineering/templates/package-assets/root/PACKAGE.md +7 -7
  227. package/package-source/docs/codex-harness-engineering/templates/package-assets/root/README.md +52 -52
  228. package/package-source/docs/codex-harness-engineering/templates/package-assets/root/install-agent-here.ps1 +57 -57
  229. package/package-source/docs/codex-harness-engineering/templates/package-assets/rules/agents.md +115 -115
  230. package/package-source/docs/codex-harness-engineering/templates/package-assets/rules/coding-style.md +57 -74
  231. package/package-source/docs/codex-harness-engineering/templates/package-assets/rules/constitution.md +4 -4
  232. package/package-source/docs/codex-harness-engineering/templates/package-assets/rules/git.rules +41 -41
  233. package/package-source/docs/codex-harness-engineering/templates/package-assets/rules/harness.rules +29 -29
  234. package/package-source/docs/codex-harness-engineering/templates/package-assets/rules/safety.rules +35 -35
  235. package/package-source/docs/codex-harness-engineering/templates/package-assets/scripts/ai-workflow/check-ai-sync-drift.ps1 +205 -205
  236. package/package-source/docs/codex-harness-engineering/templates/package-assets/scripts/ai-workflow/sync-ai-config-to-targets.ps1 +826 -826
  237. package/package-source/docs/codex-harness-engineering/templates/package-assets/scripts/ai-workflow/sync-targets.example.json +35 -35
  238. package/package-source/docs/codex-harness-engineering/templates/package-assets/scripts/harness/harness-governance-check.ps1 +864 -366
  239. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/HOW_TO_USE_SKILLS.md +79 -79
  240. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/academic-mermaid-diagrams/SKILL.md +172 -172
  241. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/ai-config-git-sync/SKILL.md +152 -152
  242. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/ai-config-git-sync/references/command-recipes.md +92 -92
  243. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/auto-commit/SKILL.md +324 -322
  244. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/better-icons/SKILL.md +3 -3
  245. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/claude-openrouter-clash-debug/SKILL.md +100 -100
  246. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/claude-openrouter-clash-debug/references/windows-openrouter-clash-playbook.md +94 -94
  247. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/claude-openrouter-clash-debug/scripts/diagnose-openrouter-route.ps1 +282 -282
  248. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/claude-openrouter-clash-debug/scripts/ensure-openrouter-rule.ps1 +101 -101
  249. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/fixing-metadata/SKILL.md +6 -6
  250. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/frontend-design/SKILL.md +37 -37
  251. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/git-xianyu-analyzer/SKILL.md +3 -3
  252. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/harness-surface-sync/SKILL.md +204 -188
  253. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/harness-surface-sync/references/current-repo-sync-matrix.md +150 -110
  254. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/harness-surface-sync/references/stale-patterns.md +65 -65
  255. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/init-autopilot/SKILL.md +17 -15
  256. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/log-analysis-optimization/SKILL.md +5 -5
  257. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/prd-writer-skill/SKILL.md +28 -28
  258. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/qa-e2e-planner/SKILL.md +11 -11
  259. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/qa-e2e-runner/SKILL.md +6 -6
  260. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/qa-mock-cleaner/SKILL.md +4 -4
  261. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/qoder-codex-api-config/SKILL.md +67 -67
  262. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/qoder-codex-api-config/agents/openai.yaml +7 -7
  263. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/qoder-codex-api-config/scripts/configure-qoder-codex-api.ps1 +278 -278
  264. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/requirements-design-template/SKILL.md +87 -87
  265. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/skills-index.md +12 -12
  266. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/speckit-analyze/SKILL.md +34 -34
  267. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/speckit-checklist/SKILL.md +8 -8
  268. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/speckit-clarify/SKILL.md +28 -28
  269. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/speckit-e2e-tasks/SKILL.md +12 -12
  270. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/speckit-implement/SKILL.md +37 -37
  271. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/speckit-plan/SKILL.md +140 -138
  272. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/speckit-specify/SKILL.md +100 -100
  273. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/speckit-tasks/SKILL.md +86 -86
  274. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/speckit-verify/SKILL.md +144 -142
  275. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/sync-project-root-docs/SKILL.md +145 -143
  276. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/sync-project-root-docs/references/current-repo-notes.md +42 -41
  277. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/sync-project-root-docs/references/github-samples.md +33 -33
  278. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/sync-project-root-docs/scripts/collect-root-doc-facts.ps1 +174 -145
  279. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/thesis-writing/references/ai-integrity-and-originality.md +191 -191
  280. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/thesis-writing/references/citation-templates.md +99 -99
  281. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/thesis-writing/references/final-checklist.md +60 -60
  282. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/thesis-writing/references/photographer-booking-paper.md +85 -85
  283. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/thesis-writing/references/test-result-templates.md +22 -22
  284. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/thesis-writing/references/thesis-analysis-template.md +63 -63
  285. package/package-source/docs/codex-harness-engineering/templates/package-assets/skills/update-codemaps/SKILL.md +31 -31
  286. package/package-source/docs/codex-harness-engineering/templates/package-assets/workflows/README.md +15 -15
  287. package/package-source/docs/codex-harness-engineering/templates/package-assets/workflows/speckit.analyze.md +60 -60
  288. package/package-source/docs/codex-harness-engineering/templates/package-assets/workflows/speckit.checklist.md +8 -8
  289. package/package-source/docs/codex-harness-engineering/templates/package-assets/workflows/speckit.clarify.md +28 -28
  290. package/package-source/docs/codex-harness-engineering/templates/package-assets/workflows/speckit.e2e-testing.md +10 -10
  291. package/package-source/docs/codex-harness-engineering/templates/package-assets/workflows/speckit.implement.md +56 -56
  292. package/package-source/docs/codex-harness-engineering/templates/package-assets/workflows/speckit.plan.md +137 -134
  293. package/package-source/docs/codex-harness-engineering/templates/package-assets/workflows/speckit.specify.md +100 -100
  294. package/package-source/docs/codex-harness-engineering/templates/package-assets/workflows/speckit.tasks.md +86 -86
  295. package/package-source/docs/codex-harness-engineering/templates/package-assets/workflows/speckit.verify.md +130 -130
  296. package/package-source/docs/codex-harness-engineering/templates/product/AGENTS.md +19 -0
  297. package/package-source/docs/codex-harness-engineering/templates/product/acceptance-criteria.md +47 -47
  298. package/package-source/docs/codex-harness-engineering/templates/product/difficulty-research.md +47 -47
  299. package/package-source/docs/codex-harness-engineering/templates/product/page-inventory.md +21 -21
  300. package/package-source/docs/codex-harness-engineering/templates/product/prd-lite.md +166 -158
  301. package/package-source/docs/codex-harness-engineering/templates/product/requirement-interface-matrix.md +37 -37
  302. package/package-source/docs/codex-harness-engineering/templates/product/state-matrix.yaml +116 -105
  303. package/package-source/docs/codex-harness-engineering/templates/prompts/AGENTS.md +15 -0
  304. package/package-source/docs/codex-harness-engineering/templates/prompts/controller-loop.md +88 -88
  305. package/package-source/docs/codex-harness-engineering/templates/prompts/failure-triage.md +71 -69
  306. package/package-source/docs/codex-harness-engineering/templates/prompts/harness-audit.md +54 -52
  307. package/package-source/docs/codex-harness-engineering/templates/prompts/implement-one-task.md +150 -148
  308. package/package-source/docs/codex-harness-engineering/templates/prompts/repair-one-finding.md +70 -67
  309. package/package-source/docs/codex-harness-engineering/templates/prompts/review-one-task.md +45 -43
  310. package/package-source/docs/codex-harness-engineering/templates/prompts/review-stage1-spec.md +111 -109
  311. package/package-source/docs/codex-harness-engineering/templates/prompts/review-stage2-quality.md +82 -80
  312. package/package-source/docs/codex-harness-engineering/templates/prompts/visual-evaluator.md +80 -78
  313. package/package-source/docs/codex-harness-engineering/templates/prompts/worker-role/backend-worker.md +41 -41
  314. package/package-source/docs/codex-harness-engineering/templates/prompts/worker-role/docs-worker.md +28 -28
  315. package/package-source/docs/codex-harness-engineering/templates/prompts/worker-role/frontend-worker.md +46 -46
  316. package/package-source/docs/codex-harness-engineering/templates/prompts/worker-role/harness-writer.md +40 -40
  317. package/package-source/docs/codex-harness-engineering/templates/prompts/worker-role/test-runner.md +27 -27
  318. package/package-source/docs/codex-harness-engineering/templates/runtime/AGENTS.md +66 -50
  319. package/package-source/docs/codex-harness-engineering/templates/runtime/codex-loop.ps1 +2129 -231
  320. package/package-source/docs/codex-harness-engineering/templates/runtime/doctor.ps1 +224 -224
  321. package/package-source/docs/codex-harness-engineering/templates/runtime/project-task-template.json +42 -39
  322. package/package-source/docs/codex-harness-engineering/templates/runtime/scripts/test-install-modes.ps1 +2 -2
  323. package/package-source/docs/codex-harness-engineering/templates/runtime/smoke-task.json +52 -52
  324. package/package-source/docs/codex-harness-engineering/templates/runtime/task-run-profile.json +86 -86
  325. package/package-source/docs/codex-harness-engineering/templates/runtime/task.json +11 -8
  326. package/package-source/docs/codex-harness-engineering/templates/runtime/verify.ps1 +21 -21
  327. package/package-source/docs/codex-harness-engineering/templates/scripts/AGENTS.md +15 -0
  328. package/package-source/docs/codex-harness-engineering/templates/scripts/ai-workflow/check-ai-sync-drift.ps1 +205 -205
  329. package/package-source/docs/codex-harness-engineering/templates/scripts/harness/harness-governance-check.ps1 +864 -366
  330. package/package-source/docs/codex-harness-engineering/templates/spec/10-data-model.md +23 -7
  331. package/package-source/docs/codex-harness-engineering/templates/spec/11-security-design.md +30 -13
  332. package/package-source/docs/codex-harness-engineering/templates/spec/AGENTS.md +15 -0
  333. package/package-source/docs/codex-harness-engineering/templates/testing/ACCEPTANCE_CRITERIA.md +39 -39
  334. package/package-source/docs/codex-harness-engineering/templates/testing/ACCEPTANCE_EXAMPLES.md +37 -37
  335. package/package-source/docs/codex-harness-engineering/templates/testing/AGENTS.md +19 -0
  336. package/package-source/docs/codex-harness-engineering/templates/testing/EVIDENCE_PROTOCOL.md +54 -48
  337. package/package-source/docs/codex-harness-engineering/templates/testing/NATURAL_LANGUAGE_TEST_CASES.md +5 -4
  338. package/package-source/docs/codex-harness-engineering/templates/testing/REGRESSION_PLAN.md +28 -20
  339. package/package-source/docs/codex-harness-engineering/templates/testing/RISK_BASED_TEST_PLAN.md +16 -16
  340. package/package-source/docs/codex-harness-engineering/templates/testing/TEST_STRATEGY.md +98 -97
  341. package/package-source/docs/codex-harness-engineering/templates/testing/TRACEABILITY_MATRIX.md +6 -5
  342. package/package-source/docs/codex-harness-engineering/templates/testing/coverage-policy.md +25 -25
  343. package/package-source/docs/codex-harness-engineering/templates/testing/e2e-plan.md +139 -139
  344. package/package-source/docs/codex-harness-engineering/templates/testing/failure-findings.example.json +3 -3
  345. package/package-source/docs/codex-harness-engineering/templates/testing/failure-triage.md +62 -62
  346. package/package-source/docs/codex-harness-engineering/templates/testing/test-data-plan.md +36 -36
  347. package/package-source/docs/codex-harness-engineering/templates/testing/test-report.md +85 -41
  348. package/package-source/docs/codex-harness-engineering/templates/testing/verify-matrix.md +15 -5
  349. package/package-source/docs/codex-harness-engineering/templates/tools/AGENTS.md +14 -0
  350. package/package-source/docs/codex-harness-engineering/templates/tools/harness/acceptance-lint.ps1 +37 -0
  351. package/package-source/docs/codex-harness-engineering/templates/tools/harness/architecture-lint.ps1 +150 -0
  352. package/package-source/docs/codex-harness-engineering/templates/tools/harness/backend-lint.ps1 +137 -0
  353. package/package-source/docs/codex-harness-engineering/templates/tools/harness/business-lint.ps1 +148 -0
  354. package/package-source/docs/codex-harness-engineering/templates/tools/harness/component-lint.ps1 +148 -0
  355. package/package-source/docs/codex-harness-engineering/templates/tools/harness/config-lint.ps1 +159 -0
  356. package/package-source/docs/codex-harness-engineering/templates/tools/harness/context-lint.ps1 +187 -0
  357. package/package-source/docs/codex-harness-engineering/templates/tools/harness/contract-lint.ps1 +148 -0
  358. package/package-source/docs/codex-harness-engineering/templates/tools/harness/data-lint.ps1 +37 -0
  359. package/package-source/docs/codex-harness-engineering/templates/tools/harness/directory-lint.ps1 +152 -0
  360. package/package-source/docs/codex-harness-engineering/templates/tools/harness/docs-lint.ps1 +40 -0
  361. package/package-source/docs/codex-harness-engineering/templates/tools/harness/impact-lint.ps1 +148 -0
  362. package/package-source/docs/codex-harness-engineering/templates/tools/harness/integration-lint.ps1 +37 -0
  363. package/package-source/docs/codex-harness-engineering/templates/tools/harness/mobile-lint.ps1 +37 -0
  364. package/package-source/docs/codex-harness-engineering/templates/tools/harness/observability-lint.ps1 +137 -0
  365. package/package-source/docs/codex-harness-engineering/templates/tools/harness/performance-lint.ps1 +148 -0
  366. package/package-source/docs/codex-harness-engineering/templates/tools/harness/refactor-lint.ps1 +137 -0
  367. package/package-source/docs/codex-harness-engineering/templates/tools/harness/security-lint.ps1 +159 -0
  368. package/package-source/docs/codex-harness-engineering/templates/tools/harness/session-lint.ps1 +126 -0
  369. package/package-source/docs/codex-harness-engineering/templates/tools/harness/spec-lint.ps1 +285 -0
  370. package/package-source/docs/codex-harness-engineering/templates/tools/harness/state-lint.ps1 +137 -0
  371. package/package-source/docs/codex-harness-engineering/templates/tools/harness/style-lint.ps1 +155 -0
  372. package/package-source/docs/codex-harness-engineering/templates/tools/harness/testing-lint.ps1 +192 -0
  373. package/package-source/docs/codex-harness-engineering/templates/tools/harness/ui-lint.ps1 +148 -0
  374. package/package-source/docs/codex-harness-engineering/templates/trace/AGENTS.md +16 -0
  375. package/package-source/docs/codex-harness-engineering/templates/trace/eval-case.yaml +24 -24
  376. package/package-source/docs/codex-harness-engineering/templates/trace/trace.schema.json +101 -101
  377. package/package-source/install-manifest.json +149 -79
  378. package/package-source/tools/install/ai-workflow/check-ai-sync-drift.ps1 +205 -205
  379. package/package-source/tools/install/ai-workflow/sync-ai-config-to-targets.ps1 +826 -826
  380. package/package-source/tools/install/ai-workflow/sync-targets.example.json +35 -35
  381. package/package-source/tools/install/bootstrap-codex-harness.ps1 +145 -129
  382. package/package-source/tools/install/env-check.ps1 +40 -40
  383. package/package-source/tools/install/init-project.ps1 +1 -1
  384. package/package-source/tools/install/install-agent-here.ps1 +57 -57
  385. package/package-source/tools/install/install-agent.ps1 +33 -31
  386. package/package.json +23 -23
@@ -1,826 +1,826 @@
1
- param(
2
- [string]$ConfigPath = "",
3
- [string]$SourceRoot = "",
4
- [string[]]$OnlyTargets = @(),
5
- [switch]$NoPush,
6
- [switch]$NoPullRequest,
7
- [switch]$AllowDirtySource
8
- )
9
-
10
- Set-StrictMode -Version Latest
11
- $ErrorActionPreference = "Stop"
12
-
13
- if ([string]::IsNullOrWhiteSpace($SourceRoot)) {
14
- $SourceRoot = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot "..\.."))
15
- }
16
-
17
- if ([string]::IsNullOrWhiteSpace($ConfigPath)) {
18
- $ConfigPath = Join-Path $PSScriptRoot "sync-targets.json"
19
- }
20
-
21
- function Write-Step {
22
- param([string]$Message)
23
- Write-Host ("[ai-sync-targets] {0}" -f $Message)
24
- }
25
-
26
- function Get-OptionalPropertyValue {
27
- param(
28
- [object]$Object,
29
- [string]$Name
30
- )
31
-
32
- if ($null -eq $Object) {
33
- return $null
34
- }
35
-
36
- $property = $Object.PSObject.Properties[$Name]
37
- if ($null -eq $property) {
38
- return $null
39
- }
40
-
41
- return $property.Value
42
- }
43
-
44
- function Set-OptionalPropertyValue {
45
- param(
46
- [object]$Object,
47
- [string]$Name,
48
- [object]$Value
49
- )
50
-
51
- $property = $Object.PSObject.Properties[$Name]
52
- if ($null -eq $property) {
53
- $Object | Add-Member -NotePropertyName $Name -NotePropertyValue $Value -Force
54
- return
55
- }
56
-
57
- $property.Value = $Value
58
- }
59
-
60
- function Normalize-RelativePath {
61
- param(
62
- [Parameter(Mandatory = $true)]
63
- [string]$Path,
64
- [switch]$Directory
65
- )
66
-
67
- $normalized = $Path.Replace("\", "/").Trim()
68
- while ($normalized.StartsWith("./", [System.StringComparison]::Ordinal)) {
69
- $normalized = $normalized.Substring(2)
70
- }
71
-
72
- if ($Directory) {
73
- return $normalized.TrimEnd("/") + "/"
74
- }
75
-
76
- return $normalized.TrimEnd("/")
77
- }
78
-
79
- function Join-NormalizedRelativePath {
80
- param(
81
- [string]$Base,
82
- [string]$Child
83
- )
84
-
85
- $left = Normalize-RelativePath -Path $Base
86
- $right = Normalize-RelativePath -Path $Child
87
- if ([string]::IsNullOrWhiteSpace($left)) {
88
- return $right
89
- }
90
- if ([string]::IsNullOrWhiteSpace($right)) {
91
- return $left
92
- }
93
-
94
- return "{0}/{1}" -f $left.TrimEnd("/"), $right
95
- }
96
-
97
- function Get-RelativePathFromBase {
98
- param(
99
- [string]$BasePath,
100
- [string]$TargetPath
101
- )
102
-
103
- $baseFull = [System.IO.Path]::GetFullPath($BasePath).TrimEnd("\", "/") + [System.IO.Path]::DirectorySeparatorChar
104
- $targetFull = [System.IO.Path]::GetFullPath($TargetPath)
105
- if (-not $targetFull.StartsWith($baseFull, [System.StringComparison]::OrdinalIgnoreCase)) {
106
- throw "路径不在预期根目录内: $TargetPath"
107
- }
108
-
109
- return Normalize-RelativePath -Path $targetFull.Substring($baseFull.Length)
110
- }
111
-
112
- function Read-JsonConfig {
113
- param(
114
- [string]$Path,
115
- [string]$Label
116
- )
117
-
118
- if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) {
119
- throw "找不到 ${Label}: $Path"
120
- }
121
-
122
- try {
123
- return Get-Content -LiteralPath $Path -Raw | ConvertFrom-Json
124
- }
125
- catch {
126
- throw "${Label} 需要使用 JSON 兼容格式,当前文件无法用 ConvertFrom-Json 解析: $Path"
127
- }
128
- }
129
-
130
- function Write-JsonFile {
131
- param(
132
- [string]$Path,
133
- [object]$Value
134
- )
135
-
136
- $parent = Split-Path -Parent $Path
137
- if (-not (Test-Path -LiteralPath $parent)) {
138
- New-Item -ItemType Directory -Force -Path $parent | Out-Null
139
- }
140
-
141
- $json = $Value | ConvertTo-Json -Depth 10
142
- [System.IO.File]::WriteAllText($Path, $json + [Environment]::NewLine, [System.Text.UTF8Encoding]::new($false))
143
- }
144
-
145
- function Get-FileSha256 {
146
- param([string]$Path)
147
- return (Get-FileHash -LiteralPath $Path -Algorithm SHA256).Hash.ToLowerInvariant()
148
- }
149
-
150
- function Quote-ProcessArgument {
151
- param([string]$Value)
152
-
153
- if ($null -eq $Value -or $Value.Length -eq 0) {
154
- return '""'
155
- }
156
-
157
- if ($Value -notmatch '[\s"]') {
158
- return $Value
159
- }
160
-
161
- $escaped = $Value -replace '(\\*)"', '$1$1\"'
162
- $escaped = $escaped -replace '(\\+)$', '$1$1'
163
- return '"' + $escaped + '"'
164
- }
165
-
166
- function Invoke-Git {
167
- param(
168
- [string]$RepositoryPath,
169
- [string[]]$Arguments
170
- )
171
-
172
- $stdoutPath = Join-Path ([System.IO.Path]::GetTempPath()) ("git-stdout-{0}.log" -f ([guid]::NewGuid().ToString("N")))
173
- $stderrPath = Join-Path ([System.IO.Path]::GetTempPath()) ("git-stderr-{0}.log" -f ([guid]::NewGuid().ToString("N")))
174
- try {
175
- $argumentString = ((@("-C", $RepositoryPath) + $Arguments) | ForEach-Object { Quote-ProcessArgument -Value ([string]$_) }) -join " "
176
- $process = Start-Process `
177
- -FilePath "git" `
178
- -ArgumentList $argumentString `
179
- -NoNewWindow `
180
- -Wait `
181
- -PassThru `
182
- -RedirectStandardOutput $stdoutPath `
183
- -RedirectStandardError $stderrPath
184
-
185
- $output = New-Object System.Collections.Generic.List[string]
186
- if (Test-Path -LiteralPath $stdoutPath -PathType Leaf) {
187
- foreach ($line in Get-Content -LiteralPath $stdoutPath) {
188
- $output.Add([string]$line)
189
- }
190
- }
191
- if (Test-Path -LiteralPath $stderrPath -PathType Leaf) {
192
- foreach ($line in Get-Content -LiteralPath $stderrPath) {
193
- $output.Add([string]$line)
194
- }
195
- }
196
-
197
- if ($process.ExitCode -ne 0) {
198
- $message = if ($output.Count -gt 0) { ($output -join [Environment]::NewLine) } else { "git command failed" }
199
- throw "git $($Arguments -join ' ') failed in $RepositoryPath`n$message"
200
- }
201
-
202
- return [string[]]$output.ToArray()
203
- }
204
- finally {
205
- if (Test-Path -LiteralPath $stdoutPath) {
206
- Remove-Item -LiteralPath $stdoutPath -Force -ErrorAction SilentlyContinue
207
- }
208
- if (Test-Path -LiteralPath $stderrPath) {
209
- Remove-Item -LiteralPath $stderrPath -Force -ErrorAction SilentlyContinue
210
- }
211
- }
212
- }
213
-
214
- function Get-GitStatusLines {
215
- param([string]$RepositoryPath)
216
- return @(Invoke-Git -RepositoryPath $RepositoryPath -Arguments @("status", "--porcelain"))
217
- }
218
-
219
- function Get-GitCurrentBranch {
220
- param([string]$RepositoryPath)
221
- $output = @(Invoke-Git -RepositoryPath $RepositoryPath -Arguments @("branch", "--show-current"))
222
- if (@($output).Count -eq 0) {
223
- return ""
224
- }
225
-
226
- return [string]$output[0]
227
- }
228
-
229
- function Get-GitHeadCommit {
230
- param([string]$RepositoryPath)
231
- $output = @(Invoke-Git -RepositoryPath $RepositoryPath -Arguments @("rev-parse", "HEAD"))
232
- return [string]$output[0]
233
- }
234
-
235
- function Test-GitRepository {
236
- param([string]$RepositoryPath)
237
- try {
238
- Invoke-Git -RepositoryPath $RepositoryPath -Arguments @("rev-parse", "--show-toplevel") | Out-Null
239
- return $true
240
- }
241
- catch {
242
- return $false
243
- }
244
- }
245
-
246
- function Expand-ManagedEntries {
247
- param(
248
- [object]$Config,
249
- [string]$SourceRoot,
250
- [string]$DestinationRoot
251
- )
252
-
253
- $entries = New-Object System.Collections.Generic.List[object]
254
- $directoryPlans = New-Object System.Collections.Generic.List[object]
255
- $destinationSet = @{}
256
- $sourceHashSet = @{}
257
-
258
- $managedEntries = Get-OptionalPropertyValue -Object $Config -Name "managed"
259
- foreach ($mapping in @($managedEntries)) {
260
- $mappingTypeValue = [string](Get-OptionalPropertyValue -Object $mapping -Name "type")
261
- $mappingType = if ([string]::IsNullOrWhiteSpace($mappingTypeValue)) { "directory" } else { $mappingTypeValue }
262
- $sourceRelative = Normalize-RelativePath -Path ([string](Get-OptionalPropertyValue -Object $mapping -Name "source")) -Directory:($mappingType -eq "directory")
263
- $destinationRelative = Normalize-RelativePath -Path ([string](Get-OptionalPropertyValue -Object $mapping -Name "dest")) -Directory:($mappingType -eq "directory")
264
- $sourcePath = Join-Path $SourceRoot $sourceRelative
265
-
266
- if ($mappingType -eq "file") {
267
- if (-not (Test-Path -LiteralPath $sourcePath -PathType Leaf)) {
268
- throw "managed source file 不存在: $sourceRelative"
269
- }
270
-
271
- if ($destinationSet.ContainsKey($destinationRelative)) {
272
- throw "发现重复的 managed destination: $destinationRelative"
273
- }
274
- $destinationSet[$destinationRelative] = $true
275
- $sourceHashSet[$sourceRelative] = $true
276
-
277
- $entries.Add([pscustomobject]@{
278
- SourceRelative = $sourceRelative
279
- SourcePath = $sourcePath
280
- DestinationRelative = $destinationRelative
281
- DestinationPath = Join-Path $DestinationRoot $destinationRelative
282
- })
283
- continue
284
- }
285
-
286
- if (-not (Test-Path -LiteralPath $sourcePath -PathType Container)) {
287
- throw "managed source directory 不存在: $sourceRelative"
288
- }
289
-
290
- $expectedDestinations = @{}
291
- foreach ($file in Get-ChildItem -LiteralPath $sourcePath -Recurse -File) {
292
- $childRelative = Get-RelativePathFromBase -BasePath $sourcePath -TargetPath $file.FullName
293
- $entrySourceRelative = Join-NormalizedRelativePath -Base $sourceRelative -Child $childRelative
294
- $entryDestinationRelative = Join-NormalizedRelativePath -Base $destinationRelative -Child $childRelative
295
-
296
- if ($destinationSet.ContainsKey($entryDestinationRelative)) {
297
- throw "发现重复的 managed destination: $entryDestinationRelative"
298
- }
299
-
300
- $destinationSet[$entryDestinationRelative] = $true
301
- $expectedDestinations[$entryDestinationRelative] = $true
302
- $sourceHashSet[$entrySourceRelative] = $true
303
-
304
- $entries.Add([pscustomobject]@{
305
- SourceRelative = $entrySourceRelative
306
- SourcePath = Join-Path $SourceRoot $entrySourceRelative
307
- DestinationRelative = $entryDestinationRelative
308
- DestinationPath = Join-Path $DestinationRoot $entryDestinationRelative
309
- })
310
- }
311
-
312
- $directoryPlans.Add([pscustomobject]@{
313
- SourceRelative = $sourceRelative
314
- DestinationRelative = $destinationRelative
315
- DestinationRootPath = Join-Path $DestinationRoot $destinationRelative
316
- ExpectedDestinations = $expectedDestinations
317
- })
318
- }
319
-
320
- return [pscustomobject]@{
321
- Entries = [object[]]$entries.ToArray()
322
- DirectoryPlans = [object[]]$directoryPlans.ToArray()
323
- SourceHashKeys = @($sourceHashSet.Keys | Sort-Object)
324
- }
325
- }
326
-
327
- function Remove-EmptyDirectories {
328
- param([string]$RootPath)
329
-
330
- if (-not (Test-Path -LiteralPath $RootPath -PathType Container)) {
331
- return
332
- }
333
-
334
- $directories = Get-ChildItem -LiteralPath $RootPath -Recurse -Directory | Sort-Object FullName -Descending
335
- foreach ($directory in $directories) {
336
- if ((Get-ChildItem -LiteralPath $directory.FullName -Force | Measure-Object).Count -eq 0) {
337
- Remove-Item -LiteralPath $directory.FullName -Force
338
- }
339
- }
340
- }
341
-
342
- function Get-DirectoryExtraFiles {
343
- param(
344
- [object]$DirectoryPlan,
345
- [string]$TargetRoot
346
- )
347
-
348
- if (-not (Test-Path -LiteralPath $DirectoryPlan.DestinationRootPath -PathType Container)) {
349
- return @()
350
- }
351
-
352
- $extras = New-Object System.Collections.Generic.List[object]
353
- foreach ($file in Get-ChildItem -LiteralPath $DirectoryPlan.DestinationRootPath -Recurse -File) {
354
- $relative = Get-RelativePathFromBase -BasePath $TargetRoot -TargetPath $file.FullName
355
- if (-not $DirectoryPlan.ExpectedDestinations.ContainsKey($relative)) {
356
- $extras.Add([pscustomobject]@{
357
- RelativePath = $relative
358
- FullPath = $file.FullName
359
- })
360
- }
361
- }
362
-
363
- return [object[]]$extras.ToArray()
364
- }
365
-
366
- function Get-DefaultSourceRepo {
367
- param([string]$RepositoryPath)
368
-
369
- try {
370
- $remoteUrl = [string](@(Invoke-Git -RepositoryPath $RepositoryPath -Arguments @("remote", "get-url", "origin"))[0])
371
- }
372
- catch {
373
- return ""
374
- }
375
-
376
- if ($remoteUrl -match 'github\.com[:/](.+?)(?:\.git)?$') {
377
- return $Matches[1]
378
- }
379
-
380
- return $remoteUrl
381
- }
382
-
383
- function Resolve-TargetRepoSlug {
384
- param(
385
- [string]$TargetRepo,
386
- [string]$RepositoryPath
387
- )
388
-
389
- if (-not [string]::IsNullOrWhiteSpace($TargetRepo)) {
390
- return $TargetRepo
391
- }
392
-
393
- return Get-DefaultSourceRepo -RepositoryPath $RepositoryPath
394
- }
395
-
396
- function Resolve-TargetPath {
397
- param(
398
- [object]$Target,
399
- [string]$RunRoot
400
- )
401
-
402
- $localPath = [string](Get-OptionalPropertyValue -Object $Target -Name "localPath")
403
- if (-not [string]::IsNullOrWhiteSpace($localPath)) {
404
- return [System.IO.Path]::GetFullPath($localPath)
405
- }
406
-
407
- $targetRepo = [string](Get-OptionalPropertyValue -Object $Target -Name "repo")
408
- if ([string]::IsNullOrWhiteSpace($targetRepo)) {
409
- throw "target 必须提供 repo 或 localPath。"
410
- }
411
-
412
- $repoKey = $targetRepo.Replace("/", "__")
413
- $checkoutPath = Join-Path $RunRoot $repoKey
414
- $checkoutParent = Split-Path -Parent $checkoutPath
415
- if (-not (Test-Path -LiteralPath $checkoutParent)) {
416
- New-Item -ItemType Directory -Force -Path $checkoutParent | Out-Null
417
- }
418
-
419
- if (Test-Path -LiteralPath $checkoutPath) {
420
- throw "临时 checkout 目录已存在: $checkoutPath"
421
- }
422
-
423
- if (Get-Command gh -ErrorAction SilentlyContinue) {
424
- Write-Step "克隆目标仓库 $targetRepo -> $checkoutPath"
425
- & gh repo clone $targetRepo $checkoutPath | Out-Host
426
- if ($LASTEXITCODE -ne 0) {
427
- throw "gh repo clone 失败: $targetRepo"
428
- }
429
- return $checkoutPath
430
- }
431
-
432
- $cloneUrl = "https://github.com/{0}.git" -f $targetRepo
433
- Write-Step "克隆目标仓库 $cloneUrl -> $checkoutPath"
434
- & git clone $cloneUrl $checkoutPath | Out-Host
435
- if ($LASTEXITCODE -ne 0) {
436
- throw "git clone 失败: $cloneUrl"
437
- }
438
-
439
- return $checkoutPath
440
- }
441
-
442
- function Resolve-TemplateValue {
443
- param(
444
- [string]$Template,
445
- [hashtable]$Variables
446
- )
447
-
448
- $result = $Template
449
- foreach ($key in $Variables.Keys) {
450
- $result = $result.Replace(("{{{{{0}}}}}" -f $key), [string]$Variables[$key])
451
- }
452
-
453
- return $result
454
- }
455
-
456
- function Sync-TargetRepository {
457
- param(
458
- [string]$SourceRoot,
459
- [string]$SourceRepo,
460
- [string]$SourceRef,
461
- [string]$SourceCommit,
462
- [string]$TargetRoot,
463
- [string]$SyncConfigPath,
464
- [bool]$PullRequestMode
465
- )
466
-
467
- $targetConfigFile = Join-Path $TargetRoot $SyncConfigPath
468
- $targetConfig = Read-JsonConfig -Path $targetConfigFile -Label ".ai-sync.yml"
469
- $targetConfigChanged = $false
470
-
471
- $targetSource = Get-OptionalPropertyValue -Object $targetConfig -Name "source"
472
- if ($null -eq $targetSource) {
473
- $targetSource = [pscustomobject]@{}
474
- Set-OptionalPropertyValue -Object $targetConfig -Name "source" -Value $targetSource
475
- }
476
-
477
- if ([string](Get-OptionalPropertyValue -Object $targetSource -Name "repo") -ne $SourceRepo) {
478
- Set-OptionalPropertyValue -Object $targetSource -Name "repo" -Value $SourceRepo
479
- $targetConfigChanged = $true
480
- }
481
-
482
- if ([string](Get-OptionalPropertyValue -Object $targetSource -Name "ref") -ne $SourceRef) {
483
- Set-OptionalPropertyValue -Object $targetSource -Name "ref" -Value $SourceRef
484
- $targetConfigChanged = $true
485
- }
486
-
487
- if ($targetConfigChanged) {
488
- Write-JsonFile -Path $targetConfigFile -Value $targetConfig
489
- }
490
-
491
- $expansion = Expand-ManagedEntries -Config $targetConfig -SourceRoot $SourceRoot -DestinationRoot $TargetRoot
492
- $copiedCount = 0
493
- $deletedCount = 0
494
-
495
- foreach ($entry in $expansion.Entries) {
496
- $expectedHash = Get-FileSha256 -Path $entry.SourcePath
497
- $actualHash = if (Test-Path -LiteralPath $entry.DestinationPath -PathType Leaf) { Get-FileSha256 -Path $entry.DestinationPath } else { $null }
498
- if ($actualHash -eq $expectedHash) {
499
- continue
500
- }
501
-
502
- $destinationParent = Split-Path -Parent $entry.DestinationPath
503
- if (-not (Test-Path -LiteralPath $destinationParent)) {
504
- New-Item -ItemType Directory -Force -Path $destinationParent | Out-Null
505
- }
506
-
507
- Copy-Item -LiteralPath $entry.SourcePath -Destination $entry.DestinationPath -Force
508
- $copiedCount += 1
509
- }
510
-
511
- foreach ($directoryPlan in $expansion.DirectoryPlans) {
512
- foreach ($extra in Get-DirectoryExtraFiles -DirectoryPlan $directoryPlan -TargetRoot $TargetRoot) {
513
- Remove-Item -LiteralPath $extra.FullPath -Force
514
- $deletedCount += 1
515
- }
516
- Remove-EmptyDirectories -RootPath $directoryPlan.DestinationRootPath
517
- }
518
-
519
- $manifestFiles = foreach ($sourceRelative in $expansion.SourceHashKeys) {
520
- [pscustomobject]@{
521
- source = $sourceRelative
522
- sha256 = Get-FileSha256 -Path (Join-Path $SourceRoot $sourceRelative)
523
- }
524
- }
525
-
526
- $manifestObject = [pscustomobject]@{
527
- version = $SourceRef
528
- commit = $SourceCommit
529
- generatedAt = (Get-Date).ToString("yyyy-MM-ddTHH:mm:sszzz")
530
- files = @($manifestFiles | Sort-Object source)
531
- }
532
-
533
- $manifestFile = Join-Path $TargetRoot "ai-sync\ai-kit-manifest.json"
534
- Write-JsonFile -Path $manifestFile -Value $manifestObject
535
- $manifestHash = Get-FileSha256 -Path $manifestFile
536
-
537
- $lockObject = [pscustomobject]@{
538
- source = [pscustomobject]@{
539
- repo = $SourceRepo
540
- ref = $SourceRef
541
- commit = $SourceCommit
542
- }
543
- manifest = [pscustomobject]@{
544
- path = "ai-sync/ai-kit-manifest.json"
545
- sha256 = $manifestHash
546
- }
547
- syncedAt = (Get-Date).ToString("yyyy-MM-ddTHH:mm:sszzz")
548
- mode = if ($PullRequestMode) { "pull-request-sync" } else { "manual-sync" }
549
- }
550
-
551
- Write-JsonFile -Path (Join-Path $TargetRoot ".ai-sync.lock.json") -Value $lockObject
552
-
553
- return [pscustomobject]@{
554
- CopiedCount = $copiedCount
555
- DeletedCount = $deletedCount
556
- }
557
- }
558
-
559
- function Invoke-VerifyCommands {
560
- param(
561
- [string]$TargetRoot,
562
- [string[]]$Commands
563
- )
564
-
565
- foreach ($command in $Commands) {
566
- if ([string]::IsNullOrWhiteSpace($command)) {
567
- continue
568
- }
569
-
570
- Write-Step "运行验证命令: $command"
571
- Push-Location $TargetRoot
572
- try {
573
- powershell -NoProfile -Command $command
574
- if ($LASTEXITCODE -ne 0) {
575
- throw "验证命令失败: $command"
576
- }
577
- }
578
- finally {
579
- Pop-Location
580
- }
581
- }
582
- }
583
-
584
- $resolvedSourceRoot = (Resolve-Path -LiteralPath $SourceRoot).Path
585
- $resolvedConfigPath = if (Test-Path -LiteralPath $ConfigPath) { (Resolve-Path -LiteralPath $ConfigPath).Path } else { $ConfigPath }
586
- $exampleConfigPath = Join-Path $PSScriptRoot "sync-targets.example.json"
587
-
588
- if (-not (Test-Path -LiteralPath $resolvedConfigPath -PathType Leaf)) {
589
- throw "找不到 sync targets 配置文件: $resolvedConfigPath。可以先复制示例: $exampleConfigPath"
590
- }
591
-
592
- if (-not $AllowDirtySource.IsPresent) {
593
- $sourceStatus = Get-GitStatusLines -RepositoryPath $resolvedSourceRoot
594
- if (@($sourceStatus).Count -gt 0) {
595
- throw "源仓库存在未提交改动。请先提交/清理,或显式传入 -AllowDirtySource。"
596
- }
597
- }
598
-
599
- $fanoutConfig = Read-JsonConfig -Path $resolvedConfigPath -Label "sync-targets.json"
600
- $defaults = Get-OptionalPropertyValue -Object $fanoutConfig -Name "defaults"
601
- if ($null -eq $defaults) {
602
- $defaults = [pscustomobject]@{}
603
- }
604
- $sourceConfig = Get-OptionalPropertyValue -Object $fanoutConfig -Name "source"
605
- $configuredSourceRepo = [string](Get-OptionalPropertyValue -Object $sourceConfig -Name "repo")
606
- $configuredSourceRef = [string](Get-OptionalPropertyValue -Object $sourceConfig -Name "ref")
607
- $sourceRepo = if (-not [string]::IsNullOrWhiteSpace($configuredSourceRepo)) { $configuredSourceRepo } else { Get-DefaultSourceRepo -RepositoryPath $resolvedSourceRoot }
608
- $sourceRef = if (-not [string]::IsNullOrWhiteSpace($configuredSourceRef)) { $configuredSourceRef } else { Get-GitCurrentBranch -RepositoryPath $resolvedSourceRoot }
609
- $sourceCommit = Get-GitHeadCommit -RepositoryPath $resolvedSourceRoot
610
-
611
- if ([string]::IsNullOrWhiteSpace($sourceRepo) -or [string]::IsNullOrWhiteSpace($sourceRef)) {
612
- throw "sync-targets.json 必须提供 source.repo/source.ref,或者当前源仓库必须能推导出 origin/current branch。"
613
- }
614
-
615
- $defaultsWorkingRoot = [string](Get-OptionalPropertyValue -Object $defaults -Name "workingRoot")
616
- $workingRoot = if (-not [string]::IsNullOrWhiteSpace($defaultsWorkingRoot)) {
617
- [System.IO.Path]::GetFullPath([Environment]::ExpandEnvironmentVariables($defaultsWorkingRoot))
618
- }
619
- else {
620
- Join-Path ([System.IO.Path]::GetTempPath()) ("ai-sync-targets-{0}" -f (Get-Date -Format "yyyyMMdd-HHmmss"))
621
- }
622
-
623
- $fanoutTargets = Get-OptionalPropertyValue -Object $fanoutConfig -Name "targets"
624
- [object[]]$targetList = if ($null -ne $fanoutTargets) { @($fanoutTargets) } else { @() }
625
- if (@($OnlyTargets).Count -gt 0) {
626
- $filtered = New-Object System.Collections.Generic.List[object]
627
- foreach ($target in $targetList) {
628
- $keys = @(
629
- [string](Get-OptionalPropertyValue -Object $target -Name "repo"),
630
- [string](Get-OptionalPropertyValue -Object $target -Name "localPath"),
631
- [string](Get-OptionalPropertyValue -Object $target -Name "name")
632
- )
633
- foreach ($needle in $OnlyTargets) {
634
- if ($keys -contains $needle) {
635
- $filtered.Add($target)
636
- break
637
- }
638
- }
639
- }
640
- $targetList = @($filtered.ToArray())
641
- }
642
-
643
- if (@($targetList).Count -eq 0) {
644
- throw "没有可执行的目标仓库。"
645
- }
646
-
647
- $results = New-Object System.Collections.Generic.List[object]
648
-
649
- foreach ($target in $targetList) {
650
- $targetRepoValue = [string](Get-OptionalPropertyValue -Object $target -Name "repo")
651
- $targetLocalPathValue = [string](Get-OptionalPropertyValue -Object $target -Name "localPath")
652
- $targetNameValue = [string](Get-OptionalPropertyValue -Object $target -Name "name")
653
- $targetRepoDisplay = if (-not [string]::IsNullOrWhiteSpace($targetRepoValue)) { $targetRepoValue } elseif (-not [string]::IsNullOrWhiteSpace($targetNameValue)) { $targetNameValue } else { $targetLocalPathValue }
654
- Write-Step "处理目标仓库: $targetRepoDisplay"
655
-
656
- $targetRoot = Resolve-TargetPath -Target $target -RunRoot $workingRoot
657
- if (-not (Test-GitRepository -RepositoryPath $targetRoot)) {
658
- throw "目标路径不是 Git 仓库: $targetRoot"
659
- }
660
-
661
- $targetRepo = Resolve-TargetRepoSlug -TargetRepo $targetRepoValue -RepositoryPath $targetRoot
662
- $targetBaseBranch = [string](Get-OptionalPropertyValue -Object $target -Name "baseBranch")
663
- $defaultBaseBranch = [string](Get-OptionalPropertyValue -Object $defaults -Name "baseBranch")
664
- $baseBranch = if (-not [string]::IsNullOrWhiteSpace($targetBaseBranch)) { $targetBaseBranch } elseif (-not [string]::IsNullOrWhiteSpace($defaultBaseBranch)) { $defaultBaseBranch } else { "main" }
665
- $targetBranchPrefix = [string](Get-OptionalPropertyValue -Object $target -Name "branchPrefix")
666
- $defaultBranchPrefix = [string](Get-OptionalPropertyValue -Object $defaults -Name "branchPrefix")
667
- $branchPrefix = if (-not [string]::IsNullOrWhiteSpace($targetBranchPrefix)) { $targetBranchPrefix } elseif (-not [string]::IsNullOrWhiteSpace($defaultBranchPrefix)) { $defaultBranchPrefix } else { "chore/ai-kit-sync" }
668
- $targetSyncConfigPath = [string](Get-OptionalPropertyValue -Object $target -Name "syncConfigPath")
669
- $defaultSyncConfigPath = [string](Get-OptionalPropertyValue -Object $defaults -Name "syncConfigPath")
670
- $syncConfigPath = if (-not [string]::IsNullOrWhiteSpace($targetSyncConfigPath)) { $targetSyncConfigPath } elseif (-not [string]::IsNullOrWhiteSpace($defaultSyncConfigPath)) { $defaultSyncConfigPath } else { ".ai-sync.yml" }
671
- $targetPushBranch = Get-OptionalPropertyValue -Object $target -Name "pushBranch"
672
- $defaultPushBranch = Get-OptionalPropertyValue -Object $defaults -Name "pushBranch"
673
- $pushBranch = if ($NoPush.IsPresent) { $false } elseif ($null -ne $targetPushBranch) { [bool]$targetPushBranch } elseif ($null -ne $defaultPushBranch) { [bool]$defaultPushBranch } else { $true }
674
- $targetCreatePullRequest = Get-OptionalPropertyValue -Object $target -Name "createPullRequest"
675
- $defaultCreatePullRequest = Get-OptionalPropertyValue -Object $defaults -Name "createPullRequest"
676
- $createPullRequest = if ($NoPullRequest.IsPresent) { $false } elseif ($null -ne $targetCreatePullRequest) { [bool]$targetCreatePullRequest } elseif ($null -ne $defaultCreatePullRequest) { [bool]$defaultCreatePullRequest } else { $true }
677
- $targetDraftPullRequest = Get-OptionalPropertyValue -Object $target -Name "draftPullRequest"
678
- $defaultDraftPullRequest = Get-OptionalPropertyValue -Object $defaults -Name "draftPullRequest"
679
- $draftPullRequest = if ($null -ne $targetDraftPullRequest) { [bool]$targetDraftPullRequest } elseif ($null -ne $defaultDraftPullRequest) { [bool]$defaultDraftPullRequest } else { $false }
680
- $targetVerifyCommands = Get-OptionalPropertyValue -Object $target -Name "verifyCommands"
681
- $defaultVerifyCommands = Get-OptionalPropertyValue -Object $defaults -Name "verifyCommands"
682
- $verifyCommands = if ($null -ne $targetVerifyCommands) { @($targetVerifyCommands) } elseif ($null -ne $defaultVerifyCommands) { @($defaultVerifyCommands) } else { @() }
683
- $targetLabels = Get-OptionalPropertyValue -Object $target -Name "labels"
684
- $defaultLabels = Get-OptionalPropertyValue -Object $defaults -Name "labels"
685
- $labels = if ($null -ne $targetLabels) { @($targetLabels) } elseif ($null -ne $defaultLabels) { @($defaultLabels) } else { @() }
686
- $targetCommitMessage = [string](Get-OptionalPropertyValue -Object $target -Name "commitMessage")
687
- $defaultCommitMessage = [string](Get-OptionalPropertyValue -Object $defaults -Name "commitMessage")
688
- $commitMessage = if (-not [string]::IsNullOrWhiteSpace($targetCommitMessage)) { $targetCommitMessage } elseif (-not [string]::IsNullOrWhiteSpace($defaultCommitMessage)) { $defaultCommitMessage } else { "chore(ai-kit): sync shared AI workflow config" }
689
- $targetPrTitle = [string](Get-OptionalPropertyValue -Object $target -Name "prTitle")
690
- $defaultPrTitle = [string](Get-OptionalPropertyValue -Object $defaults -Name "prTitle")
691
- $prTitleTemplate = if (-not [string]::IsNullOrWhiteSpace($targetPrTitle)) { $targetPrTitle } elseif (-not [string]::IsNullOrWhiteSpace($defaultPrTitle)) { $defaultPrTitle } else { "chore(ai-kit): sync shared AI workflow config from {{sourceRef}}" }
692
- $targetPrBody = [string](Get-OptionalPropertyValue -Object $target -Name "prBody")
693
- $defaultPrBody = [string](Get-OptionalPropertyValue -Object $defaults -Name "prBody")
694
- $prBodyTemplate = if (-not [string]::IsNullOrWhiteSpace($targetPrBody)) { $targetPrBody } elseif (-not [string]::IsNullOrWhiteSpace($defaultPrBody)) { $defaultPrBody } else { "## AI Workflow Kit Sync`n`nSource: {{sourceRepo}}@{{sourceRef}}`nCommit: {{sourceCommit}}`n`nChanged files:`n{{changedFiles}}`n" }
695
-
696
- $preStatus = Get-GitStatusLines -RepositoryPath $targetRoot
697
- if (@($preStatus).Count -gt 0) {
698
- throw "目标仓库存在未提交改动,拒绝自动同步: $targetRoot"
699
- }
700
-
701
- $hasOrigin = $true
702
- try {
703
- Invoke-Git -RepositoryPath $targetRoot -Arguments @("remote", "get-url", "origin") | Out-Null
704
- }
705
- catch {
706
- $hasOrigin = $false
707
- }
708
-
709
- if ($hasOrigin) {
710
- Invoke-Git -RepositoryPath $targetRoot -Arguments @("fetch", "origin", "--prune") | Out-Host
711
- }
712
-
713
- Invoke-Git -RepositoryPath $targetRoot -Arguments @("checkout", $baseBranch) | Out-Host
714
- if ($hasOrigin) {
715
- Invoke-Git -RepositoryPath $targetRoot -Arguments @("pull", "--ff-only", "origin", $baseBranch) | Out-Host
716
- }
717
-
718
- $branchName = "{0}-{1}" -f $branchPrefix.TrimEnd("/"), (Get-Date -Format "yyyyMMdd-HHmmss")
719
- Invoke-Git -RepositoryPath $targetRoot -Arguments @("switch", "-c", $branchName) | Out-Host
720
-
721
- $syncResult = Sync-TargetRepository `
722
- -SourceRoot $resolvedSourceRoot `
723
- -SourceRepo $sourceRepo `
724
- -SourceRef $sourceRef `
725
- -SourceCommit $sourceCommit `
726
- -TargetRoot $targetRoot `
727
- -SyncConfigPath $syncConfigPath `
728
- -PullRequestMode:($pushBranch -and $createPullRequest)
729
-
730
- if (@($verifyCommands).Count -gt 0) {
731
- Invoke-VerifyCommands -TargetRoot $targetRoot -Commands $verifyCommands
732
- }
733
-
734
- $postStatus = Get-GitStatusLines -RepositoryPath $targetRoot
735
- if (@($postStatus).Count -eq 0) {
736
- $results.Add([pscustomobject]@{
737
- Target = $targetRepoDisplay
738
- Branch = $branchName
739
- Status = "NO_CHANGES"
740
- PullRequest = ""
741
- })
742
- continue
743
- }
744
-
745
- Invoke-Git -RepositoryPath $targetRoot -Arguments @("add", "--all") | Out-Host
746
- $changedFiles = @(Invoke-Git -RepositoryPath $targetRoot -Arguments @("diff", "--cached", "--name-only"))
747
- $changedFilesMarkdown = if (@($changedFiles).Count -gt 0) {
748
- ($changedFiles | ForEach-Object { "- {0}" -f $_ }) -join [Environment]::NewLine
749
- }
750
- else {
751
- "- none"
752
- }
753
-
754
- $variables = @{
755
- sourceRepo = $sourceRepo
756
- sourceRef = $sourceRef
757
- sourceCommit = $sourceCommit
758
- targetRepo = $targetRepo
759
- branchName = $branchName
760
- changedFiles = $changedFilesMarkdown
761
- }
762
-
763
- $commitBody = Resolve-TemplateValue -Template "Source: {{sourceRepo}}@{{sourceRef}}`nSource-Commit: {{sourceCommit}}" -Variables $variables
764
- $commitMessageFile = Join-Path ([System.IO.Path]::GetTempPath()) ("ai-sync-commit-{0}.txt" -f ([guid]::NewGuid().ToString("N")))
765
- [System.IO.File]::WriteAllText($commitMessageFile, $commitMessage + [Environment]::NewLine + [Environment]::NewLine + $commitBody + [Environment]::NewLine, [System.Text.UTF8Encoding]::new($false))
766
- try {
767
- Invoke-Git -RepositoryPath $targetRoot -Arguments @("commit", "-F", $commitMessageFile) | Out-Host
768
- }
769
- finally {
770
- Remove-Item -LiteralPath $commitMessageFile -Force -ErrorAction SilentlyContinue
771
- }
772
-
773
- $prUrl = ""
774
- if ($pushBranch) {
775
- if (-not $hasOrigin) {
776
- throw "目标仓库没有 origin,无法 push: $targetRoot"
777
- }
778
-
779
- Invoke-Git -RepositoryPath $targetRoot -Arguments @("push", "-u", "origin", $branchName) | Out-Host
780
- }
781
-
782
- if ($pushBranch -and $createPullRequest) {
783
- if (-not (Get-Command gh -ErrorAction SilentlyContinue)) {
784
- throw "未找到 gh CLI,无法创建 Pull Request。"
785
- }
786
-
787
- $prTitle = Resolve-TemplateValue -Template $prTitleTemplate -Variables $variables
788
- $prBody = Resolve-TemplateValue -Template $prBodyTemplate -Variables $variables
789
- $prBodyFile = Join-Path ([System.IO.Path]::GetTempPath()) ("ai-sync-pr-{0}.md" -f ([guid]::NewGuid().ToString("N")))
790
- [System.IO.File]::WriteAllText($prBodyFile, $prBody, [System.Text.UTF8Encoding]::new($false))
791
- try {
792
- $prArgs = @("pr", "create", "--repo", $targetRepo, "--base", $baseBranch, "--head", $branchName, "--title", $prTitle, "--body-file", $prBodyFile)
793
- if ($draftPullRequest) {
794
- $prArgs += "--draft"
795
- }
796
- $prOutput = & gh @prArgs
797
- if ($LASTEXITCODE -ne 0) {
798
- throw "gh pr create 失败: $targetRepo"
799
- }
800
- $prUrl = ($prOutput | Select-Object -Last 1).ToString().Trim()
801
-
802
- if (@($labels).Count -gt 0) {
803
- & gh pr edit $prUrl --add-label (($labels | ForEach-Object { [string]$_ }) -join ",") | Out-Host
804
- if ($LASTEXITCODE -ne 0) {
805
- throw "gh pr edit --add-label 失败: $prUrl"
806
- }
807
- }
808
- }
809
- finally {
810
- Remove-Item -LiteralPath $prBodyFile -Force -ErrorAction SilentlyContinue
811
- }
812
- }
813
-
814
- $results.Add([pscustomobject]@{
815
- Target = $targetRepoDisplay
816
- Branch = $branchName
817
- Status = "SYNCED"
818
- PullRequest = $prUrl
819
- })
820
- }
821
-
822
- Write-Output ""
823
- Write-Output "AI sync fan-out summary:"
824
- foreach ($result in $results) {
825
- Write-Output ("- {0} [{1}] branch={2}{3}" -f $result.Target, $result.Status, $result.Branch, $(if ([string]::IsNullOrWhiteSpace($result.PullRequest)) { "" } else { " pr=" + $result.PullRequest }))
826
- }
1
+ param(
2
+ [string]$ConfigPath = "",
3
+ [string]$SourceRoot = "",
4
+ [string[]]$OnlyTargets = @(),
5
+ [switch]$NoPush,
6
+ [switch]$NoPullRequest,
7
+ [switch]$AllowDirtySource
8
+ )
9
+
10
+ Set-StrictMode -Version Latest
11
+ $ErrorActionPreference = "Stop"
12
+
13
+ if ([string]::IsNullOrWhiteSpace($SourceRoot)) {
14
+ $SourceRoot = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot "..\.."))
15
+ }
16
+
17
+ if ([string]::IsNullOrWhiteSpace($ConfigPath)) {
18
+ $ConfigPath = Join-Path $PSScriptRoot "sync-targets.json"
19
+ }
20
+
21
+ function Write-Step {
22
+ param([string]$Message)
23
+ Write-Host ("[ai-sync-targets] {0}" -f $Message)
24
+ }
25
+
26
+ function Get-OptionalPropertyValue {
27
+ param(
28
+ [object]$Object,
29
+ [string]$Name
30
+ )
31
+
32
+ if ($null -eq $Object) {
33
+ return $null
34
+ }
35
+
36
+ $property = $Object.PSObject.Properties[$Name]
37
+ if ($null -eq $property) {
38
+ return $null
39
+ }
40
+
41
+ return $property.Value
42
+ }
43
+
44
+ function Set-OptionalPropertyValue {
45
+ param(
46
+ [object]$Object,
47
+ [string]$Name,
48
+ [object]$Value
49
+ )
50
+
51
+ $property = $Object.PSObject.Properties[$Name]
52
+ if ($null -eq $property) {
53
+ $Object | Add-Member -NotePropertyName $Name -NotePropertyValue $Value -Force
54
+ return
55
+ }
56
+
57
+ $property.Value = $Value
58
+ }
59
+
60
+ function Normalize-RelativePath {
61
+ param(
62
+ [Parameter(Mandatory = $true)]
63
+ [string]$Path,
64
+ [switch]$Directory
65
+ )
66
+
67
+ $normalized = $Path.Replace("\", "/").Trim()
68
+ while ($normalized.StartsWith("./", [System.StringComparison]::Ordinal)) {
69
+ $normalized = $normalized.Substring(2)
70
+ }
71
+
72
+ if ($Directory) {
73
+ return $normalized.TrimEnd("/") + "/"
74
+ }
75
+
76
+ return $normalized.TrimEnd("/")
77
+ }
78
+
79
+ function Join-NormalizedRelativePath {
80
+ param(
81
+ [string]$Base,
82
+ [string]$Child
83
+ )
84
+
85
+ $left = Normalize-RelativePath -Path $Base
86
+ $right = Normalize-RelativePath -Path $Child
87
+ if ([string]::IsNullOrWhiteSpace($left)) {
88
+ return $right
89
+ }
90
+ if ([string]::IsNullOrWhiteSpace($right)) {
91
+ return $left
92
+ }
93
+
94
+ return "{0}/{1}" -f $left.TrimEnd("/"), $right
95
+ }
96
+
97
+ function Get-RelativePathFromBase {
98
+ param(
99
+ [string]$BasePath,
100
+ [string]$TargetPath
101
+ )
102
+
103
+ $baseFull = [System.IO.Path]::GetFullPath($BasePath).TrimEnd("\", "/") + [System.IO.Path]::DirectorySeparatorChar
104
+ $targetFull = [System.IO.Path]::GetFullPath($TargetPath)
105
+ if (-not $targetFull.StartsWith($baseFull, [System.StringComparison]::OrdinalIgnoreCase)) {
106
+ throw "路径不在预期根目录内: $TargetPath"
107
+ }
108
+
109
+ return Normalize-RelativePath -Path $targetFull.Substring($baseFull.Length)
110
+ }
111
+
112
+ function Read-JsonConfig {
113
+ param(
114
+ [string]$Path,
115
+ [string]$Label
116
+ )
117
+
118
+ if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) {
119
+ throw "找不到 ${Label}: $Path"
120
+ }
121
+
122
+ try {
123
+ return Get-Content -LiteralPath $Path -Raw | ConvertFrom-Json
124
+ }
125
+ catch {
126
+ throw "${Label} 需要使用 JSON 兼容格式,当前文件无法用 ConvertFrom-Json 解析: $Path"
127
+ }
128
+ }
129
+
130
+ function Write-JsonFile {
131
+ param(
132
+ [string]$Path,
133
+ [object]$Value
134
+ )
135
+
136
+ $parent = Split-Path -Parent $Path
137
+ if (-not (Test-Path -LiteralPath $parent)) {
138
+ New-Item -ItemType Directory -Force -Path $parent | Out-Null
139
+ }
140
+
141
+ $json = $Value | ConvertTo-Json -Depth 10
142
+ [System.IO.File]::WriteAllText($Path, $json + [Environment]::NewLine, [System.Text.UTF8Encoding]::new($false))
143
+ }
144
+
145
+ function Get-FileSha256 {
146
+ param([string]$Path)
147
+ return (Get-FileHash -LiteralPath $Path -Algorithm SHA256).Hash.ToLowerInvariant()
148
+ }
149
+
150
+ function Quote-ProcessArgument {
151
+ param([string]$Value)
152
+
153
+ if ($null -eq $Value -or $Value.Length -eq 0) {
154
+ return '""'
155
+ }
156
+
157
+ if ($Value -notmatch '[\s"]') {
158
+ return $Value
159
+ }
160
+
161
+ $escaped = $Value -replace '(\\*)"', '$1$1\"'
162
+ $escaped = $escaped -replace '(\\+)$', '$1$1'
163
+ return '"' + $escaped + '"'
164
+ }
165
+
166
+ function Invoke-Git {
167
+ param(
168
+ [string]$RepositoryPath,
169
+ [string[]]$Arguments
170
+ )
171
+
172
+ $stdoutPath = Join-Path ([System.IO.Path]::GetTempPath()) ("git-stdout-{0}.log" -f ([guid]::NewGuid().ToString("N")))
173
+ $stderrPath = Join-Path ([System.IO.Path]::GetTempPath()) ("git-stderr-{0}.log" -f ([guid]::NewGuid().ToString("N")))
174
+ try {
175
+ $argumentString = ((@("-C", $RepositoryPath) + $Arguments) | ForEach-Object { Quote-ProcessArgument -Value ([string]$_) }) -join " "
176
+ $process = Start-Process `
177
+ -FilePath "git" `
178
+ -ArgumentList $argumentString `
179
+ -NoNewWindow `
180
+ -Wait `
181
+ -PassThru `
182
+ -RedirectStandardOutput $stdoutPath `
183
+ -RedirectStandardError $stderrPath
184
+
185
+ $output = New-Object System.Collections.Generic.List[string]
186
+ if (Test-Path -LiteralPath $stdoutPath -PathType Leaf) {
187
+ foreach ($line in Get-Content -LiteralPath $stdoutPath) {
188
+ $output.Add([string]$line)
189
+ }
190
+ }
191
+ if (Test-Path -LiteralPath $stderrPath -PathType Leaf) {
192
+ foreach ($line in Get-Content -LiteralPath $stderrPath) {
193
+ $output.Add([string]$line)
194
+ }
195
+ }
196
+
197
+ if ($process.ExitCode -ne 0) {
198
+ $message = if ($output.Count -gt 0) { ($output -join [Environment]::NewLine) } else { "git command failed" }
199
+ throw "git $($Arguments -join ' ') failed in $RepositoryPath`n$message"
200
+ }
201
+
202
+ return [string[]]$output.ToArray()
203
+ }
204
+ finally {
205
+ if (Test-Path -LiteralPath $stdoutPath) {
206
+ Remove-Item -LiteralPath $stdoutPath -Force -ErrorAction SilentlyContinue
207
+ }
208
+ if (Test-Path -LiteralPath $stderrPath) {
209
+ Remove-Item -LiteralPath $stderrPath -Force -ErrorAction SilentlyContinue
210
+ }
211
+ }
212
+ }
213
+
214
+ function Get-GitStatusLines {
215
+ param([string]$RepositoryPath)
216
+ return @(Invoke-Git -RepositoryPath $RepositoryPath -Arguments @("status", "--porcelain"))
217
+ }
218
+
219
+ function Get-GitCurrentBranch {
220
+ param([string]$RepositoryPath)
221
+ $output = @(Invoke-Git -RepositoryPath $RepositoryPath -Arguments @("branch", "--show-current"))
222
+ if (@($output).Count -eq 0) {
223
+ return ""
224
+ }
225
+
226
+ return [string]$output[0]
227
+ }
228
+
229
+ function Get-GitHeadCommit {
230
+ param([string]$RepositoryPath)
231
+ $output = @(Invoke-Git -RepositoryPath $RepositoryPath -Arguments @("rev-parse", "HEAD"))
232
+ return [string]$output[0]
233
+ }
234
+
235
+ function Test-GitRepository {
236
+ param([string]$RepositoryPath)
237
+ try {
238
+ Invoke-Git -RepositoryPath $RepositoryPath -Arguments @("rev-parse", "--show-toplevel") | Out-Null
239
+ return $true
240
+ }
241
+ catch {
242
+ return $false
243
+ }
244
+ }
245
+
246
+ function Expand-ManagedEntries {
247
+ param(
248
+ [object]$Config,
249
+ [string]$SourceRoot,
250
+ [string]$DestinationRoot
251
+ )
252
+
253
+ $entries = New-Object System.Collections.Generic.List[object]
254
+ $directoryPlans = New-Object System.Collections.Generic.List[object]
255
+ $destinationSet = @{}
256
+ $sourceHashSet = @{}
257
+
258
+ $managedEntries = Get-OptionalPropertyValue -Object $Config -Name "managed"
259
+ foreach ($mapping in @($managedEntries)) {
260
+ $mappingTypeValue = [string](Get-OptionalPropertyValue -Object $mapping -Name "type")
261
+ $mappingType = if ([string]::IsNullOrWhiteSpace($mappingTypeValue)) { "directory" } else { $mappingTypeValue }
262
+ $sourceRelative = Normalize-RelativePath -Path ([string](Get-OptionalPropertyValue -Object $mapping -Name "source")) -Directory:($mappingType -eq "directory")
263
+ $destinationRelative = Normalize-RelativePath -Path ([string](Get-OptionalPropertyValue -Object $mapping -Name "dest")) -Directory:($mappingType -eq "directory")
264
+ $sourcePath = Join-Path $SourceRoot $sourceRelative
265
+
266
+ if ($mappingType -eq "file") {
267
+ if (-not (Test-Path -LiteralPath $sourcePath -PathType Leaf)) {
268
+ throw "managed source file 不存在: $sourceRelative"
269
+ }
270
+
271
+ if ($destinationSet.ContainsKey($destinationRelative)) {
272
+ throw "发现重复的 managed destination: $destinationRelative"
273
+ }
274
+ $destinationSet[$destinationRelative] = $true
275
+ $sourceHashSet[$sourceRelative] = $true
276
+
277
+ $entries.Add([pscustomobject]@{
278
+ SourceRelative = $sourceRelative
279
+ SourcePath = $sourcePath
280
+ DestinationRelative = $destinationRelative
281
+ DestinationPath = Join-Path $DestinationRoot $destinationRelative
282
+ })
283
+ continue
284
+ }
285
+
286
+ if (-not (Test-Path -LiteralPath $sourcePath -PathType Container)) {
287
+ throw "managed source directory 不存在: $sourceRelative"
288
+ }
289
+
290
+ $expectedDestinations = @{}
291
+ foreach ($file in Get-ChildItem -LiteralPath $sourcePath -Recurse -File) {
292
+ $childRelative = Get-RelativePathFromBase -BasePath $sourcePath -TargetPath $file.FullName
293
+ $entrySourceRelative = Join-NormalizedRelativePath -Base $sourceRelative -Child $childRelative
294
+ $entryDestinationRelative = Join-NormalizedRelativePath -Base $destinationRelative -Child $childRelative
295
+
296
+ if ($destinationSet.ContainsKey($entryDestinationRelative)) {
297
+ throw "发现重复的 managed destination: $entryDestinationRelative"
298
+ }
299
+
300
+ $destinationSet[$entryDestinationRelative] = $true
301
+ $expectedDestinations[$entryDestinationRelative] = $true
302
+ $sourceHashSet[$entrySourceRelative] = $true
303
+
304
+ $entries.Add([pscustomobject]@{
305
+ SourceRelative = $entrySourceRelative
306
+ SourcePath = Join-Path $SourceRoot $entrySourceRelative
307
+ DestinationRelative = $entryDestinationRelative
308
+ DestinationPath = Join-Path $DestinationRoot $entryDestinationRelative
309
+ })
310
+ }
311
+
312
+ $directoryPlans.Add([pscustomobject]@{
313
+ SourceRelative = $sourceRelative
314
+ DestinationRelative = $destinationRelative
315
+ DestinationRootPath = Join-Path $DestinationRoot $destinationRelative
316
+ ExpectedDestinations = $expectedDestinations
317
+ })
318
+ }
319
+
320
+ return [pscustomobject]@{
321
+ Entries = [object[]]$entries.ToArray()
322
+ DirectoryPlans = [object[]]$directoryPlans.ToArray()
323
+ SourceHashKeys = @($sourceHashSet.Keys | Sort-Object)
324
+ }
325
+ }
326
+
327
+ function Remove-EmptyDirectories {
328
+ param([string]$RootPath)
329
+
330
+ if (-not (Test-Path -LiteralPath $RootPath -PathType Container)) {
331
+ return
332
+ }
333
+
334
+ $directories = Get-ChildItem -LiteralPath $RootPath -Recurse -Directory | Sort-Object FullName -Descending
335
+ foreach ($directory in $directories) {
336
+ if ((Get-ChildItem -LiteralPath $directory.FullName -Force | Measure-Object).Count -eq 0) {
337
+ Remove-Item -LiteralPath $directory.FullName -Force
338
+ }
339
+ }
340
+ }
341
+
342
+ function Get-DirectoryExtraFiles {
343
+ param(
344
+ [object]$DirectoryPlan,
345
+ [string]$TargetRoot
346
+ )
347
+
348
+ if (-not (Test-Path -LiteralPath $DirectoryPlan.DestinationRootPath -PathType Container)) {
349
+ return @()
350
+ }
351
+
352
+ $extras = New-Object System.Collections.Generic.List[object]
353
+ foreach ($file in Get-ChildItem -LiteralPath $DirectoryPlan.DestinationRootPath -Recurse -File) {
354
+ $relative = Get-RelativePathFromBase -BasePath $TargetRoot -TargetPath $file.FullName
355
+ if (-not $DirectoryPlan.ExpectedDestinations.ContainsKey($relative)) {
356
+ $extras.Add([pscustomobject]@{
357
+ RelativePath = $relative
358
+ FullPath = $file.FullName
359
+ })
360
+ }
361
+ }
362
+
363
+ return [object[]]$extras.ToArray()
364
+ }
365
+
366
+ function Get-DefaultSourceRepo {
367
+ param([string]$RepositoryPath)
368
+
369
+ try {
370
+ $remoteUrl = [string](@(Invoke-Git -RepositoryPath $RepositoryPath -Arguments @("remote", "get-url", "origin"))[0])
371
+ }
372
+ catch {
373
+ return ""
374
+ }
375
+
376
+ if ($remoteUrl -match 'github\.com[:/](.+?)(?:\.git)?$') {
377
+ return $Matches[1]
378
+ }
379
+
380
+ return $remoteUrl
381
+ }
382
+
383
+ function Resolve-TargetRepoSlug {
384
+ param(
385
+ [string]$TargetRepo,
386
+ [string]$RepositoryPath
387
+ )
388
+
389
+ if (-not [string]::IsNullOrWhiteSpace($TargetRepo)) {
390
+ return $TargetRepo
391
+ }
392
+
393
+ return Get-DefaultSourceRepo -RepositoryPath $RepositoryPath
394
+ }
395
+
396
+ function Resolve-TargetPath {
397
+ param(
398
+ [object]$Target,
399
+ [string]$RunRoot
400
+ )
401
+
402
+ $localPath = [string](Get-OptionalPropertyValue -Object $Target -Name "localPath")
403
+ if (-not [string]::IsNullOrWhiteSpace($localPath)) {
404
+ return [System.IO.Path]::GetFullPath($localPath)
405
+ }
406
+
407
+ $targetRepo = [string](Get-OptionalPropertyValue -Object $Target -Name "repo")
408
+ if ([string]::IsNullOrWhiteSpace($targetRepo)) {
409
+ throw "target 必须提供 repo 或 localPath。"
410
+ }
411
+
412
+ $repoKey = $targetRepo.Replace("/", "__")
413
+ $checkoutPath = Join-Path $RunRoot $repoKey
414
+ $checkoutParent = Split-Path -Parent $checkoutPath
415
+ if (-not (Test-Path -LiteralPath $checkoutParent)) {
416
+ New-Item -ItemType Directory -Force -Path $checkoutParent | Out-Null
417
+ }
418
+
419
+ if (Test-Path -LiteralPath $checkoutPath) {
420
+ throw "临时 checkout 目录已存在: $checkoutPath"
421
+ }
422
+
423
+ if (Get-Command gh -ErrorAction SilentlyContinue) {
424
+ Write-Step "克隆目标仓库 $targetRepo -> $checkoutPath"
425
+ & gh repo clone $targetRepo $checkoutPath | Out-Host
426
+ if ($LASTEXITCODE -ne 0) {
427
+ throw "gh repo clone 失败: $targetRepo"
428
+ }
429
+ return $checkoutPath
430
+ }
431
+
432
+ $cloneUrl = "https://github.com/{0}.git" -f $targetRepo
433
+ Write-Step "克隆目标仓库 $cloneUrl -> $checkoutPath"
434
+ & git clone $cloneUrl $checkoutPath | Out-Host
435
+ if ($LASTEXITCODE -ne 0) {
436
+ throw "git clone 失败: $cloneUrl"
437
+ }
438
+
439
+ return $checkoutPath
440
+ }
441
+
442
+ function Resolve-TemplateValue {
443
+ param(
444
+ [string]$Template,
445
+ [hashtable]$Variables
446
+ )
447
+
448
+ $result = $Template
449
+ foreach ($key in $Variables.Keys) {
450
+ $result = $result.Replace(("{{{{{0}}}}}" -f $key), [string]$Variables[$key])
451
+ }
452
+
453
+ return $result
454
+ }
455
+
456
+ function Sync-TargetRepository {
457
+ param(
458
+ [string]$SourceRoot,
459
+ [string]$SourceRepo,
460
+ [string]$SourceRef,
461
+ [string]$SourceCommit,
462
+ [string]$TargetRoot,
463
+ [string]$SyncConfigPath,
464
+ [bool]$PullRequestMode
465
+ )
466
+
467
+ $targetConfigFile = Join-Path $TargetRoot $SyncConfigPath
468
+ $targetConfig = Read-JsonConfig -Path $targetConfigFile -Label ".ai-sync.yml"
469
+ $targetConfigChanged = $false
470
+
471
+ $targetSource = Get-OptionalPropertyValue -Object $targetConfig -Name "source"
472
+ if ($null -eq $targetSource) {
473
+ $targetSource = [pscustomobject]@{}
474
+ Set-OptionalPropertyValue -Object $targetConfig -Name "source" -Value $targetSource
475
+ }
476
+
477
+ if ([string](Get-OptionalPropertyValue -Object $targetSource -Name "repo") -ne $SourceRepo) {
478
+ Set-OptionalPropertyValue -Object $targetSource -Name "repo" -Value $SourceRepo
479
+ $targetConfigChanged = $true
480
+ }
481
+
482
+ if ([string](Get-OptionalPropertyValue -Object $targetSource -Name "ref") -ne $SourceRef) {
483
+ Set-OptionalPropertyValue -Object $targetSource -Name "ref" -Value $SourceRef
484
+ $targetConfigChanged = $true
485
+ }
486
+
487
+ if ($targetConfigChanged) {
488
+ Write-JsonFile -Path $targetConfigFile -Value $targetConfig
489
+ }
490
+
491
+ $expansion = Expand-ManagedEntries -Config $targetConfig -SourceRoot $SourceRoot -DestinationRoot $TargetRoot
492
+ $copiedCount = 0
493
+ $deletedCount = 0
494
+
495
+ foreach ($entry in $expansion.Entries) {
496
+ $expectedHash = Get-FileSha256 -Path $entry.SourcePath
497
+ $actualHash = if (Test-Path -LiteralPath $entry.DestinationPath -PathType Leaf) { Get-FileSha256 -Path $entry.DestinationPath } else { $null }
498
+ if ($actualHash -eq $expectedHash) {
499
+ continue
500
+ }
501
+
502
+ $destinationParent = Split-Path -Parent $entry.DestinationPath
503
+ if (-not (Test-Path -LiteralPath $destinationParent)) {
504
+ New-Item -ItemType Directory -Force -Path $destinationParent | Out-Null
505
+ }
506
+
507
+ Copy-Item -LiteralPath $entry.SourcePath -Destination $entry.DestinationPath -Force
508
+ $copiedCount += 1
509
+ }
510
+
511
+ foreach ($directoryPlan in $expansion.DirectoryPlans) {
512
+ foreach ($extra in Get-DirectoryExtraFiles -DirectoryPlan $directoryPlan -TargetRoot $TargetRoot) {
513
+ Remove-Item -LiteralPath $extra.FullPath -Force
514
+ $deletedCount += 1
515
+ }
516
+ Remove-EmptyDirectories -RootPath $directoryPlan.DestinationRootPath
517
+ }
518
+
519
+ $manifestFiles = foreach ($sourceRelative in $expansion.SourceHashKeys) {
520
+ [pscustomobject]@{
521
+ source = $sourceRelative
522
+ sha256 = Get-FileSha256 -Path (Join-Path $SourceRoot $sourceRelative)
523
+ }
524
+ }
525
+
526
+ $manifestObject = [pscustomobject]@{
527
+ version = $SourceRef
528
+ commit = $SourceCommit
529
+ generatedAt = (Get-Date).ToString("yyyy-MM-ddTHH:mm:sszzz")
530
+ files = @($manifestFiles | Sort-Object source)
531
+ }
532
+
533
+ $manifestFile = Join-Path $TargetRoot "ai-sync\ai-kit-manifest.json"
534
+ Write-JsonFile -Path $manifestFile -Value $manifestObject
535
+ $manifestHash = Get-FileSha256 -Path $manifestFile
536
+
537
+ $lockObject = [pscustomobject]@{
538
+ source = [pscustomobject]@{
539
+ repo = $SourceRepo
540
+ ref = $SourceRef
541
+ commit = $SourceCommit
542
+ }
543
+ manifest = [pscustomobject]@{
544
+ path = "ai-sync/ai-kit-manifest.json"
545
+ sha256 = $manifestHash
546
+ }
547
+ syncedAt = (Get-Date).ToString("yyyy-MM-ddTHH:mm:sszzz")
548
+ mode = if ($PullRequestMode) { "pull-request-sync" } else { "manual-sync" }
549
+ }
550
+
551
+ Write-JsonFile -Path (Join-Path $TargetRoot ".ai-sync.lock.json") -Value $lockObject
552
+
553
+ return [pscustomobject]@{
554
+ CopiedCount = $copiedCount
555
+ DeletedCount = $deletedCount
556
+ }
557
+ }
558
+
559
+ function Invoke-VerifyCommands {
560
+ param(
561
+ [string]$TargetRoot,
562
+ [string[]]$Commands
563
+ )
564
+
565
+ foreach ($command in $Commands) {
566
+ if ([string]::IsNullOrWhiteSpace($command)) {
567
+ continue
568
+ }
569
+
570
+ Write-Step "运行验证命令: $command"
571
+ Push-Location $TargetRoot
572
+ try {
573
+ powershell -NoProfile -Command $command
574
+ if ($LASTEXITCODE -ne 0) {
575
+ throw "验证命令失败: $command"
576
+ }
577
+ }
578
+ finally {
579
+ Pop-Location
580
+ }
581
+ }
582
+ }
583
+
584
+ $resolvedSourceRoot = (Resolve-Path -LiteralPath $SourceRoot).Path
585
+ $resolvedConfigPath = if (Test-Path -LiteralPath $ConfigPath) { (Resolve-Path -LiteralPath $ConfigPath).Path } else { $ConfigPath }
586
+ $exampleConfigPath = Join-Path $PSScriptRoot "sync-targets.example.json"
587
+
588
+ if (-not (Test-Path -LiteralPath $resolvedConfigPath -PathType Leaf)) {
589
+ throw "找不到 sync targets 配置文件: $resolvedConfigPath。可以先复制示例: $exampleConfigPath"
590
+ }
591
+
592
+ if (-not $AllowDirtySource.IsPresent) {
593
+ $sourceStatus = Get-GitStatusLines -RepositoryPath $resolvedSourceRoot
594
+ if (@($sourceStatus).Count -gt 0) {
595
+ throw "源仓库存在未提交改动。请先提交/清理,或显式传入 -AllowDirtySource。"
596
+ }
597
+ }
598
+
599
+ $fanoutConfig = Read-JsonConfig -Path $resolvedConfigPath -Label "sync-targets.json"
600
+ $defaults = Get-OptionalPropertyValue -Object $fanoutConfig -Name "defaults"
601
+ if ($null -eq $defaults) {
602
+ $defaults = [pscustomobject]@{}
603
+ }
604
+ $sourceConfig = Get-OptionalPropertyValue -Object $fanoutConfig -Name "source"
605
+ $configuredSourceRepo = [string](Get-OptionalPropertyValue -Object $sourceConfig -Name "repo")
606
+ $configuredSourceRef = [string](Get-OptionalPropertyValue -Object $sourceConfig -Name "ref")
607
+ $sourceRepo = if (-not [string]::IsNullOrWhiteSpace($configuredSourceRepo)) { $configuredSourceRepo } else { Get-DefaultSourceRepo -RepositoryPath $resolvedSourceRoot }
608
+ $sourceRef = if (-not [string]::IsNullOrWhiteSpace($configuredSourceRef)) { $configuredSourceRef } else { Get-GitCurrentBranch -RepositoryPath $resolvedSourceRoot }
609
+ $sourceCommit = Get-GitHeadCommit -RepositoryPath $resolvedSourceRoot
610
+
611
+ if ([string]::IsNullOrWhiteSpace($sourceRepo) -or [string]::IsNullOrWhiteSpace($sourceRef)) {
612
+ throw "sync-targets.json 必须提供 source.repo/source.ref,或者当前源仓库必须能推导出 origin/current branch。"
613
+ }
614
+
615
+ $defaultsWorkingRoot = [string](Get-OptionalPropertyValue -Object $defaults -Name "workingRoot")
616
+ $workingRoot = if (-not [string]::IsNullOrWhiteSpace($defaultsWorkingRoot)) {
617
+ [System.IO.Path]::GetFullPath([Environment]::ExpandEnvironmentVariables($defaultsWorkingRoot))
618
+ }
619
+ else {
620
+ Join-Path ([System.IO.Path]::GetTempPath()) ("ai-sync-targets-{0}" -f (Get-Date -Format "yyyyMMdd-HHmmss"))
621
+ }
622
+
623
+ $fanoutTargets = Get-OptionalPropertyValue -Object $fanoutConfig -Name "targets"
624
+ [object[]]$targetList = if ($null -ne $fanoutTargets) { @($fanoutTargets) } else { @() }
625
+ if (@($OnlyTargets).Count -gt 0) {
626
+ $filtered = New-Object System.Collections.Generic.List[object]
627
+ foreach ($target in $targetList) {
628
+ $keys = @(
629
+ [string](Get-OptionalPropertyValue -Object $target -Name "repo"),
630
+ [string](Get-OptionalPropertyValue -Object $target -Name "localPath"),
631
+ [string](Get-OptionalPropertyValue -Object $target -Name "name")
632
+ )
633
+ foreach ($needle in $OnlyTargets) {
634
+ if ($keys -contains $needle) {
635
+ $filtered.Add($target)
636
+ break
637
+ }
638
+ }
639
+ }
640
+ $targetList = @($filtered.ToArray())
641
+ }
642
+
643
+ if (@($targetList).Count -eq 0) {
644
+ throw "没有可执行的目标仓库。"
645
+ }
646
+
647
+ $results = New-Object System.Collections.Generic.List[object]
648
+
649
+ foreach ($target in $targetList) {
650
+ $targetRepoValue = [string](Get-OptionalPropertyValue -Object $target -Name "repo")
651
+ $targetLocalPathValue = [string](Get-OptionalPropertyValue -Object $target -Name "localPath")
652
+ $targetNameValue = [string](Get-OptionalPropertyValue -Object $target -Name "name")
653
+ $targetRepoDisplay = if (-not [string]::IsNullOrWhiteSpace($targetRepoValue)) { $targetRepoValue } elseif (-not [string]::IsNullOrWhiteSpace($targetNameValue)) { $targetNameValue } else { $targetLocalPathValue }
654
+ Write-Step "处理目标仓库: $targetRepoDisplay"
655
+
656
+ $targetRoot = Resolve-TargetPath -Target $target -RunRoot $workingRoot
657
+ if (-not (Test-GitRepository -RepositoryPath $targetRoot)) {
658
+ throw "目标路径不是 Git 仓库: $targetRoot"
659
+ }
660
+
661
+ $targetRepo = Resolve-TargetRepoSlug -TargetRepo $targetRepoValue -RepositoryPath $targetRoot
662
+ $targetBaseBranch = [string](Get-OptionalPropertyValue -Object $target -Name "baseBranch")
663
+ $defaultBaseBranch = [string](Get-OptionalPropertyValue -Object $defaults -Name "baseBranch")
664
+ $baseBranch = if (-not [string]::IsNullOrWhiteSpace($targetBaseBranch)) { $targetBaseBranch } elseif (-not [string]::IsNullOrWhiteSpace($defaultBaseBranch)) { $defaultBaseBranch } else { "main" }
665
+ $targetBranchPrefix = [string](Get-OptionalPropertyValue -Object $target -Name "branchPrefix")
666
+ $defaultBranchPrefix = [string](Get-OptionalPropertyValue -Object $defaults -Name "branchPrefix")
667
+ $branchPrefix = if (-not [string]::IsNullOrWhiteSpace($targetBranchPrefix)) { $targetBranchPrefix } elseif (-not [string]::IsNullOrWhiteSpace($defaultBranchPrefix)) { $defaultBranchPrefix } else { "chore/ai-kit-sync" }
668
+ $targetSyncConfigPath = [string](Get-OptionalPropertyValue -Object $target -Name "syncConfigPath")
669
+ $defaultSyncConfigPath = [string](Get-OptionalPropertyValue -Object $defaults -Name "syncConfigPath")
670
+ $syncConfigPath = if (-not [string]::IsNullOrWhiteSpace($targetSyncConfigPath)) { $targetSyncConfigPath } elseif (-not [string]::IsNullOrWhiteSpace($defaultSyncConfigPath)) { $defaultSyncConfigPath } else { ".ai-sync.yml" }
671
+ $targetPushBranch = Get-OptionalPropertyValue -Object $target -Name "pushBranch"
672
+ $defaultPushBranch = Get-OptionalPropertyValue -Object $defaults -Name "pushBranch"
673
+ $pushBranch = if ($NoPush.IsPresent) { $false } elseif ($null -ne $targetPushBranch) { [bool]$targetPushBranch } elseif ($null -ne $defaultPushBranch) { [bool]$defaultPushBranch } else { $true }
674
+ $targetCreatePullRequest = Get-OptionalPropertyValue -Object $target -Name "createPullRequest"
675
+ $defaultCreatePullRequest = Get-OptionalPropertyValue -Object $defaults -Name "createPullRequest"
676
+ $createPullRequest = if ($NoPullRequest.IsPresent) { $false } elseif ($null -ne $targetCreatePullRequest) { [bool]$targetCreatePullRequest } elseif ($null -ne $defaultCreatePullRequest) { [bool]$defaultCreatePullRequest } else { $true }
677
+ $targetDraftPullRequest = Get-OptionalPropertyValue -Object $target -Name "draftPullRequest"
678
+ $defaultDraftPullRequest = Get-OptionalPropertyValue -Object $defaults -Name "draftPullRequest"
679
+ $draftPullRequest = if ($null -ne $targetDraftPullRequest) { [bool]$targetDraftPullRequest } elseif ($null -ne $defaultDraftPullRequest) { [bool]$defaultDraftPullRequest } else { $false }
680
+ $targetVerifyCommands = Get-OptionalPropertyValue -Object $target -Name "verifyCommands"
681
+ $defaultVerifyCommands = Get-OptionalPropertyValue -Object $defaults -Name "verifyCommands"
682
+ $verifyCommands = if ($null -ne $targetVerifyCommands) { @($targetVerifyCommands) } elseif ($null -ne $defaultVerifyCommands) { @($defaultVerifyCommands) } else { @() }
683
+ $targetLabels = Get-OptionalPropertyValue -Object $target -Name "labels"
684
+ $defaultLabels = Get-OptionalPropertyValue -Object $defaults -Name "labels"
685
+ $labels = if ($null -ne $targetLabels) { @($targetLabels) } elseif ($null -ne $defaultLabels) { @($defaultLabels) } else { @() }
686
+ $targetCommitMessage = [string](Get-OptionalPropertyValue -Object $target -Name "commitMessage")
687
+ $defaultCommitMessage = [string](Get-OptionalPropertyValue -Object $defaults -Name "commitMessage")
688
+ $commitMessage = if (-not [string]::IsNullOrWhiteSpace($targetCommitMessage)) { $targetCommitMessage } elseif (-not [string]::IsNullOrWhiteSpace($defaultCommitMessage)) { $defaultCommitMessage } else { "chore(ai-kit): sync shared AI workflow config" }
689
+ $targetPrTitle = [string](Get-OptionalPropertyValue -Object $target -Name "prTitle")
690
+ $defaultPrTitle = [string](Get-OptionalPropertyValue -Object $defaults -Name "prTitle")
691
+ $prTitleTemplate = if (-not [string]::IsNullOrWhiteSpace($targetPrTitle)) { $targetPrTitle } elseif (-not [string]::IsNullOrWhiteSpace($defaultPrTitle)) { $defaultPrTitle } else { "chore(ai-kit): sync shared AI workflow config from {{sourceRef}}" }
692
+ $targetPrBody = [string](Get-OptionalPropertyValue -Object $target -Name "prBody")
693
+ $defaultPrBody = [string](Get-OptionalPropertyValue -Object $defaults -Name "prBody")
694
+ $prBodyTemplate = if (-not [string]::IsNullOrWhiteSpace($targetPrBody)) { $targetPrBody } elseif (-not [string]::IsNullOrWhiteSpace($defaultPrBody)) { $defaultPrBody } else { "## AI Workflow Kit Sync`n`nSource: {{sourceRepo}}@{{sourceRef}}`nCommit: {{sourceCommit}}`n`nChanged files:`n{{changedFiles}}`n" }
695
+
696
+ $preStatus = Get-GitStatusLines -RepositoryPath $targetRoot
697
+ if (@($preStatus).Count -gt 0) {
698
+ throw "目标仓库存在未提交改动,拒绝自动同步: $targetRoot"
699
+ }
700
+
701
+ $hasOrigin = $true
702
+ try {
703
+ Invoke-Git -RepositoryPath $targetRoot -Arguments @("remote", "get-url", "origin") | Out-Null
704
+ }
705
+ catch {
706
+ $hasOrigin = $false
707
+ }
708
+
709
+ if ($hasOrigin) {
710
+ Invoke-Git -RepositoryPath $targetRoot -Arguments @("fetch", "origin", "--prune") | Out-Host
711
+ }
712
+
713
+ Invoke-Git -RepositoryPath $targetRoot -Arguments @("checkout", $baseBranch) | Out-Host
714
+ if ($hasOrigin) {
715
+ Invoke-Git -RepositoryPath $targetRoot -Arguments @("pull", "--ff-only", "origin", $baseBranch) | Out-Host
716
+ }
717
+
718
+ $branchName = "{0}-{1}" -f $branchPrefix.TrimEnd("/"), (Get-Date -Format "yyyyMMdd-HHmmss")
719
+ Invoke-Git -RepositoryPath $targetRoot -Arguments @("switch", "-c", $branchName) | Out-Host
720
+
721
+ $syncResult = Sync-TargetRepository `
722
+ -SourceRoot $resolvedSourceRoot `
723
+ -SourceRepo $sourceRepo `
724
+ -SourceRef $sourceRef `
725
+ -SourceCommit $sourceCommit `
726
+ -TargetRoot $targetRoot `
727
+ -SyncConfigPath $syncConfigPath `
728
+ -PullRequestMode:($pushBranch -and $createPullRequest)
729
+
730
+ if (@($verifyCommands).Count -gt 0) {
731
+ Invoke-VerifyCommands -TargetRoot $targetRoot -Commands $verifyCommands
732
+ }
733
+
734
+ $postStatus = Get-GitStatusLines -RepositoryPath $targetRoot
735
+ if (@($postStatus).Count -eq 0) {
736
+ $results.Add([pscustomobject]@{
737
+ Target = $targetRepoDisplay
738
+ Branch = $branchName
739
+ Status = "NO_CHANGES"
740
+ PullRequest = ""
741
+ })
742
+ continue
743
+ }
744
+
745
+ Invoke-Git -RepositoryPath $targetRoot -Arguments @("add", "--all") | Out-Host
746
+ $changedFiles = @(Invoke-Git -RepositoryPath $targetRoot -Arguments @("diff", "--cached", "--name-only"))
747
+ $changedFilesMarkdown = if (@($changedFiles).Count -gt 0) {
748
+ ($changedFiles | ForEach-Object { "- {0}" -f $_ }) -join [Environment]::NewLine
749
+ }
750
+ else {
751
+ "- none"
752
+ }
753
+
754
+ $variables = @{
755
+ sourceRepo = $sourceRepo
756
+ sourceRef = $sourceRef
757
+ sourceCommit = $sourceCommit
758
+ targetRepo = $targetRepo
759
+ branchName = $branchName
760
+ changedFiles = $changedFilesMarkdown
761
+ }
762
+
763
+ $commitBody = Resolve-TemplateValue -Template "Source: {{sourceRepo}}@{{sourceRef}}`nSource-Commit: {{sourceCommit}}" -Variables $variables
764
+ $commitMessageFile = Join-Path ([System.IO.Path]::GetTempPath()) ("ai-sync-commit-{0}.txt" -f ([guid]::NewGuid().ToString("N")))
765
+ [System.IO.File]::WriteAllText($commitMessageFile, $commitMessage + [Environment]::NewLine + [Environment]::NewLine + $commitBody + [Environment]::NewLine, [System.Text.UTF8Encoding]::new($false))
766
+ try {
767
+ Invoke-Git -RepositoryPath $targetRoot -Arguments @("commit", "-F", $commitMessageFile) | Out-Host
768
+ }
769
+ finally {
770
+ Remove-Item -LiteralPath $commitMessageFile -Force -ErrorAction SilentlyContinue
771
+ }
772
+
773
+ $prUrl = ""
774
+ if ($pushBranch) {
775
+ if (-not $hasOrigin) {
776
+ throw "目标仓库没有 origin,无法 push: $targetRoot"
777
+ }
778
+
779
+ Invoke-Git -RepositoryPath $targetRoot -Arguments @("push", "-u", "origin", $branchName) | Out-Host
780
+ }
781
+
782
+ if ($pushBranch -and $createPullRequest) {
783
+ if (-not (Get-Command gh -ErrorAction SilentlyContinue)) {
784
+ throw "未找到 gh CLI,无法创建 Pull Request。"
785
+ }
786
+
787
+ $prTitle = Resolve-TemplateValue -Template $prTitleTemplate -Variables $variables
788
+ $prBody = Resolve-TemplateValue -Template $prBodyTemplate -Variables $variables
789
+ $prBodyFile = Join-Path ([System.IO.Path]::GetTempPath()) ("ai-sync-pr-{0}.md" -f ([guid]::NewGuid().ToString("N")))
790
+ [System.IO.File]::WriteAllText($prBodyFile, $prBody, [System.Text.UTF8Encoding]::new($false))
791
+ try {
792
+ $prArgs = @("pr", "create", "--repo", $targetRepo, "--base", $baseBranch, "--head", $branchName, "--title", $prTitle, "--body-file", $prBodyFile)
793
+ if ($draftPullRequest) {
794
+ $prArgs += "--draft"
795
+ }
796
+ $prOutput = & gh @prArgs
797
+ if ($LASTEXITCODE -ne 0) {
798
+ throw "gh pr create 失败: $targetRepo"
799
+ }
800
+ $prUrl = ($prOutput | Select-Object -Last 1).ToString().Trim()
801
+
802
+ if (@($labels).Count -gt 0) {
803
+ & gh pr edit $prUrl --add-label (($labels | ForEach-Object { [string]$_ }) -join ",") | Out-Host
804
+ if ($LASTEXITCODE -ne 0) {
805
+ throw "gh pr edit --add-label 失败: $prUrl"
806
+ }
807
+ }
808
+ }
809
+ finally {
810
+ Remove-Item -LiteralPath $prBodyFile -Force -ErrorAction SilentlyContinue
811
+ }
812
+ }
813
+
814
+ $results.Add([pscustomobject]@{
815
+ Target = $targetRepoDisplay
816
+ Branch = $branchName
817
+ Status = "SYNCED"
818
+ PullRequest = $prUrl
819
+ })
820
+ }
821
+
822
+ Write-Output ""
823
+ Write-Output "AI sync fan-out summary:"
824
+ foreach ($result in $results) {
825
+ Write-Output ("- {0} [{1}] branch={2}{3}" -f $result.Target, $result.Status, $result.Branch, $(if ([string]::IsNullOrWhiteSpace($result.PullRequest)) { "" } else { " pr=" + $result.PullRequest }))
826
+ }