File size: 31,225 Bytes
f0634fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
/**
 * ACP agent method handlers backed by the `Klient` facade (in-memory
 * transport by default β€” see `./start`), routed through the SDK's app API
 * (`agent()` builder + `onRequest` / `onNotification` β€” see
 * {@link createAcpAgentApp}).
 *
 * `initialize`, the session lifecycle (`session/new`, `/load`, `/resume`,
 * `/list`, `/close`, `/delete`, `/fork`), `session/prompt`, `session/cancel`, and the
 * config surface (model / mode / thinking) are
 * wired to `klient.global.sessions`, `klient.session(id)` lifecycle +
 * interactions, and the per-session main agent handle (`klient.session(id).
 * agent('main')`). Slash commands, skills, approval / question bridging
 * (`session/request_permission`), and `session/load` history replay live in
 * `./session` / `./interaction-bridge`. ACP `mcpServers` on `session/new` /
 * `/load` / `/resume` are converted to the engine's name-keyed record (see
 * `./convert`) and injected as ephemeral per-session MCP servers. When the
 * client advertises `clientCapabilities.terminal`, Bash executions reverse-RPC
 * through the client terminal (`./acp-terminal`).
 */

import {
  agent,
  type AgentApp,
  type AgentCapabilities,
  type AuthenticateRequest,
  type AvailableCommand,
  type AuthenticateResponse,
  type CancelNotification,
  type ClientCapabilities,
  type CloseSessionRequest,
  type CloseSessionResponse,
  type DeleteSessionRequest,
  type DeleteSessionResponse,
  type ForkSessionRequest,
  type ForkSessionResponse,
  type Implementation,
  type InitializeRequest,
  type InitializeResponse,
  type ListSessionsRequest,
  type ListSessionsResponse,
  type LoadSessionRequest,
  type LoadSessionResponse,
  type LogoutRequest,
  type LogoutResponse,
  methods,
  type NewSessionRequest,
  type NewSessionResponse,
  type PromptRequest,
  type PromptResponse,
  RequestError,
  type ResumeSessionRequest,
  type ResumeSessionResponse,
  type SessionInfo,
  type SetSessionConfigOptionRequest,
  type SetSessionConfigOptionResponse,
  type SetSessionModeRequest,
  type SetSessionModeResponse,
} from '@agentclientprotocol/sdk';
import type {
  AgentHandle,
  Klient,
  SessionHandle,
  SessionRestoreOptions,
  SessionSummary,
} from '@moonshot-ai/klient';
import { ErrorCodes, isError2 } from '@moonshot-ai/agent-core-v2';
import { RPCError } from '@moonshot-ai/klient';

import type { AcpClient } from './acp-client';
import type { IAcpConnection } from './acp-fs';
import { buildTerminalAuthMethod, TERMINAL_AUTH_METHOD } from './auth-methods';
import { acpMcpServersToConfigRecord } from './convert';
import { log } from './log';
import { isAcpModeId } from './modes';
import { AcpSession } from './session';
import { negotiateVersion } from './version';

/**
 * Klient's stable wire code for "session not found" (`RPCError.code`) β€” the
 * branch key across the wire, mirrored from the klient facade's `NOT_FOUND`.
 */
const SESSION_NOT_FOUND_CODE = 40404;

function isSessionNotFound(error: unknown): boolean {
  return (
    (error instanceof RPCError && error.code === SESSION_NOT_FOUND_CODE) ||
    (isError2(error) && error.code === ErrorCodes.SESSION_NOT_FOUND)
  );
}

/** Host-provided slash commands plus optional aliases that activate engine skills. */
export interface SlashCommandsSnapshot {
  readonly commands: ReadonlyArray<AvailableCommand>;
  readonly skillCommandMap?: ReadonlyMap<string, string>;
}

