@yaag/runtime 0.23.0 → 0.24.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/package.json +1 -1
- package/src/agent/fork.ts +7 -0
- package/src/agent/spawn-open.ts +24 -5
- package/src/agent/spawn-request.ts +123 -3
- package/src/agent/spawn.ts +3 -0
- package/src/ask/ask-hash.ts +7 -0
- package/src/contract/agent-policy.ts +15 -4
- package/src/contract/error-codes.ts +1 -1
- package/src/contract/index.ts +10 -0
- package/src/contract/spawn-options.ts +7 -1
- package/src/contract/system-prompt.ts +88 -0
- package/src/index.ts +13 -0
- package/src/public-api.ts +11 -0
- package/src/run/run.ts +3 -0
- package/src/system-prompt/agent-prompt-extension.ts +337 -0
- package/src/system-prompt/agent-prompt.ts +15 -0
- package/src/system-prompt/agent-system-prompt.ts +90 -0
- package/src/system-prompt/append-channel.ts +69 -0
- package/src/system-prompt/define-system-prompt.ts +112 -0
- package/src/system-prompt/index.ts +48 -0
- package/src/system-prompt/prompt-guards.ts +60 -0
- package/src/system-prompt/render-sentinel.ts +75 -0
- package/src/transport/append-env.ts +69 -0
- package/src/transport/index.ts +2 -0
- package/src/transport/live-transport.ts +50 -26
- package/src/transport/pi-state.ts +38 -4
- package/src/transport/transport.ts +4 -1
- package/src/types.ts +8 -0
package/package.json
CHANGED
package/src/agent/fork.ts
CHANGED
|
@@ -45,6 +45,12 @@ export interface ForkRequest {
|
|
|
45
45
|
readonly askDefaults: AskOptions | undefined;
|
|
46
46
|
readonly fork: ForkIdentity;
|
|
47
47
|
readonly compact: boolean | string | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* True when the fork carries the text its source already rendered. An
|
|
50
|
+
* explicit `fork({ systemPrompt: … })` override is author-written and stays
|
|
51
|
+
* guarded (ADR-0060).
|
|
52
|
+
*/
|
|
53
|
+
readonly inheritedPrompt: boolean;
|
|
48
54
|
}
|
|
49
55
|
|
|
50
56
|
/**
|
|
@@ -73,6 +79,7 @@ export function forkRequest(source: ForkSource, overrides?: ForkOptions): ForkRe
|
|
|
73
79
|
...(source.worktree === undefined ? {} : { worktreeFrom: source.worktree }),
|
|
74
80
|
},
|
|
75
81
|
compact,
|
|
82
|
+
inheritedPrompt: explicit.systemPrompt === undefined && inherited.systemPrompt !== undefined,
|
|
76
83
|
};
|
|
77
84
|
}
|
|
78
85
|
|
package/src/agent/spawn-open.ts
CHANGED
|
@@ -20,7 +20,12 @@ import type { ForkIdentity, ForkSpawner } from "./fork.ts";
|
|
|
20
20
|
import type { SpawnDependencies } from "./spawn.ts";
|
|
21
21
|
import { resolveSpawnExtensions } from "./spawn-extensions.ts";
|
|
22
22
|
import { resolveParent } from "./spawn-parent.ts";
|
|
23
|
-
import {
|
|
23
|
+
import {
|
|
24
|
+
openRequest,
|
|
25
|
+
type RenderedSpawnOptions,
|
|
26
|
+
renderPolicyPrompt,
|
|
27
|
+
withSelection,
|
|
28
|
+
} from "./spawn-request.ts";
|
|
24
29
|
|
|
25
30
|
/** One request the open path serves: a plain spawn, or a fork of another Agent. */
|
|
26
31
|
export interface OpenAgentRequest {
|
|
@@ -31,6 +36,8 @@ export interface OpenAgentRequest {
|
|
|
31
36
|
readonly askDefaults: AskOptions | undefined;
|
|
32
37
|
/** Definition identity stays separate from a topology-overridden Agent name. */
|
|
33
38
|
readonly definitionName: string | undefined;
|
|
39
|
+
/** The prompt is a fork's inherited rendered text, not author-written. */
|
|
40
|
+
readonly inheritedPrompt?: boolean;
|
|
34
41
|
/** Present only for a fork; adds `origin: "fork"` and the `--fork` source. */
|
|
35
42
|
readonly fork?: ForkIdentity;
|
|
36
43
|
/**
|
|
@@ -72,11 +79,18 @@ export async function openAgent(
|
|
|
72
79
|
// append to it, so a resolver sees every candidate that already failed.
|
|
73
80
|
const history = new ModelErrorHistory();
|
|
74
81
|
try {
|
|
82
|
+
// The Prompt Definition renders once, above every other spawn step, so the
|
|
83
|
+
// peek, the fallback loop and every attempt share one text (ADR-0064).
|
|
84
|
+
const rendered: RenderedSpawnOptions = renderPolicyPrompt({
|
|
85
|
+
options: request.spawnOptions,
|
|
86
|
+
agent: { name, runName: deps.runName },
|
|
87
|
+
...(request.inheritedPrompt === true ? { inheritedPrompt: true } : {}),
|
|
88
|
+
});
|
|
75
89
|
// Extension paths do not vary per candidate, so a bad path fails once, generically.
|
|
76
90
|
const extensions = await resolveSpawnExtensions({
|
|
77
91
|
configExtensions: deps.configExtensions,
|
|
78
|
-
declared:
|
|
79
|
-
useConfigExtensions:
|
|
92
|
+
declared: rendered.extensions,
|
|
93
|
+
useConfigExtensions: rendered.configExtensions !== false,
|
|
80
94
|
...(deps.programFile === undefined ? {} : { programFile: deps.programFile }),
|
|
81
95
|
projectRoot: cwd,
|
|
82
96
|
}).catch((error: unknown) => {
|
|
@@ -107,11 +121,12 @@ export async function openAgent(
|
|
|
107
121
|
const attempt = async (
|
|
108
122
|
selection: ModelSelection,
|
|
109
123
|
): Promise<{ opened: OpenedTransport; spawnOptions: ResolvedSpawnOptions }> => {
|
|
110
|
-
const settled = withSelection(
|
|
124
|
+
const settled = withSelection(rendered, selection);
|
|
111
125
|
return {
|
|
112
126
|
opened: await openTransport({
|
|
113
127
|
factory: deps.factory,
|
|
114
128
|
name,
|
|
129
|
+
runName: deps.runName,
|
|
115
130
|
cwd,
|
|
116
131
|
spawnOptions: settled,
|
|
117
132
|
...extensionFields,
|
|
@@ -127,8 +142,9 @@ export async function openAgent(
|
|
|
127
142
|
const recorded = deps.factory.recordedSpawn?.(
|
|
128
143
|
openRequest({
|
|
129
144
|
name,
|
|
145
|
+
runName: deps.runName,
|
|
130
146
|
cwd,
|
|
131
|
-
spawnOptions:
|
|
147
|
+
spawnOptions: rendered,
|
|
132
148
|
...extensionFields,
|
|
133
149
|
...forkFields,
|
|
134
150
|
...(parent === undefined ? {} : { parent }),
|
|
@@ -213,6 +229,8 @@ function isSpawnFailure(error: unknown): error is YaagError {
|
|
|
213
229
|
interface OpenTransportOptions {
|
|
214
230
|
readonly factory: TransportFactory;
|
|
215
231
|
readonly name: string;
|
|
232
|
+
/** The Run name; the peek and the open must pass the same one (ADR-0013). */
|
|
233
|
+
readonly runName: string;
|
|
216
234
|
readonly cwd: string;
|
|
217
235
|
readonly spawnOptions: ResolvedSpawnOptions;
|
|
218
236
|
readonly resolvedExtensionPaths?: readonly string[];
|
|
@@ -237,6 +255,7 @@ async function openTransport(options: OpenTransportOptions): Promise<OpenedTrans
|
|
|
237
255
|
const transport = await options.factory.open(
|
|
238
256
|
openRequest({
|
|
239
257
|
name: options.name,
|
|
258
|
+
runName: options.runName,
|
|
240
259
|
cwd: options.cwd,
|
|
241
260
|
spawnOptions: options.spawnOptions,
|
|
242
261
|
...(options.resolvedExtensionPaths === undefined
|
|
@@ -1,12 +1,105 @@
|
|
|
1
|
+
import type { SystemPromptAgent } from "../contract/index.ts";
|
|
2
|
+
import { YaagError } from "../errors.ts";
|
|
1
3
|
import type { ModelSelection } from "../model/index.ts";
|
|
4
|
+
import {
|
|
5
|
+
assertKnownSentinels,
|
|
6
|
+
assertNoLeakedSentinel,
|
|
7
|
+
dropsProgramAppend,
|
|
8
|
+
isSystemPromptDefinition,
|
|
9
|
+
programAppendWarning,
|
|
10
|
+
renderSystemPrompt,
|
|
11
|
+
yaagSystemPrompt,
|
|
12
|
+
} from "../system-prompt/index.ts";
|
|
2
13
|
import type { OpenOptions, WorktreeResolution } from "../transport/index.ts";
|
|
3
14
|
import type { ResolvedSpawnOptions, SpawnOptions } from "../types.ts";
|
|
4
15
|
|
|
16
|
+
/** Spawn options whose `systemPrompt`, if set, is already a plain string. */
|
|
17
|
+
export type RenderedSpawnOptions = Omit<SpawnOptions, "systemPrompt"> & {
|
|
18
|
+
readonly systemPrompt?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Renders a Prompt Definition into its text, once per spawn (ADR-0064).
|
|
23
|
+
*
|
|
24
|
+
* The open path calls this above every other spawn step, so the peek, the model
|
|
25
|
+
* fallback loop and every attempt share one rendered text and the builder runs
|
|
26
|
+
* exactly once. Options with no prompt, or with a plain string, pass through
|
|
27
|
+
* untouched: rendering only replaces a definition with its string and never
|
|
28
|
+
* fills the default.
|
|
29
|
+
*
|
|
30
|
+
* This seam is also where a leaked or an unknown Render Sentinel rejects the
|
|
31
|
+
* spawn, before any Agent process starts (ADR-0060, ADR-0063), and where a
|
|
32
|
+
* spawn whose prompt drops `appendSystemPrompt` writes its one warning.
|
|
33
|
+
*/
|
|
34
|
+
export function renderPolicyPrompt(request: RenderPolicyPromptOptions): RenderedSpawnOptions {
|
|
35
|
+
const { options, agent } = request;
|
|
36
|
+
const warn = request.warn ?? writeSpawnDiagnostic;
|
|
37
|
+
// The one place the guard policy lives: both branches below route through it,
|
|
38
|
+
// so the append warning cannot drift between a string and a definition.
|
|
39
|
+
const guard = (text: string, rendered: boolean): string => {
|
|
40
|
+
// Text yaag rendered itself — a definition's output, or a fork's inherited
|
|
41
|
+
// string — legitimately holds sentinels, so the leak guard must not fire.
|
|
42
|
+
if (rendered) assertKnownSentinels(text, agent.name);
|
|
43
|
+
else assertNoLeakedSentinel(text, agent.name);
|
|
44
|
+
if (options.appendSystemPrompt !== undefined && dropsProgramAppend(text)) {
|
|
45
|
+
warn(programAppendWarning(agent.name));
|
|
46
|
+
}
|
|
47
|
+
return text;
|
|
48
|
+
};
|
|
49
|
+
// Destructured rather than cast: `rest` has no `systemPrompt` at all, so each
|
|
50
|
+
// branch below builds the rendered shape from proven parts. An absent prompt
|
|
51
|
+
// stays absent, and no key with an `undefined` value enters recorded options.
|
|
52
|
+
const { systemPrompt: value, ...rest } = options;
|
|
53
|
+
// The default Agent System Prompt fills later in `openRequest`, and it always
|
|
54
|
+
// carries the `programAppend` sentinel, so no guard and no warning is due.
|
|
55
|
+
if (value === undefined) return rest;
|
|
56
|
+
if (typeof value === "string") {
|
|
57
|
+
return { ...rest, systemPrompt: guard(value, request.inheritedPrompt === true) };
|
|
58
|
+
}
|
|
59
|
+
if (!isSystemPromptDefinition(value)) {
|
|
60
|
+
throw new YaagError(
|
|
61
|
+
"SPAWN_FAILED",
|
|
62
|
+
`agent "${agent.name}": systemPrompt must be a string or a defineSystemPrompt definition`,
|
|
63
|
+
agent.name,
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
return { ...rest, systemPrompt: guard(renderSystemPrompt({ definition: value, agent }), true) };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Writes one spawn diagnostic line, best effort.
|
|
71
|
+
*
|
|
72
|
+
* Own writer rather than the recording one: `cassette/` is the wrong owner for
|
|
73
|
+
* a spawn diagnostic, and a closed stderr must never fail a spawn.
|
|
74
|
+
*/
|
|
75
|
+
function writeSpawnDiagnostic(line: string): void {
|
|
76
|
+
try {
|
|
77
|
+
process.stderr.write(`${line}\n`);
|
|
78
|
+
} catch {
|
|
79
|
+
// best effort
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** What one prompt render needs: the options, the Agent, and where a warning goes. */
|
|
84
|
+
export interface RenderPolicyPromptOptions {
|
|
85
|
+
readonly options: SpawnOptions;
|
|
86
|
+
readonly agent: SystemPromptAgent;
|
|
87
|
+
/**
|
|
88
|
+
* True when `systemPrompt` is the text a fork's source already rendered. The
|
|
89
|
+
* leak guard is then skipped: the sentinels in it are yaag's own.
|
|
90
|
+
*/
|
|
91
|
+
readonly inheritedPrompt?: boolean;
|
|
92
|
+
/** Where one warning line goes; the seam's own stderr writer by default. */
|
|
93
|
+
readonly warn?: (line: string) => void;
|
|
94
|
+
}
|
|
95
|
+
|
|
5
96
|
/** Everything one spawn needs to build its open request, before a model settles. */
|
|
6
97
|
export interface OpenRequestOptions {
|
|
7
98
|
readonly name: string;
|
|
99
|
+
/** The Run name, printed in the Agent System Prompt; `run_start.program`. */
|
|
100
|
+
readonly runName: string;
|
|
8
101
|
readonly cwd: string;
|
|
9
|
-
readonly spawnOptions:
|
|
102
|
+
readonly spawnOptions: RenderedSpawnOptions;
|
|
10
103
|
readonly resolvedExtensionPaths?: readonly string[];
|
|
11
104
|
readonly declaredExtensions?: readonly string[];
|
|
12
105
|
/** Resolved parent Agent name (Parent Link); spawn identity only, never argv. */
|
|
@@ -28,6 +121,31 @@ export interface OpenRequestOptions {
|
|
|
28
121
|
* array or resolver spec. That request is what a Cassette-backed factory peeks
|
|
29
122
|
* at, which is why the resume peek blanks model and thinking before it compares
|
|
30
123
|
* the request with the recorded spawn (ADR-0039).
|
|
124
|
+
*
|
|
125
|
+
* A spawn that sets no `systemPrompt` gets the Agent System Prompt here, so the
|
|
126
|
+
* consumers downstream of this seam — the Cassette spawn options, the replay
|
|
127
|
+
* divergence fields, and the Run record — see a plain string (ADR-0055,
|
|
128
|
+
* ADR-0064).
|
|
129
|
+
*
|
|
130
|
+
* The **Ask hash is deliberately not one of them.** `askHash` reads
|
|
131
|
+
* `ResolvedSpawnOptions`, the output of `withSelection`, which this function
|
|
132
|
+
* never touches, so a default spawn hashes `systemPrompt: null` while its spawn
|
|
133
|
+
* identity records the rendered text. That is on purpose: the default text
|
|
134
|
+
* prints `Agent: <name>` and `Run: <runName>`, so feeding it to `askHash` would
|
|
135
|
+
* make Ask identity depend on the Agent name and the Run name — exactly the two
|
|
136
|
+
* things `askMarkerContext` strips from Ask identity. Nothing is lost, because
|
|
137
|
+
* spawn identity already covers the prompt (`replay-divergence.ts`), and a
|
|
138
|
+
* changed prompt therefore diverges at spawn rather than at Ask 0.
|
|
139
|
+
*
|
|
140
|
+
* A value the program did set travels literally. Note the empty string is a
|
|
141
|
+
* value a program set and it survives this seam unchanged, but `piCommand`
|
|
142
|
+
* tests `options.systemPrompt` for truthiness, so `""` drops `--system-prompt`
|
|
143
|
+
* and the Agent falls back to pi's own prompt. The guarantee here is a seam
|
|
144
|
+
* guarantee, not an end-to-end one.
|
|
145
|
+
*
|
|
146
|
+
* A Prompt Definition never reaches this function: `renderPolicyPrompt` above
|
|
147
|
+
* replaced it with its text once, at the top of the open path, so no consumer —
|
|
148
|
+
* the Ask hash included — can ever see a function (ADR-0064).
|
|
31
149
|
*/
|
|
32
150
|
export function openRequest(options: OpenRequestOptions): OpenOptions {
|
|
33
151
|
const spawnOptions = options.spawnOptions;
|
|
@@ -37,7 +155,9 @@ export function openRequest(options: OpenRequestOptions): OpenOptions {
|
|
|
37
155
|
cwd: options.cwd,
|
|
38
156
|
name: options.name,
|
|
39
157
|
...(model === undefined ? {} : { model }),
|
|
40
|
-
|
|
158
|
+
systemPrompt:
|
|
159
|
+
spawnOptions.systemPrompt ??
|
|
160
|
+
yaagSystemPrompt({ agent: { name: options.name, runName: options.runName } }),
|
|
41
161
|
...(thinking === undefined ? {} : { thinking }),
|
|
42
162
|
...(spawnOptions.appendSystemPrompt === undefined
|
|
43
163
|
? {}
|
|
@@ -72,7 +192,7 @@ export function openRequest(options: OpenRequestOptions): OpenOptions {
|
|
|
72
192
|
|
|
73
193
|
/** Replaces the caller's `model`/`thinking` forms with one attempt's settled selection. */
|
|
74
194
|
export function withSelection(
|
|
75
|
-
options:
|
|
195
|
+
options: RenderedSpawnOptions,
|
|
76
196
|
selection: ModelSelection,
|
|
77
197
|
): ResolvedSpawnOptions {
|
|
78
198
|
// `parent` is dropped here as well: a Handle must never reach recorded options.
|
package/src/agent/spawn.ts
CHANGED
|
@@ -21,6 +21,8 @@ export interface SpawnDependencies {
|
|
|
21
21
|
readonly emit: EventSink;
|
|
22
22
|
readonly sessionDir: string | undefined;
|
|
23
23
|
readonly programFile: string | undefined;
|
|
24
|
+
/** The Run name every Agent System Prompt of this Run prints. */
|
|
25
|
+
readonly runName: string;
|
|
24
26
|
/** Effective Config entries, in merge order; empty when the Run has no config. */
|
|
25
27
|
readonly configExtensions: readonly ConfigExtension[];
|
|
26
28
|
/** The Profile this Run activated; absent for a program without Profiles. */
|
|
@@ -54,6 +56,7 @@ export function makeSpawn(deps: SpawnDependencies): SpawnGate {
|
|
|
54
56
|
parent: request.parent,
|
|
55
57
|
askDefaults: request.askDefaults,
|
|
56
58
|
definitionName: undefined,
|
|
59
|
+
inheritedPrompt: request.inheritedPrompt,
|
|
57
60
|
fork: request.fork,
|
|
58
61
|
});
|
|
59
62
|
if (request.compact !== undefined && request.compact !== false) {
|
package/src/ask/ask-hash.ts
CHANGED
|
@@ -32,6 +32,13 @@ export function askHash(options: AskHashOptions): string {
|
|
|
32
32
|
JSON.stringify({
|
|
33
33
|
cwd: spawnOptions.cwd ?? null,
|
|
34
34
|
model: spawnOptions.model ?? null,
|
|
35
|
+
// An unset `systemPrompt` hashes as null on purpose, even though the
|
|
36
|
+
// Agent really reads the Agent System Prompt: that default is filled in
|
|
37
|
+
// downstream, at the open-request seam, and never reaches
|
|
38
|
+
// `ResolvedSpawnOptions`. Hashing it would make Ask identity depend on
|
|
39
|
+
// the Agent name and the Run name the text embeds, which the Ask marker
|
|
40
|
+
// context strips deliberately. Spawn identity, not Ask identity, carries
|
|
41
|
+
// the Agent System Prompt (ADR-0055, ADR-0064).
|
|
35
42
|
systemPrompt: spawnOptions.systemPrompt ?? null,
|
|
36
43
|
...(spawnOptions.thinking === undefined ? {} : { thinking: spawnOptions.thinking }),
|
|
37
44
|
...(spawnOptions.appendSystemPrompt === undefined
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
import type { AskOptions } from "./handle.ts";
|
|
10
10
|
import type { ModelSpec, ThinkingSpec } from "./model-spec.ts";
|
|
11
|
+
import type { LiteralPromptText, SystemPromptDefinition } from "./system-prompt.ts";
|
|
11
12
|
|
|
12
13
|
/** What one Agent is, independent of where it runs. */
|
|
13
14
|
export interface AgentPolicy {
|
|
@@ -33,11 +34,21 @@ export interface AgentPolicy {
|
|
|
33
34
|
*/
|
|
34
35
|
readonly thinking?: ThinkingSpec;
|
|
35
36
|
/**
|
|
36
|
-
* Replaces
|
|
37
|
-
*
|
|
37
|
+
* Replaces the Agent System Prompt, the text an Agent reads by default. The
|
|
38
|
+
* text is passed to the Agent literally, so it also removes the runtime
|
|
39
|
+
* facts, the tool list and the Rules the Agent System Prompt states.
|
|
40
|
+
*
|
|
41
|
+
* A `defineSystemPrompt(...)` definition instead builds the text at spawn
|
|
42
|
+
* time, with the Agent name and the Render Sentinels of the Prompt Context
|
|
43
|
+
* (ADR-0064). yaag renders it once per spawn; every consumer downstream of
|
|
44
|
+
* the spawn-request seam sees the rendered string.
|
|
45
|
+
*
|
|
46
|
+
* A value from `yaagSystemPrompt()` or from a `ctx.render*()` call belongs
|
|
47
|
+
* inside a `defineSystemPrompt` definition: the type rejects it here, and
|
|
48
|
+
* yaag also rejects it at run time, before the Agent starts (ADR-0060).
|
|
38
49
|
*/
|
|
39
|
-
readonly systemPrompt?:
|
|
40
|
-
/** Adds text
|
|
50
|
+
readonly systemPrompt?: LiteralPromptText | SystemPromptDefinition;
|
|
51
|
+
/** Adds text inside the Agent System Prompt, which stays. */
|
|
41
52
|
readonly appendSystemPrompt?: string;
|
|
42
53
|
/**
|
|
43
54
|
* Restores pi discovery. Omission or false selects ADR-0026's hermetic baseline.
|
|
@@ -66,4 +66,4 @@ export type YaagErrorCode =
|
|
|
66
66
|
| "RUN_STOPPED" // the Run's abort signal fired before the program completed
|
|
67
67
|
| "WORKTREE_REFUSED" // base cwd is not a clean Git repository
|
|
68
68
|
| "MODEL_RESOLUTION_FAILED" // the resolver gave up, or every candidate failed; carries the history
|
|
69
|
-
| "SPAWN_FAILED"; //
|
|
69
|
+
| "SPAWN_FAILED"; // the spawn was refused, e.g. a guarded system prompt, a broken tool contract or a startup timeout
|
package/src/contract/index.ts
CHANGED
|
@@ -33,3 +33,13 @@ export type {
|
|
|
33
33
|
SpawnOptions,
|
|
34
34
|
SpawnOverrides,
|
|
35
35
|
} from "./spawn-options.ts";
|
|
36
|
+
export type {
|
|
37
|
+
LiteralPromptText,
|
|
38
|
+
PromptAudience,
|
|
39
|
+
PromptText,
|
|
40
|
+
SystemPromptAgent,
|
|
41
|
+
SystemPromptBuilder,
|
|
42
|
+
SystemPromptContext,
|
|
43
|
+
SystemPromptDefinition,
|
|
44
|
+
SystemPromptInput,
|
|
45
|
+
} from "./system-prompt.ts";
|
|
@@ -35,10 +35,16 @@ export interface SpawnOptions extends AgentPolicy {
|
|
|
35
35
|
* Link to a name, which travels in `OpenOptions` and stays out of Ask identity.
|
|
36
36
|
* `askDefaults` is omitted too: the Ask hash sees the defaults through the
|
|
37
37
|
* merged Ask options, so they never enter the recorded spawn options.
|
|
38
|
+
*
|
|
39
|
+
* `systemPrompt` narrows to a string: a Prompt Definition renders at the
|
|
40
|
+
* spawn-request seam, so no consumer of this shape can ever see a function
|
|
41
|
+
* (ADR-0064). An unset prompt stays unset here; the default text is filled in
|
|
42
|
+
* `openRequest` alone, so the Ask hash keeps hashing `systemPrompt: null`.
|
|
38
43
|
*/
|
|
39
44
|
export interface ResolvedSpawnOptions
|
|
40
|
-
extends Omit<SpawnOptions, "model" | "thinking" | "parent" | "askDefaults"> {
|
|
45
|
+
extends Omit<SpawnOptions, "model" | "thinking" | "parent" | "askDefaults" | "systemPrompt"> {
|
|
41
46
|
readonly model?: string;
|
|
47
|
+
readonly systemPrompt?: string;
|
|
42
48
|
readonly thinking?: ThinkingLevel;
|
|
43
49
|
}
|
|
44
50
|
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The author surface of a Prompt Definition (ADR-0057, ADR-0058, ADR-0064).
|
|
3
|
+
*
|
|
4
|
+
* Types only, and no import outside this directory (ADR-0045). The values that
|
|
5
|
+
* build a Prompt Definition live in `system-prompt/define-system-prompt.ts`.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** Brand of a rendered prompt text; no program can build the symbol. */
|
|
9
|
+
declare const renderedPromptText: unique symbol;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* What `yaagSystemPrompt` and every `ctx.render*()` call return: a string that
|
|
13
|
+
* holds Render Sentinels. It is a `string` everywhere, except where a literal
|
|
14
|
+
* prompt is expected — that is the compile-time half of ADR-0060.
|
|
15
|
+
*/
|
|
16
|
+
export type PromptText = string & { readonly [renderedPromptText]: "yaag" };
|
|
17
|
+
|
|
18
|
+
/** A `systemPrompt` string a program wrote itself, so not a `PromptText`. */
|
|
19
|
+
export type LiteralPromptText = string & { readonly [renderedPromptText]?: undefined };
|
|
20
|
+
|
|
21
|
+
/** Who reads what an Agent writes. `"user"` is not reachable from a program yet. */
|
|
22
|
+
export type PromptAudience = "program" | "user";
|
|
23
|
+
|
|
24
|
+
/** What a Prompt Definition learns about the Agent it writes the prompt for. */
|
|
25
|
+
export interface SystemPromptAgent {
|
|
26
|
+
/** The Agent name, as the Run allocated it. */
|
|
27
|
+
readonly name: string;
|
|
28
|
+
/** The Run name, the same value `run_start.program` carries. */
|
|
29
|
+
readonly runName: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The Prompt Context: one method per Prompt Variable.
|
|
34
|
+
*
|
|
35
|
+
* Every method is **write-only**. It returns a Render Sentinel, an opaque
|
|
36
|
+
* marker, and not the text behind it: the facts exist only inside the Agent
|
|
37
|
+
* process, and the private prompt extension puts them in there. Put the result
|
|
38
|
+
* in the prompt; do not read it, match it, or split on it.
|
|
39
|
+
*/
|
|
40
|
+
export interface SystemPromptContext {
|
|
41
|
+
/** Writes the model the Agent runs on, as `provider/id`. */
|
|
42
|
+
renderModel(): PromptText;
|
|
43
|
+
/** Writes the Agent process id. */
|
|
44
|
+
renderPid(): PromptText;
|
|
45
|
+
/** Writes the time the Agent started, in ISO 8601. */
|
|
46
|
+
renderStartedAt(): PromptText;
|
|
47
|
+
/** Writes the time of the current turn, in ISO 8601. */
|
|
48
|
+
renderCurrentDateTime(): PromptText;
|
|
49
|
+
/** Writes the Agent working directory. */
|
|
50
|
+
renderCwd(): PromptText;
|
|
51
|
+
/** Writes the tool list of the Agent, one line per tool. */
|
|
52
|
+
renderTools(): PromptText;
|
|
53
|
+
/** Writes the user's `APPEND_SYSTEM.md`. */
|
|
54
|
+
renderAppendSystem(): PromptText;
|
|
55
|
+
/** Writes the text the program set with `appendSystemPrompt`. */
|
|
56
|
+
renderProgramAppend(): PromptText;
|
|
57
|
+
/** Writes the context files pi loaded, in pi's own block format. */
|
|
58
|
+
renderContextFiles(): PromptText;
|
|
59
|
+
/** Writes the skills pi loaded, in pi's own block format. */
|
|
60
|
+
renderSkills(): PromptText;
|
|
61
|
+
/** Writes the default system prompt pi would have built alone. */
|
|
62
|
+
renderPiDefaultPrompt(): PromptText;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The one object a Prompt Definition builder receives. */
|
|
66
|
+
export interface SystemPromptInput {
|
|
67
|
+
/** The write-only Prompt Context. */
|
|
68
|
+
readonly ctx: SystemPromptContext;
|
|
69
|
+
/** The Agent this prompt is for. */
|
|
70
|
+
readonly agent: SystemPromptAgent;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Builds one Agent's system prompt. It must be pure and synchronous, and it
|
|
75
|
+
* runs in the Orchestrator exactly once per spawn.
|
|
76
|
+
*/
|
|
77
|
+
export type SystemPromptBuilder = (input: SystemPromptInput) => string;
|
|
78
|
+
|
|
79
|
+
/** Brand of a Prompt Definition; no program can build the symbol. */
|
|
80
|
+
declare const systemPromptDefinition: unique symbol;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The opaque, frozen result of `defineSystemPrompt`. It is a value a spawn
|
|
84
|
+
* carries, not a string: yaag renders it at the spawn-request seam.
|
|
85
|
+
*/
|
|
86
|
+
export interface SystemPromptDefinition {
|
|
87
|
+
readonly [systemPromptDefinition]: true;
|
|
88
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -181,6 +181,11 @@ export type {
|
|
|
181
181
|
RunSummary,
|
|
182
182
|
} from "./summary/index.ts";
|
|
183
183
|
export { applyEvent, initialSummary } from "./summary/index.ts";
|
|
184
|
+
export type { YaagSystemPromptOptions } from "./system-prompt/index.ts";
|
|
185
|
+
// `isSystemPromptDefinition` and `renderSystemPrompt` stay on
|
|
186
|
+
// `system-prompt/index.ts`: the spawn seam uses them, and no consumer outside
|
|
187
|
+
// this package does.
|
|
188
|
+
export { defineSystemPrompt, yaagSystemPrompt } from "./system-prompt/index.ts";
|
|
184
189
|
export type {
|
|
185
190
|
AgentStats,
|
|
186
191
|
AgentTransport,
|
|
@@ -219,10 +224,18 @@ export type {
|
|
|
219
224
|
AskOptions,
|
|
220
225
|
ForkOptions,
|
|
221
226
|
Handle,
|
|
227
|
+
LiteralPromptText,
|
|
228
|
+
PromptAudience,
|
|
229
|
+
PromptText,
|
|
222
230
|
ResolvedSpawnOptions,
|
|
223
231
|
SpawnOptions,
|
|
224
232
|
SpawnOverrides,
|
|
225
233
|
StructuredAskOptions,
|
|
234
|
+
SystemPromptAgent,
|
|
235
|
+
SystemPromptBuilder,
|
|
236
|
+
SystemPromptContext,
|
|
237
|
+
SystemPromptDefinition,
|
|
238
|
+
SystemPromptInput,
|
|
226
239
|
ThinkingLevel,
|
|
227
240
|
} from "./types.ts";
|
|
228
241
|
export {
|
package/src/public-api.ts
CHANGED
|
@@ -16,9 +16,17 @@ export type {
|
|
|
16
16
|
AgentPolicy,
|
|
17
17
|
AskOptions,
|
|
18
18
|
Handle,
|
|
19
|
+
LiteralPromptText,
|
|
20
|
+
PromptAudience,
|
|
21
|
+
PromptText,
|
|
19
22
|
SpawnOptions,
|
|
20
23
|
SpawnOverrides,
|
|
21
24
|
StructuredAskOptions,
|
|
25
|
+
SystemPromptAgent,
|
|
26
|
+
SystemPromptBuilder,
|
|
27
|
+
SystemPromptContext,
|
|
28
|
+
SystemPromptDefinition,
|
|
29
|
+
SystemPromptInput,
|
|
22
30
|
ThinkingLevel,
|
|
23
31
|
} from "./contract/index.ts";
|
|
24
32
|
export type { YaagErrorCode } from "./errors.ts";
|
|
@@ -46,3 +54,6 @@ export { prompt } from "./prompt/prompt.ts";
|
|
|
46
54
|
export type { OrchestrationProgram, ProgramDefinition } from "./run/define-run.ts";
|
|
47
55
|
export { defineRun } from "./run/define-run.ts";
|
|
48
56
|
export type { RunContext } from "./run/run-context.ts";
|
|
57
|
+
export type { YaagSystemPromptOptions } from "./system-prompt/agent-system-prompt.ts";
|
|
58
|
+
export { yaagSystemPrompt } from "./system-prompt/agent-system-prompt.ts";
|
|
59
|
+
export { defineSystemPrompt } from "./system-prompt/define-system-prompt.ts";
|
package/src/run/run.ts
CHANGED
|
@@ -204,6 +204,9 @@ export async function executeRun<Args, Result>(
|
|
|
204
204
|
askAdmission,
|
|
205
205
|
sessionDir: options.sessionDir,
|
|
206
206
|
programFile: options.programFile,
|
|
207
|
+
// The same value `run_start.program` carries, so an Agent's prompt names
|
|
208
|
+
// the Run the reader sees in the Run record.
|
|
209
|
+
runName: definition.name ?? "program",
|
|
207
210
|
configExtensions: options.config?.extensions ?? EMPTY_EFFECTIVE_CONFIG.extensions,
|
|
208
211
|
...(profile === undefined ? {} : { profile }),
|
|
209
212
|
});
|