@tangle-network/agent-app 0.60.86 → 0.60.88

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.
@@ -24,6 +24,10 @@ export interface UnansweredThread {
24
24
  pendingMessages: number;
25
25
  /** Age of the OLDEST unanswered user message, in ms. */
26
26
  oldestAgeMs: number;
27
+ /** The newest of these messages was followed by a row that showed the person
28
+ * something (a failure notice or error prose), so they know it failed. False
29
+ * or absent: nothing came back at all. */
30
+ notified?: boolean;
27
31
  }
28
32
  /** A persisted assistant row, as the sweep needs to judge it. */
29
33
  export interface PersistedTurnRow {
@@ -138,15 +142,37 @@ export interface SweepOptions {
138
142
  * blindness this module now refuses to draw conclusions from.
139
143
  */
140
144
  decodeParts?: (raw: unknown, row: PersistedTurnRow) => Promise<unknown> | unknown;
145
+ /**
146
+ * Which threads that got no reply at all should page (a `critical` alert);
147
+ * the rest are reported as a `warning`. A product passes its own rule, such
148
+ * as only its customers' and operators' threads, so internal and evaluation
149
+ * traffic stops paging. Default: threads silent for 24 hours or more.
150
+ */
151
+ pageSilentThreads?: (threads: UnansweredThread[]) => Promise<UnansweredThread[]> | UnansweredThread[];
152
+ /**
153
+ * Where a product already reports threads that ended on a failure the person
154
+ * saw, such as its reliability report. When set, those threads are counted
155
+ * but not alerted here. Unset, they are a `warning`, so a product whose every
156
+ * reply is an error message still shows up.
157
+ */
158
+ failureNoticesReportedBy?: string;
141
159
  now?: number;
142
160
  }
143
161
  /** What the sweep found. Returned as well as alerted, so a cron can log it and
144
162
  * a test can assert on it. */
145
163
  export interface SweepResult {
146
164
  product: string;
165
+ /** Threads whose user messages have no real answer: {@link silentThreads}
166
+ * plus {@link notifiedThreads}. */
147
167
  unansweredThreads: number;
148
168
  pendingUserMessages: number;
149
169
  oldestUnansweredMs: number;
170
+ /** Threads where nothing came back after the newest message. */
171
+ silentThreads: number;
172
+ /** Of {@link silentThreads}, the ones `pageSilentThreads` selected. */
173
+ pagedSilentThreads: number;
174
+ /** Threads that ended on a failure the person saw. */
175
+ notifiedThreads: number;
150
176
  turnsJudged: number;
151
177
  unhealthyTurns: number;
152
178
  emptyCompletions: number;
@@ -48,6 +48,12 @@ export declare function createExportEngine(options: ExportEngineOptions): {
48
48
  filename: string;
49
49
  };
50
50
  /** Delete exports older than `retentionMs`, and any export by id when given. */
51
+ /**
52
+ * Advance every running export whose lease has lapsed, so an export keeps
53
+ * going after the owner closes the tab. Call it from the product's scheduled
54
+ * handler (a one-minute cron). Returns how many exports it advanced.
55
+ */
56
+ continueRunning(budgetMs?: number): Promise<number>;
51
57
  prune(workspaceId: string, retentionMs: number, onlyId?: string): Promise<number>;
52
58
  };
53
59
  export type ExportEngine = ReturnType<typeof createExportEngine>;
@@ -738,6 +738,7 @@ function createExportEngine(options) {
738
738
  const base = (workspaceId, id) => `${prefix}/${assertSafeKeySegment(workspaceId)}/${assertSafeKeySegment(id)}`;
739
739
  const jobKey = (workspaceId, id) => `${base(workspaceId, id)}/job.json`;
740
740
  const archiveKey = (workspaceId, id) => `${base(workspaceId, id)}/archive.zip`;
741
+ const runningKey = (workspaceId, id) => `${prefix}/_running/${assertSafeKeySegment(workspaceId)}~${assertSafeKeySegment(id)}`;
741
742
  async function loadJob(workspaceId, id) {
742
743
  if (!/^[a-z0-9-]{8,64}$/.test(id)) return null;
743
744
  const object = await storage.get(jobKey(workspaceId, id));
@@ -1035,6 +1036,7 @@ function createExportEngine(options) {
1035
1036
  }))
1036
1037
  };
1037
1038
  await saveJob(job);
1039
+ await storage.put(runningKey(workspaceId, job.id), new Uint8Array(0));
1038
1040
  return job;
1039
1041
  },
1040
1042
  /**
@@ -1128,6 +1130,7 @@ function createExportEngine(options) {
1128
1130
  });
1129
1131
  job.lease = void 0;
1130
1132
  await saveJob(job, etag);
1133
+ if (job.status !== "running") await storage.delete([runningKey(workspaceId, id)]);
1131
1134
  return job;
1132
1135
  } catch (err) {
1133
1136
  if (err instanceof LeaseLost) return (await loadJob(workspaceId, id))?.job ?? null;
@@ -1219,6 +1222,39 @@ function createExportEngine(options) {
1219
1222
  return { length, stream, filename: `${options.app}-workspace-export-${date}.zip` };
1220
1223
  },
1221
1224
  /** Delete exports older than `retentionMs`, and any export by id when given. */
1225
+ /**
1226
+ * Advance every running export whose lease has lapsed, so an export keeps
1227
+ * going after the owner closes the tab. Call it from the product's scheduled
1228
+ * handler (a one-minute cron). Returns how many exports it advanced.
1229
+ */
1230
+ async continueRunning(budgetMs = 6e4) {
1231
+ const deadline = now() + budgetMs;
1232
+ const keys = [];
1233
+ let cursor;
1234
+ do {
1235
+ const page = await storage.list(`${prefix}/_running/`, cursor);
1236
+ keys.push(...page.keys);
1237
+ cursor = page.cursor;
1238
+ } while (cursor);
1239
+ let advanced = 0;
1240
+ for (const key of keys) {
1241
+ const remaining = deadline - now();
1242
+ if (remaining <= 0) break;
1243
+ const name = key.split("/").at(-1) ?? "";
1244
+ const workspaceId = name.slice(0, name.lastIndexOf("~"));
1245
+ const id = name.slice(name.lastIndexOf("~") + 1);
1246
+ if (!workspaceId || !id) continue;
1247
+ const loaded = await loadJob(workspaceId, id);
1248
+ if (!loaded || loaded.job.status !== "running") {
1249
+ await storage.delete([key]);
1250
+ continue;
1251
+ }
1252
+ if (loaded.job.lease && loaded.job.lease.until > now()) continue;
1253
+ await this.advance(workspaceId, id, Math.min(remaining, 2e4));
1254
+ advanced += 1;
1255
+ }
1256
+ return advanced;
1257
+ },
1222
1258
  async prune(workspaceId, retentionMs, onlyId) {
1223
1259
  const cutoff = now() - retentionMs;
1224
1260
  const doomed = [];
@@ -1353,7 +1389,8 @@ function createWorkspaceExport(options) {
1353
1389
  return refuse(500, "export_error", "The export request failed");
1354
1390
  }
1355
1391
  }
1356
- return { route, engine };
1392
+ const continueRunning = (budgetMs) => engine.continueRunning(budgetMs);
1393
+ return { route, engine, continueRunning };
1357
1394
  }
1358
1395
 
1359
1396
  // src/workspace-export/storage.ts