Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(skillopt): success-reflect prompt states the edit schema (#7257)
garrytanb1384cfe8cae
diff --git a/src/core/skillopt/reflect.ts b/src/core/skillopt/reflect.tsindex 08b095564e..e807c52db1 100644--- a/src/core/skillopt/reflect.ts+++ b/src/core/skillopt/reflect.ts@@ -79,15 +79,18 @@ export function describeJudges(tasks: ReadonlyArray<{ judge: Judge }>): string {return blocks.join('\n');}-const FAILURE_REFLECT_SYSTEM = `You are SkillOpt's optimizer. You analyze AGENT FAILURE TRAJECTORIES and propose specific edits to a SKILL document so the agent does better next time.--Output ONLY a single JSON object on one or more lines:+/** The output format both reflect modes send (#7257): parseEdits accepts exactly these op and field names. */+const EDIT_SCHEMA = `Output ONLY a single JSON object on one or more lines:{"edits": [{"op": "add|replace|delete", ...}, ...]}Edit ops:add: {"op": "add", "anchor": "<exact heading text>", "content": "<new markdown>", "reason": "<one sentence>"}replace: {"op": "replace", "target": "<exact text to find>", "replacement": "<new text>", "reason": "<one sentence>"}- delete: {"op": "delete", "target": "<exact text to remove>", "reason": "<one sentence>"}+ delete: {"op": "delete", "target": "<exact text to remove>", "reason": "<one sentence>"}`;++const FAILURE_REFLECT_SYSTEM = `You are SkillOpt's optimizer. You analyze AGENT FAILURE TRAJECTORIES and propose specific edits to a SKILL document so the agent does better next time.++${EDIT_SCHEMA}Rules:- Each edit MUST address a SPECIFIC failure pattern you observed.@@ -100,7 +103,9 @@ Rules:const SUCCESS_REFLECT_SYSTEM = `You are SkillOpt's optimizer. You analyze AGENT SUCCESS TRAJECTORIES and propose specific edits to a SKILL document so the agent CONSISTENTLY does what worked here.-Output format and rules are identical to the failure-reflect mode — same {edits: [...]} shape.+${EDIT_SCHEMA}++anchor / target MUST be uniquely identifiable in the skill body (exact match). Output at MOST 8 edits.When successes are present, look for: which rules were FOLLOWED to produce success, which rules could be MADE EXPLICIT (not yet stated, but exemplified), which anti-patterns the agent successfully AVOIDED that should be stated.diff --git a/test/skillopt/reflect-success-schema-7257.test.ts b/test/skillopt/reflect-success-schema-7257.test.tsnew file mode 100644index 0000000000..15689a0088--- /dev/null+++ b/test/skillopt/reflect-success-schema-7257.test.ts@@ -0,0 +1,65 @@+/**+ * #7257 — the success-reflect call states the same edit schema as the+ * failure-reflect call. Its system prompt used to say only that the format is+ * "identical to the failure-reflect mode", a prompt that call never sends, so+ * the optimizer guessed field names (find/replace) and parseEdits dropped+ * every success-only edit.+ */+import { describe, expect, test } from 'bun:test';+import { runReflect } from '../../src/core/skillopt/reflect.ts';+import type { ChatOpts, ChatResult } from '../../src/core/ai/gateway.ts';+import type { ScoredRollout } from '../../src/core/skillopt/types.ts';++const MODEL = 'anthropic:claude-opus-4-7';+type ChatFn = NonNullable<Parameters<typeof runReflect>[0]['chatFn']>;++function scored(id: string, score: number): ScoredRollout {+ return {+ trajectory: {+ task_id: id, task: `Task ${id}`, final_text: `output ${id}`, tool_calls: [],+ usage: { input_tokens: 0, output_tokens: 0, cache_read_tokens: 0, cache_creation_tokens: 0 },+ turns: 1, stop_reason: 'end', duration_ms: 1,+ },+ score,+ };+}++function reply(text: string): ChatResult {+ return { text, blocks: [{ type: 'text', text }], stopReason: 'end',+ usage: { input_tokens: 1, output_tokens: 1, cache_read_tokens: 0, cache_creation_tokens: 0 }, model: MODEL, providerId: 'anthropic' };+}++async function systemsFor(successes: ScoredRollout[], failures: ScoredRollout[]): Promise<string[]> {+ const systems: string[] = [];+ const chatFn = (async (opts: ChatOpts) => { systems.push(String(opts.system)); return reply('{"edits":[]}'); }) as ChatFn;+ await runReflect({ skillBodyText: '# S\n## People\n', successes, failures, rejected: [], optimizerModel: MODEL, chatFn });+ return systems;+}++const EDIT_OPS = /Edit ops:[\s\S]*?delete:[^\n]*\n/;++describe('#7257: both reflect modes send the edit schema', () => {+ test('a success-only batch sends the op and field names parseEdits accepts', async () => {+ const systems = await systemsFor([scored('s', 1)], []);+ expect(systems).toHaveLength(1);+ for (const field of ['"op"', '"anchor"', '"content"', '"target"', '"replacement"', '{"edits": [']) {+ expect(systems[0]).toContain(field);+ }+ });++ test('the success and failure prompts carry the identical output format and edit ops', async () => {+ const systems = await systemsFor([scored('s', 1)], [scored('f', 0)]);+ expect(systems).toHaveLength(2);+ const [failure, success] = systems.map(p => EDIT_OPS.exec(p)?.[0]);+ expect(failure).toBeDefined();+ expect(success).toBe(failure);+ });++ test('a success-only reply written in that schema parses into edits', async () => {+ const text = JSON.stringify({ edits: [{ op: 'replace', target: '## People', replacement: '## People and roles', reason: 'clearer' }] });+ const chatFn = (async () => reply(text)) as ChatFn;+ const res = await runReflect({ skillBodyText: '# S\n## People\n', successes: [scored('s', 1)], failures: [], rejected: [], optimizerModel: MODEL, chatFn });+ expect(res.successEdits).toHaveLength(1);+ expect(res.errors).toEqual([]);+ });+});