File size: 23,680 Bytes
63522a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { beforeEach, describe, expect, it, vi } from "vitest";
import { CANVAS_UI_CLIENT_TOOL_NAME } from "#/constants/canvas-ui";
import { LAUNCH_CHILD_CONVERSATION_TOOL_NAME } from "#/constants/child-conversation";
import { DEFAULT_SETTINGS } from "#/services/settings";
import type { Settings } from "#/types/settings";
import {
  AGENT_CANVAS_SOURCE,
  CLIENT_SOURCE_TAG_KEY,
  buildStartConversationRequest,
  buildStartPlanningConversationRequest,
  buildStartPlanningConversationRequestWithEncryptedSettings,
  toConversationPage,
} from "./agent-server-adapter";
import SettingsService from "./settings-service/settings-service.api";
import AgentProfilesService from "./agent-profiles-service/agent-profiles-service.api";
import ProfilesService from "./profiles-service/profiles-service.api";

vi.mock("./settings-service/settings-service.api", () => ({
  default: { getSettingsForConversation: vi.fn() },
}));
vi.mock("./secrets-service", () => ({
  SecretsService: { getSecrets: vi.fn().mockResolvedValue([]) },
}));
vi.mock("./agent-profiles-service/agent-profiles-service.api", () => ({
  default: { listProfiles: vi.fn() },
}));
vi.mock("./profiles-service/profiles-service.api", () => ({
  default: { getProfile: vi.fn() },
}));

const encryptedValue = "gAAAAAencrypted-mcp-header";

function makeSettings(agentSettings: Settings["agent_settings"]): Settings {
  return {
    ...DEFAULT_SETTINGS,
    agent_settings: agentSettings,
    conversation_settings: {
      confirmation_mode: false,
      security_analyzer: null,
      max_iterations: 20,
    },
  };
}

function getAgentContextSkillNames(
  payload: ReturnType<typeof buildStartConversationRequest>,
): Array<string | undefined> {
  const agentSettings = payload.agent_settings as
    | {
        agent_context?: {
          skills?: Array<{ name?: string }>;
        };
      }
    | undefined;
  return agentSettings?.agent_context?.skills?.map((skill) => skill.name) ?? [];
}

function getPlannerAgentContextSkillNames(payload: {
  agent: { agent_context?: { skills?: Array<{ name?: string }> } };
}): Array<string | undefined> {
  return payload.agent.agent_context?.skills?.map((skill) => skill.name) ?? [];
}

