on main help sign in
docs: add comment and commit notes to CRUSH.md
06a77d6Christian Rocha
CRUSH.md+7
@@ -60,6 +60,13 @@ func TestYourFunction(t *testing.T) {
60 - You can also use `task fmt` to run `gofumpt -w .` on the entire project,
60 - You can also use `task fmt` to run `gofumpt -w .` on the entire project,
61 as long as `gofumpt` is on the `PATH`.
61 as long as `gofumpt` is on the `PATH`.
62
62
63+## Comments
64+
65+- Comments that live one their own lines should start with capital letters and
66+ end with periods. Wrap comments at 78 columns.
67+
63## Committing
68## Committing
64
69
65- ALWAYS use semantic commits (`fix:`, `feat:`, `chore:`, `refactor:`, `docs:`, `sec:`, etc).
70- ALWAYS use semantic commits (`fix:`, `feat:`, `chore:`, `refactor:`, `docs:`, `sec:`, etc).
71+- Try to keep commits to one line, not including your attribution. Only use
72+ multi-line commits when additional context is truly necessary.
internal/agent/agent.go+24−22
@@ -131,7 +131,7 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
131 }
131 }
132
132
133 if len(a.tools) > 0 {
133 if len(a.tools) > 0 {
134− // add anthropic caching to the last tool
134+ // Add anthropic caching to the last tool.
135 a.tools[len(a.tools)-1].SetProviderOptions(a.getCacheControlOptions())
135 a.tools[len(a.tools)-1].SetProviderOptions(a.getCacheControlOptions())
136 }
136 }
137
137
@@ -153,7 +153,7 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
153 }
153 }
154
154
155 var wg sync.WaitGroup
155 var wg sync.WaitGroup
156− // Generate title if first message
156+ // Generate title if first message.
157 if len(msgs) == 0 {
157 if len(msgs) == 0 {
158 wg.Go(func() {
158 wg.Go(func() {
159 sessionLock.Lock()
159 sessionLock.Lock()
@@ -162,13 +162,13 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
162 })
162 })
163 }
163 }
164
164
165− // Add the user message to the session
165+ // Add the user message to the session.
166 _, err = a.createUserMessage(ctx, call)
166 _, err = a.createUserMessage(ctx, call)
167 if err != nil {
167 if err != nil {
168 return nil, err
168 return nil, err
169 }
169 }
170
170
171− // add the session to the context
171+ // Add the session to the context.
172 ctx = context.WithValue(ctx, tools.SessionIDContextKey, call.SessionID)
172 ctx = context.WithValue(ctx, tools.SessionIDContextKey, call.SessionID)
173
173
174 genCtx, cancel := context.WithCancel(ctx)
174 genCtx, cancel := context.WithCancel(ctx)
@@ -195,10 +195,10 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
195 PresencePenalty: call.PresencePenalty,
195 PresencePenalty: call.PresencePenalty,
196 TopK: call.TopK,
196 TopK: call.TopK,
197 FrequencyPenalty: call.FrequencyPenalty,
197 FrequencyPenalty: call.FrequencyPenalty,
198− // Before each step create the new assistant message
198+ // Before each step create a new assistant message.
199 PrepareStep: func(callContext context.Context, options fantasy.PrepareStepFunctionOptions) (_ context.Context, prepared fantasy.PrepareStepResult, err error) {
199 PrepareStep: func(callContext context.Context, options fantasy.PrepareStepFunctionOptions) (_ context.Context, prepared fantasy.PrepareStepResult, err error) {
200 prepared.Messages = options.Messages
200 prepared.Messages = options.Messages
201− // reset all cached items
201+ // Reset all cached items.
202 for i := range prepared.Messages {
202 for i := range prepared.Messages {
203 prepared.Messages[i].ProviderOptions = nil
203 prepared.Messages[i].ProviderOptions = nil
204 }
204 }
@@ -216,14 +216,14 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
216 lastSystemRoleInx := 0
216 lastSystemRoleInx := 0
217 systemMessageUpdated := false
217 systemMessageUpdated := false
218 for i, msg := range prepared.Messages {
218 for i, msg := range prepared.Messages {
219− // only add cache control to the last message
219+ // Only add cache control to the last message.
220 if msg.Role == fantasy.MessageRoleSystem {
220 if msg.Role == fantasy.MessageRoleSystem {
221 lastSystemRoleInx = i
221 lastSystemRoleInx = i
222 } else if !systemMessageUpdated {
222 } else if !systemMessageUpdated {
223 prepared.Messages[lastSystemRoleInx].ProviderOptions = a.getCacheControlOptions()
223 prepared.Messages[lastSystemRoleInx].ProviderOptions = a.getCacheControlOptions()
224 systemMessageUpdated = true
224 systemMessageUpdated = true
225 }
225 }
226− // than add cache control to the last 2 messages
226+ // Than add cache control to the last 2 messages.
227 if i > len(prepared.Messages)-3 {
227 if i > len(prepared.Messages)-3 {
228 prepared.Messages[i].ProviderOptions = a.getCacheControlOptions()
228 prepared.Messages[i].ProviderOptions = a.getCacheControlOptions()
229 }
229 }
@@ -394,10 +394,10 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
394 if currentAssistant == nil {
394 if currentAssistant == nil {
395 return result, err
395 return result, err
396 }
396 }
397− // Ensure we finish thinking on error to close the reasoning state
397+ // Ensure we finish thinking on error to close the reasoning state.
398 currentAssistant.FinishThinking()
398 currentAssistant.FinishThinking()
399 toolCalls := currentAssistant.ToolCalls()
399 toolCalls := currentAssistant.ToolCalls()
400− // INFO: we use the parent context here because the genCtx has been cancelled
400+ // INFO: we use the parent context here because the genCtx has been cancelled.
401 msgs, createErr := a.messages.List(ctx, currentAssistant.SessionID)
401 msgs, createErr := a.messages.List(ctx, currentAssistant.SessionID)
402 if createErr != nil {
402 if createErr != nil {
403 return nil, createErr
403 return nil, createErr
@@ -459,7 +459,8 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
459 } else {
459 } else {
460 currentAssistant.AddFinish(message.FinishReasonError, "API Error", err.Error())
460 currentAssistant.AddFinish(message.FinishReasonError, "API Error", err.Error())
461 }
461 }
462− // INFO: we use the parent context here because the genCtx has been cancelled
462+ // Note: we use the parent context here because the genCtx has been
463+ // cancelled.
463 updateErr := a.messages.Update(ctx, *currentAssistant)
464 updateErr := a.messages.Update(ctx, *currentAssistant)
464 if updateErr != nil {
465 if updateErr != nil {
465 return nil, updateErr
466 return nil, updateErr
@@ -473,7 +474,7 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
473 if summarizeErr := a.Summarize(genCtx, call.SessionID, call.ProviderOptions); summarizeErr != nil {
474 if summarizeErr := a.Summarize(genCtx, call.SessionID, call.ProviderOptions); summarizeErr != nil {
474 return nil, summarizeErr
475 return nil, summarizeErr
475 }
476 }
476− // if the agent was not done...
477+ // If the agent wasn't done...
477 if len(currentAssistant.ToolCalls()) > 0 {
478 if len(currentAssistant.ToolCalls()) > 0 {
478 existing, ok := a.messageQueue.Get(call.SessionID)
479 existing, ok := a.messageQueue.Get(call.SessionID)
479 if !ok {
480 if !ok {
@@ -485,7 +486,7 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
485 }
486 }
486 }
487 }
487
488
488− // release active request before processing queued messages
489+ // Release active request before processing queued messages.
489 a.activeRequests.Del(call.SessionID)
490 a.activeRequests.Del(call.SessionID)
490 cancel()
491 cancel()
491
492
@@ -493,7 +494,7 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
493 if !ok || len(queuedMessages) == 0 {
494 if !ok || len(queuedMessages) == 0 {
494 return result, err
495 return result, err
495 }
496 }
496− // there are queued messages restart the loop
497+ // There are queued messages restart the loop.
497 firstQueuedMessage := queuedMessages[0]
498 firstQueuedMessage := queuedMessages[0]
498 a.messageQueue.Set(call.SessionID, queuedMessages[1:])
499 a.messageQueue.Set(call.SessionID, queuedMessages[1:])
499 return a.Run(ctx, firstQueuedMessage)
500 return a.Run(ctx, firstQueuedMessage)
@@ -513,7 +514,7 @@ func (a *sessionAgent) Summarize(ctx context.Context, sessionID string, opts fan
513 return err
514 return err
514 }
515 }
515 if len(msgs) == 0 {
516 if len(msgs) == 0 {
516− // nothing to summarize
517+ // Nothing to summarize.
517 return nil
518 return nil
518 }
519 }
519
520
@@ -553,7 +554,7 @@ func (a *sessionAgent) Summarize(ctx context.Context, sessionID string, opts fan
553 return a.messages.Update(genCtx, summaryMessage)
554 return a.messages.Update(genCtx, summaryMessage)
554 },
555 },
555 OnReasoningEnd: func(id string, reasoning fantasy.ReasoningContent) error {
556 OnReasoningEnd: func(id string, reasoning fantasy.ReasoningContent) error {
556− // handle anthropic signature
557+ // Handle anthropic signature.
557 if anthropicData, ok := reasoning.ProviderMetadata["anthropic"]; ok {
558 if anthropicData, ok := reasoning.ProviderMetadata["anthropic"]; ok {
558 if signature, ok := anthropicData.(*anthropic.ReasoningOptionMetadata); ok && signature.Signature != "" {
559 if signature, ok := anthropicData.(*anthropic.ReasoningOptionMetadata); ok && signature.Signature != "" {
559 summaryMessage.AppendReasoningSignature(signature.Signature)
560 summaryMessage.AppendReasoningSignature(signature.Signature)
@@ -570,7 +571,7 @@ func (a *sessionAgent) Summarize(ctx context.Context, sessionID string, opts fan
570 if err != nil {
571 if err != nil {
571 isCancelErr := errors.Is(err, context.Canceled)
572 isCancelErr := errors.Is(err, context.Canceled)
572 if isCancelErr {
573 if isCancelErr {
573− // User cancelled summarize we need to remove the summary message
574+ // User cancelled summarize we need to remove the summary message.
574 deleteErr := a.messages.Delete(ctx, summaryMessage.ID)
575 deleteErr := a.messages.Delete(ctx, summaryMessage.ID)
575 return deleteErr
576 return deleteErr
576 }
577 }
@@ -597,7 +598,7 @@ func (a *sessionAgent) Summarize(ctx context.Context, sessionID string, opts fan
597
598
598 a.updateSessionUsage(a.largeModel, &currentSession, resp.TotalUsage, openrouterCost)
599 a.updateSessionUsage(a.largeModel, &currentSession, resp.TotalUsage, openrouterCost)
599
600
600− // just in case get just the last usage
601+ // Just in case, get just the last usage info.
601 usage := resp.Response.Usage
602 usage := resp.Response.Usage
602 currentSession.SummaryMessageID = summaryMessage.ID
603 currentSession.SummaryMessageID = summaryMessage.ID
603 currentSession.CompletionTokens = usage.OutputTokens
604 currentSession.CompletionTokens = usage.OutputTokens
@@ -643,7 +644,8 @@ func (a *sessionAgent) preparePrompt(msgs []message.Message, attachments ...mess
643 if len(m.Parts) == 0 {
644 if len(m.Parts) == 0 {
644 continue
645 continue
645 }
646 }
646− // Assistant message without content or tool calls (cancelled before it returned anything)
647+ // Assistant message without content or tool calls (cancelled before it
648+ // returned anything).
647 if m.Role == message.Assistant && len(m.ToolCalls()) == 0 && m.Content().Text == "" && m.ReasoningContent().String() == "" {
649 if m.Role == message.Assistant && len(m.ToolCalls()) == 0 && m.Content().Text == "" && m.ReasoningContent().String() == "" {
648 continue
650 continue
649 }
651 }
@@ -718,7 +720,7 @@ func (a *sessionAgent) generateTitle(ctx context.Context, session *session.Sessi
718
720
719 title = strings.ReplaceAll(title, "\n", " ")
721 title = strings.ReplaceAll(title, "\n", " ")
720
722
721− // remove thinking tags if present
723+ // Remove thinking tags if present.
722 if idx := strings.Index(title, "</think>"); idx > 0 {
724 if idx := strings.Index(title, "</think>"); idx > 0 {
723 title = title[idx+len("</think>"):]
725 title = title[idx+len("</think>"):]
724 }
726 }
@@ -784,13 +786,13 @@ func (a *sessionAgent) updateSessionUsage(model Model, session *session.Session,
784}
786}
785
787
786func (a *sessionAgent) Cancel(sessionID string) {
788func (a *sessionAgent) Cancel(sessionID string) {
787− // Cancel regular requests
789+ // Cancel regular requests.
788 if cancel, ok := a.activeRequests.Take(sessionID); ok && cancel != nil {
790 if cancel, ok := a.activeRequests.Take(sessionID); ok && cancel != nil {
789 slog.Info("Request cancellation initiated", "session_id", sessionID)
791 slog.Info("Request cancellation initiated", "session_id", sessionID)
790 cancel()
792 cancel()
791 }
793 }
792
794
793− // Also check for summarize requests
795+ // Also check for summarize requests.
794 if cancel, ok := a.activeRequests.Take(sessionID + "-summarize"); ok && cancel != nil {
796 if cancel, ok := a.activeRequests.Take(sessionID + "-summarize"); ok && cancel != nil {
795 slog.Info("Summarize cancellation initiated", "session_id", sessionID)
797 slog.Info("Summarize cancellation initiated", "session_id", sessionID)
796 cancel()
798 cancel()
internal/app/app.go+2−1
@@ -142,7 +142,8 @@ func (app *App) RunNonInteractive(ctx context.Context, output io.Writer, prompt
142 }
142 }
143 slog.Info("Created session for non-interactive run", "session_id", sess.ID)
143 slog.Info("Created session for non-interactive run", "session_id", sess.ID)
144
144
145− // Automatically approve all permission requests for this non-interactive session
145+ // Automatically approve all permission requests for this non-interactive
146+ // session.
146 app.Permissions.AutoApproveSession(sess.ID)
147 app.Permissions.AutoApproveSession(sess.ID)
147
148
148 type response struct {
149 type response struct {