Type @ at a word start to complete lane-mail lanes (@@ passes through to the stock menu)

A Claude Code mod (a plugin with a hooks module; needs Claude Code 2.1.287 or newer) that makes @ at the prompt complete lane-mail lanes: @fs, @docs, @fs:otherhost.
@ typed at a word start (start of the box, or after whitespace) opens a picker in the band above the prompt. A mid-word @ (an email address) is left alone. While the picker is open the box shows a full-width @ and what you have typed after it (@al), then one space with the cursor before it (Claude Code sends no Ctrl-F for a cursor that cannot move, and the cursor would otherwise sit at the box's end); the space is removed again whenever the picker closes and is never sent. The mark is not an ASCII @ because that opens Claude Code's own menu over the picker; and the box must not be empty, because Claude Code sends no Enter or Backspace to a mod for an empty one.@lane:host).@ was, followed by a space. It does not send the prompt. With no match, the @ and what you typed are put back as plain text (also not sent).@ and what you typed become a plain @ and text (and Claude Code's own menu may open on it).@@: a second @ right after the first closes the picker and lets a single @ through, so Claude Code's own agent/file menu opens as usual.@ is not swallowed at all.lane-mail.sh send --from @<own lane> --to @<lane> --subject ... --body -). Whether to send, or only to refer to the lane, is Claude's call from your wording. The mod never sends anything itself. A mentioned lane that has a description (lane-mail.sh describe) also gets a line in that note, @lane owns: <description>, so the model does not guess its scope from the name; a lane without one adds nothing.A lane on another host, such as @builder:hostb, is offered once it has sent this host a message. bin/list-lanes.sh reads the headers of every message in the local thread store ($LANE_MAIL_ROOT/threads/) and takes each From: @lane:host address. Picking one inserts the full @lane:host, and a prompt that mentions it gets the same lane-mail note as a local lane.
Limits:
From: counts. A To: or Cc: address (a mistyped one nobody answered, say) is never offered, and neither is an address quoted in a message body: only the header block, up to the first blank line, is read.@lane:host (or pick the peer-qualified form of a local lane name).Claude Code hands a mod only the keys the editor takes as an edit (letters, Backspace, Left/Right, ...). Tab, Esc, Up and Down never reach it, so the picker uses none of them; Enter reaches it as a submit.
| Key | In the picker |
|---|---|
letters, digits, - . : | filter |
| Ctrl-F | highlight the next match (down the band); stops at the last |
| Ctrl-B | highlight the previous match (up the band); stops at the first |
| Enter | insert the highlighted match (the band's > row); nothing is sent |
| Backspace | trim the filter; on an empty filter, drop the @ |
@ (on an empty filter) | @@: close, pass one @ to the stock menu |
| space or other punctuation, a cursor move | not a lane: cancel, keeping what was typed |
Ctrl-F and Ctrl-B are the keys because they are the ones that reach a mod (Up, Down, Tab, Ctrl-N/P do not); while the picker is open they never move the cursor or change the box, and with it closed they are the editor's own cursor-forward/back. Typing or trimming the filter puts the highlight back on the top match; typing more of the name (@fs: narrows to the cross-host forms) is the other way to reach a lane. The cursor ends at the end of the prompt after an Enter, wherever the @ was.
claude --plugin-dir mods/lane-complete
The lane list is read in the background every few seconds by bin/list-lanes.sh (never per keystroke), which takes the lane-mail root, peers file and registry directory from the lane-mail scripts. It looks for them in this checkout's scripts/ first, then wherever lane-mail.sh is on PATH.
claude plugin validate mods/lane-complete
claude plugin test mods/lane-complete
tests/lane-complete-list-lanes-test.sh
The picker logic is in hooks/lanes.ts and has no engine in it; hooks/register.tsx wires it to prompt.edit, prompt.submit and the band.
hooks/register.tsx 124 lines1import { atom, read, update } from 'claude-code'
2import type { EngineInterface, Register } from 'claude-code'
3
4import type { LanePicker } from '../types'
5import { VISIBLE, finish, highlighted, matchLanes, mentionContext, mentions, parseLanes, plain, shown, step } from './lanes'
6import type { Lane } from './lanes'
7
8// How often the lane list is re-read, in the background; never per keystroke.
9const REFRESH_MS = 5000
10
11const picker = atom({ plugin: 'lane-complete', key: 'picker' } as const, null as LanePicker | null)
12
13// The lane list the hooks read: refreshed by a timer, so never read per
14// keystroke. A reload starts it empty and session.start fills it again; a
15// failed read keeps the last good list (a missed refresh is not worth a toast).
16let lanes: Lane[] = []
17
18// After a pick the engine may put the dropped draft back over our fill. For
19// this long a write of the engine's that leaves the box other than the pick is
20// written over again; an edit of the person's ends it sooner.
21const PICK_HOLD_MS = 1000
22let picked: string | null = null
23
24async function refresh($: EngineInterface) {
25 try {
26 const ran = await $.process.run(['bash', `${$.plugin.root}/bin/list-lanes.sh`], { timeoutMs: 5000 })
27 if (ran.exitCode === 0) lanes = parseLanes(ran.stdout)
28 } catch {
29 // keep the last list
30 }
31}
32
33export const register: Register = on => {
34 on('session.start', async ($, e, next) => {
35 await refresh($)
36 $.clock.every(REFRESH_MS, () => refresh($))
37
38 return next(e)
39 })
40
41 // Every edit goes through the picker. Closed, it passes all but a word-start
42 // `@`; open, it owns the edits until Backspace-on-empty, a cursor move or a
43 // non-lane character closes it, or Enter takes the choice (below). The text
44 // itself is edited here, in the answer, so nothing depends on the band
45 // taking keyboard focus. Only edits arrive here: Enter, Tab, Esc and
46 // Up/Down never do.
47 on('prompt.edit', async ($, e, next) => {
48 picked = null
49 const open = await read($, picker)
50 if (open === null && !(e.inputText === '@' && e.start === e.end)) return next(e)
51
52 const result = step(open, lanes, e)
53 if (result.picker !== open) await update($, picker, () => result.picker)
54
55 if (result.replay !== undefined) return next({ ...e, ...result.replay })
56 return result.box ?? next(e)
57 })
58
59 // Enter with the picker open is the choice, not a send: the submit is
60 // dropped and the box put back with the best match (or, with none, as it
61 // stands, `@` and filter in it as plain text) in place of the `@` and what was typed.
62 // Otherwise a submit never rewrites the text: it only adds a note for the
63 // model when the prompt names a lane.
64 on('prompt.submit', async ($, e, next) => {
65 const open = await read($, picker)
66 if (open !== null) {
67 await update($, picker, () => null)
68 if (e.text.trim() === shown(open).text.trim()) {
69 // The cursor ends at the box's end: fill has no way to place it.
70 const text = finish(open, lanes).text
71 await $.prompt.fill({ text, mode: 'replace' })
72 picked = text
73 $.clock.after(PICK_HOLD_MS, () => {
74 if (picked === text) picked = null
75 })
76 return { drop: 'lane picked' }
77 }
78 // A box that is not the one shown was changed behind the picker: send it
79 // as it is, with a real `@` for the mark and without the space we added.
80 e = { ...e, text: plain(open, e.text).text }
81 }
82
83 const named = mentions(e.text, lanes)
84 if (named.length === 0) return next(e)
85
86 return next({ ...e, context: [...(e.context ?? []), mentionContext(named, lanes)] })
87 }).catch(($, e, next) => next(e)) // a failed note never costs the operator the prompt
88
89 on('prompt.change', async ($, e, next) => {
90 const r = await next(e)
91 if (picked !== null && e.origin.kind !== 'plugin' && e.text !== picked) {
92 await $.prompt.fill({ text: picked, mode: 'replace' })
93 }
94 return r
95 })
96
97 on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
98 const open = await read($, picker)
99 if (open === null) return next(e)
100
101 const { Box, Text } = $.ui.resolve(e)
102 const matches = matchLanes(lanes, open.filter)
103 const shown = matches.slice(0, VISIBLE)
104 const current = highlighted(open, lanes)
105
106 return (
107 <Box flexDirection="column">
108 <Text bold>
109 lane @{open.filter}
110 <Text dimColor> ({matches.length} of {lanes.length})</Text>
111 </Text>
112 {shown.length === 0 && <Text dimColor> no lane matches; Enter keeps @{open.filter} as typed</Text>}
113 {shown.map((lane, i) => (
114 <Text key={lane.address} bold={i === current} color={i === current ? 'cyan' : undefined} dimColor={lane.source === 'peer' && i !== current}>
115 {i === current ? '> ' : ' '}
116 {lane.address}
117 </Text>
118 ))}
119 <Text dimColor>Enter inserts the marked row · C-f next, C-b previous · keep typing to narrow · Backspace trims · space cancels · @ again: stock menu</Text>
120 </Box>
121 )
122 })
123}
124hooks/lanes.ts 281 lines1// The lane-complete logic with no engine in it: parsing the lane list,
2// filtering it, stepping the picker on one prompt.edit, and finding lane
3// mentions in a submitted prompt. register.tsx wires these to the hooks; the
4// tests drive them directly (prompt.edit has no call on the test's `$`).
5
6import type { LanePicker } from '../types'
7
8export type Lane = {
9 /** The lane-mail address as it is typed: `@lane` or `@lane:host`. */
10 address: string
11 source: 'live' | 'mailbox' | 'remote' | 'peer'
12 /** What the lane owns, one line (`lane-mail.sh describe`); local lanes only. */
13 description?: string
14}
15
16/** The part of a prompt.edit input the picker reads. */
17export type Edit = {
18 key?: { key: string; ctrl?: true; shift?: true; meta?: true }
19 text: string
20 cursor: number
21 start: number
22 end: number
23 inputText: string
24}
25
26/** What one edit did: the picker after it, and who answers the edit. */
27export type Step = {
28 picker: LanePicker | null
29 /** Set: answer the edit with this box and never call `next` (the key is ours). */
30 box?: { text: string; cursor: number }
31 /** Set: pass the edit on to `next` as if typed into this box (the `@` of an `@@`). */
32 replay?: { text: string; cursor: number; start: number; end: number }
33}
34
35/** How many matches the band shows at once. */
36export const VISIBLE = 6
37
38const NAME = /^[a-z0-9][a-z0-9.-]*$/
39const REMOTE = /^[a-z0-9][a-z0-9-]*:[a-z0-9][a-z0-9.-]*$/
40const FILTER_CHARS = /^[A-Za-z0-9:.-]+$/
41
42/**
43 * Parses list-lanes.sh output (`<source>\t<name>[\t<description>]` lines) into addresses:
44 * live lanes, then mailbox-only lanes, then remote lanes that have written
45 * here (`lane:host`), then each local lane qualified with each peer host (the
46 * peers file names hosts, not their lanes, and a lane is usually run on both
47 * ends of a link).
48 */
49export function parseLanes(stdout: string): Lane[] {
50 const live: string[] = []
51 const mailbox: string[] = []
52 const remote: string[] = []
53 const peers: string[] = []
54 const described = new Map<string, string>()
55 for (const line of stdout.split('\n')) {
56 const [source, name, description] = line.split('\t')
57 if (name === undefined) continue
58 if (source === 'remote') {
59 if (REMOTE.test(name)) remote.push(name)
60 continue
61 }
62 if (!NAME.test(name)) continue
63 if ((source === 'live' || source === 'mailbox') && description && !described.has(name)) {
64 described.set(name, description)
65 }
66 if (source === 'live') live.push(name)
67 else if (source === 'mailbox') mailbox.push(name)
68 else if (source === 'peer') peers.push(name)
69 }
70 const lanes: Lane[] = []
71 const seen = new Set<string>()
72 const add = (address: string, source: Lane['source']) => {
73 if (seen.has(address)) return
74 seen.add(address)
75 const description = source === 'live' || source === 'mailbox' ? described.get(address.slice(1)) : undefined
76 lanes.push(description === undefined ? { address, source } : { address, source, description })
77 }
78 for (const name of live) add(`@${name}`, 'live')
79 for (const name of mailbox) add(`@${name}`, 'mailbox')
80 for (const name of remote) add(`@${name}`, 'remote')
81 for (const name of [...live, ...mailbox]) {
82 for (const peer of peers) add(`@${name}:${peer}`, 'peer')
83 }
84 return lanes
85}
86
87/** The lanes a typed filter leaves: prefix matches first, then substring ones. */
88export function matchLanes(lanes: readonly Lane[], filter: string): Lane[] {
89 const f = filter.toLowerCase()
90 const prefix: Lane[] = []
91 const inside: Lane[] = []
92 for (const lane of lanes) {
93 const name = lane.address.slice(1)
94 if (name.startsWith(f)) prefix.push(lane)
95 else if (name.includes(f)) inside.push(lane)
96 }
97 return [...prefix, ...inside]
98}
99
100function splice(text: string, at: number, insert: string) {
101 return { text: text.slice(0, at) + insert + text.slice(at), cursor: at + insert.length }
102}
103
104/** A `@` typed here would be a mention, not the tail of an email address. */
105function isWordStart(text: string, at: number): boolean {
106 return at === 0 || /\s/.test(text.charAt(at - 1))
107}
108
109/** The row the picker has highlighted, kept inside the visible matches. */
110export function highlighted(p: LanePicker, lanes: readonly Lane[], by = 0): number {
111 const rows = Math.min(matchLanes(lanes, p.filter).length, VISIBLE)
112 return Math.max(0, Math.min((p.index ?? 0) + by, rows - 1))
113}
114
115/** The box with the picker's choice at its anchor, in place of the `@` and filter; one space after it unless one follows. */
116function accept(p: LanePicker, address: string) {
117 const at = Math.min(p.anchor, p.text.length)
118 const space = /\s/.test(p.text.charAt(at)) ? '' : ' '
119 return splice(p.text, at, `${address}${space}`)
120}
121
122/**
123 * What stands in the box for the `@` while the picker is open: a full-width
124 * commercial at, which looks like one but is not one to Claude Code's own
125 * `@` menu (an ASCII `@` in the box opens that menu over the picker).
126 */
127export const MARK = '\uFF20'
128
129/**
130 * The box while the picker is open: the mark and what was typed after it sit
131 * in the text at the anchor, so the box is never empty (Claude Code fires no
132 * submit and no Backspace edit for an empty one) and the operator sees what
133 * they typed. `p.text` is the box without them.
134 *
135 * One space follows the filter, with the cursor before it: the editor emits
136 * no edit for a cursor-forward that cannot move (the cursor at the box's end,
137 * where it sits after a start-of-box `@al`), and C-f must always arrive. The
138 * space is ours alone: `plain` takes it out again on every way out.
139 */
140export function shown(p: LanePicker) {
141 const at = Math.min(p.anchor, p.text.length)
142 return { text: splice(p.text, at, `${MARK}${p.filter} `).text, cursor: at + 1 + p.filter.length }
143}
144
145/** Where `shown` puts its space (the cursor sits here). */
146function padAt(p: LanePicker): number {
147 return Math.min(p.anchor, p.text.length) + 1 + p.filter.length
148}
149
150/**
151 * A box as `shown` made it, as plain text: a real `@` where the mark is and
152 * the space taken out, which is what any way out of the picker leaves. A
153 * position in the shown box maps to `at(i)` here. A box that is not quite the
154 * shown one (changed behind the picker) loses only the parts that are there.
155 */
156export function plain(p: LanePicker, text: string) {
157 const anchor = Math.min(p.anchor, text.length)
158 const pad = padAt(p)
159 const marked = text.charAt(anchor) === MARK
160 const unmarked = marked ? `${text.slice(0, anchor)}@${text.slice(anchor + 1)}` : text
161 const padded = marked && text.charAt(pad) === ' '
162 return {
163 text: padded ? unmarked.slice(0, pad) + unmarked.slice(pad + 1) : unmarked,
164 at: (i: number) => (padded && i > pad ? i - 1 : i),
165 }
166}
167
168/**
169 * The box an Enter pressed with the picker open leaves: the highlighted match
170 * in place of the `@` and filter, or with none the `@` and filter as plain text.
171 * The caller drops the submit and puts this box back.
172 */
173export function finish(p: LanePicker, lanes: readonly Lane[]) {
174 const best = matchLanes(lanes, p.filter)[highlighted(p, lanes)]
175 return best === undefined ? { text: plain(p, shown(p).text).text, cursor: shown(p).cursor } : accept(p, best.address)
176}
177
178/**
179 * One prompt.edit through the picker. `picker` is null while closed. A closed
180 * picker opens on a swallowed word-start `@` (only when there are lanes to
181 * pick: with none, the `@` goes on to the stock menu), answering with the box
182 * that has the `@` in it; an open one swallows every edit until it closes.
183 *
184 * Only keys the editor takes as an edit reach prompt.edit: Enter, Tab, Esc and
185 * the arrows Up/Down never do, so none of them is handled here. Enter arrives
186 * as a prompt.submit and is answered by `finish`.
187 */
188export function step(picker: LanePicker | null, lanes: readonly Lane[], e: Edit): Step {
189 // A picker whose box is not the one shown is stale (the box was cleared or
190 // changed behind it): forget it and treat this edit as one with no picker.
191 if (picker !== null && shown(picker).text !== e.text) picker = null
192
193 if (picker === null) {
194 const isAt = e.inputText === '@' && e.start === e.end && isWordStart(e.text, e.start)
195 if (!isAt || lanes.length === 0) return { picker: null }
196 const opened = { anchor: e.start, text: e.text, filter: '' }
197 return { picker: opened, box: shown(opened) }
198 }
199
200 const key = e.key
201 const name = key !== undefined && key.key.length > 1 ? key.key : undefined
202 const isMod = key !== undefined && (key.ctrl === true || key.meta === true)
203 const open = (next: LanePicker): Step => ({ picker: next, box: shown(next) })
204 // Closed with the box as the edit leaves it, `@` and filter kept as plain text.
205 const close = (box: { text: string; cursor: number }): Step => ({ picker: null, box })
206
207 // Backspace trims the filter; on an empty one it undoes the `@` itself.
208 const isBackspace = name === 'backspace' || (name === undefined && e.inputText === '' && e.end > e.start && !isMod)
209 if (isBackspace) {
210 if (picker.filter === '') return close({ text: picker.text, cursor: Math.min(picker.anchor, picker.text.length) })
211 return open({ anchor: picker.anchor, text: picker.text, filter: picker.filter.slice(0, -1) })
212 }
213
214 // C-f / C-b are the editor's cursor-forward/back, which would cancel the
215 // picker; here they move the highlight (next / previous), clamped at both
216 // ends, and the box is answered as shown so the cursor never moves.
217 if (key !== undefined && key.ctrl === true && key.meta !== true && (key.key === 'f' || key.key === 'b')) {
218 return open({ ...picker, index: highlighted({ ...picker, index: highlighted(picker, lanes) }, lanes, key.key === 'f' ? 1 : -1) })
219 }
220
221 // `@@`: a second `@` right after the swallowed one goes on to the stock
222 // menu, as the one `@` it would have been: the edit is replayed on the box
223 // without ours.
224 if (e.inputText === '@' && picker.filter === '') {
225 const at = Math.min(picker.anchor, picker.text.length)
226 return { picker: null, replay: { text: picker.text, cursor: at, start: at, end: at } }
227 }
228
229 if (name === undefined && !isMod && FILTER_CHARS.test(e.inputText)) {
230 return open({ anchor: picker.anchor, text: picker.text, filter: picker.filter + e.inputText.toLowerCase() })
231 }
232
233 // Anything else (a space, punctuation, a paste of prose, a cursor move, an
234 // unknown key) is not a lane: the `@` and what was typed stay as plain text,
235 // with the character typed after them.
236 const kept = plain(picker, e.text)
237 if (name === undefined && !isMod && e.inputText !== '') {
238 const start = kept.at(e.start)
239 const text = kept.text.slice(0, start) + e.inputText + kept.text.slice(kept.at(e.end))
240 return close({ text, cursor: start + e.inputText.length })
241 }
242 return close({ text: kept.text, cursor: kept.at(e.cursor) })
243}
244
245/**
246 * The known lanes a submitted prompt mentions, in order of first mention.
247 * A mention is a whole `@lane` or `@lane:host` word (a trailing sentence
248 * mark is allowed) that is in the lane list: an `@file.ts` or an email
249 * address is never one.
250 */
251export function mentions(text: string, lanes: readonly Lane[]): string[] {
252 const known = new Set(lanes.map(l => l.address))
253 const found: string[] = []
254 const re = /(?:^|\s)(@[a-z0-9][a-z0-9.-]*(?::[a-z0-9][a-z0-9.-]*)?)(?=[.,;:!?)\]"']*(?:\s|$))/g
255 for (const m of text.matchAll(re)) {
256 // The pattern is greedy over dots and a sentence-final one is not part of the name.
257 const address = (m[1] ?? '').replace(/[.]+$/, '')
258 if (known.has(address) && !found.includes(address)) found.push(address)
259 }
260 return found
261}
262
263/** The note the model reads beside a prompt that mentions lanes. */
264export function mentionContext(addresses: readonly string[], lanes: readonly Lane[] = []): string {
265 const list = addresses.join(', ')
266 // A lane's name invites a guess at what it owns; its description says.
267 const owns = addresses.flatMap(a => {
268 const description = lanes.find(l => l.address === a)?.description
269 return description === undefined ? [] : [`${a} owns: ${description}`]
270 })
271 return [
272 `The operator's prompt mentions lane-mail ${addresses.length === 1 ? 'lane' : 'lanes'}: ${list}.`,
273 'Those are lane-mail addresses (an agent session\'s mailbox), not files or agents.',
274 'A lane can be messaged with:',
275 ' lane-mail.sh send --from @<your own lane> --to @<lane> --subject ... --body -',
276 '(message body on stdin; `lane-mail.sh registration` prints your own lane; `@lane:host` is a lane on another host).',
277 'Decide from the prompt\'s wording whether the operator wants a message sent or is only referring to the lane; send nothing unless they asked.',
278 ...owns,
279 ].join('\n')
280}
281types/index.d.ts 18 lines1// The mod's $.state contract: the lane picker, null while it is closed.
2export type LanePicker = {
3 /** Where the picked address goes: the cursor when the picker opened. */
4 anchor: number
5 /** The box text the picker opened on, without the `@` and filter it shows in the box; a box other than the one shown means it is stale. */
6 text: string
7 /** What the operator has typed since the swallowed `@`; Enter takes the highlighted match of it. */
8 filter: string
9 /** The highlighted row among the visible matches (C-f down, C-b up); absent is the top one, and any change of the filter puts it back there. */
10 index?: number
11}
12
13declare module 'claude-code' {
14 interface PluginState {
15 'lane-complete': { picker: LanePicker | null }
16 }
17}
18