Simplify session lifecycle and MCP cleanup

This commit is contained in:
2026-05-14 20:51:37 +01:00
parent 27361f79c4
commit cc4bf9e904
16 changed files with 439 additions and 255 deletions

View File

@@ -210,19 +210,19 @@ func (s *Session) WaitForStable(timeout time.Duration) error {
}
func (s *Session) WaitForText(text string, timeout time.Duration) error {
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
return pollUntil(timeout, 25*time.Millisecond, func() (bool, error) {
screen, err := s.Screen()
if err != nil {
return err
return false, err
}
if strings.Contains(screen, text) {
return nil
return true, nil
}
time.Sleep(25 * time.Millisecond)
}
screen, _ := s.Screen()
return fmt.Errorf("text %q not found before timeout; screen:\n%s", text, screen)
return false, nil
}, func() error {
screen, _ := s.Screen()
return fmt.Errorf("text %q not found before timeout; screen:\n%s", text, screen)
})
}
func (s *Session) WaitForRegex(pattern string, timeout time.Duration) error {
@@ -230,19 +230,31 @@ func (s *Session) WaitForRegex(pattern string, timeout time.Duration) error {
if err != nil {
return err
}
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
return pollUntil(timeout, 25*time.Millisecond, func() (bool, error) {
screen, err := s.Screen()
if err != nil {
return err
return false, err
}
if re.MatchString(screen) {
return nil
return true, nil
}
time.Sleep(25 * time.Millisecond)
return false, nil
}, func() error {
screen, _ := s.Screen()
return fmt.Errorf("regex %q not found before timeout; screen:\n%s", pattern, screen)
})
}
func pollUntil(timeout, interval time.Duration, check func() (bool, error), timeoutErr func() error) error {
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
ok, err := check()
if err != nil || ok {
return err
}
time.Sleep(interval)
}
screen, _ := s.Screen()
return fmt.Errorf("regex %q not found before timeout; screen:\n%s", pattern, screen)
return timeoutErr()
}
func (s *Session) MCPCall(method string, params json.RawMessage) (json.RawMessage, error) {