Skip to content

Commit 0f086fe

Browse files
committed
Merge remote-tracking branch 'upstream/main' into feat/latex-equations
2 parents 285bc12 + cd41c4a commit 0f086fe

14 files changed

Lines changed: 759 additions & 41 deletions

‎apps/server/src/assets/AssetAccess.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { AssetResource } from "@t3tools/contracts";
22
import {
33
AssetAttachmentNotFoundError,
4-
PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
54
AssetGitHubMediaUrlValidationError,
65
AssetPreviewTypeValidationError,
76
AssetProjectFaviconInspectionError,
@@ -33,7 +32,7 @@ import {
3332
} from "@t3tools/shared/imageDimensions";
3433
import { githubMediaFetchUrl, githubMediaFileName } from "@t3tools/shared/githubMedia";
3534
import { PROJECT_FAVICON_FALLBACK_MARKER } from "@t3tools/shared/projectFavicon";
36-
import { toolOutputImages } from "@t3tools/shared/toolOutput";
35+
import { MAX_TOOL_OUTPUT_IMAGE_BASE64_LENGTH, toolOutputImages } from "@t3tools/shared/toolOutput";
3736
import * as Clock from "effect/Clock";
3837
import * as Crypto from "effect/Crypto";
3938
import * as Effect from "effect/Effect";
@@ -216,9 +215,6 @@ const optionOnNotFound = <A, R>(
216215
}),
217216
);
218217

