Changesets · Branches · Tags · Compare
| Changeset | f0ea2b85847cee64e00b5f8920de2e49333235ebdf3ce14c1f41d930f1a0958f |
|---|---|
| Author | Jakub Błach <jakubblach@gmail.com> · 2025-08-30 00:37 +02:00 |
| Committer | GitHub <noreply@github.com> · 2025-08-29 15:37 -07:00 |
| Parents | e5ba3fba5707 |
9 files changed
modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/activities/ContinuePluginStartupActivity.kt +1 −2
| @@ -266,8 +266,7 @@ | ||
| 266 | 266 | project.service<ContinuePluginDisposable>() |
| 267 | 267 | ) |
| 268 | 268 | |
| 269 | - val coreMessengerManager = CoreMessengerManager(project, ideProtocolClient, coroutineScope) | |
| 270 | - continuePluginService.coreMessengerManager = coreMessengerManager | |
| 269 | + continuePluginService.coreMessenger = CoreMessenger(project, ideProtocolClient, coroutineScope) | |
| 271 | 270 | } |
| 272 | 271 | } |
| 273 | 272 | } |
| No newline at the end of the file |
modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/CoreMessenger.kt +16 −19
| @@ -5,11 +5,9 @@ | ||
| 5 | 5 | import com.github.continuedev.continueintellijextension.`continue`.process.ContinueBinaryProcess |
| 6 | 6 | import com.github.continuedev.continueintellijextension.`continue`.process.ContinueProcessHandler |
| 7 | 7 | import com.github.continuedev.continueintellijextension.`continue`.process.ContinueSocketProcess |
| 8 | -import com.github.continuedev.continueintellijextension.services.ContinuePluginService | |
| 9 | 8 | import com.github.continuedev.continueintellijextension.utils.uuid |
| 10 | 9 | import com.google.gson.Gson |
| 11 | 10 | import com.google.gson.JsonSyntaxException |
| 12 | -import com.intellij.openapi.components.service | |
| 13 | 11 | import com.intellij.openapi.diagnostic.Logger |
| 14 | 12 | import com.intellij.openapi.project.Project |
| 15 | 13 | import kotlinx.coroutines.CoroutineScope |
| @@ -17,13 +15,14 @@ | ||
| 17 | 15 | class CoreMessenger( |
| 18 | 16 | private val project: Project, |
| 19 | 17 | private val ideProtocolClient: IdeProtocolClient, |
| 20 | - val coroutineScope: CoroutineScope, | |
| 21 | - private val onUnexpectedExit: () -> Unit | |
| 18 | + | |
| 19 | + // todo: this scope is public only to cancel the coroutines on dispose in ContinuePluginService | |
| 20 | + // todo: scope is created in ContinuePluginStartupActivity; refactor this/clean this up/encapsulate | |
| 21 | + val coroutineScope: CoroutineScope | |
| 22 | 22 | ) { |
| 23 | 23 | private val gson = Gson() |
| 24 | 24 | private val responseListeners = mutableMapOf<String, (Any?) -> Unit>() |
| 25 | - private var process = startContinueProcess() | |
| 26 | - private val log = Logger.getInstance(CoreMessenger::class.java.simpleName) | |
| 25 | + private val process = startContinueProcess() | |
| 27 | 26 | |
| 28 | 27 | fun request(messageType: String, data: Any?, messageId: String?, onResponse: (Any?) -> Unit) { |
| 29 | 28 | val id = messageId ?: uuid() |
| @@ -34,14 +33,15 @@ | ||
| 34 | 33 | |
| 35 | 34 | private fun startContinueProcess(): ContinueProcessHandler { |
| 36 | 35 | val isTcp = System.getenv("USE_TCP")?.toBoolean() ?: false |
| 37 | - val process = if (isTcp) | |
| 38 | - ContinueSocketProcess() | |
| 39 | - else | |
| 40 | - ContinueBinaryProcess(onUnexpectedExit) | |
| 41 | - return ContinueProcessHandler(coroutineScope, process, ::handleMessage) | |
| 36 | + return ContinueProcessHandler(coroutineScope, ::readProcessMessage) { | |
| 37 | + if (isTcp) | |
| 38 | + ContinueSocketProcess() | |
| 39 | + else | |
| 40 | + ContinueBinaryProcess() | |
| 41 | + } | |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | - private fun handleMessage(json: String) { | |
| 44 | + private fun readProcessMessage(json: String) { | |
| 45 | 45 | val responseMap = tryToParse(json) ?: return |
| 46 | 46 | val messageId = responseMap["messageId"].toString() |
| 47 | 47 | val messageType = responseMap["messageType"].toString() |
| @@ -76,19 +76,16 @@ | ||
| 76 | 76 | try { |
| 77 | 77 | gson.fromJson(json, Map::class.java) |
| 78 | 78 | } catch (_: JsonSyntaxException) { |
| 79 | - log.warn("Invalid message JSON: $json") // example: NODE_ENV undefined | |
| 79 | + LOG.warn("Invalid message JSON: $json") // example: NODE_ENV undefined | |
| 80 | 80 | null |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | fun restart() { |
| 84 | - log.warn("Restarting Continue process") | |
| 85 | 84 | responseListeners.clear() |
| 86 | - process.close() | |
| 87 | - process = startContinueProcess() | |
| 85 | + process.restart() | |
| 88 | 86 | } |
| 89 | 87 | |
| 90 | - fun close() { | |
| 91 | - log.warn("Closing Continue process") | |
| 92 | - process.close() | |
| 88 | + private companion object { | |
| 89 | + private val LOG = Logger.getInstance(CoreMessenger::class.java.simpleName) | |
| 93 | 90 | } |
| 94 | 91 | } |
| No newline at the end of the file |
deleted extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/CoreMessengerManager.kt +0 −33
| @@ -1,33 +0,0 @@ | ||
| 1 | -package com.github.continuedev.continueintellijextension.`continue` | |
| 2 | - | |
| 3 | -import com.github.continuedev.continueintellijextension.error.ContinuePostHogService | |
| 4 | -import com.intellij.openapi.components.service | |
| 5 | -import com.intellij.openapi.diagnostic.Logger | |
| 6 | -import com.intellij.openapi.project.Project | |
| 7 | -import kotlinx.coroutines.CoroutineScope | |
| 8 | -import kotlinx.coroutines.delay | |
| 9 | -import kotlinx.coroutines.launch | |
| 10 | -import kotlin.time.Duration.Companion.seconds | |
| 11 | - | |
| 12 | -class CoreMessengerManager( | |
| 13 | - project: Project, | |
| 14 | - ideProtocolClient: IdeProtocolClient, | |
| 15 | - private val coroutineScope: CoroutineScope | |
| 16 | -) { | |
| 17 | - val coreMessenger = CoreMessenger(project, ideProtocolClient, coroutineScope, ::onUnexpectedExit) | |
| 18 | - private var backoffIntervalSeconds = 1 | |
| 19 | - private val log = Logger.getInstance(CoreMessengerManager::class.java) | |
| 20 | - | |
| 21 | - private fun onUnexpectedExit() { | |
| 22 | - coroutineScope.launch { | |
| 23 | - try { | |
| 24 | - delay(backoffIntervalSeconds.seconds) | |
| 25 | - backoffIntervalSeconds *= 2 | |
| 26 | - log.warn("Continue process terminated externally, retrying in $backoffIntervalSeconds seconds") | |
| 27 | - coreMessenger.restart() | |
| 28 | - } catch (e: Exception) { | |
| 29 | - service<ContinuePostHogService>().capture("jetbrains_core_start_error", mapOf("error" to e)) | |
| 30 | - } | |
| 31 | - } | |
| 32 | - } | |
| 33 | -} |
added extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/process/BackoffCalculator.kt +25 −0
| @@ -0,0 +1,25 @@ | ||
| 1 | +package com.github.continuedev.continueintellijextension.`continue`.process | |
| 2 | + | |
| 3 | +import kotlin.time.Duration | |
| 4 | +import kotlin.time.Duration.Companion.seconds | |
| 5 | + | |
| 6 | +/** | |
| 7 | + * Starts at [initialDuration] and doubles on each call to [nextDuration] up to [maxDuration]. | |
| 8 | + */ | |
| 9 | +class BackoffCalculator( | |
| 10 | + private val initialDuration: Duration = 1.seconds, | |
| 11 | + private val maxDuration: Duration = 30.seconds | |
| 12 | +) { | |
| 13 | + private var currentTime: Duration = initialDuration | |
| 14 | + | |
| 15 | + init { | |
| 16 | + require(initialDuration > 0.seconds) | |
| 17 | + } | |
| 18 | + | |
| 19 | + fun nextDuration(): Duration { | |
| 20 | + val delay = currentTime | |
| 21 | + val next = currentTime * 2.0 | |
| 22 | + currentTime = if (next <= maxDuration) next else maxDuration | |
| 23 | + return delay | |
| 24 | + } | |
| 25 | +} |
modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/process/ContinueBinaryProcess.kt +11 −34
| @@ -1,12 +1,9 @@ | ||
| 1 | 1 | package com.github.continuedev.continueintellijextension.`continue`.process |
| 2 | 2 | |
| 3 | -import com.github.continuedev.continueintellijextension.error.ContinuePostHogService | |
| 4 | -import com.github.continuedev.continueintellijextension.error.ContinueSentryService | |
| 5 | 3 | import com.github.continuedev.continueintellijextension.proxy.ProxySettings |
| 6 | 4 | import com.github.continuedev.continueintellijextension.utils.OS |
| 7 | 5 | import com.github.continuedev.continueintellijextension.utils.getContinueBinaryPath |
| 8 | 6 | import com.github.continuedev.continueintellijextension.utils.getOS |
| 9 | -import com.intellij.openapi.components.service | |
| 10 | 7 | import kotlinx.coroutines.Dispatchers |
| 11 | 8 | import kotlinx.coroutines.runBlocking |
| 12 | 9 | import java.io.File |
| @@ -16,46 +13,26 @@ | ||
| 16 | 13 | import java.nio.file.Paths |
| 17 | 14 | import java.nio.file.attribute.PosixFilePermission |
| 18 | 15 | |
| 19 | -class ContinueBinaryProcess( | |
| 20 | - private val onUnexpectedExit: () -> Unit | |
| 21 | -) : ContinueProcess { | |
| 16 | +class ContinueBinaryProcess() : ContinueProcess { | |
| 22 | 17 | |
| 23 | - private val process = startBinaryProcess() | |
| 18 | + private val process = start() | |
| 24 | 19 | override val input: InputStream = process.inputStream |
| 25 | 20 | override val output: OutputStream = process.outputStream |
| 26 | 21 | |
| 27 | 22 | override fun close() = |
| 28 | 23 | process.destroy() |
| 29 | 24 | |
| 30 | - private fun startBinaryProcess(): Process { | |
| 31 | - val path = getContinueBinaryPath() | |
| 32 | - runBlocking(Dispatchers.IO) { | |
| 33 | - setPermissions() | |
| 34 | - } | |
| 35 | - | |
| 36 | - val builder = ProcessBuilder(path) | |
| 37 | - builder.environment() += ProxySettings.getSettings().toContinueEnvVars() | |
| 38 | - return builder | |
| 39 | - .directory(File(path).parentFile) | |
| 40 | - .start() | |
| 41 | - .apply { onExit().thenRun(onUnexpectedExit).thenRun(::reportErrorTelemetry) } | |
| 42 | - } | |
| 25 | + private companion object { | |
| 43 | 26 | |
| 44 | - private fun reportErrorTelemetry() { | |
| 45 | - var err = process.errorStream?.bufferedReader()?.readText()?.trim() | |
| 46 | - if (err != null) { | |
| 47 | - // There are often "⚡️Done in Xms" messages, and we want everything after the last one | |
| 48 | - val delimiter = "⚡ Done in" | |
| 49 | - val doneIndex = err.lastIndexOf(delimiter) | |
| 50 | - if (doneIndex != -1) { | |
| 51 | - err = err.substring(doneIndex + delimiter.length) | |
| 52 | - } | |
| 27 | + private fun start(): Process { | |
| 28 | + val path = getContinueBinaryPath() | |
| 29 | + setPermissions() | |
| 30 | + val builder = ProcessBuilder(path) | |
| 31 | + builder.environment() += ProxySettings.getSettings().toContinueEnvVars() | |
| 32 | + return builder | |
| 33 | + .directory(File(path).parentFile) | |
| 34 | + .start() | |
| 53 | 35 | } |
| 54 | - service<ContinueSentryService>().reportMessage("Core process exited with output: $err") | |
| 55 | - service<ContinuePostHogService>().capture("jetbrains_core_exit", mapOf("error" to err)) | |
| 56 | - } | |
| 57 | - | |
| 58 | - private companion object { | |
| 59 | 36 | |
| 60 | 37 | private fun setPermissions() { |
| 61 | 38 | val os = getOS() |
modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/process/ContinueProcessHandler.kt +42 −34
| @@ -1,49 +1,62 @@ | ||
| 1 | 1 | package com.github.continuedev.continueintellijextension.`continue`.process |
| 2 | 2 | |
| 3 | +import com.github.continuedev.continueintellijextension.error.ContinuePostHogService | |
| 3 | 4 | import com.github.continuedev.continueintellijextension.error.ContinueSentryService |
| 4 | 5 | import com.intellij.openapi.components.service |
| 5 | 6 | import com.intellij.openapi.diagnostic.Logger |
| 6 | 7 | import kotlinx.coroutines.* |
| 7 | 8 | import kotlinx.coroutines.channels.Channel |
| 8 | 9 | import java.io.BufferedReader |
| 9 | -import java.io.IOException | |
| 10 | 10 | import java.io.InputStreamReader |
| 11 | 11 | import java.io.OutputStreamWriter |
| 12 | 12 | |
| 13 | 13 | class ContinueProcessHandler( |
| 14 | - parentScope: CoroutineScope, | |
| 15 | - private val process: ContinueProcess, | |
| 16 | - handleMessage: (String) -> (Unit) | |
| 14 | + private val parentScope: CoroutineScope, | |
| 15 | + private val readMessage: (String) -> (Unit), | |
| 16 | + private val createProcess: () -> ContinueProcess | |
| 17 | 17 | ) { |
| 18 | - private val innerJob = Job() | |
| 19 | - private val scope = CoroutineScope(parentScope.coroutineContext + innerJob) | |
| 20 | 18 | private val pendingWrites = Channel<String>(Channel.UNLIMITED) |
| 21 | - private val writer = OutputStreamWriter(process.output) | |
| 22 | - private val reader = BufferedReader(InputStreamReader(process.input)) | |
| 23 | - private val log = Logger.getInstance(ContinueProcessHandler::class.java) | |
| 19 | + private val backoff = BackoffCalculator() | |
| 20 | + private var processScope: CoroutineScope? = null | |
| 21 | + private var process: ContinueProcess? = null | |
| 24 | 22 | |
| 25 | 23 | init { |
| 26 | - scope.launch(Dispatchers.IO) { | |
| 27 | - try { | |
| 28 | - while (isActive) { | |
| 29 | - val line = reader.readLine() | |
| 30 | - if (line != null && line.isNotEmpty()) { | |
| 31 | - try { | |
| 32 | - log.debug("Handle: $line") | |
| 33 | - handleMessage(line) | |
| 34 | - } catch (e: Exception) { | |
| 35 | - service<ContinueSentryService>().report(e, "Error handling message: $line") | |
| 36 | - } | |
| 37 | - } else | |
| 38 | - delay(100) | |
| 39 | - } | |
| 40 | - } catch (e: IOException) { | |
| 41 | - service<ContinueSentryService>().report(e) | |
| 24 | + restart() | |
| 25 | + } | |
| 26 | + | |
| 27 | + fun restart() { | |
| 28 | + LOG.warn("Starting Continue process") | |
| 29 | + processScope?.cancel() | |
| 30 | + process?.close() | |
| 31 | + | |
| 32 | + val handler = CoroutineExceptionHandler { _, e -> | |
| 33 | + service<ContinueSentryService>().report(e) | |
| 34 | + service<ContinuePostHogService>().capture("jetbrains_core_exit", mapOf("error" to e)) | |
| 35 | + | |
| 36 | + val backoffDuration = backoff.nextDuration() | |
| 37 | + LOG.warn("Process failed! Restarting in $backoffDuration") | |
| 38 | + parentScope.launch { | |
| 39 | + delay(backoffDuration) | |
| 40 | + restart() | |
| 41 | + } | |
| 42 | + } | |
| 43 | + | |
| 44 | + val job = SupervisorJob(parentScope.coroutineContext.job) | |
| 45 | + processScope = CoroutineScope(parentScope.coroutineContext + job + handler) | |
| 46 | + process = createProcess() | |
| 47 | + | |
| 48 | + val reader = BufferedReader(InputStreamReader(process!!.input)) | |
| 49 | + val writer = OutputStreamWriter(process!!.output) | |
| 50 | + | |
| 51 | + processScope!!.launch(Dispatchers.IO) { | |
| 52 | + while (isActive) { | |
| 53 | + val line = reader.readLine() | |
| 54 | + if (line != null && line.isNotEmpty()) | |
| 55 | + readMessage(line) | |
| 42 | 56 | } |
| 43 | 57 | } |
| 44 | - scope.launch(Dispatchers.IO) { | |
| 58 | + processScope!!.launch(Dispatchers.IO) { | |
| 45 | 59 | for (message in pendingWrites) { |
| 46 | - log.debug("Write: $message") | |
| 47 | 60 | writer.write(message) |
| 48 | 61 | writer.write("\r\n") |
| 49 | 62 | writer.flush() |
| @@ -54,12 +67,7 @@ | ||
| 54 | 67 | fun write(message: String) = |
| 55 | 68 | pendingWrites.trySend(message) |
| 56 | 69 | |
| 57 | - fun close() { | |
| 58 | - innerJob.cancel() | |
| 59 | - scope.launch(Dispatchers.IO) { | |
| 60 | - reader.close() | |
| 61 | - writer.close() | |
| 62 | - process.close() | |
| 63 | - } | |
| 70 | + private companion object { | |
| 71 | + private val LOG = Logger.getInstance(ContinueProcessHandler::class.java.simpleName) | |
| 64 | 72 | } |
| 65 | 73 | } |
modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/proxy/ProxyPoolingActivity.kt +1 −1
| @@ -28,7 +28,7 @@ | ||
| 28 | 28 | |
| 29 | 29 | private fun onSettingsChanged(project: Project) { |
| 30 | 30 | log.warn("Proxy settings changed, restarting") |
| 31 | - project.service<ContinuePluginService>().coreMessengerManager?.coreMessenger?.restart() | |
| 31 | + project.service<ContinuePluginService>().coreMessenger?.restart() | |
| 32 | 32 | } |
| 33 | 33 | } |
| 34 | 34 |
modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/services/ContinuePluginService.kt +7 −17
| @@ -1,34 +1,28 @@ | ||
| 1 | 1 | package com.github.continuedev.continueintellijextension.services |
| 2 | 2 | |
| 3 | 3 | import com.github.continuedev.continueintellijextension.`continue`.CoreMessenger |
| 4 | -import com.github.continuedev.continueintellijextension.`continue`.CoreMessengerManager | |
| 5 | 4 | import com.github.continuedev.continueintellijextension.`continue`.DiffManager |
| 6 | 5 | import com.github.continuedev.continueintellijextension.`continue`.IdeProtocolClient |
| 7 | 6 | import com.github.continuedev.continueintellijextension.listeners.ActiveHandlerManager |
| 8 | 7 | import com.github.continuedev.continueintellijextension.listeners.DocumentChangeTracker |
| 9 | -import com.github.continuedev.continueintellijextension.toolWindow.ContinuePluginToolWindowFactory | |
| 10 | 8 | import com.github.continuedev.continueintellijextension.utils.uuid |
| 9 | +import com.intellij.openapi.Disposable | |
| 11 | 10 | import com.intellij.openapi.components.Service |
| 12 | 11 | import com.intellij.openapi.components.service |
| 13 | 12 | import com.intellij.openapi.editor.EditorFactory |
| 14 | 13 | import com.intellij.openapi.project.DumbAware |
| 15 | 14 | import com.intellij.openapi.project.Project |
| 16 | 15 | import com.intellij.openapi.util.Disposer |
| 17 | -import kotlinx.coroutines.CoroutineScope | |
| 18 | -import kotlinx.coroutines.Dispatchers | |
| 19 | 16 | import kotlinx.coroutines.cancel |
| 20 | 17 | import kotlin.properties.Delegates |
| 21 | 18 | |
| 22 | 19 | @Service(Service.Level.PROJECT) |
| 23 | -class ContinuePluginService(private val project: Project) : DumbAware { | |
| 24 | - private val coroutineScope = CoroutineScope(Dispatchers.Main) | |
| 20 | +class ContinuePluginService(private val project: Project) : Disposable, DumbAware { | |
| 25 | 21 | var listener: (() -> Unit)? = null |
| 26 | 22 | var ideProtocolClient: IdeProtocolClient? by Delegates.observable(null) { _, _, _ -> |
| 27 | 23 | synchronized(this) { listener?.also { listener = null }?.invoke() } |
| 28 | 24 | } |
| 29 | - var coreMessengerManager: CoreMessengerManager? = null | |
| 30 | - val coreMessenger: CoreMessenger? | |
| 31 | - get() = coreMessengerManager?.coreMessenger | |
| 25 | + var coreMessenger: CoreMessenger? = null | |
| 32 | 26 | var workspacePaths: Array<String>? = null |
| 33 | 27 | var windowId: String = uuid() |
| 34 | 28 | var diffManager: DiffManager? = null |
| @@ -57,14 +51,6 @@ | ||
| 57 | 51 | } |
| 58 | 52 | } |
| 59 | 53 | |
| 60 | - fun dispose() { | |
| 61 | - coroutineScope.cancel() | |
| 62 | - coreMessenger?.coroutineScope?.let { | |
| 63 | - it.cancel() | |
| 64 | - coreMessenger?.close() | |
| 65 | - } | |
| 66 | - } | |
| 67 | - | |
| 68 | 54 | /** |
| 69 | 55 | * Add a listener for protocolClient initialization. |
| 70 | 56 | * Currently, only one needs to be processed. If there are more than one, |
| @@ -83,4 +69,8 @@ | ||
| 83 | 69 | listener() |
| 84 | 70 | } |
| 85 | 71 | } |
| 72 | + | |
| 73 | + override fun dispose() { | |
| 74 | + coreMessenger?.coroutineScope?.cancel() | |
| 75 | + } | |
| 86 | 76 | } |
| No newline at the end of the file |
added extensions/intellij/src/test/kotlin/com/github/continuedev/continueintellijextension/unit/BackoffCalculatorTest.kt +20 −0
| @@ -0,0 +1,20 @@ | ||
| 1 | +package com.github.continuedev.continueintellijextension.unit | |
| 2 | + | |
| 3 | +import com.github.continuedev.continueintellijextension.`continue`.process.BackoffCalculator | |
| 4 | +import junit.framework.TestCase | |
| 5 | +import kotlin.time.Duration.Companion.seconds | |
| 6 | + | |
| 7 | +class BackoffCalculatorTest : TestCase() { | |
| 8 | + | |
| 9 | + fun `test backoff`() { | |
| 10 | + val backoff = BackoffCalculator(initialDuration = 1.seconds, maxDuration = 30.seconds) | |
| 11 | + assertEquals(1.seconds, backoff.nextDuration()) | |
| 12 | + assertEquals(2.seconds, backoff.nextDuration()) | |
| 13 | + assertEquals(4.seconds, backoff.nextDuration()) | |
| 14 | + assertEquals(8.seconds, backoff.nextDuration()) | |
| 15 | + assertEquals(16.seconds, backoff.nextDuration()) | |
| 16 | + assertEquals(30.seconds, backoff.nextDuration()) | |
| 17 | + assertEquals(30.seconds, backoff.nextDuration()) | |
| 18 | + } | |
| 19 | + | |
| 20 | +} | |
| No newline at the end of the file |