Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 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

fix(hooks): index trajectory-end feedback even when steps were recorded (#3662)

Claude68dc046a2aa8
diff --git a/v3/@claude-flow/cli/__tests__/trajectory-feedback-indexing-3662.test.ts b/v3/@claude-flow/cli/__tests__/trajectory-feedback-indexing-3662.test.ts
new file mode 100644
index 0000000000..040e7f7cf2
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/trajectory-feedback-indexing-3662.test.ts
@@ -0,0 +1,114 @@
+// Regression test for #3662 (stepful part): `hooksTrajectoryEnd` only
+// promoted non-empty `feedback` to the searchable `pattern` namespace when
+// the trajectory had NO recorded steps (`#2351`'s `!hasSteps` gate). The
+// common start -> step -> end path (the one real agents actually use)
+// therefore recorded SONA state but left the feedback unindexed and
+// unreachable through `hooks_intelligence_pattern-search`.
+//
+// This test drives the full public path end to end: trajectory-start,
+// trajectory-step (so `hasSteps` is true), trajectory-end with feedback,
+// then pattern-search for that feedback text. It fails on the pre-fix
+// `!hasSteps` gate and passes once feedback is promoted regardless of
+// whether steps were recorded.
+
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+// A tiny in-memory store standing in for the real memory-initializer. It is
+// deliberately simple (substring match) β€” just enough to prove the pattern
+// namespace entry written by trajectory-end is the same one pattern-search
+// reads back, mirroring what the real HNSW/SQLite search does semantically.
+const durable = new Map<string, { value: string; namespace: string }>();
+
+const storeEntry = vi.fn(async ({ key, value, namespace }: { key: string; value: string; namespace?: string }) => {
+ durable.set(key, { value, namespace: namespace ?? 'default' });
+ return { success: true, id: key };
+});
+const getEntry = vi.fn(async ({ key }: { key: string }) => ({
+ success: true,
+ found: durable.has(key),
+ entry: { content: durable.get(key)?.value },
+}));
+const deleteEntry = vi.fn(async ({ key }: { key: string }) => ({ success: true, deleted: durable.delete(key) }));
+const searchEntries = vi.fn(async ({ query, namespace }: { query: string; namespace?: string }) => {
+ const results = Array.from(durable.entries())
+ .filter(([, entry]) => (namespace ? entry.namespace === namespace : true))
+ .filter(([, entry]) => entry.value.toLowerCase().includes(query.toLowerCase()))
+ .map(([key, entry]) => ({
+ id: key,
+ key,
+ content: entry.value,
+ score: 0.9,
+ namespace: entry.namespace,
+ }));
+ return { success: results.length > 0, results, searchTime: 0 };
+});
+
+vi.mock('../src/memory/memory-initializer.js', () => ({ storeEntry, getEntry, deleteEntry, searchEntries }));
+// Force both the trajectory-end feedback promotion and pattern-search to
+// fall through to the memory-initializer fallback above instead of a real
+// ReasoningBank/AgentDB bridge, which isn't available under test.
+vi.mock('../src/memory/memory-bridge.js', () => ({
+ bridgeStorePattern: async () => null,
+ bridgeSearchPatterns: async () => null,
+}));
+vi.mock('../src/memory/sona-optimizer.js', () => ({ getSONAOptimizer: async () => null }));
+vi.mock('../src/memory/ewc-consolidation.js', () => ({ getEWCConsolidator: async () => null }));
+vi.mock('../src/memory/intelligence.js', () => ({
+ runBackgroundLearning: async () => {},
+ getIntelligenceStats: () => ({ trajectoriesRecorded: 0 }),
+ recordTrajectory: async () => {},
+}));
+vi.mock('../src/memory/graph-edge-writer.js', () => ({ insertGraphEdge: async () => {} }));
+vi.mock('../src/ruvector/trajectory-tree.js', () => ({
+ getTrajectoryTree: () => ({ openTrajectory() {}, appendStep() {}, closeTrajectory() {} }),
+}));
+
+const fresh = async () => { vi.resetModules(); return import('../src/mcp-tools/hooks-tools.js'); };
+
+beforeEach(() => {
+ durable.clear();
+ storeEntry.mockClear();
+ getEntry.mockClear();
+ deleteEntry.mockClear();
+ searchEntries.mockClear();
+});
+
+describe('stepful trajectory feedback indexing (#3662)', () => {
+ it('promotes feedback to the pattern namespace even when steps were recorded, and it becomes searchable', async () => {
+ const hooks = await fresh();
+
+ const started = await hooks.hooksTrajectoryStart.handler({ task: 'ship the release notes', agent: 'coder' }) as any;
+
+ // Record a step so `hasSteps` is true β€” this is the common path real
+ // agents take, and the one #2351 did NOT cover.
+ const stepResult = await hooks.hooksTrajectoryStep.handler({
+ trajectoryId: started.trajectoryId,
+ action: 'drafted notes',
+ result: 'reviewed by lead',
+ }) as any;
+ expect(stepResult.recorded).toBe(true);
+ expect(stepResult.totalSteps).toBe(1);
+
+ const distilledFeedback = 'always double check the changelog version bump before tagging a release';
+ const ended = await hooks.hooksTrajectoryEnd.handler({
+ trajectoryId: started.trajectoryId,
+ success: true,
+ feedback: distilledFeedback,
+ }) as any;
+
+ // The trajectory had steps, so the pre-fix `!hasSteps` gate dropped the
+ // feedback entirely: `feedbackDistilled` stayed undefined and
+ // `patternsExtracted` only counted the one recorded step.
+ expect(ended.trajectory.totalSteps).toBe(1);
+ expect(ended.learning.feedbackDistilled).toBeDefined();
+ expect(ended.learning.feedbackDistilled?.stored).not.toBe(false);
+ expect(ended.learning.patternsExtracted).toBe(2); // 1 step + 1 distilled feedback pattern
+
+ // The feedback must now be indexed in the 'pattern' namespace and
+ // reachable through hooks_intelligence_pattern-search, the same way
+ // step-less feedback already was after #2351.
+ const search = await hooks.hooksPatternSearch.handler({ query: distilledFeedback }) as any;
+ expect(search.results.length).toBeGreaterThan(0);
+ expect(search.results.some((r: any) => String(r.pattern).includes(distilledFeedback))).toBe(true);
+ });
+});
diff --git a/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts
index c3ab3ba854..0cda0dbc2b 100644
--- a/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts
+++ b/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts
@@ -3454,19 +3454,23 @@ export const hooksTrajectoryEnd: MCPTool = {
} catch { /* intelligence module not loadable β€” keep sona-only behaviour */ }
}
- // #2351: when an agent calls trajectory-end with no recorded steps but a
- // non-empty `feedback` string, the feedback was previously dropped on the
- // floor β€” `patternsExtracted` reported 0 and `pattern-search` never
- // surfaced it. Step-less trajectories are the common case for LLM agents
- // (nothing forces step logging mid-task), and feedback is often the most
- // distilled lesson available. Route it through the same store + embed
- // path that pattern-store uses so it becomes searchable. Best-effort:
- // failures here must not turn the trajectory-end call itself into a
- // failure β€” the trajectory record was already persisted above.
+ // #2351: when an agent calls trajectory-end with a non-empty `feedback`
+ // string, the feedback was previously dropped on the floor β€”
+ // `patternsExtracted` reported 0 and `pattern-search` never surfaced it.
+ // Route it through the same store + embed path that pattern-store uses
+ // so it becomes searchable. Best-effort: failures here must not turn the
+ // trajectory-end call itself into a failure β€” the trajectory record was
+ // already persisted above.
+ //
+ // #3662: #2351's original fix gated this on `!hasSteps` (step-less
+ // trajectories only). The common start -> step -> end path (the one real
+ // agents actually use) therefore recorded SONA state while leaving the
+ // feedback unindexed. Non-empty feedback is promoted regardless of
+ // whether steps were recorded β€” a trajectory's steps and its distilled
+ // feedback are not mutually exclusive evidence.
let feedbackDistilled: { stored: boolean; patternId?: string; controller?: string } = { stored: false };
- const hasSteps = !!trajectory && trajectory.steps.length > 0;
const trimmedFeedback = typeof feedback === 'string' ? feedback.trim() : '';
- if (trajectory && !hasSteps && trimmedFeedback.length > 0) {
+ if (trajectory && trimmedFeedback.length > 0) {
const distilledPatternId = `pattern-feedback-${trajectoryId}-${Date.now()}`;
const patternMetadata: Record<string, unknown> = {
sourceTrajectoryId: trajectoryId,