cyc-stack 0.7.1 → 0.8.3

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.
@@ -4,6 +4,8 @@ import { fileURLToPath } from 'node:url';
4
4
  import { createHash, randomUUID } from 'node:crypto';
5
5
  import { spawnSync } from 'node:child_process';
6
6
  import { atomicJSON, configuration, readRun, runCommands, sourceSnapshot } from './runtime.mjs';
7
+ import { runBrowserAudit, validateBrowserPlan } from './browser-audit.mjs';
8
+ import { canonicalPattern, matches, contextLocation } from './context-review.mjs';
7
9
 
8
10
  const registryName = 'docs/context/recipes.json';
9
11
  const indexName = 'docs/context/INDEX.md';
@@ -14,8 +16,7 @@ const idPattern = /^[a-z][a-z0-9-]{0,63}$/;
14
16
  const recipeKinds = ['investigation', 'verification'];
15
17
 
16
18
  function canonical(path, patterns = false) {
17
- if (!text(path) || /[\\:\0]/.test(path) || (!patterns && /[*?\[\]{}]/.test(path)) || /[\[\]{}]/.test(path) || path.split('/').some(p => !p || p === '.' || p === '..')) throw Error(`Use canonical project-relative ${patterns ? 'patterns' : 'paths'}: ${path}`);
18
- return path;
19
+ return canonicalPattern(path, { patterns });
19
20
  }
20
21
 
21
22
  // Reject linked ancestors even inside the project: receipts must remain excluded
@@ -31,19 +32,6 @@ function local(root, path) {
31
32
  return current;
32
33
  }
33
34
 
