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
1 change: 1 addition & 0 deletions apps/server/src/mcp/McpHttpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,7 @@ export const T4_ALWAYS_LOAD = new Set([
"t3_thread_update",
"schedule_task",
"watch_workflow_run",
"wait_for_reply",
"release_queue",
"request_secret",
"link_pull_request",
Expand Down
91 changes: 91 additions & 0 deletions apps/server/src/mcp/toolkits/t4/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import * as T4Delivery from "../../../t4/T4Delivery.ts";
import * as FyiDigest from "../../../t4/fyiDigest.ts";
import * as T4Ledger from "../../../t4/T4Ledger.ts";
import * as T4Releases from "../../../t4/T4Releases.ts";
import * as T4ReplyWatch from "../../../t4/T4ReplyWatch.ts";
import * as T4RunWatch from "../../../t4/T4RunWatch.ts";
import * as T4Store from "../../../t4/T4Store.ts";
import * as ToolCallLog from "../../../t4/toolCallLog.ts";
Expand Down Expand Up @@ -985,6 +986,96 @@ export const layer = McpToolAccess.toLayer(T4Toolkit, {
}),
),

wait_for_reply: McpToolAccess.actsAsCaller((input) =>
Effect.gen(function* () {
const me = yield* callerThread;
const replyWatch = yield* T4ReplyWatch.T4ReplyWatch;
const who = input.who.trim();
if (who.length === 0) return yield* invalid("`who` is required: who you wait on");
if (input.cancel === true) {
const gone = yield* replyWatch.cancel(me.id, who);
return {
waiting: false,
who,
check: gone[0]?.check ?? "",
deadline: "",
next:
gone.length > 0
? `Stopped waiting on ${who}.`
: `You weren't waiting on ${who}; nothing to cancel.`,
};
}
const now = yield* Clock.currentTimeMillis;
let check = input.check?.trim() ?? "";
let via: string | null = null;
if (input.email !== undefined) {
if (check.length > 0) return yield* invalid("give `check` or `email`, not both");
const from = input.email.from?.trim() ?? "";
const thread = input.email.thread?.trim() ?? "";
if (from.length === 0 && thread.length === 0)
return yield* invalid(
"`email` needs `from` (their address) and/or `thread` (a message id)",
);
const quote = (text: string) => `'${text.replaceAll("'", "'\\''")}'`;
check = [
"email replies",
`--since ${DateTime.formatIso(DateTime.makeUnsafe(now))}`,
from.length > 0 ? `--from ${quote(from)}` : null,
thread.length > 0 ? `--thread ${quote(thread)}` : null,
]
.filter((part) => part !== null)
.join(" ");
via = "email";
}
if (check.length === 0)
return yield* invalid(
"give `check` (a command that prints their reply once it exists) or `email` ({from} / {thread})",
);
const deadlineMs = T4ReplyWatch.parseDuration(
input.deadline ?? (via === "email" ? "2d" : "1h"),
);
if (deadlineMs === null || deadlineMs < 60_000 || deadlineMs > T4ReplyWatch.MAX_DEADLINE_MS)
return yield* invalid("`deadline` is a time from 1m to 14d, like 30m, 2h or 3d");
const everyMs =
input.every === undefined
? T4ReplyWatch.defaultEvery(deadlineMs)
: T4ReplyWatch.parseDuration(input.every);
if (
everyMs === null ||
everyMs < T4ReplyWatch.MIN_EVERY_MS ||
everyMs > T4ReplyWatch.MAX_EVERY_MS
)
return yield* invalid("`every` is 1m to 60m");
const project =
me.worktreePath === null
? yield* Project.ProjectService.pipe(
Effect.flatMap((projects) => projects.getById(me.projectId)),
Effect.map(Option.getOrUndefined),
Effect.orElseSucceed(() => undefined),
)
: undefined;
const { watch, replaced } = yield* replyWatch
.watch({
threadId: me.id,
who,
about: input.about?.trim() || null,
check,
via: via ?? (/^\s*usars\s+chat\b/.test(check) ? "chat" : null),
cwd: me.worktreePath ?? project?.workspaceRoot ?? null,
deadlineMs,
everyMs,
})
.pipe(Effect.mapError((error) => invalid(error.message)));
return {
waiting: true,
who,
check,
deadline: T4ReplyWatch.pacific(watch.deadline),
next: `${replaced ? "Replaced your earlier watch on " + who + ". " : ""}End your turn now. You get one message when ${who} replies (checked every ${Math.round(everyMs / 60_000)} min) or at ${T4ReplyWatch.pacific(watch.deadline)} if nobody has; the dashboard shows you as waiting on ${who}. Don't post the user a card while you wait.`,
};
}),
),

release_queue: McpToolAccess.actsAsCaller((input) =>
Effect.gen(function* () {
const ledger = yield* T4Ledger.T4Ledger;
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/mcp/toolkits/t4/release.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as Project from "../../../project/ProjectService.ts";
import * as T4Config from "../../../t4/T4Config.ts";
import * as T4Delivery from "../../../t4/T4Delivery.ts";
import * as T4Ledger from "../../../t4/T4Ledger.ts";
import * as T4ReplyWatch from "../../../t4/T4ReplyWatch.ts";
import * as T4RunWatch from "../../../t4/T4RunWatch.ts";
import * as T4Releases from "../../../t4/T4Releases.ts";
import * as T4Sql from "../../../t4/T4Sql.ts";
Expand Down Expand Up @@ -201,6 +202,7 @@ const layer = McpHttpServer.layerT4Toolkit.pipe(
}),
Layer.mock(ThreadLaunch.ThreadLaunchService)({}),
Layer.mock(T4RunWatch.T4RunWatch)({}),
Layer.mock(T4ReplyWatch.T4ReplyWatch)({}),
Layer.succeed(T4Config.T4ServerConfig, T4Config.defaultT4ServerConfig),
),
),
Expand Down
61 changes: 61 additions & 0 deletions apps/server/src/mcp/toolkits/t4/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as Project from "../../../project/ProjectService.ts";
import * as T4Config from "../../../t4/T4Config.ts";
import * as T4Delivery from "../../../t4/T4Delivery.ts";
import * as T4Ledger from "../../../t4/T4Ledger.ts";
import * as T4ReplyWatch from "../../../t4/T4ReplyWatch.ts";
import * as T4RunWatch from "../../../t4/T4RunWatch.ts";
import * as T4Store from "../../../t4/T4Store.ts";

Expand Down Expand Up @@ -269,6 +270,65 @@ const WatchWorkflowRunTool = Tool.make("watch_workflow_run", {
// t4 fork: acts as the calling thread, so outside clients never see it.
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);

// t4 fork (AI-143): a thread waiting on someone outside t4 - a customer, supplier or staff member.
const WaitForReplyTool = Tool.make("wait_for_reply", {
...common,
dependencies: [...common.dependencies, T4ReplyWatch.T4ReplyWatch],
description:
"After you ask a customer, supplier or staff member something (an email, the client's own chat - the client's AGENTS.md says where each answers), be woken when they reply instead of scheduling wakes to look, then END YOUR TURN. t4 runs `check` in your checkout every minute or so at no AI cost; the first time it prints anything you get one message with that output, or one saying nobody replied by `deadline`. `email` builds the check from the user's mailbox (`email replies`). The dashboard shows you as waiting on `who`. Same `who` again replaces that watch; `cancel: true` ends it (for instance when the answer reached you another way).",
parameters: Schema.Struct({
who: Schema.String.annotate({
description: 'Who you wait on, as the user knows them: "Dolma", "Korin at Vollrath".',
}),
about: Schema.optional(
Schema.String.annotate({
description: 'A few words: what you asked ("USA-3097 early-pay terms").',
}),
),
check: Schema.optional(
Schema.String.annotate({
description:
"A shell command that prints the reply once it exists and nothing before (exit 0 either way), e.g. `usars chat replies USA-3097 --since 2026-11-23T20:09:43Z --from Dolma`. It must return at once, never wait.",
}),
),
email: Schema.optional(
Schema.Struct({
from: Schema.optional(
Schema.String.annotate({ description: "Their address(es), comma-separated." }),
),
thread: Schema.optional(
Schema.String.annotate({
description: "A message id in the conversation (from `email`).",
}),
),
}).annotate({
description: "Wait for mail from them and/or in that conversation, instead of `check`.",
}),
),
deadline: Schema.optional(
Schema.String.annotate({
description:
"How long to wait: 5m, 1h, 2d (up to 14d). Default 1h for `check`, 2d for `email`; the client's AGENTS.md may set it by urgency.",
}),
),
every: Schema.optional(
Schema.String.annotate({
description: "How often to check (1m-60m). Default 1m for waits of 3h or less, else 5m.",
}),
),
cancel: Schema.optional(Schema.Boolean),
}),
success: Schema.Struct({
waiting: Schema.Boolean,
who: Schema.String,
check: Schema.String,
deadline: Schema.String,
next: Schema.String,
}),
})
.annotate(Tool.Destructive, false)
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);

const ReleaseState = Schema.Literals(T4Store.RELEASE_STATES);

const ReleaseEntry = Schema.Struct({
Expand Down Expand Up @@ -365,5 +425,6 @@ export const T4Toolkit = Toolkit.make(
ListThreadIssuesTool,
SetThreadRoleTool,
WatchWorkflowRunTool,
WaitForReplyTool,
ReleaseQueueTool,
);
2 changes: 2 additions & 0 deletions apps/server/src/provider/AgentContextBudget.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import { VcsStatusBroadcaster } from "../vcs/VcsStatusBroadcaster.ts";
import { T4ServerConfig, defaultT4ServerConfig } from "../t4/T4Config.ts";
import { T4Delivery } from "../t4/T4Delivery.ts";
import { T4Ledger } from "../t4/T4Ledger.ts";
import { T4ReplyWatch } from "../t4/T4ReplyWatch.ts";
import { T4RunWatch } from "../t4/T4RunWatch.ts";
import { T4Store } from "../t4/T4Store.ts";
import { buildCodexAdditionalContext } from "./CodexDeveloperInstructions.ts";
Expand Down Expand Up @@ -80,6 +81,7 @@ const toolHandlerServices = Layer.mergeAll(
// t4 fork: t3_decisions reads T3's database (t4/T4Decisions.ts).
NodeSqliteClient.layer({ filename: ":memory:" }),
Layer.mock(T4RunWatch)({}),
Layer.mock(T4ReplyWatch)({}),
Layer.succeed(T4ServerConfig, defaultT4ServerConfig),
Layer.mock(PeerLinkRequests)({}),
Layer.mock(PeerLinks)({}),
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/provider/agentContextBudget.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"claudeSystemAppendBytes": 9583,
"codexAdditionalContextBytes": 10987,
"mcpToolListJsonBytes": 67186
"mcpToolListJsonBytes": 69016
}
20 changes: 20 additions & 0 deletions apps/server/src/t4/T4Http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import * as T4Config from "./T4Config.ts";
import * as T4Delivery from "./T4Delivery.ts";
import { factsFor } from "./T4Facts.ts";
import * as T4Ledger from "./T4Ledger.ts";
import * as T4ReplyWatch from "./T4ReplyWatch.ts";
import * as T4RunWatch from "./T4RunWatch.ts";
import * as T4Schedules from "./T4Schedules.ts";
import * as T4Spawn from "./T4Spawn.ts";
Expand Down Expand Up @@ -242,6 +243,25 @@ export const layer = Layer.mergeAll(
service._tag === "Some" ? service.value.watches : Effect.succeed([]),
),
),
// Who a thread waits on outside t4 (wait_for_reply, AI-143): "Waiting on Dolma".
replyWatches: yield* Effect.serviceOption(T4ReplyWatch.T4ReplyWatch).pipe(
Effect.flatMap((service) =>
service._tag === "Some" ? service.value.watches : Effect.succeed([]),
),
Effect.map((list) =>
list.map((w) => ({
threadId: w.threadId,
who: w.who,
about: w.about,
via: w.via,
since: DateTime.formatIso(DateTime.makeUnsafe(w.since)),
deadline: DateTime.formatIso(DateTime.makeUnsafe(w.deadline)),
lastCheck:
w.lastCheck > 0 ? DateTime.formatIso(DateTime.makeUnsafe(w.lastCheck)) : null,
failing: w.failures > 0 ? w.lastError : null,
})),
),
),
});
}),
),
Expand Down
4 changes: 4 additions & 0 deletions apps/server/src/t4/T4Jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import * as T4AutoArchive from "./T4AutoArchive.ts";
import * as T4ReleaseGuard from "./T4ReleaseGuard.ts";
import * as T4Releases from "./T4Releases.ts";
import * as T4Schedules from "./T4Schedules.ts";
import * as T4ReplyWatch from "./T4ReplyWatch.ts";
import * as T4RunWatch from "./T4RunWatch.ts";
import * as T4Settle from "./T4Settle.ts";
import * as T4Stalls from "./T4Stalls.ts";
Expand Down Expand Up @@ -43,6 +44,9 @@ export const layer = Layer.effectDiscard(
yield* scheduler.register("t4-stall-recovery", yield* throttled(60_000, T4Stalls.sweep));
const runWatch = yield* T4RunWatch.T4RunWatch;
yield* scheduler.register("t4-workflow-runs", yield* throttled(30_000, runWatch.sweep));
// Each watch keeps its own interval (a minute at the least); the job only looks.
const replyWatch = yield* T4ReplyWatch.T4ReplyWatch;
yield* scheduler.register("t4-reply-watches", yield* throttled(30_000, replyWatch.sweep));
yield* scheduler.register("t4-fyi-digests", yield* throttled(60_000, FyiDigest.sweep));
yield* scheduler.register("t4-release-guard", yield* throttled(120_000, T4ReleaseGuard.sweep));
yield* scheduler.register("t4-release-stale", yield* throttled(600_000, T4Releases.sweepStale));
Expand Down
Loading
Loading