GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(server): subagents pick up their parent's renamed worktree branch… · pingdotgg/t3code@4e85a93

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/server/src/orchestration-v2
    • Orchestrator.ts
    • ProviderEventIngestor.test.ts
    • ProviderEventIngestor.ts
    • ProviderSessionManager.test.ts
    • ThreadLaunchService.test.ts
    • ThreadLaunchService.ts
    • runtimeLayer.ts
    • testkit
      • ProviderReplayHarness.ts
  • packages/contracts/src
    • orchestrationV2.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Orchestrator.ts
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2539,6 +2539,17 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
25392539
cause: `Thread ${command.threadId} worktree changed before the metadata update could be applied.`,
25402540
});
25412541
}
2542+
if (
2543+
command.type === "thread.metadata.update" &&
2544+
command.expectedBranch !== undefined &&
2545+
command.expectedBranch !== thread.branch
2546+
) {
2547+
return yield* new OrchestratorDispatchError({
2548+
commandId: command.commandId,
2549+
commandType: command.type,
2550+
cause: `Thread ${command.threadId} branch changed before the metadata update could be applied.`,
2551+
});
2552+
}
25422553
if (command.type === "thread.metadata.update" && command.expectedEmpty === true) {
25432554
const records = yield* projectionStore
25442555
.getThreadRecords(command.threadId, ["runs"])
Collapse file

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

Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts
+310Lines changed: 310 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { assert, it } from "@effect/vitest";
2+
import * as NodeServices from "@effect/platform-node/NodeServices";
23
import {
34
CheckpointId,
45
CheckpointRef,
56
CheckpointScopeId,
67
MessageId,
8+
GitCommandError,
79
type ModelSelection,
810
NodeId,
911
type OrchestrationV2AppThread,
@@ -28,13 +30,16 @@ import * as DateTime from "effect/DateTime";
2830
import * as Deferred from "effect/Deferred";
2931
import * as Fiber from "effect/Fiber";
3032
import * as Effect from "effect/Effect";
33+
import * as FileSystem from "effect/FileSystem";
3134
import * as Layer from "effect/Layer";
3235
import * as SqlClient from "effect/sql/SqlClient";
3336
import * as Stream from "effect/Stream";
3437
import * as TestClock from "effect/testing/TestClock";
3538

3639
import { toolOutputImages } from "@t3tools/shared/toolOutput";
3740
import * as SqlitePersistence from "../persistence/Sqlite.ts";
41+
import * as ServerConfig from "../config.ts";
42+
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
3843
import * as EventSink from "./EventSink.ts";
3944
import * as EventStore from "./EventStore.ts";
4045
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
@@ -58,23 +63,54 @@ const layerTestEventSink = EventSink.layer.pipe(
5863
Layer.provide(Layer.mergeAll(layerTestStores, layerTestDatabase)),
5964
);
6065

66+
const layerGit = GitVcsDriver.layer.pipe(
67+
Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "ingestor-git-" })),
68+
Layer.provideMerge(NodeServices.layer),
69+
);
70+
6171
const layerTest = Layer.mergeAll(
6272
layerTestDatabase,
6373
layerTestStores,
6474
layerTestEventSink,
6575
IdAllocator.layer,
6676
ThreadCommandExecutor.layer,
77+
layerGit,
6778
ProviderEventIngestor.layer.pipe(
6879
Layer.provide(
6980
Layer.mergeAll(
7081
layerTestStores,
7182
layerTestEventSink,
7283
IdAllocator.layer,
7384
ThreadCommandExecutor.layer,
85+
layerGit,
7486
),
7587
),
7688
),
7789
);
90+
91+
const makeSharedWorktree = Effect.gen(function* () {
92+
const fs = yield* FileSystem.FileSystem;
93+
const git = yield* GitVcsDriver.GitVcsDriver;
94+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "ingestor-worktree-" });
95+
const repo = `${directory}/repo`;
96+
const worktreePath = `${directory}/shared`;
97+
yield* fs.makeDirectory(repo);
98+
const command = (cwd: string, args: string[]) =>
99+
git.execute({ operation: "ingestor-test", cwd, args });
100+
yield* command(repo, ["init", "-b", "main"]);
101+
yield* command(repo, [
102+
"-c",
103+
"user.name=Test",
104+
"-c",
105+
"[email protected]",
106+
"commit",
107+
"--allow-empty",
108+
"-m",
109+
"initial",
110+
]);
111+
yield* command(repo, ["worktree", "add", "-b", "t3/abcd1234", worktreePath]);
112+
return { repo, worktreePath, command };
113+
});
78114
const modelSelection = {
79115
instanceId: ProviderInstanceId.make("codex"),
80116
model: "gpt-5.4",
@@ -1729,6 +1765,280 @@ layer("ProviderEventIngestorV2", (it) => {
17291765
}),
17301766
);
17311767

