Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b00839b

Browse files
fix(coderd/taskname): ensure generated name is under 32 bytes
1 parent 5c2022e commit b00839b

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

coderd/taskname/taskname.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const (
2424
Requirements:
2525
- Only lowercase letters, numbers, and hyphens
2626
- Start with "task-"
27-
- Maximum 28 characters total
27+
- Maximum 27 characters total
2828
- Descriptive of the main task
2929
3030
Examples:
@@ -145,17 +145,24 @@ func Generate(ctx context.Context, prompt string, opts ...Option) (string, error
145145
return "", ErrNoNameGenerated
146146
}
147147

148-
generatedName := acc.Messages()[0].Content
149-
150-
if err := codersdk.NameValid(generatedName); err != nil {
151-
return "", xerrors.Errorf("generated name %v not valid: %w", generatedName, err)
148+
// We append a suffix to the end of the task name to reduce
149+
// the chance of collisions. We truncate the task name to
150+
// to a maximum of 27 bytes, so that when we append the
151+
// 5 byte suffix (`-` and 4 byte hex sulg), it should
152+
// remain within the 32 byte workspace name limit.
153+
taskName := acc.Messages()[0].Content
154+
taskName = taskName[:min(len(taskName), 27)]
155+
taskName = fmt.Sprintf("%s-%s", taskName, generateSuffix())
156+
157+
if err := codersdk.NameValid(taskName); err != nil {
158+
return "", xerrors.Errorf("generated name %v not valid: %w", taskName, err)
152159
}
153160

154-
if generatedName == "task-unnamed" {
161+
if taskName == "task-unnamed" {
155162
return "", ErrNoNameGenerated
156163
}
157164

158-
return fmt.Sprintf("%s-%s", generatedName, generateSuffix()), nil
165+
return fmt.Sprintf("%s-%s", taskName, generateSuffix()), nil
159166
}
160167

161168
func anthropicDataStream(ctx context.Context, client anthropic.Client, model anthropic.Model, input []aisdk.Message) (aisdk.DataStream, error) {

0 commit comments

Comments
 (0)