🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated
TypeScript agentic-aiagentic-frameworkagentic-workflowagentsai-agentsai-assistantai-skillsautonomous-agentsclaudecodexdsh-pluginharnessmcp-servermulti-agentmulti-agent-systemsnpmskillsswarmswarm-intelligencetypescript
fix(hooks): post-task reports observed learning counts, not placeholders (#3353) (#3403)
rUv9e71cf66d230
diff --git a/v3/@claude-flow/cli/__tests__/hooks-post-task-honest-counts-3353.test.ts b/v3/@claude-flow/cli/__tests__/hooks-post-task-honest-counts-3353.test.tsnew file mode 100644index 0000000000..cb812b87b3--- /dev/null+++ b/v3/@claude-flow/cli/__tests__/hooks-post-task-honest-counts-3353.test.ts@@ -0,0 +1,104 @@+/**+ * #3353 — hooks_post-task must report observed learning counts, not+ * success-derived placeholders.+ *+ * Before: `patternsUpdated: feedbackResult?.updated || (success ? 2 : 1)`,+ * `newPatterns: success ? 1 : 0`, `trajectoryId: traj-${Date.now()}` — so a+ * run whose feedback controller was unavailable still claimed "2 patterns+ * updated, 1 new pattern", and an observed `updated: 0` was turned into 2 by+ * the `||` fallback. The direct trajectory result was awaited and discarded.+ */+import { describe, it, expect, vi, beforeEach } from 'vitest';++type Feedback = { success: boolean; controller: string; updated: number } | null;++const bridgeRecordFeedback = vi.fn(async (): Promise<Feedback> => null);+const recordTrajectory = vi.fn(async (): Promise<boolean> => true);++vi.mock('../src/memory/memory-bridge.js', () => ({+ bridgeRecordFeedback,+ bridgeRecordCausalEdge: vi.fn(async () => ({ success: true, controller: 'mock' })),+ bridgeStoreEntry: vi.fn(async () => ({ success: true, controller: 'mock' })),+}));+vi.mock('../src/memory/intelligence.js', () => ({ recordTrajectory }));+vi.mock('../src/memory/graph-edge-writer.js', () => ({ insertGraphEdge: vi.fn(async () => undefined) }));++const { hooksPostTask } = await import('../src/mcp-tools/hooks-tools.js');++type PostTaskResult = {+ success: boolean;+ learningUpdates: {+ patternsUpdated: number;+ newPatterns: number | null;+ trajectoryId: string | null;+ controller: string;+ outcomePersisted: boolean;+ available: boolean;+ reason?: string;+ };+ trajectory: { recorded: boolean };+ feedback: { recorded: boolean; controller: string; updates: number };+};++const run = (success = true) =>+ hooksPostTask.handler({ taskId: 'post-task-3353', success, quality: 0.9 }) as Promise<PostTaskResult>;++beforeEach(() => {+ bridgeRecordFeedback.mockReset();+ recordTrajectory.mockReset();+});++describe('#3353 hooks_post-task reports observed learning counts', () => {+ it('feedback controller unavailable (null) + trajectory recorded: no fabricated counts', async () => {+ bridgeRecordFeedback.mockResolvedValue(null);+ recordTrajectory.mockResolvedValue(true);++ const r = await run(true);++ expect(r.success).toBe(true); // task outcome is still reported+ expect(r.learningUpdates.patternsUpdated).toBe(0);+ expect(r.learningUpdates.newPatterns).toBeNull();+ expect(r.learningUpdates.available).toBe(false);+ expect(r.learningUpdates.reason).toMatch(/feedback/i);+ expect(r.learningUpdates.controller).toBe('none');+ expect(r.trajectory.recorded).toBe(true);+ expect(r.feedback).toEqual({ recorded: false, controller: 'unavailable', updates: 0 });+ });++ it('observed updated:0 is not replaced by a || fallback', async () => {+ bridgeRecordFeedback.mockResolvedValue({ success: false, controller: 'learningSystem', updated: 0 });+ recordTrajectory.mockResolvedValue(true);++ const r = await run(true);++ expect(r.learningUpdates.patternsUpdated).toBe(0);+ expect(r.learningUpdates.newPatterns).toBeNull();+ expect(r.learningUpdates.available).toBe(false);+ });++ it('both learning paths fail: degraded, no counts, no trajectory id', async () => {+ bridgeRecordFeedback.mockRejectedValue(new Error('bridge down'));+ recordTrajectory.mockResolvedValue(false);++ const r = await run(false);++ expect(r.learningUpdates.patternsUpdated).toBe(0);+ expect(r.learningUpdates.newPatterns).toBeNull();+ expect(r.learningUpdates.trajectoryId).toBeNull();+ expect(r.learningUpdates.available).toBe(false);+ expect(r.trajectory.recorded).toBe(false);+ });++ it('a working controller reports its own observed count', async () => {+ bridgeRecordFeedback.mockResolvedValue({ success: true, controller: 'learningSystem', updated: 3 });+ recordTrajectory.mockResolvedValue(true);++ const r = await run(true);++ expect(r.learningUpdates.patternsUpdated).toBe(3);+ expect(r.learningUpdates.controller).toBe('learningSystem');+ expect(r.learningUpdates.available).toBe(true);+ expect(r.learningUpdates.reason).toBeUndefined();+ expect(r.feedback).toEqual({ recorded: true, controller: 'learningSystem', updates: 3 });+ });+});diff --git a/v3/@claude-flow/cli/src/commands/hooks.ts b/v3/@claude-flow/cli/src/commands/hooks.tsindex 505177c66e..a9585659f0 100644--- a/v3/@claude-flow/cli/src/commands/hooks.ts+++ b/v3/@claude-flow/cli/src/commands/hooks.ts@@ -2188,9 +2188,12 @@ const postTaskCommand: Command = {duration: number;learningUpdates: {patternsUpdated: number;- newPatterns: number;- trajectoryId: string;+ newPatterns: number | null;+ trajectoryId: string | null;+ available?: boolean;+ reason?: string;};+ trajectory?: { recorded: boolean };}>('hooks_post-task', {taskId,success,@@ -2221,6 +2224,13 @@ const postTaskCommand: Command = {output.writeln();output.printSuccess(`Task outcome recorded: ${success ? 'SUCCESS' : 'FAILED'}`);+ // #3353: only show observed learning results; say so when degraded.+ const lu = result.learningUpdates;+ if (lu.available === false) {+ output.writeln();+ output.printWarning(`Learning degraded: ${lu.reason ?? 'feedback not recorded'} — no pattern updates were observed`);+ }+output.writeln();output.writeln(output.bold('Learning Updates'));output.printTable({@@ -2229,10 +2239,10 @@ const postTaskCommand: Command = {{ key: 'value', header: 'Value', width: 20, align: 'right' }],data: [- { metric: 'Patterns Updated', value: result.learningUpdates.patternsUpdated },- { metric: 'New Patterns', value: result.learningUpdates.newPatterns },+ { metric: 'Patterns Updated', value: lu.patternsUpdated },+ { metric: 'New Patterns', value: lu.newPatterns ?? 'unknown' },+ { metric: 'Trajectory Recorded', value: result.trajectory ? (result.trajectory.recorded ? 'yes' : 'no') : 'unknown' },{ metric: 'Duration', value: `${(result.duration / 1000).toFixed(1)}s` },- { metric: 'Trajectory ID', value: result.learningUpdates.trajectoryId }]});diff --git a/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.tsindex b6b5e3c467..fae9d55e2e 100644--- a/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts+++ b/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts@@ -1607,13 +1607,15 @@ export const hooksPostTask: MCPTool = {// Non-fatal}- // Record trajectory via intelligence module (SONA + ReasoningBank)+ // Record trajectory via intelligence module (SONA + ReasoningBank).+ // #3353: keep the observed result instead of discarding it.+ let trajectoryRecorded = false;try {const intelligence = await import('../memory/intelligence.js');- await intelligence.recordTrajectory(+ trajectoryRecorded = (await intelligence.recordTrajectory([{ type: 'result' as const, content: (params.task as string) || taskId, metadata: { success, agent, quality }, timestamp: Date.now() }],success ? 'success' : 'failure'- );+ )) === true;} catch {// Intelligence module not available — non-fatal}@@ -1751,17 +1753,32 @@ export const hooksPostTask: MCPTool = {writeFileSync(storePath, JSON.stringify(store, null, 2), 'utf-8');} catch { /* non-critical */ }+ // #3353: report only observed learning results. The previous+ // `feedbackResult?.updated || (success ? 2 : 1)` / `newPatterns: success ? 1 : 0`+ // invented counts whenever the feedback controller was unavailable (and the+ // `||` turned an observed 0 into 2). No path reports pattern *creation*, so+ // newPatterns is null (unknown) rather than a guess; the trajectory has no+ // real id to surface, so trajectoryId is null.+ const feedbackRecorded = feedbackResult?.success === true;+ const learningAvailable = feedbackRecorded;return {taskId,success,duration,learningUpdates: {- patternsUpdated: feedbackResult?.updated || (success ? 2 : 1),- newPatterns: success ? 1 : 0,- trajectoryId: `traj-${Date.now()}`,+ patternsUpdated: feedbackRecorded ? (feedbackResult?.updated ?? 0) : 0,+ newPatterns: null as number | null,+ trajectoryId: null as string | null,controller: feedbackResult?.controller || 'none',outcomePersisted,+ available: learningAvailable,+ ...(learningAvailable ? {} : {+ reason: feedbackResult+ ? `feedback controller '${feedbackResult.controller}' did not record the outcome`+ : 'feedback controller unavailable',+ }),},+ trajectory: { recorded: trajectoryRecorded },quality,pheromone,feedback: feedbackResult ? {