@feltdb/feltkit 0.3.12 → 0.3.14
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 -3
- package/dist/app.d.ts +27 -1
- package/dist/app.js +22 -0
- package/dist/app.js.map +1 -1
- package/dist/appboundry/build.js +10 -2
- package/dist/appboundry/build.js.map +1 -1
- package/dist/appboundry/host.js +2 -2
- package/dist/appboundry/host.js.map +1 -1
- package/dist/cli/deploy.js +9 -3
- package/dist/cli/deploy.js.map +1 -1
- package/dist/cli/main.d.ts +5 -0
- package/dist/cli/main.js +141 -40
- package/dist/cli/main.js.map +1 -1
- package/dist/client/index.d.ts +3 -0
- package/dist/client/index.js +3 -0
- package/dist/client/index.js.map +1 -1
- package/dist/dashboard/assets/{index-91I1zDK-.css → index-BXTNQAg1.css} +1 -1
- package/dist/dashboard/assets/index-DnIOVGF-.js +15 -0
- package/dist/dashboard/index.html +3 -3
- package/dist/docs.d.ts +12 -0
- package/dist/docs.js +43 -0
- package/dist/docs.js.map +1 -0
- package/dist/errors.d.ts +15 -0
- package/dist/errors.js +9 -0
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/platform/changes.js +4 -2
- package/dist/platform/changes.js.map +1 -1
- package/dist/platform/deployments.js +8 -6
- package/dist/platform/deployments.js.map +1 -1
- package/dist/runtime/activity.d.ts +25 -0
- package/dist/runtime/activity.js +38 -12
- package/dist/runtime/activity.js.map +1 -1
- package/dist/runtime/agent.d.ts +16 -0
- package/dist/runtime/agent.js +97 -30
- package/dist/runtime/agent.js.map +1 -1
- package/dist/runtime/backup-environment.d.ts +8 -1
- package/dist/runtime/backup-environment.js +44 -12
- package/dist/runtime/backup-environment.js.map +1 -1
- package/dist/runtime/backup.d.ts +45 -3
- package/dist/runtime/backup.js +237 -68
- package/dist/runtime/backup.js.map +1 -1
- package/dist/runtime/capabilities.js +4 -2
- package/dist/runtime/capabilities.js.map +1 -1
- package/dist/runtime/changes.js +2 -2
- package/dist/runtime/changes.js.map +1 -1
- package/dist/runtime/contract.d.ts +10 -0
- package/dist/runtime/contract.js +37 -15
- package/dist/runtime/contract.js.map +1 -1
- package/dist/runtime/data.d.ts +30 -3
- package/dist/runtime/data.js +136 -32
- package/dist/runtime/data.js.map +1 -1
- package/dist/runtime/decisions.d.ts +41 -3
- package/dist/runtime/decisions.js +159 -1
- package/dist/runtime/decisions.js.map +1 -1
- package/dist/runtime/integrations.js +14 -6
- package/dist/runtime/integrations.js.map +1 -1
- package/dist/runtime/loader.js +22 -2
- package/dist/runtime/loader.js.map +1 -1
- package/dist/runtime/readiness.js +6 -1
- package/dist/runtime/readiness.js.map +1 -1
- package/dist/runtime/runtime.d.ts +24 -6
- package/dist/runtime/runtime.js +77 -20
- package/dist/runtime/runtime.js.map +1 -1
- package/dist/runtime/services.d.ts +19 -5
- package/dist/runtime/services.js +14 -4
- package/dist/runtime/services.js.map +1 -1
- package/dist/runtime/sessions.js +4 -3
- package/dist/runtime/sessions.js.map +1 -1
- package/dist/runtime/state-contract.js +3 -3
- package/dist/runtime/state-contract.js.map +1 -1
- package/dist/runtime/targets.d.ts +6 -1
- package/dist/runtime/targets.js +8 -0
- package/dist/runtime/targets.js.map +1 -1
- package/dist/server/admin.d.ts +2 -2
- package/dist/server/admin.js +37 -12
- package/dist/server/admin.js.map +1 -1
- package/dist/server/index.d.ts +2 -1
- package/dist/server/index.js +45 -8
- package/dist/server/index.js.map +1 -1
- package/dist/server/mcp.js +1 -1
- package/dist/server/mcp.js.map +1 -1
- package/dist/testing/index.d.ts +6 -2
- package/dist/testing/index.js +28 -4
- package/dist/testing/index.js.map +1 -1
- package/docs/AGENT-OPERATIONS.md +515 -0
- package/docs/APPBOUNDRY.md +240 -0
- package/docs/ARCHITECTURE.md +126 -0
- package/docs/BACKUP.md +115 -0
- package/docs/GUARANTEES.md +460 -0
- package/docs/PATH-TO-PRODUCTION.md +117 -0
- package/docs/README.md +16 -0
- package/docs/RESOLVER.md +167 -0
- package/docs/SECURITY.md +182 -0
- package/docs/TRANSACTIONS.md +216 -0
- package/docs/UPSTREAM-GAPS.md +270 -0
- package/docs/WHY-FELTKIT.md +64 -0
- package/docs/assets/feltkit-logo-dark.png +0 -0
- package/docs/assets/feltkit-logo.png +0 -0
- package/package.json +11 -8
- package/skills/.claude-plugin/marketplace.json +1 -1
- package/skills/.claude-plugin/plugin.json +3 -3
- package/skills/feltkit-agent/SKILL.md +9 -5
- package/skills/feltkit-build/SKILL.md +10 -1
- package/skills/feltkit-data/SKILL.md +9 -3
- package/skills/feltkit-services/SKILL.md +8 -4
- package/dist/dashboard/assets/index-BhFOquA7.js +0 -15
- package/dist/dashboard/theme.js +0 -11
package/dist/runtime/activity.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { isFeltKitError } from "../errors.js";
|
|
2
|
-
import { newId } from "./data.js";
|
|
2
|
+
import { countAll, newId, readAll } from "./data.js";
|
|
3
|
+
/** Entries removed per transaction when the log is trimmed. */
|
|
4
|
+
const PRUNE_BATCH = 200;
|
|
3
5
|
export const ACTIVITY_COLLECTION = "_feltkit_activity";
|
|
4
6
|
export class ActivityLog {
|
|
5
7
|
#db;
|
|
@@ -10,11 +12,18 @@ export class ActivityLog {
|
|
|
10
12
|
#sincePrune = 0;
|
|
11
13
|
#queue = Promise.resolve();
|
|
12
14
|
lastError = null;
|
|
15
|
+
/**
|
|
16
|
+
* Retention: when the log was last trimmed to `retain`, and the last failure to trim it. A failed
|
|
17
|
+
* prune is retried on the next recorded entry until one succeeds, and readiness reports it, so a
|
|
18
|
+
* failure cannot quietly leave the log growing.
|
|
19
|
+
*/
|
|
20
|
+
retention = { retain: 0, lastPrunedAt: null, pruneError: null };
|
|
13
21
|
constructor(db, options) {
|
|
14
22
|
this.#db = db;
|
|
15
23
|
this.#evidence = options.evidence;
|
|
16
24
|
this.#mode = options.mode;
|
|
17
25
|
this.#retain = Math.max(100, options.retain ?? 2000);
|
|
26
|
+
this.retention.retain = this.#retain;
|
|
18
27
|
}
|
|
19
28
|
get mode() {
|
|
20
29
|
return this.#mode;
|
|
@@ -48,10 +57,9 @@ export class ActivityLog {
|
|
|
48
57
|
this.#queue = this.#queue
|
|
49
58
|
.then(async () => {
|
|
50
59
|
await this.#db.collection(ACTIVITY_COLLECTION).insert(full, full.id);
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
await this
|
|
54
|
-
}
|
|
60
|
+
// Every 100 entries, or on every entry while the last prune failed (until one succeeds).
|
|
61
|
+
if (++this.#sincePrune >= 100 || this.retention.pruneError)
|
|
62
|
+
await this.prune();
|
|
55
63
|
})
|
|
56
64
|
.catch((error) => {
|
|
57
65
|
this.lastError = { at: new Date().toISOString(), message: error.message };
|
|
@@ -95,13 +103,31 @@ export class ActivityLog {
|
|
|
95
103
|
const lookup = await this.#evidence(requestId);
|
|
96
104
|
return lookup.truncated ? [] : [...lookup.records];
|
|
97
105
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
106
|
+
/**
|
|
107
|
+
* Trim the log to the newest `retain` entries. Counted and read in FeltDB pages: entry ids start
|
|
108
|
+
* with their time, so FeltDB's id order is oldest first, and only the excess is read. Returns how
|
|
109
|
+
* many entries were removed. A failure is recorded in `retention` (and thrown), never ignored.
|
|
110
|
+
*/
|
|
111
|
+
async prune() {
|
|
112
|
+
try {
|
|
113
|
+
const total = await countAll(this.#db, { collection: ACTIVITY_COLLECTION });
|
|
114
|
+
let removed = 0;
|
|
115
|
+
if (total > this.#retain) {
|
|
116
|
+
const doomed = await readAll(this.#db, { collection: ACTIVITY_COLLECTION }, total - this.#retain);
|
|
117
|
+
for (let i = 0; i < doomed.length; i += PRUNE_BATCH) {
|
|
118
|
+
await this.#db.transaction({ operations: doomed.slice(i, i + PRUNE_BATCH).map((entry) => ({ collection: ACTIVITY_COLLECTION, id: entry.id })) });
|
|
119
|
+
removed += Math.min(PRUNE_BATCH, doomed.length - i);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
this.#sincePrune = 0;
|
|
123
|
+
this.retention.lastPrunedAt = new Date().toISOString();
|
|
124
|
+
this.retention.pruneError = null;
|
|
125
|
+
return removed;
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
this.retention.pruneError = { at: new Date().toISOString(), message: error?.message ?? String(error) };
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
105
131
|
}
|
|
106
132
|
}
|
|
107
133
|
export function errorSummary(error) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"activity.js","sourceRoot":"","sources":["../../src/runtime/activity.ts"],"names":[],"mappings":"AAeA,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAE9C,OAAO,EAAE,KAAK,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"activity.js","sourceRoot":"","sources":["../../src/runtime/activity.ts"],"names":[],"mappings":"AAeA,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAE9C,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAErD,+DAA+D;AAC/D,MAAM,WAAW,GAAG,GAAG,CAAC;AAGxB,MAAM,CAAC,MAAM,mBAAmB,GAAG,mBAAmB,CAAC;AAsDvD,MAAM,OAAO,WAAW;IACb,GAAG,CAAe;IAClB,KAAK,CAA8B;IACnC,OAAO,CAAS;IAChB,UAAU,GAAG,IAAI,GAAG,EAAoB,CAAC;IACzC,SAAS,CAAmD;IACrE,WAAW,GAAG,CAAC,CAAC;IAChB,MAAM,GAAkB,OAAO,CAAC,OAAO,EAAE,CAAC;IAC1C,SAAS,GAA2C,IAAI,CAAC;IACzD;;;;OAIG;IACM,SAAS,GAAwG,EAAE,MAAM,EAAE,CAAC,EAAE,YAAY,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC;IAE9K,YAAY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C,WAAW,GAAG,CAAC,CAAC;YACrB,IAAI,CAAC,SAAS,CAAC,YAAY,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;YACvD,IAAI,CAAC,SAAS,CAAC,UAAU,GAAG,IAAI,CAAC;YACjC,OAAO,OAAO,CAAC;QACjB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,SAAS,CAAC,UAAU,GAAG,EAAE,EAAE,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,EAAE,OAAO,EAAG,KAAe,EAAE,OAAO,IAAI,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YAClH,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;CACF;AAED,MAAM,UAAU,YAAY,CAAC,KAAc;IACzC,IAAI,cAAc,CAAC,KAAK,CAAC;QAAE,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC;IAC/E,MAAM,CAAC,GAAG,KAA8C,CAAC;IACzD,OAAO,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,UAAU,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;AAC1I,CAAC"}
|
package/dist/runtime/agent.d.ts
CHANGED
|
@@ -50,6 +50,14 @@ export interface AgentTool {
|
|
|
50
50
|
description?: string;
|
|
51
51
|
/** Whether the principal asking may perform it (a decision only; nothing runs). */
|
|
52
52
|
allowed: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* How this principal can use it: `"direct"` (call the tool), or `"propose"` (only through
|
|
55
|
+
* feltkit_agent_propose: an AppPort Services operation, which is not an MCP tool, or a
|
|
56
|
+
* capability that requires a person's approval). Absent when `allowed` is false.
|
|
57
|
+
*/
|
|
58
|
+
invoke?: "direct" | "propose";
|
|
59
|
+
/** `"required"`: a consequential call by this principal runs only after a person approves it. */
|
|
60
|
+
approval?: "required";
|
|
53
61
|
}
|
|
54
62
|
export interface ProposedAction {
|
|
55
63
|
actionId: string;
|
|
@@ -269,6 +277,14 @@ export declare class AgentHost {
|
|
|
269
277
|
* duplicate or concurrent approval of the same action never runs it twice.
|
|
270
278
|
*/
|
|
271
279
|
approve(taskId: string, actionId: string, approver: Principal | null): Promise<TaskView>;
|
|
280
|
+
/**
|
|
281
|
+
* For each proposed action of a task: whether `principal` may approve it now, and if not, why
|
|
282
|
+
* (the same checks approve() makes; nothing changes). Studio shows Approve only when it may.
|
|
283
|
+
*/
|
|
284
|
+
approvability(taskId: string, principal: Principal): Promise<Record<string, {
|
|
285
|
+
allowed: boolean;
|
|
286
|
+
reason?: string;
|
|
287
|
+
}>>;
|
|
272
288
|
/** Reject a proposed action. */
|
|
273
289
|
reject(taskId: string, actionId: string, principal: Principal | null, reason?: string): Promise<TaskView>;
|
|
274
290
|
/**
|
package/dist/runtime/agent.js
CHANGED
|
@@ -33,9 +33,11 @@
|
|
|
33
33
|
*/
|
|
34
34
|
import { FELTDB_AGENT_MEMORY_COLLECTION, openAgentState } from "@feltdb/core";
|
|
35
35
|
import { IDEMPOTENCY_OUTCOME_UNKNOWN, idempotencyErrorReason, isIdempotencyClaim } from "@appport/sdk";
|
|
36
|
+
import { grantsPermission } from "@appport/authorization";
|
|
36
37
|
import { createHash, randomUUID } from "node:crypto";
|
|
37
38
|
import { FeltKitError, isFeltKitError } from "../errors.js";
|
|
38
39
|
import { authContext } from "./authority.js";
|
|
40
|
+
import { readAll } from "./data.js";
|
|
39
41
|
export const AGENT_NAME = "feltkit";
|
|
40
42
|
export const AGENT_VERSION = 1;
|
|
41
43
|
/** Permissions for using the agent itself. Actions still need their own permissions. */
|
|
@@ -191,6 +193,7 @@ export class AgentHost {
|
|
|
191
193
|
source: "application",
|
|
192
194
|
effect: (c.effect === "observation" ? "observation" : "consequential"),
|
|
193
195
|
...(c.description ? { description: c.description } : {}),
|
|
196
|
+
...(fn?.approval ? { approval: fn.approval } : {}),
|
|
194
197
|
permission: integration ? [integration.op.access] : fn && fn.access !== "public" && fn.access !== "authenticated" ? [fn.access] : [],
|
|
195
198
|
};
|
|
196
199
|
});
|
|
@@ -212,7 +215,16 @@ export class AgentHost {
|
|
|
212
215
|
target: { name: tool.name },
|
|
213
216
|
transport: "agent",
|
|
214
217
|
});
|
|
215
|
-
|
|
218
|
+
const proposeOnly = tool.source === "services" || (tool.approval === "required" && principal.type !== "user");
|
|
219
|
+
out.push({
|
|
220
|
+
name: tool.name,
|
|
221
|
+
source: tool.source,
|
|
222
|
+
effect: tool.effect,
|
|
223
|
+
...(tool.description ? { description: tool.description } : {}),
|
|
224
|
+
allowed: decision.allowed,
|
|
225
|
+
...(decision.allowed ? { invoke: proposeOnly ? "propose" : "direct" } : {}),
|
|
226
|
+
...(tool.approval ? { approval: tool.approval } : {}),
|
|
227
|
+
});
|
|
216
228
|
}
|
|
217
229
|
return out;
|
|
218
230
|
}
|
|
@@ -401,20 +413,7 @@ export class AgentHost {
|
|
|
401
413
|
const head = await this.#head(taskId, actionId);
|
|
402
414
|
if (head.record.state !== "proposed")
|
|
403
415
|
return this.#unchanged(taskId, p, head.record, "approve");
|
|
404
|
-
const
|
|
405
|
-
const owner = await this.#resolveOwner(task);
|
|
406
|
-
const allowed = async (who) => (await this.#runtime.authority.authorize({ access: tool?.permission[0] ?? "authenticated", principal: who, target: { name: head.record.capability }, transport: "agent" })).allowed;
|
|
407
|
-
const refusal = !tool
|
|
408
|
-
? `${head.record.capability} is no longer a capability of this application`
|
|
409
|
-
: this.#separateApprover && head.record.proposedBy === actorOf(p)
|
|
410
|
-
? `${actorOf(p)} proposed this action and may not also approve it`
|
|
411
|
-
: !owner
|
|
412
|
-
? `the task owner ${task.owner} cannot be resolved (no active session or key)`
|
|
413
|
-
: !(await allowed(p))
|
|
414
|
-
? `${actorOf(p)} may not perform ${head.record.capability}`
|
|
415
|
-
: !(await allowed(owner))
|
|
416
|
-
? `the task owner ${task.owner} may not perform ${head.record.capability}`
|
|
417
|
-
: null;
|
|
416
|
+
const { refusal, owner } = await this.#approvalCheck(task, head.record, p);
|
|
418
417
|
if (refusal) {
|
|
419
418
|
const refused = await this.#move(head, "proposed", {}, actorOf(p), `approval refused: ${refusal}`);
|
|
420
419
|
await this.#sync(taskId, task.owner, `Approval of ${head.record.capability} refused: ${refusal}.`, { decisions: [`approval of ${actionId} by ${actorOf(p)} refused: ${refusal}`], evidence: [memoryEvidence(refused.memory.memory_id, "refused approval")] });
|
|
@@ -433,6 +432,46 @@ export class AgentHost {
|
|
|
433
432
|
}
|
|
434
433
|
return this.#run(taskId, task.owner, approved, owner, p);
|
|
435
434
|
}
|
|
435
|
+
/** Why `p` may not approve `record` now (null: it may), and the resolved owner. The one rule approve() and Studio use. */
|
|
436
|
+
async #approvalCheck(task, record, p) {
|
|
437
|
+
const tool = this.#catalog().find((t) => t.name === record.capability);
|
|
438
|
+
const owner = await this.#resolveOwner(task);
|
|
439
|
+
const allowed = async (who) => (await this.#runtime.authority.authorize({ access: tool?.permission[0] ?? "authenticated", principal: who, target: { name: record.capability }, transport: "agent" })).allowed;
|
|
440
|
+
const refusal = !tool
|
|
441
|
+
? `${record.capability} is no longer a capability of this application`
|
|
442
|
+
: this.#separateApprover && record.proposedBy === actorOf(p)
|
|
443
|
+
? `${actorOf(p)} proposed this action and may not also approve it`
|
|
444
|
+
: !owner
|
|
445
|
+
? `the task owner ${task.owner} cannot be resolved (no active session or key)`
|
|
446
|
+
: !(await allowed(p))
|
|
447
|
+
? `${actorOf(p)} may not perform ${record.capability}`
|
|
448
|
+
: !(await allowed(owner))
|
|
449
|
+
? `the task owner ${task.owner} may not perform ${record.capability}`
|
|
450
|
+
: null;
|
|
451
|
+
return { refusal, owner };
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* For each proposed action of a task: whether `principal` may approve it now, and if not, why
|
|
455
|
+
* (the same checks approve() makes; nothing changes). Studio shows Approve only when it may.
|
|
456
|
+
*/
|
|
457
|
+
async approvability(taskId, principal) {
|
|
458
|
+
const { task } = await this.#loaded(taskId);
|
|
459
|
+
const out = {};
|
|
460
|
+
const writer = grantsPermission(this.#runtime.authority.permissionsFor(principal), AGENT_PERMISSIONS.write);
|
|
461
|
+
for (const { record } of await this.#heads(taskId)) {
|
|
462
|
+
if (record.state !== "proposed")
|
|
463
|
+
continue;
|
|
464
|
+
if (principal.type !== "user")
|
|
465
|
+
out[record.actionId] = { allowed: false, reason: "only a person can approve a consequential action" };
|
|
466
|
+
else if (!writer)
|
|
467
|
+
out[record.actionId] = { allowed: false, reason: `${actorOf(principal)} lacks ${AGENT_PERMISSIONS.write}` };
|
|
468
|
+
else {
|
|
469
|
+
const { refusal } = await this.#approvalCheck(task, record, principal);
|
|
470
|
+
out[record.actionId] = refusal ? { allowed: false, reason: refusal } : { allowed: true };
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
return out;
|
|
474
|
+
}
|
|
436
475
|
/** Execute an approved action: durable intent first, then the call, then the outcome. */
|
|
437
476
|
async #run(taskId, ownerActor, approved, owner, approver) {
|
|
438
477
|
const work = (async () => {
|
|
@@ -818,7 +857,7 @@ export class AgentHost {
|
|
|
818
857
|
}
|
|
819
858
|
async #decide(taskId, owner) {
|
|
820
859
|
const { task } = await this.#loaded(taskId);
|
|
821
|
-
const stored = (await this.#
|
|
860
|
+
const stored = (await this.#taskMemories(taskId)).find((m) => m.kind === DECISION_REQUEST_KIND);
|
|
822
861
|
if (!stored)
|
|
823
862
|
throw new FeltKitError("NOT_FOUND", `${taskId} has no decision to make.`);
|
|
824
863
|
const spec = stored.content;
|
|
@@ -880,15 +919,18 @@ export class AgentHost {
|
|
|
880
919
|
capabilities: outcome.capabilities,
|
|
881
920
|
provider: outcome.provider,
|
|
882
921
|
source: outcome.source,
|
|
922
|
+
// The Resolver receipt: reference, revision and target this decision ran on. Never a secret.
|
|
923
|
+
...(outcome.resolution ? { resolution: outcome.resolution } : {}),
|
|
883
924
|
durationMs: outcome.durationMs,
|
|
884
925
|
decidedAt: new Date().toISOString(),
|
|
885
926
|
note: "A model's choice among the offered options: a proposal, not evidence that anything happened.",
|
|
886
927
|
},
|
|
887
928
|
source: { actor: task.owner },
|
|
888
929
|
});
|
|
889
|
-
|
|
930
|
+
// With a Resolver reference, say which revision decided, so Studio and agents can see it.
|
|
931
|
+
const by = `${outcome.provider}${outcome.metadata.model ? `/${outcome.metadata.model}` : ""}${outcome.resolution ? ` (${outcome.resolution.ref} r${outcome.resolution.revision})` : ""}`;
|
|
890
932
|
const evidence = [{ kind: "record", collection: FELTDB_AGENT_MEMORY_COLLECTION, id: decided.memory_id, description: "model decision" }];
|
|
891
|
-
this.#activity("decision.made", owner, taskId, "success", { selected: selected.id, provider: outcome.provider, model: outcome.metadata.model ?? null, offered: offered.length, excluded: excluded.length });
|
|
933
|
+
this.#activity("decision.made", owner, taskId, "success", { selected: selected.id, provider: outcome.provider, model: outcome.metadata.model ?? null, offered: offered.length, excluded: excluded.length, ...(outcome.resolution ? { ref: outcome.resolution.ref, revision: outcome.resolution.revision } : {}) });
|
|
892
934
|
if (!selected.capability) {
|
|
893
935
|
await this.#state.checkpoint(taskId, {
|
|
894
936
|
actor: task.owner,
|
|
@@ -941,16 +983,21 @@ export class AgentHost {
|
|
|
941
983
|
const attribution = { owner, ...(approver ? { approver } : {}) };
|
|
942
984
|
try {
|
|
943
985
|
let output;
|
|
986
|
+
const dispatch = () => this.#runtime.appport.handleRequest({
|
|
987
|
+
protocol: "appport/1",
|
|
988
|
+
type: "request",
|
|
989
|
+
requestId,
|
|
990
|
+
capability: { name: action.capability, version: 1 },
|
|
991
|
+
input: action.input,
|
|
992
|
+
...(action.idempotencyKey ? { idempotencyKey: action.idempotencyKey } : {}),
|
|
993
|
+
metadata: { transport: "agent" },
|
|
994
|
+
}, { principal: toAppPort(owner), transport: "agent", signal: controller.signal });
|
|
995
|
+
// An approved action runs inside the approval it carries: the only way a non-person
|
|
996
|
+
// principal runs an `approval: "required"` mutation (runtime.#approvalRefusal).
|
|
944
997
|
const call = action.source === "application"
|
|
945
|
-
?
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
requestId,
|
|
949
|
-
capability: { name: action.capability, version: 1 },
|
|
950
|
-
input: action.input,
|
|
951
|
-
...(action.idempotencyKey ? { idempotencyKey: action.idempotencyKey } : {}),
|
|
952
|
-
metadata: { transport: "agent" },
|
|
953
|
-
}, { principal: toAppPort(owner), transport: "agent", signal: controller.signal })
|
|
998
|
+
? approver
|
|
999
|
+
? this.#runtime.runApproved({ capability: action.capability, taskId, actionId: action.actionId, approvedBy: actorOf(approver) }, dispatch)
|
|
1000
|
+
: dispatch()
|
|
954
1001
|
: this.#runtime.services.invoke(action.capability, action.input, owner, controller.signal, requestId);
|
|
955
1002
|
// The call itself may not observe the signal; stop waiting for it either way.
|
|
956
1003
|
const result = await Promise.race([call, new Promise((_, reject) => controller.signal.addEventListener("abort", () => reject(controller.signal.reason), { once: true }))]);
|
|
@@ -1034,7 +1081,7 @@ export class AgentHost {
|
|
|
1034
1081
|
await this.#state.remember({ taskId, kind: "outcome", content: outcome, source: { actor: owner } });
|
|
1035
1082
|
}
|
|
1036
1083
|
async #lastOutput(taskId) {
|
|
1037
|
-
const memories = await this.#
|
|
1084
|
+
const memories = await this.#taskMemories(taskId);
|
|
1038
1085
|
const outcome = memories.filter((m) => m.kind === "outcome").sort((a, b) => a.created_at.localeCompare(b.created_at)).at(-1);
|
|
1039
1086
|
return outcome ? outcome.content.output ?? null : null;
|
|
1040
1087
|
}
|
|
@@ -1049,9 +1096,29 @@ export class AgentHost {
|
|
|
1049
1096
|
await this.#db.failAgentExecution(await this.#execution(taskId), message).catch(() => undefined);
|
|
1050
1097
|
}
|
|
1051
1098
|
// ---- FeltDB state helpers ----------------------------------------------------
|
|
1099
|
+
/**
|
|
1100
|
+
* Every active memory scoped to the task, paged with FeltDB's cursor. Not `recallForTask`: it
|
|
1101
|
+
* returns at most the 100 newest per scope, with no cursor, so a task with more active memories
|
|
1102
|
+
* would lose its oldest actions and its decision request (FeltKit's records are all task-scoped).
|
|
1103
|
+
*/
|
|
1104
|
+
async #taskMemories(taskId) {
|
|
1105
|
+
const out = [];
|
|
1106
|
+
let cursor;
|
|
1107
|
+
const seen = new Set();
|
|
1108
|
+
do {
|
|
1109
|
+
const page = await this.#state.recall({ scope: { kind: "task", taskId }, limit: 1000, ...(cursor ? { cursor } : {}) });
|
|
1110
|
+
out.push(...page.memories);
|
|
1111
|
+
cursor = page.nextCursor;
|
|
1112
|
+
if (cursor && seen.has(cursor))
|
|
1113
|
+
throw new FeltKitError("INTERNAL", `FeltDB returned the same cursor twice while reading ${taskId}'s memories.`);
|
|
1114
|
+
if (cursor)
|
|
1115
|
+
seen.add(cursor);
|
|
1116
|
+
} while (cursor);
|
|
1117
|
+
return out;
|
|
1118
|
+
}
|
|
1052
1119
|
/** The current record of every consequential action on a task (one per action). */
|
|
1053
1120
|
async #heads(taskId) {
|
|
1054
|
-
const memories = await this.#
|
|
1121
|
+
const memories = await this.#taskMemories(taskId);
|
|
1055
1122
|
return memories
|
|
1056
1123
|
.filter((m) => (m.kind === ACTION_KIND || m.kind === LEGACY_PROPOSAL_KIND) && m.scope.kind === "task" && m.status === "active")
|
|
1057
1124
|
.map((m) => ({ memory: m, record: m.kind === ACTION_KIND ? m.content : legacy(m) }))
|
|
@@ -1268,7 +1335,7 @@ export class AgentHost {
|
|
|
1268
1335
|
async #legacyExecution(taskId) {
|
|
1269
1336
|
this.#legacy ??= (async () => {
|
|
1270
1337
|
const byTask = new Map();
|
|
1271
|
-
for (const row of (await this.#db
|
|
1338
|
+
for (const row of (await readAll(this.#db, { collection: LEGACY_EXECUTIONS }))) {
|
|
1272
1339
|
if (row.taskId || !Array.isArray(row.inputs))
|
|
1273
1340
|
continue;
|
|
1274
1341
|
for (const input of row.inputs) {
|