Skip to content

fix(gastown): propagate KILOCODE_TOKEN refresh to running containers#1759

Merged
jrf0110 merged 3 commits intogastown-stagingfrom
gt/birch/4df0c006
Mar 31, 2026
Merged

fix(gastown): propagate KILOCODE_TOKEN refresh to running containers#1759
jrf0110 merged 3 commits intogastown-stagingfrom
gt/birch/4df0c006

Conversation

@jrf0110
Copy link
Copy Markdown
Contributor

@jrf0110 jrf0110 commented Mar 31, 2026

Summary

Fixes KILOCODE_TOKEN not being refreshed on running containers (#1472). Closes 4 propagation gaps where token updates were not reaching the container, plus adds proactive alarm-based refresh:

  1. syncConfigToContainer() — Added KILOCODE_TOKEN to the env mapping so config updates propagate to the container via setEnvVar.
  2. LIVE_ENV_KEYS — Added KILOCODE_TOKEN so model hot-swaps read the current process.env value instead of the stale startup snapshot.
  3. CONFIG_ENV_MAP — Added kilocode_tokenKILOCODE_TOKEN mapping in the model update handler so process.env stays current before SDK server restart.
  4. refreshContainerToken mutation — Now also remints KILOCODE_TOKEN via mintKilocodeToken and pushes it via updateTownConfig + syncConfigToContainer, so the "Refresh Token" button actually resolves 401s.
  5. Alarm loop — Added refreshKilocodeTokenIfExpiring() that checks daily and proactively remints when within 7 days of the 30-day expiry, using decoded JWT payload claims to reconstruct user identity.

Verification

  • Code review: all additions follow existing patterns (CONFIG_ENV_MAP, LIVE_ENV_KEYS, syncConfigToContainer env mapping)
  • Verified imports resolve to existing exports (generateKiloApiToken, resolveSecret, mintKilocodeToken, userFromCtx)
  • Verified function signatures match call sites
  • No build artifacts or generated files in diff
  • JWT decoding uses Zod validation at IO boundary per project conventions

Visual Changes

N/A

Reviewer Notes

  • The refreshKilocodeTokenIfExpiring method decodes the JWT payload without signature verification — this is intentional since the DO is reading its own stored token to extract claims for re-signing. The Zod schema validates the parsed payload shape.
  • The throttle (lastKilocodeTokenCheckAt) resets on DO restart, which is fine — worst case is an extra check on first alarm after restart.
  • All 4 propagation fixes are simple 1-line additions to existing arrays/sets; the alarm-based refresh is the only substantial new code (~70 lines).

jrf0110 added 3 commits March 31, 2026 01:38
…sh and model swap

Close 4 gaps where KILOCODE_TOKEN was never pushed to a running
container after initial agent spawn, causing 401s after the 30-day
JWT expired:

1. syncConfigToContainer() now includes KILOCODE_TOKEN in the env
   mapping so config updates propagate to the container.
2. LIVE_ENV_KEYS includes KILOCODE_TOKEN so model hot-swaps use the
   current process.env value instead of the stale startup snapshot.
3. CONFIG_ENV_MAP in the model update handler syncs kilocode_token
   to process.env before the SDK server restarts.
4. refreshContainerToken tRPC mutation now remints KILOCODE_TOKEN
   and pushes it via updateTownConfig + syncConfigToContainer.
5. Alarm loop proactively remints KILOCODE_TOKEN when within 7 days
   of its 30-day expiry, preventing silent expiration.

Closes #1472
@kilo-code-bot
Copy link
Copy Markdown
Contributor

kilo-code-bot bot commented Mar 31, 2026

Code Review Summary

Status: 2 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 2
SUGGESTION 0
Issue Details (click to expand)

WARNING

File Line Issue
cloudflare-gastown/src/dos/Town.do.ts 3380 Proactive token remint only updates stored container env, so the running SDK server keeps the expired KILOCODE_TOKEN until restart/model swap.
cloudflare-gastown/src/trpc/router.ts 1025 Manual refresh re-mints kilocode_token as the current caller instead of the town credential owner, which can overwrite owner-scoped org credentials.

Fix these issues in Kilo Cloud

Other Observations (not in diff)

N/A

Files Reviewed (4 files)
  • cloudflare-gastown/src/dos/Town.do.ts - 1 issue
  • cloudflare-gastown/src/trpc/router.ts - 1 issue
  • cloudflare-gastown/container/src/control-server.ts - 0 issues
  • cloudflare-gastown/container/src/process-manager.ts - 0 issues

Reviewed by gpt-5.4-20260305 · 1,634,757 tokens

jrf0110 added a commit that referenced this pull request Mar 31, 2026
… by owner

Address PR #1759 review comments:

1. KILOCODE_TOKEN now propagated to the running container process:
   - Extended POST /refresh-token to accept optional kilocodeToken field
   - Added pushKilocodeTokenToContainer() helper in container-dispatch.ts
   - syncConfigToContainer() now pushes KILOCODE_TOKEN via /refresh-token
   - Added KILOCODE_TOKEN to LIVE_ENV_KEYS (model hot-swap) and
     CONFIG_ENV_MAP (model update handler)

2. refreshContainerToken mutation now guards KILOCODE_TOKEN remint:
   - Only remints if the caller is the town owner (credentialUserId === user.id)
   - For org members, pushes the existing owner token instead of
     overwriting with the caller's identity
@jrf0110 jrf0110 changed the base branch from main to gastown-staging March 31, 2026 15:18
@jrf0110 jrf0110 merged commit f1b8b72 into gastown-staging Mar 31, 2026
16 checks passed
@jrf0110 jrf0110 deleted the gt/birch/4df0c006 branch March 31, 2026 15:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant