Skip to content

Commit da42b86

Browse files
committed
Merge remote-tracking branch 'origin/main' into perf/connect-and-shell-sync-fixes
2 parents 14ea5c4 + 7d3635e commit da42b86

29 files changed

Lines changed: 1273 additions & 108 deletions

‎apps/mobile/src/connection/runtime.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ const layerProvidedClientConnection = layerSnapshotLoader.pipe(
4040
usageLimitSources: true,
4141
usageLimitsCommand: true,
4242
providerCatalogsOnDemand: true,
43+
providerCatalogDigests: true,
44+
providerStatusDeltas: true,
4345
}),
4446
),
4547
Layer.provideMerge(

‎apps/mobile/src/state/server.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export const serverEnvironment = createServerEnvironmentAtoms(connectionAtomRunt
1414
usageLimitSources: true,
1515
usageLimitsCommand: true,
1616
providerCatalogsOnDemand: true,
17+
providerCatalogDigests: true,
18+
providerStatusDeltas: true,
19+
omitRefreshedProviders: true,
1720
});
1821
export const describeOutdatedServer = createOutdatedServerDescriptionCommand(connectionAtomRuntime);
1922
export const updateOutdatedServer = createOutdatedServerUpdateCommand(connectionAtomRuntime);

‎apps/mobile/src/widgets/useSubscriptionUsage.test.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { EnvironmentId } from "@t3tools/contracts";
2+
import * as Option from "effect/Option";
23
import { Atom, AtomRegistry } from "effect/reactivity";
34
import { describe, expect, it, vi } from "vite-plus/test";
45

@@ -38,6 +39,18 @@ vi.mock("../state/presentation", async () => {
3839
};
3940
});
4041

