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
12 changes: 10 additions & 2 deletions apps/server/src/scheduledTasks/ScheduledTaskService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import * as Mac from "effect/crypto/Mac";
import * as SecureRandom from "effect/crypto/SecureRandom";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
import { splitsSurrogatePair } from "@t3tools/shared/assistantCitations";
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
import * as Exit from "effect/Exit";
import * as Duration from "effect/Duration";
Expand Down Expand Up @@ -90,6 +91,13 @@ const WEBHOOK_DEDUPE_LOG_PER_MINUTE = 10;
const WEBHOOK_FILTERED_LOG_PER_MINUTE = 10;
const WEBHOOK_DELIVERY_LOG_BODY_LIMIT = 64 * 1024;
const WEBHOOK_DELIVERY_LOG_PROMPT_LIMIT = 64 * 1024;
const loggedWebhookPrompt = (prompt: string) =>
prompt.slice(
0,
splitsSurrogatePair(prompt, WEBHOOK_DELIVERY_LOG_PROMPT_LIMIT)
? WEBHOOK_DELIVERY_LOG_PROMPT_LIMIT - 1
: WEBHOOK_DELIVERY_LOG_PROMPT_LIMIT,
);
const WEBHOOK_MAX_QUEUED_PER_TASK = 20;
// Enforce the relay's per-task/minute limit here too; tunnel hostnames are public.
const WEBHOOK_RATE_LIMIT_PER_MINUTE = 60;
Expand Down Expand Up @@ -1947,7 +1955,7 @@ export const layer = Layer.effect(
${loggedBody},
${input.request.body.byteLength}, ${truncated ? 1 : 0}, ${input.outcome},
${input.signatureVerified ? 1 : 0}, ${encodeMissingFieldsJson(input.missing)},
${input.renderedPrompt?.slice(0, WEBHOOK_DELIVERY_LOG_PROMPT_LIMIT) ?? null},
${input.renderedPrompt === null ? null : loggedWebhookPrompt(input.renderedPrompt)},
${input.error ?? null}
WHERE EXISTS (SELECT 1 FROM scheduled_tasks WHERE task_id = ${input.taskId})
-- A held request retried after a rate limit reuses its relay
Expand Down Expand Up @@ -2636,7 +2644,7 @@ export const layer = Layer.effect(
} else {
if (taskJson !== null)
yield* sql`UPDATE scheduled_task_webhook_pending SET task_json = ${taskJson} WHERE delivery_id = ${entry.delivery_id}`;
yield* sql`UPDATE scheduled_task_webhook_deliveries SET outcome = 'accepted', error = NULL, rendered_prompt = ${rendered.prompt.slice(0, WEBHOOK_DELIVERY_LOG_PROMPT_LIMIT)}, missing_fields_json = ${encodeMissingFieldsJson(rendered.missing)} WHERE delivery_id = ${entry.delivery_id}`;
yield* sql`UPDATE scheduled_task_webhook_deliveries SET outcome = 'accepted', error = NULL, rendered_prompt = ${loggedWebhookPrompt(rendered.prompt)}, missing_fields_json = ${encodeMissingFieldsJson(rendered.missing)} WHERE delivery_id = ${entry.delivery_id}`;
if (!(yield* runWebhookDelivery(task, entry.delivery_id, rendered.prompt))) return;
}
yield* remove;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,28 @@ it.effect("drops a character split by the delivery log's body limit", () =>
),
);

