flowtask-permission-gate 0.0.0-stage → 2.0.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/LICENSE +21 -0
- package/README.md +100 -2
- package/dist/authorizer.d.ts +26 -0
- package/dist/authorizer.js +489 -0
- package/dist/code-mode.d.ts +9 -0
- package/dist/code-mode.js +163 -0
- package/dist/context-budget.d.ts +138 -0
- package/dist/context-budget.js +369 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +641 -0
- package/dist/layers.d.ts +104 -0
- package/dist/layers.js +185 -0
- package/dist/registration.d.ts +44 -0
- package/dist/registration.js +135 -0
- package/dist/worktree-manager-location.d.ts +17 -0
- package/dist/worktree-manager-location.js +30 -0
- package/package.json +41 -4
package/dist/layers.d.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {{ ok: true, value: { version: number, profile: string } }} AutonomyParse
|
|
3
|
+
* @typedef {{ ok: true, value: { version: number, envelope: string } }} PermissionsParse
|
|
4
|
+
* @typedef {{ ok: false, value: null, reason: string }} LayerParseFailure
|
|
5
|
+
* @typedef {AutonomyParse | PermissionsParse | LayerParseFailure} LayerParseResult
|
|
6
|
+
* @typedef {{ ok: true, layers: { autonomy: { version: number, profile: string }, permissions: { version: number, envelope: string } } }} OperationalLayersOk
|
|
7
|
+
* @typedef {{ ok: false, layer: string, reason: string, path: string | null, configDir: string | null }} OperationalLayersError
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Validates the content of the autonomy layer.
|
|
11
|
+
*
|
|
12
|
+
* @param {string} raw
|
|
13
|
+
* @returns {LayerParseResult}
|
|
14
|
+
*/
|
|
15
|
+
export function parseAutonomyLayer(raw: string): LayerParseResult;
|
|
16
|
+
/**
|
|
17
|
+
* Validates the content of the permissions layer.
|
|
18
|
+
*
|
|
19
|
+
* @param {string} raw
|
|
20
|
+
* @returns {LayerParseResult}
|
|
21
|
+
*/
|
|
22
|
+
export function parsePermissionsLayer(raw: string): LayerParseResult;
|
|
23
|
+
/**
|
|
24
|
+
* Resolves the operational layers stored in one explicit configuration
|
|
25
|
+
* directory. Both files must exist and be valid; a missing layer is reported
|
|
26
|
+
* as a failure for that directory.
|
|
27
|
+
*
|
|
28
|
+
* @param {string} configDir
|
|
29
|
+
* @returns {OperationalLayersOk | OperationalLayersError}
|
|
30
|
+
*/
|
|
31
|
+
export function readOperationalLayersAt(configDir: string): OperationalLayersOk | OperationalLayersError;
|
|
32
|
+
/**
|
|
33
|
+
* Resolves the operational layers of a target.
|
|
34
|
+
*
|
|
35
|
+
* The first candidate directory that contains `autonomy.json` or
|
|
36
|
+
* `permissions.json` is the source: both files must exist and be valid there.
|
|
37
|
+
* When no candidate contains a layer, the target declares no layer
|
|
38
|
+
* configuration: the result is a failure with `configDir: null` and the gate
|
|
39
|
+
* denies with the permissions-layer message.
|
|
40
|
+
*
|
|
41
|
+
* @param {string[]} directories
|
|
42
|
+
* @returns {OperationalLayersOk | OperationalLayersError}
|
|
43
|
+
*/
|
|
44
|
+
export function readOperationalLayers(directories: string[]): OperationalLayersOk | OperationalLayersError;
|
|
45
|
+
/**
|
|
46
|
+
* Operational message for a missing or invalid layer.
|
|
47
|
+
*
|
|
48
|
+
* @param {{ layer?: string, reason?: string, path?: string | null }} error
|
|
49
|
+
* @returns {string}
|
|
50
|
+
*/
|
|
51
|
+
export function buildLayerBlockMessage(error: {
|
|
52
|
+
layer?: string;
|
|
53
|
+
reason?: string;
|
|
54
|
+
path?: string | null;
|
|
55
|
+
}): string;
|
|
56
|
+
/**
|
|
57
|
+
* Parser of the stored layers that the gate validates before applying the
|
|
58
|
+
* runner's technical allowlist. It does not authorize tools: it only checks
|
|
59
|
+
* that the autonomy and permissions layers exist and are valid in the target.
|
|
60
|
+
*/
|
|
61
|
+
export const AUTONOMY_PROFILES: string[];
|
|
62
|
+
export const PERMISSION_ENVELOPES: string[];
|
|
63
|
+
/** Candidate configuration directories, tried in order within each root. */
|
|
64
|
+
export const LAYER_DIR_CANDIDATES: string[];
|
|
65
|
+
export type AutonomyParse = {
|
|
66
|
+
ok: true;
|
|
67
|
+
value: {
|
|
68
|
+
version: number;
|
|
69
|
+
profile: string;
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
export type PermissionsParse = {
|
|
73
|
+
ok: true;
|
|
74
|
+
value: {
|
|
75
|
+
version: number;
|
|
76
|
+
envelope: string;
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
export type LayerParseFailure = {
|
|
80
|
+
ok: false;
|
|
81
|
+
value: null;
|
|
82
|
+
reason: string;
|
|
83
|
+
};
|
|
84
|
+
export type LayerParseResult = AutonomyParse | PermissionsParse | LayerParseFailure;
|
|
85
|
+
export type OperationalLayersOk = {
|
|
86
|
+
ok: true;
|
|
87
|
+
layers: {
|
|
88
|
+
autonomy: {
|
|
89
|
+
version: number;
|
|
90
|
+
profile: string;
|
|
91
|
+
};
|
|
92
|
+
permissions: {
|
|
93
|
+
version: number;
|
|
94
|
+
envelope: string;
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
export type OperationalLayersError = {
|
|
99
|
+
ok: false;
|
|
100
|
+
layer: string;
|
|
101
|
+
reason: string;
|
|
102
|
+
path: string | null;
|
|
103
|
+
configDir: string | null;
|
|
104
|
+
};
|
package/dist/layers.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Parser of the stored layers that the gate validates before applying the
|
|
6
|
+
* runner's technical allowlist. It does not authorize tools: it only checks
|
|
7
|
+
* that the autonomy and permissions layers exist and are valid in the target.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export const AUTONOMY_PROFILES = ["assistant", "autonomous-dev"];
|
|
11
|
+
|
|
12
|
+
export const PERMISSION_ENVELOPES = ["standard"];
|
|
13
|
+
|
|
14
|
+
/** Candidate configuration directories, tried in order within each root. */
|
|
15
|
+
export const LAYER_DIR_CANDIDATES = [
|
|
16
|
+
".opencode/flowtask/config",
|
|
17
|
+
".claude/flowtask/config",
|
|
18
|
+
".vscode/flowtask/config",
|
|
19
|
+
".flowtask/config",
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
const AUTONOMY_FILE = "autonomy.json";
|
|
23
|
+
const PERMISSIONS_FILE = "permissions.json";
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @typedef {{ ok: true, value: { version: number, profile: string } }} AutonomyParse
|
|
27
|
+
* @typedef {{ ok: true, value: { version: number, envelope: string } }} PermissionsParse
|
|
28
|
+
* @typedef {{ ok: false, value: null, reason: string }} LayerParseFailure
|
|
29
|
+
* @typedef {AutonomyParse | PermissionsParse | LayerParseFailure} LayerParseResult
|
|
30
|
+
* @typedef {{ ok: true, layers: { autonomy: { version: number, profile: string }, permissions: { version: number, envelope: string } } }} OperationalLayersOk
|
|
31
|
+
* @typedef {{ ok: false, layer: string, reason: string, path: string | null, configDir: string | null }} OperationalLayersError
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Validates the content of the autonomy layer.
|
|
36
|
+
*
|
|
37
|
+
* @param {string} raw
|
|
38
|
+
* @returns {LayerParseResult}
|
|
39
|
+
*/
|
|
40
|
+
export function parseAutonomyLayer(raw) {
|
|
41
|
+
const parsed = parseJsonObject(raw);
|
|
42
|
+
if (!parsed.ok) return parsed;
|
|
43
|
+
const value = parsed.value;
|
|
44
|
+
if (value.version !== 1) {
|
|
45
|
+
return { ok: false, value: null, reason: `invalid autonomy version: ${describe(value.version)}` };
|
|
46
|
+
}
|
|
47
|
+
if (!AUTONOMY_PROFILES.includes(value.profile)) {
|
|
48
|
+
return { ok: false, value: null, reason: `unknown autonomy profile: ${describe(value.profile)}` };
|
|
49
|
+
}
|
|
50
|
+
return { ok: true, value: { version: 1, profile: value.profile }, reason: null };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Validates the content of the permissions layer.
|
|
55
|
+
*
|
|
56
|
+
* @param {string} raw
|
|
57
|
+
* @returns {LayerParseResult}
|
|
58
|
+
*/
|
|
59
|
+
export function parsePermissionsLayer(raw) {
|
|
60
|
+
const parsed = parseJsonObject(raw);
|
|
61
|
+
if (!parsed.ok) return parsed;
|
|
62
|
+
const value = parsed.value;
|
|
63
|
+
if (value.version !== 1) {
|
|
64
|
+
return { ok: false, value: null, reason: `invalid permissions version: ${describe(value.version)}` };
|
|
65
|
+
}
|
|
66
|
+
if (!PERMISSION_ENVELOPES.includes(value.envelope)) {
|
|
67
|
+
return { ok: false, value: null, reason: `unknown envelope: ${describe(value.envelope)}` };
|
|
68
|
+
}
|
|
69
|
+
return { ok: true, value: { version: 1, envelope: value.envelope }, reason: null };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function describe(value) {
|
|
73
|
+
if (typeof value === "string") return JSON.stringify(value);
|
|
74
|
+
if (value === null) return "null";
|
|
75
|
+
if (value === undefined) return "undefined";
|
|
76
|
+
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
77
|
+
return typeof value;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function parseJsonObject(raw) {
|
|
81
|
+
let value;
|
|
82
|
+
try {
|
|
83
|
+
value = JSON.parse(raw);
|
|
84
|
+
} catch {
|
|
85
|
+
return { ok: false, value: null, reason: "invalid JSON: the file is not parseable" };
|
|
86
|
+
}
|
|
87
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
88
|
+
return { ok: false, value: null, reason: "the file does not contain a JSON object" };
|
|
89
|
+
}
|
|
90
|
+
return { ok: true, value, reason: null };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function readLayerFile(filePath, parse) {
|
|
94
|
+
let raw;
|
|
95
|
+
try {
|
|
96
|
+
raw = readFileSync(filePath, "utf8");
|
|
97
|
+
} catch (error) {
|
|
98
|
+
if (error && error.code === "ENOENT") {
|
|
99
|
+
return { ok: false, value: null, reason: "missing layer: the layer file was not found" };
|
|
100
|
+
}
|
|
101
|
+
return { ok: false, value: null, reason: "the layer file could not be read" };
|
|
102
|
+
}
|
|
103
|
+
return parse(raw);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Resolves the operational layers stored in one explicit configuration
|
|
108
|
+
* directory. Both files must exist and be valid; a missing layer is reported
|
|
109
|
+
* as a failure for that directory.
|
|
110
|
+
*
|
|
111
|
+
* @param {string} configDir
|
|
112
|
+
* @returns {OperationalLayersOk | OperationalLayersError}
|
|
113
|
+
*/
|
|
114
|
+
export function readOperationalLayersAt(configDir) {
|
|
115
|
+
const autonomyPath = join(configDir, AUTONOMY_FILE);
|
|
116
|
+
const permissionsPath = join(configDir, PERMISSIONS_FILE);
|
|
117
|
+
|
|
118
|
+
const autonomy = readLayerFile(autonomyPath, parseAutonomyLayer);
|
|
119
|
+
if (!autonomy.ok) {
|
|
120
|
+
return { ok: false, layer: "autonomy", reason: autonomy.reason, path: autonomyPath, configDir };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const permissions = readLayerFile(permissionsPath, parsePermissionsLayer);
|
|
124
|
+
if (!permissions.ok) {
|
|
125
|
+
return { ok: false, layer: "permissions", reason: permissions.reason, path: permissionsPath, configDir };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
ok: true,
|
|
130
|
+
layers: { autonomy: autonomy.value, permissions: permissions.value },
|
|
131
|
+
configDir,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Resolves the operational layers of a target.
|
|
137
|
+
*
|
|
138
|
+
* The first candidate directory that contains `autonomy.json` or
|
|
139
|
+
* `permissions.json` is the source: both files must exist and be valid there.
|
|
140
|
+
* When no candidate contains a layer, the target declares no layer
|
|
141
|
+
* configuration: the result is a failure with `configDir: null` and the gate
|
|
142
|
+
* denies with the permissions-layer message.
|
|
143
|
+
*
|
|
144
|
+
* @param {string[]} directories
|
|
145
|
+
* @returns {OperationalLayersOk | OperationalLayersError}
|
|
146
|
+
*/
|
|
147
|
+
export function readOperationalLayers(directories) {
|
|
148
|
+
const roots = Array.isArray(directories) ? directories : [directories];
|
|
149
|
+
|
|
150
|
+
for (const root of roots) {
|
|
151
|
+
if (!root) continue;
|
|
152
|
+
for (const candidate of LAYER_DIR_CANDIDATES) {
|
|
153
|
+
const configDir = resolve(root, candidate);
|
|
154
|
+
const autonomyPath = join(configDir, AUTONOMY_FILE);
|
|
155
|
+
const permissionsPath = join(configDir, PERMISSIONS_FILE);
|
|
156
|
+
if (!existsSync(autonomyPath) && !existsSync(permissionsPath)) continue;
|
|
157
|
+
return readOperationalLayersAt(configDir);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const firstRoot = roots.find(Boolean) || process.cwd();
|
|
162
|
+
return {
|
|
163
|
+
ok: false,
|
|
164
|
+
layer: "permissions",
|
|
165
|
+
reason: "missing permissions layer: the target declares no layer configuration",
|
|
166
|
+
path: join(resolve(firstRoot, LAYER_DIR_CANDIDATES[0]), PERMISSIONS_FILE),
|
|
167
|
+
configDir: null,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Operational message for a missing or invalid layer.
|
|
173
|
+
*
|
|
174
|
+
* @param {{ layer?: string, reason?: string, path?: string | null }} error
|
|
175
|
+
* @returns {string}
|
|
176
|
+
*/
|
|
177
|
+
export function buildLayerBlockMessage(error) {
|
|
178
|
+
const layerLabel = error && error.layer === "autonomy" ? "autonomy" : "permissions";
|
|
179
|
+
return [
|
|
180
|
+
`[FlowTask Permission Gate] Missing or invalid ${layerLabel} layer.`,
|
|
181
|
+
`File: ${error && error.path ? error.path : "unidentified"}`,
|
|
182
|
+
`Cause: ${error && error.reason ? error.reason : "invalid layer"}`,
|
|
183
|
+
"Action: fix the layer or run flowtask update; the Runner does not execute tools with expanded limits.",
|
|
184
|
+
].join("\n");
|
|
185
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export type RegistrationState = "absent" | "active" | "inactive" | "unsupported" | "unreadable";
|
|
2
|
+
export type RuntimeState = "active" | "missing" | "incompatible" | "unreadable" | "incomplete";
|
|
3
|
+
export type ActivationStatus = "disabled" | "inactive" | "active" | "unsupported" | "unreadable" | "broken";
|
|
4
|
+
export interface RegistrationResult {
|
|
5
|
+
status: RegistrationState;
|
|
6
|
+
registry: Record<string, unknown> | null;
|
|
7
|
+
path: string | null;
|
|
8
|
+
projectDir: string | null;
|
|
9
|
+
reason: string | null;
|
|
10
|
+
}
|
|
11
|
+
export interface RuntimeResult {
|
|
12
|
+
status: RuntimeState;
|
|
13
|
+
manifest: Record<string, unknown> | null;
|
|
14
|
+
payloadDir: string | null;
|
|
15
|
+
reason: string | null;
|
|
16
|
+
}
|
|
17
|
+
export interface ActivationResult {
|
|
18
|
+
status: ActivationStatus;
|
|
19
|
+
message: string | null;
|
|
20
|
+
registration: RegistrationResult;
|
|
21
|
+
runtime: RuntimeResult;
|
|
22
|
+
runtimeRoot: string;
|
|
23
|
+
}
|
|
24
|
+
export declare const ACTIVATION_MESSAGES: {
|
|
25
|
+
readonly unreadable: "FlowTask project registration is unreadable. Run: flowtask install.";
|
|
26
|
+
readonly unsupported: "FlowTask project registration uses an unsupported schema. Run: flowtask update.";
|
|
27
|
+
readonly broken: "FlowTask runtime is missing or incompatible. Run: flowtask install.";
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Find the closest OpenCode project registration walking up from `startDir`.
|
|
31
|
+
* The registration must declare schema 1 and the real project path.
|
|
32
|
+
*/
|
|
33
|
+
export declare function findOpencodeRegistration(startDir: string): RegistrationResult;
|
|
34
|
+
/** Read the canonical runtime manifest and validate its payload is usable. */
|
|
35
|
+
export declare function readCanonicalRuntime(runtimeRoot: string): RuntimeResult;
|
|
36
|
+
/**
|
|
37
|
+
* Decide whether the FlowTask surface must be active for this directory.
|
|
38
|
+
* Without a runtime option the gate keeps its legacy behavior. A registered
|
|
39
|
+
* project activates only when the canonical runtime and payload are usable.
|
|
40
|
+
*/
|
|
41
|
+
export declare function evaluateOpencodeActivation({ directory, runtimeRoot, }: {
|
|
42
|
+
directory: string;
|
|
43
|
+
runtimeRoot: string | null;
|
|
44
|
+
}): ActivationResult;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
export const ACTIVATION_MESSAGES = {
|
|
4
|
+
unreadable: "FlowTask project registration is unreadable. Run: flowtask install.",
|
|
5
|
+
unsupported: "FlowTask project registration uses an unsupported schema. Run: flowtask update.",
|
|
6
|
+
broken: "FlowTask runtime is missing or incompatible. Run: flowtask install.",
|
|
7
|
+
};
|
|
8
|
+
function realDirectory(value) {
|
|
9
|
+
try {
|
|
10
|
+
return realpathSync(value);
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function comparable(value) {
|
|
17
|
+
return process.platform === "win32" ? value.toLowerCase() : value;
|
|
18
|
+
}
|
|
19
|
+
function readJson(filePath) {
|
|
20
|
+
return JSON.parse(readFileSync(filePath, "utf8"));
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Find the closest OpenCode project registration walking up from `startDir`.
|
|
24
|
+
* The registration must declare schema 1 and the real project path.
|
|
25
|
+
*/
|
|
26
|
+
export function findOpencodeRegistration(startDir) {
|
|
27
|
+
let current = realDirectory(startDir);
|
|
28
|
+
for (;;) {
|
|
29
|
+
const candidate = join(current, ".opencode", "flowtask", "project.json");
|
|
30
|
+
if (existsSync(candidate)) {
|
|
31
|
+
let registry;
|
|
32
|
+
try {
|
|
33
|
+
const parsed = readJson(candidate);
|
|
34
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
35
|
+
return { status: "unreadable", registry: null, path: candidate, projectDir: current, reason: "the registration is not a JSON object" };
|
|
36
|
+
}
|
|
37
|
+
registry = parsed;
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
return {
|
|
41
|
+
status: "unreadable",
|
|
42
|
+
registry: null,
|
|
43
|
+
path: candidate,
|
|
44
|
+
projectDir: current,
|
|
45
|
+
reason: error instanceof Error ? error.message : "the registration is not valid JSON",
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
if (registry.schema !== 1) {
|
|
49
|
+
return { status: "unsupported", registry, path: candidate, projectDir: current, reason: `schema ${String(registry.schema ?? "missing")}` };
|
|
50
|
+
}
|
|
51
|
+
if (typeof registry.projectPath !== "string" || !registry.projectPath) {
|
|
52
|
+
return { status: "unreadable", registry, path: candidate, projectDir: current, reason: "the registration does not declare a project path" };
|
|
53
|
+
}
|
|
54
|
+
if (comparable(registry.projectPath) !== comparable(current)) {
|
|
55
|
+
return { status: "inactive", registry, path: candidate, projectDir: current, reason: "the registration points to another project" };
|
|
56
|
+
}
|
|
57
|
+
return { status: "active", registry, path: candidate, projectDir: current, reason: null };
|
|
58
|
+
}
|
|
59
|
+
const parent = dirname(current);
|
|
60
|
+
if (parent === current) {
|
|
61
|
+
return { status: "absent", registry: null, path: null, projectDir: null, reason: null };
|
|
62
|
+
}
|
|
63
|
+
current = parent;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/** Read the canonical runtime manifest and validate its payload is usable. */
|
|
67
|
+
export function readCanonicalRuntime(runtimeRoot) {
|
|
68
|
+
const manifestPath = join(runtimeRoot, "runtime.json");
|
|
69
|
+
if (!existsSync(manifestPath)) {
|
|
70
|
+
return { status: "missing", manifest: null, payloadDir: null, reason: `runtime manifest not found at ${manifestPath}` };
|
|
71
|
+
}
|
|
72
|
+
let manifest;
|
|
73
|
+
try {
|
|
74
|
+
const parsed = readJson(manifestPath);
|
|
75
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
76
|
+
return { status: "unreadable", manifest: null, payloadDir: null, reason: "the runtime manifest is not a JSON object" };
|
|
77
|
+
}
|
|
78
|
+
manifest = parsed;
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
return {
|
|
82
|
+
status: "unreadable",
|
|
83
|
+
manifest: null,
|
|
84
|
+
payloadDir: null,
|
|
85
|
+
reason: error instanceof Error ? error.message : "the runtime manifest is not valid JSON",
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
if (manifest.schema !== 2) {
|
|
89
|
+
return { status: "incompatible", manifest, payloadDir: null, reason: `unsupported runtime schema ${String(manifest.schema ?? "missing")}` };
|
|
90
|
+
}
|
|
91
|
+
const payloads = manifest.payloads;
|
|
92
|
+
const canonical = payloads?.canonical;
|
|
93
|
+
if (!canonical || typeof canonical.payloadId !== "string" || !canonical.payloadId || !Array.isArray(canonical.files)) {
|
|
94
|
+
return { status: "incomplete", manifest, payloadDir: null, reason: "the canonical payload is not declared" };
|
|
95
|
+
}
|
|
96
|
+
const payloadDir = join(runtimeRoot, "versions", canonical.payloadId);
|
|
97
|
+
if (!existsSync(payloadDir)
|
|
98
|
+
|| !existsSync(join(payloadDir, "runner.md"))
|
|
99
|
+
|| !existsSync(join(payloadDir, "skills"))) {
|
|
100
|
+
return { status: "incomplete", manifest, payloadDir, reason: `the canonical payload is incomplete at ${payloadDir}` };
|
|
101
|
+
}
|
|
102
|
+
return { status: "active", manifest, payloadDir, reason: null };
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Decide whether the FlowTask surface must be active for this directory.
|
|
106
|
+
* Without a runtime option the gate keeps its legacy behavior. A registered
|
|
107
|
+
* project activates only when the canonical runtime and payload are usable.
|
|
108
|
+
*/
|
|
109
|
+
export function evaluateOpencodeActivation({ directory, runtimeRoot, }) {
|
|
110
|
+
const runtime = { status: "missing", manifest: null, payloadDir: null, reason: null };
|
|
111
|
+
if (!runtimeRoot) {
|
|
112
|
+
return {
|
|
113
|
+
status: "disabled",
|
|
114
|
+
message: null,
|
|
115
|
+
registration: { status: "absent", registry: null, path: null, projectDir: null, reason: null },
|
|
116
|
+
runtime,
|
|
117
|
+
runtimeRoot: "",
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
const registration = findOpencodeRegistration(directory);
|
|
121
|
+
if (registration.status === "absent" || registration.status === "inactive") {
|
|
122
|
+
return { status: "inactive", message: null, registration, runtime, runtimeRoot };
|
|
123
|
+
}
|
|
124
|
+
if (registration.status === "unreadable") {
|
|
125
|
+
return { status: "unreadable", message: ACTIVATION_MESSAGES.unreadable, registration, runtime, runtimeRoot };
|
|
126
|
+
}
|
|
127
|
+
if (registration.status === "unsupported") {
|
|
128
|
+
return { status: "unsupported", message: ACTIVATION_MESSAGES.unsupported, registration, runtime, runtimeRoot };
|
|
129
|
+
}
|
|
130
|
+
const canonicalRuntime = readCanonicalRuntime(runtimeRoot);
|
|
131
|
+
if (canonicalRuntime.status !== "active") {
|
|
132
|
+
return { status: "broken", message: ACTIVATION_MESSAGES.broken, registration, runtime: canonicalRuntime, runtimeRoot };
|
|
133
|
+
}
|
|
134
|
+
return { status: "active", message: null, registration, runtime: canonicalRuntime, runtimeRoot };
|
|
135
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type WorktreeManagerSource = "global";
|
|
2
|
+
export declare function globalWorktreeManagerDirectory(options?: {
|
|
3
|
+
env?: Record<string, string | undefined>;
|
|
4
|
+
home?: string;
|
|
5
|
+
}): string;
|
|
6
|
+
export declare function isValidManagerCheckout(dir: string): boolean;
|
|
7
|
+
export declare function resolveWorktreeManagerLocation(options?: {
|
|
8
|
+
env?: Record<string, string | undefined>;
|
|
9
|
+
home?: string;
|
|
10
|
+
}): {
|
|
11
|
+
directory: string;
|
|
12
|
+
source: WorktreeManagerSource;
|
|
13
|
+
} | null;
|
|
14
|
+
export declare function missingManagerCause(options?: {
|
|
15
|
+
env?: Record<string, string | undefined>;
|
|
16
|
+
home?: string;
|
|
17
|
+
}): string;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
export function globalWorktreeManagerDirectory(options) {
|
|
4
|
+
const env = options?.env ?? process.env;
|
|
5
|
+
const home = options?.home ?? (process.env.HOME || process.env.USERPROFILE || "");
|
|
6
|
+
if (process.platform === "win32") {
|
|
7
|
+
const appdata = env.APPDATA || (home ? `${home}/AppData/Roaming` : "");
|
|
8
|
+
return resolve(appdata || "", "FlowTask", "worktree-manager");
|
|
9
|
+
}
|
|
10
|
+
const xdg = env.XDG_CONFIG_HOME;
|
|
11
|
+
const base = xdg ? resolve(xdg) : resolve(home, ".config");
|
|
12
|
+
return resolve(base, "flowtask", "worktree-manager");
|
|
13
|
+
}
|
|
14
|
+
export function isValidManagerCheckout(dir) {
|
|
15
|
+
try {
|
|
16
|
+
return (existsSync(resolve(dir, "worktree_manager", "__main__.py")) &&
|
|
17
|
+
existsSync(resolve(dir, "worktree_manager", "mcp_transport.py")));
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export function resolveWorktreeManagerLocation(options = {}) {
|
|
24
|
+
const directory = globalWorktreeManagerDirectory({ env: options.env, home: options.home });
|
|
25
|
+
return isValidManagerCheckout(directory) ? { directory, source: "global" } : null;
|
|
26
|
+
}
|
|
27
|
+
export function missingManagerCause(options = {}) {
|
|
28
|
+
const globalDir = globalWorktreeManagerDirectory({ env: options.env, home: options.home });
|
|
29
|
+
return `Worktree Manager is not installed at ${globalDir}. Run 'flowtask install' to install the global dependency.`;
|
|
30
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "flowtask-permission-gate",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "2.0.1",
|
|
4
|
+
"description": "OpenCode permission gate for FlowTask: runner allowlist, review stamps, and context budget.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"exports": "./dist/index.js",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"README.md",
|
|
11
|
+
"LICENSE"
|
|
12
|
+
],
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/Saez520/flowtask.git",
|
|
16
|
+
"directory": ".flowtask/plugins/flowtask-permission-gate"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://github.com/Saez520/flowtask#readme",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/Saez520/flowtask/issues"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"keywords": [
|
|
24
|
+
"opencode",
|
|
25
|
+
"opencode-plugin",
|
|
26
|
+
"flowtask",
|
|
27
|
+
"permission-gate",
|
|
28
|
+
"review-stamp",
|
|
29
|
+
"context-budget"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsc -p tsconfig.json && node -e \"require('node:fs').copyFileSync('src/layers.js','dist/layers.js')\"",
|
|
33
|
+
"test": "node --test test/index.test.mjs && node test/context-budget-smoke.mjs",
|
|
34
|
+
"prepack": "npm run build"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@opencode/plugin": "2.0.29"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/node": "^26.1.1",
|
|
41
|
+
"typescript": "^5.9.3"
|
|
42
|
+
}
|
|
43
|
+
}
|