@agentic-kit/harness 0.11.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/adapter.d.ts +33 -0
- package/adapter.js +17 -0
- package/esm/adapter.d.ts +33 -0
- package/esm/adapter.js +16 -0
- package/esm/gating/approvals.d.ts +47 -0
- package/esm/gating/approvals.js +38 -0
- package/esm/gating/confirm-gate.d.ts +21 -32
- package/esm/gating/confirm-gate.js +15 -40
- package/esm/gating/constructive-policy.d.ts +35 -0
- package/esm/gating/constructive-policy.js +45 -0
- package/esm/gating/policy.d.ts +36 -0
- package/esm/gating/policy.js +1 -0
- package/esm/gating/preview.d.ts +6 -0
- package/esm/gating/prompts.d.ts +1 -6
- package/esm/gating/run-gate.d.ts +57 -0
- package/esm/gating/run-gate.js +72 -0
- package/esm/gating/run-policy.d.ts +47 -0
- package/esm/gating/run-policy.js +43 -0
- package/esm/index.d.ts +6 -0
- package/esm/index.js +6 -0
- package/gating/approvals.d.ts +47 -0
- package/gating/approvals.js +42 -0
- package/gating/confirm-gate.d.ts +21 -32
- package/gating/confirm-gate.js +15 -40
- package/gating/constructive-policy.d.ts +35 -0
- package/gating/constructive-policy.js +48 -0
- package/gating/policy.d.ts +36 -0
- package/gating/policy.js +2 -0
- package/gating/preview.d.ts +6 -0
- package/gating/prompts.d.ts +1 -6
- package/gating/run-gate.d.ts +57 -0
- package/gating/run-gate.js +77 -0
- package/gating/run-policy.d.ts +47 -0
- package/gating/run-policy.js +48 -0
- package/index.d.ts +6 -0
- package/index.js +6 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -25,6 +25,8 @@ Code only: **no skills are bundled**. Skills are pulled at runtime from [`constr
|
|
|
25
25
|
- **Skill releases** — fetch a pinned release (npm version/range/dist-tag, or a git tag/semver/SHA/branch straight from GitHub), verified and cached under `~/.constructive/data/skills/<version>/`; offline falls back to the newest local release.
|
|
26
26
|
- **Ordered overlays** — layer skill sources (hardened base → team overlays → private known-gaps), last write wins by skill name, with include/exclude filters and transitive `requires:` expansion.
|
|
27
27
|
- **Confirm gating** — a host-neutral gate for mutating db tools: per-run decline memory (equivalent-args retries are auto-blocked), short-circuits for runnable projects, and structured previews your UI renders.
|
|
28
|
+
- **Run gate** — a policy (`allow` / `deny` / `ask`) plus an approval rendezvous, so a cloud run's `rm -rf` can be answered from a browser tab. The gate settles the decision and records it; an adapter only maps its harness's tool-call event onto it.
|
|
29
|
+
- **Harness adapter contract** — `HarnessAdapter` / `HarnessRun`: a harness is identified by an `id`, the `transcriptFormat` its entries are recorded under, and `startRun`. Nothing in the platform names a vendor.
|
|
28
30
|
- **Blueprint logic** — `expandBlueprintDefaults()`, the zod `BlueprintZod`/JSON-Schema `BlueprintSchema`, field type/default parsing, and policy-provisioning tables.
|
|
29
31
|
- **Appstash state** — everything lives in the [`appstash`](https://www.npmjs.com/package/appstash) `~/.constructive/{config,cache,data,logs}` layout; project directories are untouched.
|
|
30
32
|
|
|
@@ -71,6 +73,8 @@ materializeSkills(dirs.materializedDir, skills, {
|
|
|
71
73
|
- **Freshness** — `checkForSkillsUpdate()` / `checkForSkillsUpdateFromGit()`: TTL-cached update checks that never throw, respect `compatibleSkillsRange`, and flag `harnessUpgradeRequired` when newer skills need newer tool code.
|
|
72
74
|
- **Resolution** — `SkillsManifest`, `DirectorySkillSource` (agentskills.io layout: `<skill>/SKILL.md` + references/scripts), `resolveSkills()`, `materializeSkills()` with `{{VAR}}` substitution.
|
|
73
75
|
- **Gating** — `createConfirmGate()`, `buildConfirmPrompt()`, `MUTATING_DB_TOOLS`, `ConfirmPreview`; adapters map host events onto `GateToolCallEvent`/`GateResult` 1:1.
|
|
76
|
+
- **Run gate** — `createRunGate()`, `RunGatePolicy` / `RunGateRule` / `RunGateVerdict`, `ApprovalChannel` with `pollingApprovalChannel()` / `staticApprovalChannel()`, and `RunGateDecisionRecord` for the audit trail.
|
|
77
|
+
- **Adapters** — `HarnessAdapter`, `HarnessRun`; [`@agentic-kit/pi`](https://www.npmjs.com/package/@agentic-kit/pi) is the first implementation.
|
|
74
78
|
- **Blueprints** — `expandBlueprintDefaults()`, `BlueprintZod`/`BlueprintSchema`, field types/defaults, policy provisioning.
|
|
75
79
|
|
|
76
80
|
## Hosts
|
package/adapter.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What Constructive needs from a coding-agent harness.
|
|
3
|
+
*
|
|
4
|
+
* The lanes a run is wrapped in — the run log, the metering gateway, the run
|
|
5
|
+
* gate — are harness-neutral; only their *attachment* is vendor-specific (pi
|
|
6
|
+
* takes extension factories through a resource loader, another harness will take
|
|
7
|
+
* plugins, or a subprocess). An adapter is exactly that attachment plus the
|
|
8
|
+
* identity of the transcript it produces, so a host can start a run without
|
|
9
|
+
* naming a vendor.
|
|
10
|
+
*
|
|
11
|
+
* Deliberately thin: it declares only what a host actually calls on a started
|
|
12
|
+
* run. Prompting, tool registration and event subscription stay on the
|
|
13
|
+
* adapter's own run handle, because those surfaces differ enough between
|
|
14
|
+
* harnesses that a lowest common denominator would be a lie.
|
|
15
|
+
*/
|
|
16
|
+
export interface HarnessRun {
|
|
17
|
+
/** The run these lanes belong to; the run log's partition key. */
|
|
18
|
+
readonly runId: string;
|
|
19
|
+
/** Drain every lane that buffers (log entries, usage reports). */
|
|
20
|
+
flush(): Promise<void>;
|
|
21
|
+
/** Flush, then release the harness's resources. */
|
|
22
|
+
close(): Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
export interface HarnessAdapter<TStartOptions, TRun extends HarnessRun = HarnessRun> {
|
|
25
|
+
/** Stable adapter id, e.g. `pi`. */
|
|
26
|
+
readonly id: string;
|
|
27
|
+
/**
|
|
28
|
+
* The `transcriptFormat` the run's entries are recorded under, so a reader can
|
|
29
|
+
* pick the projector that understands them.
|
|
30
|
+
*/
|
|
31
|
+
readonly transcriptFormat: string;
|
|
32
|
+
startRun(options: TStartOptions): Promise<TRun>;
|
|
33
|
+
}
|
package/adapter.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* What Constructive needs from a coding-agent harness.
|
|
4
|
+
*
|
|
5
|
+
* The lanes a run is wrapped in — the run log, the metering gateway, the run
|
|
6
|
+
* gate — are harness-neutral; only their *attachment* is vendor-specific (pi
|
|
7
|
+
* takes extension factories through a resource loader, another harness will take
|
|
8
|
+
* plugins, or a subprocess). An adapter is exactly that attachment plus the
|
|
9
|
+
* identity of the transcript it produces, so a host can start a run without
|
|
10
|
+
* naming a vendor.
|
|
11
|
+
*
|
|
12
|
+
* Deliberately thin: it declares only what a host actually calls on a started
|
|
13
|
+
* run. Prompting, tool registration and event subscription stay on the
|
|
14
|
+
* adapter's own run handle, because those surfaces differ enough between
|
|
15
|
+
* harnesses that a lowest common denominator would be a lie.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
package/esm/adapter.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What Constructive needs from a coding-agent harness.
|
|
3
|
+
*
|
|
4
|
+
* The lanes a run is wrapped in — the run log, the metering gateway, the run
|
|
5
|
+
* gate — are harness-neutral; only their *attachment* is vendor-specific (pi
|
|
6
|
+
* takes extension factories through a resource loader, another harness will take
|
|
7
|
+
* plugins, or a subprocess). An adapter is exactly that attachment plus the
|
|
8
|
+
* identity of the transcript it produces, so a host can start a run without
|
|
9
|
+
* naming a vendor.
|
|
10
|
+
*
|
|
11
|
+
* Deliberately thin: it declares only what a host actually calls on a started
|
|
12
|
+
* run. Prompting, tool registration and event subscription stay on the
|
|
13
|
+
* adapter's own run handle, because those surfaces differ enough between
|
|
14
|
+
* harnesses that a lowest common denominator would be a lie.
|
|
15
|
+
*/
|
|
16
|
+
export interface HarnessRun {
|
|
17
|
+
/** The run these lanes belong to; the run log's partition key. */
|
|
18
|
+
readonly runId: string;
|
|
19
|
+
/** Drain every lane that buffers (log entries, usage reports). */
|
|
20
|
+
flush(): Promise<void>;
|
|
21
|
+
/** Flush, then release the harness's resources. */
|
|
22
|
+
close(): Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
export interface HarnessAdapter<TStartOptions, TRun extends HarnessRun = HarnessRun> {
|
|
25
|
+
/** Stable adapter id, e.g. `pi`. */
|
|
26
|
+
readonly id: string;
|
|
27
|
+
/**
|
|
28
|
+
* The `transcriptFormat` the run's entries are recorded under, so a reader can
|
|
29
|
+
* pick the projector that understands them.
|
|
30
|
+
*/
|
|
31
|
+
readonly transcriptFormat: string;
|
|
32
|
+
startRun(options: TStartOptions): Promise<TRun>;
|
|
33
|
+
}
|
package/esm/adapter.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What Constructive needs from a coding-agent harness.
|
|
3
|
+
*
|
|
4
|
+
* The lanes a run is wrapped in — the run log, the metering gateway, the run
|
|
5
|
+
* gate — are harness-neutral; only their *attachment* is vendor-specific (pi
|
|
6
|
+
* takes extension factories through a resource loader, another harness will take
|
|
7
|
+
* plugins, or a subprocess). An adapter is exactly that attachment plus the
|
|
8
|
+
* identity of the transcript it produces, so a host can start a run without
|
|
9
|
+
* naming a vendor.
|
|
10
|
+
*
|
|
11
|
+
* Deliberately thin: it declares only what a host actually calls on a started
|
|
12
|
+
* run. Prompting, tool registration and event subscription stay on the
|
|
13
|
+
* adapter's own run handle, because those surfaces differ enough between
|
|
14
|
+
* harnesses that a lowest common denominator would be a lie.
|
|
15
|
+
*/
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The asynchronous half of the gate: an `ask` verdict has to reach a human who
|
|
3
|
+
* may be on another machine, so approval is an interface rather than a prompt.
|
|
4
|
+
*
|
|
5
|
+
* `pollingApprovalChannel` is the baseline implementation of the platform's
|
|
6
|
+
* rendezvous model — the request is submitted (a row), and the decision is
|
|
7
|
+
* polled for (another row). No socket back into the agent's process, which is
|
|
8
|
+
* what lets a cloud run be approved from a browser.
|
|
9
|
+
*/
|
|
10
|
+
export interface ApprovalRequest {
|
|
11
|
+
runId: string;
|
|
12
|
+
toolCallId: string;
|
|
13
|
+
toolName: string;
|
|
14
|
+
input: Record<string, unknown>;
|
|
15
|
+
/** The policy's reason for asking. */
|
|
16
|
+
reason?: string;
|
|
17
|
+
requestedAt: string;
|
|
18
|
+
}
|
|
19
|
+
export interface ApprovalOutcome {
|
|
20
|
+
decision: 'allow' | 'deny';
|
|
21
|
+
reason?: string;
|
|
22
|
+
/** Who decided, when the channel knows. */
|
|
23
|
+
actorId?: string;
|
|
24
|
+
}
|
|
25
|
+
export interface ApprovalChannel {
|
|
26
|
+
/** Resolves once a decision exists; may take as long as a human takes. */
|
|
27
|
+
request(request: ApprovalRequest): Promise<ApprovalOutcome>;
|
|
28
|
+
}
|
|
29
|
+
export interface PollingApprovalChannelOptions {
|
|
30
|
+
/** Publish the request (insert the row, post to the API). */
|
|
31
|
+
submit: (request: ApprovalRequest) => Promise<void>;
|
|
32
|
+
/** Read the decision, or `undefined` while it is still outstanding. */
|
|
33
|
+
poll: (request: ApprovalRequest) => Promise<ApprovalOutcome | undefined>;
|
|
34
|
+
intervalMs?: number;
|
|
35
|
+
/** Give up after this long. Omit to wait indefinitely. */
|
|
36
|
+
timeoutMs?: number;
|
|
37
|
+
/** Verdict on timeout. Defaults to `deny`: an unattended run must not take an
|
|
38
|
+
* action nobody approved just because nobody was watching. */
|
|
39
|
+
onTimeout?: 'deny' | 'allow';
|
|
40
|
+
/** Injectable clock/sleep for tests. */
|
|
41
|
+
sleep?: (ms: number) => Promise<void>;
|
|
42
|
+
now?: () => number;
|
|
43
|
+
}
|
|
44
|
+
export declare function pollingApprovalChannel(options: PollingApprovalChannelOptions): ApprovalChannel;
|
|
45
|
+
/** Decide every request the same way — for tests, and for a headless run that is
|
|
46
|
+
* explicitly configured to auto-approve. */
|
|
47
|
+
export declare function staticApprovalChannel(outcome: ApprovalOutcome): ApprovalChannel;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The asynchronous half of the gate: an `ask` verdict has to reach a human who
|
|
3
|
+
* may be on another machine, so approval is an interface rather than a prompt.
|
|
4
|
+
*
|
|
5
|
+
* `pollingApprovalChannel` is the baseline implementation of the platform's
|
|
6
|
+
* rendezvous model — the request is submitted (a row), and the decision is
|
|
7
|
+
* polled for (another row). No socket back into the agent's process, which is
|
|
8
|
+
* what lets a cloud run be approved from a browser.
|
|
9
|
+
*/
|
|
10
|
+
export function pollingApprovalChannel(options) {
|
|
11
|
+
const intervalMs = options.intervalMs ?? 1000;
|
|
12
|
+
const onTimeout = options.onTimeout ?? 'deny';
|
|
13
|
+
const sleep = options.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
14
|
+
const now = options.now ?? (() => Date.now());
|
|
15
|
+
return {
|
|
16
|
+
async request(request) {
|
|
17
|
+
await options.submit(request);
|
|
18
|
+
const startedAt = now();
|
|
19
|
+
for (;;) {
|
|
20
|
+
const outcome = await options.poll(request);
|
|
21
|
+
if (outcome)
|
|
22
|
+
return outcome;
|
|
23
|
+
if (options.timeoutMs !== undefined && now() - startedAt >= options.timeoutMs) {
|
|
24
|
+
return {
|
|
25
|
+
decision: onTimeout,
|
|
26
|
+
reason: `gate: no decision within ${options.timeoutMs}ms`
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
await sleep(intervalMs);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/** Decide every request the same way — for tests, and for a headless run that is
|
|
35
|
+
* explicitly configured to auto-approve. */
|
|
36
|
+
export function staticApprovalChannel(outcome) {
|
|
37
|
+
return { request: () => Promise.resolve(outcome) };
|
|
38
|
+
}
|
|
@@ -1,15 +1,14 @@
|
|
|
1
|
+
import type { ConstructiveGateDeps } from './constructive-policy';
|
|
2
|
+
import type { GatePolicy, GateToolCallEvent } from './policy';
|
|
1
3
|
import type { ConfirmPreview } from './preview';
|
|
2
4
|
/**
|
|
3
|
-
* Host-neutral confirm gate
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* Host-neutral confirm gate. It owns the mechanics of asking a human —
|
|
6
|
+
* decline memory, auto-skipping a declined retry, refusing gated calls on a
|
|
7
|
+
* headless host — and takes both the host's capabilities (confirm UI, skip
|
|
8
|
+
* notification) and its policy (which calls are gated, what the prompt says)
|
|
9
|
+
* as injected dependencies. Structurally mirrors the pi extension `tool_call`
|
|
10
|
+
* hook so a pi adapter is a thin mapping: no Electron, no pi imports.
|
|
7
11
|
*/
|
|
8
|
-
export type GateToolCallEvent = {
|
|
9
|
-
toolName: string;
|
|
10
|
-
toolCallId: string;
|
|
11
|
-
input?: Record<string, unknown>;
|
|
12
|
-
};
|
|
13
12
|
/** `block: true` stops the tool call; `reason` is surfaced to the agent. */
|
|
14
13
|
export type GateResult = {
|
|
15
14
|
block: true;
|
|
@@ -23,31 +22,21 @@ export type GateHost = {
|
|
|
23
22
|
/** A retry of an already-declined call was auto-skipped without prompting. */
|
|
24
23
|
notifyToolSkipped(toolCallId: string): void;
|
|
25
24
|
};
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
* preview can't be built.
|
|
39
|
-
*/
|
|
40
|
-
resolveTemplatePreview(cwd: string, blueprintName: string | undefined, displayName: string): Promise<ConfirmPreview | undefined>;
|
|
41
|
-
/**
|
|
42
|
-
* Tool names that require a human decision. The policy belongs to the host,
|
|
43
|
-
* not the harness: Desktop/CLI gate Constructive's mutating db tools, a
|
|
44
|
-
* remote coding host gates a different set. Defaults to
|
|
45
|
-
* `MUTATING_DB_TOOLS`.
|
|
46
|
-
*/
|
|
47
|
-
gatedTools?: ReadonlySet<string>;
|
|
25
|
+
/**
|
|
26
|
+
* The Constructive database deps, from which the gate derives the default
|
|
27
|
+
* `GatePolicy`. Kept as the name every existing host passes.
|
|
28
|
+
*/
|
|
29
|
+
export type ConfirmGateDeps = ConstructiveGateDeps;
|
|
30
|
+
/**
|
|
31
|
+
* Either hand the gate the Constructive deps and get its database policy, or
|
|
32
|
+
* hand it a policy of your own — a remote coding host gating `bash` has no
|
|
33
|
+
* project context or data token to speak of.
|
|
34
|
+
*/
|
|
35
|
+
export type ConfirmGateOptions = ConfirmGateDeps | {
|
|
36
|
+
policy: GatePolicy;
|
|
48
37
|
};
|
|
49
38
|
export type ConfirmGate = {
|
|
50
39
|
onAgentStart: () => void;
|
|
51
40
|
onToolCall: (event: GateToolCallEvent, host: GateHost, cwd: string) => Promise<GateResult>;
|
|
52
41
|
};
|
|
53
|
-
export declare function createConfirmGate(
|
|
42
|
+
export declare function createConfirmGate(options: ConfirmGateOptions): ConfirmGate;
|
|
@@ -1,31 +1,14 @@
|
|
|
1
|
+
import { createConstructiveGatePolicy } from './constructive-policy';
|
|
1
2
|
import { buildDeclineReason, createDeclineGuard } from './decline-guard';
|
|
2
|
-
|
|
3
|
-
export function createConfirmGate(deps) {
|
|
3
|
+
export function createConfirmGate(options) {
|
|
4
4
|
const declineGuard = createDeclineGuard();
|
|
5
|
-
const
|
|
6
|
-
async function confirmOrDecline(event, host, input, preview) {
|
|
7
|
-
const { title, message, preview: basePreview } = buildConfirmPrompt(event.toolName, input);
|
|
8
|
-
const approved = await host.confirmTool(event.toolCallId, title, message, preview ?? basePreview);
|
|
9
|
-
if (!approved) {
|
|
10
|
-
declineGuard.recordDecline(event.toolName, input);
|
|
11
|
-
return { block: true, reason: buildDeclineReason(event.toolName) };
|
|
12
|
-
}
|
|
13
|
-
// An approved mutation changes database state, so earlier declines may no
|
|
14
|
-
// longer describe the same effect (e.g. a create_template preview derives
|
|
15
|
-
// from the blueprint, not the input) — let them re-prompt with fresh eyes.
|
|
16
|
-
declineGuard.clear();
|
|
17
|
-
return undefined;
|
|
18
|
-
}
|
|
5
|
+
const policy = 'policy' in options ? options.policy : createConstructiveGatePolicy(options);
|
|
19
6
|
return {
|
|
20
7
|
onAgentStart: () => declineGuard.clear(),
|
|
21
8
|
onToolCall: async (event, host, cwd) => {
|
|
22
|
-
if (!
|
|
9
|
+
if (!policy.isGated(event))
|
|
23
10
|
return;
|
|
24
11
|
const input = event.input;
|
|
25
|
-
// manage_entity_types multiplexes read + write actions behind one tool
|
|
26
|
-
// name; its read action is not a mutation, so it skips the gate.
|
|
27
|
-
if (event.toolName === 'manage_entity_types' && input?.action === 'list')
|
|
28
|
-
return;
|
|
29
12
|
const retryBlock = declineGuard.checkRetry(event.toolName, input);
|
|
30
13
|
if (retryBlock) {
|
|
31
14
|
if (host.hasUI) {
|
|
@@ -39,27 +22,19 @@ export function createConfirmGate(deps) {
|
|
|
39
22
|
reason: `Cannot confirm "${event.toolName}" — no confirmation UI is available.`,
|
|
40
23
|
};
|
|
41
24
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (event.toolName === 'provision_database') {
|
|
45
|
-
return confirmOrDecline(event, host, input);
|
|
46
|
-
}
|
|
47
|
-
if (!(await deps.isProjectRunnable(cwd)))
|
|
25
|
+
const prompt = await policy.resolvePrompt(event, cwd);
|
|
26
|
+
if (!prompt)
|
|
48
27
|
return;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
!(await deps.hasDataToken(cwd))) {
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
let resolvedPreview;
|
|
57
|
-
if (event.toolName === 'create_template') {
|
|
58
|
-
const blueprintName = typeof input?.blueprintName === 'string' ? input.blueprintName : undefined;
|
|
59
|
-
const displayName = typeof input?.displayName === 'string' ? input.displayName : '';
|
|
60
|
-
resolvedPreview = await deps.resolveTemplatePreview(cwd, blueprintName, displayName);
|
|
28
|
+
const approved = await host.confirmTool(event.toolCallId, prompt.title, prompt.message, prompt.preview);
|
|
29
|
+
if (!approved) {
|
|
30
|
+
declineGuard.recordDecline(event.toolName, input);
|
|
31
|
+
return { block: true, reason: buildDeclineReason(event.toolName) };
|
|
61
32
|
}
|
|
62
|
-
|
|
33
|
+
// An approved mutation changes state, so earlier declines may no longer
|
|
34
|
+
// describe the same effect (e.g. a create_template preview derives from
|
|
35
|
+
// the blueprint, not the input) — let them re-prompt with fresh eyes.
|
|
36
|
+
declineGuard.clear();
|
|
37
|
+
return undefined;
|
|
63
38
|
},
|
|
64
39
|
};
|
|
65
40
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { GatePolicy } from './policy';
|
|
2
|
+
import type { ConfirmPreview } from './preview';
|
|
3
|
+
/**
|
|
4
|
+
* Host capabilities the Constructive database policy needs. These are
|
|
5
|
+
* Constructive-specific — a policy for another host asks for whatever *its*
|
|
6
|
+
* rules depend on instead.
|
|
7
|
+
*/
|
|
8
|
+
export type ConstructiveGateDeps = {
|
|
9
|
+
/**
|
|
10
|
+
* Whether the project is provisioned/runnable at `cwd`. Unrunnable calls
|
|
11
|
+
* skip the confirm so the tool can return its clean "provision first"
|
|
12
|
+
* message instead of making the user approve something that fails.
|
|
13
|
+
*/
|
|
14
|
+
isProjectRunnable(cwd: string): Promise<boolean>;
|
|
15
|
+
/** Whether a data-plane token is available (gates `add_records`). */
|
|
16
|
+
hasDataToken(cwd: string): Promise<boolean>;
|
|
17
|
+
/**
|
|
18
|
+
* Resolve the preview for `create_template` (its tables live in the
|
|
19
|
+
* blueprint it copies, not in the tool input). Return undefined when a
|
|
20
|
+
* preview can't be built.
|
|
21
|
+
*/
|
|
22
|
+
resolveTemplatePreview(cwd: string, blueprintName: string | undefined, displayName: string): Promise<ConfirmPreview | undefined>;
|
|
23
|
+
/**
|
|
24
|
+
* Tool names that require a human decision. Defaults to
|
|
25
|
+
* `MUTATING_DB_TOOLS`; a host that wants a different set entirely is better
|
|
26
|
+
* served by its own `GatePolicy`.
|
|
27
|
+
*/
|
|
28
|
+
gatedTools?: ReadonlySet<string>;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* The gate policy for Constructive's database tools, as the Desktop and CLI
|
|
32
|
+
* hosts want it: gate every mutating db tool, and skip the confirm when the
|
|
33
|
+
* call is going to bounce off a missing project or a missing sign-in anyway.
|
|
34
|
+
*/
|
|
35
|
+
export declare function createConstructiveGatePolicy(deps: ConstructiveGateDeps): GatePolicy;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { buildConfirmPrompt, MUTATING_DB_TOOLS } from './prompts';
|
|
2
|
+
/**
|
|
3
|
+
* The gate policy for Constructive's database tools, as the Desktop and CLI
|
|
4
|
+
* hosts want it: gate every mutating db tool, and skip the confirm when the
|
|
5
|
+
* call is going to bounce off a missing project or a missing sign-in anyway.
|
|
6
|
+
*/
|
|
7
|
+
export function createConstructiveGatePolicy(deps) {
|
|
8
|
+
const gatedTools = deps.gatedTools ?? MUTATING_DB_TOOLS;
|
|
9
|
+
return {
|
|
10
|
+
isGated: (event) => {
|
|
11
|
+
if (!gatedTools.has(event.toolName))
|
|
12
|
+
return false;
|
|
13
|
+
// manage_entity_types multiplexes read + write actions behind one tool
|
|
14
|
+
// name; its read action is not a mutation, so it skips the gate.
|
|
15
|
+
if (event.toolName === 'manage_entity_types' && event.input?.action === 'list')
|
|
16
|
+
return false;
|
|
17
|
+
return true;
|
|
18
|
+
},
|
|
19
|
+
resolvePrompt: async (event, cwd) => {
|
|
20
|
+
const input = event.input;
|
|
21
|
+
const prompt = buildConfirmPrompt(event.toolName, input);
|
|
22
|
+
// provision_database is the tool that CREATES the project context, so it
|
|
23
|
+
// can't gate on an existing one — confirm it directly.
|
|
24
|
+
if (event.toolName === 'provision_database')
|
|
25
|
+
return prompt;
|
|
26
|
+
if (!(await deps.isProjectRunnable(cwd)))
|
|
27
|
+
return null;
|
|
28
|
+
// Tools that need an app sign-in skip the confirm when no data token
|
|
29
|
+
// exists — the tool returns its sign-in prompt instead of making the
|
|
30
|
+
// user approve something that fails.
|
|
31
|
+
if ((event.toolName === 'add_records' || event.toolName === 'create_api_key') &&
|
|
32
|
+
!(await deps.hasDataToken(cwd))) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
if (event.toolName === 'create_template') {
|
|
36
|
+
const blueprintName = typeof input?.blueprintName === 'string' ? input.blueprintName : undefined;
|
|
37
|
+
const displayName = typeof input?.displayName === 'string' ? input.displayName : '';
|
|
38
|
+
const preview = await deps.resolveTemplatePreview(cwd, blueprintName, displayName);
|
|
39
|
+
if (preview)
|
|
40
|
+
return { ...prompt, preview };
|
|
41
|
+
}
|
|
42
|
+
return prompt;
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ConfirmPrompt } from './preview';
|
|
2
|
+
/**
|
|
3
|
+
* What the gate knows about a tool call. Hosts map their agent's event shape
|
|
4
|
+
* onto this (pi's `ToolCallEvent`, an MCP request, …).
|
|
5
|
+
*/
|
|
6
|
+
export type GateToolCallEvent = {
|
|
7
|
+
toolName: string;
|
|
8
|
+
toolCallId: string;
|
|
9
|
+
input?: Record<string, unknown>;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* The host's answer to "which calls need a human decision, and what do we show
|
|
13
|
+
* the human?". Everything tool-specific lives here — which tool names are
|
|
14
|
+
* gated, which arguments turn a gated name into a harmless read, and the
|
|
15
|
+
* wording of the confirmation — so the gate itself stays host-neutral and only
|
|
16
|
+
* owns the mechanics (decline memory, headless blocking, prompting).
|
|
17
|
+
*
|
|
18
|
+
* `createConstructiveGatePolicy` is the Constructive database policy the
|
|
19
|
+
* Desktop/CLI hosts use; a remote coding host supplies its own.
|
|
20
|
+
*/
|
|
21
|
+
export type GatePolicy = {
|
|
22
|
+
/**
|
|
23
|
+
* Whether this call needs a human decision at all. Cheap and synchronous:
|
|
24
|
+
* it runs before the decline memory and the headless block, so it must
|
|
25
|
+
* decide from the tool name and arguments alone — no I/O.
|
|
26
|
+
*/
|
|
27
|
+
isGated(event: GateToolCallEvent): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* The prompt to put in front of the user, or `null` to let an already-gated
|
|
30
|
+
* call through without one. Runs only for gated calls on a host with a
|
|
31
|
+
* confirm surface, so it may do I/O to build a richer preview or to check a
|
|
32
|
+
* precondition that makes the confirm pointless (e.g. the call is going to
|
|
33
|
+
* fail with "sign in first" anyway).
|
|
34
|
+
*/
|
|
35
|
+
resolvePrompt(event: GateToolCallEvent, cwd: string): Promise<ConfirmPrompt | null>;
|
|
36
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/esm/gating/preview.d.ts
CHANGED
|
@@ -35,3 +35,9 @@ export type ConfirmPreview = {
|
|
|
35
35
|
tableName: string;
|
|
36
36
|
field: ConfirmPreviewField;
|
|
37
37
|
};
|
|
38
|
+
/** What a host puts in front of the user for one gated call. */
|
|
39
|
+
export type ConfirmPrompt = {
|
|
40
|
+
title: string;
|
|
41
|
+
message: string;
|
|
42
|
+
preview?: ConfirmPreview;
|
|
43
|
+
};
|
package/esm/gating/prompts.d.ts
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
1
|
export declare const MUTATING_DB_TOOLS: Set<string>;
|
|
2
|
-
import type {
|
|
3
|
-
export type ConfirmPrompt = {
|
|
4
|
-
title: string;
|
|
5
|
-
message: string;
|
|
6
|
-
preview?: ConfirmPreview;
|
|
7
|
-
};
|
|
2
|
+
import type { ConfirmPrompt } from './preview';
|
|
8
3
|
export declare function buildConfirmPrompt(toolName: string, input: Record<string, unknown> | undefined): ConfirmPrompt;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The run gate: evaluate the run's policy for one tool call and, when the policy
|
|
3
|
+
* will not decide alone, wait for a human through the approval channel.
|
|
4
|
+
*
|
|
5
|
+
* Harness-neutral on purpose. An adapter owns one line of glue — map its
|
|
6
|
+
* harness's tool-call event onto `decide()` and turn the outcome into whatever
|
|
7
|
+
* that harness expects from a blocked call — so the policy, the approval
|
|
8
|
+
* rendezvous and the audit record are the same for every harness.
|
|
9
|
+
*/
|
|
10
|
+
import { type ApprovalChannel, type ApprovalRequest } from './approvals';
|
|
11
|
+
import { RunGatePolicy, type RunGatePolicyOptions, type RunGateVerdict } from './run-policy';
|
|
12
|
+
export interface RunGateDecisionRecord {
|
|
13
|
+
runId: string;
|
|
14
|
+
toolCallId: string;
|
|
15
|
+
toolName: string;
|
|
16
|
+
input: Record<string, unknown>;
|
|
17
|
+
/** What the policy said. */
|
|
18
|
+
verdict: RunGateVerdict;
|
|
19
|
+
/** How the request was finally settled. */
|
|
20
|
+
decision: 'allow' | 'deny';
|
|
21
|
+
reason?: string;
|
|
22
|
+
actorId?: string;
|
|
23
|
+
decidedAt: string;
|
|
24
|
+
}
|
|
25
|
+
export interface RunGateOptions {
|
|
26
|
+
runId: string;
|
|
27
|
+
/** A policy, or the options to build one. */
|
|
28
|
+
policy: RunGatePolicy | RunGatePolicyOptions;
|
|
29
|
+
/** Required only if the policy can produce `ask`. */
|
|
30
|
+
approvals?: ApprovalChannel;
|
|
31
|
+
/** Every settled decision, for the run log / audit trail. */
|
|
32
|
+
onDecision?: (record: RunGateDecisionRecord) => void;
|
|
33
|
+
now?: () => Date;
|
|
34
|
+
}
|
|
35
|
+
export interface RunGateToolCall {
|
|
36
|
+
toolCallId: string;
|
|
37
|
+
toolName: string;
|
|
38
|
+
input?: Record<string, unknown>;
|
|
39
|
+
}
|
|
40
|
+
export interface RunGateOutcome {
|
|
41
|
+
/** `false` means the harness must not run the call. */
|
|
42
|
+
allowed: boolean;
|
|
43
|
+
/** The model's only explanation for a refusal, so policies should be specific. */
|
|
44
|
+
reason?: string;
|
|
45
|
+
}
|
|
46
|
+
export declare class RunGate {
|
|
47
|
+
readonly policy: RunGatePolicy;
|
|
48
|
+
/** Requests currently waiting on a human. */
|
|
49
|
+
readonly pending: ReadonlyMap<string, ApprovalRequest>;
|
|
50
|
+
private readonly options;
|
|
51
|
+
private readonly now;
|
|
52
|
+
private readonly waiting;
|
|
53
|
+
constructor(options: RunGateOptions);
|
|
54
|
+
decide(call: RunGateToolCall): Promise<RunGateOutcome>;
|
|
55
|
+
private ask;
|
|
56
|
+
}
|
|
57
|
+
export declare function createRunGate(options: RunGateOptions): RunGate;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The run gate: evaluate the run's policy for one tool call and, when the policy
|
|
3
|
+
* will not decide alone, wait for a human through the approval channel.
|
|
4
|
+
*
|
|
5
|
+
* Harness-neutral on purpose. An adapter owns one line of glue — map its
|
|
6
|
+
* harness's tool-call event onto `decide()` and turn the outcome into whatever
|
|
7
|
+
* that harness expects from a blocked call — so the policy, the approval
|
|
8
|
+
* rendezvous and the audit record are the same for every harness.
|
|
9
|
+
*/
|
|
10
|
+
import { RunGatePolicy } from './run-policy';
|
|
11
|
+
export class RunGate {
|
|
12
|
+
policy;
|
|
13
|
+
/** Requests currently waiting on a human. */
|
|
14
|
+
pending;
|
|
15
|
+
options;
|
|
16
|
+
now;
|
|
17
|
+
waiting = new Map();
|
|
18
|
+
constructor(options) {
|
|
19
|
+
this.options = options;
|
|
20
|
+
this.now = options.now ?? (() => new Date());
|
|
21
|
+
this.policy = options.policy instanceof RunGatePolicy ? options.policy : new RunGatePolicy(options.policy);
|
|
22
|
+
this.pending = this.waiting;
|
|
23
|
+
if (canAsk(this.policy) && !options.approvals) {
|
|
24
|
+
throw new Error('gate: the policy can produce "ask", so an approvals channel is required');
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async decide(call) {
|
|
28
|
+
const input = call.input ?? {};
|
|
29
|
+
const verdict = this.policy.evaluate({ toolName: call.toolName, input });
|
|
30
|
+
const settled = verdict.decision === 'ask'
|
|
31
|
+
? await this.ask(call.toolCallId, call.toolName, input, verdict)
|
|
32
|
+
: { decision: verdict.decision, ...(verdict.reason === undefined ? {} : { reason: verdict.reason }) };
|
|
33
|
+
this.options.onDecision?.({
|
|
34
|
+
runId: this.options.runId,
|
|
35
|
+
toolCallId: call.toolCallId,
|
|
36
|
+
toolName: call.toolName,
|
|
37
|
+
input,
|
|
38
|
+
verdict,
|
|
39
|
+
decision: settled.decision,
|
|
40
|
+
...(settled.reason === undefined ? {} : { reason: settled.reason }),
|
|
41
|
+
...(settled.actorId === undefined ? {} : { actorId: settled.actorId }),
|
|
42
|
+
decidedAt: this.now().toISOString()
|
|
43
|
+
});
|
|
44
|
+
if (settled.decision === 'allow')
|
|
45
|
+
return { allowed: true };
|
|
46
|
+
return { allowed: false, reason: settled.reason ?? `gate: ${call.toolName} is not permitted in this run` };
|
|
47
|
+
}
|
|
48
|
+
async ask(toolCallId, toolName, input, verdict) {
|
|
49
|
+
const request = {
|
|
50
|
+
runId: this.options.runId,
|
|
51
|
+
toolCallId,
|
|
52
|
+
toolName,
|
|
53
|
+
input,
|
|
54
|
+
...(verdict.reason === undefined ? {} : { reason: verdict.reason }),
|
|
55
|
+
requestedAt: this.now().toISOString()
|
|
56
|
+
};
|
|
57
|
+
this.waiting.set(toolCallId, request);
|
|
58
|
+
try {
|
|
59
|
+
return await this.options.approvals.request(request);
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
this.waiting.delete(toolCallId);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
export function createRunGate(options) {
|
|
67
|
+
return new RunGate(options);
|
|
68
|
+
}
|
|
69
|
+
function canAsk(policy) {
|
|
70
|
+
const decisions = [policy.defaultDecision, ...policy.rules.map((rule) => rule.decision)];
|
|
71
|
+
return decisions.includes('ask');
|
|
72
|
+
}
|