lawspec 0.25.0 → 0.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (156) hide show
  1. package/CHANGELOG.md +109 -1
  2. package/NORMALIZATION-LICENSES.txt +296 -0
  3. package/README.md +13 -4
  4. package/api.mjs +0 -8
  5. package/bin/lawspec.mjs +114 -79
  6. package/build.json +99 -117
  7. package/core.wasm +2 -2
  8. package/core_jsffi.js +5 -0
  9. package/doctor.mjs +14 -4
  10. package/examples/native-payments/clojure/lawspec.json +1 -1
  11. package/examples/native-payments/csharp/lawspec.json +1 -1
  12. package/examples/native-payments/elixir/lawspec.json +1 -1
  13. package/examples/native-payments/erlang/lawspec.json +1 -1
  14. package/examples/native-payments/fsharp/lawspec.json +1 -1
  15. package/examples/native-payments/gleam/lawspec.json +1 -1
  16. package/examples/native-payments/go/lawspec.json +1 -1
  17. package/examples/native-payments/groovy/lawspec.json +1 -1
  18. package/examples/native-payments/haskell/lawspec.json +1 -1
  19. package/examples/native-payments/java/lawspec.json +1 -1
  20. package/examples/native-payments/javascript/lawspec.json +1 -1
  21. package/examples/native-payments/kotlin/lawspec.json +1 -1
  22. package/examples/native-payments/prolog/lawspec.json +20 -0
  23. package/examples/native-payments/prolog/src/domain/payments.pl +23 -0
  24. package/examples/native-payments/prolog/tests/domain/payment_generators.pl +17 -0
  25. package/examples/native-payments/python/lawspec.json +1 -1
  26. package/examples/native-payments/rust/lawspec.json +1 -1
  27. package/examples/native-payments/scala/lawspec.json +1 -1
  28. package/examples/native-payments/swift/lawspec.json +1 -1
  29. package/examples/native-payments/typescript/lawspec.json +1 -1
  30. package/examples/packages/orders/lawspec.json +1 -1
  31. package/examples/packages/orders/orders.lawspec +27 -25
  32. package/examples/packages/shop-domain/src/domain.lawspec +15 -14
  33. package/examples/packages/shop-tax-1/src/api.lawspec +1 -0
  34. package/examples/packages/shop-tax-1/src/rates.lawspec +1 -0
  35. package/examples/packages/shop-tax-2/src/api.lawspec +1 -0
  36. package/examples/packages/shop-tax-2/src/rates.lawspec +1 -0
  37. package/examples/rc1/authorization-snapshots.lawspec +24 -23
  38. package/examples/rc1/banner-filenames.lawspec +7 -6
  39. package/examples/rc1/bit-codec.lawspec +12 -11
  40. package/examples/rc1/bounded-witness-search.lawspec +52 -45
  41. package/examples/rc1/conflicting-requirements.lawspec +8 -7
  42. package/examples/rc1/constructive-witnesses.lawspec +40 -33
  43. package/examples/rc1/documentary-review.lawspec +9 -8
  44. package/examples/rc1/empty-domains.lawspec +16 -15
  45. package/examples/rc1/finite-integer-domains.lawspec +24 -23
  46. package/examples/rc1/law-metadata.lawspec +6 -5
  47. package/examples/rc1/native-payments.lawspec +17 -16
  48. package/examples/rc1/nesting/rrr.lawspec +4 -3
  49. package/examples/rc1/nesting/rru.lawspec +7 -6
  50. package/examples/rc1/nesting/rur.lawspec +7 -6
  51. package/examples/rc1/nesting/ruu.lawspec +10 -9
  52. package/examples/rc1/nesting/urr.lawspec +7 -6
  53. package/examples/rc1/nesting/uru.lawspec +10 -9
  54. package/examples/rc1/nesting/uur.lawspec +10 -9
  55. package/examples/rc1/nesting/uuu.lawspec +13 -12
  56. package/examples/rc1/order-workflow.lawspec +18 -17
  57. package/examples/rc1/parcel-measurements.lawspec +14 -13
  58. package/examples/rc1/picture-records.lawspec +6 -5
  59. package/examples/rc1/picture-tuples.lawspec +6 -5
  60. package/examples/rc1/record-payloads.lawspec +12 -11
  61. package/examples/rc1/recursive-records.lawspec +11 -10
  62. package/examples/rc1/refund-conformance.lawspec +7 -6
  63. package/examples/rc1/refund-policy.lawspec +15 -14
  64. package/examples/rc1/requirement-composition.lawspec +26 -25
  65. package/examples/rc1/structural-records.lawspec +13 -12
  66. package/examples/rc1-authoring.lawspec +6 -5
  67. package/examples/specs/abilities.lawspec +67 -65
  68. package/examples/specs/ability_imports.lawspec +58 -57
  69. package/examples/specs/actors.lawspec +23 -22
  70. package/examples/specs/algebra.lawspec +142 -141
  71. package/examples/specs/approvals.lawspec +19 -18
  72. package/examples/specs/async_bindings.lawspec +10 -9
  73. package/examples/specs/async_fetch.lawspec +12 -11
  74. package/examples/specs/atoi_codec.lawspec +16 -15
  75. package/examples/specs/boolean_flags.lawspec +10 -9
  76. package/examples/specs/builtins.lawspec +73 -72
  77. package/examples/specs/canonical_url.lawspec +14 -13
  78. package/examples/specs/collections.lawspec +153 -154
  79. package/examples/specs/composition.lawspec +70 -66
  80. package/examples/specs/concurrent_collections.lawspec +8 -7
  81. package/examples/specs/consistency.lawspec +4 -3
  82. package/examples/specs/contracts.lawspec +80 -79
  83. package/examples/specs/crypto.lawspec +26 -25
  84. package/examples/specs/ctc.lawspec +148 -0
  85. package/examples/specs/currying.lawspec +32 -31
  86. package/examples/specs/data_types.lawspec +50 -49
  87. package/examples/specs/distribution.lawspec +57 -56
  88. package/examples/specs/domain_modeling.lawspec +68 -47
  89. package/examples/specs/durations.lawspec +39 -38
  90. package/examples/specs/equivalent.lawspec +25 -24
  91. package/examples/specs/failures.lawspec +64 -63
  92. package/examples/specs/finite_data.lawspec +32 -31
  93. package/examples/specs/flow_stack.lawspec +64 -63
  94. package/examples/specs/gadt_expressions.lawspec +50 -49
  95. package/examples/specs/generation.lawspec +115 -114
  96. package/examples/specs/handler_bindings.lawspec +42 -40
  97. package/examples/specs/handles.lawspec +2 -1
  98. package/examples/specs/harness.lawspec +45 -43
  99. package/examples/specs/harness_testing.lawspec +18 -13
  100. package/examples/specs/indexed_arithmetic.lawspec +401 -126
  101. package/examples/specs/indexed_families.lawspec +182 -66
  102. package/examples/specs/keywords.lawspec +22 -21
  103. package/examples/specs/limits.lawspec +7 -6
  104. package/examples/specs/list_contracts.lawspec +121 -120
  105. package/examples/specs/list_refinements.lawspec +43 -42
  106. package/examples/specs/lists.lawspec +45 -44
  107. package/examples/specs/matchers.lawspec +79 -78
  108. package/examples/specs/matching.lawspec +43 -42
  109. package/examples/specs/mixed_inputs.lawspec +25 -24
  110. package/examples/specs/models.lawspec +44 -43
  111. package/examples/specs/parse_port.lawspec +28 -27
  112. package/examples/specs/payments.lawspec +55 -54
  113. package/examples/specs/railway.lawspec +59 -58
  114. package/examples/specs/records.lawspec +149 -148
  115. package/examples/specs/records_inner.lawspec +5 -4
  116. package/examples/specs/recursive_refinements.lawspec +30 -29
  117. package/examples/specs/refined_definitions.lawspec +24 -23
  118. package/examples/specs/refinements.lawspec +58 -57
  119. package/examples/specs/resilience.lawspec +161 -160
  120. package/examples/specs/resources.lawspec +61 -60
  121. package/examples/specs/scalar_adapters.lawspec +60 -59
  122. package/examples/specs/scalar_catalog.lawspec +165 -164
  123. package/examples/specs/scalars.lawspec +78 -77
  124. package/examples/specs/scheduling_order.lawspec +8 -7
  125. package/examples/specs/scheduling_overlap.lawspec +5 -4
  126. package/examples/specs/sequences.lawspec +66 -65
  127. package/examples/specs/sessions.lawspec +13 -12
  128. package/examples/specs/slug.lawspec +16 -15
  129. package/examples/specs/sum_refinements.lawspec +42 -41
  130. package/examples/specs/tables.lawspec +29 -28
  131. package/examples/specs/total_functions.lawspec +71 -70
  132. package/examples/specs/workflows.lawspec +101 -100
  133. package/examples-command.mjs +7 -7
  134. package/explain.mjs +6 -4
  135. package/expression-presentation.mjs +8 -8
  136. package/files.mjs +1 -1
  137. package/index.d.ts +10 -528
  138. package/launcher.mjs +9 -2
  139. package/lsp.mjs +10 -6
  140. package/native-evidence.mjs +25 -12
  141. package/native-examples.mjs +3 -3
  142. package/package.json +2 -1
  143. package/starter-rc1.lawspec +6 -5
  144. package/starter.lawspec +16 -15
  145. package/templates.mjs +88 -5
  146. package/test-command.mjs +40 -4
  147. package/migrate.mjs +0 -135
  148. package/migration-bindings.mjs +0 -86
  149. package/migration-cases.mjs +0 -45
  150. package/migration-collections.mjs +0 -54
  151. package/migration-correspondence.mjs +0 -275
  152. package/migration-generation.mjs +0 -99
  153. package/migration-laws.mjs +0 -102
  154. package/migration-records.mjs +0 -166
  155. package/migration-source.mjs +0 -247
  156. package/migration-write.mjs +0 -156
