-
Notifications
You must be signed in to change notification settings - Fork 296
Fix Factory fallback tool use IDs #942
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
6 commits
Select commit
Hold shift + click to select a range
969fc00
test: add factory worker hook regression e2e
gtrrz-victor 36fa16f
fix: handle factory worker hook payloads
gtrrz-victor 49ffb48
Fix Factory fallback tool use IDs
gtrrz-victor e5ffb95
fix: resolve lint and architecture test violations in factoryaidroid
gtrrz-victor aa9fdd0
fix: use bytes-based null check in parseHookToolResponseAgentID
gtrrz-victor 58cc949
Merge branch 'main' into fix-factory-tool-use-hooks
gtrrz-victor 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
147 changes: 147 additions & 0 deletions
147
cmd/entire/cli/agent/factoryaidroid/tool_use_fallback.go
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 factoryaidroid | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/rand" | ||
| "encoding/hex" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/entireio/cli/cmd/entire/cli/paths" | ||
| ) | ||
|
|
||
| const fallbackToolUseStatePrefix = "factory-task-tool-use-" | ||
|
|
||
| type fallbackToolUseState struct { | ||
| Entries []fallbackToolUseEntry `json:"entries"` | ||
| } | ||
|
|
||
| type fallbackToolUseEntry struct { | ||
| Fingerprint string `json:"fingerprint"` | ||
| ToolUseID string `json:"tool_use_id"` | ||
| } | ||
|
|
||
| func registerFallbackToolUseID( | ||
| ctx context.Context, | ||
| sessionID, toolName string, | ||
| toolInput json.RawMessage, | ||
| ) (string, error) { | ||
| statePath, err := fallbackToolUseStatePath(ctx, sessionID) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| state, err := loadFallbackToolUseState(statePath) | ||
| if err != nil { | ||
| if errors.Is(err, os.ErrNotExist) { | ||
| state = &fallbackToolUseState{} | ||
| } else { | ||
| return "", err | ||
| } | ||
| } | ||
|
|
||
| toolUseID, err := newFallbackToolUseID() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| state.Entries = append(state.Entries, fallbackToolUseEntry{ | ||
| Fingerprint: fallbackToolFingerprint(toolName, toolInput), | ||
| ToolUseID: toolUseID, | ||
| }) | ||
| if err := saveFallbackToolUseState(statePath, state); err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| return toolUseID, nil | ||
| } | ||
|
|
||
| func resolveFallbackToolUseID( | ||
| ctx context.Context, | ||
| sessionID, toolName string, | ||
| toolInput json.RawMessage, | ||
| ) (string, error) { | ||
| statePath, err := fallbackToolUseStatePath(ctx, sessionID) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| state, err := loadFallbackToolUseState(statePath) | ||
| if err != nil { | ||
| if errors.Is(err, os.ErrNotExist) { | ||
| return fallbackToolUseID(sessionID, toolName, toolInput), nil | ||
| } | ||
| return "", err | ||
| } | ||
|
|
||
| fingerprint := fallbackToolFingerprint(toolName, toolInput) | ||
| for i := len(state.Entries) - 1; i >= 0; i-- { | ||
| if state.Entries[i].Fingerprint != fingerprint { | ||
| continue | ||
| } | ||
|
|
||
| toolUseID := state.Entries[i].ToolUseID | ||
| state.Entries = append(state.Entries[:i], state.Entries[i+1:]...) | ||
| if err := saveFallbackToolUseState(statePath, state); err != nil { | ||
| return "", err | ||
| } | ||
| return toolUseID, nil | ||
| } | ||
|
|
||
| return fallbackToolUseID(sessionID, toolName, toolInput), nil | ||
| } | ||
|
|
||
| func newFallbackToolUseID() (string, error) { | ||
| var suffix [8]byte | ||
| if _, err := rand.Read(suffix[:]); err != nil { | ||
| return "", fmt.Errorf("generate fallback tool_use_id: %w", err) | ||
| } | ||
| return "factorytask_" + hex.EncodeToString(suffix[:]), nil | ||
| } | ||
|
|
||
| func fallbackToolUseStatePath(ctx context.Context, sessionID string) (string, error) { | ||
| tmpDir, err := paths.AbsPath(ctx, paths.EntireTmpDir) | ||
| if err != nil { | ||
| return "", fmt.Errorf("resolve fallback tool_use_id tmp dir: %w", err) | ||
| } | ||
| if err := os.MkdirAll(tmpDir, 0o750); err != nil { | ||
| return "", fmt.Errorf("create fallback tool_use_id tmp dir: %w", err) | ||
| } | ||
|
|
||
| sessionHash := fallbackToolUseID(sessionID, "", nil) | ||
| return filepath.Join(tmpDir, fallbackToolUseStatePrefix+sessionHash+".json"), nil | ||
| } | ||
|
|
||
| func loadFallbackToolUseState(path string) (*fallbackToolUseState, error) { | ||
| data, err := os.ReadFile(filepath.Clean(path)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("read fallback tool_use_id state: %w", err) | ||
| } | ||
|
|
||
| var state fallbackToolUseState | ||
| if err := json.Unmarshal(data, &state); err != nil { | ||
| return nil, fmt.Errorf("unmarshal fallback tool_use_id state: %w", err) | ||
| } | ||
| return &state, nil | ||
| } | ||
|
|
||
| func saveFallbackToolUseState(path string, state *fallbackToolUseState) error { | ||
| if len(state.Entries) == 0 { | ||
| if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) { | ||
| return fmt.Errorf("remove empty fallback tool_use_id state: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| data, err := json.Marshal(state) | ||
| if err != nil { | ||
| return fmt.Errorf("marshal fallback tool_use_id state: %w", err) | ||
| } | ||
| if err := os.WriteFile(path, data, 0o600); err != nil { | ||
| return fmt.Errorf("write fallback tool_use_id state: %w", err) | ||
| } | ||
| return nil | ||
| } |
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.