Skip to content

Commit 4f92b28

Browse files
authored
ci: own docs agent Git lifecycle (#133124)
1 parent 1dcfbd6 commit 4f92b28

11 files changed

Lines changed: 624 additions & 96 deletions

‎.github/workflows/docs-agent.yml‎

Lines changed: 122 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ jobs:
4040
persist-credentials: false
4141
submodules: false
4242

43+
- name: Prepare Git owner
44+
uses: openclaw/openclaw/.github/actions/git-owner@dd4528b6393e7d00063067a080ca7241b48ce475
45+
4346
- name: Gate trusted main activity and hourly cadence
4447
id: gate
4548
env:
@@ -49,35 +52,55 @@ jobs:
4952
run: |
5053
set -euo pipefail
5154
52-
if [ "$EVENT_NAME" != "workflow_run" ]; then
53-
head_sha="$(git rev-parse HEAD)"
54-
review_base="$(git rev-parse "${head_sha}^" 2>/dev/null || printf '%s' "$head_sha")"
55-
{
56-
echo "run_agent=true"
57-
echo "base_sha=${head_sha}"
58-
echo "review_base_sha=${review_base}"
59-
echo "review_head_sha=${head_sha}"
60-
} >> "$GITHUB_OUTPUT"
61-
exit 0
62-
fi
55+
remote_main_file="$(mktemp "$RUNNER_TEMP/docs-agent-main.XXXXXX")"
56+
trap 'rm -f -- "$remote_main_file" "$RUNNER_TEMP/docs-agent-runs.json"' EXIT
57+
python3 -I -S "$CI_GIT_OWNER" --policy - "$remote_main_file" <<'PYTHON'
58+
import os
59+
import subprocess
60+
import sys
61+
import tempfile
62+
from ci_git_owner import run_git, git_output, GitFailure, FetchTimeout, backoff, check_cancelled
6363
64-
for attempt in 1 2 3 4 5; do
65-
if timeout --signal=TERM --kill-after=10s 120s git fetch --no-tags origin main; then
66-
break
67-
fi
68-
if [ "$attempt" = "5" ]; then
69-
echo "Failed to fetch main after retries." >&2
70-
exit 1
71-
fi
72-
echo "Fetch attempt ${attempt} failed; retrying."
73-
sleep $((attempt * 2))
74-
done
75-
remote_main="$(git rev-parse origin/main)"
76-
if [ "$remote_main" != "$WORKFLOW_HEAD_SHA" ]; then
77-
echo "CI run is superseded by ${remote_main}; skipping docs agent for ${WORKFLOW_HEAD_SHA}."
78-
echo "run_agent=false" >> "$GITHUB_OUTPUT"
64+
workspace = os.getcwd()
65+
if os.environ["EVENT_NAME"] != "workflow_run":
66+
head_sha = git_output(workspace, "rev-parse", "HEAD").rstrip("\n")
67+
try:
68+
with tempfile.TemporaryFile(dir=os.environ["RUNNER_TEMP"]) as parent:
69+
run_git(workspace, "rev-parse", f"{head_sha}^", stdout=parent, stderr=subprocess.DEVNULL)
70+
parent.seek(0)
71+
review_base = parent.read().decode().rstrip("\n")
72+
except GitFailure:
73+
review_base = head_sha
74+
check_cancelled()
75+
with open(os.environ["GITHUB_OUTPUT"], "a") as output:
76+
output.write(f"run_agent=true\nbase_sha={head_sha}\nreview_base_sha={review_base}\nreview_head_sha={head_sha}\n")
77+
raise SystemExit(0)
78+
79+
for attempt in range(1, 6):
80+
try:
81+
run_git(workspace, "fetch", "--no-tags", "origin", "main", timeout=120, reclaim_locks=True)
82+
break
83+
except (GitFailure, FetchTimeout):
84+
if attempt == 5:
85+
print("Failed to fetch main after retries.", file=sys.stderr)
86+
raise SystemExit(1)
87+
print(f"Fetch attempt {attempt} failed; retrying.", flush=True)
88+
backoff(attempt * 2)
89+
remote_main = git_output(workspace, "rev-parse", "origin/main").rstrip("\n")
90+
check_cancelled()
91+
if remote_main != os.environ["WORKFLOW_HEAD_SHA"]:
92+
print(f"CI run is superseded by {remote_main}; skipping docs agent for {os.environ['WORKFLOW_HEAD_SHA']}.")
93+
with open(os.environ["GITHUB_OUTPUT"], "a") as output:
94+
output.write("run_agent=false\n")
95+
else:
96+
# Only a current, fully drained main read permits the shell cadence query.
97+
with open(sys.argv[3], "w") as fact:
98+
fact.write(remote_main + "\n")
99+
PYTHON
100+
if [ ! -s "$remote_main_file" ]; then
79101
exit 0
80102
fi
103+
remote_main="$(cat "$remote_main_file")"
81104
82105
runs_json="$RUNNER_TEMP/docs-agent-runs.json"
83106
gh api --method GET "repos/${GITHUB_REPOSITORY}/actions/workflows/docs-agent.yml/runs" \
@@ -117,16 +140,31 @@ jobs:
117140
| select(. != $remote_main)
118141
' "$runs_json" | head -n 1
119142
)"
120-
if [ -z "$review_base" ] || ! git cat-file -e "${review_base}^{commit}" 2>/dev/null; then
121-
review_base="$(git rev-parse "${remote_main}^" 2>/dev/null || printf '%s' "$remote_main")"
122-
fi
143+
python3 -I -S "$CI_GIT_OWNER" --policy - "$remote_main" "$review_base" <<'PYTHON'
144+
import os
145+
import subprocess
146+
import sys
147+
import tempfile
148+
from ci_git_owner import run_git, GitFailure, check_cancelled
123149
124-
{
125-
echo "run_agent=true"
126-
echo "base_sha=${remote_main}"
127-
echo "review_base_sha=${review_base}"
128-
echo "review_head_sha=${remote_main}"
129-
} >> "$GITHUB_OUTPUT"
150+
remote_main, review_base = sys.argv[3:]
151+
try:
152+
if review_base:
153+
run_git(os.getcwd(), "cat-file", "-e", f"{review_base}^{{commit}}", stderr=subprocess.DEVNULL)
154+
except GitFailure:
155+
review_base = ""
156+
if not review_base:
157+
try:
158+
with tempfile.TemporaryFile(dir=os.environ["RUNNER_TEMP"]) as parent:
159+
run_git(os.getcwd(), "rev-parse", f"{remote_main}^", stdout=parent, stderr=subprocess.DEVNULL)
160+
parent.seek(0)
161+
review_base = parent.read().decode().rstrip("\n")
162+
except GitFailure:
163+
review_base = remote_main
164+
check_cancelled()
165+
with open(os.environ["GITHUB_OUTPUT"], "a") as output:
166+
output.write(f"run_agent=true\nbase_sha={remote_main}\nreview_base_sha={review_base}\nreview_head_sha={remote_main}\n")
167+
PYTHON
130168
131169
- name: Setup Node environment
132170
if: steps.gate.outputs.run_agent == 'true'
@@ -166,27 +204,30 @@ jobs:
166204
run: |
167205
set -euo pipefail
168206
169-
untracked="$(git ls-files --others --exclude-standard)"
207+
untracked="$(python3 -I -S "$CI_GIT_OWNER" --checkout-git 0 ls-files --others --exclude-standard)"
170208
if [ -n "$untracked" ]; then
171209
echo "Docs agent created untracked files; forbidden:"
172210
printf '%s\n' "$untracked"
173211
exit 1
174212
fi
175213
176-
added_or_deleted="$(git diff --name-status --diff-filter=AD)"
214+
added_or_deleted="$(python3 -I -S "$CI_GIT_OWNER" --checkout-git 0 diff --name-status --diff-filter=AD)"
177215
if [ -n "$added_or_deleted" ]; then
178216
echo "Docs agent added or deleted tracked files; forbidden:"
179217
printf '%s\n' "$added_or_deleted"
180218
exit 1
181219
fi
182220
183-
bad_paths="$(
184-
git diff --name-only | while IFS= read -r path; do
221+
filter_bad_paths() {
222+
while IFS= read -r path; do
185223
case "$path" in
186224
docs/*|README.md|CHANGELOG.md) ;;
187225
*) printf '%s\n' "$path" ;;
188226
esac
189227
done
228+
}
229+
bad_paths="$(
230+
python3 -I -S "$CI_GIT_OWNER" --checkout-git 0 diff --name-only | filter_bad_paths
190231
)"
191232
if [ -n "$bad_paths" ]; then
192233
echo "Docs agent touched non-doc paths; forbidden:"
@@ -218,33 +259,46 @@ jobs:
218259
run: |
219260
set -euo pipefail
220261
221-
if git diff --quiet; then
222-
echo "No docs changes."
223-
exit 0
224-
fi
262+
exec python3 -I -S "$CI_GIT_OWNER" --policy - <<'PYTHON'
263+
import os
264+
import sys
265+
from ci_git_owner import run_git, git_output, GitFailure, FetchTimeout, backoff
266+
267+
workspace = os.getcwd()
268+
target = os.environ["TARGET_BRANCH"]
269+
base_sha = os.environ["BASE_SHA"]
270+
try:
271+
run_git(workspace, "diff", "--quiet")
272+
except GitFailure:
273+
pass
274+
else:
275+
print("No docs changes.")
276+
raise SystemExit(0)
225277
226-
git config user.name "openclaw-docs-agent[bot]"
227-
git config user.email "openclaw-docs-agent[bot]@users.noreply.github.com"
228-
git add docs README.md CHANGELOG.md
229-
git commit --no-verify -m "docs: refresh documentation"
230-
231-
for attempt in 1 2 3 4 5; do
232-
if ! timeout --signal=TERM --kill-after=10s 120s git fetch --no-tags origin "${TARGET_BRANCH}"; then
233-
echo "Fetch attempt ${attempt} failed; retrying."
234-
sleep $((attempt * 2))
235-
continue
236-
fi
237-
if git push "https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" HEAD:"${TARGET_BRANCH}"; then
238-
exit 0
239-
fi
240-
remote_main="$(git rev-parse "origin/${TARGET_BRANCH}")"
241-
if [ "$remote_main" != "$BASE_SHA" ]; then
242-
echo "main advanced from ${BASE_SHA} to ${remote_main}; skipping stale docs update."
243-
exit 0
244-
fi
245-
echo "Docs update attempt ${attempt} failed; retrying."
246-
sleep $((attempt * 2))
247-
done
248-
249-
echo "Failed to push docs updates after retries." >&2
250-
exit 1
278+
run_git(workspace, "config", "user.name", "openclaw-docs-agent[bot]", reclaim_locks=True)
279+
run_git(workspace, "config", "user.email", "openclaw-docs-agent[bot]@users.noreply.github.com", reclaim_locks=True)
280+
run_git(workspace, "add", "docs", "README.md", "CHANGELOG.md", reclaim_locks=True)
281+
run_git(workspace, "commit", "--no-verify", "-m", "docs: refresh documentation", reclaim_locks=True)
282+
for attempt in range(1, 6):
283+
try:
284+
run_git(workspace, "fetch", "--no-tags", "origin", target, timeout=120, reclaim_locks=True)
285+
except (GitFailure, FetchTimeout):
286+
print(f"Fetch attempt {attempt} failed; retrying.", flush=True)
287+
backoff(attempt * 2)
288+
continue
289+
try:
290+
run_git(workspace, "push",
291+
f"https://x-access-token:{os.environ['GITHUB_TOKEN']}@github.com/{os.environ['GITHUB_REPOSITORY']}.git",
292+
f"HEAD:{target}", reclaim_locks=True)
293+
raise SystemExit(0)
294+
except GitFailure:
295+
# A push failure permits stale/retry decisions only after verified cleanup.
296+
remote_main = git_output(workspace, "rev-parse", f"origin/{target}").rstrip("\n")
297+
if remote_main != base_sha:
298+
print(f"main advanced from {base_sha} to {remote_main}; skipping stale docs update.")
299+
raise SystemExit(0)
300+
print(f"Docs update attempt {attempt} failed; retrying.", flush=True)
301+
backoff(attempt * 2)
302+
print("Failed to push docs updates after retries.", file=sys.stderr)
303+
raise SystemExit(1)
304+
PYTHON

‎docs/ci.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ Mantis installers and worktree preparation also use that pinned owner, prepared
200200

201201
Docs Sync Publish Repo prepares the same pinned owner after the source and ClawHub checkouts, using system Python before Node setup. Its inline typed policies own clone and the connected publication chain. Clone and both fetch sites retain 120-second operation deadlines; clone and publication retain five attempts with 2/4/6/8/10-second backoffs, including the final failed attempt. Only ordinary Git failure or `FetchTimeout` permits retry after verified cleanup. The advisory pre-commit fetch may fail and safely continue to commit; publication failure first attempts an owned rebase abort. Rebase, push, local reads, and config/add/commit remain unbounded. ClawHub HEAD must complete through the owner before the sync script consumes it. Cleanup uncertainty, setup failure, or cancellation is terminal before retry, abort, another deletion, or consumption. Only the fixed publish path is replaced, without following its symlink; exclusive publish operations reclaim newly created locks while preserving existing locks. Queue concurrency, stale-source checks, and no-change success remain unchanged.
202202

203+
Docs Agent prepares the same pinned owner immediately after checkout. Two trusted inline gate policies own Git before and after the unchanged shell GitHub/JQ cadence block; manual dispatch and superseded CI exit before that query. The connected commit policy owns diff, config, staging, commit, fetch, push, and stale-main readback. Gate and commit each retain five `fetch --no-tags` attempts with 120-second deadlines and exclusive-checkout lock reclamation. Gate backoffs are 2/4/6/8 seconds, only between attempts; commit fetch and push failures retain 2/4/6/8/10 seconds, including the final failure. Local reads, docs-only enforcement producers, config/add/commit, and push remain unbounded. Only typed ordinary Git failure or `FetchTimeout` permits the existing recovery paths after verified cleanup; setup, census, cleanup failure, and cancellation are terminal before retry, fallback, outputs, or downstream work. The one-hour cadence, REST `.id` current-run exclusion, cancelled/skipped exclusions, and review-base ordering remain unchanged.
204+
203205
To use the standalone action from another workflow, pin `openclaw/openclaw/.github/actions/git-owner@<full-40-character-commit-SHA>` to a reviewed revision containing the action. Supply policy from the trusted workflow inline or from the same trusted action package, never from the selected candidate. Within `ci.yml`, the existing bundled-protocol and CI-routing matrix tasks smoke-test the action from the separately pinned `.ci-harness` checkout before Node setup, compare its output and copied bytes, and run owned `git --version` without network access. Other workflows' direct Git commands remain outside this ownership coverage until they adopt it.
204206

205207
## Scope and routing

‎scripts/ci-changed-scope.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const APPLE_SHARED_CONTRACT_FIXTURE_RE =
4949
const MACOS_NATIVE_RE =
5050
/^(apps\/macos\/|apps\/macos-mlx-tts\/|apps\/shared\/|apps\/swabble\/|Swabble\/)/;
5151
const GIT_OWNER_SCOPE_RE =
52-
/^(?:\.github\/(?:actions\/(?:git-owner|ensure-base-commit|mantis-validate-trusted-ref)\/|workflows\/(?:workflow-sanity|qa-profile-evidence|docs-sync-publish|mantis-(?:discord-(?:smoke|status-reactions|thread-attachment)|slack-desktop-smoke|web-ui-chat-proof))\.yml$)|scripts\/generate-ci-git-owner\.mts$|test\/scripts\/(?:ci-(?:checkout|git-owner|linux-git|platform-checkout)\.test(?:-support)?\.ts|fixtures\/ci-platform-checkout\.mjs)$)/;
52+
/^(?:\.github\/(?:actions\/(?:git-owner|ensure-base-commit|mantis-validate-trusted-ref)\/|workflows\/(?:workflow-sanity|qa-profile-evidence|docs-agent|docs-sync-publish|mantis-(?:discord-(?:smoke|status-reactions|thread-attachment)|slack-desktop-smoke|web-ui-chat-proof))\.yml$)|scripts\/generate-ci-git-owner\.mts$|test\/scripts\/(?:ci-(?:checkout|git-owner|linux-git|platform-checkout)\.test(?:-support)?\.ts|fixtures\/ci-platform-checkout\.mjs)$)/;
5353
const MACOS_SCRIPT_SCOPE_RE =
5454
/^(?:scripts\/(?:check-swift-tools|codesign-mac-app|create-dmg|format-swift|install-swift-tools|install-xcodegen|lint-swift|mac-elevation-host|notarize-mac-artifact|package-mac-app|package-mac-dist|restart-mac|stage-cua-driver-macos|stage-mac-node-worker)\.sh|scripts\/test-macos-native\.mts|scripts\/(?:verify-mac-node-worker(?:-fs)?|lib\/(?:mac-node-worker-proof-state|mac-worker-portability))\.mjs|scripts\/(?:materialize-mac-node-worker|lib\/(?:mac-native-inventory|mac-bundle-mutation))\.py|scripts\/lib\/(?:mac-app-bundle|plistbuddy|swift-toolchain)\.sh|test\/helpers\/mac-(?:native|signing)\.ts|test\/scripts\/(?:codesign-mac-app|create-dmg|mac-elevation-host|mac-node-worker|macos-native-test-launch|notarize-mac-artifact|package-mac-app|package-mac-dist|restart-mac|verify-mac-node-worker-fs)\.test\.ts|test\/scripts\/(?:mac-elevation-artifact|mac-native-fixtures|mac-node-worker-materialization)\.test-support\.ts)$/;
5555
const WORKSPACE_RSYNC_RECEIVER_SCOPE_RE =

‎scripts/test-projects.test-support.mts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2122,6 +2122,7 @@ const pluginSdkEntryOwners = [
21222122
const EXACT_TOOLING_TARGETS = new Map<string, string[]>([
21232123
[".github/workflows/ci.yml", ["ci-platform-checkout", "ci-linux-git", "ci-git-owner"]],
21242124
[".github/workflows/docs-sync-publish.yml", ["docs-sync-publish"]],
2125+
[".github/workflows/docs-agent.yml", ["docs-agent-workflow"]],
21252126
[
21262127
"test/scripts/fixtures/ci-platform-checkout.mjs",
21272128
["ci-platform-checkout", "ci-linux-git", "ci-git-owner"],
@@ -2396,7 +2397,7 @@ const SEMANTIC_TOOLING_TARGET_PATTERNS: Array<[RegExp, string[]]> = [
23962397
],
23972398
[/^\.github\/workflows\/android-release\.yml$/u, [packageAcceptance, workflowGuards]],
23982399
[
2399-
/^\.github\/(?:actions\/(?:ensure-base-commit|git-owner|mantis-validate-trusted-ref)\/|workflows\/(?:workflow-sanity|qa-profile-evidence|docs-sync-publish|mantis-(?:discord-(?:smoke|status-reactions|thread-attachment)|slack-desktop-smoke|web-ui-chat-proof))\.yml$)/u,
2400+
/^\.github\/(?:actions\/(?:ensure-base-commit|git-owner|mantis-validate-trusted-ref)\/|workflows\/(?:workflow-sanity|qa-profile-evidence|docs-agent|docs-sync-publish|mantis-(?:discord-(?:smoke|status-reactions|thread-attachment)|slack-desktop-smoke|web-ui-chat-proof))\.yml$)/u,
24002401
["ci-git-owner", "ci-linux-git", "ci-platform-checkout"],
24012402
],
24022403
[/^tsconfig\.scripts\.json$/u, ["changed-lanes", "test-projects"]],

‎src/scripts/ci-changed-scope.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ describe("detectChangedScope", () => {
403403
".github/workflows/workflow-sanity.yml",
404404
".github/workflows/qa-profile-evidence.yml",
405405
".github/workflows/docs-sync-publish.yml",
406+
".github/workflows/docs-agent.yml",
406407
".github/actions/mantis-validate-trusted-ref/action.yml",
407408
".github/workflows/mantis-discord-smoke.yml",
408409
".github/workflows/mantis-discord-status-reactions.yml",

0 commit comments

Comments
 (0)