@@ -41,7 +41,7 @@ function sourcePack(files: Record<string, string>): LibraryPack {
4141 origin : { kind : 'upstream' , repository : 'offline-access-fixture' , commit : 'a' . repeat ( 40 ) , file : path . basename ( file ) } } ) ) } ;
4242}
4343
44- function fixture ( options : { packs ?: LibraryPack [ ] ; frozen ?: string [ ] ; source ?: string } = { } ) {
44+ function fixture ( options : { packs ?: LibraryPack [ ] ; frozen ?: string [ ] ; frozenPack ?: LibraryPack ; source ?: string } = { } ) {
4545 const root = temporaryDirectory ( 'library-access-' ) ; roots . push ( root ) ;
4646 const packs = options . packs ?? [ sourcePack ( sources ) ] ;
4747 const built = writeLibraryCatalog ( path . join ( root , 'release' ) , {
@@ -50,8 +50,8 @@ function fixture(options: { packs?: LibraryPack[]; frozen?: string[]; source?: s
5050 const allModules = new Map ( packs . flatMap ( pack => pack . modules ) . map ( module => [ module . file , module ] ) ) ;
5151 const frozen = options . frozen ?? [ foundation , installed ] ;
5252 const receipt = bootstrapLeanTask ( path . join ( root , 'workspace' ) , { source : options . source ?? challenge ,
53- task : 'Prove the original target.' , ...( frozen . length ? {
54- libraryPack : { schema : LIBRARY_PACK_SCHEMA , modules : frozen . map ( file => allModules . get ( file ) ! ) } ,
53+ task : 'Prove the original target.' , ...( options . frozenPack || frozen . length ? {
54+ libraryPack : options . frozenPack ?? { schema : LIBRARY_PACK_SCHEMA , modules : frozen . map ( file => allModules . get ( file ) ! ) } ,
5555 } : { } ) } ) ;
5656 const problem = readSolverProblem ( receipt . workspaceRoot , receipt . activeProblemPath ) ;
5757 const access = { file : built . file , digest : built . digest } , worktree = receipt . workspaceRoot ;
@@ -180,6 +180,48 @@ describe('agent access to the complete pinned library', () => {
180180} ) ;
181181
182182describe ( 'host validation of requested frozen continuations' , ( ) => {
183+ it ( 'retains shared modules from the exact seed while extending a catalog pack with their original imports intact' , ( ) => {
184+ const sharedBase = 'Submission/Library/Shared/Base.lean' , shared = 'Submission/Library/Shared/Result.lean' ;
185+ const sharedSources = {
186+ [ sharedBase ] : 'import Submission.Library.Access.Installed\nnamespace Shared\nlemma base : True := by trivial\nend Shared\n' ,
187+ [ shared ] : 'import Submission.Library.Shared.Base\nnamespace Shared\nlemma result : True := by exact base\nend Shared\n-- 原始共享源码 🙂\n' ,
188+ } ;
189+ const f = fixture ( { frozenPack : sourcePack ( { [ foundation ] : sources [ foundation ] , [ installed ] : sources [ installed ] , ...sharedSources } ) ,
190+ source : 'module\npublic import Submission.Library.Shared.Result\n' + challenge } ) ;
191+ // A mutable working copy must never become the source of a new frozen problem.
192+ fs . appendFileSync ( path . join ( f . receipt . projectRoot , shared ) , '-- later working copy\n' ) ;
193+ const before = frozenState ( f ) ;
194+ f . library . request ( [ 'Access.all_hold' ] , 'Extend the built-in library without replacing the selected shared library.' ) ;
195+ const result = requestedLibraryPack ( f . problem , requestValue ( f ) , f . access , f . worktree ) ;
196+ expect ( result . selection . modules . map ( module => module . file ) ) . toEqual ( [ foundation , installed , requested , support ] . sort ( ) ) ;
197+ expect ( result . pack . modules . map ( module => module . file ) . sort ( ) ) . toEqual ( [ foundation , installed , requested , support , sharedBase , shared ] . sort ( ) ) ;
198+ for ( const [ file , source ] of Object . entries ( sharedSources ) ) {
199+ const module = result . pack . modules . find ( module => module . file === file ) ! ;
200+ expect ( module . source ) . toBe ( source ) ;
201+ expect ( mathDigest ( module . source ) ) . toBe ( f . problem . frozenFiles ! [ file ] ) ;
202+ expect ( module . origin ) . toMatchObject ( { commit : f . problem . baseCommit , file : path . posix . join ( f . problem . projectRelative , file ) } ) ;
203+ }
204+ expect ( frozenState ( f ) ) . toEqual ( before ) ;
205+ } ) ;
206+
207+ it ( 'rejects a retained shared source whose bytes do not match the trusted frozen identity' , ( ) => {
208+ const shared = 'Submission/Library/Shared/Result.lean' ;
209+ const f = fixture ( { frozenPack : sourcePack ( { [ shared ] : 'lemma shared : True := by trivial\n' } ) } ) ;
210+ f . library . request ( [ 'Access.all_hold' ] , 'Request a new catalog declaration.' ) ;
211+ expect ( ( ) => requestedLibraryPack ( { ...f . problem , frozenFiles : { [ shared ] : 'b' . repeat ( 64 ) } } , requestValue ( f ) , f . access , f . worktree ) )
212+ . toThrow ( 'math_library_request_existing_module_changed' ) ;
213+ } ) ;
214+
215+ it . each ( [
216+ 'namespace Access\nlemma all_hold : True := by trivial\nend Access\n' ,
217+ 'namespace Access\nstructure Box where\n value : Nat\nend Access\n' ,
218+ ] ) ( 'rejects a catalog extension that redeclares a retained shared name: %s' , sharedSource => {
219+ const shared = 'Submission/Library/Shared/Conflicting.lean' ;
220+ const f = fixture ( { frozenPack : sourcePack ( { [ shared ] : sharedSource } ) } ) ;
221+ f . library . request ( [ 'Access.all_hold' , 'Access.Box' ] , 'The new catalog modules must not collide with the shared pack.' ) ;
222+ expect ( ( ) => requestedLibraryPack ( f . problem , requestValue ( f ) , f . access , f . worktree ) ) . toThrow ( 'math_library_catalog_declaration_conflict' ) ;
223+ } ) ;
224+
183225 it ( 'retains the complete old pack, adds the requested dependency closure, and does not select unrelated goal matches' , ( ) => {
184226 const f = fixture ( ) , before = frozenState ( f ) ;
185227 f . library . request ( [ 'Access.all_hold' ] , 'Use the quantified declaration.' ) ;
@@ -264,7 +306,7 @@ describe('host validation of requested frozen continuations', () => {
264306 expect ( frozenState ( f ) ) . toEqual ( before ) ;
265307 } ) ;
266308
267- it ( 'rejects a catalog whose version changes or omits an existing frozen module ' , ( ) => {
309+ it ( 'rejects changed catalog bytes and retains frozen modules absent from the next catalog ' , ( ) => {
268310 const f = fixture ( ) , before = frozenState ( f ) ;
269311 f . library . request ( [ 'Access.all_hold' ] , 'Need the quantified declaration.' ) ;
270312 const request = requestValue ( f ) ;
@@ -274,8 +316,8 @@ describe('host validation of requested frozen continuations', () => {
274316 . toThrow ( 'math_library_request_existing_module_changed' ) ;
275317 const missing = writeLibraryCatalog ( path . join ( f . root , 'missing-release' ) , { id : 'fixture/full-library' , revision : 'v3' ,
276318 environment : 'standard' , packs : [ sourcePack ( Object . fromEntries ( Object . entries ( sources ) . filter ( ( [ file ] ) => file !== installed ) ) ) ] } ) ;
277- expect ( ( ) => requestedLibraryPack ( f . problem , { ...request , catalogDigest : missing . digest } , { file : missing . file , digest : missing . digest } , f . worktree ) )
278- . toThrow ( 'math_library_catalog_module_missing' ) ;
319+ const retained = requestedLibraryPack ( f . problem , { ...request , catalogDigest : missing . digest } , { file : missing . file , digest : missing . digest } , f . worktree ) ;
320+ expect ( retained . pack . modules . find ( module => module . file === installed ) ?. source ) . toBe ( sources [ installed ] ) ;
279321 expect ( frozenState ( f ) ) . toEqual ( before ) ;
280322 } ) ;
281323
@@ -298,15 +340,16 @@ describe('host validation of requested frozen continuations', () => {
298340 expect ( frozenState ( f ) ) . toEqual ( before ) ;
299341 } ) ;
300342
301- it ( ' counts retained modules as well as the new dependency closure toward the installation limit', ( ) => {
343+ it . each ( [ 'catalog' , 'shared' ] as const ) ( ' counts retained %s modules as well as the new dependency closure toward the installation limit', origin => {
302344 const dependencies = Object . fromEntries ( Array . from ( { length : MAX_LIBRARY_PACK_MODULES - 2 } , ( _ , i ) => [
303345 `Submission/Library/AccessBudget/D${ i } .lean` , `namespace AccessBudget.D${ i } \ndef value : Nat := 0\nend AccessBudget.D${ i } \n` ,
304346 ] ) ) ;
305347 const entry = 'Submission/Library/AccessBudget/Entry.lean' ;
306348 const budgetPack = sourcePack ( { ...dependencies , [ entry ] : Object . keys ( dependencies )
307349 . map ( file => `import ${ file . slice ( 0 , - 5 ) . replaceAll ( '/' , '.' ) } \n` ) . join ( '' ) +
308350 'namespace AccessBudget\ntheorem entry : True := trivial\nend AccessBudget\n' } ) ;
309- const f = fixture ( { packs : [ sourcePack ( { [ foundation ] : sources [ foundation ] , [ installed ] : sources [ installed ] } ) , budgetPack ] } ) ;
351+ const oldPack = sourcePack ( { [ foundation ] : sources [ foundation ] , [ installed ] : sources [ installed ] } ) ;
352+ const f = fixture ( { packs : origin === 'catalog' ? [ oldPack , budgetPack ] : [ budgetPack ] , frozenPack : oldPack } ) ;
310353 const before = frozenState ( f ) ;
311354 const catalog = readLibraryCatalog ( f . access . file , f . access . digest ) ;
312355 expect ( catalog . select ( { source : 'import Submission.Library.AccessBudget.Entry\n' , task : '' , automatic : false ,
0 commit comments