Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(server): prefer to use tool call id first (#14322)

Nekof390d04ef249
diff --git a/src/server/services/agentSignal/procedure/__tests__/emitToolOutcome.test.ts b/src/server/services/agentSignal/procedure/__tests__/emitToolOutcome.test.ts
index 3feef8b17a0..c0b1c3ffca4 100644
--- a/src/server/services/agentSignal/procedure/__tests__/emitToolOutcome.test.ts
+++ b/src/server/services/agentSignal/procedure/__tests__/emitToolOutcome.test.ts
@@ -69,6 +69,65 @@ describe('recordToolOutcome', () => {
);
});
+ /**
+ * @example
+ * recordToolOutcome({ operationId: 'op1', toolCallId: 'call1' }) keeps each call distinct.
+ */
+ it('uses tool call id before operation id for per-call outcome identity', async () => {
+ const store = createStore();
+ const baseInput = {
+ apiName: 'addPreferenceMemory',
+ context: { userId: 'u1' },
+ domainKey: 'memory:user-preference',
+ identifier: 'lobe-user-memory',
+ intentClass: 'explicit_persistence',
+ messageId: 'm1',
+ operationId: 'op1',
+ policyStateStore: store,
+ scope: { topicId: 't1', userId: 'u1' },
+ scopeKey: 'topic:t1',
+ status: 'succeeded' as const,
+ toolAction: 'create',
+ ttlSeconds: 3600,
+ };
+
+ // ROOT CAUSE:
+ //
+ // If operation id wins over tool call id, two calls to the same tool API in one operation
+ // produce the same source id. Records and receipts use that source id as their hash field,
+ // so the second call overwrites the first projection.
+ //
+ // We keep procedure suppression message-scoped, but make outcome source identity per-call.
+ const first = await recordToolOutcome({
+ ...baseInput,
+ summary: 'Saved first preference.',
+ toolCallId: 'call1',
+ });
+ const second = await recordToolOutcome({
+ ...baseInput,
+ summary: 'Saved second preference.',
+ toolCallId: 'call2',
+ });
+
+ expect(first.sourceId).not.toBe(second.sourceId);
+
+ const records = await store.readPolicyState(PROCEDURE_RECORDS_POLICY_ID, 'topic:t1');
+ const receipts = await store.readPolicyState(PROCEDURE_RECEIPTS_POLICY_ID, 'topic:t1');
+ expect(Object.keys(records ?? {}).filter((key) => key.startsWith('record:'))).toHaveLength(2);
+ expect(Object.keys(receipts ?? {}).filter((key) => key.startsWith('receipt:'))).toHaveLength(2);
+
+ const markerKey = buildProcedureMarkerKey({
+ domainKey: 'memory:user-preference',
+ intentClass: 'explicit_persistence',
+ procedureKey: 'message:m1',
+ scopeKey: 'topic:t1',
+ });
+
+ await expect(store.readPolicyState(PROCEDURE_MARKER_POLICY_ID, markerKey)).resolves.toEqual({
+ marker: expect.stringContaining('message:m1'),
+ });
+ });
+
/**
* @example
* emitToolOutcomeSafely(input) logs but does not reject when procedure storage fails.
diff --git a/src/server/services/agentSignal/procedure/emitToolOutcome.ts b/src/server/services/agentSignal/procedure/emitToolOutcome.ts
index fd3453a9a52..3185d7a57d5 100644
--- a/src/server/services/agentSignal/procedure/emitToolOutcome.ts
+++ b/src/server/services/agentSignal/procedure/emitToolOutcome.ts
@@ -56,7 +56,7 @@ export interface EmitToolOutcomeInput {
*
* Use when:
* - Direct tool execution writes procedure state before async enqueue
- * - Idempotent source identity should prefer structured ids
+ * - Idempotent source identity should prefer the per-call tool id before broader operation ids
*
* Expects:
* - Natural-language text is not used for identity
@@ -66,8 +66,8 @@ export interface EmitToolOutcomeInput {
*/
export const createToolOutcomeSourceId = (input: EmitToolOutcomeInput) => {
const stableId =
- input.operationId ??
input.toolCallId ??
+ input.operationId ??
input.messageId ??
`${input.scopeKey}:${input.identifier}:${input.apiName ?? 'unknown'}:${input.toolAction ?? 'unknown'}`;