π OpenHands: AI-Driven Development
docs: add No Magic Strings rule to AGENTS.md (#704)
Hiep Le0f52021e510b
diff --git a/AGENTS.md b/AGENTS.mdindex f5bd122ca3..bf01707b4a 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -255,6 +255,71 @@ if (getActiveBackend().backend.kind === "cloud") {return new ConversationClient(getAgentServerClientOptions()).someMethod(...);```+## No Magic Strings++Inline string literals that carry meaning (user-facing copy, identifiers, keys, route paths, storage keys, event types, query keys, env-var names, etc.) **must not** appear at call sites. Magic strings drift across files, defeat search/refactor, bypass `tsc`'s spell-checking, and ship untranslated UI text. The `i18next/no-literal-string` rule is set to `"error"` in `eslint.config.js` and CI fails on violations β do not silence it with `eslint-disable` unless the string is genuinely non-localizable (e.g. the `ββ©` keyboard glyph in `plan-preview.tsx` / `conversation-tabs.tsx`).++### Rule 1 β User-facing strings go through i18n++Every visible string (button labels, headings, validation messages, `aria-label`, `title`, `alt`, toast copy, placeholders) **must** be routed through `react-i18next`'s `t()` keyed by an `I18nKey` enum member. Keys are declared once in `src/i18n/translation.json` with values for all 15 supported languages (see `src/i18n/index.ts::AvailableLanguages`), and `npm run make-i18n` regenerates `src/i18n/declaration.ts` + `public/locales/<lang>/openhands.json`. `npm run check-translation-completeness` fails CI if any key is missing a language.++```tsx+// CORRECT+import { useTranslation } from "react-i18next";+import { I18nKey } from "#/i18n/declaration";++const { t } = useTranslation("openhands");+return <button aria-label={t(I18nKey.CHAT$DISMISS_LABEL)}>{t(I18nKey.CHAT$DISMISS)}</button>;++// WRONG -- ships English to every locale; flagged by i18next/no-literal-string+return <button aria-label="Dismiss">Dismiss</button>;+```++Key naming follows the existing `CATEGORY$IDENTIFIER` convention (see `src/i18n/translation.json` β common prefixes: `CHAT_INTERFACE$`, `SETTINGS$`, `COMMON$`, `BUTTON$`, `HOME$`, `MICROAGENT$`, etc.). Reuse an existing prefix; only introduce a new one when no sensible bucket exists.++Caveat: `eslint-plugin-i18next`'s recommended config catches JSX text children but NOT string-literal prop values like `aria-label="β¦"` or string ternaries passed to props. Even when the rule does not flag them, treat them as user-facing strings and route them through `t()`. If you find a hardcoded prop string, fix it; do not assume the linter's silence is approval.++### Rule 2 β Non-UI identifiers live in named constants, not inline literals++For strings the user never sees but the program reads (storage keys, event names, query keys, route paths, env-var names, header names, hardcoded paths, feature-flag identifiers), declare a single named constant in the closest module that owns the concept and import it everywhere else. Co-locate related constants in a tiny dedicated file (`*-keys.ts`, `*-constants.ts`) when more than two callers need them.++```ts+// CORRECT+const ONBOARDING_COMPLETED_KEY = "openhands-onboarded";+localStorage.setItem(ONBOARDING_COMPLETED_KEY, "true");++// CORRECT -- query keys go through SETTINGS_QUERY_KEYS / SECRETS_QUERY_KEYS / β¦+// in src/hooks/query/query-keys.ts (enforced by no-restricted-syntax)+queryClient.invalidateQueries({ queryKey: SETTINGS_QUERY_KEYS.all });++// WRONG -- duplicated literal across files, no compile-time link, silent typo risk+localStorage.setItem("openhands-onboarded", "true");+queryClient.invalidateQueries({ queryKey: ["settings"] });+```++Already-named constants in this repo include `DEFAULT_WORKING_DIR` (`src/api/agent-server-config.ts`), `OPENHANDS_I18N_NAMESPACE` (`src/i18n/index.ts`), `BUNDLED_BACKEND_ID` (backend registry), and the `*_QUERY_KEYS` helpers in `src/hooks/query/query-keys.ts`. Reuse these instead of re-inlining the literal.++### Rule 3 β Discriminated-union tags use string-literal types, not bare strings++When a string is part of a discriminated union or enum-like set (event kinds, backend kinds, tab IDs, agent statuses, observation result statuses), the type itself should constrain the literal. Pass values typed against that union, not raw `string`, so callers get autocomplete and the compiler catches typos.++```ts+// CORRECT+type BackendKind = "local" | "cloud";+if (backend.kind === "cloud") { β¦ }++// WRONG -- `backend.kind` typed as `string`; "clould" compiles fine+if (backend.kind === "clould") { β¦ }+```++### Allowed exceptions++- Test fixtures (`__tests__/`, `tests/e2e/`) may use inline literals for setup data β tests are the boundary where strings stop being magic.+- Non-localizable display glyphs (keyboard shortcuts like `ββ©`, currency symbols, etc.) may stay inline behind an `eslint-disable-next-line i18next/no-literal-string` comment. Keep the disable on the single offending line; never widen it to a file-level disable for a single glyph.+- Generated files (`src/i18n/declaration.ts`, `public/locales/<lang>/openhands.json`) are produced by `npm run make-i18n`; do not hand-edit, do not lint-target.++When adding code that needs a new string, decide up front which rule it falls under: if a user reads it β Rule 1; if the program reads it β Rule 2; if it tags a union β Rule 3. Do not commit code that fails any of these rules just because the linter happens not to catch it.+- Use `@openhands/typescript-client` classes directly for agent-server-backed REST/workspace/event/VS Code calls. Centralize host/session API key/working-directory option assembly through `src/api/agent-server-client-options.ts`; the backend fallback policy itself lives in `src/api/backend-registry/active-store.ts`.- Local verification/build gotchas:- `npm run typecheck` assumes generated translation types exist; run `npm run make-i18n` first if `src/i18n/declaration.ts` is missing.