nemo-fabric-adapters-opencode 0.4.0-beta.3 → 0.5.0-beta.1

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/README.md CHANGED
@@ -16,14 +16,14 @@ To use a published adapter release, install the adapter and its compatible
16
16
  OpenCode SDK in the project that owns the NeMo Fabric configuration:
17
17
 
18
18
  ```bash
19
- npm install nemo-fabric-adapters-opencode @opencode/core@2.0.3 @opencode/sdk@2.0.3
19
+ npm install nemo-fabric-adapters-opencode @opencode/core@2.0.23 @opencode/sdk@2.0.23
20
20
  ```
21
21
 
22
22
  OpenCode Core and the OpenCode SDK are optional peers, exact-pinned to the
23
23
  supported OpenCode release. Starting the adapter without the compatible
24
24
  packages reports a stable harness-unavailable error.
25
25
 
26
- OpenCode 2.0.3 does not support npm's `install-strategy=nested`. The documented
26
+ OpenCode 2.0.23 does not support npm's `install-strategy=nested`. The documented
27
27
  command requires npm's default hoisted layout.
28
28
 
29
29
  ## Supported Configuration
@@ -1,6 +1,6 @@
1
1
  // SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- // OpenCode v2.0.3 always emits prompt_cache_key for its OpenAI-compatible
3
+ // The supported OpenCode SDK emits prompt_cache_key for its OpenAI-compatible
4
4
  // protocol. Some compatible providers reject that OpenAI-specific extension,
5
5
  // so a loopback proxy removes it only for explicitly configured endpoints.
6
6
  import { createServer } from "node:http";
@@ -2,7 +2,8 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import { LifecycleError } from "nemo-fabric-adapters-common";
4
4
  import { randomUUID } from "node:crypto";
5
- import { realpath, stat } from "node:fs/promises";
5
+ import { cp, mkdtemp, readdir, realpath, rm, stat } from "node:fs/promises";
6
+ import { tmpdir } from "node:os";
6
7
  import { isAbsolute, join, relative, resolve, sep } from "node:path";
7
8
  import { selectModel, selectSystemInstruction } from "./configuration.js";
8
9
  import { ModelEndpointProxy } from "./model-endpoint-proxy.js";