export type SlashCommandsResolver =
  | ReadonlyArray<AvailableCommand>
  | SlashCommandsSnapshot
  | ((
      session: SessionHandle,
    ) =>
      | Promise<ReadonlyArray<AvailableCommand> | SlashCommandsSnapshot>
      | ReadonlyArray<AvailableCommand>
      | SlashCommandsSnapshot);

export interface AcpServerOptions {
  /** Agent identity advertised in `initialize.agentInfo`. */
  readonly agentInfo?: Implementation;
  /**
   * Bypass the auth gate (`klient.global.auth.summarize()`). Intended for
   * tests and local dev β€” production ACP hosts should leave this `false` so
   * unauthenticated clients get a structured `auth_required` before any
   * session is created.
   */
  readonly disableAuth?: boolean;
  /**
   * Env vars to advertise in `authMethods[0].env` so the `kimi login`
   * subprocess the client spawns (via terminal-auth) lands its token under the
   * same data root the server uses (e.g. `{ KIMI_CODE_HOME: '/tmp/...' }` for
   * sandboxed test setups). Leave undefined in production so the advertised
   * env stays empty.
   */
  readonly terminalAuthEnv?: Readonly<Record<string, string>>;
  /**
   * Absolute binary path advertised in `_meta['terminal-auth'].command` for
   * clients that don't yet honor the first-class `type:'terminal'`. Defaults
   * to undefined (the `_meta` fallback is omitted).
   */
  readonly terminalAuthLegacyCommand?: string;
  /**
   * Resolve a session's media-originals dir for prompt-image compression.
   * This is a composition-root concern (it reads the live engine scope tree,
   * not the klient facade) β€” `start.ts` builds it from the bootstrapped App
   * scope. Absent β†’ `persistOriginalImage`'s shared temp-dir fallback.
   */
  readonly resolveOriginalsDir?: (sessionId: string) => string | undefined;
  readonly bindSessionRuntime?: (sessionId: string) => Promise<void>;
  readonly unbindSessionRuntime?: (sessionId: string) => Promise<void>;
  /** Static or per-session host command palette. */
  readonly slashCommands?: SlashCommandsResolver;
}

export class AcpServer {
  private clientCapabilities: ClientCapabilities | undefined;
  private readonly agentInfo: Implementation | undefined;
  private readonly disableAuth: boolean;
  private readonly terminalAuthEnv: Readonly<Record<string, string>> | undefined;
  private readonly terminalAuthLegacyCommand: string | undefined;
  private readonly resolveOriginalsDir: ((sessionId: string) => string | undefined) | undefined;
  private readonly bindSessionRuntime: ((sessionId: string) => Promise<void>) | undefined;
  private readonly unbindSessionRuntime: ((sessionId: string) => Promise<void>) | undefined;
  private readonly resolveSlashCommands: (
    session: SessionHandle,
  ) => Promise<ReadonlyArray<AvailableCommand> | SlashCommandsSnapshot>;
  private readonly sessions = new Map<string, AcpSession>();

  constructor(
    private readonly conn: AcpClient,
    private readonly klient: Klient,
    /**
     * The engine-side ACP connection holder (host file-IO reverse-RPC). This
     * is a composition-root concern, not a klient facade concern β€” `start.ts`
     * resolves it from the bootstrapped scope and passes it in.
     */
    private readonly acpConnection: IAcpConnection,
    opts: AcpServerOptions = {},
  ) {
    this.agentInfo = opts.agentInfo;
    this.disableAuth = opts.disableAuth ?? false;
    this.terminalAuthEnv = opts.terminalAuthEnv;
    this.terminalAuthLegacyCommand = opts.terminalAuthLegacyCommand;
    this.resolveOriginalsDir = opts.resolveOriginalsDir;
    this.bindSessionRuntime = opts.bindSessionRuntime;
    this.unbindSessionRuntime = opts.unbindSessionRuntime;
    const slashCommands = opts.slashCommands;
    this.resolveSlashCommands =
      typeof slashCommands === 'function'
        ? async (session) => slashCommands(session)
        : async () => slashCommands ?? [];
  }

