Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion cmd/contr/hooksession.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"io"
"os"
"regexp"
"runtime"
"strings"
"time"
Expand Down Expand Up @@ -95,7 +96,7 @@ func promptHook(stdin io.Reader, stdout, stderr io.Writer, stateDir, id string,
return nil
}
text := youContext
if strings.HasPrefix(strings.TrimSpace(in.Prompt), "<hc from=") || strings.TrimSpace(in.Prompt) == "" {
if !ownerWords(in.Prompt) {
text = ""
}
if flow != "" {
Expand All @@ -107,6 +108,15 @@ func promptHook(stdin io.Reader, stdout, stderr io.Writer, stateDir, id string,
return writeContext(stdout, "UserPromptSubmit", text)
}

// notePattern matches the blocks of a prompt that hold no request of the
// owner: a background-task notification, a system reminder and an hc note.
var notePattern = regexp.MustCompile(`(?s)<task-notification>.*?</task-notification>|</?system-reminder>|<hc from="[^"]*">.*?</hc>`)

// ownerWords reports whether the prompt holds text other than notes.
func ownerWords(prompt string) bool {
return strings.TrimSpace(notePattern.ReplaceAllString(prompt, "")) != ""
}

func postToolHook(stdin io.Reader, stateDir, id string, now uint64) error {
var in toolInput
if err := json.NewDecoder(stdin).Decode(&in); err != nil {
Expand Down
18 changes: 18 additions & 0 deletions cmd/contr/hooksession_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,24 @@ func TestPromptHook(t *testing.T) {
if out, _ := runSession(t, dir, "prompt", `{"prompt":"<hc from=\"t2\">done</hc>"}`); out != "" {
t.Fatalf("hc report got %q", out)
}
for _, p := range []string{
`<task-notification>\n<task-id>b1</task-id>\n</task-notification>`,
`<system-reminder>\n<task-notification>done</task-notification>\n</system-reminder>`,
`<hc from=\"t2\">a</hc>\n<hc from=\"t3\">b</hc>`,
` `,
} {
if out, _ := runSession(t, dir, "prompt", `{"prompt":"`+p+`"}`); out != "" {
t.Fatalf("note %q got %q", p, out)
}
}
for _, p := range []string{
`<task-notification>done</task-notification>\nnow fix the bug`,
`<hc from=\"t2\">done</hc> merge it`,
} {
if out, _ := runSession(t, dir, "prompt", `{"prompt":"`+p+`"}`); !strings.Contains(out, `[you]`) {
t.Fatalf("owner words %q got %q", p, out)
}
}
if _, e := runSession(t, dir, "prompt", `nope`); !strings.Contains(e, "bad hook input") {
t.Fatalf("stderr %q", e)
}
Expand Down
Loading