describe("buildStartConversationRequest", () => {
  it("marks OpenHands start requests as encrypted when MCP headers are encrypted", () => {
    const agentSettings = {
      agent_kind: "openhands",
      llm: {
        model: "litellm_proxy/openai/gpt-5.5",
        api_key: "gAAAAAencrypted-llm-api-key",
      },
      mcp_config: {
        linear: {
          url: "https://mcp.linear.app/mcp",
          transport: "http",
          headers: {
            Authorization: encryptedValue,
          },
        },
      },
    };
    const settings = makeSettings(agentSettings);

    const payload = buildStartConversationRequest({
      settings,
      encryptedAgentSettings: agentSettings,
      encryptedConversationSettings: settings.conversation_settings!,
      secretsEncrypted: true,
    });

    expect(payload.agent_settings!.agent_kind).toBe("openhands");
    expect(payload.agent_settings!.mcp_config).toEqual(
      agentSettings.mcp_config,
    );
    expect(payload.secrets_encrypted).toBe(true);
  });

  it("marks ACP start requests as encrypted when MCP headers are encrypted", () => {
    const agentSettings = {
      agent_kind: "acp",
      acp_server: "codex",
      acp_command: ["codex-acp"],
      acp_model: "gpt-5.5/medium",
      mcp_config: {
        linear: {
          url: "https://mcp.linear.app/mcp",
          transport: "http",
          headers: {
            Authorization: encryptedValue,
          },
        },
      },
    };
    const settings = makeSettings(agentSettings);

    const payload = buildStartConversationRequest({
      settings,
      encryptedAgentSettings: agentSettings,
      encryptedConversationSettings: settings.conversation_settings!,
      secretsEncrypted: true,
    });

    expect(payload.agent_settings!.agent_kind).toBe("acp");
    expect(payload.agent_settings!.mcp_config).toEqual(
      agentSettings.mcp_config,
    );
    expect(payload.secrets_encrypted).toBe(true);
  });

  it("builds a raw planning agent request for local Planner", () => {
    const agentSettings = {
      agent_kind: "openhands",
      llm: {
        model: "openhands/minimax-m2.7",
        api_key: "gAAAAAencrypted-llm-api-key",
      },
    };

    const payload = buildStartPlanningConversationRequest({
      encryptedAgentSettings: agentSettings,
      workingDir: "/workspace/project/agent-canvas",
      parentConversationId: "parent-1",
      secretsEncrypted: true,
      customSecrets: [{ name: "CUSTOM_TOKEN" }],
    });

    expect(payload.agent).toMatchObject({
      kind: "Agent",
      system_prompt_filename: "system_prompt_planning.j2",
      system_prompt_kwargs: {
        plan_structure: expect.stringContaining("OBJECTIVE"),
      },
      llm: {
        model: "openhands/minimax-m2.7",
        api_key: "gAAAAAencrypted-llm-api-key",
      },
      tools: [
        { name: "glob", params: {} },
        { name: "grep", params: {} },
        {
          name: "planning_file_editor",
          params: {
            plan_path: "/workspace/project/agent-canvas/.agents_tmp/PLAN.md",
          },
        },
      ],
      // Matches the SDK planning preset's get_planning_condenser.
      condenser: {
        kind: "LLMSummarizingCondenser",
        max_size: 100,
        keep_first: 6,
        llm: {
          model: "openhands/minimax-m2.7",
          usage_id: "planning_condenser",
        },
      },
    });
    expect(payload.agent_settings).toBeUndefined();
    expect(payload.worktree).toBe(false);
    expect(payload.tags).toEqual({ plannerparent: "parent-1" });
    expect(payload.secrets_encrypted).toBe(true);
    expect(payload.secrets).toHaveProperty("CUSTOM_TOKEN");
  });

  it("links the local planner to its parent server-side so the relationship survives storage loss", () => {
    const payload = buildStartPlanningConversationRequest({
      encryptedAgentSettings: {
        agent_kind: "openhands",
        llm: { model: "openhands/minimax-m2.7" },
      },
      workingDir: "/workspace/project",
      parentConversationId: "parent-1",
    });

    // The agent-server derives the parent's `sub_conversation_ids` from this,
    // which is what makes the planner recoverable without localStorage.
    expect(payload.parent_conversation_id).toBe("parent-1");
  });

  it("defaults the planner's max_iterations to 500 when not provided", () => {
    const payload = buildStartPlanningConversationRequest({
      encryptedAgentSettings: {
        agent_kind: "openhands",
        llm: { model: "openhands/minimax-m2.7" },
      },
      workingDir: "/workspace/project",
      parentConversationId: "parent-1",
    });

    expect(payload.max_iterations).toBe(500);
  });

  it("uses the caller-provided max_iterations instead of the hardcoded default", () => {
    const payload = buildStartPlanningConversationRequest({
      encryptedAgentSettings: {
        agent_kind: "openhands",
        llm: { model: "openhands/minimax-m2.7" },
      },
      workingDir: "/workspace/project",
      parentConversationId: "parent-1",
      maxIterations: 250,
    });

    expect(payload.max_iterations).toBe(250);
  });

  it("omits local planner helper conversations from paginated conversation results", () => {
    const page = toConversationPage({
      items: [
        {
          id: "main-1",
          created_at: "2024-01-01T00:00:00.000Z",
          updated_at: "2024-01-01T00:00:00.000Z",
          execution_status: "idle",
          tags: {},
        },
        {
          id: "plan-1",
          created_at: "2024-01-01T00:00:00.000Z",
          updated_at: "2024-01-01T00:00:00.000Z",
          execution_status: "idle",
          tags: { plannerparent: "main-1" },
        },
      ],
    });

    expect(page.items.map((item) => item.id)).toEqual(["main-1"]);
  });

  it("keeps ACP start requests unencrypted when no encrypted MCP values are present", () => {
    const agentSettings = {
      agent_kind: "acp",
      acp_server: "codex",
      acp_command: ["codex-acp"],
      acp_model: "gpt-5.5/medium",
      mcp_config: {
        publicDocs: {
          url: "https://docs.example.com/mcp",
          transport: "http",
        },
      },
    };
    const settings = makeSettings(agentSettings);

    const payload = buildStartConversationRequest({
      settings,
      encryptedAgentSettings: agentSettings,
      encryptedConversationSettings: settings.conversation_settings!,
      secretsEncrypted: true,
    });

    expect(payload.agent_settings!.agent_kind).toBe("acp");
    expect(payload.secrets_encrypted).toBeUndefined();
  });

  it("ships only allow-listed catalog skills to a OpenHands conversation context", () => {
    const settings = makeSettings({
      agent_kind: "openhands",
      llm: {
        model: "litellm_proxy/openai/gpt-5.5",
        api_key: "sk-test",
      },
      agent_context: {
        skills: [
          { name: "disabled-custom", content: "disabled" },
          { name: "enabled-custom", content: "enabled" },
        ],
      },
    });
    settings.disabled_skills = ["agent-memory", "disabled-custom"];

    const payload = buildStartConversationRequest({ settings });
    const skillNames = getAgentContextSkillNames(payload);

    // `agent-memory` is default-enabled, so this asserts the deny-list still
    // wins over the allow-list — the case that matters before the one-shot
    // migration has run.
    expect(skillNames).not.toContain("agent-memory");
    expect(skillNames).not.toContain("disabled-custom");
    // Skills already on the agent context are user-authored and stay opt-out.
    expect(skillNames).toContain("enabled-custom");
    // No `enabled_skills` on the settings means the curated default applies:
    // `add-skill` is flagged `defaultEnabled` in the catalog, `add-javadoc` is
    // not, and shipping every catalog skill is what OpenHands#16302 reported.
    expect(skillNames).toContain("add-skill");
    expect(skillNames).not.toContain("add-javadoc");

    expect(payload.agent_settings?.agent_context?.disabled_skills).toEqual([
      "agent-memory",
      "disabled-custom",
    ]);
  });

  it("ships only allow-listed catalog skills to a ACP conversation context", () => {
    const settings = makeSettings({
      agent_kind: "acp",
      acp_server: "codex",
      acp_command: ["codex-acp"],
      acp_model: "gpt-5.5/medium",
      agent_context: {
        skills: [
          { name: "disabled-custom", content: "disabled" },
          { name: "enabled-custom", content: "enabled" },
        ],
      },
    });
    settings.disabled_skills = ["agent-memory", "disabled-custom"];

    const payload = buildStartConversationRequest({ settings });
    const skillNames = getAgentContextSkillNames(payload);

    // `agent-memory` is default-enabled, so this asserts the deny-list still
    // wins over the allow-list — the case that matters before the one-shot
    // migration has run.
    expect(skillNames).not.toContain("agent-memory");
    expect(skillNames).not.toContain("disabled-custom");
    // Skills already on the agent context are user-authored and stay opt-out.
    expect(skillNames).toContain("enabled-custom");
    // No `enabled_skills` on the settings means the curated default applies:
    // `add-skill` is flagged `defaultEnabled` in the catalog, `add-javadoc` is
    // not, and shipping every catalog skill is what OpenHands#16302 reported.
    expect(skillNames).toContain("add-skill");
    expect(skillNames).not.toContain("add-javadoc");

    expect(payload.agent_settings?.agent_context?.disabled_skills).toEqual([
      "agent-memory",
      "disabled-custom",
    ]);
  });

  it("loads a catalog skill the opening slash command invokes", () => {
    // An automation card fills the chat input with the skill's own command
    // (`findAutomationCommand`), and 18 of the catalog's 24 slash commands
    // belong to skills that are off by default — without this the card would
    // silently do nothing.
    const settings = makeSettings({
      agent_kind: "openhands",
      llm: { model: "litellm_proxy/openai/gpt-5.5", api_key: "sk-test" },
    });

    const payload = buildStartConversationRequest({
      settings,
      query: "/standup-digest:setup",
    });

    expect(getAgentContextSkillNames(payload)).toContain(
      "slack-standup-digest",
    );
  });

  it("does not admit a skill named mid-sentence rather than invoked", () => {
    const settings = makeSettings({
      agent_kind: "openhands",
      llm: { model: "litellm_proxy/openai/gpt-5.5", api_key: "sk-test" },
    });

    const payload = buildStartConversationRequest({
      settings,
      query: "tell me what /standup-digest:setup would do",
    });

    expect(getAgentContextSkillNames(payload)).not.toContain(
      "slack-standup-digest",
    );
  });

  it("lets an invoked skill override a stored deny entry for that conversation", () => {
    const settings = makeSettings({
      agent_kind: "openhands",
      llm: { model: "litellm_proxy/openai/gpt-5.5", api_key: "sk-test" },
    });
    settings.disabled_skills = ["slack-standup-digest"];

    const payload = buildStartConversationRequest({
      settings,
      query: "/standup-digest:setup",
    });

    expect(getAgentContextSkillNames(payload)).toContain(
      "slack-standup-digest",
    );
  });
});