@@ -20,6 +21,8 @@ const NONINTERACTIVE_SESSION_PERMISSIONS = [
20
21
  // catalog discovery. Keep the Fabric readiness deadline aligned with both.
21
22
  const MCP_CONNECTION_TIMEOUT_MS = 65_000;
22
23
  const MCP_CONNECTION_POLL_INTERVAL_MS = 50;
24
+ const SKILL_DISCOVERY_TIMEOUT_MS = 5_000;
25
+ const SKILL_DISCOVERY_POLL_INTERVAL_MS = 50;
23
26
  const HTTP_HEADER_NAME = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/u;
24
27
  function loopbackHostname(hostname) {
25
28
  return hostname === "localhost" || hostname === "::1" || hostname === "[::1]" || /^127(?:\.\d{1,3}){3}$/u.test(hostname);
@@ -105,78 +108,129 @@ function hostConfigContent(model, baseUrl = model.baseUrl, systemInstruction, sk
105
108
  }
106
109
  return content;
107
110
  }
108
- async function resolveSkills(baseDir, configured) {
109
- const skills = [];
110
- for (const entry of configured) {
111
- if (typeof entry !== "string") {
112
- throw new LifecycleError("opencode_invalid_skill", "OpenCode skill paths must be strings");
113
- }
114
- let candidate;
115
- try {
116
- candidate = await realpath(resolve(baseDir, entry));
111
+ async function rejectAdditionalDiscoverableSkills(directory, root = directory) {
112
+ for (const entry of await readdir(directory, { withFileTypes: true })) {
113
+ const location = join(directory, entry.name);
114
+ if (entry.isDirectory()) {
115
+ await rejectAdditionalDiscoverableSkills(location, root);
116
+ continue;
117
117
  }
118
- catch {
119
- throw new LifecycleError("opencode_skill_not_found", "A configured NeMo Fabric skill path does not exist");
118
+ const atRoot = directory === root;
119
+ if ((atRoot && entry.name.endsWith(".md") && entry.name !== "SKILL.md") || (!atRoot && entry.name === "SKILL.md")) {
120
+ throw new LifecycleError("opencode_skill_unexpected", "Configured OpenCode skill directories must not contain additional discoverable skills");
120
121
  }
121
- try {
122
- if (!(await stat(candidate)).isDirectory()) {
123
- throw new Error("not a directory");
122
+ }
123
+ }
124
+ async function prepareRuntimeSkills(baseDir, configured) {
125
+ if (configured.length === 0) {
126
+ return { skills: [] };
127
+ }
128
+ // OpenCode reports canonical skill paths. Canonicalize the temporary root as
129
+ // well so platforms where /tmp is a symlink do not turn a configured skill
130
+ // into a false "unexpected" catalog entry.
131
+ const root = await realpath(await mkdtemp(join(tmpdir(), "nemo-fabric-opencode-skills-")));
132
+ const skills = [];
133
+ try {
134
+ for (const [index, entry] of configured.entries()) {
135
+ if (typeof entry !== "string") {
136
+ throw new LifecycleError("opencode_invalid_skill", "OpenCode skill paths must be strings");
124
137
  }
125
- }
126
- catch {
127
- throw new LifecycleError("opencode_skill_invalid", "NeMo Fabric skill paths must be directories");
128
- }
129
- let location;
130
- try {
131
- location = await realpath(join(candidate, "SKILL.md"));
132
- if (!(await stat(location)).isFile()) {
133
- throw new Error("not a file");
138
+ let source;
139
+ try {
140
+ source = await realpath(resolve(baseDir, entry));
134
141
  }
142
+ catch {
143
+ throw new LifecycleError("opencode_skill_not_found", "A configured NeMo Fabric skill path does not exist");
144
+ }
145
+ try {
146
+ if (!(await stat(source)).isDirectory()) {
147
+ throw new Error("not a directory");
148
+ }
149
+ }
150
+ catch {
151
+ throw new LifecycleError("opencode_skill_invalid", "NeMo Fabric skill paths must be directories");
152
+ }
153
+ try {
154
+ const sourceLocation = await realpath(join(source, "SKILL.md"));
155
+ if (!(await stat(sourceLocation)).isFile()) {
156
+ throw new Error("not a file");
157
+ }
158
+ }
159
+ catch {
160
+ throw new LifecycleError("opencode_skill_invalid", "NeMo Fabric skill directories must contain a SKILL.md file");
161
+ }
162
+ const directory = join(root, String(index));
163
+ try {
164
+ await cp(source, directory, { recursive: true, dereference: true, errorOnExist: true });
165
+ }
166
+ catch {
167
+ throw new LifecycleError("opencode_skill_invalid", "OpenCode could not snapshot a configured skill directory");
168
+ }
169
+ await rejectAdditionalDiscoverableSkills(directory);
170
+ skills.push({ directory, location: await realpath(join(directory, "SKILL.md")) });
135
171
  }
136
- catch {
137
- throw new LifecycleError("opencode_skill_invalid", "NeMo Fabric skill directories must contain a SKILL.md file");
138
- }
139
- skills.push({ directory: candidate, location });
172
+ return { root, skills };
173
+ }
174
+ catch (error) {
175
+ await rm(root, { recursive: true, force: true });
176
+ throw error;
140
177
  }
141
- return skills;
142
178
  }
143
179
  async function verifyLoadedSkills(client, workspace, configured) {
144
180
  if (configured.length === 0) {
145
181
  return;
146
182
  }
147
- let loaded;
148
- try {
149
- await client.plugin.awaitActivation({ location: { directory: workspace } });
150
- loaded = (await client.skill.list({ location: { directory: workspace } })).data;
151
- }
152
- catch {
153
- throw new LifecycleError("opencode_skill_status_unavailable", "OpenCode could not determine the configured skill status");
154
- }
155
- const byLocation = new Map(loaded
156
- .filter((skill) => typeof skill.name === "string" && typeof skill.location === "string")
157
- .map((skill) => [skill.location, skill]));
158
183
  const configuredLocations = new Set(configured.map((skill) => skill.location));
159
- const hasUnexpectedConfiguredSkill = Array.from(byLocation.keys()).some((location) => !configuredLocations.has(location) &&
160
- configured.some((skill) => {
161
- const relativeLocation = relative(skill.directory, location);
162
- return relativeLocation !== ".." && !relativeLocation.startsWith(`..${sep}`) && !isAbsolute(relativeLocation);
163
- }));
164
- if (hasUnexpectedConfiguredSkill) {
165
- throw new LifecycleError("opencode_skill_unexpected", "OpenCode loaded a skill that was not configured by NeMo Fabric");
166
- }
167
- const configuredSkills = configured.map((skill) => byLocation.get(skill.location));
168
- if (configuredSkills.some((skill) => skill === undefined)) {
169
- throw new LifecycleError("opencode_skill_load_failed", "OpenCode did not load every configured NeMo Fabric skill");
170
- }
171
- const names = new Set();
172
- for (const skill of configuredSkills) {
173
- if (skill === undefined) {
174
- continue;
184
+ // OpenCode catalogs are nonblocking as of 2.0.23. Poll until every configured
185
+ // skill appears or the bounded discovery window expires. Configured directories
186
+ // are immutable private snapshots, so no additional skill can appear afterward.
187
+ const deadline = Date.now() + SKILL_DISCOVERY_TIMEOUT_MS;
188
+ while (true) {
189
+ const remainingMs = deadline - Date.now();
190
+ if (remainingMs <= 0) {
191
+ throw new LifecycleError("opencode_skill_load_failed", "OpenCode did not load every configured NeMo Fabric skill");
192
+ }
193
+ const signal = AbortSignal.timeout(remainingMs);
194
+ let loaded;
195
+ try {
196
+ loaded = (await client.skill.list({ location: { directory: workspace } }, { signal })).data;
197
+ }
198
+ catch {
199
+ if (signal.aborted) {
200
+ throw new LifecycleError("opencode_skill_load_failed", "OpenCode did not load every configured NeMo Fabric skill");
201
+ }
202
+ throw new LifecycleError("opencode_skill_status_unavailable", "OpenCode could not determine the configured skill status");
203
+ }
204
+ const byLocation = new Map(loaded
205
+ .filter((skill) => typeof skill.name === "string" && typeof skill.path === "string")
206
+ .map((skill) => [skill.path, skill]));
207
+ const hasUnexpectedConfiguredSkill = Array.from(byLocation.keys()).some((location) => !configuredLocations.has(location) &&
208
+ configured.some((skill) => {
209
+ const relativeLocation = relative(skill.directory, location);
210
+ return relativeLocation !== ".." && !relativeLocation.startsWith(`..${sep}`) && !isAbsolute(relativeLocation);
211
+ }));
212
+ if (hasUnexpectedConfiguredSkill) {
213
+ throw new LifecycleError("opencode_skill_unexpected", "OpenCode loaded a skill that was not configured by NeMo Fabric");
214
+ }
215
+ const configuredSkills = configured.map((skill) => byLocation.get(skill.location));
216
+ if (configuredSkills.every((skill) => skill !== undefined)) {
217
+ const names = new Set();
218
+ for (const skill of configuredSkills) {
219
+ if (names.has(skill.name)) {
220
+ throw new LifecycleError("opencode_skill_name_duplicate", "Configured NeMo Fabric skills must have distinct names");
221
+ }
222
+ names.add(skill.name);
223
+ }
224
+ return;
175
225
  }
176
- if (names.has(skill.name)) {
177
- throw new LifecycleError("opencode_skill_name_duplicate", "Configured NeMo Fabric skills must have distinct names");
226
+ if (Date.now() >= deadline) {
227
+ throw new LifecycleError("opencode_skill_load_failed", "OpenCode did not load every configured NeMo Fabric skill");
228
+ }
229
+ const pollDelayMs = Math.min(SKILL_DISCOVERY_POLL_INTERVAL_MS, deadline - Date.now());
230
+ if (pollDelayMs <= 0) {
231
+ throw new LifecycleError("opencode_skill_load_failed", "OpenCode did not load every configured NeMo Fabric skill");
178
232
  }
179
- names.add(skill.name);
233
+ await new Promise((resolve) => setTimeout(resolve, pollDelayMs));
180
234
  }
181
235
  }
182
236
  function expandMcpHeaders(headers, configuredEnvironment, parentEnvironment) {
@@ -437,7 +491,7 @@ export async function loadOpenCodeSdk(resolveModule = (specifier) => import.meta
437
491
  sdk = resolveModule("@opencode/sdk");
438
492
  }
439
493
  catch {
440
- throw new LifecycleError("opencode_harness_unavailable", "The OpenCode v2 harness is not installed. Install compatible packages with: npm install @opencode/core@2.0.3 @opencode/sdk@2.0.3");
494
+ throw new LifecycleError("opencode_harness_unavailable", "The OpenCode v2 harness is not installed. Install compatible packages with: npm install @opencode/core@2.0.23 @opencode/sdk@2.0.23");
441
495
  }
442
496
  try {
443
497
  const [, loadedSdk] = await Promise.all([loadModule(coreConfig), loadModule(sdk)]);
@@ -448,7 +502,7 @@ export async function loadOpenCodeSdk(resolveModule = (specifier) => import.meta
448
502
  }
449
503
  }
450
504
  async function loadEmbeddedOpenCodeCreate() {
451
- // @opencode/sdk@2.0.3 exposes its two-argument Promise SDK entry point as a
505
+ // The OpenCode SDK exposes its two-argument Promise SDK entry point as a
452
506
  // packaged file but does not export it from the package root. Resolve the
453
507
  // package root first so this remains valid when npm hoists dependencies. The
454
508
  // adapter exact-pins its Core and SDK peers while this private-file workaround
@@ -531,12 +585,14 @@ class OpenCodeSdkSessionHandle {
531
585
  client;
532
586
  environment;
533
587
  endpointProxy;
588
+ skillsRoot;
534
589
  stopped = false;
535
- constructor(id, client, environment, endpointProxy) {
590
+ constructor(id, client, environment, endpointProxy, skillsRoot) {
536
591
  this.id = id;
537
592
  this.client = client;
538
593
  this.environment = environment;
539
594
  this.endpointProxy = endpointProxy;
595
+ this.skillsRoot = skillsRoot;
540
596
  }
541
597
  async prompt(text) {
542
598
  let promptMayHaveBeenSubmitted = false;
@@ -579,7 +635,14 @@ class OpenCodeSdkSessionHandle {
579
635
  await this.endpointProxy?.close();
580
636
  }
581
637
  finally {
582
- this.environment.release();
638
+ try {
639
+ if (this.skillsRoot !== undefined) {
640
+ await rm(this.skillsRoot, { recursive: true, force: true });
641
+ }
642
+ }
643
+ finally {
644
+ this.environment.release();
645
+ }
583
646
  }
584
647
  }
585
648
  }
@@ -597,15 +660,17 @@ export class OpenCodeSdkSessionFactory {
597
660
  async create(input) {
598
661
  const model = selectModel(input.config);
599
662
  const systemInstruction = selectSystemInstruction(input.config);
600
- const skills = await resolveSkills(input.baseDir, input.config.skills?.paths ?? []);
601
663
  const configuredEnvironment = input.runtimeContext.environment.env ?? {};
602
664
  const mcpServers = selectMcpServers(input.config, configuredEnvironment, process.env);
603
665
  const workspace = input.runtimeContext.environment.workspace ?? input.baseDir;
604
- const environment = leaseEnvironment(input, model.apiKeyEnv);
666
+ const runtimeSkills = await prepareRuntimeSkills(input.baseDir, input.config.skills?.paths ?? []);
667
+ const skills = runtimeSkills.skills;
668
+ let environment;
605
669
  let client;
606
670
  let endpointProxy;
607
671
  let sessionId;
608
672
  try {
673
+ environment = leaseEnvironment(input, model.apiKeyEnv);
609
674
  const sdk = await this.sdkLoader();
610
675
  if (model.baseUrl !== undefined) {
611
676
  endpointProxy = await this.endpointProxyFactory(model.baseUrl);
@@ -613,7 +678,7 @@ export class OpenCodeSdkSessionFactory {
613
678
  const configContent = hostConfigContent(model, endpointProxy?.url, systemInstruction, skills.map((skill) => skill.directory), mcpServers);
614
679
  const embedOptions = await this.embedOptionsLoader(configContent);
615
680
  // The OpenCode convenience namespace silently ignores the embed argument.
616
- // Use the v2.0.3 Promise SDK entry point for production and retain the
681
+ // Use the Promise SDK entry point for production and retain the
617
682
  // public convenience API only for injected unit-test doubles.
618
683
  const createEmbeddedClient = this.sdkLoader === loadOpenCodeSdk
619
684
  ? await loadEmbeddedOpenCodeCreate()
@@ -631,7 +696,7 @@ export class OpenCodeSdkSessionFactory {
631
696
  });
632
697
  sessionId = session.id;
633
698
  await waitForMcpConnections(client, workspace, Object.keys(mcpServers));
634
- return new OpenCodeSdkSessionHandle(session.id, client, environment, endpointProxy);
699
+ return new OpenCodeSdkSessionHandle(session.id, client, environment, endpointProxy, runtimeSkills.root);
635
700
  }
636
701
  catch (error) {
637
702
  if (client !== undefined) {
@@ -658,10 +723,20 @@ export class OpenCodeSdkSessionFactory {
658
723
  }
659
724
  finally {
660
725
  try {
661
- environment.release();
726
+ if (runtimeSkills.root !== undefined) {
727
+ await rm(runtimeSkills.root, { recursive: true, force: true });
728
+ }
662
729
  }
663
730
  catch {
664
- // Preserve the startup failure when restoring the environment fails.
731
+ // Preserve the startup failure while releasing local adapter resources.
732
+ }
733
+ finally {
734
+ try {
735
+ environment?.release();
736
+ }
737
+ catch {
738
+ // Preserve the startup failure when restoring the environment fails.
739
+ }
665
740
  }
666
741
  }
667
742
  if (error instanceof LifecycleError) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nemo-fabric-adapters-opencode",
3
- "version": "0.4.0-beta.3",
3
+ "version": "0.5.0-beta.1",
4
4
  "description": "OpenCode v2 SDK harness adapter for NVIDIA NeMo Fabric.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -42,12 +42,12 @@
42
42
  "bun": ">=1.4.2"
43
43
  },
44
44
  "dependencies": {
45
- "nemo-fabric-adapter-contract": "0.4.0-beta.3",
46
- "nemo-fabric-adapters-common": "0.4.0-beta.3"
45
+ "nemo-fabric-adapter-contract": "0.5.0-beta.1",
46
+ "nemo-fabric-adapters-common": "0.5.0-beta.1"
47
47
  },
48
48
  "peerDependencies": {
49
- "@opencode/core": "2.0.3",
50
- "@opencode/sdk": "2.0.3"
49
+ "@opencode/core": "2.0.23",
50
+ "@opencode/sdk": "2.0.23"
51
51
  },
52
52
  "peerDependenciesMeta": {
53
53
  "@opencode/core": {
@@ -58,8 +58,8 @@
58
58
  }
59
59
  },
60
60
  "devDependencies": {
61
- "@opencode/core": "2.0.3",
62
- "@opencode/sdk": "2.0.3",
61
+ "@opencode/core": "2.0.23",
62
+ "@opencode/sdk": "2.0.23",
63
63
  "@types/node": "24.12.4",
64
64
  "typescript": "5.9.3"
65
65
  }