|
1 | 1 | import { assert, it } from "@effect/vitest"; |
| 2 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
2 | 3 | import { |
3 | 4 | CheckpointId, |
4 | 5 | CheckpointRef, |
5 | 6 | CheckpointScopeId, |
6 | 7 | MessageId, |
| 8 | + GitCommandError, |
7 | 9 | type ModelSelection, |
8 | 10 | NodeId, |
9 | 11 | type OrchestrationV2AppThread, |
@@ -28,13 +30,16 @@ import * as DateTime from "effect/DateTime"; |
28 | 30 | import * as Deferred from "effect/Deferred"; |
29 | 31 | import * as Fiber from "effect/Fiber"; |
30 | 32 | import * as Effect from "effect/Effect"; |
| 33 | +import * as FileSystem from "effect/FileSystem"; |
31 | 34 | import * as Layer from "effect/Layer"; |
32 | 35 | import * as SqlClient from "effect/sql/SqlClient"; |
33 | 36 | import * as Stream from "effect/Stream"; |
34 | 37 | import * as TestClock from "effect/testing/TestClock"; |
35 | 38 |
|
36 | 39 | import { toolOutputImages } from "@t3tools/shared/toolOutput"; |
37 | 40 | import * as SqlitePersistence from "../persistence/Sqlite.ts"; |
| 41 | +import * as ServerConfig from "../config.ts"; |
| 42 | +import * as GitVcsDriver from "../vcs/GitVcsDriver.ts"; |
38 | 43 | import * as EventSink from "./EventSink.ts"; |
39 | 44 | import * as EventStore from "./EventStore.ts"; |
40 | 45 | import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; |
@@ -58,23 +63,54 @@ const layerTestEventSink = EventSink.layer.pipe( |
58 | 63 | Layer.provide(Layer.mergeAll(layerTestStores, layerTestDatabase)), |
59 | 64 | ); |
60 | 65 |
|
| 66 | +const layerGit = GitVcsDriver.layer.pipe( |
| 67 | + Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "ingestor-git-" })), |
| 68 | + Layer.provideMerge(NodeServices.layer), |
| 69 | +); |
| 70 | + |
61 | 71 | const layerTest = Layer.mergeAll( |
62 | 72 | layerTestDatabase, |
63 | 73 | layerTestStores, |
64 | 74 | layerTestEventSink, |
65 | 75 | IdAllocator.layer, |
66 | 76 | ThreadCommandExecutor.layer, |
| 77 | + layerGit, |
67 | 78 | ProviderEventIngestor.layer.pipe( |
68 | 79 | Layer.provide( |
69 | 80 | Layer.mergeAll( |
70 | 81 | layerTestStores, |
71 | 82 | layerTestEventSink, |
72 | 83 | IdAllocator.layer, |
73 | 84 | ThreadCommandExecutor.layer, |
| 85 | + layerGit, |
74 | 86 | ), |
75 | 87 | ), |
76 | 88 | ), |
77 | 89 | ); |
| 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 | + |
| 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 | +}); |
78 | 114 | const modelSelection = { |
79 | 115 | instanceId: ProviderInstanceId.make("codex"), |
80 | 116 | model: "gpt-5.4", |
@@ -1729,6 +1765,280 @@ layer("ProviderEventIngestorV2", (it) => { |
1729 | 1765 | }), |
1730 | 1766 | ); |
1731 | 1767 |
|
| 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 | + |
1732 | 2042 | it.effect("updates provider child metadata without recreating its saved thread", () => |
1733 | 2043 | Effect.gen(function* () { |
1734 | 2044 | const now = yield* DateTime.now; |
|