@codemem/opencode-plugin 0.49.0-beta.4 → 0.49.0-beta.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.
@@ -9,6 +9,7 @@ export const V2_ADAPTER_DIAGNOSTICS = Object.freeze({
9
9
  contextCleanupTimeout: "v2_context_cleanup_timeout",
10
10
  contextRecallFailed: "v2_context_recall_failed",
11
11
  eventCaptureFailed: "v2_event_capture_failed",
12
+ sessionOwnershipUnavailable: "v2_session_ownership_unavailable",
12
13
  eventStreamCleanupTimeout: "v2_event_stream_cleanup_timeout",
13
14
  eventStreamEndedUnexpectedly: "v2_event_stream_ended_unexpectedly",
14
15
  eventStreamFailed: "v2_event_stream_failed",
@@ -11,6 +11,7 @@ import {
11
11
  } from "./memory-tools.js";
12
12
  import { createCodememRuntime } from "./runtime.js";
13
13
  import { registerV2NotificationBridge } from "./v2-notification-bridge.js";
14
+ import { readV2SessionOwnership } from "./v2-session-location.js";
14
15
 
15
16
  const DEFAULT_EVENT_TASK_TIMEOUT_MS = 250;
16
17
  const FAILED_TOOL_ERROR = Object.freeze({
@@ -604,31 +605,59 @@ const captureRuntimeTaskWithinTimeout = async ({
604
605
  return { completed, task };
605
606
  };
606
607
 
607
- const consumeEvents = async (
608
- context,
609
- signal,
610
- runtime,
611
- translator,
612
- { eventTaskTimeoutMs, scheduleCapture, waitForCaptureTask, waitForDiagnosticTask },
613
- ) => {
608
+ const CAPTURE_EVENT_TYPES = new Set([
609
+ "session.inbox.enqueued", "session.text.ended", "session.step.ended",
610
+ "session.created", "session.deleted", "session.execution.failed",
611
+ "session.execution.succeeded", "session.execution.interrupted",
612
+ ]);
613
+
614
+ const canCaptureSession = async (context, state, options, { sessionID, location }) => {
615
+ const ownership = await readV2SessionOwnership(context, sessionID, {
616
+ location,
617
+ signal: state.abortController.signal,
618
+ timeoutMs: options.eventTaskTimeoutMs,
619
+ });
620
+ if (ownership === "unavailable" && state.active) {
621
+ await reportDiagnosticWithinTimeout(state.runtime,
622
+ V2_ADAPTER_DIAGNOSTICS.sessionOwnershipUnavailable,
623
+ options.waitForDiagnosticTask, options.eventTaskTimeoutMs);
624
+ }
625
+ return state.active && ownership === "owned";
626
+ };
627
+
628
+ const captureOwnedEvent = async (context, state, options, event) => {
629
+ if (!CAPTURE_EVENT_TYPES.has(event?.type)) return;
630
+ if (!(await canCaptureSession(context, state, options, {
631
+ sessionID: event?.data?.sessionID, location: event?.location,
632
+ }))) return;
633
+ const { runtime, translator, scheduleCapture } = state;
634
+ const signal = state.abortController.signal;
635
+ const { eventTaskTimeoutMs, waitForCaptureTask, waitForDiagnosticTask } = options;
636
+ for (const translated of translator.translate(event)) {
637
+ const capture = await captureRuntimeTaskWithinTimeout({
638
+ runtime,
639
+ run: () => runtime.handleEvent(translated),
640
+ scheduleCapture,
641
+ diagnosticCode: V2_ADAPTER_DIAGNOSTICS.eventCaptureFailed,
642
+ timeoutMs: eventTaskTimeoutMs,
643
+ waitForCaptureTask,
644
+ waitForDiagnosticTask,
645
+ });
646
+ if (!capture.completed) {
647
+ await waitForCaptureOrAbort(capture.task, signal);
648
+ if (signal.aborted) return;
649
+ }
650
+ }
651
+ };
652
+
653
+ const consumeEvents = async (context, state, options) => {
654
+ const { runtime } = state;
655
+ const signal = state.abortController.signal;
656
+ const { eventTaskTimeoutMs, waitForDiagnosticTask } = options;
614
657
  try {
615
658
  for await (const event of context.event.subscribe({ signal })) {
616
659
  if (signal.aborted) break;
617
- for (const translated of translator.translate(event)) {
618
- const capture = await captureRuntimeTaskWithinTimeout({
619
- runtime,
620
- run: () => runtime.handleEvent(translated),
621
- scheduleCapture,
622
- diagnosticCode: V2_ADAPTER_DIAGNOSTICS.eventCaptureFailed,
623
- timeoutMs: eventTaskTimeoutMs,
624
- waitForCaptureTask,
625
- waitForDiagnosticTask,
626
- });
627
- if (!capture.completed) {
628
- await waitForCaptureOrAbort(capture.task, signal);
629
- if (signal.aborted) return;
630
- }
631
- }
660
+ await captureOwnedEvent(context, state, options, event);
632
661
  }
633
662
  if (!signal.aborted) {
634
663
  await reportDiagnosticWithinTimeout(
@@ -774,6 +803,7 @@ const registerContextHook = (context, state, options) =>
774
803
  try {
775
804
  await state.scheduleContext.schedule(contextScheduleKey(input), async () => {
776
805
  if (state.active) {
806
+ if (!(await canCaptureSession(context, state, options, { sessionID: input?.sessionID }))) return;
777
807
  await transformV2Context(state.runtime, input, { isActive: () => state.active });
778
808
  }
779
809
  });
@@ -803,6 +833,7 @@ const registerCaptureHook = (context, state, options) =>
803
833
  // Native memory tool output is retrieved memory; capturing it would distill it into new
804
834
  // memories. With the flag off, MCP-provided codemem tools keep their existing capture path.
805
835
  if (!state.active || (state.runtime.memoryTools && isNativeMemoryToolResult(input))) return;
836
+ if (!(await canCaptureSession(context, state, options, { sessionID: input?.sessionID }))) return;
806
837
  const translated = translateV2ToolResult(input);
807
838
  await captureRuntimeTaskWithinTimeout({
808
839
  runtime: state.runtime,
@@ -870,12 +901,7 @@ const activateAdapter = async (context, runtime, registrations, options) => {
870
901
  registrations.push(await registerContextHook(context, state, options));
871
902
  registrations.push(await registerCaptureHook(context, state, options));
872
903
  registrations.push(...(await registerV2Tools(context, runtime, () => state.active)));
873
- state.eventTask = consumeEvents(context, state.abortController.signal, runtime, state.translator, {
874
- eventTaskTimeoutMs: options.eventTaskTimeoutMs,
875
- scheduleCapture: state.scheduleCapture,
876
- waitForCaptureTask: options.waitForCaptureTask,
877
- waitForDiagnosticTask: options.waitForDiagnosticTask,
878
- });
904
+ state.eventTask = consumeEvents(context, state, options);
879
905
  } catch (error) {
880
906
  await rollBackActivation(state, options);
881
907
  // Preserve the setup error that explains why activation failed.
@@ -38,7 +38,7 @@ import {
38
38
 
39
39
  const TRUTHY_VALUES = ["1", "true", "yes"];
40
40
  const DISABLED_VALUES = ["0", "false", "off"];
41
- const PINNED_BACKEND_VERSION = "0.49.0-beta.4";
41
+ const PINNED_BACKEND_VERSION = "0.49.0-beta.7";
42
42
  const COMPAT_CHECK_DELAY_MS = 1500;
43
43
  const COMPAT_CHECK_CACHE_TTL_MS = 5 * 60 * 1000;
44
44
  const MAX_UPDATE_STATUS_BYTES = 16 * 1024;
@@ -83,6 +83,12 @@ const claimPluginRegistration = (cwd) => {
83
83
  }
84
84
  };
85
85
  };
86
+
87
+ const registrationDirectoryFor = ({ directory, cwd, hostGeneration }) => {
88
+ // V2 loads plugins per location, including nested locations in one worktree.
89
+ if (hostGeneration === "v2" && directory) return directory;
90
+ return cwd;
91
+ };
86
92
  // Release an unread response body without surfacing cancellation failures.
87
93
  const discardResponseBody = (response) => {
88
94
  try {
@@ -2418,7 +2424,7 @@ export const createCodememRuntime = async ({ location, host, hostGeneration = nu
2418
2424
  if (pluginIgnored) {
2419
2425
  return null;
2420
2426
  }
2421
- const releasePluginRegistration = claimPluginRegistration(cwd);
2427
+ const releasePluginRegistration = claimPluginRegistration(registrationDirectoryFor({ directory, cwd, hostGeneration }));
2422
2428
  if (!releasePluginRegistration) {
2423
2429
  await log("warn", "codemem duplicate plugin registration skipped", {
2424
2430
  next_action: "remove either the configured npm plugin or the project-local Codemem plugin",
@@ -0,0 +1,41 @@
1
+ import { isAbsolute, resolve } from "node:path";
2
+
3
+ const validLocation = (value) => value
4
+ && typeof value.directory === "string"
5
+ && isAbsolute(value.directory)
6
+ && (value.workspaceID === undefined
7
+ || (typeof value.workspaceID === "string" && value.workspaceID.length > 0));
8
+
9
+ const sameLocation = (a, b) => resolve(a.directory) === resolve(b.directory)
10
+ && (a.workspaceID ?? null) === (b.workspaceID ?? null);
11
+
12
+ /**
13
+ * Host event envelopes retain their publication location across session moves.
14
+ * Hooks and envelopes without a location need a fresh session read.
15
+ * Message text, tool arguments and parent-session IDs are not location evidence.
16
+ */
17
+ export const readV2SessionOwnership = async (context, sessionID, {
18
+ location,
19
+ signal,
20
+ timeoutMs,
21
+ }) => {
22
+ if (typeof sessionID !== "string" || !sessionID.trim() || !validLocation(context.location)) {
23
+ return "unavailable";
24
+ }
25
+ if (location !== undefined) {
26
+ if (!validLocation(location)) return "unavailable";
27
+ return sameLocation(context.location, location) ? "owned" : "foreign";
28
+ }
29
+ if (typeof context.session?.get !== "function") return "unavailable";
30
+ try {
31
+ const session = await context.session.get({ sessionID }, {
32
+ signal: AbortSignal.any([signal, AbortSignal.timeout(Math.max(1, timeoutMs))]),
33
+ });
34
+ if (session?.id !== sessionID || !validLocation(session.location)) return "unavailable";
35
+ if (typeof session.projectID !== "string") return "unavailable";
36
+ if (session.projectID !== context.location.project?.id) return "foreign";
37
+ return sameLocation(context.location, session.location) ? "owned" : "foreign";
38
+ } catch {
39
+ return "unavailable";
40
+ }
41
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codemem/opencode-plugin",
3
- "version": "0.49.0-beta.4",
3
+ "version": "0.49.0-beta.7",
4
4
  "description": "CodeMem plugin for OpenCode",
5
5
  "type": "module",
6
6
  "main": "./index.js",
@@ -42,7 +42,7 @@
42
42
  "@babel/parser": "8.1.0",
43
43
  "@opencode/cli": "2.0.24",
44
44
  "@types/node": "^24.12.4",
45
- "codemem": "^0.49.0-beta.4",
45
+ "codemem": "^0.49.0-beta.7",
46
46
  "vitest": "^4.1.11"
47
47
  },
48
48
  "repository": {