Skip to content

Commit 6a8b4e4

Browse files
authored
Implement ACP bridge lifecycle handlers (#78880)
* Implement ACP bridge lifecycle handlers * docs: add acp smoke evidence example * docs: trim acp smoke example * docs: remove acp pr plan file * fix: tighten acp session list filters --------- Co-authored-by: Alex Knight <[email protected]>
1 parent 0fca665 commit 6a8b4e4

7 files changed

Lines changed: 832 additions & 44 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Docs: https://docs.openclaw.ai
3131
- Sessions CLI: show the selected agent runtime in the `openclaw sessions` table so terminal output matches the runtime visibility already present in JSON/status surfaces. Thanks @vincentkoc.
3232
- ACPX/Codex: preserve trusted Codex project declarations when launching isolated Codex ACP sessions, avoiding interactive trust prompts in headless runs. Thanks @Stedyclaw.
3333
- ACPX/Codex: reap stale OpenClaw-owned ACPX/Codex ACP process trees on startup and after ACP session close, preventing orphaned harness processes from slowing the Gateway. Thanks @91wan.
34+
- ACP bridge: implement stable session list, resume, and close handlers so ACP clients can page Gateway sessions, rebind existing sessions without replay, and close bridge sessions cleanly. Thanks @amknight.
3435
- ACP sessions: allow parent agents to inspect and message their own spawned cross-agent ACP sessions without enabling broad agent-to-agent visibility. Thanks @barronlroth.
3536
- Talk/voice: unify realtime relay, transcription relay, managed-room handoff, Voice Call, Google Meet, VoiceClaw, and native clients around a shared Talk session controller and add the Gateway-managed `talk.session.*` RPC surface.
3637
- Diagnostics/Talk: export bounded Talk lifecycle/audio metrics and session recovery metrics through OpenTelemetry and Prometheus without exposing transcripts, audio payloads, room ids, turn ids, or session ids.

‎docs/cli/acp.md‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ Quick rule:
4242
| ACP area | Status | Notes |
4343
| --------------------------------------------------------------------- | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
4444
| `initialize`, `newSession`, `prompt`, `cancel` | Implemented | Core bridge flow over stdio to Gateway chat/send + abort. |
45-
| `listSessions`, slash commands | Implemented | Session list works against Gateway session state; commands are advertised via `available_commands_update`. |
45+
| `listSessions`, slash commands | Implemented | Session list works against Gateway session state with bounded cursor pagination and `cwd` filtering where Gateway session rows carry workspace metadata; commands are advertised via `available_commands_update`. |
46+
| `resumeSession`, `closeSession` | Implemented | Resume rebinds an ACP session to an existing Gateway session without replaying history. Close cancels active bridge work, resolves pending prompts as cancelled, and releases bridge session state. |
4647
| `loadSession` | Partial | Rebinds the ACP session to a Gateway session key and replays stored user/assistant text history. Tool/system history is not reconstructed yet. |
4748
| Prompt content (`text`, embedded `resource`, images) | Partial | Text/resources are flattened into chat input; images become Gateway attachments. |
4849
| Session modes | Partial | `session/set_mode` is supported and the bridge exposes initial Gateway-backed session controls for thought level, tool verbosity, reasoning, usage detail, and elevated actions. Broader ACP-native mode/config surfaces are still out of scope. |
@@ -120,6 +121,50 @@ Permission model (client debug mode):
120121
- Server-provided `toolCall.kind` is treated as untrusted metadata (not an authorization source).
121122
- This ACP bridge policy is separate from ACPX harness permissions. If you run OpenClaw through the `acpx` backend, `plugins.entries.acpx.config.permissionMode=approve-all` is the break-glass "yolo" switch for that harness session.
122123

124+
## Protocol smoke testing
125+
126+
For protocol-level debugging, start a Gateway with isolated state and drive
127+
`openclaw acp` over stdio with an ACP JSON-RPC client. Cover `initialize`,
128+
`session/new`, `session/list` with an absolute `cwd`, `session/resume`,
129+
`session/close`, duplicate close, and missing resume.
130+
131+
The proof should include the advertised lifecycle capabilities, a Gateway-backed
132+
session row, update notifications, and the Gateway `sessions.list` log:
133+
134+
```json
135+
{
136+
"initialize": {
137+
"protocolVersion": 1,
138+
"agentCapabilities": {
139+
"sessionCapabilities": {
140+
"list": {},
141+
"resume": {},
142+
"close": {}
143+
}
144+
}
145+
},
146+
"listSessions": {
147+
"sessions": [
148+
{
149+
"sessionId": "agent:main:acp-smoke",
150+
"cwd": "/path/to/workspace",
151+
"_meta": {
152+
"sessionKey": "agent:main:acp-smoke",
153+
"kind": "direct"
154+
}
155+
}
156+
],
157+
"nextCursor": null
158+
},
159+
"notifications": ["session_info_update", "available_commands_update", "usage_update"],
160+
"gatewayLogTail": ["[gateway] ready", "[ws] ⇄ res ✓ sessions.list 305ms"]
161+
}
162+
```
163+
164+
Avoid using `openclaw gateway call sessions.list` as the only ACP proof. That
165+
CLI path may request a fresh-token operator scope upgrade; ACP bridge
166+
correctness is proven by ACP stdio frames plus the Gateway `sessions.list` log.
167+
123168
## How to use this
124169

125170
Use ACP when an IDE (or other client) speaks Agent Client Protocol and you want

‎src/acp/protocol-schema.test.ts‎

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import { PROTOCOL_VERSION } from "@agentclientprotocol/sdk";
2+
import {
3+
zCloseSessionRequest,
4+
zInitializeRequest,
5+
zListSessionsRequest,
6+
zLoadSessionRequest,
7+
zNewSessionRequest,
8+
zPromptRequest,
9+
zResumeSessionRequest,
10+
zSessionNotification,
11+
} from "@agentclientprotocol/sdk/dist/schema/zod.gen.js";
12+
import { describe, expect, it } from "vitest";
13+
14+
type SchemaFixture = {
15+
name: string;
16+
schema: {
17+
safeParse: (input: unknown) => { success: boolean };
18+
};
19+
valid: unknown;
20+
invalid: unknown;
21+
};
22+
23+
const fixtures: SchemaFixture[] = [
24+
{
25+
name: "initialize",
26+
schema: zInitializeRequest,
27+
valid: {
28+
protocolVersion: PROTOCOL_VERSION,
29+
clientCapabilities: {
30+
fs: { readTextFile: false, writeTextFile: false },
31+
terminal: false,
32+
},
33+
},
34+
invalid: {
35+
protocolVersion: "1",
36+
clientCapabilities: {},
37+
},
38+
},
39+
{
40+
name: "session/new",
41+
schema: zNewSessionRequest,
42+
valid: {
43+
cwd: "/tmp/openclaw",
44+
mcpServers: [],
45+
},
46+
invalid: {
47+
cwd: 42,
48+
mcpServers: [],
49+
},
50+
},
51+
{
52+
name: "session/prompt",
53+
schema: zPromptRequest,
54+
valid: {
55+
sessionId: "session-1",
56+
prompt: [{ type: "text", text: "hello" }],
57+
},
58+
invalid: {
59+
sessionId: "session-1",
60+
prompt: [{ type: "text" }],
61+
},
62+
},
63+
{
64+
name: "session/update",
65+
schema: zSessionNotification,
66+
valid: {
67+
sessionId: "session-1",
68+
update: {
69+
sessionUpdate: "agent_message_chunk",
70+
content: { type: "text", text: "hello" },
71+
},
72+
},
73+
invalid: {
74+
update: {
75+
sessionUpdate: "agent_message_chunk",
76+
content: { type: "text", text: "hello" },
77+
},
78+
},
79+
},
80+
{
81+
name: "session/list",
82+
schema: zListSessionsRequest,
83+
valid: {
84+
cwd: "/tmp/openclaw",
85+
cursor: null,
86+
},
87+
invalid: {
88+
cwd: "/tmp/openclaw",
89+
cursor: 123,
90+
},
91+
},
92+
{
93+
name: "session/load",
94+
schema: zLoadSessionRequest,
95+
valid: {
96+
sessionId: "agent:main:work",
97+
cwd: "/tmp/openclaw",
98+
mcpServers: [],
99+
},
100+
invalid: {
101+
sessionId: "agent:main:work",
102+
mcpServers: [],
103+
},
104+
},
105+
{
106+
name: "session/resume",
107+
schema: zResumeSessionRequest,
108+
valid: {
109+
sessionId: "agent:main:work",
110+
cwd: "/tmp/openclaw",
111+
mcpServers: [],
112+
},
113+
invalid: {
114+
sessionId: "agent:main:work",
115+
cwd: 42,
116+
mcpServers: [],
117+
},
118+
},
119+
{
120+
name: "session/close",
121+
schema: zCloseSessionRequest,
122+
valid: {
123+
sessionId: "agent:main:work",
124+
},
125+
invalid: {
126+
sessionId: null,
127+
},
128+
},
129+
];
130+
131+
describe("ACP SDK protocol schema fixtures", () => {
132+
it.each(fixtures)("$name validates representative payloads", ({ schema, valid, invalid }) => {
133+
expect(schema.safeParse(valid).success).toBe(true);
134+
expect(schema.safeParse(invalid).success).toBe(false);
135+
});
136+
});

‎src/acp/session.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@ describe("acp session manager", () => {
3333
expect(store.getSessionByRunId("run-1")).toBeUndefined();
3434
});
3535

36+
it("deletes sessions and aborts active runs on close", () => {
37+
const session = store.createSession({
38+
sessionId: "close-me",
39+
sessionKey: "acp:close",
40+
cwd: "/tmp",
41+
});
42+
const controller = new AbortController();
43+
store.setActiveRun(session.sessionId, "run-close", controller);
44+
45+
expect(store.deleteSession(session.sessionId)).toBe(true);
46+
47+
expect(controller.signal.aborted).toBe(true);
48+
expect(store.hasSession(session.sessionId)).toBe(false);
49+
expect(store.getSessionByRunId("run-close")).toBeUndefined();
50+
});
51+
52+
it("reports false when deleting a missing session", () => {
53+
expect(store.deleteSession("missing")).toBe(false);
54+
});
55+
3656
it("refreshes existing session IDs instead of creating duplicates", () => {
3757
const first = store.createSession({
3858
sessionId: "existing",

‎src/acp/session.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type AcpSessionStore = {
99
setActiveRun: (sessionId: string, runId: string, abortController: AbortController) => void;
1010
clearActiveRun: (sessionId: string) => void;
1111
cancelActiveRun: (sessionId: string) => boolean;
12+
deleteSession: (sessionId: string) => boolean;
1213
clearAllSessionsForTest: () => void;
1314
};
1415

@@ -167,6 +168,8 @@ export function createInMemorySessionStore(options: AcpSessionStoreOptions = {})
167168
return true;
168169
};
169170

171+
const deleteSession: AcpSessionStore["deleteSession"] = (sessionId) => removeSession(sessionId);
172+
170173
const clearAllSessionsForTest: AcpSessionStore["clearAllSessionsForTest"] = () => {
171174
for (const session of sessions.values()) {
172175
session.abortController?.abort();
@@ -183,6 +186,7 @@ export function createInMemorySessionStore(options: AcpSessionStoreOptions = {})
183186
setActiveRun,
184187
clearActiveRun,
185188
cancelActiveRun,
189+
deleteSession,
186190
clearAllSessionsForTest,
187191
};
188192
}

0 commit comments

Comments
 (0)