  /** Returns the client capabilities advertised during `initialize`, if any. */
  get clientCaps(): ClientCapabilities | undefined {
    return this.clientCapabilities;
  }

  /** @internal β€” for tests/inspection only. */
  getSession(sessionId: string): AcpSession | undefined {
    return this.sessions.get(sessionId);
  }

  async initialize(params: InitializeRequest): Promise<InitializeResponse> {
    this.clientCapabilities = params.clientCapabilities;
    this.acpConnection.bindFsCapabilities(params.clientCapabilities?.fs);
    this.acpConnection.bindTerminalCapability(params.clientCapabilities?.terminal === true);
    // Answer with the highest mutually-supported protocol version (a client
    // advertising a newer major, e.g. 99, still gets our current version).
    const negotiated = negotiateVersion(params.protocolVersion);

    const agentCapabilities: AgentCapabilities = {
      loadSession: true,
      promptCapabilities: {
        image: true,
        audio: false,
        embeddedContext: true,
      },
      sessionCapabilities: {
        list: {},
        resume: {},
        close: {},
        delete: {},
        // UNSTABLE per the SDK schema β€” declared as-is: the engine + klient
        // fork path is fully wired (`unstable_forkSession` below).
        fork: {},
        // Honored on `session/new` only β€” see the KLIENT-GAP note in
        // `loadSession` / `resumeSession`.
        additionalDirectories: {},
      },
      // Stdio is the implied baseline in ACP (no capability flag); we also
      // forward http/sse servers to the engine. The unstable ACP transport
      // is not supported (dropped with a warning β€” see `./convert`).
      mcpCapabilities: { http: true, sse: true },
      auth: { logout: {} },
    };

    return {
      protocolVersion: negotiated.protocolVersion,
      agentCapabilities,
      authMethods: [
        this.terminalAuthEnv !== undefined || this.terminalAuthLegacyCommand !== undefined
          ? buildTerminalAuthMethod({
              env: this.terminalAuthEnv,
              legacyCommand: this.terminalAuthLegacyCommand,
            })
          : TERMINAL_AUTH_METHOD,
      ],
      ...(this.agentInfo ? { agentInfo: this.agentInfo } : {}),
    };
  }

  async newSession(params: NewSessionRequest): Promise<NewSessionResponse> {
    await this.ensureAuthed();
    // The engine mints the session id and registers the workspace for the cwd
    // implicitly. ACP `mcpServers` become ephemeral per-session servers
    // (connected for this session only, never persisted).
    const meta = await this.klient.global.sessions.create({
      workDir: params.cwd,
      additionalDirs: params.additionalDirectories,
      mcpServers: acpMcpServersToConfigRecord(params.mcpServers),
    });
    return { sessionId: meta.id, ...(await this.activateSession(meta.id)) };
  }

  /**
   * Handle ACP `session/fork` (UNSTABLE in the SDK schema). Forks the source
   * session through the engine (`sessionLifecycleService.fork` via the klient
   * facade) and manages the forked session exactly like a `session/new` one β€”
   * same response surface (`sessionId` + `configOptions` + `modes`), same
   * local `AcpSession` wiring. The engine fork inherits the source session's
   * workspace and carries no slot for `cwd` / `additionalDirectories` /
   * `mcpServers` (ephemeral servers are not carried over), so those request
   * fields are ignored with a warning, mirroring load/resume. An unknown
   * source id maps to ACP `invalid_params` (-32602).
   */
  async unstable_forkSession(params: ForkSessionRequest): Promise<ForkSessionResponse> {
    await this.ensureAuthed();
    this.warnIgnoredAdditionalDirs('session/fork', params.additionalDirectories);
    if (params.mcpServers !== undefined && params.mcpServers.length > 0) {
      log.warn('acp: session/fork ignores mcpServers (engine fork keeps the source servers)', {
        servers: params.mcpServers.map((server) => server.name),
      });
    }
    let forkedId: string;
    try {
      forkedId = (await this.klient.session(params.sessionId).fork()).id;
    } catch (error) {
      if (isSessionNotFound(error)) {
        throw RequestError.invalidParams(
          { sessionId: params.sessionId },
          `Unknown sessionId: ${params.sessionId}`,
        );
      }
      throw error;
    }
    const restored = await this.klient.session(forkedId).restore();
    if (!restored) {
      throw RequestError.invalidParams(
        { sessionId: forkedId },
        `Unknown sessionId: ${forkedId}`,
      );
    }
    return { sessionId: forkedId, ...(await this.activateSession(forkedId)) };
  }

