Skip to content

Keep files with a run

View as Markdown

This guide keeps a file in the runtime with the run it came from, then adds a version, finds it, downloads it and deletes it.

A file kept in the runtime can be a report a run's output became, a document your app sent in, or an export. Each one belongs to a project, and can name the run it came from. Every change to its bytes is kept as a version: Files and versions explains versions, pins and what keeps them. A file's name, type, size and sha256 are kept in the runtime's database. Its bytes are kept on disk, in a Google Cloud Storage bucket, or in an S3 bucket.

  • A runtime that keeps files. kindgi dev keeps them in the pack's .kindgi/dev/artifacts. Another runtime keeps them once you turn them on.
  • Kindgi 0.7's CLI, and a file to keep, such as report.pdf.
  • A run's id, to keep the file with that run.

kindgi files takes the place of kindgi artifacts, and takes the same ids. 0.8.0 removes kindgi artifacts. Until then, each of its commands works as it did and names the one to use instead, such as kindgi artifacts list is deprecated: use kindgi files list. Removed in 0.8.0. What 0.8.0 removes lists each.

Uploading the file keeps it with its run, as its version 1:

Terminal window
pnpm exec kindgi files upload report.pdf --run=<run-id> --tag=kind=summary
  • report.pdf is the file to upload.
  • --run=<run-id> is the run it came from, where <run-id> is the run's id.
  • --content-type gives its type. Without it, the type comes from the file's extension, here application/pdf, or else it's application/octet-stream.
  • --tag=key=value labels it. You can give it more than once.

It prints version 1, with the file's fileId and created: true.

  • Its project: with --run, it's the run's project. Without --run, it's --project's, or else the workspace's default project. A --project that isn't the run's is refused, and so is a run that doesn't exist, with 404 run-not-found. With an API key limited to a project, an upload that names no project goes to the key's project. One whose project, named or its run's, is another is refused with 403 key-project-mismatch.
  • Its name is the file's, unless --name gives another.
  • Its size: the runtime refuses a bigger upload than its cap, 100 MiB by default. It answers 413 file-too-large, with the cap in details.maxBytes.
  • The workspace's storage limit: on a runtime that sets one, an upload the limit has no room for is refused with 403 storage-limit-reached, and nothing is stored. details has the limit (storageMiB), what the workspace's files take (usedBytes) and what the upload needs (neededBytes). Deleting files makes room.

From your app, in TypeScript, files.create sends the file and answers its version 1. Over the cap, it answers 413 file-too-large. files.versions.download streams a version's bytes back, and files.versions.get reads what's kept without the bytes.

Uploading to the file adds a version, which becomes its Latest:

Terminal window
pnpm exec kindgi files upload report.pdf --to=<file-id> --expected-version=1
  • <file-id> is the file's fileId, from the upload.
  • --expected-version=1 names the version you built on. If another version was added since, nothing is written: it answers 409 file-version-conflict, and says Latest is version 2, not 1, so nothing was written.
  • Without --expected-version, the last upload wins, and the answer's parentVersion names the version it built on.
  • A version stays in its file's project, so --to takes no --run or --project.
  • If the bytes are Latest's already, nothing is added. It prints that version, with created: false, and says Nothing new: these bytes are already version 1.

kindgi files versions <file-id> lists every version, newest first. kindgi files restore <file-id> <version> adds a version with an earlier one's bytes, and kindgi files pin <file-id> <version> keeps one, so it can't be deleted. Files and versions explains each.

To show an agent the file, name its blobId in a run's input, as Give an agent its input shows.

A file your app sends in as a run's input is uploaded before the run starts, so it can't name the run. Mark it forRun, and the runtime deletes it if no run that names it in its input starts in time:

scripts/upload-for-run.ts
import { readFile } from 'node:fs/promises';
import { createClient } from '@kindgi/sdk/client';
const kindgi = createClient(); // KINDGI_API_URL, KINDGI_API_TOKEN, or the running kindgi dev
const file = await kindgi.artifacts.upload({
body: await readFile('contract.docx'),
name: 'contract.docx',
contentType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
forRun: true,
});
console.log(file.blobId);

The run's input names the file as a kindgi.file value whose artifactId is its blobId.

  • Its record has expiresAt, when it's deleted unless such a run has started by then: 24 hours after the upload by default.
  • Once a run that names it starts, expiresAt is gone, and the file stays until it's deleted.
  • forRun with ownerRunId is refused with 400 bad-input: an upload that names its run already belongs to it.
  • With the Python client, pass data={"forRun": "true"} to artifacts.upload. Over HTTP, it's the form field forRun=true.

