gencow 0.1.288 → 0.1.290

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.
@@ -234,6 +234,17 @@ function appendRootCauseLines(lines, contract, statusCommand) {
234
234
  );
235
235
  }
236
236
 
237
+ // body.diagnostic = { code, scope }: shown only when both values match these strict patterns, never echoed otherwise.
238
+ const DIAGNOSTIC_SCOPE_PATTERN = /^[a-z][a-z0-9_]{2,31}$/u;
239
+ const DIAGNOSTIC_CODE_PATTERNS = [/^[a-z][a-z0-9_]{2,63}$/u, /^[A-Z][A-Z0-9_]{2,99}$/u];
240
+
241
+ function readStructuredDiagnosticLines(responseBody) {
242
+ const { code, scope } = responseBody?.diagnostic ?? {};
243
+ if (typeof code !== "string" || typeof scope !== "string") return [];
244
+ if (!DIAGNOSTIC_SCOPE_PATTERN.test(scope) || !DIAGNOSTIC_CODE_PATTERNS.some((p) => p.test(code))) return [];
245
+ return [`Diagnostic: ${scope}/${code}`];
246
+ }
247
+
237
248
  function safeText(value, maxLength) {
238
249
  if (typeof value !== "string") return null;
239
250
  const normalized = value.trim();
@@ -660,7 +671,7 @@ export function renderDeployFailureDiagnosticLines(responseBody, statusText, opt
660
671
  const publicMessage = contract.legacySupportAction
661
672
  ? PLATFORM_STATUS_MESSAGE
662
673
  : (safeText(responseBody.error, 240) ?? fallbackMessage);
663
- const lines = [`Deploy failed: ${publicMessage}`, `Code: ${contract.code}`, `Stage: ${contract.stage}`];
674
+ const lines = [`Deploy failed: ${publicMessage}`, `Code: ${contract.code}`, ...readStructuredDiagnosticLines(responseBody), `Stage: ${contract.stage}`];
664
675
  const phase = formatDeploymentOperationPhase(responseBody);
665
676
  if (phase) lines.push(`Phase: ${phase}`);
666
677
  const selfService = readSelfServiceDiagnostic(responseBody, contract, options.expectedAppName);
@@ -744,7 +755,7 @@ export function renderDeployFailureDiagnosticLines(responseBody, statusText, opt
744
755
  ? PLATFORM_STATUS_MESSAGE
745
756
  : (safeText(responseBody.error, 240) ?? fallbackMessage);
746
757
  const lines = [`Deploy failed: ${publicMessage}`];
747
- lines.push(`Code: ${contract.code}`, `Stage: ${contract.stage}`);
758
+ lines.push(`Code: ${contract.code}`, ...readStructuredDiagnosticLines(responseBody), `Stage: ${contract.stage}`);
748
759
  const selfService = readSelfServiceDiagnostic(responseBody, contract, options.expectedAppName);
749
760
  appendSelfServiceState(lines, selfService);
750
761
  if (isCliUpgradeContract(contract) && !selfService) {
@@ -1,4 +1,4 @@
1
- import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
1
+ import { chmodSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
2
2
  import { homedir, hostname } from "node:os";
3
3
  import { dirname, join } from "node:path";
4
4
  import { BOLD, CYAN, DIM, RESET, error, info, log, success } from "./output.mjs";
@@ -9,6 +9,7 @@ import {
9
9
  applyOpencodeEdits,
10
10
  hasGencowKey,
11
11
  hasGencowProvider,
12
+ hasKeyFileRef,
12
13
  planOpencodeEdits,
13
14
  resolveOpencodePaths,
14
15
  } from "./opencode-config.mjs";
@@ -66,7 +67,8 @@ export function createOpencodeCommands(overrides = {}) {
66
67
  }
67
68
  function writeState(state) {
68
69
  mkdirSync(dirname(statePath()), { recursive: true });
69
- writeFileSync(statePath(), `${JSON.stringify(state, null, 2)}\n`);
70
+ writeFileSync(statePath(), `${JSON.stringify(state, null, 2)}\n`, { mode: 0o600 });
71
+ if (process.platform !== "win32") chmodSync(statePath(), 0o600); // an older 644 file keeps its mode on rewrite
70
72
  }
71
73
 
72
74
  async function verifyKey(baseURL, key) {
@@ -135,7 +137,13 @@ export function createOpencodeCommands(overrides = {}) {
135
137
  const sameAccount = state && state.userId === creds.userId && state.platformUrl === creds.platformUrl;
136
138
  const fresh = sameAccount && existingKey && hasGencowProvider(paths) && Date.parse(state.expiresAt) - deps.nowImpl() > RENEW_WITHIN_MS;
137
139
  if (fresh && !flags.force && (await verifyKey(baseURL, existingKey)) === 200) {
138
- return { ok: true, code: "ALREADY_CONFIGURED", changed: false, model: `gencow/${GENCOW_MODEL_ID}`, expiresAt: state.expiresAt };
140
+ if (hasKeyFileRef(paths)) {
141
+ return { ok: true, code: "ALREADY_CONFIGURED", changed: false, model: `gencow/${GENCOW_MODEL_ID}`, expiresAt: state.expiresAt };
142
+ }
143
+ // A setup from an older CLI wrote only auth.json, which OpenCode Desktop does not read. The token is still
144
+ // valid, so add the key file and the config reference without minting or revoking anything.
145
+ applyOpencodeEdits({ paths, plan: planOpencodeEdits({ paths, baseURL, apiKey: existingKey }) });
146
+ return { ok: true, code: "CONFIGURED", changed: true, repaired: true, model: `gencow/${GENCOW_MODEL_ID}`, expiresAt: state.expiresAt };
139
147
  }
140
148
 
141
149
  // Refuse before minting when the OpenCode files cannot be edited safely, so no unused token is created.
@@ -11,6 +11,12 @@ import { dirname, join } from "node:path";
11
11
  */
12
12
  export const GENCOW_PROVIDER_ID = "gencow";
13
13
  export const GENCOW_MODEL_ID = "llm/code";
14
+ /**
15
+ * The key lives in a private file that the OpenCode config references with `{file:...}`. OpenCode Desktop keeps
16
+ * its own credential store (opencode.db) and ignores auth.json, so a config reference is the one channel both
17
+ * Desktop and the CLI read (verified: Desktop on macOS, CLI 1.19.5 and a current dev build).
18
+ */
19
+ export const GENCOW_KEY_FILE_REF = "{file:~/.gencow/opencode.key}";
14
20
 
15
21
  export class OpencodeSetupError extends Error {
16
22
  constructor(code, message, nextAction) {
@@ -31,6 +37,7 @@ export function resolveOpencodePaths({ env = process.env, home } = {}) {
31
37
  configJson: join(configDir, "opencode.json"),
32
38
  configJsonc: join(configDir, "opencode.jsonc"),
33
39
  authJson: join(dataHome, "opencode", "auth.json"),
40
+ keyFile: join(home, ".gencow", "opencode.key"),
34
41
  };
35
42
  }
36
43
 
@@ -121,7 +128,7 @@ export function gencowProviderConfig(baseURL) {
121
128
  return {
122
129
  npm: "@ai-sdk/openai-compatible",
123
130
  name: "Gencow",
124
- options: { baseURL },
131
+ options: { baseURL, apiKey: GENCOW_KEY_FILE_REF },
125
132
  models: { [GENCOW_MODEL_ID]: { name: "Gencow Code" } },
126
133
  };
127
134
  }
@@ -179,6 +186,8 @@ export function planOpencodeEdits({ paths, baseURL, apiKey }) {
179
186
  return {
180
187
  configText,
181
188
  authText,
189
+ keyFileText: readText(paths.keyFile),
190
+ nextKeyFileText: apiKey,
182
191
  nextConfigText: `${JSON.stringify(nextConfig, null, 2)}\n`,
183
192
  nextAuthText: `${JSON.stringify(nextAuth, null, 2)}\n`,
184
193
  keptUserModel: userHasModel,
@@ -196,11 +205,17 @@ export function applyOpencodeEdits({ paths, plan }) {
196
205
  if (plan.configText !== null && !existsSync(`${paths.configJson}.bak`)) {
197
206
  writeFileSync(`${paths.configJson}.bak`, plan.configText, { mode: 0o600 });
198
207
  }
199
- atomicWrite(paths.configJson, plan.nextConfigText);
208
+ atomicWrite(paths.keyFile, plan.nextKeyFileText, 0o600);
200
209
  try {
201
- atomicWrite(paths.authJson, plan.nextAuthText, 0o600);
210
+ atomicWrite(paths.configJson, plan.nextConfigText);
211
+ try {
212
+ atomicWrite(paths.authJson, plan.nextAuthText, 0o600);
213
+ } catch (error) {
214
+ restore(paths.configJson, plan.configText);
215
+ throw error;
216
+ }
202
217
  } catch (error) {
203
- restore(paths.configJson, plan.configText);
218
+ restore(paths.keyFile, plan.keyFileText);
204
219
  throw error;
205
220
  }
206
221
  } catch (error) {
@@ -221,6 +236,13 @@ export function hasGencowProvider(paths) {
221
236
  });
222
237
  }
223
238
 
239
+ /** True when the provider block already points at the key file and the key file exists (Desktop-readable setup). */
240
+ export function hasKeyFileRef(paths) {
241
+ const text = readText(paths.configJson);
242
+ const ref = text ? parseJsonc(text)?.value?.provider?.[GENCOW_PROVIDER_ID]?.options?.apiKey : null;
243
+ return ref === GENCOW_KEY_FILE_REF && Boolean(readText(paths.keyFile)?.trim());
244
+ }
245
+
224
246
  export function hasGencowKey(authJsonPath) {
225
247
  const text = readText(authJsonPath);
226
248
  const parsed = text ? parseJsonc(text) : null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gencow",
3
- "version": "0.1.288",
3
+ "version": "0.1.290",
4
4
  "description": "Gencow — AI Backend Engine",
5
5
  "type": "module",
6
6
  "bin": {
@@ -42,9 +42,9 @@
42
42
  "postgres": "^3.4.9",
43
43
  "@gencow/client": "0.2.8",
44
44
  "@gencow/core": "0.1.48",
45
- "@gencow/shared": "0.0.0",
46
45
  "@gencow/server": "0.1.3",
47
- "@gencow/react": "0.2.8"
46
+ "@gencow/react": "0.2.8",
47
+ "@gencow/shared": "0.0.0"
48
48
  },
49
49
  "scripts": {
50
50
  "build": "node scripts/bundle-core.mjs && node scripts/bundle-shared.mjs && node scripts/bundle-server.mjs",