forked from github/copilot-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-chat-inline-proof.js
More file actions
643 lines (584 loc) · 21.3 KB
/
run-chat-inline-proof.js
File metadata and controls
643 lines (584 loc) · 21.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
#!/usr/bin/env node
const { spawn, spawnSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const { LIKU_HOME, ensureLikuStructure } = require(path.join(__dirname, '..', 'src', 'shared', 'liku-home.js'));
const REPO_ROOT = path.join(__dirname, '..');
const PROOF_TRACE_DIR = path.join(LIKU_HOME, 'traces', 'chat-inline-proof');
const PROOF_RESULT_LOG = path.join(LIKU_HOME, 'telemetry', 'logs', 'chat-inline-proof-results.jsonl');
const MODEL_SHORTCUTS = new Set(['cheap', 'budget', 'free', 'older', 'vision-cheap', 'cheap-vision', 'latest-gpt', 'newest-gpt', 'gpt-latest']);
const SUITES = {
'status-basic-chat': {
description: 'Verifies inline status handling and a normal non-action assistant reply through the real chat path.',
executeMode: 'false',
prompts: [
'/status',
'Say hello in one short sentence.',
'exit'
],
expectations: [
{
name: 'status reports provider',
scope: 'transcript',
include: [/Provider:\s+copilot/i, /Copilot:\s+Authenticated/i]
},
{
name: 'assistant returns a plain chat reply',
turn: 1,
include: [/(hello|hey|hi)\b/i],
exclude: [/"actions"\s*:/i, /```json/i]
}
]
},
'direct-navigation': {
description: 'Proves direct URL planning, repeated grounding, and no-op confirmation when state is already satisfied.',
executeMode: 'false',
prompts: [
'/status',
'Open https://example.com in Edge without using search or intermediate pages. Use the most direct grounded method.',
'Open https://example.com in Edge without using search or intermediate pages. Use the most direct grounded method.',
'The Example Domain page should already be open. Confirm briefly and do not propose any new actions.',
'exit'
],
expectations: [
{
name: 'status reports provider',
scope: 'transcript',
include: [/Provider:\s+copilot/i, /Copilot:\s+Authenticated/i]
},
{
name: 'assistant uses direct URL plan',
turn: 1,
include: [/https:\/\/example\.com/i, /(bring_window_to_front|focus_window)/i],
exclude: [/google\.com/i, /bing\.com/i, /search the web/i]
},
{
name: 'repeated request stays direct',
turn: 2,
include: [/(navigate( directly)? to ((https?:\/\/)?example\.com|the example domain website)|example( domain)? website should now be open)/i],
exclude: [/search engine/i, /intermediate page/i]
},
{
name: 'final turn confirms no further actions',
turn: 3,
include: [/(Confirmed|Example( Domain)? page is not currently open|Example( Domain)? page is already open)/i, /(No further actions (needed|taken|are proposed)|No actions proposed)/i],
exclude: [/"actions"\s*:/i]
}
]
},
'recovery-noop': {
description: 'Verifies the no-action retry path and final no-op confirmation for an automation-like request.',
executeMode: 'false',
prompts: [
'/status',
'Open https://example.com in Edge without using search or intermediate pages. Use the most direct grounded method.',
'The Example Domain page should already be open. Confirm briefly and do not propose any new actions.',
'exit'
],
expectations: [
{
name: 'status reports provider',
scope: 'transcript',
include: [/Provider:\s+copilot/i, /Copilot:\s+Authenticated/i]
},
{
name: 'first automation turn stays direct',
turn: 1,
include: [/(example\.com|Example Domain is already open|https:\/\/example\.com)/i, /(bring_window_to_front|ctrl\+l|alt\+d)/i],
exclude: [/google\.com/i, /bing\.com/i]
},
{
name: 'final no-op path uses retry or deterministic short-circuit',
scope: 'transcript',
include: [/(No actions detected for an automation-like request; retrying once with stricter formatting|browser-goal-satisfied-short-circuit)/i]
},
{
name: 'final turn confirms without new actions',
turn: 2,
include: [/Confirmed/i, /(No further actions (needed|taken)|No actions proposed)/i],
exclude: [/"actions"\s*:/i, /```json/i]
}
]
},
'safety-boundaries': {
description: 'Distinguishes confirmation-worthy destructive plans from safe low-risk actions in inline chat.',
executeMode: 'prompt',
prompts: [
'/status',
'Close the current Edge window using a keyboard shortcut.',
'n',
'Take a screenshot of the current screen.',
'exit'
],
expectations: [
{
name: 'status reports provider',
scope: 'transcript',
include: [/Provider:\s+copilot/i, /Copilot:\s+Authenticated/i]
},
{
name: 'risky close plan triggers confirmation prompt',
scope: 'transcript',
include: [/Run \d+ action\(s\)\? \(y\/N\/a\/d\/c\)/i],
count: { pattern: /Run \d+ action\(s\)\? \(y\/N\/a\/d\/c\)/i, exactly: 1 }
},
{
name: 'declined risky action is skipped',
scope: 'transcript',
include: [/Skipped\./i]
},
{
name: 'safe screenshot runs without confirmation',
scope: 'transcript',
include: [/(Low-risk sequence|screenshot:)/i],
exclude: [/Confirmation required \(critical\)/i]
}
]
},
'recovery-quality': {
description: 'Verifies that action-free automation replies recover once with stricter formatting and then converge cleanly.',
executeMode: 'false',
prompts: [
'/status',
'Open https://example.com in Edge without using search or intermediate pages. Use the most direct grounded method.',
'The Example Domain page should already be open. Confirm briefly and do not propose any new actions.',
'exit'
],
expectations: [
{
name: 'status reports provider',
scope: 'transcript',
include: [/Provider:\s+copilot/i, /Copilot:\s+Authenticated/i]
},
{
name: 'recovery path retries with stricter formatting',
scope: 'transcript',
include: [/No actions detected for an automation-like request; retrying once with stricter formatting/i],
count: { pattern: /No actions detected for an automation-like request; retrying once with stricter formatting/i, exactly: 1 }
},
{
name: 'final recovery turn is concise and action-free',
turn: 2,
include: [/Confirmed/i],
exclude: [/"actions"\s*:/i, /```json/i]
}
]
},
'continuity-acknowledgement': {
description: 'Checks that acknowledgement/chit-chat after a satisfied automation exchange converges to a concise non-action reply.',
executeMode: 'false',
prompts: [
'/status',
'Open https://example.com in Edge without using search or intermediate pages. Use the most direct grounded method.',
'The Example Domain page should already be open. Confirm briefly and do not propose any new actions.',
'Thanks, that is perfect.',
'exit'
],
expectations: [
{
name: 'status reports provider',
scope: 'transcript',
include: [/Provider:\s+copilot/i, /Copilot:\s+Authenticated/i]
},
{
name: 'pre-ack turn is action-free confirmation',
turn: 2,
include: [/(Confirmed|Example( Domain)? page is not currently open|Example( Domain)? page is already open)/i],
exclude: [/"actions"\s*:/i, /```json/i]
},
{
name: 'acknowledgement turn stays conversational',
turn: 3,
include: [/(welcome|glad|any time|happy to help|perfect)/i],
exclude: [/"actions"\s*:/i, /```json/i, /screenshot/i, /confirmed/i]
}
]
},
'repo-boundary-clarification': {
description: 'Verifies that explicit repo corrections persist and the assistant asks for an explicit repo or window switch before MUSE-specific work.',
executeMode: 'false',
prompts: [
'/clear',
'MUSE is a different repo, this is copilot-liku-cli.',
'/state',
'What is the safest next step if I want to work on MUSE without mixing repos or windows? Reply briefly.',
'exit'
],
expectations: [
{
name: 'state command shows repo boundary context',
scope: 'transcript',
include: [/Current repo:\s+copilot-liku-cli/i, /Downstream repo intent:\s+muse/i]
},
{
name: 'repo correction is acknowledged against the current repo',
turn: 1,
include: [/(understood|got it|noted|different repo|separate repo)/i, /copilot-liku-cli/i]
},
{
name: 'follow-up requires an explicit repo or window switch',
turn: 2,
include: [/(switch|confirm|open|move)/i, /(repo|window|workspace)/i, /muse/i],
exclude: [/(we should|let'?s|go ahead and|next step is to)\s+(edit|patch|implement|change).{0,60}\bmuse\b/i]
}
]
},
'forgone-feature-suppression': {
description: 'Verifies that forgone features persist in session intent state and stay out of scope until explicitly re-enabled.',
executeMode: 'false',
prompts: [
'/clear',
'I have forgone the implementation of: terminal-liku ui.',
'/state',
'Should terminal-liku ui be part of the plan right now? Reply briefly.',
'exit'
],
expectations: [
{
name: 'state command shows forgone feature',
scope: 'transcript',
include: [/Forgone features:\s+terminal-liku ui/i]
},
{
name: 'follow-up keeps the forgone feature out of scope',
turn: 2,
include: [/(no|not right now|keep it out|should not)/i, /(forgone|re-?enable|explicitly re-enable|until you re-enable)/i],
exclude: [/(we should|let'?s|go ahead and|next step is to).{0,40}(implement|build|revive|restore).{0,40}(terminal-liku ui|terminal ui|hud)/i]
}
]
}
};
function getArgValue(flagName) {
const index = process.argv.indexOf(flagName);
if (index >= 0 && index + 1 < process.argv.length) {
return process.argv[index + 1];
}
return null;
}
function getArgValues(flagName) {
const value = getArgValue(flagName);
if (!value) return [];
return String(value)
.split(',')
.map((part) => part.trim())
.filter(Boolean);
}
function hasFlag(flagName) {
return process.argv.includes(flagName);
}
function normalizeRequestedModel(value) {
const normalized = String(value || '').trim();
return normalized || null;
}
function parseRequestedModels() {
const requested = [];
const single = normalizeRequestedModel(getArgValue('--model'));
if (single) requested.push(single);
for (const value of getArgValues('--models')) {
const normalized = normalizeRequestedModel(value);
if (normalized) requested.push(normalized);
}
return [...new Set(requested)];
}
function buildRequestedModelLabel(requestedModel) {
return requestedModel || 'default';
}
function buildProofInput(suite, requestedModel) {
const prompts = [];
if (requestedModel) {
prompts.push(`/model ${requestedModel}`);
}
prompts.push(...suite.prompts);
return `${prompts.join('\n')}\n`;
}
function ensureProofPaths() {
ensureLikuStructure();
if (!fs.existsSync(PROOF_TRACE_DIR)) {
fs.mkdirSync(PROOF_TRACE_DIR, { recursive: true, mode: 0o700 });
}
}
function listSuites() {
console.log('Available suites:');
for (const [name, suite] of Object.entries(SUITES)) {
console.log(`- ${name}: ${suite.description}`);
}
}
function resolveGlobalWindowsShim() {
const lookup = spawnSync('where.exe', ['liku.cmd'], {
cwd: REPO_ROOT,
encoding: 'utf8'
});
if (lookup.status !== 0) {
throw new Error('Could not resolve global liku.cmd with where.exe');
}
const candidates = String(lookup.stdout || '')
.split(/\r?\n/)
.map((line) => line.trim())
.filter(Boolean)
.filter((line) => !line.toLowerCase().startsWith(REPO_ROOT.toLowerCase()));
if (candidates.length === 0) {
throw new Error('No installed global liku.cmd found outside the repo root');
}
return candidates[0];
}
function buildCommand({ useGlobal, executeMode }) {
if (useGlobal) {
if (process.platform === 'win32') {
const globalShim = resolveGlobalWindowsShim();
const escapedShim = globalShim.replace(/'/g, "''");
return {
file: 'powershell',
args: ['-NoProfile', '-Command', `& '${escapedShim}' chat --execute ${executeMode}`]
};
}
return {
file: 'sh',
args: ['-lc', `liku chat --execute ${executeMode}`]
};
}
const cliPath = path.join(REPO_ROOT, 'src', 'cli', 'liku.js');
return {
file: process.execPath,
args: [cliPath, 'chat', '--execute', executeMode]
};
}
function renderSuiteHeader(name, suite, useGlobal, requestedModel) {
console.log('========================================');
console.log(` Inline Chat Proof: ${name}`);
console.log('========================================');
console.log(`Mode: ${useGlobal ? 'global liku command' : 'local workspace CLI'}`);
if (requestedModel) {
const shortcutSuffix = MODEL_SHORTCUTS.has(String(requestedModel).trim().toLowerCase()) ? ' (shortcut)' : '';
console.log(`Requested model: ${requestedModel}${shortcutSuffix}`);
}
console.log(`Goal: ${suite.description}`);
console.log('');
}
function extractAssistantTurns(transcript) {
const lines = String(transcript || '').split(/\r?\n/);
const turns = [];
let current = [];
let collecting = false;
for (const line of lines) {
if (/^\[copilot:/i.test(line.trim())) {
if (collecting && current.length > 0) {
turns.push(current.join('\n').trim());
}
collecting = true;
current = [];
continue;
}
if (!collecting) continue;
if (/^>\s/.test(line) || /^\[UI-WATCHER\]/.test(line) || /^PS\s/.test(line)) {
if (current.length > 0) {
turns.push(current.join('\n').trim());
}
collecting = false;
current = [];
continue;
}
current.push(line);
}
if (collecting && current.length > 0) {
turns.push(current.join('\n').trim());
}
return turns.filter(Boolean);
}
function evaluateTranscript(transcript, suite) {
const assistantTurns = extractAssistantTurns(transcript);
const results = [];
for (const expectation of suite.expectations) {
const targetText = expectation.scope === 'transcript'
? transcript
: assistantTurns[Math.max(0, Number(expectation.turn || 1) - 1)] || '';
const includePatterns = Array.isArray(expectation.include) ? expectation.include : [];
const excludePatterns = Array.isArray(expectation.exclude) ? expectation.exclude : [];
const countChecks = Array.isArray(expectation.count)
? expectation.count.filter(Boolean)
: (expectation.count ? [expectation.count] : []);
const missing = includePatterns.filter((pattern) => !pattern.test(targetText));
const forbidden = excludePatterns.filter((pattern) => pattern.test(targetText));
const countFailures = [];
for (const check of countChecks) {
if (!check.pattern) continue;
const flags = check.pattern.flags.includes('g') ? check.pattern.flags : `${check.pattern.flags}g`;
const matchCount = (targetText.match(new RegExp(check.pattern.source, flags)) || []).length;
if (Number.isFinite(check.exactly) && matchCount !== check.exactly) {
countFailures.push(`${check.pattern} expected exactly ${check.exactly}, got ${matchCount}`);
continue;
}
if (Number.isFinite(check.min) && matchCount < check.min) {
countFailures.push(`${check.pattern} expected at least ${check.min}, got ${matchCount}`);
}
if (Number.isFinite(check.max) && matchCount > check.max) {
countFailures.push(`${check.pattern} expected at most ${check.max}, got ${matchCount}`);
}
}
const passed = missing.length === 0 && forbidden.length === 0 && countFailures.length === 0;
results.push({
name: expectation.name,
passed,
missing,
forbidden,
countFailures,
turn: expectation.turn || null
});
}
return {
passed: results.every((result) => result.passed),
results
};
}
function printEvaluation(evaluation) {
console.log('');
console.log('Evaluation:');
for (const result of evaluation.results) {
if (result.passed) {
console.log(`PASS ${result.name}`);
continue;
}
console.log(`FAIL ${result.name}`);
if (result.missing.length > 0) {
console.log(` Missing: ${result.missing.map((pattern) => pattern.toString()).join(', ')}`);
}
if (result.forbidden.length > 0) {
console.log(` Forbidden: ${result.forbidden.map((pattern) => pattern.toString()).join(', ')}`);
}
if (result.countFailures.length > 0) {
console.log(` Count: ${result.countFailures.join('; ')}`);
}
}
}
function extractObservedModelHeaders(transcript) {
const lines = String(transcript || '').split(/\r?\n/);
const runtimeModels = [];
const requestedModels = [];
const providers = [];
for (const line of lines) {
const match = String(line || '').trim().match(/^\[([^:\]]+)(?::([^\]\s]+))?(?: via ([^\]]+))?\]$/);
if (!match) continue;
const provider = match[1] || null;
const runtimeModel = match[2] || null;
const requestedModel = match[3] || runtimeModel || null;
if (provider && !providers.includes(provider)) providers.push(provider);
if (runtimeModel && !runtimeModels.includes(runtimeModel)) runtimeModels.push(runtimeModel);
if (requestedModel && !requestedModels.includes(requestedModel)) requestedModels.push(requestedModel);
}
return {
providers,
runtimeModels,
requestedModels
};
}
function sanitizeName(name) {
return String(name || 'suite').replace(/[^a-z0-9._-]+/gi, '-').toLowerCase();
}
function persistRunResult({ suiteName, suite, useGlobal, evaluation, exitCode, transcript, requestedModel }) {
ensureProofPaths();
const timestamp = new Date().toISOString();
const stamp = timestamp.replace(/[:.]/g, '-');
const tracePath = path.join(PROOF_TRACE_DIR, `${stamp}-${sanitizeName(suiteName)}.log`);
fs.writeFileSync(tracePath, transcript, 'utf8');
const observedModels = extractObservedModelHeaders(transcript);
const payload = {
timestamp,
suite: suiteName,
description: suite.description,
mode: useGlobal ? 'global' : 'local',
executeMode: suite.executeMode || 'false',
requestedModel: buildRequestedModelLabel(requestedModel),
observedRuntimeModels: observedModels.runtimeModels,
observedRequestedModels: observedModels.requestedModels,
providers: observedModels.providers,
passed: exitCode === 0 && evaluation.passed,
exitCode,
failures: evaluation.results
.filter((result) => !result.passed)
.map((result) => ({
name: result.name,
missing: result.missing.map((pattern) => pattern.toString()),
forbidden: result.forbidden.map((pattern) => pattern.toString()),
countFailures: result.countFailures
})),
tracePath
};
fs.appendFileSync(PROOF_RESULT_LOG, `${JSON.stringify(payload)}\n`, 'utf8');
console.log(`Saved proof result: ${tracePath}`);
}
async function runSuite(name, suite, useGlobal, requestedModel) {
const command = buildCommand({ useGlobal, executeMode: suite.executeMode || 'false' });
renderSuiteHeader(name, suite, useGlobal, requestedModel);
const child = spawn(command.file, command.args, {
cwd: REPO_ROOT,
stdio: ['pipe', 'pipe', 'pipe'],
env: process.env
});
let transcript = '';
child.stdout.on('data', (data) => {
const text = data.toString();
transcript += text;
process.stdout.write(text);
});
child.stderr.on('data', (data) => {
const text = data.toString();
transcript += text;
process.stdout.write(text);
});
const payload = buildProofInput(suite, requestedModel);
child.stdin.write(payload);
child.stdin.end();
const exitCode = await new Promise((resolve) => child.on('close', resolve));
const evaluation = evaluateTranscript(transcript, suite);
printEvaluation(evaluation);
if (!hasFlag('--no-save')) {
persistRunResult({ suiteName: name, suite, useGlobal, evaluation, exitCode, transcript, requestedModel });
}
if (exitCode !== 0) {
console.error(`\nChat process exited with code ${exitCode}`);
}
return exitCode === 0 && evaluation.passed;
}
async function main() {
if (hasFlag('--list-suites')) {
listSuites();
return;
}
const runAll = hasFlag('--all');
const suiteName = getArgValue('--suite') || 'direct-navigation';
const useGlobal = hasFlag('--global');
const requestedModels = parseRequestedModels();
const suiteEntries = runAll
? Object.entries(SUITES)
: [[suiteName, SUITES[suiteName]]];
if (suiteEntries.some(([, suite]) => !suite)) {
console.error(`Unknown suite: ${suiteName}`);
console.error(`Available suites: ${Object.keys(SUITES).join(', ')}`);
process.exit(1);
}
let allPassed = true;
const modelEntries = requestedModels.length > 0 ? requestedModels : [null];
for (const requestedModel of modelEntries) {
for (const [name, suite] of suiteEntries) {
const passed = await runSuite(name, suite, useGlobal, requestedModel);
allPassed = allPassed && passed;
}
}
if (!allPassed) {
process.exit(1);
}
}
if (require.main === module) {
main().catch((error) => {
console.error(error.stack || error.message);
process.exit(1);
});
}
module.exports = {
SUITES,
evaluateTranscript,
extractAssistantTurns,
extractObservedModelHeaders,
buildProofInput,
buildRequestedModelLabel,
parseRequestedModels
};