Repository: UKGovernmentBEIS/inspect_ai
Issue: Unable to expose `inspect view` with ngrok due to random resources raising status 400 and 503
Root cause: Documentation Deficiency

I'm running inspect log viewer locally with `inspect view` where the viewer works well on `localhost:7575` but I see strange failures when I export it with ngrok (`ngrok http 7575`)

```
GET https://<redacted>-6575-bc6b-521e-d227.ngrok-free.app/src/navbar/Navbar.mjs 400 (Bad Request)
WorkSpace.mjs:19         
GET https://<redacted>-6575-bc6b-521e-d227.ngrok-free.app/src/samples/tools/SortFilter.mjs net::ERR_ABORTED 400 (Bad Request)
WorkSpace.mjs:25 
GET https://<redacted>-6575-bc6b-521e-d227.ngrok-free.app/src/workspace/TaskErrorPanel.mjs net::ERR_ABORTED 400 (Bad Request)
WorkSpace.mjs:18    
GET https://<redacted>-6575-bc6b-521e-d227.ngrok-free.app/src/samples/SamplesTools.mjs net::ERR_ABORTED 400 (Bad Request)
```

I'm able to open each of these URLs individually. If I refresh the page with the cache disabled, a different set of resources fail to load,

```
GET https://<redacted>-6575-bc6b-521e-d227.ngrok-free.app/src/components/ErrorPanel.mjs net::ERR_ABORTED 503 (Service Unavailable)
App.mjs:13 
GET https://<redacted>-6575-bc6b-521e-d227.ngrok-free.app/src/components/AppErrorBoundary.mjs net::ERR_ABORTED 503 (Service Unavailable)
```

Another refresh,
```
GET https://<redacted>-6575-bc6b-521e-d227.ngrok-free.app/libs/bootstrap/css/bootstrap.min.css net::ERR_ABORTED 503 (Service Unavailable)
GET https://<redacted>-6575-bc6b-521e-d227.ngrok-free.app/libs/showdown.min.js net::ERR_ABORTED 400 (Bad Request)
SamplesTools.mjs:3 
GET https://<redacted>-6575-bc6b-521e-d227.ngrok-free.app/src/samples/tools/EpochFilter.mjs net::ERR_ABORTED 400 (Bad Request)
```

