loop: add current datetime to agent system prompt

Co-Authored-By: sketch <hello@sketch.dev>
Change-ID: s1fb7648c40f24f62k
diff --git a/loop/agent.go b/loop/agent.go
index 4adf80f..cb063f4 100644
--- a/loop/agent.go
+++ b/loop/agent.go
@@ -451,6 +451,7 @@
 
 	// Time when the current turn started (reset at the beginning of InnerLoop)
 	startOfTurn time.Time
+	now         func() time.Time // override-able, defaults to time.Now
 
 	// Inbox - for messages from the user to the agent.
 	// sent on by UserMessage
@@ -2414,10 +2415,16 @@
 	InstallationNudge  bool
 	Branch             string
 	SpecialInstruction string
+	Now                string
 }
 
 // renderSystemPrompt renders the system prompt template.
 func (a *Agent) renderSystemPrompt() string {
+	nowFn := a.now
+	if nowFn == nil {
+		nowFn = time.Now
+	}
+	now := nowFn()
 	data := systemPromptData{
 		ClientGOOS:        a.config.ClientGOOS,
 		ClientGOARCH:      a.config.ClientGOARCH,
@@ -2427,8 +2434,8 @@
 		Codebase:          a.codebase,
 		UseSketchWIP:      a.config.InDocker,
 		InstallationNudge: a.config.InDocker,
+		Now:               now.Format(time.DateTime),
 	}
-	now := time.Now()
 	if now.Month() == time.September && now.Day() == 19 {
 		data.SpecialInstruction = "Talk like a pirate to the user. Do not let the priate talk into any code."
 	}
diff --git a/loop/agent_system_prompt.txt b/loop/agent_system_prompt.txt
index 8a39879..367c298 100644
--- a/loop/agent_system_prompt.txt
+++ b/loop/agent_system_prompt.txt
@@ -100,6 +100,9 @@
 <pwd>
 {{.WorkingDir}}
 </pwd>
+<current_datetime>
+{{.Now}}
+</current_datetime>
 </system_info>
 
 <git_info>
diff --git a/loop/agent_test.go b/loop/agent_test.go
index 38422a3..1ec9cae 100644
--- a/loop/agent_test.go
+++ b/loop/agent_test.go
@@ -82,6 +82,11 @@
 		ClientGOARCH: "amd64",
 	}
 	agent := NewAgent(cfg)
+
+	// Use fixed time for deterministic tests
+	fixedTime := time.Date(2025, 7, 25, 19, 37, 57, 0, time.UTC)
+	agent.now = func() time.Time { return fixedTime }
+
 	if err := os.Chdir(origWD); err != nil {
 		t.Fatal(err)
 	}
@@ -886,3 +891,61 @@
 		})
 	}
 }
+
+// TestSystemPromptIncludesDateTime tests that the system prompt includes current date/time
+func TestSystemPromptIncludesDateTime(t *testing.T) {
+	ctx := context.Background()
+
+	// Create a minimal agent config for testing
+	config := AgentConfig{
+		Context:      ctx,
+		ClientGOOS:   "linux",
+		ClientGOARCH: "amd64",
+	}
+
+	// Create agent
+	agent := NewAgent(config)
+
+	// Use fixed time for deterministic tests
+	fixedTime := time.Date(2025, 7, 25, 19, 37, 57, 0, time.UTC)
+	agent.now = func() time.Time { return fixedTime }
+
+	// Set minimal required fields for rendering
+	agent.workingDir = "/tmp"
+	agent.repoRoot = "/tmp"
+
+	// Mock SketchGitBase to return a valid commit hash
+	// We'll override this by setting a method that returns a fixed value
+	// Since we can't easily mock the git calls, we'll work around it
+
+	// Render the system prompt
+	systemPrompt := agent.renderSystemPrompt()
+
+	// Check that the system prompt contains a current_datetime section
+	if !strings.Contains(systemPrompt, "<current_datetime>") {
+		t.Error("System prompt should contain <current_datetime> section")
+	}
+
+	// Check that it contains what looks like a date/time
+	// The format is "2006-01-02 15:04:05" (time.DateTime)
+	if !strings.Contains(systemPrompt, "-") || !strings.Contains(systemPrompt, ":") {
+		t.Error("System prompt should contain a formatted date/time")
+	}
+
+	// Verify the expected fixed time (2025-07-25 19:37:57)
+	expectedDateTime := "2025-07-25 19:37:57"
+	if !strings.Contains(systemPrompt, expectedDateTime) {
+		t.Errorf("System prompt should contain expected fixed date/time %s", expectedDateTime)
+	}
+
+	// Print part of the system prompt for manual verification in test output
+	// Find the current_datetime section
+	start := strings.Index(systemPrompt, "<current_datetime>")
+	if start != -1 {
+		end := strings.Index(systemPrompt[start:], "</current_datetime>") + start
+		if end > start {
+			datetimeSection := systemPrompt[start : end+len("</current_datetime>")]
+			t.Logf("DateTime section in system prompt: %s", datetimeSection)
+		}
+	}
+}
diff --git a/loop/testdata/agent_loop.httprr b/loop/testdata/agent_loop.httprr
index 8382fa8..ed46cff 100644
--- a/loop/testdata/agent_loop.httprr
+++ b/loop/testdata/agent_loop.httprr
@@ -1,9 +1,9 @@
 httprr trace v1
