Skip to content

Commit ae9fee9

Browse files
committed
feat(server): let background jobs complete signal monitors
Codex shell jobs do not inherit their thread's MCP credentials, so they cannot report completion after a turn ends through the ACP CLI helper. Return a completion-only HTTP callback and shell command when starting a signal monitor. Bind its credential to one monitor with a persistent signing key and retain the existing cancellation and delivery barriers. Keep callback credentials out of status and client summaries. Validate bounded observations, reject archived or deleted owners, and preserve one continuation when a worker retries. Update the background-job and Codex credential guidance. Verify ended-turn and restart delivery, duplicate signals, cancellation, owner lifecycle, request validation, and existing MCP transports. Model: GPT-6 Astra (xhigh) Harness: Codex
1 parent e4f63ca commit ae9fee9

16 files changed

Lines changed: 525 additions & 19 deletions

File tree

‎apps/server/src/auth/replayMarkers.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import * as ServerSecretStore from "./ServerSecretStore.ts";
1717
const REAL_SECRET_NAMES = [
1818
"server-signing-key",
1919
"asset-access-signing-key",
20+
"monitor-signal-signing-key",
2021
"cloud-cli-oauth-token",
2122
"cloud-cli-desired-link",
2223
"cloud-link-ed25519-key-pair",

‎apps/server/src/mcp/McpHttpServer.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as ThreadMonitorSignalCallbacks from "../threadMonitor/ThreadMonitorSignalCallbacks.ts";
12
import { ThreadWorkspaceQuery } from "../orchestration-v2/ThreadWorkspaceQuery.ts";
23
import * as Orchestrator from "../orchestration-v2/Orchestrator.ts";
34
import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
@@ -311,6 +312,7 @@ const layerTest = Layer.mergeAll(
311312
Layer.provide(Layer.mock(DeviceService.DeviceService)({})),
312313
Layer.provide(Layer.mock(ThreadWorkspaceQuery)({})),
313314
Layer.provide(MonitorTestLayer),
315+
Layer.provide(Layer.mock(ThreadMonitorSignalCallbacks.ThreadMonitorSignalCallbacks)({})),
314316
Layer.provide(AgentDesktopTransferTestLayer),
315317
Layer.provide(AgentDesktopManagerTestLayer),
316318
Layer.provide(ComputerAutomationRouterTestLayer),

‎apps/server/src/mcp/McpHttpServer.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as ThreadMonitorSignalCallbacks from "../threadMonitor/ThreadMonitorSignalCallbacks.ts";
12
import { UserDesktopTransfers } from "../computer/UserDesktopTransfers.ts";
23
import { ThreadWorkspaceQuery } from "../orchestration-v2/ThreadWorkspaceQuery.ts";
34
import * as Cause from "effect/Cause";
@@ -1507,6 +1508,7 @@ const registerMonitorStandardTools = Effect.fn("McpHttpServer.registerMonitorSta
15071508
const server = yield* McpServer.McpServer;
15081509
const threads = yield* ThreadManagementService.ThreadManagementService;
15091510
const service = yield* ThreadMonitorService;
1511+
const callbacks = yield* ThreadMonitorSignalCallbacks.ThreadMonitorSignalCallbacks;
15101512
const observations = yield* ComputerObservationStore.ComputerObservationStore;
15111513
const built = yield* MonitorStandardToolkit;
15121514
for (const tool of Object.values(built.tools)) {
@@ -1539,6 +1541,10 @@ const registerMonitorStandardTools = Effect.fn("McpHttpServer.registerMonitorSta
15391541
Effect.flatMap(Effect.fromOption),
15401542
Effect.provideService(ThreadManagementService.ThreadManagementService, threads),
15411543
Effect.provideService(ThreadMonitorService, service),
1544+
Effect.provideService(
1545+
ThreadMonitorSignalCallbacks.ThreadMonitorSignalCallbacks,
1546+
callbacks,
1547+
),
15421548
Effect.provideService(
15431549
ComputerObservationStore.ComputerObservationStore,
15441550
observations,

‎apps/server/src/mcp/toolkits/monitor/handlers.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as Effect from "effect/Effect";
33

44
import * as ComputerObservationStore from "../../../computer/ComputerObservationStore.ts";
55
import { ThreadMonitorService } from "../../../threadMonitor/ThreadMonitorService.ts";
6+
import * as ThreadMonitorSignalCallbacks from "../../../threadMonitor/ThreadMonitorSignalCallbacks.ts";
67
import * as McpInvocationContext from "../../McpInvocationContext.ts";
78
import * as McpToolAccess from "../../McpToolAccess.ts";
89
import { MonitorImageToolkit, MonitorStandardToolkit, MonitorToolkit } from "./tools.ts";
@@ -26,7 +27,7 @@ const handlers = {
2627
McpInvocationContext.requireThreadScope(scope, "This monitor tool"),
2728
),
2829
);
29-
const service = yield* ThreadMonitorService;
30+
const service = yield* ThreadMonitorSignalCallbacks.ThreadMonitorSignalCallbacks;
3031
return yield* service.create({ threadId: scope.thread.threadId, monitor });
3132
}),
3233
),

‎apps/server/src/mcp/toolkits/monitor/tools.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import {
33
OrchestratorMcpFailure,
44
ThreadMonitor,
5+
ThreadMonitorStartResult,
56
ThreadMonitorCancelInput,
67
ThreadMonitorCapabilities,
78
ThreadMonitorCheckInput,
@@ -21,6 +22,7 @@ import {
2122
import * as Schema from "effect/Schema";
2223
import { Tool, Toolkit } from "effect/ai";
2324

25+
import * as ThreadMonitorSignalCallbacks from "../../../threadMonitor/ThreadMonitorSignalCallbacks.ts";
2426
import * as McpInvocationContext from "../../McpInvocationContext.ts";
2527
import { ThreadMonitorService } from "../../../threadMonitor/ThreadMonitorService.ts";
2628
import * as ComputerObservationStore from "../../../computer/ComputerObservationStore.ts";
@@ -49,11 +51,11 @@ const mutatingMonitorTool = <T extends Tool.Any>(tool: T): T =>
4951
export const MonitorStartTool = mutatingMonitorTool(
5052
Tool.make("monitor_start", {
5153
description:
52-
"Create a one-time durable wait for the current T3 thread without keeping this model turn or process asleep. Use schedule type after/at for long timers. Before choosing a cache-conscious timer, call monitor_capabilities for the current controller model's optional minimum prompt-cache lifetime. Use signal when a background watcher, subagent, automation, or later turn will call monitor_signal; an optional deadlineAt provides a restart-safe fallback. By default the trigger delivers its result into the active turn when the provider supports live input; otherwise it queues a follow-up turn using the thread's configuration. Pending approvals or user-input requests delay delivery. Set continuation=record-only when a durable result should be recorded without starting a turn. T3 persists the monitor across server restarts and retains one logical continuation message across delivery retries.",
54+
"Create a one-time durable wait for the current T3 thread without keeping this model turn or process asleep. Use schedule type after/at for long timers. Before choosing a cache-conscious timer, call monitor_capabilities for the current controller model's optional minimum prompt-cache lifetime. Use signal when a background watcher, subagent, automation, or later turn will report completion. A signal monitor returns signalCallback with a completion-only credential and a shell command for a worker on this environment: pipe a JSON object with optional summary and evidence into that command. Keep the callback private. It works after this turn ends and across server restarts at the same listening address; cancellation disables it. Agents in this thread may also call monitor_signal; an optional deadlineAt provides a restart-safe fallback. By default the trigger delivers its result into the active turn when the provider supports live input; otherwise it queues a follow-up turn using the thread's configuration. Pending approvals or user-input requests delay delivery. Set continuation=record-only when a durable result should be recorded without starting a turn. T3 persists the monitor across server restarts and retains one logical continuation message across delivery retries.",
5355
parameters: Schema.toCodecJson(ThreadMonitorStartInput),
54-
success: ThreadMonitor,
56+
success: ThreadMonitorStartResult,
5557
failure: Schema.Union([ThreadMonitorError, OrchestratorMcpFailure]),
56-
dependencies,
58+
dependencies: [...dependencies, ThreadMonitorSignalCallbacks.ThreadMonitorSignalCallbacks],
5759
}).annotate(Tool.Title, "Start durable monitor"),
5860
);
5961

‎apps/server/src/mcp/toolkits/worktree/registration.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as ThreadMonitorSignalCallbacks from "../../../threadMonitor/ThreadMonitorSignalCallbacks.ts";
12
import { AgentDesktopManager } from "../../../agentDesktop/AgentDesktopManager.ts";
23
import { AgentDesktopTransferService } from "../../../agentDesktop/AgentDesktopTransferService.ts";
34
import { ComputerAutomationRouter } from "../../../computer/ComputerAutomationRouter.ts";
@@ -110,6 +111,7 @@ it.effect("production mcp layer lists worktree tools over http", () =>
110111
Layer.provide(PreviewAutomationBroker.layer),
111112
Layer.provide(PreviewBrowser.layer),
112113
Layer.provide(layerStubServices),
114+
Layer.provide(Layer.mock(ThreadMonitorSignalCallbacks.ThreadMonitorSignalCallbacks)({})),
113115
Layer.build,
114116
);
115117

‎apps/server/src/server.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as ThreadMonitorSignalCallbacks from "./threadMonitor/ThreadMonitorSignalCallbacks.ts";
2+
import * as MonitorSignalRoute from "./threadMonitor/signalRoute.ts";
13
import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEventLoggers";
24
import * as ProviderComputerLifecycle from "./orchestration-v2/ProviderComputerLifecycle.ts";
35
import * as ThreadWorkspaceQuery from "./orchestration-v2/ThreadWorkspaceQuery.ts";
@@ -349,7 +351,6 @@ const layerHttpServer = Layer.unwrap(
349351

350352
const layerPlatformServices = NodeServices.layer;
351353

352-
353354
const layerVcsDriverRegistry = VcsDriverRegistry.layer.pipe(Layer.provide(VcsProjectConfig.layer));
354355

355356
const layerSourceControlProviderRegistry = SourceControlProviderRegistry.layer.pipe(
@@ -760,6 +761,7 @@ const layerMakeRoutes = Layer.mergeAll(
760761
Layer.provide(PullRequestHttp.layer),
761762
Layer.provide(ProjectHttp.layer),
762763
Layer.provide(ServerHttp.layerServerEnvironmentHttpApi),
764+
Layer.provide(MonitorSignalRoute.layer),
763765
Layer.provide(WebhookRoute.layer.pipe(Layer.provide(RelayDeliveryProof.layer))),
764766
Layer.provide(AuthHttp.layerAuthenticatedAuth),
765767
),
@@ -782,6 +784,7 @@ const layerMakeRoutes = Layer.mergeAll(
782784
Layer.provide(McpOAuth.layerMcpClientAuthenticator),
783785
),
784786
).pipe(
787+
Layer.provide(ThreadMonitorSignalCallbacks.layer),
785788
// Both transports consume the same service instance, so caches single-flight across clients
786789
// and mutations observed on WebSocket invalidate patches subsequently read over HTTP.
787790
Layer.provide(layerDesktopServices),

‎apps/server/src/threadMonitor/ThreadMonitor.test.ts‎

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer";
2+
import * as ThreadMonitorSignalCallbacks from "./ThreadMonitorSignalCallbacks.ts";
13
import { assert, describe, it } from "@effect/vitest";
24
import * as NodeServices from "@effect/platform-node/NodeServices";
35
import {
@@ -2893,3 +2895,143 @@ it.effect("replays an accepted Stop before delivery without cancelling newer wat
28932895
),
28942896
),
28952897
);
2898+
2899+
it.effect(
2900+
"signal callbacks survive ended turns and service restarts without broad MCP credentials",
2901+
() =>
2902+
Effect.gen(function* () {
2903+
const config = yield* ServerConfig;
2904+
const callbackRuntime = ThreadMonitorSignalCallbacks.layer.pipe(
2905+
Layer.provideMerge(ThreadMonitor.layer),
2906+
Layer.provide(computerLayer),
2907+
Layer.provideMerge(orchestrationLayer(persistentDatabase(config.dbPath))),
2908+
Layer.provide(RepositoryIdentityResolver.layer),
2909+
Layer.provide(Layer.succeed(ServerConfig, config)),
2910+
Layer.provide(SqlitePersistence.layerFromPath(config.dbPath)),
2911+
Layer.provide(NodeHttpServer.layerTest),
2912+
Layer.provide(NodeServices.layer),
2913+
);
2914+
const monitor = yield* Effect.scoped(
2915+
Effect.gen(function* () {
2916+
yield* seedThread;
2917+
yield* startTestRun();
2918+
const callbacks = yield* ThreadMonitorSignalCallbacks.ThreadMonitorSignalCallbacks;
2919+
const timer = yield* callbacks.create({
2920+
threadId,
2921+
monitor: { label: "Timer", schedule: { type: "after", durationMs: 60_000 } },
2922+
});
2923+
assert.isUndefined(timer.signalCallback);
2924+
const monitor = yield* callbacks.create({
2925+
threadId,
2926+
monitor: { label: "Build result", schedule: { type: "signal" } },
2927+
});
2928+
assert.isDefined(monitor.signalCallback);
2929+
assert.notInclude(
2930+
monitor.signalCallback!.url,
2931+
monitor.signalCallback!.authorizationHeader,
2932+
);
2933+
yield* completeTestRun;
2934+
return monitor;
2935+
}).pipe(Effect.provide(Layer.fresh(callbackRuntime))),
2936+
);
2937+
2938+
yield* Effect.scoped(
2939+
Effect.gen(function* () {
2940+
const callbacks = yield* ThreadMonitorSignalCallbacks.ThreadMonitorSignalCallbacks;
2941+
const service = yield* ThreadMonitorService;
2942+
const signal = {
2943+
monitorId: monitor.id,
2944+
authorizationHeader: monitor.signalCallback!.authorizationHeader,
2945+
result: { summary: "Build succeeded.", evidence: "exit=0" },
2946+
};
2947+
assert.strictEqual(
2948+
(yield* callbacks
2949+
.signal({ ...signal, authorizationHeader: undefined })
2950+
.pipe(Effect.flip)).code,
2951+
"unauthorized",
2952+
);
2953+
assert.strictEqual(
2954+
(yield* callbacks
2955+
.signal({ ...signal, monitorId: ThreadMonitorId.make("different-monitor") })
2956+
.pipe(Effect.flip)).code,
2957+
"unauthorized",
2958+
);
2959+
assert.deepEqual(yield* callbacks.signal(signal), { status: "triggered" });
2960+
yield* TestClock.adjust("1 second");
2961+
yield* service.checkNow({ threadId, check: { monitorId: monitor.id } });
2962+
assert.deepEqual(
2963+
yield* callbacks.signal({
2964+
...signal,
2965+
result: { summary: "Duplicate must not replace evidence." },
2966+
}),
2967+
{ status: "delivered" },
2968+
);
2969+
const stored = (yield* service.status({ threadId, query: { monitorId: monitor.id } }))
2970+
.monitors[0]!;
2971+
assert.deepEqual(stored.trigger, {
2972+
reason: "signal",
2973+
summary: "Build succeeded.",
2974+
evidence: "exit=0",
2975+
});
2976+
assert.isFalse("signalCallback" in stored);
2977+
assert.strictEqual(stored.deliveryAttempts, 1);
2978+
const projection = yield* (yield* ProjectionStore.ProjectionStoreV2).getThreadProjection(
2979+
threadId,
2980+
);
2981+
assert.strictEqual(
2982+
projection.messages.filter(
2983+
(message) => message.notification?.systemEvent?.type === "monitor.continuation",
2984+
).length,
2985+
1,
2986+
);
2987+
2988+
const engine = yield* OrchestratorV2;
2989+
yield* engine.dispatch({
2990+
type: "thread.archive",
2991+
commandId: CommandId.make("callback-archive"),
2992+
threadId,
2993+
});
2994+
assert.strictEqual(
2995+
(yield* callbacks.signal(signal).pipe(Effect.flip)).code,
2996+
"unavailable",
2997+
);
2998+
yield* engine.dispatch({
2999+
type: "thread.unarchive",
3000+
commandId: CommandId.make("callback-unarchive"),
3001+
threadId,
3002+
});
3003+
3004+
const cancelled = yield* callbacks.create({
3005+
threadId,
3006+
monitor: { label: "Cancelled job", schedule: { type: "signal" } },
3007+
});
3008+
yield* service.cancel({ threadId, cancel: { monitorId: cancelled.id } });
3009+
assert.strictEqual(
3010+
(yield* callbacks
3011+
.signal({
3012+
monitorId: cancelled.id,
3013+
authorizationHeader: cancelled.signalCallback!.authorizationHeader,
3014+
result: {},
3015+
})
3016+
.pipe(Effect.flip)).code,
3017+
"unavailable",
3018+
);
3019+
yield* engine.dispatch({
3020+
type: "thread.delete",
3021+
commandId: CommandId.make("callback-delete"),
3022+
threadId,
3023+
});
3024+
assert.strictEqual(
3025+
(yield* callbacks.signal(signal).pipe(Effect.flip)).code,
3026+
"unavailable",
3027+
);
3028+
}).pipe(Effect.provide(Layer.fresh(callbackRuntime))),
3029+
);
3030+
}).pipe(
3031+
Effect.provide(
3032+
ServerConfig.layerTest(process.cwd(), { prefix: "t3-monitor-callback-test-" }).pipe(
3033+
Layer.provideMerge(NodeServices.layer),
3034+
),
3035+
),
3036+
),
3037+
);

0 commit comments

Comments
 (0)