draftgo-cli 3.0.49 → 3.0.52

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.
@@ -15,6 +15,7 @@ const LONG_CONTENT_RESOURCE_TYPES = Object.freeze([
15
15
  'pages',
16
16
  'navigations',
17
17
  'docs/articles',
18
+ 'custom_services',
18
19
  ]);
19
20
  const DB_META_LIST_PATH = '/api/db-meta';
20
21
  const DB_META_PAGE_SIZE = 100;
@@ -292,6 +293,7 @@ function rawQuery(session, expectedName, args, server, options = {}) {
292
293
  tool,
293
294
  canonical_tool: expectedName,
294
295
  arguments: args,
296
+ fetched_at: new Date().toISOString(),
295
297
  },
296
298
  result,
297
299
  }));
@@ -600,28 +602,41 @@ async function collectContext(projectDir, taskValue, options = {}) {
600
602
  }
601
603
 
602
604
  return {
603
- schema_version: '1.1',
605
+ schema_version: '2.0',
604
606
  task,
605
- reference_bundle: {
607
+ bundled_references: {
606
608
  source: 'draftgo-cli',
607
609
  version: pkg.version,
608
610
  installed_skill_version: installedSkillVersion,
609
611
  synchronized: installedSkillVersion == null ? null : installedSkillVersion === pkg.version,
612
+ sections: references.map((entry) => ({
613
+ ...entry,
614
+ source: { ...entry.source, source: 'cli_bundle', version: pkg.version },
615
+ })),
610
616
  },
611
- references,
612
- live: {
613
- session: {
614
- server: config.server,
615
- protocol_version: session.client.protocolVersion,
616
- },
617
- project,
618
- resources,
619
- api,
620
- data_structures: dataStructures,
617
+ live_project: project,
618
+ live_resources: resources,
619
+ live_apis: api,
620
+ live_db_meta: dataStructures,
621
+ live_session: {
622
+ server: config.server,
623
+ protocol_version: session.client.protocolVersion,
624
+ fetched_at: new Date().toISOString(),
621
625
  },
622
626
  };
623
627
  }
624
628
 
629
+ function compactContext(value) {
630
+ if (Array.isArray(value)) return value.map(compactContext);
631
+ if (!value || typeof value !== 'object') return value;
632
+ if (value.source && Object.prototype.hasOwnProperty.call(value, 'result')) {
633
+ return { source: compactContext(value.source), value: structuredValueFromRaw(value.result) };
634
+ }
635
+ const result = {};
636
+ for (const [key, child] of Object.entries(value)) result[key] = compactContext(child);
637
+ return result;
638
+ }
639
+
625
640
  function readInstalledSkillVersion(projectDir) {
626
641
  return readInstalledVersion(projectDir);
627
642
  }
@@ -643,4 +658,5 @@ module.exports = {
643
658
  collectDataStructures,
644
659
  readInstalledSkillVersion,
645
660
  collectContext,
661
+ compactContext,
646
662
  };
@@ -0,0 +1,246 @@
1
+ 'use strict';
2
+
3
+ const crypto = require('crypto');
4
+ const fs = require('fs');
5
+ const path = require('path');
6
+ const { spawnSync } = require('child_process');
7
+ const AdmZip = require('adm-zip');
8
+ const { loadProjectConfig } = require('./projectConfig');
9
+ const { WorktreeError } = require('./worktree/errors');
10
+
11
+ const FILES = ['service.go', 'go.mod', 'go.sum', 'service.json'];
12
+ function root(projectDir) { return path.join(projectDir, '.draftgo', 'worktree', 'custom-services'); }
13
+ function baseRoot(projectDir) { return path.join(projectDir, '.draftgo', 'worktree', '.base', 'custom-services'); }
14
+ function conflictRoot(projectDir, id) { return path.join(projectDir, '.draftgo', 'conflicts', 'custom-services', safe(id)); }
15
+ function manifestPath(projectDir) { return path.join(root(projectDir), 'manifest.json'); }
16
+ function safe(value) { return String(value).replace(/[^A-Za-z0-9._-]+/g, '_').replace(/^\.+/, '').slice(0, 80) || 'service'; }
17
+ function sha256(value) { return crypto.createHash('sha256').update(value).digest('hex'); }
18
+ function relative(projectDir, file) { return path.relative(projectDir, file).replace(/\\/g, '/'); }
19
+ function loadManifest(projectDir) {
20
+ const file = manifestPath(projectDir);
21
+ if (!fs.existsSync(file)) return { schema_version: 1, entries: {} };
22
+ const value = JSON.parse(fs.readFileSync(file, 'utf8'));
23
+ if (!value || value.schema_version !== 1 || !value.entries || Array.isArray(value.entries)) throw new WorktreeError('INVALID_WORKTREE_MANIFEST', 'Invalid custom-service manifest.');
24
+ return value;
25
+ }
26
+ function atomicWrite(file, value) {
27
+ fs.mkdirSync(path.dirname(file), { recursive: true });
28
+ const temporary = `${file}.tmp-${process.pid}-${Date.now()}`;
29
+ fs.writeFileSync(temporary, value); fs.renameSync(temporary, file);
30
+ }
31
+ function saveManifest(projectDir, value) { value.updated_at = new Date().toISOString(); atomicWrite(manifestPath(projectDir), `${JSON.stringify(value, null, 2)}\n`); }
32
+ function entryKey(id) { return String(id); }
33
+ function serviceDir(projectDir, id, slug) { return path.join(root(projectDir), `${safe(slug || id)}-${safe(id)}`); }
34
+ function serviceBaseDir(projectDir, id) { return path.join(baseRoot(projectDir), safe(id)); }
35
+ function parseEnvelope(text, response) {
36
+ let payload = {};
37
+ try { payload = text ? JSON.parse(text) : {}; } catch { payload = { message: text }; }
38
+ if (!response.ok || (payload.code && Number(payload.code) >= 400)) {
39
+ const details = payload.error?.details || payload.details || payload;
40
+ const error = new WorktreeError(payload.error?.code || payload.code || `HTTP_${response.status}`, payload.error?.message || payload.message || `DraftGo HTTP ${response.status}`, details);
41
+ error.status = response.status; throw error;
42
+ }
43
+ return payload.data === undefined ? payload : payload.data;
44
+ }
45
+ async function jsonRequest(config, method, endpoint, body, options = {}) {
46
+ const response = await (options.fetch || fetch)(`${config.server.replace(/\/+$/, '')}/api/${endpoint.replace(/^\/+/, '')}`, {
47
+ method, headers: { Authorization: `Bearer ${config.token || config.sat}`, ...(body === undefined ? {} : { 'Content-Type': 'application/json' }) },
48
+ body: body === undefined ? undefined : JSON.stringify(body), signal: options.signal,
49
+ });
50
+ return parseEnvelope(await response.text(), response);
51
+ }
52
+ async function metadata(config, id, options = {}) { return jsonRequest(config, 'GET', `content/custom-services/${encodeURIComponent(id)}/checkout`, undefined, options); }
53
+ async function download(config, metadataValue, options = {}) {
54
+ const url = new URL(metadataValue.download_url, `${config.server.replace(/\/+$/, '')}/`).toString();
55
+ const response = await (options.fetch || fetch)(url, { headers: { Authorization: `Bearer ${config.token || config.sat}` }, signal: options.signal });
56
+ if (!response.ok) parseEnvelope(await response.text(), response);
57
+ const archive = Buffer.from(await response.arrayBuffer());
58
+ if (sha256(archive) !== metadataValue.content_hash) throw new WorktreeError('HASH_MISMATCH', 'Downloaded custom-service archive hash mismatch.');
59
+ return archive;
60
+ }
61
+ function archiveFiles(archive) {
62
+ const zip = new AdmZip(archive); const result = {};
63
+ for (const filename of FILES) {
64
+ const entry = zip.getEntry(filename);
65
+ if (!entry || entry.isDirectory) throw new WorktreeError('INVALID_SERVICE_ARCHIVE', `Custom-service archive is missing ${filename}.`);
66
+ result[filename] = entry.getData();
67
+ }
68
+ const unexpected = zip.getEntries().filter((entry) => !entry.isDirectory && !FILES.includes(entry.entryName));
69
+ if (unexpected.length) throw new WorktreeError('INVALID_SERVICE_ARCHIVE', `Unexpected archive entry ${unexpected[0].entryName}.`);
70
+ JSON.parse(result['service.json'].toString('utf8'));
71
+ return result;
72
+ }
73
+ function writeFiles(directory, files) { fs.mkdirSync(directory, { recursive: true }); for (const filename of FILES) atomicWrite(path.join(directory, filename), files[filename]); }
74
+ function readFiles(directory) {
75
+ const result = {};
76
+ for (const filename of FILES) {
77
+ const file = path.join(directory, filename);
78
+ if (!fs.existsSync(file)) throw new WorktreeError('SERVICE_FILE_MISSING', `Custom-service worktree is missing ${filename}.`);
79
+ result[filename] = fs.readFileSync(file);
80
+ }
81
+ try { JSON.parse(result['service.json'].toString('utf8')); } catch (error) { throw new WorktreeError('INVALID_SERVICE_METADATA', `service.json is invalid: ${error.message}`); }
82
+ return result;
83
+ }
84
+ function createArchive(files) {
85
+ const zip = new AdmZip();
86
+ for (const filename of FILES) zip.addFile(filename, files[filename]);
87
+ return zip.toBuffer();
88
+ }
89
+ function fileHashes(files) { return Object.fromEntries(FILES.map((filename) => [filename, sha256(files[filename])])); }
90
+ function checkoutHash(files) { return sha256(createArchive(files)); }
91
+ function remoteFileHashes(remote) {
92
+ return { 'service.go': remote.source_hash, 'go.mod': remote.go_mod_hash, 'go.sum': remote.go_sum_hash, 'service.json': remote.metadata_hash };
93
+ }
94
+ function filesMatchRemote(files, remote) {
95
+ const local = fileHashes(files); const expected = remoteFileHashes(remote);
96
+ return FILES.every((filename) => expected[filename] && local[filename] === expected[filename]);
97
+ }
98
+ function localState(entry) {
99
+ const local = readFiles(entry.local_dir); const base = readFiles(entry.base_dir);
100
+ const localHashes = fileHashes(local); const baseHashes = fileHashes(base);
101
+ const baseValid = FILES.every((filename) => baseHashes[filename] === entry.file_hashes[filename]);
102
+ return { local, base, local_hashes: localHashes, base_hashes: baseHashes, base_valid: baseValid,
103
+ modified: FILES.some((filename) => localHashes[filename] !== baseHashes[filename]) };
104
+ }
105
+ async function checkout(projectDir, ids, options = {}) {
106
+ const config = options.config || loadProjectConfig(projectDir); const manifest = loadManifest(projectDir); const results = [];
107
+ for (const id of ids) {
108
+ const remote = await metadata(config, id, options); const key = entryKey(id); const old = manifest.entries[key];
109
+ if (old && fs.existsSync(old.local_dir) && !options.force && localState(old).modified) {
110
+ throw new WorktreeError('LOCAL_CHANGES_PRESENT', `Refusing to replace locally modified custom service ${id}; commit it or use checkout --force.`);
111
+ }
112
+ const archive = await download(config, remote, options); const files = archiveFiles(archive);
113
+ const service = JSON.parse(files['service.json'].toString('utf8')); const localDir = old?.local_dir || serviceDir(projectDir, id, service.slug); const baseDir = serviceBaseDir(projectDir, id);
114
+ writeFiles(localDir, files); writeFiles(baseDir, files);
115
+ const entry = { server: config.server, resource_type: 'custom_services', resource_id: String(id), title: remote.title,
116
+ slug: service.slug, local_dir: localDir, base_dir: baseDir, local_path: relative(projectDir, localDir), base_path: relative(projectDir, baseDir),
117
+ content_type: remote.content_type, base_revision: remote.base_revision, base_etag: remote.etag,
118
+ base_hash: remote.content_hash, file_hashes: fileHashes(files), checkout_source: remote.checkout_source,
119
+ checked_out_at: new Date().toISOString(), updated_at: remote.updated_at, updated_by: remote.updated_by };
120
+ manifest.entries[key] = entry; saveManifest(projectDir, manifest); results.push(entry);
121
+ }
122
+ return results;
123
+ }
124
+ function getEntry(projectDir, id) { return loadManifest(projectDir).entries[entryKey(id)] || null; }
125
+ function diffFile(baseFile, localFile, filename) {
126
+ if (fs.readFileSync(baseFile).equals(fs.readFileSync(localFile))) return '';
127
+ const result = spawnSync('git', ['diff', '--no-index', '--', baseFile, localFile], { encoding: 'utf8', windowsHide: true });
128
+ if (result.error || ![0, 1].includes(result.status)) return `${filename}: changed\n`;
129
+ return String(result.stdout || '').replaceAll(baseFile, `a/${filename}`).replaceAll(localFile, `b/${filename}`);
130
+ }
131
+ function diff(projectDir, id) {
132
+ const entry = getEntry(projectDir, id); if (!entry) throw new WorktreeError('RESOURCE_NOT_CHECKED_OUT', `Custom service ${id} is not checked out.`);
133
+ const state = localState(entry); const differences = FILES.map((filename) => diffFile(path.join(entry.base_dir, filename), path.join(entry.local_dir, filename), filename)).filter(Boolean);
134
+ return { changed: differences.length > 0, entry, files: FILES.map((filename) => ({ filename, changed: state.local_hashes[filename] !== state.base_hashes[filename] })), output: differences.join('\n') };
135
+ }
136
+ async function writeConflict(projectDir, entry, remote, archive) {
137
+ const directory = conflictRoot(projectDir, entry.resource_id); const baseDir = path.join(directory, 'base'); const localDir = path.join(directory, 'local'); const remoteDir = path.join(directory, 'remote');
138
+ writeFiles(baseDir, readFiles(entry.base_dir)); writeFiles(localDir, readFiles(entry.local_dir)); writeFiles(remoteDir, archiveFiles(archive));
139
+ const record = { schema_version: 1, status: 'unresolved', code: 'RESOURCE_VERSION_CONFLICT', resource_type: 'custom_services', resource_id: entry.resource_id,
140
+ base_path: relative(projectDir, baseDir), local_path: relative(projectDir, localDir), remote_path: relative(projectDir, remoteDir), worktree_local_path: entry.local_path,
141
+ expected_revision: entry.base_revision, actual_revision: remote.base_revision, expected_hash: entry.base_hash, actual_hash: remote.content_hash,
142
+ actual_etag: remote.etag, actual_updated_at: remote.updated_at, created_at: new Date().toISOString() };
143
+ atomicWrite(path.join(directory, 'conflict.json'), `${JSON.stringify(record, null, 2)}\n`); return record;
144
+ }
145
+ function serviceConflicts(projectDir, options = {}) {
146
+ const directory = path.join(projectDir, '.draftgo', 'conflicts', 'custom-services'); if (!fs.existsSync(directory)) return [];
147
+ return fs.readdirSync(directory, { withFileTypes: true }).filter((entry) => entry.isDirectory()).flatMap((entry) => {
148
+ const file = path.join(directory, entry.name, 'conflict.json'); if (!fs.existsSync(file)) return [];
149
+ const record = JSON.parse(fs.readFileSync(file, 'utf8')); return options.all || record.status === 'unresolved' ? [{ ...record, manifest_path: relative(projectDir, file) }] : [];
150
+ });
151
+ }
152
+ function showConflict(projectDir, id) {
153
+ const record = serviceConflicts(projectDir, { all: true }).find((entry) => String(entry.resource_id) === String(id));
154
+ if (!record) throw new WorktreeError('CONFLICT_NOT_FOUND', `No custom-service conflict found for ${id}.`); return record;
155
+ }
156
+ function resolveConflict(projectDir, id) {
157
+ const record = showConflict(projectDir, id); if (record.status !== 'unresolved') throw new WorktreeError('CONFLICT_ALREADY_RESOLVED', `Custom-service conflict ${id} is already resolved.`);
158
+ const manifest = loadManifest(projectDir); const entry = manifest.entries[entryKey(id)]; if (!entry) throw new WorktreeError('RESOURCE_NOT_CHECKED_OUT', `Custom service ${id} is not checked out.`);
159
+ const remoteDir = path.resolve(projectDir, record.remote_path); const remoteFiles = readFiles(remoteDir); writeFiles(entry.base_dir, remoteFiles);
160
+ entry.file_hashes = fileHashes(remoteFiles); entry.base_hash = record.actual_hash; entry.base_revision = record.actual_revision; entry.base_etag = record.actual_etag; entry.updated_at = record.actual_updated_at; entry.conflict_resolved_at = new Date().toISOString();
161
+ manifest.entries[entryKey(id)] = entry; saveManifest(projectDir, manifest);
162
+ const resolved = { ...record, status: 'resolved', resolved_at: new Date().toISOString() }; delete resolved.manifest_path;
163
+ atomicWrite(path.resolve(projectDir, record.manifest_path), `${JSON.stringify(resolved, null, 2)}\n`); return { ...resolved, manifest_path: record.manifest_path };
164
+ }
165
+ async function commit(projectDir, ids, options = {}) {
166
+ const config = options.config || loadProjectConfig(projectDir); const manifest = loadManifest(projectDir); const results = [];
167
+ const prepared = [];
168
+ for (const id of ids) {
169
+ const entry = manifest.entries[entryKey(id)]; if (!entry) throw new WorktreeError('RESOURCE_NOT_CHECKED_OUT', `Custom service ${id} is not checked out.`);
170
+ if (entry.server !== config.server) throw new WorktreeError('CHECKOUT_SERVER_MISMATCH', 'Checkout belongs to a different DraftGo server.');
171
+ const state = localState(entry); if (!state.base_valid) throw new WorktreeError('BASE_HASH_MISMATCH', `Custom service ${id} base does not match its manifest.`);
172
+ if (!state.modified) { prepared.push({ id, entry, state, unchanged: true }); continue; }
173
+ const current = await metadata(config, id, options);
174
+ if (current.content_hash !== entry.base_hash || String(current.base_revision) !== String(entry.base_revision)) {
175
+ const remoteArchive = await download(config, current, options); const conflict = await writeConflict(projectDir, entry, current, remoteArchive);
176
+ throw new WorktreeError('RESOURCE_VERSION_CONFLICT', `Custom service ${id} changed remotely; conflict materials were preserved.`, conflict);
177
+ }
178
+ prepared.push({ id, entry, state, current, archive: createArchive(state.local) });
179
+ }
180
+ for (const item of prepared) {
181
+ const { id, entry, current, archive } = item;
182
+ if (item.unchanged) { results.push({ resource_type: 'custom_services', resource_id: String(id), status: 'unchanged' }); continue; }
183
+ const url = new URL(current.commit_url, `${config.server.replace(/\/+$/, '')}/`).toString();
184
+ const response = await (options.fetch || fetch)(url, { method: 'PUT', headers: { Authorization: `Bearer ${config.token || config.sat}`, 'Content-Type': current.content_type,
185
+ 'Content-Length': String(archive.length), 'X-Content-SHA256': sha256(archive), 'If-Match': entry.base_etag }, body: archive, signal: options.signal });
186
+ let committed;
187
+ try { committed = parseEnvelope(await response.text(), response); } catch (error) {
188
+ if ([409, 412].includes(error.status)) { const latest = await metadata(config, id, options); const remoteArchive = await download(config, latest, options); error.details = await writeConflict(projectDir, entry, latest, remoteArchive); }
189
+ throw error;
190
+ }
191
+ const canonicalArchive = await download(config, committed, options); const canonicalFiles = archiveFiles(canonicalArchive);
192
+ writeFiles(entry.local_dir, canonicalFiles); writeFiles(entry.base_dir, canonicalFiles);
193
+ entry.file_hashes = fileHashes(canonicalFiles); entry.base_hash = committed.content_hash;
194
+ entry.base_revision = committed.base_revision; entry.base_etag = committed.etag; entry.updated_at = committed.updated_at; entry.updated_by = committed.updated_by; entry.committed_at = new Date().toISOString();
195
+ manifest.entries[entryKey(id)] = entry; saveManifest(projectDir, manifest);
196
+ results.push({ resource_type: 'custom_services', resource_id: String(id), status: 'committed', revision: committed.base_revision, hash: committed.content_hash });
197
+ }
198
+ return results;
199
+ }
200
+ async function inspectRemote(projectDir, options = {}) {
201
+ const config = options.config || loadProjectConfig(projectDir);
202
+ const requested = options.ids ? new Set(options.ids.map(String)) : null;
203
+ const entries = Object.values(loadManifest(projectDir).entries)
204
+ .filter((entry) => !requested || requested.has(String(entry.resource_id)));
205
+ const result = [];
206
+ for (const entry of entries) {
207
+ const state = localState(entry); const remote = await metadata(config, entry.resource_id, options); const remoteMatches = remote.content_hash === entry.base_hash && String(remote.base_revision) === String(entry.base_revision);
208
+ const localArchiveHash = checkoutHash(state.local); let status;
209
+ if (remoteMatches) status = state.modified ? 'local_modified' : 'clean';
210
+ else if (!state.modified) status = 'remote_changed';
211
+ else if (filesMatchRemote(state.local, remote)) status = 'committed_unrecorded';
212
+ else status = 'diverged';
213
+ result.push({ ...entry, state: status, remote_hash: remote.content_hash, remote_revision: remote.base_revision, remote_etag: remote.etag, local_hash: localArchiveHash });
214
+ }
215
+ return result;
216
+ }
217
+ async function validate(projectDir, id, options = {}) {
218
+ const config = options.config || loadProjectConfig(projectDir); await commit(projectDir, [id], options);
219
+ const result = await jsonRequest(config, 'POST', `scripts/${encodeURIComponent(id)}/draft/validate`, {}, options);
220
+ await checkout(projectDir, [id], { ...options, config, force: true }); return result;
221
+ }
222
+ function parseHandler(handler) {
223
+ const value = String(handler || '').trim(); if (!value) return undefined;
224
+ const parts = value.split(':');
225
+ if (parts[0] === 'route' && parts.length >= 3) return { kind: 'route', method: parts[1], path: parts.slice(2).join(':') };
226
+ if (parts[0] === 'event') return { kind: 'event', event: parts.slice(1).join(':') };
227
+ if (parts[0] === 'scheduled') return { kind: 'scheduled', name: parts.slice(1).join(':') };
228
+ return { kind: 'manual', name: value };
229
+ }
230
+ async function test(projectDir, id, input = {}, options = {}) {
231
+ const config = options.config || loadProjectConfig(projectDir); await validate(projectDir, id, options);
232
+ return jsonRequest(config, 'POST', `scripts/${encodeURIComponent(id)}/draft/execute`, {
233
+ input, selector: parseHandler(options.handler), headers: options.headers, user: options.user,
234
+ test_write: Boolean(options.testWrite), side_effect_policy: options.sideEffectPolicy || 'deny',
235
+ }, options);
236
+ }
237
+ async function publish(projectDir, ids, options = {}) {
238
+ const config = options.config || loadProjectConfig(projectDir); const results = [];
239
+ for (const id of ids) {
240
+ await validate(projectDir, id, options); results.push(await jsonRequest(config, 'POST', `scripts/${encodeURIComponent(id)}/draft/publish`, {}, options));
241
+ await checkout(projectDir, [id], { ...options, config, force: true });
242
+ }
243
+ return results;
244
+ }
245
+
246
+ module.exports = { FILES, checkout, commit, diff, validate, test, publish, inspectRemote, loadManifest, getEntry, metadata, archiveFiles, createArchive, serviceConflicts, showConflict, resolveConflict, parseHandler };
package/src/projectMap.js CHANGED
@@ -6,6 +6,7 @@ const path = require('path');
6
6
  const parse5 = require('parse5');
7
7
  const { loadManifest, absolutePath } = require('./worktree/manifest');
8
8
  const { mediaType } = require('./worktree/types');
9
+ const { validateInlineScripts } = require('./worktree/inlineScripts');
9
10
 
10
11
  function normalizeRoute(route) {
11
12
  if (!route || typeof route !== 'string') return '';
@@ -121,7 +122,7 @@ function worktreeFiles(projectDir, directory) {
121
122
  .map((entry) => path.join(root, entry.name));
122
123
  }
123
124
 
124
- function analyzeProject(projectDir) {
125
+ function analyzeProject(projectDir, options = {}) {
125
126
  const map = buildProjectMap(projectDir);
126
127
  const errors = [];
127
128
  const warnings = [];
@@ -132,7 +133,12 @@ function analyzeProject(projectDir) {
132
133
  };
133
134
  const registered = new Set();
134
135
 
135
- for (const entry of map.checkouts) {
136
+ const resourceKeys = options.resourceKeys ? new Set(options.resourceKeys) : null;
137
+ const selectedCheckouts = resourceKeys
138
+ ? map.checkouts.filter((entry) => resourceKeys.has(`${entry.resource_type}:${entry.resource_id}`))
139
+ : map.checkouts;
140
+
141
+ for (const entry of selectedCheckouts) {
136
142
  let local;
137
143
  let base;
138
144
  try {
@@ -176,18 +182,24 @@ function analyzeProject(projectDir) {
176
182
  const position = issue.line ? ` at ${issue.line}:${issue.column || 1}` : '';
177
183
  addWarning('DG-HTML-001', 'high', `${entry.resource_type} ${entry.resource_id}: ${issue.code}${position}.`);
178
184
  }
185
+ for (const issue of validateInlineScripts(source).slice(0, 20)) {
186
+ errors.push(`${entry.resource_type} ${entry.resource_id}: inline script #${issue.script} `
187
+ + `(${issue.script_type}) ${issue.message} at ${entry.local_path}:${issue.line}:${issue.column}.`);
188
+ }
179
189
  }
180
190
  }
181
191
 
182
- for (const directory of ['pages', 'navigations', 'docs']) {
183
- for (const file of worktreeFiles(projectDir, directory)) {
184
- if (!registered.has(path.resolve(file).toLowerCase())) {
185
- const relative = path.relative(projectDir, file).replace(/\\/g, '/');
186
- addWarning('DG-WORKTREE-ORPHAN', 'high', `Unregistered worktree file: ${relative}.`);
192
+ if (!resourceKeys) {
193
+ for (const directory of ['pages', 'navigations', 'docs']) {
194
+ for (const file of worktreeFiles(projectDir, directory)) {
195
+ if (!registered.has(path.resolve(file).toLowerCase())) {
196
+ const relative = path.relative(projectDir, file).replace(/\\/g, '/');
197
+ addWarning('DG-WORKTREE-ORPHAN', 'high', `Unregistered worktree file: ${relative}.`);
198
+ }
187
199
  }
188
200
  }
189
201
  }
190
- for (const page of map.pages) {
202
+ for (const page of map.pages.filter((entry) => !resourceKeys || resourceKeys.has(`pages:${entry.resource_id}`))) {
191
203
  if (!page.route || page.route === '/' || page.area === 'system') continue;
192
204
  const sources = map.routeRefs[normalizeRoute(page.route)] || [];
193
205
  if (!sources.some((source) => source.startsWith('navigations:'))) {
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+
6
+ const SCHEMA_VERSION = 1;
7
+ const RETRYABLE_RENAME_CODES = new Set(['EPERM', 'EBUSY', 'EACCES']);
8
+ const sleepBuffer = new Int32Array(new SharedArrayBuffer(4));
9
+ function draftgoRoot(projectDir) { return path.resolve(projectDir, '.draftgo'); }
10
+ function manifestPath(projectDir) { return path.join(draftgoRoot(projectDir), 'runtime-manifest.json'); }
11
+ function inside(root, target) { const relation = path.relative(root, target); return relation && !relation.startsWith('..') && !path.isAbsolute(relation); }
12
+ function load(projectDir) {
13
+ const file = manifestPath(projectDir);
14
+ if (!fs.existsSync(file)) return { schema_version: SCHEMA_VERSION, entries: [] };
15
+ const value = JSON.parse(fs.readFileSync(file, 'utf8'));
16
+ if (!value || value.schema_version !== SCHEMA_VERSION || !Array.isArray(value.entries)) throw new Error('Invalid .draftgo/runtime-manifest.json.');
17
+ return value;
18
+ }
19
+ function replaceFile(temporary, file, rename = fs.renameSync, wait = (ms) => Atomics.wait(sleepBuffer, 0, 0, ms)) {
20
+ for (let attempt = 0; ; attempt += 1) {
21
+ try { rename(temporary, file); return; } catch (error) {
22
+ if (!RETRYABLE_RENAME_CODES.has(error.code) || attempt >= 5) throw error;
23
+ wait(20 * (attempt + 1));
24
+ }
25
+ }
26
+ }
27
+ function save(projectDir, value) {
28
+ const file = manifestPath(projectDir); fs.mkdirSync(path.dirname(file), { recursive: true });
29
+ const temporary = `${file}.tmp-${process.pid}-${Date.now()}`;
30
+ fs.writeFileSync(temporary, `${JSON.stringify(value, null, 2)}\n`);
31
+ try { replaceFile(temporary, file); } catch (error) {
32
+ try { fs.rmSync(temporary, { force: true }); } catch { /* retain the original rename error */ }
33
+ throw error;
34
+ }
35
+ }
36
+ function register(projectDir, file, type, command, options = {}) {
37
+ const root = draftgoRoot(projectDir); const absolute = path.resolve(file);
38
+ if (!inside(root, absolute)) throw new Error('Managed runtime files must stay inside .draftgo.');
39
+ const relative = path.relative(root, absolute).replace(/\\/g, '/'); const value = load(projectDir);
40
+ const entry = { path: relative, type: String(type), command: String(command), created_at: new Date().toISOString(), cleanable: options.cleanable !== false };
41
+ value.entries = value.entries.filter((item) => item.path !== relative); value.entries.push(entry); save(projectDir, value); return entry;
42
+ }
43
+ function managedPath(projectDir, ...parts) { const file = path.join(draftgoRoot(projectDir), ...parts); if (!inside(draftgoRoot(projectDir), file)) throw new Error('Managed path escapes .draftgo.'); return file; }
44
+ module.exports = { SCHEMA_VERSION, draftgoRoot, manifestPath, load, save, register, managedPath, inside, replaceFile };
package/src/skill.js CHANGED
@@ -187,15 +187,6 @@ function validateRenderedSkill(assetDir) {
187
187
  }
188
188
 
189
189
  function ensureRuntime(projectDir) {
190
- const dg = paths.dgDir(projectDir);
191
- ensureDir(dg);
192
- for (const sub of ['Task', 'lessons']) {
193
- ensureDir(path.join(dg, sub));
194
- }
195
- const changelog = path.join(dg, 'changelog.md');
196
- if (!exists(changelog)) {
197
- writeText(changelog, '');
198
- }
199
190
  appendGitignoreLine(projectDir, '.draftgo/config.json');
200
191
  appendGitignoreLine(projectDir, '.draftgo/token');
201
192
  appendGitignoreLine(projectDir, '.draftgo/worktree/');
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+ const runtime = require('./runtimeFiles');
6
+
7
+ const PROTECTED_PREFIXES = ['worktree/', 'conflicts/', 'Task/', 'lessons/', 'tmp/', 'config.json', 'changelog.md', 'story.yaml', '.version', 'runtime-manifest.json'];
8
+ function workspaceHealth(projectDir) {
9
+ const root = runtime.draftgoRoot(projectDir); const registered = new Set(runtime.load(projectDir).entries.map((entry) => String(entry.path).replace(/\\/g, '/')));
10
+ const summary = { managed_files: 0, unknown_files: 0, temporary_files: 0, artifact_files: 0, total_bytes: 0, reclaimable_bytes: 0, warnings: [] };
11
+ if (!fs.existsSync(root)) return summary;
12
+ const visit = (directory) => {
13
+ for (const item of fs.readdirSync(directory, { withFileTypes: true })) {
14
+ const absolute = path.join(directory, item.name); const relative = path.relative(root, absolute).replace(/\\/g, '/');
15
+ if (item.isDirectory()) {
16
+ if ((/^(.+\/)?(build|dist|node_modules|tmp-build|build-)/i.test(relative)) && directorySize(absolute) > 50 * 1024 * 1024) summary.warnings.push({ code: 'DG-WORKSPACE-LARGE-BUILD', path: relative });
17
+ visit(absolute); continue;
18
+ }
19
+ if (!item.isFile()) continue;
20
+ const size = fs.statSync(absolute).size; summary.total_bytes += size;
21
+ const managed = registered.has(relative) || PROTECTED_PREFIXES.some((prefix) => relative === prefix || relative.startsWith(prefix));
22
+ if (managed) summary.managed_files += 1; else summary.unknown_files += 1;
23
+ if (relative.startsWith('tmp/')) summary.temporary_files += 1;
24
+ if (relative.startsWith('artifacts/')) summary.artifact_files += 1;
25
+ if (registered.has(relative) || relative.startsWith('tmp/')) summary.reclaimable_bytes += size;
26
+ if (!managed && (/\.exe$/i.test(relative) || (/^[^/]+\.(go|json)$/i.test(relative) && !['config.json', 'runtime-manifest.json'].includes(relative)))) summary.warnings.push({ code: 'DG-WORKSPACE-UNKNOWN-FILE', path: relative, size });
27
+ if (relative.startsWith('artifacts/') && !registered.has(relative)) summary.warnings.push({ code: 'DG-WORKSPACE-UNREGISTERED-ARTIFACT', path: relative, size });
28
+ }
29
+ };
30
+ visit(root); return summary;
31
+ }
32
+ function directorySize(directory) { let total = 0; const visit = (current) => { for (const item of fs.readdirSync(current, { withFileTypes: true })) { const absolute = path.join(current, item.name); if (item.isDirectory()) visit(absolute); else if (item.isFile()) total += fs.statSync(absolute).size; if (total > 50 * 1024 * 1024) return; } }; visit(directory); return total; }
33
+ module.exports = { workspaceHealth };
@@ -0,0 +1,99 @@
1
+ 'use strict';
2
+
3
+ const vm = require('vm');
4
+ const { spawnSync } = require('child_process');
5
+ const parse5 = require('parse5');
6
+
7
+ const DATA_SCRIPT_TYPES = new Set([
8
+ 'application/json',
9
+ 'application/ld+json',
10
+ 'importmap',
11
+ 'speculationrules',
12
+ ]);
13
+
14
+ function attribute(node, name) {
15
+ const item = (node.attrs || []).find((candidate) => candidate.name.toLowerCase() === name);
16
+ return item ? item.value : '';
17
+ }
18
+
19
+ function scriptSource(node) {
20
+ return (node.childNodes || [])
21
+ .filter((child) => child.nodeName === '#text')
22
+ .map((child) => child.value || '')
23
+ .join('');
24
+ }
25
+
26
+ function collectInlineScripts(html) {
27
+ const document = parse5.parse(String(html || ''), { sourceCodeLocationInfo: true });
28
+ const scripts = [];
29
+ const visit = (node) => {
30
+ if (node.tagName === 'script' && !attribute(node, 'src')) {
31
+ const type = attribute(node, 'type').trim().toLowerCase();
32
+ if (!DATA_SCRIPT_TYPES.has(type) && (!type || type === 'module' || /(?:java|ecma)script/.test(type))) {
33
+ const location = node.sourceCodeLocation || {};
34
+ const startTag = location.startTag || {};
35
+ scripts.push({
36
+ index: scripts.length + 1,
37
+ type: type === 'module' ? 'module' : 'classic',
38
+ source: scriptSource(node),
39
+ startLine: startTag.endLine || location.startLine || 1,
40
+ startColumn: startTag.endCol || 1,
41
+ });
42
+ }
43
+ }
44
+ for (const child of node.childNodes || []) visit(child);
45
+ if (node.content) visit(node.content);
46
+ };
47
+ visit(document);
48
+ return scripts;
49
+ }
50
+
51
+ function syntaxPosition(message) {
52
+ const line = String(message || '').match(/(?:\[stdin\]|inline-script):(\d+)(?::(\d+))?/);
53
+ if (line) return { line: Number(line[1]), column: Number(line[2] || 1) };
54
+ const caretLines = String(message || '').split(/\r?\n/);
55
+ const caretIndex = caretLines.findIndex((value) => /^\s*\^/.test(value));
56
+ return { line: null, column: caretIndex > 0 ? caretLines[caretIndex].indexOf('^') + 1 : null };
57
+ }
58
+
59
+ function conciseMessage(error) {
60
+ const lines = String(error && (error.stderr || error.message) || error || '').split(/\r?\n/);
61
+ return lines.find((line) => /^SyntaxError:/.test(line))
62
+ || lines.find((line) => line.trim() && !/^\s*at\s/.test(line))
63
+ || 'Invalid JavaScript syntax';
64
+ }
65
+
66
+ function validateInlineScripts(html) {
67
+ const issues = [];
68
+ for (const script of collectInlineScripts(html)) {
69
+ if (!script.source.trim()) continue;
70
+ let failure = null;
71
+ if (script.type === 'module') {
72
+ const result = spawnSync(process.execPath, ['--check', '--input-type=module'], {
73
+ input: script.source,
74
+ encoding: 'utf8',
75
+ windowsHide: true,
76
+ });
77
+ if (result.error || result.status !== 0) failure = { message: result.stderr || result.stdout || result.error.message };
78
+ } else {
79
+ try { new vm.Script(script.source, { filename: 'inline-script' }); }
80
+ catch (error) { failure = error; }
81
+ }
82
+ if (!failure) continue;
83
+ const diagnostic = failure.stack || failure.message;
84
+ const position = syntaxPosition(diagnostic);
85
+ issues.push({
86
+ code: 'DG-JS-001',
87
+ script: script.index,
88
+ script_type: script.type,
89
+ line: position.line ? script.startLine + position.line - 1 : script.startLine,
90
+ column: position.column
91
+ ? position.column + (position.line === 1 ? script.startColumn - 1 : 0)
92
+ : script.startColumn,
93
+ message: conciseMessage(failure),
94
+ });
95
+ }
96
+ return issues;
97
+ }
98
+
99
+ module.exports = { DATA_SCRIPT_TYPES, collectInlineScripts, validateInlineScripts };
@@ -101,7 +101,7 @@ async function inspectRemoteCheckouts(projectDir, options = {}) {
101
101
  const session = options.backend && typeof options.backend.resolveMetadata === 'function'
102
102
  ? { client: options.client || {}, tools: options.tools || [] }
103
103
  : await openMetadataSession(config, options);
104
- const entries = Object.values(manifest.entries);
104
+ const entries = options.entries || Object.values(manifest.entries);
105
105
  return Promise.all(entries.map(async (entry) => {
106
106
  const remote = await backend.resolveMetadata(config, entry.resource_type, entry.resource_id, {
107
107
  ...options,