@chroxy/server 0.10.0 → 0.11.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/CONFIG.md +411 -14
- package/hooks/permission-hook.sh +253 -3
- package/package.json +8 -15
- package/sidecar/node_modules/.package-lock.json +29 -0
- package/sidecar/node_modules/ws/LICENSE +20 -0
- package/sidecar/node_modules/ws/README.md +548 -0
- package/sidecar/node_modules/ws/browser.js +8 -0
- package/sidecar/node_modules/ws/index.js +22 -0
- package/sidecar/node_modules/ws/lib/buffer-util.js +131 -0
- package/sidecar/node_modules/ws/lib/constants.js +19 -0
- package/sidecar/node_modules/ws/lib/event-target.js +292 -0
- package/sidecar/node_modules/ws/lib/extension.js +203 -0
- package/sidecar/node_modules/ws/lib/limiter.js +55 -0
- package/sidecar/node_modules/ws/lib/permessage-deflate.js +528 -0
- package/sidecar/node_modules/ws/lib/receiver.js +706 -0
- package/sidecar/node_modules/ws/lib/sender.js +602 -0
- package/sidecar/node_modules/ws/lib/stream.js +161 -0
- package/sidecar/node_modules/ws/lib/subprotocol.js +62 -0
- package/sidecar/node_modules/ws/lib/validation.js +152 -0
- package/sidecar/node_modules/ws/lib/websocket-server.js +554 -0
- package/sidecar/node_modules/ws/lib/websocket.js +1393 -0
- package/sidecar/node_modules/ws/package.json +70 -0
- package/sidecar/node_modules/ws/wrapper.mjs +21 -0
- package/src/base-session.js +151 -6
- package/src/billing-budget.js +5 -1
- package/src/binary-provenance-trust.js +95 -0
- package/src/built-in-tools/file-ops.js +5 -4
- package/src/byok-mcp-client.js +1061 -13
- package/src/byok-mcp-config.js +1201 -4
- package/src/byok-mcp-fleet.js +374 -29
- package/src/byok-mcp-oauth-store.js +327 -0
- package/src/byok-mcp-oauth.js +635 -0
- package/src/byok-mcp-trust.js +254 -37
- package/src/byok-session.js +789 -13
- package/src/byok-tool-executor.js +20 -13
- package/src/checkpoint-manager.js +33 -6
- package/src/chroxy-host-metadata.js +122 -0
- package/src/claude-stream-parser.js +56 -0
- package/src/claude-tui/pty-driver.js +54 -14
- package/src/claude-tui-session.js +106 -7
- package/src/cli/config-cmd.js +2 -2
- package/src/cli/deploy-cmd.js +1 -1
- package/src/cli/init-cmd.js +1 -1
- package/src/cli/providers-cmd.js +3 -3
- package/src/cli/schedule-cmd.js +775 -0
- package/src/cli/server-cmd.js +16 -1
- package/src/cli/service-cmd.js +39 -19
- package/src/cli/shared.js +7 -4
- package/src/cli/tokens-cmd.js +213 -0
- package/src/cli/tunnel-cmd.js +1 -1
- package/src/cli-session.js +66 -10
- package/src/cli.js +4 -0
- package/src/codex-app-server-session.js +671 -39
- package/src/codex-session.js +121 -18
- package/src/config.js +453 -27
- package/src/control-room/integrations.js +16 -10
- package/src/control-room/skills-inventory.js +6 -1
- package/src/control-room/survey.js +32 -0
- package/src/credential-store.js +256 -10
- package/src/dashboard-next/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +1 -0
- package/src/diagnostics.js +3 -0
- package/src/docker-sdk-session.js +16 -1
- package/src/docker-session.js +9 -0
- package/src/doctor.js +19 -8
- package/src/environments/backends/docker.js +8 -0
- package/src/environments/backends/k8s.js +9 -0
- package/src/event-ingest.js +19 -1
- package/src/event-normalizer.js +354 -19
- package/src/external-session-registry.js +0 -0
- package/src/gemini-session.js +19 -2
- package/src/github-webhook.js +188 -4
- package/src/handler-utils.js +42 -8
- package/src/handlers/checkpoint-handlers.js +126 -19
- package/src/handlers/control-room-handlers.js +48 -18
- package/src/handlers/conversation-handlers.js +127 -2
- package/src/handlers/credential-handlers.js +43 -2
- package/src/handlers/file-handlers.js +47 -3
- package/src/handlers/github-webhook-handlers.js +204 -0
- package/src/handlers/input-handlers.js +4 -0
- package/src/handlers/orchestration-handlers.js +238 -0
- package/src/handlers/scheduler-handlers.js +510 -0
- package/src/handlers/session-handlers.js +10 -2
- package/src/handlers/settings-handlers.js +510 -11
- package/src/handlers/token-handlers.js +11 -2
- package/src/http-routes.js +159 -0
- package/src/jsonl-reader.js +60 -6
- package/src/jsonl-subprocess-session.js +35 -6
- package/src/keychain.js +159 -12
- package/src/logger.js +182 -19
- package/src/notification-prefs.js +88 -6
- package/src/notifications/discord-webhook-sink.js +30 -1
- package/src/orchestration/build-manager.js +56 -0
- package/src/orchestration/decision-contract.js +245 -0
- package/src/orchestration/git-ops.js +347 -0
- package/src/orchestration/orchestration-manager.js +1295 -0
- package/src/orchestration/permission-gate.js +109 -0
- package/src/orchestration/role-prompts.js +136 -0
- package/src/orchestration/run-budget.js +81 -0
- package/src/orchestration/run-ledger.js +487 -0
- package/src/orchestration/run-model.js +205 -0
- package/src/orchestration/run-record.js +336 -0
- package/src/orchestration/run-report.js +194 -0
- package/src/orchestration/to-wire.js +201 -0
- package/src/orchestration/turn-driver.js +398 -0
- package/src/pairing.js +146 -1
- package/src/permission-audit.js +79 -3
- package/src/permission-floor.js +455 -0
- package/src/permission-manager.js +492 -40
- package/src/permission-resolver.js +55 -7
- package/src/permission-rule-store.js +411 -0
- package/src/platform.js +367 -23
- package/src/providers.js +21 -2
- package/src/schedule-parser.js +311 -0
- package/src/scheduled-task-store.js +653 -0
- package/src/scheduler.js +1029 -0
- package/src/sdk-session.js +338 -5
- package/src/server-cli/server-orchestrator.js +23 -0
- package/src/server-cli/tunnel-lifecycle-handler.js +3 -2
- package/src/server-cli.js +238 -26
- package/src/server-identity.js +28 -4
- package/src/service.js +291 -33
- package/src/session-manager.js +417 -25
- package/src/session-message-history.js +27 -21
- package/src/session-title.js +168 -0
- package/src/session-token-store.js +60 -35
- package/src/skills-usage.js +20 -2
- package/src/statusline.js +584 -0
- package/src/subagent-counter.js +9 -1
- package/src/supervisor.js +77 -27
- package/src/token-manager.js +39 -9
- package/src/transcript-tasks.js +41 -6
- package/src/tunnel/cloudflare.js +115 -8
- package/src/tunnel/index.js +4 -1
- package/src/tunnel-check.js +1 -1
- package/src/turn-tracker.js +0 -0
- package/src/usage-normalize.js +118 -0
- package/src/user-shell-session.js +98 -32
- package/src/utils/componentwise-resolver.js +205 -0
- package/src/utils/composite-key.js +45 -0
- package/src/utils/iso-datetime.js +51 -0
- package/src/utils/preflight.js +140 -28
- package/src/utils/spawn-env.js +8 -2
- package/src/utils/verify-binary.js +276 -0
- package/src/utils/verify-provenance.js +241 -0
- package/src/utils/wire-counters.js +48 -0
- package/src/worktree-gc.js +4 -1
- package/src/ws-file-ops/browser.js +42 -11
- package/src/ws-file-ops/common.js +68 -1
- package/src/ws-file-ops/git.js +221 -3
- package/src/ws-file-ops/index.js +5 -0
- package/src/ws-file-ops/memory.js +358 -0
- package/src/ws-file-ops/reader.js +124 -0
- package/src/ws-forwarding.js +44 -16
- package/src/ws-handler-context.js +19 -0
- package/src/ws-history.js +84 -11
- package/src/ws-message-handlers.js +6 -0
- package/src/ws-outbound-schemas.js +207 -0
- package/src/ws-permissions.js +285 -4
- package/src/ws-schemas.js +1 -0
- package/src/ws-server.js +301 -17
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Orchestration permission gate (engine, epic #6691, step E-2) — a scoped
|
|
3
|
+
* headless approver for the sessions a run OWNS. The engine spawns workers with
|
|
4
|
+
* a permission posture (audit workers get read-allow / write-deny rules via
|
|
5
|
+
* setPermissionRules, so file tools are settled before they ever prompt); this
|
|
6
|
+
* gate handles what's left — chiefly Bash, which can NEVER be rule-allowed
|
|
7
|
+
* (permission-manager NEVER_AUTO_ALLOW). It answers ONLY for owned sessions,
|
|
8
|
+
* never a user's session.
|
|
9
|
+
*
|
|
10
|
+
* Security posture:
|
|
11
|
+
* - Scope: acts only when `isOwnedSession(sessionId)` is true.
|
|
12
|
+
* - Same trust position as a human clicking Allow — it answers each request
|
|
13
|
+
* individually via `session.respondToPermission`, so there is no standing
|
|
14
|
+
* Bash whitelist (the NEVER_AUTO_ALLOW invariant is preserved).
|
|
15
|
+
* - Fail-closed: any tool with no explicit allow decision is denied (audit
|
|
16
|
+
* workers) or escalated to the user (implement workers, a later step).
|
|
17
|
+
* - Never uses permissionMode:'auto' / skipPermissions for workers.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
// Tools a worker must never be allowed to use headlessly: sub-delegation and
|
|
21
|
+
// network fetches (the committee owns the delegation tree; keeps cost flat).
|
|
22
|
+
const ALWAYS_DENY = new Set(['Task', 'WebFetch', 'WebSearch'])
|
|
23
|
+
|
|
24
|
+
export class OrchestrationPermissionGate {
|
|
25
|
+
/**
|
|
26
|
+
* @param {{
|
|
27
|
+
* sessionManager: import('node:events').EventEmitter,
|
|
28
|
+
* isOwnedSession: (sessionId: string) => boolean,
|
|
29
|
+
* policyForSession: (sessionId: string) => ('audit'|'implement'|null),
|
|
30
|
+
* emitEscalation?: (info: object) => void,
|
|
31
|
+
* log?: object,
|
|
32
|
+
* }} opts
|
|
33
|
+
*/
|
|
34
|
+
constructor({ sessionManager, isOwnedSession, policyForSession, emitEscalation = null, bashAllowlist = [], log = null }) {
|
|
35
|
+
if (!sessionManager || typeof sessionManager.on !== 'function') {
|
|
36
|
+
throw new Error('OrchestrationPermissionGate requires a sessionManager EventEmitter')
|
|
37
|
+
}
|
|
38
|
+
if (typeof isOwnedSession !== 'function') throw new Error('isOwnedSession is required')
|
|
39
|
+
if (typeof policyForSession !== 'function') throw new Error('policyForSession is required')
|
|
40
|
+
this._sm = sessionManager
|
|
41
|
+
this._isOwned = isOwnedSession
|
|
42
|
+
this._policyForSession = policyForSession
|
|
43
|
+
this._emitEscalation = emitEscalation
|
|
44
|
+
this._log = log
|
|
45
|
+
// Compile the implement-worker Bash allowlist once. String entries are
|
|
46
|
+
// ANCHORED (^(?:…)$) so an allowlisted phrase can't match as a substring of
|
|
47
|
+
// a larger command — `npm test` must NOT allow `npm test && curl x | sh`.
|
|
48
|
+
// Pre-built RegExp entries pass through as-authored. A match is auto-approved;
|
|
49
|
+
// a non-match escalates. There is NEVER a standing whitelist — each request
|
|
50
|
+
// is still answered per-request via respondToPermission.
|
|
51
|
+
this._bashAllowlist = (bashAllowlist || []).map((p) => (p instanceof RegExp ? p : new RegExp(`^(?:${p})$`)))
|
|
52
|
+
this._onEvent = this._handleSessionEvent.bind(this)
|
|
53
|
+
this._sm.on('session_event', this._onEvent)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
dispose() {
|
|
57
|
+
this._sm.off?.('session_event', this._onEvent)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
_handleSessionEvent({ sessionId, event, data } = {}) {
|
|
61
|
+
if (event !== 'permission_request') return
|
|
62
|
+
if (!this._isOwned(sessionId)) return // never answer a user's session
|
|
63
|
+
const role = this._policyForSession(sessionId)
|
|
64
|
+
if (!role) return
|
|
65
|
+
const toolName = data?.toolName ?? data?.tool ?? ''
|
|
66
|
+
const requestId = data?.requestId ?? data?.id ?? null
|
|
67
|
+
if (requestId == null) return
|
|
68
|
+
const decision = this._decide(role, toolName, data?.input ?? {})
|
|
69
|
+
if (decision === 'escalate') {
|
|
70
|
+
// Implement-worker Bash that isn't allowlisted: surface to the user. Until
|
|
71
|
+
// the run's escalation UX resolves it, deny so the worker never blocks
|
|
72
|
+
// indefinitely (a later step wires a real hold + user response).
|
|
73
|
+
this._emitEscalation?.({ sessionId, toolName, input: data?.input ?? null, requestId })
|
|
74
|
+
this._respond(sessionId, requestId, 'deny')
|
|
75
|
+
return
|
|
76
|
+
}
|
|
77
|
+
this._respond(sessionId, requestId, decision)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
_decide(role, toolName, input) {
|
|
81
|
+
if (ALWAYS_DENY.has(toolName)) return 'deny'
|
|
82
|
+
// Audit workers are read-only: file reads are already allowed via session
|
|
83
|
+
// rules, so anything reaching the gate (Bash, shell) is denied.
|
|
84
|
+
if (role === 'audit') return 'deny'
|
|
85
|
+
// Implement workers: Bash (never rule-eligible — NEVER_AUTO_ALLOW) is matched
|
|
86
|
+
// against the compiled allowlist. Match → allow; anything else → escalate.
|
|
87
|
+
// Fail-closed: an empty allowlist escalates every Bash command.
|
|
88
|
+
if (toolName === 'Bash' || toolName === 'shell') {
|
|
89
|
+
const command = typeof input?.command === 'string' ? input.command : ''
|
|
90
|
+
if (command && this._bashAllowlist.some((re) => re.test(command))) return 'allow'
|
|
91
|
+
return 'escalate'
|
|
92
|
+
}
|
|
93
|
+
return 'deny'
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
_respond(sessionId, requestId, decision) {
|
|
97
|
+
const entry = this._sm.getSession?.(sessionId)
|
|
98
|
+
const session = entry?.session
|
|
99
|
+
if (!session || typeof session.respondToPermission !== 'function') {
|
|
100
|
+
this._log?.warn?.(`permission-gate: cannot answer ${sessionId} (no respondToPermission)`)
|
|
101
|
+
return
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
session.respondToPermission(requestId, decision === 'allow' ? 'allow' : 'deny')
|
|
105
|
+
} catch (err) {
|
|
106
|
+
this._log?.warn?.(`permission-gate: respond failed for ${sessionId}: ${err?.message || err}`)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Role preambles + per-turn prompt builders + the repo-audit preset for the
|
|
3
|
+
* orchestration harness (engine, epic #6691, step E-2).
|
|
4
|
+
*
|
|
5
|
+
* The PREAMBLE (role framing) goes in the session preamble, which is silently
|
|
6
|
+
* capped at SESSION_PREAMBLE_MAX_LENGTH (4000) — so preambles stay short and
|
|
7
|
+
* the structured-output CONTRACT travels in the per-turn prompts (which are not
|
|
8
|
+
* capped), appended via decision-contract's `decisionInstruction`.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { decisionInstruction } from './decision-contract.js'
|
|
12
|
+
|
|
13
|
+
export const ARCHITECT_PREAMBLE = [
|
|
14
|
+
'You are the ARCHITECT of a code-audit committee. You decompose an audit goal',
|
|
15
|
+
'into independent subtasks, review each worker\'s plan-of-attack before it runs,',
|
|
16
|
+
'review each worker\'s result after, and finally synthesize a report.',
|
|
17
|
+
'You never edit files yourself. Be concrete and terse. Every turn ends with a',
|
|
18
|
+
'single fenced `chroxy-decision` JSON block, exactly as the turn instructs —',
|
|
19
|
+
'no prose after it. Absence of a valid block is treated as a failure, never as',
|
|
20
|
+
'approval, so always emit one.',
|
|
21
|
+
].join(' ')
|
|
22
|
+
|
|
23
|
+
export const AUDIT_WORKER_PREAMBLE = [
|
|
24
|
+
'You are an AUDIT WORKER on a code-audit committee. You investigate one subtask',
|
|
25
|
+
'READ-ONLY: read and search the codebase, reason about it, and report findings.',
|
|
26
|
+
'You do NOT edit files, run mutating commands, or fetch the network. First you',
|
|
27
|
+
'propose a short plan-of-attack for the architect to approve; then you execute',
|
|
28
|
+
'and report a result summary. Every turn ends with a single fenced',
|
|
29
|
+
'`chroxy-decision` JSON block, exactly as the turn instructs — no prose after it.',
|
|
30
|
+
].join(' ')
|
|
31
|
+
|
|
32
|
+
export const IMPLEMENT_WORKER_PREAMBLE = [
|
|
33
|
+
'You are an IMPLEMENT WORKER on a code committee. You carry out one subtask by',
|
|
34
|
+
'editing files IN YOUR ISOLATED WORKTREE ONLY — never touch paths outside it.',
|
|
35
|
+
'First propose a short plan-of-attack for the architect to approve; then make',
|
|
36
|
+
'the change and report a concise summary of what you did and which files you',
|
|
37
|
+
'touched. Keep changes minimal and focused on the subtask. Every turn ends with',
|
|
38
|
+
'a single fenced `chroxy-decision` JSON block, exactly as the turn instructs —',
|
|
39
|
+
'no prose after it.',
|
|
40
|
+
].join(' ')
|
|
41
|
+
|
|
42
|
+
export function architectPreamble() { return ARCHITECT_PREAMBLE }
|
|
43
|
+
export function auditWorkerPreamble() { return AUDIT_WORKER_PREAMBLE }
|
|
44
|
+
export function implementWorkerPreamble() { return IMPLEMENT_WORKER_PREAMBLE }
|
|
45
|
+
|
|
46
|
+
const join = (...parts) => parts.filter((p) => p != null && p !== '').join('\n\n')
|
|
47
|
+
|
|
48
|
+
/** Architect: decompose the epic goal into subtasks. */
|
|
49
|
+
export function buildPlanPrompt({ goal, repoMap = '', maxSubtasks = 8 }) {
|
|
50
|
+
return join(
|
|
51
|
+
`Decompose this audit goal into at most ${maxSubtasks} INDEPENDENT subtasks, each a distinct area of the repo. Each subtask has role "audit".`,
|
|
52
|
+
`GOAL:\n${goal}`,
|
|
53
|
+
repoMap ? `REPO MAP (partial):\n${repoMap}` : null,
|
|
54
|
+
decisionInstruction('epic_plan'),
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Worker: propose a plan-of-attack for one subtask. */
|
|
59
|
+
export function buildPoaPrompt({ subtask }) {
|
|
60
|
+
return join(
|
|
61
|
+
`Propose a brief plan-of-attack for this audit subtask, then wait for approval.`,
|
|
62
|
+
`SUBTASK: ${subtask.title}\nGOAL: ${subtask.goal}`,
|
|
63
|
+
subtask.successCriteria ? `SUCCESS CRITERIA: ${subtask.successCriteria}` : null,
|
|
64
|
+
decisionInstruction('plan_of_attack'),
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Architect: review a worker's plan-of-attack. */
|
|
69
|
+
export function buildPoaReviewPrompt({ subtask, poa }) {
|
|
70
|
+
return join(
|
|
71
|
+
`Review this worker's plan-of-attack for subtask "${subtask.title}".`,
|
|
72
|
+
`PLAN:\n${poa.plan}`,
|
|
73
|
+
`Verdict: approve (proceed), revise (send feedback, same worker retries), redelegate (fresh worker), or escalate (to the user).`,
|
|
74
|
+
decisionInstruction('poa_review'),
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Worker: execute the (approved) subtask and report a result. */
|
|
79
|
+
export function buildExecutePrompt({ subtask, feedback = null }) {
|
|
80
|
+
const implement = subtask.role === 'implement'
|
|
81
|
+
return join(
|
|
82
|
+
implement
|
|
83
|
+
? `Carry out subtask "${subtask.title}" by editing files in your worktree, then report a concise summary of what you changed.`
|
|
84
|
+
: `Carry out the audit for subtask "${subtask.title}" and report your findings.`,
|
|
85
|
+
feedback ? `ARCHITECT FEEDBACK to address:\n${feedback}` : null,
|
|
86
|
+
decisionInstruction('work_result'),
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Architect: review a worker's result. For implement subtasks the orchestrator
|
|
91
|
+
* attaches a capped diff of the actual change. */
|
|
92
|
+
export function buildResultReviewPrompt({ subtask, result, diff = null }) {
|
|
93
|
+
const implement = subtask.role === 'implement'
|
|
94
|
+
return join(
|
|
95
|
+
`Review this worker's ${implement ? 'implementation' : 'audit'} result for subtask "${subtask.title}".`,
|
|
96
|
+
`RESULT:\n${result.summary}`,
|
|
97
|
+
diff && diff.patch ? `DIFF (${diff.truncated ? 'truncated' : 'full'}):\n${diff.stat || ''}\n${diff.patch}` : null,
|
|
98
|
+
`Verdict: approve (accept), revise (send feedback, same worker retries), redelegate (fresh worker), or escalate.`,
|
|
99
|
+
decisionInstruction('result_review'),
|
|
100
|
+
)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Fixup worker: resolve merge conflicts in the integration worktree. */
|
|
104
|
+
export function buildFixupPrompt({ subtask, conflictFiles = [] }) {
|
|
105
|
+
return join(
|
|
106
|
+
`A merge of subtask "${subtask.title}" into the integration branch hit conflicts.`,
|
|
107
|
+
`Resolve the conflicts in these files, keeping BOTH changes' intent where possible: ${conflictFiles.join(', ') || '(see git status)'}.`,
|
|
108
|
+
`Edit the files to remove all conflict markers. Do not commit — the orchestrator commits after verifying the tree is clean.`,
|
|
109
|
+
decisionInstruction('work_result'),
|
|
110
|
+
)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Architect: synthesize the accepted results into a report. */
|
|
114
|
+
export function buildSynthesisPrompt({ goal, results = [] }) {
|
|
115
|
+
const body = results.map((r, i) => `## ${i + 1}. ${r.title}\n${r.summary}`).join('\n\n')
|
|
116
|
+
return join(
|
|
117
|
+
`Synthesize the committee's audit results into one markdown report for this goal.`,
|
|
118
|
+
`GOAL:\n${goal}`,
|
|
119
|
+
`RESULTS:\n${body || '(none)'}`,
|
|
120
|
+
decisionInstruction('synthesis'),
|
|
121
|
+
)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export const REPO_AUDIT_PRESET = Object.freeze({
|
|
125
|
+
name: 'repo-audit',
|
|
126
|
+
goalTemplate:
|
|
127
|
+
'Perform a full self-audit of this repository: correctness bugs, security issues, '
|
|
128
|
+
+ 'dead code, missing test coverage, and documentation drift. Decompose the work by area.',
|
|
129
|
+
// Every subtask in an audit run is read-only, regardless of what the architect
|
|
130
|
+
// proposes — the engine coerces role:'implement' to 'audit' with a warning.
|
|
131
|
+
forceRole: 'audit',
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
export function presetFor(name) {
|
|
135
|
+
return name === 'repo-audit' ? REPO_AUDIT_PRESET : null
|
|
136
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure soft-budget evaluator for the orchestration harness (epic #6691, step
|
|
3
|
+
* M-3). NO I/O, NO clock — a deterministic function of (budget config, latch
|
|
4
|
+
* state, usage totals). The RunLedger owns the journaling of the latch events
|
|
5
|
+
* and the persistence; this module only decides the level and which one-shot
|
|
6
|
+
* events should fire.
|
|
7
|
+
*
|
|
8
|
+
* Soft semantics (locked product decision #4): `capped` blocks NEW delegations
|
|
9
|
+
* only — in-flight turns always complete and keep folding, so `effectiveUsd`
|
|
10
|
+
* may legitimately exceed the cap; it is recorded honestly, never clamped.
|
|
11
|
+
* Budget math uses `effectiveUsd` (provider costUsd + server-derived
|
|
12
|
+
* pricedCostUsd). Turns that are neither priced nor provider-costed increment
|
|
13
|
+
* `unknownCostTurns`, and unmeterable sessions land in `meteringGaps`, so a
|
|
14
|
+
* consumer can caveat "observed spend >= shown".
|
|
15
|
+
*
|
|
16
|
+
* Latches: `warnedAt`/`capReachedAt` fire the warn/cap events exactly once
|
|
17
|
+
* (the same one-shot posture as billing-budget.js's `justWarned`/`justExceeded`
|
|
18
|
+
* return flags, though the field names here are our own). A refund (#4099,
|
|
19
|
+
* signed cost) that drops `effectiveUsd` back under a threshold re-computes
|
|
20
|
+
* `level` (so a capped run can
|
|
21
|
+
* resume delegating) but never un-fires a latch — no warning spam.
|
|
22
|
+
*
|
|
23
|
+
* v1 config is `{ maxUsd, warnPercent }` only; per-role caps and token caps are
|
|
24
|
+
* a deferred fast-follow (#6720-adjacent).
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
export const DEFAULT_WARN_PERCENT = 80
|
|
28
|
+
|
|
29
|
+
export function makeBudgetState() {
|
|
30
|
+
return { warnedAt: null, capReachedAt: null, capLiftedAt: null, perRole: {} }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @param {{
|
|
35
|
+
* budget: { maxUsd: number|null, warnPercent?: number }|null,
|
|
36
|
+
* budgetState: { warnedAt: number|null, capReachedAt: number|null },
|
|
37
|
+
* totals: { effectiveUsd: number, costUsd: number, pricedCostUsd: number, unknownCostTurns: number },
|
|
38
|
+
* meteringGaps?: string[],
|
|
39
|
+
* role?: string|null,
|
|
40
|
+
* }} arg
|
|
41
|
+
* @returns BudgetEval
|
|
42
|
+
*/
|
|
43
|
+
export function evaluateBudget({ budget, budgetState, totals, meteringGaps = [], role = null }) {
|
|
44
|
+
const maxUsd = budget && Number.isFinite(budget.maxUsd) && budget.maxUsd > 0 ? budget.maxUsd : null
|
|
45
|
+
// Clamp to [0,100] so an out-of-range config can't warn-immediately (<=0) or
|
|
46
|
+
// effectively disable the warn tier (>100 pushing the threshold above the cap).
|
|
47
|
+
const rawWarn = budget && Number.isFinite(budget.warnPercent) ? budget.warnPercent : DEFAULT_WARN_PERCENT
|
|
48
|
+
const warnPercent = Math.min(100, Math.max(0, rawWarn))
|
|
49
|
+
const effectiveUsd = Number.isFinite(totals?.effectiveUsd) ? totals.effectiveUsd : 0
|
|
50
|
+
const spentUsd = Number.isFinite(totals?.costUsd) ? totals.costUsd : 0
|
|
51
|
+
const pricedUsd = Number.isFinite(totals?.pricedCostUsd) ? totals.pricedCostUsd : 0
|
|
52
|
+
|
|
53
|
+
let level = 'ok'
|
|
54
|
+
let percentUsd = null
|
|
55
|
+
if (maxUsd != null) {
|
|
56
|
+
percentUsd = (effectiveUsd / maxUsd) * 100
|
|
57
|
+
if (effectiveUsd >= maxUsd) level = 'capped'
|
|
58
|
+
else if (effectiveUsd >= maxUsd * (warnPercent / 100)) level = 'warned'
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const alreadyWarned = budgetState?.warnedAt != null
|
|
62
|
+
const alreadyCapped = budgetState?.capReachedAt != null
|
|
63
|
+
// warn fires once at the first warn-or-cap crossing; cap fires once at the
|
|
64
|
+
// first cap crossing. A run that jumps straight to capped fires BOTH.
|
|
65
|
+
const justWarned = (level === 'warned' || level === 'capped') && !alreadyWarned
|
|
66
|
+
const justExceeded = level === 'capped' && !alreadyCapped
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
ok: level !== 'capped',
|
|
70
|
+
level,
|
|
71
|
+
role,
|
|
72
|
+
spentUsd,
|
|
73
|
+
pricedUsd,
|
|
74
|
+
effectiveUsd,
|
|
75
|
+
percentUsd,
|
|
76
|
+
unknownCostTurns: Number.isFinite(totals?.unknownCostTurns) ? totals.unknownCostTurns : 0,
|
|
77
|
+
meteringGaps: Array.isArray(meteringGaps) ? [...meteringGaps] : [],
|
|
78
|
+
justWarned,
|
|
79
|
+
justExceeded,
|
|
80
|
+
}
|
|
81
|
+
}
|