Compare commits

...

9 Commits

Author SHA1 Message Date
joakim 0d57aed8ce version bump 2026-02-25 23:07:44 +01:00
joakim 393b7a144a fix: use Modifier.Set() to maintain AttributeOrder invariant in API handlers
API handlers were populating SetAttributes directly without appending to
AttributeOrder. Since Apply() only iterates AttributeOrder, all updates
via PUT/POST were silently dropped — causing edits to revert and tasks
to disappear on reload.

Adds Modifier.Set() helper, safety net in Apply()/ApplyToNew(), adds
description and status to applyNonDateAttribute, and fixes the
recurrence->recur key mismatch.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 22:42:57 +01:00
joakim 201f32d095 Merge branch 'worktree-web-ui' 2026-02-25 22:39:32 +01:00
joakim e86d063912 fix: show Set... placeholder for scheduled/wait/until date fields
Also fix selectedTask going stale after store updates by syncing
it from the store subscription instead of manual patching.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 22:39:18 +01:00
joakim 10421b0ec6 feat: add hard delete flag and opal clean command
Add --hard flag to `opal delete` for permanent removal and a new
`opal clean` command to bulk-purge soft-deleted tasks with optional
--older duration filter.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 22:30:50 +01:00
joakim 08123aa3c5 fix: shell autocomplete bypasses preprocessing and completes attribute values
Bypass Execute() preprocessing for __complete/__completeNoDesc so Cobra's
built-in completion handles shell TAB without creating tasks. Add root
ValidArgsFunction for flexible syntax (e.g. "opal 1 de<TAB>" → delete),
attribute value completions (status:pending, priority:H, date synonyms),
and NoSpace directive on key: completions to avoid trailing space.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 22:05:15 +01:00
joakim 6c28e4d24a fix: Hide waiting tasks from reports as default behaviour 2026-02-25 21:46:50 +01:00
joakim 9973631df0 refactor: deduplicate engine internals, replace bubble sorts, remove dead code
Extract shared code that was duplicated across functions:
- taskJSON struct (MarshalJSON/UnmarshalJSON) to package-level type
- scanTask(scanner) helper for GetTask/GetTasks (~70 identical lines)
- monthNames map for parseMonthName/parseDayAndMonth
- applyNonDateAttribute helper for Apply/ApplyToNew
- resolveDisplayID calls replace inline loops in FormatTaskListWithFormat

Replace O(n²) bubble sorts with sort.Slice in all four report sort
functions (sortByUrgency, NewestReport, NextReport, OldestReport).

Remove dead code: formatTimeWithColor (unused, also used time.Now()
instead of timeNow()), getCurrentTimestamp (unnecessary wrapper).

Remove ~20 comments that restated the next line of code.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-21 01:46:46 +01:00
joakim a11f452d3b fix: break sync feedback loop, respect timestamps, surface errors
- Add migration v2: source column on change_log to distinguish local
  vs sync-originated entries, preventing the echo loop where synced
  tasks get re-pushed as local changes
- PushChanges handler now skips save when server version is newer
- Client PushChanges/pushQueuedChanges collect and report marshal errors
  instead of silently dropping them
- De-duplicate getLocalChanges/getLastSyncTime into exported sync
  package functions
- Fix logConflict winner detection via pointer identity instead of
  fragile UUID+timestamp comparison
