Skip to content
Closed
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
39 changes: 39 additions & 0 deletions apps/mobile/src/lib/threadActivity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,45 @@ describe("buildThreadFeed", () => {
label: "Worked for 2s",
});

const hook: OrchestrationV2TurnItem = {
...base("stop-hook", "2026-06-20T00:00:02.800Z", 4),
type: "notification",
source: { kind: "system" },
outcome: "updated",
summary: "Stop hook asked Claude to continue",
detail: "[./check.sh]: Run the formatter before finishing.",
};
const secondAnswer = {
...assistantMessage("2026-06-20T00:00:02.900Z"),
id: TurnItemId.make("item-second"),
messageId: MessageId.make("message-second"),
text: "The formatter has run.",
};
const secondHook = {
...hook,
...base("stop-hook-2", "2026-06-20T00:00:02.950Z", 6),
};
const hookFeed = buildThreadFeed([
projected(userMessage(), 0),
projected(opening, 1),
projected(middle, 2),
projected(command("2026-06-20T00:00:02.600Z"), 3),
projected(hook, 4),
projected(secondAnswer, 5),
projected(secondHook, 6),
projected(assistantMessage(), 7),
]);
const hookRows = deriveThreadFeedPresentation(hookFeed, latestRun, new Set());
expect(hookRows.map((entry) => entry.id)).toEqual([
"message-user",
"run-fold:run-1",
"message-middle",
"local:thread-1:stop-hook",
"message-second",
"local:thread-1:stop-hook-2",
"message-assistant",
]);

