Files
kilocode/.github/workflows/docs-sync.yml
Igor Šćekić 94de3e0eea refactor(docs-sync): declare the headless agent scope in the committed policy (#16405)
* refactor(docs-sync): declare the headless agent scope in the committed policy

Each headless pass now states its tool and path scope in configuration the CLI
enforces. agent-permissions.json holds two modes; agent-policy.mjs turns a mode
into KILO_CONFIG_CONTENT and lib.mjs applies it to every spawned run.

The gateway credentials now sit on the four steps that call the CLI, and the
checkout keeps no git credential: the fetch and push steps authenticate from
their own environment.

* fix(docs-sync): narrow the agent scope and keep attachment delivery

- `read` is allowed in both modes, because the CLI reads a `-f` attachment
  through the read tool.
- `docs-write` names every permission it needs, with a catch-all deny first.
- Edits cover the docs pages and navigation directories.
- The allowlist drops `sed`, `uniq` and `diff`, which each have a form that
  writes a file or runs a command.
- The rebuild step's comment describes the rebuild only.

* test(docs-sync): guard the command allowlist

The policy test records the allowlist the docs passes rely on, so a later edit
that changes it has to update this expectation too.

* fix(docs-sync): restrict docs edits to markdown and pin the mode tests

- The pages allow names the `.md` extension, so the Next.js app modules and API
  routes beside the docs stay out of reach.
- The mode tests compare against literal expectations instead of the map they
  read, so a new allow fails the test.
- The triage comment matches the mode it describes.

* test(docs-sync): cover sort in the allowlist guard

sort can write a file with -o and run a program with --compress-program, so the
guard that keeps such commands out of the allowlist names it too.

* test(docs-sync): drop an assertion the exact list already covers

The edit-scope test pins the exact allow list, so a second shape check could not
fail; the label and a comment now state what the list enforces.

* docs(docs-sync): describe the repair step's scope accurately

The comment on the repair step now names the paths and commands the mode
declares, so it matches agent-permissions.json.

* fix(docs-sync): require a valid agent policy before each run

Every mode is now checked as it becomes KILO_CONFIG_CONTENT: the first rule
must be the catch-all deny, and each rule and pattern must state a known
action. A mode that does not meet that requirement stops the run, so every
run carries the scope its mode declares.

The repair step builds the policy into a variable and checks it before
`kilo run`, so the pass starts only when its policy is present.

* test(docs-sync): pin the docs typecheck grant in the policy guard

* fix(docs-sync): let the mode own the emitted permission map

Each mode now supplies `permission` outright, so a run always evaluates the
rule map the mode declares, with the catch-all deny first. Provider and model
settings from the calling environment still pass through.

* fix(docs-sync): give each agent run its own step environment

- agent-policy.env() passes each pass the gateway credentials and its inputs,
  and leaves the variables a step sets for its own gh and git calls on the step.
- The edit pass prints through the same line filter as the repair step, and the
  re-verify step carries the same 10-minute build ceiling.
- The no-base config check passes an explicit empty base, so it no longer
  depends on the environment it runs in.

* fix(docs-sync): filter the agent environment by credential name

- The child environment of a kilo run keeps the caller's provider and model
  settings and drops credential-shaped names, instead of naming a fixed list.
- The gateway credentials stay, and the policy test pins a credential-shaped
  key from a step that does not exist yet.

* docs(docs-sync): state the name rule at the scope the filter applies

* fix(docs-sync): complete the read-only bash scope of the docs agent

The docs-write permission map now denies every form of an allowed inspection
command that leaves the read-only scope. A guard check pins each such deny entry
after the allow rule it qualifies, because permission evaluation takes the last
matching rule.

* fix(docs-sync): refuse an ask in a mode's rule map

A headless pass cannot answer a permission prompt, so a mode that leaves one
open fails at that tool call instead of before the model starts. Restrict the
accepted actions to allow and deny, name them in both validation messages, and
pin the rejection at a whole rule and at one pattern.

* test(docs-sync): wrap the new assertion to the file's width
2026-11-22 04:29:06 +01:00

317 lines
14 KiB
YAML

# kilocode_change - new file
name: docs-sync
# Daily bot: collects PRs merged to Kilo-Org/cloud and Kilo-Org/kilocode,
# triages them for docs relevance, runs Kilo CLI headless to update
# packages/kilo-docs, and maintains one rolling PR for human review.
#
# Security posture: scheduled/manual runs check out the dispatched ref and may
# push/comment with write permissions and org secrets. PR runs (paths-limited to
# this workflow and .github/docs-sync/**) execute branch code only in a
# read-only, secretless `selftest` job that never pushes, comments, or calls an
# LLM. `pull_request` (not `pull_request_target`) keeps fork tokens read-only.
#
# Every headless Kilo CLI pass runs under the committed policy in
# .github/docs-sync/agent-permissions.json (exported as KILO_CONFIG_CONTENT), so
# the CLI itself bounds the agent's tools and write paths. Each pass carries only
# the gateway credentials it calls, the checkout persists no git credential, and
# the steps that need git authentication get the token from their own
# environment through the credential helper installed below.
#
# State is derived from the bot's own PRs (watermark marker in the PR body), so
# missed or failed runs self-heal on the next run.
on:
schedule:
- cron: "0 7 * * *" # 07:00 UTC daily
workflow_dispatch:
inputs:
since:
description: "Override watermark (ISO date, e.g. 2026-07-20). Default: last processed-through marker, 72h fallback, 14d cap."
required: false
type: string
dry_run:
description: "Collect + triage only, no edits, no PR"
type: boolean
default: false
pull_request:
paths:
- ".github/docs-sync/**"
- ".github/workflows/docs-sync.yml"
permissions:
contents: write # push the rolling branch, create the auto-docs label
pull-requests: write # create/update the rolling PR
issues: write # comment on the rolling PR
concurrency:
group: ${{ github.event_name == 'pull_request' && format('docs-sync-pr-{0}', github.event.pull_request.number) || 'docs-sync' }}
cancel-in-progress: false
env:
TRIAGE_MODEL: ${{ vars.DOCS_SYNC_TRIAGE_MODEL || 'kilo/deepseek/deepseek-v4.1-flash' }}
EDIT_MODEL: ${{ vars.DOCS_SYNC_EDIT_MODEL || 'kilo/deepseek/deepseek-v4.1-flash' }}
# Model id and reasoning effort are both taken from the Kilo gateway model
# list at https://api.kilo.ai/api/openrouter/models (equivalently
# `kilo models kilo`), which serves `deepseek/deepseek-v4.1-flash` with
# `opencode.variants` none/low/high/max; `kilo run --help` documents the
# `--variant` flag that selects one. Overridable, but defaults to `max`: the
# whole point of this bot is hard docs reasoning.
DOCS_SYNC_VARIANT: "max"
jobs:
selftest:
if: github.repository == 'Kilo-Org/kilocode'
runs-on: blacksmith-4vcpu-ubuntu-2404
permissions:
contents: read
steps:
- name: Checkout repository
uses: actions/checkout@v6
- name: Setup Node
uses: actions/setup-node@v6
with:
node-version: "24"
package-manager-cache: false
- name: Run docs-sync selftest
run: node .github/docs-sync/selftest.mjs
- name: Run docs-sync unit tests
run: for f in .github/docs-sync/*.test.mjs; do node "$f"; done
sync:
if: github.repository == 'Kilo-Org/kilocode' && github.event_name != 'pull_request'
runs-on: blacksmith-4vcpu-ubuntu-2404
# Budget: 4 setup/collect + 10 learn + 90 triage + 120 edit + 2 verify + 10 fix + 10 re-verify + 2 upsert = 248 min, 2-minute reserve.
# These are ceilings, not costs: a caught-up run triages ~2 chunks and edits
# ~1 batch and finishes in ~25 min. The old 35/50 pair was the binding
# constraint on backlog drain — run 30306629290 deferred 54 PRs untriaged and
# 31 unedited purely on budget, with no attempt made. See the throughput note
# in the PR description for the arithmetic.
timeout-minutes: 250
steps:
- name: Checkout repository
uses: actions/checkout@v6
with:
fetch-depth: 0 # prepare-branch merges main into the rolling branch
persist-credentials: false # git steps authenticate per step, see Install git credential helper
- name: Install git credential helper
# Written outside the worktree, so prepare-branch's branch switches cannot
# replace it mid-step. The helper reads the token from the calling step's
# own environment, so no credential is written to .git/config.
run: |
cat > "$RUNNER_TEMP/docs-sync-credential.sh" <<'HELPER'
test "$1" = get || exit 0
test -n "$GH_TOKEN" || exit 0
printf 'username=x-access-token\npassword=%s\n' "$GH_TOKEN"
HELPER
chmod +x "$RUNNER_TEMP/docs-sync-credential.sh"
- name: Configure git identity
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
- name: Setup Node
uses: actions/setup-node@v6
with:
node-version: "24"
package-manager-cache: false
- name: Run docs-sync selftest
run: node .github/docs-sync/selftest.mjs
- name: Install Kilo CLI
run: |
npm install -g @kilocode/cli
kilo --version
- name: Resolve watermark
id: wm
env:
GH_TOKEN: ${{ github.token }}
INPUT_SINCE: ${{ inputs.since }}
run: node .github/docs-sync/watermark.mjs
- name: Learn from maintainer corrections
id: learn
continue-on-error: true
env:
GH_TOKEN: ${{ github.token }}
# The CLI reads these natively; only the steps that call it carry them.
KILO_API_KEY: ${{ secrets.KILO_API_KEY }}
KILO_ORG_ID: ${{ secrets.KILO_ORG_ID }}
GIT_CONFIG_COUNT: "1"
GIT_CONFIG_KEY_0: credential.helper
GIT_CONFIG_VALUE_0: ${{ runner.temp }}/docs-sync-credential.sh
LEARNINGS_BUDGET_MINUTES: "10"
DRY_RUN: ${{ inputs.dry_run }}
run: node .github/docs-sync/learn.mjs
- name: Collect merged PRs
id: collect
env:
GH_TOKEN: ${{ github.token }}
run: node .github/docs-sync/collect.mjs --since "${{ steps.wm.outputs.since }}"
- name: Triage merged PRs (LLM, chunked)
id: triage
if: steps.collect.outputs.count != '0'
env:
KILO_API_KEY: ${{ secrets.KILO_API_KEY }}
KILO_ORG_ID: ${{ secrets.KILO_ORG_ID }}
SINCE_OVERRIDE: ${{ steps.wm.outputs.since_override }}
# Default 35 fit only 8 of 11 chunks on a 254-PR window. Headroom for
# a chunk that has to re-read a large digest.
TRIAGE_BUDGET_MINUTES: "90"
run: node .github/docs-sync/triage.mjs
- name: Filter docs-worthy PRs
id: worthy
if: steps.collect.outputs.count != '0'
run: |
node .github/docs-sync/filter-worthy.mjs \
docs-sync-out/digest-full.json docs-sync-out/triage.json docs-sync-out/worthy.json
count=$(node -p "require('./docs-sync-out/worthy.json').length")
echo "count=$count" >> "$GITHUB_OUTPUT"
if [ "$count" = "0" ]; then
echo "No docs-worthy PRs in this window; skipping edit/verify/PR."
fi
- name: Setup Bun
if: (steps.worthy.outputs.count || '0') != '0' && inputs.dry_run != true
uses: ./.github/actions/setup-bun
- name: Prepare rolling branch
id: prep
if: (steps.worthy.outputs.count || '0') != '0' && inputs.dry_run != true
env:
GH_TOKEN: ${{ github.token }}
GIT_CONFIG_COUNT: "1"
GIT_CONFIG_KEY_0: credential.helper
GIT_CONFIG_VALUE_0: ${{ runner.temp }}/docs-sync-credential.sh
run: node .github/docs-sync/prepare-branch.mjs
# After prepare-branch checks out the rolling branch and merges main, the
# worktree holds main's scripts. Restore the dispatched ref's copies so a
# branch-dispatch AC9 run actually exercises the fixed code. git restore
# (not checkout) leaves them unstaged so upsert-pr's bare commit won't
# include them in the docs PR.
- name: Restore docs-sync scripts from the dispatched ref
if: (steps.worthy.outputs.count || '0') != '0' && inputs.dry_run != true
run: git restore --source=${{ github.sha }} -- .github/docs-sync
- name: Update docs (Kilo CLI, batched)
if: (steps.worthy.outputs.count || '0') != '0' && inputs.dry_run != true
continue-on-error: true
env:
KILO_API_KEY: ${{ secrets.KILO_API_KEY }}
KILO_ORG_ID: ${{ secrets.KILO_ORG_ID }}
# Default 50 fit only 4 of 11 batches. A healthy batch is ~8 min,
# and edit.mjs will not start a batch without EDIT_BATCH_TIMEOUT_MINUTES
# (15) left, so 120 covers 14 batches = 70 PRs against ~5 worthy/day.
EDIT_BUDGET_MINUTES: "120"
# This pass streams the CLI's own output, so the same line filter that
# guards the fix step must guard this stream before it reaches the job log.
run: |
set -o pipefail
node .github/docs-sync/edit.mjs | node .github/docs-sync/redact-stream.mjs
- name: Verify docs build and tests
id: verify
if: (steps.worthy.outputs.count || '0') != '0' && inputs.dry_run != true
continue-on-error: true
env:
NEXT_PUBLIC_POSTHOG_KEY: ${{ secrets.POSTHOG_API_KEY }}
run: |
set -o pipefail
{ bun run --filter @kilocode/kilo-docs build && bun run --filter @kilocode/kilo-docs test; } 2>&1 | tee docs-sync-out/verify.log
- name: Fix verify failures (one pass)
id: fix
if: steps.verify.outcome == 'failure'
continue-on-error: true
timeout-minutes: 10
env:
KILO_API_KEY: ${{ secrets.KILO_API_KEY }}
KILO_ORG_ID: ${{ secrets.KILO_ORG_ID }}
run: |
set -o pipefail
# The run's tool and path scope comes from agent-permissions.json
# (mode docs-write), passed as KILO_CONFIG_CONTENT: markdown docs and
# navigation files, read-only inspection, and the docs build and tests.
# Build the policy into a variable first and require a non-empty
# value: this step runs only when the policy is in place, and
# agent-policy.mjs validates the mode as it builds it.
if ! KILO_POLICY="$(node .github/docs-sync/agent-policy.mjs docs-write)" || [ -z "$KILO_POLICY" ]; then
echo "::error::the docs-write agent policy is required for this pass and could not be built"
exit 1
fi
KILO_CONFIG_CONTENT="$KILO_POLICY" \
kilo run "The docs build or tests failed. Read the attached docs-sync-out/verify.log and fix the packages/kilo-docs changes so they pass. Do not revert doc edits; fix them. Do not modify anything outside packages/kilo-docs." \
-m "$EDIT_MODEL" --variant "$DOCS_SYNC_VARIANT" --dir "$GITHUB_WORKSPACE" -f docs-sync-out/verify.log \
| node .github/docs-sync/redact-stream.mjs \
| tee -a docs-sync-out/edit-log.txt \
|| echo "::warning::kilo fix pass exited nonzero; re-verifying anyway"
- name: Re-verify docs after the fix pass
id: verify2
if: steps.verify.outcome == 'failure'
continue-on-error: true
timeout-minutes: 10
env:
NEXT_PUBLIC_POSTHOG_KEY: ${{ secrets.POSTHOG_API_KEY }}
run: |
set -o pipefail
# This step decides the outcome: a failed build or test fails it, and
# `set -o pipefail` is what carries that through the tee.
{ bun run --filter @kilocode/kilo-docs build && bun run --filter @kilocode/kilo-docs test; } 2>&1 | tee docs-sync-out/verify2.log
- name: Re-verify status
id: verified
if: (steps.worthy.outputs.count || '0') != '0' && inputs.dry_run != true
env:
VERIFY_OUTCOME: ${{ steps.verify.outcome }}
VERIFY2_OUTCOME: ${{ steps.verify2.outcome }}
run: |
if [ "$VERIFY_OUTCOME" = "success" ] || [ "$VERIFY2_OUTCOME" = "success" ]; then
echo "ok=true" >> "$GITHUB_OUTPUT"
else
echo "ok=false" >> "$GITHUB_OUTPUT"
fi
- name: Write the learnings file
if: (steps.worthy.outputs.count || '0') != '0' && inputs.dry_run != true
run: node .github/docs-sync/learn.mjs --apply
- name: Upsert rolling PR
if: (steps.worthy.outputs.count || '0') != '0' && inputs.dry_run != true
env:
GH_TOKEN: ${{ github.token }}
GIT_CONFIG_COUNT: "1"
GIT_CONFIG_KEY_0: credential.helper
GIT_CONFIG_VALUE_0: ${{ runner.temp }}/docs-sync-credential.sh
# Read-only token for the Kilo-Org/cloud history; a cloud surface
# falls back to the fixed `other` reviewers when it is unset.
CLOUD_REPO_TOKEN: ${{ secrets.CROSS_REPO_ACCESS_TOKEN }}
LEARNED_THROUGH: ${{ steps.learn.outputs.learned_through }}
PROCESSED_THROUGH: ${{ steps.wm.outputs.now }}
SINCE: ${{ steps.wm.outputs.since }}
SINCE_OVERRIDE: ${{ steps.wm.outputs.since_override }}
BRANCH: ${{ steps.prep.outputs.branch }}
PREP_MODE: ${{ steps.prep.outputs.mode }}
VERIFIED: ${{ steps.verified.outputs.ok }}
run: node .github/docs-sync/upsert-pr.mjs
- name: Upload run artifacts
if: always()
uses: actions/upload-artifact@v4
with:
name: docs-sync-out
path: docs-sync-out/
retention-days: 14
if-no-files-found: ignore