akm-cli 0.9.11 → 0.9.13

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 (134) hide show
  1. package/CHANGELOG.md +227 -0
  2. package/STABILITY.md +6 -1
  3. package/dist/assets/hints/cli-hints-full.md +1 -1
  4. package/dist/assets/improve-strategies/consolidate.json +1 -1
  5. package/dist/assets/improve-strategies/default.json +1 -1
  6. package/dist/assets/improve-strategies/thorough.json +1 -2
  7. package/dist/assets/workflows/workflow-template.md +4 -0
  8. package/dist/cli/shared.js +16 -4
  9. package/dist/cli.js +15 -13
  10. package/dist/commands/agent/agent-dispatch.js +8 -0
  11. package/dist/commands/command/execution-source-loader.js +25 -22
  12. package/dist/commands/command/portable-template.js +4 -26
  13. package/dist/commands/config-cli.js +10 -4
  14. package/dist/commands/env/env-binding.js +10 -3
  15. package/dist/commands/env/env-cli.js +7 -0
  16. package/dist/commands/env/secret-cli.js +15 -4
  17. package/dist/commands/health/checks.js +186 -71
  18. package/dist/commands/health.js +16 -4
  19. package/dist/commands/improve/distill/quality-gate.js +2 -2
  20. package/dist/commands/improve/distill.js +28 -12
  21. package/dist/commands/improve/execution.js +1 -2
  22. package/dist/commands/improve/extract.js +82 -56
  23. package/dist/commands/improve/improve-strategies.js +26 -8
  24. package/dist/commands/improve/improve.js +14 -0
  25. package/dist/commands/improve/preparation.js +9 -6
  26. package/dist/commands/improve/reflect.js +61 -77
  27. package/dist/commands/lint/base-linter.js +10 -0
  28. package/dist/commands/lint/index.js +3 -1
  29. package/dist/commands/migrate-cli.js +6 -4
  30. package/dist/commands/proposal/drain-policies.js +22 -2
  31. package/dist/commands/proposal/drain.js +48 -6
  32. package/dist/commands/proposal/proposal-cli.js +1 -0
  33. package/dist/commands/proposal/repository.js +4 -4
  34. package/dist/commands/proposal/validators/proposal-quality-validators.js +23 -2
  35. package/dist/commands/proposal/validators/proposals.js +10 -19
  36. package/dist/commands/read/show.js +42 -31
  37. package/dist/commands/registry-cli.js +4 -2
  38. package/dist/commands/sources/init.js +4 -8
  39. package/dist/commands/sources/self-update.js +2 -2
  40. package/dist/commands/sources/source-clone.js +5 -7
  41. package/dist/commands/sources/sources-cli.js +3 -5
  42. package/dist/commands/tasks/tasks-cli.js +4 -12
  43. package/dist/commands/tasks/tasks.js +38 -35
  44. package/dist/commands/workflow-cli.js +17 -15
  45. package/dist/core/activation-policy.js +31 -3
  46. package/dist/core/adapter/execution-source.js +39 -11
  47. package/dist/core/asset/stash-meta.js +7 -41
  48. package/dist/core/common.js +8 -17
  49. package/dist/core/config/config-schema.js +3 -23
  50. package/dist/core/config/config-walker.js +56 -6
  51. package/dist/core/config/config.js +42 -17
  52. package/dist/core/config/legacy-source-shape-shim.js +79 -0
  53. package/dist/core/config/schema/embedding.js +2 -2
  54. package/dist/core/config/schema/engines.js +2 -2
  55. package/dist/core/config/schema/index-config.js +19 -21
  56. package/dist/core/config/schema/primitives.js +27 -10
  57. package/dist/core/config/schema/sources-bundles.js +1 -6
  58. package/dist/core/errors.js +4 -3
  59. package/dist/core/improve-types.js +17 -0
  60. package/dist/core/json-schema.js +1 -11
  61. package/dist/core/maintenance-barrier.js +17 -2
  62. package/dist/core/paths.js +12 -15
  63. package/dist/core/state/migrations.js +28 -0
  64. package/dist/core/state-db.js +28 -1
  65. package/dist/core/write-source.js +6 -6
  66. package/dist/indexer/bundle-identity-guard.js +3 -0
  67. package/dist/indexer/ensure-index.js +5 -0
  68. package/dist/indexer/indexer.js +11 -3
  69. package/dist/indexer/lookup/adapter-concept-owner.js +14 -3
  70. package/dist/indexer/passes/metadata.js +16 -5
  71. package/dist/indexer/search/search-fields.js +1 -30
  72. package/dist/integrations/agent/engine-resolution.js +15 -1
  73. package/dist/integrations/agent/model-map.js +16 -10
  74. package/dist/integrations/agent/prompts.js +13 -6
  75. package/dist/integrations/lockfile.js +22 -7
  76. package/dist/llm/client.js +28 -8
  77. package/dist/llm/embedders/remote.js +3 -2
  78. package/dist/llm/index-passes.js +3 -2
  79. package/dist/output/shapes/passthrough.js +9 -3
  80. package/dist/output/shapes.js +50 -3
  81. package/dist/output/text/proposal-format.js +5 -0
  82. package/dist/output/text/workflow-format.js +8 -1
  83. package/dist/scripts/akm-migrate-node.js +1737 -1392
  84. package/dist/scripts/akm-migrate.js +1736 -1391
  85. package/dist/setup/setup.js +14 -21
  86. package/dist/sources/include.js +150 -20
  87. package/dist/sources/providers/git-install.js +14 -12
  88. package/dist/sources/providers/git-provider.js +3 -3
  89. package/dist/sources/snapshot-fetchers/website-ingest.js +54 -16
  90. package/dist/sources/website-url.js +12 -4
  91. package/dist/storage/engines/sqlite-migrations.js +40 -10
  92. package/dist/storage/like-pattern.js +7 -0
  93. package/dist/storage/repositories/extract-sessions-repository.js +23 -0
  94. package/dist/storage/repositories/index-connection.js +27 -10
  95. package/dist/storage/repositories/index-entry-schema.js +19 -2
  96. package/dist/storage/repositories/index-schema.js +30 -9
  97. package/dist/storage/repositories/proposals-repository.js +2 -1
  98. package/dist/storage/repositories/task-history-repository.js +14 -7
  99. package/dist/storage/repositories/workflow-runs-repository.js +133 -11
  100. package/dist/storage/sqlite-read-snapshot.js +11 -9
  101. package/dist/tasks/backends/cron.js +34 -5
  102. package/dist/tasks/backends/launchd.js +23 -26
  103. package/dist/tasks/backends/schtasks.js +50 -3
  104. package/dist/tasks/frozen-script.js +2 -0
  105. package/dist/tasks/prepare/prepare.js +2 -7
  106. package/dist/tasks/prepare/script-capture.js +38 -6
  107. package/dist/tasks/schedule.js +154 -13
  108. package/dist/tasks/source/task-source-v3-frozen.js +0 -1
  109. package/dist/tasks/source/task-source-v4.js +0 -1
  110. package/dist/workflows/exec/child-workflow.js +2 -3
  111. package/dist/workflows/exec/exec-unit.js +3 -4
  112. package/dist/workflows/exec/run-workflow.js +20 -11
  113. package/dist/workflows/exec/step-work.js +76 -56
  114. package/dist/workflows/freeze/resolve-steps.js +19 -11
  115. package/dist/workflows/freeze/source-freeze.js +7 -0
  116. package/dist/workflows/freeze/targets/child-workflow.js +12 -18
  117. package/dist/workflows/freeze/targets/command.js +14 -2
  118. package/dist/workflows/ir/environment-v4.js +4 -2
  119. package/dist/workflows/ir/freeze-v4.js +2 -5
  120. package/dist/workflows/ir/plan-hash.js +0 -3
  121. package/dist/workflows/ir/schema-v4.js +14 -9
  122. package/dist/workflows/ir/schema.js +1 -3
  123. package/dist/workflows/parser.js +1 -1
  124. package/dist/workflows/resource-limits.js +35 -48
  125. package/dist/workflows/runtime/plan-classifier.js +89 -41
  126. package/dist/workflows/runtime/run-outputs.js +1 -21
  127. package/dist/workflows/runtime/runs.js +104 -154
  128. package/dist/workflows/source-files.js +28 -54
  129. package/dist/workflows/source-ir/program.js +2 -2
  130. package/dist/workflows/source-ir/semantics.js +5 -23
  131. package/docs/migration/v0.9.1-to-v0.9.2.md +20 -0
  132. package/docs/reference/cli.md +92 -17
  133. package/package.json +1 -1
  134. package/schemas/akm-config.json +5 -10
