index.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * Advisory per-agent repeat-call detector. It enriches post-execute decisions
  3. * with logged model context without vetoing or rewriting calls. Configuration
  4. * and chain semantics live in the package README; rationale lives in the
  5. * repeat-tool-reminder Agent Note.
  6. * @module @deepseek-ai/dsh-repeat-tool-reminder
  7. */
  8. import type { Context } from '@deepseek-ai/cordis'
  9. import z from '@deepseek-ai/schemastery'
  10. import type { Agent, PreStepDecision } from '@deepseek-ai/dsh-agent'
  11. import { createUserMessage } from '@deepseek-ai/dsh-llm'
  12. import type { MessageSource } from '@deepseek-ai/dsh-llm'
  13. import type { UserMessage } from '@deepseek-ai/dsh-session'
  14. import type { PostToolDecision, ToolExecution } from '@deepseek-ai/dsh-tools'
  15. export const name = 'repeat-tool-reminder'
  16. /**
  17. * Plugin config, validated by the same-named schemastery schema plus the
  18. * load-time checks in `apply` (misconfiguration fails loud: an empty
  19. * `thresholds` list, a non-integer, a value below 2, or a duplicate throws at
  20. * plugin load, never a silent fall-back). `include`/`exclude` entries are
  21. * `*`-wildcard predicates over tool names at call time, not references to
  22. * registry entries — a pattern matching no currently registered tool is valid
  23. * (`exclude: [mcp_*]` must stay legal in a deployment that loads no MCP tools).
  24. */
  25. export interface Config {
  26. /** Consecutive-repeat counts that trigger a reminder (default `[3, 5, 8]`). */
  27. thresholds?: number[]
  28. /** Tool-name patterns to track; empty means every tool is tracked. */
  29. include?: string[]
  30. /** Tool-name patterns transparent to the chain (neither count nor reset). */
  31. exclude?: string[]
  32. /**
  33. * Maximum characters of canonical arguments quoted in the DETAILED reminder
  34. * (default 500). Large payloads (a `write` body, a long command) would
  35. * otherwise ride into the next request unbounded — precisely in a loop
  36. * scenario; the cap bounds the reminder, never the detection (the chain key
  37. * always compares the FULL canonical string).
  38. */
  39. argumentsPreviewChars?: number
  40. }
  41. export const Config: z<Config> = z.object({
  42. thresholds: z.array(z.number()).default([3, 5, 8]),
  43. include: z.array(z.string()).default([]),
  44. exclude: z.array(z.string()).default([]),
  45. argumentsPreviewChars: z.number().default(500),
  46. })
  47. /**
  48. * The `{kind:'plugin'}` source stamped on every reminder this guard injects —
  49. * the label is load-bearing (an unlabeled context would render as a user
  50. * prompt in derived history).
  51. */
  52. const PLUGIN_SOURCE: MessageSource = { kind: 'plugin', plugin: 'repeat-tool-reminder' }
  53. /**
  54. * The gentle first-threshold reminder. Keyed to `thresholds[0]`, not a literal
  55. * count, so a custom first threshold keeps the gentle-then-detailed escalation.
  56. */
  57. const GENTLE_REMINDER =
  58. 'You are repeating the exact same tool call with identical arguments. '
  59. + 'Carefully analyze the previous result before calling again: if the task is '
  60. + 'not complete, try a different approach or different arguments instead of '
  61. + 'repeating the call.'
  62. /** The detailed later-threshold reminder naming the tool, the run length, and the canonical arguments. */
  63. function detailedReminder(toolName: string, count: number, canonicalArguments: string): string {
  64. return 'Repeated tool call detected:\n'
  65. + `- tool: ${toolName}\n`
  66. + `- consecutive_calls: ${count}\n`
  67. + `- arguments: ${canonicalArguments}\n`
  68. + 'The repeated calls are not making progress. Do not call this tool with '
  69. + 'these exact arguments again. Inspect the latest result and choose a '
  70. + 'different action, different arguments, or finish the task if enough '
  71. + 'evidence has been gathered.'
  72. }
  73. /**
  74. * Deep key-sort of a parsed-JSON value so two argument objects that differ
  75. * only in property order canonicalize identically. Arguments reach the guard
  76. * as the loop's `JSON.parse` output (or its raw-string fallback for malformed
  77. * argument JSON), so JSON's value domain is the whole input domain — no
  78. * bigint, cycle, or `undefined` handling exists because no input path can
  79. * produce them.
  80. */
  81. function sortJsonValue(value: unknown): unknown {
  82. if (Array.isArray(value)) return value.map(sortJsonValue)
  83. if (value !== null && typeof value === 'object') {
  84. const record = value as Record<string, unknown>
  85. const sorted: Record<string, unknown> = {}
  86. for (const key of Object.keys(record).sort()) {
  87. sorted[key] = sortJsonValue(record[key])
  88. }
  89. return sorted
  90. }
  91. return value
  92. }
  93. /** Canonical string form of a call's arguments: deep key-sort, then stringify. */
  94. function canonicalize(argumentsValue: unknown): string {
  95. return JSON.stringify(sortJsonValue(argumentsValue))
  96. }
  97. /** Compile one `*`-wildcard pattern to an anchored RegExp (every other regex metacharacter is matched literally). */
  98. function wildcardToRegExp(pattern: string): RegExp {
  99. const escaped = pattern.replace(/[|\\{}()[\]^$+?.]/g, String.raw`\$&`)
  100. return new RegExp(`^${escaped.replaceAll('*', '.*')}$`)
  101. }
  102. /**
  103. * Head-truncate the canonical arguments for quoting in the detailed reminder,
  104. * marking how much was omitted. Bounds only the model-visible text — the
  105. * chain key always uses the full canonical string.
  106. */
  107. function previewArguments(canonical: string, cap: number): string {
  108. if (canonical.length <= cap) return canonical
  109. return `${canonical.slice(0, cap)}… (+${canonical.length - cap} more chars)`
  110. }
  111. /**
  112. * Validate `thresholds` per the fail-loud contract and return them sorted
  113. * ascending (the escalation rule reads `thresholds[0]` as the gentle tier, so
  114. * order is normalized here, once).
  115. */
  116. function validateThresholds(values: number[]): number[] {
  117. if (values.length === 0) {
  118. throw new Error('repeat-tool-reminder: `thresholds` must not be empty')
  119. }
  120. for (const value of values) {
  121. if (!Number.isInteger(value) || value < 2) {
  122. throw new Error(`repeat-tool-reminder: invalid threshold ${value} — every threshold must be an integer >= 2`)
  123. }
  124. }
  125. if (new Set(values).size !== values.length) {
  126. throw new Error('repeat-tool-reminder: `thresholds` must not contain duplicates')
  127. }
  128. return [...values].sort((a, b) => a - b)
  129. }
  130. /**
  131. * Prepend the guard's reminder while preserving every downstream context's
  132. * source and metadata.
  133. */
  134. function prependContext(ours: UserMessage, theirs: UserMessage[] | undefined): UserMessage[] {
  135. return [ours, ...theirs ?? []]
  136. }
  137. /** One agent's consecutive-repeat chain: the last tracked call's identity key and its run length. */
  138. interface Chain {
  139. key: string
  140. count: number
  141. }
  142. /**
  143. * Install the guard's listeners.
  144. * @param ctx - plugin context; listeners are scoped to it and disposed with it.
  145. * @param config - validated {@link Config}; `thresholds` is re-checked fail-loud here.
  146. */
  147. export function apply(ctx: Context, config: Config): void {
  148. // schemastery's .default() guarantees the fields are set after validation.
  149. const thresholds = validateThresholds(config.thresholds as number[])
  150. const thresholdSet = new Set(thresholds)
  151. const includePatterns = (config.include as string[]).map(wildcardToRegExp)
  152. const excludePatterns = (config.exclude as string[]).map(wildcardToRegExp)
  153. const argumentsPreviewChars = config.argumentsPreviewChars as number
  154. if (!Number.isInteger(argumentsPreviewChars) || argumentsPreviewChars < 1) {
  155. throw new Error(`repeat-tool-reminder: invalid argumentsPreviewChars ${argumentsPreviewChars} — must be an integer >= 1`)
  156. }
  157. const chains = new WeakMap<Agent, Chain>()
  158. /** Whether a tool participates in the chain (untracked calls are transparent: they neither count nor reset). */
  159. function tracked(toolName: string): boolean {
  160. if (includePatterns.length > 0 && !includePatterns.some(pattern => pattern.test(toolName))) return false
  161. return !excludePatterns.some(pattern => pattern.test(toolName))
  162. }
  163. /**
  164. * Advance the calling agent's chain for one attempt and return the reminder
  165. * to deliver, if this attempt's run length hits a configured threshold.
  166. * Counting happens here — in post-execute — because denied calls also flow
  167. * through this waterfall (`ToolRuntime.execute` routes a deny through the
  168. * same pipeline), and a model hammering a denied call is exactly the loop
  169. * worth breaking.
  170. */
  171. function observe(exec: ToolExecution): UserMessage | undefined {
  172. // A direct `ctx.tools.execute()` caller has no model to remind and no id
  173. // to key on; only agent-loop calls participate.
  174. if (!exec.agent) return undefined
  175. if (!tracked(exec.name)) return undefined
  176. const canonical = canonicalize(exec.arguments)
  177. const key = JSON.stringify([exec.name, canonical])
  178. const chain = chains.get(exec.agent)
  179. const count = chain !== undefined && chain.key === key ? chain.count + 1 : 1
  180. chains.set(exec.agent, { key, count })
  181. if (!thresholdSet.has(count)) return undefined
  182. const text = count === thresholds[0]
  183. ? GENTLE_REMINDER
  184. : detailedReminder(exec.name, count, previewArguments(canonical, argumentsPreviewChars))
  185. return createUserMessage({
  186. content: [{ type: 'text', text }],
  187. source: { ...PLUGIN_SOURCE, form: 'notice', summary: `${exec.name} × ${count}` },
  188. })
  189. }
  190. // Observe-and-enrich, never veto: count first (state advances regardless of
  191. // the downstream outcome), DELEGATE so a later listener can still block or
  192. // replace, then fold the reminder onto whatever came back — additionalContexts
  193. // rides both decision variants, so a blocked call still gets the nudge.
  194. ctx.on('tools/post-execute', async (exec, _result, next): Promise<PostToolDecision> => {
  195. const reminder = observe(exec)
  196. const downstream = await next()
  197. if (!reminder) return downstream
  198. if (downstream.kind === 'block') {
  199. return { kind: 'block', feedback: downstream.feedback, additionalContexts: prependContext(reminder, downstream.additionalContexts) }
  200. }
  201. return {
  202. ...downstream,
  203. additionalContexts: prependContext(reminder, downstream.additionalContexts),
  204. }
  205. })
  206. // A user interjection changes the context; repetition across it is not a
  207. // loop. Pure reset hook: always delegates (attaching nothing, vetoing
  208. // nothing).
  209. ctx.on('agent/pre-step', ({ agent, messages }, next): Promise<PreStepDecision> => {
  210. if (messages.some(message => message.source.kind === 'user')) chains.delete(agent)
  211. return next()
  212. })
  213. }