@mastra/factory 0.27.0 → 0.28.0-alpha.0
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 +6 -0
- package/dist/boards/index.js +1 -1
- package/dist/boards/project-boards.js +1 -1
- package/dist/boards/work.js +1 -1
- package/dist/dispatch-wake.d.ts +13 -0
- package/dist/dispatch-wake.d.ts.map +1 -0
- package/dist/dispatch-wake.js +30 -0
- package/dist/dispatch-wake.js.map +1 -0
- package/dist/environment/build-runner.d.ts +1 -1
- package/dist/environment/build-runner.d.ts.map +1 -1
- package/dist/environment/build-runner.js +2 -2
- package/dist/environment/build-workflow.d.ts +1 -1
- package/dist/environment/build-workflow.d.ts.map +1 -1
- package/dist/environment/build-workflow.js +1 -1
- package/dist/environment/build.js +1 -1
- package/dist/factory.d.ts.map +1 -1
- package/dist/factory.js +57 -44
- package/dist/factory.js.map +1 -1
- package/dist/index.js +9 -9
- package/dist/integrations/channel-thread-announcements.d.ts +5 -0
- package/dist/integrations/channel-thread-announcements.d.ts.map +1 -1
- package/dist/integrations/channel-thread-announcements.js +24 -2
- package/dist/integrations/channel-thread-announcements.js.map +1 -1
- package/dist/integrations/github/default-rules.js +1 -1
- package/dist/integrations/github/integration.js +2 -2
- package/dist/integrations/github/routes.d.ts.map +1 -1
- package/dist/integrations/github/routes.js +18 -17
- package/dist/integrations/github/routes.js.map +1 -1
- package/dist/integrations/github/rules.js +4 -4
- package/dist/integrations/github/sandbox.js +1 -1
- package/dist/integrations/github/session-subscriptions.js +3 -3
- package/dist/integrations/github/webhook.js +2 -2
- package/dist/integrations/gitlab/integration.js +2 -2
- package/dist/integrations/gitlab/issue-reconciler.js +1 -1
- package/dist/integrations/gitlab/merge-request-reconciler.js +3 -3
- package/dist/integrations/gitlab/routes.js +1 -1
- package/dist/integrations/gitlab/rules.js +3 -3
- package/dist/integrations/gitlab/session-subscriptions.js +1 -1
- package/dist/integrations/gitlab/webhook-dispatch.js +1 -1
- package/dist/integrations/incidentio/integration.js +3 -3
- package/dist/integrations/incidentio/rules.js +2 -2
- package/dist/integrations/jira/integration.js +2 -2
- package/dist/integrations/jira/routes.js +1 -1
- package/dist/integrations/jira/rules.js +2 -2
- package/dist/integrations/linear/integration.js +2 -2
- package/dist/integrations/linear/routes.d.ts +21 -1
- package/dist/integrations/linear/routes.d.ts.map +1 -1
- package/dist/integrations/linear/routes.js +20 -2
- package/dist/integrations/linear/routes.js.map +1 -1
- package/dist/integrations/linear/rules.js +2 -2
- package/dist/integrations/platform/github/event-worker.js +1 -1
- package/dist/integrations/platform/github/integration.d.ts.map +1 -1
- package/dist/integrations/platform/github/integration.js +8 -5
- package/dist/integrations/platform/github/integration.js.map +1 -1
- package/dist/integrations/platform/gitlab/event-worker.js +1 -1
- package/dist/integrations/platform/gitlab/integration.js +2 -2
- package/dist/integrations/platform/incidentio/integration.js +5 -5
- package/dist/integrations/platform/jira/agent-tools.js +1 -1
- package/dist/integrations/platform/jira/integration.js +2 -2
- package/dist/integrations/platform/jira/routes.js +1 -1
- package/dist/integrations/platform/linear/integration.d.ts.map +1 -1
- package/dist/integrations/platform/linear/integration.js +149 -45
- package/dist/integrations/platform/linear/integration.js.map +1 -1
- package/dist/integrations/slack/chat-tools.d.ts +16 -0
- package/dist/integrations/slack/chat-tools.d.ts.map +1 -0
- package/dist/integrations/slack/chat-tools.js +215 -0
- package/dist/integrations/slack/chat-tools.js.map +1 -0
- package/dist/integrations/slack/integration.d.ts +19 -3
- package/dist/integrations/slack/integration.d.ts.map +1 -1
- package/dist/integrations/slack/integration.js +25 -8
- package/dist/integrations/slack/integration.js.map +1 -1
- package/dist/integrations/slack/run-error-card.js +1 -1
- package/dist/integrations/slack/share-file-tool.d.ts +8 -0
- package/dist/integrations/slack/share-file-tool.d.ts.map +1 -1
- package/dist/integrations/slack/share-file-tool.js +13 -4
- package/dist/integrations/slack/share-file-tool.js.map +1 -1
- package/dist/integrations/slack/slack.js +3 -3
- package/dist/integrations/slack/tool-settings.d.ts +26 -0
- package/dist/integrations/slack/tool-settings.d.ts.map +1 -0
- package/dist/integrations/slack/tool-settings.js +35 -0
- package/dist/integrations/slack/tool-settings.js.map +1 -0
- package/dist/mcp/events.js +1 -1
- package/dist/mcp/routes.js +1 -1
- package/dist/routes/attention-activity.js +2 -2
- package/dist/routes/attention-providers.js +3 -3
- package/dist/routes/attention.js +2 -2
- package/dist/routes/automation-runs.js +1 -1
- package/dist/routes/boards.js +2 -2
- package/dist/routes/config.js +4 -4
- package/dist/routes/contracts.d.ts +30 -2
- package/dist/routes/contracts.d.ts.map +1 -1
- package/dist/routes/contracts.js +22 -5
- package/dist/routes/contracts.js.map +1 -1
- package/dist/routes/fs.js +1 -1
- package/dist/routes/intake.js +1 -1
- package/dist/routes/projects.d.ts.map +1 -1
- package/dist/routes/projects.js +35 -24
- package/dist/routes/projects.js.map +1 -1
- package/dist/routes/skills.js +2 -2
- package/dist/routes/source-control-sessions.js +2 -2
- package/dist/routes/surface.js +16 -16
- package/dist/routes/telemetry.js +1 -1
- package/dist/routes/work-items.js +9 -9
- package/dist/rules/decision-applicability.js +2 -2
- package/dist/rules/dispatcher.d.ts +14 -0
- package/dist/rules/dispatcher.d.ts.map +1 -1
- package/dist/rules/dispatcher.js +135 -47
- package/dist/rules/dispatcher.js.map +1 -1
- package/dist/rules/index.js +1 -1
- package/dist/rules/processor.d.ts +2 -0
- package/dist/rules/processor.d.ts.map +1 -1
- package/dist/rules/processor.js +4 -3
- package/dist/rules/processor.js.map +1 -1
- package/dist/rules/start-coordinator.js +2 -2
- package/dist/rules/terminal-cleanup.js.map +1 -1
- package/dist/rules/tools.js +5 -5
- package/dist/rules/transition-service.js +5 -5
- package/dist/sandbox/session-retirement.js +2 -2
- package/dist/sandbox/session-sandbox.js +1 -1
- package/dist/session/credential-policy.js +1 -1
- package/dist/session/factory-session.js +3 -3
- package/dist/session/self-review-gate.js +1 -1
- package/dist/session/source-control-tools.js +3 -3
- package/dist/storage/domains/audit/domain.js +1 -1
- package/dist/storage/domains/comments/base.js +3 -3
- package/dist/storage/domains/comments/domain.js +3 -3
- package/dist/storage/domains/comments/routes.js +1 -1
- package/dist/storage/domains/source-control/base.d.ts +20 -0
- package/dist/storage/domains/source-control/base.d.ts.map +1 -1
- package/dist/storage/domains/source-control/base.js +42 -0
- package/dist/storage/domains/source-control/base.js.map +1 -1
- package/dist/storage/domains/source-control/inmemory.d.ts +5 -1
- package/dist/storage/domains/source-control/inmemory.d.ts.map +1 -1
- package/dist/storage/domains/source-control/inmemory.js +23 -0
- package/dist/storage/domains/source-control/inmemory.js.map +1 -1
- package/dist/storage/domains/work-items/base.d.ts +16 -5
- package/dist/storage/domains/work-items/base.d.ts.map +1 -1
- package/dist/storage/domains/work-items/base.js +55 -72
- package/dist/storage/domains/work-items/base.js.map +1 -1
- package/dist/storage/domains/work-items/cards.js +1 -1
- package/dist/storage/domains/work-items/claimable-work.d.ts +15 -0
- package/dist/storage/domains/work-items/claimable-work.d.ts.map +1 -0
- package/dist/storage/domains/work-items/claimable-work.js +51 -0
- package/dist/storage/domains/work-items/claimable-work.js.map +1 -0
- package/dist/tools/index.js +2 -2
- package/dist/tools/messaging-tools.js +1 -1
- package/dist/tools/read-tools.js +4 -4
- package/dist/tools/scope.js +1 -1
- package/dist/tools/session-tools.js +9 -9
- package/dist/tools/work-item-tools.js +4 -4
- package/dist/work-items/create-work-item.js +2 -2
- package/dist/work-items/decision-actions.js +2 -2
- package/dist/work-items/providers/gitlab.js +1 -1
- package/dist/work-items/providers/incidentio.js +1 -1
- package/dist/work-items/providers/index.js +1 -1
- package/dist/work-items/providers/jira.js +1 -1
- package/dist/work-items/providers/linear.js +1 -1
- package/dist/work-items/split-migration.js +2 -2
- package/dist/workspace.d.ts +2 -0
- package/dist/workspace.d.ts.map +1 -1
- package/dist/workspace.js +10 -7
- package/dist/workspace.js.map +1 -1
- package/package.json +8 -8
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { factoryDispatchFailureMetadata } from "./dispatch-errors.js";
|
|
2
|
-
import { boardForWorkItem, resolvePhaseSemantics } from "../boards/semantics.js";
|
|
3
1
|
import "../boards/index.js";
|
|
2
|
+
import { boardForWorkItem, resolvePhaseSemantics } from "../boards/semantics.js";
|
|
4
3
|
import { workStageCarries } from "../boards/legacy-plan-role.js";
|
|
4
|
+
import { factoryDispatchFailureMetadata } from "./dispatch-errors.js";
|
|
5
5
|
// src/rules/decision-applicability.ts
|
|
6
6
|
/** The seat a skill run needs; other decision types are not tied to one phase. */
|
|
7
7
|
function skillDecisionRole(record) {
|
|
@@ -78,6 +78,20 @@ export declare class FactoryDecisionDispatcher {
|
|
|
78
78
|
constructor(options: FactoryDecisionDispatcherOptions);
|
|
79
79
|
start(): void;
|
|
80
80
|
stop(): Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* Runs a dispatch cycle now. Called after any write that leaves claimable
|
|
83
|
+
* work, locally or from another replica via pubsub. A wake during a running
|
|
84
|
+
* cycle makes the loop run once more instead of starting a second loop.
|
|
85
|
+
*/
|
|
86
|
+
wake(): void;
|
|
87
|
+
/**
|
|
88
|
+
* A run is stepping on a bound session thread. Interactive runs queue no
|
|
89
|
+
* work, so without this an idle loop would never see them and the trailing
|
|
90
|
+
* reconcile for results they leave behind would not happen. Each step keeps
|
|
91
|
+
* the loop active for one reconcile interval; only the first step after a
|
|
92
|
+
* settled pass wakes it, since an owed pass already rechecks every interval.
|
|
93
|
+
*/
|
|
94
|
+
noteBoundThreadRun(): void;
|
|
81
95
|
runOnce(now?: Date): Promise<void>;
|
|
82
96
|
}
|
|
83
97
|
export declare const FACTORY_DISPATCH_CONSTANTS: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dispatcher.d.ts","sourceRoot":"","sources":["../../src/rules/dispatcher.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC/D,OAAO,KAAK,EAAE,eAAe,EAAyC,MAAM,+BAA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M;aACN,SAAS;aACT,WAAW;aACX,cAAc;aACd,YAAY;aACZ,mCAAmC;aACnC,sBAAsB;aACtB,cAAc;aACd,WAAW;aACX,MAAM;CACE,CAAC"}
|
package/dist/rules/dispatcher.js
CHANGED
|
@@ -1,25 +1,26 @@
|
|
|
1
|
-
import { FACTORY_RULE_STAGES, externalSourceForWorkItem, workItemSource } from "./types.js";
|
|
2
|
-
import { assertFactoryDecisionTarget, validateFactoryRuleDecision } from "./validation.js";
|
|
3
|
-
import { FactoryDispatchError, factoryDispatchFailureCode, factoryDispatchFailureMetadata } from "./dispatch-errors.js";
|
|
4
|
-
import { FACTORY_RULE_MATERIALIZATION_KEY } from "../storage/domains/work-items/base.js";
|
|
5
|
-
import { workItemProviderAdapter } from "../work-items/providers/index.js";
|
|
6
|
-
import { cardRuleFacts } from "../boards/card-facts.js";
|
|
7
|
-
import { boardForWorkItem, isPullRequestCard, isTerminalWorkItem, pullRequestReviewPhase, resolvePhaseSemantics, reviewsPullRequest, workItemPhaseSemantics } from "../boards/semantics.js";
|
|
8
1
|
import "../boards/index.js";
|
|
9
|
-
import { toBoardSource } from "../boards/project-boards.js";
|
|
10
2
|
import { INTAKE_ROUTE_METADATA_KEY, landingPhase, placedByIntakeRoute, resolveIntakePlacement } from "../boards/intake-routing.js";
|
|
3
|
+
import { autoApprovedExitTool, startsInAutoPhase } from "../boards/phase-automation.js";
|
|
4
|
+
import { toBoardSource } from "../boards/project-boards.js";
|
|
11
5
|
import { moveCardToBoard } from "../boards/relocate.js";
|
|
12
|
-
import {
|
|
6
|
+
import { cardRuleFacts } from "../boards/card-facts.js";
|
|
7
|
+
import { boardForWorkItem, isPullRequestCard, isTerminalWorkItem, pullRequestReviewPhase, resolvePhaseSemantics, reviewsPullRequest, workItemPhaseSemantics } from "../boards/semantics.js";
|
|
13
8
|
import { FACTORY_OPEN_RUN_STALE_MS, heartbeatSessionOpenRun, isOpenRunLiveElsewhere, listSessionOpenRuns, recordSessionRunStart, waitForSessionRunAudit } from "../session/run-audit.js";
|
|
14
|
-
import { isHumanActorId } from "../storage/domains/audit/actors.js";
|
|
15
|
-
import { autoApprovedExitTool, startsInAutoPhase } from "../boards/phase-automation.js";
|
|
16
9
|
import { resolvePromptInvocation, resolveSkillInvocation, resolveSkillResumeInvocation } from "../skills/service.js";
|
|
10
|
+
import { isHumanActorId } from "../storage/domains/audit/actors.js";
|
|
17
11
|
import { withWorkItemFeed } from "../storage/domains/comments/feed-context.js";
|
|
12
|
+
import { FACTORY_RULE_MATERIALIZATION_KEY } from "../storage/domains/work-items/base.js";
|
|
13
|
+
import { workItemProviderAdapter } from "../work-items/providers/index.js";
|
|
14
|
+
import { decisionOvertaken } from "./decision-applicability.js";
|
|
15
|
+
import { FactoryDispatchError, factoryDispatchFailureCode, factoryDispatchFailureMetadata } from "./dispatch-errors.js";
|
|
16
|
+
import { FACTORY_RULE_STAGES, externalSourceForWorkItem, workItemSource } from "./types.js";
|
|
17
|
+
import { assertFactoryDecisionTarget, validateFactoryRuleDecision } from "./validation.js";
|
|
18
18
|
import { RequestContext } from "@mastra/core/request-context";
|
|
19
19
|
import { PROVIDER_CALL_ERROR_NAME, classifyProviderError } from "@mastra/core/error";
|
|
20
20
|
// src/rules/dispatcher.ts
|
|
21
21
|
const LEASE_MS = 3e4;
|
|
22
22
|
const POLL_MS = 1e3;
|
|
23
|
+
const IDLE_RECHECK_MS = 36e5;
|
|
23
24
|
const BATCH_SIZE = 10;
|
|
24
25
|
const MAX_ATTEMPTS = 5;
|
|
25
26
|
const MAX_PLAN_APPROVALS = 3;
|
|
@@ -314,8 +315,16 @@ var FactoryDecisionDispatcher = class {
|
|
|
314
315
|
#skillCompletionObservationTimeoutMs;
|
|
315
316
|
#lastReconcileAt;
|
|
316
317
|
#reconcileInFlight;
|
|
318
|
+
/** A reconcile is due once activity stops, to drain results of runs that just ended. */
|
|
319
|
+
#reconcileOwed = true;
|
|
320
|
+
/** Last step seen on a bound session thread; counts as activity for one reconcile interval. */
|
|
321
|
+
#lastBoundThreadRunAt;
|
|
322
|
+
#started = false;
|
|
323
|
+
#stopping = false;
|
|
324
|
+
#wakeRequested = false;
|
|
325
|
+
#unsubscribeClaimableWork;
|
|
326
|
+
#loop;
|
|
317
327
|
#timer;
|
|
318
|
-
#activeClaim;
|
|
319
328
|
/** Starts and run-bearing decisions; capped by `maxInFlight`. */
|
|
320
329
|
#runInFlight = /* @__PURE__ */ new Set();
|
|
321
330
|
/** Fast board bookkeeping (transitions, linked-card upserts); its own small cap. */
|
|
@@ -348,23 +357,111 @@ var FactoryDecisionDispatcher = class {
|
|
|
348
357
|
this.#skillCompletionObservationTimeoutMs = positiveMs(options.skillCompletionObservationTimeoutMs, SKILL_COMPLETION_OBSERVATION_TIMEOUT_MS);
|
|
349
358
|
}
|
|
350
359
|
start() {
|
|
351
|
-
if (this.#
|
|
352
|
-
this.#
|
|
353
|
-
this.#
|
|
354
|
-
this.#
|
|
360
|
+
if (this.#started) return;
|
|
361
|
+
this.#started = true;
|
|
362
|
+
this.#stopping = false;
|
|
363
|
+
this.#unsubscribeClaimableWork = this.#storage.onClaimableWork(() => this.wake());
|
|
364
|
+
this.wake();
|
|
355
365
|
}
|
|
356
366
|
async stop() {
|
|
357
|
-
|
|
358
|
-
this.#
|
|
359
|
-
|
|
367
|
+
this.#stopping = true;
|
|
368
|
+
this.#unsubscribeClaimableWork?.();
|
|
369
|
+
this.#unsubscribeClaimableWork = void 0;
|
|
370
|
+
this.#clearTimer();
|
|
371
|
+
await this.#loop;
|
|
360
372
|
await Promise.allSettled([
|
|
361
373
|
...this.#runInFlight,
|
|
362
374
|
...this.#bookkeepingInFlight,
|
|
363
375
|
...this.#backgroundInFlight
|
|
364
376
|
]);
|
|
377
|
+
this.#started = false;
|
|
378
|
+
this.#stopping = false;
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Runs a dispatch cycle now. Called after any write that leaves claimable
|
|
382
|
+
* work, locally or from another replica via pubsub. A wake during a running
|
|
383
|
+
* cycle makes the loop run once more instead of starting a second loop.
|
|
384
|
+
*/
|
|
385
|
+
wake() {
|
|
386
|
+
if (!this.#started || this.#stopping) return;
|
|
387
|
+
this.#wakeRequested = true;
|
|
388
|
+
if (this.#loop) return;
|
|
389
|
+
this.#clearTimer();
|
|
390
|
+
this.#loop = this.#runLoop();
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* A run is stepping on a bound session thread. Interactive runs queue no
|
|
394
|
+
* work, so without this an idle loop would never see them and the trailing
|
|
395
|
+
* reconcile for results they leave behind would not happen. Each step keeps
|
|
396
|
+
* the loop active for one reconcile interval; only the first step after a
|
|
397
|
+
* settled pass wakes it, since an owed pass already rechecks every interval.
|
|
398
|
+
*/
|
|
399
|
+
noteBoundThreadRun() {
|
|
400
|
+
if (!this.#reconcileToolResults) return;
|
|
401
|
+
this.#lastBoundThreadRunAt = Date.now();
|
|
402
|
+
if (this.#reconcileOwed) return;
|
|
403
|
+
this.#reconcileOwed = true;
|
|
404
|
+
this.wake();
|
|
365
405
|
}
|
|
366
406
|
async runOnce(now = /* @__PURE__ */ new Date()) {
|
|
367
|
-
|
|
407
|
+
this.#maybeSweepStaleBindings(now);
|
|
408
|
+
this.#maybeReconcileToolResults(now);
|
|
409
|
+
const { dispatches } = await this.#claimAndStart(now);
|
|
410
|
+
await Promise.all(dispatches);
|
|
411
|
+
}
|
|
412
|
+
#clearTimer() {
|
|
413
|
+
if (this.#timer) clearTimeout(this.#timer);
|
|
414
|
+
this.#timer = void 0;
|
|
415
|
+
}
|
|
416
|
+
async #runLoop() {
|
|
417
|
+
let delay = POLL_MS;
|
|
418
|
+
do {
|
|
419
|
+
this.#wakeRequested = false;
|
|
420
|
+
try {
|
|
421
|
+
delay = await this.#cycle();
|
|
422
|
+
} catch (error) {
|
|
423
|
+
console.error("Factory decision dispatch cycle failed", sanitizeDispatchError(error));
|
|
424
|
+
delay = POLL_MS;
|
|
425
|
+
}
|
|
426
|
+
} while (this.#wakeRequested && !this.#stopping);
|
|
427
|
+
this.#loop = void 0;
|
|
428
|
+
if (this.#stopping) return;
|
|
429
|
+
this.#timer = setTimeout(() => {
|
|
430
|
+
this.#timer = void 0;
|
|
431
|
+
this.wake();
|
|
432
|
+
}, delay);
|
|
433
|
+
this.#timer.unref?.();
|
|
434
|
+
}
|
|
435
|
+
/** One loop cycle; returns the delay until the next one. */
|
|
436
|
+
async #cycle() {
|
|
437
|
+
const now = /* @__PURE__ */ new Date();
|
|
438
|
+
this.#maybeSweepStaleBindings(now);
|
|
439
|
+
const active = this.#runInFlight.size > 0 || this.#bookkeepingInFlight.size > 0 || this.#controller.listActiveThreadRuns().length > 0 || this.#lastBoundThreadRunAt !== void 0 && now.getTime() - this.#lastBoundThreadRunAt < this.#reconcileIntervalMs;
|
|
440
|
+
if (!this.#reconcileToolResults) this.#reconcileOwed = false;
|
|
441
|
+
else if (active) this.#reconcileOwed = true;
|
|
442
|
+
if (this.#reconcileOwed && this.#maybeReconcileToolResults(now) && !active) this.#reconcileOwed = false;
|
|
443
|
+
let full;
|
|
444
|
+
try {
|
|
445
|
+
const claimed = await this.#claimAndStart(now);
|
|
446
|
+
full = claimed.full;
|
|
447
|
+
for (const dispatch of claimed.dispatches) dispatch.catch((error) => {
|
|
448
|
+
console.error("Factory decision dispatch failed", sanitizeDispatchError(error));
|
|
449
|
+
});
|
|
450
|
+
} catch (error) {
|
|
451
|
+
console.error("Factory decision dispatch cycle failed", sanitizeDispatchError(error));
|
|
452
|
+
return POLL_MS;
|
|
453
|
+
}
|
|
454
|
+
if (full || this.#runInFlight.size > 0 || this.#bookkeepingInFlight.size > 0) return POLL_MS;
|
|
455
|
+
const dueTimes = [];
|
|
456
|
+
if (this.#reconcileOwed) dueTimes.push((this.#lastReconcileAt?.getTime() ?? 0) + this.#reconcileIntervalMs);
|
|
457
|
+
try {
|
|
458
|
+
const nextClaimableAt = await this.#storage.nextClaimableAt();
|
|
459
|
+
if (nextClaimableAt) dueTimes.push(nextClaimableAt.getTime());
|
|
460
|
+
} catch (error) {
|
|
461
|
+
console.error("Factory decision dispatch cycle failed", sanitizeDispatchError(error));
|
|
462
|
+
return POLL_MS;
|
|
463
|
+
}
|
|
464
|
+
return Math.max(POLL_MS, Math.min(Math.min(...dueTimes, Infinity) - Date.now(), IDLE_RECHECK_MS));
|
|
368
465
|
}
|
|
369
466
|
/**
|
|
370
467
|
* Claims a batch and starts dispatches without awaiting their completion.
|
|
@@ -374,8 +471,7 @@ var FactoryDecisionDispatcher = class {
|
|
|
374
471
|
* decision. In-flight records stay protected from re-claim by lease renewal.
|
|
375
472
|
*/
|
|
376
473
|
async #claimAndStart(now) {
|
|
377
|
-
|
|
378
|
-
this.#maybeReconcileToolResults(now);
|
|
474
|
+
let full = false;
|
|
379
475
|
const leaseExpiresAt = new Date(now.getTime() + LEASE_MS);
|
|
380
476
|
const dispatches = [];
|
|
381
477
|
const runCapacity = this.#maxInFlight - this.#runInFlight.size;
|
|
@@ -388,6 +484,7 @@ var FactoryDecisionDispatcher = class {
|
|
|
388
484
|
limit
|
|
389
485
|
});
|
|
390
486
|
const decisionsLimit = limit - starts.length;
|
|
487
|
+
full = decisionsLimit <= 0;
|
|
391
488
|
const decisions = decisionsLimit > 0 ? await this.#storage.claimDeferredDecisions({
|
|
392
489
|
ownerId: this.#ownerId,
|
|
393
490
|
now,
|
|
@@ -395,20 +492,26 @@ var FactoryDecisionDispatcher = class {
|
|
|
395
492
|
limit: decisionsLimit,
|
|
396
493
|
decisionFilter: isRunBearingDecision
|
|
397
494
|
}) : [];
|
|
495
|
+
full ||= starts.length + decisions.length >= limit;
|
|
398
496
|
dispatches.push(...starts.map((start) => this.#track(this.#runInFlight, this.#dispatchPendingStart(start, now))), ...decisions.map((decision) => this.#track(this.#runInFlight, this.#dispatchDecision(decision, now))));
|
|
399
497
|
}
|
|
400
498
|
const bookkeepingCapacity = BOOKKEEPING_MAX_IN_FLIGHT - this.#bookkeepingInFlight.size;
|
|
401
499
|
if (bookkeepingCapacity > 0) {
|
|
500
|
+
const limit = Math.min(BATCH_SIZE, bookkeepingCapacity);
|
|
402
501
|
const decisions = await this.#storage.claimDeferredDecisions({
|
|
403
502
|
ownerId: this.#ownerId,
|
|
404
503
|
now,
|
|
405
504
|
leaseExpiresAt,
|
|
406
|
-
limit
|
|
505
|
+
limit,
|
|
407
506
|
decisionFilter: isBookkeepingDecision
|
|
408
507
|
});
|
|
508
|
+
full ||= decisions.length >= limit;
|
|
409
509
|
dispatches.push(...decisions.map((decision) => this.#track(this.#bookkeepingInFlight, this.#dispatchDecision(decision, now))));
|
|
410
510
|
}
|
|
411
|
-
return
|
|
511
|
+
return {
|
|
512
|
+
dispatches,
|
|
513
|
+
full
|
|
514
|
+
};
|
|
412
515
|
}
|
|
413
516
|
/**
|
|
414
517
|
* Throttled, coalesced, non-blocking bound-thread reconcile: dispatch
|
|
@@ -416,8 +519,8 @@ var FactoryDecisionDispatcher = class {
|
|
|
416
519
|
* are skipped while one is still in flight.
|
|
417
520
|
*/
|
|
418
521
|
#maybeReconcileToolResults(now) {
|
|
419
|
-
if (!this.#reconcileToolResults || this.#reconcileInFlight) return;
|
|
420
|
-
if (this.#lastReconcileAt && now.getTime() - this.#lastReconcileAt.getTime() < this.#reconcileIntervalMs) return;
|
|
522
|
+
if (!this.#reconcileToolResults || this.#reconcileInFlight) return false;
|
|
523
|
+
if (this.#lastReconcileAt && now.getTime() - this.#lastReconcileAt.getTime() < this.#reconcileIntervalMs) return false;
|
|
421
524
|
this.#lastReconcileAt = now;
|
|
422
525
|
const run = this.#reconcileToolResults().catch((error) => {
|
|
423
526
|
console.error("Factory tool-result reconcile failed", sanitizeDispatchError(error));
|
|
@@ -426,6 +529,7 @@ var FactoryDecisionDispatcher = class {
|
|
|
426
529
|
});
|
|
427
530
|
this.#reconcileInFlight = run;
|
|
428
531
|
this.#track(this.#backgroundInFlight, run);
|
|
532
|
+
return true;
|
|
429
533
|
}
|
|
430
534
|
/** Slow-cadence revocation of leaked/legacy bindings; failures never block the claim path. */
|
|
431
535
|
async #maybeSweepStaleBindings(now) {
|
|
@@ -456,23 +560,11 @@ var FactoryDecisionDispatcher = class {
|
|
|
456
560
|
}
|
|
457
561
|
#track(pool, dispatch) {
|
|
458
562
|
pool.add(dispatch);
|
|
459
|
-
dispatch.catch(() => {}).then(() =>
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
async #tick() {
|
|
463
|
-
if (this.#activeClaim) return;
|
|
464
|
-
this.#activeClaim = this.#claimAndStart(/* @__PURE__ */ new Date()).then((dispatches) => {
|
|
465
|
-
for (const dispatch of dispatches) dispatch.catch((error) => {
|
|
466
|
-
console.error("Factory decision dispatch failed", sanitizeDispatchError(error));
|
|
467
|
-
});
|
|
468
|
-
}, (error) => {
|
|
469
|
-
console.error("Factory decision dispatch cycle failed", sanitizeDispatchError(error));
|
|
563
|
+
dispatch.catch(() => {}).then(() => {
|
|
564
|
+
pool.delete(dispatch);
|
|
565
|
+
if (pool !== this.#backgroundInFlight) this.wake();
|
|
470
566
|
});
|
|
471
|
-
|
|
472
|
-
await this.#activeClaim;
|
|
473
|
-
} finally {
|
|
474
|
-
this.#activeClaim = void 0;
|
|
475
|
-
}
|
|
567
|
+
return dispatch;
|
|
476
568
|
}
|
|
477
569
|
async #dispatchDecision(record, now) {
|
|
478
570
|
let executionCompleted = false;
|
|
@@ -506,11 +598,7 @@ var FactoryDecisionDispatcher = class {
|
|
|
506
598
|
if (terminal && failed) await this.#supersedeFailureOnSettledCard(failed);
|
|
507
599
|
}
|
|
508
600
|
}
|
|
509
|
-
/**
|
|
510
|
-
* Startup repair and terminal cleanup already settle a failed effect on a done or canceled
|
|
511
|
-
* card as superseded; a failure landing after the card settled would otherwise page a person
|
|
512
|
-
* until the next restart.
|
|
513
|
-
*/
|
|
601
|
+
/** Terminal cleanup ran before this failure landed, so it never saw this row. */
|
|
514
602
|
async #supersedeFailureOnSettledCard(record) {
|
|
515
603
|
if (!record.workItemId) return;
|
|
516
604
|
try {
|