fix(orchestrator): harden Grok v2 lifecycle (#3578)

Integrate the current Grok settlement, monitor/subagent lifecycle, interrupt containment, steer visibility, and image support stack into the orchestration-v2 branch.

Co-authored-by: codex <[email protected]>
This commit is contained in:
Mike Olsonandcodex authored and Julius Marminge committed 2026-08-15 16:49:58 +02:00
1 parent 0282ab8608
commit e16d4e514b
53 files changed
+15859 -985

No files matched your search

@@ -753,6 +753,40 @@ describe("AcpSessionRuntime", () => {
),
);
it.effect("completes ad-hoc loadSession after replay becomes idle while RPC stays pending", () =>
Effect.gen(function* () {
const runtime = yield* AcpSessionRuntime.AcpSessionRuntime;
yield* runtime.start();
const loaded = yield* runtime.loadSession("mock-session-1").pipe(Effect.timeout("2 seconds"));
expect(loaded.sessionId).toBe("mock-session-1");
expect(loaded.sessionSetupResult._meta).toMatchObject({
t3SessionLoadReady: "replay_idle",
});
}).pipe(
Effect.provide(
AcpSessionRuntime.layer({
authMethodId: "test",
spawn: {
command: mockAgentCommand,
args: mockAgentArgs,
env: {
T3_ACP_HANG_LOAD_SESSION_AFTER_REPLAY: "1",
T3_ACP_LOAD_SESSION_DELAY_MS: "10000",
},
},
cwd: process.cwd(),
sessionLoadReplayIdleGap: "50 millis",
sessionLoadTimeout: "1 second",
clientInfo: { name: "t3-test", version: "0.0.0" },
}),
),
Effect.scoped,
Effect.provide(NodeServices.layer),
TestClock.withLive,
),
);
it.effect("rejects invalid config option values before sending session/set_config_option", () => {
const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "acp-runtime-"));
const requestLogPath = NodePath.join(tempDir, "requests.ndjson");
@@ -8,6 +8,7 @@ import {
parsePermissionRequest,
parseSessionModeState,
parseSessionUpdateEvent,
sessionUpdateCountsAsLoadReplayActivity,
sessionUpdateIsReplay,
syntheticLoadSessionResponseFromInitialize,
} from "./AcpRuntimeModel.ts";
@@ -83,6 +84,98 @@ describe("AcpRuntimeModel", () => {
).toBe(false);
});
it("ignores Grok keepalive chunks when tracking session/load replay activity", () => {
expect(
sessionUpdateCountsAsLoadReplayActivity({
sessionId: "session-1",
update: {
sessionUpdate: "agent_message_chunk",
content: { type: "text", text: "" },
},
} satisfies EffectAcpSchema.SessionNotification),
).toBe(false);
expect(
sessionUpdateCountsAsLoadReplayActivity({
_meta: { isReplay: true },
sessionId: "session-1",
update: {
sessionUpdate: "tool_call",
toolCallId: "replay-tool",
title: "Replay",
kind: "search",
status: "completed",
},
} satisfies EffectAcpSchema.SessionNotification),
).toBe(true);
});
it("ignores load-replay activity from other sessions while a load gate is active", () => {
expect(
sessionUpdateCountsAsLoadReplayActivity(
{
sessionId: "session-other",
update: {
sessionUpdate: "agent_message_chunk",
content: { type: "text", text: "unrelated" },
},
} satisfies EffectAcpSchema.SessionNotification,
"session-loading",
),
).toBe(false);
expect(
sessionUpdateCountsAsLoadReplayActivity(
{
sessionId: "session-loading",
update: {
sessionUpdate: "agent_message_chunk",
content: { type: "text", text: "replay" },
},
} satisfies EffectAcpSchema.SessionNotification,
"session-loading",
),
).toBe(true);
});
it("counts mode/config/session/usage updates as load-replay activity", () => {
expect(
sessionUpdateCountsAsLoadReplayActivity({
sessionId: "session-1",
update: {
sessionUpdate: "current_mode_update",
currentModeId: "code",
},
} satisfies EffectAcpSchema.SessionNotification),
).toBe(true);
expect(
sessionUpdateCountsAsLoadReplayActivity({
sessionId: "session-1",
update: {
sessionUpdate: "config_option_update",
configOptions: [],
},
} satisfies EffectAcpSchema.SessionNotification),
).toBe(true);
expect(
sessionUpdateCountsAsLoadReplayActivity({
sessionId: "session-1",
update: {
sessionUpdate: "session_info_update",
title: "Restored",
},
} satisfies EffectAcpSchema.SessionNotification),
).toBe(true);
expect(
sessionUpdateCountsAsLoadReplayActivity({
sessionId: "session-1",
update: {
sessionUpdate: "usage_update",
used: 1,
size: 10,
},
} satisfies EffectAcpSchema.SessionNotification),
).toBe(true);
});
it("builds a synthetic load response from initialize model state", () => {
const response = syntheticLoadSessionResponseFromInitialize({
protocolVersion: 1,
@@ -459,8 +459,38 @@ export function sessionUpdateIsReplay(params: EffectAcpSchema.SessionNotificatio
return isRecord(meta) && meta.isReplay === true;
}
/** Replay chunks and substantive updates during session/load; not Grok keepalives. */
export function sessionUpdateCountsAsLoadReplayActivity(
params: EffectAcpSchema.SessionNotification,
gatedSessionId?: string,
): boolean {
if (gatedSessionId !== undefined && params.sessionId !== gatedSessionId) {
return false;
}
if (sessionUpdateIsReplay(params)) return true;
const update = params.update;
switch (update.sessionUpdate) {
case "agent_message_chunk":
case "agent_thought_chunk":
return update.content.type === "text" && update.content.text.length > 0;
case "tool_call":
case "tool_call_update":
case "plan":
case "available_commands_update":
case "current_mode_update":
case "config_option_update":
case "session_info_update":
case "usage_update":
return true;
default:
return false;
}
}
export interface SessionLoadGate {
readonly active: boolean;
/** Only notifications for this session refresh load-replay idle activity. */
readonly sessionId: string;
readonly lastActivityAtMillis: number | undefined;
readonly idleGap: Duration.Duration;
readonly initializeResult: EffectAcpSchema.InitializeResponse;
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
@@ -5,9 +5,33 @@ import * as EffectAcpErrors from "effect-acp/errors";
import {
applyGrokAcpModelSelection,
buildGrokAcpSpawnInput,
grokAcpRuntimeProcessOwnership,
resolveGrokAcpBaseModelId,
} from "./GrokAcpSupport.ts";
describe("grokAcpRuntimeProcessOwnership", () => {
it("opts Grok into detached process-tree ownership on the injected host platform", () => {
expect(grokAcpRuntimeProcessOwnership("linux")).toEqual({
ownDescendantProcessGroups: true,
ownDetachedProcessGroup: true,
processGroupPlatform: "linux",
});
});
it("uses the prior provider-group path on Darwin and Windows", () => {
expect(grokAcpRuntimeProcessOwnership("darwin")).toEqual({
ownDescendantProcessGroups: false,
ownDetachedProcessGroup: true,
processGroupPlatform: "darwin",
});
expect(grokAcpRuntimeProcessOwnership("win32")).toEqual({
ownDescendantProcessGroups: false,
ownDetachedProcessGroup: true,
processGroupPlatform: "win32",
});
});
});
describe("resolveGrokAcpBaseModelId", () => {
it("normalizes empty and custom Grok model ids", () => {
expect(resolveGrokAcpBaseModelId(undefined)).toBe("grok-build");
@@ -1,3 +1,4 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import { type GrokSettings, ProviderDriverKind } from "@t3tools/contracts";
import * as Crypto from "effect/Crypto";
import * as Effect from "effect/Effect";
@@ -6,6 +7,7 @@ import * as Scope from "effect/Scope";
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
import * as EffectAcpErrors from "effect-acp/errors";
import type * as EffectAcpSchema from "effect-acp/schema";
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
import { normalizeModelSlug } from "@t3tools/shared/model";
import * as AcpSessionRuntime from "./AcpSessionRuntime.ts";
@@ -48,6 +50,21 @@ function resolveGrokAuthMethodId(environment: NodeJS.ProcessEnv | undefined): st
: GROK_AUTH_METHOD_CACHED_TOKEN;
}
export function grokAcpRuntimeProcessOwnership(
processGroupPlatform: NodeJS.Platform,
): Pick<
AcpSessionRuntime.AcpSessionRuntimeOptions,
"ownDescendantProcessGroups" | "ownDetachedProcessGroup" | "processGroupPlatform"
> {
return {
// macOS keeps the prior provider-group teardown until a stable libproc
// identity provider can cover Grok's nested detached tool groups.
ownDescendantProcessGroups: processGroupPlatform === "linux",
ownDetachedProcessGroup: true,
processGroupPlatform,
};
}
export const makeGrokAcpRuntime = (
input: GrokAcpRuntimeInput,
): Effect.Effect<
@@ -56,11 +73,15 @@ export const makeGrokAcpRuntime = (
Crypto.Crypto | Scope.Scope
> =>
Effect.gen(function* () {
const processGroupPlatform = yield* HostProcessPlatform.pipe(
Effect.provide(NodeServices.layer),
);
const acpContext = yield* Layer.build(
AcpSessionRuntime.layer({
...input,
spawn: buildGrokAcpSpawnInput(input.grokSettings, input.cwd, input.environment),
authMethodId: resolveGrokAuthMethodId(input.environment),
...grokAcpRuntimeProcessOwnership(processGroupPlatform),
}).pipe(
Layer.provide(
Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, input.childProcessSpawner),
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff