fix(orchestrator): Harden Grok v2 runtime lifecycle

Settle root turns from provider signals and preserve post-settle continuations.

Track asynchronous subagent and monitor work, allow image prompts, and keep steered messages visible.

Separate soft steering from hard Stop while containing and reaping native process trees.
This commit is contained in:
Mike Olson authored and Julius Marminge committed 2026-08-07 13:24:47 +02:00
1 parent 4a33b6715c
commit aebfec6eb9
7 files changed
+5325 -96

No files matched your search

+10
View File
@@ -80,6 +80,16 @@ jobs:
sudo apt-get install -y --no-install-recommends build-essential
command -v cc
# Blacksmith boots GitHub's Ubuntu runner image (gcc is usually present),
# but ACP process-tree live tests compile a small pthread fixture with `cc`
# and soft-skip when it is missing. Install build-essential so that path
# always runs in CI instead of silently no-oping.
- name: Install C toolchain for process-tree fixtures
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends build-essential
command -v cc
- name: Setup Vite+
uses: voidzero-dev/setup-vp@v1
with:
+5
View File
@@ -95,6 +95,11 @@ function logResidualCallbackResponse(kind: string): void {
NodeFS.appendFileSync(residualCallbackResponseLogPath, `${kind}\n`, "utf8");
}
function logResidualCallbackResponse(kind: string): void {
if (!residualCallbackResponseLogPath) return;
NodeFS.appendFileSync(residualCallbackResponseLogPath, `${kind}\n`, "utf8");
}
process.once("SIGTERM", () => {
logExit("SIGTERM");
process.exit(0);
File diff suppressed because it is too large. Load diff
@@ -2674,7 +2674,7 @@ export function makeAcpAdapterV2(options: AcpAdapterV2Options): ProviderAdapterV
});
});
const offerContinuationRun = Effect.fnUntraced(function* (_sessionId: string) {
const offerContinuationRun = Effect.fnUntraced(function* (sessionId: string) {
if (continuationRequests === undefined) {
return;
}
@@ -2706,14 +2706,6 @@ export function makeAcpAdapterV2(options: AcpAdapterV2Options): ProviderAdapterV
providerThreadId: route.providerThreadId,
driver,
detail: null,
clearIfCurrent: () =>
continuationPermit.withPermit(
Effect.gen(function* () {
if ((yield* Ref.get(continuationGeneration)) === generation) {
yield* Ref.set(continuationRequested, false);
}
}),
),
dispatchIfCurrent: (effect) =>
continuationPermit.withPermit(
Effect.gen(function* () {
@@ -2731,14 +2723,11 @@ export function makeAcpAdapterV2(options: AcpAdapterV2Options): ProviderAdapterV
return Option.none();
}
if (!(yield* Ref.get(continuationRequested))) return Option.none();
// A successful durable dispatch owns the sticky flag until its
// continuation turn starts. Clearing here opens a dispatch-to-
// start race where every late ACP frame can enqueue another
// synthetic continuation. Failures clear immediately because
// no turn will arrive to do so.
// Clear after success or failure so a dropped dispatch does not
// stick the flag and pin hasPendingBackgroundWork forever.
const exit = yield* Effect.exit(effect);
yield* clearIfOwner;
if (Exit.isFailure(exit)) {
yield* clearIfOwner;
return yield* Effect.failCause(exit.cause);
}
return Option.some(exit.value);
@@ -3875,8 +3864,8 @@ export function makeAcpAdapterV2(options: AcpAdapterV2Options): ProviderAdapterV
runtime = yield* flavor
.makeRuntime(makeRuntimeInput(runtimeGeneration))
.pipe(
Effect.provideService(Scope.Scope, runtimeScope),
Effect.provideService(Crypto.Crypto, options.crypto),
Effect.provideService(Scope.Scope, runtimeScope),
);
});
@@ -4195,12 +4184,10 @@ export function makeAcpAdapterV2(options: AcpAdapterV2Options): ProviderAdapterV
// Minimal quiet for all models (no per-model carveouts). Defer +
// awaitingBackgroundHydration hold the turn through monitors; this
// is only a short debounce after the last rearm so a slightly late
// post-hydration assistant chunk stays in the same continuation.
// Grok commonly sends its final summary just over two seconds after
// the hydrated tool frame; two seconds split that tail into a second
// synthetic wake. Longer floors (4–20s) only prolonged Working.
// first post-hydration assistant chunk is not dropped. Multi-second
// floors (4–20s) only prolonged Working on grok-4.5.
yield* Effect.gen(function* () {
yield* Effect.sleep("3000 millis");
yield* Effect.sleep("2000 millis");
if (
context.finalized ||
context.interrupted ||
@@ -4316,7 +4303,7 @@ export function makeAcpAdapterV2(options: AcpAdapterV2Options): ProviderAdapterV
const needsSessionActivation =
(yield* Ref.get(activeSessionId)) !== requestedSessionId || restartAfterInterrupt;
if (needsSessionActivation) {
const activated = yield* activateSession(requestedSessionId, turnInput.threadId);
const activated = yield* activateSession(requestedSessionId);
yield* Ref.set(activeSessionId, activated.sessionId);
yield* Ref.set(activeSessionSetup, activated);
yield* configureSession(activated, turnInput.modelSelection, turnInput.runtimePolicy);
@@ -4685,10 +4672,7 @@ export function makeAcpAdapterV2(options: AcpAdapterV2Options): ProviderAdapterV
loadingRole: null,
loadingIndex: 0,
});
const activated = yield* activateSession(
sessionId,
threadInput.providerThread.appThreadId,
);
const activated = yield* activateSession(sessionId);
yield* Ref.set(activeSessionId, activated.sessionId);
yield* Ref.set(activeSessionSetup, activated);
const nextSelection = threadInput.modelSelection ?? input.modelSelection;
@@ -4992,9 +4976,6 @@ export function makeAcpAdapterV2(options: AcpAdapterV2Options): ProviderAdapterV
Effect.timeoutOption("10 seconds"),
);
if (Option.isNone(stopped)) {
if (!context.finalized) {
yield* finalizeTurn(context, "interrupted");
}
return yield* new ProviderAdapterProtocolError({
driver,
detail: `ACP provider turn ${turnInput.providerTurnId} did not acknowledge cancellation before the interrupt timeout`,
@@ -5102,9 +5083,7 @@ export function makeAcpAdapterV2(options: AcpAdapterV2Options): ProviderAdapterV
loadingRole: null,
loadingIndex: 0,
});
const activated = yield* runtime.loadSession(sessionId, {
mcpServers: acpMcpServers(snapshotInput.providerThread.appThreadId),
});
const activated = yield* runtime.loadSession(sessionId);
yield* Ref.set(activeSessionId, activated.sessionId);
yield* Ref.set(activeSessionSetup, activated);
yield* Ref.set(activeSelection, null);
@@ -5169,9 +5148,7 @@ export function makeAcpAdapterV2(options: AcpAdapterV2Options): ProviderAdapterV
});
}
const sourceSessionId = nativeThreadId(driver, forkInput.sourceProviderThread);
const forked = yield* runtime.forkSession(sourceSessionId, {
mcpServers: acpMcpServers(forkInput.targetThreadId),
});
const forked = yield* runtime.forkSession(sourceSessionId);
yield* Ref.set(activeSessionId, forked.sessionId);
yield* Ref.set(activeSessionSetup, forked);
yield* Ref.set(activeSelection, null);
@@ -13,8 +13,6 @@ export interface ProviderContinuationRequest {
readonly dispatchIfCurrent?: <A, E, R>(
effect: Effect.Effect<A, E, R>,
) => Effect.Effect<Option.Option<A>, E, R>;
/** Clears a pending offer that the continuation worker intentionally drops. */
readonly clearIfCurrent?: () => Effect.Effect<void>;
}
/**
@@ -190,35 +190,4 @@ describe("ProviderContinuationService", () => {
);
});
});
it.effect("clears a sticky continuation offer when its thread is archived", () => {
return Effect.gen(function* () {
const dispatched = yield* Queue.unbounded<unknown>();
const cleared = yield* Ref.make(false);
yield* Effect.gen(function* () {
const requests = yield* ProviderContinuationRequests;
yield* requests.offer({
...request(),
clearIfCurrent: () => Ref.set(cleared, true),
});
for (let attempt = 0; attempt < 20 && !(yield* Ref.get(cleared)); attempt += 1) {
yield* Effect.yieldNow;
}
assert.isTrue(yield* Ref.get(cleared));
assert.isTrue(Option.isNone(yield* Queue.poll(dispatched)));
}).pipe(
Effect.provide(
testLayer({
dispatched,
getThreadProjection: () =>
Effect.succeed({
...projection,
thread: { ...projection.thread, archivedAt: "2026-07-20T00:00:00.000Z" },
} as OrchestrationV2ThreadProjection),
}),
),
Effect.scoped,
);
});
});
});
@@ -32,12 +32,8 @@ export const workerLive = Layer.effectDiscard(
threadId: request.threadId,
providerThreadId: request.providerThreadId,
});
// No continuation turn will start to clear the adapter's sticky offer.
if (request.clearIfCurrent !== undefined) {
yield* request.clearIfCurrent();
} else if (request.dispatchIfCurrent !== undefined) {
// Backward compatibility for request producers without an explicit
// drop callback.
// Still run the guard so adapters can clear sticky continuationRequested.
if (request.dispatchIfCurrent !== undefined) {
yield* request.dispatchIfCurrent(Effect.void);
}
return;