grada-run 0.42.0 → 0.43.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.
Files changed (63) hide show
  1. package/bin/cli.js +1 -1
  2. package/package.json +1 -1
  3. package/src/commands/add.js +93 -11
  4. package/src/commands/alerts.js +66 -30
  5. package/src/commands/apply.js +65 -10
  6. package/src/commands/db/backup.js +1 -2
  7. package/src/commands/db/connect.js +10 -4
  8. package/src/commands/db/enable-vector.js +19 -6
  9. package/src/commands/db/import.js +114 -22
  10. package/src/commands/db/migrate.js +82 -74
  11. package/src/commands/db/restore.js +38 -6
  12. package/src/commands/destroy.js +20 -3
  13. package/src/commands/diagnose.js +40 -21
  14. package/src/commands/doctor.js +27 -2
  15. package/src/commands/domain.js +20 -2
  16. package/src/commands/drift.js +2 -1
  17. package/src/commands/eject.js +13 -2
  18. package/src/commands/gc.js +100 -3
  19. package/src/commands/init.js +75 -9
  20. package/src/commands/logs.js +3 -1
  21. package/src/commands/mcp.js +2 -1
  22. package/src/commands/rollback.js +54 -5
  23. package/src/commands/secrets.js +260 -64
  24. package/src/commands/status.js +4 -1
  25. package/src/commands/sync-ai.js +4 -3
  26. package/src/commands/wake.js +45 -1
  27. package/src/data/action-pins.js +26 -0
  28. package/src/utils/addons.js +9 -6
  29. package/src/utils/ai-rules.js +3 -2
  30. package/src/utils/backup.js +16 -2
  31. package/src/utils/command.js +20 -0
  32. package/src/utils/db-tunnel.js +15 -5
  33. package/src/utils/detector.js +8 -0
  34. package/src/utils/ecs-runner.js +16 -3
  35. package/src/utils/frameworks.js +14 -2
  36. package/src/utils/generator.js +21 -13
  37. package/src/utils/prompts.js +20 -4
  38. package/src/utils/resolvers.js +9 -7
  39. package/src/utils/revision-discovery.js +19 -7
  40. package/templates/docker/django.Dockerfile +2 -2
  41. package/templates/docker/go.Dockerfile +2 -2
  42. package/templates/docker/nestjs.Dockerfile +2 -2
  43. package/templates/docker/nextjs.Dockerfile +3 -3
  44. package/templates/docker/node.Dockerfile +2 -2
  45. package/templates/docker/nuxt.Dockerfile +2 -2
  46. package/templates/docker/python.Dockerfile +2 -2
  47. package/templates/docker/rails.Dockerfile +2 -2
  48. package/templates/docker/static.Dockerfile +2 -2
  49. package/templates/docker/svelte.Dockerfile +2 -2
  50. package/templates/github/deploy-lambda.yml +55 -12
  51. package/templates/github/deploy-static.yml +24 -6
  52. package/templates/github/deploy.yml +96 -13
  53. package/templates/github/drift.yml +68 -5
  54. package/templates/github/preview-lambda.yml +137 -13
  55. package/templates/github/preview.yml +135 -13
  56. package/templates/github/teardown.yml +71 -12
  57. package/templates/terraform/addons/s3.tf +35 -0
  58. package/templates/terraform/backend.tf +7 -0
  59. package/templates/terraform/database-mysql.tf +9 -1
  60. package/templates/terraform/database.tf +9 -1
  61. package/templates/terraform/oidc.tf +3 -2
  62. package/templates/terraform/secrets.tf +6 -4
  63. package/templates/terraform/worker.tf +7 -0
@@ -177,6 +177,9 @@ async function runRollbackMain(input = {}) {
177
177
 
178
178
  let targetTaskDefArn;
179
179
  let targetRevisionNum;
180
+ // Case A only: the requested web revision's image, for worker
181
+ // pairing (BUG-153). Null in Case B and when undescribable.
182
+ let requestedWebImage = null;
180
183
 
181
184
  if (options.revision !== undefined && options.revision !== null && String(options.revision).trim() !== '') {
182
185
  // Case A: explicit revision.
@@ -233,6 +236,22 @@ async function runRollbackMain(input = {}) {
233
236
  exitCode: 1,
234
237
  });
235
238
  }
239
+ // BUG-127: a NEWER explicit revision would silently
240
+ // roll FORWARD — rollback only moves to older revisions.
241
+ if (Number.isInteger(Number(targetRevisionNum)) && Number.isInteger(currentRevNum)
242
+ && Number(targetRevisionNum) > currentRevNum) {
243
+ s.stop(color.red('Revision is newer than deployed.'));
244
+ return failCommand({
245
+ message: `\n✖ Revision ${targetRevisionNum} is newer than the deployed revision ${currentRevNum} — rollback only moves to older revisions.`,
246
+ hint: ' Deploy again to move forward, or request a revision older than the deployed one.\n',
247
+ event: 'rollback_run',
248
+ telemetry: { projectName, error_code: 'NEWER_REVISION_REQUESTED' },
249
+ reason: 'rollback-newer-revision',
250
+ resultExtra: { cluster, service, region },
251
+ exitCode: 1,
252
+ });
253
+ }
254
+ requestedWebImage = descResp.taskDefinition.containerDefinitions?.[0]?.image ?? null;
236
255
  s.stop(`Resolved revision ${targetRevisionNum}.`);
