Revision: 1 Last modified: 2026-12-04T19:00:00Z
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.
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. |
#!/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)"| 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. |
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.
al_initfails (state unresolvable, disabled). The adapter exits0silently — fail-soft.al_upsertfor 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_pidfinds nothing. Prints nothing; the caller must handle the empty case rather than substituting a guess.LABEL_SYNC_AGENT_ALIASunset. 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.
- On source, defines the functions; no side effects.
al_initresolves the state dir and checks whether labelling is enabled.al_upsertbuilds the entity payload from agent/session/cwd/model/effort/state (plus optionalPARENTandKIND) and asserts it against the shared bus — an upsert, so it is idempotent.al_donemarks the entity finished.al_entity_id/al_find_pidare pure lookups that print their answer or nothing.
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.
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).