@feltdb/feltkit 0.3.35 → 0.3.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/docs.d.ts +1 -1
- package/dist/docs.js +1 -0
- package/dist/docs.js.map +1 -1
- package/dist/runtime/agent-control-capabilities.d.ts +4 -0
- package/dist/runtime/agent-control-capabilities.js +66 -0
- package/dist/runtime/agent-control-capabilities.js.map +1 -0
- package/dist/runtime/agent-control.d.ts +173 -0
- package/dist/runtime/agent-control.js +405 -0
- package/dist/runtime/agent-control.js.map +1 -0
- package/dist/runtime/agent.d.ts +5 -0
- package/dist/runtime/agent.js +72 -1
- package/dist/runtime/agent.js.map +1 -1
- package/dist/runtime/context.d.ts +12 -0
- package/dist/runtime/contract.js +6 -1
- package/dist/runtime/contract.js.map +1 -1
- package/dist/runtime/runtime.js +22 -3
- package/dist/runtime/runtime.js.map +1 -1
- package/dist/server/index.js +87 -0
- package/dist/server/index.js.map +1 -1
- package/dist/server/mcp.d.ts +58 -3
- package/dist/server/mcp.js +7 -0
- package/dist/server/mcp.js.map +1 -1
- package/docs/AGENT-CONTROL.md +142 -0
- package/docs/LOCAL-AUTHORITY.md +5 -3
- package/docs/README.md +3 -2
- package/package.json +7 -7
- package/skills/.claude-plugin/marketplace.json +1 -1
- package/skills/.claude-plugin/plugin.json +1 -1
package/dist/runtime/agent.js
CHANGED
|
@@ -38,6 +38,7 @@ import { createHash, randomUUID } from "node:crypto";
|
|
|
38
38
|
import { FeltKitError, isFeltKitError } from "../errors.js";
|
|
39
39
|
import { authContext, toAppPortPrincipal } from "./authority.js";
|
|
40
40
|
import { readAll } from "./data.js";
|
|
41
|
+
import { AgentControl } from "./agent-control.js";
|
|
41
42
|
export const AGENT_NAME = "feltkit";
|
|
42
43
|
export const AGENT_VERSION = 1;
|
|
43
44
|
/** Permissions for using the agent itself. Actions still need their own permissions. */
|
|
@@ -183,12 +184,18 @@ export class AgentHost {
|
|
|
183
184
|
#legacy = null;
|
|
184
185
|
/** taskId → controllers of actions running now, for cancellation. */
|
|
185
186
|
#running = new Map();
|
|
187
|
+
/** taskId → actionIds dispatched in this process and not yet answered (observations included). */
|
|
188
|
+
#dispatched = new Map();
|
|
186
189
|
/** How long a consequential proposal can be approved (agent.proposalTtlMs; default 24 h). */
|
|
187
190
|
get #proposalTtlMs() {
|
|
188
191
|
return this.#runtime.app.options.agent?.proposalTtlMs ?? DEFAULT_PROPOSAL_TTL_MS;
|
|
189
192
|
}
|
|
190
193
|
/** Generations this process is running: recovery never marks these interrupted. */
|
|
191
194
|
#liveGenerations = new Set();
|
|
195
|
+
/** taskId → generationId → its controller, so an interrupt can cancel a running generation. */
|
|
196
|
+
#generationAborts = new Map();
|
|
197
|
+
/** People steering running tasks: updates, pause, resume, interrupt (runtime/agent-control.ts). */
|
|
198
|
+
control;
|
|
192
199
|
/** taskId → actionId → the approval running it in this process. */
|
|
193
200
|
#inflight = new Map();
|
|
194
201
|
/** Decision tasks started after a commit (ctx.agent.start), until their first decision is recorded. */
|
|
@@ -203,6 +210,42 @@ export class AgentHost {
|
|
|
203
210
|
this.#hooks = options.hooks ?? {};
|
|
204
211
|
this.#timeoutMs = options.actionTimeoutMs ?? ACTION_TIMEOUT_MS;
|
|
205
212
|
this.#separateApprover = options.separateApprover ?? false;
|
|
213
|
+
this.control = new AgentControl({
|
|
214
|
+
state: () => this.#state,
|
|
215
|
+
memories: (taskId) => this.#taskMemories(taskId),
|
|
216
|
+
loaded: async (taskId, p) => {
|
|
217
|
+
this.#require(p, AGENT_PERMISSIONS.write);
|
|
218
|
+
await this.#inScope(taskId, p);
|
|
219
|
+
return this.#loaded(taskId);
|
|
220
|
+
},
|
|
221
|
+
find: async (taskId, p) => {
|
|
222
|
+
this.#read(p);
|
|
223
|
+
await this.#inScope(taskId, p);
|
|
224
|
+
const found = await this.#state.getTask(taskId);
|
|
225
|
+
if (!found || found.task.scope?.project !== this.#runtime.app.name)
|
|
226
|
+
return null;
|
|
227
|
+
// Anyone but Studio, the owner, or a person sees no one else's task.
|
|
228
|
+
if (p !== "admin" && p.type !== "user" && found.task.owner !== actorOf(p))
|
|
229
|
+
return null;
|
|
230
|
+
return found;
|
|
231
|
+
},
|
|
232
|
+
inFlight: async (taskId) => ({
|
|
233
|
+
generations: (await this.#generationHeads(taskId)).filter((h) => OPEN_GENERATION.includes(h.record.state)).map((h) => h.record.generationId),
|
|
234
|
+
actions: [...new Set([...(await this.#heads(taskId)).filter((h) => h.record.state === "approved" || h.record.state === "executing").map((h) => h.record.actionId), ...(this.#dispatched.get(taskId) ?? [])])],
|
|
235
|
+
}),
|
|
236
|
+
abortGenerations: (taskId, reason) => {
|
|
237
|
+
const running = this.#generationAborts.get(taskId);
|
|
238
|
+
let n = 0;
|
|
239
|
+
for (const controller of running?.values() ?? []) {
|
|
240
|
+
if (!controller.signal.aborted) {
|
|
241
|
+
controller.abort(new Error(reason));
|
|
242
|
+
n += 1;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return n;
|
|
246
|
+
},
|
|
247
|
+
activity: (name, p, taskId, outcome, detail) => this.#activity(name, p, taskId, outcome, detail),
|
|
248
|
+
});
|
|
206
249
|
}
|
|
207
250
|
/** Open agent state and define the agent. The runtime reconciles open actions once it is ready (outcome hooks are function calls). */
|
|
208
251
|
async open() {
|
|
@@ -438,6 +481,10 @@ export class AgentHost {
|
|
|
438
481
|
await this.#inScope(taskId, p);
|
|
439
482
|
const { task } = await this.#owned(taskId, p);
|
|
440
483
|
const tool = this.#catalog().find((t) => t.name === proposal.capability);
|
|
484
|
+
// A retried proposal returns the first one below; a new one is new work and passes the gate.
|
|
485
|
+
const retried = proposal.idempotencyKey ? `act-${createHash("sha256").update(`${taskId}\u0000${proposal.idempotencyKey}`).digest("hex").slice(0, 20)}` : null;
|
|
486
|
+
if (!retried || !(await this.#heads(taskId)).some((h) => h.record.actionId === retried))
|
|
487
|
+
await this.control.gate(taskId, "the proposal");
|
|
441
488
|
if (!tool)
|
|
442
489
|
throw new FeltKitError("NOT_FOUND", `"${proposal.capability}" is not a capability of this application or an enabled service.`);
|
|
443
490
|
const input = proposal.input ?? {};
|
|
@@ -544,7 +591,13 @@ export class AgentHost {
|
|
|
544
591
|
const p = this.#require(principal, AGENT_PERMISSIONS.write);
|
|
545
592
|
await this.#inScope(taskId, p);
|
|
546
593
|
const { task } = await this.#owned(taskId, p);
|
|
594
|
+
// The safe boundary: no new generation while paused, interrupted, or holding an undelivered update.
|
|
595
|
+
await this.control.gate(taskId, "the generation");
|
|
547
596
|
const generationId = `gen-${randomUUID().replace(/-/g, "").slice(0, 16)}`;
|
|
597
|
+
const abort = new AbortController();
|
|
598
|
+
const aborts = this.#generationAborts.get(taskId) ?? new Map();
|
|
599
|
+
aborts.set(generationId, abort);
|
|
600
|
+
this.#generationAborts.set(taskId, aborts);
|
|
548
601
|
const at = new Date().toISOString();
|
|
549
602
|
const record = { generationId, taskId, state: "requested", seq: 1, requestedFor: actorOf(p), requestedAt: at, input, history: [{ state: "requested", at, by: actorOf(p) }] };
|
|
550
603
|
const memory = await this.#state.remember({ taskId, memoryId: `${taskId}.${generationId}.1`, kind: GENERATION_KIND, content: record, source: { actor: task.owner } });
|
|
@@ -567,12 +620,16 @@ export class AgentHost {
|
|
|
567
620
|
}
|
|
568
621
|
finally {
|
|
569
622
|
this.#liveGenerations.delete(generationId);
|
|
623
|
+
aborts.delete(generationId);
|
|
624
|
+
if (!aborts.size && this.#generationAborts.get(taskId) === aborts)
|
|
625
|
+
this.#generationAborts.delete(taskId);
|
|
570
626
|
}
|
|
571
627
|
};
|
|
572
628
|
const output = (text, toolCalls = []) => ({ text: text.slice(0, GENERATION_TEXT_LIMIT), ...(text.length > GENERATION_TEXT_LIMIT ? { truncated: text.length } : {}), toolCalls: toolCalls.map((c) => ({ id: c.id, name: c.name, input: redact(c.input) })) });
|
|
573
629
|
return {
|
|
574
630
|
generationId,
|
|
575
631
|
taskId,
|
|
632
|
+
signal: abort.signal,
|
|
576
633
|
running: async (model) => {
|
|
577
634
|
head = await this.#moveGeneration(head, "running", { model, startedAt: new Date().toISOString() }, task.owner);
|
|
578
635
|
},
|
|
@@ -723,6 +780,9 @@ export class AgentHost {
|
|
|
723
780
|
throw new FeltKitError("FORBIDDEN", "Only a person can approve a consequential action; agents and services cannot.");
|
|
724
781
|
const { task } = await this.#loaded(taskId);
|
|
725
782
|
const head = await this.#head(taskId, actionId);
|
|
783
|
+
// Approving runs the action now: not while a person has paused or interrupted the task.
|
|
784
|
+
if (head.record.state === "proposed")
|
|
785
|
+
await this.control.gateApproval(taskId);
|
|
726
786
|
// The approval is for what the person reviewed: a different operation or input is not approved.
|
|
727
787
|
if (reviewed?.inputDigest !== undefined && reviewed.inputDigest !== (head.record.inputDigest ?? actionDigest(head.record.capability, head.record.input))) {
|
|
728
788
|
throw new FeltKitError("FAILED_PRECONDITION", `The action you reviewed (${reviewed.inputDigest}) is not this proposal's operation and input; nothing was approved. Review it again.`, { details: { reviewed: reviewed.inputDigest, actual: head.record.inputDigest ?? null } });
|
|
@@ -997,6 +1057,7 @@ export class AgentHost {
|
|
|
997
1057
|
const open = (await this.#heads(taskId)).filter((h) => OPEN_STATES.includes(h.record.state));
|
|
998
1058
|
if (open.length)
|
|
999
1059
|
throw new FeltKitError("CONFLICT", `The task has open actions (${open.map((h) => `${h.record.actionId} ${h.record.state}`).join(", ")}); approve, reject, or settle them first.`);
|
|
1060
|
+
await this.control.gateComplete(taskId);
|
|
1000
1061
|
await this.#state.checkpoint(taskId, { actor: task.owner, summary: String(summary || "Completed.").slice(0, 4000), status: "completed", pendingWork: [] });
|
|
1001
1062
|
await this.#ensureActing(taskId);
|
|
1002
1063
|
const execution = await this.#execution(taskId);
|
|
@@ -1031,7 +1092,11 @@ export class AgentHost {
|
|
|
1031
1092
|
await this.#sync(taskId, task.owner, `Cancellation requested by ${p.id}; ${unknown.length} action(s) have an unknown outcome and must be settled first.`, { decisions: [`cancel requested by ${actorOf(p)}: ${reason}`], evidence });
|
|
1032
1093
|
throw new FeltKitError("CONFLICT", `Settle ${unknown.map((h) => h.record.actionId).join(", ")} (outcome unknown) before cancelling the task.`);
|
|
1033
1094
|
}
|
|
1034
|
-
|
|
1095
|
+
// Stop: no update is left open (FeltDB takes nothing after the terminal checkpoint).
|
|
1096
|
+
const abandoned = await this.control.abandonOpen(taskId, actorOf(p), reason);
|
|
1097
|
+
for (const c of this.#generationAborts.get(taskId)?.values() ?? [])
|
|
1098
|
+
c.abort(new Error(`stopped by ${actorOf(p)}`));
|
|
1099
|
+
await this.#state.checkpoint(taskId, { actor: task.owner, summary: `Cancelled by ${p.id}: ${reason}${abandoned ? ` (${abandoned} open update(s) abandoned)` : ""}`.slice(0, 4000), status: "cancelled", pendingWork: [], ...(evidence.length ? { evidence } : {}) });
|
|
1035
1100
|
const execution = await this.#execution(taskId);
|
|
1036
1101
|
if (!TERMINAL_EXECUTION.includes(execution.status))
|
|
1037
1102
|
await this.#db.cancelAgentExecution(execution, `cancelled by ${actorOf(p)}: ${reason}`.slice(0, 1000));
|
|
@@ -1333,6 +1398,9 @@ export class AgentHost {
|
|
|
1333
1398
|
const running = this.#running.get(taskId) ?? new Set();
|
|
1334
1399
|
running.add(controller);
|
|
1335
1400
|
this.#running.set(taskId, running);
|
|
1401
|
+
const dispatched = this.#dispatched.get(taskId) ?? new Set();
|
|
1402
|
+
dispatched.add(action.actionId);
|
|
1403
|
+
this.#dispatched.set(taskId, dispatched);
|
|
1336
1404
|
const attribution = { owner, ...(approver ? { approver } : {}) };
|
|
1337
1405
|
try {
|
|
1338
1406
|
// What runs is what was proposed and approved: a stored input that no longer matches its
|
|
@@ -1380,6 +1448,9 @@ export class AgentHost {
|
|
|
1380
1448
|
finally {
|
|
1381
1449
|
clearTimeout(timer);
|
|
1382
1450
|
running.delete(controller);
|
|
1451
|
+
dispatched.delete(action.actionId);
|
|
1452
|
+
if (!dispatched.size && this.#dispatched.get(taskId) === dispatched)
|
|
1453
|
+
this.#dispatched.delete(taskId);
|
|
1383
1454
|
}
|
|
1384
1455
|
}
|
|
1385
1456
|
/** A service observation through AppPort Services' invoke(), authorized as the task owner by its gateway. */
|