Skip to content
This repository was archived by the owner on Aug 17, 2026. It is now read-only.

Commit 8b07799

Browse files
authored
fix(grunt/tui): proper hover-preview + click for #N ticket refs (replaces ugly [label](url)) (#14)
Refs hivemind anomalyco#233. Closes the 'ugly link being showed' regression Nik flagged immediately after the first hover-preview attempt. ## What changed Previous shipped path: pre-process text to inject markdown links `[anomalyco#229](http://localhost:5173/tasks/229)`. Opentui's markdown renderer displays inline links as the LITERAL bracket+label+paren+URL string (see node_modules/.bun/@OpenTui+core/index*.js renderInlineToken 'link' case). Even in conceal mode the URL leaks as `<label> (url)`. Result: colored but visually ugly, no hover, no click handler. New path: split assistant text on the ticket-ref regex BEFORE markdown sees it. Render non-matching segments through markdown as before (so formatting + code blocks keep working). Render each matched `#N` as a new <TicketRef> component (packages/opencode/src/cli/cmd/tui/component/ticket-ref.tsx). ## TicketRef component - Renders `anomalyco#229` colored (theme.markdownLink) + underlined inline - onMouseOver: lazy-fetches /api/tasks/<id> from hivemind-api (with module-level cache + in-flight dedup), shows a floating tooltip with title + status + priority + zone + owner + scope preview - onMouseUp: opens http://localhost:5173/tasks/<id> via 'open' npm pkg (same package the existing Link component uses) Trade-off: inline markdown features that span a #N boundary (rare — e.g. `**bold anomalyco#229 text**`) won't span the split. Acceptable for prose. ## Bundled fix: install-local.sh version stamping Working tree also contained an uncommitted update to scripts/install-local.sh (originated from a parallel agent during this session). It changes the default version label from `git describe --dirty` (which made local builds look version-ahead of any released gruntcode → broke brew-binary boot when local migrations had advanced the DB) to a semver build-metadata format like `1.15.10-grunt.7+local.<sha>`. The base matches the latest grunt tag; the +local.<sha> part is semver build-metadata that's ignored for ordering. Also adds ahead-of-tag detection that warns about new migrations. Including it here since it was needed for THIS install to not break on next brew upgrade.
1 parent 5573d83 commit 8b07799

3 files changed

Lines changed: 321 additions & 25 deletions

