moi-computer 0.6.0 → 0.6.1-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/server/control.ts CHANGED
@@ -6,7 +6,7 @@ import { isParamsRecord } from '@/lib/workspace-tabs'
6
6
 
7
7
  import { clearAppletLog, getAppletLog, getAppletLogCount } from './applet-log'
8
8
  import { serializeWorkspaceBundle } from './bundle-queue'
9
- import { CONTROL_PORT } from './constants'
9
+ import { CONTROL_PORT, PORT } from './constants'
10
10
  import { applyEnvChanged } from './env-apply'
11
11
  import { callFunctionEphemeral, parseFunctionPath } from './functions'
12
12
  import { processIcon } from './icon'
@@ -28,6 +28,7 @@ import {
28
28
  setBuilder
29
29
  } from './view-builders'
30
30
  import { getWorkspaceConfig, setWorkspaceConfig } from './workspace-config'
31
+ import { VERSION } from './version'
31
32
 
32
33
  type ControlSocket = { send(data: string): void }
33
34
 
@@ -86,6 +87,22 @@ export const control = Bun.serve({
86
87
  return
87
88
  }
88
89
 
90
+ // Identity of the running server, for the CLI (`moi status`, `moi
91
+ // update`, `moi service`). VERSION is snapshotted at boot, so this
92
+ // reports the code actually running even after the install tree on
93
+ // disk was replaced by an update.
94
+ if (data.type === 'server:info') {
95
+ ws.send(
96
+ JSON.stringify({
97
+ version: VERSION,
98
+ pid: process.pid,
99
+ port: PORT,
100
+ service: !!process.env.MOI_SERVICE
101
+ })
102
+ )
103
+ return
104
+ }
105
+
89
106
  if (data.type === 'bundle') {
90
107
  // Resolve to the real workspace root (works from `.moi/` or any
91
108
  // subdir; errors when outside every registered workspace) so a bundle
@@ -21,6 +21,7 @@ import {
21
21
  import { appendAttachmentNote, attachmentOnlyPlaceholder } from '@/lib/attachment-note'
22
22
  import { buildSessionTitleSource } from '../session-title'
23
23
  import { ClaudeAdapter } from './adapter'
24
+ import { claudeSessionExists } from './sessions'
24
25
  import { generateClaudeSessionTitle, renameClaudeSessionIfUnchanged } from './session-title'
25
26
  import type { Part } from '@/lib/format'
26
27
  import type { SessionActivity } from '@/lib/types'
@@ -624,7 +625,9 @@ function createLiveSession(input: {
624
625
  permissionMode: 'bypassPermissions',
625
626
  allowDangerouslySkipPermissions: true,
626
627
  settingSources: ['user', 'project'],
627
- env: { ...process.env, ...input.workspaceEnv, CLAUDECODE: undefined },
628
+ // MOI_AGENT marks every shell this session spawns as agent-driven (the
629
+ // moi CLI reads it — see agent-caller.ts), surviving the CLAUDECODE strip.
630
+ env: { ...process.env, ...input.workspaceEnv, CLAUDECODE: undefined, MOI_AGENT: '1' },
628
631
  stderr: (data: string) => console.error('[SDK stderr]', data)
629
632
  }
630
633
  if (!input.isNew) options.resume = input.sessionId
@@ -691,12 +694,6 @@ export async function sendCCMessage(input: {
691
694
  ? resolveUploads(input.workspaceId, input.attachments)
692
695
  : []
693
696
  if (!input.content && uploads.length === 0) return
694
- const sessionTitleSource = input.isNew
695
- ? buildSessionTitleSource(
696
- input.content,
697
- uploads.map(upload => upload.filename)
698
- )
699
- : undefined
700
697
  const { content: userContent, parts } = buildUserMessage(input.content, uploads)
701
698
  // The envelope goes in as its OWN leading text block, never merged into the
702
699
  // user's string: the SDK's first-prompt extraction (session titles,
@@ -758,18 +755,37 @@ export async function sendCCMessage(input: {
758
755
  }
759
756
  }
760
757
  if (!s) {
758
+ // The agent only sees secrets scoped to the 'agent' sink (plus .env).
759
+ // Resolved before the probe below so both see the same env.
760
+ const workspaceEnv = await resolveWorkspaceEnv(input.workspacePath)
761
+ // Resuming a session with no file on disk hard-fails the CLI with "No
762
+ // conversation found" — and a dangling id can be selected forever (a
763
+ // brand-new chat whose first turn died before init persisted anything,
764
+ // or session files cleaned up externally), wedging the chat. Start fresh
765
+ // instead: the init rename then migrates this id to the real one
766
+ // everywhere (selected session, session config, view builders, client).
767
+ const isNew =
768
+ input.isNew ||
769
+ !(await claudeSessionExists(input.sessionId, input.workspacePath, workspaceEnv))
770
+ if (isNew !== input.isNew) {
771
+ debug(`cc resume-missing ws=${input.workspaceId} session=${input.sessionId} — starting fresh`)
772
+ }
761
773
  s = createLiveSession({
762
774
  workspaceId: input.workspaceId,
763
775
  workspacePath: input.workspacePath,
764
776
  sessionId: input.sessionId,
765
- isNew: input.isNew,
777
+ isNew,
766
778
  model: input.model,
767
779
  effort: input.effort,
768
780
  fastMode: input.fastMode,
769
781
  stream: wantStream,
770
- sessionTitleSource,
771
- // The agent only sees secrets scoped to the 'agent' sink (plus .env).
772
- workspaceEnv: await resolveWorkspaceEnv(input.workspacePath)
782
+ sessionTitleSource: isNew
783
+ ? buildSessionTitleSource(
784
+ input.content,
785
+ uploads.map(upload => upload.filename)
786
+ )
787
+ : undefined,
788
+ workspaceEnv
773
789
  })
774
790
  }
775
791
  // Streaming-input mode does NOT echo the pushed user message back in the
@@ -1,6 +1,11 @@
1
1
  // Session discovery + history replay for Claude Code workspaces, backed by
2
2
  // the Agent SDK's persisted `.jsonl` session files.
3
- import { getSessionMessages, listSessions, tagSession } from '@anthropic-ai/claude-agent-sdk'
3
+ import {
4
+ getSessionInfo,
5
+ getSessionMessages,
6
+ listSessions,
7
+ tagSession
8
+ } from '@anthropic-ai/claude-agent-sdk'
4
9
  import type { SDKSessionInfo } from '@anthropic-ai/claude-agent-sdk'
5
10
 
6
11
  import {
@@ -91,6 +96,29 @@ export async function getSessionWorkspacePreview(
91
96
  }
92
97
  }
93
98
 
99
+ // Whether the session's file exists on disk in this workspace's project dir —
100
+ // i.e. whether `resume` can possibly succeed. Reads only that session's file
101
+ // (unlike listSessions). Fails open — a probe that can't answer reliably
102
+ // counts as existing, so the resume is still attempted instead of silently
103
+ // forking a new chat:
104
+ // - a transient fs error, and
105
+ // - a workspace env that redirects CLAUDE_CONFIG_DIR: the CLI subprocess gets
106
+ // workspaceEnv on top of process.env, so its session store is somewhere
107
+ // this process (and the SDK's ambient-env resolution) can't see.
108
+ export async function claudeSessionExists(
109
+ sessionId: string,
110
+ workspacePath: string,
111
+ workspaceEnv: Record<string, string> = {}
112
+ ): Promise<boolean> {
113
+ const override = workspaceEnv.CLAUDE_CONFIG_DIR
114
+ if (override && override !== process.env.CLAUDE_CONFIG_DIR) return true
115
+ try {
116
+ return (await getSessionInfo(sessionId, { dir: workspacePath })) !== undefined
117
+ } catch {
118
+ return true
119
+ }
120
+ }
121
+
94
122
  export async function archiveClaudeSession(
95
123
  sessionId: string,
96
124
  workspacePath: string
@@ -97,7 +97,9 @@ async function startClient(workspacePath: string): Promise<ClientRecord> {
97
97
  stdin: 'pipe',
98
98
  stdout: 'pipe',
99
99
  stderr: 'pipe',
100
- env: { ...process.env, ...workspaceEnv }
100
+ // MOI_AGENT marks the agent's shells as agent-driven for the moi CLI
101
+ // (see agent-caller.ts).
102
+ env: { ...process.env, ...workspaceEnv, MOI_AGENT: '1' }
101
103
  })
102
104
 
103
105
  let alive = true