Listing a run's files gives each one's fileId, which you read and download it by:

Terminal window
pnpm exec kindgi files list --run=<run-id> --table
pnpm exec kindgi files get <file-id>
pnpm exec kindgi files download <file-id> -o report.pdf
  • list is newest change first. --run, --project and --content-type narrow it, and --table prints a table instead of JSON.
  • get gives a file's name, project and run, Latest's number, and Latest's type, size and sha256, without the bytes. get <file-id> 2 gives version 2's.
  • download writes Latest, unless you name a version, as download <file-id> 2 does. It prints the number it wrote. A version that's gone answers 404 file-version-not-found, and nothing is written: it never writes Latest instead.
  • download writes the file under its own name in this directory, unless -o names one. It keeps a file that's already there, unless you add --force.
  • download first writes to a temporary file next to the one it saves, and moves it into place only once the whole file has arrived and its sha256 is the one the server sent. A download that fails leaves no file, and with --force the file already there stays as it was.
  • One Ctrl-C (or SIGTERM) stops download, whether it's still waiting for the server or part way through the file. As after a failure, it leaves no file, and a file already there stays as it was. It says kindgi files download stopped: no file was written. and exits with code 130.
  • Ended at once, by a second Ctrl-C or kill -9, download can leave that temporary file behind: the file's name, then 8 hex digits, then .partial, such as report.pdf.1a2b3c4d.partial. You can delete it.

A download is checked against the sha256 and size kept for its version. If its bytes were changed in their store outside the runtime, on disk or in the bucket, the download stops part way: the runtime closes the connection before the last of its Content-Length bytes. download then fails with the download stopped part way (…): report.pdf wasn't written, and writes no file. In TypeScript, reading the body files.versions.download answers throws. The runtime logs each one at ERROR, as Artifact <blob-id>'s stored bytes aren't the ones put; the read was stopped, where <blob-id> is the version's blobId, which kindgi files get <file-id> <version> shows. The line comes with the workspace, where the bytes are kept, the sha256 and size kept for them, and what was read. It never holds the file's content.

With authorization on, reading a file needs read on its project, and changing it needs write. Who can see and change a file has each case. A file you can't read answers 404 file-not-found, as if it weren't there, and lists leave it out.

An artifacts.upload by someone with run and no write is always forRun. The runtime limits what one person uploads that way in a workspace, by default to 1 GiB and 1,000 files in 24 hours (KINDGI_RUN_UPLOAD_DAILY_BYTES, KINDGI_RUN_UPLOAD_DAILY_FILES). Their uploads to projects where they have write are never refused, but they count toward it too. Past it, the upload answers 429 upload-limit-reached, and Retry-After says when it fits.

Terminal window
pnpm exec kindgi files delete <file-id> --yes

<file-id> is the file's fileId. --yes skips the question delete asks first, which names the file and how many versions go. With no terminal and no --yes, it deletes nothing and exits with code 1.

The file and every version are gone from lists and downloads at once. A file with a pinned version stays: it answers 409 file-version-pinned, naming its lowest pinned version. delete <file-id> <version> deletes one version: a pinned one answers 409 file-version-pinned, and the file's only live version answers 409 file-last-version, so you delete the file instead.

A deleted file or version stays until a retention policy on file_version purges it, as Keep and purge deleted data describes.

The runtime keeps files once KINDGI_ARTIFACTS says where their bytes go:

  • local:<absolute directory>: a directory on the runtime's machine.
  • gcs:<bucket>[/<prefix>]: a Google Cloud Storage bucket, reached with the runtime's own Google identity, its Application Default Credentials. So there's no key to store.
  • s3:<bucket>[/<prefix>] (runtime 0.4.0 or later): an S3 bucket, on AWS with the runtime's own AWS identity, or in an S3-compatible store such as MinIO or Cloudflare R2.

Without it, the runtime serves no files. kindgi dev keeps them in the pack's .kindgi/dev/artifacts. KINDGI_ARTIFACT_MAX_BYTES sets the upload cap, and KINDGI_UNUSED_UPLOAD_TTL_MS how long a forRun upload is kept when no run that names it starts, in milliseconds (24 hours by default). The startup output says where files go, or that they're off. 0.8.0 renames KINDGI_ARTIFACTS and KINDGI_ARTIFACT_MAX_BYTES, as What 0.8.0 removes lists. Choose where the runtime keeps files has each one's settings and permissions, and what the runtime checks at startup.