The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
Publish to Hub: one-file games publish, drafts arrive in English, bodies pass Cloudflare (#1284)
Jacob Phamf3fbcf3a7e09
diff --git a/backend/src/lib/communityTranslate.test.ts b/backend/src/lib/communityTranslate.test.tsnew file mode 100644index 000000000..368a65ed6--- /dev/null+++ b/backend/src/lib/communityTranslate.test.ts@@ -0,0 +1,73 @@+import { describe, expect, it, vi } from 'vitest'+import { batchesOf, parseTranslation, translateDraft, translationLimits, TranslationTooLong } from './communityTranslate.js'++/** A model that answers each asked item by [answer], read back from the prompt it was given. */+const model = (answer: (text: string) => string | null) => vi.fn(async (prompt: string) => {+ const items = JSON.parse(prompt.slice(prompt.indexOf('Items:\n') + 7)) as { i: number; text: string }[]+ return { text: JSON.stringify({ items: items.map(({ i, text }) => { const en = answer(text); return en === null ? { i, same: true } : { i, en } }) }) }+})++describe('translateDraft', () => {+ it('translates the title, description and people\'s turns, keeping tool output as it is', async () => {+ const ask = model(text => text.startsWith('EN:') ? null : `EN:${text}`)+ const result = await translateDraft({+ title: 'Trò chơi rắn', description: 'EN: already English',+ conversation: [{ role: 'user', text: 'làm game rắn' }, { role: 'tool', text: 'npm ERR! lỗi' }, { role: 'assistant', text: 'Xong rồi' }],+ }, ask)+ expect(result).toEqual({+ title: 'EN:Trò chơi rắn', description: 'EN: already English',+ conversation: [{ role: 'user', text: 'EN:làm game rắn' }, { role: 'tool', text: 'npm ERR! lỗi' }, { role: 'assistant', text: 'EN:Xong rồi' }],+ translated: true,+ })+ expect(ask.mock.calls[0][0]).not.toContain('npm ERR!')+ })++ it('says nothing was translated when every item is already English', async () => {+ const result = await translateDraft({ title: 'Snake', description: '', conversation: [{ role: 'user', text: 'Make a snake game' }] }, model(() => null))+ expect(result.translated).toBe(false)+ expect(result.title).toBe('Snake')+ })++ it('asks nothing for an empty draft', async () => {+ const ask = model(() => null)+ expect((await translateDraft({ title: '', description: '', conversation: [{ role: 'tool', text: 'log' }] }, ask)).translated).toBe(false)+ expect(ask).not.toHaveBeenCalled()+ })++ it('keeps a translated title within what the Hub accepts', async () => {+ const result = await translateDraft({ title: 'Tiêu đề', description: '', conversation: [] }, model(() => 'x'.repeat(150)))+ expect(result.title).toHaveLength(100)+ })++ it('retries an incomplete reply once, then fails rather than publishing half a translation', async () => {+ const ask = vi.fn(async () => ({ text: '{"items":[]}' }))+ await expect(translateDraft({ title: 'Tiêu đề', description: '', conversation: [] }, ask)).rejects.toThrow('incomplete')+ expect(ask).toHaveBeenCalledTimes(2)+ })++ it('refuses a draft longer than one request may translate, without asking', async () => {+ const ask = model(() => null)+ const conversation = Array.from({ length: 8 }, () => ({ role: 'user' as const, text: 'x'.repeat(11_000) }))+ await expect(translateDraft({ title: 't', description: '', conversation }, ask)).rejects.toBeInstanceOf(TranslationTooLong)+ expect(ask).not.toHaveBeenCalled()+ })+})++describe('batchesOf', () => {+ it('packs items up to the batch size, skips blanks, and sends a long item alone', () => {+ expect(batchesOf([{ text: 'aaaa' }, { text: '' }, { text: 'bbbb' }, { text: 'c'.repeat(20) }, { text: 'dd' }], 10)).toEqual([[0, 2], [3], [4]])+ expect(translationLimits.batchChars).toBeLessThan(translationLimits.totalChars)+ })+})++describe('parseTranslation', () => {+ it('reads a fenced reply and ignores items it was not asked about', () => {+ const answers = parseTranslation('Sure:\n```json\n{"items":[{"i":0,"en":"Hello"},{"i":2,"same":true},{"i":9,"en":"x"}]}\n```', [0, 2])+ expect(answers).toEqual(new Map([[0, 'Hello'], [2, null]]))+ })+ it('is null when an asked item is missing or blank', () => {+ expect(parseTranslation('{"items":[{"i":0,"en":"Hello"}]}', [0, 1])).toBeNull()+ expect(parseTranslation('{"items":[{"i":0,"en":" "}]}', [0])).toBeNull()+ expect(parseTranslation('not json', [0])).toBeNull()+ })+})diff --git a/backend/src/lib/communityTranslate.ts b/backend/src/lib/communityTranslate.tsnew file mode 100644index 000000000..e41cdc159--- /dev/null+++ b/backend/src/lib/communityTranslate.ts@@ -0,0 +1,136 @@+/**+ * A Hub draft's words in English: its title, description and the people's and agent's turns.+ *+ * Tool turns stay as they are (logs and file contents, not prose). Items go to the model in batches,+ * a few at a time; an item already in English comes back marked `same`, so the model never rewrites+ * it and the author is never told something was translated when it was not.+ */+import { communityLimits } from './communityContract.js'+import { callHarnessLlm, replyJson } from './harnessLlm.js'++export type TranslationRole = 'user' | 'assistant' | 'tool'+export interface TranslationTexts {+ title: string+ description: string+ conversation: { role: TranslationRole; text: string }[]+}+export interface Translation extends TranslationTexts {+ /** False when every item was already English: the draft is unchanged. */+ translated: boolean+}++export const translationLimits = {+ /** What one request may carry: about what Desktop sends (its session tail is 60,000 characters). */+ totalChars: 80_000,+ /** Small batches, mostly all at once: the whole answer has to beat the ingress's 60-second wait. */+ batchChars: 5_000,+ concurrency: 16,+ callTimeoutMs: 25_000,+ deadlineMs: 50_000,+} as const++/** The draft is longer than one request may translate. */+export class TranslationTooLong extends Error {}++type Ask = (prompt: string, timeoutMs: number) => Promise<{ text: string }>+interface Item { text: string; max: number }++/** The words to translate, in a fixed order: title, description, then each non-tool turn. */+function itemsOf(texts: TranslationTexts): Item[] {+ return [+ { text: texts.title, max: 100 },+ { text: texts.description, max: 300 },+ ...texts.conversation.map(turn => ({ text: turn.role === 'tool' ? '' : turn.text, max: communityLimits.turnChars })),+ ]+}++/** Indexes of the items worth asking about, packed into batches of at most `batchChars` (a longer item goes alone). */+export function batchesOf(items: { text: string }[], batchChars: number = translationLimits.batchChars): number[][] {+ const batches: number[][] = []+ let current: number[] = [], size = 0+ items.forEach((item, index) => {+ if (!item.text.trim()) return+ if (current.length && size + item.text.length > batchChars) {+ batches.push(current)+ current = []+ size = 0+ }+ current.push(index)+ size += item.text.length+ })+ if (current.length) batches.push(current)+ return batches+}++export function translationPrompt(items: { i: number; text: string }[]): string {+ return (+ `Translate each item below into English. They are the title, description and conversation of a public\n` ++ `page showing what someone built with an AI coding agent.\n\n` ++ `- An item already in English: answer {"i":<n>,"same":true}, never a rewrite of it.\n` ++ `- Any other item: answer {"i":<n>,"en":"<the English text>"}.\n` ++ `- Translate only the human language. Keep code, code blocks, inline code, commands, file names, paths,\n` ++ ` URLs, numbers and Markdown exactly as they are, and keep the line breaks.\n` ++ `- Translate faithfully: never summarise, add to, explain, or answer what an item says.\n\n` ++ `Respond with ONLY this JSON, no prose, no markdown fence, one entry per item:\n` ++ `{"items":[...]}\n\n` ++ `Items:\n${JSON.stringify(items)}`+ )+}++/** Each asked item's English, or null for one already in English; null overall unless every item is answered. */+export function parseTranslation(raw: string, asked: number[]): Map<number, string | null> | null {+ const rows = (replyJson(raw) as { items?: unknown } | null)?.items+ if (!Array.isArray(rows)) return null+ const answers = new Map<number, string | null>()+ for (const row of rows) {+ const entry = row as { i?: unknown; en?: unknown; same?: unknown } | null+ if (typeof entry?.i !== 'number' || !asked.includes(entry.i)) continue+ if (entry.same === true) answers.set(entry.i, null)+ else if (typeof entry.en === 'string' && entry.en.trim()) answers.set(entry.i, entry.en)+ }+ return asked.every(index => answers.has(index)) ? answers : null+}++async function translateBatch(items: Item[], asked: number[], ask: Ask, deadline: number): Promise<Map<number, string | null>> {+ const prompt = translationPrompt(asked.map(i => ({ i, text: items[i].text })))+ // One retry, while time remains: a reply that drops an item or breaks its JSON is usually fine the second time.+ for (let attempt = 0; attempt < 2; attempt++) {+ const remaining = deadline - Date.now()+ if (remaining <= 0) break+ const answers = parseTranslation((await ask(prompt, Math.min(translationLimits.callTimeoutMs, remaining))).text, asked)+ if (answers) return answers+ }+ throw new Error('The translation came back incomplete.')+}++/** Runs [work] over [inputs] at most [limit] at a time, keeping their order. */+async function mapLimited<T, R>(inputs: T[], limit: number, work: (input: T) => Promise<R>): Promise<R[]> {+ const results: R[] = new Array(inputs.length)+ let next = 0+ const worker = async () => {+ while (next < inputs.length) {+ const index = next+++ results[index] = await work(inputs[index])+ }+ }+ await Promise.all(Array.from({ length: Math.min(limit, inputs.length) }, worker))+ return results+}++export async function translateDraft(texts: TranslationTexts, ask: Ask = callHarnessLlm): Promise<Translation> {+ const items = itemsOf(texts)+ if (items.reduce((sum, item) => sum + item.text.length, 0) > translationLimits.totalChars) throw new TranslationTooLong()+ const answers = new Map<number, string | null>(), deadline = Date.now() + translationLimits.deadlineMs+ const results = await mapLimited(batchesOf(items), translationLimits.concurrency, asked => translateBatch(items, asked, ask, deadline))+ for (const batch of results) for (const [index, english] of batch) answers.set(index, english)+ const english = items.map((item, index) => {+ const answer = answers.get(index)+ return answer ? answer.slice(0, item.max) : item.text+ })+ return {+ title: english[0],+ description: english[1],+ conversation: texts.conversation.map((turn, index) => ({ role: turn.role, text: turn.role === 'tool' ? turn.text : english[index + 2] })),+ translated: english.some((text, index) => text !== items[index].text),+ }+}diff --git a/backend/src/lib/gzipJson.ts b/backend/src/lib/gzipJson.tsnew file mode 100644index 000000000..295838686--- /dev/null+++ b/backend/src/lib/gzipJson.ts@@ -0,0 +1,32 @@+/**+ * Request bodies the Hub sends gzipped: the JSON a route reads, as `application/gzip` bytes.+ *+ * Publications carry what people made (HTML, JavaScript, any text a session wrote), and Cloudflare's+ * managed rules in front of harness.autonomous.ai and this API refuse a body with raw `<script>` in+ * it as an attack. Gzipped, such a body reaches the route, which validates it as it would a raw one.+ * Unpacking is bounded, so a small body cannot inflate past `maxBytes`.+ */+import { gunzipSync } from 'node:zlib'+import type { FastifyInstance } from 'fastify'+import { ValidationError } from '../errors/index.js'++export const GZIP_JSON_TYPE = 'application/gzip'++export function gunzipJson(body: Buffer, maxBytes: number): unknown {+ try {+ return JSON.parse(gunzipSync(body, { maxOutputLength: maxBytes }).toString('utf8'))+ } catch {+ throw new ValidationError('Invalid compressed body.')+ }+}++/** Lets `app`'s routes read a gzipped body as the JSON it carries; each route's own bodyLimit still bounds what was sent. */+export function acceptGzipJson(app: FastifyInstance, maxBytes: number): void {+ app.addContentTypeParser(GZIP_JSON_TYPE, { parseAs: 'buffer' }, (_req, body, done) => {+ try {+ done(null, gunzipJson(body as Buffer, maxBytes))+ } catch (error) {+ done(error as Error, undefined)+ }+ })+}diff --git a/backend/src/lib/harnessLlm.ts b/backend/src/lib/harnessLlm.tsnew file mode 100644index 000000000..e56234d7b--- /dev/null+++ b/backend/src/lib/harnessLlm.ts@@ -0,0 +1,46 @@+/**+ * Harness's own calls to the LLM behind CURSOR_LLM_* (an OpenAI-compatible endpoint), with plain `fetch`.+ *+ * Shared by Harness routes (agent routing, Hub translation). routes/cursor.ts keeps its own copy, and+ * knowingly so: that file belongs to the Cursor app and documents its self-containment as a decision,+ * so a Harness deploy must not change its behaviour.+ */+import { env } from '../config/env.js'++/** Routes that spend this credit fail CLOSED without it: an absent credential is never "answer some other way". */+export const harnessLlmConfigured = (): boolean => !!env.CURSOR_LLM_BASE_URL && !!env.CURSOR_LLM_API_KEY++export async function callHarnessLlm(prompt: string, timeoutMs: number): Promise<{ text: string; model: string }> {+ const model = env.CURSOR_LLM_MODEL+ const res = await fetch(`${env.CURSOR_LLM_BASE_URL!.replace(/\/+$/, '')}/chat/completions`, {+ method: 'POST',+ headers: { Authorization: `Bearer ${env.CURSOR_LLM_API_KEY}`, 'Content-Type': 'application/json' },+ body: JSON.stringify({ model, messages: [{ role: 'user', content: prompt }] }),+ signal: AbortSignal.timeout(timeoutMs),+ })+ if (!res.ok) {+ const detail = await res.text().catch(() => '')+ throw new Error(`LLM HTTP ${res.status}: ${detail.slice(0, 200)}`)+ }+ const data = (await res.json()) as { model?: string; choices?: Array<{ message?: { content?: string } }> }+ return { text: (data.choices?.[0]?.message?.content ?? '').trim(), model: data.model || model }+}++/**+ * The JSON object a reply carries, or null.+ *+ * "Respond with only JSON" is a request, not a guarantee: a fence or a sentence of preamble is normal+ * from the model this runs on, so the object is taken from between the first `{` and the last `}`.+ */+export function replyJson(raw: string): unknown {+ const fenced = raw.match(/```(?:json)?\s*([\s\S]*?)```/i)+ const body = (fenced ? fenced[1] : raw).trim()+ const start = body.indexOf('{')+ const end = body.lastIndexOf('}')+ if (start < 0 || end <= start) return null+ try {+ return JSON.parse(body.slice(start, end + 1))+ } catch {+ return null+ }+}diff --git a/backend/src/lib/windowLimiter.ts b/backend/src/lib/windowLimiter.tsnew file mode 100644index 000000000..ed11a99ec--- /dev/null+++ b/backend/src/lib/windowLimiter.ts@@ -0,0 +1,27 @@+/**+ * A per-account fixed-window limiter, process-local by design: it spreads a route's budget, it is not+ * the budget (the provider's own credit limit is).+ */+export interface WindowLimiter {+ /** Counts one request for [key] and says whether it is within the window's allowance. */+ allows(key: string, now?: number): boolean+ /** Test seam. */+ reset(): void+}++export function createWindowLimiter(windowMs: number, max: number): WindowLimiter {+ const buckets = new Map<string, { count: number; resetAt: number }>()+ return {+ allows(key, now = Date.now()) {+ const bucket = buckets.get(key)+ if (!bucket || now >= bucket.resetAt) {+ buckets.set(key, { count: 1, resetAt: now + windowMs })+ return true+ }+ if (bucket.count >= max) return false+ bucket.count+++ return true+ },+ reset: () => buckets.clear(),+ }+}diff --git a/backend/src/routes/agentRoute.ts b/backend/src/routes/agentRoute.tsindex 86496173c..3223a1f9c 100644--- a/backend/src/routes/agentRoute.ts+++ b/backend/src/routes/agentRoute.ts@@ -20,10 +20,11 @@*/import type { FastifyInstance } from 'fastify'import { z } from 'zod'-import { env } from '../config/env.js'import { validateBody } from '../middlewares/validation.js'import { sendSuccess, sendError } from '../utils/response.js'import { logger } from '../utils/logger.js'+import { callHarnessLlm, harnessLlmConfigured, replyJson } from '../lib/harnessLlm.js'+import { createWindowLimiter } from '../lib/windowLimiter.js'export const AGENT_ROUTE_PATH = '/api/route/agents'@@ -52,23 +53,12 @@ const BUDGET_MS_RANGE = { min: 3_000, max: 20_000, fallback: 10_000 } as constconst RATE_LIMIT_WINDOW_MS = 60_000const RATE_LIMIT_MAX = 60-const rateBuckets = new Map<string, { count: number; resetAt: number }>()+const rateLimiter = createWindowLimiter(RATE_LIMIT_WINDOW_MS, RATE_LIMIT_MAX)-export function routeRateLimitAllows(userId: string, now = Date.now()): boolean {- const bucket = rateBuckets.get(userId)- if (!bucket || now >= bucket.resetAt) {- rateBuckets.set(userId, { count: 1, resetAt: now + RATE_LIMIT_WINDOW_MS })- return true- }- if (bucket.count >= RATE_LIMIT_MAX) return false- bucket.count++- return true-}+export const routeRateLimitAllows = (userId: string, now = Date.now()): boolean => rateLimiter.allows(userId, now)/** Test seam: process-local by design, like the analytics limiter it copies. */-export function resetRouteRateLimits(): void {- rateBuckets.clear()-}+export const resetRouteRateLimits = (): void => rateLimiter.reset()const agentSchema = z.object({id: z.string().min(1).max(128),@@ -170,29 +160,6 @@ export function buildRankPrompt(body: RouteBody): string {)}-/**- * The LLM, over an OpenAI-compatible endpoint with plain `fetch`.- *- * A second copy of what `routes/cursor.ts` does, and knowingly so: that file documents its own- * self-containment as a decision, and importing across two products' routing to save a `fetch` would- * couple a Harness deploy to the Cursor app's behaviour.- */-async function callLlm(prompt: string, timeoutMs: number): Promise<{ text: string; model: string }> {- const model = env.CURSOR_LLM_MODEL- const res = await fetch(`${env.CURSOR_LLM_BASE_URL!.replace(/\/+$/, '')}/chat/completions`, {- method: 'POST',- headers: { Authorization: `Bearer ${env.CURSOR_LLM_API_KEY}`, 'Content-Type': 'application/json' },- body: JSON.stringify({ model, messages: [{ role: 'user', content: prompt }] }),- signal: AbortSignal.timeout(timeoutMs),- })- if (!res.ok) {- const detail = await res.text().catch(() => '')- throw new Error(`LLM HTTP ${res.status}: ${detail.slice(0, 200)}`)- }- const data = (await res.json()) as { model?: string; choices?: Array<{ message?: { content?: string } }> }- return { text: (data.choices?.[0]?.message?.content ?? '').trim(), model: data.model || model }-}-export interface RankedRow { agentId: string; score: number; reason: string }/**@@ -207,18 +174,7 @@ export interface RankedRow { agentId: string; score: number; reason: string }* than as an empty ranking — the two need opposite handling on the machine.*/export function parseRanking(raw: string, offered: Set<string>): RankedRow[] | null {- const fenced = raw.match(/```(?:json)?\s*([\s\S]*?)```/i)- const body = (fenced ? fenced[1] : raw).trim()- const start = body.indexOf('{')- const end = body.lastIndexOf('}')- if (start < 0 || end <= start) return null- let parsed: unknown- try {- parsed = JSON.parse(body.slice(start, end + 1))- } catch {- return null- }- const rows = (parsed as { ranking?: unknown })?.ranking+ const rows = (replyJson(raw) as { ranking?: unknown } | null)?.rankingif (!Array.isArray(rows)) return nullconst seen = new Set<string>()@@ -260,7 +216,7 @@ export async function agentRouteRoutes(app: FastifyInstance): Promise<void> {async (req, reply) => {// Fail CLOSED on an unconfigured model, like the Cursor routes: an absent credential must never// read as "answer some other way" on a path that spends credit.- if (!env.CURSOR_LLM_BASE_URL || !env.CURSOR_LLM_API_KEY) {+ if (!harnessLlmConfigured()) {logger.error('agent-route: CURSOR_LLM_BASE_URL/CURSOR_LLM_API_KEY not configured — refusing')sendError(reply, 'Routing is not configured', 'LLM_NOT_CONFIGURED', 503)return@@ -285,7 +241,7 @@ export async function agentRouteRoutes(app: FastifyInstance): Promise<void> {const started = Date.now()try {- const { text, model } = await callLlm(buildRankPrompt(body), budgetMs)+ const { text, model } = await callHarnessLlm(buildRankPrompt(body), budgetMs)const ranking = parseRanking(text, offered)if (!ranking) {logger.error('agent-route: unusable answer', { model, userId, sample: text.slice(0, 200) })diff --git a/backend/src/routes/community.test.ts b/backend/src/routes/community.test.tsindex 5e8df7dac..1a451d4e3 100644--- a/backend/src/routes/community.test.ts+++ b/backend/src/routes/community.test.ts@@ -1,3 +1,4 @@+import { gzipSync } from 'node:zlib'import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import Fastify, { type FastifyInstance, type InjectOptions } from 'fastify'@@ -103,6 +104,16 @@ describe('publications and access', () => {await call('DELETE', `harnesses/${id}/comments/${comment.id}`, 'bob')expect((await call('POST', `harnesses/${id}/comments`, 'alice', payload)).statusCode).toBe(404)})+ it('reads a gzipped publication as the JSON it carries, and refuses one that is not', async () => {+ const game = { ...sample, files: [{ path: 'index.html', content: '<canvas></canvas><script>document.body.innerHTML = "go"</script>' }] }+ const send = (body: Buffer) => app.inject({ method: 'POST', url: '/api/community/harnesses', payload: body, headers: { authorization: 'Bearer alice', 'x-autonomous-env': 'prod', 'content-type': 'application/gzip' } })+ const response = await send(gzipSync(JSON.stringify(game)))+ expect(response.statusCode).toBe(201)+ expect(db.communityHarness.rows[0].files).toEqual(game.files)+ expect((await send(Buffer.from('not gzip'))).statusCode).toBe(400)+ // Bounded: a small body that would inflate past the snapshot is refused, not unpacked.+ expect((await send(gzipSync(Buffer.alloc(7_000_000, 32)))).statusCode).toBe(400)+ })it('returns the same publication after a lost-response retry', async () => {const payload = { ...sample, clientId: crypto.randomUUID() }const first = await publish('alice', payload)diff --git a/backend/src/routes/community.ts b/backend/src/routes/community.tsindex d96cc6356..1148b8ff1 100644--- a/backend/src/routes/community.ts+++ b/backend/src/routes/community.ts@@ -8,6 +8,7 @@ import { communityCategories, communityEngines, communityHarnessIds, communityHaimport { validateBody, validateParams, validateQuery } from '../middlewares/validation.js'import { sendError, sendSuccess } from '../utils/response.js'import { ValidationError } from '../errors/index.js'+import { acceptGzipJson } from '../lib/gzipJson.js'const identifier = z.string().regex(/^[a-z0-9-]{1,80}$/)@@ -113,6 +114,8 @@ async function publication(id: string, autonomousEnv: string) {export async function communityRoutes(app: FastifyInstance): Promise<void> {app.addHook('onSend', async (_req, reply) => { reply.header('Cache-Control', 'no-store') })+ // A gzipped body unpacks to at most what a raw one may carry (the snapshot and its envelope).+ acceptGzipJson(app, 6_100_000)const feedQuery = z.object({cursor: z.string().uuid().or(z.enum([...communityStarters] as [string, ...string[]])).optional(),diff --git a/backend/src/routes/communityTranslate.test.ts b/backend/src/routes/communityTranslate.test.tsnew file mode 100644index 000000000..106ae46fc--- /dev/null+++ b/backend/src/routes/communityTranslate.test.ts@@ -0,0 +1,83 @@+import { gzipSync } from 'node:zlib'+import { beforeEach, describe, expect, it, vi } from 'vitest'+import Fastify, { type FastifyInstance } from 'fastify'++const authenticateAccessToken = vi.hoisted(() => vi.fn())+vi.mock('../lib/ssoAuth.js', async (importActual) => ({+ ...(await importActual<typeof import('../lib/ssoAuth.js')>()),+ authenticateAccessToken,+}))++import { communityTranslateRoutes, COMMUNITY_TRANSLATE_PATH, resetTranslateRateLimits } from './communityTranslate.js'+import { errorHandler } from '../middlewares/errorHandler.js'+import { registerAuthMiddleware, shouldSkipAuth } from '../middlewares/authMiddleware.js'+import { env } from '../config/env.js'++async function build(): Promise<FastifyInstance> {+ const app = Fastify()+ app.setErrorHandler(errorHandler)+ registerAuthMiddleware(app, authenticateAccessToken)+ await app.register(communityTranslateRoutes)+ await app.ready()+ return app+}++const draft = { title: 'Trò chơi rắn', description: '', conversation: [{ role: 'user', text: 'làm game rắn' }] }+const post = (app: FastifyInstance, payload: unknown = draft, auth = true) =>+ app.inject({ method: 'POST', url: COMMUNITY_TRANSLATE_PATH, payload: payload as object, headers: auth ? { authorization: 'Bearer sso-token' } : {} })+const llmSays = (content: string) =>+ vi.spyOn(globalThis, 'fetch').mockImplementation(async () => new Response(JSON.stringify({ model: 'test-model', choices: [{ message: { content } }] }), { status: 200, headers: { 'content-type': 'application/json' } }))++describe('POST /api/community/translate', () => {+ beforeEach(() => {+ vi.restoreAllMocks()+ resetTranslateRateLimits()+ authenticateAccessToken.mockReset()+ authenticateAccessToken.mockResolvedValue({ sub: 'u1', email: '[email protected]', role: 'user', autonomousEnv: 'prod' })+ env.CURSOR_LLM_BASE_URL = 'https://llm.example/v1'+ env.CURSOR_LLM_API_KEY = 'llm-key'+ })++ it('needs a signed-in account: it spends credit', async () => {+ expect(shouldSkipAuth(COMMUNITY_TRANSLATE_PATH)).toBe(false)+ expect((await post(await build(), draft, false)).statusCode).toBe(401)+ })++ it('answers the draft in English', async () => {+ llmSays('{"items":[{"i":0,"en":"Snake game"},{"i":2,"en":"make a snake game"}]}')+ const res = await post(await build())+ expect(res.statusCode).toBe(200)+ expect(res.json().data).toEqual({ title: 'Snake game', description: '', conversation: [{ role: 'user', text: 'make a snake game' }], translated: true })+ })++ it('reads a gzipped draft as the JSON it carries', async () => {+ llmSays('{"items":[{"i":0,"en":"Snake game"},{"i":2,"en":"make a snake game"}]}')+ const res = await (await build()).inject({ method: 'POST', url: COMMUNITY_TRANSLATE_PATH, payload: gzipSync(JSON.stringify(draft)), headers: { authorization: 'Bearer sso-token', 'content-type': 'application/gzip' } })+ expect(res.statusCode).toBe(200)+ expect(res.json().data.title).toBe('Snake game')+ })++ it('fails closed without a configured model, and on a failed call', async () => {+ env.CURSOR_LLM_API_KEY = undefined+ const fetchSpy = vi.spyOn(globalThis, 'fetch')+ expect((await post(await build())).statusCode).toBe(503)+ expect(fetchSpy).not.toHaveBeenCalled()+ env.CURSOR_LLM_API_KEY = 'llm-key'+ fetchSpy.mockRejectedValue(new Error('upstream down'))+ expect((await post(await build())).json().error.code).toBe('TRANSLATION_FAILED')+ })++ it('refuses what is not a draft, and a draft too long to translate', async () => {+ const app = await build()+ expect((await post(app, { ...draft, files: [] })).statusCode).toBe(400)+ const conversation = Array.from({ length: 8 }, () => ({ role: 'user', text: 'x'.repeat(11_000) }))+ expect((await post(app, { ...draft, conversation })).json().error.code).toBe('TOO_LONG')+ })++ it('limits each account', async () => {+ llmSays('{"items":[{"i":0,"same":true},{"i":2,"same":true}]}')+ const app = await build()+ for (let i = 0; i < 10; i++) expect((await post(app)).statusCode).toBe(200)+ expect((await post(app)).statusCode).toBe(429)+ })+})diff --git a/backend/src/routes/communityTranslate.ts b/backend/src/routes/communityTranslate.tsnew file mode 100644index 000000000..8f2144000--- /dev/null+++ b/backend/src/routes/communityTranslate.ts@@ -0,0 +1,51 @@+/**+ * POST /api/community/translate SSO access token → { title, description, conversation, translated }+ *+ * The Hub's publish page asks this once for a draft that arrived from Desktop or a bundle, so the+ * publication reads in English. The author sees the result, can go back to the original, and publishes+ * whichever they choose: nothing here stores or publishes anything.+ */+import type { FastifyInstance } from 'fastify'+import { z } from 'zod'+import { communityLimits } from '../lib/communityContract.js'+import { translateDraft, TranslationTooLong } from '../lib/communityTranslate.js'+import { acceptGzipJson } from '../lib/gzipJson.js'+import { harnessLlmConfigured } from '../lib/harnessLlm.js'+import { createWindowLimiter } from '../lib/windowLimiter.js'+import { validateBody } from '../middlewares/validation.js'+import { sendError, sendSuccess } from '../utils/response.js'+import { logger } from '../utils/logger.js'++export const COMMUNITY_TRANSLATE_PATH = '/api/community/translate'++/** A draft is translated once as it arrives, and again only when its author asks. */+const limiter = createWindowLimiter(10 * 60_000, 10)+export const resetTranslateRateLimits = (): void => limiter.reset()++const bodySchema = z.object({+ title: z.string().max(100),+ description: z.string().max(300),+ conversation: z.array(z.object({ role: z.enum(['user', 'assistant', 'tool']), text: z.string().max(communityLimits.turnChars) }).strict()).max(communityLimits.turns),+}).strict()++export async function communityTranslateRoutes(app: FastifyInstance): Promise<void> {+ acceptGzipJson(app, 1_100_000)+ app.post<{ Body: z.infer<typeof bodySchema> }>(COMMUNITY_TRANSLATE_PATH, { bodyLimit: 1_100_000, preHandler: validateBody(bodySchema) }, async (req, reply) => {+ if (!harnessLlmConfigured()) {+ logger.error('community-translate: CURSOR_LLM_BASE_URL/CURSOR_LLM_API_KEY not configured — refusing')+ return sendError(reply, 'Translation is not available right now.', 'LLM_NOT_CONFIGURED', 503)+ }+ const userId = req.user!.sub+ if (!limiter.allows(userId)) return sendError(reply, 'Please wait before translating again.', 'RATE_LIMITED', 429)+ const started = Date.now()+ try {+ const result = await translateDraft(req.body)+ logger.info('community-translate', { userId, turns: req.body.conversation.length, translated: result.translated, ms: Date.now() - started })+ sendSuccess(reply, result)+ } catch (error) {+ if (error instanceof TranslationTooLong) return sendError(reply, 'This conversation is too long to translate automatically.', 'TOO_LONG', 413)+ logger.error('community-translate failed', { userId, ms: Date.now() - started, error: error instanceof Error ? error.message : String(error) })+ sendError(reply, 'The translation failed. Try again.', 'TRANSLATION_FAILED', 502)+ }+ })+}diff --git a/backend/src/server.ts b/backend/src/server.tsindex 2bf235483..8965ac95b 100644--- a/backend/src/server.ts+++ b/backend/src/server.ts@@ -12,6 +12,7 @@ import { voiceRoutes } from './routes/voice.js'import { harnessShareRoutes } from './routes/harnessShares.js'import { harnessLinkRoutes } from './routes/harnessLinks.js'import { communityRoutes } from './routes/community.js'+import { communityTranslateRoutes } from './routes/communityTranslate.js'import { deviceKeyRoutes } from './routes/deviceKeys.js'import { qrSignInRoutes } from './routes/qrSignIn.js'import { deskRoutes } from './routes/desk.js'@@ -177,6 +178,7 @@ async function start(): Promise<void> {await app.register(harnessShareRoutes)await app.register(harnessLinkRoutes)await app.register(communityRoutes)+ await app.register(communityTranslateRoutes) // a Hub draft's words in English, for its author to reviewawait app.register(qrSignInRoutes) // sign a computer in by scanning its QR with a signed-in phoneawait app.register(deviceKeyRoutes) // the account's device key log: signing in is what trusts a deviceawait app.register(deskRoutes) // the account's tabs, the same on every computer (lib/desk.ts)diff --git a/desktop/lib/community/output_check.dart b/desktop/lib/community/output_check.dartnew file mode 100644index 000000000..c5688747b--- /dev/null+++ b/desktop/lib/community/output_check.dart@@ -0,0 +1,61 @@+/// Files and addresses an output page loads that the Hub's preview cannot reach.+///+/// ⚠️ Mirrored, not shared: `website/src/lib/community/outputCheck.ts` is the Hub's own check, and+/// `test/community_output_check_test.dart` runs the website's cases against this copy.+library;++/// Addresses the page loads for itself that the Hub's sandbox serves: none, apart from these.+final _inline = RegExp(+ r'^(?:data:|blob:|#|about:|javascript:)',+ caseSensitive: false,+);+final _sourced = RegExp(+ r'<(?:script|img|iframe|source|video|audio|embed|track)\b[^>]*>',+ caseSensitive: false,+);+final _link = RegExp(r'<link\b[^>]*>', caseSensitive: false);+final _loadedLink = RegExp(+ r'''\brel\s*=\s*["']?(?:stylesheet|preload|modulepreload)\b''',+ caseSensitive: false,+);+final _cssUrl = RegExp(+ r'''url\(\s*["']?([^"')]+)["']?\s*\)''',+ caseSensitive: false,+);+final _cssImport = RegExp(+ r'''@import\s+["']([^"']+)["']''',+ caseSensitive: false,+);++String? _attribute(String tag, String name) {+ final match = RegExp(+ '\\s$name\\s*=\\s*(?:"([^"]*)"|\'([^\']*)\'|([^\\s>]+))',+ caseSensitive: false,+ ).firstMatch(tag);+ if (match == null) return null;+ return (match.group(1) ?? match.group(2) ?? match.group(3))?.trim();+}++/// What [html] loads from elsewhere: the sandbox serves no other file and blocks the network, so+/// each one is missing for readers. Empty means the page runs on its own.+List<String> outsideResources(String html) {+ final found = <String>{};+ void add(String? value) {+ if (value == null || value.isEmpty || _inline.hasMatch(value)) return;+ found.add(value);+ }++ for (final tag in _sourced.allMatches(html)) {+ add(_attribute(tag[0]!, 'src'));+ }+ for (final tag in _link.allMatches(html)) {+ if (_loadedLink.hasMatch(tag[0]!)) add(_attribute(tag[0]!, 'href'));+ }+ for (final match in _cssUrl.allMatches(html)) {+ add(match[1]!.trim());+ }+ for (final match in _cssImport.allMatches(html)) {+ add(match[1]);+ }+ return found.toList();+}diff --git a/desktop/lib/community/publish_files.dart b/desktop/lib/community/publish_files.dartindex 3572363bb..2683540bc 100644--- a/desktop/lib/community/publish_files.dart+++ b/desktop/lib/community/publish_files.dart@@ -4,6 +4,7 @@ import 'dart:io';import 'package:path/path.dart' as p;import 'hub_contract.dart';+import 'output_check.dart';const _skippedFolders = {'node_modules',@@ -94,8 +95,11 @@ class NeedsOutput extends FormatException {////// The output is [viewer] (the page a fork was published with), or else `preview.html`, skipping a/// page still as it is in [original] (the fork's files as they arrived), which shows the original.-/// Any other page is never assumed: an app's `index.html` usually needs files the Hub's sandbox-/// cannot load. Without a page of its own, the output is [picture], a page showing the viewer.+/// Failing both, a page the session made is the output only when it runs on its own: one at the top+/// of the project (`index.html` first), or else an `index.html` in a folder of its own (an agent often+/// puts a game in `<name>/`), shallowest first. An app's `index.html` usually needs files the Hub's+/// sandbox cannot load, while a one-file game or page shows as it is. Without a page of its own, the+/// output is [picture], a page showing the viewer.Future<ProjectSelection> selectProjectFiles(String folder, {String? marker,@@ -105,17 +109,11 @@ Future<ProjectSelection> selectProjectFiles(}) async {final root = Directory(await Directory(folder).resolveSymbolicLinks());final (candidates, scannedAll) = await _candidates(root);- final (own, stale) = await _output(candidates, viewer, original);+ final output = await _output(candidates, viewer, original);final shown =- own ??+ output.page ??(picture == null- ? throw NeedsOutput(- stale != null- ? '${stale.path} is still the original\'s and has none of your changes. '- 'Ask your agent to update it to show the current result, then publish again.'- : 'The Hub shows what a session made. Ask your agent for a preview.html that runs '- 'on its own (for a review, a page presenting it), then publish again.',- )+ ? throw NeedsOutput(_needsOutputMessage(output)): _Candidate.page('preview.html', picture));final rest =candidates.where((c) => c.path != shown.path && c.path != marker).toList()@@ -162,27 +160,99 @@ Future<ProjectSelection> selectProjectFiles();}-/// The page readers see, and the original's page passed over on the way, if any.-Future<(_Candidate?, _Candidate?)> _output(+/// A page that would show readers less than it shows here, and what it loads that the Hub cannot.+typedef _NeedsFiles = ({String path, List<String> loads});++/// The page readers see, the original's page passed over on the way, and a page passed over for+/// needing other files, if any.+typedef _Output = ({+ _Candidate? page,+ _Candidate? stale,+ _NeedsFiles? needsFiles,+});++Future<_Output> _output(List<_Candidate> candidates,String? viewer,Map<String, String> original,) async {_Candidate? stale;- for (final name in {?viewer, 'preview.html'}) {+ _NeedsFiles? needsFiles;+ final named = {?viewer, 'preview.html'};+ for (final name in named) {final page = candidates.where((c) => c.path == name && name.endsWith('.html')).firstOrNull;if (page == null) continue;- if (original.containsKey(name) && await page.content() == original[name]) {+ if (await _isOriginal(page, original)) {stale ??= page;continue;}- return (page, stale);+ return (page: page, stale: stale, needsFiles: null);+ }+ for (final page in _madePages(candidates, skip: named)) {+ if (await _isOriginal(page, original)) {+ stale ??= page;+ continue;+ }+ // Measured before reading, as every file is: a page over the limit is never the output.+ if (await page.sentBytes() > hubMaxFileChars) continue;+ final content = await page.content();+ if (content == null) continue;+ final loads = outsideResources(content);+ if (loads.isEmpty) return (page: page, stale: stale, needsFiles: null);+ needsFiles ??= (path: page.path, loads: loads);+ }+ return (page: null, stale: stale, needsFiles: needsFiles);+}++/// Pages that may be the session's output, other than [skip]: the top level's (`index.html` first,+/// then by name), then each folder's `index.html`, shallowest first, then by path.+List<_Candidate> _madePages(+ List<_Candidate> candidates, {+ required Set<String> skip,+}) {+ int rank(_Candidate c) =>+ c.depth == 0 ? (c.path == 'index.html' ? 0 : 1) : 1 + c.depth;+ return candidates+ .where(+ (c) =>+ !skip.contains(c.path) &&+ (c.depth == 0+ ? p.extension(c.path).toLowerCase() == '.html'+ : p.basename(c.path) == 'index.html'),+ )+ .toList()+ ..sort(+ (a, b) =>+ rank(a) != rank(b) ? rank(a) - rank(b) : a.path.compareTo(b.path),+ );+}++/// A fork's page still as it arrived, which shows the original rather than this session's result.+Future<bool> _isOriginal(_Candidate page, Map<String, String> original) async =>+ original.containsKey(page.path) &&+ await page.content() == original[page.path];++/// What to ask the agent for when the project has no page of its own to show.+String _needsOutputMessage(_Output output) {+ final stale = output.stale, needsFiles = output.needsFiles;+ if (stale != null) {+ return '${stale.path} is still the original\'s and has none of your changes. '+ 'Ask your agent to update it to show the current result, then publish again.';+ }+ if (needsFiles != null) {+ return '${needsFiles.path} loads ${_listed(needsFiles.loads)}, which the Hub cannot reach. '+ 'Ask your agent for a preview.html with everything inside it, then publish again.';}- return (null, stale);+ return 'The Hub shows what a session made. Ask your agent for a preview.html that runs '+ 'on its own (for a review, a page presenting it), then publish again.';}+String _listed(List<String> items) =>+ items.take(4).join(', ') ++ (items.length > 4 ? ' and ${items.length - 4} more' : '');+Future<(List<_Candidate>, bool)> _candidates(Directory root) async {final found = <_Candidate>[];var visited = 0, scannedAll = true;diff --git a/desktop/lib/community/publish_project.dart b/desktop/lib/community/publish_project.dartindex f987a326d..18b61fe06 100644--- a/desktop/lib/community/publish_project.dart+++ b/desktop/lib/community/publish_project.dart@@ -139,10 +139,13 @@ class PublicationHandoff {{'127.0.0.1', 'localhost'}.contains(target.host))) {throw const FormatException('Invalid Hub address.');}- final bytes = jsonEncode(draft);- if (utf8.encode(bytes).length > 6000000) {+ final json = utf8.encode(jsonEncode(draft));+ if (json.length > 6000000) {throw const FormatException('Keep this harness under 6 MB.');}+ // Sent as a gzipped file: Cloudflare's rules in front of the Hub refuse a body with raw+ // `<script>` in it, and a draft carries what the session made. The Hub's import unpacks it.+ final packed = base64Encode(gzip.encode(json));final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);final token = List.generate(24,@@ -176,7 +179,8 @@ class PublicationHandoff {"default-src 'none'; script-src 'nonce-$token'; form-action ${target.origin}; frame-ancestors 'none'",);request.response.write(- '<!doctype html><title>Review your harness</title><p>Opening your draft in Harness Hub…</p><form method="post" enctype="multipart/form-data" action="${escape.convert(target.resolve('/hub/import').toString())}"><textarea name="draft" hidden>${escape.convert(bytes)}</textarea><button>Continue to Hub</button></form><script nonce="$token">document.forms[0].submit()</script>',+ '<!doctype html><title>Review your harness</title><p>Opening your draft in Harness Hub…</p><form method="post" enctype="multipart/form-data" action="${escape.convert(target.resolve('/hub/import').toString())}"><input type="file" name="draft" hidden><button>Continue to Hub</button></form>'+ '<script nonce="$token">const bytes=Uint8Array.from(atob("$packed"),c=>c.charCodeAt(0));const files=new DataTransfer();files.items.add(new File([bytes],"draft.json.gz",{type:"application/gzip"}));const form=document.forms[0];form.elements.draft.files=files.files;form.submit()</script>',);await request.response.close();await handoff.close();diff --git a/desktop/test/community_output_check_test.dart b/desktop/test/community_output_check_test.dartnew file mode 100644index 000000000..afc4f1f9a--- /dev/null+++ b/desktop/test/community_output_check_test.dart@@ -0,0 +1,26 @@+import 'package:flutter_test/flutter_test.dart';+import 'package:harness/community/output_check.dart';++/// The website's own cases (website/src/lib/community/outputCheck.spec.ts), run against the copy.+void main() {+ test('names files and addresses the Hub preview cannot reach', () {+ const html = '''<link rel="stylesheet" href="style.css"><link rel="icon" href="favicon.ico">+ <script type="module" src='./app.js'></script><img src=https://cdn.example/logo.png>+ <style>@import "fonts.css"; body { background: url(bg.png) }</style><a href="other.html">More</a>''';+ expect(outsideResources(html), [+ './app.js',+ 'https://cdn.example/logo.png',+ 'style.css',+ 'bg.png',+ 'fonts.css',+ ]);+ });+ test('a page that carries everything inside it loads nothing', () {+ expect(+ outsideResources(+ '<style>i{background:url("data:image/png;base64,AA==")}</style><img src="data:image/png;base64,AA=="><script>go()</script><a href="#top">Top</a>',+ ),+ isEmpty,+ );+ });+}diff --git a/desktop/test/community_publish_test.dart b/desktop/test/community_publish_test.dartindex 28727f2d8..40ef82a35 100644--- a/desktop/test/community_publish_test.dart+++ b/desktop/test/community_publish_test.dart@@ -75,30 +75,116 @@ void main() {);expect((draft['files'] as List).map((f) => f['path']), ['preview.html']);}, skip: Platform.isWindows);+ test('asks for a preview.html, naming what an app page loads that the Hub cannot', () async {+ await file('code.py', 'print(1)');+ await file(+ 'index.html',+ '<link rel="stylesheet" href="style.css"><script src="app.js"></script>',+ );+ Future<Map<String, dynamic>> build() => buildPublicationDraft(+ folder: root.path,+ title: 'Test',+ engine: 'codex',+ );+ await expectLater(+ build(),+ throwsA(+ isA<NeedsOutput>().having(+ (e) => e.message,+ 'message',+ allOf(+ contains('index.html loads app.js, style.css'),+ contains('preview.html'),+ ),+ ),+ ),+ );+ await file('preview.html', 'x' * 3000001);+ await expectLater(build(), throwsFormatException);+ });+ test('asks for a preview.html when the project has no page at all', () async {+ await file('code.py', 'print(1)');+ await expectLater(+ buildPublicationDraft(folder: root.path, title: 'Test', engine: 'codex'),+ throwsA(+ isA<NeedsOutput>().having(+ (e) => e.message,+ 'message',+ contains('The Hub shows what a session made'),+ ),+ ),+ );+ });+ test('a one-file game is the output under any top-level name', () async {+ const game =+ '<canvas id="c"></canvas><style>body{background:url(data:image/png;base64,AA==)}</style>'+ '<script>requestAnimationFrame(function loop(){requestAnimationFrame(loop)})</script>';+ await file('src/nested.html', '<p>Not at the top</p>');+ await file('game.html', '<script src="engine.js"></script>');+ await file('snake.html', game);+ Future<Map<String, dynamic>> build() => buildPublicationDraft(+ folder: root.path,+ title: 'Snake',+ engine: 'claude',+ );+ expect((await build())['viewerPath'], 'snake.html');+ await file('index.html', game);+ expect((await build())['viewerPath'], 'index.html');+ await file('preview.html', '<h1>Made for the Hub</h1>');+ expect((await build())['viewerPath'], 'preview.html');+ });+ test('a game in a folder of its own is the output', () async {+ const game = '<canvas></canvas><script>start()</script>';+ await file('docs/guide.html', '<p>Not an output</p>');+ await file('tank-1987/index.html', game);+ await file('tank-1987/levels/index.html', game);+ Future<Map<String, dynamic>> build() => buildPublicationDraft(+ folder: root.path,+ title: 'Tank',+ engine: 'codex',+ );+ expect((await build())['viewerPath'], 'tank-1987/index.html');+ await file('notes.html', game);+ expect((await build())['viewerPath'], 'notes.html');+ });test(- 'asks for a preview.html, never assuming an app page is the output',+ 'a folder\'s index.html that needs other files is never assumed',() async {- await file('code.py', 'print(1)');- await file('index.html', '<script src="app.js"></script>');- Future<Map<String, dynamic>> build() => buildPublicationDraft(- folder: root.path,- title: 'Test',- engine: 'codex',- );+ await file('app/index.html', '<script src="main.js"></script>');await expectLater(- build(),+ buildPublicationDraft(folder: root.path, title: 'App', engine: 'codex'),throwsA(- isA<FormatException>().having(+ isA<NeedsOutput>().having((e) => e.message,'message',- contains('preview.html'),+ contains('app/index.html loads main.js'),),),);- await file('preview.html', 'x' * 3000001);- await expectLater(build(), throwsFormatException);},);+ test('passes over a fork\'s unchanged top-level page', () async {+ const original = '<h1>Original game</h1>';+ await file('index.html', original);+ await file(+ 'OPEN-HARNESS.json',+ jsonEncode({+ 'files': [+ {'path': 'index.html', 'content': original},+ ],+ }),+ );+ await expectLater(+ buildPublicationDraft(folder: root.path, title: 'Fork', engine: 'codex'),+ throwsA(+ isA<NeedsOutput>().having(+ (e) => e.message,+ 'message',+ contains('index.html is still the original'),+ ),+ ),+ );+ });test('a fork keeps the page it was published with', () async {await file('index.html', '<h1>Moonlight</h1>');await file(@@ -308,9 +394,15 @@ void main() {'action="${const HtmlEscape().convert('https://harness.autonomous.ai/hub/import')}"',),);- expect(html, contains(const HtmlEscape().convert('</textarea>')));expect(html, isNot(contains('<script>bad()')));expect(html, isNot(contains('/api/community/harnesses')));+ // The draft goes as a gzipped file, never as text the edge could read.+ expect(html, contains('<input type="file" name="draft" hidden>'));+ final packed = RegExp(r'atob\("([A-Za-z0-9+/=]+)"\)').firstMatch(html)![1]!;+ expect(+ jsonDecode(utf8.decode(gzip.decode(base64Decode(packed)))),+ draft,+ );await handoff.close();});test('login return accepts only local Hub pages', () {diff --git a/website/README.md b/website/README.mdindex 6442d3218..6a75e6905 100644--- a/website/README.md+++ b/website/README.md@@ -119,11 +119,21 @@ transferred. Nothing becomes public until the creator reviews the output, sourceand clicks Publish. A draft survives the existing sign-in flow; repeated submission uses onepublication ID. Remote projects and older apps can use the project-folder picker or import`OPEN-HARNESS.json`. The output is the project's `preview.html` (or the page a fork arrived with,-once changed). Without one, as for a native result such as a Typst PDF or a Blender model, the+once changed), or else a page the session made that loads nothing the sandbox cannot reach, such as+a one-file game: a top-level page (`index.html` first), then a folder's `index.html`. Without one, as for a native result such as a Typst PDF or a Blender model, thedesktop asks the machine's daemon for a picture of the harness's viewer (`viewer_surface`, headlessChrome) and publishes it as `preview.html` and the cover; a fork still opens the real viewer. The first version requires a self-contained HTML preview, up to 30 files/6 MB;hidden configuration, symlinks and installed dependencies are excluded. Conversation tails may be-incomplete and the review form says so. `/explore` links remain compatible with `/hub`.+incomplete and the review form says so. A draft that arrives from Desktop or a bundle is translated+into English once the creator is signed in (`POST /api/community/translate`: title, description and+the people's and agent's turns; tool output stays as it is), and the form can switch back to the+original before publishing. `/explore` links remain compatible with `/hub`.++What people publish is HTML and JavaScript, and Cloudflare's managed rules in front of the Hub and+its API refuse a request body with raw `<script>` in it. So bodies travel gzipped: Desktop hands+off the draft as a `draft.json.gz` file, `communityRequest` sends every body as `application/gzip`,+the proxy forwards those bytes unchanged, and the backend unpacks them (bounded) before validating+(`backend/src/lib/gzipJson.ts`). Plain JSON bodies and an older Desktop's text field still work.Backend reads are public; likes, comments, following, publication, and unpublication require SSO.Counts are persisted, like/follow writes are idempotent, comment retries have per-user request IDs,diff --git a/website/src/app/api/community/[...path]/route.spec.ts b/website/src/app/api/community/[...path]/route.spec.tsindex 8de10ba2e..a2adea392 100644--- a/website/src/app/api/community/[...path]/route.spec.ts+++ b/website/src/app/api/community/[...path]/route.spec.ts@@ -1,4 +1,5 @@// @vitest-environment node+import { gzipSync } from 'node:zlib';import { afterEach, describe, expect, it, vi } from 'vitest';import { DELETE, GET, POST } from './route';@@ -15,6 +16,26 @@ describe('community API boundary', () => {expect(options.headers.get('cookie')).toBeNull();expect(options.redirect).toBe('error'); expect(response.headers.get('cache-control')).toBe('no-store');});+ it('forwards a translation with the longer wait it needs', async () => {+ vi.stubEnv('COMMUNITY_API_URL', 'http://127.0.0.1:54882');+ const timeout = vi.spyOn(AbortSignal, 'timeout');+ const fetcher = vi.fn().mockResolvedValue(Response.json({ success: true, data: {} })); vi.stubGlobal('fetch', fetcher);+ await POST(new Request('https://harness.example/api/community/translate', { method: 'POST', body: '{"title":"Tiêu đề"}' }), { params: Promise.resolve({ path: ['translate'] }) });+ expect(String(fetcher.mock.calls[0][0])).toBe('http://127.0.0.1:54882/api/community/translate');+ expect(timeout).toHaveBeenCalledWith(58_000);+ expect((await POST(new Request('https://harness.example/api/community/translate/x', { method: 'POST' }), { params: Promise.resolve({ path: ['translate', 'x'] }) })).status).toBe(404);+ timeout.mockRestore();+ });+ it('forwards a gzipped body byte for byte, still gzipped', async () => {+ vi.stubEnv('COMMUNITY_API_URL', 'http://127.0.0.1:54882');+ const fetcher = vi.fn().mockResolvedValue(Response.json({ success: true, data: { id: 'x' } }, { status: 201 })); vi.stubGlobal('fetch', fetcher);+ const gzipped = gzipSync(JSON.stringify({ files: [{ path: 'index.html', content: '<script>go()</script>' }] }));+ const response = await POST(new Request('https://harness.example/api/community/harnesses', { method: 'POST', body: gzipped, headers: { 'content-type': 'application/gzip' } }), { params: Promise.resolve({ path: ['harnesses'] }) });+ expect(response.status).toBe(201);+ const options = fetcher.mock.calls[0][1];+ expect(options.headers.get('content-type')).toBe('application/gzip');+ expect(Buffer.from(options.body).equals(gzipped)).toBe(true);+ });it('rejects arbitrary proxy destinations and oversized publications before forwarding', async () => {const fetcher = vi.fn(); vi.stubGlobal('fetch', fetcher);expect((await GET(new Request('https://harness.example/api/community/auth'), { params: Promise.resolve({ path: ['..', 'auth'] }) })).status).toBe(404);diff --git a/website/src/app/api/community/[...path]/route.ts b/website/src/app/api/community/[...path]/route.tsindex 06af96da4..e4ce4d72b 100644--- a/website/src/app/api/community/[...path]/route.ts+++ b/website/src/app/api/community/[...path]/route.ts@@ -1,7 +1,10 @@+import { gzipType } from '@/lib/community/gzip';import { communityApiOrigin } from '@/lib/community/server';type Context = { params: Promise<{ path: string[] }> };-const allowed = /^(?:harnesses(?:\/[a-z0-9-]{1,80}(?:\/like|\/social|\/comments(?:\/[a-f0-9-]{36})?)?)?|creators\/[a-z0-9-]{1,80}\/follow)$/;+const allowed = /^(?:harnesses(?:\/[a-z0-9-]{1,80}(?:\/like|\/social|\/comments(?:\/[a-f0-9-]{36})?)?)?|creators\/[a-z0-9-]{1,80}\/follow|translate)$/;+/** A translation asks the model several times over (the backend gives it 50 seconds); every other request answers within the usual wait. */+const waitMs = (path: string) => path === 'translate' ? 58_000 : 12_000;const coverPath = /^harnesses\/[a-f0-9-]{36}\/cover$/;const coverTypes = new Set(['image/png', 'image/jpeg', 'image/webp']);const unavailable = () => Response.json({ success: false, error: { message: 'The community is temporarily unavailable. Try again.' } }, { status: 503, headers: { 'Cache-Control': 'no-store' } });@@ -29,13 +32,15 @@ async function forward(request: Request, context: Context): Promise<Response> {for (const key of ['authorization', 'x-autonomous-env']) {const value = request.headers.get(key); if (value) headers.set(key, value);}- const body = ['GET', 'HEAD'].includes(request.method) ? undefined : (await request.text() || undefined);- if (body) headers.set('Content-Type', 'application/json');- if (body && new TextEncoder().encode(body).length > 6_100_000) return Response.json({ success: false, error: { message: 'Keep the snapshot under 6 MB.' } }, { status: 413 });+ // Bytes, as sent: a gzipped body (lib/community/gzip.ts) stays gzipped to the backend, which unpacks it.+ const bytes = ['GET', 'HEAD'].includes(request.method) ? undefined : new Uint8Array(await request.arrayBuffer());+ const body = bytes?.length ? bytes : undefined;+ if (body) headers.set('Content-Type', request.headers.get('content-type') === gzipType ? gzipType : 'application/json');+ if (body && body.length > 6_100_000) return Response.json({ success: false, error: { message: 'Keep the snapshot under 6 MB.' } }, { status: 413 });try {const url = new URL(request.url), target = new URL(sessionPath ? `/api/auth/${sessionPath}` : `/api/community/${path}`, communityApiOrigin());target.search = url.search;- const response = await fetch(target, { method: request.method, headers, body, redirect: 'error', cache: 'no-store', signal: AbortSignal.timeout(12_000) });+ const response = await fetch(target, { method: request.method, headers, body, redirect: 'error', cache: 'no-store', signal: AbortSignal.timeout(waitMs(path)) });const content = await response.json();return Response.json(content, { status: response.status, headers: { 'Cache-Control': 'no-store' } });} catch {diff --git a/website/src/app/hub/import/route.spec.ts b/website/src/app/hub/import/route.spec.tsindex 3e15fd2e6..522705ec3 100644--- a/website/src/app/hub/import/route.spec.ts+++ b/website/src/app/hub/import/route.spec.ts@@ -1,4 +1,5 @@// @vitest-environment node+import { gzipSync } from 'node:zlib';import { describe, expect, it } from 'vitest';import { POST } from './route';const draft = { version: 1, title: '</script><script>alert(1)</script>', files: [{ path: 'preview.html', content: '<h1>Hello</h1>' }], viewerPath: 'preview.html', conversation: [{ role: 'user', text: 'A reviewed brief.' }] };@@ -20,6 +21,14 @@ describe('private desktop draft handoff', () => {expect((await POST(request({ ...draft, viewerPath: 'missing.html' }))).status).toBe(400);expect((await POST(request({ ...draft, files: [null] }))).status).toBe(400);});+ it('opens the gzipped draft file Desktop sends, and refuses one that is not gzip or unpacks too large', async () => {+ const send = (bytes: Uint8Array) => { const body = new FormData(); body.set('draft', new File([new Uint8Array(bytes)], 'draft.json.gz', { type: 'application/gzip' })); return POST(new Request('http://localhost/hub/import', { method: 'POST', body })); };+ const response = await send(gzipSync(JSON.stringify(draft)));+ expect(response.status).toBe(200);+ expect(await response.text()).toContain('A reviewed brief.');+ expect((await send(new TextEncoder().encode('not gzip'))).status).toBe(400);+ expect((await send(gzipSync(Buffer.alloc(6_500_000, 32)))).status).toBe(400);+ });it('bounds streamed input even without a content-length header', async () => {const body = new ReadableStream({ start(controller) { controller.enqueue(new Uint8Array(7_000_001)); controller.close(); } });const incoming = new Request('http://localhost/hub/import', { method: 'POST', body, duplex: 'half' } as RequestInit);diff --git a/website/src/app/hub/import/route.ts b/website/src/app/hub/import/route.tsindex 12d4ef570..8d08f7623 100644--- a/website/src/app/hub/import/route.ts+++ b/website/src/app/hub/import/route.ts@@ -1,6 +1,18 @@import { randomUUID } from 'node:crypto';+import { gunzipSync } from 'node:zlib';import { draftDatabase, validateDraft } from '@/lib/community/drafts';+/**+ * The draft's JSON: a gzipped file from Desktop (opaque to Cloudflare's rules, which refuse raw+ * `<script>` in a body), or the text field an older Desktop sends. Unpacking is bounded.+ */+async function draftText(field: FormDataEntryValue | null): Promise<string | null> {+ if (typeof field === 'string') return field;+ if (!field) return null;+ try { return gunzipSync(Buffer.from(await field.arrayBuffer()), { maxOutputLength: 6_000_001 }).toString('utf8'); }+ catch { return null; }+}+/** Native Share hands off a draft, never a publication or a credential. */export async function POST(request: Request): Promise<Response> {try {@@ -16,8 +28,8 @@ export async function POST(request: Request): Promise<Response> {const bytes = new Uint8Array(length); let offset = 0;for (const chunk of chunks) { bytes.set(chunk, offset); offset += chunk.length; }const form = await new Response(bytes, { headers: { 'Content-Type': request.headers.get('Content-Type') || '' } }).formData();- const raw = form.get('draft');- if (typeof raw !== 'string' || new TextEncoder().encode(raw).length > 6_000_000) return new Response('Invalid draft.', { status: 400 });+ const raw = await draftText(form.get('draft'));+ if (raw === null || new TextEncoder().encode(raw).length > 6_000_000) return new Response('Invalid draft.', { status: 400 });const draft = validateDraft(JSON.parse(raw));const id = randomUUID(), nonce = randomUUID();const payload = JSON.stringify(draft).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029');diff --git a/website/src/app/hub/publish/components/EnglishNotice.tsx b/website/src/app/hub/publish/components/EnglishNotice.tsxnew file mode 100644index 000000000..8e1131abc--- /dev/null+++ b/website/src/app/hub/publish/components/EnglishNotice.tsx@@ -0,0 +1,13 @@+import type { EnglishState } from '@/lib/community/translation';+import styles from '../../community.module.css';++type Props = { english: EnglishState; signedIn: boolean | null; showOriginal?: () => void; useEnglish?: () => void; retry?: () => void };++/** Where the draft's English stands, with the one action that fits. */+export function EnglishNotice({ english, signedIn, showOriginal, useEnglish, retry }: Props) {+ if (english.status === 'pending') return signedIn ? <p className={styles.notice} role="status">Translating your title, description and conversation into English…</p> : null;+ if (english.status === 'translated') return <p className={styles.notice} role="status">Translated into English for the Hub. Review it below.<button type="button" onClick={showOriginal}>Show original</button></p>;+ if (english.status === 'original') return <p className={styles.notice} role="status">Showing your original words.<button type="button" onClick={useEnglish}>Use English</button></p>;+ if (english.status === 'failed') return <p className={styles.notice} role="status">Could not translate into English. {english.message}<button type="button" onClick={retry}>Try again</button></p>;+ return null;+}diff --git a/website/src/app/hub/publish/page.spec.tsx b/website/src/app/hub/publish/page.spec.tsxindex 7aa51875a..1e0275010 100644--- a/website/src/app/hub/publish/page.spec.tsx+++ b/website/src/app/hub/publish/page.spec.tsx@@ -8,17 +8,22 @@ const mocks = vi.hoisted(() => ({ request: vi.fn(), push: vi.fn(), headers: vi.fvi.mock('next/navigation', () => ({ useRouter: () => ({ push: mocks.push }) }));vi.mock('@/lib/community/client', () => ({ communityRequest: mocks.request, sessionHeaders: mocks.headers }));const project = { version: 1, title: 'My orbit', description: 'A new perspective', category: 'Experiments', engine: 'Codex', viewerPath: 'index.html', files: [{ path: 'index.html', content: '<h1>My orbit</h1>' }], conversation: [{ role: 'user', text: 'Change the orbit.' }], forkedFrom: 'starter-orbit' };-beforeEach(() => { vi.clearAllMocks(); mocks.headers.mockReturnValue({ Authorization: 'Bearer fixture' }); });+/** The Hub as it answers by default: a draft already in English, and a publication's id. */+const hub = (english?: object) => async (path: string, options?: { body?: unknown }) =>+ path === 'translate' ? (english ? { ...english, translated: true } : { ...options?.body as object, translated: false }) : { id: 'published-fixture' };+const published = () => mocks.request.mock.calls.filter(([path]) => path === 'harnesses');+const translations = () => mocks.request.mock.calls.filter(([path]) => path === 'translate');+const bundleFile = (bundle: object) => Object.assign(new File([JSON.stringify(bundle)], 'OPEN-HARNESS.json', { type: 'application/json' }), { text: async () => JSON.stringify(bundle) });+beforeEach(() => { vi.clearAllMocks(); mocks.headers.mockReturnValue({ Authorization: 'Bearer fixture' }); mocks.request.mockImplementation(hub()); });describe('explicit publication', () => {it('imports a fork, previews it safely, and preserves attribution when publishing', async () => {- mocks.request.mockResolvedValue({ id: 'published-fixture' });render(<PublishPage />);const file = Object.assign(new File([JSON.stringify(project)], 'OPEN-HARNESS.json', { type: 'application/json' }), { text: async () => JSON.stringify(project) });fireEvent.change(screen.getByLabelText('Import fork bundle'), { target: { files: [file] } });await waitFor(() => expect(screen.getByDisplayValue('My orbit')).toBeInTheDocument());expect(screen.getByTitle('Publication preview')).toHaveAttribute('sandbox', 'allow-scripts');expect(screen.getByRole('button', { name: 'Publish harness' })).toBeDisabled();- expect(mocks.request).not.toHaveBeenCalled();+ expect(published()).toHaveLength(0);expect(screen.getByText(/index.html is still the original/)).toBeInTheDocument();fireEvent.click(screen.getByRole('checkbox', { name: /I have permission/ }));expect(screen.getByRole('button', { name: 'Publish harness' })).toBeDisabled();@@ -27,8 +32,9 @@ describe('explicit publication', () => {fireEvent.click(screen.getByRole('checkbox', { name: /I have permission/ }));fireEvent.click(screen.getByRole('button', { name: 'Publish harness' }));await waitFor(() => expect(mocks.push).toHaveBeenCalledWith('/hub/published-fixture'));- expect(mocks.request.mock.calls[0][1].body).toMatchObject({ forkedFrom: 'starter-orbit', confirmed: true, license: 'MIT', files: [{ path: 'index.html', content: '<h1>My new orbit</h1>' }], conversation: project.conversation });- expect(mocks.request.mock.calls[0][1].body).not.toHaveProperty('originalOutput');+ expect(published()[0][1].body).toMatchObject({ forkedFrom: 'starter-orbit', confirmed: true, license: 'MIT', files: [{ path: 'index.html', content: '<h1>My new orbit</h1>' }], conversation: project.conversation });+ expect(published()[0][1].body).not.toHaveProperty('originalOutput');+ expect(published()[0][1].body).not.toHaveProperty('english');});it('keeps a draft after a failed publication and requires sign-in', async () => {mocks.headers.mockReturnValue({}); render(<PublishPage />); await act(async () => {});@@ -78,6 +84,54 @@ describe('explicit publication', () => {fireEvent.click(screen.getByRole('checkbox', { name: /I have permission/ }));expect(screen.getByRole('button', { name: 'Publish harness' })).toBeEnabled();});+ it('shows a one-file game as the output without asking for a preview.html', async () => {+ render(<PublishPage />); await act(async () => {});+ const game = '<canvas></canvas><script>requestAnimationFrame(function loop(){requestAnimationFrame(loop)})</script>';+ const folder = [['docs/guide.html', '<p>Guide</p>'], ['snake/index.html', game], ['notes.md', '# Snake']].map(([path, text]) => Object.assign(new File([text], path), { webkitRelativePath: `project/${path}`, text: async () => text }));+ fireEvent.change(screen.getByLabelText('Choose project folder'), { target: { files: folder } });+ expect(await screen.findByTitle('Publication preview')).toBeInTheDocument();+ expect(screen.queryByText(/The Hub shows what a session made/)).not.toBeInTheDocument();+ expect(screen.queryByText(/Your output loads/)).not.toBeInTheDocument();+ });+ it('translates an arriving draft into English and keeps the original to switch back to', async () => {+ const original = { ...project, title: 'Quỹ đạo của tôi', description: 'Một góc nhìn mới', conversation: [{ role: 'user', text: 'Đổi quỹ đạo.' }, { role: 'tool', text: 'ok' }] };+ mocks.request.mockImplementation(hub({ title: 'My orbit', description: 'A new perspective', conversation: [{ role: 'user', text: 'Change the orbit.' }, { role: 'tool', text: 'ok' }] }));+ render(<PublishPage />);+ fireEvent.change(screen.getByLabelText('Import fork bundle'), { target: { files: [bundleFile(original)] } });+ expect(await screen.findByDisplayValue('My orbit')).toBeInTheDocument();+ expect(translations()).toHaveLength(1);+ expect(translations()[0][1].body).toEqual({ title: original.title, description: original.description, conversation: original.conversation });+ expect(screen.getByText(/Translated into English/)).toBeInTheDocument();+ expect(screen.getByDisplayValue('Change the orbit.')).toBeInTheDocument();+ fireEvent.click(screen.getByRole('button', { name: 'Show original' }));+ expect(screen.getByDisplayValue('Quỹ đạo của tôi')).toBeInTheDocument();+ expect(screen.getByDisplayValue('Đổi quỹ đạo.')).toBeInTheDocument();+ fireEvent.click(screen.getByRole('button', { name: 'Use English' }));+ expect(screen.getByDisplayValue('My orbit')).toBeInTheDocument();+ expect(translations()).toHaveLength(1);+ });+ it('translates only once signed in, says when it failed, and tries again on request', async () => {+ mocks.headers.mockReturnValue({});+ render(<PublishPage />); await act(async () => {});+ fireEvent.change(screen.getByLabelText('Import fork bundle'), { target: { files: [bundleFile({ ...project, title: 'Quỹ đạo' })] } });+ await screen.findByDisplayValue('Quỹ đạo');+ expect(translations()).toHaveLength(0);+ mocks.request.mockRejectedValueOnce(new Error('The translation failed. Try again.'));+ mocks.headers.mockReturnValue({ Authorization: 'Bearer fixture' }); fireEvent.focus(window);+ expect(await screen.findByText(/Could not translate into English/)).toBeInTheDocument();+ expect(screen.getByDisplayValue('Quỹ đạo')).toBeInTheDocument();+ mocks.request.mockImplementation(hub({ title: 'Orbit', description: project.description, conversation: project.conversation }));+ fireEvent.click(screen.getByRole('button', { name: 'Try again' }));+ expect(await screen.findByDisplayValue('Orbit')).toBeInTheDocument();+ expect(translations()).toHaveLength(2);+ });+ it('never translates what the author types here', async () => {+ render(<PublishPage />); await act(async () => {});+ fireEvent.change(screen.getByLabelText('Title'), { target: { value: 'Trò chơi rắn' } });+ await act(async () => {});+ expect(translations()).toHaveLength(0);+ expect(screen.queryByText(/Translat/)).not.toBeInTheDocument();+ });it('still knows a fork\'s original output after a reload or a sign-in', async () => {vi.mocked(readDraft).mockResolvedValueOnce({ ...project, originalOutput: project.files[0].content } as never);render(<PublishPage />);diff --git a/website/src/app/hub/publish/page.tsx b/website/src/app/hub/publish/page.tsxindex a0128f161..69bbdfb37 100644--- a/website/src/app/hub/publish/page.tsx+++ b/website/src/app/hub/publish/page.tsx@@ -10,12 +10,14 @@ import { findSecrets } from '@/lib/community/secrets';import { Header, SignIn } from '../components/Header';import { useSignedIn } from '../components/useSignedIn';import { ConversationEditor } from './components/ConversationEditor';+import { EnglishNotice } from './components/EnglishNotice';import { DetailsFields } from './components/DetailsFields';import { FilesReview } from './components/FilesReview';import { OutputInputs, SourceInputs } from './components/ProjectInputs';import { PublishChecks } from './components/PublishChecks';import { originalOutput, readCover, readProjectFolder } from './projectFiles';import { toSnapshot, usePublishDraft, type PublishDraft } from './usePublishDraft';+import { useEnglishDraft } from './useEnglishDraft';import styles from '../community.module.css';const reviewInputs = ['confirmation', 'secrets'];@@ -23,6 +25,7 @@ const reviewInputs = ['confirmation', 'secrets'];export default function PublishPage() {const router = useRouter(), signedIn = useSignedIn();const { draft, update, applyBundle, saveError, publishOnce } = usePublishDraft();+ const english = useEnglishDraft(draft, update, signedIn);const [confirmed, setConfirmed] = useState(false), [checkedSecrets, setCheckedSecrets] = useState(''), [busy, setBusy] = useState(false), [error, setError] = useState('');const findings = useMemo(() => findSecrets(draft.files, draft.conversation), [draft.files, draft.conversation]);// The acknowledgement is for these findings: a new one needs checking again.@@ -31,7 +34,7 @@ export default function PublishPage() {const html = draft.files.find(file => file.path === draft.viewerPath && !file.encoding)?.content;const outside = useMemo(() => html ? outsideResources(html) : [], [html]);const unchanged = html !== undefined && html === draft.originalOutput;- const ready = !!signedIn && !!html && !unchanged && confirmed && !marker && secretsChecked;+ const ready = !!signedIn && !!html && !unchanged && confirmed && !marker && secretsChecked && draft.english.status !== 'pending';/** A changed project has to be reviewed again before it is published. */function replaceProject(patch: Partial<PublishDraft>) { update(patch); setConfirmed(false); setError(''); }@@ -70,6 +73,7 @@ export default function PublishPage() {{signedIn === false && <SignIn action="publish your harness" />}<form onChange={event => { if (!(event.target instanceof HTMLInputElement && reviewInputs.includes(event.target.name))) setConfirmed(false); }} onSubmit={publish}><div className={styles.importActions}><strong>Bring your harness here.</strong><p>In the desktop app, open Share → Publish to Hub. Your current files and conversation arrive here for review.</p><p>You can also choose a project folder below. Include a self-contained HTML preview so everyone can see the result.</p></div>+ <EnglishNotice english={draft.english} signedIn={signedIn} {...english} /><div className={styles.publishForm}><SourceInputs onFolder={files => void importFolder(files)} onBundle={file => void importBundle(file)} /><DetailsFields draft={draft} update={update} />diff --git a/website/src/app/hub/publish/projectFiles.ts b/website/src/app/hub/publish/projectFiles.tsindex 02a045069..9a2d1e739 100644--- a/website/src/app/hub/publish/projectFiles.ts+++ b/website/src/app/hub/publish/projectFiles.ts@@ -1,5 +1,6 @@import { validateDraft, type HubDraft } from '@/lib/community/drafts';import { communityLimits } from '@/lib/community/contract';+import { outsideResources } from '@/lib/community/outputCheck';import type { SourceFile } from '@/lib/community/types';const skippedFolders = ['node_modules', 'build', 'dist', 'target', 'vendor', '__pycache__'];@@ -19,6 +20,13 @@ function base64(bytes: Uint8Array): string {return btoa(raw);}+const depth = (path: string) => path.split('/').length - 1;+/** A page that may be the session's output: any at the top level, or a folder's index.html (an agent often puts a game in `<name>/`). */+const madePage = (f: SourceFile) => f.path.endsWith('.html') && !f.encoding && (!depth(f.path) || f.path.endsWith('/index.html'));+/** The top level's pages (index.html first, then by name), then each folder's index.html, shallowest first. */+const madeRank = (path: string) => depth(path) ? 1 + depth(path) : path === 'index.html' ? 0 : 1;+const madePageOrder = (a: SourceFile, b: SourceFile) => madeRank(a.path) - madeRank(b.path) || a.path.localeCompare(b.path);+/** A file's size as sent, known before reading it: base64 for binary, at least its characters for text. */const sentSize = (file: File, encoded: boolean) => encoded ? Math.ceil(file.size / 3) * 4 : file.size;@@ -47,9 +55,11 @@ export async function readProjectFolder(selected: Iterable<File>, preferredVieweif (size > communityLimits.projectBytes) throw new Error(`Keep the project under 6 MB as sent. ${sentNote}`);files.push({ path, content, ...(encoded ? { encoding: 'base64' as const } : {}) });}- // The page already chosen, or else preview.html. Any other page is offered, never assumed: an app's- // index.html usually needs files the Hub's sandbox cannot load.- const viewer = [preferredViewer, 'preview.html'].map(name => files.find(f => f.path === name && f.path.endsWith('.html') && !f.encoding)).find(Boolean);+ // The page already chosen, or else preview.html, or else a page the session made that runs on its own.+ // Any other page is offered, never assumed: an app's index.html usually needs files the Hub's sandbox+ // cannot load, while a one-file game shows as it is.+ const viewer = [preferredViewer, 'preview.html'].map(name => files.find(f => f.path === name && f.path.endsWith('.html') && !f.encoding)).find(Boolean)+ ?? files.filter(madePage).sort(madePageOrder).find(f => !outsideResources(f.content).length);if (!files.length) throw new Error('This folder has no portable project files.');return { files, viewerPath: viewer?.path ?? preferredViewer, origin };}diff --git a/website/src/app/hub/publish/useEnglishDraft.ts b/website/src/app/hub/publish/useEnglishDraft.tsnew file mode 100644index 000000000..5cef0f9de--- /dev/null+++ b/website/src/app/hub/publish/useEnglishDraft.ts@@ -0,0 +1,38 @@+'use client';+import { useEffect, useRef } from 'react';+import { hasWords, noEnglish, requestEnglish, textsOf } from '@/lib/community/translation';+import type { PublishDraft } from './usePublishDraft';++type Update = (patch: Partial<PublishDraft>) => void;++/**+ * Translates a draft that arrived from Desktop or a bundle into English once the author is signed in,+ * keeping the original to switch back to. Edits made while it translates win: the answer is dropped.+ */+export function useEnglishDraft(draft: PublishDraft, update: Update, signedIn: boolean | null) {+ const texts = textsOf(draft), key = JSON.stringify(texts);+ const latest = useRef(key), asking = useRef(false);+ const pending = draft.english.status === 'pending';++ useEffect(() => { latest.current = key; }, [key]);++ useEffect(() => {+ if (!pending || !signedIn || asking.current) return;+ const asked = JSON.parse(key) as typeof texts;+ if (!hasWords(asked)) { update({ english: noEnglish }); return; }+ asking.current = true;+ requestEnglish(asked).then(({ translated, ...english }) => {+ if (!translated || latest.current !== key) return update({ english: noEnglish });+ update({ ...textsOf(english), english: { status: 'translated', original: asked } });+ }, (error: unknown) => {+ update({ english: { status: 'failed', message: error instanceof Error ? error.message : 'The translation failed.' } });+ }).finally(() => { asking.current = false; });+ }, [pending, signedIn, key, update]);++ const english = draft.english;+ return {+ showOriginal: english.status === 'translated' ? () => update({ ...english.original, english: { status: 'original', english: texts } }) : undefined,+ useEnglish: english.status === 'original' ? () => update({ ...english.english, english: { status: 'translated', original: texts } }) : undefined,+ retry: english.status === 'failed' ? () => update({ english: { status: 'pending' } }) : undefined,+ };+}diff --git a/website/src/app/hub/publish/usePublishDraft.ts b/website/src/app/hub/publish/usePublishDraft.tsindex 39c834ab7..70d2dfc03 100644--- a/website/src/app/hub/publish/usePublishDraft.ts+++ b/website/src/app/hub/publish/usePublishDraft.ts@@ -2,12 +2,16 @@import { useCallback, useEffect, useRef, useState } from 'react';import { readDraft, saveDraft, type HubDraft } from '@/lib/community/drafts';import { communityCategories, communityEngines } from '@/lib/community/contract';+import { arrivedEnglish, noEnglish, type EnglishState } from '@/lib/community/translation';import type { HarnessSnapshot } from '@/lib/community/types';-/** `originalOutput` is the fork's output as it arrived: kept here to compare against, never published. */-export type PublishDraft = Omit<HarnessSnapshot, 'harnessName' | 'credits'> & { contextNote: string; originalOutput?: string };+/**+ * `originalOutput` is the fork's output as it arrived: kept here to compare against, never published.+ * `english` is where the draft stands with its translation, never published either.+ */+export type PublishDraft = Omit<HarnessSnapshot, 'harnessName' | 'credits'> & { contextNote: string; originalOutput?: string; english: EnglishState };-const fresh: PublishDraft = { title: '', description: '', category: 'Apps', engine: 'Codex', files: [], viewerPath: 'preview.html', conversation: [{ role: 'user', text: '' }], contextNote: '' };+const fresh: PublishDraft = { title: '', description: '', category: 'Apps', engine: 'Codex', files: [], viewerPath: 'preview.html', conversation: [{ role: 'user', text: '' }], contextNote: '', english: noEnglish };/** A handed-off or imported bundle, kept within what the form can show and the Hub accepts. */function fromBundle(bundle: HubDraft): PublishDraft {@@ -18,6 +22,7 @@ function fromBundle(bundle: HubDraft): PublishDraft {harnessId: bundle.harnessId, files: bundle.files || [], viewerPath: bundle.viewerPath || 'preview.html',conversation: bundle.conversation?.length ? bundle.conversation : fresh.conversation,forkedFrom: bundle.forkedFrom, cover: bundle.cover, contextNote: bundle.contextNote || '', originalOutput: bundle.originalOutput,+ english: arrivedEnglish(bundle.english),};}diff --git a/website/src/lib/community/client.spec.ts b/website/src/lib/community/client.spec.tsindex e22792b8b..63ddc16d5 100644--- a/website/src/lib/community/client.spec.ts+++ b/website/src/lib/community/client.spec.ts@@ -20,3 +20,15 @@ describe('existing workspace sign-in', () => {expect(fetcher).toHaveBeenCalledTimes(1);});});+describe('request bodies', () => {+ it('sends a body as gzip, so what people made never reaches the edge as raw HTML', async () => {+ const fetcher = vi.fn().mockResolvedValue(Response.json({ success: true, data: { id: 'x' } }));+ vi.stubGlobal('fetch', fetcher);+ const body = { files: [{ path: 'index.html', content: '<script>go()</script>' }] };+ await communityRequest('harnesses', { method: 'POST', body });+ const sent = fetcher.mock.calls[0][1];+ expect(sent.headers['Content-Type']).toBe('application/gzip');+ const unpacked = new Response(sent.body as Uint8Array<ArrayBuffer>).body!.pipeThrough(new DecompressionStream('gzip'));+ expect(JSON.parse(await new Response(unpacked).text())).toEqual(body);+ });+});diff --git a/website/src/lib/community/client.ts b/website/src/lib/community/client.tsindex 40e2afe4b..17e4973b5 100644--- a/website/src/lib/community/client.ts+++ b/website/src/lib/community/client.ts@@ -1,4 +1,5 @@import type { OpenHarness, SocialState } from './types';+import { gzipJson, gzipType } from './gzip';import { refreshHubSession } from './session';/** Read the existing Harness web session; the community does not mint a second identity. */@@ -18,8 +19,8 @@ export async function communityRequest<T>(path: string, options: { method?: striconst headers = sessionHeaders();const request: RequestInit = {method: options.method || 'GET', signal: options.signal,- headers: { ...headers, ...(options.body ? { 'Content-Type': 'application/json' } : {}) },- ...(options.body ? { body: JSON.stringify(options.body) } : {}), cache: 'no-store',+ headers: { ...headers, ...(options.body ? { 'Content-Type': gzipType } : {}) },+ ...(options.body ? { body: await gzipJson(options.body) } : {}), cache: 'no-store',};let response = await fetch(`/api/community/${path}`, request);if (response.status === 401 && headers.Authorization && await refreshHubSession(headers.Authorization)) {diff --git a/website/src/lib/community/drafts.ts b/website/src/lib/community/drafts.tsindex 1d427d0ce..f91174a7f 100644--- a/website/src/lib/community/drafts.ts+++ b/website/src/lib/community/drafts.ts@@ -1,8 +1,12 @@import { communityLimits } from './contract';+import type { EnglishState } from './translation';import type { HarnessSnapshot } from './types';-/** `originalOutput` is kept only by the review page, to tell a fork's new output from the one it arrived with. */-export type HubDraft = Partial<HarnessSnapshot> & { version?: number; clientId?: string; contextNote?: string; originalOutput?: string };+/**+ * `originalOutput` is kept only by the review page, to tell a fork's new output from the one it arrived with;+ * `english`, where the draft stands with its English translation.+ */+export type HubDraft = Partial<HarnessSnapshot> & { version?: number; clientId?: string; contextNote?: string; originalOutput?: string; english?: EnglishState };export const draftDatabase = 'harness.hub.drafts';export function validateDraft(value: unknown): HubDraft {diff --git a/website/src/lib/community/gzip.ts b/website/src/lib/community/gzip.tsnew file mode 100644index 000000000..e96a8adae--- /dev/null+++ b/website/src/lib/community/gzip.ts@@ -0,0 +1,13 @@+/**+ * A request body as `application/gzip` bytes of its JSON.+ *+ * Publications carry what people made (HTML, JavaScript, any text a session wrote), and Cloudflare's+ * managed rules in front of the Hub and its API refuse a body with raw `<script>` in it as an attack.+ * Gzipped, it reaches the backend, which unpacks and validates it as it would a raw one.+ */+export const gzipType = 'application/gzip';++export async function gzipJson(value: unknown): Promise<Uint8Array<ArrayBuffer>> {+ const stream = new Response(JSON.stringify(value)).body!.pipeThrough(new CompressionStream('gzip'));+ return new Uint8Array(await new Response(stream).arrayBuffer());+}diff --git a/website/src/lib/community/translation.ts b/website/src/lib/community/translation.tsnew file mode 100644index 000000000..8928ad115--- /dev/null+++ b/website/src/lib/community/translation.ts@@ -0,0 +1,36 @@+import { communityRequest } from './client';+import type { HarnessSnapshot } from './types';++/** The words of a publication, as the Hub translates them. */+export type DraftTexts = Pick<HarnessSnapshot, 'title' | 'description' | 'conversation'>;++/**+ * Where a draft stands with English. `pending` is how a draft arrives from Desktop or a bundle;+ * `translated` and `original` each keep the other version, so the author can switch back and forth.+ */+export type EnglishState =+ | { status: 'none' }+ | { status: 'pending' }+ | { status: 'failed'; message: string }+ | { status: 'translated'; original: DraftTexts }+ | { status: 'original'; english: DraftTexts };++export const noEnglish: EnglishState = { status: 'none' };+const statuses = new Set<string>(['none', 'pending', 'failed', 'translated', 'original']);++/** Only the words: a turn carries nothing else the Hub would refuse. */+export const textsOf = ({ title, description, conversation }: DraftTexts): DraftTexts =>+ ({ title, description, conversation: conversation.map(({ role, text }) => ({ role, text })) });++/** Words worth translating: tool output is never translated. */+export const hasWords = (texts: DraftTexts) =>+ !!(texts.title.trim() || texts.description.trim() || texts.conversation.some(turn => turn.role !== 'tool' && turn.text.trim()));++/** A saved draft keeps where it stood; one that just arrived has not been translated yet. */+export function arrivedEnglish(value: unknown): EnglishState {+ const status = (value as { status?: unknown } | null)?.status;+ return typeof status === 'string' && statuses.has(status) ? value as EnglishState : { status: 'pending' };+}++export const requestEnglish = (texts: DraftTexts) =>+ communityRequest<DraftTexts & { translated: boolean }>('translate', { method: 'POST', body: texts });