Skip to content

Commit a64a651

Browse files
Merge remote-tracking branch 'origin/main' into pr-33042
2 parents fca3e3a + d1aeed2 commit a64a651

10 files changed

Lines changed: 335 additions & 43 deletions

File tree

‎apps/server/src/orchestration-v2/ProjectionStore.test.ts‎

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as NodeSqlite from "node:sqlite";
2+
13
import { assert, it, vi } from "@effect/vitest";
24
import {
35
EnvironmentId,
@@ -57,6 +59,13 @@ import { limitRecoveryCommand } from "./UsageLimitRecoveryWorker.ts";
5759

5860
import { layerReaderDatabase, traceSqlRouting } from "../persistence/testkit/SqliteRouting.ts";
5961

62+
declare module "node:sqlite" {
63+
interface StatementSync {
64+
// Node 26 has this; @types/node 24 does not declare it yet.
65+
stat(counter: "vmStep"): number;
66+
}
67+
}
68+
6069
const layerTest = Layer.mergeAll(
6170
ProjectionStore.layer.pipe(Layer.provideMerge(SqlitePersistence.layerMemory)),
6271
SqlitePersistence.layerMemory,
@@ -3356,6 +3365,137 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
33563365
yield* sql`UPDATE orchestration_v2_projection_turn_items SET ordinal = 999,
33573366
payload_json = json_set(payload_json, '$.ordinal', 999)
33583367
WHERE turn_item_id = 'turn-item:bounded-sql-history:999'`;
3368+
// Work folded below the newest 502 visible rows still counts its whole group, so the
3369+
// 100 commands after the messages stay hidden in the 398-item group.
3370+
yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'assistant_message',
3371+
payload_json = json_set(payload_json, '$.type', 'assistant_message', '$.text', 'Working',
3372+
'$.messageId', turn_item_id, '$.streaming', json('false'))
3373+
WHERE thread_id = ${threadId} AND ordinal BETWEEN 300 AND 899`;
3374+
const straddledWindow = yield* projectionStore.getThreadSnapshotWindow(threadId, {
3375+
rowLimit: sqlPageLimit,
3376+
userTurnLimit: 10,
3377+
deferWork: true,
3378+
});
3379+
const straddledOrdinals = [
3380+
...Array.from({ length: sqlPageLimit }, (_, index) => 900 - sqlPageLimit + index),
3381+
1000,
3382+
];
3383+
assert.deepEqual(
3384+
straddledWindow.projection.turnItems.map((item) => item.ordinal),
3385+
straddledOrdinals,
3386+
);
3387+
// A run that starts with work (a wake) has no segment start, so the group begins at its first item.
3388+
const moveFirstItemToRun = (runId: string) => sql`
3389+
UPDATE orchestration_v2_projection_turn_items SET run_id = ${runId},
3390+
payload_json = json_set(payload_json, '$.runId', ${runId})
3391+
WHERE thread_id = ${threadId} AND ordinal = 1`;
3392+
yield* moveFirstItemToRun("run:bounded-sql-history:2");
3393+
const wakeWindow = yield* projectionStore.getThreadSnapshotWindow(threadId, {
3394+
rowLimit: sqlPageLimit,
3395+
userTurnLimit: 10,
3396+
deferWork: true,
3397+
});
3398+
assert.deepEqual(
3399+
wakeWindow.projection.turnItems.map((item) => item.ordinal),
3400+
straddledOrdinals,
3401+
);
3402+
yield* moveFirstItemToRun(sharedRunId);
3403+
// A failed error below the tail still blocks folding for its run. Only the SQL columns
3404+
// change; the row sits below the window, so its payload is never decoded.
3405+
// A steer at 100 starts a later segment, so the error sits below the scan start too.
3406+
const setItem = (ordinal: number, type: string, status: string) => sql`
3407+
UPDATE orchestration_v2_projection_turn_items SET type = ${type}, status = ${status}
3408+
WHERE thread_id = ${threadId} AND ordinal = ${ordinal}`;
3409+
yield* setItem(100, "user_message", "completed");
3410+
yield* setItem(2, "error", "failed");
3411+
const blockedWindow = yield* projectionStore.getThreadSnapshotWindow(threadId, {
3412+
rowLimit: sqlPageLimit,
3413+
userTurnLimit: 10,
3414+
deferWork: true,
3415+
});
3416+
assert.isFalse(
3417+
blockedWindow.projection.visibleTurnItems.some((row) => row.item.deferredWork),
3418+
);
3419+
assert.deepEqual(
3420+
blockedWindow.projection.turnItems.map((item) => item.ordinal),
3421+
Array.from({ length: sqlPageLimit }, (_, index) => 1001 - sqlPageLimit + index),
3422+
);
3423+
yield* setItem(2, "command_execution", "completed");
3424+
yield* setItem(100, "command_execution", "completed");
3425+
yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'command_execution',
3426+
payload_json = json_set(payload_json, '$.type', 'command_execution')
3427+
WHERE thread_id = ${threadId} AND ordinal BETWEEN 300 AND 899`;
3428+
3429+
// Runless rows below a run that starts after the newest 502 visible rows stay in the window.
3430+
yield* sql`UPDATE orchestration_v2_projection_turn_items
3431+
SET run_id = CASE WHEN ordinal > 990 THEN 'run:bounded-sql-history:991' END,
3432+
type = CASE WHEN ordinal = 991 THEN 'user_message' ELSE 'assistant_message' END,
3433+
payload_json = CASE WHEN ordinal = 991
3434+
THEN json_set(payload_json, '$.type', 'user_message', '$.runId', 'run:bounded-sql-history:991',
3435+
'$.createdBy', 'user', '$.creationSource', 'web', '$.inputIntent', 'turn_start',
3436+
'$.messageId', turn_item_id, '$.text', 'Turn 991', '$.attachments', json('[]'))
3437+
ELSE json_set(payload_json, '$.type', 'assistant_message',
3438+
'$.runId', CASE WHEN ordinal > 990 THEN 'run:bounded-sql-history:991' END,
3439+
'$.text', 'Working', '$.messageId', turn_item_id, '$.streaming', json('false'))
3440+
END
3441+
WHERE thread_id = ${threadId} AND ordinal > 1`;
3442+
const windowStatements: Array<readonly [string, ReadonlyArray<unknown>]> = [];
3443+
const runlessWindow = yield* projectionStore
3444+
.getThreadSnapshotWindow(threadId, {
3445+
rowLimit: sqlPageLimit,
3446+
userTurnLimit: 10,
3447+
deferWork: true,
3448+
})
3449+
.pipe(
3450+
Effect.provideService(Statement.CurrentTransformer, (statement) => {
3451+
windowStatements.push(statement.compile());
3452+
return Effect.succeed(statement);
3453+
}),
3454+
);
3455+
// The window read must not grow with history below the newest visible rows. VM steps are
3456+
// deterministic; reading the whole history here cost 65% more steps.
3457+
const copyPath = "file:/thread-window-steps?vfs=memdb";
3458+
{
3459+
using copy = new NodeSqlite.DatabaseSync(copyPath);
3460+
yield* sql`VACUUM INTO ${copyPath}`;
3461+
assert.isNotEmpty(windowStatements);
3462+
const windowSteps = () =>
3463+
windowStatements.reduce((steps, [query, params]) => {
3464+
const statement = copy.prepare(query);
3465+
statement.all(...(params as ReadonlyArray<NodeSqlite.SQLInputValue>));
3466+
return steps + statement.stat("vmStep");
3467+
}, 0);
3468+
const fullSteps = windowSteps();
3469+
assert.isAbove(fullSteps, 0);
3470+
copy.exec(
3471+
"DELETE FROM orchestration_v2_projection_turn_items WHERE ordinal BETWEEN 2 AND 498",
3472+
);
3473+
assert.isAtMost(fullSteps, windowSteps() * 1.1);
3474+
}
3475+
assert.deepEqual(
3476+
runlessWindow.projection.turnItems.map((item) => item.ordinal),
3477+
Array.from({ length: THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT }, (_, index) => 499 + index),
3478+
);
3479+
const runlessPage = buildBoundedThreadProjection({
3480+
projection: runlessWindow.projection,
3481+
snapshotSequence: runlessWindow.snapshotSequence,
3482+
});
3483+
assert.isTrue(runlessPage.hasMoreHistory);
3484+
assert.isNotNull(runlessPage.historyCursor);
3485+
// Limits SQLite reads differently from `tailRows` skip the tail bound and match main.
3486+
const windowOrdinalCount = (rowLimit: number) =>
3487+
projectionStore
3488+
.getThreadSnapshotWindow(threadId, { rowLimit, userTurnLimit: 10, deferWork: true })
3489+
.pipe(Effect.map((window) => window.projection.turnItems.length));
3490+
assert.strictEqual(yield* windowOrdinalCount(502.5), THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT);
3491+
yield* sql`UPDATE orchestration_v2_projection_turn_items
3492+
SET payload_json = json_set(payload_json, '$.inputIntent', 'steer')
3493+
WHERE thread_id = ${threadId} AND ordinal = 991`;
3494+
assert.strictEqual(yield* windowOrdinalCount(-1), 1000);
3495+
yield* sql`UPDATE orchestration_v2_projection_turn_items SET run_id = ${sharedRunId},
3496+
type = 'command_execution',
3497+
payload_json = json_set(payload_json, '$.type', 'command_execution', '$.runId', ${sharedRunId})
3498+
WHERE thread_id = ${threadId} AND ordinal > 1`;
33593499
yield* sql`
33603500
UPDATE orchestration_v2_projection_turn_items
33613501
SET type = 'command_execution', payload_json = json_set(payload_json, '$.type', 'command_execution')

‎apps/server/src/orchestration-v2/ProjectionStore.ts‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ import {
101101
isConversationHistoryItem,
102102
THREAD_HISTORY_MAX_RAW_TURNS,
103103
missingInterruptRequests,
104+
DEFERRED_WORK_ITEM_TYPES,
104105
deferWorkHistoryRows,
105106
selectDeferredWorkPage,
106107
encodeThreadHistoryCursor,
@@ -3032,14 +3033,56 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
30323033
SELECT ordinal, (SELECT MAX(ordinal) FROM user_anchors) AS latest FROM boundary
30333034
`;
30343035
const boundary = anchors[0]!;
3036+
// Folding needs whole segments, so the window would walk every item since the boundary.
3037+
// Non-deferrable types always keep their own row, so the window stays above the newest
3038+
// `tailRows` of them. Start at the latest segment start of each run that reaches that
3039+
// point, clamped to it so runless rows below stay in the scan.
3040+
const tailRows = Math.max(window.rowLimit, THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT);
3041+
const tailWorkStart = sql<{ readonly ordinal: number | null }>`
3042+
${eligible}, visible_tail AS (
3043+
SELECT ordinal FROM eligible
3044+
WHERE ordinal >= ${boundary.ordinal}
3045+
AND type NOT IN ${sql.in(DEFERRED_WORK_ITEM_TYPES)}
3046+
ORDER BY ordinal DESC
3047+
LIMIT ${tailRows}
3048+
), tail AS (
3049+
SELECT MIN(ordinal) AS ordinal FROM visible_tail HAVING COUNT(*) >= ${tailRows}
3050+
), tail_runs AS (
3051+
SELECT DISTINCT item.run_id
3052+
FROM tail CROSS JOIN eligible AS item
3053+
WHERE item.ordinal >= tail.ordinal AND item.run_id IS NOT NULL
3054+
)
3055+
SELECT MIN(MIN(COALESCE(
3056+
(
3057+
SELECT start.ordinal FROM eligible AS start
3058+
WHERE start.run_id = tail_runs.run_id AND start.type = 'user_message'
3059+
AND start.ordinal <= tail.ordinal
3060+
ORDER BY start.ordinal DESC LIMIT 1
3061+
),
3062+
(
3063+
SELECT MIN(first.ordinal) FROM orchestration_v2_projection_turn_items AS first
3064+
WHERE first.thread_id = ${threadId} AND first.run_id = tail_runs.run_id
3065+
),
3066+
tail.ordinal
3067+
), tail.ordinal)) AS ordinal
3068+
FROM tail LEFT JOIN tail_runs
3069+
`;
3070+
// A negative or fractional limit makes SQLite's LIMIT disagree with `tailRows`; keep the old start.
3071+
const workStart =
3072+
window.deferWork === true &&
3073+
boundary.latest !== null &&
3074+
Number.isSafeInteger(window.rowLimit) &&
3075+
window.rowLimit >= 0
3076+
? Math.max(boundary.ordinal, (yield* tailWorkStart)[0]?.ordinal ?? 0)
3077+
: boundary.ordinal;
30353078
return sql`
30363079
${eligible}, blocked_runs AS MATERIALIZED (
30373080
SELECT DISTINCT run_id FROM orchestration_v2_projection_turn_items
30383081
WHERE thread_id = ${threadId} AND run_id IS NOT NULL
30393082
AND ${window.deferWork ? 1 : 0} = 1
30403083
AND run_id IN (
30413084
SELECT run_id FROM eligible
3042-
WHERE ordinal >= ${boundary.ordinal} AND run_id IS NOT NULL
3085+
WHERE ordinal >= ${workStart} AND run_id IS NOT NULL
30433086
)
30443087
AND (type IN ('run_interrupt_request', 'run_interrupt_result')
30453088
OR (type = 'error' AND status = 'failed' AND parent_item_id IS NULL))
@@ -3048,14 +3091,14 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
30483091
MAX(CASE WHEN type = 'user_message' THEN ordinal ELSE 0 END)
30493092
OVER (PARTITION BY run_id ORDER BY ordinal) AS segment,
30503093
CASE WHEN status = 'completed' AND (run_status = 'completed' OR attempt_status = 'superseded')
3051-
AND type IN ('command_execution', 'reasoning', 'file_change', 'file_search', 'web_search', 'dynamic_tool')
3094+
AND type IN ${sql.in(DEFERRED_WORK_ITEM_TYPES)}
30523095
AND NOT (${inlineWorkOutput})
30533096
AND run_id NOT IN (SELECT run_id FROM blocked_runs)
30543097
THEN 1 ELSE 0 END AS can_defer
30553098
FROM eligible AS item
30563099
WHERE ${window.deferWork ? 1 : 0} = 1
30573100
AND ${boundary.latest === null ? 0 : 1} = 1
3058-
AND ordinal >= ${boundary.ordinal}
3101+
AND ordinal >= ${workStart}
30593102
), deferred_work AS (
30603103
SELECT run_id, segment, attempt_id, MIN(ordinal) AS first_ordinal,
30613104
MAX(ordinal) AS last_ordinal, COUNT(*) AS item_count
@@ -3090,6 +3133,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
30903133
FROM window_items
30913134
WHERE ordinal >= ${boundary.ordinal}
30923135
ORDER BY ordinal DESC, turn_item_id DESC
3136+
-- Every LIMIT here and in latest_user_turn must stay <= tailRows, or rows below workStart go missing.
30933137
LIMIT CASE
30943138
WHEN ${window.rowLimit} = 0 THEN 0
30953139
WHEN (SELECT row_count FROM latest_user_turn)

‎apps/server/src/orchestration-v2/threadHistoryPaging.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ function isThreadHistoryTurnStart(item: HistoryRow["item"]): boolean {
183183
);
184184
}
185185

186-
const DEFERRED_WORK_ITEM_TYPES = [
186+
export const DEFERRED_WORK_ITEM_TYPES = [
187187
"command_execution",
188188
"reasoning",
189189
"file_change",

‎apps/server/src/provider/ClaudeProvider.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ import {
4040
type ServerProviderDraft,
4141
} from "@t3tools/provider-core/server/snapshotProbe";
4242
import { resolveClaudeSdkExecutablePath } from "./Drivers/ClaudeExecutable.ts";
43-
import { hasClaudeManagedMcpConfig, makeClaudeEnvironment } from "./Drivers/ClaudeHome.ts";
43+
import {
44+
claudeSignedOutMessage,
45+
hasClaudeManagedMcpConfig,
46+
makeClaudeEnvironment,
47+
} from "./Drivers/ClaudeHome.ts";
4448
import { discoverClaudeSkills, readClaudeSettingsEnv } from "./Drivers/ClaudeSkills.ts";
4549
import type { ProviderWorkspaceSnapshot } from "@t3tools/provider-core/server/driver";
4650
import { makeUnavailableUsageLimits } from "@t3tools/provider-core/server/usageLimits";
@@ -674,6 +678,9 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")(
674678
endpoint.hostname !== "" &&
675679
endpoint.hostname !== "api.anthropic.com";
676680
if (!customApiEndpoint && claudeAuthStatus(capabilities) === "unauthenticated") {
681+
const path = yield* Path.Path;
682+
const configDir = (yield* makeClaudeEnvironment(claudeSettings, resolvedEnvironment))
683+
.CLAUDE_CONFIG_DIR;
677684
return buildServerProvider({
678685
presentation: CLAUDE_PRESENTATION,
679686
enabled: claudeSettings.enabled,
@@ -687,7 +694,10 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")(
687694
version: parsedVersion,
688695
status: "error",
689696
auth: { status: "unauthenticated" },
690-
message: "Claude Code is not authenticated. Run `claude auth login` and try again.",
697+
message:
698+
configDir === undefined
699+
? "Claude Code is not authenticated. Run `claude auth login` and try again."
700+
: claudeSignedOutMessage({ configDir, cwd: path.resolve(cwd ?? ".") }),
691701
},
692702
});
693703
}

‎apps/server/src/provider/ProviderRegistry.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3192,6 +3192,64 @@ it.layer(
31923192
),
31933193
);
31943194

3195+
it.effect.each([
3196+
{
3197+
name: "its own home path",
3198+
homePath: "/srv/claude-personal",
3199+
environment: { ANTHROPIC_BASE_URL: "" },
3200+
configDir: "/srv/claude-personal",
3201+
},
3202+
{
3203+
name: "an inherited CLAUDE_CONFIG_DIR",
3204+
homePath: "",
3205+
environment: { ANTHROPIC_BASE_URL: "", CLAUDE_CONFIG_DIR: "/srv/claude-work" },
3206+
configDir: "/srv/claude-work",
3207+
},
3208+
{
3209+
name: "the default config dir",
3210+
homePath: "",
3211+
environment: { ANTHROPIC_BASE_URL: "" },
3212+
configDir: undefined,
3213+
},
3214+
])(
3215+
"tells a logged-out instance on $name where to sign in",
3216+
({ homePath, environment, configDir }) =>
3217+
Effect.gen(function* () {
3218+
const status = yield* checkClaudeProviderStatus(
3219+
{ ...defaultClaudeSettings, homePath },
3220+
claudeCapabilities({
3221+
tokenSource: "none",
3222+
apiKeySource: "none",
3223+
apiProvider: "firstParty",
3224+
}),
3225+
environment,
3226+
);
3227+
assert.strictEqual(status.auth.status, "unauthenticated");
3228+
if (configDir === undefined) {
3229+
assert.strictEqual(
3230+
status.message,
3231+
"Claude Code is not authenticated. Run `claude auth login` and try again.",
3232+
);
3233+
} else {
3234+
// A home path is resolved to a native path; an inherited value is kept literal.
3235+
const expected = homePath ? (yield* Path.Path).resolve(configDir) : configDir;
3236+
assert.include(
3237+
status.message ?? "",
3238+
`CLAUDE_CONFIG_DIR set to ${JSON.stringify(expected)}`,
3239+
"a bare `claude auth login` signs in ~/.claude, not this instance's config dir",
3240+
);
3241+
}
3242+
}).pipe(
3243+
Effect.provide(
3244+
layerMockSpawner((args) => {
3245+
const joined = args.join(" ");
3246+
if (joined === "--version") return { stdout: "1.0.0\n", stderr: "", code: 0 };
3247+
throw new Error(`Unexpected args: ${joined}`);
3248+
}),
3249+
),
3250+
),
3251+
);
3252+
31953253
it.effect("tells a logged-out CLI which models need a Claude Code update", () =>
31963254
Effect.gen(function* () {
31973255
const status = yield* checkClaudeProviderStatus(

0 commit comments

Comments
 (0)