Draws the transcript rows of what the workflow-gates mod sends — an answer as the question it answered and the answer, a handoff's continuation as where the…

A Claude Code mod that draws the transcript row of an answer the workflow-gates mod sent as the question and the answer — Approve this task? → yes · Commit and continue to next task — and the row of a handoff's continuation as the line naming where the work went — → Discussion · note-window — in place of Claude Code's framing of a plugin's prompt. It is a plugin of its own because Claude Code skips a plugin's own render hooks on the row of a prompt that plugin submitted. It pairs a row with the record the mod leaves in the conversation's own folder as sent.json as it sends, drawing the record's line verbatim where it carries one, the handoff's; a row it cannot pair shows the answer alone. It changes only the drawing: the model reads Claude Code's framing, and ctrl+o shows the message in full.
Each line it draws for a row the mod sent is kept in that folder, ~/.config/workflows/conversations/{session-id}/rows.json (under WORKFLOWS_CONFIG_DIR where that is set, beside the workflows' system config), under the row's message id, so scrolling back and a resumed conversation draw the row the same way, wherever the session's working directory has moved. The folder goes once Claude Code has deleted the conversation's transcript, and the lines with it, with one exception: a conversation whose end the session-end hook never saw — the session that first installed the hook, or one that crashed — keeps its folder. The mod names the folder the way the workflows' engine does. A conversation that does not run the workflows has no folder, nor does one in a process that names neither a home directory nor WORKFLOWS_CONFIG_DIR: there the mod writes nothing and draws the row as Claude Code does.
It also draws the row of the note workflow-gates appends to a compaction — which Claude Code stores as an ordinary user row — as one dim line cut to the row's width: ⟳ Compacted · carrying on in fumi › discussion › management-window. It knows the row by the record the mod keeps in the conversation's folder as compacted.json, every note it handed up with the place it carries on in: a row whose text is a recorded note draws that place's line. It only reads the record — the gates mod is its one writer, and it keeps every note, so scrolling back and a resumed conversation draw the row the same way. A row that does not open as the engine's note costs no read, and ctrl+o shows the note in full.
hooks/register.ts 149 lines1// A plugin of its own: Claude Code skips the mod's render hooks on rows it sent.
2import type { EngineInterface, RenderPropsOf, Register } from 'claude-code'
3
4import { compacted } from './compacted.ts'
5import { folderOf } from './folder.ts'
6
7const SENDER = 'workflow-gates'
8
9const SENT = 'sent.json'
10const SPENT = 'null'
11
12const ROWS = 'rows.json'
13
14const FRAMED = /sent a message:\n([\s\S]+?)\n\nThis is how Claude Code surfaces a prompt/
15
16// A gate's answer, or a handoff's continuation with the line it draws as.
17type Sent =
18 | { answer: string; question: string; label: string }
19 | { answer: string; line: string }
20
21type Rows = Record<string, unknown>
22
23const isText = (field: unknown) => typeof field === 'string'
24
25const isSent = (value: unknown): value is Sent => {
26 const sent = (value ?? {}) as Record<string, unknown>
27
28 return (
29 isText(sent.answer) &&
30 (isText(sent.line) || (isText(sent.question) && isText(sent.label)))
31 )
32}
33
34const isRows = (value: unknown): value is Rows =>
35 typeof value === 'object' && value !== null && !Array.isArray(value)
36
37async function conversationFolder($: EngineInterface): Promise<string | null> {
38 return folderOf(
39 await $.env.get('WORKFLOWS_CONFIG_DIR'),
40 await $.env.get('HOME'),
41 await $.session.id(),
42 )
43}
44
45function lineOf(sent: Sent): string {
46 if ('line' in sent) {
47 return sent.line
48 }
49
50 const { answer, question, label } = sent
51 const answered = `${question} → ${answer}`
52
53 return label === '' || label === answer ? answered : `${answered} · ${label}`
54}
55
56function answerIn({
57 origin,
58 isExpanded,
59 text,
60}: RenderPropsOf['UserMessage']): string | null {
61 if (isExpanded || origin.kind !== 'plugin' || origin.name !== SENDER) {
62 return null
63 }
64
65 return FRAMED.exec(text)?.[1] ?? null
66}
67
68async function lastSent($: EngineInterface, folder: string): Promise<Sent | null> {
69 try {
70 const sent: unknown = JSON.parse(await $.fs.read(`${folder}/${SENT}`))
71
72 return isSent(sent) ? sent : null
73 } catch {
74 return null
75 }
76}
77
78async function keptIn($: EngineInterface, folder: string): Promise<Rows> {
79 try {
80 const rows: unknown = JSON.parse(await $.fs.read(`${folder}/${ROWS}`))
81
82 return isRows(rows) ? rows : {}
83 } catch {
84 return {}
85 }
86}
87
88async function firstLineOf(
89 $: EngineInterface,
90 requestId: string,
91 answer: string,
92): Promise<string | null> {
93 const folder = await conversationFolder($)
94
95 if (folder === null || !(await $.fs.exists(folder))) {
96 return null
97 }
98
99 const rows = await keptIn($, folder)
100 const kept = rows[requestId]
101
102 if (typeof kept === 'string') {
103 return kept
104 }
105
106 const sent = await lastSent($, folder)
107 const paired = sent?.answer === answer
108 const line = paired ? lineOf(sent) : answer
109
110 await $.fs.write(
111 `${folder}/${ROWS}`,
112 JSON.stringify({ ...rows, [requestId]: line }),
113 )
114
115 if (paired) {
116 await $.fs.write(`${folder}/${SENT}`, SPENT)
117 }
118
119 return line
120}
121
122export const register: Register = on => {
123 const lines = new Map<string, string | null>()
124
125 on('ui.render', { component: 'UserMessage' }, async ($, e, next) => {
126 const answer = answerIn(e.props)
127
128 if (answer === null) {
129 return next(e)
130 }
131
132 let line = lines.get(e.requestId)
133
134 if (line === undefined) {
135 line = await firstLineOf($, e.requestId, answer)
136 lines.set(e.requestId, line)
137 }
138
139 if (line === null) {
140 return next(e)
141 }
142
143 // The drawing alone changes: the model reads Claude Code's framing, by design.
144 return next({ ...e, props: { ...e.props, text: line } })
145 }).catch(($, e, next) => next(e))
146
147 compacted(on)
148}
149hooks/compacted.ts 80 lines1/**
2 * The row of the note the `workflow-gates` mod appends to a compaction, drawn
3 * as one dim line in place of a prompt row the person never typed. Claude
4 * Code stores that note as an ordinary user row, so the row is known by its
5 * text: the gates mod records every note it hands up in the conversation's
6 * folder as `compacted.json`, with the place it carries on in, and this
7 * module only reads it — so scrolling and a resume draw the row the same way.
8 */
9import type { EngineInterface, On } from 'claude-code'
10
11import { folderOf } from './folder.ts'
12
13const COMPACTED = 'compacted.json'
14
15/**
16 * Every note the gates mod hands up opens with the engine's words, so a row
17 * that does not is never one and costs no read.
18 */
19const OPENING = 'The conversation was just compacted. '
20
21/** Keyed by each note's text. */
22type Notes = Record<string, unknown>
23
24const isNotes = (value: unknown): value is Notes =>
25 typeof value === 'object' && value !== null && !Array.isArray(value)
26
27export const noteOf = (place: string) => `⟳ Compacted · carrying on in ${place}`
28
29/**
30 * The line the row of `text` draws: the note for the place its record names;
31 * null for any other row, and in a conversation with no folder.
32 */
33async function noteFor($: EngineInterface, text: string): Promise<string | null> {
34 const folder = folderOf(
35 await $.env.get('WORKFLOWS_CONFIG_DIR'),
36 await $.env.get('HOME'),
37 await $.session.id(),
38 )
39
40 if (folder === null || !(await $.fs.exists(folder))) {
41 return null
42 }
43
44 try {
45 const notes: unknown = JSON.parse(await $.fs.read(`${folder}/${COMPACTED}`))
46 const place = isNotes(notes) ? notes[text] : undefined
47
48 return typeof place === 'string' ? noteOf(place) : null
49 } catch {
50 return null
51 }
52}
53
54export function compacted(on: On) {
55 const notes = new Map<string, string | null>()
56
57 on('ui.render', { component: 'UserMessage' }, async ($, e, next) => {
58 const text = e.props.text.trim()
59
60 if (e.props.isExpanded || !text.startsWith(OPENING)) {
61 return next(e)
62 }
63
64 let note = notes.get(e.requestId)
65
66 if (note === undefined) {
67 note = await noteFor($, text)
68 notes.set(e.requestId, note)
69 }
70
71 if (note === null) {
72 return next(e)
73 }
74
75 const { Text } = await $.ui.resolve(e)
76
77 return Text({ dimColor: true, wrap: 'truncate-end', children: note })
78 }).catch(($, e, next) => next(e))
79}
80hooks/folder.ts 26 lines1/**
2 * The conversation's folder, as the engine names it (domain/conversation.cjs):
3 * in the workflows' system config directory — `WORKFLOWS_CONFIG_DIR`, else
4 * `.config/workflows` in the home directory — by the session id's safe
5 * characters alone, so a `cd` or a resume elsewhere finds it all the same.
6 *
7 * No `$` here: a hook's `$` is never followed across an import, so each
8 * module reads the environment and the files itself.
9 */
10
11/**
12 * The folder of the conversation `id`, from the process's
13 * `WORKFLOWS_CONFIG_DIR` and `HOME`; null where they name neither directory.
14 */
15export function folderOf(
16 configDir: string | undefined,
17 home: string | undefined,
18 id: string,
19): string | null {
20 const config = configDir || (home ? `${home}/.config/workflows` : null)
21
22 return config === null
23 ? null
24 : `${config}/conversations/${id.replace(/[^A-Za-z0-9_-]/g, '')}`
25}
26