---
title: "Tasks: ALKS CLI Keystore — Replace keytar with a Node-24-Compatible Keychain Library"
plan_source: spec-driven/alks-cli-keystore-keytar-replacement/plan.md
plan_hash: sha256:edc202e5631143d0984a4a8acfdbffa9a7a3f7b60d500adcc300160c49ca8360
spec_source: spec-driven/alks-cli-keystore-keytar-replacement/spec.md
spec_hash: sha256:898ed80fae7828d162c4b3efcaf98fdd0bbeaa80c77a351fec5ca4386e37fad9
status: final
version: 1.0
date: 2026-07-30
total_tasks: 8
total_bundles: 5
bundle_mode: sequential
validation: subagent
---

# Tasks: ALKS CLI Keystore — Replace keytar with a Node-24-Compatible Keychain Library

> Plan: spec-driven/alks-cli-keystore-keytar-replacement/plan.md | Spec: spec-driven/alks-cli-keystore-keytar-replacement/spec.md | Generated: 2026-07-30 | Status: Final

> Do not edit this document. Track completion in spec-driven/alks-cli-keystore-keytar-replacement/progress.md, not here.

> Execute bundles in order. Each bundle depends on the previous one completing.

## Traceability

| FR | AC | STEP | Tasks |
|----|----|------|-------|
| FR-1 | AC-1.1 | STEP-1, STEP-5 | TASK-1, TASK-5 |
| FR-1 | AC-1.2, AC-1.3, AC-1.4, AC-1.6 | STEP-2 | TASK-2 |
| FR-1 | AC-1.5 | STEP-8 | TASK-8 |
| FR-2 | AC-2.1 | STEP-2 | TASK-2 |
| FR-2 | AC-2.2, AC-2.3 | STEP-3 | TASK-3 |
| FR-3 | AC-3.1 | STEP-4, STEP-7 | TASK-4, TASK-7 |
| FR-3 | AC-3.2 | STEP-4 | TASK-4 |
| FR-4 | AC-4.1, AC-4.2 | STEP-5, STEP-6 | TASK-5, TASK-6 |

---

## Bundle 1: Foundation
> Phase: Walking Skeleton | Parallel: no | Files: package.json, npm-shrinkwrap.json

### TASK-1: Swap the keychain dependency (keytar → @napi-rs/keyring)
[STEP-1 → FR-1, AC-1.1] | modify `package.json`, `npm-shrinkwrap.json` | Effort: S

> **Intent**: Removes the root cause — archived, per-Node-ABI keytar with no Node 24 prebuild — and brings in `@napi-rs/keyring`, whose ABI-stable Node-API binding covers 20/22/24 from one prebuild. Placed in `dependencies` (not `optionalDependencies` as keytar was) so a missing binary can no longer be silently tolerated at install time (AD-2).

- Remove `keytar` from `optionalDependencies` in `package.json`
- Add `@napi-rs/keyring` (latest 1.3.x) to `dependencies`
- Delete `node_modules` and `npm-shrinkwrap.json`, reinstall against the public npm registry (`npm i --registry=https://registry.npmjs.org/`), then `npm shrinkwrap` per the repo note in `build-and-test.yml`
- **Verify**: `npm ci` completes; `node -e "require('@napi-rs/keyring')"` exits 0; `npm ls keytar` reports keytar absent

> Depends on: — | Enables: TASK-2, TASK-4 | Parallel with: —

---

## Bundle 2: Adapter, Honest Errors, and Support Contract
> Phase: Adapter/Errors/Contract | Parallel: no (TASK-2 gates TASK-3/TASK-4) | Files: src/lib/getKeytar.ts, src/lib/getKeytar.test.ts, src/lib/storeToken.ts, src/lib/storeToken.test.ts, package.json

### TASK-2: Rewrite getKeytar.ts as the adapter over @napi-rs/keyring
[STEP-2 → FR-1, AC-1.2, AC-1.3, AC-1.4, AC-1.6 | FR-2, AC-2.1] | modify `src/lib/getKeytar.ts`, create `src/lib/getKeytar.test.ts` | Effort: M

