Skip to content

Commit e44adde

Browse files
authored
fix(agent): avoid duplicate "missing artifact" warning on the pre-refetch resolve
Review feedback (Greptile): getArtifactsById logs a per-id warning for each unresolved id, and the new refetch path calls it twice, so a genuinely missing artifact produced two identical low-level warnings on top of the high-level summary. Add a warnOnMissing option (default true) and pass false on the speculative pre-refetch resolve, where a miss is expected and is exactly what triggers the refetch. Generated-By: PostHog Code Task-Id: 62e30413-5b19-4752-9520-b0e772c462ce
1 parent e83b0ef commit e44adde

2 files changed

Lines changed: 52 additions & 4 deletions

File tree

packages/agent/src/server/agent-server.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2284,4 +2284,44 @@ describe("AgentServer pending user attachments", () => {
22842284
expect(result).toBeNull();
22852285
expect(getTaskRun).not.toHaveBeenCalled();
22862286
});
2287+
2288+
it("warns once (not twice) about a missing artifact across the speculative and post-refetch resolves", async () => {
2289+
const internals = buildInternals();
2290+
// A non-empty manifest that never lists the requested id — so getArtifactsById
2291+
// reaches its per-id "missing" warning on both the pre- and post-refetch calls
2292+
// (an empty manifest would short-circuit before warning at all).
2293+
const decoyManifest = [
2294+
{
2295+
id: "unrelated-artifact",
2296+
name: "other.txt",
2297+
type: "user_attachment" as const,
2298+
},
2299+
];
2300+
internals.posthogAPI.getTaskRun = vi.fn(async () =>
2301+
createTaskRun({
2302+
state: { pending_user_artifact_ids: ["missing-attachment"] },
2303+
artifacts: decoyManifest,
2304+
}),
2305+
);
2306+
const loggerHost = internals as unknown as {
2307+
logger: { warn: (...args: unknown[]) => void };
2308+
};
2309+
const warnSpy = vi
2310+
.spyOn(loggerHost.logger, "warn")
2311+
.mockImplementation(() => {});
2312+
2313+
await internals.getPendingUserPrompt(
2314+
createTaskRun({
2315+
state: { pending_user_artifact_ids: ["missing-attachment"] },
2316+
artifacts: decoyManifest,
2317+
}),
2318+
);
2319+
2320+
// The speculative pre-refetch resolve stays quiet (a miss there is expected);
2321+
// only the post-refetch resolve emits the per-id "missing" warning.
2322+
const manifestWarnings = warnSpy.mock.calls.filter(
2323+
([message]) => message === "Pending artifact missing from run manifest",
2324+
);
2325+
expect(manifestWarnings).toHaveLength(1);
2326+
});
22872327
});

packages/agent/src/server/agent-server.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,7 +1772,9 @@ export class AgentServer {
17721772
// a transient gap doesn't drop the attachment and send the agent the bare
17731773
// "Attached files: …" description instead of the file it was promised.
17741774
let manifest = taskRun.artifacts ?? [];
1775-
let resolvedArtifacts = this.getArtifactsById(manifest, artifactIds);
1775+
let resolvedArtifacts = this.getArtifactsById(manifest, artifactIds, {
1776+
warnOnMissing: false,
1777+
});
17761778
if (
17771779
artifactIds.length > 0 &&
17781780
resolvedArtifacts.length < artifactIds.length
@@ -1924,6 +1926,10 @@ export class AgentServer {
19241926
private getArtifactsById(
19251927
artifacts: TaskRunArtifact[] | undefined,
19261928
artifactIds: string[],
1929+
// The speculative pre-refetch resolve passes false: a miss there is expected
1930+
// (it's what triggers the refetch), so warning would be premature and would
1931+
// double up with the post-refetch warning for a genuinely missing artifact.
1932+
{ warnOnMissing = true }: { warnOnMissing?: boolean } = {},
19271933
): TaskRunArtifact[] {
19281934
if (!artifacts?.length || artifactIds.length === 0) {
19291935
return [];
@@ -1941,9 +1947,11 @@ export class AgentServer {
19411947
return artifactIds.flatMap((artifactId) => {
19421948
const artifact = artifactsById.get(artifactId);
19431949
if (!artifact) {
1944-
this.logger.warn("Pending artifact missing from run manifest", {
1945-
artifactId,
1946-
});
1950+
if (warnOnMissing) {
1951+
this.logger.warn("Pending artifact missing from run manifest", {
1952+
artifactId,
1953+
});
1954+
}
19471955
return [];
19481956
}
19491957

0 commit comments

Comments
 (0)