const expanded = deriveThreadFeedPresentation(feed, latestRun, new Set([runId]));
expect(expanded.map((entry) => entry.type)).toEqual([
"message",
Expand Down
19 changes: 18 additions & 1 deletion apps/mobile/src/lib/threadActivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ import type {
ScheduledTaskId,
TurnItemId,
} from "@t3tools/contracts";
import { idleCompactionOf, isCompactionCommandMessage, RunId, ThreadId } from "@t3tools/contracts";
import {
CLAUDE_STOP_HOOK_CONTINUATION_SUMMARY,
idleCompactionOf,
isCompactionCommandMessage,
RunId,
ThreadId,
} from "@t3tools/contracts";
import {
classifyToolActivity,
collectToolFilePaths,
Expand Down Expand Up @@ -1067,6 +1073,7 @@ function deriveThreadFeedRunFolds(
feedFailedRunIds: ReadonlySet<RunId>,
): ReadonlyMap<string, ThreadFeedRunFold> {
const terminalAssistantMessageIdByRun = new Map<RunId, string>();
const stopHookAssistantMessageIds = new Set<string>();
const interruptedRunIds = new Set<RunId>();
const failedRunIds = new Set(feedFailedRunIds);
const promptIndexByRunId = new Map<RunId, number>();
Expand Down Expand Up @@ -1123,6 +1130,15 @@ function deriveThreadFeedRunFolds(
if (entry.type !== "activity-group") continue;
for (const activity of entry.activities) {
const item = activity.projectedItem.item;
if (
item.type === "notification" &&
item.source.kind === "system" &&
item.summary === CLAUDE_STOP_HOOK_CONTINUATION_SUMMARY &&
item.runId === runId
) {
const answerId = terminalAssistantMessageIdByRun.get(runId);
if (answerId !== undefined) stopHookAssistantMessageIds.add(answerId);
}
if (item.type === "run_interrupt_result") interruptedRunIds.add(runId);
if (
runId === runlessKey &&
Expand Down Expand Up @@ -1153,6 +1169,7 @@ function deriveThreadFeedRunFolds(
.filter(
(entry) =>
entry.id !== terminalAssistantId &&
!stopHookAssistantMessageIds.has(entry.id) &&
entry.type !== "html-render" &&
entry.type !== "mcp-app" &&
!(entry.type === "activity-group" && isContextCompactionActivityGroup(entry)) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15070,7 +15070,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
uuid,
session_id: WAKE_NATIVE_SESSION,
});
const stopHookFeedback = (uuid: string, condition: string, reason: string) =>
const stopHookFeedback = (uuid: string | undefined, condition: string, reason: string) =>
claudeSdkFrame({
type: "user",
message: {
Expand All @@ -15089,6 +15089,61 @@ describe("ClaudeAdapterV2 background wake turns", () => {
: [],
);

it.effect.each([
{ activeGoal: false, uuid: "stop-hook" },
{ activeGoal: true, uuid: "stop-hook" },
{ activeGoal: false, uuid: undefined },
])(
"shows non-goal Stop hook feedback in the current run, active goal=$activeGoal, uuid=$uuid",
({ activeGoal, uuid }) =>
Effect.gen(function* () {
const harness = yield* makeWakeHarness;
const input = makeClaudeTestTurnInput({
threadId: harness.threadId,
providerThread: harness.providerThread,
now: yield* DateTime.now,
attemptId: RunAttemptId.make("stop-hook-attempt"),
text: "Check the changes.",
attachments: [],
});
yield* harness.runtime.startTurn(input);
if (activeGoal) {
yield* Queue.offer(
harness.sdkMessages,
syntheticFrame("goal-set", "Goal set: all tests pass"),
);
}
for (const frame of [
makeAssistantTextFrame({ uuid: "answer", text: "Here is the answer." }),
stopHookFeedback(uuid, "./check.sh", "Run the formatter before finishing."),
makeAssistantTextFrame({ uuid: "reply", text: "The formatter has run." }),
makeResultFrame({ uuid: "result", result: "The formatter has run." }),
]) {
yield* Queue.offer(harness.sdkMessages, frame);
}
assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "completed");
const notices = harness.events.flatMap((event) =>
event.type === "turn_item.updated" && event.turnItem.type === "notification"
? [event.turnItem]
: [],
);
assert.lengthOf(notices, 1);
assert.deepInclude(notices[0], {
type: "notification",
runId: input.runId,
status: "completed",
source: { kind: "system" },
summary: "Stop hook asked Claude to continue",
detail: "[./check.sh]: Run the formatter before finishing.",
});
}).pipe(
Effect.scoped,
Effect.provide(
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
),
),
);

it.effect("tracks a /goal through unmet checks until Claude stops on its own", () =>
Effect.gen(function* () {
const harness = yield* makeWakeHarness;
Expand All @@ -15114,6 +15169,11 @@ describe("ClaudeAdapterV2 background wake turns", () => {
}
const terminal = yield* Queue.take(harness.terminalReceipts);
assert.equal(terminal.status, "completed");
assert.isFalse(
harness.events.some(
(event) => event.type === "turn_item.updated" && event.turnItem.type === "notification",
),
);
assert.deepEqual(goalStatuses(harness.events), [
{ objective: condition, status: "active", checks: 0 },
{
Expand Down
57 changes: 57 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import {
formatClaudeResumeCompactionQuestion,
} from "@t3tools/shared/claudeCompaction";
import {
CLAUDE_STOP_HOOK_CONTINUATION_SUMMARY,
type ChatAttachment,
PROVIDER_SEND_TURN_MAX_FILE_BYTES,
ClaudeSettings,
Expand Down Expand Up @@ -8220,6 +8221,62 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
yield* liveQuery.query.close.pipe(Effect.ignore);
return;
}
if (
message.type === "user" &&
message.parent_tool_use_id === null &&
message.isSynthetic === true &&
nextClaudeGoal(goalsByNativeThread.get(liveQuery.nativeThreadId) ?? null, message) ===
undefined
) {
const content = message.message.content;
const text =
typeof content === "string"
? content
: content.flatMap((part) => (part.type === "text" ? [part.text] : [])).join("");
const prefix = "Stop hook feedback:\n";
if (text.startsWith(prefix)) {
const now = yield* DateTime.now;
const nativeItemId =
message.uuid ??
(yield* crypto
.randomUUIDv4()
.pipe(
Effect.mapError((cause) => queryRunnerError(cause, "allocateStopHookItemId")),
));
yield* emitProviderEvent({
type: "turn_item.updated",
driver: CLAUDE_PROVIDER,
turnItem: {
id: idAllocator.derive.turnItemFromProviderItem({
driver: CLAUDE_PROVIDER,
nativeItemId,
}),
threadId: context.input.threadId,
runId: context.input.runId,
nodeId: context.input.rootNodeId,
providerThreadId: context.input.providerThread.id,
providerTurnId: context.providerTurnId,
nativeItemRef: {
driver: CLAUDE_PROVIDER,
nativeId: nativeItemId,
strength: message.uuid === undefined ? "weak" : "strong",
},
parentItemId: null,
ordinal: yield* resolveItemOrdinal(context, nativeItemId),
type: "notification",
status: "completed",
title: CLAUDE_STOP_HOOK_CONTINUATION_SUMMARY,
source: { kind: "system" },
outcome: "updated",
summary: CLAUDE_STOP_HOOK_CONTINUATION_SUMMARY,
detail: text.slice(prefix.length).trim(),
startedAt: now,
completedAt: now,
updatedAt: now,
},
});
}
}
yield* trackClaudeGoal({ nativeThreadId: liveQuery.nativeThreadId, context, message });

if (message.type === "stream_event" && !message.parent_tool_use_id) {
Expand Down
68 changes: 68 additions & 0 deletions apps/web/src/components/chat/MessagesTimeline.logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2588,6 +2588,74 @@ describe("v2 run and attempt history", () => {
"assistant-final-entry",
]);

const hook = {
id: "stop-hook",
kind: "work" as const,
createdAt: "2026-01-01T00:00:09.500Z",
entry: {
id: "stop-hook",
createdAt: "2026-01-01T00:00:09.500Z",
runId: "turn-1" as never,
label: "Stop hook asked Claude to continue",
tone: "info" as const,
itemType: "notification" as const,
structuredPayload: {
type: "notification",
source: { kind: "system" },
summary: "Stop hook asked Claude to continue",
detail: "[./check.sh]: Run the formatter before finishing.",
} as never,
},
};
const secondAnswer = {
id: "assistant-second-entry",
kind: "message" as const,
createdAt: "2026-01-01T00:00:12Z",
message: {
id: "assistant-second" as never,
role: "assistant" as const,
text: "The formatter has run.",
runId: "turn-1" as never,
createdAt: "2026-01-01T00:00:12Z",
updatedAt: "2026-01-01T00:00:12Z",
streaming: false,
},
};
const secondHook = {
...hook,
id: "stop-hook-2",
createdAt: "2026-01-01T00:00:15Z",
entry: { ...hook.entry, id: "stop-hook-2", createdAt: "2026-01-01T00:00:15Z" },
};
const hookRows = deriveMessagesTimelineRows({
timelineEntries: [
...timelineEntries.slice(0, 4),
hook,
...timelineEntries.slice(4, -1),
secondAnswer,
secondHook,
timelineEntries.at(-1)!,
],
isWorking: false,
turnDiffSummaries: [],
supportsConversationRollback: false,
});
expect(hookRows.map((row) => row.id)).toEqual([
"user-entry",
"turn-fold:turn-1",
"assistant-thought-entry",
"stop-hook",
"thread-created-entry",
"assistant-second-entry",
"stop-hook-2",
"assistant-final-entry",
]);
expect(
hookRows
.filter((row) => row.kind === "message" && row.message.role === "assistant")
.map((row) => row.kind === "message" && row.showAssistantMeta),
).toEqual([false, false, true]);

const expandedRows = deriveMessagesTimelineRows({
timelineEntries,
expandedRunIds: new Set(["turn-1" as never]),
Expand Down
Loading
Loading