@llman-sdd/cli 0.10.1 → 0.12.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.
- package/package.json +13 -5
- package/src/cli-shared.ts +87 -62
- package/src/commands/archive.ts +4 -19
- package/src/commands/change.ts +132 -172
- package/src/commands/config.ts +1 -29
- package/src/commands/init.ts +39 -11
- package/src/commands/list.ts +0 -2
- package/src/commands/project.ts +25 -115
- package/src/commands/review.ts +0 -1
- package/src/commands/self.ts +3 -1
- package/src/commands/show.ts +8 -46
- package/src/commands/spec.ts +6 -86
- package/src/commands/studio.ts +1315 -0
- package/src/commands/studioMcp.ts +862 -0
- package/src/commands/studioShared.ts +87 -0
- package/src/commands/theorem-write.ts +608 -0
- package/src/commands/theorem.ts +479 -0
- package/src/commands/validate.ts +108 -47
- package/src/io.ts +0 -1
- package/src/main.ts +3 -18
- package/src/program.ts +16 -16
- package/src/progress.ts +1 -3
- package/src/studio-server/changeScope.ts +295 -0
- package/src/studio-server/hub.ts +98 -0
- package/src/studio-server/negotiate.ts +60 -0
- package/src/studio-server/security.ts +71 -0
- package/src/studio-server/server.ts +1694 -0
- package/src/studio-server/static.ts +184 -0
- package/src/studio-server/syncDisk.ts +116 -0
- package/src/theorems/base.ts +53 -0
- package/src/theorems/diagnose.ts +90 -0
- package/src/theorems/draft.ts +252 -0
- package/src/theorems/os-lock.ts +364 -0
- package/src/theorems/paths.ts +11 -0
- package/src/theorems/prompts.ts +79 -0
- package/src/theorems/quiet.ts +21 -0
- package/src/theorems/runner.ts +48 -0
- package/src/theorems/transaction.ts +272 -0
- package/src/theorems/workspace.ts +103 -0
- package/studio-dist/index.html +14 -0
- package/studio-dist/static/css/index.00cf48cf.css +1 -0
- package/studio-dist/static/js/610.51883f69.js +218 -0
- package/studio-dist/static/js/610.51883f69.js.LICENSE.txt +26 -0
- package/studio-dist/static/js/index.c5f7c530.js +13 -0
- package/studio-dist/static/js/lib-react.a4b07007.js +2 -0
- package/studio-dist/static/js/lib-react.a4b07007.js.LICENSE.txt +49 -0
- package/src/commands/context.ts +0 -88
- package/src/commands/index.ts +0 -53
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared leaf helpers for Studio CLI (`studio.ts`) and Studio MCP (`studioMcp.ts`).
|
|
3
|
+
* One-way dependency: both command modules import from here to avoid cycles.
|
|
4
|
+
*/
|
|
5
|
+
import {
|
|
6
|
+
listStudioStates,
|
|
7
|
+
readStudioState,
|
|
8
|
+
StudioStateError,
|
|
9
|
+
validateStateId,
|
|
10
|
+
type StudioStateDoc,
|
|
11
|
+
type StudioStateIo,
|
|
12
|
+
} from '@llman-sdd/core';
|
|
13
|
+
|
|
14
|
+
import { CliError, formatNotFound } from '../cli-shared.ts';
|
|
15
|
+
import { makeIo } from '../io.ts';
|
|
16
|
+
|
|
17
|
+
export const EMPTY_DRAFT_NEXT = {
|
|
18
|
+
diff: 'run `llman-sdd studio open` to create one, or `llman-sdd studio validate --specs` to check machines straight from llmanspec/specs/.',
|
|
19
|
+
prompt: 'run `llman-sdd studio open` to create one.',
|
|
20
|
+
apply: 'run `llman-sdd studio open` to create one, then retry with a draft id.',
|
|
21
|
+
revise: 'run `llman-sdd studio open` to create one, then retry with a draft id.',
|
|
22
|
+
} as const;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Node filesystem adapter for C1 `StudioStateIo` anchored at `instanceRoot`.
|
|
26
|
+
* Writes go through sibling `.tmp.<pid>.<seq>` + atomic `renameSync` (via core `writeStudioState`);
|
|
27
|
+
* read operations never lazily create `.llman-sdd/studio/states/`.
|
|
28
|
+
*/
|
|
29
|
+
export function makeNodeStudioStateIo(instanceRoot: string): StudioStateIo {
|
|
30
|
+
const io = makeIo(instanceRoot);
|
|
31
|
+
return {
|
|
32
|
+
exists: io.exists,
|
|
33
|
+
readText: io.readText,
|
|
34
|
+
writeText: io.writeText,
|
|
35
|
+
rename: io.rename,
|
|
36
|
+
listDir: io.listDir,
|
|
37
|
+
mkdirp: io.mkdirp,
|
|
38
|
+
remove: io.remove,
|
|
39
|
+
currentPid: io.currentPid,
|
|
40
|
+
now: io.now,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Resolve an optional `[state-id]` argument:
|
|
46
|
+
* - explicit ID: validated and returned
|
|
47
|
+
* - omitted + 1 state on disk: auto-selected
|
|
48
|
+
* - omitted + 0 states: domain error (exit 1)
|
|
49
|
+
* - omitted + >1 states: domain error listing all IDs (exit 1)
|
|
50
|
+
*/
|
|
51
|
+
export function resolveStateIdOrThrow(
|
|
52
|
+
io: StudioStateIo,
|
|
53
|
+
stateId?: string,
|
|
54
|
+
next: string = EMPTY_DRAFT_NEXT.diff,
|
|
55
|
+
): string {
|
|
56
|
+
if (stateId !== undefined) {
|
|
57
|
+
return validateStateId(stateId);
|
|
58
|
+
}
|
|
59
|
+
const states = listStudioStates(io);
|
|
60
|
+
if (states.length === 0) {
|
|
61
|
+
throw new CliError(`no studio drafts in .llman-sdd/studio/states/.\nNext: ${next}`, 1);
|
|
62
|
+
}
|
|
63
|
+
if (states.length === 1) {
|
|
64
|
+
return states[0]!.id;
|
|
65
|
+
}
|
|
66
|
+
const ids = states.map((s) => s.id).join(', ');
|
|
67
|
+
throw new CliError(`multiple studio drafts found (${ids}); specify <state-id> explicitly`, 1);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function readStudioStateOrThrow(io: StudioStateIo, stateId: string): StudioStateDoc {
|
|
71
|
+
const id = validateStateId(stateId);
|
|
72
|
+
try {
|
|
73
|
+
return readStudioState(io, id);
|
|
74
|
+
} catch (error) {
|
|
75
|
+
if (error instanceof StudioStateError && error.message.startsWith('studio state not found:')) {
|
|
76
|
+
throw new CliError(
|
|
77
|
+
formatNotFound(
|
|
78
|
+
'studio state',
|
|
79
|
+
id,
|
|
80
|
+
listStudioStates(io).map((s) => s.id),
|
|
81
|
+
),
|
|
82
|
+
1,
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
throw error;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,608 @@
|
|
|
1
|
+
import { readFileSync, statSync } from 'node:fs';
|
|
2
|
+
import { basename, join } from 'node:path';
|
|
3
|
+
|
|
4
|
+
import { THEOREMS_DIR_REL, discoverTheoremFiles, validateTheoremSet } from '@llman-sdd/core';
|
|
5
|
+
import type { Command } from 'commander';
|
|
6
|
+
|
|
7
|
+
import { CliError, resolveCommandRoot, type CliContext } from '../cli-shared.ts';
|
|
8
|
+
import { promptEditTheorem, promptNewTheorem, type TheoremDraft } from '../theorems/draft.ts';
|
|
9
|
+
import {
|
|
10
|
+
applyStrongLock,
|
|
11
|
+
applyWeakFallbackLock,
|
|
12
|
+
inspectLock,
|
|
13
|
+
joinCommands,
|
|
14
|
+
nativeCommands,
|
|
15
|
+
probeLockCapability,
|
|
16
|
+
removeStrongLock,
|
|
17
|
+
type CommandRunner,
|
|
18
|
+
type TheoremPlatform,
|
|
19
|
+
} from '../theorems/os-lock.ts';
|
|
20
|
+
import {
|
|
21
|
+
isPromptAborted,
|
|
22
|
+
makeInquirerPromptPort,
|
|
23
|
+
type TheoremPromptPort,
|
|
24
|
+
} from '../theorems/prompts.ts';
|
|
25
|
+
import { INTERACTIVE_TIMEOUT_MS, makeCommandRunner } from '../theorems/runner.ts';
|
|
26
|
+
import {
|
|
27
|
+
OS_LOCK_NOTICE,
|
|
28
|
+
editInTempCopy,
|
|
29
|
+
runWriteTransaction,
|
|
30
|
+
validateDraft,
|
|
31
|
+
type TransactionContext,
|
|
32
|
+
type TransactionPlan,
|
|
33
|
+
type TransactionArgs,
|
|
34
|
+
} from '../theorems/transaction.ts';
|
|
35
|
+
|
|
36
|
+
export function nonTtyRefusal(subcommand: string): string {
|
|
37
|
+
return [
|
|
38
|
+
'Error: \u0060llman-sdd theorem ' +
|
|
39
|
+
subcommand +
|
|
40
|
+
'\u0060 requires an interactive human TTY and cannot be run non-interactively.',
|
|
41
|
+
'Human Theorems are read-only for agents. Do NOT attempt to bypass this guard.',
|
|
42
|
+
'Action required: stop modifying code, report the conflicting theorem ID, statement, and conflict details to the human, and ask the human to resolve the requirement or run \u0060llman-sdd theorem edit <id>\u0060 interactively.',
|
|
43
|
+
].join('\n');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function isInteractiveTty(): boolean {
|
|
47
|
+
return process.stdin.isTTY === true && process.stdout.isTTY === true;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function platformOf(): TheoremPlatform {
|
|
51
|
+
const platform = process.platform;
|
|
52
|
+
return platform === 'darwin' || platform === 'win32' ? platform : 'linux';
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function modeOf(file: string): number | null {
|
|
56
|
+
try {
|
|
57
|
+
return statSync(file).mode;
|
|
58
|
+
} catch {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface WriteContext {
|
|
64
|
+
root: string;
|
|
65
|
+
directory: string;
|
|
66
|
+
run: CommandRunner;
|
|
67
|
+
interactiveRun: CommandRunner;
|
|
68
|
+
platform: TheoremPlatform;
|
|
69
|
+
prompt: TheoremPromptPort;
|
|
70
|
+
io: CliContext['io'];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export async function writeContext(
|
|
74
|
+
ctx: CliContext,
|
|
75
|
+
directory: string | undefined,
|
|
76
|
+
prompt?: TheoremPromptPort,
|
|
77
|
+
): Promise<WriteContext> {
|
|
78
|
+
const root = resolveCommandRoot(ctx, directory);
|
|
79
|
+
return {
|
|
80
|
+
root,
|
|
81
|
+
directory: join(root, THEOREMS_DIR_REL),
|
|
82
|
+
run: makeCommandRunner({ cwd: root }),
|
|
83
|
+
interactiveRun: makeCommandRunner({
|
|
84
|
+
cwd: root,
|
|
85
|
+
interactive: true,
|
|
86
|
+
timeoutMs: INTERACTIVE_TIMEOUT_MS,
|
|
87
|
+
}),
|
|
88
|
+
platform: platformOf(),
|
|
89
|
+
prompt: prompt ?? (await makeInquirerPromptPort()),
|
|
90
|
+
io: ctx.io,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function transactionContext(context: WriteContext): TransactionContext {
|
|
95
|
+
return {
|
|
96
|
+
root: context.root,
|
|
97
|
+
directory: context.directory,
|
|
98
|
+
run: context.run,
|
|
99
|
+
interactiveRun: context.interactiveRun,
|
|
100
|
+
platform: context.platform,
|
|
101
|
+
io: context.io,
|
|
102
|
+
prompt: context.prompt,
|
|
103
|
+
modeOf,
|
|
104
|
+
isTty: isInteractiveTty,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function demandTty(subcommand: string): void {
|
|
109
|
+
if (isInteractiveTty()) return;
|
|
110
|
+
throw new CliError(nonTtyRefusal(subcommand));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function theoremFiles(context: WriteContext): string[] {
|
|
114
|
+
if (!context.io.exists(context.directory)) return [];
|
|
115
|
+
return discoverTheoremFiles(context.directory, context.io);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function pickTheoremFile(context: WriteContext, id: string): string[] {
|
|
119
|
+
return theoremFiles(context).filter((file) => basename(file) === id + '.yaml');
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function printCommands(context: WriteContext, files: readonly string[]): void {
|
|
123
|
+
const commands = nativeCommands(context.platform, files);
|
|
124
|
+
console.log(OS_LOCK_NOTICE);
|
|
125
|
+
console.log('');
|
|
126
|
+
console.log('# lock: ' + joinCommands(commands.lock));
|
|
127
|
+
console.log('# unlock: ' + joinCommands(commands.unlock));
|
|
128
|
+
console.log('# inspect: ' + (commands.inspect[0] ?? ''));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export interface LockOutcomeReport {
|
|
132
|
+
ok: boolean;
|
|
133
|
+
strength: string;
|
|
134
|
+
detail: string;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function reportLockOutcome(
|
|
138
|
+
outcome: LockOutcomeReport,
|
|
139
|
+
count: number,
|
|
140
|
+
platform: TheoremPlatform,
|
|
141
|
+
): void {
|
|
142
|
+
if (!outcome.ok) throw new CliError('lock failed: ' + outcome.detail);
|
|
143
|
+
console.log(
|
|
144
|
+
'Locked ' +
|
|
145
|
+
String(count) +
|
|
146
|
+
' theorem file(s) with native OS protection (' +
|
|
147
|
+
outcome.detail +
|
|
148
|
+
').',
|
|
149
|
+
);
|
|
150
|
+
console.log('Native manual unlock command (works without llman-sdd):');
|
|
151
|
+
console.log(' ' + joinCommands(nativeCommands(platform, ['<file>']).unlock));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function probeCapability(context: WriteContext): { supported: boolean; reason?: string } {
|
|
155
|
+
return probeLockCapability(context.platform, context.run, context.directory);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function registerLock(theorem: Command, ctx: CliContext): void {
|
|
159
|
+
theorem
|
|
160
|
+
.command('lock')
|
|
161
|
+
.description('Apply native OS locks to theorem .yaml files (human-only)')
|
|
162
|
+
.argument('[id]', 'lock only this theorem id')
|
|
163
|
+
.option('--print-commands', 'print the native commands without executing them')
|
|
164
|
+
.option('--fallback-readonly', 'explicit weak fallback when strong locks are unsupported')
|
|
165
|
+
.option('--directory <path>', 'instance root resolution start')
|
|
166
|
+
.action(
|
|
167
|
+
async (
|
|
168
|
+
id: string | undefined,
|
|
169
|
+
options: { printCommands?: boolean; fallbackReadonly?: boolean; directory?: string },
|
|
170
|
+
) => {
|
|
171
|
+
const context = await writeContext(ctx, options.directory);
|
|
172
|
+
const files = id === undefined ? theoremFiles(context) : pickTheoremFile(context, id);
|
|
173
|
+
if (options.printCommands === true) {
|
|
174
|
+
printCommands(context, files);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
demandTty('lock');
|
|
178
|
+
console.log(OS_LOCK_NOTICE);
|
|
179
|
+
const capability = probeCapability(context);
|
|
180
|
+
if (!capability.supported) {
|
|
181
|
+
if (options.fallbackReadonly === true) {
|
|
182
|
+
const weak = applyWeakFallbackLock(files, {
|
|
183
|
+
platform: context.platform,
|
|
184
|
+
run: context.run,
|
|
185
|
+
});
|
|
186
|
+
reportLockOutcome(weak, files.length, context.platform);
|
|
187
|
+
console.error(
|
|
188
|
+
'WARNING: weak read-only fallback in effect; same-user writes remain possible.',
|
|
189
|
+
);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
throw new CliError(
|
|
193
|
+
'native strong locks unsupported: ' +
|
|
194
|
+
(capability.reason ?? 'unknown reason') +
|
|
195
|
+
'. Re-run with --fallback-readonly or move to a supporting filesystem.',
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
const outcome = applyStrongLock(files, {
|
|
199
|
+
platform: context.platform,
|
|
200
|
+
run: context.interactiveRun,
|
|
201
|
+
});
|
|
202
|
+
reportLockOutcome(outcome, files.length, context.platform);
|
|
203
|
+
},
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export function registerUnlock(theorem: Command, ctx: CliContext): void {
|
|
208
|
+
theorem
|
|
209
|
+
.command('unlock')
|
|
210
|
+
.description('Remove native OS locks from theorem files (human-only)')
|
|
211
|
+
.argument('[id]', 'unlock only this theorem id')
|
|
212
|
+
.option('--all', 'unlock every theorem file')
|
|
213
|
+
.option('--for-git', 'print the post-git-operation relock reminder')
|
|
214
|
+
.option('--print-commands', 'print the native commands without executing them')
|
|
215
|
+
.option('--directory <path>', 'instance root resolution start')
|
|
216
|
+
.action(
|
|
217
|
+
async (
|
|
218
|
+
id: string | undefined,
|
|
219
|
+
options: {
|
|
220
|
+
all?: boolean;
|
|
221
|
+
forGit?: boolean;
|
|
222
|
+
printCommands?: boolean;
|
|
223
|
+
directory?: string;
|
|
224
|
+
},
|
|
225
|
+
) => {
|
|
226
|
+
const context = await writeContext(ctx, options.directory);
|
|
227
|
+
const all = theoremFiles(context);
|
|
228
|
+
const files =
|
|
229
|
+
options.all === true ? all : id === undefined ? all : pickTheoremFile(context, id);
|
|
230
|
+
if (options.printCommands === true) {
|
|
231
|
+
printCommands(context, files);
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
demandTty('unlock');
|
|
235
|
+
const outcome = removeStrongLock(files, {
|
|
236
|
+
platform: context.platform,
|
|
237
|
+
run: context.interactiveRun,
|
|
238
|
+
});
|
|
239
|
+
if (!outcome.ok) throw new CliError('unlock failed: ' + outcome.detail);
|
|
240
|
+
console.log('Unlocked ' + String(files.length) + ' theorem file(s).');
|
|
241
|
+
if (options.forGit === true) {
|
|
242
|
+
console.log(
|
|
243
|
+
'Finish the Git operation, then immediately run \u0060llman-sdd theorem lock\u0060.',
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
},
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
interface WorkspaceShape {
|
|
251
|
+
ids: string[];
|
|
252
|
+
groups: string[];
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function workspaceShape(context: WriteContext): WorkspaceShape {
|
|
256
|
+
const files = theoremFiles(context);
|
|
257
|
+
const sources = files.map((file) => ({
|
|
258
|
+
filePath: file,
|
|
259
|
+
content: context.io.readText(file),
|
|
260
|
+
}));
|
|
261
|
+
const ids = validateTheoremSet(sources).theorems.map((entry) => entry.id);
|
|
262
|
+
const groups = new Set<string>();
|
|
263
|
+
for (const file of files) {
|
|
264
|
+
const parent = file.slice(0, file.lastIndexOf('/'));
|
|
265
|
+
if (parent !== context.directory) {
|
|
266
|
+
groups.add(parent.slice(context.directory.length + 1));
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return { ids, groups: [...groups].toSorted() };
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
interface DraftOptions {
|
|
273
|
+
printCommands?: boolean;
|
|
274
|
+
fallbackReadonly?: boolean;
|
|
275
|
+
editor?: boolean;
|
|
276
|
+
directory?: string;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export function registerAdd(theorem: Command, ctx: CliContext): void {
|
|
280
|
+
theorem
|
|
281
|
+
.command('add')
|
|
282
|
+
.description('Create a new Human Theorem (human TTY only)')
|
|
283
|
+
.option('--print-commands', 'print the native lock commands only')
|
|
284
|
+
.option('--fallback-readonly', 'allow the explicit weak fallback')
|
|
285
|
+
.option('--directory <path>', 'instance root resolution start')
|
|
286
|
+
.action(async (options: DraftOptions) => {
|
|
287
|
+
const context = await writeContext(ctx, options.directory);
|
|
288
|
+
if (options.printCommands === true) {
|
|
289
|
+
printCommands(context, theoremFiles(context));
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
demandTty('add');
|
|
293
|
+
const shape = workspaceShape(context);
|
|
294
|
+
const plan: TransactionPlan = { targetFile: '', yaml: '', deleteInstead: false };
|
|
295
|
+
const args: TransactionArgs = {
|
|
296
|
+
plan,
|
|
297
|
+
summary: 'Write the new theorem',
|
|
298
|
+
collectDraft: async () => {
|
|
299
|
+
try {
|
|
300
|
+
const draft = await promptNewTheorem(context.prompt, {
|
|
301
|
+
existingIds: shape.ids,
|
|
302
|
+
groups: shape.groups,
|
|
303
|
+
});
|
|
304
|
+
plan.targetFile = draft.filePath;
|
|
305
|
+
plan.yaml = draft.yaml;
|
|
306
|
+
} catch (error) {
|
|
307
|
+
if (isPromptAborted(error)) return ['aborted by human'];
|
|
308
|
+
throw error;
|
|
309
|
+
}
|
|
310
|
+
return validateDraft(transactionContext(context), plan);
|
|
311
|
+
},
|
|
312
|
+
};
|
|
313
|
+
const ok = await runWriteTransaction(transactionContext(context), args);
|
|
314
|
+
if (!ok) throw new CliError('theorem add cancelled');
|
|
315
|
+
console.log('Wrote ' + plan.targetFile + '.');
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export function registerEdit(theorem: Command, ctx: CliContext): void {
|
|
320
|
+
theorem
|
|
321
|
+
.command('edit')
|
|
322
|
+
.description('Edit an existing Human Theorem (human TTY only)')
|
|
323
|
+
.argument('[id]', 'theorem id (prompted when omitted)')
|
|
324
|
+
.option('--editor', 'edit a temp copy in $EDITOR instead of field by field')
|
|
325
|
+
.option('--print-commands', 'print the native lock commands only')
|
|
326
|
+
.option('--directory <path>', 'instance root resolution start')
|
|
327
|
+
.action(async (id: string | undefined, options: DraftOptions) => {
|
|
328
|
+
const context = await writeContext(ctx, options.directory);
|
|
329
|
+
if (options.printCommands === true) {
|
|
330
|
+
printCommands(context, theoremFiles(context));
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
demandTty('edit');
|
|
334
|
+
const shape = workspaceShape(context);
|
|
335
|
+
if (shape.ids.length === 0)
|
|
336
|
+
throw new CliError('no theorem to edit; run \u0060theorem add\u0060 first');
|
|
337
|
+
const targetId =
|
|
338
|
+
id ??
|
|
339
|
+
(await context.prompt.select(
|
|
340
|
+
'Which theorem do you want to edit?',
|
|
341
|
+
shape.ids.map((entry) => ({ name: entry, value: entry })),
|
|
342
|
+
));
|
|
343
|
+
const target = pickTheoremFile(context, targetId)[0];
|
|
344
|
+
if (target === undefined) {
|
|
345
|
+
throw new CliError(
|
|
346
|
+
'theorem not found: ' + targetId + ' (known ids: ' + shape.ids.join(', ') + ')',
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
const relTarget = target.slice(context.root.length + 1);
|
|
350
|
+
const currentYaml = context.io.readText(target);
|
|
351
|
+
const current = validateTheoremSet([{ filePath: relTarget, content: currentYaml }]);
|
|
352
|
+
const docs = current.theorems;
|
|
353
|
+
if (docs.length !== 1 || docs[0] === undefined) {
|
|
354
|
+
const detail = current.issues.map((issue) => issue.message).join('; ') || 'no document';
|
|
355
|
+
throw new CliError('theorem ' + targetId + ' is not valid; fix it first: ' + detail);
|
|
356
|
+
}
|
|
357
|
+
const plan: TransactionPlan = {
|
|
358
|
+
targetFile: relTarget,
|
|
359
|
+
yaml: currentYaml,
|
|
360
|
+
deleteInstead: false,
|
|
361
|
+
};
|
|
362
|
+
const txContext = transactionContext(context);
|
|
363
|
+
|
|
364
|
+
if (options.editor === true) {
|
|
365
|
+
const accepted = await editInTempCopy(context, relTarget, currentYaml, (candidate) =>
|
|
366
|
+
validateDraft(txContext, { ...plan, yaml: candidate }),
|
|
367
|
+
);
|
|
368
|
+
if (accepted === null) throw new CliError('theorem edit cancelled');
|
|
369
|
+
plan.yaml = accepted;
|
|
370
|
+
const ok = await runWriteTransaction(txContext, {
|
|
371
|
+
plan,
|
|
372
|
+
summary: 'Write the edited theorem',
|
|
373
|
+
collectDraft: async () => [],
|
|
374
|
+
});
|
|
375
|
+
if (!ok) throw new CliError('theorem edit cancelled');
|
|
376
|
+
console.log('Updated ' + relTarget + '.');
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
const ok = await runWriteTransaction(txContext, {
|
|
381
|
+
plan,
|
|
382
|
+
summary: 'Write the edited theorem',
|
|
383
|
+
collectDraft: async () => {
|
|
384
|
+
try {
|
|
385
|
+
const draft: TheoremDraft = await promptEditTheorem(
|
|
386
|
+
context.prompt,
|
|
387
|
+
current.theorems[0]!,
|
|
388
|
+
relTarget,
|
|
389
|
+
);
|
|
390
|
+
plan.yaml = draft.yaml;
|
|
391
|
+
} catch (error) {
|
|
392
|
+
if (isPromptAborted(error)) return ['aborted by human'];
|
|
393
|
+
throw error;
|
|
394
|
+
}
|
|
395
|
+
return validateDraft(txContext, plan);
|
|
396
|
+
},
|
|
397
|
+
});
|
|
398
|
+
if (!ok) throw new CliError('theorem edit cancelled');
|
|
399
|
+
console.log('Updated ' + relTarget + '.');
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// Every file under the scanned roots that mentions the id, as a review list.
|
|
404
|
+
export function scanTheoremReferences(
|
|
405
|
+
context: WriteContext,
|
|
406
|
+
id: string,
|
|
407
|
+
roots: readonly string[] = ['llmanspec', 'tests'],
|
|
408
|
+
): string[] {
|
|
409
|
+
const needle = id;
|
|
410
|
+
const hits: string[] = [];
|
|
411
|
+
const walk = (absDir: string): void => {
|
|
412
|
+
if (!context.io.exists(absDir) || !context.io.isDirectory(absDir)) return;
|
|
413
|
+
for (const name of context.io.listDir(absDir).toSorted()) {
|
|
414
|
+
if (name.startsWith('.') || name === 'node_modules') continue;
|
|
415
|
+
const abs = join(absDir, name);
|
|
416
|
+
if (context.io.isDirectory(abs)) {
|
|
417
|
+
walk(abs);
|
|
418
|
+
continue;
|
|
419
|
+
}
|
|
420
|
+
let text: string;
|
|
421
|
+
try {
|
|
422
|
+
text = readFileSync(abs, 'utf8');
|
|
423
|
+
} catch {
|
|
424
|
+
continue;
|
|
425
|
+
}
|
|
426
|
+
if (text.includes(needle)) hits.push(abs.slice(context.root.length + 1));
|
|
427
|
+
}
|
|
428
|
+
};
|
|
429
|
+
for (const rel of roots) walk(join(context.root, rel));
|
|
430
|
+
return hits.toSorted();
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// \u0060theorem remove <id>\u0060: delete after reference review and confirmation.
|
|
434
|
+
export function registerRemove(theorem: Command, ctx: CliContext): void {
|
|
435
|
+
theorem
|
|
436
|
+
.command('remove')
|
|
437
|
+
.description('Delete a Human Theorem (human TTY only; deprecating is preferred)')
|
|
438
|
+
.argument('<id>', 'theorem id')
|
|
439
|
+
.option('--print-commands', 'print the native lock commands only')
|
|
440
|
+
.option('--directory <path>', 'instance root resolution start')
|
|
441
|
+
.action(async (id: string, options: DraftOptions) => {
|
|
442
|
+
const context = await writeContext(ctx, options.directory);
|
|
443
|
+
if (options.printCommands === true) {
|
|
444
|
+
printCommands(context, theoremFiles(context));
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
demandTty('remove');
|
|
448
|
+
const shape = workspaceShape(context);
|
|
449
|
+
const target = pickTheoremFile(context, id)[0];
|
|
450
|
+
if (target === undefined) {
|
|
451
|
+
throw new CliError(
|
|
452
|
+
'theorem not found: ' + id + ' (known ids: ' + shape.ids.join(', ') + ')',
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
const relTarget = target.slice(context.root.length + 1);
|
|
456
|
+
const references = scanTheoremReferences(context, id);
|
|
457
|
+
console.log('Deleting a theorem loses its recorded rationale.');
|
|
458
|
+
console.log(
|
|
459
|
+
'Prefer \u0060status: deprecated\u0060 via \u0060llman-sdd theorem edit ' +
|
|
460
|
+
id +
|
|
461
|
+
'\u0060 unless it must go.',
|
|
462
|
+
);
|
|
463
|
+
if (references.length > 0) {
|
|
464
|
+
console.log('');
|
|
465
|
+
console.log(
|
|
466
|
+
'References to \u0060' +
|
|
467
|
+
id +
|
|
468
|
+
'\u0060 found in ' +
|
|
469
|
+
String(references.length) +
|
|
470
|
+
' file(s):',
|
|
471
|
+
);
|
|
472
|
+
for (const file of references) console.log(' - ' + file);
|
|
473
|
+
console.log('Deprecating keeps these references meaningful.');
|
|
474
|
+
}
|
|
475
|
+
const ok = await runWriteTransaction(transactionContext(context), {
|
|
476
|
+
plan: { targetFile: relTarget, yaml: '', deleteInstead: true },
|
|
477
|
+
summary: 'Delete this theorem file',
|
|
478
|
+
collectDraft: async () => [],
|
|
479
|
+
});
|
|
480
|
+
if (!ok) throw new CliError('theorem remove cancelled');
|
|
481
|
+
console.log('Removed ' + relTarget + '.');
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
export function registerMigrate(theorem: Command, ctx: CliContext): void {
|
|
486
|
+
theorem
|
|
487
|
+
.command('migrate')
|
|
488
|
+
.description('Migrate legacy Markdown theorems to single-file YAML (human TTY only)')
|
|
489
|
+
.option('--print-commands', 'print the native lock commands only')
|
|
490
|
+
.option('--fallback-readonly', 'allow the explicit weak fallback')
|
|
491
|
+
.option('--directory <path>', 'instance root resolution start')
|
|
492
|
+
.action(async (options: DraftOptions) => {
|
|
493
|
+
const context = await writeContext(ctx, options.directory);
|
|
494
|
+
if (options.printCommands === true) {
|
|
495
|
+
printCommands(context, theoremFiles(context));
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
demandTty('migrate');
|
|
499
|
+
if (!context.io.exists(context.directory)) {
|
|
500
|
+
console.log('No theorem directory found; nothing to migrate.');
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
const { findLegacyTheoremFiles, migrateLegacyTheoremMarkdown, renderTheoremUnifiedDiff } =
|
|
504
|
+
await import('@llman-sdd/core');
|
|
505
|
+
const legacy = findLegacyTheoremFiles(context.directory, context.io).filter((file) =>
|
|
506
|
+
file.endsWith('.md'),
|
|
507
|
+
);
|
|
508
|
+
if (legacy.length === 0) {
|
|
509
|
+
console.log('No legacy Markdown theorems found.');
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
const planned: Array<{ filePath: string; yaml: string; source: string }> = [];
|
|
513
|
+
for (const file of legacy) {
|
|
514
|
+
const result = migrateLegacyTheoremMarkdown(file, context.io.readText(file));
|
|
515
|
+
if (!result.ok) {
|
|
516
|
+
throw new CliError('migration failed for ' + file + ': ' + result.issues.join('; '));
|
|
517
|
+
}
|
|
518
|
+
for (const migrated of result.theorems) {
|
|
519
|
+
planned.push({ filePath: migrated.filePath, yaml: migrated.yaml, source: file });
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
console.log('');
|
|
523
|
+
for (const item of planned) {
|
|
524
|
+
const before =
|
|
525
|
+
context.io.exists(item.filePath) && !context.io.isDirectory(item.filePath)
|
|
526
|
+
? context.io.readText(item.filePath)
|
|
527
|
+
: '';
|
|
528
|
+
const diff = renderTheoremUnifiedDiff({
|
|
529
|
+
filePath: item.filePath,
|
|
530
|
+
before,
|
|
531
|
+
after: item.yaml,
|
|
532
|
+
});
|
|
533
|
+
console.log(diff === '' ? '(unchanged) ' + item.filePath : diff);
|
|
534
|
+
}
|
|
535
|
+
console.log('');
|
|
536
|
+
console.log(OS_LOCK_NOTICE);
|
|
537
|
+
console.log('');
|
|
538
|
+
console.log(
|
|
539
|
+
'This will write ' +
|
|
540
|
+
String(planned.length) +
|
|
541
|
+
' theorem file(s), delete ' +
|
|
542
|
+
String(legacy.length) +
|
|
543
|
+
' legacy Markdown file(s) and retire .theorems.lock.json.',
|
|
544
|
+
);
|
|
545
|
+
console.log('');
|
|
546
|
+
let confirmed: boolean;
|
|
547
|
+
try {
|
|
548
|
+
confirmed = await context.prompt.confirm('Apply this migration?');
|
|
549
|
+
} catch (error) {
|
|
550
|
+
if (isPromptAborted(error)) confirmed = false;
|
|
551
|
+
else throw error;
|
|
552
|
+
}
|
|
553
|
+
if (!confirmed) throw new CliError('theorem migrate cancelled');
|
|
554
|
+
|
|
555
|
+
const written = planned.map((item) => ({ filePath: item.filePath, content: item.yaml }));
|
|
556
|
+
const validation = validateTheoremSet(written);
|
|
557
|
+
if (!validation.ok) {
|
|
558
|
+
const detail = validation.issues.map((issue) => issue.message).join('; ');
|
|
559
|
+
throw new CliError('migrated theorems are invalid: ' + detail);
|
|
560
|
+
}
|
|
561
|
+
const targets = planned.map((item) => join(context.root, item.filePath));
|
|
562
|
+
removeStrongLock(targets, { platform: context.platform, run: context.interactiveRun });
|
|
563
|
+
for (const item of planned) context.io.writeText(item.filePath, item.yaml);
|
|
564
|
+
for (const file of legacy) context.io.remove(file);
|
|
565
|
+
const lockFile = join(context.directory, '.theorems.lock.json');
|
|
566
|
+
if (context.io.exists(lockFile)) context.io.remove(lockFile);
|
|
567
|
+
|
|
568
|
+
const files = theoremFiles(context);
|
|
569
|
+
const locked = applyStrongLock(files, {
|
|
570
|
+
platform: context.platform,
|
|
571
|
+
run: context.interactiveRun,
|
|
572
|
+
});
|
|
573
|
+
if (!locked.ok && options.fallbackReadonly === true) {
|
|
574
|
+
const weak = applyWeakFallbackLock(files, { platform: context.platform, run: context.run });
|
|
575
|
+
console.error('WARNING: weak read-only fallback in effect; ' + weak.detail);
|
|
576
|
+
}
|
|
577
|
+
const strengths = files.map((file) =>
|
|
578
|
+
inspectLock(file, { platform: context.platform, run: context.run, modeOf }),
|
|
579
|
+
);
|
|
580
|
+
const strong = strengths.filter((strength) => strength === 'strong').length;
|
|
581
|
+
const weak = strengths.filter((strength) => strength === 'weak').length;
|
|
582
|
+
console.log('Migrated ' + String(planned.length) + ' theorem(s) to single-file YAML.');
|
|
583
|
+
for (const item of written) console.log(' - ' + item.filePath);
|
|
584
|
+
console.log(
|
|
585
|
+
'Lock state: ' +
|
|
586
|
+
String(strong) +
|
|
587
|
+
' strong, ' +
|
|
588
|
+
String(weak) +
|
|
589
|
+
' weak, ' +
|
|
590
|
+
String(files.length - strong - weak) +
|
|
591
|
+
' none.',
|
|
592
|
+
);
|
|
593
|
+
if (strong < files.length) {
|
|
594
|
+
console.error(
|
|
595
|
+
'WARNING: not every migrated theorem is natively locked; run \u0060theorem lock\u0060.',
|
|
596
|
+
);
|
|
597
|
+
}
|
|
598
|
+
});
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
export function registerTheoremWrites(theorem: Command, ctx: CliContext): void {
|
|
602
|
+
registerLock(theorem, ctx);
|
|
603
|
+
registerUnlock(theorem, ctx);
|
|
604
|
+
registerAdd(theorem, ctx);
|
|
605
|
+
registerEdit(theorem, ctx);
|
|
606
|
+
registerRemove(theorem, ctx);
|
|
607
|
+
registerMigrate(theorem, ctx);
|
|
608
|
+
}
|