"""THE executable local developmental program. Immutable constants + local update.
No imports, graph, planner, generator, evaluator, target, clock, or random handle.
Opaque handles support neighbors/parent/next hops; their spelling is never parsed.
"""
RESPONSE_TAG=8
def local_update(state, boundary, adjacent, delivered):
    kind,source,payload=delivered
    actions=[]
    if state is None:
        if kind!='grow': return state,actions
        state={'parent':source,'down':{},'store':None,'root':False,'response':False}
        for cue in boundary:
            if cue['kind']=='query': state['root']=True
            elif cue['kind']=='response': state['response']=True
            elif cue['kind']=='write': state['store']=cue['a']
        # All available neighboring bonds are alternatives, never forward offers.
        for neighbor,metric in adjacent:
            if neighbor!=source: actions.append(('send',neighbor,'grow',None))
        tag=state['store'] if state['store'] is not None else RESPONSE_TAG if state['response'] else None
        if tag is not None:
            state['down'][str(tag)]=None
            if source is not None: actions.append(('send',source,'register',tag))
    elif kind=='register':
        state['down'][str(payload)]=source
        if state['parent'] is not None: actions.append(('send',state['parent'],'register',payload))
    elif kind=='quiet':
        # Prune unused exploratory cells. Differentiation and routes are choices
        # of this program, not of the scheduler or a global validator.
        if state['root'] or state['down']:
            role='interface' if state['root'] else 'latch' if state['store'] is not None else 'router'
            routes={}
            for tag,next_hop in state['down'].items():
                if next_hop is not None:
                    routes['A' if int(tag)==RESPONSE_TAG else 'Q'+tag]=next_hop
            if state['parent'] is not None: routes['V']=state['parent']
            actions.append(('compile',dict(role=role,address=state['store'],response=state['response'],routes=routes)))
    return state,actions