I'm trying to share the Inspect Log Viewer with my team. [Inspect's documentation mentions](https://inspect.ai-safety-institute.org.uk/log-viewer.html#viewer-embedding) copying the directory www into the directory that has to be published. I tried that too but see the errors in the HTTP Server,
```
::1 - - [29/Jul/2024 21:55:35] "GET /src/samples/SampleScores.mjs HTTP/1.1" 200 -
::1 - - [29/Jul/2024 21:55:35] code 404, message File not found
::1 - - [29/Jul/2024 21:55:35] "GET /api/logs HTTP/1.1" 404 -
::1 - - [29/Jul/2024 21:55:35] "GET /favicon.svg HTTP/1.1" 200 -
::1 - - [29/Jul/2024 21:55:36] code 404, message File not found
::1 - - [29/Jul/2024 21:55:36] "GET /api/events?loaded_time=1722282935101&last_eval_time=0 HTTP/1.1" 404 -
::1 - - [29/Jul/2024 21:55:37] code 404, message File not found
::1 - - [29/Jul/2024 21:55:37] "GET /api/events?loaded_time=1722282935101&last_eval_time=0 HTTP/1.1" 404 -
```

@dragonstyle I think we need to not call the other api methods when we are in single file backend mode. Can we make a change to do this for the upcoming release?

@vcidst That should fix the standalone scenario.

As far as `ngrok` I can't really speak to what would be going on there.

@vcidst Could you share the Inspect version that you're using and also the urls that you are using? It seems like we are not properly detecting that we should be displaying in a single file mode, so I'm wondering whether you:

- Are using a version before we introduced this feature
- Maybe aren't forming the urls the way we expect (we use the presence of the `log_file` query parameter to use HTTP fetching to retrieve the log vs coordinating with Inspect directly)

> @dragonstyle I think we need to not call the other api methods when we are in single file backend mode. Can we make a change to do this for the upcoming release?

The single file API already implements all the methods and no-ops the unneeded ones such as `client_events`. 

@dragonstyle I'm running inspect_ai==0.3.18 with Python 3.10.14. What do you mean by the URLs I'm using? 

For the errors from HTTP Server, I ran a server with `python -m http.server` in the directory www within inspect_ai.
For Ngrok, I ran the server with `inspect view` and then ran the tunnel with `ngrok http 7575`. Ngrok generates an ephemeral URL to expose local port 7575

In order for the viewer to work when hosted, you need to link directly to the log  files (it can't enumerate log files using simple HTTP), so it will only work for specific log files, using a url like:

`https://<deployment url>/?log_file=logs/2024-07-18T09-00-04-04-00_bash_QuRMsu7hRUWrJjxe8xePLa.json`

(either as a link, or as an iframe url, for example)

But in addition, we introduced the view hosting in v0.3.19 (currently not yet released to pypi). You can install this version directly from the repo using:

`pip install git+https://github.com/ukgovernmentbeis/inspect_ai`

I just updated the package and did two tests,

I ran the ngrok tunnel again to try the URL with the log file,
https://3001-2001-16b8-cb81-d800-3936-97c6-62cd-f27a.ngrok-free.app/?log_file=logs/2024-07-29T17-15-13+02-00_llm-judge_7mKaqQFWSv9Gn4nwesA4ok.json

I see similar errors on ngrok. Looking into nrgok's dashboard, I see these additional headers in one of the failed requests,
```
X-Ngrok-Rehydrate-Enriched-Error-Arg: addr:localhost:7575
X-Ngrok-Rehydrate-Enriched-Error-Arg: errorText:dial tcp 127.0.0.1:7575: connect: connection reset by peer
X-Ngrok-Rehydrate-Enriched-Error-Code: 8012
X-Ngrok-Rehydrate-Enriched-Error-Proto: message: "Traffic was successfully tunneled to the ngrok agent, but the agent failed to establish a connection to the upstream web service at localhost:7575." legacy_number_code: 8012 grpc_status_code: 3 http_status_code: 400 stack: <   function: "0x0" > code: "8012"
```

I also ran the HTTP Server on port 8000, open one of the logs with `http://localhost:8000/?log_file=logs/2024-07-29T17-15-13+02-00_llm-judge_7mKaqQFWSv9Gn4nwesA4ok.json` but see a 404 error,

```
Error: Error: 404: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>Error response</title> </head> <body> <h1>Error response</h1> <p>Error code: 404</p> <p>Message: File not found.</p> <p>Error code explanation: HTTPStatus.NOT_FOUND - Nothing matches the given URI.</p> </body> </html> )
at Error: Error: 404: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
        <title>Error response</title>
    </head>
    <body>
        <h1>Error response</h1>
        <p>Error code: 404</p>
        <p>Message: File not found.</p>
        <p>Error code explanation: HTTPStatus.NOT_FOUND - Nothing matches the given URI.</p>
    </body>
</html>
)
    at getContents (http://localhost:8000/src/api/api-http.mjs:53:23)
    at async Object.eval_logs (http://localhost:8000/src/api/api-http.mjs:66:24)
    at async http://localhost:8000/App.mjs:137:22
```

By the way earlier today, I installed inspect_ai on replit, copied logs, ran `inspect view` and deployed it. That works completely fine and don't see any errors over there


Possibly a digression, I tried to run `inspect view --debug` but it seems to be either stuck or taking way too long (waited ~5 minutes)

```
❯ inspect view --debug
Waiting for debugger attach
```

So I'm running `inspect view` instead

I would focus on the local example first until you get that working. My guess is that there is something wrong with the `log_file` url. For example, the `+` needs to be url encoded, so I think the correct url is:

`http://localhost:8000/?log_file=logs/2024-07-29T17-15-13%2B%0A02-00_llm-judge_7mKaqQFWSv9Gn4nwesA4ok.json`

Once the local version is working, perhaps give ngrok another try, though I don't really know too much about that :(.


> Possibly a digression, I tried to run `inspect view --debug` but it seems to be either stuck or taking way too long (waited ~5 minutes)

This is because the `--debug` flag actually tells the viewer to wait for a debugger to attach (until a debugger attaches it just waits)

@dragonstyle You're right, I just copied the file path and missed url encoding! The local server works perfectly, thank you 🙏🏽 

For the record, ngrok's URL didn't work even with the encoded log file name. But it looks like that's something unique on ngrok's end and not relevant for inspect_ai

Thanks for the timely / persistent followup and glad at the least the basics are operational!