farna-bootstrapper 0.0.0-stage → 0.1.1
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 +107 -0
- package/LICENSE +30 -0
- package/NOTICE +42 -0
- package/README.md +1102 -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 +161 -0
- package/dist/compose/resolve.js +576 -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 +279 -0
- package/dist/core/constants.js +351 -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 +230 -0
- package/dist/lock/lockfile.js +333 -0
- package/dist/materialise/source-tree.d.ts +77 -0
- package/dist/materialise/source-tree.js +267 -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 +154 -0
- package/dist/source/schema.js +292 -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
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Text that reaches a human line. A subject can come from a source tree, a
|
|
3
|
+
* lockfile a user can hand-edit, or a renderer, so it is never printed raw: a C0
|
|
4
|
+
* or C1 control character would let a path rewrite the terminal line it is
|
|
5
|
+
* printed on (carriage return, backspace, an ANSI escape introducer).
|
|
6
|
+
*/
|
|
7
|
+
/** True when the string contains a C0, DEL or C1 control character. */
|
|
8
|
+
export declare function hasControlCharacters(value: string): boolean;
|
|
9
|
+
/** Escape every control character as a visible `\uXXXX` sequence. */
|
|
10
|
+
export declare function escapeControlCharacters(value: string): string;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Text that reaches a human line. A subject can come from a source tree, a
|
|
3
|
+
* lockfile a user can hand-edit, or a renderer, so it is never printed raw: a C0
|
|
4
|
+
* or C1 control character would let a path rewrite the terminal line it is
|
|
5
|
+
* printed on (carriage return, backspace, an ANSI escape introducer).
|
|
6
|
+
*/
|
|
7
|
+
function isControlCode(code) {
|
|
8
|
+
return code < 0x20 || (code >= 0x7f && code <= 0x9f);
|
|
9
|
+
}
|
|
10
|
+
/** True when the string contains a C0, DEL or C1 control character. */
|
|
11
|
+
export function hasControlCharacters(value) {
|
|
12
|
+
for (const character of value) {
|
|
13
|
+
if (isControlCode(character.codePointAt(0) ?? 0)) {
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
/** Escape every control character as a visible `\uXXXX` sequence. */
|
|
20
|
+
export function escapeControlCharacters(value) {
|
|
21
|
+
let escaped = '';
|
|
22
|
+
for (const character of value) {
|
|
23
|
+
const code = character.codePointAt(0) ?? 0;
|
|
24
|
+
escaped += isControlCode(code) ? `\\u${code.toString(16).padStart(4, '0')}` : character;
|
|
25
|
+
}
|
|
26
|
+
return escaped;
|
|
27
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public types only. The CLI is the product surface in this slice; behaviour
|
|
3
|
+
* lives behind the command modules.
|
|
4
|
+
*/
|
|
5
|
+
export type { Config, TargetConfig } from './config/schema.js';
|
|
6
|
+
export type { Severity, WarningCode } from './core/constants.js';
|
|
7
|
+
export type { Lockfile } from './lock/lockfile.js';
|
|
8
|
+
export type { PermissionCategory, PermissionKey, PermissionPatterns, PermissionTools, PermissionValue, } from './permissions/permissions.js';
|
|
9
|
+
export type { RendererIdentity, RenderRequest, RenderResult, RenderTarget } from './render/port.js';
|
|
10
|
+
export type { Warning } from './report/warnings.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,230 @@
|
|
|
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
|
+
W117_PERSONA_MODE_IGNORED: "W117_PERSONA_MODE_IGNORED";
|
|
167
|
+
}>;
|
|
168
|
+
severity: z.ZodEnum<{
|
|
169
|
+
error: "error";
|
|
170
|
+
high: "high";
|
|
171
|
+
medium: "medium";
|
|
172
|
+
low: "low";
|
|
173
|
+
info: "info";
|
|
174
|
+
}>;
|
|
175
|
+
persona: z.ZodString;
|
|
176
|
+
target: z.ZodString;
|
|
177
|
+
capability: z.ZodOptional<z.ZodEnum<{
|
|
178
|
+
mcp: "mcp";
|
|
179
|
+
rule: "rule";
|
|
180
|
+
skill: "skill";
|
|
181
|
+
command: "command";
|
|
182
|
+
permission: "permission";
|
|
183
|
+
hook: "hook";
|
|
184
|
+
model: "model";
|
|
185
|
+
}>>;
|
|
186
|
+
message: z.ZodString;
|
|
187
|
+
}, z.core.$strict>>;
|
|
188
|
+
}, z.core.$strict>;
|
|
189
|
+
/**
|
|
190
|
+
* Build the manifest of managed files. The composed persona records its base,
|
|
191
|
+
* its resolved resource ids and its permission digest; every value is derived,
|
|
192
|
+
* never timestamped. The renderer identity is the build's pin (see
|
|
193
|
+
* `prepareRender`), so the bytes are identical on two machines with the same
|
|
194
|
+
* source and toolchain.
|
|
195
|
+
*
|
|
196
|
+
* `warnings` is the source-derived warning set only. A renderer-level warning
|
|
197
|
+
* (such as `W114_RENDERER_INTEGRITY_UNVERIFIED`) is evidence about the machine
|
|
198
|
+
* that ran, not about the source, so `prepareRender` keeps it out of this
|
|
199
|
+
* document: writing it here made a verified and an unverified machine produce
|
|
200
|
+
* different lockfile bytes for the same source, and `check` reported drift for
|
|
201
|
+
* a tree whose source had not changed. The command still reports it. The
|
|
202
|
+
* lockfile does not list itself.
|
|
203
|
+
*
|
|
204
|
+
* A `structured` file records the digest and size of its owned subset, never of
|
|
205
|
+
* the whole file: the rest of that file is the user's, and recording it would
|
|
206
|
+
* make the lockfile depend on user content (two roots with the same source would
|
|
207
|
+
* no longer produce identical bytes, and a foreign-key edit would look like
|
|
208
|
+
* lockfile drift).
|
|
209
|
+
*/
|
|
210
|
+
export declare function buildLockfile(source: ResolvedSource, renderer: LockfileRenderer, files: RenderedFile[], warnings: readonly Warning[], derived: {
|
|
211
|
+
configDigest: string;
|
|
212
|
+
notLive: readonly string[];
|
|
213
|
+
}): Lockfile;
|
|
214
|
+
export declare function serialiseLockfile(lock: Lockfile): string;
|
|
215
|
+
/**
|
|
216
|
+
* Read the committed lockfile and validate the whole document. `check` reads it
|
|
217
|
+
* before comparing bytes, so it is guarded like any other input: `.farna` must
|
|
218
|
+
* be a real directory and the lockfile a real regular file of bounded size,
|
|
219
|
+
* opened with `O_NOFOLLOW`.
|
|
220
|
+
*
|
|
221
|
+
* Returns `undefined` only when the lockfile is absent. A lockfile that is
|
|
222
|
+
* present but unreadable, malformed, or of an incompatible version is a typed
|
|
223
|
+
* F001 naming `.farna/lock.json` and its recovery; it is never treated as "no
|
|
224
|
+
* records". A symlink, a FIFO, a device node or an oversized file is a typed
|
|
225
|
+
* F003 rather than a follow or a hang. Any other filesystem failure is a typed
|
|
226
|
+
* F001 naming the repository path, never a raw `EACCES`.
|
|
227
|
+
*/
|
|
228
|
+
export declare function readLockfile(repoRoot: string): Promise<Lockfile | undefined>;
|
|
229
|
+
/** The lockfile entry recorded for one managed path, when there is one. */
|
|
230
|
+
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
|
+
}
|