@ornexus/neocortex 4.61.6 → 4.64.0

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 (99) hide show
  1. package/dist/release-source.json +1 -1
  2. package/dist/sbom.cdx.json +1193 -1193
  3. package/install.ps1 +31 -34
  4. package/install.sh +28 -5
  5. package/package.json +6 -4
  6. package/packages/client/dist/agent/refresh-stubs.d.ts +6 -0
  7. package/packages/client/dist/agent/refresh-stubs.js +2 -2
  8. package/packages/client/dist/agent/update-description.d.ts +8 -1
  9. package/packages/client/dist/agent/update-description.js +2 -1
  10. package/packages/client/dist/commands/invoke.d.ts +24 -0
  11. package/packages/client/dist/commands/invoke.js +73 -73
  12. package/packages/client/dist/commands/refresh-memory.d.ts +10 -0
  13. package/packages/client/dist/commands/refresh-memory.js +5 -3
  14. package/packages/client/dist/errors/frontmatter-diagnostics.d.ts +10 -1
  15. package/packages/client/dist/errors/frontmatter-diagnostics.js +1 -1
  16. package/packages/client/dist/git/direct-yolo-checkpoint.d.ts +110 -10
  17. package/packages/client/dist/git/direct-yolo-checkpoint.js +3 -4
  18. package/packages/client/dist/git/git-lifecycle-store.d.ts +96 -2
  19. package/packages/client/dist/git/git-lifecycle-store.js +8 -7
  20. package/packages/client/dist/git/legacy-branch-mode-conversion.d.ts +58 -0
  21. package/packages/client/dist/git/legacy-branch-mode-conversion.js +4 -0
  22. package/packages/client/dist/git/release-gate-env.d.ts +23 -0
  23. package/packages/client/dist/git/release-gate-env.js +1 -0
  24. package/packages/client/dist/git/secure-filesystem-boundary.d.ts +24 -0
  25. package/packages/client/dist/git/secure-filesystem-boundary.js +20 -9
  26. package/packages/client/dist/git/state-json-field-merge.d.ts +76 -0
  27. package/packages/client/dist/git/state-json-field-merge.js +2 -0
  28. package/packages/client/dist/git/story-worktree-cleanup.d.ts +22 -1
  29. package/packages/client/dist/git/story-worktree-cleanup.js +4 -4
  30. package/packages/client/dist/git/story-worktree-integration.d.ts +48 -0
  31. package/packages/client/dist/git/story-worktree-integration.js +3 -3
  32. package/packages/client/dist/git/story-worktree-manager.d.ts +57 -0
  33. package/packages/client/dist/git/story-worktree-manager.js +3 -3
  34. package/packages/client/dist/git/story-worktree-paths.d.ts +23 -1
  35. package/packages/client/dist/git/story-worktree-paths.js +1 -1
  36. package/packages/client/dist/git/story-write-set-extension.d.ts +3 -0
  37. package/packages/client/dist/git/story-write-set-extension.js +1 -1
  38. package/packages/client/dist/private-execution/root-memory-writer.js +1 -1
  39. package/packages/client/dist/runner/headless/child-env.d.ts +92 -0
  40. package/packages/client/dist/runner/headless/child-env.js +1 -0
  41. package/packages/client/dist/runner/headless/continuous-story-workspace.js +11 -11
  42. package/packages/client/dist/runner/headless/epic-terminal-git-cli-adapter.d.ts +31 -0
  43. package/packages/client/dist/runner/headless/epic-terminal-git-cli-adapter.js +10 -10
  44. package/packages/client/dist/runner/headless/run-yolo.d.ts +15 -0
  45. package/packages/client/dist/runner/headless/run-yolo.js +1 -1
  46. package/packages/client/dist/runner/headless/run-yoloop.d.ts +55 -0
  47. package/packages/client/dist/runner/headless/run-yoloop.js +2 -2
  48. package/packages/client/dist/runner/headless/types.d.ts +9 -1
  49. package/packages/client/dist/runner/headless/types.js +2 -2
  50. package/packages/client/dist/state/project-state-snapshot.js +1 -1
  51. package/packages/client/dist/state/state-json-repair.d.ts +23 -0
  52. package/packages/client/dist/state/state-json-repair.js +4 -3
  53. package/packages/client/dist/yolo/change-evidence-collector.js +1 -1
  54. package/packages/client/dist/yolo/invoke-envelope-capability.d.ts +2 -0
  55. package/packages/client/dist/yolo/invoke-envelope-capability.js +1 -1
  56. package/packages/client/dist/yolo/project-manifest-projection-collector.d.ts +28 -0
  57. package/packages/client/dist/yolo/project-manifest-projection-collector.js +1 -0
  58. package/packages/client/dist/yolo/spec-envelope-collector.d.ts +9 -0
  59. package/packages/client/dist/yolo/spec-envelope-collector.js +3 -3
  60. package/packages/client/dist/yoloop/discovery-hook.d.ts +2 -0
  61. package/packages/client/dist/yoloop/discovery-hook.js +2 -2
  62. package/packages/client/dist/yoloop/invoke-hooks.d.ts +17 -0
  63. package/packages/client/dist/yoloop/invoke-hooks.js +14 -10
  64. package/packages/client/dist/yoloop/legacy-story-scope.d.ts +23 -0
  65. package/packages/client/dist/yoloop/legacy-story-scope.js +1 -0
  66. package/packages/client/dist/yoloop/legacy-yoloop-adoption.d.ts +69 -0
  67. package/packages/client/dist/yoloop/legacy-yoloop-adoption.js +1 -0
  68. package/packages/client/dist/yoloop/shared-discover-epics.d.ts +62 -1
  69. package/packages/client/dist/yoloop/shared-discover-epics.js +1 -1
  70. package/packages/client/dist/yoloop/yoloop-client-state-store.d.ts +23 -0
  71. package/packages/client/dist/yoloop/yoloop-client-state-store.js +1 -1
  72. package/packages/client/dist/yoloop/yoloop-start-origin-binding.d.ts +92 -0
  73. package/packages/client/dist/yoloop/yoloop-start-origin-binding.js +1 -0
  74. package/packages/shared/dist/compatibility/index.js +6 -0
  75. package/packages/shared/dist/frontmatter-context-contract.js +164 -52
  76. package/packages/shared/dist/project-manifest-projection.js +202 -0
  77. package/packages/shared/dist/project-validation-commands.js +14 -6
  78. package/packages/shared/dist/release-gate-env-contract.js +129 -0
  79. package/packages/shared/dist/yolo/spec-envelope-rules.js +85 -0
  80. package/packages/shared/dist/yolo/spec-envelope.js +33 -7
  81. package/packages/shared/dist/yoloop/branch-unit.js +119 -3
  82. package/packages/shared/dist/yoloop/epic-terminal-git.js +98 -6
  83. package/packages/shared/dist/yoloop/story-write-scope.js +212 -0
  84. package/targets-stubs/antigravity/gemini.md +1 -1
  85. package/targets-stubs/antigravity/install-antigravity.sh +13 -4
  86. package/targets-stubs/antigravity/skill/SKILL.md +1 -1
  87. package/targets-stubs/claude-code/neocortex-root.agent.yaml +1 -1
  88. package/targets-stubs/claude-code/neocortex-root.md +2 -2
  89. package/targets-stubs/claude-code/neocortex.agent.yaml +1 -1
  90. package/targets-stubs/claude-code/neocortex.md +5 -5
  91. package/targets-stubs/codex/AGENTS.md +1 -1
  92. package/targets-stubs/codex/install-codex.sh +12 -4
  93. package/targets-stubs/cursor/agent.md +2 -2
  94. package/targets-stubs/gemini-cli/agent.md +2 -2
  95. package/targets-stubs/lib/managed-config-merge.js +459 -5
  96. package/targets-stubs/lib/managed-profile.sh +54 -7
  97. package/targets-stubs/opencode/neocortex-root.md +1 -1
  98. package/targets-stubs/vscode/install-vscode.sh +21 -0
  99. package/targets-stubs/vscode/neocortex.agent.md +2 -2
