Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions docs/contributing/architecture/data-storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -1170,13 +1170,13 @@ on write unless a migration backfills existing rows.
- `published_bundle_artifacts.dependencies_json` (`0001-squashed-init.sql`)
stores package dependency pointers queried with SQLite JSON functions in
`packages/worker/src/repo/published-bundle-artifacts-repo.ts`.
- `package_invocation_tokens.package_ids_json`,
`package_invocation_tokens.package_kody_ids_json`,
`package_invocation_tokens.export_names_json`, and
`package_invocation_tokens.sources_json` (`0001-squashed-init.sql`) store
invocation-token scope projections. Keyed invocation replay lives in the
RunLog Durable Object ledger (see [Run records](./run-records.md)); the
current D1 schema has no `package_invocations` table.
- `package_invocation_tokens.export_names_json` and
`package_invocation_tokens.sources_json`
(`0017-package-owned-invocation-tokens.sql`) store per-package
invocation-token scope projections. Each token row also has a required
`package_id`. Keyed invocation replay lives in the RunLog Durable Object
ledger (see [Run records](./run-records.md)); the current D1 schema has no
`package_invocations` table.
- `webhook_endpoints` (`0001-squashed-init.sql`) stores per-user minted URL
state for `package.json#kody.webhooks`, keyed by
`(user_id, package_id, webhook_name)`. URL secrets are SHA-256 hashed;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 0026: Package-owned invocation tokens

- **Status:** accepted
- **Date:** 2026-08-19

## Context

Package invocation tokens let a trusted external client call
`POST /@:username/api/package-invocations/:kodyId/:exportName`. The HTTP path
already names the owner and the package. The token table nevertheless stored
cross-package JSON allowlists (`package_ids_json`, `package_kody_ids_json`,
including `*`), denormalized user identity, and a global unique `token_hash`
index. Auth hashed the bearer and looked up across every user, then checked that
the path matched the grant. A standalone `/account/package-invocation-tokens`
page existed to edit those grants.

That model treated a token as a personal access token. Webhooks already do the
opposite: resolve `@username` and the package from the URL, then compare a
secret on that package's row. Production had seven token rows across three
users; the multi-package and `*` rows were launchers and deploy clients, not a
reason to keep a grant table.

## Decision

An invocation token belongs to exactly one saved package. Auth resolves the
owner and package from the URL, then looks up
`(user_id, package_id, token_hash)`. There is no cross-package grant, no
account-level token page, and no global hash index. Export and source allowlists
stay as restrictions _within_ that package. Tokens are created, rotated, and
revoked from the package details page.

Cross-package HTTP clients call one package (an orchestrator or a discovery
package) and use `packages.invoke` inside Kody, or they speak MCP. They do not
hold a wildcard bearer.

## Consequences

Package delete removes that package's tokens. The same raw bearer may exist on
two packages only if two rows share the hash. `last_used_at` is a `waitUntil`
write and does not gate authentication. Identity for audit comes from the
resolved user, not columns on the token row. MCP lists tokens for one package;
`package_get` includes the same metadata.

The 0017 migration maps single-package grants, expands concrete multi-package
grants into one row per owned package (same hash, new ids), and drops `*` and
other unmapped rows. A row that lists packages in both legacy arrays is treated
as multi-package and exploded from the union. Rewrite `*` rows to a concrete
owned package (typically `raycast`) before 0017 runs. Do not apply 0017 on
production until the new worker is deployed: older workers still read the grant
columns. Cross-package HTTP launchers call the `raycast` package's `invoke`
export, which uses `packages.invoke` internally.
1 change: 1 addition & 0 deletions docs/contributing/decisions/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ behavior (see [documentation principles](../documentation.md)).
- [0023 — Progressive search disclosure](./0023-progressive-search-disclosure.md)
- [0024 — Packages outrank synthesized providers](./0024-packages-outrank-synthesized-providers.md)
- [0025 — No package services primitive](./0025-no-package-services-primitive.md)
- [0026 — Package-owned invocation tokens](./0026-package-owned-invocation-tokens.md)
79 changes: 39 additions & 40 deletions docs/contributing/package-invocation-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,91 +33,88 @@ Authentication uses a private bearer token stored in Kody's database-backed
package invocation token table.

Kody stores only the token hash for request-time lookup. The raw bearer token is
generated by a trusted client or operator, pasted into the account token
creation flow once, and then sent by the trusted external service as:
generated by a trusted client or operator, pasted into the package details token
form once, and then sent by the trusted external service as:

