| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120 |
- // L2:v4 原生 queue 命令组(deleteQueueItem / editQueueItem / reorderQueueItem /
- // setAutoDrain / setFollowupMode / sendQueuedNow)——验证命令直驱 core app 层 API,
- // sendQueuedNow 的「reserve → stop barrier → start/promote → remove」编排语义保真。
- // fake 风格参照 tests/v4-native-commands.test.ts(fake host/app,不经旧协议代码)。
- import { afterEach, describe, expect, it, vi } from "vitest";
- import { SessionEventType, type SessionEvent } from "@zcode/contracts";
- import type { CommandEnvelope, QueueItem } from "@zcode/shared/zcode-protocol-v4";
- import { V4CommandExecutor } from "../src/zcode-protocol-v4/commands/executor.js";
- import {
- V4QueueItemTextUnavailableError,
- V4QueueItemReservedError,
- V4QueueItemNotEditableError,
- V4SessionIdleTimeoutError,
- } from "../src/zcode-protocol-v4/commands/handlers/queue.js";
- import { V4CommandNoopError } from "../src/zcode-protocol-v4/index.js";
- import { shouldAutoDrainV4QueueHead } from "../src/zcode-protocol-v4/queue-auto-drain.js";
- import type {
- V4CommandCoreHost,
- V4SessionRecordView,
- } from "../src/zcode-protocol-v4/commands/types.js";
- function makeApp(overrides: Record<string, unknown> = {}) {
- const runtimeOverrides = (overrides.runtime ?? {}) as Record<string, unknown>;
- const appOverrides = { ...overrides };
- delete appOverrides.runtime;
- let foregroundPromotionLeaseId: string | undefined;
- return {
- sessionId: "s1",
- sendInput: vi.fn().mockImplementation(async (_input, options) => {
- options?.onTurnStartedObserved?.({
- id: "event-queue-turn-started",
- type: SessionEventType.TurnStarted,
- payload: { messageId: "msg-event-queue-turn-started" },
- });
- return {
- completion: Promise.resolve({}),
- kind: "started_turn",
- turnId: "turn-1",
- };
- }),
- submitPrompt: vi.fn().mockResolvedValue({}),
- steerTurn: vi.fn().mockResolvedValue({}),
- readTarget: vi.fn().mockResolvedValue(null),
- getMode: vi.fn().mockReturnValue("plan"),
- setTarget: vi.fn().mockImplementation(async (input: { objective: string }) => ({
- targetID: "g1",
- objective: input.objective,
- status: "active",
- })),
- continueActiveTarget: vi.fn().mockResolvedValue(null),
- updateTargetStatus: vi.fn().mockResolvedValue(null),
- reserveQueueItem: vi.fn().mockResolvedValue(true),
- markQueueItemPromoting: vi.fn().mockResolvedValue(true),
- releaseQueueItemReservation: vi.fn().mockResolvedValue(true),
- removeQueueItem: vi.fn().mockResolvedValue(true),
- editQueueItem: vi.fn().mockResolvedValue(true),
- reorderQueueItem: vi.fn().mockResolvedValue(true),
- setQueueAutoDrain: vi.fn().mockResolvedValue(undefined),
- setFollowupMode: vi.fn().mockResolvedValue(undefined),
- runtime: {
- acquireForegroundPromotionLease: vi.fn().mockImplementation(({ leaseId }) => {
- if (foregroundPromotionLeaseId && foregroundPromotionLeaseId !== leaseId) {
- return { kind: "conflict", leaseId: foregroundPromotionLeaseId };
- }
- foregroundPromotionLeaseId = leaseId;
- return { kind: "acquired", leaseId };
- }),
- getActiveForegroundExecutionId: vi.fn(() => undefined),
- releaseForegroundPromotionLease: vi.fn().mockImplementation((leaseId) => {
- if (foregroundPromotionLeaseId !== leaseId) return false;
- foregroundPromotionLeaseId = undefined;
- return true;
- }),
- stopActiveForegroundExecution: vi.fn().mockReturnValue({ kind: "idle" }),
- ...runtimeOverrides,
- },
- ...appOverrides,
- };
- }
- /**
- * 加速轮的协议原形:Selection 指向隐藏的加速卡内建 Provider(端点/能力来自 Provider Config),
- * 只有卡 JWT 与卡 ID 作为本次执行的动态鉴权随 modelExecution 下发。
- */
- const ORDINARY_MODEL_SELECTION = {
- providerId: "builtin:bigmodel-coding-plan",
- modelId: "GLM-5.3",
- };
- const HIGHSPEED_MODEL_SELECTION = {
- providerId: "account:bigmodel-highspeed-card",
- modelId: "GLM-5.3",
- };
- const HIGHSPEED_MODEL_EXECUTION = {
- selectionScope: "execution" as const,
- requestAuth: {
- apiKey: "card-1-jwt",
- headers: { Authorization: "Bearer card-1-jwt", "X-Highspeed-Card-ID": "card-1" },
- },
- selectionFallback: {
- providerId: "account:bigmodel-highspeed-card",
- target: ORDINARY_MODEL_SELECTION,
- rules: [
- { reason: "highspeed_card_expired" as const, providerErrorCode: "3402" },
- { reason: "highspeed_request_failed" as const },
- ],
- },
- };
- const HIGHSPEED_QUEUED_EXECUTION = {
- modelSelection: HIGHSPEED_MODEL_SELECTION,
- modelExecution: HIGHSPEED_MODEL_EXECUTION,
- };
- function queueItem(overrides: Partial<QueueItem> = {}): QueueItem {
- return {
- sourceCommandId: "source-command-q1",
- queueItemId: "q-1",
- clientId: "mobile-client",
- kind: "sendText",
- text: "排队原文",
- modelSelection: {
- providerId: "provider-at-submit",
- modelId: "model-at-submit",
- options: { reasoningLevel: "high" },
- },
- mode: "plan",
- attachments: [{ ref: "/tmp/note.txt", fileName: "note.txt", mime: "text/plain", bytes: 9 }],
- delivery: { requested: "queue", admitted: "queue" },
- order: { admissionSeq: 7, queuePosition: 0 },
- steer: { state: "notRequested" },
- dispatch: { state: "queued" },
- admittedAt: 1234,
- ...overrides,
- };
- }
- function makeRecord(
- overrides: Partial<V4SessionRecordView> & { app?: ReturnType<typeof makeApp> } = {},
- ): V4SessionRecordView {
- return {
- app: (overrides.app ?? makeApp()) as unknown as V4SessionRecordView["app"],
- workspace: { workspacePath: "/w" },
- persistence: "immediate",
- ...overrides,
- } as V4SessionRecordView;
- }
- function makeHost(
- record: V4SessionRecordView,
- hostOverrides: Partial<V4CommandCoreHost> = {},
- ): V4CommandCoreHost & { legacyMutations: string[] } {
- const legacyMutations: string[] = [];
- return {
- getRecord: (id) => (id === "s1" ? record : undefined),
- afterLegacyStateMutation: async (_r, reason) => {
- legacyMutations.push(reason);
- },
- waitForProjectionEventCommit: async () => {},
- // 默认查得到完整权威 intent(sendQueuedNow 正路径);各用例按需覆盖。
- getQueueItem: vi.fn().mockReturnValue(queueItem()),
- ...hostOverrides,
- legacyMutations,
- };
- }
- function envelope(type: string, payload: unknown): CommandEnvelope {
- return {
- type,
- payload,
- sessionId: "s1",
- commandId: "cmd-q1",
- baseRevision: 0,
- } as unknown as CommandEnvelope;
- }
- function deferred<T>() {
- let resolve!: (value: T | PromiseLike<T>) => void;
- let reject!: (reason?: unknown) => void;
- const promise = new Promise<T>((resolvePromise, rejectPromise) => {
- resolve = resolvePromise;
- reject = rejectPromise;
- });
- return { promise, reject, resolve };
- }
- async function flushDetachedWork() {
- await Promise.resolve();
- await Promise.resolve();
- await new Promise((resolve) => setTimeout(resolve, 0));
- }
- function workspaceHookReviewRequestedEvent(input: {
- id: string;
- interactionId: string;
- }): SessionEvent {
- return {
- id: input.id,
- type: SessionEventType.WorkspaceHookReviewRequested,
- payload: {
- request: {
- kind: "workspaceHookReview",
- reviewFlowId: "review-flow-queue",
- generation: 1,
- interactionId: input.interactionId,
- sessionId: "s1",
- taskId: "task-queue",
- runId: "run-queue",
- workspaceIdentity: "/w",
- workspaceLabel: "w",
- bundleDigest: "a".repeat(64),
- createdAt: Date.now(),
- deadlineAt: Date.now() + 10 * 60_000,
- sourceFiles: [
- {
- path: "/w/.zcode/config.json",
- displayPath: ".zcode/config.json",
- editable: true,
- },
- ],
- summary: { eventCount: 1, hookCount: 1, pendingCount: 1 },
- items: [
- {
- reviewItemId: "review-item-queue",
- event: "PreToolUse",
- matcher: "*",
- type: "process",
- displayName: "echo hello",
- displayCommand: "echo hello",
- sourcePath: "/w/.zcode/config.json",
- resolvedTimeoutMs: 1_000,
- resolvedMaxOutputBytes: 1_024,
- executionMode: "foreground",
- configuredEnabled: true,
- editable: true,
- trustState: "pending_trust",
- },
- ],
- warningCode: "workspace_hooks_execute_code",
- },
- },
- } as unknown as SessionEvent;
- }
- afterEach(() => {
- vi.useRealTimers();
- });
- describe("v4 原生 queue 单项命令", () => {
- it("deleteQueueItem:直驱 app.removeQueueItem(queueItemId ≡ pendingInputId)", async () => {
- const app = makeApp();
- const record = makeRecord({ app });
- await new V4CommandExecutor(makeHost(record)).execute(
- envelope("deleteQueueItem", { queueItemId: "q-1" }),
- );
- expect(app.removeQueueItem).toHaveBeenCalledWith("q-1");
- });
- it("editQueueItem:直驱 app.editQueueItem(保位原地更新)", async () => {
- const app = makeApp();
- const record = makeRecord({ app });
- await new V4CommandExecutor(makeHost(record)).execute(
- envelope("editQueueItem", { queueItemId: "q-1", newText: "改后文本" }),
- );
- expect(app.editQueueItem).toHaveBeenCalledWith("q-1", "改后文本");
- });
- it("editQueueItem:typed compact 不可编辑,避免 kind 与可见文本分裂", async () => {
- const app = makeApp();
- const record = makeRecord({ app });
- const host = makeHost(record, {
- getQueueItem: vi
- .fn()
- .mockReturnValue(queueItem({ kind: "compact", text: "/compact", attachments: [] })),
- });
- await expect(
- new V4CommandExecutor(host).execute(
- envelope("editQueueItem", { queueItemId: "q-1", newText: "伪装文本" }),
- ),
- ).rejects.toBeInstanceOf(V4QueueItemNotEditableError);
- expect(app.editQueueItem).not.toHaveBeenCalled();
- });
- it("reorderQueueItem:beforeQueueItemId 直传(null=队尾)", async () => {
- const app = makeApp();
- const record = makeRecord({ app });
- const executor = new V4CommandExecutor(makeHost(record));
- await executor.execute(
- envelope("reorderQueueItem", { queueItemId: "q-2", beforeQueueItemId: "q-1" }),
- );
- expect(app.reorderQueueItem).toHaveBeenCalledWith("q-2", "q-1");
- await executor.execute(
- envelope("reorderQueueItem", { queueItemId: "q-2", beforeQueueItemId: null }),
- );
- expect(app.reorderQueueItem).toHaveBeenLastCalledWith("q-2", null);
- });
- it("setAutoDrain:直驱 app.setQueueAutoDrain", async () => {
- const app = makeApp();
- const record = makeRecord({ app });
- const host = makeHost(record);
- await new V4CommandExecutor(host).execute(envelope("setAutoDrain", { autoDrain: true }));
- expect(app.setQueueAutoDrain).toHaveBeenCalledWith(true);
- expect(host.legacyMutations).toEqual(["queue_auto_drain_resumed"]);
- });
- it("setFollowupMode:直驱 app.setFollowupMode", async () => {
- const app = makeApp();
- const record = makeRecord({ app });
- await new V4CommandExecutor(makeHost(record)).execute(
- envelope("setFollowupMode", { mode: "guide" }),
- );
- expect(app.setFollowupMode).toHaveBeenCalledWith("guide");
- });
- it("delete 未命中抛可判别 noop,避免把已消费旧投影恢复到 composer", async () => {
- const app = makeApp({ removeQueueItem: vi.fn().mockResolvedValue(false) });
- const record = makeRecord({ app });
- const warn = vi.fn();
- await expect(
- new V4CommandExecutor(makeHost(record, { logger: { warn } })).execute(
- envelope("deleteQueueItem", { queueItemId: "q-gone" }),
- ),
- ).rejects.toMatchObject({
- reasonCode: "queue.itemMissing",
- } satisfies Partial<V4CommandNoopError>);
- expect(warn).toHaveBeenCalled();
- });
- });
- describe("v4 普通 queue 自动消费 goal gate", () => {
- it.each(["active", "paused", "budget_limited"] as const)(
- "target=%s 时即使 session ready 也不自动消费",
- (targetStatus) => {
- expect(
- shouldAutoDrainV4QueueHead({
- autoDrain: true,
- dispatchState: "queued",
- sessionBusy: false,
- targetStatus,
- }),
- ).toBe(false);
- },
- );
- it.each([null, "complete"] as const)(
- "target=%s 时保留既有 ready 自动消费语义",
- (targetStatus) => {
- expect(
- shouldAutoDrainV4QueueHead({
- autoDrain: true,
- dispatchState: "queued",
- sessionBusy: false,
- targetStatus,
- }),
- ).toBe(true);
- },
- );
- });
- describe("v4 原生 sendQueuedNow", () => {
- it("Highspeed queue 在卡仍有效时于提升边界应用单轮执行材料", async () => {
- const app = makeApp();
- const record = makeRecord({ app });
- const deleteQueuedTurnExecution = vi.fn();
- const host = makeHost(record, {
- getQueueItem: vi.fn().mockReturnValue(
- queueItem({
- // 队列持久化普通提交模型;临时 Highspeed Selection 与凭据只在 CLI 内存暂存。
- modelSelection: ORDINARY_MODEL_SELECTION,
- highspeed: {
- schemaVersion: 1,
- cardId: "card-1",
- taskId: "s1",
- provider: "bigmodel",
- model: "GLM-5.3",
- issuedAt: 1,
- expiresAt: Date.now() + 60_000,
- },
- }),
- ),
- readQueuedTurnExecution: vi.fn().mockReturnValue(HIGHSPEED_QUEUED_EXECUTION),
- deleteQueuedTurnExecution,
- });
- await new V4CommandExecutor(host).execute(envelope("sendQueuedNow", { queueItemId: "q-1" }));
- expect(host.readQueuedTurnExecution).toHaveBeenCalledWith("s1", "source-command-q1");
- expect(app.sendInput).toHaveBeenCalledWith(
- expect.any(Object),
- expect.objectContaining({
- intent: expect.objectContaining({
- highspeed: expect.objectContaining({ cardId: "card-1" }),
- modelSelection: HIGHSPEED_MODEL_SELECTION,
- }),
- }),
- );
- const sendOptions = app.sendInput.mock.calls[0]?.[1];
- // selectionScope=execution 表达「只作用于本轮」:不再需要提升后回写/恢复会话模型。
- expect(sendOptions.modelExecution.selectionScope).toBe("execution");
- await expect(
- sendOptions.modelExecution.requestDependencies.requestAuth.source.resolve(),
- ).resolves.toEqual(HIGHSPEED_MODEL_EXECUTION.requestAuth);
- expect(sendOptions.modelExecution.selectionFallback?.target).toEqual(ORDINARY_MODEL_SELECTION);
- // 凭据已冻结进本轮,暂存副本立即销毁,避免同一张卡被后续队列项复用。
- expect(deleteQueuedTurnExecution).toHaveBeenCalledWith("s1", "source-command-q1");
- });
- it("手动提升会使用命令补充的同卡执行材料,避免 CLI 暂存状态缺失后误降级", async () => {
- const app = makeApp();
- const record = makeRecord({ app });
- const highspeedMeta = {
- schemaVersion: 1 as const,
- cardId: "card-1",
- taskId: "s1",
- provider: "bigmodel",
- model: "GLM-5.3",
- issuedAt: 1,
- expiresAt: Date.now() + 60_000,
- };
- const refreshedExecution = {
- ...HIGHSPEED_MODEL_EXECUTION,
- requestAuth: {
- apiKey: "card-1-refreshed-jwt",
- headers: { Authorization: "Bearer card-1-refreshed-jwt", "X-Highspeed-Card-ID": "card-1" },
- },
- };
- const host = makeHost(record, {
- getQueueItem: vi
- .fn()
- .mockReturnValue(
- queueItem({ highspeed: highspeedMeta, modelSelection: ORDINARY_MODEL_SELECTION }),
- ),
- readQueuedTurnExecution: vi.fn().mockReturnValue(undefined),
- });
- await new V4CommandExecutor(host).execute(
- envelope("sendQueuedNow", {
- queueItemId: "q-1",
- highspeedMeta,
- modelSelection: HIGHSPEED_MODEL_SELECTION,
- modelExecution: refreshedExecution,
- }),
- );
- expect(app.sendInput).toHaveBeenCalledWith(
- expect.any(Object),
- expect.objectContaining({
- intent: expect.objectContaining({
- highspeed: highspeedMeta,
- modelSelection: HIGHSPEED_MODEL_SELECTION,
- }),
- }),
- );
- // 补供材料优先于内存暂存:这里 CLI 暂存为空,必须用命令带来的新 JWT 才不会降级。
- const sendOptions = app.sendInput.mock.calls[0]?.[1];
- await expect(
- sendOptions.modelExecution.requestDependencies.requestAuth.source.resolve(),
- ).resolves.toEqual(refreshedExecution.requestAuth);
- });
- it("Highspeed queue 提升时卡已过期则去掉加速标记并使用队列项普通模型", async () => {
- const sessionSelection = {
- providerId: "builtin:bigmodel-team-coding-plan",
- modelId: "GLM-5.3",
- };
- const app = makeApp({
- runtime: { getSessionModelSelection: vi.fn().mockReturnValue(sessionSelection) },
- });
- const record = makeRecord({ app });
- const deleteQueuedTurnExecution = vi.fn();
- const host = makeHost(record, {
- getQueueItem: vi.fn().mockReturnValue(
- queueItem({
- modelSelection: ORDINARY_MODEL_SELECTION,
- highspeed: {
- schemaVersion: 1,
- cardId: "expired-card",
- taskId: "s1",
- provider: "bigmodel",
- model: "GLM-5.3",
- issuedAt: 1,
- expiresAt: Date.now() - 1,
- },
- }),
- ),
- readQueuedTurnExecution: vi.fn().mockReturnValue(HIGHSPEED_QUEUED_EXECUTION),
- deleteQueuedTurnExecution,
- });
- await new V4CommandExecutor(host).execute(envelope("sendQueuedNow", { queueItemId: "q-1" }));
- const sendOptions = app.sendInput.mock.calls[0]?.[1];
- expect(sendOptions?.modelExecution).toBeUndefined();
- expect(sendOptions?.intent?.highspeed).toBeUndefined();
- // 队列项是发送时快照;降级不得回看之后可能已变化的 Session 模型。
- expect(sendOptions?.intent?.modelSelection).toEqual(ORDINARY_MODEL_SELECTION);
- expect(sendOptions?.intent?.modelSelection).not.toEqual(sessionSelection);
- expect(deleteQueuedTurnExecution).toHaveBeenCalledWith("s1", "source-command-q1");
- });
- it("旧队列把 Highspeed Provider 持久化且 execution 缺失时迁移回会话普通模型", async () => {
- const sessionSelection = {
- providerId: "builtin:bigmodel-team-coding-plan",
- modelId: "GLM-5.3",
- };
- const app = makeApp({
- runtime: { getSessionModelSelection: vi.fn().mockReturnValue(sessionSelection) },
- });
- const record = makeRecord({ app });
- const host = makeHost(record, {
- getQueueItem: vi.fn().mockReturnValue(
- queueItem({
- modelSelection: HIGHSPEED_MODEL_SELECTION,
- highspeed: {
- schemaVersion: 1,
- cardId: "legacy-card",
- taskId: "s1",
- provider: "bigmodel",
- model: "GLM-5.3",
- issuedAt: 1,
- expiresAt: Date.now() + 60_000,
- },
- }),
- ),
- readQueuedTurnExecution: vi.fn().mockReturnValue(undefined),
- });
- await new V4CommandExecutor(host).execute(envelope("sendQueuedNow", { queueItemId: "q-1" }));
- const sendOptions = app.sendInput.mock.calls[0]?.[1];
- expect(sendOptions?.modelExecution).toBeUndefined();
- expect(sendOptions?.intent?.highspeed).toBeUndefined();
- expect(sendOptions?.intent?.modelSelection).toEqual(sessionSelection);
- expect(sendOptions?.intent?.modelSelection).not.toEqual(HIGHSPEED_MODEL_SELECTION);
- });
- it("promotes quote-only input with its original sender metadata", async () => {
- const app = makeApp();
- const record = makeRecord({ app });
- const host = makeHost(record);
- const conversationQuotes = [{ text: "original", senderName: "Ryan Bot", senderId: "app-bot" }];
- vi.mocked(host.getQueueItem!).mockReturnValue(
- queueItem({ text: "", attachments: [], conversationQuotes }),
- );
- await new V4CommandExecutor(host).execute(envelope("sendQueuedNow", { queueItemId: "q-1" }));
- expect(app.sendInput).toHaveBeenCalledWith(
- expect.objectContaining({ text: "" }),
- expect.objectContaining({ intent: expect.objectContaining({ conversationQuotes }) }),
- );
- });
- it.each(["plan", "guarded", "yolo"] as const)(
- "%s 队列保留 mode/intent/附件:reserve → start → remove",
- async (mode) => {
- const app = makeApp();
- const record = makeRecord({ app });
- const host = makeHost(record, { getQueueItem: vi.fn().mockReturnValue(queueItem({ mode })) });
- await new V4CommandExecutor(host).execute(envelope("sendQueuedNow", { queueItemId: "q-1" }));
- expect(host.getQueueItem).toHaveBeenCalledWith("s1", "q-1");
- expect(app.reserveQueueItem).toHaveBeenCalledWith("q-1", "cmd-q1", expect.any(Object));
- expect(app.markQueueItemPromoting).toHaveBeenCalledWith("q-1", "cmd-q1", expect.any(Object));
- expect(app.sendInput).toHaveBeenCalledWith(
- {
- text: "排队原文",
- attachments: [expect.objectContaining({ path: "/tmp/note.txt", filename: "note.txt" })],
- },
- expect.objectContaining({
- delivery: "start_turn",
- inputId: "source-command-q1",
- queryId: "source-command-q1",
- intent: expect.objectContaining({
- sourceCommandId: "source-command-q1",
- queueItemId: "q-1",
- clientId: "mobile-client",
- modelSelection: {
- providerId: "provider-at-submit",
- modelId: "model-at-submit",
- options: { reasoningLevel: "high" },
- },
- mode,
- attachmentRefs: [
- { ref: "/tmp/note.txt", fileName: "note.txt", mime: "text/plain", bytes: 9 },
- ],
- }),
- }),
- );
- expect(app.removeQueueItem).toHaveBeenCalledWith(
- "q-1",
- expect.objectContaining({ reason: "promoted", reservationId: "cmd-q1" }),
- );
- expect(app.sendInput.mock.invocationCallOrder[0]).toBeLessThan(
- app.removeQueueItem.mock.invocationCallOrder[0],
- );
- },
- );
- it("Core admission 后直接 promoted/remove,不等待 turn completion", async () => {
- const turnGate = deferred<void>();
- const app = makeApp({
- sendInput: vi.fn().mockResolvedValue({
- completion: turnGate.promise,
- kind: "started_turn",
- turnId: "turn-q1",
- }),
- });
- const record = makeRecord({ app });
- const host = makeHost(record);
- await new V4CommandExecutor(host).execute(envelope("sendQueuedNow", { queueItemId: "q-1" }));
- // Core admission 完成后就移除队列项,不等待后台 turn 结束。
- expect(app.removeQueueItem).toHaveBeenCalledWith(
- "q-1",
- expect.objectContaining({ reason: "promoted", reservationId: "cmd-q1" }),
- );
- expect(app.releaseQueueItemReservation).not.toHaveBeenCalled();
- turnGate.resolve(undefined);
- await flushDetachedWork();
- });
- it("TurnStarted 缺 messageId 时仍完成 promotion,仅省略 repo 归因", async () => {
- const app = makeApp({
- sendInput: vi.fn().mockResolvedValue({
- completion: Promise.resolve({}),
- kind: "started_turn",
- turnId: "turn-q1",
- }),
- });
- await expect(
- new V4CommandExecutor(makeHost(makeRecord({ app }))).execute(
- envelope("sendQueuedNow", { queueItemId: "q-1" }),
- ),
- ).resolves.toBeUndefined();
- expect(app.removeQueueItem).toHaveBeenCalledWith(
- "q-1",
- expect.objectContaining({ reason: "promoted", reservationId: "cmd-q1" }),
- );
- expect(app.releaseQueueItemReservation).not.toHaveBeenCalled();
- });
- it("软门禁:pre-start failure 不误记 promoted,释放 reservation 回到 queued", async () => {
- const app = makeApp({
- sendInput: vi.fn().mockRejectedValue(new Error("runtime failed before queue TurnStarted")),
- });
- const record = makeRecord({ app });
- const executing = new V4CommandExecutor(makeHost(record)).execute(
- envelope("sendQueuedNow", { queueItemId: "q-1" }),
- );
- void executing.catch(() => undefined);
- await flushDetachedWork();
- await expect(executing).rejects.toMatchObject({
- message: expect.stringContaining("runtime failed"),
- });
- expect(app.removeQueueItem).not.toHaveBeenCalled();
- expect(app.releaseQueueItemReservation).toHaveBeenCalledWith(
- "q-1",
- "cmd-q1",
- expect.any(Object),
- );
- });
- it("goal queue item:sendQueuedNow 走 goal command path,不作为普通 prompt 重发", async () => {
- const app = makeApp();
- const record = makeRecord({ app });
- const host = makeHost(record, {
- getQueueItem: vi.fn().mockReturnValue({
- ...queueItem(),
- queueItemId: "q-goal",
- sourceCommandId: "source-goal",
- kind: "sendGoalCommand" as const,
- text: "/goal 排队目标",
- }),
- } as never);
- await new V4CommandExecutor(host).execute(envelope("sendQueuedNow", { queueItemId: "q-goal" }));
- expect(app.removeQueueItem).toHaveBeenCalledWith(
- "q-goal",
- expect.objectContaining({ reason: "promoted", reservationId: "cmd-q1" }),
- );
- expect(app.sendInput).not.toHaveBeenCalled();
- expect(app.setTarget).toHaveBeenCalledWith(
- expect.objectContaining({
- displayText: "/goal 排队目标",
- objective: "排队目标",
- status: "active",
- intent: expect.objectContaining({
- sourceCommandId: "source-goal",
- text: "排队目标",
- }),
- }),
- );
- });
- it("compact queue item:sendQueuedNow 走手动 compact path,不作为普通 prompt 重发", async () => {
- let finishCompact!: () => void;
- const compactGate = new Promise<Record<string, never>>((resolve) => {
- finishCompact = () => resolve({});
- });
- const app = makeApp({ submitPrompt: vi.fn().mockReturnValue(compactGate) });
- const record = makeRecord({ app });
- const host = makeHost(record, {
- getQueueItem: vi.fn().mockReturnValue(
- queueItem({
- queueItemId: "q-compact",
- sourceCommandId: "source-compact",
- kind: "compact",
- text: "/compact",
- attachments: [],
- }),
- ),
- recordPersistentCommandFact: vi.fn().mockResolvedValue(undefined),
- });
- await new V4CommandExecutor(host).execute(
- envelope("sendQueuedNow", { queueItemId: "q-compact" }),
- );
- expect(app.submitPrompt).toHaveBeenCalledWith(
- "/compact",
- expect.objectContaining({ inputId: "source-compact" }),
- );
- expect(app.sendInput).not.toHaveBeenCalled();
- expect(app.removeQueueItem).toHaveBeenCalledWith(
- "q-compact",
- expect.objectContaining({ reason: "promoted", reservationId: "cmd-q1" }),
- );
- expect(host.legacyMutations).toContain("queue_compact_promoted");
- finishCompact();
- await new Promise((resolve) => setTimeout(resolve, 0));
- });
- it("运行中会话:stop(只 abort 不 await)→ 轮询等 finally 释放锁 → 重发", async () => {
- const calls: string[] = [];
- const app = makeApp({
- sendInput: vi.fn().mockImplementation(async (_input, options) => {
- calls.push("resend");
- options?.onTurnStartedObserved?.({
- id: "event-queue-resend-turn-started",
- type: SessionEventType.TurnStarted,
- payload: { messageId: "msg-event-queue-resend-turn-started" },
- });
- return {};
- }),
- });
- const controller = new AbortController();
- // 模拟后台 turn 的 finally:收到 abort 后延迟 60ms(>1 个轮询间隔)才释放锁。
- controller.signal.addEventListener("abort", () => {
- calls.push("abort");
- setTimeout(() => {
- calls.push("release");
- record.activeAbortController = undefined;
- }, 60);
- });
- const record = makeRecord({ app, activeAbortController: controller });
- await new V4CommandExecutor(makeHost(record)).execute(
- envelope("sendQueuedNow", { queueItemId: "q-1" }),
- );
- // 关键顺序:abort → 锁释放 → 才重发(否则撞 "A prompt is already running")。
- expect(calls).toEqual(["abort", "release", "resend"]);
- expect(app.sendInput).toHaveBeenCalledWith(
- expect.objectContaining({ text: "排队原文" }),
- expect.objectContaining({ inputId: "source-command-q1" }),
- );
- });
- it.each([
- { stopKind: "stopped", withAttachment: false, autoDrain: false, expected: "user_steer" },
- { stopKind: "idle", withAttachment: false, autoDrain: false, expected: undefined },
- { stopKind: "stopped", withAttachment: true, autoDrain: false, expected: undefined },
- { stopKind: "stopped", withAttachment: false, autoDrain: true, expected: undefined },
- ])(
- "队列立即发送 human 提示:$stopKind / attachment=$withAttachment / auto=$autoDrain",
- async ({ stopKind, withAttachment, autoDrain, expected }) => {
- const stopActiveForegroundExecution = vi
- .fn()
- .mockReturnValue(
- stopKind === "stopped"
- ? { kind: "stopped", foregroundExecutionId: "foreground-1" }
- : { kind: "idle" },
- );
- const app = makeApp({ runtime: { stopActiveForegroundExecution } });
- const item = queueItem(withAttachment ? {} : { attachments: [] });
- await new V4CommandExecutor(
- makeHost(makeRecord({ app }), { getQueueItem: () => item }),
- ).execute(
- envelope("sendQueuedNow", { queueItemId: "q-1" }),
- undefined,
- autoDrain ? { autoDrainPromotion: true } : undefined,
- );
- expect(app.sendInput).toHaveBeenCalledTimes(1);
- const [input, options] = app.sendInput.mock.calls[0]!;
- expect(input.text).toBe(item.text);
- expect(options.inputPresentation).toBe(expected);
- expect(options).toMatchObject({
- inputId: item.sourceCommandId,
- requireIdle: true,
- intent: {
- sourceCommandId: item.sourceCommandId,
- clientId: item.clientId,
- requestedDelivery: "queue",
- },
- });
- expect(stopActiveForegroundExecution).toHaveBeenCalledTimes(autoDrain ? 0 : 1);
- expect(app.removeQueueItem).toHaveBeenCalledWith(
- "q-1",
- expect.objectContaining({ reason: "promoted", reservationId: "cmd-q1" }),
- );
- },
- );
- it("M07a:运行中立即发送把内部抢占标记为保留 queue auto-drain", async () => {
- const calls: string[] = [];
- let record!: V4SessionRecordView;
- const stopActiveForegroundExecution = vi.fn().mockImplementation(() => {
- calls.push("runtime-stop");
- setTimeout(() => {
- calls.push("release");
- record.activeAbortController = undefined;
- }, 20);
- return { kind: "stopped", foregroundExecutionId: "foreground-1" };
- });
- const app = makeApp({
- runtime: { stopActiveForegroundExecution },
- sendInput: vi.fn().mockImplementation(async (_input, options) => {
- calls.push("resend");
- options?.onTurnStartedObserved?.({
- id: "event-queue-preserve-turn-started",
- type: SessionEventType.TurnStarted,
- payload: { messageId: "msg-event-queue-preserve-turn-started" },
- });
- return {};
- }),
- });
- const controller = new AbortController();
- record = makeRecord({ app, activeAbortController: controller });
- await new V4CommandExecutor(makeHost(record)).execute(
- envelope("sendQueuedNow", { queueItemId: "q-1" }),
- );
- expect(stopActiveForegroundExecution).toHaveBeenCalledWith({
- preserveQueueAutoDrainOnCancel: true,
- reason: "v4 sendQueuedNow preempts active turn",
- });
- expect(controller.signal.aborted).toBe(false);
- expect(calls).toEqual(["runtime-stop", "release", "resend"]);
- });
- it("BG42:notification 只占用 Core foreground 时仍先 stop 再提升 queue item", async () => {
- const calls: string[] = [];
- let activeForegroundExecutionId: string | undefined = "notification-foreground-1";
- const stopActiveForegroundExecution = vi.fn().mockImplementation(() => {
- calls.push("runtime-stop");
- setTimeout(() => {
- calls.push("runtime-release");
- activeForegroundExecutionId = undefined;
- }, 60);
- return { kind: "stopped", foregroundExecutionId: "notification-foreground-1" };
- });
- const app = makeApp({
- runtime: {
- getActiveForegroundExecutionId: vi.fn(() => activeForegroundExecutionId),
- stopActiveForegroundExecution,
- },
- sendInput: vi.fn().mockImplementation(async (_input, options) => {
- // Bug 原因:model-only notification turn 只登记在 Core foreground authority,
- // Bootstrap 没有 activeAbortController。真实 input facade 在旧 foreground 未停止时
- // 会把同一 queue item 当 steer 接收,因而不会提交 TurnStarted authority。
- if (activeForegroundExecutionId !== undefined) {
- calls.push("steer-old-notification");
- return {};
- }
- calls.push("resend");
- options?.onTurnStartedObserved?.({
- id: "event-notification-send-now-turn-started",
- type: SessionEventType.TurnStarted,
- payload: { messageId: "msg-event-notification-send-now-turn-started" },
- });
- return {};
- }),
- });
- const record = makeRecord({ app });
- await new V4CommandExecutor(makeHost(record)).execute(
- envelope("sendQueuedNow", { queueItemId: "q-1" }),
- );
- expect(record.activeAbortController).toBeUndefined();
- expect(stopActiveForegroundExecution).toHaveBeenCalledWith({
- preserveQueueAutoDrainOnCancel: true,
- reason: "v4 sendQueuedNow preempts active turn",
- });
- expect(calls).toEqual(["runtime-stop", "runtime-release", "resend"]);
- expect(app.removeQueueItem).toHaveBeenCalledWith(
- "q-1",
- expect.objectContaining({ reason: "promoted", reservationId: "cmd-q1" }),
- );
- });
- it("BG42:Core foreground stop 后未释放时超时,原 queue item 保持可重试", async () => {
- vi.useFakeTimers();
- const stopActiveForegroundExecution = vi.fn().mockReturnValue({
- kind: "stopped",
- foregroundExecutionId: "notification-foreground-stuck",
- });
- const app = makeApp({
- runtime: {
- getActiveForegroundExecutionId: vi.fn(() => "notification-foreground-stuck"),
- stopActiveForegroundExecution,
- },
- });
- const record = makeRecord({ app });
- const pending = new V4CommandExecutor(makeHost(record)).execute(
- envelope("sendQueuedNow", { queueItemId: "q-1" }),
- );
- const assertion = expect(pending).rejects.toBeInstanceOf(V4SessionIdleTimeoutError);
- await vi.advanceTimersByTimeAsync(5_100);
- await assertion;
- expect(app.sendInput).not.toHaveBeenCalled();
- expect(app.removeQueueItem).not.toHaveBeenCalled();
- expect(app.releaseQueueItemReservation).toHaveBeenCalledWith(
- "q-1",
- "cmd-q1",
- expect.any(Object),
- );
- });
- it("BG44:notification A 释放后的 promotion 空窗不得让 notification B 抢入", async () => {
- const calls: string[] = [];
- let activeForegroundExecutionId: string | undefined = "notification-a";
- let foregroundPromotionLeaseId: string | undefined;
- let foregroundPromotionInputId: string | undefined;
- const acquireForegroundPromotionLease = vi
- .fn()
- .mockImplementation(({ leaseId, promotedInputId }) => {
- if (foregroundPromotionLeaseId && foregroundPromotionLeaseId !== leaseId) {
- return { kind: "conflict", leaseId: foregroundPromotionLeaseId };
- }
- foregroundPromotionLeaseId = leaseId;
- foregroundPromotionInputId = promotedInputId;
- calls.push("acquire-promotion-lease");
- return { kind: "acquired", leaseId };
- });
- const releaseForegroundPromotionLease = vi.fn().mockImplementation((leaseId) => {
- if (foregroundPromotionLeaseId === leaseId) foregroundPromotionLeaseId = undefined;
- foregroundPromotionInputId = undefined;
- });
- const stopActiveForegroundExecution = vi.fn().mockImplementation(() => {
- calls.push("stop-notification-a");
- activeForegroundExecutionId = undefined;
- return { kind: "stopped", foregroundExecutionId: "notification-a" };
- });
- const sendInput = vi.fn().mockImplementation(async (_input, options) => {
- if (options?.inputId === foregroundPromotionInputId) {
- foregroundPromotionLeaseId = undefined;
- foregroundPromotionInputId = undefined;
- }
- if (activeForegroundExecutionId !== undefined) {
- calls.push("promoted-prompt-rejected");
- throw new Error("A runtime foreground command is already active");
- }
- calls.push("promoted-prompt-started");
- options?.onTurnStartedObserved?.({
- id: "event-bg44-promoted-turn-started",
- type: SessionEventType.TurnStarted,
- payload: { messageId: "msg-event-bg44-promoted-turn-started" },
- });
- return {};
- });
- const app = makeApp({
- runtime: {
- acquireForegroundPromotionLease,
- getActiveForegroundExecutionId: vi.fn(() => activeForegroundExecutionId),
- releaseForegroundPromotionLease,
- stopActiveForegroundExecution,
- },
- markQueueItemPromoting: vi.fn().mockImplementation(async () => {
- calls.push("queue-item-promoting");
- // 复现 runtime command drain 在 stop/wait 与 startPromptTurn 之间取得调度权。
- queueMicrotask(() => {
- if (
- activeForegroundExecutionId === undefined &&
- foregroundPromotionLeaseId === undefined
- ) {
- activeForegroundExecutionId = "notification-b";
- calls.push("notification-b-started");
- }
- });
- return true;
- }),
- sendInput,
- });
- await new V4CommandExecutor(makeHost(makeRecord({ app }))).execute(
- envelope("sendQueuedNow", { queueItemId: "q-1" }),
- );
- expect(calls).toEqual([
- "acquire-promotion-lease",
- "stop-notification-a",
- "queue-item-promoting",
- "promoted-prompt-started",
- ]);
- expect(acquireForegroundPromotionLease).toHaveBeenCalledWith({
- leaseId: "queue-promotion:cmd-q1",
- mode: "after-current",
- promotedInputId: "source-command-q1",
- });
- expect(app.removeQueueItem).toHaveBeenCalledOnce();
- });
- it("运行中 + active goal:先 pause goal 再 abort(barrier 顺序)+ legacy 广播", async () => {
- const calls: string[] = [];
- const app = makeApp({
- readTarget: vi.fn().mockResolvedValue({ targetID: "g1", status: "active" }),
- updateTargetStatus: vi.fn().mockImplementation(async () => {
- calls.push("pause");
- return { targetID: "g1", status: "paused" };
- }),
- });
- const controller = new AbortController();
- controller.signal.addEventListener("abort", () => {
- calls.push("abort");
- record.activeAbortController = undefined;
- });
- const record = makeRecord({ app, activeAbortController: controller });
- const host = makeHost(record);
- await new V4CommandExecutor(host).execute(envelope("sendQueuedNow", { queueItemId: "q-1" }));
- expect(calls).toEqual(["pause", "abort"]);
- expect(host.legacyMutations).toContain("send_queued_now_goal_paused");
- });
- it("5s 超时:锁始终不释放 → V4SessionIdleTimeoutError,且不重发", async () => {
- vi.useFakeTimers();
- const stopActiveForegroundExecution = vi.fn().mockReturnValue({
- kind: "stopped",
- foregroundExecutionId: "foreground-timeout",
- });
- const app = makeApp({
- runtime: { stopActiveForegroundExecution },
- });
- const controller = new AbortController();
- // 不挂 abort 监听:模拟 turn 卡死,finally 永不释放锁。
- const record = makeRecord({ app, activeAbortController: controller });
- const pending = new V4CommandExecutor(makeHost(record)).execute(
- envelope("sendQueuedNow", { queueItemId: "q-1" }),
- );
- const assertion = expect(pending).rejects.toBeInstanceOf(V4SessionIdleTimeoutError);
- await vi.advanceTimersByTimeAsync(5_100);
- await assertion;
- expect(app.sendInput).not.toHaveBeenCalled();
- expect(app.removeQueueItem).not.toHaveBeenCalled();
- expect(app.releaseQueueItemReservation).toHaveBeenCalledWith(
- "q-1",
- "cmd-q1",
- expect.any(Object),
- );
- expect(stopActiveForegroundExecution).toHaveBeenCalledWith({
- preserveQueueAutoDrainOnCancel: true,
- reason: "v4 sendQueuedNow preempts active turn",
- });
- });
- it("原文查不到(已 drain/删除或 id 无效):拒绝且绝不盲删", async () => {
- const app = makeApp();
- const record = makeRecord({ app });
- const host = makeHost(record, {
- getQueueItem: vi.fn().mockReturnValue(null),
- });
- await expect(
- new V4CommandExecutor(host).execute(envelope("sendQueuedNow", { queueItemId: "q-missing" })),
- ).rejects.toBeInstanceOf(V4QueueItemTextUnavailableError);
- expect(app.removeQueueItem).not.toHaveBeenCalled();
- expect(app.sendInput).not.toHaveBeenCalled();
- });
- it("host 未接 getQueueItem 能力:同样拒绝(不能退化为 text-only 重发)", async () => {
- const app = makeApp();
- const record = makeRecord({ app });
- const host = makeHost(record, { getQueueItem: undefined });
- await expect(
- new V4CommandExecutor(host).execute(envelope("sendQueuedNow", { queueItemId: "q-1" })),
- ).rejects.toBeInstanceOf(V4QueueItemTextUnavailableError);
- });
- it("同 queue item 已被另一端 reserve:明确拒绝且不 stop/start/remove", async () => {
- const app = makeApp({ reserveQueueItem: vi.fn().mockResolvedValue(false) });
- const controller = new AbortController();
- const record = makeRecord({ app, activeAbortController: controller });
- await expect(
- new V4CommandExecutor(makeHost(record)).execute(
- envelope("sendQueuedNow", { queueItemId: "q-1" }),
- ),
- ).rejects.toBeInstanceOf(V4QueueItemReservedError);
- expect(controller.signal.aborted).toBe(false);
- expect(app.sendInput).not.toHaveBeenCalled();
- expect(app.removeQueueItem).not.toHaveBeenCalled();
- });
- it("start barrier 失败:释放 reservation,原 queue item 不移除", async () => {
- const app = makeApp();
- const record = makeRecord({ app });
- const host = makeHost(record, {
- ensureModelReady: vi.fn().mockRejectedValue(new Error("model unavailable")),
- });
- await expect(
- new V4CommandExecutor(host).execute(envelope("sendQueuedNow", { queueItemId: "q-1" })),
- ).rejects.toThrow("model unavailable");
- expect(app.releaseQueueItemReservation).toHaveBeenCalledWith(
- "q-1",
- "cmd-q1",
- expect.any(Object),
- );
- expect(app.removeQueueItem).not.toHaveBeenCalled();
- });
- });
|