Michael Suchacz
67d586bb13
refactor(coderd): share the chat prompt length check ( #31020 )
...
Five `PUT` handlers wrote the same `400` when the sanitized text is
longer than the deployment's prompt byte limit: the deployment system
prompt, the organization system prompt, plan mode instructions, the user
custom prompt, and chat project instructions. This PR moves that check
into `api.writePromptTooLongError`, which takes the sanitized text and
each handler's message. It adds 16 lines and removes 28, and drops two
comments that only said the limits match.
Each handler still reads its body and calls
`codersdk.SanitizePromptText` itself, because it keeps using the
sanitized text. Moving the sanitize call into the helper would not
remove any more lines: each call site would still need the sanitized
value and the early return.
**Why it is safe:** the messages, the detail text, and the limit are
unchanged. I ran 6 mutants with `go test -overlay`: reject a text of
exactly the limit, change the message or the detail in the helper, and
change the project instructions, custom prompt, and plan mode messages
at their call sites. The tests fail on each.
**Review focus:** the boolean result. `true` means the helper wrote the
`400`, and every call site returns on it.
Validation: on `main` and on this branch, `go test ./coderd -run
'^(TestChatLimitsFromDeploymentConfig|TestChatProjectInstructions|TestChatSystemPrompt|TestChatPlanModeInstructions)$'
-count=1` passes (4 tests, 68 with subtests) and `go test
./enterprise/coderd -run
'^(TestOrganizationChatSystemPrompt|TestChatProjectInstructionsUpdateOnlyRole|TestChatProjectInstructionsAudit)$'
-count=1` passes (3 tests, 14 with subtests). The pre-commit hook passed
(`make gen`, formatting, golangci-lint, slim build).
> Xum, an AI agent, opened this PR for @ibetitsmike and wrote this
description.
2026-10-27 16:23:59 +01:00
..
2026-10-27 11:44:25 +00:00
2026-10-22 15:13:24 +00:00
2026-10-26 11:40:51 +02:00
2026-10-26 11:40:51 +02:00
2026-09-29 11:48:52 +02:00
2026-09-11 01:48:56 +00:00
2026-10-27 11:44:25 +00:00
2026-10-07 11:43:19 +03:00
2026-09-02 11:29:35 +02:00
2026-10-18 12:58:39 +07:00
2026-08-21 23:46:27 -06:00
2026-10-15 14:20:44 -05:00
2026-06-26 07:24:21 -04:00
2026-06-25 15:41:09 -06:00
2026-10-13 07:57:56 +00:00
2026-10-27 10:23:01 +01:00
2026-10-16 00:21:40 -08:00
2026-10-05 16:52:36 -07:00
2026-10-27 14:04:37 +03:00
2026-10-13 07:57:56 +00:00
2026-10-09 16:42:41 -06:00
2026-10-13 07:57:56 +00:00
2026-09-16 14:52:21 -07:00
2026-10-19 14:37:36 +11:00
2026-10-27 01:50:56 +01:00
2026-09-27 23:34:26 +02:00
2026-08-21 23:46:27 -06:00
2026-10-14 16:34:56 +05:00
2026-10-19 17:59:18 +03:00
2026-10-20 19:50:13 +02:00
2026-10-18 18:46:46 +02:00
2026-10-22 16:37:05 +11:00
2026-07-30 08:37:45 +01:00
2026-10-27 09:58:36 +00:00
2026-08-10 14:43:16 -05:00
2026-10-27 11:44:25 +00:00
2026-08-11 11:04:51 -05:00
2026-10-22 13:58:42 +00:00
2026-08-25 08:12:00 -07:00
2026-09-17 18:14:08 +02:00
2026-10-11 19:57:49 +02:00
2026-10-27 14:04:37 +03:00
2026-10-15 15:42:33 -07:00
2026-10-14 12:35:48 -04:00
2026-10-19 17:59:18 +03:00
2026-10-02 16:40:24 -08:00
2026-08-25 16:40:41 +07:00
2026-06-10 02:18:44 -05:00
2026-10-05 12:15:42 +00:00
2026-10-21 14:07:02 -07:00
2026-10-16 00:21:40 -08:00
2026-08-03 15:56:56 -05:00
2026-10-15 17:40:00 -05:00
2026-10-26 21:48:36 +00:00
2026-10-15 14:20:44 -05:00
2026-07-07 09:18:30 -07:00
2026-07-07 09:18:30 -07:00
2026-10-27 16:16:30 +01:00
2026-09-23 20:08:31 +02:00
2026-09-09 10:16:24 +02:00
2026-07-06 23:00:09 +01:00
2026-09-11 16:03:44 +00:00
2026-09-11 16:03:44 +00:00
2026-09-29 09:20:31 +01:00
2026-05-27 11:58:43 +02:00
2026-09-29 09:20:31 +01:00
2026-09-29 09:20:31 +01:00
2026-05-25 18:04:12 +02:00
2026-09-14 13:47:48 -07:00
2026-10-07 11:43:19 +03:00
2026-10-07 11:43:19 +03:00
2026-05-05 02:54:03 +05:00
2026-05-28 18:37:57 +01:00
2026-08-19 19:46:23 +00:00
2026-09-11 01:48:56 +00:00
2026-08-06 09:18:46 -04:00
2026-08-06 09:18:46 -04:00
2026-08-06 09:18:46 -04:00
2026-07-07 10:42:01 +00:00
2026-10-19 11:32:26 +00:00
2026-10-19 11:32:26 +00:00
2026-10-13 19:15:16 +07:00
2026-10-27 16:23:59 +01:00
2026-10-05 12:52:10 -04:00
2026-10-19 11:32:26 +00:00
2026-10-23 19:32:08 +07:00
2026-10-23 19:32:08 +07:00
2026-10-19 14:01:11 +00:00
2026-10-19 11:32:26 +00:00
2026-10-01 11:49:21 -04:00
2026-10-27 03:13:52 +01:00
2026-07-01 20:15:03 +01:00
2026-09-14 08:31:26 -07:00
2026-10-27 10:15:44 +01:00
2026-07-14 12:29:38 -07:00
2026-08-18 12:54:45 -07:00
2026-08-24 12:06:56 -07:00
2026-08-21 23:46:27 -06:00
2026-09-24 15:12:28 +03:00
2026-05-05 02:54:03 +05:00
2026-05-05 02:54:03 +05:00
2026-10-27 16:18:00 +01:00
2026-10-27 16:18:00 +01:00
2026-10-26 15:01:03 +00:00
2026-09-21 17:42:31 +02:00
2026-09-28 09:25:01 +00:00
2026-09-21 17:42:31 +02:00
2026-08-27 10:49:13 +01:00
2026-08-24 11:20:26 +01:00
2026-08-26 13:33:13 +02:00
2026-09-21 15:58:18 +02:00
2026-10-22 11:04:09 +07:00
2026-10-07 15:55:04 +02:00
2026-10-27 16:20:31 +01:00
2026-10-27 01:50:56 +01:00
2026-10-27 16:23:59 +01:00
2026-10-11 19:57:49 +02:00
2026-09-29 12:23:00 +00:00
2026-09-29 12:23:00 +00:00
2026-10-19 14:02:47 +11:00
2026-07-15 12:16:25 -08:00
2026-08-19 13:00:31 -08:00
2026-06-02 10:11:49 -07:00
2026-08-18 12:54:45 -07:00
2026-06-02 08:36:01 -06:00
2026-07-06 13:15:58 +01:00
2026-10-06 13:01:11 +03:00
2026-10-06 13:01:11 +03:00
2026-05-05 02:54:03 +05:00
2026-09-25 18:51:55 +10:00
2026-09-25 18:51:55 +10:00
2026-10-23 12:53:39 +11:00
2026-10-19 17:59:18 +03:00
2026-10-26 21:48:36 +00:00
2026-10-27 10:15:44 +01:00
2026-10-13 16:43:15 +00:00
2026-10-14 14:55:29 -07:00
2026-10-13 23:03:08 +02:00
2026-10-27 10:15:44 +01:00
2026-10-09 00:58:39 +05:00
2026-10-09 00:58:39 +05:00
2026-10-09 00:58:39 +05:00
2026-10-13 23:03:08 +02:00
2026-08-26 15:29:26 +02:00
2026-08-26 15:29:26 +02:00
2026-05-05 02:54:03 +05:00
2026-09-16 11:36:45 -07:00
2026-09-28 18:46:14 +00:00
2026-08-26 11:57:56 -06:00
2026-09-14 18:31:48 -07:00
2026-09-14 18:31:48 -07:00
2026-09-28 18:46:14 +00:00
2026-09-22 23:20:24 +00:00
2026-09-22 23:20:24 +00:00
2026-05-05 02:54:03 +05:00
2026-10-13 16:43:15 +00:00
2026-10-13 16:43:15 +00:00
2026-10-13 16:24:45 +00:00
2026-10-23 19:32:08 +07:00
2026-09-08 17:20:53 +00:00
2026-09-17 14:33:37 -05:00
2026-08-21 10:31:01 -04:00
2026-08-21 10:31:01 -04:00
2026-05-05 02:54:03 +05:00
2026-08-31 11:36:43 -04:00
2026-06-03 10:46:07 +01:00
2026-10-13 03:43:55 +00:00
2026-09-27 23:34:26 +02:00
2026-05-05 02:54:03 +05:00
2026-05-05 02:54:03 +05:00
2026-10-14 14:55:29 -07:00
2026-10-14 14:55:29 -07:00
2026-10-14 14:55:29 -07:00
2026-09-02 12:51:33 -04:00
2026-09-10 08:21:29 -04:00
2026-09-10 08:21:29 -04:00
2026-09-11 18:13:16 -05:00
2026-09-11 18:13:16 -05:00
2026-09-17 11:11:38 +10:00
2026-09-11 18:13:16 -05:00
2026-08-19 16:16:51 -06:00
2026-10-13 07:57:56 +00:00
2026-05-05 02:54:03 +05:00
2026-06-04 14:37:19 -04:00
2026-08-26 11:57:56 -06:00
2026-10-07 19:03:46 +02:00
2026-10-09 16:42:41 -06:00
2026-10-23 12:53:40 +11:00
2026-07-28 09:58:33 -06:00
2026-09-02 09:21:36 -07:00
2026-09-02 09:21:36 -07:00
2026-09-02 09:21:36 -07:00
2026-08-21 23:46:27 -06:00
2026-09-02 09:21:36 -07:00
2026-08-18 12:54:45 -07:00
2026-10-27 01:06:49 +01:00
2026-10-27 01:06:49 +01:00
2026-04-26 14:49:10 -07:00
2026-05-05 02:54:03 +05:00
2026-05-05 02:54:03 +05:00
2026-07-01 15:59:55 +03:00
2026-07-14 13:50:25 +01:00
2026-10-15 14:38:42 -05:00
2026-10-16 00:21:40 -08:00
2026-10-19 23:06:41 +03:00
2026-10-14 13:54:17 +03:00
2026-10-19 23:06:41 +03:00
2026-10-07 11:43:19 +03:00
2026-10-07 11:43:19 +03:00
2026-09-25 13:59:53 -05:00
2026-09-21 11:04:12 +00:00
2026-08-21 23:46:27 -06:00
2026-10-16 15:55:28 +00:00
2026-10-16 15:55:28 +00:00
2026-05-05 02:54:03 +05:00
2026-09-24 11:41:32 -07:00
2026-09-24 11:41:32 -07:00
2026-09-19 00:32:51 +02:00
2026-08-26 11:57:56 -06:00
2026-10-26 12:59:53 +01:00
2026-10-19 14:02:47 +11:00
2026-10-15 17:40:00 -05:00
2026-09-21 21:06:06 +02:00