Skip to content
Closed
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
85 changes: 38 additions & 47 deletions apps/server/src/orchestration-v2/Adapters/ZcodeAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { ZCODE_PROVIDER, makeZcodeAdapterV2 } from "@t3tools/provider-zcode/test
import {
type FakeZcodeScenario,
ZcodeAdapterTestLayer,
fakeZcodeRequestAuth,
makeFakeZcodeConnection,
makeZcodeAdapterOptions,
zcodeDriverKind,
Expand Down Expand Up @@ -303,7 +302,7 @@ describe("ZcodeAdapterV2 (frame-driven)", () => {
// thread.runtime-mode.set detaches the session; the next turn reopens and resumes it.
const resumedScenario = yield* makeFakeZcodeConnection();
const resumed = yield* openRuntime(resumedScenario, runtimePolicy);
yield* resumed.ensureThread({
const resumedThread = yield* resumed.ensureThread({
threadId: THREAD_ID,
modelSelection: MODEL_SELECTION,
runtimePolicy,
Expand All @@ -315,6 +314,21 @@ describe("ZcodeAdapterV2 (frame-driven)", () => {
sessionId: "sess_1",
mode: "guarded",
});

// A resumed session advertises its models only through session/subscribe; the next turn
// still needs the advertised reasoning level.
yield* resumed.startTurn(
yield* makeTurnInput(
resumedThread,
TURN_TEXT,
"account:zai-individual-coding-plan/GLM-5.3-Flash",
),
);
assert.deepEqual(requestFor(resumedScenario, "session/send")?.params["modelSelection"], {
providerId: "account:zai-individual-coding-plan",
modelId: "GLM-5.3-Flash",
options: { reasoningLevel: "max" },
});
assert.deepEqual(resumedScenario.unexpectedMethods(), []);
}).pipe(Effect.scoped, Effect.provide(ZcodeAdapterTestLayer)),
);
Expand Down Expand Up @@ -451,52 +465,29 @@ describe("ZcodeAdapterV2 (frame-driven)", () => {
}).pipe(Effect.scoped, Effect.provide(ZcodeAdapterTestLayer)),
);

it.effect(
"sends account-model turns with the advertised reasoning level and serves runtime headers",
() =>
Effect.gen(function* () {
const harness = yield* makeHarness({ makeRequestAuth: fakeZcodeRequestAuth() });
// "default" omits the model entirely; account slugs select per turn.
yield* harness.startTurn(TURN_TEXT, "account:zai-individual-coding-plan/GLM-5.3-Flash");
const send = requestFor(harness.scenario, "session/send");
assert.isDefined(send);
assert.deepEqual(send!.params["modelSelection"], {
providerId: "account:zai-individual-coding-plan",
modelId: "GLM-5.3-Flash",
options: { reasoningLevel: "max" },
});

// Before an account-model request the CLI asks the host for project runtime headers;
// the injected request auth's response must reach the wire verbatim.
const broker = zcodeServerRequest("interaction/requestProviderRuntimeHeaders", {
sessionId: harness.scenario.createdSessionId(),
modelSelection: {
providerId: "account:zai-individual-coding-plan",
modelId: "GLM-5.3-Flash",
options: { reasoningLevel: "max" },
},
});
yield* harness.scenario.offerEvent(broker);
// The first two responds are the create-time runtime preferences and MCP auth headers.
yield* harness.scenario.takeRespond;
yield* harness.scenario.takeRespond;
const answered = yield* harness.scenario.takeRespond;
assert.equal(answered.id, broker.id);
assert.deepEqual(answered.result, {
headersApplied: true,
requestAuth: { apiKey: "test", apiKeyId: "k", accountScope: "a" },
});
it.effect("sends account-model turns with the advertised reasoning level", () =>
Effect.gen(function* () {
const harness = yield* makeHarness();
// "default" omits the model entirely; account slugs select per turn.
yield* harness.startTurn(TURN_TEXT, "account:zai-individual-coding-plan/GLM-5.3-Flash");
const send = requestFor(harness.scenario, "session/send");
assert.isDefined(send);
assert.deepEqual(send!.params["modelSelection"], {
providerId: "account:zai-individual-coding-plan",
modelId: "GLM-5.3-Flash",
options: { reasoningLevel: "max" },
});

yield* harness.offerTurnEvent("turn.completed", {
response: "pong",
tokenCount: 3,
toolCallCount: 0,
duration: 60,
resultType: "success",
});
yield* harness.takeEvent("turn.terminal", (event) => event.status === "completed");
assert.deepEqual(harness.scenario.unexpectedMethods(), []);
}).pipe(Effect.scoped, Effect.provide(ZcodeAdapterTestLayer)),
yield* harness.offerTurnEvent("turn.completed", {
response: "pong",
tokenCount: 3,
toolCallCount: 0,
duration: 60,
resultType: "success",
});
yield* harness.takeEvent("turn.terminal", (event) => event.status === "completed");
assert.deepEqual(harness.scenario.unexpectedMethods(), []);
}).pipe(Effect.scoped, Effect.provide(ZcodeAdapterTestLayer)),
);

it.effect("maps shell tool lifecycle into command execution items", () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ export interface FakeZcodeScenario {
}

const KNOWN_METHODS = new Set([
"provider/updateAccountConfig",
"provider/refreshRegistry",
"session/create",
"session/read",
"session/resume",
Expand Down Expand Up @@ -119,7 +117,7 @@ function zcodeSessionCreateResult(sessionId: string, params: ZcodeRecord): Zcode
};
}

/** Builds the fake `zcode app-server` NDJSON peer the adapter talks to. */
/** Builds the fake ZCode connection peer the adapter talks to. */
export const makeFakeZcodeConnection = Effect.fnUntraced(function* (): Effect.fn.Return<
FakeZcodeScenario,
never,
Expand Down Expand Up @@ -168,7 +166,14 @@ export const makeFakeZcodeConnection = Effect.fnUntraced(function* (): Effect.fn
return zcodeSessionCreateResult(createdSessionId, params);
}
case "session/subscribe":
return { sessionId: createdSessionId, eventSeq: 1, events: [] };
return {
sessionId: createdSessionId,
eventSeq: 1,
events: [],
snapshot: {
settings: zcodeSessionCreateResult(createdSessionId ?? "sess_1", params).settings,
},
};
case "session/send": {
yield* Queue.offer(events, {
method: "state.updated",
Expand All @@ -182,14 +187,6 @@ export const makeFakeZcodeConnection = Effect.fnUntraced(function* (): Effect.fn
});
return { sessionId: createdSessionId, accepted: true, stateRevision: 1 };
}
case "provider/updateAccountConfig":
return {
receivedRevision: String(params["revision"] ?? ""),
providerCount: Object.keys((params["providers"] ?? {}) as ZcodeRecord).length,
status: "received",
};
case "provider/refreshRegistry":
return { refreshed: true };
case "session/read":
return zcodeSessionCreateResult(createdSessionId ?? "sess_unknown", params);
case "session/stop":
Expand Down Expand Up @@ -263,19 +260,6 @@ export const makeZcodeAdapterOptions = (): ZcodeAdapterV2Options => ({
environment: {},
});

/**
* Request auth the adapter can drive without touching the local credential store or network:
* every runtime-headers request is answered with the fixed successful exchange.
*/
export const fakeZcodeRequestAuth =
(): NonNullable<ZcodeAdapterV2Options["makeRequestAuth"]> => () => ({
answerRuntimeHeaders: () =>
Effect.succeed({
headersApplied: true,
requestAuth: { apiKey: "test", apiKeyId: "k", accountScope: "a" },
}),
});

/** The driver kind the package driver and the hand-built adapter options must agree on. */
export const zcodeDriverKind: typeof ZCODE_PROVIDER = ZCODE_PROVIDER;

Expand Down
14 changes: 7 additions & 7 deletions docs/user/providers-zcode.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# ZCode

T3 Code runs the ZCode CLI as an agent, using your own Z.AI account, skills, plugins, and MCP
configuration.
T3 Code attaches to the ZCode daemon (`zcode serve`) as an agent, using your own Z.AI account,
skills, plugins, and MCP configuration. T3 Code starts the daemon when it is not already running
and leaves it running when the session ends, so other ZCode clients on the machine keep working.

## Set Up ZCode

Expand All @@ -16,11 +17,10 @@ with T3 Code; the server's own `zcode login` is what counts.

## Credentials

T3 Code never stores your Z.AI sign-in. The ZCode agent asks its host for credentials before every
account-model request, so the server stands in as that host: it reads the sign-in `zcode login`
already wrote on the same machine, exchanges it for a short-lived access token through the same
Z.AI endpoints the ZCode app uses, and holds that token in memory only. The store is never
written, so signing out or signing in again from a terminal applies on the next refresh.
T3 Code never stores or reads your Z.AI sign-in. The ZCode daemon owns the credential exchange:
it reads the sign-in `zcode login` wrote on the same machine and mints the short-lived account
tokens its turns need. T3 Code attaches to that daemon and never handles the tokens. Signing out
or signing in again from a terminal applies to the next turn.

## Models

Expand Down
Loading
Loading