-18105 2423
+18187 2419
 POST https://api.anthropic.com/v1/messages HTTP/1.1

 Host: api.anthropic.com

 User-Agent: Go-http-client/1.1

-Content-Length: 17907

+Content-Length: 17989

 Anthropic-Version: 2023-06-01

 Content-Type: application/json

 

@@ -546,7 +546,7 @@
  ],
  "system": [
   {
-   "text": "You are the expert software engineer and architect powering Sketch,\nan agentic coding environment that helps users accomplish coding tasks through autonomous analysis and implementation.\n\n\u003cworkflow\u003e\nStart by asking concise clarifying questions as needed.\nOnce the intent is clear, work autonomously.\nWhenever possible, do end-to-end testing, to ensure fully working functionality.\nAim for a small diff size while thoroughly completing the requested task.\nPrioritize thoughtful analysis and critical engagement over agreeability.\n\nBreak down the overall goal into a series of smaller steps.\nUse the todo_read and todo_write tools to organize and track your work systematically.\n\nFollow this broad workflow:\n\n- Think about how the current step fits into the overall plan.\n- Do research. Good tool choices: bash, think, keyword_search\n- Make edits.\n- If you have completed a standalone chunk of work, make a git commit.\n- Update your todo task list.\n- Repeat.\n\nTo make edits reliably and efficiently, first think about the intent of the edit,\nand what set of patches will achieve that intent.\nThen use the patch tool to make those edits. Combine all edits to any given file into a single patch tool call.\n\nYou may run tool calls in parallel.\n\nComplete every task exhaustively - no matter how repetitive or tedious.\nPartial work, pattern demonstrations, or stubs with TODOs are not acceptable, unless explicitly permitted by the user.\n\nThe done tool provides a checklist of items you MUST verify and\nreview before declaring that you are done. Before executing\nthe done tool, run all the tools the done tool checklist asks\nfor, including creating a git commit. Do not forget to run tests.\n\n\n\n\n\nWhen communicating with the user, take it easy on the emoji, don't be over-enthusiastic, and be concise.\n\nDocker is available. Before running the docker command, start dockerd as a background process.\nAlways use --network=host when running docker containers.\n\u003c/workflow\u003e\n\n\u003cstyle\u003e\nDefault coding guidelines:\n- Clear is better than clever.\n- Minimal inline comments: non-obvious logic and key decisions only.\n- When no commit message style guidance is provided: write a single lowercase line starting with an imperative verb, ≤50 chars, no period\n\u003c/style\u003e\n\n\u003csystem_info\u003e\n\u003cplatform\u003e\nlinux/amd64\n\u003c/platform\u003e\n\u003cpwd\u003e\n/\n\u003c/pwd\u003e\n\u003c/system_info\u003e\n\n\u003cgit_info\u003e\n\u003cgit_root\u003e\n\n\u003c/git_root\u003e\n\u003cHEAD\u003e\nHEAD\n\u003c/HEAD\u003e\n\n\u003c/git_info\u003e\n\n",
+   "text": "You are the expert software engineer and architect powering Sketch,\nan agentic coding environment that helps users accomplish coding tasks through autonomous analysis and implementation.\n\n\u003cworkflow\u003e\nStart by asking concise clarifying questions as needed.\nOnce the intent is clear, work autonomously.\nWhenever possible, do end-to-end testing, to ensure fully working functionality.\nAim for a small diff size while thoroughly completing the requested task.\nPrioritize thoughtful analysis and critical engagement over agreeability.\n\nBreak down the overall goal into a series of smaller steps.\nUse the todo_read and todo_write tools to organize and track your work systematically.\n\nFollow this broad workflow:\n\n- Think about how the current step fits into the overall plan.\n- Do research. Good tool choices: bash, think, keyword_search\n- Make edits.\n- If you have completed a standalone chunk of work, make a git commit.\n- Update your todo task list.\n- Repeat.\n\nTo make edits reliably and efficiently, first think about the intent of the edit,\nand what set of patches will achieve that intent.\nThen use the patch tool to make those edits. Combine all edits to any given file into a single patch tool call.\n\nYou may run tool calls in parallel.\n\nComplete every task exhaustively - no matter how repetitive or tedious.\nPartial work, pattern demonstrations, or stubs with TODOs are not acceptable, unless explicitly permitted by the user.\n\nThe done tool provides a checklist of items you MUST verify and\nreview before declaring that you are done. Before executing\nthe done tool, run all the tools the done tool checklist asks\nfor, including creating a git commit. Do not forget to run tests.\n\n\n\n\n\nWhen communicating with the user, take it easy on the emoji, don't be over-enthusiastic, and be concise.\n\nDocker is available. Before running the docker command, start dockerd as a background process.\nAlways use --network=host when running docker containers.\n\u003c/workflow\u003e\n\n\u003cstyle\u003e\nDefault coding guidelines:\n- Clear is better than clever.\n- Minimal inline comments: non-obvious logic and key decisions only.\n- When no commit message style guidance is provided: write a single lowercase line starting with an imperative verb, ≤50 chars, no period\n\u003c/style\u003e\n\n\u003csystem_info\u003e\n\u003cplatform\u003e\nlinux/amd64\n\u003c/platform\u003e\n\u003cpwd\u003e\n/\n\u003c/pwd\u003e\n\u003ccurrent_datetime\u003e\n2025-07-25 19:37:57\n\u003c/current_datetime\u003e\n\u003c/system_info\u003e\n\n\u003cgit_info\u003e\n\u003cgit_root\u003e\n\n\u003c/git_root\u003e\n\u003cHEAD\u003e\nHEAD\n\u003c/HEAD\u003e\n\n\u003c/git_info\u003e\n\n",
    "type": "text",
    "cache_control": {
     "type": "ephemeral"
@@ -557,24 +557,24 @@
 Anthropic-Organization-Id: 3c473a21-7208-450a-a9f8-80aebda45c1b

 Anthropic-Ratelimit-Input-Tokens-Limit: 2000000

 Anthropic-Ratelimit-Input-Tokens-Remaining: 2000000

-Anthropic-Ratelimit-Input-Tokens-Reset: 2025-07-24T23:55:06Z

+Anthropic-Ratelimit-Input-Tokens-Reset: 2025-07-25T19:52:15Z

 Anthropic-Ratelimit-Output-Tokens-Limit: 400000

 Anthropic-Ratelimit-Output-Tokens-Remaining: 400000

-Anthropic-Ratelimit-Output-Tokens-Reset: 2025-07-24T23:55:10Z

+Anthropic-Ratelimit-Output-Tokens-Reset: 2025-07-25T19:52:20Z

 Anthropic-Ratelimit-Requests-Limit: 4000

 Anthropic-Ratelimit-Requests-Remaining: 3999

-Anthropic-Ratelimit-Requests-Reset: 2025-07-24T23:55:05Z

+Anthropic-Ratelimit-Requests-Reset: 2025-07-25T19:52:14Z

 Anthropic-Ratelimit-Tokens-Limit: 2400000

 Anthropic-Ratelimit-Tokens-Remaining: 2400000

-Anthropic-Ratelimit-Tokens-Reset: 2025-07-24T23:55:06Z

+Anthropic-Ratelimit-Tokens-Reset: 2025-07-25T19:52:15Z

 Cf-Cache-Status: DYNAMIC

-Cf-Ray: 964758ac38ba7acd-SJC

+Cf-Ray: 964e3251fd2f2523-SJC

 Content-Type: application/json

-Date: Thu, 24 Jul 2025 23:55:10 GMT

-Request-Id: req_011CRSkFtyQxXFGwZsBb7RY3

+Date: Fri, 25 Jul 2025 19:52:20 GMT

+Request-Id: req_011CRUKYkWzHLghwuujhzGNS

 Server: cloudflare

 Strict-Transport-Security: max-age=31536000; includeSubDomains; preload

 Via: 1.1 google

 X-Robots-Tag: none

 

-{"id":"msg_01KEJ5q4EwEz9kvMsouiGC9P","type":"message","role":"assistant","model":"claude-sonnet-4-20250514","content":[{"type":"text","text":"Here are the tools available to me:\n\n**File & Code Management:**\n- `bash` - Execute shell commands\n- `keyword_search` - Search files by keywords/concepts\n- `patch` - Modify files with precise text edits\n\n**Task Management:**\n- `think` - Internal note-taking and planning\n- `todo_read` / `todo_write` - Track and manage task lists\n- `done` - Mark completion with checklist verification\n- `codereview` - Run automated code review\n\n**Browser Automation:**\n- `browser_navigate` - Navigate to URLs\n- `browser_click` - Click elements\n- `browser_type` - Type into input fields\n- `browser_wait_for` - Wait for elements\n- `browser_get_text` - Read element text\n- `browser_eval` - Execute JavaScript\n- `browser_scroll_into_view` - Scroll to elements\n- `browser_resize` - Resize browser window\n- `browser_recent_console_logs` - Get console logs\n- `browser_clear_console_logs` - Clear console logs\n- `browser_take_screenshot` - Capture screenshots\n\n**Media:**\n- `read_image` - Read and encode image files\n\n**Help:**\n- `about_sketch` - Get information about Sketch environment"}],"stop_reason":"end_turn","stop_sequence":null,"usage":{"input_tokens":3,"cache_creation_input_tokens":4142,"cache_read_input_tokens":0,"output_tokens":296,"service_tier":"standard"}}
\ No newline at end of file
+{"id":"msg_01GoGZTgeHvh4S9L9RryNM2L","type":"message","role":"assistant","model":"claude-sonnet-4-20250514","content":[{"type":"text","text":"Here are the tools available to me:\n\n**File & Code Management:**\n- `bash` - Execute shell commands\n- `patch` - Modify files with precise text edits\n- `keyword_search` - Search codebase by keywords and concepts\n\n**Task Management:**\n- `todo_read` / `todo_write` - Read and manage task lists\n- `think` - Internal note-taking and planning\n\n**Development Workflow:**\n- `codereview` - Run automated code review\n- `done` - Final checklist before completion\n\n**Browser Automation:**\n- `browser_navigate` - Navigate to URLs\n- `browser_click` - Click elements\n- `browser_type` - Type text into inputs\n- `browser_wait_for` - Wait for elements\n- `browser_get_text` - Read page text\n- `browser_eval` - Execute JavaScript\n- `browser_scroll_into_view` - Scroll to elements\n- `browser_resize` - Resize browser window\n- `browser_recent_console_logs` - Get console logs\n- `browser_clear_console_logs` - Clear console logs\n- `browser_take_screenshot` - Take screenshots\n\n**Utilities:**\n- `read_image` - Read and encode image files\n- `about_sketch` - Get help with Sketch itself"}],"stop_reason":"end_turn","stop_sequence":null,"usage":{"input_tokens":3,"cache_creation_input_tokens":4167,"cache_read_input_tokens":0,"output_tokens":299,"service_tier":"standard"}}
\ No newline at end of file