Skip to content
Merged
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
4 changes: 2 additions & 2 deletions apps/server/src/j5/a2a/ArchiveAgentService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ const makeHarness = (options: HarnessOptions = {}) => {
},
}),
Layer.mock(ThreadManagementService)({
getThreadProjection: () => Effect.succeed(projection()),
getThreadRecords: () => Effect.succeed(projection()),
dispatch: (command) => {
order.push(command.type === "thread.stop" ? "interrupt_requested" : command.type);
return Effect.succeed({ sequence: 1, storedEvents: [] });
Expand Down Expand Up @@ -477,7 +477,7 @@ const readFactsFor = (home: "none" | "registered", thread: "missing" | "unreadab
),
}),
Layer.mock(ThreadManagementService)({
getThreadProjection: () =>
getThreadRecords: () =>
thread === "present"
? Effect.succeed({
thread: { id: threadId, projectId, archivedAt: null },
Expand Down
18 changes: 7 additions & 11 deletions apps/server/src/j5/a2a/ArchiveAgentService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
CommandId,
OrchestrationV2RunStatus,
RunId,
ThreadId,
type OrchestrationV2ThreadProjection,
} from "@t3tools/contracts";
import { CommandId, OrchestrationV2RunStatus, RunId, ThreadId } from "@t3tools/contracts";
import * as Context from "effect/Context";
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
Expand All @@ -20,6 +14,7 @@ import {
signPayload,
timingSafeEqualBase64Url,
} from "../../auth/utils.ts";
import type { ProjectionRecords } from "../../orchestration-v2/ProjectionStore.ts";
import { ThreadLifecycleService } from "../../orchestration-v2/ThreadLifecycleService.ts";
import {
ThreadManagementService,
Expand All @@ -33,7 +28,7 @@ import {
import { A2ALedger } from "./LedgerService.ts";
import { A2ALifecycleService } from "./LifecycleService.ts";
import { stopThread } from "./stopThread.ts";
import { getThreadProjectionIfPresent } from "./threadProjectionReads.ts";
import { getThreadRecordsIfPresent } from "./threadProjectionReads.ts";
import {
ExchangeDroppedPayload,
ExchangeId,
Expand Down Expand Up @@ -217,7 +212,7 @@ const decodeExchangeDroppedOption = Schema.decodeUnknownOption(ExchangeDroppedPa
const decodeMessageSentOption = Schema.decodeUnknownOption(MessageSentPayload);

interface ReadState {
readonly projection: OrchestrationV2ThreadProjection;
readonly projection: ProjectionRecords<"runs">;
readonly retired: boolean;
readonly archived: boolean;
readonly facts: ArchiveAgentConsequenceFacts;
Expand Down Expand Up @@ -367,7 +362,7 @@ export const layer = Layer.effect(
facts: ThreadPreArchiveFacts,
) {
const projection = yield* threadManagement
.getThreadProjection(target.threadId)
.getThreadRecords(target.threadId, ["runs"])
.pipe(Effect.mapError(operationError("reading the target thread projection")));
if (
facts.state !== "registered" ||
Expand Down Expand Up @@ -651,9 +646,10 @@ export const layer = Layer.effect(
)(function* (target) {
const facts = yield* readArchiveFacts(target);
if (facts.state === "not-an-a2a-participant") {
const projection = yield* getThreadProjectionIfPresent(
const projection = yield* getThreadRecordsIfPresent(
threadManagement,
target.threadId,
[],
).pipe(Effect.mapError(operationError("reading the target thread projection")));
if (projection === null) return null;
}
Expand Down
6 changes: 3 additions & 3 deletions apps/server/src/j5/a2a/CrewCaptainArchiveCascade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ it.effect(
),
}),
Layer.mock(ThreadManagementService)({
getThreadProjection: (threadId) =>
getThreadRecords: (threadId) =>
threadId === captainThread
? Effect.succeed(captainProjection("2026-09-17T08:00:00.000Z"))
: threadId === liveCaptain
Expand Down Expand Up @@ -399,7 +399,7 @@ it.effect("unarchiving a Captain brings back the Crews that retired with it", ()
Layer.mock(ArchiveCrewService)({}),
Layer.mock(ThreadManagementService)({
// The builder's thread was archived with the Crew; the critic's was never created.
getThreadProjection: (threadId) =>
getThreadRecords: (threadId) =>
threadId === criticThread
? Effect.fail(
new OrchestratorProjectionError({
Expand Down Expand Up @@ -474,7 +474,7 @@ it.effect("a Captain archived again after an unarchive retires its Crew again",
}),
}),
Layer.mock(ThreadManagementService)({
getThreadProjection: (threadId) =>
getThreadRecords: (threadId) =>
Effect.map(Ref.get(seatArchived), (archived) =>
seatProjection(threadId, {
archivedAt: archived ? "2026-09-17T08:00:00.000Z" : null,
Expand Down
9 changes: 6 additions & 3 deletions apps/server/src/j5/a2a/CrewCaptainArchiveCascade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import { AgentCrewInstanceService, type AgentCrewInstance } from "./AgentCrewIns
import { ArchiveCrewService } from "./ArchiveCrewService.ts";
import { participantIdForThread } from "./HomeRegistrar.ts";
import { crewSeatRequestKey, lifecycleCommandId } from "./spawnIds.ts";
import { getThreadProjectionIfPresent } from "./threadProjectionReads.ts";
import {
getThreadProjectionIfPresent,
getThreadRecordsIfPresent,
} from "./threadProjectionReads.ts";

const CASCADE_SESSION = "j5-captain-archive-cascade";

Expand Down Expand Up @@ -194,7 +197,7 @@ export const layer = Layer.effect(
instance.id,
Effect.gen(function* () {
for (const member of instance.members) {
const seat = yield* getThreadProjectionIfPresent(threads, member.threadId);
const seat = yield* getThreadRecordsIfPresent(threads, member.threadId, []);
if (seat === null || seat.thread.archivedAt === null) continue;
yield* threads.dispatch({
type: "thread.unarchive",
Expand Down Expand Up @@ -287,7 +290,7 @@ export const layer = Layer.effect(
const captainGoneAt = Effect.fn("j5.a2a.captainArchiveCascade.captainGoneAt")(function* (
instance: AgentCrewInstance,
) {
const read = yield* Effect.result(threads.getThreadProjection(instance.captainThreadId));
const read = yield* Effect.result(threads.getThreadRecords(instance.captainThreadId, []));
if (Result.isFailure(read)) {
yield* Effect.logWarning("J5 Captain archive sweep could not read a Captain thread", {
captainThreadId: instance.captainThreadId,
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/j5/a2a/CrewProposalService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,7 @@ const playbookFixture = Effect.gen(function* () {
thread: thread(threadId),
messages: [],
} as unknown as OrchestrationV2ThreadProjection),
getThreadRecords: (threadId) => Effect.succeed({ thread: thread(threadId) } as never),
dispatch: (command) =>
Ref.update(commands, (items) => [...items, command]).pipe(
Effect.as({ events: [], effects: [] } as never),
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/j5/a2a/CrewRuntimeRequestService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ const makeThreads = () => {
};
const layer = Layer.mock(ThreadManagementService)({
// Fails the way the real service does: a typed projection error whose cause says why.
getThreadProjection: (threadId) => {
getThreadRecords: (threadId) => {
const projection = state.get(threadId);
if (broken.has(threadId))
return Effect.fail(
Expand Down
6 changes: 4 additions & 2 deletions apps/server/src/j5/a2a/CrewRuntimeRequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
type ThreadManagementProviderInstanceDisabledError,
} from "../../orchestration-v2/ThreadManagementService.ts";
import { AgentCrewInstanceService, type AgentCrewInstance } from "./AgentCrewInstanceService.ts";
import { getThreadProjectionIfPresent } from "./threadProjectionReads.ts";
import { getThreadRecordsIfPresent } from "./threadProjectionReads.ts";
import { makeNativeQuestionInboxRead } from "./nativeQuestionInboxRead.ts";

/** A seat's thread, and the Crew that holds the seat. */
Expand Down Expand Up @@ -128,7 +128,9 @@ export const layer = Layer.effect(
* null. Any other store failure propagates, so an outage is an error, not an empty Inbox.
*/
const liveProjection = (threadId: ThreadId) =>
getThreadProjectionIfPresent(threads, threadId).pipe(
getThreadRecordsIfPresent(threads, threadId, ["runtimeRequests", "turnItems"], {
turnItemTypes: ["approval_request"],
}).pipe(
Effect.map((projection) =>
projection !== null && projection.thread.archivedAt === null ? projection : null,
),
Expand Down
12 changes: 6 additions & 6 deletions apps/server/src/j5/a2a/CrewSeatFinishNotifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ it.effect("tells the Captain how each finished seat ended, and settles nothing",
),
Layer.provideMerge(
Layer.mock(ThreadManagementService)({
getThreadProjection: (threadId) => Effect.succeed(projection(threadId)),
getThreadRecords: (threadId) => Effect.succeed(projection(threadId)),
dispatch: (command) =>
Ref.update(dispatched, (items) => [...items, command]).pipe(
Effect.as({ events: [], effects: [] } as never),
Expand Down Expand Up @@ -490,7 +490,7 @@ it.effect(
),
Layer.provideMerge(
Layer.mock(ThreadManagementService)({
getThreadProjection: (threadId) =>
getThreadRecords: (threadId) =>
threadId === captainThread ? Ref.get(captain) : Effect.succeed(projection(threadId)),
dispatch: (command) =>
Ref.update(dispatched, (items) => [...items, command]).pipe(
Expand Down Expand Up @@ -622,7 +622,7 @@ it.effect(
),
Layer.provideMerge(
Layer.mock(ThreadManagementService)({
getThreadProjection: (threadId) => Effect.succeed(projection(threadId)),
getThreadRecords: (threadId) => Effect.succeed(projection(threadId)),
dispatch: (command) =>
Ref.get(noticesFail).pipe(
Effect.flatMap((fail) =>
Expand Down Expand Up @@ -739,7 +739,7 @@ it.effect(
),
Layer.provideMerge(
Layer.mock(ThreadManagementService)({
getThreadProjection: (threadId) =>
getThreadRecords: (threadId) =>
threadId === captainThread
? Ref.get(captain)
: Effect.succeed(
Expand Down Expand Up @@ -885,7 +885,7 @@ it.effect("the boot sweep tells the Captain about a finished seat nothing report
),
Layer.provideMerge(
Layer.mock(ThreadManagementService)({
getThreadProjection: (threadId) =>
getThreadRecords: (threadId) =>
Effect.succeed(
threadId === scoutThread
? finished(threadId)
Expand Down Expand Up @@ -987,7 +987,7 @@ it.effect(
),
Layer.provideMerge(
Layer.mock(ThreadManagementService)({
getThreadProjection: (threadId) => Effect.succeed(projection(threadId)),
getThreadRecords: (threadId) => Effect.succeed(projection(threadId)),
dispatch: (command) =>
Ref.update(dispatched, (items) => [...items, command]).pipe(
Effect.as({ events: [], effects: [] } as never),
Expand Down
26 changes: 18 additions & 8 deletions apps/server/src/j5/a2a/CrewSeatFinishNotifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const sectionParticipant = (section: string) =>
* (reviewer, builder), so two Crews' reviewers must not replace each other's baseline.
*/
export const latestSeatNotice = (
captain: OrchestrationV2ThreadProjection,
captain: Pick<OrchestrationV2ThreadProjection, "messages">,
participantId: string,
): string | null => {
const newestFirst = captain.messages
Expand All @@ -109,7 +109,9 @@ export const latestSeatNotice = (
};

/** A seat notice still queued behind the Captain's running turn; a new notice folds into it. */
export const queuedSeatDigest = (captain: OrchestrationV2ThreadProjection) => {
export const queuedSeatDigest = (
captain: Pick<OrchestrationV2ThreadProjection, "runs" | "messages">,
) => {
if (ThreadManagement.latestActiveRun(captain) === undefined) return null;
for (const run of captain.runs) {
if (run.status !== "queued") continue;
Expand Down Expand Up @@ -223,7 +225,7 @@ const makeLayer = (daemon: boolean) =>
* fails it, for the next finish or the boot sweep.
*/
const handoffFact = Effect.fn("j5.a2a.crewSeatFinish.handoffFact")(function* (
projection: OrchestrationV2ThreadProjection,
projection: Pick<OrchestrationV2ThreadProjection, "thread">,
kind: string | null,
): Effect.fn.Return<SeatHandoffFact, ArtifactWorkspaceError, never> {
const assignment = projection.thread.agentPersonaAssignment;
Expand Down Expand Up @@ -271,12 +273,17 @@ const makeLayer = (daemon: boolean) =>
const notifyCaptain = Effect.fn("j5.a2a.crewSeatFinish.notifyCaptain")(function* (
instance: AgentCrewInstance,
seatName: string,
projection: OrchestrationV2ThreadProjection,
projection: Pick<OrchestrationV2ThreadProjection, "thread" | "turnItems">,
run: OrchestrationV2Run,
handoff: SeatHandoffFact,
) {
const threadId = projection.thread.id;
const captain = yield* threads.getThreadProjection(instance.captainThreadId);
// Seat notices are system-authored user messages; the digest is a queued run's user message.
const captain = yield* threads.getThreadRecords(
instance.captainThreadId,
["runs", "messages"],
{ messageRoles: ["user"] },
);
const stable = { providerSessionId: FINISH_SESSION, requestKey: `${threadId}:${run.id}` };
const text = seatFinishedNoticeText({
seatName,
Expand Down Expand Up @@ -328,7 +335,10 @@ const makeLayer = (daemon: boolean) =>
if (membership === null) return null;
const instance = yield* crews.read(membership.crewInstanceId);
if (instance === null || instance.archivedAt !== null) return null;
const projection = yield* threads.getThreadProjection(threadId);
const projection = yield* threads.getThreadRecords(threadId, ["runs", "turnItems"], {
turnItemRunId: run.id,
turnItemTypes: ["error"],
});
if (projection.thread.archivedAt !== null) return null;
if (ThreadManagement.latestActiveRun(projection) !== undefined) return null;
if (run.status === "failed")
Expand Down Expand Up @@ -360,7 +370,7 @@ const makeLayer = (daemon: boolean) =>
});

/** Only report the latest run, never an older finish hidden behind a later interruption. */
const outstandingFinish = (projection: OrchestrationV2ThreadProjection) => {
const outstandingFinish = (projection: Pick<OrchestrationV2ThreadProjection, "runs">) => {
if (ThreadManagement.latestActiveRun(projection) !== undefined) return undefined;
const newest = projection.runs.toSorted((a, b) => b.ordinal - a.ordinal)[0];
return newest?.status === "completed" || newest?.status === "failed" ? newest : undefined;
Expand All @@ -372,7 +382,7 @@ const makeLayer = (daemon: boolean) =>
for (const member of instance.members) {
// A seat whose thread cannot be read (gone, or the store hiccuped) is skipped this pass.
const projection = yield* threads
.getThreadProjection(member.threadId)
.getThreadRecords(member.threadId, ["runs"])
.pipe(Effect.catchCause(() => Effect.succeed(null)));
if (projection === null) continue;
if (projection.thread.archivedAt !== null) continue;
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/j5/a2a/CrewStopService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ it.effect("stops every live seat, for the Captain or a person, and nobody else",
const layer = crewStopLayer.pipe(
Layer.provideMerge(
Layer.mock(ThreadManagementService)({
getThreadProjection: (threadId) => Effect.succeed(projection(threadId)),
getThreadRecords: (threadId) => Effect.succeed(projection(threadId)),
dispatch: (command) =>
Ref.update(interrupts, (items) => [
...items,
Expand Down Expand Up @@ -223,7 +223,7 @@ const ghostFixture = (ghost: "missing" | "unreadable" | "homed") =>
const layer = crewStopLayer.pipe(
Layer.provideMerge(
Layer.mock(ThreadManagementService)({
getThreadProjection: (threadId) =>
getThreadRecords: (threadId) =>
threadId !== ghostThread
? Effect.succeed(projection(threadId))
: Effect.fail(
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/j5/a2a/CrewStopService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { AgentCrewInstanceService, type AgentCrewMember } from "./AgentCrewInsta
import { A2AArchiveFacts } from "./ArchiveFactsService.ts";
import type { ParticipantId, LedgerProjectId } from "./contracts.ts";
import { AGENT_STOP_REASON, stopThread } from "./stopThread.ts";
import { getThreadProjectionIfPresent } from "./threadProjectionReads.ts";
import { getThreadRecordsIfPresent } from "./threadProjectionReads.ts";

/** `never_created`: the seat's row was recorded but its thread never came to exist. */
export type CrewSeatStopResult =
Expand Down Expand Up @@ -99,7 +99,7 @@ export const layer = Layer.effect(
* with no thread behind it, fails the stop rather than skipping a seat that may be live.
*/
const readSeat = Effect.fn("j5.a2a.crewStop.readSeat")(function* (member: AgentCrewMember) {
const projection = yield* getThreadProjectionIfPresent(threads, member.threadId).pipe(
const projection = yield* getThreadRecordsIfPresent(threads, member.threadId, ["runs"]).pipe(
Effect.mapError(seatError("reading a seat", member.seatName)),
);
if (projection !== null) return projection;
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/j5/a2a/DeliveryTransport.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,11 @@ const makeTestLayer = (
const threads = yield* ThreadManagementService;
return ThreadManagementService.of({
...threads,
getThreadProjection: (threadId) =>
getThreadRecords: (threadId, fields, filter) =>
Ref.get(harness.staleProjection).pipe(
Effect.filterOrElse(
(stale) => stale !== undefined,
() => threads.getThreadProjection(threadId),
() => threads.getThreadRecords(threadId, fields, filter),
),
),
sendToThread: (input) =>
Expand Down
Loading
Loading