Skip to content

Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.

5.4 KB
Raw Blame
{{- if and (eq .Values.computers.mode "sandbox") .Values.computers.sandbox.culler.enabled }}
{{- $component := "culler" -}}
{{/*
Suspending computers nobody is using.

A CronJob rather than a timer in the API, and the difference is the whole reason this file exists.
An interval in the server fires in every replica, so five replicas would each decide independently
to suspend the same computer. The audit-retention sweep gets away with that because deleting old
rows twice is the same as deleting them once; suspending a browser somebody just started using is
not. The work is claimed and leased out of PostgreSQL, so whichever pod runs this takes what nobody
else holds, and a pod that dies mid-suspend hands its work back when the lease expires.

`concurrencyPolicy: Forbid` on top, because a schedule that overlaps itself is the same problem in
one workload rather than across several.
*/}}
apiVersion: batch/v1
kind: CronJob
metadata:
  {{- /* Fifty-two characters, not sixty-three; see `openbot.cronJobName`. */}}
  name: {{ include "openbot.cronJobName" (dict "root" . "component" $component) }}
  labels:
{{ include "openbot.componentLabels" (dict "root" . "component" $component) | indent 4 }}
spec:
  schedule: {{ .Values.computers.sandbox.culler.schedule | quote }}
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 1
  failedJobsHistoryLimit: 3
  startingDeadlineSeconds: 120
  jobTemplate:
    spec:
      backoffLimit: 1
      {{- /*
        A CEILING ON ONE SWEEP, because `Forbid` above turns a hung one into a permanent stop.
        
        Without this a sweep that never returns — a wedged API-server call, a database connection
        that hangs rather than refuses — holds the concurrency lock for ever. Kubernetes will not
        start the next one, so culling simply ceases: no error, no restart, no alert, and the first
        sign is a cloud bill for a fleet of browsers nobody has used in a fortnight. That is exactly
        the failure this feature exists to prevent, arriving through the mechanism meant to prevent
        overlapping runs.
        
        Comfortably longer than a real sweep, which claims twenty computers and suspends them.

        Defaulted through `kindIs "invalid"` rather than `| default`, because sprig substitutes on
        EMPTY and zero is empty — the same trap that silently defeated the handoff off-switch one
        file over. Here it is defaulted because this key is newer than the culler around it. Under
        `helm upgrade --reuse-values` an existing release carries `culler` without it, so the
        template still renders and emits an empty scalar: null, which Kubernetes reads as unset. The
        ceiling described above would then silently not exist, on exactly the deployments that have
        been running long enough to have a wedged sweep.
      */}}
      {{- $deadline := 600 -}}
      {{- if not (kindIs "invalid" .Values.computers.sandbox.culler.activeDeadlineSeconds) -}}
      {{- $deadline = .Values.computers.sandbox.culler.activeDeadlineSeconds -}}
      {{- end }}
      activeDeadlineSeconds: {{ $deadline }}
      template:
        metadata:
          labels:
{{ include "openbot.componentSelectorLabels" (dict "root" . "component" $component) | indent 12 }}
            {{- if .Values.postgresql.enabled }}
            {{- /*
              The bundled database admits pods carrying this label and nothing else, which this chart pins on.
              Anything that opens the database needs it, and only the API server had it: this would have been
              refused on any cluster that actually enforces a NetworkPolicy. Not caught by hand, because the
              cluster it was driven on ships enforcement switched off.
            */}}
            {{ .Release.Name }}-postgresql-client: "true"
            {{- end }}
        spec:
          restartPolicy: Never
          serviceAccountName: {{ include "openbot.serviceAccountName" . }}
          {{- /* It reads the cluster's Sandboxes, so it needs the token the API pod does not. */}}
          automountServiceAccountToken: true
          {{- with .Values.imagePullSecrets }}
          imagePullSecrets:
{{ toYaml . | indent 12 }}
          {{- end }}
          containers:
            - name: culler
              image: {{ include "openbot.image" . }}
              imagePullPolicy: {{ .Values.image.pullPolicy }}
              workingDir: /app/server
              command: ["/usr/local/bin/bun", "scripts/cull-idle-computers.ts"]
              env:
{{ include "openbot.databaseUrlEnv" . | indent 16 }}
{{ include "openbot.commonEnv" . | indent 16 }}
              {{- /* The same shape of computer the server uses, so the two cannot disagree. */}}
              volumeMounts:
                - name: sandbox-template
                  mountPath: /etc/openbot
                  readOnly: true
                {{- with (include "openbot.extraVolumeMounts" . | trim) }}
                {{- . | nindent 16 }}
                {{- end }}
              resources:
                requests:
                  cpu: 50m
                  memory: 128Mi
                limits:
                  memory: 512Mi
          volumes:
            - name: sandbox-template
              configMap:
                name: {{ include "openbot.componentName" (dict "root" . "component" "computer-template") }}
            {{- with (include "openbot.extraVolumes" . | trim) }}
            {{- . | nindent 12 }}
            {{- end }}
{{- end }}