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
183 changes: 182 additions & 1 deletion apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const layerWorktreeLocation = (worktreesDir = managedWorktreesDir) =>
Layer.mergeAll(
Path.layer,
Layer.mock(ServerConfig.ServerConfig)({ worktreesDir } as ServerConfig.ServerConfig["Service"]),
ServerSettings.layerTest(),
ServerSettings.layerTest({ sessionSpendGuard: { mode: "off" } }),
);

function startWithMissingWorktree(
Expand Down Expand Up @@ -293,6 +293,187 @@ it.each([
expect(harness.createWorktree).toHaveBeenCalledTimes(input.recreated ? 1 : 0);
});

function startWithSpendGuard(input: {
readonly title: string;
readonly creationSource: "web" | "mcp";
readonly pullRequestLinkedAt?: string;
readonly providerTurns: ReadonlyArray<{
readonly startedAt: string;
readonly inputTokens: number;
}>;
}) {
const threadId = ThreadId.make("thread_provider_turn_start_spend_guard");
const runId = RunId.make("run_provider_turn_start_spend_guard");
const attemptId = RunAttemptId.make("attempt_provider_turn_start_spend_guard");
const rootNodeId = NodeId.make("node_provider_turn_start_spend_guard");
const providerThreadId = ProviderThreadId.make("provider_thread_provider_turn_start_spend_guard");
const messageId = MessageId.make("message_provider_turn_start_spend_guard");
const projection = {
thread: {
id: threadId,
projectId: ProjectId.make("project_provider_turn_start_spend_guard"),
title: input.title,
modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5-codex" },
worktreePath: "/tmp/provider-turn-start-spend-guard",
pullRequests:
input.pullRequestLinkedAt === undefined
? []
: [{ number: 1, source: "created", linkedAt: input.pullRequestLinkedAt }],
lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
},
runs: [
{ id: RunId.make("run_provider_turn_start_spend_guard_first"), ordinal: 1 },
{
id: runId,
status: "starting",
rootNodeId,
activeAttemptId: attemptId,
providerThreadId,
userMessageId: messageId,
ordinal: 2,
},
],
nodes: [{ id: rootNodeId, checkpointScopeId: CheckpointScopeId.make("scope_spend_guard") }],
attempts: [{ id: attemptId }],
providerThreads: [{ id: providerThreadId, providerSessionId: ProviderSessionId.make("s") }],
providerTurns: input.providerTurns.map((turn) => ({
startedAt: DateTime.makeUnsafe(turn.startedAt),
completedAt: null,
tokenUsage: { usedTokens: 0, inputTokens: turn.inputTokens, updatedAt: turn.startedAt },
})),
messages: [
{
id: messageId,
role: "user",
text: "Status?",
attachments: [],
creationSource: input.creationSource,
},
],
checkpointScopes: [{ id: CheckpointScopeId.make("scope_spend_guard") }],
checkpoints: [],
contextHandoffs: [],
contextTransfers: [],
turnItems: [],
} as unknown as OrchestrationV2ThreadProjection;
const writeIfRunCurrent = vi.fn<EventSink.EventSinkV2["Service"]["writeIfRunCurrent"]>(() =>
Effect.succeed({ committed: true, storedEvents: [] }),
);
const startRootRun = vi.fn(() => Effect.void);
const layer = ProviderTurnStart.layer.pipe(
Layer.provide(
Layer.mergeAll(
Layer.mock(ContextHandoffService.ContextHandoffServiceV2)({}),
Layer.mock(EventSink.EventSinkV2)({ writeIfRunCurrent }),
IdAllocator.layer,
Layer.mock(TurnItemPositionStore.TurnItemPositionStoreV2)({}),
Layer.succeed(FileSystem.FileSystem, {
realPath: (target: string) => Effect.succeed(target),
} as never),
Path.layer,
Layer.mock(ServerConfig.ServerConfig)({
worktreesDir: managedWorktreesDir,
} as ServerConfig.ServerConfig["Service"]),
ServerSettings.layerTest({
sessionSpendGuard: { mode: "enforce", sessionCeilingUsd: 1, idleStopUsd: 50 },
usagePriceOverrides: {
"gpt-5-codex": { inputCostPerMillionTokens: 1_000_000, outputCostPerMillionTokens: 0 },
},
}),
Layer.mock(GitWorkflow.GitWorkflowService)({}),
Layer.mock(ProjectService.ProjectService)({}),
Layer.mock(ProjectionStore.ProjectionStoreV2)({
getThreadRecords: () => Effect.succeed(projection),
getTurnStartContext: () => Effect.succeed({ ...projection, hasConversation: true }),
}),
Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({}),
Layer.mock(ProviderAuthService.ProviderAuthService)({}),
Layer.mock(RunExecutionService.RunExecutionServiceV2)({ startRootRun }),
Layer.mock(RuntimePolicy.RuntimePolicyV2)({}),
),
),
);
const refusal = () => {
const item = writeIfRunCurrent.mock.calls[0]?.[0].events.find(
(event) => event.type === "turn-item.updated",
)?.payload;
return item?.type === "error" ? item : undefined;
};
return {
start: Effect.gen(function* () {
yield* (yield* ProviderTurnStart.ProviderTurnStartServiceV2).start({ threadId, runId });
}).pipe(Effect.provide(layer)),
refusal,
startRootRun,
};
}

effectIt.effect("refuses an automated follow-up on a thread that still has the default title", () =>
Effect.gen(function* () {
const harness = startWithSpendGuard({
title: "New thread",
creationSource: "mcp",
providerTurns: [],
});
yield* harness.start;
expect(harness.refusal()).toMatchObject({
title: "Turn refused by the session spend guard",
failure: { message: expect.stringContaining("default title") },
});
expect(harness.startRootRun).not.toHaveBeenCalled();
}),
);

effectIt.effect(
"refuses the next automated turn at the session ceiling even after a linked pull request",
() =>
Effect.gen(function* () {
const harness = startWithSpendGuard({
title: "Manager work",
creationSource: "mcp",
pullRequestLinkedAt: "2099-01-01T00:00:00.000Z",
providerTurns: [
{ startedAt: "2099-01-01T00:00:01.000Z", inputTokens: 1 },
{ startedAt: "2099-01-01T00:00:02.000Z", inputTokens: 1 },
],
});
yield* harness.start;
const refusal = harness.refusal();
expect(refusal?.title).toBe("Turn refused by the session spend guard");
expect(refusal?.failure.message).toContain("session ceiling");
expect(refusal?.failure.message).not.toContain("idle limit");
expect(harness.startRootRun).not.toHaveBeenCalled();
}),
);

effectIt.effect("lets a founder's own turn through at the session ceiling", () =>
Effect.gen(function* () {
const harness = startWithSpendGuard({
title: "Manager work",
creationSource: "web",
providerTurns: [{ startedAt: "2099-01-01T00:00:01.000Z", inputTokens: 1 }],
});
yield* Effect.exit(harness.start);
expect(harness.refusal()?.title).not.toBe("Turn refused by the session spend guard");
}),
);

effectIt.effect("counts every provider turn in the thread toward the session ceiling", () =>
Effect.gen(function* () {
const harness = startWithSpendGuard({
title: "Manager work",
creationSource: "mcp",
providerTurns: Array.from({ length: 501 }, (_, index) => ({
startedAt: "2099-01-01T00:00:00.000Z",
inputTokens: index === 0 ? 1 : 0,
})),
});
yield* harness.start;
expect(harness.refusal()?.failure.message).toContain("session ceiling");
expect(harness.startRootRun).not.toHaveBeenCalled();
}),
);

const restoreLeaseExists = vi.fn(() => Effect.succeed(false));
const restoreLeaseCreateWorktree = vi.fn(() => Effect.succeed({} as never));
const layerRestoreLeaseTest = layerThreadWorktreeRestore.pipe(
Expand Down
50 changes: 50 additions & 0 deletions apps/server/src/orchestration-v2/ProviderTurnStartService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ import {
} from "@t3tools/provider-core/server/failure";
import * as RunExecutionService from "./RunExecutionService.ts";
import * as RuntimePolicy from "./RuntimePolicy.ts";
import {
checkLaunch,
refusesAutomatedTurn,
describeSpendStop,
evaluateThreadSpend,
SPEND_GUARD_REFUSAL_TITLE,
UNTITLED_LAUNCH_REFUSAL,
} from "./SessionSpendGuard.ts";
import * as TurnItemPositionStore from "./TurnItemPositionStore.ts";
import {
isRestartNoteContinuation,
Expand Down Expand Up @@ -197,6 +205,7 @@ export const layer: Layer.Layer<
const providerSessions = yield* ProviderSessionManager.ProviderSessionManagerV2;
const runExecution = yield* RunExecutionService.RunExecutionServiceV2;
const runtimePolicy = yield* RuntimePolicy.RuntimePolicyV2;
const serverSettings = yield* ServerSettings.ServerSettingsService;
const restoreMissingWorktree = yield* makeRestoreMissingWorktree;
const turnItemPositions = yield* TurnItemPositionStore.TurnItemPositionStoreV2;

Expand Down Expand Up @@ -552,6 +561,47 @@ export const layer: Layer.Layer<
});
},
);
const guardSettings = yield* serverSettings.getSettings;
const launchRefusal = checkLaunch({
title: projection.thread.title,
titlePending: projection.thread.titleRegeneration != null,
hasOtherUserMessages: projection.runs.some((candidate) => candidate.ordinal < run.ordinal),
creationSource: message.creationSource,
mode: guardSettings.sessionSpendGuard.mode,
});
if (launchRefusal !== null) {
yield* settleStartFailure({
signal: "spend-guard-refused",
title: SPEND_GUARD_REFUSAL_TITLE,
error: new Error(UNTITLED_LAUNCH_REFUSAL),
message: UNTITLED_LAUNCH_REFUSAL,
});
return;
}
if (refusesAutomatedTurn(guardSettings.sessionSpendGuard.mode, message.creationSource)) {
const { checkpoints } = yield* projectionStore.getThreadRecords(input.threadId, [
"checkpoints",
]);
const spend = evaluateThreadSpend(
{
thread: projection.thread,
providerTurns: projection.providerTurns,
checkpoints,
turnItems: [],
},
guardSettings,
);
if (spend.level === "stop") {
const detail = describeSpendStop(spend, guardSettings.sessionSpendGuard);
yield* settleStartFailure({
signal: "spend-guard-refused",
title: SPEND_GUARD_REFUSAL_TITLE,
error: new Error(detail),
message: detail,
});
return;
}
}
if (
providerMessage.attachments.length === 0 &&
providerMessage.text.trimStart().startsWith("/")
Expand Down
Loading
Loading