opencode-ahp 0.1.9 → 0.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-ahp",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "description": "OpenCode V2 plugin: hosts Agent Host Protocol (AHP) 1.0.0 in-process so opencode is a native agent in the VS Code Agents window",
5
5
  "type": "module",
6
6
  "exports": {
package/src/bridge.ts CHANGED
@@ -1383,7 +1383,6 @@ export function pastTenseMessageFor(toolName: string, inputText: string | undefi
1383
1383
  * The "after" side of uncommitted/session changes is the live disk path
1384
1384
  * itself (`file://`), read through `resourceRead` at view time.
1385
1385
  */
1386
- const CONTENT_REF_PREFIX = "ahp-content://";
1387
1386
  const CONTENT_HEAD_MARKER = "ahp-content://head/";
1388
1387
  const CONTENT_SNAP_MARKER = "ahp-content://snap/";
1389
1388
 
package/src/host-base.ts CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  import { ActionType, ChangesetStatus, ActionEnvelope, ActionOrigin, ChatAction, ChatSummary, ChangesetAction, RootAction, SessionAction, SessionSummary, StateAction, TerminalAction } from "@microsoft/agent-host-protocol";
6
6
  import { buildSessionSummary, chatSummaryOf, directoryUri, sessionSummaryStatus, type FacadeSession } from "./bridge.js";
7
- import { ChannelRegistry, ChannelKind, ROOT_CHANNEL, channelKind, parseChangesetChannel, sessionIDOf } from "./channels.js";
7
+ import { ChannelRegistry, ChannelKind, ROOT_CHANNEL, channelKind, parseChangesetChannel } from "./channels.js";
8
8
  import { Sequencer } from "./seq.js";
9
9
  import { createChangesetStateStore, createRootStateStore, ChangesetStateStore, RootStateStore } from "./state.js";
10
10
  import { SocketState, ClientRecord, ChatRuntime, SessionRecord, TerminalRecord, HostOptions } from "./host-types.js";
@@ -3,7 +3,7 @@
3
3
  */
4
4
 
5
5
  import { HostTurns } from "./host-turns.js";
6
- import { ActionType, ResponsePartKind, ToolCallConfirmationReason, ToolCallStatus, SessionActiveClient, SessionToolConfirmationRequest, ToolCallConfirmationState, ToolCallState } from "@microsoft/agent-host-protocol";
6
+ import { ActionType, ResponsePartKind, ToolCallConfirmationReason, ToolCallStatus, SessionActiveClient, ToolCallConfirmationState, ToolCallState } from "@microsoft/agent-host-protocol";
7
7
  import { invocationMessageFor, FacadeClientTool, FacadeClientToolResult } from "./bridge.js";
8
8
  import { descriptionFor, findProvider, inputSchemaFor, toolUnion, ClientToolInvocation } from "./client-tools.js";
9
9
  import { ChatRuntime, SessionRecord } from "./host-types.js";
@@ -3,7 +3,7 @@
3
3
  */
4
4
 
5
5
  import { HostSessions } from "./host-sessions.js";
6
- import { ActionType, SessionInputRequestKind, SessionStatus, ToolCallStatus, isClientDispatchable, ActionOrigin, ChatToolCallCompleteAction, ChatToolCallConfirmedAction, ChatToolCallContentChangedAction, ChatToolCallResultConfirmedAction, ChatTurnCancelledAction, ChatTurnStartedAction, ChangesetFilesReviewChangedAction, Message, SessionActiveClient, SessionCustomizationToggledAction, SessionMcpServerStartRequestedAction, SessionMcpServerStopRequestedAction, SessionToolConfirmationRequest, StateAction, ToolCallResult } from "@microsoft/agent-host-protocol";
6
+ import { ActionType, SessionInputRequestKind, ToolCallStatus, isClientDispatchable, ActionOrigin, ChatToolCallCompleteAction, ChatToolCallConfirmedAction, ChatToolCallContentChangedAction, ChatToolCallResultConfirmedAction, ChatTurnCancelledAction, ChatTurnStartedAction, ChangesetFilesReviewChangedAction, Message, SessionActiveClient, SessionCustomizationToggledAction, SessionMcpServerStartRequestedAction, SessionMcpServerStopRequestedAction, SessionToolConfirmationRequest, StateAction, ToolCallResult } from "@microsoft/agent-host-protocol";
7
7
  import { FacadeDelivery, MCP_CUSTOMIZATION_ID_PREFIX } from "./bridge.js";
8
8
  import { normalizeToolDefinitions } from "./client-tools.js";
9
9
  import { channelKind, parseChangesetChannel } from "./channels.js";
@@ -264,7 +264,7 @@ export class HostEvents extends HostDispatch {
264
264
  // tool's terminal event (before the turn guard — a command that
265
265
  // finishes after its turn was torn down still exits its
266
266
  // terminal; no-op when the call never opened one).
267
- this.finishTerminal(chat, ev.id, { ok: true, content: ev.content });
267
+ this.finishTerminal(ev.id, { ok: true, content: ev.content });
268
268
  if (chat.currentTurnId === null) return;
269
269
  this.ensureToolReady(chat, ev.id, false);
270
270
  if (this.toolCallTerminal(chat, ev.id)) {
@@ -293,7 +293,7 @@ export class HostEvents extends HostDispatch {
293
293
  return;
294
294
  }
295
295
  case "tool.failed": {
296
- this.finishTerminal(chat, ev.id, { ok: false, message: ev.error.message });
296
+ this.finishTerminal(ev.id, { ok: false, message: ev.error.message });
297
297
  if (chat.currentTurnId === null) return;
298
298
  this.ensureToolReady(chat, ev.id, false);
299
299
  if (this.toolCallTerminal(chat, ev.id)) {
package/src/host-rpc.ts CHANGED
@@ -171,6 +171,9 @@ export class HostRpc extends HostEvents {
171
171
  case "createSession":
172
172
  await this.handleCreateSession(socketId, msg);
173
173
  break;
174
+ case "disposeSession":
175
+ await this.handleDisposeSession(socketId, msg);
176
+ break;
174
177
  case "createChat":
175
178
  await this.handleCreateChat(socketId, msg);
176
179
  break;
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Session section (design §7): `createSession`/`listSessions`/`resolveSessionConfig`, external session registration (TUI/restart resurfacing, AHP markers, peer-chat re-attach), and record removal.
2
+ * Session section (design §7): `createSession`/`disposeSession`/`listSessions`/`resolveSessionConfig`, external session registration (TUI/restart resurfacing, AHP markers, peer-chat re-attach), and record removal.
3
3
  */
4
4
 
5
5
  import { HostChats } from "./host-chats.js";
@@ -35,7 +35,7 @@ import { ROOT_CHANNEL, chatChannelURI, isSessionChannel, sessionChannelURI, sess
35
35
  import { ERROR_INVALID_PARAMS } from "./handshake.js";
36
36
  import { isRecord } from "./options.js";
37
37
  import { createSessionStateStore } from "./state.js";
38
- import { ERROR_INTERNAL, ERROR_NO_PROVIDER, ERROR_PERMISSION_DENIED, ERROR_SESSION_EXISTS } from "./host-errors.js";
38
+ import { ERROR_INTERNAL, ERROR_NO_PROVIDER, ERROR_PERMISSION_DENIED, ERROR_SESSION_EXISTS, ERROR_SESSION_NOT_FOUND } from "./host-errors.js";
39
39
  import { RpcMessage, SessionRecord } from "./host-types.js";
40
40
  import { EXTERNAL_REGISTER_GRACE_MS, AHP_METADATA_KEY, parseAhpMarker, errorMessage, firstEnabledModelRef, normalizeActiveClient } from "./host-util.js";
41
41
 
@@ -358,6 +358,58 @@ export class HostSessions extends HostChats {
358
358
  this.sendFrame(socketId, { jsonrpc: "2.0", id: msg.id ?? null, result: null });
359
359
  }
360
360
 
361
+ /**
362
+ * `disposeSession` (AHP 1.0.0 session-channel command): dispose an AHP
363
+ * session and every opencode backend behind it — the default backend
364
+ * and each peer chat's backend. Result: `null`; the client's removal
365
+ * signal is the `root/sessionRemoved` notification `removeRecord`
366
+ * publishes (with the `root/activeSessionsChanged` count update),
367
+ * which always precedes the response. The channel is accepted in the
368
+ * same standard/legacy forms `createSession` echoes back, and an
369
+ * unknown channel answers -32001 (a channel that was a session but is
370
+ * already gone — e.g. disposed twice or deleted in the TUI first).
371
+ *
372
+ * The AHP-side unbind goes FIRST (the same race guard as
373
+ * `disposeChatBackend`): opencode's `session.deleted` event for each
374
+ * backend races this call through the event pump, and the pump's
375
+ * `byOpencode` guard must see the bindings already dropped — for the
376
+ * default backend a late binding would run `removeRecord` a second
377
+ * time (duplicate `root/sessionRemoved`), for a peer it would publish
378
+ * a `session/chatRemoved` for a chat whose record is already gone.
379
+ * `removeRecord` fires the peer backends' removals on the way (its
380
+ * standing behavior for the external-deletion path); the default
381
+ * backend is removed here, awaited, so a `listSessions` after the
382
+ * response does not resurface it lazily. R2: a failed backend removal
383
+ * still drops the AHP session — the lingering opencode session shows
384
+ * up in the TUI (and lazily in `listSessions`) until deleted there.
385
+ */
386
+ protected async handleDisposeSession(socketId: number, msg: RpcMessage): Promise<void> {
387
+ const params = isRecord(msg.params) ? msg.params : {};
388
+ const channel = params["channel"];
389
+ if (typeof channel !== "string" || !isSessionChannel(channel)) {
390
+ this.sendError(
391
+ socketId,
392
+ msg.id,
393
+ ERROR_INVALID_PARAMS,
394
+ "disposeSession requires a session channel (ahp-session:/… or <provider>:/…)",
395
+ );
396
+ return;
397
+ }
398
+ const rec = this.byChannel.get(channel);
399
+ if (rec === undefined) {
400
+ this.sendError(socketId, msg.id, ERROR_SESSION_NOT_FOUND, `no such session: ${channel}`);
401
+ return;
402
+ }
403
+ const defaultBackend = rec.sessionID;
404
+ this.removeRecord(rec);
405
+ try {
406
+ await this.opts.facade.removeSession(defaultBackend);
407
+ } catch (err) {
408
+ this.log(`failed to remove the session's opencode session ${defaultBackend}: ${errorMessage(err)}`);
409
+ }
410
+ this.sendFrame(socketId, { jsonrpc: "2.0", id: msg.id ?? null, result: null });
411
+ }
412
+
361
413
  /**
362
414
  * `listSessions` (AHP 1.0.0 root command): all opencode sessions,
363
415
  * most-recently-modified first, with in-memory cursor pagination.
@@ -603,7 +603,6 @@ export class HostTerminals extends HostResources {
603
603
  * record itself is retained (retention); only the state moves on.
604
604
  */
605
605
  protected finishTerminal(
606
- chat: ChatRuntime,
607
606
  toolCallId: string,
608
607
  result: { ok: true; content: FacadeToolContent[] } | { ok: false; message: string },
609
608
  ): void {
package/src/host-types.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  * Record types shared by the host section classes: the per-socket, per-client, per-session, per-chat and per-terminal state shapes, and the HostOptions the Host is constructed with.
3
3
  */
4
4
 
5
- import { ActionType, ReconnectResultType, Implementation, SessionSummary } from "@microsoft/agent-host-protocol";
5
+ import { Implementation } from "@microsoft/agent-host-protocol";
6
6
  import { OpencodeFacade } from "./bridge.js";
7
7
  import { ClientToolInvocation } from "./client-tools.js";
8
8
  import { ChatStateStore, ChangesetStateStore, SessionStateStore, TerminalStateStore } from "./state.js";
@@ -31,10 +31,13 @@ export interface PathPolicyEnv {
31
31
  * list is the unrestricted policy.
32
32
  */
33
33
  export class PathPolicy {
34
- private constructor(
35
- private readonly prefixes: readonly string[],
36
- private readonly env?: PathPolicyEnv,
37
- ) {}
34
+ private readonly prefixes: readonly string[];
35
+ private readonly env?: PathPolicyEnv;
36
+
37
+ private constructor(prefixes: readonly string[], env?: PathPolicyEnv) {
38
+ this.prefixes = prefixes;
39
+ this.env = env;
40
+ }
38
41
 
39
42
  /** No prefixes — every path is permitted. */
40
43
  static unrestricted(): PathPolicy {