carlos-flow 2.11.3 → 2.12.1
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/CHANGELOG.md +47 -0
- package/TRIGGER_GUIDE.md +783 -783
- package/bridge/mcp-bridge.js +203 -203
- package/check-all.sh +0 -0
- package/ghost.json +26 -1
- package/package.json +113 -113
- package/plugins/carlos-flow-trigger.js +215 -215
- package/scripts/check-no-dead-handler.mjs +2 -2
- package/scripts/check-templates.mjs +126 -126
- package/scripts/clean-readme-noise.mjs +53 -53
- package/scripts/install-hooks.mjs +166 -153
- package/scripts/lang-audit.mjs +172 -172
- package/scripts/mutation-check.mjs +483 -483
- package/scripts/publish-gate.mjs +24 -3
- package/scripts/sync-readme.mjs +102 -102
- package/scripts/verify-activation-e2e.sh +55 -55
- package/smoke-test.js +2 -2
- package/src/card.js +296 -288
- package/src/dispatch.js +557 -548
- package/src/index.js +1713 -1686
- package/src/profiles.js +17 -0
- package/src/protocol.js +95 -85
- package/src/state.js +930 -898
package/src/index.js
CHANGED
|
@@ -1,1686 +1,1713 @@
|
|
|
1
|
-
// carlos-flow MCP server — stdio transport.
|
|
2
|
-
// Carlos 工作流的机械门禁工具集(留痕 / 派发 / 裁定 / 审计 / 快照还原计划)。
|
|
3
|
-
// 重要:stdout 是 MCP 传输通道,所有日志一律走 stderr。
|
|
4
|
-
import { spawnSync } from "node:child_process";
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
//
|
|
27
|
-
//
|
|
28
|
-
//
|
|
29
|
-
//
|
|
30
|
-
//
|
|
31
|
-
// · payload
|
|
32
|
-
//
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
//
|
|
44
|
-
//
|
|
45
|
-
//
|
|
46
|
-
//
|
|
47
|
-
//
|
|
48
|
-
//
|
|
49
|
-
//
|
|
50
|
-
// ·
|
|
51
|
-
// ·
|
|
52
|
-
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
//
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
if (lv
|
|
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
|
-
if (fpNow.
|
|
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
|
-
if (a
|
|
125
|
-
if (a.
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
out.
|
|
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
|
-
const
|
|
193
|
-
const
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
if (
|
|
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
|
-
const
|
|
234
|
-
const
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
audit("
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
//
|
|
247
|
-
//
|
|
248
|
-
//
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
const
|
|
257
|
-
|
|
258
|
-
...(!
|
|
259
|
-
...(!
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
//
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
//
|
|
268
|
-
//
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
const
|
|
272
|
-
const
|
|
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
|
-
audit("
|
|
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
|
-
const
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
const
|
|
392
|
-
const
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
// claim
|
|
411
|
-
state.claimed
|
|
412
|
-
state.
|
|
413
|
-
state.
|
|
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
|
-
const
|
|
465
|
-
|
|
466
|
-
if (
|
|
467
|
-
if (state
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
state.
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
state.
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
const
|
|
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
|
-
const
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
const
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
const
|
|
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
|
-
const
|
|
588
|
-
|
|
589
|
-
state.
|
|
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
|
-
const
|
|
629
|
-
const
|
|
630
|
-
|
|
631
|
-
//
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
"DIRECTION
|
|
649
|
-
"
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
const
|
|
664
|
-
const
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
//
|
|
672
|
-
//
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
//
|
|
685
|
-
//
|
|
686
|
-
//
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
//
|
|
699
|
-
//
|
|
700
|
-
//
|
|
701
|
-
//
|
|
702
|
-
//
|
|
703
|
-
//
|
|
704
|
-
//
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
const
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
//
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
"
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
//
|
|
750
|
-
//
|
|
751
|
-
//
|
|
752
|
-
//
|
|
753
|
-
//
|
|
754
|
-
//
|
|
755
|
-
//
|
|
756
|
-
//
|
|
757
|
-
//
|
|
758
|
-
//
|
|
759
|
-
|
|
760
|
-
const
|
|
761
|
-
const
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
//
|
|
775
|
-
//
|
|
776
|
-
//
|
|
777
|
-
//
|
|
778
|
-
//
|
|
779
|
-
//
|
|
780
|
-
//
|
|
781
|
-
//
|
|
782
|
-
//
|
|
783
|
-
//
|
|
784
|
-
|
|
785
|
-
const
|
|
786
|
-
const
|
|
787
|
-
const
|
|
788
|
-
const
|
|
789
|
-
const
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
"
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
//
|
|
811
|
-
//
|
|
812
|
-
//
|
|
813
|
-
//
|
|
814
|
-
// ·
|
|
815
|
-
//
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
//
|
|
837
|
-
//
|
|
838
|
-
//
|
|
839
|
-
//
|
|
840
|
-
//
|
|
841
|
-
//
|
|
842
|
-
//
|
|
843
|
-
//
|
|
844
|
-
//
|
|
845
|
-
//
|
|
846
|
-
// ·
|
|
847
|
-
|
|
848
|
-
const
|
|
849
|
-
const
|
|
850
|
-
const
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
const
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
state.
|
|
884
|
-
state.
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
const
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
//
|
|
900
|
-
//
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
if (
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
name: "
|
|
915
|
-
description:
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
//
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
}
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
if (a.
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
}
|
|
1037
|
-
},
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
}
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
}
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
}
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
reason:
|
|
1136
|
-
|
|
1137
|
-
}
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
}
|
|
1156
|
-
},
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
}
|
|
1251
|
-
},
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
const
|
|
1270
|
-
const
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
}
|
|
1324
|
-
|
|
1325
|
-
const
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
}
|
|
1388
|
-
},
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
}
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
const
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
if (
|
|
1476
|
-
return ok({
|
|
1477
|
-
...base,
|
|
1478
|
-
activated: false,
|
|
1479
|
-
pending_fields:
|
|
1480
|
-
note:
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
if (
|
|
1500
|
-
return ok({
|
|
1501
|
-
...base,
|
|
1502
|
-
activated: false,
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
//
|
|
1589
|
-
|
|
1590
|
-
//
|
|
1591
|
-
//
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
//
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
}
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1
|
+
// carlos-flow MCP server — stdio transport.
|
|
2
|
+
// Carlos 工作流的机械门禁工具集(留痕 / 派发 / 裁定 / 审计 / 快照还原计划)。
|
|
3
|
+
// 重要:stdout 是 MCP 传输通道,所有日志一律走 stderr。
|
|
4
|
+
import { spawnSync } from "node:child_process";
|
|
5
|
+
import { readFileSync } from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { createServer, connect } from "node:net";
|
|
8
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
9
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
10
|
+
import { ListToolsRequestSchema, CallToolRequestSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
11
|
+
import { PROTOCOL } from "./protocol.js";
|
|
12
|
+
import { parseCard, validateCard, riskTier, isSensitive } from "./card.js";
|
|
13
|
+
import { loadState, saveState, defaultState, audit, readAudit, getUserMessages, writeUserMessage, pathExists, gitHead, isStale, isBypassed, verifyAuditChain, withStateLock, auditHashMode, workspaceFingerprint } from "./state.js";
|
|
14
|
+
import { buildContractPrompt, snapshotFiles, readManifest, readManifestAll, writeDispatchPrompt, saveAgentResult, runVerifyCommand, runNegativeControl, computeOutputHash, enforceScope, MAX_DISPATCH } from "./dispatch.js";
|
|
15
|
+
|
|
16
|
+
const log = (msg) => {
|
|
17
|
+
try {
|
|
18
|
+
process.stderr.write(`[carlos-flow] ${msg}\n`);
|
|
19
|
+
} catch {
|
|
20
|
+
/* stderr 不可写时静默 */
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// ---- 通用工具函数 ----------------------------------------------------------
|
|
25
|
+
|
|
26
|
+
// 成功响应:统一注入 ok:true。
|
|
27
|
+
//
|
|
28
|
+
// 之前各工具的 payload 有的显式写 \`ok: true\`、有的没写(如 flow_audit),
|
|
29
|
+
// 于是调用方拿到手的心智模型不一致 —— 想判断成败只能逐个工具记「这个有没有 ok」。
|
|
30
|
+
// 现在由 ok() 统一注入:
|
|
31
|
+
// · payload 里没有 ok 字段 → 补上 ok:true
|
|
32
|
+
// · payload 里已有 ok 字段 → 保留原值(不改 ok:false 的语义)
|
|
33
|
+
// 这是**增量兼容**:现有调用方读取 total / events / by_type 等字段不受影响。
|
|
34
|
+
const ok = (obj) => {
|
|
35
|
+
const payload = obj && typeof obj === "object" && !Array.isArray(obj) ? obj : { value: obj };
|
|
36
|
+
const withOk = "ok" in payload ? payload : { ok: true, ...payload };
|
|
37
|
+
return { content: [{ type: "text", text: JSON.stringify(withOk, null, 2) }] };
|
|
38
|
+
};
|
|
39
|
+
const fail = (message) => ({ content: [{ type: "text", text: "ERROR: " + String(message) }], isError: true });
|
|
40
|
+
|
|
41
|
+
const asStringArray = (v) => (Array.isArray(v) ? v.map((x) => String(x)) : []);
|
|
42
|
+
|
|
43
|
+
// 卡片文本字段(scope / acceptance / unknowns / assumptions)**保留原始元素类型**,
|
|
44
|
+
// 不做任何过滤/转换。
|
|
45
|
+
// 背景:早先用 asStringArray(String(x))会把 null→「null」、{}→「[object Object]」,
|
|
46
|
+
// 垃圾串被当成合法验收标准;改成 asTextArray(filter 掉非字符串)虽然堵住了垃圾串,
|
|
47
|
+
// 但又让「[null, \"x\"]」静默变成合法的 [\"x\"] —— 严格口径根本没机会判定。
|
|
48
|
+
// 现在的做法:**两条入口都保留原样**,由 card.js 的必填校验统一判定
|
|
49
|
+
// (文本类数组必须每个元素都是非空字符串)。这样:
|
|
50
|
+
// · 口径唯一,两条入口(结构化 / cardText)行为一致;
|
|
51
|
+
// · 含无效元素的卡片被**明确拒绝**,而不是静默降级或静默过滤;
|
|
52
|
+
// · 无效元素不会落盘、更不会进入 executor 的合同文本。
|
|
53
|
+
const asRawArray = (v) => (Array.isArray(v) ? v : []);
|
|
54
|
+
const asObject = (v) => (v && typeof v === "object" && !Array.isArray(v) ? v : {});
|
|
55
|
+
|
|
56
|
+
// 卡片里声明的验证命令(归一化后用于比较)
|
|
57
|
+
const cardVerifyCmd = (state) => {
|
|
58
|
+
const c = state && state.card && state.card.verify;
|
|
59
|
+
return c && typeof c.cmd === "string" ? c.cmd.trim() : "";
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// 真实验证是否已通过:必须存在 flow_run_verify 写入的 last_verify、ok===true,
|
|
63
|
+
// 且验证的正是**当前卡片**的 verify.cmd(防止拿旧卡的验证结果冒充新卡)。
|
|
64
|
+
const verifyPassed = (state) => {
|
|
65
|
+
const lv = state && state.last_verify;
|
|
66
|
+
if (!lv || typeof lv !== "object") return false;
|
|
67
|
+
if (lv.ok !== true) return false;
|
|
68
|
+
const want = cardVerifyCmd(state);
|
|
69
|
+
if (!want) return false;
|
|
70
|
+
return typeof lv.cmd === "string" && lv.cmd.trim() === want;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// 验证状态四态(R4:保持 verify_passed 为 boolean,只**新增**本字段)。
|
|
74
|
+
//
|
|
75
|
+
// not_run —— 没跑过 / 没有对应当前卡片的验证结果
|
|
76
|
+
// fail —— 跑了但没通过
|
|
77
|
+
// stale —— 跑通了,但验证之后代码又变了(指纹不一致)—— 这一态正是 A1 要堵的
|
|
78
|
+
// pass —— 跑通且代码自那以后没动过
|
|
79
|
+
//
|
|
80
|
+
// 指纹不可用时(非 git 目录)按 fail-open 处理:算 pass,但在 fingerprint 字段里
|
|
81
|
+
// 如实标注「不可用」,不装作验证过(A1 R1)。
|
|
82
|
+
const verifyState = (state) => {
|
|
83
|
+
const lv = state && state.last_verify;
|
|
84
|
+
if (!lv || typeof lv !== "object") return { state: "not_run", fingerprint: "unavailable" };
|
|
85
|
+
const want = cardVerifyCmd(state);
|
|
86
|
+
if (!want || typeof lv.cmd !== "string" || lv.cmd.trim() !== want) {
|
|
87
|
+
return { state: "not_run", fingerprint: "unavailable" };
|
|
88
|
+
}
|
|
89
|
+
if (lv.ok !== true) return { state: "fail", fingerprint: "unavailable" };
|
|
90
|
+
|
|
91
|
+
const fpWas = lv.fingerprint;
|
|
92
|
+
if (!fpWas || !fpWas.ok) {
|
|
93
|
+
return { state: "pass", fingerprint: "unavailable" };
|
|
94
|
+
}
|
|
95
|
+
const fpNow = workspaceFingerprint(lv.cwd || process.cwd());
|
|
96
|
+
if (!fpNow.ok) return { state: "pass", fingerprint: "unavailable" };
|
|
97
|
+
if (fpNow.diff_sha256 !== fpWas.diff_sha256) {
|
|
98
|
+
return { state: "stale", fingerprint: fpNow.diff_sha256 };
|
|
99
|
+
}
|
|
100
|
+
return { state: "pass", fingerprint: fpNow.diff_sha256 };
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
// WARN 汇总条数上限:防止 WARN 通胀把返回值灌成噪音,主 Agent 反而开始忽略。
|
|
104
|
+
const WARN_FINDINGS_LIMIT = 20;
|
|
105
|
+
|
|
106
|
+
// 汇总两轴的 WARN 结论(坏味道等 judgement call)。
|
|
107
|
+
//
|
|
108
|
+
// 为什么即时派生、而不是再存一份进 state:
|
|
109
|
+
// WARN 数据本来就在 state.audit_axes 里;再落一份进 state 就得 bump
|
|
110
|
+
// schemaVersion 并写迁移分支,而派生是零迁移、零漂移的。
|
|
111
|
+
//
|
|
112
|
+
// 口径与双轴门禁完全一致(复用 axisOk 的 card_ts 绑定):
|
|
113
|
+
// 只认 card_ts 与当前卡一致的轴结论 —— 换卡后旧卡的 WARN 必须失效,
|
|
114
|
+
// 否则「门禁判未齐、汇总却报 WARN」两种口径会打架。
|
|
115
|
+
//
|
|
116
|
+
// 附带回答 DIRECTION/LOCAL 路径:返工意见恰恰最需要坏味道清单,
|
|
117
|
+
// 所以三种裁定都带这份汇总。
|
|
118
|
+
function collectAxisWarnings(state) {
|
|
119
|
+
const ax = state && state.audit_axes;
|
|
120
|
+
if (!ax || typeof ax !== "object") return [];
|
|
121
|
+
const out = [];
|
|
122
|
+
for (const name of ["standards", "spec"]) {
|
|
123
|
+
const a = ax[name];
|
|
124
|
+
if (!a || !a.verdict) continue;
|
|
125
|
+
if (a.card_ts !== state.card_ts) continue; // 旧卡结论不算数
|
|
126
|
+
if (a.verdict !== "WARN") continue;
|
|
127
|
+
const findings = Array.isArray(a.findings) ? a.findings : [];
|
|
128
|
+
for (const f of findings) {
|
|
129
|
+
if (out.length >= WARN_FINDINGS_LIMIT) return out;
|
|
130
|
+
out.push({ axis: name, finding: String(f) });
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return out;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const TOOLS = [
|
|
137
|
+
// 1 -----------------------------------------------------------------------
|
|
138
|
+
{
|
|
139
|
+
name: "flow_protocol",
|
|
140
|
+
description:
|
|
141
|
+
"读取 Carlos 工作流协议全文(路由档位 T0/T1/T2、四段合同、报告规格、错误分层、状态字段、审计事件类型、免检触发词、任务卡模板)。" +
|
|
142
|
+
"当你不确定该走哪一档、任务卡该怎么写、executor/auditor 报告格式或门禁规则时,先调用本工具拿到权威定义,不要凭记忆猜。",
|
|
143
|
+
inputSchema: {
|
|
144
|
+
type: "object",
|
|
145
|
+
properties: {},
|
|
146
|
+
},
|
|
147
|
+
handler: async () => ok({ protocol: PROTOCOL }),
|
|
148
|
+
},
|
|
149
|
+
|
|
150
|
+
// 2 -----------------------------------------------------------------------
|
|
151
|
+
{
|
|
152
|
+
name: "flow_submit_requirement",
|
|
153
|
+
description:
|
|
154
|
+
"提交(留痕)一张任务卡,作为后续派发的唯一依据。任何 T2 改动前必须先调用本工具。" +
|
|
155
|
+
"可以传结构化参数(goal/quote/scope/acceptance/unknowns/assumptions/files/verify/confirmed)," +
|
|
156
|
+
"也可以直接传 cardText(形如 FLOW_CARD:{...} 的单行 JSON)。" +
|
|
157
|
+
"本工具会做锚点校验(quote 必须能在用户原话里定位)、scope 路径存在性校验、必填字段校验、敏感路径确认;" +
|
|
158
|
+
"校验不通过会返回 ok:false 与 findings,此时不得派发。返回 ok:true 表示留痕成功,可以继续调 flow_claim。",
|
|
159
|
+
inputSchema: {
|
|
160
|
+
type: "object",
|
|
161
|
+
properties: {
|
|
162
|
+
goal: { type: "string", description: "一句话目标(任务卡 objective)" },
|
|
163
|
+
quote: { type: "string", description: "用户原话摘录,用于锚点校验,必须能在用户消息中定位" },
|
|
164
|
+
scope: { type: "array", items: { type: "string" }, description: "只准改动的文件清单(相对 cwd 的路径)" },
|
|
165
|
+
acceptance: { type: "array", items: { type: "string" }, description: "可判定的验收标准" },
|
|
166
|
+
unknowns: { type: "array", items: { type: "string" }, description: "未知/待澄清事项" },
|
|
167
|
+
assumptions: { type: "array", items: { type: "string" }, description: "已做的假设" },
|
|
168
|
+
files: {
|
|
169
|
+
type: "array",
|
|
170
|
+
description: "关键定位点,每项需带 path 与 line",
|
|
171
|
+
items: {
|
|
172
|
+
type: "object",
|
|
173
|
+
properties: {
|
|
174
|
+
path: { type: "string" },
|
|
175
|
+
line: { type: "number" },
|
|
176
|
+
},
|
|
177
|
+
required: ["path", "line"],
|
|
178
|
+
},
|
|
179
|
+
},
|
|
180
|
+
verify: { type: "object", properties: { cmd: { type: "string" } }, description: "主 Agent 指定的验证命令" },
|
|
181
|
+
confirmed: { type: "boolean", description: "敏感路径是否已获用户确认(sensitive 档必须为 true)" },
|
|
182
|
+
parallel_plan: { type: "string", description: "并行派发计划说明(可选)" },
|
|
183
|
+
sessionId: { type: "string", description: "会话 ID,默认 unknown" },
|
|
184
|
+
cwd: { type: "string", description: "工作目录,默认 process.cwd()" },
|
|
185
|
+
userMessages: { type: "array", items: { type: "string" }, description: "用户原话列表(可选,会与自动读取的结果合并)" },
|
|
186
|
+
cardText: { type: "string", description: "完整任务卡文本,含 FLOW_CARD: 前缀;提供时优先于结构化参数" },
|
|
187
|
+
activation: { type: "boolean", description: "true = 这是智能触发引擎自动开的占位卡;在 scope/acceptance/verify.cmd 补齐前会阻止派发" },
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
handler: async (args) => {
|
|
191
|
+
try {
|
|
192
|
+
const a = args || {};
|
|
193
|
+
const sessionId = a.sessionId || "unknown";
|
|
194
|
+
const cwd = a.cwd || process.cwd();
|
|
195
|
+
|
|
196
|
+
let card = null;
|
|
197
|
+
if (typeof a.cardText === "string" && a.cardText.trim() !== "") {
|
|
198
|
+
const parsed = parseCard(a.cardText);
|
|
199
|
+
if (parsed && parsed.error) return fail(parsed.error);
|
|
200
|
+
if (!parsed || !parsed.card) return fail("FLOW_CARD 解析失败:请按模板重写(单行 JSON,英文双引号)。");
|
|
201
|
+
card = parsed.card;
|
|
202
|
+
} else {
|
|
203
|
+
card = {
|
|
204
|
+
goal: a.goal,
|
|
205
|
+
quote: a.quote,
|
|
206
|
+
scope: asRawArray(a.scope),
|
|
207
|
+
acceptance: asRawArray(a.acceptance),
|
|
208
|
+
unknowns: asRawArray(a.unknowns),
|
|
209
|
+
assumptions: asRawArray(a.assumptions),
|
|
210
|
+
files: Array.isArray(a.files) ? a.files : [],
|
|
211
|
+
verify: asObject(a.verify),
|
|
212
|
+
confirmed: a.confirmed === true,
|
|
213
|
+
};
|
|
214
|
+
if (typeof a.parallel_plan === "string" && a.parallel_plan !== "") card.parallel_plan = a.parallel_plan;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// 锚点数据源:调用方传入的 userMessages + 自动从 opencode SQLite 读取的用户原话
|
|
218
|
+
let messages = asStringArray(a.userMessages);
|
|
219
|
+
try {
|
|
220
|
+
const fetched = await getUserMessages(sessionId, 20);
|
|
221
|
+
if (Array.isArray(fetched)) messages = messages.concat(fetched);
|
|
222
|
+
} catch (e) {
|
|
223
|
+
log(`getUserMessages 失败:${e && e.message ? e.message : e}`);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const options = {
|
|
227
|
+
userMessages: messages,
|
|
228
|
+
scopeExists: (p) => pathExists(cwd, p),
|
|
229
|
+
// 走 cardText 提交的卡片,其自身的 confirmed 字段同样必须生效,否则敏感路径永远无法通过
|
|
230
|
+
confirmed: (a.confirmed === true) || card.confirmed === true,
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
const tier = riskTier(card.scope);
|
|
234
|
+
const result = validateCard(card, options);
|
|
235
|
+
const findings = (result && Array.isArray(result.findings)) ? result.findings : [];
|
|
236
|
+
|
|
237
|
+
if (!result || result.ok !== true) {
|
|
238
|
+
const reason = (result && result.reason) || "卡片不合规";
|
|
239
|
+
for (const f of findings) audit("finding", sessionId, { kind: "card_finding", detail: f, tier });
|
|
240
|
+
audit("decision", sessionId, { kind: "blocked", reason, tier });
|
|
241
|
+
return ok({ ok: false, reason, findings, tier });
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
card.env = { git_head: gitHead(cwd), project: cwd };
|
|
245
|
+
|
|
246
|
+
// ---- 闸门待补项:服务端从当前卡片内容**幂等派生** ----
|
|
247
|
+
// 内容判定:与 validateCard 的必填字段口径**严格一致**(两边都要求
|
|
248
|
+
// 「每个元素都是非空字符串」)。acceptance=[""] / [null] / ["a",null] 这类
|
|
249
|
+
// 含无效元素的伪填充,既不算卡片合规,也不算闸门已补齐 —— 统一卡严,避免绕过。
|
|
250
|
+
const cardHasScope = Array.isArray(card.scope) && card.scope.length > 0 && card.scope.every(
|
|
251
|
+
(p) => typeof p === "string" && p.trim() !== "" && p !== "." && p !== ".."
|
|
252
|
+
);
|
|
253
|
+
const cardHasAcceptance = Array.isArray(card.acceptance) && card.acceptance.length > 0 && card.acceptance.every(
|
|
254
|
+
(x) => typeof x === "string" && x.trim() !== ""
|
|
255
|
+
);
|
|
256
|
+
const cardHasVerify = !!(card.verify && typeof card.verify.cmd === "string" && card.verify.cmd.trim() !== "");
|
|
257
|
+
const currentMissing = [
|
|
258
|
+
...(!cardHasScope ? ["scope"] : []),
|
|
259
|
+
...(!cardHasAcceptance ? ["acceptance"] : []),
|
|
260
|
+
...(!cardHasVerify ? ["verify.cmd"] : []),
|
|
261
|
+
];
|
|
262
|
+
|
|
263
|
+
// activation 标记既可能来自工具参数(结构化提交),也可能来自卡片本身(cardText 提交)。
|
|
264
|
+
// 两条路径必须一致,否则 cardText 路径可以完全绕开闸门。
|
|
265
|
+
const isActivation = a.activation === true || card.activation === true;
|
|
266
|
+
|
|
267
|
+
// ---- 读-改-写放进跨进程排他锁 ----
|
|
268
|
+
// 多个 MCP server 进程共享同一 CARLOS_FLOW_HOME 时,未加锁会丢更新:
|
|
269
|
+
// 后写入者用过期 prevState 算出 pending=[],而它写的卡片字段仍为空 ⇒ 闸门被绕过。
|
|
270
|
+
const gated = withStateLock(sessionId, () => {
|
|
271
|
+
const prev = loadState(sessionId);
|
|
272
|
+
const wasActivatedBefore = prev.activation_activated === true;
|
|
273
|
+
const prevPendingBefore = Array.isArray(prev.activation_pending) ? prev.activation_pending : [];
|
|
274
|
+
|
|
275
|
+
let activationPending;
|
|
276
|
+
if (isActivation) {
|
|
277
|
+
// 显式占位卡:待补项 = 服务端从这张卡实际缺什么推导出来的。
|
|
278
|
+
activationPending = currentMissing;
|
|
279
|
+
} else if (wasActivatedBefore) {
|
|
280
|
+
// 该会话开过占位卡:继续按**当前卡片实际缺什么**锁定。
|
|
281
|
+
// 幂等派生(而不是「上一轮 pending 传递」),所以即使并发丢更新,
|
|
282
|
+
// 落盘的 pending 永远与同一份落盘卡片一致,不会出现「卡片空却 pending=[]」。
|
|
283
|
+
activationPending = currentMissing;
|
|
284
|
+
} else {
|
|
285
|
+
// 普通卡(未标 activation,且本会话从未激活过):不上闸门。
|
|
286
|
+
// 档位必填项已由 validateCard 按 tier 强制(read_only 只要 goal+quote),
|
|
287
|
+
// 此处再加闸门会误伤 T1 只读调研。
|
|
288
|
+
activationPending = [];
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const state = {
|
|
292
|
+
...defaultState(),
|
|
293
|
+
...prev,
|
|
294
|
+
card,
|
|
295
|
+
card_ts: Date.now(),
|
|
296
|
+
tier,
|
|
297
|
+
dispatch_count: 0,
|
|
298
|
+
claimed: false,
|
|
299
|
+
claimed_role: null,
|
|
300
|
+
write_files: 0,
|
|
301
|
+
last_user_ts: Date.now(),
|
|
302
|
+
bypass: false,
|
|
303
|
+
verdict: null,
|
|
304
|
+
completed_ok: false,
|
|
305
|
+
stale: false,
|
|
306
|
+
activation_pending: activationPending,
|
|
307
|
+
// 单调标记:一旦激活过就永不清除
|
|
308
|
+
activation_activated: isActivation || wasActivatedBefore,
|
|
309
|
+
};
|
|
310
|
+
saveState(sessionId, state);
|
|
311
|
+
return { state, activationPending, wasActivatedBefore, prevPendingBefore };
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
const { state, activationPending, wasActivatedBefore, prevPendingBefore } = gated;
|
|
315
|
+
|
|
316
|
+
// 闸门事件:建立 / 保持 / 解除,都要在审计链里留痕。
|
|
317
|
+
if (isActivation) {
|
|
318
|
+
audit("decision", sessionId, {
|
|
319
|
+
kind: "activation_gate_activated",
|
|
320
|
+
reason: "智能触发占位卡激活(服务端从卡片内容派生待补字段)",
|
|
321
|
+
pending: activationPending,
|
|
322
|
+
});
|
|
323
|
+
} else if (wasActivatedBefore && prevPendingBefore.length > 0 && activationPending.length === 0) {
|
|
324
|
+
// 解锁事件统一由「pending 由非空变空」触发,避免被误记成 activated。
|
|
325
|
+
audit("decision", sessionId, {
|
|
326
|
+
kind: "activation_gate_unlocked",
|
|
327
|
+
reason: "字段已补齐,激活闸门解除",
|
|
328
|
+
released: prevPendingBefore,
|
|
329
|
+
});
|
|
330
|
+
} else if (wasActivatedBefore && activationPending.length > 0) {
|
|
331
|
+
audit("decision", sessionId, {
|
|
332
|
+
kind: "activation_gate_held",
|
|
333
|
+
reason: "字段尚未补齐,激活闸门继续保持",
|
|
334
|
+
pending: activationPending,
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
audit("run", sessionId, { kind: "record", tier, goal: card.goal });
|
|
339
|
+
audit("decision", sessionId, { kind: "accepted", tier, goal: card.goal });
|
|
340
|
+
for (const f of findings) audit("finding", sessionId, { kind: "card_finding", detail: f, tier });
|
|
341
|
+
|
|
342
|
+
// 激活占位卡建立 / 闸门解除都要在审计链里留专属事件,
|
|
343
|
+
// 否则事后无法从 gate-audit 流水回答「这是不是占位卡、有没有解闸过」。
|
|
344
|
+
if (isActivation) {
|
|
345
|
+
audit("run", sessionId, {
|
|
346
|
+
kind: "activation",
|
|
347
|
+
tier,
|
|
348
|
+
goal: card.goal,
|
|
349
|
+
pending: activationPending,
|
|
350
|
+
});
|
|
351
|
+
} else if (wasActivatedBefore && prevPendingBefore.length > 0 && activationPending.length === 0) {
|
|
352
|
+
audit("decision", sessionId, {
|
|
353
|
+
kind: "activation_released",
|
|
354
|
+
tier,
|
|
355
|
+
goal: card.goal,
|
|
356
|
+
released: prevPendingBefore,
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
const sensitivePaths = (Array.isArray(card.scope) ? card.scope : []).filter((p) => isSensitive(p));
|
|
361
|
+
|
|
362
|
+
return ok({ ok: true, tier, findings, card, state, sensitive: sensitivePaths });
|
|
363
|
+
} catch (e) {
|
|
364
|
+
return fail(e && e.message ? e.message : String(e));
|
|
365
|
+
}
|
|
366
|
+
},
|
|
367
|
+
},
|
|
368
|
+
|
|
369
|
+
// 3 -----------------------------------------------------------------------
|
|
370
|
+
{
|
|
371
|
+
name: "flow_claim",
|
|
372
|
+
description:
|
|
373
|
+
"领取一次派发名额(executor 或 auditor)。返回四段合同 prompt,可直接作为子 Agent 的任务描述。" +
|
|
374
|
+
"前置条件:必须有 flow_submit_requirement 留下的卡;卡过期(stale)或派发次数达到上限会直接报错。" +
|
|
375
|
+
"每次 claim 都会消耗一次派发额度,重复 claim 前先想清楚是否必要。",
|
|
376
|
+
inputSchema: {
|
|
377
|
+
type: "object",
|
|
378
|
+
properties: {
|
|
379
|
+
sessionId: { type: "string", description: "会话 ID,默认 unknown" },
|
|
380
|
+
role: { type: "string", enum: ["executor", "auditor"], description: "领取角色" },
|
|
381
|
+
},
|
|
382
|
+
},
|
|
383
|
+
handler: async (args) => {
|
|
384
|
+
try {
|
|
385
|
+
// Phase 1-1: role 白名单校验(非法值必须报错,不得静默降级)
|
|
386
|
+
const a = args || {};
|
|
387
|
+
const rawRole = a.role === undefined || a.role === null || a.role === "" ? "executor" : String(a.role);
|
|
388
|
+
if (rawRole !== "executor" && rawRole !== "auditor") {
|
|
389
|
+
return fail("非法 role:" + rawRole + ",必须是 executor 或 auditor");
|
|
390
|
+
}
|
|
391
|
+
const role = rawRole;
|
|
392
|
+
const sessionId = a.sessionId || "unknown";
|
|
393
|
+
const state = loadState(sessionId);
|
|
394
|
+
|
|
395
|
+
if (!state.card) {
|
|
396
|
+
return fail("没有留痕(record),executor/auditor 不得开工。请先由主 Agent 调用 flow_submit_requirement 提交任务卡。");
|
|
397
|
+
}
|
|
398
|
+
if (isStale(state)) {
|
|
399
|
+
return fail("留痕已过期(stale),请重新提交任务卡。");
|
|
400
|
+
}
|
|
401
|
+
if (state.dispatch_count >= MAX_DISPATCH) {
|
|
402
|
+
return fail("派发次数已达上限 " + MAX_DISPATCH + ",停止派发并向用户汇报。");
|
|
403
|
+
}
|
|
404
|
+
// 激活占位卡闸门:占位卡未补齐字段时不允许领取派发名额
|
|
405
|
+
if (Array.isArray(state.activation_pending) && state.activation_pending.length > 0) {
|
|
406
|
+
return fail("激活占位卡尚未补齐(待补:" + state.activation_pending.join("、") +
|
|
407
|
+
")。请先补齐后重新调用 flow_submit_requirement,再来领取派发名额。");
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// Phase 1-1: 不在 claim 时增加 dispatch_count,改在 dispatch 时增加
|
|
411
|
+
// claim 只标记已领取(state.claimed),不消耗派发额度
|
|
412
|
+
state.claimed = true;
|
|
413
|
+
state.claimed_role = role;
|
|
414
|
+
state.last_user_ts = Date.now();
|
|
415
|
+
saveState(sessionId, state);
|
|
416
|
+
|
|
417
|
+
audit("decision", sessionId, { kind: "claimed", role, dispatch_count: state.dispatch_count });
|
|
418
|
+
|
|
419
|
+
const result = {
|
|
420
|
+
ok: true,
|
|
421
|
+
role,
|
|
422
|
+
card: state.card,
|
|
423
|
+
tier: state.tier,
|
|
424
|
+
dispatch_count: state.dispatch_count,
|
|
425
|
+
prompt: buildContractPrompt(role, state.card),
|
|
426
|
+
};
|
|
427
|
+
if (state.bypass === true) result.bypass = true;
|
|
428
|
+
return ok(result);
|
|
429
|
+
} catch (e) {
|
|
430
|
+
return fail(e && e.message ? e.message : String(e));
|
|
431
|
+
}
|
|
432
|
+
},
|
|
433
|
+
},
|
|
434
|
+
|
|
435
|
+
// 4 -----------------------------------------------------------------------
|
|
436
|
+
{
|
|
437
|
+
name: "flow_dispatch",
|
|
438
|
+
description:
|
|
439
|
+
"按四段合同派发子 Agent(executor 写代码 / auditor 独立复核)。" +
|
|
440
|
+
"派发前会自动对 scope 内文件做快照(用于事后还原),message 会以原样附在合同末尾。" +
|
|
441
|
+
"run=false(默认)只生成 prompt 供你自己派子 Agent;run=true 则由本工具拉起 opencode 子进程直接跑并回传 stdout/stderr。" +
|
|
442
|
+
"必须先 flow_submit_requirement 留痕,否则报错。",
|
|
443
|
+
inputSchema: {
|
|
444
|
+
type: "object",
|
|
445
|
+
properties: {
|
|
446
|
+
sessionId: { type: "string", description: "会话 ID,默认 unknown" },
|
|
447
|
+
role: { type: "string", enum: ["executor", "auditor"], description: "派发角色" },
|
|
448
|
+
message: { type: "string", description: "附加说明,原样附在合同末尾" },
|
|
449
|
+
run: { type: "boolean", description: "true = 由本工具直接运行 opencode;false/absent = 只返回 prompt" },
|
|
450
|
+
cwd: { type: "string", description: "工作目录,默认 process.cwd()" },
|
|
451
|
+
},
|
|
452
|
+
},
|
|
453
|
+
handler: async (args) => {
|
|
454
|
+
try {
|
|
455
|
+
const a = args || {};
|
|
456
|
+
// Phase 1-1: role 白名单校验(非法值必须报错,不得静默降级)
|
|
457
|
+
const rawRole = a.role === undefined || a.role === null || a.role === "" ? "executor" : String(a.role);
|
|
458
|
+
if (rawRole !== "executor" && rawRole !== "auditor") {
|
|
459
|
+
return fail("非法 role:" + rawRole + ",必须是 executor 或 auditor");
|
|
460
|
+
}
|
|
461
|
+
const role = rawRole;
|
|
462
|
+
|
|
463
|
+
// Phase 1-1: dispatch 前强制校验 stale / dispatch_count / claim
|
|
464
|
+
const sessionId = a.sessionId || "unknown";
|
|
465
|
+
const state = loadState(sessionId);
|
|
466
|
+
if (!state.card) return fail("没有留痕,不允许派发。");
|
|
467
|
+
if (isStale(state)) return fail("留痕已过期(stale),请重新提交任务卡。");
|
|
468
|
+
if (state.dispatch_count >= MAX_DISPATCH) return fail("派发次数已达上限 " + MAX_DISPATCH + ",停止派发并向用户汇报。");
|
|
469
|
+
// 激活占位卡闸门:占位卡未补齐字段时不允许派发
|
|
470
|
+
if (Array.isArray(state.activation_pending) && state.activation_pending.length > 0) {
|
|
471
|
+
return fail("激活占位卡尚未补齐(待补:" + state.activation_pending.join("、") +
|
|
472
|
+
")。请先补齐后重新调用 flow_submit_requirement,再来派发。");
|
|
473
|
+
}
|
|
474
|
+
// 必须先 flow_claim 领取派发名额(claimed 标记由 claim 设置)
|
|
475
|
+
if (state.claimed !== true) return fail("未 claim,请先 flow_claim 领取派发名额。");
|
|
476
|
+
|
|
477
|
+
const cwd = a.cwd || process.cwd();
|
|
478
|
+
|
|
479
|
+
// Phase 1-1: dispatch 时才增加 dispatch_count(claim 不计数)
|
|
480
|
+
state.dispatch_count = (Number(state.dispatch_count) || 0) + 1;
|
|
481
|
+
// 额度已消耗,需要重新 claim 才能再次派发
|
|
482
|
+
state.claimed = false;
|
|
483
|
+
state.write_files = 0;
|
|
484
|
+
// Phase 1-1: 派发后自动清零免检,避免“忘记关免检”长期生效
|
|
485
|
+
if (state.bypass === true) {
|
|
486
|
+
state.bypass = false;
|
|
487
|
+
state.bypass_ts = 0;
|
|
488
|
+
audit("decision", sessionId, { kind: "bypass_auto_reset", reason: "派发后自动清零" });
|
|
489
|
+
}
|
|
490
|
+
saveState(sessionId, state);
|
|
491
|
+
|
|
492
|
+
const prompt = buildContractPrompt(role, state.card, a.message);
|
|
493
|
+
|
|
494
|
+
const scope = Array.isArray(state.card.scope) ? state.card.scope : [];
|
|
495
|
+
const entries = snapshotFiles(sessionId, scope, cwd);
|
|
496
|
+
audit("run", sessionId, { kind: "snapshot", role, files: scope.length, ok: true });
|
|
497
|
+
|
|
498
|
+
// Phase 1-1: 记录「派发前基线」。
|
|
499
|
+
//
|
|
500
|
+
// 为什么是记录而不是当场阻断:
|
|
501
|
+
// 派发这一刻,子 Agent **还没动手** —— 它之后会改哪些文件根本无从得知。
|
|
502
|
+
// 此时跑 git diff 得到的是「用户自己已有的未提交改动」,
|
|
503
|
+
// 拿它当越界拦下来会**误伤正常用法**(用户本来就在改这些文件)。
|
|
504
|
+
//
|
|
505
|
+
// 正确的时机是**裁定前**:用「当前改动 - 派发基线」算出**本次执行引入的**
|
|
506
|
+
// 改动,再与卡片 scope 比对 —— 这才是「越界直接阻断」的真实落点,
|
|
507
|
+
// 阻断的是「越界之后仍然拿到 PASS」这个闭环。
|
|
508
|
+
const baseline = enforceScope(scope, cwd);
|
|
509
|
+
state.dispatch_baseline = Array.isArray(baseline.checked) ? baseline.checked : [];
|
|
510
|
+
saveState(sessionId, state);
|
|
511
|
+
audit("run", sessionId, {
|
|
512
|
+
kind: "dispatch_baseline",
|
|
513
|
+
role,
|
|
514
|
+
files: state.dispatch_baseline.length,
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
if (a.run === true) {
|
|
518
|
+
// 写入 prompt 文件,供主 Agent 读取后通过 subagent 工具派发
|
|
519
|
+
const { file } = writeDispatchPrompt(sessionId, role, prompt, cwd);
|
|
520
|
+
audit("run", sessionId, { kind: "dispatch", role, prompt_file: file });
|
|
521
|
+
return ok({
|
|
522
|
+
ok: true,
|
|
523
|
+
role,
|
|
524
|
+
prompt,
|
|
525
|
+
snapshot: entries,
|
|
526
|
+
run: {
|
|
527
|
+
prompt_file: file,
|
|
528
|
+
note: "主 Agent 请读取 prompt_file 内容,使用 subagent 工具按角色派发(executor=worker, auditor=reviewer)。执行完成后调用 save_agent_result 保存结果。",
|
|
529
|
+
},
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
return ok({ ok: true, role, prompt, snapshot: entries, run: null });
|
|
534
|
+
} catch (e) {
|
|
535
|
+
return fail(e && e.message ? e.message : String(e));
|
|
536
|
+
}
|
|
537
|
+
},
|
|
538
|
+
},
|
|
539
|
+
|
|
540
|
+
// 5 -----------------------------------------------------------------------
|
|
541
|
+
{
|
|
542
|
+
name: "flow_audit_axes",
|
|
543
|
+
description:
|
|
544
|
+
"派发两个并行审计 subagent(Standards 轴 / Spec 轴),生成两段审计合同 prompt。" +
|
|
545
|
+
"executor 改动通过 flow_run_verify 与负对照后、落 PASS 裁定前必须先调用本工具。",
|
|
546
|
+
inputSchema: {
|
|
547
|
+
type: "object",
|
|
548
|
+
properties: {
|
|
549
|
+
sessionId: { type: "string", description: "会话 ID" },
|
|
550
|
+
cwd: { type: "string", description: "仓库工作目录,默认 process.cwd()" },
|
|
551
|
+
},
|
|
552
|
+
},
|
|
553
|
+
handler: async (args) => {
|
|
554
|
+
try {
|
|
555
|
+
const sid = args.sessionId || "unknown";
|
|
556
|
+
const state = loadState(sid);
|
|
557
|
+
if (!state.card) return fail("无卡,无法派发审计轴");
|
|
558
|
+
|
|
559
|
+
const cwd = args.cwd || process.cwd();
|
|
560
|
+
const res = { standards: null, spec: null };
|
|
561
|
+
for (const axis of ["standards", "spec"]) {
|
|
562
|
+
const prompt = buildContractPrompt("auditor", state.card, "", axis);
|
|
563
|
+
const { file } = writeDispatchPrompt(sid, "auditor", prompt, cwd);
|
|
564
|
+
res[axis] = { prompt, file };
|
|
565
|
+
}
|
|
566
|
+
return ok({ ok: true, ...res });
|
|
567
|
+
} catch (e) { return fail(String(e)); }
|
|
568
|
+
},
|
|
569
|
+
},
|
|
570
|
+
{
|
|
571
|
+
name: "flow_audit_submit",
|
|
572
|
+
description:
|
|
573
|
+
"保存某一审计轴(standards|spec)的结论与证据到会话状态。" +
|
|
574
|
+
"PASS 裁定前必须两轴都提交,且各自绑定的 card_ts 与当前卡一致。同轴可重复提交(后者覆盖)。",
|
|
575
|
+
inputSchema: {
|
|
576
|
+
type: "object",
|
|
577
|
+
properties: {
|
|
578
|
+
sessionId: { type: "string", description: "会话 ID" },
|
|
579
|
+
axis: { type: "string", enum: ["standards", "spec"], description: "审计轴" },
|
|
580
|
+
verdict: { type: "string", enum: ["PASS", "FAIL", "WARN"], description: "轴判定;WARN=坏味道等 judgement call,不硬拦" },
|
|
581
|
+
findings: { type: "array", items: { type: "string" }, description: "每条含 file:line 或证据" },
|
|
582
|
+
evidence: { type: "string", description: "实跑命令输出 / diff 摘录" },
|
|
583
|
+
},
|
|
584
|
+
},
|
|
585
|
+
handler: async (args) => {
|
|
586
|
+
try {
|
|
587
|
+
const sid = args.sessionId || "unknown";
|
|
588
|
+
const state = loadState(sid);
|
|
589
|
+
if (!state.card) return fail("无卡,无法提交审计轴结论");
|
|
590
|
+
state.audit_axes[args.axis] = {
|
|
591
|
+
verdict: args.verdict,
|
|
592
|
+
findings: args.findings,
|
|
593
|
+
evidence: args.evidence,
|
|
594
|
+
// 绑定提交时的卡片:换卡后该轴证据立即失效,必须重审
|
|
595
|
+
card_ts: state.card_ts,
|
|
596
|
+
ts: Date.now(),
|
|
597
|
+
};
|
|
598
|
+
state.audit_axes_ts = Date.now();
|
|
599
|
+
saveState(sid, state);
|
|
600
|
+
|
|
601
|
+
// 审计留痕:轴结论此前只落在 state 里 —— 可被覆盖、不可追溯。
|
|
602
|
+
// 这里补一条审计事件,回答「这条结论是谁在什么时候提交的」。
|
|
603
|
+
// 只记结论与证据摘要,不整段拷贝 evidence(验证输出可能很长)。
|
|
604
|
+
audit("decision", sid, {
|
|
605
|
+
kind: "audit_axis_submitted",
|
|
606
|
+
axis: args.axis,
|
|
607
|
+
verdict: args.verdict,
|
|
608
|
+
findings: Array.isArray(args.findings) ? args.findings : [],
|
|
609
|
+
has_evidence: typeof args.evidence === "string" && args.evidence.trim() !== "",
|
|
610
|
+
card_ts: state.card_ts,
|
|
611
|
+
});
|
|
612
|
+
const stored = [state.audit_axes.standards, state.audit_axes.spec].filter(Boolean).length;
|
|
613
|
+
return ok({ ok: true, axis: args.axis, stored_axes_count: stored });
|
|
614
|
+
} catch (e) { return fail(String(e)); }
|
|
615
|
+
},
|
|
616
|
+
},
|
|
617
|
+
{
|
|
618
|
+
name: "flow_audit_guard",
|
|
619
|
+
description:
|
|
620
|
+
"校验两轴是否已通过且证据齐全(与 flow_verdict 内的双轴门禁同口径)。" +
|
|
621
|
+
"主 Agent 落 PASS 前可主动自查;flow_verdict 不受此调用影响,仍会再拦一道。",
|
|
622
|
+
inputSchema: {
|
|
623
|
+
type: "object",
|
|
624
|
+
properties: { sessionId: { type: "string", description: "会话 ID" } },
|
|
625
|
+
},
|
|
626
|
+
handler: async (args) => {
|
|
627
|
+
try {
|
|
628
|
+
const sid = args.sessionId || "unknown";
|
|
629
|
+
const state = loadState(sid);
|
|
630
|
+
const ax = state.audit_axes;
|
|
631
|
+
// 口径与 flow_verdict 的双轴门禁完全一致:
|
|
632
|
+
// 两轴都有 verdict(不能缺失/空)、card_ts 与当前卡一致、且无轴 FAIL。
|
|
633
|
+
const axisOk = (a) => ax && ax[a] && ax[a].verdict && ax[a].card_ts === state.card_ts;
|
|
634
|
+
if (!axisOk("standards") || !axisOk("spec")) {
|
|
635
|
+
return fail("两轴审计未齐或证据失效(需 flow_audit_axes 派发 + flow_audit_submit 两轴且 card_ts 与当前卡一致)");
|
|
636
|
+
}
|
|
637
|
+
if (ax.standards.verdict === "FAIL" || ax.spec.verdict === "FAIL") {
|
|
638
|
+
return fail("两轴中至少一轴 FAIL(standards=" + ax.standards.verdict +
|
|
639
|
+
", spec=" + ax.spec.verdict + ")。");
|
|
640
|
+
}
|
|
641
|
+
return ok({ ok: true });
|
|
642
|
+
} catch (e) { return fail(String(e)); }
|
|
643
|
+
},
|
|
644
|
+
},
|
|
645
|
+
{
|
|
646
|
+
name: "flow_verdict",
|
|
647
|
+
description:
|
|
648
|
+
"记录 auditor 的裁定结果(PASS / DIRECTION / LOCAL)。只有 PASS 会把 completed_ok 置为 true。" +
|
|
649
|
+
"DIRECTION 表示方向对但需返工,LOCAL 表示问题局限在局部。在向用户汇报完成前必须先落裁定,否则视为未复核。" +
|
|
650
|
+
"【双轴门禁】记 PASS 前必须先 flow_audit_axes 派发 Standards/Spec 两轴、由 subagent 取证后 flow_audit_submit 提交两轴结论;任一轴 FAIL 会被拒,WARN 不硬拦。",
|
|
651
|
+
inputSchema: {
|
|
652
|
+
type: "object",
|
|
653
|
+
properties: {
|
|
654
|
+
sessionId: { type: "string", description: "会话 ID,默认 unknown" },
|
|
655
|
+
verdict: { type: "string", enum: ["PASS", "DIRECTION", "LOCAL"], description: "裁定结果" },
|
|
656
|
+
notes: { type: "string", description: "裁定说明/证据摘要" },
|
|
657
|
+
role: { type: "string", description: "记录裁定者角色(通常 auditor)" },
|
|
658
|
+
cwd: { type: "string", description: "工作目录,默认 process.cwd()" },
|
|
659
|
+
},
|
|
660
|
+
},
|
|
661
|
+
handler: async (args) => {
|
|
662
|
+
try {
|
|
663
|
+
const a = args || {};
|
|
664
|
+
const sessionId = a.sessionId || "unknown";
|
|
665
|
+
const state = loadState(sessionId);
|
|
666
|
+
|
|
667
|
+
if (!state.card) return fail("没有留痕,无法记录裁定。");
|
|
668
|
+
|
|
669
|
+
const verdict = a.verdict;
|
|
670
|
+
|
|
671
|
+
// 激活占位卡尚未补齐时不允许裁定:
|
|
672
|
+
// 否则会出现「未派发、未验证、却直接 PASS」的假闭环。
|
|
673
|
+
// (先于 dispatch_count 检查:占位卡的错误信息更具体,也更可操作)
|
|
674
|
+
if (Array.isArray(state.activation_pending) && state.activation_pending.length > 0) {
|
|
675
|
+
audit("decision", sessionId, {
|
|
676
|
+
kind: "verdict_denied",
|
|
677
|
+
reason: "激活占位卡未补齐",
|
|
678
|
+
pending: state.activation_pending,
|
|
679
|
+
});
|
|
680
|
+
return fail("激活占位卡尚未补齐(待补:" + state.activation_pending.join("、") +
|
|
681
|
+
"),不能记录裁定。请先补齐后重新调用 flow_submit_requirement。");
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
// 防假闭环之二:从未派发过任何执行,就不允许落 PASS 裁定。
|
|
685
|
+
// 否则一张刚提交、什么都没跑的卡也能直接标 completed_ok=true。
|
|
686
|
+
// 只施加于 PASS:DIRECTION / LOCAL 是「有问题」的裁定,不构成「完成」,
|
|
687
|
+
// 允许在未派发时先记录返工意见。
|
|
688
|
+
if (verdict === "PASS" && !(Number(state.dispatch_count) > 0)) {
|
|
689
|
+
audit("decision", sessionId, {
|
|
690
|
+
kind: "verdict_denied",
|
|
691
|
+
reason: "尚未派发任何执行",
|
|
692
|
+
dispatch_count: state.dispatch_count,
|
|
693
|
+
});
|
|
694
|
+
return fail("尚未派发任何执行(dispatch_count=0),不能记录 PASS 裁定。" +
|
|
695
|
+
"请先 flow_claim / flow_dispatch 派发并完成验证后再落 PASS。");
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
// 防假闭环之三:记 PASS 必须有**真实验证通过**的证据。
|
|
699
|
+
// 「派发过」≠「验证过」——只查 dispatch_count 时,一张跑过但验证失败的卡
|
|
700
|
+
// 照样能落 PASS(completed_ok=true),等于把验证环节架空。
|
|
701
|
+
// 三个条件同时成立才放行:
|
|
702
|
+
// 1) 存在 flow_run_verify 写入的 state.last_verify;
|
|
703
|
+
// 2) last_verify.ok === true(真实执行退出码为 0);
|
|
704
|
+
// 3) last_verify.cmd 与**当前卡片**的 verify.cmd 一致(防止旧卡结果冒充新卡)。
|
|
705
|
+
// 只施加于 PASS:DIRECTION / LOCAL 是返工意见,不要求已完成验证。
|
|
706
|
+
if (verdict === "PASS") {
|
|
707
|
+
const lv = state.last_verify;
|
|
708
|
+
const want = cardVerifyCmd(state);
|
|
709
|
+
if (!lv || typeof lv !== "object") {
|
|
710
|
+
// 区分两种「没验证」:卡片压根没写 verify.cmd(用户无从下手)
|
|
711
|
+
// vs 写了但还没跑。前者必须给出可操作的下一步,否则是死路。
|
|
712
|
+
if (!want) {
|
|
713
|
+
audit("decision", sessionId, {
|
|
714
|
+
kind: "verdict_denied",
|
|
715
|
+
reason: "卡片未指定验证命令",
|
|
716
|
+
});
|
|
717
|
+
return fail("卡片未指定 verify.cmd,没有可核验的验证依据,不能记录 PASS 裁定。" +
|
|
718
|
+
"请重新提交带 verify.cmd 的任务卡(flow_submit_requirement)," +
|
|
719
|
+
"用 flow_run_verify 跑通后再记录裁定。");
|
|
720
|
+
}
|
|
721
|
+
audit("decision", sessionId, {
|
|
722
|
+
kind: "verdict_denied",
|
|
723
|
+
reason: "尚未执行验证",
|
|
724
|
+
});
|
|
725
|
+
return fail("尚未执行验证,不能记录 PASS 裁定。请先调用 flow_run_verify " +
|
|
726
|
+
"跑通卡片里的 verify.cmd,再记录裁定。");
|
|
727
|
+
}
|
|
728
|
+
if (lv.ok !== true) {
|
|
729
|
+
audit("decision", sessionId, {
|
|
730
|
+
kind: "verdict_denied",
|
|
731
|
+
reason: "验证未通过",
|
|
732
|
+
exitCode: lv.exitCode,
|
|
733
|
+
verify_cmd: lv.cmd,
|
|
734
|
+
});
|
|
735
|
+
return fail("验证未通过(exitCode=" + lv.exitCode + "),不能记录 PASS 裁定。" +
|
|
736
|
+
"请先修复问题、让 verify.cmd 真实跑通(flow_run_verify),再记录裁定。");
|
|
737
|
+
}
|
|
738
|
+
if (!want || typeof lv.cmd !== "string" || lv.cmd.trim() !== want) {
|
|
739
|
+
audit("decision", sessionId, {
|
|
740
|
+
kind: "verdict_denied",
|
|
741
|
+
reason: "验证命令与当前卡片不一致",
|
|
742
|
+
verify_cmd: lv.cmd,
|
|
743
|
+
card_cmd: want,
|
|
744
|
+
});
|
|
745
|
+
return fail("已有的验证结果对应的命令与当前卡片的 verify.cmd 不一致,不能记录 PASS 裁定。" +
|
|
746
|
+
"请对当前卡片重新执行 flow_run_verify,再记录裁定。");
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
// 防假闭环之五:验证结果必须绑定当时的**代码状态**(A1)。
|
|
750
|
+
//
|
|
751
|
+
// 前面三道判断只看「验证跑过 / 跑通了 / 命令对得上」,**不看代码变没变**。
|
|
752
|
+
// 于是存在这条绕过路径:
|
|
753
|
+
// 1. flow_run_verify 跑通 → last_verify = {ok:true, cmd:"npm test"}
|
|
754
|
+
// 2. executor 继续改代码(甚至改坏)
|
|
755
|
+
// 3. flow_verdict(PASS) → 三道全过 → 放行
|
|
756
|
+
// 指纹把「验证通过」绑定到一次**可复现的运行**:验证之后再动过代码,指纹就变了。
|
|
757
|
+
//
|
|
758
|
+
// 非 git 目录:fail-open(Carlos Flow 支持非 Git 项目),但要在 flow_status
|
|
759
|
+
// 里如实标注指纹不可用(见 R1)。
|
|
760
|
+
const fpCwd = a.cwd || (state.last_verify && state.last_verify.cwd) || process.cwd();
|
|
761
|
+
const fpWas = lv.fingerprint;
|
|
762
|
+
const fpNow = workspaceFingerprint(fpCwd);
|
|
763
|
+
if (fpWas && fpWas.ok && fpNow.ok && fpNow.diff_sha256 !== fpWas.diff_sha256) {
|
|
764
|
+
audit("decision", sessionId, {
|
|
765
|
+
kind: "verdict_denied",
|
|
766
|
+
reason: "验证结果已过期(代码在验证后被修改)",
|
|
767
|
+
fp_was: fpWas.diff_sha256,
|
|
768
|
+
fp_now: fpNow.diff_sha256,
|
|
769
|
+
});
|
|
770
|
+
return fail("验证结果已过期(STALE):验证通过后代码又被修改,当前 diff 指纹已变化。" +
|
|
771
|
+
"请重新执行 flow_run_verify 再记录裁定。");
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
// 防假闭环之四:越界改动必须被拦住。
|
|
775
|
+
//
|
|
776
|
+
// 「越界直接阻断」的**真实落点**在这里:判定「本次执行有没有改到
|
|
777
|
+
// scope 之外的文件」,而不是「派发前工作区干不干净」。
|
|
778
|
+
//
|
|
779
|
+
// 做法:拿「当前 git 改动清单」减去「派发时记录的基线」,
|
|
780
|
+
// 剩下的就是**本次执行引入的**改动 —— 用户自己原有的未提交改动
|
|
781
|
+
// 不会被误判为越界。
|
|
782
|
+
//
|
|
783
|
+
// 无基线(未走过 flow_dispatch)时退化为「全量改动」判定:
|
|
784
|
+
// 此时没有参照点,用全量更保守(fail-closed)。
|
|
785
|
+
const scopeNow = Array.isArray(state.card.scope) ? state.card.scope : [];
|
|
786
|
+
const current = enforceScope(scopeNow, a.cwd || process.cwd());
|
|
787
|
+
const baseline = Array.isArray(state.dispatch_baseline) ? state.dispatch_baseline : [];
|
|
788
|
+
const baselineSet = new Set(baseline.map((f) => path.normalize(f)));
|
|
789
|
+
const introduced = (current.checked || []).filter((f) => !baselineSet.has(path.normalize(f)));
|
|
790
|
+
const scopeCheck = enforceScope(scopeNow, a.cwd || process.cwd(), introduced);
|
|
791
|
+
if (!scopeCheck.ok) {
|
|
792
|
+
audit("decision", sessionId, {
|
|
793
|
+
kind: "scope_violation",
|
|
794
|
+
reason: "越界改动",
|
|
795
|
+
violations: scopeCheck.violations,
|
|
796
|
+
introduced: introduced.length,
|
|
797
|
+
});
|
|
798
|
+
return fail("检测到越界改动,不能记录 PASS 裁定:" +
|
|
799
|
+
(scopeCheck.violations || []).join(", ") +
|
|
800
|
+
"。这些文件不在卡片 scope 白名单内。" +
|
|
801
|
+
"请把越界文件加入 scope(重新提交任务卡)后重试,或先撤销这些改动。");
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
// 防假闭环之四:role 门禁绑定到会话 ID(claimed_role),而非调用方自述字符串
|
|
805
|
+
if (verdict === "PASS" && state.claimed_role !== "auditor") {
|
|
806
|
+
audit("decision", sessionId, { kind: "verdict_denied", reason: "role 未绑定到 auditor 会话", claimed_role: state.claimed_role });
|
|
807
|
+
return fail("PASS 裁定必须由已领取 auditor 角色的会话(flow_claim role=auditor)落下。当前会话 claimed_role=" + (state.claimed_role || "null"));
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
// 防假闭环之五:负对照 —— 证明 verify.cmd 真的在测「本次改动」。
|
|
811
|
+
//
|
|
812
|
+
// 「验证通过」本身不是证据:verify.cmd 可以是 `|| true` / `echo ok`,
|
|
813
|
+
// 改动前后都退 0,门禁却以为测试过了。负对照要求:
|
|
814
|
+
// · 还原到派发时(改动前)跑同一命令 → 必须失败
|
|
815
|
+
// · 还原回改动后跑同一命令 → 必须通过
|
|
816
|
+
// 只有「改动前失败 + 改动后通过」才说明这个测试有判别力。
|
|
817
|
+
const lf = state.last_falsify;
|
|
818
|
+
if (!lf || typeof lf !== "object") {
|
|
819
|
+
audit("decision", sessionId, { kind: "verdict_denied", reason: "尚未做负对照" });
|
|
820
|
+
return fail("尚未做负对照(flow_falsify),不能记录 PASS 裁定。" +
|
|
821
|
+
"请先用 flow_falsify 证明 verify.cmd 在改动前失败、改动后通过。");
|
|
822
|
+
}
|
|
823
|
+
if (lf.verdict !== "meaningful") {
|
|
824
|
+
audit("decision", sessionId, { kind: "verdict_denied", reason: "负对照未通过", falsify_verdict: lf.verdict });
|
|
825
|
+
return fail("负对照未通过(" + lf.verdict + "):" + (lf.reason || "") +
|
|
826
|
+
" 不能记录 PASS 裁定。请修正 verify.cmd 或改动后重试。");
|
|
827
|
+
}
|
|
828
|
+
// 绑定:裁定的证据必须就是负对照里「改动后」的那次运行。
|
|
829
|
+
if (typeof lf.after === "object" && lf.after && typeof lf.after.hash === "string" &&
|
|
830
|
+
state.last_verify && state.last_verify.output_hash !== lf.after.hash) {
|
|
831
|
+
audit("decision", sessionId, { kind: "verdict_denied", reason: "验证证据与负对照不一致" });
|
|
832
|
+
return fail("已有的验证结果与负对照的运行结果不一致,不能记录 PASS 裁定。" +
|
|
833
|
+
"请重新调用 flow_falsify(它会把改动后的运行写为验证证据)。");
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
// 防假闭环之六:双轴审查门禁。
|
|
837
|
+
//
|
|
838
|
+
// 位置说明(重要):必须放在负对照**之后**。语义顺序是
|
|
839
|
+
// 「先证实验证有效 → 再证审查到位」,六道门各自独立、互不掩盖。
|
|
840
|
+
// 若前置,会让「未验证」「未派发」「未做负对照」这些更基础的问题被
|
|
841
|
+
// 双轴错误信息掩盖,调用方拿到的是不可操作的下一步。
|
|
842
|
+
//
|
|
843
|
+
// 判定口径:
|
|
844
|
+
// · 两轴都必须有结论,且 card_ts 与当前卡一致(防旧卡审查结果冒充新卡,
|
|
845
|
+
// 也防两轴跨卡拼接:卡A的 standards + 卡B的 spec 不算数);
|
|
846
|
+
// · FAIL 硬拦(规范硬违规 / 未实现验收);
|
|
847
|
+
// · WARN 不硬拦(坏味道是 judgement call),由主 Agent 读 audit_axes 后标注。
|
|
848
|
+
const ax = state.audit_axes;
|
|
849
|
+
const axisOk = (a) => ax && ax[a] && ax[a].verdict && ax[a].card_ts === state.card_ts;
|
|
850
|
+
const hasStandards = axisOk("standards");
|
|
851
|
+
const hasSpec = axisOk("spec");
|
|
852
|
+
if (!hasStandards || !hasSpec) {
|
|
853
|
+
audit("decision", sessionId, {
|
|
854
|
+
kind: "verdict_denied",
|
|
855
|
+
reason: "两轴审查未齐",
|
|
856
|
+
standards: ax && ax.standards ? ax.standards.verdict : null,
|
|
857
|
+
spec: ax && ax.spec ? ax.spec.verdict : null,
|
|
858
|
+
card_ts: state.card_ts,
|
|
859
|
+
});
|
|
860
|
+
return fail("两轴审查未齐,不能记录 PASS 裁定。请先 flow_audit_axes 派发两轴、" +
|
|
861
|
+
"由 subagent 取证后分别 flow_audit_submit 提交 Standards 与 Spec 两轴结论,再记录裁定。");
|
|
862
|
+
}
|
|
863
|
+
const standardsFail = ax.standards.verdict === "FAIL";
|
|
864
|
+
const specFail = ax.spec.verdict === "FAIL";
|
|
865
|
+
if (standardsFail || specFail) {
|
|
866
|
+
audit("decision", sessionId, {
|
|
867
|
+
kind: "verdict_denied",
|
|
868
|
+
reason: "有轴不通过",
|
|
869
|
+
standards: ax.standards.verdict,
|
|
870
|
+
spec: ax.spec.verdict,
|
|
871
|
+
});
|
|
872
|
+
return fail("PASS 被拒:两轴中至少一轴 FAIL(standards=" + ax.standards.verdict +
|
|
873
|
+
", spec=" + ax.spec.verdict + ")。请修复后重跑验证与审计。");
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
const findings = [];
|
|
878
|
+
if (state.verdict && state.verdict !== verdict) {
|
|
879
|
+
findings.push(`裁定被覆盖:${state.verdict} → ${verdict}`);
|
|
880
|
+
audit("finding", sessionId, { kind: "verdict_overwrite", from: state.verdict, to: verdict, tier: state.tier });
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
state.verdict = verdict;
|
|
884
|
+
state.completed_ok = verdict === "PASS";
|
|
885
|
+
state.verdict_ts = Date.now();
|
|
886
|
+
// 落盘裁定者角色:审计链据此回溯「这条 PASS 是谁落的」。
|
|
887
|
+
state.verdict_role = a.role === undefined ? null : a.role;
|
|
888
|
+
saveState(sessionId, state);
|
|
889
|
+
|
|
890
|
+
audit("resolution", sessionId, { kind: "verdict", verdict, notes: a.notes, role: a.role });
|
|
891
|
+
|
|
892
|
+
const warns = collectAxisWarnings(state);
|
|
893
|
+
const result = {
|
|
894
|
+
ok: true,
|
|
895
|
+
verdict,
|
|
896
|
+
completed_ok: state.completed_ok,
|
|
897
|
+
notes: a.notes,
|
|
898
|
+
verdict_role: state.verdict_role,
|
|
899
|
+
// WARN 汇总:两轴里判 WARN(坏味道等 judgement call)的结论。
|
|
900
|
+
// 以前 WARN 只躺在 state.audit_axes 里,主 Agent 不主动读就看不到,
|
|
901
|
+
// 「带坏味道通过」在汇报里等于被吞掉 —— 现在随裁定一起返回。
|
|
902
|
+
warn_count: warns.length,
|
|
903
|
+
};
|
|
904
|
+
if (warns.length) result.warn_findings = warns;
|
|
905
|
+
if (findings.length) result.findings = findings;
|
|
906
|
+
return ok(result);
|
|
907
|
+
} catch (e) {
|
|
908
|
+
return fail(e && e.message ? e.message : String(e));
|
|
909
|
+
}
|
|
910
|
+
},
|
|
911
|
+
},
|
|
912
|
+
|
|
913
|
+
{
|
|
914
|
+
name: "flow_mode",
|
|
915
|
+
description: "切换工作流模式:pro(专业)/ guide(向导)。",
|
|
916
|
+
inputSchema: { type: "object", properties: { sessionId: { type: "string" }, mode: { type: "string", enum: ["pro", "guide"] }, reason: { type: "string" } }, required: ["mode"] },
|
|
917
|
+
handler: async (args) => {
|
|
918
|
+
const { sessionId, mode, reason } = args;
|
|
919
|
+
const state = loadState(sessionId || "unknown");
|
|
920
|
+
state.mode = mode;
|
|
921
|
+
state.mode_ts = Date.now();
|
|
922
|
+
saveState(sessionId, state);
|
|
923
|
+
audit("decision", sessionId || "unknown", { kind: "mode_on", mode: mode, reason: reason });
|
|
924
|
+
return ok({ mode: state.mode });
|
|
925
|
+
}
|
|
926
|
+
},
|
|
927
|
+
// 6 -----------------------------------------------------------------------
|
|
928
|
+
{
|
|
929
|
+
name: "flow_status",
|
|
930
|
+
description:
|
|
931
|
+
"查询当前会话的门禁状态:是否有任务卡、风险档、已派发次数/上限、裁定结果、是否过期、是否处于免检模式、卡片内容与时间戳。" +
|
|
932
|
+
"在汇报进度、判断能否继续派发、或确认任务是否真的收尾时调用。",
|
|
933
|
+
inputSchema: {
|
|
934
|
+
type: "object",
|
|
935
|
+
properties: {
|
|
936
|
+
sessionId: { type: "string", description: "会话 ID,默认 unknown" },
|
|
937
|
+
},
|
|
938
|
+
},
|
|
939
|
+
handler: async (args) => {
|
|
940
|
+
try {
|
|
941
|
+
const a = args || {};
|
|
942
|
+
const sessionId = a.sessionId || "unknown";
|
|
943
|
+
const state = loadState(sessionId);
|
|
944
|
+
const warns = collectAxisWarnings(state);
|
|
945
|
+
|
|
946
|
+
const result = {
|
|
947
|
+
sessionId,
|
|
948
|
+
has_card: !!state.card,
|
|
949
|
+
tier: state.tier,
|
|
950
|
+
dispatch_count: state.dispatch_count,
|
|
951
|
+
max_dispatch: MAX_DISPATCH,
|
|
952
|
+
verdict: state.verdict,
|
|
953
|
+
// 本条裁定是谁落的("auditor" | "executor" | null)。
|
|
954
|
+
// 与 claimed_role 不同:后者是「会话最近 claim 的角色」,可能已过期;
|
|
955
|
+
// 这个字段直接回答「这条 PASS 是谁落的」。
|
|
956
|
+
verdict_role: state.verdict_role === undefined ? null : state.verdict_role,
|
|
957
|
+
verified: !!state.verdict,
|
|
958
|
+
// verify_passed:真实验证是否通过(last_verify.ok===true 且命令与当前卡片一致)。
|
|
959
|
+
// 注意 verified 只表示「是否已有裁定」,不代表验证通过——两者语义不同,不可混用。
|
|
960
|
+
verify_passed: verifyPassed(state),
|
|
961
|
+
// 四态验证状态:not_run / fail / stale / pass。
|
|
962
|
+
// stale = 验证通过后代码被改过 —— 不阻断(fail-open),但必须如实告知。
|
|
963
|
+
verify_state: verifyState(state).state,
|
|
964
|
+
completed_ok: state.completed_ok === true,
|
|
965
|
+
stale: isStale(state),
|
|
966
|
+
bypass: !!state.bypass,
|
|
967
|
+
card: state.card,
|
|
968
|
+
card_ts: state.card_ts,
|
|
969
|
+
activation_pending: Array.isArray(state.activation_pending) ? state.activation_pending : [],
|
|
970
|
+
// 双轴审查证据:主 Agent 落 PASS 前可读它确认两轴状态与 card_ts 绑定。
|
|
971
|
+
// axes_ready = 两轴都有结论且 card_ts 与当前卡一致且无轴 FAIL。
|
|
972
|
+
audit_axes: state.audit_axes,
|
|
973
|
+
audit_axes_ts: state.audit_axes_ts,
|
|
974
|
+
axes_ready: (() => {
|
|
975
|
+
const ax = state.audit_axes;
|
|
976
|
+
const axisOk = (x) => ax && ax[x] && ax[x].verdict && ax[x].card_ts === state.card_ts;
|
|
977
|
+
return axisOk("standards") && axisOk("spec") &&
|
|
978
|
+
ax.standards.verdict !== "FAIL" && ax.spec.verdict !== "FAIL";
|
|
979
|
+
})(),
|
|
980
|
+
// WARN 汇总(与 flow_verdict 同一份派生口径):只读镜像,供事后复查。
|
|
981
|
+
warn_count: warns.length,
|
|
982
|
+
};
|
|
983
|
+
if (warns.length) result.warn_findings = warns;
|
|
984
|
+
if (state.card && !state.verdict) result.unreviewed = true;
|
|
985
|
+
return ok(result);
|
|
986
|
+
} catch (e) {
|
|
987
|
+
return fail(e && e.message ? e.message : String(e));
|
|
988
|
+
}
|
|
989
|
+
},
|
|
990
|
+
},
|
|
991
|
+
|
|
992
|
+
// 7 -----------------------------------------------------------------------
|
|
993
|
+
{
|
|
994
|
+
name: "flow_audit",
|
|
995
|
+
description:
|
|
996
|
+
"读取审计流水(gate-audit/*.jsonl),返回事件列表与按 type / kind 的统计。可按 sessionId、type(run/finding/decision/resolution)、日期过滤。" +
|
|
997
|
+
"不给 sessionId 则聚合全部会话。用于复核“到底派发了什么、改了什么、谁批的”,也用于事后追溯。" +
|
|
998
|
+
"同时可验证审计链完整性。",
|
|
999
|
+
inputSchema: {
|
|
1000
|
+
type: "object",
|
|
1001
|
+
properties: {
|
|
1002
|
+
sessionId: { type: "string", description: "会话 ID;不传则聚合全部" },
|
|
1003
|
+
type: { type: "string", description: "事件类型过滤:run / finding / decision / resolution" },
|
|
1004
|
+
limit: { type: "number", description: "最多返回最近 N 条" },
|
|
1005
|
+
date: { type: "string", description: "YYYY-MM-DD,默认今天" },
|
|
1006
|
+
verifyChain: { type: "boolean", description: "是否验证审计链完整性" },
|
|
1007
|
+
},
|
|
1008
|
+
},
|
|
1009
|
+
handler: async (args) => {
|
|
1010
|
+
try {
|
|
1011
|
+
const a = args || {};
|
|
1012
|
+
const opts = {};
|
|
1013
|
+
if (a.sessionId != null && a.sessionId !== "") opts.sessionId = a.sessionId;
|
|
1014
|
+
if (a.type != null && a.type !== "") opts.type = a.type;
|
|
1015
|
+
if (typeof a.limit === "number") opts.limit = a.limit;
|
|
1016
|
+
if (typeof a.date === "string" && a.date !== "") opts.date = a.date;
|
|
1017
|
+
|
|
1018
|
+
const rows = readAudit(opts);
|
|
1019
|
+
const by_type = {};
|
|
1020
|
+
const by_kind = {};
|
|
1021
|
+
for (const row of rows) {
|
|
1022
|
+
const t = row.type || "none";
|
|
1023
|
+
by_type[t] = (by_type[t] || 0) + 1;
|
|
1024
|
+
const k = row.kind || "none";
|
|
1025
|
+
by_kind[k] = (by_kind[k] || 0) + 1;
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
let chainResult = null;
|
|
1029
|
+
if (a.verifyChain) {
|
|
1030
|
+
chainResult = verifyAuditChain(opts);
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
return ok({ total: rows.length, by_type, by_kind, events: rows, chain: chainResult, mode: auditHashMode() });
|
|
1034
|
+
} catch (e) {
|
|
1035
|
+
return fail(e && e.message ? e.message : String(e));
|
|
1036
|
+
}
|
|
1037
|
+
},
|
|
1038
|
+
},
|
|
1039
|
+
|
|
1040
|
+
// 8 -----------------------------------------------------------------------
|
|
1041
|
+
{
|
|
1042
|
+
name: "flow_diff_hunks",
|
|
1043
|
+
description:
|
|
1044
|
+
"对指定文件跑 git diff -U0 并解析出每个文件的 hunk 范围(oldStart/oldCount/newStart/newCount)。" +
|
|
1045
|
+
"用于独立复核 executor 到底改了哪些行、是否越界。cwd 不是 git 仓库或 git 失败时返回 files:[] 与 error 字段(不算工具错误)。",
|
|
1046
|
+
inputSchema: {
|
|
1047
|
+
type: "object",
|
|
1048
|
+
properties: {
|
|
1049
|
+
cwd: { type: "string", description: "仓库工作目录,默认 process.cwd()" },
|
|
1050
|
+
paths: { type: "array", items: { type: "string" }, description: "要检查的文件路径列表" },
|
|
1051
|
+
},
|
|
1052
|
+
},
|
|
1053
|
+
handler: async (args) => {
|
|
1054
|
+
try {
|
|
1055
|
+
const a = args || {};
|
|
1056
|
+
const cwd = a.cwd || process.cwd();
|
|
1057
|
+
const paths = asStringArray(a.paths);
|
|
1058
|
+
|
|
1059
|
+
const proc = spawnSync("git", ["diff", "-U0", "--no-color", ...paths], {
|
|
1060
|
+
cwd,
|
|
1061
|
+
encoding: "utf8",
|
|
1062
|
+
shell: false,
|
|
1063
|
+
maxBuffer: 10 * 1024 * 1024,
|
|
1064
|
+
});
|
|
1065
|
+
|
|
1066
|
+
const stderr = proc.stderr ? String(proc.stderr) : "";
|
|
1067
|
+
if (proc.error || (proc.status !== 0 && !stderr)) {
|
|
1068
|
+
return ok({
|
|
1069
|
+
ok: false,
|
|
1070
|
+
files: [],
|
|
1071
|
+
raw_length: 0,
|
|
1072
|
+
error: proc.error ? String(proc.error.message || proc.error) : (stderr || `git exited with status ${proc.status}`),
|
|
1073
|
+
});
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
const out = proc.stdout ? String(proc.stdout) : "";
|
|
1077
|
+
const files = [];
|
|
1078
|
+
let current = null;
|
|
1079
|
+
for (const line of out.split("\n")) {
|
|
1080
|
+
if (line.startsWith("+++ ")) {
|
|
1081
|
+
const p = line.slice(4).trim();
|
|
1082
|
+
current = { path: p.startsWith("b/") ? p.slice(2) : p, hunks: [] };
|
|
1083
|
+
files.push(current);
|
|
1084
|
+
continue;
|
|
1085
|
+
}
|
|
1086
|
+
if (line.startsWith("@@")) {
|
|
1087
|
+
const m = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/.exec(line);
|
|
1088
|
+
if (m && current) {
|
|
1089
|
+
current.hunks.push({
|
|
1090
|
+
oldStart: Number(m[1]),
|
|
1091
|
+
oldCount: m[2] === undefined ? 1 : Number(m[2]),
|
|
1092
|
+
newStart: Number(m[3]),
|
|
1093
|
+
newCount: m[4] === undefined ? 1 : Number(m[4]),
|
|
1094
|
+
});
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
return ok({ ok: true, files, raw_length: out.length });
|
|
1100
|
+
} catch (e) {
|
|
1101
|
+
return fail(e && e.message ? e.message : String(e));
|
|
1102
|
+
}
|
|
1103
|
+
},
|
|
1104
|
+
},
|
|
1105
|
+
|
|
1106
|
+
// 9 -----------------------------------------------------------------------
|
|
1107
|
+
{
|
|
1108
|
+
name: "flow_bypass",
|
|
1109
|
+
description:
|
|
1110
|
+
"开关免检模式(bypass)。当用户明确说“直接干 / 跳过门禁 / 免检 / 别管流程”时调用。" +
|
|
1111
|
+
"on=true 打开,on=false 关闭,每次开关都会写入审计流水并留下时间戳与理由。" +
|
|
1112
|
+
"仅在用户明确授权时使用,不要自行开启。",
|
|
1113
|
+
inputSchema: {
|
|
1114
|
+
type: "object",
|
|
1115
|
+
properties: {
|
|
1116
|
+
sessionId: { type: "string", description: "会话 ID,默认 unknown" },
|
|
1117
|
+
on: { type: "boolean", description: "true 开启免检,false 关闭" },
|
|
1118
|
+
confirmed: { type: "boolean", description: "开启免检时必须为 true(表示用户已明确授权)" },
|
|
1119
|
+
reason: { type: "string", description: "开启/关闭理由(建议写用户原话)" },
|
|
1120
|
+
},
|
|
1121
|
+
required: ["on"],
|
|
1122
|
+
},
|
|
1123
|
+
handler: async (args) => {
|
|
1124
|
+
try {
|
|
1125
|
+
const a = args || {};
|
|
1126
|
+
const sessionId = a.sessionId || "unknown";
|
|
1127
|
+
const state = loadState(sessionId);
|
|
1128
|
+
|
|
1129
|
+
if (!state.card) {
|
|
1130
|
+
return fail("没有留痕,无法开启免检。免检只能在已提交任务卡的前提下使用。");
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
// Phase 1-1: 开启免检必须显式确认,不允许隐式触发
|
|
1134
|
+
if (a.on === true && a.confirmed !== true) {
|
|
1135
|
+
audit("decision", sessionId, { kind: "bypass_denied", reason: "未确认" });
|
|
1136
|
+
return fail("开启免检需要显式确认:用户必须明确授权后,传入 confirmed=true。");
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
state.bypass = a.on === true;
|
|
1140
|
+
state.bypass_ts = Date.now();
|
|
1141
|
+
saveState(sessionId, state);
|
|
1142
|
+
audit("decision", sessionId, {
|
|
1143
|
+
kind: state.bypass ? "bypass_on" : "bypass_off",
|
|
1144
|
+
reason: a.reason,
|
|
1145
|
+
confirmed: a.confirmed === true,
|
|
1146
|
+
});
|
|
1147
|
+
return ok({
|
|
1148
|
+
ok: true,
|
|
1149
|
+
bypass: state.bypass,
|
|
1150
|
+
reason: a.reason,
|
|
1151
|
+
note: state.bypass ? "免检已开启;下次 flow_dispatch 后会自动清零。" : "免检已关闭。",
|
|
1152
|
+
});
|
|
1153
|
+
} catch (e) {
|
|
1154
|
+
return fail(e && e.message ? e.message : String(e));
|
|
1155
|
+
}
|
|
1156
|
+
},
|
|
1157
|
+
},
|
|
1158
|
+
|
|
1159
|
+
// 10 -----------------------------------------------------------------------
|
|
1160
|
+
{
|
|
1161
|
+
name: "flow_add_user_msg",
|
|
1162
|
+
description:
|
|
1163
|
+
"写入一条用户原话到会话消息文件,供 quote 锚点校验使用。" +
|
|
1164
|
+
"在提交任务卡之前调用,把用户消息原文存下来。",
|
|
1165
|
+
inputSchema: {
|
|
1166
|
+
type: "object",
|
|
1167
|
+
properties: {
|
|
1168
|
+
sessionId: { type: "string", description: "会话 ID,默认 unknown" },
|
|
1169
|
+
text: { type: "string", description: "用户原话原文" },
|
|
1170
|
+
},
|
|
1171
|
+
},
|
|
1172
|
+
handler: async (args) => {
|
|
1173
|
+
try {
|
|
1174
|
+
const a = args || {};
|
|
1175
|
+
const sessionId = a.sessionId || "unknown";
|
|
1176
|
+
const text = String(a.text || "");
|
|
1177
|
+
if (!text) return fail("text 不能为空");
|
|
1178
|
+
const ok2 = writeUserMessage(sessionId, text);
|
|
1179
|
+
return ok({ ok: ok2, sessionId, text_len: text.length });
|
|
1180
|
+
} catch (e) {
|
|
1181
|
+
return fail(e && e.message ? e.message : String(e));
|
|
1182
|
+
}
|
|
1183
|
+
},
|
|
1184
|
+
},
|
|
1185
|
+
|
|
1186
|
+
// 11 -----------------------------------------------------------------------
|
|
1187
|
+
{
|
|
1188
|
+
name: "flow_save_result",
|
|
1189
|
+
description:
|
|
1190
|
+
"保存子 Agent(executor/auditor)的执行结果到会话目录。" +
|
|
1191
|
+
"主 Agent 通过 subagent 工具拿到结果后调用本工具持久化,便于后续审计与还原。",
|
|
1192
|
+
inputSchema: {
|
|
1193
|
+
type: "object",
|
|
1194
|
+
properties: {
|
|
1195
|
+
sessionId: { type: "string", description: "会话 ID,默认 unknown" },
|
|
1196
|
+
role: { type: "string", enum: ["executor", "auditor"], description: "执行角色" },
|
|
1197
|
+
stdout: { type: "string", description: "子 Agent 的 stdout" },
|
|
1198
|
+
stderr: { type: "string", description: "子 Agent 的 stderr" },
|
|
1199
|
+
code: { type: "number", description: "退出码" },
|
|
1200
|
+
},
|
|
1201
|
+
},
|
|
1202
|
+
handler: async (args) => {
|
|
1203
|
+
try {
|
|
1204
|
+
const a = args || {};
|
|
1205
|
+
const sessionId = a.sessionId || "unknown";
|
|
1206
|
+
const role = a.role === "auditor" ? "auditor" : "executor";
|
|
1207
|
+
const file = saveAgentResult(sessionId, role, a.stdout, a.stderr, a.code);
|
|
1208
|
+
audit("run", sessionId, { kind: "agent_result", role, code: a.code, file });
|
|
1209
|
+
return ok({ ok: !!file, sessionId, role, file });
|
|
1210
|
+
} catch (e) {
|
|
1211
|
+
return fail(e && e.message ? e.message : String(e));
|
|
1212
|
+
}
|
|
1213
|
+
},
|
|
1214
|
+
},
|
|
1215
|
+
|
|
1216
|
+
// 12 -----------------------------------------------------------------------
|
|
1217
|
+
{
|
|
1218
|
+
name: "flow_revert_plan",
|
|
1219
|
+
description:
|
|
1220
|
+
"根据本次会话的快照 manifest 生成还原计划:新建文件(created)→ delete,原有文件 → 从 backup 恢复到 orig。" +
|
|
1221
|
+
"只返回计划,绝不自动执行;实际还原需人工确认后按 plan 操作。用于改动出问题需要回滚的场景。",
|
|
1222
|
+
inputSchema: {
|
|
1223
|
+
type: "object",
|
|
1224
|
+
properties: {
|
|
1225
|
+
sessionId: { type: "string", description: "会话 ID,默认 unknown" },
|
|
1226
|
+
},
|
|
1227
|
+
},
|
|
1228
|
+
handler: async (args) => {
|
|
1229
|
+
try {
|
|
1230
|
+
const a = args || {};
|
|
1231
|
+
const sessionId = a.sessionId || "unknown";
|
|
1232
|
+
const entries = readManifest(sessionId);
|
|
1233
|
+
const plan = [];
|
|
1234
|
+
for (const entry of entries) {
|
|
1235
|
+
if (!entry) continue;
|
|
1236
|
+
if (entry.created === true) {
|
|
1237
|
+
plan.push({ action: "delete", target: entry.orig, source: null });
|
|
1238
|
+
} else {
|
|
1239
|
+
plan.push({ action: "restore", source: entry.backup, target: entry.orig });
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
return ok({
|
|
1243
|
+
sessionId,
|
|
1244
|
+
count: plan.length,
|
|
1245
|
+
plan,
|
|
1246
|
+
note: "这是还原计划,不会自动执行;实际还原需人工确认后按 plan 操作。",
|
|
1247
|
+
});
|
|
1248
|
+
} catch (e) {
|
|
1249
|
+
return fail(e && e.message ? e.message : String(e));
|
|
1250
|
+
}
|
|
1251
|
+
},
|
|
1252
|
+
},
|
|
1253
|
+
|
|
1254
|
+
// 13 -----------------------------------------------------------------------
|
|
1255
|
+
{
|
|
1256
|
+
name: "flow_run_verify",
|
|
1257
|
+
description:
|
|
1258
|
+
"执行卡片中指定的验证命令,返回执行结果(退出码、stdout、stderr)。" +
|
|
1259
|
+
"用于在派发 executor 后验证其工作成果,或手动触发验证。",
|
|
1260
|
+
inputSchema: {
|
|
1261
|
+
type: "object",
|
|
1262
|
+
properties: {
|
|
1263
|
+
sessionId: { type: "string", description: "会话 ID,默认 unknown" },
|
|
1264
|
+
cwd: { type: "string", description: "工作目录,默认 process.cwd()" },
|
|
1265
|
+
},
|
|
1266
|
+
},
|
|
1267
|
+
handler: async (args) => {
|
|
1268
|
+
try {
|
|
1269
|
+
const a = args || {};
|
|
1270
|
+
const sessionId = a.sessionId || "unknown";
|
|
1271
|
+
const state = loadState(sessionId);
|
|
1272
|
+
if (!state.card) return fail("没有留痕,无法执行验证。");
|
|
1273
|
+
if (!state.card.verify || !state.card.verify.cmd) return fail("卡片未指定验证命令。");
|
|
1274
|
+
|
|
1275
|
+
// 工作目录优先级:显式 cwd > 卡片提交时的项目目录 > server 进程 cwd。
|
|
1276
|
+
// 旧实现只用后两者中的 process.cwd() —— MCP server 是常驻进程,
|
|
1277
|
+
// 它的 cwd 是**启动时**的目录(可能是几个月前的某个旧仓库),
|
|
1278
|
+
// 于是 verify.cmd 会在错误的目录里跑,测试脚本找不到、路径全错,
|
|
1279
|
+
// 而门禁把这种失败当成了「验证不通过」(真 PASS 被误杀)。
|
|
1280
|
+
const cardProject = state.card.env && typeof state.card.env.project === "string"
|
|
1281
|
+
? state.card.env.project
|
|
1282
|
+
: "";
|
|
1283
|
+
const workdir = a.cwd || cardProject || undefined;
|
|
1284
|
+
const result = await runVerifyCommand(state.card.verify.cmd, workdir);
|
|
1285
|
+
const state2 = loadState(sessionId);
|
|
1286
|
+
state2.last_verify = {
|
|
1287
|
+
ts: new Date().toISOString(),
|
|
1288
|
+
cmd: state.card.verify.cmd,
|
|
1289
|
+
cwd: workdir || process.cwd(),
|
|
1290
|
+
exitCode: result.exitCode,
|
|
1291
|
+
ok: result.ok,
|
|
1292
|
+
// 输出指纹:把「验证通过」绑定到一次可复现的运行,而不是「我说过了」。
|
|
1293
|
+
output_hash: computeOutputHash(result.exitCode, result.stdout, result.stderr),
|
|
1294
|
+
// 代码状态指纹:把「验证通过」绑定到**当时的代码状态**。
|
|
1295
|
+
// 没有它,验证通过后再改坏代码,flow_verdict(PASS) 仍会放行(见 A1)。
|
|
1296
|
+
fingerprint: workspaceFingerprint(workdir || process.cwd()),
|
|
1297
|
+
};
|
|
1298
|
+
saveState(sessionId, state2);
|
|
1299
|
+
|
|
1300
|
+
return ok({ ok: result.ok, exitCode: result.exitCode, stdout: result.stdout, stderr: result.stderr, cwd: workdir || process.cwd() });
|
|
1301
|
+
} catch (e) {
|
|
1302
|
+
return fail(e && e.message ? e.message : String(e));
|
|
1303
|
+
}
|
|
1304
|
+
},
|
|
1305
|
+
},
|
|
1306
|
+
|
|
1307
|
+
// 13b -----------------------------------------------------------------------
|
|
1308
|
+
{
|
|
1309
|
+
name: "flow_falsify",
|
|
1310
|
+
description:
|
|
1311
|
+
"负对照(negative control):把工作区还原到派发时的「改动前」状态跑同一 verify.cmd,要求它**失败**;" +
|
|
1312
|
+
"再还原「改动后」状态跑一次,要求它**通过**。只有「改动前失败 + 改动后通过」才证明这个测试真的在测本次改动" +
|
|
1313
|
+
"(而不是 `|| true` / `echo ok` 这类空测试)。记 PASS 裁定前必须先做负对照。",
|
|
1314
|
+
inputSchema: {
|
|
1315
|
+
type: "object",
|
|
1316
|
+
properties: {
|
|
1317
|
+
sessionId: { type: "string", description: "会话 ID,默认 unknown" },
|
|
1318
|
+
cwd: { type: "string", description: "工作目录,默认 process.cwd()" },
|
|
1319
|
+
},
|
|
1320
|
+
},
|
|
1321
|
+
handler: async (args) => {
|
|
1322
|
+
try {
|
|
1323
|
+
const a = args || {};
|
|
1324
|
+
const sessionId = a.sessionId || "unknown";
|
|
1325
|
+
const state = loadState(sessionId);
|
|
1326
|
+
if (!state.card) return fail("没有留痕,无法做负对照。");
|
|
1327
|
+
if (!state.card.verify || !state.card.verify.cmd) return fail("卡片未指定验证命令,无法做负对照。");
|
|
1328
|
+
|
|
1329
|
+
const cardProject = state.card.env && typeof state.card.env.project === "string"
|
|
1330
|
+
? state.card.env.project
|
|
1331
|
+
: "";
|
|
1332
|
+
const workdir = a.cwd || cardProject || undefined;
|
|
1333
|
+
|
|
1334
|
+
const r = await runNegativeControl(sessionId, state.card.verify.cmd, workdir);
|
|
1335
|
+
if (!r.ok) {
|
|
1336
|
+
audit("run", sessionId, { kind: "falsify_denied", reason: r.error || "负对照无法执行" });
|
|
1337
|
+
return fail(r.error || "负对照无法执行");
|
|
1338
|
+
}
|
|
1339
|
+
|
|
1340
|
+
const state2 = loadState(sessionId);
|
|
1341
|
+
state2.last_falsify = {
|
|
1342
|
+
ts: new Date().toISOString(),
|
|
1343
|
+
cmd: state.card.verify.cmd,
|
|
1344
|
+
cwd: workdir || process.cwd(),
|
|
1345
|
+
verdict: r.verdict,
|
|
1346
|
+
reason: r.reason,
|
|
1347
|
+
before: { exitCode: r.before.exitCode, ok: r.before.ok, hash: r.before.hash },
|
|
1348
|
+
after: { exitCode: r.after.exitCode, ok: r.after.ok, hash: r.after.hash },
|
|
1349
|
+
};
|
|
1350
|
+
// 把「改动后」的运行写为 last_verify —— 裁定的证据绑定到这一次运行。
|
|
1351
|
+
state2.last_verify = {
|
|
1352
|
+
ts: new Date().toISOString(),
|
|
1353
|
+
cmd: state.card.verify.cmd,
|
|
1354
|
+
cwd: workdir || process.cwd(),
|
|
1355
|
+
exitCode: r.after.exitCode,
|
|
1356
|
+
ok: r.after.ok,
|
|
1357
|
+
output_hash: r.after.hash,
|
|
1358
|
+
// 代码状态指纹:负对照同样是一次「验证通过」,
|
|
1359
|
+
// 必须与 flow_run_verify 一样绑定代码状态,
|
|
1360
|
+
// 否则「先负对照、再改代码、再记 PASS」这条路径会绕过 STALE 闸门。
|
|
1361
|
+
fingerprint: workspaceFingerprint(workdir || process.cwd()),
|
|
1362
|
+
};
|
|
1363
|
+
saveState(sessionId, state2);
|
|
1364
|
+
|
|
1365
|
+
audit("run", sessionId, {
|
|
1366
|
+
kind: "falsify",
|
|
1367
|
+
verdict: r.verdict,
|
|
1368
|
+
before_exit: r.before.exitCode,
|
|
1369
|
+
after_exit: r.after.exitCode,
|
|
1370
|
+
before_hash: r.before.hash,
|
|
1371
|
+
after_hash: r.after.hash,
|
|
1372
|
+
});
|
|
1373
|
+
|
|
1374
|
+
const trunc = (s) => (typeof s === "string" && s.length > 2000 ? s.slice(0, 2000) + "…" : s);
|
|
1375
|
+
return ok({
|
|
1376
|
+
ok: true,
|
|
1377
|
+
verdict: r.verdict,
|
|
1378
|
+
reason: r.reason,
|
|
1379
|
+
before: { exitCode: r.before.exitCode, ok: r.before.ok, hash: r.before.hash, stdout: trunc(r.before.stdout), stderr: trunc(r.before.stderr) },
|
|
1380
|
+
after: { exitCode: r.after.exitCode, ok: r.after.ok, hash: r.after.hash, stdout: trunc(r.after.stdout), stderr: trunc(r.after.stderr) },
|
|
1381
|
+
note: r.verdict === "meaningful"
|
|
1382
|
+
? "负对照通过:测试有判别力,可以记 PASS。"
|
|
1383
|
+
: "负对照未通过:不能记 PASS。" + r.reason,
|
|
1384
|
+
});
|
|
1385
|
+
} catch (e) {
|
|
1386
|
+
return fail(e && e.message ? e.message : String(e));
|
|
1387
|
+
}
|
|
1388
|
+
},
|
|
1389
|
+
},
|
|
1390
|
+
|
|
1391
|
+
// 14 -----------------------------------------------------------------------
|
|
1392
|
+
{
|
|
1393
|
+
name: "flow_check_scope",
|
|
1394
|
+
description:
|
|
1395
|
+
"检查指定文件列表是否在卡片 scope 范围内。用于在提交裁定前自查越界改动。",
|
|
1396
|
+
inputSchema: {
|
|
1397
|
+
type: "object",
|
|
1398
|
+
properties: {
|
|
1399
|
+
sessionId: { type: "string", description: "会话 ID,默认 unknown" },
|
|
1400
|
+
paths: { type: "array", items: { type: "string" }, description: "要检查的文件路径列表" },
|
|
1401
|
+
cwd: { type: "string", description: "仓库工作目录,默认 process.cwd()" },
|
|
1402
|
+
},
|
|
1403
|
+
},
|
|
1404
|
+
handler: async (args) => {
|
|
1405
|
+
try {
|
|
1406
|
+
const a = args || {};
|
|
1407
|
+
const sessionId = a.sessionId || "unknown";
|
|
1408
|
+
const state = loadState(sessionId);
|
|
1409
|
+
if (!state.card) return fail("没有留痕,无法检查 scope。");
|
|
1410
|
+
|
|
1411
|
+
const scope = Array.isArray(state.card.scope) ? state.card.scope : [];
|
|
1412
|
+
const paths = Array.isArray(a.paths) ? a.paths : [];
|
|
1413
|
+
// 不传 paths → 自动跑 `git diff --name-only` 取当前实际改动清单。
|
|
1414
|
+
// 传了 paths → 用调用方给的清单(自查场景)。
|
|
1415
|
+
const result = paths.length
|
|
1416
|
+
? enforceScope(scope, a.cwd, paths)
|
|
1417
|
+
: enforceScope(scope, a.cwd);
|
|
1418
|
+
|
|
1419
|
+
return ok({ ok: result.ok, scope, checked: paths.length ? paths : result.checked || [], ...result });
|
|
1420
|
+
} catch (e) {
|
|
1421
|
+
return fail(e && e.message ? e.message : String(e));
|
|
1422
|
+
}
|
|
1423
|
+
},
|
|
1424
|
+
},
|
|
1425
|
+
// 15 -----------------------------------------------------------------------
|
|
1426
|
+
{
|
|
1427
|
+
name: "flow_trigger_check",
|
|
1428
|
+
description:
|
|
1429
|
+
"智能触发检查:判断当前这条用户消息是否应该启动 Carlos 工作流,并在达标时**实际完成激活**(写入用户原话 + 开占位卡 → 会话进入门禁态)。" +
|
|
1430
|
+
"宿主在收到用户消息时调用本工具,就能获得与命令行客户端完全一致的自动触发行为。" +
|
|
1431
|
+
"未达阈值时只如实返回评分与决策,不做任何动作;被限流时也如实返回真实评分。" +
|
|
1432
|
+
"中文流程词(工作流/门禁/派发/审计…)已内置,中文需求同样能触发。",
|
|
1433
|
+
inputSchema: {
|
|
1434
|
+
type: "object",
|
|
1435
|
+
properties: {
|
|
1436
|
+
sessionId: { type: "string", description: "会话 ID,默认 unknown" },
|
|
1437
|
+
message: { type: "string", description: "用户本轮消息原文(必须是真实原话,用于 quote 锚点)" },
|
|
1438
|
+
complexity: { type: "number", description: "任务复杂度系数,默认 1.0" },
|
|
1439
|
+
execute: { type: "boolean", description: "true = 达标时实际执行激活步骤(默认 true);false = 只评分不动作" },
|
|
1440
|
+
cwd: { type: "string", description: "工作目录,默认 process.cwd()" },
|
|
1441
|
+
},
|
|
1442
|
+
required: ["message"],
|
|
1443
|
+
},
|
|
1444
|
+
handler: async (args) => {
|
|
1445
|
+
try {
|
|
1446
|
+
const a = args || {};
|
|
1447
|
+
const sessionId = a.sessionId || "unknown";
|
|
1448
|
+
const message = String(a.message || "");
|
|
1449
|
+
if (!message.trim()) {
|
|
1450
|
+
return fail("message 不能为空:触发检查需要用户本轮消息原文(quote 锚点与评分都依赖它)");
|
|
1451
|
+
}
|
|
1452
|
+
const shouldExecute = a.execute !== false;
|
|
1453
|
+
|
|
1454
|
+
// 触发引擎(纯 JS,零外部依赖):与 CLI 客户端共用同一份实现,
|
|
1455
|
+
// 保证「命令行路径」与「MCP 路径」的评分与决策完全一致。
|
|
1456
|
+
const { TriggerEngine } = await import("./trigger.js");
|
|
1457
|
+
const result = TriggerEngine.processUserMessage(message, {
|
|
1458
|
+
sessionId,
|
|
1459
|
+
complexity: typeof a.complexity === "number" ? a.complexity : 1.0,
|
|
1460
|
+
});
|
|
1461
|
+
|
|
1462
|
+
const base = {
|
|
1463
|
+
ok: true,
|
|
1464
|
+
sessionId,
|
|
1465
|
+
decision: result.decision,
|
|
1466
|
+
score: result.score,
|
|
1467
|
+
confidence: result.confidence,
|
|
1468
|
+
breakdown: result.breakdown,
|
|
1469
|
+
reason: result.reason,
|
|
1470
|
+
should_trigger: result.shouldTrigger === true,
|
|
1471
|
+
executed: [],
|
|
1472
|
+
};
|
|
1473
|
+
|
|
1474
|
+
// 未达标 / 被限流:只如实报告,不做任何动作。
|
|
1475
|
+
if (!result.shouldTrigger || !result.activationPlan || !result.activationPlan.activated) {
|
|
1476
|
+
return ok({
|
|
1477
|
+
...base,
|
|
1478
|
+
activated: false,
|
|
1479
|
+
pending_fields: (result.activationPlan && result.activationPlan.pendingFields) || [],
|
|
1480
|
+
note: result.decision === "throttled"
|
|
1481
|
+
? "被限流:本轮不触发(已如实返回真实评分)"
|
|
1482
|
+
: (result.decision === "review"
|
|
1483
|
+
? "接近阈值:建议手动启动门禁,未自动激活"
|
|
1484
|
+
: "未达触发阈值:不激活"),
|
|
1485
|
+
});
|
|
1486
|
+
}
|
|
1487
|
+
|
|
1488
|
+
// 只读调研意图不进门禁(占位卡恒为 read_only,待补项永远无法满足 → 会话会被锁死)
|
|
1489
|
+
const plan = result.activationPlan;
|
|
1490
|
+
if (plan.activated === false) {
|
|
1491
|
+
return ok({
|
|
1492
|
+
...base,
|
|
1493
|
+
activated: false,
|
|
1494
|
+
pending_fields: plan.pendingFields || [],
|
|
1495
|
+
note: plan.reason || "不激活",
|
|
1496
|
+
});
|
|
1497
|
+
}
|
|
1498
|
+
|
|
1499
|
+
if (!shouldExecute) {
|
|
1500
|
+
return ok({
|
|
1501
|
+
...base,
|
|
1502
|
+
activated: false,
|
|
1503
|
+
execute_suppressed: true,
|
|
1504
|
+
pending_fields: plan.pendingFields || [],
|
|
1505
|
+
note: "只评分不动作(execute=false)",
|
|
1506
|
+
});
|
|
1507
|
+
}
|
|
1508
|
+
|
|
1509
|
+
// ---- 执行激活计划中 auto=true 的步骤 ----
|
|
1510
|
+
// 安全约束(与 CLI 客户端一致):
|
|
1511
|
+
// · 会话已有任务卡 → 不自动开卡(不覆盖用户已有任务)
|
|
1512
|
+
// · 只执行 auto 步骤;needsFill 步骤交回 Agent 补齐
|
|
1513
|
+
const existing = loadState(sessionId);
|
|
1514
|
+
if (existing.card) {
|
|
1515
|
+
return ok({
|
|
1516
|
+
...base,
|
|
1517
|
+
activated: false,
|
|
1518
|
+
note: "会话已有任务卡,跳过自动开卡(不覆盖用户已有任务)",
|
|
1519
|
+
});
|
|
1520
|
+
}
|
|
1521
|
+
|
|
1522
|
+
const executed = [];
|
|
1523
|
+
for (const step of plan.steps || []) {
|
|
1524
|
+
if (!step || step.auto !== true) continue;
|
|
1525
|
+
const stepArgs = { ...(step.args || {}), sessionId };
|
|
1526
|
+
if (step.tool === "flow_add_user_msg") {
|
|
1527
|
+
writeUserMessage(sessionId, String(stepArgs.text || message));
|
|
1528
|
+
executed.push({ tool: step.tool, ok: true });
|
|
1529
|
+
} else if (step.tool === "flow_submit_requirement") {
|
|
1530
|
+
const r = validateCard(stepArgs, {
|
|
1531
|
+
userMessages: [message],
|
|
1532
|
+
scopeExists: () => true,
|
|
1533
|
+
confirmed: stepArgs.confirmed === true,
|
|
1534
|
+
});
|
|
1535
|
+
if (!r || r.ok !== true) {
|
|
1536
|
+
executed.push({ tool: step.tool, ok: false, reason: (r && r.reason) || "卡片不合规" });
|
|
1537
|
+
continue;
|
|
1538
|
+
}
|
|
1539
|
+
const card = { ...stepArgs };
|
|
1540
|
+
card.env = { git_head: gitHead(a.cwd || process.cwd()), project: a.cwd || process.cwd() };
|
|
1541
|
+
const tier = riskTier(card.scope);
|
|
1542
|
+
const withLock = withStateLock(sessionId, () => {
|
|
1543
|
+
const prev = loadState(sessionId);
|
|
1544
|
+
const state = {
|
|
1545
|
+
...defaultState(),
|
|
1546
|
+
...prev,
|
|
1547
|
+
card,
|
|
1548
|
+
card_ts: Date.now(),
|
|
1549
|
+
tier,
|
|
1550
|
+
dispatch_count: 0,
|
|
1551
|
+
claimed: false,
|
|
1552
|
+
claimed_role: null,
|
|
1553
|
+
write_files: 0,
|
|
1554
|
+
last_user_ts: Date.now(),
|
|
1555
|
+
bypass: false,
|
|
1556
|
+
verdict: null,
|
|
1557
|
+
completed_ok: false,
|
|
1558
|
+
stale: false,
|
|
1559
|
+
// 幂等派生:待补项由当前卡片内容算出,落盘状态自洽
|
|
1560
|
+
activation_pending: ["scope", "acceptance", "verify.cmd"],
|
|
1561
|
+
activation_activated: true,
|
|
1562
|
+
};
|
|
1563
|
+
saveState(sessionId, state);
|
|
1564
|
+
return state;
|
|
1565
|
+
});
|
|
1566
|
+
audit("run", sessionId, { kind: "activation", tier, goal: card.goal, pending: withLock.activation_pending });
|
|
1567
|
+
executed.push({ tool: step.tool, ok: true, tier });
|
|
1568
|
+
}
|
|
1569
|
+
}
|
|
1570
|
+
|
|
1571
|
+
const after = loadState(sessionId);
|
|
1572
|
+
return ok({
|
|
1573
|
+
...base,
|
|
1574
|
+
activated: true,
|
|
1575
|
+
executed,
|
|
1576
|
+
has_card: !!after.card,
|
|
1577
|
+
activation_pending: Array.isArray(after.activation_pending) ? after.activation_pending : [],
|
|
1578
|
+
pending_fields: plan.pendingFields || [],
|
|
1579
|
+
note: "已自动激活:会话进入门禁态,补齐 scope/acceptance/verify.cmd 后方可派发",
|
|
1580
|
+
});
|
|
1581
|
+
} catch (e) {
|
|
1582
|
+
return fail(e && e.message ? e.message : String(e));
|
|
1583
|
+
}
|
|
1584
|
+
},
|
|
1585
|
+
},
|
|
1586
|
+
];
|
|
1587
|
+
|
|
1588
|
+
// ---- 服务器装配 ------------------------------------------------------------
|
|
1589
|
+
|
|
1590
|
+
// server 版本号从 package.json 读,避免硬编码与发布版本脱节。
|
|
1591
|
+
// 历史问题:此前写死 "1.0.0",而 package.json 已是 2.12.0 ——
|
|
1592
|
+
// 客户端 initialize 拿到的是错版本,无法据此判断真实能力。
|
|
1593
|
+
const pkgVersion = (() => {
|
|
1594
|
+
try {
|
|
1595
|
+
const pkgUrl = new URL("../package.json", import.meta.url);
|
|
1596
|
+
return JSON.parse(readFileSync(pkgUrl, "utf8")).version || "0.0.0";
|
|
1597
|
+
} catch {
|
|
1598
|
+
return "0.0.0";
|
|
1599
|
+
}
|
|
1600
|
+
})();
|
|
1601
|
+
|
|
1602
|
+
const server = new Server(
|
|
1603
|
+
{ name: "carlos-flow", version: pkgVersion },
|
|
1604
|
+
{ capabilities: { tools: {} } },
|
|
1605
|
+
);
|
|
1606
|
+
|
|
1607
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
1608
|
+
tools: TOOLS.map((t) => ({ name: t.name, description: t.description, inputSchema: t.inputSchema })),
|
|
1609
|
+
}));
|
|
1610
|
+
|
|
1611
|
+
// ⚠️ 这里曾经**重复注册**过一次 CallToolRequestSchema(另一个不带参数校验的版本)。
|
|
1612
|
+
// MCP SDK 的 setRequestHandler 用 Map.set → 后注册的覆盖先注册的,
|
|
1613
|
+
// 于是先注册的那个(含约 17 行)**永远不会执行** —— 当前行为是对的,
|
|
1614
|
+
// 但它靠的是「注册顺序」这种隐式约定:任何一次重排或插入都会静默
|
|
1615
|
+
// 丢掉参数校验(required 字段 / 类型 / enum 全部失效)。
|
|
1616
|
+
// 现在只保留下面那一个带 validateToolArgs 的注册。
|
|
1617
|
+
//
|
|
1618
|
+
// check:no-dead-handler 门禁会统计同一 schema 的注册次数,>1 即红。
|
|
1619
|
+
|
|
1620
|
+
// ---- Phase 1-1: 工具参数校验器(required 字段 / 类型 / enum)----
|
|
1621
|
+
function validateToolArgs(toolName, args) {
|
|
1622
|
+
const tool = TOOLS.find((t) => t.name === toolName);
|
|
1623
|
+
if (!tool || !tool.inputSchema) return { ok: true };
|
|
1624
|
+
const schema = tool.inputSchema;
|
|
1625
|
+
|
|
1626
|
+
if (Array.isArray(schema.required)) {
|
|
1627
|
+
for (const field of schema.required) {
|
|
1628
|
+
const v = args[field];
|
|
1629
|
+
if (v === undefined || v === null || v === "") {
|
|
1630
|
+
return { ok: false, error: "缺少必填字段:" + field };
|
|
1631
|
+
}
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
|
|
1635
|
+
if (schema.properties) {
|
|
1636
|
+
for (const [key, prop] of Object.entries(schema.properties)) {
|
|
1637
|
+
const v = args[key];
|
|
1638
|
+
if (v === undefined || v === null) continue;
|
|
1639
|
+
if (prop.type) {
|
|
1640
|
+
const expected = prop.type === "integer" ? "number" : prop.type;
|
|
1641
|
+
if (expected === "number" && typeof v !== "number") {
|
|
1642
|
+
return { ok: false, error: "字段 " + key + " 类型错误,期望 number" };
|
|
1643
|
+
}
|
|
1644
|
+
if (expected === "array" && !Array.isArray(v)) {
|
|
1645
|
+
return { ok: false, error: "字段 " + key + " 类型错误,期望 array" };
|
|
1646
|
+
}
|
|
1647
|
+
if (expected === "string" && typeof v !== "string") {
|
|
1648
|
+
return { ok: false, error: "字段 " + key + " 类型错误,期望 string" };
|
|
1649
|
+
}
|
|
1650
|
+
if (expected === "boolean" && typeof v !== "boolean") {
|
|
1651
|
+
return { ok: false, error: "字段 " + key + " 类型错误,期望 boolean" };
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
// 数组**元素**类型校验:schema 已声明 items:{type:"string"} 时,
|
|
1655
|
+
// 只校验顶层 array 类型是不够的 —— 元素类型不符会一路传到 Node 内部
|
|
1656
|
+
// 才报错(例:flow_check_scope 的 paths:["README.md",123] 会抛
|
|
1657
|
+
// "The \"path\" argument must be of type string" 这种底层错误),
|
|
1658
|
+
// 调用方拿到的是不可读的堆栈信息。改为在入口就给出明确提示。
|
|
1659
|
+
// 注意:**null 也要拦**(typeof null !== "string")—— 否则 unknowns=[null]
|
|
1660
|
+
// 这类值会静默落盘(虽然不参与档位必填、不进 executor 合同文本,
|
|
1661
|
+
// 但会在 flow_status 回显整卡时露出 null 元素)。
|
|
1662
|
+
if (prop.type === "array" && Array.isArray(v) && prop.items && prop.items.type) {
|
|
1663
|
+
const want = prop.items.type === "integer" ? "number" : prop.items.type;
|
|
1664
|
+
for (let i = 0; i < v.length; i++) {
|
|
1665
|
+
const el = v[i];
|
|
1666
|
+
if (el === undefined) continue;
|
|
1667
|
+
const bad =
|
|
1668
|
+
(want === "number" && typeof el !== "number") ||
|
|
1669
|
+
(want === "string" && typeof el !== "string") ||
|
|
1670
|
+
(want === "boolean" && typeof el !== "boolean") ||
|
|
1671
|
+
(want === "object" && (typeof el !== "object" || el === null || Array.isArray(el)));
|
|
1672
|
+
if (bad) {
|
|
1673
|
+
return { ok: false, error: "字段 " + key + "[" + i + "] 类型错误,期望 " + want };
|
|
1674
|
+
}
|
|
1675
|
+
}
|
|
1676
|
+
}
|
|
1677
|
+
if (prop.enum && !prop.enum.includes(v)) {
|
|
1678
|
+
return { ok: false, error: "字段 " + key + " 值非法,可选:" + prop.enum.join(", ") };
|
|
1679
|
+
}
|
|
1680
|
+
}
|
|
1681
|
+
}
|
|
1682
|
+
return { ok: true };
|
|
1683
|
+
}
|
|
1684
|
+
|
|
1685
|
+
// ---- CallTool:先校验参数,再分发到工具 handler ----
|
|
1686
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
1687
|
+
const toolName = request.params.name;
|
|
1688
|
+
const args = request.params.arguments || {};
|
|
1689
|
+
|
|
1690
|
+
const t = TOOLS.find((x) => x.name === toolName);
|
|
1691
|
+
if (!t) {
|
|
1692
|
+
return { content: [{ type: "text", text: "Unknown tool: " + toolName }], isError: true };
|
|
1693
|
+
}
|
|
1694
|
+
|
|
1695
|
+
const validation = validateToolArgs(toolName, args);
|
|
1696
|
+
if (!validation.ok) {
|
|
1697
|
+
return { content: [{ type: "text", text: "ERROR: " + validation.error }], isError: true };
|
|
1698
|
+
}
|
|
1699
|
+
|
|
1700
|
+
try {
|
|
1701
|
+
return await t.handler(args);
|
|
1702
|
+
} catch (e) {
|
|
1703
|
+
return {
|
|
1704
|
+
content: [{ type: "text", text: "ERROR: " + (e && e.message ? e.message : String(e)) }],
|
|
1705
|
+
isError: true,
|
|
1706
|
+
};
|
|
1707
|
+
}
|
|
1708
|
+
});
|
|
1709
|
+
|
|
1710
|
+
// ---- 传输模式:stdio(单次调用,兼容旧用法) ----
|
|
1711
|
+
const transport = new StdioServerTransport();
|
|
1712
|
+
await server.connect(transport);
|
|
1713
|
+
process.stderr.write("carlos-flow MCP server connected via stdio\n");
|