@qlairoslabs/acp-client 0.12.1 → 0.13.1-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG-consumer-fixes.md +17 -10
- package/CHANGELOG-controller-interface.md +258 -0
- package/CHANGELOG-metering.md +29 -0
- package/CHANGELOG-operation-lifecycle.md +15 -1
- package/CONSUMER_GUIDE.md +984 -6
- package/MIGRATION-0.10-to-0.12.md +4 -1
- package/MIGRATION-0.12-to-0.13.md +288 -0
- package/README.md +17 -16
- package/package.json +11 -2
- package/src/acp-client-handlers.ts +48 -0
- package/src/activity.ts +3 -0
- package/src/adapters.ts +25 -0
- package/src/agent/adoption/provider-attachment.ts +33 -0
- package/src/agent/adoption/provider-session-delete.ts +1 -1
- package/src/agent/adoption/provider-sessions.ts +3 -2
- package/src/agent/append-assistant-message.ts +91 -0
- package/src/agent/config.ts +54 -6
- package/src/agent/conversation-manager.ts +95 -158
- package/src/agent/manager-settings.ts +1 -0
- package/src/agent/native-channel-lifecycle.ts +111 -0
- package/src/agent/native-operations.ts +39 -0
- package/src/agent/persisted-lifecycle.ts +44 -4
- package/src/agent/persistence-policy/context.ts +13 -0
- package/src/agent/persistence-policy/resolver.ts +7 -0
- package/src/agent/persistence-policy/types.ts +1 -0
- package/src/agent/record-capture.ts +1 -0
- package/src/agent-adapter-registry.ts +33 -7
- package/src/agent-conversation-contracts.ts +14 -1
- package/src/agent-conversation-errors.ts +8 -0
- package/src/agent-conversation-owner.ts +9 -4
- package/src/agent-session-config.ts +137 -0
- package/src/agent-session.ts +440 -934
- package/src/agent.ts +168 -0
- package/src/capabilities.ts +2 -0
- package/src/checkpointing/recovery.ts +3 -0
- package/src/cli/index.ts +3 -3
- package/src/cli/managed.ts +2 -2
- package/src/client.ts +56 -96
- package/src/connection-attempt.ts +95 -0
- package/src/controller-connection.ts +115 -0
- package/src/controller.ts +158 -148
- package/src/conversation-controller/capabilities.ts +88 -0
- package/src/conversation-controller/contracts.ts +208 -0
- package/src/conversation-controller/factory.ts +39 -0
- package/src/conversation-controller/guards.ts +98 -0
- package/src/conversation-controller/index.ts +20 -0
- package/src/conversation-startup.ts +35 -0
- package/src/conversation-turns/backend-turns.ts +373 -0
- package/src/conversation-turns/clear-operations.ts +37 -0
- package/src/conversation-turns/context.ts +135 -0
- package/src/conversation-turns/index.ts +4 -0
- package/src/conversation-turns/record-helpers.ts +75 -0
- package/src/conversation-turns/recording.ts +235 -0
- package/src/conversation-turns/run-turn.ts +367 -0
- package/src/conversation-turns/turn-run.ts +57 -0
- package/src/errors.ts +41 -0
- package/src/handoff-options.ts +81 -0
- package/src/identity/checkpoint-backed.ts +1 -1
- package/src/index.ts +59 -3
- package/src/internal-inference/coordination/manager.ts +11 -0
- package/src/internal-inference/runtime/harness-runtime.ts +4 -4
- package/src/launcher.ts +37 -2
- package/src/media/parts.ts +5 -0
- package/src/message-status.ts +83 -0
- package/src/metering/engine.ts +39 -64
- package/src/metering/measurement-provenance.ts +36 -0
- package/src/metering/native/durability.ts +115 -0
- package/src/metering/native/ledger.ts +104 -0
- package/src/metering/native/operation.ts +85 -0
- package/src/metering/native/projection.ts +114 -0
- package/src/metering/native/proof.ts +51 -0
- package/src/metering/native/requirements.ts +10 -0
- package/src/metering/native/retirement.ts +92 -0
- package/src/metering/native/source.ts +90 -0
- package/src/metering/record.ts +38 -1
- package/src/metering/state.ts +13 -3
- package/src/native/approvals.ts +186 -0
- package/src/native/capabilities.ts +17 -0
- package/src/native/channel-changes.ts +211 -0
- package/src/native/connection.ts +56 -0
- package/src/native/controller.ts +592 -0
- package/src/native/delegation-sender.ts +26 -0
- package/src/native/errors.ts +35 -0
- package/src/native/functions.ts +156 -0
- package/src/native/host-config.ts +59 -0
- package/src/native/host.ts +978 -0
- package/src/native/item-receipts.ts +15 -0
- package/src/native/metering.ts +12 -0
- package/src/native/records.ts +166 -0
- package/src/native/recovery.ts +186 -0
- package/src/native/reminders/fixtures/schema21.sqlite.gz +0 -0
- package/src/native/reminders/idle.ts +70 -0
- package/src/native/reminders/limits.ts +19 -0
- package/src/native/reminders/notifications.ts +29 -0
- package/src/native/reminders/routine-guard.ts +66 -0
- package/src/native/reminders/routine-schedule.ts +25 -0
- package/src/native/reminders/routine-wire.ts +21 -0
- package/src/native/reminders/schedule.ts +97 -0
- package/src/native/reminders/service.ts +280 -0
- package/src/native/reminders/supervisor.ts +91 -0
- package/src/native/reminders/sync.ts +301 -0
- package/src/native/reminders/wire.ts +197 -0
- package/src/native/submission-withdrawal.ts +65 -0
- package/src/native/usage.ts +15 -0
- package/src/native/worker-delivery.ts +61 -0
- package/src/native-harness/README.md +726 -0
- package/src/native-harness/package.json +24 -0
- package/src/native-harness/scripts/build-routine-zones.py +47 -0
- package/src/native-harness/src/capabilities/context/build.ts +59 -0
- package/src/native-harness/src/capabilities/context/instructions/agent-demo.md +11 -0
- package/src/native-harness/src/capabilities/context/instructions/agent.md +5 -0
- package/src/native-harness/src/capabilities/context/instructions/harness.md +7 -0
- package/src/native-harness/src/capabilities/context/load-instructions.ts +22 -0
- package/src/native-harness/src/capabilities/context/session.ts +93 -0
- package/src/native-harness/src/capabilities/model/attempt.ts +27 -0
- package/src/native-harness/src/capabilities/model/openrouter-stream.ts +72 -0
- package/src/native-harness/src/capabilities/model/openrouter.ts +62 -0
- package/src/native-harness/src/capabilities/model/runner.ts +116 -0
- package/src/native-harness/src/capabilities/model/scenario-attempt.ts +50 -0
- package/src/native-harness/src/capabilities/model/scenario-file.ts +125 -0
- package/src/native-harness/src/capabilities/model/scripted-error.ts +6 -0
- package/src/native-harness/src/capabilities/model/scripted.ts +81 -0
- package/src/native-harness/src/capabilities/model/usage.ts +15 -0
- package/src/native-harness/src/capabilities/reminders/cron-calendar.ts +50 -0
- package/src/native-harness/src/capabilities/reminders/cron-fields.ts +46 -0
- package/src/native-harness/src/capabilities/reminders/cron-proof.ts +32 -0
- package/src/native-harness/src/capabilities/reminders/cron-zone.ts +53 -0
- package/src/native-harness/src/capabilities/reminders/iana-zones.json.gz +0 -0
- package/src/native-harness/src/capabilities/reminders/iana-zones.ts +34 -0
- package/src/native-harness/src/capabilities/reminders/interval.ts +30 -0
- package/src/native-harness/src/capabilities/reminders/routine-advance.ts +47 -0
- package/src/native-harness/src/capabilities/reminders/routine-backlog.ts +47 -0
- package/src/native-harness/src/capabilities/reminders/routine-expiry.ts +15 -0
- package/src/native-harness/src/capabilities/reminders/routine-occurrence.ts +65 -0
- package/src/native-harness/src/capabilities/reminders/routine-schedule.ts +70 -0
- package/src/native-harness/src/capabilities/reminders/unattended.ts +8 -0
- package/src/native-harness/src/capabilities/tools/builtin/current-time.ts +28 -0
- package/src/native-harness/src/capabilities/tools/builtin/notify-channel.ts +17 -0
- package/src/native-harness/src/capabilities/tools/debug/index.ts +13 -0
- package/src/native-harness/src/capabilities/tools/debug/result.ts +12 -0
- package/src/native-harness/src/capabilities/tools/debug/test-echo.ts +16 -0
- package/src/native-harness/src/capabilities/tools/debug/test-fail.ts +14 -0
- package/src/native-harness/src/capabilities/tools/debug/test-wait.ts +19 -0
- package/src/native-harness/src/capabilities/tools/reminders/index.ts +97 -0
- package/src/native-harness/src/capabilities/tools/runtime/catalog.ts +103 -0
- package/src/native-harness/src/capabilities/tools/runtime/functions.ts +130 -0
- package/src/native-harness/src/capabilities/tools/runtime/invocations.ts +48 -0
- package/src/native-harness/src/capabilities/tools/testing/echo.ts +18 -0
- package/src/native-harness/src/capabilities/tools/testing/record.ts +47 -0
- package/src/native-harness/src/capabilities/tools/timeline/index.ts +41 -0
- package/src/native-harness/src/client.ts +18 -0
- package/src/native-harness/src/contracts/agent.ts +105 -0
- package/src/native-harness/src/contracts/config-permissions.ts +49 -0
- package/src/native-harness/src/contracts/context.ts +112 -0
- package/src/native-harness/src/contracts/events.ts +47 -0
- package/src/native-harness/src/contracts/input-limits.ts +7 -0
- package/src/native-harness/src/contracts/inspection.ts +22 -0
- package/src/native-harness/src/contracts/loop.ts +31 -0
- package/src/native-harness/src/contracts/model-execution.ts +83 -0
- package/src/native-harness/src/contracts/operator-controls.ts +106 -0
- package/src/native-harness/src/contracts/output.ts +13 -0
- package/src/native-harness/src/contracts/protocol.ts +320 -0
- package/src/native-harness/src/contracts/reminders.ts +134 -0
- package/src/native-harness/src/contracts/routines.ts +20 -0
- package/src/native-harness/src/contracts/scenario.ts +42 -0
- package/src/native-harness/src/contracts/session-file.ts +20 -0
- package/src/native-harness/src/contracts/session.ts +184 -0
- package/src/native-harness/src/contracts/storage.ts +597 -0
- package/src/native-harness/src/contracts/timeline-retrieval.ts +40 -0
- package/src/native-harness/src/contracts/tools.ts +133 -0
- package/src/native-harness/src/governance/permissions/gate.ts +129 -0
- package/src/native-harness/src/governance/policy/resolve.ts +32 -0
- package/src/native-harness/src/index.ts +322 -0
- package/src/native-harness/src/interfaces/cli/chat.ts +83 -0
- package/src/native-harness/src/interfaces/cli/development.ts +145 -0
- package/src/native-harness/src/interfaces/cli/format-reply.ts +27 -0
- package/src/native-harness/src/interfaces/cli/listeners.ts +74 -0
- package/src/native-harness/src/interfaces/cli/main.ts +47 -0
- package/src/native-harness/src/interfaces/cli/scenario.ts +37 -0
- package/src/native-harness/src/interfaces/cli/serve.ts +21 -0
- package/src/native-harness/src/interfaces/cli/start.ts +106 -0
- package/src/native-harness/src/interfaces/client.ts +225 -0
- package/src/native-harness/src/interfaces/inspector/index.html +283 -0
- package/src/native-harness/src/interfaces/inspector/observe.ts +35 -0
- package/src/native-harness/src/interfaces/inspector/server.ts +56 -0
- package/src/native-harness/src/interfaces/inspector/state.ts +92 -0
- package/src/native-harness/src/interfaces/protocol/channel-changes.ts +153 -0
- package/src/native-harness/src/interfaces/protocol/framing.ts +47 -0
- package/src/native-harness/src/interfaces/protocol/functions.ts +87 -0
- package/src/native-harness/src/interfaces/protocol/in-process.ts +67 -0
- package/src/native-harness/src/interfaces/protocol/live-queue.ts +55 -0
- package/src/native-harness/src/interfaces/protocol/load.ts +60 -0
- package/src/native-harness/src/interfaces/protocol/local.ts +120 -0
- package/src/native-harness/src/interfaces/protocol/operator.ts +120 -0
- package/src/native-harness/src/interfaces/protocol/output.ts +59 -0
- package/src/native-harness/src/interfaces/protocol/server.ts +519 -0
- package/src/native-harness/src/interfaces/protocol/stdio-client.ts +126 -0
- package/src/native-harness/src/interfaces/protocol/stdio-server.ts +82 -0
- package/src/native-harness/src/interfaces/protocol/subscribe.ts +41 -0
- package/src/native-harness/src/interfaces/protocol/transfers.ts +146 -0
- package/src/native-harness/src/interfaces/web-chat/index.html +94 -0
- package/src/native-harness/src/interfaces/web-chat/server.ts +34 -0
- package/src/native-harness/src/kernel/channels.ts +409 -0
- package/src/native-harness/src/kernel/loop.ts +45 -0
- package/src/native-harness/src/runtime/config/development.ts +23 -0
- package/src/native-harness/src/runtime/config/managed.ts +2 -0
- package/src/native-harness/src/runtime/config/paths.ts +33 -0
- package/src/native-harness/src/runtime/reminder-timer.ts +42 -0
- package/src/native-harness/src/runtime/reminder-validation.ts +125 -0
- package/src/native-harness/src/runtime/reminders.ts +66 -0
- package/src/native-harness/src/state/storage/archive.ts +257 -0
- package/src/native-harness/src/state/storage/engine.ts +1204 -0
- package/src/native-harness/src/state/storage/inference.ts +124 -0
- package/src/native-harness/src/state/storage/memory-reminders.ts +103 -0
- package/src/native-harness/src/state/storage/memory.ts +354 -0
- package/src/native-harness/src/state/storage/policy-metadata.ts +32 -0
- package/src/native-harness/src/state/storage/reminder-archive.ts +104 -0
- package/src/native-harness/src/state/storage/reminder-change.ts +138 -0
- package/src/native-harness/src/state/storage/reminder-tables.ts +35 -0
- package/src/native-harness/src/state/storage/retrieval.ts +122 -0
- package/src/native-harness/src/state/storage/routine-row.ts +25 -0
- package/src/native-harness/src/state/storage/script-identity.ts +7 -0
- package/src/native-harness/src/state/storage/session-file.ts +85 -0
- package/src/native-harness/src/state/storage/sqlite-reminders.ts +174 -0
- package/src/native-harness/src/state/storage/sqlite.ts +632 -0
- package/src/native-harness/src/testing.ts +190 -0
- package/src/native-harness/src/util/config.ts +66 -0
- package/src/native-harness/src/util/intake.ts +7 -0
- package/src/native-harness/src/util/json.ts +41 -0
- package/src/native-harness/src/util/message-position.ts +3 -0
- package/src/native-harness/src/util/reminder-schedule.ts +44 -0
- package/src/native-harness/src/util/schema.ts +40 -0
- package/src/native-harness/src/util/timeline-text.ts +72 -0
- package/src/native-harness/src/util/timeline-tool-names.ts +3 -0
- package/src/native-harness/tsconfig.json +20 -0
- package/src/observation/agent-event-bus.ts +44 -18
- package/src/observation/index.ts +1 -0
- package/src/operations/contracts.ts +11 -3
- package/src/operations/errors.ts +5 -0
- package/src/operations/stale-policy.ts +1 -1
- package/src/operations/supervisor.ts +34 -10
- package/src/orchestrator/acp-delivery.ts +68 -0
- package/src/orchestrator/assignments.ts +27 -0
- package/src/orchestrator/busy.ts +15 -0
- package/src/orchestrator/config.ts +41 -0
- package/src/orchestrator/consumer-api.ts +24 -0
- package/src/orchestrator/contracts.ts +71 -0
- package/src/orchestrator/durable-assignments.ts +111 -0
- package/src/orchestrator/native-delegation.ts +103 -0
- package/src/orchestrator/orchestrator.ts +318 -0
- package/src/orchestrator/permissions.ts +28 -0
- package/src/orchestrator/policy.ts +44 -0
- package/src/orchestrator/pool.ts +123 -0
- package/src/orchestrator/tool-descriptions.ts +11 -0
- package/src/permissions/acp.ts +18 -0
- package/src/permissions/arrival.ts +13 -0
- package/src/permissions/consumer-config.ts +19 -0
- package/src/permissions/conversation.ts +288 -0
- package/src/permissions/matcher.ts +87 -0
- package/src/permissions/outcome.ts +26 -0
- package/src/permissions/policy.ts +86 -0
- package/src/permissions/request.ts +26 -0
- package/src/persistence/activity-projection.ts +1 -0
- package/src/persistence/backend.ts +34 -1
- package/src/persistence/channels.ts +23 -0
- package/src/persistence/index.ts +4 -0
- package/src/persistence/protocol-method.ts +5 -0
- package/src/persistence/reminder-wake.ts +49 -0
- package/src/persistence/reminders.ts +385 -0
- package/src/persistence/sqlite-backend.ts +84 -6
- package/src/persistence/sqlite-channels.ts +65 -0
- package/src/persistence/sqlite-reminders.ts +85 -0
- package/src/persistence/sqlite-schema.ts +108 -3
- package/src/persistence/sqlite-worker-delivery.ts +374 -0
- package/src/persistence/sqlite-workers.ts +59 -0
- package/src/persistence/worker-delivery-contract.ts +104 -0
- package/src/persistence/worker-terminal-reason.ts +29 -0
- package/src/persistence/workers.ts +19 -0
- package/src/provider-notices.ts +137 -0
- package/src/repl/commands.ts +2 -2
- package/src/steering/combine.ts +3 -1
- package/src/steering/contracts.ts +11 -2
- package/src/steering/controller.ts +3 -2
- package/src/terminals.ts +9 -6
- package/src/transport/acp-transport.ts +24 -14
- package/src/types.ts +15 -12
- package/src/updates.ts +14 -2
- package/src/wire-log.ts +1 -1
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { permissionArrival } from "../permissions/arrival.ts";
|
|
2
|
+
import type { ChannelEvent } from "../native-harness/src/client.ts";
|
|
3
|
+
import type { Logger } from "../logger.ts";
|
|
4
|
+
import { cancelledPermission, nativeConversationStopping, nativePermissionRequests, refuseNativeOnClose, type NativePermission, type PermissionResolution } from "../permissions/conversation.ts";
|
|
5
|
+
import { unansweredCancellation } from "../permissions/outcome.ts";
|
|
6
|
+
import { recordingPermissionHandlers } from "../permissions/policy.ts";
|
|
7
|
+
import type { PermissionRequest, PromptHandlers } from "../types.ts";
|
|
8
|
+
import type { NativeApprovalRequest, NativeApprovalResponder } from "./host.ts";
|
|
9
|
+
|
|
10
|
+
type ApprovalEvent = Extract<ChannelEvent["event"], { type: "approval_requested" }>;
|
|
11
|
+
type CloseEvent = Extract<ChannelEvent["event"], { type: "approval_closed" }>;
|
|
12
|
+
type SendApproval = (decision: "allow_once" | "deny") => Promise<boolean>;
|
|
13
|
+
const allow = { outcome: "selected", optionId: "allow_once" } as const;
|
|
14
|
+
const deny = { outcome: "selected", optionId: "deny" } as const;
|
|
15
|
+
|
|
16
|
+
class Approval implements NativePermission {
|
|
17
|
+
readonly arrival = permissionArrival();
|
|
18
|
+
readonly resolution: Promise<PermissionResolution>;
|
|
19
|
+
private resolve!: (result: PermissionResolution) => void;
|
|
20
|
+
private readonly abort = new AbortController();
|
|
21
|
+
private inFlight = false;
|
|
22
|
+
private observedAnswer?: PermissionResolution;
|
|
23
|
+
private started = false;
|
|
24
|
+
private cancelTurn?: () => void;
|
|
25
|
+
|
|
26
|
+
onCancelTurn(listener: () => void): void { this.cancelTurn = listener; }
|
|
27
|
+
|
|
28
|
+
constructor(
|
|
29
|
+
readonly request: NativeApprovalRequest,
|
|
30
|
+
readonly hostHandled: boolean,
|
|
31
|
+
private readonly send: SendApproval,
|
|
32
|
+
private readonly interrupt?: () => Promise<boolean>,
|
|
33
|
+
) {
|
|
34
|
+
this.resolution = new Promise(resolve => { this.resolve = resolve; });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
get signal(): AbortSignal { return this.abort.signal; }
|
|
38
|
+
get settled(): boolean { return this.abort.signal.aborted; }
|
|
39
|
+
get hasWireOutcome(): boolean { return this.settled || this.observedAnswer !== undefined; }
|
|
40
|
+
|
|
41
|
+
start(responder: NativeApprovalResponder, log: Logger): void {
|
|
42
|
+
if (this.started) return;
|
|
43
|
+
this.started = true;
|
|
44
|
+
void Promise.resolve().then(() => {
|
|
45
|
+
if (!this.abort.signal.aborted) {
|
|
46
|
+
return responder(this.request, decision => this.answer({ outcome: decision === "allow_once" ? allow : deny, by: "handler" }), this.abort.signal);
|
|
47
|
+
}
|
|
48
|
+
}).catch(error => log.warn("native_approval_responder_failed", { requestId: this.request.requestId, error: String(error) }));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async answer(resolution: PermissionResolution): Promise<boolean> {
|
|
52
|
+
if (this.inFlight || this.abort.signal.aborted) return false;
|
|
53
|
+
this.inFlight = true;
|
|
54
|
+
if (resolution.outcome === unansweredCancellation && this.interrupt) {
|
|
55
|
+
this.observedAnswer = resolution;
|
|
56
|
+
try {
|
|
57
|
+
this.cancelTurn?.();
|
|
58
|
+
const accepted = await this.interrupt();
|
|
59
|
+
this.finish(accepted ? resolution : cancelledPermission);
|
|
60
|
+
return accepted;
|
|
61
|
+
} finally {
|
|
62
|
+
this.finish(cancelledPermission);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const decision = resolution.outcome.outcome === "selected" && resolution.outcome.optionId === "allow_once" ? allow : deny;
|
|
66
|
+
try {
|
|
67
|
+
const accepted = await this.send(decision.optionId);
|
|
68
|
+
if (this.abort.signal.aborted) return false;
|
|
69
|
+
this.finish(accepted ? { ...resolution, outcome: decision } : this.observedAnswer ?? cancelledPermission);
|
|
70
|
+
return accepted;
|
|
71
|
+
} finally {
|
|
72
|
+
if (!this.abort.signal.aborted) this.finish(this.observedAnswer ?? cancelledPermission);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
closed(reason: CloseEvent["reason"]): void {
|
|
77
|
+
if (reason === "approval_denied" || reason === "dispatched") {
|
|
78
|
+
// The harness can emit the effect before replying to approval/respond.
|
|
79
|
+
this.observedAnswer = { outcome: reason === "dispatched" ? allow : deny, by: "consumer" };
|
|
80
|
+
if (!this.inFlight) this.finish(this.observedAnswer);
|
|
81
|
+
} else if (reason === "approval_timeout") {
|
|
82
|
+
this.finish({ outcome: deny, by: "timeout" });
|
|
83
|
+
} else if (this.observedAnswer?.outcome !== unansweredCancellation) {
|
|
84
|
+
this.finish(cancelledPermission);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
endTurn(): void {
|
|
89
|
+
if (!this.inFlight || !this.observedAnswer) this.finish(cancelledPermission);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
finish(result: PermissionResolution): void {
|
|
93
|
+
if (this.abort.signal.aborted) return;
|
|
94
|
+
this.abort.abort();
|
|
95
|
+
this.cancelTurn = undefined;
|
|
96
|
+
this.resolve(result);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Attach the same ticket to live and observed copies. Held events keep their ticket after turn cleanup. */
|
|
101
|
+
export class NativeApprovals {
|
|
102
|
+
private readonly active = new Map<string, Approval>();
|
|
103
|
+
get busy(): boolean { return [...this.active.values()].some(ticket => !ticket.settled); }
|
|
104
|
+
private readonly events = new WeakMap<ApprovalEvent, Approval>();
|
|
105
|
+
|
|
106
|
+
constructor(private readonly responder: NativeApprovalResponder | undefined, private readonly log: Logger) {}
|
|
107
|
+
|
|
108
|
+
observe(channel: string, event: ApprovalEvent, send: SendApproval, interrupt?: () => Promise<boolean>): void {
|
|
109
|
+
let ticket = this.events.get(event) ?? this.active.get(event.requestId);
|
|
110
|
+
if (!ticket) {
|
|
111
|
+
const { type: _type, ...request } = event;
|
|
112
|
+
ticket = new Approval({ ...request, channel }, this.responder !== undefined, send, interrupt);
|
|
113
|
+
this.active.set(event.requestId, ticket);
|
|
114
|
+
}
|
|
115
|
+
this.events.set(event, ticket);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
start(event: ApprovalEvent): void {
|
|
119
|
+
if (this.responder) this.events.get(event)?.start(this.responder, this.log);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
ticket(event: ApprovalEvent): NativePermission | undefined { return this.events.get(event); }
|
|
123
|
+
|
|
124
|
+
closed(event: CloseEvent): void { this.active.get(event.requestId)?.closed(event.reason); }
|
|
125
|
+
|
|
126
|
+
end(channel: string, turnId: string): void {
|
|
127
|
+
for (const [id, ticket] of this.active) {
|
|
128
|
+
if (ticket.request.channel !== channel || ticket.request.turnId !== turnId) continue;
|
|
129
|
+
this.active.delete(id);
|
|
130
|
+
ticket.endTurn();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
disconnected(): void {
|
|
135
|
+
for (const ticket of this.active.values()) ticket.finish(cancelledPermission);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
stop(): void {
|
|
139
|
+
this.disconnected();
|
|
140
|
+
this.active.clear();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const APPROVAL_OPTIONS: PermissionRequest["options"] = [
|
|
145
|
+
{ optionId: "allow_once", name: "Allow once", kind: "allow_once" },
|
|
146
|
+
{ optionId: "deny", name: "Deny", kind: "reject_once" },
|
|
147
|
+
];
|
|
148
|
+
|
|
149
|
+
export async function runNativeApproval(
|
|
150
|
+
ticket: NativePermission, event: ApprovalEvent, handlers: PromptHandlers, cancelled: AbortSignal, log: Logger,
|
|
151
|
+
): Promise<void> {
|
|
152
|
+
const request: PermissionRequest = {
|
|
153
|
+
toolCallId: event.toolCallId, title: event.tool, options: APPROVAL_OPTIONS.map(option => ({ ...option })),
|
|
154
|
+
tool: { name: event.tool, rawInput: event.arguments }, raw: event,
|
|
155
|
+
};
|
|
156
|
+
nativePermissionRequests.set(request, ticket);
|
|
157
|
+
const handler = handlers.onPermissionRequest;
|
|
158
|
+
const recording = handler && recordingPermissionHandlers.has(handler) ? handler : undefined;
|
|
159
|
+
const close = () => refuseNativeOnClose(ticket, cancelled.reason === nativeConversationStopping, () => log);
|
|
160
|
+
if (!recording) {
|
|
161
|
+
if (cancelled.aborted) close();
|
|
162
|
+
else cancelled.addEventListener("abort", close, { once: true });
|
|
163
|
+
}
|
|
164
|
+
try {
|
|
165
|
+
if (recording) {
|
|
166
|
+
await recording(request, cancelled);
|
|
167
|
+
} else if (ticket.hostHandled) {
|
|
168
|
+
await ticket.resolution;
|
|
169
|
+
} else if (!cancelled.aborted) {
|
|
170
|
+
const signal = AbortSignal.any([cancelled, ticket.signal]);
|
|
171
|
+
if (signal.aborted) return;
|
|
172
|
+
const answer = handler ? await Promise.race([
|
|
173
|
+
Promise.resolve().then(() => handler.call(handlers, request, signal)),
|
|
174
|
+
ticket.resolution.then(result => result.outcome),
|
|
175
|
+
]) : deny;
|
|
176
|
+
if (!signal.aborted) await ticket.answer({ outcome: answer, by: handler ? "handler" : "default" });
|
|
177
|
+
}
|
|
178
|
+
} catch (error) {
|
|
179
|
+
log.warn("native_approval_handler_failed", { requestId: event.requestId, error: String(error) });
|
|
180
|
+
if (!ticket.hostHandled && !cancelled.aborted && !recording) {
|
|
181
|
+
await ticket.answer({ outcome: deny, by: "handler" }).catch(() => false);
|
|
182
|
+
}
|
|
183
|
+
} finally {
|
|
184
|
+
cancelled.removeEventListener("abort", close);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ConversationCapabilities } from "../conversation-controller/capabilities.ts";
|
|
2
|
+
|
|
3
|
+
export function nativeConversationCapabilities(reminders = true, routines = false): ConversationCapabilities {
|
|
4
|
+
return {
|
|
5
|
+
nativeSteering: true,
|
|
6
|
+
reminders,
|
|
7
|
+
...(routines ? { routines } : {}),
|
|
8
|
+
// The harness times out model calls, approvals and tools itself and reports the outcome.
|
|
9
|
+
supervision: "backend",
|
|
10
|
+
freshContext: false,
|
|
11
|
+
backend: {
|
|
12
|
+
loadSession: false, forkSession: false, listSessions: false,
|
|
13
|
+
media: { image: false, audio: false, file: false },
|
|
14
|
+
mcpServers: { stdio: false, http: false, sse: false },
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import type { Logger } from "../logger.ts";
|
|
2
|
+
import type { ChannelDesire, ChannelPersistence, PendingChange } from "../persistence/channels.ts";
|
|
3
|
+
import type { ChannelChangeResult, ChannelStatusResult, HarnessClient, PendingChannelChange } from "../native-harness/src/client.ts";
|
|
4
|
+
import { ambiguous, codeOf, HarnessUnavailableError, pendingRetirementError } from "./errors.ts";
|
|
5
|
+
import type { NativeHostRecords } from "./records.ts";
|
|
6
|
+
|
|
7
|
+
export type ChannelChangeOutcome =
|
|
8
|
+
| { readonly state: "applied" }
|
|
9
|
+
/** Stored for another delivery attempt. A change never starts the harness. */
|
|
10
|
+
| { readonly state: "pending" }
|
|
11
|
+
| { readonly state: "blocked"; readonly reason: NonNullable<ChannelChangeResult["reason"]> };
|
|
12
|
+
|
|
13
|
+
export function channelStore(records: NativeHostRecords): ChannelPersistence {
|
|
14
|
+
return {
|
|
15
|
+
desire: async (channel, desire) => records.desire(channel, desire),
|
|
16
|
+
pendingChange: async (channel) => records.pendingChange(channel),
|
|
17
|
+
pendingChanges: async () => records.pendingChanges(),
|
|
18
|
+
confirmChange: async (channel, version) => records.confirmChange(channel, version),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class NativeChannelChanges {
|
|
23
|
+
readonly #queues = new Map<string, Promise<unknown>>();
|
|
24
|
+
readonly #blockedVersions = new Map<string, number>();
|
|
25
|
+
readonly #writes = new Set<Promise<unknown>>();
|
|
26
|
+
#revision = 0;
|
|
27
|
+
get busy(): boolean { return this.#queues.size > 0 || this.#writes.size > 0; }
|
|
28
|
+
|
|
29
|
+
constructor(
|
|
30
|
+
private readonly store: ChannelPersistence,
|
|
31
|
+
private readonly channels: Map<string, ChannelStatusResult>,
|
|
32
|
+
private readonly client: () => HarnessClient,
|
|
33
|
+
private readonly log: Logger,
|
|
34
|
+
private readonly onBlocked?: (result: ChannelChangeResult) => void,
|
|
35
|
+
) {}
|
|
36
|
+
|
|
37
|
+
pendingChange(channel: string): Promise<PendingChange | undefined> { return this.store.pendingChange(channel); }
|
|
38
|
+
|
|
39
|
+
pendingChanges(): Promise<PendingChange[]> { return this.store.pendingChanges(); }
|
|
40
|
+
|
|
41
|
+
async resume(channel: string) {
|
|
42
|
+
const client = this.client();
|
|
43
|
+
return this.enqueue(channel, async () => {
|
|
44
|
+
const pending = await this.store.pendingChange(channel);
|
|
45
|
+
if (pending?.desire.lifecycle === "retired") throw pendingRetirementError(channel);
|
|
46
|
+
return client.resumeChannel({ channel });
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Per channel, order live edits and pushes before any repository or network wait. */
|
|
51
|
+
enqueue<T>(channel: string, work: () => Promise<T>): Promise<T> {
|
|
52
|
+
const previous = this.#queues.get(channel) ?? Promise.resolve();
|
|
53
|
+
const next = previous.then(work);
|
|
54
|
+
const tail = next.catch(() => undefined);
|
|
55
|
+
this.#queues.set(channel, tail);
|
|
56
|
+
void tail.then(() => { if (this.#queues.get(channel) === tail) this.#queues.delete(channel); });
|
|
57
|
+
return next;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async change(channel: string, desire: ChannelDesire, running: boolean): Promise<ChannelChangeOutcome> {
|
|
61
|
+
// Start persisting immediately, even if an older RPC is outstanding. That RPC's confirmation
|
|
62
|
+
// must see the new version. Register delivery synchronously so a subsequent push queues behind it.
|
|
63
|
+
const lifecycle = desire.lifecycle;
|
|
64
|
+
const write = this.store.desire(channel, desire);
|
|
65
|
+
this.#trackWrite(write);
|
|
66
|
+
const deliver = async (): Promise<ChannelChangeOutcome> => {
|
|
67
|
+
const committed = await write;
|
|
68
|
+
if ((lifecycle === "suspended" || lifecycle === "open") && committed.desire.lifecycle === "retired") {
|
|
69
|
+
return { state: "blocked", reason: "channel_retired" };
|
|
70
|
+
}
|
|
71
|
+
return running ? this.#flushChange(channel) : { state: "pending" };
|
|
72
|
+
};
|
|
73
|
+
return running ? this.enqueue(channel, deliver) : deliver();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async deliverCommitted<T>(channel: string, commit: Promise<T>, running: () => boolean, deliver: (result: T) => boolean): Promise<T> {
|
|
77
|
+
this.#trackWrite(commit);
|
|
78
|
+
if (!running()) return commit;
|
|
79
|
+
return this.enqueue(channel, async () => {
|
|
80
|
+
const result = await commit;
|
|
81
|
+
if (running() && deliver(result)) await this.#deliverPending(channel);
|
|
82
|
+
return result;
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
#trackWrite(write: Promise<unknown>): void {
|
|
87
|
+
this.#revision++;
|
|
88
|
+
this.#writes.add(write);
|
|
89
|
+
const finished = () => { this.#writes.delete(write); };
|
|
90
|
+
void write.then(finished, finished);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
startupBatch(signal: AbortSignal): Promise<PendingChannelChange[]> {
|
|
94
|
+
return this.#scan(signal, (changes) => changes.map(({ channel, version, desire }) => ({
|
|
95
|
+
channel, changeId: String(version), ...desire,
|
|
96
|
+
})));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async acknowledgeOpen(results: readonly ChannelChangeResult[], signal: AbortSignal): Promise<void> {
|
|
100
|
+
this.#blockedVersions.clear();
|
|
101
|
+
for (const result of results) {
|
|
102
|
+
signal.throwIfAborted();
|
|
103
|
+
const version = Number(result.changeId);
|
|
104
|
+
if (result.outcome === "applied") await this.store.confirmChange(result.channel, version);
|
|
105
|
+
else {
|
|
106
|
+
this.#blockedVersions.set(result.channel, version);
|
|
107
|
+
this.log.warn("native_channel_change_blocked", { ...result });
|
|
108
|
+
try { this.onBlocked?.(result); } catch { /* observer */ }
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Reserve delivery of post-cutoff edits before replaying submissions and publishing running. */
|
|
114
|
+
afterOpen(signal: AbortSignal, ready: () => void): Promise<void> {
|
|
115
|
+
return this.#scan(signal, (changes) => {
|
|
116
|
+
for (const change of changes) {
|
|
117
|
+
if (this.#blockedVersions.get(change.channel) === change.version) continue;
|
|
118
|
+
void this.enqueue(change.channel, () => this.#deliverPending(change.channel));
|
|
119
|
+
}
|
|
120
|
+
ready();
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async #deliverPending(channel: string): Promise<void> {
|
|
125
|
+
try {
|
|
126
|
+
await this.#flushChange(channel);
|
|
127
|
+
} catch (error) {
|
|
128
|
+
// The local commit is already authoritative. A failed delivery leaves the intent for retry.
|
|
129
|
+
this.log.warn("native_channel_change_delivery_failed", { channel, error: String(error) });
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async #scan<T>(signal: AbortSignal, use: (changes: PendingChange[]) => T): Promise<T> {
|
|
134
|
+
for (;;) {
|
|
135
|
+
signal.throwIfAborted();
|
|
136
|
+
const revision = this.#revision;
|
|
137
|
+
await Promise.allSettled(this.#writes);
|
|
138
|
+
signal.throwIfAborted();
|
|
139
|
+
const changes = await this.store.pendingChanges();
|
|
140
|
+
signal.throwIfAborted();
|
|
141
|
+
// A write can commit after the read's snapshot. Repeat the scan if an edit started during it,
|
|
142
|
+
// including one that has already committed. The final scheduling happens in this same job.
|
|
143
|
+
if (revision === this.#revision) return use(changes);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async #flushChange(channel: string): Promise<ChannelChangeOutcome> {
|
|
148
|
+
for (;;) {
|
|
149
|
+
const pending = await this.store.pendingChange(channel);
|
|
150
|
+
if (!pending) return { state: "applied" };
|
|
151
|
+
const outcome = await this.#applyLive(pending);
|
|
152
|
+
if (outcome.state !== "applied") {
|
|
153
|
+
if (outcome.state === "blocked") this.#blockedVersions.set(channel, pending.version);
|
|
154
|
+
return outcome;
|
|
155
|
+
}
|
|
156
|
+
// An old acknowledgment can't clear an edit that superseded it while the RPC was in flight.
|
|
157
|
+
if (await this.store.confirmChange(channel, pending.version)) return outcome;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async #applyLive(change: PendingChange): Promise<ChannelChangeOutcome> {
|
|
162
|
+
const { channel, desire } = change;
|
|
163
|
+
const seen = (result: { channel: ChannelStatusResult }) => { this.channels.set(channel, result.channel); };
|
|
164
|
+
const known = this.channels.get(channel);
|
|
165
|
+
const blocked = (reason: NonNullable<ChannelChangeResult["reason"]>): ChannelChangeOutcome => {
|
|
166
|
+
const result: ChannelChangeResult = { channel, changeId: String(change.version), outcome: "blocked", reason };
|
|
167
|
+
try { this.onBlocked?.(result); } catch { /* observer */ }
|
|
168
|
+
return { state: "blocked", reason };
|
|
169
|
+
};
|
|
170
|
+
try {
|
|
171
|
+
const client = this.client();
|
|
172
|
+
if (known?.lifecycle === "retired") return desire.lifecycle === "retired" ? { state: "applied" } : blocked("channel_retired");
|
|
173
|
+
if (desire.lifecycle === "retired") {
|
|
174
|
+
if (!known) seen(await client.createChannel({ channel }));
|
|
175
|
+
try {
|
|
176
|
+
seen(await client.deleteChannel({ channel }));
|
|
177
|
+
} catch (error) {
|
|
178
|
+
if (codeOf(error) !== "channel_busy") throw error;
|
|
179
|
+
// The same rule as at open: pending work must neither run nor be lost.
|
|
180
|
+
seen(await client.suspendChannel({ channel }));
|
|
181
|
+
return blocked("channel_busy");
|
|
182
|
+
}
|
|
183
|
+
return { state: "applied" };
|
|
184
|
+
}
|
|
185
|
+
if (!known) {
|
|
186
|
+
seen(await client.createChannel({ channel,
|
|
187
|
+
...(typeof desire.displayName === "string" ? { displayName: desire.displayName } : {}),
|
|
188
|
+
...(typeof desire.policy === "string" ? { policy: desire.policy } : {}) }));
|
|
189
|
+
} else if (desire.displayName !== undefined || desire.policy !== undefined) {
|
|
190
|
+
// The edit replaces the name, so a change that omits it restates the current one.
|
|
191
|
+
seen(await client.editChannel({ channel,
|
|
192
|
+
displayName: desire.displayName === undefined ? known.displayName ?? null : desire.displayName,
|
|
193
|
+
...(desire.policy === undefined ? {} : { policy: desire.policy }) }));
|
|
194
|
+
}
|
|
195
|
+
if (desire.lifecycle === "suspended") seen(await client.suspendChannel({ channel }));
|
|
196
|
+
if (desire.lifecycle === "open") {
|
|
197
|
+
try { seen(await client.resumeChannel({ channel })); }
|
|
198
|
+
catch (error) {
|
|
199
|
+
if (codeOf(error) !== "incompatible") throw error;
|
|
200
|
+
return blocked("incompatible");
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return { state: "applied" };
|
|
204
|
+
} catch (error) {
|
|
205
|
+
if (codeOf(error) === "channel_retired") return blocked("channel_retired");
|
|
206
|
+
// Unknown outcome or no connection: the change stays pending and goes with the next open.
|
|
207
|
+
if (ambiguous(error) || error instanceof HarnessUnavailableError) return { state: "pending" };
|
|
208
|
+
throw error;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { separateReminderNotifications } from "./reminders/notifications.ts";
|
|
2
|
+
import type { ReminderNotification } from "./reminders/wire.ts";
|
|
3
|
+
import { HarnessClient, createStdioTransport } from "../native-harness/src/client.ts";
|
|
4
|
+
import { prepareNativeLaunch, onceDispose } from "../launcher.ts";
|
|
5
|
+
import type { Logger } from "../logger.ts";
|
|
6
|
+
import { HarnessUnavailableError } from "./errors.ts";
|
|
7
|
+
import type { NativeConnection, NativeHarnessHostConfig } from "./host.ts";
|
|
8
|
+
|
|
9
|
+
const HARNESS_MAIN = new URL("../native-harness/src/interfaces/cli/main.ts", import.meta.url).pathname;
|
|
10
|
+
|
|
11
|
+
export interface Deferred<T> { promise: Promise<T>; resolve(value: T): void; reject(error: unknown): void }
|
|
12
|
+
export function deferred<T>(): Deferred<T> {
|
|
13
|
+
let resolve!: (value: T) => void, reject!: (error: unknown) => void;
|
|
14
|
+
const promise = new Promise<T>((res, rej) => { resolve = res; reject = rej; });
|
|
15
|
+
promise.catch(() => undefined); // observed by whoever awaits it; never an unhandled rejection
|
|
16
|
+
return { promise, resolve, reject };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function launchNativeConnection(config: NativeHarnessHostConfig, log: Logger): Promise<NativeConnection> {
|
|
20
|
+
const command = config.command ?? [process.execPath, HARNESS_MAIN, "serve", "--stdio", "--home", config.home];
|
|
21
|
+
const launch = await prepareNativeLaunch(config.launcher, { command, env: config.env, cwd: config.cwd });
|
|
22
|
+
const dispose = onceDispose(launch.dispose);
|
|
23
|
+
let child: ReturnType<typeof Bun.spawn<"pipe", "pipe", "pipe">>;
|
|
24
|
+
try {
|
|
25
|
+
child = Bun.spawn(launch.command, { cwd: launch.spawnCwd, env: launch.spawnEnv, stdin: "pipe", stdout: "pipe", stderr: "pipe" });
|
|
26
|
+
} catch (error) {
|
|
27
|
+
await dispose();
|
|
28
|
+
throw new HarnessUnavailableError(`the native harness could not be started: ${String(error)}`, { cause: error, code: "start_failed" });
|
|
29
|
+
}
|
|
30
|
+
void (async () => {
|
|
31
|
+
// Diagnostics only: the protocol is on stdout.
|
|
32
|
+
const text = await new Response(child.stderr).text().catch(() => "");
|
|
33
|
+
if (text.trim()) log.warn("native_harness_stderr", { text: text.slice(-4000) });
|
|
34
|
+
})();
|
|
35
|
+
let notify: (event: ReminderNotification) => void = () => {};
|
|
36
|
+
const transport = createStdioTransport({
|
|
37
|
+
readable: separateReminderNotifications(child.stdout, event => notify(event)),
|
|
38
|
+
exited: child.exited,
|
|
39
|
+
async write(bytes) { child.stdin.write(bytes); await child.stdin.flush(); },
|
|
40
|
+
async end() { await child.stdin.end(); },
|
|
41
|
+
});
|
|
42
|
+
const closed = deferred<void>();
|
|
43
|
+
transport.onClose(() => closed.resolve());
|
|
44
|
+
void child.exited.then(() => { closed.resolve(); void dispose(); });
|
|
45
|
+
const client = new HarnessClient(transport, config.requestTimeoutMs ?? 30_000);
|
|
46
|
+
return {
|
|
47
|
+
client,
|
|
48
|
+
onReminderNotification: listener => { notify = listener; },
|
|
49
|
+
closed: closed.promise,
|
|
50
|
+
async kill() {
|
|
51
|
+
client.close(); // closes the harness's input: it stops, recording running turns interrupted
|
|
52
|
+
const forced = setTimeout(() => child.kill("SIGKILL"), 2000);
|
|
53
|
+
try { await child.exited; } finally { clearTimeout(forced); }
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|