Repository navigation
FIX: recover missing commands in action and how-to replies #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package app | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/merefield/clai/internal/model" | ||
| "github.com/merefield/clai/internal/provider" | ||
| ) | ||
|
|
||
| func (a *Application) retryMissingCommand(ctx context.Context, kind string) (model.Reply, error) { | ||
| feedback := "The previous reply did not provide a usable command. Revisit the original request and put a suitable action or diagnostic command in cmd." | ||
| noCommand := "No command was proposed." | ||
| if kind == "how_to" { | ||
| feedback = "The previous reply did not provide a command example. Put the actual Bash example in cmd for the user to read. CLAI will display it as instructions." | ||
| noCommand = "No command example was provided." | ||
| } | ||
| text, err := a.requestCommandRetry(ctx, kind, feedback+" If you cannot provide one, explain the blocker or ask for the missing detail in info. Return one CLAI JSON reply without calling tools.") | ||
| if err != nil { | ||
| return model.Reply{}, fmt.Errorf("retry missing command: %w", err) | ||
| } | ||
| reply := parseReplyOrText(text) | ||
| if reply.Command == "" { | ||
| if detail := strings.TrimSpace(reply.Info); detail != "" { | ||
| noCommand += "\n\n" + detail | ||
| } | ||
| return model.Reply{Info: noCommand, Risk: model.RiskNone, Variables: []model.Variable{}}, nil | ||
| } | ||
| return reply, nil | ||
| } | ||
|
|
||
| func (a *Application) requestCommandRetry(ctx context.Context, kind, feedback string) (string, error) { | ||
| messages := append(a.messages(kind), model.TextMessage("user", feedback)) | ||
| response, err := a.Client.Complete(ctx, provider.Request{Messages: messages}) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| if len(response.ToolCalls) > 0 || response.FinishReason == "tool_calls" { | ||
| return "", fmt.Errorf("provider requested a tool during command retry") | ||
| } | ||
| return response.Text, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| package app | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/merefield/clai/internal/mcptools" | ||
| "github.com/merefield/clai/internal/model" | ||
| "github.com/merefield/clai/internal/provider" | ||
| "github.com/merefield/clai/internal/systemone" | ||
| "github.com/merefield/clai/internal/ui" | ||
| ) | ||
|
|
||
| func TestActionWithoutCommandRetriesBeforeNormalReview(t *testing.T) { | ||
| const query = "resolve LoadError Discourse requires libvips for image processing" | ||
| const diagnostic = "pkg-config --modversion vips" | ||
| for _, mode := range []string{"routed", "explicit"} { | ||
| t.Run(mode, func(t *testing.T) { | ||
| a, out, _ := contextFixture(t) | ||
| a.UI = ui.New(strings.NewReader("n\n"), out, out, true) | ||
| router := &fakeSystemOne{intent: systemone.IntentDecision{Intent: systemone.IntentExecute, Confidence: 1}, risk: systemone.RiskDecision{Risk: "none", Confidence: 1}} | ||
| a.SystemOne = router | ||
| args := []string{query} | ||
| if mode == "explicit" { | ||
| router.intent.Intent = systemone.IntentQuestion | ||
| args = []string{"--execute", query} | ||
| } | ||
| client := &fakeClient{responses: []provider.Response{ | ||
| {Text: `{"cmd":"","info":"Installs the libvips development package.","risk":"none","variables":[]}`}, | ||
| {Text: `{"cmd":"pkg-config --modversion vips","info":"Checks whether pkg-config can find libvips.","risk":"none","variables":[]}`}, | ||
| }} | ||
| a.Client = client | ||
| r := &fakeRunner{} | ||
| var checked string | ||
| r.check = func(command string) (string, error) { checked = command; return "", nil } | ||
| a.Runner = r | ||
| if err := a.Run(t.Context(), args); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if (len(router.intentRequests) == 0) != (mode == "explicit") || len(router.riskRequests) != 1 || router.riskRequests[0].Command != diagnostic { | ||
| t.Fatalf("intent=%v audit=%v", router.intentRequests, router.riskRequests) | ||
| } | ||
| if len(client.requests) != 2 || len(client.requests[1].Tools) != 0 || !strings.Contains(requestText(client.requests[1]), "did not provide a usable command") { | ||
| t.Fatalf("unexpected retry: %+v", client.requests) | ||
| } | ||
| if checked != diagnostic || len(r.calls) != 0 || !strings.Contains(out.String(), diagnostic) || !strings.Contains(out.String(), "execute command?") || strings.Contains(out.String(), "Installs the libvips") { | ||
| t.Fatalf("expected a checked proposal at the approval prompt: checked=%q ran=%v output=%s", checked, r.calls, out) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestActionStillWithoutCommandShowsBlockerAndStops(t *testing.T) { | ||
| for name, blocker := range map[string]string{ | ||
| "json": `{"cmd":"","info":"Which environment needs the dependency?","risk":"none","variables":[]}`, | ||
| "plain text": "Which environment needs the dependency?", | ||
| } { | ||
| t.Run(name, func(t *testing.T) { | ||
| a, out, _ := contextFixture(t) | ||
| client := &fakeClient{responses: []provider.Response{{Text: "Installs the dependency."}, {Text: blocker}}} | ||
| a.Client = client | ||
| if err := a.Run(t.Context(), []string{"--execute", "fix the dependency"}); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(client.requests) != 2 || len(a.Runner.(*fakeRunner).calls) != 0 || !strings.Contains(out.String(), "No command was proposed.") || !strings.Contains(out.String(), "Which environment needs the dependency?") { | ||
| t.Fatalf("expected one retry and a visible blocker: %s", out) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestHowToRetryDisplaysCommandExampleWithoutApprovingOrRunningIt(t *testing.T) { | ||
| a, out, _ := contextFixture(t) | ||
| a.Config.Set("risk_appetite", "2") | ||
| a.Config.Set("share_command_results", "true") | ||
| a.Config.Set("use_tools", "true") | ||
| a.Tools = testTools(t, fakeTool{}) | ||
| const query = "how to uninstall this?" | ||
| const command = "sudo apt remove imagemagick" | ||
| a.History.AppendText("user", "I installed ImageMagick using apt.") | ||
| router := &fakeSystemOne{intent: systemone.IntentDecision{Intent: systemone.IntentHowTo, Confidence: 1}} | ||
| a.SystemOne = router | ||
| client := &fakeClient{tools: true, responses: []provider.Response{ | ||
| {Text: `{"cmd":"","info":"Uninstalls ImageMagick and removes unused dependencies.","risk":"none","variables":[]}`}, | ||
| {Text: `{"cmd":"sudo apt remove imagemagick","info":"Removes the package. Review apt's proposed changes before confirming.","risk":"reversible change","variables":[]}`}, | ||
| }} | ||
| a.Client = client | ||
| r := &fakeRunner{check: func(command string) (string, error) { | ||
| t.Fatalf("how-to example entered the syntax check: %s", command) | ||
| return "", nil | ||
| }} | ||
| a.Runner = r | ||
| if err := a.Run(t.Context(), []string{query}); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(router.intentRequests) != 1 || len(router.riskRequests) != 0 || len(r.calls) != 0 || strings.Contains(out.String(), "execute command?") { | ||
| t.Fatalf("how-to example entered the execution flow: %v / %s", r.calls, out) | ||
| } | ||
| if len(client.requests) != 2 || !strings.Contains(requestText(client.requests[1]), "did not provide a command example") || !strings.Contains(requestText(client.requests[1]), "installed ImageMagick using apt") { | ||
| t.Fatalf("how-to retry lost the reason or conversation context: %+v", client.requests) | ||
| } | ||
| for _, request := range client.requests { | ||
| if len(request.Tools) != 0 || !strings.Contains(request.Messages[0].ContentText(), "tools are unavailable while preparing shell instructions") { | ||
| t.Fatalf("how-to request advertised tools: %+v", request) | ||
| } | ||
| } | ||
| if !strings.Contains(out.String(), command) || strings.Contains(out.String(), "removes unused dependencies") { | ||
| t.Fatalf("missing example or stale explanation: %s", out) | ||
| } | ||
| saved, err := ParseReply(a.History.Messages[len(a.History.Messages)-1].ContentText()) | ||
| if err != nil || saved.Command != "" || !strings.Contains(saved.Info, " "+command) { | ||
| t.Fatalf("example was not saved as inert instructions: %+v, %v", saved, err) | ||
| } | ||
| } | ||
|
|
||
| func TestHowToRejectsUnexpectedToolCallsBeforeDispatch(t *testing.T) { | ||
| a, out, _ := contextFixture(t) | ||
| a.Config.Set("use_tools", "true") | ||
| a.Tools = testTools(t, fakeTool{}) | ||
| manager, err := mcptools.New(t.TempDir(), out) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| t.Cleanup(func() { _ = manager.Close() }) | ||
| a.ToolManager = manager | ||
| a.SystemOne = &fakeSystemOne{intent: systemone.IntentDecision{Intent: systemone.IntentHowTo, Confidence: 1}} | ||
| client := &fakeClient{tools: true, responses: []provider.Response{ | ||
| {ToolCalls: []model.ToolCall{{ID: "unexpected", Function: model.FunctionCall{Name: "lookup", Arguments: `{}`}}}}, | ||
| {Text: `{"cmd":"printf later","info":"An example.","risk":"none","variables":[]}`}, | ||
| }} | ||
| a.Client = client | ||
| err = a.Run(t.Context(), []string{"how do I uninstall this?"}) | ||
| if err == nil || !strings.Contains(err.Error(), "provider requested a tool while preparing shell instructions") { | ||
| t.Fatalf("expected a rejected tool call, got %v", err) | ||
| } | ||
| if a.toolsLoaded || len(client.requests) != 1 || len(a.History.Messages) != 1 || len(a.History.Messages[0].ToolCalls) != 0 { | ||
| t.Fatalf("how-to request entered the tool workflow: loaded=%v requests=%d history=%+v", a.toolsLoaded, len(client.requests), a.History.Messages) | ||
| } | ||
| } | ||
|
|
||
| func TestExecuteOptionRequiresRequestAndConflictsWithQuestion(t *testing.T) { | ||
| for _, args := range [][]string{{"--execute"}, {"--question", "--execute", "test"}, {"--execute", "--question", "test"}} { | ||
| if err := (&Application{}).Run(t.Context(), args); err == nil { | ||
| t.Errorf("accepted invalid options: %v", args) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.