@mosadd/mcp 3.0.0-alpha.32 → 3.0.0-alpha.33
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/CHANGELOG.md +74 -0
- package/README.md +11 -11
- package/dist/bin/mcp.js +1 -1
- package/dist/{chunk-KX34DDJB.js → chunk-IWLN5EO5.js} +614 -134
- package/dist/index.js +1 -1
- package/package.json +15 -15
- package/src/__tests__/golden-path.test.ts +137 -137
- package/src/__tests__/integration.test.ts +179 -179
- package/src/__tests__/knowledge-ingest.test.ts +114 -0
- package/src/__tests__/mdm-e2ee.test.ts +212 -212
- package/src/__tests__/mtalk.test.ts +122 -122
- package/src/__tests__/presence.test.ts +141 -0
- package/src/__tests__/server.test.ts +71 -71
- package/src/__tests__/tool-count-consistency.test.ts +121 -1
- package/src/bin/login.ts +138 -138
- package/src/bin/mcp.ts +145 -145
- package/src/config.ts +66 -66
- package/src/crypto/mdm-session.ts +502 -502
- package/src/index.ts +19 -19
- package/src/observability/sentry.ts +79 -79
- package/src/providers/blob-upload.ts +116 -116
- package/src/providers/memory-voice.ts +79 -79
- package/src/providers/supabase-dm.ts +122 -122
- package/src/providers/supabase-voice.ts +91 -91
- package/src/providers/supabase.ts +148 -148
- package/src/tools/actions.ts +162 -162
- package/src/tools/attachments.ts +268 -268
- package/src/tools/capabilities.ts +68 -68
- package/src/tools/embed.ts +108 -108
- package/src/tools/index.ts +173 -104
- package/src/tools/knowledge-graph.ts +178 -0
- package/src/tools/knowledge.ts +319 -191
- package/src/tools/mail-agentbox.ts +147 -0
- package/src/tools/mail-aliases.ts +25 -25
- package/src/tools/mail-provenance.ts +135 -135
- package/src/tools/mail.ts +381 -381
- package/src/tools/mdm.ts +476 -479
- package/src/tools/mirc-edge.ts +190 -190
- package/src/tools/mirc-members.ts +2 -2
- package/src/tools/mirc-messages.ts +155 -155
- package/src/tools/mirc.ts +2 -2
- package/src/tools/mroom-messages.ts +142 -142
- package/src/tools/mroom.ts +221 -221
- package/src/tools/mtalk.ts +121 -121
- package/src/tools/murl.ts +4 -2
- package/src/tools/presence.ts +208 -0
- package/src/types.ts +94 -94
- package/src/voice/floor.ts +132 -132
- package/LICENSE +0 -201
- package/dist/bin/login.d.ts +0 -13
- package/dist/bin/mcp.d.ts +0 -1
- package/dist/index.d.ts +0 -266
package/src/tools/mtalk.ts
CHANGED
|
@@ -1,121 +1,121 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* mTALK — Push-to-Talk (the kill feature).
|
|
3
|
-
*
|
|
4
|
-
* Half-duplex voice over a real-time room, with a walkie-talkie floor
|
|
5
|
-
* discipline: one transmitter at a time, others queue, and an anti-hog timeout
|
|
6
|
-
* keeps the floor moving. An AGENT uses these tools to: spin up a PTT room, get
|
|
7
|
-
* join credentials for a human/bot client, and drive the floor (press/release
|
|
8
|
-
* PTT, see who's transmitting). The audio itself flows over the media transport
|
|
9
|
-
* the VoiceProvider points at (LiveKit by default); these tools coordinate WHO
|
|
10
|
-
* may transmit, which is the part that makes a PTT channel work.
|
|
11
|
-
*
|
|
12
|
-
* Transport-agnostic: the same tools drive an off-grid radio host, where the
|
|
13
|
-
* floor is the physical RF channel (the host injects its own VoiceProvider).
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
import { z } from "zod";
|
|
17
|
-
import type { MosaddTool, MosaddToolContext } from "../types.js";
|
|
18
|
-
|
|
19
|
-
const RoomLabel = z
|
|
20
|
-
.string()
|
|
21
|
-
.min(1)
|
|
22
|
-
.max(120)
|
|
23
|
-
.regex(/^[a-zA-Z0-9._-]+$/)
|
|
24
|
-
.describe("Human-readable room label (e.g. 'ops', 'field-team'). Used to derive a stable room id.");
|
|
25
|
-
|
|
26
|
-
const RoomId = z.string().min(1).describe("Room id returned by mTALK_open.");
|
|
27
|
-
|
|
28
|
-
const mTALK_open_input = z.object({
|
|
29
|
-
label: RoomLabel.optional().describe("Optional label; defaults to a personal room."),
|
|
30
|
-
});
|
|
31
|
-
const mTALK_join_input = z.object({ room_id: RoomId });
|
|
32
|
-
const mTALK_press_input = z.object({ room_id: RoomId });
|
|
33
|
-
const mTALK_release_input = z.object({ room_id: RoomId });
|
|
34
|
-
const mTALK_state_input = z.object({ room_id: RoomId });
|
|
35
|
-
|
|
36
|
-
async function mTALK_open(
|
|
37
|
-
input: z.infer<typeof mTALK_open_input>,
|
|
38
|
-
ctx: MosaddToolContext,
|
|
39
|
-
): Promise<{ room_id: string }> {
|
|
40
|
-
const { roomId } = await ctx.providers.voice.createRoom(input.label);
|
|
41
|
-
ctx.log("debug", "mTALK_open", { room_id: roomId });
|
|
42
|
-
return { room_id: roomId };
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
async function mTALK_join(
|
|
46
|
-
input: z.infer<typeof mTALK_join_input>,
|
|
47
|
-
ctx: MosaddToolContext,
|
|
48
|
-
): Promise<{ room_id: string; token: string; url: string; identity: string }> {
|
|
49
|
-
const ticket = await ctx.providers.voice.joinTicket(input.room_id);
|
|
50
|
-
ctx.log("debug", "mTALK_join", { room_id: ticket.roomId, identity: ticket.identity });
|
|
51
|
-
return { room_id: ticket.roomId, token: ticket.token, url: ticket.url, identity: ticket.identity };
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
async function mTALK_press(
|
|
55
|
-
input: z.infer<typeof mTALK_press_input>,
|
|
56
|
-
ctx: MosaddToolContext,
|
|
57
|
-
): Promise<{ granted: boolean; position: number; holder: string | null; queue: string[] }> {
|
|
58
|
-
const res = await ctx.providers.voice.requestFloor(input.room_id);
|
|
59
|
-
ctx.log("debug", "mTALK_press", { room_id: input.room_id, granted: res.granted, position: res.position });
|
|
60
|
-
return { granted: res.granted, position: res.position, holder: res.snapshot.holder, queue: res.snapshot.queue };
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
async function mTALK_release(
|
|
64
|
-
input: z.infer<typeof mTALK_release_input>,
|
|
65
|
-
ctx: MosaddToolContext,
|
|
66
|
-
): Promise<{ holder: string | null; queue: string[] }> {
|
|
67
|
-
const snap = await ctx.providers.voice.releaseFloor(input.room_id);
|
|
68
|
-
ctx.log("debug", "mTALK_release", { room_id: input.room_id, holder: snap.holder });
|
|
69
|
-
return { holder: snap.holder, queue: snap.queue };
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
async function mTALK_state(
|
|
73
|
-
input: z.infer<typeof mTALK_state_input>,
|
|
74
|
-
ctx: MosaddToolContext,
|
|
75
|
-
): Promise<{ holder: string | null; since: string | null; queue: string[] }> {
|
|
76
|
-
const snap = await ctx.providers.voice.floorState(input.room_id);
|
|
77
|
-
return { holder: snap.holder, since: snap.since, queue: snap.queue };
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export const mtalkTools: MosaddTool[] = [
|
|
81
|
-
{
|
|
82
|
-
name: "mTALK_open",
|
|
83
|
-
requires: "any",
|
|
84
|
-
description:
|
|
85
|
-
"Open (or get) a push-to-talk room. Returns a stable room_id. Pass an optional label to share a named room (e.g. 'ops'); omit it for a personal room. Works over network (LiveKit) or off-grid radio depending on the host.",
|
|
86
|
-
inputSchema: mTALK_open_input,
|
|
87
|
-
handler: mTALK_open as MosaddTool["handler"],
|
|
88
|
-
},
|
|
89
|
-
{
|
|
90
|
-
name: "mTALK_join",
|
|
91
|
-
requires: "network",
|
|
92
|
-
description:
|
|
93
|
-
"Get credentials (token + media URL) to join a PTT room's audio transport. Hand these to a human or bot client to connect. The agent itself drives the floor with mTALK_press / mTALK_release.",
|
|
94
|
-
inputSchema: mTALK_join_input,
|
|
95
|
-
handler: mTALK_join as MosaddTool["handler"],
|
|
96
|
-
},
|
|
97
|
-
{
|
|
98
|
-
name: "mTALK_press",
|
|
99
|
-
requires: "any",
|
|
100
|
-
description:
|
|
101
|
-
"Press push-to-talk: request the floor in a room. Half-duplex — if no one is transmitting you are GRANTED the floor (granted=true) and may speak; otherwise you are queued FIFO and `position` tells you where you are. Call mTALK_release when done. A holder who never releases is auto-bumped after the anti-hog timeout.",
|
|
102
|
-
inputSchema: mTALK_press_input,
|
|
103
|
-
handler: mTALK_press as MosaddTool["handler"],
|
|
104
|
-
},
|
|
105
|
-
{
|
|
106
|
-
name: "mTALK_release",
|
|
107
|
-
requires: "any",
|
|
108
|
-
description:
|
|
109
|
-
"Release push-to-talk: give up the floor (or cancel your queued request). If you held the floor, the next participant in the queue is promoted automatically. Returns the new holder and queue.",
|
|
110
|
-
inputSchema: mTALK_release_input,
|
|
111
|
-
handler: mTALK_release as MosaddTool["handler"],
|
|
112
|
-
},
|
|
113
|
-
{
|
|
114
|
-
name: "mTALK_state",
|
|
115
|
-
requires: "any",
|
|
116
|
-
description:
|
|
117
|
-
"Read a PTT room's floor state without changing it: who is currently transmitting (holder), since when, and the waiting queue.",
|
|
118
|
-
inputSchema: mTALK_state_input,
|
|
119
|
-
handler: mTALK_state as MosaddTool["handler"],
|
|
120
|
-
},
|
|
121
|
-
];
|
|
1
|
+
/**
|
|
2
|
+
* mTALK — Push-to-Talk (the kill feature).
|
|
3
|
+
*
|
|
4
|
+
* Half-duplex voice over a real-time room, with a walkie-talkie floor
|
|
5
|
+
* discipline: one transmitter at a time, others queue, and an anti-hog timeout
|
|
6
|
+
* keeps the floor moving. An AGENT uses these tools to: spin up a PTT room, get
|
|
7
|
+
* join credentials for a human/bot client, and drive the floor (press/release
|
|
8
|
+
* PTT, see who's transmitting). The audio itself flows over the media transport
|
|
9
|
+
* the VoiceProvider points at (LiveKit by default); these tools coordinate WHO
|
|
10
|
+
* may transmit, which is the part that makes a PTT channel work.
|
|
11
|
+
*
|
|
12
|
+
* Transport-agnostic: the same tools drive an off-grid radio host, where the
|
|
13
|
+
* floor is the physical RF channel (the host injects its own VoiceProvider).
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { z } from "zod";
|
|
17
|
+
import type { MosaddTool, MosaddToolContext } from "../types.js";
|
|
18
|
+
|
|
19
|
+
const RoomLabel = z
|
|
20
|
+
.string()
|
|
21
|
+
.min(1)
|
|
22
|
+
.max(120)
|
|
23
|
+
.regex(/^[a-zA-Z0-9._-]+$/)
|
|
24
|
+
.describe("Human-readable room label (e.g. 'ops', 'field-team'). Used to derive a stable room id.");
|
|
25
|
+
|
|
26
|
+
const RoomId = z.string().min(1).describe("Room id returned by mTALK_open.");
|
|
27
|
+
|
|
28
|
+
const mTALK_open_input = z.object({
|
|
29
|
+
label: RoomLabel.optional().describe("Optional label; defaults to a personal room."),
|
|
30
|
+
});
|
|
31
|
+
const mTALK_join_input = z.object({ room_id: RoomId });
|
|
32
|
+
const mTALK_press_input = z.object({ room_id: RoomId });
|
|
33
|
+
const mTALK_release_input = z.object({ room_id: RoomId });
|
|
34
|
+
const mTALK_state_input = z.object({ room_id: RoomId });
|
|
35
|
+
|
|
36
|
+
async function mTALK_open(
|
|
37
|
+
input: z.infer<typeof mTALK_open_input>,
|
|
38
|
+
ctx: MosaddToolContext,
|
|
39
|
+
): Promise<{ room_id: string }> {
|
|
40
|
+
const { roomId } = await ctx.providers.voice.createRoom(input.label);
|
|
41
|
+
ctx.log("debug", "mTALK_open", { room_id: roomId });
|
|
42
|
+
return { room_id: roomId };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function mTALK_join(
|
|
46
|
+
input: z.infer<typeof mTALK_join_input>,
|
|
47
|
+
ctx: MosaddToolContext,
|
|
48
|
+
): Promise<{ room_id: string; token: string; url: string; identity: string }> {
|
|
49
|
+
const ticket = await ctx.providers.voice.joinTicket(input.room_id);
|
|
50
|
+
ctx.log("debug", "mTALK_join", { room_id: ticket.roomId, identity: ticket.identity });
|
|
51
|
+
return { room_id: ticket.roomId, token: ticket.token, url: ticket.url, identity: ticket.identity };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function mTALK_press(
|
|
55
|
+
input: z.infer<typeof mTALK_press_input>,
|
|
56
|
+
ctx: MosaddToolContext,
|
|
57
|
+
): Promise<{ granted: boolean; position: number; holder: string | null; queue: string[] }> {
|
|
58
|
+
const res = await ctx.providers.voice.requestFloor(input.room_id);
|
|
59
|
+
ctx.log("debug", "mTALK_press", { room_id: input.room_id, granted: res.granted, position: res.position });
|
|
60
|
+
return { granted: res.granted, position: res.position, holder: res.snapshot.holder, queue: res.snapshot.queue };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function mTALK_release(
|
|
64
|
+
input: z.infer<typeof mTALK_release_input>,
|
|
65
|
+
ctx: MosaddToolContext,
|
|
66
|
+
): Promise<{ holder: string | null; queue: string[] }> {
|
|
67
|
+
const snap = await ctx.providers.voice.releaseFloor(input.room_id);
|
|
68
|
+
ctx.log("debug", "mTALK_release", { room_id: input.room_id, holder: snap.holder });
|
|
69
|
+
return { holder: snap.holder, queue: snap.queue };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function mTALK_state(
|
|
73
|
+
input: z.infer<typeof mTALK_state_input>,
|
|
74
|
+
ctx: MosaddToolContext,
|
|
75
|
+
): Promise<{ holder: string | null; since: string | null; queue: string[] }> {
|
|
76
|
+
const snap = await ctx.providers.voice.floorState(input.room_id);
|
|
77
|
+
return { holder: snap.holder, since: snap.since, queue: snap.queue };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export const mtalkTools: MosaddTool[] = [
|
|
81
|
+
{
|
|
82
|
+
name: "mTALK_open",
|
|
83
|
+
requires: "any",
|
|
84
|
+
description:
|
|
85
|
+
"Open (or get) a push-to-talk room. Returns a stable room_id. Pass an optional label to share a named room (e.g. 'ops'); omit it for a personal room. Works over network (LiveKit) or off-grid radio depending on the host.",
|
|
86
|
+
inputSchema: mTALK_open_input,
|
|
87
|
+
handler: mTALK_open as MosaddTool["handler"],
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
name: "mTALK_join",
|
|
91
|
+
requires: "network",
|
|
92
|
+
description:
|
|
93
|
+
"Get credentials (token + media URL) to join a PTT room's audio transport. Hand these to a human or bot client to connect. The agent itself drives the floor with mTALK_press / mTALK_release.",
|
|
94
|
+
inputSchema: mTALK_join_input,
|
|
95
|
+
handler: mTALK_join as MosaddTool["handler"],
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
name: "mTALK_press",
|
|
99
|
+
requires: "any",
|
|
100
|
+
description:
|
|
101
|
+
"Press push-to-talk: request the floor in a room. Half-duplex — if no one is transmitting you are GRANTED the floor (granted=true) and may speak; otherwise you are queued FIFO and `position` tells you where you are. Call mTALK_release when done. A holder who never releases is auto-bumped after the anti-hog timeout.",
|
|
102
|
+
inputSchema: mTALK_press_input,
|
|
103
|
+
handler: mTALK_press as MosaddTool["handler"],
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
name: "mTALK_release",
|
|
107
|
+
requires: "any",
|
|
108
|
+
description:
|
|
109
|
+
"Release push-to-talk: give up the floor (or cancel your queued request). If you held the floor, the next participant in the queue is promoted automatically. Returns the new holder and queue.",
|
|
110
|
+
inputSchema: mTALK_release_input,
|
|
111
|
+
handler: mTALK_release as MosaddTool["handler"],
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
name: "mTALK_state",
|
|
115
|
+
requires: "any",
|
|
116
|
+
description:
|
|
117
|
+
"Read a PTT room's floor state without changing it: who is currently transmitting (holder), since when, and the waiting queue.",
|
|
118
|
+
inputSchema: mTALK_state_input,
|
|
119
|
+
handler: mTALK_state as MosaddTool["handler"],
|
|
120
|
+
},
|
|
121
|
+
];
|
package/src/tools/murl.ts
CHANGED
|
@@ -183,7 +183,9 @@ const mURL_create_input = z.object({
|
|
|
183
183
|
status: z
|
|
184
184
|
.enum(["open", "claimed", "blocked"])
|
|
185
185
|
.optional()
|
|
186
|
-
.describe(
|
|
186
|
+
.describe(
|
|
187
|
+
"Status for a channel that does NOT exist yet: 'open' (default) = public agent-native room; 'claimed' = registered as yours; 'blocked' = closed. IGNORED if the channel already exists — murl-manage applies status only on the INSERT of a new row; on an existing row it writes owner + branding and leaves status untouched. mURL rooms auto-create on first activity, so an existing room is the common case: change its status with mURL_update.",
|
|
188
|
+
),
|
|
187
189
|
});
|
|
188
190
|
async function mURL_create(
|
|
189
191
|
input: z.infer<typeof mURL_create_input>,
|
|
@@ -275,7 +277,7 @@ export const murlTools: MosaddTool[] = [
|
|
|
275
277
|
name: "mURL_create",
|
|
276
278
|
requires: "network",
|
|
277
279
|
description:
|
|
278
|
-
"Create / claim the mURL channel for a web DOMAIN and register YOU as its owner (idempotent — returns the existing channel if you already own it; fails with 'claimed_by_other' if another account owns it). Optionally set
|
|
280
|
+
"Create / claim the mURL channel for a web DOMAIN and register YOU as its owner (idempotent — returns the existing channel if you already own it; fails with 'claimed_by_other' if another account owns it). Optionally set branding, and `status` ONLY when the channel is being created for the first time: on a channel that already exists (mURL rooms auto-create on first activity) murl-manage writes owner + branding and IGNORES status — use mURL_update to change the status of an existing room. Owner-side: needs your mosADD login session (user JWT via `mosadd login`), not just a hub key — same as mURL_list_channels action='mine'. Backend: murl-manage EF.",
|
|
279
281
|
inputSchema: mURL_create_input,
|
|
280
282
|
handler: mURL_create as MosaddTool["handler"],
|
|
281
283
|
},
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* comms_session_attach — a LIVE agent session claims the account's reply lane.
|
|
3
|
+
*
|
|
4
|
+
* WHY THIS EXISTS (measured 2026-08-14). mosADD answers a DM in one of two ways: a real agent
|
|
5
|
+
* session (a Claude Code / Cowork / Desktop session holding this MCP server) or a cloud stand-in
|
|
6
|
+
* (`agent-dm-responder`, pg_cron, small model). They must never both answer, and the stand-in must
|
|
7
|
+
* never speak in the session's name — the founder caught exactly that twice, and stopped trusting
|
|
8
|
+
* the agent afterwards.
|
|
9
|
+
*
|
|
10
|
+
* The arbitration already existed: `agent_bridge_heartbeat`. A fresh row = "somebody real is on
|
|
11
|
+
* this line", and the cron stands down. But the ONLY thing that could write that row was a bespoke
|
|
12
|
+
* script on the founder's own PC (`mosadd-agent/bridge-claude.mjs`). So "a real Claude answers you"
|
|
13
|
+
* meant "the founder keeps a window open on one machine" — and that machine went quiet on
|
|
14
|
+
* 2026-08-05 while the account kept looking alive. Four of his five agent contacts had nobody home.
|
|
15
|
+
*
|
|
16
|
+
* This tool moves the beat into the MCP server itself, so ANY session anywhere — his laptop, a
|
|
17
|
+
* VPS, the legal dispatcher, a cloud runner — attaches with one call and the stand-in defers to it
|
|
18
|
+
* for as long as that session's process lives.
|
|
19
|
+
*
|
|
20
|
+
* ⭐ THE BEAT IS NOT THE MODEL'S JOB. The heartbeat window is minutes wide, and a model cannot be
|
|
21
|
+
* relied on to call a tool on a timer — it answers prompts, it does not hold a clock. So attaching
|
|
22
|
+
* starts an internal keep-alive interval inside THIS process. The signal is therefore "the session
|
|
23
|
+
* process is alive", not "the model remembered", which is the honest thing to measure: when the
|
|
24
|
+
* session dies (limit, crash, closed window) the beats stop with it and the cloud takes over on
|
|
25
|
+
* its own. The timer is `unref()`ed so it can never keep a process alive that would otherwise exit.
|
|
26
|
+
*
|
|
27
|
+
* ⚠ MULTI-TENANCY. The hosted HTTP gateway serves many users from one process and resolves each
|
|
28
|
+
* caller's session into an AsyncLocalStorage store (see providers/supabase.ts). A background timer
|
|
29
|
+
* fires OUTSIDE that store, so it must not call `readSupabaseEnv()` later — it would read another
|
|
30
|
+
* tenant's env or throw. The env is captured at attach time and passed explicitly to
|
|
31
|
+
* `getSupabase(env)` on every beat, and attachments are tracked per identity, not per process.
|
|
32
|
+
*
|
|
33
|
+
* Security: the write is plain RLS, no privileged path. The policy on agent_bridge_heartbeat is
|
|
34
|
+
* `identity_id IN (SELECT id FROM identities WHERE user_id = auth.uid())` for ALL commands
|
|
35
|
+
* (verified on prod), so a session can only ever claim the account whose key it holds.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
import { hostname } from "node:os";
|
|
39
|
+
import { z } from "zod";
|
|
40
|
+
import type { MosaddTool, MosaddToolContext } from "../types.js";
|
|
41
|
+
import { getSupabase, readSupabaseEnv, type SupabaseEnv } from "../providers/supabase.js";
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* How often we re-stamp the row. The consumer (`agent-dm-responder`, BRIDGE_FRESH_MS) trusts a beat
|
|
45
|
+
* for 5 minutes; beating every 60s means two consecutive failures still leave three minutes of
|
|
46
|
+
* cover. Do not raise this above ~2 min without raising the window on that side first — the
|
|
47
|
+
* founder has already lived through the failure where the beat aged out MID-ANSWER and the cloud
|
|
48
|
+
* posted a competing reply next to the real one.
|
|
49
|
+
*/
|
|
50
|
+
const BEAT_MS = 60_000;
|
|
51
|
+
|
|
52
|
+
/** Advertised to the caller so it knows how long an attachment survives without this process. */
|
|
53
|
+
const HOLD_SECONDS = 300;
|
|
54
|
+
|
|
55
|
+
interface Attachment {
|
|
56
|
+
timer: ReturnType<typeof setInterval>;
|
|
57
|
+
env: SupabaseEnv;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** identity_id → its keep-alive. Keyed per identity so one gateway process can hold many. */
|
|
61
|
+
const attachments = new Map<string, Attachment>();
|
|
62
|
+
|
|
63
|
+
/** One upsert of the liveness row. Best-effort by design: a missed beat must never throw into a timer. */
|
|
64
|
+
async function beat(env: SupabaseEnv, identityId: string, host: string, mode: string): Promise<void> {
|
|
65
|
+
await getSupabase(env)
|
|
66
|
+
.from("agent_bridge_heartbeat")
|
|
67
|
+
.upsert(
|
|
68
|
+
{ identity_id: identityId, last_seen_at: new Date().toISOString(), host, mode },
|
|
69
|
+
{ onConflict: "identity_id" },
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Resolve the identity row behind the current credentials.
|
|
75
|
+
*
|
|
76
|
+
* Same shape as mdm.ts's resolveSelfIdentityId — kept local rather than exported/shared because
|
|
77
|
+
* that one is private to mDM and this file must not reach into another module's internals.
|
|
78
|
+
* The identity id is NOT the auth user id; the heartbeat table is keyed on the identity.
|
|
79
|
+
*/
|
|
80
|
+
async function resolveSelf(env: SupabaseEnv): Promise<{ id: string; display_name: string | null }> {
|
|
81
|
+
const sb = getSupabase(env);
|
|
82
|
+
const { data: u, error: ue } = await sb.auth.getUser();
|
|
83
|
+
if (ue || !u?.user) {
|
|
84
|
+
throw new Error(
|
|
85
|
+
"Unable to resolve the current account. Set MOSADD_API_KEY (hub key) or MOSADD_USER_JWT, or run `mosadd login`.",
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
const { data, error } = await sb
|
|
89
|
+
.from("identities")
|
|
90
|
+
.select("id, display_name")
|
|
91
|
+
.eq("user_id", u.user.id)
|
|
92
|
+
.maybeSingle();
|
|
93
|
+
if (error || !data) {
|
|
94
|
+
throw new Error("This account has no mosadd identity row yet — sign in to mosadd.com once to create it.");
|
|
95
|
+
}
|
|
96
|
+
return { id: data.id as string, display_name: (data.display_name as string | null) ?? null };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const comms_session_attach_input = z.object({
|
|
100
|
+
label: z
|
|
101
|
+
.string()
|
|
102
|
+
.max(120)
|
|
103
|
+
.optional()
|
|
104
|
+
.describe(
|
|
105
|
+
"Short name for THIS session, shown to the owner as who is holding the line (e.g. 'CTO — Claude Code', 'dispatcher — Cowork'). Defaults to the machine name.",
|
|
106
|
+
),
|
|
107
|
+
release: z
|
|
108
|
+
.boolean()
|
|
109
|
+
.optional()
|
|
110
|
+
.describe(
|
|
111
|
+
"Hand the line back immediately instead of claiming it: stops this session's keep-alive and clears the liveness row, so the 24/7 cloud stand-in resumes answering at once rather than after the hold expires. Call this when the session is finishing.",
|
|
112
|
+
),
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
interface AttachResult {
|
|
116
|
+
attached: boolean;
|
|
117
|
+
identity_id: string;
|
|
118
|
+
display_name: string | null;
|
|
119
|
+
/** Where the beat says it is coming from — the owner sees this. */
|
|
120
|
+
host?: string;
|
|
121
|
+
/** Seconds a beat stays trusted if this process stops beating. */
|
|
122
|
+
holds_for_seconds?: number;
|
|
123
|
+
note: string;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async function comms_session_attach(
|
|
127
|
+
input: z.infer<typeof comms_session_attach_input>,
|
|
128
|
+
ctx: MosaddToolContext,
|
|
129
|
+
): Promise<AttachResult> {
|
|
130
|
+
// Capture the caller's session ONCE, here, inside the request context — see the multi-tenancy
|
|
131
|
+
// note in the file header. Everything after this (including timer callbacks) uses this value.
|
|
132
|
+
const env = readSupabaseEnv();
|
|
133
|
+
const self = await resolveSelf(env);
|
|
134
|
+
|
|
135
|
+
// Whether attaching or releasing, any previous keep-alive for this identity is stale: a second
|
|
136
|
+
// attach replaces the first rather than doubling the beat rate.
|
|
137
|
+
const existing = attachments.get(self.id);
|
|
138
|
+
if (existing) {
|
|
139
|
+
clearInterval(existing.timer);
|
|
140
|
+
attachments.delete(self.id);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (input.release) {
|
|
144
|
+
ctx.log("info", "comms_session_attach: releasing line", { identity_id: self.id });
|
|
145
|
+
// DELETE, not a backdated timestamp. "No row" is the truthful representation of "nobody is on
|
|
146
|
+
// this line"; writing a fake past `last_seen_at` would put a lie in a table other code reads.
|
|
147
|
+
const { error } = await getSupabase(env).from("agent_bridge_heartbeat").delete().eq("identity_id", self.id);
|
|
148
|
+
if (error) throw new Error(`Could not release the line: ${error.message}`);
|
|
149
|
+
return {
|
|
150
|
+
attached: false,
|
|
151
|
+
identity_id: self.id,
|
|
152
|
+
display_name: self.display_name,
|
|
153
|
+
note: "Line released. The 24/7 cloud stand-in answers this account from now on.",
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const host = input.label?.trim() || `mcp:${hostname()}`;
|
|
158
|
+
const mode = "mcp-session";
|
|
159
|
+
|
|
160
|
+
// First beat is awaited so the caller learns immediately if RLS refuses (wrong account / no
|
|
161
|
+
// identity) instead of failing silently in a timer nobody watches.
|
|
162
|
+
try {
|
|
163
|
+
await beat(env, self.id, host, mode);
|
|
164
|
+
} catch (e) {
|
|
165
|
+
throw new Error(`Could not claim the line: ${e instanceof Error ? e.message : String(e)}`);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const timer = setInterval(() => {
|
|
169
|
+
void beat(env, self.id, host, mode).catch((e) => {
|
|
170
|
+
// A single missed beat is survivable (3 min of cover remain) and must not kill the interval:
|
|
171
|
+
// a transient network blip would otherwise silently un-attach the session for good.
|
|
172
|
+
ctx.log("warn", "comms_session_attach: beat failed", { identity_id: self.id, error: String(e) });
|
|
173
|
+
});
|
|
174
|
+
}, BEAT_MS);
|
|
175
|
+
// Never hold the process open on account of the beat — when the session ends, it ends.
|
|
176
|
+
timer.unref?.();
|
|
177
|
+
attachments.set(self.id, { timer, env });
|
|
178
|
+
|
|
179
|
+
ctx.log("info", "comms_session_attach: line claimed", { identity_id: self.id, host });
|
|
180
|
+
return {
|
|
181
|
+
attached: true,
|
|
182
|
+
identity_id: self.id,
|
|
183
|
+
display_name: self.display_name,
|
|
184
|
+
host,
|
|
185
|
+
holds_for_seconds: HOLD_SECONDS,
|
|
186
|
+
note:
|
|
187
|
+
"This session now owns the account's reply lane; the cloud stand-in will not answer while it is held. " +
|
|
188
|
+
"The hold renews itself for as long as this process runs and lapses on its own if the session dies. " +
|
|
189
|
+
"Call again with release:true to hand it back immediately.",
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export const presenceTools: MosaddTool[] = [
|
|
194
|
+
{
|
|
195
|
+
name: "comms_session_attach",
|
|
196
|
+
requires: "network",
|
|
197
|
+
description:
|
|
198
|
+
"Claim this mosADD account's reply lane for the CURRENT agent session, so the 24/7 cloud stand-in stops answering in your place and the owner talks to you instead. Call it once at the start of a session that will answer the owner's messages; the hold renews itself while the session runs and lapses on its own if the session dies, so the account is never left looking alive when nobody is there. Pass release:true when finishing to hand the line back immediately. Owner-scoped: a session can only claim the account whose key it holds.",
|
|
199
|
+
inputSchema: comms_session_attach_input,
|
|
200
|
+
handler: comms_session_attach as MosaddTool["handler"],
|
|
201
|
+
},
|
|
202
|
+
];
|
|
203
|
+
|
|
204
|
+
/** Test-only: stop every keep-alive so a suite does not leak timers between cases. */
|
|
205
|
+
export function __clearAttachmentsForTest(): void {
|
|
206
|
+
for (const a of attachments.values()) clearInterval(a.timer);
|
|
207
|
+
attachments.clear();
|
|
208
|
+
}
|