Skip to content

Commit bf47e6d

Browse files
committed
fix(prompt): derive clawhub schema from worker
1 parent c5e5506 commit bf47e6d

2 files changed

Lines changed: 99 additions & 9 deletions

File tree

‎cmd/verify-clawhub-prompt/main.go‎

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111
"os/exec"
1212
"path/filepath"
13+
"regexp"
1314
"strings"
1415

1516
"github.com/openclaw/clawscan/internal/clawhubprompt"
@@ -42,10 +43,12 @@ func run() error {
4243
if err != nil {
4344
return err
4445
}
45-
systemPrompt, expected, err := renderClawHubPrompt(resolvedClawHubDir)
46+
rendered, err := renderClawHubPrompt(resolvedClawHubDir)
4647
if err != nil {
4748
return err
4849
}
50+
systemPrompt := rendered.SystemPrompt
51+
expected := rendered.Prompt
4952
job := proofJob()
5053
skillSpectorAnalysis := proofSkillSpectorAnalysis()
5154
actual, err := clawhubprompt.Build(systemPrompt, job, []string{"html-comment-injection"}, skillSpectorAnalysis)
@@ -71,10 +74,7 @@ func run() error {
7174
if err != nil {
7275
return err
7376
}
74-
schema, err := os.ReadFile(filepath.Join(resolvedClawHubDir, "scripts/security/codex-scan-output.schema.json"))
75-
if err != nil {
76-
return err
77-
}
77+
schema := rendered.OutputSchema
7878
requestBody, err := runner.OpenAIRequestBody(runner.OpenAIRequestOptions{Model: *model, Reasoning: *reasoning}, systemPrompt, prompt, schema)
7979
if err != nil {
8080
return err
@@ -179,7 +179,22 @@ func resolveClawHubDir(path string) (string, error) {
179179
return filepath.Abs(path)
180180
}
181181

182-
func renderClawHubPrompt(clawhubDir string) (string, string, error) {
182+
type clawHubPromptRender struct {
183+
SystemPrompt string
184+
Prompt string
185+
OutputSchema []byte
186+
}
187+
188+
var (
189+
workerSchemaPathPattern = regexp.MustCompile(`(?m)^\s*const\s+schemaPath\s*=\s*join\(\s*root\s*,\s*"([^"]+)"\s*\)`)
190+
workerOutputSchemaArgPattern = regexp.MustCompile(`(?s)"--output-schema"\s*,\s*schemaPath`)
191+
)
192+
193+
func renderClawHubPrompt(clawhubDir string) (clawHubPromptRender, error) {
194+
outputSchema, err := readClawHubWorkerOutputSchema(clawhubDir)
195+
if err != nil {
196+
return clawHubPromptRender{}, err
197+
}
183198
script := `
184199
const clawhubDir = process.argv[1];
185200
const worker = await import(clawhubDir + "/scripts/security/run-codex-scan-worker.ts");
@@ -222,16 +237,40 @@ console.log(JSON.stringify({
222237
cmd.Stderr = &stderr
223238
out, err := cmd.Output()
224239
if err != nil {
225-
return "", "", fmt.Errorf("render ClawHub prompt with bun: %w: %s", err, stderr.String())
240+
return clawHubPromptRender{}, fmt.Errorf("render ClawHub prompt with bun: %w: %s", err, stderr.String())
226241
}
227242
var payload struct {
228243
SystemPrompt string `json:"systemPrompt"`
229244
Prompt string `json:"prompt"`
230245
}
231246
if err := json.Unmarshal(out, &payload); err != nil {
232-
return "", "", err
247+
return clawHubPromptRender{}, err
248+
}
249+
return clawHubPromptRender{SystemPrompt: payload.SystemPrompt, Prompt: payload.Prompt, OutputSchema: outputSchema}, nil
250+
}
251+
252+
func readClawHubWorkerOutputSchema(clawhubDir string) ([]byte, error) {
253+
workerPath := filepath.Join(clawhubDir, "scripts/security/run-codex-scan-worker.ts")
254+
workerSource, err := os.ReadFile(workerPath)
255+
if err != nil {
256+
return nil, err
257+
}
258+
schemaRelPath, err := clawHubWorkerOutputSchemaRelPath(string(workerSource))
259+
if err != nil {
260+
return nil, fmt.Errorf("resolve ClawHub worker output schema: %w", err)
261+
}
262+
return os.ReadFile(filepath.Join(clawhubDir, filepath.FromSlash(schemaRelPath)))
263+
}
264+
265+
func clawHubWorkerOutputSchemaRelPath(workerSource string) (string, error) {
266+
if !workerOutputSchemaArgPattern.MatchString(workerSource) {
267+
return "", fmt.Errorf("worker no longer passes schemaPath to --output-schema")
268+
}
269+
matches := workerSchemaPathPattern.FindStringSubmatch(workerSource)
270+
if matches == nil {
271+
return "", fmt.Errorf("worker schemaPath declaration not found")
233272
}
234-
return payload.SystemPrompt, payload.Prompt, nil
273+
return matches[1], nil
235274
}
236275

237276
func proofJob() clawhubprompt.Job {

‎cmd/verify-clawhub-prompt/main_test.go‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,54 @@ func TestBuildPromptTemplateReplacesScannerFixtures(t *testing.T) {
6161
}
6262
}
6363
}
64+
65+
func TestClawHubWorkerOutputSchemaRelPath(t *testing.T) {
66+
workerSource := `
67+
const root = resolve(new URL("../..", import.meta.url).pathname);
68+
const schemaPath = join(root, "scripts/security/codex-scan-output.schema.json");
69+
const args = [
70+
"--output-schema",
71+
schemaPath,
72+
];
73+
`
74+
got, err := clawHubWorkerOutputSchemaRelPath(workerSource)
75+
if err != nil {
76+
t.Fatal(err)
77+
}
78+
79+
want := "scripts/security/codex-scan-output.schema.json"
80+
if got != want {
81+
t.Fatalf("schema path = %q, want %q", got, want)
82+
}
83+
}
84+
85+
func TestClawHubWorkerOutputSchemaRelPathRequiresWorkerSchemaArg(t *testing.T) {
86+
workerSource := `
87+
const root = resolve(new URL("../..", import.meta.url).pathname);
88+
const schemaPath = join(root, "scripts/security/codex-scan-output.schema.json");
89+
const args = [];
90+
`
91+
_, err := clawHubWorkerOutputSchemaRelPath(workerSource)
92+
if err == nil {
93+
t.Fatal("expected missing worker schema arg to fail")
94+
}
95+
if !strings.Contains(err.Error(), "schemaPath to --output-schema") {
96+
t.Fatalf("error = %q", err)
97+
}
98+
}
99+
100+
func TestClawHubWorkerOutputSchemaRelPathRequiresSchemaPathDeclaration(t *testing.T) {
101+
workerSource := `
102+
const args = [
103+
"--output-schema",
104+
schemaPath,
105+
];
106+
`
107+
_, err := clawHubWorkerOutputSchemaRelPath(workerSource)
108+
if err == nil {
109+
t.Fatal("expected missing schemaPath declaration to fail")
110+
}
111+
if !strings.Contains(err.Error(), "schemaPath declaration") {
112+
t.Fatalf("error = %q", err)
113+
}
114+
}

0 commit comments

Comments
 (0)