| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747 |
- import { describe, expect, it, vi } from 'vitest';
- import type { SendMessageReq } from '@mavis/local-runtime-v2/cli-service';
- import { TuiFailure } from '../../src/failure.js';
- import {
- TuiChatController as ProductionTuiChatController,
- type CreateTuiChatControllerOptions,
- } from '../../src/tui/controller/chat-controller.js';
- import type { TuiStreamEvent } from '../../src/runtime/stream-events.js';
- import { TranscriptStore } from '../../src/tui/transcript/store.js';
- import { TranscriptView } from '../../src/tui/transcript/view.js';
- import { TuiRunCoordinator, type TuiRunRuntime } from '../../src/application/run-coordinator.js';
- import { resolveTuiVisiblePresentation } from '../../src/tui/controller/projection/visible-presentation.js';
- import { TuiActivityLine } from '../../src/tui/shell/activity-line.js';
- import { isQuestionnaireTool } from '../../src/tui/controller/projection/turn-tool-projection.js';
- class TuiChatController extends ProductionTuiChatController {
- constructor(options: CreateTuiChatControllerOptions) {
- super(options);
- }
- }
- describe('TuiChatController', () => {
- it('hides plain and namespaced AskUser protocol tools from the transcript', () => {
- expect(isQuestionnaireTool('ask_user')).toBe(true);
- expect(isQuestionnaireTool('functions.AskUser')).toBe(true);
- expect(isQuestionnaireTool('tools.ask_user_question')).toBe(true);
- expect(isQuestionnaireTool('read_file')).toBe(false);
- });
- it('shows estimated output throughput while a text response is still streaming', async () => {
- let nowMs = 0;
- let releaseResponse: (() => void) | undefined;
- const responseGate = new Promise<void>((resolve) => {
- releaseResponse = resolve;
- });
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-live-output-rate' })),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield {
- type: 'delta',
- turnId: 'turn-live-output-rate',
- messageId: 'message-live-output-rate',
- content: 'hello',
- timestamp: 1_000,
- };
- nowMs = 250;
- yield {
- type: 'delta',
- turnId: 'turn-live-output-rate',
- messageId: 'message-live-output-rate',
- content: ' world',
- timestamp: 2_000,
- };
- await responseGate;
- yield {
- type: 'delta',
- turnId: 'turn-live-output-rate',
- messageId: 'message-live-output-rate',
- finish: true,
- timestamp: 3_000,
- };
- yield {
- type: 'message',
- message: {
- id: 'message-live-output-rate',
- turnId: 'turn-live-output-rate',
- role: 'assistant',
- content: 'hello world',
- usage: { outputTokens: 126, requestDurationMs: 2_000 },
- },
- };
- yield { type: 'done', turnId: 'turn-live-output-rate' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new ProductionTuiChatController({
- runtime: runtime as never,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-live-output-rate',
- now: () => nowMs,
- });
- const submission = controller.submit('Say hello');
- await vi.waitFor(() =>
- expect(controller.snapshot()).toMatchObject({
- activeTurnId: 'turn-live-output-rate',
- outputTokensPerSecond: 2,
- outputTokensPerSecondEstimated: true,
- }),
- );
- const snapshot = controller.snapshot();
- const presentation = resolveTuiVisiblePresentation({
- snapshot,
- connection: { phase: 'live', generation: 1 },
- surface: 'conversation',
- currentLiveRunId: snapshot.activeTurnId,
- transcript,
- runtimeQueuedCount: 0,
- queueEnabled: true,
- activePermission: false,
- activeQuestionnaire: false,
- compacting: false,
- attachmentCount: 0,
- version: 'test',
- workspace: '/workspace',
- });
- const line = new TuiActivityLine(presentation.activity, { animate: false });
- expect(line.render(120).join('\n')).toContain('⚡ ~2.0 tok/s');
- releaseResponse?.();
- await submission;
- });
- it('keeps provider-calibrated output throughput visible after a text-only turn settles', async () => {
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-output-rate' })),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield {
- type: 'delta',
- turnId: 'turn-output-rate',
- messageId: 'message-output-rate',
- content: 'hello',
- timestamp: 1_000,
- };
- yield {
- type: 'delta',
- turnId: 'turn-output-rate',
- messageId: 'message-output-rate',
- finish: true,
- timestamp: 3_000,
- };
- yield {
- type: 'message',
- message: {
- id: 'message-output-rate',
- turnId: 'turn-output-rate',
- role: 'assistant',
- content: 'hello',
- usage: { outputTokens: 126, requestDurationMs: 2_000 },
- },
- };
- yield { type: 'done', turnId: 'turn-output-rate' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new ProductionTuiChatController({
- runtime: runtime as never,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-output-rate',
- });
- await controller.submit('Say hello');
- const view = new TranscriptView(() => transcript.snapshot());
- expect(view.render(80).join('\n')).toContain('⚡ 63.0 tok/s');
- });
- it('accumulates provider output throughput for one turn and resets it for the next', () => {
- const controller = new ProductionTuiChatController({
- runtime: {} as never,
- transcript: new TranscriptStore(),
- workspaceDir: '/workspace',
- });
- controller.beginRuntimeTurn('turn-1', 500);
- controller.applyRuntimeTurnEvent('turn-1', {
- type: 'delta',
- turnId: 'turn-1',
- messageId: 'message-1',
- content: 'first chunk',
- timestamp: 1_000,
- });
- controller.applyRuntimeTurnEvent('turn-1', {
- type: 'delta',
- turnId: 'turn-1',
- messageId: 'message-1',
- finish: true,
- timestamp: 3_000,
- });
- controller.applyRuntimeTurnEvent('turn-1', {
- type: 'message',
- message: {
- id: 'message-1',
- turnId: 'turn-1',
- role: 'assistant',
- usage: { outputTokens: 126, requestDurationMs: 2_000 },
- },
- });
- expect(controller.snapshot().outputTokensPerSecond).toBe(63);
- controller.beginRuntimeTurn('turn-2', 4_000);
- expect(controller.snapshot().outputTokensPerSecond).toBeUndefined();
- });
- it('publishes one state change when a stream event updates throughput and Transcript', () => {
- let nowMs = 0;
- const onChange = vi.fn();
- const controller = new ProductionTuiChatController({
- runtime: {} as never,
- transcript: new TranscriptStore(),
- workspaceDir: '/workspace',
- onChange,
- now: () => nowMs,
- });
- controller.beginRuntimeTurn('turn-batched', 500);
- controller.applyRuntimeTurnEvent('turn-batched', {
- type: 'delta',
- turnId: 'turn-batched',
- messageId: 'message-batched',
- content: 'first chunk',
- timestamp: 1_000,
- });
- onChange.mockClear();
- nowMs = 250;
- controller.applyRuntimeTurnEvent('turn-batched', {
- type: 'delta',
- turnId: 'turn-batched',
- messageId: 'message-batched',
- content: 'second chunk',
- timestamp: 2_000,
- });
- expect(controller.snapshot().outputTokensPerSecond).toBeGreaterThan(0);
- expect(onChange).toHaveBeenCalledOnce();
- });
- it('projects a pending user message before the login preflight settles', async () => {
- let resolveAccount:
- | ((account: { status: 'ready'; managedTokenPresent: true; warnings: never[] }) => void)
- | undefined;
- const runtime = {
- getAccountStatus: vi.fn(
- () =>
- new Promise<{
- status: 'ready';
- managedTokenPresent: true;
- warnings: never[];
- }>((resolve) => {
- resolveAccount = resolve;
- }),
- ),
- createSession: vi.fn(async () => ({
- sessionId: 'session-optimistic',
- workspaceDir: '/workspace',
- })),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield { type: 'done', turnId: 'turn-optimistic' };
- }),
- abortSession: vi.fn(async () => true),
- steer: vi.fn(),
- };
- const transcript = new TranscriptStore();
- const onUserSubmissionProjected = vi.fn();
- const controller = new ProductionTuiChatController({
- runtime: runtime as never,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-optimistic',
- now: () => 100,
- onUserSubmissionProjected,
- });
- controller.projectOptimisticUserMessage('submission-1', 'Show this immediately', 90);
- const sending = controller.submit('Show this immediately', {
- optimisticRequestId: 'submission-1',
- });
- expect(transcript.get('user:turn-optimistic')).toMatchObject({
- kind: 'user',
- status: 'pending',
- content: 'Show this immediately',
- createdAtMs: 90,
- });
- expect(transcript.get('optimistic:user:submission-1')).toBeUndefined();
- expect(transcript.snapshot().filter((cell) => cell.kind === 'user')).toHaveLength(1);
- expect(onUserSubmissionProjected).toHaveBeenCalledOnce();
- expect(runtime.createSession).not.toHaveBeenCalled();
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- resolveAccount?.({ status: 'ready', managedTokenPresent: true, warnings: [] });
- await expect(sending).resolves.toBe('succeeded');
- });
- it('detaches the foreground observer when switching Sessions without aborting the Runtime turn', async () => {
- let releaseTurn: (() => void) | undefined;
- const runtime = {
- createSession: vi.fn(),
- getSession: vi.fn(async (sessionId: string) => ({
- sessionId,
- workspaceDir: '/workspace',
- })),
- getMessages: vi.fn(async () => []),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- await new Promise<void>((resolve) => {
- releaseTurn = resolve;
- });
- yield { type: 'delta', text: 'late answer from A' };
- yield { type: 'done' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-a',
- now: () => 100,
- });
- await controller.loadSessionProjection('session-a');
- const running = controller.submit('keep running in A');
- await vi.waitFor(() => expect(releaseTurn).toBeTypeOf('function'));
- await controller.loadSessionProjection('session-b');
- expect(runtime.abortSession).not.toHaveBeenCalled();
- expect(controller.snapshot().session?.sessionId).toBe('session-b');
- releaseTurn?.();
- await expect(running).resolves.toBe('succeeded');
- expect(controller.snapshot().session?.sessionId).toBe('session-b');
- expect(transcript.snapshot().some((cell) => cell.content.includes('late answer from A'))).toBe(
- false,
- );
- });
- it('requires MiniMax login before starting a managed-model Turn', async () => {
- const runtime = {
- getAccountStatus: vi.fn(async () => ({
- status: 'needs-login' as const,
- authMode: 'managed-login',
- modelSource: 'token-plan' as const,
- managedTokenPresent: false,
- warnings: [],
- })),
- createSession: vi.fn(async () => ({
- sessionId: 'session-login-required',
- workspaceDir: '/workspace',
- })),
- sendMessage: vi.fn(),
- abortSession: vi.fn(async () => true),
- steer: vi.fn(),
- };
- const transcript = new TranscriptStore();
- const controller = new ProductionTuiChatController({
- runtime: runtime as never,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-login-required',
- now: () => 100,
- });
- await expect(controller.submit('Do not send this yet')).rejects.toMatchObject({
- name: 'TuiLoginRequiredError',
- code: 'auth.login_required',
- });
- expect(runtime.getAccountStatus).toHaveBeenCalledWith(undefined, undefined);
- expect(runtime.createSession).not.toHaveBeenCalled();
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- expect(transcript.snapshot()).toEqual([]);
- expect(controller.snapshot()).toMatchObject({
- status: 'idle',
- activeTurnId: undefined,
- account: { managedTokenPresent: false },
- });
- });
- it('defers owner history reconciliation until the active Turn has settled', async () => {
- let releaseTurn: (() => void) | undefined;
- const runtime = {
- createSession: vi.fn(async () => ({
- sessionId: 'session-history-deferred',
- workspaceDir: '/workspace',
- })),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- await new Promise<void>((resolve) => {
- releaseTurn = resolve;
- });
- yield { type: 'done', turnId: 'turn-history-deferred' };
- }),
- abortSession: vi.fn(async () => true),
- getSession: vi.fn(async () => ({
- sessionId: 'session-history-deferred',
- workspaceDir: '/workspace',
- })),
- getMessages: vi.fn(async () => []),
- };
- const controller = new TuiChatController({
- runtime,
- transcript: new TranscriptStore(),
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-history-deferred',
- now: () => 100,
- });
- const submission = controller.submit('Keep the Turn active');
- await vi.waitFor(() =>
- expect(controller.snapshot().activeTurnId).toBe('turn-history-deferred'),
- );
- await vi.waitFor(() => expect(releaseTurn).toBeTypeOf('function'));
- await expect(controller.reconcileOwnerHistory(false)).resolves.toBe(false);
- expect(runtime.getMessages).not.toHaveBeenCalled();
- releaseTurn?.();
- await submission;
- await vi.waitFor(() =>
- expect(runtime.getMessages).toHaveBeenCalledWith('session-history-deferred'),
- );
- });
- it('shares in-flight session creation with runtime-owned follow-up submission', async () => {
- let resolveSession: ((session: { sessionId: string }) => void) | undefined;
- const runtime = {
- createSession: vi.fn(
- () =>
- new Promise<{ sessionId: string }>((resolve) => {
- resolveSession = resolve;
- }),
- ),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield { type: 'done' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const controller = new TuiChatController({
- runtime,
- transcript: new TranscriptStore(),
- workspaceDir: '/workspace',
- });
- const submitting = controller.submit('First turn');
- await vi.waitFor(() => expect(resolveSession).toBeTypeOf('function'));
- const waitingForSession = controller.waitForCurrentSession();
- resolveSession?.({ sessionId: 'session-1' });
- await expect(waitingForSession).resolves.toEqual({ sessionId: 'session-1' });
- await submitting;
- expect(runtime.createSession).toHaveBeenCalledTimes(1);
- });
- it('creates a session lazily and updates one assistant cell in place while streaming', async () => {
- const runtime = {
- createSession: vi.fn(async () => ({
- sessionId: 'session-1',
- title: 'New session',
- workspaceDir: '/workspace',
- })),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield { type: 'delta', turnId: 'turn-1', role: 'assistant', content: 'Hel' };
- yield { type: 'delta', turnId: 'turn-1', role: 'assistant', content: 'lo' };
- yield { type: 'done', turnId: 'turn-1' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-1',
- now: (() => {
- let value = 100;
- return () => value++;
- })(),
- });
- await controller.submit(' Say hello ');
- expect(runtime.createSession).toHaveBeenCalledWith({ workspaceDir: '/workspace' });
- expect(runtime.sendMessage).toHaveBeenCalledWith(
- { id: 'session-1', content: 'Say hello', turnId: 'turn-1' },
- expect.any(AbortSignal),
- );
- expect(transcript.snapshot()).toEqual([
- expect.objectContaining({
- id: 'user:turn-1',
- kind: 'user',
- status: 'succeeded',
- content: 'Say hello',
- }),
- expect.objectContaining({
- id: 'assistant:turn-1',
- kind: 'assistant',
- status: 'succeeded',
- content: 'Hello',
- }),
- expect.objectContaining({
- id: 'turn-duration:turn-1',
- kind: 'turn-duration',
- status: 'succeeded',
- }),
- ]);
- expect(controller.snapshot()).toMatchObject({
- status: 'idle',
- session: { sessionId: 'session-1' },
- });
- });
- it('refreshes the authoritative interaction mode after an accepted direct Turn', async () => {
- const runtime = {
- createSession: vi.fn(async () => ({
- sessionId: 'session-plan-sync',
- workspaceDir: '/workspace',
- })),
- getSession: vi.fn(async () => ({
- sessionId: 'session-plan-sync',
- workspaceDir: '/workspace',
- interactionMode: 'plan' as const,
- })),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield { type: 'done', turnId: 'turn-plan-sync' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const controller = new TuiChatController({
- runtime,
- transcript: new TranscriptStore(),
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-plan-sync',
- });
- await expect(controller.submit('Let the Agent decide whether to plan')).resolves.toBe(
- 'succeeded',
- );
- expect(runtime.getSession).toHaveBeenCalledWith('session-plan-sync');
- expect(controller.snapshot().session?.interactionMode).toBe('plan');
- });
- it('records content review rewind as failed even after assistant output', async () => {
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-blocked' })),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield {
- type: 'message',
- message: {
- id: 'message-before-block',
- turnId: 'turn-blocked',
- role: 'assistant',
- content: 'Partial response before the block.',
- },
- };
- yield {
- type: 'generic',
- eventType: 'messages-rewound',
- data: {},
- } as TuiStreamEvent;
- }),
- abortSession: vi.fn(async () => true),
- };
- const controller = new TuiChatController({
- runtime,
- transcript: new TranscriptStore(),
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-blocked',
- });
- await expect(controller.submit('Stop for input')).resolves.toBe('failed');
- expect(controller.snapshot().lastSettledTurn).toEqual({
- sessionId: 'session-blocked',
- turnId: 'turn-blocked',
- status: 'failed',
- });
- });
- it('records every Runtime-owned terminal outcome against its current Session', async () => {
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-runtime-owned' })),
- };
- const controller = new TuiChatController({
- runtime,
- transcript: new TranscriptStore(),
- workspaceDir: '/workspace',
- });
- await controller.ensureSession();
- for (const [turnId, status] of [
- ['turn-runtime-done', 'succeeded'],
- ['turn-runtime-fail', 'failed'],
- ['turn-runtime-blocked', 'blocked'],
- ['turn-runtime-cancel', 'cancelled'],
- ] as const) {
- controller.beginRuntimeTurn(turnId, 0);
- await controller.runtimeTurnSettlement.settle('session-runtime-owned', turnId, status, 12);
- expect(controller.snapshot().lastSettledTurn).toEqual({
- sessionId: 'session-runtime-owned',
- turnId,
- status,
- });
- }
- await controller.runtimeTurnSettlement.settle('session-other', 'turn-foreign', 'succeeded', 12);
- expect(controller.snapshot().lastSettledTurn).toEqual({
- sessionId: 'session-runtime-owned',
- turnId: 'turn-runtime-cancel',
- status: 'cancelled',
- });
- });
- it('writes a Runtime-owned result before publishing its terminal outcome', async () => {
- const delivered = Promise.withResolvers<void>();
- const writeAutomationResult = vi.fn(async () => delivered.promise);
- const transcript = new TranscriptStore();
- transcript.upsert({
- id: 'assistant:turn-previous',
- kind: 'assistant',
- status: 'succeeded',
- content: 'Previous answer',
- turnId: 'turn-previous',
- createdAtMs: 1,
- updatedAtMs: 1,
- });
- const controller = new TuiChatController({
- runtime: {
- createSession: vi.fn(async () => ({ sessionId: 'session-runtime-result' })),
- },
- transcript,
- workspaceDir: '/workspace',
- writeAutomationResult,
- });
- await controller.ensureSession();
- controller.beginRuntimeTurn('turn-runtime-result', 10);
- controller.applyRuntimeTurnEvent('turn-runtime-result', {
- type: 'message',
- message: {
- id: 'message-runtime-result',
- turnId: 'turn-runtime-result',
- role: 'assistant',
- content: 'Runtime-owned answer',
- },
- });
- controller.runtimeTurnSettlement.settleProjection('turn-runtime-result', 'succeeded', 20);
- const settledTurn = controller.runtimeTurnSettlement.prepare(
- 'session-runtime-result',
- 'turn-runtime-result',
- 'succeeded',
- );
- expect(settledTurn).toBeDefined();
- if (!settledTurn) throw new Error('Expected a Runtime-owned settled turn.');
- const publishing = controller.runtimeTurnSettlement.publish(settledTurn, 20);
- await vi.waitFor(() => expect(writeAutomationResult).toHaveBeenCalledOnce());
- expect(writeAutomationResult).toHaveBeenCalledWith(
- expect.objectContaining({
- sessionId: 'session-runtime-result',
- turnId: 'turn-runtime-result',
- status: 'succeeded',
- answer: 'Runtime-owned answer',
- durationMs: 20,
- }),
- );
- expect(controller.snapshot().lastSettledTurn).toBeUndefined();
- delivered.resolve();
- await publishing;
- expect(controller.snapshot().lastSettledTurn).toEqual({
- sessionId: 'session-runtime-result',
- turnId: 'turn-runtime-result',
- status: 'succeeded',
- });
- });
- it('fails a Runtime-owned automation turn that has no final assistant answer', async () => {
- const writeAutomationResult = vi.fn();
- const controller = new TuiChatController({
- runtime: {
- createSession: vi.fn(async () => ({ sessionId: 'session-runtime-empty' })),
- },
- transcript: new TranscriptStore(),
- workspaceDir: '/workspace',
- writeAutomationResult,
- });
- await controller.ensureSession();
- controller.beginRuntimeTurn('turn-runtime-empty', 10);
- controller.runtimeTurnSettlement.settleProjection('turn-runtime-empty', 'succeeded', 20);
- const settledTurn = controller.runtimeTurnSettlement.prepare(
- 'session-runtime-empty',
- 'turn-runtime-empty',
- 'succeeded',
- );
- if (!settledTurn) throw new Error('Expected a Runtime-owned settled turn.');
- await controller.runtimeTurnSettlement.publish(settledTurn, 20);
- expect(writeAutomationResult).toHaveBeenCalledWith(
- expect.objectContaining({
- status: 'failed',
- answer: null,
- error: expect.objectContaining({ code: 'EMPTY_RESPONSE' }),
- }),
- );
- expect(controller.snapshot().lastSettledTurn).toEqual({
- sessionId: 'session-runtime-empty',
- turnId: 'turn-runtime-empty',
- status: 'failed',
- });
- });
- it('does not publish a terminal turn before the enabled automation result is written', async () => {
- const delivered = Promise.withResolvers<void>();
- const writeAutomationResult = vi.fn(async () => delivered.promise);
- const onAutomationResultPublished = vi.fn();
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-automation-result' })),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield {
- type: 'message',
- message: { role: 'assistant', content: 'structured answer' },
- };
- yield { type: 'done' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const controller = new TuiChatController({
- runtime,
- transcript: new TranscriptStore(),
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-automation-result',
- writeAutomationResult,
- onAutomationResultPublished,
- });
- const submission = controller.submit('Answer');
- await vi.waitFor(() => expect(writeAutomationResult).toHaveBeenCalledOnce());
- expect(onAutomationResultPublished).not.toHaveBeenCalled();
- expect(controller.snapshot()).toMatchObject({
- status: 'running',
- activeTurnId: 'turn-automation-result',
- lastSettledTurn: undefined,
- });
- delivered.resolve();
- await expect(submission).resolves.toBe('succeeded');
- expect(onAutomationResultPublished).toHaveBeenCalledWith(
- expect.objectContaining({
- sessionId: 'session-automation-result',
- turnId: 'turn-automation-result',
- status: 'succeeded',
- }),
- );
- expect(controller.snapshot().lastSettledTurn).toEqual({
- sessionId: 'session-automation-result',
- turnId: 'turn-automation-result',
- status: 'succeeded',
- });
- });
- it.each([
- ['local_session_busy', 'queue-required'],
- ['local_session_compacting', 'queue-required'],
- ['local_session_queue_paused', 'draft-kept'],
- ] as const)('does not publish unaccepted %s as a terminal result', async (code, status) => {
- const writeAutomationResult = vi.fn();
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-queue-fallback' })),
- sendMessage: vi.fn(async function* rejectedAdmission(): AsyncGenerator<TuiStreamEvent> {
- yield* [];
- throw new TuiFailure('runtime', 'Session is busy', {
- code,
- retryable: false,
- });
- }),
- abortSession: vi.fn(async () => true),
- };
- const controller = new TuiChatController({
- runtime,
- transcript: new TranscriptStore(),
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-queue-fallback',
- writeAutomationResult,
- });
- await expect(controller.submit('Queue this', { onQueuePaused: async () => 'cancel' })).resolves.toBe(status);
- expect(writeAutomationResult).not.toHaveBeenCalled();
- expect(runtime.sendMessage).toHaveBeenCalledOnce();
- expect(controller.snapshot().lastSettledTurn).toBeUndefined();
- expect(controller.snapshot()).toMatchObject({
- status: 'idle',
- activeTurnId: undefined,
- error: undefined,
- });
- });
- it('retains a rejected paused recovery instead of silently enqueuing the new instruction', async () => {
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-recovery-race' })),
- sendMessage: vi.fn<TuiRunRuntime['sendMessage']>(async function* (_request, _signal, options) {
- yield* [];
- throw new TuiFailure('runtime', 'Admission rejected', {
- code: options ? 'local_session_busy' : 'local_session_queue_paused',
- retryable: false,
- });
- }),
- abortSession: vi.fn(async () => true),
- };
- const controller = new TuiChatController({ runtime, transcript: new TranscriptStore(), workspaceDir: '/workspace' });
- await expect(controller.submit('Enter Plan first', {
- clientIntent: 'plan-entry', onQueuePaused: async () => 'paused-queue-keep',
- })).resolves.toBe('failed');
- expect(runtime.sendMessage).toHaveBeenCalledTimes(2);
- expect(runtime.sendMessage.mock.calls[1]?.[0]).toMatchObject({ clientIntent: 'plan-entry' });
- });
- it('keeps submission serialized until authoritative Session metadata is refreshed', async () => {
- let resolveMetadata: ((session: { sessionId: string }) => void) | undefined;
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-metadata-fence' })),
- getSession: vi.fn(
- async () =>
- await new Promise<{ sessionId: string }>((resolve) => {
- resolveMetadata = resolve;
- }),
- ),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield { type: 'done', turnId: 'turn-metadata-fence' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const controller = new TuiChatController({
- runtime,
- transcript: new TranscriptStore(),
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-metadata-fence',
- });
- const first = controller.submit('First');
- await vi.waitFor(() => expect(resolveMetadata).toBeTypeOf('function'));
- await expect(controller.submit('Second')).rejects.toThrow('already running');
- resolveMetadata?.({ sessionId: 'session-metadata-fence' });
- await expect(first).resolves.toBe('succeeded');
- });
- it('keeps pre-token waiting state out of the durable Transcript', async () => {
- let releaseFirstEvent: (() => void) | undefined;
- const firstEventGate = new Promise<void>((resolve) => {
- releaseFirstEvent = resolve;
- });
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-thinking-placeholder' })),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- await firstEventGate;
- yield {
- type: 'delta',
- turnId: 'turn-thinking-placeholder',
- content: 'Direct answer',
- };
- yield { type: 'done', turnId: 'turn-thinking-placeholder' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-thinking-placeholder',
- now: () => 100,
- });
- const submission = controller.submit('Answer directly');
- await vi.waitFor(() =>
- expect(transcript.snapshot()).toEqual([
- expect.objectContaining({ kind: 'user', content: 'Answer directly' }),
- ]),
- );
- releaseFirstEvent?.();
- await submission;
- expect(transcript.snapshot()).toEqual([
- expect.objectContaining({ kind: 'user', content: 'Answer directly' }),
- expect.objectContaining({
- id: 'assistant:turn-thinking-placeholder',
- kind: 'assistant',
- status: 'succeeded',
- content: 'Direct answer',
- }),
- expect.objectContaining({
- id: 'turn-duration:turn-thinking-placeholder',
- kind: 'turn-duration',
- status: 'succeeded',
- }),
- ]);
- });
- it('preserves thinking, preamble, tool, and final-answer chronology across one turn', async () => {
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-segmented' })),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield { type: 'delta', turnId: 'turn-segmented', thinking: 'Plan the inspection' };
- yield {
- type: 'delta',
- turnId: 'turn-segmented',
- messageId: 'message-preamble',
- content: 'I will inspect the workspace.',
- };
- yield {
- type: 'delta',
- turnId: 'turn-segmented',
- toolCalls: [
- {
- id: 'tool-read',
- name: 'read',
- status: 'running',
- input: { path: 'README.md' },
- },
- ],
- };
- yield {
- type: 'delta',
- turnId: 'turn-segmented',
- toolCalls: [
- {
- id: 'tool-read',
- name: 'read',
- status: 'completed',
- input: { path: 'README.md' },
- output: 'workspace details',
- },
- ],
- };
- yield { type: 'delta', turnId: 'turn-segmented', thinking: 'Interpret the result' };
- yield {
- type: 'delta',
- turnId: 'turn-segmented',
- messageId: 'message-final',
- content: 'Here is the conclusion.',
- };
- yield { type: 'done', turnId: 'turn-segmented' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-segmented',
- now: (() => {
- let value = 100;
- return () => value++;
- })(),
- });
- await controller.submit('Inspect');
- expect(transcript.snapshot()).toEqual([
- expect.objectContaining({
- id: 'user:turn-segmented',
- kind: 'user',
- content: 'Inspect',
- }),
- expect.objectContaining({
- id: 'thinking:turn-segmented',
- kind: 'thinking',
- status: 'succeeded',
- content: 'Plan the inspection',
- }),
- expect.objectContaining({
- id: 'assistant:turn-segmented',
- kind: 'assistant-preamble',
- status: 'succeeded',
- content: 'I will inspect the workspace.',
- }),
- expect.objectContaining({
- id: 'tool:turn-segmented:tool-read',
- kind: 'tool',
- status: 'succeeded',
- content: '{"path":"README.md"}',
- detail: 'workspace details',
- }),
- expect.objectContaining({
- id: 'thinking:turn-segmented:2',
- kind: 'thinking',
- status: 'succeeded',
- content: 'Interpret the result',
- }),
- expect.objectContaining({
- id: 'assistant:turn-segmented:2',
- kind: 'assistant',
- status: 'succeeded',
- content: 'Here is the conclusion.',
- }),
- expect.objectContaining({
- id: 'turn-duration:turn-segmented',
- kind: 'turn-duration',
- status: 'succeeded',
- }),
- ]);
- });
- it('does not reactivate an earlier preamble when its completed message arrives after a tool', async () => {
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-delayed-completion' })),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield {
- type: 'delta',
- turnId: 'turn-delayed-completion',
- messageId: 'message-preamble',
- content: 'I will inspect this.',
- };
- yield {
- type: 'delta',
- turnId: 'turn-delayed-completion',
- toolCalls: [{ id: 'tool-read', name: 'read', status: 'completed' }],
- };
- yield {
- type: 'message',
- message: {
- id: 'message-preamble',
- turnId: 'turn-delayed-completion',
- role: 'assistant',
- content: 'I will inspect this.',
- },
- };
- yield {
- type: 'delta',
- turnId: 'turn-delayed-completion',
- messageId: 'message-final',
- content: 'The inspection is complete.',
- };
- yield { type: 'done', turnId: 'turn-delayed-completion' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-delayed-completion',
- now: () => 100,
- });
- await controller.submit('Inspect');
- expect(
- transcript
- .snapshot()
- .filter((cell) => cell.kind === 'assistant' || cell.kind === 'assistant-preamble'),
- ).toEqual([
- expect.objectContaining({
- id: 'assistant:turn-delayed-completion',
- kind: 'assistant-preamble',
- content: 'I will inspect this.',
- }),
- expect.objectContaining({
- id: 'assistant:turn-delayed-completion:2',
- kind: 'assistant',
- content: 'The inspection is complete.',
- }),
- ]);
- });
- it('uses authoritative Thinking duration from the completed message lifecycle', async () => {
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-thinking-duration' })),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield {
- type: 'delta',
- turnId: 'turn-thinking-duration',
- messageId: 'message-1',
- thinking: 'Inspect carefully',
- };
- yield {
- type: 'message',
- message: {
- id: 'message-1',
- turnId: 'turn-thinking-duration',
- role: 'assistant',
- kind: 'final',
- thinking: 'Inspect carefully',
- thinkingDurationMs: 4_200,
- content: 'Done.',
- },
- };
- yield { type: 'done', turnId: 'turn-thinking-duration' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-thinking-duration',
- now: () => 100,
- });
- await controller.submit('Inspect');
- expect(transcript.get('thinking:turn-thinking-duration')).toMatchObject({
- status: 'succeeded',
- durationMs: 4_200,
- content: 'Inspect carefully',
- });
- });
- it('restores aggregate Thinking before text when its empty start placeholder was removed', async () => {
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-late-thinking-snapshot' })),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield {
- type: 'delta',
- turnId: 'turn-late-thinking-snapshot',
- messageId: 'message-1',
- started: true,
- };
- yield {
- type: 'delta',
- turnId: 'turn-late-thinking-snapshot',
- messageId: 'message-1',
- content: 'Direct answer',
- };
- yield {
- type: 'message',
- message: {
- id: 'message-1',
- turnId: 'turn-late-thinking-snapshot',
- role: 'assistant',
- kind: 'final',
- thinking: 'Late aggregate snapshot',
- thinkingDurationMs: 900,
- content: 'Direct answer',
- },
- };
- yield { type: 'done', turnId: 'turn-late-thinking-snapshot' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-late-thinking-snapshot',
- now: () => 100,
- });
- await expect(controller.submit('Answer')).resolves.toBe('succeeded');
- expect(transcript.snapshot()).toEqual([
- expect.objectContaining({ kind: 'user' }),
- expect.objectContaining({
- kind: 'thinking',
- status: 'succeeded',
- content: 'Late aggregate snapshot',
- durationMs: 900,
- }),
- expect.objectContaining({ kind: 'assistant', content: 'Direct answer' }),
- expect.objectContaining({ kind: 'turn-duration', status: 'succeeded' }),
- ]);
- });
- it('opens a new ordered Thinking segment when a real delta follows direct text', async () => {
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-late-thinking-delta' })),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield {
- type: 'delta',
- turnId: 'turn-late-thinking-delta',
- messageId: 'message-1',
- started: true,
- };
- yield {
- type: 'delta',
- turnId: 'turn-late-thinking-delta',
- messageId: 'message-1',
- content: 'First text',
- };
- yield {
- type: 'delta',
- turnId: 'turn-late-thinking-delta',
- messageId: 'message-1',
- thinking: 'Reconsider',
- };
- yield {
- type: 'delta',
- turnId: 'turn-late-thinking-delta',
- messageId: 'message-2',
- content: 'Final text',
- };
- yield { type: 'done', turnId: 'turn-late-thinking-delta' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-late-thinking-delta',
- now: () => 100,
- });
- await expect(controller.submit('Reconsider')).resolves.toBe('succeeded');
- expect(transcript.snapshot().map((cell) => cell.kind)).toEqual([
- 'user',
- 'assistant-preamble',
- 'thinking',
- 'assistant',
- 'turn-duration',
- ]);
- });
- it('appends partial tool updates without duplicating cumulative snapshots', async () => {
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-tool-progress' })),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield {
- type: 'delta',
- turnId: 'turn-tool-progress',
- toolCalls: [
- {
- id: 'tool-bash',
- name: 'bash',
- status: 'started',
- input: { command: 'pnpm test' },
- },
- ],
- };
- yield {
- type: 'delta',
- turnId: 'turn-tool-progress',
- toolCalls: [{ id: 'tool-bash', name: 'bash', status: 'running', output: 'first\n' }],
- };
- yield {
- type: 'delta',
- turnId: 'turn-tool-progress',
- toolCalls: [
- {
- id: 'tool-bash',
- name: 'bash',
- status: 'running',
- output: 'first\nsecond\n',
- },
- ],
- };
- yield {
- type: 'delta',
- turnId: 'turn-tool-progress',
- toolCalls: [
- {
- id: 'tool-bash',
- name: 'bash',
- status: 'completed',
- output: 'first\nsecond\npassed',
- },
- ],
- };
- yield { type: 'done', turnId: 'turn-tool-progress' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-tool-progress',
- now: () => 100,
- });
- await controller.submit('Test');
- expect(transcript.get('tool:turn-tool-progress:tool-bash')).toMatchObject({
- status: 'succeeded',
- content: '{"command":"pnpm test"}',
- detail: 'first\nsecond\npassed',
- });
- });
- it('places a final reply after tool activity when tool events arrive first', async () => {
- let releaseReply: (() => void) | undefined;
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-ordered' })),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield {
- type: 'delta',
- turnId: 'turn-ordered',
- thinking: 'Inspecting files',
- toolCalls: [
- {
- id: 'read-a',
- name: 'read',
- status: 'completed',
- input: { path: 'src/a.ts' },
- },
- {
- id: 'read-b',
- name: 'read',
- status: 'completed',
- input: { path: 'src/b.ts' },
- },
- ],
- };
- await new Promise<void>((resolve) => {
- releaseReply = resolve;
- });
- yield { type: 'delta', turnId: 'turn-ordered', content: 'Finished.' };
- yield { type: 'done', turnId: 'turn-ordered' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-ordered',
- now: () => 100,
- });
- const submission = controller.submit('Inspect');
- await vi.waitFor(() =>
- expect(transcript.snapshot().map((cell) => cell.kind)).toEqual([
- 'user',
- 'thinking',
- 'tool',
- 'tool',
- ]),
- );
- releaseReply?.();
- await submission;
- expect(transcript.snapshot().map((cell) => cell.kind)).toEqual([
- 'user',
- 'thinking',
- 'tool',
- 'tool',
- 'assistant',
- 'turn-duration',
- ]);
- expect(transcript.snapshot().at(-2)).toMatchObject({
- kind: 'assistant',
- content: 'Finished.',
- status: 'succeeded',
- });
- });
- it('does not create an empty assistant row for a successful turn with no text', async () => {
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-no-text' })),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield { type: 'done', turnId: 'turn-no-text' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-no-text',
- now: () => 100,
- });
- await controller.submit('Run');
- expect(transcript.snapshot().map((cell) => cell.kind)).toEqual(['user', 'turn-duration']);
- });
- it('does not create a thinking row for whitespace-only runtime deltas or history', async () => {
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-empty-thinking' })),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield { type: 'delta', turnId: 'turn-empty-thinking', thinking: ' \n\t' };
- yield {
- type: 'message',
- turnId: 'turn-empty-thinking',
- message: {
- role: 'assistant',
- thinking: ' ',
- },
- };
- yield { type: 'done', turnId: 'turn-empty-thinking' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-empty-thinking',
- now: () => 100,
- });
- await controller.submit('Run');
- expect(transcript.snapshot().map((cell) => cell.kind)).toEqual(['user', 'turn-duration']);
- });
- it('submits attachment-only turns and keeps attachment metadata in the user transcript', async () => {
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-attachment' })),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield { type: 'done' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-attachment',
- now: () => 100,
- });
- const attachments = [
- {
- type: 'image' as const,
- filePath: '/workspace/diagram.png',
- fileName: 'diagram.png',
- mimeType: 'image/png',
- sizeBytes: 2048,
- },
- ];
- await controller.submit('', { attachments });
- expect(runtime.sendMessage).toHaveBeenCalledWith(
- {
- id: 'session-attachment',
- content: '',
- turnId: 'turn-attachment',
- attachments: [
- {
- local: { filePath: '/workspace/diagram.png' },
- meta: {
- attachmentType: 'image',
- fileName: 'diagram.png',
- mimeType: 'image/png',
- sizeBytes: 2048,
- },
- },
- ],
- },
- expect.any(AbortSignal),
- );
- expect(transcript.get('user:turn-attachment')).toMatchObject({
- content: '',
- attachments: [
- {
- type: 'image',
- fileName: 'diagram.png',
- mimeType: 'image/png',
- sizeBytes: 2048,
- filePath: '/workspace/diagram.png',
- },
- ],
- });
- });
- it('reconciles a pasted image with a Runtime echo that omits its local size', async () => {
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-image-echo' })),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield {
- type: 'message',
- message: {
- id: 'message-image-echo',
- turnId: 'turn-image-echo',
- role: 'user',
- content: 'Inspect this image',
- attachments: [
- {
- type: 'image',
- fileName: 'clipboard.png',
- mimeType: 'image/png',
- },
- ],
- },
- };
- yield { type: 'done', turnId: 'turn-image-echo' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-image-echo',
- now: () => 100,
- });
- await controller.submit('Inspect this image', {
- attachments: [
- {
- type: 'image',
- filePath: '/tmp/clipboard.png',
- fileName: 'clipboard.png',
- mimeType: 'image/png',
- sizeBytes: 2_048,
- },
- ],
- });
- const userCells = transcript.snapshot().filter((cell) => cell.kind === 'user');
- expect(userCells).toHaveLength(1);
- expect(userCells[0]).toMatchObject({
- id: 'user:turn-image-echo',
- sourceMessageId: 'message-image-echo',
- status: 'succeeded',
- attachments: [expect.objectContaining({ sizeBytes: 2_048 })],
- });
- const rendered = new TranscriptView(() => transcript.snapshot()).render(80).join('\n');
- expect(rendered.match(/Image {2}clipboard\.png/gu)).toHaveLength(1);
- expect(rendered.match(/2 KB/gu)).toHaveLength(1);
- expect(rendered.match(/Inspect this image/gu)).toHaveLength(1);
- });
- it('renders thinking, tools, and a useful runtime error without losing partial output', async () => {
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-2' })),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield { type: 'delta', turnId: 'turn-2', thinking: 'Inspecting files' };
- yield {
- type: 'delta',
- turnId: 'turn-2',
- content: 'Partial answer',
- toolCalls: [{ id: 'tool-1', name: 'read', status: 'running' }],
- };
- yield { type: 'error', turnId: 'turn-2', message: 'provider unavailable' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-2',
- now: () => 100,
- });
- await controller.submit('Inspect');
- expect(transcript.snapshot()).toEqual(
- expect.arrayContaining([
- expect.objectContaining({
- id: 'thinking:turn-2',
- kind: 'thinking',
- content: 'Inspecting files',
- }),
- expect.objectContaining({
- id: 'tool:turn-2:tool-1',
- kind: 'tool',
- status: 'failed',
- title: 'read',
- }),
- expect.objectContaining({
- id: 'assistant:turn-2',
- kind: 'assistant-preamble',
- status: 'succeeded',
- content: 'Partial answer',
- }),
- expect.objectContaining({
- id: 'error:turn-2',
- kind: 'error',
- content:
- 'The model provider is temporarily unavailable.\nRun /retry to resend your last message. Your prompt is preserved.',
- }),
- ]),
- );
- expect(transcript.snapshot()).not.toEqual(
- expect.arrayContaining([expect.objectContaining({ content: 'provider unavailable' })]),
- );
- expect(controller.snapshot().status).toBe('error');
- });
- it('preserves the submitted message and renders an error when session creation fails', async () => {
- const runtime = {
- createSession: vi.fn(async () => {
- throw new Error('authentication required');
- }),
- sendMessage: vi.fn(),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-auth',
- now: () => 100,
- });
- await expect(controller.submit('Hello')).resolves.toBe('failed');
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- expect(transcript.snapshot()).toEqual([
- expect.objectContaining({
- id: 'user:turn-auth',
- kind: 'user',
- status: 'failed',
- content: 'Hello',
- }),
- expect.objectContaining({
- id: 'assistant:turn-auth',
- kind: 'assistant',
- status: 'failed',
- }),
- expect.objectContaining({
- id: 'error:turn-auth',
- kind: 'error',
- content:
- 'The service rejected request authentication.\nIf the problem persists, report this error and its code. Your prompt is preserved.',
- }),
- ]);
- expect(controller.snapshot()).toMatchObject({
- status: 'error',
- error:
- 'The service rejected request authentication.\nIf the problem persists, report this error and its code. Your prompt is preserved.',
- });
- });
- it('does not suggest retry when local-runtime marks a failure as non-retryable', async () => {
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-non-retryable' })),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield* [] as TuiStreamEvent[];
- throw Object.assign(new Error('provider unavailable'), { retryable: false });
- }),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-non-retryable',
- now: () => 100,
- });
- await expect(controller.submit('Unsupported request')).resolves.toBe('failed');
- expect(controller.snapshot()).toMatchObject({
- status: 'error',
- errorRetryable: false,
- });
- expect(controller.snapshot().error).not.toContain('/retry');
- expect(transcript.get('error:turn-non-retryable')?.content).not.toContain('/retry');
- });
- it('maps local-runtime numeric tool statuses to terminal states', async () => {
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-tools' })),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield {
- type: 'delta',
- toolCalls: [
- { id: 'tool-finished', name: 'read', status: 2 },
- { id: 'tool-failed', name: 'bash', status: 3 },
- ],
- };
- yield { type: 'done' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-tools',
- now: () => 100,
- });
- await controller.submit('Run tools');
- expect(transcript.get('tool:turn-tools:tool-finished')).toMatchObject({
- status: 'succeeded',
- });
- expect(transcript.get('tool:turn-tools:tool-failed')).toMatchObject({
- status: 'failed',
- });
- });
- it('routes stop through local-runtime and marks the turn cancelled', async () => {
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-3' })),
- sendMessage: vi.fn(async function* sendMessage(
- _req: SendMessageReq,
- signal?: AbortSignal,
- ): AsyncGenerator<TuiStreamEvent> {
- await new Promise<void>((resolve) => {
- signal?.addEventListener('abort', () => resolve(), { once: true });
- });
- yield { type: 'done', turnId: 'turn-3' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-3',
- now: () => 100,
- });
- const submitting = controller.submit('Wait');
- await vi.waitFor(() => expect(controller.snapshot().status).toBe('running'));
- await expect(controller.abort()).resolves.toBe(true);
- await submitting;
- expect(runtime.abortSession).toHaveBeenCalledWith({
- id: 'session-3',
- turnId: 'turn-3',
- reason: 'user_stop',
- });
- expect(transcript.get('assistant:turn-3')).toMatchObject({ status: 'cancelled' });
- expect(controller.snapshot().status).toBe('idle');
- });
- it('cancels a turn while its first session is still being created', async () => {
- let finishSessionCreation:
- | ((session: { sessionId: string; workspaceDir: string }) => void)
- | undefined;
- const runtime = {
- createSession: vi.fn(
- async () =>
- await new Promise<{ sessionId: string; workspaceDir: string }>((resolve) => {
- finishSessionCreation = resolve;
- }),
- ),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield { type: 'done' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-creating-session',
- now: () => 100,
- });
- const submitting = controller.submit('Wait for the session');
- await vi.waitFor(() =>
- expect(controller.snapshot()).toMatchObject({
- status: 'starting',
- activeTurnId: 'turn-creating-session',
- }),
- );
- await expect(controller.abort()).resolves.toBe(true);
- finishSessionCreation?.({ sessionId: 'session-created', workspaceDir: '/workspace' });
- await expect(submitting).resolves.toBe('cancelled');
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- expect(runtime.abortSession).not.toHaveBeenCalled();
- expect(transcript.get('assistant:turn-creating-session')).toMatchObject({
- status: 'cancelled',
- });
- expect(controller.snapshot()).toMatchObject({
- status: 'idle',
- activeTurnId: undefined,
- });
- expect(controller.snapshot().session).toBeUndefined();
- });
- it('retries Session creation after cancellation and ignores the stale result', async () => {
- const resolvers: Array<(session: { sessionId: string; workspaceDir: string }) => void> = [];
- const runtime = {
- createSession: vi.fn(
- async () =>
- await new Promise<{ sessionId: string; workspaceDir: string }>((resolve) => {
- resolvers.push(resolve);
- }),
- ),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield { type: 'done' };
- }),
- abortSession: vi.fn(async () => true),
- };
- let turnSequence = 0;
- const controller = new TuiChatController({
- runtime,
- transcript: new TranscriptStore(),
- workspaceDir: '/workspace',
- createTurnId: () => `turn-session-retry-${++turnSequence}`,
- now: () => 100,
- });
- const first = controller.submit('First attempt');
- await vi.waitFor(() => expect(resolvers).toHaveLength(1));
- await expect(controller.abort()).resolves.toBe(true);
- await expect(first).resolves.toBe('cancelled');
- const second = controller.submit('Second attempt');
- await vi.waitFor(() => expect(resolvers).toHaveLength(2));
- resolvers[1]?.({ sessionId: 'session-current', workspaceDir: '/workspace' });
- await expect(second).resolves.toBe('succeeded');
- resolvers[0]?.({ sessionId: 'session-stale', workspaceDir: '/workspace' });
- await Promise.resolve();
- expect(runtime.createSession).toHaveBeenCalledTimes(2);
- expect(runtime.sendMessage).toHaveBeenCalledWith(
- {
- id: 'session-current',
- content: 'Second attempt',
- turnId: 'turn-session-retry-2',
- },
- expect.any(AbortSignal),
- );
- expect(controller.snapshot().session?.sessionId).toBe('session-current');
- });
- it('stops the local stream even when the remote abort request fails', async () => {
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-4' })),
- sendMessage: vi.fn(async function* sendMessage(
- _req: SendMessageReq,
- signal?: AbortSignal,
- ): AsyncGenerator<TuiStreamEvent> {
- await new Promise<void>((resolve) => {
- signal?.addEventListener('abort', () => resolve(), { once: true });
- });
- yield { type: 'done', turnId: 'turn-4' };
- }),
- abortSession: vi.fn(async () => {
- throw new Error('abort endpoint unavailable');
- }),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-4',
- now: () => 100,
- });
- const submitting = controller.submit('Wait');
- await vi.waitFor(() => expect(controller.snapshot().status).toBe('running'));
- await expect(controller.abort()).resolves.toBe(false);
- await submitting;
- expect(transcript.get('assistant:turn-4')).toMatchObject({ status: 'cancelled' });
- expect(controller.snapshot().status).toBe('idle');
- });
- it('keeps the controller editable but blocks a new turn until cancellation retires', async () => {
- let sequence = 0;
- let releaseCancelledStream: (() => void) | undefined;
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-hung-cancel' })),
- sendMessage: vi.fn(async function* sendMessage(
- req: SendMessageReq,
- signal?: AbortSignal,
- ): AsyncGenerator<TuiStreamEvent> {
- if (req.content === 'First') {
- await new Promise<void>((resolve) => {
- signal?.addEventListener('abort', resolve, { once: true });
- });
- await new Promise<void>((resolve) => {
- releaseCancelledStream = resolve;
- });
- }
- yield { type: 'delta', content: 'Recovered' };
- yield { type: 'done' };
- }),
- abortSession: vi.fn(async () => true),
- steer: vi.fn(),
- };
- const transcript = new TranscriptStore();
- const controller = new ProductionTuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- createTurnId: () => `turn-${++sequence}`,
- retirementWarningTimeoutMs: 5,
- runCoordinator: new TuiRunCoordinator(runtime, {
- cancellationSettlementTimeoutMs: 10,
- }),
- });
- void controller.submit('First');
- await vi.waitFor(() => expect(controller.snapshot().status).toBe('running'));
- await expect(controller.abort()).resolves.toBe(true);
- expect(controller.snapshot()).toMatchObject({
- status: 'idle',
- activeTurnId: undefined,
- cancelling: false,
- retiringTurnId: 'turn-1',
- });
- await expect(controller.submit('Second')).rejects.toThrow(
- 'The previous run is still stopping.',
- );
- await vi.waitFor(() => {
- expect(controller.snapshot()).toMatchObject({
- status: 'error',
- retiringTurnId: 'turn-1',
- error: expect.stringContaining('taking too long to stop'),
- });
- });
- releaseCancelledStream?.();
- await vi.waitFor(() => expect(controller.snapshot().retiringTurnId).toBeUndefined());
- expect(controller.snapshot()).toMatchObject({ status: 'idle', error: undefined });
- await expect(controller.submit('Second')).resolves.toBe('succeeded');
- expect(runtime.sendMessage).toHaveBeenCalledTimes(2);
- expect(transcript.get('assistant:turn-2')).toMatchObject({
- status: 'succeeded',
- content: 'Recovered',
- });
- });
- it('defers an automation cancel terminal until a slow result write retires', async () => {
- const resultWritten = Promise.withResolvers<void>();
- const writeAutomationResult = vi.fn(async () => resultWritten.promise);
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-slow-result-cancel' })),
- sendMessage: vi.fn(async function* sendMessage(
- _req: SendMessageReq,
- signal?: AbortSignal,
- ): AsyncGenerator<TuiStreamEvent> {
- await new Promise<void>((resolve) => {
- signal?.addEventListener('abort', resolve, { once: true });
- });
- yield { type: 'done' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const controller = new TuiChatController({
- runtime,
- transcript: new TranscriptStore(),
- workspaceDir: '/workspace',
- createTurnId: () => 'turn-slow-result-cancel',
- writeAutomationResult,
- runCoordinator: new TuiRunCoordinator(runtime, {
- cancellationSettlementTimeoutMs: 1,
- }),
- });
- const submitting = controller.submit('Wait');
- await vi.waitFor(() => expect(controller.snapshot().status).toBe('running'));
- await expect(controller.abort()).resolves.toBe(true);
- await vi.waitFor(() => expect(writeAutomationResult).toHaveBeenCalledOnce());
- expect(controller.snapshot()).toMatchObject({
- retiringTurnId: 'turn-slow-result-cancel',
- lastSettledTurn: undefined,
- });
- resultWritten.resolve();
- await submitting;
- await vi.waitFor(() =>
- expect(controller.snapshot().lastSettledTurn).toEqual({
- sessionId: 'session-slow-result-cancel',
- turnId: 'turn-slow-result-cancel',
- status: 'cancelled',
- }),
- );
- });
- it('loads Desktop sessions, hydrates history, and supports rename and archive', async () => {
- const runtime = {
- createSession: vi.fn(async (input: { parentSessionId?: string; title?: string }) => ({
- sessionId: 'session-fork',
- parentSessionId: input.parentSessionId,
- title: input.title,
- workspaceDir: '/workspace',
- })),
- listSessions: vi.fn(async () => [
- {
- sessionId: 'session-1',
- title: 'Existing',
- workspaceDir: '/workspace',
- updatedAt: 200,
- },
- ]),
- getSession: vi.fn(async () => ({
- sessionId: 'session-1',
- title: 'Existing',
- workspaceDir: '/workspace',
- })),
- getMessages: vi.fn(async () => [
- {
- id: 'message-user',
- turnId: 'turn-history',
- role: 'user' as const,
- content: 'Earlier question',
- attachments: [
- {
- type: 'file' as const,
- fileName: 'notes.md',
- mimeType: 'text/markdown',
- sizeBytes: 2048,
- },
- ],
- timestamp: 100,
- },
- {
- id: 'message-assistant',
- turnId: 'turn-history',
- role: 'assistant' as const,
- content: 'Earlier answer',
- toolCalls: [{ id: 'tool-history', name: 'read', status: 'completed' }],
- timestamp: 101,
- },
- ]),
- renameSession: vi.fn(async (_sessionId: string, title: string) => ({
- sessionId: 'session-1',
- title,
- workspaceDir: '/workspace',
- })),
- archiveSession: vi.fn(async () => undefined),
- getAccountStatus: vi.fn(async () => ({
- status: 'ready' as const,
- defaultModel: 'minimax/MiniMax-M2.7',
- providerId: 'minimax',
- authMode: 'managed-login',
- managedTokenPresent: true,
- warnings: [],
- })),
- sendMessage: vi.fn(),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- now: () => 300,
- });
- await controller.initialize();
- await controller.loadSessionProjection('session-1');
- expect(controller.snapshot()).toMatchObject({
- status: 'idle',
- session: { sessionId: 'session-1', title: 'Existing' },
- sessions: [expect.objectContaining({ sessionId: 'session-1' })],
- account: expect.objectContaining({
- status: 'ready',
- defaultModel: 'minimax/MiniMax-M2.7',
- }),
- });
- expect(transcript.snapshot()).toEqual([
- expect.objectContaining({
- kind: 'user',
- content: 'Earlier question',
- attachments: [
- {
- type: 'file',
- fileName: 'notes.md',
- mimeType: 'text/markdown',
- sizeBytes: 2048,
- },
- ],
- turnId: 'turn-history',
- }),
- expect.objectContaining({
- kind: 'assistant-preamble',
- content: 'Earlier answer',
- turnId: 'turn-history',
- }),
- expect.objectContaining({
- kind: 'tool',
- title: 'read',
- status: 'succeeded',
- }),
- ]);
- await controller.renameCurrentSession('Renamed');
- expect(controller.snapshot().session?.title).toBe('Renamed');
- await controller.runtimeTurnSettlement.settle('session-1', 'turn-archived', 'succeeded', 0);
- await controller.archiveCurrentSession();
- expect(runtime.archiveSession).toHaveBeenCalledWith('session-1', true);
- expect(controller.snapshot().session).toBeUndefined();
- expect(controller.snapshot().lastSettledTurn).toBeUndefined();
- });
- it('drops a stale Session resume when a newer Session finishes loading first', async () => {
- let resolveFirstMessages:
- | ((messages: Array<{ id: string; role: 'user'; content: string }>) => void)
- | undefined;
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-created' })),
- getSession: vi.fn(async (sessionId: string) => ({ sessionId, title: sessionId })),
- getMessages: vi.fn(async (sessionId: string) => {
- if (sessionId === 'session-a') {
- return await new Promise<Array<{ id: string; role: 'user'; content: string }>>(
- (resolve) => {
- resolveFirstMessages = resolve;
- },
- );
- }
- return [{ id: 'message-b', role: 'user' as const, content: 'History B' }];
- }),
- sendMessage: vi.fn(),
- abortSession: vi.fn(async () => true),
- steer: vi.fn(),
- };
- const transcript = new TranscriptStore();
- const controller = new ProductionTuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- });
- const firstResume = controller.loadSessionProjection('session-a');
- await vi.waitFor(() => expect(resolveFirstMessages).toBeTypeOf('function'));
- await controller.loadSessionProjection('session-b');
- resolveFirstMessages?.([{ id: 'message-a', role: 'user', content: 'History A' }]);
- await firstResume;
- expect(controller.snapshot()).toMatchObject({
- status: 'idle',
- session: { sessionId: 'session-b' },
- });
- expect(transcript.snapshot()).toEqual([
- expect.objectContaining({ kind: 'user', content: 'History B' }),
- ]);
- });
- it('keeps the latest Session catalog when refreshes resolve out of order', async () => {
- const sessionResolvers: Array<(sessions: Array<{ sessionId: string; title: string }>) => void> =
- [];
- const runtime = {
- listSessions: vi.fn(
- async () =>
- await new Promise<Array<{ sessionId: string; title: string }>>((resolve) => {
- sessionResolvers.push(resolve);
- }),
- ),
- };
- const controller = new ProductionTuiChatController({
- runtime: runtime as never,
- transcript: new TranscriptStore(),
- workspaceDir: '/workspace',
- });
- const olderRefresh = controller.refreshSessionList();
- const newerRefresh = controller.refreshSessionList();
- await vi.waitFor(() => expect(sessionResolvers).toHaveLength(2));
- sessionResolvers[1]?.([{ sessionId: 'session-new', title: 'New catalog' }]);
- await newerRefresh;
- sessionResolvers[0]?.([{ sessionId: 'session-old', title: 'Old catalog' }]);
- await olderRefresh;
- expect(controller.snapshot().sessions).toEqual([
- expect.objectContaining({ sessionId: 'session-new', title: 'New catalog' }),
- ]);
- });
- it('refreshes account quota after a completed turn', async () => {
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-meters' })),
- listSessions: vi.fn(async () => []),
- getAccountStatus: vi.fn(async (_sessionId, options) => ({
- status: 'ready' as const,
- defaultModel: 'minimax/MiniMax-M2.7',
- providerId: 'minimax',
- modelId: 'MiniMax-M2.7',
- authMode: 'managed-login',
- managedTokenPresent: true,
- modelSource: 'token-plan' as const,
- ...(options?.includeMembership === true
- ? {
- tokenPlanQuota: {
- fiveHour: { remainingPercent: 82, unlimited: false },
- weekly: { remainingPercent: 64, unlimited: false },
- },
- }
- : {}),
- warnings: [],
- })),
- sendMessage: vi.fn(async function* sendMessage(): AsyncGenerator<TuiStreamEvent> {
- yield { type: 'delta', content: 'Done' };
- yield { type: 'done' };
- }),
- abortSession: vi.fn(async () => true),
- };
- const controller = new TuiChatController({
- runtime,
- transcript: new TranscriptStore(),
- workspaceDir: '/workspace',
- });
- await controller.initialize();
- await controller.submit('Measure this turn');
- await vi.waitFor(() => {
- expect(controller.snapshot()).toMatchObject({
- account: {
- modelSource: 'token-plan',
- tokenPlanQuota: {
- fiveHour: { remainingPercent: 82 },
- weekly: { remainingPercent: 64 },
- },
- },
- });
- });
- expect(runtime.getAccountStatus).toHaveBeenCalledWith(undefined, {
- includeMembership: false,
- });
- expect(runtime.getAccountStatus).toHaveBeenCalledWith('session-meters', {
- includeMembership: false,
- });
- await vi.waitFor(() =>
- expect(runtime.getAccountStatus).toHaveBeenCalledWith('session-meters', {
- includeMembership: true,
- }),
- );
- });
- it('preserves ephemeral local cells in place during same-Session history reconciliation', async () => {
- let messages = [
- {
- id: 'message-1',
- turnId: 'turn-1',
- role: 'assistant' as const,
- content: 'First durable answer',
- },
- ];
- const runtime = {
- getSession: vi.fn(async (sessionId: string) => ({ sessionId })),
- getMessages: vi.fn(async () => messages),
- };
- const transcript = new TranscriptStore();
- const controller = new ProductionTuiChatController({
- runtime: runtime as never,
- transcript,
- workspaceDir: '/workspace',
- });
- await controller.loadSessionProjection('session-local-cells');
- transcript.upsert({
- id: 'local:status',
- kind: 'final-summary',
- status: 'succeeded',
- content: 'MCode status',
- ephemeral: true,
- createdAtMs: 100,
- updatedAtMs: 100,
- });
- messages = [
- {
- ...messages[0],
- id: 'message-1-reprojected',
- },
- {
- id: 'message-2',
- turnId: 'turn-2',
- role: 'assistant' as const,
- content: 'Second durable answer',
- },
- ];
- await controller.refreshCurrentSessionHistory();
- expect(transcript.snapshot().map((cell) => cell.content)).toEqual([
- 'First durable answer',
- 'MCode status',
- 'Second durable answer',
- ]);
- messages = [];
- await controller.loadSessionProjection('session-other');
- expect(transcript.snapshot()).toEqual([]);
- });
- it('restores thinking, tool evidence and Tasks while filtering durable protocol rows', async () => {
- const onTodoChange = vi.fn();
- const runtime = {
- createSession: vi.fn(),
- getSession: vi.fn(async () => ({ sessionId: 'session-resume' })),
- getMessages: vi.fn(async () => [
- {
- id: 'assistant-tool',
- turnId: 'turn-resume',
- role: 'assistant' as const,
- thinking: 'Inspect the history projection.',
- toolCalls: [
- {
- id: 'tool-grep',
- name: 'grep',
- status: 'completed',
- input: '{"pattern":"hydrateHistory","path":"packages/tui"}',
- output:
- '{"content":[{"type":"text","text":"packages/tui/src/a.ts:12:hydrateHistory"}]}',
- },
- ],
- },
- {
- id: 'todo-event',
- turnId: 'turn-resume',
- role: 'unknown' as const,
- content: JSON.stringify({
- eventType: 'todo_updated',
- todos: [{ content: 'Verify resume', status: 'completed', priority: 'high' }],
- }),
- },
- {
- id: 'todo-tool',
- turnId: 'turn-resume',
- role: 'assistant' as const,
- toolCalls: [{ id: 'todo-write', name: 'functions.TodoWrite', status: 'completed' }],
- },
- ]),
- sendMessage: vi.fn(),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- onTodoChange,
- });
- await controller.loadSessionProjection('session-resume');
- expect(transcript.snapshot()).toEqual([
- expect.objectContaining({
- kind: 'thinking',
- content: 'Inspect the history projection.',
- }),
- expect.objectContaining({
- kind: 'tool',
- title: 'grep',
- content: '{"pattern":"hydrateHistory","path":"packages/tui"}',
- detail: 'packages/tui/src/a.ts:12:hydrateHistory',
- }),
- ]);
- expect(onTodoChange).toHaveBeenLastCalledWith([
- { content: 'Verify resume', status: 'completed' },
- ]);
- const view = new TranscriptView(() => transcript.snapshot());
- const compact = view.render(80).join('\n');
- expect(compact).toContain('Thought');
- expect(compact).toContain('Searched (hydrateHistory in packages/tui)');
- expect(compact).not.toContain('Inspect the history projection.');
- expect(compact).not.toContain('packages/tui/src/a.ts:12:hydrateHistory');
- view.toggleDetailMode();
- const detailed = view.render(80).join('\n');
- expect(detailed).toContain('Inspect the history projection.');
- expect(detailed).toContain('Searched (hydrateHistory in packages/tui)');
- expect(detailed).toContain('packages/tui/src/a.ts:12:hydrateHistory');
- expect(detailed).not.toContain('todo_updated');
- expect(detailed).not.toContain('TodoWrite');
- });
- it('hydrates ordered message parts without flattening text and tools', async () => {
- const runtime = {
- createSession: vi.fn(),
- listSessions: vi.fn(async () => []),
- getSession: vi.fn(async () => ({ sessionId: 'session-parts' })),
- getMessages: vi.fn(async () => [
- {
- id: 'message-parts',
- turnId: 'turn-parts',
- role: 'assistant' as const,
- parts: [
- { id: 'thinking-1', type: 'thinking' as const, content: 'Plan' },
- { id: 'text-1', type: 'text' as const, content: 'I will inspect.' },
- {
- id: 'tool-1',
- type: 'tool' as const,
- toolCall: {
- id: 'call-1',
- name: 'read',
- status: 'completed',
- input: { path: 'README.md' },
- },
- },
- { id: 'thinking-2', type: 'thinking' as const, content: 'Interpret' },
- { id: 'text-2', type: 'text' as const, content: 'Done.' },
- ],
- },
- ]),
- sendMessage: vi.fn(),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- });
- await controller.loadSessionProjection('session-parts');
- expect(
- transcript.snapshot().map((cell) => ({
- kind: cell.kind,
- content: cell.content,
- title: cell.title,
- })),
- ).toEqual([
- { kind: 'thinking', content: 'Plan', title: undefined },
- { kind: 'assistant-preamble', content: 'I will inspect.', title: undefined },
- { kind: 'tool', content: '{"path":"README.md"}', title: 'read' },
- { kind: 'thinking', content: 'Interpret', title: undefined },
- { kind: 'assistant', content: 'Done.', title: undefined },
- ]);
- });
- it('keeps distinct durable assistant messages when one turn repeats the same text', async () => {
- const runtime = {
- createSession: vi.fn(),
- getSession: vi.fn(async () => ({ sessionId: 'session-repeated-text' })),
- getMessages: vi.fn(async () => [
- {
- id: 'assistant-first',
- turnId: 'turn-repeated-text',
- role: 'assistant' as const,
- content: 'Same answer',
- timestamp: 100,
- },
- {
- id: 'assistant-second',
- turnId: 'turn-repeated-text',
- role: 'assistant' as const,
- content: 'Same answer',
- timestamp: 101,
- },
- ]),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- });
- await controller.loadSessionProjection('session-repeated-text');
- expect(
- transcript
- .snapshot()
- .filter((cell) => cell.kind === 'assistant')
- .map((cell) => cell.id),
- ).toEqual(['history:assistant:assistant-first', 'history:assistant:assistant-second']);
- });
- it('projects questionnaire history as a readable receipt and hides internal Ask User protocol', async () => {
- const runtime = {
- createSession: vi.fn(async () => ({ sessionId: 'session-questionnaire' })),
- listSessions: vi.fn(async () => []),
- getSession: vi.fn(async () => ({ sessionId: 'session-questionnaire' })),
- getMessages: vi.fn(async () => [
- {
- id: 'ask-preamble',
- turnId: 'turn-ask',
- role: 'assistant' as const,
- content: 'I need two choices before continuing.',
- toolCalls: [
- {
- id: 'ask-tool',
- name: 'functions.AskUser',
- status: 'completed',
- input: { title: 'Choose', steps: [{ question: 'Which option?' }] },
- },
- ],
- },
- {
- id: 'ask-reply',
- turnId: 'turn-ask-reply',
- role: 'user' as const,
- content: [
- '<questionnaire-response>',
- ' <requestId>ask_1</requestId>',
- ' <answers />',
- '</questionnaire-response>',
- '',
- 'Q: Which option? ',
- 'A: Stable',
- ].join('\n'),
- },
- {
- id: 'final-a',
- turnId: 'turn-final',
- role: 'assistant' as const,
- content: 'Continuing with Stable.',
- },
- ]),
- sendMessage: vi.fn(),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- });
- await controller.loadSessionProjection('session-questionnaire');
- expect(transcript.snapshot()).toEqual([
- expect.objectContaining({
- kind: 'assistant',
- content: 'I need two choices before continuing.',
- }),
- expect.objectContaining({
- id: 'question:ask_1',
- kind: 'question',
- status: 'resolved',
- title: 'Answers sent',
- content: 'Which option? Stable',
- }),
- expect.objectContaining({
- kind: 'assistant',
- content: 'Continuing with Stable.',
- }),
- ]);
- const renderedContent = transcript
- .snapshot()
- .map((cell) => `${cell.title ?? ''}\n${cell.content}`)
- .join('\n');
- expect(renderedContent).not.toContain('functions.AskUser');
- expect(renderedContent).not.toContain('questionnaire-response');
- expect(renderedContent.match(/Continuing with Stable\./gu)).toHaveLength(1);
- });
- it('replays the durable continuation reply after a resolved questionnaire', async () => {
- const runtime = {
- createSession: vi.fn(),
- getSession: vi.fn(async () => ({ sessionId: 'session-questionnaire-continuation' })),
- getMessages: vi.fn(async () => [
- {
- id: 'ask-user-turn',
- turnId: 'turn-ask-user',
- role: 'assistant' as const,
- content: '',
- toolCalls: [{ name: 'ask_user', status: 'completed' }],
- timestamp: 100,
- },
- {
- id: 'ask-user-reply',
- turnId: 'turn-questionnaire-continuation',
- role: 'user' as const,
- content: [
- '<questionnaire-response>',
- ' <requestId>ask_1</requestId>',
- ' <answers />',
- '</questionnaire-response>',
- '',
- 'Q: What next? ',
- 'A: Chat',
- ].join('\n'),
- timestamp: 200,
- },
- {
- id: 'continuation-final',
- turnId: 'turn-questionnaire-continuation',
- role: 'assistant' as const,
- content: '好呀,那就随便聊聊。',
- timestamp: 300,
- },
- ]),
- sendMessage: vi.fn(),
- abortSession: vi.fn(async () => true),
- };
- const transcript = new TranscriptStore();
- const controller = new TuiChatController({
- runtime,
- transcript,
- workspaceDir: '/workspace',
- });
- await controller.loadSessionProjection('session-questionnaire-continuation');
- expect(transcript.snapshot()).toContainEqual(
- expect.objectContaining({
- kind: 'assistant',
- turnId: 'turn-questionnaire-continuation',
- content: '好呀,那就随便聊聊。',
- }),
- );
- });
- it('restores an archived Session before making it the active projection', async () => {
- let archived = true;
- const runtime = {
- getSession: vi.fn(async () => ({
- sessionId: 'session-archived',
- workspaceDir: '/workspace',
- archived,
- })),
- getMessages: vi.fn(async () => []),
- archiveSession: vi.fn(async (_sessionId: string, value: boolean) => {
- archived = value;
- }),
- sendMessage: vi.fn(),
- abortSession: vi.fn(async () => true),
- };
- const controller = new TuiChatController({
- runtime,
- transcript: new TranscriptStore(),
- workspaceDir: '/workspace',
- });
- await controller.loadSessionProjection('session-archived');
- expect(runtime.archiveSession).toHaveBeenCalledWith('session-archived', false);
- expect(controller.snapshot().session).toMatchObject({
- sessionId: 'session-archived',
- archived: false,
- });
- });
- it('manages a selected Desktop session without requiring it to be active first', async () => {
- let archived = false;
- const runtime = {
- createSession: vi.fn(
- async (input: { parentSessionId?: string; title?: string; workspaceDir: string }) => ({
- sessionId: 'session-fork',
- parentSessionId: input.parentSessionId,
- title: input.title,
- workspaceDir: input.workspaceDir,
- }),
- ),
- listSessions: vi.fn(async () => [
- {
- sessionId: 'session-1',
- title: 'First',
- workspaceDir: '/workspace',
- updatedAt: 200,
- },
- {
- sessionId: 'session-2',
- title: 'Second',
- workspaceDir: '/other',
- updatedAt: 100,
- },
- ]),
- getSession: vi.fn(async (sessionId: string) => ({
- sessionId,
- title: sessionId === 'session-2' ? 'Renamed second' : 'First',
- workspaceDir: sessionId === 'session-2' ? '/other' : '/workspace',
- archived,
- })),
- getMessages: vi.fn(async () => []),
- renameSession: vi.fn(async (sessionId: string, title: string) => ({
- sessionId,
- title,
- workspaceDir: '/other',
- })),
- archiveSession: vi.fn(async (_sessionId: string, value: boolean) => {
- archived = value;
- }),
- getAccountStatus: vi.fn(async () => ({
- status: 'ready' as const,
- warnings: [],
- })),
- sendMessage: vi.fn(),
- abortSession: vi.fn(async () => true),
- };
- const controller = new TuiChatController({
- runtime,
- transcript: new TranscriptStore(),
- workspaceDir: '/workspace',
- });
- await controller.initialize();
- await controller.loadSessionProjection('session-1');
- await expect(controller.renameSession('session-2', 'Renamed second')).resolves.toMatchObject({
- sessionId: 'session-2',
- title: 'Renamed second',
- });
- expect(controller.snapshot().session?.sessionId).toBe('session-1');
- await controller.loadSessionProjection('session-1');
- await controller.setSessionArchived('session-2', true);
- expect(controller.snapshot().sessions.map((session) => session.sessionId)).not.toContain(
- 'session-2',
- );
- expect(controller.snapshot().session?.sessionId).toBe('session-1');
- await expect(controller.renameSession('session-2', 'Archived second')).resolves.toMatchObject({
- sessionId: 'session-2',
- title: 'Archived second',
- archived: true,
- });
- expect(controller.snapshot().sessions.map((session) => session.sessionId)).not.toContain(
- 'session-2',
- );
- await expect(controller.setSessionArchived('session-2', false)).resolves.toMatchObject({
- sessionId: 'session-2',
- archived: false,
- });
- expect(controller.snapshot().sessions.map((session) => session.sessionId)).toContain(
- 'session-2',
- );
- });
- it('clears input-adjacent tasks when starting a new session', () => {
- const onTodoChange = vi.fn();
- const controller = new ProductionTuiChatController({
- runtime: {} as never,
- transcript: new TranscriptStore(),
- workspaceDir: '/workspace',
- onTodoChange,
- });
- controller.applyRuntimeTurnEvent('turn-todo', {
- type: 'generic',
- eventType: 'todo_updated',
- turnId: 'turn-todo',
- data: { todos: [{ content: 'Current session task', status: 'in_progress' }] },
- });
- expect(onTodoChange).toHaveBeenLastCalledWith([
- { content: 'Current session task', status: 'in_progress' },
- ]);
- controller.startNewSession();
- expect(onTodoChange).toHaveBeenLastCalledWith([]);
- });
- it('carries the clear source from /new to the lazily-created session', async () => {
- const onSessionLifecycle = vi.fn();
- const controller = new ProductionTuiChatController({
- runtime: {
- createSession: vi.fn(async () => ({ sessionId: 'session-after-clear' })),
- } as never,
- transcript: new TranscriptStore(),
- workspaceDir: '/workspace',
- onSessionLifecycle,
- });
- controller.startNewSession();
- await controller.ensureSession();
- expect(onSessionLifecycle.mock.calls).toEqual([[], ['session-after-clear']]);
- });
- });
|