gencow 0.1.279 → 0.1.281

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 { appNotFoundDiagnostic } from "./app-response-contract.mjs";
2
2
  import { describeProdTargetFailure, resolveProdAppTarget } from "./cloud-targets.mjs";
3
+ import { responseHeaderCorrelationId, SAFE_CORRELATION_ID } from "./http-correlation-id.mjs";
3
4
  import { readJsonObjectResponse } from "./http-response-json.mjs";
4
5
  import { authCauseNextAction, safeAuthCauseCode } from "./release-auth-cause.mjs";
5
6
 
@@ -12,14 +13,19 @@ const PUBLIC_LOOKUP_ERROR_CODES = new Set([
12
13
  "APP_RELEASE_ENVIRONMENT_INVALID",
13
14
  "APP_RELEASE_POLICY_READ_UNAVAILABLE",
14
15
  ]);
15
- const SAFE_CORRELATION_ID = /^[A-Za-z0-9_-]{8,128}$/u;
16
+ const UNTYPED_STAGE = "deploy target lookup";
16
17
 
17
18
  function publicLookupFailure(response, body, creds) {
18
- const code = PUBLIC_LOOKUP_ERROR_CODES.has(body?.code)
19
+ const typed = PUBLIC_LOOKUP_ERROR_CODES.has(body?.code);
20
+ const code = typed
19
21
  ? body.code
20
22
  : response.status === 401
21
23
  ? "APP_RELEASE_AUTH_REQUIRED"
22
24
  : "APP_DEPLOY_TARGET_LOOKUP_FAILED";
25
+ // A 5xx without a public code (empty, HTML or unknown-code body) may come from the edge or a proxy, so it is
26
+ // untyped. Its Stage and state lines are rendered; the header ID is used only here, so typed output is unchanged.
27
+ const untyped = !typed && response.status >= 500;
28
+ const bodyRetryable = typeof body?.retryable === "boolean" ? body.retryable : undefined;
23
29
  const causeCode =
24
30
  code === "APP_RELEASE_AUTH_REQUIRED" && body?.stage === "auth" ? safeAuthCauseCode(body.causeCode) : null;
25
31
  // Only a fixed env name is kept, so the next action can tell the user to replace or unset it.
@@ -28,20 +34,63 @@ function publicLookupFailure(response, body, creds) {
28
34
  (creds.credentialEnvName === "GENCOW_TOKEN" || creds.credentialEnvName === "GENCOW_DEPLOY_TOKEN")
29
35
  ? creds.credentialEnvName
30
36
  : null;
37
+ const correlationId =
38
+ typeof body?.correlationId === "string" && SAFE_CORRELATION_ID.test(body.correlationId)
39
+ ? body.correlationId
40
+ : untyped
41
+ ? responseHeaderCorrelationId(response)
42
+ : undefined;
31
43
  return {
32
44
  kind: "error",
33
45
  code,
34
46
  status: response.status,
35
- retryable: typeof body?.retryable === "boolean" ? body.retryable : response.status >= 500,
36
- ...(typeof body?.correlationId === "string" && SAFE_CORRELATION_ID.test(body.correlationId)
37
- ? { correlationId: body.correlationId }
38
- : {}),
47
+ retryable: bodyRetryable ?? response.status >= 500,
48
+ // The CLI estimates retryability when the Platform states none.
49
+ ...(bodyRetryable === undefined && untyped ? { retryableEstimated: true } : {}),
50
+ ...(correlationId ? { correlationId } : {}),
39
51
  ...(causeCode ? { causeCode } : {}),
40
52
  ...(causeCode && credentialEnvName ? { credentialEnvName } : {}),
53
+ ...(untyped ? { untyped: true } : {}),
41
54
  };
42
55
  }
43
56
 
44
- function formatLookupFailure(failure, target) {
57
+ // Lookup failure without a typed Platform error: an untyped 5xx, or no HTTP response at all (network error).
58
+ // Serving is stated only for this lookup: a GET changes nothing, so the incumbent serving state is unchanged by it.
59
+ function formatUntypedLookupFailure(failure, target, retryCommand) {
60
+ const lines = [
61
+ `${failure.code}: Could not verify the configured ${target} app. No replacement app was created and gencow.json was unchanged.`,
62
+ `Stage: ${UNTYPED_STAGE}`,
63
+ ];
64
+ if (failure.transportError) lines.push("Response: none received (network error)");
65
+ else lines.push(`HTTP status: ${failure.status}`);
66
+ lines.push(
67
+ failure.retryableEstimated
68
+ ? "Retry: rerun the same command (the Platform did not say whether a retry is safe)"
69
+ : `Retryable: ${failure.retryable ? "yes" : "no"}`,
70
+ );
71
+ lines.push(`Changed: ${DEPLOY_CHANGED_TEXT}`);
72
+ lines.push("Incumbent serving: PRESERVED (this lookup did not change it)");
73
+ lines.push(`Correlation ID: ${failure.correlationId ?? "none returned"}`);
74
+ const reportTail =
75
+ `add the failure time (UTC)${failure.correlationId ? " and the Correlation ID above" : ""} to the GitHub issue.`;
76
+ if (failure.transportError) {
77
+ lines.push(
78
+ "Action: check the network connection, then wait for Platform release capability to recover and retry.",
79
+ );
80
+ } else if (failure.retryable === false) {
81
+ // The Platform stated the failure is not retryable: no rerun instruction, only a report.
82
+ lines.push(`Action: do not retry this command. Report it: ${reportTail}`);
83
+ } else {
84
+ lines.push(`Rerun: ${retryCommand ?? "the same deploy command"}`);
85
+ lines.push(
86
+ `Action: wait 5-10 seconds, then rerun the same command once. If it fails again, ${reportTail}`,
87
+ );
88
+ }
89
+ return lines.join("\n");
90
+ }
91
+
92
+ function formatLookupFailure(failure, target, retryCommand = null) {
93
+ if (failure?.untyped) return formatUntypedLookupFailure(failure, target, retryCommand);
45
94
  const code = failure?.kind === "error" ? failure.code : "APP_DEPLOY_TARGET_LOOKUP_FAILED";
46
95
  const lines = [
47
96
  `${code}: Could not verify the configured ${target} app. No replacement app was created and gencow.json was unchanged.`,
@@ -198,6 +247,8 @@ export async function lookupOwnedApp({ creds, appId, environment, platformFetchI
198
247
  kind: "error",
199
248
  code: "APP_RELEASE_CAPABILITY_UNAVAILABLE",
200
249
  retryable: true,
250
+ untyped: true,
251
+ transportError: true,
201
252
  };
202
253
  }
203
254
  }
@@ -210,6 +261,7 @@ export async function resolveDeployAppId({
210
261
  platformFetchImpl,
211
262
  allowMissingAppReplacement,
212
263
  explicitApp = false,
264
+ retryCommand = null,
213
265
  infoImpl,
214
266
  errorImpl,
215
267
  exitImpl,
@@ -218,7 +270,7 @@ export async function resolveDeployAppId({
218
270
  const lookup = await lookupOwnedApp({ creds, appId, environment, platformFetchImpl });
219
271
  if (lookup === "found" || lookup === "unsupported") return appId;
220
272
  if (lookup?.kind === "error" || lookup === "error") {
221
- errorImpl(formatLookupFailure(lookup, `"${appId}"`));
273
+ errorImpl(formatLookupFailure(lookup, `"${appId}"`, retryCommand));
222
274
  exitImpl(1);
223
275
  return false;
224
276
  }
@@ -245,6 +297,7 @@ export async function reconcileProductionAppTarget({
245
297
  platformFetchImpl,
246
298
  createCloudAppIfNeeded,
247
299
  explicitApp = false,
300
+ retryCommand = null,
248
301
  infoImpl,
249
302
  errorImpl,
250
303
  exitImpl,
@@ -256,7 +309,7 @@ export async function reconcileProductionAppTarget({
256
309
  }
257
310
 
258
311
  const failLookup = (failure, target) => {
259
- errorImpl(formatLookupFailure(failure, target));
312
+ errorImpl(formatLookupFailure(failure, target, retryCommand));
260
313
  exitImpl(1);
261
314
  return null;
262
315
  };
@@ -29,7 +29,7 @@ export function renderDeployHelp() {
29
29
  log(` ${BOLD}Examples:${RESET}`);
30
30
  log(` gencow deploy`);
31
31
  log(` gencow deploy status`);
32
- log(` gencow deploy status --deployment <operation-id>`);
32
+ log(` gencow deploy status --deployment <operation-id> [--prod] [--app <name>]`);
33
33
  log(` gencow deploy status --release-attempt <attempt-id> [--prod]`);
34
34
  log(` gencow deploy logs [-n 100] [--history]`);
35
35
  log(` gencow deploy --static dist/`);
@@ -49,6 +49,8 @@ export function parseDeployLogScope(deployArgs) {
49
49
  return deployArgs.includes("--history") ? "history" : "current";
50
50
  }
51
51
 
52
+ const APP_NAME_PATTERN = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/u;
53
+
52
54
  function readReleaseAttemptStatusId(deployArgs) {
53
55
  const indices = deployArgs.flatMap((arg, index) => (arg === "--release-attempt" ? [index] : []));
54
56
  if (indices.length === 0) return null;
@@ -72,7 +74,7 @@ function readReleaseAttemptStatusId(deployArgs) {
72
74
  }
73
75
  if ((targetArgs[cursor] === "--app" || targetArgs[cursor] === "-a") && !appSelected) {
74
76
  const appName = targetArgs[cursor + 1];
75
- if (typeof appName !== "string" || !/^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/u.test(appName)) return undefined;
77
+ if (typeof appName !== "string" || !APP_NAME_PATTERN.test(appName)) return undefined;
76
78
  appSelected = true;
77
79
  cursor += 1;
78
80
  continue;
@@ -82,24 +84,55 @@ function readReleaseAttemptStatusId(deployArgs) {
82
84
  return value;
83
85
  }
84
86
 
85
- function readDeploymentStatusOperationId(deployArgs) {
86
- const index = deployArgs.findIndex((arg) => arg === "--deployment");
87
- if (index === -1) return null;
88
- const value = deployArgs[index + 1];
89
- // The public diagnostic contract emits `--prod` after the operation id for
90
- // production-owned deployments. The operation endpoint is already scoped
91
- // by the id, so accept that harmless target selector instead of rejecting
92
- // the exact self-service command we just rendered.
93
- const trailingArgs = deployArgs.slice(index + 2);
94
- if (
95
- trailingArgs.some((arg) => arg !== "--prod") ||
96
- typeof value !== "string" ||
97
- !/^[1-9][0-9]*$/u.test(value) ||
98
- !Number.isSafeInteger(Number(value))
99
- ) {
100
- return undefined;
87
+ function isDeploymentOperationIdText(value) {
88
+ return typeof value === "string" && /^[1-9][0-9]*$/u.test(value) && Number.isSafeInteger(Number(value));
89
+ }
90
+
91
+ const INLINE_OPTION_PATTERN = /^(-{1,2}[a-z][a-z0-9-]*)=(.*)$/u;
92
+ const MAX_ECHOED_OPTION_LENGTH = 40;
93
+
94
+ // --deployment is the operation-scoped form: the endpoint is addressed by the id alone. --prod and
95
+ // --app/-a are accepted in any order as selectors; --app is format-checked but does not change the lookup.
96
+ // `--name=value` is split into `--name value` first, so both spellings share one validation path.
97
+ function readDeploymentStatusArgs(rawDeployArgs) {
98
+ const deployArgs = rawDeployArgs.flatMap((arg) => {
99
+ const inline = INLINE_OPTION_PATTERN.exec(arg);
100
+ return inline ? [inline[1], inline[2]] : [arg];
101
+ });
102
+ if (!deployArgs.includes("--deployment")) return null;
103
+ const rawOperationId = deployArgs[deployArgs.indexOf("--deployment") + 1];
104
+ const operationText = isDeploymentOperationIdText(rawOperationId) ? rawOperationId : "<operation-id>";
105
+ const seen = new Set();
106
+ let operationId = null;
107
+ for (let cursor = 1; cursor < deployArgs.length; cursor += 1) {
108
+ const arg = deployArgs[cursor];
109
+ if (arg === "--deployment" || arg === "--prod" || arg === "--app" || arg === "-a") {
110
+ const option = arg === "-a" ? "--app" : arg;
111
+ if (seen.has(option))
112
+ return { error: "args", reason: `option ${option} was given more than once`, operationText };
113
+ seen.add(option);
114
+ if (option === "--prod") continue;
115
+ const value = deployArgs[cursor + 1];
116
+ cursor += 1;
117
+ if (option === "--deployment") {
118
+ if (!isDeploymentOperationIdText(value)) return { error: "operation-id" };
119
+ operationId = Number(value);
120
+ } else if (typeof value !== "string" || !APP_NAME_PATTERN.test(value)) {
121
+ return { error: "args", reason: "option --app needs a valid app name", operationText };
122
+ }
123
+ continue;
124
+ }
125
+ // Only [a-z0-9-] option names are echoed, and the echo is length-capped; anything else is not repeated.
126
+ const reason = /^-{1,2}[a-z][a-z0-9-]*$/u.test(arg)
127
+ ? `unknown option ${arg.slice(0, MAX_ECHOED_OPTION_LENGTH)}${arg.length > MAX_ECHOED_OPTION_LENGTH ? "..." : ""}`
128
+ : "unexpected argument";
129
+ return { error: "args", reason, operationText };
101
130
  }
102
- return Number(value);
131
+ return { operationId };
132
+ }
133
+
134
+ function renderDeploymentStatusArgsInvalid({ reason, operationText }) {
135
+ return `DEPLOYMENT_OPERATION_ARGS_INVALID: ${reason}. Stage: status argument parsing. Changed: nothing; status is read-only. Retry: run again with only the allowed options: gencow deploy status --deployment ${operationText} [--prod] [--app <name>]`;
103
136
  }
104
137
 
105
138
  function logDeploymentOperationStatus(logImpl, infoImpl, body) {
@@ -207,12 +240,18 @@ export async function handleDeployReadonlySubcommand(params) {
207
240
  logImpl("");
208
241
  return true;
209
242
  }
210
- const deploymentId = subcmd === "status" ? readDeploymentStatusOperationId(deployArgs) : null;
211
- if (deploymentId === undefined) {
243
+ const deploymentStatus = subcmd === "status" ? readDeploymentStatusArgs(deployArgs) : null;
244
+ if (deploymentStatus?.error === "operation-id") {
212
245
  errorImpl("DEPLOYMENT_OPERATION_ID_INVALID: use a positive numeric --deployment value.");
213
246
  exitImpl(1);
214
247
  return true;
215
248
  }
249
+ if (deploymentStatus?.error) {
250
+ errorImpl(renderDeploymentStatusArgsInvalid(deploymentStatus));
251
+ exitImpl(1);
252
+ return true;
253
+ }
254
+ const deploymentId = deploymentStatus?.operationId ?? null;
216
255
  if (deploymentId !== null) {
217
256
  if (typeof platformFetchImpl !== "function") {
218
257
  errorImpl("DEPLOYMENT_STATUS_UNAVAILABLE: operation status transport is unavailable.");
@@ -2,7 +2,7 @@ import { existsSync, readFileSync, writeFileSync } from "fs";
2
2
 
3
3
  import { CONFIRMATION_MODE, resolveConfirmationMode } from "./confirm-gate.mjs";
4
4
  import { confirmFirstProductionDeploy } from "./deploy-first-prod-confirm.mjs";
5
- import { reconcileProductionAppTarget } from "./deploy-app-target.mjs";
5
+ import { prodRetryCommandLine, reconcileProductionAppTarget } from "./deploy-app-target.mjs";
6
6
  import { readJsonObjectResponse } from "./http-response-json.mjs";
7
7
  import { BOLD, DIM, GREEN, RED, RESET, error, info, log, success } from "./output.mjs";
8
8
  import { prodCreateFailureMessage } from "./prod-create-failure.mjs";
@@ -52,6 +52,7 @@ export async function ensureProductionDeployTarget({
52
52
  platformFetchImpl,
53
53
  createCloudAppIfNeeded,
54
54
  explicitApp,
55
+ retryCommand: prodRetryCommandLine({ command: "deploy", args: deployArgs ?? [] }),
55
56
  infoImpl,
56
57
  errorImpl,
57
58
  exitImpl,
@@ -33,8 +33,8 @@ export function readDeploymentOperationPhase(body) {
33
33
  /**
34
34
  * One-line acceptance notice for `gencow deploy`. The identity was validated by
35
35
  * pollAcceptedDeploymentOperation (positive integer operation ID, safe correlation ID).
36
- * The status command uses the operation-scoped `--deployment` form, which accepts only `--prod` as
37
- * a trailing selector, so no `--app` is appended.
36
+ * The status command uses the operation-scoped `--deployment` form. The endpoint is addressed by the
37
+ * operation ID alone; `--prod` and `--app` are optional selectors, so no `--app` is appended.
38
38
  */
39
39
  export function formatDeploymentAcceptedLine({ operationId, correlationId, envTarget }) {
40
40
  const prodSelector = envTarget === "prod" ? " --prod" : "";
@@ -0,0 +1,13 @@
1
+ // Correlation ID shape accepted from Platform bodies and headers. Anything else is dropped, never echoed.
2
+ export const SAFE_CORRELATION_ID = /^[A-Za-z0-9_-]{8,128}$/u;
3
+
4
+ // Header names checked, in order, when the JSON body carries no correlation ID.
5
+ const SAFE_CORRELATION_HEADERS = ["x-gencow-correlation-id", "x-correlation-id", "x-request-id"];
6
+
7
+ export function responseHeaderCorrelationId(response) {
8
+ for (const name of SAFE_CORRELATION_HEADERS) {
9
+ const value = response?.headers?.get?.(name);
10
+ if (typeof value === "string" && SAFE_CORRELATION_ID.test(value)) return value;
11
+ }
12
+ return undefined;
13
+ }
@@ -1,6 +1,7 @@
1
1
  import { randomUUID } from "node:crypto";
2
2
  import { unlink } from "node:fs/promises";
3
3
 
4
+ import { responseHeaderCorrelationId } from "./http-correlation-id.mjs";
4
5
  import { safeAuthCauseCode } from "./release-auth-cause.mjs";
5
6
  import { platformFetch } from "./platform-client.mjs";
6
7
  import { createReleaseSourcePackage } from "./release-source-package.mjs";
@@ -66,17 +67,6 @@ function safePlatformOrigin(value) {
66
67
  }
67
68
  }
68
69
 
69
- // Header names checked, in order, when the JSON body carries no correlation ID.
70
- const SAFE_CORRELATION_HEADERS = ["x-gencow-correlation-id", "x-correlation-id", "x-request-id"];
71
-
72
- function responseHeaderCorrelationId(response) {
73
- for (const name of SAFE_CORRELATION_HEADERS) {
74
- const value = response?.headers?.get?.(name);
75
- if (typeof value === "string" && SAFE_CORRELATION_ID.test(value)) return value;
76
- }
77
- return undefined;
78
- }
79
-
80
70
  // `context.stage` is set only where the request is known to happen before any serving
81
71
  // change, so an untyped 5xx there can be reported as "incumbent serving preserved".
82
72
  function throwResponseError(response, fallback, body, creds = {}, context = {}) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gencow",
3
- "version": "0.1.279",
3
+ "version": "0.1.281",
4
4
  "description": "Gencow — AI Backend Engine",
5
5
  "type": "module",
6
6
  "bin": {
@@ -40,11 +40,11 @@
40
40
  "ai": "^6.0.314",
41
41
  "better-auth": "1.7.11",
42
42
  "postgres": "^3.4.9",
43
- "@gencow/core": "0.1.47",
44
43
  "@gencow/client": "0.2.8",
45
44
  "@gencow/react": "0.2.8",
46
- "@gencow/shared": "0.0.0",
47
- "@gencow/server": "0.1.3"
45
+ "@gencow/server": "0.1.3",
46
+ "@gencow/core": "0.1.47",
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",