> **Intent**: The single adapter boundary is what keeps this migration small — the library's `AsyncEntry(service, account)` object API is translated here so the three call sites keep calling `getPassword/setPassword/deletePassword(service, account)` unchanged (AC-1.6). Two behavioral gotchas carry the risk: `@napi-rs/keyring` raises `NoEntry` where keytar returned `null`, so `getPassword` must map `NoEntry` → `undefined` (else the plaintext fallback breaks) and `deletePassword` must treat `NoEntry` as a no-op. This task also stops swallowing the real import/load error into a generic "reinstall" string (AC-2.1).
> **Standards**: Lock the new runtime behavior with at least one test (CLAUDE.md).

- Write failing tests in `src/lib/getKeytar.test.ts` (mock `@napi-rs/keyring`'s `AsyncEntry`): `setPassword` then `getPassword` returns the stored secret (AC-1.2); `getPassword` maps a `NoEntry` rejection to `undefined` (AC-1.3); `deletePassword` treats `NoEntry` as a no-op success, not a throw (AC-1.4); returned object exposes `getPassword`/`setPassword`/`deletePassword` with `(service, account[, secret])` signatures (AC-1.6); an import/instantiation failure surfaces the underlying error message, not a bare "reinstall" string (AC-2.1)
- Rewrite `getKeytar()` to return an adapter whose methods construct `new AsyncEntry(service, account)` and delegate to `.getPassword()`/`.setPassword(secret)`/`.deletePassword()`, catching `NoEntry` → `undefined` (get) and `NoEntry` → no-op (delete)
- On import/instantiation failure, propagate the real error (keep the Linux `libsecret` hint but include the underlying cause rather than replacing it)
- Follow mocking/`resetAllMocks` pattern from `src/lib/promptForToken.test.ts`
- **Verify**: `npm test -- --no-watch --testPathPattern=getKeytar` (asserts NoEntry→undefined, delete no-op, and real error surfaced) then `npm run build`

> Depends on: TASK-1 | Enables: TASK-3, TASK-5 | Parallel with: —

### TASK-3: Make storeToken.ts report the real failure and keep an honest fallback
[STEP-3 → FR-2, AC-2.2, AC-2.3] | modify `src/lib/storeToken.ts`, create `src/lib/storeToken.test.ts` | Effort: S

> **Intent**: `storeToken` currently prints "No keychain could be found for storing the token" for any keystore failure — false, since the real cause was a native module that never loaded. It must print the caught error's actual message before offering the plaintext fallback, so breakage is self-diagnosing without `-v`. The plaintext fallback (write `credentials.refresh_token`, read back via `getTokenFromKeystore`) stays intact — removing it is a Non-Goal.
> **Standards**: Lock the new runtime behavior with at least one test (CLAUDE.md).

- Write failing tests in `src/lib/storeToken.test.ts` (mock `./getKeytar`, `./confirm`, `./state/credentials`, `./log`): when the adapter throws, the caught error's message is printed and the literal string "No keychain could be found for storing the token" is not (AC-2.2); on confirmation the token is written via `setCredentials` (AC-2.3)
- Modify `storeToken.ts` to print the caught error's message in place of the hardcoded "No keychain could be found…" line, then proceed to the existing plaintext-fallback confirm/write
- Add an assertion (here or in `getTokenFromKeystore.test.ts`) that `getTokenFromKeystore` returns `credentials.refresh_token` when the adapter fails (AC-2.3)
- Follow keystore-collaborator mocking pattern from `src/lib/state/token.test.ts`
- **Verify**: `npm test -- --no-watch --testPathPattern="storeToken|getTokenFromKeystore"` (asserts real message shown, "No keychain could be found" absent, fallback token round-trips) then `npm run build`

> Depends on: TASK-2 | Enables: TASK-8 | Parallel with: TASK-4, TASK-5

### TASK-4: Align package.json engines with the CI matrix
[STEP-4 → FR-3, AC-3.1, AC-3.2] | modify `package.json` | Effort: XS

> **Intent**: `engines.node` says `>=16.0.0`, contradicting the CI matrix (20/22/24) and the release Node (24). Setting it to `>=20` makes the support contract honest; `@napi-rs/keyring` declares `node >= 10`, so it imposes no higher floor (AC-3.2).

- Change `engines.node` from `">=16.0.0"` to `">=20"`
- Confirm `@napi-rs/keyring`'s declared `engines.node` (`>=10`) is satisfied by the new `>=20` floor (AC-3.2)
- **Verify**: `node -e "const e=require('./package.json').engines.node; if(!/(^|[^0-9])20/.test(e)||/16/.test(e))process.exit(1)"` exits 0 (floor 20, no reference to 16); `node -e "const k=require('@napi-rs/keyring/package.json').engines?.node||'>=10'; process.exit(parseInt(k.replace(/[^0-9]/g,''))>20?1:0)"` confirms the lib floor ≤20

> Depends on: TASK-1 | Enables: TASK-7 | Parallel with: TASK-3, TASK-5

---

## Bundle 3: Regression Guard (Non-Mocked) and CI
> Phase: CI Guard | Parallel: no (TASK-5 gates TASK-6) | Files: src/lib/getKeytar.integration.test.ts, .github/workflows/build-and-test.yml

### TASK-5: Add a non-mocked keystore round-trip integration test
[STEP-5 → FR-4, AC-4.1, AC-4.2 | FR-1, AC-1.1] | create `src/lib/getKeytar.integration.test.ts` | Effort: M

> **Intent**: The incident shipped green because every keystore test mocked keytar and the real binding was never loaded (`getKeytar` had 0 function hits). This test uses no mocks: a binding-load assertion (import + instantiate `AsyncEntry`) runs everywhere and would have caught the exact `Cannot find module keytar.node` failure (AC-4.2), plus a full `setPassword → getPassword → deletePassword` round-trip gated on a secret service being available (macOS always; Linux when provisioned in TASK-6) (AC-4.1, AC-1.1).
> **Standards**: Lock the new runtime behavior with at least one test (CLAUDE.md).

- Create `src/lib/getKeytar.integration.test.ts` that does NOT mock `@napi-rs/keyring`: assert the adapter loads and `new AsyncEntry('alkscli-selftest','roundtrip')` instantiates — fail the test if the native binding cannot load (AC-4.2)
- When a secret service is available, perform a real `setPassword` → `getPassword` (value matches) → `deletePassword` → `getPassword` (returns `undefined`) against a disposable key, cleaning up after (AC-4.1, AC-1.1)
- Gate the round-trip on service availability (darwin always; Linux only when `DBUS_SESSION_BUS_ADDRESS`/a CI availability flag is set), otherwise run the load assertion only
- Ensure jest discovers the file (co-located `*.test.ts`) so it runs under `npm test` on each matrix Node
- **Verify**: `npm test -- --no-watch --testPathPattern=getKeytar.integration` (load assertion always; full round-trip on macOS returns the stored value then `undefined` after delete)

> Depends on: TASK-2 | Enables: TASK-6 | Parallel with: TASK-3, TASK-4

### TASK-6: Provision the secret service in CI and fail on keystore breakage
[STEP-6 → FR-4, AC-4.1, AC-4.2] | modify `.github/workflows/build-and-test.yml` | Effort: M

> **Intent**: CI runs on headless `cai-standard-x86-linux`, which has no secret service by default, so the round-trip would skip on Linux. Installing `libsecret` + `gnome-keyring` and running the keystore test under `dbus-run-session` with an unlocked keyring lets the full round-trip execute on every matrix Node (20/22/24); the binding-load assertion is the guaranteed floor. A keystore failure now fails the build instead of shipping green.

- Add a workflow step to install `libsecret-1-0`, `libsecret-tools`, `gnome-keyring`, and `dbus-x11` on the Linux runner before the test step
- Run the test suite (or a dedicated keystore step) under `dbus-run-session -- sh -c '...unlock gnome-keyring...; npm test -- --no-watch'`, exporting the secret-service availability flag consumed by TASK-5
- Ensure a non-zero exit from the keystore test fails the matrix job (no `|| true`) across Node 20/22/24
- **Verify**: `python3 -c "import yaml; yaml.safe_load(open('.github/workflows/build-and-test.yml'))"` exits 0; on a pushed branch the keystore round-trip runs green on 20/22/24 and goes red when the `@napi-rs/keyring` import is deliberately broken

> Depends on: TASK-5 | Enables: TASK-8 | Parallel with: —

---

## Bundle 4: Documentation and Release Notes [P]
> Phase: Docs | Parallel: yes (file-disjoint from Bundle 3) | Files: changelog.txt

### TASK-7: Update changelog.txt and release note
[STEP-7 → FR-3, AC-3.1] | modify `changelog.txt` | Effort: XS

> **Intent**: Project convention requires a `changelog.txt` entry before opening a PR. The entry must call out the `engines` floor bump to `>=20` as a soft breaking change for EOL Node 16/18 users, the keytar → `@napi-rs/keyring` swap, and the retained Linux `libsecret` runtime requirement.
> **Standards**: Update `changelog.txt` before opening a PR (CLAUDE.md).

- Add a `changelog.txt` entry: replaced keytar with `@napi-rs/keyring` (fixes keychain storage failing on Node 24), honest keystore error reporting, `engines.node` now `>=20` (Node 16/18 no longer supported), Linux still requires `libsecret` at runtime
- **Verify**: `grep -iq "napi-rs/keyring" changelog.txt && grep -Eiq "engines|node.*20|keychain" changelog.txt && grep -iq "libsecret" changelog.txt`

> Depends on: TASK-4 | Enables: TASK-8 | Parallel with: TASK-5, TASK-6

---

## Bundle 5: Verification
> Phase: Verification | Parallel: no | Files: (full suite; may modify src/lib/getKeytar.ts if a migration is needed)

### TASK-8: Full suite, build, and empirical migration check
[STEP-8 → FR-1, AC-1.5 | All FRs] | execute test suite + manual keychain check | Effort: S

> **Intent**: Confirms zero regressions and validates the one thing that can't be unit-tested: that a token written by the old keytar build under `alkscli`/`alkstoken` is still readable by `@napi-rs/keyring` after the swap (AC-1.5). If it is not, the default is to add a one-time read-old-write-new migration in `getKeytar.ts` before release (Open Question 1).

- Run `npm run build` (clean typecheck) and `npm test -- --no-watch` (full suite green, including the new keystore tests)
- On macOS, with a keytar-written entry present (service `alkscli`, account `alkstoken`), confirm the new adapter reads it back with no re-auth prompt (AC-1.5); if it fails, add a one-time migration in `src/lib/getKeytar.ts` with a covering test and re-run
- Confirm `alks developer configure` stores to the OS keychain (Keychain Access: service `alkscli`, account `alkstoken`) with no "No keychain could be found" message and no forced plaintext fallback
- **Verify**: `npm run build && npm test -- --no-watch` exit 0; on macOS the pre-existing `alkscli`/`alkstoken` entry's value is returned unchanged by `getTokenFromKeystore` with no credential/re-auth prompt shown

> Depends on: TASK-3, TASK-6, TASK-7 | Enables: — | Parallel with: —

---

## Conflict Analysis

> Note: Covers explicitly declared file paths only. Implicit touches (barrel files, shared configs, type re-exports) may require manual sequencing during execution.

| Hot File | Touched By | Strategy |
|----------|------------|----------|
| package.json | TASK-1 (Bundle 1), TASK-4 (Bundle 2) | Sequential — TASK-4 depends on TASK-1; never run in parallel |
| src/lib/getKeytar.ts | TASK-2 (Bundle 2), TASK-8 (Bundle 5, conditional migration only) | Sequential — TASK-8 runs last; edits only if the migration check fails |

No parallel-marked tasks share a file: within Bundle 2, TASK-3 (storeToken.ts), TASK-4 (package.json), and TASK-5 (getKeytar.integration.test.ts, in Bundle 3) are file-disjoint. Bundle 4 (changelog.txt) is disjoint from Bundle 3 and may run in parallel [P].

---

## Architecture Decisions

See: spec-driven/alks-cli-keystore-keytar-replacement/plan.md § Architecture Decisions

---

## Effort Legend

| Size | Guideline |
|------|-----------|
| XS | < 1 hour: trivial change, single function/config |
| S | 1-4 hours: small feature, single file |
| M | 4-8 hours: moderate feature, 2-3 files |
| L | 1-3 days: significant feature, multiple files |
| XL | 3+ days: complex feature — should be split |
