A SimpleX Chat bot that answers arithmetic (closes #1)
check / check (push) Successful in 54s

Remove the template's HTTP service, database and fx wiring. Add exact
arithmetic on go/parser and go/constant, a client that runs simplex-chat
as a child process and drives its WebSocket API, and the bot, which keeps
an auto-accepting address and replies to each message. The image adds the
checksum-pinned simplex-chat v7.0.2 on Ubuntu 22.04.

Model: opus-5-5
This commit is contained in:
clawbot
2026-09-26 22:01:51 +00:00
parent f8ce8cef83
commit 16649e0f2f
66 changed files with 2119 additions and 5059 deletions
+2 -13
View File
@@ -1,19 +1,12 @@
// Command simplexcalc is the service entrypoint.
//
// Seeding a new project: script/rename renames this directory, the
// module path and the binary. Nothing here needs editing by hand.
// Command simplexcalc is a SimpleX Chat bot that answers arithmetic.
package main
import (
"fmt"
"os"
"runtime"
"sneak.berlin/go/simplexcalc/internal/globals"
)
// appname is the name the service reports in logs, metrics and the
// healthcheck.
// appname is the name the program reports in its logs and its version.
const appname = "simplexcalc"
// version is injected at build time with -ldflags "-X main.version=...".
@@ -23,10 +16,6 @@ const appname = "simplexcalc"
var version = "dev"
func main() {
globals.Appname = appname
globals.Version = version
globals.Buildarch = runtime.GOARCH
err := rootCmd().Execute()
if err != nil {
// cobra has already printed the error; this only sets the exit
+8 -7
View File
@@ -8,15 +8,16 @@ import (
)
// rootCmd builds the command tree. Configuration comes from the
// environment (see internal/config), not from flags: the service runs
// in containers, where the environment is the interface, and one source
// of truth means there is no precedence rule to get wrong.
// environment (see internal/config), not from flags: the bot runs in a
// container, where the environment is the interface, and one source of
// truth means there is no precedence rule to get wrong.
func rootCmd() *cobra.Command {
root := &cobra.Command{
Use: appname,
Short: appname + " — an HTTP service",
Long: appname + " is an HTTP service.\n\n" +
"Configuration is read from the environment and from an\n" +
Short: appname + " — a SimpleX Chat bot that answers arithmetic",
Long: appname + " is a SimpleX Chat bot that answers arithmetic.\n\n" +
"It runs the simplex-chat command-line client, which must be on\n" +
"PATH. Configuration is read from the environment and from an\n" +
"optional .env file in the working directory. See README.md\n" +
"for the full list of variables.",
SilenceUsage: true,
@@ -25,7 +26,7 @@ func rootCmd() *cobra.Command {
SilenceErrors: true,
}
root.AddCommand(serveCmd(), versionCmd())
root.AddCommand(runCmd(), versionCmd())
return root
}
+58
View File
@@ -0,0 +1,58 @@
package main
import (
"context"
"fmt"
"os"
"os/signal"
"runtime"
"syscall"
"github.com/spf13/cobra"
"sneak.berlin/go/simplexcalc/internal/bot"
"sneak.berlin/go/simplexcalc/internal/config"
"sneak.berlin/go/simplexcalc/internal/logger"
)
func runCmd() *cobra.Command {
return &cobra.Command{
Use: "run",
Short: "run the bot",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
return run(cmd.Context())
},
}
}
// run reads the configuration and runs the bot until SIGINT or SIGTERM,
// or until it fails. A failure is the process's exit status, so a
// supervisor restarts it.
func run(ctx context.Context) error {
cfg, err := config.New()
if err != nil {
return fmt.Errorf("reading configuration: %w", err)
}
log := logger.New(os.Stdout, cfg.Debug)
log.Info("starting",
"appname", appname,
"version", version,
"arch", runtime.GOARCH,
"data_dir", cfg.DataDir,
)
ctx, stop := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
defer stop()
err = bot.Run(ctx, log, cfg.DataDir)
if err != nil {
log.Error("stopped", "error", err)
return err
}
log.Info("stopped")
return nil
}
-80
View File
@@ -1,80 +0,0 @@
package main
import (
"context"
"fmt"
"time"
"github.com/spf13/cobra"
"go.uber.org/fx"
"sneak.berlin/go/simplexcalc/internal/app"
)
// startTimeout bounds startup: opening the database, applying
// migrations and binding the port. A start that hangs past this is a
// start that failed, and saying so beats waiting forever.
const startTimeout = 30 * time.Second
// stopTimeout bounds the whole stop sequence. The server's own drain is
// bounded by SHUTDOWN_GRACE within this; the margin covers closing the
// database and flushing Sentry after the drain finishes.
const stopTimeout = 45 * time.Second
func serveCmd() *cobra.Command {
return &cobra.Command{
Use: "serve",
Short: "run the HTTP server",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
return serve(cmd.Context())
},
}
}
// serve builds the graph and runs until a signal arrives.
//
// fx.App.Run handles SIGINT and SIGTERM and then runs the stop
// sequence, which is why nothing here installs a signal handler: two
// handlers for the same signal is how a shutdown ends up half done.
func serve(ctx context.Context) error {
fxApp := app.New(
fx.StartTimeout(startTimeout),
fx.StopTimeout(stopTimeout),
)
// A construction error is reported here rather than by Run, which
// would exit the process itself and give the caller nothing to
// report. Configuration failures arrive through this path.
err := fxApp.Err()
if err != nil {
return fmt.Errorf("building application: %w", err)
}
startCtx, cancel := context.WithTimeout(ctx, startTimeout)
defer cancel()
err = fxApp.Start(startCtx)
if err != nil {
return fmt.Errorf("starting application: %w", err)
}
// Block until a signal. fx closes this channel after it receives
// one; the stop sequence is ours to run.
<-fxApp.Wait()
// Deliberately NOT derived from ctx: by the time this runs, the
// signal that ends the process has already cancelled it, and a
// stop context that is cancelled from the outset gives the drain
// no time at all — which is the abrupt termination the graceful
// shutdown exists to avoid.
stopCtx, stopCancel := context.WithTimeout(context.Background(), stopTimeout)
defer stopCancel()
//nolint:contextcheck // see above: the fresh context is the point.
err = fxApp.Stop(stopCtx)
if err != nil {
return fmt.Errorf("stopping application: %w", err)
}
return nil
}