Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions internal/cli/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ type indexNote struct {
// nt index --json # structured
// nt index --tag auth # scope to a tag (AND, repeatable)
// nt index --folder ref # scope to a folder
// nt index --project foo # scope to a project (hard filter, unlike `recall --project`)
func cmdIndex(args []string) int {
fs := flag.NewFlagSet("index", flag.ContinueOnError)
folder := fs.String("folder", "", `only notes under this folder, e.g. ref ("." = root notes)`)
project := fs.String("project", "", `only notes/tasks in this project — a hard filter on the note's "project:" frontmatter and a task's +project tag (unlike "recall --project", which only ranks, never excludes)`)
asJSON := fs.Bool("json", false, "machine-readable output")
noTasks := fs.Bool("no-tasks", false, "omit the active-task section")
all := fs.Bool("all", false, "full catalog: every note stub, no tiering (large stores tier by default)")
Expand Down Expand Up @@ -113,6 +115,9 @@ func cmdIndex(args []string) int {
if !match {
continue
}
if *project != "" && n.Project() != *project {
continue
}
if since != "" && n.ChangedDate() < since {
continue // "what's changed since T" — skip anything older
}
Expand All @@ -136,7 +141,7 @@ func cmdIndex(args []string) int {
// recent stubs in full, the long tail as per-folder counts. Any explicit
// scope (--all/--tag/--folder/--updated-since) means the caller is already
// narrowing — show every match, exactly as before.
scoped := *all || prefix != "" || len(tags) > 0 || since != ""
scoped := *all || prefix != "" || len(tags) > 0 || since != "" || *project != ""
tiers := note.Tiers{Recent: filtered}
if !scoped {
tiers = note.TierIndex(filtered, time.Now())
Expand Down Expand Up @@ -206,6 +211,9 @@ func cmdIndex(args []string) int {
break
}
}
if keep && *project != "" && !contains(t.Projects(), *project) {
keep = false
}
if !keep {
continue
}
Expand Down Expand Up @@ -374,8 +382,29 @@ func cmdIndex(args []string) int {
fmt.Printf("- [x] %s `%s`\n", strings.TrimSpace(t.Text), shortID(t.ID()))
}
}
if len(stubs) == 0 && len(active) == 0 && len(recent) == 0 {
fmt.Println("index is empty" + freshHint(e))
// The empty-state line must be honest about BOTH halves of the catalog —
// agents reading only this line, not the header comment, mistook a
// tasks-only "add your first task" nudge for proof the store had no
// notes at all. --no-tasks means the caller excluded tasks on purpose,
// so this stays silent on task counts rather than misreport a filter as
// the store's real state.
notesEmpty := len(stubs) == 0 && len(pinned) == 0
tasksEmpty := len(active) == 0 && len(blockedTasks) == 0 && len(recent) == 0
switch {
case *noTasks:
if notesEmpty {
fmt.Println("no notes — add one: nt note \"title\"")
}
case notesEmpty && tasksEmpty:
msg := "index is empty — 0 notes, 0 tasks"
if h := freshHint(e); h != "" {
msg += "\n add a note: nt note \"my first note\"" + h
}
fmt.Println(msg)
case notesEmpty:
fmt.Println("no notes — add one: nt note \"title\"")
case tasksEmpty:
fmt.Println("no active tasks — add one: nt add \"title\"")
}
return 0
}
Expand Down
86 changes: 86 additions & 0 deletions internal/cli/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,92 @@ func TestDoctorHealthyStoreWithGoodLinks(t *testing.T) {
}
}

// The empty-state line must say plainly that BOTH the note and task halves
// are empty, not just nudge toward `nt add` — agents reading only this line
// mistook the old tasks-only copy for proof notes lived somewhere else.
func TestIndexEmptyStoreMentionsBothNotesAndTasks(t *testing.T) {
t.Setenv("NT_DIR", t.TempDir())

out := captureRun(t, "index")
if !strings.Contains(out, "0 notes") || !strings.Contains(out, "0 tasks") {
t.Errorf("empty index should state both halves are empty:\n%s", out)
}
if !strings.Contains(out, "nt note") {
t.Errorf("empty index should point at the note entry point too:\n%s", out)
}
}

// 0 notes but N tasks is a partially-empty case that must still say "no
// notes" — the old code printed nothing about notes at all, so a reader of
// only the body (not the header comment) couldn't tell "no notes exist"
// from "index doesn't show notes here".
func TestIndexNoNotesButTasksSaysSo(t *testing.T) {
t.Setenv("NT_DIR", t.TempDir())
captureRun(t, "add", "wire refresh endpoint")

out := captureRun(t, "index")
if !strings.Contains(out, "no notes") {
t.Errorf("0-notes/N-tasks index should call out the empty note half:\n%s", out)
}
}

// N notes but 0 tasks is the mirror case — must say "no active tasks", not
// silently omit the tasks section with no explanation.
func TestIndexNoTasksButNotesSaysSo(t *testing.T) {
t.Setenv("NT_DIR", t.TempDir())
captureRun(t, "note", "JWT design", "--folder", "ref", "--body", "x")

out := captureRun(t, "index")
if !strings.Contains(out, "no active tasks") {
t.Errorf("N-notes/0-tasks index should call out the empty task half:\n%s", out)
}
}

// --no-tasks means the caller deliberately excluded tasks, so an otherwise
// empty note catalog must not claim "0 tasks" — that would misreport a
// filter as the store's real state.
func TestIndexEmptyStoreWithNoTasksFlagStaysSilentOnTaskCount(t *testing.T) {
t.Setenv("NT_DIR", t.TempDir())
captureRun(t, "add", "a task that exists")

out := captureRun(t, "index", "--no-tasks")
if strings.Contains(out, "0 tasks") || strings.Contains(out, "1 task") {
t.Errorf("--no-tasks empty-note-catalog message should not claim a task count:\n%s", out)
}
if !strings.Contains(out, "no notes") {
t.Errorf("--no-tasks should still report the empty note half:\n%s", out)
}
}

// nt index --project filters notes by `project:` frontmatter and tasks by
// their +project tag — a hard filter, unlike `recall --project`'s soft
// ranking preference (recall never excludes; index does).
func TestIndexProjectFiltersNotesAndTasks(t *testing.T) {
t.Setenv("NT_DIR", t.TempDir())
captureRun(t, "note", "wtcockpit note", "--folder", "ref", "--project", "wtcockpit", "--body", "x")
captureRun(t, "note", "roost note", "--folder", "ref", "--project", "roost", "--body", "y")
captureRun(t, "add", "wtcockpit task", "--project", "wtcockpit")
captureRun(t, "add", "roost task", "--project", "roost")

out := captureRun(t, "index", "--project", "wtcockpit")
if !strings.Contains(out, "wtcockpit note") || strings.Contains(out, "roost note") {
t.Errorf("--project should scope notes to the matching project:\n%s", out)
}
if !strings.Contains(out, "wtcockpit task") || strings.Contains(out, "roost task") {
t.Errorf("--project should scope tasks to the matching project:\n%s", out)
}
}

// nt index --project foo used to fail outright: "flag provided but not
// defined: -project". The flag must parse and produce a clean exit.
func TestIndexProjectFlagIsRecognized(t *testing.T) {
t.Setenv("NT_DIR", t.TempDir())
out, code := runWithStdout("index", "--project", "anything")
if code != 0 {
t.Fatalf("index --project should be a recognized flag, got exit %d:\n%s", code, out)
}
}

// nt index --limit caps the catalog and flags truncation.
func TestIndexLimitTruncates(t *testing.T) {
t.Setenv("NT_DIR", t.TempDir())
Expand Down
Loading