237
256
  } else {
238
257
  // Case B: discover eligible older revisions via the shared seam.
@@ -333,8 +352,38 @@ async function runRollbackMain(input = {}) {
333
352
  exitCode: 1,
334
353
  });
335
354
  }
336
- const workerTargetArn = pickNewestEligible(workerEligible);
337
- workerTarget = { service: workerService, taskDefArn: workerTargetArn, revision: workerTargetArn.split(':').pop() };
355
+ // BUG-153: in Case A the worker target pairs with the
356
+ // REQUESTED web image (deploys pin both to the same SHA) —
357
+ // newest-previous regardless would skew the pairing. When
358
+ // no eligible worker revision carries the web image, fall
359
+ // back to newest-previous, loudly when both images are
360
+ // known and differ. Case B (newest-previous web) keeps
361
+ // newest-previous worker: paired by construction.
362
+ let workerTargetArn = pickNewestEligible(workerEligible);
363
+ let workerImageMatched = null;
364
+ if (requestedWebImage) {
365
+ const settled = await Promise.allSettled(
366
+ workerEligible.map((workerArn) => ecsClient.send(new DescribeTaskDefinitionCommand({ taskDefinition: workerArn })))
367
+ );
368
+ const withImages = workerEligible.map((workerArn, index) => ({
369
+ workerArn,
370
+ image: settled[index].status === 'fulfilled'
371
+ ? settled[index].value?.taskDefinition?.containerDefinitions?.[0]?.image ?? null
372
+ : null,
373
+ }));
374
+ const match = withImages.find((entry) => entry.image === requestedWebImage);
375
+ if (match) {
376
+ workerTargetArn = match.workerArn;
377
+ workerImageMatched = true;
378
+ } else {
379
+ workerImageMatched = false;
380
+ const newestImage = withImages[0]?.image ?? null;
381
+ if (newestImage && newestImage !== requestedWebImage) {
382
+ console.log(color.yellow(`⚠ No worker revision carries the requested web image ${requestedWebImage} — rolling the worker back to newest-previous (${newestImage}) instead. Web and worker will run different images.`));
383
+ }
384
+ }
385
+ }
386
+ workerTarget = { service: workerService, taskDefArn: workerTargetArn, revision: workerTargetArn.split(':').pop(), imageMatched: workerImageMatched };
338
387
  }
339
388
  }
340
389
 
@@ -358,7 +407,7 @@ async function runRollbackMain(input = {}) {
358
407
  }));
359
408
  }
360
409
 
