Skip to content

Latest commit

 

History

History
118 lines (86 loc) · 5.23 KB

adapter_lib.md

File metadata and controls

118 lines (86 loc) · 5.23 KB

adapters/lib/adapter_lib.sh

Revision: 1 Last modified: 2026-12-04T19:00:00Z

Overview

This is a SOURCE-ONLY library. It defines functions for the non-Claude CLI-agent adapters and is never executed directly.

It is the shared translation layer between agent lifecycle events and the label bus. Every non-Claude adapter — kimi, opencode, crush, aider, codex, gemini, qwen, and the generic wrapper — sources this file. Each per-agent script's only job is to translate its own native hook payload into these functions; all the label logic lives here once.

The result is that every agent, on every platform, shows a live (T<N>/<branch> - <alias> - <model> - <effort>) label against the shared bus (docs/label-sync/CONTRACT.md).

The reason to centralise is the one this repository keeps running into: near-forked adapters drift. Eight copies of "how do I assert an entity is running" would become eight slightly different answers, and the one that mattered would be the wrong one.

Prerequisites

  • bash, jq.
  • Sourced from an adapter, resolved relative to the sourcing script.
  • No environment variables are required, but see the table below for the ones that change behaviour.

Environment variables (names only):

Variable Meaning
LABEL_SYNC_DIR State dir (CONTRACT §1; tests point it at a temp dir).
LABEL_SYNC_AGENT_ALIAS The alias to show (default: the agent name).
LABEL_SYNC_FOLLOW_ACTIVE 1 = follow the operator's active alias instead of the pinned one.

Usage examples

#!/usr/bin/env bash
. "$(dirname "${BASH_SOURCE[0]}")/../lib/adapter_lib.sh"
al_init || exit 0

# agent started / is working
al_upsert kimi "$session_id" "$PWD" "$model" "$effort" running

# agent finished
al_done kimi "$session_id"

# helpers
entity="$(al_entity_id kimi "$session_id")"
pid="$(al_find_pid kimi)"

Exported surface

Function Contract
al_init Initialises adapter state. Returns non-zero when the adapter must exit 0 silently — mirroring the hook fail-soft contract.
al_upsert AGENT SESSION CWD MODEL EFFORT STATE [PARENT] [KIND] Level-triggered assertion that the entity exists with this state. Upsert, not insert: re-asserting an existing entity refreshes it.
al_done AGENT SESSION Marks the entity done/unregistered.
al_entity_id AGENT SESSION Prints the entity id for that agent/session pair.
al_find_pid AGENT Prints the agent's pid, or nothing when it cannot be found.

Exit codes

As a library it returns rather than exiting. The one status a caller must honour is al_init's: non-zero means exit 0 silently — an adapter must never break the agent it is instrumenting.

al_find_pid deliberately prints nothing rather than a placeholder when no pid is found. An absent pid is a fact; inventing one would attach a label to the wrong process.

Edge cases

  • al_init fails (state unresolvable, disabled). The adapter exits 0 silently — fail-soft.
  • al_upsert for an entity that does not exist yet. It is created (upsert), which is what makes the adapters robust to a missed start event.
  • al_find_pid finds nothing. Prints nothing; the caller must handle the empty case rather than substituting a guess.
  • LABEL_SYNC_AGENT_ALIAS unset. The agent's own name is used as the alias.
  • LABEL_SYNC_FOLLOW_ACTIVE=1. The operator's active alias is followed rather than the pinned value — the opposite of the Claude Code hook's pinning behaviour, and deliberate: a non-Claude agent usually has no per-process alias to pin to.
  • A label bus that is down. The upsert cannot land; the adapter stays silent rather than failing the agent.
  • An unusual/invalid STATE. The bus owns validation; an invalid state should be corrected at the adapter's own translation layer, not silently coerced here.

Internal behaviour

  1. On source, defines the functions; no side effects.
  2. al_init resolves the state dir and checks whether labelling is enabled.
  3. al_upsert builds the entity payload from agent/session/cwd/model/effort/state (plus optional PARENT and KIND) and asserts it against the shared bus — an upsert, so it is idempotent.
  4. al_done marks the entity finished.
  5. al_entity_id / al_find_pid are pure lookups that print their answer or nothing.

Related scripts

Sourced by every non-Claude adapter: scripts/label-sync/adapters/kimi/kimi-hook.sh, adapters/opencode/*, adapters/crush/crush-hook.sh, adapters/aider/aider-notify.sh and aider-labelled.sh, adapters/codex/codex-notify.sh, adapters/gemini/gemini-hook.sh, adapters/qwen/qwen-hook.sh, adapters/generic/label-agent-run.sh.

Sibling entry for adapters that cannot source bash: scripts/label-sync/adapters/lib/label-adapter-event.sh.

Wired by scripts/label-sync/adapters/install_adapters.sh.

Cross-reference: docs/label-sync/CONTRACT.md.

See also docs/scripts/label-adapter-event.md, docs/scripts/install_adapters.md and docs/scripts/label-sync-lib.md.

Last verified

2026-12-04 — bash -n scripts/label-sync/adapters/lib/adapter_lib.sh → exit 0. Sourcing it in a bash shell and running type al_upsert prints the function definition (source succeeds, nothing executes).