forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.ts
More file actions
124 lines (108 loc) · 3.13 KB
/
executor.ts
File metadata and controls
124 lines (108 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { createHash } from "crypto"
import type { Tool } from "../tool/tool.js"
import type { Molecule } from "./types.js"
export namespace Executor {
export interface Context {
sessionID: string
messageID: string
agent: string
toolRegistry: Map<string, Tool.Info>
}
export interface ExecutionResult {
success: boolean
attestation: Molecule.Attestation
outputs: Map<string, any>
errors?: Error[]
}
function hash(content: string): string {
return createHash("sha256").update(content).digest("hex")
}
export async function execute(spec: Molecule.Spec, ctx: Context): Promise<ExecutionResult> {
const outputs = new Map<string, any>()
const errors: Error[] = []
const oracleResults: Molecule.OracleResult[] = []
const inputHash = hash(JSON.stringify({ spec, timestamp: Date.now() }))
for (const oracle of spec.oracles.filter((o) => o.type === "bash")) {
const result = await runBashOracle(oracle, ctx)
oracleResults.push(result)
if (!result.passed) {
errors.push(new Error(`Pre-oracle failed: ${oracle.check}`))
return {
success: false,
attestation: {
moleculeID: spec.id,
timestamp: Date.now(),
inputHash,
outputHash: "",
oracleResults,
success: false,
},
outputs,
errors,
}
}
}
for (const action of spec.actions) {
const tool = ctx.toolRegistry.get(action.toolID)
if (!tool) {
errors.push(new Error(`Tool not found: ${action.toolID}`))
continue
}
const toolImpl = await tool.init()
const result = await toolImpl.execute(action.params, {
sessionID: ctx.sessionID,
messageID: ctx.messageID,
agent: ctx.agent,
abort: new AbortController().signal,
metadata: () => {},
})
outputs.set(action.toolID, result)
}
const outputHash = hash(
JSON.stringify({
outputs: Array.from(outputs.entries()),
timestamp: Date.now(),
}),
)
const attestation: Molecule.Attestation = {
moleculeID: spec.id,
timestamp: Date.now(),
inputHash,
outputHash,
oracleResults,
success: errors.length === 0,
}
return {
success: errors.length === 0,
attestation,
outputs,
errors: errors.length > 0 ? errors : undefined,
}
}
async function runBashOracle(oracle: Molecule.OracleRef, ctx: Context): Promise<Molecule.OracleResult> {
const bashTool = ctx.toolRegistry.get("bash")
if (!bashTool) {
return {
oracle,
passed: false,
output: "Bash tool not available",
}
}
const toolImpl = await bashTool.init()
const result = await toolImpl.execute(
{ command: oracle.check, description: "Oracle check" },
{
sessionID: ctx.sessionID,
messageID: ctx.messageID,
agent: ctx.agent,
abort: new AbortController().signal,
metadata: () => {},
},
)
return {
oracle,
passed: result.metadata?.["exit"] === 0,
output: result.output,
}
}
}