anchor-ai-sdk 0.8.11 → 0.8.12
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 +45 -1
- package/dist/agents/index.d.ts +1 -0
- package/dist/agents/index.d.ts.map +1 -1
- package/dist/agents/index.js +1 -0
- package/dist/agents/index.js.map +1 -1
- package/dist/agents/types.d.ts +20 -0
- package/dist/agents/types.d.ts.map +1 -1
- package/dist/agents/workflow/index.d.ts +49 -0
- package/dist/agents/workflow/index.d.ts.map +1 -0
- package/dist/agents/workflow/index.js +46 -0
- package/dist/agents/workflow/index.js.map +1 -0
- package/dist/agents/workflow/prompt.d.ts +11 -0
- package/dist/agents/workflow/prompt.d.ts.map +1 -0
- package/dist/agents/workflow/prompt.js +25 -0
- package/dist/agents/workflow/prompt.js.map +1 -0
- package/dist/engine/loop.d.ts.map +1 -1
- package/dist/engine/loop.js +156 -16
- package/dist/engine/loop.js.map +1 -1
- package/dist/engine/types.d.ts +15 -0
- package/dist/engine/types.d.ts.map +1 -1
- package/dist/engine/workflow.d.ts +74 -0
- package/dist/engine/workflow.d.ts.map +1 -0
- package/dist/engine/workflow.js +125 -0
- package/dist/engine/workflow.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/schema.d.ts +64 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +166 -0
- package/dist/schema.js.map +1 -0
- package/dist/tools/finish.d.ts +15 -0
- package/dist/tools/finish.d.ts.map +1 -1
- package/dist/tools/finish.js +39 -12
- package/dist/tools/finish.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -576,6 +576,50 @@ Validate inputs and enforce permissions in your implementation.
|
|
|
576
576
|
**[Tools and schemas](https://anchor-sdk.dev/docs.html#tools)** ·
|
|
577
577
|
**[Runnable SDK examples](https://github.com/hrithikkoduri/anchor-ai-sdk/tree/main/examples)**
|
|
578
578
|
|
|
579
|
+
### Headless workflows: `workflowAgent`
|
|
580
|
+
|
|
581
|
+
For background jobs that never talk to a user — environment setup, migrations
|
|
582
|
+
— build a workflow agent. Nobody reads its prose and nobody answers its
|
|
583
|
+
questions, so the engine enforces the contract instead of the conversation:
|
|
584
|
+
|
|
585
|
+
```js
|
|
586
|
+
import { bashTool, editFileTool, readFileTool, runAgent, userTurn, workflowAgent } from "anchor-ai-sdk";
|
|
587
|
+
|
|
588
|
+
const environment = workflowAgent({
|
|
589
|
+
name: "environment",
|
|
590
|
+
brief: "Infer how to install and boot this app. Write the recipe to .sailor/run.sh and prove it boots.",
|
|
591
|
+
tools: [bashTool, readFileTool, editFileTool],
|
|
592
|
+
check: { command: "bash .sailor/run.sh --check", description: "the app boots and answers on its port" },
|
|
593
|
+
output: {
|
|
594
|
+
name: "recipe",
|
|
595
|
+
schema: {
|
|
596
|
+
type: "object",
|
|
597
|
+
properties: { install: { type: "string" }, start: { type: "string" }, port: { type: "integer" } },
|
|
598
|
+
required: ["install", "start", "port"],
|
|
599
|
+
additionalProperties: false,
|
|
600
|
+
},
|
|
601
|
+
},
|
|
602
|
+
budget: { maxTurns: 30 },
|
|
603
|
+
});
|
|
604
|
+
|
|
605
|
+
const result = await runAgent(userTurn("Repository: acme/web"), { llm, sandbox, agent: environment });
|
|
606
|
+
if (result.ok) console.log(result.output); // { install, start, port }, already validated
|
|
607
|
+
else console.log(result.reason); // e.g. "budget_exhausted"
|
|
608
|
+
```
|
|
609
|
+
|
|
610
|
+
- `finish` is the only exit, and it is accepted only when the latest `bash` run
|
|
611
|
+
of `check.command` in this run passed (exit 0, no timeout) and no file was
|
|
612
|
+
edited after it. The check must be the command's final step — a `cd dir &&`
|
|
613
|
+
prefix is fine; `|| true`, a pipe, or `echo` does not count. A rejected
|
|
614
|
+
finish, or a prose reply, comes back as a corrective message and the run continues.
|
|
615
|
+
- `output` (a JSON schema or a zod schema) is validated inside that gate and
|
|
616
|
+
returned as `result.output`.
|
|
617
|
+
- The conversational nudges (silent-tool, verbose-turn, prose checkpoint) are
|
|
618
|
+
off; the repeated-call, empty-turn, and capped-reply corrections stay on.
|
|
619
|
+
- Running out of any budget, `maxTurns` included, ends with `ok: false` and
|
|
620
|
+
`reason: "budget_exhausted"`.
|
|
621
|
+
- `ask_questions` is refused, and so is `EngineOptions.inbox`: a workflow run is one-shot.
|
|
622
|
+
|
|
579
623
|
## Configure the runtime, not another loop
|
|
580
624
|
|
|
581
625
|
| Capability | Your controls |
|
|
@@ -753,7 +797,7 @@ and an extraction failure, not the full benchmark or a general superiority claim
|
|
|
753
797
|
|
|
754
798
|
## Status and contributing
|
|
755
799
|
|
|
756
|
-
**Early alpha, 0.x. Source version: 0.8.
|
|
800
|
+
**Early alpha, 0.x. Source version: 0.8.12; latest published release: v0.8.12.** Pin exact versions and test your
|
|
757
801
|
integration. Provider and runtime features are version-specific; the current
|
|
758
802
|
source may be ahead of the installed package. Daytona, agent teams, context
|
|
759
803
|
reset, MCP, and consumer skill bundles remain roadmap work in the reviewed
|
package/dist/agents/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/agents/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,cAAc,YAAY,CAAC;AAC3B,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/agents/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,cAAc,YAAY,CAAC;AAC3B,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC"}
|
package/dist/agents/index.js
CHANGED
package/dist/agents/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/agents/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,cAAc,YAAY,CAAC;AAC3B,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/agents/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,cAAc,YAAY,CAAC;AAC3B,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC"}
|
package/dist/agents/types.d.ts
CHANGED
|
@@ -10,7 +10,10 @@
|
|
|
10
10
|
* same `strategy` + `tools` options it always took, and explicit options win
|
|
11
11
|
* per-field. Custom agents are just objects built from the same primitives.
|
|
12
12
|
*/
|
|
13
|
+
import type { BudgetLimits } from "../engine/budgets.js";
|
|
13
14
|
import type { AgentStrategy } from "../engine/strategy.js";
|
|
15
|
+
import type { GuardrailConfig } from "../engine/types.js";
|
|
16
|
+
import type { WorkflowContract } from "../engine/workflow.js";
|
|
14
17
|
import type { ReasoningEffort } from "../llm/types.js";
|
|
15
18
|
import type { Tool } from "../tools/types.js";
|
|
16
19
|
export interface Agent {
|
|
@@ -30,5 +33,22 @@ export interface Agent {
|
|
|
30
33
|
* `EngineOptions.reasoningEffort` wins; absent, the provider default applies.
|
|
31
34
|
*/
|
|
32
35
|
reasoningEffort?: ReasoningEffort;
|
|
36
|
+
/**
|
|
37
|
+
* Guardrail defaults this bundle suggests. Merged per field under
|
|
38
|
+
* `EngineOptions.guardrails`: an explicit field wins, an absent one falls
|
|
39
|
+
* back to the bundle, then to the engine default.
|
|
40
|
+
*/
|
|
41
|
+
guardrails?: GuardrailConfig;
|
|
42
|
+
/**
|
|
43
|
+
* Budget defaults this bundle suggests, merged per field under
|
|
44
|
+
* `EngineOptions.budgets` the same way.
|
|
45
|
+
*/
|
|
46
|
+
budgets?: Partial<BudgetLimits>;
|
|
47
|
+
/**
|
|
48
|
+
* Present only on workflow bundles (`workflowAgent`): a headless run with a
|
|
49
|
+
* check-gated, schema-validated finish. The engine enforces it; see
|
|
50
|
+
* engine/workflow.ts.
|
|
51
|
+
*/
|
|
52
|
+
workflow?: WorkflowContract;
|
|
33
53
|
}
|
|
34
54
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/agents/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACvD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAE9C,MAAM,WAAW,KAAK;IACpB,wEAAwE;IACxE,IAAI,EAAE,MAAM,CAAC;IACb,2EAA2E;IAC3E,QAAQ,EAAE,aAAa,CAAC;IACxB,6EAA6E;IAC7E,KAAK,EAAE,IAAI,EAAE,CAAC;IACd;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;IACjC;;;OAGG;IACH,eAAe,CAAC,EAAE,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/agents/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACvD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAE9C,MAAM,WAAW,KAAK;IACpB,wEAAwE;IACxE,IAAI,EAAE,MAAM,CAAC;IACb,2EAA2E;IAC3E,QAAQ,EAAE,aAAa,CAAC;IACxB,6EAA6E;IAC7E,KAAK,EAAE,IAAI,EAAE,CAAC;IACd;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;IACjC;;;OAGG;IACH,eAAe,CAAC,EAAE,eAAe,CAAC;IAClC;;;;OAIG;IACH,UAAU,CAAC,EAAE,eAAe,CAAC;IAC7B;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC,YAAY,CAAC,CAAC;IAChC;;;;OAIG;IACH,QAAQ,CAAC,EAAE,gBAAgB,CAAC;CAC7B"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* workflowAgent — a headless agent for background jobs that never talk to a
|
|
3
|
+
* user (environment setup, migrations, scheduled maintenance).
|
|
4
|
+
*
|
|
5
|
+
* It is an ordinary bundle (strategy + tools + defaults) plus a
|
|
6
|
+
* `WorkflowContract` the engine enforces with seams it already has:
|
|
7
|
+
*
|
|
8
|
+
* - the system prompt says plainly that no one reads the output and no one
|
|
9
|
+
* answers; the brief is the whole task;
|
|
10
|
+
* - the conversational nudges (silent-tool, verbose-turn, prose checkpoint)
|
|
11
|
+
* are off by default; turn-lifecycle guardrails stay on;
|
|
12
|
+
* - `finish` is the only exit, accepted only after the check passed in this
|
|
13
|
+
* run's own tool log, and only with a schema-valid `output`, which lands on
|
|
14
|
+
* `AgentResult.output`;
|
|
15
|
+
* - running out of budget (`maxTurns` included) ends as `budget_exhausted`,
|
|
16
|
+
* a failure;
|
|
17
|
+
* - `ask_questions` is refused, and so are inbox chains and steering.
|
|
18
|
+
*/
|
|
19
|
+
import type { Tool } from "../../tools/types.js";
|
|
20
|
+
import type { JSONSchema, OutputSchema } from "../../schema.js";
|
|
21
|
+
import type { Agent } from "../types.js";
|
|
22
|
+
export { buildWorkflowPrompt } from "./prompt.js";
|
|
23
|
+
export interface WorkflowSpec {
|
|
24
|
+
/** Short name for logs and traces: "environment", "migration", … */
|
|
25
|
+
name: string;
|
|
26
|
+
/** Everything the run must achieve. There is no conversation: this must be complete. */
|
|
27
|
+
brief: string;
|
|
28
|
+
/** Domain tools; coding tools are composed by the caller. */
|
|
29
|
+
tools: Tool[];
|
|
30
|
+
/** The proof the run must produce before it may finish. */
|
|
31
|
+
check: {
|
|
32
|
+
command: string;
|
|
33
|
+
description: string;
|
|
34
|
+
};
|
|
35
|
+
/** The structured result the run must return; validated, not suggested. */
|
|
36
|
+
output?: {
|
|
37
|
+
name: string;
|
|
38
|
+
schema: OutputSchema;
|
|
39
|
+
/** Shown to the model when `schema` is a zod schema without `toJSONSchema()`. */
|
|
40
|
+
jsonSchema?: JSONSchema;
|
|
41
|
+
description?: string;
|
|
42
|
+
};
|
|
43
|
+
budget?: {
|
|
44
|
+
maxTurns?: number;
|
|
45
|
+
maxOutputTokens?: number;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
export declare function workflowAgent(spec: WorkflowSpec): Agent;
|
|
49
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/agents/workflow/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAGjD,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAChE,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAGzC,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAElD,MAAM,WAAW,YAAY;IAC3B,oEAAoE;IACpE,IAAI,EAAE,MAAM,CAAC;IACb,wFAAwF;IACxF,KAAK,EAAE,MAAM,CAAC;IACd,6DAA6D;IAC7D,KAAK,EAAE,IAAI,EAAE,CAAC;IACd,2DAA2D;IAC3D,KAAK,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,CAAA;KAAE,CAAC;IAChD,2EAA2E;IAC3E,MAAM,CAAC,EAAE;QACP,IAAI,EAAE,MAAM,CAAC;QACb,MAAM,EAAE,YAAY,CAAC;QACrB,iFAAiF;QACjF,UAAU,CAAC,EAAE,UAAU,CAAC;QACxB,WAAW,CAAC,EAAE,MAAM,CAAC;KACtB,CAAC;IACF,MAAM,CAAC,EAAE;QAAE,QAAQ,CAAC,EAAE,MAAM,CAAC;QAAC,eAAe,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CAC1D;AAID,wBAAgB,aAAa,CAAC,IAAI,EAAE,YAAY,GAAG,KAAK,CA8CvD"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { buildWorkflowPrompt } from "./prompt.js";
|
|
2
|
+
export { buildWorkflowPrompt } from "./prompt.js";
|
|
3
|
+
const QUESTION_TOOL = "ask_questions";
|
|
4
|
+
export function workflowAgent(spec) {
|
|
5
|
+
if (!spec.name.trim())
|
|
6
|
+
throw new Error("workflowAgent: name is required");
|
|
7
|
+
if (!spec.brief.trim())
|
|
8
|
+
throw new Error(`workflowAgent "${spec.name}": brief is required — it is the whole task`);
|
|
9
|
+
if (!spec.check.command.trim())
|
|
10
|
+
throw new Error(`workflowAgent "${spec.name}": check.command is required`);
|
|
11
|
+
if (spec.tools.some((tool) => tool.definition.name === QUESTION_TOOL))
|
|
12
|
+
throw new Error(`workflowAgent "${spec.name}": ${QUESTION_TOOL} is not allowed — no one answers a workflow's questions`);
|
|
13
|
+
if (!spec.tools.some((tool) => tool.definition.name === "bash"))
|
|
14
|
+
throw new Error(`workflowAgent "${spec.name}": a bash tool is required — the check is proven by running it`);
|
|
15
|
+
const { maxTurns, maxOutputTokens } = spec.budget ?? {};
|
|
16
|
+
if (maxTurns !== undefined && (!Number.isInteger(maxTurns) || maxTurns <= 0))
|
|
17
|
+
throw new Error(`workflowAgent "${spec.name}": budget.maxTurns must be a positive integer`);
|
|
18
|
+
const strategy = {
|
|
19
|
+
name: spec.name,
|
|
20
|
+
phases: [{ id: "run", goal: "achieve the brief and prove it with the check" }],
|
|
21
|
+
requiredTools: ["bash"],
|
|
22
|
+
requiresSandbox: true,
|
|
23
|
+
systemPrompt: ({ context, summary }) => buildWorkflowPrompt(spec, {
|
|
24
|
+
...(context ? { context } : {}),
|
|
25
|
+
...(summary ? { summary } : {}),
|
|
26
|
+
}),
|
|
27
|
+
};
|
|
28
|
+
const workflow = {
|
|
29
|
+
check: { ...spec.check },
|
|
30
|
+
...(spec.output ? { output: { ...spec.output } } : {}),
|
|
31
|
+
};
|
|
32
|
+
return {
|
|
33
|
+
name: spec.name,
|
|
34
|
+
strategy,
|
|
35
|
+
tools: [...spec.tools],
|
|
36
|
+
...(maxOutputTokens !== undefined ? { maxOutputTokens } : {}),
|
|
37
|
+
...(maxTurns !== undefined ? { budgets: { maxSteps: maxTurns } } : {}),
|
|
38
|
+
guardrails: {
|
|
39
|
+
toolOnlyNudgeLimit: false,
|
|
40
|
+
verboseTurnLimit: false,
|
|
41
|
+
proseCheckpoint: false,
|
|
42
|
+
},
|
|
43
|
+
workflow,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/agents/workflow/index.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The workflow system prompt. Short on purpose: the brief is the task, and the
|
|
3
|
+
* rest only removes the conversational habits a chat-trained model brings
|
|
4
|
+
* (asking, narrating, ending on a reply).
|
|
5
|
+
*/
|
|
6
|
+
import type { WorkflowSpec } from "./index.js";
|
|
7
|
+
export declare function buildWorkflowPrompt(spec: Pick<WorkflowSpec, "name" | "brief" | "check" | "output">, extra?: {
|
|
8
|
+
context?: string;
|
|
9
|
+
summary?: string;
|
|
10
|
+
}): string;
|
|
11
|
+
//# sourceMappingURL=prompt.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prompt.d.ts","sourceRoot":"","sources":["../../../src/agents/workflow/prompt.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,IAAI,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO,GAAG,OAAO,GAAG,QAAQ,CAAC,EAC/D,KAAK,GAAE;IAAE,OAAO,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAA;CAAO,GACjD,MAAM,CAuBR"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export function buildWorkflowPrompt(spec, extra = {}) {
|
|
2
|
+
const output = spec.output
|
|
3
|
+
? `\n\n# Output\nReturn \`${spec.output.name}\` as the \`output\` argument of \`finish\`. It is validated against its schema; an invalid value is rejected and you must fix it.` +
|
|
4
|
+
(spec.output.description ? `\n${spec.output.description}` : "")
|
|
5
|
+
: "";
|
|
6
|
+
const context = extra.context ? `\n\n# Workspace\n${extra.context}` : "";
|
|
7
|
+
const summary = extra.summary
|
|
8
|
+
? `\n\n# Summary of earlier work in this run\n${extra.summary}`
|
|
9
|
+
: "";
|
|
10
|
+
return `You are running the "${spec.name}" workflow: a background job with no conversation.
|
|
11
|
+
|
|
12
|
+
No one is reading your output. No one will answer a question. Do not ask for permission, confirmation, or clarification, and do not narrate. The brief below is complete: where it leaves a choice open, decide from what you observe in the workspace and keep going.
|
|
13
|
+
|
|
14
|
+
Work only through tool calls. A prose reply does not end the run. The only way to end it is \`finish\`, and \`finish\` is accepted only after the check below has passed in this run. Report results as data in \`finish\`, not as prose.
|
|
15
|
+
|
|
16
|
+
# Brief
|
|
17
|
+
${spec.brief.trim()}
|
|
18
|
+
|
|
19
|
+
# Check
|
|
20
|
+
${spec.check.description.trim()}
|
|
21
|
+
Run it with bash exactly as written (a \`cd <dir> &&\` prefix is fine):
|
|
22
|
+
\`${spec.check.command}\`
|
|
23
|
+
A pass counts only if it is the latest run of the check and no file was edited after it.${output}${context}${summary}`;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=prompt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prompt.js","sourceRoot":"","sources":["../../../src/agents/workflow/prompt.ts"],"names":[],"mappings":"AAOA,MAAM,UAAU,mBAAmB,CACjC,IAA+D,EAC/D,KAAK,GAA2C,EAAE;IAElD,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM;QACxB,CAAC,CAAC,0BAA0B,IAAI,CAAC,MAAM,CAAC,IAAI,oIAAoI;YAC9K,CAAC,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACjE,CAAC,CAAC,EAAE,CAAC;IACP,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,oBAAoB,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IACzE,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO;QAC3B,CAAC,CAAC,8CAA8C,KAAK,CAAC,OAAO,EAAE;QAC/D,CAAC,CAAC,EAAE,CAAC;IACP,OAAO,wBAAwB,IAAI,CAAC,IAAI;;;;;;;EAOxC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE;;;EAGjB,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,IAAI,EAAE;;IAE3B,IAAI,CAAC,KAAK,CAAC,OAAO;0FACoE,MAAM,GAAG,OAAO,GAAG,OAAO,EAAE,CAAC;AACvH,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loop.d.ts","sourceRoot":"","sources":["../../src/engine/loop.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"loop.d.ts","sourceRoot":"","sources":["../../src/engine/loop.ts"],"names":[],"mappings":"AA+GA,OAAO,KAAK,EACV,QAAQ,EACR,aAAa,EACb,WAAW,EAIZ,MAAM,YAAY,CAAC;AAUpB,YAAY,EACV,QAAQ,EACR,aAAa,EACb,WAAW,EACX,YAAY,EACZ,eAAe,EACf,cAAc,GACf,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAkmFtC,gFAAgF;AAChF;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CASpE;AAuHD,wBAAsB,QAAQ,CAC5B,KAAK,EAAE,QAAQ,EACf,IAAI,EAAE,aAAa,GAClB,OAAO,CAAC,WAAW,CAAC,CAStB"}
|
package/dist/engine/loop.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { createFinishTool } from "../tools/finish.js";
|
|
2
2
|
import { validateRepos } from "../repo.js";
|
|
3
|
+
import { jsonSchemaOf, validateOutput } from "../schema.js";
|
|
4
|
+
import { checkEvidence, checkRejection, WORKFLOW_KICKOFF, workflowProseRejection, } from "./workflow.js";
|
|
3
5
|
import { BudgetTracker, DEFAULT_BUDGET_LIMITS, DEFAULT_CHILD_SOFT_CAP, DEFAULT_MAX_OUTPUT_TOKENS, } from "./budgets.js";
|
|
4
6
|
import { DEFAULT_TRUNCATED_REPLY_CHARS, DEFAULT_MAX_ASSISTANT_CHARS, elideReply, spillReply, } from "./elision.js";
|
|
5
7
|
import { compactConversation, compactConversationStructured, DEFAULT_COMPACTION, estimateTokens, renderCompactionHandoff, } from "./compaction.js";
|
|
@@ -221,14 +223,80 @@ async function runOne(input, opts, chain = {}) {
|
|
|
221
223
|
let completionGate;
|
|
222
224
|
let verified = false;
|
|
223
225
|
let finishGateAttempts = 0;
|
|
226
|
+
// A workflow bundle's contract (engine/workflow.ts). Its finish is required,
|
|
227
|
+
// gated on the check having passed in THIS run's own tool log, and carries a
|
|
228
|
+
// schema-validated output. Absent: every path below is today's behavior.
|
|
229
|
+
const workflow = opts.agent?.workflow;
|
|
230
|
+
const workflowLog = [];
|
|
231
|
+
let workflowOutput;
|
|
232
|
+
const workflowGate = (contract, output) => {
|
|
233
|
+
const problems = [];
|
|
234
|
+
const observed = checkEvidence(workflowLog, contract.check.command);
|
|
235
|
+
if (observed.status !== "passed")
|
|
236
|
+
problems.push(checkRejection(contract, observed));
|
|
237
|
+
let parsed;
|
|
238
|
+
if (contract.output) {
|
|
239
|
+
const name = contract.output.name;
|
|
240
|
+
if (output === undefined) {
|
|
241
|
+
problems.push(`finish rejected: \`output\` (${name}) is required — pass it on finish.`);
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
const verdict = validateOutput(contract.output.schema, output);
|
|
245
|
+
if (verdict.ok)
|
|
246
|
+
parsed = { value: verdict.value };
|
|
247
|
+
else
|
|
248
|
+
problems.push(`finish rejected: \`output\` (${name}) does not match its schema — ${verdict.error}. Fix it and call finish again.`);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
if (problems.length) {
|
|
252
|
+
const content = problems.join("\n\n");
|
|
253
|
+
emit({ type: "guardrail", kind: "finish_rejected", detail: content.slice(0, 500) });
|
|
254
|
+
return { ok: false, content };
|
|
255
|
+
}
|
|
256
|
+
return { ok: true, ...(parsed ? { parsed } : {}) };
|
|
257
|
+
};
|
|
224
258
|
const finish = createFinishTool({
|
|
225
259
|
...(opts.handoff ? { handoff: opts.handoff } : {}),
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
260
|
+
...(workflow ? { required: true } : {}),
|
|
261
|
+
...(workflow?.output
|
|
262
|
+
? {
|
|
263
|
+
output: {
|
|
264
|
+
name: workflow.output.name,
|
|
265
|
+
schema: jsonSchemaOf(workflow.output.schema, workflow.output.jsonSchema),
|
|
266
|
+
...(workflow.output.description
|
|
267
|
+
? { description: workflow.output.description }
|
|
268
|
+
: {}),
|
|
269
|
+
},
|
|
270
|
+
}
|
|
271
|
+
: {}),
|
|
272
|
+
validate: async (input) => {
|
|
273
|
+
if (!workflow)
|
|
274
|
+
return completionGate
|
|
275
|
+
? completionGate(input.summary, input.success)
|
|
276
|
+
: { ok: true };
|
|
277
|
+
const gate = workflowGate(workflow, input.output);
|
|
278
|
+
if (!gate.ok)
|
|
279
|
+
return gate;
|
|
280
|
+
const verdict = completionGate
|
|
281
|
+
? await completionGate(input.summary, input.success)
|
|
282
|
+
: { ok: true };
|
|
283
|
+
if (verdict.ok && gate.parsed)
|
|
284
|
+
workflowOutput = gate.parsed;
|
|
285
|
+
return verdict;
|
|
286
|
+
},
|
|
229
287
|
});
|
|
230
288
|
if (opts.intrinsics?.finish !== false)
|
|
231
289
|
available.set("finish", finish);
|
|
290
|
+
if (workflow) {
|
|
291
|
+
// A workflow never talks to anyone: finish is its only exit, nobody will
|
|
292
|
+
// answer a question, and nobody steers it from outside.
|
|
293
|
+
if (opts.intrinsics?.finish === false)
|
|
294
|
+
throw new Error(`workflow "${opts.agent?.name}" ends only through finish — intrinsics.finish cannot be false`);
|
|
295
|
+
if (available.has("ask_questions"))
|
|
296
|
+
throw new Error(`workflow "${opts.agent?.name}" cannot have ask_questions — no one answers a workflow's questions`);
|
|
297
|
+
if (chain.steer)
|
|
298
|
+
throw new Error(`workflow "${opts.agent?.name}" cannot run as a delegated team member — workflow runs are not steerable`);
|
|
299
|
+
}
|
|
232
300
|
const missing = (strategy.requiredTools ?? []).filter((name) => !available.has(name));
|
|
233
301
|
if (missing.length)
|
|
234
302
|
throw new Error(`strategy "${strategy.name}" requires tools that were not supplied: ${missing.join(", ")}`);
|
|
@@ -262,7 +330,7 @@ async function runOne(input, opts, chain = {}) {
|
|
|
262
330
|
phase = prior.phase;
|
|
263
331
|
let summary = prior?.summary;
|
|
264
332
|
let compaction = prior?.compaction;
|
|
265
|
-
const budget = chain.budget ?? new BudgetTracker(opts
|
|
333
|
+
const budget = chain.budget ?? new BudgetTracker(resolveBudgets(opts));
|
|
266
334
|
const cancelGraceMs = opts.cancelGraceMs ?? DEFAULT_CANCEL_GRACE_MS;
|
|
267
335
|
const stallAfterMs = opts.stallAfterMs === false
|
|
268
336
|
? false
|
|
@@ -712,7 +780,11 @@ async function runOne(input, opts, chain = {}) {
|
|
|
712
780
|
budget.record(compiled.usage);
|
|
713
781
|
attachLedger(compiled.ledger);
|
|
714
782
|
}
|
|
715
|
-
|
|
783
|
+
// The brief lives in the workflow's system prompt; a caller with no per-run
|
|
784
|
+
// facts to add may pass no messages, and the run still opens on a user turn.
|
|
785
|
+
const currentRequests = workflow && !input.messages.some((message) => message.role === "user")
|
|
786
|
+
? [...input.messages, { role: "user", content: WORKFLOW_KICKOFF }]
|
|
787
|
+
: input.messages;
|
|
716
788
|
const revivalOn = !!opts.revival;
|
|
717
789
|
const revivalWarnAt = revivalWarnPercent(opts.revival ? opts.revival.warnAtPercent : undefined);
|
|
718
790
|
let revivalWarned = false;
|
|
@@ -841,9 +913,11 @@ async function runOne(input, opts, chain = {}) {
|
|
|
841
913
|
return value;
|
|
842
914
|
};
|
|
843
915
|
const threshold = (name, value) => value === false ? false : positiveNumber(name, value);
|
|
844
|
-
|
|
845
|
-
const
|
|
846
|
-
const
|
|
916
|
+
// The bundle may suggest guardrails; an explicit field wins, like budgets.
|
|
917
|
+
const guardrails = mergeDefined(opts.agent?.guardrails, opts.guardrails);
|
|
918
|
+
const repeatLimit = threshold("guardrails.repeatedToolCallLimit", guardrails.repeatedToolCallLimit ?? 3);
|
|
919
|
+
const toolOnlyNudge = threshold("guardrails.toolOnlyNudgeLimit", guardrails.toolOnlyNudgeLimit ?? 8);
|
|
920
|
+
const verboseTurnLimit = threshold("guardrails.verboseTurnLimit", guardrails.verboseTurnLimit ?? 8_000);
|
|
847
921
|
const repeat = new RepeatTracker(repeatLimit);
|
|
848
922
|
let verboseNudged = false;
|
|
849
923
|
// Per-call output cap. The agent bundle may suggest one; an explicit engine
|
|
@@ -988,7 +1062,7 @@ async function runOne(input, opts, chain = {}) {
|
|
|
988
1062
|
let softCapNudged = false;
|
|
989
1063
|
/** Files the child has named in its tool calls, for progress. */
|
|
990
1064
|
const touchedFiles = new Set();
|
|
991
|
-
const proseCheckpoint =
|
|
1065
|
+
const proseCheckpoint = guardrails.proseCheckpoint ?? true;
|
|
992
1066
|
// The shared mechanic lives in guardrails.ts; the policy (when a correction
|
|
993
1067
|
// fires, and when it stops) stays here.
|
|
994
1068
|
const courseCorrect = createCourseCorrect({
|
|
@@ -1504,6 +1578,17 @@ async function runOne(input, opts, chain = {}) {
|
|
|
1504
1578
|
*/
|
|
1505
1579
|
const absorbJobs = async (finished, lost) => {
|
|
1506
1580
|
for (const job of finished) {
|
|
1581
|
+
// A detached check that completed is still the engine's own observation.
|
|
1582
|
+
if (workflow && job.job.command !== undefined)
|
|
1583
|
+
workflowLog.push({
|
|
1584
|
+
tool: job.job.tool,
|
|
1585
|
+
ok: job.result.exitCode === 0 && !job.result.timedOut,
|
|
1586
|
+
meta: {
|
|
1587
|
+
command: job.job.command,
|
|
1588
|
+
exitCode: job.result.exitCode,
|
|
1589
|
+
timedOut: job.result.timedOut,
|
|
1590
|
+
},
|
|
1591
|
+
});
|
|
1507
1592
|
messages.push({
|
|
1508
1593
|
role: "user",
|
|
1509
1594
|
// `runtime`, not `engine`: a job result is a fact, so it survives in
|
|
@@ -1704,6 +1789,12 @@ async function runOne(input, opts, chain = {}) {
|
|
|
1704
1789
|
kind: "budget_exceeded",
|
|
1705
1790
|
detail: over.detail,
|
|
1706
1791
|
});
|
|
1792
|
+
// A workflow's budget is a hard stop and a failure: no partial success.
|
|
1793
|
+
if (workflow)
|
|
1794
|
+
return conclude("budget_exhausted", {
|
|
1795
|
+
ok: false,
|
|
1796
|
+
error: `workflow budget exhausted: ${over.detail}`,
|
|
1797
|
+
});
|
|
1707
1798
|
// A child out of its OWN budget hands back what it has. The leader's
|
|
1708
1799
|
// run continues; a worker hitting its cap is not the leader's failure.
|
|
1709
1800
|
if (chain.child) {
|
|
@@ -1822,9 +1913,11 @@ async function runOne(input, opts, chain = {}) {
|
|
|
1822
1913
|
// Compaction itself consumes budget; check again before inference.
|
|
1823
1914
|
const after = budget.exceeded(stepsBefore + steps);
|
|
1824
1915
|
if (after)
|
|
1825
|
-
return conclude("budget_exceeded", {
|
|
1916
|
+
return conclude(workflow ? "budget_exhausted" : "budget_exceeded", {
|
|
1826
1917
|
ok: false,
|
|
1827
|
-
error:
|
|
1918
|
+
error: workflow
|
|
1919
|
+
? `workflow budget exhausted: ${after.detail}`
|
|
1920
|
+
: after.detail,
|
|
1828
1921
|
});
|
|
1829
1922
|
}
|
|
1830
1923
|
}
|
|
@@ -1943,7 +2036,9 @@ async function runOne(input, opts, chain = {}) {
|
|
|
1943
2036
|
// one runtime message and take the next turn. Once per run; a second
|
|
1944
2037
|
// capped prose turn stalls, so this cannot loop.
|
|
1945
2038
|
if (outputCapped) {
|
|
1946
|
-
|
|
2039
|
+
// Turn lifecycle, not conversation: a workflow keeps it even with
|
|
2040
|
+
// the prose checkpoint off.
|
|
2041
|
+
if (!outputCapContinued && (proseCheckpoint || workflow)) {
|
|
1947
2042
|
outputCapContinued = true;
|
|
1948
2043
|
await courseCorrect({
|
|
1949
2044
|
kind: "output_cap_continue",
|
|
@@ -1959,6 +2054,22 @@ async function runOne(input, opts, chain = {}) {
|
|
|
1959
2054
|
`and returned no tool call — the reply is incomplete`,
|
|
1960
2055
|
});
|
|
1961
2056
|
}
|
|
2057
|
+
// A workflow has no reader: prose is never its result. Reject it like
|
|
2058
|
+
// any other ungated finish and keep going; the budget bounds this.
|
|
2059
|
+
if (workflow) {
|
|
2060
|
+
emit({
|
|
2061
|
+
type: "guardrail",
|
|
2062
|
+
kind: "finish_rejected",
|
|
2063
|
+
detail: `step ${steps} replied in prose without finish — a workflow ends only through its gated finish`,
|
|
2064
|
+
});
|
|
2065
|
+
messages.push({
|
|
2066
|
+
role: "user",
|
|
2067
|
+
origin: "engine",
|
|
2068
|
+
content: workflowProseRejection(workflow),
|
|
2069
|
+
});
|
|
2070
|
+
await persist();
|
|
2071
|
+
continue;
|
|
2072
|
+
}
|
|
1962
2073
|
// The engine just asked for something visible, and the model answered
|
|
1963
2074
|
// in prose and stopped. That is exactly the shape that cut a run off
|
|
1964
2075
|
// mid-task (#78). Ask once whether the run is really over; the model
|
|
@@ -2026,6 +2137,12 @@ async function runOne(input, opts, chain = {}) {
|
|
|
2026
2137
|
signal: toolSignal,
|
|
2027
2138
|
pauseRequested: () => pauseRequested,
|
|
2028
2139
|
onToolResult: (call, result) => {
|
|
2140
|
+
if (workflow)
|
|
2141
|
+
workflowLog.push({
|
|
2142
|
+
tool: call.name,
|
|
2143
|
+
ok: result.ok,
|
|
2144
|
+
...(result.meta ? { meta: result.meta } : {}),
|
|
2145
|
+
});
|
|
2029
2146
|
// Contract tools describe the ledger; they are not evidence about
|
|
2030
2147
|
// the work and must not be recorded into the ledger they manage.
|
|
2031
2148
|
if (!ledger || LEDGER_TOOL_NAMES.has(call.name)) {
|
|
@@ -2108,7 +2225,11 @@ async function runOne(input, opts, chain = {}) {
|
|
|
2108
2225
|
// the model confirm; a second finish with nothing outstanding ends.
|
|
2109
2226
|
if (await settleBeforeFinish())
|
|
2110
2227
|
continue;
|
|
2111
|
-
return conclude("finished", {
|
|
2228
|
+
return conclude("finished", {
|
|
2229
|
+
...declared,
|
|
2230
|
+
completion: "declared",
|
|
2231
|
+
...(workflowOutput ? { output: workflowOutput.value } : {}),
|
|
2232
|
+
});
|
|
2112
2233
|
}
|
|
2113
2234
|
if (pauseRequested)
|
|
2114
2235
|
return conclude("paused", {
|
|
@@ -2140,8 +2261,24 @@ async function runOne(input, opts, chain = {}) {
|
|
|
2140
2261
|
return conclude("error", { ok: false, error });
|
|
2141
2262
|
}
|
|
2142
2263
|
}
|
|
2264
|
+
/**
|
|
2265
|
+
* Bundle defaults under explicit options, field by field. An explicit field
|
|
2266
|
+
* set to `undefined` does not erase the bundle's value.
|
|
2267
|
+
*/
|
|
2268
|
+
function mergeDefined(defaults, explicit) {
|
|
2269
|
+
const merged = { ...defaults };
|
|
2270
|
+
for (const [key, value] of Object.entries(explicit ?? {})) {
|
|
2271
|
+
if (value !== undefined)
|
|
2272
|
+
merged[key] = value;
|
|
2273
|
+
}
|
|
2274
|
+
return merged;
|
|
2275
|
+
}
|
|
2276
|
+
/** The run's budget limits: the bundle's suggestion, then explicit options. */
|
|
2277
|
+
function resolveBudgets(opts) {
|
|
2278
|
+
return mergeDefined(opts.agent?.budgets, opts.budgets);
|
|
2279
|
+
}
|
|
2143
2280
|
function seededBudget(opts, spend) {
|
|
2144
|
-
const budget = new BudgetTracker(opts
|
|
2281
|
+
const budget = new BudgetTracker(resolveBudgets(opts));
|
|
2145
2282
|
budget.promptTokens = spend.promptTokens;
|
|
2146
2283
|
budget.completionTokens = spend.completionTokens;
|
|
2147
2284
|
budget.costUsd = spend.costUsd;
|
|
@@ -2222,7 +2359,7 @@ async function runChain(input, opts) {
|
|
|
2222
2359
|
// A run normally spends at least one step, so the budget bounds the chain.
|
|
2223
2360
|
// This counter only stops the one shape that spends nothing: an interjection
|
|
2224
2361
|
// arriving before every single model call. No new public dial.
|
|
2225
|
-
const maxZeroProgressRuns = Math.max(1, opts.
|
|
2362
|
+
const maxZeroProgressRuns = Math.max(1, resolveBudgets(opts).maxSteps ?? DEFAULT_BUDGET_LIMITS.maxSteps);
|
|
2226
2363
|
let zeroProgressRuns = 0;
|
|
2227
2364
|
const runs = [];
|
|
2228
2365
|
let spend = {
|
|
@@ -2307,8 +2444,11 @@ async function runChain(input, opts) {
|
|
|
2307
2444
|
return { ...result, runs };
|
|
2308
2445
|
}
|
|
2309
2446
|
export async function runAgent(input, opts) {
|
|
2310
|
-
if (opts.inbox)
|
|
2447
|
+
if (opts.inbox) {
|
|
2448
|
+
if (opts.agent?.workflow)
|
|
2449
|
+
throw new Error(`anchor: workflow "${opts.agent.name}" is one-shot — EngineOptions.inbox (run chains and interjections) does not apply to a workflow agent`);
|
|
2311
2450
|
return runChain(input, opts);
|
|
2451
|
+
}
|
|
2312
2452
|
return runOne(input, opts);
|
|
2313
2453
|
}
|
|
2314
2454
|
//# sourceMappingURL=loop.js.map
|