42+
vi.mock("../state/shell", async () => {
43+
const { Atom } = await import("effect/reactivity");
44+
const Option = await import("effect/Option");
45+
return {
46+
environmentShell: {
47+
stateValueAtom: Atom.family((_id: string) =>
48+
Atom.make({ status: "synchronizing", error: Option.none<string>() }),
49+
),
50+
},
51+
};
52+
});
53+
4154
vi.mock("../state/server", () => ({
4255
serverEnvironment: { refreshProviders: { label: "Refresh providers" } },
4356
}));
@@ -56,6 +69,7 @@ vi.mock("@t3tools/client-runtime/state/runtime", () => ({
5669
import { environmentCatalog } from "../connection/catalog";
5770
import { appAtomRegistry } from "../state/atom-registry";
5871
import { environmentPresentations } from "../state/presentation";
72+
import { environmentShell } from "../state/shell";
5973
import { installSubscriptionUsageRefresh } from "./useSubscriptionUsage";
6074

6175
const registry = appAtomRegistry as AtomRegistry.AtomRegistry;
@@ -66,8 +80,16 @@ const presentationsAtom = environmentPresentations.presentationsAtom as unknown
6680
Map<string, { connection: { phase: string } }>
6781
>;
6882

83+
const shellAtom = (environmentId: EnvironmentId) =>
84+
environmentShell.stateValueAtom(environmentId) as unknown as Atom.Writable<{
85+
status: string;
86+
error: Option.Option<string>;
87+
}>;
88+
6989
const connected = EnvironmentId.make("env-connected");
7090
const connecting = EnvironmentId.make("env-connecting");
91+
const syncing = EnvironmentId.make("env-syncing");
92+
const failed = EnvironmentId.make("env-failed");
7193

7294
describe("installSubscriptionUsageRefresh", () => {
7395
it("refreshes connected environments once the catalog is ready and the app is active", () => {
@@ -79,8 +101,13 @@ describe("installSubscriptionUsageRefresh", () => {
79101
new Map([
80102
[connected, { connection: { phase: "connected" } }],
81103
[connecting, { connection: { phase: "connecting" } }],
104+
[syncing, { connection: { phase: "connected" } }],
105+
[failed, { connection: { phase: "connected" } }],
82106
]),
83107
);
108+
registry.set(shellAtom(connected), { status: "live", error: Option.none() });
109+
// A shell that failed never goes live; its quotas still refresh.
110+
registry.set(shellAtom(failed), { status: "cached", error: Option.some("Shell failed") });
84111

85112
const dispose = installSubscriptionUsageRefresh();
86113
registry.set(catalogAtom, { isReady: true });
@@ -89,7 +116,11 @@ describe("installSubscriptionUsageRefresh", () => {
89116

90117
fixture.appState = "active";
91118
for (const listener of fixture.appStateListeners) listener();
92-
expect(fixture.refreshed).toEqual([connected]);
119+
expect(fixture.refreshed).toEqual([connected, failed]);
120+
121+
// The refresh waits for the thread list, so it never competes with it on a cold start.
122+
registry.set(shellAtom(syncing), { status: "live", error: Option.none() });
123+
expect(fixture.refreshed).toEqual([connected, failed, syncing]);
93124

94125
dispose();
95126
expect(fixture.appStateListeners.size).toBe(0);

‎apps/mobile/src/widgets/useSubscriptionUsage.ts‎

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
11
import { runAtomCommand } from "@t3tools/client-runtime/state/runtime";
22
import type { EnvironmentId } from "@t3tools/contracts";
3+
import * as Option from "effect/Option";
4+
import { Atom } from "effect/reactivity";
35

46
import { environmentCatalog } from "../connection/catalog";
57
import { isAppActive, subscribeAppState } from "../lib/appState";
68
import { appAtomRegistry } from "../state/atom-registry";
79
import { environmentPresentations } from "../state/presentation";
810
import { serverEnvironment } from "../state/server";
11+
import { environmentShell } from "../state/shell";
912
import { createWidgetRefresher, WIDGET_REFRESH_INTERVAL } from "./subscriptionUsageSnapshot";
1013

14+
// A shell that failed never goes live, and its environment still needs fresh quotas.
15+
const shellSettledAtom = Atom.family((environmentId: EnvironmentId) =>
16+
Atom.make((get) => {
17+
const shell = get(environmentShell.stateValueAtom(environmentId));
18+
return shell.status === "live" || Option.isSome(shell.error);
19+
}),
20+
);
21+
22+
/**
23+
* Connected environments whose thread list has loaded. A refresh re-probes every provider on
24+
* the host, so on a cold start it waits until it cannot compete with the thread list.
25+
*/
26+
const refreshableEnvironmentsAtom = Atom.make((get) =>
27+
[...get(environmentPresentations.presentationsAtom)]
28+
.filter(
29+
([environmentId, presentation]) =>
30+
presentation.connection.phase === "connected" && get(shellSettledAtom(environmentId)),
31+
)
32+
.map(([environmentId]) => environmentId),
33+
);
34+
1135
/**
1236
* App-lifetime worker that keeps provider quotas fresh for the usage widget:
1337
* on resume, on every connection change, and on a fixed interval while active.
@@ -24,23 +48,18 @@ export function installSubscriptionUsageRefresh(): () => void {
2448
);
2549
const update = () => {
2650
if (!isAppActive() || !appAtomRegistry.get(environmentCatalog.catalogValueAtom).isReady) return;
27-
const connected = [...appAtomRegistry.get(environmentPresentations.presentationsAtom)]
28-
.filter(([, presentation]) => presentation.connection.phase === "connected")
29-
.map(([id]) => id);
30-
void refresh(connected, Date.now());
51+
void refresh(appAtomRegistry.get(refreshableEnvironmentsAtom), Date.now());
3152
};
3253

3354
const unsubscribeCatalog = appAtomRegistry.subscribe(environmentCatalog.catalogValueAtom, update);
34-
const unsubscribePresentations = appAtomRegistry.subscribe(
35-
environmentPresentations.presentationsAtom,
36-
update,
37-
{ immediate: true },
38-
);
55+
const unsubscribeEnvironments = appAtomRegistry.subscribe(refreshableEnvironmentsAtom, update, {
56+
immediate: true,
57+
});
3958
const unsubscribeAppState = subscribeAppState(update);
4059
const timer = setInterval(update, WIDGET_REFRESH_INTERVAL);
4160
return () => {
4261
unsubscribeCatalog();
43-
unsubscribePresentations();
62+
unsubscribeEnvironments();
4463
unsubscribeAppState();
4564
clearInterval(timer);
4665
};

‎apps/server/integration/TransferBudgetReport.integration.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ const FLEET_SHELL_BUDGET = {
106106
// About 14% above the observed 75 KB. The window holds the working run only: 2 nodes, 2 subagents,
107107
// 1 plan, 1 provider turn and the last finished turn's checkpoint. When finished runs ride along
108108
// it holds 82 nodes, 82 subagents, 41 plans, 41 provider turns, 40 checkpoints and 740 KB.
109+
// The working run's ready handoff ships without its 256 KB history; with it the open is 339 KB.
109110
const STALE_TODO_BUDGET = {
110111
decodedBytes: 85_000,
111112
rows: 75,

‎apps/server/integration/TransferBudgetV2Fixture.integration.ts‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
CheckpointId,
33
CheckpointRef,
44
CheckpointScopeId,
5+
ContextHandoffId,
56
EventId,
67
MessageId,
78
NodeId,
@@ -446,6 +447,7 @@ export const STALE_TODO_THREAD_ID = ThreadId.make("transfer-budget-stale-todo-li
446447
export const STALE_TODO_SETTLED_RUNS = 40;
447448
export const STALE_TODO_SUBAGENTS_PER_RUN = 2;
448449
export const STALE_TODO_LIVE_ROWS = 600;
450+
const STALE_TODO_HANDOFF_HISTORY_BYTES = 256 * 1024;
449451
const staleTodoCheckpointId = (index: number) =>
450452
CheckpointId.make(`checkpoint:thread:${STALE_TODO_THREAD_ID}:ordinal:${index + 1}`);
451453
/** The checkpoint clients read the last finished turn's diff from. */
@@ -742,7 +744,50 @@ export function staleTodoListThreadBatches(
742744
exitCode: 0,
743745
},
744746
});
745-
if (live) return events;
747+
if (live) {
748+
// A ready handoff rides along with every window. Its history and delivery are for the
749+
// provider turn start and must not reach a client.
750+
const earlierItemId = TurnItemId.make(`turn-item:stale-todo:${index}:handoff-source`);
751+
events.push({
752+
...common,
753+
id: EventId.make(`stale-todo-handoff-${index}`),
754+
type: "context-handoff.updated",
755+
payload: {
756+
id: ContextHandoffId.make(`handoff:run:${runKey}`),
757+
transferId: null,
758+
threadId,
759+
targetRunId: runId,
760+
fromProviderThreadIds: [providerThreadId],
761+
toProviderThreadId: providerThreadId,
762+
coveredRunOrdinals: { from: 1, to: index },
763+
strategy: "full_thread_summary",
764+
status: "ready",
765+
summaryMessageId: null,
766+
summaryText: text(index, 6, 1_024),
767+
history: {
768+
messages: [
769+
{
770+
role: "user",
771+
text: text(index, 7, STALE_TODO_HANDOFF_HISTORY_BYTES),
772+
threadId,
773+
runId: null,
774+
itemId: earlierItemId,
775+
providerThreadId,
776+
status: "completed",
777+
kind: "user_message",
778+
},
779+
],
780+
coverage: "full",
781+
omittedItems: 0,
782+
},
783+
delivery: { nativeThreadId: "native", status: "injected", itemIds: [earlierItemId] },
784+
createdByProviderInstanceId: providerInstanceId,
785+
createdAt: now,
786+
updatedAt: now,
787+
},
788+
});
789+
return events;
790+
}
746791

747792
message("assistant", text(index, 5, 1_200));
748793
const checkpointId = staleTodoCheckpointId(index);

‎apps/server/integration/transferBudgetV2.integration.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,8 @@ it.live("reports V2 thread HTTP and WebSocket transfer budgets", () =>
448448
staleTodo.subagents.filter((subagent) => subagent.runId === STALE_TODO_LIVE_RUN_ID),
449449
STALE_TODO_SUBAGENTS_PER_RUN,
450450
);
451+
// The byte cap below also passes when the window loses the handoff.
452+
assert.lengthOf(staleTodo.contextHandoffs, 1);
451453
const staleTodoColdOpen = {
452454
http: staleTodoHttp,
453455
rows: staleTodo.visibleTurnItems.length,

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3664,6 +3664,64 @@ describe("ClaudeAdapterV2 background wake turns", () => {
36643664
});
36653665
const makeWakeHarness = makeWakeHarnessWithOptions();
36663666

3667+
it.effect("resumes the session a /clear selected after the Claude process is replaced", () =>
3668+
Effect.gen(function* () {
3669+
const harness = yield* makeWakeHarnessWithOptions({
3670+
freshQueueOnReopen: true,
3671+
close: (queue) => Queue.shutdown(queue),
3672+
});
3673+
const turn = (ordinal: number, providerThread: OrchestrationV2ProviderThread) =>
3674+
makeClaudeTestTurnInput({
3675+
threadId: harness.threadId,
3676+
providerThread,
3677+
now: DateTime.makeUnsafe(0),
3678+
attemptId: RunAttemptId.make(`clear-session-${ordinal}`),
3679+
text: ordinal === 1 ? "/clear" : "Continue the cleared conversation",
3680+
attachments: [],
3681+
providerTurnOrdinal: ordinal,
3682+
});
3683+
yield* harness.runtime.startTurn(turn(1, harness.providerThread));
3684+
// Claude Code reports the session /clear started in its next init frame.
3685+
yield* harness.offerAndWait(
3686+
claudeSdkFrame({
3687+
type: "system",
3688+
subtype: "init",
3689+
session_id: "cleared-native-session",
3690+
uuid: "00000000-0000-4000-8000-000000000501",
3691+
}),
3692+
);
3693+
yield* harness.offerAndWait(turnOneResult);
3694+
yield* Queue.take(harness.terminalReceipts);
3695+
const latest = (): OrchestrationV2ProviderThread => {
3696+
const event = harness.events.findLast(
3697+
(candidate) => candidate.type === "provider_thread.updated",
3698+
);
3699+
return event?.type === "provider_thread.updated"
3700+
? event.providerThread
3701+
: harness.providerThread;
3702+
};
3703+
assert.equal(latest().nativeThreadRef?.nativeId, "cleared-native-session");
3704+
yield* harness.runtime.interruptTurn({
3705+
providerThread: latest(),
3706+
providerTurnId: lastProviderTurn(harness.events).id,
3707+
requestRuntimeRestart: true,
3708+
});
3709+
yield* harness.runtime.startTurn(turn(2, latest()));
3710+
assert.equal(harness.processQueues.length, 2);
3711+
assert.equal(harness.getOpenedOptions()?.resume, "cleared-native-session");
3712+
}).pipe(
3713+
Effect.scoped,
3714+
Effect.provide(
3715+
Layer.mergeAll(
3716+
IdAllocator.layer,
3717+
McpProviderSessions.layer,
3718+
NodeServices.layer,
3719+
NodeCrypto.layer,
3720+
),
3721+
),
3722+
),
3723+
);
3724+
36673725
it.effect.each(["release-after", "cancel-after", "release-before", "resume-pending"] as const)(
36683726
"keeps attachment reservations across compaction: %s boundary",
36693727
(outcome) =>

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5846,6 +5846,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
58465846
);
58475847
const absorbedWakes = yield* Ref.make(new Set<string>());
58485848
const heldProposedPlansByToolUseId = new Map<string, string>();
5849+
// /clear moves a live process to a new native session; persist that one so a reopen resumes it.
5850+
const clearedNativeSessions = new Map<string, string>();
58495851
const runtimeContext = yield* Effect.context<never>();
58505852
const runPromise = Effect.runPromiseWith(runtimeContext);
58515853

@@ -5867,6 +5869,23 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
58675869
},
58685870
};
58695871
}
5872+
const nativeSessionId = clearedNativeSessions.get(
5873+
event.providerThread.nativeThreadRef?.nativeId ?? "",
5874+
);
5875+
if (nativeSessionId !== undefined) {
5876+
event = {
5877+
...event,
5878+
providerThread: {
5879+
...event.providerThread,
5880+
nativeThreadRef: {
5881+
driver: CLAUDE_PROVIDER,
5882+
nativeId: nativeSessionId,
5883+
strength: "strong",
5884+
},
5885+
nativeConversationHeadRef: null,
5886+
},
5887+
};
5888+
}
58705889
}
58715890
return Queue.offer(events, event).pipe(Effect.asVoid);
58725891
};
@@ -11999,6 +12018,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1199912018
path,
1200012019
});
1200112020
const closedExistingNativeThreadId = existing !== null ? existing.nativeThreadId : null;
12021+
clearedNativeSessions.delete(nativeThreadId);
1200212022
if (existing !== null) {
1200312023
yield* existing.query.close.pipe(Effect.ignore);
1200412024
if (existing.nativeThreadId !== nativeThreadId) {
@@ -12159,6 +12179,14 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1215912179
context.capabilities = new Set(message.capabilities ?? []);
1216012180
context.claudeCodeVersion = message.claude_code_version;
1216112181
}
12182+
if (
12183+
message.type === "system" &&
12184+
message.subtype === "init" &&
12185+
message.session_id.length > 0 &&
12186+
message.session_id !== context.nativeThreadId
12187+
) {
12188+
clearedNativeSessions.set(context.nativeThreadId, message.session_id);
12189+
}
1216212190
if (
1216312191
message.type === "system" &&
1216412192
(message.subtype === "init" || message.subtype === "status") &&
@@ -12903,7 +12931,11 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1290312931

1290412932
const closeLiveQueryForNativeThread = Effect.fnUntraced(function* (nativeThreadId: string) {
1290512933
const existing = yield* Ref.get(queryContext);
12906-
if (existing === null || existing.nativeThreadId !== nativeThreadId) {
12934+
if (
12935+
existing === null ||
12936+
(existing.nativeThreadId !== nativeThreadId &&
12937+
clearedNativeSessions.get(existing.nativeThreadId) !== nativeThreadId)
12938+
) {
1290712939
return;
1290812940
}
1290912941

@@ -12925,7 +12957,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1292512957
);
1292612958
if (ownedLiveQuery) {
1292712959
yield* finalizeActiveTurnAfterQueryExit(existing);
12928-
yield* endSubagentCallsOfClosedQuery(nativeThreadId, "interrupted");
12960+
yield* endSubagentCallsOfClosedQuery(existing.nativeThreadId, "interrupted");
1292912961
} else {
1293012962
yield* endSubagentsOfReplacedQuery(existing);
1293112963
}

0 commit comments

Comments
 (0)