@cursor/july 0.1.20 → 0.1.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin/agent-serve.js +0 -0
- package/dist/channels/github/instrument.d.ts +20 -0
- package/dist/channels/github/instrument.d.ts.map +1 -0
- package/dist/channels/slack/api.d.ts +2 -0
- package/dist/channels/slack/api.d.ts.map +1 -1
- package/dist/channels/slack/api.js +3 -0
- package/dist/channels/slack/bot-mentions.d.ts +108 -0
- package/dist/channels/slack/bot-mentions.d.ts.map +1 -0
- package/dist/channels/slack/bot-mentions.js +264 -0
- package/dist/channels/slack/dispatch.d.ts +6 -0
- package/dist/channels/slack/dispatch.d.ts.map +1 -1
- package/dist/channels/slack/dispatch.js +24 -3
- package/dist/channels/slack/inbound.d.ts +10 -1
- package/dist/channels/slack/inbound.d.ts.map +1 -1
- package/dist/channels/slack/inbound.js +12 -4
- package/dist/channels/slack/index.d.ts +1 -0
- package/dist/channels/slack/index.d.ts.map +1 -1
- package/dist/channels/slack/index.js +1 -0
- package/dist/channels/slack/slack-channel.d.ts.map +1 -1
- package/dist/channels/slack/slack-channel.js +61 -22
- package/dist/channels/slack/types.d.ts +69 -0
- package/dist/channels/slack/types.d.ts.map +1 -1
- package/dist/docs/404.html +1 -1
- package/dist/docs/ab.html +2 -2
- package/dist/docs/assets/{app.T-5ImnM3.js → app.BKNKMLp4.js} +1 -1
- package/dist/docs/assets/chunks/@localSearchIndexroot.YFE6WoMB.js +1 -0
- package/dist/docs/assets/chunks/{VPLocalSearchBox.OMGMWH6H.js → VPLocalSearchBox.sOSfEZR6.js} +1 -1
- package/dist/docs/assets/chunks/{theme.DLg4yB4D.js → theme.BvCHT5lj.js} +2 -2
- package/dist/docs/building-with-agents.html +2 -2
- package/dist/docs/concepts.html +2 -2
- package/dist/docs/deployment.html +2 -2
- package/dist/docs/evals.html +2 -2
- package/dist/docs/example-agents/approval-buddy.html +2 -2
- package/dist/docs/example-agents/benny.html +2 -2
- package/dist/docs/example-agents/bugbot.html +2 -2
- package/dist/docs/example-agents/codebase-wiki.html +2 -2
- package/dist/docs/example-agents/codeowners-review.html +2 -2
- package/dist/docs/example-agents/concierge.html +2 -2
- package/dist/docs/example-agents/fsd.html +2 -2
- package/dist/docs/example-agents/index.html +2 -2
- package/dist/docs/example-agents/knowledge-base.html +2 -2
- package/dist/docs/example-agents/oncall.html +2 -2
- package/dist/docs/example-agents/security-reviewer.html +2 -2
- package/dist/docs/example-agents/slack-agent.html +2 -2
- package/dist/docs/example-agents/weather-agent.html +2 -2
- package/dist/docs/guides/agent-to-agent.html +2 -2
- package/dist/docs/guides/cloud-runtime.html +2 -2
- package/dist/docs/guides/github.html +2 -2
- package/dist/docs/guides/human-in-the-loop.html +2 -2
- package/dist/docs/guides/mcp-oauth.html +2 -2
- package/dist/docs/guides/slack.html +2 -2
- package/dist/docs/guides/webhooks.html +2 -2
- package/dist/docs/hillclimbing.html +2 -2
- package/dist/docs/index.html +2 -2
- package/dist/docs/quickstart.html +2 -2
- package/dist/docs/reference/agent-config.html +2 -2
- package/dist/docs/reference/channels.html +2 -2
- package/dist/docs/reference/cli.html +2 -2
- package/dist/docs/reference/connections.html +2 -2
- package/dist/docs/reference/hooks.html +2 -2
- package/dist/docs/reference/http-api.html +2 -2
- package/dist/docs/reference/instructions.html +2 -2
- package/dist/docs/reference/playground.html +2 -2
- package/dist/docs/reference/project-layout.html +2 -2
- package/dist/docs/reference/prompt.html +2 -2
- package/dist/docs/reference/schedules.html +2 -2
- package/dist/docs/reference/sessions.html +2 -2
- package/dist/docs/reference/skills.html +2 -2
- package/dist/docs/reference/subagents.html +2 -2
- package/dist/docs/reference/tools.html +2 -2
- package/dist/docs/scaffolding-agents.html +2 -2
- package/dist/docs/storage.html +2 -2
- package/dist/docs/troubleshooting.html +2 -2
- package/dist/internal/json-dir-store.d.ts +32 -0
- package/dist/internal/json-dir-store.d.ts.map +1 -0
- package/dist/internal/json-dir-store.js +100 -0
- package/dist/internal/local-env.d.ts +6 -4
- package/dist/internal/local-env.d.ts.map +1 -1
- package/dist/internal/local-env.js +14 -6
- package/dist/internal/resolved-connections.d.ts +1 -1
- package/dist/internal/resolved-connections.d.ts.map +1 -1
- package/dist/internal/resolved-connections.js +10 -0
- package/dist/internal/session-engine.d.ts +4 -1
- package/dist/internal/session-engine.d.ts.map +1 -1
- package/dist/internal/session-engine.js +13 -2
- package/dist/playground/assets/index-CjOQ4hN9.css +1 -0
- package/dist/playground/assets/index-dshZQJCp.js +85 -0
- package/dist/playground/index.html +2 -2
- package/dist/types.d.ts +7 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +9 -0
- package/package.json +24 -24
- package/src/bin/agent-serve.version.test.ts +62 -0
- package/src/channels/github/api.test.ts +64 -0
- package/src/channels/github/auth.test.ts +105 -0
- package/src/channels/github/cursor-account.test.ts +204 -0
- package/src/channels/github/forward.test.ts +457 -0
- package/src/channels/github/github.test.ts +937 -0
- package/src/channels/github/replay.test.ts +179 -0
- package/src/channels/slack/api.post-message.test.ts +148 -0
- package/src/channels/slack/api.ts +5 -0
- package/src/channels/slack/approvals.test.ts +328 -0
- package/src/channels/slack/block-actions.test.ts +452 -0
- package/src/channels/slack/bot-mentions.test.ts +267 -0
- package/src/channels/slack/bot-mentions.ts +339 -0
- package/src/channels/slack/channel-watch.test.ts +363 -0
- package/src/channels/slack/cursor-account.test.ts +253 -0
- package/src/channels/slack/defaults.final-post.test.ts +182 -0
- package/src/channels/slack/dispatch.test.ts +795 -0
- package/src/channels/slack/dispatch.ts +27 -1
- package/src/channels/slack/eval-directive.test.ts +273 -0
- package/src/channels/slack/inbound.ts +25 -4
- package/src/channels/slack/index.ts +1 -0
- package/src/channels/slack/message-body.test.ts +54 -0
- package/src/channels/slack/nudge-store.test.ts +143 -0
- package/src/channels/slack/slack-channel.ts +66 -8
- package/src/channels/slack/slack.test.ts +391 -0
- package/src/channels/slack/stop.test.ts +23 -0
- package/src/channels/slack/thread-context.test.ts +202 -0
- package/src/channels/slack/types.ts +71 -0
- package/src/evals/assertions.test.ts +580 -0
- package/src/evals/expect.test.ts +144 -0
- package/src/evals/judge.test.ts +181 -0
- package/src/evals/loaders.test.ts +132 -0
- package/src/evals/matchers.test.ts +95 -0
- package/src/evals/reporters.test.ts +303 -0
- package/src/evals/run-facts.test.ts +259 -0
- package/src/internal/ab-snapshot.test.ts +325 -0
- package/src/internal/approval-gate.test.ts +49 -0
- package/src/internal/approvals.integration.test.ts +383 -0
- package/src/internal/authored-loaders.test.ts +31 -0
- package/src/internal/builtin-tools/reminders.test.ts +201 -0
- package/src/internal/channel-route-schema.test.ts +294 -0
- package/src/internal/chat-attach.test.ts +262 -0
- package/src/internal/cli-deploy.test.ts +1991 -0
- package/src/internal/cli-mcp.test.ts +789 -0
- package/src/internal/cli-skills.test.ts +133 -0
- package/src/internal/cli-slack.test.ts +1647 -0
- package/src/internal/cloud-merge.test.ts +74 -0
- package/src/internal/cron.test.ts +22 -0
- package/src/internal/cursor/account-mcp.test.ts +807 -0
- package/src/internal/cursor/backend-client.test.ts +591 -0
- package/src/internal/cursor/credentials.test.ts +351 -0
- package/src/internal/cursor/github-credentials.test.ts +136 -0
- package/src/internal/cursor-account-mcp-auth.test.ts +310 -0
- package/src/internal/cursor-account.integration.test.ts +441 -0
- package/src/internal/cursor-event-relay.test.ts +746 -0
- package/src/internal/cursor-github-credentials.integration.test.ts +271 -0
- package/src/internal/cursor-slack-relay.test.ts +525 -0
- package/src/internal/deploy-source.test.ts +111 -0
- package/src/internal/discovery.builtin-tools.test.ts +94 -0
- package/src/internal/discovery.concurrency.test.ts +60 -0
- package/src/internal/discovery.cursor-account.test.ts +133 -0
- package/src/internal/discovery.cwd.test.ts +83 -0
- package/src/internal/discovery.hosting.test.ts +80 -0
- package/src/internal/discovery.identity.test.ts +44 -0
- package/src/internal/docs-site.test.ts +66 -0
- package/src/internal/duration.test.ts +29 -0
- package/src/internal/eval-judge-model.test.ts +187 -0
- package/src/internal/eval-run-store.cancel.test.ts +142 -0
- package/src/internal/eval-run-store.storage.test.ts +211 -0
- package/src/internal/eval-runner.http.test.ts +403 -0
- package/src/internal/eval-runner.run.test.ts +928 -0
- package/src/internal/evals-client.test.ts +307 -0
- package/src/internal/event-mapper.test.ts +243 -0
- package/src/internal/github-fanout.test.ts +213 -0
- package/src/internal/handleAgentServeTrigger.test.ts +179 -0
- package/src/internal/host-kv.test.ts +82 -0
- package/src/internal/host-platforms.test.ts +126 -0
- package/src/internal/http-channel.test.ts +402 -0
- package/src/internal/init-project.test.ts +269 -0
- package/src/internal/install-cursor-skills.test.ts +262 -0
- package/src/internal/local-env.test.ts +120 -0
- package/src/internal/local-env.ts +14 -6
- package/src/internal/log-ring.test.ts +31 -0
- package/src/internal/logs-client.test.ts +350 -0
- package/src/internal/mcp-endpoint.test.ts +436 -0
- package/src/internal/mcp-host.test.ts +298 -0
- package/src/internal/mcp-oauth.test.ts +148 -0
- package/src/internal/net.test.ts +17 -0
- package/src/internal/peer-connections.test.ts +128 -0
- package/src/internal/peer-mcp.integration.test.ts +289 -0
- package/src/internal/playground/toolchain.test.ts +53 -0
- package/src/internal/playground-cli.test.ts +187 -0
- package/src/internal/playground-proxy.test.ts +376 -0
- package/src/internal/prompt-context.integration.test.ts +232 -0
- package/src/internal/prompt-context.test.ts +127 -0
- package/src/internal/reminder-runner.test.ts +390 -0
- package/src/internal/reminder-store.test.ts +53 -0
- package/src/internal/request-headers.test.ts +27 -0
- package/src/internal/resolve-prod-target.test.ts +787 -0
- package/src/internal/resolved-connections.test.ts +295 -0
- package/src/internal/resolved-connections.ts +18 -1
- package/src/internal/router.test.ts +57 -0
- package/src/internal/sdk-runner.test.ts +290 -0
- package/src/internal/session-engine.coalesce.test.ts +169 -0
- package/src/internal/session-engine.concurrency.test.ts +250 -0
- package/src/internal/session-engine.host-oauth-mcp.test.ts +110 -0
- package/src/internal/session-engine.interrupt.test.ts +577 -0
- package/src/internal/session-engine.storage.test.ts +547 -0
- package/src/internal/session-engine.ts +15 -1
- package/src/internal/session-urls.test.ts +28 -0
- package/src/internal/sessions-client.test.ts +518 -0
- package/src/internal/storage-coordinator.test.ts +517 -0
- package/src/internal/tool-call.test.ts +458 -0
- package/src/internal/tool-result.test.ts +52 -0
- package/src/internal/trajectory.approvals.test.ts +83 -0
- package/src/internal/trajectory.subagents.test.ts +198 -0
- package/src/internal/turn-governor.test.ts +137 -0
- package/src/internal/update-check.test.ts +485 -0
- package/src/internal/workspace.test.ts +81 -0
- package/src/storage-backends/cursor-hosted.test.ts +121 -0
- package/src/types.ts +12 -0
- package/dist/docs/assets/chunks/@localSearchIndexroot.CvejISXF.js +0 -1
- package/dist/playground/assets/index-CidizGZv.css +0 -1
- package/dist/playground/assets/index-CuiAKZaP.js +0 -85
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
botMentionsFromEnv,
|
|
4
|
+
buildDelegationMetadata,
|
|
5
|
+
normalizeBotMentionAllowlist,
|
|
6
|
+
parseDelegationMetadata,
|
|
7
|
+
SLACK_DELEGATION_METADATA_EVENT_TYPE,
|
|
8
|
+
SlackBotMentionGate,
|
|
9
|
+
} from "./bot-mentions.js";
|
|
10
|
+
import { parseAppMentionEvent } from "./inbound.js";
|
|
11
|
+
import type { SlackMessage } from "./types.js";
|
|
12
|
+
|
|
13
|
+
function botMention(patch: Partial<SlackMessage> = {}): SlackMessage {
|
|
14
|
+
return {
|
|
15
|
+
text: "<@ULASSO> please deploy #123",
|
|
16
|
+
body: "<@ULASSO> please deploy #123",
|
|
17
|
+
markdown: "@ULASSO please deploy #123",
|
|
18
|
+
ts: "2.0",
|
|
19
|
+
threadTs: "1.0",
|
|
20
|
+
channelId: "C9",
|
|
21
|
+
author: { userId: "ULASSO", isBot: true, botId: "BLASSO" },
|
|
22
|
+
...patch,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function gate(
|
|
27
|
+
options: Partial<ConstructorParameters<typeof SlackBotMentionGate>[0]> = {},
|
|
28
|
+
identity: { botId?: string; botUserId?: string } = {
|
|
29
|
+
botId: "BSELF",
|
|
30
|
+
botUserId: "USELF",
|
|
31
|
+
}
|
|
32
|
+
): SlackBotMentionGate {
|
|
33
|
+
return new SlackBotMentionGate(
|
|
34
|
+
{ allowFrom: ["BLASSO"], ...options },
|
|
35
|
+
identity,
|
|
36
|
+
{ log: { info() {}, warn() {}, error() {}, debug() {}, write() {} } }
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
describe("delegation metadata", () => {
|
|
41
|
+
it("round-trips build → parse", () => {
|
|
42
|
+
const metadata = buildDelegationMetadata({
|
|
43
|
+
depth: 2,
|
|
44
|
+
fromAgent: "lasso",
|
|
45
|
+
originSessionId: "sess-1",
|
|
46
|
+
});
|
|
47
|
+
expect(metadata.event_type).toBe(SLACK_DELEGATION_METADATA_EVENT_TYPE);
|
|
48
|
+
const parsed = parseDelegationMetadata({ metadata });
|
|
49
|
+
expect(parsed).toEqual({
|
|
50
|
+
depth: 2,
|
|
51
|
+
fromAgent: "lasso",
|
|
52
|
+
originSessionId: "sess-1",
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("ignores foreign and malformed metadata", () => {
|
|
57
|
+
expect(parseDelegationMetadata({})).toBeUndefined();
|
|
58
|
+
expect(
|
|
59
|
+
parseDelegationMetadata({
|
|
60
|
+
metadata: { event_type: "other", event_payload: { depth: 1 } },
|
|
61
|
+
})
|
|
62
|
+
).toBeUndefined();
|
|
63
|
+
expect(
|
|
64
|
+
parseDelegationMetadata({
|
|
65
|
+
metadata: {
|
|
66
|
+
event_type: SLACK_DELEGATION_METADATA_EVENT_TYPE,
|
|
67
|
+
event_payload: { depth: 0 },
|
|
68
|
+
},
|
|
69
|
+
})
|
|
70
|
+
).toBeUndefined();
|
|
71
|
+
expect(
|
|
72
|
+
parseDelegationMetadata({
|
|
73
|
+
metadata: {
|
|
74
|
+
event_type: SLACK_DELEGATION_METADATA_EVENT_TYPE,
|
|
75
|
+
event_payload: { depth: "two" },
|
|
76
|
+
},
|
|
77
|
+
})
|
|
78
|
+
).toBeUndefined();
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
describe("botMentionsFromEnv", () => {
|
|
83
|
+
it("returns undefined when unset and options when set", () => {
|
|
84
|
+
const envVar = "BOT_MENTIONS_TEST_VAR";
|
|
85
|
+
delete process.env[envVar];
|
|
86
|
+
expect(botMentionsFromEnv(envVar)).toBeUndefined();
|
|
87
|
+
process.env[envVar] = " B1 ,,U2 ";
|
|
88
|
+
expect(botMentionsFromEnv(envVar)).toEqual({ allowFrom: ["B1", "U2"] });
|
|
89
|
+
delete process.env[envVar];
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
describe("parseAppMentionEvent bot-authored", () => {
|
|
94
|
+
const body = {
|
|
95
|
+
type: "event_callback",
|
|
96
|
+
team_id: "T1",
|
|
97
|
+
event: {
|
|
98
|
+
type: "app_mention",
|
|
99
|
+
user: "ULASSO",
|
|
100
|
+
bot_id: "BLASSO",
|
|
101
|
+
text: "<@UTARGET> look into this",
|
|
102
|
+
ts: "2.0",
|
|
103
|
+
thread_ts: "1.0",
|
|
104
|
+
channel: "C9",
|
|
105
|
+
metadata: buildDelegationMetadata({ depth: 1, fromAgent: "lasso" }),
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
it("drops bot mentions by default", () => {
|
|
110
|
+
expect(parseAppMentionEvent(body)).toBeNull();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("keeps bot mentions with delegation info when opted in", () => {
|
|
114
|
+
const message = parseAppMentionEvent(body, { includeBotAuthored: true });
|
|
115
|
+
expect(message).not.toBeNull();
|
|
116
|
+
expect(message?.author?.isBot).toBe(true);
|
|
117
|
+
expect(message?.author?.botId).toBe("BLASSO");
|
|
118
|
+
expect(message?.delegation).toEqual({ depth: 1, fromAgent: "lasso" });
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("still parses human mentions when opted in", () => {
|
|
122
|
+
const message = parseAppMentionEvent(
|
|
123
|
+
{
|
|
124
|
+
type: "event_callback",
|
|
125
|
+
event: {
|
|
126
|
+
type: "app_mention",
|
|
127
|
+
user: "U1",
|
|
128
|
+
text: "<@UTARGET> hi",
|
|
129
|
+
ts: "3.0",
|
|
130
|
+
channel: "C9",
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
{ includeBotAuthored: true }
|
|
134
|
+
);
|
|
135
|
+
expect(message?.author?.isBot).toBe(false);
|
|
136
|
+
expect(message?.delegation).toBeUndefined();
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
describe("normalizeBotMentionAllowlist", () => {
|
|
141
|
+
it("trims entries and rejects empty lists", () => {
|
|
142
|
+
expect(normalizeBotMentionAllowlist([" B1 ", "U2"])).toEqual(["B1", "U2"]);
|
|
143
|
+
expect(() => normalizeBotMentionAllowlist([" "])).toThrow(/allowFrom/);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
describe("SlackBotMentionGate", () => {
|
|
148
|
+
it("requires its own bot identity", () => {
|
|
149
|
+
expect(() => gate({}, {})).toThrow(/identity/);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("admits an allowlisted bot mention", () => {
|
|
153
|
+
const verdict = gate().evaluate(botMention());
|
|
154
|
+
expect(verdict).toEqual({ allow: true, depth: 1 });
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("matches on bot user id as well as bot_id", () => {
|
|
158
|
+
const verdict = gate({ allowFrom: ["ULASSO"] }).evaluate(botMention());
|
|
159
|
+
expect(verdict.allow).toBe(true);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("matches compound botId:username entries case-insensitively", () => {
|
|
163
|
+
const g = gate({ allowFrom: ["BCURSOR:Lasso"] });
|
|
164
|
+
const fromCursorAsLasso = botMention({
|
|
165
|
+
author: {
|
|
166
|
+
userId: "UCURSOR",
|
|
167
|
+
isBot: true,
|
|
168
|
+
botId: "BCURSOR",
|
|
169
|
+
userName: "lasso",
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
expect(g.evaluate(fromCursorAsLasso).allow).toBe(true);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("compound entries deny other usernames and missing usernames", () => {
|
|
176
|
+
const g = gate({ allowFrom: ["BCURSOR:Lasso"] });
|
|
177
|
+
expect(
|
|
178
|
+
g.evaluate(
|
|
179
|
+
botMention({
|
|
180
|
+
author: {
|
|
181
|
+
userId: "UCURSOR",
|
|
182
|
+
isBot: true,
|
|
183
|
+
botId: "BCURSOR",
|
|
184
|
+
userName: "SomeOtherAgent",
|
|
185
|
+
},
|
|
186
|
+
})
|
|
187
|
+
)
|
|
188
|
+
).toMatchObject({ allow: false });
|
|
189
|
+
// No username on the event → fail closed.
|
|
190
|
+
expect(
|
|
191
|
+
g.evaluate(
|
|
192
|
+
botMention({
|
|
193
|
+
author: { userId: "UCURSOR", isBot: true, botId: "BCURSOR" },
|
|
194
|
+
})
|
|
195
|
+
)
|
|
196
|
+
).toMatchObject({ allow: false });
|
|
197
|
+
// Bare bot_id was not allowlisted, only the compound form.
|
|
198
|
+
expect(
|
|
199
|
+
g.evaluate(
|
|
200
|
+
botMention({
|
|
201
|
+
author: {
|
|
202
|
+
userId: "UCURSOR",
|
|
203
|
+
isBot: true,
|
|
204
|
+
botId: "BCURSOR",
|
|
205
|
+
userName: undefined,
|
|
206
|
+
},
|
|
207
|
+
})
|
|
208
|
+
)
|
|
209
|
+
).toMatchObject({ allow: false });
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("denies bots not in the allowlist", () => {
|
|
213
|
+
const verdict = gate().evaluate(
|
|
214
|
+
botMention({ author: { userId: "UOTHER", isBot: true, botId: "BOTHER" } })
|
|
215
|
+
);
|
|
216
|
+
expect(verdict).toMatchObject({ allow: false });
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("denies its own posts even when allowlisted", () => {
|
|
220
|
+
const verdict = gate({ allowFrom: ["BSELF"] }).evaluate(
|
|
221
|
+
botMention({ author: { userId: "USELF", isBot: true, botId: "BSELF" } })
|
|
222
|
+
);
|
|
223
|
+
expect(verdict).toMatchObject({ allow: false, reason: /own post/ });
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it("enforces the depth budget", () => {
|
|
227
|
+
const g = gate();
|
|
228
|
+
expect(g.evaluate(botMention({ delegation: { depth: 2 } })).allow).toBe(
|
|
229
|
+
true
|
|
230
|
+
);
|
|
231
|
+
expect(
|
|
232
|
+
g.evaluate(botMention({ ts: "3.0", delegation: { depth: 3 } }))
|
|
233
|
+
).toMatchObject({ allow: false, reason: /depth/ });
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it("enforces the rate budget with a sliding window", () => {
|
|
237
|
+
const g = gate({ maxPerThreadPerHour: 2 });
|
|
238
|
+
expect(g.evaluate(botMention(), 1000).allow).toBe(true);
|
|
239
|
+
expect(g.evaluate(botMention(), 2000).allow).toBe(true);
|
|
240
|
+
expect(g.evaluate(botMention(), 3000)).toMatchObject({
|
|
241
|
+
allow: false,
|
|
242
|
+
reason: /rate budget/,
|
|
243
|
+
});
|
|
244
|
+
// Window slides: an hour later the thread has rate budget again.
|
|
245
|
+
const later = 3000 + 60 * 60 * 1000;
|
|
246
|
+
expect(g.evaluate(botMention(), later).allow).toBe(true);
|
|
247
|
+
// Other threads are unaffected.
|
|
248
|
+
expect(g.evaluate(botMention({ threadTs: "9.0" }), 3000).allow).toBe(true);
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it("terminates ping-pong with the lifetime thread cap", () => {
|
|
252
|
+
const g = gate({ maxPerThreadPerHour: 100, maxPerThreadTotal: 3 });
|
|
253
|
+
expect(g.evaluate(botMention(), 1000).allow).toBe(true);
|
|
254
|
+
expect(g.evaluate(botMention(), 2000).allow).toBe(true);
|
|
255
|
+
expect(g.evaluate(botMention(), 3000).allow).toBe(true);
|
|
256
|
+
expect(g.evaluate(botMention(), 4000)).toMatchObject({
|
|
257
|
+
allow: false,
|
|
258
|
+
reason: /lifetime/,
|
|
259
|
+
});
|
|
260
|
+
// Sliding the window does NOT refill the lifetime cap.
|
|
261
|
+
const muchLater = 4000 + 5 * 60 * 60 * 1000;
|
|
262
|
+
expect(g.evaluate(botMention(), muchLater)).toMatchObject({
|
|
263
|
+
allow: false,
|
|
264
|
+
reason: /lifetime/,
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
});
|
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Opt-in bot-to-bot mention wake (`engagement.botMentions`).
|
|
3
|
+
*
|
|
4
|
+
* Layered loop protection, any one of which blocks a runaway chain:
|
|
5
|
+
* 1. Allowlist — only mentions from explicitly trusted bots wake the agent.
|
|
6
|
+
* 2. Own-post filter — the agent's own posts never wake it (always on).
|
|
7
|
+
* 3. Depth budget — delegation metadata carries a hop count; mentions
|
|
8
|
+
* without metadata count as depth 1. Default max is 2.
|
|
9
|
+
* 4. Thread budgets — a sliding-window rate cap plus a lifetime total per
|
|
10
|
+
* thread, so undetected ping-pong between trusted bots terminates
|
|
11
|
+
* instead of merely slowing down.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { postMessage } from "./api.js";
|
|
15
|
+
import { defaultSlackLog, type SlackLog } from "./log.js";
|
|
16
|
+
import type {
|
|
17
|
+
SlackBotMentionsOptions,
|
|
18
|
+
SlackBotToken,
|
|
19
|
+
SlackDelegationInfo,
|
|
20
|
+
SlackMessage,
|
|
21
|
+
} from "./types.js";
|
|
22
|
+
|
|
23
|
+
/** Slack message `metadata.event_type` for agentkit delegation posts. */
|
|
24
|
+
export const SLACK_DELEGATION_METADATA_EVENT_TYPE = "agentkit_delegation";
|
|
25
|
+
|
|
26
|
+
/** Default budgets for {@link SlackBotMentionsOptions}. */
|
|
27
|
+
export const SLACK_BOT_MENTION_DEFAULTS = {
|
|
28
|
+
maxDepth: 2,
|
|
29
|
+
maxPerThreadPerHour: 5,
|
|
30
|
+
maxPerThreadTotal: 20,
|
|
31
|
+
} as const;
|
|
32
|
+
|
|
33
|
+
const THREAD_BUDGET_WINDOW_MS = 60 * 60 * 1000;
|
|
34
|
+
/** Threads idle this long are evicted (their lifetime budget resets). */
|
|
35
|
+
const THREAD_IDLE_EVICT_MS = 24 * 60 * 60 * 1000;
|
|
36
|
+
/** Eviction sweep runs when the thread map grows past this many keys. */
|
|
37
|
+
const THREAD_SWEEP_THRESHOLD = 512;
|
|
38
|
+
|
|
39
|
+
function asRecord(value: unknown): Record<string, unknown> | null {
|
|
40
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
return value as Record<string, unknown>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Parse delegation metadata off a raw Slack event (`event.metadata`).
|
|
48
|
+
* Returns undefined for absent, foreign, or malformed metadata.
|
|
49
|
+
*/
|
|
50
|
+
export function parseDelegationMetadata(
|
|
51
|
+
event: unknown
|
|
52
|
+
): SlackDelegationInfo | undefined {
|
|
53
|
+
const root = asRecord(event);
|
|
54
|
+
const metadata = asRecord(root?.metadata);
|
|
55
|
+
if (
|
|
56
|
+
metadata === null ||
|
|
57
|
+
metadata.event_type !== SLACK_DELEGATION_METADATA_EVENT_TYPE
|
|
58
|
+
) {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
const payload = asRecord(metadata.event_payload);
|
|
62
|
+
if (payload === null) {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
const depth =
|
|
66
|
+
typeof payload.depth === "number" && Number.isFinite(payload.depth)
|
|
67
|
+
? Math.floor(payload.depth)
|
|
68
|
+
: undefined;
|
|
69
|
+
if (depth === undefined || depth < 1) {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
const info: SlackDelegationInfo = { depth };
|
|
73
|
+
if (typeof payload.from_agent === "string" && payload.from_agent !== "") {
|
|
74
|
+
info.fromAgent = payload.from_agent;
|
|
75
|
+
}
|
|
76
|
+
if (
|
|
77
|
+
typeof payload.origin_session_id === "string" &&
|
|
78
|
+
payload.origin_session_id !== ""
|
|
79
|
+
) {
|
|
80
|
+
info.originSessionId = payload.origin_session_id;
|
|
81
|
+
}
|
|
82
|
+
return info;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Build the Slack `metadata` body for an outbound delegation post. */
|
|
86
|
+
export function buildDelegationMetadata(args: {
|
|
87
|
+
depth: number;
|
|
88
|
+
fromAgent?: string;
|
|
89
|
+
originSessionId?: string;
|
|
90
|
+
}): Record<string, unknown> {
|
|
91
|
+
const payload: Record<string, unknown> = { depth: args.depth };
|
|
92
|
+
if (args.fromAgent !== undefined && args.fromAgent !== "") {
|
|
93
|
+
payload.from_agent = args.fromAgent;
|
|
94
|
+
}
|
|
95
|
+
if (args.originSessionId !== undefined && args.originSessionId !== "") {
|
|
96
|
+
payload.origin_session_id = args.originSessionId;
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
event_type: SLACK_DELEGATION_METADATA_EVENT_TYPE,
|
|
100
|
+
event_payload: payload,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Validate and normalize a `botMentions.allowFrom` list. Shared by channel
|
|
106
|
+
* construction (fail at definition time) and the gate (fail at start).
|
|
107
|
+
*/
|
|
108
|
+
export function normalizeBotMentionAllowlist(
|
|
109
|
+
allowFrom: readonly string[]
|
|
110
|
+
): string[] {
|
|
111
|
+
const entries = allowFrom
|
|
112
|
+
.map((entry) => entry.trim())
|
|
113
|
+
.filter((entry) => entry !== "");
|
|
114
|
+
if (entries.length === 0) {
|
|
115
|
+
throw new Error(
|
|
116
|
+
"slackChannel engagement.botMentions.allowFrom must list at least one bot id (B…) or bot user id (U…); there is no wildcard."
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
return entries;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Read `engagement.botMentions` options from the environment: a
|
|
124
|
+
* comma-separated bot-id allowlist in `AGENT_DELEGATOR_SLACK_BOT_IDS` (or a
|
|
125
|
+
* custom var). Returns undefined when unset, so channels can pass the
|
|
126
|
+
* result straight through and stay bot-mention-free by default:
|
|
127
|
+
*
|
|
128
|
+
* ```ts
|
|
129
|
+
* engagement: { botMentions: botMentionsFromEnv() }
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
export function botMentionsFromEnv(
|
|
133
|
+
envVar = "AGENT_DELEGATOR_SLACK_BOT_IDS"
|
|
134
|
+
): SlackBotMentionsOptions | undefined {
|
|
135
|
+
const allowFrom = (process.env[envVar] ?? "")
|
|
136
|
+
.split(",")
|
|
137
|
+
.map((entry) => entry.trim())
|
|
138
|
+
.filter((entry) => entry !== "");
|
|
139
|
+
return allowFrom.length === 0 ? undefined : { allowFrom };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export type SlackBotMentionVerdict =
|
|
143
|
+
| { allow: true; depth: number }
|
|
144
|
+
| { allow: false; reason: string };
|
|
145
|
+
|
|
146
|
+
interface ThreadBudget {
|
|
147
|
+
/** Lifetime admitted wakes for this thread (until idle eviction). */
|
|
148
|
+
total: number;
|
|
149
|
+
/** Timestamps of recent admitted wakes (pruned to the window). */
|
|
150
|
+
recent: number[];
|
|
151
|
+
lastAtMs: number;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Admission gate for bot-authored mentions. One instance per channel;
|
|
156
|
+
* thread budgets are in-memory (reset on restart — acceptable, the depth
|
|
157
|
+
* and allowlist layers do not depend on process state). Threads idle for
|
|
158
|
+
* 24h are evicted, which also resets their lifetime budget.
|
|
159
|
+
*/
|
|
160
|
+
export class SlackBotMentionGate {
|
|
161
|
+
private readonly allowFrom: Set<string>;
|
|
162
|
+
/** Compound entries: posting `bot_id` → allowed lowercase usernames. */
|
|
163
|
+
private readonly allowFromCompound: Map<string, Set<string>>;
|
|
164
|
+
private readonly maxDepth: number;
|
|
165
|
+
private readonly maxPerThreadPerHour: number;
|
|
166
|
+
private readonly maxPerThreadTotal: number;
|
|
167
|
+
private readonly windowMs: number;
|
|
168
|
+
private readonly threadBudgets = new Map<string, ThreadBudget>();
|
|
169
|
+
private readonly log: SlackLog;
|
|
170
|
+
|
|
171
|
+
constructor(
|
|
172
|
+
options: SlackBotMentionsOptions,
|
|
173
|
+
private readonly identity: { botUserId?: string; botId?: string },
|
|
174
|
+
args?: { log?: SlackLog; windowMs?: number }
|
|
175
|
+
) {
|
|
176
|
+
if (identity.botUserId === undefined && identity.botId === undefined) {
|
|
177
|
+
throw new Error(
|
|
178
|
+
"SlackBotMentionGate requires the agent's own bot identity (auth.test) for loop safety."
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
this.allowFrom = new Set();
|
|
182
|
+
this.allowFromCompound = new Map();
|
|
183
|
+
for (const entry of normalizeBotMentionAllowlist(options.allowFrom)) {
|
|
184
|
+
const separator = entry.indexOf(":");
|
|
185
|
+
if (separator <= 0 || separator === entry.length - 1) {
|
|
186
|
+
this.allowFrom.add(entry);
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
const botId = entry.slice(0, separator);
|
|
190
|
+
const username = entry.slice(separator + 1).toLowerCase();
|
|
191
|
+
const usernames = this.allowFromCompound.get(botId) ?? new Set();
|
|
192
|
+
usernames.add(username);
|
|
193
|
+
this.allowFromCompound.set(botId, usernames);
|
|
194
|
+
}
|
|
195
|
+
this.maxDepth = options.maxDepth ?? SLACK_BOT_MENTION_DEFAULTS.maxDepth;
|
|
196
|
+
this.maxPerThreadPerHour =
|
|
197
|
+
options.maxPerThreadPerHour ??
|
|
198
|
+
SLACK_BOT_MENTION_DEFAULTS.maxPerThreadPerHour;
|
|
199
|
+
this.maxPerThreadTotal =
|
|
200
|
+
options.maxPerThreadTotal ?? SLACK_BOT_MENTION_DEFAULTS.maxPerThreadTotal;
|
|
201
|
+
this.windowMs = args?.windowMs ?? THREAD_BUDGET_WINDOW_MS;
|
|
202
|
+
this.log = args?.log ?? defaultSlackLog;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Evaluate a bot-authored mention. Callers must only pass messages whose
|
|
207
|
+
* author `isBot`; human mentions never consult the gate. An `allow`
|
|
208
|
+
* verdict consumes thread budget.
|
|
209
|
+
*/
|
|
210
|
+
evaluate(
|
|
211
|
+
message: SlackMessage,
|
|
212
|
+
nowMs: number = Date.now()
|
|
213
|
+
): SlackBotMentionVerdict {
|
|
214
|
+
const author = message.author;
|
|
215
|
+
if (author === undefined || !author.isBot) {
|
|
216
|
+
return { allow: false, reason: "not bot-authored" };
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Own posts never wake the agent, allowlisted or not.
|
|
220
|
+
if (
|
|
221
|
+
(this.identity.botId !== undefined &&
|
|
222
|
+
author.botId === this.identity.botId) ||
|
|
223
|
+
(this.identity.botUserId !== undefined &&
|
|
224
|
+
author.userId !== "" &&
|
|
225
|
+
author.userId === this.identity.botUserId)
|
|
226
|
+
) {
|
|
227
|
+
return { allow: false, reason: "own post (loop safety)" };
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const compoundUsernames =
|
|
231
|
+
author.botId === undefined
|
|
232
|
+
? undefined
|
|
233
|
+
: this.allowFromCompound.get(author.botId);
|
|
234
|
+
const allowed =
|
|
235
|
+
(author.botId !== undefined && this.allowFrom.has(author.botId)) ||
|
|
236
|
+
(author.userId !== "" && this.allowFrom.has(author.userId)) ||
|
|
237
|
+
// Compound entry: shared app bot_id + server-pinned username. Fails
|
|
238
|
+
// closed when the event carries no username.
|
|
239
|
+
(compoundUsernames !== undefined &&
|
|
240
|
+
author.userName !== undefined &&
|
|
241
|
+
compoundUsernames.has(author.userName.toLowerCase()));
|
|
242
|
+
if (!allowed) {
|
|
243
|
+
return { allow: false, reason: "author not in botMentions.allowFrom" };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const depth = message.delegation?.depth ?? 1;
|
|
247
|
+
if (depth > this.maxDepth) {
|
|
248
|
+
return {
|
|
249
|
+
allow: false,
|
|
250
|
+
reason: `delegation depth ${depth} exceeds max ${this.maxDepth}`,
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
this.maybeEvictIdleThreads(nowMs);
|
|
255
|
+
const key = `${message.channelId}:${message.threadTs}`;
|
|
256
|
+
const budget = this.threadBudgets.get(key) ?? {
|
|
257
|
+
total: 0,
|
|
258
|
+
recent: [],
|
|
259
|
+
lastAtMs: nowMs,
|
|
260
|
+
};
|
|
261
|
+
budget.recent = budget.recent.filter(
|
|
262
|
+
(atMs) => atMs > nowMs - this.windowMs
|
|
263
|
+
);
|
|
264
|
+
if (budget.total >= this.maxPerThreadTotal) {
|
|
265
|
+
return {
|
|
266
|
+
allow: false,
|
|
267
|
+
reason: `thread lifetime budget exhausted (${this.maxPerThreadTotal})`,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
if (budget.recent.length >= this.maxPerThreadPerHour) {
|
|
271
|
+
return {
|
|
272
|
+
allow: false,
|
|
273
|
+
reason: `thread rate budget exhausted (${this.maxPerThreadPerHour}/window)`,
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
budget.total += 1;
|
|
277
|
+
budget.recent.push(nowMs);
|
|
278
|
+
budget.lastAtMs = nowMs;
|
|
279
|
+
this.threadBudgets.set(key, budget);
|
|
280
|
+
this.log.info("bot mention admitted", {
|
|
281
|
+
channel: message.channelId,
|
|
282
|
+
thread_ts: message.threadTs,
|
|
283
|
+
from_bot: author.botId ?? author.userId,
|
|
284
|
+
depth,
|
|
285
|
+
thread_total: budget.total,
|
|
286
|
+
});
|
|
287
|
+
return { allow: true, depth };
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
private maybeEvictIdleThreads(nowMs: number): void {
|
|
291
|
+
if (this.threadBudgets.size < THREAD_SWEEP_THRESHOLD) {
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
for (const [key, budget] of this.threadBudgets) {
|
|
295
|
+
if (nowMs - budget.lastAtMs > THREAD_IDLE_EVICT_MS) {
|
|
296
|
+
this.threadBudgets.delete(key);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Post an @mention of another bot with delegation metadata attached, so the
|
|
304
|
+
* target's `engagement.botMentions` gate can read the hop count. Use this
|
|
305
|
+
* from dispatcher tools instead of a bare `chat.postMessage`.
|
|
306
|
+
*/
|
|
307
|
+
export async function postDelegationMention(args: {
|
|
308
|
+
botToken: SlackBotToken | undefined;
|
|
309
|
+
channelId: string;
|
|
310
|
+
/** Target bot's Slack *user* id (`U…`) — what `<@…>` mentions require. */
|
|
311
|
+
targetUserId: string;
|
|
312
|
+
text: string;
|
|
313
|
+
threadTs?: string;
|
|
314
|
+
/** Defaults to 1 (dispatcher woken by a human). */
|
|
315
|
+
depth?: number;
|
|
316
|
+
fromAgent?: string;
|
|
317
|
+
originSessionId?: string;
|
|
318
|
+
}): Promise<{ ts: string }> {
|
|
319
|
+
const posted = await postMessage({
|
|
320
|
+
botToken: args.botToken,
|
|
321
|
+
channel: args.channelId,
|
|
322
|
+
text: `<@${args.targetUserId}> ${args.text}`.trim(),
|
|
323
|
+
threadTs: args.threadTs,
|
|
324
|
+
// Mention markup only notifies via mrkdwn `text`, and metadata must
|
|
325
|
+
// ride the same chat.postMessage call.
|
|
326
|
+
plainText: true,
|
|
327
|
+
metadata: buildDelegationMetadata({
|
|
328
|
+
depth: args.depth ?? 1,
|
|
329
|
+
fromAgent: args.fromAgent,
|
|
330
|
+
originSessionId: args.originSessionId,
|
|
331
|
+
}),
|
|
332
|
+
});
|
|
333
|
+
if (posted.ts === "") {
|
|
334
|
+
throw new Error(
|
|
335
|
+
"Slack delegation mention posted without a message ts; cannot track the handoff."
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
return { ts: posted.ts };
|
|
339
|
+
}
|