From b5f0cbc016f021d28962daecb1f52e5a4ebab5f0 Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Sun, 29 Nov 2026 00:57:08 +0530 Subject: [PATCH 1/4] fix(kin): share Claude's static instructions cache across sessions Mark the existing instructions as a reusable prefix and default shared chats to the same cache lifetime as the owner's chat. Record relative prices for known Claude models without activating a cost rule. People-change: none Co-Authored-By: GPT-6.2 Sol --- .../__snapshots__/claude.test.ts.snap | 20 ++--- .../kin/lead/turn/providers/claude.test.ts | 87 +++++++++++++++---- .../src/kin/lead/turn/providers/claude.ts | 18 +++- .../src/kin/lead/turn/providers/provider.ts | 23 +++++ apps/server/src/kin/lead/turn/session.ts | 5 +- 5 files changed, 120 insertions(+), 33 deletions(-) diff --git a/apps/server/src/kin/lead/turn/providers/__snapshots__/claude.test.ts.snap b/apps/server/src/kin/lead/turn/providers/__snapshots__/claude.test.ts.snap index ae35c9fc170f..31d7c990a150 100644 --- a/apps/server/src/kin/lead/turn/providers/__snapshots__/claude.test.ts.snap +++ b/apps/server/src/kin/lead/turn/providers/__snapshots__/claude.test.ts.snap @@ -1,8 +1,8 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`every seat's options match Kin byte for byte > bills 1`] = `"46a15de66b84756a9bd624b07154e4597f718e367c2407c134bbc3a00e8165ab"`; +exports[`every seat's options match Kin byte for byte > bills 1`] = `"f9f3806137a0aa8a13e3e9fb5e5512e6cea3d6d5fe57ff2bc04d3a1f451bc74a"`; -exports[`every seat's options match Kin byte for byte > bills without lookup 1`] = `"cb5394c4dfee139a410fc9195a6eaf10d9cb3d14108bdac1f30c18aaaf6481c3"`; +exports[`every seat's options match Kin byte for byte > bills without lookup 1`] = `"40ffc63a0cfad7f2e031c9ec6a9c02ab364e024bf392f6ed152762c53125f029"`; exports[`every seat's options match Kin byte for byte > delegated 1`] = `"bedb0c0da98aea45f5f50134e988ae1cb0ce4d632d04fc107b0f4a95171ce0a1"`; @@ -10,18 +10,18 @@ exports[`every seat's options match Kin byte for byte > delegated without lookup exports[`every seat's options match Kin byte for byte > dream 1`] = `"7a15b18823863542dfbb28ceb5fbb88fde6a2f57d15b70d911ac1f4d0e5854e6"`; -exports[`every seat's options match Kin byte for byte > owner with lookup 1`] = `"81b4e4fde052ec790760ee4ce07ff47b0060e73c3ded42dd167747fdd7fca26d"`; +exports[`every seat's options match Kin byte for byte > owner with lookup 1`] = `"3ab5d508452b9f2ba7037139f52cf2dae65aa64c9d21230d0edaacbc42160073"`; -exports[`every seat's options match Kin byte for byte > owner without lookup 1`] = `"87e0b5a6ed9300e9ef1edd87774c4fd1ba899800e540fdbd1391ee8cf8c58f5b"`; +exports[`every seat's options match Kin byte for byte > owner without lookup 1`] = `"5a9c84fc86c594e492e99b06e067058a2028cefd1fd87a693acbc213c20d74ad"`; -exports[`every seat's options match Kin byte for byte > review 1`] = `"e8bf99c959c5de4e4e266edc91de2feffb029a57afc8ea2bf3d0cd45b706d024"`; +exports[`every seat's options match Kin byte for byte > review 1`] = `"3284efc92724be5972b6cc2fb07e2e03ca2c663a2c5301f0d7e59f8247822760"`; -exports[`every seat's options match Kin byte for byte > review without lookup 1`] = `"ebd1ded9ba7e50f62d9d98ad6be9b9befaa704b3caf6d94a496573f71007672e"`; +exports[`every seat's options match Kin byte for byte > review without lookup 1`] = `"9b3f09b36c9bbe1cf8665832f15d8b945d5b5cc0e9372c85f730234c5bc7b45b"`; -exports[`every seat's options match Kin byte for byte > shared 1`] = `"9e11c324f2d1571ac1bcc64f8913ad8073f5d93116da1102812f01ab05f7962a"`; +exports[`every seat's options match Kin byte for byte > shared 1`] = `"92b88a68606bbdd63bb3a2d22c560eda596b813c8526483c55fa6c8a744738af"`; -exports[`every seat's options match Kin byte for byte > shared without lookup 1`] = `"c6a57b9a1638c4e93d5cdb5165316332131a3abf712b06adf8633dcc86d6ee59"`; +exports[`every seat's options match Kin byte for byte > shared without lookup 1`] = `"5a0b9b6551fc43da156fc656e53bb5a3cf49346ea081281b3f2d1413de36ed06"`; -exports[`every seat's options match Kin byte for byte > triage 1`] = `"1defebe09b6d04791f7f341cf716949e6ca0b3f538064db58b73f02d2d9cf959"`; +exports[`every seat's options match Kin byte for byte > triage 1`] = `"4fd891d89479f22f622440d37ebafb0d9226dbade92d2f1eabe80e537cb05731"`; -exports[`every seat's options match Kin byte for byte > triage without lookup 1`] = `"c2c3c8b5640b3bdaffab7e70d9748ce07c4ba89eb38fc60eaa63cb7718123c3a"`; +exports[`every seat's options match Kin byte for byte > triage without lookup 1`] = `"a8891241c94a157510240964017133be7f0a6ede2ae18a0fbffa030f008eeb3d"`; diff --git a/apps/server/src/kin/lead/turn/providers/claude.test.ts b/apps/server/src/kin/lead/turn/providers/claude.test.ts index ee4f6a28fd19..c78224e8629e 100644 --- a/apps/server/src/kin/lead/turn/providers/claude.test.ts +++ b/apps/server/src/kin/lead/turn/providers/claude.test.ts @@ -1,4 +1,4 @@ -import type { HookInput } from "@anthropic-ai/claude-agent-sdk"; +import { SYSTEM_PROMPT_DYNAMIC_BOUNDARY, type HookInput } from "@anthropic-ai/claude-agent-sdk"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { assert, expect, it } from "@effect/vitest"; import * as HostProcess from "@t3tools/shared/HostProcess"; @@ -31,6 +31,7 @@ import { OWNER_SEAT_NO_SUBAGENTS, OWNER_SEAT_OFF, } from "./claude.ts"; +import { cacheWeights } from "./provider.ts"; import { captureClaudeOptions } from "./claude.testkit.ts"; import { BILLS_SEAT_INSTRUCTIONS, kinInstructions, REVIEW_SEAT_INSTRUCTIONS } from "../session.ts"; import { KinSessionContext } from "../sessionContext.ts"; @@ -218,6 +219,16 @@ it.effect("every seat's options match Kin byte for byte", () => subscribeForViewer: Option.none(), }), ); + if (!seat.startsWith("delegated") && seat !== "dream") { + const system = options?.systemPrompt; + assert.strictEqual( + typeof system === "object" && "prompt" in system && Array.isArray(system.prompt) + ? system.prompt.at(-1) + : undefined, + SYSTEM_PROMPT_DYNAMIC_BOUNDARY, + seat, + ); + } const bytes = JSON.stringify(options, (_key, value: unknown) => { if (typeof value === "string") return value.replaceAll(stateDir, ""); if (typeof value === "function") return "[Function]"; @@ -238,6 +249,23 @@ it.effect("every seat's options match Kin byte for byte", () => ), ); +it("weights only priced Claude models, in units of plain input", () => { + const haiku = { input: 1, read: 0.1, write5m: 1.25, write1h: 2, output: 5 }; + const high = { ...haiku, read: 0.05 }; + for (const [driver, model, expected] of [ + ["claudeAgent", "claude-haiku-5-5", haiku], + ["claudeAgent", "claude-opus-5-5", high], + ["claudeAgent", "claude-sonnet-5-5", high], + ["claudeAgent", "claude-sonnet-4-6", undefined], + ["claudeAgent", "claude-haiku-5-5-custom", undefined], + ["codex", "claude-haiku-5-5", undefined], + ["acpRegistry", "claude-haiku-5-5", undefined], + ["unknown", "claude-haiku-5-5", undefined], + ] as const) { + expect(cacheWeights(driver, model), `${driver}/${model}`).toEqual(expected); + } +}); + it.effect("an enabled owner seat without KinSessionContext logs one error and still opens", () => { const errors: string[] = []; return withPreparedContext( @@ -423,7 +451,7 @@ it.effect.each([ // The instructions alone: no coding-agent preset, no T3 thread guidance. systemPrompt: { type: "custom", - prompt: kinInstructions("confined", "claudeAgent"), + prompt: [kinInstructions("confined", "claudeAgent"), SYSTEM_PROMPT_DYNAMIC_BOUNDARY], snapshot: false, }, // No settings file: user settings carry hooks, plugins and env a lead must not inherit. @@ -458,7 +486,10 @@ it.effect.each([ } satisfies typeof leadRest; assert.deepStrictEqual(leadRest, confinedBaseline); const { hooks: sharedHooks, ...sharedRest } = sharedOpened ?? lead; - assert.deepStrictEqual(sharedRest, confinedBaseline); + assert.deepStrictEqual(sharedRest, { + ...confinedBaseline, + env: { ...confinedBaseline.env, CLAUDE_CODE_PROMPT_CACHE_TTL: "1h" }, + }); for (const [seat, expectedPrompt] of [ [reviewOpened, REVIEW_SEAT_INSTRUCTIONS], [billsOpened, BILLS_SEAT_INSTRUCTIONS], @@ -466,7 +497,11 @@ it.effect.each([ const { hooks, ...rest } = seat ?? lead; assert.deepStrictEqual(rest, { ...confinedBaseline, - systemPrompt: { type: "custom", prompt: expectedPrompt, snapshot: false }, + systemPrompt: { + type: "custom", + prompt: [expectedPrompt, SYSTEM_PROMPT_DYNAMIC_BOUNDARY], + snapshot: false, + }, tools: ["ToolSearch"], disallowedTools: [...(lead.disallowedTools ?? []), ...builtinTools], }); @@ -535,7 +570,7 @@ it.effect.each([ // An outsider's pull request's review: its own short system prompt, and no web search. assert.deepStrictEqual(reviewOpened?.systemPrompt, { type: "custom", - prompt: REVIEW_SEAT_INSTRUCTIONS, + prompt: [REVIEW_SEAT_INSTRUCTIONS, SYSTEM_PROMPT_DYNAMIC_BOUNDARY], snapshot: false, }); assert.deepStrictEqual(reviewOpened?.tools, ["ToolSearch"]); @@ -547,7 +582,7 @@ it.effect.each([ // Bills from outside: the bills seat's own short instructions, and no built-in. assert.deepStrictEqual(billsOpened?.systemPrompt, { type: "custom", - prompt: BILLS_SEAT_INSTRUCTIONS, + prompt: [BILLS_SEAT_INSTRUCTIONS, SYSTEM_PROMPT_DYNAMIC_BOUNDARY], snapshot: false, }); assert.deepStrictEqual(billsOpened?.tools, ["ToolSearch"]); @@ -664,11 +699,29 @@ it.effect( assert.notProperty(full, "maxTurns"); const { hooks: fullHooks, env: fullEnv, settings: fullSettings, ...fullRequest } = full; const { env: _env, settings: leadSettings, ...leadRequest } = lead; + assert.deepStrictEqual( + yield* Effect.forEach( + [ + ["kin:tech", "5m"], + ["kin-shared:tech", undefined], + ["kin-shared:tech", "5m"], + ["kin-shared:bills:tech", undefined], + ["kin-shared:review:tech", undefined], + ["kin-shared:triage:tech", undefined], + ] as const, + ([thread, override]) => + open(thread, { + ...lead, + env: { ...lead.env, CLAUDE_CODE_PROMPT_CACHE_TTL: override }, + }).pipe(Effect.map((options) => options?.env?.CLAUDE_CODE_PROMPT_CACHE_TTL)), + ), + ["5m", "1h", "5m", undefined, undefined, undefined], + ); assert.deepStrictEqual(fullRequest, { ...leadRequest, systemPrompt: { type: "custom", - prompt: kinInstructions("owner", "claudeAgent"), + prompt: [kinInstructions("owner", "claudeAgent"), SYSTEM_PROMPT_DYNAMIC_BOUNDARY], snapshot: false, }, settingSources: [], @@ -701,14 +754,7 @@ it.effect( idleCompaction: false, ...dietSettings, }); - const preconfigured = yield* open("kin:tech", { - ...lead, - env: { ...lead.env, CLAUDE_CODE_PROMPT_CACHE_TTL: "5m" }, - }); - assert.deepStrictEqual(preconfigured?.env, { - ...fullEnv, - CLAUDE_CODE_PROMPT_CACHE_TTL: "5m", - }); + assert.sameMembers(Object.keys(fullHooks ?? {}), [ "PreToolUse", "SessionStart", @@ -768,7 +814,7 @@ it.effect( assert.deepStrictEqual(full?.settingSources, []); assert.deepStrictEqual(full?.systemPrompt, { type: "custom", - prompt: kinInstructions("owner", "claudeAgent"), + prompt: [kinInstructions("owner", "claudeAgent"), SYSTEM_PROMPT_DYNAMIC_BOUNDARY], snapshot: false, }); const allows = (tool: string, input: unknown) => @@ -990,9 +1036,12 @@ it.effect( // resumed sessions only). assert.strictEqual(first?.env?.MCP_CONNECTION_NONBLOCKING, "0", threadId); const system = first?.systemPrompt; - prompts.push( - typeof system === "object" && "prompt" in system ? [system.prompt].flat().join("") : "", - ); + const blocks = + typeof system === "object" && "prompt" in system && Array.isArray(system.prompt) + ? system.prompt + : []; + assert.deepStrictEqual(blocks.slice(1), [SYSTEM_PROMPT_DYNAMIC_BOUNDARY], threadId); + prompts.push(blocks[0] ?? ""); } assert.deepStrictEqual(prompts, [ kinInstructions("owner", "claudeAgent"), diff --git a/apps/server/src/kin/lead/turn/providers/claude.ts b/apps/server/src/kin/lead/turn/providers/claude.ts index 756bc82b4348..988385b6e7ed 100644 --- a/apps/server/src/kin/lead/turn/providers/claude.ts +++ b/apps/server/src/kin/lead/turn/providers/claude.ts @@ -1,4 +1,8 @@ -import type { HookCallback, HookCallbackMatcher } from "@anthropic-ai/claude-agent-sdk"; +import { + SYSTEM_PROMPT_DYNAMIC_BOUNDARY, + type HookCallback, + type HookCallbackMatcher, +} from "@anthropic-ai/claude-agent-sdk"; import * as HostProcess from "@t3tools/shared/HostProcess"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; @@ -12,6 +16,7 @@ import { sessionEnvironment, withConfinementHook, } from "../../../claudeConfinement.ts"; +import { SHARED_THREAD_PREFIX } from "../../Audience.ts"; import { type KinSessionPlan, KinSessions } from "../session.ts"; import * as SessionContext from "../sessionContext.ts"; import type { KinProvider } from "./provider.ts"; @@ -163,6 +168,7 @@ const apply = ( const tools = nativeTools(plan, loading.search === "true"); const confined = plan.seat === "confined"; const owner = plan.seat === "owner"; + const sharedChat = confined && plan.nativeSearch && plan.thread.startsWith(SHARED_THREAD_PREFIX); const hooks = session.hook === undefined ? undefined : withConfinementHook(options.hooks, session.hook); return { @@ -170,7 +176,13 @@ const apply = ( settingSources: [], ...(plan.instructions === undefined ? {} - : { systemPrompt: { type: "custom" as const, prompt: plan.instructions, snapshot: false } }), + : { + systemPrompt: { + type: "custom" as const, + prompt: [plan.instructions, SYSTEM_PROMPT_DYNAMIC_BOUNDARY], + snapshot: false, + }, + }), ...(tools === undefined ? {} : { tools }), ...(confined ? { @@ -236,7 +248,7 @@ const apply = ( }), ENABLE_TOOL_SEARCH: loading.search, CLAUDE_CODE_DISABLE_AUTO_MEMORY: "1", - ...(plan.seat === "owner" && environment.CLAUDE_CODE_PROMPT_CACHE_TTL === undefined + ...((owner || sharedChat) && environment.CLAUDE_CODE_PROMPT_CACHE_TTL === undefined ? { CLAUDE_CODE_PROMPT_CACHE_TTL: "1h" } : {}), // Wait for Kin's tools before the first request; a different tool block misses the cache. diff --git a/apps/server/src/kin/lead/turn/providers/provider.ts b/apps/server/src/kin/lead/turn/providers/provider.ts index 74a099b0fea9..60368ff170f0 100644 --- a/apps/server/src/kin/lead/turn/providers/provider.ts +++ b/apps/server/src/kin/lead/turn/providers/provider.ts @@ -16,3 +16,26 @@ export interface KinProvider { readonly capabilities: Partial>>>; readonly install: (base: Layer.Layer) => Layer.Layer; } + +const claudeOpusAndSonnetWeights = { + input: 1, + read: 0.05, + write5m: 1.25, + write1h: 2, + output: 5, +} as const; +const claudeHaikuWeights = { ...claudeOpusAndSonnetWeights, read: 0.1 } as const; + +/** Relative token prices in units of plain input. Unpriced models have no cost comparison. */ +export const cacheWeights = (driver: string, model: string) => { + if (driver !== "claudeAgent") return undefined; + switch (model) { + case "claude-opus-5-5": + case "claude-sonnet-5-5": + return claudeOpusAndSonnetWeights; + case "claude-haiku-5-5": + return claudeHaikuWeights; + default: + return undefined; + } +}; diff --git a/apps/server/src/kin/lead/turn/session.ts b/apps/server/src/kin/lead/turn/session.ts index d70bd9f5b9ea..ef97021c36ec 100644 --- a/apps/server/src/kin/lead/turn/session.ts +++ b/apps/server/src/kin/lead/turn/session.ts @@ -51,7 +51,10 @@ export interface KinSessionPlan { /** Kin tools listed in full; the rest are found by name where the provider supports lookup. */ readonly kinUpFront: ReadonlySet; readonly thread: ThreadId; - /** The whole system prompt for `owner` and `confined`; the provider's own otherwise. */ + /** + * The whole system prompt for `owner` and `confined`; the provider's own otherwise. + * Stable across sessions of the same seat and driver, so the provider may cache it across sessions. + */ readonly instructions: string | undefined; /** * The provider can defer tools behind lookup; false where the model can't look a tool up, From 485d0ba757d4e5d1d1677763af7a61b02af9a7be Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Sun, 29 Nov 2026 01:38:56 +0530 Subject: [PATCH 2/4] fix(kin): drop the ineffective Claude cache boundary Fresh sessions still rematerialize Kin's instructions through the gateway, even with equivalent plain API calls or an explicit scope. Restore the plain custom prompt; keep the shared-chat cache lifetime and neutral price weights. People-change: none Co-Authored-By: GPT-6.2 Sol --- .../__snapshots__/claude.test.ts.snap | 20 +++++------ .../kin/lead/turn/providers/claude.test.ts | 33 ++++++------------- .../src/kin/lead/turn/providers/claude.ts | 14 ++------ apps/server/src/kin/lead/turn/session.ts | 5 +-- 4 files changed, 23 insertions(+), 49 deletions(-) diff --git a/apps/server/src/kin/lead/turn/providers/__snapshots__/claude.test.ts.snap b/apps/server/src/kin/lead/turn/providers/__snapshots__/claude.test.ts.snap index 31d7c990a150..bf25cefee731 100644 --- a/apps/server/src/kin/lead/turn/providers/__snapshots__/claude.test.ts.snap +++ b/apps/server/src/kin/lead/turn/providers/__snapshots__/claude.test.ts.snap @@ -1,8 +1,8 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`every seat's options match Kin byte for byte > bills 1`] = `"f9f3806137a0aa8a13e3e9fb5e5512e6cea3d6d5fe57ff2bc04d3a1f451bc74a"`; +exports[`every seat's options match Kin byte for byte > bills 1`] = `"46a15de66b84756a9bd624b07154e4597f718e367c2407c134bbc3a00e8165ab"`; -exports[`every seat's options match Kin byte for byte > bills without lookup 1`] = `"40ffc63a0cfad7f2e031c9ec6a9c02ab364e024bf392f6ed152762c53125f029"`; +exports[`every seat's options match Kin byte for byte > bills without lookup 1`] = `"cb5394c4dfee139a410fc9195a6eaf10d9cb3d14108bdac1f30c18aaaf6481c3"`; exports[`every seat's options match Kin byte for byte > delegated 1`] = `"bedb0c0da98aea45f5f50134e988ae1cb0ce4d632d04fc107b0f4a95171ce0a1"`; @@ -10,18 +10,18 @@ exports[`every seat's options match Kin byte for byte > delegated without lookup exports[`every seat's options match Kin byte for byte > dream 1`] = `"7a15b18823863542dfbb28ceb5fbb88fde6a2f57d15b70d911ac1f4d0e5854e6"`; -exports[`every seat's options match Kin byte for byte > owner with lookup 1`] = `"3ab5d508452b9f2ba7037139f52cf2dae65aa64c9d21230d0edaacbc42160073"`; +exports[`every seat's options match Kin byte for byte > owner with lookup 1`] = `"81b4e4fde052ec790760ee4ce07ff47b0060e73c3ded42dd167747fdd7fca26d"`; -exports[`every seat's options match Kin byte for byte > owner without lookup 1`] = `"5a9c84fc86c594e492e99b06e067058a2028cefd1fd87a693acbc213c20d74ad"`; +exports[`every seat's options match Kin byte for byte > owner without lookup 1`] = `"87e0b5a6ed9300e9ef1edd87774c4fd1ba899800e540fdbd1391ee8cf8c58f5b"`; -exports[`every seat's options match Kin byte for byte > review 1`] = `"3284efc92724be5972b6cc2fb07e2e03ca2c663a2c5301f0d7e59f8247822760"`; +exports[`every seat's options match Kin byte for byte > review 1`] = `"e8bf99c959c5de4e4e266edc91de2feffb029a57afc8ea2bf3d0cd45b706d024"`; -exports[`every seat's options match Kin byte for byte > review without lookup 1`] = `"9b3f09b36c9bbe1cf8665832f15d8b945d5b5cc0e9372c85f730234c5bc7b45b"`; +exports[`every seat's options match Kin byte for byte > review without lookup 1`] = `"ebd1ded9ba7e50f62d9d98ad6be9b9befaa704b3caf6d94a496573f71007672e"`; -exports[`every seat's options match Kin byte for byte > shared 1`] = `"92b88a68606bbdd63bb3a2d22c560eda596b813c8526483c55fa6c8a744738af"`; +exports[`every seat's options match Kin byte for byte > shared 1`] = `"5ccc5b3cc193749ae68a284a54794bcf2caffa2db011ffc1ddf6bbaf885cfd28"`; -exports[`every seat's options match Kin byte for byte > shared without lookup 1`] = `"5a0b9b6551fc43da156fc656e53bb5a3cf49346ea081281b3f2d1413de36ed06"`; +exports[`every seat's options match Kin byte for byte > shared without lookup 1`] = `"bfc39a6d06edf7cf012d484ff3ace606ee53e627428e1cc058315ad407206280"`; -exports[`every seat's options match Kin byte for byte > triage 1`] = `"4fd891d89479f22f622440d37ebafb0d9226dbade92d2f1eabe80e537cb05731"`; +exports[`every seat's options match Kin byte for byte > triage 1`] = `"1defebe09b6d04791f7f341cf716949e6ca0b3f538064db58b73f02d2d9cf959"`; -exports[`every seat's options match Kin byte for byte > triage without lookup 1`] = `"a8891241c94a157510240964017133be7f0a6ede2ae18a0fbffa030f008eeb3d"`; +exports[`every seat's options match Kin byte for byte > triage without lookup 1`] = `"c2c3c8b5640b3bdaffab7e70d9748ce07c4ba89eb38fc60eaa63cb7718123c3a"`; diff --git a/apps/server/src/kin/lead/turn/providers/claude.test.ts b/apps/server/src/kin/lead/turn/providers/claude.test.ts index c78224e8629e..eb5454f3eb74 100644 --- a/apps/server/src/kin/lead/turn/providers/claude.test.ts +++ b/apps/server/src/kin/lead/turn/providers/claude.test.ts @@ -1,4 +1,4 @@ -import { SYSTEM_PROMPT_DYNAMIC_BOUNDARY, type HookInput } from "@anthropic-ai/claude-agent-sdk"; +import type { HookInput } from "@anthropic-ai/claude-agent-sdk"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { assert, expect, it } from "@effect/vitest"; import * as HostProcess from "@t3tools/shared/HostProcess"; @@ -219,16 +219,6 @@ it.effect("every seat's options match Kin byte for byte", () => subscribeForViewer: Option.none(), }), ); - if (!seat.startsWith("delegated") && seat !== "dream") { - const system = options?.systemPrompt; - assert.strictEqual( - typeof system === "object" && "prompt" in system && Array.isArray(system.prompt) - ? system.prompt.at(-1) - : undefined, - SYSTEM_PROMPT_DYNAMIC_BOUNDARY, - seat, - ); - } const bytes = JSON.stringify(options, (_key, value: unknown) => { if (typeof value === "string") return value.replaceAll(stateDir, ""); if (typeof value === "function") return "[Function]"; @@ -451,7 +441,7 @@ it.effect.each([ // The instructions alone: no coding-agent preset, no T3 thread guidance. systemPrompt: { type: "custom", - prompt: [kinInstructions("confined", "claudeAgent"), SYSTEM_PROMPT_DYNAMIC_BOUNDARY], + prompt: kinInstructions("confined", "claudeAgent"), snapshot: false, }, // No settings file: user settings carry hooks, plugins and env a lead must not inherit. @@ -499,7 +489,7 @@ it.effect.each([ ...confinedBaseline, systemPrompt: { type: "custom", - prompt: [expectedPrompt, SYSTEM_PROMPT_DYNAMIC_BOUNDARY], + prompt: expectedPrompt, snapshot: false, }, tools: ["ToolSearch"], @@ -570,7 +560,7 @@ it.effect.each([ // An outsider's pull request's review: its own short system prompt, and no web search. assert.deepStrictEqual(reviewOpened?.systemPrompt, { type: "custom", - prompt: [REVIEW_SEAT_INSTRUCTIONS, SYSTEM_PROMPT_DYNAMIC_BOUNDARY], + prompt: REVIEW_SEAT_INSTRUCTIONS, snapshot: false, }); assert.deepStrictEqual(reviewOpened?.tools, ["ToolSearch"]); @@ -582,7 +572,7 @@ it.effect.each([ // Bills from outside: the bills seat's own short instructions, and no built-in. assert.deepStrictEqual(billsOpened?.systemPrompt, { type: "custom", - prompt: [BILLS_SEAT_INSTRUCTIONS, SYSTEM_PROMPT_DYNAMIC_BOUNDARY], + prompt: BILLS_SEAT_INSTRUCTIONS, snapshot: false, }); assert.deepStrictEqual(billsOpened?.tools, ["ToolSearch"]); @@ -721,7 +711,7 @@ it.effect( ...leadRequest, systemPrompt: { type: "custom", - prompt: [kinInstructions("owner", "claudeAgent"), SYSTEM_PROMPT_DYNAMIC_BOUNDARY], + prompt: kinInstructions("owner", "claudeAgent"), snapshot: false, }, settingSources: [], @@ -814,7 +804,7 @@ it.effect( assert.deepStrictEqual(full?.settingSources, []); assert.deepStrictEqual(full?.systemPrompt, { type: "custom", - prompt: [kinInstructions("owner", "claudeAgent"), SYSTEM_PROMPT_DYNAMIC_BOUNDARY], + prompt: kinInstructions("owner", "claudeAgent"), snapshot: false, }); const allows = (tool: string, input: unknown) => @@ -1036,12 +1026,9 @@ it.effect( // resumed sessions only). assert.strictEqual(first?.env?.MCP_CONNECTION_NONBLOCKING, "0", threadId); const system = first?.systemPrompt; - const blocks = - typeof system === "object" && "prompt" in system && Array.isArray(system.prompt) - ? system.prompt - : []; - assert.deepStrictEqual(blocks.slice(1), [SYSTEM_PROMPT_DYNAMIC_BOUNDARY], threadId); - prompts.push(blocks[0] ?? ""); + prompts.push( + typeof system === "object" && "prompt" in system ? [system.prompt].flat().join("") : "", + ); } assert.deepStrictEqual(prompts, [ kinInstructions("owner", "claudeAgent"), diff --git a/apps/server/src/kin/lead/turn/providers/claude.ts b/apps/server/src/kin/lead/turn/providers/claude.ts index 988385b6e7ed..1ea149b74fb3 100644 --- a/apps/server/src/kin/lead/turn/providers/claude.ts +++ b/apps/server/src/kin/lead/turn/providers/claude.ts @@ -1,8 +1,4 @@ -import { - SYSTEM_PROMPT_DYNAMIC_BOUNDARY, - type HookCallback, - type HookCallbackMatcher, -} from "@anthropic-ai/claude-agent-sdk"; +import type { HookCallback, HookCallbackMatcher } from "@anthropic-ai/claude-agent-sdk"; import * as HostProcess from "@t3tools/shared/HostProcess"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; @@ -176,13 +172,7 @@ const apply = ( settingSources: [], ...(plan.instructions === undefined ? {} - : { - systemPrompt: { - type: "custom" as const, - prompt: [plan.instructions, SYSTEM_PROMPT_DYNAMIC_BOUNDARY], - snapshot: false, - }, - }), + : { systemPrompt: { type: "custom" as const, prompt: plan.instructions, snapshot: false } }), ...(tools === undefined ? {} : { tools }), ...(confined ? { diff --git a/apps/server/src/kin/lead/turn/session.ts b/apps/server/src/kin/lead/turn/session.ts index ef97021c36ec..d70bd9f5b9ea 100644 --- a/apps/server/src/kin/lead/turn/session.ts +++ b/apps/server/src/kin/lead/turn/session.ts @@ -51,10 +51,7 @@ export interface KinSessionPlan { /** Kin tools listed in full; the rest are found by name where the provider supports lookup. */ readonly kinUpFront: ReadonlySet; readonly thread: ThreadId; - /** - * The whole system prompt for `owner` and `confined`; the provider's own otherwise. - * Stable across sessions of the same seat and driver, so the provider may cache it across sessions. - */ + /** The whole system prompt for `owner` and `confined`; the provider's own otherwise. */ readonly instructions: string | undefined; /** * The provider can defer tools behind lookup; false where the model can't look a tool up, From 46045f8f6a9dde3974ded8be2a31b286c59ad2db Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Sun, 29 Nov 2026 01:48:01 +0530 Subject: [PATCH 3/4] fix(kin): keep shared chats on the provider's own cache lifetime With no instructions reused across fresh sessions, a one-hour write on shared chats only pays when a later wake arrives 5-60 minutes after the last request with the same prefix. On the recorded week that is 7 of 35 wakes against a break-even of 88%, so an install that honours the marker pays about $0.07 more per shared wake; the recorded gateway bills every write at one hour already, so there it changes nothing. Restore kin's lifetime rule and pin it: owner 1h by default, explicit lifetimes win, shared, bills, review and triage seats unset. cacheWeights stays. People-change: none Co-Authored-By: Claude Opus 5.5 --- .../providers/__snapshots__/claude.test.ts.snap | 4 ++-- .../src/kin/lead/turn/providers/claude.test.ts | 15 ++++----------- apps/server/src/kin/lead/turn/providers/claude.ts | 4 +--- 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/apps/server/src/kin/lead/turn/providers/__snapshots__/claude.test.ts.snap b/apps/server/src/kin/lead/turn/providers/__snapshots__/claude.test.ts.snap index bf25cefee731..ae35c9fc170f 100644 --- a/apps/server/src/kin/lead/turn/providers/__snapshots__/claude.test.ts.snap +++ b/apps/server/src/kin/lead/turn/providers/__snapshots__/claude.test.ts.snap @@ -18,9 +18,9 @@ exports[`every seat's options match Kin byte for byte > review 1`] = `"e8bf99c95 exports[`every seat's options match Kin byte for byte > review without lookup 1`] = `"ebd1ded9ba7e50f62d9d98ad6be9b9befaa704b3caf6d94a496573f71007672e"`; -exports[`every seat's options match Kin byte for byte > shared 1`] = `"5ccc5b3cc193749ae68a284a54794bcf2caffa2db011ffc1ddf6bbaf885cfd28"`; +exports[`every seat's options match Kin byte for byte > shared 1`] = `"9e11c324f2d1571ac1bcc64f8913ad8073f5d93116da1102812f01ab05f7962a"`; -exports[`every seat's options match Kin byte for byte > shared without lookup 1`] = `"bfc39a6d06edf7cf012d484ff3ace606ee53e627428e1cc058315ad407206280"`; +exports[`every seat's options match Kin byte for byte > shared without lookup 1`] = `"c6a57b9a1638c4e93d5cdb5165316332131a3abf712b06adf8633dcc86d6ee59"`; exports[`every seat's options match Kin byte for byte > triage 1`] = `"1defebe09b6d04791f7f341cf716949e6ca0b3f538064db58b73f02d2d9cf959"`; diff --git a/apps/server/src/kin/lead/turn/providers/claude.test.ts b/apps/server/src/kin/lead/turn/providers/claude.test.ts index eb5454f3eb74..ee2eccdb376e 100644 --- a/apps/server/src/kin/lead/turn/providers/claude.test.ts +++ b/apps/server/src/kin/lead/turn/providers/claude.test.ts @@ -476,10 +476,7 @@ it.effect.each([ } satisfies typeof leadRest; assert.deepStrictEqual(leadRest, confinedBaseline); const { hooks: sharedHooks, ...sharedRest } = sharedOpened ?? lead; - assert.deepStrictEqual(sharedRest, { - ...confinedBaseline, - env: { ...confinedBaseline.env, CLAUDE_CODE_PROMPT_CACHE_TTL: "1h" }, - }); + assert.deepStrictEqual(sharedRest, confinedBaseline); for (const [seat, expectedPrompt] of [ [reviewOpened, REVIEW_SEAT_INSTRUCTIONS], [billsOpened, BILLS_SEAT_INSTRUCTIONS], @@ -487,11 +484,7 @@ it.effect.each([ const { hooks, ...rest } = seat ?? lead; assert.deepStrictEqual(rest, { ...confinedBaseline, - systemPrompt: { - type: "custom", - prompt: expectedPrompt, - snapshot: false, - }, + systemPrompt: { type: "custom", prompt: expectedPrompt, snapshot: false }, tools: ["ToolSearch"], disallowedTools: [...(lead.disallowedTools ?? []), ...builtinTools], }); @@ -692,6 +685,7 @@ it.effect( assert.deepStrictEqual( yield* Effect.forEach( [ + ["kin:tech", undefined], ["kin:tech", "5m"], ["kin-shared:tech", undefined], ["kin-shared:tech", "5m"], @@ -705,7 +699,7 @@ it.effect( env: { ...lead.env, CLAUDE_CODE_PROMPT_CACHE_TTL: override }, }).pipe(Effect.map((options) => options?.env?.CLAUDE_CODE_PROMPT_CACHE_TTL)), ), - ["5m", "1h", "5m", undefined, undefined, undefined], + ["1h", "5m", undefined, "5m", undefined, undefined, undefined], ); assert.deepStrictEqual(fullRequest, { ...leadRequest, @@ -744,7 +738,6 @@ it.effect( idleCompaction: false, ...dietSettings, }); - assert.sameMembers(Object.keys(fullHooks ?? {}), [ "PreToolUse", "SessionStart", diff --git a/apps/server/src/kin/lead/turn/providers/claude.ts b/apps/server/src/kin/lead/turn/providers/claude.ts index 1ea149b74fb3..756bc82b4348 100644 --- a/apps/server/src/kin/lead/turn/providers/claude.ts +++ b/apps/server/src/kin/lead/turn/providers/claude.ts @@ -12,7 +12,6 @@ import { sessionEnvironment, withConfinementHook, } from "../../../claudeConfinement.ts"; -import { SHARED_THREAD_PREFIX } from "../../Audience.ts"; import { type KinSessionPlan, KinSessions } from "../session.ts"; import * as SessionContext from "../sessionContext.ts"; import type { KinProvider } from "./provider.ts"; @@ -164,7 +163,6 @@ const apply = ( const tools = nativeTools(plan, loading.search === "true"); const confined = plan.seat === "confined"; const owner = plan.seat === "owner"; - const sharedChat = confined && plan.nativeSearch && plan.thread.startsWith(SHARED_THREAD_PREFIX); const hooks = session.hook === undefined ? undefined : withConfinementHook(options.hooks, session.hook); return { @@ -238,7 +236,7 @@ const apply = ( }), ENABLE_TOOL_SEARCH: loading.search, CLAUDE_CODE_DISABLE_AUTO_MEMORY: "1", - ...((owner || sharedChat) && environment.CLAUDE_CODE_PROMPT_CACHE_TTL === undefined + ...(plan.seat === "owner" && environment.CLAUDE_CODE_PROMPT_CACHE_TTL === undefined ? { CLAUDE_CODE_PROMPT_CACHE_TTL: "1h" } : {}), // Wait for Kin's tools before the first request; a different tool block misses the cache. From d35b52e265c20c803de0978e1c9e149411c0f057 Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Sun, 29 Nov 2026 02:24:40 +0530 Subject: [PATCH 4/4] fix(kin): the carry cost gate calls the provider's price table directly, and four models re-confirmed for train 16 With step 5's cacheWeights(driver, model) on the line, the optional shim in WakeupRunner ("until then no turn is priced") is no longer true: call the export directly. The test's comment no longer names a branch. Re-confirm money, tasks, discussion and reply (their READMEs name work/bills.ts, lead/LeadPrompt.ts and lead/WakeupRunner.ts): no spec change, every rule holds and every broken variant is caught. No pin moved. People-change: none Co-Authored-By: Claude Opus 5.5 --- apps/server/src/kin/lead/WakeupRunner.test.ts | 2 +- apps/server/src/kin/lead/WakeupRunner.ts | 9 +-------- verification/kin/discussion/README.md | 2 ++ verification/kin/money/README.md | 2 ++ verification/kin/reply/README.md | 2 ++ verification/kin/tasks/README.md | 2 ++ 6 files changed, 10 insertions(+), 9 deletions(-) diff --git a/apps/server/src/kin/lead/WakeupRunner.test.ts b/apps/server/src/kin/lead/WakeupRunner.test.ts index 48223275109a..d6095d16d37b 100644 --- a/apps/server/src/kin/lead/WakeupRunner.test.ts +++ b/apps/server/src/kin/lead/WakeupRunner.test.ts @@ -464,7 +464,7 @@ type CacheWeights = { write1h: number; output: number; }; -// The shape `provider.ts` exports on `fix/kin-cache-instructions-boundary`, the change this one joins after. +// The shape `cacheWeights` in `provider.ts` returns. const pricing = vi.hoisted(() => ({ cacheWeights: (_driver: string, _model: string): CacheWeights | undefined => undefined, })); diff --git a/apps/server/src/kin/lead/WakeupRunner.ts b/apps/server/src/kin/lead/WakeupRunner.ts index 113f9987961d..6a7dedbc9a29 100644 --- a/apps/server/src/kin/lead/WakeupRunner.ts +++ b/apps/server/src/kin/lead/WakeupRunner.ts @@ -236,13 +236,6 @@ const decodeRunning = Schema.decodeUnknownEffect(RunningRow); const decodeTaskIds = Schema.decodeUnknownOption( Schema.fromJsonString(Schema.Array(Schema.String)), ); -// `cacheWeights` arrives with the instructions-boundary change; until then no turn is priced. -const pricing: typeof cachePricing & { - readonly cacheWeights?: ( - driver: string, - model: string, - ) => { readonly read: number; readonly write1h: number; readonly output: number } | undefined; -} = cachePricing; const make = Effect.gen(function* () { const boot = yield* Effect.serviceOption(KinBoot); @@ -2387,7 +2380,7 @@ const make = Effect.gen(function* () { const send = yield* Effect.gen(function* () { const toolListSha = agentSessions?.lastToolList(threadId) ?? null; const weights = ownerClaude - ? pricing.cacheWeights?.(claude.driver, modelSelection.model) + ? cachePricing.cacheWeights(claude.driver, modelSelection.model) : undefined; // The owner seat writes its prompt cache for an hour. const cacheWeights = diff --git a/verification/kin/discussion/README.md b/verification/kin/discussion/README.md index e67761ffaf86..eee7a1db11d6 100644 --- a/verification/kin/discussion/README.md +++ b/verification/kin/discussion/README.md @@ -107,3 +107,5 @@ On 2026-11-26, against `feat/kin-session-changes` (`fc8c965117c`, on kin `4df1cc **Re-confirmed 2026-11-28** on train 16 (the last Finance train, five steps joined on kin `db4a66e447d`; no spec change, all 13 rules hold over 44,410 states in 31 s; 16/16 variants caught in 25 s). The one covered file the line changes, `chat/KinChatService.ts`, differs from kin only by the money-in check (step 3): the bank-check card joins the types a generic widget upsert refuses. No discussion, ask, hand-off, room or check-in changes. **Re-confirmed 2026-11-29** for the session carry clocks and cost gate (base `bc0259ac6be`; no spec change; all 13 rules hold over 44,410 states, 16/16 broken variants caught). `WakeupRunner` changes how it ages and prices an owner-only session, not who can join or speak, which thread a turn uses, what the Team room can read, or how `finishWakeup`, `wakeupEnded` and `reportBack` settle a turn. A Team-room turn still starts fresh and inherits no session marks. + +**Re-confirmed 2026-11-29** on train 16 of 29 Nov (the cache fixes and the Finance follow-through, five steps joined on kin `bc0259ac6be`; no spec change, all 13 rules hold over 44,410 states in 34 s; 16/16 variants caught in 23 s). The one covered file the line changes, `lead/WakeupRunner.ts`, differs from kin only by the carry clocks and cost gate (the line above), which now price with the turn provider's real `cacheWeights(driver, model)` table, so the shim that waited for it is gone. Pricing is asked only on the owner-only Claude path; a Team-room turn still starts fresh and inherits no session marks. diff --git a/verification/kin/money/README.md b/verification/kin/money/README.md index d73c1cc24da1..c314c417e649 100644 --- a/verification/kin/money/README.md +++ b/verification/kin/money/README.md @@ -64,3 +64,5 @@ On 2026-11-27, against `feat/kin-credit-which-invoice` at `f79f03f9280` (the rev **Re-confirmed 2026-11-28** on `feat/kin-money-in-check` after its independent review, joined on kin `1fadc6799d1` (no spec change, all 7 rules hold over 934,934 states in 17 s; 9/9 variants caught in 6 s). The one covered file the branch changes, `statementReview.ts`, is as its line above says: `recordedPayments` leaves out a payment a confirmed statement linked (`prior.matched`), and the shared read uses `countedPayment`. The review's fixes touch only the read-only check (`moneyInCheck.ts`), its tool words and Finance's sentence: no Confirm, receipt, payment or statement row changes. **Re-confirmed 2026-11-28** on train 16 (the last Finance train, five steps joined on kin `db4a66e447d`: chat photo copies, the bills card screens, the money-in check, bills read many at once and bills fixed by talking; no spec change, all 7 rules hold over 934,934 states in 16 s; 9/9 variants caught in 8 s). The one covered file the line changes, `work/statements/statementReview.ts`, differs from kin only by the money-in check (step 3, as its lines above say): `recordedPayments` leaves out a payment a confirmed statement linked, and the shared read uses `countedPayment`. Outside the covered files, the join's fix in `work/bills.ts` gives Fix and keep's expense its own record when a chat correction had withdrawn the bill's suggestion; it is still kept through the existing `confirm` path. No receipt, payment or statement rule changes. + +**Re-confirmed 2026-11-29** on train 16 of 29 Nov (the cache fixes and the Finance follow-through, five steps joined on kin `bc0259ac6be`; no spec change, all 7 rules hold over 934,934 states in 15 s; 9/9 variants caught in 7 s). The line changes no covered file. `work/bills.ts`, named above, now reads a printed mark such as "Rs." in the company's own currency when it fits, carries an upload's "reimburse me" as an amount owed (never repaid), and reads a wrongly cut photo again as one bill, refused when a piece is kept. Every keep is still the owner's or a co-owner's tap through `keepRead`, `keepEntered` or `confirm`. The money-in check card is drawn on web and phone with no server change, and no receipt, payment or statement rule changes. diff --git a/verification/kin/reply/README.md b/verification/kin/reply/README.md index 7b349eace251..564145388b7d 100644 --- a/verification/kin/reply/README.md +++ b/verification/kin/reply/README.md @@ -116,3 +116,5 @@ The guard "Steer waits after a Stop" (rule 12) now rests on code, from kin's `15 **Re-confirmed 2026-11-28** on train 16 (the last Finance train, five steps joined on kin `db4a66e447d`; no spec change, all 17 rules hold over 1,554,826 states in 422 s; 28/28 variants caught in 40 s). The one covered file the line changes, `reply/classify.ts`, differs from kin only by bills fixed by talking (step 5, as its line above says): working-line labels for `bills_list`, `bill_set` and `bill_recut`. Every message still ends in a reply, a plain failure line or the person's own Stop. **Re-confirmed 2026-11-29** for the session carry clocks and cost gate (base `bc0259ac6be`; no spec change; all 17 rules hold over 1,554,826 states, 28/28 broken variants caught). `WakeupRunner` uses the previous provider completion to age a session and can start a fresh provider context when carry costs more. It still takes the same claim, audience, delivery, rescue, requeue and Stop paths; the extra wake-up stamp fields do not feed those paths. + +**Re-confirmed 2026-11-29** on train 16 of 29 Nov (the cache fixes and the Finance follow-through, five steps joined on kin `bc0259ac6be`; no spec change, all 17 rules hold over 1,554,826 states in 649 s; 28/28 variants caught in 46 s). The one covered file the line changes, `lead/WakeupRunner.ts`, differs from kin only by the carry clocks and cost gate (the line above), which now price with the turn provider's real `cacheWeights(driver, model)` table, so the shim that waited for it is gone. A carried session that costs more only starts fresh; it takes the same claim, audience, delivery, rescue, requeue and Stop paths. diff --git a/verification/kin/tasks/README.md b/verification/kin/tasks/README.md index f84ff7aca258..361f7afdbd8b 100644 --- a/verification/kin/tasks/README.md +++ b/verification/kin/tasks/README.md @@ -65,3 +65,5 @@ On 2026-11-27, against the advertised record-writer core (`perf/kin-advertised-c **Re-confirmed 2026-11-28** for the bills chat card (train 13 base `5ec5553ef2b`; all 10 rules hold over 244,584 states; 12/12 variants caught). `KinRecords.update` refreshes only an expense's bill-batch card. Task authority, assignment, hand-back and notices are unchanged. **Re-confirmed 2026-11-28** on train 15 (the phone fix, bills round 2 and the phone look's fixes, seven steps joined on kin `19fff00b325`; no spec change, all 10 rules hold over 244,584 states; 12/12 variants caught). `work/KinRecords.ts` differs from kin only by the bills chat card's refresh in `update` (step 2, above); the other steps touch no covered file. Task authority, assignment, hand-back and notices are unchanged. + +**Re-confirmed 2026-11-29** on train 16 of 29 Nov (the cache fixes and the Finance follow-through, five steps joined on kin `bc0259ac6be`; no spec change, all 10 rules hold over 244,584 states in 7 s; 12/12 variants caught in 9 s). The line changes no covered file. `LeadPrompt.ts`, named above, now leaves the memory index's leading "As of" line out of a carried session's change list and fingerprint only; fresh prompts still show it. Task authority, assignment, hand-back and notices are unchanged.