From d2fea9b400763b85012293350a90f53a3788cd43 Mon Sep 17 00:00:00 2001 From: Asgeir Frimannsson Date: Fri, 2 Oct 2026 10:14:14 +0200 Subject: [PATCH 01/20] Rename the cache-tm input to parse-cache The input caches kapi's parse cache, which has nothing to do with content memory. cache-tm stays as a deprecated alias: when a workflow sets it, it decides and the action prints a warning naming parse-cache. One step resolves the value and both conditions read it. The input description and the README now describe kapi 1.3: the project's context lives in kapi's workspace and moves through a context backend with kapi context pull, which this action leaves to kapi-action's context-sync input or the workflow's own step. The test workflow covers parse-cache: false, cache-tm: false, and cache-tm deciding over parse-cache. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01PWc9qYQJ93y88XrN6TkMvT --- .github/workflows/test.yml | 49 +++++++++++++++++++++++++++++ README.md | 14 +++++---- action.yml | 64 +++++++++++++++++++++++++++----------- 3 files changed, 103 insertions(+), 24 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7c331e6..1e99657 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -260,6 +260,55 @@ jobs: name: state-cache-restore-${{ matrix.os }} path: ${{ runner.temp }}/results + # parse-cache and its deprecated name cache-tm. The save job left a parse + # cache to restore, and no kapi command runs here, so whether the cache + # directory exists after setup shows which value the action acted on. When + # cache-tm is set, it decides. + test-parse-cache-input: + name: "Parse cache input (${{ matrix.case }})" + needs: test-state-cache-save + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + include: + - case: "parse-cache: false" + parse_cache: "false" + cache_tm: "" + restored: "false" + - case: "cache-tm: false" + parse_cache: "true" + cache_tm: "false" + restored: "false" + - case: "cache-tm: true over parse-cache: false" + parse_cache: "false" + cache_tm: "true" + restored: "true" + steps: + - uses: actions/checkout@v6 + + - name: Setup kapi + uses: ./ + with: + token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} + version: ${{ env.STATE_CACHE_KAPI_VERSION }} + plugins: "" + project-dir: test/fixture + parse-cache: ${{ matrix.parse_cache }} + cache-tm: ${{ matrix.cache_tm }} + + - name: Check whether the parse cache was restored + shell: bash + env: + WANT_RESTORED: ${{ matrix.restored }} + run: | + docs=test/fixture/.kapi/work/cache/docs + if [ "${WANT_RESTORED}" = "true" ]; then + test -s "${docs}/index.db" + else + test ! -e "${docs}" + fi + test-cache: name: "Cache hit (${{ matrix.os }})" runs-on: ${{ matrix.os }} diff --git a/README.md b/README.md index 4390ddd..30aec44 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,8 @@ The newest stable release, which `latest` installs, has no `kapi up`, so this ex | `plugins` | Newline- or comma-separated plugin refs to install, as the registry names them (`bowrain`, `okapi-bridge`; a `kapi-` prefix is stripped). Pass `''` to install nothing | `bowrain` | No | | `auth-token` | Bowrain server JWT, exported as `BOWRAIN_AUTH_TOKEN` | — | No | | `server` | Bowrain server URL, exported as `BOWRAIN_SERVER_URL` | — | No | -| `cache-tm` | Carry kapi's parse cache (`.kapi/work/cache/docs`) between runs with the job cache; see [Project parse cache](#project-parse-cache). Runs only when a `kapi.yaml` recipe (or legacy `*.kapi`) is present. Set `false` to disable | `true` | No | +| `parse-cache` | Carry kapi's parse cache (`.kapi/work/cache/docs`) between runs with the job cache; see [Project parse cache](#project-parse-cache). Runs only when a `kapi.yaml` recipe (or legacy `*.kapi`) is present. Set `false` to disable | `true` | No | +| `cache-tm` | Deprecated name of `parse-cache`. When set, it decides in place of `parse-cache`, and the action prints a warning | `''` | No | | `project-dir` | Directory holding the `kapi.yaml` project whose parse cache is carried between runs | `.` | No | ## Outputs @@ -105,7 +106,7 @@ image. 4. **Add to PATH** — makes `kapi` available to all subsequent steps. 5. **Configure auth** (optional) — exports `BOWRAIN_AUTH_TOKEN`/`BOWRAIN_SERVER_URL` when `auth-token` is set. 6. **Install plugins** — installs each ref in `plugins` (default: `bowrain`) via `kapi plugins install`, cached keyed on the plugin set + OS + arch. Refs use the registry names; a `kapi-` binary prefix is stripped (`kapi-bowrain` → `bowrain`). -7. **Restore the project parse cache** (when a `kapi.yaml` recipe, or legacy `*.kapi`, is present): restores `.kapi/work/cache/docs` from the job cache, and saves it again at job end under a key unique to the job and run attempt. See [Project parse cache](#project-parse-cache). Disable with `cache-tm: false`. +7. **Restore the project parse cache** (when a `kapi.yaml` recipe, or legacy `*.kapi`, is present): restores `.kapi/work/cache/docs` from the job cache, and saves it again at job end under a key unique to the job and run attempt. See [Project parse cache](#project-parse-cache) for what it leaves to `kapi context pull`. Disable with `parse-cache: false`. ## Caching @@ -113,14 +114,15 @@ The binary is cached keyed on version + OS + arch; plugins are cached keyed on t ### Project parse cache -kapi keeps the state it derives out of git, under `.kapi/work/` (the project's `.kapi/.gitignore` ignores `work/` and `filters.local.json`). With `cache-tm` on and a recipe in `project-dir`, the action restores one directory of it, `.kapi/work/cache/docs`, before your steps run, and `actions/cache` saves it again when the job ends. kapi records there how it parsed each source and target file, so a later run can replay a file instead of parsing it again. +kapi keeps what it derives from a checkout under `.kapi/`, out of git. With `parse-cache` on and a recipe in `project-dir`, the action restores one directory of it, `.kapi/work/cache/docs`, before your steps run, and `actions/cache` saves it again when the job ends. kapi records there how it parsed each source and target file, so a later run can replay a file instead of parsing it again. A restored parse cache does not change any result. kapi keys each entry by the file's path and content hash, the parse configuration, the recipe and the kapi build, and parses the file again when any of them differs. The test workflow checks this on every change: with the cache restored, `kapi status`, `kapi check`, `kapi check --ship` and `kapi up` must match a cold run exactly, in exit codes, output and every file written, and they must still match after the source changes. -Everything else under `.kapi/` stays out of the cache: +The project's context (its terms, voice profiles, content memory and recorded decisions) lives in kapi's workspace, outside git, and moves between machines through a context backend (kapi 1.3 and later). A runner starts with an empty workspace, so a job that needs the context runs `kapi context pull` before kapi works, and `kapi context push` after a run records something. setup-kapi runs neither command: [`kapi-action`](https://github.com/neokapi/kapi-action)'s `context-sync` input runs them around its command, or your workflow can run them as steps of its own. -- The content memory, terms, voice profile and unit-state record are committed under `.kapi/`, so the checkout already holds them, and kapi builds its local store from them. -- `.kapi/work/store.db` also holds stored targets and staged review decisions. Restored from an earlier run, it reports targets the checkout does not hold, and `kapi status`, `kapi check --ship` and `kapi up` report differently than they would on a cold run. +Everything else under `.kapi/work/` stays out of the cache: + +- `.kapi/work/store.db` holds this checkout's block cache and the targets a run wrote. Restored from an earlier run, it reports targets the checkout does not hold, and `kapi status`, `kapi check --ship` and `kapi up` report differently than they would on a cold run. - `.kapi/work/cache/extractions/` holds `kapi extract` batches for `kapi merge`, and `.kapi/work/cache/redaction/` and `.kapi/work/vault/` hold withheld original values. - `.kapi/work/cache/sync-cache.json` and `.kapi/work/cache/refs.json` hold server sync state, including a claim token. diff --git a/action.yml b/action.yml index a7bb2a1..82da3d5 100644 --- a/action.yml +++ b/action.yml @@ -30,20 +30,29 @@ inputs: description: "Bowrain server URL (exported as BOWRAIN_SERVER_URL)" required: false default: "" - cache-tm: + parse-cache: description: >- Carry kapi's parse cache (.kapi/work/cache/docs) between CI runs with the - job cache. kapi keeps the state it derives out of git, under .kapi/work/. - The content memory, terms and unit-state record are committed under - .kapi/, and kapi builds its local store from them in a fresh checkout, so - they need no cache. Only the parse cache is restored: each entry is keyed - by the file's content, the parse configuration, the recipe and the kapi - build, and kapi parses a file again when any of them differs. The store - and the rest of .kapi/work/ are never restored, because restoring them - changes what kapi status, kapi check and kapi up report. Runs only when a - kapi.yaml recipe (or legacy *.kapi) is present. Set 'false' to disable. + job cache. From kapi 1.3 the project's context (terms, voice profiles, + content memory and recorded decisions) lives in kapi's workspace outside + git and is shared through a context backend, and .kapi/ holds a cache of + what kapi derives from the checkout. A job fetches the context with + `kapi context pull`, through kapi-action's `context-sync` input or a step + of its own; this action restores only the parse cache. Each entry is + keyed by the file's content, the parse configuration, the recipe and the + kapi build, and kapi parses a file again when any of them differs. The + store and the rest of .kapi/work/ are never restored, because restoring + them changes what kapi status, kapi check and kapi up report. Runs only + when a kapi.yaml recipe (or legacy *.kapi) is present. Set 'false' to + disable. required: false default: "true" + cache-tm: + description: >- + Deprecated: use parse-cache. When set, it decides in place of + parse-cache, and the action prints a warning. + required: false + default: "" project-dir: description: "Directory holding the kapi.yaml project whose parse cache is carried between runs. Default: repository root." required: false @@ -190,12 +199,28 @@ runs: kapi plugins install ${args[@]+"${args[@]}"} "${plugin}" done <<< "${PLUGINS}" - # 11. Detect a kapi project: a committed kapi.yaml recipe (or legacy *.kapi). + # 11. Resolve whether to carry the parse cache. cache-tm is the deprecated + # name of parse-cache; a workflow that still sets it keeps its meaning. + - name: Resolve parse cache input + id: parse-cache + shell: bash + env: + INPUT_PARSE_CACHE: ${{ inputs.parse-cache }} + INPUT_CACHE_TM: ${{ inputs.cache-tm }} + run: | + ENABLED="${INPUT_PARSE_CACHE}" + if [ -n "${INPUT_CACHE_TM}" ]; then + echo "::warning title=setup-kapi::The cache-tm input is deprecated; use parse-cache instead." + ENABLED="${INPUT_CACHE_TM}" + fi + echo "enabled=${ENABLED}" >> "$GITHUB_OUTPUT" + + # 12. Detect a kapi project: a committed kapi.yaml recipe (or legacy *.kapi). # The parse cache is carried only for a project. The recipe is the # signal because .kapi/work/ is gitignored and absent from a checkout. - name: Detect kapi project id: detect-project - if: inputs.cache-tm != 'false' + if: steps.parse-cache.outputs.enabled != 'false' shell: bash env: PROJECT_DIR: ${{ inputs.project-dir }} @@ -207,7 +232,7 @@ runs: echo "found=false" >> "$GITHUB_OUTPUT" fi - # 12. Restore kapi's parse cache, and save it at job end. The key is unique + # 13. Restore kapi's parse cache, and save it at job end. The key is unique # to the job and run attempt, so actions/cache saves through its own # post step; the restore takes the newest cache for the same kapi # version, preferring the same ref. @@ -217,19 +242,22 @@ runs: # recipe and the kapi build, and parses again on any mismatch, so a # restored entry is either valid for the checkout or ignored. The rest # of .kapi/work/ stays out of the cache: - # - store.db holds stored targets and the unit working set; restored, - # it changes what kapi status, kapi check --ship and kapi up report. + # - store.db holds the checkout's block cache and the targets a run + # wrote; restored, it changes what kapi status, kapi check --ship + # and kapi up report. # - cache/extractions holds kapi extract batches that kapi merge reads. # - cache/redaction and vault/ hold withheld original values. # - cache/sync-cache.json and cache/refs.json hold server sync state, # including a claim token. - # The content memory, terms and unit-state record are committed under - # .kapi/ and come with the checkout. + # The project's context (terms, voice profiles, content memory and + # recorded decisions) lives in kapi's workspace outside the checkout. A + # job fetches it with `kapi context pull`, which this action leaves to + # kapi-action's context-sync input or the workflow's own step. # # The kapi version is part of the key because the parse cache keeps the # entries every build wrote; a new version starts from an empty cache. - name: Restore kapi parse cache - if: inputs.cache-tm != 'false' && steps.detect-project.outputs.found == 'true' + if: steps.parse-cache.outputs.enabled != 'false' && steps.detect-project.outputs.found == 'true' uses: actions/cache@v5 with: path: ${{ inputs.project-dir }}/.kapi/work/cache/docs From d8b8be899946bf2a2fb53c53ab890a75d191de3b Mon Sep 17 00:00:00 2001 From: Asgeir Frimannsson Date: Mon, 23 Nov 2026 18:34:15 +0100 Subject: [PATCH 02/20] Carry the parse cache from the state directory kapi state path names A kapi that keeps each checkout's state outside the checkout prints that directory with `kapi state path`. The action keeps the cache in a fixed directory under runner.temp and links the state directory's work/cache/docs to it, so actions/cache saves it in its post step. A kapi without the command keeps the in-tree path. A pull_request_target run restores and never saves. Verified: test/parse-cache/locate.sh with the stub, against kapi 1.4.0-rc5 (tree) and a build of neokapi#3780 (state); two-run warm/cold comparison with the #3780 build; actionlint; shellcheck Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01GtGobCdyR3d9yLXAfX9jsr --- .github/workflows/test.yml | 54 +++++++++++++++++++++-- README.md | 33 +++++++------- action.yml | 81 +++++++++++++++++++++++----------- scripts/parse-cache-dir.sh | 66 ++++++++++++++++++++++++++++ test/parse-cache/locate.sh | 89 ++++++++++++++++++++++++++++++++++++++ test/parse-cache/stub/kapi | 12 +++++ 6 files changed, 291 insertions(+), 44 deletions(-) create mode 100755 scripts/parse-cache-dir.sh create mode 100755 test/parse-cache/locate.sh create mode 100755 test/parse-cache/stub/kapi diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1e51ae5..0b67286 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -313,6 +313,54 @@ jobs: test ! -e "${docs}" fi + # Where the parse cache is carried. scripts/parse-cache-dir.sh asks kapi for + # the checkout's state directory with `kapi state path`; the harness answers + # with a kapi stub, once as a kapi that keeps state outside the checkout and + # once as one without the command, and checks the link it makes. + test-parse-cache-location: + name: "Parse cache location (kapi stub, ${{ matrix.os }})" + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest] + steps: + - uses: actions/checkout@v6 + + - name: Run the location harness + shell: bash + run: test/parse-cache/locate.sh + + # Released kapi versions without `kapi state path` keep the parse cache in + # the checkout. The action must find that path, make no link, and leave the + # cache where kapi writes it. + test-parse-cache-in-tree: + name: "Parse cache in the checkout (kapi ${{ matrix.version }})" + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + version: ["1.3.0", "1.4.0-rc5"] + steps: + - uses: actions/checkout@v6 + + - name: Setup kapi + uses: ./ + with: + token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} + version: ${{ matrix.version }} + plugins: "" + project-dir: test/fixture + + - name: Check the action chose the checkout's path + shell: bash + run: | + test ! -e "${RUNNER_TEMP}/setup-kapi-parse-cache" + KAPI="$(command -v kapi)" test/parse-cache/locate.sh | tee "${RUNNER_TEMP}/locate.txt" + grep -qx 'real kapi: mode=tree' "${RUNNER_TEMP}/locate.txt" + kapi status -p test/fixture/kapi.yaml > /dev/null + test -s test/fixture/.kapi/work/cache/docs/index.db + test-cache: name: "Cache hit (${{ matrix.os }})" runs-on: ${{ matrix.os }} @@ -367,7 +415,7 @@ jobs: token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} version: ${{ env.PLUGIN_SEED_KAPI_VERSION }} plugins: bowrain - cache-tm: "false" + parse-cache: "false" - name: Leave an older bowrain in the plugin root shell: bash @@ -392,7 +440,7 @@ jobs: with: token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} plugins: bowrain - cache-tm: "false" + parse-cache: "false" - name: Require a newer kapi than the seed shell: bash @@ -422,7 +470,7 @@ jobs: with: token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} plugins: bowrain - cache-tm: "false" + parse-cache: "false" - name: Require a plugin cache hit shell: bash diff --git a/README.md b/README.md index f39d04a..0673d82 100644 --- a/README.md +++ b/README.md @@ -62,9 +62,9 @@ The newest stable release, which `latest` installs, has no `kapi up`, so this ex | `plugins` | Newline- or comma-separated plugin refs to install, as the registry names them (`bowrain`, `okapi-bridge`; a `kapi-` prefix is stripped). Pass `''` to install nothing | `bowrain` | No | | `auth-token` | Bowrain server JWT, exported as `BOWRAIN_AUTH_TOKEN` | — | No | | `server` | Bowrain server URL, exported as `BOWRAIN_SERVER_URL` | — | No | -| `parse-cache` | Carry kapi's parse cache (`.kapi/work/cache/docs`) between runs with the job cache; see [Project parse cache](#project-parse-cache). Runs only when a `kapi.yaml` recipe (or legacy `*.kapi`) is present. Set `false` to disable | `true` | No | +| `parse-cache` | Keep kapi's parse cache between runs, so a later run replays files it already parsed; see [Project parse cache](#project-parse-cache). Runs only when a `kapi.yaml` recipe (or legacy `*.kapi`) is present. Set `false` to turn it off | `true` | No | | `cache-tm` | Deprecated name of `parse-cache`. When set, it decides in place of `parse-cache`, and the action prints a warning | `''` | No | -| `project-dir` | Directory holding the `kapi.yaml` project whose parse cache is carried between runs | `.` | No | +| `project-dir` | Directory holding the `kapi.yaml` project whose parse cache is kept between runs | `.` | No | ## Outputs @@ -99,13 +99,13 @@ is paid once for each kapi version and plugin release. ## How it works -1. **Resolve version** — `latest` resolves the newest *stable CLI* release (a `vX.Y.Z` tag). It deliberately does not use GitHub's "latest release" flag: the same repository publishes plugin and app releases (`check-v0.1.0`, `asr-v0.1.1`, `bowrain-v…`), and that flag lands on whichever was published last. Pinned versions pass through. -2. **Cache check** — restores a cached binary keyed on `kapi-{version}-{os}-{arch}`. -3. **Download + verify** (on cache miss) — downloads the archive and `checksums.txt` from the GitHub release, verifies the SHA-256 checksum, and extracts the binary. -4. **Add to PATH** — makes `kapi` available to all subsequent steps. -5. **Configure auth** (optional) — exports `BOWRAIN_AUTH_TOKEN`/`BOWRAIN_SERVER_URL` when `auth-token` is set. -6. **Install plugins** — installs each ref in `plugins` (default: `bowrain`) via `kapi plugins install`, cached as described below. Refs use the registry names; a `kapi-` binary prefix is stripped (`kapi-bowrain` → `bowrain`). -7. **Restore the project parse cache** (when a `kapi.yaml` recipe, or legacy `*.kapi`, is present): restores `.kapi/work/cache/docs` from the job cache, and saves it again at job end under a key unique to the job and run attempt. See [Project parse cache](#project-parse-cache) for what it leaves to `kapi context pull`. Disable with `parse-cache: false`. +1. **Resolve version**: `latest` resolves the newest *stable CLI* release (a `vX.Y.Z` tag). It deliberately does not use GitHub's "latest release" flag: the same repository publishes plugin and app releases (`check-v0.1.0`, `asr-v0.1.1`, `bowrain-v…`), and that flag lands on whichever was published last. Pinned versions pass through. +2. **Cache check**: restores a cached binary keyed on `kapi-{version}-{os}-{arch}`. +3. **Download + verify** (on cache miss): downloads the archive and `checksums.txt` from the GitHub release, verifies the SHA-256 checksum, and extracts the binary. +4. **Add to PATH**: makes `kapi` available to all subsequent steps. +5. **Configure auth** (optional): exports `BOWRAIN_AUTH_TOKEN`/`BOWRAIN_SERVER_URL` when `auth-token` is set. +6. **Install plugins**: installs each ref in `plugins` (default: `bowrain`) via `kapi plugins install`, cached as described below. Refs use the registry names; a `kapi-` binary prefix is stripped (`kapi-bowrain` → `bowrain`). +7. **Restore the project parse cache** (when a `kapi.yaml` recipe, or legacy `*.kapi`, is present): restores kapi's parse cache from the job cache, and saves it again at job end under a key unique to the job and run attempt. See [Project parse cache](#project-parse-cache). Disable with `parse-cache: false`. ## Caching @@ -113,20 +113,21 @@ The binary is cached keyed on version + OS + arch. Plugins are cached keyed on t ### Project parse cache -kapi keeps what it derives from a checkout under `.kapi/`, out of git. With `parse-cache` on and a recipe in `project-dir`, the action restores one directory of it, `.kapi/work/cache/docs`, before your steps run, and `actions/cache` saves it again when the job ends. kapi records there how it parsed each source and target file, so a later run can replay a file instead of parsing it again. +kapi records how it parsed each source and target file, so a later run can replay a file instead of parsing it again. With `parse-cache` on and a recipe in `project-dir`, the action restores that parse cache before your steps run, and `actions/cache` saves it again when the job ends. Run `actions/checkout` before this action, so the checkout it finds is the one your kapi steps use. -A restored parse cache does not change any result. kapi keys each entry by the file's path and content hash, the parse configuration, the recipe and the kapi build, and parses the file again when any of them differs. The test workflow checks this on every change: with the cache restored, `kapi status`, `kapi check`, `kapi check --ship` and `kapi up` must match a cold run exactly, in exit codes, output and every file written, and they must still match after the source changes. +Where the parse cache lives depends on the kapi version: -The project's context (its terms, voice profiles, content memory and recorded decisions) lives in kapi's workspace, outside git, and moves between machines through a context backend (kapi 1.3 and later). A runner starts with an empty workspace, so a job that needs the context runs `kapi context pull` before kapi works, and `kapi context push` after a run records something. setup-kapi runs neither command: [`kapi-action`](https://github.com/neokapi/kapi-action)'s `context-sync` input runs them around its command, or your workflow can run them as steps of its own. +- A kapi that keeps each checkout's state outside the checkout prints that directory with `kapi state path -p `. The directory is named for the checkout folder, which is new on every hosted runner, so the action keeps the cache in a fixed directory under `runner.temp` and links the state directory's `work/cache/docs` to it. kapi reads and writes the parse cache through the link. On Windows runners the action carries no parse cache for this kapi, and on a runner that already holds a parse cache in the state directory it leaves that cache alone. +- A kapi without that command (1.3 and earlier) keeps the parse cache in the checkout, and the action caches `/.kapi/work/cache/docs`. -Everything else under `.kapi/work/` stays out of the cache: +A restored parse cache does not change any result. kapi keys each entry by the file's path and content hash, the parse configuration, the recipe and the kapi build, and parses the file again when any of them differs. The test workflow checks this on every change: with the cache restored, `kapi status`, `kapi check`, `kapi check --ship` and `kapi up` must match a cold run exactly, in exit codes, output and every file written, and they must still match after the source changes. -- `.kapi/work/store.db` holds this checkout's block cache and the targets a run wrote. Restored from an earlier run, it reports targets the checkout does not hold, and `kapi status`, `kapi check --ship` and `kapi up` report differently than they would on a cold run. -- `.kapi/work/cache/extractions/` holds `kapi extract` batches for `kapi merge`, and `.kapi/work/cache/redaction/` and `.kapi/work/vault/` hold withheld original values. -- `.kapi/work/cache/sync-cache.json` and `.kapi/work/cache/refs.json` hold server sync state, including a claim token. +The action carries the parse cache and nothing else. The project store holds this checkout's blocks and the targets a run wrote, and a restored copy changes what `kapi status`, `kapi check --ship` and `kapi up` report. The extraction batches, the redaction vault and the sync state (with its claim token) belong to the checkout that wrote them. The project's context (its terms, voice profiles, content memory and recorded decisions) lives in kapi's workspace and moves between machines through a context backend (kapi 1.3 and later). A job that needs it runs `kapi context pull` before kapi works and `kapi context push` after a run records something. [`kapi-action`](https://github.com/neokapi/kapi-action)'s `context-sync` input runs both around its command, or your workflow can run them as steps of its own. The cache key holds the runner OS, the resolved kapi version and the ref, so a new kapi version starts from an empty cache. How much time the cache saves depends on the formats: container formats such as DOCX replay faster than they parse, while JSON and Markdown parse about as fast as they replay. +kapi trusts what it finds in its parse cache, so who can write the cache decides whether a restored entry can be trusted. GitHub scopes caches by branch: a run restores only caches saved on its own branch, on the default branch, or, for a pull request, on its base branch. A cache saved by a pull request, including one from a fork, is never restored on the default branch. A `pull_request_target` run works in the base branch's scope, so there the action restores the parse cache and does not save it. Any other workflow that runs untrusted code with this action can write to the cache of the branch it runs on; set `parse-cache: false` in such a workflow. + ## Platform support | Runner OS | Architectures | diff --git a/action.yml b/action.yml index e6ec776..603ac14 100644 --- a/action.yml +++ b/action.yml @@ -32,19 +32,14 @@ inputs: default: "" parse-cache: description: >- - Carry kapi's parse cache (.kapi/work/cache/docs) between CI runs with the - job cache. From kapi 1.3 the project's context (terms, voice profiles, - content memory and recorded decisions) lives in kapi's workspace outside - git and is shared through a context backend, and .kapi/ holds a cache of - what kapi derives from the checkout. A job fetches the context with - `kapi context pull`, through kapi-action's `context-sync` input or a step - of its own; this action restores only the parse cache. Each entry is - keyed by the file's content, the parse configuration, the recipe and the - kapi build, and kapi parses a file again when any of them differs. The - store and the rest of .kapi/work/ are never restored, because restoring - them changes what kapi status, kapi check and kapi up report. Runs only - when a kapi.yaml recipe (or legacy *.kapi) is present. Set 'false' to - disable. + Keep kapi's parse cache between CI runs, so a later run replays the files + it already parsed instead of parsing them again. A restored entry never + changes a result: kapi parses a file again when its content, the parse + configuration, the recipe or the kapi build differs. Only the parse cache + is carried, never the project store or the project's context; a job that + needs the context runs `kapi context pull`. Runs only when a kapi.yaml + recipe (or legacy *.kapi) is present in project-dir. Set 'false' to turn + it off. required: false default: "true" cache-tm: @@ -54,7 +49,7 @@ inputs: required: false default: "" project-dir: - description: "Directory holding the kapi.yaml project whose parse cache is carried between runs. Default: repository root." + description: "Directory holding the kapi.yaml project whose parse cache is kept between runs. Default: repository root." required: false default: "." @@ -224,7 +219,8 @@ runs: # 12. Detect a kapi project: a committed kapi.yaml recipe (or legacy *.kapi). # The parse cache is carried only for a project. The recipe is the - # signal because .kapi/work/ is gitignored and absent from a checkout. + # signal because kapi's state is never committed, so a fresh checkout + # holds none. - name: Detect kapi project id: detect-project if: steps.parse-cache.outputs.enabled != 'false' @@ -239,23 +235,41 @@ runs: echo "found=false" >> "$GITHUB_OUTPUT" fi - # 13. Restore kapi's parse cache, and save it at job end. The key is unique + # 13. Find the directory that holds the parse cache for this kapi, and + # prepare it. A kapi that keeps each checkout's state outside the + # checkout prints that directory with `kapi state path`; its name + # changes from run to run, so the cache is carried through a fixed + # directory under runner.temp that the state directory links to. An + # older kapi keeps the parse cache in the checkout. See + # scripts/parse-cache-dir.sh. + - name: Locate kapi parse cache + id: parse-cache-dir + if: steps.parse-cache.outputs.enabled != 'false' && steps.detect-project.outputs.found == 'true' + shell: bash + env: + ACTION_PATH: ${{ github.action_path }} + PROJECT_DIR: ${{ inputs.project-dir }} + FIXED_DIR: ${{ runner.temp }}/setup-kapi-parse-cache + run: | + set -o pipefail + "${ACTION_PATH}/scripts/parse-cache-dir.sh" "${PROJECT_DIR}" "${FIXED_DIR}" | tee -a "$GITHUB_OUTPUT" + + # 14. Restore kapi's parse cache, and save it at job end. The key is unique # to the job and run attempt, so actions/cache saves through its own # post step; the restore takes the newest cache for the same kapi # version, preferring the same ref. # - # Only .kapi/work/cache/docs is carried. kapi keys each entry by the - # file's absolute path and content hash, the parse configuration, the - # recipe and the kapi build, and parses again on any mismatch, so a - # restored entry is either valid for the checkout or ignored. The rest - # of .kapi/work/ stays out of the cache: + # Only the parse cache (work/cache/docs) is carried. kapi keys each + # entry by the file's absolute path and content hash, the parse + # configuration, the recipe and the kapi build, and parses again on any + # mismatch, so a restored entry is either valid for the checkout or + # ignored. The rest of the work directory stays out of the cache: # - store.db holds the checkout's block cache and the targets a run # wrote; restored, it changes what kapi status, kapi check --ship # and kapi up report. # - cache/extractions holds kapi extract batches that kapi merge reads. # - cache/redaction and vault/ hold withheld original values. - # - cache/sync-cache.json and cache/refs.json hold server sync state, - # including a claim token. + # - the sync state holds a claim token. # The project's context (terms, voice profiles, content memory and # recorded decisions) lives in kapi's workspace outside the checkout. A # job fetches it with `kapi context pull`, which this action leaves to @@ -263,11 +277,28 @@ runs: # # The kapi version is part of the key because the parse cache keeps the # entries every build wrote; a new version starts from an empty cache. + # + # GitHub scopes caches by ref: a run restores caches its own ref saved + # and those of the default branch (and a pull request's base), never + # another branch's or a pull request's, so a pull request from a fork + # cannot reach the default branch's runs through this cache. A + # pull_request_target run executes in the base branch's scope, so there + # the cache is restored and never saved. - name: Restore kapi parse cache - if: steps.parse-cache.outputs.enabled != 'false' && steps.detect-project.outputs.found == 'true' + if: steps.parse-cache-dir.outputs.path != '' && github.event_name != 'pull_request_target' uses: actions/cache@v5 with: - path: ${{ inputs.project-dir }}/.kapi/work/cache/docs + path: ${{ steps.parse-cache-dir.outputs.path }} + key: kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}-${{ github.ref_name }}-${{ github.job }}-${{ github.run_id }}-${{ github.run_attempt }} + restore-keys: | + kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}-${{ github.ref_name }}- + kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}- + + - name: Restore kapi parse cache (no save) + if: steps.parse-cache-dir.outputs.path != '' && github.event_name == 'pull_request_target' + uses: actions/cache/restore@v5 + with: + path: ${{ steps.parse-cache-dir.outputs.path }} key: kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}-${{ github.ref_name }}-${{ github.job }}-${{ github.run_id }}-${{ github.run_attempt }} restore-keys: | kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}-${{ github.ref_name }}- diff --git a/scripts/parse-cache-dir.sh b/scripts/parse-cache-dir.sh new file mode 100755 index 0000000..dbf9993 --- /dev/null +++ b/scripts/parse-cache-dir.sh @@ -0,0 +1,66 @@ +#!/usr/bin/env bash +# Decide which directory setup-kapi carries as kapi's parse cache, and prepare +# it before the cache is restored. +# +# Usage: parse-cache-dir.sh +# Prints: mode= and path=, one per line, +# for $GITHUB_OUTPUT. Notes for the job log go to stderr. +# +# A kapi that keeps each checkout's state outside the checkout answers +# `kapi state path -p ` with that checkout's state directory. Its +# name comes from the checkout folder's identity, so a fresh runner gets a +# different one on every run, and actions/cache saves (in its post step) the +# path it was given at restore time. The cache therefore lives at a fixed +# directory under runner.temp, and the state directory's work/cache/docs is a +# link to it: kapi reads and writes the parse cache through the link, and the +# post step saves the fixed directory. +# +# A kapi without that command (1.3 and earlier) keeps the parse cache in the +# checkout, at /.kapi/work/cache/docs, and that path is cached. +set -euo pipefail + +project_dir="$1" +fixed_dir="$2" + +state_dir="" +if out="$(kapi state path -p "${project_dir}" 2> /dev/null)"; then + state_dir="$(printf '%s\n' "${out}" | sed '/^[[:space:]]*$/d' | tail -n 1)" +fi + +if [ -z "${state_dir}" ]; then + echo "mode=tree" + echo "path=${project_dir}/.kapi/work/cache/docs" + exit 0 +fi + +case "${state_dir}" in + /* | [A-Za-z]:[\\/]*) ;; + *) + echo "::warning title=setup-kapi::kapi state path printed '${state_dir}', which is not an absolute path; the parse cache is not carried." >&2 + echo "mode=none" + exit 0 + ;; +esac + +if [ "${RUNNER_OS:-}" = "Windows" ]; then + echo "setup-kapi: the parse cache is not carried on Windows with a kapi that keeps its state outside the checkout." >&2 + echo "mode=none" + exit 0 +fi + +docs="${state_dir}/work/cache/docs" +if [ -L "${docs}" ]; then + # A link an earlier setup-kapi run left on a runner that keeps its data. + rm "${docs}" +elif [ -e "${docs}" ]; then + # A runner that keeps kapi's data between jobs already holds the cache. + echo "setup-kapi: ${docs} already exists on this runner, so the parse cache is not restored over it." >&2 + echo "mode=none" + exit 0 +fi + +mkdir -p "${fixed_dir}" "${state_dir}/work/cache" +ln -s "${fixed_dir}" "${docs}" +echo "setup-kapi: kapi keeps this checkout's state in ${state_dir}; its parse cache is carried through ${fixed_dir}." >&2 +echo "mode=state" +echo "path=${fixed_dir}" diff --git a/test/parse-cache/locate.sh b/test/parse-cache/locate.sh new file mode 100755 index 0000000..b27473c --- /dev/null +++ b/test/parse-cache/locate.sh @@ -0,0 +1,89 @@ +#!/usr/bin/env bash +# Runs scripts/parse-cache-dir.sh against the kapi stub in stub/ for each case +# it handles, and asserts the mode and path it prints and what it leaves on +# disk. With KAPI set to a kapi binary, it also runs the script against that +# binary on test/fixture and prints the mode it chose. +set -euo pipefail + +here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +root="$(cd "${here}/../.." && pwd)" +script="${root}/scripts/parse-cache-dir.sh" +work="$(mktemp -d)" +trap 'rm -rf "${work}"' EXIT + +failures=0 +fail() { + echo "FAIL $1: $2" + failures=$((failures + 1)) +} +value() { + sed -n "s/^$1=//p" "$2" | tail -n 1 +} + +# run_case NAME STATE_DIR [RUNNER_OS] runs the script with the stub printing +# STATE_DIR (empty: the stub fails like a kapi without `state path`). +run_case() { + local name="$1" state="$2" os="${3:-Linux}" + mkdir -p "${work}/${name}/project" + env PATH="${here}/stub:${PATH}" KAPI_STUB_STATE_DIR="${state}" RUNNER_OS="${os}" \ + "${script}" "${work}/${name}/project" "${work}/${name}/fixed" \ + > "${work}/${name}/out" 2> "${work}/${name}/err" || fail "${name}" "exit code $?" +} + +# A kapi that keeps state outside the checkout: the state directory's parse +# cache links to the fixed directory, and a write through it lands there. +state="${work}/state/data/checkouts/abc" +run_case state "${state}" +[ "$(value mode "${work}/state/out")" = state ] || fail state "mode: $(cat "${work}/state/out")" +[ "$(value path "${work}/state/out")" = "${work}/state/fixed" ] || fail state "path: $(cat "${work}/state/out")" +[ -L "${state}/work/cache/docs" ] || fail state "work/cache/docs is not a link" +echo entry > "${state}/work/cache/docs/index.db" +[ -f "${work}/state/fixed/index.db" ] || fail state "a write through the link did not reach the fixed directory" +[ ! -e "${work}/state/project/.kapi" ] || fail state "wrote into the checkout" + +# A kapi without `state path`: the parse cache stays in the checkout. +run_case tree "" +[ "$(value mode "${work}/tree/out")" = tree ] || fail tree "mode: $(cat "${work}/tree/out")" +[ "$(value path "${work}/tree/out")" = "${work}/tree/project/.kapi/work/cache/docs" ] || fail tree "path: $(cat "${work}/tree/out")" +[ ! -e "${work}/tree/fixed" ] || fail tree "created the fixed directory" + +# A link an earlier run left behind is replaced. +stale="${work}/stale/data/checkouts/abc" +mkdir -p "${stale}/work/cache" +ln -s "${work}/elsewhere" "${stale}/work/cache/docs" +run_case stale "${stale}" +[ "$(value mode "${work}/stale/out")" = state ] || fail stale "mode: $(cat "${work}/stale/out")" +[ "$(readlink "${stale}/work/cache/docs")" = "${work}/stale/fixed" ] || fail stale "link: $(readlink "${stale}/work/cache/docs")" + +# A parse cache the runner already holds is left alone and not restored over. +kept="${work}/kept/data/checkouts/abc" +mkdir -p "${kept}/work/cache/docs" +echo entry > "${kept}/work/cache/docs/index.db" +run_case kept "${kept}" +[ "$(value mode "${work}/kept/out")" = none ] || fail kept "mode: $(cat "${work}/kept/out")" +[ -z "$(value path "${work}/kept/out")" ] || fail kept "printed a path" +[ -f "${kept}/work/cache/docs/index.db" ] && [ ! -L "${kept}/work/cache/docs" ] || fail kept "changed the existing cache" + +# A relative answer is not trusted. +run_case relative "data/checkouts/abc" +[ "$(value mode "${work}/relative/out")" = none ] || fail relative "mode: $(cat "${work}/relative/out")" +[ -z "$(value path "${work}/relative/out")" ] || fail relative "printed a path" + +# Windows: no link is made, and no cache is carried. +run_case windows "${work}/windows/data/checkouts/abc" Windows +[ "$(value mode "${work}/windows/out")" = none ] || fail windows "mode: $(cat "${work}/windows/out")" +[ ! -e "${work}/windows/data" ] || fail windows "created the state directory" + +if [ -n "${KAPI:-}" ]; then + bin="${work}/bin" + mkdir -p "${bin}" + ln -s "${KAPI}" "${bin}/kapi" + cp -R "${root}/test/fixture" "${work}/real" + PATH="${bin}:${PATH}" "${script}" "${work}/real" "${work}/real-fixed" | sed "s|${work}||; s/^/real kapi: /" +fi + +if [ "${failures}" -gt 0 ]; then + echo "${failures} assertion(s) failed" + exit 1 +fi +echo "all parse cache locations passed" diff --git a/test/parse-cache/stub/kapi b/test/parse-cache/stub/kapi new file mode 100755 index 0000000..fb7ca30 --- /dev/null +++ b/test/parse-cache/stub/kapi @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +# A stand-in for the kapi CLI that answers `kapi state path`. With +# KAPI_STUB_STATE_DIR set it prints that directory, as a kapi that keeps each +# checkout's state outside the checkout does; unset, it fails the way a kapi +# without the command does. +set -euo pipefail +if [ "${1:-}" = "state" ] && [ "${2:-}" = "path" ] && [ -n "${KAPI_STUB_STATE_DIR:-}" ]; then + printf '%s\n' "${KAPI_STUB_STATE_DIR}" + exit 0 +fi +echo "Error: unknown command \"${1:-}\" for \"kapi\"" >&2 +exit 1 From 88264cf483105965fbc816eed0154fd613a0ac8d Mon Sep 17 00:00:00 2001 From: Asgeir Frimannsson Date: Mon, 23 Nov 2026 18:40:39 +0100 Subject: [PATCH 03/20] Accept kapi state path only as a state directory under kapi's data root The answer must be one line, an absolute path with no control characters, shaped /checkouts/<32 hex>, with no symbolic link among its components. Anything else carries no cache, prints a warning, and leaves the file system untouched. Verified: test/parse-cache/locate.sh (good, empty, two lines, relative, outside the data root, not a checkout, control character, through a link) and against kapi 1.4.0-rc5 and a neokapi#3780 build; shellcheck Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01GtGobCdyR3d9yLXAfX9jsr --- scripts/parse-cache-dir.sh | 70 +++++++++++++++++++++++------ test/parse-cache/locate.sh | 92 +++++++++++++++++++++++++------------- test/parse-cache/stub/kapi | 10 ++--- 3 files changed, 122 insertions(+), 50 deletions(-) diff --git a/scripts/parse-cache-dir.sh b/scripts/parse-cache-dir.sh index dbf9993..e69e17a 100755 --- a/scripts/parse-cache-dir.sh +++ b/scripts/parse-cache-dir.sh @@ -22,32 +22,74 @@ set -euo pipefail project_dir="$1" fixed_dir="$2" -state_dir="" -if out="$(kapi state path -p "${project_dir}" 2> /dev/null)"; then - state_dir="$(printf '%s\n' "${out}" | sed '/^[[:space:]]*$/d' | tail -n 1)" -fi +# skip REASON: carry no parse cache, touch nothing, and say why. +skip() { + echo "::warning title=setup-kapi::The parse cache is not carried: $1" >&2 + echo "mode=none" + exit 0 +} + +# data_root prints the data root kapi uses, by the rule kapi applies: +# KAPI_DATA_DIR, then XDG_DATA_HOME/kapi, then the platform default. +data_root() { + if [ -n "${KAPI_DATA_DIR:-}" ]; then + printf '%s\n' "${KAPI_DATA_DIR%/}" + elif [ -n "${XDG_DATA_HOME:-}" ]; then + printf '%s\n' "${XDG_DATA_HOME%/}/kapi" + elif [ "${RUNNER_OS:-}" = "macOS" ] || [ "$(uname -s)" = "Darwin" ]; then + printf '%s\n' "${HOME%/}/Library/Application Support/kapi" + else + printf '%s\n' "${HOME%/}/.local/share/kapi" + fi +} -if [ -z "${state_dir}" ]; then +if ! out="$(kapi state path -p "${project_dir}" 2> /dev/null)"; then + # A kapi without the command keeps the parse cache in the checkout. echo "mode=tree" echo "path=${project_dir}/.kapi/work/cache/docs" exit 0 fi -case "${state_dir}" in - /* | [A-Za-z]:[\\/]*) ;; - *) - echo "::warning title=setup-kapi::kapi state path printed '${state_dir}', which is not an absolute path; the parse cache is not carried." >&2 - echo "mode=none" - exit 0 - ;; -esac - if [ "${RUNNER_OS:-}" = "Windows" ]; then echo "setup-kapi: the parse cache is not carried on Windows with a kapi that keeps its state outside the checkout." >&2 echo "mode=none" exit 0 fi +# Accept the answer only when it names this checkout's state directory under +# kapi's data root: one line, an absolute path without control characters, +# shaped /checkouts/<32 hex digits>, with no link on the way. +if [ -z "${out}" ]; then + skip "kapi state path printed nothing." +fi +if [ "$(printf '%s\n' "${out}" | wc -l | tr -d ' ')" != 1 ]; then + skip "kapi state path printed more than one line." +fi +state_dir="${out}" +if printf '%s' "${state_dir}" | LC_ALL=C grep -q '[[:cntrl:]]'; then + skip "kapi state path printed a control character." +fi +case "${state_dir}" in + /*) ;; + *) skip "kapi state path printed a path that is not absolute." ;; +esac +root="$(data_root)" +case "${state_dir}" in + "${root}/checkouts/"*) ;; + *) skip "kapi state path printed a directory outside kapi's data root (${root})." ;; +esac +name="${state_dir#"${root}/checkouts/"}" +if ! printf '%s' "${name}" | LC_ALL=C grep -Eqx '[0-9a-f]{32}'; then + skip "kapi state path printed a directory that is not a checkout's state directory." +fi +check="${state_dir}/work/cache/docs" +while [ -n "${check}" ] && [ "${check}" != "/" ]; do + if [ -L "${check}" ] && [ "${check}" != "${state_dir}/work/cache/docs" ]; then + skip "${check} is a symbolic link." + fi + check="$(dirname "${check}")" +done + docs="${state_dir}/work/cache/docs" if [ -L "${docs}" ]; then # A link an earlier setup-kapi run left on a runner that keeps its data. diff --git a/test/parse-cache/locate.sh b/test/parse-cache/locate.sh index b27473c..0c14454 100755 --- a/test/parse-cache/locate.sh +++ b/test/parse-cache/locate.sh @@ -1,14 +1,15 @@ #!/usr/bin/env bash -# Runs scripts/parse-cache-dir.sh against the kapi stub in stub/ for each case -# it handles, and asserts the mode and path it prints and what it leaves on -# disk. With KAPI set to a kapi binary, it also runs the script against that -# binary on test/fixture and prints the mode it chose. +# Runs scripts/parse-cache-dir.sh against the kapi stub in stub/ for each +# answer `kapi state path` can give, and asserts the mode and path it prints +# and what it leaves on disk. An answer the script does not accept must leave +# the file system as it was. With KAPI set to a kapi binary, it also runs the +# script against that binary on test/fixture and prints the mode it chose. set -euo pipefail -here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -root="$(cd "${here}/../.." && pwd)" +here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" +root="$(cd "${here}/../.." && pwd -P)" script="${root}/scripts/parse-cache-dir.sh" -work="$(mktemp -d)" +work="$(cd "$(mktemp -d)" && pwd -P)" trap 'rm -rf "${work}"' EXIT failures=0 @@ -19,36 +20,70 @@ fail() { value() { sed -n "s/^$1=//p" "$2" | tail -n 1 } +# snapshot DIR lists everything beneath DIR, links included. +snapshot() { + (cd "$1" && find . -not -name out -not -name err | LC_ALL=C sort) +} + +key=0123456789abcdef0123456789abcdef -# run_case NAME STATE_DIR [RUNNER_OS] runs the script with the stub printing -# STATE_DIR (empty: the stub fails like a kapi without `state path`). +# run_case NAME OUTPUT [SET] [RUNNER_OS]: the stub prints OUTPUT (SET=0 makes +# it fail like a kapi without `state path`). The data root is NAME/data. run_case() { - local name="$1" state="$2" os="${3:-Linux}" - mkdir -p "${work}/${name}/project" - env PATH="${here}/stub:${PATH}" KAPI_STUB_STATE_DIR="${state}" RUNNER_OS="${os}" \ + local name="$1" output="$2" set="${3:-1}" os="${4:-Linux}" + mkdir -p "${work}/${name}/project" "${work}/${name}/data" + env PATH="${here}/stub:${PATH}" KAPI_STUB_STATE_SET="${set}" KAPI_STUB_STATE_OUTPUT="${output}" \ + KAPI_DATA_DIR="${work}/${name}/data" RUNNER_OS="${os}" \ "${script}" "${work}/${name}/project" "${work}/${name}/fixed" \ > "${work}/${name}/out" 2> "${work}/${name}/err" || fail "${name}" "exit code $?" } -# A kapi that keeps state outside the checkout: the state directory's parse -# cache links to the fixed directory, and a write through it lands there. -state="${work}/state/data/checkouts/abc" -run_case state "${state}" -[ "$(value mode "${work}/state/out")" = state ] || fail state "mode: $(cat "${work}/state/out")" -[ "$(value path "${work}/state/out")" = "${work}/state/fixed" ] || fail state "path: $(cat "${work}/state/out")" -[ -L "${state}/work/cache/docs" ] || fail state "work/cache/docs is not a link" +# expect_skip NAME OUTPUT [PREPARE]: the script carries no cache, prints a +# warning, and changes nothing on disk. +expect_skip() { + local name="$1" output="$2" + mkdir -p "${work}/${name}/data" "${work}/${name}/project" + local before + before="$(snapshot "${work}/${name}")" + run_case "${name}" "${output}" + [ "$(value mode "${work}/${name}/out")" = none ] || fail "${name}" "mode: $(cat "${work}/${name}/out")" + [ -z "$(value path "${work}/${name}/out")" ] || fail "${name}" "printed a path" + grep -q '::warning' "${work}/${name}/err" || fail "${name}" "printed no warning" + [ "$(snapshot "${work}/${name}")" = "${before}" ] || fail "${name}" "changed the file system" + [ ! -e "${work}/${name}/fixed" ] || fail "${name}" "created the fixed directory" +} + +# A good path: the state directory's parse cache links to the fixed +# directory, and a write through it lands there. +state="${work}/good/data/checkouts/${key}" +run_case good "${state}" +[ "$(value mode "${work}/good/out")" = state ] || fail good "mode: $(cat "${work}/good/out")" +[ "$(value path "${work}/good/out")" = "${work}/good/fixed" ] || fail good "path: $(cat "${work}/good/out")" +[ -L "${state}/work/cache/docs" ] || fail good "work/cache/docs is not a link" echo entry > "${state}/work/cache/docs/index.db" -[ -f "${work}/state/fixed/index.db" ] || fail state "a write through the link did not reach the fixed directory" -[ ! -e "${work}/state/project/.kapi" ] || fail state "wrote into the checkout" +[ -f "${work}/good/fixed/index.db" ] || fail good "a write through the link did not reach the fixed directory" +[ ! -e "${work}/good/project/.kapi" ] || fail good "wrote into the checkout" # A kapi without `state path`: the parse cache stays in the checkout. -run_case tree "" +run_case tree "" 0 [ "$(value mode "${work}/tree/out")" = tree ] || fail tree "mode: $(cat "${work}/tree/out")" [ "$(value path "${work}/tree/out")" = "${work}/tree/project/.kapi/work/cache/docs" ] || fail tree "path: $(cat "${work}/tree/out")" [ ! -e "${work}/tree/fixed" ] || fail tree "created the fixed directory" +# Answers that are refused without touching anything. +expect_skip empty "" +expect_skip two-lines "${work}/two-lines/data/checkouts/${key} +${work}/two-lines/data/checkouts/${key}" +expect_skip relative "data/checkouts/${key}" +expect_skip outside "${work}/outside/elsewhere/checkouts/${key}" +expect_skip not-a-checkout "${work}/not-a-checkout/data/checkouts/../../.." +expect_skip control "${work}/control/data/checkouts/${key}$(printf '\t')" +mkdir -p "${work}/link/real/checkouts/${key}" "${work}/link/data" +ln -s "${work}/link/real/checkouts" "${work}/link/data/checkouts" +expect_skip link "${work}/link/data/checkouts/${key}" + # A link an earlier run left behind is replaced. -stale="${work}/stale/data/checkouts/abc" +stale="${work}/stale/data/checkouts/${key}" mkdir -p "${stale}/work/cache" ln -s "${work}/elsewhere" "${stale}/work/cache/docs" run_case stale "${stale}" @@ -56,7 +91,7 @@ run_case stale "${stale}" [ "$(readlink "${stale}/work/cache/docs")" = "${work}/stale/fixed" ] || fail stale "link: $(readlink "${stale}/work/cache/docs")" # A parse cache the runner already holds is left alone and not restored over. -kept="${work}/kept/data/checkouts/abc" +kept="${work}/kept/data/checkouts/${key}" mkdir -p "${kept}/work/cache/docs" echo entry > "${kept}/work/cache/docs/index.db" run_case kept "${kept}" @@ -64,15 +99,10 @@ run_case kept "${kept}" [ -z "$(value path "${work}/kept/out")" ] || fail kept "printed a path" [ -f "${kept}/work/cache/docs/index.db" ] && [ ! -L "${kept}/work/cache/docs" ] || fail kept "changed the existing cache" -# A relative answer is not trusted. -run_case relative "data/checkouts/abc" -[ "$(value mode "${work}/relative/out")" = none ] || fail relative "mode: $(cat "${work}/relative/out")" -[ -z "$(value path "${work}/relative/out")" ] || fail relative "printed a path" - # Windows: no link is made, and no cache is carried. -run_case windows "${work}/windows/data/checkouts/abc" Windows +run_case windows "${work}/windows/data/checkouts/${key}" 1 Windows [ "$(value mode "${work}/windows/out")" = none ] || fail windows "mode: $(cat "${work}/windows/out")" -[ ! -e "${work}/windows/data" ] || fail windows "created the state directory" +[ ! -e "${work}/windows/data/checkouts" ] || fail windows "created the state directory" if [ -n "${KAPI:-}" ]; then bin="${work}/bin" diff --git a/test/parse-cache/stub/kapi b/test/parse-cache/stub/kapi index fb7ca30..a8a186d 100755 --- a/test/parse-cache/stub/kapi +++ b/test/parse-cache/stub/kapi @@ -1,11 +1,11 @@ #!/usr/bin/env bash # A stand-in for the kapi CLI that answers `kapi state path`. With -# KAPI_STUB_STATE_DIR set it prints that directory, as a kapi that keeps each -# checkout's state outside the checkout does; unset, it fails the way a kapi -# without the command does. +# KAPI_STUB_STATE_SET=1 it prints KAPI_STUB_STATE_OUTPUT exactly (which may be +# empty or span lines), as a kapi that keeps each checkout's state outside the +# checkout does; otherwise it fails the way a kapi without the command does. set -euo pipefail -if [ "${1:-}" = "state" ] && [ "${2:-}" = "path" ] && [ -n "${KAPI_STUB_STATE_DIR:-}" ]; then - printf '%s\n' "${KAPI_STUB_STATE_DIR}" +if [ "${1:-}" = "state" ] && [ "${2:-}" = "path" ] && [ "${KAPI_STUB_STATE_SET:-}" = 1 ]; then + printf '%s' "${KAPI_STUB_STATE_OUTPUT:-}" exit 0 fi echo "Error: unknown command \"${1:-}\" for \"kapi\"" >&2 From b2d0d8508ea0e68e23ee8eb6574da0e3b07eba53 Mon Sep 17 00:00:00 2001 From: Asgeir Frimannsson Date: Mon, 23 Nov 2026 18:43:47 +0100 Subject: [PATCH 04/20] Carry the parse cache through KAPI_PARSE_CACHE_DIR; adjust tests to registry#6 The action sets KAPI_PARSE_CACHE_DIR to a fixed directory under runner.temp (or keeps a caller's value under runner.temp or the workspace) and caches that directory and the in-tree .kapi/work/cache/docs. It no longer runs kapi state path, makes no link and takes no path from a command's output. Tests: the Windows jobs install no plugin and assert that bowrain has no Windows build (registry#6); the plugin seed is bowrain 1.4.0-rc5 from beta, since 1.0.2 is no longer listed. Verified: test/parse-cache/env.sh; kapi 1.2.0 installs bowrain@1.4.0-rc5 from beta; actionlint; shellcheck Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01GtGobCdyR3d9yLXAfX9jsr --- .github/workflows/test.yml | 88 +++++++++++++++-------- README.md | 11 ++- action.yml | 93 +++++++++++++++--------- scripts/parse-cache-dir.sh | 108 ---------------------------- test/parse-cache/env.sh | 94 ++++++++++++++++++++++++ test/parse-cache/locate.sh | 119 ------------------------------- test/parse-cache/stub/kapi | 12 ---- test/plugins/windows-unlisted.sh | 21 ++++++ 8 files changed, 234 insertions(+), 312 deletions(-) delete mode 100755 scripts/parse-cache-dir.sh create mode 100755 test/parse-cache/env.sh delete mode 100755 test/parse-cache/locate.sh delete mode 100755 test/parse-cache/stub/kapi create mode 100755 test/plugins/windows-unlisted.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0b67286..7dacc0b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,10 +17,12 @@ env: # The state cache jobs need kapi 1.2, the first release that keeps its state # under .kapi/work/. STATE_CACHE_KAPI_VERSION: 1.2.0-rc32 - # The plugin cache jobs seed a cache with this kapi and an older bowrain, - # then set up the newest kapi over it. + # The plugin cache jobs seed a cache with this kapi and a bowrain the newest + # stable kapi does not pick on a fresh install, then set up the newest kapi + # over it. The registry lists bowrain 1.3.0 (stable) and 1.4.0-rc5 (beta) + # only (registry#6), so the seed is the beta. PLUGIN_SEED_KAPI_VERSION: 1.2.0 - PLUGIN_SEED_BOWRAIN_VERSION: 1.0.2 + PLUGIN_SEED_BOWRAIN_VERSION: 1.4.0-rc5 jobs: test-latest: @@ -36,8 +38,17 @@ jobs: - name: Setup kapi (latest) id: setup uses: ./ + # No bowrain build is listed for Windows until the next kapi release + # (registry#6, a founder decision), so the Windows run installs no + # plugin and the step below asserts the install still fails there. with: token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} + plugins: ${{ matrix.os != 'windows-latest' && 'bowrain' || '' }} + + - name: Assert no bowrain build is listed for Windows + if: runner.os == 'Windows' + shell: bash + run: test/plugins/windows-unlisted.sh - name: Verify version output shell: bash @@ -80,9 +91,18 @@ jobs: - name: Setup kapi (pinned) id: setup uses: ./ + # No bowrain build is listed for Windows until the next kapi release + # (registry#6, a founder decision), so the Windows run installs no + # plugin and the step below asserts the install still fails there. with: token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} version: ${{ steps.version.outputs.version }} + plugins: ${{ matrix.os != 'windows-latest' && 'bowrain' || '' }} + + - name: Assert no bowrain build is listed for Windows + if: runner.os == 'Windows' + shell: bash + run: test/plugins/windows-unlisted.sh - name: Verify correct version shell: bash @@ -313,27 +333,23 @@ jobs: test ! -e "${docs}" fi - # Where the parse cache is carried. scripts/parse-cache-dir.sh asks kapi for - # the checkout's state directory with `kapi state path`; the harness answers - # with a kapi stub, once as a kapi that keeps state outside the checkout and - # once as one without the command, and checks the link it makes. - test-parse-cache-location: - name: "Parse cache location (kapi stub, ${{ matrix.os }})" - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - os: [ubuntu-latest, macos-latest] + # The step that names the parse cache directory, run out of action.yml for + # each value KAPI_PARSE_CACHE_DIR can have, and the two paths both parse + # cache steps carry. + test-parse-cache-dir: + name: "Parse cache directory (KAPI_PARSE_CACHE_DIR)" + runs-on: ubuntu-latest steps: - uses: actions/checkout@v6 - - name: Run the location harness + - name: Run the parse cache directory harness shell: bash - run: test/parse-cache/locate.sh + run: | + yq --version + test/parse-cache/env.sh - # Released kapi versions without `kapi state path` keep the parse cache in - # the checkout. The action must find that path, make no link, and leave the - # cache where kapi writes it. + # Released kapi versions ignore KAPI_PARSE_CACHE_DIR and keep the parse + # cache in the checkout, which the action also carries. test-parse-cache-in-tree: name: "Parse cache in the checkout (kapi ${{ matrix.version }})" runs-on: ubuntu-latest @@ -352,14 +368,13 @@ jobs: plugins: "" project-dir: test/fixture - - name: Check the action chose the checkout's path + - name: Check the exported directory and the in-tree cache shell: bash run: | - test ! -e "${RUNNER_TEMP}/setup-kapi-parse-cache" - KAPI="$(command -v kapi)" test/parse-cache/locate.sh | tee "${RUNNER_TEMP}/locate.txt" - grep -qx 'real kapi: mode=tree' "${RUNNER_TEMP}/locate.txt" + test "${KAPI_PARSE_CACHE_DIR}" = "${RUNNER_TEMP}/kapi-parse-cache" kapi status -p test/fixture/kapi.yaml > /dev/null test -s test/fixture/.kapi/work/cache/docs/index.db + test ! -e "${KAPI_PARSE_CACHE_DIR}" test-cache: name: "Cache hit (${{ matrix.os }})" @@ -371,17 +386,27 @@ jobs: steps: - uses: actions/checkout@v6 - # First install — populates cache + # First install: populates the cache. - name: Setup kapi (first) uses: ./ + # No bowrain build is listed for Windows until the next kapi release + # (registry#6, a founder decision), so the Windows run installs no + # plugin and the step below asserts the install still fails there. with: token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} + plugins: ${{ matrix.os != 'windows-latest' && 'bowrain' || '' }} - # Second install — should hit cache + # Second install: should hit the cache. - name: Setup kapi (cached) uses: ./ with: token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} + plugins: ${{ matrix.os != 'windows-latest' && 'bowrain' || '' }} + + - name: Assert no bowrain build is listed for Windows + if: runner.os == 'Windows' + shell: bash + run: test/plugins/windows-unlisted.sh - name: Verify kapi works from cache shell: bash @@ -389,16 +414,17 @@ jobs: # A cache hit installs no plugin, so check the cached one is current. - name: Verify the cached plugin is current + if: runner.os != 'Windows' shell: bash run: test/plugins/matches-fresh.sh bowrain # The plugin cache across a kapi upgrade. The seed job sets up an older kapi - # and leaves an older bowrain in the plugin root, as a cache saved before a - # newer bowrain was published would hold; the action saves that at job end. + # and leaves a bowrain in the plugin root that a fresh install by the newest + # kapi would not pick, as a cache saved for another kapi or registry entry + # would hold; the action saves that at job end. # The upgrade job then sets up the newest kapi with the same plugins input, # and the bowrain it is left with must be the one this kapi installs fresh. - # Windows is absent because the registry has only one Windows bowrain build, - # so no older one can be cached there. + # Windows is absent because the registry lists no Windows bowrain build. test-plugin-seed: name: "Plugin cache seed (${{ matrix.os }})" runs-on: ${{ matrix.os }} @@ -417,10 +443,10 @@ jobs: plugins: bowrain parse-cache: "false" - - name: Leave an older bowrain in the plugin root + - name: Leave a bowrain a fresh install would not pick shell: bash run: | - kapi plugin install "bowrain@${PLUGIN_SEED_BOWRAIN_VERSION}" + kapi plugin install --channel beta "bowrain@${PLUGIN_SEED_BOWRAIN_VERSION}" grep -q "\"${PLUGIN_SEED_BOWRAIN_VERSION}\"" ~/.local/share/kapi/plugins/bowrain/installed.json test-plugin-upgrade: diff --git a/README.md b/README.md index 0673d82..1ba2d02 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ The newest stable release, which `latest` installs, has no `kapi up`, so this ex |-------|-------------|---------|----------| | `version` | Kapi CLI version (e.g. `1.1.0`), or `latest` for the newest stable CLI release | `latest` | No | | `token` | GitHub token for release downloads and API rate limits | `${{ github.token }}` | No | -| `plugins` | Newline- or comma-separated plugin refs to install, as the registry names them (`bowrain`, `okapi-bridge`; a `kapi-` prefix is stripped). Pass `''` to install nothing | `bowrain` | No | +| `plugins` | Newline- or comma-separated plugin refs to install, as the registry names them (`bowrain`, `okapi-bridge`; a `kapi-` prefix is stripped). Pass `''` to install nothing. No Bowrain plugin build is listed for Windows before the next kapi release, so a Windows job sets `plugins: ""` until then | `bowrain` | No | | `auth-token` | Bowrain server JWT, exported as `BOWRAIN_AUTH_TOKEN` | — | No | | `server` | Bowrain server URL, exported as `BOWRAIN_SERVER_URL` | — | No | | `parse-cache` | Keep kapi's parse cache between runs, so a later run replays files it already parsed; see [Project parse cache](#project-parse-cache). Runs only when a `kapi.yaml` recipe (or legacy `*.kapi`) is present. Set `false` to turn it off | `true` | No | @@ -113,18 +113,15 @@ The binary is cached keyed on version + OS + arch. Plugins are cached keyed on t ### Project parse cache -kapi records how it parsed each source and target file, so a later run can replay a file instead of parsing it again. With `parse-cache` on and a recipe in `project-dir`, the action restores that parse cache before your steps run, and `actions/cache` saves it again when the job ends. Run `actions/checkout` before this action, so the checkout it finds is the one your kapi steps use. +kapi records how it parsed each source and target file, so a later run can replay a file instead of parsing it again. With `parse-cache` on and a recipe in `project-dir`, the action restores that parse cache before your steps run, and `actions/cache` saves it again when the job ends. -Where the parse cache lives depends on the kapi version: - -- A kapi that keeps each checkout's state outside the checkout prints that directory with `kapi state path -p `. The directory is named for the checkout folder, which is new on every hosted runner, so the action keeps the cache in a fixed directory under `runner.temp` and links the state directory's `work/cache/docs` to it. kapi reads and writes the parse cache through the link. On Windows runners the action carries no parse cache for this kapi, and on a runner that already holds a parse cache in the state directory it leaves that cache alone. -- A kapi without that command (1.3 and earlier) keeps the parse cache in the checkout, and the action caches `/.kapi/work/cache/docs`. +The action sets `KAPI_PARSE_CACHE_DIR` to a directory under `runner.temp` for the rest of the job, and caches that directory together with `/.kapi/work/cache/docs`. A kapi that reads the variable keeps its parse cache there; kapi 1.3 and earlier keep it in the checkout. If your workflow sets `KAPI_PARSE_CACHE_DIR` itself, the action keeps your value and caches it when it lies under `runner.temp` or the workspace. On another CI system, set `KAPI_PARSE_CACHE_DIR` to a fixed directory and cache that directory. A restored parse cache does not change any result. kapi keys each entry by the file's path and content hash, the parse configuration, the recipe and the kapi build, and parses the file again when any of them differs. The test workflow checks this on every change: with the cache restored, `kapi status`, `kapi check`, `kapi check --ship` and `kapi up` must match a cold run exactly, in exit codes, output and every file written, and they must still match after the source changes. The action carries the parse cache and nothing else. The project store holds this checkout's blocks and the targets a run wrote, and a restored copy changes what `kapi status`, `kapi check --ship` and `kapi up` report. The extraction batches, the redaction vault and the sync state (with its claim token) belong to the checkout that wrote them. The project's context (its terms, voice profiles, content memory and recorded decisions) lives in kapi's workspace and moves between machines through a context backend (kapi 1.3 and later). A job that needs it runs `kapi context pull` before kapi works and `kapi context push` after a run records something. [`kapi-action`](https://github.com/neokapi/kapi-action)'s `context-sync` input runs both around its command, or your workflow can run them as steps of its own. -The cache key holds the runner OS, the resolved kapi version and the ref, so a new kapi version starts from an empty cache. How much time the cache saves depends on the formats: container formats such as DOCX replay faster than they parse, while JSON and Markdown parse about as fast as they replay. +Each run saves its own cache, keyed by the runner OS, the resolved kapi version, the ref, the job and the run. A run restores the newest cache for the same kapi version, from its own ref first. The key does not change with your source files: a cache from before a source edit still holds the entries for every file the edit left alone. A new kapi version starts from an empty cache. How much time the cache saves depends on the formats: container formats such as DOCX replay faster than they parse, while JSON and Markdown parse about as fast as they replay. kapi trusts what it finds in its parse cache, so who can write the cache decides whether a restored entry can be trusted. GitHub scopes caches by branch: a run restores only caches saved on its own branch, on the default branch, or, for a pull request, on its base branch. A cache saved by a pull request, including one from a fork, is never restored on the default branch. A `pull_request_target` run works in the base branch's scope, so there the action restores the parse cache and does not save it. Any other workflow that runs untrusted code with this action can write to the cache of the branch it runs on; set `parse-cache: false` in such a workflow. diff --git a/action.yml b/action.yml index 603ac14..9fb42b5 100644 --- a/action.yml +++ b/action.yml @@ -235,45 +235,64 @@ runs: echo "found=false" >> "$GITHUB_OUTPUT" fi - # 13. Find the directory that holds the parse cache for this kapi, and - # prepare it. A kapi that keeps each checkout's state outside the - # checkout prints that directory with `kapi state path`; its name - # changes from run to run, so the cache is carried through a fixed - # directory under runner.temp that the state directory links to. An - # older kapi keeps the parse cache in the checkout. See - # scripts/parse-cache-dir.sh. - - name: Locate kapi parse cache + # 13. Name the directory kapi keeps its parse cache in. A kapi that reads + # KAPI_PARSE_CACHE_DIR keeps the parse cache there; a kapi up to 1.3 + # ignores the variable and keeps it in the checkout, at + # .kapi/work/cache/docs. The cache step carries both paths, and the one + # the installed kapi does not use is absent. Both are the same strings + # on every run, which keeps the cache version stable. + # + # A KAPI_PARSE_CACHE_DIR the workflow set itself is kept, and cached + # when it lies under runner.temp or the workspace; otherwise the + # parse cache is not carried. + - name: Set kapi parse cache directory id: parse-cache-dir if: steps.parse-cache.outputs.enabled != 'false' && steps.detect-project.outputs.found == 'true' shell: bash env: - ACTION_PATH: ${{ github.action_path }} - PROJECT_DIR: ${{ inputs.project-dir }} - FIXED_DIR: ${{ runner.temp }}/setup-kapi-parse-cache + FIXED_DIR: ${{ runner.temp }}/kapi-parse-cache run: | - set -o pipefail - "${ACTION_PATH}/scripts/parse-cache-dir.sh" "${PROJECT_DIR}" "${FIXED_DIR}" | tee -a "$GITHUB_OUTPUT" + if [ -z "${KAPI_PARSE_CACHE_DIR:-}" ]; then + echo "KAPI_PARSE_CACHE_DIR=${FIXED_DIR}" >> "$GITHUB_ENV" + echo "dir=${FIXED_DIR}" >> "$GITHUB_OUTPUT" + exit 0 + fi + dir="${KAPI_PARSE_CACHE_DIR}" + case "/${dir//\\//}/" in + */../*) + echo "::warning title=setup-kapi::KAPI_PARSE_CACHE_DIR (${dir}) has a '..' component, so the parse cache is not carried." + exit 0 + ;; + esac + for base in "${RUNNER_TEMP%[/\\]}" "${GITHUB_WORKSPACE%[/\\]}"; do + [ -n "${base}" ] || continue + case "${dir}" in + "${base}"/* | "${base}"\\*) + echo "dir=${dir}" >> "$GITHUB_OUTPUT" + exit 0 + ;; + esac + done + echo "::warning title=setup-kapi::KAPI_PARSE_CACHE_DIR (${dir}) is not under runner.temp or the workspace, so the parse cache is not carried." # 14. Restore kapi's parse cache, and save it at job end. The key is unique - # to the job and run attempt, so actions/cache saves through its own - # post step; the restore takes the newest cache for the same kapi - # version, preferring the same ref. + # to the job and run attempt, so every run saves its own cache through + # actions/cache's post step, and the restore takes the newest cache for + # the same kapi version, preferring the same ref. The key hashes no + # source file: kapi keys each entry by the file's path and content + # hash, the parse configuration, the recipe and the kapi build, and + # parses again on any mismatch, so an older cache is still useful after + # a source edit, and a restored entry is either valid for the checkout + # or ignored. # - # Only the parse cache (work/cache/docs) is carried. kapi keys each - # entry by the file's absolute path and content hash, the parse - # configuration, the recipe and the kapi build, and parses again on any - # mismatch, so a restored entry is either valid for the checkout or - # ignored. The rest of the work directory stays out of the cache: - # - store.db holds the checkout's block cache and the targets a run - # wrote; restored, it changes what kapi status, kapi check --ship - # and kapi up report. - # - cache/extractions holds kapi extract batches that kapi merge reads. - # - cache/redaction and vault/ hold withheld original values. - # - the sync state holds a claim token. - # The project's context (terms, voice profiles, content memory and - # recorded decisions) lives in kapi's workspace outside the checkout. A - # job fetches it with `kapi context pull`, which this action leaves to - # kapi-action's context-sync input or the workflow's own step. + # Only the parse cache is carried. The project store, the extraction + # batches, the redaction vault and the sync state (with its claim + # token) stay out: a restored store changes what kapi status, kapi + # check --ship and kapi up report, and the rest belongs to the + # checkout that wrote it. The project's context lives in kapi's + # workspace; a job fetches it with `kapi context pull`, which this + # action leaves to kapi-action's context-sync input or the workflow's + # own step. # # The kapi version is part of the key because the parse cache keeps the # entries every build wrote; a new version starts from an empty cache. @@ -285,20 +304,24 @@ runs: # pull_request_target run executes in the base branch's scope, so there # the cache is restored and never saved. - name: Restore kapi parse cache - if: steps.parse-cache-dir.outputs.path != '' && github.event_name != 'pull_request_target' + if: steps.parse-cache-dir.outputs.dir != '' && github.event_name != 'pull_request_target' uses: actions/cache@v5 with: - path: ${{ steps.parse-cache-dir.outputs.path }} + path: | + ${{ steps.parse-cache-dir.outputs.dir }} + ${{ inputs.project-dir }}/.kapi/work/cache/docs key: kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}-${{ github.ref_name }}-${{ github.job }}-${{ github.run_id }}-${{ github.run_attempt }} restore-keys: | kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}-${{ github.ref_name }}- kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}- - name: Restore kapi parse cache (no save) - if: steps.parse-cache-dir.outputs.path != '' && github.event_name == 'pull_request_target' + if: steps.parse-cache-dir.outputs.dir != '' && github.event_name == 'pull_request_target' uses: actions/cache/restore@v5 with: - path: ${{ steps.parse-cache-dir.outputs.path }} + path: | + ${{ steps.parse-cache-dir.outputs.dir }} + ${{ inputs.project-dir }}/.kapi/work/cache/docs key: kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}-${{ github.ref_name }}-${{ github.job }}-${{ github.run_id }}-${{ github.run_attempt }} restore-keys: | kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}-${{ github.ref_name }}- diff --git a/scripts/parse-cache-dir.sh b/scripts/parse-cache-dir.sh deleted file mode 100755 index e69e17a..0000000 --- a/scripts/parse-cache-dir.sh +++ /dev/null @@ -1,108 +0,0 @@ -#!/usr/bin/env bash -# Decide which directory setup-kapi carries as kapi's parse cache, and prepare -# it before the cache is restored. -# -# Usage: parse-cache-dir.sh -# Prints: mode= and path=, one per line, -# for $GITHUB_OUTPUT. Notes for the job log go to stderr. -# -# A kapi that keeps each checkout's state outside the checkout answers -# `kapi state path -p ` with that checkout's state directory. Its -# name comes from the checkout folder's identity, so a fresh runner gets a -# different one on every run, and actions/cache saves (in its post step) the -# path it was given at restore time. The cache therefore lives at a fixed -# directory under runner.temp, and the state directory's work/cache/docs is a -# link to it: kapi reads and writes the parse cache through the link, and the -# post step saves the fixed directory. -# -# A kapi without that command (1.3 and earlier) keeps the parse cache in the -# checkout, at /.kapi/work/cache/docs, and that path is cached. -set -euo pipefail - -project_dir="$1" -fixed_dir="$2" - -# skip REASON: carry no parse cache, touch nothing, and say why. -skip() { - echo "::warning title=setup-kapi::The parse cache is not carried: $1" >&2 - echo "mode=none" - exit 0 -} - -# data_root prints the data root kapi uses, by the rule kapi applies: -# KAPI_DATA_DIR, then XDG_DATA_HOME/kapi, then the platform default. -data_root() { - if [ -n "${KAPI_DATA_DIR:-}" ]; then - printf '%s\n' "${KAPI_DATA_DIR%/}" - elif [ -n "${XDG_DATA_HOME:-}" ]; then - printf '%s\n' "${XDG_DATA_HOME%/}/kapi" - elif [ "${RUNNER_OS:-}" = "macOS" ] || [ "$(uname -s)" = "Darwin" ]; then - printf '%s\n' "${HOME%/}/Library/Application Support/kapi" - else - printf '%s\n' "${HOME%/}/.local/share/kapi" - fi -} - -if ! out="$(kapi state path -p "${project_dir}" 2> /dev/null)"; then - # A kapi without the command keeps the parse cache in the checkout. - echo "mode=tree" - echo "path=${project_dir}/.kapi/work/cache/docs" - exit 0 -fi - -if [ "${RUNNER_OS:-}" = "Windows" ]; then - echo "setup-kapi: the parse cache is not carried on Windows with a kapi that keeps its state outside the checkout." >&2 - echo "mode=none" - exit 0 -fi - -# Accept the answer only when it names this checkout's state directory under -# kapi's data root: one line, an absolute path without control characters, -# shaped /checkouts/<32 hex digits>, with no link on the way. -if [ -z "${out}" ]; then - skip "kapi state path printed nothing." -fi -if [ "$(printf '%s\n' "${out}" | wc -l | tr -d ' ')" != 1 ]; then - skip "kapi state path printed more than one line." -fi -state_dir="${out}" -if printf '%s' "${state_dir}" | LC_ALL=C grep -q '[[:cntrl:]]'; then - skip "kapi state path printed a control character." -fi -case "${state_dir}" in - /*) ;; - *) skip "kapi state path printed a path that is not absolute." ;; -esac -root="$(data_root)" -case "${state_dir}" in - "${root}/checkouts/"*) ;; - *) skip "kapi state path printed a directory outside kapi's data root (${root})." ;; -esac -name="${state_dir#"${root}/checkouts/"}" -if ! printf '%s' "${name}" | LC_ALL=C grep -Eqx '[0-9a-f]{32}'; then - skip "kapi state path printed a directory that is not a checkout's state directory." -fi -check="${state_dir}/work/cache/docs" -while [ -n "${check}" ] && [ "${check}" != "/" ]; do - if [ -L "${check}" ] && [ "${check}" != "${state_dir}/work/cache/docs" ]; then - skip "${check} is a symbolic link." - fi - check="$(dirname "${check}")" -done - -docs="${state_dir}/work/cache/docs" -if [ -L "${docs}" ]; then - # A link an earlier setup-kapi run left on a runner that keeps its data. - rm "${docs}" -elif [ -e "${docs}" ]; then - # A runner that keeps kapi's data between jobs already holds the cache. - echo "setup-kapi: ${docs} already exists on this runner, so the parse cache is not restored over it." >&2 - echo "mode=none" - exit 0 -fi - -mkdir -p "${fixed_dir}" "${state_dir}/work/cache" -ln -s "${fixed_dir}" "${docs}" -echo "setup-kapi: kapi keeps this checkout's state in ${state_dir}; its parse cache is carried through ${fixed_dir}." >&2 -echo "mode=state" -echo "path=${fixed_dir}" diff --git a/test/parse-cache/env.sh b/test/parse-cache/env.sh new file mode 100755 index 0000000..e1769a9 --- /dev/null +++ b/test/parse-cache/env.sh @@ -0,0 +1,94 @@ +#!/usr/bin/env bash +# Runs the action's "Set kapi parse cache directory" step, read out of +# action.yml, for each value KAPI_PARSE_CACHE_DIR can have when the step +# starts, and asserts what it exports and outputs. Then it asserts that both +# parse cache steps carry the directory the step names and the in-tree path a +# kapi up to 1.3 uses. Needs yq (v4). +set -euo pipefail + +here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +root="$(cd "${here}/../.." && pwd)" +work="$(mktemp -d)" +trap 'rm -rf "${work}"' EXIT + +yq '.runs.steps[] | select(.id == "parse-cache-dir") | .run' "${root}/action.yml" > "${work}/step.sh" +if ! grep -q KAPI_PARSE_CACHE_DIR "${work}/step.sh"; then + echo "could not read the parse-cache-dir step out of action.yml" >&2 + exit 1 +fi + +failures=0 +fail() { + echo "FAIL $1: $2" + failures=$((failures + 1)) +} + +temp="${work}/runner-temp" +workspace="${work}/workspace" + +# run_case NAME PRESET WANT_DIR WANT_ENV WANT_WARNING: PRESET is the value of +# KAPI_PARSE_CACHE_DIR before the step ("" for unset). WANT_ENV is the line the +# step must append to GITHUB_ENV ("" for none). WANT_WARNING is "yes" or "no". +run_case() { + local name="$1" preset="$2" want_dir="$3" want_env="$4" want_warning="$5" + local dir="${work}/cases/${name}" + mkdir -p "${dir}" + : > "${dir}/env" + : > "${dir}/output" + local rc=0 + if [ -n "${preset}" ]; then + env KAPI_PARSE_CACHE_DIR="${preset}" FIXED_DIR="${temp}/kapi-parse-cache" \ + RUNNER_TEMP="${temp}" GITHUB_WORKSPACE="${workspace}" \ + GITHUB_ENV="${dir}/env" GITHUB_OUTPUT="${dir}/output" \ + bash -e "${work}/step.sh" > "${dir}/stdout" 2>&1 || rc=$? + else + env -u KAPI_PARSE_CACHE_DIR FIXED_DIR="${temp}/kapi-parse-cache" \ + RUNNER_TEMP="${temp}" GITHUB_WORKSPACE="${workspace}" \ + GITHUB_ENV="${dir}/env" GITHUB_OUTPUT="${dir}/output" \ + bash -e "${work}/step.sh" > "${dir}/stdout" 2>&1 || rc=$? + fi + local before="${failures}" + [ "${rc}" = 0 ] || fail "${name}" "exit code ${rc}" + local got + got="$(sed -n 's/^dir=//p' "${dir}/output")" + [ "${got}" = "${want_dir}" ] || fail "${name}" "dir: got '${got}', want '${want_dir}'" + got="$(cat "${dir}/env")" + [ "${got}" = "${want_env}" ] || fail "${name}" "GITHUB_ENV: got '${got}', want '${want_env}'" + if [ "${want_warning}" = yes ]; then + grep -q '^::warning' "${dir}/stdout" || fail "${name}" "printed no warning" + else + ! grep -q '^::warning' "${dir}/stdout" || fail "${name}" "unexpected warning: $(cat "${dir}/stdout")" + fi + [ ! -e "${temp}" ] || fail "${name}" "created a directory" + if [ "${failures}" = "${before}" ]; then + echo "ok ${name}" + fi +} + +run_case unset "" "${temp}/kapi-parse-cache" "KAPI_PARSE_CACHE_DIR=${temp}/kapi-parse-cache" no +run_case preset-temp "${temp}/mine" "${temp}/mine" "" no +run_case preset-workspace "${workspace}/.cache/kapi" "${workspace}/.cache/kapi" "" no +run_case preset-outside "/home/runner/.cache/kapi" "" "" yes +run_case preset-relative "kapi-parse-cache" "" "" yes +run_case preset-dotdot "${temp}/../elsewhere" "" "" yes +run_case preset-prefix "${temp}-other/cache" "" "" yes + +# Both parse cache steps carry the directory the step names and the in-tree +# path, as fixed strings. +for name in "Restore kapi parse cache" "Restore kapi parse cache (no save)"; do + paths="$(NAME="${name}" yq '.runs.steps[] | select(.name == strenv(NAME)) | .with.path' "${root}/action.yml")" + # shellcheck disable=SC2016 # the literal expression text in action.yml + want='${{ steps.parse-cache-dir.outputs.dir }} +${{ inputs.project-dir }}/.kapi/work/cache/docs' + if [ "${paths}" = "${want}" ]; then + echo "ok ${name}: both paths" + else + fail "${name}" "path: got '${paths}'" + fi +done + +if [ "${failures}" -gt 0 ]; then + echo "${failures} assertion(s) failed" + exit 1 +fi +echo "all parse cache directory cases passed" diff --git a/test/parse-cache/locate.sh b/test/parse-cache/locate.sh deleted file mode 100755 index 0c14454..0000000 --- a/test/parse-cache/locate.sh +++ /dev/null @@ -1,119 +0,0 @@ -#!/usr/bin/env bash -# Runs scripts/parse-cache-dir.sh against the kapi stub in stub/ for each -# answer `kapi state path` can give, and asserts the mode and path it prints -# and what it leaves on disk. An answer the script does not accept must leave -# the file system as it was. With KAPI set to a kapi binary, it also runs the -# script against that binary on test/fixture and prints the mode it chose. -set -euo pipefail - -here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" -root="$(cd "${here}/../.." && pwd -P)" -script="${root}/scripts/parse-cache-dir.sh" -work="$(cd "$(mktemp -d)" && pwd -P)" -trap 'rm -rf "${work}"' EXIT - -failures=0 -fail() { - echo "FAIL $1: $2" - failures=$((failures + 1)) -} -value() { - sed -n "s/^$1=//p" "$2" | tail -n 1 -} -# snapshot DIR lists everything beneath DIR, links included. -snapshot() { - (cd "$1" && find . -not -name out -not -name err | LC_ALL=C sort) -} - -key=0123456789abcdef0123456789abcdef - -# run_case NAME OUTPUT [SET] [RUNNER_OS]: the stub prints OUTPUT (SET=0 makes -# it fail like a kapi without `state path`). The data root is NAME/data. -run_case() { - local name="$1" output="$2" set="${3:-1}" os="${4:-Linux}" - mkdir -p "${work}/${name}/project" "${work}/${name}/data" - env PATH="${here}/stub:${PATH}" KAPI_STUB_STATE_SET="${set}" KAPI_STUB_STATE_OUTPUT="${output}" \ - KAPI_DATA_DIR="${work}/${name}/data" RUNNER_OS="${os}" \ - "${script}" "${work}/${name}/project" "${work}/${name}/fixed" \ - > "${work}/${name}/out" 2> "${work}/${name}/err" || fail "${name}" "exit code $?" -} - -# expect_skip NAME OUTPUT [PREPARE]: the script carries no cache, prints a -# warning, and changes nothing on disk. -expect_skip() { - local name="$1" output="$2" - mkdir -p "${work}/${name}/data" "${work}/${name}/project" - local before - before="$(snapshot "${work}/${name}")" - run_case "${name}" "${output}" - [ "$(value mode "${work}/${name}/out")" = none ] || fail "${name}" "mode: $(cat "${work}/${name}/out")" - [ -z "$(value path "${work}/${name}/out")" ] || fail "${name}" "printed a path" - grep -q '::warning' "${work}/${name}/err" || fail "${name}" "printed no warning" - [ "$(snapshot "${work}/${name}")" = "${before}" ] || fail "${name}" "changed the file system" - [ ! -e "${work}/${name}/fixed" ] || fail "${name}" "created the fixed directory" -} - -# A good path: the state directory's parse cache links to the fixed -# directory, and a write through it lands there. -state="${work}/good/data/checkouts/${key}" -run_case good "${state}" -[ "$(value mode "${work}/good/out")" = state ] || fail good "mode: $(cat "${work}/good/out")" -[ "$(value path "${work}/good/out")" = "${work}/good/fixed" ] || fail good "path: $(cat "${work}/good/out")" -[ -L "${state}/work/cache/docs" ] || fail good "work/cache/docs is not a link" -echo entry > "${state}/work/cache/docs/index.db" -[ -f "${work}/good/fixed/index.db" ] || fail good "a write through the link did not reach the fixed directory" -[ ! -e "${work}/good/project/.kapi" ] || fail good "wrote into the checkout" - -# A kapi without `state path`: the parse cache stays in the checkout. -run_case tree "" 0 -[ "$(value mode "${work}/tree/out")" = tree ] || fail tree "mode: $(cat "${work}/tree/out")" -[ "$(value path "${work}/tree/out")" = "${work}/tree/project/.kapi/work/cache/docs" ] || fail tree "path: $(cat "${work}/tree/out")" -[ ! -e "${work}/tree/fixed" ] || fail tree "created the fixed directory" - -# Answers that are refused without touching anything. -expect_skip empty "" -expect_skip two-lines "${work}/two-lines/data/checkouts/${key} -${work}/two-lines/data/checkouts/${key}" -expect_skip relative "data/checkouts/${key}" -expect_skip outside "${work}/outside/elsewhere/checkouts/${key}" -expect_skip not-a-checkout "${work}/not-a-checkout/data/checkouts/../../.." -expect_skip control "${work}/control/data/checkouts/${key}$(printf '\t')" -mkdir -p "${work}/link/real/checkouts/${key}" "${work}/link/data" -ln -s "${work}/link/real/checkouts" "${work}/link/data/checkouts" -expect_skip link "${work}/link/data/checkouts/${key}" - -# A link an earlier run left behind is replaced. -stale="${work}/stale/data/checkouts/${key}" -mkdir -p "${stale}/work/cache" -ln -s "${work}/elsewhere" "${stale}/work/cache/docs" -run_case stale "${stale}" -[ "$(value mode "${work}/stale/out")" = state ] || fail stale "mode: $(cat "${work}/stale/out")" -[ "$(readlink "${stale}/work/cache/docs")" = "${work}/stale/fixed" ] || fail stale "link: $(readlink "${stale}/work/cache/docs")" - -# A parse cache the runner already holds is left alone and not restored over. -kept="${work}/kept/data/checkouts/${key}" -mkdir -p "${kept}/work/cache/docs" -echo entry > "${kept}/work/cache/docs/index.db" -run_case kept "${kept}" -[ "$(value mode "${work}/kept/out")" = none ] || fail kept "mode: $(cat "${work}/kept/out")" -[ -z "$(value path "${work}/kept/out")" ] || fail kept "printed a path" -[ -f "${kept}/work/cache/docs/index.db" ] && [ ! -L "${kept}/work/cache/docs" ] || fail kept "changed the existing cache" - -# Windows: no link is made, and no cache is carried. -run_case windows "${work}/windows/data/checkouts/${key}" 1 Windows -[ "$(value mode "${work}/windows/out")" = none ] || fail windows "mode: $(cat "${work}/windows/out")" -[ ! -e "${work}/windows/data/checkouts" ] || fail windows "created the state directory" - -if [ -n "${KAPI:-}" ]; then - bin="${work}/bin" - mkdir -p "${bin}" - ln -s "${KAPI}" "${bin}/kapi" - cp -R "${root}/test/fixture" "${work}/real" - PATH="${bin}:${PATH}" "${script}" "${work}/real" "${work}/real-fixed" | sed "s|${work}||; s/^/real kapi: /" -fi - -if [ "${failures}" -gt 0 ]; then - echo "${failures} assertion(s) failed" - exit 1 -fi -echo "all parse cache locations passed" diff --git a/test/parse-cache/stub/kapi b/test/parse-cache/stub/kapi deleted file mode 100755 index a8a186d..0000000 --- a/test/parse-cache/stub/kapi +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env bash -# A stand-in for the kapi CLI that answers `kapi state path`. With -# KAPI_STUB_STATE_SET=1 it prints KAPI_STUB_STATE_OUTPUT exactly (which may be -# empty or span lines), as a kapi that keeps each checkout's state outside the -# checkout does; otherwise it fails the way a kapi without the command does. -set -euo pipefail -if [ "${1:-}" = "state" ] && [ "${2:-}" = "path" ] && [ "${KAPI_STUB_STATE_SET:-}" = 1 ]; then - printf '%s' "${KAPI_STUB_STATE_OUTPUT:-}" - exit 0 -fi -echo "Error: unknown command \"${1:-}\" for \"kapi\"" >&2 -exit 1 diff --git a/test/plugins/windows-unlisted.sh b/test/plugins/windows-unlisted.sh new file mode 100755 index 0000000..7ef9e59 --- /dev/null +++ b/test/plugins/windows-unlisted.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +# Asserts that the registry lists no bowrain plugin build for Windows: a +# default install fails and names the platform. registry#6 left Windows +# without a listed build until the next kapi release, by decision. When this +# script fails because the install succeeds, a Windows build is listed again: +# restore the default `plugins` input in the Windows jobs and delete this +# script. +set -uo pipefail + +out="$(kapi plugin install bowrain 2>&1)" +rc=$? +echo "${out}" +if [ "${rc}" -eq 0 ]; then + echo "::error::bowrain installed on Windows, so a Windows build is listed again. Restore the default plugins input in the Windows jobs and delete test/plugins/windows-unlisted.sh." + exit 1 +fi +if ! grep -q 'no version of plugin "bowrain"' <<< "${out}" || ! grep -q 'for platform windows/' <<< "${out}"; then + echo "::error::bowrain failed to install on Windows for a reason other than a missing Windows build." + exit 1 +fi +echo "As expected: no bowrain build is listed for Windows." From 8b2cdf4bb127f73adb8d8d2771799c9569536262 Mon Sep 17 00:00:00 2001 From: Asgeir Frimannsson Date: Mon, 23 Nov 2026 19:30:03 +0100 Subject: [PATCH 05/20] Accept a caller's KAPI_PARSE_CACHE_DIR only under runner.temp kapi refuses a parse cache directory inside the checkout, a link, or one wider than 0700, and creates it at 0700 itself, so the action creates no directory and the README tells other CI systems to let kapi create it. Verified: test/parse-cache/env.sh; actionlint Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01GtGobCdyR3d9yLXAfX9jsr --- README.md | 2 +- action.yml | 17 +++++++++++------ test/parse-cache/env.sh | 2 +- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1ba2d02..308547e 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,7 @@ The binary is cached keyed on version + OS + arch. Plugins are cached keyed on t kapi records how it parsed each source and target file, so a later run can replay a file instead of parsing it again. With `parse-cache` on and a recipe in `project-dir`, the action restores that parse cache before your steps run, and `actions/cache` saves it again when the job ends. -The action sets `KAPI_PARSE_CACHE_DIR` to a directory under `runner.temp` for the rest of the job, and caches that directory together with `/.kapi/work/cache/docs`. A kapi that reads the variable keeps its parse cache there; kapi 1.3 and earlier keep it in the checkout. If your workflow sets `KAPI_PARSE_CACHE_DIR` itself, the action keeps your value and caches it when it lies under `runner.temp` or the workspace. On another CI system, set `KAPI_PARSE_CACHE_DIR` to a fixed directory and cache that directory. +The action sets `KAPI_PARSE_CACHE_DIR` to a directory under `runner.temp` for the rest of the job, and caches that directory together with `/.kapi/work/cache/docs`. A kapi that reads the variable keeps its parse cache there; kapi 1.3 and earlier keep it in the checkout. If your workflow sets `KAPI_PARSE_CACHE_DIR` itself, the action keeps your value and caches it when it lies under `runner.temp`. On another CI system, set `KAPI_PARSE_CACHE_DIR` to a fixed absolute directory outside the checkout and cache that directory. Let kapi create it: kapi makes it readable by your user only, and refuses a directory that others can read or write. A restored parse cache does not change any result. kapi keys each entry by the file's path and content hash, the parse configuration, the recipe and the kapi build, and parses the file again when any of them differs. The test workflow checks this on every change: with the cache restored, `kapi status`, `kapi check`, `kapi check --ship` and `kapi up` must match a cold run exactly, in exit codes, output and every file written, and they must still match after the source changes. diff --git a/action.yml b/action.yml index 9fb42b5..68eee64 100644 --- a/action.yml +++ b/action.yml @@ -243,8 +243,13 @@ runs: # on every run, which keeps the cache version stable. # # A KAPI_PARSE_CACHE_DIR the workflow set itself is kept, and cached - # when it lies under runner.temp or the workspace; otherwise the - # parse cache is not carried. + # when it lies under runner.temp; otherwise the parse cache is not + # carried. (kapi refuses a directory inside the checkout.) + # + # The step creates no directory. kapi creates the parse cache + # directory with owner-only permissions (0700) and refuses one that is + # wider, a link, or owned by someone else; a directory made here with + # a plain mkdir would be 0755 and be saved, and restored, that way. - name: Set kapi parse cache directory id: parse-cache-dir if: steps.parse-cache.outputs.enabled != 'false' && steps.detect-project.outputs.found == 'true' @@ -264,16 +269,16 @@ runs: exit 0 ;; esac - for base in "${RUNNER_TEMP%[/\\]}" "${GITHUB_WORKSPACE%[/\\]}"; do - [ -n "${base}" ] || continue + base="${RUNNER_TEMP%[/\\]}" + if [ -n "${base}" ]; then case "${dir}" in "${base}"/* | "${base}"\\*) echo "dir=${dir}" >> "$GITHUB_OUTPUT" exit 0 ;; esac - done - echo "::warning title=setup-kapi::KAPI_PARSE_CACHE_DIR (${dir}) is not under runner.temp or the workspace, so the parse cache is not carried." + fi + echo "::warning title=setup-kapi::KAPI_PARSE_CACHE_DIR (${dir}) is not under runner.temp, so the parse cache is not carried." # 14. Restore kapi's parse cache, and save it at job end. The key is unique # to the job and run attempt, so every run saves its own cache through diff --git a/test/parse-cache/env.sh b/test/parse-cache/env.sh index e1769a9..bc20762 100755 --- a/test/parse-cache/env.sh +++ b/test/parse-cache/env.sh @@ -67,7 +67,7 @@ run_case() { run_case unset "" "${temp}/kapi-parse-cache" "KAPI_PARSE_CACHE_DIR=${temp}/kapi-parse-cache" no run_case preset-temp "${temp}/mine" "${temp}/mine" "" no -run_case preset-workspace "${workspace}/.cache/kapi" "${workspace}/.cache/kapi" "" no +run_case preset-workspace "${workspace}/.cache/kapi" "" "" yes run_case preset-outside "/home/runner/.cache/kapi" "" "" yes run_case preset-relative "kapi-parse-cache" "" "" yes run_case preset-dotdot "${temp}/../elsewhere" "" "" yes From 73931d644381de2bc57f3cbfc5075fd01ae51798 Mon Sep 17 00:00:00 2001 From: Asgeir Frimannsson Date: Mon, 23 Nov 2026 19:32:49 +0100 Subject: [PATCH 06/20] Add a temporary probe of cache directory modes after restore Verified: none (a measurement job, removed before merge) Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01GtGobCdyR3d9yLXAfX9jsr --- .github/workflows/test.yml | 64 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7dacc0b..3ce3aab 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -507,3 +507,67 @@ jobs: - name: Verify bowrain matches a fresh install shell: bash run: test/plugins/matches-fresh.sh bowrain + + # TEMPORARY: measures the owner and mode of a parse cache directory after an + # actions/cache round trip, on each hosted runner. Remove before merging. + probe-cache-mode-save: + name: "Probe cache mode: save (${{ matrix.os }})" + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + steps: + - name: Make a 0700 directory with files + shell: bash + run: | + d="${RUNNER_TEMP}/kapi-parse-cache" + mkdir -m 700 "$d" + echo entry > "$d/index.db" + (umask 077; echo part > "$d/parts-a.log") + mkdir -m 700 "$d/sub" + echo x > "$d/sub/blob" + echo "umask: $(umask)" + if stat -c '%A %U:%G %a %n' "$d" > /dev/null 2>&1; then + stat -c '%A %U:%G %a %n' "$d" "$d"/* "$d/sub/blob" + else + stat -f '%Sp %Su:%Sg %Lp %N' "$d" "$d"/* "$d/sub/blob" + fi + if [ "$RUNNER_OS" = Windows ]; then icacls "$(cygpath -w "$d")"; fi + + - uses: actions/cache/save@v5 + with: + path: | + ${{ runner.temp }}/kapi-parse-cache + test/fixture/.kapi/work/cache/docs + key: probe-mode-${{ runner.os }}-${{ github.run_id }}-${{ github.run_attempt }} + + probe-cache-mode-restore: + name: "Probe cache mode: restore (${{ matrix.os }})" + needs: probe-cache-mode-save + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + steps: + - uses: actions/cache/restore@v5 + with: + path: | + ${{ runner.temp }}/kapi-parse-cache + test/fixture/.kapi/work/cache/docs + key: probe-mode-${{ runner.os }}-${{ github.run_id }}-${{ github.run_attempt }} + fail-on-cache-miss: true + + - name: Print owner and mode of what was restored + shell: bash + run: | + d="${RUNNER_TEMP}/kapi-parse-cache" + echo "user: $(id -un 2>/dev/null || whoami) umask: $(umask)" + ls -ld "$d" + if stat -c '%A %U:%G %a %n' "$d" > /dev/null 2>&1; then + stat -c '%A %U:%G %a %n' "$d" "$d"/* "$d/sub/blob" + else + stat -f '%Sp %Su:%Sg %Lp %N' "$d" "$d"/* "$d/sub/blob" + fi + if [ "$RUNNER_OS" = Windows ]; then icacls "$(cygpath -w "$d")"; icacls "$(cygpath -w "$d/index.db")"; fi From ba39774e5a408ffd5a743fc224d6a9a90b8083ab Mon Sep 17 00:00:00 2001 From: Asgeir Frimannsson Date: Mon, 23 Nov 2026 19:34:41 +0100 Subject: [PATCH 07/20] Let the cache mode probe run on Windows Verified: none (a measurement job, removed before merge) Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01GtGobCdyR3d9yLXAfX9jsr --- .github/workflows/test.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3ce3aab..fcbbf51 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -522,10 +522,11 @@ jobs: shell: bash run: | d="${RUNNER_TEMP}/kapi-parse-cache" - mkdir -m 700 "$d" + # Git Bash on Windows cannot set a mode with mkdir -m. + if [ "$RUNNER_OS" = Windows ]; then mkdir "$d"; else mkdir -m 700 "$d"; fi echo entry > "$d/index.db" (umask 077; echo part > "$d/parts-a.log") - mkdir -m 700 "$d/sub" + if [ "$RUNNER_OS" = Windows ]; then mkdir "$d/sub"; else mkdir -m 700 "$d/sub"; fi echo x > "$d/sub/blob" echo "umask: $(umask)" if stat -c '%A %U:%G %a %n' "$d" > /dev/null 2>&1; then @@ -545,6 +546,7 @@ jobs: probe-cache-mode-restore: name: "Probe cache mode: restore (${{ matrix.os }})" needs: probe-cache-mode-save + if: ${{ !cancelled() }} runs-on: ${{ matrix.os }} strategy: fail-fast: false From 6c271f7568e91fbb5365b163c15fa79c1e7343bd Mon Sep 17 00:00:00 2001 From: Asgeir Frimannsson Date: Mon, 23 Nov 2026 19:36:40 +0100 Subject: [PATCH 08/20] Remove the cache mode probe Verified: none (removes a measurement job; results in run 45239899921) Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01GtGobCdyR3d9yLXAfX9jsr --- .github/workflows/test.yml | 66 -------------------------------------- 1 file changed, 66 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index fcbbf51..7dacc0b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -507,69 +507,3 @@ jobs: - name: Verify bowrain matches a fresh install shell: bash run: test/plugins/matches-fresh.sh bowrain - - # TEMPORARY: measures the owner and mode of a parse cache directory after an - # actions/cache round trip, on each hosted runner. Remove before merging. - probe-cache-mode-save: - name: "Probe cache mode: save (${{ matrix.os }})" - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - os: [ubuntu-latest, macos-latest, windows-latest] - steps: - - name: Make a 0700 directory with files - shell: bash - run: | - d="${RUNNER_TEMP}/kapi-parse-cache" - # Git Bash on Windows cannot set a mode with mkdir -m. - if [ "$RUNNER_OS" = Windows ]; then mkdir "$d"; else mkdir -m 700 "$d"; fi - echo entry > "$d/index.db" - (umask 077; echo part > "$d/parts-a.log") - if [ "$RUNNER_OS" = Windows ]; then mkdir "$d/sub"; else mkdir -m 700 "$d/sub"; fi - echo x > "$d/sub/blob" - echo "umask: $(umask)" - if stat -c '%A %U:%G %a %n' "$d" > /dev/null 2>&1; then - stat -c '%A %U:%G %a %n' "$d" "$d"/* "$d/sub/blob" - else - stat -f '%Sp %Su:%Sg %Lp %N' "$d" "$d"/* "$d/sub/blob" - fi - if [ "$RUNNER_OS" = Windows ]; then icacls "$(cygpath -w "$d")"; fi - - - uses: actions/cache/save@v5 - with: - path: | - ${{ runner.temp }}/kapi-parse-cache - test/fixture/.kapi/work/cache/docs - key: probe-mode-${{ runner.os }}-${{ github.run_id }}-${{ github.run_attempt }} - - probe-cache-mode-restore: - name: "Probe cache mode: restore (${{ matrix.os }})" - needs: probe-cache-mode-save - if: ${{ !cancelled() }} - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - os: [ubuntu-latest, macos-latest, windows-latest] - steps: - - uses: actions/cache/restore@v5 - with: - path: | - ${{ runner.temp }}/kapi-parse-cache - test/fixture/.kapi/work/cache/docs - key: probe-mode-${{ runner.os }}-${{ github.run_id }}-${{ github.run_attempt }} - fail-on-cache-miss: true - - - name: Print owner and mode of what was restored - shell: bash - run: | - d="${RUNNER_TEMP}/kapi-parse-cache" - echo "user: $(id -un 2>/dev/null || whoami) umask: $(umask)" - ls -ld "$d" - if stat -c '%A %U:%G %a %n' "$d" > /dev/null 2>&1; then - stat -c '%A %U:%G %a %n' "$d" "$d"/* "$d/sub/blob" - else - stat -f '%Sp %Su:%Sg %Lp %N' "$d" "$d"/* "$d/sub/blob" - fi - if [ "$RUNNER_OS" = Windows ]; then icacls "$(cygpath -w "$d")"; icacls "$(cygpath -w "$d/index.db")"; fi From 7e6629667fc4eb5c7594b9aa1e54c744f48c906a Mon Sep 17 00:00:00 2001 From: Asgeir Frimannsson Date: Mon, 23 Nov 2026 19:47:04 +0100 Subject: [PATCH 09/20] Add temporary cold and warm runs against the kapi build from neokapi#3804 Verified: none (a measurement job, removed before merge) Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01GtGobCdyR3d9yLXAfX9jsr --- .github/workflows/test.yml | 175 +++++++++++++++++++++++++++++++++ test/parse-cache/branch-e2e.sh | 152 ++++++++++++++++++++++++++++ 2 files changed, 327 insertions(+) create mode 100755 test/parse-cache/branch-e2e.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7dacc0b..a75cd90 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -507,3 +507,178 @@ jobs: - name: Verify bowrain matches a fresh install shell: bash run: test/plugins/matches-fresh.sh bowrain + + # TEMPORARY: cold and warm runs of the kapi build from neokapi/neokapi#3804 + # through this action. Removed before merging. + e2e-build-kapi: + name: "E2E: build kapi #3804 (${{ matrix.os }})" + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest] + steps: + - uses: actions/checkout@v6 + with: + repository: neokapi/neokapi + ref: bd3ab498682717f43f4dcb773f69b98bafa97cdb + path: neokapi + + - uses: actions/setup-go@v6 + with: + go-version-file: neokapi/go.mod + cache-dependency-path: neokapi/**/go.sum + + - name: Install ICU + shell: bash + run: | + if [ "$RUNNER_OS" = Linux ]; then + sudo apt-get update -q && sudo apt-get install -y -q libicu-dev pkg-config + else + brew list icu4c > /dev/null 2>&1 || brew install icu4c pkg-config + echo "PKG_CONFIG_PATH=$(brew --prefix icu4c)/lib/pkgconfig" >> "$GITHUB_ENV" + fi + + - name: Build kapi + shell: bash + working-directory: neokapi + run: | + go run ./core/i18n/gen/catalogs + cd kapi + go build -trimpath -tags fts5 -o "${RUNNER_TEMP}/kapi-branch/kapi" ./cmd/kapi + "${RUNNER_TEMP}/kapi-branch/kapi" version + + - uses: actions/upload-artifact@v7 + with: + name: kapi-3804-${{ matrix.os }} + path: ${{ runner.temp }}/kapi-branch + retention-days: 1 + + e2e-cold: + name: "E2E: cold run (${{ matrix.os }})" + needs: e2e-build-kapi + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest] + steps: + - uses: actions/checkout@v6 + + - uses: actions/download-artifact@v8 + with: + name: kapi-3804-${{ matrix.os }} + path: ${{ runner.temp }}/kapi-branch + + - name: Setup kapi + uses: ./ + with: + token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} + version: 1.4.0-rc5 + plugins: "" + project-dir: test/fixture + + - name: Put the #3804 build first on PATH + shell: bash + run: | + chmod +x "${RUNNER_TEMP}/kapi-branch/kapi" + echo "${RUNNER_TEMP}/kapi-branch" >> "$GITHUB_PATH" + + - name: Cold run + shell: bash + run: test/parse-cache/branch-e2e.sh cold "${RUNNER_TEMP}/e2e/cold" + + - uses: actions/upload-artifact@v7 + with: + name: e2e-cold-${{ matrix.os }} + path: ${{ runner.temp }}/e2e/cold + retention-days: 1 + + e2e-warm: + name: "E2E: warm run (${{ matrix.os }})" + needs: e2e-cold + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest] + steps: + - uses: actions/checkout@v6 + + - uses: actions/download-artifact@v8 + with: + name: kapi-3804-${{ matrix.os }} + path: ${{ runner.temp }}/kapi-branch + + - uses: actions/download-artifact@v8 + with: + name: e2e-cold-${{ matrix.os }} + path: ${{ runner.temp }}/e2e/cold + + - name: Install strace + if: runner.os == 'Linux' + shell: bash + run: sudo apt-get install -y -q strace + + - name: Setup kapi + uses: ./ + with: + token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} + version: 1.4.0-rc5 + plugins: "" + project-dir: test/fixture + + - name: Put the #3804 build first on PATH + shell: bash + run: | + chmod +x "${RUNNER_TEMP}/kapi-branch/kapi" + echo "${RUNNER_TEMP}/kapi-branch" >> "$GITHUB_PATH" + + - name: Warm run + shell: bash + run: test/parse-cache/branch-e2e.sh warm "${RUNNER_TEMP}/e2e/cold" "${RUNNER_TEMP}/e2e/warm" + + e2e-refuse: + name: "E2E: refused (${{ matrix.case }}, ${{ matrix.os }})" + needs: e2e-build-kapi + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest] + case: [inside-project, wider] + steps: + - uses: actions/checkout@v6 + + - uses: actions/download-artifact@v8 + with: + name: kapi-3804-${{ matrix.os }} + path: ${{ runner.temp }}/kapi-branch + + - name: Set a parse cache directory inside the project + if: matrix.case == 'inside-project' + shell: bash + run: echo "KAPI_PARSE_CACHE_DIR=${GITHUB_WORKSPACE}/test/fixture/.kapi-parse" >> "$GITHUB_ENV" + + - name: Setup kapi + uses: ./ + with: + token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} + version: 1.4.0-rc5 + plugins: "" + project-dir: test/fixture + + - name: Put the #3804 build first on PATH + shell: bash + run: | + chmod +x "${RUNNER_TEMP}/kapi-branch/kapi" + echo "${RUNNER_TEMP}/kapi-branch" >> "$GITHUB_PATH" + + - name: Make the directory wider than kapi accepts + if: matrix.case == 'wider' + shell: bash + run: mkdir -m 755 "${KAPI_PARSE_CACHE_DIR}" + + - name: Every kapi command fails with kapi's message + shell: bash + run: test/parse-cache/branch-e2e.sh refuse "${RUNNER_TEMP}/e2e/refuse" diff --git a/test/parse-cache/branch-e2e.sh b/test/parse-cache/branch-e2e.sh new file mode 100755 index 0000000..3ae6bf3 --- /dev/null +++ b/test/parse-cache/branch-e2e.sh @@ -0,0 +1,152 @@ +#!/usr/bin/env bash +# TEMPORARY: cold and warm runs of a kapi that reads KAPI_PARSE_CACHE_DIR, +# through setup-kapi. Removed before merging. +# +# branch-e2e.sh cold +# branch-e2e.sh warm +# branch-e2e.sh refuse (expects every kapi command to fail) +# +# The action has already set KAPI_PARSE_CACHE_DIR; kapi on PATH is the build +# under test; the project is test/fixture. +set -euo pipefail + +here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +root="$(cd "${here}/../.." && pwd)" +mode="$1" +shift + +pc="${KAPI_PARSE_CACHE_DIR:?KAPI_PARSE_CACHE_DIR is not set}" + +mode_of() { + if stat -c '%a' "$1" > /dev/null 2>&1; then stat -c '%a %U' "$1"; else stat -f '%Lp %Su' "$1"; fi +} + +# snapshot DIR: each file under DIR with its digest, and the index rows. +snapshot() { + (cd "$1" && find . -type f ! -name 'index.db*' | LC_ALL=C sort | while read -r f; do + printf '%s %s\n' "$(shasum -a 256 < "$f" | cut -d' ' -f1)" "$f" + done) + python3 - "$1/index.db" <<'PY' +import sqlite3, sys +db = sqlite3.connect(sys.argv[1]) +for row in db.execute("select path, build, config_key, content_hash, parts_ref, parts_digest from documents order by path, config_key"): + print("row", *row) +PY +} + +# normalize RECORD: drop timestamps that differ between any two runs. +normalize() { + local out="$1.norm" + mkdir -p "$out" + for f in "$1"/*.json; do + jq -c 'del(.. | .at?)' "$f" > "$out/$(basename "$f")" 2> /dev/null || cp "$f" "$out/" + done + cp "$1"/*.rc "$1/tree.txt" "$out/" +} + +check_layout() { + local results="$1" + kapi version | tee "${results}/version.txt" + kapi state path -p test/fixture --json | tee "${results}/state-path.json" + local state got + state="$(jq -r .path "${results}/state-path.json")" + got="$(jq -r .parse_cache "${results}/state-path.json")" + [ "${got}" = "${pc}" ] || { echo "::error::kapi resolved the parse cache to ${got}, not ${pc}"; exit 1; } + [ ! -e "${state}/work/cache/docs" ] || { echo "::error::the state directory holds a parse cache"; exit 1; } + [ ! -e test/fixture/.kapi/work ] || { echo "::error::the checkout holds .kapi/work"; exit 1; } + echo "parse cache directory: ${pc} ($(mode_of "${pc}"))" | tee -a "${results}/summary.txt" + [ "$(mode_of "${pc}" | cut -d' ' -f1)" = 700 ] || { echo "::error::the parse cache directory is not 0700"; exit 1; } + find "${pc}" -type f | sed "s|${pc}||" | LC_ALL=C sort | tee "${results}/pc-files.txt" +} + +case "${mode}" in + cold) + results="$1" + mkdir -p "${results}" + [ ! -e "${pc}" ] || { echo "::error::the cold run found ${pc} already present"; exit 1; } + "${root}/test/state-cache/run.sh" test/fixture "${results}/run" + check_layout "${results}" + snapshot "${pc}" > "${results}/snapshot.txt" + echo "entries: $(grep -c '^row ' "${results}/snapshot.txt"), part files: $(grep -vc '^row ' "${results}/snapshot.txt")" | tee -a "${results}/summary.txt" + ;; + + warm) + cold="$1" + results="$2" + mkdir -p "${results}" + # What the restore brought: only the parse cache directory. + [ -s "${pc}/index.db" ] || { echo "::error::no parse cache was restored into ${pc}"; exit 1; } + echo "restored: ${pc} ($(mode_of "${pc}"))" | tee -a "${results}/summary.txt" + [ "$(mode_of "${pc}" | cut -d' ' -f1)" = 700 ] || { echo "::error::the restored directory is not 0700"; exit 1; } + [ ! -e test/fixture/.kapi/work ] || { echo "::error::the restore wrote into the checkout"; exit 1; } + snapshot "${pc}" > "${results}/before.txt" + diff -u "${cold}/snapshot.txt" "${results}/before.txt" + + if command -v strace > /dev/null; then + strace -f -qq -e trace=openat -o "${results}/openat.txt" \ + "${root}/test/state-cache/run.sh" test/fixture "${results}/run" + opened="$(grep -c "${pc}/parts-[^\"]*\.log\", O_RDONLY" "${results}/openat.txt" || true)" + echo "the warm run opened recorded part files for reading ${opened} times" | tee -a "${results}/summary.txt" + [ "${opened}" -gt 0 ] || { echo "::error::the warm run read no recorded part file"; exit 1; } + else + "${root}/test/state-cache/run.sh" test/fixture "${results}/run" + fi + check_layout "${results}" + snapshot "${pc}" > "${results}/after.txt" + python3 - "${pc}/index.db" <<'PY' | tee -a "${results}/summary.txt" +import sqlite3, sys +for path, checkout in sqlite3.connect(sys.argv[1]).execute("select path, checkout from documents order by path"): + print(f"entry {path}: recorded for checkout {checkout}") +PY + if diff -u "${results}/before.txt" "${results}/after.txt"; then + echo "the warm run wrote no entry: every file it parsed came from the cache" | tee -a "${results}/summary.txt" + else + echo "::error::the warm run changed the parse cache" + exit 1 + fi + + # A cold run of a fresh copy with its own empty parse cache and data root. + "${root}/test/state-cache/cold-copy.sh" "${RUNNER_TEMP}/cold" + (cd "${RUNNER_TEMP}/cold" && KAPI_PARSE_CACHE_DIR="${RUNNER_TEMP}/cold-parse" KAPI_DATA_DIR="${RUNNER_TEMP}/cold-data" \ + "${root}/test/state-cache/run.sh" test/fixture "${results}/cold-here") + normalize "${results}/run" + normalize "${results}/cold-here" + normalize "${cold}/run" + "${root}/test/state-cache/compare.sh" "${results}/cold-here.norm" "${results}/run.norm" + "${root}/test/state-cache/compare.sh" "${cold}/run.norm" "${results}/run.norm" + + # Control: with one recorded part file damaged, kapi must notice and parse + # that file again, which shows the warm run's entries were read and checked. + ctl="${RUNNER_TEMP}/control-parse" + rm -rf "${ctl}" + cp -Rp "${pc}" "${ctl}" + chmod 700 "${ctl}" + # The entry for the source file every command reads; one byte changed. + victim="$(python3 -c 'import sqlite3,sys; print(sqlite3.connect(sys.argv[1]).execute("select parts_ref from documents where path = ?", ("locales/en.json",)).fetchone()[0])' "${ctl}/index.db")" + python3 -c 'import sys; p=sys.argv[1]; b=bytearray(open(p,"rb").read()); i=len(b)//2; b[i] = ord("Z") if b[i] != ord("Z") else ord("Y"); open(p,"wb").write(b)' "${ctl}/${victim}" + snapshot "${ctl}" > "${results}/control-before.txt" + "${root}/test/state-cache/cold-copy.sh" "${RUNNER_TEMP}/control" + (cd "${RUNNER_TEMP}/control" && KAPI_PARSE_CACHE_DIR="${ctl}" KAPI_DATA_DIR="${RUNNER_TEMP}/control-data" \ + kapi status -p test/fixture/kapi.yaml --json > /dev/null) + snapshot "${ctl}" > "${results}/control-after.txt" + if diff -q "${results}/control-before.txt" "${results}/control-after.txt" > /dev/null; then + echo "::error::kapi accepted a damaged part file" + exit 1 + fi + echo "control: with ${victim} damaged, kapi parsed that file again:" | tee -a "${results}/summary.txt" + diff "${results}/control-before.txt" "${results}/control-after.txt" | tee -a "${results}/summary.txt" || true + ;; + + refuse) + results="$1" + mkdir -p "${results}" + for cmd in "status -p test/fixture/kapi.yaml" "up -p test/fixture/kapi.yaml --json"; do + rc=0 + # shellcheck disable=SC2086 # the command is a word list + kapi ${cmd} > "${results}/out.txt" 2>&1 || rc=$? + echo "kapi ${cmd}: exit ${rc}" | tee -a "${results}/summary.txt" + sed 's/^/ | /' "${results}/out.txt" | tail -n 5 | tee -a "${results}/summary.txt" + [ "${rc}" -ne 0 ] || { echo "::error::kapi ${cmd} accepted the parse cache directory"; exit 1; } + done + ;; +esac From 33b0f2741fd53f0b05f92fc195bd7b8d4606aed7 Mon Sep 17 00:00:00 2001 From: Asgeir Frimannsson Date: Mon, 23 Nov 2026 19:47:18 +0100 Subject: [PATCH 10/20] Widen an existing parse cache directory in the refusal probe Verified: none (a measurement job, removed before merge) Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01GtGobCdyR3d9yLXAfX9jsr --- .github/workflows/test.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a75cd90..985844e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -677,7 +677,10 @@ jobs: - name: Make the directory wider than kapi accepts if: matrix.case == 'wider' shell: bash - run: mkdir -m 755 "${KAPI_PARSE_CACHE_DIR}" + run: | + # A restored cache may already be there; widen it either way. + mkdir -p "${KAPI_PARSE_CACHE_DIR}" + chmod 755 "${KAPI_PARSE_CACHE_DIR}" - name: Every kapi command fails with kapi's message shell: bash From ed00ae04905b4fd1c257bfd821e5c25dcfbc30f8 Mon Sep 17 00:00:00 2001 From: Asgeir Frimannsson Date: Mon, 23 Nov 2026 19:53:39 +0100 Subject: [PATCH 11/20] Clear a restored in-tree cache before the #3804 runs Verified: none (a measurement job, removed before merge) Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01GtGobCdyR3d9yLXAfX9jsr --- test/parse-cache/branch-e2e.sh | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/parse-cache/branch-e2e.sh b/test/parse-cache/branch-e2e.sh index 3ae6bf3..ded3d0c 100755 --- a/test/parse-cache/branch-e2e.sh +++ b/test/parse-cache/branch-e2e.sh @@ -17,6 +17,15 @@ shift pc="${KAPI_PARSE_CACHE_DIR:?KAPI_PARSE_CACHE_DIR is not set}" +# These jobs install release 1.4.0-rc5 and then run the #3804 build, so the +# cache key names 1.4.0-rc5 and the restore can bring the in-tree cache another +# job saved with the real 1.4.0-rc5. Remove it, so the check below sees only +# what this build writes. +if [ -e test/fixture/.kapi/work ]; then + echo "note: removed test/fixture/.kapi/work, restored from a 1.4.0-rc5 in-tree cache" + rm -rf test/fixture/.kapi/work +fi + mode_of() { if stat -c '%a' "$1" > /dev/null 2>&1; then stat -c '%a %U' "$1"; else stat -f '%Lp %Su' "$1"; fi } From 273a49179bcd77e67daeebad73151a8596d8bda6 Mon Sep 17 00:00:00 2001 From: Asgeir Frimannsson Date: Mon, 23 Nov 2026 19:58:17 +0100 Subject: [PATCH 12/20] Give the #3804 runs a cache key of their own Verified: none (a measurement job, removed before merge) Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01GtGobCdyR3d9yLXAfX9jsr --- .github/workflows/test.yml | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 985844e..2d338ff 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -574,7 +574,8 @@ jobs: uses: ./ with: token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} - version: 1.4.0-rc5 + # Not a version any other job caches under, so the key is this pair's own. + version: 1.2.0 plugins: "" project-dir: test/fixture @@ -624,7 +625,8 @@ jobs: uses: ./ with: token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} - version: 1.4.0-rc5 + # Not a version any other job caches under, so the key is this pair's own. + version: 1.2.0 plugins: "" project-dir: test/fixture @@ -685,3 +687,10 @@ jobs: - name: Every kapi command fails with kapi's message shell: bash run: test/parse-cache/branch-e2e.sh refuse "${RUNNER_TEMP}/e2e/refuse" + + # Leave nothing for the post step to save: a widened directory saved + # into the cache would be restored, and refused, by every later run. + - name: Remove the refused directory + if: always() + shell: bash + run: rm -rf "${KAPI_PARSE_CACHE_DIR}" From a15c618c77190a654501feb9bb10e6aec97d205f Mon Sep 17 00:00:00 2001 From: Asgeir Frimannsson Date: Mon, 23 Nov 2026 20:02:25 +0100 Subject: [PATCH 13/20] Match part files opened by name relative to the cache directory Verified: none (a measurement job, removed before merge) Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01GtGobCdyR3d9yLXAfX9jsr --- test/parse-cache/branch-e2e.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/parse-cache/branch-e2e.sh b/test/parse-cache/branch-e2e.sh index ded3d0c..bbe2f35 100755 --- a/test/parse-cache/branch-e2e.sh +++ b/test/parse-cache/branch-e2e.sh @@ -94,8 +94,12 @@ case "${mode}" in if command -v strace > /dev/null; then strace -f -qq -e trace=openat -o "${results}/openat.txt" \ "${root}/test/state-cache/run.sh" test/fixture "${results}/run" - opened="$(grep -c "${pc}/parts-[^\"]*\.log\", O_RDONLY" "${results}/openat.txt" || true)" - echo "the warm run opened recorded part files for reading ${opened} times" | tee -a "${results}/summary.txt" + # kapi opens a part file relative to the cache directory's descriptor, + # so the traced name is the bare file name. + opened="$(grep -cE 'openat2?\(.*"([^"]*/)?parts-[0-9a-f]+\.log".*O_RDONLY' "${results}/openat.txt" || true)" + echo "the warm run opened recorded part files for reading ${opened} times; examples:" | tee -a "${results}/summary.txt" + grep -E 'openat2?\(.*"([^"]*/)?parts-[0-9a-f]+\.log".*O_RDONLY' "${results}/openat.txt" | head -n 3 | tee -a "${results}/summary.txt" || true + grep -E 'parts-|index\.db' "${results}/openat.txt" | head -n 5 || true [ "${opened}" -gt 0 ] || { echo "::error::the warm run read no recorded part file"; exit 1; } else "${root}/test/state-cache/run.sh" test/fixture "${results}/run" From a5aa85fd8397cb3c904e43eaf41a62c03ce9b2a3 Mon Sep 17 00:00:00 2001 From: Asgeir Frimannsson Date: Mon, 23 Nov 2026 20:04:36 +0100 Subject: [PATCH 14/20] Start the cold run from an empty parse cache Verified: none (a measurement job, removed before merge) Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01GtGobCdyR3d9yLXAfX9jsr --- test/parse-cache/branch-e2e.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/parse-cache/branch-e2e.sh b/test/parse-cache/branch-e2e.sh index bbe2f35..1650942 100755 --- a/test/parse-cache/branch-e2e.sh +++ b/test/parse-cache/branch-e2e.sh @@ -72,7 +72,11 @@ case "${mode}" in cold) results="$1" mkdir -p "${results}" - [ ! -e "${pc}" ] || { echo "::error::the cold run found ${pc} already present"; exit 1; } + # An earlier run of this workflow may have left a cache under this key. + if [ -e "${pc}" ]; then + echo "note: removed ${pc}, restored from an earlier run, so this run starts cold" + rm -rf "${pc}" + fi "${root}/test/state-cache/run.sh" test/fixture "${results}/run" check_layout "${results}" snapshot "${pc}" > "${results}/snapshot.txt" From b59b7c91090fb9b11e9c0358f8e1de5a7758c315 Mon Sep 17 00:00:00 2001 From: Asgeir Frimannsson Date: Mon, 23 Nov 2026 20:08:37 +0100 Subject: [PATCH 15/20] Remove the temporary #3804 runs; state kapi's directory rules in the README Verified: cold and warm runs against the neokapi#3804 build through this action on Linux and macOS (run 45245109823); actionlint Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01GtGobCdyR3d9yLXAfX9jsr --- .github/workflows/test.yml | 187 --------------------------------- README.md | 2 +- test/parse-cache/branch-e2e.sh | 169 ----------------------------- 3 files changed, 1 insertion(+), 357 deletions(-) delete mode 100755 test/parse-cache/branch-e2e.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2d338ff..7dacc0b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -507,190 +507,3 @@ jobs: - name: Verify bowrain matches a fresh install shell: bash run: test/plugins/matches-fresh.sh bowrain - - # TEMPORARY: cold and warm runs of the kapi build from neokapi/neokapi#3804 - # through this action. Removed before merging. - e2e-build-kapi: - name: "E2E: build kapi #3804 (${{ matrix.os }})" - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - os: [ubuntu-latest, macos-latest] - steps: - - uses: actions/checkout@v6 - with: - repository: neokapi/neokapi - ref: bd3ab498682717f43f4dcb773f69b98bafa97cdb - path: neokapi - - - uses: actions/setup-go@v6 - with: - go-version-file: neokapi/go.mod - cache-dependency-path: neokapi/**/go.sum - - - name: Install ICU - shell: bash - run: | - if [ "$RUNNER_OS" = Linux ]; then - sudo apt-get update -q && sudo apt-get install -y -q libicu-dev pkg-config - else - brew list icu4c > /dev/null 2>&1 || brew install icu4c pkg-config - echo "PKG_CONFIG_PATH=$(brew --prefix icu4c)/lib/pkgconfig" >> "$GITHUB_ENV" - fi - - - name: Build kapi - shell: bash - working-directory: neokapi - run: | - go run ./core/i18n/gen/catalogs - cd kapi - go build -trimpath -tags fts5 -o "${RUNNER_TEMP}/kapi-branch/kapi" ./cmd/kapi - "${RUNNER_TEMP}/kapi-branch/kapi" version - - - uses: actions/upload-artifact@v7 - with: - name: kapi-3804-${{ matrix.os }} - path: ${{ runner.temp }}/kapi-branch - retention-days: 1 - - e2e-cold: - name: "E2E: cold run (${{ matrix.os }})" - needs: e2e-build-kapi - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - os: [ubuntu-latest, macos-latest] - steps: - - uses: actions/checkout@v6 - - - uses: actions/download-artifact@v8 - with: - name: kapi-3804-${{ matrix.os }} - path: ${{ runner.temp }}/kapi-branch - - - name: Setup kapi - uses: ./ - with: - token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} - # Not a version any other job caches under, so the key is this pair's own. - version: 1.2.0 - plugins: "" - project-dir: test/fixture - - - name: Put the #3804 build first on PATH - shell: bash - run: | - chmod +x "${RUNNER_TEMP}/kapi-branch/kapi" - echo "${RUNNER_TEMP}/kapi-branch" >> "$GITHUB_PATH" - - - name: Cold run - shell: bash - run: test/parse-cache/branch-e2e.sh cold "${RUNNER_TEMP}/e2e/cold" - - - uses: actions/upload-artifact@v7 - with: - name: e2e-cold-${{ matrix.os }} - path: ${{ runner.temp }}/e2e/cold - retention-days: 1 - - e2e-warm: - name: "E2E: warm run (${{ matrix.os }})" - needs: e2e-cold - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - os: [ubuntu-latest, macos-latest] - steps: - - uses: actions/checkout@v6 - - - uses: actions/download-artifact@v8 - with: - name: kapi-3804-${{ matrix.os }} - path: ${{ runner.temp }}/kapi-branch - - - uses: actions/download-artifact@v8 - with: - name: e2e-cold-${{ matrix.os }} - path: ${{ runner.temp }}/e2e/cold - - - name: Install strace - if: runner.os == 'Linux' - shell: bash - run: sudo apt-get install -y -q strace - - - name: Setup kapi - uses: ./ - with: - token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} - # Not a version any other job caches under, so the key is this pair's own. - version: 1.2.0 - plugins: "" - project-dir: test/fixture - - - name: Put the #3804 build first on PATH - shell: bash - run: | - chmod +x "${RUNNER_TEMP}/kapi-branch/kapi" - echo "${RUNNER_TEMP}/kapi-branch" >> "$GITHUB_PATH" - - - name: Warm run - shell: bash - run: test/parse-cache/branch-e2e.sh warm "${RUNNER_TEMP}/e2e/cold" "${RUNNER_TEMP}/e2e/warm" - - e2e-refuse: - name: "E2E: refused (${{ matrix.case }}, ${{ matrix.os }})" - needs: e2e-build-kapi - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - os: [ubuntu-latest, macos-latest] - case: [inside-project, wider] - steps: - - uses: actions/checkout@v6 - - - uses: actions/download-artifact@v8 - with: - name: kapi-3804-${{ matrix.os }} - path: ${{ runner.temp }}/kapi-branch - - - name: Set a parse cache directory inside the project - if: matrix.case == 'inside-project' - shell: bash - run: echo "KAPI_PARSE_CACHE_DIR=${GITHUB_WORKSPACE}/test/fixture/.kapi-parse" >> "$GITHUB_ENV" - - - name: Setup kapi - uses: ./ - with: - token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} - version: 1.4.0-rc5 - plugins: "" - project-dir: test/fixture - - - name: Put the #3804 build first on PATH - shell: bash - run: | - chmod +x "${RUNNER_TEMP}/kapi-branch/kapi" - echo "${RUNNER_TEMP}/kapi-branch" >> "$GITHUB_PATH" - - - name: Make the directory wider than kapi accepts - if: matrix.case == 'wider' - shell: bash - run: | - # A restored cache may already be there; widen it either way. - mkdir -p "${KAPI_PARSE_CACHE_DIR}" - chmod 755 "${KAPI_PARSE_CACHE_DIR}" - - - name: Every kapi command fails with kapi's message - shell: bash - run: test/parse-cache/branch-e2e.sh refuse "${RUNNER_TEMP}/e2e/refuse" - - # Leave nothing for the post step to save: a widened directory saved - # into the cache would be restored, and refused, by every later run. - - name: Remove the refused directory - if: always() - shell: bash - run: rm -rf "${KAPI_PARSE_CACHE_DIR}" diff --git a/README.md b/README.md index 308547e..e0a4c71 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,7 @@ The binary is cached keyed on version + OS + arch. Plugins are cached keyed on t kapi records how it parsed each source and target file, so a later run can replay a file instead of parsing it again. With `parse-cache` on and a recipe in `project-dir`, the action restores that parse cache before your steps run, and `actions/cache` saves it again when the job ends. -The action sets `KAPI_PARSE_CACHE_DIR` to a directory under `runner.temp` for the rest of the job, and caches that directory together with `/.kapi/work/cache/docs`. A kapi that reads the variable keeps its parse cache there; kapi 1.3 and earlier keep it in the checkout. If your workflow sets `KAPI_PARSE_CACHE_DIR` itself, the action keeps your value and caches it when it lies under `runner.temp`. On another CI system, set `KAPI_PARSE_CACHE_DIR` to a fixed absolute directory outside the checkout and cache that directory. Let kapi create it: kapi makes it readable by your user only, and refuses a directory that others can read or write. +The action sets `KAPI_PARSE_CACHE_DIR` to a directory under `runner.temp` for the rest of the job, and caches that directory together with `/.kapi/work/cache/docs`. A kapi that reads the variable keeps its parse cache there; kapi 1.3 and earlier keep it in the checkout. If your workflow sets `KAPI_PARSE_CACHE_DIR` itself, the action keeps your value and caches it when it lies under `runner.temp`. On another CI system, set `KAPI_PARSE_CACHE_DIR` to a fixed absolute directory outside the project and cache that directory. Let kapi create it, which it does with access for your user only. kapi stops with an error when the directory is inside the project, is a link, belongs to another user, or gives group or other users any access. A restored parse cache does not change any result. kapi keys each entry by the file's path and content hash, the parse configuration, the recipe and the kapi build, and parses the file again when any of them differs. The test workflow checks this on every change: with the cache restored, `kapi status`, `kapi check`, `kapi check --ship` and `kapi up` must match a cold run exactly, in exit codes, output and every file written, and they must still match after the source changes. diff --git a/test/parse-cache/branch-e2e.sh b/test/parse-cache/branch-e2e.sh deleted file mode 100755 index 1650942..0000000 --- a/test/parse-cache/branch-e2e.sh +++ /dev/null @@ -1,169 +0,0 @@ -#!/usr/bin/env bash -# TEMPORARY: cold and warm runs of a kapi that reads KAPI_PARSE_CACHE_DIR, -# through setup-kapi. Removed before merging. -# -# branch-e2e.sh cold -# branch-e2e.sh warm -# branch-e2e.sh refuse (expects every kapi command to fail) -# -# The action has already set KAPI_PARSE_CACHE_DIR; kapi on PATH is the build -# under test; the project is test/fixture. -set -euo pipefail - -here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -root="$(cd "${here}/../.." && pwd)" -mode="$1" -shift - -pc="${KAPI_PARSE_CACHE_DIR:?KAPI_PARSE_CACHE_DIR is not set}" - -# These jobs install release 1.4.0-rc5 and then run the #3804 build, so the -# cache key names 1.4.0-rc5 and the restore can bring the in-tree cache another -# job saved with the real 1.4.0-rc5. Remove it, so the check below sees only -# what this build writes. -if [ -e test/fixture/.kapi/work ]; then - echo "note: removed test/fixture/.kapi/work, restored from a 1.4.0-rc5 in-tree cache" - rm -rf test/fixture/.kapi/work -fi - -mode_of() { - if stat -c '%a' "$1" > /dev/null 2>&1; then stat -c '%a %U' "$1"; else stat -f '%Lp %Su' "$1"; fi -} - -# snapshot DIR: each file under DIR with its digest, and the index rows. -snapshot() { - (cd "$1" && find . -type f ! -name 'index.db*' | LC_ALL=C sort | while read -r f; do - printf '%s %s\n' "$(shasum -a 256 < "$f" | cut -d' ' -f1)" "$f" - done) - python3 - "$1/index.db" <<'PY' -import sqlite3, sys -db = sqlite3.connect(sys.argv[1]) -for row in db.execute("select path, build, config_key, content_hash, parts_ref, parts_digest from documents order by path, config_key"): - print("row", *row) -PY -} - -# normalize RECORD: drop timestamps that differ between any two runs. -normalize() { - local out="$1.norm" - mkdir -p "$out" - for f in "$1"/*.json; do - jq -c 'del(.. | .at?)' "$f" > "$out/$(basename "$f")" 2> /dev/null || cp "$f" "$out/" - done - cp "$1"/*.rc "$1/tree.txt" "$out/" -} - -check_layout() { - local results="$1" - kapi version | tee "${results}/version.txt" - kapi state path -p test/fixture --json | tee "${results}/state-path.json" - local state got - state="$(jq -r .path "${results}/state-path.json")" - got="$(jq -r .parse_cache "${results}/state-path.json")" - [ "${got}" = "${pc}" ] || { echo "::error::kapi resolved the parse cache to ${got}, not ${pc}"; exit 1; } - [ ! -e "${state}/work/cache/docs" ] || { echo "::error::the state directory holds a parse cache"; exit 1; } - [ ! -e test/fixture/.kapi/work ] || { echo "::error::the checkout holds .kapi/work"; exit 1; } - echo "parse cache directory: ${pc} ($(mode_of "${pc}"))" | tee -a "${results}/summary.txt" - [ "$(mode_of "${pc}" | cut -d' ' -f1)" = 700 ] || { echo "::error::the parse cache directory is not 0700"; exit 1; } - find "${pc}" -type f | sed "s|${pc}||" | LC_ALL=C sort | tee "${results}/pc-files.txt" -} - -case "${mode}" in - cold) - results="$1" - mkdir -p "${results}" - # An earlier run of this workflow may have left a cache under this key. - if [ -e "${pc}" ]; then - echo "note: removed ${pc}, restored from an earlier run, so this run starts cold" - rm -rf "${pc}" - fi - "${root}/test/state-cache/run.sh" test/fixture "${results}/run" - check_layout "${results}" - snapshot "${pc}" > "${results}/snapshot.txt" - echo "entries: $(grep -c '^row ' "${results}/snapshot.txt"), part files: $(grep -vc '^row ' "${results}/snapshot.txt")" | tee -a "${results}/summary.txt" - ;; - - warm) - cold="$1" - results="$2" - mkdir -p "${results}" - # What the restore brought: only the parse cache directory. - [ -s "${pc}/index.db" ] || { echo "::error::no parse cache was restored into ${pc}"; exit 1; } - echo "restored: ${pc} ($(mode_of "${pc}"))" | tee -a "${results}/summary.txt" - [ "$(mode_of "${pc}" | cut -d' ' -f1)" = 700 ] || { echo "::error::the restored directory is not 0700"; exit 1; } - [ ! -e test/fixture/.kapi/work ] || { echo "::error::the restore wrote into the checkout"; exit 1; } - snapshot "${pc}" > "${results}/before.txt" - diff -u "${cold}/snapshot.txt" "${results}/before.txt" - - if command -v strace > /dev/null; then - strace -f -qq -e trace=openat -o "${results}/openat.txt" \ - "${root}/test/state-cache/run.sh" test/fixture "${results}/run" - # kapi opens a part file relative to the cache directory's descriptor, - # so the traced name is the bare file name. - opened="$(grep -cE 'openat2?\(.*"([^"]*/)?parts-[0-9a-f]+\.log".*O_RDONLY' "${results}/openat.txt" || true)" - echo "the warm run opened recorded part files for reading ${opened} times; examples:" | tee -a "${results}/summary.txt" - grep -E 'openat2?\(.*"([^"]*/)?parts-[0-9a-f]+\.log".*O_RDONLY' "${results}/openat.txt" | head -n 3 | tee -a "${results}/summary.txt" || true - grep -E 'parts-|index\.db' "${results}/openat.txt" | head -n 5 || true - [ "${opened}" -gt 0 ] || { echo "::error::the warm run read no recorded part file"; exit 1; } - else - "${root}/test/state-cache/run.sh" test/fixture "${results}/run" - fi - check_layout "${results}" - snapshot "${pc}" > "${results}/after.txt" - python3 - "${pc}/index.db" <<'PY' | tee -a "${results}/summary.txt" -import sqlite3, sys -for path, checkout in sqlite3.connect(sys.argv[1]).execute("select path, checkout from documents order by path"): - print(f"entry {path}: recorded for checkout {checkout}") -PY - if diff -u "${results}/before.txt" "${results}/after.txt"; then - echo "the warm run wrote no entry: every file it parsed came from the cache" | tee -a "${results}/summary.txt" - else - echo "::error::the warm run changed the parse cache" - exit 1 - fi - - # A cold run of a fresh copy with its own empty parse cache and data root. - "${root}/test/state-cache/cold-copy.sh" "${RUNNER_TEMP}/cold" - (cd "${RUNNER_TEMP}/cold" && KAPI_PARSE_CACHE_DIR="${RUNNER_TEMP}/cold-parse" KAPI_DATA_DIR="${RUNNER_TEMP}/cold-data" \ - "${root}/test/state-cache/run.sh" test/fixture "${results}/cold-here") - normalize "${results}/run" - normalize "${results}/cold-here" - normalize "${cold}/run" - "${root}/test/state-cache/compare.sh" "${results}/cold-here.norm" "${results}/run.norm" - "${root}/test/state-cache/compare.sh" "${cold}/run.norm" "${results}/run.norm" - - # Control: with one recorded part file damaged, kapi must notice and parse - # that file again, which shows the warm run's entries were read and checked. - ctl="${RUNNER_TEMP}/control-parse" - rm -rf "${ctl}" - cp -Rp "${pc}" "${ctl}" - chmod 700 "${ctl}" - # The entry for the source file every command reads; one byte changed. - victim="$(python3 -c 'import sqlite3,sys; print(sqlite3.connect(sys.argv[1]).execute("select parts_ref from documents where path = ?", ("locales/en.json",)).fetchone()[0])' "${ctl}/index.db")" - python3 -c 'import sys; p=sys.argv[1]; b=bytearray(open(p,"rb").read()); i=len(b)//2; b[i] = ord("Z") if b[i] != ord("Z") else ord("Y"); open(p,"wb").write(b)' "${ctl}/${victim}" - snapshot "${ctl}" > "${results}/control-before.txt" - "${root}/test/state-cache/cold-copy.sh" "${RUNNER_TEMP}/control" - (cd "${RUNNER_TEMP}/control" && KAPI_PARSE_CACHE_DIR="${ctl}" KAPI_DATA_DIR="${RUNNER_TEMP}/control-data" \ - kapi status -p test/fixture/kapi.yaml --json > /dev/null) - snapshot "${ctl}" > "${results}/control-after.txt" - if diff -q "${results}/control-before.txt" "${results}/control-after.txt" > /dev/null; then - echo "::error::kapi accepted a damaged part file" - exit 1 - fi - echo "control: with ${victim} damaged, kapi parsed that file again:" | tee -a "${results}/summary.txt" - diff "${results}/control-before.txt" "${results}/control-after.txt" | tee -a "${results}/summary.txt" || true - ;; - - refuse) - results="$1" - mkdir -p "${results}" - for cmd in "status -p test/fixture/kapi.yaml" "up -p test/fixture/kapi.yaml --json"; do - rc=0 - # shellcheck disable=SC2086 # the command is a word list - kapi ${cmd} > "${results}/out.txt" 2>&1 || rc=$? - echo "kapi ${cmd}: exit ${rc}" | tee -a "${results}/summary.txt" - sed 's/^/ | /' "${results}/out.txt" | tail -n 5 | tee -a "${results}/summary.txt" - [ "${rc}" -ne 0 ] || { echo "::error::kapi ${cmd} accepted the parse cache directory"; exit 1; } - done - ;; -esac From 3cdec4b66c1a97912eeb791599128a916d3c1b06 Mon Sep 17 00:00:00 2001 From: Asgeir Frimannsson Date: Mon, 23 Nov 2026 20:11:53 +0100 Subject: [PATCH 16/20] Say outside the repository, and how to clear a refused parse cache Verified: none (README only; actions/cache@v5 runs its save step under post-if: success()) Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01GtGobCdyR3d9yLXAfX9jsr --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e0a4c71..4911de2 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,7 @@ The binary is cached keyed on version + OS + arch. Plugins are cached keyed on t kapi records how it parsed each source and target file, so a later run can replay a file instead of parsing it again. With `parse-cache` on and a recipe in `project-dir`, the action restores that parse cache before your steps run, and `actions/cache` saves it again when the job ends. -The action sets `KAPI_PARSE_CACHE_DIR` to a directory under `runner.temp` for the rest of the job, and caches that directory together with `/.kapi/work/cache/docs`. A kapi that reads the variable keeps its parse cache there; kapi 1.3 and earlier keep it in the checkout. If your workflow sets `KAPI_PARSE_CACHE_DIR` itself, the action keeps your value and caches it when it lies under `runner.temp`. On another CI system, set `KAPI_PARSE_CACHE_DIR` to a fixed absolute directory outside the project and cache that directory. Let kapi create it, which it does with access for your user only. kapi stops with an error when the directory is inside the project, is a link, belongs to another user, or gives group or other users any access. +The action sets `KAPI_PARSE_CACHE_DIR` to a directory under `runner.temp` for the rest of the job, and caches that directory together with `/.kapi/work/cache/docs`. A kapi that reads the variable keeps its parse cache there; kapi 1.3 and earlier keep it in the checkout. If your workflow sets `KAPI_PARSE_CACHE_DIR` itself, the action keeps your value and caches it when it lies under `runner.temp`. On another CI system, set `KAPI_PARSE_CACHE_DIR` to a fixed absolute directory outside the repository and cache that directory. Let kapi create it, which it does with access for your user only. kapi stops with an error when the directory is inside the repository, is a link, belongs to another user, or gives group or other users any access. A restored parse cache does not change any result. kapi keys each entry by the file's path and content hash, the parse configuration, the recipe and the kapi build, and parses the file again when any of them differs. The test workflow checks this on every change: with the cache restored, `kapi status`, `kapi check`, `kapi check --ship` and `kapi up` must match a cold run exactly, in exit codes, output and every file written, and they must still match after the source changes. @@ -123,6 +123,8 @@ The action carries the parse cache and nothing else. The project store holds thi Each run saves its own cache, keyed by the runner OS, the resolved kapi version, the ref, the job and the run. A run restores the newest cache for the same kapi version, from its own ref first. The key does not change with your source files: a cache from before a source edit still holds the entries for every file the edit left alone. A new kapi version starts from an empty cache. How much time the cache saves depends on the formats: container formats such as DOCX replay faster than they parse, while JSON and Markdown parse about as fast as they replay. +If kapi stops with `KAPI_PARSE_CACHE_DIR is refused`, the directory it names is one kapi will not use, for example because something made it readable by other users; `actions/cache` saves only from a job that succeeds, so such a directory reaches the cache only when a workflow changes it after kapi's last run or lets a failed kapi step pass, and deleting the repository's parse cache entries (`gh cache delete --all`, or each `kapi-parse-` key) clears it. + kapi trusts what it finds in its parse cache, so who can write the cache decides whether a restored entry can be trusted. GitHub scopes caches by branch: a run restores only caches saved on its own branch, on the default branch, or, for a pull request, on its base branch. A cache saved by a pull request, including one from a fork, is never restored on the default branch. A `pull_request_target` run works in the base branch's scope, so there the action restores the parse cache and does not save it. Any other workflow that runs untrusted code with this action can write to the cache of the branch it runs on; set `parse-cache: false` in such a workflow. ## Platform support From b03738e97478abb64c81ad016744df4caeb6c2a6 Mon Sep 17 00:00:00 2001 From: Asgeir Frimannsson Date: Mon, 23 Nov 2026 20:24:36 +0100 Subject: [PATCH 17/20] Refuse a line break or bare runner.temp in KAPI_PARSE_CACHE_DIR; harden other output writes - A caller's KAPI_PARSE_CACHE_DIR with a control character, or naming runner.temp itself, is not cached; dir= is written only after the checks. - The version must look like a release version; parse-cache's output is true or false; the auth values go to GITHUB_ENV with a random delimiter. - workflow_run and issue_comment runs restore the parse cache and never save. - README: repository wording, scoped test claim, gh cache commands for the kapi-parse- prefix only, kapi up example without a pin, no em dash. Verified: test/parse-cache/env.sh (newline, CR, runner.temp itself and with / and /., nested, auth token with a line break); the resolve step on a version with a line break; actionlint; shellcheck Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01GtGobCdyR3d9yLXAfX9jsr --- README.md | 16 +++++---- action.yml | 72 +++++++++++++++++++++++++++++------------ test/parse-cache/env.sh | 25 ++++++++++++++ 3 files changed, 86 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 4911de2..93ad123 100644 --- a/README.md +++ b/README.md @@ -42,15 +42,12 @@ needs credentials: steps: - uses: neokapi/setup-kapi@v1 with: - version: "1.2.0-rc32" auth-token: ${{ secrets.BOWRAIN_AUTH_TOKEN }} server: https://your.bowrain.server - run: kapi up ``` -The newest stable release, which `latest` installs, has no `kapi up`, so this example pins the prerelease that has it. - `kapi up` runs the kapi loop: with the bowrain plugin installed and a `server:` block in the recipe, it pushes, catches up on the server, and pulls the produced targets back. To run it and commit the results, pair this with [`kapi-action`](https://github.com/neokapi/kapi-action). ## Inputs @@ -76,7 +73,7 @@ The newest stable release, which `latest` installs, has no `kapi up`, so this ex ### With the Okapi bridge (Java plugin) The `okapi-bridge` plugin runs Okapi Framework filters as a Java subprocess, -so the runner needs a JVM — Java 11+, or Java 17+ for Okapi 1.48.0 and later. +so the runner needs a JVM: Java 11+, or Java 17+ for Okapi 1.48.0 and later. GitHub's hosted Ubuntu runners ship a default JDK; pin one explicitly with `setup-java` when you need a specific version: @@ -117,15 +114,20 @@ kapi records how it parsed each source and target file, so a later run can repla The action sets `KAPI_PARSE_CACHE_DIR` to a directory under `runner.temp` for the rest of the job, and caches that directory together with `/.kapi/work/cache/docs`. A kapi that reads the variable keeps its parse cache there; kapi 1.3 and earlier keep it in the checkout. If your workflow sets `KAPI_PARSE_CACHE_DIR` itself, the action keeps your value and caches it when it lies under `runner.temp`. On another CI system, set `KAPI_PARSE_CACHE_DIR` to a fixed absolute directory outside the repository and cache that directory. Let kapi create it, which it does with access for your user only. kapi stops with an error when the directory is inside the repository, is a link, belongs to another user, or gives group or other users any access. -A restored parse cache does not change any result. kapi keys each entry by the file's path and content hash, the parse configuration, the recipe and the kapi build, and parses the file again when any of them differs. The test workflow checks this on every change: with the cache restored, `kapi status`, `kapi check`, `kapi check --ship` and `kapi up` must match a cold run exactly, in exit codes, output and every file written, and they must still match after the source changes. +A restored parse cache does not change any result. kapi keys each entry by the file's path and content hash, the parse configuration, the recipe and the kapi build, and parses the file again when any of them differs. The test workflow checks this on every change for the parse cache kept in the checkout (kapi 1.2): with the cache restored, `kapi status`, `kapi check`, `kapi check --ship` and `kapi up` must match a cold run exactly, in exit codes, output and every file written, and they must still match after the source changes. The action carries the parse cache and nothing else. The project store holds this checkout's blocks and the targets a run wrote, and a restored copy changes what `kapi status`, `kapi check --ship` and `kapi up` report. The extraction batches, the redaction vault and the sync state (with its claim token) belong to the checkout that wrote them. The project's context (its terms, voice profiles, content memory and recorded decisions) lives in kapi's workspace and moves between machines through a context backend (kapi 1.3 and later). A job that needs it runs `kapi context pull` before kapi works and `kapi context push` after a run records something. [`kapi-action`](https://github.com/neokapi/kapi-action)'s `context-sync` input runs both around its command, or your workflow can run them as steps of its own. Each run saves its own cache, keyed by the runner OS, the resolved kapi version, the ref, the job and the run. A run restores the newest cache for the same kapi version, from its own ref first. The key does not change with your source files: a cache from before a source edit still holds the entries for every file the edit left alone. A new kapi version starts from an empty cache. How much time the cache saves depends on the formats: container formats such as DOCX replay faster than they parse, while JSON and Markdown parse about as fast as they replay. -If kapi stops with `KAPI_PARSE_CACHE_DIR is refused`, the directory it names is one kapi will not use, for example because something made it readable by other users; `actions/cache` saves only from a job that succeeds, so such a directory reaches the cache only when a workflow changes it after kapi's last run or lets a failed kapi step pass, and deleting the repository's parse cache entries (`gh cache delete --all`, or each `kapi-parse-` key) clears it. +If kapi stops with `KAPI_PARSE_CACHE_DIR is refused`, the directory it names is one kapi will not use, for example because something made it readable by other users. `actions/cache` saves only from a job that succeeds, so such a directory reaches the cache only when a workflow changes it after kapi's last run or lets a failed kapi step pass. To clear it, list the repository's parse cache entries and delete them: + +```bash +gh cache list --key kapi-parse- --limit 1000 +gh cache list --key kapi-parse- --limit 1000 --json id --jq '.[].id' | xargs -n 1 gh cache delete +``` -kapi trusts what it finds in its parse cache, so who can write the cache decides whether a restored entry can be trusted. GitHub scopes caches by branch: a run restores only caches saved on its own branch, on the default branch, or, for a pull request, on its base branch. A cache saved by a pull request, including one from a fork, is never restored on the default branch. A `pull_request_target` run works in the base branch's scope, so there the action restores the parse cache and does not save it. Any other workflow that runs untrusted code with this action can write to the cache of the branch it runs on; set `parse-cache: false` in such a workflow. +kapi trusts what it finds in its parse cache, so who can write the cache decides whether a restored entry can be trusted. GitHub scopes caches by branch: a run restores only caches saved on its own branch, on the default branch, or, for a pull request, on its base branch. A cache saved by a pull request, including one from a fork, is never restored on the default branch. A `pull_request_target`, `workflow_run` or `issue_comment` run works in the default or base branch's scope, often for someone who cannot push, so in those runs the action restores the parse cache and does not save it. Any other workflow that runs untrusted code with this action can write to the cache of the branch it runs on; set `parse-cache: false` in such a workflow. ## Platform support diff --git a/action.yml b/action.yml index 68eee64..0a38b57 100644 --- a/action.yml +++ b/action.yml @@ -74,6 +74,14 @@ runs: ACTION_PATH: ${{ github.action_path }} run: | VERSION=$("${ACTION_PATH}/scripts/resolve-version.sh" "${INPUT_VERSION}") + # The version names a release tag and becomes an output and a cache + # key, so it may hold only the characters a release version has. + case "${VERSION}" in + "" | *[!0-9A-Za-z.+-]*) + echo "::error title=setup-kapi::The version input is not a release version." + exit 1 + ;; + esac echo "version=${VERSION}" >> "$GITHUB_OUTPUT" echo "Resolved version: ${VERSION}" @@ -138,9 +146,16 @@ runs: INPUT_AUTH_TOKEN: ${{ inputs.auth-token }} INPUT_SERVER: ${{ inputs.server }} run: | - echo "BOWRAIN_AUTH_TOKEN=${INPUT_AUTH_TOKEN}" >> "$GITHUB_ENV" + # The multi-line form with a random delimiter, so a line break in a + # value cannot start another variable. + put_env() { + local delim + delim="SETUP_KAPI_EOF_$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')" + printf '%s<<%s\n%s\n%s\n' "$1" "${delim}" "$2" "${delim}" >> "$GITHUB_ENV" + } + put_env BOWRAIN_AUTH_TOKEN "${INPUT_AUTH_TOKEN}" if [ -n "${INPUT_SERVER}" ]; then - echo "BOWRAIN_SERVER_URL=${INPUT_SERVER}" >> "$GITHUB_ENV" + put_env BOWRAIN_SERVER_URL "${INPUT_SERVER}" fi echo "Auth configured for kapi" @@ -215,7 +230,13 @@ runs: echo "::warning title=setup-kapi::The cache-tm input is deprecated; use parse-cache instead." ENABLED="${INPUT_CACHE_TM}" fi - echo "enabled=${ENABLED}" >> "$GITHUB_OUTPUT" + # Only 'false' turns the cache off, as before; write nothing else of + # the input to the output. + if [ "${ENABLED}" = "false" ]; then + echo "enabled=false" >> "$GITHUB_OUTPUT" + else + echo "enabled=true" >> "$GITHUB_OUTPUT" + fi # 12. Detect a kapi project: a committed kapi.yaml recipe (or legacy *.kapi). # The parse cache is carried only for a project. The recipe is the @@ -263,22 +284,32 @@ runs: exit 0 fi dir="${KAPI_PARSE_CACHE_DIR}" - case "/${dir//\\//}/" in - */../*) - echo "::warning title=setup-kapi::KAPI_PARSE_CACHE_DIR (${dir}) has a '..' component, so the parse cache is not carried." - exit 0 - ;; + skip() { + echo "::warning title=setup-kapi::KAPI_PARSE_CACHE_DIR $1, so the parse cache is not carried." + exit 0 + } + case "${dir}" in + *[[:cntrl:]]*) skip "contains a control character" ;; esac base="${RUNNER_TEMP%[/\\]}" - if [ -n "${base}" ]; then - case "${dir}" in - "${base}"/* | "${base}"\\*) - echo "dir=${dir}" >> "$GITHUB_OUTPUT" - exit 0 - ;; + [ -n "${base}" ] || skip "cannot be checked, because RUNNER_TEMP is not set" + case "${dir}" in + "${base}"/* | "${base}"\\*) rel="${dir:${#base}+1}" ;; + *) skip "(${dir}) is not under runner.temp" ;; + esac + # Count the components below runner.temp, ignoring empty ones and + # '.'; a '..' refuses the value outright. + IFS='/\' read -ra parts <<< "${rel}" + depth=0 + for part in ${parts[@]+"${parts[@]}"}; do + case "${part}" in + "" | .) ;; + ..) skip "(${dir}) has a '..' component" ;; + *) depth=$((depth + 1)) ;; esac - fi - echo "::warning title=setup-kapi::KAPI_PARSE_CACHE_DIR (${dir}) is not under runner.temp, so the parse cache is not carried." + done + [ "${depth}" -gt 0 ] || skip "(${dir}) names runner.temp itself" + echo "dir=${dir}" >> "$GITHUB_OUTPUT" # 14. Restore kapi's parse cache, and save it at job end. The key is unique # to the job and run attempt, so every run saves its own cache through @@ -306,10 +337,11 @@ runs: # and those of the default branch (and a pull request's base), never # another branch's or a pull request's, so a pull request from a fork # cannot reach the default branch's runs through this cache. A - # pull_request_target run executes in the base branch's scope, so there - # the cache is restored and never saved. + # pull_request_target, workflow_run or issue_comment run executes in + # the default (or base) branch's scope and often on behalf of someone + # who cannot push, so there the cache is restored and never saved. - name: Restore kapi parse cache - if: steps.parse-cache-dir.outputs.dir != '' && github.event_name != 'pull_request_target' + if: steps.parse-cache-dir.outputs.dir != '' && !contains(fromJSON('["pull_request_target","workflow_run","issue_comment"]'), github.event_name) uses: actions/cache@v5 with: path: | @@ -321,7 +353,7 @@ runs: kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}- - name: Restore kapi parse cache (no save) - if: steps.parse-cache-dir.outputs.dir != '' && github.event_name == 'pull_request_target' + if: steps.parse-cache-dir.outputs.dir != '' && contains(fromJSON('["pull_request_target","workflow_run","issue_comment"]'), github.event_name) uses: actions/cache/restore@v5 with: path: | diff --git a/test/parse-cache/env.sh b/test/parse-cache/env.sh index bc20762..29a8ba2 100755 --- a/test/parse-cache/env.sh +++ b/test/parse-cache/env.sh @@ -59,6 +59,9 @@ run_case() { else ! grep -q '^::warning' "${dir}/stdout" || fail "${name}" "unexpected warning: $(cat "${dir}/stdout")" fi + if [ -z "${want_dir}" ] && [ -s "${dir}/output" ]; then + fail "${name}" "wrote outputs: $(cat "${dir}/output")" + fi [ ! -e "${temp}" ] || fail "${name}" "created a directory" if [ "${failures}" = "${before}" ]; then echo "ok ${name}" @@ -72,6 +75,28 @@ run_case preset-outside "/home/runner/.cache/kapi" "" "" yes run_case preset-relative "kapi-parse-cache" "" "" yes run_case preset-dotdot "${temp}/../elsewhere" "" "" yes run_case preset-prefix "${temp}-other/cache" "" "" yes +run_case preset-newline "${temp}/x +dir=/etc/ssl" "" "" yes +run_case preset-cr "${temp}/x$(printf '\r')" "" "" yes +run_case preset-temp-itself "${temp}" "" "" yes +run_case preset-temp-slash "${temp}/" "" "" yes +run_case preset-temp-dot "${temp}/." "" "" yes +run_case preset-temp-dots "${temp}/./" "" "" yes +run_case preset-temp-nested "${temp}/a/./b" "${temp}/a/./b" "" no + +# The auth step writes each value with a random delimiter, so a line break in +# a token cannot add another variable. +yq '.runs.steps[] | select(.name == "Configure auth") | .run' "${root}/action.yml" > "${work}/auth.sh" +: > "${work}/auth.env" +env INPUT_AUTH_TOKEN="tok +BASH_ENV=/tmp/evil" INPUT_SERVER="https://example.invalid" GITHUB_ENV="${work}/auth.env" \ + bash -e "${work}/auth.sh" > /dev/null +names="$(awk 'd != "" { if ($0 == d) d = ""; next } /< Date: Mon, 23 Nov 2026 20:24:47 +0100 Subject: [PATCH 18/20] Add temporary cold and warm runs against neokapi main with #3804 Verified: none (a measurement job, removed before merge) Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01GtGobCdyR3d9yLXAfX9jsr --- .github/workflows/test.yml | 192 +++++++++++++++++++++++++++++++++ test/parse-cache/branch-e2e.sh | 169 +++++++++++++++++++++++++++++ 2 files changed, 361 insertions(+) create mode 100755 test/parse-cache/branch-e2e.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7dacc0b..34f1e7f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -507,3 +507,195 @@ jobs: - name: Verify bowrain matches a fresh install shell: bash run: test/plugins/matches-fresh.sh bowrain + + # TEMPORARY: cold and warm runs of the kapi build from neokapi/neokapi main (24b78f1, with #3804) + # through this action. Removed before merging. + e2e-build-kapi: + name: "E2E: build kapi main 24b78f1 (${{ matrix.os }})" + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest] + steps: + - uses: actions/checkout@v6 + with: + repository: neokapi/neokapi + ref: 24b78f17467f0ff31aaec2429d5417c2c45139d2 + path: neokapi + + - uses: actions/setup-go@v6 + with: + go-version-file: neokapi/go.mod + cache-dependency-path: neokapi/**/go.sum + + - name: Install ICU + shell: bash + run: | + if [ "$RUNNER_OS" = Linux ]; then + sudo apt-get update -q && sudo apt-get install -y -q libicu-dev pkg-config + else + brew list icu4c > /dev/null 2>&1 || brew install icu4c pkg-config + echo "PKG_CONFIG_PATH=$(brew --prefix icu4c)/lib/pkgconfig" >> "$GITHUB_ENV" + fi + + - name: Build kapi + shell: bash + working-directory: neokapi + run: | + go run ./core/i18n/gen/catalogs + cd kapi + go build -trimpath -tags fts5 -o "${RUNNER_TEMP}/kapi-branch/kapi" ./cmd/kapi + "${RUNNER_TEMP}/kapi-branch/kapi" version + + - uses: actions/upload-artifact@v7 + with: + name: kapi-3804-${{ matrix.os }} + path: ${{ runner.temp }}/kapi-branch + retention-days: 1 + + e2e-cold: + name: "E2E: cold run (${{ matrix.os }})" + needs: e2e-build-kapi + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest] + steps: + - uses: actions/checkout@v6 + + - uses: actions/download-artifact@v8 + with: + name: kapi-3804-${{ matrix.os }} + path: ${{ runner.temp }}/kapi-branch + + - name: Setup kapi + uses: ./ + with: + token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} + # Not a version any other job caches under, so the key is this pair's own. + version: 1.2.0 + plugins: "" + project-dir: test/fixture + + - name: Put the main 24b78f1 build first on PATH + shell: bash + run: | + chmod +x "${RUNNER_TEMP}/kapi-branch/kapi" + echo "${RUNNER_TEMP}/kapi-branch" >> "$GITHUB_PATH" + + - name: Cold run + shell: bash + run: test/parse-cache/branch-e2e.sh cold "${RUNNER_TEMP}/e2e/cold" + + - uses: actions/upload-artifact@v7 + with: + name: e2e-cold-${{ matrix.os }} + path: ${{ runner.temp }}/e2e/cold + retention-days: 1 + + e2e-warm: + name: "E2E: warm run (${{ matrix.os }})" + needs: e2e-cold + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest] + steps: + - uses: actions/checkout@v6 + + - uses: actions/download-artifact@v8 + with: + name: kapi-3804-${{ matrix.os }} + path: ${{ runner.temp }}/kapi-branch + + - uses: actions/download-artifact@v8 + with: + name: e2e-cold-${{ matrix.os }} + path: ${{ runner.temp }}/e2e/cold + + - name: Install strace + if: runner.os == 'Linux' + shell: bash + run: sudo apt-get install -y -q strace + + - name: Setup kapi + uses: ./ + with: + token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} + # Not a version any other job caches under, so the key is this pair's own. + version: 1.2.0 + plugins: "" + project-dir: test/fixture + + - name: Put the main 24b78f1 build first on PATH + shell: bash + run: | + chmod +x "${RUNNER_TEMP}/kapi-branch/kapi" + echo "${RUNNER_TEMP}/kapi-branch" >> "$GITHUB_PATH" + + - name: Warm run + shell: bash + run: test/parse-cache/branch-e2e.sh warm "${RUNNER_TEMP}/e2e/cold" "${RUNNER_TEMP}/e2e/warm" + + e2e-refuse: + name: "E2E: refused (${{ matrix.case }}, ${{ matrix.os }})" + needs: e2e-build-kapi + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest] + case: [inside-project, inside-repository, wider] + steps: + - uses: actions/checkout@v6 + + - uses: actions/download-artifact@v8 + with: + name: kapi-3804-${{ matrix.os }} + path: ${{ runner.temp }}/kapi-branch + + - name: Set a parse cache directory inside the project + if: matrix.case == 'inside-project' + shell: bash + run: echo "KAPI_PARSE_CACHE_DIR=${GITHUB_WORKSPACE}/test/fixture/.kapi-parse" >> "$GITHUB_ENV" + + - name: Set a parse cache directory in the repository, outside the project + if: matrix.case == 'inside-repository' + shell: bash + run: echo "KAPI_PARSE_CACHE_DIR=${GITHUB_WORKSPACE}/.kapi-parse" >> "$GITHUB_ENV" + + - name: Setup kapi + uses: ./ + with: + token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} + version: 1.4.0-rc5 + plugins: "" + project-dir: test/fixture + + - name: Put the main 24b78f1 build first on PATH + shell: bash + run: | + chmod +x "${RUNNER_TEMP}/kapi-branch/kapi" + echo "${RUNNER_TEMP}/kapi-branch" >> "$GITHUB_PATH" + + - name: Make the directory wider than kapi accepts + if: matrix.case == 'wider' + shell: bash + run: | + # A restored cache may already be there; widen it either way. + mkdir -p "${KAPI_PARSE_CACHE_DIR}" + chmod 755 "${KAPI_PARSE_CACHE_DIR}" + + - name: Every kapi command fails with kapi's message + shell: bash + run: test/parse-cache/branch-e2e.sh refuse "${RUNNER_TEMP}/e2e/refuse" + + # Leave nothing for the post step to save: a widened directory saved + # into the cache would be restored, and refused, by every later run. + - name: Remove the refused directory + if: always() + shell: bash + run: rm -rf "${KAPI_PARSE_CACHE_DIR}" diff --git a/test/parse-cache/branch-e2e.sh b/test/parse-cache/branch-e2e.sh new file mode 100755 index 0000000..1650942 --- /dev/null +++ b/test/parse-cache/branch-e2e.sh @@ -0,0 +1,169 @@ +#!/usr/bin/env bash +# TEMPORARY: cold and warm runs of a kapi that reads KAPI_PARSE_CACHE_DIR, +# through setup-kapi. Removed before merging. +# +# branch-e2e.sh cold +# branch-e2e.sh warm +# branch-e2e.sh refuse (expects every kapi command to fail) +# +# The action has already set KAPI_PARSE_CACHE_DIR; kapi on PATH is the build +# under test; the project is test/fixture. +set -euo pipefail + +here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +root="$(cd "${here}/../.." && pwd)" +mode="$1" +shift + +pc="${KAPI_PARSE_CACHE_DIR:?KAPI_PARSE_CACHE_DIR is not set}" + +# These jobs install release 1.4.0-rc5 and then run the #3804 build, so the +# cache key names 1.4.0-rc5 and the restore can bring the in-tree cache another +# job saved with the real 1.4.0-rc5. Remove it, so the check below sees only +# what this build writes. +if [ -e test/fixture/.kapi/work ]; then + echo "note: removed test/fixture/.kapi/work, restored from a 1.4.0-rc5 in-tree cache" + rm -rf test/fixture/.kapi/work +fi + +mode_of() { + if stat -c '%a' "$1" > /dev/null 2>&1; then stat -c '%a %U' "$1"; else stat -f '%Lp %Su' "$1"; fi +} + +# snapshot DIR: each file under DIR with its digest, and the index rows. +snapshot() { + (cd "$1" && find . -type f ! -name 'index.db*' | LC_ALL=C sort | while read -r f; do + printf '%s %s\n' "$(shasum -a 256 < "$f" | cut -d' ' -f1)" "$f" + done) + python3 - "$1/index.db" <<'PY' +import sqlite3, sys +db = sqlite3.connect(sys.argv[1]) +for row in db.execute("select path, build, config_key, content_hash, parts_ref, parts_digest from documents order by path, config_key"): + print("row", *row) +PY +} + +# normalize RECORD: drop timestamps that differ between any two runs. +normalize() { + local out="$1.norm" + mkdir -p "$out" + for f in "$1"/*.json; do + jq -c 'del(.. | .at?)' "$f" > "$out/$(basename "$f")" 2> /dev/null || cp "$f" "$out/" + done + cp "$1"/*.rc "$1/tree.txt" "$out/" +} + +check_layout() { + local results="$1" + kapi version | tee "${results}/version.txt" + kapi state path -p test/fixture --json | tee "${results}/state-path.json" + local state got + state="$(jq -r .path "${results}/state-path.json")" + got="$(jq -r .parse_cache "${results}/state-path.json")" + [ "${got}" = "${pc}" ] || { echo "::error::kapi resolved the parse cache to ${got}, not ${pc}"; exit 1; } + [ ! -e "${state}/work/cache/docs" ] || { echo "::error::the state directory holds a parse cache"; exit 1; } + [ ! -e test/fixture/.kapi/work ] || { echo "::error::the checkout holds .kapi/work"; exit 1; } + echo "parse cache directory: ${pc} ($(mode_of "${pc}"))" | tee -a "${results}/summary.txt" + [ "$(mode_of "${pc}" | cut -d' ' -f1)" = 700 ] || { echo "::error::the parse cache directory is not 0700"; exit 1; } + find "${pc}" -type f | sed "s|${pc}||" | LC_ALL=C sort | tee "${results}/pc-files.txt" +} + +case "${mode}" in + cold) + results="$1" + mkdir -p "${results}" + # An earlier run of this workflow may have left a cache under this key. + if [ -e "${pc}" ]; then + echo "note: removed ${pc}, restored from an earlier run, so this run starts cold" + rm -rf "${pc}" + fi + "${root}/test/state-cache/run.sh" test/fixture "${results}/run" + check_layout "${results}" + snapshot "${pc}" > "${results}/snapshot.txt" + echo "entries: $(grep -c '^row ' "${results}/snapshot.txt"), part files: $(grep -vc '^row ' "${results}/snapshot.txt")" | tee -a "${results}/summary.txt" + ;; + + warm) + cold="$1" + results="$2" + mkdir -p "${results}" + # What the restore brought: only the parse cache directory. + [ -s "${pc}/index.db" ] || { echo "::error::no parse cache was restored into ${pc}"; exit 1; } + echo "restored: ${pc} ($(mode_of "${pc}"))" | tee -a "${results}/summary.txt" + [ "$(mode_of "${pc}" | cut -d' ' -f1)" = 700 ] || { echo "::error::the restored directory is not 0700"; exit 1; } + [ ! -e test/fixture/.kapi/work ] || { echo "::error::the restore wrote into the checkout"; exit 1; } + snapshot "${pc}" > "${results}/before.txt" + diff -u "${cold}/snapshot.txt" "${results}/before.txt" + + if command -v strace > /dev/null; then + strace -f -qq -e trace=openat -o "${results}/openat.txt" \ + "${root}/test/state-cache/run.sh" test/fixture "${results}/run" + # kapi opens a part file relative to the cache directory's descriptor, + # so the traced name is the bare file name. + opened="$(grep -cE 'openat2?\(.*"([^"]*/)?parts-[0-9a-f]+\.log".*O_RDONLY' "${results}/openat.txt" || true)" + echo "the warm run opened recorded part files for reading ${opened} times; examples:" | tee -a "${results}/summary.txt" + grep -E 'openat2?\(.*"([^"]*/)?parts-[0-9a-f]+\.log".*O_RDONLY' "${results}/openat.txt" | head -n 3 | tee -a "${results}/summary.txt" || true + grep -E 'parts-|index\.db' "${results}/openat.txt" | head -n 5 || true + [ "${opened}" -gt 0 ] || { echo "::error::the warm run read no recorded part file"; exit 1; } + else + "${root}/test/state-cache/run.sh" test/fixture "${results}/run" + fi + check_layout "${results}" + snapshot "${pc}" > "${results}/after.txt" + python3 - "${pc}/index.db" <<'PY' | tee -a "${results}/summary.txt" +import sqlite3, sys +for path, checkout in sqlite3.connect(sys.argv[1]).execute("select path, checkout from documents order by path"): + print(f"entry {path}: recorded for checkout {checkout}") +PY + if diff -u "${results}/before.txt" "${results}/after.txt"; then + echo "the warm run wrote no entry: every file it parsed came from the cache" | tee -a "${results}/summary.txt" + else + echo "::error::the warm run changed the parse cache" + exit 1 + fi + + # A cold run of a fresh copy with its own empty parse cache and data root. + "${root}/test/state-cache/cold-copy.sh" "${RUNNER_TEMP}/cold" + (cd "${RUNNER_TEMP}/cold" && KAPI_PARSE_CACHE_DIR="${RUNNER_TEMP}/cold-parse" KAPI_DATA_DIR="${RUNNER_TEMP}/cold-data" \ + "${root}/test/state-cache/run.sh" test/fixture "${results}/cold-here") + normalize "${results}/run" + normalize "${results}/cold-here" + normalize "${cold}/run" + "${root}/test/state-cache/compare.sh" "${results}/cold-here.norm" "${results}/run.norm" + "${root}/test/state-cache/compare.sh" "${cold}/run.norm" "${results}/run.norm" + + # Control: with one recorded part file damaged, kapi must notice and parse + # that file again, which shows the warm run's entries were read and checked. + ctl="${RUNNER_TEMP}/control-parse" + rm -rf "${ctl}" + cp -Rp "${pc}" "${ctl}" + chmod 700 "${ctl}" + # The entry for the source file every command reads; one byte changed. + victim="$(python3 -c 'import sqlite3,sys; print(sqlite3.connect(sys.argv[1]).execute("select parts_ref from documents where path = ?", ("locales/en.json",)).fetchone()[0])' "${ctl}/index.db")" + python3 -c 'import sys; p=sys.argv[1]; b=bytearray(open(p,"rb").read()); i=len(b)//2; b[i] = ord("Z") if b[i] != ord("Z") else ord("Y"); open(p,"wb").write(b)' "${ctl}/${victim}" + snapshot "${ctl}" > "${results}/control-before.txt" + "${root}/test/state-cache/cold-copy.sh" "${RUNNER_TEMP}/control" + (cd "${RUNNER_TEMP}/control" && KAPI_PARSE_CACHE_DIR="${ctl}" KAPI_DATA_DIR="${RUNNER_TEMP}/control-data" \ + kapi status -p test/fixture/kapi.yaml --json > /dev/null) + snapshot "${ctl}" > "${results}/control-after.txt" + if diff -q "${results}/control-before.txt" "${results}/control-after.txt" > /dev/null; then + echo "::error::kapi accepted a damaged part file" + exit 1 + fi + echo "control: with ${victim} damaged, kapi parsed that file again:" | tee -a "${results}/summary.txt" + diff "${results}/control-before.txt" "${results}/control-after.txt" | tee -a "${results}/summary.txt" || true + ;; + + refuse) + results="$1" + mkdir -p "${results}" + for cmd in "status -p test/fixture/kapi.yaml" "up -p test/fixture/kapi.yaml --json"; do + rc=0 + # shellcheck disable=SC2086 # the command is a word list + kapi ${cmd} > "${results}/out.txt" 2>&1 || rc=$? + echo "kapi ${cmd}: exit ${rc}" | tee -a "${results}/summary.txt" + sed 's/^/ | /' "${results}/out.txt" | tail -n 5 | tee -a "${results}/summary.txt" + [ "${rc}" -ne 0 ] || { echo "::error::kapi ${cmd} accepted the parse cache directory"; exit 1; } + done + ;; +esac From 804cff90eb7210f436b3408d3b96d44c674f6d12 Mon Sep 17 00:00:00 2001 From: Asgeir Frimannsson Date: Mon, 23 Nov 2026 20:28:08 +0100 Subject: [PATCH 19/20] End the version in the parse cache key, and remove the temporary runs The restore prefix kapi-parse--1.4.0- matched caches saved by 1.4.0-rc5, as the run against neokapi main showed (1.2.0 restored a 1.2.0-rc32 cache). An underscore now ends the version, which the version check does not allow inside one. Verified: cold and warm runs against neokapi main 24b78f1 (with #3804) through this action on Linux and macOS, run 45248697071; test/parse-cache/env.sh; actionlint Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01GtGobCdyR3d9yLXAfX9jsr --- .github/workflows/test.yml | 192 --------------------------------- action.yml | 14 +-- test/parse-cache/branch-e2e.sh | 169 ----------------------------- 3 files changed, 8 insertions(+), 367 deletions(-) delete mode 100755 test/parse-cache/branch-e2e.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 34f1e7f..7dacc0b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -507,195 +507,3 @@ jobs: - name: Verify bowrain matches a fresh install shell: bash run: test/plugins/matches-fresh.sh bowrain - - # TEMPORARY: cold and warm runs of the kapi build from neokapi/neokapi main (24b78f1, with #3804) - # through this action. Removed before merging. - e2e-build-kapi: - name: "E2E: build kapi main 24b78f1 (${{ matrix.os }})" - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - os: [ubuntu-latest, macos-latest] - steps: - - uses: actions/checkout@v6 - with: - repository: neokapi/neokapi - ref: 24b78f17467f0ff31aaec2429d5417c2c45139d2 - path: neokapi - - - uses: actions/setup-go@v6 - with: - go-version-file: neokapi/go.mod - cache-dependency-path: neokapi/**/go.sum - - - name: Install ICU - shell: bash - run: | - if [ "$RUNNER_OS" = Linux ]; then - sudo apt-get update -q && sudo apt-get install -y -q libicu-dev pkg-config - else - brew list icu4c > /dev/null 2>&1 || brew install icu4c pkg-config - echo "PKG_CONFIG_PATH=$(brew --prefix icu4c)/lib/pkgconfig" >> "$GITHUB_ENV" - fi - - - name: Build kapi - shell: bash - working-directory: neokapi - run: | - go run ./core/i18n/gen/catalogs - cd kapi - go build -trimpath -tags fts5 -o "${RUNNER_TEMP}/kapi-branch/kapi" ./cmd/kapi - "${RUNNER_TEMP}/kapi-branch/kapi" version - - - uses: actions/upload-artifact@v7 - with: - name: kapi-3804-${{ matrix.os }} - path: ${{ runner.temp }}/kapi-branch - retention-days: 1 - - e2e-cold: - name: "E2E: cold run (${{ matrix.os }})" - needs: e2e-build-kapi - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - os: [ubuntu-latest, macos-latest] - steps: - - uses: actions/checkout@v6 - - - uses: actions/download-artifact@v8 - with: - name: kapi-3804-${{ matrix.os }} - path: ${{ runner.temp }}/kapi-branch - - - name: Setup kapi - uses: ./ - with: - token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} - # Not a version any other job caches under, so the key is this pair's own. - version: 1.2.0 - plugins: "" - project-dir: test/fixture - - - name: Put the main 24b78f1 build first on PATH - shell: bash - run: | - chmod +x "${RUNNER_TEMP}/kapi-branch/kapi" - echo "${RUNNER_TEMP}/kapi-branch" >> "$GITHUB_PATH" - - - name: Cold run - shell: bash - run: test/parse-cache/branch-e2e.sh cold "${RUNNER_TEMP}/e2e/cold" - - - uses: actions/upload-artifact@v7 - with: - name: e2e-cold-${{ matrix.os }} - path: ${{ runner.temp }}/e2e/cold - retention-days: 1 - - e2e-warm: - name: "E2E: warm run (${{ matrix.os }})" - needs: e2e-cold - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - os: [ubuntu-latest, macos-latest] - steps: - - uses: actions/checkout@v6 - - - uses: actions/download-artifact@v8 - with: - name: kapi-3804-${{ matrix.os }} - path: ${{ runner.temp }}/kapi-branch - - - uses: actions/download-artifact@v8 - with: - name: e2e-cold-${{ matrix.os }} - path: ${{ runner.temp }}/e2e/cold - - - name: Install strace - if: runner.os == 'Linux' - shell: bash - run: sudo apt-get install -y -q strace - - - name: Setup kapi - uses: ./ - with: - token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} - # Not a version any other job caches under, so the key is this pair's own. - version: 1.2.0 - plugins: "" - project-dir: test/fixture - - - name: Put the main 24b78f1 build first on PATH - shell: bash - run: | - chmod +x "${RUNNER_TEMP}/kapi-branch/kapi" - echo "${RUNNER_TEMP}/kapi-branch" >> "$GITHUB_PATH" - - - name: Warm run - shell: bash - run: test/parse-cache/branch-e2e.sh warm "${RUNNER_TEMP}/e2e/cold" "${RUNNER_TEMP}/e2e/warm" - - e2e-refuse: - name: "E2E: refused (${{ matrix.case }}, ${{ matrix.os }})" - needs: e2e-build-kapi - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - os: [ubuntu-latest, macos-latest] - case: [inside-project, inside-repository, wider] - steps: - - uses: actions/checkout@v6 - - - uses: actions/download-artifact@v8 - with: - name: kapi-3804-${{ matrix.os }} - path: ${{ runner.temp }}/kapi-branch - - - name: Set a parse cache directory inside the project - if: matrix.case == 'inside-project' - shell: bash - run: echo "KAPI_PARSE_CACHE_DIR=${GITHUB_WORKSPACE}/test/fixture/.kapi-parse" >> "$GITHUB_ENV" - - - name: Set a parse cache directory in the repository, outside the project - if: matrix.case == 'inside-repository' - shell: bash - run: echo "KAPI_PARSE_CACHE_DIR=${GITHUB_WORKSPACE}/.kapi-parse" >> "$GITHUB_ENV" - - - name: Setup kapi - uses: ./ - with: - token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} - version: 1.4.0-rc5 - plugins: "" - project-dir: test/fixture - - - name: Put the main 24b78f1 build first on PATH - shell: bash - run: | - chmod +x "${RUNNER_TEMP}/kapi-branch/kapi" - echo "${RUNNER_TEMP}/kapi-branch" >> "$GITHUB_PATH" - - - name: Make the directory wider than kapi accepts - if: matrix.case == 'wider' - shell: bash - run: | - # A restored cache may already be there; widen it either way. - mkdir -p "${KAPI_PARSE_CACHE_DIR}" - chmod 755 "${KAPI_PARSE_CACHE_DIR}" - - - name: Every kapi command fails with kapi's message - shell: bash - run: test/parse-cache/branch-e2e.sh refuse "${RUNNER_TEMP}/e2e/refuse" - - # Leave nothing for the post step to save: a widened directory saved - # into the cache would be restored, and refused, by every later run. - - name: Remove the refused directory - if: always() - shell: bash - run: rm -rf "${KAPI_PARSE_CACHE_DIR}" diff --git a/action.yml b/action.yml index 0a38b57..a28d0cc 100644 --- a/action.yml +++ b/action.yml @@ -332,6 +332,8 @@ runs: # # The kapi version is part of the key because the parse cache keeps the # entries every build wrote; a new version starts from an empty cache. + # An underscore ends the version, a character no release version holds, + # so the restore prefix for 1.4.0 does not match a 1.4.0-rc5 cache. # # GitHub scopes caches by ref: a run restores caches its own ref saved # and those of the default branch (and a pull request's base), never @@ -347,10 +349,10 @@ runs: path: | ${{ steps.parse-cache-dir.outputs.dir }} ${{ inputs.project-dir }}/.kapi/work/cache/docs - key: kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}-${{ github.ref_name }}-${{ github.job }}-${{ github.run_id }}-${{ github.run_attempt }} + key: kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}_${{ github.ref_name }}-${{ github.job }}-${{ github.run_id }}-${{ github.run_attempt }} restore-keys: | - kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}-${{ github.ref_name }}- - kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}- + kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}_${{ github.ref_name }}- + kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}_ - name: Restore kapi parse cache (no save) if: steps.parse-cache-dir.outputs.dir != '' && contains(fromJSON('["pull_request_target","workflow_run","issue_comment"]'), github.event_name) @@ -359,7 +361,7 @@ runs: path: | ${{ steps.parse-cache-dir.outputs.dir }} ${{ inputs.project-dir }}/.kapi/work/cache/docs - key: kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}-${{ github.ref_name }}-${{ github.job }}-${{ github.run_id }}-${{ github.run_attempt }} + key: kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}_${{ github.ref_name }}-${{ github.job }}-${{ github.run_id }}-${{ github.run_attempt }} restore-keys: | - kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}-${{ github.ref_name }}- - kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}- + kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}_${{ github.ref_name }}- + kapi-parse-${{ runner.os }}-${{ steps.resolve.outputs.version }}_ diff --git a/test/parse-cache/branch-e2e.sh b/test/parse-cache/branch-e2e.sh deleted file mode 100755 index 1650942..0000000 --- a/test/parse-cache/branch-e2e.sh +++ /dev/null @@ -1,169 +0,0 @@ -#!/usr/bin/env bash -# TEMPORARY: cold and warm runs of a kapi that reads KAPI_PARSE_CACHE_DIR, -# through setup-kapi. Removed before merging. -# -# branch-e2e.sh cold -# branch-e2e.sh warm -# branch-e2e.sh refuse (expects every kapi command to fail) -# -# The action has already set KAPI_PARSE_CACHE_DIR; kapi on PATH is the build -# under test; the project is test/fixture. -set -euo pipefail - -here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -root="$(cd "${here}/../.." && pwd)" -mode="$1" -shift - -pc="${KAPI_PARSE_CACHE_DIR:?KAPI_PARSE_CACHE_DIR is not set}" - -# These jobs install release 1.4.0-rc5 and then run the #3804 build, so the -# cache key names 1.4.0-rc5 and the restore can bring the in-tree cache another -# job saved with the real 1.4.0-rc5. Remove it, so the check below sees only -# what this build writes. -if [ -e test/fixture/.kapi/work ]; then - echo "note: removed test/fixture/.kapi/work, restored from a 1.4.0-rc5 in-tree cache" - rm -rf test/fixture/.kapi/work -fi - -mode_of() { - if stat -c '%a' "$1" > /dev/null 2>&1; then stat -c '%a %U' "$1"; else stat -f '%Lp %Su' "$1"; fi -} - -# snapshot DIR: each file under DIR with its digest, and the index rows. -snapshot() { - (cd "$1" && find . -type f ! -name 'index.db*' | LC_ALL=C sort | while read -r f; do - printf '%s %s\n' "$(shasum -a 256 < "$f" | cut -d' ' -f1)" "$f" - done) - python3 - "$1/index.db" <<'PY' -import sqlite3, sys -db = sqlite3.connect(sys.argv[1]) -for row in db.execute("select path, build, config_key, content_hash, parts_ref, parts_digest from documents order by path, config_key"): - print("row", *row) -PY -} - -# normalize RECORD: drop timestamps that differ between any two runs. -normalize() { - local out="$1.norm" - mkdir -p "$out" - for f in "$1"/*.json; do - jq -c 'del(.. | .at?)' "$f" > "$out/$(basename "$f")" 2> /dev/null || cp "$f" "$out/" - done - cp "$1"/*.rc "$1/tree.txt" "$out/" -} - -check_layout() { - local results="$1" - kapi version | tee "${results}/version.txt" - kapi state path -p test/fixture --json | tee "${results}/state-path.json" - local state got - state="$(jq -r .path "${results}/state-path.json")" - got="$(jq -r .parse_cache "${results}/state-path.json")" - [ "${got}" = "${pc}" ] || { echo "::error::kapi resolved the parse cache to ${got}, not ${pc}"; exit 1; } - [ ! -e "${state}/work/cache/docs" ] || { echo "::error::the state directory holds a parse cache"; exit 1; } - [ ! -e test/fixture/.kapi/work ] || { echo "::error::the checkout holds .kapi/work"; exit 1; } - echo "parse cache directory: ${pc} ($(mode_of "${pc}"))" | tee -a "${results}/summary.txt" - [ "$(mode_of "${pc}" | cut -d' ' -f1)" = 700 ] || { echo "::error::the parse cache directory is not 0700"; exit 1; } - find "${pc}" -type f | sed "s|${pc}||" | LC_ALL=C sort | tee "${results}/pc-files.txt" -} - -case "${mode}" in - cold) - results="$1" - mkdir -p "${results}" - # An earlier run of this workflow may have left a cache under this key. - if [ -e "${pc}" ]; then - echo "note: removed ${pc}, restored from an earlier run, so this run starts cold" - rm -rf "${pc}" - fi - "${root}/test/state-cache/run.sh" test/fixture "${results}/run" - check_layout "${results}" - snapshot "${pc}" > "${results}/snapshot.txt" - echo "entries: $(grep -c '^row ' "${results}/snapshot.txt"), part files: $(grep -vc '^row ' "${results}/snapshot.txt")" | tee -a "${results}/summary.txt" - ;; - - warm) - cold="$1" - results="$2" - mkdir -p "${results}" - # What the restore brought: only the parse cache directory. - [ -s "${pc}/index.db" ] || { echo "::error::no parse cache was restored into ${pc}"; exit 1; } - echo "restored: ${pc} ($(mode_of "${pc}"))" | tee -a "${results}/summary.txt" - [ "$(mode_of "${pc}" | cut -d' ' -f1)" = 700 ] || { echo "::error::the restored directory is not 0700"; exit 1; } - [ ! -e test/fixture/.kapi/work ] || { echo "::error::the restore wrote into the checkout"; exit 1; } - snapshot "${pc}" > "${results}/before.txt" - diff -u "${cold}/snapshot.txt" "${results}/before.txt" - - if command -v strace > /dev/null; then - strace -f -qq -e trace=openat -o "${results}/openat.txt" \ - "${root}/test/state-cache/run.sh" test/fixture "${results}/run" - # kapi opens a part file relative to the cache directory's descriptor, - # so the traced name is the bare file name. - opened="$(grep -cE 'openat2?\(.*"([^"]*/)?parts-[0-9a-f]+\.log".*O_RDONLY' "${results}/openat.txt" || true)" - echo "the warm run opened recorded part files for reading ${opened} times; examples:" | tee -a "${results}/summary.txt" - grep -E 'openat2?\(.*"([^"]*/)?parts-[0-9a-f]+\.log".*O_RDONLY' "${results}/openat.txt" | head -n 3 | tee -a "${results}/summary.txt" || true - grep -E 'parts-|index\.db' "${results}/openat.txt" | head -n 5 || true - [ "${opened}" -gt 0 ] || { echo "::error::the warm run read no recorded part file"; exit 1; } - else - "${root}/test/state-cache/run.sh" test/fixture "${results}/run" - fi - check_layout "${results}" - snapshot "${pc}" > "${results}/after.txt" - python3 - "${pc}/index.db" <<'PY' | tee -a "${results}/summary.txt" -import sqlite3, sys -for path, checkout in sqlite3.connect(sys.argv[1]).execute("select path, checkout from documents order by path"): - print(f"entry {path}: recorded for checkout {checkout}") -PY - if diff -u "${results}/before.txt" "${results}/after.txt"; then - echo "the warm run wrote no entry: every file it parsed came from the cache" | tee -a "${results}/summary.txt" - else - echo "::error::the warm run changed the parse cache" - exit 1 - fi - - # A cold run of a fresh copy with its own empty parse cache and data root. - "${root}/test/state-cache/cold-copy.sh" "${RUNNER_TEMP}/cold" - (cd "${RUNNER_TEMP}/cold" && KAPI_PARSE_CACHE_DIR="${RUNNER_TEMP}/cold-parse" KAPI_DATA_DIR="${RUNNER_TEMP}/cold-data" \ - "${root}/test/state-cache/run.sh" test/fixture "${results}/cold-here") - normalize "${results}/run" - normalize "${results}/cold-here" - normalize "${cold}/run" - "${root}/test/state-cache/compare.sh" "${results}/cold-here.norm" "${results}/run.norm" - "${root}/test/state-cache/compare.sh" "${cold}/run.norm" "${results}/run.norm" - - # Control: with one recorded part file damaged, kapi must notice and parse - # that file again, which shows the warm run's entries were read and checked. - ctl="${RUNNER_TEMP}/control-parse" - rm -rf "${ctl}" - cp -Rp "${pc}" "${ctl}" - chmod 700 "${ctl}" - # The entry for the source file every command reads; one byte changed. - victim="$(python3 -c 'import sqlite3,sys; print(sqlite3.connect(sys.argv[1]).execute("select parts_ref from documents where path = ?", ("locales/en.json",)).fetchone()[0])' "${ctl}/index.db")" - python3 -c 'import sys; p=sys.argv[1]; b=bytearray(open(p,"rb").read()); i=len(b)//2; b[i] = ord("Z") if b[i] != ord("Z") else ord("Y"); open(p,"wb").write(b)' "${ctl}/${victim}" - snapshot "${ctl}" > "${results}/control-before.txt" - "${root}/test/state-cache/cold-copy.sh" "${RUNNER_TEMP}/control" - (cd "${RUNNER_TEMP}/control" && KAPI_PARSE_CACHE_DIR="${ctl}" KAPI_DATA_DIR="${RUNNER_TEMP}/control-data" \ - kapi status -p test/fixture/kapi.yaml --json > /dev/null) - snapshot "${ctl}" > "${results}/control-after.txt" - if diff -q "${results}/control-before.txt" "${results}/control-after.txt" > /dev/null; then - echo "::error::kapi accepted a damaged part file" - exit 1 - fi - echo "control: with ${victim} damaged, kapi parsed that file again:" | tee -a "${results}/summary.txt" - diff "${results}/control-before.txt" "${results}/control-after.txt" | tee -a "${results}/summary.txt" || true - ;; - - refuse) - results="$1" - mkdir -p "${results}" - for cmd in "status -p test/fixture/kapi.yaml" "up -p test/fixture/kapi.yaml --json"; do - rc=0 - # shellcheck disable=SC2086 # the command is a word list - kapi ${cmd} > "${results}/out.txt" 2>&1 || rc=$? - echo "kapi ${cmd}: exit ${rc}" | tee -a "${results}/summary.txt" - sed 's/^/ | /' "${results}/out.txt" | tail -n 5 | tee -a "${results}/summary.txt" - [ "${rc}" -ne 0 ] || { echo "::error::kapi ${cmd} accepted the parse cache directory"; exit 1; } - done - ;; -esac From c1ee6b32b906bd993ec0042c46e44f3b31b8d4dc Mon Sep 17 00:00:00 2001 From: Asgeir Frimannsson Date: Mon, 23 Nov 2026 20:37:05 +0100 Subject: [PATCH 20/20] Compare parse-cache case-insensitively, mask the auth token, test it through a real run - 'False' and 'FALSE' turn the cache off for parse-cache and cache-tm, as v1's expression comparison did. - Each line of auth-token is masked before it is written to GITHUB_ENV. - A job passes a two-line dummy token through the action on Linux, macOS and Windows and reads both variables back. Verified: test/parse-cache/env.sh (False/FALSE/false for both inputs, the mask commands); actionlint; shellcheck Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01GtGobCdyR3d9yLXAfX9jsr --- .github/workflows/test.yml | 32 ++++++++++++++++++++++++++++++++ action.yml | 12 +++++++++--- test/parse-cache/env.sh | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7dacc0b..6aa7765 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -507,3 +507,35 @@ jobs: - name: Verify bowrain matches a fresh install shell: bash run: test/plugins/matches-fresh.sh bowrain + + # A token passed as a plain value through the real action reaches later + # steps intact, and its line break starts no other variable. The token's + # second line has the shape of an assignment. + test-auth-env: + name: "Auth token through GITHUB_ENV (${{ matrix.os }})" + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + steps: + - uses: actions/checkout@v6 + + - name: Setup kapi with a dummy token + uses: ./ + with: + token: ${{ secrets.NEOKAPI_GITHUB_TOKEN }} + plugins: "" + auth-token: "dummy-token-a\nSETUP_KAPI_INJECTED=1" + server: https://bowrain.invalid + + - name: Read the variables back + shell: bash + run: | + want="$(printf 'dummy-token-a\nSETUP_KAPI_INJECTED=1')" + test "${BOWRAIN_AUTH_TOKEN}" = "${want}" + test "${BOWRAIN_SERVER_URL}" = "https://bowrain.invalid" + test -z "${SETUP_KAPI_INJECTED+set}" + if env | grep -q '^SETUP_KAPI_EOF'; then exit 1; fi + echo "BOWRAIN_AUTH_TOKEN and BOWRAIN_SERVER_URL arrived intact; no other variable was set." + diff --git a/action.yml b/action.yml index a28d0cc..ef45d2f 100644 --- a/action.yml +++ b/action.yml @@ -146,6 +146,11 @@ runs: INPUT_AUTH_TOKEN: ${{ inputs.auth-token }} INPUT_SERVER: ${{ inputs.server }} run: | + # Mask the token before anything else touches it, each line on its + # own, so a token passed as a plain value stays out of later logs. + while IFS= read -r line; do + if [ -n "${line}" ]; then echo "::add-mask::${line}"; fi + done <<< "${INPUT_AUTH_TOKEN}" # The multi-line form with a random delimiter, so a line break in a # value cannot start another variable. put_env() { @@ -230,9 +235,10 @@ runs: echo "::warning title=setup-kapi::The cache-tm input is deprecated; use parse-cache instead." ENABLED="${INPUT_CACHE_TM}" fi - # Only 'false' turns the cache off, as before; write nothing else of - # the input to the output. - if [ "${ENABLED}" = "false" ]; then + # Only 'false' turns the cache off, compared without regard to case + # as v1's expression `inputs.cache-tm != 'false'` did; write nothing + # else of the input to the output. + if [ "$(printf '%s' "${ENABLED}" | tr '[:upper:]' '[:lower:]')" = "false" ]; then echo "enabled=false" >> "$GITHUB_OUTPUT" else echo "enabled=true" >> "$GITHUB_OUTPUT" diff --git a/test/parse-cache/env.sh b/test/parse-cache/env.sh index 29a8ba2..1477167 100755 --- a/test/parse-cache/env.sh +++ b/test/parse-cache/env.sh @@ -97,6 +97,39 @@ if [ "${names}" = "BOWRAIN_AUTH_TOKEN BOWRAIN_SERVER_URL " ]; then else fail auth "variables set: ${names}" fi +masks="$(env INPUT_AUTH_TOKEN="tok +BASH_ENV=/tmp/evil +" INPUT_SERVER="" GITHUB_ENV=/dev/null bash -e "${work}/auth.sh" | grep '^::add-mask::' | tr '\n' ' ')" +if [ "${masks}" = "::add-mask::tok ::add-mask::BASH_ENV=/tmp/evil " ]; then + echo "ok auth: each line of the token is masked" +else + fail auth-mask "mask commands: '${masks}'" +fi + +# parse-cache and cache-tm turn the cache off on 'false' in any case, as v1's +# expression did, and on nothing else. +yq '.runs.steps[] | select(.id == "parse-cache") | .run' "${root}/action.yml" > "${work}/enabled.sh" +# enabled_case PARSE_CACHE CACHE_TM WANT +enabled_case() { + : > "${work}/enabled.out" + env INPUT_PARSE_CACHE="$1" INPUT_CACHE_TM="$2" GITHUB_OUTPUT="${work}/enabled.out" \ + bash -e "${work}/enabled.sh" > /dev/null + local got + got="$(cat "${work}/enabled.out")" + if [ "${got}" = "enabled=$3" ]; then + echo "ok parse-cache='$1' cache-tm='$2': $3" + else + fail enabled "parse-cache='$1' cache-tm='$2': got '${got}', want 'enabled=$3'" + fi +} +for value in false False FALSE; do + enabled_case "${value}" "" false + enabled_case true "${value}" false +done +enabled_case true "" true +enabled_case false true true +enabled_case "no" "" true + # Both parse cache steps carry the directory the step names and the in-tree # path, as fixed strings.