File tree

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
// grunt-it: clickable hivemind ticket-ref (#229) component for assistant chat (#233).
2+
//
3+
// Renders `#229` colored + underlined inline. Hover lazily fetches the ticket detail
4+
// from hivemind-api (localhost:7890) and shows a tooltip overlay with title + status +
5+
// scope-preview. Click opens hivemind-ui at /tasks/<id> in the system browser.
6+
//
7+
// Why not markdown links: opentui's markdown component renders `[label](url)` literally
8+
// (with brackets + URL visible) and has no hover/click hooks for inline tokens. We bypass
9+
// it for ticket refs and render directly.
10+
11+
import { createSignal, Show } from "solid-js"
12+
import open from "open"
13+
import { useTheme } from "../context/theme"
14+
import { useHivemind } from "../context/hivemind"
15+
16+
const HIVEMIND_UI_BASE = process.env.HIVEMIND_UI_BASE ?? "http://localhost:5173"
17+
const HIVEMIND_API_BASE = process.env.HIVEMIND_API_BASE ?? "http://127.0.0.1:7890"
18+
19+
type TicketDetail = {
20+
id: number
21+
title: string
22+
scope?: string | null
23+
status: string
24+
priority: string
25+
zone?: string | null
26+
owner?: string | null
27+
}
28+
29+
const cache = new Map<number, TicketDetail | null>()
30+
const inflight = new Map<number, Promise<TicketDetail | null>>()
31+
32+
function fetchTicket(id: number): Promise<TicketDetail | null> {
33+
if (cache.has(id)) return Promise.resolve(cache.get(id)!)
34+
const existing = inflight.get(id)
35+
if (existing) return existing
36+
const p = (async () => {
37+
try {
38+
const controller = new AbortController()
39+
const t = setTimeout(() => controller.abort(), 1500)
40+
const res = await fetch(`${HIVEMIND_API_BASE}/api/tasks/${id}`, { signal: controller.signal })
41+
clearTimeout(t)
42+
if (!res.ok) {
43+
cache.set(id, null)
44+
return null
45+
}
46+
const body = (await res.json()) as { task?: TicketDetail } | TicketDetail
47+
const task = (body as { task?: TicketDetail }).task ?? (body as TicketDetail)
48+
cache.set(id, task ?? null)
49+
return task ?? null
50+
} catch {
51+
cache.set(id, null)
52+
return null
53+
} finally {
54+
inflight.delete(id)
55+
}
56+
})()
57+
inflight.set(id, p)
58+
return p
59+
}
60+
61+
export function TicketRef(props: { id: number }) {
62+
const { theme } = useTheme()
63+
const hive = useHivemind()
64+
const [hovered, setHovered] = createSignal(false)
65+
const [detail, setDetail] = createSignal<TicketDetail | null | undefined>(cache.get(props.id))
66+
67+
function onEnter() {
68+
setHovered(true)
69+
if (detail() === undefined) {
70+
void fetchTicket(props.id).then((d) => setDetail(d))
71+
}
72+
}
73+
74+
function onLeave() {
75+
setHovered(false)
76+
}
77+
78+
function onClick() {
79+
open(`${HIVEMIND_UI_BASE}/tasks/${props.id}`).catch(() => {})
80+
}
81+
82+
return (
83+
<box flexDirection="column" flexShrink={0}>
84+
<text
85+
fg={theme.markdownLink ?? theme.primary}
86+
attributes={1}
87+
onMouseOver={onEnter}
88+
onMouseOut={onLeave}
89+
onMouseUp={onClick}
90+
>
91+
#{props.id}
92+
</text>
93+
<Show when={hovered() && hive.state.apiOnline}>
94+
<box
95+
position="absolute"
96+
backgroundColor={theme.backgroundPanel}
97+
border={true}
98+
borderColor={theme.border}
99+
paddingLeft={1}
100+
paddingRight={1}
101+
paddingTop={0}
102+
paddingBottom={0}
103+
zIndex={2000}
104+
maxWidth={60}
105+
>
106+
<Show
107+
when={detail() !== undefined}
108+
fallback={<text fg={theme.textMuted}>loading #{props.id}...</text>}
109+
>
110+
<Show
111+
when={detail()}
112+
fallback={<text fg={theme.textMuted}>#{props.id}: (not found)</text>}
113+
>
114+
{(d) => (
115+
<>
116+
<text fg={theme.text}>
117+
<b>#{d().id} {d().title}</b>
118+
</text>
119+
<text fg={theme.textMuted}>
120+
<span>{d().status}</span>
121+
{" · "}
122+
<span>{d().priority}</span>
123+
<Show when={d().zone}>
124+
{" · "}
125+
<span>zone={d().zone}</span>
126+
</Show>
127+
<Show when={d().owner}>
128+
{" · "}
129+
<span>owner={d().owner}</span>
130+
</Show>
131+
</text>
132+
<Show when={d().scope}>
133+
<text fg={theme.textMuted} wrapMode="word">
134+
{(d().scope ?? "").slice(0, 200)}
135+
{(d().scope ?? "").length > 200 ? "..." : ""}
136+
</text>
137+
</Show>
138+
<text fg={theme.textMuted}>
139+
<span>click to open in hivemind-ui</span>
140+
</text>
141+
</>
142+
)}
143+
</Show>
144+
</Show>
145+
</box>
146+
</Show>
147+
</box>
148+
)
149+
}

‎packages/opencode/src/cli/cmd/tui/routes/session/index.tsx‎

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import { useSDK } from "@tui/context/sdk"
5555
import { useEditorContext } from "@tui/context/editor"
5656
import { useDialog } from "../../ui/dialog"
5757
import { TodoItem } from "../../component/todo-item"
58+
import { TicketRef } from "../../component/ticket-ref"
5859
import { DialogMessage } from "./dialog-message"
5960
import type { PromptInfo } from "../../component/prompt/history"
6061
import { DialogConfirm } from "@tui/ui/dialog-confirm"
@@ -1596,34 +1597,83 @@ function ReasoningHeader(props: {
15961597
)
15971598
}
15981599

1599-
// grunt-it: linkify hivemind ticket refs (#229) inline so they render as clickable
1600-
// markdown links pointing at the local hivemind-ui. The markdown renderer styles them
1601-
// with theme.markdownLink (blue/cyan) and terminals supporting OSC 8 (iTerm2, kitty, etc.)
1602-
// make the link clickable to open the URL. Falls back to colored-but-non-clickable text
1603-
// in older terminals — still better than plain text.
1604-
// Refs hivemind #233.
1600+
// grunt-it: hivemind ticket refs (#229) in assistant text — render as interactive
1601+
// <TicketRef> components with hover-preview + click-to-open-hivemind-ui. Refs #233.
1602+
//
1603+
// We split the text on the ticket-ref regex, render each non-matching segment through
1604+
// the markdown renderer (so prose formatting, code blocks, etc. keep working), and
1605+
// interleave <TicketRef> components for the matches. Trade-off: inline markdown features
1606+
// that cross a #N boundary (rare — e.g. **bold #229 text**) won't span the split. Acceptable
1607+
// for typical prose. The wins: clean visual (no [label](url) artifact), real hover tooltip,
1608+
// proper click handler.
16051609
const HIVEMIND_TICKET_RE = /(^|[^\w/#])#(\d{1,5})\b/g
1606-
const HIVEMIND_UI_BASE = process.env.HIVEMIND_UI_BASE ?? "http://localhost:5173"
1607-
function linkifyTicketRefs(text: string): string {
1608-
return text.replace(HIVEMIND_TICKET_RE, (_, prefix, id) => `${prefix}[#${id}](${HIVEMIND_UI_BASE}/tasks/${id})`)
1610+
1611+
type TextSegment = { kind: "text"; text: string } | { kind: "ticket"; id: number }
1612+
1613+
function splitOnTicketRefs(text: string): TextSegment[] {
1614+
const segments: TextSegment[] = []
1615+
let lastIndex = 0
1616+
HIVEMIND_TICKET_RE.lastIndex = 0
1617+
let match: RegExpExecArray | null
1618+
while ((match = HIVEMIND_TICKET_RE.exec(text)) !== null) {
1619+
const [whole, prefix, idStr] = match
1620+
const start = match.index + prefix.length
1621+
if (start > lastIndex) {
1622+
segments.push({ kind: "text", text: text.slice(lastIndex, start) })
1623+
}
1624+
segments.push({ kind: "ticket", id: Number(idStr) })
1625+
lastIndex = match.index + whole.length
1626+
}
1627+
if (lastIndex < text.length) {
1628+
segments.push({ kind: "text", text: text.slice(lastIndex) })
1629+
}
1630+
// Optimization: if no ticket matches, return a single text segment (caller can fast-path)
1631+
return segments.length > 0 ? segments : [{ kind: "text", text }]
16091632
}
16101633

16111634
function TextPart(props: { last: boolean; part: TextPart; message: AssistantMessage }) {
16121635
const ctx = use()
16131636
const { theme, syntax } = useTheme()
1637+
const segments = createMemo(() => splitOnTicketRefs(props.part.text.trim()))
16141638
return (
16151639
<Show when={props.part.text.trim()}>
16161640
<box id={"text-" + props.part.id} paddingLeft={3} marginTop={1} flexShrink={0}>
1617-
<markdown
1618-
syntaxStyle={syntax()}
1619-
streaming={true}
1620-
internalBlockMode="top-level"
1621-
content={linkifyTicketRefs(props.part.text.trim())}
1622-
tableOptions={{ style: "grid" }}
1623-
conceal={ctx.conceal()}
1624-
fg={theme.markdownText}
1625-
bg={theme.background}
1626-
/>
1641+
<Show
1642+
when={segments().some((s) => s.kind === "ticket")}
1643+
fallback={
1644+
<markdown
1645+
syntaxStyle={syntax()}
1646+
streaming={true}
1647+
internalBlockMode="top-level"
1648+
content={props.part.text.trim()}
1649+
tableOptions={{ style: "grid" }}
1650+
conceal={ctx.conceal()}
1651+
fg={theme.markdownText}
1652+
bg={theme.background}
1653+
/>
1654+
}
1655+
>
1656+
<box flexDirection="row" flexWrap="wrap">
1657+
<For each={segments()}>
1658+
{(seg) =>
1659+
seg.kind === "text" ? (
1660+
<markdown
1661+
syntaxStyle={syntax()}
1662+
streaming={true}
1663+
internalBlockMode="top-level"
1664+
content={seg.text}
1665+
tableOptions={{ style: "grid" }}
1666+
conceal={ctx.conceal()}
1667+
fg={theme.markdownText}
1668+
bg={theme.background}
1669+
/>
1670+
) : (
1671+
<TicketRef id={seg.id} />
1672+
)
1673+
}
1674+
</For>
1675+
</box>
1676+
</Show>
16271677
</box>
16281678
</Show>
16291679
)

‎scripts/install-local.sh‎

Lines changed: 103 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,118 @@
66
# Usage:
77
# bash scripts/install-local.sh [version-label]
88
#
9-
# version-label defaults to "$(git describe --always --dirty)-local" so it's
10-
# obvious from `gruntcode --version` that this is a local-build, not a release.
9+
# Version label
10+
# -------------
11+
# By default, the script stamps the binary with the SAME base version CI would
12+
# stamp if you pushed the latest reachable `v*-grunt.*` tag — e.g.
13+
# `1.15.10-grunt.7+local.5573d8375`. The part before `+` matches a real
14+
# release; the part after `+` is semver build-metadata (ignored for ordering),
15+
# so brew + version-comparisons + peers see the same release this binary is
16+
# "based on", while `gruntcode --version` still flags it as a local build.
17+
#
18+
# Why this matters
19+
# ----------------
20+
# Stamping local builds with `git describe --dirty` (the previous default) made
21+
# them look version-ahead of any released gruntcode. If the worktree contained
22+
# new drizzle migrations that the released brew binary doesn't have, the local
23+
# build would apply them to ~/.local/share/opencode/opencode.db — and then the
24+
# brew binary would crash on startup trying to re-apply migrations whose
25+
# columns already exist. Today's incident: see today's activity log.
26+
#
27+
# Ahead-of-tag detection
28+
# ----------------------
29+
# If HEAD is ahead of the latest grunt tag, the script prints a loud warning
30+
# listing the new commits + any new migration files. It proceeds anyway (per
31+
# Nik's policy choice) but the warning makes it impossible to miss the
32+
# "you're building something CI hasn't released, the DB is now ahead of brew"
33+
# trap. Pass `--ahead-ok` to suppress the warning when intentional.
1134
#
1235
# Pairs with the standard release flow: tag a release for distribution, but
1336
# install locally immediately so Nik isn't blocked on CI for his own bin.
1437

1538
set -euo pipefail
1639

1740
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
18-
VERSION_LABEL="${1:-$(git -C "$ROOT" describe --always --dirty)-local}"
1941

20-
echo "→ Building gruntcode from source (version=$VERSION_LABEL)..."
42+
# --- parse args --------------------------------------------------------------
43+
AHEAD_OK=0
44+
EXPLICIT_LABEL=""
45+
for arg in "$@"; do
46+
case "$arg" in
47+
--ahead-ok) AHEAD_OK=1 ;;
48+
-h|--help)
49+
sed -n '2,40p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'
50+
exit 0
51+
;;
52+
*) EXPLICIT_LABEL="$arg" ;;
53+
esac
54+
done
55+
56+
# --- compute version label ---------------------------------------------------
57+
if [ -n "$EXPLICIT_LABEL" ]; then
58+
VERSION_LABEL="$EXPLICIT_LABEL"
59+
else
60+
# Latest grunt-* tag reachable from HEAD. Strip leading `v` to match CI's
61+
# `${TAG#v}` (see .github/workflows/grunt-release.yml).
62+
LATEST_TAG=$(git -C "$ROOT" describe --tags --abbrev=0 --match 'v*-grunt.*' 2>/dev/null || echo "")
63+
if [ -z "$LATEST_TAG" ]; then
64+
echo "ERROR: no reachable v*-grunt.* tag from HEAD; can't derive a CI-equivalent version." >&2
65+
echo " Pass an explicit version label as the first arg, or tag a release first." >&2
66+
exit 1
67+
fi
68+
BASE_VERSION="${LATEST_TAG#v}"
69+
70+
SHORT_SHA=$(git -C "$ROOT" rev-parse --short HEAD)
71+
# Semver build metadata can only contain [0-9A-Za-z-] segments separated by '.'.
72+
# We use `local.<sha>` (+ optional `.dirty`) to stay strictly valid.
73+
BUILD_META="local.${SHORT_SHA}"
74+
if [ -n "$(git -C "$ROOT" status --porcelain)" ]; then
75+
BUILD_META="${BUILD_META}.dirty"
76+
fi
77+
VERSION_LABEL="${BASE_VERSION}+${BUILD_META}"
78+
fi
79+
80+
# --- ahead-of-tag warning ----------------------------------------------------
81+
if [ -z "$EXPLICIT_LABEL" ] && [ "$AHEAD_OK" -eq 0 ]; then
82+
AHEAD_COUNT=$(git -C "$ROOT" rev-list --count "${LATEST_TAG}..HEAD" 2>/dev/null || echo 0)
83+
if [ "$AHEAD_COUNT" -gt 0 ]; then
84+
# New migration files since the tag = the migration-desync trap.
85+
NEW_MIGRATIONS=$(git -C "$ROOT" diff --name-only --diff-filter=A "${LATEST_TAG}..HEAD" \
86+
-- 'packages/opencode/migration/**/migration.sql' 2>/dev/null || true)
87+
88+
{
89+
echo
90+
echo "⚠️ HEAD is ${AHEAD_COUNT} commit(s) ahead of ${LATEST_TAG}."
91+
echo " This local build contains code that CI has NOT released."
92+
if [ -n "$NEW_MIGRATIONS" ]; then
93+
echo
94+
echo " NEW MIGRATIONS in this build (vs ${LATEST_TAG}):"
95+
# shellcheck disable=SC2001 # sed is fine here; bash subst can't easily prefix every line
96+
echo "$NEW_MIGRATIONS" | sed 's|^| • |'
97+
echo
98+
echo " After installing this binary, the brew-released gruntcode (${LATEST_TAG})"
99+
echo " will likely CRASH on startup against your DB until you also tag+release"
100+
echo " these migrations, or downgrade your DB. You've been warned."
101+
else
102+
echo " No new migrations detected — binary swap is schema-safe."
103+
fi
104+
echo
105+
echo " Pass --ahead-ok to suppress this warning when intentional."
106+
echo
107+
} >&2
108+
fi
109+
fi
110+
111+
# --- build -------------------------------------------------------------------
112+
# Note: we deliberately do NOT set OPENCODE_RELEASE=1 here. That flag enables
113+
# the post-build `gh release upload` step in packages/opencode/script/build.ts,
114+
# which is for CI only. Setting it locally caused the build to fail with
115+
# `bun: no matches found: ./dist/*.tar.gz` after the binary was produced — the
116+
# binary still got copied to brew because the upload step ran last, but the
117+
# error was noise we don't want.
118+
echo "→ Building gruntcode from source (version=${VERSION_LABEL})..."
21119
cd "$ROOT/packages/opencode"
22120
OPENCODE_VERSION="$VERSION_LABEL" \
23-
OPENCODE_RELEASE=1 \
24121
OPENCODE_CHANNEL=latest \
25122
bun script/build.ts --single --skip-embed-web-ui 2>&1 | tail -5 || true
26123

@@ -31,7 +128,7 @@ if [ -z "$SRC_BIN" ] || [ ! -x "$SRC_BIN" ]; then
31128
exit 1
32129
fi
33130

34-
# Find the current brew Cellar gruntcode dir
131+
# --- install -----------------------------------------------------------------
35132
CELLAR_BIN=$(find /opt/homebrew/Cellar/gruntcode -name gruntcode -type f 2>/dev/null | sort -r | head -1)
36133
if [ -z "$CELLAR_BIN" ]; then
37134
echo "ERROR: no brew-installed gruntcode found. Run 'brew install grunt-it/tap/gruntcode' first." >&2

0 commit comments

Comments
 (0)