@@ -1,4 +1,3 @@
1
- import { Buffer } from 'node:buffer';
2
1
  export const FRONTMATTER_CONTEXTS = [
3
2
  'wrapper',
4
3
  'delegated-prompt',
@@ -31,6 +30,42 @@ export const FRONTMATTER_LIMITS = Object.freeze({
31
30
  maxListItems: 32,
32
31
  maxValueChars: 512,
33
32
  });
33
+ /**
34
+ * P226.04 / ADR-P226-03: per-field list limits. Every list not named here keeps
35
+ * the global `FRONTMATTER_LIMITS.maxListItems` / `maxValueChars` bounds.
36
+ */
37
+ export const FRONTMATTER_FIELD_LIMITS = Object.freeze({
38
+ files_to_modify: Object.freeze({ maxItems: 256, maxItemChars: 240 }),
39
+ });
40
+ /**
41
+ * Additional envelope budget that only `files_to_modify` items may consume. The
42
+ * envelope is accepted when `lines <= maxLines + min(extraLines, fileLines)` and
43
+ * `bytes <= maxBytes + min(extraBytes, fileBytes)`; excess from any other field
44
+ * remains `frontmatter-oversized`.
45
+ */
46
+ export const FRONTMATTER_FIELD_BUDGET = Object.freeze({
47
+ extraLines: 256,
48
+ extraBytes: 64 * 1024,
49
+ });
50
+ const BUDGET_FIELD = 'files_to_modify';
51
+ const UNLISTED_FIELD_LABEL = 'unlisted-field';
52
+ export const FRONTMATTER_LIST_TOO_LONG_PATTERN = /^frontmatter-list-too-long:(files_to_modify|depends_on|related_epics|unlisted-field):[0-9]{1,6}>[0-9]{1,6}$/;
53
+ export function frontmatterListLimit(field) {
54
+ return field === BUDGET_FIELD ? FRONTMATTER_FIELD_LIMITS.files_to_modify : DEFAULT_LIST_LIMIT;
55
+ }
56
+ const DEFAULT_LIST_LIMIT = Object.freeze({
57
+ maxItems: FRONTMATTER_LIMITS.maxListItems,
58
+ maxItemChars: FRONTMATTER_LIMITS.maxValueChars,
59
+ });
60
+ export function formatFrontmatterListTooLong(field, count, limit) {
61
+ const label = LIST_FIELDS.has(field) ? field : UNLISTED_FIELD_LABEL;
62
+ const safeCount = Number.isSafeInteger(count) && count >= 0 ? Math.min(count, 999_999) : 0;
63
+ const safeLimit = Number.isSafeInteger(limit) && limit >= 0 ? Math.min(limit, 999_999) : 0;
64
+ return `frontmatter-list-too-long:${label}:${safeCount}>${safeLimit}`;
65
+ }
66
+ export function isFrontmatterListTooLongReasonCode(value) {
67
+ return typeof value === 'string' && FRONTMATTER_LIST_TOO_LONG_PATTERN.test(value);
68
+ }
34
69
  const EPIC_TARGET_FIELDS = Object.freeze([
35
70
  'id',
36
71
  'title',
@@ -91,8 +126,29 @@ export const FRONTMATTER_CONTEXT_POLICY = Object.freeze({
91
126
  allowedFields: Object.freeze([]),
92
127
  }),
93
128
  });
129
+ const ENVELOPE_CEILING = Object.freeze({
130
+ maxLines: FRONTMATTER_LIMITS.maxLines + FRONTMATTER_FIELD_BUDGET.extraLines,
131
+ maxBytes: FRONTMATTER_LIMITS.maxBytes + FRONTMATTER_FIELD_BUDGET.extraBytes,
132
+ });
133
+ /**
134
+ * Dependency-free UTF-8 byte count (matches `Buffer.byteLength(..., 'utf8')`,
135
+ * including 3 bytes for lone surrogates) so dependency-free consumers can share
136
+ * this contract without loading `node:*` modules.
137
+ */
94
138
  function byteLength(value) {
95
- return Buffer.byteLength(value, 'utf8');
139
+ let bytes = 0;
140
+ for (const char of value) {
141
+ const codePoint = char.codePointAt(0) ?? 0;
142
+ if (codePoint < 0x80)
143
+ bytes += 1;
144
+ else if (codePoint < 0x800)
145
+ bytes += 2;
146
+ else if (codePoint < 0x10000)
147
+ bytes += 3;
148
+ else
149
+ bytes += 4;
150
+ }
151
+ return bytes;
96
152
  }
97
153
  function scanFrontmatter(source) {
98
154
  if (typeof source !== 'string')
@@ -105,21 +161,25 @@ function scanFrontmatter(source) {
105
161
  const contentStart = opening[0].length;
106
162
  let cursor = contentStart;
107
163
  let lineCount = 1;
164
+ let bytes = contentStart;
108
165
  while (cursor <= source.length) {
109
166
  const newlineIndex = source.indexOf('\n', cursor);
110
167
  const lineEnd = newlineIndex === -1 ? source.length : newlineIndex;
111
168
  const rawLine = source.slice(cursor, lineEnd);
112
169
  const line = rawLine.endsWith('\r') ? rawLine.slice(0, -1) : rawLine;
113
170
  lineCount += 1;
114
- const inspectedEnd = newlineIndex === -1 ? lineEnd : newlineIndex + 1;
115
- if (lineCount > FRONTMATTER_LIMITS.maxLines ||
116
- byteLength(source.slice(0, inspectedEnd)) > FRONTMATTER_LIMITS.maxBytes) {
171
+ bytes += byteLength(rawLine) + (newlineIndex === -1 ? 0 : 1);
172
+ // Absolute structural ceiling; the proportional base budget is enforced
173
+ // after parsing, once the `files_to_modify` contribution is known.
174
+ if (lineCount > ENVELOPE_CEILING.maxLines || bytes > ENVELOPE_CEILING.maxBytes) {
117
175
  return { status: 'oversized' };
118
176
  }
119
177
  if (line === '---') {
120
178
  return {
121
179
  status: 'valid',
122
180
  block: source.slice(contentStart, cursor),
181
+ lines: lineCount,
182
+ bytes,
123
183
  };
124
184
  }
125
185
  if (newlineIndex === -1)
@@ -128,8 +188,8 @@ function scanFrontmatter(source) {
128
188
  }
129
189
  return { status: 'malformed' };
130
190
  }
131
- function unquote(value) {
132
- if (value.length > FRONTMATTER_LIMITS.maxValueChars)
191
+ function unquote(value, maxChars = FRONTMATTER_LIMITS.maxValueChars) {
192
+ if (value.length > maxChars)
133
193
  return null;
134
194
  const first = value[0];
135
195
  const last = value[value.length - 1];
@@ -142,63 +202,93 @@ function unquote(value) {
142
202
  }
143
203
  return value;
144
204
  }
145
- function parseListItem(value) {
146
- const parsed = unquote(value.trim());
205
+ function parseListItem(value, limit) {
206
+ const trimmed = value.trim();
207
+ // Default lists keep the historical raw-length bound. Budget-field items are
208
+ // bounded by content length, so their two quote delimiters are not counted.
209
+ const delimiterAllowance = limit === FRONTMATTER_FIELD_LIMITS.files_to_modify ? 2 : 0;
210
+ if (trimmed.length > limit.maxItemChars + delimiterAllowance)
211
+ return { status: 'oversized' };
212
+ const parsed = unquote(trimmed, limit.maxItemChars + delimiterAllowance);
147
213
  if (parsed === null || parsed.length === 0)
148
- return null;
149
- return parsed;
214
+ return { status: 'malformed' };
215
+ if (parsed.length > limit.maxItemChars)
216
+ return { status: 'oversized' };
217
+ return { status: 'item', value: parsed };
150
218
  }
151
- function parseInlineValue(value) {
219
+ function parseInlineValue(key, value) {
152
220
  const trimmed = value.trim();
153
221
  if (trimmed.length === 0)
154
- return null;
222
+ return { status: 'malformed' };
155
223
  if (trimmed.startsWith('[') || trimmed.endsWith(']')) {
156
224
  if (!trimmed.startsWith('[') || !trimmed.endsWith(']'))
157
- return null;
225
+ return { status: 'malformed' };
158
226
  const body = trimmed.slice(1, -1).trim();
159
227
  if (!body)
160
- return Object.freeze([]);
228
+ return { status: 'value', value: Object.freeze([]) };
229
+ const limit = frontmatterListLimit(key);
161
230
  const rawItems = body.split(',');
162
- if (rawItems.length > FRONTMATTER_LIMITS.maxListItems)
163
- return null;
164
- const items = rawItems.map(parseListItem);
165
- if (items.some((item) => item === null))
166
- return null;
167
- return Object.freeze(items);
231
+ if (rawItems.length > limit.maxItems) {
232
+ return {
233
+ status: 'list-too-long',
234
+ reasonCode: formatFrontmatterListTooLong(key, rawItems.length, limit.maxItems),
235
+ };
236
+ }
237
+ const items = [];
238
+ for (const rawItem of rawItems) {
239
+ const item = parseListItem(rawItem, limit);
240
+ if (item.status !== 'item')
241
+ return item;
242
+ items.push(item.value);
243
+ }
244
+ return { status: 'value', value: Object.freeze(items) };
168
245
  }
246
+ if (trimmed.length > FRONTMATTER_LIMITS.maxValueChars)
247
+ return { status: 'oversized' };
169
248
  const unquoted = unquote(trimmed);
170
249
  if (unquoted === null)
171
- return null;
250
+ return { status: 'malformed' };
172
251
  const quoted = (trimmed.startsWith('"') && trimmed.endsWith('"')) ||
173
252
  (trimmed.startsWith("'") && trimmed.endsWith("'"));
174
253
  if (quoted)
175
- return unquoted;
254
+ return { status: 'value', value: unquoted };
176
255
  if (unquoted === 'true')
177
- return true;
256
+ return { status: 'value', value: true };
178
257
  if (unquoted === 'false')
179
- return false;
258
+ return { status: 'value', value: false };
180
259
  if (/^-?(?:0|[1-9]\d*)$/.test(unquoted)) {
181
260
  const numberValue = Number(unquoted);
182
- if (!Number.isFinite(numberValue) || !Number.isSafeInteger(numberValue))
183
- return null;
184
- return numberValue;
261
+ if (!Number.isFinite(numberValue) || !Number.isSafeInteger(numberValue)) {
262
+ return { status: 'malformed' };
263
+ }
264
+ return { status: 'value', value: numberValue };
185
265
  }
186
- return unquoted;
266
+ return { status: 'value', value: unquoted };
187
267
  }
188
268
  function parseFrontmatterBlock(block) {
189
269
  const fields = {};
190
270
  let fieldCount = 0;
191
271
  let pendingListKey = null;
192
272
  let pendingList = [];
273
+ let pendingCount = 0;
274
+ let budgetLines = 0;
275
+ let budgetBytes = 0;
276
+ // Returns a list-too-long outcome when the committed list exceeds its limit.
193
277
  const commitPendingList = () => {
194
278
  if (!pendingListKey)
195
- return true;
196
- if (pendingList.length > FRONTMATTER_LIMITS.maxListItems)
197
- return false;
279
+ return null;
280
+ const limit = frontmatterListLimit(pendingListKey);
281
+ if (pendingCount > limit.maxItems) {
282
+ return {
283
+ status: 'list-too-long',
284
+ reasonCode: formatFrontmatterListTooLong(pendingListKey, pendingCount, limit.maxItems),
285
+ };
286
+ }
198
287
  fields[pendingListKey] = Object.freeze([...pendingList]);
199
288
  pendingListKey = null;
200
289
  pendingList = [];
201
- return true;
290
+ pendingCount = 0;
291
+ return null;
202
292
  };
203
293
  for (const rawLine of block.split(/\r?\n/)) {
204
294
  if (!rawLine.trim() || rawLine.trimStart().startsWith('#'))
@@ -207,19 +297,26 @@ function parseFrontmatterBlock(block) {
207
297
  if (listMatch) {
208
298
  if (!pendingListKey)
209
299
  return { status: 'malformed', unsupportedValue: true };
210
- if (listMatch[1].trim().length > FRONTMATTER_LIMITS.maxValueChars) {
300
+ const limit = frontmatterListLimit(pendingListKey);
301
+ const item = parseListItem(listMatch[1], limit);
302
+ if (item.status === 'oversized')
211
303
  return { status: 'oversized' };
212
- }
213
- const item = parseListItem(listMatch[1]);
214
- if (item === null)
304
+ if (item.status === 'malformed')
215
305
  return { status: 'malformed', unsupportedValue: true };
216
- pendingList.push(item);
217
- if (pendingList.length > FRONTMATTER_LIMITS.maxListItems)
218
- return { status: 'oversized' };
306
+ pendingCount += 1;
307
+ // Keep counting past the limit so the reason code reports the real size;
308
+ // the structural ceiling bounds the total work. Excess items are dropped.
309
+ if (pendingCount <= limit.maxItems)
310
+ pendingList.push(item.value);
311
+ if (pendingListKey === BUDGET_FIELD) {
312
+ budgetLines += 1;
313
+ budgetBytes += byteLength(rawLine) + 1;
314
+ }
219
315
  continue;
220
316
  }
221
- if (!commitPendingList())
222
- return { status: 'oversized' };
317
+ const committed = commitPendingList();
318
+ if (committed)
319
+ return committed;
223
320
  const fieldMatch = rawLine.match(/^([A-Za-z_][A-Za-z0-9_-]{0,63}):(?:\s*(.*))?$/);
224
321
  if (!fieldMatch)
225
322
  return { status: 'malformed', unsupportedValue: true };
@@ -235,17 +332,29 @@ function parseFrontmatterBlock(block) {
235
332
  pendingListKey = key;
236
333
  continue;
237
334
  }
238
- const value = parseInlineValue(rawValue);
239
- if (value === null) {
240
- return rawValue.length > FRONTMATTER_LIMITS.maxValueChars
241
- ? { status: 'oversized' }
242
- : { status: 'malformed', unsupportedValue: true };
335
+ const value = parseInlineValue(key, rawValue);
336
+ if (value.status === 'list-too-long')
337
+ return value;
338
+ if (value.status === 'oversized')
339
+ return { status: 'oversized' };
340
+ if (value.status === 'malformed')
341
+ return { status: 'malformed', unsupportedValue: true };
342
+ if (key === BUDGET_FIELD && Array.isArray(value.value)) {
343
+ budgetBytes += byteLength(rawValue);
243
344
  }
244
- fields[key] = value;
345
+ fields[key] = value.value;
245
346
  }
246
- if (!commitPendingList())
247
- return { status: 'oversized' };
248
- return { status: 'valid', fields: Object.freeze(fields) };
347
+ const committed = commitPendingList();
348
+ if (committed)
349
+ return committed;
350
+ return { status: 'valid', fields: Object.freeze(fields), budgetLines, budgetBytes };
351
+ }
352
+ function withinProportionalBudget(structural, parsed) {
353
+ const allowedLines = FRONTMATTER_LIMITS.maxLines +
354
+ Math.min(FRONTMATTER_FIELD_BUDGET.extraLines, parsed.budgetLines);
355
+ const allowedBytes = FRONTMATTER_LIMITS.maxBytes +
356
+ Math.min(FRONTMATTER_FIELD_BUDGET.extraBytes, parsed.budgetBytes);
357
+ return structural.lines <= allowedLines && structural.bytes <= allowedBytes;
249
358
  }
250
359
  function result(context, status, reasonCodes, metadata = Object.freeze({})) {
251
360
  return Object.freeze({
@@ -287,7 +396,10 @@ export function inspectFrontmatterContext(source, options) {
287
396
  ...(parsed.unsupportedValue ? ['frontmatter-unsupported-value'] : []),
288
397
  ]);
289
398
  }
290
- if (parsed.status === 'oversized') {
399
+ if (parsed.status === 'list-too-long') {
400
+ return result(options.context, 'oversized', [parsed.reasonCode, 'frontmatter-oversized']);
401
+ }
402
+ if (parsed.status === 'oversized' || !withinProportionalBudget(structural, parsed)) {
291
403
  return result(options.context, 'oversized', ['frontmatter-oversized']);
292
404
  }
293
405
  const policy = FRONTMATTER_CONTEXT_POLICY[options.context];
@@ -0,0 +1,202 @@
1
+ import { createHash } from 'node:crypto';
2
+ /**
3
+ * P226.09 `ProjectManifestProjection/1.0` (P226-CON-MANIFEST-PROJECTION).
4
+ *
5
+ * Planning data only: script *names* and relative manifest paths of the
6
+ * consumer project. Never script bodies, versions, dependencies, registry URLs,
7
+ * environment values or absolute paths. It grants no execution or QA authority.
8
+ */
9
+ export const PROJECT_MANIFEST_PROJECTION_SCHEMA = 'ProjectManifestProjection/1.0';
10
+ /** Public server capability; clients send the optional request field only when advertised. */
11
+ export const PROJECT_MANIFEST_PROJECTION_CAPABILITY = 'project-manifest-projection-v1';
12
+ export const PROJECT_MANIFEST_PROJECTION_MAX_MANIFESTS = 16;
13
+ export const PROJECT_MANIFEST_PROJECTION_MAX_SCRIPTS = 64;
14
+ export const PROJECT_MANIFEST_PROJECTION_MAX_PATH_LENGTH = 256;
15
+ export const PROJECT_MANIFEST_PROJECTION_MAX_MANIFEST_BYTES = 64 * 1024;
16
+ export const PROJECT_MANIFEST_PROJECTION_SCRIPT_NAME = /^[a-z0-9:_.-]{1,64}$/;
17
+ export const PROJECT_MANIFEST_PACKAGE_MANAGERS = ['npm', 'pnpm', 'yarn', 'bun', 'unknown'];
18
+ export const PROJECT_MANIFEST_PROJECTION_OMISSION_KEYS = [
19
+ 'invalidJson',
20
+ 'invalidScriptNames',
21
+ 'outOfScope',
22
+ 'overLimit',
23
+ 'oversize',
24
+ 'symlink',
25
+ ];
26
+ const TOP_KEYS = ['manifests', 'omitted', 'schema'];
27
+ const ENTRY_KEYS = ['packageManager', 'path', 'scriptNames'];
28
+ const ATTACHMENT_KEYS = ['projection', 'sha256'];
29
+ const SHA256 = /^[a-f0-9]{64}$/;
30
+ function isRecord(value) {
31
+ return !!value && typeof value === 'object' && !Array.isArray(value);
32
+ }
33
+ function hasExactKeys(value, keys) {
34
+ const own = Object.keys(value);
35
+ return own.length === keys.length && own.every((key) => keys.includes(key));
36
+ }
37
+ /** Relative POSIX path to a `package.json`, inside the project root. Workspace
38
+ * dirs can reach argv, so whitespace anywhere and option-like segments (leading
39
+ * `-`, e.g. `-g`, `--registry=x`) are refused (P227.12, EV-15). */
40
+ export function isProjectManifestPath(value) {
41
+ if (typeof value !== 'string' || value.length === 0
42
+ || value.length > PROJECT_MANIFEST_PROJECTION_MAX_PATH_LENGTH)
43
+ return false;
44
+ if (value !== 'package.json' && !value.endsWith('/package.json'))
45
+ return false;
46
+ if (value.startsWith('/') || value.includes('\\') || value.includes('//'))
47
+ return false;
48
+ if (/[\u0000-\u001f\u007f]/.test(value) || /\s/u.test(value))
49
+ return false;
50
+ return value.split('/').every((segment) => segment !== '' && segment !== '.' && segment !== '..'
51
+ && !segment.startsWith('-'));
52
+ }
53
+ /** Root manifest first, then lexicographic by path. */
54
+ export function compareProjectManifestPaths(left, right) {
55
+ if (left === right)
56
+ return 0;
57
+ if (left === 'package.json')
58
+ return -1;
59
+ if (right === 'package.json')
60
+ return 1;
61
+ return left < right ? -1 : 1;
62
+ }
63
+ function canonicalJson(value) {
64
+ if (Array.isArray(value))
65
+ return `[${value.map(canonicalJson).join(',')}]`;
66
+ if (isRecord(value)) {
67
+ return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${canonicalJson(value[key])}`).join(',')}}`;
68
+ }
69
+ return JSON.stringify(value);
70
+ }
71
+ export function projectManifestProjectionSha256(projection) {
72
+ return createHash('sha256').update(canonicalJson(projection), 'utf8').digest('hex');
73
+ }
74
+ /** Closed validator. Reasons are stable and never echo the rejected value. */
75
+ export function validateProjectManifestProjection(value) {
76
+ const fail = (reason) => ({ ok: false, reason });
77
+ if (!isRecord(value))
78
+ return fail('project-manifest-projection-not-object');
79
+ if (!hasExactKeys(value, TOP_KEYS))
80
+ return fail('project-manifest-projection-unknown-field');
81
+ if (value.schema !== PROJECT_MANIFEST_PROJECTION_SCHEMA)
82
+ return fail('project-manifest-projection-schema-mismatch');
83
+ const omitted = value.omitted;
84
+ if (!isRecord(omitted) || !hasExactKeys(omitted, PROJECT_MANIFEST_PROJECTION_OMISSION_KEYS)
85
+ || PROJECT_MANIFEST_PROJECTION_OMISSION_KEYS.some((key) => {
86
+ const count = omitted[key];
87
+ return typeof count !== 'number' || !Number.isSafeInteger(count) || count < 0;
88
+ }))
89
+ return fail('project-manifest-projection-omitted-invalid');
90
+ if (!Array.isArray(value.manifests))
91
+ return fail('project-manifest-projection-not-object');
92
+ if (value.manifests.length > PROJECT_MANIFEST_PROJECTION_MAX_MANIFESTS) {
93
+ return fail('project-manifest-projection-too-many-manifests');
94
+ }
95
+ const seen = new Set();
96
+ let previous;
97
+ for (const entry of value.manifests) {
98
+ if (!isRecord(entry))
99
+ return fail('project-manifest-projection-not-object');
100
+ if (!hasExactKeys(entry, ENTRY_KEYS))
101
+ return fail('project-manifest-projection-unknown-field');
102
+ if (!isProjectManifestPath(entry.path))
103
+ return fail('project-manifest-projection-path-invalid');
104
+ if (seen.has(entry.path))
105
+ return fail('project-manifest-projection-path-duplicate');
106
+ if (previous !== undefined && compareProjectManifestPaths(previous, entry.path) >= 0) {
107
+ return fail('project-manifest-projection-order-invalid');
108
+ }
109
+ seen.add(entry.path);
110
+ previous = entry.path;
111
+ if (typeof entry.packageManager !== 'string'
112
+ || !PROJECT_MANIFEST_PACKAGE_MANAGERS.includes(entry.packageManager)) {
113
+ return fail('project-manifest-projection-package-manager-invalid');
114
+ }
115
+ if (!Array.isArray(entry.scriptNames))
116
+ return fail('project-manifest-projection-script-name-invalid');
117
+ if (entry.scriptNames.length > PROJECT_MANIFEST_PROJECTION_MAX_SCRIPTS) {
118
+ return fail('project-manifest-projection-too-many-scripts');
119
+ }
120
+ let previousName;
121
+ for (const name of entry.scriptNames) {
122
+ if (typeof name !== 'string' || !PROJECT_MANIFEST_PROJECTION_SCRIPT_NAME.test(name)) {
123
+ return fail('project-manifest-projection-script-name-invalid');
124
+ }
125
+ if (previousName !== undefined && previousName >= name) {
126
+ return fail('project-manifest-projection-script-names-unsorted');
127
+ }
128
+ previousName = name;
129
+ }
130
+ }
131
+ return { ok: true, value: value };
132
+ }
133
+ /** Validate a request attachment and its digest binding. */
134
+ export function validateProjectManifestProjectionAttachment(value) {
135
+ if (!isRecord(value))
136
+ return { ok: false, reason: 'project-manifest-projection-not-object' };
137
+ if (!hasExactKeys(value, ATTACHMENT_KEYS))
138
+ return { ok: false, reason: 'project-manifest-projection-unknown-field' };
139
+ const validation = validateProjectManifestProjection(value.projection);
140
+ if (!validation.ok)
141
+ return validation;
142
+ if (typeof value.sha256 !== 'string' || !SHA256.test(value.sha256)
143
+ || projectManifestProjectionSha256(validation.value) !== value.sha256) {
144
+ return { ok: false, reason: 'project-manifest-projection-digest-mismatch' };
145
+ }
146
+ return validation;
147
+ }
148
+ /**
149
+ * Build a frozen, valid projection. Script names outside the public pattern are
150
+ * dropped and counted; names are deduplicated/sorted and capped at 64; manifests
151
+ * are sorted root-first and capped at 16 (excess counted as `overLimit`).
152
+ */
153
+ export function createProjectManifestProjection(input) {
154
+ const omitted = {
155
+ invalidJson: 0, invalidScriptNames: 0, outOfScope: 0, overLimit: 0, oversize: 0, symlink: 0,
156
+ };
157
+ for (const key of PROJECT_MANIFEST_PROJECTION_OMISSION_KEYS) {
158
+ const count = input.omitted?.[key];
159
+ if (count !== undefined) {
160
+ if (!Number.isSafeInteger(count) || count < 0)
161
+ throw new Error('project-manifest-projection-omitted-invalid');
162
+ omitted[key] = count;
163
+ }
164
+ }
165
+ const byPath = new Map();
166
+ for (const entry of input.manifests) {
167
+ if (!isProjectManifestPath(entry.path)) {
168
+ omitted.outOfScope += 1;
169
+ continue;
170
+ }
171
+ if (byPath.has(entry.path))
172
+ continue;
173
+ const valid = [...new Set(entry.scriptNames)].filter((name) => PROJECT_MANIFEST_PROJECTION_SCRIPT_NAME.test(name)).sort();
174
+ omitted.invalidScriptNames += new Set(entry.scriptNames).size - valid.length;
175
+ if (valid.length > PROJECT_MANIFEST_PROJECTION_MAX_SCRIPTS) {
176
+ omitted.invalidScriptNames += valid.length - PROJECT_MANIFEST_PROJECTION_MAX_SCRIPTS;
177
+ }
178
+ byPath.set(entry.path, Object.freeze({
179
+ path: entry.path,
180
+ packageManager: PROJECT_MANIFEST_PACKAGE_MANAGERS.includes(entry.packageManager)
181
+ ? entry.packageManager : 'unknown',
182
+ scriptNames: Object.freeze(valid.slice(0, PROJECT_MANIFEST_PROJECTION_MAX_SCRIPTS)),
183
+ }));
184
+ }
185
+ const sorted = [...byPath.values()].sort((left, right) => compareProjectManifestPaths(left.path, right.path));
186
+ omitted.overLimit += Math.max(0, sorted.length - PROJECT_MANIFEST_PROJECTION_MAX_MANIFESTS);
187
+ const projection = Object.freeze({
188
+ schema: PROJECT_MANIFEST_PROJECTION_SCHEMA,
189
+ manifests: Object.freeze(sorted.slice(0, PROJECT_MANIFEST_PROJECTION_MAX_MANIFESTS)),
190
+ omitted: Object.freeze(omitted),
191
+ });
192
+ const validation = validateProjectManifestProjection(projection);
193
+ if (!validation.ok)
194
+ throw new Error(validation.reason);
195
+ return projection;
196
+ }
197
+ export function createProjectManifestProjectionAttachment(projection) {
198
+ const validation = validateProjectManifestProjection(projection);
199
+ if (!validation.ok)
200
+ throw new Error(validation.reason);
201
+ return Object.freeze({ projection, sha256: projectManifestProjectionSha256(projection) });
202
+ }
@@ -2,6 +2,19 @@
2
2
  * These are planning data, never permission to execute or independent QA.
3
3
  */
4
4
  export const PROJECT_VALIDATION_KINDS = ['unit', 'integration', 'contract', 'bdd-example', 'e2e'];
5
+ /** Literal argv-shaped command: no shell language, quoted substitutions,
6
+ * inline secrets, private URLs or parent-directory traversal. Shared by
7
+ * declared commands and server-derived commands (P226.10). */
8
+ export function isSafeProjectValidationCommand(command) {
9
+ return command.length >= 1 && command.length <= 220
10
+ && /^[A-Za-z0-9_./-]+(?: [A-Za-z0-9_./:@%+=,-]+)*$/.test(command)
11
+ && !/(?:token|password|license_key|secret|credential)=|(?:https?|ssh):\/\/|@[^ /]+\.[A-Za-z]/i.test(command)
12
+ && !command.split(' ').some(arg => arg.split('/').includes('..'));
13
+ }
14
+ /** Reads `## Validation Commands` declarations. A kind that is simply not
15
+ * declared is not an error (P226.10): the planner derives it or records
16
+ * `project-validation-commands-undeclared` debt for that kind only. `invalid`
17
+ * is reserved for malformed, unsafe or conflicting declarations. */
5
18
  export function readProjectValidationCommands(storyId, sources) {
6
19
  const byKind = new Map();
7
20
  let declared = false, invalid = false;
@@ -45,11 +58,8 @@ export function readProjectValidationCommands(storyId, sources) {
45
58
  continue;
46
59
  }
47
60
  const kind = match[1], command = match[2];
48
- // No shell language, quoted substitutions, inline secrets or private URLs.
49
61
  // A command stays a literal argv-shaped string throughout this planner.
50
- if (!/^[A-Za-z0-9_./-]+(?: [A-Za-z0-9_./:@%+=,-]+)*$/.test(command)
51
- || /(?:token|password|license_key|secret|credential)=|(?:https?|ssh):\/\/|@[^ /]+\.[A-Za-z]/i.test(command)
52
- || command.split(' ').some(arg => arg.split('/').includes('..'))) {
62
+ if (!isSafeProjectValidationCommand(command)) {
53
63
  invalid = true;
54
64
  continue;
55
65
  }
@@ -68,7 +78,5 @@ export function readProjectValidationCommands(storyId, sources) {
68
78
  if (active && entries === 0)
69
79
  invalid = true;
70
80
  }
71
- if (declared && !byKind.has('unit'))
72
- invalid = true;
73
81
  return { declared, invalid, commands: [...byKind.values()] };
74
82
  }