↓Skip to main content

Week Notes 2026.04

Solving a Logic Puzzle with miniKanren #

Someone I’m following on Mastodon shared a link to a blog post Solving a “Layton Puzzle” with Prolog that drew my interest. In this post, the author describes a Prolog program to solve the following puzzle:

We have a test with 10 true/false questions (denoted a/b) and four student attempts. Given the scores of the first three students, what is the score of the fourth student?

bbababbabb = 7
baaababaaa = 5
baaabbbaba = 3
bbaaabbaaa = ???

Back in the depths of time, I gave a workshop on core.logic to the Cambridge Clojure user group (core.logic is an implementation of miniKanren in Clojure). Could I remember enough to write a solution in Guile, using the Guile implementation of miniKanren?

Just about… The key thing is to implement a goal that, given the answer key, will compute the score for a given attempt. In this function, both xs and ys must be lists of as and bs of the same length, and s is the corresponding score:

(use-modules (minikanren))

(define (scoreo xs ys s)
   (conde
    ((nullo xs) (nullo ys) (== s '()))
    ((fresh (x rest-xs y rest-ys)
            (conso x rest-xs xs)
            (conso y rest-ys ys)
            (conde
             ((== x 'a) (== y 'a) (fresh (t) (scoreo rest-xs rest-ys t) (+o t '(1) s)))
             ((== x 'a) (== y 'b) (scoreo rest-xs rest-ys s))
             ((== x 'b) (== y 'b) (fresh (t) (scoreo rest-xs rest-ys t) (+o t '(1) s)))
             ((== x 'b) (== y 'a) (scoreo rest-xs rest-ys s)))))))

This implementation of miniKanren does not support negation, so I had to spell out all four cases for x and y. You run it like this:

(run* (q) (scoreo '(a a a a a a a a a a) '(b b a b a b b a b b) q))

This will bind q to the score, and return a list of all possible values of q, which in this case has exactly one element: (1 1). Numbers here are represented as a list of binary digits with the least significant digit first, so this corresponds to the number 3 in decimal. (The Guile miniKanren library provides a build-num function to return the binary representation of an integer.)

Now, the interesting thing about logic programming is that the “unknown” can appear anywhere, so we can also ask for all possible answers that would score 3:

(run* (q) (scoreo '(a a a a a a a a a a) q (build-num 3)))

This returns a list of 120 items. For those who remember their combinatorics, there are \(\binom{10}{3}\) ways of choosing the three positions that contain a correct answer, given by the formula:

\[ \binom{n}{r} = \frac{n!}{r!(n-r)!} \]

Plugging in \(n=10, r=3\) gives us the answer \(120\).

Back to the puzzle: the solution is straightforward using scroreo — we just tell it the information we know and ask for the answer!

(define (solve)
  (run* (q)
        (fresh (x)
               (scoreo x '(b b a b a b b a b b) (build-num 7))
               (scoreo x '(b a a a b a b a a a) (build-num 5))
               (scoreo x '(b a a a b b b a b a) (build-num 3))
               (scoreo x '(b b a a a b b a a a) q))))

This gives us:

((0 1 1) (0 1 1) (0 1 1) (0 1 1))

So the score for the fourth candidate is 6, but there are four different answer keys that might lead to the same solution. If we introduce another fresh variable we can get miniKanren to tell us the answer key as well as the score:

(define (solve-with-key)
  (run* (q)
        (fresh (x s)
               (scoreo x '(b b a b a b b a b b) (build-num 7))
               (scoreo x '(b a a a b a b a a a) (build-num 5))
               (scoreo x '(b a a a b b b a b a) (build-num 3))
               (scoreo x '(b b a a a b b a a a) s)
               (== q (list x s)))))

This gives us:

(((a b a b a a b a a b) (0 1 1))
 ((b b a b a a a a a b) (0 1 1))
 ((b b a b a a b b a b) (0 1 1))
 ((b b b b a a b a a b) (0 1 1)))

If you are interested in learning more about this style of programming, grab yourself a copy of The Reasoned Schemer and check out the miniKanren website for a list of other resources.

The Hidden Cost of Incomplete Multipart Uploads #

Last week I was puzzling over some S3 bucket metrics and thought maybe it was incomplete multipart uploads contributing to the unexpected size of a bucket. I configured a lifecycle rule to delete incomplete multipart uploads after one day. This has now kicked in and the bucket metrics show that this was, indeed, the cause:

S3 Bucket Metrics

Bitbucket Blues #

Bitbucket welcomed me today with a 404 error for every page I tried to visit. I cleared browser state and logged in again (via my work Google account) with the same effect. This is unexpected as I am workspace admin (or at least, I was last week).

Raised a support ticket with Bitbucket, they reply suggesting I try a different browser. Login with Chrome, everything works, but their site is still non-functional with Firefox.

Addendum: upgraded Firefox to the latest version from Flathub, 147.0.1, and I can now access Bitbucket with Firefox.

Switching from Terraform to OpenTofu #

Back in 2023 when Hashicorp did their rug-pull and switched Terraform to a non open-source license, I froze the container we are using for our deployments at terraform v1.5. This ensured that we didn’t inadvertently come to depend on proprietary features, keeping our options open to switch to an open-source fork in the future.

Now I need to add Butane to the container so I can add a pipeline step to generate an ignition file for the CoreOS EC2 instance deployment. Seems like a good time to switch to OpenTofu, which I am already using elsewhere with GitLab CI/CD.

This turned out to be easier than I thought: both these packages are available in Fedora, so the Dockerfile is a simple as:

FROM docker.io/library/fedora:43

RUN dnf -y install awscli2 jq git git-lfs opentofu butane

Generating an Ignition File with Terraform #

Now that I have a container image with both OpenTofu and Butane, I can remove the manually-generated ignition file from the repository and generate it on the fly with Terraform. This enables the following flow in the terraform:

  1. Create an S3 bucket for the ignition files.
  2. Interpolate the S3 bucket name into the Butane file (so we can download additional files from S3 during provisioning).
  3. Generate the ignition file from the Butane file.
  4. Upload the ignition file to the S3 bucket.

The terraform for this looks like:

module "ignition_files" {
  source  = "terraform-aws-modules/s3-bucket/aws"
  version = "5.9.0"

  bucket_prefix                         = "ignition-"
  block_public_acls                     = true
  block_public_policy                   = true
  ignore_public_acls                    = true
  restrict_public_buckets               = true
  attach_deny_insecure_transport_policy = true

  server_side_encryption_configuration = {
    rule = {
      apply_server_side_encryption_by_default = {
        sse_algorithm = "AES256"
      }
    }
  }

  tags = {
    "description" = "Configuration for Fedora CoreOS systems"
  }
}

resource "local_file" "butane_file" {
  filename = "${path.module}/ignition/bootstrap.bu"
  content = templatefile("${path.module}/ignition/bootstrap.tmpl", {
    ignition_files_bucket = module.ignition_files.s3_bucket_id
  })
}

resource "terraform_data" "ignition_file" {
  triggers_replace = [
    local_file.butane_file.content_sha256
  ]
  provisioner "local-exec" {
    command = "butane --pretty --strict -o ${path.module}/ignition/bootstrap.ign ${local_file.butane_file.filename}"
  }
}

resource "aws_s3_object" "ignition_file" {
  bucket = module.ignition_files.s3_bucket_id
  key    = "caddy-oauth-server/bootstrap.ign"
  source = "${path.module}/ignition/bootstrap.ign"
  lifecycle {
    replace_triggered_by = [
      local_file.butane_file.content_sha256
    ]
  }
  depends_on = [
    terraform_data.ignition_file
  ]
}

What happened to semaphore.social? #

For the last year or so, I have been using semaphore.social for web-based access to the Fediverse, but suddenly the DNS record does not resolve. I must have been sleeping not to notice that this software is no longer supported; the GitHub repository was archived on 12th December.

There are several alternatives: the original https://pinafore.social/ is still up (even though that software is no longer maintained either); there’s another fork of Pinafore https://enafore.social/ that’s still actively maintained; or I could try https://masto-fe.superseriousbusiness.org/ which is designed to work with GoToSocial servers.

Ubuntu Extended Security Maintenance #

I ran into a security problem with a Docker image based on Ubuntu 22.04: as soon as we install ffmpeg and push the resulting image to ECR, the AWS security scanner detects a critical vulnerability, CVE-2024-35366.

The fix is in ffmpeg version 7:6.1.1-3ubuntu5+esm5. The problem: that esm in the version means the fix is only available with an Ubuntu Pro subscription. While my company would be quite happy to purchase an Ubuntu Pro subscription, the instructions How to enable Ubuntu Pro Services in a Dockerfile note the prerequisite:

You should also build your Docker image on a host machine with an active Ubuntu Pro subscription.

We usually use GitLab or Bitbucket hosted runners for our builds, so we don’t control the host machine, and setting up our own runner would be more work than I have time for right now.

That Ubuntu CVE page shows that the version of ffmpeg in Ubuntu 24.04 also has critical vulnerabilities fixed in an ESM version of the package, while 25.10 is not affected.

You might wonder why I’m using such an old base image. The image I started with is pytorch/pytorch:2.6.0-cuda12.6-cudnn9-runtime, which our RnD team is using for a GenAI project, and it seems all the compatible pytorch images on Docker Hub are based on Ubuntu 22.04. The fall-back plan might be to try updating that to Ubuntu 25.10, but that might also entail a lot of work.

Listening #