Skip to content

Commit f0f0fed

Browse files
Merge remote-tracking branch 'origin/main' into takeover/19692
2 parents b4dfa8e + 3ff452b commit f0f0fed

35 files changed

Lines changed: 1668 additions & 260 deletions

‎apps/mobile/src/features/pull-requests/PullRequestsScreen.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import { useAndroidWorkspaceSelection } from "../workspace/useAndroidWorkspaceSe
4343
function PullRequestScrollView(props: ScrollViewProps) {
4444
const insets = useSafeAreaInsets();
4545
return (
46-
<ScrollViewMarker>
46+
<ScrollViewMarker style={{ flex: 1 }}>
4747
<ScrollView
4848
{...props}
4949
automaticallyAdjustContentInsets={false}

‎apps/mobile/src/lib/composerContext.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {
44
ProjectId,
55
ProviderInstanceId,
66
ComposerContextId,
7+
EnvironmentId,
8+
ThreadId,
79
type OrchestrationMessageContext,
810
} from "@t3tools/contracts";
911
import { collectComposerInlineTokens } from "@t3tools/shared/composerInlineTokens";
@@ -24,6 +26,7 @@ import {
2426
uploadedComposerContext,
2527
serializeComposerMessageForServer,
2628
pullRequestComposerContext,
29+
threadComposerContext,
2730
} from "./composerContext";
2831

2932
const terminal = {
@@ -318,3 +321,20 @@ describe("host context compatibility", () => {
318321
expect(context.records).toEqual([terminal, review, pr]);
319322
});
320323
});
324+
325+
describe("threadComposerContext", () => {
326+
it("mints a schema-valid, stable id for agent-spawned thread ids", () => {
327+
const environmentId = EnvironmentId.make("env-1");
328+
const ref = {
329+
environmentId,
330+
threadId: ThreadId.make(`thread:mcp:${encodeURIComponent("3f2b:delegate")}:review:0`),
331+
};
332+
const contextId = threadComposerContext(ref, "Review").contextId;
333+
334+
expect(() => ComposerContextId.make(contextId)).not.toThrow();
335+
expect(threadComposerContext(ref, "Renamed").contextId).toBe(contextId);
336+
expect(
337+
threadComposerContext({ environmentId, threadId: ThreadId.make("abc-123") }, "t").contextId,
338+
).toBe("thread_abc-123");
339+
});
340+
});

‎apps/mobile/src/lib/composerContext.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
isImageReferencesOnly,
2121
replaceComposerContextReferences,
2222
sanitizeComposerContextLabel,
23+
toKindScopedComposerContextId,
2324
} from "@t3tools/shared/composerContextReferences";
2425
import {
2526
collectComposerInlineTokens,
@@ -110,7 +111,7 @@ export function threadComposerContext(ref: ScopedThreadRef, title: string): Thre
110111
return {
111112
version: 1,
112113
kind: "thread",
113-
contextId: ComposerContextId.make(`thread_${ref.threadId}`),
114+
contextId: toKindScopedComposerContextId("thread", ref.threadId),
114115
label,
115116
environmentId: ref.environmentId,
116117
threadId: ref.threadId,

‎apps/server/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"proper-lockfile": "4.1.2",
6060
"stream-chain": "^4.2.5",
6161
"stream-json": "3.6.0",
62+
"undici": "catalog:",
6263
"yaml": "catalog:",
6364
"yauzl": "^3.4.0"
6465
},

‎apps/server/src/binCli.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { browserCommand } from "./cli/browser.ts";
1414
import { connectCommand } from "./cli/connect.ts";
1515
import { pairCommand } from "./cli/pair.ts";
1616
import { hasCloudPublicConfig } from "./cloud/publicConfig.ts";
17+
import { keepOutboundFetchOnHttp1 } from "./outboundHttp.ts";
1718
import { sharedServerCommandFlags } from "./cli/config.ts";
1819
import { projectCommand } from "./cli/project.ts";
1920
import { runDefaultServerCommand, serveCommand, startCommand } from "./cli/server.ts";
@@ -92,6 +93,7 @@ export const makeCli = ({ cloudEnabled = hasCloudPublicConfig } = {}) =>
9293
export const cli = makeCli();
9394

9495
export function runCli() {
96+
keepOutboundFetchOnHttp1();
9597
Command.run(cli, { version: packageJson.version }).pipe(
9698
Effect.scoped,
9799
Effect.provide(layerCliRuntime),

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,7 +1746,7 @@ it.effect("ProviderSessionManagerV2 releases sessions in parallel on server shut
17461746
}),
17471747
);
17481748

1749-
it.effect("ProviderSessionManagerV2 closes event subscriptions normally on server shutdown", () =>
1749+
it.effect("ProviderSessionManagerV2 interrupts event subscriptions on server shutdown", () =>
17501750
Effect.gen(function* () {
17511751
const state = yield* Ref.make(emptyState);
17521752
const effect = Effect.gen(function* () {
@@ -1781,7 +1781,10 @@ it.effect("ProviderSessionManagerV2 closes event subscriptions normally on serve
17811781

17821782
yield* manager.shutdown;
17831783

1784-
assert.isEmpty(yield* bufferedSubscription.events.pipe(Stream.runCollect));
1784+
// Buffered events are dropped and the consumer sees an interruption, not
1785+
// a clean end, so it leaves run settlement to restart recovery.
1786+
const exit = yield* bufferedSubscription.events.pipe(Stream.runCollect, Effect.exit);
1787+
assert.isTrue(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause));
17851788
});
17861789

17871790
yield* effect.pipe(Effect.provide(layerTest({ state, idleTimeoutMs: 60_000 })));

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -593,12 +593,15 @@ export const layerWithOptions = (
593593
);
594594
});
595595

596-
const closeSubscribers = (entry: LiveSessionEntry) =>
596+
// Server shutdown interrupts subscribers rather than ending them: a
597+
// cleanly ended stream without a terminal fails its run, while an
598+
// interrupted one leaves run settlement to restart recovery.
599+
const interruptSubscribers = (entry: LiveSessionEntry) =>
597600
Effect.gen(function* () {
598601
const subscribers = yield* Ref.getAndSet(entry.eventSubscribers, new Map());
599602
yield* Effect.forEach(
600603
subscribers.values(),
601-
(queue) => Queue.clear(queue).pipe(Effect.andThen(Queue.end(queue))),
604+
(queue) => Queue.clear(queue).pipe(Effect.andThen(Queue.interrupt(queue))),
602605
{ discard: true },
603606
);
604607
});
@@ -1078,7 +1081,7 @@ export const layerWithOptions = (
10781081
if (input.gracefulSubscribers === true) {
10791082
yield* endSubscribers(entry);
10801083
} else if (input.reason === "server_shutdown") {
1081-
yield* closeSubscribers(entry);
1084+
yield* interruptSubscribers(entry);
10821085
} else {
10831086
yield* failSubscribers(
10841087
entry,
@@ -2096,15 +2099,17 @@ export const layerWithOptions = (
20962099
}).pipe(logReleaseFailure(entry.runtime.providerSessionId));
20972100
return;
20982101
}
2099-
const cause = Exit.isFailure(exit)
2100-
? exit.cause
2101-
: Cause.fail(
2102-
new ProviderAdapter.ProviderAdapterEventStreamError({
2103-
driver: entry.runtime.driver,
2104-
providerSessionId: entry.runtime.providerSessionId,
2105-
cause: "Provider event stream ended unexpectedly.",
2106-
}),
2107-
);
2102+
// Subscribers read an interrupt as server shutdown, so keep it for that.
2103+
const cause =
2104+
Exit.isFailure(exit) && !Cause.hasInterruptsOnly(exit.cause)
2105+
? exit.cause
2106+
: Cause.fail(
2107+
new ProviderAdapter.ProviderAdapterEventStreamError({
2108+
driver: entry.runtime.driver,
2109+
providerSessionId: entry.runtime.providerSessionId,
2110+
cause: "Provider event stream ended unexpectedly.",
2111+
}),
2112+
);
21082113
yield* publishToSubscribers(entry.eventSubscribers, {
21092114
type: "failure",
21102115
cause,

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

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3742,9 +3742,11 @@ it.effect("does not refresh pull requests for auxiliary or stale provider termin
37423742
{ ...ids, rootProviderTurnId: ProviderTurnId.make("turn:previous") },
37433743
"interrupted",
37443744
),
3745+
rootTerminalEvent(ids, "cancelled"),
37453746
]),
37463747
});
3747-
assert.deepEqual(observed, []);
3748+
// Only the root terminal settles the run and refreshes pull requests.
3749+
assert.deepEqual(observed, ["run:cancelled", "pull-requests-refreshed"]);
37483750
}),
37493751
);
37503752