```http
Authorization: Bearer <raw-token>
```

Each token belongs to exactly one saved package. Auth resolves the owner and
package from the URL, then looks up `(user_id, package_id, token_hash)`.

Each token row includes:

- token id and human-readable name
- owning `user_id`, `email`, and `display_name`
- package scope (`package_ids_json` and/or `package_kody_ids_json`)
- owning `user_id` and `package_id`
- allowed package exports (`export_names_json`)
- allowed request sources (`sources_json`)
- `last_used_at`
- `revoked_at`

The token is not a global backdoor:

- package lookup is user-owned
- package access is scoped by the token row
- export access requires an explicit allowlist
- the path names the owner and package
- the bearer proves access to that package only
- export access requires an explicit allowlist (including per-package `*`)
- `source` metadata is checked against the allowlist when provided
- tokens can be revoked without deploys
- execution uses normal package runtime machinery
- `last_used_at` is a best-effort write and does not gate authentication

### Managing tokens

Signed-in users manage package invocation tokens from:

- `/account/package-invocation-tokens`
- `/account/package-invocation-tokens/new`
Signed-in users manage invocation tokens on the package details page at
`/account/packages/:packageId`.

The creation form accepts the raw token exactly once, hashes it server-side, and
stores only the hash. Token list/detail payloads never return the raw token or
token hash.

The backing same-origin JSON endpoint is:

- `GET /account/package-invocation-tokens.json` — list owned packages and token
metadata
- `POST /account/package-invocation-tokens.json` with `action: "create"` —
create a token from a raw token and allowlists
- `POST /account/package-invocation-tokens.json` with `action: "update"` —
update token name, allowlists, and optionally replace the stored token hash
from a new raw token
- `POST /account/package-invocation-tokens.json` with `action: "revoke"` —
revoke a token by id
- `POST /account/package-invocation-tokens.json` with `action: "reinstate"` —
reinstate a revoked token by id
- `POST /account/package-invocation-tokens.json` with `action: "delete"` —
permanently delete a token row by id
- `GET /account/packages.json` — list packages; include `selected=<packageId>`
to load that package's token metadata
- `POST /account/packages.json` with `action: "create-token"` — create a token
for one owned package
- `POST /account/packages.json` with `action: "update-token"` — update token
name, allowlists, and optionally replace the stored token hash from a new raw
token
- `POST /account/packages.json` with `action: "revoke-token"` — revoke a token
by id
- `POST /account/packages.json` with `action: "reinstate-token"` — reinstate a
revoked token by id
- `POST /account/packages.json` with `action: "delete-token"` — permanently
delete a token row by id

Create payload shape:

```json
{
"action": "create",
"action": "create-token",
"packageId": "<saved-package-id>",
"name": "Trusted external client",
"rawToken": "<raw-token>",
"packageKodyIds": ["*"],
"packageIds": [],
"exportNames": ["*"],
"sources": ["trusted-client"]
}
```

Concrete `packageIds` and `packageKodyIds` must refer to packages owned by the
signed-in user. Package and export allowlists support the deliberate wildcard
value `*`. Source values do not use wildcard matching: if a request includes
Export allowlists support the deliberate wildcard value `*` for every export on
that package. Source values do not use wildcard matching: if a request includes
`source`, that exact value must be present in the token's `sources` list.

The `/new` page supports metadata prefill query parameters for setup flows:
Package details support metadata prefill query parameters for setup flows:

- `newToken=1` opens the create form
- `name`
- `packageKodyId` / `packageKodyIds` / `kodyId` / `kodyIds`
- `packageId` / `packageIds`
- `exportName` / `exportNames`
- `allowedSource` / `allowedSources` / `source` / `sources`

Snake case and kebab case aliases are accepted for list fields, such as
`package_kody_ids`, `package-kody-ids`, `export_names`, `export-names`,
`allowed_sources`, and `allowed-sources`. Repeat parameters or comma-separate
values. Do not put the raw token in the URL; paste it into the form or submit it
over the authenticated same-origin JSON API.
`export_names`, `export-names`, `allowed_sources`, and `allowed-sources`. Repeat
parameters or comma-separate values. Do not put the raw token in the URL; paste
it into the form or submit it over the authenticated same-origin JSON API.