361
- if (options.skipWait === true) {
410
+ if (options.skipWait === true || options.skipWait === 'true') {
362
411
  const skipLabel = workerTarget
363
412
  ? `revision ${targetRevisionNum} (web) and ${workerTarget.revision} (worker)`
364
413
  : `revision ${targetRevisionNum}`;
@@ -367,7 +416,7 @@ async function runRollbackMain(input = {}) {
367
416
  projectName,
368
417
  targetRevision: String(targetRevisionNum),
369
418
  skipWait: true,
370
- ...(workerTarget ? { workerTargetRevision: String(workerTarget.revision) } : {}),
419
+ ...(workerTarget ? { workerTargetRevision: String(workerTarget.revision), workerImageMatched: workerTarget.imageMatched ?? undefined } : {}),
371
420
  });
372
421
  outro(color.green(`Rollback to ${skipLabel} initiated! 🚀`));
373
422
  return {
@@ -402,7 +451,7 @@ async function runRollbackMain(input = {}) {
402
451
  await trackSuccess('rollback_run', {
403
452
  projectName,
404
453
  targetRevision: String(targetRevisionNum),
405
- ...(workerTarget ? { workerTargetRevision: String(workerTarget.revision) } : {}),
454
+ ...(workerTarget ? { workerTargetRevision: String(workerTarget.revision), workerImageMatched: workerTarget.imageMatched ?? undefined } : {}),
406
455
  });
407
456
  outro(color.green(workerTarget
408
457
  ? `Services successfully rolled back to ${doneLabel}! 🚀`
@@ -9,10 +9,11 @@ import { trackEvent, flushTelemetry, trackSuccess, trackFailure, setActiveComman
9
9
  import { failCommand, refuseIfNonInteractive } from '../utils/command.js';
10
10
  import { atomicWriteFile } from '../utils/atomic-write.js';
11
11
  import { handleAuthErrorBranch, resolveClient } from '../utils/aws.js';
12
- import { readTerraformComputeTarget, guardComputeTarget, readResourceSuffix, readRenderedContainerEnvNames } from '../utils/resolvers.js';
12
+ import { readTerraformComputeTarget, guardComputeTarget, readResourceSuffix, readRenderedContainerEnvNames, readTerraformProjectName } from '../utils/resolvers.js';
13
13
  import { ADDON_ENV_VARS, ADDON_REGISTRY } from '../utils/addons.js';
14
14
  import { requireManagedTree } from '../utils/terraform-metadata.js';
15
15
  import { truncateSpinnerText } from '../utils/spinner-text.js';
16
+ import { withProjectLock } from '../utils/file-lock.js';
16
17
 
17
18
  // Static sites provision no secrets vault, so push/pull/audit would fail
18
19
  // obscurely against a Secrets Manager secret that can never exist. One
@@ -63,9 +64,11 @@ function resolveSecretsFile(envFilePath) {
63
64
  }
64
65
 
65
66
  function resolveSecretsProject(projectName) {
66
- return (typeof projectName === 'string' && projectName.trim())
67
- ? projectName.trim()
68
- : path.basename(process.cwd());
67
+ // BUG-162: the terraform project name (what provisioned the vault)
68
+ // outranks the directory basename — a renamed directory otherwise
69
+ // silently aims at a missing vault.
70
+ if (typeof projectName === 'string' && projectName.trim()) return projectName.trim();
71
+ return readTerraformProjectName(process.cwd()) || path.basename(process.cwd());
69
72
  }
70
73
 
71
74
  // Vault identifiers carry the tree's resource suffix (matching the
@@ -84,9 +87,15 @@ export async function resolveSecretsRegion(options = {}) {
84
87
  try {
85
88
  const backendTfPath = path.join(process.cwd(), 'terraform', 'backend.tf');
86
89
  const backendTfContent = await fs.readFile(backendTfPath, 'utf-8');
87
- const regionMatch = backendTfContent.match(/region\s*=\s*"([^"]+)"/);
88
- if (regionMatch && !regionMatch[1].includes('{{')) {
89
- return regionMatch[1];
90
+ // BUG-164: scan line-wise skipping comments — the old whole-file
91
+ // regex resolved commented-out regions (or legacy lines) as live.
92
+ for (const rawLine of backendTfContent.split('\n')) {
93
+ const line = rawLine.trim();
94
+ if (line === '' || line.startsWith('#')) continue;
95
+ const regionMatch = line.match(/region\s*=\s*"([^"]+)"/);
96
+ if (regionMatch && !regionMatch[1].includes('{{')) {
97
+ return regionMatch[1];
98
+ }
90
99
  }
91
100
  } catch {
92
101
  // Fall through to the product default region if the file is missing or unreadable
@@ -176,17 +185,21 @@ export async function getManagedEnvNames(cwd = process.cwd(), target = 'ecs') {
176
185
  // the base set above (fail-closed; the TF precondition backstops
177
186
  // real collisions there).
178
187
  if (!tfFiles.includes('worker.tf')) names.delete('NODE_ENV');
179
- const dbFile = tfFiles.find((file) => file.startsWith('database') && file.endsWith('.tf'));
180
- if (dbFile) {
188
+ const dbFiles = tfFiles.filter((file) => file.startsWith('database') && file.endsWith('.tf'));
189
+ if (dbFiles.length > 0) {
181
190
  for (const name of DB_MANAGED_ENV) names.add(name);
182
191
  // DB_ENGINE is rendered only for non-postgres engines: including
183
192
  // it blindly would auto-skip a legitimate custom key on postgres
184
- // trees with a false "injected by Grada" notice. Unparseable
193
+ // trees with a false "injected by Grada" notice. Every database
194
+ // file is sniffed — a stale database.tf sorting before the live
195
+ // database-mysql.tf must not hide the engine. Unparseable
185
196
  // files exclude it — the preflights still catch real collisions.
186
197
  try {
187
- const dbContent = await fs.readFile(path.join(cwd, 'terraform', dbFile), 'utf-8');
188
- const engine = /engine\s*=\s*"([^"]+)"/.exec(dbContent)?.[1];
189
- if (engine && engine !== 'postgres') names.add('DB_ENGINE');
198
+ const contents = await Promise.all(
199
+ dbFiles.map((dbFile) => fs.readFile(path.join(cwd, 'terraform', dbFile), 'utf-8'))
200
+ );
201
+ const engines = contents.map((dbContent) => /engine\s*=\s*"([^"]+)"/.exec(dbContent)?.[1]);
202
+ if (engines.some((engine) => engine && engine !== 'postgres')) names.add('DB_ENGINE');
190
203
  } catch {
191
204
  // Fall through with the base DB set.
192
205
  }
@@ -231,6 +244,96 @@ export function diffSecretsEnvs(localSecrets = {}, remoteSecrets = {}) {
231
244
  return { missingLocally, mismatched, untrackedLocally };
232
245
  }
233
246
 
247
+ // Names the shape of a non-object vault payload for refusal messages.
248
+ function vaultPayloadShape(parsed) {
249
+ if (parsed === null) return 'null';
250
+ if (Array.isArray(parsed)) return 'array';
251
+ return typeof parsed;
252
+ }
253
+
254
+ // Validates a GetSecretValue response into the managed JSON object.
255
+ // Refuses binary payloads (BUG-145: they read as "empty" and a push
256
+ // would full-replace them away without the removal confirm) and
257
+ // non-object/non-string payloads (BUG-159: phantom `0,1` removals,
258
+ // garbage pull keys, TypeError crashes). A response carrying neither
259
+ // field means an empty/new vault — live AWS never returns that shape
260
+ // for a valued secret.
261
+ function vaultPayloadObject(resp, secretId) {
262
+ if (resp.SecretBinary != null) {
263
+ const binaryError = new Error(`Refusing to operate on "${secretId}": the vault payload is binary, not the JSON object secrets commands manage. Migrate it to a JSON string payload first.`);
264
+ binaryError.code = 'REMOTE_SECRET_BINARY';
265
+ throw binaryError;
266
+ }
267
+ if (resp.SecretString == null) return {};
268
+ let parsed;
269
+ try {
270
+ parsed = JSON.parse(resp.SecretString);
271
+ } catch {
272
+ const jsonError = new Error('Remote secret payload is not valid JSON.');
273
+ jsonError.code = 'REMOTE_SECRET_MALFORMED';
274
+ throw jsonError;
275
+ }
276
+ const valuesOk = parsed !== null && typeof parsed === 'object' && !Array.isArray(parsed)
277
+ && Object.values(parsed).every((value) => typeof value === 'string');
278
+ if (!valuesOk) {
279
+ const shapeError = new Error(`Remote secret payload must be a JSON object of string values, found ${vaultPayloadShape(parsed)}.`);
280
+ shapeError.code = 'REMOTE_SECRET_MALFORMED';
281
+ throw shapeError;
282
+ }
283
+ return parsed;
284
+ }
285
+
286
+ // Lenient key-attempt extractor for drop accounting: what key did this
287
+ // line TRY to define? Calibrated against the real dotenv parse — a line
288
+ // is dropped only when its attempted key is absent from it.
289
+ const ATTEMPTED_KEY_RE = /^\s*([^=:#\s][^=:]*?)\s*[=:]/;
290
+
291
+ // Finds .env input lines dotenv silently dropped plus duplicate keys
292
+ // (BUG-143). Multiline double-quoted values span lines — continuation
293
+ // lines are value text, not dropped lines. Pure and unit-tested via
294
+ // push output.
295
+ function analyzeEnvLines(envContent, parsedSecrets) {
296
+ const dropped = [];
297
+ const seen = new Map();
298
+ const lines = String(envContent ?? '').split('\n');
299
+ let continuation = false;
300
+ lines.forEach((rawLine, index) => {
301
+ const line = rawLine.replace(/\r$/, '');
302
+ const wasContinuation = continuation;
303
+ // Track unescaped double quotes across lines (backslash escapes).
304
+ let open = continuation;
305
+ for (let i = 0; i < line.length; i += 1) {
306
+ if (line[i] === '\\') {
307
+ i += 1;
308
+ } else if (line[i] === '"') {
309
+ open = !open;
310
+ }
311
+ }
312
+ continuation = open;
313
+ if (wasContinuation) return;
314
+ const trimmed = line.trim();
315
+ if (trimmed === '' || trimmed.startsWith('#')) return;
316
+ const effective = line.replace(/^\s*export\s+/, '');
317
+ const match = ATTEMPTED_KEY_RE.exec(effective);
318
+ if (!match) {
319
+ dropped.push({ line: index + 1, content: trimmed });
320
+ return;
321
+ }
322
+ const attempted = match[1].trim();
323
+ if (!Object.hasOwn(parsedSecrets, attempted)) {
324
+ dropped.push({ line: index + 1, content: trimmed });
325
+ return;
326
+ }
327
+ if (!seen.has(attempted)) seen.set(attempted, []);
328
+ seen.get(attempted).push(index + 1);
329
+ });
330
+ const duplicates = [];
331
+ for (const [key, dupLines] of seen) {
332
+ if (dupLines.length > 1) duplicates.push({ key, lines: dupLines });
333
+ }
334
+ return { dropped, duplicates };
335
+ }
336
+
234
337
  // Shared failure tail for push/pull/audit: identical telemetry stamps,
235
338
  // not-found guidance, auth branch, and spinner/exit routing, so the
236
339
  // three subcommands cannot drift apart again.
@@ -245,6 +348,7 @@ async function handleSecretsCatch({
245
348
  notFoundGuidance,
246
349
  spinnerStopped = false,
247
350
  stackTraceOnTypeError = false,
351
+ postVaultStep = null,
248
352
  }) {
249
353
  await trackFailure(event, {
250
354
  projectName,
@@ -258,13 +362,25 @@ async function handleSecretsCatch({
258
362
  for (const line of notFoundGuidance) console.log(line);
259
363
  } else if (handleAuthErrorBranch(error, s, opts)) {
260
364
  return;
261
- } else if (spinnerStopped) {
365
+ }
366
+ // BUG-161: failures after the vault write (keys file, ECS restart)
367
+ // must report the vault as updated with the recovery step — a bare
368
+ // "Failed to push" lies about what succeeded.
369
+ const postVaultHints = {
370
+ 'secret_keys.json write': ' Re-run the push to regenerate terraform/secret_keys.json — the vault already holds the new values.\n',
371
+ 'ECS restart': ' Trigger the restart by hand (aws ecs update-service --force-new-deployment) or deploy — the vault already holds the new values.\n',
372
+ };
373
+ const failureMessage = postVaultStep === null
374
+ ? `❌ Failed to ${verb} secrets: ${error.message}`
375
+ : `❌ Vault updated, but the ${postVaultStep} failed: ${error.message}`;
376
+ const failureHint = postVaultStep === null ? null : (postVaultHints[postVaultStep] ?? null);
377
+ if (spinnerStopped) {
262
378
  // The spinner already stopped, so its stop is a no-op —
263
379
  // route the message through the failure stamp instead.
264
- return failCommand({ message: `❌ Failed to ${verb} secrets: ${error.message}`, exitCode: 1 });
265
- } else {
266
- s.stop(`❌ Failed to ${verb} secrets: ${error.message}`);
380
+ return failCommand({ message: failureMessage, hint: failureHint, exitCode: 1 });
267
381
  }
382
+ s.stop(failureMessage);
383
+ if (failureHint) console.log(color.dim(failureHint));
268
384
 
269
385
  return failCommand({ exitCode: 1 });
270
386
  }
@@ -299,6 +415,27 @@ export async function pushSecrets(envFilePath, projectName, options = {}) {
299
415
  }
300
416
 
301
417
  async function pushSecretsMain(envFilePath, projectName, options = {}) {
418
+ // BUG-144: push is read-modify-full-replace — serialize local
419
+ // concurrent pushes like every other mutating family (the re-read
420
+ // before UpdateSecret below covers cross-machine second writers).
421
+ return withProjectLock(process.cwd(), () => pushSecretsInner(envFilePath, projectName, options));
422
+ }
423
+
424
+ // Builds the secrets_pushed success payload shared by the full-push and
425
+ // all-skipped early-return paths, so the two can never skew key names.
426
+ function pushSuccessTelemetry({ projectName, pushableSecrets, keysChanged, removed, collisions, skipped, staleSkipped }) {
427
+ return {
428
+ projectName,
429
+ secret_count: Object.keys(pushableSecrets).length,
430
+ keys_changed: keysChanged,
431
+ removed_count: removed.length,
432
+ managed_collisions: collisions.length,
433
+ skipped_count: skipped.length,
434
+ stale_skipped_count: staleSkipped.length,
435
+ };
436
+ }
437
+
438
+ async function pushSecretsInner(envFilePath, projectName, options = {}) {
302
439
  const refused = await secretsManagedTreeGuard('push', 'secrets_pushed', projectName, options);
303
440
  if (refused) return refused;
304
441
  const pushGuard = secretsTargetGuard('push');
@@ -319,13 +456,17 @@ async function pushSecretsMain(envFilePath, projectName, options = {}) {
319
456
  resolvedFilePath = '.env';
320
457
  }
321
458
 
322
- const resolvedProjectName = (typeof explicitProjectName === 'string' && explicitProjectName.trim())
323
- ? explicitProjectName.trim()
324
- : path.basename(process.cwd());
459
+ // BUG-162: shared helper — the terraform project name outranks the
460
+ // directory basename (a renamed directory must not aim at a phantom).
461
+ const resolvedProjectName = resolveSecretsProject(explicitProjectName);
325
462
 
326
463
  const s = spinner();
327
464
  s.start(truncateSpinnerText(`Reading ${envFilePath} and pushing to AWS Secrets Manager...`));
328
465
 
466
+ // BUG-161: write progress for the catch below — names the post-vault
467
+ // follow-up step when one fails, so the report says the vault is
468
+ // updated instead of misreporting a failed push.
469
+ let postVaultStep = null;
329
470
  try {
330
471
  // 1. Read and parse the local .env file
331
472
  const envPath = path.resolve(process.cwd(), resolvedFilePath);
@@ -377,6 +518,23 @@ async function pushSecretsMain(envFilePath, projectName, options = {}) {
377
518
 
378
519
  const parsedSecrets = dotenv.parse(envContent);
379
520
 
521
+ // BUG-143: dotenv silently drops lines it cannot parse (and
522
+ // last-wins duplicates) — name them so the success count below
523
+ // reconciles with the file. Runs before the empty early-return
524
+ // so an all-dropped file explains itself.
525
+ const { dropped: droppedLines, duplicates: duplicateKeys } = analyzeEnvLines(envContent, parsedSecrets);
526
+ if (droppedLines.length > 0 || duplicateKeys.length > 0) {
527
+ s.stop('Pre-flight checks done.');
528
+ if (droppedLines.length > 0) {
529
+ console.log(color.yellow(`⚠️ Warning: ${droppedLines.length} ${droppedLines.length === 1 ? 'line' : 'lines'} in ${resolvedFilePath} could not be parsed and ${droppedLines.length === 1 ? 'was' : 'were'} ignored:`));
530
+ for (const { line, content } of droppedLines) console.log(color.yellow(`- line ${line}: ${content}`));
531
+ }
532
+ for (const { key, lines } of duplicateKeys) {
533
+ console.log(color.yellow(`⚠️ Warning: duplicate key "${key}" (lines ${lines.join(', ')}) — last value wins.`));
534
+ }
535
+ s.start(truncateSpinnerText(`Reading ${envFilePath} and pushing to AWS Secrets Manager...`));
536
+ }
537
+
380
538
  if (Object.keys(parsedSecrets).length === 0) {
381
539
  s.stop('No secrets found in file.');
382
540
  return;
@@ -416,6 +574,15 @@ async function pushSecretsMain(envFilePath, projectName, options = {}) {
416
574
  }
417
575
  if (Object.keys(pushableSecrets).length === 0) {
418
576
  console.log(color.dim('Nothing left to push — the vault and secret_keys.json are unchanged.'));
577
+ await trackSuccess('secrets_pushed', pushSuccessTelemetry({
578
+ projectName: resolvedProjectName,
579
+ pushableSecrets,
580
+ keysChanged: [],
581
+ removed: [],
582
+ collisions,
583
+ skipped,
584
+ staleSkipped: [],
585
+ }));
419
586
  return;
420
587
  }
421
588
  if (needsPreWriteOutput) s.start(truncateSpinnerText(`Reading ${envFilePath} and pushing to AWS Secrets Manager...`));
@@ -429,19 +596,14 @@ async function pushSecretsMain(envFilePath, projectName, options = {}) {
429
596
 
430
597
  // 4. Fetch the existing secret payload to detect key changes (safe on new vaults)
431
598
  let existingKeys = null;
599
+ let firstSecretString = null;
600
+ const vaultId = `${secretVaultId(resolvedProjectName)}`;
432
601
  try {
433
602
  const existingResp = await client.send(new GetSecretValueCommand({
434
- SecretId: `${secretVaultId(resolvedProjectName)}`,
603
+ SecretId: vaultId,
435
604
  }));
436
- if (existingResp.SecretString) {
437
- try {
438
- existingKeys = Object.keys(JSON.parse(existingResp.SecretString));
439
- } catch {
440
- existingKeys = [];
441
- }
442
- } else {
443
- existingKeys = [];
444
- }
605
+ firstSecretString = existingResp.SecretString ?? null;
606
+ existingKeys = Object.keys(vaultPayloadObject(existingResp, vaultId));
445
607
  } catch (fetchError) {
446
608
  if (fetchError && fetchError.name === 'ResourceNotFoundException') {
447
609
  existingKeys = [];
@@ -494,9 +656,32 @@ async function pushSecretsMain(envFilePath, projectName, options = {}) {
494
656
  s.start(truncateSpinnerText(`Reading ${envFilePath} and pushing to AWS Secrets Manager...`));
495
657
  }
496
658
 
659
+ // BUG-144: the confirm above can wait unboundedly — re-read the
660
+ // vault immediately before the full-replace write and abort when
661
+ // a second writer moved the payload since the first read.
662
+ let rereadSecretString = null;
663
+ let rereadBinary = false;
664
+ try {
665
+ const rereadResp = await client.send(new GetSecretValueCommand({ SecretId: vaultId }));
666
+ rereadSecretString = rereadResp.SecretString ?? null;
667
+ rereadBinary = rereadResp.SecretBinary != null;
668
+ } catch (rereadError) {
669
+ if (!rereadError || rereadError.name !== 'ResourceNotFoundException') throw rereadError;
670
+ }
671
+ if (rereadBinary || rereadSecretString !== firstSecretString) {
672
+ s.stop('Pre-flight checks done.');
673
+ return failCommand({
674
+ message: `\n✖ The vault changed while this push was waiting (a second writer updated it) — refusing to overwrite. Re-run the push to merge with the latest vault content.\n`,
675
+ event: 'secrets_pushed',
676
+ telemetry: { projectName: resolvedProjectName, error_code: 'SECRETS_VAULT_CHANGED' },
677
+ errorCode: 'SECRETS_VAULT_CHANGED',
678
+ reason: 'secrets-vault-changed',
679
+ });
680
+ }
681
+
497
682
  // 5. Update the secret string in AWS
498
683
  const command = new UpdateSecretCommand({
499
- SecretId: `${secretVaultId(resolvedProjectName)}`,
684
+ SecretId: vaultId,
500
685
  SecretString: JSON.stringify(pushableSecrets),
501
686
  });
502
687
 
@@ -505,7 +690,19 @@ async function pushSecretsMain(envFilePath, projectName, options = {}) {
505
690
  const keys = Object.keys(pushableSecrets);
506
691
  const keysFilePath = path.join(process.cwd(), 'terraform', 'secret_keys.json');
507
692
 
508
- await atomicWriteFile(keysFilePath, JSON.stringify(keys, null, 2));
693
+ // BUG-160: skip the write when the file is already byte-identical
694
+ // so unchanged pushes don't dirty `git status`.
695
+ const nextKeysContent = JSON.stringify(keys, null, 2);
696
+ let keysUnchanged = false;
697
+ try {
698
+ keysUnchanged = (await fs.readFile(keysFilePath, 'utf-8')) === nextKeysContent;
699
+ } catch {
700
+ keysUnchanged = false;
701
+ }
702
+ if (!keysUnchanged) {
703
+ postVaultStep = 'secret_keys.json write';
704
+ await atomicWriteFile(keysFilePath, nextKeysContent);
705
+ }
509
706
 
510
707
  s.stop(`✅ Successfully pushed ${Object.keys(pushableSecrets).length} secrets to AWS (${targetRegion || 'default region'})!`);
511
708
 
@@ -572,6 +769,7 @@ async function pushSecretsMain(envFilePath, projectName, options = {}) {
572
769
  const restartSpinner = spinner();
573
770
  restartSpinner.start('Triggering rolling ECS restart...');
574
771
  try {
772
+ postVaultStep = 'ECS restart';
575
773
  await ecsClient.send(new UpdateServiceCommand({
576
774
  cluster,
577
775
  service,
@@ -599,15 +797,15 @@ async function pushSecretsMain(envFilePath, projectName, options = {}) {
599
797
  }
600
798
  console.log(color.blue(`\n📘 Learn how secrets reach your app: ${color.underline('https://github.com/grada-run/grada/blob/main/apps/docs/src/content/docs/guides/secrets-management.md')}`));
601
799
 
602
- await trackSuccess('secrets_pushed', {
800
+ await trackSuccess('secrets_pushed', pushSuccessTelemetry({
603
801
  projectName: resolvedProjectName,
604
- secret_count: Object.keys(pushableSecrets).length,
605
- keys_changed: keysChanged,
606
- removed_count: removed.length,
607
- managed_collisions: collisions.length,
608
- skipped_count: skipped.length,
609
- stale_skipped_count: staleSkipped.length
610
- });
802
+ pushableSecrets,
803
+ keysChanged,
804
+ removed,
805
+ collisions,
806
+ skipped,
807
+ staleSkipped,
808
+ }));
611
809
  return { keysChanged, skipped };
612
810
 
613
811
  } catch (error) {
@@ -618,6 +816,7 @@ async function pushSecretsMain(envFilePath, projectName, options = {}) {
618
816
  s,
619
817
  opts,
620
818
  projectName: resolvedProjectName,
819
+ postVaultStep,
621
820
  notFoundMessage: `❌ Secrets Vault "${secretVaultId(resolvedProjectName)}" does not exist in AWS yet.`,
622
821
  notFoundGuidance: [
623
822
  `Run ${color.cyan('npx --yes grada-run apply')} first to provision the infrastructure and Secrets Manager vault.`,
@@ -662,16 +861,7 @@ async function pullSecretsMain(envFilePath, projectName, options = {}) {
662
861
  SecretId: `${secretVaultId(resolvedProjectName)}`,
663
862
  }));
664
863
 
665
- let remoteSecrets = {};
666
- if (resp.SecretString) {
667
- try {
668
- remoteSecrets = JSON.parse(resp.SecretString);
669
- } catch {
670
- const jsonError = new Error('Remote secret payload is not valid JSON.');
671
- jsonError.code = 'REMOTE_SECRET_MALFORMED';
672
- throw jsonError;
673
- }
674
- }
864
+ const remoteSecrets = vaultPayloadObject(resp, `${secretVaultId(resolvedProjectName)}`);
675
865
  const remoteKeys = Object.keys(remoteSecrets);
676
866
  s.stop('Remote secrets fetched.');
677
867
  remoteFetched = true;
@@ -679,8 +869,9 @@ async function pullSecretsMain(envFilePath, projectName, options = {}) {
679
869
  const envPath = path.resolve(process.cwd(), resolvedFilePath);
680
870
  let localSecrets = {};
681
871
  let localExists = true;
872
+ let envContent = null;
682
873
  try {
683
- const envContent = await fs.readFile(envPath, 'utf-8');
874
+ envContent = await fs.readFile(envPath, 'utf-8');
684
875
  localSecrets = dotenv.parse(envContent);
685
876
  } catch (fsError) {
686
877
  if (fsError.code === 'ENOENT') {
@@ -726,7 +917,16 @@ async function pullSecretsMain(envFilePath, projectName, options = {}) {
726
917
  ...Object.keys(localSecrets),
727
918
  ...remoteKeys.filter((k) => !Object.hasOwn(localSecrets, k)),
728
919
  ];
729
- const unrepresentable = orderedKeys.filter((key) =>
920
+ // The merge appends remote-only keys (reviving local deletions)
921
+ // — name what came back instead of syncing silently.
922
+ const appended = localExists ? remoteKeys.filter((k) => !Object.hasOwn(localSecrets, k)) : [];
923
+ if (appended.length > 0) {
924
+ console.log(color.yellow(`⚠️ Pulled ${appended.length} key${appended.length === 1 ? '' : 's'} not present locally (revived deletions?): ${appended.join(', ')}`));
925
+ }
926
+ // Only keys being added/overwritten can block the write: a
927
+ // local-only exotic value must not abort an unrelated sync.
928
+ const writtenKeys = remoteKeys.filter((key) => !Object.hasOwn(localSecrets, key) || overwrite);
929
+ const unrepresentable = writtenKeys.filter((key) =>
730
930
  key === '__proto__' || quoteEnvValue(merged[key]) === null
731
931
  );
732
932
  if (unrepresentable.length > 0) {
@@ -738,7 +938,12 @@ async function pullSecretsMain(envFilePath, projectName, options = {}) {
738
938
  abort.code = 'SECRETS_PULL_UNREPRESENTABLE_VALUE';
739
939
  throw abort;
740
940
  }
741
- await fs.writeFile(envPath, serializeEnv(orderedKeys, merged));
941
+ // Skip the write when the file is already byte-identical so
942
+ // in-sync pulls don't strip comments/formatting for no reason.
943
+ const nextEnvContent = serializeEnv(orderedKeys, merged);
944
+ if (!localExists || envContent !== nextEnvContent) {
945
+ await fs.writeFile(envPath, nextEnvContent);
946
+ }
742
947
 
743
948
  outro(`Successfully synced ${remoteKeys.length} secrets to ${resolvedFilePath}`);
744
949
 
@@ -748,7 +953,7 @@ async function pullSecretsMain(envFilePath, projectName, options = {}) {
748
953
  });
749
954
  await flushTelemetry();
750
955
 
751
- return { synced: remoteKeys.length, file: resolvedFilePath, overwritten: overwrite, conflicts: mismatched };
956
+ return { synced: remoteKeys.length, file: resolvedFilePath, overwritten: overwrite, conflicts: mismatched, appended };
752
957
  } catch (error) {
753
958
  return handleSecretsCatch({
754
959
  error,
@@ -799,16 +1004,7 @@ async function auditSecretsMain(envFilePath, projectName, options = {}) {
799
1004
  SecretId: `${secretVaultId(resolvedProjectName)}`,
800
1005
  }));
801
1006
 
802
- let remoteSecrets = {};
803
- if (resp.SecretString) {
804
- try {
805
- remoteSecrets = JSON.parse(resp.SecretString);
806
- } catch {
807
- const jsonError = new Error('Remote secret payload is not valid JSON.');
808
- jsonError.code = 'REMOTE_SECRET_MALFORMED';
809
- throw jsonError;
810
- }
811
- }
1007
+ const remoteSecrets = vaultPayloadObject(resp, `${secretVaultId(resolvedProjectName)}`);
812
1008
 
813
1009
  const envPath = path.resolve(process.cwd(), resolvedFilePath);
814
1010
  let localSecrets = {};
@@ -502,7 +502,10 @@ async function runStatusMain(input = {}) {
502
502
  return;
503
503
  }
504
504
 
505
- const alarmsResp = await cloudWatchClient.send(new DescribeAlarmsCommand({ AlarmNamePrefix: projectName, MaxRecords: 100 }));
505
+ // Namespaced stem (BUG-151): the raw project prefix merged prod
506
+ // + preview alarms into one verdict and could auto-diagnose the
507
+ // wrong cluster. Paired with workspace-aware alarm names (BUG-132).
508
+ const alarmsResp = await cloudWatchClient.send(new DescribeAlarmsCommand({ AlarmNamePrefix: `${namespacedProject}${resourceSuffix}`, MaxRecords: 100 }));
506
509
  const rawAlarms = [...(alarmsResp.MetricAlarms || []), ...(alarmsResp.CompositeAlarms || [])];
507
510
 
508
511
  const health = getServiceHealth(serviceDesc, rawAlarms);
@@ -7,6 +7,7 @@ import { getBaseRules, getCursorRules, injectManagedBlock } from '../utils/ai-ru
7
7
  import { readTerraformComputeTarget, resolveHeadless } from '../utils/resolvers.js';
8
8
  import { trackEvent, flushTelemetry, setActiveCommandName, resetActiveCommandName } from '../core/telemetry.js';
9
9
  import { failCommand } from '../utils/command.js';
10
+ import { atomicWriteFile } from '../utils/atomic-write.js';
10
11
 
11
12
  function getProjectContext(cwd) {
12
13
  const context = { region: '', port: '', target: readTerraformComputeTarget(cwd) };
@@ -72,13 +73,13 @@ async function syncAiMain(options = {}) {
72
73
  if (assistants.includes('cursor')) {
73
74
  const cursorDir = path.join(cwd, '.cursor', 'rules');
74
75
  if (!fsSync.existsSync(cursorDir)) fsSync.mkdirSync(cursorDir, { recursive: true });
75
- fsSync.writeFileSync(path.join(cursorDir, 'grada.mdc'), getCursorRules(context));
76
+ await atomicWriteFile(path.join(cursorDir, 'grada.mdc'), getCursorRules(context));
76
77
  }
77
78
 
78
79
  if (assistants.includes('roo')) {
79
80
  const rooDir = path.join(cwd, '.roo', 'rules');
80
81
  if (!fsSync.existsSync(rooDir)) fsSync.mkdirSync(rooDir, { recursive: true });
81
- fsSync.writeFileSync(path.join(rooDir, 'grada.md'), getBaseRules(context));
82
+ await atomicWriteFile(path.join(rooDir, 'grada.md'), getBaseRules(context));
82
83
  }
83
84
 
84
85
  if (assistants.includes('trae')) {
@@ -90,7 +91,7 @@ async function syncAiMain(options = {}) {
90
91
  if (assistants.includes('continue')) {
91
92
  const promptsDir = path.join(cwd, '.prompts');
92
93
  if (!fsSync.existsSync(promptsDir)) fsSync.mkdirSync(promptsDir, { recursive: true });
93
- fsSync.writeFileSync(path.join(promptsDir, 'grada.prompt'), getBaseRules(context));
94
+ await atomicWriteFile(path.join(promptsDir, 'grada.prompt'), getBaseRules(context));
94
95
  }
95
96
 
96
97
  if (assistants.includes('windsurf')) {