  async loadSession(params: LoadSessionRequest): Promise<LoadSessionResponse> {
    await this.ensureAuthed();
    this.warnIgnoredAdditionalDirs('session/load', params.additionalDirectories);
    const acpSession = await this.resumeAcpSession(
      params.sessionId,
      acpMcpServersToConfigRecord(params.mcpServers),
    );
    // Replay the persisted history as an ordered batch of `session/update`
    // notifications BEFORE settling, so the client re-renders prior turns
    // before the load response lands. This is the one differentiator vs.
    // `resumeSession`, which deliberately skips replay per the ACP spec.
    await acpSession.replayHistory();
    this.scheduleAvailableCommandsUpdate(acpSession);
    return { configOptions: await acpSession.configOptions(), modes: acpSession.modeState() };
  }

  async resumeSession(params: ResumeSessionRequest): Promise<ResumeSessionResponse> {
    await this.ensureAuthed();
    this.warnIgnoredAdditionalDirs('session/resume', params.additionalDirectories);
    const acpSession = await this.resumeAcpSession(
      params.sessionId,
      acpMcpServersToConfigRecord(params.mcpServers),
    );
    this.scheduleAvailableCommandsUpdate(acpSession);
    return { configOptions: await acpSession.configOptions(), modes: acpSession.modeState() };
  }

  async listSessions(params: ListSessionsRequest): Promise<ListSessionsResponse> {
    const cwd = params.cwd ?? undefined;
    const page = await this.klient.global.sessions.list({});
    const sessions: SessionInfo[] = filterSessionSummariesByCwd(page.items, cwd).map(
      sessionSummaryToSessionInfo,
    );
    return { sessions, nextCursor: page.nextCursor ?? null };
  }

  /**
   * Handle ACP `session/close`. Cancels any in-flight turn, tears down the
   * per-session ACP resources (interaction bridge, event subscriptions), and
   * asks the engine to dispose the live session scope. Best-effort: an
   * unknown or already-closed session id is not an error β€” `close` is a
   * cleanup operation, and the lifecycle close is a no-op for a session that
   * is not currently live.
   */
  async closeSession(params: CloseSessionRequest): Promise<CloseSessionResponse | void> {
    const acpSession = this.sessions.get(params.sessionId);
    if (acpSession !== undefined) {
      acpSession.dispose();
      this.sessions.delete(params.sessionId);
    }
    await this.klient.session(params.sessionId).close();
    await this.unbindSessionRuntime?.(params.sessionId);
  }

  /**
   * Handle ACP `session/delete`. Permanently removes the session through the
   * engine (`sessionLifecycleService.delete` β€” closes a live session first,
   * then drops its persisted data and index entries) and tears down any local
   * ACP state for it. Unlike `close`, delete is NOT best-effort: the client
   * asked to remove one specific listed session, so an unknown id maps to ACP
   * `invalid_params` (-32602).
   */
  async deleteSession(params: DeleteSessionRequest): Promise<DeleteSessionResponse> {
    try {
      await this.klient.session(params.sessionId).delete();
    } catch (error) {
      if (isSessionNotFound(error)) {
        throw RequestError.invalidParams(
          { sessionId: params.sessionId },
          `Unknown sessionId: ${params.sessionId}`,
        );
      }
      throw error;
    }
    const acpSession = this.sessions.get(params.sessionId);
    if (acpSession !== undefined) {
      acpSession.dispose();
      this.sessions.delete(params.sessionId);
    }
    await this.unbindSessionRuntime?.(params.sessionId);
    return {};
  }

