fix(server): stop replaying old agent alerts on restart (#13340)
This commit is contained in:
1 parent
894d33419d
commit
e4eb9977f0
2 files changed
+207
-7
No files matched your search
@@ -20,6 +20,7 @@ import { CommandId, ProviderInstanceId } from "@t3tools/contracts";
|
||||
import { RelayClientTracer } from "@t3tools/shared/relayTracing";
|
||||
import { RELAY_ACTIVITY_PUBLISH_TYP, verifyRelayJwt } from "@t3tools/shared/relayJwt";
|
||||
import { describe, expect, it } from "@effect/vitest";
|
||||
import * as DateTime from "effect/DateTime";
|
||||
import * as Deferred from "effect/Deferred";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
@@ -325,6 +326,9 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {
|
||||
const projectId = "project-1" as ProjectId;
|
||||
const activeThreadId = "thread-active" as ThreadId;
|
||||
const idleThreadId = "thread-idle" as ThreadId;
|
||||
const oldCompletedId = "thread-old-completed" as ThreadId;
|
||||
const newCompletedId = "thread-new-completed" as ThreadId;
|
||||
const freshMessageId = "thread-fresh-message" as ThreadId;
|
||||
|
||||
const baseThread = {
|
||||
projectId,
|
||||
@@ -351,6 +355,7 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {
|
||||
expect(
|
||||
AgentAwarenessRelay.resolveAgentAwarenessRelayActiveThreadIds({
|
||||
environmentId,
|
||||
startedAt: Date.parse(now),
|
||||
projects: [
|
||||
{
|
||||
id: projectId,
|
||||
@@ -374,6 +379,44 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {
|
||||
...baseThread,
|
||||
id: idleThreadId,
|
||||
},
|
||||
{
|
||||
...baseThread,
|
||||
id: oldCompletedId,
|
||||
latestTurn: {
|
||||
turnId: "turn-old" as TurnId,
|
||||
state: "completed",
|
||||
requestedAt: "2026-05-24T00:00:00.000Z",
|
||||
startedAt: "2026-05-24T00:00:00.000Z",
|
||||
completedAt: "2026-05-24T00:01:00.000Z",
|
||||
assistantMessageId: null,
|
||||
},
|
||||
},
|
||||
{
|
||||
...baseThread,
|
||||
id: newCompletedId,
|
||||
latestTurn: {
|
||||
turnId: "turn-new" as TurnId,
|
||||
state: "completed",
|
||||
requestedAt: "2026-05-25T00:00:01.000Z",
|
||||
startedAt: "2026-05-25T00:00:01.000Z",
|
||||
completedAt: "2026-05-25T00:00:02.000Z",
|
||||
assistantMessageId: null,
|
||||
},
|
||||
},
|
||||
{
|
||||
...baseThread,
|
||||
id: freshMessageId,
|
||||
latestUserMessageAt: "2026-05-25T00:00:01.000Z",
|
||||
session: {
|
||||
threadId: freshMessageId,
|
||||
status: "ready",
|
||||
providerName: "Codex",
|
||||
runtimeMode: "full-access",
|
||||
activeTurnId: null,
|
||||
lastError: null,
|
||||
updatedAt: "2026-05-25T00:00:02.000Z",
|
||||
},
|
||||
},
|
||||
{
|
||||
...baseThread,
|
||||
id: "thread-missing-project" as ThreadId,
|
||||
@@ -389,7 +432,7 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {
|
||||
},
|
||||
],
|
||||
}),
|
||||
).toEqual([activeThreadId]);
|
||||
).toEqual([activeThreadId, newCompletedId]);
|
||||
});
|
||||
|
||||
it.effect("signs the activity publish JWT and rejects tampering", () =>
|
||||
@@ -797,4 +840,138 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
it.effect("does not alert for historical completions after startup", () =>
|
||||
Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
const secrets = makeMemorySecretStore();
|
||||
const now = yield* DateTime.now;
|
||||
const old = DateTime.formatIso(DateTime.add(now, { days: -7 }));
|
||||
const threadId = "thread-old" as ThreadId;
|
||||
const projectId = "project-1" as ProjectId;
|
||||
const environmentId = "env-1" as EnvironmentId;
|
||||
const project = {
|
||||
id: projectId,
|
||||
title: "T3 Code",
|
||||
workspaceRoot: "/workspace",
|
||||
repositoryIdentity: null,
|
||||
defaultModelSelection: null,
|
||||
scripts: [],
|
||||
createdAt: old,
|
||||
updatedAt: old,
|
||||
} satisfies OrchestrationProjectShell;
|
||||
const completedTurn = {
|
||||
turnId: "turn-1" as TurnId,
|
||||
state: "completed",
|
||||
requestedAt: old,
|
||||
startedAt: old,
|
||||
completedAt: old,
|
||||
assistantMessageId: null,
|
||||
} as const;
|
||||
const completedThread = {
|
||||
id: threadId,
|
||||
projectId,
|
||||
title: "Old task",
|
||||
modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
|
||||
runtimeMode: "full-access",
|
||||
interactionMode: "default",
|
||||
branch: null,
|
||||
worktreePath: null,
|
||||
pullRequests: [],
|
||||
latestTurn: completedTurn,
|
||||
createdAt: old,
|
||||
updatedAt: old,
|
||||
archivedAt: null,
|
||||
settledOverride: null,
|
||||
settledAt: null,
|
||||
session: null,
|
||||
latestUserMessageAt: old,
|
||||
hasPendingApprovals: false,
|
||||
hasPendingUserInput: false,
|
||||
hasActionableProposedPlan: false,
|
||||
} satisfies OrchestrationThreadShell;
|
||||
let currentThread: OrchestrationThreadShell | null = completedThread;
|
||||
let publishes = 0;
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = (() => {
|
||||
publishes += 1;
|
||||
return Promise.resolve(Response.json({ ok: true, deliveries: [] }));
|
||||
}) as unknown as typeof fetch;
|
||||
yield* Effect.addFinalizer(() =>
|
||||
Effect.sync(() => {
|
||||
globalThis.fetch = originalFetch;
|
||||
}),
|
||||
);
|
||||
yield* secrets.setString(RELAY_URL_SECRET, "https://relay.example.test");
|
||||
yield* secrets.setString(RELAY_ENVIRONMENT_CREDENTIAL_SECRET, "relay-credential");
|
||||
yield* secrets.setString(PUBLISH_AGENT_ACTIVITY_SECRET, "true");
|
||||
|
||||
const layer = Layer.mergeAll(
|
||||
Layer.succeed(ServerSecretStore.ServerSecretStore, secrets.store),
|
||||
Layer.succeed(ServerEnvironment.ServerEnvironment, {
|
||||
getEnvironmentId: Effect.succeed(environmentId),
|
||||
getDescriptor: Effect.die("unused descriptor"),
|
||||
}),
|
||||
Layer.succeed(OrchestrationEngineService, {} as OrchestrationEngineShape),
|
||||
Layer.succeed(ProjectionSnapshotQuery, {
|
||||
getThreadShellById: () => Effect.sync(() => Option.fromNullishOr(currentThread)),
|
||||
getProjectShellById: () => Effect.succeed(Option.some(project)),
|
||||
} as unknown as ProjectionSnapshotQueryShape),
|
||||
);
|
||||
|
||||
yield* Effect.gen(function* () {
|
||||
const relay = yield* AgentAwarenessRelay.AgentAwarenessRelay;
|
||||
yield* relay.publishThread(threadId);
|
||||
expect(publishes).toBe(0);
|
||||
|
||||
currentThread = {
|
||||
...completedThread,
|
||||
latestTurn: null,
|
||||
latestUserMessageAt: DateTime.formatIso(DateTime.add(now, { seconds: 1 })),
|
||||
session: {
|
||||
threadId,
|
||||
status: "ready",
|
||||
providerName: "Codex",
|
||||
runtimeMode: "full-access",
|
||||
activeTurnId: null,
|
||||
lastError: null,
|
||||
updatedAt: DateTime.formatIso(DateTime.add(now, { seconds: 2 })),
|
||||
},
|
||||
};
|
||||
expect(
|
||||
AgentAwarenessRelay.resolveAgentAwarenessRelayPublishSnapshot({
|
||||
environmentId,
|
||||
threadId,
|
||||
thread: Option.some(currentThread),
|
||||
project: Option.some(project),
|
||||
}).state?.phase,
|
||||
).toBe("completed");
|
||||
yield* relay.publishThread(threadId);
|
||||
expect(publishes).toBe(0);
|
||||
|
||||
currentThread = {
|
||||
...completedThread,
|
||||
session: {
|
||||
threadId,
|
||||
status: "error",
|
||||
providerName: "Codex",
|
||||
runtimeMode: "full-access",
|
||||
activeTurnId: null,
|
||||
lastError: "old failure",
|
||||
updatedAt: old,
|
||||
},
|
||||
};
|
||||
yield* relay.publishThread(threadId);
|
||||
expect(publishes).toBe(0);
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
AgentAwarenessRelay.layer.pipe(
|
||||
Layer.provide(layer),
|
||||
Layer.provideMerge(NodeServices.layer),
|
||||
),
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
@@ -267,8 +267,13 @@ export function resolveAgentAwarenessRelayPublishSnapshot(input: {
|
||||
};
|
||||
}
|
||||
|
||||
function terminalWorkSinceStart(thread: OrchestrationThreadShell, startedAt: number): boolean {
|
||||
return Date.parse(thread.latestTurn?.completedAt ?? "") > startedAt;
|
||||
}
|
||||
|
||||
export function resolveAgentAwarenessRelayActiveThreadIds(input: {
|
||||
readonly environmentId: EnvironmentId;
|
||||
readonly startedAt: number;
|
||||
readonly projects: ReadonlyArray<Pick<OrchestrationProjectShell, "id" | "title">>;
|
||||
readonly threads: ReadonlyArray<OrchestrationThreadShell>;
|
||||
}): ReadonlyArray<ThreadId> {
|
||||
@@ -279,12 +284,16 @@ export function resolveAgentAwarenessRelayActiveThreadIds(input: {
|
||||
if (!project) {
|
||||
return false;
|
||||
}
|
||||
const state = projectThreadAwareness({
|
||||
environmentId: input.environmentId,
|
||||
project,
|
||||
thread,
|
||||
});
|
||||
return (
|
||||
projectThreadAwareness({
|
||||
environmentId: input.environmentId,
|
||||
project,
|
||||
thread,
|
||||
}) !== null
|
||||
state !== null &&
|
||||
(state.phase !== "completed" && state.phase !== "failed"
|
||||
? true
|
||||
: terminalWorkSinceStart(thread, input.startedAt))
|
||||
);
|
||||
})
|
||||
.map((thread) => thread.id);
|
||||
@@ -298,6 +307,7 @@ export const make = Effect.gen(function* () {
|
||||
const orchestrationEngine = yield* OrchestrationEngine.OrchestrationEngineService;
|
||||
const crypto = yield* Crypto.Crypto;
|
||||
const cloudLinkKeyPair = yield* getOrCreateEnvironmentKeyPairFromSecretStore(secrets);
|
||||
const startedAt = (yield* DateTime.now).epochMilliseconds;
|
||||
const activeSnapshotPublishedRef = yield* Ref.make(false);
|
||||
const publishedStateByThreadRef = yield* Ref.make(new Map<ThreadId, string>());
|
||||
|
||||
@@ -417,6 +427,14 @@ export const make = Effect.gen(function* () {
|
||||
});
|
||||
const publishIdentity = agentAwarenessPublishIdentity(snapshot.state);
|
||||
const publishedStateByThread = yield* Ref.get(publishedStateByThreadRef);
|
||||
if (
|
||||
(snapshot.state?.phase === "completed" || snapshot.state?.phase === "failed") &&
|
||||
!publishedStateByThread.has(threadId)
|
||||
) {
|
||||
// Startup has no publish history. Only work from this server process may
|
||||
// produce an initial terminal alert; historical threads remain quiet.
|
||||
if (Option.isNone(thread) || !terminalWorkSinceStart(thread.value, startedAt)) return;
|
||||
}
|
||||
if (publishedStateByThread.get(threadId) === publishIdentity) {
|
||||
// The projection is back at (or never left) the last published state, so
|
||||
// any pending deferred confirmation is moot. Leaving the deadline in
|
||||
@@ -495,7 +513,11 @@ export const make = Effect.gen(function* () {
|
||||
});
|
||||
yield* Ref.update(publishedStateByThreadRef, (publishedStates) => {
|
||||
const nextPublishedStates = new Map(publishedStates);
|
||||
nextPublishedStates.set(threadId, publishIdentity);
|
||||
if (snapshot.state === null) {
|
||||
nextPublishedStates.delete(threadId);
|
||||
} else {
|
||||
nextPublishedStates.set(threadId, publishIdentity);
|
||||
}
|
||||
return nextPublishedStates;
|
||||
});
|
||||
});
|
||||
@@ -529,6 +551,7 @@ export const make = Effect.gen(function* () {
|
||||
const snapshot = yield* snapshotQuery.getShellSnapshot();
|
||||
const activeThreadIds = resolveAgentAwarenessRelayActiveThreadIds({
|
||||
environmentId,
|
||||
startedAt,
|
||||
projects: snapshot.projects,
|
||||
threads: snapshot.threads,
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user