PsyNet Deployment Ops
*********************

Note:

  This is an Agent Skill: a workflow written mainly for coding agents,
  and also useful as a checklist for people. PsyNet installs it into
  experiments at ".cursor/skills/psynet/deploy-experiment/SKILL.md";
  in Cursor, run "/deploy-experiment". Edit it in the PsyNet
  repository at ".cursor/skills/experiment/deploy-experiment/".


Read first
==========

Read these pages before acting. The "Documentation" section of the
experiment's "AGENTS.md" explains how to find and search them.

* How deployment works — what gets deployed and how "deploy.toml"
  selects files

* Setting up a server — provisioning and registering servers

* Running a study — launching, monitoring, and closing a study

* Exporting data — exporting data before teardown


Safety rules
============

* Do not run paid recruitment, app destruction, EC2 teardown, or other
  irreversible commands unless the user explicitly asks.

* Before any destroy/teardown recommendation, verify whether data has
  been exported and record the export path or blocker.

* Export and destroy only once no async jobs are pending. "psynet
  export ssh" and "psynet export local" end with a warning that the
  export misses pending jobs; "psynet destroy ssh" refuses when it
  cannot ask. Wait for the jobs, then export again; never add "--
  discard-pending-jobs" unless the user accepts losing those results.
  If destroy prints "Skipped the check for pending async jobs", it
  destroys the app without the check, so confirm the export first.

* Do not commit or publish real AWS credentials, recruiter tokens,
  dashboard passwords, ".env" files, private keys, or participant
  data.

* Prefer command generation and readiness reports over live operations
  unless the task explicitly asks for execution.


Readiness audit
===============

Check that the experiment folder has:

* "experiment.py"

* "config.txt"

* "requirements.txt"

* "constraints.txt"

* "Dockerfile"

* "deploy.toml" with reviewed "[exclude]" rules. Run "dallinger
  deployment-files list" to inspect every file that PsyNet will copy.
  Git-ignored files are deployed unless "deploy.toml" excludes them.
  Confirm "audit/" is excluded. Stock templates include it; existing
  "deploy.toml" files are not overwritten, so add "audit" to
  "[exclude].paths" if it is missing.

* ".gitignore" excluding ".venv/", ".deploy/", ".pytest_cache/",
  "exports/", "deploy_logs/", source archives, generated logs, and the
  managed ".cursor/skills/psynet/" bundle

* recruiter/qualification JSON files when a recruiter requires them

* qualification files and recruiter settings that match any in-
  experiment prescreeners; use "filter-participants/SKILL.md" when the
  mapping is unclear

* local "static/" stimulus files present and deployable when the
  experiment plays pregenerated media

* no broken symlinks to local user paths

* no stale app/server/study names copied from another template

* a "DEPLOYMENT_LOG.md" or clear deployment metadata


Dependency checks
=================

* Install from "constraints.txt", not directly from
  "requirements.txt".

* Prefer "psynet setup" to regenerate constraints and refresh the
  environment when dependencies intentionally change or
  "constraints.txt" is missing.

* If local and deployed behavior differ, check for stale constraints,
  Docker context problems, missing assets, and hardcoded local paths.


Deployment records
==================

"DEPLOYMENT_LOG.md" is not a core PsyNet file. It is an emerging lab
practice for making deployments recoverable by humans and agents. If a
folder uses another deployment record, inspect that instead. If no
record exists, recommend creating one.

A useful deployment record should include:

* folder path

* app name

* server name

* DNS host

* region

* instance type

* recruiter

* qualification/config files

* provision command

* deploy command

* dashboard/log URLs

* export command and path

* destroy app command

* EC2 teardown command

* final verification command


Command patterns
================

Local debug:

   source .venv/bin/activate
   psynet debug local