  async authenticate(params: AuthenticateRequest): Promise<AuthenticateResponse | void> {
    if (params.methodId !== 'login') {
      throw RequestError.invalidParams(
        { methodId: params.methodId },
        `Unknown auth method: ${params.methodId}`,
      );
    }
    // Re-check the gate; clients spawn `kimi login` themselves via the
    // terminal-auth method and re-invoke `authenticate('login')` to confirm the
    // token landed. `void` = empty success body.
    await this.ensureAuthed();
  }

  /**
   * Handle ACP `logout`. Drops the managed provider's token through the engine
   * (`oauthService.logout`, which also deprovisions managed config). The auth
   * gate re-derives from `klient.global.auth.summarize()` on every gated call,
   * so no extra state is needed here β€” the next gated method hits
   * `auth_required` again naturally. `void` = empty success body.
   */
  async logout(_params: LogoutRequest): Promise<LogoutResponse | void> {
    await this.klient.global.auth.logout();
  }

  /**
   * Handle ACP `session/prompt`. `signal` is the app-API per-request abort
   * signal: a JSON-RPC `$/cancel_request` for this request aborts it, and the
   * abort is routed into the exact same cancel path as the `session/cancel`
   * notification ({@link cancel}).
   */
  async prompt(params: PromptRequest, signal?: AbortSignal): Promise<PromptResponse> {
    const acpSession = this.sessions.get(params.sessionId);
    if (!acpSession) {
      throw RequestError.invalidParams(undefined, `Unknown sessionId: ${params.sessionId}`);
    }
    if (signal === undefined) {
      return acpSession.prompt(params.prompt);
    }
    const onAbort = (): void => {
      try {
        acpSession.cancel();
      } catch (error) {
        log.warn('acp: error while cancelling session', {
          sessionId: params.sessionId,
          error: error instanceof Error ? error.message : String(error),
        });
      }
    };
    signal.addEventListener('abort', onAbort, { once: true });
    try {
      // An already-aborted signal never fires the listener β€” cancel up front.
      if (signal.aborted) onAbort();
      return await acpSession.prompt(params.prompt);
    } finally {
      signal.removeEventListener('abort', onAbort);
    }
  }

  async cancel(params: CancelNotification): Promise<void> {
    const acpSession = this.sessions.get(params.sessionId);
    if (!acpSession) {
      // `session/cancel` is a notification β€” the spec forbids returning errors.
      log.warn('acp: cancel for unknown sessionId', { sessionId: params.sessionId });
      return;
    }
    try {
      acpSession.cancel();
    } catch (error) {
      log.warn('acp: error while cancelling session', {
        sessionId: params.sessionId,
        error: error instanceof Error ? error.message : String(error),
      });
    }
  }

  async setSessionMode(params: SetSessionModeRequest): Promise<SetSessionModeResponse | void> {
    const acpSession = this.sessions.get(params.sessionId);
    if (!acpSession) {
      throw RequestError.invalidParams(
        { sessionId: params.sessionId },
        `Unknown sessionId: ${params.sessionId}`,
      );
    }
    if (!isAcpModeId(params.modeId)) {
      throw RequestError.invalidParams(
        { modeId: params.modeId },
        `Unknown modeId: ${params.modeId}`,
      );
    }
    await acpSession.setMode(params.modeId);
  }