it.effect("drops an emoji split by the delivery log's prompt limit", () =>
withService(({ service }) =>
Effect.gen(function* () {
const { task } = yield* service.upsert(yield* webhookTaskInput({ prompt: "{{body.text}}" }));
const limit = 64 * 1024;
const frameOpen = "<webhook-data-000000000000>".length;
// The emoji's high surrogate is the last code unit the log keeps.
const bodyText = JSON.stringify({ text: `${"a".repeat(limit - 1 - frameOpen)}😀 tail` });
yield* service.triggerWebhook(
requestFor(task, { bodyText, body: new TextEncoder().encode(bodyText) }),
);
const [summary] = (yield* service.listWebhookDeliveries({ id: task.id })).deliveries;
const { delivery } = yield* service.getWebhookDelivery({
id: task.id,
deliveryId: summary!.id,
});
assert.equal(delivery.renderedPrompt!.slice(-3), "aaa");
assert.equal(delivery.renderedPrompt!.length, limit - 1);
}),
),
);

const queuedDeliveryCases = [
{
change: "paused",
Expand Down
75 changes: 75 additions & 0 deletions scripts/scenarios/webhook-log-keeps-whole-emoji.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { ProjectSnapshot, ProviderInstanceId } from "@t3tools/contracts";
import { expect } from "e2e";
import * as Effect from "effect/Effect";
import { FetchHttpClient, HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";

import { T3_HOME } from "../e2e.config.ts";
import { makeState, rpcClient, runPromise, test, WEB } from "./t3.ts";

const PROMPT_LOG_LIMIT = 64 * 1024;
const FRAME_OPEN = "<webhook-data-000000000000>".length;

const deliverStraddlingEmoji = Effect.fn("scenario.deliverStraddlingEmoji")(
function* (origin: string) {
const issued = yield* makeState(T3_HOME).cli(["auth", "session", "issue", "--token-only"]);
const { projects } = yield* HttpClient.execute(
HttpClientRequest.get(`${origin}/api/projects`).pipe(
HttpClientRequest.bearerToken(issued.stdout.trim()),
),
).pipe(
Effect.flatMap(HttpClientResponse.filterStatusOk),
Effect.flatMap(HttpClientResponse.schemaBodyJson(ProjectSnapshot)),
);
const client = yield* rpcClient(T3_HOME, origin);
const { task } = yield* client["scheduledTasks.upsert"]({
title: "Webhook log",
prompt: "{{body.text}}",
enabled: true,
schedule: { type: "webhook" },
projectId: projects.find((project) => project.title === "webhook-log")!.id,
workspaceStrategy: { type: "root" },
modelSelection: { instanceId: ProviderInstanceId.make("devReplay"), model: "showcase" },
runtimeMode: "full-access",
interactionMode: "default",
});
// The emoji's high surrogate is the last code unit the delivery log keeps.
const text = `${"a".repeat(PROMPT_LOG_LIMIT - 1 - FRAME_OPEN)}😀 tail`;
yield* HttpClient.execute(
HttpClientRequest.post(`${origin}${task.webhook!.path}`).pipe(
HttpClientRequest.bodyJsonUnsafe({ text }),
),
).pipe(Effect.flatMap(HttpClientResponse.filterStatusOk));
},
Effect.scoped,
Effect.provide(FetchHttpClient.layer),
);

test(
"A long webhook prompt in the delivery log does not end in a broken character",
WEB,
async ({ t3, app, browser, screen }) => {
await t3.project("webhook-log");
await runPromise(deliverStraddlingEmoji(app.baseUrl ?? ""));

await t3.open("/settings/scheduled-tasks");
await screen.getByRole("button", "Actions for Webhook log").tap();
await screen.getByRole("menuitem", "Deliveries").tap();
const dialog = screen.getByRole("dialog");
await dialog.getByRole("button", /POST/).tap();
const prompt = dialog.getByText(/^<webhook-data-/);
await prompt.waitFor();
await browser.evaluate(() => {
const block = [...document.querySelectorAll("pre")].find((pre) =>
pre.textContent?.startsWith("<webhook-data-"),
);
block?.scrollIntoView({ block: "center" });
if (block) block.scrollTop = block.scrollHeight;
return null;
});
try {
await expect(prompt).not.toContainText("�");
} finally {
await app.screenshot("logged-prompt-end");
}
},
);
Loading