@feltdb/feltkit 0.3.21 → 0.3.23
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/dist/app.d.ts +17 -3
- package/dist/app.js +10 -0
- package/dist/app.js.map +1 -1
- package/dist/appboundry/build.js +4 -0
- package/dist/appboundry/build.js.map +1 -1
- package/dist/authorize.d.ts +1 -1
- package/dist/cli/main.js +8 -152
- package/dist/cli/main.js.map +1 -1
- package/dist/client/index.d.ts +83 -4
- package/dist/client/index.js +154 -2
- package/dist/client/index.js.map +1 -1
- package/dist/dashboard/assets/index-BEjtLLYq.js +15 -0
- package/dist/dashboard/index.html +1 -1
- package/dist/docs.d.ts +1 -1
- package/dist/docs.js +1 -0
- package/dist/docs.js.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.js.map +1 -1
- package/dist/platform/approvals.d.ts +102 -0
- package/dist/platform/approvals.js +155 -0
- package/dist/platform/approvals.js.map +1 -0
- package/dist/platform/artifacts.d.ts +126 -0
- package/dist/platform/artifacts.js +251 -0
- package/dist/platform/artifacts.js.map +1 -0
- package/dist/platform/blobs.d.ts +25 -0
- package/dist/platform/blobs.js +78 -0
- package/dist/platform/blobs.js.map +1 -0
- package/dist/platform/diff.d.ts +23 -0
- package/dist/platform/diff.js +107 -0
- package/dist/platform/diff.js.map +1 -0
- package/dist/platform/exec.d.ts +182 -0
- package/dist/platform/exec.js +575 -0
- package/dist/platform/exec.js.map +1 -0
- package/dist/platform/operations.d.ts +30 -13
- package/dist/platform/operations.js +8 -17
- package/dist/platform/operations.js.map +1 -1
- package/dist/platform/permissions.d.ts +15 -0
- package/dist/platform/permissions.js +16 -0
- package/dist/platform/permissions.js.map +1 -0
- package/dist/platform/platform.d.ts +28 -0
- package/dist/platform/platform.js +84 -5
- package/dist/platform/platform.js.map +1 -1
- package/dist/platform/releases.d.ts +110 -0
- package/dist/platform/releases.js +344 -0
- package/dist/platform/releases.js.map +1 -0
- package/dist/platform/review.d.ts +10 -0
- package/dist/platform/review.js +109 -0
- package/dist/platform/review.js.map +1 -0
- package/dist/platform/server.js +7 -0
- package/dist/platform/server.js.map +1 -1
- package/dist/platform/source.d.ts +2 -0
- package/dist/platform/source.js +1 -1
- package/dist/platform/source.js.map +1 -1
- package/dist/platform/workspace-operations.d.ts +2 -0
- package/dist/platform/workspace-operations.js +590 -0
- package/dist/platform/workspace-operations.js.map +1 -0
- package/dist/platform/workspaces.d.ts +211 -0
- package/dist/platform/workspaces.js +435 -0
- package/dist/platform/workspaces.js.map +1 -0
- package/dist/react/index.d.ts +29 -1
- package/dist/react/index.js +50 -0
- package/dist/react/index.js.map +1 -1
- package/dist/runtime/activity.d.ts +1 -1
- package/dist/runtime/agent.d.ts +216 -2
- package/dist/runtime/agent.js +333 -7
- package/dist/runtime/agent.js.map +1 -1
- package/dist/runtime/approval-rules.d.ts +16 -0
- package/dist/runtime/approval-rules.js +37 -0
- package/dist/runtime/approval-rules.js.map +1 -0
- package/dist/runtime/authority.d.ts +18 -1
- package/dist/runtime/authority.js +26 -1
- package/dist/runtime/authority.js.map +1 -1
- package/dist/runtime/browser-inference.d.ts +52 -0
- package/dist/runtime/browser-inference.js +53 -0
- package/dist/runtime/browser-inference.js.map +1 -0
- package/dist/runtime/context.d.ts +110 -0
- package/dist/runtime/decisions.d.ts +23 -0
- package/dist/runtime/decisions.js +54 -0
- package/dist/runtime/decisions.js.map +1 -1
- package/dist/runtime/generation.d.ts +94 -0
- package/dist/runtime/generation.js +295 -0
- package/dist/runtime/generation.js.map +1 -0
- package/dist/runtime/runtime.d.ts +10 -2
- package/dist/runtime/runtime.js +125 -1
- package/dist/runtime/runtime.js.map +1 -1
- package/dist/server/admin.d.ts +9 -1
- package/dist/server/admin.js +84 -2
- package/dist/server/admin.js.map +1 -1
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.js +159 -8
- package/dist/server/index.js.map +1 -1
- package/dist/testing/index.d.ts +11 -0
- package/dist/testing/index.js +7 -3
- package/dist/testing/index.js.map +1 -1
- package/docs/AUTH.md +2 -2
- package/docs/GENERATION.md +136 -0
- package/docs/README.md +3 -2
- package/package.json +2 -4
- package/skills/.claude-plugin/marketplace.json +1 -1
- package/skills/.claude-plugin/plugin.json +1 -1
- package/dist/cli/compose.d.ts +0 -31
- package/dist/cli/compose.js +0 -274
- package/dist/cli/compose.js.map +0 -1
- package/dist/cli/create.d.ts +0 -219
- package/dist/cli/create.js +0 -1193
- package/dist/cli/create.js.map +0 -1
- package/dist/cli/domain.d.ts +0 -174
- package/dist/cli/domain.js +0 -1119
- package/dist/cli/domain.js.map +0 -1
- package/dist/cli/shell.d.ts +0 -7
- package/dist/cli/shell.js +0 -59
- package/dist/cli/shell.js.map +0 -1
- package/dist/composition/catalog.d.ts +0 -3
- package/dist/composition/catalog.js +0 -217
- package/dist/composition/catalog.js.map +0 -1
- package/dist/composition/contract.d.ts +0 -165
- package/dist/composition/contract.js +0 -12
- package/dist/composition/contract.js.map +0 -1
- package/dist/composition/execute.d.ts +0 -110
- package/dist/composition/execute.js +0 -350
- package/dist/composition/execute.js.map +0 -1
- package/dist/composition/generate.d.ts +0 -105
- package/dist/composition/generate.js +0 -274
- package/dist/composition/generate.js.map +0 -1
- package/dist/composition/plan.d.ts +0 -131
- package/dist/composition/plan.js +0 -558
- package/dist/composition/plan.js.map +0 -1
- package/dist/composition/profiles.d.ts +0 -20
- package/dist/composition/profiles.js +0 -81
- package/dist/composition/profiles.js.map +0 -1
- package/dist/composition/reconcile.d.ts +0 -153
- package/dist/composition/reconcile.js +0 -282
- package/dist/composition/reconcile.js.map +0 -1
- package/dist/composition/recover.d.ts +0 -89
- package/dist/composition/recover.js +0 -407
- package/dist/composition/recover.js.map +0 -1
- package/dist/composition/resolve.d.ts +0 -2
- package/dist/composition/resolve.js +0 -208
- package/dist/composition/resolve.js.map +0 -1
- package/dist/dashboard/assets/index-BrC1AtZ1.js +0 -15
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The capability contract. A capability is a named, versioned selection
|
|
3
|
-
* of what FeltKit already offers: it contributes a fragment of the application's `feltkit({...})`
|
|
4
|
-
* declaration and nothing else. Every type here is derived from the public `FeltKitOptions`, so a
|
|
5
|
-
* contribution that names an option the SDK does not have does not compile.
|
|
6
|
-
*
|
|
7
|
-
* This module has no runtime code: the catalog (catalog.ts) holds data and pure functions, and the
|
|
8
|
-
* resolver (resolve.ts) combines them.
|
|
9
|
-
*/
|
|
10
|
-
import type { FeltKitOptions } from "../index.js";
|
|
11
|
-
type Auth = NonNullable<FeltKitOptions["auth"]>;
|
|
12
|
-
type Accounts = NonNullable<Auth["accounts"]>;
|
|
13
|
-
type Tenant = NonNullable<Auth["tenant"]>;
|
|
14
|
-
type Services = NonNullable<FeltKitOptions["services"]>;
|
|
15
|
-
/**
|
|
16
|
-
* What a capability may contribute: the declaration's data options, never a function (an `authorize`
|
|
17
|
-
* hook, an `authenticate` hook or a decision runtime is application code, not composition data).
|
|
18
|
-
* Fragments are partial; the resolver merges them and the result is checked by `feltkit({...})`.
|
|
19
|
-
*/
|
|
20
|
-
export interface FoundationOptions {
|
|
21
|
-
roles?: Record<string, string[]>;
|
|
22
|
-
auth?: {
|
|
23
|
-
accounts?: Partial<Accounts>;
|
|
24
|
-
tenant?: Partial<Tenant>;
|
|
25
|
-
};
|
|
26
|
-
services?: Partial<Services>;
|
|
27
|
-
}
|
|
28
|
-
/** Capability ids in the v1 catalog. A closed union: an unknown id does not compile in catalog or profile code. */
|
|
29
|
-
export type CapabilityId = "identity.accounts" | "workspace.tenancy" | "workspace.invitations" | "agents.membership" | "ops.operator" | "services.files" | "services.notifications" | "services.jobs" | "services.schedules";
|
|
30
|
-
/** A parameter the capability accepts: its JSON-able default (absent means required) and a check. */
|
|
31
|
-
export interface ParameterSpec<T> {
|
|
32
|
-
readonly description: string;
|
|
33
|
-
/** Present: the parameter is optional and takes this value. Absent: required. */
|
|
34
|
-
readonly default?: T;
|
|
35
|
-
/** Returns a problem (what is wrong and what to give instead) or null. */
|
|
36
|
-
readonly check: (value: unknown) => string | null;
|
|
37
|
-
}
|
|
38
|
-
/** What a contribution may read: the resolved parameters of the capabilities selected, and which are selected. */
|
|
39
|
-
export interface ResolutionContext {
|
|
40
|
-
has(id: CapabilityId): boolean;
|
|
41
|
-
params(id: CapabilityId): Readonly<Record<string, unknown>>;
|
|
42
|
-
}
|
|
43
|
-
export interface Capability<P extends Record<string, unknown> = Record<string, unknown>> {
|
|
44
|
-
readonly id: CapabilityId;
|
|
45
|
-
/** Contract major: what the capability guarantees. A change to what it contributes for the same parameters is a new version. */
|
|
46
|
-
readonly version: 1;
|
|
47
|
-
readonly purpose: string;
|
|
48
|
-
readonly parameters: {
|
|
49
|
-
readonly [K in keyof P]-?: ParameterSpec<P[K]>;
|
|
50
|
-
};
|
|
51
|
-
/**
|
|
52
|
-
* Consistency between this capability's own parameters (a rule no declaration option can carry, e.g.
|
|
53
|
-
* the manager roles must be workspace roles). Rules between declaration options stay in feltkit({...}).
|
|
54
|
-
*/
|
|
55
|
-
readonly validate?: (params: Readonly<P>) => ReadonlyArray<{
|
|
56
|
-
parameter: keyof P & string;
|
|
57
|
-
problem: string;
|
|
58
|
-
}>;
|
|
59
|
-
/** Hard dependencies: resolution fails when one is not selected (it is never added silently). */
|
|
60
|
-
readonly requires: readonly CapabilityId[];
|
|
61
|
-
/** Capabilities it cannot be combined with. */
|
|
62
|
-
readonly conflicts: readonly CapabilityId[];
|
|
63
|
-
/**
|
|
64
|
-
* The declaration fragment, a pure function of the parameters. Only values that differ from the
|
|
65
|
-
* SDK's own defaults are set, so a composed declaration reads like a handwritten one.
|
|
66
|
-
*/
|
|
67
|
-
readonly contribute: (params: Readonly<P>, context: ResolutionContext) => FoundationOptions;
|
|
68
|
-
/** Where the SDK implements it and which tests prove it (repository paths). */
|
|
69
|
-
readonly evidence: {
|
|
70
|
-
readonly sdk: readonly string[];
|
|
71
|
-
readonly tests: readonly string[];
|
|
72
|
-
};
|
|
73
|
-
/** Composition owns the contributed declaration; application behavior stays the application's. */
|
|
74
|
-
readonly ownership: "declaration";
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Guarantees the platform enforces for every application, whatever the profile, so a caller may require
|
|
78
|
-
* them (`requireEnforced`) and every result records them. Each names where the SDK enforces it.
|
|
79
|
-
*/
|
|
80
|
-
export declare const ENFORCED_GUARANTEES: ReadonlyArray<{
|
|
81
|
-
readonly id: string;
|
|
82
|
-
readonly guarantee: string;
|
|
83
|
-
readonly enforcedBy: string;
|
|
84
|
-
}>;
|
|
85
|
-
/** A guarantee a profile needs that the platform cannot enforce from the declaration. */
|
|
86
|
-
export interface Limitation {
|
|
87
|
-
readonly id: string;
|
|
88
|
-
readonly requirement: string;
|
|
89
|
-
/** How it is upheld today. */
|
|
90
|
-
readonly enforcedBy: "application-tests" | "application-code";
|
|
91
|
-
readonly evidence: readonly string[];
|
|
92
|
-
/** The smallest platform change that would let FeltKit enforce it. */
|
|
93
|
-
readonly platformChange: string;
|
|
94
|
-
}
|
|
95
|
-
/** Profile ids. A closed union: a profile is a reviewed definition in profiles.ts, never free text. */
|
|
96
|
-
export type ProfileId = "collaborative-workspace" | "individual-accounts";
|
|
97
|
-
export interface Profile {
|
|
98
|
-
readonly id: ProfileId;
|
|
99
|
-
readonly version: 1;
|
|
100
|
-
readonly purpose: string;
|
|
101
|
-
/** Always selected. */
|
|
102
|
-
readonly required: readonly CapabilityId[];
|
|
103
|
-
/** Selected only when the caller includes them. */
|
|
104
|
-
readonly optional: readonly CapabilityId[];
|
|
105
|
-
/** Parameter defaults the profile sets on top of each capability's own. */
|
|
106
|
-
readonly defaults: Partial<Record<CapabilityId, Readonly<Record<string, unknown>>>>;
|
|
107
|
-
/** Parameters the caller must give for this profile, even where the capability has a default. */
|
|
108
|
-
readonly requireParameters: Partial<Record<CapabilityId, readonly string[]>>;
|
|
109
|
-
/** What the profile requires that FeltKit cannot enforce: always reported with the result. */
|
|
110
|
-
readonly limitations: readonly Limitation[];
|
|
111
|
-
}
|
|
112
|
-
/** What the caller asks for. Plain data: the same input gives the same result. */
|
|
113
|
-
export interface CompositionRequest {
|
|
114
|
-
readonly profile: string;
|
|
115
|
-
/** The application's name (`feltkit({ name })`), used to run the SDK's declaration checks. */
|
|
116
|
-
readonly name: string;
|
|
117
|
-
/** Optional capabilities of the profile to include. */
|
|
118
|
-
readonly include?: readonly string[];
|
|
119
|
-
/** Parameters per capability. */
|
|
120
|
-
readonly params?: Readonly<Record<string, Readonly<Record<string, unknown>>>>;
|
|
121
|
-
/**
|
|
122
|
-
* Guarantees the caller requires to be enforced by the platform. One the platform cannot enforce is an
|
|
123
|
-
* error (UNSUPPORTED_REQUIREMENT), never silently accepted.
|
|
124
|
-
*/
|
|
125
|
-
readonly requireEnforced?: readonly string[];
|
|
126
|
-
}
|
|
127
|
-
export type CompositionErrorCode = "UNKNOWN_PROFILE" | "UNKNOWN_CAPABILITY" | "NOT_IN_PROFILE" | "MISSING_DEPENDENCY" | "CONFLICT" | "UNKNOWN_PARAMETER" | "MISSING_PARAMETER" | "INVALID_PARAMETER" | "UNSUPPORTED_OPTION" | "DECLARATION_INVALID" | "UNSUPPORTED_REQUIREMENT";
|
|
128
|
-
export interface CompositionError {
|
|
129
|
-
readonly code: CompositionErrorCode;
|
|
130
|
-
readonly message: string;
|
|
131
|
-
/** What to do about it. */
|
|
132
|
-
readonly hint: string;
|
|
133
|
-
readonly capability?: string;
|
|
134
|
-
readonly parameter?: string;
|
|
135
|
-
/** Declaration path, for CONFLICT and UNSUPPORTED_OPTION. */
|
|
136
|
-
readonly path?: string;
|
|
137
|
-
}
|
|
138
|
-
export interface ResolvedCapability {
|
|
139
|
-
readonly id: CapabilityId;
|
|
140
|
-
readonly version: number;
|
|
141
|
-
readonly params: Readonly<Record<string, unknown>>;
|
|
142
|
-
}
|
|
143
|
-
export interface ResolvedComposition {
|
|
144
|
-
readonly profile: {
|
|
145
|
-
readonly id: string;
|
|
146
|
-
readonly version: number;
|
|
147
|
-
};
|
|
148
|
-
/** In resolution order: dependencies first, then by id. */
|
|
149
|
-
readonly capabilities: readonly ResolvedCapability[];
|
|
150
|
-
/** The foundation declaration, to spread into `feltkit({ name, ...options })` with the application's own roles merged in. */
|
|
151
|
-
readonly options: FoundationOptions;
|
|
152
|
-
/** Which capabilities set each declaration path (several, for a list or a role two capabilities extend). */
|
|
153
|
-
readonly provenance: Readonly<Record<string, readonly CapabilityId[]>>;
|
|
154
|
-
readonly limitations: readonly Limitation[];
|
|
155
|
-
/** Platform-enforced guarantees (ENFORCED_GUARANTEES), by id: what the application need not uphold itself. */
|
|
156
|
-
readonly enforced: readonly string[];
|
|
157
|
-
}
|
|
158
|
-
export type CompositionResult = {
|
|
159
|
-
readonly ok: true;
|
|
160
|
-
readonly value: ResolvedComposition;
|
|
161
|
-
} | {
|
|
162
|
-
readonly ok: false;
|
|
163
|
-
readonly errors: readonly CompositionError[];
|
|
164
|
-
};
|
|
165
|
-
export {};
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Guarantees the platform enforces for every application, whatever the profile, so a caller may require
|
|
3
|
-
* them (`requireEnforced`) and every result records them. Each names where the SDK enforces it.
|
|
4
|
-
*/
|
|
5
|
-
export const ENFORCED_GUARANTEES = Object.freeze([
|
|
6
|
-
{
|
|
7
|
-
id: "authorization.deny-by-default",
|
|
8
|
-
guarantee: 'Every function, event and agent names its access (a permission, "authenticated" or "public"); a declaration without one is refused, and a caller holding no permission is refused on every interface. Being signed in grants nothing.',
|
|
9
|
-
enforcedBy: "packages/sdk/src/app.ts checkAccess (declaration time); packages/sdk/src/runtime/authority.ts Authority.authorize (every call, every transport)",
|
|
10
|
-
},
|
|
11
|
-
]);
|
|
12
|
-
//# sourceMappingURL=contract.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"contract.js","sourceRoot":"","sources":["../../src/composition/contract.ts"],"names":[],"mappings":"AAmFA;;;GAGG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAoG,MAAM,CAAC,MAAM,CAAC;IAChJ;QACE,EAAE,EAAE,+BAA+B;QACnC,SAAS,EAAE,uOAAuO;QAClP,UAAU,EAAE,iJAAiJ;KAC9J;CACF,CAAC,CAAC"}
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import { PLAN_FORMAT, PLAN_RULES, type CompositionPlan, type Operation, type PlanOptions, type PlanRequest } from "./plan.js";
|
|
2
|
-
import { RECONCILIATION_FORMAT, type ReconciliationState } from "./reconcile.js";
|
|
3
|
-
export declare const EXECUTION_FORMAT = "feltkit.composition-execution/1";
|
|
4
|
-
export declare const ENGINE = "feltkit-compose-apply/1";
|
|
5
|
-
export declare const JOURNAL_FORMAT = "feltkit.composition-journal/1";
|
|
6
|
-
/** Where a transaction's journal lives: under the project's own state directory, never among application files. */
|
|
7
|
-
export declare const JOURNAL_DIR = ".feltkit/composition";
|
|
8
|
-
export declare const JOURNAL_PATH = ".feltkit/composition/transaction.json";
|
|
9
|
-
export type ExecutionCode = "APPLIED" | "NO_CHANGE" | "ALREADY_CURRENT" | "PLAN_MISMATCH" | "STALE_STATE" | "APPROVAL_REQUIRED" | "REVIEW_REQUIRED" | "BLOCKED" | "UNSUPPORTED_OPERATION" | "OWNERSHIP_VIOLATION" | "EXECUTION_FAILED" | "POSTCONDITION_FAILED" | "RECOVERY_REQUIRED";
|
|
10
|
-
/** Operation types this slice executes. Everything else is refused before any write. */
|
|
11
|
-
export declare const EXECUTABLE: ReadonlySet<Operation["type"]>;
|
|
12
|
-
export interface FileOutcome {
|
|
13
|
-
readonly before: string | null;
|
|
14
|
-
readonly after: string | null;
|
|
15
|
-
readonly expected: string | null;
|
|
16
|
-
/** Written by this execution. */
|
|
17
|
-
readonly written: boolean;
|
|
18
|
-
}
|
|
19
|
-
export interface Journal {
|
|
20
|
-
readonly format: typeof JOURNAL_FORMAT;
|
|
21
|
-
/** sha256 of the plan id and the prior hashes: one transaction, one identity. */
|
|
22
|
-
readonly transaction: string;
|
|
23
|
-
readonly plan: string;
|
|
24
|
-
readonly engine: string;
|
|
25
|
-
readonly phase: "staged" | "committing" | "committed" | "verified";
|
|
26
|
-
readonly files: Readonly<Record<string, {
|
|
27
|
-
prior: string | null;
|
|
28
|
-
expected: string;
|
|
29
|
-
}>>;
|
|
30
|
-
/** Paths already renamed into place, in commit order. */
|
|
31
|
-
readonly committed: readonly string[];
|
|
32
|
-
/** Why the transaction stopped, when it did. */
|
|
33
|
-
readonly failure?: string;
|
|
34
|
-
}
|
|
35
|
-
export interface ExecutionResult {
|
|
36
|
-
readonly format: typeof EXECUTION_FORMAT;
|
|
37
|
-
readonly engine: typeof ENGINE;
|
|
38
|
-
readonly code: ExecutionCode;
|
|
39
|
-
readonly ok: boolean;
|
|
40
|
-
readonly message: string;
|
|
41
|
-
readonly approved: string | null;
|
|
42
|
-
readonly plan: {
|
|
43
|
-
readonly id: string | null;
|
|
44
|
-
readonly format: typeof PLAN_FORMAT;
|
|
45
|
-
readonly rules: typeof PLAN_RULES;
|
|
46
|
-
readonly status: CompositionPlan["status"] | null;
|
|
47
|
-
};
|
|
48
|
-
readonly current: {
|
|
49
|
-
readonly composition: string | null;
|
|
50
|
-
readonly inputs: string | null;
|
|
51
|
-
};
|
|
52
|
-
readonly target: {
|
|
53
|
-
readonly composition: string | null;
|
|
54
|
-
readonly inputs: string | null;
|
|
55
|
-
};
|
|
56
|
-
readonly files: Readonly<Record<string, FileOutcome>>;
|
|
57
|
-
readonly operations: {
|
|
58
|
-
readonly executed: readonly string[];
|
|
59
|
-
readonly skipped: ReadonlyArray<{
|
|
60
|
-
id: string;
|
|
61
|
-
reason: string;
|
|
62
|
-
}>;
|
|
63
|
-
readonly refused: ReadonlyArray<{
|
|
64
|
-
id: string;
|
|
65
|
-
reason: string;
|
|
66
|
-
}>;
|
|
67
|
-
};
|
|
68
|
-
readonly preconditions: readonly string[];
|
|
69
|
-
readonly postconditions: readonly string[];
|
|
70
|
-
readonly reconciliation: {
|
|
71
|
-
readonly format: typeof RECONCILIATION_FORMAT;
|
|
72
|
-
readonly state: ReconciliationState | null;
|
|
73
|
-
readonly findings: readonly string[];
|
|
74
|
-
};
|
|
75
|
-
readonly transaction: {
|
|
76
|
-
readonly id: string | null;
|
|
77
|
-
readonly journal: string;
|
|
78
|
-
readonly outcome: "none" | "aborted-before-write" | "committed" | "interrupted";
|
|
79
|
-
};
|
|
80
|
-
/** Facts behind the code: hashes, ids, finding codes. */
|
|
81
|
-
readonly evidence: readonly string[];
|
|
82
|
-
}
|
|
83
|
-
export interface ExecuteOptions extends PlanOptions {
|
|
84
|
-
/** Test hooks at the write protocol's boundaries. A throw stops the transaction there. */
|
|
85
|
-
hooks?: {
|
|
86
|
-
/** After the plan is recomputed, before freshness is verified. */
|
|
87
|
-
afterPlan?: () => void;
|
|
88
|
-
/** After the journal exists (phase staged), before any file is staged. */
|
|
89
|
-
afterJournal?: () => void;
|
|
90
|
-
afterStage?: (path: string) => void;
|
|
91
|
-
beforeCommit?: (path: string, committed: readonly string[]) => void;
|
|
92
|
-
afterCommit?: (path: string, committed: readonly string[]) => void;
|
|
93
|
-
/** After the journal says committed, before postconditions are verified. */
|
|
94
|
-
beforeVerify?: () => void;
|
|
95
|
-
/** After the journal says verified, before it is removed. */
|
|
96
|
-
beforeJournalRemoval?: () => void;
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
export interface ExecuteRequest extends PlanRequest {
|
|
100
|
-
/** The exact plan id being approved (`feltkit compose plan` prints it). Absent: APPROVAL_REQUIRED. */
|
|
101
|
-
readonly approve?: string;
|
|
102
|
-
}
|
|
103
|
-
/** A staged temporary file: `<file>.<pid>.<8 hex of the transaction>.tmp`, beside its target. */
|
|
104
|
-
export declare const STAGED_TEMPORARY: RegExp;
|
|
105
|
-
/**
|
|
106
|
-
* Execute the plan for `request` in `projectDir`, if `request.approve` is its exact id and every
|
|
107
|
-
* operation is executable. Writes only the generator-owned files, and only after every precondition
|
|
108
|
-
* holds; verifies everything afterwards.
|
|
109
|
-
*/
|
|
110
|
-
export declare function executePlan(projectDir: string, request: ExecuteRequest, options?: ExecuteOptions): Promise<ExecutionResult>;
|
|
@@ -1,350 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Execution (M5): apply one approved plan, exactly, against exactly the state it was planned on, and
|
|
3
|
-
* prove what happened. The result (`feltkit.composition-execution/1`) is evidence, not an authority:
|
|
4
|
-
* the record and lock stay the composition's authorities (generate.ts), the application stays the
|
|
5
|
-
* authority for its own code (never written here).
|
|
6
|
-
*
|
|
7
|
-
* The approval is the plan's id (plan.ts): the hash of the whole plan, which carries the current
|
|
8
|
-
* state (file hashes, fingerprints, reconciliation, the application sources' hashes) and the target.
|
|
9
|
-
* Execution recomputes the plan from the same request and refuses unless the id is the approved one,
|
|
10
|
-
* so an approval never applies to a plan that differs in any meaningful input. It executes only a
|
|
11
|
-
* narrow, fully deterministic slice (EXECUTABLE), writes only the three generator-owned files, and
|
|
12
|
-
* refuses a plan that needs a decision, is blocked, or contains anything else, before any write.
|
|
13
|
-
*
|
|
14
|
-
* Writes follow a staged protocol: every output computed and hashed in memory, a journal written
|
|
15
|
-
* first (what is about to change, from which hashes to which), each file staged as a temporary file
|
|
16
|
-
* in its own directory and verified, then committed in a fixed order with the journal updated after
|
|
17
|
-
* each rename, then every byte verified, `checkFoundation` run, and `reconcile` required to be
|
|
18
|
-
* CURRENT. Three renames are not one atomic operation, and this code does not claim they are: the
|
|
19
|
-
* journal makes an interruption detectable, and an incomplete journal stops the next execution with
|
|
20
|
-
* RECOVERY_REQUIRED, never a silent cleanup.
|
|
21
|
-
*/
|
|
22
|
-
import { closeSync, existsSync, fsyncSync, lstatSync, mkdirSync, openSync, readFileSync, realpathSync, renameSync, rmSync, unlinkSync, writeFileSync } from "node:fs";
|
|
23
|
-
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
24
|
-
import { DECLARATION_PATH, GENERATED_PATHS, GENERATOR, LOCK_PATH, RECORD_PATH, canonical, checkFoundation, generateFoundation, sha256 } from "./generate.js";
|
|
25
|
-
import { PLAN_FORMAT, PLAN_RULES, applicationSources, planComposition } from "./plan.js";
|
|
26
|
-
import { RECONCILIATION_FORMAT, readGeneratedState, reconcile } from "./reconcile.js";
|
|
27
|
-
export const EXECUTION_FORMAT = "feltkit.composition-execution/1";
|
|
28
|
-
export const ENGINE = "feltkit-compose-apply/1";
|
|
29
|
-
export const JOURNAL_FORMAT = "feltkit.composition-journal/1";
|
|
30
|
-
/** Where a transaction's journal lives: under the project's own state directory, never among application files. */
|
|
31
|
-
export const JOURNAL_DIR = ".feltkit/composition";
|
|
32
|
-
export const JOURNAL_PATH = `${JOURNAL_DIR}/transaction.json`;
|
|
33
|
-
/** Operation types this slice executes. Everything else is refused before any write. */
|
|
34
|
-
export const EXECUTABLE = new Set([
|
|
35
|
-
"resolve-target",
|
|
36
|
-
"validate-target",
|
|
37
|
-
"verify-current-state",
|
|
38
|
-
"change-parameter",
|
|
39
|
-
"add-capability",
|
|
40
|
-
"update-capability",
|
|
41
|
-
"change-declaration-path",
|
|
42
|
-
"regenerate-declaration",
|
|
43
|
-
"update-record",
|
|
44
|
-
"update-lock",
|
|
45
|
-
"revalidate-application",
|
|
46
|
-
]);
|
|
47
|
-
const WRITE_TYPES = new Set(["regenerate-declaration", "update-record", "update-lock"]);
|
|
48
|
-
/** A staged temporary file: `<file>.<pid>.<8 hex of the transaction>.tmp`, beside its target. */
|
|
49
|
-
export const STAGED_TEMPORARY = /\.(\d+)\.([0-9a-f]{8})\.tmp$/;
|
|
50
|
-
const COMMIT_ORDER = [DECLARATION_PATH, RECORD_PATH, LOCK_PATH];
|
|
51
|
-
const hashOf = (dir, path) => (existsSync(join(dir, path)) ? sha256(readFileSync(join(dir, path), "utf8")) : null);
|
|
52
|
-
function readJournal(projectDir) {
|
|
53
|
-
const file = join(projectDir, JOURNAL_PATH);
|
|
54
|
-
if (!existsSync(file))
|
|
55
|
-
return null;
|
|
56
|
-
try {
|
|
57
|
-
const j = JSON.parse(readFileSync(file, "utf8"));
|
|
58
|
-
return j && j.format === JOURNAL_FORMAT ? j : { format: JOURNAL_FORMAT, transaction: "unreadable", plan: "unreadable", engine: "unknown", phase: "committing", files: {}, committed: [], failure: "unreadable journal" };
|
|
59
|
-
}
|
|
60
|
-
catch {
|
|
61
|
-
return { format: JOURNAL_FORMAT, transaction: "unreadable", plan: "unreadable", engine: "unknown", phase: "committing", files: {}, committed: [], failure: "unreadable journal" };
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Write bytes to a temporary file beside `file`, flush them to the device, rename into place, and flush
|
|
66
|
-
* the directory, so the rename is on disk before anything that depends on it. Best effort: platforms
|
|
67
|
-
* that cannot fsync a directory are tolerated; the protocol's ordering of operations does not change.
|
|
68
|
-
*/
|
|
69
|
-
function writeDurably(file, text, temporary) {
|
|
70
|
-
writeFileSync(temporary, text);
|
|
71
|
-
syncFile(temporary);
|
|
72
|
-
renameSync(temporary, file);
|
|
73
|
-
syncDirectory(dirname(file));
|
|
74
|
-
}
|
|
75
|
-
function syncFile(path) {
|
|
76
|
-
const fd = openSync(path, "r+");
|
|
77
|
-
try {
|
|
78
|
-
fsyncSync(fd);
|
|
79
|
-
}
|
|
80
|
-
finally {
|
|
81
|
-
closeSync(fd);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
function syncDirectory(dir) {
|
|
85
|
-
try {
|
|
86
|
-
const fd = openSync(dir, "r");
|
|
87
|
-
try {
|
|
88
|
-
fsyncSync(fd);
|
|
89
|
-
}
|
|
90
|
-
finally {
|
|
91
|
-
closeSync(fd);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
catch {
|
|
95
|
-
// Not every platform lets a directory be opened or synced.
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
function writeJournal(projectDir, journal) {
|
|
99
|
-
const file = join(projectDir, JOURNAL_PATH);
|
|
100
|
-
mkdirSync(dirname(file), { recursive: true });
|
|
101
|
-
writeDurably(file, `${JSON.stringify(journal, null, 2)}\n`, `${file}.${process.pid}.tmp`);
|
|
102
|
-
}
|
|
103
|
-
/** A generator-owned path, inside the project, not a symlink (and no symlinked parent): the only kind execution writes. */
|
|
104
|
-
function ownedPathProblem(projectDir, path) {
|
|
105
|
-
if (!GENERATED_PATHS.includes(path))
|
|
106
|
-
return `${path} is not a generator-owned path (${GENERATED_PATHS.join(", ")}).`;
|
|
107
|
-
if (isAbsolute(path) || path.split(/[\\/]/).includes(".."))
|
|
108
|
-
return `${path} is not a plain relative path inside the project.`;
|
|
109
|
-
const root = realpathSync(projectDir);
|
|
110
|
-
const file = join(projectDir, path);
|
|
111
|
-
// Every existing component from the project root down must be a real directory or file, not a link.
|
|
112
|
-
let at = projectDir;
|
|
113
|
-
for (const part of path.split("/")) {
|
|
114
|
-
at = join(at, part);
|
|
115
|
-
if (!existsSync(at))
|
|
116
|
-
break;
|
|
117
|
-
if (lstatSync(at).isSymbolicLink())
|
|
118
|
-
return `${relative(projectDir, at)} is a symbolic link; execution writes only real files inside the project.`;
|
|
119
|
-
}
|
|
120
|
-
const real = existsSync(file) ? realpathSync(file) : realpathSync(dirname(existsSync(dirname(file)) ? file : join(projectDir, path.split("/")[0])));
|
|
121
|
-
if (real !== root && !real.startsWith(root + sep))
|
|
122
|
-
return `${path} resolves outside the project (${real}).`;
|
|
123
|
-
return null;
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Execute the plan for `request` in `projectDir`, if `request.approve` is its exact id and every
|
|
127
|
-
* operation is executable. Writes only the generator-owned files, and only after every precondition
|
|
128
|
-
* holds; verifies everything afterwards.
|
|
129
|
-
*/
|
|
130
|
-
export async function executePlan(projectDir, request, options = {}) {
|
|
131
|
-
const { approve, ...planRequest } = request;
|
|
132
|
-
const evidence = [];
|
|
133
|
-
const preconditions = [];
|
|
134
|
-
const postconditions = [];
|
|
135
|
-
const filesBefore = Object.fromEntries(GENERATED_PATHS.map((p) => [p, hashOf(projectDir, p)]));
|
|
136
|
-
const outcome = (files, extra, code, message, plan) => ({
|
|
137
|
-
format: EXECUTION_FORMAT,
|
|
138
|
-
engine: ENGINE,
|
|
139
|
-
code,
|
|
140
|
-
ok: code === "APPLIED" || code === "NO_CHANGE" || code === "ALREADY_CURRENT",
|
|
141
|
-
message,
|
|
142
|
-
approved: approve ?? null,
|
|
143
|
-
plan: { id: plan?.id ?? null, format: PLAN_FORMAT, rules: PLAN_RULES, status: plan?.status ?? null },
|
|
144
|
-
current: { composition: plan?.current.composition ?? null, inputs: plan?.current.inputs_hash ?? null },
|
|
145
|
-
target: { composition: plan?.target.composition ?? null, inputs: plan?.target.inputs_hash ?? null },
|
|
146
|
-
files,
|
|
147
|
-
operations: { executed: [], skipped: [], refused: [] },
|
|
148
|
-
preconditions,
|
|
149
|
-
postconditions,
|
|
150
|
-
reconciliation: { format: RECONCILIATION_FORMAT, state: plan?.current.reconciliation.state ?? null, findings: plan?.current.reconciliation.findings ?? [] },
|
|
151
|
-
transaction: { id: null, journal: JOURNAL_PATH, outcome: "none" },
|
|
152
|
-
evidence,
|
|
153
|
-
...extra,
|
|
154
|
-
});
|
|
155
|
-
const unchanged = () => Object.fromEntries(GENERATED_PATHS.map((p) => [p, { before: filesBefore[p], after: hashOf(projectDir, p), expected: null, written: false }]));
|
|
156
|
-
// 0. An interrupted transaction is never stepped over.
|
|
157
|
-
const journal = readJournal(projectDir);
|
|
158
|
-
if (journal) {
|
|
159
|
-
evidence.push(`journal ${JOURNAL_PATH}: phase ${journal.phase}, plan ${journal.plan}, committed ${journal.committed.join(", ") || "nothing"}${journal.failure ? `, failure: ${journal.failure}` : ""}`);
|
|
160
|
-
for (const [path, f] of Object.entries(journal.files))
|
|
161
|
-
evidence.push(`${path}: prior ${f.prior ?? "absent"}, expected ${f.expected}, on disk ${hashOf(projectDir, path) ?? "absent"}`);
|
|
162
|
-
return outcome(unchanged(), { transaction: { id: journal.transaction, journal: JOURNAL_PATH, outcome: "interrupted" } }, "RECOVERY_REQUIRED", journal.phase === "verified"
|
|
163
|
-
? `A previous execution completed and verified, but its journal was not removed (${JOURNAL_PATH}). Run \`feltkit compose recover --inspect\` to confirm the files still verify, then remove the journal by hand. Nothing was changed now.`
|
|
164
|
-
: `A previous execution did not complete (${JOURNAL_PATH}, phase ${journal.phase}). Run \`feltkit compose recover --inspect\`: it compares each file with the journal's prior and expected hashes and says which consistent state can be restored and how. Nothing was changed now.`, null);
|
|
165
|
-
}
|
|
166
|
-
// 1. Recompute the plan from the request: this is what the approval must name.
|
|
167
|
-
const plan = await planComposition(projectDir, planRequest, options);
|
|
168
|
-
evidence.push(`plan ${plan.id} (${plan.status}); reconciliation ${plan.current.reconciliation.state}${plan.current.reconciliation.findings.length ? ` [${plan.current.reconciliation.findings.join(", ")}]` : ""}`);
|
|
169
|
-
if (plan.status === "NO_CHANGE") {
|
|
170
|
-
return outcome(unchanged(), {}, "ALREADY_CURRENT", "The recorded composition already is the target; there is nothing to execute and nothing was written.", plan);
|
|
171
|
-
}
|
|
172
|
-
if (!approve) {
|
|
173
|
-
return outcome(unchanged(), {}, "APPROVAL_REQUIRED", `No plan was approved. Review \`feltkit compose plan …\` for this target and, if it is what you want, run again with --approve ${plan.id}.`, plan);
|
|
174
|
-
}
|
|
175
|
-
if (approve !== plan.id) {
|
|
176
|
-
return outcome(unchanged(), {}, "PLAN_MISMATCH", `The approved plan ${approve} is not the plan for this target against the current state (${plan.id}). The request differs from what was approved, or the application, its generated files, or FeltKit's definitions changed since: reconciliation is ${plan.current.reconciliation.state}. Review the plan again before approving it.`, plan);
|
|
177
|
-
}
|
|
178
|
-
// 2. The plan must be fully executable: no decision, blocker or unsupported change, and every operation in this slice.
|
|
179
|
-
const refused = [];
|
|
180
|
-
for (const o of plan.operations) {
|
|
181
|
-
if (o.classification === "REVIEW_REQUIRED")
|
|
182
|
-
refused.push({ id: o.id, reason: o.decision ?? "needs a decision" });
|
|
183
|
-
else if (o.classification === "BLOCKED")
|
|
184
|
-
refused.push({ id: o.id, reason: o.decision ?? "blocked" });
|
|
185
|
-
else if (o.classification === "UNSUPPORTED")
|
|
186
|
-
refused.push({ id: o.id, reason: o.decision ?? "unsupported target" });
|
|
187
|
-
else if (!EXECUTABLE.has(o.type))
|
|
188
|
-
refused.push({ id: o.id, reason: `${o.type} is not executed by ${ENGINE}: ${o.type === "remove-capability" ? "capability removal is outside this execution slice" : o.type === "adopt-generator" ? "a generator change is regenerated by `feltkit compose`, reviewed by hand" : "outside the executable set"}.` });
|
|
189
|
-
}
|
|
190
|
-
if (refused.length) {
|
|
191
|
-
const code = plan.status === "REVIEW_REQUIRED" ? "REVIEW_REQUIRED" : plan.status === "BLOCKED" ? "BLOCKED" : "UNSUPPORTED_OPERATION";
|
|
192
|
-
return outcome(unchanged(), { operations: { executed: [], skipped: [], refused } }, code, `${refused.length} operation(s) cannot be executed by ${ENGINE}; nothing was written. ${code === "REVIEW_REQUIRED" ? "Decide each one, express the decision in feltkit/app.ts or the composition inputs, and plan again." : ""}`.trim(), plan);
|
|
193
|
-
}
|
|
194
|
-
// 3. The write set: only generator-owned paths, inside the project, no links.
|
|
195
|
-
const writeOps = plan.operations.filter((o) => WRITE_TYPES.has(o.type));
|
|
196
|
-
const writeSet = writeOps.map((o) => o.target);
|
|
197
|
-
for (const path of writeSet) {
|
|
198
|
-
const problem = ownedPathProblem(projectDir, path);
|
|
199
|
-
if (problem) {
|
|
200
|
-
evidence.push(problem);
|
|
201
|
-
return outcome(unchanged(), {}, "OWNERSHIP_VIOLATION", `Refused before any write: ${problem}`, plan);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
const sources = applicationSources(projectDir);
|
|
205
|
-
for (const path of Object.keys(sources))
|
|
206
|
-
if (writeSet.includes(path))
|
|
207
|
-
return outcome(unchanged(), {}, "OWNERSHIP_VIOLATION", `Refused: ${path} is application-owned.`, plan);
|
|
208
|
-
preconditions.push(`write set ${writeSet.join(", ")} ⊆ generator-owned paths, inside the project, no symbolic links`);
|
|
209
|
-
if (!writeSet.length)
|
|
210
|
-
return outcome(unchanged(), {}, "NO_CHANGE", "The plan changes no file.", plan);
|
|
211
|
-
// 4. Freshness, immediately before writing: the state the plan describes is the state on disk now.
|
|
212
|
-
options.hooks?.afterPlan?.();
|
|
213
|
-
const stale = [];
|
|
214
|
-
if (hashOf(projectDir, DECLARATION_PATH) !== plan.current.declaration)
|
|
215
|
-
stale.push(`${DECLARATION_PATH} is ${hashOf(projectDir, DECLARATION_PATH) ?? "absent"}, the plan saw ${plan.current.declaration}`);
|
|
216
|
-
const nowSources = applicationSources(projectDir);
|
|
217
|
-
if (canonical(nowSources) !== canonical(plan.current.sources))
|
|
218
|
-
stale.push(`application sources changed: ${Object.keys({ ...nowSources, ...plan.current.sources }).filter((k) => nowSources[k] !== plan.current.sources[k]).join(", ")}`);
|
|
219
|
-
const state = readGeneratedState(projectDir);
|
|
220
|
-
const check = checkFoundation(state, options.catalog, options.profiles);
|
|
221
|
-
if (!check.ok)
|
|
222
|
-
stale.push(`integrity: ${check.problems.map((p) => p.code).join(", ")}`);
|
|
223
|
-
if (stale.length) {
|
|
224
|
-
evidence.push(...stale);
|
|
225
|
-
return outcome(unchanged(), {}, "STALE_STATE", `The application changed between planning and execution; nothing was written. Plan again.`, plan);
|
|
226
|
-
}
|
|
227
|
-
preconditions.push(`${DECLARATION_PATH} ${plan.current.declaration}`, `application sources ${Object.entries(plan.current.sources).map(([p, h]) => `${p} ${h.slice(0, 19)}…`).join(", ")}`, "`feltkit compose --check` passes on the current files");
|
|
228
|
-
// 5. Outputs, fully in memory, and the target the plan promised.
|
|
229
|
-
const generated = generateFoundation(plan.target.inputs, options.catalog, options.profiles);
|
|
230
|
-
if (!generated.ok)
|
|
231
|
-
return outcome(unchanged(), {}, "EXECUTION_FAILED", `The target no longer generates: ${generated.errors.map((e) => `${e.code} ${e.message}`).join("; ")}`, plan);
|
|
232
|
-
if (generated.lock.files[DECLARATION_PATH].sha256 !== plan.target.declaration)
|
|
233
|
-
return outcome(unchanged(), {}, "EXECUTION_FAILED", `The generated declaration (${generated.lock.files[DECLARATION_PATH].sha256}) is not the plan's target (${plan.target.declaration}).`, plan);
|
|
234
|
-
if (generated.record.composition !== plan.target.composition)
|
|
235
|
-
return outcome(unchanged(), {}, "EXECUTION_FAILED", "The generated composition is not the plan's target.", plan);
|
|
236
|
-
preconditions.push(`target declaration ${plan.target.declaration}`, `target composition ${plan.target.composition}`);
|
|
237
|
-
const expected = Object.fromEntries(GENERATED_PATHS.map((p) => [p, sha256(generated.files[p])]));
|
|
238
|
-
const toWrite = COMMIT_ORDER.filter((p) => expected[p] !== filesBefore[p]);
|
|
239
|
-
for (const path of writeSet)
|
|
240
|
-
if (!toWrite.includes(path) && expected[path] === filesBefore[path])
|
|
241
|
-
evidence.push(`${path}: planned, but its bytes are already the target's; not rewritten`);
|
|
242
|
-
if (!toWrite.length) {
|
|
243
|
-
return outcome(Object.fromEntries(GENERATED_PATHS.map((p) => [p, { before: filesBefore[p], after: filesBefore[p], expected: expected[p], written: false }])), {}, "NO_CHANGE", "Every generated file already holds the target bytes; nothing was written.", plan);
|
|
244
|
-
}
|
|
245
|
-
// 6. The transaction: journal, stage, verify, commit in order, verify.
|
|
246
|
-
const transaction = sha256(canonical({ plan: plan.id, prior: filesBefore }));
|
|
247
|
-
let journalState = { format: JOURNAL_FORMAT, transaction, plan: plan.id, engine: ENGINE, phase: "staged", files: Object.fromEntries(toWrite.map((p) => [p, { prior: filesBefore[p], expected: expected[p] }])), committed: [] };
|
|
248
|
-
const staged = {};
|
|
249
|
-
const executed = [];
|
|
250
|
-
const files = () => Object.fromEntries(GENERATED_PATHS.map((p) => [p, { before: filesBefore[p], after: hashOf(projectDir, p), expected: expected[p], written: journalState.committed.includes(p) }]));
|
|
251
|
-
const fail = (code, message, phase) => {
|
|
252
|
-
journalState = { ...journalState, phase, failure: message };
|
|
253
|
-
try {
|
|
254
|
-
writeJournal(projectDir, journalState);
|
|
255
|
-
}
|
|
256
|
-
catch {
|
|
257
|
-
// The journal itself could not be written: the result still says what happened.
|
|
258
|
-
}
|
|
259
|
-
for (const t of Object.values(staged))
|
|
260
|
-
if (existsSync(t))
|
|
261
|
-
rmSync(t, { force: true });
|
|
262
|
-
return outcome(files(), { operations: { executed, skipped: [], refused: [] }, transaction: { id: transaction, journal: JOURNAL_PATH, outcome: journalState.committed.length ? "interrupted" : "aborted-before-write" } }, code, message, plan);
|
|
263
|
-
};
|
|
264
|
-
try {
|
|
265
|
-
writeJournal(projectDir, journalState);
|
|
266
|
-
options.hooks?.afterJournal?.();
|
|
267
|
-
for (const path of toWrite) {
|
|
268
|
-
const file = join(projectDir, path);
|
|
269
|
-
mkdirSync(dirname(file), { recursive: true });
|
|
270
|
-
const temporary = `${file}.${process.pid}.${transaction.slice(7, 15)}.tmp`;
|
|
271
|
-
writeFileSync(temporary, generated.files[path]);
|
|
272
|
-
syncFile(temporary);
|
|
273
|
-
const observed = sha256(readFileSync(temporary, "utf8"));
|
|
274
|
-
if (observed !== expected[path])
|
|
275
|
-
throw new Error(`staged ${path} hashes to ${observed}, expected ${expected[path]}`);
|
|
276
|
-
staged[path] = temporary;
|
|
277
|
-
options.hooks?.afterStage?.(path);
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
catch (error) {
|
|
281
|
-
return fail("EXECUTION_FAILED", `Staging failed: ${error.message}. No generated file was changed.`, "staged");
|
|
282
|
-
}
|
|
283
|
-
journalState = { ...journalState, phase: "committing" };
|
|
284
|
-
try {
|
|
285
|
-
writeJournal(projectDir, journalState);
|
|
286
|
-
for (const path of toWrite) {
|
|
287
|
-
options.hooks?.beforeCommit?.(path, journalState.committed);
|
|
288
|
-
// The file must still be what the plan saw: nothing else may have touched it meanwhile.
|
|
289
|
-
if (hashOf(projectDir, path) !== filesBefore[path])
|
|
290
|
-
throw new Error(`${path} changed during execution (${hashOf(projectDir, path) ?? "absent"}, expected ${filesBefore[path]})`);
|
|
291
|
-
renameSync(staged[path], join(projectDir, path));
|
|
292
|
-
syncDirectory(dirname(join(projectDir, path)));
|
|
293
|
-
delete staged[path];
|
|
294
|
-
journalState = { ...journalState, committed: [...journalState.committed, path] };
|
|
295
|
-
writeJournal(projectDir, journalState);
|
|
296
|
-
executed.push(...writeOps.filter((o) => o.target === path).map((o) => o.id));
|
|
297
|
-
options.hooks?.afterCommit?.(path, journalState.committed);
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
catch (error) {
|
|
301
|
-
const message = error.message;
|
|
302
|
-
return fail(journalState.committed.length ? "RECOVERY_REQUIRED" : "EXECUTION_FAILED", journalState.committed.length ? `Commit failed after ${journalState.committed.join(", ")} were written: ${message}. The generated files are in a mixed state; ${JOURNAL_PATH} records each file's prior and expected hash. Restore one consistent state (version control holds the prior one) and delete the journal.` : `Commit failed before any file was replaced: ${message}.`, "committing");
|
|
303
|
-
}
|
|
304
|
-
journalState = { ...journalState, phase: "committed" };
|
|
305
|
-
writeJournal(projectDir, journalState);
|
|
306
|
-
options.hooks?.beforeVerify?.();
|
|
307
|
-
executed.push(...plan.operations.filter((o) => !WRITE_TYPES.has(o.type) && o.type !== "revalidate-application").map((o) => o.id));
|
|
308
|
-
// 7. Postconditions: bytes, integrity, reconciliation, and nothing else changed.
|
|
309
|
-
const problems = [];
|
|
310
|
-
for (const path of GENERATED_PATHS) {
|
|
311
|
-
const after = hashOf(projectDir, path);
|
|
312
|
-
if (after !== expected[path])
|
|
313
|
-
problems.push(`${path} is ${after ?? "absent"}, expected ${expected[path]}`);
|
|
314
|
-
else
|
|
315
|
-
postconditions.push(`${path} ${after}`);
|
|
316
|
-
}
|
|
317
|
-
const integrity = checkFoundation(readGeneratedState(projectDir), options.catalog, options.profiles);
|
|
318
|
-
if (!integrity.ok)
|
|
319
|
-
problems.push(`integrity: ${integrity.problems.map((p) => p.code).join(", ")}`);
|
|
320
|
-
else
|
|
321
|
-
postconditions.push("`feltkit compose --check` passes");
|
|
322
|
-
const after = await reconcile(projectDir, options);
|
|
323
|
-
if (after.state !== "CURRENT")
|
|
324
|
-
problems.push(`reconciliation ${after.state}: ${after.findings.map((f) => f.code).join(", ")}`);
|
|
325
|
-
else
|
|
326
|
-
postconditions.push("reconciliation CURRENT");
|
|
327
|
-
const sourcesAfter = applicationSources(projectDir);
|
|
328
|
-
if (canonical(sourcesAfter) !== canonical(plan.current.sources))
|
|
329
|
-
problems.push("application sources changed during execution");
|
|
330
|
-
else
|
|
331
|
-
postconditions.push("application sources byte-identical");
|
|
332
|
-
const extra = Object.keys(sourcesAfter).filter((p) => !(p in plan.current.sources));
|
|
333
|
-
if (extra.length)
|
|
334
|
-
problems.push(`unexpected application files: ${extra.join(", ")}`);
|
|
335
|
-
if (problems.length) {
|
|
336
|
-
evidence.push(...problems);
|
|
337
|
-
journalState = { ...journalState, failure: problems.join("; ") };
|
|
338
|
-
writeJournal(projectDir, journalState);
|
|
339
|
-
return outcome(files(), { operations: { executed, skipped: [], refused: [] }, transaction: { id: transaction, journal: JOURNAL_PATH, outcome: "committed" }, reconciliation: { format: RECONCILIATION_FORMAT, state: after.state, findings: after.findings.map((f) => f.code) } }, "POSTCONDITION_FAILED", `The files were written, but the result is not what the plan promised: ${problems.join("; ")}. ${JOURNAL_PATH} records prior and expected hashes; nothing was undone.`, plan);
|
|
340
|
-
}
|
|
341
|
-
// 8. Done: the journal is complete and removed (its evidence is in this result).
|
|
342
|
-
journalState = { ...journalState, phase: "verified" };
|
|
343
|
-
writeJournal(projectDir, journalState);
|
|
344
|
-
options.hooks?.beforeJournalRemoval?.();
|
|
345
|
-
unlinkSync(join(projectDir, JOURNAL_PATH));
|
|
346
|
-
executed.push(...plan.operations.filter((o) => o.type === "revalidate-application").map((o) => o.id));
|
|
347
|
-
const skipped = writeOps.filter((o) => !toWrite.includes(o.target)).map((o) => ({ id: o.id, reason: "bytes already the target's" }));
|
|
348
|
-
return outcome(files(), { operations: { executed, skipped, refused: [] }, transaction: { id: transaction, journal: JOURNAL_PATH, outcome: "committed" }, reconciliation: { format: RECONCILIATION_FORMAT, state: after.state, findings: [] } }, "APPLIED", `Applied plan ${plan.id}: ${toWrite.join(", ")} written; integrity and reconciliation verified.`, plan);
|
|
349
|
-
}
|
|
350
|
-
//# sourceMappingURL=execute.js.map
|