  async setSessionConfigOption(
    params: SetSessionConfigOptionRequest,
  ): Promise<SetSessionConfigOptionResponse> {
    const acpSession = this.sessions.get(params.sessionId);
    if (!acpSession) {
      throw RequestError.invalidParams(
        { sessionId: params.sessionId },
        `Unknown sessionId: ${params.sessionId}`,
      );
    }
    const value = (params as { value: unknown }).value;
    switch (params.configId) {
      case 'model':
        await acpSession.setModel(String(value));
        break;
      case 'mode': {
        if (!isAcpModeId(value)) {
          throw RequestError.invalidParams({ modeId: value }, `Unknown modeId: ${String(value)}`);
        }
        await acpSession.setMode(value);
        break;
      }
      case 'thinking': {
        // Capability-aware validation lives in the session (allowed set
        // depends on the current model's declared efforts); an unacceptable
        // value maps to `invalid_params`.
        const accepted = await acpSession.setThinking(String(value));
        if (!accepted) {
          throw RequestError.invalidParams(
            { configId: params.configId, value },
            `Unknown thinking value: ${String(value)}`,
          );
        }
        break;
      }
      default:
        throw RequestError.invalidParams(
          { configId: params.configId },
          `Unknown configId: ${params.configId}`,
        );
    }
    return { configOptions: await acpSession.configOptions() };
  }

  /**
   * Handle the custom `session/set_model` request (dropped from the SDK's
   * typed method set in 1.x; preserved here as an extension method β€” see
   * {@link createAcpAgentApp} for its registration and param parsing).
   * Returns the empty success body.
   */
  async setSessionModel(params: SetSessionModelParams): Promise<Record<string, unknown>> {
    const acpSession = this.sessions.get(params.sessionId);
    if (!acpSession) {
      throw RequestError.invalidParams(
        { sessionId: params.sessionId },
        `Unknown sessionId: ${params.sessionId}`,
      );
    }
    await acpSession.setModel(params.modelId);
    return {};
  }

  /**
   * Resume a persisted session into the live scope tree and build its ACP
   * session. An unknown session id maps to ACP `invalid_params` (-32602)
   * rather than a generic internal error. `mcpServers` (already converted to
   * the engine record shape) is forwarded to the re-materialize path; it is
   * ignored when the session is already live (restore passes through).
   */
  private async resumeAcpSession(
    sessionId: string,
    mcpServers?: SessionRestoreOptions['mcpServers'],
  ): Promise<AcpSession> {
    // `restore` re-materializes a persisted session (a live one passes
    // through) and reports `false` only when the id no longer exists.
    const restored = await this.klient.session(sessionId).restore({ mcpServers });
    if (!restored) {
      throw RequestError.invalidParams({ sessionId }, `Unknown sessionId: ${sessionId}`);
    }
    const acpSession = await this.wireSession(sessionId);
    this.sessions.get(sessionId)?.dispose();
    this.sessions.set(sessionId, acpSession);
    return acpSession;
  }

  /**
   * Build the ACP session for a live session: bind the configured default
   * model to the main agent (best-effort β€” a missing default model leaves the
   * agent unbound, and `prompt` settles gracefully until a model is set via
   * `set_config_option`), then subscribe its event stream.
   */
  private async wireSession(sessionId: string): Promise<AcpSession> {
    const session = this.klient.session(sessionId);
    await this.bindDefaultModel(session.agent('main'));
    await this.bindSessionRuntime?.(sessionId);
    const hostCommands = await this.resolveSlashCommands(session);
    const acpSession = new AcpSession(
      this.conn,
      this.klient,
      sessionId,
      this.acpConnection,
      Boolean(this.clientCapabilities?.elicitation?.form),
      this.resolveOriginalsDir,
      hostCommands,
    );
    await acpSession.init();
    return acpSession;
  }

  /**
   * Bring a freshly created/forked session under local ACP management
   * (wire + register + advertise slash commands) and build the response
   * surface shared by `session/new` and `session/fork`.
   */
  private async activateSession(sessionId: string): Promise<{
    configOptions: Awaited<ReturnType<AcpSession['configOptions']>>;
    modes: ReturnType<AcpSession['modeState']>;
  }> {
    const acpSession = await this.wireSession(sessionId);
    this.sessions.set(sessionId, acpSession);
    this.scheduleAvailableCommandsUpdate(acpSession);
    return { configOptions: await acpSession.configOptions(), modes: acpSession.modeState() };
  }

