"""Compose unchanged installed nanobot AgentLoop and caller-owned native ToolRegistry.

This is a task-owned SDK caller, not a patch to nanobot or a claim about CLI options.
Preflight creates the same full agent without requesting any model generation.
"""
import asyncio,hashlib,json,sys
from pathlib import Path
from nanobot.agent.loop import AgentLoop
from nanobot.agent.tools.registry import ToolRegistry
from nanobot.config.loader import load_config,set_config_path
from nanobot.cron.service import CronService

config_path=Path(sys.argv[2]);folder=Path(sys.argv[3]);set_config_path(config_path);config=load_config(config_path)
write=lambda name,value:(folder/name).write_text(json.dumps(value,ensure_ascii=False,indent=2)+'\n',encoding='utf-8')
sha=lambda p:hashlib.sha256(p.read_bytes()).hexdigest()
async def main():
 registry=ToolRegistry();agent=AgentLoop.from_config(config,tool_registry=registry,cron_service=CronService(config.runtime_data_dir/'cron/jobs.json'))
 discovered=registry.tool_names.copy()
 for name in discovered:registry.unregister(name)
 assert registry is agent.tools and len(agent.tools)==0 and not agent.tools.get_definitions()
 write('effective-native-sdk.json',{'mode':'Native Python AgentLoop SDK composition, not stock CLI generation','registeredBeforeRemoval':discovered,'callerOwnedNativeRegistryUnregistered':discovered,'effectiveCallableTools':len(registry),'providerClass':type(agent.provider).__name__,'maxIterations':agent.max_iterations,'configSha256':sha(config_path),'unchangedInstalledProduct':True,'modelCallsBeforeRun':0})
 try:
  if sys.argv[1].startswith('preflight'):return
  assert sys.argv[1]=='run'
  input_text=(folder/'native-prompt.txt').read_text(encoding='utf-8')
  response=await agent.process_direct(input_text,session_key='cli:synthetic-first',channel='cli',chat_id='synthetic-first',ephemeral=True,persist_user_message=False)
  assert response is not None
  native_content=response.content
  (folder/'first-assistant.txt').write_text(native_content,encoding='utf-8')
  write('native-outbound-metadata.json',{'contentSha256':sha(folder/'first-assistant.txt'),'metadata':response.metadata,'channel':response.channel,'chatId':response.chat_id,'registeredToolsAfter':registry.tool_names})
  sys.stdout.write(native_content)
 finally:await agent.aclose()
asyncio.run(main())