describe("buildStartPlanningConversationRequestWithEncryptedSettings", () => {
  const globallyActiveSettings = {
    agentSettings: {
      agent_kind: "openhands",
      llm: {
        model: "openhands/globally-active-model",
        api_key: "gAAAAAglobal-key",
        base_url: "https://global.example.com",
      },
    },
    conversationSettings: {},
    secretsEncrypted: true,
    skillEnablement: { disabledSkills: [] },
  };

  beforeEach(() => {
    vi.clearAllMocks();
    vi.mocked(SettingsService.getSettingsForConversation).mockResolvedValue(
      globallyActiveSettings,
    );
    vi.mocked(AgentProfilesService.listProfiles).mockResolvedValue({
      profiles: [
        {
          id: "profile-parent",
          name: "parent-profile",
          agent_kind: "openhands",
          revision: 1,
          llm_profile_ref: "parent-llm",
          mcp_server_refs: null,
        },
      ],
      active_agent_profile_id: "profile-other",
    });
    vi.mocked(ProfilesService.getProfile).mockResolvedValue({
      name: "parent-llm",
      api_key_set: true,
      config: {
        model: "openhands/parent-profile-model",
        api_key: "gAAAAAparent-key",
        base_url: "https://parent.example.com",
      },
    });
  });

  it("pins the planner to the parent's launched profile, not the globally active one", async () => {
    const payload =
      await buildStartPlanningConversationRequestWithEncryptedSettings({
        workingDir: "/workspace/project",
        parentConversationId: "parent-1",
        parentAgentProfileId: "profile-parent",
      });

    expect(ProfilesService.getProfile).toHaveBeenCalledWith(
      "parent-llm",
      "encrypted",
    );
    expect(payload.agent.llm).toMatchObject({
      model: "openhands/parent-profile-model",
      api_key: "gAAAAAparent-key",
      base_url: "https://parent.example.com",
    });
  });

  it("falls back to global settings when the parent was not launched from a profile", async () => {
    const payload =
      await buildStartPlanningConversationRequestWithEncryptedSettings({
        workingDir: "/workspace/project",
        parentConversationId: "parent-1",
        parentAgentProfileId: null,
      });

    expect(AgentProfilesService.listProfiles).not.toHaveBeenCalled();
    expect(payload.agent.llm).toMatchObject({
      model: "openhands/globally-active-model",
    });
  });

  it("prefers the parent conversation's current active_profile over its launched agent profile", async () => {
    // The conversation launched under "parent-profile" (-> "parent-llm"), but
    // was later switched to a different LLM profile via /model or the
    // agent's own SwitchLLMTool — active_profile tracks that switch.
    vi.mocked(ProfilesService.getProfile).mockImplementation(async (name) =>
      name === "switched-llm"
        ? {
            name: "switched-llm",
            api_key_set: true,
            config: {
              model: "openhands/switched-model",
              api_key: "gAAAAAswitched-key",
              base_url: "https://switched.example.com",
            },
          }
        : {
            name: "parent-llm",
            api_key_set: true,
            config: {
              model: "openhands/parent-profile-model",
              api_key: "gAAAAAparent-key",
              base_url: "https://parent.example.com",
            },
          },
    );

    const payload =
      await buildStartPlanningConversationRequestWithEncryptedSettings({
        workingDir: "/workspace/project",
        parentConversationId: "parent-1",
        parentActiveProfileName: "switched-llm",
        parentAgentProfileId: "profile-parent",
      });

    expect(ProfilesService.getProfile).toHaveBeenCalledWith(
      "switched-llm",
      "encrypted",
    );
    // The launched agent profile must not even be consulted once
    // active_profile resolves successfully.
    expect(AgentProfilesService.listProfiles).not.toHaveBeenCalled();
    expect(payload.agent.llm).toMatchObject({
      model: "openhands/switched-model",
      api_key: "gAAAAAswitched-key",
      base_url: "https://switched.example.com",
    });
  });

  it("falls back to the launched agent profile when active_profile can't be resolved", async () => {
    vi.mocked(ProfilesService.getProfile).mockImplementation(async (name) => {
      if (name === "dangling-llm") throw new Error("not found");
      return {
        name: "parent-llm",
        api_key_set: true,
        config: {
          model: "openhands/parent-profile-model",
          api_key: "gAAAAAparent-key",
          base_url: "https://parent.example.com",
        },
      };
    });

    const payload =
      await buildStartPlanningConversationRequestWithEncryptedSettings({
        workingDir: "/workspace/project",
        parentConversationId: "parent-1",
        parentActiveProfileName: "dangling-llm",
        parentAgentProfileId: "profile-parent",
      });

    expect(payload.agent.llm).toMatchObject({
      model: "openhands/parent-profile-model",
    });
  });

  it("falls back to global settings when the parent's profile reference dangles", async () => {
    vi.mocked(AgentProfilesService.listProfiles).mockResolvedValue({
      profiles: [],
      active_agent_profile_id: null,
    });

    const payload =
      await buildStartPlanningConversationRequestWithEncryptedSettings({
        workingDir: "/workspace/project",
        parentConversationId: "parent-1",
        parentAgentProfileId: "profile-parent",
      });

    expect(ProfilesService.getProfile).not.toHaveBeenCalled();
    expect(payload.agent.llm).toMatchObject({
      model: "openhands/globally-active-model",
    });
  });

  it("mirrors the parent's configured max_iterations instead of hardcoding a lower cap", async () => {
    vi.mocked(SettingsService.getSettingsForConversation).mockResolvedValue({
      ...globallyActiveSettings,
      conversationSettings: { max_iterations: 1000 },
    });

    const payload =
      await buildStartPlanningConversationRequestWithEncryptedSettings({
        workingDir: "/workspace/project",
        parentConversationId: "parent-1",
      });

    expect(payload.max_iterations).toBe(1000);
  });

  it("falls back to 500 when the parent has no configured max_iterations", async () => {
    const payload =
      await buildStartPlanningConversationRequestWithEncryptedSettings({
        workingDir: "/workspace/project",
        parentConversationId: "parent-1",
      });

    expect(payload.max_iterations).toBe(500);
  });

  it("applies the full skill enablement to the planner's agent context, mirroring the code agent", async () => {
    // Both lists, because they cover different populations (#16302): the
    // allow-list gates the bundled catalog, the deny-list still wins over it.
    // `add-javadoc` is a catalog skill outside the recommended defaults, so
    // it only loads because the allow-list names it.
    vi.mocked(SettingsService.getSettingsForConversation).mockResolvedValue({
      ...globallyActiveSettings,
      skillEnablement: {
        enabledSkills: ["add-javadoc", "agent-memory"],
        disabledSkills: ["agent-memory"],
      },
    });

    const payload =
      await buildStartPlanningConversationRequestWithEncryptedSettings({
        workingDir: "/workspace/project",
        parentConversationId: "parent-1",
      });

    const skillNames = getPlannerAgentContextSkillNames(payload);
    expect(skillNames).not.toContain("agent-memory");
    expect(skillNames).toContain("add-javadoc");
  });
});