@@ -1,156 +0,0 @@
1
- // Generated from templates/npm/migration-write.mjs by lawspec-dev generate. Do not edit.
2
- // A write always obtains a fresh checked report. Serialized reports are review
3
- // artifacts, never authorization to bypass the compiler's correspondence gates.
4
- import {readFile,writeFile,open,link,rename,unlink,lstat,chmod} from 'node:fs/promises';
5
- import path from 'node:path';
6
- import {randomUUID} from 'node:crypto';
7
- import {safePath,collectSources} from './files.mjs';
8
- import {migrationReport} from './migrate.mjs';
9
-
10
- const failure=(code,message,extra={})=>Object.assign(new Error(message),
11
- {diagnostics:[{code,message,at:null}],...extra});
12
- const remove=async file=>{try{await unlink(file);}catch(error){if(error.code!=='ENOENT')throw error;}};
13
- const unchanged=async entry=>{
14
- const info=await lstat(entry.file),bytes=await readFile(entry.file);
15
- if(!info.isFile()||info.dev!==entry.info.dev||info.ino!==entry.info.ino||info.mode!==entry.info.mode||
16
- !bytes.equals(Buffer.from(entry.expected,'utf8')))
17
- throw failure('migration-stale','File changed during migration: '+entry.path);
18
- };
19
-
20
- // Stage every replacement before committing, retain original inodes for
21
- // rollback, and commit configuration last. A process/OS crash is not a global
22
- // filesystem transaction; recovery paths are retained if rollback cannot finish.
23
- export async function commitMigration(root,changes,{recheck=async()=>{},packageLocations=new Map()}={}){
24
- // Only manifest-observed package sources receive an alternate root. Ordinary
25
- // destinations retain the project's traversal and symbolic-link checks.
26
- const destination=relative=>{
27
- const location=packageLocations.get(relative);
28
- return location?safePath(location.root,location.path):safePath(root,relative);
29
- };
30
- const lockPath=await safePath(root,'.lawspec-migration.lock');
31
- let lock;
32
- try{lock=await open(lockPath,'wx');}
33
- catch(error){throw failure('migration-write',error.code==='EEXIST'?
34
- 'A migration lock already exists: '+lockPath:error.message);}
35
- const entries=[],observed=[],applied=[],unrestored=[],recovery=[];
36
- let problem;
37
- try{
38
- if(new Set(changes.map(change=>change.path)).size!==changes.length)
39
- throw failure('migration-write','Duplicate migration destination.');
40
- for(const change of changes){
41
- if(typeof change.expected!=='string'||typeof change.content!=='string')
42
- throw failure('migration-write','Migration replacements require exact original and destination text.');
43
- const file=await destination(change.path),info=await lstat(file),bytes=await readFile(file);
44
- if(!info.isFile()||!(info.mode&0o222))throw failure('migration-write','Migration requires a writable regular file: '+change.path);
45
- if(!bytes.equals(Buffer.from(change.expected,'utf8')))
46
- throw failure('migration-stale','File changed during migration: '+change.path);
47
- observed.push({...change,file,info});
48
- if(change.expected===change.content)continue;
49
- const suffix='.lawspec-migrate-'+randomUUID();
50
- const entry={...change,file,info,stage:file+suffix+'.tmp',backup:file+suffix+'.bak'};
51
- entries.push(entry);
52
- await writeFile(entry.stage,entry.content,{flag:'wx',mode:info.mode&0o7777});
53
- await chmod(entry.stage,info.mode&0o7777);
54
- entry.stageInfo=await lstat(entry.stage);
55
- await link(file,entry.backup);
56
- }
57
- await recheck();
58
- for(const entry of observed){await destination(entry.path);await unchanged(entry);}
59
- for(const entry of entries){
60
- await destination(entry.path);await unchanged(entry);
61
- await rename(entry.stage,entry.file);applied.push(entry);
62
- }
63
- }catch(error){
64
- problem=error;
65
- for(const entry of applied.reverse()){
66
- try{
67
- // An editor's newer value must not be overwritten by rollback.
68
- const info=await lstat(entry.file);
69
- if(info.ino!==entry.stageInfo.ino||info.dev!==entry.stageInfo.dev||info.mode!==entry.stageInfo.mode||
70
- (await readFile(entry.file,'utf8'))!==entry.content)throw new Error('File changed after replacement.');
71
- await rename(entry.backup,entry.file);
72
- }catch(rollbackError){unrestored.push(entry.path);recovery.push({path:entry.path,original:entry.backup,reason:rollbackError.message});}
73
- }
74
- }finally{
75
- for(const entry of entries)for(const file of [entry.stage,entry.backup]){
76
- if(recovery.some(item=>item.original===file))continue;
77
- try{await remove(file);}catch(error){recovery.push({path:entry.path,original:file,reason:error.message});}
78
- }
79
- await lock.close();
80
- try{await remove(lockPath);}catch(error){recovery.push({path:'.lawspec-migration.lock',original:lockPath,reason:error.message});}
81
- }
82
- if(problem||recovery.length){
83
- const written=problem?unrestored:entries.map(entry=>entry.path);
84
- throw failure(problem?.diagnostics?.[0]?.code??'migration-write',
85
- (problem?.message??'Migration committed, but temporary-file cleanup is incomplete.')+
86
- (recovery.length?' Recovery files: '+recovery.map(item=>item.original).join(', '):''),
87
- {filesWritten:written,recovery});
88
- }
89
- return entries.map(entry=>entry.path);
90
- }
91
-
92
- export async function writeMigration(compiler,root,configurationPath,configurationText,input,{recheck}={}){
93
- const config=JSON.parse(configurationText),snapshot=structuredClone(input);
94
- if(config.version!==1||!Array.isArray(config.sources)||!Array.isArray(config.targets))
95
- throw failure('migration-configuration','Writing requires a version 1 configuration with sources and targets arrays.');
96
- const roots=config.targets.map(target=>typeof target?.root==='string'&&target.root&&!target.root.includes('\0')?
97
- path.resolve(root,target.root):null);
98
- if(roots.includes(null)||new Set(roots).size!==roots.length)
99
- throw failure('migration-configuration','Every configured target requires a distinct project root.');
100
- const packagePaths=config.packages??[],packages=snapshot.packages??[];
101
- if(!Array.isArray(packagePaths)||packagePaths.some(value=>typeof value!=='string')||
102
- !Array.isArray(packages)||packages.length!==packagePaths.length||snapshot.package!==undefined||
103
- JSON.stringify(snapshot.dependencies??{})!==JSON.stringify(config.dependencies??{}))
104
- throw failure('migration-configuration','Checked package inputs must match the project configuration.');
105
- if(snapshot.nativeBindings!==undefined)
106
- throw failure('migration-configuration','Project writing requires native bindings in configured targets, rather than one-off API bindings.');
107
- if((snapshot.machineBits??64)!==(config.machineBits??64)||
108
- JSON.stringify(snapshot.generation)!==JSON.stringify(config.generation))
109
- throw failure('migration-configuration','Project writing requires the machine width and generation settings from its configuration.');
110
- const packageLocations=new Map(),packageManifests=[];
111
- for(let index=0;index<packages.length;index++){
112
- const packageRoot=path.resolve(root,packagePaths[index]);
113
- const manifestPath=path.join(packageRoot,'lawspec-package.json');
114
- const text=await readFile(manifestPath,'utf8'),manifest=JSON.parse(text),pkg=packages[index];
115
- if(pkg.name!==manifest.name||pkg.version!==manifest.version||
116
- JSON.stringify(pkg.dependencies??{})!==JSON.stringify(manifest.dependencies??{})||
117
- JSON.stringify(pkg.sources)!==JSON.stringify(await collectSources(packageRoot,manifest.sources,root)))
118
- throw failure('migration-configuration','Checked package sources must match their configured manifest: '+packagePaths[index]);
119
- packageManifests.push({root:packageRoot,path:manifestPath,text,sources:manifest.sources,expected:pkg.sources});
120
- for(const source of pkg.sources){
121
- const relative=path.relative(packageRoot,path.resolve(root,source.path)).split(path.sep).join('/');
122
- await safePath(packageRoot,relative);
123
- if(packageLocations.has(source.path))throw failure('migration-configuration','Duplicate package source: '+source.path);
124
- packageLocations.set(source.path,{root:packageRoot,path:relative});
125
- }
126
- }
127
- const observeSources=async()=>{
128
- if(JSON.stringify(await collectSources(root,config.sources))!==JSON.stringify(snapshot.sources))
129
- throw failure('migration-stale','The complete project source inventory changed or differs from the checked input.');
130
- for(const pkg of packageManifests){
131
- if(await readFile(pkg.path,'utf8')!==pkg.text||
132
- JSON.stringify(await collectSources(pkg.root,pkg.sources,root))!==JSON.stringify(pkg.expected))
133
- throw failure('migration-stale','A package manifest or complete source inventory changed: '+pkg.path);
134
- }
135
- };
136
- if(['packages','package','dependencies'].some(key=>snapshot[key]!==undefined)&&typeof recheck!=='function')
137
- throw failure('migration-write','Package-backed migration requires rechecking package inputs before committing.');
138
- await observeSources();
139
- const report=await migrationReport(compiler,config,snapshot);
140
- if(report.diagnostics.length)throw Object.assign(new Error(report.diagnostics.map(item=>item.message).join(' ')),
141
- {diagnostics:report.diagnostics});
142
- if(!report.validated)throw failure('migration-incomplete',report.blockers.join(' ')||'Migration correspondence was not established.');
143
- const originalSources=[...snapshot.sources,...packages.flatMap(pkg=>pkg.sources)];
144
- const previewSources=[...report.sourcePreview.sources,...(report.sourcePreview.packages??[]).flatMap(pkg=>pkg.sources)];
145
- const originals=new Map(originalSources.map(source=>[source.path,source.content]));
146
- if(originals.size!==originalSources.length||previewSources.length!==originals.size||
147
- new Set(previewSources.map(source=>source.path)).size!==originals.size||
148
- previewSources.some(source=>!originals.has(source.path)))
149
- throw failure('migration-write','Every admitted source must have exactly one replacement.');
150
- const changes=previewSources.map(source=>({path:source.path,
151
- expected:originals.get(source.path),content:source.content}));
152
- changes.push({path:configurationPath,expected:configurationText,
153
- content:JSON.stringify(report.proposedConfiguration,null,2)+'\n'});
154
- report.filesWritten=await commitMigration(root,changes,{packageLocations,recheck:async()=>{await recheck?.();await observeSources();}});
155
- return report;
156
- }