2026-08-06 09f01778d5a034e2af50ae05da8908f7b6a871c8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import fs from 'node:fs';
import path from 'node:path';
import { slugFromTarget } from './target-slug.mjs';
 
export const SURFACE_BRIEF_VERSION = 1;
 
export function getSurfaceBriefDir(projectRoot) {
  return path.join(projectRoot, '.impeccable', 'surfaces');
}
 
export function normalizeSurfaceTarget(target, { projectRoot = process.cwd() } = {}) {
  if (!target || typeof target !== 'string' || !target.trim()) return null;
  const trimmed = target.trim();
  if (/^https?:\/\//i.test(trimmed)) {
    try {
      const url = new URL(trimmed);
      url.hash = '';
      url.search = '';
      return url.toString().replace(/\/$/, '') || url.origin;
    } catch {
      return null;
    }
  }
  if (/^route:/i.test(trimmed)) {
    const route = trimmed.slice(trimmed.indexOf(':') + 1).trim();
    if (!route.startsWith('/') || route.includes('..')) return null;
    const normalizedRoute = route.split(/[?#]/, 1)[0].replace(/\/{2,}/g, '/').replace(/\/$/, '') || '/';
    return `route:${normalizedRoute}`;
  }
  if (trimmed === '/') return 'route:/';
  if (trimmed.startsWith('/')) {
    const absolute = path.resolve(trimmed);
    const relativeToProject = path.relative(projectRoot, absolute);
    const isProjectFile = relativeToProject && !relativeToProject.startsWith('..') && !path.isAbsolute(relativeToProject);
    if (!isProjectFile && !fs.existsSync(absolute) && !trimmed.includes('..')) {
      const normalizedRoute = trimmed.split(/[?#]/, 1)[0].replace(/\/{2,}/g, '/').replace(/\/$/, '') || '/';
      return `route:${normalizedRoute}`;
    }
  }
  const abs = path.isAbsolute(trimmed) ? trimmed : path.resolve(projectRoot, trimmed);
  const rel = path.relative(projectRoot, abs);
  if (!rel || rel === '.' || rel.startsWith('..') || path.isAbsolute(rel)) return null;
  return rel.split(path.sep).join('/');
}
 
export function surfaceBriefPathForTarget(target, { projectRoot = process.cwd() } = {}) {
  const normalized = normalizeSurfaceTarget(target, { projectRoot });
  if (!normalized) return null;
  const slugInput = normalized.startsWith('route:') ? `route${normalized.slice('route:'.length)}` : normalized;
  const slug = slugFromTarget(slugInput, { cwd: projectRoot });
  return slug ? path.join(getSurfaceBriefDir(projectRoot), `${slug}.md`) : null;
}
 
export function parseSurfaceBrief(text, filePath = null) {
  const match = String(text || '').match(/^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n|$)/);
  const meta = {};
  if (match) {
    for (const line of match[1].split(/\r?\n/)) {
      const colon = line.indexOf(':');
      if (colon < 0) continue;
      const key = line.slice(0, colon).trim();
      const raw = line.slice(colon + 1).trim();
      if (!key) continue;
      if (/^(?:\[|\{|\")/.test(raw) || /^(?:true|false|null|-?\d+(?:\.\d+)?)$/.test(raw)) {
        try { meta[key] = JSON.parse(raw); continue; } catch { /* keep string */ }
      }
      meta[key] = raw.replace(/^['"]|['"]$/g, '');
    }
  }
  const primaryTarget = typeof meta.primary_target === 'string' ? meta.primary_target : null;
  const relatedTargets = Array.isArray(meta.related_targets)
    ? meta.related_targets.filter((value) => typeof value === 'string')
    : [];
  return {
    path: filePath,
    text: String(text || ''),
    body: match ? String(text || '').slice(match[0].length).trim() : String(text || '').trim(),
    meta,
    slug: typeof meta.slug === 'string' ? meta.slug : filePath ? path.basename(filePath, '.md') : null,
    primaryTarget,
    relatedTargets,
    targets: [primaryTarget, ...relatedTargets].filter(Boolean),
  };
}
 
export function listSurfaceBriefs(projectRoot = process.cwd()) {
  const dir = getSurfaceBriefDir(projectRoot);
  let names;
  try {
    names = fs.readdirSync(dir).filter((name) => name.endsWith('.md')).sort();
  } catch {
    return [];
  }
  return names.flatMap((name) => {
    const filePath = path.join(dir, name);
    try {
      return [parseSurfaceBrief(fs.readFileSync(filePath, 'utf-8'), filePath)];
    } catch {
      return [];
    }
  });
}
 
export function resolveSurfaceBrief(projectRoot = process.cwd(), target = null) {
  const briefs = listSurfaceBriefs(projectRoot);
  if (!target) {
    return {
      brief: briefs.length === 1 ? briefs[0] : null,
      candidates: briefs,
      reason: briefs.length === 1 ? 'only-brief' : briefs.length > 1 ? 'ambiguous' : 'none',
    };
  }
 
  const normalized = normalizeSurfaceTarget(target, { projectRoot });
  if (!normalized) return { brief: null, candidates: briefs, reason: 'invalid-target' };
  const exactPath = surfaceBriefPathForTarget(normalized, { projectRoot });
  const exact = briefs.find((brief) => brief.path === exactPath && (!brief.targets.length || brief.targets.includes(normalized)));
  if (exact) return { brief: exact, candidates: briefs, reason: 'slug' };
  const mapped = briefs.filter((brief) => brief.targets.includes(normalized));
  return {
    brief: mapped.length === 1 ? mapped[0] : null,
    candidates: mapped.length > 1 ? mapped : briefs,
    reason: mapped.length === 1 ? 'mapping' : mapped.length > 1 ? 'ambiguous-target' : 'not-found',
  };
}
 
export function writeSurfaceBrief({
  projectRoot = process.cwd(),
  primaryTarget,
  relatedTargets = [],
  body,
}) {
  const normalizedPrimary = normalizeSurfaceTarget(primaryTarget, { projectRoot });
  if (!normalizedPrimary) throw new Error('surface brief requires a concrete project-relative primary target or URL');
  const normalizedRelated = [...new Set(relatedTargets
    .map((target) => normalizeSurfaceTarget(target, { projectRoot }))
    .filter((target) => target && target !== normalizedPrimary))];
  const slug = slugFromTarget(normalizedPrimary, { cwd: projectRoot });
  const filePath = surfaceBriefPathForTarget(normalizedPrimary, { projectRoot });
  fs.mkdirSync(path.dirname(filePath), { recursive: true });
  const frontmatter = [
    '---',
    `version: ${SURFACE_BRIEF_VERSION}`,
    `slug: ${JSON.stringify(slug)}`,
    `primary_target: ${JSON.stringify(normalizedPrimary)}`,
    `related_targets: ${JSON.stringify(normalizedRelated)}`,
    '---',
  ].join('\n');
  fs.writeFileSync(filePath, `${frontmatter}\n\n${String(body || '').trim()}\n`, 'utf-8');
  return filePath;
}