1768+
it.effect.each([
1769+
{ parentMoved: false, failure: null },
1770+
{ parentMoved: true, failure: null },
1771+
{ parentMoved: false, failure: "write" },
1772+
{ parentMoved: false, failure: "git" },
1773+
])(
1774+
"rechecks a pending native child's branch after the rename scan: %j",
1775+
({ parentMoved, failure }) =>
1776+
Effect.gen(function* () {
1777+
const { worktreePath, command } = yield* makeSharedWorktree;
1778+
const git = yield* GitVcsDriver.GitVcsDriver;
1779+
const now = yield* DateTime.now;
1780+
const eventSink = yield* EventSink.EventSinkV2;
1781+
const projections = yield* ProjectionStore.ProjectionStoreV2;
1782+
const ids = yield* IdAllocator.IdAllocatorV2;
1783+
const rootEvent = yield* threadCreatedEvent(now);
1784+
if (rootEvent.type !== "thread.created") throw new Error("Expected thread fixture");
1785+
const parent = {
1786+
...rootEvent.payload,
1787+
branch: "t3/abcd1234",
1788+
worktreePath,
1789+
};
1790+
yield* eventSink.write({ events: [{ ...rootEvent, payload: parent }] });
1791+
const childId = ids.derive.threadFromProviderThread({
1792+
driver: CODEX_DRIVER,
1793+
nativeThreadId: `child-created-during-rename:${parentMoved}:${failure}`,
1794+
});
1795+
const beforeCommit = yield* Deferred.make<
1796+
void,
1797+
ProviderEventIngestor.ProviderEventIngestorV2Error
1798+
>();
1799+
const allowCommit = yield* Deferred.make<void>();
1800+
let correctionWrites = 0;
1801+
const gatedSink = EventSink.EventSinkV2.of({
1802+
...eventSink,
1803+
write: (input) => {
1804+
if (
1805+
input.events.some(
1806+
(event) => event.type === "thread.metadata-updated" && event.threadId === childId,
1807+
)
1808+
) {
1809+
correctionWrites++;
1810+
if (failure === "write" && correctionWrites === 1)
1811+
return Effect.fail(new EventSink.EventSinkWriteError({ eventCount: 1 }));
1812+
}
1813+
return input.events.some(
1814+
(event) => event.type === "thread.created" && event.threadId === childId,
1815+
)
1816+
? Deferred.succeed(beforeCommit, undefined).pipe(
1817+
Effect.andThen(Deferred.await(allowCommit)),
1818+
Effect.andThen(eventSink.write(input)),
1819+
)
1820+
: eventSink.write(input);
1821+
},
1822+
});
1823+
const ingestor = yield* ProviderEventIngestor.ProviderEventIngestorV2.pipe(
1824+
Effect.provide(Layer.fresh(ProviderEventIngestor.layer)),
1825+
Effect.provideService(EventSink.EventSinkV2, gatedSink),
1826+
Effect.provideService(GitVcsDriver.GitVcsDriver, {
1827+
...git,
1828+
resolveBranchName: (cwd, refName) =>
1829+
failure === "git"
1830+
? Effect.fail(
1831+
new GitCommandError({
1832+
operation: "ingestor-test",
1833+
cwd,
1834+
command: "rev-parse",
1835+
detail: "Read failed",
1836+
}),
1837+
)
1838+
: git.resolveBranchName(cwd, refName),
1839+
}),
1840+
);
1841+
const providerSessionId = yield* ids.allocate.providerSession({
1842+
providerInstanceId: modelSelection.instanceId,
1843+
threadId: parent.id,
1844+
});
1845+
const creationInput = {
1846+
providerSessionId,
1847+
providerInstanceId: modelSelection.instanceId,
1848+
threadId: parent.id,
1849+
event: {
1850+
type: "app_thread.created",
1851+
driver: CODEX_DRIVER,
1852+
appThread: {
1853+
...parent,
1854+
id: childId,
1855+
lineage: {
1856+
parentThreadId: parent.id,
1857+
rootThreadId: parent.id,
1858+
relationshipToParent: "subagent",
1859+
},
1860+
},
1861+
},
1862+
} satisfies ProviderEventIngestor.ProviderEventIngestInput;
1863+
const creation = yield* ingestor.ingestNormalized(creationInput).pipe(
1864+
Effect.tapCause((cause) => Deferred.failCause(beforeCommit, cause)),
1865+
Effect.forkScoped,
1866+
);
1867+
yield* Deferred.await(beforeCommit);
1868+
yield* command(worktreePath, ["branch", "-m", "generated-branch"]);
1869+
yield* eventSink.write({
1870+
events: [
1871+
{
1872+
...rootEvent,
1873+
id: yield* ids.allocate.event({ threadId: parent.id }),
1874+
type: "thread.metadata-updated",
1875+
payload: {
1876+
...parent,
1877+
branch: parentMoved ? "user/checkout" : "generated-branch",
1878+
worktreePath: parentMoved ? `${worktreePath}-moved` : worktreePath,
1879+
},
1880+
},
1881+
],
1882+
});
1883+
// The rename's final scan finishes while the child is still uncommitted.
1884+
const snapshot = yield* projections.getShellSnapshot();
1885+
assert.isFalse(
1886+
[...snapshot.threads, ...snapshot.archivedThreads].some(
1887+
(thread) =>
1888+
thread.worktreePath === parent.worktreePath && thread.branch === parent.branch,
1889+
),
1890+
);
1891+
yield* Deferred.succeed(allowCommit, undefined);
1892+
const stored = yield* Fiber.join(creation);
1893+
assert.equal(stored[0]?.event.type, "thread.created");
1894+
const child = yield* projections.getThread(childId);
1895+
assert.equal(child.branch, failure === null ? "generated-branch" : parent.branch);
1896+
assert.equal(child.worktreePath, parent.worktreePath);
1897+
assert.equal(correctionWrites, failure === "git" ? 0 : 1);
1898+
assert.deepEqual(yield* ingestor.ingestNormalized(creationInput), []);
1899+
}),
1900+
);
1901+
1902+
it.effect.each(["explicit", "detached", "unchanged", "legacy"])(
1903+
"preserves a native child's branch when the checkout needs no correction: %s",
1904+
(scenario) =>
1905+
Effect.gen(function* () {
1906+
const { worktreePath, command } = yield* makeSharedWorktree;
1907+
const git = yield* GitVcsDriver.GitVcsDriver;
1908+
const branch =
1909+
scenario === "explicit"
1910+
? "child-feature"
1911+
: scenario === "legacy"
1912+
? "t3-abcd1234"
1913+
: "t3/abcd1234";
1914+
if (scenario === "explicit" || scenario === "legacy")
1915+
yield* command(worktreePath, ["branch", "-m", branch]);
1916+
if (scenario === "detached") yield* command(worktreePath, ["checkout", "--detach"]);
1917+
const now = yield* DateTime.now;
1918+
const sink = yield* EventSink.EventSinkV2;
1919+
const projections = yield* ProjectionStore.ProjectionStoreV2;
1920+
const ids = yield* IdAllocator.IdAllocatorV2;
1921+
const root = yield* threadCreatedEvent(now);
1922+
if (root.type !== "thread.created") throw new Error("Expected thread fixture");
1923+
yield* sink.write({
1924+
events: [
1925+
{ ...root, payload: { ...root.payload, branch: "parent-feature", worktreePath } },
1926+
],
1927+
});
1928+
const childId = ids.derive.threadFromProviderThread({
1929+
driver: CODEX_DRIVER,
1930+
nativeThreadId: `no-correction:${scenario}`,
1931+
});
1932+
let gitReads = 0;
1933+
const ingestor = yield* ProviderEventIngestor.ProviderEventIngestorV2.pipe(
1934+
Effect.provide(Layer.fresh(ProviderEventIngestor.layer)),
1935+
Effect.provideService(GitVcsDriver.GitVcsDriver, {
1936+
...git,
1937+
resolveBranchName: (cwd, refName) => {
1938+
gitReads++;
1939+
return git.resolveBranchName(cwd, refName);
1940+
},
1941+
}),
1942+
);
1943+
const stored = yield* ingestor.ingestNormalized({
1944+
providerSessionId: yield* ids.allocate.providerSession({
1945+
providerInstanceId: modelSelection.instanceId,
1946+
threadId: root.threadId,
1947+
}),
1948+
providerInstanceId: modelSelection.instanceId,
1949+
threadId: root.threadId,
1950+
event: {
1951+
type: "app_thread.created",
1952+
driver: CODEX_DRIVER,
1953+
appThread: {
1954+
...root.payload,
1955+
id: childId,
1956+
branch,
1957+
worktreePath,
1958+
lineage: {
1959+
parentThreadId: root.threadId,
1960+
rootThreadId: root.threadId,
1961+
relationshipToParent: "subagent",
1962+
},
1963+
},
1964+
},
1965+
});
1966+
assert.equal((yield* projections.getThread(childId)).branch, branch);
1967+
assert.equal(stored.length, 1);
1968+
assert.equal(gitReads, scenario === "explicit" ? 0 : 1);
1969+
}),
1970+
);
1971+
1972+
it.effect.each(
1973+
["codex", "claude"].flatMap((driver) =>
1974+
["/repo/shared", "/repo/isolated", null].map((worktreePath) => ({ driver, worktreePath })),
1975+
),
1976+
)("records a native child with current shared workspace: %j", ({ driver, worktreePath }) =>
1977+
Effect.gen(function* () {
1978+
const { repo, worktreePath: sharedPath, command } = yield* makeSharedWorktree;
1979+
const isolatedPath = `${sharedPath}-isolated`;
1980+
yield* command(sharedPath, ["branch", "-m", "generated-branch"]);
1981+
yield* command(repo, ["worktree", "add", "-b", "t3/abcd1234", isolatedPath]);
1982+
const actualPath =
1983+
worktreePath === "/repo/shared" ? sharedPath : worktreePath === null ? null : isolatedPath;
1984+
const now = yield* DateTime.now;
1985+
const eventSink = yield* EventSink.EventSinkV2;
1986+
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
1987+
const ingestor = yield* ProviderEventIngestor.ProviderEventIngestorV2;
1988+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
1989+
const rootEvent = yield* threadCreatedEvent(now);
1990+
if (rootEvent.type !== "thread.created") throw new Error("Expected thread fixture");
1991+
const childThreadId = idAllocator.derive.threadFromProviderThread({
1992+
driver: ProviderDriverKind.make(driver),
1993+
nativeThreadId: `late-native-subagent:${worktreePath}`,
1994+
});
1995+
const offered: OrchestrationV2AppThread = {
1996+
...rootEvent.payload,
1997+
id: childThreadId,
1998+
branch: "t3/abcd1234",
1999+
worktreePath: actualPath,
2000+
lineage: {
2001+
parentThreadId: rootEvent.threadId,
2002+
relationshipToParent: "subagent",
2003+
rootThreadId: rootEvent.threadId,
2004+
},
2005+
};
2006+
yield* eventSink.write({
2007+
events: [
2008+
{
2009+
...rootEvent,
2010+
payload: {
2011+
...rootEvent.payload,
2012+
branch: "generated-branch",
2013+
worktreePath: sharedPath,
2014+
},
2015+
},
2016+
],
2017+
});
2018+
const providerSessionId = yield* idAllocator.allocate.providerSession({
2019+
providerInstanceId: modelSelection.instanceId,
2020+
threadId: rootEvent.threadId,
2021+
});
2022+
yield* ingestor.ingestNormalized({
2023+
providerSessionId,
2024+
providerInstanceId: modelSelection.instanceId,
2025+
threadId: rootEvent.threadId,
2026+
event: {
2027+
type: "app_thread.created",
2028+
driver: ProviderDriverKind.make(driver),
2029+
appThread: offered,
2030+
},
2031+
});
2032+
const child = yield* projectionStore.getThread(childThreadId);
2033+
assert.equal(
2034+
child.branch,
2035+
worktreePath === "/repo/shared" ? "generated-branch" : offered.branch,
2036+
);
2037+
assert.equal(child.worktreePath, actualPath);
2038+
assert.deepEqual(child.lineage, offered.lineage);
2039+
}),
2040+
);
2041+
17322042
it.effect("updates provider child metadata without recreating its saved thread", () =>
17332043
Effect.gen(function* () {
17342044
const now = yield* DateTime.now;

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.