@@ -3772,6 +3774,58 @@ it.effect("refreshes pull requests after a provider stream exits with an error",
37723774
}),
37733775
);
37743776

3777+
it.effect("fails the run when its event stream ends without a root terminal", () =>
3778+
Effect.gen(function* () {
3779+
const { observed, finalEvents } = yield* captureRootRunTermination({
3780+
key: "stream-end-without-terminal",
3781+
shouldFinalizeRun: () => Effect.succeed(true),
3782+
events: () => Stream.empty,
3783+
});
3784+
assert.deepEqual(observed, ["run:failed", "pull-requests-refreshed"]);
3785+
assert.equal(
3786+
finalEvents.find((event) => event.type === "provider-thread.updated")?.payload.status,
3787+
"idle",
3788+
);
3789+
}),
3790+
);
3791+
3792+
it.effect("leaves the run to restart recovery when shutdown interrupts its event stream", () =>
3793+
Effect.gen(function* () {
3794+
const { written, observed } = yield* captureRootRunTermination({
3795+
key: "stream-end:shutdown",
3796+
shouldFinalizeRun: () => Effect.succeed(true),
3797+
events: () => Stream.failCause(Cause.interrupt()),
3798+
});
3799+
assert.deepEqual(written, []);
3800+
assert.deepEqual(observed, []);
3801+
}),
3802+
);
3803+
3804+
it.effect("does not start a turn whose run settled during the app context read", () =>
3805+
Effect.gen(function* () {
3806+
const contextReadStarted = yield* Deferred.make<void>();
3807+
const runFailed = yield* Deferred.make<void>();
3808+
const current = yield* Ref.make(true);
3809+
const providerCalls = yield* Ref.make(0);
3810+
const { observed } = yield* captureRootRunTermination({
3811+
key: "stream-end:context-read",
3812+
shouldStartProviderTurn: () => Ref.get(current),
3813+
shouldFinalizeRun: () => Ref.get(current),
3814+
events: () => Stream.unwrap(Deferred.await(contextReadStarted).pipe(Effect.as(Stream.empty))),
3815+
appContext: Deferred.succeed(contextReadStarted, undefined).pipe(
3816+
Effect.andThen(Deferred.await(runFailed)),
3817+
Effect.as([]),
3818+
),
3819+
refreshAfterTurn: Ref.set(current, false).pipe(
3820+
Effect.andThen(Deferred.succeed(runFailed, undefined)),
3821+
),
3822+
startTurn: () => Ref.update(providerCalls, (count) => count + 1),
3823+
});
3824+
assert.deepEqual(observed, ["run:failed", "pull-requests-refreshed"]);
3825+
assert.equal(yield* Ref.get(providerCalls), 0);
3826+
}),
3827+
);
3828+
37753829
it.effect("refreshes pull requests only once when startup failure closes its event stream", () =>
37763830
Effect.gen(function* () {
37773831
const ingestionStarted = yield* Deferred.make<void>();
@@ -3828,6 +3882,8 @@ function captureRootRunTermination(input: {
38283882
>;
38293883
readonly startTurn?: ProviderAdapter.ProviderAdapterV2SessionRuntime["startTurn"];
38303884
readonly refreshAfterTurn?: Effect.Effect<void>;
3885+
readonly shouldStartProviderTurn?: RunExecutionService.RunExecutionServiceV2StartRootRunInput["shouldStartProviderTurn"];
3886+
readonly appContext?: ReturnType<McpAppModelContext.McpAppModelContext["Service"]["forThread"]>;
38313887
}) {
38323888
return Effect.gen(function* () {
38333889
const ids = backgroundScenarioIds(input.key);
@@ -3840,6 +3896,7 @@ function captureRootRunTermination(input: {
38403896
status: "running",
38413897
});
38423898
const writtenItems = yield* Ref.make<ReadonlyArray<OrchestrationV2TurnItem>>([]);
3899+
const finalEvents = yield* Ref.make<ReadonlyArray<OrchestrationV2DomainEvent>>([]);
38433900
const observed = yield* Ref.make<ReadonlyArray<string>>([]);
38443901
const submittedEffects = yield* Ref.make<ReadonlyArray<PendingOrchestrationEffectV2>>([]);
38453902
const committedEffects = yield* Ref.make<ReadonlyArray<PendingOrchestrationEffectV2>>([]);
@@ -3848,6 +3905,7 @@ function captureRootRunTermination(input: {
38483905
Ref.update(writtenItems, (current) => [...current, payload]);
38493906
const captureFinalEvents = (events: ReadonlyArray<OrchestrationV2DomainEvent>) =>
38503907
Effect.gen(function* () {
3908+
yield* Ref.update(finalEvents, (current) => [...current, ...events]);
38513909
for (const event of events) {
38523910
if (event.type === "turn-item.updated") {
38533911
yield* captureTurnItem(event.payload);
@@ -3860,7 +3918,9 @@ function captureRootRunTermination(input: {
38603918
const layerTest = RunExecutionService.layer.pipe(
38613919
Layer.provide(
38623920
Layer.mergeAll(
3863-
McpAppModelContext.layerEmpty,
3921+
Layer.mock(McpAppModelContext.McpAppModelContext)({
3922+
forThread: () => input.appContext ?? Effect.succeed([]),
3923+
}),
38643924
Layer.mock(CheckpointService.CheckpointServiceV2)({ captureBaseline: () => Effect.void }),
38653925
Layer.mock(EventSink.EventSinkV2)({
38663926
write: (payload) =>
@@ -3970,6 +4030,9 @@ function captureRootRunTermination(input: {
39704030
attemptId: ids.attemptId,
39714031
providerTurnOrdinal: 1,
39724032
shouldFinalizeRun: input.shouldFinalizeRun,
4033+
...(input.shouldStartProviderTurn === undefined
4034+
? {}
4035+
: { shouldStartProviderTurn: input.shouldStartProviderTurn }),
39734036
...(input.hasUnpairedRunInterruptRequest === undefined
39744037
? {}
39754038
: {
@@ -4000,6 +4063,7 @@ function captureRootRunTermination(input: {
40004063
yield* Deferred.await(ingestionDone);
40014064
return {
40024065
written: yield* Ref.get(writtenItems),
4066+
finalEvents: yield* Ref.get(finalEvents),
40034067
observed: yield* Ref.get(observed),
40044068
submittedEffects: yield* Ref.get(submittedEffects),
40054069
committedEffects: yield* Ref.get(committedEffects),

0 commit comments

Comments
 (0)