- Fix sync down to actually parse, save, and tag-sync pulled changes

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-21 01:11:04 +01:00
21 changed files with 606 additions and 418 deletions
+1 -1
View File
@@ -1 +1 @@
0.1.0
0.2.0
+77
View File
@@ -0,0 +1,77 @@
package cmd
import (
"fmt"
"os"
"time"
"git.jnss.me/joakim/opal/internal/engine"
"github.com/spf13/cobra"
)
var olderFlag string
var cleanCmd = &cobra.Command{
Use: "clean",
Short: "Purge soft-deleted tasks from the database",
Run: func(cmd *cobra.Command, args []string) {
if err := cleanTasks(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
},
}
func init() {
cleanCmd.Flags().StringVar(&olderFlag, "older", "", "Only purge tasks deleted longer than this duration ago (e.g. 30d, 1w)")
}
func cleanTasks() error {
var olderThan *time.Duration
if olderFlag != "" {
d, err := engine.ParseRecurrencePattern(olderFlag)
if err != nil {
return fmt.Errorf("invalid duration %q: %w", olderFlag, err)
}
olderThan = &d
}
tasks, err := engine.GetDeletedTasks(olderThan)
if err != nil {
return err
}
if len(tasks) == 0 {
fmt.Println("No deleted tasks to purge.")
return nil
}
if dryRunFlag {
fmt.Printf("Would permanently remove %d deleted task(s).\n", len(tasks))
return nil
}
if len(tasks) > 1 {
fmt.Printf("Permanently remove %d deleted task(s)? This cannot be undone. (y/N): ", len(tasks))
var confirm string
fmt.Scanln(&confirm)
if confirm != "y" && confirm != "Y" {
fmt.Println("Cancelled.")
return nil
}
}
for _, task := range tasks {
if err := task.Delete(true); err != nil {
return fmt.Errorf("failed to purge task %s: %w", task.UUID, err)
}
}
fmt.Printf("Purged %d deleted task(s).\n", len(tasks))
if err := engine.CleanupChangeLog(); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to clean up change log: %v\n", err)
}
return nil
}
+84 -2
View File
@@ -2,14 +2,37 @@ package cmd
import (
"fmt"
"strings"
"git.jnss.me/joakim/opal/internal/engine"
"github.com/spf13/cobra"
)
// taskFilterCompletion provides dynamic completions for task filter arguments.
// Suggests +tag and project:name completions from the database.
// Suggests +tag, project:name, and attribute value completions from the database.
func taskFilterCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// If typing a key:value, complete the value part
if idx := strings.IndexByte(toComplete, ':'); idx >= 0 {
key := toComplete[:idx]
if engine.ValidAttributeKeys[key] {
return attributeValueCompletions(key, toComplete), cobra.ShellCompDirectiveNoFileComp
}
}
// If toComplete is a prefix of an attribute key, return only key:
// completions with NoSpace so the cursor stays after the colon.
if toComplete != "" && !strings.HasPrefix(toComplete, "+") && !strings.HasPrefix(toComplete, "-") {
var keyCompletions []string
for key := range engine.ValidAttributeKeys {
if strings.HasPrefix(key, toComplete) {
keyCompletions = append(keyCompletions, fmt.Sprintf("%s:", key))
}
}
if len(keyCompletions) > 0 {
return keyCompletions, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveNoSpace
}
}
var completions []string
tags, err := engine.GetAllTags()
@@ -31,10 +54,69 @@ func taskFilterCompletion(cmd *cobra.Command, args []string, toComplete string)
completions = append(completions, fmt.Sprintf("%s:", key))
}
return completions, cobra.ShellCompDirectiveNoFileComp
return completions, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveNoSpace
}
// attributeValueCompletions returns key:value completions for a known attribute key.
// Cobra filters by prefix automatically, so we return all values prefixed with "key:".
func attributeValueCompletions(key, toComplete string) []string {
var values []string
switch key {
case "status":
values = []string{"pending", "completed", "deleted", "recurring"}
case "priority":
values = []string{"H", "M", "L"}
case "project":
projects, err := engine.GetAllProjects()
if err == nil {
values = projects
}
case "due", "wait", "scheduled", "until":
values = []string{
"today", "tomorrow", "yesterday", "now",
"eod", "sow", "eow", "som", "eom",
"mon", "tue", "wed", "thu", "fri", "sat", "sun",
}
case "recur":
values = []string{"daily", "weekly", "monthly", "yearly", "1d", "1w", "2w", "1m", "1y"}
}
completions := make([]string, 0, len(values))
for _, v := range values {
completions = append(completions, fmt.Sprintf("%s:%s", key, v))
}
return completions
}
// rootValidArgsFunction provides completions for root-level arguments,
// enabling flexible syntax like "opal 1 de<TAB>" to complete to "delete".
func rootValidArgsFunction(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// Delegate to taskFilterCompletion first — if toComplete is a partial
// attribute key, it returns early with NoSpace and we should honour that.
filterCompletions, directive := taskFilterCompletion(cmd, args, toComplete)
var completions []string
// Suggest command names
for _, name := range commandNames {
completions = append(completions, name)
}
// Suggest report names
for _, name := range reportNames {
completions = append(completions, name)
}
completions = append(completions, filterCompletions...)
return completions, directive
}
func init() {
// Root command completions for flexible syntax (e.g., "opal 1 de<TAB>")
rootCmd.ValidArgsFunction = rootValidArgsFunction
// Register dynamic completions for commands that accept filters
addCmd.ValidArgsFunction = taskFilterCompletion
doneCmd.ValidArgsFunction = taskFilterCompletion
+29 -8
View File
@@ -8,19 +8,25 @@ import (
"github.com/spf13/cobra"
)
var hardDeleteFlag bool
var deleteCmd = &cobra.Command{
Use: "delete [filter...]",
Short: "Delete tasks",
Run: func(cmd *cobra.Command, args []string) {
parsed := getParsedArgs(cmd)
if err := deleteTasks(parsed.Filters); err != nil {
if err := deleteTasks(parsed.Filters, hardDeleteFlag); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
},
}
func deleteTasks(args []string) error {
func init() {
deleteCmd.Flags().BoolVar(&hardDeleteFlag, "hard", false, "Permanently remove task from database")
}
func deleteTasks(args []string, hard bool) error {
filter, err := engine.ParseFilter(args)
if err != nil {
return err
@@ -53,15 +59,24 @@ func deleteTasks(args []string) error {
return fmt.Errorf("no tasks matched filter")
}
action := "delete"
if hard {
action = "permanently delete"
}
if dryRunFlag {
fmt.Print(engine.FormatTaskConfirmList("delete", tasks, ws))
fmt.Print(engine.FormatTaskConfirmList(action, tasks, ws))
fmt.Println("Dry run — no changes made.")
return nil
}
if len(tasks) > 1 {
fmt.Print(engine.FormatTaskConfirmList("delete", tasks, ws))
if len(tasks) > 1 || hard {
fmt.Print(engine.FormatTaskConfirmList(action, tasks, ws))
if hard {
fmt.Printf("This cannot be undone. Proceed? (y/N): ")
} else {
fmt.Printf("Proceed? (y/N): ")
}
var confirm string
fmt.Scanln(&confirm)
if confirm != "y" && confirm != "Y" {
@@ -71,14 +86,20 @@ func deleteTasks(args []string) error {
}
for _, task := range tasks {
task.Delete(false) // Soft delete
task.Delete(hard)
if !hard {
engine.RecordUndo("delete", task.UUID)
}
}
verb := "Deleted"
if hard {
verb = "Permanently deleted"
}
if len(tasks) == 1 {
fmt.Printf("Deleted task %s\n", engine.FormatTaskSummary(tasks[0], ws))
fmt.Printf("%s task %s\n", verb, engine.FormatTaskSummary(tasks[0], ws))
} else {
fmt.Printf("Deleted %d task(s).\n", len(tasks))
fmt.Printf("%s %d task(s).\n", verb, len(tasks))
}
return nil
}
+9 -1
View File
@@ -32,7 +32,7 @@ var (
// Command classification
var commandNames = []string{
"add", "done", "modify", "delete",
"add", "done", "modify", "delete", "clean",
"start", "stop", "count", "projects", "tags",
"info", "edit", "server", "sync", "reports", "setup",
"version", "annotate", "denotate", "undo", "uncomplete", "log", "completion",
@@ -56,6 +56,7 @@ var rootCmd = &cobra.Command{
Short: "Opal task manager - taskwarrior-inspired CLI task management",
Long: `Opal is a powerful command-line task manager.
It supports filtering, tags, priorities, projects, and recurring tasks.`,
Args: cobra.ArbitraryArgs,
Run: func(cmd *cobra.Command, args []string) {
// Default behavior: run configured default report (defaults to "list")
parsed := getParsedArgs(cmd)
@@ -87,6 +88,11 @@ func Execute() error {
if firstArg == "-h" || firstArg == "--help" || firstArg == "help" {
return rootCmd.Execute()
}
// Let Cobra's built-in completion machinery handle shell completions
// directly, bypassing preprocessing that would create tasks.
if firstArg == "__complete" || firstArg == "__completeNoDesc" {
return rootCmd.Execute()
}
}
// Preprocess arguments (read-only scan — os.Args is never mutated)
@@ -278,6 +284,7 @@ func init() {
undoCmd.GroupID = "task"
uncompleteCmd.GroupID = "task"
logCmd.GroupID = "task"
cleanCmd.GroupID = "task"
rootCmd.AddCommand(addCmd)
rootCmd.AddCommand(doneCmd)
@@ -292,6 +299,7 @@ func init() {
rootCmd.AddCommand(undoCmd)
rootCmd.AddCommand(uncompleteCmd)
rootCmd.AddCommand(logCmd)
rootCmd.AddCommand(cleanCmd)
// Other commands
countCmd.GroupID = "other"
+52 -61
View File
@@ -224,8 +224,8 @@ var syncUpCmd = &cobra.Command{
client := sync.NewClient(cfg.SyncURL, cfg.SyncAPIKey, cfg.SyncClientID)
// Get local changes
lastSync := getLastSyncTime(cfg.SyncClientID)
localChanges, err := getLocalChanges(lastSync)
lastSync := sync.GetLastSyncTime(cfg.SyncClientID)
localChanges, err := sync.GetLocalChanges(lastSync)
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting local changes: %v\n", err)
os.Exit(1)
@@ -264,7 +264,7 @@ var syncDownCmd = &cobra.Command{
}
client := sync.NewClient(cfg.SyncURL, cfg.SyncAPIKey, cfg.SyncClientID)
lastSync := getLastSyncTime(cfg.SyncClientID)
lastSync := sync.GetLastSyncTime(cfg.SyncClientID)
changes, err := client.PullChanges(lastSync)
if err != nil {
@@ -277,7 +277,55 @@ var syncDownCmd = &cobra.Command{
return
}
fmt.Printf("✓ Pulled %d changes from server\n", len(changes))
// Parse changes into tasks
tasks, err := client.ParseChanges(changes)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing changes: %v\n", err)
os.Exit(1)
}
// Apply each task locally
var applied int
for _, task := range tasks {
if err := task.Save(); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to save task %s: %v\n", task.UUID, err)
continue
}
// Mark as sync-originated to prevent feedback loop
_ = engine.MarkChangeLogAsSync(task.UUID.String())
// Sync tags
savedTask, err := engine.GetTask(task.UUID)
if err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to reload task %s: %v\n", task.UUID, err)
continue
}
currentTags, _ := savedTask.GetTags()
currentSet := make(map[string]bool)
for _, tag := range currentTags {
currentSet[tag] = true
}
desiredSet := make(map[string]bool)
for _, tag := range task.Tags {
desiredSet[tag] = true
}
for tag := range currentSet {
if !desiredSet[tag] {
savedTask.RemoveTag(tag)
}
}
for tag := range desiredSet {
if !currentSet[tag] {
savedTask.AddTag(tag)
}
}
applied++
}
fmt.Printf("✓ Pulled %d changes, applied %d tasks from server\n", len(changes), applied)
},
}
@@ -414,63 +462,6 @@ func init() {
syncCmd.PersistentFlags().BoolVarP(&quietFlag, "quiet", "q", false, "Suppress progress output")
}
// Helper functions
func getLastSyncTime(clientID string) int64 {
db := engine.GetDB()
if db == nil {
return 0
}
var lastSync int64
err := db.QueryRow("SELECT last_sync FROM sync_state WHERE client_id = ?", clientID).Scan(&lastSync)
if err != nil {
return 0
}
return lastSync
}
func getLocalChanges(since int64) ([]*engine.Task, error) {
db := engine.GetDB()
if db == nil {
return nil, fmt.Errorf("database not initialized")
}
rows, err := db.Query(`
SELECT DISTINCT task_uuid
FROM change_log
WHERE changed_at > ?
ORDER BY changed_at ASC
`, since)
if err != nil {
return nil, err
}
defer rows.Close()
var tasks []*engine.Task
for rows.Next() {
var uuidStr string
if err := rows.Scan(&uuidStr); err != nil {
continue
}
taskUUID, err := uuid.Parse(uuidStr)
if err != nil {
continue
}
task, err := engine.GetTask(taskUUID)
if err != nil {
continue
}
tasks = append(tasks, task)
}
return tasks, nil
}
func formatTimestamp(ts int64) string {
t := time.Unix(ts, 0)
now := time.Now()
+7 -2
View File
@@ -104,6 +104,8 @@ func PushChanges(w http.ResponseWriter, r *http.Request) {
if err := task.Save(); err != nil {
continue
}
// Mark as sync-originated to prevent feedback loop
_ = engine.MarkChangeLogAsSync(task.UUID.String())
// Add tags
for _, tag := range task.Tags {
_ = task.AddTag(tag)
@@ -114,15 +116,18 @@ func PushChanges(w http.ResponseWriter, r *http.Request) {
// Task exists - check timestamps for conflicts
if existing.Modified.Unix() > task.Modified.Unix() {
// Server version is newer - conflict (but we'll apply last-write-wins)
// Server version is newer - skip this push
conflicts++
continue
}
// Apply changes (last-write-wins)
// Apply changes (client is newer or equal)
task.ID = existing.ID // Preserve database ID
if err := task.Save(); err != nil {
continue
}
// Mark as sync-originated to prevent feedback loop
_ = engine.MarkChangeLogAsSync(task.UUID.String())
// Sync tags
existingTags := make(map[string]bool)
+16 -16
View File
@@ -130,35 +130,35 @@ func CreateTask(w http.ResponseWriter, r *http.Request) {
mod.AddTags = req.Tags
if req.Project != nil {
mod.SetAttributes["project"] = req.Project
mod.Set("project", req.Project)
}
if req.Priority != nil {
mod.SetAttributes["priority"] = req.Priority
mod.Set("priority", req.Priority)
}
if req.Due != nil {
dueStr := fmt.Sprintf("%d", *req.Due)
mod.SetAttributes["due"] = &dueStr
mod.Set("due", &dueStr)
}
if req.Scheduled != nil {
scheduledStr := fmt.Sprintf("%d", *req.Scheduled)
mod.SetAttributes["scheduled"] = &scheduledStr
mod.Set("scheduled", &scheduledStr)
}
if req.Wait != nil {
waitStr := fmt.Sprintf("%d", *req.Wait)
mod.SetAttributes["wait"] = &waitStr
mod.Set("wait", &waitStr)
}
if req.Until != nil {
untilStr := fmt.Sprintf("%d", *req.Until)
mod.SetAttributes["until"] = &untilStr
mod.Set("until", &untilStr)
}
if req.Recurrence != nil {
mod.SetAttributes["recurrence"] = req.Recurrence
mod.Set("recur", req.Recurrence)
}
// Create task
@@ -232,39 +232,39 @@ func UpdateTask(w http.ResponseWriter, r *http.Request) {
mod := engine.NewModifier()
if req.Description != nil {
mod.SetAttributes["description"] = req.Description
mod.Set("description", req.Description)
}
if req.Status != nil {
mod.SetAttributes["status"] = req.Status
mod.Set("status", req.Status)
}
if req.Priority != nil {
mod.SetAttributes["priority"] = req.Priority
mod.Set("priority", req.Priority)
}
if req.Project != nil {
mod.SetAttributes["project"] = req.Project
mod.Set("project", req.Project)
}
if req.Due != nil {
dueStr := fmt.Sprintf("%d", *req.Due)
mod.SetAttributes["due"] = &dueStr
mod.Set("due", &dueStr)
}
if req.Scheduled != nil {
scheduledStr := fmt.Sprintf("%d", *req.Scheduled)
mod.SetAttributes["scheduled"] = &scheduledStr
mod.Set("scheduled", &scheduledStr)
}
if req.Wait != nil {
waitStr := fmt.Sprintf("%d", *req.Wait)
mod.SetAttributes["wait"] = &waitStr
mod.Set("wait", &waitStr)
}
if req.Until != nil {
untilStr := fmt.Sprintf("%d", *req.Until)
mod.SetAttributes["until"] = &untilStr
mod.Set("until", &untilStr)
}
if req.Start != nil {
@@ -273,7 +273,7 @@ func UpdateTask(w http.ResponseWriter, r *http.Request) {
}
if req.Recurrence != nil {
mod.SetAttributes["recurrence"] = req.Recurrence
mod.Set("recur", req.Recurrence)
}
// Apply modifier
+30 -7
View File
@@ -307,6 +307,13 @@ func runMigrations() error {
END;
`,
},
{
version: 2,
sql: `
ALTER TABLE change_log ADD COLUMN source TEXT NOT NULL DEFAULT 'local';
CREATE INDEX idx_change_log_source ON change_log(source);
`,
},
}
// Apply pending migrations
@@ -327,7 +334,7 @@ func runMigrations() error {
if _, err := tx.Exec(
"INSERT INTO schema_version (version, applied_at) VALUES (?, ?)",
migration.version,
getCurrentTimestamp(),
GetCurrentTimestamp(),
); err != nil {
tx.Rollback()
return fmt.Errorf("failed to record migration %d: %w", migration.version, err)
@@ -342,14 +349,9 @@ func runMigrations() error {
return nil
}
// getCurrentTimestamp returns the current Unix timestamp
func getCurrentTimestamp() int64 {
return timeNow().Unix()
}
// GetCurrentTimestamp returns the current Unix timestamp (exported for API use)
func GetCurrentTimestamp() int64 {
return getCurrentTimestamp()
return timeNow().Unix()
}
// CleanupChangeLog removes old change log entries based on retention policy
@@ -409,3 +411,24 @@ func SetChangeLogRetentionDays(days int) error {
_, err := db.Exec("INSERT OR REPLACE INTO sync_config (key, value) VALUES ('change_log_retention_days', ?)", days)
return err
}
// MarkChangeLogAsSync marks the most recent change_log entry for a task UUID
// as originating from sync (not local), preventing the feedback loop where
// synced changes get re-pushed as local changes.
func MarkChangeLogAsSync(taskUUID string) error {
db := GetDB()
if db == nil {
return fmt.Errorf("database not initialized")
}
_, err := db.Exec(`
UPDATE change_log SET source = 'sync'
WHERE id = (
SELECT id FROM change_log
WHERE task_uuid = ?
ORDER BY id DESC
LIMIT 1
)
`, taskUUID)
return err
}
+17 -32
View File
@@ -7,6 +7,21 @@ import (
"time"
)
var monthNames = map[string]time.Month{
"jan": time.January, "january": time.January,
"feb": time.February, "february": time.February,
"mar": time.March, "march": time.March,
"apr": time.April, "april": time.April,
"may": time.May,
"jun": time.June, "june": time.June,
"jul": time.July, "july": time.July,
"aug": time.August, "august": time.August,
"sep": time.September, "september": time.September,
"oct": time.October, "october": time.October,
"nov": time.November, "november": time.November,
"dec": time.December, "december": time.December,
}
// DateParser handles all date/time/duration parsing with configurable options
type DateParser struct {
base time.Time
@@ -238,22 +253,7 @@ func (p *DateParser) parseWeekday(s string) (time.Time, bool) {
// parseMonthName handles month names (jan, january, feb, february, etc.)
func (p *DateParser) parseMonthName(s string) (time.Time, bool) {
months := map[string]time.Month{
"jan": time.January, "january": time.January,
"feb": time.February, "february": time.February,
"mar": time.March, "march": time.March,
"apr": time.April, "april": time.April,
"may": time.May,
"jun": time.June, "june": time.June,
"jul": time.July, "july": time.July,
"aug": time.August, "august": time.August,
"sep": time.September, "september": time.September,
"oct": time.October, "october": time.October,
"nov": time.November, "november": time.November,
"dec": time.December, "december": time.December,
}
month, ok := months[s]
month, ok := monthNames[s]
if !ok {
return time.Time{}, false
}
@@ -316,22 +316,7 @@ func (p *DateParser) parseDayAndMonth(dayStr, monthStr string) (int, time.Month,
return 0, 0, false
}
months := map[string]time.Month{
"jan": time.January, "january": time.January,
"feb": time.February, "february": time.February,
"mar": time.March, "march": time.March,
"apr": time.April, "april": time.April,
"may": time.May,
"jun": time.June, "june": time.June,
"jul": time.July, "july": time.July,
"aug": time.August, "august": time.August,
"sep": time.September, "september": time.September,
"oct": time.October, "october": time.October,
"nov": time.November, "november": time.November,
"dec": time.December, "december": time.December,
}
month, ok := months[monthStr]
month, ok := monthNames[monthStr]
if !ok {
return 0, 0, false
}
+6 -29
View File
@@ -29,15 +29,9 @@ func FormatTaskListWithFormat(tasks []*Task, ws *WorkingSet, format string) stri
if format == "minimal" {
result := ""
for i, task := range tasks {
displayID := i + 1
if ws != nil {
// Use working set display ID if available
for id, uuid := range ws.byID {
if uuid == task.UUID {
displayID = id
break
}
}
displayID := resolveDisplayID(task, ws)
if displayID == 0 {
displayID = i + 1
}
urgency := task.CalculateUrgency(coeffs)
urgencyColor := getUrgencyColor(urgency)
@@ -71,15 +65,9 @@ func FormatTaskListWithFormat(tasks []*Task, ws *WorkingSet, format string) stri
// Add rows
for i, task := range tasks {
displayID := i + 1
if ws != nil {
// Use working set display ID if available
for id, uuid := range ws.byID {
if uuid == task.UUID {
displayID = id
break
}
}
displayID := resolveDisplayID(task, ws)
if displayID == 0 {
displayID = i + 1
}
urgency := task.CalculateUrgency(coeffs)
@@ -270,8 +258,6 @@ func FormatTagCounts(tagCounts map[string]int) string {
return t.Render()
}
// Helper functions
func formatStatus(status Status) string {
switch status {
case StatusPending:
@@ -322,7 +308,6 @@ func formatUrgency(urgency float64) string {
}
func getUrgencyColor(urgency float64) *color.Color {
// Returns color for minimal format
if urgency >= 10.0 {
return color.New(color.FgHiRed, color.Bold)
} else if urgency >= 5.0 {
@@ -362,14 +347,6 @@ func formatDue(due *time.Time) string {
return rel
}
func formatTimeWithColor(t time.Time) string {
now := time.Now()
if t.Before(now) {
return color.RedString(t.Format("2006-01-02 15:04"))
}
return t.Format("2006-01-02 15:04")
}
func formatTime(t time.Time) string {
return t.Format("2006-01-02 15:04")
}
-5
View File
@@ -71,10 +71,8 @@ func (f *Filter) ToSQL() (string, []interface{}) {
conditions := []string{}
args := []interface{}{}
// Track if we have an explicit status filter
hasStatusFilter := false
// Status filter
if status, ok := f.Attributes["status"]; ok {
hasStatusFilter = true
@@ -104,13 +102,11 @@ func (f *Filter) ToSQL() (string, []interface{}) {
}
}
// Project filter
if project, ok := f.Attributes["project"]; ok {
conditions = append(conditions, "project = ?")
args = append(args, project)
}
// Priority filter
if priority, ok := f.Attributes["priority"]; ok {
priorityInt := priorityStringToInt(priority)
conditions = append(conditions, "priority = ?")
@@ -138,7 +134,6 @@ func (f *Filter) ToSQL() (string, []interface{}) {
args = append(args, tag)
}
// UUID filter
if len(f.UUIDs) > 0 {
placeholders := strings.Repeat("?,", len(f.UUIDs))
placeholders = placeholders[:len(placeholders)-1]
+1
View File
@@ -4,6 +4,7 @@ package engine
// Used by parseAddArgs (cmd/add.go), ParseFilter, and ParseModifier
// to distinguish modifiers from description text.
var ValidAttributeKeys = map[string]bool{
"description": true,
"due": true,
"priority": true,
"project": true,
+44 -27
View File
@@ -23,6 +23,13 @@ func NewModifier() *Modifier {
}
}
// Set adds an attribute to the modifier, maintaining the SetAttributes and
// AttributeOrder invariant. Pass nil to clear the attribute.
func (m *Modifier) Set(key string, value *string) {
m.SetAttributes[key] = value
m.AttributeOrder = append(m.AttributeOrder, key)
}
// ParseModifier parses command-line args into Modifier.
// Only recognized attribute keys (ValidAttributeKeys) are accepted;
// unrecognized key:value tokens produce an error.
@@ -86,13 +93,20 @@ func (m *Modifier) Apply(task *Task) error {
resolvedDates["created"] = task.Created
resolvedDates["modified"] = task.Modified
// Safety net: if SetAttributes were populated without AttributeOrder,
// reconstruct order from map keys so updates aren't silently dropped.
if len(m.AttributeOrder) == 0 && len(m.SetAttributes) > 0 {
for key := range m.SetAttributes {
m.AttributeOrder = append(m.AttributeOrder, key)
}
}
// Apply attributes in the order they were specified (important for relative references)
dateKeys := DateKeys
for _, key := range m.AttributeOrder {
valuePtr := m.SetAttributes[key]
// Handle date attributes with relative expression support
if dateKeys[key] {
if err := applyDateAttribute(key, valuePtr, task, resolvedDates); err != nil {
return err
@@ -100,30 +114,11 @@ func (m *Modifier) Apply(task *Task) error {
continue
}
// Handle non-date attributes
switch key {
case "priority":
if valuePtr == nil {
task.Priority = PriorityDefault
} else {
task.Priority = Priority(priorityStringToInt(*valuePtr))
}
case "project":
task.Project = valuePtr
case "recur":
if valuePtr == nil {
task.RecurrenceDuration = nil
} else {
duration, err := ParseRecurrencePattern(*valuePtr)
if err != nil {
return fmt.Errorf("invalid recurrence: %w", err)
}
task.RecurrenceDuration = &duration
}
if err := applyNonDateAttribute(key, valuePtr, task); err != nil {
return err
}
}
// Apply tag changes
for _, tag := range m.AddTags {
if err := task.AddTag(tag); err != nil {
return err
@@ -154,13 +149,20 @@ func (m *Modifier) ApplyToNew(task *Task) error {
resolvedDates["created"] = task.Created
}
// Safety net: if SetAttributes were populated without AttributeOrder,
// reconstruct order from map keys so updates aren't silently dropped.
if len(m.AttributeOrder) == 0 && len(m.SetAttributes) > 0 {
for key := range m.SetAttributes {
m.AttributeOrder = append(m.AttributeOrder, key)
}
}
// Apply attributes in the order they were specified (important for relative references)
dateKeys := DateKeys
for _, key := range m.AttributeOrder {
valuePtr := m.SetAttributes[key]
// Handle date attributes with relative expression support
if dateKeys[key] {
if err := applyDateAttribute(key, valuePtr, task, resolvedDates); err != nil {
return err
@@ -168,8 +170,26 @@ func (m *Modifier) ApplyToNew(task *Task) error {
continue
}
// Handle non-date attributes
if err := applyNonDateAttribute(key, valuePtr, task); err != nil {
return err
}
}
// Note: Tags are added after task is saved (in CreateTask function)
return nil
}
// applyNonDateAttribute applies a non-date attribute to a task.
func applyNonDateAttribute(key string, valuePtr *string, task *Task) error {
switch key {
case "description":
if valuePtr != nil {
task.Description = *valuePtr
}
case "status":
if valuePtr != nil && len(*valuePtr) > 0 {
task.Status = Status((*valuePtr)[0])
}
case "priority":
if valuePtr == nil {
task.Priority = PriorityDefault
@@ -189,9 +209,6 @@ func (m *Modifier) ApplyToNew(task *Task) error {
task.RecurrenceDuration = &duration
}
}
}
// Note: Tags are added after task is saved (in CreateTask function)
return nil
}
-4
View File
@@ -14,20 +14,16 @@ func ParseKeyValueFormat(data string, skipComments bool) (map[string]string, err
lines := strings.Split(data, "\n")
for i, line := range lines {
// Trim whitespace
line = strings.TrimSpace(line)
// Skip empty lines
if line == "" {
continue
}
// Skip comments if requested
if skipComments && strings.HasPrefix(line, "#") {
continue
}
// Split on first ':'
parts := strings.SplitN(line, ":", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("line %d: invalid format (expected 'key:value')", i+1)
+22 -36
View File
@@ -2,6 +2,7 @@ package engine
import (
"fmt"
"sort"
)
// DisplayFormat defines how tasks should be displayed
@@ -20,6 +21,7 @@ type Report struct {
DisplayFormat DisplayFormat // How to display results
SortFunc func([]*Task) []*Task
LimitFunc func([]*Task) []*Task
ShowWaiting bool // If false (default), tasks with future wait dates are hidden
}
// AllReports returns all predefined reports
@@ -76,6 +78,7 @@ func AllReport() *Report {
Description: "All tasks",
BaseFilter: filter,
DisplayFormat: DisplayFormatTable,
ShowWaiting: true,
}
}
@@ -131,16 +134,11 @@ func NewestReport() *Report {
BaseFilter: filter,
DisplayFormat: DisplayFormatTable,
SortFunc: func(tasks []*Task) []*Task {
// Sort by created descending
sorted := make([]*Task, len(tasks))
copy(sorted, tasks)
for i := 0; i < len(sorted)-1; i++ {
for j := i + 1; j < len(sorted); j++ {
if sorted[i].Created.Before(sorted[j].Created) {
sorted[i], sorted[j] = sorted[j], sorted[i]
}
}
}
sort.Slice(sorted, func(i, j int) bool {
return sorted[i].Created.After(sorted[j].Created)
})
return sorted
},
LimitFunc: func(tasks []*Task) []*Task {
@@ -164,23 +162,14 @@ func NextReport() *Report {
BaseFilter: filter,
DisplayFormat: DisplayFormatTable,
SortFunc: func(tasks []*Task) []*Task {
// Sort by urgency descending
cfg, _ := GetConfig()
coeffs := BuildUrgencyCoefficients(cfg)
sorted := make([]*Task, len(tasks))
copy(sorted, tasks)
for i := 0; i < len(sorted)-1; i++ {
for j := i + 1; j < len(sorted); j++ {
urgI := sorted[i].CalculateUrgency(coeffs)
urgJ := sorted[j].CalculateUrgency(coeffs)
if urgI < urgJ {
sorted[i], sorted[j] = sorted[j], sorted[i]
}
}
}
sort.Slice(sorted, func(i, j int) bool {
return sorted[i].CalculateUrgency(coeffs) > sorted[j].CalculateUrgency(coeffs)
})
return sorted
},
LimitFunc: func(tasks []*Task) []*Task {
@@ -208,16 +197,11 @@ func OldestReport() *Report {
BaseFilter: filter,
DisplayFormat: DisplayFormatTable,
SortFunc: func(tasks []*Task) []*Task {
// Sort by created ascending (already default, but explicit)
sorted := make([]*Task, len(tasks))
copy(sorted, tasks)
for i := 0; i < len(sorted)-1; i++ {
for j := i + 1; j < len(sorted); j++ {
if sorted[i].Created.After(sorted[j].Created) {
sorted[i], sorted[j] = sorted[j], sorted[i]
}
}
}
sort.Slice(sorted, func(i, j int) bool {
return sorted[i].Created.Before(sorted[j].Created)
})
return sorted
},
}
@@ -291,6 +275,7 @@ func WaitingReport() *Report {
Description: "Hidden/waiting tasks",
BaseFilter: filter,
DisplayFormat: DisplayFormatTable,
ShowWaiting: true,
}
}
@@ -340,6 +325,12 @@ func (r *Report) applyPostFilters(tasks []*Task) []*Task {
for _, task := range tasks {
include := true
// By default, hide tasks with a future wait date (like taskwarrior).
// Reports that need to show waiting tasks set ShowWaiting = true.
if !r.ShowWaiting && task.Wait != nil && task.Wait.After(now) {
include = false
}
// Check for _started marker
if r.BaseFilter.Attributes["_started"] == "true" {
if task.Start == nil {
@@ -429,18 +420,13 @@ func sortByUrgency(tasks []*Task) []*Task {
sorted := make([]*Task, len(tasks))
copy(sorted, tasks)
// Calculate and store urgency on each task
for _, t := range sorted {
t.Urgency = t.CalculateUrgency(coeffs)
}
for i := 0; i < len(sorted)-1; i++ {
for j := i + 1; j < len(sorted); j++ {
if sorted[i].Urgency < sorted[j].Urgency {
sorted[i], sorted[j] = sorted[j], sorted[i]
}
}
}
sort.Slice(sorted, func(i, j int) bool {
return sorted[i].Urgency > sorted[j].Urgency
})
return sorted
}
+79 -114
View File
@@ -83,9 +83,8 @@ type Task struct {
Urgency float64 `json:"urgency"`
}
// MarshalJSON emits Task with unix timestamps (int64) instead of RFC3339 strings.
func (t Task) MarshalJSON() ([]byte, error) {
type taskJSON struct {
// taskJSON is the wire format for Task, using unix timestamps instead of time.Time.
type taskJSON struct {
UUID uuid.UUID `json:"uuid"`
ID int `json:"id"`
Status Status `json:"status"`
@@ -105,8 +104,10 @@ func (t Task) MarshalJSON() ([]byte, error) {
Annotations []Annotation `json:"annotations,omitempty"`
Tags []string `json:"tags"`
Urgency float64 `json:"urgency"`
}
}
// MarshalJSON emits Task with unix timestamps (int64) instead of RFC3339 strings.
func (t Task) MarshalJSON() ([]byte, error) {
toUnix := func(tp *time.Time) *int64 {
if tp == nil {
return nil
@@ -146,28 +147,6 @@ func (t Task) MarshalJSON() ([]byte, error) {
// UnmarshalJSON parses Task from JSON with unix timestamps (int64) and duration in seconds.
func (t *Task) UnmarshalJSON(data []byte) error {
type taskJSON struct {
UUID uuid.UUID `json:"uuid"`
ID int `json:"id"`
Status Status `json:"status"`
Description string `json:"description"`
Project *string `json:"project"`
Priority Priority `json:"priority"`
Created int64 `json:"created"`
Modified int64 `json:"modified"`
Start *int64 `json:"start,omitempty"`
End *int64 `json:"end,omitempty"`
Due *int64 `json:"due,omitempty"`
Scheduled *int64 `json:"scheduled,omitempty"`
Wait *int64 `json:"wait,omitempty"`
Until *int64 `json:"until,omitempty"`
RecurrenceDuration *int64 `json:"recurrence_duration,omitempty"`
ParentUUID *uuid.UUID `json:"parent_uuid,omitempty"`
Annotations []Annotation `json:"annotations,omitempty"`
Tags []string `json:"tags"`
Urgency float64 `json:"urgency"`
}
var raw taskJSON
if err := json.Unmarshal(data, &raw); err != nil {
return err
@@ -366,21 +345,13 @@ func CreateTaskWithModifier(description string, mod *Modifier) (*Task, error) {
return task, nil
}
// GetTask retrieves a task by UUID
func GetTask(taskUUID uuid.UUID) (*Task, error) {
db := GetDB()
if db == nil {
return nil, fmt.Errorf("database not initialized")
}
query := `
SELECT id, uuid, status, description, project, priority,
created, modified, start, end, due, scheduled, wait, until_date,
recurrence_duration, parent_uuid, annotations
FROM tasks
WHERE uuid = ?
`
// scanner is satisfied by both *sql.Row and *sql.Rows.
type scanner interface {
Scan(dest ...interface{}) error
}
// scanTask reads a single task row from a scanner and populates all fields including tags.
func scanTask(s scanner) (*Task, error) {
task := &Task{}
var (
uuidStr string
@@ -398,7 +369,7 @@ func GetTask(taskUUID uuid.UUID) (*Task, error) {
annotationsStr interface{}
)
err := db.QueryRow(query, taskUUID.String()).Scan(
err := s.Scan(
&task.ID,
&uuidStr,
&task.Status,
@@ -417,22 +388,18 @@ func GetTask(taskUUID uuid.UUID) (*Task, error) {
&parentUUIDStr,
&annotationsStr,
)
if err != nil {
return nil, fmt.Errorf("failed to get task: %w", err)
return nil, err
}
// Parse UUID
task.UUID, err = uuid.Parse(uuidStr)
if err != nil {
return nil, fmt.Errorf("failed to parse UUID: %w", err)
}
// Convert timestamps
task.Created = time.Unix(created, 0)
task.Modified = time.Unix(modified, 0)
// Convert nullable fields
task.Project = sqlToStringPtr(project)
task.Start = sqlToTime(start)
task.End = sqlToTime(end)
@@ -444,7 +411,6 @@ func GetTask(taskUUID uuid.UUID) (*Task, error) {
task.ParentUUID = sqlToUUIDPtr(parentUUIDStr)
task.Annotations = sqlToAnnotations(annotationsStr)
// Load tags
tags, err := task.GetTags()
if err != nil {
return nil, fmt.Errorf("failed to load tags: %w", err)
@@ -454,6 +420,29 @@ func GetTask(taskUUID uuid.UUID) (*Task, error) {
return task, nil
}
// GetTask retrieves a task by UUID
func GetTask(taskUUID uuid.UUID) (*Task, error) {
db := GetDB()
if db == nil {
return nil, fmt.Errorf("database not initialized")
}
query := `
SELECT id, uuid, status, description, project, priority,
created, modified, start, end, due, scheduled, wait, until_date,
recurrence_duration, parent_uuid, annotations
FROM tasks
WHERE uuid = ?
`
task, err := scanTask(db.QueryRow(query, taskUUID.String()))
if err != nil {
return nil, fmt.Errorf("failed to get task: %w", err)
}
return task, nil
}
// GetTasks retrieves all tasks with optional filtering
func GetTasks(filter *Filter) ([]*Task, error) {
db := GetDB()
@@ -489,76 +478,10 @@ func GetTasks(filter *Filter) ([]*Task, error) {
tasks := []*Task{}
for rows.Next() {
task := &Task{}
var (
uuidStr string
project interface{}
created int64
modified int64
start interface{}
end interface{}
due interface{}
scheduled interface{}
wait interface{}
until interface{}
recurDuration interface{}
parentUUIDStr interface{}
annotationsStr interface{}
)
err := rows.Scan(
&task.ID,
&uuidStr,
&task.Status,
&task.Description,
&project,
&task.Priority,
&created,
&modified,
&start,
&end,
&due,
&scheduled,
&wait,
&until,
&recurDuration,
&parentUUIDStr,
&annotationsStr,
)
task, err := scanTask(rows)
if err != nil {
return nil, fmt.Errorf("failed to scan task: %w", err)
}
// Parse UUID
task.UUID, err = uuid.Parse(uuidStr)
if err != nil {
return nil, fmt.Errorf("failed to parse UUID: %w", err)
}
// Convert timestamps
task.Created = time.Unix(created, 0)
task.Modified = time.Unix(modified, 0)
// Convert nullable fields
task.Project = sqlToStringPtr(project)
task.Start = sqlToTime(start)
task.End = sqlToTime(end)
task.Due = sqlToTime(due)
task.Scheduled = sqlToTime(scheduled)
task.Wait = sqlToTime(wait)
task.Until = sqlToTime(until)
task.RecurrenceDuration = sqlToDuration(recurDuration)
task.ParentUUID = sqlToUUIDPtr(parentUUIDStr)
task.Annotations = sqlToAnnotations(annotationsStr)
// Load tags
tags, err := task.GetTags()
if err != nil {
return nil, fmt.Errorf("failed to load tags: %w", err)
}
task.Tags = tags
tasks = append(tasks, task)
}
@@ -813,6 +736,48 @@ func (t *Task) IsRecurringInstance() bool {
return t.ParentUUID != nil
}
// GetDeletedTasks retrieves soft-deleted tasks, optionally filtered by age.
// If olderThan is non-nil, only returns tasks deleted more than that duration ago.
func GetDeletedTasks(olderThan *time.Duration) ([]*Task, error) {
db := GetDB()
if db == nil {
return nil, fmt.Errorf("database not initialized")
}
query := `
SELECT id, uuid, status, description, project, priority,
created, modified, start, end, due, scheduled, wait, until_date,
recurrence_duration, parent_uuid, annotations
FROM tasks
WHERE status = ?`
args := []interface{}{byte(StatusDeleted)}
if olderThan != nil {
cutoff := timeNow().Add(-*olderThan).Unix()
query += ` AND end < ?`
args = append(args, cutoff)
}
query += ` ORDER BY end ASC`
rows, err := db.Query(query, args...)
if err != nil {
return nil, fmt.Errorf("failed to query deleted tasks: %w", err)
}
defer rows.Close()
var tasks []*Task
for rows.Next() {
task, err := scanTask(rows)
if err != nil {
return nil, fmt.Errorf("failed to scan task: %w", err)
}
tasks = append(tasks, task)
}
return tasks, nil
}
// PopulateUrgency computes and sets the Urgency field on the given tasks.
func PopulateUrgency(tasks ...*Task) {
cfg, _ := GetConfig()
+58 -17
View File
@@ -102,14 +102,20 @@ func (c *Client) PullChanges(since int64) ([]ChangeLogEntry, error) {
func (c *Client) PushChanges(tasks []*engine.Task) error {
// Convert tasks to JSON
var taskData []json.RawMessage
var marshalErrors []string
for _, task := range tasks {
data, err := json.Marshal(task)
if err != nil {
marshalErrors = append(marshalErrors, fmt.Sprintf("task %s: %v", task.UUID, err))
continue
}
taskData = append(taskData, data)
}
if len(taskData) == 0 && len(marshalErrors) > 0 {
return fmt.Errorf("all tasks failed to marshal: %s", strings.Join(marshalErrors, "; "))
}
reqBody := map[string]interface{}{
"tasks": taskData,
"client_id": c.clientID,
@@ -139,6 +145,11 @@ func (c *Client) PushChanges(tasks []*engine.Task) error {
return fmt.Errorf("server returned %d: %s", resp.StatusCode, string(body))
}
if len(marshalErrors) > 0 {
return fmt.Errorf("pushed %d tasks but %d failed to marshal: %s",
len(taskData), len(marshalErrors), strings.Join(marshalErrors, "; "))
}
return nil
}
@@ -219,7 +230,7 @@ func (c *Client) Sync(strategy ConflictResolution, reporter ProgressReporter) (*
}
// Convert changes to tasks
remoteTasks, err := c.parseChanges(changes)
remoteTasks, err := c.ParseChanges(changes)
if err != nil {
if len(changes) > 0 {
reporter.CompletePhase()
@@ -283,6 +294,11 @@ func (c *Client) Sync(strategy ConflictResolution, reporter ProgressReporter) (*
continue
}
// Mark change_log entry as sync-originated to prevent feedback loop
if err := engine.MarkChangeLogAsSync(task.UUID.String()); err != nil {
result.Errors = append(result.Errors, fmt.Sprintf("failed to mark change as sync for %s: %v", task.UUID, err))
}
// Reload task to ensure we have the database ID
savedTask, err := engine.GetTask(task.UUID)
if err != nil {
@@ -347,18 +363,7 @@ func (c *Client) Sync(strategy ConflictResolution, reporter ProgressReporter) (*
// getLastSyncTime retrieves the last sync timestamp from database
func (c *Client) getLastSyncTime() int64 {
db := engine.GetDB()
if db == nil {
return 0
}
var lastSync int64
err := db.QueryRow("SELECT last_sync FROM sync_state WHERE client_id = ?", c.clientID).Scan(&lastSync)
if err != nil {
return 0
}
return lastSync
return GetLastSyncTime(c.clientID)
}
// updateLastSyncTime updates the last sync timestamp
@@ -376,6 +381,27 @@ func (c *Client) updateLastSyncTime(timestamp int64) {
// getLocalChanges retrieves local changes since a timestamp
func (c *Client) getLocalChanges(since int64) ([]*engine.Task, error) {
return GetLocalChanges(since)
}
// GetLastSyncTime retrieves the last sync timestamp for a client ID from the database.
func GetLastSyncTime(clientID string) int64 {
db := engine.GetDB()
if db == nil {
return 0
}
var lastSync int64
err := db.QueryRow("SELECT last_sync FROM sync_state WHERE client_id = ?", clientID).Scan(&lastSync)
if err != nil {
return 0
}
return lastSync
}
// GetLocalChanges retrieves local (non-sync-originated) changes since a timestamp.
func GetLocalChanges(since int64) ([]*engine.Task, error) {
db := engine.GetDB()
if db == nil {
return nil, fmt.Errorf("database not initialized")
@@ -384,7 +410,7 @@ func (c *Client) getLocalChanges(since int64) ([]*engine.Task, error) {
rows, err := db.Query(`
SELECT DISTINCT task_uuid
FROM change_log
WHERE changed_at > ?
WHERE changed_at > ? AND source = 'local'
ORDER BY changed_at ASC
`, since)
if err != nil {
@@ -415,8 +441,8 @@ func (c *Client) getLocalChanges(since int64) ([]*engine.Task, error) {
return tasks, nil
}
// parseChanges converts change log entries to tasks
func (c *Client) parseChanges(changes []ChangeLogEntry) ([]*engine.Task, error) {
// ParseChanges converts change log entries to tasks
func (c *Client) ParseChanges(changes []ChangeLogEntry) ([]*engine.Task, error) {
// Sort changes by timestamp (primary) and ID (secondary) to ensure correct order
// This handles same-second updates (e.g., CREATE followed by UPDATE with tags)
sort.Slice(changes, func(i, j int) bool {
@@ -666,16 +692,31 @@ func parseTagsFromChangeLog(s string) []string {
// pushQueuedChanges sends queued changes to server
func (c *Client) pushQueuedChanges(changes []QueuedChange) error {
var tasks []*engine.Task
var unmarshalErrors []string
for _, change := range changes {
var task engine.Task
if err := json.Unmarshal(change.Data, &task); err != nil {
unmarshalErrors = append(unmarshalErrors, fmt.Sprintf("queued change: %v", err))
continue
}
tasks = append(tasks, &task)
}
return c.PushChanges(tasks)
if len(tasks) == 0 && len(unmarshalErrors) > 0 {
return fmt.Errorf("all queued changes failed to unmarshal: %s", strings.Join(unmarshalErrors, "; "))
}
if err := c.PushChanges(tasks); err != nil {
return err
}
if len(unmarshalErrors) > 0 {
return fmt.Errorf("pushed %d tasks but %d queued changes failed to unmarshal: %s",
len(tasks), len(unmarshalErrors), strings.Join(unmarshalErrors, "; "))
}
return nil
}
// SyncResult represents the result of a sync operation
+6 -7
View File
@@ -57,7 +57,11 @@ func MergeTasks(local, remote []*engine.Task, strategy ConflictResolution) ([]*e
if DetectConflict(task, remoteTask) {
conflicts++
winner := resolveConflict(task, remoteTask, strategy)
logConflict(task, remoteTask, winner)
winnerLabel := "local"
if winner == remoteTask {
winnerLabel = "remote"
}
logConflict(task, remoteTask, winnerLabel)
result = append(result, winner)
} else {
// No conflict - use either (same content)
@@ -110,17 +114,12 @@ func resolveConflict(local, remote *engine.Task, strategy ConflictResolution) *e
}
// logConflict writes conflict information to log file
func logConflict(local, remote *engine.Task, winner *engine.Task) {
func logConflict(local, remote *engine.Task, winnerLabel string) {
logPath, err := engine.GetSyncConflictLogPath()
if err != nil {
return
}
winnerLabel := "local"
if winner.UUID == remote.UUID && winner.Modified.Equal(remote.Modified) {
winnerLabel = "remote"
}
entry := fmt.Sprintf(
"[%s] Conflict on task %s\n"+
" Local: modified %s - %s\n"+
@@ -402,6 +402,12 @@
</span>
{/if}
</div>
{:else}
<!-- svelte-ignore a11y-click-events-have-key-events a11y-no-static-element-interactions -->
<div class="field-row editable" on:click={() => startEdit('scheduled')}>
<span class="field-label">Scheduled</span>
<span class="field-value clickable">Set...</span>
</div>
{/if}
<!-- Wait -->
@@ -425,6 +431,12 @@
</span>
{/if}
</div>
{:else}
<!-- svelte-ignore a11y-click-events-have-key-events a11y-no-static-element-interactions -->
<div class="field-row editable" on:click={() => startEdit('wait')}>
<span class="field-label">Wait</span>
<span class="field-value clickable">Set...</span>
</div>
{/if}
<!-- Until -->
@@ -448,6 +460,12 @@
</span>
{/if}
</div>
{:else}
<!-- svelte-ignore a11y-click-events-have-key-events a11y-no-static-element-interactions -->
<div class="field-row editable" on:click={() => startEdit('until')}>
<span class="field-label">Until</span>
<span class="field-value clickable">Set...</span>
</div>
{/if}
<!-- Active since -->
+5 -4
View File
@@ -37,6 +37,11 @@
// Subscribe to store
const unsubscribe = tasksStore.subscribe(value => {
tasks = value;
// Keep selectedTask in sync with store changes
if (selectedTask) {
const updated = value.find(t => t.uuid === selectedTask.uuid);
if (updated) selectedTask = updated;
}
});
onMount(() => {
@@ -197,10 +202,6 @@
async function handleUpdate(uuid, updates) {
try {
await tasksStore.updateTask(uuid, updates);
// Keep selectedTask fresh
if (selectedTask?.uuid === uuid) {
selectedTask = { ...selectedTask, ...updates };
}
} catch (error) {
console.error('Failed to update task:', error);
}