Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(agents): keep the session list refresh's row reads in one trace (#8764)

gbirmancd4512786264
diff --git a/apps/web/src/lib/queries/agent-session/list-sync.test.ts b/apps/web/src/lib/queries/agent-session/list-sync.test.ts
index 46df71c6a8..e015a1c437 100644
--- a/apps/web/src/lib/queries/agent-session/list-sync.test.ts
+++ b/apps/web/src/lib/queries/agent-session/list-sync.test.ts
@@ -21,8 +21,42 @@ const mocks = vi.hoisted(() => ({
held: vi.fn((_id: string) => false),
cached: vi.fn((_id: string): unknown => undefined),
hydrate: vi.fn(),
+ spans: [] as {
+ name: string;
+ parent: string | null;
+ attrs: Record<string, unknown>;
+ }[],
}));
+/** Records each span with the span it was started from; never an ambient one. */
+vi.mock('@macro-inc/observability', () => {
+ type Recorded = (typeof mocks.spans)[number];
+ const start = (name: string, parent: Recorded | null) => {
+ const record: Recorded = { name, parent: parent?.name ?? null, attrs: {} };
+ mocks.spans.push(record);
+ const span = {
+ setAttr: (key: string, value: unknown) => {
+ record.attrs[key] = value;
+ },
+ error: () => {},
+ span: (child: string, operation: (span: unknown) => Promise<unknown>) =>
+ run(child, record, operation),
+ };
+ return span;
+ };
+ const run = (
+ name: string,
+ parent: Recorded | null,
+ operation: (span: unknown) => Promise<unknown>
+ ) => operation(start(name, parent));
+ return {
+ Telemetry: {
+ span: (name: string, operation: (span: unknown) => Promise<unknown>) =>
+ run(name, null, operation),
+ },
+ };
+});
+
vi.mock('@queries/soup/refresh', () => ({
refreshSoupEntities: mocks.rest,
}));
@@ -62,6 +96,7 @@ import { invalidateAgentSessionMetadata } from './session-metadata-sync';
beforeEach(() => {
vi.clearAllMocks();
+ mocks.spans.length = 0;
mocks.graphqlSoup.mockReturnValue(false);
mocks.held.mockReturnValue(false);
});
@@ -167,6 +202,31 @@ it('lets every row read settle before retrying the batch', async () => {
expect(mocks.row).toHaveBeenCalledTimes(4);
});
+it('keeps the refetch and every row read in the pass trace', async () => {
+ mocks.graphqlSoup.mockReturnValue(true);
+ mocks.row.mockResolvedValue({
+ data: { user: { agentSession: { id: 'first', turnState: 'idle' } } },
+ });
+ await Promise.all([
+ refreshAgentSessionLists('first'),
+ refreshAgentSessionLists('second'),
+ ]);
+
+ expect(mocks.spans.map(({ name, parent }) => ({ name, parent }))).toEqual([
+ { name: 'agent_session.lists_refresh', parent: null },
+ {
+ name: 'agent_session.lists_refetch',
+ parent: 'agent_session.lists_refresh',
+ },
+ { name: 'agent_session.row_read', parent: 'agent_session.lists_refresh' },
+ { name: 'agent_session.row_read', parent: 'agent_session.lists_refresh' },
+ ]);
+ expect(mocks.spans[2].attrs).toMatchObject({
+ 'agent_session.id': 'first',
+ 'agent_session.turn_state': 'idle',
+ });
+});
+
it('leaves soup lists alone when the gateway socket opens', async () => {
await invalidateAgentSessionMetadata();
expect(mocks.rest).not.toHaveBeenCalled();
diff --git a/apps/web/src/lib/queries/agent-session/list-sync.ts b/apps/web/src/lib/queries/agent-session/list-sync.ts
index 5dbd9b7d81..4b7991e599 100644
--- a/apps/web/src/lib/queries/agent-session/list-sync.ts
+++ b/apps/web/src/lib/queries/agent-session/list-sync.ts
@@ -3,7 +3,7 @@ import {
isFeatureEnabled,
} from '@core/constant/featureFlags';
import { fireAndForget } from '@core/util/fireAndForget';
-import { Telemetry } from '@macro-inc/observability';
+import { type Span, Telemetry } from '@macro-inc/observability';
import {
getSoupEntityById,
hasSoupEntity,
@@ -21,8 +21,12 @@ const changedSessions = new Set<string>();
let pending: Promise<void> | undefined;
/** Read one session's row from the primary and apply it to the lists. */
-function readSessionRow(client: Client, sessionId: string): Promise<void> {
- return Telemetry.span('agent_session.row_read', async (span) => {
+function readSessionRow(
+ parent: Span,
+ client: Client,
+ sessionId: string
+): Promise<void> {
+ return parent.span('agent_session.row_read', async (span) => {
span.setAttr('agent_session.id', sessionId);
const result = await client
.query(
@@ -59,29 +63,36 @@ function readSessionRow(client: Client, sessionId: string): Promise<void> {
* Every read settles before a failure reaches the retry, so a late read cannot
* land after the retry's newer one.
*/
-async function readSessionRows(sessions: string[]): Promise<void> {
+async function readSessionRows(
+ parent: Span,
+ sessions: string[]
+): Promise<void> {
const graphqlLists = isFeatureEnabled(enableGraphqlSoup);
const held = sessions.filter((id) => graphqlLists || hasSoupEntity(id));
if (held.length === 0) return;
const client = getGraphqlSoupClient();
const reads = await Promise.allSettled(
- held.map((sessionId) => readSessionRow(client, sessionId))
+ held.map((sessionId) => readSessionRow(parent, client, sessionId))
);
const failed = reads.find((read) => read.status === 'rejected');
if (failed?.status === 'rejected') throw failed.reason;
}
-/** One pass over a batch: the REST list refetch, then the primary row reads. */
+/**
+ * One pass over a batch: the REST list refetch, then the primary row reads.
+ * The phases start from the pass's span, not the ambient context, which does
+ * not survive an `await`.
+ */
function refreshBatch(sessions: string[], attempt: number): Promise<void> {
return Telemetry.span('agent_session.lists_refresh', async (span) => {
span.setAttr('agent_session.ids', sessions.join(','));
span.setAttr('agent_session.count', sessions.length);
span.setAttr('agent_session.lists_refresh.attempt', attempt);
try {
- await Telemetry.span('agent_session.lists_refetch', () =>
+ await span.span('agent_session.lists_refetch', () =>
refreshSoupEntities(sessions, { throwOnError: true })
);
- await readSessionRows(sessions);
+ await readSessionRows(span, sessions);
} catch (error) {
span.error(error);
throw error;