  /**
   * Push the `available_commands_update` AFTER the triggering lifecycle
   * response (`session/new` / `/fork` / `/load` / `/resume`) has settled.
   * Clients register the session when the response lands and silently drop
   * `session/update` notifications that arrive earlier (Zed), so an eager
   * push leaves the client's slash-command palette empty.
   */
  private scheduleAvailableCommandsUpdate(acpSession: AcpSession): void {
    setTimeout(() => {
      void acpSession.emitAvailableCommandsUpdate();
    }, 0);
  }

  private async bindDefaultModel(agent: AgentHandle): Promise<void> {
    try {
      // `getModel` is '' while the profile has no model bound (the same guard
      // the old engine-direct binding expressed via `isRunnable()`).
      if ((await agent.getModel()).length > 0) return;
      const inspected = await this.klient.global.config.inspect<string>('defaultModel');
      const model = inspected.value;
      if (typeof model === 'string' && model.length > 0) {
        await agent.setModel(model);
      }
    } catch (error) {
      log.warn('acp: default model binding skipped', {
        error: error instanceof Error ? error.message : String(error),
      });
    }
  }

  /** Auth gate: throws `auth_required` unless authed (or `disableAuth`). */
  private async ensureAuthed(): Promise<void> {
    if (this.disableAuth) return;
    // Primary: the engine's own readiness probe for the default model β€”
    // config-file apiKey / provider env-bag credentials / OAuth token all
    // count, matching how the model is actually used (the OAuth-only
    // `summarize()` view is too narrow on its own).
    try {
      await this.klient.global.auth.ensureReady();
      return;
    } catch (error) {
      log.info('acp: auth readiness probe failed, trying the OAuth summary', {
        error: error instanceof Error ? error.message : String(error),
      });
    }
    // Fallback: any logged-in OAuth provider counts as authed even when the
    // default model is not usable (the legacy adapter's first branch).
    const summaries = await this.klient.global.auth.summarize();
    if (!summaries.some((s) => s.loggedIn)) {
      throw RequestError.authRequired();
    }
  }

  /**
   * KLIENT-GAP(additionalDirectories): the engine merges additional roots only
   * on session create (`workspaceDirs.mergeAdditionalDirs`); load/resume have
   * no merge slot yet, so the field is ignored there with a warning rather
   * than silently honored.
   */
  private warnIgnoredAdditionalDirs(method: string, dirs: readonly string[] | undefined): void {
    if (dirs === undefined || dirs.length === 0) return;
    log.warn(`acp: ${method} ignores additionalDirectories (engine merges dirs only on create)`, {
      dirs,
    });
  }
}

/** The custom ACP method name for per-session model selection. */
const SET_SESSION_MODEL_METHOD = 'session/set_model';

/** Parsed params of the custom `session/set_model` request. */
export interface SetSessionModelParams {
  readonly sessionId: string;
  readonly modelId: string;
}

/**
 * Params parser for the custom `session/set_model` route (the app API
 * requires every custom method to bring its own parser). Hand-rolled rather
 * than zod β€” this package has no zod dependency, and the narrowing plus the
 * thrown `invalid_params` error are exactly what the legacy `extMethod`
 * path produced.
 */
function parseSetSessionModelParams(params: unknown): SetSessionModelParams {
  const { sessionId, modelId } = (params ?? {}) as Record<string, unknown>;
  if (typeof sessionId !== 'string' || typeof modelId !== 'string') {
    throw RequestError.invalidParams(
      params,
      'session/set_model expects { sessionId: string, modelId: string }',
    );
  }
  return { sessionId, modelId };
}

