Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
530 changes: 372 additions & 158 deletions apps/server/src/kin/lead/WakeupRunner.test.ts

Large diffs are not rendered by default.

13 changes: 2 additions & 11 deletions apps/server/src/kin/lead/WakeupRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ import { KinAgentSessions } from "../mcp/KinAgentSessions.ts";
import { captureChatCursor, readCursor, sessionChanges, type Cursor } from "./sessionChanges.ts";
import { KinSessionContext, type SessionSnapshot } from "./turn/sessionContext.ts";
import { claude } from "./turn/providers/claude.ts";
import * as cachePricing from "./turn/providers/provider.ts";
import { KinWorkspace } from "../gendocs/workspace.ts";
import { KinBoot, carryFacts, decideCarry, ulid, type PromptFingerprint } from "./sessionCarry.ts";
import { closeQuietRunCards, postQuietRunCard } from "./quietRunCard.ts";
Expand Down Expand Up @@ -871,7 +870,7 @@ const make = Effect.gen(function* () {
if (row.threadId === null) return;
const threadId = row.threadId;
const unread = sessionContext?.take(threadId) !== undefined;
// A released turn can leave the previous row eligible but its snapshot is no longer valid.
// Never leave the older snapshot active if capturing this turn fails.
if (snapshot !== undefined && sessionContext !== undefined)
yield* sessionContext.forget(threadId);
if (row.runId === null) return;
Expand Down Expand Up @@ -2379,14 +2378,6 @@ const make = Effect.gen(function* () {
const ownerClaude = alone && confined === undefined && driver === claude.driver;
const send = yield* Effect.gen(function* () {
const toolListSha = agentSessions?.lastToolList(threadId) ?? null;
const weights = ownerClaude
? cachePricing.cacheWeights(claude.driver, modelSelection.model)
: undefined;
// The owner seat writes its prompt cache for an hour.
const cacheWeights =
weights === undefined
? undefined
: { read: weights.read, write: weights.write1h, output: weights.output };
const factsEffect = carryFacts(row, {
threadId,
ownerThread: alone && confined === undefined,
Expand All @@ -2403,7 +2394,6 @@ const make = Effect.gen(function* () {
prompt: promptFingerprint,
enabled: hostSettings.kinSessionCarry,
...(yield* carryClocks),
...(cacheWeights === undefined ? {} : { cacheWeights }),
stopRevision: refreshHub?.stopRevision(orgId) ?? 0,
compactAtTokens: hostSettings.kinSessionCompactAtTokens,
});
Expand Down Expand Up @@ -2487,6 +2477,7 @@ const make = Effect.gen(function* () {
ageClock: 2,
companyGapMs: facts.facts.companyGapMs,
...facts.diagnostics,
...(facts.approvedDraft ? { approvedDraft: true } : {}),
contextTokens: facts.contextTokens,
bootId,
toolListSha,
Expand Down
61 changes: 47 additions & 14 deletions apps/server/src/kin/lead/sessionCarry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import * as SqlClient from "effect/sql/SqlClient";
import { it as effectIt } from "@effect/vitest";
import { describe, expect, it } from "vite-plus/test";

import { type CarryFacts, carryFacts, decideCarry, fingerprintPrompt } from "./sessionCarry.ts";
import {
CARRY_LIFETIME_MS,
type CarryFacts,
carryFacts,
decideCarry,
fingerprintPrompt,
} from "./sessionCarry.ts";
import { agentId, harness, orgId, seedCarry, selection, wake } from "./WakeupRunner.testkit.ts";
import { provideDeterministicTestRuntime } from "../../orchestration-v2/testkit/DeterministicRuntime.ts";

Expand All @@ -17,8 +23,8 @@ const clean: CarryFacts = {
previousCompleted: true,
marked: false,
foreignRun: false,
gapMs: 55 * 60_000,
companyGapMs: 55 * 60_000,
gapMs: CARRY_LIFETIME_MS,
companyGapMs: CARRY_LIFETIME_MS,
sameBoot: true,
sameModel: true,
sameInstructions: true,
Expand All @@ -41,14 +47,13 @@ describe("decides carry only when every condition holds", () => {
["provider", { provider: "codex", ownerThread: false }],
["thread", { ownerThread: false, sameChat: false }],
["chat", { sameChat: false, previousCompleted: false }],
["ended", { previousCompleted: false, marked: true }],
["marked", { marked: true, foreignRun: true }],
["marked", { marked: true, previousCompleted: false }],
["ended", { previousCompleted: false, foreignRun: true }],
["foreign_run", { foreignRun: true, gapMs: null }],
["expired", { gapMs: 55 * 60_000 + 1, companyGapMs: 1_000, sameBoot: false }],
["expired", { companyGapMs: 55 * 60_000 + 1, gapMs: 1_000 }],
["expired", { gapMs: CARRY_LIFETIME_MS + 1, companyGapMs: 1_000, sameBoot: false }],
["expired", { companyGapMs: CARRY_LIFETIME_MS + 1, gapMs: 1_000 }],
["expired", { companyGapMs: -1 }],
["expired", { companyGapMs: null }],
["cost", { carriedCost: 101, freshCost: 100, sameBoot: false }],
["boot", { sameBoot: false, sameModel: false }],
["model", { sameModel: false, sameInstructions: false }],
["instructions", { sameInstructions: false, sameToolList: false }],
Expand All @@ -67,8 +72,37 @@ describe("decides carry only when every condition holds", () => {
},
);

it("a session started under the system layout starts fresh for instructions under the block layout", () => {
const systemStamp = { instructions: "a".repeat(64) };
const blockStamp = { instructions: "b".repeat(64) };
const nextWake = (previous: { instructions: string }) =>
decideCarry({
...clean,
sameInstructions: previous.instructions === blockStamp.instructions,
});

expect(nextWake(systemStamp)).toEqual({ carry: false, reason: "instructions" });
expect(nextWake(blockStamp)).toEqual({ carry: true });
});

const limit = CARRY_LIFETIME_MS;
it.each([
["wall time over", limit + 1, limit - 1, { carry: false, reason: "expired" }],
["company time over", limit - 1, limit + 1, { carry: false, reason: "expired" }],
["both under", limit - 1, limit - 1, { carry: true }],
] as const)(
"a session idle past the limit by either clock starts fresh for expired (%s)",
(_, gapMs, companyGapMs, expected) => {
const facts = { ...clean, gapMs, companyGapMs };
expect(decideCarry(facts)).toEqual(expected);
expect(decideCarry({ ...facts, compactedSince: true })).toEqual(
expected.carry ? { carry: true, refill: true } : expected,
);
},
);

it("carries at both inclusive limits", () => {
expect(decideCarry({ ...clean, carriedCost: 100, freshCost: 100 })).toEqual({ carry: true });
expect(decideCarry(clean)).toEqual({ carry: true });
});
});

Expand All @@ -93,8 +127,6 @@ effectIt.effect("size wins over boot and accepts the inclusive limit", () =>
decideCarry({
...atLimit,
contextTokens: 100_001,
carriedCost: 110,
freshCost: 100,
sameBoot: false,
}),
).toEqual({
Expand Down Expand Up @@ -153,19 +185,20 @@ effectIt.effect("names the lane, mark source and ending behind a refusal", () =>
const marked = yield* compare;
expect(marked.diagnostics).toEqual({
lane: "owner",
markSource: "this_wake",
markSource: undefined,
endedDetail: undefined,
});
expect(marked.facts.marked).toBe(true);
expect(marked.facts.marked).toBe(false);
yield* sql`UPDATE kin_events SET json = json_set(json, '$.subject', json('{"approved":true}'))
WHERE org_id = ${orgId} AND wakeup_id = ${prior!.wakeupId}`;
expect((yield* compare).diagnostics.markSource).toBe("approved_draft");
yield* sql`UPDATE kin_wakeups SET status = 'failed' WHERE wakeup_id = ${prior!.wakeupId}`;
const failed = yield* compare;
expect(failed.diagnostics.endedDetail).toBe("failed");
expect(failed.diagnostics.markSource).toBe("approved_draft");
expect(decideCarry({ ...failed.facts, enabled: true })).toEqual({
carry: false,
reason: "ended",
reason: "marked",
});
yield* sql`UPDATE kin_wakeups SET status = 'completed',
carry_json = json_set(carry_json, '$.sessionStart', 'missing-session')
Expand Down
Loading
Loading