Skip to content

Commit 2a0c059

Browse files
Merge remote-tracking branch 'origin/main' into fix/issue-17531
2 parents e703b2d + f416175 commit 2a0c059

28 files changed

Lines changed: 2051 additions & 80 deletions

‎apps/desktop/src/ipc/methods/sshEnvironment.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export class DesktopSshEnvironmentRequestError extends Data.TaggedError(
8383
readonly cause: DesktopSshEnvironmentRequestCause;
8484
readonly sshHttpStatus: number | null;
8585
}> {
86+
// The web client matches "[ssh_http:500]" to explain version-skewed SSH pairing.
8687
override get message() {
8788
const prefix = this.sshHttpStatus === null ? "" : `[ssh_http:${this.sshHttpStatus}] `;
8889
return `${prefix}SSH remote API request failed during ${this.operation}.`;

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

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,26 @@ const activeRunStatuses: ReadonlySet<ProjectionRun["status"]> = new Set([
351351
"waiting",
352352
]);
353353

354+
// A fork inherits its source's history before it has messages of its own,
355+
// unless a portable handoff's budget left none of that history to deliver.
356+
const inheritsForkConversation = ({
357+
thread,
358+
contextTransfers,
359+
contextHandoffs,
360+
}: Pick<OrchestrationV2ThreadProjection, "thread" | "contextTransfers" | "contextHandoffs">) => {
361+
if (thread.forkedFrom?.type !== "run") return false;
362+
const resolution = contextTransfers.find(
363+
(transfer) => transfer.type === "fork" && transfer.targetThreadId === thread.id,
364+
)?.resolution;
365+
return (
366+
resolution?.strategy !== "portable_context" ||
367+
contextHandoffs.some(
368+
(handoff) =>
369+
handoff.id === resolution.contextHandoffId && (handoff.history?.messages.length ?? 0) > 0,
370+
)
371+
);
372+
};
373+
354374
// In-memory twin of the omitSettledLeafNodes SQL clause in readCanonicalProjection.
355375
const isSettledLeafNode = (node: ProjectionNode, runs: ReadonlyArray<ProjectionRun>) =>
356376
settledLeafNodeKinds.has(node.kind) &&
@@ -4265,7 +4285,9 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
42654285
) AS present
42664286
`;
42674287
return {
4268-
hasConversation: conversation[0]?.present === 1,
4288+
hasConversation:
4289+
conversation[0]?.present === 1 ||
4290+
inheritsForkConversation({ thread, contextTransfers, contextHandoffs }),
42694291
thread,
42704292
runs,
42714293
attempts,
@@ -7249,12 +7271,13 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
72497271
service.getThreadProjection(threadId).pipe(
72507272
Effect.map((projection) => ({
72517273
...projection,
7252-
hasConversation: projection.messages.some(
7253-
(message) =>
7254-
message.role === "user" &&
7255-
(message.text.trim().toLowerCase() !== "/compact" ||
7256-
message.attachments.length > 0),
7257-
),
7274+
hasConversation:
7275+
projection.messages.some(
7276+
(message) =>
7277+
message.role === "user" &&
7278+
(message.text.trim().toLowerCase() !== "/compact" ||
7279+
message.attachments.length > 0),
7280+
) || inheritsForkConversation(projection),
72587281
turnItems: projection.turnItems.filter((item) => item.runId === runId),
72597282
})),
72607283
),

‎apps/server/src/orchestration-v2/ThreadFork.execution.test.ts‎

Lines changed: 216 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ import * as TestClock from "effect/testing/TestClock";
2020

2121
import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
2222
import { ClaudeProviderCapabilitiesV2 } from "./Adapters/ClaudeAdapterV2.ts";
23+
import * as EffectWorker from "./EffectWorker.ts";
2324
import * as EventSink from "./EventSink.ts";
2425
import * as Orchestrator from "./Orchestrator.ts";
2526
import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts";
2627
import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts";
27-
import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
28+
import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
2829

2930
const forkCases = (
3031
[
@@ -395,3 +396,217 @@ it.effect.each(forkCases)(
395396
assert.isNull(target.providerThreads[0]?.forkedFrom);
396397
}).pipe(Effect.provide(layer)),
397398
);
399+
400+
it.effect.each([
401+
{ execution: "portable_context", rejected: true },
402+
{ execution: "native_fork", rejected: false },
403+
] as const)(
404+
"a $execution fork's first /compact is rejected only when no history reaches it: $rejected",
405+
({ execution, rejected }) => {
406+
const opened: Array<ProviderInstanceId> = [];
407+
const adapter = (driverName: "claudeAgent" | "codex") => {
408+
const driver = ProviderDriverKind.make(driverName);
409+
const instanceId = ProviderInstanceId.make(driverName);
410+
return {
411+
instanceId,
412+
driver,
413+
getCapabilities: () =>
414+
Effect.succeed(
415+
driver === "codex" ? CodexProviderCapabilitiesV2 : ClaudeProviderCapabilitiesV2,
416+
),
417+
planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" }),
418+
openSession: (input) =>
419+
Effect.sync(() => opened.push(instanceId)).pipe(
420+
Effect.andThen(
421+
new ProviderAdapter.ProviderAdapterOpenSessionError({
422+
driver,
423+
providerSessionId: input.providerSessionId,
424+
}),
425+
),
426+
),
427+
} satisfies ProviderAdapter.ProviderAdapterV2["Service"];
428+
};
429+
const claude = adapter("claudeAgent");
430+
const target = execution === "portable_context" ? adapter("codex") : claude;
431+
return Effect.gen(function* () {
432+
const orchestrator = yield* Orchestrator.OrchestratorV2;
433+
const eventSink = yield* EventSink.EventSinkV2;
434+
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
435+
const now = yield* DateTime.now;
436+
const sourceThreadId = ThreadId.make("fork-compact-source");
437+
const targetThreadId = ThreadId.make("fork-compact-target");
438+
const providerThreadId = ProviderThreadId.make("fork-compact-native-thread");
439+
const sourceRunId = RunId.make("fork-compact-source-run");
440+
const userMessageId = MessageId.make("fork-compact-source-message");
441+
const sourceSelection = { instanceId: claude.instanceId, model: "test-model" };
442+
443+
yield* orchestrator.dispatch({
444+
type: "thread.create",
445+
commandId: CommandId.make("fork-compact-create-source"),
446+
threadId: sourceThreadId,
447+
projectId: ProjectId.make("fork-compact-project"),
448+
title: "Fork compact source",
449+
modelSelection: sourceSelection,
450+
runtimeMode: "full-access",
451+
interactionMode: "default",
452+
branch: null,
453+
worktreePath: null,
454+
createdBy: "user",
455+
creationSource: "web",
456+
});
457+
const item = {
458+
threadId: sourceThreadId,
459+
runId: sourceRunId,
460+
nodeId: null,
461+
providerThreadId,
462+
providerTurnId: null,
463+
nativeItemRef: null,
464+
parentItemId: null,
465+
status: "completed",
466+
title: null,
467+
startedAt: now,
468+
completedAt: now,
469+
updatedAt: now,
470+
} as const;
471+
yield* eventSink.write({
472+
events: [
473+
{
474+
id: EventId.make("fork-compact-provider-thread"),
475+
type: "provider-thread.updated",
476+
threadId: sourceThreadId,
477+
occurredAt: now,
478+
payload: {
479+
id: providerThreadId,
480+
driver: claude.driver,
481+
providerInstanceId: claude.instanceId,
482+
providerSessionId: null,
483+
appThreadId: sourceThreadId,
484+
ownerNodeId: null,
485+
nativeThreadRef: {
486+
driver: claude.driver,
487+
nativeId: "native-fork-compact-source",
488+
strength: "strong",
489+
},
490+
nativeConversationHeadRef: null,
491+
status: "idle",
492+
firstRunOrdinal: 1,
493+
lastRunOrdinal: 1,
494+
handoffIds: [],
495+
forkedFrom: null,
496+
createdAt: now,
497+
updatedAt: now,
498+
},
499+
},
500+
{
501+
id: EventId.make("fork-compact-source-run"),
502+
type: "run.created",
503+
threadId: sourceThreadId,
504+
runId: sourceRunId,
505+
occurredAt: now,
506+
payload: {
507+
id: sourceRunId,
508+
threadId: sourceThreadId,
509+
ordinal: 1,
510+
providerInstanceId: claude.instanceId,
511+
modelSelection: sourceSelection,
512+
providerThreadId,
513+
userMessageId,
514+
rootNodeId: null,
515+
activeAttemptId: null,
516+
status: "completed",
517+
queuePosition: null,
518+
requestedAt: now,
519+
startedAt: now,
520+
completedAt: now,
521+
checkpointId: null,
522+
contextHandoffId: null,
523+
},
524+
},
525+
// Each message alone exceeds the default 16k handoff budget, so a
526+
// portable handoff omits both whole items.
527+
{
528+
id: EventId.make("fork-compact-user-item"),
529+
type: "turn-item.updated",
530+
threadId: sourceThreadId,
531+
runId: sourceRunId,
532+
occurredAt: now,
533+
payload: {
534+
...item,
535+
id: TurnItemId.make("fork-compact-user-item"),
536+
ordinal: 1,
537+
type: "user_message",
538+
createdBy: "user",
539+
creationSource: "web",
540+
inputIntent: "turn_start",
541+
messageId: userMessageId,
542+
text: "u".repeat(17_000),
543+
attachments: [],
544+
},
545+
},
546+
{
547+
id: EventId.make("fork-compact-assistant-item"),
548+
type: "turn-item.updated",
549+
threadId: sourceThreadId,
550+
runId: sourceRunId,
551+
occurredAt: now,
552+
payload: {
553+
...item,
554+
id: TurnItemId.make("fork-compact-assistant-item"),
555+
ordinal: 2,
556+
type: "assistant_message",
557+
messageId: MessageId.make("fork-compact-assistant-message"),
558+
text: "a".repeat(17_000),
559+
streaming: false,
560+
},
561+
},
562+
],
563+
});
564+
yield* orchestrator.dispatch({
565+
type: "thread.fork",
566+
commandId: CommandId.make("fork-compact-fork"),
567+
sourceThreadId,
568+
targetThreadId,
569+
sourcePoint: { type: "run", runId: sourceRunId },
570+
createdBy: "user",
571+
creationSource: "web",
572+
});
573+
yield* orchestrator.dispatch({
574+
type: "message.dispatch",
575+
commandId: CommandId.make("fork-compact-compact"),
576+
threadId: targetThreadId,
577+
messageId: MessageId.make("fork-compact-compact"),
578+
text: "/compact",
579+
attachments: [],
580+
modelSelection: { instanceId: target.instanceId, model: "test-model" },
581+
dispatchMode: { type: "start_immediately" },
582+
createdBy: "user",
583+
creationSource: "web",
584+
});
585+
yield* worker.drain();
586+
587+
const projection = yield* orchestrator.getThreadProjection(targetThreadId);
588+
if (execution === "portable_context") {
589+
assert.equal(projection.contextTransfers[0]?.resolution?.strategy, "portable_context");
590+
assert.deepEqual(projection.contextHandoffs[0]?.history?.messages, []);
591+
} else {
592+
assert.isEmpty(projection.contextHandoffs);
593+
}
594+
const emptyCompaction = projection.turnItems.some(
595+
(turnItem) =>
596+
turnItem.type === "error" &&
597+
turnItem.failure.message === "Start a conversation before compacting this thread.",
598+
);
599+
assert.equal(emptyCompaction, rejected);
600+
assert.deepEqual(opened, rejected ? [] : [target.instanceId]);
601+
}).pipe(
602+
Effect.provide(
603+
ProviderReplayHarness.layerWithRegistry(
604+
{ name: `fork-compact-${execution}` },
605+
ProviderAdapterRegistry.layerFromAdapters(
606+
target === claude ? [claude] : [claude, target],
607+
),
608+
),
609+
),
610+
);
611+
},
612+
);

0 commit comments

Comments
 (0)