34
- function matches(pattern, path) {
35
- canonical(pattern, true); canonical(path);
36
- let regex = '';
37
- for (let i = 0; i < pattern.length; i++) {
38
- if (pattern[i] === '*' && pattern[i + 1] === '*') { i++; if (pattern[i + 1] === '/') { i++; regex += '(?:.*/)?'; } else regex += '.*'; }
39
- else if (pattern[i] === '*') regex += '[^/]*';
40
- else if (pattern[i] === '?') regex += '[^/]';
41
- else regex += pattern[i].replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
42
- }
43
- const prefix = pattern.split(/[?*]/)[0].replace(/\/$/, '');
44
- return new RegExp(`^${regex}$`).test(path) || (prefix && (prefix === path || prefix.startsWith(path + '/'))) || (!/[?*]/.test(pattern) && path.startsWith(pattern + '/'));
45
- }
46
-
47
35
  function selection(root, { paths = [], goal = '', run } = {}) {
48
36
  if (!Array.isArray(paths)) throw Error('Paths must be an array.');
49
37
  if (run) {
@@ -72,6 +60,7 @@ function registry(root) {
72
60
  ids.add(recipe.id);
73
61
  if (recipe.command.includes('\0') || recipe.args.some(arg => arg.includes('\0'))) throw Error('Recipe command/args cannot contain null bytes.');
74
62
  recipe.covers.forEach(p => canonical(p, true));
63
+ if (recipe.browser !== undefined) { validateBrowserPlan(recipe.browser); recipe.browser.prerequisiteFiles.forEach(p => canonical(p)); }
75
64
  for (const name of recipe.references) if (!lstatSync(local(root, name)).isFile()) throw Error(`Recipe reference is not a regular file: ${name}`);
76
65
  if (recipe.timeoutMs !== undefined && (!Number.isSafeInteger(recipe.timeoutMs) || recipe.timeoutMs <= 0 || recipe.timeoutMs > 2147483647)) throw Error('Recipe timeoutMs must be between 1 and 2147483647.');
77
66
  }
@@ -89,6 +78,7 @@ function receiptStatus(root, recipe, loaded, environment) {
89
78
  const result = receipt.results?.[0];
90
79
  const current = environment();
91
80
  if (receipt.schemaVersion !== 1 || receipt.recipeHash !== hash(JSON.stringify(recipe)) || receipt.registryHash !== loaded.hash || receipt.sourceHash !== current.sourceHash || receipt.configHash !== current.configHash) return { status: 'stale', receiptPath };
81
+ if (recipe.browser) return { status: receipt.passed && receipt.browser?.status === 'passed' && receipt.sourceStable && receipt.definitionStable && receipt.configStable ? 'passed' : receipt.browser?.status ?? 'failed', receiptPath, checkedAt: receipt.checkedAt };
92
82
  const passed = receipt.passed === true && receipt.sourceStable === true && receipt.definitionStable === true && receipt.configStable === true && receipt.results.length === 1 && result.id === recipe.id && result.command === recipe.command && JSON.stringify(result.args) === JSON.stringify(recipe.args) && result.passed === true && result.exitCode === 0 && !result.error;
93
83
  return { status: passed ? 'passed' : 'failed', receiptPath, checkedAt: receipt.checkedAt };
94
84
  } catch { return { status: 'invalid', receiptPath }; }
@@ -129,6 +119,7 @@ export function contextLinks(cell) {
129
119
  }
130
120
 
131
121
  export function investigateProject(target, options = {}) {
122
+ const { index: indexName } = contextLocation(realpathSync(target));
132
123
  validateRecipeKind(options.kind);
133
124
  const root = realpathSync(target), selected = selection(root, options), limit = options.limit ?? 8;
134
125
  if (!Number.isSafeInteger(limit) || limit < 1 || limit > 50) throw Error('Limit must be between 1 and 50.');
@@ -172,10 +163,21 @@ export function investigateProject(target, options = {}) {
172
163
  note: 'Paths and records are leads, not an exhaustive dependency map. Read matched records and inspect current consumers. Matching bytes do not establish semantic correctness. Project commands were not executed.' };
173
164
  }
174
165
 
175
- export function runRecipe(target, id, { run } = {}) {
166
+ export function runRecipe(target, id, { run, driver, scanner, signal } = {}) {
176
167
  const root = realpathSync(target), loaded = registry(root), recipe = loaded.recipes.find(r => r.id === id);
177
168
  if (!recipe) throw Error(`Unknown recipe: ${id}`);
178
169
  if (run && readRun(root, run).status !== 'running') throw Error('Recipe execution associated with a run needs that run to be running.');
170
+ if (recipe.browser) {
171
+ const before = sourceSnapshot(root), configBefore = configHash(root);
172
+ return runBrowserAudit(root, recipe.browser, { driver, scanner, signal }).then(browser => {
173
+ const after = sourceSnapshot(root), configAfter = configHash(root), definitionStable = registry(root).hash === loaded.hash;
174
+ const receipt = { schemaVersion: 1, recipe: id, kind: recipe.kind, runId: run ?? null, checkedAt: new Date().toISOString(), registryHash: loaded.hash, recipeHash: hash(JSON.stringify(recipe)), sourceHash: after.hash, configHash: configAfter, sourceStable: before.hash === after.hash, definitionStable, configStable: configBefore === configAfter, browser };
175
+ receipt.passed = browser.ok && receipt.sourceStable && definitionStable && receipt.configStable;
176
+ const receiptPath = `.cyc/runtime/recipes/${id}/${Date.now()}-${randomUUID()}.json`;
177
+ atomicJSON(local(root, receiptPath), receipt);
178
+ return { ...browser, ok: receipt.passed, recipe: id, receiptPath, expected: recipe.expected, note: 'Named external browser/scanner observations; no command receipt or native activation is inferred.' };
179
+ });
180
+ }
179
181
  local(root, `.cyc/runtime/recipes/${id}`); // Validate receipt ancestors before execution.
180
182
  const before = sourceSnapshot(root), configBefore = configHash(root);
181
183
  if (before.skipped.length) throw Error('Recipe verification cannot cover linked source; inspect the links first.');
@@ -0,0 +1,4 @@
1
+ // Same isolated worker pattern as baseline-worker; owns the existing controller operations.
2
+ import { executeOperation } from './runtime.mjs';
3
+ const [root, id, operationId] = process.argv.slice(2);
4
+ if (root && id && operationId) executeOperation(root, id, operationId);