Skip to content

Tokens live in state.db on both daemon kinds #758

Closed

Description

@V3RON

Part of #754.

Scope

A worker and a gateway keep their tokens in state.db, one row per token, and refuse to start while a tokens.json sits in their folder. Creating, listing, revoking and checking a token work as today.

In short

simlock token create, list and revoke, and every token check (socket hello, HTTP bearer, the gateway's uplink join token), read and write the tokens table in the daemon's state.db instead of tokens.json. A create or a revoke is one transaction, so two at once never lose one another. A daemon that finds tokens.json refuses with the feature's old-file message, naming <data directory>/tokens.json, with its role's upgrade step: a gateway's says to create each worker's join token again and set it as that worker's gateway.token, since a gateway refuses nuke.run (src/gateway/dispatcher.ts:238).

flowchart LR
  C[token create / revoke] --> T["tokens table (changed)"]
  V[hello, HTTP, uplink check] --> T
  S[daemon start] --> O{"tokens.json present? (new)"}
  O -->|yes| X[refuse]
Loading

If this goes wrong, a user would notice a token that stops working after a restart, or a revoked one that still works.

Technical spec

Facts

Commit 8db8112b.

# Fact Source
F1 TokenStore reads and rewrites the whole tokens.json array per create and revoke, behind an in-process queue (#mutation), mode 0600 src/auth/token-store.ts:23-111
F2 verify re-reads the file on every call src/auth/token-store.ts:73-77
F3 TokenRecord: id (tok_<id>), hash, role (one of TOKEN_ROLES), label?, createdAt src/auth/token-store.ts:41-48, :121-131; src/auth/tokens.ts
F4 Bad JSON throws Invalid JSON in token store: <path> (<reason>), a bad shape Invalid token store: <path> (TokenStoreError); it answers INTERNAL on HTTP and the socket src/auth/token-store.ts:95-104; src/daemon/error-code.ts:80-96; src/http/errors.ts:151; src/daemon/server.ts:1658
F5 createAuthFrom builds the store at join(dataDirectory, "tokens.json"); Auth.tokens delegates create, list, revoke; resolveHelloRole and authenticateUplink call verify src/auth/index.ts:59-65, :97, :103, :109-111
F6 createAuth is built before the mode branch, so both daemon kinds use it src/daemon/main.ts:222
F7 auth imports only ports and contract .dependency-cruiser.mjs:186-188; src/auth/AGENTS.md "Uses: ports, contract only"
F8 The CLI never writes tokens.json; token commands go through the daemon src/cli/index.test.ts:2846-2847; src/contract/operations.ts:685-687
F9 Tests that build a TokenStore or createAuth over MemoryFilesystem: src/auth/token-store.test.ts, auth.test.ts, proxy.test.ts, src/cli/index.test.ts:7173, :7286, src/daemon/dispatcher.test.ts:342, src/daemon/server.test.ts:3779, src/http/app.test.ts:2483, src/http/auth.test.ts:122-136 rg -l "TokenStore|createAuth" src -g '*.test.ts'
F10 Tests that write tokens.json directly: src/daemon/main.test.ts:1342, :1731, src/agent-device/gateway-front.test.ts:189, src/agent-device/edge.test.ts:156; e2e reads it at e2e/token-route.test.ts:100 as cited
F11 e2e creates tokens through token create e2e/http-api.test.ts:73, e2e/gateway-fleet.test.ts:145
F12 Existing store tests: concurrent creates (:97), concurrent revokes (:132), verify sees another instance's token (:177), corrupt file fails loudly (:213, :223), mode 0600 (:70) src/auth/token-store.test.ts
F13 Task 1 adds SqliteSchemaOwner, the transaction helper in src/ports/sqlite.ts, the owner and old-file lists, and the messages #754 Shared values

Modules touched

Searches: TokenStore, tokens.json, createAuth(, TokenStoreError over src/, e2e/, docs/.

  • src/auth/token-store.ts, src/auth/index.ts (createAuth takes database: SqliteDatabase; exports tokensOwner), src/auth/testing.ts.
  • src/daemon/main.ts: tokensOwner in both owner lists, tokens.json in both old-file lists, state.database into createAuth.
  • Tests of F9 and F10; e2e/token-route.test.ts reads hashes from state.db; src/daemon/gateway-fleet.e2e.test.ts (its restart test).
  • Comments and titles naming tokens.json (rg -n "tokens\.json" src docs e2e --glob '!docs/internal/adr/**'): src/core/instance-identity.ts:6, src/ports/logger.ts:121, src/gateway/drain-store.ts:16, src/daemon/dispatcher.ts:182, src/contract/operations.ts:685-687, src/daemon/gateway-fleet.e2e.test.ts:164, :406, e2e/http-api.test.ts:71, e2e/token-route.test.ts:10, and the test title src/cli/index.test.ts:2846, which becomes "CLI: token operations go through the daemon" with the same assertions.
  • Docs: docs/CONFIGURATION.md:200, docs/HTTP-API.md:55-58, docs/CLI.md:2732 and the upgrade section (tokens are created again; on a gateway every worker's gateway.token is replaced with a new join token, or the worker is refused with 401 and keeps redialling), src/auth/AGENTS.md, src/http/AGENTS.md:95, src/gateway/AGENTS.md:122, src/daemon/AGENTS.md.
flowchart LR
  main["daemon/main.ts *"] --> state[state]
  main --> auth["auth createAuth *"]
  auth --> ts["TokenStore *"]
  ts --> ports[ports/sqlite.ts]
Loading

Surface and extension points

createAuth(options) takes database: SqliteDatabase in place of filesystem for tokens. AdminSecretManager keeps its filesystem, since admin.token stays a file. src/auth/index.ts exports tokensOwner. Owner tokens, version 1: table tokens with id (key), hash (unique), role (CHECK one of TOKEN_ROLES), label (NULL when absent), created_at. list answers in creation order (ORDER BY rowid), as the array order does today.

Contract and event changes

None (searched src/contract for TokenRecord, tokens.json: the doc comment at src/contract/operations.ts:685-687 changes words only).

Other code on the same state

Searched tokens.json, TokenStore, verify( over src/: auth only writes it (F8). token.revoke closes the revoked token's uplinks (src/gateway/AGENTS.md "token.revoke closes its uplinks"), unchanged. Task 4 adds the gateway's own owner to the same file.

Failure modes

Call Outcome What happens Limit, counted from
create, revoke ok one transaction —
SQLite throws rollback; the call fails INTERNAL, as a failed write does today busy wait 5 s, from the statement
verify, list ok one indexed read per call —
SQLite throws the request, hello or uplink join fails INTERNAL, as a failed file read does today busy wait 5 s, from the statement
a bad role cannot be stored: the CHECK refuses it at insert, so TokenStoreError and its parse checks go —
start tokens.json present refuse with the old-file message —

Two calls at once: each runs as one synchronous transaction, so neither can see the other half done.

Rules in play

  • ADR 0036 §1, tokens in state.db on both kinds; §5, refuse on an old file, no import.
  • ADR 0003, "no CLI code reads tokens.json itself" (F8): the daemon stays the only owner.
  • src/auth/AGENTS.md, auth "Uses: ports, contract only": it takes a SqliteDatabase, never the state module.
  • Architecture rule 8, the daemon owns all state.

Tests

Seams: TokenStore over openMemoryStateDatabase([tokensOwner]) (model: src/auth/token-store.test.ts), and startDaemon (model: src/daemon/main.test.ts:1342).

  • Every existing test in src/auth/token-store.test.ts keeps its claim on the table, including the creation order of :87 (create three, list them in that order), except the file-only cases at :45, :206, :213, :223 and the mode test at :70, which go: task 1 proves the file's mode and its damage rule.
  • A token created, then the daemon restarted on the same state.db (temp file), verifies and lists with every field, including no label when none was given.
  • startDaemon as a worker and as a gateway with tokens.json in its folder throws the old-file message naming it, each with its role's step word for word.
  • A gateway restarted on the same state.db still accepts a worker join token created before the stop: extend src/daemon/gateway-fleet.e2e.test.ts:410, whose workers reconnect with tokens made before the restart, to run on state.db.
  • GET /v1/tokens never answers a secret or a hash reads the stored hashes from state.db (extends e2e/token-route.test.ts:95).

Done when

  • pnpm test:changed passes.
  • pnpm test:e2e e2e/token-route.test.ts e2e/http-api.test.ts passes.
  • rg -n "tokens\.json" src docs --glob '!docs/internal/adr/**' finds only the old-file lists, their tests and the docs/CLI.md upgrade section.

Out of scope

  • admin.token (stays a file).
  • Importing tokens.json.

Depends on

Written by an agent.

Activity

  1. added
    task:draft
    on Oct 20, 2026
  2. changed the title [-]iOS and Android device data in their own state.db tables[/-] [+]Tokens live in state.db on both daemon kinds[/+] on Oct 20, 2026
  3. added
    task:ready
    and removed
    task:draft
    on Oct 20, 2026
  4. self-assigned this
    on Oct 20, 2026
  5. added a commit that references this issue on Oct 20, 2026
    fb86b85
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

task:ready

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions