diff --git a/docs/llm-design-spec-audit.md b/docs/llm-design-spec-audit.md index 10abf13..e5495a9 100644 --- a/docs/llm-design-spec-audit.md +++ b/docs/llm-design-spec-audit.md @@ -186,7 +186,12 @@ the *only* new section and stops there. The leverage is therefore not at capture time: - **Capture stays cheap.** The evidence supports fewer fields, not more. Median - opening prompt is 363 chars and that is fine. + opening prompt is 363 chars and that is fine. Implemented: a draft that is + nothing but a pasted link now selects the freeform `blank` template + automatically (`NoteTemplate.forDraft`), so filing a link costs one paste + instead of a seven-step stepper — a third of the freeform backlog is exactly + that. Typing any prose alongside the link restores the spec template, and an + explicit pick from the dropdown or wizard is never overridden. - **Per-repo constants belong in that repo's `CLAUDE.md`, written once** — never re-typed per note. `verify` exists for the exception, not the rule. - **The real cost centre is the turn-24 fork**, which no prompt could have diff --git a/lib/data/note_template.dart b/lib/data/note_template.dart index 7693ece..f9d7f7e 100644 --- a/lib/data/note_template.dart +++ b/lib/data/note_template.dart @@ -192,6 +192,28 @@ class NoteTemplate { /// The default template applied to new notes. static const NoteTemplate defaultTemplate = llmDesignSpec; + + /// The template a *draft* of [text] should be authored with. + /// + /// A bare link gets [blank]: a third of the freeform notes in the backlog are + /// nothing but a pasted URL, and walking a seven-step spec stepper to file a + /// link is pure friction. Everything else keeps [defaultTemplate]. + static NoteTemplate forDraft(String text) => + isBareLink(text) ? blank : defaultTemplate; +} + +/// Matches a single `http(s)://…` token. +final RegExp _linkPattern = RegExp(r'^https?://\S+$'); + +/// Whether [text] is nothing but one or more links. +/// +/// Deliberately strict: one word of context ("read this https://…") means the +/// note has prose in it and is no longer a bare link. +bool isBareLink(String text) { + final tokens = text.trim().split(RegExp(r'\s+')) + ..removeWhere((t) => t.isEmpty); + if (tokens.isEmpty) return false; + return tokens.every(_linkPattern.hasMatch); } /// Result of parsing stored text against a template. diff --git a/lib/ui/note_editor.dart b/lib/ui/note_editor.dart index 3909b32..40e3345 100644 --- a/lib/ui/note_editor.dart +++ b/lib/ui/note_editor.dart @@ -115,6 +115,10 @@ class _NoteEditorState extends State { /// One controller per structured section (keyed by section key). final Map _section = {}; + /// Set once the user picks a template themselves, which stops + /// [_retemplateDraft] from overriding that choice as they keep typing. + bool _templatePickedByUser = false; + /// Single field used for the freeform [NoteTemplate.blank] body and for raw /// mode of a structured template. final TextEditingController _body = TextEditingController(); @@ -236,6 +240,24 @@ class _NoteEditorState extends State { void _emit() => widget.onChanged(_currentText()); + /// Re-picks the draft's template from what has been typed so far, unless the + /// user has chosen one explicitly. + /// + /// Only ever runs while the raw body *is* the source, so there are no section + /// values to convert and the switch is lossless in both directions: paste a + /// link and Guided drops away, write a sentence under it and the spec + /// template comes back. + void _retemplateDraft() { + if (_templatePickedByUser || !_rawSource) return; + final next = NoteTemplate.forDraft(_body.text); + if (next.id == _template.id) return; + setState(() { + _template = next; + _currentStep = 0; + _mode = _resolveMode(_mode); + }); + } + void _switchTemplate(NoteTemplate next) { if (next.id == _template.id) return; final text = _currentText(); @@ -306,6 +328,8 @@ class _NoteEditorState extends State { /// is unchanged, which would skip flipping out of the wizard here. void _enterGuidedWithTemplate(NoteTemplate template) { final text = _currentText(); + // Choosing a template in the wizard is as explicit as using the dropdown. + _templatePickedByUser = true; setState(() { _template = template; _currentStep = 0; @@ -363,6 +387,7 @@ class _NoteEditorState extends State { ], onChanged: (id) { if (id == null) return; + _templatePickedByUser = true; _switchTemplate(NoteTemplate.all.firstWhere((t) => t.id == id)); }, ), @@ -538,7 +563,10 @@ class _NoteEditorState extends State { border: InputBorder.none, hintText: 'Write your idea…', ), - onChanged: (_) => _emit(), + onChanged: (_) { + _retemplateDraft(); + _emit(); + }, ), ), ); diff --git a/test/note_editor_test.dart b/test/note_editor_test.dart index b8fcdc4..1307567 100644 --- a/test/note_editor_test.dart +++ b/test/note_editor_test.dart @@ -101,6 +101,57 @@ void main() { expect(find.text('Next'), findsNothing); }); + testWidgets('pasting a bare link drops the draft to the blank template', ( + tester, + ) async { + await pumpEditor( + tester, + initialTemplate: spec, + initialMode: NoteEditorMode.raw, + ); + expect(find.text('LLM design spec'), findsOneWidget); + + await tester.enterText( + find.byType(TextField).first, + 'https://example.com/x', + ); + await tester.pump(); + + // Freeform now, so there is no seven-step stepper to walk for a link. + expect(find.text('Blank'), findsOneWidget); + expect(find.text('Guided'), findsNothing); + + // Adding prose puts the spec template back — lossless in both directions. + await tester.enterText( + find.byType(TextField).first, + 'https://example.com/x — port this to Dart', + ); + await tester.pump(); + expect(find.text('LLM design spec'), findsOneWidget); + expect(find.text('Guided'), findsOneWidget); + }); + + testWidgets('an explicit template choice survives further typing', ( + tester, + ) async { + await pumpEditor( + tester, + initialTemplate: spec, + initialMode: NoteEditorMode.raw, + ); + + await tester.tap(find.text('LLM design spec')); + await tester.pumpAndSettle(); + await tester.tap(find.text('Blank').last); + await tester.pumpAndSettle(); + expect(find.text('Blank'), findsOneWidget); + + // Prose would normally re-select the spec template; an explicit pick wins. + await tester.enterText(find.byType(TextField).first, 'build a thing'); + await tester.pump(); + expect(find.text('Blank'), findsOneWidget); + }); + testWidgets('switching to View renders the note as Markdown', (tester) async { await pumpEditor( tester, diff --git a/test/note_template_test.dart b/test/note_template_test.dart index 22cfa82..d518039 100644 --- a/test/note_template_test.dart +++ b/test/note_template_test.dart @@ -140,6 +140,29 @@ void main() { }); }); + group('bare-link drafts', () { + test('isBareLink accepts only link-only text', () { + expect(isBareLink('https://example.com/a'), isTrue); + expect(isBareLink(' http://example.com '), isTrue); + // Several pasted links are still just links. + expect(isBareLink('https://a.example\nhttps://b.example'), isTrue); + // One word of context means the note has prose in it. + expect(isBareLink('read this https://example.com'), isFalse); + expect(isBareLink('https://example.com — do the thing'), isFalse); + expect(isBareLink('just a title'), isFalse); + expect(isBareLink(''), isFalse); + expect(isBareLink(' \n '), isFalse); + // Not a URL scheme we recognise. + expect(isBareLink('ftp://example.com/x'), isFalse); + }); + + test('forDraft picks blank for a link and the spec otherwise', () { + expect(NoteTemplate.forDraft('https://example.com').id, 'blank'); + expect(NoteTemplate.forDraft('build a thing').id, 'llm-design-spec'); + expect(NoteTemplate.forDraft('').id, 'llm-design-spec'); + }); + }); + group('noteTitle', () { test('strips the leading # from a heading', () { expect(noteTitle('# My title\n\n## what\nx'), 'My title');