Q15: show which messages each agent actually read
Runs record a context manifest once a model turn succeeds; the chat marks each human message with the agents that read its current version. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
592ce69219
commit
469f2f2c35
14 files changed
+144
-10
No files matched your search
@@ -19,6 +19,8 @@ export interface AgentRunLoopInput {
|
||||
externalTools?: ToolDefinition[];
|
||||
/** Messages that arrived mid-run, or null. */
|
||||
pollNewMessages?: () => string | null;
|
||||
/** Called after each model turn the provider answered. */
|
||||
onModelTurn?: () => void;
|
||||
}
|
||||
|
||||
export interface AgentRunLoopResult {
|
||||
@@ -57,6 +59,7 @@ export async function runAgentLoop(deps: AgentRunLoopDeps, input: AgentRunLoopIn
|
||||
tools: allTools,
|
||||
});
|
||||
lastRequestId = turn.requestId;
|
||||
input.onModelTurn?.();
|
||||
|
||||
if (turn.toolCalls.length === 0) {
|
||||
if (toolCallCount === 0 && !turn.text.trim()) {
|
||||
|
||||
@@ -50,9 +50,44 @@ export function loadChatWakeContext(db: DatabaseSync, orgId: string, agentId: st
|
||||
spaceId: thread.space_id,
|
||||
history: [...(omitted > 0 ? [`(${omitted} earlier messages in this thread are not shown.)`] : []), ...history.map((m) => formatMessage(db, m))].join("\n"),
|
||||
fresh: fresh.map((m) => formatMessage(db, m)).join("\n"),
|
||||
manifest: { history: history.map(readEntry), fresh: fresh.map(readEntry), omittedCount: omitted },
|
||||
};
|
||||
}
|
||||
|
||||
/** A message as the model saw it: id plus edit-or-create time. */
|
||||
export interface ReadEntry {
|
||||
id: string;
|
||||
version: string;
|
||||
}
|
||||
|
||||
/** What a run read: thread messages up front plus anything injected mid-run. */
|
||||
export interface RunContextManifest {
|
||||
threadId: string | null;
|
||||
history: ReadEntry[];
|
||||
fresh: ReadEntry[];
|
||||
omittedCount: number;
|
||||
midRun: ReadEntry[];
|
||||
}
|
||||
|
||||
/** Collects what the run read; commit() persists it once a model turn succeeds. */
|
||||
export function createRunContextRecorder(db: DatabaseSync, runId: string, chatContext: ChatContextBlock | null) {
|
||||
const manifest: RunContextManifest = {
|
||||
threadId: chatContext?.threadId ?? null,
|
||||
history: chatContext?.manifest.history ?? [],
|
||||
fresh: chatContext?.manifest.fresh ?? [],
|
||||
omittedCount: chatContext?.manifest.omittedCount ?? 0,
|
||||
midRun: [],
|
||||
};
|
||||
return {
|
||||
addMidRun: (entries: ReadEntry[]) => { manifest.midRun.push(...entries); },
|
||||
commit: () => { db.prepare("update task_runs set context_json = ? where id = ?").run(JSON.stringify(manifest), runId); },
|
||||
};
|
||||
}
|
||||
|
||||
function readEntry(m: ThreadMessageRow): ReadEntry {
|
||||
return { id: m.id, version: m.edited_at ?? m.created_at };
|
||||
}
|
||||
|
||||
function formatMessage(db: DatabaseSync, m: ThreadMessageRow): string {
|
||||
const who = m.sender_role ? `${m.sender_name} (${m.sender_type}, ${m.sender_role})` : `${m.sender_name} (${m.sender_type})`;
|
||||
const edited = m.edited_at ? ` (edited ${utcMinute(m.edited_at)})` : "";
|
||||
@@ -66,7 +101,7 @@ function utcMinute(iso: string): string {
|
||||
/** Polls the threads this run is part of for messages from others. */
|
||||
export function createRunMessagePoller(
|
||||
db: DatabaseSync,
|
||||
input: { orgId: string; agentId: string; sourceThreadId: string | null; runStartedAt: string },
|
||||
input: { orgId: string; agentId: string; sourceThreadId: string | null; runStartedAt: string; onInjected: (entries: ReadEntry[]) => void },
|
||||
): () => string | null {
|
||||
let watermark = input.runStartedAt;
|
||||
return () => {
|
||||
@@ -81,6 +116,7 @@ export function createRunMessagePoller(
|
||||
`).all(input.orgId, watermark, input.agentId, input.sourceThreadId, input.orgId, input.agentId, input.runStartedAt) as unknown as ThreadMessageRow[];
|
||||
if (rows.length === 0) return null;
|
||||
watermark = rows[rows.length - 1].created_at;
|
||||
input.onInjected(rows.map(readEntry));
|
||||
console.info(`[run-inbox] agent ${input.agentId}: ${rows.length} new message(s) injected mid-run`);
|
||||
return ["Messages that arrived while you were working (they may change what you should do):", ...rows.map((m) => formatMessage(db, m))].join("\n");
|
||||
};
|
||||
|
||||
@@ -24,7 +24,7 @@ import {
|
||||
type WorkspaceRow,
|
||||
} from "./task-prompts";
|
||||
import { renderTaskHandoff } from "./agent-task-handoff";
|
||||
import { createRunMessagePoller, loadChatWakeContext } from "./chat-wake-context";
|
||||
import { createRunContextRecorder, createRunMessagePoller, loadChatWakeContext } from "./chat-wake-context";
|
||||
import { runningWorkspaceWriter } from "./workspace-write-lock";
|
||||
import { loadComposioTools } from "./composio/composio-tool-bridge";
|
||||
import { connectorDisplayName } from "./composio/connector-brands";
|
||||
@@ -159,6 +159,7 @@ export class TaskExecutionService {
|
||||
// Watermark for reply delivery and mid-run message polling.
|
||||
const runStartedAt = new Date().toISOString();
|
||||
const chatContext = loadChatWakeContext(this.db, orgId, agent.id, isolation.secretScopeJson);
|
||||
const contextRecorder = createRunContextRecorder(this.db, runId, chatContext);
|
||||
const toolContext: AgentToolContext = {
|
||||
orgId,
|
||||
agent: { id: agent.id, modelProviderId: agent.model_provider_id, modelId: agent.model_id },
|
||||
@@ -206,7 +207,8 @@ export class TaskExecutionService {
|
||||
}),
|
||||
toolContext,
|
||||
externalTools,
|
||||
pollNewMessages: createRunMessagePoller(this.db, { orgId, agentId: agent.id, sourceThreadId: chatContext?.threadId ?? null, runStartedAt }),
|
||||
onModelTurn: contextRecorder.commit,
|
||||
pollNewMessages: createRunMessagePoller(this.db, { orgId, onInjected: contextRecorder.addMidRun, agentId: agent.id, sourceThreadId: chatContext?.threadId ?? null, runStartedAt }),
|
||||
},
|
||||
);
|
||||
providerRequestId = result.lastRequestId;
|
||||
|
||||
@@ -76,6 +76,7 @@ export interface ChatContextBlock {
|
||||
history: string;
|
||||
/** Messages after it: what this run answers. */
|
||||
fresh: string;
|
||||
manifest: { history: Array<{ id: string; version: string }>; fresh: Array<{ id: string; version: string }>; omittedCount: number };
|
||||
}
|
||||
|
||||
export interface AgentInstructionContext {
|
||||
|
||||
Reference in new issue
Block a user