@parcha/agentrun-serve 0.1.0-beta.52 → 0.1.0-beta.54
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 +149 -1
- package/README.md +295 -37
- package/dist/agent-delete.d.ts +5 -6
- package/dist/agent-delete.js +30 -67
- package/dist/agent-package.d.ts +71 -0
- package/dist/agent-package.js +345 -0
- package/dist/agent-rows.d.ts +1 -1
- package/dist/agents-store.d.ts +26 -20
- package/dist/agents-store.js +42 -34
- package/dist/api-only.d.ts +25 -6
- package/dist/api-only.js +34 -13
- package/dist/claude-login.d.ts +1 -0
- package/dist/claude-login.js +1 -1
- package/dist/cli-learning/cli.d.ts +3 -0
- package/dist/cli-learning/cli.js +17 -3
- package/dist/cli-learning/connections.js +3 -2
- package/dist/cli-learning/schema.js +2 -1
- package/dist/cli.js +76 -8
- package/dist/composio-preset.d.ts +3 -1
- package/dist/composio-preset.js +5 -3
- package/dist/composio-triggers.d.ts +4 -0
- package/dist/composio-triggers.js +4 -1
- package/dist/computer-needs.d.ts +32 -0
- package/dist/computer-needs.js +80 -0
- package/dist/computer-setting.d.ts +5 -2
- package/dist/computer-setting.js +8 -1
- package/dist/computers.d.ts +0 -2
- package/dist/computers.js +1 -1
- package/dist/console/assets/index-B_85An2t.js +745 -0
- package/dist/console/assets/index-n1YsbnYh.css +1 -0
- package/dist/console/index.html +2 -2
- package/dist/console-links.d.ts +1 -0
- package/dist/console-links.js +2 -0
- package/dist/env-switch.d.ts +14 -0
- package/dist/env-switch.js +23 -0
- package/dist/errors.d.ts +5 -0
- package/dist/errors.js +10 -0
- package/dist/eval-previous.js +4 -3
- package/dist/fal-price.js +10 -13
- package/dist/github-connection.d.ts +50 -1
- package/dist/github-connection.js +96 -3
- package/dist/github-faux.d.ts +11 -2
- package/dist/github-faux.js +138 -7
- package/dist/github-pull-request.d.ts +37 -0
- package/dist/github-pull-request.js +110 -0
- package/dist/handed-secret.d.ts +4 -0
- package/dist/handed-secret.js +10 -0
- package/dist/harness-archive.d.ts +15 -5
- package/dist/harness-archive.js +71 -34
- package/dist/harness-check.d.ts +2 -1
- package/dist/harness-check.js +36 -95
- package/dist/harness-egress.d.ts +23 -0
- package/dist/harness-egress.js +69 -0
- package/dist/harness-recipe-entry.d.ts +71 -0
- package/dist/harness-recipe-entry.js +184 -0
- package/dist/harness-recipe.d.ts +73 -6
- package/dist/harness-recipe.js +185 -31
- package/dist/harness-sign-in.d.ts +29 -0
- package/dist/harness-sign-in.js +122 -0
- package/dist/harness-table.d.ts +21 -0
- package/dist/harness-table.js +58 -3
- package/dist/harness-zip.d.ts +6 -0
- package/dist/harness-zip.js +203 -0
- package/dist/ids.d.ts +11 -0
- package/dist/ids.js +36 -0
- package/dist/learn/commit.d.ts +14 -1
- package/dist/learn/commit.js +156 -1
- package/dist/learn/compile.js +22 -8
- package/dist/learn/folder.d.ts +5 -0
- package/dist/learn/folder.js +10 -1
- package/dist/learn/learning.sh +15 -3
- package/dist/learn/promote.d.ts +2 -2
- package/dist/learn/secrets.js +30 -7
- package/dist/learning-routes.d.ts +23 -11
- package/dist/learning-routes.js +110 -20
- package/dist/learning.d.ts +9 -6
- package/dist/learning.js +9 -4
- package/dist/live-reach.d.ts +34 -7
- package/dist/live-reach.js +76 -11
- package/dist/mcp-gateway.js +15 -2
- package/dist/mcp-probe.d.ts +3 -0
- package/dist/mcp-probe.js +8 -3
- package/dist/mcp-resources.d.ts +44 -0
- package/dist/mcp-resources.js +100 -0
- package/dist/mcp-servers-store.d.ts +16 -12
- package/dist/model.d.ts +4 -4
- package/dist/model.js +6 -6
- package/dist/native/cli.js +9 -2
- package/dist/native/connection.d.ts +2 -2
- package/dist/native/connection.js +5 -2
- package/dist/native/discovery.d.ts +2 -0
- package/dist/native/discovery.js +10 -6
- package/dist/native/remote-worker-bridge.js +36 -4
- package/dist/native/remote.d.ts +18 -1
- package/dist/native/remote.js +126 -37
- package/dist/native/schema.d.ts +6 -0
- package/dist/native/schema.js +31 -6
- package/dist/native/source-account.d.ts +13 -0
- package/dist/native/source-account.js +114 -0
- package/dist/native/source-capture-hooks.d.ts +5 -0
- package/dist/native/source-capture-hooks.js +102 -0
- package/dist/native/source-mcp-selection.d.ts +13 -0
- package/dist/native/source-mcp-selection.js +72 -0
- package/dist/native/source-runtime-tools.d.ts +19 -0
- package/dist/native/source-runtime-tools.js +103 -0
- package/dist/native/source-snapshot-types.d.ts +138 -0
- package/dist/native/source-snapshot-types.js +12 -0
- package/dist/native/source-snapshot.d.ts +8 -0
- package/dist/native/source-snapshot.js +1201 -0
- package/dist/observation/learning.js +3 -2
- package/dist/openapi.js +275 -40
- package/dist/print.js +1 -1
- package/dist/purged-workflow.d.ts +12 -0
- package/dist/purged-workflow.js +43 -0
- package/dist/run-keys.d.ts +46 -0
- package/dist/run-keys.js +109 -0
- package/dist/run-launch.d.ts +16 -1
- package/dist/run-launch.js +30 -1
- package/dist/run-learning-served.d.ts +2 -2
- package/dist/run-model-key.d.ts +2 -2
- package/dist/run-model-key.js +5 -5
- package/dist/run-secrets.d.ts +2 -0
- package/dist/run-secrets.js +7 -4
- package/dist/run-view.d.ts +17 -0
- package/dist/run-view.js +25 -1
- package/dist/runner/adapter-loop.d.ts +11 -3
- package/dist/runner/adapter-loop.js +138 -22
- package/dist/runner/adapter-run-spec.js +26 -13
- package/dist/runner/audit.d.ts +2 -0
- package/dist/runner/audit.js +9 -1
- package/dist/runner/beside.js +2 -1
- package/dist/runner/claim-secrets.d.ts +29 -3
- package/dist/runner/claim-secrets.js +38 -4
- package/dist/runner/cli-run-spec.js +24 -13
- package/dist/runner/harness-loops.d.ts +6 -0
- package/dist/runner/harness-loops.js +11 -1
- package/dist/runner/held.d.ts +4 -0
- package/dist/runner/journal-kinds.d.ts +2 -0
- package/dist/runner/learned-first.d.ts +2 -0
- package/dist/runner/learned-first.js +1 -1
- package/dist/runner/learning-end.d.ts +7 -2
- package/dist/runner/learning-end.js +22 -10
- package/dist/runner/learning-seed.d.ts +9 -3
- package/dist/runner/learning-seed.js +2 -2
- package/dist/runner/main.d.ts +1 -0
- package/dist/runner/main.js +20 -13
- package/dist/runner/mcp-claim.js +6 -1
- package/dist/runner/mcp-run.d.ts +10 -7
- package/dist/runner/mcp-run.js +3 -5
- package/dist/runner/metrics.d.ts +4 -2
- package/dist/runner/metrics.js +15 -3
- package/dist/runner/monid-run.js +9 -11
- package/dist/runner/pr-checkout.d.ts +21 -4
- package/dist/runner/pr-checkout.js +103 -4
- package/dist/runner/project-checkpoint.d.ts +35 -0
- package/dist/runner/project-checkpoint.js +116 -0
- package/dist/runner/project-drain.d.ts +8 -0
- package/dist/runner/project-drain.js +25 -0
- package/dist/runner/project-host.d.ts +10 -0
- package/dist/runner/project-host.js +52 -0
- package/dist/runner/promotion.d.ts +14 -6
- package/dist/runner/promotion.js +98 -41
- package/dist/runner/redact.d.ts +13 -0
- package/dist/runner/redact.js +67 -1
- package/dist/runner/run-channel.d.ts +9 -0
- package/dist/runner/run-channel.js +15 -2
- package/dist/runner/run-child-host.d.ts +8 -4
- package/dist/runner/run-child-host.js +178 -45
- package/dist/runner/run-errors.js +2 -2
- package/dist/runner/runner-check.d.ts +3 -0
- package/dist/runner/runner-check.js +28 -2
- package/dist/runner/runner.js +26 -9
- package/dist/runner/store-secrets.js +7 -3
- package/dist/runner/workflow-first.d.ts +4 -0
- package/dist/runner/workflow-first.js +13 -0
- package/dist/runner/workflow-loop.d.ts +2 -1
- package/dist/runner/workflow-loop.js +2 -1
- package/dist/runner/workflow-spec.js +1 -1
- package/dist/runs-over-store.d.ts +34 -6
- package/dist/runs-over-store.js +133 -25
- package/dist/sdk/runs.d.ts +11 -1
- package/dist/sdk/runs.js +117 -26
- package/dist/sdk/schema.js +12 -2
- package/dist/sdk/workflows.d.ts +1 -1
- package/dist/server.d.ts +79 -32
- package/dist/server.js +673 -113
- package/dist/skills/agentrun-serve/SKILL.md +2 -1
- package/dist/source-sync/adoption.d.ts +15 -0
- package/dist/source-sync/adoption.js +36 -0
- package/dist/source-sync/auxiliary.d.ts +23 -0
- package/dist/source-sync/auxiliary.js +64 -0
- package/dist/source-sync/capture-git.d.ts +5 -0
- package/dist/source-sync/capture-git.js +272 -0
- package/dist/source-sync/capture-project.d.ts +53 -0
- package/dist/source-sync/capture-project.js +206 -0
- package/dist/source-sync/capture-refusal.d.ts +7 -0
- package/dist/source-sync/capture-refusal.js +16 -0
- package/dist/source-sync/capture.d.ts +11 -0
- package/dist/source-sync/capture.js +22 -0
- package/dist/source-sync/checkpoint-gate.d.ts +21 -0
- package/dist/source-sync/checkpoint-gate.js +52 -0
- package/dist/source-sync/checkpoints.d.ts +66 -0
- package/dist/source-sync/checkpoints.js +168 -0
- package/dist/source-sync/claim.d.ts +11 -0
- package/dist/source-sync/claim.js +48 -0
- package/dist/source-sync/destination.d.ts +47 -0
- package/dist/source-sync/destination.js +239 -0
- package/dist/source-sync/io.d.ts +15 -0
- package/dist/source-sync/io.js +52 -0
- package/dist/source-sync/local.d.ts +43 -0
- package/dist/source-sync/local.js +229 -0
- package/dist/source-sync/prepare.d.ts +59 -0
- package/dist/source-sync/prepare.js +93 -0
- package/dist/source-sync/projects.d.ts +61 -0
- package/dist/source-sync/projects.js +130 -0
- package/dist/source-sync/registry.d.ts +95 -0
- package/dist/source-sync/registry.js +226 -0
- package/dist/source-sync/service.d.ts +33 -0
- package/dist/source-sync/service.js +118 -0
- package/dist/source-sync/skill-edits.d.ts +12 -0
- package/dist/source-sync/skill-edits.js +122 -0
- package/dist/source-sync/snapshots.d.ts +48 -0
- package/dist/source-sync/snapshots.js +448 -0
- package/dist/source-sync/types.d.ts +9 -0
- package/dist/source-sync/types.js +14 -0
- package/dist/source-sync/workspace.d.ts +66 -0
- package/dist/source-sync/workspace.js +273 -0
- package/dist/store/agent-body.d.ts +6 -2
- package/dist/store/agent-body.js +18 -7
- package/dist/store/index.d.ts +2 -2
- package/dist/store/index.js +1 -1
- package/dist/store/schema.d.ts +2 -1
- package/dist/store/schema.js +2 -1
- package/dist/store/seal.d.ts +5 -1
- package/dist/store/seal.js +12 -3
- package/dist/store/sqlite/project-checkpoints.d.ts +11 -0
- package/dist/store/sqlite/project-checkpoints.js +70 -0
- package/dist/store/sqlite/run-launch.d.ts +4 -12
- package/dist/store/sqlite/run-launch.js +5 -19
- package/dist/store/sqlite/store.js +224 -170
- package/dist/store/store.d.ts +116 -39
- package/dist/store/store.js +14 -1
- package/dist/store-reads/learning.d.ts +2 -2
- package/dist/store-view.d.ts +3 -1
- package/dist/store-view.js +17 -5
- package/dist/suggestions.d.ts +282 -0
- package/dist/suggestions.js +919 -0
- package/dist/telemetry.d.ts +8 -1
- package/dist/telemetry.js +12 -4
- package/dist/template-import.d.ts +27 -2
- package/dist/template-import.js +148 -13
- package/dist/templates.d.ts +103 -77
- package/dist/templates.js +71 -17
- package/dist/timeline/index.d.ts +1 -1
- package/dist/timeline/index.js +1 -1
- package/dist/timeline/timeline.d.ts +5 -0
- package/dist/timeline/timeline.js +8 -0
- package/dist/tool-plane.d.ts +12 -12
- package/dist/tools.d.ts +8 -8
- package/dist/trigger-launch-hook.d.ts +49 -0
- package/dist/trigger-launch-hook.js +91 -0
- package/dist/trigger-targets.d.ts +14 -1
- package/dist/trigger-targets.js +20 -0
- package/dist/triggers.d.ts +54 -5
- package/dist/triggers.js +143 -8
- package/dist/uploads-archil.js +1 -1
- package/dist/uploads.js +1 -1
- package/dist/validate.d.ts +1 -1
- package/dist/validate.js +3 -4
- package/dist/workspace/computer-for-claim.d.ts +2 -0
- package/dist/workspace/computer-for-claim.js +43 -0
- package/dist/workspace/paths.d.ts +6 -0
- package/dist/workspace/paths.js +6 -0
- package/dist/workspace/resolve-computer.d.ts +1 -0
- package/dist/workspace/run-tools.d.ts +2 -0
- package/dist/workspace/run-tools.js +8 -0
- package/dist/workspace/synced-folder.d.ts +1 -1
- package/dist/workspace/synced-folder.js +11 -14
- package/launcher/agentrun-serve +1 -1
- package/package.json +10 -10
- package/skills/agentrun-serve/SKILL.md +2 -1
- package/templates/README.md +8 -4
- package/templates/agent-scout/agent.md +46 -0
- package/templates/agent-scout/skills/scout/SKILL.md +88 -0
- package/templates/agent-scout/skills/scout/suggestions.schema.json +112 -0
- package/dist/console/assets/index-B-D0EM3j.js +0 -733
- package/dist/console/assets/index-DlQepP2J.css +0 -1
package/dist/runs-over-store.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { reconcileProjectCheckpoints } from "./source-sync/checkpoints.js";
|
|
2
|
+
import { requireSavedProject } from "./source-sync/checkpoint-gate.js";
|
|
1
3
|
import { NATIVE_RUN_OVERRIDES_UNSUPPORTED, nativeOf, nativeRunOverrides } from "./native/binding.js";
|
|
2
4
|
import { launchNative } from "./native/connection.js";
|
|
3
5
|
// The write half of the run's server cut (design 2.1, "the runner takes every run"): the functions the v2 routes call to
|
|
@@ -12,29 +14,32 @@ import { launchNative } from "./native/connection.js";
|
|
|
12
14
|
// is the run view's to read from the journal (the server's view layer, run-view.ts). These writers take it as `RunMoment`
|
|
13
15
|
// and never recompute it; the coarse facts a row already holds (its state, its launch cap) they read from the store.
|
|
14
16
|
import { randomBytes } from "node:crypto";
|
|
15
|
-
import { externalKey, getAgent } from "./agents-store.js";
|
|
17
|
+
import { externalKey, getAgent, ownedElsewhere } from "./agents-store.js";
|
|
16
18
|
import { CAP_TOLERANCE, money } from "./cap.js";
|
|
17
19
|
import { holdOf } from "./computer-held.js";
|
|
18
20
|
import { computerOfPlace, settingOfDefinition } from "./computer-setting.js";
|
|
19
21
|
import { ApiError, invalid, unsupported } from "./errors.js";
|
|
20
22
|
import { labelsOf } from "./run-labels.js";
|
|
21
23
|
import { harnessOfBody } from "./harness.js";
|
|
24
|
+
import { uuidv7 } from "./ids.js";
|
|
22
25
|
import { redacted, redactedOutput } from "./key-refusal.js";
|
|
23
26
|
import { chooseWorkflows, learnedSection, learnsOf, RUN_LEARNING_USE } from "./learning.js";
|
|
24
27
|
import { thinkingLevel } from "./model.js";
|
|
25
28
|
import { resolvePlace } from "./place.js";
|
|
26
29
|
import { githubTargetOf } from "./github.js";
|
|
27
30
|
import { repoOf } from "./run-repo.js";
|
|
28
|
-
import { launchBody } from "./run-launch.js";
|
|
31
|
+
import { launchBody, sourceProjectOf } from "./run-launch.js";
|
|
29
32
|
import { learningDiskOf } from "./learn/learning-of.js";
|
|
30
33
|
import { modelKeyRequired, serverKeyMissing } from "./run-model-key.js";
|
|
31
34
|
import { raiseFollowUpId } from "./run-view.js";
|
|
32
35
|
import { sendCommand } from "./runner/commands.js";
|
|
33
36
|
import { refuseWhileNoRunner } from "./runner/presence.js";
|
|
34
|
-
import { AgentDeleting, AnswerTaken, ExternalBodyConflict, RunFinal, StoreConstraint, StoreLaunchMissing } from "./store/index.js";
|
|
37
|
+
import { RevisionMoved, AgentDeleting, AgentPurged, AgentArchived, AnswerTaken, ExternalBodyConflict, RunFinal, StoreConstraint, StoreLaunchMissing } from "./store/index.js";
|
|
35
38
|
import { canonical } from "./store/agent-body.js";
|
|
39
|
+
import { sourceSyncOf } from "./source-sync/types.js";
|
|
36
40
|
import { toolingOf } from "./tools.js";
|
|
37
41
|
import { planeFor } from "./tool-plane.js";
|
|
42
|
+
import { declaredKeys, undeclaredKeys, withCarried } from "./run-keys.js";
|
|
38
43
|
import { clip } from "./web-fetch-direct.js";
|
|
39
44
|
import { isObject } from "./validate.js";
|
|
40
45
|
/** Why a message that may not answer a person's question is not sent (runs.ts WAITS_ON_A_PERSON). */
|
|
@@ -175,7 +180,8 @@ export async function message(store, moment, content, { requestId, answers = tru
|
|
|
175
180
|
throw new ApiError("input_invalid", "Native jobs do not support in-session steering or follow-up messages. Start a new native run.");
|
|
176
181
|
// Archiving closes a run: it, or a run of its archived agent, takes no message, said as archived (never as finished). Another
|
|
177
182
|
// system's agent is archived only by its delete: said so, as every run of it is refused (AgentDeleting).
|
|
178
|
-
|
|
183
|
+
const archivedAgent = moment.archived === "agent" ? await store.agents.get(moment.agentId) : null;
|
|
184
|
+
if (archivedAgent && ownedElsewhere(archivedAgent))
|
|
179
185
|
throw new AgentDeleting(moment.agentId);
|
|
180
186
|
if (moment.archived !== undefined)
|
|
181
187
|
throw archivedRefusal(moment);
|
|
@@ -397,16 +403,6 @@ export async function approve(store, moment, id, decision, note, confirmed = fal
|
|
|
397
403
|
throw alreadyAnswered(id, end.decision);
|
|
398
404
|
}
|
|
399
405
|
}
|
|
400
|
-
// ---- start -----------------------------------------------------------------------------------------------------------
|
|
401
|
-
/** A UUIDv7 run id: time-ordered, so a listing by start time is stable, and unique per run. */
|
|
402
|
-
export function uuidv7(now = Date.now()) {
|
|
403
|
-
const bytes = randomBytes(16);
|
|
404
|
-
bytes.writeUIntBE(now, 0, 6);
|
|
405
|
-
bytes[6] = (bytes[6] & 0x0f) | 0x70;
|
|
406
|
-
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
407
|
-
const hex = bytes.toString("hex");
|
|
408
|
-
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
409
|
-
}
|
|
410
406
|
/** A no-op redactor, when the server gives none: no secret is cut (the switch wires the real shown for a learning run). */
|
|
411
407
|
const noShown = { text: (text) => text, name: (name) => name, holds: () => false };
|
|
412
408
|
/** The run's learning start: undefined when the run does not learn (the server's learning off, a workflow revision, or
|
|
@@ -448,19 +444,37 @@ export async function learningStartFor(deps, entry, revision, rbody, { use = fal
|
|
|
448
444
|
* (store.runs.create), which a runner reads at its claim. No process, no door, no setup file: where the run runs is the
|
|
449
445
|
* runner's. Under `origin`, a key that already started a run gets that run (the store's one-run-per-key), so a retry never
|
|
450
446
|
* starts a second. */
|
|
451
|
-
export async function startRun(store, deps, entry, body, { trigger, origin, capped = false, from, title, sealed } = {}) {
|
|
447
|
+
export async function startRun(store, deps, entry, body, { trigger, origin, capped = false, from, title, sealed, keys } = {}) {
|
|
448
|
+
if (sourceSyncOf(entry.revision?.body)) {
|
|
449
|
+
// Re-delivery of a firing that already admitted a run is not a new start and needs no new project preparation.
|
|
450
|
+
if (origin) {
|
|
451
|
+
const found = await store.runOfOrigin(origin.key);
|
|
452
|
+
const existing = found && found.fingerprint === (origin.fingerprint ?? null) ? await store.runs.get(found.runId) : undefined;
|
|
453
|
+
if (existing?.agentId === entry.agent.id)
|
|
454
|
+
return existing;
|
|
455
|
+
}
|
|
456
|
+
await reconcileProjectCheckpoints(store, entry.agent.id);
|
|
457
|
+
const current = await getAgent(store, entry.agent.id);
|
|
458
|
+
if (!current)
|
|
459
|
+
throw new ApiError("not_found", "The agent no longer exists.");
|
|
460
|
+
// The body still pins any explicitly requested revision; refresh the current pointer used by admission's CAS.
|
|
461
|
+
entry = current;
|
|
462
|
+
await requireSavedProject(store, entry.agent.id);
|
|
463
|
+
}
|
|
452
464
|
if (entry.revision?.body?.cli_learning)
|
|
453
465
|
throw new ApiError("agent_not_runnable", "Start the next prompt in your existing CLI session. AgentRun records its learning; it does not launch this connection.");
|
|
454
466
|
if (entry.revision?.body?.sdk)
|
|
455
467
|
throw new ApiError("agent_not_runnable", "This agent runs in your backend SDK. Start it through its SDK learning wrapper; AgentRun does not launch its tools or credentials.");
|
|
456
|
-
const { launch, revision } = await prepareLaunch(deps, entry, body, { ...(trigger ? { trigger } : {}), capped, ...(from ? { from } : {}), ...(title !== undefined ? { title } : {}) });
|
|
468
|
+
const { launch, revision } = await prepareLaunch(deps, entry, body, { ...(trigger ? { trigger } : {}), capped, ...(from ? { from } : {}), ...(title !== undefined ? { title } : {}), ...(keys ? { keys } : {}) });
|
|
457
469
|
keyedOrRefused(deps, launch, revision.kind, sealed);
|
|
458
470
|
const runId = (deps.newRunId ?? uuidv7)();
|
|
459
471
|
try {
|
|
460
472
|
const labels = labelsOf(body.labels);
|
|
461
|
-
return await withHeldFiles(deps, runId, entry.agent.id, launch, (launch) => store.runs.create({ id: runId, agentId: entry.agent.id, revisionId: revision.id, ...(origin ? { origin } : {}), ...(labels ? { labels } : {}) }, { body: launchBody(launch), ...(sealed ? { sealed: sealed(runId, launch, revision.kind) } : {}) }));
|
|
473
|
+
return await withHeldFiles(store, deps, runId, entry.agent.id, revision, launch, (launch) => store.runs.create({ id: runId, agentId: entry.agent.id, revisionId: revision.id, expectedCurrent: entry.agent.currentRevision, ...(origin ? { origin } : {}), ...(labels ? { labels } : {}) }, { body: launchBody(launch), ...(sealed ? { sealed: sealed(runId, launch, revision.kind) } : {}) }));
|
|
462
474
|
}
|
|
463
475
|
catch (error) {
|
|
476
|
+
if (error instanceof RevisionMoved)
|
|
477
|
+
throw new ApiError("precondition_failed", "The agent changed while this run was prepared. Start again using its current setup.");
|
|
464
478
|
// Only another writer of the store got here first under the same origin key: its run is the one asked for.
|
|
465
479
|
if (origin && error instanceof StoreConstraint) {
|
|
466
480
|
const existing = await store.runOfOrigin(origin.key);
|
|
@@ -482,7 +496,95 @@ function keyedOrRefused(deps, launch, kind, sealed) {
|
|
|
482
496
|
* hold that fails part way, a write that fails, or one that answers another run (an origin key's run started before),
|
|
483
497
|
* lets go of the hold. In Mode 2 they are placed in the run's folder instead (StartDeps.files.place), and the run is
|
|
484
498
|
* written with the launch that says so; the same failures undo the placing. */
|
|
485
|
-
async function withHeldFiles(deps, runId, agentId, launch, write) {
|
|
499
|
+
export async function withHeldFiles(store, deps, runId, agentId, revision, launch, write) {
|
|
500
|
+
const source = sourceSyncOf(revision.body);
|
|
501
|
+
if (!source)
|
|
502
|
+
return withUploads(deps, runId, agentId, launch, write);
|
|
503
|
+
if (!deps.project)
|
|
504
|
+
throw new ApiError("agent_not_runnable", "This project must be prepared before its run can start.");
|
|
505
|
+
const ownership = await deps.project.hold?.({ runId, agentId });
|
|
506
|
+
let projectSettled = false;
|
|
507
|
+
const preparationOwnership = ownership ? { settled: async () => { projectSettled = true; } } : undefined;
|
|
508
|
+
const undos = [];
|
|
509
|
+
const undo = async (settled = false) => {
|
|
510
|
+
let complete = true;
|
|
511
|
+
for (const release of [...undos].reverse())
|
|
512
|
+
try {
|
|
513
|
+
await release();
|
|
514
|
+
}
|
|
515
|
+
catch {
|
|
516
|
+
complete = false;
|
|
517
|
+
}
|
|
518
|
+
if ((settled || projectSettled) && complete)
|
|
519
|
+
await ownership?.settled();
|
|
520
|
+
};
|
|
521
|
+
let prepared = launch, admitted;
|
|
522
|
+
try {
|
|
523
|
+
const ids = launch.files ?? [];
|
|
524
|
+
if (ids.length && deps.files) {
|
|
525
|
+
const uploaded = await deps.files.place?.(runId, agentId, prepared);
|
|
526
|
+
if (uploaded) {
|
|
527
|
+
prepared = uploaded.launch;
|
|
528
|
+
undos.push(() => uploaded.undo());
|
|
529
|
+
}
|
|
530
|
+
else {
|
|
531
|
+
undos.push(() => deps.files.release(runId));
|
|
532
|
+
await deps.files.hold(runId, ids);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
const placed = await deps.project.place({ runId, agentId, revision, launch: prepared, ...(preparationOwnership ? { hold: preparationOwnership } : {}) });
|
|
536
|
+
if (!placed)
|
|
537
|
+
throw new ApiError("agent_not_runnable", "This project has no completed placement for its run.");
|
|
538
|
+
undos.push(() => placed.undo());
|
|
539
|
+
admitted = placed.admitted;
|
|
540
|
+
prepared = placed.launch;
|
|
541
|
+
const receipt = sourceProjectOf(prepared.sourceProject);
|
|
542
|
+
if (!receipt || receipt.sourceId !== source.id || receipt.digest !== source.project || receipt.baselineRevision !== revision.id)
|
|
543
|
+
throw new ApiError("agent_not_runnable", "The prepared project does not match this run's pinned revision.");
|
|
544
|
+
}
|
|
545
|
+
catch (error) {
|
|
546
|
+
await undo();
|
|
547
|
+
throw error;
|
|
548
|
+
}
|
|
549
|
+
try {
|
|
550
|
+
const row = await write(prepared);
|
|
551
|
+
if (row.id !== runId)
|
|
552
|
+
await undo(true);
|
|
553
|
+
else {
|
|
554
|
+
if (ownership)
|
|
555
|
+
await ownership.settled();
|
|
556
|
+
else
|
|
557
|
+
await admitted?.();
|
|
558
|
+
}
|
|
559
|
+
return row;
|
|
560
|
+
}
|
|
561
|
+
catch (error) {
|
|
562
|
+
// A transport/COMMIT answer can be lost after admission. Never erase a runner's files on that evidence. Even a
|
|
563
|
+
// successful absent-row read does not prove an in-flight commit cannot land later: unknown admissions stay retained.
|
|
564
|
+
let knownAbsent = false;
|
|
565
|
+
try {
|
|
566
|
+
const row = await store.runs.get(runId);
|
|
567
|
+
if (row) {
|
|
568
|
+
const kept = await store.runs.launch(runId);
|
|
569
|
+
if (row.agentId === agentId && row.revisionId === revision.id && kept && canonical(kept.body) === canonical(launchBody(prepared))) {
|
|
570
|
+
if (ownership)
|
|
571
|
+
await ownership.settled();
|
|
572
|
+
else
|
|
573
|
+
await admitted?.();
|
|
574
|
+
return row;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
else
|
|
578
|
+
knownAbsent = true;
|
|
579
|
+
}
|
|
580
|
+
catch { /* preserve preparation until the admission can be resolved */ }
|
|
581
|
+
if (knownAbsent && (error instanceof StoreConstraint || error instanceof RevisionMoved || error instanceof AgentDeleting
|
|
582
|
+
|| error instanceof AgentPurged || error instanceof AgentArchived || error instanceof ExternalBodyConflict || error instanceof ApiError))
|
|
583
|
+
await undo(true);
|
|
584
|
+
throw error;
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
async function withUploads(deps, runId, agentId, launch, write) {
|
|
486
588
|
const ids = launch.files ?? [];
|
|
487
589
|
if (!ids.length || !deps.files)
|
|
488
590
|
return await write(launch);
|
|
@@ -546,7 +648,7 @@ export async function startInline(store, deps, inline, body, options = {}) {
|
|
|
546
648
|
}
|
|
547
649
|
/** The agent id each reference's first starts in progress share, and a failed first start's kept (startInline). */
|
|
548
650
|
const tentativeAgents = new Map();
|
|
549
|
-
async function startInlineOnce(store, deps, inline, body, { origin, sealed }, newAgentId) {
|
|
651
|
+
async function startInlineOnce(store, deps, inline, body, { origin, sealed, keys }, newAgentId) {
|
|
550
652
|
const { external, revision, stored: storedRevision } = inline;
|
|
551
653
|
const ref = externalKey(external);
|
|
552
654
|
// A version's body never changes here: a different one is refused before anything else is read or written.
|
|
@@ -570,12 +672,12 @@ async function startInlineOnce(store, deps, inline, body, { origin, sealed }, ne
|
|
|
570
672
|
};
|
|
571
673
|
// A store revision is the revision the run path reads as a pi Revision (its body is the revision body), as pinnedOf hands
|
|
572
674
|
// one across.
|
|
573
|
-
const { launch } = await prepareLaunch(deps, entry, body, { revision: (storedRevision ?? revision) });
|
|
675
|
+
const { launch } = await prepareLaunch(deps, entry, body, { revision: (storedRevision ?? revision), ...(keys ? { keys } : {}) });
|
|
574
676
|
keyedOrRefused(deps, launch, (storedRevision ?? revision).kind, sealed);
|
|
575
677
|
const runId = (deps.newRunId ?? uuidv7)();
|
|
576
678
|
const labels = labelsOf(body.labels);
|
|
577
679
|
try {
|
|
578
|
-
return await withHeldFiles(deps, runId, entry.agent.id, launch, (launch) => store.runs.createInline({
|
|
680
|
+
return await withHeldFiles(store, deps, runId, entry.agent.id, revision, launch, (launch) => store.runs.createInline({
|
|
579
681
|
agent: { id: entry.agent.id, name: inline.name, description: inline.description, ref },
|
|
580
682
|
revision: { id: revision.id, version: external.version, kind: revision.kind, body: revision.body, skills: revision.skills, ...(revision.note !== undefined ? { note: revision.note } : {}), ...(revision.mcpPin !== undefined ? { mcpPin: revision.mcpPin } : {}), ...(revision.monidPin !== undefined ? { monidPin: revision.monidPin } : {}) },
|
|
581
683
|
run: { id: runId, ...(origin ? { origin } : {}), ...(labels ? { labels } : {}) },
|
|
@@ -600,7 +702,7 @@ async function startInlineOnce(store, deps, inline, body, { origin, sealed }, ne
|
|
|
600
702
|
* caller INSERTs the run with it (startRun; an inline agent's run, which writes its agent and revision in the same
|
|
601
703
|
* transaction). `revision`: the revision the run starts on, given (an inline agent's, not yet stored), in place of the
|
|
602
704
|
* one the body pins or the agent's current (deps.pinnedOf). */
|
|
603
|
-
export async function prepareLaunch(deps, entry, body, { trigger, capped = false, from, title: given, revision: onRevision } = {}) {
|
|
705
|
+
export async function prepareLaunch(deps, entry, body, { trigger, capped = false, from, title: given, revision: onRevision, keys } = {}) {
|
|
604
706
|
const pinnedOf = onRevision ? async () => ({ revision: onRevision, version: entry.version }) : () => deps.pinnedOf(entry, body);
|
|
605
707
|
const issues = deps.runBodyIssues(body);
|
|
606
708
|
if (issues.length) {
|
|
@@ -630,6 +732,11 @@ export async function prepareLaunch(deps, entry, body, { trigger, capped = false
|
|
|
630
732
|
const wrong = await deps.inputIssuesOf(revision, input);
|
|
631
733
|
if (wrong.length)
|
|
632
734
|
throw deps.told(invalid(await deps.explainRun(wrong, body, revision)));
|
|
735
|
+
// The tool keys the run carries (run-keys.ts), by name: each one a tool of its revision reads, or its template declared.
|
|
736
|
+
const carried = keys ?? [];
|
|
737
|
+
const undeclared = carried.length ? undeclaredKeys(carried, await declaredKeys(revision)) : [];
|
|
738
|
+
if (undeclared.length)
|
|
739
|
+
throw invalid(undeclared);
|
|
633
740
|
// The run's own cap, else the agent's default: the caller chose its own.
|
|
634
741
|
const cap = typeof body.max_cost_usd === "number" ? body.max_cost_usd : rbody.maxCostUsd;
|
|
635
742
|
if (capped && cap === undefined)
|
|
@@ -642,7 +749,7 @@ export async function prepareLaunch(deps, entry, body, { trigger, capped = false
|
|
|
642
749
|
const place = resolvePlace(rbody.place, deps.fallbackPlace(revision.kind, revision.kind === "agentic" ? (rbody.tools ?? []) : []));
|
|
643
750
|
// Checked as pinned: an earlier revision with no pin is pinned now, once (StartDeps.pinFirst); one that could not be, as
|
|
644
751
|
// its server could not be listed, is refused with the rest (runnable).
|
|
645
|
-
await deps.runnable(deps.pinFirst ? await deps.pinFirst(revision) : revision, place, { capped: cap !== undefined });
|
|
752
|
+
await deps.runnable(deps.pinFirst ? await deps.pinFirst(revision) : revision, place, { capped: cap !== undefined, carried });
|
|
646
753
|
// A held computer (computer-held.ts): only one that speaks the protocol, its kind as the claim reads it (the
|
|
647
754
|
// revision's computer, else its place's).
|
|
648
755
|
const hold = holdOf(body, (settingOfDefinition(rbody)?.computer ?? computerOfPlace(place)).kind);
|
|
@@ -657,9 +764,10 @@ export async function prepareLaunch(deps, entry, body, { trigger, capped = false
|
|
|
657
764
|
// A starter's title (a trigger's "<Source> <name>") is shown words too: cut as the input is, then as every title is below.
|
|
658
765
|
const title = given !== undefined ? String(redactedOutput(given, deps.serverSecrets())) : revision.kind === "workflow" ? deps.workflowTitle(entry.agent.name || entry.agent.id, isObject(shown) ? shown : {}) : typeof input.content === "string" ? input.content : (Object.values(input).find((v) => typeof v === "string") ?? JSON.stringify(input));
|
|
659
766
|
// Its tools' providers, resolved before the run exists (tool-plane.ts) and kept in its launch body, so a claim reads them
|
|
660
|
-
// back, never the settings of that moment. (The old start wrote them to serve.json; now they ride the launch row.)
|
|
767
|
+
// back, never the settings of that moment. (The old start wrote them to serve.json; now they ride the launch row.) A key
|
|
768
|
+
// the run carries resolves a tool as one the host holds would.
|
|
661
769
|
const tooling = await toolingOf(revision);
|
|
662
|
-
const plane = tooling ? planeFor(deps.planeEnv(), tooling, deps.toolSettings(), { capped: cap !== undefined, run: true }) : {};
|
|
770
|
+
const plane = tooling ? planeFor(withCarried(deps.planeEnv(), carried), tooling, deps.toolSettings(), { capped: cap !== undefined, run: true }) : {};
|
|
663
771
|
// The run's model, resolved once, at start (the runner reads launch.model only).
|
|
664
772
|
const model = await deps.modelOf(entry, revision);
|
|
665
773
|
// The run's Archil disk, chosen now and kept in its launch (run-launch.ts archilDisk): the one the body names (a disk per
|
package/dist/sdk/runs.d.ts
CHANGED
|
@@ -64,6 +64,9 @@ export type SdkInbox = {
|
|
|
64
64
|
}[];
|
|
65
65
|
/** Exact promoted executable workflows delivered by the start receipt. */
|
|
66
66
|
learning_workflows?: LearnedWorkflow[];
|
|
67
|
+
/** The learning commit each of learning_workflows was read at, by name (its seed's, or its champion record's): where
|
|
68
|
+
* the receipt asks whether a purged run wrote it (receipt). */
|
|
69
|
+
learning_commits?: Record<string, string>;
|
|
67
70
|
/** A compiled draft reserved for a shadow check, never supplied to the native agent. */
|
|
68
71
|
learning_check?: SdkWorkflowCheck;
|
|
69
72
|
/** Server cleaning changed evidence: two different secret-bearing answers must never agree by their placeholder. */
|
|
@@ -76,10 +79,12 @@ export type SdkInbox = {
|
|
|
76
79
|
};
|
|
77
80
|
/** The settings key an SDK run's record is kept under (the run's delete with its agent's: agent-delete.ts). */
|
|
78
81
|
export declare const sdkRunKey: (id: string) => string;
|
|
82
|
+
export declare const SDK_MAX_OPEN_MS = 86400000;
|
|
79
83
|
export declare const sdkOf: (body: unknown) => SdkBinding | undefined;
|
|
80
84
|
export declare function sdkInbox(store: Pick<Store, "settings">, runId: string): Promise<SdkInbox>;
|
|
81
85
|
/** Completion and closure compete on the same inbox. A stale runner cannot close it, and a completion that won
|
|
82
|
-
* remains available to the next claim even if this one cannot read or journal it. No external SDK call is replayed.
|
|
86
|
+
* remains available to the next claim even if this one cannot read or journal it. No external SDK call is replayed.
|
|
87
|
+
* An expiry is checked against the row it replaces: an events batch that moved the deadline keeps the inbox open. */
|
|
83
88
|
export declare function closeSdkInbox(store: Pick<Store, "settings">, claim: Claim, reason: NonNullable<SdkInbox["closed"]>): Promise<SdkInbox>;
|
|
84
89
|
/** The key a server signs its SDK runs' credentials with: derived from its seal key (seal-key.ts), which stays the
|
|
85
90
|
* store's for its life, so adding, reordering or removing an API token (api-token.ts) never changes a run's credential. */
|
|
@@ -126,6 +131,11 @@ export declare class SdkRuns {
|
|
|
126
131
|
workflows: LearnedWorkflow[];
|
|
127
132
|
};
|
|
128
133
|
}>;
|
|
134
|
+
/** The purged run (purged-workflow.ts) that wrote a workflow of `pinned`, each read at the learning commit a start took
|
|
135
|
+
* its text from. One read-only exec per commit. */
|
|
136
|
+
private purgedPinned;
|
|
137
|
+
/** The workflows a start delivers and the learning commit each was read at (`commits`: the seed's, or its champion
|
|
138
|
+
* record's), and the drafts a check may take. */
|
|
129
139
|
private selectedWorkflows;
|
|
130
140
|
private modify;
|
|
131
141
|
private writable;
|
package/dist/sdk/runs.js
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
// Backend SDK calls remain in their application. This inbox only records their evidence and immutable completion;
|
|
2
2
|
// the ordinary fenced runner owns journals, reflection, checking and learning commits.
|
|
3
|
+
import { cliCaptureOf } from "../source-sync/capture.js";
|
|
4
|
+
import { sourceKey } from "../source-sync/registry.js";
|
|
3
5
|
import { createHash, createHmac, timingSafeEqual } from "node:crypto";
|
|
4
6
|
import { inspectWorkflow, resolveSchemaForWorkflow } from "@parcha/agentrun-dsl";
|
|
5
|
-
import { ApiError } from "../errors.js";
|
|
7
|
+
import { ApiError, invalid } from "../errors.js";
|
|
6
8
|
import { canonical, isObject, cliLearningOf, learningSettingForBody } from "../store/agent-body.js";
|
|
7
|
-
import { uuidv7 } from "../
|
|
9
|
+
import { uuidv7 } from "../ids.js";
|
|
8
10
|
import { launchBody } from "../run-launch.js";
|
|
9
11
|
import { learningStartFor } from "../runs-over-store.js";
|
|
12
|
+
import { labelsIssues, labelsOf } from "../run-labels.js";
|
|
10
13
|
import { chooseWorkflows, learningFolderOf, seedAt, secretsShown, withAgentRepo } from "../learning.js";
|
|
11
14
|
import { INSTRUCTION_EVIDENCE_BYTES } from "../native/instructions.js";
|
|
12
15
|
import { CLI_POLICY_BYTES } from "../cli-learning/policy.js";
|
|
@@ -14,10 +17,12 @@ import { refuseWhileNoRunner } from "../runner/presence.js";
|
|
|
14
17
|
import { secretRedactor } from "../runner/redact.js";
|
|
15
18
|
import { redactedOutput } from "../key-refusal.js";
|
|
16
19
|
import { decodedStrings, draftProblems } from "../learn/workflow-draft.js";
|
|
20
|
+
import { snapshotAt } from "../learn/folder.js";
|
|
21
|
+
import { purgedRun, purgedWorkflows } from "../purged-workflow.js";
|
|
17
22
|
import { sdkWorkflowManifest, sdkDraftTools } from "./workflows.js";
|
|
18
23
|
import { promotionOf, promotionsOf, resetAtOf } from "../learn/promote.js";
|
|
19
24
|
import { championTextOf, releaseSdkCheck, reserveSdkCheck } from "../runner/promotion.js";
|
|
20
|
-
import {
|
|
25
|
+
import { sdkTakes, workflowDigest } from "../runner/workflow-first.js";
|
|
21
26
|
import { SDK_WORKFLOW_RUNTIMES, sdkBindingOf } from "./runtimes.js";
|
|
22
27
|
export { SDK_RUNTIMES, isSdkRuntime } from "./runtimes.js";
|
|
23
28
|
/** Claude's begin snapshot is provisional; only its immutable same-window completion can establish policy. */
|
|
@@ -30,6 +35,13 @@ export function cliPolicyOf(launch, completion) {
|
|
|
30
35
|
const keyOf = (id) => `sdk-run:${id}`;
|
|
31
36
|
/** The settings key an SDK run's record is kept under (the run's delete with its agent's: agent-delete.ts). */
|
|
32
37
|
export const sdkRunKey = keyOf;
|
|
38
|
+
/** An inbox takes evidence until an hour after its start or its latest events batch, and never past a day after its start. */
|
|
39
|
+
const SDK_OPEN_MS = 3_600_000;
|
|
40
|
+
export const SDK_MAX_OPEN_MS = 86_400_000;
|
|
41
|
+
/** A start's `started_at`: at most this far ahead of the server's clock, at most this far behind it. */
|
|
42
|
+
const STARTED_SKEW_MS = 60_000, STARTED_BACK_MS = 7 * 86_400_000;
|
|
43
|
+
const TITLE_LIMIT = 200;
|
|
44
|
+
const RFC3339 = /^(\d{4})-(\d{2})-(\d{2})[Tt](\d{2}):(\d{2}):(\d{2})(\.\d+)?(?:[Zz]|([+-])(\d{2}):(\d{2}))$/;
|
|
33
45
|
const TEXT_LIMIT = 128_000, BATCH_LIMIT = 256_000, TOTAL_LIMIT = 4_000_000;
|
|
34
46
|
// Always leave enough room for bounded completion after an evidence upload reaches its limit.
|
|
35
47
|
const EVIDENCE_LIMIT = TOTAL_LIMIT - 600_000;
|
|
@@ -61,6 +73,31 @@ const usageOf = (value) => {
|
|
|
61
73
|
}
|
|
62
74
|
return result;
|
|
63
75
|
};
|
|
76
|
+
/** An RFC 3339 date-time as epoch ms, or undefined when it is not one (a field out of range included). */
|
|
77
|
+
function rfc3339(value) {
|
|
78
|
+
const m = RFC3339.exec(value);
|
|
79
|
+
if (!m)
|
|
80
|
+
return undefined;
|
|
81
|
+
const [y, mo, d, h, mi, s] = m.slice(1, 7).map(Number);
|
|
82
|
+
const day = new Date(Date.UTC(y, mo - 1, d));
|
|
83
|
+
if (day.getUTCFullYear() !== y || day.getUTCMonth() !== mo - 1 || day.getUTCDate() !== d || h > 23 || mi > 59 || s > 59)
|
|
84
|
+
return undefined;
|
|
85
|
+
if (m[8] && (Number(m[9]) > 23 || Number(m[10]) > 59))
|
|
86
|
+
return undefined;
|
|
87
|
+
const offset = m[8] ? (m[8] === "-" ? -1 : 1) * (Number(m[9]) * 60 + Number(m[10])) * 60_000 : 0;
|
|
88
|
+
return Date.UTC(y, mo - 1, d, h, mi, s, Math.floor(Number(`0${m[7] ?? ""}`) * 1000)) - offset;
|
|
89
|
+
}
|
|
90
|
+
/** A start's `started_at` as epoch ms: its form only, refused when it is not an RFC 3339 date-time. */
|
|
91
|
+
const rfc3339Of = (value) => (typeof value === "string" ? rfc3339(value) : undefined) ?? refuse("started_at must be an RFC 3339 date-time, such as 2026-01-02T03:04:05Z.");
|
|
92
|
+
/** A new start's `started_at` against the server's clock at `now`: when the backend began the task, if before this request.
|
|
93
|
+
* The run's `created_at` takes it. Never applied to a retry of a start already made (its receipt is returned first). */
|
|
94
|
+
const startedWithin = (at, now) => {
|
|
95
|
+
if (at > now + STARTED_SKEW_MS)
|
|
96
|
+
refuse("started_at must not be in the future.");
|
|
97
|
+
if (at < now - STARTED_BACK_MS)
|
|
98
|
+
refuse("started_at must be at most 7 days ago.");
|
|
99
|
+
return Math.min(at, now);
|
|
100
|
+
};
|
|
64
101
|
export const sdkOf = (body) => isObject(body) ? sdkBindingOf(body.sdk) : undefined;
|
|
65
102
|
export async function sdkInbox(store, runId) {
|
|
66
103
|
const row = await store.settings.get(keyOf(runId));
|
|
@@ -69,12 +106,15 @@ export async function sdkInbox(store, runId) {
|
|
|
69
106
|
return row;
|
|
70
107
|
}
|
|
71
108
|
/** Completion and closure compete on the same inbox. A stale runner cannot close it, and a completion that won
|
|
72
|
-
* remains available to the next claim even if this one cannot read or journal it. No external SDK call is replayed.
|
|
109
|
+
* remains available to the next claim even if this one cannot read or journal it. No external SDK call is replayed.
|
|
110
|
+
* An expiry is checked against the row it replaces: an events batch that moved the deadline keeps the inbox open. */
|
|
73
111
|
export async function closeSdkInbox(store, claim, reason) {
|
|
74
112
|
for (let retry = 0; retry < 20; retry++) {
|
|
75
113
|
const row = await sdkInbox(store, claim.runId);
|
|
76
114
|
if (row.completion || row.closed)
|
|
77
115
|
return row;
|
|
116
|
+
if (reason === "expired" && row.expires_at >= Date.now())
|
|
117
|
+
return row;
|
|
78
118
|
const next = { ...row, closed: reason };
|
|
79
119
|
if (await store.settings.replaceIf(keyOf(claim.runId), row, next, claim))
|
|
80
120
|
return next;
|
|
@@ -175,13 +215,22 @@ export class SdkRuns {
|
|
|
175
215
|
async start(entry, body, deps, origin, compile, consoleOrigin, cli) {
|
|
176
216
|
if (this.lane !== (cli ? "cli" : "sdk"))
|
|
177
217
|
refuse("The run must use its enrolled credential lane.");
|
|
178
|
-
only(body, cli ? ["runtime", "input", "instructions", "model"] : ["runtime", "platform", "input", "instructions", "model", "workflow_tools", "workflow_adapters"]);
|
|
218
|
+
only(body, cli ? ["runtime", "input", "instructions", "model"] : ["runtime", "platform", "input", "instructions", "model", "workflow_tools", "workflow_adapters", "started_at", "title", "labels"]);
|
|
179
219
|
const source = cli ? undefined : sdkBindingOf(body) ?? refuse("The SDK source needs a supported runtime; external requires a platform label of 1–80 characters without surrounding whitespace, controls or lone surrogates. Other runtimes do not take platform.");
|
|
180
220
|
if (source && !SDK_WORKFLOW_RUNTIMES.includes(source.runtime) && (body.workflow_tools !== undefined || body.workflow_adapters !== undefined))
|
|
181
221
|
refuse("External runs receive owner corrections; workflow_tools and workflow_adapters require a backend workflow bridge.");
|
|
182
222
|
const workflow = cli ? undefined : sdkWorkflowManifest(body.workflow_tools, body.workflow_adapters);
|
|
183
223
|
if (workflow && canonical(this.clean(workflow)) !== canonical(workflow))
|
|
184
224
|
refuse("The workflow manifest contains a protected value; keep credentials in the backend.");
|
|
225
|
+
const expectedCurrent = entry.agent.currentRevision;
|
|
226
|
+
const managedLearning = learningSettingForBody(entry.revision?.body);
|
|
227
|
+
const capture = cli ? await cliCaptureOf(this.store, entry) : undefined;
|
|
228
|
+
const sourceGuards = cli ? [{ key: `cli-learning-connection:${cli.connection_id}`, fields: { active: true, generation: cli.connection_generation, agent_id: entry.agent.id } }] : [];
|
|
229
|
+
if (capture) {
|
|
230
|
+
entry = { ...entry, revision: capture.revision };
|
|
231
|
+
if (capture.source)
|
|
232
|
+
sourceGuards.push({ key: sourceKey(entry.agent.id), fields: { id: capture.source.id, capture_revision: capture.source.capture_revision } });
|
|
233
|
+
}
|
|
185
234
|
const binding = sdkOf(entry.revision?.body);
|
|
186
235
|
if (cli) {
|
|
187
236
|
if (cliLearningOf(entry.revision?.body)?.connection_id !== cli.connection_id)
|
|
@@ -198,6 +247,18 @@ export class SdkRuns {
|
|
|
198
247
|
throw new ApiError("agent_archived", "This SDK agent is archived.");
|
|
199
248
|
const input = text(body.input, "input"), instructions = text(body.instructions, "instructions", cli ? CLI_POLICY_BYTES : TEXT_LIMIT);
|
|
200
249
|
const model = body.model === undefined ? undefined : text(body.model, "model", 256);
|
|
250
|
+
// Its form is checked now; its distance from the clock only for a new start, below: a retry of a start that was made
|
|
251
|
+
// (its origin and fingerprint kept) gets its receipt however late it comes.
|
|
252
|
+
const reportedAt = body.started_at === undefined ? undefined : rfc3339Of(body.started_at);
|
|
253
|
+
const title = body.title === undefined ? undefined : text(body.title, "title").trim().replace(/\s+/g, " ");
|
|
254
|
+
if (title !== undefined && ([...title].length < 1 || [...title].length > TITLE_LIMIT))
|
|
255
|
+
refuse(`title must be text of 1 to ${TITLE_LIMIT} characters.`);
|
|
256
|
+
const labelIssues = labelsIssues(body.labels);
|
|
257
|
+
if (labelIssues.length)
|
|
258
|
+
throw invalid(labelIssues);
|
|
259
|
+
const labels = labelsOf(body.labels);
|
|
260
|
+
if (labels && canonical(this.clean(labels)) !== canonical(labels))
|
|
261
|
+
refuse("A label contains a protected value; keep credentials in the backend.");
|
|
201
262
|
if (origin) {
|
|
202
263
|
const known = await this.store.runOfOrigin(origin.key);
|
|
203
264
|
if (known) {
|
|
@@ -206,6 +267,7 @@ export class SdkRuns {
|
|
|
206
267
|
return this.receipt(known.runId, consoleOrigin);
|
|
207
268
|
}
|
|
208
269
|
}
|
|
270
|
+
const startedAt = reportedAt === undefined ? undefined : startedWithin(reportedAt, Date.now());
|
|
209
271
|
const cleanedInput = this.clean({ input }).input;
|
|
210
272
|
const pinned = await deps.pinnedOf(entry, {});
|
|
211
273
|
if (!pinned)
|
|
@@ -217,7 +279,7 @@ export class SdkRuns {
|
|
|
217
279
|
// their words, as any start is (prepareLaunch), before the caller holds a receipt it would run on.
|
|
218
280
|
await refuseWhileNoRunner(this.store.runners);
|
|
219
281
|
// A connected agent's run names no disk.
|
|
220
|
-
const learning = await learningStartFor(deps, entry, pinned.revision, { ...revisionBody, learning: learningSettingForBody(revisionBody) }, {
|
|
282
|
+
const learning = await learningStartFor(deps, entry, pinned.revision, { ...revisionBody, learning: cli ? managedLearning : learningSettingForBody(revisionBody) }, {
|
|
221
283
|
disk: null,
|
|
222
284
|
noWorkflows: !workflow,
|
|
223
285
|
...(compile.model ? { model: compile.model } : {}),
|
|
@@ -239,27 +301,20 @@ export class SdkRuns {
|
|
|
239
301
|
const reset = learning ? await resetAtOf(this.store.settings, entry.agent.id) : undefined;
|
|
240
302
|
if (learning && reset !== undefined && !(learning.seededAt !== undefined && learning.seededAt > reset))
|
|
241
303
|
throw new ApiError("unavailable", "The agent's learning was reset while this run started: start it again.", { retry_after_ms: 1_000 });
|
|
242
|
-
const selected = learning && !learning.noWorkflows && workflow ? await this.selectedWorkflows(entry.agent.id, learning.seed, workflow) : { workflows: [], candidates: [] };
|
|
304
|
+
const selected = learning && !learning.noWorkflows && workflow ? await this.selectedWorkflows(entry.agent.id, learning.seed, workflow) : { workflows: [], commits: {}, candidates: [] };
|
|
243
305
|
const cleaned = this.clean({ instructions, ...(model ? { model } : {}) });
|
|
244
306
|
const runId = uuidv7();
|
|
245
307
|
const redacted = cleanedInput !== input || cleaned.instructions !== instructions;
|
|
246
|
-
let task;
|
|
247
|
-
try {
|
|
248
|
-
task = JSON.parse(input);
|
|
249
|
-
}
|
|
250
|
-
catch { /* A prose task: the client resolves the draft's input from the turn. */ }
|
|
251
308
|
// A turn is a check only when it can be one's baseline: no protected value in its task or policy, and an input the
|
|
252
|
-
// draft takes. A
|
|
253
|
-
|
|
254
|
-
// cannot resolve is given back (releaseSdkCheck), never counted.
|
|
255
|
-
const takes = (flow) => !redacted && (!isObject(task) || fits(flow, task, resolveSchemaForWorkflow));
|
|
309
|
+
// draft takes (sdkTakes). A prose turn whose input the client cannot resolve is given back (releaseSdkCheck).
|
|
310
|
+
const takes = (flow) => !redacted && sdkTakes(flow, input, resolveSchemaForWorkflow);
|
|
256
311
|
const checkDeps = { store: this.store, agentId: entry.agent.id, log: () => { }, shown: async () => secretsShown(this.secrets(), { local: true }) };
|
|
257
312
|
const check = learning ? await reserveSdkCheck(checkDeps, { runId, revision: learning.seed, learning: revisionBody.learning, flows: selected.candidates, takes }) : undefined;
|
|
258
313
|
// A shadow check needs the original agent's own answer, not an answer produced by its existing champion.
|
|
259
314
|
const workflows = check ? [] : selected.workflows;
|
|
260
315
|
const launch = {
|
|
261
316
|
input: { content: cleanedInput },
|
|
262
|
-
title: cleanedInput.trim().replace(/\s+/g, " ").slice(0, 80),
|
|
317
|
+
title: title === undefined ? cleanedInput.trim().replace(/\s+/g, " ").slice(0, 80) : this.clean({ title }).title,
|
|
263
318
|
place: { mode: "runner" },
|
|
264
319
|
...(cli ? {
|
|
265
320
|
harness: "cli",
|
|
@@ -275,14 +330,15 @@ export class SdkRuns {
|
|
|
275
330
|
await this.store.settings.put(keyOf(runId), {
|
|
276
331
|
run_id: runId,
|
|
277
332
|
created_at: now,
|
|
278
|
-
expires_at: now + (cli ?
|
|
333
|
+
expires_at: now + (cli ? SDK_MAX_OPEN_MS : SDK_OPEN_MS),
|
|
279
334
|
batches: [],
|
|
280
335
|
learning_workflows: workflows,
|
|
336
|
+
...(workflows.length ? { learning_commits: selected.commits } : {}),
|
|
281
337
|
...(check ? { learning_check: check } : {}),
|
|
282
338
|
...(redacted ? { check_redacted: true } : {}),
|
|
283
339
|
...(cli && cleanedInput !== input ? { evidence_incomplete: true } : {}),
|
|
284
340
|
});
|
|
285
|
-
await this.store.runs.create({ id: runId, agentId: entry.agent.id, revisionId: revision.id, ...(origin ? { origin } : {}) }, { body: launchBody(launch) });
|
|
341
|
+
await this.store.runs.create({ id: runId, agentId: entry.agent.id, revisionId: revision.id, ...(cli ? { expectedCurrent, settingsGuards: sourceGuards } : {}), ...(origin ? { origin } : {}), ...(labels ? { labels } : {}), ...(startedAt !== undefined ? { startedAt } : {}) }, { body: launchBody(launch) });
|
|
286
342
|
}
|
|
287
343
|
catch (error) {
|
|
288
344
|
await this.store.settings.remove(keyOf(runId));
|
|
@@ -321,6 +377,15 @@ export class SdkRuns {
|
|
|
321
377
|
const launch = (await this.store.runs.launch(runId)).body;
|
|
322
378
|
const inbox = await sdkInbox(this.store, runId);
|
|
323
379
|
const workflows = inbox.learning_workflows ?? [];
|
|
380
|
+
// A start sent again answers the receipt it pinned, never a workflow a purged run wrote: 410 purged, as its file read.
|
|
381
|
+
// Each is read at the commit it was taken from (learning_commits; the check's draft at the seed).
|
|
382
|
+
const seed = launch.learning?.seed ?? null;
|
|
383
|
+
const purged = seed === null ? undefined : await this.purgedPinned(run.agentId, [
|
|
384
|
+
...workflows.map((flow) => [flow, inbox.learning_commits?.[flow.name] ?? seed]),
|
|
385
|
+
...(inbox.learning_check ? [[inbox.learning_check.workflow, seed]] : []),
|
|
386
|
+
]);
|
|
387
|
+
if (purged)
|
|
388
|
+
throw purgedRun(purged);
|
|
324
389
|
const shown = secretsShown(this.secrets(), { local: true });
|
|
325
390
|
if (shown.holds(launch.learning?.section ?? "") ||
|
|
326
391
|
[...workflows, ...(inbox.learning_check ? [inbox.learning_check.workflow] : [])].some((flow) => {
|
|
@@ -342,10 +407,32 @@ export class SdkRuns {
|
|
|
342
407
|
},
|
|
343
408
|
};
|
|
344
409
|
}
|
|
410
|
+
/** The purged run (purged-workflow.ts) that wrote a workflow of `pinned`, each read at the learning commit a start took
|
|
411
|
+
* its text from. One read-only exec per commit. */
|
|
412
|
+
async purgedPinned(agentId, pinned) {
|
|
413
|
+
if (pinned.length === 0)
|
|
414
|
+
return undefined;
|
|
415
|
+
const folder = learningFolderOf(this.data, agentId);
|
|
416
|
+
try {
|
|
417
|
+
const reads = new Map();
|
|
418
|
+
const at = (commit) => reads.get(commit) ?? reads.set(commit, snapshotAt(folder.workspace, commit).then((snapshot) => purgedWorkflows(snapshot, this.store))).get(commit);
|
|
419
|
+
for (const [flow, commit] of pinned) {
|
|
420
|
+
const row = (await at(commit)).get(flow.name);
|
|
421
|
+
if (row)
|
|
422
|
+
return row;
|
|
423
|
+
}
|
|
424
|
+
return undefined;
|
|
425
|
+
}
|
|
426
|
+
finally {
|
|
427
|
+
await folder.end();
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
/** The workflows a start delivers and the learning commit each was read at (`commits`: the seed's, or its champion
|
|
431
|
+
* record's), and the drafts a check may take. */
|
|
345
432
|
async selectedWorkflows(agentId, revision, binding) {
|
|
346
433
|
const folder = learningFolderOf(this.data, agentId);
|
|
347
434
|
try {
|
|
348
|
-
const seed = await seedAt(folder.workspace, revision);
|
|
435
|
+
const seed = await seedAt(folder.workspace, revision, this.store);
|
|
349
436
|
const { flows } = chooseWorkflows(seed, secretsShown(this.secrets(), { local: true }));
|
|
350
437
|
const dsl = await import("@parcha/agentrun-dsl");
|
|
351
438
|
const { tools, gated, deniedBy } = sdkDraftTools(binding.workflow_tools);
|
|
@@ -358,16 +445,18 @@ export class SdkRuns {
|
|
|
358
445
|
room -= bytes;
|
|
359
446
|
return true;
|
|
360
447
|
};
|
|
361
|
-
const workflows = [], candidates = [];
|
|
362
|
-
const atCommit = async (name, commit) => chooseWorkflows(await seedAt(folder.workspace, commit), secretsShown(this.secrets(), { local: true })).flows.find((one) => one.name === name);
|
|
448
|
+
const workflows = [], candidates = [], commits = {};
|
|
449
|
+
const atCommit = async (name, commit) => chooseWorkflows(await seedAt(folder.workspace, commit, this.store), secretsShown(this.secrets(), { local: true })).flows.find((one) => one.name === name);
|
|
363
450
|
for (const flow of flows) {
|
|
364
|
-
const { text: serving } = await championTextOf(records, flow, atCommit);
|
|
365
|
-
if (serving && compatible(serving))
|
|
451
|
+
const { text: serving, champion } = await championTextOf(records, flow, atCommit);
|
|
452
|
+
if (serving && compatible(serving)) {
|
|
366
453
|
workflows.push(serving);
|
|
454
|
+
commits[serving.name] = serving === flow ? seed.sha : champion.commit;
|
|
455
|
+
}
|
|
367
456
|
if ((!serving || workflowDigest(serving) !== workflowDigest(flow)) && compatible(flow))
|
|
368
457
|
candidates.push(flow);
|
|
369
458
|
}
|
|
370
|
-
return { workflows, candidates };
|
|
459
|
+
return { workflows, commits, candidates };
|
|
371
460
|
}
|
|
372
461
|
finally {
|
|
373
462
|
await folder.end();
|
|
@@ -488,7 +577,9 @@ export class SdkRuns {
|
|
|
488
577
|
for (const check of checks)
|
|
489
578
|
if (!row.learning_check || check.id !== row.learning_check.id || check.digest !== row.learning_check.digest)
|
|
490
579
|
refuse("The workflow check must name this SDK run's reserved candidate.");
|
|
491
|
-
|
|
580
|
+
// Every accepted batch, an empty one too, keeps the inbox open another hour, up to a day after its start.
|
|
581
|
+
const expires = Math.max(row.expires_at, Math.min(Date.now() + SDK_OPEN_MS, row.created_at + SDK_MAX_OPEN_MS));
|
|
582
|
+
return { ...row, batches: [...row.batches, batch], expires_at: expires, ...(checkRedacted ? { check_redacted: true, ...(launch.cli_learning ? { evidence_incomplete: true } : {}) } : {}) };
|
|
492
583
|
}, EVIDENCE_LIMIT);
|
|
493
584
|
return { accepted: true };
|
|
494
585
|
}
|