Skip to content

Commit 91f8c35

Browse files
fix(server): Claude workflow members stream their thinking and tool calls into their threads (pingdotgg#20856)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent e1f33e0 commit 91f8c35

7 files changed

Lines changed: 1170 additions & 515 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 527 additions & 130 deletions
Large diffs are not rendered by default.

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 428 additions & 176 deletions
Large diffs are not rendered by default.

‎apps/server/src/orchestration-v2/workflowAgentAnswers.test.ts‎

Lines changed: 0 additions & 117 deletions
This file was deleted.

‎apps/server/src/orchestration-v2/workflowAgentAnswers.ts‎

Lines changed: 0 additions & 91 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// @effect-diagnostics nodeBuiltinImport:off
2+
import * as NodeFS from "node:fs";
3+
import * as NodePath from "node:path";
4+
import * as NodeServices from "@effect/platform-node/NodeServices";
5+
import { it } from "@effect/vitest";
6+
import * as Effect from "effect/Effect";
7+
import * as FileSystem from "effect/FileSystem";
8+
import { assert } from "vite-plus/test";
9+
10+
import {
11+
initialWorkflowAgentTranscriptCursor,
12+
readWorkflowAgentTranscript,
13+
} from "./workflowAgentTranscript.ts";
14+
15+
const line = (value: unknown) => `${JSON.stringify(value)}\n`;
16+
const assistant = (uuid: string, text: string) =>
17+
line({
18+
type: "assistant",
19+
uuid,
20+
message: { role: "assistant", id: uuid, content: [{ type: "text", text }] },
21+
});
22+
const uuids = (lines: ReadonlyArray<unknown>) =>
23+
lines.map((entry) => (entry as { readonly uuid: string }).uuid);
24+
25+
it.layer(NodeServices.layer)("readWorkflowAgentTranscript", (it) => {
26+
it.effect("reads complete lines from its cursor and starts over on a new file", () =>
27+
Effect.gen(function* () {
28+
const fileSystem = yield* FileSystem.FileSystem;
29+
const configDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "wf-transcript-" });
30+
const transcriptDir = NodePath.join(configDir, "projects", "test");
31+
yield* fileSystem.makeDirectory(transcriptDir, { recursive: true });
32+
const file = NodePath.join(transcriptDir, "agent-a1.jsonl");
33+
const read = (cursor = initialWorkflowAgentTranscriptCursor) =>
34+
readWorkflowAgentTranscript({ transcriptDir, agentId: "a1", cursor, configDir });
35+
36+
const bookkeeping =
37+
line({ type: "attachment", uuid: "att", attachment: { type: "skill_listing" } }) +
38+
line({ type: "user", uuid: "meta", isMeta: true, message: { content: "context" } });
39+
const second = assistant("b", "second");
40+
NodeFS.writeFileSync(file, bookkeeping + assistant("a", "first") + second.slice(0, 20));
41+
const first = yield* read();
42+
assert.deepEqual(uuids(first.lines), ["a"]);
43+
assert.equal(
44+
first.cursor.offset,
45+
Buffer.byteLength(bookkeeping + assistant("a", "first")),
46+
"a half-written line is left for the next read",
47+
);
48+
49+
NodeFS.appendFileSync(file, second.slice(20));
50+
const next = yield* read(first.cursor);
51+
assert.deepEqual(uuids(next.lines), ["b"]);
52+
assert.equal(next.cursor.offset, NodeFS.statSync(file).size);
53+
54+
NodeFS.rmSync(file);
55+
NodeFS.writeFileSync(file, assistant("c", "replaced"));
56+
const replaced = yield* read(next.cursor);
57+
assert.deepEqual(uuids(replaced.lines), ["c"], "a new inode reads from 0");
58+
59+
NodeFS.writeFileSync(file, assistant("d", "x"));
60+
assert.equal(NodeFS.statSync(file).ino, replaced.cursor.identity?.ino);
61+
const shrunk = yield* read(replaced.cursor);
62+
assert.deepEqual(uuids(shrunk.lines), ["d"], "a shrunk file reads from 0");
63+
}).pipe(Effect.scoped),
64+
);
65+
});
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// @effect-diagnostics nodeBuiltinImport:off
2+
/**
3+
* A workflow member never becomes a provider task and the CLI forwards none of
4+
* its messages, so the transcript the harness writes is the only record of its
5+
* conversation. The path is derived here from the run's transcript directory
6+
* and the member's provider id rather than accepted from the caller, and the
7+
* read itself is contained by readContainedWorkflowFile.
8+
*/
9+
import * as NodePath from "node:path";
10+
11+
import { OrchestrationWorkflowFileError } from "@t3tools/contracts";
12+
import * as Context from "effect/Context";
13+
import * as Effect from "effect/Effect";
14+
import * as Option from "effect/Option";
15+
import * as Schema from "effect/Schema";
16+
17+
import { field, text } from "./unknownField.ts";
18+
import { readContainedWorkflowFile, type WorkflowFileIdentity } from "./workflowFileRead.ts";
19+
20+
const TRANSCRIPT_READ_BYTE_CAP = 512 * 1024;
21+
22+
const AGENT_ID_PATTERN = /^[A-Za-z0-9_-]{1,128}$/;
23+
24+
const decodeJsonLine = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Unknown));
25+
26+
export interface WorkflowAgentTranscriptCursor {
27+
readonly offset: number;
28+
readonly identity: WorkflowFileIdentity | null;
29+
}
30+
31+
export const initialWorkflowAgentTranscriptCursor: WorkflowAgentTranscriptCursor = {
32+
offset: 0,
33+
identity: null,
34+
};
35+
36+
/** Sees every transcript read: where it started and ended, and how many lines it kept. */
37+
export const WorkflowAgentTranscriptReadObserver = Context.Reference<
38+
(read: {
39+
readonly agentId: string;
40+
readonly startOffset: number;
41+
readonly endOffset: number;
42+
readonly lineCount: number;
43+
}) => Effect.Effect<void>
44+
>("t3/orchestration/WorkflowAgentTranscriptReadObserver", {
45+
defaultValue: () => () => Effect.void,
46+
});
47+
48+
/**
49+
* Conversation lines only: `attachment` lines and `isMeta` user lines are
50+
* harness bookkeeping (hook output, skill listings, injected context), and a
51+
* line without a uuid cannot be projected idempotently.
52+
*/
53+
function parseWorkflowAgentTranscriptLines(contents: string): ReadonlyArray<unknown> {
54+
return contents.split("\n").flatMap((line) => {
55+
if (line.trim().length === 0) return [];
56+
const entry = Option.getOrUndefined(decodeJsonLine(line));
57+
const type = text(field(entry, "type"));
58+
if (type !== "assistant" && type !== "user") return [];
59+
if (field(entry, "isMeta") === true || text(field(entry, "uuid")) === undefined) return [];
60+
return [entry];
61+
});
62+
}
63+
64+
/** Reads the complete lines written since `cursor`, and the cursor after them. */
65+
export const readWorkflowAgentTranscript = Effect.fn("orchestration.readWorkflowAgentTranscript")(
66+
function* (input: {
67+
readonly transcriptDir: string;
68+
readonly agentId: string;
69+
readonly cursor: WorkflowAgentTranscriptCursor;
70+
readonly configDir?: string;
71+
}) {
72+
if (!AGENT_ID_PATTERN.test(input.agentId)) {
73+
return yield* new OrchestrationWorkflowFileError({
74+
reason: "invalid-path",
75+
path: input.agentId,
76+
});
77+
}
78+
const file = yield* readContainedWorkflowFile({
79+
path: NodePath.join(input.transcriptDir, `agent-${input.agentId}.jsonl`),
80+
extension: ".jsonl",
81+
byteCap: TRANSCRIPT_READ_BYTE_CAP,
82+
fromOffset: input.cursor,
83+
...(input.configDir === undefined ? {} : { configDir: input.configDir }),
84+
});
85+
const lines = parseWorkflowAgentTranscriptLines(file.contents);
86+
yield* (yield* WorkflowAgentTranscriptReadObserver)({
87+
agentId: input.agentId,
88+
startOffset: file.startOffset,
89+
endOffset: file.endOffset,
90+
lineCount: lines.length,
91+
});
92+
return {
93+
lines,
94+
cursor: { offset: file.endOffset, identity: file.identity },
95+
// More complete lines may follow past the per-read cap.
96+
truncated: file.truncated,
97+
};
98+
},
99+
);

0 commit comments

Comments
 (0)