From eb4df315c1f8f461b80b78a4e35ea4cda5f916b2 Mon Sep 17 00:00:00 2001 From: Josh Lambert Date: Fri, 29 May 2026 17:52:32 -0400 Subject: [PATCH 1/6] fix(cli): sync headless sessions authenticated by API key --- .changeset/sync-headless-sessions.md | 5 ++ .../pages/code-with-ai/platforms/cli.md | 2 + .../src/kilo-sessions/kilo-sessions.ts | 3 + .../test/kilocode/kilo-sessions.test.ts | 89 +++++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 .changeset/sync-headless-sessions.md diff --git a/.changeset/sync-headless-sessions.md b/.changeset/sync-headless-sessions.md new file mode 100644 index 000000000000..2ebcaa2a6e50 --- /dev/null +++ b/.changeset/sync-headless-sessions.md @@ -0,0 +1,5 @@ +--- +"@kilocode/cli": patch +--- + +Sync headless CLI sessions to Kilo session history when authenticated with `KILO_API_KEY`. diff --git a/packages/kilo-docs/pages/code-with-ai/platforms/cli.md b/packages/kilo-docs/pages/code-with-ai/platforms/cli.md index 7a647f2e3e2a..6de671567080 100644 --- a/packages/kilo-docs/pages/code-with-ai/platforms/cli.md +++ b/packages/kilo-docs/pages/code-with-ai/platforms/cli.md @@ -487,6 +487,8 @@ This instructs the AI to proceed without user input. kilo run "Implement the new feature" --auto ``` +When `KILO_API_KEY` is available, non-interactive sessions sync to your private Kilo session history so they can be inspected after a CI run. Set `KILO_DISABLE_SESSION_INGEST=1` for automation that should call the Kilo Gateway without retaining its session transcript in Kilo. + ## Session Continuation Resume your last conversation from the current workspace using the `--continue` (or `-c`) flag: diff --git a/packages/opencode/src/kilo-sessions/kilo-sessions.ts b/packages/opencode/src/kilo-sessions/kilo-sessions.ts index d8a67c402c67..ee7a62a3b5ce 100644 --- a/packages/opencode/src/kilo-sessions/kilo-sessions.ts +++ b/packages/opencode/src/kilo-sessions/kilo-sessions.ts @@ -107,6 +107,9 @@ export namespace KiloSessions { if (auth?.type === "api" && auth.key.length > 0) return auth.key if (auth?.type === "oauth" && auth.access.length > 0) return auth.access if (auth?.type === "wellknown" && auth.token.length > 0) return auth.token + + const key = process.env["KILO_API_KEY"]?.trim() + if (key) return key return undefined }) } diff --git a/packages/opencode/test/kilocode/kilo-sessions.test.ts b/packages/opencode/test/kilocode/kilo-sessions.test.ts index 64938ff002da..321dada73d97 100644 --- a/packages/opencode/test/kilocode/kilo-sessions.test.ts +++ b/packages/opencode/test/kilocode/kilo-sessions.test.ts @@ -5,6 +5,7 @@ import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner" import { Auth } from "../../src/auth" import { Bus } from "../../src/bus" import type { Config } from "../../src/config/config" +import { clearInFlightCache } from "../../src/kilo-sessions/inflight-cache" import { KiloSessions } from "../../src/kilo-sessions/kilo-sessions" import { ProjectID } from "../../src/project/schema" import { Session } from "../../src/session/session" @@ -25,6 +26,12 @@ function layer(overrides: Partial = {}) { ) } +function reset(...tokens: string[]) { + clearInFlightCache("kilo-sessions:token") + clearInFlightCache("kilo-sessions:client") + for (const token of tokens) clearInFlightCache(`kilo-sessions:token-valid:${token}`) +} + it.instance("initializes once per instance through Config.Service", () => { let reads = 0 @@ -46,6 +53,86 @@ it.instance("initializes once per instance through Config.Service", () => { ) }) +it.instance("bootstraps session ingest from KILO_API_KEY without stored auth", () => { + const original = process.env.KILO_API_KEY + const calls: string[] = [] + const fetch: typeof globalThis.fetch = Object.assign( + async (input: RequestInfo | URL, init?: RequestInit) => { + const url = String(input) + if (url.endsWith("/api/user")) { + calls.push(new Headers(init?.headers).get("Authorization") ?? "") + return new Response("{}", { status: 200 }) + } + if (url.endsWith("/api/session")) { + calls.push(new Headers(init?.headers).get("Authorization") ?? "") + return Response.json({ id: "remote-env", ingestPath: "/api/ingest/env" }) + } + return new Response("{}", { status: 200 }) + }, + { preconnect: globalThis.fetch.preconnect }, + ) + const request = spyOn(globalThis, "fetch").mockImplementation(fetch) + + process.env.KILO_API_KEY = "env-token" + reset("env-token") + + return Effect.promise(() => KiloSessions.bootstrap("session-env")).pipe( + Effect.andThen(() => Effect.sync(() => expect(calls).toEqual(["Bearer env-token", "Bearer env-token"]))), + Effect.ensuring( + Effect.sync(() => { + if (original === undefined) delete process.env.KILO_API_KEY + else process.env.KILO_API_KEY = original + reset("env-token") + request.mockRestore() + }), + ), + Effect.provide(layer()), + ) +}) + +it.instance("prefers stored auth over KILO_API_KEY for session ingest", () => { + const original = process.env.KILO_API_KEY + const calls: string[] = [] + const fetch: typeof globalThis.fetch = Object.assign( + async (input: RequestInfo | URL, init?: RequestInit) => { + const url = String(input) + if (url.endsWith("/api/user")) { + calls.push(new Headers(init?.headers).get("Authorization") ?? "") + return new Response("{}", { status: 200 }) + } + if (url.endsWith("/api/session")) { + calls.push(new Headers(init?.headers).get("Authorization") ?? "") + return Response.json({ id: "remote-auth", ingestPath: "/api/ingest/auth" }) + } + return new Response("{}", { status: 200 }) + }, + { preconnect: globalThis.fetch.preconnect }, + ) + const request = spyOn(globalThis, "fetch").mockImplementation(fetch) + + process.env.KILO_API_KEY = "env-token" + reset("env-token", "stored-token") + + return Effect.gen(function* () { + const auth = yield* Auth.Service + yield* auth.set("kilo", { type: "api", key: "stored-token" }) + yield* Effect.promise(() => KiloSessions.bootstrap("session-auth")) + expect(calls).toEqual(["Bearer stored-token", "Bearer stored-token"]) + }).pipe( + Effect.ensuring( + Effect.gen(function* () { + const auth = yield* Auth.Service + yield* auth.remove("kilo").pipe(Effect.orDie) + if (original === undefined) delete process.env.KILO_API_KEY + else process.env.KILO_API_KEY = original + reset("env-token", "stored-token") + request.mockRestore() + }), + ), + Effect.provide(layer()), + ) +}) + it.instance("does not duplicate created-session subscribers when init is repeated", () => { const calls: string[] = [] const fetch: typeof globalThis.fetch = Object.assign( @@ -62,6 +149,7 @@ it.instance("does not duplicate created-session subscribers when init is repeate ) const request = spyOn(globalThis, "fetch").mockImplementation(fetch) + reset("test-token") const id = SessionID.descending("session-created") return Effect.gen(function* () { @@ -91,6 +179,7 @@ it.instance("does not duplicate created-session subscribers when init is repeate Effect.gen(function* () { const auth = yield* Auth.Service yield* auth.remove("kilo").pipe(Effect.orDie) + reset("test-token") request.mockRestore() }), ), From 6e8d6f7d5354d5380c165482c6af87baceca07bd Mon Sep 17 00:00:00 2001 From: Josh Lambert Date: Fri, 29 May 2026 18:17:31 -0400 Subject: [PATCH 2/6] docs(cli): clarify API key session sync scope --- .changeset/sync-api-key-sessions.md | 5 +++++ .changeset/sync-headless-sessions.md | 5 ----- packages/kilo-docs/pages/code-with-ai/platforms/cli.md | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) create mode 100644 .changeset/sync-api-key-sessions.md delete mode 100644 .changeset/sync-headless-sessions.md diff --git a/.changeset/sync-api-key-sessions.md b/.changeset/sync-api-key-sessions.md new file mode 100644 index 000000000000..5f4ef5c872ba --- /dev/null +++ b/.changeset/sync-api-key-sessions.md @@ -0,0 +1,5 @@ +--- +"@kilocode/cli": patch +--- + +Sync CLI sessions to Kilo session history when authenticated with `KILO_API_KEY`. diff --git a/.changeset/sync-headless-sessions.md b/.changeset/sync-headless-sessions.md deleted file mode 100644 index 2ebcaa2a6e50..000000000000 --- a/.changeset/sync-headless-sessions.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@kilocode/cli": patch ---- - -Sync headless CLI sessions to Kilo session history when authenticated with `KILO_API_KEY`. diff --git a/packages/kilo-docs/pages/code-with-ai/platforms/cli.md b/packages/kilo-docs/pages/code-with-ai/platforms/cli.md index 6de671567080..ab5a006fa07d 100644 --- a/packages/kilo-docs/pages/code-with-ai/platforms/cli.md +++ b/packages/kilo-docs/pages/code-with-ai/platforms/cli.md @@ -487,8 +487,6 @@ This instructs the AI to proceed without user input. kilo run "Implement the new feature" --auto ``` -When `KILO_API_KEY` is available, non-interactive sessions sync to your private Kilo session history so they can be inspected after a CI run. Set `KILO_DISABLE_SESSION_INGEST=1` for automation that should call the Kilo Gateway without retaining its session transcript in Kilo. - ## Session Continuation Resume your last conversation from the current workspace using the `--continue` (or `-c`) flag: @@ -573,6 +571,8 @@ The CLI supports overriding config values with environment variables. The suppor - For `kilocode` provider: `KILOCODE_` (e.g., `KILOCODE_MODEL` → `kilocodeModel`) - For other providers: `KILO_` (e.g., `KILO_API_KEY` → `apiKey`) +When `KILO_API_KEY` authenticates the CLI, sessions created in both interactive mode and `kilo run` sync to your private Kilo session history. Set `KILO_DISABLE_SESSION_INGEST=1` to call the Kilo Gateway without retaining session transcripts in Kilo. + ## Using the CLI in an Organization If you belong to a Kilo organization (Team or Enterprise), you can route CLI requests through that organization. The process differs slightly between interactive and non-interactive usage. From 5cc89c5898d76c4132325862abb48b0d3ecf8dc8 Mon Sep 17 00:00:00 2001 From: Joshua Lambert <25085430+lambertjosh@users.noreply.github.com> Date: Sat, 30 May 2026 11:44:59 -0400 Subject: [PATCH 3/6] Apply suggestion from @lambertjosh --- packages/kilo-docs/pages/code-with-ai/platforms/cli.md | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/kilo-docs/pages/code-with-ai/platforms/cli.md b/packages/kilo-docs/pages/code-with-ai/platforms/cli.md index ab5a006fa07d..85e86218aff0 100644 --- a/packages/kilo-docs/pages/code-with-ai/platforms/cli.md +++ b/packages/kilo-docs/pages/code-with-ai/platforms/cli.md @@ -571,7 +571,6 @@ The CLI supports overriding config values with environment variables. The suppor - For `kilocode` provider: `KILOCODE_` (e.g., `KILOCODE_MODEL` → `kilocodeModel`) - For other providers: `KILO_` (e.g., `KILO_API_KEY` → `apiKey`) -When `KILO_API_KEY` authenticates the CLI, sessions created in both interactive mode and `kilo run` sync to your private Kilo session history. Set `KILO_DISABLE_SESSION_INGEST=1` to call the Kilo Gateway without retaining session transcripts in Kilo. ## Using the CLI in an Organization From 58f18e32e92f9f838fef662f894a1b90e37d8379 Mon Sep 17 00:00:00 2001 From: Joshua Lambert <25085430+lambertjosh@users.noreply.github.com> Date: Mon, 1 Jun 2026 14:14:30 -0400 Subject: [PATCH 4/6] Apply suggestion from @lambertjosh --- packages/kilo-docs/pages/code-with-ai/platforms/cli.md | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/kilo-docs/pages/code-with-ai/platforms/cli.md b/packages/kilo-docs/pages/code-with-ai/platforms/cli.md index 85e86218aff0..7a647f2e3e2a 100644 --- a/packages/kilo-docs/pages/code-with-ai/platforms/cli.md +++ b/packages/kilo-docs/pages/code-with-ai/platforms/cli.md @@ -571,7 +571,6 @@ The CLI supports overriding config values with environment variables. The suppor - For `kilocode` provider: `KILOCODE_` (e.g., `KILOCODE_MODEL` → `kilocodeModel`) - For other providers: `KILO_` (e.g., `KILO_API_KEY` → `apiKey`) - ## Using the CLI in an Organization If you belong to a Kilo organization (Team or Enterprise), you can route CLI requests through that organization. The process differs slightly between interactive and non-interactive usage. From 04e0df57dc0acc5256ee45ceb69c85cc4d753b8e Mon Sep 17 00:00:00 2001 From: Joshua Lambert <25085430+lambertjosh@users.noreply.github.com> Date: Mon, 1 Jun 2026 14:14:56 -0400 Subject: [PATCH 5/6] Apply suggestion from @lambertjosh --- .changeset/sync-api-key-sessions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/sync-api-key-sessions.md b/.changeset/sync-api-key-sessions.md index 5f4ef5c872ba..55deb5a530ee 100644 --- a/.changeset/sync-api-key-sessions.md +++ b/.changeset/sync-api-key-sessions.md @@ -2,4 +2,4 @@ "@kilocode/cli": patch --- -Sync CLI sessions to Kilo session history when authenticated with `KILO_API_KEY`. +Sync CLI sessions to Kilo session history when authenticated with `KILO_API_KEY`, and make it override other options when present. From 6fd42be1d0fde4939759f8c72f6761d0654a5384 Mon Sep 17 00:00:00 2001 From: Joshua Lambert <25085430+lambertjosh@users.noreply.github.com> Date: Wed, 3 Jun 2026 17:42:33 -0400 Subject: [PATCH 6/6] Update .changeset/sync-api-key-sessions.md Co-authored-by: kilo-code-bot[bot] <240665456+kilo-code-bot[bot]@users.noreply.github.com> --- .changeset/sync-api-key-sessions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/sync-api-key-sessions.md b/.changeset/sync-api-key-sessions.md index 55deb5a530ee..059681c1f577 100644 --- a/.changeset/sync-api-key-sessions.md +++ b/.changeset/sync-api-key-sessions.md @@ -2,4 +2,4 @@ "@kilocode/cli": patch --- -Sync CLI sessions to Kilo session history when authenticated with `KILO_API_KEY`, and make it override other options when present. +Sync CLI sessions to Kilo session history when authenticated with `KILO_API_KEY` when no stored Kilo auth is present.