opencode-ahp 0.1.6 → 0.1.7

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.6",
3
+ "version": "0.1.7",
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
@@ -855,6 +855,63 @@ export function userMessage(text: string, model?: string, agentUri?: string): Me
855
855
  return message;
856
856
  }
857
857
 
858
+ /** Bitmask covering the mutually-exclusive activity bits (bits 0–4). */
859
+ const STATUS_ACTIVITY_MASK = (1 << 5) - 1;
860
+
861
+ /**
862
+ * The bit that uniquely marks an input-needed status: `InputNeeded` (24)
863
+ * *includes* the `InProgress` bit (8), so `status & InputNeeded` also
864
+ * matches plain in-progress — bit 16 (`InputNeeded & ~InProgress`) is the
865
+ * discriminator (the same bit the official reducer's
866
+ * `withInputNeededStatus` clears on drain).
867
+ */
868
+ const INPUT_NEEDED_MARKER = (SessionStatus.InputNeeded & ~SessionStatus.InProgress) as SessionStatus;
869
+
870
+ /**
871
+ * Derive `SessionSummary.status` per the AHP aggregation rules: the
872
+ * activity bits (0–4) come from the default chat, else the most recently
873
+ * modified chat; they are **promoted** to `InputNeeded` whenever any chat
874
+ * is blocked on input, else to `Error` whenever any chat is in an error
875
+ * state. The orthogonal flags (`IsRead` / `IsArchived`) are session-scoped
876
+ * and are preserved from `sessionStatus`.
877
+ *
878
+ * The activity bits are deliberately NOT taken from `sessionStatus`: the
879
+ * session reducer only reaches them through the input-queue path, and that
880
+ * path cannot lower them back to `Idle` once a turn ends (draining the
881
+ * queue leaves `InProgress` behind — the official reducer's "unblocked
882
+ * turn falls back to `InProgress`" assumes the session was promoted to
883
+ * `InProgress` when the turn started, which it is not; only the chat is).
884
+ * The chat summaries are the activity source of truth.
885
+ */
886
+ export function sessionSummaryStatus(
887
+ sessionStatus: SessionStatus,
888
+ chats: readonly ChatSummary[],
889
+ defaultChat: string | undefined,
890
+ ): SessionStatus {
891
+ const flags = sessionStatus & ~STATUS_ACTIVITY_MASK;
892
+ let source = defaultChat !== undefined ? chats.find((c) => c.resource === defaultChat) : undefined;
893
+ if (source === undefined) {
894
+ for (const c of chats) {
895
+ if (source === undefined || c.modifiedAt > source.modifiedAt) source = c;
896
+ }
897
+ }
898
+ let promoted: SessionStatus | undefined;
899
+ for (const c of chats) {
900
+ if (c.status & INPUT_NEEDED_MARKER) {
901
+ promoted = SessionStatus.InputNeeded;
902
+ break;
903
+ }
904
+ if (c.status & SessionStatus.Error) promoted = SessionStatus.Error;
905
+ }
906
+ const activity =
907
+ promoted !== undefined
908
+ ? promoted
909
+ : source !== undefined
910
+ ? ((source.status & STATUS_ACTIVITY_MASK) as SessionStatus)
911
+ : SessionStatus.Idle;
912
+ return (flags | activity) as SessionStatus;
913
+ }
914
+
858
915
  /**
859
916
  * Build the `SessionSummary` for root notifications / `listSessions` from
860
917
  * a live record's denormalized fields. `chats` is the full catalog in
package/src/host-base.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  */
4
4
 
5
5
  import { ActionType, ChangesetStatus, ActionEnvelope, ActionOrigin, ChatAction, ChatSummary, ChangesetAction, RootAction, SessionAction, SessionSummary, StateAction, TerminalAction } from "@microsoft/agent-host-protocol";
6
- import { buildSessionSummary, chatSummaryOf, directoryUri, FacadeSession } from "./bridge.js";
6
+ import { buildSessionSummary, chatSummaryOf, directoryUri, sessionSummaryStatus, type FacadeSession } from "./bridge.js";
7
7
  import { ChannelRegistry, ChannelKind, ROOT_CHANNEL, channelKind, parseChangesetChannel, sessionIDOf } from "./channels.js";
8
8
  import { Sequencer } from "./seq.js";
9
9
  import { createChangesetStateStore, createRootStateStore, ChangesetStateStore, RootStateStore } from "./state.js";
@@ -154,7 +154,9 @@ export class HostBase {
154
154
  * Propagate a changed chat summary into the session store's chat catalog
155
155
  * (`session/chatUpdated`) — the session status itself is only
156
156
  * reducer-reachable for read/archive/input-needed bits, so the chat
157
- * summary is the activity source of truth.
157
+ * summary is the activity source of truth (the summary-level
158
+ * `SessionSummary.status` is aggregated from the chats by
159
+ * `sessionSummaryStatus`, never read back from the session store).
158
160
  */
159
161
  private syncChatSummary(rec: SessionRecord, chatURI: string): void {
160
162
  const chat = rec.chats.get(chatURI)?.chatStore.state;
@@ -225,7 +227,12 @@ export class HostBase {
225
227
  channel: rec.channel,
226
228
  provider: rec.provider,
227
229
  title: s.title,
228
- status: s.status,
230
+ // Activity bits aggregated from the chat summaries (AHP rule): the
231
+ // session store's own status cannot source them — its reducer only
232
+ // reaches them through the input-queue path, which leaves
233
+ // `InProgress` set after an input-needed round trip when the turn
234
+ // ends. The `IsRead`/`IsArchived` flags stay session-scoped.
235
+ status: sessionSummaryStatus(s.status, chats, rec.defaultChatURI),
229
236
  activity: s.activity,
230
237
  createdAt: rec.createdAt,
231
238
  modifiedAt,