From 09c839ea5bc6cfc2cf34624afc8c62b9bed971ba Mon Sep 17 00:00:00 2001 From: Bardesss <30936735+bardesss@users.noreply.github.com> Date: Mon, 14 Sep 2026 16:29:04 +0200 Subject: [PATCH] fix: give one next action when a confirmation token is rejected The rejection text interpolated check.remedy, which says to call the tool again *without* `confirm`. That is right for a failure that hands nothing back, and wrong here: every rejection that reaches this branch has just issued a fresh token, named in the very next sentence. A caller that follows the remedy discards a usable token and spends another preview. The remedy stays in `confirm_error` as the diagnosis. The text now gives the single next action, worded the way the plain preview words it. The token also moves to the end of the sentence, which is the property that makes it liftable. A reader clipping the trailing word or the final backticked span gets the token; prose after it defeats both, and unwrap() strips punctuation, not a clause. Measured across both texts and four clipping strategies, all eight now apply; with the warning after the token, two of four failed on the rejection path. Co-Authored-By: Claude Opus 5 --- src/tools/write.ts | 11 +++++++++-- test/writeTool.test.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/tools/write.ts b/src/tools/write.ts index 46b623b5..1129ad4d 100644 --- a/src/tools/write.ts +++ b/src/tools/write.ts @@ -321,8 +321,15 @@ export function registerWriteTool( confirm_error: check.remedy, confirm_token: fresh }), - `Not applied — the confirmation token was rejected (${check.failure}). ${check.remedy} ` + - `A fresh token for this exact operation: \`${fresh}\`.` + // `check.remedy` says to call again *without* `confirm`, which is + // right when no token comes back and wrong here, because one does. + // It stays in `confirm_error` as the diagnosis; the text gives the + // single next action, worded as the plain preview words it. The + // token goes last in both: a reader clipping the trailing word or + // the final backticked span gets it, and prose after it would not. + `Not applied — the confirmation token was rejected (${check.failure}). ` + + `Do not resend the rejected one. To apply this, call ${spec.name} again with ` + + `the same arguments plus \`confirm\` set to \`${fresh}\`.` ); } } else { diff --git a/test/writeTool.test.ts b/test/writeTool.test.ts index 4fd4c5de..aae869b7 100644 --- a/test/writeTool.test.ts +++ b/test/writeTool.test.ts @@ -163,6 +163,32 @@ describe('write tool harness — preview and confirm', () => { expect(content[0]?.text).toContain(structuredContent.confirm_token); }); + // Every failure that reissues carries a remedy telling the caller to call + // again *without* `confirm`, which contradicts the usable token in the very + // next sentence. A model that follows the remedy throws away a good token + // and spends another preview. The diagnosis stays in `confirm_error`. + it('gives one next action on a rejection, not two', async () => { + const { content, structuredContent } = await harness.call('delete_media', { id: '5', confirm: 'nonsense' }); + const text = content[0]?.text ?? ''; + + expect(text).toContain(structuredContent.confirm_token); + expect(text).not.toContain('without `confirm`'); + expect(structuredContent.confirm_error).toContain('without `confirm`'); + }); + + // The token is the last thing in both texts on purpose. A reader clipping + // it out takes the trailing word or the final backticked span, and prose + // after the token defeats both — `unwrap` strips punctuation, not a clause. + it('ends both the preview and the rejection with the token itself', async () => { + const ending = /`v1\.[\w.-]+`\.?$/u; + + const preview = await harness.call('delete_media', { id: '5' }); + expect(preview.content[0]?.text.trimEnd()).toMatch(ending); + + const rejected = await harness.call('delete_media', { id: '5', confirm: 'nonsense' }); + expect(rejected.content[0]?.text.trimEnd()).toMatch(ending); + }); + it('lets the fresh token from a rejection be used', async () => { const rejected = await harness.call('delete_media', { id: '5', confirm: 'nonsense' }); const applied = await harness.call('delete_media', {