@operad-io/harness-process 0.1.0 → 0.2.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/package.json +4 -3
- package/src/acp.ts +4 -0
- package/src/agent-settings.ts +35 -0
- package/src/agent-step.ts +259 -0
- package/src/always-allow.ts +9 -56
- package/src/ask-call.ts +285 -10
- package/src/ask-note.ts +45 -1
- package/src/flows.ts +44 -0
- package/src/fold-lines.ts +85 -15
- package/src/inbox.ts +5 -3
- package/src/index.ts +14 -0
- package/src/jsonl-log.ts +48 -6
- package/src/keep.ts +646 -0
- package/src/limit.ts +1 -0
- package/src/lines.ts +225 -0
- package/src/probe-cache.ts +43 -0
- package/src/process.ts +605 -91
- package/src/question-form.ts +133 -0
- package/src/refusal.ts +47 -0
- package/src/skills.ts +232 -0
- package/src/step.ts +107 -25
- package/src/timings.ts +83 -0
- package/src/tool-events.ts +22 -0
- package/src/version.ts +49 -0
- package/src/wire.ts +205 -0
- package/src/worker-entry.ts +84 -0
- package/src/worker-step.ts +119 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @operad-io/harness-process
|
|
2
2
|
|
|
3
|
-
**Shared process and log helpers for the harness plugins.** The code the harness packages have in common: running a child process with a wall time and a stall limit, its logs, and an ACP client.
|
|
3
|
+
**Shared process and log helpers for the harness plugins.** The code the harness packages have in common: reading an `agent` step and starting from its access set, running a child process with a wall time and a stall limit, its logs and wire checks, the child program's entry, and an ACP client.
|
|
4
4
|
|
|
5
5
|
This package is part of [operad](https://github.com/operad-io/operad). Most people install only [`@operad-io/cli`](https://www.npmjs.com/package/@operad-io/cli).
|
|
6
6
|
|
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@operad-io/harness-process",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./src/index.ts"
|
|
7
7
|
},
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@operad-io/
|
|
10
|
-
"@operad-io/
|
|
9
|
+
"@operad-io/access-match": "0.2.0",
|
|
10
|
+
"@operad-io/engine": "0.2.0",
|
|
11
|
+
"@operad-io/mcp": "0.2.0"
|
|
11
12
|
},
|
|
12
13
|
"description": "operad: the shared process and log helpers of the harness plugins",
|
|
13
14
|
"repository": {
|
package/src/acp.ts
CHANGED
|
@@ -90,6 +90,8 @@ export interface AcpClientOptions {
|
|
|
90
90
|
readonly mcpServers?: readonly Readonly<Record<string, unknown>>[] | undefined;
|
|
91
91
|
readonly clientName: string;
|
|
92
92
|
readonly clientVersion?: string | undefined;
|
|
93
|
+
/** The session is open (or loaded) and the settings are about to be sent. */
|
|
94
|
+
readonly onReady?: ((sessionId: string) => void) | undefined;
|
|
93
95
|
/** Each `session/update` of the running prompt, in order. */
|
|
94
96
|
readonly onUpdate: (update: Readonly<Record<string, unknown>>) => void | Promise<void>;
|
|
95
97
|
/** True lets the call run once; false refuses it. */
|
|
@@ -326,6 +328,8 @@ export class AcpClient {
|
|
|
326
328
|
}
|
|
327
329
|
this.session = sessionId;
|
|
328
330
|
}
|
|
331
|
+
const ready = this.session;
|
|
332
|
+
if (ready !== null) this.options.onReady?.(ready);
|
|
329
333
|
this.phase = "config";
|
|
330
334
|
this.nextStage();
|
|
331
335
|
return;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The run time and stall limits every process harness has: the same defaults,
|
|
3
|
+
* the same setting entries and the same four constants.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** How long an agent may run before the step stops it. */
|
|
7
|
+
export const DEFAULT_WALL_TIME_SECS = 3600;
|
|
8
|
+
|
|
9
|
+
/** How long an agent may go without a new output line. */
|
|
10
|
+
export const DEFAULT_STALL_SECS = 600;
|
|
11
|
+
|
|
12
|
+
/** One run of the step may take this long; it is above every allowed wall time. */
|
|
13
|
+
export const STEP_TIMEOUT_SECS = 8 * 3600;
|
|
14
|
+
|
|
15
|
+
/** The longest wall time a run may ask for: the step timeout minus a margin. */
|
|
16
|
+
export const MAX_WALL_TIME_SECS = STEP_TIMEOUT_SECS - 300;
|
|
17
|
+
|
|
18
|
+
/** The `wallTimeSecs` setting entry of a harness. */
|
|
19
|
+
export const WALL_TIME_SETTING = {
|
|
20
|
+
type: "number" as const,
|
|
21
|
+
title: "Run time limit",
|
|
22
|
+
minimum: 1,
|
|
23
|
+
maximum: MAX_WALL_TIME_SECS,
|
|
24
|
+
default: DEFAULT_WALL_TIME_SECS,
|
|
25
|
+
description: "The longest one agent run may take.",
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/** The `stallSecs` setting entry of a harness. */
|
|
29
|
+
export const STALL_SETTING = {
|
|
30
|
+
type: "number" as const,
|
|
31
|
+
title: "Stall limit",
|
|
32
|
+
minimum: 1,
|
|
33
|
+
default: DEFAULT_STALL_SECS,
|
|
34
|
+
description: "How long an agent may be silent while no tool call is open.",
|
|
35
|
+
};
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The first half of every process harness's `agent` step: the params and
|
|
3
|
+
* settings it reads the same way, the refusal it writes when one is wrong, and
|
|
4
|
+
* the access set it starts the worker from. A harness adds only the fields its
|
|
5
|
+
* own program takes.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { hasLiveAccess, secretValuesOf, withDefaultAccess, type AccessProfile } from "@operad-io/access-match";
|
|
9
|
+
import {
|
|
10
|
+
directoryError,
|
|
11
|
+
isRecord,
|
|
12
|
+
paramsObject,
|
|
13
|
+
type PluginStepResult,
|
|
14
|
+
type StepContext,
|
|
15
|
+
} from "@operad-io/engine";
|
|
16
|
+
import {
|
|
17
|
+
accessWithAlwaysAllow,
|
|
18
|
+
alwaysAllowFromGrant,
|
|
19
|
+
appendAlwaysAllow,
|
|
20
|
+
personStarted,
|
|
21
|
+
readAlwaysAllow,
|
|
22
|
+
} from "./always-allow.ts";
|
|
23
|
+
import { ALLOW_ALWAYS, canAsk, recordTargetGrantFromAnswer } from "./ask-call.ts";
|
|
24
|
+
import { askNote } from "./ask-note.ts";
|
|
25
|
+
import { eventNote } from "./event-note.ts";
|
|
26
|
+
import { hasGrants } from "./grants.ts";
|
|
27
|
+
import { inboxOf } from "./inbox.ts";
|
|
28
|
+
import { flagValue } from "./limit.ts";
|
|
29
|
+
import { refuseResult } from "./refusal.ts";
|
|
30
|
+
import { optionalText, parseStepFlags, secs, stringListParam, text, type StepFlagSpec } from "./step.ts";
|
|
31
|
+
import { StepTimings } from "./timings.ts";
|
|
32
|
+
import type { UsageSource } from "./usage.ts";
|
|
33
|
+
|
|
34
|
+
/** What one harness adds to the shared reading of an `agent` step. */
|
|
35
|
+
export interface AgentStepSpec<Level extends string> {
|
|
36
|
+
/** The step name messages use, such as `claude/agent`. */
|
|
37
|
+
readonly at: string;
|
|
38
|
+
/** The name the step goes by in usage and limit keys. */
|
|
39
|
+
readonly harness: string;
|
|
40
|
+
/** The spelling of this harness's step `args`. */
|
|
41
|
+
readonly flags: StepFlagSpec<Level>;
|
|
42
|
+
readonly defaultWallTimeSecs: number;
|
|
43
|
+
readonly defaultStallSecs: number;
|
|
44
|
+
readonly maxWallTimeSecs: number;
|
|
45
|
+
/** The setting that names the reasoning level, such as `effort`. */
|
|
46
|
+
readonly levelSetting: string;
|
|
47
|
+
/** Milliseconds now, from the injected clock. */
|
|
48
|
+
readonly now: () => number;
|
|
49
|
+
/**
|
|
50
|
+
* Reads a param this harness uses in its prompt, from the prompt on and
|
|
51
|
+
* before the shared note, directory and flags.
|
|
52
|
+
*/
|
|
53
|
+
readonly early?: (
|
|
54
|
+
input: Readonly<Record<string, unknown>>,
|
|
55
|
+
flow: Readonly<Record<string, unknown>>,
|
|
56
|
+
prompt: string,
|
|
57
|
+
) => void;
|
|
58
|
+
/**
|
|
59
|
+
* Reads this harness's own settings and params, after the shared fields and
|
|
60
|
+
* before the directory check. A returned result refuses the step.
|
|
61
|
+
*/
|
|
62
|
+
readonly beforeDirectory?: (
|
|
63
|
+
read: AgentStepValues<Level>,
|
|
64
|
+
) => void | PluginStepResult | Promise<void | PluginStepResult>;
|
|
65
|
+
/**
|
|
66
|
+
* Reads the rest, after the directory check and before the access set. A
|
|
67
|
+
* returned result refuses the step.
|
|
68
|
+
*/
|
|
69
|
+
readonly afterDirectory?: (
|
|
70
|
+
read: AgentStepValues<Level>,
|
|
71
|
+
) => void | PluginStepResult | Promise<void | PluginStepResult>;
|
|
72
|
+
/** Extra data a refusal carries, such as codex's image call count. */
|
|
73
|
+
readonly refusalExtra?: Readonly<Record<string, unknown>>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The values the shared part of an `agent` step reads. */
|
|
77
|
+
export interface AgentStepValues<Level extends string> {
|
|
78
|
+
readonly at: string;
|
|
79
|
+
readonly harness: string;
|
|
80
|
+
readonly ctx: StepContext;
|
|
81
|
+
readonly params: unknown;
|
|
82
|
+
readonly timings: StepTimings;
|
|
83
|
+
readonly input: Readonly<Record<string, unknown>>;
|
|
84
|
+
readonly flow: Readonly<Record<string, unknown>>;
|
|
85
|
+
readonly prompt: string;
|
|
86
|
+
readonly note: string | undefined;
|
|
87
|
+
readonly rawAskAnswer: unknown;
|
|
88
|
+
readonly answerNote: string | undefined;
|
|
89
|
+
readonly promptWithNote: string;
|
|
90
|
+
readonly cwd: string;
|
|
91
|
+
readonly args: unknown;
|
|
92
|
+
readonly flags: { readonly model?: string; readonly level?: Level };
|
|
93
|
+
/** The model the step will use, known before anything can fail. */
|
|
94
|
+
readonly knownModel: string | undefined;
|
|
95
|
+
readonly early: UsageSource;
|
|
96
|
+
readonly resumeSession: string | undefined;
|
|
97
|
+
readonly wallTimeSecs: number;
|
|
98
|
+
readonly stallSecs: number;
|
|
99
|
+
readonly model: string | undefined;
|
|
100
|
+
/** The level the step asks for: the flag, the param or the setting; the worker validates it. */
|
|
101
|
+
readonly level: string | undefined;
|
|
102
|
+
/** The early failed result: writes the message and returns `failed` with `reason`. */
|
|
103
|
+
readonly refuse: (reason: string, message: string) => Promise<PluginStepResult>;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The access set every harness builds from an answer and the profile. */
|
|
107
|
+
export interface AgentStepAccess {
|
|
108
|
+
readonly startAccess: AccessProfile;
|
|
109
|
+
readonly access: AccessProfile;
|
|
110
|
+
readonly runAccess: AccessProfile | undefined;
|
|
111
|
+
readonly allowAlways: boolean;
|
|
112
|
+
readonly secrets: readonly string[];
|
|
113
|
+
/** Whether the worker needs a reply channel for permissions or mid-run messages. */
|
|
114
|
+
readonly interactive: boolean;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** A step that is reading its params or has read them all. */
|
|
118
|
+
export type AgentStepRead<Level extends string> =
|
|
119
|
+
| { readonly refused: PluginStepResult }
|
|
120
|
+
| { readonly values: AgentStepValues<Level>; readonly access: AgentStepAccess };
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Reads the fields every process harness reads the same way, refuses a bad one
|
|
124
|
+
* with the early failed result, checks the directory and builds the access set.
|
|
125
|
+
*/
|
|
126
|
+
export async function readAgentStep<Level extends string>(
|
|
127
|
+
ctx: StepContext,
|
|
128
|
+
params: unknown,
|
|
129
|
+
spec: AgentStepSpec<Level>,
|
|
130
|
+
): Promise<AgentStepRead<Level>> {
|
|
131
|
+
const at = spec.at;
|
|
132
|
+
const timings = new StepTimings(spec.now);
|
|
133
|
+
const input = paramsObject(params, at);
|
|
134
|
+
const flow = isRecord(ctx.params) ? ctx.params : {};
|
|
135
|
+
|
|
136
|
+
const prompt = text(input.prompt ?? flow.prompt, `${at}: params.prompt`);
|
|
137
|
+
spec.early?.(input, flow, prompt);
|
|
138
|
+
const note = eventNote(ctx.trigger);
|
|
139
|
+
const rawAskAnswer = input.askAnswer ?? flow.askAnswer;
|
|
140
|
+
const answerNote = askNote(rawAskAnswer);
|
|
141
|
+
const promptWithNote = [prompt, note, answerNote]
|
|
142
|
+
.filter((part): part is string => part !== undefined)
|
|
143
|
+
.join("\n\n");
|
|
144
|
+
const cwd = text(input.cwd ?? flow.cwd, `${at}: params.cwd`);
|
|
145
|
+
const args = input.args === undefined ? flow.args : input.args;
|
|
146
|
+
const flags = parseStepFlags(stringListParam(args, `${at}: params.args`), spec.flags);
|
|
147
|
+
const settingModel = typeof ctx.settings.model === "string" ? ctx.settings.model : undefined;
|
|
148
|
+
const knownModel =
|
|
149
|
+
typeof flags === "string" ? (flagValue(args, "--model") ?? settingModel) : (flags.model ?? settingModel);
|
|
150
|
+
const early: UsageSource = { harness: spec.harness, model: knownModel ?? null };
|
|
151
|
+
|
|
152
|
+
const refuse = (reason: string, message: string): Promise<PluginStepResult> =>
|
|
153
|
+
refuseResult({
|
|
154
|
+
log: (text) => ctx.logger.write(text),
|
|
155
|
+
timings,
|
|
156
|
+
who: early,
|
|
157
|
+
reason,
|
|
158
|
+
message,
|
|
159
|
+
extra: spec.refusalExtra,
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
if (typeof flags === "string") return { refused: await refuse("invalid_args", `${at}: ${flags}`) };
|
|
163
|
+
|
|
164
|
+
const resumeSession = optionalText(input.resumeSession ?? flow.resumeSession, `${at}: params.resumeSession`);
|
|
165
|
+
const wallTimeSecs = secs(
|
|
166
|
+
input.wallTimeSecs ?? ctx.settings.wallTimeSecs ?? spec.defaultWallTimeSecs,
|
|
167
|
+
`${at}: params.wallTimeSecs`,
|
|
168
|
+
);
|
|
169
|
+
if (wallTimeSecs > spec.maxWallTimeSecs) {
|
|
170
|
+
throw new Error(`${at}: params.wallTimeSecs: must be at most ${spec.maxWallTimeSecs}`);
|
|
171
|
+
}
|
|
172
|
+
const stallSecs = secs(
|
|
173
|
+
input.stallSecs ?? ctx.settings.stallSecs ?? spec.defaultStallSecs,
|
|
174
|
+
`${at}: params.stallSecs`,
|
|
175
|
+
);
|
|
176
|
+
const model = flags.model ?? optionalText(ctx.settings.model, `${at}: settings.model`);
|
|
177
|
+
const level =
|
|
178
|
+
flags.level ??
|
|
179
|
+
optionalText(input.effort, `${at}: params.effort`) ??
|
|
180
|
+
optionalText(ctx.settings[spec.levelSetting], `${at}: settings.${spec.levelSetting}`);
|
|
181
|
+
|
|
182
|
+
const values: AgentStepValues<Level> = {
|
|
183
|
+
at,
|
|
184
|
+
harness: spec.harness,
|
|
185
|
+
ctx,
|
|
186
|
+
params,
|
|
187
|
+
timings,
|
|
188
|
+
input,
|
|
189
|
+
flow,
|
|
190
|
+
prompt,
|
|
191
|
+
note,
|
|
192
|
+
rawAskAnswer,
|
|
193
|
+
answerNote,
|
|
194
|
+
promptWithNote,
|
|
195
|
+
cwd,
|
|
196
|
+
args,
|
|
197
|
+
flags,
|
|
198
|
+
knownModel,
|
|
199
|
+
early,
|
|
200
|
+
resumeSession,
|
|
201
|
+
wallTimeSecs,
|
|
202
|
+
stallSecs,
|
|
203
|
+
model,
|
|
204
|
+
level,
|
|
205
|
+
refuse,
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
const before = await spec.beforeDirectory?.(values);
|
|
209
|
+
if (before !== undefined) return { refused: before };
|
|
210
|
+
|
|
211
|
+
const cwdProblem = await directoryError(cwd);
|
|
212
|
+
if (cwdProblem !== undefined) return { refused: await refuse("cwd", cwdProblem) };
|
|
213
|
+
|
|
214
|
+
const after = await spec.afterDirectory?.(values);
|
|
215
|
+
if (after !== undefined) return { refused: after };
|
|
216
|
+
|
|
217
|
+
// A generated step that names no profile still starts from the default.
|
|
218
|
+
const startAccess = withDefaultAccess(ctx.access as AccessProfile | undefined);
|
|
219
|
+
const secrets = await secretValuesOf(ctx.secrets);
|
|
220
|
+
const allowAlways = personStarted(ctx.trigger);
|
|
221
|
+
// A run no person's message started asks about every outward write.
|
|
222
|
+
const access = allowAlways ? startAccess : { ...startAccess, background: true };
|
|
223
|
+
if (allowAlways && isRecord(rawAskAnswer) && rawAskAnswer["choice"] === ALLOW_ALWAYS) {
|
|
224
|
+
const entry = alwaysAllowFromGrant(access, cwd);
|
|
225
|
+
if (entry !== undefined) {
|
|
226
|
+
try {
|
|
227
|
+
await appendAlwaysAllow(ctx.home, entry);
|
|
228
|
+
} catch (error) {
|
|
229
|
+
await ctx.logger.write(
|
|
230
|
+
`allow_always could not be written to the user access file: ${String(error)}; this call runs once`,
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (ctx.flow !== undefined) {
|
|
236
|
+
try {
|
|
237
|
+
await recordTargetGrantFromAnswer(ctx.home, ctx.flow, access, rawAskAnswer, spec.now);
|
|
238
|
+
} catch (error) {
|
|
239
|
+
await ctx.logger.write(`the target grant could not be written: ${String(error)}; this call runs once`);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
let runAccess = accessWithAlwaysAllow(access, [], false);
|
|
243
|
+
if (allowAlways) {
|
|
244
|
+
const user = await readAlwaysAllow(ctx.home);
|
|
245
|
+
if (user.problem !== undefined) await ctx.logger.write(user.problem);
|
|
246
|
+
runAccess = accessWithAlwaysAllow(access, user.entries, true);
|
|
247
|
+
}
|
|
248
|
+
return {
|
|
249
|
+
values,
|
|
250
|
+
access: {
|
|
251
|
+
startAccess,
|
|
252
|
+
access,
|
|
253
|
+
runAccess,
|
|
254
|
+
allowAlways,
|
|
255
|
+
secrets,
|
|
256
|
+
interactive: hasGrants(access) || canAsk(access) || hasLiveAccess(access) || inboxOf(params) !== undefined,
|
|
257
|
+
},
|
|
258
|
+
};
|
|
259
|
+
}
|
package/src/always-allow.ts
CHANGED
|
@@ -7,26 +7,18 @@
|
|
|
7
7
|
* platform file lock and replace the file atomically.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
10
|
+
import { messageOf } from "@operad-io/engine";
|
|
11
|
+
import { readFile } from "node:fs/promises";
|
|
12
12
|
import { join } from "node:path";
|
|
13
|
-
import { isRecord } from "@operad-io/engine";
|
|
14
|
-
import { withLockFile } from "@operad-io/engine/platform";
|
|
13
|
+
import { isMissingFile, isRecord } from "@operad-io/engine";
|
|
14
|
+
import { withLockFile, writeJsonAtomic } from "@operad-io/engine/platform";
|
|
15
|
+
import { personStarted, type AlwaysAllowEntry } from "@operad-io/access-match";
|
|
16
|
+
export { personStarted };
|
|
17
|
+
export type { AlwaysAllowEntry };
|
|
15
18
|
|
|
16
19
|
/** The file name of the user layer under the operad home. */
|
|
17
20
|
export const ACCESS_USER_FILE = "access-user.json";
|
|
18
21
|
|
|
19
|
-
/** One call a person allowed always: its kind, exact value and working directory. */
|
|
20
|
-
export interface AlwaysAllowEntry {
|
|
21
|
-
readonly kind: "command" | "path";
|
|
22
|
-
/** The exact command text, or the path as the call wrote it. */
|
|
23
|
-
readonly value: string;
|
|
24
|
-
/** The working directory the person allowed it in. */
|
|
25
|
-
readonly cwd: string;
|
|
26
|
-
/** The tool a path entry names; absent for a command entry. */
|
|
27
|
-
readonly tool?: string;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
22
|
/** What one asked call carries into an `alwaysAllow` entry. */
|
|
31
23
|
export interface AskedCall {
|
|
32
24
|
readonly kind: "command" | "path";
|
|
@@ -49,14 +41,6 @@ export function alwaysAllowEntry(call: AskedCall, cwd: string): AlwaysAllowEntry
|
|
|
49
41
|
};
|
|
50
42
|
}
|
|
51
43
|
|
|
52
|
-
function messageOf(error: unknown): string {
|
|
53
|
-
return error instanceof Error ? error.message : String(error);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function isMissing(error: unknown): boolean {
|
|
57
|
-
return typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
|
|
58
|
-
}
|
|
59
|
-
|
|
60
44
|
function entryOf(value: unknown): AlwaysAllowEntry | undefined {
|
|
61
45
|
if (!isRecord(value)) return undefined;
|
|
62
46
|
const kind = value["kind"];
|
|
@@ -99,7 +83,7 @@ export async function readAlwaysAllow(home: string): Promise<AlwaysAllowRead> {
|
|
|
99
83
|
try {
|
|
100
84
|
text = await readFile(path, "utf8");
|
|
101
85
|
} catch (error) {
|
|
102
|
-
if (
|
|
86
|
+
if (isMissingFile(error)) return { entries: [] };
|
|
103
87
|
return invalid(path, `cannot be read: ${messageOf(error)}`);
|
|
104
88
|
}
|
|
105
89
|
let raw: unknown;
|
|
@@ -124,19 +108,7 @@ export async function appendAlwaysAllow(home: string, entry: AlwaysAllowEntry):
|
|
|
124
108
|
await withLockFile(`${path}.lock`, async () => {
|
|
125
109
|
const found = await readAlwaysAllow(home);
|
|
126
110
|
const entries = [...found.entries.filter((existing) => !sameEntry(existing, entry)), entry];
|
|
127
|
-
await
|
|
128
|
-
const temporary = join(home, `.${ACCESS_USER_FILE}.${process.pid}.${randomUUID()}.tmp`);
|
|
129
|
-
try {
|
|
130
|
-
await writeFile(temporary, `${JSON.stringify(entries, null, 2)}\n`, { mode: 0o600 });
|
|
131
|
-
await rename(temporary, path);
|
|
132
|
-
} catch (error) {
|
|
133
|
-
try {
|
|
134
|
-
await unlink(temporary);
|
|
135
|
-
} catch {
|
|
136
|
-
// The temporary file was never made, or is already gone.
|
|
137
|
-
}
|
|
138
|
-
throw error;
|
|
139
|
-
}
|
|
111
|
+
await writeJsonAtomic(path, entries, { space: 2, mode: 0o600 });
|
|
140
112
|
}, { label: "access user layer" });
|
|
141
113
|
}
|
|
142
114
|
|
|
@@ -183,22 +155,3 @@ export function accessWithAlwaysAllow(
|
|
|
183
155
|
return { ...(base ?? {}), alwaysAllow: combined };
|
|
184
156
|
}
|
|
185
157
|
|
|
186
|
-
/** The sources `chat:send` uses for a message a person wrote in a chat. */
|
|
187
|
-
const MESSAGE_SOURCES = new Set(["ios", "telegram", "cli", "tui", "other"]);
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Whether a run may see `allow_always`: no trigger is a person's command, and
|
|
191
|
-
* a message event from one of the chat's own client sources is a person's
|
|
192
|
-
* message in that conversation. Every other event (a schedule, a webhook, an
|
|
193
|
-
* incoming message from another source, a run's own event) is a background run
|
|
194
|
-
* (`docs/backlog/operad-sandbox.md`, not in an ADR yet).
|
|
195
|
-
*/
|
|
196
|
-
export function personStarted(trigger: unknown): boolean {
|
|
197
|
-
if (trigger === undefined || trigger === null) return true;
|
|
198
|
-
return (
|
|
199
|
-
isRecord(trigger) &&
|
|
200
|
-
trigger["type"] === "message" &&
|
|
201
|
-
typeof trigger["source"] === "string" &&
|
|
202
|
-
MESSAGE_SOURCES.has(trigger["source"])
|
|
203
|
-
);
|
|
204
|
-
}
|