@@ -7,6 +7,7 @@
7
7
  * change.
8
8
  */
9
9
  import { z } from "zod";
10
+ import { warnOnce } from "../../warn.js";
10
11
  import { engineName, LlmInvocationOverridesSchema, nonEmptyString, positiveInt } from "./primitives.js";
11
12
  // ── Index / per-pass ────────────────────────────────────────────────────────
12
13
  //
@@ -53,26 +54,23 @@ export const IndexPassConfigSchema = z.preprocess((raw, ctx) => {
53
54
  return raw; // let z.object below produce the type error
54
55
  }
55
56
  const obj = raw;
57
+ let cleaned;
56
58
  for (const key of Object.keys(obj)) {
59
+ const dotted = [...(ctx.path ?? []), key].join(".");
57
60
  if (INDEX_PASS_RETIRED_KEYS.has(key)) {
58
- ctx.addIssue({
59
- code: z.ZodIssueCode.custom,
60
- message: `Retired or misplaced engine setting: \`${[...(ctx.path ?? []), key].join(".")}\` is not allowed. ` +
61
- "Select a named engine and use typed invocation fields instead.",
62
- });
63
- return raw;
61
+ warnOnce(`index-pass:retired:${dotted}`, `\`${dotted}\` is a retired engine setting and is ignored; select a named engine and use typed invocation fields instead.`);
62
+ cleaned ??= { ...obj };
63
+ delete cleaned[key];
64
64
  }
65
- if (!INDEX_PASS_KNOWN_KEYS.has(key)) {
66
- ctx.addIssue({
67
- code: z.ZodIssueCode.custom,
68
- message: `Unknown key \`${[...(ctx.path ?? []), key].join(".")}\`. Per-pass entries support ` +
69
- "`engine`, `model`, `timeoutMs`, `enabled`, `llm`, `graphExtractionBatchSize`, " +
70
- "`graphExtractionIncludeTypes`, and `lazyGraphExtraction`.",
71
- });
72
- return raw;
65
+ else if (!INDEX_PASS_KNOWN_KEYS.has(key)) {
66
+ warnOnce(`index-pass:unknown:${dotted}`, `Unknown key \`${dotted}\` ignored. Per-pass entries support ` +
67
+ "`engine`, `model`, `timeoutMs`, `enabled`, `llm`, `graphExtractionBatchSize`, " +
68
+ "`graphExtractionIncludeTypes`, and `lazyGraphExtraction`.");
69
+ cleaned ??= { ...obj };
70
+ delete cleaned[key];
73
71
  }
74
72
  }
75
- return raw;
73
+ return cleaned ?? raw;
76
74
  }, z
77
75
  .object({
78
76
  engine: engineName.optional(),
@@ -120,13 +118,13 @@ const IndexConfigRuntimeSchema = z.preprocess((raw, ctx) => {
120
118
  }
121
119
  if (typeof raw !== "object")
122
120
  return raw;
121
+ let cleaned;
123
122
  for (const [passName, value] of Object.entries(raw)) {
124
123
  if (passName === "stalenessDetection") {
125
- ctx.addIssue({
126
- code: z.ZodIssueCode.custom,
127
- message: "Invalid `index.stalenessDetection`: the removed pass is not supported.",
128
- });
129
- return raw;
124
+ warnOnce("index:stalenessDetection", "`index.stalenessDetection` is a retired pass and is ignored.");
125
+ cleaned ??= { ...raw };
126
+ delete cleaned.stalenessDetection;
127
+ continue;
130
128
  }
131
129
  if (typeof value !== "object" || value === null || Array.isArray(value)) {
132
130
  ctx.addIssue({
@@ -136,7 +134,7 @@ const IndexConfigRuntimeSchema = z.preprocess((raw, ctx) => {
136
134
  return raw;
137
135
  }
138
136
  }
139
- return raw;
137
+ return cleaned ?? raw;
140
138
  }, z
141
139
  .object({
142
140
  defaults: IndexDefaultsSchema.optional(),
@@ -8,6 +8,7 @@
8
8
  */
9
9
  import { z } from "zod";
10
10
  import { validateExtraParams } from "../../extra-params.js";
11
+ import { warnOnce } from "../../warn.js";
11
12
  import { ENGINE_NAME_PATTERN_SOURCE } from "../engine-semantics.js";
12
13
  /** Persisted config schema version. Package prerelease/patch versions do not change this value. */
13
14
  export const CURRENT_CONFIG_VERSION = "0.9.0";
@@ -27,25 +28,41 @@ export const httpUrl = z.string().refine((v) => v.startsWith("http://") || v.sta
27
28
  });
28
29
  const ENGINE_NAME_PATTERN = new RegExp(ENGINE_NAME_PATTERN_SOURCE);
29
30
  export const ENV_REFERENCE_PATTERN = /^\$[A-Za-z_][A-Za-z0-9_]*$|^\$\{[A-Za-z_][A-Za-z0-9_]*\}$/;
31
+ /** `secret://<name>` — an apiKey reference into the akm secret store, resolved via `resolveSecretFromStore`. */
32
+ export const SECRET_STORE_REFERENCE_PATTERN = /^secret:\/\/(.+)$/;
30
33
  export const engineName = z
31
34
  .string()
32
35
  .max(63)
33
36
  .regex(ENGINE_NAME_PATTERN, "names must be lowercase kebab-case and must not begin with reserved akm-");
37
+ /** The two symbolic apiKey forms akm accepts: an env-var reference or a secret-store reference. Never matches a literal key. */
38
+ export function isApiKeyReference(value) {
39
+ return ENV_REFERENCE_PATTERN.test(value) || SECRET_STORE_REFERENCE_PATTERN.test(value);
40
+ }
41
+ export function symbolicOrWarnApiKey(label) {
42
+ return z.string().superRefine((value) => {
43
+ if (isApiKeyReference(value))
44
+ return;
45
+ warnOnce(`config:literal-api-key:${label}`, `A ${label} in config.json is a literal API key, not a $VAR/\${VAR}/secret:// reference; using it as configured. Prefer \`akm config set ...apiKey '$VAR'\` (with the corresponding env var set) or \`secret://<name>\` (with \`akm secret set <name> ...\`) — see docs/reference/data-and-telemetry.md.`);
46
+ });
47
+ }
34
48
  export const chatCompletionsEndpoint = z.string().superRefine((value, ctx) => {
49
+ let url;
35
50
  try {
36
- const url = new URL(value);
37
- if (url.protocol !== "http:" && url.protocol !== "https:") {
38
- ctx.addIssue({ code: z.ZodIssueCode.custom, message: "endpoint must use http:// or https://" });
39
- }
40
- if (url.username || url.password || url.search || url.hash || !url.pathname.endsWith("/chat/completions")) {
41
- ctx.addIssue({
42
- code: z.ZodIssueCode.custom,
43
- message: "endpoint must be a credential-free OpenAI chat-completions URL without query or fragment",
44
- });
45
- }
51
+ url = new URL(value);
46
52
  }
47
53
  catch {
48
54
  ctx.addIssue({ code: z.ZodIssueCode.custom, message: "endpoint must be a complete URL" });
55
+ return;
56
+ }
57
+ if (url.protocol !== "http:" && url.protocol !== "https:") {
58
+ ctx.addIssue({ code: z.ZodIssueCode.custom, message: "endpoint must use http:// or https://" });
59
+ return;
60
+ }
61
+ if (url.username || url.password) {
62
+ warnOnce(`chatCompletionsEndpoint:userinfo:${value}`, `Config endpoint "${value}" embeds a username/password; consider moving the credential to the engine's apiKey field instead.`);
63
+ }
64
+ if (!url.pathname.endsWith("/chat/completions")) {
65
+ warnOnce(`chatCompletionsEndpoint:path:${value}`, `Config endpoint "${value}" does not end in /chat/completions; using it as configured.`);
49
66
  }
50
67
  });
51
68
  export const ExtraParamsSchema = z.record(z.unknown()).superRefine((value, ctx) => {
@@ -13,7 +13,6 @@ import { z } from "zod";
13
13
  // and, transitively, the indexer modules they delegate to).
14
14
  import { VALID_ADAPTER_IDS } from "../../adapter/adapter-ids.js";
15
15
  import { isBundleSlug } from "../../asset/asset-ref.js";
16
- import { hasRegistryUrlCredentials, REGISTRY_CREDENTIALS_UNSUPPORTED } from "../../registry-url.js";
17
16
  import { httpUrl, nonEmptyString, positiveInt } from "./primitives.js";
18
17
  const VALID_ADAPTER_IDS_SET = new Set(VALID_ADAPTER_IDS);
19
18
  // ── Sources / registries / installed ────────────────────────────────────────
@@ -56,11 +55,7 @@ export const SourceConfigEntrySchema = z
56
55
  });
57
56
  export const RegistryConfigEntrySchema = z
58
57
  .object({
59
- url: httpUrl.superRefine((value, ctx) => {
60
- if (hasRegistryUrlCredentials(value)) {
61
- ctx.addIssue({ code: z.ZodIssueCode.custom, message: REGISTRY_CREDENTIALS_UNSUPPORTED });
62
- }
63
- }),
58
+ url: httpUrl,
64
59
  name: z.string().min(1).optional(),
65
60
  enabled: z.boolean().optional(),
66
61
  provider: z.string().min(1).optional(),
@@ -14,10 +14,10 @@ const CONFIG_HINTS = {
14
14
  EMBEDDING_NOT_CONFIGURED: 'Run `akm config set embedding \'{"endpoint":"...","model":"..."}\'` to enable embeddings.',
15
15
  LLM_NOT_CONFIGURED: 'Run `akm setup` or configure an `engines` entry with `kind: "llm"`, then select it with `defaults.llmEngine`.',
16
16
  TEST_ISOLATION_MISSING: "Under bun test, when AKM_BUNDLE_DIR is set you MUST also set XDG_DATA_HOME (or AKM_DATA_DIR) and XDG_STATE_HOME (or AKM_STATE_DIR) to temp directories so the test does not touch the developer's real ~/.local/share/akm or ~/.local/state/akm.",
17
- SETUP_TMP_STASH_REFUSED: "Use a persistent directory, or set AKM_FORCE_SETUP_TMP_STASH=1 to opt in to a sandboxed setup (setup also pre-sets AKM_BUNDLE_DIR so config and cache writes auto-isolate into $stashDir/.akm/ — host config is preserved).",
18
17
  UNSAFE_STASH_DIR: "Choose a path inside your home directory (e.g. ~/akm) or another empty workspace. The bundle directory cannot be the filesystem root, your home directory itself, or a sensitive system path like /etc, /var, ~/.config, or ~/.ssh.",
19
18
  UNKNOWN_IMPROVE_STRATEGY: "Pass one of the listed strategy names to `--strategy`, or define it under `improve.strategies`. Names are case-sensitive.",
20
19
  EXECUTION_NOT_AUTHORIZED: "Change the selected tools or update the machine/user execution policy, then retry.",
20
+ SECRET_REFERENCE_UNRESOLVED: "Check the secret exists (`akm secret list`) and the name after `secret://` matches, or run `akm secret set <name> <value>` to store it.",
21
21
  };
22
22
  // Code-review finding: COMPOSITION_INVALID covers several unrelated causes
23
23
  // (a rejected with:, a multi-job source, a composition cycle/depth/size
@@ -39,7 +39,7 @@ const USAGE_HINTS = {
39
39
  INVALID_SOURCE_VALUE: "Pick one of: local, registry, all, or a configured source name.",
40
40
  INVALID_FORMAT_VALUE: "Pick one of: json, jsonl, yaml, text, md, html.",
41
41
  INVALID_DETAIL_VALUE: "Pick one of: brief, normal, full. For agent/summary projections use --shape.",
42
- INVALID_SHAPE_VALUE: "Pick one of: human, agent, summary (summary is only valid on `akm show`).",
42
+ INVALID_SHAPE_VALUE: "Pick one of: human, agent, summary (summary falls back to agent, with a warning, on commands with no summary projection).",
43
43
  INVALID_JSON_CONFIG_VALUE: 'Quote JSON values in your shell, for example: akm config set embedding \'{"endpoint":"http://localhost:11434/v1/embeddings","model":"nomic-embed-text"}\'.',
44
44
  MISSING_OR_AMBIGUOUS_TARGET: "Use `akm bundle update --all` or pass a target like `akm bundle update npm:@scope/pkg` (not both).",
45
45
  TARGET_NOT_UPDATABLE: "Run `akm bundle list` to view your sources, then retry with one of those values.",
@@ -74,9 +74,10 @@ const USAGE_HINTS = {
74
74
  TASK_TARGET_UNSUPPORTED: "Task definitions support command, script, workflow, and shell (run:) targets; akm/command is layered by callers.",
75
75
  // P3a (docs/plans/specs/p3a-plan-v5-child-freeze.md §3.2, A-N2): the
76
76
  // complete-or-abandon policy for a stored pre-irVersion-5 run.
77
- WORKFLOW_IR_VERSION_UNSUPPORTED: "Abandon the run with `akm workflow abandon <id>`, then start it again from the workflow source — pre-0.9.2 frozen plans are not re-executable.",
77
+ WORKFLOW_IR_VERSION_UNSUPPORTED: "Abandon the run with `akm workflow abandon <id>`, then start it again from the workflow source — a frozen plan this akm cannot execute is not re-executable in place.",
78
78
  // P3b (docs/plans/specs/p3b-child-executor.md §4.3).
79
79
  WORKFLOW_OUTPUT_INVALID: "Check each `outputs:` entry's `from:` against the step artifact it names, and its `schema:` against the value that step actually promotes.",
80
+ RUN_LEASE_HELD: "Wait for the named engine invocation to finish or for the lease to expire, then retry. `akm workflow status <id>` shows the current lease.",
80
81
  };
81
82
  /** Default hint for each NotFoundError code. */
82
83
  const NOT_FOUND_HINTS = {
@@ -49,6 +49,23 @@ export function classifyImproveAction(mode) {
49
49
  return assertNever(mode);
50
50
  }
51
51
  }
52
+ /**
53
+ * #912 — skip reasons that mean infrastructure failed to run the extraction,
54
+ * not that a session was legitimately uninteresting. `already_extracted`,
55
+ * `too_short`, and `triaged_out` are the ledger and pre-filter doing their
56
+ * job and are deliberately excluded; `malformed_model_output` already gets
57
+ * its own per-session forwarding into the envelope's `warnings[]` and is
58
+ * excluded here to avoid double-reporting the same failure two ways.
59
+ *
60
+ * Typed against `ExtractedSessionResult["skipReason"]` so a future reason
61
+ * cannot be added to one union without a compiler error surfacing here.
62
+ */
63
+ export const EXTRACT_INFRASTRUCTURE_SKIP_REASONS = [
64
+ "llm_unavailable",
65
+ "read_failed",
66
+ "exception",
67
+ "locked_concurrent",
68
+ ];
52
69
  /** Upper bound on retained sample refs PER reason in {@link DistillSkippedAggregate}. */
53
70
  export const DISTILL_SKIPPED_SAMPLE_CAP_PER_REASON = 3;
54
71
  /**
@@ -61,15 +61,9 @@ import { isRecord } from "./common.js";
61
61
  * same schema tree, so they share one bound.
62
62
  */
63
63
  const MAX_DEFINITION_DEPTH = 64;
64
- /** Total (schema node × value node) visits one {@link validateJsonSchemaSubset} call may make. */
65
- const MAX_VALIDATION_NODES = 100_000;
66
64
  export function validateJsonSchemaSubset(value, schema, options) {
67
65
  const errors = [];
68
- const budget = { nodes: MAX_VALIDATION_NODES };
69
- validateNode(value, schema, "$", { errors, budget, depth: 0, redactValues: options?.redactValues ?? false });
70
- if (budget.nodes < 0) {
71
- errors.push(`$: schema evaluation exceeded the limit of ${MAX_VALIDATION_NODES} checks and was stopped`);
72
- }
66
+ validateNode(value, schema, "$", { errors, depth: 0, redactValues: options?.redactValues ?? false });
73
67
  return errors;
74
68
  }
75
69
  /** Human-readable list of the keywords {@link validateJsonSchemaSubset} enforces (for error messages). */
@@ -372,10 +366,6 @@ function validateNode(value, schema, path, ctx) {
372
366
  errors.push(`${path}: schema nesting exceeds the depth limit of ${MAX_DEFINITION_DEPTH}`);
373
367
  return;
374
368
  }
375
- // Fail CLOSED: a truncated evaluation never returns "valid" — the counter
376
- // going negative is what the wrapper turns into a top-level error.
377
- if (--ctx.budget.nodes < 0)
378
- return;
379
369
  const actual = typeOf(value);
380
370
  const declared = schema.type;
381
371
  if (typeof declared === "string" || Array.isArray(declared)) {
@@ -10,6 +10,20 @@ import { ConfigError } from "./errors.js";
10
10
  import { createLockPayload, probeLock, reclaimStaleLock, releaseLock, tryAcquireLockSync } from "./file-lock.js";
11
11
  import { getMaintenanceBarrierPath } from "./paths.js";
12
12
  const heldBarrierContext = new AsyncLocalStorage();
13
+ /**
14
+ * The barrier is meant to be held only for the short critical section that
15
+ * registers one lock/lease/activity — a process that still holds it past
16
+ * this age is wedged (crashed mid-section, deadlocked, killed without
17
+ * cleanup), not doing legitimate long-running work. Without an age bound, a
18
+ * probe only reclaims a lock whose holder PID has verifiably died; a wedged
19
+ * — but still-alive — holder (or a PID a container/namespace boundary
20
+ * reused, making `isProcessAlive` see the wrong process as live) locked
21
+ * every other akm invocation out of ANY maintenance registration forever,
22
+ * with no recovery but killing the holder by hand. 5 minutes matches the
23
+ * stale-lock window already used for the improve extract-session lock
24
+ * (`commands/improve/extract.ts`).
25
+ */
26
+ const MAINTENANCE_BARRIER_STALE_AFTER_MS = 5 * 60 * 1000;
13
27
  /**
14
28
  * Serialize the short critical section that creates each long-lived AKM lock,
15
29
  * lease, or state activity. The operation keeps its own ownership record; this
@@ -23,7 +37,7 @@ export function tryAcquireMaintenanceBarrier() {
23
37
  if (ownership) {
24
38
  return () => releaseLock(ownership);
25
39
  }
26
- const probe = probeLock(lockPath);
40
+ const probe = probeLock(lockPath, { staleAfterMs: MAINTENANCE_BARRIER_STALE_AFTER_MS });
27
41
  if (probe.state !== "stale" || !reclaimStaleLock(lockPath, probe))
28
42
  return undefined;
29
43
  }
@@ -33,7 +47,8 @@ export function acquireMaintenanceBarrier() {
33
47
  const release = tryAcquireMaintenanceBarrier();
34
48
  if (release)
35
49
  return release;
36
- throw new ConfigError(`AKM maintenance is in progress (barrier ${getMaintenanceBarrierPath()}); retry after it completes.`, "INVALID_CONFIG_FILE");
50
+ throw new ConfigError(`AKM maintenance is in progress (barrier ${getMaintenanceBarrierPath()}); retry after it completes. ` +
51
+ `A sentinel older than ${MAINTENANCE_BARRIER_STALE_AFTER_MS / 60_000} minute(s) is reclaimed automatically on the next attempt.`, "INVALID_CONFIG_FILE");
37
52
  }
38
53
  export function withMaintenanceStartBarrier(run) {
39
54
  if (heldBarrierContext.getStore()?.active)
@@ -13,6 +13,7 @@ import path from "node:path";
13
13
  import { shortHash } from "./bundle-id.js";
14
14
  import { ConfigError } from "./errors.js";
15
15
  import { IS_WINDOWS } from "./platform.js";
16
+ import { warnOnce } from "./warn.js";
16
17
  /**
17
18
  * Returns true when the current process appears to be running under
18
19
  * `bun test` (either via the BUN_TEST sentinel Bun sets on the test
@@ -424,7 +425,6 @@ export function assertSafeStashDir(stashDir, env = process.env) {
424
425
  const SYSTEM_ROOTS = new Set([
425
426
  "/etc",
426
427
  "/var",
427
- "/var/tmp",
428
428
  "/usr",
429
429
  "/usr/local",
430
430
  "/opt",
@@ -448,6 +448,9 @@ export function assertSafeStashDir(stashDir, env = process.env) {
448
448
  if (SYSTEM_ROOTS.has(resolved)) {
449
449
  throw new ConfigError(`Refusing stashDir at system path (${resolved}). Pick a path inside your home directory.`, "UNSAFE_STASH_DIR");
450
450
  }
451
+ if (resolved === "/var/tmp") {
452
+ warnOnce("stash-dir:var-tmp", `Stash directory is at ${resolved}, a shared scratch directory system cleanup jobs may periodically empty; using it as configured.`);
453
+ }
451
454
  // User home — exact match only. Subdirs (~/akm, ~/work/stash) are fine.
452
455
  // Check BOTH the env-controlled home and the OS-reported home, so the
453
456
  // refusal can't be bypassed by unsetting HOME, and so it still fires
@@ -465,27 +468,21 @@ export function assertSafeStashDir(stashDir, env = process.env) {
465
468
  catch {
466
469
  // os.homedir() can throw on misconfigured systems; ignore.
467
470
  }
468
- const HIDDEN_USER_PARENTS = [
469
- ".config",
470
- ".local",
471
- ".cache",
472
- ".ssh",
473
- ".gnupg",
474
- ".aws",
475
- ".kube",
476
- ".docker",
477
- "Documents",
478
- "Downloads",
479
- "AppData",
480
- ];
471
+ const CREDENTIAL_USER_PARENTS = [".config", ".local", ".cache", ".ssh", ".gnupg", ".aws", ".kube", ".docker"];
472
+ const PLAIN_USER_DATA_PARENTS = ["Documents", "Downloads", "AppData"];
481
473
  for (const home of candidateHomes) {
482
474
  if (resolved === home) {
483
475
  throw new ConfigError(`Refusing stashDir at your home directory (${resolved}). Pick a subdirectory like ~/akm.`, "UNSAFE_STASH_DIR");
484
476
  }
485
- for (const sub of HIDDEN_USER_PARENTS) {
477
+ for (const sub of CREDENTIAL_USER_PARENTS) {
486
478
  if (resolved === path.join(home, sub)) {
487
479
  throw new ConfigError(`Refusing stashDir at sensitive user directory (${resolved}). Pick a subdirectory or a dedicated workspace.`, "UNSAFE_STASH_DIR");
488
480
  }
489
481
  }
482
+ for (const sub of PLAIN_USER_DATA_PARENTS) {
483
+ if (resolved === path.join(home, sub)) {
484
+ warnOnce(`stash-dir:plain-user-data:${sub}`, `Stash directory is at ${resolved}, your ${sub} folder; using it as configured, though it is usually a large, unrelated-content directory to index.`);
485
+ }
486
+ }
490
487
  }
491
488
  }
@@ -37,6 +37,7 @@ export const STATE_MIGRATION_SAFETY_BY_ID = Object.freeze({
37
37
  "024-workflow-run-outputs": "additive",
38
38
  "025-task-history-vocabulary-backfill": "data-preserving-rebuild",
39
39
  "026-proposals-strip-legacy-fragment-refs": "data-preserving-rebuild",
40
+ "027-extract-sessions-seen-harness-rename": "data-preserving-rebuild",
40
41
  });
41
42
  export const STATE_MIGRATIONS = [
42
43
  // ── Migration 001 — initial schema ──────────────────────────────────────────
@@ -1149,6 +1150,33 @@ export const STATE_MIGRATIONS = [
1149
1150
  WHERE ref LIKE '%#%';
1150
1151
  `,
1151
1152
  },
1153
+ // ── Migration 027 — claude-code -> claude harness rename (#915) ──
1154
+ //
1155
+ // The 0.9.2 rename shipped without a data migration, so rows written under
1156
+ // the old key were invisible to every reader keyed on "claude".
1157
+ //
1158
+ // `extract_sessions_seen` has PRIMARY KEY (harness, session_id), so a
1159
+ // session already recorded under "claude" (written by a run that really
1160
+ // happened post-rename) collides with its "claude-code" counterpart on
1161
+ // rename. `UPDATE OR IGNORE` keeps the newer "claude" row exactly as it
1162
+ // was — the older row's outcome is superseded, not more correct — and the
1163
+ // trailing DELETE drops that now-unreachable duplicate so the "claude-code"
1164
+ // key space is fully empty afterwards, not just mostly-migrated.
1165
+ //
1166
+ // `workflow_runs.agent_harness` (migration 020) has no uniqueness
1167
+ // constraint on that column, so a plain UPDATE is sufficient there. Both
1168
+ // statements are kept in one migration so the rename is a single ledger
1169
+ // event. `improve_runs.result_json` is deliberately untouched: the harness
1170
+ // name embedded in those JSON blobs is reporting data about a past run, not
1171
+ // a lookup key, so rewriting it would not fix anything a reader depends on.
1172
+ {
1173
+ id: "027-extract-sessions-seen-harness-rename",
1174
+ up: `
1175
+ UPDATE OR IGNORE extract_sessions_seen SET harness = 'claude' WHERE harness = 'claude-code';
1176
+ DELETE FROM extract_sessions_seen WHERE harness = 'claude-code';
1177
+ UPDATE workflow_runs SET agent_harness = 'claude' WHERE agent_harness = 'claude-code';
1178
+ `,
1179
+ },
1152
1180
  ];
1153
1181
  assertMigrationRegistry(STATE_MIGRATIONS);
1154
1182
  function assertStateMigrationSafetyRegistry() {
@@ -69,9 +69,11 @@ import { sleepSync } from "../runtime.js";
69
69
  import { openDatabase } from "../storage/database.js";
70
70
  import { assertMigrationLedger } from "../storage/engines/sqlite-migrations.js";
71
71
  import { openManagedDatabase, withManagedDb } from "../storage/managed-db.js";
72
+ import { pkgVersion } from "../version.js";
72
73
  import { acquireMaintenanceActivitySync } from "./maintenance-barrier.js";
73
74
  import { getDataDir } from "./paths.js";
74
75
  import { runMigrations, STATE_MIGRATIONS } from "./state/migrations.js";
76
+ import { warnOnce } from "./warn.js";
75
77
  // ── Path helper ──────────────────────────────────────────────────────────────
76
78
  /**
77
79
  * Default path: `<dataDir>/state.db`.
@@ -370,6 +372,25 @@ function createHistoricalStateSafetyCopy(source, migrationId) {
370
372
  * matches the value used in openDatabase() for index.db; 5 s proved too
371
373
  * narrow when a post-inference reindex overlapped a parallel event write.
372
374
  */
375
+ /**
376
+ * Tell the operator once when state.db was migrated by a newer akm than the
377
+ * one running. The open proceeds: every migration this binary knows is already
378
+ * applied, so it reads and writes the tables it knows. Commands that depend on
379
+ * something a later migration changed may still report less than the truth,
380
+ * which is why this is said out loud rather than swallowed.
381
+ */
382
+ function warnNewerStateLedger(ledger) {
383
+ if (ledger.status !== "newer")
384
+ return;
385
+ warnOnce("state-db-newer-ledger", `[state.db] This akm (v${pkgVersion}) is older than the state database: ${ledger.detail}. ` +
386
+ "Continuing with the schema this version knows; upgrade akm if its output looks incomplete.");
387
+ }
388
+ function unversionedDatabaseHasNoTables(db) {
389
+ const tables = db
390
+ .prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' AND name != ?")
391
+ .get("schema_migrations");
392
+ return !tables;
393
+ }
373
394
  export function openStateDatabase(dbPath, options) {
374
395
  const canonicalPath = getStateDbPath();
375
396
  const resolvedPath = dbPath ?? canonicalPath;
@@ -392,6 +413,7 @@ export function openStateDatabase(dbPath, options) {
392
413
  let existingSource;
393
414
  let openedDb;
394
415
  let existingUnversionedDatabase = false;
416
+ let treatUnversionedAsFresh = false;
395
417
  let stateSafetyCopyCreated = false;
396
418
  try {
397
419
  fs.mkdirSync(path.dirname(resolvedPath), { recursive: true });
@@ -404,7 +426,12 @@ export function openStateDatabase(dbPath, options) {
404
426
  try {
405
427
  preflight.exec("PRAGMA busy_timeout = 30000");
406
428
  const ledger = assertMigrationLedger(preflight, STATE_MIGRATIONS);
429
+ warnNewerStateLedger(ledger);
407
430
  existingUnversionedDatabase = ledger.migrationIds.length === 0;
431
+ if (existingUnversionedDatabase && unversionedDatabaseHasNoTables(preflight)) {
432
+ existingUnversionedDatabase = false;
433
+ treatUnversionedAsFresh = true;
434
+ }
408
435
  if (existingUnversionedDatabase && !options?.allowHistoricalDestructiveStateUpgrade) {
409
436
  throw new Error("Refusing to migrate an existing unversioned state.db during an ordinary managed open. " +
410
437
  "Run `akm upgrade` (or `akm migrate apply`) to create a verified snapshot before migration 001 " +
@@ -421,7 +448,7 @@ export function openStateDatabase(dbPath, options) {
421
448
  pragmas: { dataDir: path.dirname(resolvedPath) },
422
449
  init: (db) => {
423
450
  runMigrations(db, {
424
- freshDatabase: !!freshReservation,
451
+ freshDatabase: !!freshReservation || treatUnversionedAsFresh,
425
452
  existingUnversionedDatabase,
426
453
  allowHistoricalDestructiveStateUpgrade: options?.allowHistoricalDestructiveStateUpgrade,
427
454
  beforeExistingUnversionedStateMigration: options?.allowHistoricalDestructiveStateUpgrade
@@ -37,7 +37,7 @@ import { existingFileMode, isWithin, resolveStashDir, writeFileAtomic } from "./
37
37
  import { bundleContentRoot, resolveConfiguredSources } from "./config/config.js";
38
38
  import { ConfigError, UsageError } from "./errors.js";
39
39
  import { sanitizeCommitMessage } from "./git-message.js";
40
- import { warn } from "./warn.js";
40
+ import { warn, warnOnce } from "./warn.js";
41
41
  import { recordWrittenPath } from "./write-provenance.js";
42
42
  /**
43
43
  * Source kinds that the loader is allowed to mark `writable: true`. Anything
@@ -872,10 +872,10 @@ export function prepareWriteTargetForMutation(target, options = {}) {
872
872
  }
873
873
  const upstream = inspectGitUpstream(repoPath);
874
874
  if (upstream.behind > 0) {
875
- throw new UsageError(`Writable Git target "${target.source.name}" is behind ${upstream.upstream}; run \`akm bundle update ${target.source.name}\` before writing.`, "INVALID_FLAG_VALUE");
875
+ warnOnce(`write-source:git-behind:${realRepoPath}`, `Writable Git target "${target.source.name}" is ${upstream.behind} commit(s) behind ${upstream.upstream}; writing anyway. Run \`akm bundle update ${target.source.name}\` to catch up.`);
876
876
  }
877
- if (upstream.ahead > 0 && options.allowAhead !== true) {
878
- throw new UsageError(`Writable Git target "${target.source.name}" has unpushed commits; push or reconcile them before AKM writes another commit.`, "INVALID_FLAG_VALUE");
877
+ if (upstream.ahead > 0) {
878
+ warnOnce(`write-source:git-ahead:${realRepoPath}`, `Writable Git target "${target.source.name}" has ${upstream.ahead} unpushed commit(s); writing another on top. Push or reconcile them when convenient.`);
879
879
  }
880
880
  return {
881
881
  ...target,
@@ -997,7 +997,7 @@ const WINDOWS_RESERVED_DEVICE_NAMES = new Set([
997
997
  function resolveAssetFilePath(source, ref) {
998
998
  const basename = path.posix.basename(ref.name.replaceAll("\\", "/")).replace(/\.md$/i, "").toLowerCase();
999
999
  if (basename === "index" || basename === "log") {
1000
- throw new UsageError(`Reserved concept name "${basename}" cannot be written.`, "INVALID_FLAG_VALUE");
1000
+ warnOnce(`write-source:reserved-basename:${basename}`, `Concept name "${basename}" collides with a reserved word some tooling treats specially; writing it anyway.`);
1001
1001
  }
1002
1002
  // Windows resolves these names as DEVICES no matter the directory or the
1003
1003
  // extension, so `CON.md` is not a file — a write goes to the console and a
@@ -1005,7 +1005,7 @@ function resolveAssetFilePath(source, ref) {
1005
1005
  // portable: an asset authored on Linux must not become unopenable when the
1006
1006
  // same bundle is used on Windows.
1007
1007
  if (WINDOWS_RESERVED_DEVICE_NAMES.has(basename)) {
1008
- throw new UsageError(`Asset name "${basename}" is a reserved Windows device name and cannot be written.`, "INVALID_FLAG_VALUE");
1008
+ warnOnce(`write-source:windows-device-name:${basename}`, `Asset name "${basename}" is a reserved Windows device name; writing it anyway, but this bundle will not be portable to Windows.`);
1009
1009
  }
1010
1010
  const typeDir = stashDirFor(ref.type);
1011
1011
  if (!typeDir) {
@@ -33,6 +33,7 @@ import { rethrowIfTestIsolationError } from "../core/errors.js";
33
33
  import { getDbPath } from "../core/paths.js";
34
34
  import { warn } from "../core/warn.js";
35
35
  import { closeDatabase, openReadonlyExistingDatabase } from "../storage/repositories/index-connection.js";
36
+ import { isCanonicalIndexGeneration } from "../storage/repositories/index-entry-schema.js";
36
37
  let guardSettled = false;
37
38
  /** TEST-ONLY: re-arm the once-per-process guard between cases. */
38
39
  export function resetBundleIdentityGuardForTests() {
@@ -47,6 +48,8 @@ function indexBundlePrefixes(dbPath) {
47
48
  db = openReadonlyExistingDatabase(dbPath);
48
49
  if (!db)
49
50
  return undefined;
51
+ if (!isCanonicalIndexGeneration(db))
52
+ return undefined;
50
53
  return db
51
54
  .prepare("SELECT DISTINCT bundle_id AS b FROM entries WHERE bundle_id IS NOT NULL AND bundle_id != ''")
52
55
  .all().map((row) => row.b);
@@ -26,6 +26,7 @@ import { classifyPathAccess } from "../core/path-access.js";
26
26
  import { getDbPath } from "../core/paths.js";
27
27
  import { assertIndexPathReadable, closeDatabase, openExistingDatabase } from "../storage/repositories/index-connection.js";
28
28
  import { getEntryCount, getIndexedFilePaths } from "../storage/repositories/index-entries-repository.js";
29
+ import { isCanonicalIndexGeneration } from "../storage/repositories/index-entry-schema.js";
29
30
  import { getMeta } from "../storage/repositories/index-meta-repository.js";
30
31
  import { warnOnBundleRenameDrift } from "./bundle-identity-guard.js";
31
32
  function getIndexableFiles(root, spec) {
@@ -118,6 +119,8 @@ export function isIndexStale(stashDir) {
118
119
  let db;
119
120
  try {
120
121
  db = openExistingDatabase(dbPath);
122
+ if (!isCanonicalIndexGeneration(db))
123
+ return true;
121
124
  const entryCount = getEntryCount(db);
122
125
  if (entryCount === 0)
123
126
  return true;
@@ -164,6 +167,8 @@ function indexCanServeStash(stashDir) {
164
167
  let db;
165
168
  try {
166
169
  db = openExistingDatabase(dbPath);
170
+ if (!isCanonicalIndexGeneration(db))
171
+ return false;
167
172
  if (getEntryCount(db) === 0)
168
173
  return false;
169
174
  const storedStashDir = getMeta(db, "stashDir");
@@ -1770,9 +1770,17 @@ async function lookupBundleRefWithResolutionUsing(ref, openLookupDatabase) {
1770
1770
  const entry = readLookupEntry(db, id, ref.conceptId, source.path);
1771
1771
  if (entry) {
1772
1772
  if (owner.workflowSource) {
1773
- assertIndexedWorkflowSourceIdentity(inputRef, entry.filePath, owner.workflowSource);
1774
- if (entry.adapterId !== adapterId) {
1775
- throw new WorkflowSourceIdentityError(inputRef, entry.filePath, owner.path);
1773
+ try {
1774
+ assertIndexedWorkflowSourceIdentity(inputRef, entry.filePath, owner.workflowSource);
1775
+ if (entry.adapterId !== adapterId) {
1776
+ throw new WorkflowSourceIdentityError(inputRef, entry.filePath, owner.path);
1777
+ }
1778
+ }
1779
+ catch (error) {
1780
+ if (!(error instanceof WorkflowSourceIdentityError))
1781
+ throw error;
1782
+ warn(`${error.message} Falling back to the physical owner.`);
1783
+ return { entry: null, owner, ...(indexError === undefined ? {} : { indexError }) };
1776
1784
  }
1777
1785
  }
1778
1786
  else if (entry.adapterId !== adapterId || !indexedPathMatchesOwner(entry.filePath, owner)) {
@@ -7,6 +7,7 @@ import { adapterForId } from "../../core/adapter/registry.js";
7
7
  import { compareCodePoints, hasErrnoCode, isWithin } from "../../core/common.js";
8
8
  import { ConfigError, UsageError } from "../../core/errors.js";
9
9
  import { canonicalizeWorkflowName } from "../../core/recognition-util.js";
10
+ import { warnOnce } from "../../core/warn.js";
10
11
  import { resolveUniqueWorkflowSource, workflowNameForConceptId, } from "../../workflows/source-files.js";
11
12
  import { buildFileContext } from "../walk/file-context.js";
12
13
  const CONTENT_READ_REQUIRED = Symbol("adapter ownership probe requires content");
@@ -123,7 +124,7 @@ function claimsWithoutContent(adapter, component, owner) {
123
124
  * adapter supplies its own read placements and is probed with a byte-denying
124
125
  * FileContext so path-level abstention remains authoritative.
125
126
  */
126
- export function resolveAdapterConceptOwner(sourcePath, adapterId, conceptId) {
127
+ export function resolveAdapterConceptOwner(sourcePath, adapterId, conceptId, options) {
127
128
  const adapter = adapterForId(adapterId);
128
129
  const normalized = normalizedConceptId(conceptId);
129
130
  if (!adapter || !normalized)
@@ -185,9 +186,19 @@ export function resolveAdapterConceptOwner(sourcePath, adapterId, conceptId) {
185
186
  if (claimsWithoutContent(adapter, component, candidate))
186
187
  ownersByIdentity.set(identity, candidate);
187
188
  }
188
- const owners = [...ownersByIdentity.values()].filter((owner) => owner.conceptId === resolutionConceptId);
189
+ const owners = [...ownersByIdentity.values()]
190
+ .filter((owner) => owner.conceptId === resolutionConceptId)
191
+ .sort((left, right) => compareCodePoints(left.path, right.path));
189
192
  if (owners.length > 1) {
190
- throw new AdapterConceptCollisionError(adapterId, resolutionConceptId, owners.map((owner) => path.relative(sourcePath, owner.path).replaceAll("\\", "/")));
193
+ if (options?.mode !== "read") {
194
+ throw new AdapterConceptCollisionError(adapterId, resolutionConceptId, owners.map((owner) => path.relative(sourcePath, owner.path).replaceAll("\\", "/")));
195
+ }
196
+ const [winner, ...losers] = owners;
197
+ warnOnce(`adapter-concept-collision:${adapterId}:${resolutionConceptId}`, `Adapter "${adapterId}" has multiple physical owners for "${resolutionConceptId}": ` +
198
+ `${owners.map((owner) => path.relative(sourcePath, owner.path).replaceAll("\\", "/")).join(", ")}. ` +
199
+ `Reading "${path.relative(sourcePath, winner.path).replaceAll("\\", "/")}" and ignoring ` +
200
+ `${losers.map((owner) => path.relative(sourcePath, owner.path).replaceAll("\\", "/")).join(", ")}.`);
201
+ return winner;
191
202
  }
192
203
  return owners[0];
193
204
  }