Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

3.2 KB
Raw Blame
/** Typed prerequisites keep another repository's item out of local Git dependency paths [B24,H16]. */
import { Refused } from './refused.js';

/** Refuse an invalid dependency rather than turning it into NaN or a local item number. */
function invalid() {
  throw new Refused('BAD_DEPENDENCIES', 'use positive item ids or repo#id after --after, separated by commas; use --after none to clear them');
}

/** Parse one exact local or repository-qualified prerequisite without consulting machine state. */
export function parseDependency(value) {
  if (Number.isSafeInteger(value) && value > 0) return { kind: 'local', id: value };
  if (typeof value !== 'string') invalid();
  const local = Number(value.replace(/^#/, ''));
  if (value.trim() && Number.isSafeInteger(local) && local > 0) return { kind: 'local', id: local };
  const match = /^([A-Za-z0-9][A-Za-z0-9._-]{0,127})#([1-9][0-9]*)$/u.exec(value);
  if (!match || !Number.isSafeInteger(Number(match[2]))) invalid();
  return { kind: 'foreign', repo: match[1], id: Number(match[2]) };
}

/** Keep native dependency-edit arrays typed; only CLI and persisted fields decode textual local ids. */
export function validateNativeDependencies(value) {
  if (!Array.isArray(value)) invalid();
  for (const part of value) {
    const reference = parseDependency(part);
    if (reference.kind === 'local' && typeof part !== 'number') invalid();
  }
}

/** Read a persisted comma-separated field or a public dependency list as typed references. */
export function dependencyReferences(value) {
  if (value && typeof value === 'object' && !Array.isArray(value)) value = value.item_after;
  if (value === undefined || value === null || value === '') return [];
  const parts = Array.isArray(value) ? value : typeof value === 'string' ? value.split(',') : invalid();
  return parts.map(parseDependency);
}

/** Preserve the historical numeric local representation and the exact qualified repository name. */
export function dependencyValues(value) {
  return dependencyReferences(value).map(ref => ref.kind === 'local' ? ref.id : `${ref.repo}#${ref.id}`);
}

/** Local traversal, merge and lane-file inheritance must never consume another board's item id. */
export function localDependencyIds(value) {
  return dependencyReferences(value).filter(ref => ref.kind === 'local').map(ref => ref.id);
}

/** Keep unavailable foreign prerequisites visible until their own board's authority resolves them. */
export function foreignDependencies(value) {
  return dependencyReferences(value).filter(ref => ref.kind === 'foreign');
}

/** Display a local item with its hash, retaining the repository on a foreign item. */
export function dependencyLabel(ref) {
  return ref.kind === 'local' ? `#${ref.id}` : `${ref.repo}#${ref.id}`;
}

/** Detect the new wire shape without changing the native validation of malformed older arguments. */
export function hasForeignDependencies(value) {
  if (value && typeof value === 'object' && !Array.isArray(value)) value = value.item_after;
  const parts = Array.isArray(value) ? value : typeof value === 'string' ? value.split(',') : [];
  return parts.some(part => typeof part === 'string' && /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}#[1-9][0-9]*$/u.test(part));
}