@nones/agentflow-strategy-goal 0.1.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 AgentFlow contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,6 +1,18 @@
1
- # Goal strategy
1
+ # `@nones/agentflow-strategy-goal`
2
2
 
3
- `goalStrategy.compile(input)` accepts:
3
+ A same-session goal loop for AgentFlow. The agent receives a goal and a
4
+ structured completion protocol; each reply continues the same native session
5
+ until the goal is reached, blocked or a configured budget is exhausted.
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ bun add @nones/agentflow-strategy-goal
11
+ ```
12
+
13
+ Bun 1.4.5 or newer is required.
14
+
15
+ ## Input
4
16
 
5
17
  ```ts
6
18
  {
@@ -12,62 +24,118 @@
12
24
  }
13
25
  ```
14
26
 
15
- It returns a one-wave `RunPlan`. The compiled agent has:
27
+ ## Compile and run
16
28
 
17
- - a prompt containing the original prompt, `Goal: <goal>`, and the completion protocol;
18
- - `strategy: "goal"`;
19
- - `strategyInput` containing the validated goal settings.
29
+ ```ts
30
+ import { goalStrategy } from "@nones/agentflow-strategy-goal";
20
31
 
21
- The settings on `strategyInput` are also how `decide` reads the goal because
22
- `TurnContext` does not carry the run-level input. An embedded goal agent can be
23
- built with the same settings directly:
32
+ const plan = goalStrategy.compile({
33
+ agent: {
34
+ id: "fixer",
35
+ prompt: "Work in small verified steps.",
36
+ cwd: process.cwd(),
37
+ },
38
+ goal: "Make `bun test apps/cli` pass without changing unrelated files.",
39
+ maxTurns: 6,
40
+ verifier: "tests-pass",
41
+ });
42
+ ```
43
+
44
+ Through the CLI, pass the goal and its agent to the `goal` strategy:
24
45
 
25
46
  ```ts
