glove-foundry 0.9.0 → 0.10.1
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/README.md +1 -1
- package/dist/{chunk-LI7VIGVH.js → chunk-L72FKI7P.js} +329 -143
- package/dist/{chunk-L5JQVTHD.js → chunk-N2GVJ257.js} +12 -0
- package/dist/{chunk-EUGKVW3J.js → chunk-VLD3DKPI.js} +166 -12
- package/dist/cli.js +2 -2
- package/dist/{client-CCY4_vqN.d.ts → client-HM2x2-Ut.d.ts} +41 -5
- package/dist/client.d.ts +2 -2
- package/dist/client.js +1 -1
- package/dist/{event-delivery-CNCsVqi8.d.ts → event-delivery-C7rYiNPs.d.ts} +148 -50
- package/dist/execution-daemon.js +0 -7
- package/dist/execution-loader.js +4 -2
- package/dist/index.d.ts +9 -5
- package/dist/index.js +44 -3
- package/dist/voice.d.ts +1 -1
- package/docs/architecture.md +18 -0
- package/docs/building-with-foundry.md +1 -1
- package/package.json +6 -6
|
@@ -14,24 +14,33 @@ import {
|
|
|
14
14
|
FOUNDRY_CORE_COMMAND_EVENT,
|
|
15
15
|
FOUNDRY_MODEL_REQUESTS_ENV,
|
|
16
16
|
MemoryFoundryDataAdapter,
|
|
17
|
+
TERMINAL_OCCURRENCE_STATUSES,
|
|
18
|
+
activationScheduleRevision,
|
|
19
|
+
compareOccurrences,
|
|
17
20
|
createAgentInstance,
|
|
18
21
|
createConversation,
|
|
19
22
|
createManifest,
|
|
20
23
|
definePlaybookSubscription,
|
|
21
24
|
discoverAgents,
|
|
25
|
+
firstScheduledTime,
|
|
22
26
|
foundryApprovalRequested,
|
|
23
27
|
foundryApprovalSuspended,
|
|
24
28
|
freezeGloveMessage,
|
|
25
29
|
id,
|
|
26
30
|
listFoundryApprovals,
|
|
27
31
|
markFoundryApprovalSuspended,
|
|
32
|
+
matchesOccurrenceFilter,
|
|
28
33
|
normalizeAgentInstallations,
|
|
34
|
+
planScheduleOccurrence,
|
|
35
|
+
prunableOccurrences,
|
|
29
36
|
readFoundryApprovalResolution,
|
|
30
37
|
reconstructPlaybook,
|
|
31
38
|
reconstructPlaybookSubscription,
|
|
32
39
|
resolveFoundryApproval,
|
|
40
|
+
scheduleOccurrenceId,
|
|
41
|
+
scheduleRevisionMatches,
|
|
33
42
|
transmissionPredicate
|
|
34
|
-
} from "./chunk-
|
|
43
|
+
} from "./chunk-VLD3DKPI.js";
|
|
35
44
|
import {
|
|
36
45
|
FOUNDRY_CONVERSATION_LEASE_EVENT,
|
|
37
46
|
FOUNDRY_CONVERSATION_LISTEN_EVENT,
|
|
@@ -1090,11 +1099,6 @@ ${error.stderrTail}
|
|
|
1090
1099
|
getAdapter() {
|
|
1091
1100
|
return { ping: () => this.ping() };
|
|
1092
1101
|
}
|
|
1093
|
-
schedules = {
|
|
1094
|
-
add: (schedule) => this.request({ method: "schedule.add", schedule }),
|
|
1095
|
-
delete: (id2) => this.request({ method: "schedule.delete", id: id2 }),
|
|
1096
|
-
ping: () => this.ping()
|
|
1097
|
-
};
|
|
1098
1102
|
async waitForRun(id2, options = {}) {
|
|
1099
1103
|
const deadline = Date.now() + (options.timeoutMs ?? 6e4);
|
|
1100
1104
|
do {
|
|
@@ -1106,11 +1110,6 @@ ${error.stderrTail}
|
|
|
1106
1110
|
}
|
|
1107
1111
|
};
|
|
1108
1112
|
|
|
1109
|
-
// src/runtime.ts
|
|
1110
|
-
import {
|
|
1111
|
-
nextCronOccurrence
|
|
1112
|
-
} from "station-schedules";
|
|
1113
|
-
|
|
1114
1113
|
// src/conversation-gate.ts
|
|
1115
1114
|
var ConversationGate = class {
|
|
1116
1115
|
constructor(hooks) {
|
|
@@ -1732,7 +1731,6 @@ var FoundryRuntime = class _FoundryRuntime {
|
|
|
1732
1731
|
execution;
|
|
1733
1732
|
observer;
|
|
1734
1733
|
signalRunner;
|
|
1735
|
-
scheduleAdapter;
|
|
1736
1734
|
services;
|
|
1737
1735
|
runnerLoops = [];
|
|
1738
1736
|
materializedActivations = /* @__PURE__ */ new Set();
|
|
@@ -1740,6 +1738,14 @@ var FoundryRuntime = class _FoundryRuntime {
|
|
|
1740
1738
|
activationTransitions = Effect5.unsafeMakeSemaphore(1);
|
|
1741
1739
|
activationDispatches = /* @__PURE__ */ new Map();
|
|
1742
1740
|
activationTimers = /* @__PURE__ */ new Map();
|
|
1741
|
+
/** Occurrences when the data adapter does not store them. */
|
|
1742
|
+
memoryOccurrences = /* @__PURE__ */ new Map();
|
|
1743
|
+
/** Occurrences this process handed (or is handing) to a run. */
|
|
1744
|
+
occurrencesInHand = /* @__PURE__ */ new Set();
|
|
1745
|
+
/** Run id to the occurrence it delivers. */
|
|
1746
|
+
occurrenceRuns = /* @__PURE__ */ new Map();
|
|
1747
|
+
/** How recent runs settled, for an occurrence recorded after its run already finished. */
|
|
1748
|
+
recentlySettledRuns = /* @__PURE__ */ new Map();
|
|
1743
1749
|
/** Whether each run's child process still holds its conversation, from FOUNDRY_RUN_PROCESS_EVENT. */
|
|
1744
1750
|
runProcesses = /* @__PURE__ */ new Map();
|
|
1745
1751
|
approvalDirectory = join2(
|
|
@@ -1969,7 +1975,6 @@ var FoundryRuntime = class _FoundryRuntime {
|
|
|
1969
1975
|
...options.config?.observability?.modelRequests === false ? { [FOUNDRY_MODEL_REQUESTS_ENV]: "off" } : {}
|
|
1970
1976
|
}
|
|
1971
1977
|
});
|
|
1972
|
-
this.scheduleAdapter = this.signalRunner.schedules;
|
|
1973
1978
|
const topologyLayer = memoryTopologyStore;
|
|
1974
1979
|
const defaultServices = Layer3.mergeAll(
|
|
1975
1980
|
memoryAccountDirectory(this.application.accounts ?? []),
|
|
@@ -2292,7 +2297,7 @@ var FoundryRuntime = class _FoundryRuntime {
|
|
|
2292
2297
|
await this.withActivationTransition(async () => {
|
|
2293
2298
|
for (const activation of await Effect5.runPromise(this.data.listActivations(current.workspaceId))) {
|
|
2294
2299
|
if (activation.agentId !== current.id || ["cancelled", "completed"].includes(activation.status)) continue;
|
|
2295
|
-
await this.disarmActivation(activation);
|
|
2300
|
+
await this.disarmActivation(activation, "paused");
|
|
2296
2301
|
await Effect5.runPromise(this.data.putActivation({ ...activation, status: "paused", updatedAt: (/* @__PURE__ */ new Date()).toISOString() }));
|
|
2297
2302
|
}
|
|
2298
2303
|
});
|
|
@@ -3388,7 +3393,7 @@ var FoundryRuntime = class _FoundryRuntime {
|
|
|
3388
3393
|
const [execution, environment, activations] = await Promise.all([
|
|
3389
3394
|
this.signalRunner.getAdapter().ping(),
|
|
3390
3395
|
this.signalRunner.ping(),
|
|
3391
|
-
this.
|
|
3396
|
+
this.signalRunner.ping()
|
|
3392
3397
|
]);
|
|
3393
3398
|
return {
|
|
3394
3399
|
ok: execution && environment && activations,
|
|
@@ -4834,6 +4839,17 @@ var FoundryRuntime = class _FoundryRuntime {
|
|
|
4834
4839
|
}
|
|
4835
4840
|
async handleRunSettled(event) {
|
|
4836
4841
|
void this.settleApprovalsOf(event.run.id).catch((cause) => this.reportApprovalFailure(event.run.id, cause));
|
|
4842
|
+
this.recentlySettledRuns.set(event.run.id, event.status);
|
|
4843
|
+
if (this.recentlySettledRuns.size > 1e3) this.recentlySettledRuns.delete(this.recentlySettledRuns.keys().next().value);
|
|
4844
|
+
void this.settleOccurrence(event.run.id, event.status).catch((cause) => {
|
|
4845
|
+
this.observability.append({
|
|
4846
|
+
type: "activation.persistence.error",
|
|
4847
|
+
category: "activation",
|
|
4848
|
+
agent: event.route,
|
|
4849
|
+
runId: event.run.id,
|
|
4850
|
+
data: { error: cause instanceof Error ? cause.message : String(cause) }
|
|
4851
|
+
});
|
|
4852
|
+
});
|
|
4837
4853
|
this.releaseConversationRun(event.run.id);
|
|
4838
4854
|
void this.waitForRunProcessRelease(this.toFoundryRun(event.run), event.route).finally(() => this.gate.settled(event.run.id));
|
|
4839
4855
|
const finishDelivery = async () => {
|
|
@@ -5191,21 +5207,20 @@ var FoundryRuntime = class _FoundryRuntime {
|
|
|
5191
5207
|
throw new Error(`Scheduled activation "${activation.id}" is not owned by this agent instance.`);
|
|
5192
5208
|
}
|
|
5193
5209
|
}
|
|
5194
|
-
|
|
5195
|
-
return `activation/${activationId.replaceAll("_", "-")}`;
|
|
5196
|
-
}
|
|
5197
|
-
async disarmActivation(activation) {
|
|
5210
|
+
async disarmActivation(activation, reason) {
|
|
5198
5211
|
this.activationDispatches.delete(activation.id);
|
|
5199
5212
|
const timer = this.activationTimers.get(activation.id);
|
|
5200
5213
|
if (timer) {
|
|
5201
5214
|
clearTimeout(timer);
|
|
5202
5215
|
this.activationTimers.delete(activation.id);
|
|
5203
5216
|
}
|
|
5204
|
-
if (activation.timing.kind === "
|
|
5205
|
-
await this.scheduleAdapter.delete(this.backendScheduleId(activation.id));
|
|
5206
|
-
} else if (activation.lastRunId) {
|
|
5217
|
+
if (activation.timing.kind === "at" && activation.lastRunId) {
|
|
5207
5218
|
await this.signalRunner.cancel(activation.lastRunId);
|
|
5208
5219
|
}
|
|
5220
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
5221
|
+
for (const occurrence of await this.listScheduleOccurrences({ activationId: activation.id, status: "pending" })) {
|
|
5222
|
+
await this.putOccurrence({ ...occurrence, status: "cancelled", reason, updatedAt: now, settledAt: now });
|
|
5223
|
+
}
|
|
5209
5224
|
this.materializedActivations.delete(activation.id);
|
|
5210
5225
|
}
|
|
5211
5226
|
async syncDefinitionSchedules(command, parentRunId) {
|
|
@@ -5214,7 +5229,7 @@ var FoundryRuntime = class _FoundryRuntime {
|
|
|
5214
5229
|
);
|
|
5215
5230
|
const desiredIds = new Set(command.schedules.map((item) => item.id));
|
|
5216
5231
|
for (const stale of current.filter((item) => !desiredIds.has(item.id))) {
|
|
5217
|
-
await this.disarmActivation(stale);
|
|
5232
|
+
await this.disarmActivation(stale, "cancelled");
|
|
5218
5233
|
await Effect5.runPromise(this.data.putActivation({
|
|
5219
5234
|
...stale,
|
|
5220
5235
|
status: "cancelled",
|
|
@@ -5226,7 +5241,7 @@ var FoundryRuntime = class _FoundryRuntime {
|
|
|
5226
5241
|
if (existing) this.assertActivationOwnership(existing, command);
|
|
5227
5242
|
if (!desired.enabled) {
|
|
5228
5243
|
if (existing && existing.status !== "cancelled") {
|
|
5229
|
-
await this.disarmActivation(existing);
|
|
5244
|
+
await this.disarmActivation(existing, "cancelled");
|
|
5230
5245
|
await Effect5.runPromise(this.data.putActivation({
|
|
5231
5246
|
...existing,
|
|
5232
5247
|
status: "cancelled",
|
|
@@ -5237,7 +5252,7 @@ var FoundryRuntime = class _FoundryRuntime {
|
|
|
5237
5252
|
}
|
|
5238
5253
|
const unchanged = existing?.definitionRevision === desired.revision;
|
|
5239
5254
|
if (unchanged || existing?.status === "cancelled") continue;
|
|
5240
|
-
if (existing) await this.disarmActivation(existing);
|
|
5255
|
+
if (existing) await this.disarmActivation(existing, "superseded");
|
|
5241
5256
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
5242
5257
|
const activation = {
|
|
5243
5258
|
id: desired.id,
|
|
@@ -5265,9 +5280,10 @@ var FoundryRuntime = class _FoundryRuntime {
|
|
|
5265
5280
|
const existing = await Effect5.runPromise(this.data.getActivation(command.activationId));
|
|
5266
5281
|
if (!existing) throw new Error(`Scheduled activation "${command.activationId}" was not found.`);
|
|
5267
5282
|
this.assertActivationOwnership(existing, command);
|
|
5268
|
-
await this.disarmActivation(existing);
|
|
5283
|
+
await this.disarmActivation(existing, "superseded");
|
|
5284
|
+
const { nextOccurrenceAt: _cursor, scheduleRevision: _revision, ...unarmed } = existing;
|
|
5269
5285
|
const updated = {
|
|
5270
|
-
...
|
|
5286
|
+
...unarmed,
|
|
5271
5287
|
...command.patch,
|
|
5272
5288
|
status: existing.status === "paused" ? "paused" : "pending",
|
|
5273
5289
|
createdByRunId: parentRunId,
|
|
@@ -5292,7 +5308,7 @@ var FoundryRuntime = class _FoundryRuntime {
|
|
|
5292
5308
|
throw new Error(`Scheduled activation "${command.activationId}" cannot be paused from status "${existing.status}".`);
|
|
5293
5309
|
}
|
|
5294
5310
|
if (existing.status !== "paused") {
|
|
5295
|
-
await this.disarmActivation(existing);
|
|
5311
|
+
await this.disarmActivation(existing, "paused");
|
|
5296
5312
|
await Effect5.runPromise(this.data.putActivation({
|
|
5297
5313
|
...existing,
|
|
5298
5314
|
status: "paused",
|
|
@@ -5314,8 +5330,9 @@ var FoundryRuntime = class _FoundryRuntime {
|
|
|
5314
5330
|
if (existing.status !== "paused") {
|
|
5315
5331
|
throw new Error(`Scheduled activation "${command.activationId}" is not paused.`);
|
|
5316
5332
|
}
|
|
5333
|
+
const { nextOccurrenceAt: _paused, scheduleRevision: _revision, ...unpaused } = existing;
|
|
5317
5334
|
const resumed = {
|
|
5318
|
-
...
|
|
5335
|
+
...unpaused,
|
|
5319
5336
|
status: "pending",
|
|
5320
5337
|
createdByRunId: parentRunId,
|
|
5321
5338
|
lastRunId: void 0,
|
|
@@ -5335,7 +5352,7 @@ var FoundryRuntime = class _FoundryRuntime {
|
|
|
5335
5352
|
const existing = await Effect5.runPromise(this.data.getActivation(command.activationId));
|
|
5336
5353
|
if (!existing) throw new Error(`Scheduled activation "${command.activationId}" was not found.`);
|
|
5337
5354
|
this.assertActivationOwnership(existing, command);
|
|
5338
|
-
await this.disarmActivation(existing);
|
|
5355
|
+
await this.disarmActivation(existing, "cancelled");
|
|
5339
5356
|
await Effect5.runPromise(this.data.putActivation({
|
|
5340
5357
|
...existing,
|
|
5341
5358
|
status: "cancelled",
|
|
@@ -5351,12 +5368,37 @@ var FoundryRuntime = class _FoundryRuntime {
|
|
|
5351
5368
|
}
|
|
5352
5369
|
reconstructActivations() {
|
|
5353
5370
|
return this.withActivationTransition(async () => {
|
|
5371
|
+
await this.replayScheduleOccurrences();
|
|
5354
5372
|
for (const activation of await Effect5.runPromise(this.data.listActivations())) {
|
|
5355
5373
|
if (activation.status === "paused" || activation.status === "completed" || activation.status === "cancelled") continue;
|
|
5356
5374
|
await this.materializeActivation(activation);
|
|
5357
5375
|
}
|
|
5358
5376
|
});
|
|
5359
5377
|
}
|
|
5378
|
+
/** List occurrences of schedules and sleeps: what came due, when it was meant for, and what became of it. */
|
|
5379
|
+
async listScheduleOccurrences(filter = {}) {
|
|
5380
|
+
if (this.durableOccurrences()) return Effect5.runPromise(this.data.listScheduleOccurrences(filter));
|
|
5381
|
+
return [...this.memoryOccurrences.values()].filter((item) => matchesOccurrenceFilter(item, filter)).sort(compareOccurrences).map((item) => structuredClone(item));
|
|
5382
|
+
}
|
|
5383
|
+
durableOccurrences() {
|
|
5384
|
+
return Boolean(this.data.claimScheduleOccurrence && this.data.putScheduleOccurrence && this.data.getScheduleOccurrence && this.data.listScheduleOccurrences);
|
|
5385
|
+
}
|
|
5386
|
+
async claimOccurrence(occurrence) {
|
|
5387
|
+
if (this.durableOccurrences()) return Effect5.runPromise(this.data.claimScheduleOccurrence(occurrence));
|
|
5388
|
+
if (this.memoryOccurrences.has(occurrence.id)) return false;
|
|
5389
|
+
this.memoryOccurrences.set(occurrence.id, structuredClone(occurrence));
|
|
5390
|
+
return true;
|
|
5391
|
+
}
|
|
5392
|
+
async putOccurrence(occurrence) {
|
|
5393
|
+
if (this.durableOccurrences()) return Effect5.runPromise(this.data.putScheduleOccurrence(occurrence));
|
|
5394
|
+
this.memoryOccurrences.set(occurrence.id, structuredClone(occurrence));
|
|
5395
|
+
for (const id2 of prunableOccurrences(this.memoryOccurrences.values(), occurrence.activationId)) this.memoryOccurrences.delete(id2);
|
|
5396
|
+
}
|
|
5397
|
+
async getOccurrence(id2) {
|
|
5398
|
+
if (this.durableOccurrences()) return Effect5.runPromise(this.data.getScheduleOccurrence(id2));
|
|
5399
|
+
const occurrence = this.memoryOccurrences.get(id2);
|
|
5400
|
+
return occurrence ? structuredClone(occurrence) : null;
|
|
5401
|
+
}
|
|
5360
5402
|
async materializeActivation(activation) {
|
|
5361
5403
|
if (this.materializedActivations.has(activation.id)) return;
|
|
5362
5404
|
this.materializedActivations.add(activation.id);
|
|
@@ -5384,141 +5426,271 @@ var FoundryRuntime = class _FoundryRuntime {
|
|
|
5384
5426
|
if (activation.timing.kind === "at" && Number.isNaN(new Date(activation.timing.at).getTime())) {
|
|
5385
5427
|
throw new Error(`Activation "${activation.id}" has an invalid wake time.`);
|
|
5386
5428
|
}
|
|
5387
|
-
const
|
|
5388
|
-
|
|
5389
|
-
|
|
5390
|
-
|
|
5391
|
-
|
|
5392
|
-
|
|
5393
|
-
|
|
5429
|
+
const now = /* @__PURE__ */ new Date();
|
|
5430
|
+
const stored = activation.scheduleRevision;
|
|
5431
|
+
const resumes = stored !== void 0 && scheduleRevisionMatches(activation, stored);
|
|
5432
|
+
const revision = resumes ? stored : activationScheduleRevision(activation);
|
|
5433
|
+
const cursor = resumes ? activation.nextOccurrenceAt : firstScheduledTime(activation.timing, now).toISOString();
|
|
5434
|
+
const { nextOccurrenceAt: _previous, ...rest } = activation;
|
|
5435
|
+
const active = {
|
|
5436
|
+
...rest,
|
|
5437
|
+
status: "active",
|
|
5438
|
+
...activation.timing.kind === "at" && !resumes ? { lastRunId: void 0 } : {},
|
|
5439
|
+
scheduleRevision: revision,
|
|
5440
|
+
...cursor ? { nextOccurrenceAt: cursor } : {},
|
|
5441
|
+
updatedAt: now.toISOString()
|
|
5394
5442
|
};
|
|
5395
|
-
if (
|
|
5396
|
-
|
|
5397
|
-
const schedule = {
|
|
5398
|
-
id: this.backendScheduleId(activation.id),
|
|
5399
|
-
kind: "signal",
|
|
5400
|
-
target: discovered.executionName,
|
|
5401
|
-
...activation.timing.kind === "every" ? { interval: `${activation.timing.intervalMs}ms` } : { cron: activation.timing.expression, timezone: activation.timing.timezone },
|
|
5402
|
-
// Station checks overlap per signal, and a signal is a whole
|
|
5403
|
-
// definition: "skip" would drop one agent's run because another
|
|
5404
|
-
// agent of the same definition is busy. Runs overlap like messages
|
|
5405
|
-
// do, and the conversation gate still runs one at a time per conversation.
|
|
5406
|
-
overlapPolicy: "allow",
|
|
5407
|
-
misfirePolicy: "fire-once",
|
|
5408
|
-
misfireGraceMs: 6e4,
|
|
5409
|
-
input: await this.executionEnvelope(request),
|
|
5410
|
-
enabled: true,
|
|
5411
|
-
nextRunAt: activation.timing.kind === "every" ? new Date(now.getTime() + activation.timing.intervalMs) : nextCronOccurrence(
|
|
5412
|
-
activation.timing.expression,
|
|
5413
|
-
activation.timing.timezone,
|
|
5414
|
-
now
|
|
5415
|
-
),
|
|
5416
|
-
createdAt: now,
|
|
5417
|
-
updatedAt: now,
|
|
5418
|
-
createdBy: `core:${activation.createdByRunId}`
|
|
5419
|
-
};
|
|
5420
|
-
await this.scheduleAdapter.add(schedule);
|
|
5421
|
-
await Effect5.runPromise(this.data.putActivation({
|
|
5422
|
-
...activation,
|
|
5423
|
-
status: "active",
|
|
5424
|
-
updatedAt: now.toISOString()
|
|
5425
|
-
}));
|
|
5426
|
-
this.observability.append({
|
|
5427
|
-
type: "scheduled-action.created",
|
|
5428
|
-
category: "activation",
|
|
5429
|
-
agent: activation.definitionId,
|
|
5430
|
-
runId: activation.createdByRunId,
|
|
5431
|
-
data: { commandId: activation.id, recurring: true, nextRunAt: schedule.nextRunAt }
|
|
5432
|
-
});
|
|
5443
|
+
if (!cursor) {
|
|
5444
|
+
await this.settleFiredOneShot(active);
|
|
5433
5445
|
return;
|
|
5434
5446
|
}
|
|
5435
|
-
await this.
|
|
5447
|
+
await Effect5.runPromise(this.data.putActivation(active));
|
|
5448
|
+
this.observability.append({
|
|
5449
|
+
type: "scheduled-action.created",
|
|
5450
|
+
category: "activation",
|
|
5451
|
+
agent: activation.definitionId,
|
|
5452
|
+
runId: activation.createdByRunId,
|
|
5453
|
+
data: { commandId: activation.id, recurring: activation.timing.kind !== "at", nextRunAt: cursor }
|
|
5454
|
+
});
|
|
5455
|
+
this.armActivation(active);
|
|
5436
5456
|
} catch (cause) {
|
|
5437
5457
|
this.materializedActivations.delete(activation.id);
|
|
5438
5458
|
throw cause;
|
|
5439
5459
|
}
|
|
5440
5460
|
}
|
|
5441
|
-
|
|
5442
|
-
|
|
5443
|
-
|
|
5444
|
-
|
|
5445
|
-
|
|
5446
|
-
|
|
5461
|
+
/** A one-shot that fired before a restart: finish it if its occurrence has settled. */
|
|
5462
|
+
async settleFiredOneShot(activation) {
|
|
5463
|
+
const occurrences = (await this.listScheduleOccurrences({ activationId: activation.id })).filter((item) => item.revision === activation.scheduleRevision);
|
|
5464
|
+
const latest = occurrences.at(-1);
|
|
5465
|
+
if (latest && !TERMINAL_OCCURRENCE_STATUSES.has(latest.status)) return;
|
|
5466
|
+
await Effect5.runPromise(this.data.putActivation({
|
|
5447
5467
|
...activation,
|
|
5448
|
-
status: "
|
|
5449
|
-
|
|
5450
|
-
|
|
5451
|
-
|
|
5452
|
-
|
|
5468
|
+
status: latest && latest.status !== "cancelled" && latest.status !== "skipped" ? "completed" : "cancelled"
|
|
5469
|
+
}));
|
|
5470
|
+
}
|
|
5471
|
+
/** Wait for the activation's cursor, then fire; repeat while it has one. */
|
|
5472
|
+
armActivation(activation) {
|
|
5473
|
+
const id2 = activation.id;
|
|
5474
|
+
const revision = activation.scheduleRevision;
|
|
5475
|
+
let dueAt = activation.nextOccurrenceAt;
|
|
5476
|
+
let failures = 0;
|
|
5453
5477
|
const generation = {};
|
|
5454
|
-
this.activationDispatches.set(
|
|
5455
|
-
const
|
|
5456
|
-
if (this.activationDispatches.get(
|
|
5457
|
-
|
|
5458
|
-
|
|
5459
|
-
|
|
5460
|
-
const
|
|
5461
|
-
if (
|
|
5462
|
-
|
|
5463
|
-
|
|
5464
|
-
|
|
5465
|
-
|
|
5466
|
-
|
|
5467
|
-
|
|
5478
|
+
this.activationDispatches.set(id2, generation);
|
|
5479
|
+
const arm = () => {
|
|
5480
|
+
if (this.activationDispatches.get(id2) !== generation || !dueAt || this.stopping || this.disposed) {
|
|
5481
|
+
if (this.activationDispatches.get(id2) === generation) this.activationDispatches.delete(id2);
|
|
5482
|
+
return;
|
|
5483
|
+
}
|
|
5484
|
+
const remaining = Date.parse(dueAt) - Date.now();
|
|
5485
|
+
if (remaining > 0) {
|
|
5486
|
+
this.activationTimers.set(id2, setTimeout(arm, Math.min(remaining, 2147483647)));
|
|
5487
|
+
return;
|
|
5488
|
+
}
|
|
5489
|
+
this.activationTimers.delete(id2);
|
|
5490
|
+
void this.fireActivation(id2, revision, generation).then((next) => {
|
|
5491
|
+
failures = 0;
|
|
5492
|
+
dueAt = next;
|
|
5493
|
+
arm();
|
|
5494
|
+
}, (cause) => {
|
|
5495
|
+
failures++;
|
|
5496
|
+
this.observability.append({
|
|
5497
|
+
type: "activation.dispatch.error",
|
|
5498
|
+
category: "activation",
|
|
5499
|
+
agent: activation.definitionId,
|
|
5500
|
+
runId: activation.createdByRunId,
|
|
5501
|
+
data: { activationId: id2, error: cause instanceof Error ? cause.message : String(cause) }
|
|
5502
|
+
});
|
|
5503
|
+
if (this.activationDispatches.get(id2) !== generation) return;
|
|
5504
|
+
this.activationTimers.set(id2, setTimeout(arm, Math.min(1e3 * 2 ** Math.min(failures - 1, 8), 3e5)));
|
|
5505
|
+
});
|
|
5506
|
+
};
|
|
5507
|
+
arm();
|
|
5508
|
+
}
|
|
5509
|
+
/**
|
|
5510
|
+
* Take the occurrence at the activation's cursor: store it, then move the
|
|
5511
|
+
* cursor past it, then hand it to a run. Returns the new cursor.
|
|
5512
|
+
*/
|
|
5513
|
+
fireActivation(id2, revision, generation) {
|
|
5514
|
+
return this.withActivationTransition(async () => {
|
|
5515
|
+
if (this.activationDispatches.get(id2) !== generation || this.stopping || this.disposed) return void 0;
|
|
5516
|
+
const current = await Effect5.runPromise(this.data.getActivation(id2));
|
|
5517
|
+
if (!current || current.status !== "active" || current.scheduleRevision !== revision || !current.nextOccurrenceAt) {
|
|
5518
|
+
return void 0;
|
|
5519
|
+
}
|
|
5520
|
+
const scheduledFor = new Date(current.nextOccurrenceAt);
|
|
5521
|
+
const now = /* @__PURE__ */ new Date();
|
|
5522
|
+
const plan = planScheduleOccurrence(current.timing, scheduledFor, now);
|
|
5523
|
+
const occurrence = this.newOccurrence(current, scheduledFor, plan, now);
|
|
5524
|
+
const fresh = await this.claimOccurrence(occurrence);
|
|
5525
|
+
const { nextOccurrenceAt: _fired, ...rest } = current;
|
|
5526
|
+
const advanced = {
|
|
5527
|
+
...rest,
|
|
5528
|
+
...plan.next ? { nextOccurrenceAt: plan.next.toISOString() } : {},
|
|
5529
|
+
updatedAt: now.toISOString()
|
|
5468
5530
|
};
|
|
5469
|
-
await Effect5.runPromise(this.data.putActivation(
|
|
5531
|
+
await Effect5.runPromise(this.data.putActivation(advanced));
|
|
5532
|
+
const stored = fresh ? occurrence : await this.getOccurrence(occurrence.id);
|
|
5533
|
+
if (stored?.status === "pending" || stored?.status === "accepted" && !fresh) {
|
|
5534
|
+
await this.deliverOccurrence(stored, advanced);
|
|
5535
|
+
} else if (stored?.status === "skipped" && fresh) {
|
|
5536
|
+
this.observability.append({
|
|
5537
|
+
type: "scheduled-action.skipped",
|
|
5538
|
+
category: "activation",
|
|
5539
|
+
agent: current.definitionId,
|
|
5540
|
+
runId: current.createdByRunId,
|
|
5541
|
+
data: { activationId: id2, occurrenceId: stored.id, scheduledFor: stored.scheduledFor, reason: stored.reason }
|
|
5542
|
+
});
|
|
5543
|
+
} else if (stored && TERMINAL_OCCURRENCE_STATUSES.has(stored.status) && !plan.next) {
|
|
5544
|
+
await this.settleFiredOneShot(advanced);
|
|
5545
|
+
}
|
|
5546
|
+
return plan.next?.toISOString();
|
|
5547
|
+
});
|
|
5548
|
+
}
|
|
5549
|
+
newOccurrence(activation, scheduledFor, plan, now) {
|
|
5550
|
+
const at = scheduledFor.toISOString();
|
|
5551
|
+
const timestamp = now.toISOString();
|
|
5552
|
+
const id2 = scheduleOccurrenceId(activation.id, activation.scheduleRevision, at);
|
|
5553
|
+
return {
|
|
5554
|
+
id: id2,
|
|
5555
|
+
activationId: activation.id,
|
|
5556
|
+
revision: activation.scheduleRevision,
|
|
5557
|
+
scheduledFor: at,
|
|
5558
|
+
definitionId: activation.definitionId,
|
|
5559
|
+
agentId: activation.agentId,
|
|
5560
|
+
conversationId: activation.conversationId,
|
|
5561
|
+
workspaceId: activation.workspaceId,
|
|
5562
|
+
request: {
|
|
5563
|
+
agentId: activation.agentId,
|
|
5564
|
+
conversationId: activation.conversationId,
|
|
5565
|
+
workspaceId: activation.workspaceId,
|
|
5566
|
+
message: activation.message,
|
|
5567
|
+
...activation.payload !== void 0 ? { payload: activation.payload } : {},
|
|
5568
|
+
source: { kind: "activation", id: activation.id, occurrenceId: id2, scheduledFor: at }
|
|
5569
|
+
},
|
|
5570
|
+
status: plan.deliver ? "pending" : "skipped",
|
|
5571
|
+
...plan.deliver ? {} : { reason: "misfire", settledAt: timestamp },
|
|
5572
|
+
attempts: 0,
|
|
5573
|
+
...plan.coalesced ? { coalesced: plan.coalesced } : {},
|
|
5574
|
+
createdAt: timestamp,
|
|
5575
|
+
updatedAt: timestamp
|
|
5576
|
+
};
|
|
5577
|
+
}
|
|
5578
|
+
/**
|
|
5579
|
+
* Hand an occurrence to its conversation. Runs under the activation lock,
|
|
5580
|
+
* so a pause or cancel never sees it half handed over.
|
|
5581
|
+
*/
|
|
5582
|
+
async deliverOccurrence(occurrence, activation) {
|
|
5583
|
+
if (this.occurrencesInHand.has(occurrence.id)) return;
|
|
5584
|
+
this.occurrencesInHand.add(occurrence.id);
|
|
5585
|
+
try {
|
|
5586
|
+
const agent = await Effect5.runPromise(this.data.getAgent(occurrence.agentId));
|
|
5587
|
+
let inactive = !agent;
|
|
5588
|
+
if (agent?.durableSubagent) {
|
|
5589
|
+
try {
|
|
5590
|
+
await this.assertDurableActive(agent);
|
|
5591
|
+
} catch {
|
|
5592
|
+
inactive = true;
|
|
5593
|
+
}
|
|
5594
|
+
}
|
|
5595
|
+
if (inactive) {
|
|
5596
|
+
const now2 = (/* @__PURE__ */ new Date()).toISOString();
|
|
5597
|
+
await this.putOccurrence({ ...occurrence, status: "cancelled", reason: "agent-inactive", updatedAt: now2, settledAt: now2 });
|
|
5598
|
+
this.occurrencesInHand.delete(occurrence.id);
|
|
5599
|
+
return;
|
|
5600
|
+
}
|
|
5601
|
+
const runId = await this.enqueueCoreRequest(occurrence.definitionId, occurrence.request);
|
|
5602
|
+
this.occurrenceRuns.set(runId, occurrence.id);
|
|
5603
|
+
const latest = await this.getOccurrence(occurrence.id) ?? occurrence;
|
|
5604
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
5605
|
+
await this.putOccurrence({
|
|
5606
|
+
...latest,
|
|
5607
|
+
status: "accepted",
|
|
5608
|
+
runId,
|
|
5609
|
+
runIds: [...latest.runIds ?? [], runId],
|
|
5610
|
+
attempts: latest.attempts + 1,
|
|
5611
|
+
acceptedAt: now,
|
|
5612
|
+
updatedAt: now
|
|
5613
|
+
});
|
|
5614
|
+
const current = await Effect5.runPromise(this.data.getActivation(activation.id));
|
|
5615
|
+
if (current && current.status !== "cancelled" && current.status !== "completed") {
|
|
5616
|
+
await Effect5.runPromise(this.data.putActivation({ ...current, lastRunId: runId, updatedAt: now }));
|
|
5617
|
+
}
|
|
5470
5618
|
this.observability.append({
|
|
5471
5619
|
type: "core.command.accepted",
|
|
5472
5620
|
category: "system",
|
|
5473
|
-
agent:
|
|
5621
|
+
agent: occurrence.definitionId,
|
|
5474
5622
|
runId: activation.createdByRunId,
|
|
5475
|
-
data: {
|
|
5623
|
+
data: {
|
|
5624
|
+
commandId: activation.id,
|
|
5625
|
+
childRunId: runId,
|
|
5626
|
+
type: activation.kind,
|
|
5627
|
+
occurrenceId: occurrence.id,
|
|
5628
|
+
scheduledFor: occurrence.scheduledFor,
|
|
5629
|
+
attempt: latest.attempts + 1
|
|
5630
|
+
}
|
|
5631
|
+
});
|
|
5632
|
+
const settled = this.recentlySettledRuns.get(runId);
|
|
5633
|
+
if (settled) await this.settleOccurrence(runId, settled, { locked: true });
|
|
5634
|
+
} catch (cause) {
|
|
5635
|
+
this.occurrencesInHand.delete(occurrence.id);
|
|
5636
|
+
throw cause;
|
|
5637
|
+
}
|
|
5638
|
+
}
|
|
5639
|
+
/** The run handling an occurrence settled. A run stopped by shutdown did not: the next start delivers it again. */
|
|
5640
|
+
async settleOccurrence(runId, status, options = {}) {
|
|
5641
|
+
const occurrenceId = this.occurrenceRuns.get(runId);
|
|
5642
|
+
if (!occurrenceId || this.stopping) return;
|
|
5643
|
+
const work = async () => {
|
|
5644
|
+
const occurrence = await this.getOccurrence(occurrenceId);
|
|
5645
|
+
this.occurrenceRuns.delete(runId);
|
|
5646
|
+
if (!occurrence || occurrence.runId !== runId || TERMINAL_OCCURRENCE_STATUSES.has(occurrence.status)) return;
|
|
5647
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
5648
|
+
await this.putOccurrence({
|
|
5649
|
+
...occurrence,
|
|
5650
|
+
status,
|
|
5651
|
+
...status === "cancelled" ? { reason: "run-cancelled" } : {},
|
|
5652
|
+
updatedAt: now,
|
|
5653
|
+
settledAt: now
|
|
5476
5654
|
});
|
|
5477
|
-
|
|
5478
|
-
|
|
5479
|
-
|
|
5480
|
-
if (!latest || latest.status !== "active" || latest.lastRunId !== runId) return;
|
|
5655
|
+
this.occurrencesInHand.delete(occurrenceId);
|
|
5656
|
+
const activation = await Effect5.runPromise(this.data.getActivation(occurrence.activationId));
|
|
5657
|
+
if (activation?.timing.kind === "at" && activation.status === "active" && activation.scheduleRevision === occurrence.revision && activation.lastRunId === runId) {
|
|
5481
5658
|
await Effect5.runPromise(this.data.putActivation({
|
|
5482
|
-
...
|
|
5483
|
-
status:
|
|
5484
|
-
updatedAt:
|
|
5659
|
+
...activation,
|
|
5660
|
+
status: status === "cancelled" ? "cancelled" : "completed",
|
|
5661
|
+
updatedAt: now
|
|
5485
5662
|
}));
|
|
5486
|
-
}
|
|
5663
|
+
}
|
|
5664
|
+
};
|
|
5665
|
+
await (options.locked ? work() : this.withActivationTransition(work));
|
|
5666
|
+
}
|
|
5667
|
+
/**
|
|
5668
|
+
* After a restart: hand over every occurrence a previous process claimed
|
|
5669
|
+
* and no run finished, unless its activation has since been paused,
|
|
5670
|
+
* cancelled or changed.
|
|
5671
|
+
*/
|
|
5672
|
+
async replayScheduleOccurrences() {
|
|
5673
|
+
for (const occurrence of await this.listScheduleOccurrences({ status: ["pending", "accepted"] })) {
|
|
5674
|
+
if (this.occurrencesInHand.has(occurrence.id)) continue;
|
|
5675
|
+
if (occurrence.runId && await this.signalRunner.getRun(occurrence.runId)) continue;
|
|
5676
|
+
const activation = await Effect5.runPromise(this.data.getActivation(occurrence.activationId));
|
|
5677
|
+
const reason = !activation || activation.status === "cancelled" || activation.status === "completed" ? "cancelled" : activation.status === "paused" ? "paused" : !scheduleRevisionMatches(activation, occurrence.revision) ? "superseded" : void 0;
|
|
5678
|
+
if (reason) {
|
|
5679
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
5680
|
+
await this.putOccurrence({ ...occurrence, status: "cancelled", reason, updatedAt: now, settledAt: now });
|
|
5681
|
+
continue;
|
|
5682
|
+
}
|
|
5683
|
+
try {
|
|
5684
|
+
await this.deliverOccurrence(occurrence, activation);
|
|
5685
|
+
} catch (cause) {
|
|
5487
5686
|
this.observability.append({
|
|
5488
|
-
type: "activation.
|
|
5687
|
+
type: "activation.dispatch.error",
|
|
5489
5688
|
category: "activation",
|
|
5490
|
-
agent:
|
|
5491
|
-
|
|
5492
|
-
data: { error: cause instanceof Error ? cause.message : String(cause) }
|
|
5689
|
+
agent: occurrence.definitionId,
|
|
5690
|
+
data: { activationId: occurrence.activationId, occurrenceId: occurrence.id, error: cause instanceof Error ? cause.message : String(cause) }
|
|
5493
5691
|
});
|
|
5494
|
-
});
|
|
5495
|
-
});
|
|
5496
|
-
const arm = () => {
|
|
5497
|
-
const remaining = target - Date.now();
|
|
5498
|
-
if (remaining <= 0) {
|
|
5499
|
-
void dispatch().catch((cause) => {
|
|
5500
|
-
this.materializedActivations.delete(activation.id);
|
|
5501
|
-
this.observability.append({
|
|
5502
|
-
type: "activation.dispatch.error",
|
|
5503
|
-
category: "activation",
|
|
5504
|
-
agent: activation.definitionId,
|
|
5505
|
-
runId: activation.createdByRunId,
|
|
5506
|
-
data: { error: cause instanceof Error ? cause.message : String(cause) }
|
|
5507
|
-
});
|
|
5508
|
-
});
|
|
5509
|
-
return;
|
|
5510
5692
|
}
|
|
5511
|
-
|
|
5512
|
-
this.activationTimers.set(activation.id, timer);
|
|
5513
|
-
};
|
|
5514
|
-
this.observability.append({
|
|
5515
|
-
type: "scheduled-action.created",
|
|
5516
|
-
category: "activation",
|
|
5517
|
-
agent: activation.definitionId,
|
|
5518
|
-
runId: activation.createdByRunId,
|
|
5519
|
-
data: { commandId: activation.id, recurring: false, nextRunAt: activation.timing.at }
|
|
5520
|
-
});
|
|
5521
|
-
arm();
|
|
5693
|
+
}
|
|
5522
5694
|
}
|
|
5523
5695
|
async deliverOutbound(command, parentRunId, signal) {
|
|
5524
5696
|
return this.dispatchOutbound({
|
|
@@ -8499,6 +8671,20 @@ var FoundryServer = class {
|
|
|
8499
8671
|
);
|
|
8500
8672
|
return;
|
|
8501
8673
|
}
|
|
8674
|
+
if (url.pathname === "/api/schedule-occurrences" && method === "GET") {
|
|
8675
|
+
const status = url.searchParams.getAll("status").filter(Boolean);
|
|
8676
|
+
json(
|
|
8677
|
+
response,
|
|
8678
|
+
200,
|
|
8679
|
+
await this.runtime.listScheduleOccurrences({
|
|
8680
|
+
...url.searchParams.get("activation") ? { activationId: url.searchParams.get("activation") } : {},
|
|
8681
|
+
...url.searchParams.get("workspace") ? { workspaceId: url.searchParams.get("workspace") } : {},
|
|
8682
|
+
...url.searchParams.get("agent") ? { agentId: url.searchParams.get("agent") } : {},
|
|
8683
|
+
...status.length ? { status } : {}
|
|
8684
|
+
})
|
|
8685
|
+
);
|
|
8686
|
+
return;
|
|
8687
|
+
}
|
|
8502
8688
|
if (url.pathname === "/api/activations" && method === "GET") {
|
|
8503
8689
|
json(
|
|
8504
8690
|
response,
|