-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIncrementalCompiler.ts
More file actions
1157 lines (1035 loc) · 33.2 KB
/
Copy pathIncrementalCompiler.ts
File metadata and controls
1157 lines (1035 loc) · 33.2 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
/**
* IncrementalCompiler - Hot reload and incremental compilation support
*
* Features:
* - AST diffing to detect changes
* - Selective recompilation of changed nodes
* - State preservation during reload
* - Dependency tracking between objects
*/
import type {
HoloComposition,
HoloObjectDecl,
HoloObjectProperty,
} from '../parser/HoloCompositionTypes';
import type { Extensible } from '../types/utility-types';
import type { HSPlusCompileResult } from '../types/AdvancedTypeSystem';
import type { ReadFileFn } from '../parser/HoloCompositionTypes';
import {
TraitDependencyGraph,
globalTraitGraph,
type TraitUsage,
type TraitChangeInfo,
} from './TraitDependencyGraph';
import {
ImportResolver,
resolveImportPath,
type ImportResolutionResult,
type ImportResolutionError,
} from '../parser/ImportResolver';
import {
SemanticCache,
createSemanticCache,
hashSourceCode,
hashASTSubtree,
type SemanticCacheStats,
} from './SemanticCache';
import { getRBAC, ResourceType, type AccessDecision } from './identity/AgentRBAC';
import { WorkflowStep } from './identity/AgentIdentity';
import { UnauthorizedCompilerAccessError } from './CompilerBase';
import { BuildCache, type CacheEntryType } from './BuildCache';
import { computeContentHash } from '../deploy/provenance';
import { readJson } from '../errors/safeJsonParse';
/**
* Types of changes detected during AST diff
*/
export type ChangeType = 'added' | 'removed' | 'modified' | 'unchanged';
/**
* Represents a single change in the AST
*/
export interface ASTChange {
type: ChangeType;
path: string[];
nodeName: string;
nodeType: 'object' | 'property' | 'trait' | 'logic' | 'composition';
oldValue?: unknown;
newValue?: unknown;
}
/**
* Result of diffing two ASTs
*/
export interface DiffResult {
hasChanges: boolean;
changes: ASTChange[];
addedObjects: string[];
removedObjects: string[];
modifiedObjects: string[];
unchangedObjects: string[];
}
/**
* Compilation cache entry
*/
export interface CacheEntry {
hash: string;
compiledCode: string;
dependencies: string[];
timestamp: number;
}
/**
* State snapshot for hot reload
*/
export interface StateSnapshot {
objectStates: Map<string, Record<string, unknown>>;
timestamp: number;
}
/**
* Options for incremental compilation
*/
export interface IncrementalCompileOptions {
preserveState?: boolean;
forceRecompile?: string[];
skipUnchanged?: boolean;
// ── Import resolution ──────────────────────────────────────────────────────
/**
* When `true`, the compiler will call `ImportResolver.resolve()` on the
* `HSPlusCompileResult` before handing off to the object compiler.
* Requires `baseDir` to be supplied.
* @default false
*/
enableImports?: boolean;
/**
* Base directory for resolving relative `@import` paths.
* Required when `enableImports` is `true`.
*/
baseDir?: string;
/**
* Canonical absolute path for the source file being compiled.
* Used as the cache key and as the starting point for path resolution.
*/
sourceFile?: string;
/**
* Custom file-reader function.
* Falls back to Node.js `fs.promises.readFile` when omitted.
* @see ReadFileFn in HoloCompositionTypes.ts
*/
readFile?: ReadFileFn;
// ── Semantic Caching ───────────────────────────────────────────────────────
/**
* Enable semantic caching with Redis backend.
* Falls back to in-memory cache if Redis unavailable.
* @default false
*/
enableSemanticCache?: boolean;
/**
* Redis connection URL for semantic cache.
* @default 'redis://localhost:6379'
*/
redisUrl?: string;
/**
* Cache TTL in seconds.
* @default 604800 (7 days)
*/
cacheTTL?: number;
// ── Agent Identity ────────────────────────────────────────────────────────
/**
* JWT token proving agent identity.
* When provided, RBAC validation is enforced before compilation.
* Omit for backwards compatibility / testing without authentication.
*/
agentToken?: string;
}
/**
* Incremental compilation result
*/
export interface IncrementalCompileResult {
fullRecompile: boolean;
recompiledObjects: string[];
cachedObjects: string[];
statePreserved: boolean;
compiledCode: string;
changes: DiffResult;
// ── Import resolution ──────────────────────────────────────────────────────
/**
* Merged scope returned by `ImportResolver.resolve()`.
* Only populated when `options.enableImports === true`.
* Keys are `alias.ExportName` (namespace) or bare export names (named imports).
*/
importScope?: Map<string, unknown>;
/** Any import resolution errors (missing files, cycles, etc.) */
importErrors?: ImportResolutionError[];
}
/**
* Simple hash function for content comparison (DJB2)
*/
function hashContent(content: string): string {
let hash = 0;
for (let i = 0; i < content.length; i++) {
const char = content.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash = hash & hash;
}
return hash.toString(36);
}
/**
* Hash a config object for trait comparison
* Uses stable JSON serialization with sorted keys
*/
function hashConfig(config: Record<string, unknown>): string {
const keys = Object.keys(config).sort();
const parts: string[] = [];
for (const key of keys) {
const value = config[key];
parts.push(`${key}:${JSON.stringify(value)}`);
}
const str = parts.join('|');
// DJB2 hash
let hash = 5381;
for (let i = 0; i < str.length; i++) {
hash = ((hash << 5) + hash) ^ str.charCodeAt(i);
}
return (hash >>> 0).toString(36);
}
/**
* Serialize a HoloObjectDecl for comparison
*/
function serializeObject(obj: HoloObjectDecl): string {
return JSON.stringify({
name: obj.name,
properties: obj.properties,
traits: obj.traits,
children: obj.children?.map(serializeObject),
logic: (obj as Extensible<HoloObjectDecl>).logic, // logic might not be directly on HoloObjectDecl but used in diff
});
}
/**
* Serialize a property value for comparison
*/
function serializeProperty(prop: HoloObjectProperty): string {
return JSON.stringify({ key: prop.key, value: prop.value });
}
/**
* IncrementalCompiler class for hot reload support
*/
export class IncrementalCompiler {
private cache: Map<string, CacheEntry> = new Map();
private previousAST: HoloComposition | null = null;
private stateSnapshot: StateSnapshot | null = null;
private dependencyGraph: Map<string, Set<string>> = new Map();
private traitGraph: TraitDependencyGraph;
private semanticCache: SemanticCache | null = null;
/** Persistent disk cache keyed by provenance content-hash (paper-10). */
private buildCache: BuildCache | null = null;
constructor(
traitGraph?: TraitDependencyGraph,
options?: {
enableSemanticCache?: boolean;
redisUrl?: string;
cacheTTL?: number;
/**
* Persistent build cache for cross-session provenance-aware caching.
* When supplied, compiled objects are stored/retrieved by their SHA-256
* content hash (paper-10 §3.2 "content-addressable cache").
*/
buildCache?: BuildCache;
}
) {
this.traitGraph = traitGraph || globalTraitGraph;
this.buildCache = options?.buildCache ?? null;
// Initialize semantic cache if enabled
if (options?.enableSemanticCache) {
this.semanticCache = createSemanticCache({
redisUrl: options.redisUrl,
ttl: options.cacheTTL,
debug: false,
});
// Initialize async
this.semanticCache.initialize().catch((err) => {
console.warn(`Semantic cache initialization failed: ${err}`);
this.semanticCache = null;
});
}
}
/**
* Diff two ASTs and return the changes
*/
diff(oldAST: HoloComposition | null, newAST: HoloComposition): DiffResult {
const changes: ASTChange[] = [];
const addedObjects: string[] = [];
const removedObjects: string[] = [];
const modifiedObjects: string[] = [];
const unchangedObjects: string[] = [];
if (!oldAST) {
// First compile - everything is new
const allObjects = this.collectObjectNames(newAST);
return {
hasChanges: true,
changes: allObjects.map((name) => ({
type: 'added' as ChangeType,
path: [name],
nodeName: name,
nodeType: 'object' as const,
newValue: this.findObject(newAST, name),
})),
addedObjects: allObjects,
removedObjects: [],
modifiedObjects: [],
unchangedObjects: [],
};
}
const oldObjects = new Map<string, HoloObjectDecl>();
const newObjects = new Map<string, HoloObjectDecl>();
// Build maps of objects by name
this.collectObjects(oldAST, oldObjects);
this.collectObjects(newAST, newObjects);
// Find added objects
for (const [name, obj] of newObjects) {
if (!oldObjects.has(name)) {
addedObjects.push(name);
changes.push({
type: 'added',
path: [name],
nodeName: name,
nodeType: 'object',
newValue: obj,
});
}
}
// Find removed objects
for (const [name, obj] of oldObjects) {
if (!newObjects.has(name)) {
removedObjects.push(name);
changes.push({
type: 'removed',
path: [name],
nodeName: name,
nodeType: 'object',
oldValue: obj,
});
}
}
// Find modified and unchanged objects
for (const [name, newObj] of newObjects) {
if (oldObjects.has(name)) {
const oldObj = oldObjects.get(name)!;
const objectChanges = this.diffObject(oldObj, newObj, [name]);
if (objectChanges.length > 0) {
modifiedObjects.push(name);
changes.push(...objectChanges);
} else {
unchangedObjects.push(name);
}
}
}
return {
hasChanges: changes.length > 0,
changes,
addedObjects,
removedObjects,
modifiedObjects,
unchangedObjects,
};
}
/**
* Diff two objects and return property-level changes
*/
private diffObject(oldObj: HoloObjectDecl, newObj: HoloObjectDecl, path: string[]): ASTChange[] {
const changes: ASTChange[] = [];
// Compare properties
const oldProps = new Map(oldObj.properties?.map((p) => [p.key, p]) || []);
const newProps = new Map(newObj.properties?.map((p) => [p.key, p]) || []);
for (const [key, newProp] of newProps) {
if (!oldProps.has(key)) {
changes.push({
type: 'added',
path: [...path, key],
nodeName: key,
nodeType: 'property',
newValue: newProp.value,
});
} else {
const oldProp = oldProps.get(key)!;
if (serializeProperty(oldProp) !== serializeProperty(newProp)) {
changes.push({
type: 'modified',
path: [...path, key],
nodeName: key,
nodeType: 'property',
oldValue: oldProp.value,
newValue: newProp.value,
});
}
}
}
for (const key of oldProps.keys()) {
if (!newProps.has(key)) {
changes.push({
type: 'removed',
path: [...path, key],
nodeName: key,
nodeType: 'property',
oldValue: oldProps.get(key)!.value,
});
}
}
// Compare traits with config-aware detection
const oldTraitUsages = this.extractTraitUsages(oldObj.traits || []);
const newTraitUsages = this.extractTraitUsages(newObj.traits || []);
const oldTraitMap = new Map(oldTraitUsages.map((t) => [t.name, t]));
const newTraitMap = new Map(newTraitUsages.map((t) => [t.name, t]));
// Check for added traits
for (const [name, newTrait] of newTraitMap) {
if (!oldTraitMap.has(name)) {
changes.push({
type: 'added',
path: [...path, '@' + name],
nodeName: name,
nodeType: 'trait',
newValue: newTrait,
});
}
}
// Check for removed traits
for (const [name, oldTrait] of oldTraitMap) {
if (!newTraitMap.has(name)) {
changes.push({
type: 'removed',
path: [...path, '@' + name],
nodeName: name,
nodeType: 'trait',
oldValue: oldTrait,
});
}
}
// Check for config changes (trait exists in both but config differs)
for (const [name, newTrait] of newTraitMap) {
const oldTrait = oldTraitMap.get(name);
if (oldTrait && oldTrait.configHash !== newTrait.configHash) {
changes.push({
type: 'modified',
path: [...path, '@' + name],
nodeName: name,
nodeType: 'trait',
oldValue: oldTrait,
newValue: newTrait,
});
}
}
// Compare logic blocks
if (
JSON.stringify((oldObj as Extensible<HoloObjectDecl>).logic) !==
JSON.stringify((newObj as Extensible<HoloObjectDecl>).logic)
) {
changes.push({
type: 'modified',
path: [...path, 'logic'],
nodeName: 'logic',
nodeType: 'logic',
oldValue: (oldObj as Extensible<HoloObjectDecl>).logic,
newValue: (newObj as Extensible<HoloObjectDecl>).logic,
});
}
// Compare children recursively
const oldChildren = new Map((oldObj.children || []).map((c) => [c.name, c]));
const newChildren = new Map((newObj.children || []).map((c) => [c.name, c]));
for (const [name, newChild] of newChildren) {
if (!oldChildren.has(name)) {
changes.push({
type: 'added',
path: [...path, name],
nodeName: name,
nodeType: 'object',
newValue: newChild,
});
} else {
const childChanges = this.diffObject(oldChildren.get(name)!, newChild, [...path, name]);
changes.push(...childChanges);
}
}
for (const name of oldChildren.keys()) {
if (!newChildren.has(name)) {
changes.push({
type: 'removed',
path: [...path, name],
nodeName: name,
nodeType: 'object',
oldValue: oldChildren.get(name),
});
}
}
return changes;
}
/**
* Extract trait usages with config hashes from trait array
* Handles both string traits and trait objects with config
*/
private extractTraitUsages(traits: unknown[]): TraitUsage[] {
return traits.map((t) => {
if (typeof t === 'string') {
// Simple string trait: "@physics" → { name: "physics", config: {}, configHash: "..." }
return {
name: t,
config: {},
configHash: hashConfig({}),
};
}
if (t && typeof t === 'object') {
const traitObj = t as Record<string, unknown>;
// Trait with name property: { name: "physics", mass: 2.0 }
if ('name' in traitObj) {
const name = String(traitObj.name);
// Extract config (everything except 'name')
const config: Record<string, unknown> = {};
for (const [key, value] of Object.entries(traitObj)) {
if (key !== 'name') {
config[key] = value;
}
}
return {
name,
config,
configHash: hashConfig(config),
};
}
// Single key object: { physics: { mass: 2.0 } }
const keys = Object.keys(traitObj);
if (keys.length === 1) {
const name = keys[0];
const config = (traitObj[name] as Record<string, unknown>) || {};
return {
name,
config: typeof config === 'object' ? config : {},
configHash: hashConfig(typeof config === 'object' ? config : {}),
};
}
}
// Fallback: stringify unknown format
return {
name: String(t),
config: {},
configHash: hashConfig({}),
};
});
}
/**
* Collect all object names from a composition
*/
private collectObjectNames(composition: HoloComposition): string[] {
const names: string[] = [];
const collectFromObject = (obj: HoloObjectDecl) => {
names.push(obj.name);
obj.children?.forEach(collectFromObject);
};
composition.objects?.forEach(collectFromObject);
composition.spatialGroups?.forEach((group) => {
names.push(group.name);
group.objects?.forEach(collectFromObject);
});
return names;
}
/**
* Collect all objects into a map
*/
private collectObjects(composition: HoloComposition, map: Map<string, HoloObjectDecl>): void {
const collectFromObject = (obj: HoloObjectDecl) => {
map.set(obj.name, obj);
obj.children?.forEach(collectFromObject);
};
composition.objects?.forEach(collectFromObject);
composition.spatialGroups?.forEach((group) => {
group.objects?.forEach(collectFromObject);
});
}
/**
* Find an object by name in a composition
*/
private findObject(composition: HoloComposition, name: string): HoloObjectDecl | undefined {
const searchInObject = (obj: HoloObjectDecl): HoloObjectDecl | undefined => {
if (obj.name === name) return obj;
for (const child of obj.children || []) {
const found = searchInObject(child);
if (found) return found;
}
return undefined;
};
for (const obj of composition.objects || []) {
const found = searchInObject(obj);
if (found) return found;
}
for (const group of composition.spatialGroups || []) {
for (const obj of group.objects || []) {
const found = searchInObject(obj);
if (found) return found;
}
}
return undefined;
}
/**
* Get cached compilation for an object if still valid
*/
getCached(objectName: string, currentHash: string): CacheEntry | null {
const entry = this.cache.get(objectName);
if (entry && entry.hash === currentHash) {
return entry;
}
return null;
}
/**
* Store compilation result in cache
*/
setCached(objectName: string, hash: string, compiledCode: string, dependencies: string[]): void {
this.cache.set(objectName, {
hash,
compiledCode,
dependencies,
timestamp: Date.now(),
});
}
/**
* Save current state for hot reload
*/
saveState(states: Map<string, Record<string, unknown>>): void {
this.stateSnapshot = {
objectStates: new Map(states),
timestamp: Date.now(),
};
}
/**
* Restore state after hot reload
*/
restoreState(): StateSnapshot | null {
return this.stateSnapshot;
}
/**
* Clear state snapshot
*/
clearState(): void {
this.stateSnapshot = null;
}
/**
* Update dependency graph
*/
updateDependencies(objectName: string, dependencies: string[]): void {
this.dependencyGraph.set(objectName, new Set(dependencies));
}
/**
* Get objects that depend on a given object
*/
getDependents(objectName: string): string[] {
const dependents: string[] = [];
for (const [name, deps] of this.dependencyGraph) {
if (deps.has(objectName)) {
dependents.push(name);
}
}
return dependents;
}
/**
* Get all objects that need recompilation due to a change
* Enhanced with trait-aware dependency tracking
*/
getRecompilationSet(changedObjects: string[]): Set<string> {
const toRecompile = new Set<string>(changedObjects);
// Add all dependents transitively (object dependencies)
const queue = [...changedObjects];
while (queue.length > 0) {
const current = queue.shift()!;
const dependents = this.getDependents(current);
for (const dep of dependents) {
if (!toRecompile.has(dep)) {
toRecompile.add(dep);
queue.push(dep);
}
}
}
// Also use trait graph for template inheritance tracking
const traitRecompileSet = this.traitGraph.calculateRecompilationSet(changedObjects);
for (const obj of traitRecompileSet) {
toRecompile.add(obj);
}
return toRecompile;
}
/**
* Get trait changes for specific objects
* Used for fine-grained trait config change detection
*/
getTraitChanges(objectName: string, newTraits: TraitUsage[]): TraitChangeInfo[] {
return this.traitGraph.detectTraitChanges(objectName, newTraits);
}
/**
* Get the trait dependency graph for external access
*/
getTraitGraph(): TraitDependencyGraph {
return this.traitGraph;
}
// ===========================================================================
// IMPORT RESOLUTION (Asset Pipeline)
// ===========================================================================
/**
* Resolve all `@import` directives in a `HSPlusCompileResult`, merge the
* imported exports into a scope, and register cross-file edges in the
* `TraitDependencyGraph` for incremental recompilation.
*
* This method is **async** because it may read files from disk (or any
* injected async reader).
*
* ### Typical usage
* ```ts
* const parser = new HoloScriptPlusParser({ enableTypeScriptImports: true });
* const result = parser.parse(source);
*
* const compiler = new IncrementalCompiler();
* const { importScope, importErrors } = await compiler.resolveImports(
* result,
* { baseDir: '/project', sourceFile: '/project/scene.hs' }
* );
* ```
*
* @param result Parse result that may contain `ast.imports`
* @param options Must include `baseDir` (and optionally `sourceFile`, `readFile`)
* @returns `{ scope, modules, errors }` from `ImportResolver.resolve()`
*/
async resolveImports(
result: {
ast?: {
imports?: Array<{
path: string;
alias: string;
namedImports?: string[];
isWildcard?: boolean;
}>;
};
},
options: Pick<IncrementalCompileOptions, 'baseDir' | 'sourceFile' | 'readFile'>
): Promise<ImportResolutionResult> {
const baseDir = options.baseDir ?? '/';
const sourceFile = options.sourceFile ?? `${baseDir}/unknown.hs`;
const resolver = new ImportResolver();
const resolution = await resolver.resolve(result as HSPlusCompileResult, sourceFile, {
baseDir,
readFile: options.readFile,
});
// ── Register cross-file import edges in TraitDependencyGraph ─────────────
// This enables getFilesAffectedByChange() to propagate recompilation when
// an imported file changes.
const imports = (result.ast?.imports ?? []) as Array<{ path: string }>;
this.traitGraph.clearImportsForFile(sourceFile);
for (const imp of imports) {
const canonicalPath = resolveImportPath(imp.path, baseDir);
this.traitGraph.registerImport(sourceFile, canonicalPath);
}
return resolution;
}
// ===========================================================================
// RBAC ENFORCEMENT
// ===========================================================================
/**
* Validate agent has permission for incremental compilation.
* Skips validation when no token is provided (backwards compatibility / testing).
*/
private validateAccess(agentToken?: string): void {
if (!agentToken) return;
const rbac = getRBAC();
const decision = rbac.checkAccess({
token: agentToken,
resourceType: ResourceType.AST,
operation: 'read',
expectedWorkflowStep: WorkflowStep.GENERATE_ASSEMBLY,
});
if (!decision.allowed) {
throw new UnauthorizedIncrementalCompilerAccessError(decision, 'IncrementalCompiler');
}
}
/**
* Perform incremental compilation
*/
async compile(
newAST: HoloComposition,
compileObject: (obj: HoloObjectDecl) => string,
options: IncrementalCompileOptions = {}
): Promise<IncrementalCompileResult> {
const { preserveState = true, forceRecompile = [], skipUnchanged = true, agentToken } = options;
// ─── Agent Identity Verification ───────────────────────────────────────
this.validateAccess(agentToken);
// ───────────────────────────────────────────────────────────────────────
// Diff with previous AST
const diffResult = this.diff(this.previousAST, newAST);
// Determine what needs recompilation
const changedObjects = [
...diffResult.addedObjects,
...diffResult.modifiedObjects,
...forceRecompile,
];
const recompileSet = this.getRecompilationSet(changedObjects);
// Also need to recompile objects with removed dependencies
for (const removed of diffResult.removedObjects) {
const dependents = this.getDependents(removed);
for (const dep of dependents) {
recompileSet.add(dep);
}
}
const recompiledObjects: string[] = [];
const cachedObjects: string[] = [];
const compiledParts: string[] = [];
// Compile each object
const allObjects = new Map<string, HoloObjectDecl>();
this.collectObjects(newAST, allObjects);
for (const [name, obj] of allObjects) {
const hash = hashContent(serializeObject(obj));
// SHA-256 provenance hash for content-addressable cross-session caching (paper-10 §3.2)
const provenanceHash = computeContentHash(serializeObject(obj));
// Register object with trait graph for dependency tracking
const traitUsages = this.extractTraitUsages(obj.traits || []);
this.traitGraph.registerObject({
objectName: name,
sourceId: newAST.name || 'default',
traits: traitUsages,
template: (obj as Extensible<HoloObjectDecl>).template as string | undefined,
});
// Provenance-keyed persistent cache: check FIRST, superseding AST diff (paper-10 §3.2).
// Same content hash means same compiled output regardless of session or diff result.
if (this.buildCache) {
const provResult = await this.buildCache.getByProvenanceHash<string>(
provenanceHash,
'compiled'
);
if (provResult.hit) {
cachedObjects.push(name);
compiledParts.push(provResult.entry!.data);
continue;
}
}
if (skipUnchanged && !recompileSet.has(name)) {
// Try semantic cache (Redis-backed, if enabled)
if (this.semanticCache) {
const astHash = hashASTSubtree(obj);
const semanticResult = await this.semanticCache.get<string>(astHash, 'compiled-object');
if (semanticResult.hit) {
cachedObjects.push(name);
compiledParts.push(semanticResult.entry!.data);
continue;
}
}
// Fallback to in-memory cache
const cached = this.getCached(name, hash);
if (cached) {
cachedObjects.push(name);
compiledParts.push(cached.compiledCode);
continue;
}
}
// Compile the object
const compiled = compileObject(obj);
recompiledObjects.push(name);
compiledParts.push(compiled);
// Update in-memory cache
this.setCached(name, hash, compiled, []);
// Persist to provenance-keyed build cache (paper-10: survives across sessions)
if (this.buildCache) {
await this.buildCache.setByProvenanceHash(provenanceHash, 'compiled', compiled, {
tags: ['incremental-compiler', newAST.name || 'unknown'],
});
}
// Update semantic cache (if enabled)
if (this.semanticCache) {
const astHash = hashASTSubtree(obj);
await this.semanticCache.set(astHash, 'compiled-object', compiled, {
sourcePath: newAST.name,
dependencies: [],
});
}
}
// Update previous AST
this.previousAST = newAST;
return {
fullRecompile: cachedObjects.length === 0,
recompiledObjects,
cachedObjects,
statePreserved: preserveState && this.stateSnapshot !== null,
compiledCode: compiledParts.join('\n\n'),
changes: diffResult,
};
}
/**
* Reset compiler state
*/
reset(): void {
this.cache.clear();
this.previousAST = null;
this.stateSnapshot = null;
this.dependencyGraph.clear();
}
/**
* Get cache statistics including trait graph info
*/
async getStats(): Promise<{
cacheSize: number;
objectsCached: string[];
dependencyEdges: number;
traitGraphStats: {
traitCount: number;
objectCount: number;
sourceCount: number;
dependencyEdges: number;
};
semanticCache?: SemanticCacheStats;
}> {
let dependencyEdges = 0;
for (const deps of this.dependencyGraph.values()) {
dependencyEdges += deps.size;
}
const stats = {
cacheSize: this.cache.size,
objectsCached: Array.from(this.cache.keys()),
dependencyEdges,
traitGraphStats: this.traitGraph.getStats(),
};
// Add semantic cache stats if enabled
if (this.semanticCache) {
const semanticStats = await this.semanticCache.getStats();
return {
...stats,
semanticCache: semanticStats,
};
}
return stats;
}
/**
* Get semantic cache statistics
*/
async getSemanticCacheStats(): Promise<SemanticCacheStats | null> {
if (!this.semanticCache) {