gmc-openspec 1.1.0 → 1.4.2
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.
- package/README.md +2 -2
- package/bin/openspec.js +3 -1
- package/dist/cli/index.d.ts +4 -1
- package/dist/cli/index.js +36 -2
- package/dist/commands/config.js +4 -4
- package/dist/commands/context-store.d.ts +3 -0
- package/dist/commands/context-store.js +475 -0
- package/dist/commands/initiative.d.ts +13 -0
- package/dist/commands/initiative.js +318 -0
- package/dist/commands/workflow/index.d.ts +2 -0
- package/dist/commands/workflow/index.js +1 -0
- package/dist/commands/workflow/initiative-link.d.ts +24 -0
- package/dist/commands/workflow/initiative-link.js +47 -0
- package/dist/commands/workflow/instructions.js +10 -2
- package/dist/commands/workflow/new-change.d.ts +4 -0
- package/dist/commands/workflow/new-change.js +72 -23
- package/dist/commands/workflow/set-change.d.ts +13 -0
- package/dist/commands/workflow/set-change.js +87 -0
- package/dist/commands/workflow/shared.d.ts +2 -0
- package/dist/commands/workflow/status.js +3 -0
- package/dist/commands/workspace/context-status.d.ts +4 -0
- package/dist/commands/workspace/context-status.js +59 -0
- package/dist/commands/workspace/open-target-selection.d.ts +13 -0
- package/dist/commands/workspace/open-target-selection.js +146 -0
- package/dist/commands/workspace/open-view.d.ts +62 -0
- package/dist/commands/workspace/open-view.js +249 -0
- package/dist/commands/workspace/open.d.ts +16 -8
- package/dist/commands/workspace/open.js +40 -14
- package/dist/commands/workspace/opener-selection.d.ts +11 -0
- package/dist/commands/workspace/opener-selection.js +98 -0
- package/dist/commands/workspace/operations.d.ts +14 -8
- package/dist/commands/workspace/operations.js +228 -160
- package/dist/commands/workspace/prompt-theme.d.ts +29 -0
- package/dist/commands/workspace/prompt-theme.js +24 -0
- package/dist/commands/workspace/registration.d.ts +13 -0
- package/dist/commands/workspace/registration.js +84 -0
- package/dist/commands/workspace/selection.d.ts +3 -0
- package/dist/commands/workspace/selection.js +42 -40
- package/dist/commands/workspace/setup-prompts.d.ts +13 -0
- package/dist/commands/workspace/setup-prompts.js +121 -0
- package/dist/commands/workspace/types.d.ts +15 -0
- package/dist/commands/workspace.js +59 -340
- package/dist/core/artifact-graph/index.d.ts +2 -1
- package/dist/core/artifact-graph/instruction-loader.d.ts +10 -23
- package/dist/core/artifact-graph/instruction-loader.js +28 -89
- package/dist/core/artifact-graph/types.d.ts +0 -7
- package/dist/core/artifact-graph/types.js +0 -19
- package/dist/core/change-metadata/index.d.ts +2 -0
- package/dist/core/change-metadata/index.js +2 -0
- package/dist/core/change-metadata/schema.d.ts +18 -0
- package/dist/core/change-metadata/schema.js +28 -0
- package/dist/core/change-status-policy.d.ts +50 -0
- package/dist/core/change-status-policy.js +70 -0
- package/dist/core/collections/index.d.ts +3 -0
- package/dist/core/collections/index.js +3 -0
- package/dist/core/collections/initiatives/collection.d.ts +4 -0
- package/dist/core/collections/initiatives/collection.js +17 -0
- package/dist/core/collections/initiatives/index.d.ts +6 -0
- package/dist/core/collections/initiatives/index.js +6 -0
- package/dist/core/collections/initiatives/operations.d.ts +49 -0
- package/dist/core/collections/initiatives/operations.js +175 -0
- package/dist/core/collections/initiatives/resolution.d.ts +87 -0
- package/dist/core/collections/initiatives/resolution.js +374 -0
- package/dist/core/collections/initiatives/schema.d.ts +41 -0
- package/dist/core/collections/initiatives/schema.js +134 -0
- package/dist/core/collections/initiatives/templates.d.ts +12 -0
- package/dist/core/collections/initiatives/templates.js +90 -0
- package/dist/core/collections/runtime.d.ts +46 -0
- package/dist/core/collections/runtime.js +194 -0
- package/dist/core/completions/command-registry.d.ts +1 -5
- package/dist/core/completions/command-registry.js +475 -70
- package/dist/core/completions/shared-flags.d.ts +12 -0
- package/dist/core/completions/shared-flags.js +28 -0
- package/dist/core/config.js +2 -1
- package/dist/core/context-store/binding.d.ts +53 -0
- package/dist/core/context-store/binding.js +197 -0
- package/dist/core/context-store/errors.d.ts +20 -0
- package/dist/core/context-store/errors.js +22 -0
- package/dist/core/context-store/foundation.d.ts +55 -0
- package/dist/core/context-store/foundation.js +321 -0
- package/dist/core/context-store/index.d.ts +6 -0
- package/dist/core/context-store/index.js +6 -0
- package/dist/core/context-store/operations.d.ts +85 -0
- package/dist/core/context-store/operations.js +528 -0
- package/dist/core/context-store/registry.d.ts +45 -0
- package/dist/core/context-store/registry.js +229 -0
- package/dist/core/index.d.ts +2 -0
- package/dist/core/index.js +2 -0
- package/dist/core/jira/templates.js +14 -5
- package/dist/core/planning-home.js +5 -21
- package/dist/core/validation/validator.d.ts +11 -0
- package/dist/core/validation/validator.js +19 -2
- package/dist/core/workspace/foundation.d.ts +28 -48
- package/dist/core/workspace/foundation.js +130 -214
- package/dist/core/workspace/index.d.ts +2 -0
- package/dist/core/workspace/index.js +2 -0
- package/dist/core/workspace/legacy-state.d.ts +28 -0
- package/dist/core/workspace/legacy-state.js +200 -0
- package/dist/core/workspace/open-surface.d.ts +29 -8
- package/dist/core/workspace/open-surface.js +122 -44
- package/dist/core/workspace/openers.js +11 -6
- package/dist/core/workspace/registry.d.ts +24 -0
- package/dist/core/workspace/registry.js +146 -0
- package/dist/core/workspace/skills.d.ts +4 -2
- package/dist/core/workspace/skills.js +2 -2
- package/dist/core/workspace/state-io.d.ts +10 -0
- package/dist/core/workspace/state-io.js +119 -0
- package/dist/utils/change-metadata.d.ts +5 -2
- package/dist/utils/change-metadata.js +6 -12
- package/dist/utils/change-utils.d.ts +2 -2
- package/package.json +1 -1
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import * as nodeFs from 'node:fs';
|
|
3
|
+
import * as os from 'node:os';
|
|
4
|
+
import * as path from 'node:path';
|
|
5
|
+
import { promisify } from 'node:util';
|
|
6
|
+
import { FileSystemUtils } from '../../utils/file-system.js';
|
|
7
|
+
import { getDefaultContextStoreRoot, getContextStoreMetadataPath, getContextStoreRegistryPath, listContextStoreRegistryEntries, readContextStoreRegistryState, readOptionalContextStoreMetadataState, resolveGitContextStoreBackendConfig, validateContextStoreId, } from './foundation.js';
|
|
8
|
+
import { ContextStoreError, makeContextStoreDiagnostic } from './errors.js';
|
|
9
|
+
import { getStoreRootForBackend, assertNoRegisteredStoreConflict, commitContextStoreRegistration, getRegisteredContextStore, listRegisteredContextStores, unregisterContextStoreRegistration, } from './registry.js';
|
|
10
|
+
const fs = nodeFs.promises;
|
|
11
|
+
const execFileAsync = promisify(execFile);
|
|
12
|
+
async function pathKind(targetPath) {
|
|
13
|
+
try {
|
|
14
|
+
const stat = await fs.stat(targetPath);
|
|
15
|
+
if (stat.isDirectory())
|
|
16
|
+
return 'directory';
|
|
17
|
+
if (stat.isFile())
|
|
18
|
+
return 'file';
|
|
19
|
+
return 'other';
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
if (typeof error === 'object' &&
|
|
23
|
+
error !== null &&
|
|
24
|
+
'code' in error &&
|
|
25
|
+
error.code === 'ENOENT') {
|
|
26
|
+
return 'missing';
|
|
27
|
+
}
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async function isDirectoryEmpty(directory) {
|
|
32
|
+
return (await fs.readdir(directory)).length === 0;
|
|
33
|
+
}
|
|
34
|
+
async function readStoreMetadataForOperation(storeRoot) {
|
|
35
|
+
try {
|
|
36
|
+
return await readOptionalContextStoreMetadataState(storeRoot);
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
throw new ContextStoreError(error instanceof Error ? error.message : String(error), 'invalid_context_store_metadata', {
|
|
40
|
+
target: 'context_store.metadata',
|
|
41
|
+
fix: `Repair ${getContextStoreMetadataPath(storeRoot)}.`,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
async function isGitRepositoryAtRoot(storeRoot) {
|
|
46
|
+
const gitPath = path.join(storeRoot, '.git');
|
|
47
|
+
const kind = await pathKind(gitPath);
|
|
48
|
+
return kind === 'directory' || kind === 'file';
|
|
49
|
+
}
|
|
50
|
+
async function nearestExistingDirectory(targetPath) {
|
|
51
|
+
let current = path.resolve(targetPath);
|
|
52
|
+
while (true) {
|
|
53
|
+
const kind = await pathKind(current);
|
|
54
|
+
if (kind === 'directory')
|
|
55
|
+
return current;
|
|
56
|
+
if (kind !== 'missing')
|
|
57
|
+
return null;
|
|
58
|
+
const parent = path.dirname(current);
|
|
59
|
+
if (parent === current)
|
|
60
|
+
return null;
|
|
61
|
+
current = parent;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async function findContainingGitRepositoryRoot(storeRoot) {
|
|
65
|
+
const resolvedStoreRoot = path.resolve(storeRoot);
|
|
66
|
+
const nearestParent = await nearestExistingDirectory(path.dirname(resolvedStoreRoot));
|
|
67
|
+
if (!nearestParent)
|
|
68
|
+
return null;
|
|
69
|
+
const comparableStoreRoot = path.resolve(FileSystemUtils.canonicalizeExistingPath(nearestParent), path.relative(nearestParent, resolvedStoreRoot));
|
|
70
|
+
const gitRootContainsStore = (gitRoot) => {
|
|
71
|
+
const normalizedGitRoot = FileSystemUtils.canonicalizeExistingPath(gitRoot);
|
|
72
|
+
const relative = path.relative(normalizedGitRoot, comparableStoreRoot);
|
|
73
|
+
return relative.length > 0 && !relative.startsWith('..') && !path.isAbsolute(relative)
|
|
74
|
+
? normalizedGitRoot
|
|
75
|
+
: null;
|
|
76
|
+
};
|
|
77
|
+
try {
|
|
78
|
+
const { stdout } = await execFileAsync('git', [
|
|
79
|
+
'-C',
|
|
80
|
+
nearestParent,
|
|
81
|
+
'rev-parse',
|
|
82
|
+
'--show-toplevel',
|
|
83
|
+
]);
|
|
84
|
+
return gitRootContainsStore(stdout.trim());
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
let current = nearestParent;
|
|
88
|
+
while (true) {
|
|
89
|
+
if (await isGitRepositoryAtRoot(current)) {
|
|
90
|
+
return gitRootContainsStore(current);
|
|
91
|
+
}
|
|
92
|
+
const parent = path.dirname(current);
|
|
93
|
+
if (parent === current)
|
|
94
|
+
return null;
|
|
95
|
+
current = parent;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
async function assertSetupPathIsNotNestedInGitRepo(storeRoot, options) {
|
|
100
|
+
if (options.allowInsideGitRepository)
|
|
101
|
+
return;
|
|
102
|
+
const containingGitRoot = await findContainingGitRepositoryRoot(storeRoot);
|
|
103
|
+
if (!containingGitRoot)
|
|
104
|
+
return;
|
|
105
|
+
throw new ContextStoreError(`Context store setup path is inside another Git repository: ${containingGitRoot}`, 'context_store_setup_inside_git_repo', {
|
|
106
|
+
target: 'context_store.root',
|
|
107
|
+
fix: 'Choose the managed OpenSpec location, choose a path outside that Git repository, or rerun setup interactively to confirm this location.',
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
async function initGitRepository(storeRoot) {
|
|
111
|
+
if (await isGitRepositoryAtRoot(storeRoot)) {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
await execFileAsync('git', ['init'], { cwd: storeRoot });
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
throw new ContextStoreError(`Failed to initialize Git repository: ${error instanceof Error ? error.message : String(error)}`, 'context_store_git_init_failed', {
|
|
119
|
+
target: 'context_store.git',
|
|
120
|
+
fix: 'Install Git or rerun setup with --no-init-git.',
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
function expandUserPath(inputPath) {
|
|
126
|
+
const trimmed = inputPath.trim();
|
|
127
|
+
if (trimmed === '~')
|
|
128
|
+
return os.homedir();
|
|
129
|
+
if (trimmed.startsWith('~/') || trimmed.startsWith('~\\')) {
|
|
130
|
+
return path.join(os.homedir(), trimmed.slice(2));
|
|
131
|
+
}
|
|
132
|
+
return trimmed;
|
|
133
|
+
}
|
|
134
|
+
function resolveSetupRoot(id, inputPath) {
|
|
135
|
+
if (inputPath !== undefined && inputPath.trim().length === 0) {
|
|
136
|
+
throw new ContextStoreError('Pass a non-empty --path value.', 'context_store_path_required', {
|
|
137
|
+
target: 'context_store.root',
|
|
138
|
+
fix: `openspec context-store setup ${id} --path /path/to/context-store`,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
if (inputPath !== undefined) {
|
|
142
|
+
return path.resolve(expandUserPath(inputPath));
|
|
143
|
+
}
|
|
144
|
+
return getDefaultContextStoreRoot(id);
|
|
145
|
+
}
|
|
146
|
+
function resolveRegisterRoot(inputPath) {
|
|
147
|
+
if (inputPath === undefined || inputPath.trim().length === 0) {
|
|
148
|
+
throw new ContextStoreError('Pass a context store path.', 'context_store_path_required', {
|
|
149
|
+
target: 'context_store.root',
|
|
150
|
+
fix: 'openspec context-store register /path/to/context-store',
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
return path.resolve(expandUserPath(inputPath));
|
|
154
|
+
}
|
|
155
|
+
function inferStoreIdFromPath(storeRoot) {
|
|
156
|
+
return validateContextStoreId(path.basename(storeRoot));
|
|
157
|
+
}
|
|
158
|
+
function mutationPayload(id, storeRoot, git, createdFiles) {
|
|
159
|
+
return {
|
|
160
|
+
store: {
|
|
161
|
+
id,
|
|
162
|
+
root: storeRoot,
|
|
163
|
+
metadataPath: getContextStoreMetadataPath(storeRoot),
|
|
164
|
+
},
|
|
165
|
+
registryCommit: {
|
|
166
|
+
path: getContextStoreRegistryPath(),
|
|
167
|
+
},
|
|
168
|
+
git: {
|
|
169
|
+
isRepository: git.isRepository,
|
|
170
|
+
initialized: git.initialized,
|
|
171
|
+
},
|
|
172
|
+
createdArtifacts: createdFiles,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
async function prepareSetupPlan(input) {
|
|
176
|
+
const id = validateContextStoreId(input.id ?? '');
|
|
177
|
+
const storeRoot = resolveSetupRoot(id, input.path);
|
|
178
|
+
const kind = await pathKind(storeRoot);
|
|
179
|
+
if (kind === 'file' || kind === 'other') {
|
|
180
|
+
throw new ContextStoreError(`Context store setup path is not a directory: ${storeRoot}`, 'context_store_setup_path_not_directory', {
|
|
181
|
+
target: 'context_store.root',
|
|
182
|
+
fix: 'Choose an empty directory or omit --path to use the managed OpenSpec context-store location.',
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
// Context stores may be Git-backed, but creating one inside an implementation
|
|
186
|
+
// repo is almost always an accidental nested-repo setup.
|
|
187
|
+
await assertSetupPathIsNotNestedInGitRepo(storeRoot, {
|
|
188
|
+
allowInsideGitRepository: input.allowInsideGitRepository,
|
|
189
|
+
});
|
|
190
|
+
let metadata = null;
|
|
191
|
+
let backend;
|
|
192
|
+
if (kind === 'directory') {
|
|
193
|
+
metadata = await readStoreMetadataForOperation(storeRoot);
|
|
194
|
+
if (metadata) {
|
|
195
|
+
if (metadata.id !== id) {
|
|
196
|
+
throw new ContextStoreError(`Context store metadata id '${metadata.id}' does not match requested id '${id}'.`, 'context_store_metadata_id_mismatch', {
|
|
197
|
+
target: 'context_store.metadata',
|
|
198
|
+
fix: `Use id '${metadata.id}' or choose a different setup path.`,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
else if (!(await isDirectoryEmpty(storeRoot))) {
|
|
203
|
+
throw new ContextStoreError('Context store setup does not support initializing a non-empty folder yet.', 'context_store_setup_non_empty_directory', {
|
|
204
|
+
target: 'context_store.root',
|
|
205
|
+
fix: 'Create an empty folder or use context-store register for an existing context store.',
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
backend = await resolveGitContextStoreBackendConfig({ localPath: storeRoot });
|
|
209
|
+
}
|
|
210
|
+
const registry = await readContextStoreRegistryState();
|
|
211
|
+
const conflictBackend = backend ?? {
|
|
212
|
+
type: 'git',
|
|
213
|
+
local_path: FileSystemUtils.canonicalizeExistingPath(storeRoot),
|
|
214
|
+
};
|
|
215
|
+
assertNoRegisteredStoreConflict(registry, id, conflictBackend);
|
|
216
|
+
return {
|
|
217
|
+
id,
|
|
218
|
+
storeRoot,
|
|
219
|
+
kind,
|
|
220
|
+
registry,
|
|
221
|
+
...(backend ? { backend } : {}),
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
export async function prepareContextStoreSetup(input) {
|
|
225
|
+
const plan = await prepareSetupPlan(input);
|
|
226
|
+
return {
|
|
227
|
+
id: plan.id,
|
|
228
|
+
root: plan.storeRoot,
|
|
229
|
+
rootKind: plan.kind,
|
|
230
|
+
registry: plan.registry,
|
|
231
|
+
...(plan.backend ? { backend: plan.backend } : {}),
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
export async function setupPreparedContextStore(prepared, input = {}) {
|
|
235
|
+
const plan = {
|
|
236
|
+
id: prepared.id,
|
|
237
|
+
storeRoot: prepared.root,
|
|
238
|
+
kind: prepared.rootKind,
|
|
239
|
+
registry: prepared.registry,
|
|
240
|
+
...(prepared.backend ? { backend: prepared.backend } : {}),
|
|
241
|
+
};
|
|
242
|
+
const { id, storeRoot, kind, registry } = plan;
|
|
243
|
+
let { backend } = plan;
|
|
244
|
+
const createdFiles = [];
|
|
245
|
+
const initGit = input.initGit ?? false;
|
|
246
|
+
if (kind === 'missing') {
|
|
247
|
+
await fs.mkdir(storeRoot, { recursive: true });
|
|
248
|
+
}
|
|
249
|
+
try {
|
|
250
|
+
backend ??= await resolveGitContextStoreBackendConfig({ localPath: storeRoot });
|
|
251
|
+
assertNoRegisteredStoreConflict(registry, id, backend);
|
|
252
|
+
const gitInitialized = initGit ? await initGitRepository(storeRoot) : false;
|
|
253
|
+
const registered = await commitContextStoreRegistration({
|
|
254
|
+
id,
|
|
255
|
+
backend,
|
|
256
|
+
writeMetadataIfMissing: true,
|
|
257
|
+
});
|
|
258
|
+
if (registered.metadataCreated) {
|
|
259
|
+
createdFiles.push('.openspec-store/store.yaml');
|
|
260
|
+
}
|
|
261
|
+
const isRepository = await isGitRepositoryAtRoot(registered.storeRoot);
|
|
262
|
+
return mutationPayload(id, registered.storeRoot, {
|
|
263
|
+
isRepository,
|
|
264
|
+
initialized: gitInitialized,
|
|
265
|
+
}, createdFiles);
|
|
266
|
+
}
|
|
267
|
+
catch (error) {
|
|
268
|
+
if (kind === 'missing') {
|
|
269
|
+
await fs.rm(storeRoot, { recursive: true, force: true });
|
|
270
|
+
}
|
|
271
|
+
throw error;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
export async function setupContextStore(input) {
|
|
275
|
+
return setupPreparedContextStore(await prepareContextStoreSetup(input), {
|
|
276
|
+
initGit: input.initGit,
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
export async function registerExistingContextStore(input) {
|
|
280
|
+
const storeRoot = resolveRegisterRoot(input.path);
|
|
281
|
+
const kind = await pathKind(storeRoot);
|
|
282
|
+
if (kind === 'missing') {
|
|
283
|
+
throw new ContextStoreError(`Context store path does not exist: ${storeRoot}`, 'context_store_path_missing', {
|
|
284
|
+
target: 'context_store.root',
|
|
285
|
+
fix: 'Clone or create the context store folder before registering it.',
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
if (kind !== 'directory') {
|
|
289
|
+
throw new ContextStoreError(`Context store path is not a directory: ${storeRoot}`, 'context_store_path_not_directory', {
|
|
290
|
+
target: 'context_store.root',
|
|
291
|
+
fix: 'Pass an existing context store directory.',
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
const metadata = await readStoreMetadataForOperation(storeRoot);
|
|
295
|
+
const explicitId = input.id !== undefined ? validateContextStoreId(input.id) : undefined;
|
|
296
|
+
if (metadata && explicitId !== undefined && metadata.id !== explicitId) {
|
|
297
|
+
throw new ContextStoreError(`Context store metadata id '${metadata.id}' does not match --id '${explicitId}'.`, 'context_store_metadata_id_mismatch', {
|
|
298
|
+
target: 'context_store.id',
|
|
299
|
+
fix: `Use --id ${metadata.id} or register a different folder.`,
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
const id = metadata?.id ?? explicitId ?? inferStoreIdFromPath(storeRoot);
|
|
303
|
+
const backend = await resolveGitContextStoreBackendConfig({ localPath: storeRoot });
|
|
304
|
+
const registry = await readContextStoreRegistryState();
|
|
305
|
+
assertNoRegisteredStoreConflict(registry, id, backend);
|
|
306
|
+
const createdFiles = [];
|
|
307
|
+
const registered = await commitContextStoreRegistration({
|
|
308
|
+
id,
|
|
309
|
+
backend,
|
|
310
|
+
writeMetadataIfMissing: true,
|
|
311
|
+
});
|
|
312
|
+
if (registered.metadataCreated) {
|
|
313
|
+
createdFiles.push('.openspec-store/store.yaml');
|
|
314
|
+
}
|
|
315
|
+
return mutationPayload(id, registered.storeRoot, {
|
|
316
|
+
isRepository: await isGitRepositoryAtRoot(registered.storeRoot),
|
|
317
|
+
initialized: false,
|
|
318
|
+
}, createdFiles);
|
|
319
|
+
}
|
|
320
|
+
function cleanupStoreOutput(id, storeRoot) {
|
|
321
|
+
return {
|
|
322
|
+
id,
|
|
323
|
+
root: storeRoot,
|
|
324
|
+
metadataPath: getContextStoreMetadataPath(storeRoot),
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
export async function prepareContextStoreCleanup(input) {
|
|
328
|
+
const id = validateContextStoreId(input.id);
|
|
329
|
+
const entry = await getRegisteredContextStore({
|
|
330
|
+
id,
|
|
331
|
+
globalDataDir: input.globalDataDir,
|
|
332
|
+
});
|
|
333
|
+
return {
|
|
334
|
+
...cleanupStoreOutput(entry.id, entry.storeRoot),
|
|
335
|
+
backend: entry.backend,
|
|
336
|
+
...(input.globalDataDir ? { globalDataDir: input.globalDataDir } : {}),
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
export async function unregisterContextStore(input) {
|
|
340
|
+
const target = await prepareContextStoreCleanup(input);
|
|
341
|
+
const removed = await unregisterContextStoreRegistration({
|
|
342
|
+
id: target.id,
|
|
343
|
+
expectedBackend: target.backend,
|
|
344
|
+
globalDataDir: target.globalDataDir,
|
|
345
|
+
});
|
|
346
|
+
return {
|
|
347
|
+
store: cleanupStoreOutput(removed.id, removed.storeRoot),
|
|
348
|
+
registryCommit: {
|
|
349
|
+
path: getContextStoreRegistryPath({ globalDataDir: target.globalDataDir }),
|
|
350
|
+
removed: true,
|
|
351
|
+
},
|
|
352
|
+
files: {
|
|
353
|
+
deleted: false,
|
|
354
|
+
leftOnDisk: removed.storeRoot,
|
|
355
|
+
},
|
|
356
|
+
diagnostics: [],
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
async function assertSafeToDeleteContextStoreRoot(storeRoot, id) {
|
|
360
|
+
const kind = await pathKind(storeRoot);
|
|
361
|
+
if (kind === 'missing') {
|
|
362
|
+
return { exists: false };
|
|
363
|
+
}
|
|
364
|
+
if (kind !== 'directory') {
|
|
365
|
+
throw new ContextStoreError(`Context store path is not a directory: ${storeRoot}`, 'context_store_remove_path_not_directory', {
|
|
366
|
+
target: 'context_store.root',
|
|
367
|
+
fix: 'Run context-store unregister if you only want to forget this local registry entry.',
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
const metadata = await readStoreMetadataForOperation(storeRoot);
|
|
371
|
+
if (!metadata) {
|
|
372
|
+
throw new ContextStoreError('Context store remove refuses to delete a folder without context-store metadata.', 'context_store_remove_metadata_missing', {
|
|
373
|
+
target: 'context_store.metadata',
|
|
374
|
+
fix: 'Run context-store unregister if you only want to forget this local registry entry.',
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
if (metadata.id !== id) {
|
|
378
|
+
throw new ContextStoreError(`Context store metadata id '${metadata.id}' does not match requested id '${id}'.`, 'context_store_metadata_id_mismatch', {
|
|
379
|
+
target: 'context_store.metadata',
|
|
380
|
+
fix: 'Repair the registry or run context-store unregister instead of deleting this folder.',
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
return { exists: true };
|
|
384
|
+
}
|
|
385
|
+
export async function removeContextStore(target) {
|
|
386
|
+
const id = validateContextStoreId(target.id);
|
|
387
|
+
const diagnostics = [];
|
|
388
|
+
let deleted = false;
|
|
389
|
+
const removed = await unregisterContextStoreRegistration({
|
|
390
|
+
id,
|
|
391
|
+
expectedBackend: target.backend,
|
|
392
|
+
globalDataDir: target.globalDataDir,
|
|
393
|
+
beforeCommit: async (entry) => {
|
|
394
|
+
const safeTarget = await assertSafeToDeleteContextStoreRoot(entry.storeRoot, id);
|
|
395
|
+
if (!safeTarget.exists) {
|
|
396
|
+
diagnostics.push(makeContextStoreDiagnostic('warning', 'context_store_root_missing', 'Context store files were already missing.', {
|
|
397
|
+
target: 'context_store.root',
|
|
398
|
+
}));
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
await fs.rm(entry.storeRoot, { recursive: true, force: true });
|
|
402
|
+
deleted = true;
|
|
403
|
+
},
|
|
404
|
+
});
|
|
405
|
+
return {
|
|
406
|
+
store: cleanupStoreOutput(removed.id, removed.storeRoot),
|
|
407
|
+
registryCommit: {
|
|
408
|
+
path: getContextStoreRegistryPath({ globalDataDir: target.globalDataDir }),
|
|
409
|
+
removed: true,
|
|
410
|
+
},
|
|
411
|
+
files: {
|
|
412
|
+
deleted,
|
|
413
|
+
...(deleted ? { deletedPath: removed.storeRoot } : {}),
|
|
414
|
+
},
|
|
415
|
+
diagnostics,
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
export async function listContextStores() {
|
|
419
|
+
const entries = await listRegisteredContextStores();
|
|
420
|
+
return {
|
|
421
|
+
stores: entries.map((entry) => ({
|
|
422
|
+
id: entry.id,
|
|
423
|
+
root: entry.storeRoot,
|
|
424
|
+
})),
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
function doctorStatusForError(error, code, target, fix) {
|
|
428
|
+
if (error instanceof ContextStoreError) {
|
|
429
|
+
return error.diagnostic;
|
|
430
|
+
}
|
|
431
|
+
return makeContextStoreDiagnostic('error', code, error instanceof Error ? error.message : String(error), {
|
|
432
|
+
target,
|
|
433
|
+
...(fix ? { fix } : {}),
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
async function inspectContextStore(entry) {
|
|
437
|
+
const root = getStoreRootForBackend(entry.backend);
|
|
438
|
+
const metadataPath = getContextStoreMetadataPath(root);
|
|
439
|
+
const diagnostics = [];
|
|
440
|
+
const kind = await pathKind(root);
|
|
441
|
+
let metadata = {
|
|
442
|
+
present: null,
|
|
443
|
+
valid: null,
|
|
444
|
+
};
|
|
445
|
+
let git = {
|
|
446
|
+
isRepository: null,
|
|
447
|
+
};
|
|
448
|
+
if (kind === 'missing') {
|
|
449
|
+
diagnostics.push(makeContextStoreDiagnostic('error', 'context_store_root_missing', 'Context store location does not exist.', {
|
|
450
|
+
target: 'context_store.root',
|
|
451
|
+
fix: `Run openspec context-store register /path/to/${entry.id} --id ${entry.id}.`,
|
|
452
|
+
}));
|
|
453
|
+
}
|
|
454
|
+
else if (kind !== 'directory') {
|
|
455
|
+
diagnostics.push(makeContextStoreDiagnostic('error', 'context_store_root_not_directory', 'Context store location is not a directory.', {
|
|
456
|
+
target: 'context_store.root',
|
|
457
|
+
fix: 'Register a directory path for this context store.',
|
|
458
|
+
}));
|
|
459
|
+
}
|
|
460
|
+
else {
|
|
461
|
+
try {
|
|
462
|
+
const parsed = await readOptionalContextStoreMetadataState(root);
|
|
463
|
+
if (!parsed) {
|
|
464
|
+
metadata = { present: false, valid: false };
|
|
465
|
+
diagnostics.push(makeContextStoreDiagnostic('error', 'context_store_metadata_missing', 'Context store metadata is missing.', {
|
|
466
|
+
target: 'context_store.metadata',
|
|
467
|
+
fix: `Create ${metadataPath} or rerun context-store register.`,
|
|
468
|
+
}));
|
|
469
|
+
}
|
|
470
|
+
else if (parsed.id !== entry.id) {
|
|
471
|
+
metadata = { present: true, valid: false, id: parsed.id };
|
|
472
|
+
diagnostics.push(makeContextStoreDiagnostic('error', 'context_store_metadata_id_mismatch', `Context store metadata id '${parsed.id}' does not match registry id '${entry.id}'.`, {
|
|
473
|
+
target: 'context_store.metadata',
|
|
474
|
+
fix: 'Repair the local registry or store metadata so the ids match.',
|
|
475
|
+
}));
|
|
476
|
+
}
|
|
477
|
+
else {
|
|
478
|
+
metadata = { present: true, valid: true, id: parsed.id };
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
catch (error) {
|
|
482
|
+
metadata = { present: true, valid: false };
|
|
483
|
+
diagnostics.push(doctorStatusForError(error, 'context_store_metadata_invalid', 'context_store.metadata', `Repair ${metadataPath}.`));
|
|
484
|
+
}
|
|
485
|
+
git = {
|
|
486
|
+
isRepository: await isGitRepositoryAtRoot(root),
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
return {
|
|
490
|
+
id: entry.id,
|
|
491
|
+
root,
|
|
492
|
+
metadataPath,
|
|
493
|
+
metadata,
|
|
494
|
+
git,
|
|
495
|
+
diagnostics,
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
export async function doctorContextStores(id) {
|
|
499
|
+
const selectedId = id !== undefined ? validateContextStoreId(id) : undefined;
|
|
500
|
+
const registry = await readContextStoreRegistryState();
|
|
501
|
+
if (!registry) {
|
|
502
|
+
if (selectedId !== undefined) {
|
|
503
|
+
throw new ContextStoreError(`Unknown context store '${selectedId}'.`, 'context_store_not_found', {
|
|
504
|
+
target: 'context_store.id',
|
|
505
|
+
fix: 'Run openspec context-store list to see registered stores.',
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
return { stores: [], diagnostics: [] };
|
|
509
|
+
}
|
|
510
|
+
const entries = listContextStoreRegistryEntries(registry);
|
|
511
|
+
const selected = selectedId
|
|
512
|
+
? entries.filter((entry) => entry.id === selectedId)
|
|
513
|
+
: entries;
|
|
514
|
+
if (selectedId && selected.length === 0) {
|
|
515
|
+
throw new ContextStoreError(`Unknown context store '${selectedId}'.`, 'context_store_not_found', {
|
|
516
|
+
target: 'context_store.id',
|
|
517
|
+
fix: 'Run openspec context-store list to see registered stores.',
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
return {
|
|
521
|
+
stores: await Promise.all(selected.map(inspectContextStore)),
|
|
522
|
+
diagnostics: [],
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
export function normalizeContextStorePathForComparison(targetPath) {
|
|
526
|
+
return FileSystemUtils.canonicalizeExistingPath(targetPath);
|
|
527
|
+
}
|
|
528
|
+
//# sourceMappingURL=operations.js.map
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type ContextStoreBackendConfig, type ContextStoreGitBackendConfig, type ContextStorePathOptions, type ContextStoreRegistryEntry, type ContextStoreRegistryState } from './foundation.js';
|
|
2
|
+
export interface RegisterContextStoreInput extends ContextStorePathOptions {
|
|
3
|
+
id: string;
|
|
4
|
+
localPath: string;
|
|
5
|
+
remote?: string;
|
|
6
|
+
branch?: string;
|
|
7
|
+
cwd?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface ResolveRegisteredContextStoreInput extends ContextStorePathOptions {
|
|
10
|
+
id: string;
|
|
11
|
+
}
|
|
12
|
+
export interface GetRegisteredContextStoreInput extends ResolveRegisteredContextStoreInput {
|
|
13
|
+
expectedBackend?: ContextStoreGitBackendConfig;
|
|
14
|
+
}
|
|
15
|
+
export interface UnregisterContextStoreInput extends ContextStorePathOptions {
|
|
16
|
+
id: string;
|
|
17
|
+
expectedBackend?: ContextStoreGitBackendConfig;
|
|
18
|
+
beforeCommit?: (entry: RegisteredContextStoreEntry) => Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
export type ListRegisteredContextStoresOptions = ContextStorePathOptions;
|
|
21
|
+
export interface RegisteredContextStoreEntry extends ContextStoreRegistryEntry {
|
|
22
|
+
storeRoot: string;
|
|
23
|
+
}
|
|
24
|
+
export interface ResolvedContextStore {
|
|
25
|
+
id: string;
|
|
26
|
+
storeRoot: string;
|
|
27
|
+
backend: ContextStoreGitBackendConfig;
|
|
28
|
+
}
|
|
29
|
+
export interface ContextStoreRegistrationCommit extends ResolvedContextStore {
|
|
30
|
+
metadataCreated: boolean;
|
|
31
|
+
}
|
|
32
|
+
export interface CommitContextStoreRegistrationInput extends ContextStorePathOptions {
|
|
33
|
+
id: string;
|
|
34
|
+
backend: ContextStoreGitBackendConfig;
|
|
35
|
+
writeMetadataIfMissing: boolean;
|
|
36
|
+
}
|
|
37
|
+
export declare function getStoreRootForBackend(backend: ContextStoreBackendConfig): string;
|
|
38
|
+
export declare function assertNoRegisteredStoreConflict(registry: ContextStoreRegistryState | null, id: string, backend: ContextStoreGitBackendConfig): void;
|
|
39
|
+
export declare function commitContextStoreRegistration(input: CommitContextStoreRegistrationInput): Promise<ContextStoreRegistrationCommit>;
|
|
40
|
+
export declare function registerContextStore(input: RegisterContextStoreInput): Promise<ResolvedContextStore>;
|
|
41
|
+
export declare function listRegisteredContextStores(options?: ListRegisteredContextStoresOptions): Promise<RegisteredContextStoreEntry[]>;
|
|
42
|
+
export declare function getRegisteredContextStore(input: GetRegisteredContextStoreInput): Promise<RegisteredContextStoreEntry>;
|
|
43
|
+
export declare function unregisterContextStoreRegistration(input: UnregisterContextStoreInput): Promise<RegisteredContextStoreEntry>;
|
|
44
|
+
export declare function resolveRegisteredContextStore(input: ResolveRegisteredContextStoreInput): Promise<ResolvedContextStore>;
|
|
45
|
+
//# sourceMappingURL=registry.d.ts.map
|