Repository navigation
Expand file tree
/
Copy pathnstack.py
More file actions
executable file
·376 lines (355 loc) · 25.9 KB
/
Copy pathnstack.py
File metadata and controls
executable file
·376 lines (355 loc) · 25.9 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
#!/usr/bin/env python3
"""Native-first project capability registry and content-addressed publisher."""
from __future__ import annotations
import argparse,contextlib,fcntl,hashlib,json,os,pathlib,re,shutil,subprocess,sys,tempfile,time
P=pathlib.Path
ROOT=P(__file__).resolve().parent
STATE=P(os.environ.get('NSTACK_STATE',P.home()/'Library/Application Support/nstack'))
def load(path):return json.loads(P(path).read_text())
def dump(path,data):
path=P(path);path.parent.mkdir(parents=True,exist_ok=True)
raw=(json.dumps(data,sort_keys=True,indent=2,ensure_ascii=False)+'\n').encode()
if path.exists() and path.read_bytes()==raw:return
fd,name=tempfile.mkstemp(prefix='.'+path.name,dir=path.parent)
try:
with os.fdopen(fd,'wb') as f:f.write(raw);f.flush();os.fsync(f.fileno())
os.replace(name,path)
finally:
if os.path.exists(name):os.unlink(name)
def sha(data):return hashlib.sha256(data).hexdigest()
def manifest():return load(ROOT/'registry/manifest.json')
def component(id):
for row in manifest()['components']:
if row['id']==id:return row
raise ValueError('No admitted component '+id+'. Use nstack search TERMS --brief to find an ID; excluded and consultation-only entries cannot be shown as admitted components.')
def leaf(id,path):
for row in manifest()['skills']:
if row['candidate_id']==id and row['source_path']==path:return row
raise ValueError('No selected leaf at these coordinates')
def safe_relative(name):
p=P(name)
if p.is_absolute() or '..' in p.parts:raise ValueError('Unsafe relative path')
return p
@contextlib.contextmanager
def lock(path):
path.parent.mkdir(parents=True,exist_ok=True)
if path.is_symlink():raise ValueError('Linked lock file')
fd=os.open(path,os.O_CREAT|os.O_APPEND|os.O_WRONLY|getattr(os,'O_NOFOLLOW',0),0o600)
with os.fdopen(fd,'a') as f:
fcntl.flock(f,fcntl.LOCK_EX);yield
@contextlib.contextmanager
def read_lock(path):
"""Readers need no write grant to the commissioned runtime registry."""
fd=os.open(path,os.O_RDONLY|getattr(os,'O_NOFOLLOW',0))
with os.fdopen(fd,'r') as f:
fcntl.flock(f,fcntl.LOCK_SH);yield
def tree(path):
result={}
for f in sorted(path.rglob('*')):
if f.is_symlink():raise ValueError('Release contains a symlink')
if f.is_file() and f!=path/'.nstack-release.json':result[str(f.relative_to(path))]=sha(f.read_bytes())
return result
def verify_release(path):
receipt=load(path/'.nstack-release.json')
if tree(path)!=receipt['files']:raise ValueError('Release tampered: '+str(path))
if sha(json.dumps(receipt['files'],sort_keys=True).encode())!=receipt['content_sha256']:raise ValueError('Invalid release identity')
return receipt
def check_source(dest):
info=load(dest/'.nstack-source.json')
if not info.get('files'):raise ValueError('Source cache requires verified file hashes: '+str(dest))
files={str(f.relative_to(dest)):sha(f.read_bytes()) for f in dest.rglob('*') if f.is_file() and f!=dest/'.nstack-source.json'}
if files!=info['files']:raise ValueError('Source cache changed: '+str(dest))
return dest
def acquire(repo,rev):
if not isinstance(repo,str) or not isinstance(rev,str) or not re.fullmatch(r'[\w.-]+/[\w.-]+',repo) or not re.fullmatch(r'[0-9a-f]{40}',rev):raise ValueError('A reviewed full Git commit and GitHub repository are required')
dest=STATE/'sources'/repo.replace('/','--')/rev
if (dest/'.nstack-source.json').exists():return check_source(dest)
dest.parent.mkdir(parents=True,exist_ok=True)
with lock(dest.parent/'.fetch.lock'):
if (dest/'.nstack-source.json').exists():return check_source(dest)
with tempfile.TemporaryDirectory(prefix='.fetch-',dir=dest.parent) as tmp:
tmp=P(tmp);git=tmp/'git';out=tmp/'source';out.mkdir()
subprocess.run(['git','init','--bare','-q',str(git)],check=True)
subprocess.run(['git','-C',str(git),'fetch','-q','--depth=1','https://github.com/'+repo+'.git',rev],check=True)
actual=subprocess.check_output(['git','-C',str(git),'rev-parse','FETCH_HEAD'],text=True).strip()
if actual!=rev:raise ValueError('Source revision mismatch')
entries=subprocess.check_output(['git','-C',str(git),'ls-tree','-rz',rev])
batch=subprocess.Popen(['git','-C',str(git),'cat-file','--batch'],stdin=subprocess.PIPE,stdout=subprocess.PIPE)
try:
for record in entries.split(b'\0'):
if not record:continue
meta,name=record.split(b'\t',1);mode,kind,blob=meta.split();rel=safe_relative(os.fsdecode(name))
if kind!=b'blob' or mode==b'120000':continue
batch.stdin.write(blob+b'\n');batch.stdin.flush();header=batch.stdout.readline().split()
if len(header)!=3 or header[:2]!=[blob,b'blob']:raise ValueError('Git source blob identity/type differs')
length=int(header[2]);raw=batch.stdout.read(length)
if len(raw)!=length or batch.stdout.read(1)!=b'\n':raise ValueError('Truncated Git source blob')
f=out/rel;f.parent.mkdir(parents=True,exist_ok=True);f.write_bytes(raw);f.chmod(0o755 if mode==b'100755' else 0o644)
batch.stdin.close()
if batch.wait()!=0:raise ValueError('Git source extraction failed')
finally:
if batch.poll() is None:batch.terminate();batch.wait()
dump(out/'.nstack-source.json',{'repository':repo,'revision':rev,'git_verified':True,'files':{str(f.relative_to(out)):sha(f.read_bytes()) for f in out.rglob('*') if f.is_file()}})
os.rename(out,dest)
return dest
def shelf_name_from_row(id,path,repo,selected):
raw=selected.get('native_name') or P(path).parent.name or repo.split('/')[-1]
return id.lower()+'-'+re.sub(r'[^a-z0-9-]+','-',raw.lower()).strip('-')
def shelf_name(id,path,repo,*,catalog=None):
"""Reuse a caller-owned operation snapshot; default lookups stay current."""
selected=next((s for s in (manifest() if catalog is None else catalog)['skills'] if (s['candidate_id'],s['source_path'],s['source_repository'])==(id,path,repo)),{})
return shelf_name_from_row(id,path,repo,selected)
def builder_identity():
paths=[ROOT/'nstack.py',ROOT/'lib/skill_build.py',ROOT/'lib/academic_addon.py',ROOT/'lib/publish_scope.py',ROOT/'lib/claim_evidence.py',ROOT/'lib/closure.py',ROOT/'lib/voltage_check.py',ROOT/'lib/gh_comments.py',ROOT/'lib/unreal_two_shot_probe.py',ROOT/'lib/gp_delivery_guard.py',ROOT/'lib/unreal_animation_probe.py']+sorted((ROOT/'lib').glob('patch_*.py'))
# Readable English assets are build inputs, including text-only corrections.
paths+=sorted(p for p in (ROOT/'ports/english').rglob('*') if p.is_file())
paths+=[ROOT/'addons/agent-reach/SOURCE.json']+sorted((ROOT/'addons/agent-reach/references').glob('*.md'))
paths+=sorted(p for p in (ROOT/'addons/agent-reach/ENGLISH-PORT').rglob('*') if p.is_file())
return sha(json.dumps({str(p.relative_to(ROOT)):sha(p.read_bytes()) for p in paths},sort_keys=True).encode())
def build(id,path):
c=component(id);s=leaf(id,path)
if c['deployment']=='consult' or s['delivery'] not in ('intact upstream leaf','narrow-patched leaf','native runtime/method adapter') or (P(path).name!='SKILL.md' and not s.get('native_name')):raise ValueError('This entry must remain a reference, support file, link or entitled surface')
source=acquire(s['source_repository'],s['revision']);source_leaf=source/safe_relative(path).parent
if not (source/safe_relative(path)).is_file():raise ValueError('Pinned source entry is missing')
# The explicit builder defines lawful closure and exact patches. No generic "append override" patch.
sys.path.insert(0,str(ROOT/'lib'))
from skill_build import materialize
pub=STATE/'build/skills'/id;pub.mkdir(parents=True,exist_ok=True)
with lock(pub/'.build.lock'):
with tempfile.TemporaryDirectory(prefix='.stage-',dir=pub) as tmp:
out=P(tmp)/'release';out.mkdir();support={}
if id=='P001' and P(path).parent.name=='codebase-design':
for ref in manifest()['skills']:
if ref['candidate_id']==id and ref['delivery']=='selective-merge reference':support[ref['source_repository']]=acquire(ref['source_repository'],ref['revision'])
entry_dir=materialize(source,out,s,c,manifest(),support) or '.'
from patch_english import apply as english_port
english_port(source,out,s)
for f in out.rglob('*'):
if f.is_symlink():raise ValueError('Linked closure is not allowed')
files=tree(out);digest=sha(json.dumps(files,sort_keys=True).encode());name=shelf_name(id,path,s['source_repository'])
receipt={'entry_dir':entry_dir,'builder_sha256':builder_identity(),'candidate_id':id,'source_repository':s['source_repository'],'revision':s['revision'],'source_path':path,'delivery':s['delivery'],'deployment':c['deployment'],'name':name,'files':files,'content_sha256':digest,'manifest_sha256':sha((ROOT/'registry/manifest.json').read_bytes())}
dump(out/'.nstack-release.json',receipt);dest=pub/sha(json.dumps(receipt,sort_keys=True).encode())
if dest.exists():verify_release(dest)
else:
for f in out.rglob('*'):f.chmod(0o555 if f.is_dir() or f.stat().st_mode & 0o111 else 0o444)
os.rename(out,dest);dest.chmod(0o555)
dump(STATE/'publisher-index'/dest.name,{'receipt_sha256':sha((dest/'.nstack-release.json').read_bytes()),'release':str(dest)})
return dest
def registered_release(release):
release=P(release).resolve(strict=True);r=verify_release(release)
if r.get('builder_sha256')!=builder_identity():raise ValueError('Superseded build recipe; rebuild from the reviewed current patch set')
c=component(r['candidate_id']);s=leaf(c['id'],r['source_path'])
for field in ('source_repository','revision','delivery'):
if r.get(field)!=s[field]:raise ValueError('Release source tuple differs from registry')
if r.get('deployment')!=c['deployment'] or r.get('manifest_sha256')!=sha((ROOT/'registry/manifest.json').read_bytes()):raise ValueError('Stale release manifest')
sys.path.insert(0,str(ROOT/'lib'));from closure import needs
from patch_native34 import handles as handles34,entry as entry34
expected_entry=entry34(s) if handles34(s) else str(P(s['source_path']).parent) if needs(s) or (c['id']=='P343' and P(s['source_path']).parent.name in ('prospecting','cold-email')) else '.'
if r.get('entry_dir','.')!=expected_entry:raise ValueError('Unexpected entry directory for this selected leaf')
name=shelf_name(c['id'],s['source_path'],s['source_repository'])
if r.get('name')!=name or not re.fullmatch(r'[a-z0-9][a-z0-9-]{0,127}',name):raise ValueError('Invalid effective shelf name')
expected=STATE/'build/skills'/c['id']/release.name
if release!=expected.resolve() or not release.is_relative_to((STATE/'build/skills').resolve()):raise ValueError('Release outside publisher build root')
index=STATE/'publisher-index'/release.name
if not index.is_file() or index.is_symlink():raise ValueError('Release lacks publisher provenance')
seal=load(index)
if seal!={'receipt_sha256':sha((release/'.nstack-release.json').read_bytes()),'release':str(release)}:raise ValueError('Publisher provenance changed')
return r,c,s
def project_paths(project):
project=P(project).resolve(strict=True)
if not project.is_dir() or project==P.home().resolve() or STATE.resolve().is_relative_to(project) or project.is_relative_to(STATE.resolve()) or any(project.is_relative_to(P.home()/root) for root in ('.codex','.agents')):raise ValueError('A bounded project outside the private state and home root is required')
for rel in ('.nstack','.agents','.agents/skills'):
p=project/rel
if p.resolve()!=p:raise ValueError('Linked project shelf or journal')
for rel in ('.nstack/activations.json','.nstack/publish.lock'):
if (project/rel).is_symlink():raise ValueError('Linked project ownership record')
return project
def journal_paths(project):
st=project.stat();identity={'project':str(project),'device':st.st_dev,'inode':st.st_ino}
root=STATE/'project-owners'/sha(json.dumps(identity,sort_keys=True).encode())
return root,identity
def activation_rows(project):
root,identity=journal_paths(project);file=root/'activations.json'
if file.is_symlink():raise ValueError('Linked authoritative journal')
if not file.exists():return {}
record=load(file)
if record.get('identity')!=identity:raise ValueError('Project publisher identity differs')
return record['entries']
def save_activations(project,entries):
root,identity=journal_paths(project)
dump(root/'activations.json',{'identity':identity,'entries':entries})
# Do not write a privileged mirror through a worker-writable directory.
# `verify` returns inspection JSON for the worker to save in its own scope.
def publish(project,release,gate=None):
project=project_paths(project);release=P(release).resolve(strict=True);r,c,s=registered_release(release)
if c['deployment']=='consult':raise ValueError('Consult entries cannot be activated')
if c['deployment'] in ('trial','substitute'):
raise ValueError(c['deployment']+' remains locked until its per-ID acceptance verifier is implemented; a generic gate cannot authorize it')
target=release/safe_relative(r.get('entry_dir','.'))
shelf=project/'.agents/skills'
if shelf.resolve()!=shelf:raise ValueError('Project shelf or parent is linked')
shelf.mkdir(parents=True,exist_ok=True);dest=shelf/r['name'];record=project/'.nstack/activations.json'
owner,_=journal_paths(project)
with lock(owner/'publish.lock'):
entries=activation_rows(project)
if dest.is_symlink() and dest.resolve()==target:
prior=entries.get(r['name'])
if not prior or prior.get('release')!=str(release) or prior.get('content_sha256')!=r['content_sha256']:raise ValueError('Matching link has no matching ownership journal')
if prior.get('phase')!='active':
prior['phase']='active';save_activations(project,entries)
return str(dest)
if dest.exists() or dest.is_symlink():raise ValueError('Shelf destination already exists; deactivate its recorded release first')
# Write-ahead journal survives interruption between journal and link.
entries[r['name']]={'release':str(release),'content_sha256':r['content_sha256'],'target':str(target),'phase':'prepared'};save_activations(project,entries)
dest.symlink_to(target,target_is_directory=True)
entries[r['name']]['phase']='active';save_activations(project,entries)
return str(dest)
def deactivate(project,name):
if not re.fullmatch(r'[a-z0-9][a-z0-9-]{0,127}',name):raise ValueError('Invalid shelf name')
project=project_paths(project);record=project/'.nstack/activations.json';shelf=project/'.agents/skills';dest=shelf/name
owner,_=journal_paths(project)
with lock(owner/'publish.lock'):
entries=activation_rows(project);r=entries.get(name)
if r is None:raise ValueError('No nstack ownership record for '+name)
if dest.is_symlink() and str(dest.resolve())==r.get('target',r['release']):dest.unlink()
elif dest.exists() or dest.is_symlink():raise ValueError('Refusing to remove a changed or unowned shelf entry')
del entries[name];save_activations(project,entries)
def verify(project):
project=project_paths(project);rows=activation_rows(project)
for name,r in rows.items():
if not re.fullmatch(r'[a-z0-9][a-z0-9-]{0,127}',name):raise ValueError('Invalid journal name')
release=P(r['release']);actual,_,_=registered_release(release)
if r.get('phase')!='active' or r.get('content_sha256')!=actual['content_sha256']:raise ValueError('Unreconciled activation or digest mismatch')
dest=project/'.agents/skills'/name
if not dest.is_symlink() or dest.resolve()!=(release/safe_relative(actual.get('entry_dir','.'))):raise ValueError('Incomplete activation: '+name)
return {'project':str(project),'active':list(rows),'verified':True}
def native_selection(catalog):
"""Select native leaves from one caller-owned registry snapshot."""
sys.path.insert(0,str(ROOT/'lib'))
import skill_names
modes={c['id']:c['deployment'] for c in catalog['components']};rows=[];first={}
allowed=('intact upstream leaf','narrow-patched leaf','native runtime/method adapter')
for row in catalog['skills']:
id=row['candidate_id'];path=row['source_path'];repo=row['source_repository']
selected=first.setdefault((id,path,repo),row)
if modes[id]!='on demand' or row['delivery'] not in allowed:continue
name=shelf_name_from_row(id,path,repo,selected)
prefix=id.lower()+'-'
if not name.startswith(prefix):raise ValueError('Unexpected source skill name')
rows.append((skill_names.canonical(name[len(prefix):]),row))
if len({name for name,_ in rows})!=len(rows):raise ValueError('Natural skill names collide')
return rows
def discovery_names():
"""Catalog names, not evidence of installed state; references are excluded."""
names={}
for name,row in native_selection(manifest()):
names.setdefault(row['candidate_id'],[]).append(name)
return names
def main():
if sys.argv[1:2]==['selections']:
from lib.selected_catalog import main as selections_main
return selections_main(sys.argv[2:])
from lib import review_packets
sys.path.insert(0,str(ROOT/'addons/01/scripts'))
import project_find
p=argparse.ArgumentParser(description='Find reviewed capabilities and inspect their routes before use.',epilog='Start read-only: nstack search web-ui-design --brief, then nstack show COMPONENT_ID. Search and show do not install or activate anything.');sub=p.add_subparsers(dest='cmd',required=True,metavar='COMMAND')
sub.add_parser('selections', help='Inspect portable scope and pinned sources for selected methods')
project_find.arguments(sub.add_parser('find',help='Search eligible project skill instructions and references'))
review_packets.add_arguments(sub.add_parser('review', help='Pin, read and verify local Elephant–Goldfish review packets'))
a=sub.add_parser('search',help='Find catalog entries by task, source or selected skill name',description='Search reviewed catalog entries. Results are JSON; selected names describe available catalog leaves, not installed skills.')
a.add_argument('terms',nargs='+',help='Pass distinctive words separately, or quote a phrase to match it literally; entries matching more terms rank first');a.add_argument('--all',action='store_true',help='Include excluded and consultation-only entries');a.add_argument('--limit',type=int,default=12,help='Maximum results (1–535; default 12)');a.add_argument('--brief',action='store_true',help='Show component ID, summary, admission and selected native names')
a=sub.add_parser('show',help='Inspect an admitted component, its paths and prerequisites');a.add_argument('id',help='Component ID from search, for example P001')
a=sub.add_parser('route',help='Inspect source, prerequisites and actions for an admitted component');a.add_argument('id',help='Component ID from search')
a=sub.add_parser('inventory',help='Inspect project and native tool availability');a.add_argument('project',nargs='?',default=str(P.cwd()));a.add_argument('--binary');a.add_argument('--plugins',action='store_true')
a=sub.add_parser('workspace',help='Prepare an isolated source workspace');a.add_argument('id');a.add_argument('project');a.add_argument('--fresh',action='store_true')
a=sub.add_parser('retire-workspace',help='Retire an owned workspace while preserving its bytes');a.add_argument('id');a.add_argument('project')
a=sub.add_parser('source',help='Fetch a pinned admitted source into private state');a.add_argument('id')
a=sub.add_parser('build',help='Build a selected skill from pinned source');a.add_argument('id');a.add_argument('path')
a=sub.add_parser('activate',help='Activate a built release in a bounded project');a.add_argument('project');a.add_argument('release');a.add_argument('--gate')
a=sub.add_parser('deactivate',help='Remove an owned project activation');a.add_argument('project');a.add_argument('name')
a=sub.add_parser('verify',help='Verify project activation ownership and release integrity');a.add_argument('project')
a=sub.add_parser('publisher',help='Open the project publisher interface');a.add_argument('project')
a=sub.add_parser('project-init',help='Configure an optional project integration');a.add_argument('project');a.add_argument('--trust',action='store_true')
a=sub.add_parser('project-remove',help='Remove owned project integration');a.add_argument('project')
for command in ['commission','run','check','remove-runtime']:
a=sub.add_parser(command,help={'commission':'Prepare a selected runtime', 'run':'Run a commissioned operation', 'check':'Check a commissioned operation', 'remove-runtime':'Remove an owned runtime'}[command]);a.add_argument('id');a.add_argument('operation');
if command=='run':a.add_argument('arguments',nargs=argparse.REMAINDER)
a=sub.add_parser('doctor',help='Show registry, state and executable locations')
a.add_argument('--runtime-paths',action='store_true',help='Check recorded absolute invocation paths without starting any runtime')
a=sub.add_parser('knowledge',help='Read or update project-scoped knowledge');a.add_argument('action',choices=['set','read','delete']);a.add_argument('project');a.add_argument('id');a.add_argument('--claim');a.add_argument('--source');a.add_argument('--expected-generation',type=int,default=0)
args=p.parse_args()
try:
if args.cmd=='review':return review_packets.command(args)
if args.cmd=='find':return project_find.run(args,sys.modules[__name__])
if args.cmd=='search':
rows=load(ROOT/'registry/discovery.json');terms=[t.lower() for t in args.terms]
names=discovery_names()
result=[]
for r in rows:
text=(json.dumps(r,ensure_ascii=False)+' '+json.dumps(names.get(r['id'],[]),ensure_ascii=False)).lower();score=sum(term in text for term in terms)
if score and (args.all or r['admission'] not in ('excluded','consult')):result.append((score,r))
if not 1<=args.limit<=535:raise ValueError('Search limit must be between 1 and 535')
selected=[r for _,r in sorted(result,key=lambda x:-x[0])][:args.limit]
if args.brief:
selected=[{'id':r['id'],'summary':r['summary'],'admission':r['admission'],'selected_native_names':names.get(r['id'],[])} for r in selected]
if not selected:
if any(len(term.split()) > 1 for term in terms):
print('No matches for the literal phrase(s). Try broader terms as separate words. Example: nstack search keyboard layout accessibility --brief',file=sys.stderr)
else:
print('No matches. Try broader terms or --all for excluded and consultation-only entries.',file=sys.stderr)
print(json.dumps(selected,indent=2,ensure_ascii=not args.brief))
elif args.cmd=='show':
c=component(args.id);rows=[]
for file in (STATE/'publisher-index').glob('*'):
try:
seal=load(file);path=P(seal['release']);summary=load(path/'.nstack-release.json')
if summary.get('candidate_id')!=args.id:continue
r,_,_=registered_release(path)
if r['candidate_id']==args.id:rows.append({'path':r['source_path'],'release':seal['release'],'name':r['name']})
except (OSError,ValueError,KeyError):continue
routes=load(ROOT/'registry/routes.json') if (ROOT/'registry/routes.json').is_file() else {}
print(json.dumps({**c,'selected_paths':[s for s in manifest()['skills'] if s['candidate_id']==args.id],'staged_builds':rows,'operation_recipes':[load(p) for p in (ROOT/'recipes'/args.id).rglob('recipe.json')],'native_activation_route':routes.get(args.id)},indent=2))
elif args.cmd=='route':
component(args.id);print(json.dumps(load(ROOT/'registry/routes.json')[args.id],indent=2))
elif args.cmd=='inventory':
sys.path.insert(0,str(ROOT/'lib'));import native_inventory
print(json.dumps(native_inventory.inspect(args.project,args.binary,args.plugins),indent=2))
elif args.cmd in ('workspace','retire-workspace'):
sys.path.insert(0,str(ROOT/'lib'));import build_workspace
print(json.dumps(build_workspace.prepare(sys.modules[__name__],args.id,args.project,getattr(args,'fresh',False),args.cmd=='retire-workspace'),indent=2))
elif args.cmd=='source':
c=component(args.id)
if c['deployment']=='consult':raise ValueError('Consult only: follow read-only source locator')
if c['implementation_mode']=='official plugin' or not isinstance(c.get('revision'),str) or not re.fullmatch(r'[0-9a-f]{40}',c['revision']):raise ValueError('Use the registered official/platform locator and its entitlement/version procedure; no source checkout is authorized by this operation')
print(acquire(c['repository'],c['revision']))
elif args.cmd=='build':print(build(args.id,args.path))
elif args.cmd=='activate':print(publish(args.project,args.release,args.gate))
elif args.cmd=='deactivate':deactivate(args.project,args.name);print('Deactivated '+args.name)
elif args.cmd=='verify':print(json.dumps(verify(args.project)))
elif args.cmd=='publisher':
sys.path.insert(0,str(ROOT/'lib'));import project_publisher
project_publisher.serve(sys.modules[__name__],args.project)
elif args.cmd in ('project-init','project-remove'):
sys.path.insert(0,str(ROOT/'lib'));import project_setup
print(json.dumps(project_setup.configure(sys.modules[__name__],args.project,args.cmd=='project-remove',getattr(args,'trust',False)),indent=2))
elif args.cmd in ('commission','run','check','remove-runtime'):
sys.path.insert(0,str(ROOT/'lib'));import runtime
if args.cmd=='commission':print(json.dumps(runtime.commission(sys.modules[__name__],args.id,args.operation),indent=2))
elif args.cmd=='remove-runtime':print(json.dumps(runtime.remove(sys.modules[__name__],args.id,args.operation),indent=2))
elif args.cmd=='check':print(json.dumps(runtime.check(sys.modules[__name__],args.id,args.operation),indent=2))
else:runtime.invoke(sys.modules[__name__],args.id,args.operation,[a for a in args.arguments if a!='--'])
elif args.cmd=='knowledge':
sys.path.insert(0,str(ROOT/'lib'));import knowledge
if args.action=='read':result=knowledge.recall(args.project,args.id)
else:
if args.action=='set' and (not args.claim or not args.source):raise ValueError('set requires claim and source')
result=knowledge.change(args.project,args.id,args.claim,args.source,args.action=='delete',dump,args.expected_generation)
print(json.dumps(result,indent=2))
elif args.cmd=='doctor':
m=manifest();report={'registry':str(ROOT/'registry/manifest.json'),'manifest_version':m['version'],'components':len(m['components']),'paths':len(m['skills']),'decisions':len(load(ROOT/'registry/discovery.json')),'state':str(STATE),'codex':shutil.which('codex'),'git':shutil.which('git'),'python':sys.version.split()[0]}
if args.runtime_paths:
from lib.runtime_paths import audit
report['runtime_paths']=audit(STATE)
print(json.dumps(report,indent=2))
except (ValueError,OSError,subprocess.CalledProcessError) as e:p.exit(1,str(e)+'\n')
if __name__=='__main__':sys.exit(main())