26
- agent({
27
- ...,
47
+ const run = await flow.start({
28
48
  strategy: "goal",
29
- strategyInput: { goal, maxTurns, timeoutMs, verifier },
49
+ input: {
50
+ agent: {
51
+ id: "fixer",
52
+ prompt: "Work in small verified steps.",
53
+ cwd: process.cwd(),
54
+ },
55
+ goal: "Make `bun test apps/cli` pass.",
56
+ maxTurns: 6,
57
+ },
30
58
  });
31
59
  ```
32
60
 
61
+ When composing a server from the project directory, register both the strategy
62
+ and any verifier. This verifier checks the test exit code in the server:
63
+
64
+ ```ts
65
+ import type { ServeOptions } from "@nones/agentflow-core";
66
+ import { goalStrategy } from "@nones/agentflow-strategy-goal";
67
+
68
+ const strategyOptions = {
69
+ strategies: [goalStrategy],
70
+ verifiers: {
71
+ "tests-pass": async () => {
72
+ const tests = Bun.spawn([process.execPath, "test", "apps/cli"], {
73
+ stdout: "ignore",
74
+ stderr: "ignore",
75
+ });
76
+ return (await tests.exited) === 0;
77
+ },
78
+ },
79
+ } satisfies Pick<ServeOptions, "strategies" | "verifiers">;
80
+ ```
81
+
82
+ The compiled agent has a prompt containing the original prompt, `Goal: <goal>`
83
+ and the completion protocol, plus `strategy: "goal"` and the validated settings
84
+ in `strategyInput`. Continuation decisions never change the session ID, so every
85
+ turn stays in the same native session.
86
+
33
87
  ## Completion protocol
34
88
 
35
89
  Every reply must end with exactly one block:
36
90
 
37
- ```
91
+ ```text
38
92
  <<<AGENTFLOW_GOAL>>>
39
93
  {"status":"continue","reason":"short reason"}
40
94
  <<<AGENTFLOW_GOAL_END>>>
41
95
  ```
42
96
 
43
- `status` must be exactly `continue`, `reached`, or `blocked`. `reason` is
44
- required for `blocked`. A `reached` reply may include `evidence` for a
45
- configured verifier. Prose before the block is allowed; text after the end
46
- marker, multiple blocks, unknown statuses, malformed JSON, and missing blocked
47
- reasons are all rejected. Rejections produce a `paused` decision with an
48
- `invalid goal response: ...` diagnostic.
97
+ `status` must be exactly `continue`, `reached` or `blocked`. `reason` is required
98
+ for `blocked`. A `reached` reply may include `evidence` for a configured
99
+ verifier. Prose before the block is allowed. Text after the end marker, multiple
100
+ blocks, unknown statuses, malformed JSON and missing blocked reasons are
101
+ rejected; a rejection pauses the agent with an `invalid goal response: ...`
102
+ diagnostic.
49
103
 
50
- For compatibility, a reply containing no protocol markers may instead be
51
- exactly one status word (`continue`, `reached`, or `blocked`) or a JSON status
52
- object (optionally in a single fenced block). A plain `blocked` word uses a
53
- generic blocker reason because it has no detail. If a protocol marker appears,
54
- only the canonical tagged form is accepted.
104
+ For compatibility, a reply containing no protocol markers may instead be exactly
105
+ one status word (`continue`, `reached` or `blocked`) or a JSON status object,
106
+ optionally in a single fenced block. Once a protocol marker appears, only the
107
+ canonical tagged form is accepted.
55
108
 
56
109
  ## Decisions
57
110
 
58
- - `reached` becomes `complete`, after the configured verifier passes.
59
- - A configured verifier must be provided as `TurnContext.verify`. If it is
60
- missing, throws, returns a non-boolean, or rejects the evidence, the goal does
61
- not complete. A rejection becomes a `continue` decision with feedback for the
62
- next turn, unless a budget is already exhausted.
63
- - `continue` becomes a `continue` decision whose prompt includes the protocol
64
- again, unless a budget is exhausted.
111
+ - `reached` becomes `complete` after the configured verifier passes. Without a
112
+ verifier, it completes directly.
113
+ - A configured verifier must be present as `TurnContext.verify`. If it is
114
+ missing, throws, returns a non-boolean or rejects the evidence, the goal does
115
+ not complete. A rejection becomes a `continue` decision with feedback until a
116
+ budget is exhausted.
117
+ - `continue` becomes a continuation prompt containing the protocol again unless
118
+ a budget is exhausted.
65
119
  - `blocked` becomes `paused`.
66
- - No decision carries a session ID, so core keeps the same session for every
67
- continuation turn.
68
-
69
- `TurnContext.turns` is treated as the 1-based number of the current goal turn.
70
- An explicit `maxTurns` is checked after a `reached` reply has been handled, so a
71
- successful final turn completes. A `continue` on the final turn pauses. With no
72
- `maxTurns` or `timeoutMs`, continuation may run indefinitely. `timeoutMs` is
73
- checked against `TurnContext.elapsedMs` in the same way.
120
+ - Failed turns become `failed`.
121
+
122
+ `TurnContext.turns` is the 1-based current goal turn. An explicit `maxTurns` is
123
+ checked after a `reached` reply is handled, so a successful final turn
124
+ completes; a `continue` on the final turn pauses. `timeoutMs` is compared against
125
+ `TurnContext.elapsedMs` in the same way. Without `maxTurns` or `timeoutMs`,
126
+ continuation may run until the goal is reached or blocked.
127
+
128
+ The package also exports `parseGoalReply`, `GoalResponseError`, `buildGoalPrompt`,
129
+ `goalProtocolText`, `appendGoalProtocol` and the `GOAL_PROTOCOL_*` constants for
130
+ custom integrations.
131
+
132
+ ## Documentation
133
+
134
+ - [Goal example](https://github.com/nones-tools/agentflow/blob/main/examples/goal.ts)
135
+ - [Core contracts](https://github.com/nones-tools/agentflow/blob/main/docs/contracts.md)
136
+ - [Repository README](https://github.com/nones-tools/agentflow/blob/main/README.md)
137
+ - [Issues](https://github.com/nones-tools/agentflow/issues)
138
+
139
+ ## License
140
+
141
+ MIT. See [LICENSE](https://github.com/nones-tools/agentflow/blob/main/LICENSE).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nones/agentflow-strategy-goal",
3
- "version": "0.1.0",
3
+ "version": "0.6.0",
4
4
  "description": "Same-session goal AgentFlow strategy",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -35,6 +35,10 @@
35
35
  "test": "bun test"
36
36
  },
37
37
  "dependencies": {
38
- "@nones/agentflow-core": "^0.1.0"
39
- }
38
+ "@nones/agentflow-core": "0.6.0"
39
+ },
40
+ "bugs": {
41
+ "url": "https://github.com/nones-tools/agentflow/issues"
42
+ },
43
+ "license": "MIT"
40
44
  }
package/src/agent.ts ADDED
@@ -0,0 +1,46 @@
1
+ import type { AgentSpec } from "@nones/agentflow-core";
2
+ import { normalizeGoalInput } from "./internal";
3
+ import { buildGoalPrompt } from "./protocol";
4
+ import type { GoalInput, GoalSettings } from "./types";
5
+
6
+ function goalSettings(input: GoalInput): GoalSettings {
7
+ const settings: GoalSettings = { goal: input.goal };
8
+ if (input.maxTurns !== undefined) {
9
+ settings.maxTurns = input.maxTurns;
10
+ }
11
+ if (input.timeoutMs !== undefined) {
12
+ settings.timeoutMs = input.timeoutMs;
13
+ }
14
+ if (input.verifier !== undefined) {
15
+ settings.verifier = input.verifier;
16
+ }
17
+ return settings;
18
+ }
19
+
20
+ /**
21
+ * Build the same single goal agent that {@link goalStrategy.compile} produces.
22
+ *
23
+ * The builder shares the standalone strategy normalization and protocol, so an
24
+ * embedded goal agent is validated and serialized exactly like a goal run.
25
+ */
26
+ export function goalAgent(input: GoalInput): AgentSpec {
27
+ return buildGoalAgent(input);
28
+ }
29
+
30
+ /**
31
+ * Unknown-input entry used by `goalStrategy.compile`. The public builder keeps
32
+ * its explicit `GoalInput` type while the runtime still validates the value.
33
+ */
34
+ export function buildGoalAgent(input: unknown): AgentSpec {
35
+ const normalized = normalizeGoalInput(input);
36
+ return {
37
+ ...normalized.agent,
38
+ prompt: buildGoalPrompt(
39
+ normalized.agent.prompt,
40
+ normalized.goal,
41
+ normalized.verifier,
42
+ ),
43
+ strategy: "goal",
44
+ strategyInput: goalSettings(normalized),
45
+ };
46
+ }
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export { goalAgent } from "./agent";
1
2
  export { goalStrategy } from "./strategy";
2
3
  export {
3
4
  GOAL_PROTOCOL_END,
package/src/internal.ts CHANGED
@@ -8,6 +8,7 @@ const AGENT_FIELDS = [
8
8
  "prompt",
9
9
  "cwd",
10
10
  "profile",
11
+ "preset",
11
12
  "model",
12
13
  "settings",
13
14
  "strategy",
@@ -159,6 +160,11 @@ export function normalizeAgent(value: unknown, path: string): AgentSpec {
159
160
  spec.profile = profile;
160
161
  }
161
162
 
163
+ const preset = expectOptionalString(record.preset, `${path}.preset`);
164
+ if (preset !== undefined) {
165
+ spec.preset = preset;
166
+ }
167
+
162
168
  const model = expectOptionalString(record.model, `${path}.model`);
163
169
  if (model !== undefined) {
164
170
  spec.model = model;
package/src/protocol.ts CHANGED
@@ -35,6 +35,10 @@ export function goalProtocolText(verifier?: GoalVerifierConfig): string {
35
35
  return lines.join("\n");
36
36
  }
37
37
 
38
+ function protocolMarkerIndex(prompt: string): number {
39
+ return prompt.search(/^<<<AGENTFLOW_GOAL>>>[ \t]*\r?$/mu);
40
+ }
41
+
38
42
  /**
39
43
  * Append the completion protocol unless the prompt already contains it.
40
44
  */
@@ -42,7 +46,7 @@ export function appendGoalProtocol(
42
46
  prompt: string,
43
47
  verifier?: GoalVerifierConfig,
44
48
  ): string {
45
- if (prompt.includes(GOAL_PROTOCOL_MARKER)) {
49
+ if (protocolMarkerIndex(prompt) !== -1) {
46
50
  return prompt;
47
51
  }
48
52
  return `${prompt.trimEnd()}\n\n${goalProtocolText(verifier)}`;
@@ -58,7 +62,7 @@ export function buildGoalPrompt(
58
62
  verifier?: GoalVerifierConfig,
59
63
  ): string {
60
64
  const goalSection = `Goal:\n${goal}`;
61
- const markerIndex = agentPrompt.indexOf(GOAL_PROTOCOL_MARKER);
65
+ const markerIndex = protocolMarkerIndex(agentPrompt);
62
66
  if (markerIndex !== -1) {
63
67
  const head = agentPrompt.slice(0, markerIndex).trimEnd();
64
68
  const tail = agentPrompt.slice(markerIndex);
package/src/strategy.ts CHANGED
@@ -1,17 +1,13 @@
1
1
  import type {
2
- AgentSpec,
3
2
  RunPlan,
4
3
  Strategy,
5
4
  TurnContext,
6
5
  TurnDecision,
7
6
  } from "@nones/agentflow-core";
8
- import {
9
- isRecord,
10
- normalizeGoalInput,
11
- normalizeGoalSettings,
12
- } from "./internal";
7
+ import { buildGoalAgent } from "./agent";
8
+ import { isRecord, normalizeGoalSettings } from "./internal";
13
9
  import { parseGoalReply } from "./parse";
14
- import { buildGoalPrompt, goalProtocolText, verifierName } from "./protocol";
10
+ import { goalProtocolText, verifierName } from "./protocol";
15
11
  import type { GoalReply, GoalSettings, GoalVerifierConfig } from "./types";
16
12
 
17
13
  const WAVE_ID = "wave-1";
@@ -164,32 +160,8 @@ export const goalStrategy: Strategy = {
164
160
  id: "goal",
165
161
  singleAdapter: true,
166
162
  compile(input: unknown): RunPlan {
167
- const goalInput = normalizeGoalInput(input);
168
-
169
- const settings: GoalSettings = { goal: goalInput.goal };
170
- if (goalInput.maxTurns !== undefined) {
171
- settings.maxTurns = goalInput.maxTurns;
172
- }
173
- if (goalInput.timeoutMs !== undefined) {
174
- settings.timeoutMs = goalInput.timeoutMs;
175
- }
176
- if (goalInput.verifier !== undefined) {
177
- settings.verifier = goalInput.verifier;
178
- }
179
-
180
- const spec: AgentSpec = {
181
- ...goalInput.agent,
182
- prompt: buildGoalPrompt(
183
- goalInput.agent.prompt,
184
- goalInput.goal,
185
- goalInput.verifier,
186
- ),
187
- strategy: "goal",
188
- strategyInput: settings,
189
- };
190
-
191
163
  return {
192
- waves: [{ id: WAVE_ID, agents: [spec] }],
164
+ waves: [{ id: WAVE_ID, agents: [buildGoalAgent(input)] }],
193
165
  };
194
166
  },
195
167