@namzu/sdk 61.3.0 → 62.0.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/CHANGELOG.md +61 -0
- package/dist/agents/QueryAgent.d.ts.map +1 -1
- package/dist/agents/QueryAgent.js +2 -0
- package/dist/agents/QueryAgent.js.map +1 -1
- package/dist/bridge/acp/permission.d.ts +8 -0
- package/dist/bridge/acp/permission.d.ts.map +1 -1
- package/dist/bridge/acp/permission.js.map +1 -1
- package/dist/bridge/acp/server.d.ts.map +1 -1
- package/dist/bridge/acp/server.js +8 -1
- package/dist/bridge/acp/server.js.map +1 -1
- package/dist/constants/tools/index.d.ts +2 -0
- package/dist/constants/tools/index.d.ts.map +1 -1
- package/dist/constants/tools/index.js +23 -0
- package/dist/constants/tools/index.js.map +1 -1
- package/dist/eval/harness-protection.js +1 -1
- package/dist/eval/harness-protection.js.map +1 -1
- package/dist/learning/cycle.d.ts +5 -3
- package/dist/learning/cycle.d.ts.map +1 -1
- package/dist/learning/cycle.js +11 -13
- package/dist/learning/cycle.js.map +1 -1
- package/dist/learning/guidance.d.ts +74 -0
- package/dist/learning/guidance.d.ts.map +1 -0
- package/dist/learning/guidance.js +259 -0
- package/dist/learning/guidance.js.map +1 -0
- package/dist/learning/skill.d.ts +2 -2
- package/dist/learning/skill.d.ts.map +1 -1
- package/dist/learning/skill.js +6 -6
- package/dist/learning/skill.js.map +1 -1
- package/dist/learning/store.d.ts.map +1 -1
- package/dist/learning/store.js +6 -6
- package/dist/learning/store.js.map +1 -1
- package/dist/pals/communication/ingress-schema.d.ts +8 -8
- package/dist/pals/communication/schema.d.ts +2 -2
- package/dist/peers/client.d.ts +2 -3
- package/dist/peers/client.d.ts.map +1 -1
- package/dist/peers/client.js +2 -3
- package/dist/peers/client.js.map +1 -1
- package/dist/peers/endpoint.d.ts +1 -2
- package/dist/peers/endpoint.d.ts.map +1 -1
- package/dist/peers/endpoint.js +1 -2
- package/dist/peers/endpoint.js.map +1 -1
- package/dist/peers/protocol.d.ts.map +1 -1
- package/dist/peers/protocol.js +1 -0
- package/dist/peers/protocol.js.map +1 -1
- package/dist/prompt/coding-agent-doctrine.d.ts +4 -4
- package/dist/prompt/coding-agent-doctrine.d.ts.map +1 -1
- package/dist/prompt/coding-agent-doctrine.js +5 -3
- package/dist/prompt/coding-agent-doctrine.js.map +1 -1
- package/dist/prompt/index.d.ts +0 -4
- package/dist/prompt/index.d.ts.map +1 -1
- package/dist/prompt/index.js +0 -2
- package/dist/prompt/index.js.map +1 -1
- package/dist/provider/fallback.d.ts.map +1 -1
- package/dist/provider/fallback.js +3 -0
- package/dist/provider/fallback.js.map +1 -1
- package/dist/provider/idle-timeout.d.ts.map +1 -1
- package/dist/provider/idle-timeout.js +5 -0
- package/dist/provider/idle-timeout.js.map +1 -1
- package/dist/provider/model-window-suffix.d.ts.map +1 -1
- package/dist/provider/model-window-suffix.js +1 -0
- package/dist/provider/model-window-suffix.js.map +1 -1
- package/dist/provider/retry.d.ts.map +1 -1
- package/dist/provider/retry.js +5 -0
- package/dist/provider/retry.js.map +1 -1
- package/dist/provider/step-stopped.d.ts +19 -0
- package/dist/provider/step-stopped.d.ts.map +1 -0
- package/dist/provider/step-stopped.js +25 -0
- package/dist/provider/step-stopped.js.map +1 -0
- package/dist/provider/token-budget.d.ts.map +1 -1
- package/dist/provider/token-budget.js +6 -1
- package/dist/provider/token-budget.js.map +1 -1
- package/dist/public-runtime.d.ts +4 -15
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +3 -14
- package/dist/public-runtime.js.map +1 -1
- package/dist/public-tools.d.ts +0 -2
- package/dist/public-tools.d.ts.map +1 -1
- package/dist/public-tools.js +0 -2
- package/dist/public-tools.js.map +1 -1
- package/dist/public-types.d.ts +3 -21
- package/dist/public-types.d.ts.map +1 -1
- package/dist/runtime/query/iteration/index.d.ts +17 -0
- package/dist/runtime/query/iteration/index.d.ts.map +1 -1
- package/dist/runtime/query/iteration/index.js +168 -5
- package/dist/runtime/query/iteration/index.js.map +1 -1
- package/dist/runtime/query/iteration/phases/per-call-review.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/per-call-review.js +4 -5
- package/dist/runtime/query/iteration/phases/per-call-review.js.map +1 -1
- package/dist/runtime/query/iteration/phases/tool-review.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/tool-review.js +6 -3
- package/dist/runtime/query/iteration/phases/tool-review.js.map +1 -1
- package/dist/runtime/query/iteration/stream-turn.d.ts +9 -1
- package/dist/runtime/query/iteration/stream-turn.d.ts.map +1 -1
- package/dist/runtime/query/iteration/stream-turn.js +48 -2
- package/dist/runtime/query/iteration/stream-turn.js.map +1 -1
- package/dist/runtime/query/resume-pending.d.ts.map +1 -1
- package/dist/runtime/query/resume-pending.js +7 -4
- package/dist/runtime/query/resume-pending.js.map +1 -1
- package/dist/runtime/query/review-policy.d.ts +40 -8
- package/dist/runtime/query/review-policy.d.ts.map +1 -1
- package/dist/runtime/query/review-policy.js +139 -24
- package/dist/runtime/query/review-policy.js.map +1 -1
- package/dist/runtime/query/turn/completion-audit.d.ts +32 -0
- package/dist/runtime/query/turn/completion-audit.d.ts.map +1 -0
- package/dist/runtime/query/turn/completion-audit.js +56 -0
- package/dist/runtime/query/turn/completion-audit.js.map +1 -0
- package/dist/runtime/query/turn/stall-monitor.d.ts +78 -0
- package/dist/runtime/query/turn/stall-monitor.d.ts.map +1 -0
- package/dist/runtime/query/turn/stall-monitor.js +101 -0
- package/dist/runtime/query/turn/stall-monitor.js.map +1 -0
- package/dist/session/paths.d.ts +1 -2
- package/dist/session/paths.d.ts.map +1 -1
- package/dist/session/paths.js +1 -4
- package/dist/session/paths.js.map +1 -1
- package/dist/tools/builtins/bash.d.ts +2 -1
- package/dist/tools/builtins/bash.d.ts.map +1 -1
- package/dist/tools/builtins/bash.js +5 -2
- package/dist/tools/builtins/bash.js.map +1 -1
- package/dist/tools/builtins/computer-use.d.ts +80 -0
- package/dist/tools/builtins/computer-use.d.ts.map +1 -1
- package/dist/tools/builtins/computer-use.js +172 -9
- package/dist/tools/builtins/computer-use.js.map +1 -1
- package/dist/tools/builtins/content-fingerprint.d.ts +13 -0
- package/dist/tools/builtins/content-fingerprint.d.ts.map +1 -1
- package/dist/tools/builtins/content-fingerprint.js +25 -0
- package/dist/tools/builtins/content-fingerprint.js.map +1 -1
- package/dist/tools/builtins/edit.d.ts.map +1 -1
- package/dist/tools/builtins/edit.js +7 -2
- package/dist/tools/builtins/edit.js.map +1 -1
- package/dist/tools/builtins/shell-read-credit.d.ts +15 -0
- package/dist/tools/builtins/shell-read-credit.d.ts.map +1 -0
- package/dist/tools/builtins/shell-read-credit.js +161 -0
- package/dist/tools/builtins/shell-read-credit.js.map +1 -0
- package/dist/tools/paths.d.ts +15 -2
- package/dist/tools/paths.d.ts.map +1 -1
- package/dist/tools/paths.js +17 -2
- package/dist/tools/paths.js.map +1 -1
- package/dist/turn/evidence-recall.d.ts +1 -1
- package/dist/turn/evidence-recall.d.ts.map +1 -1
- package/dist/turn/evidence-recall.js +5 -14
- package/dist/turn/evidence-recall.js.map +1 -1
- package/dist/types/acp/index.d.ts +6 -0
- package/dist/types/acp/index.d.ts.map +1 -1
- package/dist/types/agent/base.d.ts +4 -0
- package/dist/types/agent/base.d.ts.map +1 -1
- package/dist/types/hitl/index.d.ts +8 -0
- package/dist/types/hitl/index.d.ts.map +1 -1
- package/dist/types/hitl/index.js.map +1 -1
- package/dist/types/ids/index.d.ts +1 -1
- package/dist/types/message/index.d.ts +1 -1
- package/dist/types/message/index.d.ts.map +1 -1
- package/dist/types/message/index.js +1 -0
- package/dist/types/message/index.js.map +1 -1
- package/dist/types/provider/interface.d.ts +12 -0
- package/dist/types/provider/interface.d.ts.map +1 -1
- package/dist/types/session/config.d.ts +25 -0
- package/dist/types/session/config.d.ts.map +1 -1
- package/dist/types/session/records.d.ts.map +1 -1
- package/dist/types/session/records.js +2 -0
- package/dist/types/session/records.js.map +1 -1
- package/dist/types/session/turn.d.ts +1 -1
- package/dist/types/session/turn.d.ts.map +1 -1
- package/dist/types/session/turn.js.map +1 -1
- package/package.json +1 -1
- package/src/agents/QueryAgent.ts +2 -0
- package/src/bridge/acp/permission.ts +8 -0
- package/src/bridge/acp/server.ts +7 -1
- package/src/constants/tools/index.ts +27 -0
- package/src/eval/harness-protection.ts +1 -1
- package/src/learning/cycle.ts +15 -16
- package/src/learning/guidance.ts +339 -0
- package/src/learning/skill.ts +7 -7
- package/src/learning/store.ts +5 -6
- package/src/peers/client.ts +2 -3
- package/src/peers/endpoint.ts +1 -2
- package/src/peers/protocol.ts +1 -0
- package/src/prompt/coding-agent-doctrine.ts +5 -3
- package/src/prompt/index.ts +0 -4
- package/src/provider/fallback.ts +7 -0
- package/src/provider/idle-timeout.ts +8 -0
- package/src/provider/model-window-suffix.ts +1 -0
- package/src/provider/retry.ts +8 -0
- package/src/provider/step-stopped.ts +27 -0
- package/src/provider/token-budget.ts +6 -1
- package/src/public-runtime.ts +10 -28
- package/src/public-tools.ts +0 -3
- package/src/public-types.ts +5 -137
- package/src/runtime/query/iteration/index.ts +207 -5
- package/src/runtime/query/iteration/phases/per-call-review.ts +7 -5
- package/src/runtime/query/iteration/phases/tool-review.ts +5 -3
- package/src/runtime/query/iteration/stream-turn.ts +51 -1
- package/src/runtime/query/resume-pending.ts +11 -4
- package/src/runtime/query/review-policy.ts +157 -24
- package/src/runtime/query/turn/completion-audit.ts +71 -0
- package/src/runtime/query/turn/stall-monitor.ts +158 -0
- package/src/session/paths.ts +2 -6
- package/src/tools/builtins/bash.ts +6 -2
- package/src/tools/builtins/computer-use.ts +245 -7
- package/src/tools/builtins/content-fingerprint.ts +25 -0
- package/src/tools/builtins/edit.ts +11 -2
- package/src/tools/builtins/shell-read-credit.ts +159 -0
- package/src/tools/paths.ts +18 -2
- package/src/turn/evidence-recall.ts +7 -15
- package/src/types/acp/index.ts +6 -0
- package/src/types/agent/base.ts +4 -0
- package/src/types/hitl/index.ts +8 -0
- package/src/types/ids/index.ts +1 -1
- package/src/types/message/index.ts +1 -0
- package/src/types/provider/interface.ts +13 -0
- package/src/types/session/config.ts +27 -0
- package/src/types/session/records.ts +2 -0
- package/src/types/session/turn.ts +2 -1
- package/dist/learning/deprecated.d.ts +0 -86
- package/dist/learning/deprecated.d.ts.map +0 -1
- package/dist/learning/deprecated.js +0 -22
- package/dist/learning/deprecated.js.map +0 -1
- package/dist/manager/resident/activity.d.ts +0 -74
- package/dist/manager/resident/activity.d.ts.map +0 -1
- package/dist/manager/resident/activity.js +0 -125
- package/dist/manager/resident/activity.js.map +0 -1
- package/dist/manager/resident/agenda.d.ts +0 -167
- package/dist/manager/resident/agenda.d.ts.map +0 -1
- package/dist/manager/resident/agenda.js +0 -588
- package/dist/manager/resident/agenda.js.map +0 -1
- package/dist/manager/resident/consumption.d.ts +0 -98
- package/dist/manager/resident/consumption.d.ts.map +0 -1
- package/dist/manager/resident/consumption.js +0 -237
- package/dist/manager/resident/consumption.js.map +0 -1
- package/dist/manager/resident/delivery-window.d.ts +0 -28
- package/dist/manager/resident/delivery-window.d.ts.map +0 -1
- package/dist/manager/resident/delivery-window.js +0 -80
- package/dist/manager/resident/delivery-window.js.map +0 -1
- package/dist/manager/resident/evidence-recall.d.ts +0 -30
- package/dist/manager/resident/evidence-recall.d.ts.map +0 -1
- package/dist/manager/resident/evidence-recall.js +0 -297
- package/dist/manager/resident/evidence-recall.js.map +0 -1
- package/dist/manager/resident/history-disk.d.ts +0 -10
- package/dist/manager/resident/history-disk.d.ts.map +0 -1
- package/dist/manager/resident/history-disk.js +0 -50
- package/dist/manager/resident/history-disk.js.map +0 -1
- package/dist/manager/resident/history.d.ts +0 -115
- package/dist/manager/resident/history.d.ts.map +0 -1
- package/dist/manager/resident/history.js +0 -203
- package/dist/manager/resident/history.js.map +0 -1
- package/dist/manager/resident/host.d.ts +0 -112
- package/dist/manager/resident/host.d.ts.map +0 -1
- package/dist/manager/resident/host.js +0 -254
- package/dist/manager/resident/host.js.map +0 -1
- package/dist/manager/resident/initiative.d.ts +0 -86
- package/dist/manager/resident/initiative.d.ts.map +0 -1
- package/dist/manager/resident/initiative.js +0 -131
- package/dist/manager/resident/initiative.js.map +0 -1
- package/dist/manager/resident/loop.d.ts +0 -49
- package/dist/manager/resident/loop.d.ts.map +0 -1
- package/dist/manager/resident/loop.js +0 -75
- package/dist/manager/resident/loop.js.map +0 -1
- package/dist/manager/resident/outbox.d.ts +0 -124
- package/dist/manager/resident/outbox.d.ts.map +0 -1
- package/dist/manager/resident/outbox.js +0 -216
- package/dist/manager/resident/outbox.js.map +0 -1
- package/dist/manager/resident/proposal.d.ts +0 -65
- package/dist/manager/resident/proposal.d.ts.map +0 -1
- package/dist/manager/resident/proposal.js +0 -77
- package/dist/manager/resident/proposal.js.map +0 -1
- package/dist/manager/resident/store.d.ts +0 -108
- package/dist/manager/resident/store.d.ts.map +0 -1
- package/dist/manager/resident/store.js +0 -205
- package/dist/manager/resident/store.js.map +0 -1
- package/dist/manager/resident/tool-evidence.d.ts +0 -112
- package/dist/manager/resident/tool-evidence.d.ts.map +0 -1
- package/dist/manager/resident/tool-evidence.js +0 -289
- package/dist/manager/resident/tool-evidence.js.map +0 -1
- package/dist/prompt/resident-learning.d.ts +0 -29
- package/dist/prompt/resident-learning.d.ts.map +0 -1
- package/dist/prompt/resident-learning.js +0 -126
- package/dist/prompt/resident-learning.js.map +0 -1
- package/dist/prompt/resident-step.d.ts +0 -42
- package/dist/prompt/resident-step.d.ts.map +0 -1
- package/dist/prompt/resident-step.js +0 -137
- package/dist/prompt/resident-step.js.map +0 -1
- package/dist/tools/resident-history.d.ts +0 -12
- package/dist/tools/resident-history.d.ts.map +0 -1
- package/dist/tools/resident-history.js +0 -82
- package/dist/tools/resident-history.js.map +0 -1
- package/dist/tools/resident-tool-evidence.d.ts +0 -9
- package/dist/tools/resident-tool-evidence.d.ts.map +0 -1
- package/dist/tools/resident-tool-evidence.js +0 -61
- package/dist/tools/resident-tool-evidence.js.map +0 -1
- package/src/learning/deprecated.ts +0 -124
- package/src/manager/resident/activity.ts +0 -211
- package/src/manager/resident/agenda.ts +0 -937
- package/src/manager/resident/consumption.ts +0 -335
- package/src/manager/resident/delivery-window.ts +0 -99
- package/src/manager/resident/evidence-recall.ts +0 -369
- package/src/manager/resident/history-disk.ts +0 -63
- package/src/manager/resident/history.ts +0 -348
- package/src/manager/resident/host.ts +0 -390
- package/src/manager/resident/initiative.ts +0 -231
- package/src/manager/resident/loop.ts +0 -119
- package/src/manager/resident/outbox.ts +0 -341
- package/src/manager/resident/proposal.ts +0 -119
- package/src/manager/resident/store.ts +0 -279
- package/src/manager/resident/tool-evidence.ts +0 -455
- package/src/prompt/resident-learning.ts +0 -152
- package/src/prompt/resident-step.ts +0 -182
- package/src/tools/resident-history.ts +0 -88
- package/src/tools/resident-tool-evidence.ts +0 -72
|
@@ -1,279 +0,0 @@
|
|
|
1
|
-
import { createHash, randomUUID } from 'node:crypto'
|
|
2
|
-
import { join } from 'node:path'
|
|
3
|
-
import { z } from 'zod'
|
|
4
|
-
import {
|
|
5
|
-
DiskRevisionRecordStore,
|
|
6
|
-
revisionFileSegment,
|
|
7
|
-
} from '../../store/kv/revision-record-store.js'
|
|
8
|
-
import { defineSchema } from '../../store/schema.js'
|
|
9
|
-
import type { TenantId } from '../../types/ids/index.js'
|
|
10
|
-
import { asTenantId } from '../../utils/id.js'
|
|
11
|
-
|
|
12
|
-
const text = z.string().trim().min(1).max(8_000)
|
|
13
|
-
const time = z.number().int().nonnegative().safe()
|
|
14
|
-
const MAX_WAKE_EVIDENCE_CHARS = 16_000
|
|
15
|
-
const wakeEvidence = z
|
|
16
|
-
.array(z.object({ reason: text, receivedAt: time }).readonly())
|
|
17
|
-
.min(1)
|
|
18
|
-
.max(16, 'Resident has 16 pending wake inputs; process them before adding more.')
|
|
19
|
-
.refine(
|
|
20
|
-
(entries) =>
|
|
21
|
-
entries.reduce((total, entry) => total + entry.reason.length, 0) <= MAX_WAKE_EVIDENCE_CHARS,
|
|
22
|
-
'Resident wake evidence exceeds 16000 characters; process pending evidence before adding more.',
|
|
23
|
-
)
|
|
24
|
-
.readonly()
|
|
25
|
-
export const residentDecisionSchema = z.discriminatedUnion('kind', [
|
|
26
|
-
z.object({ kind: z.literal('wait'), summary: text, wakeAt: time.nullable() }),
|
|
27
|
-
z.object({ kind: z.literal('complete'), summary: text }),
|
|
28
|
-
z.object({ kind: z.literal('blocked'), summary: text }),
|
|
29
|
-
])
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* @experimental One bounded step's durable disposition; silence is not completion.
|
|
33
|
-
*
|
|
34
|
-
* @deprecated Residents are deprecated and removed in the next major release. Learning (`runLearningCycle` and the other `Learning*` names) keeps working, but `runLearningCycle` still takes a resident agenda until learning gets its own activation target in the next major release; for long-running work use scheduled tasks (`LocalTaskScheduler`), session goals and background agents.
|
|
35
|
-
*/
|
|
36
|
-
export type ResidentDecision = z.infer<typeof residentDecisionSchema>
|
|
37
|
-
|
|
38
|
-
export const residentStateSchema = z
|
|
39
|
-
.object({
|
|
40
|
-
tenantId: z.string().uuid(),
|
|
41
|
-
agentKey: z.string().min(1).max(200),
|
|
42
|
-
pursuitId: z.string().uuid().optional(),
|
|
43
|
-
identity: text,
|
|
44
|
-
objective: text,
|
|
45
|
-
revision: z.number().int().positive().safe(),
|
|
46
|
-
stepsAdmitted: time,
|
|
47
|
-
phase: z.enum(['waiting', 'running', 'complete', 'blocked']),
|
|
48
|
-
wakeAt: time.nullable(),
|
|
49
|
-
reason: text,
|
|
50
|
-
/** Accepted wake inputs, in commit order, retained until this step settles. */
|
|
51
|
-
wakeEvidence: wakeEvidence.optional(),
|
|
52
|
-
summary: text.nullable(),
|
|
53
|
-
claimId: z.string().uuid().nullable(),
|
|
54
|
-
})
|
|
55
|
-
.superRefine((state, context) => {
|
|
56
|
-
if ((state.phase === 'running') !== (state.claimId !== null)) {
|
|
57
|
-
context.addIssue({
|
|
58
|
-
code: z.ZodIssueCode.custom,
|
|
59
|
-
message: 'Running state requires a claim.',
|
|
60
|
-
})
|
|
61
|
-
}
|
|
62
|
-
if (state.phase !== 'waiting' && state.wakeAt !== null) {
|
|
63
|
-
context.addIssue({
|
|
64
|
-
code: z.ZodIssueCode.custom,
|
|
65
|
-
message: 'Only waiting state may have a wake time.',
|
|
66
|
-
})
|
|
67
|
-
}
|
|
68
|
-
if (state.wakeEvidence && state.phase !== 'waiting' && state.phase !== 'running') {
|
|
69
|
-
context.addIssue({
|
|
70
|
-
code: z.ZodIssueCode.custom,
|
|
71
|
-
message: 'Only waiting or unresolved running work may retain wake evidence.',
|
|
72
|
-
})
|
|
73
|
-
}
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* @experimental Persisted identity and one pursuit, independent of a conversation.
|
|
78
|
-
*
|
|
79
|
-
* @deprecated Residents are deprecated and removed in the next major release. Learning (`runLearningCycle` and the other `Learning*` names) keeps working, but `runLearningCycle` still takes a resident agenda until learning gets its own activation target in the next major release; for long-running work use scheduled tasks (`LocalTaskScheduler`), session goals and background agents.
|
|
80
|
-
*/
|
|
81
|
-
export type ResidentState = Readonly<z.infer<typeof residentStateSchema>>
|
|
82
|
-
|
|
83
|
-
/** Bounded addressing for local resident state. Stored scope is still validated. */
|
|
84
|
-
export function residentKeySegment(key: string): string {
|
|
85
|
-
const encoded = revisionFileSegment(key)
|
|
86
|
-
return encoded.length <= 255
|
|
87
|
-
? encoded
|
|
88
|
-
: `~sha256-${createHash('sha256').update(encoded).digest('hex')}`
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* @experimental Minimal atomic contract needed to execute an existing pursuit.
|
|
93
|
-
*
|
|
94
|
-
* @deprecated Residents are deprecated and removed in the next major release. Learning (`runLearningCycle` and the other `Learning*` names) keeps working, but `runLearningCycle` still takes a resident agenda until learning gets its own activation target in the next major release; for long-running work use scheduled tasks (`LocalTaskScheduler`), session goals and background agents.
|
|
95
|
-
*/
|
|
96
|
-
export type ResidentExecutionStore = Pick<ResidentStore, 'read' | 'claim' | 'settle'>
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* @experimental Backends must atomically compare revisions and publish admission before work.
|
|
100
|
-
*
|
|
101
|
-
* @deprecated Residents are deprecated and removed in the next major release. Learning (`runLearningCycle` and the other `Learning*` names) keeps working, but `runLearningCycle` still takes a resident agenda until learning gets its own activation target in the next major release; for long-running work use scheduled tasks (`LocalTaskScheduler`), session goals and background agents.
|
|
102
|
-
*/
|
|
103
|
-
export interface ResidentStore {
|
|
104
|
-
read(): Promise<ResidentState | null>
|
|
105
|
-
create(identity: string, objective: string): Promise<ResidentState>
|
|
106
|
-
claim(expected: ResidentState, now: number): Promise<ResidentState>
|
|
107
|
-
settle(expected: ResidentState, decision: ResidentDecision, now: number): Promise<ResidentState>
|
|
108
|
-
wake(expected: ResidentState, reason: string, now: number): Promise<ResidentState>
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* A competing owner advanced this resident's immutable revision.
|
|
113
|
-
*
|
|
114
|
-
* @deprecated Residents are deprecated and removed in the next major release. Learning (`runLearningCycle` and the other `Learning*` names) keeps working, but `runLearningCycle` still takes a resident agenda until learning gets its own activation target in the next major release; for long-running work use scheduled tasks (`LocalTaskScheduler`), session goals and background agents.
|
|
115
|
-
*/
|
|
116
|
-
export class ResidentConflictError extends Error {
|
|
117
|
-
constructor() {
|
|
118
|
-
super('Resident state changed; read the current state before retrying.')
|
|
119
|
-
this.name = 'ResidentConflictError'
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* @experimental Local-filesystem state for one tenant/agent key and one pursuit.
|
|
125
|
-
* A running claim never expires automatically: a crashed step may have effects.
|
|
126
|
-
* Uses exclusive immutable revision publication, not a read/rename lock.
|
|
127
|
-
*
|
|
128
|
-
* @deprecated Residents are deprecated and removed in the next major release. Learning (`runLearningCycle` and the other `Learning*` names) keeps working, but `runLearningCycle` still takes a resident agenda until learning gets its own activation target in the next major release; for long-running work use scheduled tasks (`LocalTaskScheduler`), session goals and background agents.
|
|
129
|
-
*/
|
|
130
|
-
export class DiskResidentStore implements ResidentStore {
|
|
131
|
-
private readonly records = new DiskRevisionRecordStore<ResidentState>(
|
|
132
|
-
defineSchema({ kind: 'resident', current: 2, migrations: { 1: (record) => record } }),
|
|
133
|
-
'resident store',
|
|
134
|
-
(record) => record.revision,
|
|
135
|
-
)
|
|
136
|
-
private readonly location
|
|
137
|
-
private readonly tenantId: TenantId
|
|
138
|
-
private readonly agentKey: string
|
|
139
|
-
|
|
140
|
-
constructor(root: string, scope: { tenantId: TenantId; agentKey: string }) {
|
|
141
|
-
this.tenantId = asTenantId(scope.tenantId)
|
|
142
|
-
this.agentKey = z.string().min(1).max(200).parse(scope.agentKey)
|
|
143
|
-
const directory = join(root, this.tenantId, residentKeySegment(this.agentKey))
|
|
144
|
-
this.location = {
|
|
145
|
-
legacyPath: join(directory, 'state.json'),
|
|
146
|
-
revisionsDir: join(directory, 'revisions'),
|
|
147
|
-
publishLegacyProjection: false,
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
private checked(record: ResidentState): ResidentState {
|
|
152
|
-
const state = residentStateSchema.parse(record)
|
|
153
|
-
if (
|
|
154
|
-
state.pursuitId !== undefined ||
|
|
155
|
-
state.tenantId !== this.tenantId ||
|
|
156
|
-
state.agentKey !== this.agentKey
|
|
157
|
-
) {
|
|
158
|
-
throw new Error('Resident record does not match the bound tenant and agent.')
|
|
159
|
-
}
|
|
160
|
-
return Object.freeze(state)
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
async read(): Promise<ResidentState | null> {
|
|
164
|
-
const state = await this.records.read(this.location)
|
|
165
|
-
return state === null ? null : this.checked(state)
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
async create(identity: string, objective: string): Promise<ResidentState> {
|
|
169
|
-
const initial = this.checked({
|
|
170
|
-
tenantId: this.tenantId,
|
|
171
|
-
agentKey: this.agentKey,
|
|
172
|
-
identity,
|
|
173
|
-
objective,
|
|
174
|
-
revision: 1,
|
|
175
|
-
stepsAdmitted: 0,
|
|
176
|
-
phase: 'waiting',
|
|
177
|
-
wakeAt: 0,
|
|
178
|
-
reason: 'Initial pursuit',
|
|
179
|
-
summary: null,
|
|
180
|
-
claimId: null,
|
|
181
|
-
})
|
|
182
|
-
return this.records.transact(this.location, (current) => {
|
|
183
|
-
if (current !== null) throw new ResidentConflictError()
|
|
184
|
-
return { record: initial, result: initial }
|
|
185
|
-
})
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
private async change(
|
|
189
|
-
expected: ResidentState,
|
|
190
|
-
mutate: (current: ResidentState) => ResidentState,
|
|
191
|
-
): Promise<ResidentState> {
|
|
192
|
-
this.checked(expected)
|
|
193
|
-
return this.records.transact(this.location, (record) => {
|
|
194
|
-
if (record === null) throw new ResidentConflictError()
|
|
195
|
-
const current = this.checked(record)
|
|
196
|
-
if (current.revision !== expected.revision || current.claimId !== expected.claimId) {
|
|
197
|
-
throw new ResidentConflictError()
|
|
198
|
-
}
|
|
199
|
-
const next = this.checked({
|
|
200
|
-
...mutate(current),
|
|
201
|
-
revision: current.revision + 1,
|
|
202
|
-
})
|
|
203
|
-
return { record: next, result: next }
|
|
204
|
-
})
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/** Persist admission before invoking a model, tool or developer callback. */
|
|
208
|
-
async claim(expected: ResidentState, now: number): Promise<ResidentState> {
|
|
209
|
-
return this.change(expected, (state) => claimResidentState(state, now))
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/** Only the exact admitted revision may settle; late owners cannot overwrite recovery. */
|
|
213
|
-
async settle(
|
|
214
|
-
expected: ResidentState,
|
|
215
|
-
decision: ResidentDecision,
|
|
216
|
-
now: number,
|
|
217
|
-
): Promise<ResidentState> {
|
|
218
|
-
return this.change(expected, (state) => settleResidentState(state, decision, now))
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
/** Host-supplied new evidence can wake a waiting pursuit; terminal work stays terminal. */
|
|
222
|
-
async wake(expected: ResidentState, reason: string, now: number): Promise<ResidentState> {
|
|
223
|
-
return this.change(expected, (state) => wakeResidentState(state, reason, now))
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/** Internal transitions shared by single-pursuit and whole-agent admission. */
|
|
228
|
-
export function claimResidentState(state: ResidentState, now: number): ResidentState {
|
|
229
|
-
time.parse(now)
|
|
230
|
-
if (state.phase !== 'waiting' || state.wakeAt === null || state.wakeAt > now)
|
|
231
|
-
throw new Error('Resident is not due.')
|
|
232
|
-
return {
|
|
233
|
-
...state,
|
|
234
|
-
phase: 'running',
|
|
235
|
-
wakeAt: null,
|
|
236
|
-
claimId: randomUUID(),
|
|
237
|
-
stepsAdmitted: state.stepsAdmitted + 1,
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
export function settleResidentState(
|
|
242
|
-
state: ResidentState,
|
|
243
|
-
decision: ResidentDecision,
|
|
244
|
-
now: number,
|
|
245
|
-
): ResidentState {
|
|
246
|
-
time.parse(now)
|
|
247
|
-
const outcome = residentDecisionSchema.parse(decision)
|
|
248
|
-
if (outcome.kind === 'wait' && outcome.wakeAt !== null && outcome.wakeAt <= now)
|
|
249
|
-
throw new Error('A scheduled continuation must be in the future.')
|
|
250
|
-
if (state.phase !== 'running') throw new Error('Resident has no admitted step to settle.')
|
|
251
|
-
// Admission alone is not acknowledgment: an interrupted step retains every
|
|
252
|
-
// input. Only successful exact-claim settlement consumes the captured batch.
|
|
253
|
-
const { wakeEvidence: _consumed, ...settled } = state
|
|
254
|
-
return {
|
|
255
|
-
...settled,
|
|
256
|
-
phase: outcome.kind === 'wait' ? 'waiting' : outcome.kind,
|
|
257
|
-
wakeAt: outcome.kind === 'wait' ? outcome.wakeAt : null,
|
|
258
|
-
summary: outcome.summary,
|
|
259
|
-
reason: 'Scheduled continuation',
|
|
260
|
-
claimId: null,
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
export function wakeResidentState(
|
|
265
|
-
state: ResidentState,
|
|
266
|
-
reason: string,
|
|
267
|
-
now: number,
|
|
268
|
-
): ResidentState {
|
|
269
|
-
time.parse(now)
|
|
270
|
-
const evidence = text.parse(reason)
|
|
271
|
-
if (state.phase !== 'waiting') throw new Error('Only a waiting resident can be woken.')
|
|
272
|
-
// Never evict an accepted input to admit another. Backpressure leaves both
|
|
273
|
-
// the durable state and its revision untouched, so the sender can retry later.
|
|
274
|
-
const pending = wakeEvidence.parse([
|
|
275
|
-
...(state.wakeEvidence ?? []),
|
|
276
|
-
{ reason: evidence, receivedAt: now },
|
|
277
|
-
])
|
|
278
|
-
return { ...state, wakeAt: now, reason: evidence, wakeEvidence: pending }
|
|
279
|
-
}
|