describe("buildStartConversationRequest — agentProfileId path", () => {
  it("sends agent_profile_id and omits agent_settings (mutually exclusive)", () => {
    const settings = makeSettings({
      agent_kind: "openhands",
      llm: { model: "litellm_proxy/openai/gpt-5.5", api_key: "sk-test" },
    });

    const payload = buildStartConversationRequest({
      settings,
      agentProfileId: "profile-xyz",
      agentProfileKind: "openhands",
    });

    expect(payload.agent_profile_id).toBe("profile-xyz");
    expect(payload.agent_settings).toBeUndefined();
    expect(payload.client_tools.map((tool) => tool.name)).toEqual([
      CANVAS_UI_CLIENT_TOOL_NAME,
      LAUNCH_CHILD_CONVERSATION_TOOL_NAME,
    ]);
  });

  it("suppresses the ACP server tag when launching from a profile", () => {
    const agentSettings = {
      agent_kind: "acp",
      acp_server: "codex",
      acp_command: ["codex-acp"],
      acp_model: "gpt-5.5/medium",
    };

    // Without a profile the ACP server tag is stamped from settings...
    expect(
      buildStartConversationRequest({ settings: makeSettings(agentSettings) })
        .tags,
    ).toBeDefined();

    // ...but a profile launch resolves the server server-side, so the tag
    // (which may not match the launched profile) is omitted while the client
    // source telemetry tag is still stamped.
    const payload = buildStartConversationRequest({
      settings: makeSettings(agentSettings),
      agentProfileId: "profile-xyz",
    });
    expect(payload.tags).toEqual({
      [CLIENT_SOURCE_TAG_KEY]: AGENT_CANVAS_SOURCE,
    });
  });

  it("suppresses secrets_encrypted when launching from a profile", () => {
    const agentSettings = {
      agent_kind: "openhands",
      llm: {
        model: "litellm_proxy/openai/gpt-5.5",
        api_key: "gAAAAAencrypted-llm-api-key",
      },
      mcp_config: {
        mcpServers: {
          linear: {
            url: "https://mcp.linear.app/mcp",
            transport: "http",
            headers: { Authorization: encryptedValue },
          },
        },
      },
    };
    const settings = makeSettings(agentSettings);

    // Same inputs without a profile would set secrets_encrypted (covered
    // above); the profile path defers secret resolution to the server.
    const payload = buildStartConversationRequest({
      settings,
      encryptedAgentSettings: agentSettings,
      encryptedConversationSettings: settings.conversation_settings!,
      secretsEncrypted: true,
      agentProfileId: "profile-xyz",
    });

    expect(payload.secrets_encrypted).toBeUndefined();
  });
});