Buckets:
| """Source-derived components, including their callers and mutable-state closure. | |
| There are no task names, tensor-shape routes, preferred function names, grid | |
| schedules, algorithm stages, or component-quality models in this frontend. | |
| The entry name is the native evaluator's public interface. Function identities | |
| inside a lineage follow source bindings; unrelated same-shaped functions are | |
| not matched. Native execution remains the semantic compatibility test. | |
| """ | |
| from __future__ import annotations | |
| import ast,copy,hashlib,json,symtable,types | |
| from dataclasses import dataclass | |
| def sha(x):return hashlib.sha256(x.encode()).hexdigest() | |
| def normalized(node):return ast.dump(node,include_attributes=False) | |
| class Version: | |
| source_id:str | |
| symbol:str | |
| mode:str='closed' | |
| def key(self):return (self.source_id,self.symbol,self.mode) | |
| class Source: | |
| def __init__(self,key,text,entry): | |
| self.key=key;self.text=text;self.entry=entry;self.tree=ast.parse(text) | |
| self.functions={n.name:n for n in self.tree.body if isinstance(n,(ast.FunctionDef,ast.AsyncFunctionDef))} | |
| if entry not in self.functions:raise ValueError('source does not define the native entry') | |
| if any(isinstance(n,ast.AsyncFunctionDef)for n in self.functions.values()):raise ValueError('async requires an asynchronous native executor') | |
| self.abis={n:self.abi(f)for n,f in self.functions.items()} | |
| self.fingerprints={n:sha(normalized(f))for n,f in self.functions.items()} | |
| self.support_fingerprint=sha(repr([normalized(n)for n in self.tree.body if not isinstance(n,(ast.FunctionDef,ast.AsyncFunctionDef))])) | |
| names=set(self.functions);self.dependencies={};self.reads={};self.writes={} | |
| table=symtable.symtable(text,'<source>','exec') | |
| top_tables={t.get_name():t for t in table.get_children()} | |
| for name,fn in self.functions.items(): | |
| def global_reads(t): | |
| result={s.get_name()for s in t.get_symbols()if s.is_referenced()and s.is_global()} | |
| for child in t.get_children():result|=global_reads(child) | |
| return result | |
| read=global_reads(top_tables[name]) | |
| # Decorators, annotations and omitted defaults execute in module scope. | |
| head=copy.deepcopy(fn);head.body=[] | |
| read|={n.id for n in ast.walk(head)if isinstance(n,ast.Name)and isinstance(n.ctx,ast.Load)} | |
| self.reads[name]=read;self.dependencies[name]=read&names | |
| declared={v for n in ast.walk(fn)if isinstance(n,ast.Global)for v in n.names} | |
| write={n.id for n in ast.walk(fn)if isinstance(n,ast.Name)and isinstance(n.ctx,ast.Store)and n.id in declared} | |
| for n in ast.walk(fn): | |
| if isinstance(n,(ast.Assign,ast.AugAssign,ast.Delete)): | |
| targets=n.targets if isinstance(n,(ast.Assign,ast.Delete))else[n.target] | |
| for target in targets: | |
| while isinstance(target,(ast.Subscript,ast.Attribute)):target=target.value | |
| if isinstance(target,ast.Name)and target.id in read:write.add(target.id) | |
| if isinstance(n,ast.Call)and isinstance(n.func,ast.Attribute)and n.func.attr in {'append','extend','clear','update','pop','remove','add','discard','sort','reverse'}: | |
| value=n.func.value | |
| while isinstance(value,(ast.Attribute,ast.Subscript)):value=value.value | |
| if isinstance(value,ast.Name)and value.id in read:write.add(value.id) | |
| self.writes[name]=write | |
| self.reachable=self.closure(entry) | |
| # State-coupled functions are a source-derived atomic ownership group. | |
| links={n:set(self.dependencies[n])for n in names} | |
| for name in names: | |
| for state in self.writes[name]: | |
| users={n for n in names if state in self.reads[n]or state in self.writes[n]} | |
| for a in users:links[a]|=users-{a} | |
| self.links=links;self.state_groups=self.scc(links) | |
| self.group_for={name:group for group in self.state_groups for name in group} | |
| self.closed_fingerprints={name:sha(repr((self.support_fingerprint,[(n,self.fingerprints[n])for n in sorted(self.closure(name))])))for name in names} | |
| self.binding_identities={};self.controls={};self.binding_requires={} | |
| for node in self.tree.body: | |
| if isinstance(node,ast.Assign)and any(isinstance(t,ast.Name)and t.id=='__source_binding_identities__'for t in node.targets):self.binding_identities=ast.literal_eval(node.value) | |
| if isinstance(node,ast.Assign)and any(isinstance(t,ast.Name)and t.id=='__source_control_domains__'for t in node.targets):self.controls=ast.literal_eval(node.value) | |
| if isinstance(node,ast.Assign)and any(isinstance(t,ast.Name)and t.id=='__source_binding_requires__'for t in node.targets):self.binding_requires=ast.literal_eval(node.value) | |
| def abi(fn): | |
| a=fn.args | |
| # Names are needed for keyword arguments; default VALUES stay owned by | |
| # the implementation, but their omission convention must be compatible. | |
| return (tuple(x.arg for x in a.posonlyargs),tuple(x.arg for x in a.args),a.vararg.arg if a.vararg else None,tuple(x.arg for x in a.kwonlyargs),a.kwarg.arg if a.kwarg else None,len(a.defaults),tuple(x is None for x in a.kw_defaults)) | |
| def scc(graph): | |
| index={};low={};stack=[];on=set();groups=[] | |
| def visit(v): | |
| index[v]=low[v]=len(index);stack.append(v);on.add(v) | |
| for w in graph[v]: | |
| if w not in index:visit(w);low[v]=min(low[v],low[w]) | |
| elif w in on:low[v]=min(low[v],index[w]) | |
| if low[v]==index[v]: | |
| group=[] | |
| while True: | |
| w=stack.pop();on.remove(w);group.append(w) | |
| if w==v:break | |
| groups.append(tuple(sorted(group))) | |
| for v in graph: | |
| if v not in index:visit(v) | |
| return groups | |
| def closure(self,name): | |
| seen=set();todo=[name] | |
| while todo: | |
| n=todo.pop() | |
| if n in seen:continue | |
| seen.add(n);todo.extend(self.dependencies[n]-seen) | |
| return seen | |
| def statement_count(self,name):return len(self.tree.body)if name=='@module'else len(self.functions[name].body) | |
| def global_fingerprint(self,name): | |
| if name=='_sg_controls':return sha(repr({k:{n:v for n,v in d.items()if n!='initial'}for k,d in self.controls.items()})) | |
| return self.binding_identities.get(name,self.closed_fingerprints.get(name,self.support_fingerprint)) | |
| class Family: | |
| def __init__(self,sources:dict[str,str],entry:str,base:str): | |
| if base not in sources:raise ValueError('unknown base source') | |
| self.entry=entry;self.base=base;self.sources={k:Source(k,v,entry)for k,v in sources.items()} | |
| abi=self.sources[base].abis[entry] | |
| if any(s.abis[entry]!=abi for s in self.sources.values()):raise ValueError('native entry interfaces differ') | |
| self.ids=tuple(sources) | |
| self.source_hashes={k:sha(v)for k,v in sources.items()} | |
| def initial(self,source=None):return dict(root=source or self.base,patches={}) | |
| def active_ports(self,config): | |
| root=self.sources[config['root']];patches=config.get('patches',{});seen=set();todo=[self.entry] | |
| while todo: | |
| name=todo.pop() | |
| if name in seen:continue | |
| seen.add(name) | |
| if name in patches: | |
| provider,mode=patches[name] | |
| if mode=='closed':continue | |
| donor=self.sources[provider] | |
| todo.extend(n for n in donor.closure(name)-{name}if n in root.functions and donor.abis[n]==root.abis[n]) | |
| else:todo.extend(root.dependencies[name]-seen) | |
| return seen | |
| def canonical(self,config): | |
| root=config['root'];source=self.sources[root];patches={};active=self.active_ports(config) | |
| for name,value in sorted(config.get('patches',{}).items()): | |
| provider,mode=value | |
| if mode not in ['shared','closed']:raise ValueError('unknown ownership mode') | |
| if name==self.entry:raise ValueError('entry is selected as the caller source') | |
| donor=self.sources[provider] | |
| if name not in source.functions or name not in donor.functions:raise ValueError('port absent in this caller') | |
| if source.abis[name]!=donor.abis[name]:raise ValueError('source ABI differs') | |
| for group in donor.state_groups: | |
| if name in group: | |
| for peer in group: | |
| if peer in active and peer!=name and (peer not in config.get('patches',{})or config['patches'][peer]!=[provider,mode]): | |
| raise ValueError('mutable-state/recursive closure must move together') | |
| if name in active:patches[name]=[provider,mode] | |
| controls={} | |
| for name,domain in source.controls.items(): | |
| value=list(config.get('controls',{}).get(name,domain['initial'])) | |
| if domain['kind']!='simplex':raise ValueError('unknown declared source control domain') | |
| if any(x<0 for x in value)or abs(sum(value)-1)>1e-7 or sum(x>1e-8 for x in value)>domain.get('max_active',len(value)):raise ValueError('source control outside its original feasible domain') | |
| controls[name]=value | |
| return dict(root=root,patches=patches,controls=controls) | |
| def identity(self,config): | |
| c=self.canonical(config) | |
| parts={name:[self.sources[p].closed_fingerprints[name]if mode=='closed'else[self.sources[p].support_fingerprint,self.sources[p].fingerprints[name]],mode]for name,(p,mode)in c['patches'].items()} | |
| return sha(json.dumps(dict(root=self.source_hashes[c['root']],patches=parts,controls=c['controls']),sort_keys=True)) | |
| def proposals(self,config): | |
| """Single closed regions and source-coherent shared dependency rewrites.""" | |
| c=self.canonical(config);receiver=self.sources[c['root']];seen={self.identity(c)} | |
| def emit(value): | |
| value['controls']=copy.deepcopy(c['controls']) | |
| try: | |
| v=self.canonical(value);key=self.identity(v) | |
| except ValueError:return None | |
| if key in seen:return None | |
| seen.add(key);return v | |
| for provider,donor in self.sources.items(): | |
| if provider==c['root']:continue | |
| for name in sorted(self.active_ports(c)-{self.entry}): | |
| if name.startswith('_sg_'):continue | |
| if name not in donor.functions or receiver.abis[name]!=donor.abis[name]:continue | |
| if receiver.closed_fingerprints[name]==donor.closed_fingerprints[name]:continue | |
| p=dict(c['patches']) | |
| for member in donor.group_for[name]: | |
| if member!=self.entry and member in receiver.functions and receiver.abis[member]==donor.abis[member]:p[member]=[provider,'closed'] | |
| v=emit(dict(root=c['root'],patches=p)) | |
| if v is not None:yield v | |
| # Include the changed caller/callee closure as one transaction. | |
| group=donor.closure(name)&receiver.functions.keys();group-={self.entry} | |
| group|={member for n in tuple(group)for member in donor.group_for[n]if member in receiver.functions and member!=self.entry} | |
| p=dict(c['patches']) | |
| for member in group: | |
| if receiver.abis[member]==donor.abis[member]:p[member]=[provider,'shared'] | |
| v=emit(dict(root=c['root'],patches=p)) | |
| if v is not None:yield v | |
| # Retain provenance-supported joint changes, including disjoint helpers | |
| # co-designed in one actual source, without enumerating all products. | |
| for provider,donor in self.sources.items(): | |
| p=dict(c['patches']) | |
| for name in self.active_ports(c)-{self.entry}: | |
| if name.startswith('_sg_'):continue | |
| if name in donor.functions and receiver.abis[name]==donor.abis[name]and receiver.fingerprints[name]!=donor.fingerprints[name]:p[name]=[provider,'shared'] | |
| v=emit(dict(root=c['root'],patches=p)) | |
| if v is not None:yield v | |
| def emit(self,config): | |
| """Standalone package-free Python, preserving versioned lexical state. | |
| Definitions and ordinary top-level initialization execute in their own | |
| provider namespaces. Only the evaluator-defined context is injected. | |
| Closed regions retain their private callees; shared groups explicitly | |
| rebind the selected dependency graph. Calls/loops are ordinary source. | |
| """ | |
| c=self.canonical(config);used={c['root']}|{p for p,_ in c['patches'].values()}|{p for p,_ in c.get('regions',{}).values()};payload={k:self.sources[k].text for k in self.ids if k in used} | |
| data=repr(dict(sources=payload,config=c,entry=self.entry)) | |
| groups={p:{n:':'.join(self.sources[p].group_for[n])for n in self.sources[p].functions}for p in payload} | |
| abis={p:self.sources[p].abis for p in payload} | |
| data=repr(dict(sources=payload,config=c,entry=self.entry,groups=groups,abis=abis)) | |
| return '''import types as _sg_types, sys as _sg_sys, linecache as _sg_linecache | |
| _sg_spec='''+data+''' | |
| def build(_source_context=None): | |
| _context={}if _source_context is None else dict(_source_context) | |
| _context['_sg_controls']=_sg_spec['config']['controls'] | |
| _instances={} | |
| def instance(provider,owner): | |
| key=(provider,owner) | |
| if key not in _instances: | |
| name='_source_owner_'+str(id(_instances))+'_'+str(len(_instances)) | |
| module=_sg_types.ModuleType(name);ns=module.__dict__;ns.update(_context) | |
| filename=name+'.py';ns['__file__']=filename | |
| text=_sg_spec['sources'][provider] | |
| _sg_linecache.cache[filename]=(len(text),None,text.splitlines(True),filename) | |
| _sg_sys.modules[name]=module | |
| exec(compile(text,filename,'exec'),ns) | |
| _instances[key]=ns | |
| return _instances[key] | |
| root=instance(_sg_spec['config']['root'],'root') | |
| selected={} | |
| for name,(provider,mode)in _sg_spec['config']['patches'].items(): | |
| ns=instance(provider,mode+':'+(provider if mode=='shared'else _sg_spec['groups'][provider][name])) | |
| selected[name]=ns[name] | |
| root.update(selected) | |
| for (provider,owner),ns in _instances.items(): | |
| if owner.startswith('shared:'): | |
| for name,abi in _sg_spec['abis'][_sg_spec['config']['root']].items(): | |
| if name in _sg_spec['abis'][provider]and tuple(abi)==tuple(_sg_spec['abis'][provider][name]):ns[name]=root[name] | |
| return root[_sg_spec['entry']] | |
| ''' | |
| def manifest(self): | |
| return dict(entry=self.entry,source_hashes=self.source_hashes, | |
| source_graphs={k:dict(functions=sorted(s.functions),reachable=sorted(s.reachable),dependencies={n:sorted(v)for n,v in s.dependencies.items()},state_groups=s.state_groups,binding_identities=s.binding_identities,binding_dependencies=s.binding_requires,source_control_domains=s.controls)for k,s in self.sources.items()},task_specific_stage_labels=False,caller_and_control_flow_preserved=True) | |
Xet Storage Details
- Size:
- 15.3 kB
- Xet hash:
- c0dc2effcdab8676cf1ae4e54a0aa3756ef6651b8288bd1dce9632f597648241
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.