gencow 0.1.276 → 0.1.278

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.
@@ -1,5 +1,6 @@
1
1
  import { formatProcedureRpcDiagnostic } from "./procedure-rpc-diagnostic.mjs";
2
2
  import { describeMigrationFolderFormatFailure } from "./migration-folder-format.mjs";
3
+ import { authCauseNextAction, safeAuthCauseCode } from "./release-auth-cause.mjs";
3
4
  import {
4
5
  SCHEMA_LOAD_FAILURE_CLI_ACTION,
5
6
  SCHEMA_LOAD_FAILURE_CLI_MESSAGE,
@@ -378,6 +379,8 @@ function commandErrorMessage(caught) {
378
379
  }
379
380
  if (caught instanceof Error && (caught.message === "APP_RELEASE_AUTH_REQUIRED" || caught.status === 401)) {
380
381
  const lines = ["Login expired or invalid for this Platform."];
382
+ const authCauseCode = safeAuthCauseCode(caught.causeCode);
383
+ if (authCauseCode) lines.push("Stage: auth", `Cause: ${authCauseCode}`);
381
384
  const platformOrigin = safePlatformOrigin(caught.platformUrl);
382
385
  if (platformOrigin) lines.push(`Platform: ${platformOrigin}`);
383
386
  const credentialEnvName =
@@ -390,9 +393,9 @@ function commandErrorMessage(caught) {
390
393
  lines.push(
391
394
  `Action: replace or unset ${credentialEnvName}; environment credentials override interactive login.`,
392
395
  );
393
- } else {
394
- lines.push("Run: npx gencow@latest login");
395
396
  }
397
+ if (authCauseCode) lines.push(`Next: ${authCauseNextAction(credentialEnvName)}`);
398
+ else if (!credentialEnvName) lines.push("Run: npx gencow@latest login");
396
399
  if (typeof caught.correlationId === "string" && SAFE_CORRELATION_ID.test(caught.correlationId)) {
397
400
  lines.push(`Correlation ID: ${caught.correlationId}`);
398
401
  }
@@ -1,6 +1,7 @@
1
1
  import { appNotFoundDiagnostic } from "./app-response-contract.mjs";
2
2
  import { describeProdTargetFailure, resolveProdAppTarget } from "./cloud-targets.mjs";
3
3
  import { readJsonObjectResponse } from "./http-response-json.mjs";
4
+ import { authCauseNextAction, safeAuthCauseCode } from "./release-auth-cause.mjs";
4
5
 
5
6
  const PUBLIC_LOOKUP_ERROR_CODES = new Set([
6
7
  "ACCOUNT_DELETED",
@@ -13,12 +14,20 @@ const PUBLIC_LOOKUP_ERROR_CODES = new Set([
13
14
  ]);
14
15
  const SAFE_CORRELATION_ID = /^[A-Za-z0-9_-]{8,128}$/u;
15
16
 
16
- function publicLookupFailure(response, body) {
17
+ function publicLookupFailure(response, body, creds) {
17
18
  const code = PUBLIC_LOOKUP_ERROR_CODES.has(body?.code)
18
19
  ? body.code
19
20
  : response.status === 401
20
21
  ? "APP_RELEASE_AUTH_REQUIRED"
21
22
  : "APP_DEPLOY_TARGET_LOOKUP_FAILED";
23
+ const causeCode =
24
+ code === "APP_RELEASE_AUTH_REQUIRED" && body?.stage === "auth" ? safeAuthCauseCode(body.causeCode) : null;
25
+ // Only a fixed env name is kept, so the next action can tell the user to replace or unset it.
26
+ const credentialEnvName =
27
+ creds?.credentialSource === "environment" &&
28
+ (creds.credentialEnvName === "GENCOW_TOKEN" || creds.credentialEnvName === "GENCOW_DEPLOY_TOKEN")
29
+ ? creds.credentialEnvName
30
+ : null;
22
31
  return {
23
32
  kind: "error",
24
33
  code,
@@ -27,6 +36,8 @@ function publicLookupFailure(response, body) {
27
36
  ...(typeof body?.correlationId === "string" && SAFE_CORRELATION_ID.test(body.correlationId)
28
37
  ? { correlationId: body.correlationId }
29
38
  : {}),
39
+ ...(causeCode ? { causeCode } : {}),
40
+ ...(causeCode && credentialEnvName ? { credentialEnvName } : {}),
30
41
  };
31
42
  }
32
43
 
@@ -37,9 +48,14 @@ function formatLookupFailure(failure, target) {
37
48
  ];
38
49
  if (failure?.status) lines.push(`HTTP status: ${failure.status}`);
39
50
  if (typeof failure?.retryable === "boolean") lines.push(`Retryable: ${failure.retryable ? "yes" : "no"}`);
51
+ if (failure?.causeCode) lines.push("Stage: auth", `Cause: ${failure.causeCode}`);
40
52
  if (failure?.correlationId) lines.push(`Correlation ID: ${failure.correlationId}`);
41
53
  if (code === "APP_RELEASE_AUTH_REQUIRED") {
42
- lines.push("Action: run gencow login, or replace/unset the environment credential, then retry.");
54
+ lines.push(
55
+ failure?.causeCode
56
+ ? `Action: ${authCauseNextAction(failure.credentialEnvName ?? null)}`
57
+ : "Action: run gencow login, or replace/unset the environment credential, then retry.",
58
+ );
43
59
  } else if (code === "ACCOUNT_SUSPENDED" || code === "ACCOUNT_DELETION_PENDING" || code === "ACCOUNT_DELETED") {
44
60
  lines.push("Action: resolve the account status in Platform before retrying.");
45
61
  } else if (failure?.retryable) {
@@ -172,10 +188,10 @@ export async function lookupOwnedApp({ creds, appId, environment, platformFetchI
172
188
  const body = await readJsonObjectResponse(response);
173
189
  if (response.status === 404) {
174
190
  if (body?.code === "APP_RELEASE_APP_NOT_FOUND") return "missing";
175
- return body?.code ? publicLookupFailure(response, body) : "unsupported";
191
+ return body?.code ? publicLookupFailure(response, body, creds) : "unsupported";
176
192
  }
177
193
  if (response.status === 409 && body?.code === "APP_RELEASE_VERSIONS_DISABLED") return "found";
178
- if (!response.ok) return publicLookupFailure(response, body);
194
+ if (!response.ok) return publicLookupFailure(response, body, creds);
179
195
  return body?.protocol === "app-release-v1" && typeof body.enabled === "boolean" ? "found" : "error";
180
196
  } catch {
181
197
  return {
@@ -342,9 +342,10 @@ export function renderMigrationDiagnosticLines(diagnostic) {
342
342
  lines.push(`Correlation: ${diagnostic.correlationId}`);
343
343
  return lines.map((line) => escapeTerminalControls(line));
344
344
  }
345
- // Without a verified local span, the server's "exact statement" wording would overclaim.
345
+ // Without a verified statement span, the server's "exact statement" wording would overclaim. A verified
346
+ // file without a statement index is still unlocated; its path is shown once, in the location line.
346
347
  const unlocatedRollback =
347
- isUserFixableRolledBackDiagnostic(diagnostic) && !diagnostic.localSource?.projectRelativePath;
348
+ isUserFixableRolledBackDiagnostic(diagnostic) && !diagnostic.localSource?.statementIndex;
348
349
  const retryCondition = unlocatedRollback
349
350
  ? UNLOCATED_ROLLBACK_RETRY_CONDITION
350
351
  : diagnostic.retryCondition;
@@ -358,7 +359,7 @@ export function renderMigrationDiagnosticLines(diagnostic) {
358
359
  `Retry policy: ${diagnostic.retryPolicy}${retryCondition ? ` — ${retryCondition}` : ""}`,
359
360
  ];
360
361
  const source = diagnostic.localSource;
361
- if (source?.projectRelativePath) {
362
+ if (source?.projectRelativePath && !unlocatedRollback) {
362
363
  lines.push("", `File: ${source.projectRelativePath}`);
363
364
  if (source.statementIndex) {
364
365
  const span = source.lineStart
@@ -367,13 +368,10 @@ export function renderMigrationDiagnosticLines(diagnostic) {
367
368
  lines.push(`Statement: ${source.statementIndex}${span}`);
368
369
  }
369
370
  if (source.excerpt) lines.push("Local SQL context (literals redacted):", source.excerpt);
370
- } else if (diagnostic.migration?.name) {
371
+ } else if (!source?.projectRelativePath && diagnostic.migration?.name) {
371
372
  lines.push("", `Migration: ${diagnostic.migration.name}`);
372
373
  }
373
- if (unlocatedRollback) {
374
- if (diagnostic.migration?.name) lines.push(renderUnlocatedRollbackLocation(diagnostic));
375
- else lines.push("", renderUnlocatedRollbackLocation(diagnostic));
376
- }
374
+ if (unlocatedRollback) lines.push("", renderUnlocatedRollbackLocation(diagnostic));
377
375
  if (diagnostic.operation?.kind) {
378
376
  lines.push(
379
377
  `Operation: ${diagnostic.operation.kind}${diagnostic.operation.objectName ? ` ${diagnostic.operation.objectName}` : ""}`,
@@ -30,34 +30,42 @@ const NOT_NULL_REPAIR_HINT =
30
30
  "For a NOT NULL failure, give the new column a DEFAULT or make it nullable, or split the change into forward migrations (expand → backfill → contract).";
31
31
 
32
32
  // Type/data repair hints for the unlocated fallback. Mirrors the server's suggested-fix text in
33
- // migration-rollback-diagnostic.ts; both must name only the column DEFAULT or literal in schema.ts.
34
- const TYPE_REPAIR_HINTS = new Map([
33
+ // migration-rollback-diagnostic.ts (SHARED_REPAIR_PATH and USER_REASON_CAUSES); the driver-chain test
34
+ // asserts the two stay equal. Static text only.
35
+ const SHARED_REPAIR_PATH =
36
+ 'Correct the cause in gencow/schema.ts first, because db:generate rebuilds an unapplied folder from that file. Then run `gencow db:check --inspect-state` (for a PROD target add `--prod --app <id>`). If it lists pending folders under "Pending migrations that may be regenerated", back up or commit the project, remove every folder it lists, and run `gencow db:generate`. If it lists nothing, correct the failing statement in its unapplied migration file in place. Then run `gencow db:check` and `gencow db:push`. Applied migrations and the Drizzle journal are never changed.';
37
+
38
+ const TYPE_REASON_CAUSES = new Map([
35
39
  [
36
40
  "invalid_text_representation",
37
- "For a value that does not match its column type, change the column DEFAULT or inserted literal in gencow/schema.ts to a value valid for the declared type (for example, a whole number for an integer column), then run db:generate to replace only this unapplied migration.",
41
+ "A value does not match its column type: use a value valid for the declared type (for example, a whole number for an integer column) in the DEFAULT or inserted literal.",
38
42
  ],
39
43
  [
40
44
  "string_data_right_truncation",
41
- "For a value longer than its column allows, shorten the literal or change the column length in gencow/schema.ts, then run db:generate to replace only this unapplied migration.",
45
+ "A value is longer than its column allows: shorten the literal or change the column length.",
42
46
  ],
43
47
  [
44
48
  "feature_not_supported_in_statement",
45
- "For a DEFAULT PostgreSQL does not accept (a bare word, a subquery, or a column reference), change the DEFAULT in gencow/schema.ts to a literal valid for the column type: a quoted string for text columns, a number for integer columns, true or false for boolean columns. Then run db:generate to replace only this unapplied migration.",
49
+ "A DEFAULT PostgreSQL does not accept (a bare word, a subquery, or a column reference): use a literal valid for the column type (a quoted string for text, a number for integer, true or false for boolean).",
46
50
  ],
47
51
  [
48
52
  "datatype_mismatch",
49
- "For a DEFAULT whose value type does not match its column, change the DEFAULT in gencow/schema.ts to a value of the column's declared type, then run db:generate to replace only this unapplied migration.",
53
+ "A DEFAULT value type does not match its column: use a value of the column's declared type.",
50
54
  ],
51
55
  [
52
56
  "undefined_function",
53
- "The statement calls a function that does not exist in this database. Correct the function name or remove it from the DEFAULT in gencow/schema.ts, then run db:generate to replace only this unapplied migration.",
57
+ "The statement calls a function that does not exist in this database: correct the function name or remove it from the DEFAULT.",
54
58
  ],
55
59
  [
56
60
  "undefined_object",
57
- "The statement uses a type name that does not exist in this database. Correct the column type in gencow/schema.ts, then run db:generate to replace only this unapplied migration.",
61
+ "The statement uses a type name that does not exist in this database: correct the column type.",
58
62
  ],
59
63
  ]);
60
64
 
65
+ const TYPE_REPAIR_HINTS = new Map(
66
+ [...TYPE_REASON_CAUSES].map(([reason, cause]) => [reason, `${cause} ${SHARED_REPAIR_PATH}`]),
67
+ );
68
+
61
69
  const SAFE_REASON_CODE = /^[a-z0-9_]{1,80}$/u;
62
70
 
63
71
  export function isUserFixableRolledBackDiagnostic(diagnostic) {
@@ -84,24 +92,36 @@ export function renderRollbackCauseLine(diagnostic) {
84
92
 
85
93
  // Honest replacement for the server's "exact statement" wording when the CLI could not
86
94
  // verify a local source span. Only the already-safe migration name is shown.
95
+ // The file to inspect: the verified project-relative path when the CLI could check it, otherwise the
96
+ // migration folder name. Never raw SQL.
97
+ export function unappliedMigrationLabel(diagnostic) {
98
+ return diagnostic?.localSource?.projectRelativePath ?? diagnostic?.migration?.name ?? null;
99
+ }
100
+
101
+ const PENDING_LIST_COMMAND = "gencow db:check --inspect-state";
102
+
87
103
  export function renderUnlocatedRollbackFix(diagnostic) {
88
- const name = diagnostic?.migration?.name;
104
+ const label = unappliedMigrationLabel(diagnostic);
89
105
  const code = safeRollbackReasonCode(diagnostic);
90
106
  const typeHint = code ? TYPE_REPAIR_HINTS.get(code) : undefined;
91
- const hint = code === "not_null_violation" ? ` ${NOT_NULL_REPAIR_HINT}` : typeHint ? ` ${typeHint}` : "";
107
+ const detail =
108
+ code === "not_null_violation"
109
+ ? `Repair only the unapplied migration source for the cause above, review the generated diff, and create a fresh plan. Applied migrations and the journal are not edited. ${NOT_NULL_REPAIR_HINT}`
110
+ : (typeHint ??
111
+ "Repair only the unapplied migration source for the cause above, review the generated diff, and create a fresh plan. Applied migrations and the journal are not edited.");
92
112
  return {
93
- title: name
94
- ? `Inspect the unapplied migration file ${name}`
95
- : "Inspect the pending migrations listed by db:check",
96
- detail: `Repair only the unapplied migration source for the cause above, review the generated diff, and create a fresh plan. Applied migrations and the journal are not edited.${hint}`,
113
+ title: label
114
+ ? `Inspect the unapplied migration file ${label}`
115
+ : `Inspect the pending migrations listed by ${PENDING_LIST_COMMAND}`,
116
+ detail,
97
117
  };
98
118
  }
99
119
 
100
120
  export function renderUnlocatedRollbackLocation(diagnostic) {
101
- const name = diagnostic?.migration?.name;
102
- return name
103
- ? `Statement location: unavailable — inspect the unapplied migration file ${name}`
104
- : "Statement location: unavailable — inspect the pending migrations listed by db:check";
121
+ const label = unappliedMigrationLabel(diagnostic);
122
+ return label
123
+ ? `Statement location: unavailable — inspect the unapplied migration file ${label}`
124
+ : `Statement location: unavailable — inspect the pending migrations listed by ${PENDING_LIST_COMMAND}`;
105
125
  }
106
126
 
107
127
  export const UNLOCATED_ROLLBACK_SUMMARY =
@@ -0,0 +1,26 @@
1
+ // Safe cause of a release capability 401 (APP_RELEASE_AUTH_REQUIRED). The Platform sends it as
2
+ // `stage: "auth"` plus `causeCode`; only these fixed values are rendered. Any other value is
3
+ // ignored, so an older Platform response keeps the output it had before this field existed.
4
+ const SAFE_AUTH_CAUSE_CODES = new Set([
5
+ "token_not_found",
6
+ "token_expired",
7
+ "token_scope_invalid",
8
+ "no_identity",
9
+ ]);
10
+
11
+ export function safeAuthCauseCode(value) {
12
+ return typeof value === "string" && SAFE_AUTH_CAUSE_CODES.has(value) ? value : null;
13
+ }
14
+
15
+ // Sentence after "Action:" or "Next:" when the Platform stated an auth cause. It does not claim whether the
16
+ // Platform or the credential is at fault; it tells the user the one check that separates the two.
17
+ // There is no automatic retry: the user reruns the command once by hand after the check succeeds.
18
+ // `credentialEnvName` (GENCOW_TOKEN or GENCOW_DEPLOY_TOKEN, already validated by the caller) is passed only
19
+ // when that variable is the credential. It overrides `gencow login`, so the fallback names the env change.
20
+ export function authCauseNextAction(credentialEnvName = null) {
21
+ const check = "run `gencow whoami` first. If it succeeds, rerun the same command once manually (no automatic retry); ";
22
+ if (credentialEnvName) {
23
+ return `${check}if it fails, replace or unset ${credentialEnvName} (gencow login does not override it). If it repeats, send the Correlation ID.`;
24
+ }
25
+ return `${check}if it fails, run \`gencow login\`. If it repeats, send the Correlation ID.`;
26
+ }
@@ -1,6 +1,7 @@
1
1
  import { randomUUID } from "node:crypto";
2
2
  import { unlink } from "node:fs/promises";
3
3
 
4
+ import { safeAuthCauseCode } from "./release-auth-cause.mjs";
4
5
  import { platformFetch } from "./platform-client.mjs";
5
6
  import { createReleaseSourcePackage } from "./release-source-package.mjs";
6
7
 
@@ -99,6 +100,8 @@ function throwResponseError(response, fallback, body, creds = {}, context = {})
99
100
  if (correlationId) error.correlationId = correlationId;
100
101
  if (authRequired) {
101
102
  error.retryable = false;
103
+ // Set only when the Platform states the auth stage; an older body has neither field.
104
+ if (body?.stage === "auth" && safeAuthCauseCode(body.causeCode)) error.causeCode = body.causeCode;
102
105
  } else if (typeof body?.retryable === "boolean") {
103
106
  error.retryable = body.retryable;
104
107
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gencow",
3
- "version": "0.1.276",
3
+ "version": "0.1.278",
4
4
  "description": "Gencow — AI Backend Engine",
5
5
  "type": "module",
6
6
  "bin": {
@@ -41,9 +41,9 @@
41
41
  "better-auth": "1.7.11",
42
42
  "postgres": "^3.4.9",
43
43
  "@gencow/client": "0.2.8",
44
- "@gencow/server": "0.1.3",
45
44
  "@gencow/react": "0.2.8",
46
45
  "@gencow/core": "0.1.47",
46
+ "@gencow/server": "0.1.3",
47
47
  "@gencow/shared": "0.0.0"
48
48
  },
49
49
  "scripts": {