T1b verb-timeout-serve: a verb served from a bundle waits the timeout its definition names
Tool gains RunWithin and Within; a bundle's tool call waits `within` where the seat verb it serves names a timeout, else CallTimeout. serveSeats takes the bound of a verb's key beside `chunked`. Builds: design 47 H §760, H-A-43 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
This commit is contained in:
@@ -46,6 +46,20 @@ type Tool struct {
|
||||
// Run calls the tool for caller: the bus user the call's subject named, or "" when it named none. A
|
||||
// caller is handed to the bundle in the call's `_meta` (MetaCaller), never among its arguments.
|
||||
Run func(caller string, args json.RawMessage) (json.RawMessage, error)
|
||||
// RunWithin is Run that waits within for the bundle's answer, the `timeout` the verb's seat definition names (design 47 H §760,
|
||||
// H-A-43); zero waits CallTimeout. A tool the runtime serves itself has none: Run is its call.
|
||||
RunWithin func(caller string, args json.RawMessage, within time.Duration) (json.RawMessage, error)
|
||||
}
|
||||
|
||||
// Within is the tool as called for a verb whose definition names a timeout: its Run waits within for the bundle's answer. A zero
|
||||
// within, and a tool that has no RunWithin, are the tool as it is.
|
||||
func (t Tool) Within(within time.Duration) Tool {
|
||||
if within <= 0 || t.RunWithin == nil {
|
||||
return t
|
||||
}
|
||||
run := t.RunWithin
|
||||
t.Run = func(caller string, args json.RawMessage) (json.RawMessage, error) { return run(caller, args, within) }
|
||||
return t
|
||||
}
|
||||
|
||||
// MetaCaller is where in a `tools/call`'s `_meta` the runtime names the caller it took from the subject the
|
||||
@@ -799,13 +813,17 @@ func Start(module, entry string, env []string, mesh Bus, logf func(string, ...an
|
||||
input = json.RawMessage("{}")
|
||||
}
|
||||
tool := Tool{Name: name, Description: t.Description, Input: input,
|
||||
Run: func(caller string, args json.RawMessage) (json.RawMessage, error) {
|
||||
Run: nil, RunWithin: func(caller string, args json.RawMessage, within time.Duration) (json.RawMessage, error) {
|
||||
if len(args) == 0 || string(args) == "null" {
|
||||
args = json.RawMessage("{}")
|
||||
}
|
||||
call, end := mesh.Begin(caller)
|
||||
defer end()
|
||||
res, err := asking("tools/call", callParams(full, caller, call, args), CallTimeout)
|
||||
bound := CallTimeout
|
||||
if within > 0 {
|
||||
bound = within
|
||||
}
|
||||
res, err := asking("tools/call", callParams(full, caller, call, args), bound)
|
||||
if err != nil {
|
||||
if errors.Is(err, errNoAnswer) {
|
||||
// Its caller is gone by now and nobody else would hear of it (issue 1966).
|
||||
@@ -841,6 +859,9 @@ func Start(module, entry string, env []string, mesh Bus, logf func(string, ...an
|
||||
b, _ := json.Marshal(text)
|
||||
return b, nil
|
||||
}}
|
||||
tool.Run = func(caller string, args json.RawMessage) (json.RawMessage, error) {
|
||||
return tool.RunWithin(caller, args, 0)
|
||||
}
|
||||
if _, seen := groups[under]; !seen {
|
||||
order = append(order, under)
|
||||
}
|
||||
|
||||
@@ -82,7 +82,8 @@ while IFS= read -r line; do
|
||||
case "$line" in
|
||||
*'"method":"initialize"'*) echo started >&2; printf '{"jsonrpc":"2.0","id":%s,"result":{}}\n' "$id" ;;
|
||||
*'"method":"tools/list"'*) printf '{"jsonrpc":"2.0","id":%s,"result":{"tools":[{"name":"t","description":"d"}]}}\n' "$id" ;;
|
||||
*'"method":"tools/call"'*) [ "$MODE" = long ] && printf '%4096s\n' x ;;
|
||||
*'"method":"tools/call"'*) [ "$MODE" = long ] && printf '%4096s\n' x
|
||||
[ "$MODE" = late ] && { sleep 0.3; printf '{"jsonrpc":"2.0","id":%s,"result":{"content":[{"type":"text","text":"{}"}]}}\n' "$id"; } ;;
|
||||
esac
|
||||
done
|
||||
`
|
||||
@@ -150,3 +151,28 @@ func TestAnOverlongLineAndAnUnansweredCallAreSaid(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Design 47 H §760: a verb served from a bundle waits the `timeout` its definition names, not the runner's own bound: a bundle
|
||||
// that answers after CallTimeout is answered through Within and fails through Run.
|
||||
func TestAToolWithinWaitsTheBoundItsVerbNames(t *testing.T) {
|
||||
oldCall := CallTimeout
|
||||
CallTimeout = 100 * time.Millisecond
|
||||
t.Cleanup(func() { CallTimeout = oldCall })
|
||||
dir := t.TempDir()
|
||||
entry := filepath.Join(dir, "bundle")
|
||||
if err := os.WriteFile(entry, []byte(callingBundle), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
l, err := Start("caller", entry, append(os.Environ(), "MODE=late"), &refusingState{}, func(string, ...any) {})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(l.Stop)
|
||||
tool := l.Registrations[0].Tools[0]
|
||||
if _, err := tool.Run("caller-1", nil); err == nil || !strings.Contains(err.Error(), "did not answer") {
|
||||
t.Fatalf("a call that waits the runner's own bound of 100ms: %v, want it unanswered", err)
|
||||
}
|
||||
if got, err := tool.Within(5*time.Second).Run("caller-1", nil); err != nil || string(got) != "{}" {
|
||||
t.Fatalf("a call that waits the verb's 5s: %s, %v, want its answer", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user