farna-bootstrapper 0.0.0-stage → 0.1.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/CHANGELOG.md +91 -0
- package/LICENSE +30 -0
- package/NOTICE +42 -0
- package/README.md +1079 -2
- package/SECURITY.md +65 -0
- package/dist/cli/commands/check.d.ts +58 -0
- package/dist/cli/commands/check.js +198 -0
- package/dist/cli/commands/generate.d.ts +75 -0
- package/dist/cli/commands/generate.js +218 -0
- package/dist/cli/commands/init.d.ts +52 -0
- package/dist/cli/commands/init.js +271 -0
- package/dist/cli/main.d.ts +2 -0
- package/dist/cli/main.js +51 -0
- package/dist/cli/program.d.ts +23 -0
- package/dist/cli/program.js +183 -0
- package/dist/compose/resolve.d.ts +154 -0
- package/dist/compose/resolve.js +545 -0
- package/dist/config/load.d.ts +4 -0
- package/dist/config/load.js +45 -0
- package/dist/config/schema.d.ts +22 -0
- package/dist/config/schema.js +46 -0
- package/dist/core/constants.d.ts +268 -0
- package/dist/core/constants.js +336 -0
- package/dist/core/errors.d.ts +44 -0
- package/dist/core/errors.js +75 -0
- package/dist/core/fs.d.ts +31 -0
- package/dist/core/fs.js +169 -0
- package/dist/core/json.d.ts +22 -0
- package/dist/core/json.js +174 -0
- package/dist/core/paths.d.ts +45 -0
- package/dist/core/paths.js +110 -0
- package/dist/core/text.d.ts +10 -0
- package/dist/core/text.js +27 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +1 -0
- package/dist/lock/lockfile.d.ts +229 -0
- package/dist/lock/lockfile.js +333 -0
- package/dist/materialise/source-tree.d.ts +77 -0
- package/dist/materialise/source-tree.js +264 -0
- package/dist/permissions/permissions.d.ts +128 -0
- package/dist/permissions/permissions.js +452 -0
- package/dist/render/commit.d.ts +59 -0
- package/dist/render/commit.js +279 -0
- package/dist/render/generate-lock.d.ts +9 -0
- package/dist/render/generate-lock.js +203 -0
- package/dist/render/identity.d.ts +23 -0
- package/dist/render/identity.js +396 -0
- package/dist/render/orphans.d.ts +59 -0
- package/dist/render/orphans.js +234 -0
- package/dist/render/port.d.ts +96 -0
- package/dist/render/port.js +1 -0
- package/dist/render/prepare.d.ts +33 -0
- package/dist/render/prepare.js +139 -0
- package/dist/render/root-cleanliness.d.ts +28 -0
- package/dist/render/root-cleanliness.js +76 -0
- package/dist/render/stage.d.ts +36 -0
- package/dist/render/stage.js +92 -0
- package/dist/render/staging.d.ts +16 -0
- package/dist/render/staging.js +94 -0
- package/dist/render/structured.d.ts +35 -0
- package/dist/render/structured.js +258 -0
- package/dist/render/upstream.d.ts +41 -0
- package/dist/render/upstream.js +179 -0
- package/dist/report/format.d.ts +7 -0
- package/dist/report/format.js +97 -0
- package/dist/report/warnings.d.ts +21 -0
- package/dist/report/warnings.js +24 -0
- package/dist/source/digest.d.ts +18 -0
- package/dist/source/digest.js +77 -0
- package/dist/source/frontmatter.d.ts +12 -0
- package/dist/source/frontmatter.js +29 -0
- package/dist/source/load.d.ts +103 -0
- package/dist/source/load.js +676 -0
- package/dist/source/schema.d.ts +149 -0
- package/dist/source/schema.js +283 -0
- package/dist/source/yaml.d.ts +10 -0
- package/dist/source/yaml.js +49 -0
- package/dist/verify/containment.d.ts +22 -0
- package/dist/verify/containment.js +753 -0
- package/package.json +79 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { ResolvedSource } from '../compose/resolve.js';
|
|
3
|
+
import { type Capability, type RenderMode, SCHEMA_VERSION, type Severity, type WarningCode } from '../core/constants.js';
|
|
4
|
+
import type { Warning } from '../report/warnings.js';
|
|
5
|
+
/**
|
|
6
|
+
* The ownership shape of a managed path. A `structured` file is merged key by
|
|
7
|
+
* key, so its entry describes the owned subset rather than the whole file.
|
|
8
|
+
*/
|
|
9
|
+
export type LockEntryShape = 'whole' | 'tree' | 'structured' | 'region';
|
|
10
|
+
export type LockFileEntry = {
|
|
11
|
+
/** Repository-relative, POSIX-spelled, control-character-free. */
|
|
12
|
+
path: string;
|
|
13
|
+
/**
|
|
14
|
+
* The digest whose match proves the bytes are still ours. For a `whole` or
|
|
15
|
+
* `tree` file it is the whole file; for a `structured` file it is the
|
|
16
|
+
* canonical owned subset, because the rest of that file is the user's.
|
|
17
|
+
*/
|
|
18
|
+
sha256: string;
|
|
19
|
+
/** The byte length of the thing `sha256` describes. */
|
|
20
|
+
size: number;
|
|
21
|
+
shape?: LockEntryShape | undefined;
|
|
22
|
+
/** The top-level keys this tool owns, for a `structured` file. */
|
|
23
|
+
ownedKeys?: string[] | undefined;
|
|
24
|
+
};
|
|
25
|
+
export type LockResource = {
|
|
26
|
+
kind: 'rule' | 'permission' | 'mcp' | 'skill' | 'command' | 'hook';
|
|
27
|
+
id: string;
|
|
28
|
+
sha256: string;
|
|
29
|
+
/** The persona in the composition chain that contributed the resource. */
|
|
30
|
+
fromPersona: string;
|
|
31
|
+
};
|
|
32
|
+
export type LockPersona = {
|
|
33
|
+
id: string;
|
|
34
|
+
/** Present when the persona composes a base persona. */
|
|
35
|
+
extends?: string | undefined;
|
|
36
|
+
digest: string;
|
|
37
|
+
permissionDigest: string;
|
|
38
|
+
resources: LockResource[];
|
|
39
|
+
};
|
|
40
|
+
export type LockWarning = {
|
|
41
|
+
code: WarningCode;
|
|
42
|
+
severity: Severity;
|
|
43
|
+
persona: string;
|
|
44
|
+
target: string;
|
|
45
|
+
capability?: Capability | undefined;
|
|
46
|
+
message: string;
|
|
47
|
+
};
|
|
48
|
+
/** The lockfile's renderer record requires an integrity; the reader enforces it. */
|
|
49
|
+
export type LockfileRenderer = {
|
|
50
|
+
name: string;
|
|
51
|
+
version: string;
|
|
52
|
+
integrity: string;
|
|
53
|
+
};
|
|
54
|
+
export type RenderedFile = {
|
|
55
|
+
/** Repository-relative path. */
|
|
56
|
+
path: string;
|
|
57
|
+
bytes: string;
|
|
58
|
+
/** The ownership shape, when it is not a whole file. */
|
|
59
|
+
shape?: LockEntryShape;
|
|
60
|
+
/** The owned top-level keys, for a `structured` file. */
|
|
61
|
+
ownedKeys?: string[];
|
|
62
|
+
/** The digest of the owned subset, for a `structured` file. */
|
|
63
|
+
ownedSha256?: string;
|
|
64
|
+
/** The byte length of the owned subset, for a `structured` file. */
|
|
65
|
+
ownedSize?: number;
|
|
66
|
+
};
|
|
67
|
+
/** The validated and normalised whole document. Every field the reader consumes is checked. */
|
|
68
|
+
export type Lockfile = {
|
|
69
|
+
schemaVersion: typeof SCHEMA_VERSION;
|
|
70
|
+
product: {
|
|
71
|
+
name: string;
|
|
72
|
+
version: string;
|
|
73
|
+
};
|
|
74
|
+
target: string;
|
|
75
|
+
/** `agent` (every persona live) or `profile` (the active persona owns the workspace). */
|
|
76
|
+
mode: RenderMode;
|
|
77
|
+
/** The persona selected by `targets.<t>.active` or `--persona`; null in agent mode. */
|
|
78
|
+
activePersona: string | null;
|
|
79
|
+
renderer: {
|
|
80
|
+
name: string;
|
|
81
|
+
version: string;
|
|
82
|
+
integrity?: string | undefined;
|
|
83
|
+
};
|
|
84
|
+
/** Digest of the config plus every persona and resource file. */
|
|
85
|
+
configDigest?: string | undefined;
|
|
86
|
+
personas: LockPersona[];
|
|
87
|
+
/** Personas resolved but not live. Empty in agent mode; absent in a legacy lockfile. */
|
|
88
|
+
notLive: string[];
|
|
89
|
+
files: LockFileEntry[];
|
|
90
|
+
warnings: LockWarning[];
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* The document as it may appear on disk. `mode` and `activePersona` are optional
|
|
94
|
+
* here so a lockfile written before profile mode existed still parses: treating
|
|
95
|
+
* a present-but-older lockfile as absent discarded the recorded path set, which
|
|
96
|
+
* skipped orphan removal and let a removed persona's artefacts stay live while
|
|
97
|
+
* `check` reported clean. A present document is either understood or refused
|
|
98
|
+
* with a typed error; it is never silently ignored.
|
|
99
|
+
*/
|
|
100
|
+
export declare const lockfileDocumentSchema: z.ZodObject<{
|
|
101
|
+
schemaVersion: z.ZodLiteral<1>;
|
|
102
|
+
product: z.ZodObject<{
|
|
103
|
+
name: z.ZodString;
|
|
104
|
+
version: z.ZodString;
|
|
105
|
+
}, z.core.$strict>;
|
|
106
|
+
target: z.ZodEnum<{
|
|
107
|
+
claudecode: "claudecode";
|
|
108
|
+
opencode: "opencode";
|
|
109
|
+
opencodev1: "opencodev1";
|
|
110
|
+
}>;
|
|
111
|
+
mode: z.ZodOptional<z.ZodEnum<{
|
|
112
|
+
agent: "agent";
|
|
113
|
+
profile: "profile";
|
|
114
|
+
}>>;
|
|
115
|
+
activePersona: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
116
|
+
renderer: z.ZodObject<{
|
|
117
|
+
name: z.ZodString;
|
|
118
|
+
version: z.ZodString;
|
|
119
|
+
integrity: z.ZodOptional<z.ZodString>;
|
|
120
|
+
}, z.core.$strict>;
|
|
121
|
+
configDigest: z.ZodOptional<z.ZodString>;
|
|
122
|
+
personas: z.ZodArray<z.ZodObject<{
|
|
123
|
+
id: z.ZodString;
|
|
124
|
+
extends: z.ZodOptional<z.ZodString>;
|
|
125
|
+
digest: z.ZodString;
|
|
126
|
+
permissionDigest: z.ZodString;
|
|
127
|
+
resources: z.ZodArray<z.ZodObject<{
|
|
128
|
+
kind: z.ZodEnum<{
|
|
129
|
+
mcp: "mcp";
|
|
130
|
+
rule: "rule";
|
|
131
|
+
skill: "skill";
|
|
132
|
+
command: "command";
|
|
133
|
+
permission: "permission";
|
|
134
|
+
hook: "hook";
|
|
135
|
+
}>;
|
|
136
|
+
id: z.ZodString;
|
|
137
|
+
sha256: z.ZodString;
|
|
138
|
+
fromPersona: z.ZodString;
|
|
139
|
+
}, z.core.$strict>>;
|
|
140
|
+
}, z.core.$strict>>;
|
|
141
|
+
notLive: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
142
|
+
files: z.ZodArray<z.ZodObject<{
|
|
143
|
+
path: z.ZodString;
|
|
144
|
+
sha256: z.ZodString;
|
|
145
|
+
size: z.ZodNumber;
|
|
146
|
+
shape: z.ZodOptional<z.ZodEnum<{
|
|
147
|
+
whole: "whole";
|
|
148
|
+
tree: "tree";
|
|
149
|
+
structured: "structured";
|
|
150
|
+
region: "region";
|
|
151
|
+
}>>;
|
|
152
|
+
ownedKeys: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
153
|
+
}, z.core.$strict>>;
|
|
154
|
+
warnings: z.ZodArray<z.ZodObject<{
|
|
155
|
+
code: z.ZodEnum<{
|
|
156
|
+
W107_OUTSIDE_OWNERSHIP: "W107_OUTSIDE_OWNERSHIP";
|
|
157
|
+
W101_CAPABILITY_DROPPED: "W101_CAPABILITY_DROPPED";
|
|
158
|
+
W106_DENY_WINS: "W106_DENY_WINS";
|
|
159
|
+
W109_PROFILE_BY_CHOICE: "W109_PROFILE_BY_CHOICE";
|
|
160
|
+
W111_ASK_DOWNGRADED: "W111_ASK_DOWNGRADED";
|
|
161
|
+
W112_PERMISSION_LOSSY: "W112_PERMISSION_LOSSY";
|
|
162
|
+
W113_ALLOW_LIST_NARROWS: "W113_ALLOW_LIST_NARROWS";
|
|
163
|
+
W114_RENDERER_INTEGRITY_UNVERIFIED: "W114_RENDERER_INTEGRITY_UNVERIFIED";
|
|
164
|
+
W115_CREDENTIAL_IN_URL: "W115_CREDENTIAL_IN_URL";
|
|
165
|
+
W116_SECRET_IN_ARGS: "W116_SECRET_IN_ARGS";
|
|
166
|
+
}>;
|
|
167
|
+
severity: z.ZodEnum<{
|
|
168
|
+
error: "error";
|
|
169
|
+
high: "high";
|
|
170
|
+
medium: "medium";
|
|
171
|
+
low: "low";
|
|
172
|
+
info: "info";
|
|
173
|
+
}>;
|
|
174
|
+
persona: z.ZodString;
|
|
175
|
+
target: z.ZodString;
|
|
176
|
+
capability: z.ZodOptional<z.ZodEnum<{
|
|
177
|
+
mcp: "mcp";
|
|
178
|
+
rule: "rule";
|
|
179
|
+
skill: "skill";
|
|
180
|
+
command: "command";
|
|
181
|
+
permission: "permission";
|
|
182
|
+
hook: "hook";
|
|
183
|
+
model: "model";
|
|
184
|
+
}>>;
|
|
185
|
+
message: z.ZodString;
|
|
186
|
+
}, z.core.$strict>>;
|
|
187
|
+
}, z.core.$strict>;
|
|
188
|
+
/**
|
|
189
|
+
* Build the manifest of managed files. The composed persona records its base,
|
|
190
|
+
* its resolved resource ids and its permission digest; every value is derived,
|
|
191
|
+
* never timestamped. The renderer identity is the build's pin (see
|
|
192
|
+
* `prepareRender`), so the bytes are identical on two machines with the same
|
|
193
|
+
* source and toolchain.
|
|
194
|
+
*
|
|
195
|
+
* `warnings` is the source-derived warning set only. A renderer-level warning
|
|
196
|
+
* (such as `W114_RENDERER_INTEGRITY_UNVERIFIED`) is evidence about the machine
|
|
197
|
+
* that ran, not about the source, so `prepareRender` keeps it out of this
|
|
198
|
+
* document: writing it here made a verified and an unverified machine produce
|
|
199
|
+
* different lockfile bytes for the same source, and `check` reported drift for
|
|
200
|
+
* a tree whose source had not changed. The command still reports it. The
|
|
201
|
+
* lockfile does not list itself.
|
|
202
|
+
*
|
|
203
|
+
* A `structured` file records the digest and size of its owned subset, never of
|
|
204
|
+
* the whole file: the rest of that file is the user's, and recording it would
|
|
205
|
+
* make the lockfile depend on user content (two roots with the same source would
|
|
206
|
+
* no longer produce identical bytes, and a foreign-key edit would look like
|
|
207
|
+
* lockfile drift).
|
|
208
|
+
*/
|
|
209
|
+
export declare function buildLockfile(source: ResolvedSource, renderer: LockfileRenderer, files: RenderedFile[], warnings: readonly Warning[], derived: {
|
|
210
|
+
configDigest: string;
|
|
211
|
+
notLive: readonly string[];
|
|
212
|
+
}): Lockfile;
|
|
213
|
+
export declare function serialiseLockfile(lock: Lockfile): string;
|
|
214
|
+
/**
|
|
215
|
+
* Read the committed lockfile and validate the whole document. `check` reads it
|
|
216
|
+
* before comparing bytes, so it is guarded like any other input: `.farna` must
|
|
217
|
+
* be a real directory and the lockfile a real regular file of bounded size,
|
|
218
|
+
* opened with `O_NOFOLLOW`.
|
|
219
|
+
*
|
|
220
|
+
* Returns `undefined` only when the lockfile is absent. A lockfile that is
|
|
221
|
+
* present but unreadable, malformed, or of an incompatible version is a typed
|
|
222
|
+
* F001 naming `.farna/lock.json` and its recovery; it is never treated as "no
|
|
223
|
+
* records". A symlink, a FIFO, a device node or an oversized file is a typed
|
|
224
|
+
* F003 rather than a follow or a hang. Any other filesystem failure is a typed
|
|
225
|
+
* F001 naming the repository path, never a raw `EACCES`.
|
|
226
|
+
*/
|
|
227
|
+
export declare function readLockfile(repoRoot: string): Promise<Lockfile | undefined>;
|
|
228
|
+
/** The lockfile entry recorded for one managed path, when there is one. */
|
|
229
|
+
export declare function recordedEntry(lockfile: Lockfile | undefined, path: string): LockFileEntry | undefined;
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
import { constants } from 'node:fs';
|
|
2
|
+
import { open } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { LOCK_FILE, LOCK_PATH, LOCKFILE_MAX_BYTES, MODE_AGENT, MODE_PROFILE, PRODUCT_NAME, PRODUCT_VERSION, RENDER_MODES, ROOT_DIR, SCHEMA_VERSION, TARGET_ENUM, WARNING_CODES, } from '../core/constants.js';
|
|
6
|
+
import { invalidConfig, pathUnsafe } from '../core/errors.js';
|
|
7
|
+
import { decodeUtf8 } from '../core/fs.js';
|
|
8
|
+
import { canonicalJsonFile, compareCodepoints, sha256Hex } from '../core/json.js';
|
|
9
|
+
import { assertRealDirectory, assertRealFile } from '../core/paths.js';
|
|
10
|
+
import { hasControlCharacters } from '../core/text.js';
|
|
11
|
+
const sha256Schema = z.string().regex(/^[0-9a-f]{64}$/, 'must be a lower-case SHA-256 hex digest');
|
|
12
|
+
const lockResourceSchema = z.strictObject({
|
|
13
|
+
kind: z.enum(['rule', 'permission', 'mcp', 'skill', 'command', 'hook']),
|
|
14
|
+
id: z.string().min(1),
|
|
15
|
+
sha256: sha256Schema,
|
|
16
|
+
fromPersona: z.string().min(1),
|
|
17
|
+
});
|
|
18
|
+
const lockPersonaSchema = z.strictObject({
|
|
19
|
+
id: z.string().min(1),
|
|
20
|
+
extends: z.string().min(1).optional(),
|
|
21
|
+
digest: sha256Schema,
|
|
22
|
+
permissionDigest: sha256Schema,
|
|
23
|
+
resources: z.array(lockResourceSchema),
|
|
24
|
+
});
|
|
25
|
+
const lockWarningSchema = z.strictObject({
|
|
26
|
+
code: z.enum(WARNING_CODES),
|
|
27
|
+
severity: z.enum(['error', 'high', 'medium', 'low', 'info']),
|
|
28
|
+
persona: z.string().min(1),
|
|
29
|
+
target: z.string().min(1),
|
|
30
|
+
capability: z.enum(['rule', 'mcp', 'skill', 'command', 'permission', 'hook', 'model']).optional(),
|
|
31
|
+
message: z.string().min(1),
|
|
32
|
+
});
|
|
33
|
+
/**
|
|
34
|
+
* A managed path from a document a user could hand-edit. The reader is the
|
|
35
|
+
* boundary: a path with a control character, an absolute path, a backslash or a
|
|
36
|
+
* `..` segment is refused before it can reach a filesystem call or a terminal.
|
|
37
|
+
*/
|
|
38
|
+
function isSafeManagedPath(path) {
|
|
39
|
+
return (path.length > 0 &&
|
|
40
|
+
!hasControlCharacters(path) &&
|
|
41
|
+
!path.includes('\\') &&
|
|
42
|
+
!path.startsWith('/') &&
|
|
43
|
+
!path.split('/').includes('..') &&
|
|
44
|
+
path !== '.');
|
|
45
|
+
}
|
|
46
|
+
const managedPathSchema = z
|
|
47
|
+
.string()
|
|
48
|
+
.refine(isSafeManagedPath, 'must be a relative, escape-free, control-character-free path');
|
|
49
|
+
const lockFileEntrySchema = z
|
|
50
|
+
.strictObject({
|
|
51
|
+
path: managedPathSchema,
|
|
52
|
+
sha256: sha256Schema,
|
|
53
|
+
size: z.number().int().nonnegative(),
|
|
54
|
+
shape: z.enum(['whole', 'tree', 'structured', 'region']).optional(),
|
|
55
|
+
ownedKeys: z.array(z.string().min(1)).optional(),
|
|
56
|
+
})
|
|
57
|
+
.superRefine((entry, context) => {
|
|
58
|
+
if (entry.shape === 'structured') {
|
|
59
|
+
if (entry.ownedKeys === undefined || entry.ownedKeys.length === 0) {
|
|
60
|
+
context.addIssue({
|
|
61
|
+
code: 'custom',
|
|
62
|
+
path: ['ownedKeys'],
|
|
63
|
+
message: 'a structured entry names the keys it owns',
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (entry.ownedKeys !== undefined) {
|
|
69
|
+
context.addIssue({
|
|
70
|
+
code: 'custom',
|
|
71
|
+
path: ['ownedKeys'],
|
|
72
|
+
message: 'only a structured entry owns keys',
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
/**
|
|
77
|
+
* The document as it may appear on disk. `mode` and `activePersona` are optional
|
|
78
|
+
* here so a lockfile written before profile mode existed still parses: treating
|
|
79
|
+
* a present-but-older lockfile as absent discarded the recorded path set, which
|
|
80
|
+
* skipped orphan removal and let a removed persona's artefacts stay live while
|
|
81
|
+
* `check` reported clean. A present document is either understood or refused
|
|
82
|
+
* with a typed error; it is never silently ignored.
|
|
83
|
+
*/
|
|
84
|
+
export const lockfileDocumentSchema = z.strictObject({
|
|
85
|
+
schemaVersion: z.literal(SCHEMA_VERSION),
|
|
86
|
+
product: z.strictObject({ name: z.string().min(1), version: z.string().min(1) }),
|
|
87
|
+
/**
|
|
88
|
+
* The target that produced the committed bytes. It is the closed set this
|
|
89
|
+
* build renders: a lockfile naming anything else is not one this build can
|
|
90
|
+
* re-render, and treating it as merely stale would let `check` compare a tree
|
|
91
|
+
* against a target the config cannot select.
|
|
92
|
+
*/
|
|
93
|
+
target: z.enum(TARGET_ENUM),
|
|
94
|
+
mode: z.enum(RENDER_MODES).optional(),
|
|
95
|
+
/** The persona selected by `targets.<t>.active` or `--persona`; null in agent mode. */
|
|
96
|
+
activePersona: z.string().min(1).nullable().optional(),
|
|
97
|
+
renderer: z.strictObject({
|
|
98
|
+
name: z.string().min(1),
|
|
99
|
+
version: z.string().min(1),
|
|
100
|
+
// Written by every current build, but optional in the schema so a legacy
|
|
101
|
+
// lockfile is read and then refused by the identity check (F010) rather
|
|
102
|
+
// than silently treated as drift.
|
|
103
|
+
integrity: z.string().min(1).optional(),
|
|
104
|
+
}),
|
|
105
|
+
configDigest: sha256Schema.optional(),
|
|
106
|
+
personas: z.array(lockPersonaSchema),
|
|
107
|
+
notLive: z.array(z.string().min(1)).optional(),
|
|
108
|
+
files: z.array(lockFileEntrySchema),
|
|
109
|
+
warnings: z.array(lockWarningSchema),
|
|
110
|
+
});
|
|
111
|
+
/**
|
|
112
|
+
* Build the manifest of managed files. The composed persona records its base,
|
|
113
|
+
* its resolved resource ids and its permission digest; every value is derived,
|
|
114
|
+
* never timestamped. The renderer identity is the build's pin (see
|
|
115
|
+
* `prepareRender`), so the bytes are identical on two machines with the same
|
|
116
|
+
* source and toolchain.
|
|
117
|
+
*
|
|
118
|
+
* `warnings` is the source-derived warning set only. A renderer-level warning
|
|
119
|
+
* (such as `W114_RENDERER_INTEGRITY_UNVERIFIED`) is evidence about the machine
|
|
120
|
+
* that ran, not about the source, so `prepareRender` keeps it out of this
|
|
121
|
+
* document: writing it here made a verified and an unverified machine produce
|
|
122
|
+
* different lockfile bytes for the same source, and `check` reported drift for
|
|
123
|
+
* a tree whose source had not changed. The command still reports it. The
|
|
124
|
+
* lockfile does not list itself.
|
|
125
|
+
*
|
|
126
|
+
* A `structured` file records the digest and size of its owned subset, never of
|
|
127
|
+
* the whole file: the rest of that file is the user's, and recording it would
|
|
128
|
+
* make the lockfile depend on user content (two roots with the same source would
|
|
129
|
+
* no longer produce identical bytes, and a foreign-key edit would look like
|
|
130
|
+
* lockfile drift).
|
|
131
|
+
*/
|
|
132
|
+
export function buildLockfile(source, renderer, files, warnings, derived) {
|
|
133
|
+
const personas = source.personas
|
|
134
|
+
.map((persona) => ({
|
|
135
|
+
id: persona.id,
|
|
136
|
+
...(persona.extends === undefined ? {} : { extends: persona.extends }),
|
|
137
|
+
digest: persona.digest,
|
|
138
|
+
permissionDigest: persona.permissionDigest,
|
|
139
|
+
resources: persona.resources.map((resource) => ({
|
|
140
|
+
kind: resource.kind,
|
|
141
|
+
id: resource.id,
|
|
142
|
+
sha256: resource.sha256,
|
|
143
|
+
fromPersona: resource.fromPersona,
|
|
144
|
+
})),
|
|
145
|
+
}))
|
|
146
|
+
.sort((a, b) => compareCodepoints(a.id, b.id));
|
|
147
|
+
return {
|
|
148
|
+
schemaVersion: SCHEMA_VERSION,
|
|
149
|
+
product: { name: PRODUCT_NAME, version: PRODUCT_VERSION },
|
|
150
|
+
target: source.target,
|
|
151
|
+
mode: source.mode,
|
|
152
|
+
activePersona: source.active,
|
|
153
|
+
renderer,
|
|
154
|
+
configDigest: derived.configDigest,
|
|
155
|
+
personas,
|
|
156
|
+
notLive: [...derived.notLive].sort(compareCodepoints),
|
|
157
|
+
files: files
|
|
158
|
+
.map((file) => {
|
|
159
|
+
// A `structured` entry must name the keys it owns; the reader refuses
|
|
160
|
+
// one with an empty `ownedKeys`. A file that declares the shape but owns
|
|
161
|
+
// no key (the generated document has none of them) is recorded whole
|
|
162
|
+
// instead, so everything this builder produces reads back. Any other
|
|
163
|
+
// shape is carried through unchanged.
|
|
164
|
+
const ownedStructured = file.shape === 'structured' && file.ownedKeys !== undefined && file.ownedKeys.length > 0;
|
|
165
|
+
const shape = ownedStructured
|
|
166
|
+
? 'structured'
|
|
167
|
+
: file.shape === 'structured'
|
|
168
|
+
? undefined
|
|
169
|
+
: file.shape;
|
|
170
|
+
return {
|
|
171
|
+
path: file.path,
|
|
172
|
+
sha256: ownedStructured
|
|
173
|
+
? (file.ownedSha256 ?? sha256Hex(file.bytes))
|
|
174
|
+
: sha256Hex(file.bytes),
|
|
175
|
+
size: ownedStructured
|
|
176
|
+
? (file.ownedSize ?? Buffer.byteLength(file.bytes))
|
|
177
|
+
: Buffer.byteLength(file.bytes),
|
|
178
|
+
...(shape === undefined ? {} : { shape }),
|
|
179
|
+
...(ownedStructured && file.ownedKeys !== undefined ? { ownedKeys: file.ownedKeys } : {}),
|
|
180
|
+
};
|
|
181
|
+
})
|
|
182
|
+
.sort((a, b) => compareCodepoints(a.path, b.path)),
|
|
183
|
+
warnings: warnings.map((entry) => ({
|
|
184
|
+
code: entry.code,
|
|
185
|
+
severity: entry.severity,
|
|
186
|
+
persona: entry.persona,
|
|
187
|
+
target: entry.target,
|
|
188
|
+
...(entry.capability === undefined ? {} : { capability: entry.capability }),
|
|
189
|
+
message: entry.message,
|
|
190
|
+
})),
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
export function serialiseLockfile(lock) {
|
|
194
|
+
return canonicalJsonFile(lock);
|
|
195
|
+
}
|
|
196
|
+
const NO_FOLLOW = constants.O_NOFOLLOW ?? 0;
|
|
197
|
+
const NO_BLOCK = constants.O_NONBLOCK ?? 0;
|
|
198
|
+
function errorCode(error) {
|
|
199
|
+
return error instanceof Error && 'code' in error && typeof error.code === 'string'
|
|
200
|
+
? error.code
|
|
201
|
+
: undefined;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* A present lockfile that cannot be understood. It is never reported as "no
|
|
205
|
+
* records": doing that made orphan removal a no-op and rewrote a lockfile that
|
|
206
|
+
* had forgotten every earlier path, while `check` called the tree clean.
|
|
207
|
+
*/
|
|
208
|
+
function malformedLockfile(detail) {
|
|
209
|
+
return invalidConfig(`${LOCK_PATH} ${detail}`, {
|
|
210
|
+
subject: LOCK_PATH,
|
|
211
|
+
action: 'delete the lockfile and re-run generate; a lockfile is derived from the source and small',
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
function describeLockIssue(error) {
|
|
215
|
+
const issue = error.issues[0];
|
|
216
|
+
if (issue === undefined) {
|
|
217
|
+
return 'did not validate';
|
|
218
|
+
}
|
|
219
|
+
const pointer = issue.path.join('.');
|
|
220
|
+
return pointer === '' ? issue.message : `${pointer}: ${issue.message}`;
|
|
221
|
+
}
|
|
222
|
+
function parseLockfileBytes(bytes) {
|
|
223
|
+
let text;
|
|
224
|
+
try {
|
|
225
|
+
text = decodeUtf8(bytes, LOCK_PATH);
|
|
226
|
+
}
|
|
227
|
+
catch {
|
|
228
|
+
throw malformedLockfile('is not valid UTF-8');
|
|
229
|
+
}
|
|
230
|
+
let value;
|
|
231
|
+
try {
|
|
232
|
+
value = JSON.parse(text);
|
|
233
|
+
}
|
|
234
|
+
catch {
|
|
235
|
+
throw malformedLockfile('is not valid JSON');
|
|
236
|
+
}
|
|
237
|
+
const parsed = lockfileDocumentSchema.safeParse(value);
|
|
238
|
+
if (!parsed.success) {
|
|
239
|
+
throw malformedLockfile(`did not validate (${describeLockIssue(parsed.error)})`);
|
|
240
|
+
}
|
|
241
|
+
const document = parsed.data;
|
|
242
|
+
// A legacy lockfile has neither field: it was written when every target was
|
|
243
|
+
// in agent mode, so the honest normalisation is agent mode with no active
|
|
244
|
+
// persona. A document that names profile mode must name the persona.
|
|
245
|
+
const mode = document.mode ?? (document.activePersona != null ? MODE_PROFILE : MODE_AGENT);
|
|
246
|
+
const activePersona = mode === MODE_PROFILE ? (document.activePersona ?? null) : null;
|
|
247
|
+
if (mode === MODE_PROFILE && activePersona === null) {
|
|
248
|
+
throw malformedLockfile('records profile mode without an active persona');
|
|
249
|
+
}
|
|
250
|
+
return {
|
|
251
|
+
...document,
|
|
252
|
+
mode,
|
|
253
|
+
activePersona,
|
|
254
|
+
notLive: [...(document.notLive ?? [])].sort(compareCodepoints),
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Read the committed lockfile and validate the whole document. `check` reads it
|
|
259
|
+
* before comparing bytes, so it is guarded like any other input: `.farna` must
|
|
260
|
+
* be a real directory and the lockfile a real regular file of bounded size,
|
|
261
|
+
* opened with `O_NOFOLLOW`.
|
|
262
|
+
*
|
|
263
|
+
* Returns `undefined` only when the lockfile is absent. A lockfile that is
|
|
264
|
+
* present but unreadable, malformed, or of an incompatible version is a typed
|
|
265
|
+
* F001 naming `.farna/lock.json` and its recovery; it is never treated as "no
|
|
266
|
+
* records". A symlink, a FIFO, a device node or an oversized file is a typed
|
|
267
|
+
* F003 rather than a follow or a hang. Any other filesystem failure is a typed
|
|
268
|
+
* F001 naming the repository path, never a raw `EACCES`.
|
|
269
|
+
*/
|
|
270
|
+
export async function readLockfile(repoRoot) {
|
|
271
|
+
const subject = LOCK_PATH;
|
|
272
|
+
await assertRealDirectory(join(repoRoot, ROOT_DIR), ROOT_DIR);
|
|
273
|
+
const path = join(repoRoot, ROOT_DIR, LOCK_FILE);
|
|
274
|
+
const stats = await assertRealFile(path, subject);
|
|
275
|
+
if (stats === undefined) {
|
|
276
|
+
return undefined;
|
|
277
|
+
}
|
|
278
|
+
if (stats.size > LOCKFILE_MAX_BYTES) {
|
|
279
|
+
throw pathUnsafe(`${subject} is larger than ${LOCKFILE_MAX_BYTES} bytes; refusing to read it`, {
|
|
280
|
+
subject,
|
|
281
|
+
action: 'delete the lockfile and re-run generate; a lockfile is derived and small',
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
let handle;
|
|
285
|
+
try {
|
|
286
|
+
// O_NOFOLLOW closes the window between the lstat above and the open: a
|
|
287
|
+
// symlink swapped in is refused (ELOOP), never followed.
|
|
288
|
+
// O_NONBLOCK matters only in the race where the path becomes a FIFO after
|
|
289
|
+
// the lstat: without it the open would block forever waiting for a writer.
|
|
290
|
+
handle = await open(path, constants.O_RDONLY | NO_FOLLOW | NO_BLOCK);
|
|
291
|
+
}
|
|
292
|
+
catch (error) {
|
|
293
|
+
const code = errorCode(error);
|
|
294
|
+
if (code === 'ENOENT') {
|
|
295
|
+
return undefined;
|
|
296
|
+
}
|
|
297
|
+
if (code === 'ELOOP') {
|
|
298
|
+
throw pathUnsafe('symlink not allowed: .farna/lock.json', {
|
|
299
|
+
subject,
|
|
300
|
+
action: 'replace the symbolic link with a real file',
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
throw invalidConfig(`cannot read ${subject}`, {
|
|
304
|
+
subject,
|
|
305
|
+
action: 'make the lockfile readable, or delete it and re-run generate; a lockfile is derived from the source',
|
|
306
|
+
cause: error,
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
try {
|
|
310
|
+
// The open fd is authoritative: the path may have changed after the lstat.
|
|
311
|
+
const opened = await handle.stat();
|
|
312
|
+
if (!opened.isFile()) {
|
|
313
|
+
throw pathUnsafe(`${subject} is not a regular file`, {
|
|
314
|
+
subject,
|
|
315
|
+
action: 'delete it and re-run generate; a lockfile is a regular file',
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
if (opened.size > LOCKFILE_MAX_BYTES) {
|
|
319
|
+
throw pathUnsafe(`${subject} is larger than ${LOCKFILE_MAX_BYTES} bytes; refusing to read it`, {
|
|
320
|
+
subject,
|
|
321
|
+
action: 'delete the lockfile and re-run generate; a lockfile is derived and small',
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
return parseLockfileBytes(await handle.readFile());
|
|
325
|
+
}
|
|
326
|
+
finally {
|
|
327
|
+
await handle.close();
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
/** The lockfile entry recorded for one managed path, when there is one. */
|
|
331
|
+
export function recordedEntry(lockfile, path) {
|
|
332
|
+
return lockfile?.files.find((file) => file.path === path);
|
|
333
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { ResolvedPersona, ResolvedSource } from '../compose/resolve.js';
|
|
2
|
+
import { type OpencodeTarget, type SupportedTarget } from '../core/constants.js';
|
|
3
|
+
import type { RenderFeature } from '../render/port.js';
|
|
4
|
+
/**
|
|
5
|
+
* Join rule bodies into the persona body, in canonical order. The rule body
|
|
6
|
+
* itself is never re-wrapped or re-indented. This is the only place a persona's
|
|
7
|
+
* rules are carried on either target: there is no shared root rule file.
|
|
8
|
+
*/
|
|
9
|
+
export declare function personaBody(persona: ResolvedPersona): string;
|
|
10
|
+
/**
|
|
11
|
+
* A workspace resource body, with only its boundary newlines removed. The
|
|
12
|
+
* newlines around the body are file layout; the text between them is never
|
|
13
|
+
* re-wrapped or re-indented.
|
|
14
|
+
*/
|
|
15
|
+
export declare function resourceBody(body: string): string;
|
|
16
|
+
/**
|
|
17
|
+
* Whether this persona's source form carries `tools`. The target reads it as an
|
|
18
|
+
* allow-list, so it is emitted only when a permission resource opted in AND at
|
|
19
|
+
* least one entry survived. `claude.allowlist` is that effective decision (see
|
|
20
|
+
* `mapPermissions`), so it is the one place the question is answered; a bare
|
|
21
|
+
* `read: allow` must not silently remove Glob, Grep, WebFetch and every other
|
|
22
|
+
* tool the product does not model.
|
|
23
|
+
*/
|
|
24
|
+
export declare function emitsTools(persona: ResolvedPersona): boolean;
|
|
25
|
+
export type ClaudeBlock = {
|
|
26
|
+
model?: string;
|
|
27
|
+
tools?: string[];
|
|
28
|
+
disallowedTools?: string[];
|
|
29
|
+
};
|
|
30
|
+
export type OpencodePersonaSection = {
|
|
31
|
+
mode: 'subagent';
|
|
32
|
+
model?: string;
|
|
33
|
+
permission?: Record<string, Record<string, 'allow' | 'deny'>>;
|
|
34
|
+
};
|
|
35
|
+
export type PersonaFrontmatter = {
|
|
36
|
+
name: string;
|
|
37
|
+
targets: string[];
|
|
38
|
+
description: string;
|
|
39
|
+
claudecode?: ClaudeBlock;
|
|
40
|
+
opencode?: OpencodePersonaSection;
|
|
41
|
+
opencodev1?: OpencodePersonaSection;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* The renderer source form for the Claude Code per-agent block. `tools` and
|
|
45
|
+
* `disallowedTools` already hold the harness's own tool names (`Agent`, `Bash`,
|
|
46
|
+
* `Edit`, `Read`, `Write`, and `Bash(P)` for a pattern), never a product category.
|
|
47
|
+
*/
|
|
48
|
+
export declare function personaFrontmatter(persona: ResolvedPersona): PersonaFrontmatter;
|
|
49
|
+
export declare function personaSourceFile(persona: ResolvedPersona): string;
|
|
50
|
+
/**
|
|
51
|
+
* The renderer source form for an OpenCode per-agent block. The V2 target
|
|
52
|
+
* (`opencode`) emits `description`, `mode`, `model` and the ordered
|
|
53
|
+
* `permissions` array; the V1 target (`opencodev1`) emits `name`,
|
|
54
|
+
* `description`, `mode`, `model` and the per-tool `permission` map. Both are
|
|
55
|
+
* authored from the same resolved permission map under rulesync's canonical
|
|
56
|
+
* category names (`read`, `edit`, `bash`, `task`): the adapter chooses the
|
|
57
|
+
* emitted shape, so the product never restates one target's shape for the
|
|
58
|
+
* other.
|
|
59
|
+
*/
|
|
60
|
+
export declare function opencodePersonaFrontmatter(persona: ResolvedPersona, target: OpencodeTarget): PersonaFrontmatter;
|
|
61
|
+
export declare function opencodePersonaSourceFile(persona: ResolvedPersona, target: OpencodeTarget): string;
|
|
62
|
+
/** The rulesync features one render of this source needs, never a wildcard. */
|
|
63
|
+
export declare function renderFeatures(target: SupportedTarget, source: ResolvedSource): RenderFeature[];
|
|
64
|
+
/**
|
|
65
|
+
* Write the rulesync-shaped source tree into the stage. One target is rendered
|
|
66
|
+
* per run, so the stage is already target-scoped.
|
|
67
|
+
*
|
|
68
|
+
* On Claude Code a persona is one subagent and nothing else is authored: a
|
|
69
|
+
* shared permission, MCP or hook block would leak one persona's restriction
|
|
70
|
+
* onto every other persona.
|
|
71
|
+
*
|
|
72
|
+
* On OpenCode the MCP server, skill, command and hook declarations are
|
|
73
|
+
* workspace-level, so they are authored once from the union of the live
|
|
74
|
+
* personas rather than per persona. Rules are never authored as a root file:
|
|
75
|
+
* the per-agent body is the only rule carrier.
|
|
76
|
+
*/
|
|
77
|
+
export declare function materialiseSourceTree(sourceRoot: string, source: ResolvedSource): Promise<void>;
|