AFKSwitch by augbastos — Tell your agents when you're away and when you're back.

<img src="assets/readme/hero-light.png" alt="AFKSwitch — away and back presence for your agents">
AFKSwitch by augbastos — Tell your agents when you're away and when you're back.
AFKSwitch is a presence plugin for coding agents: mark yourself away, then return to collect status where the host supports it. Presence is not permission.
Authorized work continues while you're away; steps that need you wait. In Claude Code, returning collects status with your blockers first, followed by completed work.
Full support in Claude Code; Codex and Antigravity are state-only. See compatibility.
For Claude Code:
/plugin marketplace add augbastos/afkswitch
/plugin install afkswitch@afkswitch
/reload-plugins
Needs python3 (Python 3.9+) on PATH; the terminal switch needs Claude Code function hooks. AFKSwitch is a Claude Code Mod, live-tested on 2.1.287 on Windows. The compatibility record distinguishes local-marketplace module loading from live clicks and peer notification.
If your build does not load installed mods, use the local clone. See switch setup and behavior for details.
[ AFK □■ ] you're here
[ AFK ■□ ] you're away
<img src="assets/readme/afkswitch-demo.gif" alt="Real Claude Code capture: present, one click sets AFK and tells the other session, the other session keeps working and reports, one click back">
/afk / /back.Capabilities from the adapter matrix:
| Host | State | Sync | Notify peers | Collect return status | Visual switch |
|---|---|---|---|---|---|
| Claude Code | yes | yes | yes | yes | terminal, with modules |
| Codex | yes | no | no | no | no |
| Antigravity CLI | yes | no | no | no | no |
| Generic local agent | yes | before each turn, if the host calls the helper | no | no | no |
Codex and Antigravity are state-only. Their experimental hook files have no default wiring or verified live-host sync. See Codex installation and tested versions and limits.
/afk [optional context] I'm physically away
/back [optional context] I'm physically present again
In Claude Code, /afk saves your presence and tells the live local sessions the host can reach; the report names any it could not reach. Authorized work continues, and only steps that need you wait. /back collects a summary with what needs you first, then what got done. Optional context is information, never permission; /back context belongs to that return event and is not saved in the state file.
While AFK, sessions keep a durable decision queue and record reversible choices within existing authorization. Independent authorized work continues. At the next natural pause after /back, each session presents all queued decisions together as multiple-choice questions, recommended option first, then resumes asking in real time.
Click the terminal switch to run the same skills without context. Each invocation starts a model turn; the host's permissions and costs still apply.
/back ends AFK, including through the switch. Time passing, a remote reply, or a session restart never means you returned.Privacy: state stays local in ~/.afkswitch/state.json, or in state.json under AFKSWITCH_STATE_DIR when set. It contains status, time, optional AFK context, and a change counter. The helper briefly creates a lock while writing. If the old state contains malformed content and its bytes can be read, the helper copies it to state.json.corrupt-<time> before replacing it. If the file cannot be read, the transition fails and the file stays intact.
AFKSwitch runs no server and makes no network requests of its own. Messages between sessions travel through the host's own mechanisms and are governed by the host.
Read-only sync hooks inspect state and a bounded session transcript tail, then exit. The switch reads presence from state. There is no daemon, polling, account, or telemetry. Claude Code peer messages can include your optional context; host data handling applies.
Read the Privacy policy and Security policy.
git clone https://github.com/augbastos/afkswitch.git
claude --plugin-dir ./afkswitch
codex plugin marketplace add https://github.com/augbastos/afkswitch
codex plugin add afkswitch@afkswitch
Use $afkswitch:afk and $afkswitch:back. The state folder must be writable; see Codex sandbox setup.
Claude Code syncs presence at session start and prompt submission. The switch re-reads state on host events. See lifecycle sync, switch mechanics, and the presence specification.
Changelog · Details and usage · Compatibility · Presence specification · Writing an adapter · Conformance tests · Support · Contributing · MIT license
hooks/switch.tsx 220 lines1import type { EngineInterface, Register } from 'claude-code'
2
3type Status = 'available' | 'afk'
4type ReadStatus = Status | 'missing' | null
5
6let showOnboarding = false
7type Cache = {
8 initialized: boolean
9 lastGood: ReadStatus
10 readable: boolean
11 pending: boolean
12 awaiting: Status | null
13 started: boolean
14 failed: boolean
15 revision: number
16}
17
18// Mirror parse_since/context_problem/problems_v1/problems_v2 in the state helper.
19function validSince(value: unknown): boolean {
20 if (typeof value !== 'string') return false
21 const parts = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(Z|[+-](\d{2}):(\d{2}))$/.exec(value)
22 if (!parts || parts[0] !== value) return false
23 const [, y, m, d, hour, minute, s, zone, oh, om] = parts
24 const year = Number(y), month = Number(m), day = Number(d)
25 const leap = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)
26 const days = [31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
27 return year >= 1 && month >= 1 && month <= 12 && day >= 1 && day <= days[month - 1] &&
28 Number(hour) < 24 && Number(minute) < 60 && Number(s) < 60 &&
29 (zone === 'Z' || Number(oh) * 60 + Number(om) < 1440)
30}
31
32const validContext = (value: unknown): boolean => value === null ||
33 (typeof value === 'string' && [...value].length <= 2048 &&
34 !/[\u0000-\u0008\u000b-\u001f\u007f-\u009f\ud800-\udfff]/u.test(value))
35
36async function readStatus($: EngineInterface): Promise<ReadStatus> {
37 const override = await $.env.get('AFKSWITCH_STATE_DIR')
38 const home = override ? undefined : (await $.env.get('HOME')) || (await $.env.get('USERPROFILE'))
39 const directory = override || (home ? `${home}/.afkswitch` : undefined)
40 if (!directory) return null
41
42 try {
43 const path = `${directory.replace(/[\\/]+$/, '')}/state.json`
44 if (await $.fs.exists(path) === false) return 'missing'
45 const text = await $.fs.read(path)
46 const value: unknown = JSON.parse(text.replace(/^\uFEFF/, ''))
47 if (value === null || typeof value !== 'object' || Array.isArray(value)) return null
48 const state = value as Record<string, unknown>
49 const keys = state.version === 1
50 ? ['version', 'status', 'since', 'context']
51 : ['version', 'status', 'since', 'context', 'generation']
52 if (Object.keys(state).length !== keys.length || !keys.every(key => Object.hasOwn(state, key))) return null
53 if (state.version !== 1 && state.version !== 2) return null
54 if (state.status !== 'afk' && state.status !== 'available') return null
55 if (!validSince(state.since) || !validContext(state.context)) return null
56 if (state.version === 2 &&
57 (typeof state.generation !== 'number' || !Number.isInteger(state.generation) || state.generation < 1 ||
58 (state.status === 'available' && state.context !== null))) return null
59 return state.status
60 } catch {
61 // Unreadable, malformed and future state are unknown, never present.
62 return null
63 }
64}
65
66async function refresh($: EngineInterface, cache: Cache): Promise<ReadStatus> {
67 const request = ++cache.revision
68 try {
69 const status = await readStatus($)
70 if (request === cache.revision) {
71 cache.initialized = true
72 cache.readable = status !== null
73 if (status !== null) cache.lastGood = status
74 if (cache.awaiting && status === cache.awaiting) {
75 cache.pending = false
76 cache.awaiting = null
77 cache.started = false
78 cache.failed = false
79 }
80 }
81 return status
82 } catch (error) {
83 if (request === cache.revision) {
84 cache.initialized = false
85 cache.readable = false
86 }
87 throw error
88 }
89}
90
91// A drawing cache only: the helper used by the skills remains the single writer.
92// A hook that throws is skipped by the engine and the chain continues without it.
93export const register: Register = on => {
94 const cache: Cache = {
95 initialized: false, lastGood: null, readable: false,
96 pending: false, awaiting: null, started: false, failed: false, revision: 0,
97 }
98
99 on('session.start', async ($, e, next) => {
100 showOnboarding = false
101 try {
102 const seen = await $.store.get('onboardingVersion')
103 if (!(typeof seen === 'number' && seen >= 1)) {
104 showOnboarding = true
105 await $.store.set('onboardingVersion', 1)
106 }
107 } catch { showOnboarding = false }
108 await refresh($, cache)
109 cache.failed = false
110 $.ui.invalidate('ui.render')
111 return next(e)
112 })
113
114 on('prompt.submit', async ($, e, next) => {
115 showOnboarding = false
116 $.ui.invalidate('ui.render')
117 await refresh($, cache)
118 $.ui.invalidate('ui.render')
119 return next(e)
120 })
121
122 on('command.run', ($, e, next) => {
123 if (cache.awaiting && e.origin?.kind === 'plugin' && e.origin.name === 'afkswitch' &&
124 (e.command === 'afkswitch:afk' || e.command === 'afkswitch:back')) cache.started = true
125 return next(e)
126 })
127
128 on('turn.complete', async ($, e, next) => {
129 await refresh($, cache)
130 if (cache.awaiting) {
131 if (cache.started) {
132 cache.pending = false
133 cache.awaiting = null
134 cache.started = false
135 cache.failed = true
136 }
137 } else cache.failed = false
138 $.ui.invalidate('ui.render')
139 return next(e)
140 })
141
142 on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
143 // This release advertises the terminal only; remote paint is not verified.
144 if (e.surface !== 'terminal') return next(e)
145 if (!cache.initialized) await refresh($, cache)
146
147 const status = cache.readable ? cache.lastGood : null
148 const cells = status === 'afk' ? '■□' : status === 'available' ? '□■' : '□□'
149 // Capture explicit intent from this drawing; never toggle a later disk value.
150 const intended: Status | null = status === 'available' || status === 'missing' ? 'afk' : status === 'afk' ? 'available' : null
151 const { Box, Text, Button } = $.ui.resolve(e)
152 const original = await next(e)
153
154 const press = async () => {
155 // The synchronous guard also covers two presses of an old drawing.
156 if (cache.pending || intended === null) return
157 showOnboarding = false
158 cache.pending = true
159 cache.failed = false
160 try {
161 $.ui.invalidate('ui.render')
162 const current = await refresh($, cache)
163 if (current === intended) return
164 // Do not act from a file we cannot currently understand.
165 if (current === null) {
166 cache.failed = true
167 return
168 }
169 // Set before running: an idle session fires command.run inside this call.
170 cache.awaiting = intended
171 cache.started = false
172 intended === 'afk'
173 ? await $.command.run({ command: 'afkswitch:afk' })
174 : await $.command.run({ command: 'afkswitch:back' })
175 } catch {
176 cache.awaiting = null
177 cache.started = false
178 cache.failed = true
179 // Show disk truth even if command dispatch or completion failed.
180 try { await refresh($, cache) } catch { cache.readable = false }
181 } finally {
182 cache.pending = cache.awaiting !== null
183 // A redraw error must not escape a button handler into the host.
184 try { $.ui.invalidate('ui.render') } catch { /* text commands still work */ }
185 }
186 }
187
188 return (
189 <Box flexDirection="column">
190 {original}
191 {/* A Box takes no clicks, so while the switch can be pressed the whole inside of
192 the border is one Button (padding in its label). A Button has no color, so
193 "away" shows on the border instead of the word. */}
194 <Box
195 flexDirection="row"
196 alignSelf="flex-start"
197 borderStyle="single"
198 borderColor={status === 'afk' ? '#F28C28' : undefined}
199 paddingX={cache.pending || intended === null ? 1 : 0}
200 flexShrink={0}
201 >
202 {cache.pending || intended === null
203 ? <Text color={status === 'afk' ? '#F28C28' : undefined} dimColor={status !== 'afk'}>AFK</Text>
204 : <Button key="afkswitch-toggle" label={` AFK ${cells} `} plain dimColor={status !== 'afk'} onPress={press} />}
205 {cache.pending || intended === null ? <Text>{' '}</Text> : null}
206 {cache.pending || intended === null ? <Text dimColor={status !== 'afk'}>{cells}</Text> : null}
207 {cache.pending || cache.failed ? <Text>{' '}</Text> : null}
208 {cache.pending ? <Text dimColor>saving…</Text> : null}
209 {cache.failed ? <Text color="#F28C28">not switched — try /afk or /back</Text> : null}
210 </Box>
211 {showOnboarding ? <Box flexDirection="column">
212 <Text dimColor>{"AFKSwitch is ready — [ AFK □■ ] means you're here."}</Text>
213 <Text dimColor>{"Click it when you leave; click again when you're back."}</Text>
214 <Text dimColor>Use /afk [note] or /back [note] for optional context.</Text>
215 </Box> : null}
216 </Box>
217 )
218 })
219}
220