]> git.giacomolli.uk - onechat/blobdiff - rebuild.go
Send stored images to WhatsApp chats
[onechat] / rebuild.go
index 11c6bfcc536d1fbff9eef1d0d7c9a83412e5410c..598371caa5343965f4e27cd481cd5a289132ae1c 100644 (file)
@@ -26,7 +26,9 @@ const rebuildCallLimit = 1500
 // removes the new directory and leaves the original directory unchanged.
 // Success copies the other agent files, adds this run's usage, and swaps the
 // directories, with the original kept as a timestamped backup. Message IDs are
 // removes the new directory and leaves the original directory unchanged.
 // Success copies the other agent files, adds this run's usage, and swaps the
 // directories, with the original kept as a timestamped backup. Message IDs are
-// new positions in the merged log, but kind, text, and date stay exact.
+// new positions in the merged log, but kind, text, and date stay exact. A child
+// keeps its staging directory and backup outside children so that neither is
+// read as a configured child.
 func rebuild(ctx context.Context, agent *Agent, added []DatedMessage, client *ModelClient, summarizer Summarizer, out io.Writer) (backup string, err error) {
        err = rebuildIdle(agent.dir)
        if err != nil {
 func rebuild(ctx context.Context, agent *Agent, added []DatedMessage, client *ModelClient, summarizer Summarizer, out io.Writer) (backup string, err error) {
        err = rebuildIdle(agent.dir)
        if err != nil {
@@ -43,7 +45,14 @@ func rebuild(ctx context.Context, agent *Agent, added []DatedMessage, client *Mo
                return "", fmt.Errorf("merge rebuild messages: %w", err)
        }
 
                return "", fmt.Errorf("merge rebuild messages: %w", err)
        }
 
-       root, err := os.MkdirTemp(filepath.Dir(agent.dir), ".agent-rebuild-")
+       parent := filepath.Dir(agent.dir)
+       backupDir := agent.dir
+       if agent.config.Parent != "" {
+               parent = filepath.Dir(parent)
+               backupDir = filepath.Join(parent, agent.config.Name)
+       }
+
+       root, err := os.MkdirTemp(parent, ".agent-rebuild-")
        if err != nil {
                return "", fmt.Errorf("create rebuild directory: %w", err)
        }
        if err != nil {
                return "", fmt.Errorf("create rebuild directory: %w", err)
        }
@@ -110,7 +119,7 @@ func rebuild(ctx context.Context, agent *Agent, added []DatedMessage, client *Mo
                return "", ctx.Err()
        }
 
                return "", ctx.Err()
        }
 
-       backup = agent.dir + "-backup-" + time.Now().Format("20060102-150405.000000000")
+       backup = backupDir + "-backup-" + time.Now().Format("20060102-150405.000000000")
        err = os.Rename(agent.dir, backup)
        if err != nil {
                return "", fmt.Errorf("back up agent: %w", err)
        err = os.Rename(agent.dir, backup)
        if err != nil {
                return "", fmt.Errorf("back up agent: %w", err)