# Add an Arena54 audit log to this project

Goal: Record this game’s staff actions in a log that shows if an entry was changed.

You are a coding agent working inside my project. Do the work. Do not ask me anything you can find out by reading the project. Do not invent an endpoint, a field or an error code that is not written below.

## 1. Look at the project first

- Find the framework and language, how the project is run and tested, and every place a member of staff or an admin tool changes something about a player or the game: bans, refunds, grants, edits.
- Find how the project reads configuration and environment variables, and which files git ignores.
- Any backend or script will do. Use the HTTP client and the router the project already has, or the runtime’s own if it has none. If the project has nowhere to run code off the page, stop and tell me.
- If the project has no environment example file, add one, and load the environment the way the runtime provides.
- If the project already has an Arena54 client or key variable, reuse it.
- Tell me in two or three lines what you found and what you will change, then carry on.

## 2. The contract

Base URL: `https://developers.arena54.com/api/v1` (called $BASE below).

Every answer is JSON in one envelope:

```json
{ "success": true, "data": …, "meta": { "request_id": "req_…", "mode": "test" } }
{ "success": false, "error": { "code": "…", "message": "…" }, "meta": { "request_id": "req_…", "mode": "test" } }
```

Branch on `error.code`. Log `meta.request_id` with any failure.

Keys and headers:

- Use the game’s SECRET key (it starts `a54_test_` or `a54_live_`, with no `pub_`). Read it from the environment variable `ARENA54_SECRET_KEY`, on the server only. Never send it to a page and never commit it. A secret key that arrives from a browser is refused.
- Send `Authorization: Bearer <secret key>`. A body is JSON; a `Content-Type` header is optional.
- A test key writes to test data of its own that never touches live data. Build with the test key.
- A 429 carries `Retry-After` in seconds.

Calls:

- POST /audit: Append an entry: actor, action, target, reason, details.
    data: { "id": 69 }   (body: { "actor", "action": "player.ban", "target"?, "reason"?, "details"? })
- GET /audit: Entries, newest first. Filter with action, actor, target.
    data: [{ "id": 69, "at": "…", "actor": "mod-ada", "action": "player.ban", "target": "pl_…", "reason": "…", "details": {}, "hash": "…" }]
- GET /audit/verify: Whether the chain is whole, and the first entry that is not.
    data: { "whole": true, "first_bad_id": null }

How it works:

- An entry is who did it (`actor`, needed, 80 characters at most), what (`action`, needed), to whom or what (`target`), why (`reason`) and any `details` (an object, 4,000 characters at most). Arena54 adds the time.
- An `action` is 1 to 60 lowercase letters, digits, dots, dashes or underscores, starting with a letter or a digit, like `player.ban`. Anything else answers 400.
- Each entry stores a hash of the one before it, so a changed or missing entry shows at GET /audit/verify.
- GET /audit lists newest first. Filter with `action`, `actor` or `target`; page with `before` = the last id you saw, and `limit` up to 100.
- There is no edit and no delete.

Refusals:

- 401 missing_key: No Authorization header, or what follows “Bearer” is not shaped like a game’s key.
- 401 invalid_key: The key is shaped like one but is unknown or revoked.
- 503 module_off: Arena54 has the module switched off.
- 403 module_not_enabled: The module is not ticked for this game in the dashboard.
- 403 secret_key_required: A publishable key asked for something only a secret key may do.
- 403 secret_key_in_browser: A secret key arrived from a page.
- 400 bad_request: A body that is not the shape asked for.
- 429 limit: The plan’s allowance for the day is used up. Nothing was written.
- 429 rate_limited: Too many calls this minute. Retry-After says how long to wait.

## 3. Do this, in order

1. Add ARENA54_SECRET_KEY to the project’s environment example file with an empty value. Read it only in server code.
2. Write one server function `audit(actor, action, target, reason, details)` that calls POST /audit. If the call fails, log the failure and let the staff action finish: do not lose the action because the log was down. Say in your summary that you did this.
3. Call it at each staff action you found. Use a small fixed set of action names and list them for me. Take `actor` from the project’s own signed-in staff identity; if staff routes have none, require an actor in the request and tell me it is unverified. If an action turns out to be a repeat of one already done, do not write a second entry.
4. Add a page or a command for staff that shows recent entries with GET /audit, and a check that calls GET /audit/verify.

## 4. Prove it works

Set `BASE` and `ARENA54_SECRET_KEY` (my TEST secret key) in the shell. If I have not given you a key, ask me for a TEST secret key once, and for nothing else. `BASE=https://developers.arena54.com/api/v1`.

- Run: `curl -X POST "$BASE/audit" -H "Authorization: Bearer $ARENA54_SECRET_KEY" -d '{"actor":"prove","action":"prove.check","reason":"testing"}'`. Expect HTTP 200 and an `id`.
- Run: `curl "$BASE/audit?action=prove.check" -H "Authorization: Bearer $ARENA54_SECRET_KEY"`. Expect the entry.
- Run: `curl "$BASE/audit/verify" -H "Authorization: Bearer $ARENA54_SECRET_KEY"`. Expect `"whole": true`.
- Send an action with a space in it. Expect HTTP 400 `bad_request`.

Show me the output of each check.

## 5. Rules you must not break

- The secret key never reaches a page, a game build or a commit.
- Do not put a password, a token, an email or a phone number in an entry: it can never be removed.
- Write the entry for what was done, by the person who did it. Never write entries on behalf of a player.

## 6. Done when

- Each staff action writes one entry.
- Staff can read recent entries and run the verify check.
- The curl checks give the answers above.

Then give me a short list of the files you changed and anything I must do by hand.

For a person, not needed for this task: https://developers.arena54.com/docs · This prompt: https://developers.arena54.com/prompts/audit.server.md
