|
| 1 | +import {afterEach, describe, expect, it} from 'vitest'; |
| 2 | +import {connectSession, settle, type PromptSession, type TranscriptEntry} from './v2-prompt-harness'; |
| 3 | +import type * as acpV2 from '@agentclientprotocol/sdk/experimental/v2'; |
| 4 | +import type {ServerNotification} from '../../app-server'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Codex reports every change under a skill root as `skills/changed`, naming nothing that changed. The adapter |
| 8 | + * lists the skills again and publishes the slash commands, so a skill that appears while a session is open - a |
| 9 | + * new file, a new symlink, a changed link target - becomes a command right away instead of only after the next |
| 10 | + * completed turn. |
| 11 | + */ |
| 12 | +describe('skills/changed republishes the available commands', () => { |
| 13 | + let closeClient: (() => void) | null = null; |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + closeClient?.(); |
| 17 | + closeClient = null; |
| 18 | + }); |
| 19 | + |
| 20 | + const skillsChanged = (): ServerNotification => |
| 21 | + ({method: "skills/changed", params: {}} as unknown as ServerNotification); |
| 22 | + |
| 23 | + const skills = (...names: string[]) => ({ |
| 24 | + data: [{ |
| 25 | + cwd: "/workspace", |
| 26 | + errors: [], |
| 27 | + skills: names.map(name => ({ |
| 28 | + name, |
| 29 | + description: name, |
| 30 | + shortDescription: name, |
| 31 | + path: `/workspace/.agents/skills/${name}`, |
| 32 | + scope: "project" as const, |
| 33 | + enabled: true, |
| 34 | + pluginId: null, |
| 35 | + })), |
| 36 | + }], |
| 37 | + }); |
| 38 | + |
| 39 | + const commandNames = (transcript: TranscriptEntry[]): string[][] => |
| 40 | + transcript.flatMap(entry => |
| 41 | + "sessionUpdate" in entry && entry.sessionUpdate.sessionUpdate === "available_commands_update" |
| 42 | + ? [(entry.sessionUpdate.availableCommands as acpV2.AvailableCommand[]).map(command => command.name)] |
| 43 | + : []); |
| 44 | + |
| 45 | + /** Waits out the publish the notification debounces, plus the wire round-trip behind it. */ |
| 46 | + const settleSkillsPublish = async (client: PromptSession) => { |
| 47 | + await new Promise(resolve => setTimeout(resolve, 400)); |
| 48 | + await settle(); |
| 49 | + }; |
| 50 | + |
| 51 | + it('publishes a skill that appeared before any turn has run', async () => { |
| 52 | + const client = await connectSession(); |
| 53 | + closeClient = () => client.connection.close(); |
| 54 | + client.setCodexResponse("skills/list", async () => skills("deploy")); |
| 55 | + const start = client.transcript.length; |
| 56 | + |
| 57 | + client.emit(skillsChanged()); |
| 58 | + await settleSkillsPublish(client); |
| 59 | + |
| 60 | + expect(commandNames(client.transcript.slice(start)).at(-1)).toContain("$deploy"); |
| 61 | + }); |
| 62 | + |
| 63 | + it('publishes once for a burst of changes, and nothing when the skills are the same', async () => { |
| 64 | + const client = await connectSession(); |
| 65 | + closeClient = () => client.connection.close(); |
| 66 | + let listed = 0; |
| 67 | + client.setCodexResponse("skills/list", async () => { |
| 68 | + listed += 1; |
| 69 | + return skills("deploy"); |
| 70 | + }); |
| 71 | + |
| 72 | + client.emit(skillsChanged()); |
| 73 | + client.emit(skillsChanged()); |
| 74 | + client.emit(skillsChanged()); |
| 75 | + await settleSkillsPublish(client); |
| 76 | + |
| 77 | + expect(listed).toBe(1); |
| 78 | + const start = client.transcript.length; |
| 79 | + |
| 80 | + // The same skills: the list is read again, but the client already has those commands. |
| 81 | + client.emit(skillsChanged()); |
| 82 | + await settleSkillsPublish(client); |
| 83 | + |
| 84 | + expect(listed).toBe(2); |
| 85 | + expect(commandNames(client.transcript.slice(start))).toEqual([]); |
| 86 | + }); |
| 87 | + |
| 88 | + it('keeps the newer list when a slower earlier publish answers last', async () => { |
| 89 | + const client = await connectSession(); |
| 90 | + closeClient = () => client.connection.close(); |
| 91 | + let listed = 0; |
| 92 | + client.setCodexResponse("skills/list", async () => { |
| 93 | + listed += 1; |
| 94 | + if (listed === 1) { |
| 95 | + // The first list is answered long after the second one, and it carries the older skills. |
| 96 | + await new Promise(resolve => setTimeout(resolve, 1_000)); |
| 97 | + return skills("build"); |
| 98 | + } |
| 99 | + return skills("build", "deploy"); |
| 100 | + }); |
| 101 | + const start = client.transcript.length; |
| 102 | + |
| 103 | + client.emit(skillsChanged()); |
| 104 | + await new Promise(resolve => setTimeout(resolve, 400)); |
| 105 | + client.emit(skillsChanged()); |
| 106 | + await new Promise(resolve => setTimeout(resolve, 1_500)); |
| 107 | + await settle(); |
| 108 | + |
| 109 | + expect(listed).toBe(2); |
| 110 | + expect(commandNames(client.transcript.slice(start)).at(-1)).toContain("$deploy"); |
| 111 | + }); |
| 112 | + |
| 113 | + it('publishes nothing for a session that closed before its change settled', async () => { |
| 114 | + const client = await connectSession(); |
| 115 | + closeClient = () => client.connection.close(); |
| 116 | + let listed = 0; |
| 117 | + client.setCodexResponse("skills/list", async () => { |
| 118 | + listed += 1; |
| 119 | + return skills("deploy"); |
| 120 | + }); |
| 121 | + const listedAtClose = () => listed; |
| 122 | + |
| 123 | + client.emit(skillsChanged()); |
| 124 | + await client.request("session/close", {sessionId: "thread-1"}); |
| 125 | + const before = listedAtClose(); |
| 126 | + await settleSkillsPublish(client); |
| 127 | + |
| 128 | + expect(listed).toBe(before); |
| 129 | + }); |
| 130 | +}); |
0 commit comments