When editing an existing token in the account UI, the raw token field is
write-only and optional: leave it blank to preserve the current token hash, or
Expand Down Expand Up @@ -284,13 +281,15 @@ Recommended flow for a trusted external client:
2. copy the raw token to the clipboard or keep it in the client's local secret
storage
3. open Kody at
`/account/package-invocation-tokens/new?name=Trusted%20External%20Client&packageKodyIds=*&exportNames=*&sources=trusted-client`
`/account/packages/<packageId>?newToken=1&name=Trusted%20External%20Client&exportNames=*&sources=trusted-client`
4. paste the raw token into the form and create the token
5. send invocation requests with `Authorization: Bearer <raw-token>` and
`"source": "trusted-client"`

A wildcard token can call any package/export owned by the signed-in Kody user,
but only for requests that identify themselves with an allowed source.
A token with `exportNames: ["*"]` can call any export on that package, but only
for requests that identify themselves with an allowed source when sources are
scoped. Cross-package callers invoke one package (an orchestrator or discovery
package) and use `packages.invoke` inside Kody, or they speak MCP.

## Related

Expand Down
6 changes: 3 additions & 3 deletions docs/contributing/setup-manifest.md
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,8 @@ Configure these GitHub Actions secrets and variables for workflows:
uses it to `POST`
`https://kody.codes/@kentcdodds/api/package-invocations/weekly-site-perf/__root__`.
Create a token scoped to `weekly-site-perf` / `.` / source `weekly-site-perf`
at `/account/package-invocation-tokens/new`. Not a Worker secret; the weekly
job skips invoke when this is unset.)
on that package's details page. Not a Worker secret; the weekly job skips
invoke when this is unset.)
- `SENTRY_AUTH_TOKEN` (optional GitHub **secret**; Sentry auth token with
`project:releases` / source map upload permissions — used only by CI to run
`npm run sentry:upload-sourcemaps` after deploy)
Expand Down Expand Up @@ -624,7 +624,7 @@ How to get/set each value:
sync a different worker secret than production.
- `KODY_PACKAGE_INVOCATION_TOKEN` (optional)
- In Kody: open
`/account/package-invocation-tokens/new?name=Weekly%20site%20perf&packageKodyIds=weekly-site-perf&exportNames=.&allowedSources=weekly-site-perf`,
`/account/packages/<weekly-site-perf-package-id>?newToken=1&name=Weekly%20site%20perf&exportNames=.&allowedSources=weekly-site-perf`,
generate a raw token in the browser, and store that value as the repository
secret `KODY_PACKAGE_INVOCATION_TOKEN`. Scope it to the `weekly-site-perf`
package and source `weekly-site-perf`. The weekly workflow uses it only to
Expand Down
5 changes: 3 additions & 2 deletions docs/contributing/weekly-site-perf.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ package. The agent implements an obvious local fix when it can, follows
[`.agents/skills/ship-pr/SKILL.md`](../../.agents/skills/ship-pr/SKILL.md), or
leaves the tracking issue open when a human should decide.

Create a scoped invocation token at
`https://kody.codes/account/package-invocation-tokens/new?name=Weekly%20site%20perf&packageKodyIds=weekly-site-perf&exportNames=.&allowedSources=weekly-site-perf`
Create a scoped invocation token on the `weekly-site-perf` package details page
(`exportNames=.`, source `weekly-site-perf`) at
`https://kody.codes/account/packages/<weekly-site-perf-package-id>?newToken=1&name=Weekly%20site%20perf&exportNames=.&allowedSources=weekly-site-perf`
and store the raw value as the repository (or org) secret
`KODY_PACKAGE_INVOCATION_TOKEN`. Do not put the raw token in the URL. See
[setup manifest](./setup-manifest.md).
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ content:
| [values.md](./values.md) | Do not write new values; map existing rows to memories, package storage, repos, secrets, or integrations |
| [oauth.md](./oauth.md) | **Start here** for third-party OAuth (`/connect/oauth`, redirect URI, params) |
| [account-secret-setup.md](./account-secret-setup.md) | `/account/secrets/new` URL parameters and policies |
| [account-package-invocation-token-setup.md](./account-package-invocation-token-setup.md) | `/account/package-invocation-tokens/new` URL parameters and bearer-token safety policy |
| [account-package-invocation-token-setup.md](./account-package-invocation-token-setup.md) | `/account/packages/:packageId?newToken=1` URL parameters and bearer-token safety policy |

## Provider guides

Expand Down
Loading
Loading