Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix(cli): amp — render server-run tools, stop replaying a resumed thread, widen the recap timeout

0xken15769cd2e59d37
diff --git a/cli/src/engines/amp/normalizer.spec.ts b/cli/src/engines/amp/normalizer.spec.ts
index 416540da8..2904047a1 100644
--- a/cli/src/engines/amp/normalizer.spec.ts
+++ b/cli/src/engines/amp/normalizer.spec.ts
@@ -118,12 +118,33 @@ describe('amp tool names', () => {
})
describe('amp tool output', () => {
- it('unwraps both shapes Amp returns', () => {
+ it('unwraps the shapes Amp returns', () => {
expect(ampToolOutput({ output: 'hello\n', exitCode: 0 })).toBe('hello\n')
expect(ampToolOutput({ content: [{ type: 'text', text: 'a' }, { type: 'text', text: 'b' }] })).toBe('a\nb')
expect(ampToolOutput('plain')).toBe('plain')
expect(ampToolOutput(undefined)).toBe('')
})
+
+ /**
+ * A web search arrives in TWO different shapes depending on which path carried it — the `tool.result`
+ * event hands over a JSON *string*, the message block hands over `{result:[…]}` — and both hold whole
+ * scraped pages under `excerpts`. Rendered raw, one search filled the card with site chrome.
+ */
+ it('reduces a web search to its sources, from either shape', () => {
+ const hits = [
+ { title: 'Bitcoin price today', url: 'https://coinmarketcap.com/currencies/bitcoin/', excerpts: ['…pages of scraped text…'] },
+ { title: 'Bitcoin | CoinGecko', url: 'https://www.coingecko.com/en/coins/bitcoin', excerpts: ['…more…'] },
+ ]
+ const expected = 'Bitcoin price today — https://coinmarketcap.com/currencies/bitcoin/\n'
+ + 'Bitcoin | CoinGecko — https://www.coingecko.com/en/coins/bitcoin'
+ expect(ampToolOutput(JSON.stringify(hits))).toBe(expected) // the event path
+ expect(ampToolOutput({ result: hits, status: 'done' })).toBe(expected) // the message-block path
+ })
+
+ it('leaves a string that merely looks like JSON alone', () => {
+ expect(ampToolOutput('[1,2,3]')).toBe('[1,2,3]')
+ expect(ampToolOutput('{"not":"a search"}')).toBe('{"not":"a search"}')
+ })
})
describe('amp runtime profile', () => {
diff --git a/cli/src/engines/amp/normalizer.ts b/cli/src/engines/amp/normalizer.ts
index f9c7dfaec..302d63c8f 100644
--- a/cli/src/engines/amp/normalizer.ts
+++ b/cli/src/engines/amp/normalizer.ts
@@ -93,12 +93,42 @@ export function ampWorkspaceRoot(firstLine: string): string | null {
/**
* A tool result's output, as text.
*
- * Amp's `output` is not a string. Measured, it is `{content:[{type:'text',text:…}]}` for the tools that
- * return prose and a bare object (`{output:'hello\n',exitCode:0}`) for shell. Rendering the JSON of the
- * first shape put a wall of escaped braces on the card where two lines of output belonged.
+ * Amp's `output` is never a plain string, and it has THREE shapes, all measured:
+ *
+ * {content:[{type:'text',text:…}]} tools that return prose (skill)
+ * {output:'hello\n',exitCode:0} shell
+ * {result:[{url,title,excerpts:[…]}],status} the SERVER-run tools, web_search among them
+ *
+ * The third one matters most on screen: a web search returns whole scraped pages, and rendering its JSON
+ * put thousands of characters of site chrome on a card where a list of sources belonged. It is reduced to
+ * `title — url` lines here, which is what the result actually says.
*/
+/** `[{title,url,excerpts}]` → one `title — url` line each, or '' if that is not what this is. */
+function searchHits(value: unknown): string {
+ if (!Array.isArray(value)) return ''
+ const lines = value
+ .map((entry) => {
+ const hit = obj(entry)
+ if (!hit) return ''
+ const title = str(hit.title).trim()
+ const url = str(hit.url).trim()
+ return title && url ? `${title} — ${url}` : title || url
+ })
+ .filter(Boolean)
+ return lines.length ? lines.join('\n') : ''
+}
+
export function ampToolOutput(output: unknown): string {
- if (typeof output === 'string') return output
+ if (typeof output === 'string') {
+ // The same search result reaches us two ways and only one of them is an object: the `tool.result`
+ // EVENT hands over a JSON *string* holding `[{title,url,excerpts}]` (measured), while the message
+ // block hands over `{result:[…]}`. Unparsed, the card showed a wall of scraped page text.
+ try {
+ const hits = searchHits(JSON.parse(output))
+ if (hits) return hits
+ } catch { /* an ordinary string, which is the common case */ }
+ return output
+ }
const body = obj(output)
if (!body) return output === undefined || output === null ? '' : JSON.stringify(output)
const content = body.content
@@ -110,6 +140,8 @@ export function ampToolOutput(output: unknown): string {
if (text) return text
}
if (typeof body.output === 'string') return body.output
+ const hits = searchHits(body.result)
+ if (hits) return hits
return JSON.stringify(body)
}
diff --git a/cli/src/lib/hooks.ts b/cli/src/lib/hooks.ts
index 68d3a1854..f7ca7e1bb 100644
--- a/cli/src/lib/hooks.ts
+++ b/cli/src/lib/hooks.ts
@@ -627,8 +627,12 @@ export default function (amp: any) {
const workdir = process.env.HARNESS_AGENT_CWD || process.env.PWD || process.cwd()
const emitted = new Set()
+ // Tool ids already written, so a tool seen BOTH as an event and as a message block is written once.
+ const toolCalls = new Set()
+ const toolResults = new Set()
let registered = false
let file = ''
+ let seeded = false
const line = (record: any) => {
try {
@@ -665,28 +669,59 @@ export default function (amp: any) {
} catch {}
}
- // Emit any assistant text/thinking not written yet. Tool blocks are deliberately skipped: tool.call and
- // tool.result own those cards, and emitting them here too would double every tool in the transcript.
- const drain = async (ctx: any) => {
+ const resultId = (block: any) => String(block.toolUseID || block.tool_use_id || '')
+
+ /**
+ * Write everything in the thread that has not been written yet.
+ *
+ * This reads message BLOCKS rather than events, because the events do not cover the thread:
+ *
+ * - no event carries assistant text at all, and
+ * - \`tool.call\`/\`tool.result\` only fire for tools the CLIENT executes. MEASURED: a \`web_search\`
+ * turn produced no tool event and no tool lease in Amp's own log, yet the message plainly held a
+ * \`tool_use\` block — Amp runs that tool on its server. Skipping blocks meant a search rendered as
+ * nothing at all on web and device while the pane showed "Explored 1 search".
+ *
+ * \`seed\` marks what already exists WITHOUT writing it, for a thread that is being resumed: those
+ * messages belong to earlier turns, and replaying them made a fresh turn open with an old answer in it.
+ */
+ const drain = async (ctx: any, seed = false) => {
let messages: any[] = []
try { messages = await ctx.thread.messages({ from: 'end', limit: 20 }) } catch { return }
for (const message of messages) {
- if (!message || message.role !== 'assistant') continue
const blocks = Array.isArray(message.content) ? message.content : []
for (let i = 0; i < blocks.length; i++) {
const block = blocks[i]
- if (!block || (block.type !== 'text' && block.type !== 'thinking')) continue
- const key = String(message.id) + '#' + i
- if (emitted.has(key)) continue
- const text = typeof block.text === 'string' ? block.text : block.thinking
- if (typeof text !== 'string' || !text) continue
- emitted.add(key)
- line({ t: block.type, id: message.id, i, text })
+ if (!block) continue
+ if (block.type === 'text' || block.type === 'thinking') {
+ if (message.role !== 'assistant') continue
+ const key = String(message.id) + '#' + i
+ if (emitted.has(key)) continue
+ const text = typeof block.text === 'string' ? block.text : block.thinking
+ if (typeof text !== 'string' || !text) continue
+ emitted.add(key)
+ if (!seed) line({ t: block.type, id: message.id, i, text })
+ } else if (block.type === 'tool_use') {
+ const id = String(block.id || '')
+ if (!id || toolCalls.has(id)) continue
+ toolCalls.add(id)
+ if (!seed) line({ t: 'tool_call', id, tool: block.name, input: block.input })
+ } else if (block.type === 'tool_result') {
+ const id = resultId(block)
+ if (!id || toolResults.has(id)) continue
+ toolResults.add(id)
+ // A server-run tool puts its payload under \`run\`; a client-run one under \`content\`/\`output\`.
+ if (!seed) line({ t: 'tool_result', id, status: 'done', output: block.run ?? block.content ?? block.output })
+ }
}
}
}
- amp.on('session.start', async (event: any) => { await register(event.thread.id) })
+ amp.on('session.start', async (event: any, ctx: any) => {
+ await register(event.thread.id)
+ // A resumed thread arrives full of earlier turns. Claim them silently, once.
+ if (!seeded) { seeded = true; await drain(ctx, true) }
+ })
amp.on('agent.start', async (event: any, ctx: any) => {
await register(event.thread.id)
@@ -697,10 +732,12 @@ export default function (amp: any) {
amp.on('tool.call', async (event: any, ctx: any) => {
await register(event.thread.id)
await drain(ctx)
+ toolCalls.add(String(event.toolUseID))
line({ t: 'tool_call', id: event.toolUseID, tool: event.tool, input: event.input })
})
amp.on('tool.result', async (event: any, ctx: any) => {
+ toolResults.add(String(event.toolUseID))
line({
t: 'tool_result',
id: event.toolUseID,
diff --git a/cli/src/lib/oneshot.ts b/cli/src/lib/oneshot.ts
index ffb878abe..f60cd3f55 100644
--- a/cli/src/lib/oneshot.ts
+++ b/cli/src/lib/oneshot.ts
@@ -935,7 +935,16 @@ export function runAmpOneShot(opts: OneShotOptions): Promise<OneShotResult> {
delete processEnv.TMUX
delete processEnv.TMUX_PANE
delete processEnv.MACHINE_ID
- const timeoutMs = opts.timeoutMs ?? 60_000
+ /**
+ * Amp gets 120s where every other engine gets 60.
+ *
+ * Not a guess: a real recap died on `amp one-shot timed out after 60000ms`, and five cold runs of a
+ * prompt as trivial as "reply OK" then measured 8.7s, 10.0s, 23.5s, 25.0s and 26.3s. The spread is
+ * Amp's server connect — `AMP_SKIP_UPDATE_CHECK=1` was tried and changed nothing, so it is not set
+ * here — and a real recap prompt starts from the far end of it. 60s left almost no room for the
+ * model itself.
+ */
+ const timeoutMs = opts.timeoutMs ?? 120_000
return new Promise<OneShotResult>((resolve, reject) => {
const child = spawn(env.AMP_PATH || 'amp', args, {