@operad-io/dev 0.1.1 → 0.3.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/README.md +1 -1
- package/dev.schema.json +53 -31
- package/package.json +9 -6
- package/skills/git/SKILL.md +70 -0
- package/skills/setup/SKILL.md +74 -0
- package/src/access-grants.ts +59 -0
- package/src/actions.ts +81 -17
- package/src/announce.ts +134 -17
- package/src/chat.ts +586 -36
- package/src/check-host.ts +111 -0
- package/src/check-load.ts +67 -0
- package/src/check.ts +45 -22
- package/src/clock.ts +29 -0
- package/src/clone-chat.ts +32 -0
- package/src/commands.ts +103 -34
- package/src/create-actions.ts +210 -0
- package/src/flaky.ts +45 -29
- package/src/flow-compiler.ts +156 -69
- package/src/followups.ts +20 -22
- package/src/gate.ts +254 -88
- package/src/git-state.ts +155 -0
- package/src/git.ts +92 -4
- package/src/index.ts +52 -6
- package/src/integrate.ts +162 -62
- package/src/integration-queue.ts +23 -10
- package/src/lock.ts +101 -5
- package/src/mainclone.ts +61 -5
- package/src/model-error.ts +70 -0
- package/src/model.ts +61 -35
- package/src/notcode.ts +51 -15
- package/src/places.ts +2 -2
- package/src/prepare.ts +7 -2
- package/src/project-flows.ts +135 -4
- package/src/project-settings.ts +110 -3
- package/src/register-requests.ts +121 -0
- package/src/repository-key.ts +4 -0
- package/src/rerun.ts +23 -7
- package/src/retry.ts +2 -12
- package/src/rework.ts +8 -0
- package/src/schedule.ts +342 -93
- package/src/service.ts +241 -185
- package/src/settings.ts +409 -65
- package/src/setup-git.ts +366 -0
- package/src/standing.ts +23 -6
- package/src/step-edge.ts +22 -0
- package/src/steps.ts +945 -179
- package/src/throughput.ts +4 -21
- package/src/timeouts.ts +10 -2
- package/src/views.ts +325 -86
- package/src/waits.ts +59 -0
package/src/chat.ts
CHANGED
|
@@ -5,18 +5,56 @@
|
|
|
5
5
|
* changes reach main only through a task, through integrate.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import {
|
|
9
|
-
import
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
8
|
+
import { messageOf } from "@operad-io/engine";
|
|
9
|
+
import {
|
|
10
|
+
ACCESS_ACTION_REQUEST,
|
|
11
|
+
CHAT_TYPE,
|
|
12
|
+
ACCESS_FACT,
|
|
13
|
+
ENTRY_FACT,
|
|
14
|
+
WORKTREE_EVENT,
|
|
15
|
+
WORKTREE_NOTE,
|
|
16
|
+
WORKTREE_REPLY_EVENT,
|
|
17
|
+
agentConditionals,
|
|
18
|
+
added,
|
|
19
|
+
type AccessChangedData,
|
|
20
|
+
type AgentChoices,
|
|
21
|
+
chatData,
|
|
22
|
+
chatRef,
|
|
23
|
+
createChat,
|
|
24
|
+
hasSkill,
|
|
25
|
+
isDirectory,
|
|
26
|
+
sendMessage,
|
|
27
|
+
skillsInside,
|
|
28
|
+
startingEffort,
|
|
29
|
+
startingModel,
|
|
30
|
+
titleFromMessage,
|
|
31
|
+
} from "@operad-io/chat";
|
|
32
|
+
import { defaultAccess } from "@operad-io/access-match";
|
|
33
|
+
import {
|
|
34
|
+
isRecord,
|
|
35
|
+
modelIds,
|
|
36
|
+
stringList,
|
|
37
|
+
type ActionArgsSchema,
|
|
38
|
+
type ActionContext,
|
|
39
|
+
type ActionTypeDefinition,
|
|
40
|
+
type HarnessModelInfo,
|
|
41
|
+
type JsonValue,
|
|
42
|
+
type ObjectStore,
|
|
43
|
+
type PluginBaseContext,
|
|
44
|
+
type PluginStepResult,
|
|
45
|
+
type StepContext,
|
|
16
46
|
} from "@operad-io/engine";
|
|
17
|
-
import { existsSync } from "node:fs";
|
|
47
|
+
import { existsSync, readFileSync, readdirSync, realpathSync, statSync } from "node:fs";
|
|
48
|
+
import { basename, join, relative, resolve, sep } from "node:path";
|
|
18
49
|
import { DevError, DevRefusal, repositoryData, type RepositoryData } from "./model.ts";
|
|
19
|
-
import {
|
|
50
|
+
import {
|
|
51
|
+
assertInsideRepository,
|
|
52
|
+
readDevSettings,
|
|
53
|
+
type DevSettings,
|
|
54
|
+
} from "./settings.ts";
|
|
55
|
+
import { git } from "./git.ts";
|
|
56
|
+
import { gitStateOf } from "./git-state.ts";
|
|
57
|
+
import { cloneHolder } from "./service.ts";
|
|
20
58
|
import { isRegistered, prepareWorktree, workBranch, worktreePath } from "./steps.ts";
|
|
21
59
|
|
|
22
60
|
/**
|
|
@@ -30,7 +68,11 @@ export const CHAT_HARNESSES = ["claude", "pi"] as const;
|
|
|
30
68
|
export const RADIAN_NOTE =
|
|
31
69
|
"radian is not offered as an agent here: it cannot stop a tool call before it starts and cannot continue a session in another directory, so it cannot keep the main clone read-only or move to a worktree; use it for a bot chat.";
|
|
32
70
|
|
|
33
|
-
/**
|
|
71
|
+
/**
|
|
72
|
+
* What a repository chat adds to the built-in default: read-only in the main
|
|
73
|
+
* clone, and no operad command. The default already holds the credential paths
|
|
74
|
+
* and the dangerous commands, so the chat keeps them at every level.
|
|
75
|
+
*/
|
|
34
76
|
export const CHAT_ACCESS: Readonly<Record<string, JsonValue>> = {
|
|
35
77
|
readOnly: true,
|
|
36
78
|
denyPaths: ["~/.ssh", "~/.gnupg", "~/.aws", "~/.config/gh"],
|
|
@@ -38,11 +80,297 @@ export const CHAT_ACCESS: Readonly<Record<string, JsonValue>> = {
|
|
|
38
80
|
denyCommands: ["operad"],
|
|
39
81
|
};
|
|
40
82
|
|
|
83
|
+
/** The step a repository chat calls at every reply to read its skills and access. */
|
|
84
|
+
export const CHAT_SETTINGS_STEP = "dev/chat_settings";
|
|
85
|
+
|
|
86
|
+
/** The action a repository chat runs before it changes its access level. */
|
|
87
|
+
export const CHAT_ACCESS_ACTION = "dev/chat_access";
|
|
88
|
+
|
|
89
|
+
/** The most bytes of the repository's instruction file a chat starts with. */
|
|
90
|
+
export const CHAT_CONTEXT_FILE_MAX_BYTES = 32 * 1024;
|
|
91
|
+
|
|
92
|
+
/** The most other agent file paths a chat starts with. */
|
|
93
|
+
export const CHAT_CONTEXT_PATH_MAX = 50;
|
|
94
|
+
|
|
95
|
+
/** What a repository chat's first session starts with: its place, and the same words alone. */
|
|
96
|
+
export interface RepositoryChatContext {
|
|
97
|
+
readonly [key: string]: unknown;
|
|
98
|
+
/** The place, the instructions and the other agent files. */
|
|
99
|
+
readonly text: string;
|
|
100
|
+
/** The place alone, for a chat that moved to its worktree after the text was built. */
|
|
101
|
+
readonly where: string;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function realPathOrUndefined(path: string): string | undefined {
|
|
105
|
+
try {
|
|
106
|
+
return realpathSync(path);
|
|
107
|
+
} catch {
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function insideRoot(root: string, path: string): boolean {
|
|
113
|
+
const real = realPathOrUndefined(path);
|
|
114
|
+
return real !== undefined && (real === root || real.startsWith(`${root}${sep}`));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** The instruction file at the root: `AGENTS.md`, else `CLAUDE.md`, never one outside through a link. */
|
|
118
|
+
function instructionFile(base: string, root: string): { readonly path: string; readonly text: string } | undefined {
|
|
119
|
+
for (const name of ["AGENTS.md", "CLAUDE.md"] as const) {
|
|
120
|
+
const path = join(base, name);
|
|
121
|
+
const real = realPathOrUndefined(path);
|
|
122
|
+
if (real === undefined || (real !== root && !real.startsWith(`${root}${sep}`))) continue;
|
|
123
|
+
let isFile = false;
|
|
124
|
+
try {
|
|
125
|
+
isFile = statSync(real).isFile();
|
|
126
|
+
} catch {
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (!isFile) continue;
|
|
130
|
+
const bytes = readFileSync(real);
|
|
131
|
+
if (bytes.length <= CHAT_CONTEXT_FILE_MAX_BYTES) return { path, text: bytes.toString("utf8") };
|
|
132
|
+
// Cut at a character boundary and then at the last line end before the limit.
|
|
133
|
+
let end = CHAT_CONTEXT_FILE_MAX_BYTES;
|
|
134
|
+
while (end > 0 && ((bytes[end] ?? 0) & 0xc0) === 0x80) end -= 1;
|
|
135
|
+
let text = bytes.subarray(0, end).toString("utf8");
|
|
136
|
+
const lineEnd = text.lastIndexOf("\n");
|
|
137
|
+
if (lineEnd >= 0) text = text.slice(0, lineEnd);
|
|
138
|
+
const note = `The file was cut at 32 KB; the whole file is at ${path}.`;
|
|
139
|
+
return { path, text: text.length === 0 ? note : `${text}\n\n${note}` };
|
|
140
|
+
}
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Every tracked `AGENTS.md` below the root, never the root's own and never one outside through a link. */
|
|
145
|
+
function trackedAgentFiles(base: string, root: string): string[] {
|
|
146
|
+
const listed = git(base, ["ls-files", "-z"], { raw: true });
|
|
147
|
+
if (!listed.ok) return [];
|
|
148
|
+
const found: string[] = [];
|
|
149
|
+
for (const path of listed.out.split("\0")) {
|
|
150
|
+
if (path.length === 0 || path === "AGENTS.md" || basename(path) !== "AGENTS.md") continue;
|
|
151
|
+
const absolute = join(base, path);
|
|
152
|
+
if (!insideRoot(root, absolute)) continue;
|
|
153
|
+
const real = realPathOrUndefined(absolute);
|
|
154
|
+
if (real === undefined) continue;
|
|
155
|
+
try {
|
|
156
|
+
if (!statSync(real).isFile()) continue;
|
|
157
|
+
} catch {
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
found.push(absolute);
|
|
161
|
+
}
|
|
162
|
+
return found;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function insideSkill(path: string, skills: readonly string[]): boolean {
|
|
166
|
+
const real = realPathOrUndefined(path);
|
|
167
|
+
if (real === undefined) return false;
|
|
168
|
+
return skills.some((skill) => {
|
|
169
|
+
const base = realPathOrUndefined(skill);
|
|
170
|
+
return base !== undefined && (real === base || real.startsWith(`${base}${sep}`));
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Every file under the root `.agents`, except those inside a skill directory the chat loads. */
|
|
175
|
+
function agentFiles(base: string, root: string, skills: readonly string[]): string[] {
|
|
176
|
+
const start = join(base, ".agents");
|
|
177
|
+
if (!insideRoot(root, start)) return [];
|
|
178
|
+
const found: string[] = [];
|
|
179
|
+
const seen = new Set<string>();
|
|
180
|
+
const walk = (path: string): void => {
|
|
181
|
+
const real = realPathOrUndefined(path);
|
|
182
|
+
if (real === undefined || (real !== root && !real.startsWith(`${root}${sep}`))) return;
|
|
183
|
+
let isDirectory = false;
|
|
184
|
+
try {
|
|
185
|
+
isDirectory = statSync(real).isDirectory();
|
|
186
|
+
} catch {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
if (isDirectory) {
|
|
190
|
+
if (seen.has(real)) return;
|
|
191
|
+
seen.add(real);
|
|
192
|
+
let entries;
|
|
193
|
+
try {
|
|
194
|
+
entries = readdirSync(path, { withFileTypes: true });
|
|
195
|
+
} catch {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
for (const entry of entries) walk(join(path, entry.name));
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
let isFile = false;
|
|
202
|
+
try {
|
|
203
|
+
isFile = statSync(real).isFile();
|
|
204
|
+
} catch {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
if (isFile && !insideSkill(path, skills)) found.push(path);
|
|
208
|
+
};
|
|
209
|
+
walk(start);
|
|
210
|
+
return found;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** The other agent files a chat reads by path, relative to its working directory, with no duplicates. */
|
|
214
|
+
function otherAgentFiles(base: string, root: string, skills: readonly string[]): string[] {
|
|
215
|
+
const absolute = new Set([...trackedAgentFiles(base, root), ...agentFiles(base, root, skills)]);
|
|
216
|
+
return [...absolute]
|
|
217
|
+
.map((path) => relative(base, path))
|
|
218
|
+
.sort((left, right) => left.localeCompare(right));
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* The text a repository chat's first session starts with: where it is, the
|
|
223
|
+
* repository's instruction file, and the paths of its other agent files. Built
|
|
224
|
+
* from the working directory the reply uses, so a worktree chat names its branch.
|
|
225
|
+
*/
|
|
226
|
+
export function repositoryChatContext(input: {
|
|
227
|
+
readonly repository: string;
|
|
228
|
+
readonly cwd: string;
|
|
229
|
+
readonly readOnly: boolean;
|
|
230
|
+
/** The chat works in the main clone on the current branch, at its access level (workIn branch). */
|
|
231
|
+
readonly inPlace?: boolean;
|
|
232
|
+
/** The task that holds the main clone on its own branch; the reply is read-only meanwhile. */
|
|
233
|
+
readonly heldBy?: string | undefined;
|
|
234
|
+
readonly branch?: string | undefined;
|
|
235
|
+
/** The folder has no git (no program, no repository or no commit): nothing records or undoes a change. */
|
|
236
|
+
readonly noGit?: boolean;
|
|
237
|
+
readonly skills: readonly string[];
|
|
238
|
+
readonly denyCommands: readonly string[];
|
|
239
|
+
}): RepositoryChatContext {
|
|
240
|
+
const root = realPathOrUndefined(input.cwd) ?? resolve(input.cwd);
|
|
241
|
+
const where = [
|
|
242
|
+
`You are answering in an operad chat about the repository ${input.repository}, in ${input.cwd}.`,
|
|
243
|
+
input.heldBy !== undefined
|
|
244
|
+
? `This chat is read-only in the main clone for now: the task ${input.heldBy} holds the clone on its own branch and is the only one that changes files there. Read and answer, but do not change files or run commands that change anything; when the person wants a change, tell them to wait until ${input.heldBy} is merged, cancelled or rejected, and then ask again.`
|
|
245
|
+
: input.inPlace === true
|
|
246
|
+
? "This chat works in the main clone, on the branch that is checked out, at its access level. Changes made here are changes to the repository itself; no worktree is opened."
|
|
247
|
+
: input.readOnly
|
|
248
|
+
? "This chat is read-only in the main clone. Commands that only read run at once; a call that changes a file asks the person first."
|
|
249
|
+
: `This chat works in its own worktree on the branch ${input.branch ?? "unknown"}. Changes reach main through a task, not by merging here.`,
|
|
250
|
+
...(input.noGit === true
|
|
251
|
+
? [
|
|
252
|
+
input.inPlace === true && input.heldBy === undefined
|
|
253
|
+
? "This folder has no git (no repository with a commit), so nothing records or undoes a change. The chat may change files here at its access level; say what you changed."
|
|
254
|
+
: "This folder has no git (no repository with a commit).",
|
|
255
|
+
]
|
|
256
|
+
: []),
|
|
257
|
+
...(input.denyCommands.length === 0
|
|
258
|
+
? []
|
|
259
|
+
: [`These commands are refused here: ${input.denyCommands.join(", ")}.`]),
|
|
260
|
+
...(input.denyCommands.includes("operad")
|
|
261
|
+
? ["Use the operad tools you were given to read plans, tasks and pending items."]
|
|
262
|
+
: []),
|
|
263
|
+
].join("\n");
|
|
264
|
+
const parts = [where];
|
|
265
|
+
const instructions = instructionFile(input.cwd, root);
|
|
266
|
+
if (instructions !== undefined) {
|
|
267
|
+
parts.push(`The repository's instructions follow. Follow them.\n\n${instructions.text}`);
|
|
268
|
+
}
|
|
269
|
+
const files = otherAgentFiles(input.cwd, root, input.skills);
|
|
270
|
+
if (files.length > 0) {
|
|
271
|
+
const shown = files.slice(0, CHAT_CONTEXT_PATH_MAX);
|
|
272
|
+
const lines = [
|
|
273
|
+
"Other agent files in this repository; read the ones that apply to what you work on:",
|
|
274
|
+
...shown,
|
|
275
|
+
...(files.length > CHAT_CONTEXT_PATH_MAX
|
|
276
|
+
? [`[${files.length - CHAT_CONTEXT_PATH_MAX} more files were left out.]`]
|
|
277
|
+
: []),
|
|
278
|
+
];
|
|
279
|
+
parts.push(lines.join("\n"));
|
|
280
|
+
}
|
|
281
|
+
return { where, text: parts.join("\n\n") };
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** True when the folder is known to have no git; a folder git cannot judge claims nothing. */
|
|
285
|
+
function folderHasNoGit(folder: string): boolean {
|
|
286
|
+
try {
|
|
287
|
+
const state = gitStateOf(folder).state;
|
|
288
|
+
return state === "no program" || state === "no repository" || state === "no commit";
|
|
289
|
+
} catch {
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** Why a worktree cannot be opened in the folder, or undefined when git is ready there. */
|
|
295
|
+
function worktreeRefusal(folder: string): string | undefined {
|
|
296
|
+
try {
|
|
297
|
+
const state = gitStateOf(folder);
|
|
298
|
+
if (state.state === "ready") return undefined;
|
|
299
|
+
if (state.state === "subfolder") {
|
|
300
|
+
return `this folder is inside the git repository at ${state.root}; a worktree needs the repository's own folder`;
|
|
301
|
+
}
|
|
302
|
+
return "this folder has no git yet, so a chat cannot get a worktree here";
|
|
303
|
+
} catch (error) {
|
|
304
|
+
return messageOf(error);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** The event dev sends after a live worktree yes, so the chat answers the same message there. */
|
|
309
|
+
export const CHAT_REPLY_EVENT = WORKTREE_REPLY_EVENT;
|
|
310
|
+
|
|
41
311
|
/** The directory name and branch suffix of a chat's worktree. */
|
|
42
312
|
export function chatWorktreeName(chatId: string): string {
|
|
43
313
|
return `chat-${chatId}`;
|
|
44
314
|
}
|
|
45
315
|
|
|
316
|
+
interface ChatSkillList {
|
|
317
|
+
/** The skill directories given to the agent. */
|
|
318
|
+
readonly directories: string[];
|
|
319
|
+
/** The directories a listed path names, denied for writing. */
|
|
320
|
+
readonly roots: string[];
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* The skills a repository gives its chats at `base` (the main clone or the
|
|
325
|
+
* chat's worktree), read now. A path may be a skill itself, a directory of
|
|
326
|
+
* skills (such as `.agents/skills`), or a `.agents` directory, whose `skills`
|
|
327
|
+
* directory is read the same way. Every skill directly inside is included, so
|
|
328
|
+
* one added later is read at the next reply.
|
|
329
|
+
*/
|
|
330
|
+
export function repositoryChatSkills(settings: DevSettings, base: string): ChatSkillList {
|
|
331
|
+
const directories: string[] = [];
|
|
332
|
+
const roots: string[] = [];
|
|
333
|
+
settings.skills.forEach((written, index) => {
|
|
334
|
+
const path = resolve(base, written);
|
|
335
|
+
assertInsideRepository(base, path, `skills[${index}]`);
|
|
336
|
+
if (hasSkill(path)) {
|
|
337
|
+
added(roots, path);
|
|
338
|
+
added(directories, path);
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
const agents = isDirectory(path) && basename(path) === ".agents";
|
|
342
|
+
const parent = agents ? join(path, "skills") : path;
|
|
343
|
+
if (isDirectory(path)) added(roots, parent);
|
|
344
|
+
for (const skill of skillsInside(parent)) added(directories, skill);
|
|
345
|
+
});
|
|
346
|
+
return { directories, roots };
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* The access profile of a repository chat. In the main clone its skills may be
|
|
351
|
+
* read but not changed; in its own worktree they are repository files the chat
|
|
352
|
+
* changes like any other, and the change reaches main through a task.
|
|
353
|
+
*/
|
|
354
|
+
export function chatAccess(skillPaths: readonly string[], readOnly: boolean): Record<string, JsonValue> {
|
|
355
|
+
const base = defaultAccess();
|
|
356
|
+
const denyPaths = [...(base.denyPaths ?? [])];
|
|
357
|
+
for (const path of stringList(CHAT_ACCESS.denyPaths)) if (!denyPaths.includes(path)) denyPaths.push(path);
|
|
358
|
+
const denyCommands = [...(base.denyCommands ?? [])];
|
|
359
|
+
for (const command of stringList(CHAT_ACCESS.denyCommands)) {
|
|
360
|
+
if (!denyCommands.includes(command)) denyCommands.push(command);
|
|
361
|
+
}
|
|
362
|
+
const protectedPaths = readOnly ? skillPaths : [];
|
|
363
|
+
for (const path of protectedPaths) if (!denyPaths.includes(path)) denyPaths.push(path);
|
|
364
|
+
return {
|
|
365
|
+
denyPaths,
|
|
366
|
+
denyCommands,
|
|
367
|
+
askCommands: [...(base.askCommands ?? [])],
|
|
368
|
+
askPaths: [...(base.askPaths ?? [])],
|
|
369
|
+
readOnly,
|
|
370
|
+
...(protectedPaths.length === 0 ? {} : { readPaths: [...protectedPaths] }),
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
|
|
46
374
|
/**
|
|
47
375
|
* The worktree name a task opened from chat `chatId` works in. The chat must be
|
|
48
376
|
* about this repository and must already have been given its worktree.
|
|
@@ -72,6 +400,7 @@ function chatArgs(
|
|
|
72
400
|
models: readonly string[],
|
|
73
401
|
efforts: readonly string[],
|
|
74
402
|
agents: readonly AgentChoices[] = [],
|
|
403
|
+
info?: Readonly<Record<string, HarnessModelInfo>> | undefined,
|
|
75
404
|
): ActionArgsSchema {
|
|
76
405
|
return {
|
|
77
406
|
type: "object",
|
|
@@ -85,6 +414,7 @@ function chatArgs(
|
|
|
85
414
|
model: {
|
|
86
415
|
type: "string",
|
|
87
416
|
...(models.length === 0 ? {} : { enum: [...models] }),
|
|
417
|
+
...(info === undefined ? {} : { info }),
|
|
88
418
|
description: "The model the agent uses; one from that agent's list. The agent's default when left out. It can be changed later.",
|
|
89
419
|
},
|
|
90
420
|
effort: {
|
|
@@ -118,10 +448,15 @@ const chatAction: ActionTypeDefinition = {
|
|
|
118
448
|
args: chatArgs([], []),
|
|
119
449
|
argsFor: (_object, env) => {
|
|
120
450
|
const offered = CHAT_HARNESSES.filter((name) => (env.harnesses ?? []).includes(name));
|
|
451
|
+
const info: Record<string, HarnessModelInfo> = {};
|
|
452
|
+
for (const name of offered) {
|
|
453
|
+
for (const [id, model] of Object.entries(env.models?.[name]?.info ?? {})) info[id] = model;
|
|
454
|
+
}
|
|
121
455
|
return chatArgs(
|
|
122
|
-
[...new Set(offered.flatMap((name) => env.models?.[name]
|
|
456
|
+
[...new Set(offered.flatMap((name) => modelIds(env.models?.[name])))],
|
|
123
457
|
[...new Set(offered.flatMap((name) => env.efforts?.[name]?.list ?? []))],
|
|
124
458
|
offered.map((name) => ({ name, models: env.models?.[name], efforts: env.efforts?.[name] })),
|
|
459
|
+
Object.keys(info).length === 0 ? undefined : info,
|
|
125
460
|
);
|
|
126
461
|
},
|
|
127
462
|
when: (state) => repositoryData(state).removed !== true,
|
|
@@ -151,9 +486,16 @@ const chatAction: ActionTypeDefinition = {
|
|
|
151
486
|
model = startingModel(harness, ctx.models[harness], input.model);
|
|
152
487
|
effort = startingEffort(harness, ctx.efforts[harness], input.effort);
|
|
153
488
|
} catch (error) {
|
|
154
|
-
throw new DevRefusal(
|
|
489
|
+
throw new DevRefusal(messageOf(error));
|
|
155
490
|
}
|
|
156
491
|
const effortDefault = ctx.efforts[harness]?.default ?? undefined;
|
|
492
|
+
const settings = await readDevSettings(repository.path);
|
|
493
|
+
let skills: ChatSkillList;
|
|
494
|
+
try {
|
|
495
|
+
skills = repositoryChatSkills(settings, repository.path);
|
|
496
|
+
} catch (error) {
|
|
497
|
+
throw new DevRefusal(messageOf(error));
|
|
498
|
+
}
|
|
157
499
|
const id = await createChat(
|
|
158
500
|
ctx.store,
|
|
159
501
|
{
|
|
@@ -163,7 +505,12 @@ const chatAction: ActionTypeDefinition = {
|
|
|
163
505
|
...(effort === undefined ? {} : { effort }),
|
|
164
506
|
...(effort === undefined || effortDefault === undefined ? {} : { effortDefault }),
|
|
165
507
|
cwd: repository.path,
|
|
166
|
-
access:
|
|
508
|
+
access: chatAccess(skills.roots, settings.workIn !== "branch"),
|
|
509
|
+
accessLevel: "plan",
|
|
510
|
+
accessDefault: "plan",
|
|
511
|
+
skills: skills.directories,
|
|
512
|
+
refresh: CHAT_SETTINGS_STEP,
|
|
513
|
+
accessAction: CHAT_ACCESS_ACTION,
|
|
167
514
|
title:
|
|
168
515
|
input.title ??
|
|
169
516
|
(input.message === undefined ? undefined : titleFromMessage(input.message)) ??
|
|
@@ -176,31 +523,159 @@ const chatAction: ActionTypeDefinition = {
|
|
|
176
523
|
},
|
|
177
524
|
};
|
|
178
525
|
|
|
179
|
-
/** The actions of the chat part of dev. */
|
|
180
|
-
export const chatActions: readonly ActionTypeDefinition[] = [chatAction];
|
|
181
|
-
|
|
182
526
|
function failed(message: string): PluginStepResult {
|
|
183
527
|
return { result: "failed", data: { message } };
|
|
184
528
|
}
|
|
185
529
|
|
|
186
530
|
/**
|
|
187
|
-
* Step `dev/
|
|
188
|
-
*
|
|
189
|
-
*
|
|
531
|
+
* Step `dev/chat_settings`: the skills and access of a repository chat as they
|
|
532
|
+
* are now. The reply calls it every time, so a skill added, changed or removed
|
|
533
|
+
* is read at the next reply, from the main clone while the chat is read-only
|
|
534
|
+
* and from its worktree once it has one.
|
|
190
535
|
*/
|
|
191
|
-
export async function
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
536
|
+
export async function runChatSettings(ctx: StepContext, params: unknown): Promise<PluginStepResult> {
|
|
537
|
+
const chatId = isRecord(params) && typeof params.chat === "string" ? params.chat : undefined;
|
|
538
|
+
const stored = chatId === undefined ? undefined : await ctx.store.get(CHAT_TYPE, chatId);
|
|
539
|
+
if (stored === undefined || chatId === undefined) return failed("there is no such chat");
|
|
540
|
+
const chat = chatData(stored.data);
|
|
541
|
+
const parent = typeof chat.parent === "string" ? chat.parent : "";
|
|
542
|
+
const name = parent.startsWith("repo:") ? parent.slice("repo:".length) : undefined;
|
|
543
|
+
if (name === undefined) {
|
|
544
|
+
return failed(`chat ${chatId} is not a chat about a repository`);
|
|
545
|
+
}
|
|
546
|
+
const repoObject = await ctx.store.get("repo", name);
|
|
547
|
+
if (repoObject === undefined) return failed(`there is no repository "${name}"`);
|
|
548
|
+
const repository = repositoryData(repoObject.data);
|
|
549
|
+
let settings;
|
|
550
|
+
try {
|
|
551
|
+
settings = await readDevSettings(repository.path);
|
|
552
|
+
} catch (error) {
|
|
553
|
+
return failed(messageOf(error));
|
|
554
|
+
}
|
|
555
|
+
const cwd = chat.cwd.length > 0 ? chat.cwd : repository.path;
|
|
556
|
+
const hasWorktree = isRecord(stored.data.worktree);
|
|
557
|
+
let skills: ChatSkillList;
|
|
558
|
+
try {
|
|
559
|
+
skills = repositoryChatSkills(settings, cwd);
|
|
560
|
+
} catch (error) {
|
|
561
|
+
return failed(messageOf(error));
|
|
562
|
+
}
|
|
563
|
+
const inPlace = !hasWorktree && settings.workIn === "branch";
|
|
564
|
+
// A task that holds the clone on its own branch is the only writer there: the
|
|
565
|
+
// reply reads and says why (ADR 0028 D3, ADR 0039 D3).
|
|
566
|
+
const holder = inPlace ? await cloneHolder(ctx.store, name) : undefined;
|
|
567
|
+
const access = chatAccess(skills.roots, (!hasWorktree && !inPlace) || holder !== undefined);
|
|
568
|
+
const worktree = isRecord(stored.data.worktree) ? stored.data.worktree : {};
|
|
569
|
+
const branch =
|
|
570
|
+
hasWorktree
|
|
571
|
+
? typeof worktree.branch === "string"
|
|
572
|
+
? worktree.branch
|
|
573
|
+
: workBranch(chatWorktreeName(chatId))
|
|
574
|
+
: undefined;
|
|
575
|
+
const denyCommands = Array.isArray(access.denyCommands)
|
|
576
|
+
? access.denyCommands.filter((command): command is string => typeof command === "string")
|
|
577
|
+
: [];
|
|
578
|
+
const context = repositoryChatContext({
|
|
579
|
+
repository: name,
|
|
580
|
+
cwd,
|
|
581
|
+
...(hasWorktree || !folderHasNoGit(repository.path) ? {} : { noGit: true }),
|
|
582
|
+
readOnly: !hasWorktree && !inPlace,
|
|
583
|
+
inPlace,
|
|
584
|
+
...(holder === undefined ? {} : { heldBy: holder }),
|
|
585
|
+
...(branch === undefined ? {} : { branch }),
|
|
586
|
+
skills: skills.directories,
|
|
587
|
+
denyCommands,
|
|
588
|
+
});
|
|
589
|
+
return {
|
|
590
|
+
result: "done",
|
|
591
|
+
data: {
|
|
592
|
+
cwd,
|
|
593
|
+
access,
|
|
594
|
+
skills: [...skills.directories],
|
|
595
|
+
context,
|
|
596
|
+
// Nothing to offer when the chat works in the main clone itself.
|
|
597
|
+
...(inPlace ? { worktreeOffer: false } : {}),
|
|
598
|
+
},
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* The action a repository chat runs before its access level changes: a chat
|
|
604
|
+
* still in the main clone that is raised to `edits` or `full` first gets its
|
|
605
|
+
* worktree, exactly as the worktree answer gives it. A chat already in its
|
|
606
|
+
* worktree, a bot chat and a level below `edits` need nothing. A worktree that
|
|
607
|
+
* cannot be opened refuses the change, so the level stays as it was. It is
|
|
608
|
+
* hidden from clients: the chat marks its in-memory object only while it calls
|
|
609
|
+
* this action.
|
|
610
|
+
*/
|
|
611
|
+
export async function runChatAccess(ctx: ActionContext, args: unknown): Promise<unknown> {
|
|
612
|
+
const level = isRecord(args) && typeof args.level === "string" ? args.level : undefined;
|
|
613
|
+
if (level === undefined) throw new Error("the chat's new access level is needed");
|
|
614
|
+
const stored = await ctx.store.get(CHAT_TYPE, ctx.object.id);
|
|
615
|
+
if (stored === undefined) throw new Error(`there is no chat ${ctx.object.id}`);
|
|
616
|
+
if (level !== "edits" && level !== "full") return "no worktree is needed";
|
|
617
|
+
if (isRecord(stored.data.worktree)) {
|
|
618
|
+
return "the chat already has its worktree";
|
|
619
|
+
}
|
|
620
|
+
const chat = chatData(stored.data);
|
|
621
|
+
const parent = typeof chat.parent === "string" ? chat.parent : "";
|
|
622
|
+
if (!parent.startsWith("repo:")) return "a bot chat works in its own directory";
|
|
623
|
+
const repoObject = await ctx.store.get("repo", parent.slice("repo:".length));
|
|
624
|
+
if (repoObject !== undefined) {
|
|
625
|
+
const settings = await readDevSettings(repositoryData(repoObject.data).path);
|
|
626
|
+
if (settings.workIn === "branch") return "no worktree is needed: the chat works in the main clone";
|
|
627
|
+
}
|
|
628
|
+
const result = await openChatWorktree(
|
|
629
|
+
ctx,
|
|
630
|
+
ctx.object.id,
|
|
631
|
+
ctx.runId ?? `action:dev/chat_access:${ctx.object.id}`,
|
|
632
|
+
"action:dev/chat_access",
|
|
633
|
+
);
|
|
634
|
+
const data = isRecord(result.data) ? result.data : {};
|
|
635
|
+
if (result.result !== "done") {
|
|
636
|
+
throw new Error(
|
|
637
|
+
typeof data.message === "string" && data.message.length > 0
|
|
638
|
+
? data.message
|
|
639
|
+
: `chat ${ctx.object.id} could not open its worktree`,
|
|
640
|
+
);
|
|
641
|
+
}
|
|
642
|
+
return "the chat now has its worktree";
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
const chatAccessAction: ActionTypeDefinition = {
|
|
646
|
+
name: "chat_access",
|
|
647
|
+
on: CHAT_TYPE,
|
|
648
|
+
args: {
|
|
649
|
+
type: "object",
|
|
650
|
+
properties: { level: { type: "string" } },
|
|
651
|
+
required: ["level"],
|
|
652
|
+
additionalProperties: false,
|
|
653
|
+
},
|
|
654
|
+
when: (state) => state[ACCESS_ACTION_REQUEST] === true,
|
|
655
|
+
run: runChatAccess,
|
|
656
|
+
};
|
|
657
|
+
|
|
658
|
+
/** The actions of the chat part of dev. */
|
|
659
|
+
export const chatActions: readonly ActionTypeDefinition[] = [chatAction, chatAccessAction];
|
|
660
|
+
|
|
661
|
+
/** Opens a chat's worktree and records where it moved; shared by the step and the access action. */
|
|
662
|
+
async function openChatWorktree(
|
|
663
|
+
ctx: Pick<PluginBaseContext, "store" | "facts">,
|
|
664
|
+
chatId: string,
|
|
665
|
+
runId: string,
|
|
666
|
+
cause: string,
|
|
667
|
+
raiseLevel = false,
|
|
668
|
+
): Promise<PluginStepResult> {
|
|
669
|
+
const stored = await ctx.store.get(CHAT_TYPE, chatId);
|
|
670
|
+
if (stored === undefined) return failed(`there is no chat ${chatId}`);
|
|
671
|
+
if (isRecord(stored.data.worktree)) {
|
|
197
672
|
return { result: "done", data: { moved: false } };
|
|
198
673
|
}
|
|
199
674
|
const chat = chatData(stored.data);
|
|
200
675
|
const parent = typeof chat.parent === "string" ? chat.parent : "";
|
|
201
676
|
const name = parent.startsWith("repo:") ? parent.slice("repo:".length) : undefined;
|
|
202
677
|
const repoObject = name === undefined ? undefined : await ctx.store.get("repo", name);
|
|
203
|
-
if (repoObject === undefined) return failed(`chat ${
|
|
678
|
+
if (repoObject === undefined) return failed(`chat ${chatId} is not a chat about a repository`);
|
|
204
679
|
const repository = repositoryData(repoObject.data);
|
|
205
680
|
|
|
206
681
|
let settings;
|
|
@@ -209,26 +684,101 @@ export async function runChatWorktree(ctx: StepContext): Promise<PluginStepResul
|
|
|
209
684
|
} catch (error) {
|
|
210
685
|
return failed(error instanceof DevError ? error.message : String(error));
|
|
211
686
|
}
|
|
212
|
-
const
|
|
687
|
+
const refusal = worktreeRefusal(repository.path);
|
|
688
|
+
if (refusal !== undefined) return failed(refusal);
|
|
689
|
+
const worktreeName = chatWorktreeName(chatId);
|
|
213
690
|
const prepared = prepareWorktree(repository, settings, worktreeName);
|
|
214
691
|
if (prepared.kind !== "ready") {
|
|
215
|
-
return failed(prepared.message ?? `cannot open a worktree for chat ${
|
|
692
|
+
return failed(prepared.message ?? `cannot open a worktree for chat ${chatId}`);
|
|
216
693
|
}
|
|
217
694
|
const path = worktreePath(repository.path, settings, worktreeName);
|
|
218
695
|
const branch = workBranch(worktreeName);
|
|
219
696
|
|
|
220
697
|
// Read again: the reply step may have moved the session while git ran.
|
|
221
|
-
const fresh = await ctx.store.get(CHAT_TYPE,
|
|
222
|
-
if (fresh === undefined) return failed(`there is no chat ${
|
|
223
|
-
if (
|
|
698
|
+
const fresh = await ctx.store.get(CHAT_TYPE, chatId);
|
|
699
|
+
if (fresh === undefined) return failed(`there is no chat ${chatId}`);
|
|
700
|
+
if (isRecord(fresh.data.worktree)) {
|
|
224
701
|
return { result: "done", data: { moved: false } };
|
|
225
702
|
}
|
|
226
|
-
|
|
703
|
+
let skills: ChatSkillList;
|
|
704
|
+
try {
|
|
705
|
+
skills = repositoryChatSkills(settings, path);
|
|
706
|
+
} catch (error) {
|
|
707
|
+
return failed(messageOf(error));
|
|
708
|
+
}
|
|
709
|
+
const clean: Record<string, unknown> = { ...fresh.data };
|
|
710
|
+
// Only chat:access marks this in memory, to call its hidden action; it is never stored.
|
|
711
|
+
delete clean[ACCESS_ACTION_REQUEST];
|
|
712
|
+
// A chat that asked for the worktree is read-only only by its level `plan`;
|
|
713
|
+
// the worktree is where it writes, so the level moves to `edits` with it. A
|
|
714
|
+
// level the person set above `plan` stays as it is.
|
|
715
|
+
const freshChat = chatData(fresh.data);
|
|
716
|
+
const level = freshChat.accessLevel ?? (isRecord(fresh.data.access) && fresh.data.access.readOnly === true ? "plan" : "ask");
|
|
717
|
+
const raised = raiseLevel && level === "plan";
|
|
227
718
|
await ctx.store.put(
|
|
228
719
|
CHAT_TYPE,
|
|
229
|
-
|
|
230
|
-
{
|
|
231
|
-
|
|
720
|
+
chatId,
|
|
721
|
+
{
|
|
722
|
+
...clean,
|
|
723
|
+
...(raised ? { accessLevel: "edits" } : {}),
|
|
724
|
+
cwd: path,
|
|
725
|
+
access: chatAccess(skills.roots, false),
|
|
726
|
+
skills: [...skills.directories],
|
|
727
|
+
worktree: { path, branch },
|
|
728
|
+
},
|
|
729
|
+
cause,
|
|
232
730
|
);
|
|
731
|
+
// The transcript says where the chat moved, once. It is an entry of the
|
|
732
|
+
// worktree run, not of a reply, so it does not become the agent's words.
|
|
733
|
+
await ctx.facts.append({
|
|
734
|
+
type: ENTRY_FACT,
|
|
735
|
+
object: chatRef(chatId),
|
|
736
|
+
data: { runId, kind: "worktree", text: WORKTREE_NOTE },
|
|
737
|
+
});
|
|
738
|
+
if (raised) {
|
|
739
|
+
const changed: AccessChangedData = { by: { via: "dev", id: "worktree" }, from: "plan", to: "edits" };
|
|
740
|
+
await ctx.facts.append({ type: ACCESS_FACT, object: chatRef(chatId), data: changed });
|
|
741
|
+
}
|
|
233
742
|
return { result: "done", data: { moved: true, path, branch } };
|
|
234
743
|
}
|
|
744
|
+
|
|
745
|
+
/**
|
|
746
|
+
* Step `dev/chat_worktree`: opens `work/chat-<id>` for a chat of a repository
|
|
747
|
+
* and moves the chat's working directory there, once. The chat is writable in
|
|
748
|
+
* the worktree; a chat that already has one is left as it is.
|
|
749
|
+
*/
|
|
750
|
+
export async function runChatWorktree(ctx: StepContext): Promise<PluginStepResult> {
|
|
751
|
+
const object = ctx.object;
|
|
752
|
+
if (object === undefined || object.type !== CHAT_TYPE) return failed("this runs on a chat");
|
|
753
|
+
const result = await openChatWorktree(ctx, object.id, ctx.runId, "step:dev/chat_worktree", true);
|
|
754
|
+
// A live yes ended the reply that asked; answer the same message again now
|
|
755
|
+
// that the chat is writable. A reply that ended because the chat moved to
|
|
756
|
+
// its worktree meanwhile (its level was raised) is answered again the same
|
|
757
|
+
// way. A yes that came after the reply ended only opens the worktree, so it
|
|
758
|
+
// starts no reply.
|
|
759
|
+
if (result.result === "done" && ctx.trigger?.type === WORKTREE_EVENT && !lateWorktree(ctx)) {
|
|
760
|
+
await ctx.emit({
|
|
761
|
+
type: CHAT_REPLY_EVENT,
|
|
762
|
+
key: `worktree:${object.id}`,
|
|
763
|
+
object: `${CHAT_TYPE}:${object.id}`,
|
|
764
|
+
});
|
|
765
|
+
}
|
|
766
|
+
return result;
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
/** Whether the event says its yes arrived after the asking reply had ended. */
|
|
770
|
+
function lateWorktree(ctx: StepContext): boolean {
|
|
771
|
+
const path = ctx.trigger?.data;
|
|
772
|
+
if (path !== undefined) {
|
|
773
|
+
try {
|
|
774
|
+
const value: unknown = JSON.parse(readFileSync(path, "utf8"));
|
|
775
|
+
if (isRecord(value) && value.late === true) return true;
|
|
776
|
+
} catch {
|
|
777
|
+
// A missing or broken file is not the late marker; fall back to the key.
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
// The kernel scopes an emitted key as `<runId>:<key>`, so the reply's "late"
|
|
781
|
+
// arrives as `<runId>:late`. The data file is the primary marker; the key
|
|
782
|
+
// covers an event that carries no data.
|
|
783
|
+
return ctx.trigger?.key.endsWith(":late") === true;
|
|
784
|
+
}
|