Provision (choose "worker_processes" with "plan-server-
resources/SKILL.md"):

   dallinger ec2 provision \
     --name <server-name> \
     --region <region> \
     --dns-host <server-name>.cap-experiments.com \
     --type m7i.xlarge

Before provisioning or registering a server, check (without printing
values) that "~/.dallingerconfig" sets a real
"contact_email_on_error". Server commands ignore the experiment's
"config.txt" and reject its template placeholder "XXX@gmail.com"; ask
the user for the address rather than inventing one. "dallinger ec2
provision" also needs "dashboard_password", and "server_pem" holding
the same unencrypted private key as "~/.ssh/<ec2_default_pem>.pem".

Provisioning also starts the shared Caddy, Postgres and Dozzle
services and prints the Dozzle password once; record that it exists,
not its value.

Deploy:

   psynet deploy ssh \
     --app <app-name> \
     --dns-host <server-name>.cap-experiments.com \
     --server <server-name>.cap-experiments.com

Export:

   psynet export ssh \
     --app <app-name> \
     --server <server-name>.cap-experiments.com \
     --path <experiment-folder>/exports

Destroy and teardown:

   psynet destroy ssh --app <app-name> --server <server-name>.cap-experiments.com
   dallinger ec2 teardown --name <server-name> --region <region>

Teardown finds the DNS name only in the server record that "ec2
provision" saved on this computer. On another computer, after a
provision finished with the printed "servers add" commands, or for an
instance provisioned by an older Dallinger, add the "--dns-host" it
was provisioned with, or its DNS records stay behind.


Common failure modes
====================

* "psynet debug local" fails because port 5000 is already occupied.

* "psynet debug local" reports an oversized deployment plan; inspect
  it with "dallinger deployment-files list", then add local paths,
  names, or suffixes to "[exclude]" in "deploy.toml".

* "dallinger ec2 list instances --all" is slow or times out; prefer
  explicit "--region" scans.

* AWS commands fail with missing credentials; do not invent
  credentials.

* Deployed Docker environment lacks local files, broken symlink
  targets, or external assets.

* App/server/DNS names diverge between logs, commands, and config
  files.

* A classic deploy stops before the build with "is not set up for this
  classic-ingress deploy" or "An older Dallinger version has rewritten
  the shared files". Run the command the message names. If it suggests
  saving the DNS name the server already serves ("servers add --host
  <ip> --dns-host <name>"), try that first; it restarts nothing.
  "dallinger docker-ssh servers upgrade --host <server>" briefly
  restarts the shared services, so on a server with other experiments,
  ask the user first. On a server set up by Dallinger 13.0 or earlier,
  it also moves the shared database to PostgreSQL 18 and stops every
  classic app during the copy: offer to export the apps the user still
  needs first, and remind them that no one may deploy to that server
  with PsyNet 13 or earlier, or with PsyNet 14.0.0rc1 to rc3,
  afterwards.

* "dallinger ec2 provision" fails after launching the instance: it
  keeps running and costs money. Fix the printed problem, then run
  either the printed "servers add" commands that finish the setup (and
  "servers upgrade" if "servers add" says it left the shared files
  unchanged) or the printed "ec2 teardown"; teardown is irreversible,
  so ask the user first.

* A deploy stops with "No Let's Encrypt contact email": save one with
  "dallinger docker-ssh servers add --host <server> --contact-email
  <email>" (restarts nothing; other saved settings are kept), using an
  address the user gives.

* A deploy fails with "PermissionError: [Errno 13] Permission denied"
  on "dallinger/docker-compose.yml": the installed Dallinger predates
  "servers upgrade", and the server's shared files are read-only.
  Update the PsyNet pin; never "chmod" the shared files.

* Export exists for one app but teardown is being planned for another.

* SSL/TLS errors during first launch can be transient cold-start
  issues; retry after a short wait before changing code.

* Relative export paths are easy to lose track of; prefer absolute "--
  path" values in export commands and logs.

* Large asset exports may fail even when anonymized tabular exports
  are usable; record exactly which export mode succeeded or failed.
