Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions apps/server/src/project/AgentSessionImporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ const seedOwner = Effect.fn(function* (
});

const makeImporter = Effect.fn(function* (
candidates: ReadonlyArray<ReturnType<typeof candidate>>,
candidates: ReadonlyArray<AgentSessionScanner.AgentSessionRecentThread>,
beforeTransaction: Effect.Effect<void> = Effect.void,
getAdapter?: ProviderAdapterRegistry.ProviderAdapterRegistryV2Shape["get"],
threadCommands?: KeyedLock.KeyedLock<ThreadId>,
Expand All @@ -192,6 +192,7 @@ const makeImporter = Effect.fn(function* (
const events = yield* EventStore.EventStoreV2;
const projections = yield* ProjectionStore.ProjectionStoreV2;
const published: Array<ApplicationStoredEvent> = [];
const ownedSourceCalls: Array<ReadonlyArray<unknown>> = [];
const withTransaction: typeof sql.withTransaction = (effect) =>
beforeTransaction.pipe(Effect.andThen(sql.withTransaction(effect)));
const importSql = new Proxy(sql, {
Expand Down Expand Up @@ -229,7 +230,10 @@ const makeImporter = Effect.fn(function* (
yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository,
),
Layer.mock(AgentSessionScanner.AgentSessionScanner)({
recentThreads: () => Stream.fromIterable(candidates),
recentThreads: (_workspaceRoot, _completedSources, ownedSources = []) => {
ownedSourceCalls.push(ownedSources);
return Stream.fromIterable(candidates);
},
}),
Layer.mock(ProjectService.ProjectService)({
getById: () =>
Expand Down Expand Up @@ -270,6 +274,7 @@ const makeImporter = Effect.fn(function* (
run: importer.importRecentAgentThreads({ projectId }),
importNativeHistory: importer.importNativeHistory,
published,
ownedSourceCalls,
};
});

Expand Down Expand Up @@ -311,11 +316,14 @@ it.effect(
deleted,
ownedPi,
otherInstance,
{ _tag: "Deferred" },
unowned,
{ _tag: "Skipped" },
unownedPi,
{ _tag: "Deferred" },
]);

expect(yield* importer.run).toEqual({ importedCount: 3, skippedCount: 0 });
expect(yield* importer.run).toEqual({ importedCount: 3, skippedCount: 3, deferredCount: 2 });
for (const source of [active, archived, deleted, ownedPi]) {
const threadId = importId(source);
expect(yield* Effect.option(projections.getThread(threadId))).toEqual(Option.none());
Expand Down Expand Up @@ -367,6 +375,12 @@ it.effect(
runtimePayload: { importedTranscripts: [source.source] },
});
}

yield* importer.run;
expect(importer.ownedSourceCalls).toEqual([
[],
[active.source, archived.source, deleted.source, ownedPi.source],
]);
}).pipe(Effect.scoped, Effect.provide(persistence)),
);

Expand All @@ -386,8 +400,12 @@ it.effect("rechecks ownership after the import reaches the commit transaction",
// Commit ownership after all pre-transaction reads, before the import acquires SQLite.
const ownerId = yield* seedOwner(source);
yield* Deferred.succeed(continueTransaction, undefined);
expect(yield* Fiber.join(pending)).toEqual({ importedCount: 0, skippedCount: 0 });
expect(yield* importer.run).toEqual({ importedCount: 0, skippedCount: 0 });
expect(yield* Fiber.join(pending)).toEqual({
importedCount: 0,
skippedCount: 0,
deferredCount: 0,
});
expect(yield* importer.run).toEqual({ importedCount: 0, skippedCount: 0, deferredCount: 0 });
const projections = yield* ProjectionStore.ProjectionStoreV2;
const events = yield* EventStore.EventStoreV2;
const runtimes = yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository;
Expand All @@ -414,23 +432,23 @@ it.effect(
const threadId = importId(source);
const importer = yield* makeImporter([source]);
yield* sql`CREATE TEMP TRIGGER fail_import BEFORE INSERT ON orchestration_events WHEN NEW.event_type = 'message.updated' BEGIN SELECT RAISE(ABORT, 'test publication failure'); END`;
expect(yield* importer.run).toEqual({ importedCount: 0, skippedCount: 1 });
expect(yield* importer.run).toEqual({ importedCount: 0, skippedCount: 1, deferredCount: 0 });
expect(yield* events.read({ threadId }).pipe(Stream.runCollect)).toEqual([]);
expect(yield* Effect.option(projections.getThread(threadId))).toEqual(Option.none());
expect(yield* runtimes.list()).toEqual([]);
expect(importer.published).toEqual([]);
yield* sql`DROP TRIGGER fail_import`;

yield* sql`CREATE TEMP TRIGGER fail_runtime BEFORE INSERT ON provider_session_runtime BEGIN SELECT RAISE(ABORT, 'test runtime failure'); END`;
expect(yield* importer.run).toEqual({ importedCount: 0, skippedCount: 1 });
expect(yield* importer.run).toEqual({ importedCount: 0, skippedCount: 1, deferredCount: 0 });
const committed = yield* events.read({ threadId }).pipe(Stream.runCollect);
expect(committed).toHaveLength(6);
expect(importer.published).toHaveLength(6);
expect(yield* runtimes.list()).toEqual([]);
yield* sql`DROP TRIGGER fail_runtime`;

expect(yield* importer.run).toEqual({ importedCount: 1, skippedCount: 0 });
expect(yield* importer.run).toEqual({ importedCount: 1, skippedCount: 0 });
expect(yield* importer.run).toEqual({ importedCount: 1, skippedCount: 0, deferredCount: 0 });
expect(yield* importer.run).toEqual({ importedCount: 1, skippedCount: 0, deferredCount: 0 });
expect(yield* events.read({ threadId }).pipe(Stream.runCollect)).toEqual(committed);
expect(importer.published).toHaveLength(6);
expect(
Expand Down Expand Up @@ -781,7 +799,7 @@ it.effect("imports under a policy ceiling in the project's default, kept under t
},
),
);
expect(yield* importer.run).toEqual({ importedCount: 1, skippedCount: 0 });
expect(yield* importer.run).toEqual({ importedCount: 1, skippedCount: 0, deferredCount: 0 });
expect((yield* projections.getThread(threadId)).runtimeMode).toBe("auto-accept-edits");
expect(Option.getOrThrow(yield* runtimes.getByThreadId({ threadId })).runtimeMode).toBe(
"auto-accept-edits",
Expand Down
19 changes: 16 additions & 3 deletions apps/server/src/project/AgentSessionImporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ const make = Effect.gen(function* () {
const threadCommands = yield* ThreadCommandExecutor.ThreadCommandExecutor;
const settingsService = yield* ServerSettings.ServerSettingsService;
const ceiling = settingsService.managedPolicy?.limits?.maxRuntimeMode;
/** Transcripts whose native session a T3 thread owns, so later attempts skip them for free. */
const ownedTranscripts = new Map<string, AgentSessionImportSource>();
const importRecentAgentThreads = Effect.fn("importRecentAgentThreadsV2")(function* (
input: AgentSessionImportInput,
) {
Expand Down Expand Up @@ -261,17 +263,25 @@ const make = Effect.gen(function* () {
}
return payload.value.importedTranscripts ?? [];
});
const outcomes = scanner.recentThreads(project.workspaceRoot, completedSources);
const outcomes = scanner.recentThreads(project.workspaceRoot, completedSources, [
...ownedTranscripts.values(),
]);
const importedThreadIds = new Set<ThreadId>();
let importedCount = 0;
let skippedCount = 0;
let deferredCount = 0;

yield* Stream.runForEach(outcomes, (outcome) =>
Effect.gen(function* () {
if (outcome._tag === "Skipped") {
skippedCount += 1;
return;
}
if (outcome._tag === "Deferred") {
skippedCount += 1;
deferredCount += 1;
return;
}
const source = outcome.source;
const threadId = ThreadId.make(
`import:${source.providerInstanceId}:${source.providerSessionId}`,
Expand Down Expand Up @@ -457,7 +467,10 @@ const make = Effect.gen(function* () {
},
],
});
if (committed.length === 0) return null;
if (committed.length === 0) {
ownedTranscripts.set(`${source.providerInstanceId}\0${source.filePath}`, source);
return null;
}
yield* persistRuntime;
yield* runtimes.recordImportedTranscript({ threadId, source });
return true;
Expand All @@ -479,7 +492,7 @@ const make = Effect.gen(function* () {
}),
);

return { importedCount, skippedCount } satisfies AgentSessionImportResult;
return { importedCount, skippedCount, deferredCount } satisfies AgentSessionImportResult;
});

/**
Expand Down
Loading
Loading