Skip to content

Commit b24881e

Browse files
fix(tlon): chunk outbound text over the declared delivery limit (#117438)
* fix(tlon): chunk outbound text over the declared delivery limit * fix(tlon): guard optional outbound adapter in chunking test * test(tlon): prove chunked outbound over real urbit loopback
1 parent db49a9a commit b24881e

4 files changed

Lines changed: 193 additions & 1 deletion

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { ChannelOutboundAdapter } from "openclaw/plugin-sdk/channel-send-result";
2+
// Tlon outbound chunking tests cover the declared text delivery limit.
3+
import { describe, expect, it } from "vitest";
4+
import { tlonPlugin } from "./channel.js";
5+
6+
function requireTlonOutbound(): ChannelOutboundAdapter {
7+
const outbound = tlonPlugin.outbound;
8+
if (!outbound) {
9+
throw new Error("expected tlon plugin to declare an outbound adapter");
10+
}
11+
return outbound;
12+
}
13+
14+
describe("tlon channel outbound", () => {
15+
it("declares bounded Markdown chunking for text delivery", () => {
16+
const outbound = requireTlonOutbound();
17+
expect(outbound.chunkerMode).toBe("markdown");
18+
expect(outbound.textChunkLimit).toBe(10_000);
19+
expect(outbound.chunker).toBeTypeOf("function");
20+
});
21+
22+
it("splits text above the declared limit into bounded chunks", () => {
23+
const outbound = requireTlonOutbound();
24+
const chunker = outbound.chunker;
25+
if (!chunker) {
26+
throw new Error("expected tlon outbound to declare a chunker");
27+
}
28+
const chunks = chunker("x".repeat(10_001), 10_000);
29+
expect(chunks).toEqual(["x".repeat(10_000), "x"]);
30+
expect(chunks.every((chunk) => chunk.length <= 10_000)).toBe(true);
31+
});
32+
});

‎extensions/tlon/src/channel.runtime.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
66
import type { ChannelPlugin } from "openclaw/plugin-sdk/core";
77
import { expectDefined } from "openclaw/plugin-sdk/expect-runtime";
88
import { readResponseTextLimited } from "openclaw/plugin-sdk/provider-http";
9+
import { chunkTextForOutbound } from "openclaw/plugin-sdk/text-chunking";
910
import { runChannelProbe } from "openclaw/plugin-sdk/text-utility-runtime";
1011
import { monitorTlonProvider } from "./monitor/index.js";
1112
import { tlonSetupWizard } from "./setup-surface.js";
@@ -140,6 +141,8 @@ async function withHttpPokeAccountApi<T>(
140141

141142
export const tlonRuntimeOutbound: ChannelOutboundAdapter = {
142143
deliveryMode: "direct",
144+
chunker: chunkTextForOutbound,
145+
chunkerMode: "markdown",
143146
textChunkLimit: 10000,
144147
resolveTarget: ({ to }) => resolveTlonOutboundTarget(to),
145148
deliveryCapabilities: {

‎extensions/tlon/src/channel.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import {
1111
createComputedAccountStatusAdapter,
1212
createDefaultChannelRuntimeState,
1313
} from "openclaw/plugin-sdk/status-helpers";
14-
import { sanitizeAssistantVisibleText } from "openclaw/plugin-sdk/text-chunking";
14+
import {
15+
chunkTextForOutbound,
16+
sanitizeAssistantVisibleText,
17+
} from "openclaw/plugin-sdk/text-chunking";
1518
import { tlonChannelConfigSchema } from "./config-schema.js";
1619
import { tlonDoctor } from "./doctor.js";
1720
import { resolveTlonOutboundSessionRoute } from "./session-route.js";
@@ -64,6 +67,8 @@ const tlonConfigAdapter = createHybridChannelConfigAdapter({
6467

6568
const tlonChannelOutbound: ChannelOutboundAdapter = {
6669
deliveryMode: "direct",
70+
chunker: chunkTextForOutbound,
71+
chunkerMode: "markdown",
6772
textChunkLimit: 10000,
6873
sanitizeText: ({ text }) => sanitizeAssistantVisibleText(text),
6974
resolveTarget: ({ to }) => resolveTlonOutboundTarget(to),
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// Tlon outbound loopback tests exercise the real HTTP poke path against a mock
2+
// urbit ship: authenticate then one bounded poke per chunked text unit.
3+
import { once } from "node:events";
4+
import * as http from "node:http";
5+
import { afterEach, describe, expect, it } from "vitest";
6+
import { tlonPlugin } from "./channel.js";
7+
8+
const TEXT_LIMIT = 10_000;
9+
10+
type TlonPoke = {
11+
app?: string;
12+
mark?: string;
13+
json?: {
14+
ship?: string;
15+
diff?: {
16+
delta?: {
17+
add?: {
18+
memo?: { content?: unknown; author?: string };
19+
};
20+
};
21+
};
22+
};
23+
};
24+
25+
/** Join the plain-string run of a Tlon story the same way a reader reconstructs text. */
26+
function extractStoryText(content: unknown): string {
27+
const verses = Array.isArray(content) ? (content as unknown[]) : [];
28+
const parts: string[] = [];
29+
for (const verse of verses) {
30+
if (
31+
verse &&
32+
typeof verse === "object" &&
33+
"inline" in verse &&
34+
Array.isArray((verse as { inline?: unknown }).inline)
35+
) {
36+
for (const item of (verse as { inline: unknown[] }).inline) {
37+
if (typeof item === "string") {
38+
parts.push(item);
39+
}
40+
}
41+
}
42+
}
43+
return parts.join("");
44+
}
45+
46+
describe("tlon outbound chunking loopback", () => {
47+
let server: http.Server | undefined;
48+
49+
async function listenLoopback(handler: http.RequestListener): Promise<number> {
50+
server = http.createServer(handler);
51+
server.on("clientError", (_err, socket) => socket.destroy());
52+
server.listen(0, "127.0.0.1");
53+
await once(server, "listening");
54+
const address = server.address();
55+
if (!address || typeof address === "string") {
56+
throw new Error("expected loopback server address");
57+
}
58+
return address.port;
59+
}
60+
61+
afterEach(async () => {
62+
if (server) {
63+
await new Promise<void>((resolve, reject) => {
64+
server?.close((err) => (err ? reject(err) : resolve()));
65+
server?.closeAllConnections?.();
66+
});
67+
server = undefined;
68+
}
69+
});
70+
71+
it("delivers each chunked unit as a bounded independent poke the urbit transport accepts", async () => {
72+
const pokes: TlonPoke[] = [];
73+
const rejectedOversized: Array<{ length: number }> = [];
74+
let loginCount = 0;
75+
const port = await listenLoopback((req, res) => {
76+
const url = new URL(req.url ?? "/", "http://127.0.0.1");
77+
if (req.method === "POST" && url.pathname === "/~/login") {
78+
loginCount += 1;
79+
res.writeHead(200, {
80+
"Content-Type": "text/plain",
81+
"set-cookie": "urbauth-~zod=mock-cookie",
82+
});
83+
res.end("ok");
84+
return;
85+
}
86+
if (req.method === "PUT" && url.pathname.startsWith("/~/channel/")) {
87+
let body = "";
88+
req.setEncoding("utf8");
89+
req.on("data", (chunk: string) => {
90+
body += chunk;
91+
});
92+
req.on("end", () => {
93+
const received = JSON.parse(body) as TlonPoke[];
94+
pokes.push(...received);
95+
const oversized = received.some(
96+
(poke) =>
97+
extractStoryText(poke.json?.diff?.delta?.add?.memo?.content).length > TEXT_LIMIT,
98+
);
99+
if (oversized) {
100+
rejectedOversized.push({ length: received.length });
101+
res.writeHead(413, { "Content-Type": "text/plain" });
102+
res.end("memo too long");
103+
return;
104+
}
105+
res.writeHead(200, { "Content-Type": "text/plain" });
106+
res.end("ok");
107+
});
108+
return;
109+
}
110+
res.writeHead(404, { "Content-Type": "text/plain" });
111+
res.end("not found");
112+
});
113+
114+
const cfg = {
115+
channels: {
116+
tlon: {
117+
enabled: true,
118+
ship: "~zod",
119+
url: `http://127.0.0.1:${port}`,
120+
code: "mock-code",
121+
network: { dangerouslyAllowPrivateNetwork: true },
122+
},
123+
},
124+
};
125+
126+
const outbound = tlonPlugin.outbound;
127+
if (!outbound) {
128+
throw new Error("expected tlon plugin to declare an outbound adapter");
129+
}
130+
const chunker = outbound.chunker;
131+
if (!chunker || outbound.textChunkLimit !== TEXT_LIMIT) {
132+
throw new Error("expected tlon outbound to declare a bounded chunker");
133+
}
134+
135+
const text = "x".repeat(10_001);
136+
const chunks = chunker(text, TEXT_LIMIT);
137+
for (const chunk of chunks) {
138+
await outbound.sendText?.({ cfg, to: "~nec", text: chunk });
139+
}
140+
141+
expect(chunks.length).toBeGreaterThan(1);
142+
expect(pokes).toHaveLength(chunks.length);
143+
expect(loginCount).toBeGreaterThan(0);
144+
// Every unit fits the transport limit, so the urbit transport accepts each poke.
145+
expect(rejectedOversized).toEqual([]);
146+
const delivered = pokes.map((poke) =>
147+
extractStoryText(poke.json?.diff?.delta?.add?.memo?.content),
148+
);
149+
expect(delivered.every((part) => part.length <= TEXT_LIMIT)).toBe(true);
150+
expect(delivered.join("")).toBe(text);
151+
});
152+
});

0 commit comments

Comments
 (0)