/**
 * Build the ACP agent app (SDK `agent()` builder) that routes every inbound
 * method to the {@link AcpServer} returned by `getServer`.
 *
 * `getServer` is dereferenced lazily per request because the app must be
 * connected before the outbound client surface (and thus the server) exists
 * β€” see `./start`. Handlers for unregistered methods never arise here: the
 * connection layer answers unknown requests with `method_not_found` (-32601)
 * and silently drops unknown notifications, matching the legacy
 * `extMethod` / `extNotification` fallbacks.
 */
export function createAcpAgentApp(getServer: () => AcpServer): AgentApp {
  return agent({ name: 'kimi-code-acp' })
    .onRequest(methods.agent.initialize, (ctx) => getServer().initialize(ctx.params))
    .onRequest(methods.agent.authenticate, (ctx) => getServer().authenticate(ctx.params))
    .onRequest(methods.agent.logout, (ctx) => getServer().logout(ctx.params))
    .onRequest(methods.agent.session.new, (ctx) => getServer().newSession(ctx.params))
    .onRequest(methods.agent.session.load, (ctx) => getServer().loadSession(ctx.params))
    .onRequest(methods.agent.session.resume, (ctx) => getServer().resumeSession(ctx.params))
    .onRequest(methods.agent.session.list, (ctx) => getServer().listSessions(ctx.params))
    .onRequest(methods.agent.session.close, (ctx) => getServer().closeSession(ctx.params))
    .onRequest(methods.agent.session.delete, (ctx) => getServer().deleteSession(ctx.params))
    .onRequest(methods.agent.session.fork, (ctx) => getServer().unstable_forkSession(ctx.params))
    .onRequest(methods.agent.session.setMode, (ctx) => getServer().setSessionMode(ctx.params))
    .onRequest(methods.agent.session.setConfigOption, (ctx) =>
      getServer().setSessionConfigOption(ctx.params),
    )
    .onRequest(methods.agent.session.prompt, (ctx) => getServer().prompt(ctx.params, ctx.signal))
    .onNotification(methods.agent.session.cancel, (ctx) => getServer().cancel(ctx.params))
    .onRequest(SET_SESSION_MODEL_METHOD, parseSetSessionModelParams, (ctx) =>
      getServer().setSessionModel(ctx.params),
    );
}

/**
 * Apply the optional `session/list` cwd filter to wire {@link SessionSummary}s.
 *
 * `cwd === undefined` means no filter (the adapter treats the schema-allowed
 * `null` sentinel the same way β€” the caller normalizes it before calling).
 * When a filter IS active, summaries that carry no `cwd` at all (sessions
 * persisted before the field existed) are KEPT: their workspace is unknown,
 * not known-different, and silently dropping them would make those sessions
 * unreachable from any cwd-filtered listing. The filter lives here (not in
 * the engine query) because the engine's `sessions.list` has no cwd predicate
 * β€” `workspaceIds` is its only workspace-level filter, and a raw cwd string is
 * not a workspace id.
 */
export function filterSessionSummariesByCwd(
  items: readonly SessionSummary[],
  cwd: string | undefined,
): readonly SessionSummary[] {
  if (cwd === undefined) return items;
  return items.filter((s) => s.cwd === undefined || s.cwd === cwd);
}

/**
 * Project a wire {@link SessionSummary} into the ACP {@link SessionInfo}
 * shape used by `session/list`.
 */
function sessionSummaryToSessionInfo(summary: SessionSummary): SessionInfo {
  let updatedAt: string | null = null;
  if (typeof summary.updatedAt === 'number' && Number.isFinite(summary.updatedAt)) {
    const date = new Date(summary.updatedAt);
    if (!Number.isNaN(date.getTime())) {
      updatedAt = date.toISOString();
    }
  }
  const titleRaw = summary.title;
  const title = typeof titleRaw === 'string' && titleRaw.length > 0 ? titleRaw : null;
  return {
    sessionId: summary.id,
    cwd: summary.cwd ?? '',
    title,
    updatedAt,
  };
}