219-
// The largest image a provider turn accepts, as base64 (4 characters per 3 bytes).
220-
const MAX_TOOL_OUTPUT_IMAGE_BASE64_LENGTH = Math.ceil(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES / 3) * 4;
221-
222218
/**
223219
* Decodes one image a tool returned inline; null when the stored item has no
224220
* such image, or it is larger than a provider turn accepts.

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

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import * as Layer from "effect/Layer";
2727
import * as Stream from "effect/Stream";
2828
import * as TestClock from "effect/testing/TestClock";
2929

30+
import { toolOutputImages } from "@t3tools/shared/toolOutput";
3031
import * as SqlitePersistence from "../persistence/Sqlite.ts";
3132
import * as EventSink from "./EventSink.ts";
3233
import * as EventStore from "./EventStore.ts";
@@ -1169,6 +1170,97 @@ layer("ProviderEventIngestorV2", (it) => {
11691170
}),
11701171
);
11711172

1173+
it.effect("stores tool image bytes only where a tool-output-image asset serves them", () =>
1174+
Effect.gen(function* () {
1175+
const now = yield* DateTime.now;
1176+
const eventSink = yield* EventSink.EventSinkV2;
1177+
const eventStore = yield* EventStore.EventStoreV2;
1178+
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
1179+
const ingestor = yield* ProviderEventIngestor.ProviderEventIngestorV2;
1180+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
1181+
const threadEvent = yield* threadCreatedEvent(now);
1182+
const providerSessionId = yield* idAllocator.allocate.providerSession({
1183+
providerInstanceId: modelSelection.instanceId,
1184+
threadId: threadEvent.threadId,
1185+
});
1186+
const readBase64 = Buffer.alloc(30_000, 7).toString("base64");
1187+
const screenshotBase64 = Buffer.alloc(20_000, 9).toString("base64");
1188+
const toolItem = (
1189+
id: string,
1190+
ordinal: number,
1191+
toolName: string,
1192+
output: unknown,
1193+
): OrchestrationV2TurnItem => ({
1194+
id: TurnItemId.make(id),
1195+
threadId: threadEvent.threadId,
1196+
runId: null,
1197+
nodeId: null,
1198+
providerThreadId: null,
1199+
providerTurnId: null,
1200+
nativeItemRef: null,
1201+
parentItemId: null,
1202+
ordinal,
1203+
status: "completed",
1204+
title: toolName,
1205+
startedAt: now,
1206+
completedAt: now,
1207+
updatedAt: now,
1208+
type: "dynamic_tool",
1209+
toolName,
1210+
input: {},
1211+
output,
1212+
});
1213+
const read = toolItem("turn-item:read-image", 1, "Read", {
1214+
type: "image",
1215+
file: { base64: readBase64, type: "image/png", originalSize: 30_000 },
1216+
});
1217+
const screenshot = toolItem("turn-item:screenshot", 2, "mcp__t3-code__device_screenshot", {
1218+
content: [
1219+
{
1220+
type: "image",
1221+
source: { type: "base64", media_type: "image/png", data: screenshotBase64 },
1222+
},
1223+
],
1224+
});
1225+
1226+
yield* eventSink.write({ events: [threadEvent] });
1227+
for (const turnItem of [read, screenshot]) {
1228+
yield* ingestor.ingestNormalized({
1229+
providerSessionId,
1230+
providerInstanceId: modelSelection.instanceId,
1231+
threadId: threadEvent.threadId,
1232+
event: { type: "turn_item.updated", driver: CODEX_DRIVER, turnItem },
1233+
});
1234+
}
1235+
1236+
const storedEvents = yield* eventStore
1237+
.read({ threadId: threadEvent.threadId, eventType: "turn-item.updated" })
1238+
.pipe(Stream.runCollect);
1239+
const storedJson = JSON.stringify(Array.from(storedEvents, (stored) => stored.event));
1240+
const projectedRead = yield* projectionStore.getTurnItem({
1241+
threadId: threadEvent.threadId,
1242+
itemId: read.id,
1243+
});
1244+
const projectedScreenshot = yield* projectionStore.getTurnItem({
1245+
threadId: threadEvent.threadId,
1246+
itemId: screenshot.id,
1247+
});
1248+
1249+
assert.equal(storedJson.includes(readBase64), false);
1250+
assert.equal(storedJson.includes(screenshotBase64), true);
1251+
assert.deepEqual(projectedRead?.type === "dynamic_tool" ? projectedRead.output : null, {
1252+
type: "image",
1253+
file: { type: "image/png", originalSize: 30_000, sizeBytes: 30_000 },
1254+
});
1255+
assert.deepEqual(
1256+
toolOutputImages(
1257+
projectedScreenshot?.type === "dynamic_tool" ? projectedScreenshot.output : null,
1258+
),
1259+
[{ mimeType: "image/png", data: screenshotBase64 }],
1260+
);
1261+
}),
1262+
);
1263+
11721264
it.effect("routes provider-owned child artifacts to their child app thread", () =>
11731265
Effect.gen(function* () {
11741266
const now = yield* DateTime.now;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import * as IdAllocator from "./IdAllocator.ts";
3333
import { ProviderAdapterV2Event } from "./ProviderAdapter.ts";
3434
import { makeProviderFailureTurnItem } from "./ProviderFailure.ts";
3535
import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts";
36+
import { stripUnservedToolOutputImageBytes } from "./toolOutputImageBytes.ts";
3637

3738
export class ProviderEventNormalizeError extends Schema.TaggedError<ProviderEventNormalizeError>()(
3839
"ProviderEventNormalizeError",
@@ -465,7 +466,7 @@ export const layer: Layer.Layer<
465466
yield* makeDomainEvent(input, {
466467
type: "turn-item.updated",
467468
threadId: input.event.turnItem.threadId,
468-
payload: input.event.turnItem,
469+
payload: stripUnservedToolOutputImageBytes(input.event.turnItem),
469470
runId: input.event.turnItem.runId,
470471
nodeId: input.event.turnItem.nodeId,
471472
}),

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
type PullRequestRef,
1616
type PullRequestStack,
1717
type PullRequestSummary,
18+
type ThreadPullRequestKey,
1819
type ThreadPullRequestLink,
1920
type ThreadPullRequestSnapshot,
2021
} from "@t3tools/contracts";
@@ -181,6 +182,7 @@ const makeHarness = Effect.fn("makePullRequestSyncHarness")(function* (options:
181182
const summaryCalls = yield* Ref.make<ReadonlyArray<PullRequestRef>>([]);
182183
const stackCalls = yield* Ref.make<ReadonlyArray<PullRequestRef>>([]);
183184
const domainEvents = yield* Queue.unbounded<OrchestrationV2DomainEvent>();
185+
const stateChanges = yield* Queue.unbounded<ThreadPullRequestKey>();
184186

185187
const summary: PullRequestService.PullRequestService["Service"]["summary"] = (
186188
input,
@@ -218,6 +220,7 @@ const makeHarness = Effect.fn("makePullRequestSyncHarness")(function* (options:
218220
summary,
219221
stack,
220222
invalidate: options.invalidate ?? (() => Effect.void),
223+
subscribeStateChanges: Effect.succeed(Stream.fromQueue(stateChanges)),
221224
}),
222225
Layer.mock(ProjectionStore.ProjectionStoreV2)({
223226
// Mirrors the store's filter: active threads that have at least one link.
@@ -269,6 +272,7 @@ const makeHarness = Effect.fn("makePullRequestSyncHarness")(function* (options:
269272
summaryCalls,
270273
stackCalls,
271274
domainEvents,
275+
stateChanges,
272276
layer: PullRequestSyncReactor.layer.pipe(Layer.provide(layerDependencies)),
273277
};
274278
});
@@ -654,6 +658,49 @@ describe("PullRequestSyncReactor", () => {
654658
),
655659
);
656660

661+
it.effect("syncs a pull request a reader saw merge without waiting for the sweep", () =>
662+
Effect.scoped(
663+
Effect.gen(function* () {
664+
yield* TestClock.setTime(Date.parse(NOW));
665+
const state = yield* Ref.make<PullRequestSummary["state"]>("open");
666+
const invalidated = yield* Ref.make<ReadonlyArray<number>>([]);
667+
const fixture = yield* makeHarness({
668+
snapshot: makeSnapshot([
669+
makeThread("agent", { pullRequests: [makeLink(7, { state: "open" })] }),
670+
]),
671+
summary: (input) =>
672+
Ref.get(state).pipe(
673+
Effect.map((current) =>
674+
makeSummary(input, current === "merged" ? { state: current, mergedAt: NOW } : {}),
675+
),
676+
),
677+
invalidate: ({ reference }) =>
678+
Ref.update(invalidated, (numbers) => [...numbers, reference?.number ?? -1]),
679+
});
680+
681+
yield* Effect.gen(function* () {
682+
const reactor = yield* startAndSweep(fixture);
683+
yield* Ref.set(state, "merged");
684+
685+
// The clock stays put: the next sweep is still a minute away.
686+
yield* Queue.offer(fixture.stateChanges, {
687+
host: "github.com",
688+
repository: "owner/repository",
689+
number: 7,
690+
});
691+
yield* Queue.take(fixture.snapshotReads);
692+
yield* reactor.drain;
693+
694+
assert.deepStrictEqual(yield* Ref.get(invalidated), [7]);
695+
assert.deepStrictEqual(
696+
(yield* Ref.get(fixture.syncCommands)).map((command) => command.snapshot.state),
697+
["merged"],
698+
);
699+
}).pipe(Effect.provide(fixture.layer));
700+
}),
701+
),
702+
);
703+
657704
it.effect("discovers externally reopened pull requests after fifteen minutes", () =>
658705
Effect.scoped(
659706
Effect.gen(function* () {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,13 @@ export const make = Effect.gen(function* () {
424424
"PullRequestSyncReactor.start",
425425
)(function* () {
426426
const events = engine.streamDomainEvents;
427+
// A client reading a pull request can see it merge or close before the next sweep does.
428+
const stateChanges = yield* pullRequests.subscribeStateChanges;
429+
yield* forkParked(
430+
Stream.runForEach(stateChanges, requestSync).pipe(
431+
Effect.catchCause(logSkipped("pull request state change stream failed", {})),
432+
),
433+
);
427434
yield* forkParked(
428435
Stream.runForEach(events, (event) => {
429436
switch (event.type) {

0 commit comments

Comments
 (0)