Repository navigation
Expand file tree
/
Copy pathopencode-documentation-review.yml
More file actions
436 lines (418 loc) · 20.2 KB
/
Copy pathopencode-documentation-review.yml
File metadata and controls
436 lines (418 loc) · 20.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
name: OpenCode PR docs review
on:
pull_request_target:
types: [opened]
workflow_dispatch:
inputs:
pull_number:
description: Pull-request number to review
required: true
type: string
review_request_string:
description: Comment substring that requests an additional review after prior feedback
required: false
type: string
default: 'AI REVIEW REQUESTED'
workflow_call:
inputs:
configuration_source:
description: default (supplied profile), local (caller repository), or an approved central alias
type: string
required: false
default: default
configuration_ref:
description: Full 40-character commit SHA for a remote bundle
type: string
required: false
default: ''
configuration_profile:
description: Bundle profile name
type: string
required: true
focus:
description: Allowlisted review focus
type: string
required: false
default: ''
max_comments:
description: Bounded feedback count (0-20)
type: number
required: false
default: 10
dry_run:
description: Resolve, validate, and generate output without publishing
type: boolean
required: false
default: false
validate_only:
description: Resolve and validate configuration only
type: boolean
required: false
default: false
pull_number:
description: Pull-request number to review when event context is unavailable
type: number
required: false
default: 0
review_request_string:
description: Comment substring that requests an additional review after prior feedback
type: string
required: false
default: 'AI REVIEW REQUESTED'
secrets:
OPENROUTER_API_KEY:
required: true
central_config_token:
description: Optional read token for a private central configuration repository; falls back to github.token.
required: false
jobs:
review:
runs-on: ubuntu-latest
permissions:
contents: read
issues: read
pull-requests: write
env:
FEEDBACK_KIND: pr-documentation-review
steps:
# Keep the caller's trusted base at the workspace root for local
# configuration, policy overlays, and repository operations.
- name: Check out caller repository
uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6
with:
ref: ${{ github.event.pull_request.base.sha || github.sha }}
persist-credentials: false
fetch-depth: 0
# A reusable workflow loads only this YAML from the called repository.
# Fetch its implementation at the exact revision GitHub selected. Skip
# this checkout when a direct/local invocation already has that exact
# repository revision at the workspace root.
- name: Check out pinned workflow helpers
if: job.workflow_repository != github.repository || job.workflow_sha != github.sha
uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6
with:
repository: ${{ job.workflow_repository }}
ref: ${{ job.workflow_sha }}
path: .agentic-workflow
persist-credentials: false
- name: Select trusted workflow helpers
env:
WORKFLOW_REPOSITORY: ${{ job.workflow_repository }}
WORKFLOW_SHA: ${{ job.workflow_sha }}
run: |
if [[ "$WORKFLOW_REPOSITORY" == "$GITHUB_REPOSITORY" && "$WORKFLOW_SHA" == "$GITHUB_SHA" ]]; then
echo "AGENTIC_WORKFLOW_ROOT=$GITHUB_WORKSPACE" >> "$GITHUB_ENV"
else
echo "AGENTIC_WORKFLOW_ROOT=$GITHUB_WORKSPACE/.agentic-workflow" >> "$GITHUB_ENV"
fi
- name: Resolve invocation
id: resolve
env:
AGENTIC_WORKFLOW: pr-documentation-review
# The inputs context is populated for both workflow_call and
# workflow_dispatch and is empty for pull_request_target. Do not use
# github.event_name to identify a reusable call: it retains the
# caller's original event name.
AGENTIC_CONFIGURATION_SOURCE: ${{ inputs.configuration_source || 'local' }}
AGENTIC_CONFIGURATION_REF: ${{ inputs.configuration_ref || '' }}
AGENTIC_CONFIGURATION_PROFILE: ${{ inputs.configuration_profile || 'documentation-review' }}
AGENTIC_FOCUS: ${{ inputs.focus || '' }}
AGENTIC_MAX_COMMENTS: ${{ inputs.max_comments || '' }}
AGENTIC_DRY_RUN: ${{ inputs.dry_run || false }}
AGENTIC_VALIDATE_ONLY: ${{ inputs.validate_only || false }}
AGENTIC_TARGET_NUMBER: ${{ inputs.pull_number || github.event.pull_request.number || '' }}
AGENTIC_REVIEW_REQUEST_STRING: ${{ inputs.review_request_string || 'AI REVIEW REQUESTED' }}
run: |
set -euo pipefail
python3 "$AGENTIC_WORKFLOW_ROOT/scripts/resolve_invocation.py" \
--workflow pr-documentation-review \
--configuration-source "$AGENTIC_CONFIGURATION_SOURCE" \
--configuration-ref "$AGENTIC_CONFIGURATION_REF" \
--configuration-profile "$AGENTIC_CONFIGURATION_PROFILE" \
--focus "$AGENTIC_FOCUS" \
--max-comments "$AGENTIC_MAX_COMMENTS" \
--dry-run "$AGENTIC_DRY_RUN" \
--validate-only "$AGENTIC_VALIDATE_ONLY" \
--target-number "$AGENTIC_TARGET_NUMBER" \
--review-request-string "$AGENTIC_REVIEW_REQUEST_STRING" \
--github-output "$GITHUB_OUTPUT" \
--github-step-summary "$GITHUB_STEP_SUMMARY" \
--result "$RUNNER_TEMP/resolved-invocation.json"
- name: Stop after validation-only
if: steps.resolve.outputs.validate_only == 'true'
env:
AGENTIC_WORKFLOW_VERSION: ${{ job.workflow_sha }}
run: |
echo "validate_only selected; no checkout, model invocation, or publication performed."
# Emit a minimal redacted provenance record even in validate-only.
python3 - <<'PY'
import json, os
from pathlib import Path
import importlib.util
spec = importlib.util.spec_from_file_location(
"agentic_provenance", Path(os.environ["AGENTIC_WORKFLOW_ROOT"]) / "scripts/agentic_provenance.py"
)
prov = importlib.util.module_from_spec(spec)
import sys; sys.modules[spec.name] = prov; spec.loader.exec_module(prov)
record = prov.build_provenance(
workflow_version=os.environ.get("AGENTIC_WORKFLOW_VERSION", "dev"),
workflow_name="pr-documentation-review",
caller_repository=os.environ.get("GITHUB_REPOSITORY", ""),
target_kind="pull_request",
target_number=None,
target_head_sha=None,
bundle=None,
prompt_template_sha256=None,
output_contract=None,
model_profile=None,
effective_policy_sha256=None,
mode="validate-only",
result="validated",
)
out = Path(os.environ["RUNNER_TEMP"]) / "resolved-agentic-provenance.json"
out.write_text(record.to_json() + "\n", encoding="utf-8")
summary = Path(os.environ["GITHUB_STEP_SUMMARY"])
existing = summary.read_text() if summary.exists() else ""
if existing and not existing.endswith("\n"):
existing += "\n"
summary.write_text(existing + prov.job_summary(record), encoding="utf-8")
PY
echo "::notice::Resolved configuration written to job summary and artifact."
- name: Resolve pull request
if: steps.resolve.outputs.validate_only != 'true'
id: pull-request
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ steps.resolve.outputs.target_number }}
run: |
if ! [[ "$PR_NUMBER" =~ ^[1-9][0-9]*$ ]]; then
echo "A positive pull-request number is required." >&2
exit 1
fi
pull_request=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER")
if [[ $(jq -r '.state' <<< "$pull_request") != "open" ]]; then
echo "Pull request #$PR_NUMBER is not open." >&2
exit 1
fi
{
echo "number=$PR_NUMBER"
echo "base_sha=$(jq -r '.base.sha' <<< "$pull_request")"
echo "head_sha=$(jq -r '.head.sha' <<< "$pull_request")"
echo "author=$(jq -r '.user.login' <<< "$pull_request")"
echo "title=$(jq -r '.title' <<< "$pull_request")"
} >> "$GITHUB_OUTPUT"
jq '{number, title, body, user: {login: .user.login}, base: {sha: .base.sha, ref: .base.ref}, head: {sha: .head.sha, ref: .head.ref}}' \
<<< "$pull_request" > "$RUNNER_TEMP/pr-metadata.json"
- uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6
if: steps.resolve.outputs.validate_only != 'true'
with:
python-version: '3.12'
- name: Resolve agentic configuration
if: steps.resolve.outputs.validate_only != 'true'
id: config
env:
AGENTIC_WORKFLOW: pr-documentation-review
AGENTIC_CONFIGURATION_SOURCE: ${{ steps.resolve.outputs.configuration_source }}
AGENTIC_CONFIGURATION_REF: ${{ steps.resolve.outputs.configuration_ref }}
AGENTIC_CONFIGURATION_PROFILE: ${{ steps.resolve.outputs.configuration_profile }}
# Prefer a caller-provided read token for a private central config
# repository; fall back to github.token where valid. The token is
# never logged or written to artifacts.
GITHUB_TOKEN: ${{ secrets.central_config_token || github.token }}
AGENTIC_WORKFLOW_VERSION: ${{ job.workflow_sha }}
run: |
set -euo pipefail
if ! python3 "$AGENTIC_WORKFLOW_ROOT/scripts/agentic_configuration.py" \
--workflow pr-documentation-review \
--configuration-source "$AGENTIC_CONFIGURATION_SOURCE" \
--configuration-ref "$AGENTIC_CONFIGURATION_REF" \
--configuration-profile "$AGENTIC_CONFIGURATION_PROFILE" \
--result "$RUNNER_TEMP/resolved-agentic-configuration.json"; then
# Emit a minimal redacted failure provenance record before exiting.
python3 - <<'PY'
import json, os, sys
from pathlib import Path
import importlib.util
spec = importlib.util.spec_from_file_location("agentic_provenance", Path(os.environ["AGENTIC_WORKFLOW_ROOT"]) / "scripts/agentic_provenance.py")
prov = importlib.util.module_from_spec(spec); sys.modules[spec.name] = prov; spec.loader.exec_module(prov)
record = prov.failure_record(
workflow_version=os.environ.get("AGENTIC_WORKFLOW_VERSION", "dev"),
workflow_name="pr-documentation-review",
caller_repository=os.environ.get("GITHUB_REPOSITORY", ""),
mode="publish",
error=RuntimeError("configuration resolution failed"),
)
Path(os.environ["RUNNER_TEMP"] + "/resolved-agentic-provenance.json").write_text(record.to_json() + "\n")
PY
exit 1
fi
jq -r '.model_profile' "$RUNNER_TEMP/resolved-agentic-configuration.json" > /tmp/model-profile
echo "model_profile=$(cat /tmp/model-profile)" >> "$GITHUB_OUTPUT"
- name: Resolve effective policy
if: steps.resolve.outputs.validate_only != 'true'
id: policy
env:
AGENTIC_WORKFLOW: pr-documentation-review
DRY_RUN: ${{ steps.resolve.outputs.dry_run }}
AGENTIC_WORKFLOW_VERSION: ${{ job.workflow_sha }}
run: |
set -euo pipefail
model_profile=$(jq -r '.model_profile' "$RUNNER_TEMP/resolved-agentic-configuration.json")
mode="publish"
if [[ "$DRY_RUN" == "true" ]]; then mode="dry-run"; fi
# Derive actual agent capabilities from verified resolved front
# matter (required change #3), not a hardcoded empty object.
agent_caps=$(python3 - <<'PY'
import json, sys, os, importlib.util
from pathlib import Path
spec = importlib.util.spec_from_file_location("agentic_policy", Path(os.environ["AGENTIC_WORKFLOW_ROOT"]) / "scripts/agentic_policy.py")
pol = importlib.util.module_from_spec(spec); sys.modules[spec.name] = pol; spec.loader.exec_module(pol)
data = json.loads(Path(os.environ["RUNNER_TEMP"] + "/resolved-agentic-configuration.json").read_text())
agent_text = (Path(data["bundle_root"]) / data["agent_file"]).read_text(encoding="utf-8")
print(json.dumps(pol.parse_agent_capabilities(agent_text)))
PY
)
overlay_arg=""
if [[ -f .github/agentic-policy-overlay.json ]]; then
overlay_arg="--overlay .github/agentic-policy-overlay.json"
fi
python3 "$AGENTIC_WORKFLOW_ROOT/scripts/agentic_policy.py" \
--workflow "$AGENTIC_WORKFLOW" \
--model-profile "$model_profile" \
--resolved-config "$RUNNER_TEMP/resolved-agentic-configuration.json" \
--agent-capabilities-json "$agent_caps" \
--mode "$mode" \
$overlay_arg \
--result "$RUNNER_TEMP/effective-policy.json" \
--github-step-summary "$GITHUB_STEP_SUMMARY" \
|| {
# On policy failure, emit a minimal redacted provenance record.
python3 - <<'PY'
import json, os, sys
from pathlib import Path
import importlib.util
spec = importlib.util.spec_from_file_location("agentic_provenance", Path(os.environ["AGENTIC_WORKFLOW_ROOT"]) / "scripts/agentic_provenance.py")
prov = importlib.util.module_from_spec(spec); sys.modules[spec.name] = prov; spec.loader.exec_module(prov)
bundle = json.loads(Path(os.environ["RUNNER_TEMP"] + "/resolved-agentic-configuration.json").read_text())
record = prov.failure_record(
workflow_version=os.environ.get("AGENTIC_WORKFLOW_VERSION", "dev"),
workflow_name="pr-documentation-review",
caller_repository=os.environ.get("GITHUB_REPOSITORY", ""),
mode="publish",
error=RuntimeError("policy resolution failed"),
bundle=bundle,
)
Path(os.environ["RUNNER_TEMP"] + "/resolved-agentic-provenance.json").write_text(record.to_json() + "\n")
PY
exit 1
}
echo "sha256=$(jq -r '.sha256' "$RUNNER_TEMP/effective-policy.json")" >> "$GITHUB_OUTPUT"
- name: Get pull request diff
if: steps.resolve.outputs.validate_only != 'true'
env:
BASE_SHA: ${{ steps.pull-request.outputs.base_sha }}
PR_NUMBER: ${{ steps.pull-request.outputs.number }}
# `actions/checkout` intentionally removes persisted credentials.
# Supply this read-scoped job token only to the fetch so reusable
# workflow invocations can retrieve private pull-request heads.
GH_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
auth_header="AUTHORIZATION: basic $(printf 'x-access-token:%s' "$GH_TOKEN" | base64 --wrap=0)"
git -c "http.https://github.com/.extraheader=$auth_header" \
fetch --no-tags origin "pull/$PR_NUMBER/head:pr-head"
merge_base="$(git merge-base "$BASE_SHA" pr-head)"
git diff "$merge_base" pr-head -- > pr.diff
- name: Set up Node.js 24
if: steps.resolve.outputs.validate_only != 'true'
uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5
with:
node-version: '24'
- name: Install OpenCode
if: steps.resolve.outputs.validate_only != 'true'
# Keep CLI parsing stable; `--file` accepts multiple paths in current
# OpenCode releases, so unpinned updates can change invocation behavior.
# The postinstall script creates the real platform binary
# (bin/opencode.exe); npm's allow-scripts gate blocks it unless the
# exact-pinned package is allow-listed. The scoped flag permits only
# opencode-ai, with no wildcard or persistent npm config.
run: |
set -euo pipefail
npm install -g --allow-scripts=opencode-ai opencode-ai@1.18.16
opencode --version
- name: Review documentation impact
if: steps.resolve.outputs.validate_only != 'true'
env:
GITHUB_TOKEN: ${{ github.token }}
OPENROUTER_API_KEY: ${{ secrets.OPENROUTER_API_KEY }}
FAIL_IF_REVIEWED: ${{ github.event_name == 'workflow_dispatch' }}
DRY_RUN: ${{ steps.resolve.outputs.dry_run }}
FOCUS: ${{ steps.resolve.outputs.focus }}
MAX_COMMENTS: ${{ steps.resolve.outputs.max_comments }}
REVIEW_REQUEST_STRING: ${{ steps.resolve.outputs.review_request_string }}
run: |
review_args=(
--input pr.diff
--comments-url "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/${{ steps.pull-request.outputs.number }}/comments"
--repository "${{ github.repository }}"
--pull-number "${{ steps.pull-request.outputs.number }}"
--head-sha "${{ steps.pull-request.outputs.head_sha }}"
--feedback-kind "$FEEDBACK_KIND"
--author "${{ steps.pull-request.outputs.author }}"
--target-title "${{ steps.pull-request.outputs.title }}"
--pr-metadata "$RUNNER_TEMP/pr-metadata.json"
--base-ref "${{ steps.pull-request.outputs.base_sha }}"
--head-ref pr-head
--resolved-config "$RUNNER_TEMP/resolved-agentic-configuration.json"
--effective-policy "$RUNNER_TEMP/effective-policy.json"
--provenance "$RUNNER_TEMP/resolved-agentic-provenance.json"
--response-diagnostics "$RUNNER_TEMP/opencode-response-diagnostics.json"
--publication-preview "$RUNNER_TEMP/agentic-publication-preview.json"
)
review_args+=(--review-request-string "$REVIEW_REQUEST_STRING")
if [[ "$FAIL_IF_REVIEWED" == "true" ]]; then
review_args+=(--fail-if-reviewed)
fi
if [[ "$DRY_RUN" == "true" ]]; then
review_args+=(--dry-run)
fi
if [[ -n "$FOCUS" ]]; then
review_args+=(--focus "$FOCUS")
fi
if [[ -n "$MAX_COMMENTS" ]]; then
review_args+=(--max-comments "$MAX_COMMENTS")
fi
python3 "$AGENTIC_WORKFLOW_ROOT/scripts/run_agentic_feedback.py" "${review_args[@]}"
- name: Provenance job summary
if: always() && steps.resolve.outputs.validate_only != 'true'
run: |
if [[ -f "$RUNNER_TEMP/resolved-agentic-provenance.json" ]]; then
python3 - <<'PY'
import json, os, sys
from pathlib import Path
record = json.loads(Path(os.environ["RUNNER_TEMP"] + "/resolved-agentic-provenance.json").read_text())
summary_path = Path(os.environ["GITHUB_STEP_SUMMARY"])
existing = summary_path.read_text() if summary_path.exists() else ""
if existing and not existing.endswith("\n"):
existing += "\n"
lines = ["### Agentic workflow provenance", ""]
for k in ("workflow_name", "mode", "result", "output_contract", "model_profile", "configuration_digest"):
if record.get(k):
lines.append(f"- {k}: `{record[k]}`")
summary_path.write_text(existing + "\n".join(lines) + "\n")
PY
fi
- name: Upload resolved invocation artifact
if: always()
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
with:
name: resolved-invocation-pr-review
path: |
${{ runner.temp }}/resolved-invocation.json
${{ runner.temp }}/resolved-agentic-configuration.json
${{ runner.temp }}/effective-policy.json
${{ runner.temp }}/resolved-agentic-provenance.json
${{ runner.temp }}/opencode-response-diagnostics.json
${{ runner.temp }}/agentic-publication-preview.json
retention-days: 14
if-no-files-found: ignore