Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "veryfront",
"version": "0.1.405",
"version": "0.1.406",
"license": "Apache-2.0",
"nodeModulesDir": "auto",
"workspace": [
Expand Down
78 changes: 78 additions & 0 deletions src/agent/hosted-child-fork-run-context.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { assertEquals } from "@std/assert";
import type { ChatMessageMetadata, ChatUiMessageChunk } from "#veryfront/chat/protocol.ts";
import { createHostedChildForkRunContext } from "./hosted-child-fork-run-context.ts";

Deno.test("createHostedChildForkRunContext wires stream mirror state and buffers", async () => {
const chunks: ChatUiMessageChunk<ChatMessageMetadata>[] = [];
const context = createHostedChildForkRunContext({
mirror: {
handleChunk: (chunk) => {
chunks.push(chunk);
},
},
messageId: "message-1",
pendingToolLogContext: {
conversationId: "conversation-1",
parentRunId: "run-1",
description: "Check the app",
},
});

assertEquals(context.streamMirrorContext.durableRunMirror, true);
assertEquals(context.streamMirrorContext.durableMessageId, "message-1");
assertEquals(context.streamMirrorContext.durableReasoningMessageId, "message-1:reasoning");
assertEquals(context.streamMirrorContext.hasStartedStep(), false);
assertEquals(context.streamMirrorContext.hasEmittedProgress(), false);
assertEquals(context.streamState.finalText, "");
assertEquals(context.toolCalls, []);
assertEquals(context.toolResults, []);

context.streamMirrorContext.markDurableStepStarted();
await context.streamMirrorContext.appendDurableMirrorChunk({ type: "start-step" });

assertEquals(context.streamMirrorContext.hasStartedStep(), true);
assertEquals(context.streamMirrorContext.hasEmittedProgress(), true);
assertEquals(chunks, [{ type: "start-step" }]);
});

Deno.test("createHostedChildForkRunContext closes pending tool calls with host logger", async () => {
const chunks: ChatUiMessageChunk<ChatMessageMetadata>[] = [];
const warnings: Array<{ message: string; context: Record<string, unknown> }> = [];
const context = createHostedChildForkRunContext({
mirror: {
handleChunk: (chunk) => {
chunks.push(chunk);
},
},
pendingToolLogContext: {
conversationId: "conversation-1",
parentRunId: "run-1",
description: "Check the app",
},
pendingToolLogWriter: {
warn: (message, logContext) => {
warnings.push({ message, context: logContext });
},
},
});

context.pendingToolLifecycle.upsertPendingToolCall("tool-call-1", {
phase: "awaiting_result",
toolName: "read_file",
input: { path: "README.md" },
});

await context.pendingToolLifecycle.closePendingToolCalls({ kind: "aborted" });

assertEquals(chunks.map((chunk) => chunk.type), ["tool-input-start", "tool-output-error"]);
assertEquals(warnings.length, 1);
assertEquals(warnings[0]?.message, "Closing incomplete child fork tool lifecycles");
assertEquals(warnings[0]?.context, {
conversationId: "conversation-1",
runId: "run-1",
description: "Check the app",
reason: "aborted",
toolCallIds: ["tool-call-1"],
errorMessage: null,
});
});
99 changes: 99 additions & 0 deletions src/agent/hosted-child-fork-run-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import type { ChatMessageMetadata, ChatUiMessageChunk } from "#veryfront/chat/protocol.ts";
import {
createHostedChildMirrorContext,
type HostedChildChunkMirror,
type HostedChildMirrorContext,
} from "./hosted-child-mirror.ts";
import {
createHostedChildPendingToolLifecycle,
createHostedChildPendingToolLifecycleLogger,
type HostedChildPendingToolLifecycleLogContext,
type HostedChildPendingToolLifecycleLogWriter,
} from "./hosted-child-pending-tool-lifecycle.ts";
import type { HostedChildForkPendingToolLifecycle } from "./hosted-child-fork-stream-execution.ts";

export interface HostedChildForkToolCallSnapshot {
toolName: string;
toolCallId: string;
input?: unknown;
}

export interface HostedChildForkToolResultSnapshot {
toolName: string;
toolCallId: string;
input: unknown;
output: unknown;
}

export interface HostedChildForkStreamState {
finalText: string;
}

export interface HostedChildForkStreamMirrorContext {
durableRunMirror: boolean;
durableMessageId: string | null;
durableReasoningMessageId: string | null;
durableMirrorState: HostedChildMirrorContext["state"];
appendDurableMirrorChunk: (chunk: ChatUiMessageChunk<ChatMessageMetadata>) => Promise<void>;
closeDurableMirrorReasoning: () => Promise<void>;
closeDurableMirrorText: () => Promise<void>;
markDurableStepStarted: () => void;
hasStartedStep: () => boolean;
hasEmittedProgress: () => boolean;
}

export interface HostedChildForkRunContext {
mirrorContext: HostedChildMirrorContext;
streamMirrorContext: HostedChildForkStreamMirrorContext;
pendingToolLifecycle: HostedChildForkPendingToolLifecycle;
toolCalls: HostedChildForkToolCallSnapshot[];
toolResults: HostedChildForkToolResultSnapshot[];
streamState: HostedChildForkStreamState;
}

export interface HostedChildForkRunContextInput {
mirror: HostedChildChunkMirror | null;
messageId?: string | null;
reasoningMessageId?: string | null;
pendingToolLogContext: HostedChildPendingToolLifecycleLogContext;
pendingToolLogWriter?: HostedChildPendingToolLifecycleLogWriter;
}

export function createHostedChildForkRunContext(
input: HostedChildForkRunContextInput,
): HostedChildForkRunContext {
const mirrorContext = createHostedChildMirrorContext({
mirror: input.mirror,
messageId: input.messageId,
reasoningMessageId: input.reasoningMessageId,
});
const streamMirrorContext: HostedChildForkStreamMirrorContext = {
durableRunMirror: Boolean(mirrorContext.mirror),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Require a message id before enabling the durable mirror

When this helper is used to feed executeHostedChildForkStream with a mirror but without the optional messageId, this sets durableRunMirror to true and the executor immediately mirrors start-step, making hasEmittedProgress() true. Subsequent text chunks are not mirrored because handleHostedChildForkStreamPart only emits text when durableMessageId is present, and final fallback chunks are also skipped because progress was already emitted, so the mirrored child run can end up with no assistant text. Either require/generate a message id for mirrored runs or do not enable the durable mirror without one.

Useful? React with 👍 / 👎.

durableMessageId: mirrorContext.messageId,
durableReasoningMessageId: mirrorContext.reasoningMessageId,
durableMirrorState: mirrorContext.state,
appendDurableMirrorChunk: mirrorContext.appendChunk,
closeDurableMirrorReasoning: mirrorContext.closeReasoningSegment,
closeDurableMirrorText: mirrorContext.closeTextSegment,
markDurableStepStarted: mirrorContext.markStepStarted,
hasStartedStep: mirrorContext.hasStartedStep,
hasEmittedProgress: mirrorContext.hasEmittedProgress,
};

return {
mirrorContext,
streamMirrorContext,
pendingToolLifecycle: createHostedChildPendingToolLifecycle({
appendMirrorChunk: streamMirrorContext.appendDurableMirrorChunk,
logger: input.pendingToolLogWriter
? createHostedChildPendingToolLifecycleLogger(
input.pendingToolLogContext,
input.pendingToolLogWriter,
)
: undefined,
}),
toolCalls: [],
toolResults: [],
streamState: { finalText: "" },
};
}
9 changes: 9 additions & 0 deletions src/agent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,15 @@ export {
HOSTED_CHILD_FORK_INSTRUCTIONS_BASE,
type HostedChildForkInstructionsContext,
} from "./hosted-child-fork-instructions.ts";
export {
createHostedChildForkRunContext,
type HostedChildForkRunContext,
type HostedChildForkRunContextInput,
type HostedChildForkStreamMirrorContext,
type HostedChildForkStreamState,
type HostedChildForkToolCallSnapshot,
type HostedChildForkToolResultSnapshot,
} from "./hosted-child-fork-run-context.ts";
export {
executeHostedChildForkStream,
type ExecuteHostedChildForkStreamInput,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/version-constant.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Keep in sync with deno.json version.
// scripts/release.ts updates this constant during releases.
export const VERSION = "0.1.405";
export const VERSION = "0.1.406";
Loading