Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Allow workspace-owned Slack apps to use smaller history pages (#1264)

Regan Bell1c950bf27717
diff --git a/.env.example b/.env.example
index 1ce66b9cf..98652ba0e 100644
--- a/.env.example
+++ b/.env.example
@@ -46,3 +46,4 @@ BUDGET_WINDOW_MS=86400000
# Defaults resolved once for each new swarm; initial spawn may override these.
# SWARM_DEFAULTS={"agents":32,"turnMs":600000}
SLACK_CONTEXT_SOURCE=live
+SLACK_HISTORY_LIMIT=
diff --git a/src/slack/README.md b/src/slack/README.md
index b348da43f..2f1c63df1 100644
--- a/src/slack/README.md
+++ b/src/slack/README.md
@@ -246,6 +246,15 @@ your per-dev name + tokens live only in your local app and your gitignored `.env
the full member list (with a Slack-Connect / guest external marker, and an actor-only
fallback when membership is unreadable) so the core's audience-floor is fine-grained.
+## History page size
+
+`SLACK_HISTORY_LIMIT` optionally sets the live history/replies page size and default
+`read_thread` result count for workspace-owned apps (integer 1–200). Without it,
+owned apps request 200-message pages and return up to 100 messages by default.
+Shared-app installations use 15 for both defaults regardless of this setting.
+An explicit tool count overrides the result count, but not the live page size.
+Stored mirror reads keep their existing window; shadow comparisons match the live page size.
+
## Mirror qualification
`SLACK_CONTEXT_SOURCE` controls conversation context and default Slack tool reads:
diff --git a/src/slack/config.ts b/src/slack/config.ts
index a32e7b924..35e76f750 100644
--- a/src/slack/config.ts
+++ b/src/slack/config.ts
@@ -15,8 +15,17 @@ export function parseSlackContextSource(value: string | undefined): SlackContext
return source;
}
+function parseSlackHistoryLimit(value: string | undefined): number | undefined {
+ if (!value?.trim()) return undefined;
+ const limit = Number(value);
+ if (!Number.isInteger(limit) || limit < 1 || limit > 200)
+ throw new Error("SLACK_HISTORY_LIMIT must be an integer from 1 to 200");
+ return limit;
+}
+
export interface SlackPluginConfig {
contextSource?: SlackContextSource;
+ historyLimit?: number;
installationId?: string;
sharedServiceUrl?: string;
receiverFactory?: (staging?: EnvelopeStaging) => Receiver;
@@ -78,6 +87,7 @@ export function slackPluginConfigFromEnv(
botToken: env.SLACK_BOT_TOKEN,
...(env.SLACK_CONTEXT_SOURCE ? { contextSource: parseSlackContextSource(env.SLACK_CONTEXT_SOURCE) } : {}),
...(receiverFactory ? { receiverFactory } : {}),
+ ...opt("historyLimit", parseSlackHistoryLimit(env.SLACK_HISTORY_LIMIT)),
...opt("appToken", env.SLACK_APP_TOKEN),
...opt("apiUrl", env.SLACK_API_URL),
...(eventsMode === "http" ? { eventsMode } : {}),
@@ -146,6 +156,7 @@ export function slackAccountConfigsFromEnv(env: Record<string, string | undefine
SLACK_IDENTITY_EMAIL: str(a.identityEmail) ?? "1",
SLACK_LOG_LEVEL: env.SLACK_LOG_LEVEL,
SLACK_CONTEXT_SOURCE: env.SLACK_CONTEXT_SOURCE,
+ SLACK_HISTORY_LIMIT: env.SLACK_HISTORY_LIMIT,
WEB_UI_PUBLIC_URL: env.WEB_UI_PUBLIC_URL,
QM_SLACK_SERVICE_URL: env.QM_SLACK_SERVICE_URL,
};
diff --git a/src/slack/history.ts b/src/slack/history.ts
index 6de83aceb..fc9d2f76d 100644
--- a/src/slack/history.ts
+++ b/src/slack/history.ts
@@ -34,10 +34,11 @@ export function createSlackHistoryReader(deps: {
source?: SlackContextSource;
ids: BotIdentity;
managed?: boolean;
+ historyLimit?: number;
setupUrl?: string;
historyClient?: { conversations: { history(args: any): Promise<unknown>; replies(args: any): Promise<unknown> } };
}): SlackHistoryReader {
- const limit = deps.managed ? SHARED_SLACK_HISTORY_LIMIT : 200;
+ const limit = deps.managed ? SHARED_SLACK_HISTORY_LIMIT : (deps.historyLimit ?? 200);
async function liveHistory(
client: any,
channel: string,
diff --git a/src/slack/index.ts b/src/slack/index.ts
index 16f9d82ca..712b45b2b 100644
--- a/src/slack/index.ts
+++ b/src/slack/index.ts
@@ -235,6 +235,7 @@ export async function startSlackPlugin(
) as Pick<typeof historyApi.conversations, "history" | "replies">,
};
const readHistory = createSlackHistoryReader({
+ historyLimit: cfg.historyLimit,
core,
ids,
historyClient,
@@ -350,6 +351,7 @@ export async function startSlackPlugin(
ensureHeader,
});
const surfaceContext = createSurfaceContextFulfiller({
+ historyLimit: cfg.historyLimit,
rateLimitNotice,
historyClient,
readHistory,
diff --git a/src/slack/surface-context.ts b/src/slack/surface-context.ts
index 11b70652b..47b1478c7 100644
--- a/src/slack/surface-context.ts
+++ b/src/slack/surface-context.ts
@@ -35,6 +35,7 @@ export function createSurfaceContextFulfiller(deps: {
clientOptions: Record<string, unknown>;
readHistory?: SlackHistoryReader;
historyClient?: any;
+ historyLimit?: number;
historyRateLimitOptions?: { managed?: boolean; setupUrl?: string };
}): { fulfillSurfaceContext(client: any, r: SurfaceContextRequest): Promise<void> } {
const { core, directory, serializer, botToken, trustedFileHost, userToken, clientOptions } = deps;
@@ -88,7 +89,7 @@ export function createSurfaceContextFulfiller(deps: {
await client.conversations.replies({
channel,
ts: threadTs,
- limit: managed ? SHARED_SLACK_HISTORY_LIMIT : RECENT_THREAD_LIMIT,
+ limit: managed ? SHARED_SLACK_HISTORY_LIMIT : (deps.historyLimit ?? RECENT_THREAD_LIMIT),
...page,
}),
);
@@ -97,7 +98,7 @@ export function createSurfaceContextFulfiller(deps: {
const { messages, hasMore } = parseMessageList(
await client.conversations.history({
channel,
- limit: managed ? SHARED_SLACK_HISTORY_LIMIT : RECENT_HISTORY_LIMIT,
+ limit: managed ? SHARED_SLACK_HISTORY_LIMIT : (deps.historyLimit ?? RECENT_HISTORY_LIMIT),
...page,
}),
);
@@ -244,7 +245,10 @@ export function createSurfaceContextFulfiller(deps: {
}
const count = Math.max(
1,
- Math.min(RECENT_THREAD_LIMIT, Number(q.count) || (managed ? SHARED_SLACK_HISTORY_LIMIT : 100)),
+ Math.min(
+ RECENT_THREAD_LIMIT,
+ Number(q.count) || (managed ? SHARED_SLACK_HISTORY_LIMIT : (deps.historyLimit ?? 100)),
+ ),
);
const before = typeof q.before === "string" && q.before ? q.before : undefined;
const outcomes = await Promise.allSettled([
diff --git a/test/slack-history-rate-limit.test.ts b/test/slack-history-rate-limit.test.ts
index e2bdb953b..cc6b3d787 100644
--- a/test/slack-history-rate-limit.test.ts
+++ b/test/slack-history-rate-limit.test.ts
@@ -3,7 +3,7 @@ import { createServer } from "node:http";
import { once } from "node:events";
import { test } from "node:test";
import { WebClient, LogLevel } from "@slack/web-api";
-import { HISTORY_NO_RETRY } from "../src/slack/config.ts";
+import { HISTORY_NO_RETRY, slackPluginConfigFromEnv, slackAccountConfigsFromEnv } from "../src/slack/config.ts";
import { slackHistoryRateLimitMessage } from "../src/slack/history-rate-limit.ts";
test("managed history throttling gives retry timing and workspace app setup without leaking the error", () => {
@@ -71,8 +71,13 @@ test("Slack history 429 returns immediately instead of sleeping inside the SDK",
});
for (const source of ["live", "shadow", "mirror"] as const) {
- for (const managed of [false, true]) {
- test(`${source} history uses the ${managed ? "shared" : "workspace-owned"} app page size`, async () => {
+ for (const { managed, historyLimit } of [
+ { managed: false },
+ { managed: true },
+ { managed: false, historyLimit: 15 },
+ { managed: true, historyLimit: 200 },
+ ]) {
+ test(`${source} history uses the ${managed ? "shared" : "workspace-owned"} app page size (override ${historyLimit ?? "unset"})`, async () => {
const { createSlackHistoryReader } = await import("../src/slack/history.ts");
const calls: Array<{ method: string; args: any }> = [];
const client = {
@@ -91,13 +96,14 @@ for (const source of ["live", "shadow", "mirror"] as const) {
ids: { botUserId: "UBOT", ownBotId: "BBOT" } as import("../src/slack/directory.ts").BotIdentity,
source,
managed,
+ historyLimit,
});
const channel = await reader(client, "C1", undefined, "10.0", true);
const thread = await reader(client, "C1", "1.0");
assert.equal(calls.length, source === "mirror" ? 2 : 3);
assert.ok(calls.some((call) => call.method === "history"));
assert.ok(calls.some((call) => call.method === "replies"));
- assert.ok(calls.every((call) => call.args.limit === (managed ? 15 : 200)));
+ assert.ok(calls.every((call) => call.args.limit === (managed ? 15 : (historyLimit ?? 200))));
assert.equal(calls[0]!.args.latest, "10.0");
assert.equal(calls[0]!.args.inclusive, false);
for (const page of [channel, thread]) {
@@ -111,8 +117,13 @@ for (const source of ["live", "shadow", "mirror"] as const) {
}
}
-for (const managed of [false, true]) {
- test(`surface history defaults for ${managed ? "shared" : "workspace-owned"} apps`, async () => {
+for (const { managed, historyLimit } of [
+ { managed: false },
+ { managed: true },
+ { managed: false, historyLimit: 15 },
+ { managed: true, historyLimit: 200 },
+]) {
+ test(`surface history defaults for ${managed ? "shared" : "workspace-owned"} apps (override ${historyLimit ?? "unset"})`, async () => {
const { createSurfaceContextFulfiller } = await import("../src/slack/surface-context.ts");
let outcome: any;
const calls: any[] = [];
@@ -147,16 +158,17 @@ for (const managed of [false, true]) {
botToken: "test",
clientOptions: {},
historyRateLimitOptions: { managed },
+ historyLimit,
});
const query = { conversationTarget: "C1:1.0" };
await fulfiller.fulfillSurfaceContext(client, {
id: "test",
query,
} as import("../src/api/slack-core-client.ts").SurfaceContextRequest);
- assert.equal(outcome.result.messages.length, managed ? 15 : 30);
+ assert.equal(outcome.result.messages.length, managed ? 15 : (historyLimit ?? 30));
assert.equal(outcome.result.hasMore, true);
assert.ok(outcome.result.nextBefore);
- assert.ok(calls.every((args) => args.limit === (managed ? 15 : 200)));
+ assert.ok(calls.every((args) => args.limit === (managed ? 15 : (historyLimit ?? 200))));
assert.equal(outcome.result.note, undefined);
await fulfiller.fulfillSurfaceContext(client, {
id: "test",
@@ -165,3 +177,19 @@ for (const managed of [false, true]) {
assert.equal(outcome.result.messages.length, 5);
});
}
+
+test("history limit configuration is optional, validated, and inherited by additional accounts", () => {
+ const env = { SLACK_BOT_TOKEN: "test", SLACK_APP_TOKEN: "test" };
+ assert.equal(slackPluginConfigFromEnv(env)?.historyLimit, undefined);
+ assert.equal(slackPluginConfigFromEnv({ ...env, SLACK_HISTORY_LIMIT: "15" })?.historyLimit, 15);
+ for (const value of ["0", "201", "1.5", "nope", "Infinity"]) {
+ assert.throws(() => slackPluginConfigFromEnv({ ...env, SLACK_HISTORY_LIMIT: value }), /SLACK_HISTORY_LIMIT/);
+ }
+ const accounts = slackAccountConfigsFromEnv({
+ ...env,
+ SLACK_HISTORY_LIMIT: "15",
+ SLACK_ACCOUNTS: JSON.stringify([{ id: "extra", botToken: "other", appToken: "other" }]),
+ });
+ assert.equal(accounts.length, 1);
+ assert.ok(accounts.every((account) => account.historyLimit === 15));
+});