forked from deepseek-ai/deepseek-harness
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
338 lines (315 loc) · 13.4 KB
/
Copy pathindex.ts
File metadata and controls
338 lines (315 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/**
* Model-facing `get_goal`, `create_goal`, and `update_goal` tools over the
* persisted same-session goal domain.
* @module @deepseek-ai/dsh-tool-goal
*/
import type { Context } from '@deepseek-ai/cordis'
import z from '@deepseek-ai/schemastery'
import { GoalId } from '@deepseek-ai/dsh-goal'
import type { GoalRef, GoalView } from '@deepseek-ai/dsh-goal'
import { boundContextSummary, createUserMessage, HarnessError } from '@deepseek-ai/dsh-llm'
import { defineTool } from '@deepseek-ai/dsh-tools'
import type { GenericCallView } from '@deepseek-ai/dsh-tools'
import type {} from '@deepseek-ai/dsh-system-prompt'
import {
completionAuthority,
goalToolExecution,
requireDirectHuman,
} from './authority.ts'
import { renderWrapupContext } from './wrapup.ts'
export const name = 'tool-goal'
export const inject = ['agents', 'goals', 'tools', 'systemPrompt']
/** Model policy and hard lower bounds for goal-state updates. */
export interface Config {
/** Minimum admitted goal rounds before the model may self-report `blocked`. */
blockedAfterConsecutiveRounds?: number
}
/** Schemastery config for the goal-tool policy. */
export const Config: z<Config> = z.object({
blockedAfterConsecutiveRounds: z.number().step(1).min(1).default(3),
})
/** Fully materialized tool policy. */
interface ResolvedConfig {
readonly blockedAfterConsecutiveRounds: number
}
type UpdateAction = 'edit' | 'pause' | 'resume' | 'complete' | 'blocked'
const UPDATE_ACTIONS: UpdateAction[] = ['edit', 'pause', 'resume', 'complete', 'blocked']
const CREATE_DESCRIPTION =
'Create one persisted same-session completion goal when the current direct human request '
+ 'is a long-running objective that should continue across autonomous goal rounds. You may '
+ 'infer that intent without requiring the user to say "create a goal". Do not use this for '
+ 'trivial single-turn work. Execution rejects non-human and subagent authority.'
const GET_DESCRIPTION =
'Read the current same-session goal, including its exact id/revision, objective, phase, completed '
+ 'continuation rounds, round limit, blocker reason when present, and whether another continuation is armed. '
+ 'Call this before updating a goal.'
/** Canonical goal-tool output, matching the existing compact Native JSON. */
type GoalToolValue =
| { goal: null }
| {
goal: {
id: string
revision: number
objective: string
phase: GoalView['phase']
roundsStarted: number
maxGoalRounds: number
blockedReason?: { code: string; message: string }
}
activation: GoalView['activation']
}
const GOAL_VALUE_SCHEMA = {
oneOf: [
{
type: 'object',
additionalProperties: false,
properties: {
goal: { type: 'null', required: true },
},
},
{
type: 'object',
additionalProperties: false,
properties: {
goal: {
type: 'object',
additionalProperties: false,
required: true,
properties: {
id: { type: 'string', required: true },
revision: { type: 'integer', required: true },
objective: { type: 'string', required: true },
phase: { type: 'string', required: true, enum: ['active', 'paused', 'blocked', 'complete'] },
roundsStarted: { type: 'integer', required: true },
maxGoalRounds: { type: 'integer', required: true },
blockedReason: {
type: 'object',
additionalProperties: false,
properties: {
code: { type: 'string', required: true },
message: { type: 'string', required: true },
},
},
},
},
activation: { type: 'string', required: true, enum: ['armed', 'disarmed'] },
},
},
],
} as const
/** Render policy guidance with its deployment-selected blocked threshold. */
function guidance(blockedAfter: number): string {
return 'Use goal tools for one long-running completion objective in the current session. '
+ 'create_goal may infer goal intent from a direct human request in any language; do not '
+ 'create a goal for routine single-turn work. Call get_goal before update_goal and copy its '
+ 'exact goal_id and revision. After session resume or fork, an active goal is disarmed: when '
+ 'a human asks to continue or resume in any wording or language, use update_goal action '
+ 'resume to rearm it. Mark complete only when the objective is actually achieved. Mark '
+ `blocked only after the same blocking condition persists for at least ${blockedAfter} `
+ 'consecutive goal rounds, and report that concrete condition in blocked_reason; difficulty, uncertainty, '
+ 'or useful remaining work is not blocked.'
}
/** Validate config even when apply is called directly outside Loader normalization. */
function resolveConfig(config: Config): ResolvedConfig {
const blockedAfter = config.blockedAfterConsecutiveRounds ?? 3
if (!Number.isSafeInteger(blockedAfter) || blockedAfter < 1) {
throw new TypeError('blockedAfterConsecutiveRounds must be a positive safe integer')
}
return { blockedAfterConsecutiveRounds: blockedAfter }
}
/** Whether optional text is meaningful rather than a strict-schema empty filler. */
function hasText(value: string | undefined): value is string {
return value !== undefined && value !== ''
}
/** Whether an optional round cap is meaningful rather than a strict-schema zero filler. */
function hasRoundCap(value: number | undefined): value is number {
return value !== undefined && value !== 0
}
/** Build the exact compare-and-set ref from model arguments. */
function goalRef(goalId: string, revision: number): GoalRef {
if (goalId.length === 0 || goalId !== goalId.trim()
|| !Number.isSafeInteger(revision) || revision < 1) {
throw new HarnessError(
'goal_id must be non-empty and revision must be a positive safe integer',
'GOAL_TOOL_INVALID_UPDATE',
)
}
return { id: GoalId(goalId), revision }
}
/** Stable compact model result; activation is an observation, not replay state. */
function goalValue(goal: GoalView | undefined): GoalToolValue {
if (goal === undefined) return { goal: null }
return {
goal: {
id: goal.id,
revision: goal.revision,
objective: goal.objective,
phase: goal.phase,
roundsStarted: goal.roundsStarted,
maxGoalRounds: goal.maxGoalRounds,
...goal.blockedReason === undefined ? {} : {
blockedReason: { code: goal.blockedReason.code, message: goal.blockedReason.message },
},
},
activation: goal.activation,
}
}
/** Reusable canonical output declaration for all three goal controls. */
const GOAL_OUTPUT = {
schema: GOAL_VALUE_SCHEMA,
render: (_args: unknown, value: GoalToolValue) => [{ type: 'text' as const, text: JSON.stringify(value) }],
}
/** Generic, args-only pending presentation shared by the goal tools. */
function present(title: string, kind: 'read' | 'other', rawInput?: unknown): GenericCallView {
return { card: 'generic', title, kind, ...rawInput === undefined ? {} : { rawInput } }
}
/** Register the three Codex-shaped goal tools and their shared policy section. */
export function apply(ctx: Context, config: Config): void {
const resolved = resolveConfig(config)
ctx.systemPrompt.section({
name: 'tool:goal',
order: 114,
text: guidance(resolved.blockedAfterConsecutiveRounds),
})
ctx.tools.register(defineTool({
name: 'get_goal',
description: GET_DESCRIPTION,
parameters: {},
output: GOAL_OUTPUT,
execute(_args, exec) {
const execution = goalToolExecution(ctx, exec)
return Promise.resolve(goalValue(ctx.goals.get(execution.agent)))
},
presentCall: () => present('Read current goal', 'read'),
}))
ctx.tools.register(defineTool({
name: 'create_goal',
description: CREATE_DESCRIPTION,
parameters: {
objective: {
type: 'string',
required: true,
description: 'The concrete completion objective inferred from the direct human request.',
},
max_goal_rounds: {
type: 'number',
description: 'Optional positive safe-integer limit on automatic continuation rounds.',
},
},
output: GOAL_OUTPUT,
execute(args, exec) {
const execution = goalToolExecution(ctx, exec)
requireDirectHuman(ctx, execution)
const goal = ctx.goals.create(execution.agent, {
objective: args.objective,
...args.max_goal_rounds === undefined ? {} : { maxGoalRounds: args.max_goal_rounds },
})
return Promise.resolve(goalValue(goal))
},
presentCall: args => present('Create goal', 'other', args.objective),
}))
ctx.tools.register(defineTool({
name: 'update_goal',
description: 'Update the exact current goal revision. edit, pause, and resume require a direct '
+ 'top-level human request. During an automatic continuation of the current goal, complete '
+ 'and blocked are also allowed. blocked is rejected before the configured minimum round count; the model remains '
+ 'responsible for judging that the same condition persisted across those rounds and must explain it in blocked_reason.',
parameters: {
goal_id: { type: 'string', required: true, description: 'Exact id returned by get_goal.' },
revision: { type: 'number', required: true, description: 'Exact positive revision returned by get_goal.' },
action: {
type: 'string',
required: true,
enum: UPDATE_ACTIONS,
description: 'edit | pause | resume | complete | blocked',
},
objective: { type: 'string', description: 'Replacement objective; valid only with action edit.' },
max_goal_rounds: { type: 'number', description: 'Replacement cap; valid only with action edit.' },
blocked_reason: {
type: 'string',
description: 'Concrete blocking condition; required only with action blocked.',
},
},
output: GOAL_OUTPUT,
execute(args, exec) {
const execution = goalToolExecution(ctx, exec)
const ref = goalRef(args.goal_id, args.revision)
const replacements = {
...hasText(args.objective) ? { objective: args.objective } : {},
...hasRoundCap(args.max_goal_rounds) ? { maxGoalRounds: args.max_goal_rounds } : {},
}
if (args.action === 'edit') {
requireDirectHuman(ctx, execution)
if (hasText(args.blocked_reason)) {
throw new HarnessError('blocked_reason is valid only with action blocked', 'GOAL_TOOL_INVALID_UPDATE')
}
const goal = ctx.goals.edit(execution.agent, ref, replacements)
return Promise.resolve(goalValue(goal))
}
if (args.action === 'pause' || args.action === 'resume') {
requireDirectHuman(ctx, execution)
if (hasText(args.objective) || hasRoundCap(args.max_goal_rounds) || hasText(args.blocked_reason)) {
throw new HarnessError(
'objective and max_goal_rounds are valid only with action edit; blocked_reason is valid only with action blocked',
'GOAL_TOOL_INVALID_UPDATE',
)
}
const goal = args.action === 'pause'
? ctx.goals.pause(execution.agent, ref)
: ctx.goals.resume(execution.agent, ref)
return Promise.resolve(goalValue(goal))
}
const authority = completionAuthority(ctx, execution)
if (hasText(args.objective) || hasRoundCap(args.max_goal_rounds)) {
throw new HarnessError(
'objective and max_goal_rounds are valid only with action edit',
'GOAL_TOOL_INVALID_UPDATE',
)
}
if (args.action === 'complete' && hasText(args.blocked_reason)) {
throw new HarnessError('blocked_reason is valid only with action blocked', 'GOAL_TOOL_INVALID_UPDATE')
}
if (args.action === 'blocked'
&& (args.blocked_reason === undefined || args.blocked_reason.trim().length === 0)) {
throw new HarnessError('blocked_reason is required with action blocked', 'GOAL_TOOL_INVALID_UPDATE')
}
if (args.action === 'blocked' && authority.kind === 'goal-round'
&& authority.goal.roundsStarted < resolved.blockedAfterConsecutiveRounds) {
throw new HarnessError(
`blocked requires at least ${resolved.blockedAfterConsecutiveRounds} consecutive goal rounds; `
+ `current round is ${authority.goal.roundsStarted}`,
'GOAL_TOOL_BLOCK_THRESHOLD',
)
}
const goal = args.action === 'complete'
? ctx.goals.complete(execution.agent, ref)
: ctx.goals.block(execution.agent, ref, {
code: 'model-reported',
message: args.blocked_reason as string,
})
if (authority.kind === 'goal-round') {
exec.deferContext(createUserMessage({
content: args.action === 'complete'
? renderWrapupContext(goal.objective)
: renderWrapupContext(goal.objective, args.blocked_reason as string),
source: {
kind: 'plugin',
plugin: 'tool-goal',
form: 'notice',
summary: boundContextSummary(`${args.action as string}: ${goal.objective}`),
},
}))
}
return Promise.resolve(goalValue(goal))
},
presentCall: args => present(
`${args.action === 'blocked' ? 'Mark' : args.action.charAt(0).toUpperCase() + args.action.slice(1)} goal`,
'other',
hasText(args.blocked_reason)
? args.blocked_reason
: hasText(args.objective)
? args.objective
: hasRoundCap(args.max_goal_rounds) ? args.max_goal_rounds : args.goal_id,
),
}))
}