@stage-labs/metro 0.1.0-beta.281 → 0.1.0-beta.283

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stage-labs/metro",
3
- "version": "0.1.0-beta.281",
3
+ "version": "0.1.0-beta.283",
4
4
  "description": "The metro command line: run your agent on this machine, relaying its chat channels and connectors to Claude Code.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "metro",
3
- "version": "0.1.0-beta.281",
3
+ "version": "0.1.0-beta.283",
4
4
  "description": "Metro for Claude Code, installed by the metro daemon on this machine: every connector the agent holds as its own MCP server relayed through the daemon, the orchestrator-only guard on the main thread, and the standing rules loaded at each session start. No vendor credential ever sits in a config file.",
5
5
  "author": {
6
6
  "name": "Bonustrack",
@@ -11,6 +11,7 @@ import { replyMeta } from './addressed.js';
11
11
  import {
12
12
  capSet,
13
13
  displayNameMeta,
14
+ missingAttachmentNames,
14
15
  senderMeta,
15
16
  tsMeta,
16
17
  takeMediaCtx,
@@ -56,6 +57,7 @@ interface InboundDeps {
56
57
  }
57
58
 
58
59
  const ATTACH_TIMEOUT_MS = 15_000;
60
+ const LATE_ATTACH_WINDOW_MS = 15 * 60_000;
59
61
  const DEDUPE_TTL_MS = 30_000;
60
62
  const DEDUPE_MAX = 2_000;
61
63
  const ALLOWED_LINES_MAX = 2_000;
@@ -149,27 +151,24 @@ export class InboundRelay {
149
151
  private async flushPendingFallback(id: string): Promise<void> {
150
152
  const e = this.pendingAttachments.get(id);
151
153
  if (!e) return;
152
- this.pendingAttachments.delete(id);
153
- const missing = e.attachments.filter((_, i) => !e.saved.has(i));
154
- if (!missing.length) return;
155
- const names = missing
156
- .map((a) => a.name ?? a.kind ?? 'attachment')
157
- .join(', ');
154
+ const missing = missingAttachmentNames(e);
155
+ if (!missing.length) {
156
+ this.pendingAttachments.delete(id);
157
+ return;
158
+ }
159
+ e.timer = setTimeout(() => { this.pendingAttachments.delete(id); }, LATE_ATTACH_WINDOW_MS);
160
+ const ctx = takeMediaCtx(e);
158
161
  await this.notify('notifications/claude/channel', {
159
162
  content:
160
- (e.text ? `${e.text}\n` : '') +
161
- `[attachment(s) could not be fetched in time: ${names}]`,
163
+ (ctx.text ? `${ctx.text}\n` : '') +
164
+ `[attachment(s) still downloading: ${missing.join(', ')}; they follow in a separate note]`,
162
165
  meta: {
163
- line: e.line,
164
- from: e.from,
165
- station: e.station,
166
- ...tsMeta(e.ts),
167
- message_id: e.messageId,
168
- line_name: e.lineName,
169
- from_name: e.fromName,
170
- ...displayNameMeta(e.fromDisplayName),
171
- ...e.reply,
172
- ...e.callMeta?.(),
166
+ line: ctx.line,
167
+ from: ctx.from,
168
+ station: ctx.station,
169
+ ...senderMeta(ctx),
170
+ ...ctx.reply,
171
+ ...ctx.callMeta?.(),
173
172
  },
174
173
  });
175
174
  }
@@ -62,6 +62,12 @@ export const senderMeta = (c: MediaCtx): Record<string, string> => ({
62
62
  ...displayNameMeta(c.fromDisplayName),
63
63
  });
64
64
 
65
+ export function missingAttachmentNames(buf: PendingMsg): string[] {
66
+ return buf.attachments
67
+ .filter((_, i) => !buf.saved.has(i))
68
+ .map((a) => a.name ?? a.kind ?? 'attachment');
69
+ }
70
+
65
71
  export function takeMediaCtx(buf: PendingMsg): MediaCtx {
66
72
  const ctx: MediaCtx = {
67
73
  line: buf.line,
@@ -12,8 +12,22 @@ import { errMsg } from '@metro-labs/core/log';
12
12
 
13
13
  export type { SavedAttachment };
14
14
 
15
- export const REMOTE_FETCH_ATTEMPTS = 3;
16
- const REMOTE_FETCH_BACKOFF_MS = 400;
15
+ interface FetchSchedule {
16
+ delaysMs: readonly number[];
17
+ requestTimeoutMs: number;
18
+ }
19
+
20
+ const READ_SCHEDULE: FetchSchedule = { delaysMs: [400, 800], requestTimeoutMs: 9_000 };
21
+ export const INBOUND_FETCH_SCHEDULE: FetchSchedule = {
22
+ delaysMs: [
23
+ 2_000, 4_000, 8_000, 15_000, 30_000, 30_000,
24
+ 60_000, 60_000, 60_000, 60_000, 60_000, 60_000,
25
+ ],
26
+ requestTimeoutMs: 120_000,
27
+ };
28
+ export const REMOTE_FETCH_ATTEMPTS = READ_SCHEDULE.delaysMs.length + 1;
29
+ const SWARM_FALLBACK_GATEWAY = 'https://download.gateway.ethswarm.org/bzz/';
30
+ const SWARM_BZZ_URL = /^https:\/\/[^/]+\/bzz\/([0-9a-f]{64}(?:[0-9a-f]{64})?)\/?$/i;
17
31
 
18
32
  const sleep = (ms: number): Promise<void> =>
19
33
  new Promise((resolve) => setTimeout(resolve, ms));
@@ -74,22 +88,56 @@ interface DecodedAttachment {
74
88
  data: Uint8Array;
75
89
  }
76
90
 
77
- async function loadRemote(r: RemoteEntry): Promise<DecodedAttachment> {
91
+ function remoteUrls(url: string): string[] {
92
+ const ref = SWARM_BZZ_URL.exec(url)?.[1];
93
+ if (ref === undefined) return [url];
94
+ const fallback = `${SWARM_FALLBACK_GATEWAY}${ref.toLowerCase()}/`;
95
+ return url.toLowerCase().replace(/\/?$/, '/') === fallback ? [url] : [url, fallback];
96
+ }
97
+
98
+ function withTimeout<T>(work: Promise<T>, ms: number, url: string): Promise<T> {
99
+ let timer: ReturnType<typeof setTimeout> | undefined;
100
+ const expired = new Promise<never>((_resolve, reject) => {
101
+ timer = setTimeout(() => {
102
+ reject(new Error(`no answer from ${url} within ${ms / 1000}s`));
103
+ }, ms);
104
+ });
105
+ return Promise.race([work, expired]).finally(() => {
106
+ clearTimeout(timer);
107
+ });
108
+ }
109
+
110
+ function loadFrom(r: RemoteEntry, url: string, timeoutMs: number): Promise<DecodedAttachment> {
111
+ return withTimeout(
112
+ RemoteAttachmentCodec.load<DecodedAttachment>(
113
+ { ...toRemoteDescriptor(r), url },
114
+ loadRegistry,
115
+ ),
116
+ timeoutMs,
117
+ url,
118
+ );
119
+ }
120
+
121
+ async function loadRemote(
122
+ r: RemoteEntry,
123
+ schedule: FetchSchedule,
124
+ ): Promise<DecodedAttachment> {
125
+ const urls = remoteUrls(r.url);
78
126
  let last = '';
79
- for (let attempt = 1; attempt <= REMOTE_FETCH_ATTEMPTS; attempt += 1) {
80
- try {
81
- return await RemoteAttachmentCodec.load<DecodedAttachment>(
82
- toRemoteDescriptor(r),
83
- loadRegistry,
84
- );
85
- } catch (err) {
86
- last = errMsg(err);
87
- if (attempt < REMOTE_FETCH_ATTEMPTS)
88
- await sleep(REMOTE_FETCH_BACKOFF_MS * attempt);
127
+ for (const delay of [0, ...schedule.delaysMs]) {
128
+ if (delay > 0) await sleep(delay);
129
+ const errors: string[] = [];
130
+ for (const url of urls) {
131
+ try {
132
+ return await loadFrom(r, url, schedule.requestTimeoutMs);
133
+ } catch (err) {
134
+ errors.push(errMsg(err));
135
+ }
89
136
  }
137
+ last = errors.join('; ');
90
138
  }
91
139
  throw new Error(
92
- `xmtp remote attachment fetch failed after ${REMOTE_FETCH_ATTEMPTS} attempts: ${last}`,
140
+ `xmtp remote attachment fetch failed after ${schedule.delaysMs.length + 1} attempts: ${last}`,
93
141
  );
94
142
  }
95
143
 
@@ -97,9 +145,10 @@ export async function saveRemoteAttachment(
97
145
  r: RemoteEntry,
98
146
  messageId: string,
99
147
  index = 0,
148
+ schedule: FetchSchedule = READ_SCHEDULE,
100
149
  ): Promise<SavedAttachment> {
101
150
  if (r.contentLength) assertAttachmentSize(r.contentLength);
102
- const decoded = await loadRemote(r);
151
+ const decoded = await loadRemote(r, schedule);
103
152
  return saveBufferToCache(decoded.data, messageId, index, {
104
153
  mime: decoded.mimeType,
105
154
  name: decoded.filename ?? r.filename,
@@ -3,6 +3,7 @@ import type { Reply } from '@xmtp/content-type-reply';
3
3
  import { errMsg } from '@metro-labs/core/log';
4
4
  import { transcribeAndEmit } from './transcribe.js';
5
5
  import {
6
+ INBOUND_FETCH_SCHEDULE,
6
7
  saveInlineAttachment,
7
8
  saveRemoteAttachment,
8
9
  type RemoteEntry,
@@ -207,7 +208,7 @@ function remoteStaticPayload(
207
208
  ctx: EnvelopeCtx,
208
209
  ): Record<string, unknown> {
209
210
  const kind = IMG_RE.test(c.url) ? 'image' : 'file';
210
- reportAttachment(saveRemoteAttachment(c, ctx.msgId, 0), { station: 'xmtp', account: ctx.accountId, line: ctx.line, forId: ctx.baseId, index: 0 });
211
+ reportAttachment(saveRemoteAttachment(c, ctx.msgId, 0, INBOUND_FETCH_SCHEDULE), { station: 'xmtp', account: ctx.accountId, line: ctx.line, forId: ctx.baseId, index: 0 });
211
212
  return {
212
213
  ...base,
213
214
  text: `[${kind}: ${c.filename ?? c.url}]`,
@@ -223,7 +224,7 @@ function multiRemoteEnvelope(
223
224
  ): Record<string, unknown> {
224
225
  const m = c as { attachments?: RemoteEntry[] };
225
226
  (Array.isArray(m.attachments) ? m.attachments : []).forEach((r, i) => {
226
- reportAttachment(saveRemoteAttachment(r, ctx.msgId, i), { station: 'xmtp', account: ctx.accountId, line: ctx.line, forId: ctx.baseId, index: i });
227
+ reportAttachment(saveRemoteAttachment(r, ctx.msgId, i, INBOUND_FETCH_SCHEDULE), { station: 'xmtp', account: ctx.accountId, line: ctx.line, forId: ctx.baseId, index: i });
227
228
  });
228
229
  return multiRemotePayload(base, typeId, c);
229
230
  }
@@ -1,3 +1,3 @@
1
1
  {
2
- "version": "0.1.0-beta.281"
2
+ "version": "0.1.0-beta.283"
3
3
  }
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.1.0-beta.281",
2
+ "version": "0.1.0-beta.283",
3
3
  "dependencies": {
4
4
  "@anthropic-ai/claude-agent-sdk": "0.3.287",
5
5
  "@modelcontextprotocol/sdk": "^1.29.0",