Repository navigation
Expand file tree
/
Copy pathcontext.ts
More file actions
1487 lines (1469 loc) · 116 KB
/
Copy pathcontext.ts
File metadata and controls
1487 lines (1469 loc) · 116 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
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
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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
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
import path from 'node:path';
import fs from 'node:fs';
import { mathDigest, canonicalMathJson, readMathText } from '../../math/mathRuntimeIo';
import { assertMathCanonicalPath } from '../../math/paths';
import { readLeanResidualGoalSnapshot } from '../../math/leanResidualGoals';
import { mathematicsRuntimeInstruction } from '../../math/mathRuntimeEnvironment';
import { MATH_TOOLS_SHA256_ENV, mathToolsInstruction, type MathTools } from '../../math/mathTools';
import { CERTIFICATE_KERNEL_MODULE, isCertificateSlotFile } from '../../math/certificateSlots';
import { isTheoryModuleFile, THEORY_MODULE_NAMESPACE } from '../../math/theoryModules';
import { DECLARATION_INDEX_FILE } from '../declarationIndex';
import { MAX_FAMILY_MEMBERS } from '../taskFamilies';
import type { SolverRunState } from '../archive';
import { committedSource, type SolverProblem } from '../problem';
import type { ProofCheck } from './evidence';
import { solverProofFeedback, type SolverFailure } from '../feedback';
import {
assignmentDeliveryFile,
leadPlanDeliveryFile,
isActiveFinalCandidateReview,
isFeasibilityReview,
isFinalCandidateReview,
taskGroup,
taskVersion,
type CooperativeAction,
} from '../tasks';
import type { CooperationState } from '../cooperationShared';
import { leadPlanPublicationFeedback } from '../cooperationShared';
import { contextCheckpointRejectionFeedback } from '../contextCheckpoint';
import { assignmentDeliveryReceipts } from '../deliveryReceipts';
import { statementReviewSummary, type StatementAction, type StatementReview } from '../statementReview';
import { buildProofContinuation } from '../progress';
import { originalGoalTaskFocus, originalGoalFrontierSnapshot } from './search';
import { createProofRoutePriorityIndex, selectProofRoutePriorityContext } from '../routePriority';
import { cooperativeWorkContract } from './contracts';
import {
knowledgeContextConsumers,
knowledgeContextSections,
type KnowledgeContextSnapshot,
type KnowledgeRetrieval,
} from '../knowledgeContext';
import { continuationContextSection, readContinuationSnapshot, type ContinuationSnapshot } from '../continuationInput';
import { continuationReferenceSection, referenceQuery } from '../continuationSearch';
import { knowledgeApplicationContextSections } from '../knowledgeApplications';
import { installedLibraryContextSections } from '../installedLibraryContext';
import type { LibraryAccess } from '../libraryAccess';
import {
compileContextSections,
compileTaskContext,
cooperativeResponseSections,
deliveryIndexRow,
frozenSourceContext,
landedHelperSection,
leadPlanRejectionSection,
leadResearchInputSection,
TaskContextError,
type CompiledContext,
type ContextManifest,
type ContextSection,
type TaskContextInput,
} from '../contextCompiler';
import type { CustomResearchModule, ResearchModuleSlot } from './policy';
import { defaultResearchRouteConcurrency, researchTaskRole, type ResearchWorker } from './organization';
import { leadPlanningEvidence, leadPlanningEvidenceText } from '../planningEvidence';
import { mathematicalGapContextText } from '../mathematicalGaps';
import { nextCheckContextText } from '../nextChecks';
import { goalProgressTaskContext } from '../goalProgress';
import { assertMathAgentSkillsCurrent, type MathAgentSkill } from '../../runtime/agentSkills';
import {
buildCommonResearchContext,
buildCommonResearchGuide,
cooperativeDeliveryProtocol,
cooperativeHelperProtocol,
leadPlanToolInstruction,
} from '../researchGuidance';
import { agentTeamContextSection, buildSkillResearchContext, executionLimitsSection } from '../skillResearchContext';
import type { AgentTeamAssignment, AgentTeamRole } from '../agentTeamContext';
import { researchSkillIdentity } from '../researchSkill';
import { buildResearchToolGuide, longCommandGuidance } from '../researchToolGuide';
import type { ResearchToolHelp } from '../researchToolHelp';
export type ContextAccess = Readonly<{
teamRole?: AgentTeamRole;
teamAssignment?: AgentTeamAssignment;
/** Dispatch-time observations; execution and request admission remain host enforced. */
executionLimits?: Readonly<{ turnTimeoutMs?: number; turnCheckpointMs?: number; remainingRunMs(): number }>;
root: string;
/** The bundled Agent commands (scripts/build-agent-tools.ts) for shells that cannot read `root`; default `root`/bin/math.ts. */
tools?: string;
worktree: string;
environment: NodeJS.ProcessEnv;
taskText: string;
continuation?: ContinuationSnapshot;
/** Read and digest-bound by the run; its instruction is advisory and needs no file access here. */
mathTools?: MathTools;
corpusFile?: string;
knowledgeFile?: string;
knowledgeDigest?: string;
knowledgeSnapshot?: KnowledgeContextSnapshot;
installedLibrary?: KnowledgeContextSnapshot;
libraryCatalog?: LibraryAccess;
retrievalRoot: string;
sourceFeedback?: string;
previousCheck?: ProofCheck;
previousFailures?: readonly SolverFailure[];
assignment?: CooperativeAction;
/** Prompt byte allowance only; this does not limit solving time or turns. */
maxContextBytes?: number;
maxOptionalContextBytes?: number;
customModuleRules?: readonly CustomResearchModule[];
/** Same mathematical skill for every explicitly controlled comparison arm. */
commonResearchSkill?: MathAgentSkill;
researchSkill?: MathAgentSkill;
/** Pure fixture context; real frozen problems always load their committed source. */
frozenContexts?: Readonly<Record<string, string>>;
}>;
export type ContextBundle = Readonly<{
assignment?: CooperativeAction;
workContract?: ReturnType<typeof cooperativeWorkContract>;
toolHelp?: ResearchToolHelp;
instructions: string;
message: string;
/** Same-thread dispatch compiled under the same byte budget as the full context. */
continuationMessage?: string;
continuationManifest?: ContextManifest;
/** Recompile before dispatch so an added checkpoint instruction participates in selection and accounting. */
compileDispatch?: (options: ResearchDispatchOptions, current?: ContextBundle) => CompiledContext;
/** Re-anchor exact current inputs after compaction with less optional history. */
compileCompactedDispatch?: (options: ResearchDispatchOptions, current?: ContextBundle) => CompiledContext | undefined;
scope: 'original-problem-and-allowed-library' | 'fixed-statement';
sources: Readonly<{ problemReceiptDigest: string }>;
manifest?: ContextManifest;
knowledgeRetrievals?: readonly (KnowledgeRetrieval & { included: boolean; omissionReason?: string })[];
}>;
export type ResearchDispatchOptions = Readonly<{
continuation?: boolean;
taskSwitch?: boolean;
checkpointInstruction?: string;
}>;
const dispatchPrefix = (text?: string): ContextSection[] =>
text
? [
{
id: 'dispatch-checkpoint-instruction',
kind: 'checkpoint',
text,
},
]
: [];
/** Finalize the exact message before the host saves its manifest or opens a model turn. */
function compileCustomDispatch(
context: ContextBundle,
options: ResearchDispatchOptions,
preserveInventory = true,
): CompiledContext {
const continued = options.continuation || (options.taskSwitch && context.continuationMessage !== undefined);
const previous = continued ? (context.continuationManifest ?? context.manifest) : context.manifest;
const message = continued ? (context.continuationMessage ?? context.message) : context.message;
const sourceManifest = preserveInventory
? continued && context.continuationMessage !== context.message
? context.continuationManifest
: context.manifest
: undefined;
let compiled = compileContextSections({
instructions: context.instructions,
mandatory: [
...dispatchPrefix(options.checkpointInstruction),
{ id: 'dispatch-message', kind: 'continuation', text: message },
],
maxContextBytes: previous?.maxBytes,
maxOptionalContextBytes: previous?.maxOptionalBytes,
requiredTaskIds: sourceManifest?.requiredTaskIds,
omitted: sourceManifest?.omitted,
inlineManifest: false,
});
if (sourceManifest)
compiled = {
...compiled,
manifest: {
...compiled.manifest,
included: [
...compiled.manifest.included.filter(item => item.id === 'dispatch-checkpoint-instruction'),
...sourceManifest.included,
],
optionalBytes: sourceManifest.optionalBytes,
mandatoryBytes: compiled.manifest.usedBytes - sourceManifest.optionalBytes,
},
};
return compiled;
}
export function compactedResearchDispatch(
context: ContextBundle,
options: ResearchDispatchOptions,
): ContextBundle | undefined {
let compiled: CompiledContext | undefined;
try {
compiled = context.compileCompactedDispatch?.(options, context);
} catch (error) {
// A valid full dispatch can fit when the compacted prompt's required recovery note cannot.
if (!(error instanceof TaskContextError) || error.code !== 'math_context_mandatory_overflow') throw error;
return undefined;
}
if (!compiled || Buffer.byteLength(compiled.message) >= Buffer.byteLength(context.message)) return;
return withResearchDispatch(context, compiled);
}
/** Finalize the actual M4 output, including replacements of the built-in prompt. */
export function finalizeResearchDispatch(context: ContextBundle, options: ResearchDispatchOptions = {}): ContextBundle {
const compiled = context.compileDispatch?.(options, context) ?? compileCustomDispatch(context, options);
return withResearchDispatch(context, compiled);
}
function withResearchDispatch(context: ContextBundle, compiled: CompiledContext): ContextBundle {
return {
...context,
...compiled,
...(context.knowledgeRetrievals
? {
knowledgeRetrievals: context.knowledgeRetrievals.map(retrieval => ({
...retrieval,
included: compiled.manifest.included.some(item => item.id === retrieval.sectionId),
omissionReason: compiled.manifest.omitted.find(item => item.id === retrieval.sectionId)?.reason,
})),
}
: {}),
};
}
/** Backtranslation is built from the formal snapshot alone, in a fresh session. */
export function buildStatementReviewContext(
review: StatementReview,
action: StatementAction,
problemReceiptDigest: string,
): ContextBundle {
const blind = action.kind === 'backtranslate';
const instructions =
'You are the Independent Reviewer of one fixed Lean declaration. Read only STATEMENT.json and the supplied definitions in this workspace. ' +
'Do not read parent directories, other workspaces, git history, task journals or external candidate solutions. Leave all snapshot inputs unchanged; write only RESEARCH.md. ' +
'Use the Lean-exported elaboration in STATEMENT.json to resolve names, coercions and imported definitions. Flag relevant ambiguous or unavailable definitions instead of guessing. ' +
'A translation or review is advisory and grants no Lean proof acceptance. Bound tool output in bytes.\n' +
(blind
? 'Translate the Lean statement into mathematics using its formal context only. State all quantifiers, assumptions and relevant definitions explicitly. ' +
'The intended mathematical explanation is deliberately withheld. Write the translation as ordinary Markdown in RESEARCH.md.\n'
: 'Compare the saved independent backtranslation with the intended mathematics and premises below. Check missing hypotheses, changed quantifiers and changes of meaning. ' +
'Write one JSON object in RESEARCH.md: {"verdict":"accepted|changes_requested"}. Optional summary and evidenceRefs explain the decision. ' +
'Plain JSON or a math/json fence is accepted. The host binds your response to the fixed snapshot.\n');
return {
instructions,
scope: 'fixed-statement',
sources: { problemReceiptDigest },
message:
'Cooperative assignment:\n' +
JSON.stringify(action) +
'\n\nRead the fixed formal snapshot in STATEMENT.json.\n' +
(blind
? ''
: '\nIntended mathematics:\n' +
review.intendedStatement +
'\nPremises:\n' +
JSON.stringify(review.premises) +
'\nSaved independent backtranslation:\n' +
JSON.stringify(review.translation)),
};
}
/** M4 builds prompts from the host's scoped cooperation view; hints remain advisory. */
export function buildResearchContext(
task: SolverProblem,
state: Readonly<SolverRunState>,
access: ContextAccess,
): ContextBundle {
if ((state.commonResearchSkill || access.commonResearchSkill) && state.researchDelegation?.mode !== 'team')
return buildCommonResearchContext(task, state, access, readFrozenContexts(task, access));
const skillWorkflow = state.researchMode === 'skill' && state.researchSkillWorkflow === 'full';
if (skillWorkflow) {
if (
!access.researchSkill ||
canonicalMathJson(researchSkillIdentity(access.researchSkill)) !== canonicalMathJson(state.researchSkill)
)
throw new Error('math_research_skill_changed');
assertMathAgentSkillsCurrent([access.researchSkill]);
}
// A native team's approval record uses the historical cooperation shape for storage only.
const nativeTeam = state.researchDelegation?.mode === 'team';
const cooperation = nativeTeam ? undefined : state.cooperation;
const singleSkill =
skillWorkflow && !state.workers && !cooperation && !access.assignment
? buildSkillResearchContext(task, state, {
...access,
executionLimits: undefined,
researchSkill: access.researchSkill!,
})
: undefined;
const command = `bun ${shellArgument(access.tools ?? path.join(access.root, 'bin/math.ts'))}`;
const fixedReview = access.assignment?.kind === 'review';
const planningLead = access.assignment?.kind === 'lead';
const readOnlySources =
fixedReview ||
planningLead ||
(access.assignment?.kind === 'task' &&
(isFeasibilityReview(access.assignment.task) || isFinalCandidateReview(access.assignment.task)));
const planDraft = planningLead ? '.scratch/plan.json' : undefined;
const taskLogPath =
access.assignment?.kind === 'task'
? 'the current worktree-root delivery log named in the assignment message'
: undefined;
if (Boolean(access.continuation) !== Boolean(state.continuationDigest))
throw new Error('math_continuation_identity_missing');
if (access.continuation && access.continuation.digest !== state.continuationDigest)
throw new Error('math_continuation_identity_changed');
const continuation =
access.continuation &&
readContinuationSnapshot(access.continuation.file, access.continuation.digest, {
taskDigest: state.taskDigest,
problemReceiptDigest: task.receiptDigest,
});
if (Boolean(access.knowledgeFile) !== Boolean(access.knowledgeDigest))
throw new Error('math_knowledge_identity_missing');
if (
access.knowledgeSnapshot &&
(!access.knowledgeFile ||
access.knowledgeSnapshot.digest !== access.knowledgeDigest ||
(state.knowledgeDigest && state.knowledgeDigest !== access.knowledgeSnapshot.digest))
)
throw new Error('math_knowledge_identity_changed');
if (access.mathTools && access.environment[MATH_TOOLS_SHA256_ENV] !== access.mathTools.digest)
throw new Error('math_tools_identity_changed');
const toolHelp =
access.assignment && ['lead', 'task'].includes(access.assignment.kind) && !skillWorkflow
? buildResearchToolGuide(task, access, state.environmentDigest).help.build()
: undefined;
const knowledgeRules = access.knowledgeFile
? `${planningLead ? 'Before planning or repeating a search' : 'Before developing a new proof or repeating a search'}, consult the saved theorem knowledge base: ${command} knowledge search --catalog ${shellArgument(access.knowledgeFile)} --sha256 ${shellArgument(access.knowledgeDigest!)} --environment ${shellArgument(state.environmentDigest)} --problem-receipt ${shellArgument(state.problem.receiptDigest)} --query 'MATHEMATICAL_KEYWORDS' --goal 'EXACT_LEAN_GOAL_AND_CONTEXT'. Batch up to eight --query arguments to share one search index. You may omit --query to search only the exact goal; goal shape and version compatibility labels are advisory. ` +
`Inspect a hit with ${command} knowledge get --catalog ${shellArgument(access.knowledgeFile)} --sha256 ${shellArgument(access.knowledgeDigest!)} --id 'THEOREM_ID' --metadata --max-bytes 16000. Deferred fields have complete:false and a read handle; they are not empty or complete interfaces. Rerun the same metadata command with --field 'FIELD_NAME' --offset OFFSET, starting at read.startOffset and then following nextOffset until truncated:false. Concatenate text pages exactly; parse JSON fields only after all pages are joined. Read the complete type and premises before applying a candidate. ` +
`Inspect recorded dependencies with ${command} knowledge dependencies --catalog ${shellArgument(access.knowledgeFile)} --sha256 ${shellArgument(access.knowledgeDigest!)} --id 'THEOREM_ID' --depth 2. Missing coverage is unknown. ` +
`Inspect historical consumers with ${command} knowledge consumers --catalog ${shellArgument(access.knowledgeFile)} --sha256 ${shellArgument(access.knowledgeDigest!)} --id 'THEOREM_ID' --max-bytes 16000; follow nextOffset with --offset to continue. These are same-context local reference closures, not instantiated applications. Read each consumer's complete type, elaboration and source with its handles, then check the proposed application and remaining premises in the current problem. ` +
`Read a dependency or definition in bounded excerpts with ${command} knowledge source --catalog ${shellArgument(access.knowledgeFile)} --sha256 ${shellArgument(access.knowledgeDigest!)} --id 'THEOREM_ID' --file 'ARCHIVED_FILE' --name 'QUALIFIED_DECLARATION' --max-bytes 16000; use --start-line 1 to inspect file context, or --offset NEXT_OFFSET to continue a truncated excerpt without repeating text. Excerpts do not establish complete context. ` +
'Saved route findings are advisory research records with exact scopes, not accepted theorems. Search with --include-findings and inspect one with knowledge finding --catalog CATALOG --sha256 DIGEST --id FINDING_ID; follow its pagination to read the complete record. Recheck its assumptions, counterexample and reopening conditions against the current problem. ' +
(fixedReview
? 'Inspect only knowledge records relevant to the fixed proof, with their full source, context and premises. Do not adapt, import or redeclare them during review. '
: planningLead
? 'Inspect knowledge records relevant to the planned route, with their full source, context and premises. Record the intended application and remaining premises in RESEARCH.md or the task objective; assigned workers perform source adaptation and Lean application checks. Do not adapt, import or redeclare them during planning. '
: 'A hit with installation.status:"installed-frozen" already exists in the current project with matching source bytes: use its provided import and qualified name, never copy or redeclare it. A hit with kind:"definition" is a def body, not a proposition to apply; use an installed definition by name, or read and adapt an archived definition and its dependencies into an owned file, then prove the needed properties. Its knowledge consumers list historical theorems stated about it. ' +
'When you try a saved theorem, optionally add knowledgeUses:[{id:"THEOREM_ID",catalogDigest:"PINNED_DIGEST",declaration:{name:"Qualified.adaptedHelper",file:"Owned/File.lean"}}] to the same JSON research/proof response in RESEARCH.md. The host records this as a reported origin and separately checks actual Lean references; a citation alone is not proof acceptance. ' +
'For a specific application, add application:{expression:"CLOSED_LEAN_TERM",expectedType:"CLOSED_EXPECTED_TYPE",context:{file:"Owned/File.lean",declaration:"ASSIGNED_THEOREM"},arguments:[{parameter:"PARAMETER",value:"ACTUAL_ARGUMENT"}],additionalPremises:[],typeclassIssues:[],adaptationSteps:[],remainingGoals:[],reconsiderWhen:[]} inside that knowledgeUses item. Close local variables with explicit binders and lambdas. The host checks up to four selected expressions in isolated proof-erased snapshots and retains scoped successes and failures. This checks the requested type only: it does not establish that the type closes the original consumer, or accept any erased helper proof. Changed inputs require a fresh check. ' +
`You may inspect one expression beforehand with ${command} check-lean --workspace ${shellArgument(path.join(access.worktree, task.projectRelative))} --file 'Owned/File.lean' --context 'ASSIGNED_THEOREM' --type 'CLOSED_EXPECTED_TYPE' --expression 'CLOSED_LEAN_TERM'. Do not scan all catalog entries with application checks. `) +
'The task context may include bounded saved-theorem candidates already searched by the host for this exact assignment or the original-goal frontier. Inspect these candidates before repeating the same search; the manifest records hints omitted for context budget. An input-not-searched status or zero lexical matches does not establish that no useful theorem exists. ' +
(fixedReview
? 'Historical records and local diagnostics remain advisory; inspect actual fixed-candidate references and current host evidence before deciding.\n'
: planningLead
? 'Historical receipts never accept a proof in the current problem. Inspect complete statement and proof dependencies, respect the frozen assumptions, and record the knowledge theorem id, intended consumer and exact remaining inference in RESEARCH.md.\n'
: 'Inspect the complete type, all premises and statement/proof dependencies before reuse. source-header types require the complete source context and are not closed types. Search is lexical and a missing hit does not establish a missing theorem. For entries without an installed-frozen match, adapt only needed declarations from their historical sources into owned files. Respect the frozen problem and allowed imports, then check the actual application with Lean and return for independent host acceptance. Never copy an entire archived Submission over current goals or inherit historical receipts. Record the knowledge theorem id and the consumer in RESEARCH.md.\n')
: '';
const readOnlyLibraryRules =
readOnlySources && access.libraryCatalog
? `For a library declaration used by the ${fixedReview ? 'fixed candidate' : 'planned route'}, inspect its full pinned interface and context with ${command} library search --worktree ${shellArgument(access.worktree)} --query 'QUALIFIED_NAME' --goal 'EXACT_LEAN_CONTEXT', then ${command} library get --worktree ${shellArgument(access.worktree)} --name 'QUALIFIED_NAME' --max-bytes 16000. Follow source.nextOffset with --offset until source.complete is true. These are read-only inspections; do not import, request or copy modules during ${fixedReview ? 'review' : 'planning'}.\n`
: '';
const taskRules =
access.assignment?.kind === 'task' && access.assignment.task.kind === 'lemma'
? access.assignment.task.declaration === 'definition'
? "Define the declaration named in the current assignment: save one def of exactly the requested type with a complete body, matching the objective's intended meaning. Preserve other helper statements, definition bodies and original goal proofs.\n"
: 'Prove the lemma named in the current assignment. You may repair solver-created lemma proofs and add newly named helpers in owned Lean files. Preserve existing helper statements, definition bodies and every original goal proof exactly as it appears in assignment.seedCommit, including existing sorry placeholders. A preserved placeholder in another declaration does not block delivery of your completed helper.\n'
: access.assignment?.kind === 'task' && access.assignment.task.kind === 'goal'
? 'Prove only the original declaration named in the current assignment. Preserve every other original goal proof exactly as it appears in assignment.seedCommit, including existing sorry placeholders. Other workers and the host handle those goals.\n'
: '';
const cooperative = Boolean(cooperation);
const proofFiles = [...new Set(task.targets.map(target => target.proofPath))];
const proofFileArguments = proofFiles.length
? proofFiles.map(file => `--file ${shellArgument(file)}`).join(' ')
: "--file 'OWNED_FILE.lean'";
const singleSolverDelivery =
(state.workerCount ?? 1) === 1 && !cooperative && !access.assignment
? `When every original target has a complete proof, remove probes and save all proof files and a concise worktree-root RESEARCH.md before the final diagnostic. Run ${command} check-lean --worktree ${shellArgument(access.worktree)} ${proofFileArguments} --timeout-seconds 0. A successful diagnostic covering all current original proof files automatically hands writing back to the host. Reuse a successful diagnostic when its sources and environment are unchanged; do not rebuild or repeat it. Return briefly if the turn is still open. Do not search for delivery protocols or create result JSON or receipts. The host freezes the sources and performs independent Lean acceptance; a local diagnostic is not acceptance. If the host reports a failed check, repair the reported error and submit the corrected files through the same diagnostic. Then return for independent host checking.\n`
: '';
const responsibility = access.assignment
? 'Carry out only your current cooperative assignment. The original problem and task text describe the overall research objective, not permission to prove or edit other assignments. The host integrates checked work and verifies every original target.'
: 'You prove the original mathematical problem in Lean.';
const diagnosticExecutionRules = planningLead
? ''
: `Use this entry point for saved-file checks and temporary #check/#synth probes: ${command} check-lean --worktree ${shellArgument(access.worktree)} --file 'OWNED_FILE.lean' --timeout-seconds 0. ` +
'It prepares local dependency artifacts without writing shared package caches. Direct lake build or lake env lean can attempt writes to read-only packages or miss local module artifacts. Remove temporary probe commands before proof delivery. ';
const instructions = skillWorkflow
? (singleSkill?.instructions ??
buildCommonResearchGuide(task, access, state.environmentDigest, 'workflow').instructions +
'\n' +
responsibility +
'\n' +
`Editable paths: ${JSON.stringify([...(readOnlySources ? [] : task.ownedFiles.filter(file => !Object.hasOwn(task.frozenFiles ?? {}, file))), 'RESEARCH.md', ...(planDraft ? [planDraft] : [])])}. ` +
(taskLogPath ? 'You may append to ' + taskLogPath + '. ' : '') +
(readOnlySources ? 'Leave every Lean source unchanged. ' : '') +
taskRules) +
'\n' +
(['M1', 'M3', 'M4', 'M5'] as const)
.map(slot => strategyAdvice(access, slot, '', slot !== 'M5' || cooperative))
.join('\n')
: `${responsibility} Work only in this isolated worktree.
Edit only ${[...(readOnlySources ? [] : editableOwnedFiles(task.ownedFiles, file => path.join(access.worktree, task.projectRelative, file), task.frozenFiles)), path.join(access.worktree, 'RESEARCH.md'), ...(planDraft ? [path.join(access.worktree, planDraft)] : []), ...(taskLogPath ? [taskLogPath] : [])].join(', ')}.
${access.assignment ? 'Save RESEARCH.md and delivery logs at these exact worktree-root paths' : 'Save RESEARCH.md at its exact worktree-root path'}, including when a diagnostic command runs inside the Lean project subdirectory.
Temporary ${planningLead ? 'mathematical notes and tool output' : 'probes, backups and diagnostic output'} may be written under ${path.join(access.worktree, '.scratch', 'tmp')} or the Lean project's .scratch/tmp directory. The shell's TMPDIR points to the worktree-local directory; run mkdir -p "$TMPDIR" before creating temporary files. /tmp is read-only. Untracked regular scratch files stay advisory and remain outside formal proof checkpoints. Do not git add scratch files or use symlinks there. ${planningLead ? 'Retain useful mathematical findings in RESEARCH.md before returning.' : 'Retain reusable proof code and findings in RESEARCH.md or owned Lean files before returning.'}
Preserve original declaration kinds, names, parameters, types, assumptions, options and namespace/section context. Keep all existing imports. ${fixedReview ? 'Review original definition bodies and statement types without changing them.' : planningLead ? 'Inspect original definition bodies and statement types without changing them.' : 'Original goals may include def declarations whose bodies must be filled; retain all other original definition bodies.'} ${readOnlySources ? 'Leave all Lean source files, imports and environment configuration unchanged.' : 'In a Mathlib project, you may add import Mathlib or any pinned Mathlib.* import in owned files. You may also import installed frozen Submission.Library.* modules, frozen local modules already imported by the original owned files, or other owned modules. Avoid import cycles and do not edit trusted problem or package files.'} Proofs you submit for acceptance must not use sorry, admit, native_decide, unsafe or new axioms. Do not read external candidate solutions.
${longCommandGuidance}
${taskRules}${knowledgeRules}${readOnlyLibraryRules}${
access.libraryCatalog &&
!readOnlySources &&
!(
access.assignment?.kind === 'task' &&
(isFeasibilityReview(access.assignment.task) || isFinalCandidateReview(access.assignment.task))
)
? `Search the complete pinned TheoremOS release before reproving a fact or reporting a library gap: ${command} library search --worktree ${shellArgument(access.worktree)} --query 'MATHEMATICAL_KEYWORDS' --goal 'EXACT_LEAN_GOAL_AND_CONTEXT'. This also finds modules not installed in this problem. Inspect the full interface and module context with ${command} library get --worktree ${shellArgument(access.worktree)} --name 'QUALIFIED_NAME' --max-bytes 16000; follow source.nextOffset with --offset until source.complete is true. A source-header type may depend on section assumptions. For installed-frozen hits, import the given module and test the actual application in Lean. For a needed not-installed hit, submit ${command} library request --worktree ${shellArgument(access.worktree)} --name 'QUALIFIED_NAME' --reason 'EXACT_USE_AND_PREMISES', save your current proof work and RESEARCH.md, and return. You may request up to eight names. The host validates a pinned dependency closure and starts a new frozen continuation of the same original problem; requests neither mutate this run's library nor grant proof acceptance. Do not copy or redeclare library sources.\n`
: ''
}${strategyAdvice(access, 'M1', fixedReview ? 'Inspect the fixed proof and its exact source-bound evidence. Batch independent reads and bound output in bytes.' : planningLead ? 'Inspect the frozen problem, relevant library interfaces and saved findings to develop the mathematical route. Batch independent read-only searches and bound output in bytes.' : 'Use installed library lemmas and test uncertain bridges early. Batch independent searches and reads, reuse results, bound output in bytes and show the earliest Lean error and goal.')} ${cooperative ? 'The host launches the cooperative agents; do not spawn agents inside this session.' : 'Do not spawn agents inside this session.'} ${strategyAdvice(access, 'M5', 'Lead planning saves a task graph and returns promptly; assigned workers investigate and prove it.', cooperative)}
${planningLead ? strategyAdvice(access, 'M3', 'Use the current mathematical route and saved peer evidence to choose the next planning decision.') : `For diagnostics, run ${command} check-lean --workspace ${shellArgument(path.join(access.worktree, task.projectRelative))} --file 'OWNED_FILE.lean' --timeout-seconds 0. Its --file path is relative to --workspace: use Submission.lean or Submission/Theory/Topic.lean without repeating the Lean project prefix. It writes JSON to stdout and progress to stderr; capture them separately (for example, > "$TMPDIR/check.json" 2> "$TMPDIR/check.log") and parse only stdout. It compiles saved sources with pinned imports and bounded output. ${diagnosticExecutionRules}Read firstBlocking and its exact position; follow diagnosticArtifacts for captured messages, local hypotheses and goals when previews are incomplete. Source/environment digests identify the checked inputs; inspect captureComplete before treating captured output as complete. The diagnostic sourceDigest hashes project configuration and transitive source files; it is not the file SHA256 or the host owned-source digest. Its environmentDigest also uses the diagnostic tool's identity scheme; different digest schemes need not match. Compare file bytes with the sourceFiles entries in .lake/lean-diagnostics-latest.json or diagnosticArtifacts, and compare digests only within the same scheme. phases[].cached:true means the tool matched source, configuration, environment and compiled artifact digests; it reuses a saved diagnostic, not a new compilation or proof acceptance. A new source change or a concrete failure needs a new check; an unchanged successful check does not need a copy or rebuild just to clear cached:true. To preflight an uncertain closed task type, add --type 'CLOSED_LEAN_TYPE' --context 'EXISTING_QUALIFIED_DECLARATION' with exactly one --file. This elaborates a type in a disposable proof-erased snapshot using that declaration's scopes; it does not check a proof or make future helper names available. Keep unavailable future dependencies explicit and revise an invalid interface before sending workers to prove it. Poll a returned process ID. A timeout is inconclusive. ${strategyAdvice(access, 'M3', fixedReview ? 'Report a failed check against the fixed candidate without changing its sources; resolve the discrepancy or request changes.' : 'Repair the earliest error; avoid rebuilding dependencies or repeating unchanged checks.')} ${fixedReview ? 'If the environment is unavailable, report the exact limitation in your verdict and return.' : 'For math_original_context_changed or math_original_declaration_changed, use the reported file and recovery instructions to restore the offending context or declaration from the seed commit while preserving new proof work, then retry the saved-file check. Keep helper-specific scopes and instances inside their proofs. If the environment is unavailable or the same startup error remains after a repair, report it and return.'}`}
${planningLead ? '' : `For a finite Boolean inference, use ${command} check-finite --json 'JSON' --witnesses 16. Supply {"schema":"theorem-os.finite-boolean-cnf.v1","variables":["A"],"fixed":[],"clauses":[[{"variable":"A","value":true}]]}. Each clause is a disjunction of matching literals and all clauses must hold. Preserve the exact input data and returned digests, satisfyingCount and complete witnesses in your research evidence. A satisfying assignment can refute only a claimed contradiction of those constraints; it does not refute their geometric construction or the original theorem. Size-unsupported results are inconclusive. Enumeration and its translation remain advisory and must be formalized for proof acceptance.\n`}
${declarationIndexRules(access.worktree, command)}${readOnlySources && !planningLead ? 'Inspect relevant declarations and their original assumptions without editing them.' : planningLead ? `Inspect relevant declarations and their original assumptions without editing them. ${strategyAdvice(access, 'M4', 'Keep RESEARCH.md as a concise mathematical route checkpoint with exact open questions and intended consumers.')}` : `${kernelCertificateRules(task, command, path.join(access.worktree, task.projectRelative))}${theoryModuleRules(task)}${libraryModuleRules(task)}Add helpers in owned modules; definitions may be noncomputable. For type lookup, add single-name #check commands such as #check Finset.sum_eq_single in the existing namespace. For instance lookup, use #synth MeasureSpace ℝ or parenthesize complex types, such as #synth Fintype (Fin 2). If a known library declaration is unavailable, find its module in the installed corpus and import that explicit Mathlib.* module in the owned file; an unknown name alone does not establish a missing library theorem. Check the actual saved file, remove probes before delivery, and save checked helpers, gaps and next steps in RESEARCH.md. ${strategyAdvice(access, 'M4', 'Keep this file as a concise current checkpoint; replace obsolete status instead of appending transcripts. Earlier committed notes remain in git history.')} ${singleSolverDelivery || 'Then return for independent host checking. '}Turns may be checkpointed and resumed. Verify computational results in Lean.`}
${access.corpusFile ? `For library lookup, run ${command} search-lean --workspace ${shellArgument(access.retrievalRoot)} --corpus ${shellArgument(access.corpusFile)} --sha256 ${shellArgument(access.environment.DEEPSCIENTIST_MATH_RETRIEVAL_CORPUS_SHA256!)} --query 'THEOREM_NAME_OR_DESCRIPTION'. Batch up to 8 queries. Pass --goal 'LEAN_GOAL_AND_CONTEXT' when you know the exact Lean target. Try a Lean name, a common theorem name, or words from its statement. Ranking is advisory. ${planningLead ? 'Read complete source context and saved elaboration evidence before expanding a search; delegate an uncertain Lean application check to an assigned worker.' : 'Confirm types and known names with Lean #check before expanding a search: generated and core declarations may have no Mathlib source header.'}\n` : ''}${access.mathTools ? mathToolsInstruction(access.mathTools, access.worktree) : ''}${mathematicsRuntimeInstruction(access.environment.DEEPSCIENTIST_MATH_ENV_FILE)}`;
const originalProblem = `${access.taskText}\n\nOriginal problem:\n${task.contract.formalStatement}`;
let message =
access.sourceFeedback ||
(state.check && !state.check.passed
? solverProofFeedback(state.check, access.previousCheck, access.previousFailures ?? state.failures)
: originalProblem);
let checkpoint: string | undefined;
if (!state.threadId && state.previousThreadIds?.length) {
const notes = path.join(access.worktree, 'RESEARCH.md');
if (fs.existsSync(notes)) {
assertMathCanonicalPath(notes);
// A checkpoint is optional. Its full text is either selected or explicitly omitted by the compiler.
checkpoint = readMathText(notes, 1_000_000);
}
}
const bundle: ContextBundle = {
instructions,
...(toolHelp ? { toolHelp } : {}),
message,
scope: 'original-problem-and-allowed-library',
sources: { problemReceiptDigest: task.receiptDigest },
};
const residualSnapshot = readLeanResidualGoalSnapshot({
projectRoot: path.join(access.worktree, task.projectRelative),
env: access.environment,
consumers: knowledgeContextConsumers(task, cooperation, access.assignment, state.environmentDigest),
});
const residualGoals = residualSnapshot.goals;
const residualSections: ContextSection[] = residualGoals.map(goal => ({
id: `residual-goal:${mathDigest(canonicalMathJson(goal))}`,
kind: 'feedback',
priority: 6,
text:
'Current residual goal from complete, source-matched Lean diagnostics (advisory; not proof acceptance):\n' +
JSON.stringify({ ...goal, goalText: undefined }) +
'\n' +
goal.goalText,
}));
for (const record of Object.values(cooperation?.proofSearchRecords ?? {}))
residualSections.push({
id: `proof-search:${record.taskId}:${record.checkpoint.digest}`,
kind: 'feedback',
priority: 6,
text:
'Host-retained advisory proof-search checkpoint for this scope. Read the worker-local copy in COOPERATION.json.proofSearchRecords before continuing:\n' +
JSON.stringify({
taskId: record.taskId,
taskDigest: record.taskDigest,
basis: record.basisDigest,
generation: record.generation,
checkpointDigest: record.checkpoint.digest,
workerReportedToolUsage: record.toolUsage,
proofAcceptance: record.proofAcceptance,
}),
});
if (residualSnapshot.omissions.length)
residualSections.push({
id: 'residual-goals:reader-omissions',
kind: 'feedback',
priority: 6,
text:
'Additional source-matched residual diagnostics were omitted by the bounded reader. Inspect the current diagnostic artifacts before treating these goals as fully covered:\n' +
JSON.stringify(residualSnapshot.omissions),
});
const knowledgeSections = access.knowledgeSnapshot
? knowledgeContextSections({
problem: task,
cooperation,
action: access.assignment,
knowledge: access.knowledgeSnapshot,
environmentDigest: state.environmentDigest,
residualGoals,
installedLibrary: access.installedLibrary,
residualOmissions: residualSnapshot.omissions,
})
: [];
const librarySections = access.installedLibrary
? installedLibraryContextSections({
problem: task,
cooperation,
action: access.assignment,
library: access.installedLibrary,
taskText: access.taskText,
environmentDigest: state.environmentDigest,
residualGoals,
residualOmissions: residualSnapshot.omissions,
})
: [];
const allowKnowledgeApplications =
!access.assignment ||
access.assignment.kind === 'lead' ||
(access.assignment.kind === 'task' && !isFinalCandidateReview(access.assignment.task));
const applicationSections =
access.knowledgeSnapshot && allowKnowledgeApplications
? knowledgeApplicationContextSections({
uses: cooperation
? (cooperation.deliveries?.flatMap(delivery => delivery.knowledgeUses ?? []) ?? [])
: (state.knowledgeUses ?? []),
projectRoot: path.join(access.worktree, task.projectRelative),
env: access.environment,
environmentDigest: state.environmentDigest,
catalogDigest: access.knowledgeSnapshot.digest,
tasks:
cooperation?.tasks.map(node => node.task) ??
task.targets.map((target, index) => ({
id: `original_goal_${index + 1}`,
kind: 'goal' as const,
revision: 0,
objective: `Prove ${target.theoremName}`,
dependsOn: [],
...target,
})),
})
: [];
const query = continuation && referenceQuery(task, access.taskText, access.assignment);
const reference = continuation && query ? continuationReferenceSection(continuation, command, query) : undefined;
const compilation = {
problem: task,
cooperation,
action: access.assignment,
taskText: access.taskText,
feedback: message.startsWith(originalProblem) ? message.slice(originalProblem.length).trim() || undefined : message,
checkpoint,
frozenContexts: readFrozenContexts(task, access),
maxContextBytes: access.maxContextBytes,
maxOptionalContextBytes: access.maxOptionalContextBytes,
environmentDigest: state.environmentDigest,
mandatory: [
...(continuation ? [continuationContextSection(continuation, command, task.projectRelative)] : []),
...(singleSkill && state.researchDelegation?.mode === 'team' ? [agentTeamContextSection(state)] : []),
...(skillWorkflow
? [
{
id: 'research-skill-identity',
kind: 'assignment' as const,
text: 'Frozen research skill:\n' + canonicalMathJson(state.researchSkill),
},
]
: []),
...(singleSkill && fs.existsSync(path.join(access.worktree, '.scratch/declaration-feedback.json'))
? [
{
id: 'declaration-feedback',
kind: 'feedback' as const,
text:
'Read current host declaration feedback at ' +
path.join(access.worktree, '.scratch/declaration-feedback.json') +
' before resubmitting.',
},
]
: []),
],
optional: [
...residualSections,
...librarySections,
...knowledgeSections,
...applicationSections,
...(reference ? [reference] : []),
],
};
const compileFull = (checkpointInstruction?: string) =>
compileTaskContext({
...compilation,
instructions,
mandatory: [
...dispatchPrefix(checkpointInstruction),
...(access.executionLimits ? [executionLimitsSection(access.executionLimits)] : []),
...(compilation.mandatory ?? []),
],
});
const compileSingleContinuation = (checkpointInstruction?: string) =>
compileContextSections({
instructions,
mandatory: [
...dispatchPrefix(checkpointInstruction),
...(access.executionLimits ? [executionLimitsSection(access.executionLimits)] : []),
{ id: 'same-thread-skill-workflow', kind: 'continuation', text: singleSkill!.continuationMessage! },
],
optional: compilation.optional,
maxContextBytes: access.maxContextBytes,
maxOptionalContextBytes: access.maxOptionalContextBytes,
inlineManifest: false,
});
const continued = singleSkill ? compileSingleContinuation() : undefined;
const compiled =
access.assignment && cooperation
? buildCooperativeContext(
task,
cooperation,
bundle,
access.assignment,
compilation,
state.workers,
skillWorkflow ? access.researchSkill : undefined,
)
: {
...bundle,
...compileFull(),
...(continued ? { continuationMessage: continued.message, continuationManifest: continued.manifest } : {}),
compileDispatch: (options: ResearchDispatchOptions) =>
options.continuation && continued
? compileSingleContinuation(options.checkpointInstruction)
: compileFull(options.checkpointInstruction),
};
const result: ContextBundle = {
...compiled,
...(access.knowledgeSnapshot
? {
knowledgeRetrievals: knowledgeSections.map(section => ({
...section.knowledge,
included: compiled.manifest?.included.some(item => item.id === section.id) ?? false,
omissionReason: compiled.manifest?.omitted.find(item => item.id === section.id)?.reason,
})),
}
: {}),
};
const recompile = result.compileDispatch,
recompileCompacted = result.compileCompactedDispatch;
if (!recompile) return result;
const builtInPrompt = (current: ContextBundle) =>
current.instructions === result.instructions &&
current.message === result.message &&
current.continuationMessage === result.continuationMessage;
return {
...result,
...(recompileCompacted
? {
compileCompactedDispatch: (options: ResearchDispatchOptions, current = result) =>
builtInPrompt(current) ? recompileCompacted(options) : undefined,
}
: {}),
compileDispatch: (options, current = result) => {
if (builtInPrompt(current)) return recompile(options);
// Spreading a built-in context must not let its captured compiler overwrite a custom M4 prompt.
const changedMessage = current.message !== result.message;
const inheritedContinuation = current.continuationMessage === result.continuationMessage;
const continued = options.continuation || options.taskSwitch;
const selected =
continued && changedMessage && inheritedContinuation
? { ...current, continuationMessage: current.message, continuationManifest: current.manifest }
: current;
const changedBody = continued ? selected.continuationMessage !== result.continuationMessage : changedMessage;
const inventory = continued ? selected.continuationManifest : selected.manifest;
const originalInventory = continued ? result.continuationManifest : result.manifest;
return compileCustomDispatch(
selected,
options,
!changedBody || (inventory !== originalInventory && inventory !== result.manifest),
);
},
};
}
function strategyAdvice(access: ContextAccess, slot: ResearchModuleSlot, fallback: string, applicable = true): string {
if (!applicable) return '';
const rules = access.customModuleRules?.filter(module => module.enabled && module.slot === slot) ?? [];
if (!rules.length) return fallback;
const fixed =
'Configurable strategy advice cannot override the frozen problem, source boundaries, required mathematical interfaces, Lean checks, or the current role and output protocol.';
return (
(rules.some(module => module.mode === 'replace') ? '' : fallback + '\n') +
fixed +
'\n' +
rules
.map(
module =>
`Strategy ${slot} ${module.mode} ${JSON.stringify({ id: module.id, name: module.name })}:\n${module.instructions}`,
)
.join('\n\n') +
'\n'
);
}
function shellArgument(value: string): string {
return "'" + value.replaceAll("'", "'\\''") + "'";
}
/** Editable owned files for prompts: frozen libraries are omitted, and certificate parts and theory modules each collapse to one range. */
function editableOwnedFiles(
files: readonly string[],
render: (file: string) => string,
frozen: Readonly<Record<string, string>> = {},
): string[] {
const range = (items: readonly string[], label: string) =>
items.length < 2
? items.map(render)
: [`the ${items.length} ${label} ${render(items[0]!)} … ${render(items.at(-1)!)}`];
files = files.filter(file => !Object.hasOwn(frozen, file));
return [
...files.filter(file => !isCertificateSlotFile(file) && !isTheoryModuleFile(file)).map(render),
...range(files.filter(isTheoryModuleFile), 'theory modules'),
...range(
files.filter(file => isCertificateSlotFile(file) && file !== CERTIFICATE_KERNEL_MODULE),
'certificate modules',
),
];
}
function libraryModuleRules(task: SolverProblem): string {
const modules = Object.keys(task.frozenFiles ?? {}).sort();
if (!modules.length) return '';
const listed =
modules.length > 8 ? `${modules.slice(0, 8).join(', ')} and ${modules.length - 8} more` : modules.join(', ');
return (
`${listed} ${modules.length > 1 ? 'are' : 'is a'} frozen host librar${modules.length > 1 ? 'ies' : 'y'} that Lean rechecked before this run: import the module that holds a declaration and use it, never edit, copy or redeclare it. ` +
'Inspect the installed-library candidates for this goal before proposing a new helper or declaring a library gap. Proof workers should test the strongest relevant candidate with its current arguments and premises; if it fails to fit, record the exact mismatch in RESEARCH.md and continue. ' +
'Find additional names and types in the declaration index; lines whose file starts with Submission/Library/ are library declarations.\n'
);
}
function theoryModuleRules(task: SolverProblem): string {
const modules = task.ownedFiles.filter(isTheoryModuleFile);
if (!modules.length) return '';
return (
`${modules[0]}${modules.length > 1 ? ` … ${modules.at(-1)}` : ''} are owned theory-library modules; each starts as an empty namespace ${THEORY_MODULE_NAMESPACE} block. ` +
'Give each mathematical topic its own module: its definitions, their basic API and sanity lemmas, then the theorems built on them. Small modules compile and are cached separately, and later runs reuse them. ' +
`Write declarations between the existing namespace and end lines, so a theorem named Foo.bar there is ${THEORY_MODULE_NAMESPACE}.Foo.bar; never add top-level open, variable, section or nested namespace lines, and keep open … in or set_option … in inside proofs. ` +
'Keep existing imports. In a Mathlib project, add import Mathlib or any pinned Mathlib.* import as needed. Import earlier theory modules or the frozen modules already imported by original files. Import a theory module from a consumer without creating a cycle. Record which module holds which topic in RESEARCH.md.\n'
);
}
function kernelCertificateRules(task: SolverProblem, command: string, project: string): string {
if (!task.ownedFiles.includes(CERTIFICATE_KERNEL_MODULE)) return '';
return (
`For a finite integer computation, generate a kernel-checked certificate with ${command} kernel-certificate --json 'SPEC' --workspace ${shellArgument(project)}; run it with --help first for the box and witness spec formats and limits, and omit --workspace for a dry-run plan. ` +
`Set "kernelModule":${JSON.stringify(CERTIFICATE_KERNEL_MODULE)} and list unused Submission/Certificates/PartNN.lean files in "modules"; one certificate occupies one or more parts and each part holds at most one certificate. ` +
`${CERTIFICATE_KERNEL_MODULE} is a frozen host library: import it, never edit it. The first part holds shared definitions, middle parts hold chunk theorems and the last proves NAME.holds by decide +kernel without native_decide. ` +
'Each local module is compiled in isolation and unchanged modules are reused from a cache, so split large computations over more parts instead of enlarging one. Check the first chunk module with check-lean before generating the full range. ' +
'Import the last part from a non-certificate owned file and bridge NAME.check to the needed proposition with KernelCompute.intLe_eq_true, intLt_eq_true, intEq_eq_true and Bool.and_eq_true. ' +
'Record the spec, returned inputDigest and covered ranges in RESEARCH.md. Generated text is unchecked until Lean accepts every module.\n'
);
}
function declarationIndexRules(worktree: string, command: string): string {
const index = shellArgument(path.join(worktree, DECLARATION_INDEX_FILE));
return (
`To find an owned helper, run ${command} declarations search --index ${index} --query 'KEYWORDS' --limit 12 --max-bytes 4096 before scanning Lean files. ` +
`Use --file 'Submission.lean' to restrict the exact project-relative file, and ${command} declarations get --index ${index} --name 'QUALIFIED_NAME' --max-bytes 8192 for its indexed type; then read source at the returned file:line if truncated. ` +
'Search returns short type previews and omittedMatches, so refine keywords or file instead of dumping the raw index. The host rebuilds it from saved owned sources each turn. ' +
"It is lexical, not Lean-checked, omits structures, inductives and this turn's edits, and reports indexOmissions for skipped declarations or unreadable files. Leave it unchanged.\n"
);
}
const frozenContextCache = new Map<string, Readonly<Record<string, string>>>();
function readFrozenContexts(problem: SolverProblem, access: ContextAccess): Readonly<Record<string, string>> {
if (!problem.workspace && access.frozenContexts) return access.frozenContexts;
const key = JSON.stringify([problem.receiptDigest, problem.baseCommit, access.worktree, problem.workspace]);
const cached = frozenContextCache.get(key);
if (cached) return cached;
const contexts = Object.fromEntries(
problem.ownedFiles
.filter(file => file.endsWith('.lean') && !Object.hasOwn(problem.frozenFiles ?? {}, file))
.map(file => {
if (path.posix.basename(file) === 'Solution.lean')
throw new TaskContextError('math_context_missing_required', `forbidden-source:${file}`);
let source: string | undefined;
for (const repository of [...new Set([access.worktree, problem.workspace].filter(Boolean))]) {
try {
source = committedSource(repository, problem.baseCommit, path.posix.join(problem.projectRelative, file));
break;
} catch {
/* A newly prepared fixture may expose the frozen object only in its source repository. */
}
}
if (source === undefined) throw new TaskContextError('math_context_missing_required', `frozen-source:${file}`);
return [file, frozenSourceContext(problem, file, source)];
}),
);
frozenContextCache.set(key, contexts);
if (frozenContextCache.size > 16) frozenContextCache.delete(frozenContextCache.keys().next().value!);
return contexts;
}
// The Lead's one-line-per-task view, so a revision decision does not start by parsing COOPERATION.json.
// One observed Lead spent 26 of its 80 commands parsing COOPERATION.json with ad hoc scripts.
const LEAD_TASK_TABLE_ROWS = 60;
function leadTaskTable(cooperation: CooperationState): string {
const nodes = cooperation.tasks.filter(node => !node.capacityExploration);
if (!nodes.length) return '';
const done = (status: string) => ['integrated', 'completed'].includes(status);
const statusById = new Map(nodes.map(node => [node.task.id, node.status]));
const open = nodes.filter(node => !done(node.status)),
checked = nodes.filter(node => done(node.status));
const rows = [...open, ...checked].slice(0, LEAD_TASK_TABLE_ROWS).map(node => {
if (isFinalCandidateReview(node.task))
return [node.task.id, 'final-review', 'status=' + (node.task.status ?? 'active')].join(' ');
const unmet = node.task.dependsOn.filter(id => !done(statusById.get(id) ?? ''));
return [
node.task.id,
node.task.kind,
'status=' + (node.task.status ?? 'active'),
'executionStatus=' + node.status,
'owner=' + (node.assignment?.workerId ?? node.task.ownerId ?? '-'),
'attempts=' + node.attempts,
'waitsOn=' + (unmet.length ? unmet.join(',') : '-'),
].join(' ');
});
const omitted = nodes.length - rows.length;
return (
'Current tasks (id kind status executionStatus owner attempts unmet dependencies; status is plan policy; executionStatus is host scheduling, not proof acceptance):\n' +
rows.join('\n') +
(omitted ? `\n${omitted} more tasks (unfinished tasks are listed first) are in COOPERATION.json.` : '')
);
}
/** Add role rules; the compiler selects a scoped, versioned mathematical context for dispatch. */
function buildCooperativeContext(
problem: SolverProblem,
cooperation: CooperationState,
context: ContextBundle,
action: CooperativeAction,
compilation: Omit<TaskContextInput, 'instructions'>,
workers: readonly Pick<ResearchWorker, 'id' | 'role'>[] = [],
researchSkill?: MathAgentSkill,
): ContextBundle {
const skillWorkflow = Boolean(researchSkill);
const workContract = cooperativeWorkContract(action, cooperation.tasks, cooperation);
const capacityExploration =
action.kind === 'task'
? cooperation.tasks.find(node => node.task.id === action.task.id)?.capacityExploration
: undefined;
const organizationRules =
action.kind === 'lead' && workers.some(worker => worker.role === 'prover')
? 'Available worker responsibilities: ' +
JSON.stringify(workers.map(({ id, role }) => ({ id, role }))) +
'. ' +
'Provers claim only typed lemma tasks in your plan. Give each useful independent proof obligation a full closed Lean type, owned proofPath, stable theoremName and exact dependencies; use ownerId only when a reservation is needed. ' +
'Researchers prioritize mathematical investigation and precise lemma interfaces; the Engineer prioritizes original goals and consumes checked helpers. Researchers and the Engineer can also take eligible research or lemma tasks. ' +
'Keep proof work executable as findings arrive, and let Provers wait when no useful independent typed obligation is known. ' +
'The host may use spare slots for distinct advisory capacity explorations. These execution overlays appear in COOPERATION.json; omit them from your math-plan because the host preserves their ownership and drafts separately. Never depend on an exploration ID as a proof premise; adopt its exact interface under a new typed task ID using the actual typed inputs. Consume useful findings through the usual plan. ' +
`An unspecified route maxConcurrent uses ${defaultResearchRouteConcurrency(workers)} task slots; an explicit limit is respected.\n`
: '';
const feasibilityRules =
!skillWorkflow && action.kind === 'task' && isFeasibilityReview(action.task)
? 'This is an early mathematical feasibility review. Independently challenge the exact dependency interfaces and their proposed original-goal application before their proofs are complete. Identify the hardest missing construction or estimate and whether its difficulty has merely moved into a helper or assumption. Check quantifier order, uniform constants, quantitative strength, boundary cases and applicability of cited results. First test whether the proposed inputs would actually suffice for the consumer; then test the most doubtful input with a counterexample, tractable special case, weaker statement or alternative construction. Choose a discriminating check that can change the route decision, rather than repeating an unchanged search. Keep a complete useful argument, exact remaining gap and reproducible evidence in RESEARCH.md. Use the existing research response with conclusion supporting, refuted or inconclusive; distinguish an invalid route from a false original theorem. Supporting evidence and local Lean probes remain advisory. Leave Lean sources unchanged on delivery.\n'
: '';
const earlyRouteRules =
!skillWorkflow && action.kind === 'lead'
? 'Before expanding an uncertain decomposition, identify its hardest substantive inference and test that the proposed inputs suffice for their actual consumer. For a typed consumer with unproved lemma inputs, arrange an early conditional:true proof using those fixed inputs; for proofRoutes, do this on the route closing lemma while the original target still waits for a checked route. A conditional receipt checks sufficiency only, not the truth or feasibility of its premises. Request a distinct untyped research/sketch task with reviewScope:"feasibility" to challenge a doubtful core input, with dependsOn naming the consumer and core tasks it must inspect. Advisory dependencies are inspection inputs and do not wait for proofs. Keep the consumer independent of the advisory review in its proof dependencies to avoid a cycle. Give the review a specific mathematical question and discriminating check; an ordinary direct proof does not require a review task. A fresh reviewer session and author exclusion support an independent check; if no independent peer is eligible, keep useful proof work executable and state the missing review.\n'
: '';
const connectionRules =
action.kind === 'task' && action.assignment.connectionCheck
? 'This is a host-dispatched consumer connection check. First prove the assigned consumer from the exact typed inputs fixed in assignment.connectionCheck; their sufficiency is the current mathematical question. You may reference those dependency lemma names without proving them or adding stubs, axioms or extra assumptions. The host supplies only those fixed inputs as assumptions in a disposable projection. Full-file diagnostics may still report absent unproved input declarations; preserve that distinction and return the consumer proof for the host conditional check. A conditional success does not prove any input or accept the original target. Independent useful proof work continues while inputs are proved.\n'
: '';
const familyRules =
action.kind === 'lead'
? `A finite case table may be one family lemma task: add family:{"lo":0,"hi":N,"chunk":K,"predicate":"Namespace.P"} to a lemma task, where P : Nat → Prop is a declaration (list a definition task for it in dependsOn). The host sets its type to ∀ i : Nat, lo ≤ i → i < hi → P i, derives member lemma tasks ID_c0, ID_c1, … for consecutive ranges of K indices (at most ${MAX_FAMILY_MEMBERS}), and proves the family itself by a case split over the members. The family is conditional:true by default, so its check binds unproved members as explicit hypotheses and it integrates unconditionally once every member does; consumers depend on the family task. Members inherit the family's other dependsOn, proofPath, group and status; edit the family task, never a member, and do not use ids of the form ID_cN for other tasks. Order the table so each chunk shares one proof method.\n`
: '';
const familyMemberRules =
action.kind === 'task' && action.task.familyMember
? 'This lemma is a family member. Prove only its own index range; the host proves the family from its members, so leave the family theorem and sibling members alone.\n'
: '';
const initialPlanning = action.kind === 'lead' && cooperation.tasks.length === 0;
const initialPlanningRules =
initialPlanning && !skillWorkflow
? 'Initial planning shares the proof budget. After one focused inspection of the exact targets and definitions, publish the smallest useful executable task graph before further literature searches or proof exploration. ' +
'The first graph must cover every original target exactly once; it does not need a complete proof decomposition. Direct original-goal tasks plus distinct research tasks for specific uncertain inferences are a valid first plan and let peers work in parallel. ' +
'Add a helper only when its full closed Lean type and intended consumer are known; never invent a proposition to fill the graph. Keep original goals independent of advisory research in proof dependencies. ' +
'Save the current route and exact uncertainties in RESEARCH.md, publish the complete plan with plan-apply --publish, and return. Later Lead decisions use the accepted graph and new peer evidence to add typed steps or revise a route.\n'
: '';
const leadDeliveryRules =
action.kind === 'lead'
? 'Once a complete plan has useful executable work, save the plan, or a revision of the accepted graph, and return promptly so peers can start it. Delegate unresolved library or mathematical searches as research tasks to eligible Researchers or the Engineer. For new, unstarted helpers that need no worker-specific context, omit ownerId so eligible peers can claim them. Keep explicit reservations when needed; removing ownerId never releases a saved assignment. Before adding work, inspect ready tasks reserved by ownerId to a busy worker while an eligible generalist is idle. Explicitly reassign an unstarted, unassigned task through the plan when useful, preserving its mathematical contract. Preserve route gates, group scope, independent reviewer eligibility and running assignments; an idle Engineer can do research while its original goal waits on a genuine route prerequisite. Apply this on every decision, including later replanning. Include every original target and preserve checked task contracts and active independent task definitions; publish the plan or revision once with plan-apply --publish; keep concise mathematical route notes in RESEARCH.md. For intake on return, omit --publish. After a successful publication, a second copy of the graph in RESEARCH.md or the final response is unnecessary. The host owns mathematician plan approval; return after publication without polling or waiting for approval. Add only mathematically useful executable tasks. ' +
'A goal/lemma objective may guide the argument, but cannot replace its frozen proof deliverable with a read-only audit or diagnostic report. Create a separate untyped research/sketch task for those checks. ' +
"consumerGoalIds is allowed only on untyped research/sketch tasks; connect a typed lemma through its actual consumer's dependsOn or proofRoutes. " +
'Plan task.status may be active, paused or stopped; omission means active. Context task interfaces preserve this policy in status and show host scheduling separately in executionStatus. Never copy executionStatus values into a plan task.status; the host determines scheduling and proof acceptance. ' +
'Use the generated tool help for command syntax.\n'
: '';
// A revision states only changed tasks; the host expands it against the accepted graph and validates the result.
const planRevisionRules =
action.kind === 'lead' && cooperation.tasks.length
? 'Revise the accepted graph instead of retyping it. Read the current task graph in COOPERATION.json or the host-synced accepted graph in the math-plan block in RESEARCH.md. For large notes, use COOPERATION.json and plan-apply --publish. ' +
`Use this revision schema in the draft passed to plan-apply: {"baseVersion":${JSON.stringify(action.planVersion ?? '')},"upsert":[{"id":"existing_task","status":"paused"},{"id":"new_helper","kind":"lemma","objective":"...","theoremName":"...","type":"...","dependsOn":[]}],"remove":["obsolete_task"]}. ` +
'Include the decisions requested for this dispatch in the same object. An upsert of an existing id replaces only the fields it supplies (null deletes a field; a changed kind replaces the whole task); a new id needs a complete task definition. ' +
'Unlisted tasks, groups and proofRoutes stay unchanged; supply groups or proofRoutes only to replace that whole list. ' +
'The expanded graph is validated like a complete plan, and the host rewrites the fence with the full accepted graph after acceptance. A revision against an older baseVersion is rejected; rewrite it against the current accepted graph. ' +
'Adopt a proposal by upserting its exact typed task. A complete tasks list remains valid when most of the graph changes.\n' +
(cooperation.tasks.some(node => node.task.family)
? 'Family member tasks are derived from their family task and are not in the synced graph; revise the family task (its family range, chunk, predicate or status) instead.\n'
: '')
: '';
// Frozen library modules are imported, never edited, so plans and source-bound findings omit them.
const leanFiles = problem.ownedFiles.filter(
file => file.endsWith('.lean') && !Object.hasOwn(problem.frozenFiles ?? {}, file),
);
const proofFiles = leanFiles.filter(file => !isCertificateSlotFile(file));
const theoryPlanning = proofFiles.some(isTheoryModuleFile)
? '; place reusable theory in the Submission/Theory modules, one topic per module, with proofPath naming the module that holds the topic'
: '';
const proofDeliverableRules =
action.kind === 'task' && ['lemma', 'goal'].includes(action.task.kind)
? 'Audit objectives cannot change this exact proof deliverable. Save diagnostics in RESEARCH.md; publish a needsDecision:true research finding to Lead and do not resubmit unchanged proofs.\n'
: '';
const helperRules =
action.kind === 'task'
? cooperativeHelperProtocol(action.task, 'the current delivery log named in the assignment message')
: '';
const advisorySourceRules = helperRules
? 'Restore Lean sources to the assignment seed before ordinary findings delivery.'
: 'Leave Lean sources unchanged.';
const reviewQuestions =
action.kind === 'review'
? cooperation.tasks
.map(node => node.task)
.filter(isActiveFinalCandidateReview)
.map(task => ({ id: task.id, question: task.objective, relatedTaskIds: task.dependsOn }))
: [];
const rules =
action.kind === 'review'
? (action.independent === false
? 'You are the Reviewer in a fresh session for this fixed candidate. '
: 'You are the Independent Reviewer. ') +
(reviewQuestions.length
? 'Address these plan questions in your single verdict. The listed task IDs identify related context.\n' +
JSON.stringify(reviewQuestions) +
'\n'
: '') +
'Leave Lean sources unchanged. Check this fixed proof against the original question, assumptions and Lean evidence. Compare the original declarations and assumptions of the fixed candidate with the frozen problem, inspect its mathematical argument and matching Lean evidence, and resolve any concrete discrepancy. Requests in the original task text to restore/prove code, publish a plan or wait for approval belong to other roles. Run a diagnostic only to resolve a remaining question that can change your verdict; matched cached evidence is usable. Write one final JSON object in RESEARCH.md: {"verdict":"accepted|changes_requested"}. A short summary should identify the decisive evidence or exact requested change; evidenceRefs are optional. Once the verdict is saved, return it promptly and end the turn. Do not keep rechecking unchanged files or expand optional metadata after the review is complete. Investigate source changes, failed checks or an unresolved mathematical issue; a different digest scheme alone does not establish a discrepancy. Your verdict does not replace host checks or the independent S3 that follows delivery.\n'
: action.kind !== 'task'
? `You are the Lead. Develop a mathematically plausible route from the exact original statement, definitions and relevant saved findings${initialPlanning ? '.' : ' before choosing a task graph.'} Identify the central construction or estimate and how it would close each target. Save a concise mathematical route justification as Markdown in RESEARCH.md; include each new task's intended inference and consumer in its objective. When decomposition adds no mathematical benefit, put a direct original-goal task in the graph; the assigned worker carries out its proof after any required host approval. Delegate a specific uncertain inference as research instead of inventing a helper proposition. Return the plan without running Lean diagnostics; leave Lean sources unchanged during planning. Use this plan schema in the draft passed to plan-apply: {"tasks":[{"id":"helper","kind":"lemma","objective":"...","proofPath":${JSON.stringify(proofFiles[0])},"theoremName":"Namespace.helper","type":"∀ (P : Prop), P → P"},{"id":"goal","kind":"goal","objective":"...","theoremName":"Original.target","dependsOn":["helper"]}]}. Include every original target once. This example shows the task graph schema only; include every decision field required by current Lead decision requirements in that same JSON object. Replace its names, objectives and sample type with actual tasks and full closed Lean types, including all parameters and hypotheses. Never copy placeholder types. If no exact helper proposition is known, use an untyped research/sketch task. Choose proofPath from ${JSON.stringify(proofFiles)}${proofFiles.length < leanFiles.length ? '; workers generate Submission/Certificates modules and import them from these files' : ''}${theoryPlanning}; goals infer it from theoremName, and lemmas may omit it only when one Lean file is owned. Use lemma tasks whenever workers should save proofs; research/sketch tasks without an exact typed target only return advisory findings and restore temporary probes. Typed research tasks are normalized to lemma tasks. dependsOn defaults to []; revision to 0. Keep accepted task definitions stable; record progress in notes rather than rewriting objectives, which invalidates their checked deliveries. Revise a mathematical contract to change a route; changing only prose or revision does not resolve a recorded blocker. conditional:true allows fixed typed lemma premises. statementReview:true with informalStatement requests meaning review. Optional blueprint fields informalProof (the intended argument in at most 2000 characters) and citations (up to 8 short source references such as "Bhargava–Hanke 2005, Thm 2") guide the assigned worker; they never change the task contract or act as premises. A new concept is a lemma task with declaration:"definition" and its Lean type (e.g. "ℕ → ℕ → Prop"); put its intended meaning in objective, and list it in the dependsOn of lemmas whose types mention it. Consumers wait for its checked body. Pair each definition with a typed sanity lemma in the same proofPath that pins its intended meaning on a characterizing or small case, so a vacuous or mistaken body fails before consumers rely on it. Optional groups set ownership, sharing and budgets.\n`
: ['research', 'sketch'].includes(action.task.kind)
? 'You are a Mathematical Researcher. Explore the assigned mathematical question. ' +
advisorySourceRules +
' Deliver a concise mathematical argument or counterexample with its exact assumptions, unresolved inference and intended original-goal application, so another solver can continue the work. Use library searches to resolve specific uncertainties in that argument. Save a scoped negative library result or a classification gap in worktree-root RESEARCH.md as soon as it is established, rather than waiting for a proof or the end of the turn. Record the pinned library/environment version, searched paths, queries or method, exact result and the missing mathematical inference. A scoped search failure is not proof of global absence. Replace inherited RESEARCH.md with findings for this assignment before returning; ordinary Markdown is supported. Timed checkpoints preserve progress and resume the same assignment without completing it. When a negative result or classification gap needs a route decision, promptly publish the existing research finding with needsDecision:true, conclusion:"inconclusive", a summary stating the precise gap, evidenceRefs and remainingObligations; use conclusion:"refuted" only for an actual refutation of the scoped claim. Otherwise ordinary findings may use JSON {"summary":"findings and evidence","needsDecision":true} to request a route change. Optional conclusion is supporting, refuted or inconclusive; findings remain advisory. To turn a useful bridge into proof work, add lemmaRequests:[{id,kind:"lemma",objective,theoremName,type,proofPath,dependsOn:[]}]. Supply new ids and full closed Lean types in your own route group. The host validates these proposals and presents them to Lead; adoption in the Lead plan creates a separate typed assignment whose proof requires independent checking. A proposal does not authorize edits to Lean sources.\n'
: (action.task.kind === 'lemma'
? 'You are a Lean Prover. Write and deliver the assigned auxiliary proof for its declared consumers. '
: 'You are the main-goal Engineer. Close the original target using checked helpers. ') +
'Requests in the original task text to publish a plan or wait for mathematician plan approval belong to the Lead and host. The host applies any required plan-approval gate before dispatching typed proof work. Complete this assignment and deliver its result; do not start another planning or approval cycle. ' +
'Prove the assigned Lean declaration with its exact requested type. ' +
proofDeliverableRules +
'Save argument, consumer use and gap in RESEARCH.md; separate math from library/type/tactic issues and discharge every premise. An original goal may be a def: fill only its body. Helpers must be theorems/lemmas. Repair solver-created lemma bodies, preserving statements/placement. Preserve unrelated seed proofs, sorry and diagnostics; do not prove or rewrite them to make the whole file pass. Conditional proofs use fixed dependency lemmas, no stubs or extra assumptions. Check the saved target and imports; a Helpers-only check does not check Submission. The host checks this declaration in a disposable projection and checks the full candidate after integration.\n';
const researchSourcesRules =
action.kind === 'task' && ['lemma', 'goal'].includes(action.task.kind)
? 'Use research-observe --snapshot-sources when a finding depends on changed saved Lean code. Otherwise it is bound to the assignment seed. Publish a fresh snapshot if the supporting files change.\n'
: '';
const sharing =
action.kind === 'task'
? 'Use research-observe for findings that need to reach peers before you return. For typed proofs, save the assigned declaration and return for host checking.\n'
: leadPlanDeliveryFile(action)
? 'Use plan-apply --publish for live intake, then return promptly. The host accepts only one publication per dispatch. A notes-only return keeps the published graph. Further changes in the same dispatch can use plan-apply without --publish for intake on return.\n'
: '';
const visibleIds = new Set(cooperation.tasks.map(node => node.task.id));
const proofRoutes = (
cooperation.proofRoutes ?? cooperation.tasks.flatMap(node => node.alternativeRoutes ?? [])
).filter(
route =>
visibleIds.has(route.targetTaskId) &&
[route.closingTaskId, ...route.prerequisiteTaskIds].every(id => visibleIds.has(id)),
);
const routeTasks = cooperation.tasks.map(node => ({
...node,
alternativeRoutes: proofRoutes.filter(route => route.targetTaskId === node.task.id),
}));
const continuation = buildProofContinuation(routeTasks, {
groups: cooperation.groups,
environmentDigest: compilation.environmentDigest,
proofRoutes,
});
const planningEvidence =
action.kind === 'lead'
? leadPlanningEvidence(cooperation, compilation.environmentDigest, { goalPathReview: cooperation.goalPathReview })
: undefined;
const focus = originalGoalTaskFocus(routeTasks, cooperation.groups, compilation.environmentDigest);
const frontierSnapshot = originalGoalFrontierSnapshot(focus);
const routeIndex = createProofRoutePriorityIndex(routeTasks, {
groups: cooperation.groups,
environmentDigest: compilation.environmentDigest,
proofRoutes,
});
const routeContext = selectProofRoutePriorityContext(
routeIndex,
action.kind === 'task'
? {
assignedRouteId: action.assignment.routeId,
relatedRouteIds: proofRoutes
.filter(
route =>
route.targetTaskId === action.task.id ||
route.closingTaskId === action.task.id ||
route.prerequisiteTaskIds.includes(action.task.id),
)
.map(route => route.id),
}